compress_offload.c 30.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  compress_core.c - compress offload core
 *
 *  Copyright (C) 2011 Intel Corporation
 *  Authors:	Vinod Koul <vinod.koul@linux.intel.com>
 *		Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
#define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)

#include <linux/file.h>
#include <linux/fs.h>
#include <linux/list.h>
18
#include <linux/math64.h>
19 20 21 22 23
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/sched.h>
24
#include <linux/types.h>
25 26 27
#include <linux/uio.h>
#include <linux/uaccess.h>
#include <linux/module.h>
28
#include <linux/compat.h>
29 30
#include <sound/core.h>
#include <sound/initval.h>
31
#include <sound/info.h>
32 33 34 35
#include <sound/compress_params.h>
#include <sound/compress_offload.h>
#include <sound/compress_driver.h>

36 37 38 39 40 41 42
/* struct snd_compr_codec_caps overflows the ioctl bit size for some
 * architectures, so we need to disable the relevant ioctls.
 */
#if _IOC_SIZEBITS < 14
#define COMPR_CODEC_CAPS_OVERFLOW
#endif

43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* TODO:
 * - add substream support for multiple devices in case of
 *	SND_DYNAMIC_MINORS is not used
 * - Multiple node representation
 *	driver should be able to register multiple nodes
 */

static DEFINE_MUTEX(device_mutex);

struct snd_compr_file {
	unsigned long caps;
	struct snd_compr_stream stream;
};

57 58
static void error_delayed_work(struct work_struct *work);

59 60
/*
 * a note on stream states used:
61
 * we use following states in the compressed core
62 63
 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
 * 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 67 68
 * SNDRV_PCM_STATE_PREPARED: When a stream has been written to (for
 *	playback only). User after setting up stream writes the data buffer
 *	before starting the stream.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
 *	decoding/encoding and rendering/capturing data.
 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
 *	by calling SNDRV_COMPRESS_DRAIN.
 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
 *	SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
 *	SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
 */
static int snd_compr_open(struct inode *inode, struct file *f)
{
	struct snd_compr *compr;
	struct snd_compr_file *data;
	struct snd_compr_runtime *runtime;
	enum snd_compr_direction dirn;
	int maj = imajor(inode);
	int ret;

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		return -EINVAL;

	if (maj == snd_major)
		compr = snd_lookup_minor_data(iminor(inode),
					SNDRV_DEVICE_TYPE_COMPRESS);
	else
		return -EBADFD;

	if (compr == NULL) {
		pr_err("no device data!!!\n");
		return -ENODEV;
	}

	if (dirn != compr->direction) {
		pr_err("this device doesn't support this direction\n");
106
		snd_card_unref(compr->card);
107 108 109 110
		return -EINVAL;
	}

	data = kzalloc(sizeof(*data), GFP_KERNEL);
111 112
	if (!data) {
		snd_card_unref(compr->card);
113
		return -ENOMEM;
114
	}
115 116 117

	INIT_DELAYED_WORK(&data->stream.error_work, error_delayed_work);

118 119 120 121 122 123 124
	data->stream.ops = compr->ops;
	data->stream.direction = dirn;
	data->stream.private_data = compr->private_data;
	data->stream.device = compr;
	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
	if (!runtime) {
		kfree(data);
125
		snd_card_unref(compr->card);
126 127 128 129 130 131 132 133 134 135 136 137 138
		return -ENOMEM;
	}
	runtime->state = SNDRV_PCM_STATE_OPEN;
	init_waitqueue_head(&runtime->sleep);
	data->stream.runtime = runtime;
	f->private_data = (void *)data;
	mutex_lock(&compr->lock);
	ret = compr->ops->open(&data->stream);
	mutex_unlock(&compr->lock);
	if (ret) {
		kfree(runtime);
		kfree(data);
	}
139
	snd_card_unref(compr->card);
140
	return ret;
141 142 143 144 145
}

static int snd_compr_free(struct inode *inode, struct file *f)
{
	struct snd_compr_file *data = f->private_data;
146 147
	struct snd_compr_runtime *runtime = data->stream.runtime;

148 149
	cancel_delayed_work_sync(&data->stream.error_work);

150 151 152 153 154 155 156 157 158 159
	switch (runtime->state) {
	case SNDRV_PCM_STATE_RUNNING:
	case SNDRV_PCM_STATE_DRAINING:
	case SNDRV_PCM_STATE_PAUSED:
		data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
		break;
	default:
		break;
	}

160
	data->stream.ops->free(&data->stream);
161 162
	if (!data->stream.runtime->dma_buffer_p)
		kfree(data->stream.runtime->buffer);
163 164 165 166 167
	kfree(data->stream.runtime);
	kfree(data);
	return 0;
}

