blob: 52ca4cce1462551b6c0649934dd21113d4ea31bd [file] [log] [blame]
Vinod Koulb21c60a2011-12-23 10:36:39 +05301/*
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
52static DEFINE_MUTEX(device_mutex);
53
54struct 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 */
74static 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
Dan Carpenter81cb3242012-09-11 14:12:43 +030083 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +053084 dirn = SND_COMPRESS_PLAYBACK;
Dan Carpenter81cb3242012-09-11 14:12:43 +030085 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +053086 dirn = SND_COMPRESS_CAPTURE;
Dan Carpenter81cb3242012-09-11 14:12:43 +030087 else
Vinod Koulb21c60a2011-12-23 10:36:39 +053088 return -EINVAL;
Vinod Koulb21c60a2011-12-23 10:36:39 +053089
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");
Takashi Iwaia0830db2012-10-16 13:05:59 +0200103 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530104 return -EINVAL;
105 }
106
107 data = kzalloc(sizeof(*data), GFP_KERNEL);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200108 if (!data) {
109 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530110 return -ENOMEM;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200111 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530112 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);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200119 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530120 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 }
Takashi Iwaia0830db2012-10-16 13:05:59 +0200133 snd_card_unref(compr->card);
134 return 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530135}
136
137static 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
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000147static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
Vinod Koulb21c60a2011-12-23 10:36:39 +0530148 struct snd_compr_tstamp *tstamp)
149{
150 if (!stream->ops->pointer)
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000151 return -ENOTSUPP;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530152 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;
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100156 if (stream->direction == SND_COMPRESS_PLAYBACK)
157 stream->runtime->total_bytes_transferred = tstamp->copied_total;
158 else
159 stream->runtime->total_bytes_available = tstamp->copied_total;
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000160 return 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530161}
162
163static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
164 struct snd_compr_avail *avail)
165{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000166 memset(avail, 0, sizeof(*avail));
Vinod Koulb21c60a2011-12-23 10:36:39 +0530167 snd_compr_update_tstamp(stream, &avail->tstamp);
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000168 /* Still need to return avail even if tstamp can't be filled in */
Vinod Koulb21c60a2011-12-23 10:36:39 +0530169
Vinod Koulb21c60a2011-12-23 10:36:39 +0530170 if (stream->runtime->total_bytes_available == 0 &&
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100171 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
172 stream->direction == SND_COMPRESS_PLAYBACK) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530173 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) {
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100181 if (stream->direction == SND_COMPRESS_PLAYBACK) {
182 pr_debug("both pointers are same, returning full avail\n");
183 return stream->runtime->buffer_size;
184 } else {
185 pr_debug("both pointers are same, returning no avail\n");
186 return 0;
187 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530188 }
189
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100190 avail->avail = stream->runtime->total_bytes_available -
191 stream->runtime->total_bytes_transferred;
192 if (stream->direction == SND_COMPRESS_PLAYBACK)
193 avail->avail = stream->runtime->buffer_size - avail->avail;
194
Charles Keepax4c28e322013-04-18 10:59:23 +0100195 pr_debug("ret avail as %lld\n", avail->avail);
196 return avail->avail;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530197}
198
199static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
200{
201 struct snd_compr_avail avail;
202
203 return snd_compr_calc_avail(stream, &avail);
204}
205
206static int
207snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
208{
209 struct snd_compr_avail ioctl_avail;
210 size_t avail;
211
212 avail = snd_compr_calc_avail(stream, &ioctl_avail);
213 ioctl_avail.avail = avail;
214
215 if (copy_to_user((__u64 __user *)arg,
216 &ioctl_avail, sizeof(ioctl_avail)))
217 return -EFAULT;
218 return 0;
219}
220
221static int snd_compr_write_data(struct snd_compr_stream *stream,
222 const char __user *buf, size_t count)
223{
224 void *dstn;
225 size_t copy;
226 struct snd_compr_runtime *runtime = stream->runtime;
227
228 dstn = runtime->buffer + runtime->app_pointer;
229 pr_debug("copying %ld at %lld\n",
230 (unsigned long)count, runtime->app_pointer);
231 if (count < runtime->buffer_size - runtime->app_pointer) {
232 if (copy_from_user(dstn, buf, count))
233 return -EFAULT;
234 runtime->app_pointer += count;
235 } else {
236 copy = runtime->buffer_size - runtime->app_pointer;
237 if (copy_from_user(dstn, buf, copy))
238 return -EFAULT;
239 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
240 return -EFAULT;
241 runtime->app_pointer = count - copy;
242 }
243 /* if DSP cares, let it know data has been written */
244 if (stream->ops->ack)
245 stream->ops->ack(stream, count);
246 return count;
247}
248
249static ssize_t snd_compr_write(struct file *f, const char __user *buf,
250 size_t count, loff_t *offset)
251{
252 struct snd_compr_file *data = f->private_data;
253 struct snd_compr_stream *stream;
254 size_t avail;
255 int retval;
256
257 if (snd_BUG_ON(!data))
258 return -EFAULT;
259
260 stream = &data->stream;
261 mutex_lock(&stream->device->lock);
262 /* write is allowed when stream is running or has been steup */
263 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
264 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
265 mutex_unlock(&stream->device->lock);
266 return -EBADFD;
267 }
268
269 avail = snd_compr_get_avail(stream);
270 pr_debug("avail returned %ld\n", (unsigned long)avail);
271 /* calculate how much we can write to buffer */
272 if (avail > count)
273 avail = count;
274
Charles Keepax4daf8912013-04-18 11:01:38 +0100275 if (stream->ops->copy) {
276 char __user* cbuf = (char __user*)buf;
277 retval = stream->ops->copy(stream, cbuf, avail);
278 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530279 retval = snd_compr_write_data(stream, buf, avail);
Charles Keepax4daf8912013-04-18 11:01:38 +0100280 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530281 if (retval > 0)
282 stream->runtime->total_bytes_available += retval;
283
284 /* while initiating the stream, write should be called before START
285 * call, so in setup move state */
286 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
287 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
288 pr_debug("stream prepared, Houston we are good to go\n");
289 }
290
291 mutex_unlock(&stream->device->lock);
292 return retval;
293}
294
295
296static ssize_t snd_compr_read(struct file *f, char __user *buf,
297 size_t count, loff_t *offset)
298{
299 return -ENXIO;
300}
301
302static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
303{
304 return -ENXIO;
305}
306
307static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
308{
309 if (stream->direction == SND_COMPRESS_PLAYBACK)
310 return POLLOUT | POLLWRNORM;
311 else
312 return POLLIN | POLLRDNORM;
313}
314
315static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
316{
317 struct snd_compr_file *data = f->private_data;
318 struct snd_compr_stream *stream;
319 size_t avail;
320 int retval = 0;
321
322 if (snd_BUG_ON(!data))
323 return -EFAULT;
324 stream = &data->stream;
325 if (snd_BUG_ON(!stream))
326 return -EFAULT;
327
328 mutex_lock(&stream->device->lock);
329 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
330 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
331 retval = -EBADFD;
332 goto out;
333 }
334 poll_wait(f, &stream->runtime->sleep, wait);
335
336 avail = snd_compr_get_avail(stream);
337 pr_debug("avail is %ld\n", (unsigned long)avail);
338 /* check if we have at least one fragment to fill */
339 switch (stream->runtime->state) {
340 case SNDRV_PCM_STATE_DRAINING:
341 /* stream has been woken up after drain is complete
342 * draining done so set stream state to stopped
343 */
344 retval = snd_compr_get_poll(stream);
345 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
346 break;
347 case SNDRV_PCM_STATE_RUNNING:
348 case SNDRV_PCM_STATE_PREPARED:
349 case SNDRV_PCM_STATE_PAUSED:
350 if (avail >= stream->runtime->fragment_size)
351 retval = snd_compr_get_poll(stream);
352 break;
353 default:
354 if (stream->direction == SND_COMPRESS_PLAYBACK)
355 retval = POLLOUT | POLLWRNORM | POLLERR;
356 else
357 retval = POLLIN | POLLRDNORM | POLLERR;
358 break;
359 }
360out:
361 mutex_unlock(&stream->device->lock);
362 return retval;
363}
364
365static int
366snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
367{
368 int retval;
369 struct snd_compr_caps caps;
370
371 if (!stream->ops->get_caps)
372 return -ENXIO;
373
374 retval = stream->ops->get_caps(stream, &caps);
375 if (retval)
376 goto out;
377 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
378 retval = -EFAULT;
379out:
380 return retval;
381}
382
383static int
384snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
385{
386 int retval;
387 struct snd_compr_codec_caps *caps;
388
389 if (!stream->ops->get_codec_caps)
390 return -ENXIO;
391
392 caps = kmalloc(sizeof(*caps), GFP_KERNEL);
393 if (!caps)
394 return -ENOMEM;
395
396 retval = stream->ops->get_codec_caps(stream, caps);
397 if (retval)
398 goto out;
399 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
400 retval = -EFAULT;
401
402out:
403 kfree(caps);
404 return retval;
405}
406
407/* revisit this with snd_pcm_preallocate_xxx */
408static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
409 struct snd_compr_params *params)
410{
411 unsigned int buffer_size;
412 void *buffer;
413
414 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
415 if (stream->ops->copy) {
416 buffer = NULL;
417 /* if copy is defined the driver will be required to copy
418 * the data from core
419 */
420 } else {
421 buffer = kmalloc(buffer_size, GFP_KERNEL);
422 if (!buffer)
423 return -ENOMEM;
424 }
425 stream->runtime->fragment_size = params->buffer.fragment_size;
426 stream->runtime->fragments = params->buffer.fragments;
427 stream->runtime->buffer = buffer;
428 stream->runtime->buffer_size = buffer_size;
429 return 0;
430}
431
Vinod Koul4dc040a2012-09-17 11:51:25 +0530432static int snd_compress_check_input(struct snd_compr_params *params)
433{
434 /* first let's check the buffer parameter's */
435 if (params->buffer.fragment_size == 0 ||
436 params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
437 return -EINVAL;
438
Vinod Koulfb4a9772012-09-17 11:51:26 +0530439 /* now codec parameters */
440 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
441 return -EINVAL;
442
443 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
444 return -EINVAL;
445
446 if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
447 return -EINVAL;
448
Vinod Koul4dc040a2012-09-17 11:51:25 +0530449 return 0;
450}
451
Vinod Koulb21c60a2011-12-23 10:36:39 +0530452static int
453snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
454{
455 struct snd_compr_params *params;
456 int retval;
457
458 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
459 /*
460 * we should allow parameter change only when stream has been
461 * opened not in other cases
462 */
463 params = kmalloc(sizeof(*params), GFP_KERNEL);
464 if (!params)
465 return -ENOMEM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100466 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
467 retval = -EFAULT;
468 goto out;
469 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530470
471 retval = snd_compress_check_input(params);
472 if (retval)
473 goto out;
474
Vinod Koulb21c60a2011-12-23 10:36:39 +0530475 retval = snd_compr_allocate_buffer(stream, params);
476 if (retval) {
Jesper Juhl769fab22012-01-23 21:02:57 +0100477 retval = -ENOMEM;
478 goto out;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530479 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530480
Vinod Koulb21c60a2011-12-23 10:36:39 +0530481 retval = stream->ops->set_params(stream, params);
482 if (retval)
483 goto out;
484 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
Jeeja KP9727b492013-02-14 16:52:51 +0530485 stream->metadata_set = false;
486 stream->next_track = false;
Jesper Juhl769fab22012-01-23 21:02:57 +0100487 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530488 return -EPERM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100489 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530490out:
491 kfree(params);
492 return retval;
493}
494
495static int
496snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
497{
498 struct snd_codec *params;
499 int retval;
500
501 if (!stream->ops->get_params)
502 return -EBADFD;
503
504 params = kmalloc(sizeof(*params), GFP_KERNEL);
505 if (!params)
506 return -ENOMEM;
507 retval = stream->ops->get_params(stream, params);
508 if (retval)
509 goto out;
510 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
511 retval = -EFAULT;
512
513out:
514 kfree(params);
515 return retval;
516}
517
Jeeja KP9727b492013-02-14 16:52:51 +0530518static int
519snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
520{
521 struct snd_compr_metadata metadata;
522 int retval;
523
524 if (!stream->ops->get_metadata)
525 return -ENXIO;
526
527 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
528 return -EFAULT;
529
530 retval = stream->ops->get_metadata(stream, &metadata);
531 if (retval != 0)
532 return retval;
533
534 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
535 return -EFAULT;
536
537 return 0;
538}
539
540static int
541snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
542{
543 struct snd_compr_metadata metadata;
544 int retval;
545
546 if (!stream->ops->set_metadata)
547 return -ENXIO;
548 /*
549 * we should allow parameter change only when stream has been
550 * opened not in other cases
551 */
552 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
553 return -EFAULT;
554
555 retval = stream->ops->set_metadata(stream, &metadata);
556 stream->metadata_set = true;
557
558 return retval;
559}
560
Vinod Koulb21c60a2011-12-23 10:36:39 +0530561static inline int
562snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
563{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000564 struct snd_compr_tstamp tstamp = {0};
565 int ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530566
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000567 ret = snd_compr_update_tstamp(stream, &tstamp);
568 if (ret == 0)
569 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
570 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
571 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530572}
573
574static int snd_compr_pause(struct snd_compr_stream *stream)
575{
576 int retval;
577
578 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
579 return -EPERM;
580 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
Vinod Koul6b18f792012-06-12 16:16:17 +0530581 if (!retval)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530582 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530583 return retval;
584}
585
586static int snd_compr_resume(struct snd_compr_stream *stream)
587{
588 int retval;
589
590 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
591 return -EPERM;
592 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
593 if (!retval)
594 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
595 return retval;
596}
597
598static int snd_compr_start(struct snd_compr_stream *stream)
599{
600 int retval;
601
602 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
603 return -EPERM;
604 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
605 if (!retval)
606 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
607 return retval;
608}
609
610static int snd_compr_stop(struct snd_compr_stream *stream)
611{
612 int retval;
613
614 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
615 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
616 return -EPERM;
617 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
618 if (!retval) {
619 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
620 wake_up(&stream->runtime->sleep);
Vinod Koul8b214602012-06-12 16:16:18 +0530621 stream->runtime->hw_pointer = 0;
622 stream->runtime->app_pointer = 0;
623 stream->runtime->total_bytes_available = 0;
624 stream->runtime->total_bytes_transferred = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530625 }
626 return retval;
627}
628
629static int snd_compr_drain(struct snd_compr_stream *stream)
630{
631 int retval;
632
633 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
634 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
635 return -EPERM;
636 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
637 if (!retval) {
638 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
639 wake_up(&stream->runtime->sleep);
640 }
641 return retval;
642}
643
Jeeja KP9727b492013-02-14 16:52:51 +0530644static int snd_compr_next_track(struct snd_compr_stream *stream)
645{
646 int retval;
647
648 /* only a running stream can transition to next track */
649 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
650 return -EPERM;
651
652 /* you can signal next track isf this is intended to be a gapless stream
653 * and current track metadata is set
654 */
655 if (stream->metadata_set == false)
656 return -EPERM;
657
658 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
659 if (retval != 0)
660 return retval;
661 stream->metadata_set = false;
662 stream->next_track = true;
663 return 0;
664}
665
666static int snd_compr_partial_drain(struct snd_compr_stream *stream)
667{
668 int retval;
669 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
670 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
671 return -EPERM;
672 /* stream can be drained only when next track has been signalled */
673 if (stream->next_track == false)
674 return -EPERM;
675
676 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
677
678 stream->next_track = false;
679 return retval;
680}
681
Vinod Koulb21c60a2011-12-23 10:36:39 +0530682static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
683{
684 struct snd_compr_file *data = f->private_data;
685 struct snd_compr_stream *stream;
686 int retval = -ENOTTY;
687
688 if (snd_BUG_ON(!data))
689 return -EFAULT;
690 stream = &data->stream;
691 if (snd_BUG_ON(!stream))
692 return -EFAULT;
693 mutex_lock(&stream->device->lock);
694 switch (_IOC_NR(cmd)) {
695 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
696 put_user(SNDRV_COMPRESS_VERSION,
697 (int __user *)arg) ? -EFAULT : 0;
698 break;
699 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
700 retval = snd_compr_get_caps(stream, arg);
701 break;
702 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
703 retval = snd_compr_get_codec_caps(stream, arg);
704 break;
705 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
706 retval = snd_compr_set_params(stream, arg);
707 break;
708 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
709 retval = snd_compr_get_params(stream, arg);
710 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530711 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
712 retval = snd_compr_set_metadata(stream, arg);
713 break;
714 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
715 retval = snd_compr_get_metadata(stream, arg);
716 break;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530717 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
718 retval = snd_compr_tstamp(stream, arg);
719 break;
720 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
721 retval = snd_compr_ioctl_avail(stream, arg);
722 break;
723 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
724 retval = snd_compr_pause(stream);
725 break;
726 case _IOC_NR(SNDRV_COMPRESS_RESUME):
727 retval = snd_compr_resume(stream);
728 break;
729 case _IOC_NR(SNDRV_COMPRESS_START):
730 retval = snd_compr_start(stream);
731 break;
732 case _IOC_NR(SNDRV_COMPRESS_STOP):
733 retval = snd_compr_stop(stream);
734 break;
735 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
736 retval = snd_compr_drain(stream);
737 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530738 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
739 retval = snd_compr_partial_drain(stream);
740 break;
741 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
742 retval = snd_compr_next_track(stream);
743 break;
744
Vinod Koulb21c60a2011-12-23 10:36:39 +0530745 }
746 mutex_unlock(&stream->device->lock);
747 return retval;
748}
749
750static const struct file_operations snd_compr_file_ops = {
751 .owner = THIS_MODULE,
752 .open = snd_compr_open,
753 .release = snd_compr_free,
754 .write = snd_compr_write,
755 .read = snd_compr_read,
756 .unlocked_ioctl = snd_compr_ioctl,
757 .mmap = snd_compr_mmap,
758 .poll = snd_compr_poll,
759};
760
761static int snd_compress_dev_register(struct snd_device *device)
762{
763 int ret = -EINVAL;
764 char str[16];
765 struct snd_compr *compr;
766
767 if (snd_BUG_ON(!device || !device->device_data))
768 return -EBADFD;
769 compr = device->device_data;
770
771 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
772 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
773 compr->direction);
774 /* register compressed device */
775 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
776 compr->device, &snd_compr_file_ops, compr, str);
777 if (ret < 0) {
778 pr_err("snd_register_device failed\n %d", ret);
779 return ret;
780 }
781 return ret;
782
783}
784
785static int snd_compress_dev_disconnect(struct snd_device *device)
786{
787 struct snd_compr *compr;
788
789 compr = device->device_data;
790 snd_unregister_device(compr->direction, compr->card, compr->device);
791 return 0;
792}
793
794/*
795 * snd_compress_new: create new compress device
796 * @card: sound card pointer
797 * @device: device number
798 * @dirn: device direction, should be of type enum snd_compr_direction
799 * @compr: compress device pointer
800 */
801int snd_compress_new(struct snd_card *card, int device,
802 int dirn, struct snd_compr *compr)
803{
804 static struct snd_device_ops ops = {
805 .dev_free = NULL,
806 .dev_register = snd_compress_dev_register,
807 .dev_disconnect = snd_compress_dev_disconnect,
808 };
809
810 compr->card = card;
811 compr->device = device;
812 compr->direction = dirn;
813 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
814}
815EXPORT_SYMBOL_GPL(snd_compress_new);
816
817static int snd_compress_add_device(struct snd_compr *device)
818{
819 int ret;
820
821 if (!device->card)
822 return -EINVAL;
823
824 /* register the card */
825 ret = snd_card_register(device->card);
826 if (ret)
827 goto out;
828 return 0;
829
830out:
831 pr_err("failed with %d\n", ret);
832 return ret;
833
834}
835
836static int snd_compress_remove_device(struct snd_compr *device)
837{
838 return snd_card_free(device->card);
839}
840
841/**
842 * snd_compress_register - register compressed device
843 *
844 * @device: compressed device to register
845 */
846int snd_compress_register(struct snd_compr *device)
847{
848 int retval;
849
850 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
851 return -EINVAL;
852
853 pr_debug("Registering compressed device %s\n", device->name);
854 if (snd_BUG_ON(!device->ops->open))
855 return -EINVAL;
856 if (snd_BUG_ON(!device->ops->free))
857 return -EINVAL;
858 if (snd_BUG_ON(!device->ops->set_params))
859 return -EINVAL;
860 if (snd_BUG_ON(!device->ops->trigger))
861 return -EINVAL;
862
863 mutex_init(&device->lock);
864
865 /* register a compressed card */
866 mutex_lock(&device_mutex);
867 retval = snd_compress_add_device(device);
868 mutex_unlock(&device_mutex);
869 return retval;
870}
871EXPORT_SYMBOL_GPL(snd_compress_register);
872
873int snd_compress_deregister(struct snd_compr *device)
874{
875 pr_debug("Removing compressed device %s\n", device->name);
876 mutex_lock(&device_mutex);
877 snd_compress_remove_device(device);
878 mutex_unlock(&device_mutex);
879 return 0;
880}
881EXPORT_SYMBOL_GPL(snd_compress_deregister);
882
883static int __init snd_compress_init(void)
884{
885 return 0;
886}
887
888static void __exit snd_compress_exit(void)
889{
890}
891
892module_init(snd_compress_init);
893module_exit(snd_compress_exit);
894
895MODULE_DESCRIPTION("ALSA Compressed offload framework");
896MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
897MODULE_LICENSE("GPL v2");