Version:  2.0.40 2.2.26 2.4.37 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7

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/math64.h>
 32 #include <linux/mm.h>
 33 #include <linux/mutex.h>
 34 #include <linux/poll.h>
 35 #include <linux/slab.h>
 36 #include <linux/sched.h>
 37 #include <linux/types.h>
 38 #include <linux/uio.h>
 39 #include <linux/uaccess.h>
 40 #include <linux/module.h>
 41 #include <linux/compat.h>
 42 #include <sound/core.h>
 43 #include <sound/initval.h>
 44 #include <sound/info.h>
 45 #include <sound/compress_params.h>
 46 #include <sound/compress_offload.h>
 47 #include <sound/compress_driver.h>
 48 
 49 /* struct snd_compr_codec_caps overflows the ioctl bit size for some
 50  * architectures, so we need to disable the relevant ioctls.
 51  */
 52 #if _IOC_SIZEBITS < 14
 53 #define COMPR_CODEC_CAPS_OVERFLOW
 54 #endif
 55 
 56 /* TODO:
 57  * - add substream support for multiple devices in case of
 58  *      SND_DYNAMIC_MINORS is not used
 59  * - Multiple node representation
 60  *      driver should be able to register multiple nodes
 61  */
 62 
 63 static DEFINE_MUTEX(device_mutex);
 64 
 65 struct snd_compr_file {
 66         unsigned long caps;
 67         struct snd_compr_stream stream;
 68 };
 69 
 70 /*
 71  * a note on stream states used:
 72  * we use following states in the compressed core
 73  * SNDRV_PCM_STATE_OPEN: When stream has been opened.
 74  * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
 75  *      calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
 76  *      state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
 77  * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
 78  *      playback only). User after setting up stream writes the data buffer
 79  *      before starting the stream.
 80  * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
 81  *      decoding/encoding and rendering/capturing data.
 82  * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
 83  *      by calling SNDRV_COMPRESS_DRAIN.
 84  * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
 85  *      SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
 86  *      SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
 87  */
 88 static int snd_compr_open(struct inode *inode, struct file *f)
 89 {
 90         struct snd_compr *compr;
 91         struct snd_compr_file *data;
 92         struct snd_compr_runtime *runtime;
 93         enum snd_compr_direction dirn;
 94         int maj = imajor(inode);
 95         int ret;
 96 
 97         if ((f->f_flags & O_ACCMODE) == O_WRONLY)
 98                 dirn = SND_COMPRESS_PLAYBACK;
 99         else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
100                 dirn = SND_COMPRESS_CAPTURE;
101         else
102                 return -EINVAL;
103 
104         if (maj == snd_major)
105                 compr = snd_lookup_minor_data(iminor(inode),
106                                         SNDRV_DEVICE_TYPE_COMPRESS);
107         else
108                 return -EBADFD;
109 
110         if (compr == NULL) {
111                 pr_err("no device data!!!\n");
112                 return -ENODEV;
113         }
114 
115         if (dirn != compr->direction) {
116                 pr_err("this device doesn't support this direction\n");
117                 snd_card_unref(compr->card);
118                 return -EINVAL;
119         }
120 
121         data = kzalloc(sizeof(*data), GFP_KERNEL);
122         if (!data) {
123                 snd_card_unref(compr->card);
124                 return -ENOMEM;
125         }
126         data->stream.ops = compr->ops;
127         data->stream.direction = dirn;
128         data->stream.private_data = compr->private_data;
129         data->stream.device = compr;
130         runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
131         if (!runtime) {
132                 kfree(data);
133                 snd_card_unref(compr->card);
134                 return -ENOMEM;
135         }
136         runtime->state = SNDRV_PCM_STATE_OPEN;
137         init_waitqueue_head(&runtime->sleep);
138         data->stream.runtime = runtime;
139         f->private_data = (void *)data;
140         mutex_lock(&compr->lock);
141         ret = compr->ops->open(&data->stream);
142         mutex_unlock(&compr->lock);
143         if (ret) {
144                 kfree(runtime);
145                 kfree(data);
146         }
147         snd_card_unref(compr->card);
148         return ret;
149 }
150 
151 static int snd_compr_free(struct inode *inode, struct file *f)
152 {
153         struct snd_compr_file *data = f->private_data;
154         struct snd_compr_runtime *runtime = data->stream.runtime;
155 
156         switch (runtime->state) {
157         case SNDRV_PCM_STATE_RUNNING:
158         case SNDRV_PCM_STATE_DRAINING:
159         case SNDRV_PCM_STATE_PAUSED:
160                 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
161                 break;
162         default:
163                 break;
164         }
165 
166         data->stream.ops->free(&data->stream);
167         kfree(data->stream.runtime->buffer);
168         kfree(data->stream.runtime);
169         kfree(data);
170         return 0;
171 }
172 
173 static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
174                 struct snd_compr_tstamp *tstamp)
175 {
176         if (!stream->ops->pointer)
177                 return -ENOTSUPP;
178         stream->ops->pointer(stream, tstamp);
179         pr_debug("dsp consumed till %d total %d bytes\n",
180                 tstamp->byte_offset, tstamp->copied_total);
181         if (stream->direction == SND_COMPRESS_PLAYBACK)
182                 stream->runtime->total_bytes_transferred = tstamp->copied_total;
183         else
184                 stream->runtime->total_bytes_available = tstamp->copied_total;
185         return 0;
186 }
187 
188 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
189                 struct snd_compr_avail *avail)
190 {
191         memset(avail, 0, sizeof(*avail));
192         snd_compr_update_tstamp(stream, &avail->tstamp);
193         /* Still need to return avail even if tstamp can't be filled in */
194 
195         if (stream->runtime->total_bytes_available == 0 &&
196                         stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
197                         stream->direction == SND_COMPRESS_PLAYBACK) {
198                 pr_debug("detected init and someone forgot to do a write\n");
199                 return stream->runtime->buffer_size;
200         }
201         pr_debug("app wrote %lld, DSP consumed %lld\n",
202                         stream->runtime->total_bytes_available,
203                         stream->runtime->total_bytes_transferred);
204         if (stream->runtime->total_bytes_available ==
205                                 stream->runtime->total_bytes_transferred) {
206                 if (stream->direction == SND_COMPRESS_PLAYBACK) {
207                         pr_debug("both pointers are same, returning full avail\n");
208                         return stream->runtime->buffer_size;
209                 } else {
210                         pr_debug("both pointers are same, returning no avail\n");
211                         return 0;
212                 }
213         }
214 
215         avail->avail = stream->runtime->total_bytes_available -
216                         stream->runtime->total_bytes_transferred;
217         if (stream->direction == SND_COMPRESS_PLAYBACK)
218                 avail->avail = stream->runtime->buffer_size - avail->avail;
219 
220         pr_debug("ret avail as %lld\n", avail->avail);
221         return avail->avail;
222 }
223 
224 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
225 {
226         struct snd_compr_avail avail;
227 
228         return snd_compr_calc_avail(stream, &avail);
229 }
230 
231 static int
232 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
233 {
234         struct snd_compr_avail ioctl_avail;
235         size_t avail;
236 
237         avail = snd_compr_calc_avail(stream, &ioctl_avail);
238         ioctl_avail.avail = avail;
239 
240         if (copy_to_user((__u64 __user *)arg,
241                                 &ioctl_avail, sizeof(ioctl_avail)))
242                 return -EFAULT;
243         return 0;
244 }
245 
246 static int snd_compr_write_data(struct snd_compr_stream *stream,
247                const char __user *buf, size_t count)
248 {
249         void *dstn;
250         size_t copy;
251         struct snd_compr_runtime *runtime = stream->runtime;
252         /* 64-bit Modulus */
253         u64 app_pointer = div64_u64(runtime->total_bytes_available,
254                                     runtime->buffer_size);
255         app_pointer = runtime->total_bytes_available -
256                       (app_pointer * runtime->buffer_size);
257 
258         dstn = runtime->buffer + app_pointer;
259         pr_debug("copying %ld at %lld\n",
260                         (unsigned long)count, app_pointer);
261         if (count < runtime->buffer_size - app_pointer) {
262                 if (copy_from_user(dstn, buf, count))
263                         return -EFAULT;
264         } else {
265                 copy = runtime->buffer_size - app_pointer;
266                 if (copy_from_user(dstn, buf, copy))
267                         return -EFAULT;
268                 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
269                         return -EFAULT;
270         }
271         /* if DSP cares, let it know data has been written */
272         if (stream->ops->ack)
273                 stream->ops->ack(stream, count);
274         return count;
275 }
276 
277 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
278                 size_t count, loff_t *offset)
279 {
280         struct snd_compr_file *data = f->private_data;
281         struct snd_compr_stream *stream;
282         size_t avail;
283         int retval;
284 
285         if (snd_BUG_ON(!data))
286                 return -EFAULT;
287 
288         stream = &data->stream;
289         mutex_lock(&stream->device->lock);
290         /* write is allowed when stream is running or has been steup */
291         switch (stream->runtime->state) {
292         case SNDRV_PCM_STATE_SETUP:
293         case SNDRV_PCM_STATE_PREPARED:
294         case SNDRV_PCM_STATE_RUNNING:
295                 break;
296         default:
297                 mutex_unlock(&stream->device->lock);
298                 return -EBADFD;
299         }
300 
301         avail = snd_compr_get_avail(stream);
302         pr_debug("avail returned %ld\n", (unsigned long)avail);
303         /* calculate how much we can write to buffer */
304         if (avail > count)
305                 avail = count;
306 
307         if (stream->ops->copy) {
308                 char __user* cbuf = (char __user*)buf;
309                 retval = stream->ops->copy(stream, cbuf, avail);
310         } else {
311                 retval = snd_compr_write_data(stream, buf, avail);
312         }
313         if (retval > 0)
314                 stream->runtime->total_bytes_available += retval;
315 
316         /* while initiating the stream, write should be called before START
317          * call, so in setup move state */
318         if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
319                 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
320                 pr_debug("stream prepared, Houston we are good to go\n");
321         }
322 
323         mutex_unlock(&stream->device->lock);
324         return retval;
325 }
326 
327 
328 static ssize_t snd_compr_read(struct file *f, char __user *buf,
329                 size_t count, loff_t *offset)
330 {
331         struct snd_compr_file *data = f->private_data;
332         struct snd_compr_stream *stream;
333         size_t avail;
334         int retval;
335 
336         if (snd_BUG_ON(!data))
337                 return -EFAULT;
338 
339         stream = &data->stream;
340         mutex_lock(&stream->device->lock);
341 
342         /* read is allowed when stream is running, paused, draining and setup
343          * (yes setup is state which we transition to after stop, so if user
344          * wants to read data after stop we allow that)
345          */
346         switch (stream->runtime->state) {
347         case SNDRV_PCM_STATE_OPEN:
348         case SNDRV_PCM_STATE_PREPARED:
349         case SNDRV_PCM_STATE_XRUN:
350         case SNDRV_PCM_STATE_SUSPENDED:
351         case SNDRV_PCM_STATE_DISCONNECTED:
352                 retval = -EBADFD;
353                 goto out;
354         }
355 
356         avail = snd_compr_get_avail(stream);
357         pr_debug("avail returned %ld\n", (unsigned long)avail);
358         /* calculate how much we can read from buffer */
359         if (avail > count)
360                 avail = count;
361 
362         if (stream->ops->copy) {
363                 retval = stream->ops->copy(stream, buf, avail);
364         } else {
365                 retval = -ENXIO;
366                 goto out;
367         }
368         if (retval > 0)
369                 stream->runtime->total_bytes_transferred += retval;
370 
371 out:
372         mutex_unlock(&stream->device->lock);
373         return retval;
374 }
375 
376 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
377 {
378         return -ENXIO;
379 }
380 
381 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
382 {
383         if (stream->direction == SND_COMPRESS_PLAYBACK)
384                 return POLLOUT | POLLWRNORM;
385         else
386                 return POLLIN | POLLRDNORM;
387 }
388 
389 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
390 {
391         struct snd_compr_file *data = f->private_data;
392         struct snd_compr_stream *stream;
393         size_t avail;
394         int retval = 0;
395 
396         if (snd_BUG_ON(!data))
397                 return POLLERR;
398 
399         stream = &data->stream;
400 
401         mutex_lock(&stream->device->lock);
402         if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
403                 retval = snd_compr_get_poll(stream) | POLLERR;
404                 goto out;
405         }
406         poll_wait(f, &stream->runtime->sleep, wait);
407 
408         avail = snd_compr_get_avail(stream);
409         pr_debug("avail is %ld\n", (unsigned long)avail);
410         /* check if we have at least one fragment to fill */
411         switch (stream->runtime->state) {
412         case SNDRV_PCM_STATE_DRAINING:
413                 /* stream has been woken up after drain is complete
414                  * draining done so set stream state to stopped
415                  */
416                 retval = snd_compr_get_poll(stream);
417                 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
418                 break;
419         case SNDRV_PCM_STATE_RUNNING:
420         case SNDRV_PCM_STATE_PREPARED:
421         case SNDRV_PCM_STATE_PAUSED:
422                 if (avail >= stream->runtime->fragment_size)
423                         retval = snd_compr_get_poll(stream);
424                 break;
425         default:
426                 retval = snd_compr_get_poll(stream) | POLLERR;
427                 break;
428         }
429 out:
430         mutex_unlock(&stream->device->lock);
431         return retval;
432 }
433 
434 static int
435 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
436 {
437         int retval;
438         struct snd_compr_caps caps;
439 
440         if (!stream->ops->get_caps)
441                 return -ENXIO;
442 
443         memset(&caps, 0, sizeof(caps));
444         retval = stream->ops->get_caps(stream, &caps);
445         if (retval)
446                 goto out;
447         if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
448                 retval = -EFAULT;
449 out:
450         return retval;
451 }
452 
453 #ifndef COMPR_CODEC_CAPS_OVERFLOW
454 static int
455 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
456 {
457         int retval;
458         struct snd_compr_codec_caps *caps;
459 
460         if (!stream->ops->get_codec_caps)
461                 return -ENXIO;
462 
463         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
464         if (!caps)
465                 return -ENOMEM;
466 
467         retval = stream->ops->get_codec_caps(stream, caps);
468         if (retval)
469                 goto out;
470         if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
471                 retval = -EFAULT;
472 
473 out:
474         kfree(caps);
475         return retval;
476 }
477 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
478 
479 /* revisit this with snd_pcm_preallocate_xxx */
480 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
481                 struct snd_compr_params *params)
482 {
483         unsigned int buffer_size;
484         void *buffer;
485 
486         buffer_size = params->buffer.fragment_size * params->buffer.fragments;
487         if (stream->ops->copy) {
488                 buffer = NULL;
489                 /* if copy is defined the driver will be required to copy
490                  * the data from core
491                  */
492         } else {
493                 buffer = kmalloc(buffer_size, GFP_KERNEL);
494                 if (!buffer)
495                         return -ENOMEM;
496         }
497         stream->runtime->fragment_size = params->buffer.fragment_size;
498         stream->runtime->fragments = params->buffer.fragments;
499         stream->runtime->buffer = buffer;
500         stream->runtime->buffer_size = buffer_size;
501         return 0;
502 }
503 
504 static int snd_compress_check_input(struct snd_compr_params *params)
505 {
506         /* first let's check the buffer parameter's */
507         if (params->buffer.fragment_size == 0 ||
508             params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
509                 return -EINVAL;
510 
511         /* now codec parameters */
512         if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
513                 return -EINVAL;
514 
515         if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
516                 return -EINVAL;
517 
518         return 0;
519 }
520 
521 static int
522 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
523 {
524         struct snd_compr_params *params;
525         int retval;
526 
527         if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
528                 /*
529                  * we should allow parameter change only when stream has been
530                  * opened not in other cases
531                  */
532                 params = kmalloc(sizeof(*params), GFP_KERNEL);
533                 if (!params)
534                         return -ENOMEM;
535                 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
536                         retval = -EFAULT;
537                         goto out;
538                 }
539 
540                 retval = snd_compress_check_input(params);
541                 if (retval)
542                         goto out;
543 
544                 retval = snd_compr_allocate_buffer(stream, params);
545                 if (retval) {
546                         retval = -ENOMEM;
547                         goto out;
548                 }
549 
550                 retval = stream->ops->set_params(stream, params);
551                 if (retval)
552                         goto out;
553 
554                 stream->metadata_set = false;
555                 stream->next_track = false;
556 
557                 if (stream->direction == SND_COMPRESS_PLAYBACK)
558                         stream->runtime->state = SNDRV_PCM_STATE_SETUP;
559                 else
560                         stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
561         } else {
562                 return -EPERM;
563         }
564 out:
565         kfree(params);
566         return retval;
567 }
568 
569 static int
570 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
571 {
572         struct snd_codec *params;
573         int retval;
574 
575         if (!stream->ops->get_params)
576                 return -EBADFD;
577 
578         params = kzalloc(sizeof(*params), GFP_KERNEL);
579         if (!params)
580                 return -ENOMEM;
581         retval = stream->ops->get_params(stream, params);
582         if (retval)
583                 goto out;
584         if (copy_to_user((char __user *)arg, params, sizeof(*params)))
585                 retval = -EFAULT;
586 
587 out:
588         kfree(params);
589         return retval;
590 }
591 
592 static int
593 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
594 {
595         struct snd_compr_metadata metadata;
596         int retval;
597 
598         if (!stream->ops->get_metadata)
599                 return -ENXIO;
600 
601         if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
602                 return -EFAULT;
603 
604         retval = stream->ops->get_metadata(stream, &metadata);
605         if (retval != 0)
606                 return retval;
607 
608         if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
609                 return -EFAULT;
610 
611         return 0;
612 }
613 
614 static int
615 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
616 {
617         struct snd_compr_metadata metadata;
618         int retval;
619 
620         if (!stream->ops->set_metadata)
621                 return -ENXIO;
622         /*
623         * we should allow parameter change only when stream has been
624         * opened not in other cases
625         */
626         if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
627                 return -EFAULT;
628 
629         retval = stream->ops->set_metadata(stream, &metadata);
630         stream->metadata_set = true;
631 
632         return retval;
633 }
634 
635 static inline int
636 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
637 {
638         struct snd_compr_tstamp tstamp = {0};
639         int ret;
640 
641         ret = snd_compr_update_tstamp(stream, &tstamp);
642         if (ret == 0)
643                 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
644                         &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
645         return ret;
646 }
647 
648 static int snd_compr_pause(struct snd_compr_stream *stream)
649 {
650         int retval;
651 
652         if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
653                 return -EPERM;
654         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
655         if (!retval)
656                 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
657         return retval;
658 }
659 
660 static int snd_compr_resume(struct snd_compr_stream *stream)
661 {
662         int retval;
663 
664         if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
665                 return -EPERM;
666         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
667         if (!retval)
668                 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
669         return retval;
670 }
671 
672 static int snd_compr_start(struct snd_compr_stream *stream)
673 {
674         int retval;
675 
676         if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
677                 return -EPERM;
678         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
679         if (!retval)
680                 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
681         return retval;
682 }
683 
684 static int snd_compr_stop(struct snd_compr_stream *stream)
685 {
686         int retval;
687 
688         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
689                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
690                 return -EPERM;
691         retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
692         if (!retval) {
693                 snd_compr_drain_notify(stream);
694                 stream->runtime->total_bytes_available = 0;
695                 stream->runtime->total_bytes_transferred = 0;
696         }
697         return retval;
698 }
699 
700 static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
701 {
702         int ret;
703 
704         /*
705          * We are called with lock held. So drop the lock while we wait for
706          * drain complete notification from the driver
707          *
708          * It is expected that driver will notify the drain completion and then
709          * stream will be moved to SETUP state, even if draining resulted in an
710          * error. We can trigger next track after this.
711          */
712         stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
713         mutex_unlock(&stream->device->lock);
714 
715         /* we wait for drain to complete here, drain can return when
716          * interruption occurred, wait returned error or success.
717          * For the first two cases we don't do anything different here and
718          * return after waking up
719          */
720 
721         ret = wait_event_interruptible(stream->runtime->sleep,
722                         (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
723         if (ret == -ERESTARTSYS)
724                 pr_debug("wait aborted by a signal");
725         else if (ret)
726                 pr_debug("wait for drain failed with %d\n", ret);
727 
728 
729         wake_up(&stream->runtime->sleep);
730         mutex_lock(&stream->device->lock);
731 
732         return ret;
733 }
734 
735 static int snd_compr_drain(struct snd_compr_stream *stream)
736 {
737         int retval;
738 
739         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
740                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
741                 return -EPERM;
742 
743         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
744         if (retval) {
745                 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
746                 wake_up(&stream->runtime->sleep);
747                 return retval;
748         }
749 
750         return snd_compress_wait_for_drain(stream);
751 }
752 
753 static int snd_compr_next_track(struct snd_compr_stream *stream)
754 {
755         int retval;
756 
757         /* only a running stream can transition to next track */
758         if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
759                 return -EPERM;
760 
761         /* you can signal next track if this is intended to be a gapless stream
762          * and current track metadata is set
763          */
764         if (stream->metadata_set == false)
765                 return -EPERM;
766 
767         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
768         if (retval != 0)
769                 return retval;
770         stream->metadata_set = false;
771         stream->next_track = true;
772         return 0;
773 }
774 
775 static int snd_compr_partial_drain(struct snd_compr_stream *stream)
776 {
777         int retval;
778         if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
779                         stream->runtime->state == SNDRV_PCM_STATE_SETUP)
780                 return -EPERM;
781         /* stream can be drained only when next track has been signalled */
782         if (stream->next_track == false)
783                 return -EPERM;
784 
785         retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
786         if (retval) {
787                 pr_debug("Partial drain returned failure\n");
788                 wake_up(&stream->runtime->sleep);
789                 return retval;
790         }
791 
792         stream->next_track = false;
793         return snd_compress_wait_for_drain(stream);
794 }
795 
796 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
797 {
798         struct snd_compr_file *data = f->private_data;
799         struct snd_compr_stream *stream;
800         int retval = -ENOTTY;
801 
802         if (snd_BUG_ON(!data))
803                 return -EFAULT;
804 
805         stream = &data->stream;
806 
807         mutex_lock(&stream->device->lock);
808         switch (_IOC_NR(cmd)) {
809         case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
810                 retval = put_user(SNDRV_COMPRESS_VERSION,
811                                 (int __user *)arg) ? -EFAULT : 0;
812                 break;
813         case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
814                 retval = snd_compr_get_caps(stream, arg);
815                 break;
816 #ifndef COMPR_CODEC_CAPS_OVERFLOW
817         case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
818                 retval = snd_compr_get_codec_caps(stream, arg);
819                 break;
820 #endif
821         case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
822                 retval = snd_compr_set_params(stream, arg);
823                 break;
824         case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
825                 retval = snd_compr_get_params(stream, arg);
826                 break;
827         case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
828                 retval = snd_compr_set_metadata(stream, arg);
829                 break;
830         case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
831                 retval = snd_compr_get_metadata(stream, arg);
832                 break;
833         case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
834                 retval = snd_compr_tstamp(stream, arg);
835                 break;
836         case _IOC_NR(SNDRV_COMPRESS_AVAIL):
837                 retval = snd_compr_ioctl_avail(stream, arg);
838                 break;
839         case _IOC_NR(SNDRV_COMPRESS_PAUSE):
840                 retval = snd_compr_pause(stream);
841                 break;
842         case _IOC_NR(SNDRV_COMPRESS_RESUME):
843                 retval = snd_compr_resume(stream);
844                 break;
845         case _IOC_NR(SNDRV_COMPRESS_START):
846                 retval = snd_compr_start(stream);
847                 break;
848         case _IOC_NR(SNDRV_COMPRESS_STOP):
849                 retval = snd_compr_stop(stream);
850                 break;
851         case _IOC_NR(SNDRV_COMPRESS_DRAIN):
852                 retval = snd_compr_drain(stream);
853                 break;
854         case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
855                 retval = snd_compr_partial_drain(stream);
856                 break;
857         case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
858                 retval = snd_compr_next_track(stream);
859                 break;
860 
861         }
862         mutex_unlock(&stream->device->lock);
863         return retval;
864 }
865 
866 /* support of 32bit userspace on 64bit platforms */
867 #ifdef CONFIG_COMPAT
868 static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
869                                                 unsigned long arg)
870 {
871         return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
872 }
873 #endif
874 
875 static const struct file_operations snd_compr_file_ops = {
876                 .owner =        THIS_MODULE,
877                 .open =         snd_compr_open,
878                 .release =      snd_compr_free,
879                 .write =        snd_compr_write,
880                 .read =         snd_compr_read,
881                 .unlocked_ioctl = snd_compr_ioctl,
882 #ifdef CONFIG_COMPAT
883                 .compat_ioctl = snd_compr_ioctl_compat,
884 #endif
885                 .mmap =         snd_compr_mmap,
886                 .poll =         snd_compr_poll,
887 };
888 
889 static int snd_compress_dev_register(struct snd_device *device)
890 {
891         int ret = -EINVAL;
892         char str[16];
893         struct snd_compr *compr;
894 
895         if (snd_BUG_ON(!device || !device->device_data))
896                 return -EBADFD;
897         compr = device->device_data;
898 
899         pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
900                         compr->direction);
901         /* register compressed device */
902         ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
903                                   compr->card, compr->device,
904                                   &snd_compr_file_ops, compr, &compr->dev);
905         if (ret < 0) {
906                 pr_err("snd_register_device failed\n %d", ret);
907                 return ret;
908         }
909         return ret;
910 
911 }
912 
913 static int snd_compress_dev_disconnect(struct snd_device *device)
914 {
915         struct snd_compr *compr;
916 
917         compr = device->device_data;
918         snd_unregister_device(&compr->dev);
919         return 0;
920 }
921 
922 #ifdef CONFIG_SND_VERBOSE_PROCFS
923 static void snd_compress_proc_info_read(struct snd_info_entry *entry,
924                                         struct snd_info_buffer *buffer)
925 {
926         struct snd_compr *compr = (struct snd_compr *)entry->private_data;
927 
928         snd_iprintf(buffer, "card: %d\n", compr->card->number);
929         snd_iprintf(buffer, "device: %d\n", compr->device);
930         snd_iprintf(buffer, "stream: %s\n",
931                         compr->direction == SND_COMPRESS_PLAYBACK
932                                 ? "PLAYBACK" : "CAPTURE");
933         snd_iprintf(buffer, "id: %s\n", compr->id);
934 }
935 
936 static int snd_compress_proc_init(struct snd_compr *compr)
937 {
938         struct snd_info_entry *entry;
939         char name[16];
940 
941         sprintf(name, "compr%i", compr->device);
942         entry = snd_info_create_card_entry(compr->card, name,
943                                            compr->card->proc_root);
944         if (!entry)
945                 return -ENOMEM;
946         entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
947         if (snd_info_register(entry) < 0) {
948                 snd_info_free_entry(entry);
949                 return -ENOMEM;
950         }
951         compr->proc_root = entry;
952 
953         entry = snd_info_create_card_entry(compr->card, "info",
954                                            compr->proc_root);
955         if (entry) {
956                 snd_info_set_text_ops(entry, compr,
957                                       snd_compress_proc_info_read);
958                 if (snd_info_register(entry) < 0) {
959                         snd_info_free_entry(entry);
960                         entry = NULL;
961                 }
962         }
963         compr->proc_info_entry = entry;
964 
965         return 0;
966 }
967 
968 static void snd_compress_proc_done(struct snd_compr *compr)
969 {
970         snd_info_free_entry(compr->proc_info_entry);
971         compr->proc_info_entry = NULL;
972         snd_info_free_entry(compr->proc_root);
973         compr->proc_root = NULL;
974 }
975 
976 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
977 {
978         strlcpy(compr->id, id, sizeof(compr->id));
979 }
980 #else
981 static inline int snd_compress_proc_init(struct snd_compr *compr)
982 {
983         return 0;
984 }
985 
986 static inline void snd_compress_proc_done(struct snd_compr *compr)
987 {
988 }
989 
990 static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
991 {
992 }
993 #endif
994 
995 static int snd_compress_dev_free(struct snd_device *device)
996 {
997         struct snd_compr *compr;
998 
999         compr = device->device_data;
1000         snd_compress_proc_done(compr);
1001         put_device(&compr->dev);
1002         return 0;
1003 }
1004 
1005 /*
1006  * snd_compress_new: create new compress device
1007  * @card: sound card pointer
1008  * @device: device number
1009  * @dirn: device direction, should be of type enum snd_compr_direction
1010  * @compr: compress device pointer
1011  */
1012 int snd_compress_new(struct snd_card *card, int device,
1013                         int dirn, const char *id, struct snd_compr *compr)
1014 {
1015         static struct snd_device_ops ops = {
1016                 .dev_free = snd_compress_dev_free,
1017                 .dev_register = snd_compress_dev_register,
1018                 .dev_disconnect = snd_compress_dev_disconnect,
1019         };
1020         int ret;
1021 
1022         compr->card = card;
1023         compr->device = device;
1024         compr->direction = dirn;
1025 
1026         snd_compress_set_id(compr, id);
1027 
1028         snd_device_initialize(&compr->dev, card);
1029         dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
1030 
1031         ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
1032         if (ret == 0)
1033                 snd_compress_proc_init(compr);
1034 
1035         return ret;
1036 }
1037 EXPORT_SYMBOL_GPL(snd_compress_new);
1038 
1039 static int snd_compress_add_device(struct snd_compr *device)
1040 {
1041         int ret;
1042 
1043         if (!device->card)
1044                 return -EINVAL;
1045 
1046         /* register the card */
1047         ret = snd_card_register(device->card);
1048         if (ret)
1049                 goto out;
1050         return 0;
1051 
1052 out:
1053         pr_err("failed with %d\n", ret);
1054         return ret;
1055 
1056 }
1057 
1058 static int snd_compress_remove_device(struct snd_compr *device)
1059 {
1060         return snd_card_free(device->card);
1061 }
1062 
1063 /**
1064  * snd_compress_register - register compressed device
1065  *
1066  * @device: compressed device to register
1067  */
1068 int snd_compress_register(struct snd_compr *device)
1069 {
1070         int retval;
1071 
1072         if (device->name == NULL || device->ops == NULL)
1073                 return -EINVAL;
1074 
1075         pr_debug("Registering compressed device %s\n", device->name);
1076         if (snd_BUG_ON(!device->ops->open))
1077                 return -EINVAL;
1078         if (snd_BUG_ON(!device->ops->free))
1079                 return -EINVAL;
1080         if (snd_BUG_ON(!device->ops->set_params))
1081                 return -EINVAL;
1082         if (snd_BUG_ON(!device->ops->trigger))
1083                 return -EINVAL;
1084 
1085         mutex_init(&device->lock);
1086 
1087         /* register a compressed card */
1088         mutex_lock(&device_mutex);
1089         retval = snd_compress_add_device(device);
1090         mutex_unlock(&device_mutex);
1091         return retval;
1092 }
1093 EXPORT_SYMBOL_GPL(snd_compress_register);
1094 
1095 int snd_compress_deregister(struct snd_compr *device)
1096 {
1097         pr_debug("Removing compressed device %s\n", device->name);
1098         mutex_lock(&device_mutex);
1099         snd_compress_remove_device(device);
1100         mutex_unlock(&device_mutex);
1101         return 0;
1102 }
1103 EXPORT_SYMBOL_GPL(snd_compress_deregister);
1104 
1105 static int __init snd_compress_init(void)
1106 {
1107         return 0;
1108 }
1109 
1110 static void __exit snd_compress_exit(void)
1111 {
1112 }
1113 
1114 module_init(snd_compress_init);
1115 module_exit(snd_compress_exit);
1116 
1117 MODULE_DESCRIPTION("ALSA Compressed offload framework");
1118 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1119 MODULE_LICENSE("GPL v2");
1120 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us