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_WRONLY)
84 dirn = SND_COMPRESS_PLAYBACK;
85 else if (f->f_flags & O_RDONLY)
86 dirn = SND_COMPRESS_CAPTURE;
88 pr_err("invalid direction\n");
93 compr = snd_lookup_minor_data(iminor(inode),
94 SNDRV_DEVICE_TYPE_COMPRESS);
99 pr_err("no device data!!!\n");
103 if (dirn != compr->direction) {
104 pr_err("this device doesn't support this direction\n");
108 data = kzalloc(sizeof(*data), GFP_KERNEL);
111 data->stream.ops = compr->ops;
112 data->stream.direction = dirn;
113 data->stream.private_data = compr->private_data;
114 data->stream.device = compr;
115 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
120 runtime->state = SNDRV_PCM_STATE_OPEN;
121 init_waitqueue_head(&runtime->sleep);
122 data->stream.runtime = runtime;
123 f->private_data = (void *)data;
124 mutex_lock(&compr->lock);
125 ret = compr->ops->open(&data->stream);
126 mutex_unlock(&compr->lock);
134 static int snd_compr_free(struct inode *inode, struct file *f)
136 struct snd_compr_file *data = f->private_data;
137 data->stream.ops->free(&data->stream);
138 kfree(data->stream.runtime->buffer);
139 kfree(data->stream.runtime);
144 static void snd_compr_update_tstamp(struct snd_compr_stream *stream,
145 struct snd_compr_tstamp *tstamp)
147 if (!stream->ops->pointer)
149 stream->ops->pointer(stream, tstamp);
150 pr_debug("dsp consumed till %d total %d bytes\n",
151 tstamp->byte_offset, tstamp->copied_total);
152 stream->runtime->hw_pointer = tstamp->byte_offset;
153 stream->runtime->total_bytes_transferred = tstamp->copied_total;
156 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
157 struct snd_compr_avail *avail)
159 long avail_calc; /*this needs to be signed variable */
161 snd_compr_update_tstamp(stream, &avail->tstamp);
163 /* FIXME: This needs to be different for capture stream,
164 available is # of compressed data, for playback it's
165 remainder of buffer */
167 if (stream->runtime->total_bytes_available == 0 &&
168 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
169 pr_debug("detected init and someone forgot to do a write\n");
170 return stream->runtime->buffer_size;
172 pr_debug("app wrote %lld, DSP consumed %lld\n",
173 stream->runtime->total_bytes_available,
174 stream->runtime->total_bytes_transferred);
175 if (stream->runtime->total_bytes_available ==
176 stream->runtime->total_bytes_transferred) {
177 pr_debug("both pointers are same, returning full avail\n");
178 return stream->runtime->buffer_size;
181 /* FIXME: this routine isn't consistent, in one test we use
182 * cumulative values and in the other byte offsets. Do we
183 * really need the byte offsets if the cumulative values have
184 * been updated? In the PCM interface app_ptr and hw_ptr are
185 * already cumulative */
187 avail_calc = stream->runtime->buffer_size -
188 (stream->runtime->app_pointer - stream->runtime->hw_pointer);
189 pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc,
190 stream->runtime->app_pointer,
191 stream->runtime->hw_pointer);
192 if (avail_calc >= stream->runtime->buffer_size)
193 avail_calc -= stream->runtime->buffer_size;
194 pr_debug("ret avail as %ld\n", avail_calc);
195 avail->avail = avail_calc;
199 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
201 struct snd_compr_avail avail;
203 return snd_compr_calc_avail(stream, &avail);
207 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
209 struct snd_compr_avail ioctl_avail;
212 avail = snd_compr_calc_avail(stream, &ioctl_avail);
213 ioctl_avail.avail = avail;
215 if (copy_to_user((__u64 __user *)arg,
216 &ioctl_avail, sizeof(ioctl_avail)))
221 static int snd_compr_write_data(struct snd_compr_stream *stream,
222 const char __user *buf, size_t count)
226 struct snd_compr_runtime *runtime = stream->runtime;
228 dstn = runtime->buffer + runtime->app_pointer;
229 pr_debug("copying %ld at %lld\n",
230 (unsigned long)count, runtime->app_pointer);
231 if (count < runtime->buffer_size - runtime->app_pointer) {
232 if (copy_from_user(dstn, buf, count))
234 runtime->app_pointer += count;
236 copy = runtime->buffer_size - runtime->app_pointer;
237 if (copy_from_user(dstn, buf, copy))
239 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
241 runtime->app_pointer = count - copy;
243 /* if DSP cares, let it know data has been written */
244 if (stream->ops->ack)
245 stream->ops->ack(stream, count);
249 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
250 size_t count, loff_t *offset)
252 struct snd_compr_file *data = f->private_data;
253 struct snd_compr_stream *stream;
257 if (snd_BUG_ON(!data))
260 stream = &data->stream;
261 mutex_lock(&stream->device->lock);
262 /* write is allowed when stream is running or has been steup */
263 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
264 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
265 mutex_unlock(&stream->device->lock);
269 avail = snd_compr_get_avail(stream);
270 pr_debug("avail returned %ld\n", (unsigned long)avail);
271 /* calculate how much we can write to buffer */
275 if (stream->ops->copy)
276 retval = stream->ops->copy(stream, buf, avail);
278 retval = snd_compr_write_data(stream, buf, avail);
280 stream->runtime->total_bytes_available += retval;
282 /* while initiating the stream, write should be called before START
283 * call, so in setup move state */
284 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
285 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
286 pr_debug("stream prepared, Houston we are good to go\n");
289 mutex_unlock(&stream->device->lock);
294 static ssize_t snd_compr_read(struct file *f, char __user *buf,
295 size_t count, loff_t *offset)
300 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
305 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
307 if (stream->direction == SND_COMPRESS_PLAYBACK)
308 return POLLOUT | POLLWRNORM;
310 return POLLIN | POLLRDNORM;
313 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
315 struct snd_compr_file *data = f->private_data;
316 struct snd_compr_stream *stream;
320 if (snd_BUG_ON(!data))
322 stream = &data->stream;
323 if (snd_BUG_ON(!stream))
326 mutex_lock(&stream->device->lock);
327 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
328 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
332 poll_wait(f, &stream->runtime->sleep, wait);
334 avail = snd_compr_get_avail(stream);
335 pr_debug("avail is %ld\n", (unsigned long)avail);
336 /* check if we have at least one fragment to fill */
337 switch (stream->runtime->state) {
338 case SNDRV_PCM_STATE_DRAINING:
339 /* stream has been woken up after drain is complete
340 * draining done so set stream state to stopped
342 retval = snd_compr_get_poll(stream);
343 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
345 case SNDRV_PCM_STATE_RUNNING:
346 case SNDRV_PCM_STATE_PREPARED:
347 case SNDRV_PCM_STATE_PAUSED:
348 if (avail >= stream->runtime->fragment_size)
349 retval = snd_compr_get_poll(stream);
352 if (stream->direction == SND_COMPRESS_PLAYBACK)
353 retval = POLLOUT | POLLWRNORM | POLLERR;
355 retval = POLLIN | POLLRDNORM | POLLERR;
359 mutex_unlock(&stream->device->lock);
364 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
367 struct snd_compr_caps caps;
369 if (!stream->ops->get_caps)
372 retval = stream->ops->get_caps(stream, &caps);
375 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
382 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
385 struct snd_compr_codec_caps *caps;
387 if (!stream->ops->get_codec_caps)
390 caps = kmalloc(sizeof(*caps), GFP_KERNEL);
394 retval = stream->ops->get_codec_caps(stream, caps);
397 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
405 /* revisit this with snd_pcm_preallocate_xxx */
406 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
407 struct snd_compr_params *params)
409 unsigned int buffer_size;
412 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
413 if (stream->ops->copy) {
415 /* if copy is defined the driver will be required to copy
419 buffer = kmalloc(buffer_size, GFP_KERNEL);
423 stream->runtime->fragment_size = params->buffer.fragment_size;
424 stream->runtime->fragments = params->buffer.fragments;
425 stream->runtime->buffer = buffer;
426 stream->runtime->buffer_size = buffer_size;
431 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
433 struct snd_compr_params *params;
436 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
438 * we should allow parameter change only when stream has been
439 * opened not in other cases
441 params = kmalloc(sizeof(*params), GFP_KERNEL);
444 if (copy_from_user(params, (void __user *)arg, sizeof(*params)))
446 retval = snd_compr_allocate_buffer(stream, params);
451 retval = stream->ops->set_params(stream, params);
454 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
463 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
465 struct snd_codec *params;
468 if (!stream->ops->get_params)
471 params = kmalloc(sizeof(*params), GFP_KERNEL);
474 retval = stream->ops->get_params(stream, params);
477 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
486 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
488 struct snd_compr_tstamp tstamp;
490 snd_compr_update_tstamp(stream, &tstamp);
491 return copy_to_user((struct snd_compr_tstamp __user *)arg,
492 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
495 static int snd_compr_pause(struct snd_compr_stream *stream)
499 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
501 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
503 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
504 wake_up(&stream->runtime->sleep);
509 static int snd_compr_resume(struct snd_compr_stream *stream)
513 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
515 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
517 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
521 static int snd_compr_start(struct snd_compr_stream *stream)
525 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
527 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
529 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
533 static int snd_compr_stop(struct snd_compr_stream *stream)
537 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
538 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
540 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
542 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
543 wake_up(&stream->runtime->sleep);
548 static int snd_compr_drain(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, SND_COMPR_TRIGGER_DRAIN);
557 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
558 wake_up(&stream->runtime->sleep);
563 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
565 struct snd_compr_file *data = f->private_data;
566 struct snd_compr_stream *stream;
567 int retval = -ENOTTY;
569 if (snd_BUG_ON(!data))
571 stream = &data->stream;
572 if (snd_BUG_ON(!stream))
574 mutex_lock(&stream->device->lock);
575 switch (_IOC_NR(cmd)) {
576 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
577 put_user(SNDRV_COMPRESS_VERSION,
578 (int __user *)arg) ? -EFAULT : 0;
580 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
581 retval = snd_compr_get_caps(stream, arg);
583 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
584 retval = snd_compr_get_codec_caps(stream, arg);
586 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
587 retval = snd_compr_set_params(stream, arg);
589 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
590 retval = snd_compr_get_params(stream, arg);
592 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
593 retval = snd_compr_tstamp(stream, arg);
595 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
596 retval = snd_compr_ioctl_avail(stream, arg);
598 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
599 retval = snd_compr_pause(stream);
601 case _IOC_NR(SNDRV_COMPRESS_RESUME):
602 retval = snd_compr_resume(stream);
604 case _IOC_NR(SNDRV_COMPRESS_START):
605 retval = snd_compr_start(stream);
607 case _IOC_NR(SNDRV_COMPRESS_STOP):
608 retval = snd_compr_stop(stream);
610 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
611 retval = snd_compr_drain(stream);
614 mutex_unlock(&stream->device->lock);
618 static const struct file_operations snd_compr_file_ops = {
619 .owner = THIS_MODULE,
620 .open = snd_compr_open,
621 .release = snd_compr_free,
622 .write = snd_compr_write,
623 .read = snd_compr_read,
624 .unlocked_ioctl = snd_compr_ioctl,
625 .mmap = snd_compr_mmap,
626 .poll = snd_compr_poll,
629 static int snd_compress_dev_register(struct snd_device *device)
633 struct snd_compr *compr;
635 if (snd_BUG_ON(!device || !device->device_data))
637 compr = device->device_data;
639 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
640 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
642 /* register compressed device */
643 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
644 compr->device, &snd_compr_file_ops, compr, str);
646 pr_err("snd_register_device failed\n %d", ret);
653 static int snd_compress_dev_disconnect(struct snd_device *device)
655 struct snd_compr *compr;
657 compr = device->device_data;
658 snd_unregister_device(compr->direction, compr->card, compr->device);
663 * snd_compress_new: create new compress device
664 * @card: sound card pointer
665 * @device: device number
666 * @dirn: device direction, should be of type enum snd_compr_direction
667 * @compr: compress device pointer
669 int snd_compress_new(struct snd_card *card, int device,
670 int dirn, struct snd_compr *compr)
672 static struct snd_device_ops ops = {
674 .dev_register = snd_compress_dev_register,
675 .dev_disconnect = snd_compress_dev_disconnect,
679 compr->device = device;
680 compr->direction = dirn;
681 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
683 EXPORT_SYMBOL_GPL(snd_compress_new);
685 static int snd_compress_add_device(struct snd_compr *device)
692 /* register the card */
693 ret = snd_card_register(device->card);
699 pr_err("failed with %d\n", ret);
704 static int snd_compress_remove_device(struct snd_compr *device)
706 return snd_card_free(device->card);
710 * snd_compress_register - register compressed device
712 * @device: compressed device to register
714 int snd_compress_register(struct snd_compr *device)
718 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
721 pr_debug("Registering compressed device %s\n", device->name);
722 if (snd_BUG_ON(!device->ops->open))
724 if (snd_BUG_ON(!device->ops->free))
726 if (snd_BUG_ON(!device->ops->set_params))
728 if (snd_BUG_ON(!device->ops->trigger))
731 mutex_init(&device->lock);
733 /* register a compressed card */
734 mutex_lock(&device_mutex);
735 retval = snd_compress_add_device(device);
736 mutex_unlock(&device_mutex);
739 EXPORT_SYMBOL_GPL(snd_compress_register);
741 int snd_compress_deregister(struct snd_compr *device)
743 pr_debug("Removing compressed device %s\n", device->name);
744 mutex_lock(&device_mutex);
745 snd_compress_remove_device(device);
746 mutex_unlock(&device_mutex);
749 EXPORT_SYMBOL_GPL(snd_compress_deregister);
751 static int __init snd_compress_init(void)
756 static void __exit snd_compress_exit(void)
760 module_init(snd_compress_init);
761 module_exit(snd_compress_exit);
763 MODULE_DESCRIPTION("ALSA Compressed offload framework");
764 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
765 MODULE_LICENSE("GPL v2");