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