blob: 1f69863a83d29bc1706ea6bd5b8b594faccd6973 [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
275 if (stream->ops->copy)
276 retval = stream->ops->copy(stream, buf, avail);
277 else
278 retval = snd_compr_write_data(stream, buf, avail);
279 if (retval > 0)
280 stream->runtime->total_bytes_available += retval;
281
282 /* while initiating the stream, write should be called before START
283 * call, so in setup move state */
284 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
285 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
286 pr_debug("stream prepared, Houston we are good to go\n");
287 }
288
289 mutex_unlock(&stream->device->lock);
290 return retval;
291}
292
293
294static ssize_t snd_compr_read(struct file *f, char __user *buf,
295 size_t count, loff_t *offset)
296{
297 return -ENXIO;
298}
299
300static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
301{
302 return -ENXIO;
303}
304
305static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
306{
307 if (stream->direction == SND_COMPRESS_PLAYBACK)
308 return POLLOUT | POLLWRNORM;
309 else
310 return POLLIN | POLLRDNORM;
311}
312
313static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
314{
315 struct snd_compr_file *data = f->private_data;
316 struct snd_compr_stream *stream;
317 size_t avail;
318 int retval = 0;
319
320 if (snd_BUG_ON(!data))
321 return -EFAULT;
322 stream = &data->stream;
323 if (snd_BUG_ON(!stream))
324 return -EFAULT;
325
326 mutex_lock(&stream->device->lock);
327 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
328 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
329 retval = -EBADFD;
330 goto out;
331 }
332 poll_wait(f, &stream->runtime->sleep, wait);
333
334 avail = snd_compr_get_avail(stream);
335 pr_debug("avail is %ld\n", (unsigned long)avail);
336 /* check if we have at least one fragment to fill */
337 switch (stream->runtime->state) {
338 case SNDRV_PCM_STATE_DRAINING:
339 /* stream has been woken up after drain is complete
340 * draining done so set stream state to stopped
341 */
342 retval = snd_compr_get_poll(stream);
343 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
344 break;
345 case SNDRV_PCM_STATE_RUNNING:
346 case SNDRV_PCM_STATE_PREPARED:
347 case SNDRV_PCM_STATE_PAUSED:
348 if (avail >= stream->runtime->fragment_size)
349 retval = snd_compr_get_poll(stream);
350 break;
351 default:
352 if (stream->direction == SND_COMPRESS_PLAYBACK)
353 retval = POLLOUT | POLLWRNORM | POLLERR;
354 else
355 retval = POLLIN | POLLRDNORM | POLLERR;
356 break;
357 }
358out:
359 mutex_unlock(&stream->device->lock);
360 return retval;
361}
362
363static int
364snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
365{
366 int retval;
367 struct snd_compr_caps caps;
368
369 if (!stream->ops->get_caps)
370 return -ENXIO;
371
372 retval = stream->ops->get_caps(stream, &caps);
373 if (retval)
374 goto out;
375 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
376 retval = -EFAULT;
377out:
378 return retval;
379}
380
381static int
382snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
383{
384 int retval;
385 struct snd_compr_codec_caps *caps;
386
387 if (!stream->ops->get_codec_caps)
388 return -ENXIO;
389
390 caps = kmalloc(sizeof(*caps), GFP_KERNEL);
391 if (!caps)
392 return -ENOMEM;
393
394 retval = stream->ops->get_codec_caps(stream, caps);
395 if (retval)
396 goto out;
397 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
398 retval = -EFAULT;
399
400out:
401 kfree(caps);
402 return retval;
403}
404
405/* revisit this with snd_pcm_preallocate_xxx */
406static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
407 struct snd_compr_params *params)
408{
409 unsigned int buffer_size;
410 void *buffer;
411
412 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
413 if (stream->ops->copy) {
414 buffer = NULL;
415 /* if copy is defined the driver will be required to copy
416 * the data from core
417 */
418 } else {
419 buffer = kmalloc(buffer_size, GFP_KERNEL);
420 if (!buffer)
421 return -ENOMEM;
422 }
423 stream->runtime->fragment_size = params->buffer.fragment_size;
424 stream->runtime->fragments = params->buffer.fragments;
425 stream->runtime->buffer = buffer;
426 stream->runtime->buffer_size = buffer_size;
427 return 0;
428}
429
Vinod Koul4dc040a2012-09-17 11:51:25 +0530430static int snd_compress_check_input(struct snd_compr_params *params)
431{
432 /* first let's check the buffer parameter's */
433 if (params->buffer.fragment_size == 0 ||
434 params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
435 return -EINVAL;
436
Vinod Koulfb4a9772012-09-17 11:51:26 +0530437 /* now codec parameters */
438 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
439 return -EINVAL;
440
441 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
442 return -EINVAL;
443
444 if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
445 return -EINVAL;
446
Vinod Koul4dc040a2012-09-17 11:51:25 +0530447 return 0;
448}
449
Vinod Koulb21c60a2011-12-23 10:36:39 +0530450static int
451snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
452{
453 struct snd_compr_params *params;
454 int retval;
455
456 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
457 /*
458 * we should allow parameter change only when stream has been
459 * opened not in other cases
460 */
461 params = kmalloc(sizeof(*params), GFP_KERNEL);
462 if (!params)
463 return -ENOMEM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100464 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
465 retval = -EFAULT;
466 goto out;
467 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530468
469 retval = snd_compress_check_input(params);
470 if (retval)
471 goto out;
472
Vinod Koulb21c60a2011-12-23 10:36:39 +0530473 retval = snd_compr_allocate_buffer(stream, params);
474 if (retval) {
Jesper Juhl769fab22012-01-23 21:02:57 +0100475 retval = -ENOMEM;
476 goto out;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530477 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530478
Vinod Koulb21c60a2011-12-23 10:36:39 +0530479 retval = stream->ops->set_params(stream, params);
480 if (retval)
481 goto out;
482 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
Jeeja KP9727b492013-02-14 16:52:51 +0530483 stream->metadata_set = false;
484 stream->next_track = false;
Jesper Juhl769fab22012-01-23 21:02:57 +0100485 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530486 return -EPERM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100487 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530488out:
489 kfree(params);
490 return retval;
491}
492
493static int
494snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
495{
496 struct snd_codec *params;
497 int retval;
498
499 if (!stream->ops->get_params)
500 return -EBADFD;
501
502 params = kmalloc(sizeof(*params), GFP_KERNEL);
503 if (!params)
504 return -ENOMEM;
505 retval = stream->ops->get_params(stream, params);
506 if (retval)
507 goto out;
508 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
509 retval = -EFAULT;
510
511out:
512 kfree(params);
513 return retval;
514}
515
Jeeja KP9727b492013-02-14 16:52:51 +0530516static int
517snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
518{
519 struct snd_compr_metadata metadata;
520 int retval;
521
522 if (!stream->ops->get_metadata)
523 return -ENXIO;
524
525 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
526 return -EFAULT;
527
528 retval = stream->ops->get_metadata(stream, &metadata);
529 if (retval != 0)
530 return retval;
531
532 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
533 return -EFAULT;
534
535 return 0;
536}
537
538static int
539snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
540{
541 struct snd_compr_metadata metadata;
542 int retval;
543
544 if (!stream->ops->set_metadata)
545 return -ENXIO;
546 /*
547 * we should allow parameter change only when stream has been
548 * opened not in other cases
549 */
550 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
551 return -EFAULT;
552
553 retval = stream->ops->set_metadata(stream, &metadata);
554 stream->metadata_set = true;
555
556 return retval;
557}
558
Vinod Koulb21c60a2011-12-23 10:36:39 +0530559static inline int
560snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
561{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000562 struct snd_compr_tstamp tstamp = {0};
563 int ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530564
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000565 ret = snd_compr_update_tstamp(stream, &tstamp);
566 if (ret == 0)
567 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
568 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
569 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530570}
571
572static int snd_compr_pause(struct snd_compr_stream *stream)
573{
574 int retval;
575
576 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
577 return -EPERM;
578 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
Vinod Koul6b18f792012-06-12 16:16:17 +0530579 if (!retval)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530580 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530581 return retval;
582}
583
584static int snd_compr_resume(struct snd_compr_stream *stream)
585{
586 int retval;
587
588 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
589 return -EPERM;
590 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
591 if (!retval)
592 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
593 return retval;
594}
595
596static int snd_compr_start(struct snd_compr_stream *stream)
597{
598 int retval;
599
600 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
601 return -EPERM;
602 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
603 if (!retval)
604 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
605 return retval;
606}
607
608static int snd_compr_stop(struct snd_compr_stream *stream)
609{
610 int retval;
611
612 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
613 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
614 return -EPERM;
615 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
616 if (!retval) {
617 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
618 wake_up(&stream->runtime->sleep);
Vinod Koul8b214602012-06-12 16:16:18 +0530619 stream->runtime->hw_pointer = 0;
620 stream->runtime->app_pointer = 0;
621 stream->runtime->total_bytes_available = 0;
622 stream->runtime->total_bytes_transferred = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530623 }
624 return retval;
625}
626
627static int snd_compr_drain(struct snd_compr_stream *stream)
628{
629 int retval;
630
631 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
632 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
633 return -EPERM;
634 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
635 if (!retval) {
636 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
637 wake_up(&stream->runtime->sleep);
638 }
639 return retval;
640}
641
Jeeja KP9727b492013-02-14 16:52:51 +0530642static int snd_compr_next_track(struct snd_compr_stream *stream)
643{
644 int retval;
645
646 /* only a running stream can transition to next track */
647 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
648 return -EPERM;
649
650 /* you can signal next track isf this is intended to be a gapless stream
651 * and current track metadata is set
652 */
653 if (stream->metadata_set == false)
654 return -EPERM;
655
656 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
657 if (retval != 0)
658 return retval;
659 stream->metadata_set = false;
660 stream->next_track = true;
661 return 0;
662}
663
664static int snd_compr_partial_drain(struct snd_compr_stream *stream)
665{
666 int retval;
667 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
668 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
669 return -EPERM;
670 /* stream can be drained only when next track has been signalled */
671 if (stream->next_track == false)
672 return -EPERM;
673
674 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
675
676 stream->next_track = false;
677 return retval;
678}
679
Vinod Koulb21c60a2011-12-23 10:36:39 +0530680static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
681{
682 struct snd_compr_file *data = f->private_data;
683 struct snd_compr_stream *stream;
684 int retval = -ENOTTY;
685
686 if (snd_BUG_ON(!data))
687 return -EFAULT;
688 stream = &data->stream;
689 if (snd_BUG_ON(!stream))
690 return -EFAULT;
691 mutex_lock(&stream->device->lock);
692 switch (_IOC_NR(cmd)) {
693 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
694 put_user(SNDRV_COMPRESS_VERSION,
695 (int __user *)arg) ? -EFAULT : 0;
696 break;
697 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
698 retval = snd_compr_get_caps(stream, arg);
699 break;
700 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
701 retval = snd_compr_get_codec_caps(stream, arg);
702 break;
703 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
704 retval = snd_compr_set_params(stream, arg);
705 break;
706 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
707 retval = snd_compr_get_params(stream, arg);
708 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530709 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
710 retval = snd_compr_set_metadata(stream, arg);
711 break;
712 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
713 retval = snd_compr_get_metadata(stream, arg);
714 break;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530715 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
716 retval = snd_compr_tstamp(stream, arg);
717 break;
718 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
719 retval = snd_compr_ioctl_avail(stream, arg);
720 break;
721 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
722 retval = snd_compr_pause(stream);
723 break;
724 case _IOC_NR(SNDRV_COMPRESS_RESUME):
725 retval = snd_compr_resume(stream);
726 break;
727 case _IOC_NR(SNDRV_COMPRESS_START):
728 retval = snd_compr_start(stream);
729 break;
730 case _IOC_NR(SNDRV_COMPRESS_STOP):
731 retval = snd_compr_stop(stream);
732 break;
733 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
734 retval = snd_compr_drain(stream);
735 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530736 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
737 retval = snd_compr_partial_drain(stream);
738 break;
739 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
740 retval = snd_compr_next_track(stream);
741 break;
742
Vinod Koulb21c60a2011-12-23 10:36:39 +0530743 }
744 mutex_unlock(&stream->device->lock);
745 return retval;
746}
747
748static const struct file_operations snd_compr_file_ops = {
749 .owner = THIS_MODULE,
750 .open = snd_compr_open,
751 .release = snd_compr_free,
752 .write = snd_compr_write,
753 .read = snd_compr_read,
754 .unlocked_ioctl = snd_compr_ioctl,
755 .mmap = snd_compr_mmap,
756 .poll = snd_compr_poll,
757};
758
759static int snd_compress_dev_register(struct snd_device *device)
760{
761 int ret = -EINVAL;
762 char str[16];
763 struct snd_compr *compr;
764
765 if (snd_BUG_ON(!device || !device->device_data))
766 return -EBADFD;
767 compr = device->device_data;
768
769 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
770 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
771 compr->direction);
772 /* register compressed device */
773 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
774 compr->device, &snd_compr_file_ops, compr, str);
775 if (ret < 0) {
776 pr_err("snd_register_device failed\n %d", ret);
777 return ret;
778 }
779 return ret;
780
781}
782
783static int snd_compress_dev_disconnect(struct snd_device *device)
784{
785 struct snd_compr *compr;
786
787 compr = device->device_data;
788 snd_unregister_device(compr->direction, compr->card, compr->device);
789 return 0;
790}
791
792/*
793 * snd_compress_new: create new compress device
794 * @card: sound card pointer
795 * @device: device number
796 * @dirn: device direction, should be of type enum snd_compr_direction
797 * @compr: compress device pointer
798 */
799int snd_compress_new(struct snd_card *card, int device,
800 int dirn, struct snd_compr *compr)
801{
802 static struct snd_device_ops ops = {
803 .dev_free = NULL,
804 .dev_register = snd_compress_dev_register,
805 .dev_disconnect = snd_compress_dev_disconnect,
806 };
807
808 compr->card = card;
809 compr->device = device;
810 compr->direction = dirn;
811 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
812}
813EXPORT_SYMBOL_GPL(snd_compress_new);
814
815static int snd_compress_add_device(struct snd_compr *device)
816{
817 int ret;
818
819 if (!device->card)
820 return -EINVAL;
821
822 /* register the card */
823 ret = snd_card_register(device->card);
824 if (ret)
825 goto out;
826 return 0;
827
828out:
829 pr_err("failed with %d\n", ret);
830 return ret;
831
832}
833
834static int snd_compress_remove_device(struct snd_compr *device)
835{
836 return snd_card_free(device->card);
837}
838
839/**
840 * snd_compress_register - register compressed device
841 *
842 * @device: compressed device to register
843 */
844int snd_compress_register(struct snd_compr *device)
845{
846 int retval;
847
848 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
849 return -EINVAL;
850
851 pr_debug("Registering compressed device %s\n", device->name);
852 if (snd_BUG_ON(!device->ops->open))
853 return -EINVAL;
854 if (snd_BUG_ON(!device->ops->free))
855 return -EINVAL;
856 if (snd_BUG_ON(!device->ops->set_params))
857 return -EINVAL;
858 if (snd_BUG_ON(!device->ops->trigger))
859 return -EINVAL;
860
861 mutex_init(&device->lock);
862
863 /* register a compressed card */
864 mutex_lock(&device_mutex);
865 retval = snd_compress_add_device(device);
866 mutex_unlock(&device_mutex);
867 return retval;
868}
869EXPORT_SYMBOL_GPL(snd_compress_register);
870
871int snd_compress_deregister(struct snd_compr *device)
872{
873 pr_debug("Removing compressed device %s\n", device->name);
874 mutex_lock(&device_mutex);
875 snd_compress_remove_device(device);
876 mutex_unlock(&device_mutex);
877 return 0;
878}
879EXPORT_SYMBOL_GPL(snd_compress_deregister);
880
881static int __init snd_compress_init(void)
882{
883 return 0;
884}
885
886static void __exit snd_compress_exit(void)
887{
888}
889
890module_init(snd_compress_init);
891module_exit(snd_compress_exit);
892
893MODULE_DESCRIPTION("ALSA Compressed offload framework");
894MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
895MODULE_LICENSE("GPL v2");