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