168
static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
169 170 171
		struct snd_compr_tstamp *tstamp)
{
	if (!stream->ops->pointer)
172
		return -ENOTSUPP;
173 174 175
	stream->ops->pointer(stream, tstamp);
	pr_debug("dsp consumed till %d total %d bytes\n",
		tstamp->byte_offset, tstamp->copied_total);
176 177 178 179
	if (stream->direction == SND_COMPRESS_PLAYBACK)
		stream->runtime->total_bytes_transferred = tstamp->copied_total;
	else
		stream->runtime->total_bytes_available = tstamp->copied_total;
180
	return 0;
181 182 183 184 185
}

static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
		struct snd_compr_avail *avail)
{
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 192
			stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
			stream->direction == SND_COMPRESS_PLAYBACK) {
193 194 195 196 197 198 199 200
		pr_debug("detected init and someone forgot to do a write\n");
		return stream->runtime->buffer_size;
	}
	pr_debug("app wrote %lld, DSP consumed %lld\n",
			stream->runtime->total_bytes_available,
			stream->runtime->total_bytes_transferred);
	if (stream->runtime->total_bytes_available ==
				stream->runtime->total_bytes_transferred) {
201 202 203 204 205 206 207
		if (stream->direction == SND_COMPRESS_PLAYBACK) {
			pr_debug("both pointers are same, returning full avail\n");
			return stream->runtime->buffer_size;
		} else {
			pr_debug("both pointers are same, returning no avail\n");
			return 0;
		}
208 209
	}

210 211 212 213 214
	avail->avail = stream->runtime->total_bytes_available -
			stream->runtime->total_bytes_transferred;
	if (stream->direction == SND_COMPRESS_PLAYBACK)
		avail->avail = stream->runtime->buffer_size - avail->avail;

215 216
	pr_debug("ret avail as %lld\n", avail->avail);
	return avail->avail;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
{
	struct snd_compr_avail avail;

	return snd_compr_calc_avail(stream, &avail);
}

static int
snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
{
	struct snd_compr_avail ioctl_avail;
	size_t avail;

	avail = snd_compr_calc_avail(stream, &ioctl_avail);
	ioctl_avail.avail = avail;

235 236 237 238 239 240 241 242 243
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
		return -EBADFD;
	case SNDRV_PCM_STATE_XRUN:
		return -EPIPE;
	default:
		break;
	}

244 245 246 247 248 249 250 251 252 253 254 255
	if (copy_to_user((__u64 __user *)arg,
				&ioctl_avail, sizeof(ioctl_avail)))
		return -EFAULT;
	return 0;
}

static int snd_compr_write_data(struct snd_compr_stream *stream,
	       const char __user *buf, size_t count)
{
	void *dstn;
	size_t copy;
	struct snd_compr_runtime *runtime = stream->runtime;
256 257 258 259 260
	/* 64-bit Modulus */
	u64 app_pointer = div64_u64(runtime->total_bytes_available,
				    runtime->buffer_size);
	app_pointer = runtime->total_bytes_available -
		      (app_pointer * runtime->buffer_size);
261

262
	dstn = runtime->buffer + app_pointer;
263
	pr_debug("copying %ld at %lld\n",
264 265
			(unsigned long)count, app_pointer);
	if (count < runtime->buffer_size - app_pointer) {
266 267 268
		if (copy_from_user(dstn, buf, count))
			return -EFAULT;
	} else {
269
		copy = runtime->buffer_size - app_pointer;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		if (copy_from_user(dstn, buf, copy))
			return -EFAULT;
		if (copy_from_user(runtime->buffer, buf + copy, count - copy))
			return -EFAULT;
	}
	/* if DSP cares, let it know data has been written */
	if (stream->ops->ack)
		stream->ops->ack(stream, count);
	return count;
}

