blob: a0986561f77c0dab7cbefa4e8aebc182b42a2275 [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>
Ravindra Lokhandec1036882015-12-07 12:08:31 +053041#include <linux/compat.h>
Vinod Koulb21c60a2011-12-23 10:36:39 +053042#include <sound/core.h>
43#include <sound/initval.h>
Richard Fitzgerald31742722015-11-25 13:00:23 +000044#include <sound/info.h>
Vinod Koulb21c60a2011-12-23 10:36:39 +053045#include <sound/compress_params.h>
46#include <sound/compress_offload.h>
47#include <sound/compress_driver.h>
48
Takashi Iwai462b3f12016-01-25 13:59:21 +010049/* struct snd_compr_codec_caps overflows the ioctl bit size for some
50 * architectures, so we need to disable the relevant ioctls.
51 */
52#if _IOC_SIZEBITS < 14
53#define COMPR_CODEC_CAPS_OVERFLOW
54#endif
55
Vinod Koulb21c60a2011-12-23 10:36:39 +053056/* TODO:
57 * - add substream support for multiple devices in case of
58 * SND_DYNAMIC_MINORS is not used
59 * - Multiple node representation
60 * driver should be able to register multiple nodes
61 */
62
63static DEFINE_MUTEX(device_mutex);
64
65struct snd_compr_file {
66 unsigned long caps;
67 struct snd_compr_stream stream;
68};
69
Charles Keepaxa4f2d872016-06-13 14:17:10 +010070static void error_delayed_work(struct work_struct *work);
71
Vinod Koulb21c60a2011-12-23 10:36:39 +053072/*
73 * a note on stream states used:
Vinod Koul41eb94f2016-03-04 20:25:30 +053074 * we use following states in the compressed core
Vinod Koulb21c60a2011-12-23 10:36:39 +053075 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
76 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
Vinod Koul41eb94f2016-03-04 20:25:30 +053077 * calling SNDRV_COMPRESS_SET_PARAMS. Running streams will come to this
Vinod Koulb21c60a2011-12-23 10:36:39 +053078 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
Vinod Koul862bca52016-03-04 20:25:29 +053079 * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
80 * playback only). User after setting up stream writes the data buffer
81 * before starting the stream.
Vinod Koulb21c60a2011-12-23 10:36:39 +053082 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
83 * decoding/encoding and rendering/capturing data.
84 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
85 * by calling SNDRV_COMPRESS_DRAIN.
86 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
87 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
88 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
89 */
90static int snd_compr_open(struct inode *inode, struct file *f)
91{
92 struct snd_compr *compr;
93 struct snd_compr_file *data;
94 struct snd_compr_runtime *runtime;
95 enum snd_compr_direction dirn;
96 int maj = imajor(inode);
97 int ret;
98
Dan Carpenter81cb3242012-09-11 14:12:43 +030099 if ((f->f_flags & O_ACCMODE) == O_WRONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530100 dirn = SND_COMPRESS_PLAYBACK;
Dan Carpenter81cb3242012-09-11 14:12:43 +0300101 else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530102 dirn = SND_COMPRESS_CAPTURE;
Dan Carpenter81cb3242012-09-11 14:12:43 +0300103 else
Vinod Koulb21c60a2011-12-23 10:36:39 +0530104 return -EINVAL;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530105
106 if (maj == snd_major)
107 compr = snd_lookup_minor_data(iminor(inode),
108 SNDRV_DEVICE_TYPE_COMPRESS);
109 else
110 return -EBADFD;
111
112 if (compr == NULL) {
113 pr_err("no device data!!!\n");
114 return -ENODEV;
115 }
116
117 if (dirn != compr->direction) {
118 pr_err("this device doesn't support this direction\n");
Takashi Iwaia0830db2012-10-16 13:05:59 +0200119 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530120 return -EINVAL;
121 }
122
123 data = kzalloc(sizeof(*data), GFP_KERNEL);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200124 if (!data) {
125 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530126 return -ENOMEM;
Takashi Iwaia0830db2012-10-16 13:05:59 +0200127 }
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100128
129 INIT_DELAYED_WORK(&data->stream.error_work, error_delayed_work);
130
Vinod Koulb21c60a2011-12-23 10:36:39 +0530131 data->stream.ops = compr->ops;
132 data->stream.direction = dirn;
133 data->stream.private_data = compr->private_data;
134 data->stream.device = compr;
135 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
136 if (!runtime) {
137 kfree(data);
Takashi Iwaia0830db2012-10-16 13:05:59 +0200138 snd_card_unref(compr->card);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530139 return -ENOMEM;
140 }
141 runtime->state = SNDRV_PCM_STATE_OPEN;
142 init_waitqueue_head(&runtime->sleep);
143 data->stream.runtime = runtime;
144 f->private_data = (void *)data;
145 mutex_lock(&compr->lock);
146 ret = compr->ops->open(&data->stream);
147 mutex_unlock(&compr->lock);
148 if (ret) {
149 kfree(runtime);
150 kfree(data);
151 }
Takashi Iwaia0830db2012-10-16 13:05:59 +0200152 snd_card_unref(compr->card);
Charles Keepax749d3222014-03-19 12:59:39 +0000153 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530154}
155
156static int snd_compr_free(struct inode *inode, struct file *f)
157{
158 struct snd_compr_file *data = f->private_data;
Liam Girdwoodb26d19e2013-09-13 17:43:16 +0100159 struct snd_compr_runtime *runtime = data->stream.runtime;
160
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100161 cancel_delayed_work_sync(&data->stream.error_work);
162
Liam Girdwoodb26d19e2013-09-13 17:43:16 +0100163 switch (runtime->state) {
164 case SNDRV_PCM_STATE_RUNNING:
165 case SNDRV_PCM_STATE_DRAINING:
166 case SNDRV_PCM_STATE_PAUSED:
167 data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
168 break;
169 default:
170 break;
171 }
172
Vinod Koulb21c60a2011-12-23 10:36:39 +0530173 data->stream.ops->free(&data->stream);
174 kfree(data->stream.runtime->buffer);
175 kfree(data->stream.runtime);
176 kfree(data);
177 return 0;
178}
179
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000180static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
Vinod Koulb21c60a2011-12-23 10:36:39 +0530181 struct snd_compr_tstamp *tstamp)
182{
Aviral Gupta1ad212c2014-06-20 10:47:16 +0530183 int err = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530184 if (!stream->ops->pointer)
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000185 return -ENOTSUPP;
Aviral Gupta1ad212c2014-06-20 10:47:16 +0530186 err = stream->ops->pointer(stream, tstamp);
187 if (err)
188 return err;
Banajit Goswamiead1a152016-12-21 18:09:18 -0800189 pr_debug("dsp consumed till %d total %llu bytes\n",
Vinod Koulb21c60a2011-12-23 10:36:39 +0530190 tstamp->byte_offset, tstamp->copied_total);
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100191 if (stream->direction == SND_COMPRESS_PLAYBACK)
192 stream->runtime->total_bytes_transferred = tstamp->copied_total;
193 else
194 stream->runtime->total_bytes_available = tstamp->copied_total;
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000195 return 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530196}
197
198static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
199 struct snd_compr_avail *avail)
200{
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000201 memset(avail, 0, sizeof(*avail));
Vinod Koulb21c60a2011-12-23 10:36:39 +0530202 snd_compr_update_tstamp(stream, &avail->tstamp);
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000203 /* Still need to return avail even if tstamp can't be filled in */
Vinod Koulb21c60a2011-12-23 10:36:39 +0530204
Vinod Koulb21c60a2011-12-23 10:36:39 +0530205 if (stream->runtime->total_bytes_available == 0 &&
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100206 stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
207 stream->direction == SND_COMPRESS_PLAYBACK) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530208 pr_debug("detected init and someone forgot to do a write\n");
209 return stream->runtime->buffer_size;
210 }
211 pr_debug("app wrote %lld, DSP consumed %lld\n",
212 stream->runtime->total_bytes_available,
213 stream->runtime->total_bytes_transferred);
214 if (stream->runtime->total_bytes_available ==
215 stream->runtime->total_bytes_transferred) {
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100216 if (stream->direction == SND_COMPRESS_PLAYBACK) {
217 pr_debug("both pointers are same, returning full avail\n");
218 return stream->runtime->buffer_size;
219 } else {
220 pr_debug("both pointers are same, returning no avail\n");
221 return 0;
222 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530223 }
224
Charles Keepax5b1f79f2013-04-18 11:01:03 +0100225 avail->avail = stream->runtime->total_bytes_available -
226 stream->runtime->total_bytes_transferred;
227 if (stream->direction == SND_COMPRESS_PLAYBACK)
228 avail->avail = stream->runtime->buffer_size - avail->avail;
229
Charles Keepax4c28e322013-04-18 10:59:23 +0100230 pr_debug("ret avail as %lld\n", avail->avail);
231 return avail->avail;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530232}
233
234static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
235{
236 struct snd_compr_avail avail;
237
238 return snd_compr_calc_avail(stream, &avail);
239}
240
241static int
242snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
243{
244 struct snd_compr_avail ioctl_avail;
245 size_t avail;
246
247 avail = snd_compr_calc_avail(stream, &ioctl_avail);
248 ioctl_avail.avail = avail;
249
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100250 switch (stream->runtime->state) {
251 case SNDRV_PCM_STATE_OPEN:
252 return -EBADFD;
253 case SNDRV_PCM_STATE_XRUN:
254 return -EPIPE;
255 default:
256 break;
257 }
258
Vinod Koulb21c60a2011-12-23 10:36:39 +0530259 if (copy_to_user((__u64 __user *)arg,
260 &ioctl_avail, sizeof(ioctl_avail)))
261 return -EFAULT;
262 return 0;
263}
264
265static int snd_compr_write_data(struct snd_compr_stream *stream,
266 const char __user *buf, size_t count)
267{
268 void *dstn;
269 size_t copy;
270 struct snd_compr_runtime *runtime = stream->runtime;
Charles Keepaxf0283b52013-04-18 11:03:46 +0100271 /* 64-bit Modulus */
272 u64 app_pointer = div64_u64(runtime->total_bytes_available,
273 runtime->buffer_size);
274 app_pointer = runtime->total_bytes_available -
275 (app_pointer * runtime->buffer_size);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530276
Charles Keepaxf0283b52013-04-18 11:03:46 +0100277 dstn = runtime->buffer + app_pointer;
Banajit Goswami46d8cd92016-11-21 21:01:01 -0800278 pr_debug("copying %zu at %lld\n",
279 count, app_pointer);
Charles Keepaxf0283b52013-04-18 11:03:46 +0100280 if (count < runtime->buffer_size - app_pointer) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530281 if (copy_from_user(dstn, buf, count))
282 return -EFAULT;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530283 } else {
Charles Keepaxf0283b52013-04-18 11:03:46 +0100284 copy = runtime->buffer_size - app_pointer;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530285 if (copy_from_user(dstn, buf, copy))
286 return -EFAULT;
287 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
288 return -EFAULT;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530289 }
290 /* if DSP cares, let it know data has been written */
291 if (stream->ops->ack)
292 stream->ops->ack(stream, count);
293 return count;
294}
295
296static ssize_t snd_compr_write(struct file *f, const char __user *buf,
297 size_t count, loff_t *offset)
298{
299 struct snd_compr_file *data = f->private_data;
300 struct snd_compr_stream *stream;
301 size_t avail;
302 int retval;
303
304 if (snd_BUG_ON(!data))
305 return -EFAULT;
306
307 stream = &data->stream;
308 mutex_lock(&stream->device->lock);
309 /* write is allowed when stream is running or has been steup */
Charles Keepax875f6ff2016-05-04 14:59:11 +0100310 switch (stream->runtime->state) {
311 case SNDRV_PCM_STATE_SETUP:
312 case SNDRV_PCM_STATE_PREPARED:
313 case SNDRV_PCM_STATE_RUNNING:
314 break;
315 default:
Vinod Koulb21c60a2011-12-23 10:36:39 +0530316 mutex_unlock(&stream->device->lock);
317 return -EBADFD;
318 }
319
320 avail = snd_compr_get_avail(stream);
Banajit Goswami46d8cd92016-11-21 21:01:01 -0800321 pr_debug("avail returned %zu\n", avail);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530322 /* calculate how much we can write to buffer */
323 if (avail > count)
324 avail = count;
325
Charles Keepax4daf8912013-04-18 11:01:38 +0100326 if (stream->ops->copy) {
327 char __user* cbuf = (char __user*)buf;
328 retval = stream->ops->copy(stream, cbuf, avail);
329 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530330 retval = snd_compr_write_data(stream, buf, avail);
Charles Keepax4daf8912013-04-18 11:01:38 +0100331 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530332 if (retval > 0)
333 stream->runtime->total_bytes_available += retval;
334
335 /* while initiating the stream, write should be called before START
336 * call, so in setup move state */
337 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
338 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
339 pr_debug("stream prepared, Houston we are good to go\n");
340 }
341
342 mutex_unlock(&stream->device->lock);
343 return retval;
344}
345
346
347static ssize_t snd_compr_read(struct file *f, char __user *buf,
348 size_t count, loff_t *offset)
349{
Charles Keepax49bb6402013-04-18 11:02:08 +0100350 struct snd_compr_file *data = f->private_data;
351 struct snd_compr_stream *stream;
352 size_t avail;
353 int retval;
354
355 if (snd_BUG_ON(!data))
356 return -EFAULT;
357
358 stream = &data->stream;
359 mutex_lock(&stream->device->lock);
360
Vinod Koul75481342013-04-29 14:25:23 +0530361 /* read is allowed when stream is running, paused, draining and setup
362 * (yes setup is state which we transition to after stop, so if user
363 * wants to read data after stop we allow that)
364 */
365 switch (stream->runtime->state) {
366 case SNDRV_PCM_STATE_OPEN:
367 case SNDRV_PCM_STATE_PREPARED:
Vinod Koul75481342013-04-29 14:25:23 +0530368 case SNDRV_PCM_STATE_SUSPENDED:
369 case SNDRV_PCM_STATE_DISCONNECTED:
Charles Keepax49bb6402013-04-18 11:02:08 +0100370 retval = -EBADFD;
371 goto out;
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100372 case SNDRV_PCM_STATE_XRUN:
373 retval = -EPIPE;
374 goto out;
Charles Keepax49bb6402013-04-18 11:02:08 +0100375 }
376
377 avail = snd_compr_get_avail(stream);
Banajit Goswami46d8cd92016-11-21 21:01:01 -0800378 pr_debug("avail returned %zu\n", avail);
Charles Keepax49bb6402013-04-18 11:02:08 +0100379 /* calculate how much we can read from buffer */
380 if (avail > count)
381 avail = count;
382
383 if (stream->ops->copy) {
384 retval = stream->ops->copy(stream, buf, avail);
385 } else {
386 retval = -ENXIO;
387 goto out;
388 }
389 if (retval > 0)
390 stream->runtime->total_bytes_transferred += retval;
391
392out:
393 mutex_unlock(&stream->device->lock);
394 return retval;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530395}
396
397static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
398{
399 return -ENXIO;
400}
401
402static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
403{
404 if (stream->direction == SND_COMPRESS_PLAYBACK)
405 return POLLOUT | POLLWRNORM;
406 else
407 return POLLIN | POLLRDNORM;
408}
409
410static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
411{
412 struct snd_compr_file *data = f->private_data;
413 struct snd_compr_stream *stream;
414 size_t avail;
415 int retval = 0;
416
417 if (snd_BUG_ON(!data))
Charles Keepax1d03f2b2016-05-04 14:59:10 +0100418 return POLLERR;
Charles Keepax5bd05392016-05-04 14:59:09 +0100419
Vinod Koulb21c60a2011-12-23 10:36:39 +0530420 stream = &data->stream;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530421
422 mutex_lock(&stream->device->lock);
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100423
424 switch (stream->runtime->state) {
425 case SNDRV_PCM_STATE_OPEN:
426 case SNDRV_PCM_STATE_XRUN:
Charles Keepax1d03f2b2016-05-04 14:59:10 +0100427 retval = snd_compr_get_poll(stream) | POLLERR;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530428 goto out;
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100429 default:
430 break;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530431 }
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100432
Vinod Koulb21c60a2011-12-23 10:36:39 +0530433 poll_wait(f, &stream->runtime->sleep, wait);
434
435 avail = snd_compr_get_avail(stream);
Banajit Goswami46d8cd92016-11-21 21:01:01 -0800436 pr_debug("avail is %zu\n", avail);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530437 /* check if we have at least one fragment to fill */
438 switch (stream->runtime->state) {
439 case SNDRV_PCM_STATE_DRAINING:
440 /* stream has been woken up after drain is complete
441 * draining done so set stream state to stopped
442 */
443 retval = snd_compr_get_poll(stream);
444 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
445 break;
446 case SNDRV_PCM_STATE_RUNNING:
447 case SNDRV_PCM_STATE_PREPARED:
448 case SNDRV_PCM_STATE_PAUSED:
449 if (avail >= stream->runtime->fragment_size)
450 retval = snd_compr_get_poll(stream);
451 break;
452 default:
Charles Keepax0b92b0c2016-05-04 14:59:08 +0100453 retval = snd_compr_get_poll(stream) | POLLERR;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530454 break;
455 }
456out:
457 mutex_unlock(&stream->device->lock);
458 return retval;
459}
460
461static int
462snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
463{
464 int retval;
465 struct snd_compr_caps caps;
466
467 if (!stream->ops->get_caps)
468 return -ENXIO;
469
Dan Carpenter1c62e9f2013-04-21 14:07:29 +0300470 memset(&caps, 0, sizeof(caps));
Vinod Koulb21c60a2011-12-23 10:36:39 +0530471 retval = stream->ops->get_caps(stream, &caps);
472 if (retval)
473 goto out;
474 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
475 retval = -EFAULT;
476out:
477 return retval;
478}
479
Takashi Iwai462b3f12016-01-25 13:59:21 +0100480#ifndef COMPR_CODEC_CAPS_OVERFLOW
Vinod Koulb21c60a2011-12-23 10:36:39 +0530481static int
482snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
483{
484 int retval;
485 struct snd_compr_codec_caps *caps;
486
487 if (!stream->ops->get_codec_caps)
488 return -ENXIO;
489
Takashi Iwai47966e92013-04-22 10:38:26 +0200490 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530491 if (!caps)
492 return -ENOMEM;
493
494 retval = stream->ops->get_codec_caps(stream, caps);
495 if (retval)
496 goto out;
497 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
498 retval = -EFAULT;
499
500out:
501 kfree(caps);
502 return retval;
503}
Takashi Iwai462b3f12016-01-25 13:59:21 +0100504#endif /* !COMPR_CODEC_CAPS_OVERFLOW */
Vinod Koulb21c60a2011-12-23 10:36:39 +0530505
506/* revisit this with snd_pcm_preallocate_xxx */
507static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
508 struct snd_compr_params *params)
509{
510 unsigned int buffer_size;
511 void *buffer;
512
513 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
514 if (stream->ops->copy) {
515 buffer = NULL;
516 /* if copy is defined the driver will be required to copy
517 * the data from core
518 */
519 } else {
520 buffer = kmalloc(buffer_size, GFP_KERNEL);
521 if (!buffer)
522 return -ENOMEM;
523 }
524 stream->runtime->fragment_size = params->buffer.fragment_size;
525 stream->runtime->fragments = params->buffer.fragments;
526 stream->runtime->buffer = buffer;
527 stream->runtime->buffer_size = buffer_size;
528 return 0;
529}
530
Vinod Koul4dc040a2012-09-17 11:51:25 +0530531static int snd_compress_check_input(struct snd_compr_params *params)
532{
533 /* first let's check the buffer parameter's */
534 if (params->buffer.fragment_size == 0 ||
Xiaojun Sang9c8c53e2016-06-01 11:26:45 +0800535 params->buffer.fragments > U32_MAX / params->buffer.fragment_size)
Vinod Koul4dc040a2012-09-17 11:51:25 +0530536 return -EINVAL;
537
Vinod Koulfb4a9772012-09-17 11:51:26 +0530538 /* now codec parameters */
539 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
540 return -EINVAL;
541
542 if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
543 return -EINVAL;
544
Vinod Koul4dc040a2012-09-17 11:51:25 +0530545 return 0;
546}
547
Vinod Koulb21c60a2011-12-23 10:36:39 +0530548static int
549snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
550{
551 struct snd_compr_params *params;
552 int retval;
553
554 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
555 /*
556 * we should allow parameter change only when stream has been
557 * opened not in other cases
558 */
Markus Elfringc2f14ba2016-08-21 21:02:06 +0200559 params = memdup_user((void __user *)arg, sizeof(*params));
560 if (IS_ERR(params))
561 return PTR_ERR(params);
Vinod Koul4dc040a2012-09-17 11:51:25 +0530562
563 retval = snd_compress_check_input(params);
564 if (retval)
565 goto out;
566
Vinod Koulb21c60a2011-12-23 10:36:39 +0530567 retval = snd_compr_allocate_buffer(stream, params);
568 if (retval) {
Jesper Juhl769fab22012-01-23 21:02:57 +0100569 retval = -ENOMEM;
570 goto out;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530571 }
Vinod Koul4dc040a2012-09-17 11:51:25 +0530572
Vinod Koulb21c60a2011-12-23 10:36:39 +0530573 retval = stream->ops->set_params(stream, params);
574 if (retval)
575 goto out;
Charles Keepax49bb6402013-04-18 11:02:08 +0100576
Jeeja KP9727b492013-02-14 16:52:51 +0530577 stream->metadata_set = false;
578 stream->next_track = false;
Charles Keepax49bb6402013-04-18 11:02:08 +0100579
580 if (stream->direction == SND_COMPRESS_PLAYBACK)
581 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
582 else
583 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
Jesper Juhl769fab22012-01-23 21:02:57 +0100584 } else {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530585 return -EPERM;
Jesper Juhl769fab22012-01-23 21:02:57 +0100586 }
Vinod Koulb21c60a2011-12-23 10:36:39 +0530587out:
588 kfree(params);
589 return retval;
590}
591
592static int
593snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
594{
595 struct snd_codec *params;
596 int retval;
597
598 if (!stream->ops->get_params)
599 return -EBADFD;
600
Takashi Iwai47966e92013-04-22 10:38:26 +0200601 params = kzalloc(sizeof(*params), GFP_KERNEL);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530602 if (!params)
603 return -ENOMEM;
604 retval = stream->ops->get_params(stream, params);
605 if (retval)
606 goto out;
607 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
608 retval = -EFAULT;
609
610out:
611 kfree(params);
612 return retval;
613}
614
Jeeja KP9727b492013-02-14 16:52:51 +0530615static int
616snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
617{
618 struct snd_compr_metadata metadata;
619 int retval;
620
621 if (!stream->ops->get_metadata)
622 return -ENXIO;
623
624 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
625 return -EFAULT;
626
627 retval = stream->ops->get_metadata(stream, &metadata);
628 if (retval != 0)
629 return retval;
630
631 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
632 return -EFAULT;
633
634 return 0;
635}
636
637static int
638snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
639{
640 struct snd_compr_metadata metadata;
641 int retval;
642
643 if (!stream->ops->set_metadata)
644 return -ENXIO;
645 /*
646 * we should allow parameter change only when stream has been
647 * opened not in other cases
648 */
649 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
650 return -EFAULT;
651
652 retval = stream->ops->set_metadata(stream, &metadata);
653 stream->metadata_set = true;
654
655 return retval;
656}
657
Vinod Koulb21c60a2011-12-23 10:36:39 +0530658static inline int
659snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
660{
Krishnankutty Kolathappilly4dee9542013-11-06 10:08:39 -0800661 struct snd_compr_tstamp tstamp;
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000662 int ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530663
Krishnankutty Kolathappilly4dee9542013-11-06 10:08:39 -0800664 memset(&tstamp, 0, sizeof(tstamp));
Richard Fitzgerald17ac8e52013-02-11 13:44:53 +0000665 ret = snd_compr_update_tstamp(stream, &tstamp);
666 if (ret == 0)
667 ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
668 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
669 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530670}
671
672static int snd_compr_pause(struct snd_compr_stream *stream)
673{
674 int retval;
675
676 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
677 return -EPERM;
678 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
Vinod Koul6b18f792012-06-12 16:16:17 +0530679 if (!retval)
Vinod Koulb21c60a2011-12-23 10:36:39 +0530680 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530681 return retval;
682}
683
684static int snd_compr_resume(struct snd_compr_stream *stream)
685{
686 int retval;
687
688 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
689 return -EPERM;
690 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
691 if (!retval)
692 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
693 return retval;
694}
695
696static int snd_compr_start(struct snd_compr_stream *stream)
697{
698 int retval;
699
700 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
701 return -EPERM;
702 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
703 if (!retval)
704 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
705 return retval;
706}
707
708static int snd_compr_stop(struct snd_compr_stream *stream)
709{
710 int retval;
711
712 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
713 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
714 return -EPERM;
715 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
716 if (!retval) {
Banajit Goswamifd495392016-11-21 20:46:10 -0800717 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
718 wake_up(&stream->runtime->sleep);
Vinod Koul8b214602012-06-12 16:16:18 +0530719 stream->runtime->total_bytes_available = 0;
720 stream->runtime->total_bytes_transferred = 0;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530721 }
722 return retval;
723}
724
Charles Keepaxa4f2d872016-06-13 14:17:10 +0100725static void error_delayed_work(struct work_struct *work)
726{
727 struct snd_compr_stream *stream;
728
729 stream = container_of(work, struct snd_compr_stream, error_work.work);
730
731 mutex_lock(&stream->device->lock);
732
733 stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
734 wake_up(&stream->runtime->sleep);
735
736 mutex_unlock(&stream->device->lock);
737}
738
739/*
740 * snd_compr_stop_error: Report a fatal error on a stream
741 * @stream: pointer to stream
742 * @state: state to transition the stream to
743 *
744 * Stop the stream and set its state.
745 *
746 * Should be called with compressed device lock held.
747 */
748int snd_compr_stop_error(struct snd_compr_stream *stream,
749 snd_pcm_state_t state)
750{
751 if (stream->runtime->state == state)
752 return 0;
753
754 stream->runtime->state = state;
755
756 pr_debug("Changing state to: %d\n", state);
757
758 queue_delayed_work(system_power_efficient_wq, &stream->error_work, 0);
759
760 return 0;
761}
762EXPORT_SYMBOL_GPL(snd_compr_stop_error);
763
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800764/* this fn is called without lock being held and we change stream states here
765 * so while using the stream state auquire the lock but relase before invoking
766 * DSP as the call will possibly take a while
767 */
Vinod Koulb21c60a2011-12-23 10:36:39 +0530768static int snd_compr_drain(struct snd_compr_stream *stream)
769{
770 int retval;
771
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800772 mutex_lock(&stream->device->lock);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530773 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800774 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
775 retval = -EPERM;
776 goto ret;
777 }
778 mutex_unlock(&stream->device->lock);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530779 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800780 mutex_lock(&stream->device->lock);
Banajit Goswamifd495392016-11-21 20:46:10 -0800781 if (!retval) {
782 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530783 wake_up(&stream->runtime->sleep);
Vinod Koul917f4b52013-10-24 16:37:31 +0530784 return retval;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530785 }
Vinod Koul917f4b52013-10-24 16:37:31 +0530786
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800787ret:
788 mutex_unlock(&stream->device->lock);
Banajit Goswamifd495392016-11-21 20:46:10 -0800789 return retval;
Vinod Koulb21c60a2011-12-23 10:36:39 +0530790}
791
Jeeja KP9727b492013-02-14 16:52:51 +0530792static int snd_compr_next_track(struct snd_compr_stream *stream)
793{
794 int retval;
795
796 /* only a running stream can transition to next track */
797 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
798 return -EPERM;
799
Vinod Koulcdb1ee32016-03-04 23:59:29 +0530800 /* you can signal next track if this is intended to be a gapless stream
Jeeja KP9727b492013-02-14 16:52:51 +0530801 * and current track metadata is set
802 */
803 if (stream->metadata_set == false)
804 return -EPERM;
805
806 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
807 if (retval != 0)
808 return retval;
809 stream->metadata_set = false;
810 stream->next_track = true;
811 return 0;
812}
813
814static int snd_compr_partial_drain(struct snd_compr_stream *stream)
815{
816 int retval;
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800817
818 mutex_lock(&stream->device->lock);
Jeeja KP9727b492013-02-14 16:52:51 +0530819 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800820 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
821 mutex_unlock(&stream->device->lock);
Jeeja KP9727b492013-02-14 16:52:51 +0530822 return -EPERM;
Eric Laurent5b1e55f2016-11-21 20:51:40 -0800823 }
824 mutex_unlock(&stream->device->lock);
Jeeja KP9727b492013-02-14 16:52:51 +0530825 /* stream can be drained only when next track has been signalled */
826 if (stream->next_track == false)
827 return -EPERM;
828
829 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
830
831 stream->next_track = false;
Banajit Goswamifd495392016-11-21 20:46:10 -0800832 return retval;
Jeeja KP9727b492013-02-14 16:52:51 +0530833}
834
Alexy Joseph998ee752015-09-10 00:04:30 -0700835static int snd_compr_set_next_track_param(struct snd_compr_stream *stream,
836 unsigned long arg)
837{
838 union snd_codec_options codec_options;
839 int retval;
840
841 /* set next track params when stream is running or has been setup */
842 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
843 stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
844 return -EPERM;
845
846 if (copy_from_user(&codec_options, (void __user *)arg,
847 sizeof(codec_options)))
848 return -EFAULT;
849
850 retval = stream->ops->set_next_track_param(stream, &codec_options);
851 return retval;
852}
853
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800854static int snd_compress_simple_ioctls(struct file *file,
855 struct snd_compr_stream *stream,
856 unsigned int cmd, unsigned long arg)
857{
858 int retval = -ENOTTY;
859
860 switch (_IOC_NR(cmd)) {
861 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
862 retval = put_user(SNDRV_COMPRESS_VERSION,
863 (int __user *)arg) ? -EFAULT : 0;
864 break;
865
866 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
867 retval = snd_compr_get_caps(stream, arg);
868 break;
869
870#ifndef COMPR_CODEC_CAPS_OVERFLOW
871 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
872 retval = snd_compr_get_codec_caps(stream, arg);
873 break;
874#endif
875
876 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
877 retval = snd_compr_tstamp(stream, arg);
878 break;
879
880 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
881 retval = snd_compr_ioctl_avail(stream, arg);
882 break;
883
884 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
885 retval = snd_compr_drain(stream);
886 break;
887
888 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
889 retval = snd_compr_partial_drain(stream);
890 break;
891 }
892
893 return retval;
894}
895
Vinod Koulb21c60a2011-12-23 10:36:39 +0530896static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
897{
898 struct snd_compr_file *data = f->private_data;
899 struct snd_compr_stream *stream;
900 int retval = -ENOTTY;
901
902 if (snd_BUG_ON(!data))
903 return -EFAULT;
Charles Keepax5bd05392016-05-04 14:59:09 +0100904
Vinod Koulb21c60a2011-12-23 10:36:39 +0530905 stream = &data->stream;
Charles Keepax5bd05392016-05-04 14:59:09 +0100906
Vinod Koulb21c60a2011-12-23 10:36:39 +0530907 mutex_lock(&stream->device->lock);
908 switch (_IOC_NR(cmd)) {
Vinod Koulb21c60a2011-12-23 10:36:39 +0530909 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
910 retval = snd_compr_set_params(stream, arg);
911 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800912
Vinod Koulb21c60a2011-12-23 10:36:39 +0530913 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
914 retval = snd_compr_get_params(stream, arg);
915 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800916
Jeeja KP9727b492013-02-14 16:52:51 +0530917 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
918 retval = snd_compr_set_metadata(stream, arg);
919 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800920
Jeeja KP9727b492013-02-14 16:52:51 +0530921 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
922 retval = snd_compr_get_metadata(stream, arg);
923 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800924
Vinod Koulb21c60a2011-12-23 10:36:39 +0530925 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
926 retval = snd_compr_pause(stream);
927 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800928
Vinod Koulb21c60a2011-12-23 10:36:39 +0530929 case _IOC_NR(SNDRV_COMPRESS_RESUME):
930 retval = snd_compr_resume(stream);
931 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800932
Vinod Koulb21c60a2011-12-23 10:36:39 +0530933 case _IOC_NR(SNDRV_COMPRESS_START):
934 retval = snd_compr_start(stream);
935 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800936
Vinod Koulb21c60a2011-12-23 10:36:39 +0530937 case _IOC_NR(SNDRV_COMPRESS_STOP):
938 retval = snd_compr_stop(stream);
939 break;
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800940
Jeeja KP9727b492013-02-14 16:52:51 +0530941 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
942 retval = snd_compr_next_track(stream);
943 break;
944
Alexy Joseph998ee752015-09-10 00:04:30 -0700945 case _IOC_NR(SNDRV_COMPRESS_SET_NEXT_TRACK_PARAM):
946 retval = snd_compr_set_next_track_param(stream, arg);
947 break;
948
Eric Laurent7bb01ad2016-11-21 20:40:13 -0800949 default:
950 mutex_unlock(&stream->device->lock);
951 return snd_compress_simple_ioctls(f, stream, cmd, arg);
952
Vinod Koulb21c60a2011-12-23 10:36:39 +0530953 }
954 mutex_unlock(&stream->device->lock);
955 return retval;
956}
957
Ravindra Lokhandec1036882015-12-07 12:08:31 +0530958/* support of 32bit userspace on 64bit platforms */
959#ifdef CONFIG_COMPAT
960static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
961 unsigned long arg)
962{
963 return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
964}
965#endif
966
Vinod Koulb21c60a2011-12-23 10:36:39 +0530967static const struct file_operations snd_compr_file_ops = {
968 .owner = THIS_MODULE,
969 .open = snd_compr_open,
970 .release = snd_compr_free,
971 .write = snd_compr_write,
972 .read = snd_compr_read,
973 .unlocked_ioctl = snd_compr_ioctl,
Ravindra Lokhandec1036882015-12-07 12:08:31 +0530974#ifdef CONFIG_COMPAT
975 .compat_ioctl = snd_compr_ioctl_compat,
976#endif
Vinod Koulb21c60a2011-12-23 10:36:39 +0530977 .mmap = snd_compr_mmap,
978 .poll = snd_compr_poll,
979};
980
981static int snd_compress_dev_register(struct snd_device *device)
982{
983 int ret = -EINVAL;
984 char str[16];
985 struct snd_compr *compr;
986
987 if (snd_BUG_ON(!device || !device->device_data))
988 return -EBADFD;
989 compr = device->device_data;
990
Vinod Koulb21c60a2011-12-23 10:36:39 +0530991 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
992 compr->direction);
993 /* register compressed device */
Takashi Iwai40a4b262015-01-30 08:34:58 +0100994 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
995 compr->card, compr->device,
996 &snd_compr_file_ops, compr, &compr->dev);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530997 if (ret < 0) {
Colin Ian Kingc5a905d2016-09-16 17:46:41 +0100998 pr_err("snd_register_device failed %d\n", ret);
Vinod Koulb21c60a2011-12-23 10:36:39 +0530999 return ret;
1000 }
1001 return ret;
1002
1003}
1004
1005static int snd_compress_dev_disconnect(struct snd_device *device)
1006{
1007 struct snd_compr *compr;
1008
1009 compr = device->device_data;
Takashi Iwai40a4b262015-01-30 08:34:58 +01001010 snd_unregister_device(&compr->dev);
Vinod Koulb21c60a2011-12-23 10:36:39 +05301011 return 0;
1012}
1013
Richard Fitzgerald31742722015-11-25 13:00:23 +00001014#ifdef CONFIG_SND_VERBOSE_PROCFS
1015static void snd_compress_proc_info_read(struct snd_info_entry *entry,
1016 struct snd_info_buffer *buffer)
1017{
1018 struct snd_compr *compr = (struct snd_compr *)entry->private_data;
1019
1020 snd_iprintf(buffer, "card: %d\n", compr->card->number);
1021 snd_iprintf(buffer, "device: %d\n", compr->device);
1022 snd_iprintf(buffer, "stream: %s\n",
1023 compr->direction == SND_COMPRESS_PLAYBACK
1024 ? "PLAYBACK" : "CAPTURE");
1025 snd_iprintf(buffer, "id: %s\n", compr->id);
1026}
1027
1028static int snd_compress_proc_init(struct snd_compr *compr)
1029{
1030 struct snd_info_entry *entry;
1031 char name[16];
1032
1033 sprintf(name, "compr%i", compr->device);
1034 entry = snd_info_create_card_entry(compr->card, name,
1035 compr->card->proc_root);
1036 if (!entry)
1037 return -ENOMEM;
1038 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
1039 if (snd_info_register(entry) < 0) {
1040 snd_info_free_entry(entry);
1041 return -ENOMEM;
1042 }
1043 compr->proc_root = entry;
1044
1045 entry = snd_info_create_card_entry(compr->card, "info",
1046 compr->proc_root);
1047 if (entry) {
1048 snd_info_set_text_ops(entry, compr,
1049 snd_compress_proc_info_read);
1050 if (snd_info_register(entry) < 0) {
1051 snd_info_free_entry(entry);
1052 entry = NULL;
1053 }
1054 }
1055 compr->proc_info_entry = entry;
1056
1057 return 0;
1058}
1059
1060static void snd_compress_proc_done(struct snd_compr *compr)
1061{
1062 snd_info_free_entry(compr->proc_info_entry);
1063 compr->proc_info_entry = NULL;
1064 snd_info_free_entry(compr->proc_root);
1065 compr->proc_root = NULL;
1066}
Richard Fitzgeralde5241a82015-11-25 13:00:24 +00001067
1068static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1069{
1070 strlcpy(compr->id, id, sizeof(compr->id));
1071}
Richard Fitzgerald31742722015-11-25 13:00:23 +00001072#else
1073static inline int snd_compress_proc_init(struct snd_compr *compr)
1074{
1075 return 0;
1076}
1077
1078static inline void snd_compress_proc_done(struct snd_compr *compr)
1079{
1080}
Richard Fitzgeralde5241a82015-11-25 13:00:24 +00001081
1082static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
1083{
1084}
Richard Fitzgerald31742722015-11-25 13:00:23 +00001085#endif
1086
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001087static int snd_compress_dev_free(struct snd_device *device)
1088{
1089 struct snd_compr *compr;
1090
1091 compr = device->device_data;
Richard Fitzgerald31742722015-11-25 13:00:23 +00001092 snd_compress_proc_done(compr);
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001093 put_device(&compr->dev);
1094 return 0;
1095}
1096
Vinod Koulb21c60a2011-12-23 10:36:39 +05301097/*
1098 * snd_compress_new: create new compress device
1099 * @card: sound card pointer
1100 * @device: device number
1101 * @dirn: device direction, should be of type enum snd_compr_direction
1102 * @compr: compress device pointer
1103 */
1104int snd_compress_new(struct snd_card *card, int device,
Richard Fitzgeralde5241a82015-11-25 13:00:24 +00001105 int dirn, const char *id, struct snd_compr *compr)
Vinod Koulb21c60a2011-12-23 10:36:39 +05301106{
1107 static struct snd_device_ops ops = {
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001108 .dev_free = snd_compress_dev_free,
Vinod Koulb21c60a2011-12-23 10:36:39 +05301109 .dev_register = snd_compress_dev_register,
1110 .dev_disconnect = snd_compress_dev_disconnect,
1111 };
Richard Fitzgerald31742722015-11-25 13:00:23 +00001112 int ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +05301113
1114 compr->card = card;
1115 compr->device = device;
1116 compr->direction = dirn;
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001117
Richard Fitzgeralde5241a82015-11-25 13:00:24 +00001118 snd_compress_set_id(compr, id);
1119
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001120 snd_device_initialize(&compr->dev, card);
1121 dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
1122
Richard Fitzgerald31742722015-11-25 13:00:23 +00001123 ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
1124 if (ret == 0)
1125 snd_compress_proc_init(compr);
1126
1127 return ret;
Vinod Koulb21c60a2011-12-23 10:36:39 +05301128}
1129EXPORT_SYMBOL_GPL(snd_compress_new);
1130
Liam Girdwood3e4f0102016-11-21 20:05:58 -08001131/*
1132 * snd_compress_free: free compress device
1133 * @card: sound card pointer
1134 * @compr: compress device pointer
1135 */
1136void snd_compress_free(struct snd_card *card, struct snd_compr *compr)
1137{
1138 snd_device_free(card, compr);
1139}
1140EXPORT_SYMBOL(snd_compress_free);
1141
Vinod Koulb21c60a2011-12-23 10:36:39 +05301142static int snd_compress_add_device(struct snd_compr *device)
1143{
1144 int ret;
1145
1146 if (!device->card)
1147 return -EINVAL;
1148
1149 /* register the card */
1150 ret = snd_card_register(device->card);
1151 if (ret)
1152 goto out;
1153 return 0;
1154
1155out:
1156 pr_err("failed with %d\n", ret);
1157 return ret;
1158
1159}
1160
1161static int snd_compress_remove_device(struct snd_compr *device)
1162{
1163 return snd_card_free(device->card);
1164}
1165
1166/**
1167 * snd_compress_register - register compressed device
1168 *
1169 * @device: compressed device to register
1170 */
1171int snd_compress_register(struct snd_compr *device)
1172{
1173 int retval;
1174
Takashi Iwai04c5d5a2015-01-30 08:16:35 +01001175 if (device->name == NULL || device->ops == NULL)
Vinod Koulb21c60a2011-12-23 10:36:39 +05301176 return -EINVAL;
1177
1178 pr_debug("Registering compressed device %s\n", device->name);
1179 if (snd_BUG_ON(!device->ops->open))
1180 return -EINVAL;
1181 if (snd_BUG_ON(!device->ops->free))
1182 return -EINVAL;
1183 if (snd_BUG_ON(!device->ops->set_params))
1184 return -EINVAL;
1185 if (snd_BUG_ON(!device->ops->trigger))
1186 return -EINVAL;
1187
1188 mutex_init(&device->lock);
1189
1190 /* register a compressed card */
1191 mutex_lock(&device_mutex);
1192 retval = snd_compress_add_device(device);
1193 mutex_unlock(&device_mutex);
1194 return retval;
1195}
1196EXPORT_SYMBOL_GPL(snd_compress_register);
1197
1198int snd_compress_deregister(struct snd_compr *device)
1199{
1200 pr_debug("Removing compressed device %s\n", device->name);
1201 mutex_lock(&device_mutex);
1202 snd_compress_remove_device(device);
1203 mutex_unlock(&device_mutex);
1204 return 0;
1205}
1206EXPORT_SYMBOL_GPL(snd_compress_deregister);
1207
1208static int __init snd_compress_init(void)
1209{
1210 return 0;
1211}
1212
1213static void __exit snd_compress_exit(void)
1214{
1215}
1216
1217module_init(snd_compress_init);
1218module_exit(snd_compress_exit);
1219
1220MODULE_DESCRIPTION("ALSA Compressed offload framework");
1221MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
1222MODULE_LICENSE("GPL v2");