Commit 769fab2a41da4bd3c59eee38f47d6d5405738fe0

Authored by Jesper Juhl
Committed by Takashi Iwai
1 parent 4d20bb1d5f

ALSA: Fix memory leak on error in snd_compr_set_params()

If copy_from_user() does not return 0 we'll leak the memory we
allocated for 'params' when that variable goes out of scope.

Also a small CodingStyle cleanup: Use braces on both branches of
if/else when one branch needs it.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Acked-by: Vinod Koul <vinod.koul@linux.intel.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

Showing 1 changed file with 8 additions and 5 deletions Inline Diff

sound/core/compress_offload.c
1 /* 1 /*
2 * compress_core.c - compress offload core 2 * compress_core.c - compress offload core
3 * 3 *
4 * Copyright (C) 2011 Intel Corporation 4 * Copyright (C) 2011 Intel Corporation
5 * Authors: Vinod Koul <vinod.koul@linux.intel.com> 5 * Authors: Vinod Koul <vinod.koul@linux.intel.com>
6 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> 6 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License. 11 * the Free Software Foundation; version 2 of the License.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, but 13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 16 * General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License along 18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc., 19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * 21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 * 23 *
24 */ 24 */
25 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ 25 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
27 27
28 #include <linux/file.h> 28 #include <linux/file.h>
29 #include <linux/fs.h> 29 #include <linux/fs.h>
30 #include <linux/list.h> 30 #include <linux/list.h>
31 #include <linux/mm.h> 31 #include <linux/mm.h>
32 #include <linux/mutex.h> 32 #include <linux/mutex.h>
33 #include <linux/poll.h> 33 #include <linux/poll.h>
34 #include <linux/slab.h> 34 #include <linux/slab.h>
35 #include <linux/sched.h> 35 #include <linux/sched.h>
36 #include <linux/uio.h> 36 #include <linux/uio.h>
37 #include <linux/uaccess.h> 37 #include <linux/uaccess.h>
38 #include <linux/module.h> 38 #include <linux/module.h>
39 #include <sound/core.h> 39 #include <sound/core.h>
40 #include <sound/initval.h> 40 #include <sound/initval.h>
41 #include <sound/compress_params.h> 41 #include <sound/compress_params.h>
42 #include <sound/compress_offload.h> 42 #include <sound/compress_offload.h>
43 #include <sound/compress_driver.h> 43 #include <sound/compress_driver.h>
44 44
45 /* TODO: 45 /* TODO:
46 * - add substream support for multiple devices in case of 46 * - add substream support for multiple devices in case of
47 * SND_DYNAMIC_MINORS is not used 47 * SND_DYNAMIC_MINORS is not used
48 * - Multiple node representation 48 * - Multiple node representation
49 * driver should be able to register multiple nodes 49 * driver should be able to register multiple nodes
50 */ 50 */
51 51
52 static DEFINE_MUTEX(device_mutex); 52 static DEFINE_MUTEX(device_mutex);
53 53
54 struct snd_compr_file { 54 struct snd_compr_file {
55 unsigned long caps; 55 unsigned long caps;
56 struct snd_compr_stream stream; 56 struct snd_compr_stream stream;
57 }; 57 };
58 58
59 /* 59 /*
60 * a note on stream states used: 60 * a note on stream states used:
61 * we use follwing states in the compressed core 61 * we use follwing states in the compressed core
62 * SNDRV_PCM_STATE_OPEN: When stream has been opened. 62 * SNDRV_PCM_STATE_OPEN: When stream has been opened.
63 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by 63 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
64 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this 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. 65 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
66 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is 66 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
67 * decoding/encoding and rendering/capturing data. 67 * decoding/encoding and rendering/capturing data.
68 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done 68 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
69 * by calling SNDRV_COMPRESS_DRAIN. 69 * by calling SNDRV_COMPRESS_DRAIN.
70 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling 70 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
71 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling 71 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
72 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. 72 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
73 */ 73 */
74 static int snd_compr_open(struct inode *inode, struct file *f) 74 static int snd_compr_open(struct inode *inode, struct file *f)
75 { 75 {
76 struct snd_compr *compr; 76 struct snd_compr *compr;
77 struct snd_compr_file *data; 77 struct snd_compr_file *data;
78 struct snd_compr_runtime *runtime; 78 struct snd_compr_runtime *runtime;
79 enum snd_compr_direction dirn; 79 enum snd_compr_direction dirn;
80 int maj = imajor(inode); 80 int maj = imajor(inode);
81 int ret; 81 int ret;
82 82
83 if (f->f_flags & O_WRONLY) 83 if (f->f_flags & O_WRONLY)
84 dirn = SND_COMPRESS_PLAYBACK; 84 dirn = SND_COMPRESS_PLAYBACK;
85 else if (f->f_flags & O_RDONLY) 85 else if (f->f_flags & O_RDONLY)
86 dirn = SND_COMPRESS_CAPTURE; 86 dirn = SND_COMPRESS_CAPTURE;
87 else { 87 else {
88 pr_err("invalid direction\n"); 88 pr_err("invalid direction\n");
89 return -EINVAL; 89 return -EINVAL;
90 } 90 }
91 91
92 if (maj == snd_major) 92 if (maj == snd_major)
93 compr = snd_lookup_minor_data(iminor(inode), 93 compr = snd_lookup_minor_data(iminor(inode),
94 SNDRV_DEVICE_TYPE_COMPRESS); 94 SNDRV_DEVICE_TYPE_COMPRESS);
95 else 95 else
96 return -EBADFD; 96 return -EBADFD;
97 97
98 if (compr == NULL) { 98 if (compr == NULL) {
99 pr_err("no device data!!!\n"); 99 pr_err("no device data!!!\n");
100 return -ENODEV; 100 return -ENODEV;
101 } 101 }
102 102
103 if (dirn != compr->direction) { 103 if (dirn != compr->direction) {
104 pr_err("this device doesn't support this direction\n"); 104 pr_err("this device doesn't support this direction\n");
105 return -EINVAL; 105 return -EINVAL;
106 } 106 }
107 107
108 data = kzalloc(sizeof(*data), GFP_KERNEL); 108 data = kzalloc(sizeof(*data), GFP_KERNEL);
109 if (!data) 109 if (!data)
110 return -ENOMEM; 110 return -ENOMEM;
111 data->stream.ops = compr->ops; 111 data->stream.ops = compr->ops;
112 data->stream.direction = dirn; 112 data->stream.direction = dirn;
113 data->stream.private_data = compr->private_data; 113 data->stream.private_data = compr->private_data;
114 data->stream.device = compr; 114 data->stream.device = compr;
115 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 115 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
116 if (!runtime) { 116 if (!runtime) {
117 kfree(data); 117 kfree(data);
118 return -ENOMEM; 118 return -ENOMEM;
119 } 119 }
120 runtime->state = SNDRV_PCM_STATE_OPEN; 120 runtime->state = SNDRV_PCM_STATE_OPEN;
121 init_waitqueue_head(&runtime->sleep); 121 init_waitqueue_head(&runtime->sleep);
122 data->stream.runtime = runtime; 122 data->stream.runtime = runtime;
123 f->private_data = (void *)data; 123 f->private_data = (void *)data;
124 mutex_lock(&compr->lock); 124 mutex_lock(&compr->lock);
125 ret = compr->ops->open(&data->stream); 125 ret = compr->ops->open(&data->stream);
126 mutex_unlock(&compr->lock); 126 mutex_unlock(&compr->lock);
127 if (ret) { 127 if (ret) {
128 kfree(runtime); 128 kfree(runtime);
129 kfree(data); 129 kfree(data);
130 } 130 }
131 return ret; 131 return ret;
132 } 132 }
133 133
134 static int snd_compr_free(struct inode *inode, struct file *f) 134 static int snd_compr_free(struct inode *inode, struct file *f)
135 { 135 {
136 struct snd_compr_file *data = f->private_data; 136 struct snd_compr_file *data = f->private_data;
137 data->stream.ops->free(&data->stream); 137 data->stream.ops->free(&data->stream);
138 kfree(data->stream.runtime->buffer); 138 kfree(data->stream.runtime->buffer);
139 kfree(data->stream.runtime); 139 kfree(data->stream.runtime);
140 kfree(data); 140 kfree(data);
141 return 0; 141 return 0;
142 } 142 }
143 143
144 static void snd_compr_update_tstamp(struct snd_compr_stream *stream, 144 static void snd_compr_update_tstamp(struct snd_compr_stream *stream,
145 struct snd_compr_tstamp *tstamp) 145 struct snd_compr_tstamp *tstamp)
146 { 146 {
147 if (!stream->ops->pointer) 147 if (!stream->ops->pointer)
148 return; 148 return;
149 stream->ops->pointer(stream, tstamp); 149 stream->ops->pointer(stream, tstamp);
150 pr_debug("dsp consumed till %d total %d bytes\n", 150 pr_debug("dsp consumed till %d total %d bytes\n",
151 tstamp->byte_offset, tstamp->copied_total); 151 tstamp->byte_offset, tstamp->copied_total);
152 stream->runtime->hw_pointer = tstamp->byte_offset; 152 stream->runtime->hw_pointer = tstamp->byte_offset;
153 stream->runtime->total_bytes_transferred = tstamp->copied_total; 153 stream->runtime->total_bytes_transferred = tstamp->copied_total;
154 } 154 }
155 155
156 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, 156 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
157 struct snd_compr_avail *avail) 157 struct snd_compr_avail *avail)
158 { 158 {
159 long avail_calc; /*this needs to be signed variable */ 159 long avail_calc; /*this needs to be signed variable */
160 160
161 snd_compr_update_tstamp(stream, &avail->tstamp); 161 snd_compr_update_tstamp(stream, &avail->tstamp);
162 162
163 /* FIXME: This needs to be different for capture stream, 163 /* FIXME: This needs to be different for capture stream,
164 available is # of compressed data, for playback it's 164 available is # of compressed data, for playback it's
165 remainder of buffer */ 165 remainder of buffer */
166 166
167 if (stream->runtime->total_bytes_available == 0 && 167 if (stream->runtime->total_bytes_available == 0 &&
168 stream->runtime->state == SNDRV_PCM_STATE_SETUP) { 168 stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
169 pr_debug("detected init and someone forgot to do a write\n"); 169 pr_debug("detected init and someone forgot to do a write\n");
170 return stream->runtime->buffer_size; 170 return stream->runtime->buffer_size;
171 } 171 }
172 pr_debug("app wrote %lld, DSP consumed %lld\n", 172 pr_debug("app wrote %lld, DSP consumed %lld\n",
173 stream->runtime->total_bytes_available, 173 stream->runtime->total_bytes_available,
174 stream->runtime->total_bytes_transferred); 174 stream->runtime->total_bytes_transferred);
175 if (stream->runtime->total_bytes_available == 175 if (stream->runtime->total_bytes_available ==
176 stream->runtime->total_bytes_transferred) { 176 stream->runtime->total_bytes_transferred) {
177 pr_debug("both pointers are same, returning full avail\n"); 177 pr_debug("both pointers are same, returning full avail\n");
178 return stream->runtime->buffer_size; 178 return stream->runtime->buffer_size;
179 } 179 }
180 180
181 /* FIXME: this routine isn't consistent, in one test we use 181 /* FIXME: this routine isn't consistent, in one test we use
182 * cumulative values and in the other byte offsets. Do we 182 * cumulative values and in the other byte offsets. Do we
183 * really need the byte offsets if the cumulative values have 183 * really need the byte offsets if the cumulative values have
184 * been updated? In the PCM interface app_ptr and hw_ptr are 184 * been updated? In the PCM interface app_ptr and hw_ptr are
185 * already cumulative */ 185 * already cumulative */
186 186
187 avail_calc = stream->runtime->buffer_size - 187 avail_calc = stream->runtime->buffer_size -
188 (stream->runtime->app_pointer - stream->runtime->hw_pointer); 188 (stream->runtime->app_pointer - stream->runtime->hw_pointer);
189 pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc, 189 pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc,
190 stream->runtime->app_pointer, 190 stream->runtime->app_pointer,
191 stream->runtime->hw_pointer); 191 stream->runtime->hw_pointer);
192 if (avail_calc >= stream->runtime->buffer_size) 192 if (avail_calc >= stream->runtime->buffer_size)
193 avail_calc -= stream->runtime->buffer_size; 193 avail_calc -= stream->runtime->buffer_size;
194 pr_debug("ret avail as %ld\n", avail_calc); 194 pr_debug("ret avail as %ld\n", avail_calc);
195 avail->avail = avail_calc; 195 avail->avail = avail_calc;
196 return avail_calc; 196 return avail_calc;
197 } 197 }
198 198
199 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream) 199 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
200 { 200 {
201 struct snd_compr_avail avail; 201 struct snd_compr_avail avail;
202 202
203 return snd_compr_calc_avail(stream, &avail); 203 return snd_compr_calc_avail(stream, &avail);
204 } 204 }
205 205
206 static int 206 static int
207 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg) 207 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
208 { 208 {
209 struct snd_compr_avail ioctl_avail; 209 struct snd_compr_avail ioctl_avail;
210 size_t avail; 210 size_t avail;
211 211
212 avail = snd_compr_calc_avail(stream, &ioctl_avail); 212 avail = snd_compr_calc_avail(stream, &ioctl_avail);
213 ioctl_avail.avail = avail; 213 ioctl_avail.avail = avail;
214 214
215 if (copy_to_user((__u64 __user *)arg, 215 if (copy_to_user((__u64 __user *)arg,
216 &ioctl_avail, sizeof(ioctl_avail))) 216 &ioctl_avail, sizeof(ioctl_avail)))
217 return -EFAULT; 217 return -EFAULT;
218 return 0; 218 return 0;
219 } 219 }
220 220
221 static int snd_compr_write_data(struct snd_compr_stream *stream, 221 static int snd_compr_write_data(struct snd_compr_stream *stream,
222 const char __user *buf, size_t count) 222 const char __user *buf, size_t count)
223 { 223 {
224 void *dstn; 224 void *dstn;
225 size_t copy; 225 size_t copy;
226 struct snd_compr_runtime *runtime = stream->runtime; 226 struct snd_compr_runtime *runtime = stream->runtime;
227 227
228 dstn = runtime->buffer + runtime->app_pointer; 228 dstn = runtime->buffer + runtime->app_pointer;
229 pr_debug("copying %ld at %lld\n", 229 pr_debug("copying %ld at %lld\n",
230 (unsigned long)count, runtime->app_pointer); 230 (unsigned long)count, runtime->app_pointer);
231 if (count < runtime->buffer_size - runtime->app_pointer) { 231 if (count < runtime->buffer_size - runtime->app_pointer) {
232 if (copy_from_user(dstn, buf, count)) 232 if (copy_from_user(dstn, buf, count))
233 return -EFAULT; 233 return -EFAULT;
234 runtime->app_pointer += count; 234 runtime->app_pointer += count;
235 } else { 235 } else {
236 copy = runtime->buffer_size - runtime->app_pointer; 236 copy = runtime->buffer_size - runtime->app_pointer;
237 if (copy_from_user(dstn, buf, copy)) 237 if (copy_from_user(dstn, buf, copy))
238 return -EFAULT; 238 return -EFAULT;
239 if (copy_from_user(runtime->buffer, buf + copy, count - copy)) 239 if (copy_from_user(runtime->buffer, buf + copy, count - copy))
240 return -EFAULT; 240 return -EFAULT;
241 runtime->app_pointer = count - copy; 241 runtime->app_pointer = count - copy;
242 } 242 }
243 /* if DSP cares, let it know data has been written */ 243 /* if DSP cares, let it know data has been written */
244 if (stream->ops->ack) 244 if (stream->ops->ack)
245 stream->ops->ack(stream, count); 245 stream->ops->ack(stream, count);
246 return count; 246 return count;
247 } 247 }
248 248
249 static ssize_t snd_compr_write(struct file *f, const char __user *buf, 249 static ssize_t snd_compr_write(struct file *f, const char __user *buf,
250 size_t count, loff_t *offset) 250 size_t count, loff_t *offset)
251 { 251 {
252 struct snd_compr_file *data = f->private_data; 252 struct snd_compr_file *data = f->private_data;
253 struct snd_compr_stream *stream; 253 struct snd_compr_stream *stream;
254 size_t avail; 254 size_t avail;
255 int retval; 255 int retval;
256 256
257 if (snd_BUG_ON(!data)) 257 if (snd_BUG_ON(!data))
258 return -EFAULT; 258 return -EFAULT;
259 259
260 stream = &data->stream; 260 stream = &data->stream;
261 mutex_lock(&stream->device->lock); 261 mutex_lock(&stream->device->lock);
262 /* write is allowed when stream is running or has been steup */ 262 /* write is allowed when stream is running or has been steup */
263 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP && 263 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
264 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) { 264 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
265 mutex_unlock(&stream->device->lock); 265 mutex_unlock(&stream->device->lock);
266 return -EBADFD; 266 return -EBADFD;
267 } 267 }
268 268
269 avail = snd_compr_get_avail(stream); 269 avail = snd_compr_get_avail(stream);
270 pr_debug("avail returned %ld\n", (unsigned long)avail); 270 pr_debug("avail returned %ld\n", (unsigned long)avail);
271 /* calculate how much we can write to buffer */ 271 /* calculate how much we can write to buffer */
272 if (avail > count) 272 if (avail > count)
273 avail = count; 273 avail = count;
274 274
275 if (stream->ops->copy) 275 if (stream->ops->copy)
276 retval = stream->ops->copy(stream, buf, avail); 276 retval = stream->ops->copy(stream, buf, avail);
277 else 277 else
278 retval = snd_compr_write_data(stream, buf, avail); 278 retval = snd_compr_write_data(stream, buf, avail);
279 if (retval > 0) 279 if (retval > 0)
280 stream->runtime->total_bytes_available += retval; 280 stream->runtime->total_bytes_available += retval;
281 281
282 /* while initiating the stream, write should be called before START 282 /* while initiating the stream, write should be called before START
283 * call, so in setup move state */ 283 * call, so in setup move state */
284 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { 284 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
285 stream->runtime->state = SNDRV_PCM_STATE_PREPARED; 285 stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
286 pr_debug("stream prepared, Houston we are good to go\n"); 286 pr_debug("stream prepared, Houston we are good to go\n");
287 } 287 }
288 288
289 mutex_unlock(&stream->device->lock); 289 mutex_unlock(&stream->device->lock);
290 return retval; 290 return retval;
291 } 291 }
292 292
293 293
294 static ssize_t snd_compr_read(struct file *f, char __user *buf, 294 static ssize_t snd_compr_read(struct file *f, char __user *buf,
295 size_t count, loff_t *offset) 295 size_t count, loff_t *offset)
296 { 296 {
297 return -ENXIO; 297 return -ENXIO;
298 } 298 }
299 299
300 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) 300 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
301 { 301 {
302 return -ENXIO; 302 return -ENXIO;
303 } 303 }
304 304
305 static inline int snd_compr_get_poll(struct snd_compr_stream *stream) 305 static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
306 { 306 {
307 if (stream->direction == SND_COMPRESS_PLAYBACK) 307 if (stream->direction == SND_COMPRESS_PLAYBACK)
308 return POLLOUT | POLLWRNORM; 308 return POLLOUT | POLLWRNORM;
309 else 309 else
310 return POLLIN | POLLRDNORM; 310 return POLLIN | POLLRDNORM;
311 } 311 }
312 312
313 static unsigned int snd_compr_poll(struct file *f, poll_table *wait) 313 static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
314 { 314 {
315 struct snd_compr_file *data = f->private_data; 315 struct snd_compr_file *data = f->private_data;
316 struct snd_compr_stream *stream; 316 struct snd_compr_stream *stream;
317 size_t avail; 317 size_t avail;
318 int retval = 0; 318 int retval = 0;
319 319
320 if (snd_BUG_ON(!data)) 320 if (snd_BUG_ON(!data))
321 return -EFAULT; 321 return -EFAULT;
322 stream = &data->stream; 322 stream = &data->stream;
323 if (snd_BUG_ON(!stream)) 323 if (snd_BUG_ON(!stream))
324 return -EFAULT; 324 return -EFAULT;
325 325
326 mutex_lock(&stream->device->lock); 326 mutex_lock(&stream->device->lock);
327 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED || 327 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
328 stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 328 stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
329 retval = -EBADFD; 329 retval = -EBADFD;
330 goto out; 330 goto out;
331 } 331 }
332 poll_wait(f, &stream->runtime->sleep, wait); 332 poll_wait(f, &stream->runtime->sleep, wait);
333 333
334 avail = snd_compr_get_avail(stream); 334 avail = snd_compr_get_avail(stream);
335 pr_debug("avail is %ld\n", (unsigned long)avail); 335 pr_debug("avail is %ld\n", (unsigned long)avail);
336 /* check if we have at least one fragment to fill */ 336 /* check if we have at least one fragment to fill */
337 switch (stream->runtime->state) { 337 switch (stream->runtime->state) {
338 case SNDRV_PCM_STATE_DRAINING: 338 case SNDRV_PCM_STATE_DRAINING:
339 /* stream has been woken up after drain is complete 339 /* stream has been woken up after drain is complete
340 * draining done so set stream state to stopped 340 * draining done so set stream state to stopped
341 */ 341 */
342 retval = snd_compr_get_poll(stream); 342 retval = snd_compr_get_poll(stream);
343 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 343 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
344 break; 344 break;
345 case SNDRV_PCM_STATE_RUNNING: 345 case SNDRV_PCM_STATE_RUNNING:
346 case SNDRV_PCM_STATE_PREPARED: 346 case SNDRV_PCM_STATE_PREPARED:
347 case SNDRV_PCM_STATE_PAUSED: 347 case SNDRV_PCM_STATE_PAUSED:
348 if (avail >= stream->runtime->fragment_size) 348 if (avail >= stream->runtime->fragment_size)
349 retval = snd_compr_get_poll(stream); 349 retval = snd_compr_get_poll(stream);
350 break; 350 break;
351 default: 351 default:
352 if (stream->direction == SND_COMPRESS_PLAYBACK) 352 if (stream->direction == SND_COMPRESS_PLAYBACK)
353 retval = POLLOUT | POLLWRNORM | POLLERR; 353 retval = POLLOUT | POLLWRNORM | POLLERR;
354 else 354 else
355 retval = POLLIN | POLLRDNORM | POLLERR; 355 retval = POLLIN | POLLRDNORM | POLLERR;
356 break; 356 break;
357 } 357 }
358 out: 358 out:
359 mutex_unlock(&stream->device->lock); 359 mutex_unlock(&stream->device->lock);
360 return retval; 360 return retval;
361 } 361 }
362 362
363 static int 363 static int
364 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg) 364 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
365 { 365 {
366 int retval; 366 int retval;
367 struct snd_compr_caps caps; 367 struct snd_compr_caps caps;
368 368
369 if (!stream->ops->get_caps) 369 if (!stream->ops->get_caps)
370 return -ENXIO; 370 return -ENXIO;
371 371
372 retval = stream->ops->get_caps(stream, &caps); 372 retval = stream->ops->get_caps(stream, &caps);
373 if (retval) 373 if (retval)
374 goto out; 374 goto out;
375 if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 375 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
376 retval = -EFAULT; 376 retval = -EFAULT;
377 out: 377 out:
378 return retval; 378 return retval;
379 } 379 }
380 380
381 static int 381 static int
382 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) 382 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
383 { 383 {
384 int retval; 384 int retval;
385 struct snd_compr_codec_caps *caps; 385 struct snd_compr_codec_caps *caps;
386 386
387 if (!stream->ops->get_codec_caps) 387 if (!stream->ops->get_codec_caps)
388 return -ENXIO; 388 return -ENXIO;
389 389
390 caps = kmalloc(sizeof(*caps), GFP_KERNEL); 390 caps = kmalloc(sizeof(*caps), GFP_KERNEL);
391 if (!caps) 391 if (!caps)
392 return -ENOMEM; 392 return -ENOMEM;
393 393
394 retval = stream->ops->get_codec_caps(stream, caps); 394 retval = stream->ops->get_codec_caps(stream, caps);
395 if (retval) 395 if (retval)
396 goto out; 396 goto out;
397 if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) 397 if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
398 retval = -EFAULT; 398 retval = -EFAULT;
399 399
400 out: 400 out:
401 kfree(caps); 401 kfree(caps);
402 return retval; 402 return retval;
403 } 403 }
404 404
405 /* revisit this with snd_pcm_preallocate_xxx */ 405 /* revisit this with snd_pcm_preallocate_xxx */
406 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream, 406 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
407 struct snd_compr_params *params) 407 struct snd_compr_params *params)
408 { 408 {
409 unsigned int buffer_size; 409 unsigned int buffer_size;
410 void *buffer; 410 void *buffer;
411 411
412 buffer_size = params->buffer.fragment_size * params->buffer.fragments; 412 buffer_size = params->buffer.fragment_size * params->buffer.fragments;
413 if (stream->ops->copy) { 413 if (stream->ops->copy) {
414 buffer = NULL; 414 buffer = NULL;
415 /* if copy is defined the driver will be required to copy 415 /* if copy is defined the driver will be required to copy
416 * the data from core 416 * the data from core
417 */ 417 */
418 } else { 418 } else {
419 buffer = kmalloc(buffer_size, GFP_KERNEL); 419 buffer = kmalloc(buffer_size, GFP_KERNEL);
420 if (!buffer) 420 if (!buffer)
421 return -ENOMEM; 421 return -ENOMEM;
422 } 422 }
423 stream->runtime->fragment_size = params->buffer.fragment_size; 423 stream->runtime->fragment_size = params->buffer.fragment_size;
424 stream->runtime->fragments = params->buffer.fragments; 424 stream->runtime->fragments = params->buffer.fragments;
425 stream->runtime->buffer = buffer; 425 stream->runtime->buffer = buffer;
426 stream->runtime->buffer_size = buffer_size; 426 stream->runtime->buffer_size = buffer_size;
427 return 0; 427 return 0;
428 } 428 }
429 429
430 static int 430 static int
431 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) 431 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
432 { 432 {
433 struct snd_compr_params *params; 433 struct snd_compr_params *params;
434 int retval; 434 int retval;
435 435
436 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 436 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
437 /* 437 /*
438 * we should allow parameter change only when stream has been 438 * we should allow parameter change only when stream has been
439 * opened not in other cases 439 * opened not in other cases
440 */ 440 */
441 params = kmalloc(sizeof(*params), GFP_KERNEL); 441 params = kmalloc(sizeof(*params), GFP_KERNEL);
442 if (!params) 442 if (!params)
443 return -ENOMEM; 443 return -ENOMEM;
444 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) 444 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
445 return -EFAULT; 445 retval = -EFAULT;
446 goto out;
447 }
446 retval = snd_compr_allocate_buffer(stream, params); 448 retval = snd_compr_allocate_buffer(stream, params);
447 if (retval) { 449 if (retval) {
448 kfree(params); 450 retval = -ENOMEM;
449 return -ENOMEM; 451 goto out;
450 } 452 }
451 retval = stream->ops->set_params(stream, params); 453 retval = stream->ops->set_params(stream, params);
452 if (retval) 454 if (retval)
453 goto out; 455 goto out;
454 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 456 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
455 } else 457 } else {
456 return -EPERM; 458 return -EPERM;
459 }
457 out: 460 out:
458 kfree(params); 461 kfree(params);
459 return retval; 462 return retval;
460 } 463 }
461 464
462 static int 465 static int
463 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) 466 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
464 { 467 {
465 struct snd_codec *params; 468 struct snd_codec *params;
466 int retval; 469 int retval;
467 470
468 if (!stream->ops->get_params) 471 if (!stream->ops->get_params)
469 return -EBADFD; 472 return -EBADFD;
470 473
471 params = kmalloc(sizeof(*params), GFP_KERNEL); 474 params = kmalloc(sizeof(*params), GFP_KERNEL);
472 if (!params) 475 if (!params)
473 return -ENOMEM; 476 return -ENOMEM;
474 retval = stream->ops->get_params(stream, params); 477 retval = stream->ops->get_params(stream, params);
475 if (retval) 478 if (retval)
476 goto out; 479 goto out;
477 if (copy_to_user((char __user *)arg, params, sizeof(*params))) 480 if (copy_to_user((char __user *)arg, params, sizeof(*params)))
478 retval = -EFAULT; 481 retval = -EFAULT;
479 482
480 out: 483 out:
481 kfree(params); 484 kfree(params);
482 return retval; 485 return retval;
483 } 486 }
484 487
485 static inline int 488 static inline int
486 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) 489 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
487 { 490 {
488 struct snd_compr_tstamp tstamp; 491 struct snd_compr_tstamp tstamp;
489 492
490 snd_compr_update_tstamp(stream, &tstamp); 493 snd_compr_update_tstamp(stream, &tstamp);
491 return copy_to_user((struct snd_compr_tstamp __user *)arg, 494 return copy_to_user((struct snd_compr_tstamp __user *)arg,
492 &tstamp, sizeof(tstamp)) ? -EFAULT : 0; 495 &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
493 } 496 }
494 497
495 static int snd_compr_pause(struct snd_compr_stream *stream) 498 static int snd_compr_pause(struct snd_compr_stream *stream)
496 { 499 {
497 int retval; 500 int retval;
498 501
499 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) 502 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
500 return -EPERM; 503 return -EPERM;
501 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); 504 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
502 if (!retval) { 505 if (!retval) {
503 stream->runtime->state = SNDRV_PCM_STATE_PAUSED; 506 stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
504 wake_up(&stream->runtime->sleep); 507 wake_up(&stream->runtime->sleep);
505 } 508 }
506 return retval; 509 return retval;
507 } 510 }
508 511
509 static int snd_compr_resume(struct snd_compr_stream *stream) 512 static int snd_compr_resume(struct snd_compr_stream *stream)
510 { 513 {
511 int retval; 514 int retval;
512 515
513 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED) 516 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
514 return -EPERM; 517 return -EPERM;
515 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 518 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
516 if (!retval) 519 if (!retval)
517 stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 520 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
518 return retval; 521 return retval;
519 } 522 }
520 523
521 static int snd_compr_start(struct snd_compr_stream *stream) 524 static int snd_compr_start(struct snd_compr_stream *stream)
522 { 525 {
523 int retval; 526 int retval;
524 527
525 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED) 528 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
526 return -EPERM; 529 return -EPERM;
527 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START); 530 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
528 if (!retval) 531 if (!retval)
529 stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 532 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
530 return retval; 533 return retval;
531 } 534 }
532 535
533 static int snd_compr_stop(struct snd_compr_stream *stream) 536 static int snd_compr_stop(struct snd_compr_stream *stream)
534 { 537 {
535 int retval; 538 int retval;
536 539
537 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 540 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
538 stream->runtime->state == SNDRV_PCM_STATE_SETUP) 541 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
539 return -EPERM; 542 return -EPERM;
540 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); 543 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
541 if (!retval) { 544 if (!retval) {
542 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 545 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
543 wake_up(&stream->runtime->sleep); 546 wake_up(&stream->runtime->sleep);
544 } 547 }
545 return retval; 548 return retval;
546 } 549 }
547 550
548 static int snd_compr_drain(struct snd_compr_stream *stream) 551 static int snd_compr_drain(struct snd_compr_stream *stream)
549 { 552 {
550 int retval; 553 int retval;
551 554
552 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 555 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
553 stream->runtime->state == SNDRV_PCM_STATE_SETUP) 556 stream->runtime->state == SNDRV_PCM_STATE_SETUP)
554 return -EPERM; 557 return -EPERM;
555 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); 558 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
556 if (!retval) { 559 if (!retval) {
557 stream->runtime->state = SNDRV_PCM_STATE_DRAINING; 560 stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
558 wake_up(&stream->runtime->sleep); 561 wake_up(&stream->runtime->sleep);
559 } 562 }
560 return retval; 563 return retval;
561 } 564 }
562 565
563 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 566 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
564 { 567 {
565 struct snd_compr_file *data = f->private_data; 568 struct snd_compr_file *data = f->private_data;
566 struct snd_compr_stream *stream; 569 struct snd_compr_stream *stream;
567 int retval = -ENOTTY; 570 int retval = -ENOTTY;
568 571
569 if (snd_BUG_ON(!data)) 572 if (snd_BUG_ON(!data))
570 return -EFAULT; 573 return -EFAULT;
571 stream = &data->stream; 574 stream = &data->stream;
572 if (snd_BUG_ON(!stream)) 575 if (snd_BUG_ON(!stream))
573 return -EFAULT; 576 return -EFAULT;
574 mutex_lock(&stream->device->lock); 577 mutex_lock(&stream->device->lock);
575 switch (_IOC_NR(cmd)) { 578 switch (_IOC_NR(cmd)) {
576 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): 579 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
577 put_user(SNDRV_COMPRESS_VERSION, 580 put_user(SNDRV_COMPRESS_VERSION,
578 (int __user *)arg) ? -EFAULT : 0; 581 (int __user *)arg) ? -EFAULT : 0;
579 break; 582 break;
580 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): 583 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
581 retval = snd_compr_get_caps(stream, arg); 584 retval = snd_compr_get_caps(stream, arg);
582 break; 585 break;
583 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): 586 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
584 retval = snd_compr_get_codec_caps(stream, arg); 587 retval = snd_compr_get_codec_caps(stream, arg);
585 break; 588 break;
586 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): 589 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
587 retval = snd_compr_set_params(stream, arg); 590 retval = snd_compr_set_params(stream, arg);
588 break; 591 break;
589 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): 592 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
590 retval = snd_compr_get_params(stream, arg); 593 retval = snd_compr_get_params(stream, arg);
591 break; 594 break;
592 case _IOC_NR(SNDRV_COMPRESS_TSTAMP): 595 case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
593 retval = snd_compr_tstamp(stream, arg); 596 retval = snd_compr_tstamp(stream, arg);
594 break; 597 break;
595 case _IOC_NR(SNDRV_COMPRESS_AVAIL): 598 case _IOC_NR(SNDRV_COMPRESS_AVAIL):
596 retval = snd_compr_ioctl_avail(stream, arg); 599 retval = snd_compr_ioctl_avail(stream, arg);
597 break; 600 break;
598 case _IOC_NR(SNDRV_COMPRESS_PAUSE): 601 case _IOC_NR(SNDRV_COMPRESS_PAUSE):
599 retval = snd_compr_pause(stream); 602 retval = snd_compr_pause(stream);
600 break; 603 break;
601 case _IOC_NR(SNDRV_COMPRESS_RESUME): 604 case _IOC_NR(SNDRV_COMPRESS_RESUME):
602 retval = snd_compr_resume(stream); 605 retval = snd_compr_resume(stream);
603 break; 606 break;
604 case _IOC_NR(SNDRV_COMPRESS_START): 607 case _IOC_NR(SNDRV_COMPRESS_START):
605 retval = snd_compr_start(stream); 608 retval = snd_compr_start(stream);
606 break; 609 break;
607 case _IOC_NR(SNDRV_COMPRESS_STOP): 610 case _IOC_NR(SNDRV_COMPRESS_STOP):
608 retval = snd_compr_stop(stream); 611 retval = snd_compr_stop(stream);
609 break; 612 break;
610 case _IOC_NR(SNDRV_COMPRESS_DRAIN): 613 case _IOC_NR(SNDRV_COMPRESS_DRAIN):
611 retval = snd_compr_drain(stream); 614 retval = snd_compr_drain(stream);
612 break; 615 break;
613 } 616 }
614 mutex_unlock(&stream->device->lock); 617 mutex_unlock(&stream->device->lock);
615 return retval; 618 return retval;
616 } 619 }
617 620
618 static const struct file_operations snd_compr_file_ops = { 621 static const struct file_operations snd_compr_file_ops = {
619 .owner = THIS_MODULE, 622 .owner = THIS_MODULE,
620 .open = snd_compr_open, 623 .open = snd_compr_open,
621 .release = snd_compr_free, 624 .release = snd_compr_free,
622 .write = snd_compr_write, 625 .write = snd_compr_write,
623 .read = snd_compr_read, 626 .read = snd_compr_read,
624 .unlocked_ioctl = snd_compr_ioctl, 627 .unlocked_ioctl = snd_compr_ioctl,
625 .mmap = snd_compr_mmap, 628 .mmap = snd_compr_mmap,
626 .poll = snd_compr_poll, 629 .poll = snd_compr_poll,
627 }; 630 };
628 631
629 static int snd_compress_dev_register(struct snd_device *device) 632 static int snd_compress_dev_register(struct snd_device *device)
630 { 633 {
631 int ret = -EINVAL; 634 int ret = -EINVAL;
632 char str[16]; 635 char str[16];
633 struct snd_compr *compr; 636 struct snd_compr *compr;
634 637
635 if (snd_BUG_ON(!device || !device->device_data)) 638 if (snd_BUG_ON(!device || !device->device_data))
636 return -EBADFD; 639 return -EBADFD;
637 compr = device->device_data; 640 compr = device->device_data;
638 641
639 sprintf(str, "comprC%iD%i", compr->card->number, compr->device); 642 sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
640 pr_debug("reg %s for device %s, direction %d\n", str, compr->name, 643 pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
641 compr->direction); 644 compr->direction);
642 /* register compressed device */ 645 /* register compressed device */
643 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, 646 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
644 compr->device, &snd_compr_file_ops, compr, str); 647 compr->device, &snd_compr_file_ops, compr, str);
645 if (ret < 0) { 648 if (ret < 0) {
646 pr_err("snd_register_device failed\n %d", ret); 649 pr_err("snd_register_device failed\n %d", ret);
647 return ret; 650 return ret;
648 } 651 }
649 return ret; 652 return ret;
650 653
651 } 654 }
652 655
653 static int snd_compress_dev_disconnect(struct snd_device *device) 656 static int snd_compress_dev_disconnect(struct snd_device *device)
654 { 657 {
655 struct snd_compr *compr; 658 struct snd_compr *compr;
656 659
657 compr = device->device_data; 660 compr = device->device_data;
658 snd_unregister_device(compr->direction, compr->card, compr->device); 661 snd_unregister_device(compr->direction, compr->card, compr->device);
659 return 0; 662 return 0;
660 } 663 }
661 664
662 /* 665 /*
663 * snd_compress_new: create new compress device 666 * snd_compress_new: create new compress device
664 * @card: sound card pointer 667 * @card: sound card pointer
665 * @device: device number 668 * @device: device number
666 * @dirn: device direction, should be of type enum snd_compr_direction 669 * @dirn: device direction, should be of type enum snd_compr_direction
667 * @compr: compress device pointer 670 * @compr: compress device pointer
668 */ 671 */
669 int snd_compress_new(struct snd_card *card, int device, 672 int snd_compress_new(struct snd_card *card, int device,
670 int dirn, struct snd_compr *compr) 673 int dirn, struct snd_compr *compr)
671 { 674 {
672 static struct snd_device_ops ops = { 675 static struct snd_device_ops ops = {
673 .dev_free = NULL, 676 .dev_free = NULL,
674 .dev_register = snd_compress_dev_register, 677 .dev_register = snd_compress_dev_register,
675 .dev_disconnect = snd_compress_dev_disconnect, 678 .dev_disconnect = snd_compress_dev_disconnect,
676 }; 679 };
677 680
678 compr->card = card; 681 compr->card = card;
679 compr->device = device; 682 compr->device = device;
680 compr->direction = dirn; 683 compr->direction = dirn;
681 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); 684 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
682 } 685 }
683 EXPORT_SYMBOL_GPL(snd_compress_new); 686 EXPORT_SYMBOL_GPL(snd_compress_new);
684 687
685 static int snd_compress_add_device(struct snd_compr *device) 688 static int snd_compress_add_device(struct snd_compr *device)
686 { 689 {
687 int ret; 690 int ret;
688 691
689 if (!device->card) 692 if (!device->card)
690 return -EINVAL; 693 return -EINVAL;
691 694
692 /* register the card */ 695 /* register the card */
693 ret = snd_card_register(device->card); 696 ret = snd_card_register(device->card);
694 if (ret) 697 if (ret)
695 goto out; 698 goto out;
696 return 0; 699 return 0;
697 700
698 out: 701 out:
699 pr_err("failed with %d\n", ret); 702 pr_err("failed with %d\n", ret);
700 return ret; 703 return ret;
701 704
702 } 705 }
703 706
704 static int snd_compress_remove_device(struct snd_compr *device) 707 static int snd_compress_remove_device(struct snd_compr *device)
705 { 708 {
706 return snd_card_free(device->card); 709 return snd_card_free(device->card);
707 } 710 }
708 711
709 /** 712 /**
710 * snd_compress_register - register compressed device 713 * snd_compress_register - register compressed device
711 * 714 *
712 * @device: compressed device to register 715 * @device: compressed device to register
713 */ 716 */
714 int snd_compress_register(struct snd_compr *device) 717 int snd_compress_register(struct snd_compr *device)
715 { 718 {
716 int retval; 719 int retval;
717 720
718 if (device->name == NULL || device->dev == NULL || device->ops == NULL) 721 if (device->name == NULL || device->dev == NULL || device->ops == NULL)
719 return -EINVAL; 722 return -EINVAL;
720 723
721 pr_debug("Registering compressed device %s\n", device->name); 724 pr_debug("Registering compressed device %s\n", device->name);
722 if (snd_BUG_ON(!device->ops->open)) 725 if (snd_BUG_ON(!device->ops->open))
723 return -EINVAL; 726 return -EINVAL;
724 if (snd_BUG_ON(!device->ops->free)) 727 if (snd_BUG_ON(!device->ops->free))
725 return -EINVAL; 728 return -EINVAL;
726 if (snd_BUG_ON(!device->ops->set_params)) 729 if (snd_BUG_ON(!device->ops->set_params))
727 return -EINVAL; 730 return -EINVAL;
728 if (snd_BUG_ON(!device->ops->trigger)) 731 if (snd_BUG_ON(!device->ops->trigger))
729 return -EINVAL; 732 return -EINVAL;
730 733
731 mutex_init(&device->lock); 734 mutex_init(&device->lock);
732 735
733 /* register a compressed card */ 736 /* register a compressed card */
734 mutex_lock(&device_mutex); 737 mutex_lock(&device_mutex);
735 retval = snd_compress_add_device(device); 738 retval = snd_compress_add_device(device);
736 mutex_unlock(&device_mutex); 739 mutex_unlock(&device_mutex);
737 return retval; 740 return retval;
738 } 741 }
739 EXPORT_SYMBOL_GPL(snd_compress_register); 742 EXPORT_SYMBOL_GPL(snd_compress_register);
740 743
741 int snd_compress_deregister(struct snd_compr *device) 744 int snd_compress_deregister(struct snd_compr *device)
742 { 745 {
743 pr_debug("Removing compressed device %s\n", device->name); 746 pr_debug("Removing compressed device %s\n", device->name);
744 mutex_lock(&device_mutex); 747 mutex_lock(&device_mutex);
745 snd_compress_remove_device(device); 748 snd_compress_remove_device(device);
746 mutex_unlock(&device_mutex); 749 mutex_unlock(&device_mutex);
747 return 0; 750 return 0;
748 } 751 }
749 EXPORT_SYMBOL_GPL(snd_compress_deregister); 752 EXPORT_SYMBOL_GPL(snd_compress_deregister);
750 753
751 static int __init snd_compress_init(void) 754 static int __init snd_compress_init(void)
752 { 755 {
753 return 0; 756 return 0;
754 } 757 }
755 758
756 static void __exit snd_compress_exit(void) 759 static void __exit snd_compress_exit(void)
757 { 760 {
758 } 761 }
759 762
760 module_init(snd_compress_init); 763 module_init(snd_compress_init);
761 module_exit(snd_compress_exit); 764 module_exit(snd_compress_exit);
762 765
763 MODULE_DESCRIPTION("ALSA Compressed offload framework"); 766 MODULE_DESCRIPTION("ALSA Compressed offload framework");
764 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); 767 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
765 MODULE_LICENSE("GPL v2"); 768 MODULE_LICENSE("GPL v2");
766 769