static ssize_t snd_compr_write(struct file *f, const char __user *buf,
		size_t count, loff_t *offset)
{
	struct snd_compr_file *data = f->private_data;
	struct snd_compr_stream *stream;
	size_t avail;
	int retval;

	if (snd_BUG_ON(!data))
		return -EFAULT;

	stream = &data->stream;
	mutex_lock(&stream->device->lock);
	/* write is allowed when stream is running or has been steup */
295 296 297 298 299 300
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
	case SNDRV_PCM_STATE_RUNNING:
		break;
	default:
301 302 303 304 305 306 307 308 309 310
		mutex_unlock(&stream->device->lock);
		return -EBADFD;
	}

	avail = snd_compr_get_avail(stream);
	pr_debug("avail returned %ld\n", (unsigned long)avail);
	/* calculate how much we can write to buffer */
	if (avail > count)
		avail = count;

311 312 313 314
	if (stream->ops->copy) {
		char __user* cbuf = (char __user*)buf;
		retval = stream->ops->copy(stream, cbuf, avail);
	} else {
315
		retval = snd_compr_write_data(stream, buf, avail);
316
	}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	if (retval > 0)
		stream->runtime->total_bytes_available += retval;

	/* while initiating the stream, write should be called before START
	 * call, so in setup move state */
	if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
		stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
		pr_debug("stream prepared, Houston we are good to go\n");
	}

	mutex_unlock(&stream->device->lock);
	return retval;
}


static ssize_t snd_compr_read(struct file *f, char __user *buf,
		size_t count, loff_t *offset)
{
335 336 337 338 339 340 341 342 343 344 345
	struct snd_compr_file *data = f->private_data;
	struct snd_compr_stream *stream;
	size_t avail;
	int retval;

	if (snd_BUG_ON(!data))
		return -EFAULT;

	stream = &data->stream;
	mutex_lock(&stream->device->lock);

346 347 348 349 350 351 352 353 354
	/* read is allowed when stream is running, paused, draining and setup
	 * (yes setup is state which we transition to after stop, so if user
	 * wants to read data after stop we allow that)
	 */
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_PREPARED:
	case SNDRV_PCM_STATE_SUSPENDED:
	case SNDRV_PCM_STATE_DISCONNECTED:
355 356
		retval = -EBADFD;
		goto out;
357 358 359
	case SNDRV_PCM_STATE_XRUN:
		retval = -EPIPE;
		goto out;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	}

	avail = snd_compr_get_avail(stream);
	pr_debug("avail returned %ld\n", (unsigned long)avail);
	/* calculate how much we can read from buffer */
	if (avail > count)
		avail = count;

	if (stream->ops->copy) {
		retval = stream->ops->copy(stream, buf, avail);
	} else {
		retval = -ENXIO;
		goto out;
	}
	if (retval > 0)
		stream->runtime->total_bytes_transferred += retval;

out:
	mutex_unlock(&stream->device->lock);
	return retval;
380 381 382 383 384 385 386
}

static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
{
	return -ENXIO;
}

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 398 399
{
	struct snd_compr_file *data = f->private_data;
	struct snd_compr_stream *stream;
	size_t avail;
400
	__poll_t retval = 0;
401 402

	if (snd_BUG_ON(!data))
403
		return EPOLLERR;
404

405 406 407
	stream = &data->stream;

	mutex_lock(&stream->device->lock);
408 409 410 411

	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_XRUN:
412
		retval = snd_compr_get_poll(stream) | EPOLLERR;
413
		goto out;
414 415
	default:
		break;
416
	}
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	poll_wait(f, &stream->runtime->sleep, wait);

	avail = snd_compr_get_avail(stream);
	pr_debug("avail is %ld\n", (unsigned long)avail);
	/* check if we have at least one fragment to fill */
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_DRAINING:
		/* stream has been woken up after drain is complete
		 * draining done so set stream state to stopped
		 */
		retval = snd_compr_get_poll(stream);
		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
		break;
	case SNDRV_PCM_STATE_RUNNING:
	case SNDRV_PCM_STATE_PREPARED:
	case SNDRV_PCM_STATE_PAUSED:
		if (avail >= stream->runtime->fragment_size)
			retval = snd_compr_get_poll(stream);
		break;
	default:
438
		retval = snd_compr_get_poll(stream) | EPOLLERR;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		break;
	}
out:
	mutex_unlock(&stream->device->lock);
	return retval;
}

