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 struct snd_compr_runtime *runtime = data->stream.runtime;
151 switch (runtime->state) {
152 case SNDRV_PCM_STATE_RUNNING:
153 case SNDRV_PCM_STATE_DRAINING:
154 case SNDRV_PCM_STATE_PAUSED:
155 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
161 data->stream.ops->free(&data->stream);
162 kfree(data->stream.runtime->buffer);
163 kfree(data->stream.runtime);
168 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
169 struct snd_compr_tstamp *tstamp)
171 if (!stream->ops->pointer)
173 stream->ops->pointer(stream, tstamp);
174 pr_debug("dsp consumed till %d total %d bytes\n",
175 tstamp->byte_offset, tstamp->copied_total);
176 if (stream->direction == SND_COMPRESS_PLAYBACK)
177 stream->runtime->total_bytes_transferred = tstamp->copied_total;
179 stream->runtime->total_bytes_available = tstamp->copied_total;
183 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
184 struct snd_compr_avail *avail)
186 memset(avail, 0, sizeof(*avail));
187 snd_compr_update_tstamp(stream, &avail->tstamp);
188 /* Still need to return avail even if tstamp can't be filled in */
190 if (stream->runtime->total_bytes_available == 0 &&
191 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
192 stream->direction == SND_COMPRESS_PLAYBACK) {
193 pr_debug("detected init and someone forgot to do a write\n");
194 return stream->runtime->buffer_size;
196 pr_debug("app wrote %lld, DSP consumed %lld\n",
197 stream->runtime->total_bytes_available,
198 stream->runtime->total_bytes_transferred);
199 if (stream->runtime->total_bytes_available ==
200 stream->runtime->total_bytes_transferred) {
201 if (stream->direction == SND_COMPRESS_PLAYBACK) {
202 pr_debug("both pointers are same, returning full avail\n");
203 return stream->runtime->buffer_size;
205 pr_debug("both pointers are same, returning no avail\n");
210 avail->avail = stream->runtime->total_bytes_available -
211 stream->runtime->total_bytes_transferred;
212 if (stream->direction == SND_COMPRESS_PLAYBACK)
213 avail->avail = stream->runtime->buffer_size - avail->avail;
215 pr_debug("ret avail as %lld\n", avail->avail);
219 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
221 struct snd_compr_avail avail;
223 return snd_compr_calc_avail(stream, &avail);
227 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
229 struct snd_compr_avail ioctl_avail;
232 avail = snd_compr_calc_avail(stream, &ioctl_avail);
233 ioctl_avail.avail = avail;
235 if (copy_to_user((__u64 __user *)arg,
236 &ioctl_avail, sizeof(ioctl_avail)))
241 static int snd_compr_write_data(struct snd_compr_stream *stream,
242 const char __user *buf, size_t count)
246 struct snd_compr_runtime *runtime = stream->runtime;
248 u64 app_pointer = div64_u64(runtime->total_bytes_available,
249 runtime->buffer_size);
250 app_pointer = runtime->total_bytes_available -
251 (app_pointer * runtime->buffer_size);
253 dstn = runtime->buffer + app_pointer;
254 pr_debug("copying %ld at %lld\n",
255 (unsigned long)count, app_pointer);
256 if (count < runtime->buffer_size - app_pointer) {
257 if (copy_from_user(dstn, buf, count))
260 copy = runtime->buffer_size - app_pointer;
261 if (copy_from_user(dstn, buf, copy))
263 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
266 /* if DSP cares, let it know data has been written */
267 if (stream->ops->ack)
268 stream->ops->ack(stream, count);
272 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
273 size_t count, loff_t *offset)
275 struct snd_compr_file *data = f->private_data;
276 struct snd_compr_stream *stream;
280 if (snd_BUG_ON(!data))
283 stream = &data->stream;
284 mutex_lock(&stream->device->lock);
285 /* write is allowed when stream is running or has been steup */
286 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
287 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
288 mutex_unlock(&stream->device->lock);
292 avail = snd_compr_get_avail(stream);
293 pr_debug("avail returned %ld\n", (unsigned long)avail);
294 /* calculate how much we can write to buffer */
298 if (stream->ops->copy) {
299 char __user* cbuf = (char __user*)buf;
300 retval = stream->ops->copy(stream, cbuf, avail);
302 retval = snd_compr_write_data(stream, buf, avail);
305 stream->runtime->total_bytes_available += retval;
307 /* while initiating the stream, write should be called before START
308 * call, so in setup move state */
309 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
310 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
311 pr_debug("stream prepared, Houston we are good to go\n");
314 mutex_unlock(&stream->device->lock);
319 static ssize_t snd_compr_read(struct file *f, char __user *buf,
320 size_t count, loff_t *offset)
322 struct snd_compr_file *data = f->private_data;
323 struct snd_compr_stream *stream;
327 if (snd_BUG_ON(!data))
330 stream = &data->stream;
331 mutex_lock(&stream->device->lock);
333 /* read is allowed when stream is running, paused, draining and setup
334 * (yes setup is state which we transition to after stop, so if user
335 * wants to read data after stop we allow that)
337 switch (stream->runtime->state) {
338 case SNDRV_PCM_STATE_OPEN:
339 case SNDRV_PCM_STATE_PREPARED:
340 case SNDRV_PCM_STATE_XRUN:
341 case SNDRV_PCM_STATE_SUSPENDED:
342 case SNDRV_PCM_STATE_DISCONNECTED:
347 avail = snd_compr_get_avail(stream);
348 pr_debug("avail returned %ld\n", (unsigned long)avail);
349 /* calculate how much we can read from buffer */
353 if (stream->ops->copy) {
354 retval = stream->ops->copy(stream, buf, avail);
360 stream->runtime->total_bytes_transferred += retval;
363 mutex_unlock(&stream->device->lock);
367 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
372 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
374 if (stream->direction == SND_COMPRESS_PLAYBACK)
375 return POLLOUT | POLLWRNORM;
377 return POLLIN | POLLRDNORM;
380 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
382 struct snd_compr_file *data = f->private_data;
383 struct snd_compr_stream *stream;
387 if (snd_BUG_ON(!data))
389 stream = &data->stream;
390 if (snd_BUG_ON(!stream))
393 mutex_lock(&stream->device->lock);
394 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
398 poll_wait(f, &stream->runtime->sleep, wait);
400 avail = snd_compr_get_avail(stream);
401 pr_debug("avail is %ld\n", (unsigned long)avail);
402 /* check if we have at least one fragment to fill */
403 switch (stream->runtime->state) {
404 case SNDRV_PCM_STATE_DRAINING:
405 /* stream has been woken up after drain is complete
406 * draining done so set stream state to stopped
408 retval = snd_compr_get_poll(stream);
409 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
411 case SNDRV_PCM_STATE_RUNNING:
412 case SNDRV_PCM_STATE_PREPARED:
413 case SNDRV_PCM_STATE_PAUSED:
414 if (avail >= stream->runtime->fragment_size)
415 retval = snd_compr_get_poll(stream);
418 if (stream->direction == SND_COMPRESS_PLAYBACK)
419 retval = POLLOUT | POLLWRNORM | POLLERR;
421 retval = POLLIN | POLLRDNORM | POLLERR;
425 mutex_unlock(&stream->device->lock);
430 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
433 struct snd_compr_caps caps;
435 if (!stream->ops->get_caps)
438 memset(&caps, 0, sizeof(caps));
439 retval = stream->ops->get_caps(stream, &caps);
442 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
448 #ifndef COMPR_CODEC_CAPS_OVERFLOW
450 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
453 struct snd_compr_codec_caps *caps;
455 if (!stream->ops->get_codec_caps)
458 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
462 retval = stream->ops->get_codec_caps(stream, caps);
465 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
472 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
474 /* revisit this with snd_pcm_preallocate_xxx */
475 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
476 struct snd_compr_params *params)
478 unsigned int buffer_size;
481 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
482 if (stream->ops->copy) {
484 /* if copy is defined the driver will be required to copy
488 buffer = kmalloc(buffer_size, GFP_KERNEL);
492 stream->runtime->fragment_size = params->buffer.fragment_size;
493 stream->runtime->fragments = params->buffer.fragments;
494 stream->runtime->buffer = buffer;
495 stream->runtime->buffer_size = buffer_size;
499 static int snd_compress_check_input(struct snd_compr_params *params)
501 /* first let's check the buffer parameter's */
502 if (params->buffer.fragment_size == 0 ||
503 params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
506 /* now codec parameters */
507 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
510 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
517 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
519 struct snd_compr_params *params;
522 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
524 * we should allow parameter change only when stream has been
525 * opened not in other cases
527 params = kmalloc(sizeof(*params), GFP_KERNEL);
530 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
535 retval = snd_compress_check_input(params);
539 retval = snd_compr_allocate_buffer(stream, params);
545 retval = stream->ops->set_params(stream, params);
549 stream->metadata_set = false;
550 stream->next_track = false;
552 if (stream->direction == SND_COMPRESS_PLAYBACK)
553 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
555 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
565 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
567 struct snd_codec *params;
570 if (!stream->ops->get_params)
573 params = kzalloc(sizeof(*params), GFP_KERNEL);
576 retval = stream->ops->get_params(stream, params);
579 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
588 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
590 struct snd_compr_metadata metadata;
593 if (!stream->ops->get_metadata)
596 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
599 retval = stream->ops->get_metadata(stream, &metadata);
603 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
610 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
612 struct snd_compr_metadata metadata;
615 if (!stream->ops->set_metadata)
618 * we should allow parameter change only when stream has been
619 * opened not in other cases
621 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
624 retval = stream->ops->set_metadata(stream, &metadata);
625 stream->metadata_set = true;
631 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
633 struct snd_compr_tstamp tstamp = {0};
636 ret = snd_compr_update_tstamp(stream, &tstamp);
638 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
639 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
643 static int snd_compr_pause(struct snd_compr_stream *stream)
647 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
649 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
651 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
655 static int snd_compr_resume(struct snd_compr_stream *stream)
659 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
661 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
663 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
667 static int snd_compr_start(struct snd_compr_stream *stream)
671 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
673 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
675 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
679 static int snd_compr_stop(struct snd_compr_stream *stream)
683 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
684 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
686 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
688 snd_compr_drain_notify(stream);
689 stream->runtime->total_bytes_available = 0;
690 stream->runtime->total_bytes_transferred = 0;
695 static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
700 * We are called with lock held. So drop the lock while we wait for
701 * drain complete notfication from the driver
703 * It is expected that driver will notify the drain completion and then
704 * stream will be moved to SETUP state, even if draining resulted in an
705 * error. We can trigger next track after this.
707 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
708 mutex_unlock(&stream->device->lock);
710 /* we wait for drain to complete here, drain can return when
711 * interruption occurred, wait returned error or success.
712 * For the first two cases we don't do anything different here and
713 * return after waking up
716 ret = wait_event_interruptible(stream->runtime->sleep,
717 (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
718 if (ret == -ERESTARTSYS)
719 pr_debug("wait aborted by a signal");
721 pr_debug("wait for drain failed with %d\n", ret);
724 wake_up(&stream->runtime->sleep);
725 mutex_lock(&stream->device->lock);
730 static int snd_compr_drain(struct snd_compr_stream *stream)
734 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
735 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
738 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
740 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
741 wake_up(&stream->runtime->sleep);
745 return snd_compress_wait_for_drain(stream);
748 static int snd_compr_next_track(struct snd_compr_stream *stream)
752 /* only a running stream can transition to next track */
753 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
756 /* you can signal next track isf this is intended to be a gapless stream
757 * and current track metadata is set
759 if (stream->metadata_set == false)
762 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
765 stream->metadata_set = false;
766 stream->next_track = true;
770 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
773 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
774 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
776 /* stream can be drained only when next track has been signalled */
777 if (stream->next_track == false)
780 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
782 pr_debug("Partial drain returned failure\n");
783 wake_up(&stream->runtime->sleep);
787 stream->next_track = false;
788 return snd_compress_wait_for_drain(stream);
791 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
793 struct snd_compr_file *data = f->private_data;
794 struct snd_compr_stream *stream;
795 int retval = -ENOTTY;
797 if (snd_BUG_ON(!data))
799 stream = &data->stream;
800 if (snd_BUG_ON(!stream))
802 mutex_lock(&stream->device->lock);
803 switch (_IOC_NR(cmd)) {
804 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
805 retval = put_user(SNDRV_COMPRESS_VERSION,
806 (int __user *)arg) ? -EFAULT : 0;
808 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
809 retval = snd_compr_get_caps(stream, arg);
811 #ifndef COMPR_CODEC_CAPS_OVERFLOW
812 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
813 retval = snd_compr_get_codec_caps(stream, arg);
816 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
817 retval = snd_compr_set_params(stream, arg);
819 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
820 retval = snd_compr_get_params(stream, arg);
822 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
823 retval = snd_compr_set_metadata(stream, arg);
825 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
826 retval = snd_compr_get_metadata(stream, arg);
828 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
829 retval = snd_compr_tstamp(stream, arg);
831 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
832 retval = snd_compr_ioctl_avail(stream, arg);
834 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
835 retval = snd_compr_pause(stream);
837 case _IOC_NR(SNDRV_COMPRESS_RESUME):
838 retval = snd_compr_resume(stream);
840 case _IOC_NR(SNDRV_COMPRESS_START):
841 retval = snd_compr_start(stream);
843 case _IOC_NR(SNDRV_COMPRESS_STOP):
844 retval = snd_compr_stop(stream);
846 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
847 retval = snd_compr_drain(stream);
849 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
850 retval = snd_compr_partial_drain(stream);
852 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
853 retval = snd_compr_next_track(stream);
857 mutex_unlock(&stream->device->lock);
861 static const struct file_operations snd_compr_file_ops = {
862 .owner = THIS_MODULE,
863 .open = snd_compr_open,
864 .release = snd_compr_free,
865 .write = snd_compr_write,
866 .read = snd_compr_read,
867 .unlocked_ioctl = snd_compr_ioctl,
868 .mmap = snd_compr_mmap,
869 .poll = snd_compr_poll,
872 static int snd_compress_dev_register(struct snd_device *device)
876 struct snd_compr *compr;
878 if (snd_BUG_ON(!device || !device->device_data))
880 compr = device->device_data;
882 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
884 /* register compressed device */
885 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
886 compr->card, compr->device,
887 &snd_compr_file_ops, compr, &compr->dev);
889 pr_err("snd_register_device failed\n %d", ret);
896 static int snd_compress_dev_disconnect(struct snd_device *device)
898 struct snd_compr *compr;
900 compr = device->device_data;
901 snd_unregister_device(&compr->dev);
905 static int snd_compress_dev_free(struct snd_device *device)
907 struct snd_compr *compr;
909 compr = device->device_data;
910 put_device(&compr->dev);
915 * snd_compress_new: create new compress device
916 * @card: sound card pointer
917 * @device: device number
918 * @dirn: device direction, should be of type enum snd_compr_direction
919 * @compr: compress device pointer
921 int snd_compress_new(struct snd_card *card, int device,
922 int dirn, struct snd_compr *compr)
924 static struct snd_device_ops ops = {
925 .dev_free = snd_compress_dev_free,
926 .dev_register = snd_compress_dev_register,
927 .dev_disconnect = snd_compress_dev_disconnect,
931 compr->device = device;
932 compr->direction = dirn;
934 snd_device_initialize(&compr->dev, card);
935 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
937 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
939 EXPORT_SYMBOL_GPL(snd_compress_new);
941 static int snd_compress_add_device(struct snd_compr *device)
948 /* register the card */
949 ret = snd_card_register(device->card);
955 pr_err("failed with %d\n", ret);
960 static int snd_compress_remove_device(struct snd_compr *device)
962 return snd_card_free(device->card);
966 * snd_compress_register - register compressed device
968 * @device: compressed device to register
970 int snd_compress_register(struct snd_compr *device)
974 if (device->name == NULL || device->ops == NULL)
977 pr_debug("Registering compressed device %s\n", device->name);
978 if (snd_BUG_ON(!device->ops->open))
980 if (snd_BUG_ON(!device->ops->free))
982 if (snd_BUG_ON(!device->ops->set_params))
984 if (snd_BUG_ON(!device->ops->trigger))
987 mutex_init(&device->lock);
989 /* register a compressed card */
990 mutex_lock(&device_mutex);
991 retval = snd_compress_add_device(device);
992 mutex_unlock(&device_mutex);
995 EXPORT_SYMBOL_GPL(snd_compress_register);
997 int snd_compress_deregister(struct snd_compr *device)
999 pr_debug("Removing compressed device %s\n", device->name);
1000 mutex_lock(&device_mutex);
1001 snd_compress_remove_device(device);
1002 mutex_unlock(&device_mutex);
1005 EXPORT_SYMBOL_GPL(snd_compress_deregister);
1007 static int __init snd_compress_init(void)
1012 static void __exit snd_compress_exit(void)
1016 module_init(snd_compress_init);
1017 module_exit(snd_compress_exit);
1019 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1020 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1021 MODULE_LICENSE("GPL v2");