1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * compress_core.c - compress offload core
4 *
5 * Copyright (C) 2011 Intel Corporation
6 * Authors: Vinod Koul <vinod.koul@linux.intel.com>
7 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
14
15 #include <linux/file.h>
16 #include <linux/fs.h>
17 #include <linux/list.h>
18 #include <linux/math64.h>
19 #include <linux/mm.h>
20 #include <linux/mutex.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/types.h>
25 #include <linux/uio.h>
26 #include <linux/uaccess.h>
27 #include <linux/module.h>
28 #include <linux/compat.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
31 #include <sound/info.h>
32 #include <sound/compress_params.h>
33 #include <sound/compress_offload.h>
34 #include <sound/compress_driver.h>
35
36 #ifndef __GENKSYMS__
37 #include <trace/hooks/snd_compr.h>
38 #endif
39
40 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
41 * architectures, so we need to disable the relevant ioctls.
42 */
43 #if _IOC_SIZEBITS < 14
44 #define COMPR_CODEC_CAPS_OVERFLOW
45 #endif
46
47 /* TODO:
48 * - add substream support for multiple devices in case of
49 * SND_DYNAMIC_MINORS is not used
50 * - Multiple node representation
51 * driver should be able to register multiple nodes
52 */
53
54 static DEFINE_MUTEX(device_mutex);
55
56 struct snd_compr_file {
57 unsigned long caps;
58 struct snd_compr_stream stream;
59 };
60
61 static void error_delayed_work(struct work_struct *work);
62
63 /*
64 * a note on stream states used:
65 * we use following states in the compressed core
66 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
67 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
68 * calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
69 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
70 * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
71 * playback only). User after setting up stream writes the data buffer
72 * before starting the stream.
73 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
74 * decoding/encoding and rendering/capturing data.
75 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
76 * by calling SNDRV_COMPRESS_DRAIN.
77 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
78 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
79 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
80 */
snd_compr_open(struct inode * inode,struct file * f)81 static int snd_compr_open(struct inode *inode, struct file *f)
82 {
83 struct snd_compr *compr;
84 struct snd_compr_file *data;
85 struct snd_compr_runtime *runtime;
86 enum snd_compr_direction dirn;
87 int maj = imajor(inode);
88 int ret;
89
90 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
91 dirn = SND_COMPRESS_PLAYBACK;
92 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
93 dirn = SND_COMPRESS_CAPTURE;
94 else
95 return -EINVAL;
96
97 if (maj == snd_major)
98 compr = snd_lookup_minor_data(iminor(inode),
99 SNDRV_DEVICE_TYPE_COMPRESS);
100 else
101 return -EBADFD;
102
103 if (compr == NULL) {
104 pr_err("no device data!!!\n");
105 return -ENODEV;
106 }
107
108 if (dirn != compr->direction) {
109 pr_err("this device doesn't support this direction\n");
110 snd_card_unref(compr->card);
111 return -EINVAL;
112 }
113
114 data = kzalloc(sizeof(*data), GFP_KERNEL);
115 if (!data) {
116 snd_card_unref(compr->card);
117 return -ENOMEM;
118 }
119
120 INIT_DELAYED_WORK(&data->stream.error_work, error_delayed_work);
121
122 data->stream.ops = compr->ops;
123 data->stream.direction = dirn;
124 data->stream.private_data = compr->private_data;
125 data->stream.device = compr;
126 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
127 if (!runtime) {
128 kfree(data);
129 snd_card_unref(compr->card);
130 return -ENOMEM;
131 }
132 runtime->state = SNDRV_PCM_STATE_OPEN;
133 init_waitqueue_head(&runtime->sleep);
134 data->stream.runtime = runtime;
135 f->private_data = (void *)data;
136 mutex_lock(&compr->lock);
137 ret = compr->ops->open(&data->stream);
138 mutex_unlock(&compr->lock);
139 if (ret) {
140 kfree(runtime);
141 kfree(data);
142 }
143 snd_card_unref(compr->card);
144 return ret;
145 }
146
snd_compr_free(struct inode * inode,struct file * f)147 static int snd_compr_free(struct inode *inode, struct file *f)
148 {
149 struct snd_compr_file *data = f->private_data;
150 struct snd_compr_runtime *runtime = data->stream.runtime;
151
152 cancel_delayed_work_sync(&data->stream.error_work);
153
154 switch (runtime->state) {
155 case SNDRV_PCM_STATE_RUNNING:
156 case SNDRV_PCM_STATE_DRAINING:
157 case SNDRV_PCM_STATE_PAUSED:
158 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
159 break;
160 default:
161 break;
162 }
163
164 data->stream.ops->free(&data->stream);
165 if (!data->stream.runtime->dma_buffer_p)
166 kfree(data->stream.runtime->buffer);
167 kfree(data->stream.runtime);
168 kfree(data);
169 return 0;
170 }
171
snd_compr_update_tstamp(struct snd_compr_stream * stream,struct snd_compr_tstamp * tstamp)172 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
173 struct snd_compr_tstamp *tstamp)
174 {
175 if (!stream->ops->pointer)
176 return -ENOTSUPP;
177 stream->ops->pointer(stream, tstamp);
178 pr_debug("dsp consumed till %d total %d bytes\n",
179 tstamp->byte_offset, tstamp->copied_total);
180 if (stream->direction == SND_COMPRESS_PLAYBACK)
181 stream->runtime->total_bytes_transferred = tstamp->copied_total;
182 else
183 stream->runtime->total_bytes_available = tstamp->copied_total;
184 return 0;
185 }
186
snd_compr_calc_avail(struct snd_compr_stream * stream,struct snd_compr_avail * avail)187 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
188 struct snd_compr_avail *avail)
189 {
190 memset(avail, 0, sizeof(*avail));
191 snd_compr_update_tstamp(stream, &avail->tstamp);
192 /* Still need to return avail even if tstamp can't be filled in */
193
194 if (stream->runtime->total_bytes_available == 0 &&
195 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
196 stream->direction == SND_COMPRESS_PLAYBACK) {
197 pr_debug("detected init and someone forgot to do a write\n");
198 return stream->runtime->buffer_size;
199 }
200 pr_debug("app wrote %lld, DSP consumed %lld\n",
201 stream->runtime->total_bytes_available,
202 stream->runtime->total_bytes_transferred);
203 if (stream->runtime->total_bytes_available ==
204 stream->runtime->total_bytes_transferred) {
205 if (stream->direction == SND_COMPRESS_PLAYBACK) {
206 pr_debug("both pointers are same, returning full avail\n");
207 return stream->runtime->buffer_size;
208 } else {
209 pr_debug("both pointers are same, returning no avail\n");
210 return 0;
211 }
212 }
213
214 avail->avail = stream->runtime->total_bytes_available -
215 stream->runtime->total_bytes_transferred;
216 if (stream->direction == SND_COMPRESS_PLAYBACK)
217 avail->avail = stream->runtime->buffer_size - avail->avail;
218
219 pr_debug("ret avail as %lld\n", avail->avail);
220 return avail->avail;
221 }
222
snd_compr_get_avail(struct snd_compr_stream * stream)223 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
224 {
225 struct snd_compr_avail avail;
226
227 return snd_compr_calc_avail(stream, &avail);
228 }
229
230 static int
snd_compr_ioctl_avail(struct snd_compr_stream * stream,unsigned long arg)231 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
232 {
233 struct snd_compr_avail ioctl_avail;
234 size_t avail;
235
236 avail = snd_compr_calc_avail(stream, &ioctl_avail);
237 ioctl_avail.avail = avail;
238
239 switch (stream->runtime->state) {
240 case SNDRV_PCM_STATE_OPEN:
241 return -EBADFD;
242 case SNDRV_PCM_STATE_XRUN:
243 return -EPIPE;
244 default:
245 break;
246 }
247
248 if (copy_to_user((__u64 __user *)arg,
249 &ioctl_avail, sizeof(ioctl_avail)))
250 return -EFAULT;
251 return 0;
252 }
253
snd_compr_write_data(struct snd_compr_stream * stream,const char __user * buf,size_t count)254 static int snd_compr_write_data(struct snd_compr_stream *stream,
255 const char __user *buf, size_t count)
256 {
257 void *dstn;
258 size_t copy;
259 struct snd_compr_runtime *runtime = stream->runtime;
260 /* 64-bit Modulus */
261 u64 app_pointer = div64_u64(runtime->total_bytes_available,
262 runtime->buffer_size);
263 app_pointer = runtime->total_bytes_available -
264 (app_pointer * runtime->buffer_size);
265
266 dstn = runtime->buffer + app_pointer;
267 pr_debug("copying %ld at %lld\n",
268 (unsigned long)count, app_pointer);
269 if (count < runtime->buffer_size - app_pointer) {
270 if (copy_from_user(dstn, buf, count))
271 return -EFAULT;
272 } else {
273 copy = runtime->buffer_size - app_pointer;
274 if (copy_from_user(dstn, buf, copy))
275 return -EFAULT;
276 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
277 return -EFAULT;
278 }
279 /* if DSP cares, let it know data has been written */
280 if (stream->ops->ack)
281 stream->ops->ack(stream, count);
282 return count;
283 }
284
snd_compr_write(struct file * f,const char __user * buf,size_t count,loff_t * offset)285 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
286 size_t count, loff_t *offset)
287 {
288 struct snd_compr_file *data = f->private_data;
289 struct snd_compr_stream *stream;
290 size_t avail;
291 int retval;
292
293 if (snd_BUG_ON(!data))
294 return -EFAULT;
295
296 stream = &data->stream;
297 mutex_lock(&stream->device->lock);
298 /* write is allowed when stream is running or has been steup */
299 switch (stream->runtime->state) {
300 case SNDRV_PCM_STATE_SETUP:
301 case SNDRV_PCM_STATE_PREPARED:
302 case SNDRV_PCM_STATE_RUNNING:
303 break;
304 default:
305 mutex_unlock(&stream->device->lock);
306 return -EBADFD;
307 }
308
309 avail = snd_compr_get_avail(stream);
310 pr_debug("avail returned %ld\n", (unsigned long)avail);
311 /* calculate how much we can write to buffer */
312 if (avail > count)
313 avail = count;
314
315 if (stream->ops->copy) {
316 char __user* cbuf = (char __user*)buf;
317 retval = stream->ops->copy(stream, cbuf, avail);
318 } else {
319 retval = snd_compr_write_data(stream, buf, avail);
320 }
321 if (retval > 0)
322 stream->runtime->total_bytes_available += retval;
323
324 /* while initiating the stream, write should be called before START
325 * call, so in setup move state */
326 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
327 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
328 pr_debug("stream prepared, Houston we are good to go\n");
329 }
330
331 mutex_unlock(&stream->device->lock);
332 return retval;
333 }
334
335
snd_compr_read(struct file * f,char __user * buf,size_t count,loff_t * offset)336 static ssize_t snd_compr_read(struct file *f, char __user *buf,
337 size_t count, loff_t *offset)
338 {
339 struct snd_compr_file *data = f->private_data;
340 struct snd_compr_stream *stream;
341 size_t avail;
342 int retval;
343
344 if (snd_BUG_ON(!data))
345 return -EFAULT;
346
347 stream = &data->stream;
348 mutex_lock(&stream->device->lock);
349
350 /* read is allowed when stream is running, paused, draining and setup
351 * (yes setup is state which we transition to after stop, so if user
352 * wants to read data after stop we allow that)
353 */
354 switch (stream->runtime->state) {
355 case SNDRV_PCM_STATE_OPEN:
356 case SNDRV_PCM_STATE_PREPARED:
357 case SNDRV_PCM_STATE_SUSPENDED:
358 case SNDRV_PCM_STATE_DISCONNECTED:
359 retval = -EBADFD;
360 goto out;
361 case SNDRV_PCM_STATE_XRUN:
362 retval = -EPIPE;
363 goto out;
364 }
365
366 avail = snd_compr_get_avail(stream);
367 pr_debug("avail returned %ld\n", (unsigned long)avail);
368 /* calculate how much we can read from buffer */
369 if (avail > count)
370 avail = count;
371
372 if (stream->ops->copy) {
373 retval = stream->ops->copy(stream, buf, avail);
374 } else {
375 retval = -ENXIO;
376 goto out;
377 }
378 if (retval > 0)
379 stream->runtime->total_bytes_transferred += retval;
380
381 out:
382 mutex_unlock(&stream->device->lock);
383 return retval;
384 }
385
snd_compr_mmap(struct file * f,struct vm_area_struct * vma)386 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
387 {
388 return -ENXIO;
389 }
390
snd_compr_get_poll(struct snd_compr_stream * stream)391 static __poll_t snd_compr_get_poll(struct snd_compr_stream *stream)
392 {
393 if (stream->direction == SND_COMPRESS_PLAYBACK)
394 return EPOLLOUT | EPOLLWRNORM;
395 else
396 return EPOLLIN | EPOLLRDNORM;
397 }
398
snd_compr_poll(struct file * f,poll_table * wait)399 static __poll_t snd_compr_poll(struct file *f, poll_table *wait)
400 {
401 struct snd_compr_file *data = f->private_data;
402 struct snd_compr_stream *stream;
403 size_t avail;
404 __poll_t retval = 0;
405
406 if (snd_BUG_ON(!data))
407 return EPOLLERR;
408
409 stream = &data->stream;
410
411 mutex_lock(&stream->device->lock);
412
413 switch (stream->runtime->state) {
414 case SNDRV_PCM_STATE_OPEN:
415 case SNDRV_PCM_STATE_XRUN:
416 retval = snd_compr_get_poll(stream) | EPOLLERR;
417 goto out;
418 default:
419 break;
420 }
421
422 poll_wait(f, &stream->runtime->sleep, wait);
423
424 avail = snd_compr_get_avail(stream);
425 pr_debug("avail is %ld\n", (unsigned long)avail);
426 /* check if we have at least one fragment to fill */
427 switch (stream->runtime->state) {
428 case SNDRV_PCM_STATE_DRAINING:
429 /* stream has been woken up after drain is complete
430 * draining done so set stream state to stopped
431 */
432 retval = snd_compr_get_poll(stream);
433 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
434 break;
435 case SNDRV_PCM_STATE_RUNNING:
436 case SNDRV_PCM_STATE_PREPARED:
437 case SNDRV_PCM_STATE_PAUSED:
438 if (avail >= stream->runtime->fragment_size)
439 retval = snd_compr_get_poll(stream);
440 break;
441 default:
442 retval = snd_compr_get_poll(stream) | EPOLLERR;
443 break;
444 }
445 out:
446 mutex_unlock(&stream->device->lock);
447 return retval;
448 }
449
450 static int
snd_compr_get_caps(struct snd_compr_stream * stream,unsigned long arg)451 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
452 {
453 int retval;
454 struct snd_compr_caps caps;
455
456 if (!stream->ops->get_caps)
457 return -ENXIO;
458
459 memset(&caps, 0, sizeof(caps));
460 retval = stream->ops->get_caps(stream, &caps);
461 if (retval)
462 goto out;
463 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
464 retval = -EFAULT;
465 out:
466 return retval;
467 }
468
469 #ifndef COMPR_CODEC_CAPS_OVERFLOW
470 static int
snd_compr_get_codec_caps(struct snd_compr_stream * stream,unsigned long arg)471 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
472 {
473 int retval;
474 struct snd_compr_codec_caps *caps;
475
476 if (!stream->ops->get_codec_caps)
477 return -ENXIO;
478
479 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
480 if (!caps)
481 return -ENOMEM;
482
483 retval = stream->ops->get_codec_caps(stream, caps);
484 if (retval)
485 goto out;
486 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
487 retval = -EFAULT;
488
489 out:
490 kfree(caps);
491 return retval;
492 }
493 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
494
snd_compr_malloc_pages(struct snd_compr_stream * stream,size_t size)495 int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size)
496 {
497 struct snd_dma_buffer *dmab;
498 int ret;
499
500 if (snd_BUG_ON(!(stream) || !(stream)->runtime))
501 return -EINVAL;
502 dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
503 if (!dmab)
504 return -ENOMEM;
505 dmab->dev = stream->dma_buffer.dev;
506 ret = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev, size, dmab);
507 if (ret < 0) {
508 kfree(dmab);
509 return ret;
510 }
511
512 snd_compr_set_runtime_buffer(stream, dmab);
513 stream->runtime->dma_bytes = size;
514 return 1;
515 }
516 EXPORT_SYMBOL(snd_compr_malloc_pages);
517
snd_compr_free_pages(struct snd_compr_stream * stream)518 int snd_compr_free_pages(struct snd_compr_stream *stream)
519 {
520 struct snd_compr_runtime *runtime;
521
522 if (snd_BUG_ON(!(stream) || !(stream)->runtime))
523 return -EINVAL;
524 runtime = stream->runtime;
525 if (runtime->dma_area == NULL)
526 return 0;
527 if (runtime->dma_buffer_p != &stream->dma_buffer) {
528 /* It's a newly allocated buffer. Release it now. */
529 snd_dma_free_pages(runtime->dma_buffer_p);
530 kfree(runtime->dma_buffer_p);
531 }
532
533 snd_compr_set_runtime_buffer(stream, NULL);
534 return 0;
535 }
536 EXPORT_SYMBOL(snd_compr_free_pages);
537
538 /* revisit this with snd_pcm_preallocate_xxx */
snd_compr_allocate_buffer(struct snd_compr_stream * stream,struct snd_compr_params * params)539 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
540 struct snd_compr_params *params)
541 {
542 unsigned int buffer_size;
543 void *buffer = NULL;
544
545 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
546 if (stream->ops->copy) {
547 buffer = NULL;
548 /* if copy is defined the driver will be required to copy
549 * the data from core
550 */
551 } else {
552 if (stream->runtime->dma_buffer_p) {
553
554 if (buffer_size > stream->runtime->dma_buffer_p->bytes)
555 dev_err(&stream->device->dev,
556 "Not enough DMA buffer");
557 else
558 buffer = stream->runtime->dma_buffer_p->area;
559
560 } else {
561 buffer = kmalloc(buffer_size, GFP_KERNEL);
562 }
563
564 if (!buffer)
565 return -ENOMEM;
566 }
567 stream->runtime->fragment_size = params->buffer.fragment_size;
568 stream->runtime->fragments = params->buffer.fragments;
569 stream->runtime->buffer = buffer;
570 stream->runtime->buffer_size = buffer_size;
571 return 0;
572 }
573
snd_compress_check_input(struct snd_compr_params * params)574 static int snd_compress_check_input(struct snd_compr_params *params)
575 {
576 /* first let's check the buffer parameter's */
577 if (params->buffer.fragment_size == 0 ||
578 params->buffer.fragments > U32_MAX / params->buffer.fragment_size ||
579 params->buffer.fragments == 0)
580 return -EINVAL;
581
582 /* now codec parameters */
583 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
584 return -EINVAL;
585
586 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
587 return -EINVAL;
588
589 return 0;
590 }
591
592 static int
snd_compr_set_params(struct snd_compr_stream * stream,unsigned long arg)593 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
594 {
595 struct snd_compr_params *params;
596 int retval;
597
598 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
599 /*
600 * we should allow parameter change only when stream has been
601 * opened not in other cases
602 */
603 params = memdup_user((void __user *)arg, sizeof(*params));
604 if (IS_ERR(params))
605 return PTR_ERR(params);
606
607 retval = snd_compress_check_input(params);
608 if (retval)
609 goto out;
610
611 retval = snd_compr_allocate_buffer(stream, params);
612 if (retval) {
613 retval = -ENOMEM;
614 goto out;
615 }
616
617 retval = stream->ops->set_params(stream, params);
618 if (retval)
619 goto out;
620
621 stream->metadata_set = false;
622 stream->next_track = false;
623
624 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
625 } else {
626 return -EPERM;
627 }
628 out:
629 kfree(params);
630 return retval;
631 }
632
633 static int
snd_compr_get_params(struct snd_compr_stream * stream,unsigned long arg)634 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
635 {
636 struct snd_codec *params;
637 int retval;
638
639 if (!stream->ops->get_params)
640 return -EBADFD;
641
642 params = kzalloc(sizeof(*params), GFP_KERNEL);
643 if (!params)
644 return -ENOMEM;
645 retval = stream->ops->get_params(stream, params);
646 if (retval)
647 goto out;
648 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
649 retval = -EFAULT;
650
651 out:
652 kfree(params);
653 return retval;
654 }
655
656 static int
snd_compr_get_metadata(struct snd_compr_stream * stream,unsigned long arg)657 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
658 {
659 struct snd_compr_metadata metadata;
660 int retval;
661
662 if (!stream->ops->get_metadata)
663 return -ENXIO;
664
665 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
666 return -EFAULT;
667
668 retval = stream->ops->get_metadata(stream, &metadata);
669 if (retval != 0)
670 return retval;
671
672 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
673 return -EFAULT;
674
675 return 0;
676 }
677
678 static int
snd_compr_set_metadata(struct snd_compr_stream * stream,unsigned long arg)679 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
680 {
681 struct snd_compr_metadata metadata;
682 int retval;
683
684 if (!stream->ops->set_metadata)
685 return -ENXIO;
686 /*
687 * we should allow parameter change only when stream has been
688 * opened not in other cases
689 */
690 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
691 return -EFAULT;
692
693 retval = stream->ops->set_metadata(stream, &metadata);
694 stream->metadata_set = true;
695
696 return retval;
697 }
698
699 static inline int
snd_compr_tstamp(struct snd_compr_stream * stream,unsigned long arg)700 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
701 {
702 struct snd_compr_tstamp tstamp = {0};
703 int ret;
704
705 ret = snd_compr_update_tstamp(stream, &tstamp);
706 if (ret == 0)
707 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
708 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
709 return ret;
710 }
711
snd_compr_pause(struct snd_compr_stream * stream)712 static int snd_compr_pause(struct snd_compr_stream *stream)
713 {
714 int retval;
715 bool use_pause_in_drain = false;
716 bool leave_draining_state = false;
717
718 trace_android_vh_snd_compr_use_pause_in_drain(&use_pause_in_drain,
719 &leave_draining_state);
720
721 if (use_pause_in_drain && stream->runtime->state == SNDRV_PCM_STATE_DRAINING) {
722 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
723 if (!retval && leave_draining_state) {
724 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
725 wake_up(&stream->runtime->sleep);
726 }
727 return retval;
728 }
729
730 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
731 return -EPERM;
732 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
733 if (!retval)
734 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
735 return retval;
736 }
737
snd_compr_resume(struct snd_compr_stream * stream)738 static int snd_compr_resume(struct snd_compr_stream *stream)
739 {
740 int retval;
741 bool use_pause_in_drain = false;
742 bool leave_draining_state = false;
743
744 trace_android_vh_snd_compr_use_pause_in_drain(&use_pause_in_drain,
745 &leave_draining_state);
746
747 if (use_pause_in_drain && stream->runtime->state == SNDRV_PCM_STATE_DRAINING)
748 return stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
749
750 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
751 return -EPERM;
752 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
753 if (!retval)
754 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
755 return retval;
756 }
757
snd_compr_start(struct snd_compr_stream * stream)758 static int snd_compr_start(struct snd_compr_stream *stream)
759 {
760 int retval;
761
762 switch (stream->runtime->state) {
763 case SNDRV_PCM_STATE_SETUP:
764 if (stream->direction != SND_COMPRESS_CAPTURE)
765 return -EPERM;
766 break;
767 case SNDRV_PCM_STATE_PREPARED:
768 break;
769 default:
770 return -EPERM;
771 }
772
773 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
774 if (!retval)
775 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
776 return retval;
777 }
778
snd_compr_stop(struct snd_compr_stream * stream)779 static int snd_compr_stop(struct snd_compr_stream *stream)
780 {
781 int retval;
782
783 switch (stream->runtime->state) {
784 case SNDRV_PCM_STATE_OPEN:
785 case SNDRV_PCM_STATE_SETUP:
786 case SNDRV_PCM_STATE_PREPARED:
787 return -EPERM;
788 default:
789 break;
790 }
791
792 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
793 if (!retval) {
794 /* clear flags and stop any drain wait */
795 stream->partial_drain = false;
796 stream->metadata_set = false;
797 snd_compr_drain_notify(stream);
798 stream->runtime->total_bytes_available = 0;
799 stream->runtime->total_bytes_transferred = 0;
800 }
801 return retval;
802 }
803
error_delayed_work(struct work_struct * work)804 static void error_delayed_work(struct work_struct *work)
805 {
806 struct snd_compr_stream *stream;
807
808 stream = container_of(work, struct snd_compr_stream, error_work.work);
809
810 mutex_lock(&stream->device->lock);
811
812 stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
813 wake_up(&stream->runtime->sleep);
814
815 mutex_unlock(&stream->device->lock);
816 }
817
818 /*
819 * snd_compr_stop_error: Report a fatal error on a stream
820 * @stream: pointer to stream
821 * @state: state to transition the stream to
822 *
823 * Stop the stream and set its state.
824 *
825 * Should be called with compressed device lock held.
826 */
snd_compr_stop_error(struct snd_compr_stream * stream,snd_pcm_state_t state)827 int snd_compr_stop_error(struct snd_compr_stream *stream,
828 snd_pcm_state_t state)
829 {
830 if (stream->runtime->state == state)
831 return 0;
832
833 stream->runtime->state = state;
834
835 pr_debug("Changing state to: %d\n", state);
836
837 queue_delayed_work(system_power_efficient_wq, &stream->error_work, 0);
838
839 return 0;
840 }
841 EXPORT_SYMBOL_GPL(snd_compr_stop_error);
842
snd_compress_wait_for_drain(struct snd_compr_stream * stream)843 static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
844 {
845 int ret;
846
847 /*
848 * We are called with lock held. So drop the lock while we wait for
849 * drain complete notification from the driver
850 *
851 * It is expected that driver will notify the drain completion and then
852 * stream will be moved to SETUP state, even if draining resulted in an
853 * error. We can trigger next track after this.
854 */
855 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
856 mutex_unlock(&stream->device->lock);
857
858 /* we wait for drain to complete here, drain can return when
859 * interruption occurred, wait returned error or success.
860 * For the first two cases we don't do anything different here and
861 * return after waking up
862 */
863
864 ret = wait_event_interruptible(stream->runtime->sleep,
865 (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
866 if (ret == -ERESTARTSYS)
867 pr_debug("wait aborted by a signal\n");
868 else if (ret)
869 pr_debug("wait for drain failed with %d\n", ret);
870
871
872 wake_up(&stream->runtime->sleep);
873 mutex_lock(&stream->device->lock);
874
875 return ret;
876 }
877
snd_compr_drain(struct snd_compr_stream * stream)878 static int snd_compr_drain(struct snd_compr_stream *stream)
879 {
880 int retval;
881
882 switch (stream->runtime->state) {
883 case SNDRV_PCM_STATE_OPEN:
884 case SNDRV_PCM_STATE_SETUP:
885 case SNDRV_PCM_STATE_PREPARED:
886 case SNDRV_PCM_STATE_PAUSED:
887 return -EPERM;
888 case SNDRV_PCM_STATE_XRUN:
889 return -EPIPE;
890 default:
891 break;
892 }
893
894 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
895 if (retval) {
896 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
897 wake_up(&stream->runtime->sleep);
898 return retval;
899 }
900
901 return snd_compress_wait_for_drain(stream);
902 }
903
snd_compr_next_track(struct snd_compr_stream * stream)904 static int snd_compr_next_track(struct snd_compr_stream *stream)
905 {
906 int retval;
907
908 /* only a running stream can transition to next track */
909 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
910 return -EPERM;
911
912 /* next track doesn't have any meaning for capture streams */
913 if (stream->direction == SND_COMPRESS_CAPTURE)
914 return -EPERM;
915
916 /* you can signal next track if this is intended to be a gapless stream
917 * and current track metadata is set
918 */
919 if (stream->metadata_set == false)
920 return -EPERM;
921
922 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
923 if (retval != 0)
924 return retval;
925 stream->metadata_set = false;
926 stream->next_track = true;
927 return 0;
928 }
929
snd_compr_partial_drain(struct snd_compr_stream * stream)930 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
931 {
932 int retval;
933
934 switch (stream->runtime->state) {
935 case SNDRV_PCM_STATE_OPEN:
936 case SNDRV_PCM_STATE_SETUP:
937 case SNDRV_PCM_STATE_PREPARED:
938 case SNDRV_PCM_STATE_PAUSED:
939 return -EPERM;
940 case SNDRV_PCM_STATE_XRUN:
941 return -EPIPE;
942 default:
943 break;
944 }
945
946 /* partial drain doesn't have any meaning for capture streams */
947 if (stream->direction == SND_COMPRESS_CAPTURE)
948 return -EPERM;
949
950 /* stream can be drained only when next track has been signalled */
951 if (stream->next_track == false)
952 return -EPERM;
953
954 stream->partial_drain = true;
955 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
956 if (retval) {
957 pr_debug("Partial drain returned failure\n");
958 wake_up(&stream->runtime->sleep);
959 return retval;
960 }
961
962 stream->next_track = false;
963 return snd_compress_wait_for_drain(stream);
964 }
965
snd_compr_ioctl(struct file * f,unsigned int cmd,unsigned long arg)966 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
967 {
968 struct snd_compr_file *data = f->private_data;
969 struct snd_compr_stream *stream;
970 int retval = -ENOTTY;
971
972 if (snd_BUG_ON(!data))
973 return -EFAULT;
974
975 stream = &data->stream;
976
977 mutex_lock(&stream->device->lock);
978 switch (_IOC_NR(cmd)) {
979 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
980 retval = put_user(SNDRV_COMPRESS_VERSION,
981 (int __user *)arg) ? -EFAULT : 0;
982 break;
983 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
984 retval = snd_compr_get_caps(stream, arg);
985 break;
986 #ifndef COMPR_CODEC_CAPS_OVERFLOW
987 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
988 retval = snd_compr_get_codec_caps(stream, arg);
989 break;
990 #endif
991 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
992 retval = snd_compr_set_params(stream, arg);
993 break;
994 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
995 retval = snd_compr_get_params(stream, arg);
996 break;
997 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
998 retval = snd_compr_set_metadata(stream, arg);
999 break;
1000 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
1001 retval = snd_compr_get_metadata(stream, arg);
1002 break;
1003 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
1004 retval = snd_compr_tstamp(stream, arg);
1005 break;
1006 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
1007 retval = snd_compr_ioctl_avail(stream, arg);
1008 break;
1009 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
1010 retval = snd_compr_pause(stream);
1011 break;
1012 case _IOC_NR(SNDRV_COMPRESS_RESUME):
1013 retval = snd_compr_resume(stream);
1014 break;
1015 case _IOC_NR(SNDRV_COMPRESS_START):
1016 retval = snd_compr_start(stream);
1017 break;
1018 case _IOC_NR(SNDRV_COMPRESS_STOP):
1019 retval = snd_compr_stop(stream);
1020 break;
1021 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
1022 retval = snd_compr_drain(stream);
1023 break;
1024 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
1025 retval = snd_compr_partial_drain(stream);
1026 break;
1027 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
1028 retval = snd_compr_next_track(stream);
1029 break;
1030
1031 }
1032 mutex_unlock(&stream->device->lock);
1033 return retval;
1034 }
1035
1036 /* support of 32bit userspace on 64bit platforms */
1037 #ifdef CONFIG_COMPAT
snd_compr_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)1038 static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
1039 unsigned long arg)
1040 {
1041 return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1042 }
1043 #endif
1044
1045 static const struct file_operations snd_compr_file_ops = {
1046 .owner = THIS_MODULE,
1047 .open = snd_compr_open,
1048 .release = snd_compr_free,
1049 .write = snd_compr_write,
1050 .read = snd_compr_read,
1051 .unlocked_ioctl = snd_compr_ioctl,
1052 #ifdef CONFIG_COMPAT
1053 .compat_ioctl = snd_compr_ioctl_compat,
1054 #endif
1055 .mmap = snd_compr_mmap,
1056 .poll = snd_compr_poll,
1057 };
1058
snd_compress_dev_register(struct snd_device * device)1059 static int snd_compress_dev_register(struct snd_device *device)
1060 {
1061 int ret;
1062 struct snd_compr *compr;
1063
1064 if (snd_BUG_ON(!device || !device->device_data))
1065 return -EBADFD;
1066 compr = device->device_data;
1067
1068 pr_debug("reg device %s, direction %d\n", compr->name,
1069 compr->direction);
1070 /* register compressed device */
1071 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
1072 compr->card, compr->device,
1073 &snd_compr_file_ops, compr, &compr->dev);
1074 if (ret < 0) {
1075 pr_err("snd_register_device failed %d\n", ret);
1076 return ret;
1077 }
1078 return ret;
1079
1080 }
1081
snd_compress_dev_disconnect(struct snd_device * device)1082 static int snd_compress_dev_disconnect(struct snd_device *device)
1083 {
1084 struct snd_compr *compr;
1085
1086 compr = device->device_data;
1087 snd_unregister_device(&compr->dev);
1088 return 0;
1089 }
1090
1091 #ifdef CONFIG_SND_VERBOSE_PROCFS
snd_compress_proc_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1092 static void snd_compress_proc_info_read(struct snd_info_entry *entry,
1093 struct snd_info_buffer *buffer)
1094 {
1095 struct snd_compr *compr = (struct snd_compr *)entry->private_data;
1096
1097 snd_iprintf(buffer, "card: %d\n", compr->card->number);
1098 snd_iprintf(buffer, "device: %d\n", compr->device);
1099 snd_iprintf(buffer, "stream: %s\n",
1100 compr->direction == SND_COMPRESS_PLAYBACK
1101 ? "PLAYBACK" : "CAPTURE");
1102 snd_iprintf(buffer, "id: %s\n", compr->id);
1103 }
1104
snd_compress_proc_init(struct snd_compr * compr)1105 static int snd_compress_proc_init(struct snd_compr *compr)
1106 {
1107 struct snd_info_entry *entry;
1108 char name[16];
1109
1110 sprintf(name, "compr%i", compr->device);
1111 entry = snd_info_create_card_entry(compr->card, name,
1112 compr->card->proc_root);
1113 if (!entry)
1114 return -ENOMEM;
1115 entry->mode = S_IFDIR | 0555;
1116 compr->proc_root = entry;
1117
1118 entry = snd_info_create_card_entry(compr->card, "info",
1119 compr->proc_root);
1120 if (entry)
1121 snd_info_set_text_ops(entry, compr,
1122 snd_compress_proc_info_read);
1123 compr->proc_info_entry = entry;
1124
1125 return 0;
1126 }
1127
snd_compress_proc_done(struct snd_compr * compr)1128 static void snd_compress_proc_done(struct snd_compr *compr)
1129 {
1130 snd_info_free_entry(compr->proc_info_entry);
1131 compr->proc_info_entry = NULL;
1132 snd_info_free_entry(compr->proc_root);
1133 compr->proc_root = NULL;
1134 }
1135
snd_compress_set_id(struct snd_compr * compr,const char * id)1136 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1137 {
1138 strlcpy(compr->id, id, sizeof(compr->id));
1139 }
1140 #else
snd_compress_proc_init(struct snd_compr * compr)1141 static inline int snd_compress_proc_init(struct snd_compr *compr)
1142 {
1143 return 0;
1144 }
1145
snd_compress_proc_done(struct snd_compr * compr)1146 static inline void snd_compress_proc_done(struct snd_compr *compr)
1147 {
1148 }
1149
snd_compress_set_id(struct snd_compr * compr,const char * id)1150 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1151 {
1152 }
1153 #endif
1154
snd_compress_dev_free(struct snd_device * device)1155 static int snd_compress_dev_free(struct snd_device *device)
1156 {
1157 struct snd_compr *compr;
1158
1159 compr = device->device_data;
1160 snd_compress_proc_done(compr);
1161 put_device(&compr->dev);
1162 return 0;
1163 }
1164
1165 /*
1166 * snd_compress_new: create new compress device
1167 * @card: sound card pointer
1168 * @device: device number
1169 * @dirn: device direction, should be of type enum snd_compr_direction
1170 * @compr: compress device pointer
1171 */
snd_compress_new(struct snd_card * card,int device,int dirn,const char * id,struct snd_compr * compr)1172 int snd_compress_new(struct snd_card *card, int device,
1173 int dirn, const char *id, struct snd_compr *compr)
1174 {
1175 static const struct snd_device_ops ops = {
1176 .dev_free = snd_compress_dev_free,
1177 .dev_register = snd_compress_dev_register,
1178 .dev_disconnect = snd_compress_dev_disconnect,
1179 };
1180 int ret;
1181
1182 compr->card = card;
1183 compr->device = device;
1184 compr->direction = dirn;
1185
1186 snd_compress_set_id(compr, id);
1187
1188 snd_device_initialize(&compr->dev, card);
1189 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
1190
1191 ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
1192 if (ret == 0)
1193 snd_compress_proc_init(compr);
1194
1195 return ret;
1196 }
1197 EXPORT_SYMBOL_GPL(snd_compress_new);
1198
snd_compress_add_device(struct snd_compr * device)1199 static int snd_compress_add_device(struct snd_compr *device)
1200 {
1201 int ret;
1202
1203 if (!device->card)
1204 return -EINVAL;
1205
1206 /* register the card */
1207 ret = snd_card_register(device->card);
1208 if (ret)
1209 goto out;
1210 return 0;
1211
1212 out:
1213 pr_err("failed with %d\n", ret);
1214 return ret;
1215
1216 }
1217
snd_compress_remove_device(struct snd_compr * device)1218 static int snd_compress_remove_device(struct snd_compr *device)
1219 {
1220 return snd_card_free(device->card);
1221 }
1222
1223 /**
1224 * snd_compress_register - register compressed device
1225 *
1226 * @device: compressed device to register
1227 */
snd_compress_register(struct snd_compr * device)1228 int snd_compress_register(struct snd_compr *device)
1229 {
1230 int retval;
1231
1232 if (device->name == NULL || device->ops == NULL)
1233 return -EINVAL;
1234
1235 pr_debug("Registering compressed device %s\n", device->name);
1236 if (snd_BUG_ON(!device->ops->open))
1237 return -EINVAL;
1238 if (snd_BUG_ON(!device->ops->free))
1239 return -EINVAL;
1240 if (snd_BUG_ON(!device->ops->set_params))
1241 return -EINVAL;
1242 if (snd_BUG_ON(!device->ops->trigger))
1243 return -EINVAL;
1244
1245 mutex_init(&device->lock);
1246
1247 /* register a compressed card */
1248 mutex_lock(&device_mutex);
1249 retval = snd_compress_add_device(device);
1250 mutex_unlock(&device_mutex);
1251 return retval;
1252 }
1253 EXPORT_SYMBOL_GPL(snd_compress_register);
1254
snd_compress_deregister(struct snd_compr * device)1255 int snd_compress_deregister(struct snd_compr *device)
1256 {
1257 pr_debug("Removing compressed device %s\n", device->name);
1258 mutex_lock(&device_mutex);
1259 snd_compress_remove_device(device);
1260 mutex_unlock(&device_mutex);
1261 return 0;
1262 }
1263 EXPORT_SYMBOL_GPL(snd_compress_deregister);
1264
1265 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1266 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1267 MODULE_LICENSE("GPL v2");
1268