static int
snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
{
	int retval;
	struct snd_compr_caps caps;

	if (!stream->ops->get_caps)
		return -ENXIO;

455
	memset(&caps, 0, sizeof(caps));
456 457 458 459 460 461 462 463 464
	retval = stream->ops->get_caps(stream, &caps);
	if (retval)
		goto out;
	if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
		retval = -EFAULT;
out:
	return retval;
}

465
#ifndef COMPR_CODEC_CAPS_OVERFLOW
466 467 468 469 470 471 472 473 474
static int
snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
{
	int retval;
	struct snd_compr_codec_caps *caps;

	if (!stream->ops->get_codec_caps)
		return -ENXIO;

475
	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
476 477 478 479 480 481 482 483 484 485 486 487 488
	if (!caps)
		return -ENOMEM;

	retval = stream->ops->get_codec_caps(stream, caps);
	if (retval)
		goto out;
	if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
		retval = -EFAULT;

out:
	kfree(caps);
	return retval;
}
489
#endif /* !COMPR_CODEC_CAPS_OVERFLOW */
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size)
{
	struct snd_dma_buffer *dmab;
	int ret;

	if (snd_BUG_ON(!(stream) || !(stream)->runtime))
		return -EINVAL;
	dmab = kzalloc(sizeof(*dmab), GFP_KERNEL);
	if (!dmab)
		return -ENOMEM;
	dmab->dev = stream->dma_buffer.dev;
	ret = snd_dma_alloc_pages(dmab->dev.type, dmab->dev.dev, size, dmab);
	if (ret < 0) {
		kfree(dmab);
		return ret;
	}

	snd_compr_set_runtime_buffer(stream, dmab);
	stream->runtime->dma_bytes = size;
	return 1;
}
EXPORT_SYMBOL(snd_compr_malloc_pages);

int snd_compr_free_pages(struct snd_compr_stream *stream)
{
516
	struct snd_compr_runtime *runtime;
517 518 519

	if (snd_BUG_ON(!(stream) || !(stream)->runtime))
		return -EINVAL;
520
	runtime = stream->runtime;
521 522 523 524 525 526 527 528 529 530 531 532 533
	if (runtime->dma_area == NULL)
		return 0;
	if (runtime->dma_buffer_p != &stream->dma_buffer) {
		/* It's a newly allocated buffer. Release it now. */
		snd_dma_free_pages(runtime->dma_buffer_p);
		kfree(runtime->dma_buffer_p);
	}

	snd_compr_set_runtime_buffer(stream, NULL);
	return 0;
}
EXPORT_SYMBOL(snd_compr_free_pages);

534 535 536 537 538
/* revisit this with snd_pcm_preallocate_xxx */
static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
		struct snd_compr_params *params)
{
	unsigned int buffer_size;
539
	void *buffer = NULL;
540 541 542 543 544 545 546 547

	buffer_size = params->buffer.fragment_size * params->buffer.fragments;
	if (stream->ops->copy) {
		buffer = NULL;
		/* if copy is defined the driver will be required to copy
		 * the data from core
		 */
	} else {
548 549 550 551 552 553 554 555 556 557 558 559
		if (stream->runtime->dma_buffer_p) {

			if (buffer_size > stream->runtime->dma_buffer_p->bytes)
				dev_err(&stream->device->dev,
						"Not enough DMA buffer");
			else
				buffer = stream->runtime->dma_buffer_p->area;

		} else {
			buffer = kmalloc(buffer_size, GFP_KERNEL);
		}

560 561 562 563 564 565 566 567 568 569
		if (!buffer)
			return -ENOMEM;
	}
	stream->runtime->fragment_size = params->buffer.fragment_size;
	stream->runtime->fragments = params->buffer.fragments;
	stream->runtime->buffer = buffer;
	stream->runtime->buffer_size = buffer_size;
	return 0;
}

570 571 572 573
static int snd_compress_check_input(struct snd_compr_params *params)
{
	/* first let's check the buffer parameter's */
	if (params->buffer.fragment_size == 0 ||
574
	    params->buffer.fragments > U32_MAX / params->buffer.fragment_size ||
575
	    params->buffer.fragments == 0)
576 577
		return -EINVAL;

578 579 580 581 582 583 584
	/* now codec parameters */
	if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
		return -EINVAL;

	if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
		return -EINVAL;

585 586 587
	return 0;
}

