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 <linux/compat.h>
42 #include <sound/core.h>
43 #include <sound/initval.h>
44 #include <sound/compress_params.h>
45 #include <sound/compress_offload.h>
46 #include <sound/compress_driver.h>
48 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
49 * architectures, so we need to disable the relevant ioctls.
51 #if _IOC_SIZEBITS < 14
52 #define COMPR_CODEC_CAPS_OVERFLOW
56 * - add substream support for multiple devices in case of
57 * SND_DYNAMIC_MINORS is not used
58 * - Multiple node representation
59 * driver should be able to register multiple nodes
62 static DEFINE_MUTEX(device_mutex);
64 struct snd_compr_file {
66 struct snd_compr_stream stream;
70 * a note on stream states used:
71 * we use follwing states in the compressed core
72 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
73 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
74 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
75 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
76 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
77 * decoding/encoding and rendering/capturing data.
78 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
79 * by calling SNDRV_COMPRESS_DRAIN.
80 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
81 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
82 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
84 static int snd_compr_open(struct inode *inode, struct file *f)
86 struct snd_compr *compr;
87 struct snd_compr_file *data;
88 struct snd_compr_runtime *runtime;
89 enum snd_compr_direction dirn;
90 int maj = imajor(inode);
93 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
94 dirn = SND_COMPRESS_PLAYBACK;
95 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
96 dirn = SND_COMPRESS_CAPTURE;
100 if (maj == snd_major)
101 compr = snd_lookup_minor_data(iminor(inode),
102 SNDRV_DEVICE_TYPE_COMPRESS);
107 pr_err("no device data!!!\n");
111 if (dirn != compr->direction) {
112 pr_err("this device doesn't support this direction\n");
113 snd_card_unref(compr->card);
117 data = kzalloc(sizeof(*data), GFP_KERNEL);
119 snd_card_unref(compr->card);
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);
129 snd_card_unref(compr->card);
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);
143 snd_card_unref(compr->card);
147 static int snd_compr_free(struct inode *inode, struct file *f)
149 struct snd_compr_file *data = f->private_data;
150 struct snd_compr_runtime *runtime = data->stream.runtime;
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);
162 data->stream.ops->free(&data->stream);
163 kfree(data->stream.runtime->buffer);
164 kfree(data->stream.runtime);
169 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
170 struct snd_compr_tstamp *tstamp)
173 if (!stream->ops->pointer)
175 err = stream->ops->pointer(stream, tstamp);
178 pr_debug("dsp consumed till %d total %llu 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;
183 stream->runtime->total_bytes_available = tstamp->copied_total;
187 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
188 struct snd_compr_avail *avail)
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 */
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;
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;
209 pr_debug("both pointers are same, returning no avail\n");
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;
219 pr_debug("ret avail as %lld\n", avail->avail);
223 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
225 struct snd_compr_avail avail;
227 return snd_compr_calc_avail(stream, &avail);
231 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
233 struct snd_compr_avail ioctl_avail;
236 avail = snd_compr_calc_avail(stream, &ioctl_avail);
237 ioctl_avail.avail = avail;
239 if (copy_to_user((__u64 __user *)arg,
240 &ioctl_avail, sizeof(ioctl_avail)))
245 static int snd_compr_write_data(struct snd_compr_stream *stream,
246 const char __user *buf, size_t count)
250 struct snd_compr_runtime *runtime = stream->runtime;
252 u64 app_pointer = div64_u64(runtime->total_bytes_available,
253 runtime->buffer_size);
254 app_pointer = runtime->total_bytes_available -
255 (app_pointer * runtime->buffer_size);
257 dstn = runtime->buffer + app_pointer;
258 pr_debug("copying %zu at %lld\n",
260 if (count < runtime->buffer_size - app_pointer) {
261 if (copy_from_user(dstn, buf, count))
264 copy = runtime->buffer_size - app_pointer;
265 if (copy_from_user(dstn, buf, copy))
267 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
270 /* if DSP cares, let it know data has been written */
271 if (stream->ops->ack)
272 stream->ops->ack(stream, count);
276 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
277 size_t count, loff_t *offset)
279 struct snd_compr_file *data = f->private_data;
280 struct snd_compr_stream *stream;
284 if (snd_BUG_ON(!data))
287 stream = &data->stream;
288 mutex_lock(&stream->device->lock);
289 /* write is allowed when stream is running or has been steup */
290 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
291 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
292 mutex_unlock(&stream->device->lock);
296 avail = snd_compr_get_avail(stream);
297 pr_debug("avail returned %zu\n", avail);
298 /* calculate how much we can write to buffer */
302 if (stream->ops->copy) {
303 char __user* cbuf = (char __user*)buf;
304 retval = stream->ops->copy(stream, cbuf, avail);
306 retval = snd_compr_write_data(stream, buf, avail);
309 stream->runtime->total_bytes_available += retval;
311 /* while initiating the stream, write should be called before START
312 * call, so in setup move state */
313 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
314 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
315 pr_debug("stream prepared, Houston we are good to go\n");
318 mutex_unlock(&stream->device->lock);
323 static ssize_t snd_compr_read(struct file *f, char __user *buf,
324 size_t count, loff_t *offset)
326 struct snd_compr_file *data = f->private_data;
327 struct snd_compr_stream *stream;
331 if (snd_BUG_ON(!data))
334 stream = &data->stream;
335 mutex_lock(&stream->device->lock);
337 /* read is allowed when stream is running, paused, draining and setup
338 * (yes setup is state which we transition to after stop, so if user
339 * wants to read data after stop we allow that)
341 switch (stream->runtime->state) {
342 case SNDRV_PCM_STATE_OPEN:
343 case SNDRV_PCM_STATE_PREPARED:
344 case SNDRV_PCM_STATE_XRUN:
345 case SNDRV_PCM_STATE_SUSPENDED:
346 case SNDRV_PCM_STATE_DISCONNECTED:
351 avail = snd_compr_get_avail(stream);
352 pr_debug("avail returned %zu\n", avail);
353 /* calculate how much we can read from buffer */
357 if (stream->ops->copy) {
358 retval = stream->ops->copy(stream, buf, avail);
364 stream->runtime->total_bytes_transferred += retval;
367 mutex_unlock(&stream->device->lock);
371 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
376 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
378 if (stream->direction == SND_COMPRESS_PLAYBACK)
379 return POLLOUT | POLLWRNORM;
381 return POLLIN | POLLRDNORM;
384 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
386 struct snd_compr_file *data = f->private_data;
387 struct snd_compr_stream *stream;
391 if (snd_BUG_ON(!data))
393 stream = &data->stream;
394 if (snd_BUG_ON(!stream))
397 mutex_lock(&stream->device->lock);
398 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
402 poll_wait(f, &stream->runtime->sleep, wait);
404 avail = snd_compr_get_avail(stream);
405 pr_debug("avail is %zu\n", avail);
406 /* check if we have at least one fragment to fill */
407 switch (stream->runtime->state) {
408 case SNDRV_PCM_STATE_DRAINING:
409 /* stream has been woken up after drain is complete
410 * draining done so set stream state to stopped
412 retval = snd_compr_get_poll(stream);
413 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
415 case SNDRV_PCM_STATE_RUNNING:
416 case SNDRV_PCM_STATE_PREPARED:
417 case SNDRV_PCM_STATE_PAUSED:
418 if (avail >= stream->runtime->fragment_size)
419 retval = snd_compr_get_poll(stream);
422 if (stream->direction == SND_COMPRESS_PLAYBACK)
423 retval = POLLOUT | POLLWRNORM | POLLERR;
425 retval = POLLIN | POLLRDNORM | POLLERR;
429 mutex_unlock(&stream->device->lock);
434 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
437 struct snd_compr_caps caps;
439 if (!stream->ops->get_caps)
442 memset(&caps, 0, sizeof(caps));
443 retval = stream->ops->get_caps(stream, &caps);
446 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
452 #ifndef COMPR_CODEC_CAPS_OVERFLOW
454 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
457 struct snd_compr_codec_caps *caps;
459 if (!stream->ops->get_codec_caps)
462 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
466 retval = stream->ops->get_codec_caps(stream, caps);
469 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
476 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
478 /* revisit this with snd_pcm_preallocate_xxx */
479 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
480 struct snd_compr_params *params)
482 unsigned int buffer_size;
485 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
486 if (stream->ops->copy) {
488 /* if copy is defined the driver will be required to copy
492 buffer = kmalloc(buffer_size, GFP_KERNEL);
496 stream->runtime->fragment_size = params->buffer.fragment_size;
497 stream->runtime->fragments = params->buffer.fragments;
498 stream->runtime->buffer = buffer;
499 stream->runtime->buffer_size = buffer_size;
503 static int snd_compress_check_input(struct snd_compr_params *params)
505 /* first let's check the buffer parameter's */
506 if (params->buffer.fragment_size == 0 ||
507 params->buffer.fragments > U32_MAX / params->buffer.fragment_size ||
508 params->buffer.fragments == 0)
511 /* now codec parameters */
512 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
515 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
522 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
524 struct snd_compr_params *params;
527 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
529 * we should allow parameter change only when stream has been
530 * opened not in other cases
532 params = kmalloc(sizeof(*params), GFP_KERNEL);
535 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
540 retval = snd_compress_check_input(params);
544 retval = snd_compr_allocate_buffer(stream, params);
550 retval = stream->ops->set_params(stream, params);
554 stream->metadata_set = false;
555 stream->next_track = false;
557 if (stream->direction == SND_COMPRESS_PLAYBACK)
558 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
560 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
570 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
572 struct snd_codec *params;
575 if (!stream->ops->get_params)
578 params = kzalloc(sizeof(*params), GFP_KERNEL);
581 retval = stream->ops->get_params(stream, params);
584 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
593 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
595 struct snd_compr_metadata metadata;
598 if (!stream->ops->get_metadata)
601 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
604 retval = stream->ops->get_metadata(stream, &metadata);
608 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
615 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
617 struct snd_compr_metadata metadata;
620 if (!stream->ops->set_metadata)
623 * we should allow parameter change only when stream has been
624 * opened not in other cases
626 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
629 retval = stream->ops->set_metadata(stream, &metadata);
630 stream->metadata_set = true;
636 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
638 struct snd_compr_tstamp tstamp;
641 memset(&tstamp, 0, sizeof(tstamp));
642 ret = snd_compr_update_tstamp(stream, &tstamp);
644 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
645 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
649 static int snd_compr_pause(struct snd_compr_stream *stream)
653 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
655 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
657 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
661 static int snd_compr_resume(struct snd_compr_stream *stream)
665 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
667 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
669 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
673 static int snd_compr_start(struct snd_compr_stream *stream)
677 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
679 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
681 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
685 static int snd_compr_stop(struct snd_compr_stream *stream)
689 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
690 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
692 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
694 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
695 wake_up(&stream->runtime->sleep);
696 stream->runtime->total_bytes_available = 0;
697 stream->runtime->total_bytes_transferred = 0;
702 /* this fn is called without lock being held and we change stream states here
703 * so while using the stream state auquire the lock but relase before invoking
704 * DSP as the call will possibly take a while
706 static int snd_compr_drain(struct snd_compr_stream *stream)
710 mutex_lock(&stream->device->lock);
711 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
712 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
716 mutex_unlock(&stream->device->lock);
717 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
718 mutex_lock(&stream->device->lock);
720 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
721 wake_up(&stream->runtime->sleep);
725 mutex_unlock(&stream->device->lock);
729 static int snd_compr_next_track(struct snd_compr_stream *stream)
733 /* only a running stream can transition to next track */
734 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
737 /* you can signal next track isf this is intended to be a gapless stream
738 * and current track metadata is set
740 if (stream->metadata_set == false)
743 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
746 stream->metadata_set = false;
747 stream->next_track = true;
751 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
755 mutex_lock(&stream->device->lock);
756 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
757 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
758 mutex_unlock(&stream->device->lock);
761 mutex_unlock(&stream->device->lock);
762 /* stream can be drained only when next track has been signalled */
763 if (stream->next_track == false)
766 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
768 stream->next_track = false;
772 static int snd_compr_set_next_track_param(struct snd_compr_stream *stream,
775 union snd_codec_options codec_options;
778 /* set next track params when stream is running or has been setup */
779 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
780 stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
783 if (copy_from_user(&codec_options, (void __user *)arg,
784 sizeof(codec_options)))
787 retval = stream->ops->set_next_track_param(stream, &codec_options);
791 static int snd_compress_simple_ioctls(struct file *file,
792 struct snd_compr_stream *stream,
793 unsigned int cmd, unsigned long arg)
795 int retval = -ENOTTY;
797 switch (_IOC_NR(cmd)) {
798 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
799 retval = put_user(SNDRV_COMPRESS_VERSION,
800 (int __user *)arg) ? -EFAULT : 0;
803 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
804 retval = snd_compr_get_caps(stream, arg);
807 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
808 retval = snd_compr_get_codec_caps(stream, arg);
812 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
813 retval = snd_compr_tstamp(stream, arg);
816 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
817 retval = snd_compr_ioctl_avail(stream, arg);
820 /* drain and partial drain need special handling
821 * we need to drop the locks here as the streams would get blocked on
822 * the dsp to get drained. The locking would be handled in respective
825 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
826 retval = snd_compr_drain(stream);
829 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
830 retval = snd_compr_partial_drain(stream);
837 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
839 struct snd_compr_file *data = f->private_data;
840 struct snd_compr_stream *stream;
841 int retval = -ENOTTY;
843 if (snd_BUG_ON(!data))
845 stream = &data->stream;
846 if (snd_BUG_ON(!stream))
849 mutex_lock(&stream->device->lock);
850 switch (_IOC_NR(cmd)) {
851 #ifndef COMPR_CODEC_CAPS_OVERFLOW
852 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
853 retval = snd_compr_get_codec_caps(stream, arg);
856 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
857 retval = snd_compr_set_params(stream, arg);
860 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
861 retval = snd_compr_get_params(stream, arg);
864 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
865 retval = snd_compr_set_metadata(stream, arg);
868 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
869 retval = snd_compr_get_metadata(stream, arg);
872 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
873 retval = snd_compr_pause(stream);
876 case _IOC_NR(SNDRV_COMPRESS_RESUME):
877 retval = snd_compr_resume(stream);
880 case _IOC_NR(SNDRV_COMPRESS_START):
881 retval = snd_compr_start(stream);
884 case _IOC_NR(SNDRV_COMPRESS_STOP):
885 retval = snd_compr_stop(stream);
888 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
889 retval = snd_compr_next_track(stream);
892 case _IOC_NR(SNDRV_COMPRESS_SET_NEXT_TRACK_PARAM):
893 retval = snd_compr_set_next_track_param(stream, arg);
897 mutex_unlock(&stream->device->lock);
898 return snd_compress_simple_ioctls(f, stream, cmd, arg);
902 mutex_unlock(&stream->device->lock);
906 /* support of 32bit userspace on 64bit platforms */
908 static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
911 return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
915 static const struct file_operations snd_compr_file_ops = {
916 .owner = THIS_MODULE,
917 .open = snd_compr_open,
918 .release = snd_compr_free,
919 .write = snd_compr_write,
920 .read = snd_compr_read,
921 .unlocked_ioctl = snd_compr_ioctl,
923 .compat_ioctl = snd_compr_ioctl_compat,
925 .compat_ioctl = snd_compr_ioctl,
927 .mmap = snd_compr_mmap,
928 .poll = snd_compr_poll,
931 static int snd_compress_dev_register(struct snd_device *device)
934 struct snd_compr *compr;
936 if (snd_BUG_ON(!device || !device->device_data))
938 compr = device->device_data;
940 pr_debug("reg device %s, direction %d\n", compr->name,
942 /* register compressed device */
943 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
944 compr->card, compr->device,
945 &snd_compr_file_ops, compr, &compr->dev);
947 pr_err("snd_register_device failed\n %d", ret);
954 static int snd_compress_dev_disconnect(struct snd_device *device)
956 struct snd_compr *compr;
958 compr = device->device_data;
959 snd_unregister_device(&compr->dev);
963 static int snd_compress_dev_free(struct snd_device *device)
965 struct snd_compr *compr;
967 compr = device->device_data;
968 put_device(&compr->dev);
973 * snd_compress_new: create new compress device
974 * @card: sound card pointer
975 * @device: device number
976 * @dirn: device direction, should be of type enum snd_compr_direction
977 * @compr: compress device pointer
979 int snd_compress_new(struct snd_card *card, int device,
980 int dirn, struct snd_compr *compr)
982 static struct snd_device_ops ops = {
983 .dev_free = snd_compress_dev_free,
984 .dev_register = snd_compress_dev_register,
985 .dev_disconnect = snd_compress_dev_disconnect,
989 compr->device = device;
990 compr->direction = dirn;
992 snd_device_initialize(&compr->dev, card);
993 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
995 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
997 EXPORT_SYMBOL_GPL(snd_compress_new);
1000 * snd_compress_free: free compress device
1001 * @card: sound card pointer
1002 * @compr: compress device pointer
1004 void snd_compress_free(struct snd_card *card, struct snd_compr *compr)
1006 snd_device_free(card, compr);
1008 EXPORT_SYMBOL_GPL(snd_compress_free);
1010 static int snd_compress_add_device(struct snd_compr *device)
1017 /* register the card */
1018 ret = snd_card_register(device->card);
1024 pr_err("failed with %d\n", ret);
1029 static int snd_compress_remove_device(struct snd_compr *device)
1031 return snd_card_free(device->card);
1035 * snd_compress_register - register compressed device
1037 * @device: compressed device to register
1039 int snd_compress_register(struct snd_compr *device)
1043 if (device->name == NULL || device->ops == NULL)
1046 pr_debug("Registering compressed device %s\n", device->name);
1047 if (snd_BUG_ON(!device->ops->open))
1049 if (snd_BUG_ON(!device->ops->free))
1051 if (snd_BUG_ON(!device->ops->set_params))
1053 if (snd_BUG_ON(!device->ops->trigger))
1056 mutex_init(&device->lock);
1058 /* register a compressed card */
1059 mutex_lock(&device_mutex);
1060 retval = snd_compress_add_device(device);
1061 mutex_unlock(&device_mutex);
1064 EXPORT_SYMBOL_GPL(snd_compress_register);
1066 int snd_compress_deregister(struct snd_compr *device)
1068 pr_debug("Removing compressed device %s\n", device->name);
1069 mutex_lock(&device_mutex);
1070 snd_compress_remove_device(device);
1071 mutex_unlock(&device_mutex);
1074 EXPORT_SYMBOL_GPL(snd_compress_deregister);
1076 static int __init snd_compress_init(void)
1081 static void __exit snd_compress_exit(void)
1085 module_init(snd_compress_init);
1086 module_exit(snd_compress_exit);
1088 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1089 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1090 MODULE_LICENSE("GPL v2");