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