Commit | Line | Data |
---|---|---|
b21c60a4 VK |
1 | /* |
2 | * compress_core.c - compress offload core | |
3 | * | |
4 | * Copyright (C) 2011 Intel Corporation | |
5 | * Authors: Vinod Koul <vinod.koul@linux.intel.com> | |
6 | * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> | |
7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | * | |
22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
23 | * | |
24 | */ | |
25 | #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ | |
26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) | |
27 | ||
28 | #include <linux/file.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/list.h> | |
f0283b58 | 31 | #include <linux/math64.h> |
b21c60a4 VK |
32 | #include <linux/mm.h> |
33 | #include <linux/mutex.h> | |
34 | #include <linux/poll.h> | |
35 | #include <linux/slab.h> | |
36 | #include <linux/sched.h> | |
f0283b58 | 37 | #include <linux/types.h> |
b21c60a4 VK |
38 | #include <linux/uio.h> |
39 | #include <linux/uaccess.h> | |
40 | #include <linux/module.h> | |
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 | ||
074e46b6 TI |
47 | /* struct snd_compr_codec_caps overflows the ioctl bit size for some |
48 | * architectures, so we need to disable the relevant ioctls. | |
49 | */ | |
50 | #if _IOC_SIZEBITS < 14 | |
51 | #define COMPR_CODEC_CAPS_OVERFLOW | |
52 | #endif | |
53 | ||
b21c60a4 VK |
54 | /* TODO: |
55 | * - add substream support for multiple devices in case of | |
56 | * SND_DYNAMIC_MINORS is not used | |
57 | * - Multiple node representation | |
58 | * driver should be able to register multiple nodes | |
59 | */ | |
60 | ||
61 | static DEFINE_MUTEX(device_mutex); | |
62 | ||
63 | struct snd_compr_file { | |
64 | unsigned long caps; | |
65 | struct snd_compr_stream stream; | |
66 | }; | |
67 | ||
68 | /* | |
69 | * a note on stream states used: | |
70 | * we use follwing states in the compressed core | |
71 | * SNDRV_PCM_STATE_OPEN: When stream has been opened. | |
72 | * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by | |
73 | * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this | |
74 | * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain. | |
75 | * SNDRV_PCM_STATE_RUNNING: When stream has been started and is | |
76 | * decoding/encoding and rendering/capturing data. | |
77 | * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done | |
78 | * by calling SNDRV_COMPRESS_DRAIN. | |
79 | * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling | |
80 | * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling | |
81 | * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. | |
82 | */ | |
83 | static int snd_compr_open(struct inode *inode, struct file *f) | |
84 | { | |
85 | struct snd_compr *compr; | |
86 | struct snd_compr_file *data; | |
87 | struct snd_compr_runtime *runtime; | |
88 | enum snd_compr_direction dirn; | |
89 | int maj = imajor(inode); | |
90 | int ret; | |
91 | ||
81cb3246 | 92 | if ((f->f_flags & O_ACCMODE) == O_WRONLY) |
b21c60a4 | 93 | dirn = SND_COMPRESS_PLAYBACK; |
81cb3246 | 94 | else if ((f->f_flags & O_ACCMODE) == O_RDONLY) |
b21c60a4 | 95 | dirn = SND_COMPRESS_CAPTURE; |
81cb3246 | 96 | else |
b21c60a4 | 97 | return -EINVAL; |
b21c60a4 VK |
98 | |
99 | if (maj == snd_major) | |
100 | compr = snd_lookup_minor_data(iminor(inode), | |
101 | SNDRV_DEVICE_TYPE_COMPRESS); | |
102 | else | |
103 | return -EBADFD; | |
104 | ||
105 | if (compr == NULL) { | |
106 | pr_err("no device data!!!\n"); | |
107 | return -ENODEV; | |
108 | } | |
109 | ||
110 | if (dirn != compr->direction) { | |
111 | pr_err("this device doesn't support this direction\n"); | |
a0830dbd | 112 | snd_card_unref(compr->card); |
b21c60a4 VK |
113 | return -EINVAL; |
114 | } | |
115 | ||
116 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
a0830dbd TI |
117 | if (!data) { |
118 | snd_card_unref(compr->card); | |
b21c60a4 | 119 | return -ENOMEM; |
a0830dbd | 120 | } |
b21c60a4 VK |
121 | data->stream.ops = compr->ops; |
122 | data->stream.direction = dirn; | |
123 | data->stream.private_data = compr->private_data; | |
124 | data->stream.device = compr; | |
125 | runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); | |
126 | if (!runtime) { | |
127 | kfree(data); | |
a0830dbd | 128 | snd_card_unref(compr->card); |
b21c60a4 VK |
129 | return -ENOMEM; |
130 | } | |
131 | runtime->state = SNDRV_PCM_STATE_OPEN; | |
132 | init_waitqueue_head(&runtime->sleep); | |
133 | data->stream.runtime = runtime; | |
134 | f->private_data = (void *)data; | |
135 | mutex_lock(&compr->lock); | |
136 | ret = compr->ops->open(&data->stream); | |
137 | mutex_unlock(&compr->lock); | |
138 | if (ret) { | |
139 | kfree(runtime); | |
140 | kfree(data); | |
141 | } | |
a0830dbd | 142 | snd_card_unref(compr->card); |
62726e2f | 143 | return ret; |
b21c60a4 VK |
144 | } |
145 | ||
146 | static int snd_compr_free(struct inode *inode, struct file *f) | |
147 | { | |
148 | struct snd_compr_file *data = f->private_data; | |
149 | data->stream.ops->free(&data->stream); | |
150 | kfree(data->stream.runtime->buffer); | |
151 | kfree(data->stream.runtime); | |
152 | kfree(data); | |
153 | return 0; | |
154 | } | |
155 | ||
17ac8e5c | 156 | static int snd_compr_update_tstamp(struct snd_compr_stream *stream, |
b21c60a4 VK |
157 | struct snd_compr_tstamp *tstamp) |
158 | { | |
159 | if (!stream->ops->pointer) | |
17ac8e5c | 160 | return -ENOTSUPP; |
b21c60a4 VK |
161 | stream->ops->pointer(stream, tstamp); |
162 | pr_debug("dsp consumed till %d total %d bytes\n", | |
163 | tstamp->byte_offset, tstamp->copied_total); | |
5b1f79f7 CK |
164 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
165 | stream->runtime->total_bytes_transferred = tstamp->copied_total; | |
166 | else | |
167 | stream->runtime->total_bytes_available = tstamp->copied_total; | |
17ac8e5c | 168 | return 0; |
b21c60a4 VK |
169 | } |
170 | ||
171 | static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, | |
172 | struct snd_compr_avail *avail) | |
173 | { | |
17ac8e5c | 174 | memset(avail, 0, sizeof(*avail)); |
b21c60a4 | 175 | snd_compr_update_tstamp(stream, &avail->tstamp); |
17ac8e5c | 176 | /* Still need to return avail even if tstamp can't be filled in */ |
b21c60a4 | 177 | |
b21c60a4 | 178 | if (stream->runtime->total_bytes_available == 0 && |
5b1f79f7 CK |
179 | stream->runtime->state == SNDRV_PCM_STATE_SETUP && |
180 | stream->direction == SND_COMPRESS_PLAYBACK) { | |
b21c60a4 VK |
181 | pr_debug("detected init and someone forgot to do a write\n"); |
182 | return stream->runtime->buffer_size; | |
183 | } | |
184 | pr_debug("app wrote %lld, DSP consumed %lld\n", | |
185 | stream->runtime->total_bytes_available, | |
186 | stream->runtime->total_bytes_transferred); | |
187 | if (stream->runtime->total_bytes_available == | |
188 | stream->runtime->total_bytes_transferred) { | |
5b1f79f7 CK |
189 | if (stream->direction == SND_COMPRESS_PLAYBACK) { |
190 | pr_debug("both pointers are same, returning full avail\n"); | |
191 | return stream->runtime->buffer_size; | |
192 | } else { | |
193 | pr_debug("both pointers are same, returning no avail\n"); | |
194 | return 0; | |
195 | } | |
b21c60a4 VK |
196 | } |
197 | ||
5b1f79f7 CK |
198 | avail->avail = stream->runtime->total_bytes_available - |
199 | stream->runtime->total_bytes_transferred; | |
200 | if (stream->direction == SND_COMPRESS_PLAYBACK) | |
201 | avail->avail = stream->runtime->buffer_size - avail->avail; | |
202 | ||
4c28e32d CK |
203 | pr_debug("ret avail as %lld\n", avail->avail); |
204 | return avail->avail; | |
b21c60a4 VK |
205 | } |
206 | ||
207 | static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream) | |
208 | { | |
209 | struct snd_compr_avail avail; | |
210 | ||
211 | return snd_compr_calc_avail(stream, &avail); | |
212 | } | |
213 | ||
214 | static int | |
215 | snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg) | |
216 | { | |
217 | struct snd_compr_avail ioctl_avail; | |
218 | size_t avail; | |
219 | ||
220 | avail = snd_compr_calc_avail(stream, &ioctl_avail); | |
221 | ioctl_avail.avail = avail; | |
222 | ||
223 | if (copy_to_user((__u64 __user *)arg, | |
224 | &ioctl_avail, sizeof(ioctl_avail))) | |
225 | return -EFAULT; | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int snd_compr_write_data(struct snd_compr_stream *stream, | |
230 | const char __user *buf, size_t count) | |
231 | { | |
232 | void *dstn; | |
233 | size_t copy; | |
234 | struct snd_compr_runtime *runtime = stream->runtime; | |
f0283b58 CK |
235 | /* 64-bit Modulus */ |
236 | u64 app_pointer = div64_u64(runtime->total_bytes_available, | |
237 | runtime->buffer_size); | |
238 | app_pointer = runtime->total_bytes_available - | |
239 | (app_pointer * runtime->buffer_size); | |
b21c60a4 | 240 | |
f0283b58 | 241 | dstn = runtime->buffer + app_pointer; |
b21c60a4 | 242 | pr_debug("copying %ld at %lld\n", |
f0283b58 CK |
243 | (unsigned long)count, app_pointer); |
244 | if (count < runtime->buffer_size - app_pointer) { | |
b21c60a4 VK |
245 | if (copy_from_user(dstn, buf, count)) |
246 | return -EFAULT; | |
b21c60a4 | 247 | } else { |
f0283b58 | 248 | copy = runtime->buffer_size - app_pointer; |
b21c60a4 VK |
249 | if (copy_from_user(dstn, buf, copy)) |
250 | return -EFAULT; | |
251 | if (copy_from_user(runtime->buffer, buf + copy, count - copy)) | |
252 | return -EFAULT; | |
b21c60a4 VK |
253 | } |
254 | /* if DSP cares, let it know data has been written */ | |
255 | if (stream->ops->ack) | |
256 | stream->ops->ack(stream, count); | |
257 | return count; | |
258 | } | |
259 | ||
260 | static ssize_t snd_compr_write(struct file *f, const char __user *buf, | |
261 | size_t count, loff_t *offset) | |
262 | { | |
263 | struct snd_compr_file *data = f->private_data; | |
264 | struct snd_compr_stream *stream; | |
265 | size_t avail; | |
266 | int retval; | |
267 | ||
268 | if (snd_BUG_ON(!data)) | |
269 | return -EFAULT; | |
270 | ||
271 | stream = &data->stream; | |
272 | mutex_lock(&stream->device->lock); | |
273 | /* write is allowed when stream is running or has been steup */ | |
274 | if (stream->runtime->state != SNDRV_PCM_STATE_SETUP && | |
275 | stream->runtime->state != SNDRV_PCM_STATE_RUNNING) { | |
276 | mutex_unlock(&stream->device->lock); | |
277 | return -EBADFD; | |
278 | } | |
279 | ||
280 | avail = snd_compr_get_avail(stream); | |
281 | pr_debug("avail returned %ld\n", (unsigned long)avail); | |
282 | /* calculate how much we can write to buffer */ | |
283 | if (avail > count) | |
284 | avail = count; | |
285 | ||
4daf891c CK |
286 | if (stream->ops->copy) { |
287 | char __user* cbuf = (char __user*)buf; | |
288 | retval = stream->ops->copy(stream, cbuf, avail); | |
289 | } else { | |
b21c60a4 | 290 | retval = snd_compr_write_data(stream, buf, avail); |
4daf891c | 291 | } |
b21c60a4 VK |
292 | if (retval > 0) |
293 | stream->runtime->total_bytes_available += retval; | |
294 | ||
295 | /* while initiating the stream, write should be called before START | |
296 | * call, so in setup move state */ | |
297 | if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { | |
298 | stream->runtime->state = SNDRV_PCM_STATE_PREPARED; | |
299 | pr_debug("stream prepared, Houston we are good to go\n"); | |
300 | } | |
301 | ||
302 | mutex_unlock(&stream->device->lock); | |
303 | return retval; | |
304 | } | |
305 | ||
306 | ||
307 | static ssize_t snd_compr_read(struct file *f, char __user *buf, | |
308 | size_t count, loff_t *offset) | |
309 | { | |
49bb6402 CK |
310 | struct snd_compr_file *data = f->private_data; |
311 | struct snd_compr_stream *stream; | |
312 | size_t avail; | |
313 | int retval; | |
314 | ||
315 | if (snd_BUG_ON(!data)) | |
316 | return -EFAULT; | |
317 | ||
318 | stream = &data->stream; | |
319 | mutex_lock(&stream->device->lock); | |
320 | ||
75481347 VK |
321 | /* read is allowed when stream is running, paused, draining and setup |
322 | * (yes setup is state which we transition to after stop, so if user | |
323 | * wants to read data after stop we allow that) | |
324 | */ | |
325 | switch (stream->runtime->state) { | |
326 | case SNDRV_PCM_STATE_OPEN: | |
327 | case SNDRV_PCM_STATE_PREPARED: | |
328 | case SNDRV_PCM_STATE_XRUN: | |
329 | case SNDRV_PCM_STATE_SUSPENDED: | |
330 | case SNDRV_PCM_STATE_DISCONNECTED: | |
49bb6402 CK |
331 | retval = -EBADFD; |
332 | goto out; | |
333 | } | |
334 | ||
335 | avail = snd_compr_get_avail(stream); | |
336 | pr_debug("avail returned %ld\n", (unsigned long)avail); | |
337 | /* calculate how much we can read from buffer */ | |
338 | if (avail > count) | |
339 | avail = count; | |
340 | ||
341 | if (stream->ops->copy) { | |
342 | retval = stream->ops->copy(stream, buf, avail); | |
343 | } else { | |
344 | retval = -ENXIO; | |
345 | goto out; | |
346 | } | |
347 | if (retval > 0) | |
348 | stream->runtime->total_bytes_transferred += retval; | |
349 | ||
350 | out: | |
351 | mutex_unlock(&stream->device->lock); | |
352 | return retval; | |
b21c60a4 VK |
353 | } |
354 | ||
355 | static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) | |
356 | { | |
357 | return -ENXIO; | |
358 | } | |
359 | ||
360 | static inline int snd_compr_get_poll(struct snd_compr_stream *stream) | |
361 | { | |
362 | if (stream->direction == SND_COMPRESS_PLAYBACK) | |
363 | return POLLOUT | POLLWRNORM; | |
364 | else | |
365 | return POLLIN | POLLRDNORM; | |
366 | } | |
367 | ||
368 | static unsigned int snd_compr_poll(struct file *f, poll_table *wait) | |
369 | { | |
370 | struct snd_compr_file *data = f->private_data; | |
371 | struct snd_compr_stream *stream; | |
372 | size_t avail; | |
373 | int retval = 0; | |
374 | ||
375 | if (snd_BUG_ON(!data)) | |
376 | return -EFAULT; | |
377 | stream = &data->stream; | |
378 | if (snd_BUG_ON(!stream)) | |
379 | return -EFAULT; | |
380 | ||
381 | mutex_lock(&stream->device->lock); | |
382 | if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED || | |
383 | stream->runtime->state == SNDRV_PCM_STATE_OPEN) { | |
384 | retval = -EBADFD; | |
385 | goto out; | |
386 | } | |
387 | poll_wait(f, &stream->runtime->sleep, wait); | |
388 | ||
389 | avail = snd_compr_get_avail(stream); | |
390 | pr_debug("avail is %ld\n", (unsigned long)avail); | |
391 | /* check if we have at least one fragment to fill */ | |
392 | switch (stream->runtime->state) { | |
393 | case SNDRV_PCM_STATE_DRAINING: | |
394 | /* stream has been woken up after drain is complete | |
395 | * draining done so set stream state to stopped | |
396 | */ | |
397 | retval = snd_compr_get_poll(stream); | |
398 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; | |
399 | break; | |
400 | case SNDRV_PCM_STATE_RUNNING: | |
401 | case SNDRV_PCM_STATE_PREPARED: | |
402 | case SNDRV_PCM_STATE_PAUSED: | |
403 | if (avail >= stream->runtime->fragment_size) | |
404 | retval = snd_compr_get_poll(stream); | |
405 | break; | |
406 | default: | |
407 | if (stream->direction == SND_COMPRESS_PLAYBACK) | |
408 | retval = POLLOUT | POLLWRNORM | POLLERR; | |
409 | else | |
410 | retval = POLLIN | POLLRDNORM | POLLERR; | |
411 | break; | |
412 | } | |
413 | out: | |
414 | mutex_unlock(&stream->device->lock); | |
415 | return retval; | |
416 | } | |
417 | ||
418 | static int | |
419 | snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg) | |
420 | { | |
421 | int retval; | |
422 | struct snd_compr_caps caps; | |
423 | ||
424 | if (!stream->ops->get_caps) | |
425 | return -ENXIO; | |
426 | ||
1c62e9f2 | 427 | memset(&caps, 0, sizeof(caps)); |
b21c60a4 VK |
428 | retval = stream->ops->get_caps(stream, &caps); |
429 | if (retval) | |
430 | goto out; | |
431 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) | |
432 | retval = -EFAULT; | |
433 | out: | |
434 | return retval; | |
435 | } | |
436 | ||
074e46b6 | 437 | #ifndef COMPR_CODEC_CAPS_OVERFLOW |
b21c60a4 VK |
438 | static int |
439 | snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) | |
440 | { | |
441 | int retval; | |
442 | struct snd_compr_codec_caps *caps; | |
443 | ||
444 | if (!stream->ops->get_codec_caps) | |
445 | return -ENXIO; | |
446 | ||
47966e97 | 447 | caps = kzalloc(sizeof(*caps), GFP_KERNEL); |
b21c60a4 VK |
448 | if (!caps) |
449 | return -ENOMEM; | |
450 | ||
451 | retval = stream->ops->get_codec_caps(stream, caps); | |
452 | if (retval) | |
453 | goto out; | |
454 | if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) | |
455 | retval = -EFAULT; | |
456 | ||
457 | out: | |
458 | kfree(caps); | |
459 | return retval; | |
460 | } | |
074e46b6 | 461 | #endif /* !COMPR_CODEC_CAPS_OVERFLOW */ |
b21c60a4 VK |
462 | |
463 | /* revisit this with snd_pcm_preallocate_xxx */ | |
464 | static int snd_compr_allocate_buffer(struct snd_compr_stream *stream, | |
465 | struct snd_compr_params *params) | |
466 | { | |
467 | unsigned int buffer_size; | |
468 | void *buffer; | |
469 | ||
470 | buffer_size = params->buffer.fragment_size * params->buffer.fragments; | |
471 | if (stream->ops->copy) { | |
472 | buffer = NULL; | |
473 | /* if copy is defined the driver will be required to copy | |
474 | * the data from core | |
475 | */ | |
476 | } else { | |
477 | buffer = kmalloc(buffer_size, GFP_KERNEL); | |
478 | if (!buffer) | |
479 | return -ENOMEM; | |
480 | } | |
481 | stream->runtime->fragment_size = params->buffer.fragment_size; | |
482 | stream->runtime->fragments = params->buffer.fragments; | |
483 | stream->runtime->buffer = buffer; | |
484 | stream->runtime->buffer_size = buffer_size; | |
485 | return 0; | |
486 | } | |
487 | ||
4dc040a0 VK |
488 | static int snd_compress_check_input(struct snd_compr_params *params) |
489 | { | |
490 | /* first let's check the buffer parameter's */ | |
491 | if (params->buffer.fragment_size == 0 || | |
4b9e9796 | 492 | params->buffer.fragments > INT_MAX / params->buffer.fragment_size) |
4dc040a0 VK |
493 | return -EINVAL; |
494 | ||
fb4a9779 VK |
495 | /* now codec parameters */ |
496 | if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX) | |
497 | return -EINVAL; | |
498 | ||
499 | if (params->codec.ch_in == 0 || params->codec.ch_out == 0) | |
500 | return -EINVAL; | |
501 | ||
502 | if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000)) | |
503 | return -EINVAL; | |
504 | ||
4dc040a0 VK |
505 | return 0; |
506 | } | |
507 | ||
b21c60a4 VK |
508 | static int |
509 | snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) | |
510 | { | |
511 | struct snd_compr_params *params; | |
512 | int retval; | |
513 | ||
514 | if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { | |
515 | /* | |
516 | * we should allow parameter change only when stream has been | |
517 | * opened not in other cases | |
518 | */ | |
519 | params = kmalloc(sizeof(*params), GFP_KERNEL); | |
520 | if (!params) | |
521 | return -ENOMEM; | |
769fab2a JJ |
522 | if (copy_from_user(params, (void __user *)arg, sizeof(*params))) { |
523 | retval = -EFAULT; | |
524 | goto out; | |
525 | } | |
4dc040a0 VK |
526 | |
527 | retval = snd_compress_check_input(params); | |
528 | if (retval) | |
529 | goto out; | |
530 | ||
b21c60a4 VK |
531 | retval = snd_compr_allocate_buffer(stream, params); |
532 | if (retval) { | |
769fab2a JJ |
533 | retval = -ENOMEM; |
534 | goto out; | |
b21c60a4 | 535 | } |
4dc040a0 | 536 | |
b21c60a4 VK |
537 | retval = stream->ops->set_params(stream, params); |
538 | if (retval) | |
539 | goto out; | |
49bb6402 | 540 | |
9727b490 JK |
541 | stream->metadata_set = false; |
542 | stream->next_track = false; | |
49bb6402 CK |
543 | |
544 | if (stream->direction == SND_COMPRESS_PLAYBACK) | |
545 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; | |
546 | else | |
547 | stream->runtime->state = SNDRV_PCM_STATE_PREPARED; | |
769fab2a | 548 | } else { |
b21c60a4 | 549 | return -EPERM; |
769fab2a | 550 | } |
b21c60a4 VK |
551 | out: |
552 | kfree(params); | |
553 | return retval; | |
554 | } | |
555 | ||
556 | static int | |
557 | snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) | |
558 | { | |
559 | struct snd_codec *params; | |
560 | int retval; | |
561 | ||
562 | if (!stream->ops->get_params) | |
563 | return -EBADFD; | |
564 | ||
47966e97 | 565 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
b21c60a4 VK |
566 | if (!params) |
567 | return -ENOMEM; | |
568 | retval = stream->ops->get_params(stream, params); | |
569 | if (retval) | |
570 | goto out; | |
571 | if (copy_to_user((char __user *)arg, params, sizeof(*params))) | |
572 | retval = -EFAULT; | |
573 | ||
574 | out: | |
575 | kfree(params); | |
576 | return retval; | |
577 | } | |
578 | ||
9727b490 JK |
579 | static int |
580 | snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg) | |
581 | { | |
582 | struct snd_compr_metadata metadata; | |
583 | int retval; | |
584 | ||
585 | if (!stream->ops->get_metadata) | |
586 | return -ENXIO; | |
587 | ||
588 | if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) | |
589 | return -EFAULT; | |
590 | ||
591 | retval = stream->ops->get_metadata(stream, &metadata); | |
592 | if (retval != 0) | |
593 | return retval; | |
594 | ||
595 | if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata))) | |
596 | return -EFAULT; | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | static int | |
602 | snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg) | |
603 | { | |
604 | struct snd_compr_metadata metadata; | |
605 | int retval; | |
606 | ||
607 | if (!stream->ops->set_metadata) | |
608 | return -ENXIO; | |
609 | /* | |
610 | * we should allow parameter change only when stream has been | |
611 | * opened not in other cases | |
612 | */ | |
613 | if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) | |
614 | return -EFAULT; | |
615 | ||
616 | retval = stream->ops->set_metadata(stream, &metadata); | |
617 | stream->metadata_set = true; | |
618 | ||
619 | return retval; | |
620 | } | |
621 | ||
b21c60a4 VK |
622 | static inline int |
623 | snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) | |
624 | { | |
17ac8e5c RF |
625 | struct snd_compr_tstamp tstamp = {0}; |
626 | int ret; | |
b21c60a4 | 627 | |
17ac8e5c RF |
628 | ret = snd_compr_update_tstamp(stream, &tstamp); |
629 | if (ret == 0) | |
630 | ret = copy_to_user((struct snd_compr_tstamp __user *)arg, | |
631 | &tstamp, sizeof(tstamp)) ? -EFAULT : 0; | |
632 | return ret; | |
b21c60a4 VK |
633 | } |
634 | ||
635 | static int snd_compr_pause(struct snd_compr_stream *stream) | |
636 | { | |
637 | int retval; | |
638 | ||
639 | if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) | |
640 | return -EPERM; | |
641 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); | |
6b18f793 | 642 | if (!retval) |
b21c60a4 | 643 | stream->runtime->state = SNDRV_PCM_STATE_PAUSED; |
b21c60a4 VK |
644 | return retval; |
645 | } | |
646 | ||
647 | static int snd_compr_resume(struct snd_compr_stream *stream) | |
648 | { | |
649 | int retval; | |
650 | ||
651 | if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED) | |
652 | return -EPERM; | |
653 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); | |
654 | if (!retval) | |
655 | stream->runtime->state = SNDRV_PCM_STATE_RUNNING; | |
656 | return retval; | |
657 | } | |
658 | ||
659 | static int snd_compr_start(struct snd_compr_stream *stream) | |
660 | { | |
661 | int retval; | |
662 | ||
663 | if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED) | |
664 | return -EPERM; | |
665 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START); | |
666 | if (!retval) | |
667 | stream->runtime->state = SNDRV_PCM_STATE_RUNNING; | |
668 | return retval; | |
669 | } | |
670 | ||
671 | static int snd_compr_stop(struct snd_compr_stream *stream) | |
672 | { | |
673 | int retval; | |
674 | ||
675 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || | |
676 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) | |
677 | return -EPERM; | |
678 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); | |
679 | if (!retval) { | |
16442d4f | 680 | snd_compr_drain_notify(stream); |
8b21460a VK |
681 | stream->runtime->total_bytes_available = 0; |
682 | stream->runtime->total_bytes_transferred = 0; | |
b21c60a4 VK |
683 | } |
684 | return retval; | |
685 | } | |
686 | ||
16442d4f VK |
687 | static int snd_compress_wait_for_drain(struct snd_compr_stream *stream) |
688 | { | |
86e6de78 VK |
689 | int ret; |
690 | ||
16442d4f VK |
691 | /* |
692 | * We are called with lock held. So drop the lock while we wait for | |
693 | * drain complete notfication from the driver | |
694 | * | |
695 | * It is expected that driver will notify the drain completion and then | |
696 | * stream will be moved to SETUP state, even if draining resulted in an | |
697 | * error. We can trigger next track after this. | |
698 | */ | |
699 | stream->runtime->state = SNDRV_PCM_STATE_DRAINING; | |
700 | mutex_unlock(&stream->device->lock); | |
701 | ||
86e6de78 VK |
702 | /* we wait for drain to complete here, drain can return when |
703 | * interruption occurred, wait returned error or success. | |
704 | * For the first two cases we don't do anything different here and | |
705 | * return after waking up | |
706 | */ | |
707 | ||
708 | ret = wait_event_interruptible(stream->runtime->sleep, | |
709 | (stream->runtime->state != SNDRV_PCM_STATE_DRAINING)); | |
710 | if (ret == -ERESTARTSYS) | |
711 | pr_debug("wait aborted by a signal"); | |
712 | else if (ret) | |
713 | pr_debug("wait for drain failed with %d\n", ret); | |
714 | ||
16442d4f VK |
715 | |
716 | wake_up(&stream->runtime->sleep); | |
717 | mutex_lock(&stream->device->lock); | |
718 | ||
86e6de78 | 719 | return ret; |
16442d4f VK |
720 | } |
721 | ||
b21c60a4 VK |
722 | static int snd_compr_drain(struct snd_compr_stream *stream) |
723 | { | |
724 | int retval; | |
725 | ||
726 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || | |
727 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) | |
728 | return -EPERM; | |
16442d4f | 729 | |
b21c60a4 | 730 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); |
16442d4f | 731 | if (retval) { |
86e6de78 | 732 | pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval); |
b21c60a4 | 733 | wake_up(&stream->runtime->sleep); |
16442d4f | 734 | return retval; |
b21c60a4 | 735 | } |
16442d4f | 736 | |
86e6de78 | 737 | return snd_compress_wait_for_drain(stream); |
b21c60a4 VK |
738 | } |
739 | ||
9727b490 JK |
740 | static int snd_compr_next_track(struct snd_compr_stream *stream) |
741 | { | |
742 | int retval; | |
743 | ||
744 | /* only a running stream can transition to next track */ | |
745 | if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) | |
746 | return -EPERM; | |
747 | ||
748 | /* you can signal next track isf this is intended to be a gapless stream | |
749 | * and current track metadata is set | |
750 | */ | |
751 | if (stream->metadata_set == false) | |
752 | return -EPERM; | |
753 | ||
754 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK); | |
755 | if (retval != 0) | |
756 | return retval; | |
757 | stream->metadata_set = false; | |
758 | stream->next_track = true; | |
759 | return 0; | |
760 | } | |
761 | ||
762 | static int snd_compr_partial_drain(struct snd_compr_stream *stream) | |
763 | { | |
764 | int retval; | |
765 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || | |
766 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) | |
767 | return -EPERM; | |
768 | /* stream can be drained only when next track has been signalled */ | |
769 | if (stream->next_track == false) | |
770 | return -EPERM; | |
771 | ||
772 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN); | |
16442d4f | 773 | if (retval) { |
86e6de78 | 774 | pr_debug("Partial drain returned failure\n"); |
16442d4f VK |
775 | wake_up(&stream->runtime->sleep); |
776 | return retval; | |
777 | } | |
9727b490 JK |
778 | |
779 | stream->next_track = false; | |
16442d4f | 780 | return snd_compress_wait_for_drain(stream); |
9727b490 JK |
781 | } |
782 | ||
b21c60a4 VK |
783 | static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
784 | { | |
785 | struct snd_compr_file *data = f->private_data; | |
786 | struct snd_compr_stream *stream; | |
787 | int retval = -ENOTTY; | |
788 | ||
789 | if (snd_BUG_ON(!data)) | |
790 | return -EFAULT; | |
791 | stream = &data->stream; | |
792 | if (snd_BUG_ON(!stream)) | |
793 | return -EFAULT; | |
794 | mutex_lock(&stream->device->lock); | |
795 | switch (_IOC_NR(cmd)) { | |
796 | case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): | |
d54d012b | 797 | retval = put_user(SNDRV_COMPRESS_VERSION, |
b21c60a4 VK |
798 | (int __user *)arg) ? -EFAULT : 0; |
799 | break; | |
800 | case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): | |
801 | retval = snd_compr_get_caps(stream, arg); | |
802 | break; | |
074e46b6 | 803 | #ifndef COMPR_CODEC_CAPS_OVERFLOW |
b21c60a4 VK |
804 | case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): |
805 | retval = snd_compr_get_codec_caps(stream, arg); | |
806 | break; | |
074e46b6 | 807 | #endif |
b21c60a4 VK |
808 | case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): |
809 | retval = snd_compr_set_params(stream, arg); | |
810 | break; | |
811 | case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): | |
812 | retval = snd_compr_get_params(stream, arg); | |
813 | break; | |
9727b490 JK |
814 | case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): |
815 | retval = snd_compr_set_metadata(stream, arg); | |
816 | break; | |
817 | case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): | |
818 | retval = snd_compr_get_metadata(stream, arg); | |
819 | break; | |
b21c60a4 VK |
820 | case _IOC_NR(SNDRV_COMPRESS_TSTAMP): |
821 | retval = snd_compr_tstamp(stream, arg); | |
822 | break; | |
823 | case _IOC_NR(SNDRV_COMPRESS_AVAIL): | |
824 | retval = snd_compr_ioctl_avail(stream, arg); | |
825 | break; | |
826 | case _IOC_NR(SNDRV_COMPRESS_PAUSE): | |
827 | retval = snd_compr_pause(stream); | |
828 | break; | |
829 | case _IOC_NR(SNDRV_COMPRESS_RESUME): | |
830 | retval = snd_compr_resume(stream); | |
831 | break; | |
832 | case _IOC_NR(SNDRV_COMPRESS_START): | |
833 | retval = snd_compr_start(stream); | |
834 | break; | |
835 | case _IOC_NR(SNDRV_COMPRESS_STOP): | |
836 | retval = snd_compr_stop(stream); | |
837 | break; | |
838 | case _IOC_NR(SNDRV_COMPRESS_DRAIN): | |
839 | retval = snd_compr_drain(stream); | |
840 | break; | |
9727b490 JK |
841 | case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): |
842 | retval = snd_compr_partial_drain(stream); | |
843 | break; | |
844 | case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): | |
845 | retval = snd_compr_next_track(stream); | |
846 | break; | |
847 | ||
b21c60a4 VK |
848 | } |
849 | mutex_unlock(&stream->device->lock); | |
850 | return retval; | |
851 | } | |
852 | ||
853 | static const struct file_operations snd_compr_file_ops = { | |
854 | .owner = THIS_MODULE, | |
855 | .open = snd_compr_open, | |
856 | .release = snd_compr_free, | |
857 | .write = snd_compr_write, | |
858 | .read = snd_compr_read, | |
859 | .unlocked_ioctl = snd_compr_ioctl, | |
860 | .mmap = snd_compr_mmap, | |
861 | .poll = snd_compr_poll, | |
862 | }; | |
863 | ||
864 | static int snd_compress_dev_register(struct snd_device *device) | |
865 | { | |
866 | int ret = -EINVAL; | |
867 | char str[16]; | |
868 | struct snd_compr *compr; | |
869 | ||
870 | if (snd_BUG_ON(!device || !device->device_data)) | |
871 | return -EBADFD; | |
872 | compr = device->device_data; | |
873 | ||
874 | sprintf(str, "comprC%iD%i", compr->card->number, compr->device); | |
875 | pr_debug("reg %s for device %s, direction %d\n", str, compr->name, | |
876 | compr->direction); | |
877 | /* register compressed device */ | |
878 | ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, | |
879 | compr->device, &snd_compr_file_ops, compr, str); | |
880 | if (ret < 0) { | |
881 | pr_err("snd_register_device failed\n %d", ret); | |
882 | return ret; | |
883 | } | |
884 | return ret; | |
885 | ||
886 | } | |
887 | ||
888 | static int snd_compress_dev_disconnect(struct snd_device *device) | |
889 | { | |
890 | struct snd_compr *compr; | |
891 | ||
892 | compr = device->device_data; | |
1aff4161 LG |
893 | snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, |
894 | compr->device); | |
b21c60a4 VK |
895 | return 0; |
896 | } | |
897 | ||
898 | /* | |
899 | * snd_compress_new: create new compress device | |
900 | * @card: sound card pointer | |
901 | * @device: device number | |
902 | * @dirn: device direction, should be of type enum snd_compr_direction | |
903 | * @compr: compress device pointer | |
904 | */ | |
905 | int snd_compress_new(struct snd_card *card, int device, | |
906 | int dirn, struct snd_compr *compr) | |
907 | { | |
908 | static struct snd_device_ops ops = { | |
909 | .dev_free = NULL, | |
910 | .dev_register = snd_compress_dev_register, | |
911 | .dev_disconnect = snd_compress_dev_disconnect, | |
912 | }; | |
913 | ||
914 | compr->card = card; | |
915 | compr->device = device; | |
916 | compr->direction = dirn; | |
917 | return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); | |
918 | } | |
919 | EXPORT_SYMBOL_GPL(snd_compress_new); | |
920 | ||
921 | static int snd_compress_add_device(struct snd_compr *device) | |
922 | { | |
923 | int ret; | |
924 | ||
925 | if (!device->card) | |
926 | return -EINVAL; | |
927 | ||
928 | /* register the card */ | |
929 | ret = snd_card_register(device->card); | |
930 | if (ret) | |
931 | goto out; | |
932 | return 0; | |
933 | ||
934 | out: | |
935 | pr_err("failed with %d\n", ret); | |
936 | return ret; | |
937 | ||
938 | } | |
939 | ||
940 | static int snd_compress_remove_device(struct snd_compr *device) | |
941 | { | |
942 | return snd_card_free(device->card); | |
943 | } | |
944 | ||
945 | /** | |
946 | * snd_compress_register - register compressed device | |
947 | * | |
948 | * @device: compressed device to register | |
949 | */ | |
950 | int snd_compress_register(struct snd_compr *device) | |
951 | { | |
952 | int retval; | |
953 | ||
954 | if (device->name == NULL || device->dev == NULL || device->ops == NULL) | |
955 | return -EINVAL; | |
956 | ||
957 | pr_debug("Registering compressed device %s\n", device->name); | |
958 | if (snd_BUG_ON(!device->ops->open)) | |
959 | return -EINVAL; | |
960 | if (snd_BUG_ON(!device->ops->free)) | |
961 | return -EINVAL; | |
962 | if (snd_BUG_ON(!device->ops->set_params)) | |
963 | return -EINVAL; | |
964 | if (snd_BUG_ON(!device->ops->trigger)) | |
965 | return -EINVAL; | |
966 | ||
967 | mutex_init(&device->lock); | |
968 | ||
969 | /* register a compressed card */ | |
970 | mutex_lock(&device_mutex); | |
971 | retval = snd_compress_add_device(device); | |
972 | mutex_unlock(&device_mutex); | |
973 | return retval; | |
974 | } | |
975 | EXPORT_SYMBOL_GPL(snd_compress_register); | |
976 | ||
977 | int snd_compress_deregister(struct snd_compr *device) | |
978 | { | |
979 | pr_debug("Removing compressed device %s\n", device->name); | |
980 | mutex_lock(&device_mutex); | |
981 | snd_compress_remove_device(device); | |
982 | mutex_unlock(&device_mutex); | |
983 | return 0; | |
984 | } | |
985 | EXPORT_SYMBOL_GPL(snd_compress_deregister); | |
986 | ||
987 | static int __init snd_compress_init(void) | |
988 | { | |
989 | return 0; | |
990 | } | |
991 | ||
992 | static void __exit snd_compress_exit(void) | |
993 | { | |
994 | } | |
995 | ||
996 | module_init(snd_compress_init); | |
997 | module_exit(snd_compress_exit); | |
998 | ||
999 | MODULE_DESCRIPTION("ALSA Compressed offload framework"); | |
1000 | MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); | |
1001 | MODULE_LICENSE("GPL v2"); |