588 589 590 591 592 593 594 595 596 597 598
static int
snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
{
	struct snd_compr_params *params;
	int retval;

	if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
		/*
		 * we should allow parameter change only when stream has been
		 * opened not in other cases
		 */
599 600 601
		params = memdup_user((void __user *)arg, sizeof(*params));
		if (IS_ERR(params))
			return PTR_ERR(params);
602 603 604 605 606

		retval = snd_compress_check_input(params);
		if (retval)
			goto out;

607 608
		retval = snd_compr_allocate_buffer(stream, params);
		if (retval) {
609 610
			retval = -ENOMEM;
			goto out;
611
		}
612

613 614 615
		retval = stream->ops->set_params(stream, params);
		if (retval)
			goto out;
616

617 618
		stream->metadata_set = false;
		stream->next_track = false;
619

620
		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
621
	} else {
622
		return -EPERM;
623
	}
624 625 626 627 628 629 630 631 632 633 634 635 636 637
out:
	kfree(params);
	return retval;
}

static int
snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
{
	struct snd_codec *params;
	int retval;

	if (!stream->ops->get_params)
		return -EBADFD;

638
	params = kzalloc(sizeof(*params), GFP_KERNEL);
639 640 641 642 643 644 645 646 647 648 649 650 651
	if (!params)
		return -ENOMEM;
	retval = stream->ops->get_params(stream, params);
	if (retval)
		goto out;
	if (copy_to_user((char __user *)arg, params, sizeof(*params)))
		retval = -EFAULT;

out:
	kfree(params);
	return retval;
}

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
static int
snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
{
	struct snd_compr_metadata metadata;
	int retval;

	if (!stream->ops->get_metadata)
		return -ENXIO;

	if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
		return -EFAULT;

	retval = stream->ops->get_metadata(stream, &metadata);
	if (retval != 0)
		return retval;

	if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
		return -EFAULT;

	return 0;
}

static int
snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
{
	struct snd_compr_metadata metadata;
	int retval;

	if (!stream->ops->set_metadata)
		return -ENXIO;
	/*
	* we should allow parameter change only when stream has been
	* opened not in other cases
	*/
	if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
		return -EFAULT;

	retval = stream->ops->set_metadata(stream, &metadata);
	stream->metadata_set = true;

	return retval;
}

695 696 697
static inline int
snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
{
698 699
	struct snd_compr_tstamp tstamp = {0};
	int ret;
700

701 702 703 704 705
	ret = snd_compr_update_tstamp(stream, &tstamp);
	if (ret == 0)
		ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
			&tstamp, sizeof(tstamp)) ? -EFAULT : 0;
	return ret;
706 707 708 709 710 711 712 713 714
}

static int snd_compr_pause(struct snd_compr_stream *stream)
{
	int retval;

	if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
		return -EPERM;
	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
715
	if (!retval)
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
		stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
	return retval;
}

static int snd_compr_resume(struct snd_compr_stream *stream)
{
	int retval;

	if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
		return -EPERM;
	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
	if (!retval)
		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
	return retval;
}

static int snd_compr_start(struct snd_compr_stream *stream)
{
	int retval;

736 737 738 739 740 741 742 743
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_SETUP:
		if (stream->direction != SND_COMPRESS_CAPTURE)
			return -EPERM;
		break;
	case SNDRV_PCM_STATE_PREPARED:
		break;
	default:
744
		return -EPERM;
745 746
	}

747 748 749 750 751 752 753 754 755 756
	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
	if (!retval)
		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
	return retval;
}

static int snd_compr_stop(struct snd_compr_stream *stream)
{
	int retval;

757 758 759 760
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
761
		return -EPERM;
762 763 764 765
	default:
		break;
	}

766 767
	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
	if (!retval) {
768 769 770
		/* clear flags and stop any drain wait */
		stream->partial_drain = false;
		stream->metadata_set = false;
771
		snd_compr_drain_notify(stream);
772 773
		stream->runtime->total_bytes_available = 0;
		stream->runtime->total_bytes_transferred = 0;
774 775 776 777
	}
	return retval;
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
static void error_delayed_work(struct work_struct *work)
{
	struct snd_compr_stream *stream;

	stream = container_of(work, struct snd_compr_stream, error_work.work);

	mutex_lock(&stream->device->lock);

	stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
	wake_up(&stream->runtime->sleep);

	mutex_unlock(&stream->device->lock);
}

