xref: /openbmc/linux/sound/core/compress_offload.c (revision 47966e97)
1b21c60a4SVinod Koul /*
2b21c60a4SVinod Koul  *  compress_core.c - compress offload core
3b21c60a4SVinod Koul  *
4b21c60a4SVinod Koul  *  Copyright (C) 2011 Intel Corporation
5b21c60a4SVinod Koul  *  Authors:	Vinod Koul <vinod.koul@linux.intel.com>
6b21c60a4SVinod Koul  *		Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
7b21c60a4SVinod Koul  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8b21c60a4SVinod Koul  *
9b21c60a4SVinod Koul  *  This program is free software; you can redistribute it and/or modify
10b21c60a4SVinod Koul  *  it under the terms of the GNU General Public License as published by
11b21c60a4SVinod Koul  *  the Free Software Foundation; version 2 of the License.
12b21c60a4SVinod Koul  *
13b21c60a4SVinod Koul  *  This program is distributed in the hope that it will be useful, but
14b21c60a4SVinod Koul  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15b21c60a4SVinod Koul  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16b21c60a4SVinod Koul  *  General Public License for more details.
17b21c60a4SVinod Koul  *
18b21c60a4SVinod Koul  *  You should have received a copy of the GNU General Public License along
19b21c60a4SVinod Koul  *  with this program; if not, write to the Free Software Foundation, Inc.,
20b21c60a4SVinod Koul  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21b21c60a4SVinod Koul  *
22b21c60a4SVinod Koul  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23b21c60a4SVinod Koul  *
24b21c60a4SVinod Koul  */
25b21c60a4SVinod Koul #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
26b21c60a4SVinod Koul #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
27b21c60a4SVinod Koul 
28b21c60a4SVinod Koul #include <linux/file.h>
29b21c60a4SVinod Koul #include <linux/fs.h>
30b21c60a4SVinod Koul #include <linux/list.h>
31f0283b58SCharles Keepax #include <linux/math64.h>
32b21c60a4SVinod Koul #include <linux/mm.h>
33b21c60a4SVinod Koul #include <linux/mutex.h>
34b21c60a4SVinod Koul #include <linux/poll.h>
35b21c60a4SVinod Koul #include <linux/slab.h>
36b21c60a4SVinod Koul #include <linux/sched.h>
37f0283b58SCharles Keepax #include <linux/types.h>
38b21c60a4SVinod Koul #include <linux/uio.h>
39b21c60a4SVinod Koul #include <linux/uaccess.h>
40b21c60a4SVinod Koul #include <linux/module.h>
41b21c60a4SVinod Koul #include <sound/core.h>
42b21c60a4SVinod Koul #include <sound/initval.h>
43b21c60a4SVinod Koul #include <sound/compress_params.h>
44b21c60a4SVinod Koul #include <sound/compress_offload.h>
45b21c60a4SVinod Koul #include <sound/compress_driver.h>
46b21c60a4SVinod Koul 
47b21c60a4SVinod Koul /* TODO:
48b21c60a4SVinod Koul  * - add substream support for multiple devices in case of
49b21c60a4SVinod Koul  *	SND_DYNAMIC_MINORS is not used
50b21c60a4SVinod Koul  * - Multiple node representation
51b21c60a4SVinod Koul  *	driver should be able to register multiple nodes
52b21c60a4SVinod Koul  */
53b21c60a4SVinod Koul 
54b21c60a4SVinod Koul static DEFINE_MUTEX(device_mutex);
55b21c60a4SVinod Koul 
56b21c60a4SVinod Koul struct snd_compr_file {
57b21c60a4SVinod Koul 	unsigned long caps;
58b21c60a4SVinod Koul 	struct snd_compr_stream stream;
59b21c60a4SVinod Koul };
60b21c60a4SVinod Koul 
61b21c60a4SVinod Koul /*
62b21c60a4SVinod Koul  * a note on stream states used:
63b21c60a4SVinod Koul  * we use follwing states in the compressed core
64b21c60a4SVinod Koul  * SNDRV_PCM_STATE_OPEN: When stream has been opened.
65b21c60a4SVinod Koul  * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
66b21c60a4SVinod Koul  *	calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
67b21c60a4SVinod Koul  *	state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
68b21c60a4SVinod Koul  * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
69b21c60a4SVinod Koul  *	decoding/encoding and rendering/capturing data.
70b21c60a4SVinod Koul  * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
71b21c60a4SVinod Koul  *	by calling SNDRV_COMPRESS_DRAIN.
72b21c60a4SVinod Koul  * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
73b21c60a4SVinod Koul  *	SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
74b21c60a4SVinod Koul  *	SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
75b21c60a4SVinod Koul  */
76b21c60a4SVinod Koul static int snd_compr_open(struct inode *inode, struct file *f)
77b21c60a4SVinod Koul {
78b21c60a4SVinod Koul 	struct snd_compr *compr;
79b21c60a4SVinod Koul 	struct snd_compr_file *data;
80b21c60a4SVinod Koul 	struct snd_compr_runtime *runtime;
81b21c60a4SVinod Koul 	enum snd_compr_direction dirn;
82b21c60a4SVinod Koul 	int maj = imajor(inode);
83b21c60a4SVinod Koul 	int ret;
84b21c60a4SVinod Koul 
8581cb3246SDan Carpenter 	if ((f->f_flags & O_ACCMODE) == O_WRONLY)
86b21c60a4SVinod Koul 		dirn = SND_COMPRESS_PLAYBACK;
8781cb3246SDan Carpenter 	else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
88b21c60a4SVinod Koul 		dirn = SND_COMPRESS_CAPTURE;
8981cb3246SDan Carpenter 	else
90b21c60a4SVinod Koul 		return -EINVAL;
91b21c60a4SVinod Koul 
92b21c60a4SVinod Koul 	if (maj == snd_major)
93b21c60a4SVinod Koul 		compr = snd_lookup_minor_data(iminor(inode),
94b21c60a4SVinod Koul 					SNDRV_DEVICE_TYPE_COMPRESS);
95b21c60a4SVinod Koul 	else
96b21c60a4SVinod Koul 		return -EBADFD;
97b21c60a4SVinod Koul 
98b21c60a4SVinod Koul 	if (compr == NULL) {
99b21c60a4SVinod Koul 		pr_err("no device data!!!\n");
100b21c60a4SVinod Koul 		return -ENODEV;
101b21c60a4SVinod Koul 	}
102b21c60a4SVinod Koul 
103b21c60a4SVinod Koul 	if (dirn != compr->direction) {
104b21c60a4SVinod Koul 		pr_err("this device doesn't support this direction\n");
105a0830dbdSTakashi Iwai 		snd_card_unref(compr->card);
106b21c60a4SVinod Koul 		return -EINVAL;
107b21c60a4SVinod Koul 	}
108b21c60a4SVinod Koul 
109b21c60a4SVinod Koul 	data = kzalloc(sizeof(*data), GFP_KERNEL);
110a0830dbdSTakashi Iwai 	if (!data) {
111a0830dbdSTakashi Iwai 		snd_card_unref(compr->card);
112b21c60a4SVinod Koul 		return -ENOMEM;
113a0830dbdSTakashi Iwai 	}
114b21c60a4SVinod Koul 	data->stream.ops = compr->ops;
115b21c60a4SVinod Koul 	data->stream.direction = dirn;
116b21c60a4SVinod Koul 	data->stream.private_data = compr->private_data;
117b21c60a4SVinod Koul 	data->stream.device = compr;
118b21c60a4SVinod Koul 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
119b21c60a4SVinod Koul 	if (!runtime) {
120b21c60a4SVinod Koul 		kfree(data);
121a0830dbdSTakashi Iwai 		snd_card_unref(compr->card);
122b21c60a4SVinod Koul 		return -ENOMEM;
123b21c60a4SVinod Koul 	}
124b21c60a4SVinod Koul 	runtime->state = SNDRV_PCM_STATE_OPEN;
125b21c60a4SVinod Koul 	init_waitqueue_head(&runtime->sleep);
126b21c60a4SVinod Koul 	data->stream.runtime = runtime;
127b21c60a4SVinod Koul 	f->private_data = (void *)data;
128b21c60a4SVinod Koul 	mutex_lock(&compr->lock);
129b21c60a4SVinod Koul 	ret = compr->ops->open(&data->stream);
130b21c60a4SVinod Koul 	mutex_unlock(&compr->lock);
131b21c60a4SVinod Koul 	if (ret) {
132b21c60a4SVinod Koul 		kfree(runtime);
133b21c60a4SVinod Koul 		kfree(data);
134b21c60a4SVinod Koul 	}
135a0830dbdSTakashi Iwai 	snd_card_unref(compr->card);
136a0830dbdSTakashi Iwai 	return 0;
137b21c60a4SVinod Koul }
138b21c60a4SVinod Koul 
139b21c60a4SVinod Koul static int snd_compr_free(struct inode *inode, struct file *f)
140b21c60a4SVinod Koul {
141b21c60a4SVinod Koul 	struct snd_compr_file *data = f->private_data;
142b21c60a4SVinod Koul 	data->stream.ops->free(&data->stream);
143b21c60a4SVinod Koul 	kfree(data->stream.runtime->buffer);
144b21c60a4SVinod Koul 	kfree(data->stream.runtime);
145b21c60a4SVinod Koul 	kfree(data);
146b21c60a4SVinod Koul 	return 0;
147b21c60a4SVinod Koul }
148b21c60a4SVinod Koul 
14917ac8e5cSRichard Fitzgerald static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
150b21c60a4SVinod Koul 		struct snd_compr_tstamp *tstamp)
151b21c60a4SVinod Koul {
152b21c60a4SVinod Koul 	if (!stream->ops->pointer)
15317ac8e5cSRichard Fitzgerald 		return -ENOTSUPP;
154b21c60a4SVinod Koul 	stream->ops->pointer(stream, tstamp);
155b21c60a4SVinod Koul 	pr_debug("dsp consumed till %d total %d bytes\n",
156b21c60a4SVinod Koul 		tstamp->byte_offset, tstamp->copied_total);
1575b1f79f7SCharles Keepax 	if (stream->direction == SND_COMPRESS_PLAYBACK)
158b21c60a4SVinod Koul 		stream->runtime->total_bytes_transferred = tstamp->copied_total;
1595b1f79f7SCharles Keepax 	else
1605b1f79f7SCharles Keepax 		stream->runtime->total_bytes_available = tstamp->copied_total;
16117ac8e5cSRichard Fitzgerald 	return 0;
162b21c60a4SVinod Koul }
163b21c60a4SVinod Koul 
164b21c60a4SVinod Koul static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
165b21c60a4SVinod Koul 		struct snd_compr_avail *avail)
166b21c60a4SVinod Koul {
16717ac8e5cSRichard Fitzgerald 	memset(avail, 0, sizeof(*avail));
168b21c60a4SVinod Koul 	snd_compr_update_tstamp(stream, &avail->tstamp);
16917ac8e5cSRichard Fitzgerald 	/* Still need to return avail even if tstamp can't be filled in */
170b21c60a4SVinod Koul 
171b21c60a4SVinod Koul 	if (stream->runtime->total_bytes_available == 0 &&
1725b1f79f7SCharles Keepax 			stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
1735b1f79f7SCharles Keepax 			stream->direction == SND_COMPRESS_PLAYBACK) {
174b21c60a4SVinod Koul 		pr_debug("detected init and someone forgot to do a write\n");
175b21c60a4SVinod Koul 		return stream->runtime->buffer_size;
176b21c60a4SVinod Koul 	}
177b21c60a4SVinod Koul 	pr_debug("app wrote %lld, DSP consumed %lld\n",
178b21c60a4SVinod Koul 			stream->runtime->total_bytes_available,
179b21c60a4SVinod Koul 			stream->runtime->total_bytes_transferred);
180b21c60a4SVinod Koul 	if (stream->runtime->total_bytes_available ==
181b21c60a4SVinod Koul 				stream->runtime->total_bytes_transferred) {
1825b1f79f7SCharles Keepax 		if (stream->direction == SND_COMPRESS_PLAYBACK) {
183b21c60a4SVinod Koul 			pr_debug("both pointers are same, returning full avail\n");
184b21c60a4SVinod Koul 			return stream->runtime->buffer_size;
1855b1f79f7SCharles Keepax 		} else {
1865b1f79f7SCharles Keepax 			pr_debug("both pointers are same, returning no avail\n");
1875b1f79f7SCharles Keepax 			return 0;
1885b1f79f7SCharles Keepax 		}
189b21c60a4SVinod Koul 	}
190b21c60a4SVinod Koul 
1915b1f79f7SCharles Keepax 	avail->avail = stream->runtime->total_bytes_available -
1925b1f79f7SCharles Keepax 			stream->runtime->total_bytes_transferred;
1935b1f79f7SCharles Keepax 	if (stream->direction == SND_COMPRESS_PLAYBACK)
1945b1f79f7SCharles Keepax 		avail->avail = stream->runtime->buffer_size - avail->avail;
1955b1f79f7SCharles Keepax 
1964c28e32dSCharles Keepax 	pr_debug("ret avail as %lld\n", avail->avail);
1974c28e32dSCharles Keepax 	return avail->avail;
198b21c60a4SVinod Koul }
199b21c60a4SVinod Koul 
200b21c60a4SVinod Koul static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
201b21c60a4SVinod Koul {
202b21c60a4SVinod Koul 	struct snd_compr_avail avail;
203b21c60a4SVinod Koul 
204b21c60a4SVinod Koul 	return snd_compr_calc_avail(stream, &avail);
205b21c60a4SVinod Koul }
206b21c60a4SVinod Koul 
207b21c60a4SVinod Koul static int
208b21c60a4SVinod Koul snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
209b21c60a4SVinod Koul {
210b21c60a4SVinod Koul 	struct snd_compr_avail ioctl_avail;
211b21c60a4SVinod Koul 	size_t avail;
212b21c60a4SVinod Koul 
213b21c60a4SVinod Koul 	avail = snd_compr_calc_avail(stream, &ioctl_avail);
214b21c60a4SVinod Koul 	ioctl_avail.avail = avail;
215b21c60a4SVinod Koul 
216b21c60a4SVinod Koul 	if (copy_to_user((__u64 __user *)arg,
217b21c60a4SVinod Koul 				&ioctl_avail, sizeof(ioctl_avail)))
218b21c60a4SVinod Koul 		return -EFAULT;
219b21c60a4SVinod Koul 	return 0;
220b21c60a4SVinod Koul }
221b21c60a4SVinod Koul 
222b21c60a4SVinod Koul static int snd_compr_write_data(struct snd_compr_stream *stream,
223b21c60a4SVinod Koul 	       const char __user *buf, size_t count)
224b21c60a4SVinod Koul {
225b21c60a4SVinod Koul 	void *dstn;
226b21c60a4SVinod Koul 	size_t copy;
227b21c60a4SVinod Koul 	struct snd_compr_runtime *runtime = stream->runtime;
228f0283b58SCharles Keepax 	/* 64-bit Modulus */
229f0283b58SCharles Keepax 	u64 app_pointer = div64_u64(runtime->total_bytes_available,
230f0283b58SCharles Keepax 				    runtime->buffer_size);
231f0283b58SCharles Keepax 	app_pointer = runtime->total_bytes_available -
232f0283b58SCharles Keepax 		      (app_pointer * runtime->buffer_size);
233b21c60a4SVinod Koul 
234f0283b58SCharles Keepax 	dstn = runtime->buffer + app_pointer;
235b21c60a4SVinod Koul 	pr_debug("copying %ld at %lld\n",
236f0283b58SCharles Keepax 			(unsigned long)count, app_pointer);
237f0283b58SCharles Keepax 	if (count < runtime->buffer_size - app_pointer) {
238b21c60a4SVinod Koul 		if (copy_from_user(dstn, buf, count))
239b21c60a4SVinod Koul 			return -EFAULT;
240b21c60a4SVinod Koul 	} else {
241f0283b58SCharles Keepax 		copy = runtime->buffer_size - app_pointer;
242b21c60a4SVinod Koul 		if (copy_from_user(dstn, buf, copy))
243b21c60a4SVinod Koul 			return -EFAULT;
244b21c60a4SVinod Koul 		if (copy_from_user(runtime->buffer, buf + copy, count - copy))
245b21c60a4SVinod Koul 			return -EFAULT;
246b21c60a4SVinod Koul 	}
247b21c60a4SVinod Koul 	/* if DSP cares, let it know data has been written */
248b21c60a4SVinod Koul 	if (stream->ops->ack)
249b21c60a4SVinod Koul 		stream->ops->ack(stream, count);
250b21c60a4SVinod Koul 	return count;
251b21c60a4SVinod Koul }
252b21c60a4SVinod Koul 
253b21c60a4SVinod Koul static ssize_t snd_compr_write(struct file *f, const char __user *buf,
254b21c60a4SVinod Koul 		size_t count, loff_t *offset)
255b21c60a4SVinod Koul {
256b21c60a4SVinod Koul 	struct snd_compr_file *data = f->private_data;
257b21c60a4SVinod Koul 	struct snd_compr_stream *stream;
258b21c60a4SVinod Koul 	size_t avail;
259b21c60a4SVinod Koul 	int retval;
260b21c60a4SVinod Koul 
261b21c60a4SVinod Koul 	if (snd_BUG_ON(!data))
262b21c60a4SVinod Koul 		return -EFAULT;
263b21c60a4SVinod Koul 
264b21c60a4SVinod Koul 	stream = &data->stream;
265b21c60a4SVinod Koul 	mutex_lock(&stream->device->lock);
266b21c60a4SVinod Koul 	/* write is allowed when stream is running or has been steup */
267b21c60a4SVinod Koul 	if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
268b21c60a4SVinod Koul 			stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
269b21c60a4SVinod Koul 		mutex_unlock(&stream->device->lock);
270b21c60a4SVinod Koul 		return -EBADFD;
271b21c60a4SVinod Koul 	}
272b21c60a4SVinod Koul 
273b21c60a4SVinod Koul 	avail = snd_compr_get_avail(stream);
274b21c60a4SVinod Koul 	pr_debug("avail returned %ld\n", (unsigned long)avail);
275b21c60a4SVinod Koul 	/* calculate how much we can write to buffer */
276b21c60a4SVinod Koul 	if (avail > count)
277b21c60a4SVinod Koul 		avail = count;
278b21c60a4SVinod Koul 
2794daf891cSCharles Keepax 	if (stream->ops->copy) {
2804daf891cSCharles Keepax 		char __user* cbuf = (char __user*)buf;
2814daf891cSCharles Keepax 		retval = stream->ops->copy(stream, cbuf, avail);
2824daf891cSCharles Keepax 	} else {
283b21c60a4SVinod Koul 		retval = snd_compr_write_data(stream, buf, avail);
2844daf891cSCharles Keepax 	}
285b21c60a4SVinod Koul 	if (retval > 0)
286b21c60a4SVinod Koul 		stream->runtime->total_bytes_available += retval;
287b21c60a4SVinod Koul 
288b21c60a4SVinod Koul 	/* while initiating the stream, write should be called before START
289b21c60a4SVinod Koul 	 * call, so in setup move state */
290b21c60a4SVinod Koul 	if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
291b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
292b21c60a4SVinod Koul 		pr_debug("stream prepared, Houston we are good to go\n");
293b21c60a4SVinod Koul 	}
294b21c60a4SVinod Koul 
295b21c60a4SVinod Koul 	mutex_unlock(&stream->device->lock);
296b21c60a4SVinod Koul 	return retval;
297b21c60a4SVinod Koul }
298b21c60a4SVinod Koul 
299b21c60a4SVinod Koul 
300b21c60a4SVinod Koul static ssize_t snd_compr_read(struct file *f, char __user *buf,
301b21c60a4SVinod Koul 		size_t count, loff_t *offset)
302b21c60a4SVinod Koul {
30349bb6402SCharles Keepax 	struct snd_compr_file *data = f->private_data;
30449bb6402SCharles Keepax 	struct snd_compr_stream *stream;
30549bb6402SCharles Keepax 	size_t avail;
30649bb6402SCharles Keepax 	int retval;
30749bb6402SCharles Keepax 
30849bb6402SCharles Keepax 	if (snd_BUG_ON(!data))
30949bb6402SCharles Keepax 		return -EFAULT;
31049bb6402SCharles Keepax 
31149bb6402SCharles Keepax 	stream = &data->stream;
31249bb6402SCharles Keepax 	mutex_lock(&stream->device->lock);
31349bb6402SCharles Keepax 
31449bb6402SCharles Keepax 	/* read is allowed when stream is running */
31549bb6402SCharles Keepax 	if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
31649bb6402SCharles Keepax 		retval = -EBADFD;
31749bb6402SCharles Keepax 		goto out;
31849bb6402SCharles Keepax 	}
31949bb6402SCharles Keepax 
32049bb6402SCharles Keepax 	avail = snd_compr_get_avail(stream);
32149bb6402SCharles Keepax 	pr_debug("avail returned %ld\n", (unsigned long)avail);
32249bb6402SCharles Keepax 	/* calculate how much we can read from buffer */
32349bb6402SCharles Keepax 	if (avail > count)
32449bb6402SCharles Keepax 		avail = count;
32549bb6402SCharles Keepax 
32649bb6402SCharles Keepax 	if (stream->ops->copy) {
32749bb6402SCharles Keepax 		retval = stream->ops->copy(stream, buf, avail);
32849bb6402SCharles Keepax 	} else {
32949bb6402SCharles Keepax 		retval = -ENXIO;
33049bb6402SCharles Keepax 		goto out;
33149bb6402SCharles Keepax 	}
33249bb6402SCharles Keepax 	if (retval > 0)
33349bb6402SCharles Keepax 		stream->runtime->total_bytes_transferred += retval;
33449bb6402SCharles Keepax 
33549bb6402SCharles Keepax out:
33649bb6402SCharles Keepax 	mutex_unlock(&stream->device->lock);
33749bb6402SCharles Keepax 	return retval;
338b21c60a4SVinod Koul }
339b21c60a4SVinod Koul 
340b21c60a4SVinod Koul static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
341b21c60a4SVinod Koul {
342b21c60a4SVinod Koul 	return -ENXIO;
343b21c60a4SVinod Koul }
344b21c60a4SVinod Koul 
345b21c60a4SVinod Koul static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
346b21c60a4SVinod Koul {
347b21c60a4SVinod Koul 	if (stream->direction == SND_COMPRESS_PLAYBACK)
348b21c60a4SVinod Koul 		return POLLOUT | POLLWRNORM;
349b21c60a4SVinod Koul 	else
350b21c60a4SVinod Koul 		return POLLIN | POLLRDNORM;
351b21c60a4SVinod Koul }
352b21c60a4SVinod Koul 
353b21c60a4SVinod Koul static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
354b21c60a4SVinod Koul {
355b21c60a4SVinod Koul 	struct snd_compr_file *data = f->private_data;
356b21c60a4SVinod Koul 	struct snd_compr_stream *stream;
357b21c60a4SVinod Koul 	size_t avail;
358b21c60a4SVinod Koul 	int retval = 0;
359b21c60a4SVinod Koul 
360b21c60a4SVinod Koul 	if (snd_BUG_ON(!data))
361b21c60a4SVinod Koul 		return -EFAULT;
362b21c60a4SVinod Koul 	stream = &data->stream;
363b21c60a4SVinod Koul 	if (snd_BUG_ON(!stream))
364b21c60a4SVinod Koul 		return -EFAULT;
365b21c60a4SVinod Koul 
366b21c60a4SVinod Koul 	mutex_lock(&stream->device->lock);
367b21c60a4SVinod Koul 	if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
368b21c60a4SVinod Koul 			stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
369b21c60a4SVinod Koul 		retval = -EBADFD;
370b21c60a4SVinod Koul 		goto out;
371b21c60a4SVinod Koul 	}
372b21c60a4SVinod Koul 	poll_wait(f, &stream->runtime->sleep, wait);
373b21c60a4SVinod Koul 
374b21c60a4SVinod Koul 	avail = snd_compr_get_avail(stream);
375b21c60a4SVinod Koul 	pr_debug("avail is %ld\n", (unsigned long)avail);
376b21c60a4SVinod Koul 	/* check if we have at least one fragment to fill */
377b21c60a4SVinod Koul 	switch (stream->runtime->state) {
378b21c60a4SVinod Koul 	case SNDRV_PCM_STATE_DRAINING:
379b21c60a4SVinod Koul 		/* stream has been woken up after drain is complete
380b21c60a4SVinod Koul 		 * draining done so set stream state to stopped
381b21c60a4SVinod Koul 		 */
382b21c60a4SVinod Koul 		retval = snd_compr_get_poll(stream);
383b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
384b21c60a4SVinod Koul 		break;
385b21c60a4SVinod Koul 	case SNDRV_PCM_STATE_RUNNING:
386b21c60a4SVinod Koul 	case SNDRV_PCM_STATE_PREPARED:
387b21c60a4SVinod Koul 	case SNDRV_PCM_STATE_PAUSED:
388b21c60a4SVinod Koul 		if (avail >= stream->runtime->fragment_size)
389b21c60a4SVinod Koul 			retval = snd_compr_get_poll(stream);
390b21c60a4SVinod Koul 		break;
391b21c60a4SVinod Koul 	default:
392b21c60a4SVinod Koul 		if (stream->direction == SND_COMPRESS_PLAYBACK)
393b21c60a4SVinod Koul 			retval = POLLOUT | POLLWRNORM | POLLERR;
394b21c60a4SVinod Koul 		else
395b21c60a4SVinod Koul 			retval = POLLIN | POLLRDNORM | POLLERR;
396b21c60a4SVinod Koul 		break;
397b21c60a4SVinod Koul 	}
398b21c60a4SVinod Koul out:
399b21c60a4SVinod Koul 	mutex_unlock(&stream->device->lock);
400b21c60a4SVinod Koul 	return retval;
401b21c60a4SVinod Koul }
402b21c60a4SVinod Koul 
403b21c60a4SVinod Koul static int
404b21c60a4SVinod Koul snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
405b21c60a4SVinod Koul {
406b21c60a4SVinod Koul 	int retval;
407b21c60a4SVinod Koul 	struct snd_compr_caps caps;
408b21c60a4SVinod Koul 
409b21c60a4SVinod Koul 	if (!stream->ops->get_caps)
410b21c60a4SVinod Koul 		return -ENXIO;
411b21c60a4SVinod Koul 
4121c62e9f2SDan Carpenter 	memset(&caps, 0, sizeof(caps));
413b21c60a4SVinod Koul 	retval = stream->ops->get_caps(stream, &caps);
414b21c60a4SVinod Koul 	if (retval)
415b21c60a4SVinod Koul 		goto out;
416b21c60a4SVinod Koul 	if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
417b21c60a4SVinod Koul 		retval = -EFAULT;
418b21c60a4SVinod Koul out:
419b21c60a4SVinod Koul 	return retval;
420b21c60a4SVinod Koul }
421b21c60a4SVinod Koul 
422b21c60a4SVinod Koul static int
423b21c60a4SVinod Koul snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
424b21c60a4SVinod Koul {
425b21c60a4SVinod Koul 	int retval;
426b21c60a4SVinod Koul 	struct snd_compr_codec_caps *caps;
427b21c60a4SVinod Koul 
428b21c60a4SVinod Koul 	if (!stream->ops->get_codec_caps)
429b21c60a4SVinod Koul 		return -ENXIO;
430b21c60a4SVinod Koul 
43147966e97STakashi Iwai 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
432b21c60a4SVinod Koul 	if (!caps)
433b21c60a4SVinod Koul 		return -ENOMEM;
434b21c60a4SVinod Koul 
435b21c60a4SVinod Koul 	retval = stream->ops->get_codec_caps(stream, caps);
436b21c60a4SVinod Koul 	if (retval)
437b21c60a4SVinod Koul 		goto out;
438b21c60a4SVinod Koul 	if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
439b21c60a4SVinod Koul 		retval = -EFAULT;
440b21c60a4SVinod Koul 
441b21c60a4SVinod Koul out:
442b21c60a4SVinod Koul 	kfree(caps);
443b21c60a4SVinod Koul 	return retval;
444b21c60a4SVinod Koul }
445b21c60a4SVinod Koul 
446b21c60a4SVinod Koul /* revisit this with snd_pcm_preallocate_xxx */
447b21c60a4SVinod Koul static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
448b21c60a4SVinod Koul 		struct snd_compr_params *params)
449b21c60a4SVinod Koul {
450b21c60a4SVinod Koul 	unsigned int buffer_size;
451b21c60a4SVinod Koul 	void *buffer;
452b21c60a4SVinod Koul 
453b21c60a4SVinod Koul 	buffer_size = params->buffer.fragment_size * params->buffer.fragments;
454b21c60a4SVinod Koul 	if (stream->ops->copy) {
455b21c60a4SVinod Koul 		buffer = NULL;
456b21c60a4SVinod Koul 		/* if copy is defined the driver will be required to copy
457b21c60a4SVinod Koul 		 * the data from core
458b21c60a4SVinod Koul 		 */
459b21c60a4SVinod Koul 	} else {
460b21c60a4SVinod Koul 		buffer = kmalloc(buffer_size, GFP_KERNEL);
461b21c60a4SVinod Koul 		if (!buffer)
462b21c60a4SVinod Koul 			return -ENOMEM;
463b21c60a4SVinod Koul 	}
464b21c60a4SVinod Koul 	stream->runtime->fragment_size = params->buffer.fragment_size;
465b21c60a4SVinod Koul 	stream->runtime->fragments = params->buffer.fragments;
466b21c60a4SVinod Koul 	stream->runtime->buffer = buffer;
467b21c60a4SVinod Koul 	stream->runtime->buffer_size = buffer_size;
468b21c60a4SVinod Koul 	return 0;
469b21c60a4SVinod Koul }
470b21c60a4SVinod Koul 
4714dc040a0SVinod Koul static int snd_compress_check_input(struct snd_compr_params *params)
4724dc040a0SVinod Koul {
4734dc040a0SVinod Koul 	/* first let's check the buffer parameter's */
4744dc040a0SVinod Koul 	if (params->buffer.fragment_size == 0 ||
4754dc040a0SVinod Koul 			params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
4764dc040a0SVinod Koul 		return -EINVAL;
4774dc040a0SVinod Koul 
478fb4a9779SVinod Koul 	/* now codec parameters */
479fb4a9779SVinod Koul 	if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
480fb4a9779SVinod Koul 		return -EINVAL;
481fb4a9779SVinod Koul 
482fb4a9779SVinod Koul 	if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
483fb4a9779SVinod Koul 		return -EINVAL;
484fb4a9779SVinod Koul 
485fb4a9779SVinod Koul 	if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
486fb4a9779SVinod Koul 		return -EINVAL;
487fb4a9779SVinod Koul 
4884dc040a0SVinod Koul 	return 0;
4894dc040a0SVinod Koul }
4904dc040a0SVinod Koul 
491b21c60a4SVinod Koul static int
492b21c60a4SVinod Koul snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
493b21c60a4SVinod Koul {
494b21c60a4SVinod Koul 	struct snd_compr_params *params;
495b21c60a4SVinod Koul 	int retval;
496b21c60a4SVinod Koul 
497b21c60a4SVinod Koul 	if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
498b21c60a4SVinod Koul 		/*
499b21c60a4SVinod Koul 		 * we should allow parameter change only when stream has been
500b21c60a4SVinod Koul 		 * opened not in other cases
501b21c60a4SVinod Koul 		 */
502b21c60a4SVinod Koul 		params = kmalloc(sizeof(*params), GFP_KERNEL);
503b21c60a4SVinod Koul 		if (!params)
504b21c60a4SVinod Koul 			return -ENOMEM;
505769fab2aSJesper Juhl 		if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
506769fab2aSJesper Juhl 			retval = -EFAULT;
507769fab2aSJesper Juhl 			goto out;
508769fab2aSJesper Juhl 		}
5094dc040a0SVinod Koul 
5104dc040a0SVinod Koul 		retval = snd_compress_check_input(params);
5114dc040a0SVinod Koul 		if (retval)
5124dc040a0SVinod Koul 			goto out;
5134dc040a0SVinod Koul 
514b21c60a4SVinod Koul 		retval = snd_compr_allocate_buffer(stream, params);
515b21c60a4SVinod Koul 		if (retval) {
516769fab2aSJesper Juhl 			retval = -ENOMEM;
517769fab2aSJesper Juhl 			goto out;
518b21c60a4SVinod Koul 		}
5194dc040a0SVinod Koul 
520b21c60a4SVinod Koul 		retval = stream->ops->set_params(stream, params);
521b21c60a4SVinod Koul 		if (retval)
522b21c60a4SVinod Koul 			goto out;
52349bb6402SCharles Keepax 
5249727b490SJeeja KP 		stream->metadata_set = false;
5259727b490SJeeja KP 		stream->next_track = false;
52649bb6402SCharles Keepax 
52749bb6402SCharles Keepax 		if (stream->direction == SND_COMPRESS_PLAYBACK)
52849bb6402SCharles Keepax 			stream->runtime->state = SNDRV_PCM_STATE_SETUP;
52949bb6402SCharles Keepax 		else
53049bb6402SCharles Keepax 			stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
531769fab2aSJesper Juhl 	} else {
532b21c60a4SVinod Koul 		return -EPERM;
533769fab2aSJesper Juhl 	}
534b21c60a4SVinod Koul out:
535b21c60a4SVinod Koul 	kfree(params);
536b21c60a4SVinod Koul 	return retval;
537b21c60a4SVinod Koul }
538b21c60a4SVinod Koul 
539b21c60a4SVinod Koul static int
540b21c60a4SVinod Koul snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
541b21c60a4SVinod Koul {
542b21c60a4SVinod Koul 	struct snd_codec *params;
543b21c60a4SVinod Koul 	int retval;
544b21c60a4SVinod Koul 
545b21c60a4SVinod Koul 	if (!stream->ops->get_params)
546b21c60a4SVinod Koul 		return -EBADFD;
547b21c60a4SVinod Koul 
54847966e97STakashi Iwai 	params = kzalloc(sizeof(*params), GFP_KERNEL);
549b21c60a4SVinod Koul 	if (!params)
550b21c60a4SVinod Koul 		return -ENOMEM;
551b21c60a4SVinod Koul 	retval = stream->ops->get_params(stream, params);
552b21c60a4SVinod Koul 	if (retval)
553b21c60a4SVinod Koul 		goto out;
554b21c60a4SVinod Koul 	if (copy_to_user((char __user *)arg, params, sizeof(*params)))
555b21c60a4SVinod Koul 		retval = -EFAULT;
556b21c60a4SVinod Koul 
557b21c60a4SVinod Koul out:
558b21c60a4SVinod Koul 	kfree(params);
559b21c60a4SVinod Koul 	return retval;
560b21c60a4SVinod Koul }
561b21c60a4SVinod Koul 
5629727b490SJeeja KP static int
5639727b490SJeeja KP snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
5649727b490SJeeja KP {
5659727b490SJeeja KP 	struct snd_compr_metadata metadata;
5669727b490SJeeja KP 	int retval;
5679727b490SJeeja KP 
5689727b490SJeeja KP 	if (!stream->ops->get_metadata)
5699727b490SJeeja KP 		return -ENXIO;
5709727b490SJeeja KP 
5719727b490SJeeja KP 	if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
5729727b490SJeeja KP 		return -EFAULT;
5739727b490SJeeja KP 
5749727b490SJeeja KP 	retval = stream->ops->get_metadata(stream, &metadata);
5759727b490SJeeja KP 	if (retval != 0)
5769727b490SJeeja KP 		return retval;
5779727b490SJeeja KP 
5789727b490SJeeja KP 	if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
5799727b490SJeeja KP 		return -EFAULT;
5809727b490SJeeja KP 
5819727b490SJeeja KP 	return 0;
5829727b490SJeeja KP }
5839727b490SJeeja KP 
5849727b490SJeeja KP static int
5859727b490SJeeja KP snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
5869727b490SJeeja KP {
5879727b490SJeeja KP 	struct snd_compr_metadata metadata;
5889727b490SJeeja KP 	int retval;
5899727b490SJeeja KP 
5909727b490SJeeja KP 	if (!stream->ops->set_metadata)
5919727b490SJeeja KP 		return -ENXIO;
5929727b490SJeeja KP 	/*
5939727b490SJeeja KP 	* we should allow parameter change only when stream has been
5949727b490SJeeja KP 	* opened not in other cases
5959727b490SJeeja KP 	*/
5969727b490SJeeja KP 	if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
5979727b490SJeeja KP 		return -EFAULT;
5989727b490SJeeja KP 
5999727b490SJeeja KP 	retval = stream->ops->set_metadata(stream, &metadata);
6009727b490SJeeja KP 	stream->metadata_set = true;
6019727b490SJeeja KP 
6029727b490SJeeja KP 	return retval;
6039727b490SJeeja KP }
6049727b490SJeeja KP 
605b21c60a4SVinod Koul static inline int
606b21c60a4SVinod Koul snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
607b21c60a4SVinod Koul {
60817ac8e5cSRichard Fitzgerald 	struct snd_compr_tstamp tstamp = {0};
60917ac8e5cSRichard Fitzgerald 	int ret;
610b21c60a4SVinod Koul 
61117ac8e5cSRichard Fitzgerald 	ret = snd_compr_update_tstamp(stream, &tstamp);
61217ac8e5cSRichard Fitzgerald 	if (ret == 0)
61317ac8e5cSRichard Fitzgerald 		ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
614b21c60a4SVinod Koul 			&tstamp, sizeof(tstamp)) ? -EFAULT : 0;
61517ac8e5cSRichard Fitzgerald 	return ret;
616b21c60a4SVinod Koul }
617b21c60a4SVinod Koul 
618b21c60a4SVinod Koul static int snd_compr_pause(struct snd_compr_stream *stream)
619b21c60a4SVinod Koul {
620b21c60a4SVinod Koul 	int retval;
621b21c60a4SVinod Koul 
622b21c60a4SVinod Koul 	if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
623b21c60a4SVinod Koul 		return -EPERM;
624b21c60a4SVinod Koul 	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
6256b18f793SVinod Koul 	if (!retval)
626b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
627b21c60a4SVinod Koul 	return retval;
628b21c60a4SVinod Koul }
629b21c60a4SVinod Koul 
630b21c60a4SVinod Koul static int snd_compr_resume(struct snd_compr_stream *stream)
631b21c60a4SVinod Koul {
632b21c60a4SVinod Koul 	int retval;
633b21c60a4SVinod Koul 
634b21c60a4SVinod Koul 	if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
635b21c60a4SVinod Koul 		return -EPERM;
636b21c60a4SVinod Koul 	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
637b21c60a4SVinod Koul 	if (!retval)
638b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
639b21c60a4SVinod Koul 	return retval;
640b21c60a4SVinod Koul }
641b21c60a4SVinod Koul 
642b21c60a4SVinod Koul static int snd_compr_start(struct snd_compr_stream *stream)
643b21c60a4SVinod Koul {
644b21c60a4SVinod Koul 	int retval;
645b21c60a4SVinod Koul 
646b21c60a4SVinod Koul 	if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
647b21c60a4SVinod Koul 		return -EPERM;
648b21c60a4SVinod Koul 	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
649b21c60a4SVinod Koul 	if (!retval)
650b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
651b21c60a4SVinod Koul 	return retval;
652b21c60a4SVinod Koul }
653b21c60a4SVinod Koul 
654b21c60a4SVinod Koul static int snd_compr_stop(struct snd_compr_stream *stream)
655b21c60a4SVinod Koul {
656b21c60a4SVinod Koul 	int retval;
657b21c60a4SVinod Koul 
658b21c60a4SVinod Koul 	if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
659b21c60a4SVinod Koul 			stream->runtime->state == SNDRV_PCM_STATE_SETUP)
660b21c60a4SVinod Koul 		return -EPERM;
661b21c60a4SVinod Koul 	retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
662b21c60a4SVinod Koul 	if (!retval) {
663b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
664b21c60a4SVinod Koul 		wake_up(&stream->runtime->sleep);
6658b21460aSVinod Koul 		stream->runtime->total_bytes_available = 0;
6668b21460aSVinod Koul 		stream->runtime->total_bytes_transferred = 0;
667b21c60a4SVinod Koul 	}
668b21c60a4SVinod Koul 	return retval;
669b21c60a4SVinod Koul }
670b21c60a4SVinod Koul 
671b21c60a4SVinod Koul static int snd_compr_drain(struct snd_compr_stream *stream)
672b21c60a4SVinod Koul {
673b21c60a4SVinod Koul 	int retval;
674b21c60a4SVinod Koul 
675b21c60a4SVinod Koul 	if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
676b21c60a4SVinod Koul 			stream->runtime->state == SNDRV_PCM_STATE_SETUP)
677b21c60a4SVinod Koul 		return -EPERM;
678b21c60a4SVinod Koul 	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
679b21c60a4SVinod Koul 	if (!retval) {
680b21c60a4SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
681b21c60a4SVinod Koul 		wake_up(&stream->runtime->sleep);
682b21c60a4SVinod Koul 	}
683b21c60a4SVinod Koul 	return retval;
684b21c60a4SVinod Koul }
685b21c60a4SVinod Koul 
6869727b490SJeeja KP static int snd_compr_next_track(struct snd_compr_stream *stream)
6879727b490SJeeja KP {
6889727b490SJeeja KP 	int retval;
6899727b490SJeeja KP 
6909727b490SJeeja KP 	/* only a running stream can transition to next track */
6919727b490SJeeja KP 	if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
6929727b490SJeeja KP 		return -EPERM;
6939727b490SJeeja KP 
6949727b490SJeeja KP 	/* you can signal next track isf this is intended to be a gapless stream
6959727b490SJeeja KP 	 * and current track metadata is set
6969727b490SJeeja KP 	 */
6979727b490SJeeja KP 	if (stream->metadata_set == false)
6989727b490SJeeja KP 		return -EPERM;
6999727b490SJeeja KP 
7009727b490SJeeja KP 	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
7019727b490SJeeja KP 	if (retval != 0)
7029727b490SJeeja KP 		return retval;
7039727b490SJeeja KP 	stream->metadata_set = false;
7049727b490SJeeja KP 	stream->next_track = true;
7059727b490SJeeja KP 	return 0;
7069727b490SJeeja KP }
7079727b490SJeeja KP 
7089727b490SJeeja KP static int snd_compr_partial_drain(struct snd_compr_stream *stream)
7099727b490SJeeja KP {
7109727b490SJeeja KP 	int retval;
7119727b490SJeeja KP 	if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
7129727b490SJeeja KP 			stream->runtime->state == SNDRV_PCM_STATE_SETUP)
7139727b490SJeeja KP 		return -EPERM;
7149727b490SJeeja KP 	/* stream can be drained only when next track has been signalled */
7159727b490SJeeja KP 	if (stream->next_track == false)
7169727b490SJeeja KP 		return -EPERM;
7179727b490SJeeja KP 
7189727b490SJeeja KP 	retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
7199727b490SJeeja KP 
7209727b490SJeeja KP 	stream->next_track = false;
7219727b490SJeeja KP 	return retval;
7229727b490SJeeja KP }
7239727b490SJeeja KP 
724b21c60a4SVinod Koul static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
725b21c60a4SVinod Koul {
726b21c60a4SVinod Koul 	struct snd_compr_file *data = f->private_data;
727b21c60a4SVinod Koul 	struct snd_compr_stream *stream;
728b21c60a4SVinod Koul 	int retval = -ENOTTY;
729b21c60a4SVinod Koul 
730b21c60a4SVinod Koul 	if (snd_BUG_ON(!data))
731b21c60a4SVinod Koul 		return -EFAULT;
732b21c60a4SVinod Koul 	stream = &data->stream;
733b21c60a4SVinod Koul 	if (snd_BUG_ON(!stream))
734b21c60a4SVinod Koul 		return -EFAULT;
735b21c60a4SVinod Koul 	mutex_lock(&stream->device->lock);
736b21c60a4SVinod Koul 	switch (_IOC_NR(cmd)) {
737b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
738b21c60a4SVinod Koul 		put_user(SNDRV_COMPRESS_VERSION,
739b21c60a4SVinod Koul 				(int __user *)arg) ? -EFAULT : 0;
740b21c60a4SVinod Koul 		break;
741b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
742b21c60a4SVinod Koul 		retval = snd_compr_get_caps(stream, arg);
743b21c60a4SVinod Koul 		break;
744b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
745b21c60a4SVinod Koul 		retval = snd_compr_get_codec_caps(stream, arg);
746b21c60a4SVinod Koul 		break;
747b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
748b21c60a4SVinod Koul 		retval = snd_compr_set_params(stream, arg);
749b21c60a4SVinod Koul 		break;
750b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
751b21c60a4SVinod Koul 		retval = snd_compr_get_params(stream, arg);
752b21c60a4SVinod Koul 		break;
7539727b490SJeeja KP 	case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
7549727b490SJeeja KP 		retval = snd_compr_set_metadata(stream, arg);
7559727b490SJeeja KP 		break;
7569727b490SJeeja KP 	case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
7579727b490SJeeja KP 		retval = snd_compr_get_metadata(stream, arg);
7589727b490SJeeja KP 		break;
759b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
760b21c60a4SVinod Koul 		retval = snd_compr_tstamp(stream, arg);
761b21c60a4SVinod Koul 		break;
762b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_AVAIL):
763b21c60a4SVinod Koul 		retval = snd_compr_ioctl_avail(stream, arg);
764b21c60a4SVinod Koul 		break;
765b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_PAUSE):
766b21c60a4SVinod Koul 		retval = snd_compr_pause(stream);
767b21c60a4SVinod Koul 		break;
768b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_RESUME):
769b21c60a4SVinod Koul 		retval = snd_compr_resume(stream);
770b21c60a4SVinod Koul 		break;
771b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_START):
772b21c60a4SVinod Koul 		retval = snd_compr_start(stream);
773b21c60a4SVinod Koul 		break;
774b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_STOP):
775b21c60a4SVinod Koul 		retval = snd_compr_stop(stream);
776b21c60a4SVinod Koul 		break;
777b21c60a4SVinod Koul 	case _IOC_NR(SNDRV_COMPRESS_DRAIN):
778b21c60a4SVinod Koul 		retval = snd_compr_drain(stream);
779b21c60a4SVinod Koul 		break;
7809727b490SJeeja KP 	case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
7819727b490SJeeja KP 		retval = snd_compr_partial_drain(stream);
7829727b490SJeeja KP 		break;
7839727b490SJeeja KP 	case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
7849727b490SJeeja KP 		retval = snd_compr_next_track(stream);
7859727b490SJeeja KP 		break;
7869727b490SJeeja KP 
787b21c60a4SVinod Koul 	}
788b21c60a4SVinod Koul 	mutex_unlock(&stream->device->lock);
789b21c60a4SVinod Koul 	return retval;
790b21c60a4SVinod Koul }
791b21c60a4SVinod Koul 
792b21c60a4SVinod Koul static const struct file_operations snd_compr_file_ops = {
793b21c60a4SVinod Koul 		.owner =	THIS_MODULE,
794b21c60a4SVinod Koul 		.open =		snd_compr_open,
795b21c60a4SVinod Koul 		.release =	snd_compr_free,
796b21c60a4SVinod Koul 		.write =	snd_compr_write,
797b21c60a4SVinod Koul 		.read =		snd_compr_read,
798b21c60a4SVinod Koul 		.unlocked_ioctl = snd_compr_ioctl,
799b21c60a4SVinod Koul 		.mmap =		snd_compr_mmap,
800b21c60a4SVinod Koul 		.poll =		snd_compr_poll,
801b21c60a4SVinod Koul };
802b21c60a4SVinod Koul 
803b21c60a4SVinod Koul static int snd_compress_dev_register(struct snd_device *device)
804b21c60a4SVinod Koul {
805b21c60a4SVinod Koul 	int ret = -EINVAL;
806b21c60a4SVinod Koul 	char str[16];
807b21c60a4SVinod Koul 	struct snd_compr *compr;
808b21c60a4SVinod Koul 
809b21c60a4SVinod Koul 	if (snd_BUG_ON(!device || !device->device_data))
810b21c60a4SVinod Koul 		return -EBADFD;
811b21c60a4SVinod Koul 	compr = device->device_data;
812b21c60a4SVinod Koul 
813b21c60a4SVinod Koul 	sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
814b21c60a4SVinod Koul 	pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
815b21c60a4SVinod Koul 			compr->direction);
816b21c60a4SVinod Koul 	/* register compressed device */
817b21c60a4SVinod Koul 	ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
818b21c60a4SVinod Koul 			compr->device, &snd_compr_file_ops, compr, str);
819b21c60a4SVinod Koul 	if (ret < 0) {
820b21c60a4SVinod Koul 		pr_err("snd_register_device failed\n %d", ret);
821b21c60a4SVinod Koul 		return ret;
822b21c60a4SVinod Koul 	}
823b21c60a4SVinod Koul 	return ret;
824b21c60a4SVinod Koul 
825b21c60a4SVinod Koul }
826b21c60a4SVinod Koul 
827b21c60a4SVinod Koul static int snd_compress_dev_disconnect(struct snd_device *device)
828b21c60a4SVinod Koul {
829b21c60a4SVinod Koul 	struct snd_compr *compr;
830b21c60a4SVinod Koul 
831b21c60a4SVinod Koul 	compr = device->device_data;
832b21c60a4SVinod Koul 	snd_unregister_device(compr->direction, compr->card, compr->device);
833b21c60a4SVinod Koul 	return 0;
834b21c60a4SVinod Koul }
835b21c60a4SVinod Koul 
836b21c60a4SVinod Koul /*
837b21c60a4SVinod Koul  * snd_compress_new: create new compress device
838b21c60a4SVinod Koul  * @card: sound card pointer
839b21c60a4SVinod Koul  * @device: device number
840b21c60a4SVinod Koul  * @dirn: device direction, should be of type enum snd_compr_direction
841b21c60a4SVinod Koul  * @compr: compress device pointer
842b21c60a4SVinod Koul  */
843b21c60a4SVinod Koul int snd_compress_new(struct snd_card *card, int device,
844b21c60a4SVinod Koul 			int dirn, struct snd_compr *compr)
845b21c60a4SVinod Koul {
846b21c60a4SVinod Koul 	static struct snd_device_ops ops = {
847b21c60a4SVinod Koul 		.dev_free = NULL,
848b21c60a4SVinod Koul 		.dev_register = snd_compress_dev_register,
849b21c60a4SVinod Koul 		.dev_disconnect = snd_compress_dev_disconnect,
850b21c60a4SVinod Koul 	};
851b21c60a4SVinod Koul 
852b21c60a4SVinod Koul 	compr->card = card;
853b21c60a4SVinod Koul 	compr->device = device;
854b21c60a4SVinod Koul 	compr->direction = dirn;
855b21c60a4SVinod Koul 	return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
856b21c60a4SVinod Koul }
857b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_new);
858b21c60a4SVinod Koul 
859b21c60a4SVinod Koul static int snd_compress_add_device(struct snd_compr *device)
860b21c60a4SVinod Koul {
861b21c60a4SVinod Koul 	int ret;
862b21c60a4SVinod Koul 
863b21c60a4SVinod Koul 	if (!device->card)
864b21c60a4SVinod Koul 		return -EINVAL;
865b21c60a4SVinod Koul 
866b21c60a4SVinod Koul 	/* register the card */
867b21c60a4SVinod Koul 	ret = snd_card_register(device->card);
868b21c60a4SVinod Koul 	if (ret)
869b21c60a4SVinod Koul 		goto out;
870b21c60a4SVinod Koul 	return 0;
871b21c60a4SVinod Koul 
872b21c60a4SVinod Koul out:
873b21c60a4SVinod Koul 	pr_err("failed with %d\n", ret);
874b21c60a4SVinod Koul 	return ret;
875b21c60a4SVinod Koul 
876b21c60a4SVinod Koul }
877b21c60a4SVinod Koul 
878b21c60a4SVinod Koul static int snd_compress_remove_device(struct snd_compr *device)
879b21c60a4SVinod Koul {
880b21c60a4SVinod Koul 	return snd_card_free(device->card);
881b21c60a4SVinod Koul }
882b21c60a4SVinod Koul 
883b21c60a4SVinod Koul /**
884b21c60a4SVinod Koul  * snd_compress_register - register compressed device
885b21c60a4SVinod Koul  *
886b21c60a4SVinod Koul  * @device: compressed device to register
887b21c60a4SVinod Koul  */
888b21c60a4SVinod Koul int snd_compress_register(struct snd_compr *device)
889b21c60a4SVinod Koul {
890b21c60a4SVinod Koul 	int retval;
891b21c60a4SVinod Koul 
892b21c60a4SVinod Koul 	if (device->name == NULL || device->dev == NULL || device->ops == NULL)
893b21c60a4SVinod Koul 		return -EINVAL;
894b21c60a4SVinod Koul 
895b21c60a4SVinod Koul 	pr_debug("Registering compressed device %s\n", device->name);
896b21c60a4SVinod Koul 	if (snd_BUG_ON(!device->ops->open))
897b21c60a4SVinod Koul 		return -EINVAL;
898b21c60a4SVinod Koul 	if (snd_BUG_ON(!device->ops->free))
899b21c60a4SVinod Koul 		return -EINVAL;
900b21c60a4SVinod Koul 	if (snd_BUG_ON(!device->ops->set_params))
901b21c60a4SVinod Koul 		return -EINVAL;
902b21c60a4SVinod Koul 	if (snd_BUG_ON(!device->ops->trigger))
903b21c60a4SVinod Koul 		return -EINVAL;
904b21c60a4SVinod Koul 
905b21c60a4SVinod Koul 	mutex_init(&device->lock);
906b21c60a4SVinod Koul 
907b21c60a4SVinod Koul 	/* register a compressed card */
908b21c60a4SVinod Koul 	mutex_lock(&device_mutex);
909b21c60a4SVinod Koul 	retval = snd_compress_add_device(device);
910b21c60a4SVinod Koul 	mutex_unlock(&device_mutex);
911b21c60a4SVinod Koul 	return retval;
912b21c60a4SVinod Koul }
913b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_register);
914b21c60a4SVinod Koul 
915b21c60a4SVinod Koul int snd_compress_deregister(struct snd_compr *device)
916b21c60a4SVinod Koul {
917b21c60a4SVinod Koul 	pr_debug("Removing compressed device %s\n", device->name);
918b21c60a4SVinod Koul 	mutex_lock(&device_mutex);
919b21c60a4SVinod Koul 	snd_compress_remove_device(device);
920b21c60a4SVinod Koul 	mutex_unlock(&device_mutex);
921b21c60a4SVinod Koul 	return 0;
922b21c60a4SVinod Koul }
923b21c60a4SVinod Koul EXPORT_SYMBOL_GPL(snd_compress_deregister);
924b21c60a4SVinod Koul 
925b21c60a4SVinod Koul static int __init snd_compress_init(void)
926b21c60a4SVinod Koul {
927b21c60a4SVinod Koul 	return 0;
928b21c60a4SVinod Koul }
929b21c60a4SVinod Koul 
930b21c60a4SVinod Koul static void __exit snd_compress_exit(void)
931b21c60a4SVinod Koul {
932b21c60a4SVinod Koul }
933b21c60a4SVinod Koul 
934b21c60a4SVinod Koul module_init(snd_compress_init);
935b21c60a4SVinod Koul module_exit(snd_compress_exit);
936b21c60a4SVinod Koul 
937b21c60a4SVinod Koul MODULE_DESCRIPTION("ALSA Compressed offload framework");
938b21c60a4SVinod Koul MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
939b21c60a4SVinod Koul MODULE_LICENSE("GPL v2");
940