blob: 68d3ffcac78bc5c9a901ac51fa47383c37c888bd [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>
Charles Keepaxf0283b52013-04-18 11:03:46 +010031#include <linux/math64.h>
Vinod Koulb21c60a2011-12-23 10:36:39 +053032#include <linux/mm.h>
33#include <linux/mutex.h>
34#include <linux/poll.h>
35#include <linux/slab.h>
36#include <linux/sched.h>
Charles Keepaxf0283b52013-04-18 11:03:46 +010037#include <linux/types.h>
Vinod Koulb21c60a2011-12-23 10:36:39 +053038#include <linux/uio.h>
39#include <linux/uaccess.h>
40#include <linux/module.h>
41#include <sound/core.h>
42#include <sound/initval.h>
43#include <sound/compress_params.h>
44#include <sound/compress_offload.h>
45#include <sound/compress_driver.h>
46
47/* TODO:
48 * - add substream support for multiple devices in case of
49 * SND_DYNAMIC_MINORS is not used
50 * - Multiple node representation
51 * driver should be able to register multiple nodes
52 */
53
54static DEFINE_MUTEX(device_mutex);
55
56struct snd_compr_file {
57 unsigned long caps;
58 struct snd_compr_stream stream;
59};
60
61/*
62 * a note on stream states used:
63 * we use follwing states in the compressed core
64 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
65 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
66 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
67 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
68 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
69 * decoding/encoding and rendering/capturing data.
70 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
71 * by calling SNDRV_COMPRESS_DRAIN.
72 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
73 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
74 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
75 */
76static int snd_compr_open(struct inode *inode, struct file *f)
77{
78 struct snd_compr *compr;
79 struct snd_compr_file *data;
80 struct snd_compr_runtime *runtime;
81 enum snd_compr_direction dirn;
82 int maj = imajor(inode);
83 int ret;
84
Dan Carpenter81cb3242012-09-11 14:12:43 +030085 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +053086 dirn = SND_COMPRESS_PLAYBACK;
Dan Carpenter81cb3242012-09-11 14:12:43 +030087 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +053088 dirn = SND_COMPRESS_CAPTURE;
Dan Carpenter81cb3242012-09-11 14:12:43 +030089 else
Vinod Koulb21c60a2011-12-23 10:36:39 +053090 return -EINVAL;
Vinod Koulb21c60a2011-12-23 10:36:39 +053091
92 if (maj == snd_major)
93 compr = snd_lookup_minor_data(iminor(inode),
94 SNDRV_DEVICE_TYPE_COMPRESS);
95 else
96 return -EBADFD;
97
98 if (compr == NULL) {
99 pr_err("no device data!!!\n");
100 return -ENODEV;
101 }
102
103 if (dirn != compr->direction) {
104 pr_err("this device doesn't support this direction\n");
Takashi Iwaia0830db2012-10-16 13:05:59 +0200105 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530106 return -EINVAL;
107 }
108
109 data = kzalloc(sizeof(*data), GFP_KERNEL);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200110 if (!data) {
111 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530112 return -ENOMEM;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200113 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530114 data->stream.ops = compr->ops;
115 data->stream.direction = dirn;
116 data->stream.private_data = compr->private_data;
117 data->stream.device = compr;
118 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
119 if (!runtime) {
120 kfree(data);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200121 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530122 return -ENOMEM;
123 }
124 runtime->state = SNDRV_PCM_STATE_OPEN;
125 init_waitqueue_head(&runtime->sleep);
Vinod Koul16442d42013-10-24 16:37:31 +0530126 init_waitqueue_head(&runtime->wait);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530127 data->stream.runtime = runtime;
128 f->private_data = (void *)data;
129 mutex_lock(&compr->lock);
130 ret = compr->ops->open(&data->stream);
131 mutex_unlock(&compr->lock);
132 if (ret) {
133 kfree(runtime);
134 kfree(data);
135 }
Takashi Iwaia0830db2012-10-16 13:05:59 +0200136 snd_card_unref(compr->card);
137 return 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530138}
139
140static int snd_compr_free(struct inode *inode, struct file *f)
141{
142 struct snd_compr_file *data = f->private_data;
143 data->stream.ops->free(&data->stream);
144 kfree(data->stream.runtime->buffer);
145 kfree(data->stream.runtime);
146 kfree(data);
147 return 0;
148}
149
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000150static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
Vinod Koulb21c60a2011-12-23 10:36:39 +0530151 struct snd_compr_tstamp *tstamp)
152{
153 if (!stream->ops->pointer)
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000154 return -ENOTSUPP;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530155 stream->ops->pointer(stream, tstamp);
156 pr_debug("dsp consumed till %d total %d bytes\n",
157 tstamp->byte_offset, tstamp->copied_total);
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100158 if (stream->direction == SND_COMPRESS_PLAYBACK)
159 stream->runtime->total_bytes_transferred = tstamp->copied_total;
160 else
161 stream->runtime->total_bytes_available = tstamp->copied_total;
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000162 return 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530163}
164
165static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
166 struct snd_compr_avail *avail)
167{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000168 memset(avail, 0, sizeof(*avail));
Vinod Koulb21c60a2011-12-23 10:36:39 +0530169 snd_compr_update_tstamp(stream, &avail->tstamp);
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000170 /* Still need to return avail even if tstamp can't be filled in */
Vinod Koulb21c60a2011-12-23 10:36:39 +0530171
Vinod Koulb21c60a2011-12-23 10:36:39 +0530172 if (stream->runtime->total_bytes_available == 0 &&
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100173 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
174 stream->direction == SND_COMPRESS_PLAYBACK) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530175 pr_debug("detected init and someone forgot to do a write\n");
176 return stream->runtime->buffer_size;
177 }
178 pr_debug("app wrote %lld, DSP consumed %lld\n",
179 stream->runtime->total_bytes_available,
180 stream->runtime->total_bytes_transferred);
181 if (stream->runtime->total_bytes_available ==
182 stream->runtime->total_bytes_transferred) {
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100183 if (stream->direction == SND_COMPRESS_PLAYBACK) {
184 pr_debug("both pointers are same, returning full avail\n");
185 return stream->runtime->buffer_size;
186 } else {
187 pr_debug("both pointers are same, returning no avail\n");
188 return 0;
189 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530190 }
191
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100192 avail->avail = stream->runtime->total_bytes_available -
193 stream->runtime->total_bytes_transferred;
194 if (stream->direction == SND_COMPRESS_PLAYBACK)
195 avail->avail = stream->runtime->buffer_size - avail->avail;
196
Charles Keepax4c28e322013-04-18 10:59:23 +0100197 pr_debug("ret avail as %lld\n", avail->avail);
198 return avail->avail;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530199}
200
201static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
202{
203 struct snd_compr_avail avail;
204
205 return snd_compr_calc_avail(stream, &avail);
206}
207
208static int
209snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
210{
211 struct snd_compr_avail ioctl_avail;
212 size_t avail;
213
214 avail = snd_compr_calc_avail(stream, &ioctl_avail);
215 ioctl_avail.avail = avail;
216
217 if (copy_to_user((__u64 __user *)arg,
218 &ioctl_avail, sizeof(ioctl_avail)))
219 return -EFAULT;
220 return 0;
221}
222
223static int snd_compr_write_data(struct snd_compr_stream *stream,
224 const char __user *buf, size_t count)
225{
226 void *dstn;
227 size_t copy;
228 struct snd_compr_runtime *runtime = stream->runtime;
Charles Keepaxf0283b52013-04-18 11:03:46 +0100229 /* 64-bit Modulus */
230 u64 app_pointer = div64_u64(runtime->total_bytes_available,
231 runtime->buffer_size);
232 app_pointer = runtime->total_bytes_available -
233 (app_pointer * runtime->buffer_size);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530234
Charles Keepaxf0283b52013-04-18 11:03:46 +0100235 dstn = runtime->buffer + app_pointer;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530236 pr_debug("copying %ld at %lld\n",
Charles Keepaxf0283b52013-04-18 11:03:46 +0100237 (unsigned long)count, app_pointer);
238 if (count < runtime->buffer_size - app_pointer) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530239 if (copy_from_user(dstn, buf, count))
240 return -EFAULT;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530241 } else {
Charles Keepaxf0283b52013-04-18 11:03:46 +0100242 copy = runtime->buffer_size - app_pointer;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530243 if (copy_from_user(dstn, buf, copy))
244 return -EFAULT;
245 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
246 return -EFAULT;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530247 }
248 /* if DSP cares, let it know data has been written */
249 if (stream->ops->ack)
250 stream->ops->ack(stream, count);
251 return count;
252}
253
254static ssize_t snd_compr_write(struct file *f, const char __user *buf,
255 size_t count, loff_t *offset)
256{
257 struct snd_compr_file *data = f->private_data;
258 struct snd_compr_stream *stream;
259 size_t avail;
260 int retval;
261
262 if (snd_BUG_ON(!data))
263 return -EFAULT;
264
265 stream = &data->stream;
266 mutex_lock(&stream->device->lock);
267 /* write is allowed when stream is running or has been steup */
268 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
269 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
270 mutex_unlock(&stream->device->lock);
271 return -EBADFD;
272 }
273
274 avail = snd_compr_get_avail(stream);
275 pr_debug("avail returned %ld\n", (unsigned long)avail);
276 /* calculate how much we can write to buffer */
277 if (avail > count)
278 avail = count;
279
Charles Keepax4daf8912013-04-18 11:01:38 +0100280 if (stream->ops->copy) {
281 char __user* cbuf = (char __user*)buf;
282 retval = stream->ops->copy(stream, cbuf, avail);
283 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530284 retval = snd_compr_write_data(stream, buf, avail);
Charles Keepax4daf8912013-04-18 11:01:38 +0100285 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530286 if (retval > 0)
287 stream->runtime->total_bytes_available += retval;
288
289 /* while initiating the stream, write should be called before START
290 * call, so in setup move state */
291 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
292 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
293 pr_debug("stream prepared, Houston we are good to go\n");
294 }
295
296 mutex_unlock(&stream->device->lock);
297 return retval;
298}
299
300
301static ssize_t snd_compr_read(struct file *f, char __user *buf,
302 size_t count, loff_t *offset)
303{
Charles Keepax49bb6402013-04-18 11:02:08 +0100304 struct snd_compr_file *data = f->private_data;
305 struct snd_compr_stream *stream;
306 size_t avail;
307 int retval;
308
309 if (snd_BUG_ON(!data))
310 return -EFAULT;
311
312 stream = &data->stream;
313 mutex_lock(&stream->device->lock);
314
Vinod Koul75481342013-04-29 14:25:23 +0530315 /* read is allowed when stream is running, paused, draining and setup
316 * (yes setup is state which we transition to after stop, so if user
317 * wants to read data after stop we allow that)
318 */
319 switch (stream->runtime->state) {
320 case SNDRV_PCM_STATE_OPEN:
321 case SNDRV_PCM_STATE_PREPARED:
322 case SNDRV_PCM_STATE_XRUN:
323 case SNDRV_PCM_STATE_SUSPENDED:
324 case SNDRV_PCM_STATE_DISCONNECTED:
Charles Keepax49bb6402013-04-18 11:02:08 +0100325 retval = -EBADFD;
326 goto out;
327 }
328
329 avail = snd_compr_get_avail(stream);
330 pr_debug("avail returned %ld\n", (unsigned long)avail);
331 /* calculate how much we can read from buffer */
332 if (avail > count)
333 avail = count;
334
335 if (stream->ops->copy) {
336 retval = stream->ops->copy(stream, buf, avail);
337 } else {
338 retval = -ENXIO;
339 goto out;
340 }
341 if (retval > 0)
342 stream->runtime->total_bytes_transferred += retval;
343
344out:
345 mutex_unlock(&stream->device->lock);
346 return retval;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530347}
348
349static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
350{
351 return -ENXIO;
352}
353
354static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
355{
356 if (stream->direction == SND_COMPRESS_PLAYBACK)
357 return POLLOUT | POLLWRNORM;
358 else
359 return POLLIN | POLLRDNORM;
360}
361
362static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
363{
364 struct snd_compr_file *data = f->private_data;
365 struct snd_compr_stream *stream;
366 size_t avail;
367 int retval = 0;
368
369 if (snd_BUG_ON(!data))
370 return -EFAULT;
371 stream = &data->stream;
372 if (snd_BUG_ON(!stream))
373 return -EFAULT;
374
375 mutex_lock(&stream->device->lock);
376 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
377 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
378 retval = -EBADFD;
379 goto out;
380 }
381 poll_wait(f, &stream->runtime->sleep, wait);
382
383 avail = snd_compr_get_avail(stream);
384 pr_debug("avail is %ld\n", (unsigned long)avail);
385 /* check if we have at least one fragment to fill */
386 switch (stream->runtime->state) {
387 case SNDRV_PCM_STATE_DRAINING:
388 /* stream has been woken up after drain is complete
389 * draining done so set stream state to stopped
390 */
391 retval = snd_compr_get_poll(stream);
392 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
393 break;
394 case SNDRV_PCM_STATE_RUNNING:
395 case SNDRV_PCM_STATE_PREPARED:
396 case SNDRV_PCM_STATE_PAUSED:
397 if (avail >= stream->runtime->fragment_size)
398 retval = snd_compr_get_poll(stream);
399 break;
400 default:
401 if (stream->direction == SND_COMPRESS_PLAYBACK)
402 retval = POLLOUT | POLLWRNORM | POLLERR;
403 else
404 retval = POLLIN | POLLRDNORM | POLLERR;
405 break;
406 }
407out:
408 mutex_unlock(&stream->device->lock);
409 return retval;
410}
411
412static int
413snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
414{
415 int retval;
416 struct snd_compr_caps caps;
417
418 if (!stream->ops->get_caps)
419 return -ENXIO;
420
Dan Carpenter1c62e9f2013-04-21 14:07:29 +0300421 memset(&caps, 0, sizeof(caps));
Vinod Koulb21c60a2011-12-23 10:36:39 +0530422 retval = stream->ops->get_caps(stream, &caps);
423 if (retval)
424 goto out;
425 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
426 retval = -EFAULT;
427out:
428 return retval;
429}
430
431static int
432snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
433{
434 int retval;
435 struct snd_compr_codec_caps *caps;
436
437 if (!stream->ops->get_codec_caps)
438 return -ENXIO;
439
Takashi Iwai47966e92013-04-22 10:38:26 +0200440 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530441 if (!caps)
442 return -ENOMEM;
443
444 retval = stream->ops->get_codec_caps(stream, caps);
445 if (retval)
446 goto out;
447 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
448 retval = -EFAULT;
449
450out:
451 kfree(caps);
452 return retval;
453}
454
455/* revisit this with snd_pcm_preallocate_xxx */
456static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
457 struct snd_compr_params *params)
458{
459 unsigned int buffer_size;
460 void *buffer;
461
462 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
463 if (stream->ops->copy) {
464 buffer = NULL;
465 /* if copy is defined the driver will be required to copy
466 * the data from core
467 */
468 } else {
469 buffer = kmalloc(buffer_size, GFP_KERNEL);
470 if (!buffer)
471 return -ENOMEM;
472 }
473 stream->runtime->fragment_size = params->buffer.fragment_size;
474 stream->runtime->fragments = params->buffer.fragments;
475 stream->runtime->buffer = buffer;
476 stream->runtime->buffer_size = buffer_size;
477 return 0;
478}
479
Vinod Koul4dc040a2012-09-17 11:51:25 +0530480static int snd_compress_check_input(struct snd_compr_params *params)
481{
482 /* first let's check the buffer parameter's */
483 if (params->buffer.fragment_size == 0 ||
484 params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
485 return -EINVAL;
486
Vinod Koulfb4a9772012-09-17 11:51:26 +0530487 /* now codec parameters */
488 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
489 return -EINVAL;
490
491 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
492 return -EINVAL;
493
494 if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
495 return -EINVAL;
496
Vinod Koul4dc040a2012-09-17 11:51:25 +0530497 return 0;
498}
499
Vinod Koulb21c60a2011-12-23 10:36:39 +0530500static int
501snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
502{
503 struct snd_compr_params *params;
504 int retval;
505
506 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
507 /*
508 * we should allow parameter change only when stream has been
509 * opened not in other cases
510 */
511 params = kmalloc(sizeof(*params), GFP_KERNEL);
512 if (!params)
513 return -ENOMEM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100514 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
515 retval = -EFAULT;
516 goto out;
517 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530518
519 retval = snd_compress_check_input(params);
520 if (retval)
521 goto out;
522
Vinod Koulb21c60a2011-12-23 10:36:39 +0530523 retval = snd_compr_allocate_buffer(stream, params);
524 if (retval) {
Jesper Juhl769fab22012-01-23 21:02:57 +0100525 retval = -ENOMEM;
526 goto out;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530527 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530528
Vinod Koulb21c60a2011-12-23 10:36:39 +0530529 retval = stream->ops->set_params(stream, params);
530 if (retval)
531 goto out;
Charles Keepax49bb6402013-04-18 11:02:08 +0100532
Jeeja KP9727b492013-02-14 16:52:51 +0530533 stream->metadata_set = false;
534 stream->next_track = false;
Charles Keepax49bb6402013-04-18 11:02:08 +0100535
536 if (stream->direction == SND_COMPRESS_PLAYBACK)
537 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
538 else
539 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
Jesper Juhl769fab22012-01-23 21:02:57 +0100540 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530541 return -EPERM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100542 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530543out:
544 kfree(params);
545 return retval;
546}
547
548static int
549snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
550{
551 struct snd_codec *params;
552 int retval;
553
554 if (!stream->ops->get_params)
555 return -EBADFD;
556
Takashi Iwai47966e92013-04-22 10:38:26 +0200557 params = kzalloc(sizeof(*params), GFP_KERNEL);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530558 if (!params)
559 return -ENOMEM;
560 retval = stream->ops->get_params(stream, params);
561 if (retval)
562 goto out;
563 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
564 retval = -EFAULT;
565
566out:
567 kfree(params);
568 return retval;
569}
570
Jeeja KP9727b492013-02-14 16:52:51 +0530571static int
572snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
573{
574 struct snd_compr_metadata metadata;
575 int retval;
576
577 if (!stream->ops->get_metadata)
578 return -ENXIO;
579
580 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
581 return -EFAULT;
582
583 retval = stream->ops->get_metadata(stream, &metadata);
584 if (retval != 0)
585 return retval;
586
587 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
588 return -EFAULT;
589
590 return 0;
591}
592
593static int
594snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
595{
596 struct snd_compr_metadata metadata;
597 int retval;
598
599 if (!stream->ops->set_metadata)
600 return -ENXIO;
601 /*
602 * we should allow parameter change only when stream has been
603 * opened not in other cases
604 */
605 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
606 return -EFAULT;
607
608 retval = stream->ops->set_metadata(stream, &metadata);
609 stream->metadata_set = true;
610
611 return retval;
612}
613
Vinod Koulb21c60a2011-12-23 10:36:39 +0530614static inline int
615snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
616{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000617 struct snd_compr_tstamp tstamp = {0};
618 int ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530619
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000620 ret = snd_compr_update_tstamp(stream, &tstamp);
621 if (ret == 0)
622 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
623 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
624 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530625}
626
627static int snd_compr_pause(struct snd_compr_stream *stream)
628{
629 int retval;
630
631 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
632 return -EPERM;
633 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
Vinod Koul6b18f792012-06-12 16:16:17 +0530634 if (!retval)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530635 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530636 return retval;
637}
638
639static int snd_compr_resume(struct snd_compr_stream *stream)
640{
641 int retval;
642
643 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
644 return -EPERM;
645 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
646 if (!retval)
647 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
648 return retval;
649}
650
651static int snd_compr_start(struct snd_compr_stream *stream)
652{
653 int retval;
654
655 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
656 return -EPERM;
657 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
658 if (!retval)
659 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
660 return retval;
661}
662
663static int snd_compr_stop(struct snd_compr_stream *stream)
664{
665 int retval;
666
667 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
668 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
669 return -EPERM;
670 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
671 if (!retval) {
672 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
673 wake_up(&stream->runtime->sleep);
Vinod Koul16442d42013-10-24 16:37:31 +0530674 snd_compr_drain_notify(stream);
Vinod Koul8b214602012-06-12 16:16:18 +0530675 stream->runtime->total_bytes_available = 0;
676 stream->runtime->total_bytes_transferred = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530677 }
678 return retval;
679}
680
Vinod Koul16442d42013-10-24 16:37:31 +0530681static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
682{
683 /*
684 * We are called with lock held. So drop the lock while we wait for
685 * drain complete notfication from the driver
686 *
687 * It is expected that driver will notify the drain completion and then
688 * stream will be moved to SETUP state, even if draining resulted in an
689 * error. We can trigger next track after this.
690 */
691 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
692 mutex_unlock(&stream->device->lock);
693
694 wait_event(stream->runtime->wait, stream->runtime->drain_wake);
695
696 wake_up(&stream->runtime->sleep);
697 mutex_lock(&stream->device->lock);
698
699 return 0;
700}
701
Vinod Koulb21c60a2011-12-23 10:36:39 +0530702static int snd_compr_drain(struct snd_compr_stream *stream)
703{
704 int retval;
705
706 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
707 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
708 return -EPERM;
Vinod Koul16442d42013-10-24 16:37:31 +0530709
710 stream->runtime->drain_wake = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530711 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
Vinod Koul16442d42013-10-24 16:37:31 +0530712 if (retval) {
713 pr_err("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530714 wake_up(&stream->runtime->sleep);
Vinod Koul16442d42013-10-24 16:37:31 +0530715 return retval;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530716 }
Vinod Koul16442d42013-10-24 16:37:31 +0530717
718 retval = snd_compress_wait_for_drain(stream);
719 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530720 return retval;
721}
722
Jeeja KP9727b492013-02-14 16:52:51 +0530723static int snd_compr_next_track(struct snd_compr_stream *stream)
724{
725 int retval;
726
727 /* only a running stream can transition to next track */
728 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
729 return -EPERM;
730
731 /* you can signal next track isf this is intended to be a gapless stream
732 * and current track metadata is set
733 */
734 if (stream->metadata_set == false)
735 return -EPERM;
736
737 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
738 if (retval != 0)
739 return retval;
740 stream->metadata_set = false;
741 stream->next_track = true;
742 return 0;
743}
744
745static int snd_compr_partial_drain(struct snd_compr_stream *stream)
746{
747 int retval;
748 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
749 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
750 return -EPERM;
751 /* stream can be drained only when next track has been signalled */
752 if (stream->next_track == false)
753 return -EPERM;
754
Vinod Koul16442d42013-10-24 16:37:31 +0530755 stream->runtime->drain_wake = 0;
Jeeja KP9727b492013-02-14 16:52:51 +0530756 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
Vinod Koul16442d42013-10-24 16:37:31 +0530757 if (retval) {
758 pr_err("Partial drain returned failure\n");
759 wake_up(&stream->runtime->sleep);
760 return retval;
761 }
Jeeja KP9727b492013-02-14 16:52:51 +0530762
763 stream->next_track = false;
Vinod Koul16442d42013-10-24 16:37:31 +0530764 return snd_compress_wait_for_drain(stream);
Jeeja KP9727b492013-02-14 16:52:51 +0530765}
766
Vinod Koulb21c60a2011-12-23 10:36:39 +0530767static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
768{
769 struct snd_compr_file *data = f->private_data;
770 struct snd_compr_stream *stream;
771 int retval = -ENOTTY;
772
773 if (snd_BUG_ON(!data))
774 return -EFAULT;
775 stream = &data->stream;
776 if (snd_BUG_ON(!stream))
777 return -EFAULT;
778 mutex_lock(&stream->device->lock);
779 switch (_IOC_NR(cmd)) {
780 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
Vinod Kould54d0122013-07-29 15:10:22 +0530781 retval = put_user(SNDRV_COMPRESS_VERSION,
Vinod Koulb21c60a2011-12-23 10:36:39 +0530782 (int __user *)arg) ? -EFAULT : 0;
783 break;
784 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
785 retval = snd_compr_get_caps(stream, arg);
786 break;
787 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
788 retval = snd_compr_get_codec_caps(stream, arg);
789 break;
790 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
791 retval = snd_compr_set_params(stream, arg);
792 break;
793 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
794 retval = snd_compr_get_params(stream, arg);
795 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530796 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
797 retval = snd_compr_set_metadata(stream, arg);
798 break;
799 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
800 retval = snd_compr_get_metadata(stream, arg);
801 break;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530802 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
803 retval = snd_compr_tstamp(stream, arg);
804 break;
805 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
806 retval = snd_compr_ioctl_avail(stream, arg);
807 break;
808 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
809 retval = snd_compr_pause(stream);
810 break;
811 case _IOC_NR(SNDRV_COMPRESS_RESUME):
812 retval = snd_compr_resume(stream);
813 break;
814 case _IOC_NR(SNDRV_COMPRESS_START):
815 retval = snd_compr_start(stream);
816 break;
817 case _IOC_NR(SNDRV_COMPRESS_STOP):
818 retval = snd_compr_stop(stream);
819 break;
820 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
821 retval = snd_compr_drain(stream);
822 break;
Jeeja KP9727b492013-02-14 16:52:51 +0530823 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
824 retval = snd_compr_partial_drain(stream);
825 break;
826 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
827 retval = snd_compr_next_track(stream);
828 break;
829
Vinod Koulb21c60a2011-12-23 10:36:39 +0530830 }
831 mutex_unlock(&stream->device->lock);
832 return retval;
833}
834
835static const struct file_operations snd_compr_file_ops = {
836 .owner = THIS_MODULE,
837 .open = snd_compr_open,
838 .release = snd_compr_free,
839 .write = snd_compr_write,
840 .read = snd_compr_read,
841 .unlocked_ioctl = snd_compr_ioctl,
842 .mmap = snd_compr_mmap,
843 .poll = snd_compr_poll,
844};
845
846static int snd_compress_dev_register(struct snd_device *device)
847{
848 int ret = -EINVAL;
849 char str[16];
850 struct snd_compr *compr;
851
852 if (snd_BUG_ON(!device || !device->device_data))
853 return -EBADFD;
854 compr = device->device_data;
855
856 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
857 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
858 compr->direction);
859 /* register compressed device */
860 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
861 compr->device, &snd_compr_file_ops, compr, str);
862 if (ret < 0) {
863 pr_err("snd_register_device failed\n %d", ret);
864 return ret;
865 }
866 return ret;
867
868}
869
870static int snd_compress_dev_disconnect(struct snd_device *device)
871{
872 struct snd_compr *compr;
873
874 compr = device->device_data;
Liam Girdwood1aff4162013-09-13 17:43:17 +0100875 snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
876 compr->device);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530877 return 0;
878}
879
880/*
881 * snd_compress_new: create new compress device
882 * @card: sound card pointer
883 * @device: device number
884 * @dirn: device direction, should be of type enum snd_compr_direction
885 * @compr: compress device pointer
886 */
887int snd_compress_new(struct snd_card *card, int device,
888 int dirn, struct snd_compr *compr)
889{
890 static struct snd_device_ops ops = {
891 .dev_free = NULL,
892 .dev_register = snd_compress_dev_register,
893 .dev_disconnect = snd_compress_dev_disconnect,
894 };
895
896 compr->card = card;
897 compr->device = device;
898 compr->direction = dirn;
899 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
900}
901EXPORT_SYMBOL_GPL(snd_compress_new);
902
903static int snd_compress_add_device(struct snd_compr *device)
904{
905 int ret;
906
907 if (!device->card)
908 return -EINVAL;
909
910 /* register the card */
911 ret = snd_card_register(device->card);
912 if (ret)
913 goto out;
914 return 0;
915
916out:
917 pr_err("failed with %d\n", ret);
918 return ret;
919
920}
921
922static int snd_compress_remove_device(struct snd_compr *device)
923{
924 return snd_card_free(device->card);
925}
926
927/**
928 * snd_compress_register - register compressed device
929 *
930 * @device: compressed device to register
931 */
932int snd_compress_register(struct snd_compr *device)
933{
934 int retval;
935
936 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
937 return -EINVAL;
938
939 pr_debug("Registering compressed device %s\n", device->name);
940 if (snd_BUG_ON(!device->ops->open))
941 return -EINVAL;
942 if (snd_BUG_ON(!device->ops->free))
943 return -EINVAL;
944 if (snd_BUG_ON(!device->ops->set_params))
945 return -EINVAL;
946 if (snd_BUG_ON(!device->ops->trigger))
947 return -EINVAL;
948
949 mutex_init(&device->lock);
950
951 /* register a compressed card */
952 mutex_lock(&device_mutex);
953 retval = snd_compress_add_device(device);
954 mutex_unlock(&device_mutex);
955 return retval;
956}
957EXPORT_SYMBOL_GPL(snd_compress_register);
958
959int snd_compress_deregister(struct snd_compr *device)
960{
961 pr_debug("Removing compressed device %s\n", device->name);
962 mutex_lock(&device_mutex);
963 snd_compress_remove_device(device);
964 mutex_unlock(&device_mutex);
965 return 0;
966}
967EXPORT_SYMBOL_GPL(snd_compress_deregister);
968
969static int __init snd_compress_init(void)
970{
971 return 0;
972}
973
974static void __exit snd_compress_exit(void)
975{
976}
977
978module_init(snd_compress_init);
979module_exit(snd_compress_exit);
980
981MODULE_DESCRIPTION("ALSA Compressed offload framework");
982MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
983MODULE_LICENSE("GPL v2");