/*
 * snd_compr_stop_error: Report a fatal error on a stream
 * @stream: pointer to stream
 * @state: state to transition the stream to
 *
 * Stop the stream and set its state.
 *
 * Should be called with compressed device lock held.
 */
int snd_compr_stop_error(struct snd_compr_stream *stream,
			 snd_pcm_state_t state)
{
	if (stream->runtime->state == state)
		return 0;

	stream->runtime->state = state;

	pr_debug("Changing state to: %d\n", state);

	queue_delayed_work(system_power_efficient_wq, &stream->error_work, 0);

	return 0;
}
EXPORT_SYMBOL_GPL(snd_compr_stop_error);

817 818
static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
{
819 820
	int ret;

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 826 827 828 829 830 831
	 *
	 * It is expected that driver will notify the drain completion and then
	 * stream will be moved to SETUP state, even if draining resulted in an
	 * error. We can trigger next track after this.
	 */
	stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
	mutex_unlock(&stream->device->lock);

832 833 834 835 836 837 838 839 840
	/* we wait for drain to complete here, drain can return when
	 * interruption occurred, wait returned error or success.
	 * For the first two cases we don't do anything different here and
	 * return after waking up
	 */

	ret = wait_event_interruptible(stream->runtime->sleep,
			(stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
	if (ret == -ERESTARTSYS)
841
		pr_debug("wait aborted by a signal\n");
842 843 844
	else if (ret)
		pr_debug("wait for drain failed with %d\n", ret);

845 846 847 848

	wake_up(&stream->runtime->sleep);
	mutex_lock(&stream->device->lock);

849
	return ret;
850 851
}

852 853 854 855
static int snd_compr_drain(struct snd_compr_stream *stream)
{
	int retval;

856 857 858 859
	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
860
	case SNDRV_PCM_STATE_PAUSED:
861
		return -EPERM;
862 863
	case SNDRV_PCM_STATE_XRUN:
		return -EPIPE;
864 865 866
	default:
		break;
	}
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
}

878 879 880 881 882 883 884 885
static int snd_compr_next_track(struct snd_compr_stream *stream)
{
	int retval;

	/* only a running stream can transition to next track */
	if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
		return -EPERM;

886 887 888 889
	/* next track doesn't have any meaning for capture streams */
	if (stream->direction == SND_COMPRESS_CAPTURE)
		return -EPERM;

890
	/* you can signal next track if this is intended to be a gapless stream
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
	 * and current track metadata is set
	 */
	if (stream->metadata_set == false)
		return -EPERM;

	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
	if (retval != 0)
		return retval;
	stream->metadata_set = false;
	stream->next_track = true;
	return 0;
}

static int snd_compr_partial_drain(struct snd_compr_stream *stream)
{
	int retval;
907 908 909 910 911

	switch (stream->runtime->state) {
	case SNDRV_PCM_STATE_OPEN:
	case SNDRV_PCM_STATE_SETUP:
	case SNDRV_PCM_STATE_PREPARED:
912
	case SNDRV_PCM_STATE_PAUSED:
913
		return -EPERM;
914 915
	case SNDRV_PCM_STATE_XRUN:
		return -EPIPE;
916 917 918 919
	default:
		break;
	}

920 921 922 923
	/* partial drain doesn't have any meaning for capture streams */
	if (stream->direction == SND_COMPRESS_CAPTURE)
		return -EPERM;

924 925 926 927
	/* stream can be drained only when next track has been signalled */
	if (stream->next_track == false)
		return -EPERM;

928
	stream->partial_drain = true;
929
	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
930
	if (retval) {
931
		pr_debug("Partial drain returned failure\n");
932 933 934
		wake_up(&stream->runtime->sleep);
		return retval;
	}
935 936

	stream->next_track = false;
937
	return snd_compress_wait_for_drain(stream);
938 939
}

940 941 942 943 944 945 946 947
static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
	struct snd_compr_file *data = f->private_data;
	struct snd_compr_stream *stream;
	int retval = -ENOTTY;

	if (snd_BUG_ON(!data))
		return -EFAULT;
948

949
	stream = &data->stream;
950

951 952 953
	mutex_lock(&stream->device->lock);
	switch (_IOC_NR(cmd)) {
	case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
954
		retval = put_user(SNDRV_COMPRESS_VERSION,
955 956 957 958 959
				(int __user *)arg) ? -EFAULT : 0;
		break;
	case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
		retval = snd_compr_get_caps(stream, arg);
		break;
960
#ifndef COMPR_CODEC_CAPS_OVERFLOW
961 962 963
	case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
		retval = snd_compr_get_codec_caps(stream, arg);
		break;
964
#endif
965 966 967 968 969 970
	case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
		retval = snd_compr_set_params(stream, arg);
		break;
	case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
		retval = snd_compr_get_params(stream, arg);
		break;
971 972 973 974 975 976
	case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
		retval = snd_compr_set_metadata(stream, arg);
		break;
	case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
		retval = snd_compr_get_metadata(stream, arg);
		break;
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
		retval = snd_compr_tstamp(stream, arg);
		break;
	case _IOC_NR(SNDRV_COMPRESS_AVAIL):
		retval = snd_compr_ioctl_avail(stream, arg);
		break;
	case _IOC_NR(SNDRV_COMPRESS_PAUSE):
		retval = snd_compr_pause(stream);
		break;
	case _IOC_NR(SNDRV_COMPRESS_RESUME):
		retval = snd_compr_resume(stream);
		break;
	case _IOC_NR(SNDRV_COMPRESS_START):
		retval = snd_compr_start(stream);
		break;
	case _IOC_NR(SNDRV_COMPRESS_STOP):
		retval = snd_compr_stop(stream);
		break;
	case _IOC_NR(SNDRV_COMPRESS_DRAIN):
		retval = snd_compr_drain(stream);
		break;
998 999 1000 1001 1002 1003 1004
	case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
		retval = snd_compr_partial_drain(stream);
		break;
	case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
		retval = snd_compr_next_track(stream);
		break;

1005 1006 1007 1008 1009
	}
	mutex_unlock(&stream->device->lock);
	return retval;
}

