Debian Codegraph @

sound/core/compress_offload.c

raw
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 <[email protected]>
7 * Pierre-Louis Bossart <[email protected]>
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/dma-buf.h>
28 #include <linux/module.h>
29 #include <linux/compat.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/info.h>
33 #include <sound/compress_params.h>
34 #include <sound/compress_offload.h>
35 #include <sound/compress_driver.h>
36
37 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
38 * architectures, so we need to disable the relevant ioctls.
39 */
40 #if _IOC_SIZEBITS < 14
41 #define COMPR_CODEC_CAPS_OVERFLOW
42 #endif
43
44 struct snd_compr_file {
45 unsigned long caps;
46 struct snd_compr_stream stream;
47 };
48
49 static void error_delayed_work(struct work_struct *work);
50
51 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
52 static void snd_compr_task_free_all(struct snd_compr_stream *stream);
53 #else
54 static inline void snd_compr_task_free_all(struct snd_compr_stream *stream) { }
55 #endif
56
57 /*
58 * a note on stream states used:
59 * we use following states in the compressed core
60 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
61 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
62 * calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
63 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
64 * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
65 * playback only). User after setting up stream writes the data buffer
66 * before starting the stream.
67 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
68 * decoding/encoding and rendering/capturing data.
69 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
70 * by calling SNDRV_COMPRESS_DRAIN.
71 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
72 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
73 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
74 */
75 static int snd_compr_open(struct inode *inode, struct file *f)
76 {
77 struct snd_compr *compr;
78 struct snd_compr_file *data;
79 struct snd_compr_runtime *runtime;
80 enum snd_compr_direction dirn;
81 int maj = imajor(inode);
82 int ret;
83
84 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
85 dirn = SND_COMPRESS_PLAYBACK;
86 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
87 dirn = SND_COMPRESS_CAPTURE;
88 else if ((f->f_flags & O_ACCMODE) == O_RDWR)
89 dirn = SND_COMPRESS_ACCEL;
90 else
91 return -EINVAL;
92
93 if (maj == snd_major)
94 compr = snd_lookup_minor_data(iminor(inode),
95 SNDRV_DEVICE_TYPE_COMPRESS);
96 else
97 return -EBADFD;
98
99 if (compr == NULL) {
100 pr_err("no device data!!!\n");
101 return -ENODEV;
102 }
103
104 if (dirn != compr->direction) {
105 pr_err("this device doesn't support this direction\n");
106 snd_card_unref(compr->card);
107 return -EINVAL;
108 }
109
110 data = kzalloc(sizeof(*data), GFP_KERNEL);
111 if (!data) {
112 snd_card_unref(compr->card);
113 return -ENOMEM;
114 }
115
116 INIT_DELAYED_WORK(&data->stream.error_work, error_delayed_work);
117
118 data->stream.ops = compr->ops;
119 data->stream.direction = dirn;
120 data->stream.private_data = compr->private_data;
121 data->stream.device = compr;
122 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
123 if (!runtime) {
124 kfree(data);
125 snd_card_unref(compr->card);
126 return -ENOMEM;
127 }
128 runtime->state = SNDRV_PCM_STATE_OPEN;
129 init_waitqueue_head(&runtime->sleep);
130 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
131 INIT_LIST_HEAD(&runtime->tasks);
132 #endif
133 data->stream.runtime = runtime;
134 f->private_data = (void *)data;
135 scoped_guard(mutex, &compr->lock)
136 ret = compr->ops->open(&data->stream);
137 if (ret) {
138 kfree(runtime);
139 kfree(data);
140 }
141 snd_card_unref(compr->card);
142 return ret;
143 }
144
145 static int snd_compr_free(struct inode *inode, struct file *f)
146 {
147 struct snd_compr_file *data = f->private_data;
148 struct snd_compr_runtime *runtime = data->stream.runtime;
149
150 cancel_delayed_work_sync(&data->stream.error_work);
151
152 switch (runtime->state) {
153 case SNDRV_PCM_STATE_RUNNING:
154 case SNDRV_PCM_STATE_DRAINING:
155 case SNDRV_PCM_STATE_PAUSED:
156 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
157 break;
158 default:
159 break;
160 }
161
162 snd_compr_task_free_all(&data->stream);
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
172 static void
173 snd_compr_tstamp32_from_64(struct snd_compr_tstamp *tstamp32,
174 const struct snd_compr_tstamp64 *tstamp64)
175 {
176 tstamp32->byte_offset = tstamp64->byte_offset;
177 tstamp32->copied_total = (u32)tstamp64->copied_total;
178 tstamp32->pcm_frames = (u32)tstamp64->pcm_frames;
179 tstamp32->pcm_io_frames = (u32)tstamp64->pcm_io_frames;
180 tstamp32->sampling_rate = tstamp64->sampling_rate;
181 }
182
183 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
184 struct snd_compr_tstamp64 *tstamp)
185 {
186 if (!stream->ops->pointer)
187 return -ENOTSUPP;
188 stream->ops->pointer(stream, tstamp);
189 pr_debug("dsp consumed till %u total %llu bytes\n", tstamp->byte_offset,
190 tstamp->copied_total);
191 if (stream->direction == SND_COMPRESS_PLAYBACK)
192 stream->runtime->total_bytes_transferred = tstamp->copied_total;
193 else
194 stream->runtime->total_bytes_available = tstamp->copied_total;
195 return 0;
196 }
197
198 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
199 struct snd_compr_avail64 *avail)
200 {
201 memset(avail, 0, sizeof(*avail));
202 snd_compr_update_tstamp(stream, &avail->tstamp);
203 /* Still need to return avail even if tstamp can't be filled in */
204
205 if (stream->runtime->total_bytes_available == 0 &&
206 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
207 stream->direction == SND_COMPRESS_PLAYBACK) {
208 pr_debug("detected init and someone forgot to do a write\n");
209 return stream->runtime->buffer_size;
210 }
211 pr_debug("app wrote %llu, DSP consumed %llu\n",
212 stream->runtime->total_bytes_available,
213 stream->runtime->total_bytes_transferred);
214 if (stream->runtime->total_bytes_available ==
215 stream->runtime->total_bytes_transferred) {
216 if (stream->direction == SND_COMPRESS_PLAYBACK) {
217 pr_debug("both pointers are same, returning full avail\n");
218 return stream->runtime->buffer_size;
219 } else {
220 pr_debug("both pointers are same, returning no avail\n");
221 return 0;
222 }
223 }
224
225 avail->avail = stream->runtime->total_bytes_available -
226 stream->runtime->total_bytes_transferred;
227 if (stream->direction == SND_COMPRESS_PLAYBACK)
228 avail->avail = stream->runtime->buffer_size - avail->avail;
229
230 pr_debug("ret avail as %zu\n", (size_t)avail->avail);
231 return avail->avail;
232 }
233
234 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
235 {
236 struct snd_compr_avail64 avail;
237
238 return snd_compr_calc_avail(stream, &avail);
239 }
240
241 static void snd_compr_avail32_from_64(struct snd_compr_avail *avail32,
242 const struct snd_compr_avail64 *avail64)
243 {
244 avail32->avail = avail64->avail;
245 snd_compr_tstamp32_from_64(&avail32->tstamp, &avail64->tstamp);
246 }
247
248 static int snd_compr_ioctl_avail(struct snd_compr_stream *stream,
249 unsigned long arg, bool is_32bit)
250 {
251 struct snd_compr_avail64 ioctl_avail64;
252 struct snd_compr_avail ioctl_avail32;
253 size_t avail;
254 const void *copy_from = &ioctl_avail64;
255 size_t copy_size = sizeof(ioctl_avail64);
256
257 if (stream->direction == SND_COMPRESS_ACCEL)
258 return -EBADFD;
259
260 avail = snd_compr_calc_avail(stream, &ioctl_avail64);
261 ioctl_avail64.avail = avail;
262 if (is_32bit) {
263 snd_compr_avail32_from_64(&ioctl_avail32, &ioctl_avail64);
264 copy_from = &ioctl_avail32;
265 copy_size = sizeof(ioctl_avail32);
266 }
267
268 switch (stream->runtime->state) {
269 case SNDRV_PCM_STATE_OPEN:
270 return -EBADFD;
271 case SNDRV_PCM_STATE_XRUN:
272 return -EPIPE;
273 default:
274 break;
275 }
276
277 if (copy_to_user((__u64 __user *)arg, copy_from, copy_size))
278 return -EFAULT;
279 return 0;
280 }
281
282 static int snd_compr_write_data(struct snd_compr_stream *stream,
283 const char __user *buf, size_t count)
284 {
285 void *dstn;
286 size_t copy;
287 struct snd_compr_runtime *runtime = stream->runtime;
288 /* 64-bit Modulus */
289 u64 app_pointer = div64_u64(runtime->total_bytes_available,
290 runtime->buffer_size);
291 app_pointer = runtime->total_bytes_available -
292 (app_pointer * runtime->buffer_size);
293
294 dstn = runtime->buffer + app_pointer;
295 pr_debug("copying %lu at %llu\n", (unsigned long)count, app_pointer);
296 if (count < runtime->buffer_size - app_pointer) {
297 if (copy_from_user(dstn, buf, count))
298 return -EFAULT;
299 } else {
300 copy = runtime->buffer_size - app_pointer;
301 if (copy_from_user(dstn, buf, copy))
302 return -EFAULT;
303 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
304 return -EFAULT;
305 }
306 /* if DSP cares, let it know data has been written */
307 if (stream->ops->ack)
308 stream->ops->ack(stream, count);
309 return count;
310 }
311
312 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
313 size_t count, loff_t *offset)
314 {
315 struct snd_compr_file *data = f->private_data;
316 struct snd_compr_stream *stream;
317 size_t avail;
318 int retval;
319
320 if (snd_BUG_ON(!data))
321 return -EFAULT;
322
323 stream = &data->stream;
324 if (stream->direction == SND_COMPRESS_ACCEL)
325 return -EBADFD;
326 guard(mutex)(&stream->device->lock);
327 /* write is allowed when stream is running or has been setup */
328 switch (stream->runtime->state) {
329 case SNDRV_PCM_STATE_SETUP:
330 case SNDRV_PCM_STATE_PREPARED:
331 case SNDRV_PCM_STATE_RUNNING:
332 break;
333 default:
334 return -EBADFD;
335 }
336
337 avail = snd_compr_get_avail(stream);
338 pr_debug("avail returned %lu\n", (unsigned long)avail);
339 /* calculate how much we can write to buffer */
340 if (avail > count)
341 avail = count;
342
343 if (stream->ops->copy) {
344 char __user* cbuf = (char __user*)buf;
345 retval = stream->ops->copy(stream, cbuf, avail);
346 } else {
347 retval = snd_compr_write_data(stream, buf, avail);
348 }
349 if (retval > 0)
350 stream->runtime->total_bytes_available += retval;
351
352 /* while initiating the stream, write should be called before START
353 * call, so in setup move state */
354 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
355 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
356 pr_debug("stream prepared, Houston we are good to go\n");
357 }
358
359 return retval;
360 }
361
362
363 static ssize_t snd_compr_read(struct file *f, char __user *buf,
364 size_t count, loff_t *offset)
365 {
366 struct snd_compr_file *data = f->private_data;
367 struct snd_compr_stream *stream;
368 size_t avail;
369 int retval;
370
371 if (snd_BUG_ON(!data))
372 return -EFAULT;
373
374 stream = &data->stream;
375 if (stream->direction == SND_COMPRESS_ACCEL)
376 return -EBADFD;
377 guard(mutex)(&stream->device->lock);
378
379 /* read is allowed when stream is running, paused, draining and setup
380 * (yes setup is state which we transition to after stop, so if user
381 * wants to read data after stop we allow that)
382 */
383 switch (stream->runtime->state) {
384 case SNDRV_PCM_STATE_OPEN:
385 case SNDRV_PCM_STATE_PREPARED:
386 case SNDRV_PCM_STATE_SUSPENDED:
387 case SNDRV_PCM_STATE_DISCONNECTED:
388 return -EBADFD;
389 case SNDRV_PCM_STATE_XRUN:
390 return -EPIPE;
391 }
392
393 avail = snd_compr_get_avail(stream);
394 pr_debug("avail returned %lu\n", (unsigned long)avail);
395 /* calculate how much we can read from buffer */
396 if (avail > count)
397 avail = count;
398
399 if (stream->ops->copy)
400 retval = stream->ops->copy(stream, buf, avail);
401 else
402 return -ENXIO;
403 if (retval > 0)
404 stream->runtime->total_bytes_transferred += retval;
405
406 return retval;
407 }
408
409 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
410 {
411 return -ENXIO;
412 }
413
414 static __poll_t snd_compr_get_poll(struct snd_compr_stream *stream)
415 {
416 if (stream->direction == SND_COMPRESS_PLAYBACK)
417 return EPOLLOUT | EPOLLWRNORM;
418 else
419 return EPOLLIN | EPOLLRDNORM;
420 }
421
422 static __poll_t snd_compr_poll(struct file *f, poll_table *wait)
423 {
424 struct snd_compr_file *data = f->private_data;
425 struct snd_compr_stream *stream;
426 struct snd_compr_runtime *runtime;
427 size_t avail;
428 __poll_t retval = 0;
429
430 if (snd_BUG_ON(!data))
431 return EPOLLERR;
432
433 stream = &data->stream;
434 runtime = stream->runtime;
435
436 guard(mutex)(&stream->device->lock);
437
438 switch (runtime->state) {
439 case SNDRV_PCM_STATE_OPEN:
440 case SNDRV_PCM_STATE_XRUN:
441 return snd_compr_get_poll(stream) | EPOLLERR;
442 default:
443 break;
444 }
445
446 poll_wait(f, &runtime->sleep, wait);
447
448 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
449 if (stream->direction == SND_COMPRESS_ACCEL) {
450 struct snd_compr_task_runtime *task;
451 if (runtime->fragments > runtime->active_tasks)
452 retval |= EPOLLOUT | EPOLLWRNORM;
453 task = list_first_entry_or_null(&runtime->tasks,
454 struct snd_compr_task_runtime,
455 list);
456 if (task && task->state == SND_COMPRESS_TASK_STATE_FINISHED)
457 retval |= EPOLLIN | EPOLLRDNORM;
458 return retval;
459 }
460 #endif
461
462 avail = snd_compr_get_avail(stream);
463 pr_debug("avail is %lu\n", (unsigned long)avail);
464 /* check if we have at least one fragment to fill */
465 switch (runtime->state) {
466 case SNDRV_PCM_STATE_DRAINING:
467 /* stream has been woken up after drain is complete
468 * draining done so set stream state to stopped
469 */
470 retval = snd_compr_get_poll(stream);
471 runtime->state = SNDRV_PCM_STATE_SETUP;
472 break;
473 case SNDRV_PCM_STATE_RUNNING:
474 case SNDRV_PCM_STATE_PREPARED:
475 case SNDRV_PCM_STATE_PAUSED:
476 if (avail >= runtime->fragment_size)
477 retval = snd_compr_get_poll(stream);
478 break;
479 default:
480 return snd_compr_get_poll(stream) | EPOLLERR;
481 }
482
483 return retval;
484 }
485
486 static int
487 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
488 {
489 int retval;
490 struct snd_compr_caps caps;
491
492 if (!stream->ops->get_caps)
493 return -ENXIO;
494
495 memset(&caps, 0, sizeof(caps));
496 retval = stream->ops->get_caps(stream, &caps);
497 if (retval)
498 goto out;
499 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
500 retval = -EFAULT;
501 out:
502 return retval;
503 }
504
505 #ifndef COMPR_CODEC_CAPS_OVERFLOW
506 static int
507 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
508 {
509 int retval;
510
511 if (!stream->ops->get_codec_caps)
512 return -ENXIO;
513
514 struct snd_compr_codec_caps *caps __free(kfree) =
515 kzalloc_obj(*caps);
516 if (!caps)
517 return -ENOMEM;
518
519 retval = stream->ops->get_codec_caps(stream, caps);
520 if (retval)
521 return retval;
522 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
523 return -EFAULT;
524 return retval;
525 }
526 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
527
528 int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size)
529 {
530 struct snd_dma_buffer *dmab;
531 int ret;
532
533 if (snd_BUG_ON(!(stream) || !(stream)->runtime))
534 return -EINVAL;
535 dmab = kzalloc_obj(*dmab);
536 if (!dmab)
537 return -ENOMEM;
538 dmab->dev = stream->dma_buffer.dev;
539 ret = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev, size, dmab);
540 if (ret < 0) {
541 kfree(dmab);
542 return ret;
543 }
544
545 snd_compr_set_runtime_buffer(stream, dmab);
546 stream->runtime->dma_bytes = size;
547 return 1;
548 }
549 EXPORT_SYMBOL(snd_compr_malloc_pages);
550
551 int snd_compr_free_pages(struct snd_compr_stream *stream)
552 {
553 struct snd_compr_runtime *runtime;
554
555 if (snd_BUG_ON(!(stream) || !(stream)->runtime))
556 return -EINVAL;
557 runtime = stream->runtime;
558 if (runtime->dma_area == NULL)
559 return 0;
560 if (runtime->dma_buffer_p != &stream->dma_buffer) {
561 /* It's a newly allocated buffer. Release it now. */
562 snd_dma_free_pages(runtime->dma_buffer_p);
563 kfree(runtime->dma_buffer_p);
564 }
565
566 snd_compr_set_runtime_buffer(stream, NULL);
567 return 0;
568 }
569 EXPORT_SYMBOL(snd_compr_free_pages);
570
571 /* revisit this with snd_pcm_preallocate_xxx */
572 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
573 struct snd_compr_params *params)
574 {
575 unsigned int buffer_size;
576 void *buffer = NULL;
577
578 if (stream->direction == SND_COMPRESS_ACCEL)
579 goto params;
580
581 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
582 if (stream->ops->copy) {
583 buffer = NULL;
584 /* if copy is defined the driver will be required to copy
585 * the data from core
586 */
587 } else {
588 if (stream->runtime->dma_buffer_p) {
589
590 if (buffer_size > stream->runtime->dma_buffer_p->bytes)
591 dev_err(stream->device->dev,
592 "Not enough DMA buffer");
593 else
594 buffer = stream->runtime->dma_buffer_p->area;
595
596 } else {
597 buffer = kmalloc(buffer_size, GFP_KERNEL);
598 }
599
600 if (!buffer)
601 return -ENOMEM;
602 }
603
604 stream->runtime->buffer = buffer;
605 stream->runtime->buffer_size = buffer_size;
606 params:
607 stream->runtime->fragment_size = params->buffer.fragment_size;
608 stream->runtime->fragments = params->buffer.fragments;
609 return 0;
610 }
611
612 static int
613 snd_compress_check_input(struct snd_compr_stream *stream, struct snd_compr_params *params)
614 {
615 u32 max_fragments;
616
617 /* first let's check the buffer parameter's */
618 if (params->buffer.fragment_size == 0)
619 return -EINVAL;
620
621 if (stream->direction == SND_COMPRESS_ACCEL)
622 max_fragments = 64; /* safe value */
623 else
624 max_fragments = U32_MAX / params->buffer.fragment_size;
625
626 if (params->buffer.fragments > max_fragments ||
627 params->buffer.fragments == 0)
628 return -EINVAL;
629
630 /* now codec parameters */
631 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
632 return -EINVAL;
633
634 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
635 return -EINVAL;
636
637 return 0;
638 }
639
640 static int
641 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
642 {
643 int retval;
644
645 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN || stream->next_track) {
646 /*
647 * we should allow parameter change only when stream has been
648 * opened not in other cases
649 */
650 struct snd_compr_params *params __free(kfree) =
651 memdup_user((void __user *)arg, sizeof(*params));
652
653 if (IS_ERR(params))
654 return PTR_ERR(params);
655
656 retval = snd_compress_check_input(stream, params);
657 if (retval)
658 return retval;
659
660 retval = snd_compr_allocate_buffer(stream, params);
661 if (retval)
662 return -ENOMEM;
663
664 retval = stream->ops->set_params(stream, params);
665 if (retval)
666 return retval;
667
668 if (stream->next_track)
669 return retval;
670
671 stream->metadata_set = false;
672 stream->next_track = false;
673
674 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
675 } else {
676 return -EPERM;
677 }
678 return retval;
679 }
680
681 static int
682 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
683 {
684 int retval;
685
686 if (!stream->ops->get_params)
687 return -EBADFD;
688
689 struct snd_codec *params __free(kfree) =
690 kzalloc_obj(*params);
691 if (!params)
692 return -ENOMEM;
693 retval = stream->ops->get_params(stream, params);
694 if (retval)
695 return retval;
696 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
697 return -EFAULT;
698 return retval;
699 }
700
701 static int
702 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
703 {
704 struct snd_compr_metadata metadata;
705 int retval;
706
707 if (!stream->ops->get_metadata)
708 return -ENXIO;
709
710 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
711 return -EFAULT;
712
713 retval = stream->ops->get_metadata(stream, &metadata);
714 if (retval != 0)
715 return retval;
716
717 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
718 return -EFAULT;
719
720 return 0;
721 }
722
723 static int
724 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
725 {
726 struct snd_compr_metadata metadata;
727 int retval;
728
729 if (!stream->ops->set_metadata)
730 return -ENXIO;
731 /*
732 * we should allow parameter change only when stream has been
733 * opened not in other cases
734 */
735 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
736 return -EFAULT;
737
738 retval = stream->ops->set_metadata(stream, &metadata);
739 stream->metadata_set = true;
740
741 return retval;
742 }
743
744 static inline int snd_compr_tstamp(struct snd_compr_stream *stream,
745 unsigned long arg, bool is_32bit)
746 {
747 struct snd_compr_tstamp64 tstamp64 = { 0 };
748 struct snd_compr_tstamp tstamp32 = { 0 };
749 const void *copy_from = &tstamp64;
750 size_t copy_size = sizeof(tstamp64);
751 int ret;
752
753 ret = snd_compr_update_tstamp(stream, &tstamp64);
754 if (ret == 0) {
755 if (is_32bit) {
756 snd_compr_tstamp32_from_64(&tstamp32, &tstamp64);
757 copy_from = &tstamp32;
758 copy_size = sizeof(tstamp32);
759 }
760 ret = copy_to_user((void __user *)arg, copy_from, copy_size) ?
761 -EFAULT :
762 0;
763 }
764 return ret;
765 }
766
767 static int snd_compr_pause(struct snd_compr_stream *stream)
768 {
769 int retval;
770
771 switch (stream->runtime->state) {
772 case SNDRV_PCM_STATE_RUNNING:
773 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
774 if (!retval)
775 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
776 break;
777 case SNDRV_PCM_STATE_DRAINING:
778 if (!stream->device->use_pause_in_draining)
779 return -EPERM;
780 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
781 if (!retval)
782 stream->pause_in_draining = true;
783 break;
784 default:
785 return -EPERM;
786 }
787 return retval;
788 }
789
790 static int snd_compr_resume(struct snd_compr_stream *stream)
791 {
792 int retval;
793
794 switch (stream->runtime->state) {
795 case SNDRV_PCM_STATE_PAUSED:
796 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
797 if (!retval)
798 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
799 break;
800 case SNDRV_PCM_STATE_DRAINING:
801 if (!stream->pause_in_draining)
802 return -EPERM;
803 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
804 if (!retval)
805 stream->pause_in_draining = false;
806 break;
807 default:
808 return -EPERM;
809 }
810 return retval;
811 }
812
813 static int snd_compr_start(struct snd_compr_stream *stream)
814 {
815 int retval;
816
817 switch (stream->runtime->state) {
818 case SNDRV_PCM_STATE_SETUP:
819 if (stream->direction != SND_COMPRESS_CAPTURE)
820 return -EPERM;
821 break;
822 case SNDRV_PCM_STATE_PREPARED:
823 break;
824 default:
825 return -EPERM;
826 }
827
828 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
829 if (!retval)
830 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
831 return retval;
832 }
833
834 static int snd_compr_stop(struct snd_compr_stream *stream)
835 {
836 int retval;
837
838 switch (stream->runtime->state) {
839 case SNDRV_PCM_STATE_OPEN:
840 case SNDRV_PCM_STATE_SETUP:
841 case SNDRV_PCM_STATE_PREPARED:
842 return -EPERM;
843 default:
844 break;
845 }
846
847 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
848 if (!retval) {
849 /* clear flags and stop any drain wait */
850 stream->partial_drain = false;
851 stream->metadata_set = false;
852 stream->pause_in_draining = false;
853 snd_compr_drain_notify(stream);
854 stream->runtime->total_bytes_available = 0;
855 stream->runtime->total_bytes_transferred = 0;
856 }
857 return retval;
858 }
859
860 static void error_delayed_work(struct work_struct *work)
861 {
862 struct snd_compr_stream *stream;
863
864 stream = container_of(work, struct snd_compr_stream, error_work.work);
865
866 guard(mutex)(&stream->device->lock);
867
868 stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
869 wake_up(&stream->runtime->sleep);
870 }
871
872 /**
873 * snd_compr_stop_error: Report a fatal error on a stream
874 * @stream: pointer to stream
875 * @state: state to transition the stream to
876 *
877 * Stop the stream and set its state.
878 *
879 * Should be called with compressed device lock held.
880 *
881 * Return: zero if successful, or a negative error code
882 */
883 int snd_compr_stop_error(struct snd_compr_stream *stream,
884 snd_pcm_state_t state)
885 {
886 if (stream->runtime->state == state)
887 return 0;
888
889 stream->runtime->state = state;
890
891 pr_debug("Changing state to: %d\n", state);
892
893 queue_delayed_work(system_power_efficient_wq, &stream->error_work, 0);
894
895 return 0;
896 }
897 EXPORT_SYMBOL_GPL(snd_compr_stop_error);
898
899 static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
900 {
901 int ret;
902
903 /*
904 * We are called with lock held. So drop the lock while we wait for
905 * drain complete notification from the driver
906 *
907 * It is expected that driver will notify the drain completion and then
908 * stream will be moved to SETUP state, even if draining resulted in an
909 * error. We can trigger next track after this.
910 */
911 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
912 mutex_unlock(&stream->device->lock);
913
914 /* we wait for drain to complete here, drain can return when
915 * interruption occurred, wait returned error or success.
916 * For the first two cases we don't do anything different here and
917 * return after waking up
918 */
919
920 ret = wait_event_interruptible(stream->runtime->sleep,
921 (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
922 if (ret == -ERESTARTSYS)
923 pr_debug("wait aborted by a signal\n");
924 else if (ret)
925 pr_debug("wait for drain failed with %d\n", ret);
926
927
928 wake_up(&stream->runtime->sleep);
929 mutex_lock(&stream->device->lock);
930
931 return ret;
932 }
933
934 static int snd_compr_drain(struct snd_compr_stream *stream)
935 {
936 int retval;
937
938 switch (stream->runtime->state) {
939 case SNDRV_PCM_STATE_OPEN:
940 case SNDRV_PCM_STATE_SETUP:
941 case SNDRV_PCM_STATE_PREPARED:
942 case SNDRV_PCM_STATE_PAUSED:
943 return -EPERM;
944 case SNDRV_PCM_STATE_XRUN:
945 return -EPIPE;
946 default:
947 break;
948 }
949
950 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
951 if (retval) {
952 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
953 wake_up(&stream->runtime->sleep);
954 return retval;
955 }
956
957 return snd_compress_wait_for_drain(stream);
958 }
959
960 static int snd_compr_next_track(struct snd_compr_stream *stream)
961 {
962 int retval;
963
964 /* only a running stream can transition to next track */
965 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
966 return -EPERM;
967
968 /* next track doesn't have any meaning for capture streams */
969 if (stream->direction == SND_COMPRESS_CAPTURE)
970 return -EPERM;
971
972 /* you can signal next track if this is intended to be a gapless stream
973 * and current track metadata is set
974 */
975 if (stream->metadata_set == false)
976 return -EPERM;
977
978 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
979 if (retval != 0)
980 return retval;
981 stream->metadata_set = false;
982 stream->next_track = true;
983 return 0;
984 }
985
986 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
987 {
988 int retval;
989
990 switch (stream->runtime->state) {
991 case SNDRV_PCM_STATE_OPEN:
992 case SNDRV_PCM_STATE_SETUP:
993 case SNDRV_PCM_STATE_PREPARED:
994 case SNDRV_PCM_STATE_PAUSED:
995 return -EPERM;
996 case SNDRV_PCM_STATE_XRUN:
997 return -EPIPE;
998 default:
999 break;
1000 }
1001
1002 /* partial drain doesn't have any meaning for capture streams */
1003 if (stream->direction == SND_COMPRESS_CAPTURE)
1004 return -EPERM;
1005
1006 /* stream can be drained only when next track has been signalled */
1007 if (stream->next_track == false)
1008 return -EPERM;
1009
1010 stream->partial_drain = true;
1011 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
1012 if (retval) {
1013 pr_debug("Partial drain returned failure\n");
1014 wake_up(&stream->runtime->sleep);
1015 return retval;
1016 }
1017
1018 stream->next_track = false;
1019 return snd_compress_wait_for_drain(stream);
1020 }
1021
1022 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
1023
1024 static struct snd_compr_task_runtime *
1025 snd_compr_find_task(struct snd_compr_stream *stream, __u64 seqno)
1026 {
1027 struct snd_compr_task_runtime *task;
1028
1029 list_for_each_entry(task, &stream->runtime->tasks, list) {
1030 if (task->seqno == seqno)
1031 return task;
1032 }
1033 return NULL;
1034 }
1035
1036 static void snd_compr_task_free(struct snd_compr_task_runtime *task)
1037 {
1038 if (task->output)
1039 dma_buf_put(task->output);
1040 if (task->input)
1041 dma_buf_put(task->input);
1042 kfree(task);
1043 }
1044
1045 static u64 snd_compr_seqno_next(struct snd_compr_stream *stream)
1046 {
1047 u64 seqno = ++stream->runtime->task_seqno;
1048 if (seqno == 0)
1049 seqno = ++stream->runtime->task_seqno;
1050 return seqno;
1051 }
1052
1053 static int snd_compr_task_new(struct snd_compr_stream *stream, struct snd_compr_task *utask)
1054 {
1055 struct snd_compr_task_runtime *task;
1056 int retval, fd_i, fd_o;
1057
1058 if (stream->runtime->total_tasks >= stream->runtime->fragments)
1059 return -EBUSY;
1060 if (utask->origin_seqno != 0 || utask->input_size != 0)
1061 return -EINVAL;
1062 task = kzalloc_obj(*task);
1063 if (task == NULL)
1064 return -ENOMEM;
1065 task->seqno = utask->seqno = snd_compr_seqno_next(stream);
1066 task->input_size = utask->input_size;
1067 retval = stream->ops->task_create(stream, task);
1068 if (retval < 0)
1069 goto cleanup;
1070 /* similar functionality as in dma_buf_fd(), but ensure that both
1071 file descriptors are allocated before fd_install() */
1072 if (!task->input || !task->input->file || !task->output || !task->output->file) {
1073 retval = -EINVAL;
1074 goto cleanup;
1075 }
1076 fd_i = get_unused_fd_flags(O_WRONLY|O_CLOEXEC);
1077 if (fd_i < 0)
1078 goto cleanup;
1079 fd_o = get_unused_fd_flags(O_RDONLY|O_CLOEXEC);
1080 if (fd_o < 0) {
1081 put_unused_fd(fd_i);
1082 goto cleanup;
1083 }
1084 /* keep dmabuf reference until freed with task free ioctl */
1085 get_dma_buf(task->input);
1086 get_dma_buf(task->output);
1087 fd_install(fd_i, task->input->file);
1088 fd_install(fd_o, task->output->file);
1089 utask->input_fd = fd_i;
1090 utask->output_fd = fd_o;
1091 list_add_tail(&task->list, &stream->runtime->tasks);
1092 stream->runtime->total_tasks++;
1093 return 0;
1094 cleanup:
1095 snd_compr_task_free(task);
1096 return retval;
1097 }
1098
1099 static int snd_compr_task_create(struct snd_compr_stream *stream, unsigned long arg)
1100 {
1101 int retval;
1102
1103 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
1104 return -EPERM;
1105
1106 struct snd_compr_task *task __free(kfree) =
1107 memdup_user((void __user *)arg, sizeof(*task));
1108 if (IS_ERR(task))
1109 return PTR_ERR(task);
1110 retval = snd_compr_task_new(stream, task);
1111 if (retval >= 0)
1112 if (copy_to_user((void __user *)arg, task, sizeof(*task)))
1113 retval = -EFAULT;
1114 return retval;
1115 }
1116
1117 static int snd_compr_task_start_prepare(struct snd_compr_task_runtime *task,
1118 struct snd_compr_task *utask)
1119 {
1120 if (task == NULL)
1121 return -EINVAL;
1122 if (task->state >= SND_COMPRESS_TASK_STATE_FINISHED)
1123 return -EBUSY;
1124 if (utask->input_size > task->input->size)
1125 return -EINVAL;
1126 task->flags = utask->flags;
1127 task->input_size = utask->input_size;
1128 task->state = SND_COMPRESS_TASK_STATE_IDLE;
1129 return 0;
1130 }
1131
1132 static int snd_compr_task_start(struct snd_compr_stream *stream, struct snd_compr_task *utask)
1133 {
1134 struct snd_compr_task_runtime *task;
1135 int retval;
1136
1137 if (utask->origin_seqno > 0) {
1138 task = snd_compr_find_task(stream, utask->origin_seqno);
1139 retval = snd_compr_task_start_prepare(task, utask);
1140 if (retval < 0)
1141 return retval;
1142 task->seqno = utask->seqno = snd_compr_seqno_next(stream);
1143 utask->origin_seqno = 0;
1144 list_move_tail(&task->list, &stream->runtime->tasks);
1145 } else {
1146 task = snd_compr_find_task(stream, utask->seqno);
1147 if (task && task->state != SND_COMPRESS_TASK_STATE_IDLE)
1148 return -EBUSY;
1149 retval = snd_compr_task_start_prepare(task, utask);
1150 if (retval < 0)
1151 return retval;
1152 }
1153 retval = stream->ops->task_start(stream, task);
1154 if (retval >= 0) {
1155 task->state = SND_COMPRESS_TASK_STATE_ACTIVE;
1156 stream->runtime->active_tasks++;
1157 }
1158 return retval;
1159 }
1160
1161 static int snd_compr_task_start_ioctl(struct snd_compr_stream *stream, unsigned long arg)
1162 {
1163 int retval;
1164
1165 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
1166 return -EPERM;
1167
1168 struct snd_compr_task *task __free(kfree) =
1169 memdup_user((void __user *)arg, sizeof(*task));
1170 if (IS_ERR(task))
1171 return PTR_ERR(task);
1172 retval = snd_compr_task_start(stream, task);
1173 if (retval >= 0)
1174 if (copy_to_user((void __user *)arg, task, sizeof(*task)))
1175 retval = -EFAULT;
1176 return retval;
1177 }
1178
1179 static void snd_compr_task_stop_one(struct snd_compr_stream *stream,
1180 struct snd_compr_task_runtime *task)
1181 {
1182 if (task->state != SND_COMPRESS_TASK_STATE_ACTIVE)
1183 return;
1184 stream->ops->task_stop(stream, task);
1185 if (!snd_BUG_ON(stream->runtime->active_tasks == 0))
1186 stream->runtime->active_tasks--;
1187 list_move_tail(&task->list, &stream->runtime->tasks);
1188 task->state = SND_COMPRESS_TASK_STATE_IDLE;
1189 }
1190
1191 static void snd_compr_task_free_one(struct snd_compr_stream *stream,
1192 struct snd_compr_task_runtime *task)
1193 {
1194 snd_compr_task_stop_one(stream, task);
1195 stream->ops->task_free(stream, task);
1196 list_del(&task->list);
1197 snd_compr_task_free(task);
1198 stream->runtime->total_tasks--;
1199 }
1200
1201 static void snd_compr_task_free_all(struct snd_compr_stream *stream)
1202 {
1203 struct snd_compr_task_runtime *task, *temp;
1204
1205 list_for_each_entry_safe_reverse(task, temp, &stream->runtime->tasks, list)
1206 snd_compr_task_free_one(stream, task);
1207 }
1208
1209 typedef void (*snd_compr_seq_func_t)(struct snd_compr_stream *stream,
1210 struct snd_compr_task_runtime *task);
1211
1212 static int snd_compr_task_seq(struct snd_compr_stream *stream, unsigned long arg,
1213 snd_compr_seq_func_t fcn)
1214 {
1215 struct snd_compr_task_runtime *task, *temp;
1216 __u64 seqno;
1217 int retval;
1218
1219 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
1220 return -EPERM;
1221 retval = copy_from_user(&seqno, (__u64 __user *)arg, sizeof(seqno));
1222 if (retval)
1223 return -EFAULT;
1224 retval = 0;
1225 if (seqno == 0) {
1226 list_for_each_entry_safe_reverse(task, temp, &stream->runtime->tasks, list)
1227 fcn(stream, task);
1228 } else {
1229 task = snd_compr_find_task(stream, seqno);
1230 if (task == NULL) {
1231 retval = -EINVAL;
1232 } else {
1233 fcn(stream, task);
1234 }
1235 }
1236 return retval;
1237 }
1238
1239 static int snd_compr_task_status(struct snd_compr_stream *stream,
1240 struct snd_compr_task_status *status)
1241 {
1242 struct snd_compr_task_runtime *task;
1243
1244 task = snd_compr_find_task(stream, status->seqno);
1245 if (task == NULL)
1246 return -EINVAL;
1247 status->input_size = task->input_size;
1248 status->output_size = task->output_size;
1249 status->state = task->state;
1250 return 0;
1251 }
1252
1253 static int snd_compr_task_status_ioctl(struct snd_compr_stream *stream, unsigned long arg)
1254 {
1255 int retval;
1256
1257 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP)
1258 return -EPERM;
1259
1260 struct snd_compr_task_status *status __free(kfree) =
1261 memdup_user((void __user *)arg, sizeof(*status));
1262 if (IS_ERR(status))
1263 return PTR_ERR(status);
1264 retval = snd_compr_task_status(stream, status);
1265 if (retval >= 0)
1266 if (copy_to_user((void __user *)arg, status, sizeof(*status)))
1267 retval = -EFAULT;
1268 return retval;
1269 }
1270
1271 /**
1272 * snd_compr_task_finished: Notify that the task was finished
1273 * @stream: pointer to stream
1274 * @task: runtime task structure
1275 *
1276 * Set the finished task state and notify waiters.
1277 */
1278 void snd_compr_task_finished(struct snd_compr_stream *stream,
1279 struct snd_compr_task_runtime *task)
1280 {
1281 guard(mutex)(&stream->device->lock);
1282 if (!snd_BUG_ON(stream->runtime->active_tasks == 0))
1283 stream->runtime->active_tasks--;
1284 task->state = SND_COMPRESS_TASK_STATE_FINISHED;
1285 wake_up(&stream->runtime->sleep);
1286 }
1287 EXPORT_SYMBOL_GPL(snd_compr_task_finished);
1288
1289 MODULE_IMPORT_NS("DMA_BUF");
1290 #endif /* CONFIG_SND_COMPRESS_ACCEL */
1291
1292 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1293 {
1294 struct snd_compr_file *data = f->private_data;
1295 struct snd_compr_stream *stream;
1296
1297 if (snd_BUG_ON(!data))
1298 return -EFAULT;
1299
1300 stream = &data->stream;
1301
1302 guard(mutex)(&stream->device->lock);
1303 switch (cmd) {
1304 case SNDRV_COMPRESS_IOCTL_VERSION:
1305 return put_user(SNDRV_COMPRESS_VERSION,
1306 (int __user *)arg) ? -EFAULT : 0;
1307 case SNDRV_COMPRESS_GET_CAPS:
1308 return snd_compr_get_caps(stream, arg);
1309 #ifndef COMPR_CODEC_CAPS_OVERFLOW
1310 case SNDRV_COMPRESS_GET_CODEC_CAPS:
1311 return snd_compr_get_codec_caps(stream, arg);
1312 #endif
1313 case SNDRV_COMPRESS_SET_PARAMS:
1314 return snd_compr_set_params(stream, arg);
1315 case SNDRV_COMPRESS_GET_PARAMS:
1316 return snd_compr_get_params(stream, arg);
1317 case SNDRV_COMPRESS_SET_METADATA:
1318 return snd_compr_set_metadata(stream, arg);
1319 case SNDRV_COMPRESS_GET_METADATA:
1320 return snd_compr_get_metadata(stream, arg);
1321 }
1322
1323 if (stream->direction == SND_COMPRESS_ACCEL) {
1324 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
1325 switch (cmd) {
1326 case SNDRV_COMPRESS_TASK_CREATE:
1327 return snd_compr_task_create(stream, arg);
1328 case SNDRV_COMPRESS_TASK_FREE:
1329 return snd_compr_task_seq(stream, arg, snd_compr_task_free_one);
1330 case SNDRV_COMPRESS_TASK_START:
1331 return snd_compr_task_start_ioctl(stream, arg);
1332 case SNDRV_COMPRESS_TASK_STOP:
1333 return snd_compr_task_seq(stream, arg, snd_compr_task_stop_one);
1334 case SNDRV_COMPRESS_TASK_STATUS:
1335 return snd_compr_task_status_ioctl(stream, arg);
1336 }
1337 #endif
1338 return -ENOTTY;
1339 }
1340
1341 switch (cmd) {
1342 case SNDRV_COMPRESS_TSTAMP:
1343 return snd_compr_tstamp(stream, arg, true);
1344 case SNDRV_COMPRESS_TSTAMP64:
1345 return snd_compr_tstamp(stream, arg, false);
1346 case SNDRV_COMPRESS_AVAIL:
1347 return snd_compr_ioctl_avail(stream, arg, true);
1348 case SNDRV_COMPRESS_AVAIL64:
1349 return snd_compr_ioctl_avail(stream, arg, false);
1350 case SNDRV_COMPRESS_PAUSE:
1351 return snd_compr_pause(stream);
1352 case SNDRV_COMPRESS_RESUME:
1353 return snd_compr_resume(stream);
1354 case SNDRV_COMPRESS_START:
1355 return snd_compr_start(stream);
1356 case SNDRV_COMPRESS_STOP:
1357 return snd_compr_stop(stream);
1358 case SNDRV_COMPRESS_DRAIN:
1359 return snd_compr_drain(stream);
1360 case SNDRV_COMPRESS_PARTIAL_DRAIN:
1361 return snd_compr_partial_drain(stream);
1362 case SNDRV_COMPRESS_NEXT_TRACK:
1363 return snd_compr_next_track(stream);
1364 }
1365
1366 return -ENOTTY;
1367 }
1368
1369 /* support of 32bit userspace on 64bit platforms */
1370 #ifdef CONFIG_COMPAT
1371 static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
1372 unsigned long arg)
1373 {
1374 return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
1375 }
1376 #endif
1377
1378 static const struct file_operations snd_compr_file_ops = {
1379 .owner = THIS_MODULE,
1380 .open = snd_compr_open,
1381 .release = snd_compr_free,
1382 .write = snd_compr_write,
1383 .read = snd_compr_read,
1384 .unlocked_ioctl = snd_compr_ioctl,
1385 #ifdef CONFIG_COMPAT
1386 .compat_ioctl = snd_compr_ioctl_compat,
1387 #endif
1388 .mmap = snd_compr_mmap,
1389 .poll = snd_compr_poll,
1390 };
1391
1392 static int snd_compress_dev_register(struct snd_device *device)
1393 {
1394 int ret;
1395 struct snd_compr *compr;
1396
1397 if (snd_BUG_ON(!device || !device->device_data))
1398 return -EBADFD;
1399 compr = device->device_data;
1400
1401 pr_debug("reg device %s, direction %d\n", compr->name,
1402 compr->direction);
1403 /* register compressed device */
1404 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
1405 compr->card, compr->device,
1406 &snd_compr_file_ops, compr, compr->dev);
1407 if (ret < 0) {
1408 pr_err("snd_register_device failed %d\n", ret);
1409 return ret;
1410 }
1411 return ret;
1412
1413 }
1414
1415 static int snd_compress_dev_disconnect(struct snd_device *device)
1416 {
1417 struct snd_compr *compr;
1418
1419 compr = device->device_data;
1420 snd_unregister_device(compr->dev);
1421 return 0;
1422 }
1423
1424 #ifdef CONFIG_SND_VERBOSE_PROCFS
1425 static void snd_compress_proc_info_read(struct snd_info_entry *entry,
1426 struct snd_info_buffer *buffer)
1427 {
1428 struct snd_compr *compr = (struct snd_compr *)entry->private_data;
1429
1430 snd_iprintf(buffer, "card: %d\n", compr->card->number);
1431 snd_iprintf(buffer, "device: %d\n", compr->device);
1432 snd_iprintf(buffer, "stream: %s\n",
1433 compr->direction == SND_COMPRESS_PLAYBACK
1434 ? "PLAYBACK" : "CAPTURE");
1435 snd_iprintf(buffer, "id: %s\n", compr->id);
1436 }
1437
1438 static int snd_compress_proc_init(struct snd_compr *compr)
1439 {
1440 struct snd_info_entry *entry;
1441 char name[16];
1442
1443 sprintf(name, "compr%i", compr->device);
1444 entry = snd_info_create_card_entry(compr->card, name,
1445 compr->card->proc_root);
1446 if (!entry)
1447 return -ENOMEM;
1448 entry->mode = S_IFDIR | 0555;
1449 compr->proc_root = entry;
1450
1451 entry = snd_info_create_card_entry(compr->card, "info",
1452 compr->proc_root);
1453 if (entry)
1454 snd_info_set_text_ops(entry, compr,
1455 snd_compress_proc_info_read);
1456 compr->proc_info_entry = entry;
1457
1458 return 0;
1459 }
1460
1461 static void snd_compress_proc_done(struct snd_compr *compr)
1462 {
1463 snd_info_free_entry(compr->proc_info_entry);
1464 compr->proc_info_entry = NULL;
1465 snd_info_free_entry(compr->proc_root);
1466 compr->proc_root = NULL;
1467 }
1468
1469 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1470 {
1471 strscpy(compr->id, id, sizeof(compr->id));
1472 }
1473 #else
1474 static inline int snd_compress_proc_init(struct snd_compr *compr)
1475 {
1476 return 0;
1477 }
1478
1479 static inline void snd_compress_proc_done(struct snd_compr *compr)
1480 {
1481 }
1482
1483 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1484 {
1485 }
1486 #endif
1487
1488 static int snd_compress_dev_free(struct snd_device *device)
1489 {
1490 struct snd_compr *compr;
1491
1492 compr = device->device_data;
1493 snd_compress_proc_done(compr);
1494 put_device(compr->dev);
1495 return 0;
1496 }
1497
1498 /**
1499 * snd_compress_new: create new compress device
1500 * @card: sound card pointer
1501 * @device: device number
1502 * @dirn: device direction, should be of type enum snd_compr_direction
1503 * @id: ID string
1504 * @compr: compress device pointer
1505 *
1506 * Return: zero if successful, or a negative error code
1507 */
1508 int snd_compress_new(struct snd_card *card, int device,
1509 int dirn, const char *id, struct snd_compr *compr)
1510 {
1511 static const struct snd_device_ops ops = {
1512 .dev_free = snd_compress_dev_free,
1513 .dev_register = snd_compress_dev_register,
1514 .dev_disconnect = snd_compress_dev_disconnect,
1515 };
1516 int ret;
1517
1518 #if !IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
1519 if (snd_BUG_ON(dirn == SND_COMPRESS_ACCEL))
1520 return -EINVAL;
1521 #endif
1522
1523 compr->card = card;
1524 compr->device = device;
1525 compr->direction = dirn;
1526 mutex_init(&compr->lock);
1527
1528 snd_compress_set_id(compr, id);
1529
1530 ret = snd_device_alloc(&compr->dev, card);
1531 if (ret)
1532 return ret;
1533 dev_set_name(compr->dev, "comprC%iD%i", card->number, device);
1534
1535 ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
1536 if (ret == 0)
1537 snd_compress_proc_init(compr);
1538 else
1539 put_device(compr->dev);
1540
1541 return ret;
1542 }
1543 EXPORT_SYMBOL_GPL(snd_compress_new);
1544
1545 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1546 MODULE_AUTHOR("Vinod Koul <[email protected]>");
1547 MODULE_LICENSE("GPL v2");