2 * compress_core.c - compress offload core
4 * Copyright (C) 2011 Intel Corporation
5 * Authors: Vinod Koul <vinod.koul@linux.intel.com>
6 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
28 #include <linux/file.h>
30 #include <linux/list.h>
31 #include <linux/math64.h>
33 #include <linux/mutex.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/uio.h>
39 #include <linux/uaccess.h>
40 #include <linux/module.h>
41 #include <sound/core.h>
42 #include <sound/initval.h>
43 #include <sound/compress_params.h>
44 #include <sound/compress_offload.h>
45 #include <sound/compress_driver.h>
47 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
48 * architectures, so we need to disable the relevant ioctls.
50 #if _IOC_SIZEBITS < 14
51 #define COMPR_CODEC_CAPS_OVERFLOW
55 * - add substream support for multiple devices in case of
56 * SND_DYNAMIC_MINORS is not used
57 * - Multiple node representation
58 * driver should be able to register multiple nodes
61 static DEFINE_MUTEX(device_mutex
);
63 struct snd_compr_file
{
65 struct snd_compr_stream stream
;
69 * a note on stream states used:
70 * we use follwing states in the compressed core
71 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
72 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
73 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
74 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
75 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
76 * decoding/encoding and rendering/capturing data.
77 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
78 * by calling SNDRV_COMPRESS_DRAIN.
79 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
80 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
81 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
83 static int snd_compr_open(struct inode
*inode
, struct file
*f
)
85 struct snd_compr
*compr
;
86 struct snd_compr_file
*data
;
87 struct snd_compr_runtime
*runtime
;
88 enum snd_compr_direction dirn
;
89 int maj
= imajor(inode
);
92 if ((f
->f_flags
& O_ACCMODE
) == O_WRONLY
)
93 dirn
= SND_COMPRESS_PLAYBACK
;
94 else if ((f
->f_flags
& O_ACCMODE
) == O_RDONLY
)
95 dirn
= SND_COMPRESS_CAPTURE
;
100 compr
= snd_lookup_minor_data(iminor(inode
),
101 SNDRV_DEVICE_TYPE_COMPRESS
);
106 pr_err("no device data!!!\n");
110 if (dirn
!= compr
->direction
) {
111 pr_err("this device doesn't support this direction\n");
112 snd_card_unref(compr
->card
);
116 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
118 snd_card_unref(compr
->card
);
121 data
->stream
.ops
= compr
->ops
;
122 data
->stream
.direction
= dirn
;
123 data
->stream
.private_data
= compr
->private_data
;
124 data
->stream
.device
= compr
;
125 runtime
= kzalloc(sizeof(*runtime
), GFP_KERNEL
);
128 snd_card_unref(compr
->card
);
131 runtime
->state
= SNDRV_PCM_STATE_OPEN
;
132 init_waitqueue_head(&runtime
->sleep
);
133 data
->stream
.runtime
= runtime
;
134 f
->private_data
= (void *)data
;
135 mutex_lock(&compr
->lock
);
136 ret
= compr
->ops
->open(&data
->stream
);
137 mutex_unlock(&compr
->lock
);
142 snd_card_unref(compr
->card
);
146 static int snd_compr_free(struct inode
*inode
, struct file
*f
)
148 struct snd_compr_file
*data
= f
->private_data
;
149 data
->stream
.ops
->free(&data
->stream
);
150 kfree(data
->stream
.runtime
->buffer
);
151 kfree(data
->stream
.runtime
);
156 static int snd_compr_update_tstamp(struct snd_compr_stream
*stream
,
157 struct snd_compr_tstamp
*tstamp
)
159 if (!stream
->ops
->pointer
)
161 stream
->ops
->pointer(stream
, tstamp
);
162 pr_debug("dsp consumed till %d total %d bytes\n",
163 tstamp
->byte_offset
, tstamp
->copied_total
);
164 if (stream
->direction
== SND_COMPRESS_PLAYBACK
)
165 stream
->runtime
->total_bytes_transferred
= tstamp
->copied_total
;
167 stream
->runtime
->total_bytes_available
= tstamp
->copied_total
;
171 static size_t snd_compr_calc_avail(struct snd_compr_stream
*stream
,
172 struct snd_compr_avail
*avail
)
174 memset(avail
, 0, sizeof(*avail
));
175 snd_compr_update_tstamp(stream
, &avail
->tstamp
);
176 /* Still need to return avail even if tstamp can't be filled in */
178 if (stream
->runtime
->total_bytes_available
== 0 &&
179 stream
->runtime
->state
== SNDRV_PCM_STATE_SETUP
&&
180 stream
->direction
== SND_COMPRESS_PLAYBACK
) {
181 pr_debug("detected init and someone forgot to do a write\n");
182 return stream
->runtime
->buffer_size
;
184 pr_debug("app wrote %lld, DSP consumed %lld\n",
185 stream
->runtime
->total_bytes_available
,
186 stream
->runtime
->total_bytes_transferred
);
187 if (stream
->runtime
->total_bytes_available
==
188 stream
->runtime
->total_bytes_transferred
) {
189 if (stream
->direction
== SND_COMPRESS_PLAYBACK
) {
190 pr_debug("both pointers are same, returning full avail\n");
191 return stream
->runtime
->buffer_size
;
193 pr_debug("both pointers are same, returning no avail\n");
198 avail
->avail
= stream
->runtime
->total_bytes_available
-
199 stream
->runtime
->total_bytes_transferred
;
200 if (stream
->direction
== SND_COMPRESS_PLAYBACK
)
201 avail
->avail
= stream
->runtime
->buffer_size
- avail
->avail
;
203 pr_debug("ret avail as %lld\n", avail
->avail
);
207 static inline size_t snd_compr_get_avail(struct snd_compr_stream
*stream
)
209 struct snd_compr_avail avail
;
211 return snd_compr_calc_avail(stream
, &avail
);
215 snd_compr_ioctl_avail(struct snd_compr_stream
*stream
, unsigned long arg
)
217 struct snd_compr_avail ioctl_avail
;
220 avail
= snd_compr_calc_avail(stream
, &ioctl_avail
);
221 ioctl_avail
.avail
= avail
;
223 if (copy_to_user((__u64 __user
*)arg
,
224 &ioctl_avail
, sizeof(ioctl_avail
)))
229 static int snd_compr_write_data(struct snd_compr_stream
*stream
,
230 const char __user
*buf
, size_t count
)
234 struct snd_compr_runtime
*runtime
= stream
->runtime
;
236 u64 app_pointer
= div64_u64(runtime
->total_bytes_available
,
237 runtime
->buffer_size
);
238 app_pointer
= runtime
->total_bytes_available
-
239 (app_pointer
* runtime
->buffer_size
);
241 dstn
= runtime
->buffer
+ app_pointer
;
242 pr_debug("copying %ld at %lld\n",
243 (unsigned long)count
, app_pointer
);
244 if (count
< runtime
->buffer_size
- app_pointer
) {
245 if (copy_from_user(dstn
, buf
, count
))
248 copy
= runtime
->buffer_size
- app_pointer
;
249 if (copy_from_user(dstn
, buf
, copy
))
251 if (copy_from_user(runtime
->buffer
, buf
+ copy
, count
- copy
))
254 /* if DSP cares, let it know data has been written */
255 if (stream
->ops
->ack
)
256 stream
->ops
->ack(stream
, count
);
260 static ssize_t
snd_compr_write(struct file
*f
, const char __user
*buf
,
261 size_t count
, loff_t
*offset
)
263 struct snd_compr_file
*data
= f
->private_data
;
264 struct snd_compr_stream
*stream
;
268 if (snd_BUG_ON(!data
))
271 stream
= &data
->stream
;
272 mutex_lock(&stream
->device
->lock
);
273 /* write is allowed when stream is running or has been steup */
274 if (stream
->runtime
->state
!= SNDRV_PCM_STATE_SETUP
&&
275 stream
->runtime
->state
!= SNDRV_PCM_STATE_RUNNING
) {
276 mutex_unlock(&stream
->device
->lock
);
280 avail
= snd_compr_get_avail(stream
);
281 pr_debug("avail returned %ld\n", (unsigned long)avail
);
282 /* calculate how much we can write to buffer */
286 if (stream
->ops
->copy
) {
287 char __user
* cbuf
= (char __user
*)buf
;
288 retval
= stream
->ops
->copy(stream
, cbuf
, avail
);
290 retval
= snd_compr_write_data(stream
, buf
, avail
);
293 stream
->runtime
->total_bytes_available
+= retval
;
295 /* while initiating the stream, write should be called before START
296 * call, so in setup move state */
297 if (stream
->runtime
->state
== SNDRV_PCM_STATE_SETUP
) {
298 stream
->runtime
->state
= SNDRV_PCM_STATE_PREPARED
;
299 pr_debug("stream prepared, Houston we are good to go\n");
302 mutex_unlock(&stream
->device
->lock
);
307 static ssize_t
snd_compr_read(struct file
*f
, char __user
*buf
,
308 size_t count
, loff_t
*offset
)
310 struct snd_compr_file
*data
= f
->private_data
;
311 struct snd_compr_stream
*stream
;
315 if (snd_BUG_ON(!data
))
318 stream
= &data
->stream
;
319 mutex_lock(&stream
->device
->lock
);
321 /* read is allowed when stream is running, paused, draining and setup
322 * (yes setup is state which we transition to after stop, so if user
323 * wants to read data after stop we allow that)
325 switch (stream
->runtime
->state
) {
326 case SNDRV_PCM_STATE_OPEN
:
327 case SNDRV_PCM_STATE_PREPARED
:
328 case SNDRV_PCM_STATE_XRUN
:
329 case SNDRV_PCM_STATE_SUSPENDED
:
330 case SNDRV_PCM_STATE_DISCONNECTED
:
335 avail
= snd_compr_get_avail(stream
);
336 pr_debug("avail returned %ld\n", (unsigned long)avail
);
337 /* calculate how much we can read from buffer */
341 if (stream
->ops
->copy
) {
342 retval
= stream
->ops
->copy(stream
, buf
, avail
);
348 stream
->runtime
->total_bytes_transferred
+= retval
;
351 mutex_unlock(&stream
->device
->lock
);
355 static int snd_compr_mmap(struct file
*f
, struct vm_area_struct
*vma
)
360 static inline int snd_compr_get_poll(struct snd_compr_stream
*stream
)
362 if (stream
->direction
== SND_COMPRESS_PLAYBACK
)
363 return POLLOUT
| POLLWRNORM
;
365 return POLLIN
| POLLRDNORM
;
368 static unsigned int snd_compr_poll(struct file
*f
, poll_table
*wait
)
370 struct snd_compr_file
*data
= f
->private_data
;
371 struct snd_compr_stream
*stream
;
375 if (snd_BUG_ON(!data
))
377 stream
= &data
->stream
;
378 if (snd_BUG_ON(!stream
))
381 mutex_lock(&stream
->device
->lock
);
382 if (stream
->runtime
->state
== SNDRV_PCM_STATE_PAUSED
||
383 stream
->runtime
->state
== SNDRV_PCM_STATE_OPEN
) {
387 poll_wait(f
, &stream
->runtime
->sleep
, wait
);
389 avail
= snd_compr_get_avail(stream
);
390 pr_debug("avail is %ld\n", (unsigned long)avail
);
391 /* check if we have at least one fragment to fill */
392 switch (stream
->runtime
->state
) {
393 case SNDRV_PCM_STATE_DRAINING
:
394 /* stream has been woken up after drain is complete
395 * draining done so set stream state to stopped
397 retval
= snd_compr_get_poll(stream
);
398 stream
->runtime
->state
= SNDRV_PCM_STATE_SETUP
;
400 case SNDRV_PCM_STATE_RUNNING
:
401 case SNDRV_PCM_STATE_PREPARED
:
402 case SNDRV_PCM_STATE_PAUSED
:
403 if (avail
>= stream
->runtime
->fragment_size
)
404 retval
= snd_compr_get_poll(stream
);
407 if (stream
->direction
== SND_COMPRESS_PLAYBACK
)
408 retval
= POLLOUT
| POLLWRNORM
| POLLERR
;
410 retval
= POLLIN
| POLLRDNORM
| POLLERR
;
414 mutex_unlock(&stream
->device
->lock
);
419 snd_compr_get_caps(struct snd_compr_stream
*stream
, unsigned long arg
)
422 struct snd_compr_caps caps
;
424 if (!stream
->ops
->get_caps
)
427 memset(&caps
, 0, sizeof(caps
));
428 retval
= stream
->ops
->get_caps(stream
, &caps
);
431 if (copy_to_user((void __user
*)arg
, &caps
, sizeof(caps
)))
437 #ifndef COMPR_CODEC_CAPS_OVERFLOW
439 snd_compr_get_codec_caps(struct snd_compr_stream
*stream
, unsigned long arg
)
442 struct snd_compr_codec_caps
*caps
;
444 if (!stream
->ops
->get_codec_caps
)
447 caps
= kzalloc(sizeof(*caps
), GFP_KERNEL
);
451 retval
= stream
->ops
->get_codec_caps(stream
, caps
);
454 if (copy_to_user((void __user
*)arg
, caps
, sizeof(*caps
)))
461 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
463 /* revisit this with snd_pcm_preallocate_xxx */
464 static int snd_compr_allocate_buffer(struct snd_compr_stream
*stream
,
465 struct snd_compr_params
*params
)
467 unsigned int buffer_size
;
470 buffer_size
= params
->buffer
.fragment_size
* params
->buffer
.fragments
;
471 if (stream
->ops
->copy
) {
473 /* if copy is defined the driver will be required to copy
477 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
481 stream
->runtime
->fragment_size
= params
->buffer
.fragment_size
;
482 stream
->runtime
->fragments
= params
->buffer
.fragments
;
483 stream
->runtime
->buffer
= buffer
;
484 stream
->runtime
->buffer_size
= buffer_size
;
488 static int snd_compress_check_input(struct snd_compr_params
*params
)
490 /* first let's check the buffer parameter's */
491 if (params
->buffer
.fragment_size
== 0 ||
492 params
->buffer
.fragments
> INT_MAX
/ params
->buffer
.fragment_size
)
495 /* now codec parameters */
496 if (params
->codec
.id
== 0 || params
->codec
.id
> SND_AUDIOCODEC_MAX
)
499 if (params
->codec
.ch_in
== 0 || params
->codec
.ch_out
== 0)
502 if (!(params
->codec
.sample_rate
& SNDRV_PCM_RATE_8000_192000
))
509 snd_compr_set_params(struct snd_compr_stream
*stream
, unsigned long arg
)
511 struct snd_compr_params
*params
;
514 if (stream
->runtime
->state
== SNDRV_PCM_STATE_OPEN
) {
516 * we should allow parameter change only when stream has been
517 * opened not in other cases
519 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
522 if (copy_from_user(params
, (void __user
*)arg
, sizeof(*params
))) {
527 retval
= snd_compress_check_input(params
);
531 retval
= snd_compr_allocate_buffer(stream
, params
);
537 retval
= stream
->ops
->set_params(stream
, params
);
541 stream
->metadata_set
= false;
542 stream
->next_track
= false;
544 if (stream
->direction
== SND_COMPRESS_PLAYBACK
)
545 stream
->runtime
->state
= SNDRV_PCM_STATE_SETUP
;
547 stream
->runtime
->state
= SNDRV_PCM_STATE_PREPARED
;
557 snd_compr_get_params(struct snd_compr_stream
*stream
, unsigned long arg
)
559 struct snd_codec
*params
;
562 if (!stream
->ops
->get_params
)
565 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
568 retval
= stream
->ops
->get_params(stream
, params
);
571 if (copy_to_user((char __user
*)arg
, params
, sizeof(*params
)))
580 snd_compr_get_metadata(struct snd_compr_stream
*stream
, unsigned long arg
)
582 struct snd_compr_metadata metadata
;
585 if (!stream
->ops
->get_metadata
)
588 if (copy_from_user(&metadata
, (void __user
*)arg
, sizeof(metadata
)))
591 retval
= stream
->ops
->get_metadata(stream
, &metadata
);
595 if (copy_to_user((void __user
*)arg
, &metadata
, sizeof(metadata
)))
602 snd_compr_set_metadata(struct snd_compr_stream
*stream
, unsigned long arg
)
604 struct snd_compr_metadata metadata
;
607 if (!stream
->ops
->set_metadata
)
610 * we should allow parameter change only when stream has been
611 * opened not in other cases
613 if (copy_from_user(&metadata
, (void __user
*)arg
, sizeof(metadata
)))
616 retval
= stream
->ops
->set_metadata(stream
, &metadata
);
617 stream
->metadata_set
= true;
623 snd_compr_tstamp(struct snd_compr_stream
*stream
, unsigned long arg
)
625 struct snd_compr_tstamp tstamp
= {0};
628 ret
= snd_compr_update_tstamp(stream
, &tstamp
);
630 ret
= copy_to_user((struct snd_compr_tstamp __user
*)arg
,
631 &tstamp
, sizeof(tstamp
)) ? -EFAULT
: 0;
635 static int snd_compr_pause(struct snd_compr_stream
*stream
)
639 if (stream
->runtime
->state
!= SNDRV_PCM_STATE_RUNNING
)
641 retval
= stream
->ops
->trigger(stream
, SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
643 stream
->runtime
->state
= SNDRV_PCM_STATE_PAUSED
;
647 static int snd_compr_resume(struct snd_compr_stream
*stream
)
651 if (stream
->runtime
->state
!= SNDRV_PCM_STATE_PAUSED
)
653 retval
= stream
->ops
->trigger(stream
, SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
655 stream
->runtime
->state
= SNDRV_PCM_STATE_RUNNING
;
659 static int snd_compr_start(struct snd_compr_stream
*stream
)
663 if (stream
->runtime
->state
!= SNDRV_PCM_STATE_PREPARED
)
665 retval
= stream
->ops
->trigger(stream
, SNDRV_PCM_TRIGGER_START
);
667 stream
->runtime
->state
= SNDRV_PCM_STATE_RUNNING
;
671 static int snd_compr_stop(struct snd_compr_stream
*stream
)
675 if (stream
->runtime
->state
== SNDRV_PCM_STATE_PREPARED
||
676 stream
->runtime
->state
== SNDRV_PCM_STATE_SETUP
)
678 retval
= stream
->ops
->trigger(stream
, SNDRV_PCM_TRIGGER_STOP
);
680 snd_compr_drain_notify(stream
);
681 stream
->runtime
->total_bytes_available
= 0;
682 stream
->runtime
->total_bytes_transferred
= 0;
687 static int snd_compress_wait_for_drain(struct snd_compr_stream
*stream
)
692 * We are called with lock held. So drop the lock while we wait for
693 * drain complete notfication from the driver
695 * It is expected that driver will notify the drain completion and then
696 * stream will be moved to SETUP state, even if draining resulted in an
697 * error. We can trigger next track after this.
699 stream
->runtime
->state
= SNDRV_PCM_STATE_DRAINING
;
700 mutex_unlock(&stream
->device
->lock
);
702 /* we wait for drain to complete here, drain can return when
703 * interruption occurred, wait returned error or success.
704 * For the first two cases we don't do anything different here and
705 * return after waking up
708 ret
= wait_event_interruptible(stream
->runtime
->sleep
,
709 (stream
->runtime
->state
!= SNDRV_PCM_STATE_DRAINING
));
710 if (ret
== -ERESTARTSYS
)
711 pr_debug("wait aborted by a signal");
713 pr_debug("wait for drain failed with %d\n", ret
);
716 wake_up(&stream
->runtime
->sleep
);
717 mutex_lock(&stream
->device
->lock
);
722 static int snd_compr_drain(struct snd_compr_stream
*stream
)
726 if (stream
->runtime
->state
== SNDRV_PCM_STATE_PREPARED
||
727 stream
->runtime
->state
== SNDRV_PCM_STATE_SETUP
)
730 retval
= stream
->ops
->trigger(stream
, SND_COMPR_TRIGGER_DRAIN
);
732 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval
);
733 wake_up(&stream
->runtime
->sleep
);
737 return snd_compress_wait_for_drain(stream
);
740 static int snd_compr_next_track(struct snd_compr_stream
*stream
)
744 /* only a running stream can transition to next track */
745 if (stream
->runtime
->state
!= SNDRV_PCM_STATE_RUNNING
)
748 /* you can signal next track isf this is intended to be a gapless stream
749 * and current track metadata is set
751 if (stream
->metadata_set
== false)
754 retval
= stream
->ops
->trigger(stream
, SND_COMPR_TRIGGER_NEXT_TRACK
);
757 stream
->metadata_set
= false;
758 stream
->next_track
= true;
762 static int snd_compr_partial_drain(struct snd_compr_stream
*stream
)
765 if (stream
->runtime
->state
== SNDRV_PCM_STATE_PREPARED
||
766 stream
->runtime
->state
== SNDRV_PCM_STATE_SETUP
)
768 /* stream can be drained only when next track has been signalled */
769 if (stream
->next_track
== false)
772 retval
= stream
->ops
->trigger(stream
, SND_COMPR_TRIGGER_PARTIAL_DRAIN
);
774 pr_debug("Partial drain returned failure\n");
775 wake_up(&stream
->runtime
->sleep
);
779 stream
->next_track
= false;
780 return snd_compress_wait_for_drain(stream
);
783 static long snd_compr_ioctl(struct file
*f
, unsigned int cmd
, unsigned long arg
)
785 struct snd_compr_file
*data
= f
->private_data
;
786 struct snd_compr_stream
*stream
;
787 int retval
= -ENOTTY
;
789 if (snd_BUG_ON(!data
))
791 stream
= &data
->stream
;
792 if (snd_BUG_ON(!stream
))
794 mutex_lock(&stream
->device
->lock
);
795 switch (_IOC_NR(cmd
)) {
796 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION
):
797 retval
= put_user(SNDRV_COMPRESS_VERSION
,
798 (int __user
*)arg
) ? -EFAULT
: 0;
800 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS
):
801 retval
= snd_compr_get_caps(stream
, arg
);
803 #ifndef COMPR_CODEC_CAPS_OVERFLOW
804 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS
):
805 retval
= snd_compr_get_codec_caps(stream
, arg
);
808 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS
):
809 retval
= snd_compr_set_params(stream
, arg
);
811 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS
):
812 retval
= snd_compr_get_params(stream
, arg
);
814 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA
):
815 retval
= snd_compr_set_metadata(stream
, arg
);
817 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA
):
818 retval
= snd_compr_get_metadata(stream
, arg
);
820 case _IOC_NR(SNDRV_COMPRESS_TSTAMP
):
821 retval
= snd_compr_tstamp(stream
, arg
);
823 case _IOC_NR(SNDRV_COMPRESS_AVAIL
):
824 retval
= snd_compr_ioctl_avail(stream
, arg
);
826 case _IOC_NR(SNDRV_COMPRESS_PAUSE
):
827 retval
= snd_compr_pause(stream
);
829 case _IOC_NR(SNDRV_COMPRESS_RESUME
):
830 retval
= snd_compr_resume(stream
);
832 case _IOC_NR(SNDRV_COMPRESS_START
):
833 retval
= snd_compr_start(stream
);
835 case _IOC_NR(SNDRV_COMPRESS_STOP
):
836 retval
= snd_compr_stop(stream
);
838 case _IOC_NR(SNDRV_COMPRESS_DRAIN
):
839 retval
= snd_compr_drain(stream
);
841 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN
):
842 retval
= snd_compr_partial_drain(stream
);
844 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK
):
845 retval
= snd_compr_next_track(stream
);
849 mutex_unlock(&stream
->device
->lock
);
853 static const struct file_operations snd_compr_file_ops
= {
854 .owner
= THIS_MODULE
,
855 .open
= snd_compr_open
,
856 .release
= snd_compr_free
,
857 .write
= snd_compr_write
,
858 .read
= snd_compr_read
,
859 .unlocked_ioctl
= snd_compr_ioctl
,
860 .mmap
= snd_compr_mmap
,
861 .poll
= snd_compr_poll
,
864 static int snd_compress_dev_register(struct snd_device
*device
)
868 struct snd_compr
*compr
;
870 if (snd_BUG_ON(!device
|| !device
->device_data
))
872 compr
= device
->device_data
;
874 sprintf(str
, "comprC%iD%i", compr
->card
->number
, compr
->device
);
875 pr_debug("reg %s for device %s, direction %d\n", str
, compr
->name
,
877 /* register compressed device */
878 ret
= snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS
, compr
->card
,
879 compr
->device
, &snd_compr_file_ops
, compr
, str
);
881 pr_err("snd_register_device failed\n %d", ret
);
888 static int snd_compress_dev_disconnect(struct snd_device
*device
)
890 struct snd_compr
*compr
;
892 compr
= device
->device_data
;
893 snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS
, compr
->card
,
899 * snd_compress_new: create new compress device
900 * @card: sound card pointer
901 * @device: device number
902 * @dirn: device direction, should be of type enum snd_compr_direction
903 * @compr: compress device pointer
905 int snd_compress_new(struct snd_card
*card
, int device
,
906 int dirn
, struct snd_compr
*compr
)
908 static struct snd_device_ops ops
= {
910 .dev_register
= snd_compress_dev_register
,
911 .dev_disconnect
= snd_compress_dev_disconnect
,
915 compr
->device
= device
;
916 compr
->direction
= dirn
;
917 return snd_device_new(card
, SNDRV_DEV_COMPRESS
, compr
, &ops
);
919 EXPORT_SYMBOL_GPL(snd_compress_new
);
921 static int snd_compress_add_device(struct snd_compr
*device
)
928 /* register the card */
929 ret
= snd_card_register(device
->card
);
935 pr_err("failed with %d\n", ret
);
940 static int snd_compress_remove_device(struct snd_compr
*device
)
942 return snd_card_free(device
->card
);
946 * snd_compress_register - register compressed device
948 * @device: compressed device to register
950 int snd_compress_register(struct snd_compr
*device
)
954 if (device
->name
== NULL
|| device
->dev
== NULL
|| device
->ops
== NULL
)
957 pr_debug("Registering compressed device %s\n", device
->name
);
958 if (snd_BUG_ON(!device
->ops
->open
))
960 if (snd_BUG_ON(!device
->ops
->free
))
962 if (snd_BUG_ON(!device
->ops
->set_params
))
964 if (snd_BUG_ON(!device
->ops
->trigger
))
967 mutex_init(&device
->lock
);
969 /* register a compressed card */
970 mutex_lock(&device_mutex
);
971 retval
= snd_compress_add_device(device
);
972 mutex_unlock(&device_mutex
);
975 EXPORT_SYMBOL_GPL(snd_compress_register
);
977 int snd_compress_deregister(struct snd_compr
*device
)
979 pr_debug("Removing compressed device %s\n", device
->name
);
980 mutex_lock(&device_mutex
);
981 snd_compress_remove_device(device
);
982 mutex_unlock(&device_mutex
);
985 EXPORT_SYMBOL_GPL(snd_compress_deregister
);
987 static int __init
snd_compress_init(void)
992 static void __exit
snd_compress_exit(void)
996 module_init(snd_compress_init
);
997 module_exit(snd_compress_exit
);
999 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1000 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1001 MODULE_LICENSE("GPL v2");