]> Pileus Git - ~andy/linux/blob - sound/core/compress_offload.c
27bd81ad284102f196442302d816432721872319
[~andy/linux] / sound / core / compress_offload.c
1 /*
2  *  compress_core.c - compress offload core
3  *
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  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
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.
12  *
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.
17  *
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.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  *
24  */
25 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
27
28 #include <linux/file.h>
29 #include <linux/fs.h>
30 #include <linux/list.h>
31 #include <linux/mm.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>
44
45 /* TODO:
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
50  */
51
52 static DEFINE_MUTEX(device_mutex);
53
54 struct snd_compr_file {
55         unsigned long caps;
56         struct snd_compr_stream stream;
57 };
58
59 /*
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.
73  */
74 static int snd_compr_open(struct inode *inode, struct file *f)
75 {
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);
81         int ret;
82
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;
87         else
88                 return -EINVAL;
89
90         if (maj == snd_major)
91                 compr = snd_lookup_minor_data(iminor(inode),
92                                         SNDRV_DEVICE_TYPE_COMPRESS);
93         else
94                 return -EBADFD;
95
96         if (compr == NULL) {
97                 pr_err("no device data!!!\n");
98                 return -ENODEV;
99         }
100
101         if (dirn != compr->direction) {
102                 pr_err("this device doesn't support this direction\n");
103                 snd_card_unref(compr->card);
104                 return -EINVAL;
105         }
106
107         data = kzalloc(sizeof(*data), GFP_KERNEL);
108         if (!data) {
109                 snd_card_unref(compr->card);
110                 return -ENOMEM;
111         }
112         data->stream.ops = compr->ops;
113         data->stream.direction = dirn;
114         data->stream.private_data = compr->private_data;
115         data->stream.device = compr;
116         runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
117         if (!runtime) {
118                 kfree(data);
119                 snd_card_unref(compr->card);
120                 return -ENOMEM;
121         }
122         runtime->state = SNDRV_PCM_STATE_OPEN;
123         init_waitqueue_head(&runtime->sleep);
124         data->stream.runtime = runtime;
125         f->private_data = (void *)data;
126         mutex_lock(&compr->lock);
127         ret = compr->ops->open(&data->stream);
128         mutex_unlock(&compr->lock);
129         if (ret) {
130                 kfree(runtime);
131                 kfree(data);
132         }
133         snd_card_unref(compr->card);
134         return 0;
135 }
136
137 static int snd_compr_free(struct inode *inode, struct file *f)
138 {
139         struct snd_compr_file *data = f->private_data;
140         data->stream.ops->free(&data->stream);
141         kfree(data->stream.runtime->buffer);
142         kfree(data->stream.runtime);
143         kfree(data);
144         return 0;
145 }
146
147 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
148                 struct snd_compr_tstamp *tstamp)
149 {
150         if (!stream->ops->pointer)
151                 return -ENOTSUPP;
152         stream->ops->pointer(stream, tstamp);
153         pr_debug("dsp consumed till %d total %d bytes\n",
154                 tstamp->byte_offset, tstamp->copied_total);
155         stream->runtime->hw_pointer = tstamp->byte_offset;
156         stream->runtime->total_bytes_transferred = tstamp->copied_total;
157         return 0;
158 }
159
160 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
161                 struct snd_compr_avail *avail)
162 {
163         memset(avail, 0, sizeof(*avail));
164         snd_compr_update_tstamp(stream, &avail->tstamp);
165         /* Still need to return avail even if tstamp can't be filled in */
166
167         /* FIXME: This needs to be different for capture stream,
168            available is # of compressed data, for playback it's
169            remainder of buffer */
170
171         if (stream->runtime->total_bytes_available == 0 &&
172                         stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
173                 pr_debug("detected init and someone forgot to do a write\n");
174                 return stream->runtime->buffer_size;
175         }
176         pr_debug("app wrote %lld, DSP consumed %lld\n",
177                         stream->runtime->total_bytes_available,
178                         stream->runtime->total_bytes_transferred);
179         if (stream->runtime->total_bytes_available ==
180                                 stream->runtime->total_bytes_transferred) {
181                 pr_debug("both pointers are same, returning full avail\n");
182                 return stream->runtime->buffer_size;
183         }
184
185         avail->avail = stream->runtime->buffer_size -
186                 (stream->runtime->total_bytes_available -
187                  stream->runtime->total_bytes_transferred);
188         pr_debug("ret avail as %lld\n", avail->avail);
189         return avail->avail;
190 }
191
192 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
193 {
194         struct snd_compr_avail avail;
195
196         return snd_compr_calc_avail(stream, &avail);
197 }
198
199 static int
200 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
201 {
202         struct snd_compr_avail ioctl_avail;
203         size_t avail;
204
205         avail = snd_compr_calc_avail(stream, &ioctl_avail);
206         ioctl_avail.avail = avail;
207
208         if (copy_to_user((__u64 __user *)arg,
209                                 &ioctl_avail, sizeof(ioctl_avail)))
210                 return -EFAULT;
211         return 0;
212 }
213
214 static int snd_compr_write_data(struct snd_compr_stream *stream,
215                const char __user *buf, size_t count)
216 {
217         void *dstn;
218         size_t copy;
219         struct snd_compr_runtime *runtime = stream->runtime;
220
221         dstn = runtime->buffer + runtime->app_pointer;
222         pr_debug("copying %ld at %lld\n",
223                         (unsigned long)count, runtime->app_pointer);
224         if (count < runtime->buffer_size - runtime->app_pointer) {
225                 if (copy_from_user(dstn, buf, count))
226                         return -EFAULT;
227                 runtime->app_pointer += count;
228         } else {
229                 copy = runtime->buffer_size - runtime->app_pointer;
230                 if (copy_from_user(dstn, buf, copy))
231                         return -EFAULT;
232                 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
233                         return -EFAULT;
234                 runtime->app_pointer = count - copy;
235         }
236         /* if DSP cares, let it know data has been written */
237         if (stream->ops->ack)
238                 stream->ops->ack(stream, count);
239         return count;
240 }
241
242 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
243                 size_t count, loff_t *offset)
244 {
245         struct snd_compr_file *data = f->private_data;
246         struct snd_compr_stream *stream;
247         size_t avail;
248         int retval;
249
250         if (snd_BUG_ON(!data))
251                 return -EFAULT;
252
253         stream = &data->stream;
254         mutex_lock(&stream->device->lock);
255         /* write is allowed when stream is running or has been steup */
256         if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
257                         stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
258                 mutex_unlock(&stream->device->lock);
259                 return -EBADFD;
260         }
261
262         avail = snd_compr_get_avail(stream);
263         pr_debug("avail returned %ld\n", (unsigned long)avail);
264         /* calculate how much we can write to buffer */
265         if (avail > count)
266                 avail = count;
267
268         if (stream->ops->copy)
269                 retval = stream->ops->copy(stream, buf, avail);
270         else
271                 retval = snd_compr_write_data(stream, buf, avail);
272         if (retval > 0)
273                 stream->runtime->total_bytes_available += retval;
274
275         /* while initiating the stream, write should be called before START
276          * call, so in setup move state */
277         if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
278                 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
279                 pr_debug("stream prepared, Houston we are good to go\n");
280         }
281
282         mutex_unlock(&stream->device->lock);
283         return retval;
284 }
285
286
287 static ssize_t snd_compr_read(struct file *f, char __user *buf,
288                 size_t count, loff_t *offset)
289 {
290         return -ENXIO;
291 }
292
293 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
294 {
295         return -ENXIO;
296 }
297
298 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
299 {
300         if (stream->direction == SND_COMPRESS_PLAYBACK)
301                 return POLLOUT | POLLWRNORM;
302         else
303                 return POLLIN | POLLRDNORM;
304 }
305
306 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
307 {
308         struct snd_compr_file *data = f->private_data;
309         struct snd_compr_stream *stream;
310         size_t avail;
311         int retval = 0;
312
313         if (snd_BUG_ON(!data))
314                 return -EFAULT;
315         stream = &data->stream;
316         if (snd_BUG_ON(!stream))
317                 return -EFAULT;
318
319         mutex_lock(&stream->device->lock);
320         if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
321                         stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
322                 retval = -EBADFD;
323                 goto out;
324         }
325         poll_wait(f, &stream->runtime->sleep, wait);
326
327         avail = snd_compr_get_avail(stream);
328         pr_debug("avail is %ld\n", (unsigned long)avail);
329         /* check if we have at least one fragment to fill */
330         switch (stream->runtime->state) {
331         case SNDRV_PCM_STATE_DRAINING:
332                 /* stream has been woken up after drain is complete
333                  * draining done so set stream state to stopped
334                  */
335                 retval = snd_compr_get_poll(stream);
336                 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
337                 break;
338         case SNDRV_PCM_STATE_RUNNING:
339         case SNDRV_PCM_STATE_PREPARED:
340         case SNDRV_PCM_STATE_PAUSED:
341                 if (avail >= stream->runtime->fragment_size)
342                         retval = snd_compr_get_poll(stream);
343                 break;
344         default:
345                 if (stream->direction == SND_COMPRESS_PLAYBACK)
346                         retval = POLLOUT | POLLWRNORM | POLLERR;
347                 else
348                         retval = POLLIN | POLLRDNORM | POLLERR;
349                 break;
350         }
351 out:
352         mutex_unlock(&stream->device->lock);
353         return retval;
354 }
355
356 static int
357 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
358 {
359         int retval;
360         struct snd_compr_caps caps;
361
362         if (!stream->ops->get_caps)
363                 return -ENXIO;
364
365         retval = stream->ops->get_caps(stream, &caps);
366         if (retval)
367                 goto out;
368         if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
369                 retval = -EFAULT;
370 out:
371         return retval;
372 }
373
374 static int
375 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
376 {
377         int retval;
378         struct snd_compr_codec_caps *caps;
379
380         if (!stream->ops->get_codec_caps)
381                 return -ENXIO;
382
383         caps = kmalloc(sizeof(*caps), GFP_KERNEL);
384         if (!caps)
385                 return -ENOMEM;
386
387         retval = stream->ops->get_codec_caps(stream, caps);
388         if (retval)
389                 goto out;
390         if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
391                 retval = -EFAULT;
392
393 out:
394         kfree(caps);
395         return retval;
396 }
397
398 /* revisit this with snd_pcm_preallocate_xxx */
399 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
400                 struct snd_compr_params *params)
401 {
402         unsigned int buffer_size;
403         void *buffer;
404
405         buffer_size = params->buffer.fragment_size * params->buffer.fragments;
406         if (stream->ops->copy) {
407                 buffer = NULL;
408                 /* if copy is defined the driver will be required to copy
409                  * the data from core
410                  */
411         } else {
412                 buffer = kmalloc(buffer_size, GFP_KERNEL);
413                 if (!buffer)
414                         return -ENOMEM;
415         }
416         stream->runtime->fragment_size = params->buffer.fragment_size;
417         stream->runtime->fragments = params->buffer.fragments;
418         stream->runtime->buffer = buffer;
419         stream->runtime->buffer_size = buffer_size;
420         return 0;
421 }
422
423 static int snd_compress_check_input(struct snd_compr_params *params)
424 {
425         /* first let's check the buffer parameter's */
426         if (params->buffer.fragment_size == 0 ||
427                         params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
428                 return -EINVAL;
429
430         /* now codec parameters */
431         if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
432                 return -EINVAL;
433
434         if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
435                 return -EINVAL;
436
437         if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
438                 return -EINVAL;
439
440         return 0;
441 }
442
443 static int
444 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
445 {
446         struct snd_compr_params *params;
447         int retval;
448
449         if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
450                 /*
451                  * we should allow parameter change only when stream has been
452                  * opened not in other cases
453                  */
454                 params = kmalloc(sizeof(*params), GFP_KERNEL);
455                 if (!params)
456                         return -ENOMEM;
457                 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
458                         retval = -EFAULT;
459                         goto out;
460                 }
461
462                 retval = snd_compress_check_input(params);
463                 if (retval)
464                         goto out;
465
466                 retval = snd_compr_allocate_buffer(stream, params);
467                 if (retval) {
468                         retval = -ENOMEM;
469                         goto out;
470                 }
471
472                 retval = stream->ops->set_params(stream, params);
473                 if (retval)
474                         goto out;
475                 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
476                 stream->metadata_set = false;
477                 stream->next_track = false;
478         } else {
479                 return -EPERM;
480         }
481 out:
482         kfree(params);
483         return retval;
484 }
485
486 static int
487 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
488 {
489         struct snd_codec *params;
490         int retval;
491
492         if (!stream->ops->get_params)
493                 return -EBADFD;
494
495         params = kmalloc(sizeof(*params), GFP_KERNEL);
496         if (!params)
497                 return -ENOMEM;
498         retval = stream->ops->get_params(stream, params);
499         if (retval)
500                 goto out;
501         if (copy_to_user((char __user *)arg, params, sizeof(*params)))
502                 retval = -EFAULT;
503
504 out:
505         kfree(params);
506         return retval;
507 }
508
509 static int
510 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
511 {
512         struct snd_compr_metadata metadata;
513         int retval;
514
515         if (!stream->ops->get_metadata)
516                 return -ENXIO;
517
518         if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
519                 return -EFAULT;
520
521         retval = stream->ops->get_metadata(stream, &metadata);
522         if (retval != 0)
523                 return retval;
524
525         if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
526                 return -EFAULT;
527
528         return 0;
529 }
530
531 static int
532 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
533 {
534         struct snd_compr_metadata metadata;
535         int retval;
536
537         if (!stream->ops->set_metadata)
538                 return -ENXIO;
539         /*
540         * we should allow parameter change only when stream has been
541         * opened not in other cases
542         */
543         if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
544                 return -EFAULT;
545
546         retval = stream->ops->set_metadata(stream, &metadata);
547         stream->metadata_set = true;
548
549         return retval;
550 }
551
552 static inline int
553 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
554 {
555         struct snd_compr_tstamp tstamp = {0};
556         int ret;
557
558         ret = snd_compr_update_tstamp(stream, &tstamp);
559         if (ret == 0)
560                 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
561                         &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
562         return ret;
563 }
564
565 static int snd_compr_pause(struct snd_compr_stream *stream)
566 {
567         int retval;
568
569         if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
570                 return -EPERM;
571         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
572         if (!retval)
573                 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
574         return retval;
575 }
576
577 static int snd_compr_resume(struct snd_compr_stream *stream)
578 {
579         int retval;
580
581         if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
582                 return -EPERM;
583         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
584         if (!retval)
585                 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
586         return retval;
587 }
588
589 static int snd_compr_start(struct snd_compr_stream *stream)
590 {
591         int retval;
592
593         if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
594                 return -EPERM;
595         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
596         if (!retval)
597                 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
598         return retval;
599 }
600
601 static int snd_compr_stop(struct snd_compr_stream *stream)
602 {
603         int retval;
604
605         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
606                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
607                 return -EPERM;
608         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
609         if (!retval) {
610                 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
611                 wake_up(&stream->runtime->sleep);
612                 stream->runtime->hw_pointer = 0;
613                 stream->runtime->app_pointer = 0;
614                 stream->runtime->total_bytes_available = 0;
615                 stream->runtime->total_bytes_transferred = 0;
616         }
617         return retval;
618 }
619
620 static int snd_compr_drain(struct snd_compr_stream *stream)
621 {
622         int retval;
623
624         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
625                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
626                 return -EPERM;
627         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
628         if (!retval) {
629                 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
630                 wake_up(&stream->runtime->sleep);
631         }
632         return retval;
633 }
634
635 static int snd_compr_next_track(struct snd_compr_stream *stream)
636 {
637         int retval;
638
639         /* only a running stream can transition to next track */
640         if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
641                 return -EPERM;
642
643         /* you can signal next track isf this is intended to be a gapless stream
644          * and current track metadata is set
645          */
646         if (stream->metadata_set == false)
647                 return -EPERM;
648
649         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
650         if (retval != 0)
651                 return retval;
652         stream->metadata_set = false;
653         stream->next_track = true;
654         return 0;
655 }
656
657 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
658 {
659         int retval;
660         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
661                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
662                 return -EPERM;
663         /* stream can be drained only when next track has been signalled */
664         if (stream->next_track == false)
665                 return -EPERM;
666
667         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
668
669         stream->next_track = false;
670         return retval;
671 }
672
673 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
674 {
675         struct snd_compr_file *data = f->private_data;
676         struct snd_compr_stream *stream;
677         int retval = -ENOTTY;
678
679         if (snd_BUG_ON(!data))
680                 return -EFAULT;
681         stream = &data->stream;
682         if (snd_BUG_ON(!stream))
683                 return -EFAULT;
684         mutex_lock(&stream->device->lock);
685         switch (_IOC_NR(cmd)) {
686         case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
687                 put_user(SNDRV_COMPRESS_VERSION,
688                                 (int __user *)arg) ? -EFAULT : 0;
689                 break;
690         case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
691                 retval = snd_compr_get_caps(stream, arg);
692                 break;
693         case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
694                 retval = snd_compr_get_codec_caps(stream, arg);
695                 break;
696         case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
697                 retval = snd_compr_set_params(stream, arg);
698                 break;
699         case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
700                 retval = snd_compr_get_params(stream, arg);
701                 break;
702         case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
703                 retval = snd_compr_set_metadata(stream, arg);
704                 break;
705         case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
706                 retval = snd_compr_get_metadata(stream, arg);
707                 break;
708         case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
709                 retval = snd_compr_tstamp(stream, arg);
710                 break;
711         case _IOC_NR(SNDRV_COMPRESS_AVAIL):
712                 retval = snd_compr_ioctl_avail(stream, arg);
713                 break;
714         case _IOC_NR(SNDRV_COMPRESS_PAUSE):
715                 retval = snd_compr_pause(stream);
716                 break;
717         case _IOC_NR(SNDRV_COMPRESS_RESUME):
718                 retval = snd_compr_resume(stream);
719                 break;
720         case _IOC_NR(SNDRV_COMPRESS_START):
721                 retval = snd_compr_start(stream);
722                 break;
723         case _IOC_NR(SNDRV_COMPRESS_STOP):
724                 retval = snd_compr_stop(stream);
725                 break;
726         case _IOC_NR(SNDRV_COMPRESS_DRAIN):
727                 retval = snd_compr_drain(stream);
728                 break;
729         case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
730                 retval = snd_compr_partial_drain(stream);
731                 break;
732         case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
733                 retval = snd_compr_next_track(stream);
734                 break;
735
736         }
737         mutex_unlock(&stream->device->lock);
738         return retval;
739 }
740
741 static const struct file_operations snd_compr_file_ops = {
742                 .owner =        THIS_MODULE,
743                 .open =         snd_compr_open,
744                 .release =      snd_compr_free,
745                 .write =        snd_compr_write,
746                 .read =         snd_compr_read,
747                 .unlocked_ioctl = snd_compr_ioctl,
748                 .mmap =         snd_compr_mmap,
749                 .poll =         snd_compr_poll,
750 };
751
752 static int snd_compress_dev_register(struct snd_device *device)
753 {
754         int ret = -EINVAL;
755         char str[16];
756         struct snd_compr *compr;
757
758         if (snd_BUG_ON(!device || !device->device_data))
759                 return -EBADFD;
760         compr = device->device_data;
761
762         sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
763         pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
764                         compr->direction);
765         /* register compressed device */
766         ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
767                         compr->device, &snd_compr_file_ops, compr, str);
768         if (ret < 0) {
769                 pr_err("snd_register_device failed\n %d", ret);
770                 return ret;
771         }
772         return ret;
773
774 }
775
776 static int snd_compress_dev_disconnect(struct snd_device *device)
777 {
778         struct snd_compr *compr;
779
780         compr = device->device_data;
781         snd_unregister_device(compr->direction, compr->card, compr->device);
782         return 0;
783 }
784
785 /*
786  * snd_compress_new: create new compress device
787  * @card: sound card pointer
788  * @device: device number
789  * @dirn: device direction, should be of type enum snd_compr_direction
790  * @compr: compress device pointer
791  */
792 int snd_compress_new(struct snd_card *card, int device,
793                         int dirn, struct snd_compr *compr)
794 {
795         static struct snd_device_ops ops = {
796                 .dev_free = NULL,
797                 .dev_register = snd_compress_dev_register,
798                 .dev_disconnect = snd_compress_dev_disconnect,
799         };
800
801         compr->card = card;
802         compr->device = device;
803         compr->direction = dirn;
804         return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
805 }
806 EXPORT_SYMBOL_GPL(snd_compress_new);
807
808 static int snd_compress_add_device(struct snd_compr *device)
809 {
810         int ret;
811
812         if (!device->card)
813                 return -EINVAL;
814
815         /* register the card */
816         ret = snd_card_register(device->card);
817         if (ret)
818                 goto out;
819         return 0;
820
821 out:
822         pr_err("failed with %d\n", ret);
823         return ret;
824
825 }
826
827 static int snd_compress_remove_device(struct snd_compr *device)
828 {
829         return snd_card_free(device->card);
830 }
831
832 /**
833  * snd_compress_register - register compressed device
834  *
835  * @device: compressed device to register
836  */
837 int snd_compress_register(struct snd_compr *device)
838 {
839         int retval;
840
841         if (device->name == NULL || device->dev == NULL || device->ops == NULL)
842                 return -EINVAL;
843
844         pr_debug("Registering compressed device %s\n", device->name);
845         if (snd_BUG_ON(!device->ops->open))
846                 return -EINVAL;
847         if (snd_BUG_ON(!device->ops->free))
848                 return -EINVAL;
849         if (snd_BUG_ON(!device->ops->set_params))
850                 return -EINVAL;
851         if (snd_BUG_ON(!device->ops->trigger))
852                 return -EINVAL;
853
854         mutex_init(&device->lock);
855
856         /* register a compressed card */
857         mutex_lock(&device_mutex);
858         retval = snd_compress_add_device(device);
859         mutex_unlock(&device_mutex);
860         return retval;
861 }
862 EXPORT_SYMBOL_GPL(snd_compress_register);
863
864 int snd_compress_deregister(struct snd_compr *device)
865 {
866         pr_debug("Removing compressed device %s\n", device->name);
867         mutex_lock(&device_mutex);
868         snd_compress_remove_device(device);
869         mutex_unlock(&device_mutex);
870         return 0;
871 }
872 EXPORT_SYMBOL_GPL(snd_compress_deregister);
873
874 static int __init snd_compress_init(void)
875 {
876         return 0;
877 }
878
879 static void __exit snd_compress_exit(void)
880 {
881 }
882
883 module_init(snd_compress_init);
884 module_exit(snd_compress_exit);
885
886 MODULE_DESCRIPTION("ALSA Compressed offload framework");
887 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
888 MODULE_LICENSE("GPL v2");