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