Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * compress_core.c - compress offload core |
| 3 | * |
| 4 | * Copyright (C) 2011 Intel Corporation |
| 5 | * Authors: Vinod Koul <vinod.koul@linux.intel.com> |
| 6 | * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 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 |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 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., |
| 20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 | * |
| 22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | * |
| 24 | */ |
| 25 | #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ |
| 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) |
| 27 | |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/list.h> |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 31 | #include <linux/math64.h> |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 32 | #include <linux/mm.h> |
| 33 | #include <linux/mutex.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/sched.h> |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 37 | #include <linux/types.h> |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 38 | #include <linux/uio.h> |
| 39 | #include <linux/uaccess.h> |
| 40 | #include <linux/module.h> |
| 41 | #include <sound/core.h> |
| 42 | #include <sound/initval.h> |
| 43 | #include <sound/compress_params.h> |
| 44 | #include <sound/compress_offload.h> |
| 45 | #include <sound/compress_driver.h> |
| 46 | |
| 47 | /* TODO: |
| 48 | * - add substream support for multiple devices in case of |
| 49 | * SND_DYNAMIC_MINORS is not used |
| 50 | * - Multiple node representation |
| 51 | * driver should be able to register multiple nodes |
| 52 | */ |
| 53 | |
| 54 | static DEFINE_MUTEX(device_mutex); |
| 55 | |
| 56 | struct snd_compr_file { |
| 57 | unsigned long caps; |
| 58 | struct snd_compr_stream stream; |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * a note on stream states used: |
| 63 | * we use follwing states in the compressed core |
| 64 | * SNDRV_PCM_STATE_OPEN: When stream has been opened. |
| 65 | * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by |
| 66 | * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this |
| 67 | * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain. |
| 68 | * SNDRV_PCM_STATE_RUNNING: When stream has been started and is |
| 69 | * decoding/encoding and rendering/capturing data. |
| 70 | * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done |
| 71 | * by calling SNDRV_COMPRESS_DRAIN. |
| 72 | * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling |
| 73 | * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling |
| 74 | * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. |
| 75 | */ |
| 76 | static int snd_compr_open(struct inode *inode, struct file *f) |
| 77 | { |
| 78 | struct snd_compr *compr; |
| 79 | struct snd_compr_file *data; |
| 80 | struct snd_compr_runtime *runtime; |
| 81 | enum snd_compr_direction dirn; |
| 82 | int maj = imajor(inode); |
| 83 | int ret; |
| 84 | |
Dan Carpenter | 81cb324 | 2012-09-11 14:12:43 +0300 | [diff] [blame] | 85 | if ((f->f_flags & O_ACCMODE) == O_WRONLY) |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 86 | dirn = SND_COMPRESS_PLAYBACK; |
Dan Carpenter | 81cb324 | 2012-09-11 14:12:43 +0300 | [diff] [blame] | 87 | else if ((f->f_flags & O_ACCMODE) == O_RDONLY) |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 88 | dirn = SND_COMPRESS_CAPTURE; |
Dan Carpenter | 81cb324 | 2012-09-11 14:12:43 +0300 | [diff] [blame] | 89 | else |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 90 | return -EINVAL; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 91 | |
| 92 | if (maj == snd_major) |
| 93 | compr = snd_lookup_minor_data(iminor(inode), |
| 94 | SNDRV_DEVICE_TYPE_COMPRESS); |
| 95 | else |
| 96 | return -EBADFD; |
| 97 | |
| 98 | if (compr == NULL) { |
| 99 | pr_err("no device data!!!\n"); |
| 100 | return -ENODEV; |
| 101 | } |
| 102 | |
| 103 | if (dirn != compr->direction) { |
| 104 | pr_err("this device doesn't support this direction\n"); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 105 | snd_card_unref(compr->card); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 110 | if (!data) { |
| 111 | snd_card_unref(compr->card); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 112 | return -ENOMEM; |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 113 | } |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 114 | data->stream.ops = compr->ops; |
| 115 | data->stream.direction = dirn; |
| 116 | data->stream.private_data = compr->private_data; |
| 117 | data->stream.device = compr; |
| 118 | runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); |
| 119 | if (!runtime) { |
| 120 | kfree(data); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 121 | snd_card_unref(compr->card); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 122 | return -ENOMEM; |
| 123 | } |
| 124 | runtime->state = SNDRV_PCM_STATE_OPEN; |
| 125 | init_waitqueue_head(&runtime->sleep); |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 126 | init_waitqueue_head(&runtime->wait); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 127 | data->stream.runtime = runtime; |
| 128 | f->private_data = (void *)data; |
| 129 | mutex_lock(&compr->lock); |
| 130 | ret = compr->ops->open(&data->stream); |
| 131 | mutex_unlock(&compr->lock); |
| 132 | if (ret) { |
| 133 | kfree(runtime); |
| 134 | kfree(data); |
| 135 | } |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 136 | snd_card_unref(compr->card); |
| 137 | return 0; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int snd_compr_free(struct inode *inode, struct file *f) |
| 141 | { |
| 142 | struct snd_compr_file *data = f->private_data; |
| 143 | data->stream.ops->free(&data->stream); |
| 144 | kfree(data->stream.runtime->buffer); |
| 145 | kfree(data->stream.runtime); |
| 146 | kfree(data); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 150 | static int snd_compr_update_tstamp(struct snd_compr_stream *stream, |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 151 | struct snd_compr_tstamp *tstamp) |
| 152 | { |
| 153 | if (!stream->ops->pointer) |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 154 | return -ENOTSUPP; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 155 | stream->ops->pointer(stream, tstamp); |
| 156 | pr_debug("dsp consumed till %d total %d bytes\n", |
| 157 | tstamp->byte_offset, tstamp->copied_total); |
Charles Keepax | 5b1f79f | 2013-04-18 11:01:03 +0100 | [diff] [blame] | 158 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
| 159 | stream->runtime->total_bytes_transferred = tstamp->copied_total; |
| 160 | else |
| 161 | stream->runtime->total_bytes_available = tstamp->copied_total; |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 162 | return 0; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, |
| 166 | struct snd_compr_avail *avail) |
| 167 | { |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 168 | memset(avail, 0, sizeof(*avail)); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 169 | snd_compr_update_tstamp(stream, &avail->tstamp); |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 170 | /* Still need to return avail even if tstamp can't be filled in */ |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 171 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 172 | if (stream->runtime->total_bytes_available == 0 && |
Charles Keepax | 5b1f79f | 2013-04-18 11:01:03 +0100 | [diff] [blame] | 173 | stream->runtime->state == SNDRV_PCM_STATE_SETUP && |
| 174 | stream->direction == SND_COMPRESS_PLAYBACK) { |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 175 | pr_debug("detected init and someone forgot to do a write\n"); |
| 176 | return stream->runtime->buffer_size; |
| 177 | } |
| 178 | pr_debug("app wrote %lld, DSP consumed %lld\n", |
| 179 | stream->runtime->total_bytes_available, |
| 180 | stream->runtime->total_bytes_transferred); |
| 181 | if (stream->runtime->total_bytes_available == |
| 182 | stream->runtime->total_bytes_transferred) { |
Charles Keepax | 5b1f79f | 2013-04-18 11:01:03 +0100 | [diff] [blame] | 183 | if (stream->direction == SND_COMPRESS_PLAYBACK) { |
| 184 | pr_debug("both pointers are same, returning full avail\n"); |
| 185 | return stream->runtime->buffer_size; |
| 186 | } else { |
| 187 | pr_debug("both pointers are same, returning no avail\n"); |
| 188 | return 0; |
| 189 | } |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 190 | } |
| 191 | |
Charles Keepax | 5b1f79f | 2013-04-18 11:01:03 +0100 | [diff] [blame] | 192 | avail->avail = stream->runtime->total_bytes_available - |
| 193 | stream->runtime->total_bytes_transferred; |
| 194 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
| 195 | avail->avail = stream->runtime->buffer_size - avail->avail; |
| 196 | |
Charles Keepax | 4c28e32 | 2013-04-18 10:59:23 +0100 | [diff] [blame] | 197 | pr_debug("ret avail as %lld\n", avail->avail); |
| 198 | return avail->avail; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream) |
| 202 | { |
| 203 | struct snd_compr_avail avail; |
| 204 | |
| 205 | return snd_compr_calc_avail(stream, &avail); |
| 206 | } |
| 207 | |
| 208 | static int |
| 209 | snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg) |
| 210 | { |
| 211 | struct snd_compr_avail ioctl_avail; |
| 212 | size_t avail; |
| 213 | |
| 214 | avail = snd_compr_calc_avail(stream, &ioctl_avail); |
| 215 | ioctl_avail.avail = avail; |
| 216 | |
| 217 | if (copy_to_user((__u64 __user *)arg, |
| 218 | &ioctl_avail, sizeof(ioctl_avail))) |
| 219 | return -EFAULT; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int snd_compr_write_data(struct snd_compr_stream *stream, |
| 224 | const char __user *buf, size_t count) |
| 225 | { |
| 226 | void *dstn; |
| 227 | size_t copy; |
| 228 | struct snd_compr_runtime *runtime = stream->runtime; |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 229 | /* 64-bit Modulus */ |
| 230 | u64 app_pointer = div64_u64(runtime->total_bytes_available, |
| 231 | runtime->buffer_size); |
| 232 | app_pointer = runtime->total_bytes_available - |
| 233 | (app_pointer * runtime->buffer_size); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 234 | |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 235 | dstn = runtime->buffer + app_pointer; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 236 | pr_debug("copying %ld at %lld\n", |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 237 | (unsigned long)count, app_pointer); |
| 238 | if (count < runtime->buffer_size - app_pointer) { |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 239 | if (copy_from_user(dstn, buf, count)) |
| 240 | return -EFAULT; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 241 | } else { |
Charles Keepax | f0283b5 | 2013-04-18 11:03:46 +0100 | [diff] [blame] | 242 | copy = runtime->buffer_size - app_pointer; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 243 | if (copy_from_user(dstn, buf, copy)) |
| 244 | return -EFAULT; |
| 245 | if (copy_from_user(runtime->buffer, buf + copy, count - copy)) |
| 246 | return -EFAULT; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 247 | } |
| 248 | /* if DSP cares, let it know data has been written */ |
| 249 | if (stream->ops->ack) |
| 250 | stream->ops->ack(stream, count); |
| 251 | return count; |
| 252 | } |
| 253 | |
| 254 | static ssize_t snd_compr_write(struct file *f, const char __user *buf, |
| 255 | size_t count, loff_t *offset) |
| 256 | { |
| 257 | struct snd_compr_file *data = f->private_data; |
| 258 | struct snd_compr_stream *stream; |
| 259 | size_t avail; |
| 260 | int retval; |
| 261 | |
| 262 | if (snd_BUG_ON(!data)) |
| 263 | return -EFAULT; |
| 264 | |
| 265 | stream = &data->stream; |
| 266 | mutex_lock(&stream->device->lock); |
| 267 | /* write is allowed when stream is running or has been steup */ |
| 268 | if (stream->runtime->state != SNDRV_PCM_STATE_SETUP && |
| 269 | stream->runtime->state != SNDRV_PCM_STATE_RUNNING) { |
| 270 | mutex_unlock(&stream->device->lock); |
| 271 | return -EBADFD; |
| 272 | } |
| 273 | |
| 274 | avail = snd_compr_get_avail(stream); |
| 275 | pr_debug("avail returned %ld\n", (unsigned long)avail); |
| 276 | /* calculate how much we can write to buffer */ |
| 277 | if (avail > count) |
| 278 | avail = count; |
| 279 | |
Charles Keepax | 4daf891 | 2013-04-18 11:01:38 +0100 | [diff] [blame] | 280 | if (stream->ops->copy) { |
| 281 | char __user* cbuf = (char __user*)buf; |
| 282 | retval = stream->ops->copy(stream, cbuf, avail); |
| 283 | } else { |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 284 | retval = snd_compr_write_data(stream, buf, avail); |
Charles Keepax | 4daf891 | 2013-04-18 11:01:38 +0100 | [diff] [blame] | 285 | } |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 286 | if (retval > 0) |
| 287 | stream->runtime->total_bytes_available += retval; |
| 288 | |
| 289 | /* while initiating the stream, write should be called before START |
| 290 | * call, so in setup move state */ |
| 291 | if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { |
| 292 | stream->runtime->state = SNDRV_PCM_STATE_PREPARED; |
| 293 | pr_debug("stream prepared, Houston we are good to go\n"); |
| 294 | } |
| 295 | |
| 296 | mutex_unlock(&stream->device->lock); |
| 297 | return retval; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static ssize_t snd_compr_read(struct file *f, char __user *buf, |
| 302 | size_t count, loff_t *offset) |
| 303 | { |
Charles Keepax | 49bb640 | 2013-04-18 11:02:08 +0100 | [diff] [blame] | 304 | struct snd_compr_file *data = f->private_data; |
| 305 | struct snd_compr_stream *stream; |
| 306 | size_t avail; |
| 307 | int retval; |
| 308 | |
| 309 | if (snd_BUG_ON(!data)) |
| 310 | return -EFAULT; |
| 311 | |
| 312 | stream = &data->stream; |
| 313 | mutex_lock(&stream->device->lock); |
| 314 | |
Vinod Koul | 7548134 | 2013-04-29 14:25:23 +0530 | [diff] [blame] | 315 | /* read is allowed when stream is running, paused, draining and setup |
| 316 | * (yes setup is state which we transition to after stop, so if user |
| 317 | * wants to read data after stop we allow that) |
| 318 | */ |
| 319 | switch (stream->runtime->state) { |
| 320 | case SNDRV_PCM_STATE_OPEN: |
| 321 | case SNDRV_PCM_STATE_PREPARED: |
| 322 | case SNDRV_PCM_STATE_XRUN: |
| 323 | case SNDRV_PCM_STATE_SUSPENDED: |
| 324 | case SNDRV_PCM_STATE_DISCONNECTED: |
Charles Keepax | 49bb640 | 2013-04-18 11:02:08 +0100 | [diff] [blame] | 325 | retval = -EBADFD; |
| 326 | goto out; |
| 327 | } |
| 328 | |
| 329 | avail = snd_compr_get_avail(stream); |
| 330 | pr_debug("avail returned %ld\n", (unsigned long)avail); |
| 331 | /* calculate how much we can read from buffer */ |
| 332 | if (avail > count) |
| 333 | avail = count; |
| 334 | |
| 335 | if (stream->ops->copy) { |
| 336 | retval = stream->ops->copy(stream, buf, avail); |
| 337 | } else { |
| 338 | retval = -ENXIO; |
| 339 | goto out; |
| 340 | } |
| 341 | if (retval > 0) |
| 342 | stream->runtime->total_bytes_transferred += retval; |
| 343 | |
| 344 | out: |
| 345 | mutex_unlock(&stream->device->lock); |
| 346 | return retval; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) |
| 350 | { |
| 351 | return -ENXIO; |
| 352 | } |
| 353 | |
| 354 | static inline int snd_compr_get_poll(struct snd_compr_stream *stream) |
| 355 | { |
| 356 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
| 357 | return POLLOUT | POLLWRNORM; |
| 358 | else |
| 359 | return POLLIN | POLLRDNORM; |
| 360 | } |
| 361 | |
| 362 | static unsigned int snd_compr_poll(struct file *f, poll_table *wait) |
| 363 | { |
| 364 | struct snd_compr_file *data = f->private_data; |
| 365 | struct snd_compr_stream *stream; |
| 366 | size_t avail; |
| 367 | int retval = 0; |
| 368 | |
| 369 | if (snd_BUG_ON(!data)) |
| 370 | return -EFAULT; |
| 371 | stream = &data->stream; |
| 372 | if (snd_BUG_ON(!stream)) |
| 373 | return -EFAULT; |
| 374 | |
| 375 | mutex_lock(&stream->device->lock); |
| 376 | if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED || |
| 377 | stream->runtime->state == SNDRV_PCM_STATE_OPEN) { |
| 378 | retval = -EBADFD; |
| 379 | goto out; |
| 380 | } |
| 381 | poll_wait(f, &stream->runtime->sleep, wait); |
| 382 | |
| 383 | avail = snd_compr_get_avail(stream); |
| 384 | pr_debug("avail is %ld\n", (unsigned long)avail); |
| 385 | /* check if we have at least one fragment to fill */ |
| 386 | switch (stream->runtime->state) { |
| 387 | case SNDRV_PCM_STATE_DRAINING: |
| 388 | /* stream has been woken up after drain is complete |
| 389 | * draining done so set stream state to stopped |
| 390 | */ |
| 391 | retval = snd_compr_get_poll(stream); |
| 392 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; |
| 393 | break; |
| 394 | case SNDRV_PCM_STATE_RUNNING: |
| 395 | case SNDRV_PCM_STATE_PREPARED: |
| 396 | case SNDRV_PCM_STATE_PAUSED: |
| 397 | if (avail >= stream->runtime->fragment_size) |
| 398 | retval = snd_compr_get_poll(stream); |
| 399 | break; |
| 400 | default: |
| 401 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
| 402 | retval = POLLOUT | POLLWRNORM | POLLERR; |
| 403 | else |
| 404 | retval = POLLIN | POLLRDNORM | POLLERR; |
| 405 | break; |
| 406 | } |
| 407 | out: |
| 408 | mutex_unlock(&stream->device->lock); |
| 409 | return retval; |
| 410 | } |
| 411 | |
| 412 | static int |
| 413 | snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg) |
| 414 | { |
| 415 | int retval; |
| 416 | struct snd_compr_caps caps; |
| 417 | |
| 418 | if (!stream->ops->get_caps) |
| 419 | return -ENXIO; |
| 420 | |
Dan Carpenter | 1c62e9f | 2013-04-21 14:07:29 +0300 | [diff] [blame] | 421 | memset(&caps, 0, sizeof(caps)); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 422 | retval = stream->ops->get_caps(stream, &caps); |
| 423 | if (retval) |
| 424 | goto out; |
| 425 | if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) |
| 426 | retval = -EFAULT; |
| 427 | out: |
| 428 | return retval; |
| 429 | } |
| 430 | |
| 431 | static int |
| 432 | snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) |
| 433 | { |
| 434 | int retval; |
| 435 | struct snd_compr_codec_caps *caps; |
| 436 | |
| 437 | if (!stream->ops->get_codec_caps) |
| 438 | return -ENXIO; |
| 439 | |
Takashi Iwai | 47966e9 | 2013-04-22 10:38:26 +0200 | [diff] [blame] | 440 | caps = kzalloc(sizeof(*caps), GFP_KERNEL); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 441 | if (!caps) |
| 442 | return -ENOMEM; |
| 443 | |
| 444 | retval = stream->ops->get_codec_caps(stream, caps); |
| 445 | if (retval) |
| 446 | goto out; |
| 447 | if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) |
| 448 | retval = -EFAULT; |
| 449 | |
| 450 | out: |
| 451 | kfree(caps); |
| 452 | return retval; |
| 453 | } |
| 454 | |
| 455 | /* revisit this with snd_pcm_preallocate_xxx */ |
| 456 | static int snd_compr_allocate_buffer(struct snd_compr_stream *stream, |
| 457 | struct snd_compr_params *params) |
| 458 | { |
| 459 | unsigned int buffer_size; |
| 460 | void *buffer; |
| 461 | |
| 462 | buffer_size = params->buffer.fragment_size * params->buffer.fragments; |
| 463 | if (stream->ops->copy) { |
| 464 | buffer = NULL; |
| 465 | /* if copy is defined the driver will be required to copy |
| 466 | * the data from core |
| 467 | */ |
| 468 | } else { |
| 469 | buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 470 | if (!buffer) |
| 471 | return -ENOMEM; |
| 472 | } |
| 473 | stream->runtime->fragment_size = params->buffer.fragment_size; |
| 474 | stream->runtime->fragments = params->buffer.fragments; |
| 475 | stream->runtime->buffer = buffer; |
| 476 | stream->runtime->buffer_size = buffer_size; |
| 477 | return 0; |
| 478 | } |
| 479 | |
Vinod Koul | 4dc040a | 2012-09-17 11:51:25 +0530 | [diff] [blame] | 480 | static int snd_compress_check_input(struct snd_compr_params *params) |
| 481 | { |
| 482 | /* first let's check the buffer parameter's */ |
| 483 | if (params->buffer.fragment_size == 0 || |
| 484 | params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size) |
| 485 | return -EINVAL; |
| 486 | |
Vinod Koul | fb4a977 | 2012-09-17 11:51:26 +0530 | [diff] [blame] | 487 | /* now codec parameters */ |
| 488 | if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | if (params->codec.ch_in == 0 || params->codec.ch_out == 0) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000)) |
| 495 | return -EINVAL; |
| 496 | |
Vinod Koul | 4dc040a | 2012-09-17 11:51:25 +0530 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 500 | static int |
| 501 | snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) |
| 502 | { |
| 503 | struct snd_compr_params *params; |
| 504 | int retval; |
| 505 | |
| 506 | if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { |
| 507 | /* |
| 508 | * we should allow parameter change only when stream has been |
| 509 | * opened not in other cases |
| 510 | */ |
| 511 | params = kmalloc(sizeof(*params), GFP_KERNEL); |
| 512 | if (!params) |
| 513 | return -ENOMEM; |
Jesper Juhl | 769fab2 | 2012-01-23 21:02:57 +0100 | [diff] [blame] | 514 | if (copy_from_user(params, (void __user *)arg, sizeof(*params))) { |
| 515 | retval = -EFAULT; |
| 516 | goto out; |
| 517 | } |
Vinod Koul | 4dc040a | 2012-09-17 11:51:25 +0530 | [diff] [blame] | 518 | |
| 519 | retval = snd_compress_check_input(params); |
| 520 | if (retval) |
| 521 | goto out; |
| 522 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 523 | retval = snd_compr_allocate_buffer(stream, params); |
| 524 | if (retval) { |
Jesper Juhl | 769fab2 | 2012-01-23 21:02:57 +0100 | [diff] [blame] | 525 | retval = -ENOMEM; |
| 526 | goto out; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 527 | } |
Vinod Koul | 4dc040a | 2012-09-17 11:51:25 +0530 | [diff] [blame] | 528 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 529 | retval = stream->ops->set_params(stream, params); |
| 530 | if (retval) |
| 531 | goto out; |
Charles Keepax | 49bb640 | 2013-04-18 11:02:08 +0100 | [diff] [blame] | 532 | |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 533 | stream->metadata_set = false; |
| 534 | stream->next_track = false; |
Charles Keepax | 49bb640 | 2013-04-18 11:02:08 +0100 | [diff] [blame] | 535 | |
| 536 | if (stream->direction == SND_COMPRESS_PLAYBACK) |
| 537 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; |
| 538 | else |
| 539 | stream->runtime->state = SNDRV_PCM_STATE_PREPARED; |
Jesper Juhl | 769fab2 | 2012-01-23 21:02:57 +0100 | [diff] [blame] | 540 | } else { |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 541 | return -EPERM; |
Jesper Juhl | 769fab2 | 2012-01-23 21:02:57 +0100 | [diff] [blame] | 542 | } |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 543 | out: |
| 544 | kfree(params); |
| 545 | return retval; |
| 546 | } |
| 547 | |
| 548 | static int |
| 549 | snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) |
| 550 | { |
| 551 | struct snd_codec *params; |
| 552 | int retval; |
| 553 | |
| 554 | if (!stream->ops->get_params) |
| 555 | return -EBADFD; |
| 556 | |
Takashi Iwai | 47966e9 | 2013-04-22 10:38:26 +0200 | [diff] [blame] | 557 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 558 | if (!params) |
| 559 | return -ENOMEM; |
| 560 | retval = stream->ops->get_params(stream, params); |
| 561 | if (retval) |
| 562 | goto out; |
| 563 | if (copy_to_user((char __user *)arg, params, sizeof(*params))) |
| 564 | retval = -EFAULT; |
| 565 | |
| 566 | out: |
| 567 | kfree(params); |
| 568 | return retval; |
| 569 | } |
| 570 | |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 571 | static int |
| 572 | snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg) |
| 573 | { |
| 574 | struct snd_compr_metadata metadata; |
| 575 | int retval; |
| 576 | |
| 577 | if (!stream->ops->get_metadata) |
| 578 | return -ENXIO; |
| 579 | |
| 580 | if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) |
| 581 | return -EFAULT; |
| 582 | |
| 583 | retval = stream->ops->get_metadata(stream, &metadata); |
| 584 | if (retval != 0) |
| 585 | return retval; |
| 586 | |
| 587 | if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata))) |
| 588 | return -EFAULT; |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int |
| 594 | snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg) |
| 595 | { |
| 596 | struct snd_compr_metadata metadata; |
| 597 | int retval; |
| 598 | |
| 599 | if (!stream->ops->set_metadata) |
| 600 | return -ENXIO; |
| 601 | /* |
| 602 | * we should allow parameter change only when stream has been |
| 603 | * opened not in other cases |
| 604 | */ |
| 605 | if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) |
| 606 | return -EFAULT; |
| 607 | |
| 608 | retval = stream->ops->set_metadata(stream, &metadata); |
| 609 | stream->metadata_set = true; |
| 610 | |
| 611 | return retval; |
| 612 | } |
| 613 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 614 | static inline int |
| 615 | snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) |
| 616 | { |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 617 | struct snd_compr_tstamp tstamp = {0}; |
| 618 | int ret; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 619 | |
Richard Fitzgerald | 17ac8e5 | 2013-02-11 13:44:53 +0000 | [diff] [blame] | 620 | ret = snd_compr_update_tstamp(stream, &tstamp); |
| 621 | if (ret == 0) |
| 622 | ret = copy_to_user((struct snd_compr_tstamp __user *)arg, |
| 623 | &tstamp, sizeof(tstamp)) ? -EFAULT : 0; |
| 624 | return ret; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int snd_compr_pause(struct snd_compr_stream *stream) |
| 628 | { |
| 629 | int retval; |
| 630 | |
| 631 | if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) |
| 632 | return -EPERM; |
| 633 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); |
Vinod Koul | 6b18f79 | 2012-06-12 16:16:17 +0530 | [diff] [blame] | 634 | if (!retval) |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 635 | stream->runtime->state = SNDRV_PCM_STATE_PAUSED; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 636 | return retval; |
| 637 | } |
| 638 | |
| 639 | static int snd_compr_resume(struct snd_compr_stream *stream) |
| 640 | { |
| 641 | int retval; |
| 642 | |
| 643 | if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED) |
| 644 | return -EPERM; |
| 645 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); |
| 646 | if (!retval) |
| 647 | stream->runtime->state = SNDRV_PCM_STATE_RUNNING; |
| 648 | return retval; |
| 649 | } |
| 650 | |
| 651 | static int snd_compr_start(struct snd_compr_stream *stream) |
| 652 | { |
| 653 | int retval; |
| 654 | |
| 655 | if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED) |
| 656 | return -EPERM; |
| 657 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START); |
| 658 | if (!retval) |
| 659 | stream->runtime->state = SNDRV_PCM_STATE_RUNNING; |
| 660 | return retval; |
| 661 | } |
| 662 | |
| 663 | static int snd_compr_stop(struct snd_compr_stream *stream) |
| 664 | { |
| 665 | int retval; |
| 666 | |
| 667 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || |
| 668 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) |
| 669 | return -EPERM; |
| 670 | retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); |
| 671 | if (!retval) { |
| 672 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; |
| 673 | wake_up(&stream->runtime->sleep); |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 674 | snd_compr_drain_notify(stream); |
Vinod Koul | 8b21460 | 2012-06-12 16:16:18 +0530 | [diff] [blame] | 675 | stream->runtime->total_bytes_available = 0; |
| 676 | stream->runtime->total_bytes_transferred = 0; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 677 | } |
| 678 | return retval; |
| 679 | } |
| 680 | |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 681 | static int snd_compress_wait_for_drain(struct snd_compr_stream *stream) |
| 682 | { |
| 683 | /* |
| 684 | * We are called with lock held. So drop the lock while we wait for |
| 685 | * drain complete notfication from the driver |
| 686 | * |
| 687 | * It is expected that driver will notify the drain completion and then |
| 688 | * stream will be moved to SETUP state, even if draining resulted in an |
| 689 | * error. We can trigger next track after this. |
| 690 | */ |
| 691 | stream->runtime->state = SNDRV_PCM_STATE_DRAINING; |
| 692 | mutex_unlock(&stream->device->lock); |
| 693 | |
| 694 | wait_event(stream->runtime->wait, stream->runtime->drain_wake); |
| 695 | |
| 696 | wake_up(&stream->runtime->sleep); |
| 697 | mutex_lock(&stream->device->lock); |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 702 | static int snd_compr_drain(struct snd_compr_stream *stream) |
| 703 | { |
| 704 | int retval; |
| 705 | |
| 706 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || |
| 707 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) |
| 708 | return -EPERM; |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 709 | |
| 710 | stream->runtime->drain_wake = 0; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 711 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 712 | if (retval) { |
| 713 | pr_err("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 714 | wake_up(&stream->runtime->sleep); |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 715 | return retval; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 716 | } |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 717 | |
| 718 | retval = snd_compress_wait_for_drain(stream); |
| 719 | stream->runtime->state = SNDRV_PCM_STATE_SETUP; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 720 | return retval; |
| 721 | } |
| 722 | |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 723 | static int snd_compr_next_track(struct snd_compr_stream *stream) |
| 724 | { |
| 725 | int retval; |
| 726 | |
| 727 | /* only a running stream can transition to next track */ |
| 728 | if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) |
| 729 | return -EPERM; |
| 730 | |
| 731 | /* you can signal next track isf this is intended to be a gapless stream |
| 732 | * and current track metadata is set |
| 733 | */ |
| 734 | if (stream->metadata_set == false) |
| 735 | return -EPERM; |
| 736 | |
| 737 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK); |
| 738 | if (retval != 0) |
| 739 | return retval; |
| 740 | stream->metadata_set = false; |
| 741 | stream->next_track = true; |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int snd_compr_partial_drain(struct snd_compr_stream *stream) |
| 746 | { |
| 747 | int retval; |
| 748 | if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || |
| 749 | stream->runtime->state == SNDRV_PCM_STATE_SETUP) |
| 750 | return -EPERM; |
| 751 | /* stream can be drained only when next track has been signalled */ |
| 752 | if (stream->next_track == false) |
| 753 | return -EPERM; |
| 754 | |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 755 | stream->runtime->drain_wake = 0; |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 756 | retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN); |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 757 | if (retval) { |
| 758 | pr_err("Partial drain returned failure\n"); |
| 759 | wake_up(&stream->runtime->sleep); |
| 760 | return retval; |
| 761 | } |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 762 | |
| 763 | stream->next_track = false; |
Vinod Koul | 16442d4 | 2013-10-24 16:37:31 +0530 | [diff] [blame^] | 764 | return snd_compress_wait_for_drain(stream); |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 765 | } |
| 766 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 767 | static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
| 768 | { |
| 769 | struct snd_compr_file *data = f->private_data; |
| 770 | struct snd_compr_stream *stream; |
| 771 | int retval = -ENOTTY; |
| 772 | |
| 773 | if (snd_BUG_ON(!data)) |
| 774 | return -EFAULT; |
| 775 | stream = &data->stream; |
| 776 | if (snd_BUG_ON(!stream)) |
| 777 | return -EFAULT; |
| 778 | mutex_lock(&stream->device->lock); |
| 779 | switch (_IOC_NR(cmd)) { |
| 780 | case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): |
Vinod Koul | d54d012 | 2013-07-29 15:10:22 +0530 | [diff] [blame] | 781 | retval = put_user(SNDRV_COMPRESS_VERSION, |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 782 | (int __user *)arg) ? -EFAULT : 0; |
| 783 | break; |
| 784 | case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): |
| 785 | retval = snd_compr_get_caps(stream, arg); |
| 786 | break; |
| 787 | case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): |
| 788 | retval = snd_compr_get_codec_caps(stream, arg); |
| 789 | break; |
| 790 | case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): |
| 791 | retval = snd_compr_set_params(stream, arg); |
| 792 | break; |
| 793 | case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): |
| 794 | retval = snd_compr_get_params(stream, arg); |
| 795 | break; |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 796 | case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): |
| 797 | retval = snd_compr_set_metadata(stream, arg); |
| 798 | break; |
| 799 | case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): |
| 800 | retval = snd_compr_get_metadata(stream, arg); |
| 801 | break; |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 802 | case _IOC_NR(SNDRV_COMPRESS_TSTAMP): |
| 803 | retval = snd_compr_tstamp(stream, arg); |
| 804 | break; |
| 805 | case _IOC_NR(SNDRV_COMPRESS_AVAIL): |
| 806 | retval = snd_compr_ioctl_avail(stream, arg); |
| 807 | break; |
| 808 | case _IOC_NR(SNDRV_COMPRESS_PAUSE): |
| 809 | retval = snd_compr_pause(stream); |
| 810 | break; |
| 811 | case _IOC_NR(SNDRV_COMPRESS_RESUME): |
| 812 | retval = snd_compr_resume(stream); |
| 813 | break; |
| 814 | case _IOC_NR(SNDRV_COMPRESS_START): |
| 815 | retval = snd_compr_start(stream); |
| 816 | break; |
| 817 | case _IOC_NR(SNDRV_COMPRESS_STOP): |
| 818 | retval = snd_compr_stop(stream); |
| 819 | break; |
| 820 | case _IOC_NR(SNDRV_COMPRESS_DRAIN): |
| 821 | retval = snd_compr_drain(stream); |
| 822 | break; |
Jeeja KP | 9727b49 | 2013-02-14 16:52:51 +0530 | [diff] [blame] | 823 | case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): |
| 824 | retval = snd_compr_partial_drain(stream); |
| 825 | break; |
| 826 | case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): |
| 827 | retval = snd_compr_next_track(stream); |
| 828 | break; |
| 829 | |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 830 | } |
| 831 | mutex_unlock(&stream->device->lock); |
| 832 | return retval; |
| 833 | } |
| 834 | |
| 835 | static const struct file_operations snd_compr_file_ops = { |
| 836 | .owner = THIS_MODULE, |
| 837 | .open = snd_compr_open, |
| 838 | .release = snd_compr_free, |
| 839 | .write = snd_compr_write, |
| 840 | .read = snd_compr_read, |
| 841 | .unlocked_ioctl = snd_compr_ioctl, |
| 842 | .mmap = snd_compr_mmap, |
| 843 | .poll = snd_compr_poll, |
| 844 | }; |
| 845 | |
| 846 | static int snd_compress_dev_register(struct snd_device *device) |
| 847 | { |
| 848 | int ret = -EINVAL; |
| 849 | char str[16]; |
| 850 | struct snd_compr *compr; |
| 851 | |
| 852 | if (snd_BUG_ON(!device || !device->device_data)) |
| 853 | return -EBADFD; |
| 854 | compr = device->device_data; |
| 855 | |
| 856 | sprintf(str, "comprC%iD%i", compr->card->number, compr->device); |
| 857 | pr_debug("reg %s for device %s, direction %d\n", str, compr->name, |
| 858 | compr->direction); |
| 859 | /* register compressed device */ |
| 860 | ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, |
| 861 | compr->device, &snd_compr_file_ops, compr, str); |
| 862 | if (ret < 0) { |
| 863 | pr_err("snd_register_device failed\n %d", ret); |
| 864 | return ret; |
| 865 | } |
| 866 | return ret; |
| 867 | |
| 868 | } |
| 869 | |
| 870 | static int snd_compress_dev_disconnect(struct snd_device *device) |
| 871 | { |
| 872 | struct snd_compr *compr; |
| 873 | |
| 874 | compr = device->device_data; |
Liam Girdwood | 1aff416 | 2013-09-13 17:43:17 +0100 | [diff] [blame] | 875 | snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, |
| 876 | compr->device); |
Vinod Koul | b21c60a | 2011-12-23 10:36:39 +0530 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* |
| 881 | * snd_compress_new: create new compress device |
| 882 | * @card: sound card pointer |
| 883 | * @device: device number |
| 884 | * @dirn: device direction, should be of type enum snd_compr_direction |
| 885 | * @compr: compress device pointer |
| 886 | */ |
| 887 | int snd_compress_new(struct snd_card *card, int device, |
| 888 | int dirn, struct snd_compr *compr) |
| 889 | { |
| 890 | static struct snd_device_ops ops = { |
| 891 | .dev_free = NULL, |
| 892 | .dev_register = snd_compress_dev_register, |
| 893 | .dev_disconnect = snd_compress_dev_disconnect, |
| 894 | }; |
| 895 | |
| 896 | compr->card = card; |
| 897 | compr->device = device; |
| 898 | compr->direction = dirn; |
| 899 | return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); |
| 900 | } |
| 901 | EXPORT_SYMBOL_GPL(snd_compress_new); |
| 902 | |
| 903 | static int snd_compress_add_device(struct snd_compr *device) |
| 904 | { |
| 905 | int ret; |
| 906 | |
| 907 | if (!device->card) |
| 908 | return -EINVAL; |
| 909 | |
| 910 | /* register the card */ |
| 911 | ret = snd_card_register(device->card); |
| 912 | if (ret) |
| 913 | goto out; |
| 914 | return 0; |
| 915 | |
| 916 | out: |
| 917 | pr_err("failed with %d\n", ret); |
| 918 | return ret; |
| 919 | |
| 920 | } |
| 921 | |
| 922 | static int snd_compress_remove_device(struct snd_compr *device) |
| 923 | { |
| 924 | return snd_card_free(device->card); |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * snd_compress_register - register compressed device |
| 929 | * |
| 930 | * @device: compressed device to register |
| 931 | */ |
| 932 | int snd_compress_register(struct snd_compr *device) |
| 933 | { |
| 934 | int retval; |
| 935 | |
| 936 | if (device->name == NULL || device->dev == NULL || device->ops == NULL) |
| 937 | return -EINVAL; |
| 938 | |
| 939 | pr_debug("Registering compressed device %s\n", device->name); |
| 940 | if (snd_BUG_ON(!device->ops->open)) |
| 941 | return -EINVAL; |
| 942 | if (snd_BUG_ON(!device->ops->free)) |
| 943 | return -EINVAL; |
| 944 | if (snd_BUG_ON(!device->ops->set_params)) |
| 945 | return -EINVAL; |
| 946 | if (snd_BUG_ON(!device->ops->trigger)) |
| 947 | return -EINVAL; |
| 948 | |
| 949 | mutex_init(&device->lock); |
| 950 | |
| 951 | /* register a compressed card */ |
| 952 | mutex_lock(&device_mutex); |
| 953 | retval = snd_compress_add_device(device); |
| 954 | mutex_unlock(&device_mutex); |
| 955 | return retval; |
| 956 | } |
| 957 | EXPORT_SYMBOL_GPL(snd_compress_register); |
| 958 | |
| 959 | int snd_compress_deregister(struct snd_compr *device) |
| 960 | { |
| 961 | pr_debug("Removing compressed device %s\n", device->name); |
| 962 | mutex_lock(&device_mutex); |
| 963 | snd_compress_remove_device(device); |
| 964 | mutex_unlock(&device_mutex); |
| 965 | return 0; |
| 966 | } |
| 967 | EXPORT_SYMBOL_GPL(snd_compress_deregister); |
| 968 | |
| 969 | static int __init snd_compress_init(void) |
| 970 | { |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static void __exit snd_compress_exit(void) |
| 975 | { |
| 976 | } |
| 977 | |
| 978 | module_init(snd_compress_init); |
| 979 | module_exit(snd_compress_exit); |
| 980 | |
| 981 | MODULE_DESCRIPTION("ALSA Compressed offload framework"); |
| 982 | MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); |
| 983 | MODULE_LICENSE("GPL v2"); |