1010 1011 1012 1013 1014 1015 1016 1017 1018
/* support of 32bit userspace on 64bit platforms */
#ifdef CONFIG_COMPAT
static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
						unsigned long arg)
{
	return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
#endif

1019 1020 1021 1022 1023 1024 1025
static const struct file_operations snd_compr_file_ops = {
		.owner =	THIS_MODULE,
		.open =		snd_compr_open,
		.release =	snd_compr_free,
		.write =	snd_compr_write,
		.read =		snd_compr_read,
		.unlocked_ioctl = snd_compr_ioctl,
1026 1027 1028
#ifdef CONFIG_COMPAT
		.compat_ioctl = snd_compr_ioctl_compat,
#endif
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		.mmap =		snd_compr_mmap,
		.poll =		snd_compr_poll,
};

static int snd_compress_dev_register(struct snd_device *device)
{
	int ret = -EINVAL;
	struct snd_compr *compr;

	if (snd_BUG_ON(!device || !device->device_data))
		return -EBADFD;
	compr = device->device_data;

1042
	pr_debug("reg device %s, direction %d\n", compr->name,
1043 1044
			compr->direction);
	/* register compressed device */
1045 1046 1047
	ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
				  compr->card, compr->device,
				  &snd_compr_file_ops, compr, &compr->dev);
1048
	if (ret < 0) {
1049
		pr_err("snd_register_device failed %d\n", ret);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
		return ret;
	}
	return ret;

}

static int snd_compress_dev_disconnect(struct snd_device *device)
{
	struct snd_compr *compr;

	compr = device->device_data;
1061
	snd_unregister_device(&compr->dev);
1062 1063 1064
	return 0;
}

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
#ifdef CONFIG_SND_VERBOSE_PROCFS
static void snd_compress_proc_info_read(struct snd_info_entry *entry,
					struct snd_info_buffer *buffer)
{
	struct snd_compr *compr = (struct snd_compr *)entry->private_data;

	snd_iprintf(buffer, "card: %d\n", compr->card->number);
	snd_iprintf(buffer, "device: %d\n", compr->device);
	snd_iprintf(buffer, "stream: %s\n",
			compr->direction == SND_COMPRESS_PLAYBACK
				? "PLAYBACK" : "CAPTURE");
	snd_iprintf(buffer, "id: %s\n", compr->id);
}

