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/info.h>
45 #include <sound/compress_params.h>
46 #include <sound/compress_offload.h>
47 #include <sound/compress_driver.h>
49 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
50 * architectures, so we need to disable the relevant ioctls.
52 #if _IOC_SIZEBITS < 14
53 #define COMPR_CODEC_CAPS_OVERFLOW
57 * - add substream support for multiple devices in case of
58 * SND_DYNAMIC_MINORS is not used
59 * - Multiple node representation
60 * driver should be able to register multiple nodes
63 static DEFINE_MUTEX(device_mutex);
65 struct snd_compr_file {
67 struct snd_compr_stream stream;
71 * a note on stream states used:
72 * we use following states in the compressed core
73 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
74 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
75 * calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
76 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
77 * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
78 * playback only). User after setting up stream writes the data buffer
79 * before starting the stream.
80 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
81 * decoding/encoding and rendering/capturing data.
82 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
83 * by calling SNDRV_COMPRESS_DRAIN.
84 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
85 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
86 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
88 static int snd_compr_open(struct inode *inode, struct file *f)
90 struct snd_compr *compr;
91 struct snd_compr_file *data;
92 struct snd_compr_runtime *runtime;
93 enum snd_compr_direction dirn;
94 int maj = imajor(inode);
97 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
98 dirn = SND_COMPRESS_PLAYBACK;
99 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
100 dirn = SND_COMPRESS_CAPTURE;
104 if (maj == snd_major)
105 compr = snd_lookup_minor_data(iminor(inode),
106 SNDRV_DEVICE_TYPE_COMPRESS);
111 pr_err("no device data!!!\n");
115 if (dirn != compr->direction) {
116 pr_err("this device doesn't support this direction\n");
117 snd_card_unref(compr->card);
121 data = kzalloc(sizeof(*data), GFP_KERNEL);
123 snd_card_unref(compr->card);
126 data->stream.ops = compr->ops;
127 data->stream.direction = dirn;
128 data->stream.private_data = compr->private_data;
129 data->stream.device = compr;
130 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
133 snd_card_unref(compr->card);
136 runtime->state = SNDRV_PCM_STATE_OPEN;
137 init_waitqueue_head(&runtime->sleep);
138 data->stream.runtime = runtime;
139 f->private_data = (void *)data;
140 mutex_lock(&compr->lock);
141 ret = compr->ops->open(&data->stream);
142 mutex_unlock(&compr->lock);
147 snd_card_unref(compr->card);
151 static int snd_compr_free(struct inode *inode, struct file *f)
153 struct snd_compr_file *data = f->private_data;
154 struct snd_compr_runtime *runtime = data->stream.runtime;
156 switch (runtime->state) {
157 case SNDRV_PCM_STATE_RUNNING:
158 case SNDRV_PCM_STATE_DRAINING:
159 case SNDRV_PCM_STATE_PAUSED:
160 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
166 data->stream.ops->free(&data->stream);
167 kfree(data->stream.runtime->buffer);
168 kfree(data->stream.runtime);
173 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
174 struct snd_compr_tstamp *tstamp)
176 if (!stream->ops->pointer)
178 stream->ops->pointer(stream, tstamp);
179 pr_debug("dsp consumed till %d total %d bytes\n",
180 tstamp->byte_offset, tstamp->copied_total);
181 if (stream->direction == SND_COMPRESS_PLAYBACK)
182 stream->runtime->total_bytes_transferred = tstamp->copied_total;
184 stream->runtime->total_bytes_available = tstamp->copied_total;
188 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
189 struct snd_compr_avail *avail)
191 memset(avail, 0, sizeof(*avail));
192 snd_compr_update_tstamp(stream, &avail->tstamp);
193 /* Still need to return avail even if tstamp can't be filled in */
195 if (stream->runtime->total_bytes_available == 0 &&
196 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
197 stream->direction == SND_COMPRESS_PLAYBACK) {
198 pr_debug("detected init and someone forgot to do a write\n");
199 return stream->runtime->buffer_size;
201 pr_debug("app wrote %lld, DSP consumed %lld\n",
202 stream->runtime->total_bytes_available,
203 stream->runtime->total_bytes_transferred);
204 if (stream->runtime->total_bytes_available ==
205 stream->runtime->total_bytes_transferred) {
206 if (stream->direction == SND_COMPRESS_PLAYBACK) {
207 pr_debug("both pointers are same, returning full avail\n");
208 return stream->runtime->buffer_size;
210 pr_debug("both pointers are same, returning no avail\n");
215 avail->avail = stream->runtime->total_bytes_available -
216 stream->runtime->total_bytes_transferred;
217 if (stream->direction == SND_COMPRESS_PLAYBACK)
218 avail->avail = stream->runtime->buffer_size - avail->avail;
220 pr_debug("ret avail as %lld\n", avail->avail);
224 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
226 struct snd_compr_avail avail;
228 return snd_compr_calc_avail(stream, &avail);
232 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
234 struct snd_compr_avail ioctl_avail;
237 avail = snd_compr_calc_avail(stream, &ioctl_avail);
238 ioctl_avail.avail = avail;
240 if (copy_to_user((__u64 __user *)arg,
241 &ioctl_avail, sizeof(ioctl_avail)))
246 static int snd_compr_write_data(struct snd_compr_stream *stream,
247 const char __user *buf, size_t count)
251 struct snd_compr_runtime *runtime = stream->runtime;
253 u64 app_pointer = div64_u64(runtime->total_bytes_available,
254 runtime->buffer_size);
255 app_pointer = runtime->total_bytes_available -
256 (app_pointer * runtime->buffer_size);
258 dstn = runtime->buffer + app_pointer;
259 pr_debug("copying %ld at %lld\n",
260 (unsigned long)count, app_pointer);
261 if (count < runtime->buffer_size - app_pointer) {
262 if (copy_from_user(dstn, buf, count))
265 copy = runtime->buffer_size - app_pointer;
266 if (copy_from_user(dstn, buf, copy))
268 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
271 /* if DSP cares, let it know data has been written */
272 if (stream->ops->ack)
273 stream->ops->ack(stream, count);
277 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
278 size_t count, loff_t *offset)
280 struct snd_compr_file *data = f->private_data;
281 struct snd_compr_stream *stream;
285 if (snd_BUG_ON(!data))
288 stream = &data->stream;
289 mutex_lock(&stream->device->lock);
290 /* write is allowed when stream is running or has been steup */
291 switch (stream->runtime->state) {
292 case SNDRV_PCM_STATE_SETUP:
293 case SNDRV_PCM_STATE_PREPARED:
294 case SNDRV_PCM_STATE_RUNNING:
297 mutex_unlock(&stream->device->lock);
301 avail = snd_compr_get_avail(stream);
302 pr_debug("avail returned %ld\n", (unsigned long)avail);
303 /* calculate how much we can write to buffer */
307 if (stream->ops->copy) {
308 char __user* cbuf = (char __user*)buf;
309 retval = stream->ops->copy(stream, cbuf, avail);
311 retval = snd_compr_write_data(stream, buf, avail);
314 stream->runtime->total_bytes_available += retval;
316 /* while initiating the stream, write should be called before START
317 * call, so in setup move state */
318 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
319 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
320 pr_debug("stream prepared, Houston we are good to go\n");
323 mutex_unlock(&stream->device->lock);
328 static ssize_t snd_compr_read(struct file *f, char __user *buf,
329 size_t count, loff_t *offset)
331 struct snd_compr_file *data = f->private_data;
332 struct snd_compr_stream *stream;
336 if (snd_BUG_ON(!data))
339 stream = &data->stream;
340 mutex_lock(&stream->device->lock);
342 /* read is allowed when stream is running, paused, draining and setup
343 * (yes setup is state which we transition to after stop, so if user
344 * wants to read data after stop we allow that)
346 switch (stream->runtime->state) {
347 case SNDRV_PCM_STATE_OPEN:
348 case SNDRV_PCM_STATE_PREPARED:
349 case SNDRV_PCM_STATE_XRUN:
350 case SNDRV_PCM_STATE_SUSPENDED:
351 case SNDRV_PCM_STATE_DISCONNECTED:
356 avail = snd_compr_get_avail(stream);
357 pr_debug("avail returned %ld\n", (unsigned long)avail);
358 /* calculate how much we can read from buffer */
362 if (stream->ops->copy) {
363 retval = stream->ops->copy(stream, buf, avail);
369 stream->runtime->total_bytes_transferred += retval;
372 mutex_unlock(&stream->device->lock);
376 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
381 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
383 if (stream->direction == SND_COMPRESS_PLAYBACK)
384 return POLLOUT | POLLWRNORM;
386 return POLLIN | POLLRDNORM;
389 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
391 struct snd_compr_file *data = f->private_data;
392 struct snd_compr_stream *stream;
396 if (snd_BUG_ON(!data))
399 stream = &data->stream;
401 mutex_lock(&stream->device->lock);
402 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
403 retval = snd_compr_get_poll(stream) | POLLERR;
406 poll_wait(f, &stream->runtime->sleep, wait);
408 avail = snd_compr_get_avail(stream);
409 pr_debug("avail is %ld\n", (unsigned long)avail);
410 /* check if we have at least one fragment to fill */
411 switch (stream->runtime->state) {
412 case SNDRV_PCM_STATE_DRAINING:
413 /* stream has been woken up after drain is complete
414 * draining done so set stream state to stopped
416 retval = snd_compr_get_poll(stream);
417 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
419 case SNDRV_PCM_STATE_RUNNING:
420 case SNDRV_PCM_STATE_PREPARED:
421 case SNDRV_PCM_STATE_PAUSED:
422 if (avail >= stream->runtime->fragment_size)
423 retval = snd_compr_get_poll(stream);
426 retval = snd_compr_get_poll(stream) | POLLERR;
430 mutex_unlock(&stream->device->lock);
435 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
438 struct snd_compr_caps caps;
440 if (!stream->ops->get_caps)
443 memset(&caps, 0, sizeof(caps));
444 retval = stream->ops->get_caps(stream, &caps);
447 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
453 #ifndef COMPR_CODEC_CAPS_OVERFLOW
455 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
458 struct snd_compr_codec_caps *caps;
460 if (!stream->ops->get_codec_caps)
463 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
467 retval = stream->ops->get_codec_caps(stream, caps);
470 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
477 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
479 /* revisit this with snd_pcm_preallocate_xxx */
480 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
481 struct snd_compr_params *params)
483 unsigned int buffer_size;
486 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
487 if (stream->ops->copy) {
489 /* if copy is defined the driver will be required to copy
493 buffer = kmalloc(buffer_size, GFP_KERNEL);
497 stream->runtime->fragment_size = params->buffer.fragment_size;
498 stream->runtime->fragments = params->buffer.fragments;
499 stream->runtime->buffer = buffer;
500 stream->runtime->buffer_size = buffer_size;
504 static int snd_compress_check_input(struct snd_compr_params *params)
506 /* first let's check the buffer parameter's */
507 if (params->buffer.fragment_size == 0 ||
508 params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
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 = {0};
641 ret = snd_compr_update_tstamp(stream, &tstamp);
643 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
644 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
648 static int snd_compr_pause(struct snd_compr_stream *stream)
652 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
654 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
656 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
660 static int snd_compr_resume(struct snd_compr_stream *stream)
664 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
666 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
668 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
672 static int snd_compr_start(struct snd_compr_stream *stream)
676 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
678 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
680 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
684 static int snd_compr_stop(struct snd_compr_stream *stream)
688 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
689 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
691 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
693 snd_compr_drain_notify(stream);
694 stream->runtime->total_bytes_available = 0;
695 stream->runtime->total_bytes_transferred = 0;
700 static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
705 * We are called with lock held. So drop the lock while we wait for
706 * drain complete notification from the driver
708 * It is expected that driver will notify the drain completion and then
709 * stream will be moved to SETUP state, even if draining resulted in an
710 * error. We can trigger next track after this.
712 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
713 mutex_unlock(&stream->device->lock);
715 /* we wait for drain to complete here, drain can return when
716 * interruption occurred, wait returned error or success.
717 * For the first two cases we don't do anything different here and
718 * return after waking up
721 ret = wait_event_interruptible(stream->runtime->sleep,
722 (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
723 if (ret == -ERESTARTSYS)
724 pr_debug("wait aborted by a signal");
726 pr_debug("wait for drain failed with %d\n", ret);
729 wake_up(&stream->runtime->sleep);
730 mutex_lock(&stream->device->lock);
735 static int snd_compr_drain(struct snd_compr_stream *stream)
739 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
740 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
743 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
745 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
746 wake_up(&stream->runtime->sleep);
750 return snd_compress_wait_for_drain(stream);
753 static int snd_compr_next_track(struct snd_compr_stream *stream)
757 /* only a running stream can transition to next track */
758 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
761 /* you can signal next track if this is intended to be a gapless stream
762 * and current track metadata is set
764 if (stream->metadata_set == false)
767 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
770 stream->metadata_set = false;
771 stream->next_track = true;
775 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
778 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
779 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
781 /* stream can be drained only when next track has been signalled */
782 if (stream->next_track == false)
785 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
787 pr_debug("Partial drain returned failure\n");
788 wake_up(&stream->runtime->sleep);
792 stream->next_track = false;
793 return snd_compress_wait_for_drain(stream);
796 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
798 struct snd_compr_file *data = f->private_data;
799 struct snd_compr_stream *stream;
800 int retval = -ENOTTY;
802 if (snd_BUG_ON(!data))
805 stream = &data->stream;
807 mutex_lock(&stream->device->lock);
808 switch (_IOC_NR(cmd)) {
809 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
810 retval = put_user(SNDRV_COMPRESS_VERSION,
811 (int __user *)arg) ? -EFAULT : 0;
813 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
814 retval = snd_compr_get_caps(stream, arg);
816 #ifndef COMPR_CODEC_CAPS_OVERFLOW
817 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
818 retval = snd_compr_get_codec_caps(stream, arg);
821 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
822 retval = snd_compr_set_params(stream, arg);
824 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
825 retval = snd_compr_get_params(stream, arg);
827 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
828 retval = snd_compr_set_metadata(stream, arg);
830 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
831 retval = snd_compr_get_metadata(stream, arg);
833 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
834 retval = snd_compr_tstamp(stream, arg);
836 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
837 retval = snd_compr_ioctl_avail(stream, arg);
839 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
840 retval = snd_compr_pause(stream);
842 case _IOC_NR(SNDRV_COMPRESS_RESUME):
843 retval = snd_compr_resume(stream);
845 case _IOC_NR(SNDRV_COMPRESS_START):
846 retval = snd_compr_start(stream);
848 case _IOC_NR(SNDRV_COMPRESS_STOP):
849 retval = snd_compr_stop(stream);
851 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
852 retval = snd_compr_drain(stream);
854 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
855 retval = snd_compr_partial_drain(stream);
857 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
858 retval = snd_compr_next_track(stream);
862 mutex_unlock(&stream->device->lock);
866 /* support of 32bit userspace on 64bit platforms */
868 static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
871 return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
875 static const struct file_operations snd_compr_file_ops = {
876 .owner = THIS_MODULE,
877 .open = snd_compr_open,
878 .release = snd_compr_free,
879 .write = snd_compr_write,
880 .read = snd_compr_read,
881 .unlocked_ioctl = snd_compr_ioctl,
883 .compat_ioctl = snd_compr_ioctl_compat,
885 .mmap = snd_compr_mmap,
886 .poll = snd_compr_poll,
889 static int snd_compress_dev_register(struct snd_device *device)
893 struct snd_compr *compr;
895 if (snd_BUG_ON(!device || !device->device_data))
897 compr = device->device_data;
899 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
901 /* register compressed device */
902 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
903 compr->card, compr->device,
904 &snd_compr_file_ops, compr, &compr->dev);
906 pr_err("snd_register_device failed\n %d", ret);
913 static int snd_compress_dev_disconnect(struct snd_device *device)
915 struct snd_compr *compr;
917 compr = device->device_data;
918 snd_unregister_device(&compr->dev);
922 #ifdef CONFIG_SND_VERBOSE_PROCFS
923 static void snd_compress_proc_info_read(struct snd_info_entry *entry,
924 struct snd_info_buffer *buffer)
926 struct snd_compr *compr = (struct snd_compr *)entry->private_data;
928 snd_iprintf(buffer, "card: %d\n", compr->card->number);
929 snd_iprintf(buffer, "device: %d\n", compr->device);
930 snd_iprintf(buffer, "stream: %s\n",
931 compr->direction == SND_COMPRESS_PLAYBACK
932 ? "PLAYBACK" : "CAPTURE");
933 snd_iprintf(buffer, "id: %s\n", compr->id);
936 static int snd_compress_proc_init(struct snd_compr *compr)
938 struct snd_info_entry *entry;
941 sprintf(name, "compr%i", compr->device);
942 entry = snd_info_create_card_entry(compr->card, name,
943 compr->card->proc_root);
946 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
947 if (snd_info_register(entry) < 0) {
948 snd_info_free_entry(entry);
951 compr->proc_root = entry;
953 entry = snd_info_create_card_entry(compr->card, "info",
956 snd_info_set_text_ops(entry, compr,
957 snd_compress_proc_info_read);
958 if (snd_info_register(entry) < 0) {
959 snd_info_free_entry(entry);
963 compr->proc_info_entry = entry;
968 static void snd_compress_proc_done(struct snd_compr *compr)
970 snd_info_free_entry(compr->proc_info_entry);
971 compr->proc_info_entry = NULL;
972 snd_info_free_entry(compr->proc_root);
973 compr->proc_root = NULL;
976 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
978 strlcpy(compr->id, id, sizeof(compr->id));
981 static inline int snd_compress_proc_init(struct snd_compr *compr)
986 static inline void snd_compress_proc_done(struct snd_compr *compr)
990 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
995 static int snd_compress_dev_free(struct snd_device *device)
997 struct snd_compr *compr;
999 compr = device->device_data;
1000 snd_compress_proc_done(compr);
1001 put_device(&compr->dev);
1006 * snd_compress_new: create new compress device
1007 * @card: sound card pointer
1008 * @device: device number
1009 * @dirn: device direction, should be of type enum snd_compr_direction
1010 * @compr: compress device pointer
1012 int snd_compress_new(struct snd_card *card, int device,
1013 int dirn, const char *id, struct snd_compr *compr)
1015 static struct snd_device_ops ops = {
1016 .dev_free = snd_compress_dev_free,
1017 .dev_register = snd_compress_dev_register,
1018 .dev_disconnect = snd_compress_dev_disconnect,
1023 compr->device = device;
1024 compr->direction = dirn;
1026 snd_compress_set_id(compr, id);
1028 snd_device_initialize(&compr->dev, card);
1029 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
1031 ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
1033 snd_compress_proc_init(compr);
1037 EXPORT_SYMBOL_GPL(snd_compress_new);
1039 static int snd_compress_add_device(struct snd_compr *device)
1046 /* register the card */
1047 ret = snd_card_register(device->card);
1053 pr_err("failed with %d\n", ret);
1058 static int snd_compress_remove_device(struct snd_compr *device)
1060 return snd_card_free(device->card);
1064 * snd_compress_register - register compressed device
1066 * @device: compressed device to register
1068 int snd_compress_register(struct snd_compr *device)
1072 if (device->name == NULL || device->ops == NULL)
1075 pr_debug("Registering compressed device %s\n", device->name);
1076 if (snd_BUG_ON(!device->ops->open))
1078 if (snd_BUG_ON(!device->ops->free))
1080 if (snd_BUG_ON(!device->ops->set_params))
1082 if (snd_BUG_ON(!device->ops->trigger))
1085 mutex_init(&device->lock);
1087 /* register a compressed card */
1088 mutex_lock(&device_mutex);
1089 retval = snd_compress_add_device(device);
1090 mutex_unlock(&device_mutex);
1093 EXPORT_SYMBOL_GPL(snd_compress_register);
1095 int snd_compress_deregister(struct snd_compr *device)
1097 pr_debug("Removing compressed device %s\n", device->name);
1098 mutex_lock(&device_mutex);
1099 snd_compress_remove_device(device);
1100 mutex_unlock(&device_mutex);
1103 EXPORT_SYMBOL_GPL(snd_compress_deregister);
1105 static int __init snd_compress_init(void)
1110 static void __exit snd_compress_exit(void)
1114 module_init(snd_compress_init);
1115 module_exit(snd_compress_exit);
1117 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1118 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1119 MODULE_LICENSE("GPL v2");