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