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>
32 #include <linux/mutex.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/sched.h>
36 #include <linux/uio.h>
37 #include <linux/uaccess.h>
38 #include <linux/module.h>
39 #include <sound/core.h>
40 #include <sound/initval.h>
41 #include <sound/compress_params.h>
42 #include <sound/compress_offload.h>
43 #include <sound/compress_driver.h>
46 * - add substream support for multiple devices in case of
47 * SND_DYNAMIC_MINORS is not used
48 * - Multiple node representation
49 * driver should be able to register multiple nodes
52 static DEFINE_MUTEX(device_mutex);
54 struct snd_compr_file {
56 struct snd_compr_stream stream;
60 * a note on stream states used:
61 * we use follwing states in the compressed core
62 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
63 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
64 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
65 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
66 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
67 * decoding/encoding and rendering/capturing data.
68 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
69 * by calling SNDRV_COMPRESS_DRAIN.
70 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
71 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
72 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
74 static int snd_compr_open(struct inode *inode, struct file *f)
76 struct snd_compr *compr;
77 struct snd_compr_file *data;
78 struct snd_compr_runtime *runtime;
79 enum snd_compr_direction dirn;
80 int maj = imajor(inode);
83 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
84 dirn = SND_COMPRESS_PLAYBACK;
85 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
86 dirn = SND_COMPRESS_CAPTURE;
91 compr = snd_lookup_minor_data(iminor(inode),
92 SNDRV_DEVICE_TYPE_COMPRESS);
97 pr_err("no device data!!!\n");
101 if (dirn != compr->direction) {
102 pr_err("this device doesn't support this direction\n");
106 data = kzalloc(sizeof(*data), GFP_KERNEL);
109 data->stream.ops = compr->ops;
110 data->stream.direction = dirn;
111 data->stream.private_data = compr->private_data;
112 data->stream.device = compr;
113 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
118 runtime->state = SNDRV_PCM_STATE_OPEN;
119 init_waitqueue_head(&runtime->sleep);
120 data->stream.runtime = runtime;
121 f->private_data = (void *)data;
122 mutex_lock(&compr->lock);
123 ret = compr->ops->open(&data->stream);
124 mutex_unlock(&compr->lock);
132 static int snd_compr_free(struct inode *inode, struct file *f)
134 struct snd_compr_file *data = f->private_data;
135 data->stream.ops->free(&data->stream);
136 kfree(data->stream.runtime->buffer);
137 kfree(data->stream.runtime);
142 static void snd_compr_update_tstamp(struct snd_compr_stream *stream,
143 struct snd_compr_tstamp *tstamp)
145 if (!stream->ops->pointer)
147 stream->ops->pointer(stream, tstamp);
148 pr_debug("dsp consumed till %d total %d bytes\n",
149 tstamp->byte_offset, tstamp->copied_total);
150 stream->runtime->hw_pointer = tstamp->byte_offset;
151 stream->runtime->total_bytes_transferred = tstamp->copied_total;
154 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
155 struct snd_compr_avail *avail)
157 long avail_calc; /*this needs to be signed variable */
159 snd_compr_update_tstamp(stream, &avail->tstamp);
161 /* FIXME: This needs to be different for capture stream,
162 available is # of compressed data, for playback it's
163 remainder of buffer */
165 if (stream->runtime->total_bytes_available == 0 &&
166 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
167 pr_debug("detected init and someone forgot to do a write\n");
168 return stream->runtime->buffer_size;
170 pr_debug("app wrote %lld, DSP consumed %lld\n",
171 stream->runtime->total_bytes_available,
172 stream->runtime->total_bytes_transferred);
173 if (stream->runtime->total_bytes_available ==
174 stream->runtime->total_bytes_transferred) {
175 pr_debug("both pointers are same, returning full avail\n");
176 return stream->runtime->buffer_size;
179 /* FIXME: this routine isn't consistent, in one test we use
180 * cumulative values and in the other byte offsets. Do we
181 * really need the byte offsets if the cumulative values have
182 * been updated? In the PCM interface app_ptr and hw_ptr are
183 * already cumulative */
185 avail_calc = stream->runtime->buffer_size -
186 (stream->runtime->app_pointer - stream->runtime->hw_pointer);
187 pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc,
188 stream->runtime->app_pointer,
189 stream->runtime->hw_pointer);
190 if (avail_calc >= stream->runtime->buffer_size)
191 avail_calc -= stream->runtime->buffer_size;
192 pr_debug("ret avail as %ld\n", avail_calc);
193 avail->avail = avail_calc;
197 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
199 struct snd_compr_avail avail;
201 return snd_compr_calc_avail(stream, &avail);
205 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
207 struct snd_compr_avail ioctl_avail;
210 avail = snd_compr_calc_avail(stream, &ioctl_avail);
211 ioctl_avail.avail = avail;
213 if (copy_to_user((__u64 __user *)arg,
214 &ioctl_avail, sizeof(ioctl_avail)))
219 static int snd_compr_write_data(struct snd_compr_stream *stream,
220 const char __user *buf, size_t count)
224 struct snd_compr_runtime *runtime = stream->runtime;
226 dstn = runtime->buffer + runtime->app_pointer;
227 pr_debug("copying %ld at %lld\n",
228 (unsigned long)count, runtime->app_pointer);
229 if (count < runtime->buffer_size - runtime->app_pointer) {
230 if (copy_from_user(dstn, buf, count))
232 runtime->app_pointer += count;
234 copy = runtime->buffer_size - runtime->app_pointer;
235 if (copy_from_user(dstn, buf, copy))
237 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
239 runtime->app_pointer = count - copy;
241 /* if DSP cares, let it know data has been written */
242 if (stream->ops->ack)
243 stream->ops->ack(stream, count);
247 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
248 size_t count, loff_t *offset)
250 struct snd_compr_file *data = f->private_data;
251 struct snd_compr_stream *stream;
255 if (snd_BUG_ON(!data))
258 stream = &data->stream;
259 mutex_lock(&stream->device->lock);
260 /* write is allowed when stream is running or has been steup */
261 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
262 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
263 mutex_unlock(&stream->device->lock);
267 avail = snd_compr_get_avail(stream);
268 pr_debug("avail returned %ld\n", (unsigned long)avail);
269 /* calculate how much we can write to buffer */
273 if (stream->ops->copy)
274 retval = stream->ops->copy(stream, buf, avail);
276 retval = snd_compr_write_data(stream, buf, avail);
278 stream->runtime->total_bytes_available += retval;
280 /* while initiating the stream, write should be called before START
281 * call, so in setup move state */
282 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
283 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
284 pr_debug("stream prepared, Houston we are good to go\n");
287 mutex_unlock(&stream->device->lock);
292 static ssize_t snd_compr_read(struct file *f, char __user *buf,
293 size_t count, loff_t *offset)
298 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
303 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
305 if (stream->direction == SND_COMPRESS_PLAYBACK)
306 return POLLOUT | POLLWRNORM;
308 return POLLIN | POLLRDNORM;
311 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
313 struct snd_compr_file *data = f->private_data;
314 struct snd_compr_stream *stream;
318 if (snd_BUG_ON(!data))
320 stream = &data->stream;
321 if (snd_BUG_ON(!stream))
324 mutex_lock(&stream->device->lock);
325 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
326 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
330 poll_wait(f, &stream->runtime->sleep, wait);
332 avail = snd_compr_get_avail(stream);
333 pr_debug("avail is %ld\n", (unsigned long)avail);
334 /* check if we have at least one fragment to fill */
335 switch (stream->runtime->state) {
336 case SNDRV_PCM_STATE_DRAINING:
337 /* stream has been woken up after drain is complete
338 * draining done so set stream state to stopped
340 retval = snd_compr_get_poll(stream);
341 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
343 case SNDRV_PCM_STATE_RUNNING:
344 case SNDRV_PCM_STATE_PREPARED:
345 case SNDRV_PCM_STATE_PAUSED:
346 if (avail >= stream->runtime->fragment_size)
347 retval = snd_compr_get_poll(stream);
350 if (stream->direction == SND_COMPRESS_PLAYBACK)
351 retval = POLLOUT | POLLWRNORM | POLLERR;
353 retval = POLLIN | POLLRDNORM | POLLERR;
357 mutex_unlock(&stream->device->lock);
362 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
365 struct snd_compr_caps caps;
367 if (!stream->ops->get_caps)
370 retval = stream->ops->get_caps(stream, &caps);
373 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
380 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
383 struct snd_compr_codec_caps *caps;
385 if (!stream->ops->get_codec_caps)
388 caps = kmalloc(sizeof(*caps), GFP_KERNEL);
392 retval = stream->ops->get_codec_caps(stream, caps);
395 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
403 /* revisit this with snd_pcm_preallocate_xxx */
404 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
405 struct snd_compr_params *params)
407 unsigned int buffer_size;
410 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
411 if (stream->ops->copy) {
413 /* if copy is defined the driver will be required to copy
417 buffer = kmalloc(buffer_size, GFP_KERNEL);
421 stream->runtime->fragment_size = params->buffer.fragment_size;
422 stream->runtime->fragments = params->buffer.fragments;
423 stream->runtime->buffer = buffer;
424 stream->runtime->buffer_size = buffer_size;
428 static int snd_compress_check_input(struct snd_compr_params *params)
430 /* first let's check the buffer parameter's */
431 if (params->buffer.fragment_size == 0 ||
432 params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
439 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
441 struct snd_compr_params *params;
444 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
446 * we should allow parameter change only when stream has been
447 * opened not in other cases
449 params = kmalloc(sizeof(*params), GFP_KERNEL);
452 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
457 retval = snd_compress_check_input(params);
461 retval = snd_compr_allocate_buffer(stream, params);
467 retval = stream->ops->set_params(stream, params);
470 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
480 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
482 struct snd_codec *params;
485 if (!stream->ops->get_params)
488 params = kmalloc(sizeof(*params), GFP_KERNEL);
491 retval = stream->ops->get_params(stream, params);
494 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
503 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
505 struct snd_compr_tstamp tstamp;
507 snd_compr_update_tstamp(stream, &tstamp);
508 return copy_to_user((struct snd_compr_tstamp __user *)arg,
509 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
512 static int snd_compr_pause(struct snd_compr_stream *stream)
516 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
518 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
520 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
524 static int snd_compr_resume(struct snd_compr_stream *stream)
528 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
530 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
532 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
536 static int snd_compr_start(struct snd_compr_stream *stream)
540 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
542 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
544 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
548 static int snd_compr_stop(struct snd_compr_stream *stream)
552 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
553 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
555 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
557 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
558 wake_up(&stream->runtime->sleep);
559 stream->runtime->hw_pointer = 0;
560 stream->runtime->app_pointer = 0;
561 stream->runtime->total_bytes_available = 0;
562 stream->runtime->total_bytes_transferred = 0;
567 static int snd_compr_drain(struct snd_compr_stream *stream)
571 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
572 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
574 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
576 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
577 wake_up(&stream->runtime->sleep);
582 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
584 struct snd_compr_file *data = f->private_data;
585 struct snd_compr_stream *stream;
586 int retval = -ENOTTY;
588 if (snd_BUG_ON(!data))
590 stream = &data->stream;
591 if (snd_BUG_ON(!stream))
593 mutex_lock(&stream->device->lock);
594 switch (_IOC_NR(cmd)) {
595 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
596 put_user(SNDRV_COMPRESS_VERSION,
597 (int __user *)arg) ? -EFAULT : 0;
599 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
600 retval = snd_compr_get_caps(stream, arg);
602 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
603 retval = snd_compr_get_codec_caps(stream, arg);
605 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
606 retval = snd_compr_set_params(stream, arg);
608 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
609 retval = snd_compr_get_params(stream, arg);
611 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
612 retval = snd_compr_tstamp(stream, arg);
614 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
615 retval = snd_compr_ioctl_avail(stream, arg);
617 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
618 retval = snd_compr_pause(stream);
620 case _IOC_NR(SNDRV_COMPRESS_RESUME):
621 retval = snd_compr_resume(stream);
623 case _IOC_NR(SNDRV_COMPRESS_START):
624 retval = snd_compr_start(stream);
626 case _IOC_NR(SNDRV_COMPRESS_STOP):
627 retval = snd_compr_stop(stream);
629 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
630 retval = snd_compr_drain(stream);
633 mutex_unlock(&stream->device->lock);
637 static const struct file_operations snd_compr_file_ops = {
638 .owner = THIS_MODULE,
639 .open = snd_compr_open,
640 .release = snd_compr_free,
641 .write = snd_compr_write,
642 .read = snd_compr_read,
643 .unlocked_ioctl = snd_compr_ioctl,
644 .mmap = snd_compr_mmap,
645 .poll = snd_compr_poll,
648 static int snd_compress_dev_register(struct snd_device *device)
652 struct snd_compr *compr;
654 if (snd_BUG_ON(!device || !device->device_data))
656 compr = device->device_data;
658 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
659 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
661 /* register compressed device */
662 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
663 compr->device, &snd_compr_file_ops, compr, str);
665 pr_err("snd_register_device failed\n %d", ret);
672 static int snd_compress_dev_disconnect(struct snd_device *device)
674 struct snd_compr *compr;
676 compr = device->device_data;
677 snd_unregister_device(compr->direction, compr->card, compr->device);
682 * snd_compress_new: create new compress device
683 * @card: sound card pointer
684 * @device: device number
685 * @dirn: device direction, should be of type enum snd_compr_direction
686 * @compr: compress device pointer
688 int snd_compress_new(struct snd_card *card, int device,
689 int dirn, struct snd_compr *compr)
691 static struct snd_device_ops ops = {
693 .dev_register = snd_compress_dev_register,
694 .dev_disconnect = snd_compress_dev_disconnect,
698 compr->device = device;
699 compr->direction = dirn;
700 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
702 EXPORT_SYMBOL_GPL(snd_compress_new);
704 static int snd_compress_add_device(struct snd_compr *device)
711 /* register the card */
712 ret = snd_card_register(device->card);
718 pr_err("failed with %d\n", ret);
723 static int snd_compress_remove_device(struct snd_compr *device)
725 return snd_card_free(device->card);
729 * snd_compress_register - register compressed device
731 * @device: compressed device to register
733 int snd_compress_register(struct snd_compr *device)
737 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
740 pr_debug("Registering compressed device %s\n", device->name);
741 if (snd_BUG_ON(!device->ops->open))
743 if (snd_BUG_ON(!device->ops->free))
745 if (snd_BUG_ON(!device->ops->set_params))
747 if (snd_BUG_ON(!device->ops->trigger))
750 mutex_init(&device->lock);
752 /* register a compressed card */
753 mutex_lock(&device_mutex);
754 retval = snd_compress_add_device(device);
755 mutex_unlock(&device_mutex);
758 EXPORT_SYMBOL_GPL(snd_compress_register);
760 int snd_compress_deregister(struct snd_compr *device)
762 pr_debug("Removing compressed device %s\n", device->name);
763 mutex_lock(&device_mutex);
764 snd_compress_remove_device(device);
765 mutex_unlock(&device_mutex);
768 EXPORT_SYMBOL_GPL(snd_compress_deregister);
770 static int __init snd_compress_init(void)
775 static void __exit snd_compress_exit(void)
779 module_init(snd_compress_init);
780 module_exit(snd_compress_exit);
782 MODULE_DESCRIPTION("ALSA Compressed offload framework");
783 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
784 MODULE_LICENSE("GPL v2");