static int snd_compress_proc_init(struct snd_compr *compr)
{
	struct snd_info_entry *entry;
	char name[16];

	sprintf(name, "compr%i", compr->device);
	entry = snd_info_create_card_entry(compr->card, name,
					   compr->card->proc_root);
	if (!entry)
		return -ENOMEM;
1089
	entry->mode = S_IFDIR | 0555;
1090 1091 1092 1093
	compr->proc_root = entry;

	entry = snd_info_create_card_entry(compr->card, "info",
					   compr->proc_root);
1094
	if (entry)
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		snd_info_set_text_ops(entry, compr,
				      snd_compress_proc_info_read);
	compr->proc_info_entry = entry;

	return 0;
}

static void snd_compress_proc_done(struct snd_compr *compr)
{
	snd_info_free_entry(compr->proc_info_entry);
	compr->proc_info_entry = NULL;
	snd_info_free_entry(compr->proc_root);
	compr->proc_root = NULL;
}
1109 1110 1111 1112 1113

static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
{
	strlcpy(compr->id, id, sizeof(compr->id));
}
1114 1115 1116 1117 1118 1119 1120 1121 1122
#else
static inline int snd_compress_proc_init(struct snd_compr *compr)
{
	return 0;
}

static inline void snd_compress_proc_done(struct snd_compr *compr)
{
}
1123 1124 1125 1126

static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
{
}
1127 1128
#endif

1129 1130 1131 1132 1133
static int snd_compress_dev_free(struct snd_device *device)
{
	struct snd_compr *compr;

	compr = device->device_data;
1134
	snd_compress_proc_done(compr);
1135 1136 1137 1138
	put_device(&compr->dev);
	return 0;
}

1139 1140 1141 1142 1143 1144 1145 1146
/*
 * snd_compress_new: create new compress device
 * @card: sound card pointer
 * @device: device number
 * @dirn: device direction, should be of type enum snd_compr_direction
 * @compr: compress device pointer
 */
int snd_compress_new(struct snd_card *card, int device,
1147
			int dirn, const char *id, struct snd_compr *compr)
1148
{
1149
	static const struct snd_device_ops ops = {
1150
		.dev_free = snd_compress_dev_free,
1151 1152 1153
		.dev_register = snd_compress_dev_register,
		.dev_disconnect = snd_compress_dev_disconnect,
	};
1154
	int ret;
1155 1156 1157 1158

	compr->card = card;
	compr->device = device;
	compr->direction = dirn;
1159

1160 1161
	snd_compress_set_id(compr, id);

1162 1163 1164
	snd_device_initialize(&compr->dev, card);
	dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);

1165 1166 1167 1168 1169
	ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
	if (ret == 0)
		snd_compress_proc_init(compr);

	return ret;
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
}
EXPORT_SYMBOL_GPL(snd_compress_new);

static int snd_compress_add_device(struct snd_compr *device)
{
	int ret;

	if (!device->card)
		return -EINVAL;

	/* register the card */
	ret = snd_card_register(device->card);
	if (ret)
		goto out;
	return 0;

out:
	pr_err("failed with %d\n", ret);
	return ret;

}

static int snd_compress_remove_device(struct snd_compr *device)
{
	return snd_card_free(device->card);
}

/**
 * snd_compress_register - register compressed device
 *
 * @device: compressed device to register
 */
int snd_compress_register(struct snd_compr *device)
{
	int retval;

1206
	if (device->name == NULL || device->ops == NULL)
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
		return -EINVAL;

	pr_debug("Registering compressed device %s\n", device->name);
	if (snd_BUG_ON(!device->ops->open))
		return -EINVAL;
	if (snd_BUG_ON(!device->ops->free))
		return -EINVAL;
	if (snd_BUG_ON(!device->ops->set_params))
		return -EINVAL;
	if (snd_BUG_ON(!device->ops->trigger))
		return -EINVAL;

	mutex_init(&device->lock);

	/* register a compressed card */
	mutex_lock(&device_mutex);
	retval = snd_compress_add_device(device);
	mutex_unlock(&device_mutex);
	return retval;
}
EXPORT_SYMBOL_GPL(snd_compress_register);

int snd_compress_deregister(struct snd_compr *device)
{
	pr_debug("Removing compressed device %s\n", device->name);
	mutex_lock(&device_mutex);
	snd_compress_remove_device(device);
	mutex_unlock(&device_mutex);
	return 0;
}
EXPORT_SYMBOL_GPL(snd_compress_deregister);

MODULE_DESCRIPTION("ALSA Compressed offload framework");
MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
MODULE_LICENSE("GPL v2");