Merge tag 'mmc-v4.7-rc1' of git://git.linaro.org/people/ulf.hansson/mmc
[linux-2.6-block.git] / sound / core / compress_offload.c
CommitLineData
b21c60a4
VK
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>
f0283b58 31#include <linux/math64.h>
b21c60a4
VK
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>
f0283b58 37#include <linux/types.h>
b21c60a4
VK
38#include <linux/uio.h>
39#include <linux/uaccess.h>
40#include <linux/module.h>
c1036889 41#include <linux/compat.h>
b21c60a4
VK
42#include <sound/core.h>
43#include <sound/initval.h>
31742724 44#include <sound/info.h>
b21c60a4
VK
45#include <sound/compress_params.h>
46#include <sound/compress_offload.h>
47#include <sound/compress_driver.h>
48
462b3f16
TI
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
b21c60a4
VK
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
63static DEFINE_MUTEX(device_mutex);
64
65struct snd_compr_file {
66 unsigned long caps;
67 struct snd_compr_stream stream;
68};
69
70/*
71 * a note on stream states used:
41eb94fd 72 * we use following states in the compressed core
b21c60a4
VK
73 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
74 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
41eb94fd 75 * calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
b21c60a4 76 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
862bca5d
VK
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.
b21c60a4
VK
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 */
88static 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
81cb3246 97 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
b21c60a4 98 dirn = SND_COMPRESS_PLAYBACK;
81cb3246 99 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
b21c60a4 100 dirn = SND_COMPRESS_CAPTURE;
81cb3246 101 else
b21c60a4 102 return -EINVAL;
b21c60a4
VK
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");
a0830dbd 117 snd_card_unref(compr->card);
b21c60a4
VK
118 return -EINVAL;
119 }
120
121 data = kzalloc(sizeof(*data), GFP_KERNEL);
a0830dbd
TI
122 if (!data) {
123 snd_card_unref(compr->card);
b21c60a4 124 return -ENOMEM;
a0830dbd 125 }
b21c60a4
VK
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);
a0830dbd 133 snd_card_unref(compr->card);
b21c60a4
VK
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 }
a0830dbd 147 snd_card_unref(compr->card);
749d3223 148 return ret;
b21c60a4
VK
149}
150
151static int snd_compr_free(struct inode *inode, struct file *f)
152{
153 struct snd_compr_file *data = f->private_data;
b26d19e4
LG
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
b21c60a4
VK
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
17ac8e5c 173static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
b21c60a4
VK
174 struct snd_compr_tstamp *tstamp)
175{
176 if (!stream->ops->pointer)
17ac8e5c 177 return -ENOTSUPP;
b21c60a4
VK
178 stream->ops->pointer(stream, tstamp);
179 pr_debug("dsp consumed till %d total %d bytes\n",
180 tstamp->byte_offset, tstamp->copied_total);
5b1f79f7
CK
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;
17ac8e5c 185 return 0;
b21c60a4
VK
186}
187
188static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
189 struct snd_compr_avail *avail)
190{
17ac8e5c 191 memset(avail, 0, sizeof(*avail));
b21c60a4 192 snd_compr_update_tstamp(stream, &avail->tstamp);
17ac8e5c 193 /* Still need to return avail even if tstamp can't be filled in */
b21c60a4 194
b21c60a4 195 if (stream->runtime->total_bytes_available == 0 &&
5b1f79f7
CK
196 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
197 stream->direction == SND_COMPRESS_PLAYBACK) {
b21c60a4
VK
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) {
5b1f79f7
CK
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 }
b21c60a4
VK
213 }
214
5b1f79f7
CK
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
4c28e32d
CK
220 pr_debug("ret avail as %lld\n", avail->avail);
221 return avail->avail;
b21c60a4
VK
222}
223
224static 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
231static int
232snd_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
246static 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;
f0283b58
CK
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);
b21c60a4 257
f0283b58 258 dstn = runtime->buffer + app_pointer;
b21c60a4 259 pr_debug("copying %ld at %lld\n",
f0283b58
CK
260 (unsigned long)count, app_pointer);
261 if (count < runtime->buffer_size - app_pointer) {
b21c60a4
VK
262 if (copy_from_user(dstn, buf, count))
263 return -EFAULT;
b21c60a4 264 } else {
f0283b58 265 copy = runtime->buffer_size - app_pointer;
b21c60a4
VK
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;
b21c60a4
VK
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
277static 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 */
875f6fff
CK
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:
b21c60a4
VK
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
4daf891c
CK
307 if (stream->ops->copy) {
308 char __user* cbuf = (char __user*)buf;
309 retval = stream->ops->copy(stream, cbuf, avail);
310 } else {
b21c60a4 311 retval = snd_compr_write_data(stream, buf, avail);
4daf891c 312 }
b21c60a4
VK
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
328static ssize_t snd_compr_read(struct file *f, char __user *buf,
329 size_t count, loff_t *offset)
330{
49bb6402
CK
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
75481347
VK
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:
49bb6402
CK
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
371out:
372 mutex_unlock(&stream->device->lock);
373 return retval;
b21c60a4
VK
374}
375
376static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
377{
378 return -ENXIO;
379}
380
381static 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
389static 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))
1d03f2bd 397 return POLLERR;
5bd05390 398
b21c60a4 399 stream = &data->stream;
b21c60a4
VK
400
401 mutex_lock(&stream->device->lock);
c15b149a 402 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
1d03f2bd 403 retval = snd_compr_get_poll(stream) | POLLERR;
b21c60a4
VK
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:
0b92b0cd 426 retval = snd_compr_get_poll(stream) | POLLERR;
b21c60a4
VK
427 break;
428 }
429out:
430 mutex_unlock(&stream->device->lock);
431 return retval;
432}
433
434static int
435snd_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
1c62e9f2 443 memset(&caps, 0, sizeof(caps));
b21c60a4
VK
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;
449out:
450 return retval;
451}
452
462b3f16 453#ifndef COMPR_CODEC_CAPS_OVERFLOW
b21c60a4
VK
454static int
455snd_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
47966e97 463 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
b21c60a4
VK
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
473out:
474 kfree(caps);
475 return retval;
476}
462b3f16 477#endif /* !COMPR_CODEC_CAPS_OVERFLOW */
b21c60a4
VK
478
479/* revisit this with snd_pcm_preallocate_xxx */
480static 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
4dc040a0
VK
504static 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 ||
6217e5ed 508 params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
4dc040a0
VK
509 return -EINVAL;
510
fb4a9779
VK
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;
fb4a9779 517
4dc040a0
VK
518 return 0;
519}
520
b21c60a4
VK
521static int
522snd_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;
769fab2a
JJ
535 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
536 retval = -EFAULT;
537 goto out;
538 }
4dc040a0
VK
539
540 retval = snd_compress_check_input(params);
541 if (retval)
542 goto out;
543
b21c60a4
VK
544 retval = snd_compr_allocate_buffer(stream, params);
545 if (retval) {
769fab2a
JJ
546 retval = -ENOMEM;
547 goto out;
b21c60a4 548 }
4dc040a0 549
b21c60a4
VK
550 retval = stream->ops->set_params(stream, params);
551 if (retval)
552 goto out;
49bb6402 553
9727b490
JK
554 stream->metadata_set = false;
555 stream->next_track = false;
49bb6402
CK
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;
769fab2a 561 } else {
b21c60a4 562 return -EPERM;
769fab2a 563 }
b21c60a4
VK
564out:
565 kfree(params);
566 return retval;
567}
568
569static int
570snd_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
47966e97 578 params = kzalloc(sizeof(*params), GFP_KERNEL);
b21c60a4
VK
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
587out:
588 kfree(params);
589 return retval;
590}
591
9727b490
JK
592static int
593snd_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
614static int
615snd_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
b21c60a4
VK
635static inline int
636snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
637{
17ac8e5c
RF
638 struct snd_compr_tstamp tstamp = {0};
639 int ret;
b21c60a4 640
17ac8e5c
RF
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;
b21c60a4
VK
646}
647
648static 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);
6b18f793 655 if (!retval)
b21c60a4 656 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
b21c60a4
VK
657 return retval;
658}
659
660static 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
672static 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
684static 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) {
917f4b5c 693 snd_compr_drain_notify(stream);
8b21460a
VK
694 stream->runtime->total_bytes_available = 0;
695 stream->runtime->total_bytes_transferred = 0;
b21c60a4
VK
696 }
697 return retval;
698}
699
917f4b5c
VK
700static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
701{
f44f2a54
VK
702 int ret;
703
917f4b5c
VK
704 /*
705 * We are called with lock held. So drop the lock while we wait for
cdb1ee3f 706 * drain complete notification from the driver
917f4b5c
VK
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
f44f2a54
VK
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
917f4b5c
VK
728
729 wake_up(&stream->runtime->sleep);
730 mutex_lock(&stream->device->lock);
731
f44f2a54 732 return ret;
917f4b5c
VK
733}
734
b21c60a4
VK
735static 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;
917f4b5c 742
b21c60a4 743 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
917f4b5c 744 if (retval) {
f44f2a54 745 pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
b21c60a4 746 wake_up(&stream->runtime->sleep);
917f4b5c 747 return retval;
b21c60a4 748 }
917f4b5c 749
f44f2a54 750 return snd_compress_wait_for_drain(stream);
b21c60a4
VK
751}
752
9727b490
JK
753static 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
cdb1ee3f 761 /* you can signal next track if this is intended to be a gapless stream
9727b490
JK
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
775static 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);
917f4b5c 786 if (retval) {
f44f2a54 787 pr_debug("Partial drain returned failure\n");
917f4b5c
VK
788 wake_up(&stream->runtime->sleep);
789 return retval;
790 }
9727b490
JK
791
792 stream->next_track = false;
917f4b5c 793 return snd_compress_wait_for_drain(stream);
9727b490
JK
794}
795
b21c60a4
VK
796static 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;
5bd05390 804
b21c60a4 805 stream = &data->stream;
5bd05390 806
b21c60a4
VK
807 mutex_lock(&stream->device->lock);
808 switch (_IOC_NR(cmd)) {
809 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
a8d30608 810 retval = put_user(SNDRV_COMPRESS_VERSION,
b21c60a4
VK
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;
462b3f16 816#ifndef COMPR_CODEC_CAPS_OVERFLOW
b21c60a4
VK
817 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
818 retval = snd_compr_get_codec_caps(stream, arg);
819 break;
462b3f16 820#endif
b21c60a4
VK
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;
9727b490
JK
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;
b21c60a4
VK
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;
9727b490
JK
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
b21c60a4
VK
861 }
862 mutex_unlock(&stream->device->lock);
863 return retval;
864}
865
c1036889
RL
866/* support of 32bit userspace on 64bit platforms */
867#ifdef CONFIG_COMPAT
868static 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
b21c60a4
VK
875static 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,
c1036889
RL
882#ifdef CONFIG_COMPAT
883 .compat_ioctl = snd_compr_ioctl_compat,
884#endif
b21c60a4
VK
885 .mmap = snd_compr_mmap,
886 .poll = snd_compr_poll,
887};
888
889static 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
b21c60a4
VK
899 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
900 compr->direction);
901 /* register compressed device */
40a4b263
TI
902 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
903 compr->card, compr->device,
904 &snd_compr_file_ops, compr, &compr->dev);
b21c60a4
VK
905 if (ret < 0) {
906 pr_err("snd_register_device failed\n %d", ret);
907 return ret;
908 }
909 return ret;
910
911}
912
913static int snd_compress_dev_disconnect(struct snd_device *device)
914{
915 struct snd_compr *compr;
916
917 compr = device->device_data;
40a4b263 918 snd_unregister_device(&compr->dev);
b21c60a4
VK
919 return 0;
920}
921
31742724
RF
922#ifdef CONFIG_SND_VERBOSE_PROCFS
923static 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
936static 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
968static 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}
e5241a8c
RF
975
976static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
977{
978 strlcpy(compr->id, id, sizeof(compr->id));
979}
31742724
RF
980#else
981static inline int snd_compress_proc_init(struct snd_compr *compr)
982{
983 return 0;
984}
985
986static inline void snd_compress_proc_done(struct snd_compr *compr)
987{
988}
e5241a8c
RF
989
990static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
991{
992}
31742724
RF
993#endif
994
04c5d5a4
TI
995static int snd_compress_dev_free(struct snd_device *device)
996{
997 struct snd_compr *compr;
998
999 compr = device->device_data;
31742724 1000 snd_compress_proc_done(compr);
04c5d5a4
TI
1001 put_device(&compr->dev);
1002 return 0;
1003}
1004
b21c60a4
VK
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 */
1012int snd_compress_new(struct snd_card *card, int device,
e5241a8c 1013 int dirn, const char *id, struct snd_compr *compr)
b21c60a4
VK
1014{
1015 static struct snd_device_ops ops = {
04c5d5a4 1016 .dev_free = snd_compress_dev_free,
b21c60a4
VK
1017 .dev_register = snd_compress_dev_register,
1018 .dev_disconnect = snd_compress_dev_disconnect,
1019 };
31742724 1020 int ret;
b21c60a4
VK
1021
1022 compr->card = card;
1023 compr->device = device;
1024 compr->direction = dirn;
04c5d5a4 1025
e5241a8c
RF
1026 snd_compress_set_id(compr, id);
1027
04c5d5a4
TI
1028 snd_device_initialize(&compr->dev, card);
1029 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
1030
31742724
RF
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;
b21c60a4
VK
1036}
1037EXPORT_SYMBOL_GPL(snd_compress_new);
1038
1039static 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
1052out:
1053 pr_err("failed with %d\n", ret);
1054 return ret;
1055
1056}
1057
1058static 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 */
1068int snd_compress_register(struct snd_compr *device)
1069{
1070 int retval;
1071
04c5d5a4 1072 if (device->name == NULL || device->ops == NULL)
b21c60a4
VK
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}
1093EXPORT_SYMBOL_GPL(snd_compress_register);
1094
1095int 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}
1103EXPORT_SYMBOL_GPL(snd_compress_deregister);
1104
1105static int __init snd_compress_init(void)
1106{
1107 return 0;
1108}
1109
1110static void __exit snd_compress_exit(void)
1111{
1112}
1113
1114module_init(snd_compress_init);
1115module_exit(snd_compress_exit);
1116
1117MODULE_DESCRIPTION("ALSA Compressed offload framework");
1118MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1119MODULE_LICENSE("GPL v2");