Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Digital Audio (PCM) abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/file.h> |
| 25 | #include <linux/slab.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 26 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/time.h> |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 28 | #include <linux/pm_qos.h> |
Takashi Iwai | 6cbbfe1 | 2015-01-28 16:49:33 +0100 | [diff] [blame] | 29 | #include <linux/io.h> |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 30 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/core.h> |
| 32 | #include <sound/control.h> |
| 33 | #include <sound/info.h> |
| 34 | #include <sound/pcm.h> |
| 35 | #include <sound/pcm_params.h> |
| 36 | #include <sound/timer.h> |
| 37 | #include <sound/minors.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 38 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Takashi Sakamoto | 2c4842d | 2017-05-26 09:30:46 +0900 | [diff] [blame^] | 40 | #include "pcm_local.h" |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Compatibility |
| 44 | */ |
| 45 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 46 | struct snd_pcm_hw_params_old { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | unsigned int flags; |
| 48 | unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - |
| 49 | SNDRV_PCM_HW_PARAM_ACCESS + 1]; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 50 | struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1]; |
| 52 | unsigned int rmask; |
| 53 | unsigned int cmask; |
| 54 | unsigned int info; |
| 55 | unsigned int msbits; |
| 56 | unsigned int rate_num; |
| 57 | unsigned int rate_den; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 58 | snd_pcm_uframes_t fifo_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | unsigned char reserved[64]; |
| 60 | }; |
| 61 | |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 62 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 63 | #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old) |
| 64 | #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 66 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
| 67 | struct snd_pcm_hw_params_old __user * _oparams); |
| 68 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, |
| 69 | struct snd_pcm_hw_params_old __user * _oparams); |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 70 | #endif |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 71 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * |
| 75 | */ |
| 76 | |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 77 | static DEFINE_RWLOCK(snd_pcm_link_rwlock); |
| 78 | static DECLARE_RWSEM(snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Takashi Iwai | 67ec1072 | 2016-02-17 14:30:26 +0100 | [diff] [blame] | 80 | /* Writer in rwsem may block readers even during its waiting in queue, |
| 81 | * and this may lead to a deadlock when the code path takes read sem |
| 82 | * twice (e.g. one in snd_pcm_action_nonatomic() and another in |
| 83 | * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to |
| 84 | * spin until it gets the lock. |
| 85 | */ |
| 86 | static inline void down_write_nonblock(struct rw_semaphore *lock) |
| 87 | { |
| 88 | while (!down_write_trylock(lock)) |
| 89 | cond_resched(); |
| 90 | } |
| 91 | |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 92 | /** |
| 93 | * snd_pcm_stream_lock - Lock the PCM stream |
| 94 | * @substream: PCM substream |
| 95 | * |
| 96 | * This locks the PCM stream's spinlock or mutex depending on the nonatomic |
| 97 | * flag of the given substream. This also takes the global link rw lock |
| 98 | * (or rw sem), too, for avoiding the race with linked streams. |
| 99 | */ |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 100 | void snd_pcm_stream_lock(struct snd_pcm_substream *substream) |
| 101 | { |
| 102 | if (substream->pcm->nonatomic) { |
Takashi Iwai | 67756e3 | 2015-07-17 15:22:33 +0200 | [diff] [blame] | 103 | down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING); |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 104 | mutex_lock(&substream->self_group.mutex); |
| 105 | } else { |
| 106 | read_lock(&snd_pcm_link_rwlock); |
| 107 | spin_lock(&substream->self_group.lock); |
| 108 | } |
| 109 | } |
| 110 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock); |
| 111 | |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 112 | /** |
| 113 | * snd_pcm_stream_lock - Unlock the PCM stream |
| 114 | * @substream: PCM substream |
| 115 | * |
| 116 | * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock(). |
| 117 | */ |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 118 | void snd_pcm_stream_unlock(struct snd_pcm_substream *substream) |
| 119 | { |
| 120 | if (substream->pcm->nonatomic) { |
| 121 | mutex_unlock(&substream->self_group.mutex); |
| 122 | up_read(&snd_pcm_link_rwsem); |
| 123 | } else { |
| 124 | spin_unlock(&substream->self_group.lock); |
| 125 | read_unlock(&snd_pcm_link_rwlock); |
| 126 | } |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock); |
| 129 | |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 130 | /** |
| 131 | * snd_pcm_stream_lock_irq - Lock the PCM stream |
| 132 | * @substream: PCM substream |
| 133 | * |
| 134 | * This locks the PCM stream like snd_pcm_stream_lock() and disables the local |
| 135 | * IRQ (only when nonatomic is false). In nonatomic case, this is identical |
| 136 | * as snd_pcm_stream_lock(). |
| 137 | */ |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 138 | void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream) |
| 139 | { |
| 140 | if (!substream->pcm->nonatomic) |
| 141 | local_irq_disable(); |
| 142 | snd_pcm_stream_lock(substream); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq); |
| 145 | |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 146 | /** |
| 147 | * snd_pcm_stream_unlock_irq - Unlock the PCM stream |
| 148 | * @substream: PCM substream |
| 149 | * |
| 150 | * This is a counter-part of snd_pcm_stream_lock_irq(). |
| 151 | */ |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 152 | void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream) |
| 153 | { |
| 154 | snd_pcm_stream_unlock(substream); |
| 155 | if (!substream->pcm->nonatomic) |
| 156 | local_irq_enable(); |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq); |
| 159 | |
| 160 | unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream) |
| 161 | { |
| 162 | unsigned long flags = 0; |
| 163 | if (!substream->pcm->nonatomic) |
| 164 | local_irq_save(flags); |
| 165 | snd_pcm_stream_lock(substream); |
| 166 | return flags; |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave); |
| 169 | |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 170 | /** |
| 171 | * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream |
| 172 | * @substream: PCM substream |
| 173 | * @flags: irq flags |
| 174 | * |
| 175 | * This is a counter-part of snd_pcm_stream_lock_irqsave(). |
| 176 | */ |
Takashi Iwai | 7af142f | 2014-09-01 11:19:37 +0200 | [diff] [blame] | 177 | void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, |
| 178 | unsigned long flags) |
| 179 | { |
| 180 | snd_pcm_stream_unlock(substream); |
| 181 | if (!substream->pcm->nonatomic) |
| 182 | local_irq_restore(flags); |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 186 | int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 188 | struct snd_pcm_runtime *runtime; |
| 189 | struct snd_pcm *pcm = substream->pcm; |
| 190 | struct snd_pcm_str *pstr = substream->pstr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | memset(info, 0, sizeof(*info)); |
| 193 | info->card = pcm->card->number; |
| 194 | info->device = pcm->device; |
| 195 | info->stream = substream->stream; |
| 196 | info->subdevice = substream->number; |
| 197 | strlcpy(info->id, pcm->id, sizeof(info->id)); |
| 198 | strlcpy(info->name, pcm->name, sizeof(info->name)); |
| 199 | info->dev_class = pcm->dev_class; |
| 200 | info->dev_subclass = pcm->dev_subclass; |
| 201 | info->subdevices_count = pstr->substream_count; |
| 202 | info->subdevices_avail = pstr->substream_count - pstr->substream_opened; |
| 203 | strlcpy(info->subname, substream->name, sizeof(info->subname)); |
| 204 | runtime = substream->runtime; |
| 205 | /* AB: FIXME!!! This is definitely nonsense */ |
| 206 | if (runtime) { |
| 207 | info->sync = runtime->sync; |
| 208 | substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info); |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 213 | int snd_pcm_info_user(struct snd_pcm_substream *substream, |
| 214 | struct snd_pcm_info __user * _info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 216 | struct snd_pcm_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int err; |
| 218 | |
| 219 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 220 | if (! info) |
| 221 | return -ENOMEM; |
| 222 | err = snd_pcm_info(substream, info); |
| 223 | if (err >= 0) { |
| 224 | if (copy_to_user(_info, info, sizeof(*info))) |
| 225 | err = -EFAULT; |
| 226 | } |
| 227 | kfree(info); |
| 228 | return err; |
| 229 | } |
| 230 | |
Takashi Iwai | 63825f3 | 2014-10-22 12:04:46 +0200 | [diff] [blame] | 231 | static bool hw_support_mmap(struct snd_pcm_substream *substream) |
| 232 | { |
| 233 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP)) |
| 234 | return false; |
| 235 | /* check architectures that return -EINVAL from dma_mmap_coherent() */ |
| 236 | /* FIXME: this should be some global flag */ |
| 237 | #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\ |
| 238 | defined(CONFIG_PARISC) || defined(CONFIG_XTENSA) |
| 239 | if (!substream->ops->mmap && |
| 240 | substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) |
| 241 | return false; |
| 242 | #endif |
| 243 | return true; |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | #undef RULES_DEBUG |
| 247 | |
| 248 | #ifdef RULES_DEBUG |
| 249 | #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v |
Joe Perches | 47023ec | 2010-09-13 21:24:02 -0700 | [diff] [blame] | 250 | static const char * const snd_pcm_hw_param_names[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | HW_PARAM(ACCESS), |
| 252 | HW_PARAM(FORMAT), |
| 253 | HW_PARAM(SUBFORMAT), |
| 254 | HW_PARAM(SAMPLE_BITS), |
| 255 | HW_PARAM(FRAME_BITS), |
| 256 | HW_PARAM(CHANNELS), |
| 257 | HW_PARAM(RATE), |
| 258 | HW_PARAM(PERIOD_TIME), |
| 259 | HW_PARAM(PERIOD_SIZE), |
| 260 | HW_PARAM(PERIOD_BYTES), |
| 261 | HW_PARAM(PERIODS), |
| 262 | HW_PARAM(BUFFER_TIME), |
| 263 | HW_PARAM(BUFFER_SIZE), |
| 264 | HW_PARAM(BUFFER_BYTES), |
| 265 | HW_PARAM(TICK_TIME), |
| 266 | }; |
| 267 | #endif |
| 268 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 269 | int snd_pcm_hw_refine(struct snd_pcm_substream *substream, |
| 270 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 273 | struct snd_pcm_hardware *hw; |
| 274 | struct snd_interval *i = NULL; |
| 275 | struct snd_mask *m = NULL; |
| 276 | struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | unsigned int rstamps[constrs->rules_num]; |
| 278 | unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1]; |
| 279 | unsigned int stamp = 2; |
| 280 | int changed, again; |
| 281 | |
| 282 | params->info = 0; |
| 283 | params->fifo_size = 0; |
| 284 | if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS)) |
| 285 | params->msbits = 0; |
| 286 | if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) { |
| 287 | params->rate_num = 0; |
| 288 | params->rate_den = 0; |
| 289 | } |
| 290 | |
| 291 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { |
| 292 | m = hw_param_mask(params, k); |
| 293 | if (snd_mask_empty(m)) |
| 294 | return -EINVAL; |
| 295 | if (!(params->rmask & (1 << k))) |
| 296 | continue; |
| 297 | #ifdef RULES_DEBUG |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 298 | pr_debug("%s = ", snd_pcm_hw_param_names[k]); |
| 299 | pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | #endif |
| 301 | changed = snd_mask_refine(m, constrs_mask(constrs, k)); |
| 302 | #ifdef RULES_DEBUG |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 303 | pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | #endif |
| 305 | if (changed) |
| 306 | params->cmask |= 1 << k; |
| 307 | if (changed < 0) |
| 308 | return changed; |
| 309 | } |
| 310 | |
| 311 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { |
| 312 | i = hw_param_interval(params, k); |
| 313 | if (snd_interval_empty(i)) |
| 314 | return -EINVAL; |
| 315 | if (!(params->rmask & (1 << k))) |
| 316 | continue; |
| 317 | #ifdef RULES_DEBUG |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 318 | pr_debug("%s = ", snd_pcm_hw_param_names[k]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (i->empty) |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 320 | pr_cont("empty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | else |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 322 | pr_cont("%c%u %u%c", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | i->openmin ? '(' : '[', i->min, |
| 324 | i->max, i->openmax ? ')' : ']'); |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 325 | pr_cont(" -> "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | #endif |
| 327 | changed = snd_interval_refine(i, constrs_interval(constrs, k)); |
| 328 | #ifdef RULES_DEBUG |
| 329 | if (i->empty) |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 330 | pr_cont("empty\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | else |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 332 | pr_cont("%c%u %u%c\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | i->openmin ? '(' : '[', i->min, |
| 334 | i->max, i->openmax ? ')' : ']'); |
| 335 | #endif |
| 336 | if (changed) |
| 337 | params->cmask |= 1 << k; |
| 338 | if (changed < 0) |
| 339 | return changed; |
| 340 | } |
| 341 | |
| 342 | for (k = 0; k < constrs->rules_num; k++) |
| 343 | rstamps[k] = 0; |
| 344 | for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
| 345 | vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0; |
| 346 | do { |
| 347 | again = 0; |
| 348 | for (k = 0; k < constrs->rules_num; k++) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 349 | struct snd_pcm_hw_rule *r = &constrs->rules[k]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | unsigned int d; |
| 351 | int doit = 0; |
| 352 | if (r->cond && !(r->cond & params->flags)) |
| 353 | continue; |
| 354 | for (d = 0; r->deps[d] >= 0; d++) { |
| 355 | if (vstamps[r->deps[d]] > rstamps[k]) { |
| 356 | doit = 1; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | if (!doit) |
| 361 | continue; |
| 362 | #ifdef RULES_DEBUG |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 363 | pr_debug("Rule %d [%p]: ", k, r->func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (r->var >= 0) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 365 | pr_cont("%s = ", snd_pcm_hw_param_names[r->var]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (hw_is_mask(r->var)) { |
| 367 | m = hw_param_mask(params, r->var); |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 368 | pr_cont("%x", *m->bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } else { |
| 370 | i = hw_param_interval(params, r->var); |
| 371 | if (i->empty) |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 372 | pr_cont("empty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | else |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 374 | pr_cont("%c%u %u%c", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | i->openmin ? '(' : '[', i->min, |
| 376 | i->max, i->openmax ? ')' : ']'); |
| 377 | } |
| 378 | } |
| 379 | #endif |
| 380 | changed = r->func(params, r); |
| 381 | #ifdef RULES_DEBUG |
| 382 | if (r->var >= 0) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 383 | pr_cont(" -> "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (hw_is_mask(r->var)) |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 385 | pr_cont("%x", *m->bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | else { |
| 387 | if (i->empty) |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 388 | pr_cont("empty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | else |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 390 | pr_cont("%c%u %u%c", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | i->openmin ? '(' : '[', i->min, |
| 392 | i->max, i->openmax ? ')' : ']'); |
| 393 | } |
| 394 | } |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 395 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | #endif |
| 397 | rstamps[k] = stamp; |
| 398 | if (changed && r->var >= 0) { |
| 399 | params->cmask |= (1 << r->var); |
| 400 | vstamps[r->var] = stamp; |
| 401 | again = 1; |
| 402 | } |
| 403 | if (changed < 0) |
| 404 | return changed; |
| 405 | stamp++; |
| 406 | } |
| 407 | } while (again); |
| 408 | if (!params->msbits) { |
| 409 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
| 410 | if (snd_interval_single(i)) |
| 411 | params->msbits = snd_interval_value(i); |
| 412 | } |
| 413 | |
| 414 | if (!params->rate_den) { |
| 415 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 416 | if (snd_interval_single(i)) { |
| 417 | params->rate_num = snd_interval_value(i); |
| 418 | params->rate_den = 1; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | hw = &substream->runtime->hw; |
Takashi Iwai | 63825f3 | 2014-10-22 12:04:46 +0200 | [diff] [blame] | 423 | if (!params->info) { |
Libin Yang | 48d8829 | 2014-12-31 22:09:54 +0800 | [diff] [blame] | 424 | params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES | |
| 425 | SNDRV_PCM_INFO_DRAIN_TRIGGER); |
Takashi Iwai | 63825f3 | 2014-10-22 12:04:46 +0200 | [diff] [blame] | 426 | if (!hw_support_mmap(substream)) |
| 427 | params->info &= ~(SNDRV_PCM_INFO_MMAP | |
| 428 | SNDRV_PCM_INFO_MMAP_VALID); |
| 429 | } |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 430 | if (!params->fifo_size) { |
Jaroslav Kysela | 3be522a | 2010-02-16 11:55:43 +0100 | [diff] [blame] | 431 | m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 432 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 433 | if (snd_mask_min(m) == snd_mask_max(m) && |
| 434 | snd_interval_min(i) == snd_interval_max(i)) { |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 435 | changed = substream->ops->ioctl(substream, |
| 436 | SNDRV_PCM_IOCTL1_FIFO_SIZE, params); |
Mariusz Kozlowski | 8bd9bca | 2009-06-21 20:26:59 +0200 | [diff] [blame] | 437 | if (changed < 0) |
Jaroslav Kysela | 8bea869 | 2009-04-27 09:44:40 +0200 | [diff] [blame] | 438 | return changed; |
| 439 | } |
| 440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | params->rmask = 0; |
| 442 | return 0; |
| 443 | } |
| 444 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 445 | EXPORT_SYMBOL(snd_pcm_hw_refine); |
| 446 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 447 | static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream, |
| 448 | struct snd_pcm_hw_params __user * _params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 450 | struct snd_pcm_hw_params *params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | int err; |
| 452 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 453 | params = memdup_user(_params, sizeof(*params)); |
| 454 | if (IS_ERR(params)) |
| 455 | return PTR_ERR(params); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | err = snd_pcm_hw_refine(substream, params); |
| 458 | if (copy_to_user(_params, params, sizeof(*params))) { |
| 459 | if (!err) |
| 460 | err = -EFAULT; |
| 461 | } |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | kfree(params); |
| 464 | return err; |
| 465 | } |
| 466 | |
Takashi Iwai | 9442e69 | 2006-09-30 23:27:19 -0700 | [diff] [blame] | 467 | static int period_to_usecs(struct snd_pcm_runtime *runtime) |
| 468 | { |
| 469 | int usecs; |
| 470 | |
| 471 | if (! runtime->rate) |
| 472 | return -1; /* invalid */ |
| 473 | |
| 474 | /* take 75% of period time as the deadline */ |
| 475 | usecs = (750000 / runtime->rate) * runtime->period_size; |
| 476 | usecs += ((750000 % runtime->rate) * runtime->period_size) / |
| 477 | runtime->rate; |
| 478 | |
| 479 | return usecs; |
| 480 | } |
| 481 | |
Takashi Iwai | 9b0573c | 2012-10-12 15:07:34 +0200 | [diff] [blame] | 482 | static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state) |
| 483 | { |
| 484 | snd_pcm_stream_lock_irq(substream); |
| 485 | if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED) |
| 486 | substream->runtime->status->state = state; |
| 487 | snd_pcm_stream_unlock_irq(substream); |
| 488 | } |
| 489 | |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 490 | static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream, |
| 491 | int event) |
| 492 | { |
| 493 | #ifdef CONFIG_SND_PCM_TIMER |
| 494 | if (substream->timer) |
| 495 | snd_timer_notify(substream->timer, event, |
| 496 | &substream->runtime->trigger_tstamp); |
| 497 | #endif |
| 498 | } |
| 499 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 500 | static int snd_pcm_hw_params(struct snd_pcm_substream *substream, |
| 501 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 503 | struct snd_pcm_runtime *runtime; |
Takashi Iwai | 9442e69 | 2006-09-30 23:27:19 -0700 | [diff] [blame] | 504 | int err, usecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | unsigned int bits; |
| 506 | snd_pcm_uframes_t frames; |
| 507 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 508 | if (PCM_RUNTIME_CHECK(substream)) |
| 509 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | snd_pcm_stream_lock_irq(substream); |
| 512 | switch (runtime->status->state) { |
| 513 | case SNDRV_PCM_STATE_OPEN: |
| 514 | case SNDRV_PCM_STATE_SETUP: |
| 515 | case SNDRV_PCM_STATE_PREPARED: |
| 516 | break; |
| 517 | default: |
| 518 | snd_pcm_stream_unlock_irq(substream); |
| 519 | return -EBADFD; |
| 520 | } |
| 521 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 522 | #if IS_ENABLED(CONFIG_SND_PCM_OSS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (!substream->oss.oss) |
| 524 | #endif |
Takashi Iwai | 9c323fc | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 525 | if (atomic_read(&substream->mmap_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return -EBADFD; |
| 527 | |
| 528 | params->rmask = ~0U; |
| 529 | err = snd_pcm_hw_refine(substream, params); |
| 530 | if (err < 0) |
| 531 | goto _error; |
| 532 | |
| 533 | err = snd_pcm_hw_params_choose(substream, params); |
| 534 | if (err < 0) |
| 535 | goto _error; |
| 536 | |
| 537 | if (substream->ops->hw_params != NULL) { |
| 538 | err = substream->ops->hw_params(substream, params); |
| 539 | if (err < 0) |
| 540 | goto _error; |
| 541 | } |
| 542 | |
| 543 | runtime->access = params_access(params); |
| 544 | runtime->format = params_format(params); |
| 545 | runtime->subformat = params_subformat(params); |
| 546 | runtime->channels = params_channels(params); |
| 547 | runtime->rate = params_rate(params); |
| 548 | runtime->period_size = params_period_size(params); |
| 549 | runtime->periods = params_periods(params); |
| 550 | runtime->buffer_size = params_buffer_size(params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | runtime->info = params->info; |
| 552 | runtime->rate_num = params->rate_num; |
| 553 | runtime->rate_den = params->rate_den; |
Clemens Ladisch | ab69a49 | 2010-11-15 10:46:23 +0100 | [diff] [blame] | 554 | runtime->no_period_wakeup = |
| 555 | (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) && |
| 556 | (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | bits = snd_pcm_format_physical_width(runtime->format); |
| 559 | runtime->sample_bits = bits; |
| 560 | bits *= runtime->channels; |
| 561 | runtime->frame_bits = bits; |
| 562 | frames = 1; |
| 563 | while (bits % 8 != 0) { |
| 564 | bits *= 2; |
| 565 | frames *= 2; |
| 566 | } |
| 567 | runtime->byte_align = bits / 8; |
| 568 | runtime->min_align = frames; |
| 569 | |
| 570 | /* Default sw params */ |
| 571 | runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; |
| 572 | runtime->period_step = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | runtime->control->avail_min = runtime->period_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | runtime->start_threshold = 1; |
| 575 | runtime->stop_threshold = runtime->buffer_size; |
| 576 | runtime->silence_threshold = 0; |
| 577 | runtime->silence_size = 0; |
Clemens Ladisch | ead4046 | 2010-05-21 09:15:59 +0200 | [diff] [blame] | 578 | runtime->boundary = runtime->buffer_size; |
| 579 | while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) |
| 580 | runtime->boundary *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | snd_pcm_timer_resolution_change(substream); |
Takashi Iwai | 9b0573c | 2012-10-12 15:07:34 +0200 | [diff] [blame] | 583 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP); |
Takashi Iwai | 9442e69 | 2006-09-30 23:27:19 -0700 | [diff] [blame] | 584 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 585 | if (pm_qos_request_active(&substream->latency_pm_qos_req)) |
| 586 | pm_qos_remove_request(&substream->latency_pm_qos_req); |
Takashi Iwai | 9442e69 | 2006-09-30 23:27:19 -0700 | [diff] [blame] | 587 | if ((usecs = period_to_usecs(runtime)) >= 0) |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 588 | pm_qos_add_request(&substream->latency_pm_qos_req, |
| 589 | PM_QOS_CPU_DMA_LATENCY, usecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return 0; |
| 591 | _error: |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 592 | /* hardware might be unusable from this time, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | so we force application to retry to set |
| 594 | the correct hardware parameter settings */ |
Takashi Iwai | 9b0573c | 2012-10-12 15:07:34 +0200 | [diff] [blame] | 595 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (substream->ops->hw_free != NULL) |
| 597 | substream->ops->hw_free(substream); |
| 598 | return err; |
| 599 | } |
| 600 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 601 | static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream, |
| 602 | struct snd_pcm_hw_params __user * _params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 604 | struct snd_pcm_hw_params *params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | int err; |
| 606 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 607 | params = memdup_user(_params, sizeof(*params)); |
| 608 | if (IS_ERR(params)) |
| 609 | return PTR_ERR(params); |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | err = snd_pcm_hw_params(substream, params); |
| 612 | if (copy_to_user(_params, params, sizeof(*params))) { |
| 613 | if (!err) |
| 614 | err = -EFAULT; |
| 615 | } |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | kfree(params); |
| 618 | return err; |
| 619 | } |
| 620 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 621 | static int snd_pcm_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 623 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | int result = 0; |
| 625 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 626 | if (PCM_RUNTIME_CHECK(substream)) |
| 627 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | snd_pcm_stream_lock_irq(substream); |
| 630 | switch (runtime->status->state) { |
| 631 | case SNDRV_PCM_STATE_SETUP: |
| 632 | case SNDRV_PCM_STATE_PREPARED: |
| 633 | break; |
| 634 | default: |
| 635 | snd_pcm_stream_unlock_irq(substream); |
| 636 | return -EBADFD; |
| 637 | } |
| 638 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | 9c323fc | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 639 | if (atomic_read(&substream->mmap_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | return -EBADFD; |
| 641 | if (substream->ops->hw_free) |
| 642 | result = substream->ops->hw_free(substream); |
Takashi Iwai | 9b0573c | 2012-10-12 15:07:34 +0200 | [diff] [blame] | 643 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 644 | pm_qos_remove_request(&substream->latency_pm_qos_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | return result; |
| 646 | } |
| 647 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 648 | static int snd_pcm_sw_params(struct snd_pcm_substream *substream, |
| 649 | struct snd_pcm_sw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 651 | struct snd_pcm_runtime *runtime; |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 652 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 654 | if (PCM_RUNTIME_CHECK(substream)) |
| 655 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | snd_pcm_stream_lock_irq(substream); |
| 658 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
| 659 | snd_pcm_stream_unlock_irq(substream); |
| 660 | return -EBADFD; |
| 661 | } |
| 662 | snd_pcm_stream_unlock_irq(substream); |
| 663 | |
Dan Carpenter | 145d92e | 2015-09-23 12:42:28 +0300 | [diff] [blame] | 664 | if (params->tstamp_mode < 0 || |
| 665 | params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | return -EINVAL; |
Takashi Iwai | 58900810 | 2014-07-16 17:45:27 +0200 | [diff] [blame] | 667 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) && |
| 668 | params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST) |
Takashi Iwai | 5646eda | 2014-07-10 09:50:19 +0200 | [diff] [blame] | 669 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (params->avail_min == 0) |
| 671 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (params->silence_size >= runtime->boundary) { |
| 673 | if (params->silence_threshold != 0) |
| 674 | return -EINVAL; |
| 675 | } else { |
| 676 | if (params->silence_size > params->silence_threshold) |
| 677 | return -EINVAL; |
| 678 | if (params->silence_threshold > runtime->buffer_size) |
| 679 | return -EINVAL; |
| 680 | } |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 681 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | snd_pcm_stream_lock_irq(substream); |
| 683 | runtime->tstamp_mode = params->tstamp_mode; |
Takashi Iwai | 58900810 | 2014-07-16 17:45:27 +0200 | [diff] [blame] | 684 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12)) |
| 685 | runtime->tstamp_type = params->tstamp_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | runtime->period_step = params->period_step; |
| 687 | runtime->control->avail_min = params->avail_min; |
| 688 | runtime->start_threshold = params->start_threshold; |
| 689 | runtime->stop_threshold = params->stop_threshold; |
| 690 | runtime->silence_threshold = params->silence_threshold; |
| 691 | runtime->silence_size = params->silence_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | params->boundary = runtime->boundary; |
| 693 | if (snd_pcm_running(substream)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 695 | runtime->silence_size > 0) |
| 696 | snd_pcm_playback_silence(substream, ULONG_MAX); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 697 | err = snd_pcm_update_state(substream, runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | snd_pcm_stream_unlock_irq(substream); |
Jaroslav Kysela | 1250932 | 2010-01-07 15:36:31 +0100 | [diff] [blame] | 700 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 703 | static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, |
| 704 | struct snd_pcm_sw_params __user * _params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 706 | struct snd_pcm_sw_params params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | int err; |
| 708 | if (copy_from_user(¶ms, _params, sizeof(params))) |
| 709 | return -EFAULT; |
| 710 | err = snd_pcm_sw_params(substream, ¶ms); |
| 711 | if (copy_to_user(_params, ¶ms, sizeof(params))) |
| 712 | return -EFAULT; |
| 713 | return err; |
| 714 | } |
| 715 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 716 | int snd_pcm_status(struct snd_pcm_substream *substream, |
| 717 | struct snd_pcm_status *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 719 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | snd_pcm_stream_lock_irq(substream); |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 722 | |
| 723 | snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data, |
| 724 | &runtime->audio_tstamp_config); |
| 725 | |
| 726 | /* backwards compatible behavior */ |
| 727 | if (runtime->audio_tstamp_config.type_requested == |
| 728 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) { |
| 729 | if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK) |
| 730 | runtime->audio_tstamp_config.type_requested = |
| 731 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK; |
| 732 | else |
| 733 | runtime->audio_tstamp_config.type_requested = |
| 734 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT; |
| 735 | runtime->audio_tstamp_report.valid = 0; |
| 736 | } else |
| 737 | runtime->audio_tstamp_report.valid = 1; |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | status->state = runtime->status->state; |
| 740 | status->suspended_state = runtime->status->suspended_state; |
| 741 | if (status->state == SNDRV_PCM_STATE_OPEN) |
| 742 | goto _end; |
| 743 | status->trigger_tstamp = runtime->trigger_tstamp; |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 744 | if (snd_pcm_running(substream)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | snd_pcm_update_hw_ptr(substream); |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 746 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
| 747 | status->tstamp = runtime->status->tstamp; |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 748 | status->driver_tstamp = runtime->driver_tstamp; |
Pierre-Louis Bossart | 4eeaaea | 2012-10-22 16:42:15 -0500 | [diff] [blame] | 749 | status->audio_tstamp = |
| 750 | runtime->status->audio_tstamp; |
Pierre-Louis Bossart | 3179f62 | 2015-02-13 15:14:06 -0600 | [diff] [blame] | 751 | if (runtime->audio_tstamp_report.valid == 1) |
| 752 | /* backwards compatibility, no report provided in COMPAT mode */ |
| 753 | snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data, |
| 754 | &status->audio_tstamp_accuracy, |
| 755 | &runtime->audio_tstamp_report); |
| 756 | |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 757 | goto _tstamp_end; |
| 758 | } |
Pierre-Louis Bossart | 0d59b81 | 2015-02-06 15:55:50 -0600 | [diff] [blame] | 759 | } else { |
| 760 | /* get tstamp only in fallback mode and only if enabled */ |
| 761 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) |
| 762 | snd_pcm_gettime(runtime, &status->tstamp); |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 763 | } |
Jaroslav Kysela | 8c12158 | 2008-01-11 08:45:08 +0100 | [diff] [blame] | 764 | _tstamp_end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | status->appl_ptr = runtime->control->appl_ptr; |
| 766 | status->hw_ptr = runtime->status->hw_ptr; |
| 767 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 768 | status->avail = snd_pcm_playback_avail(runtime); |
| 769 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING || |
Takashi Iwai | 4bbe1dd | 2008-10-13 03:07:14 +0200 | [diff] [blame] | 770 | runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | status->delay = runtime->buffer_size - status->avail; |
Takashi Iwai | 4bbe1dd | 2008-10-13 03:07:14 +0200 | [diff] [blame] | 772 | status->delay += runtime->delay; |
| 773 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | status->delay = 0; |
| 775 | } else { |
| 776 | status->avail = snd_pcm_capture_avail(runtime); |
| 777 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) |
Takashi Iwai | 4bbe1dd | 2008-10-13 03:07:14 +0200 | [diff] [blame] | 778 | status->delay = status->avail + runtime->delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | else |
| 780 | status->delay = 0; |
| 781 | } |
| 782 | status->avail_max = runtime->avail_max; |
| 783 | status->overrange = runtime->overrange; |
| 784 | runtime->avail_max = 0; |
| 785 | runtime->overrange = 0; |
| 786 | _end: |
| 787 | snd_pcm_stream_unlock_irq(substream); |
| 788 | return 0; |
| 789 | } |
| 790 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 791 | static int snd_pcm_status_user(struct snd_pcm_substream *substream, |
Pierre-Louis Bossart | 38ca2a4 | 2015-02-13 15:14:04 -0600 | [diff] [blame] | 792 | struct snd_pcm_status __user * _status, |
| 793 | bool ext) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 795 | struct snd_pcm_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | int res; |
Pierre-Louis Bossart | 38ca2a4 | 2015-02-13 15:14:04 -0600 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | memset(&status, 0, sizeof(status)); |
Pierre-Louis Bossart | 38ca2a4 | 2015-02-13 15:14:04 -0600 | [diff] [blame] | 799 | /* |
| 800 | * with extension, parameters are read/write, |
| 801 | * get audio_tstamp_data from user, |
| 802 | * ignore rest of status structure |
| 803 | */ |
| 804 | if (ext && get_user(status.audio_tstamp_data, |
| 805 | (u32 __user *)(&_status->audio_tstamp_data))) |
| 806 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | res = snd_pcm_status(substream, &status); |
| 808 | if (res < 0) |
| 809 | return res; |
| 810 | if (copy_to_user(_status, &status, sizeof(status))) |
| 811 | return -EFAULT; |
| 812 | return 0; |
| 813 | } |
| 814 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 815 | static int snd_pcm_channel_info(struct snd_pcm_substream *substream, |
| 816 | struct snd_pcm_channel_info * info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 818 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | unsigned int channel; |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | channel = info->channel; |
| 822 | runtime = substream->runtime; |
| 823 | snd_pcm_stream_lock_irq(substream); |
| 824 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
| 825 | snd_pcm_stream_unlock_irq(substream); |
| 826 | return -EBADFD; |
| 827 | } |
| 828 | snd_pcm_stream_unlock_irq(substream); |
| 829 | if (channel >= runtime->channels) |
| 830 | return -EINVAL; |
| 831 | memset(info, 0, sizeof(*info)); |
| 832 | info->channel = channel; |
| 833 | return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info); |
| 834 | } |
| 835 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 836 | static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream, |
| 837 | struct snd_pcm_channel_info __user * _info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 839 | struct snd_pcm_channel_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | int res; |
| 841 | |
| 842 | if (copy_from_user(&info, _info, sizeof(info))) |
| 843 | return -EFAULT; |
| 844 | res = snd_pcm_channel_info(substream, &info); |
| 845 | if (res < 0) |
| 846 | return res; |
| 847 | if (copy_to_user(_info, &info, sizeof(info))) |
| 848 | return -EFAULT; |
| 849 | return 0; |
| 850 | } |
| 851 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 852 | static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 854 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (runtime->trigger_master == NULL) |
| 856 | return; |
| 857 | if (runtime->trigger_master == substream) { |
Pierre-Louis Bossart | 2b79d7a | 2015-02-06 15:55:51 -0600 | [diff] [blame] | 858 | if (!runtime->trigger_tstamp_latched) |
| 859 | snd_pcm_gettime(runtime, &runtime->trigger_tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } else { |
| 861 | snd_pcm_trigger_tstamp(runtime->trigger_master); |
| 862 | runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp; |
| 863 | } |
| 864 | runtime->trigger_master = NULL; |
| 865 | } |
| 866 | |
| 867 | struct action_ops { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 868 | int (*pre_action)(struct snd_pcm_substream *substream, int state); |
| 869 | int (*do_action)(struct snd_pcm_substream *substream, int state); |
| 870 | void (*undo_action)(struct snd_pcm_substream *substream, int state); |
| 871 | void (*post_action)(struct snd_pcm_substream *substream, int state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | /* |
| 875 | * this functions is core for handling of linked stream |
| 876 | * Note: the stream state might be changed also on failure |
| 877 | * Note2: call with calling stream lock + link lock |
| 878 | */ |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 879 | static int snd_pcm_action_group(const struct action_ops *ops, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 880 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | int state, int do_lock) |
| 882 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 883 | struct snd_pcm_substream *s = NULL; |
| 884 | struct snd_pcm_substream *s1; |
Takashi Iwai | dde1c65 | 2014-10-21 15:32:13 +0200 | [diff] [blame] | 885 | int res = 0, depth = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 887 | snd_pcm_group_for_each_entry(s, substream) { |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 888 | if (do_lock && s != substream) { |
| 889 | if (s->pcm->nonatomic) |
Takashi Iwai | dde1c65 | 2014-10-21 15:32:13 +0200 | [diff] [blame] | 890 | mutex_lock_nested(&s->self_group.mutex, depth); |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 891 | else |
Takashi Iwai | dde1c65 | 2014-10-21 15:32:13 +0200 | [diff] [blame] | 892 | spin_lock_nested(&s->self_group.lock, depth); |
| 893 | depth++; |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | res = ops->pre_action(s, state); |
| 896 | if (res < 0) |
| 897 | goto _unlock; |
| 898 | } |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 899 | snd_pcm_group_for_each_entry(s, substream) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | res = ops->do_action(s, state); |
| 901 | if (res < 0) { |
| 902 | if (ops->undo_action) { |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 903 | snd_pcm_group_for_each_entry(s1, substream) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | if (s1 == s) /* failed stream */ |
| 905 | break; |
| 906 | ops->undo_action(s1, state); |
| 907 | } |
| 908 | } |
| 909 | s = NULL; /* unlock all */ |
| 910 | goto _unlock; |
| 911 | } |
| 912 | } |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 913 | snd_pcm_group_for_each_entry(s, substream) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | ops->post_action(s, state); |
| 915 | } |
| 916 | _unlock: |
| 917 | if (do_lock) { |
| 918 | /* unlock streams */ |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 919 | snd_pcm_group_for_each_entry(s1, substream) { |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 920 | if (s1 != substream) { |
Takashi Iwai | 811deed | 2014-10-13 23:14:46 +0200 | [diff] [blame] | 921 | if (s1->pcm->nonatomic) |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 922 | mutex_unlock(&s1->self_group.mutex); |
| 923 | else |
| 924 | spin_unlock(&s1->self_group.lock); |
| 925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | if (s1 == s) /* end */ |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | return res; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Note: call with stream lock |
| 935 | */ |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 936 | static int snd_pcm_action_single(const struct action_ops *ops, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 937 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | int state) |
| 939 | { |
| 940 | int res; |
| 941 | |
| 942 | res = ops->pre_action(substream, state); |
| 943 | if (res < 0) |
| 944 | return res; |
| 945 | res = ops->do_action(substream, state); |
| 946 | if (res == 0) |
| 947 | ops->post_action(substream, state); |
| 948 | else if (ops->undo_action) |
| 949 | ops->undo_action(substream, state); |
| 950 | return res; |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Note: call with stream lock |
| 955 | */ |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 956 | static int snd_pcm_action(const struct action_ops *ops, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 957 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | int state) |
| 959 | { |
| 960 | int res; |
| 961 | |
Takashi Iwai | aa8edd8 | 2014-10-31 15:19:36 +0100 | [diff] [blame] | 962 | if (!snd_pcm_stream_linked(substream)) |
| 963 | return snd_pcm_action_single(ops, substream, state); |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 964 | |
Takashi Iwai | aa8edd8 | 2014-10-31 15:19:36 +0100 | [diff] [blame] | 965 | if (substream->pcm->nonatomic) { |
| 966 | if (!mutex_trylock(&substream->group->mutex)) { |
| 967 | mutex_unlock(&substream->self_group.mutex); |
| 968 | mutex_lock(&substream->group->mutex); |
| 969 | mutex_lock(&substream->self_group.mutex); |
| 970 | } |
| 971 | res = snd_pcm_action_group(ops, substream, state, 1); |
| 972 | mutex_unlock(&substream->group->mutex); |
| 973 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (!spin_trylock(&substream->group->lock)) { |
| 975 | spin_unlock(&substream->self_group.lock); |
| 976 | spin_lock(&substream->group->lock); |
| 977 | spin_lock(&substream->self_group.lock); |
| 978 | } |
| 979 | res = snd_pcm_action_group(ops, substream, state, 1); |
| 980 | spin_unlock(&substream->group->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | return res; |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Note: don't use any locks before |
| 987 | */ |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 988 | static int snd_pcm_action_lock_irq(const struct action_ops *ops, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 989 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | int state) |
| 991 | { |
| 992 | int res; |
| 993 | |
Takashi Iwai | e3a4bd5 | 2014-10-31 14:45:04 +0100 | [diff] [blame] | 994 | snd_pcm_stream_lock_irq(substream); |
| 995 | res = snd_pcm_action(ops, substream, state); |
| 996 | snd_pcm_stream_unlock_irq(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return res; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | */ |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1002 | static int snd_pcm_action_nonatomic(const struct action_ops *ops, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1003 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | int state) |
| 1005 | { |
| 1006 | int res; |
| 1007 | |
| 1008 | down_read(&snd_pcm_link_rwsem); |
| 1009 | if (snd_pcm_stream_linked(substream)) |
| 1010 | res = snd_pcm_action_group(ops, substream, state, 0); |
| 1011 | else |
| 1012 | res = snd_pcm_action_single(ops, substream, state); |
| 1013 | up_read(&snd_pcm_link_rwsem); |
| 1014 | return res; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * start callbacks |
| 1019 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1020 | static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1022 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (runtime->status->state != SNDRV_PCM_STATE_PREPARED) |
| 1024 | return -EBADFD; |
| 1025 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 1026 | !snd_pcm_playback_data(substream)) |
| 1027 | return -EPIPE; |
Pierre-Louis Bossart | 2b79d7a | 2015-02-06 15:55:51 -0600 | [diff] [blame] | 1028 | runtime->trigger_tstamp_latched = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | runtime->trigger_master = substream; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1033 | static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
| 1035 | if (substream->runtime->trigger_master != substream) |
| 1036 | return 0; |
| 1037 | return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START); |
| 1038 | } |
| 1039 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1040 | static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
| 1042 | if (substream->runtime->trigger_master == substream) |
| 1043 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
| 1044 | } |
| 1045 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1046 | static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1048 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | snd_pcm_trigger_tstamp(substream); |
Takashi Iwai | 6af3fb7 | 2009-05-27 10:49:26 +0200 | [diff] [blame] | 1050 | runtime->hw_ptr_jiffies = jiffies; |
Jaroslav Kysela | bd76af0 | 2010-08-18 14:16:54 +0200 | [diff] [blame] | 1051 | runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / |
| 1052 | runtime->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | runtime->status->state = state; |
| 1054 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 1055 | runtime->silence_size > 0) |
| 1056 | snd_pcm_playback_silence(substream, ULONG_MAX); |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1057 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1060 | static const struct action_ops snd_pcm_action_start = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | .pre_action = snd_pcm_pre_start, |
| 1062 | .do_action = snd_pcm_do_start, |
| 1063 | .undo_action = snd_pcm_undo_start, |
| 1064 | .post_action = snd_pcm_post_start |
| 1065 | }; |
| 1066 | |
| 1067 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1068 | * snd_pcm_start - start all linked streams |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1069 | * @substream: the PCM substream instance |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1070 | * |
| 1071 | * Return: Zero if successful, or a negative error code. |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 1072 | * The stream lock must be acquired before calling this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1074 | int snd_pcm_start(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1076 | return snd_pcm_action(&snd_pcm_action_start, substream, |
| 1077 | SNDRV_PCM_STATE_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 1080 | /* take the stream lock and start the streams */ |
| 1081 | static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream) |
| 1082 | { |
| 1083 | return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, |
| 1084 | SNDRV_PCM_STATE_RUNNING); |
| 1085 | } |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | /* |
| 1088 | * stop callbacks |
| 1089 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1090 | static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1092 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 1094 | return -EBADFD; |
| 1095 | runtime->trigger_master = substream; |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1099 | static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
| 1101 | if (substream->runtime->trigger_master == substream && |
| 1102 | snd_pcm_running(substream)) |
| 1103 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
| 1104 | return 0; /* unconditonally stop all substreams */ |
| 1105 | } |
| 1106 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1107 | static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1109 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (runtime->status->state != state) { |
| 1111 | snd_pcm_trigger_tstamp(substream); |
Takashi Iwai | 9bc889b | 2014-11-06 12:15:25 +0100 | [diff] [blame] | 1112 | runtime->status->state = state; |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1113 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | } |
| 1115 | wake_up(&runtime->sleep); |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 1116 | wake_up(&runtime->tsleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1119 | static const struct action_ops snd_pcm_action_stop = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | .pre_action = snd_pcm_pre_stop, |
| 1121 | .do_action = snd_pcm_do_stop, |
| 1122 | .post_action = snd_pcm_post_stop |
| 1123 | }; |
| 1124 | |
| 1125 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1126 | * snd_pcm_stop - try to stop all running streams in the substream group |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1127 | * @substream: the PCM substream instance |
| 1128 | * @state: PCM state after stopping the stream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1130 | * The state of each stream is then changed to the given state unconditionally. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1131 | * |
Masanari Iida | 0a11458 | 2014-02-09 00:47:36 +0900 | [diff] [blame] | 1132 | * Return: Zero if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | */ |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 1134 | int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | { |
| 1136 | return snd_pcm_action(&snd_pcm_action_stop, substream, state); |
| 1137 | } |
| 1138 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1139 | EXPORT_SYMBOL(snd_pcm_stop); |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1142 | * snd_pcm_drain_done - stop the DMA only when the given stream is playback |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1143 | * @substream: the PCM substream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | * |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1145 | * After stopping, the state is changed to SETUP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | * Unlike snd_pcm_stop(), this affects only the given stream. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1147 | * |
| 1148 | * Return: Zero if succesful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1150 | int snd_pcm_drain_done(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1152 | return snd_pcm_action_single(&snd_pcm_action_stop, substream, |
| 1153 | SNDRV_PCM_STATE_SETUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Takashi Iwai | 1fb8510 | 2014-11-07 17:08:28 +0100 | [diff] [blame] | 1156 | /** |
| 1157 | * snd_pcm_stop_xrun - stop the running streams as XRUN |
| 1158 | * @substream: the PCM substream instance |
Takashi Iwai | 1fb8510 | 2014-11-07 17:08:28 +0100 | [diff] [blame] | 1159 | * |
| 1160 | * This stops the given running substream (and all linked substreams) as XRUN. |
| 1161 | * Unlike snd_pcm_stop(), this function takes the substream lock by itself. |
| 1162 | * |
| 1163 | * Return: Zero if successful, or a negative error code. |
| 1164 | */ |
| 1165 | int snd_pcm_stop_xrun(struct snd_pcm_substream *substream) |
| 1166 | { |
| 1167 | unsigned long flags; |
| 1168 | int ret = 0; |
| 1169 | |
| 1170 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1171 | if (snd_pcm_running(substream)) |
| 1172 | ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 1173 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun); |
| 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | /* |
| 1179 | * pause callbacks |
| 1180 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1181 | static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1183 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | if (!(runtime->info & SNDRV_PCM_INFO_PAUSE)) |
| 1185 | return -ENOSYS; |
| 1186 | if (push) { |
| 1187 | if (runtime->status->state != SNDRV_PCM_STATE_RUNNING) |
| 1188 | return -EBADFD; |
| 1189 | } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED) |
| 1190 | return -EBADFD; |
| 1191 | runtime->trigger_master = substream; |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1195 | static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | { |
| 1197 | if (substream->runtime->trigger_master != substream) |
| 1198 | return 0; |
Jaroslav Kysela | 56385a1 | 2010-08-18 14:08:17 +0200 | [diff] [blame] | 1199 | /* some drivers might use hw_ptr to recover from the pause - |
| 1200 | update the hw_ptr now */ |
| 1201 | if (push) |
| 1202 | snd_pcm_update_hw_ptr(substream); |
Takashi Iwai | 6af3fb7 | 2009-05-27 10:49:26 +0200 | [diff] [blame] | 1203 | /* The jiffies check in snd_pcm_update_hw_ptr*() is done by |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1204 | * a delta between the current jiffies, this gives a large enough |
Takashi Iwai | 6af3fb7 | 2009-05-27 10:49:26 +0200 | [diff] [blame] | 1205 | * delta, effectively to skip the check once. |
| 1206 | */ |
| 1207 | substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | return substream->ops->trigger(substream, |
| 1209 | push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH : |
| 1210 | SNDRV_PCM_TRIGGER_PAUSE_RELEASE); |
| 1211 | } |
| 1212 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1213 | static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
| 1215 | if (substream->runtime->trigger_master == substream) |
| 1216 | substream->ops->trigger(substream, |
| 1217 | push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE : |
| 1218 | SNDRV_PCM_TRIGGER_PAUSE_PUSH); |
| 1219 | } |
| 1220 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1221 | static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1223 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | snd_pcm_trigger_tstamp(substream); |
| 1225 | if (push) { |
| 1226 | runtime->status->state = SNDRV_PCM_STATE_PAUSED; |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1227 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | wake_up(&runtime->sleep); |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 1229 | wake_up(&runtime->tsleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } else { |
| 1231 | runtime->status->state = SNDRV_PCM_STATE_RUNNING; |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1232 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1236 | static const struct action_ops snd_pcm_action_pause = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | .pre_action = snd_pcm_pre_pause, |
| 1238 | .do_action = snd_pcm_do_pause, |
| 1239 | .undo_action = snd_pcm_undo_pause, |
| 1240 | .post_action = snd_pcm_post_pause |
| 1241 | }; |
| 1242 | |
| 1243 | /* |
| 1244 | * Push/release the pause for all linked streams. |
| 1245 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1246 | static int snd_pcm_pause(struct snd_pcm_substream *substream, int push) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | { |
| 1248 | return snd_pcm_action(&snd_pcm_action_pause, substream, push); |
| 1249 | } |
| 1250 | |
| 1251 | #ifdef CONFIG_PM |
| 1252 | /* suspend */ |
| 1253 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1254 | static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1256 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) |
| 1258 | return -EBUSY; |
| 1259 | runtime->trigger_master = substream; |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1263 | static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1265 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | if (runtime->trigger_master != substream) |
| 1267 | return 0; |
| 1268 | if (! snd_pcm_running(substream)) |
| 1269 | return 0; |
| 1270 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); |
| 1271 | return 0; /* suspend unconditionally */ |
| 1272 | } |
| 1273 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1274 | static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1276 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | snd_pcm_trigger_tstamp(substream); |
Takashi Iwai | 9bc889b | 2014-11-06 12:15:25 +0100 | [diff] [blame] | 1278 | runtime->status->suspended_state = runtime->status->state; |
| 1279 | runtime->status->state = SNDRV_PCM_STATE_SUSPENDED; |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1280 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | wake_up(&runtime->sleep); |
Jaroslav Kysela | c91a988 | 2010-01-21 10:32:15 +0100 | [diff] [blame] | 1282 | wake_up(&runtime->tsleep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1285 | static const struct action_ops snd_pcm_action_suspend = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | .pre_action = snd_pcm_pre_suspend, |
| 1287 | .do_action = snd_pcm_do_suspend, |
| 1288 | .post_action = snd_pcm_post_suspend |
| 1289 | }; |
| 1290 | |
| 1291 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1292 | * snd_pcm_suspend - trigger SUSPEND to all linked streams |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1293 | * @substream: the PCM substream |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | * After this call, all streams are changed to SUSPENDED state. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1296 | * |
| 1297 | * Return: Zero if successful (or @substream is %NULL), or a negative error |
| 1298 | * code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1300 | int snd_pcm_suspend(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | { |
| 1302 | int err; |
| 1303 | unsigned long flags; |
| 1304 | |
Takashi Iwai | 603bf52 | 2005-11-17 15:59:14 +0100 | [diff] [blame] | 1305 | if (! substream) |
| 1306 | return 0; |
| 1307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | snd_pcm_stream_lock_irqsave(substream, flags); |
| 1309 | err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0); |
| 1310 | snd_pcm_stream_unlock_irqrestore(substream, flags); |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1314 | EXPORT_SYMBOL(snd_pcm_suspend); |
| 1315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1317 | * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1318 | * @pcm: the PCM instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | * After this call, all streams are changed to SUSPENDED state. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1321 | * |
| 1322 | * Return: Zero if successful (or @pcm is %NULL), or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1324 | int snd_pcm_suspend_all(struct snd_pcm *pcm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1326 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | int stream, err = 0; |
| 1328 | |
Takashi Iwai | 603bf52 | 2005-11-17 15:59:14 +0100 | [diff] [blame] | 1329 | if (! pcm) |
| 1330 | return 0; |
| 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | for (stream = 0; stream < 2; stream++) { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1333 | for (substream = pcm->streams[stream].substream; |
| 1334 | substream; substream = substream->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | /* FIXME: the open/close code should lock this as well */ |
| 1336 | if (substream->runtime == NULL) |
| 1337 | continue; |
| 1338 | err = snd_pcm_suspend(substream); |
| 1339 | if (err < 0 && err != -EBUSY) |
| 1340 | return err; |
| 1341 | } |
| 1342 | } |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 1346 | EXPORT_SYMBOL(snd_pcm_suspend_all); |
| 1347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | /* resume */ |
| 1349 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1350 | static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1352 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) |
| 1354 | return -ENOSYS; |
| 1355 | runtime->trigger_master = substream; |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1359 | static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1361 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | if (runtime->trigger_master != substream) |
| 1363 | return 0; |
| 1364 | /* DMA not running previously? */ |
| 1365 | if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING && |
| 1366 | (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING || |
| 1367 | substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) |
| 1368 | return 0; |
| 1369 | return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); |
| 1370 | } |
| 1371 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1372 | static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | { |
| 1374 | if (substream->runtime->trigger_master == substream && |
| 1375 | snd_pcm_running(substream)) |
| 1376 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); |
| 1377 | } |
| 1378 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1379 | static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1381 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | snd_pcm_trigger_tstamp(substream); |
Takashi Iwai | 9bc889b | 2014-11-06 12:15:25 +0100 | [diff] [blame] | 1383 | runtime->status->state = runtime->status->suspended_state; |
Jie Yang | 90bbaf6 | 2015-10-16 17:57:46 +0800 | [diff] [blame] | 1384 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1387 | static const struct action_ops snd_pcm_action_resume = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | .pre_action = snd_pcm_pre_resume, |
| 1389 | .do_action = snd_pcm_do_resume, |
| 1390 | .undo_action = snd_pcm_undo_resume, |
| 1391 | .post_action = snd_pcm_post_resume |
| 1392 | }; |
| 1393 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1394 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1396 | struct snd_card *card = substream->pcm->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | int res; |
| 1398 | |
| 1399 | snd_power_lock(card); |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 1400 | if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0); |
| 1402 | snd_power_unlock(card); |
| 1403 | return res; |
| 1404 | } |
| 1405 | |
| 1406 | #else |
| 1407 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1408 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | { |
| 1410 | return -ENOSYS; |
| 1411 | } |
| 1412 | |
| 1413 | #endif /* CONFIG_PM */ |
| 1414 | |
| 1415 | /* |
| 1416 | * xrun ioctl |
| 1417 | * |
| 1418 | * Change the RUNNING stream(s) to XRUN state. |
| 1419 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1420 | static int snd_pcm_xrun(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1422 | struct snd_card *card = substream->pcm->card; |
| 1423 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | int result; |
| 1425 | |
| 1426 | snd_power_lock(card); |
| 1427 | if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 1428 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (result < 0) |
| 1430 | goto _unlock; |
| 1431 | } |
| 1432 | |
| 1433 | snd_pcm_stream_lock_irq(substream); |
| 1434 | switch (runtime->status->state) { |
| 1435 | case SNDRV_PCM_STATE_XRUN: |
| 1436 | result = 0; /* already there */ |
| 1437 | break; |
| 1438 | case SNDRV_PCM_STATE_RUNNING: |
| 1439 | result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 1440 | break; |
| 1441 | default: |
| 1442 | result = -EBADFD; |
| 1443 | } |
| 1444 | snd_pcm_stream_unlock_irq(substream); |
| 1445 | _unlock: |
| 1446 | snd_power_unlock(card); |
| 1447 | return result; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * reset ioctl |
| 1452 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1453 | static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1455 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | switch (runtime->status->state) { |
| 1457 | case SNDRV_PCM_STATE_RUNNING: |
| 1458 | case SNDRV_PCM_STATE_PREPARED: |
| 1459 | case SNDRV_PCM_STATE_PAUSED: |
| 1460 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1461 | return 0; |
| 1462 | default: |
| 1463 | return -EBADFD; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1467 | static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1469 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); |
| 1471 | if (err < 0) |
| 1472 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | runtime->hw_ptr_base = 0; |
Jaroslav Kysela | e763692 | 2010-01-26 17:08:24 +0100 | [diff] [blame] | 1474 | runtime->hw_ptr_interrupt = runtime->status->hw_ptr - |
| 1475 | runtime->status->hw_ptr % runtime->period_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | runtime->silence_start = runtime->status->hw_ptr; |
| 1477 | runtime->silence_filled = 0; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1481 | static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1483 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
| 1485 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 1486 | runtime->silence_size > 0) |
| 1487 | snd_pcm_playback_silence(substream, ULONG_MAX); |
| 1488 | } |
| 1489 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1490 | static const struct action_ops snd_pcm_action_reset = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | .pre_action = snd_pcm_pre_reset, |
| 1492 | .do_action = snd_pcm_do_reset, |
| 1493 | .post_action = snd_pcm_post_reset |
| 1494 | }; |
| 1495 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1496 | static int snd_pcm_reset(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | { |
| 1498 | return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0); |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * prepare ioctl |
| 1503 | */ |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1504 | /* we use the second argument for updating f_flags */ |
| 1505 | static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, |
| 1506 | int f_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1508 | struct snd_pcm_runtime *runtime = substream->runtime; |
Takashi Iwai | de1b8b9 | 2006-11-08 15:41:29 +0100 | [diff] [blame] | 1509 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN || |
| 1510 | runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return -EBADFD; |
| 1512 | if (snd_pcm_running(substream)) |
| 1513 | return -EBUSY; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1514 | substream->f_flags = f_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | return 0; |
| 1516 | } |
| 1517 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1518 | static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | { |
| 1520 | int err; |
| 1521 | err = substream->ops->prepare(substream); |
| 1522 | if (err < 0) |
| 1523 | return err; |
| 1524 | return snd_pcm_do_reset(substream, 0); |
| 1525 | } |
| 1526 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1527 | static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1529 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
Takashi Iwai | 9b0573c | 2012-10-12 15:07:34 +0200 | [diff] [blame] | 1531 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1534 | static const struct action_ops snd_pcm_action_prepare = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | .pre_action = snd_pcm_pre_prepare, |
| 1536 | .do_action = snd_pcm_do_prepare, |
| 1537 | .post_action = snd_pcm_post_prepare |
| 1538 | }; |
| 1539 | |
| 1540 | /** |
Randy Dunlap | 1c85cc6 | 2008-10-15 14:38:40 -0700 | [diff] [blame] | 1541 | * snd_pcm_prepare - prepare the PCM substream to be triggerable |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1542 | * @substream: the PCM substream instance |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1543 | * @file: file to refer f_flags |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1544 | * |
| 1545 | * Return: Zero if successful, or a negative error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | */ |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1547 | static int snd_pcm_prepare(struct snd_pcm_substream *substream, |
| 1548 | struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | { |
| 1550 | int res; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1551 | struct snd_card *card = substream->pcm->card; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1552 | int f_flags; |
| 1553 | |
| 1554 | if (file) |
| 1555 | f_flags = file->f_flags; |
| 1556 | else |
| 1557 | f_flags = substream->f_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | snd_power_lock(card); |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 1560 | if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0) |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 1561 | res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, |
| 1562 | substream, f_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | snd_power_unlock(card); |
| 1564 | return res; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * drain ioctl |
| 1569 | */ |
| 1570 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1571 | static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | { |
Takashi Iwai | 4f7c39d | 2012-05-21 11:59:57 +0200 | [diff] [blame] | 1573 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1574 | switch (runtime->status->state) { |
| 1575 | case SNDRV_PCM_STATE_OPEN: |
| 1576 | case SNDRV_PCM_STATE_DISCONNECTED: |
| 1577 | case SNDRV_PCM_STATE_SUSPENDED: |
| 1578 | return -EBADFD; |
| 1579 | } |
| 1580 | runtime->trigger_master = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
| 1583 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1584 | static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1586 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1588 | switch (runtime->status->state) { |
| 1589 | case SNDRV_PCM_STATE_PREPARED: |
| 1590 | /* start playback stream if possible */ |
| 1591 | if (! snd_pcm_playback_empty(substream)) { |
| 1592 | snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); |
| 1593 | snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); |
Takashi Iwai | 70372a7 | 2014-12-18 10:02:41 +0100 | [diff] [blame] | 1594 | } else { |
| 1595 | runtime->status->state = SNDRV_PCM_STATE_SETUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | case SNDRV_PCM_STATE_RUNNING: |
| 1599 | runtime->status->state = SNDRV_PCM_STATE_DRAINING; |
| 1600 | break; |
Takashi Iwai | 4f7c39d | 2012-05-21 11:59:57 +0200 | [diff] [blame] | 1601 | case SNDRV_PCM_STATE_XRUN: |
| 1602 | runtime->status->state = SNDRV_PCM_STATE_SETUP; |
| 1603 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | default: |
| 1605 | break; |
| 1606 | } |
| 1607 | } else { |
| 1608 | /* stop running stream */ |
| 1609 | if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) { |
Marcin Ślusarz | b05e578 | 2007-12-14 12:50:16 +0100 | [diff] [blame] | 1610 | int new_state = snd_pcm_capture_avail(runtime) > 0 ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; |
Marcin Ślusarz | b05e578 | 2007-12-14 12:50:16 +0100 | [diff] [blame] | 1612 | snd_pcm_do_stop(substream, new_state); |
| 1613 | snd_pcm_post_stop(substream, new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | } |
| 1615 | } |
Libin Yang | 48d8829 | 2014-12-31 22:09:54 +0800 | [diff] [blame] | 1616 | |
| 1617 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && |
| 1618 | runtime->trigger_master == substream && |
| 1619 | (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER)) |
| 1620 | return substream->ops->trigger(substream, |
| 1621 | SNDRV_PCM_TRIGGER_DRAIN); |
| 1622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | return 0; |
| 1624 | } |
| 1625 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1626 | static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | { |
| 1628 | } |
| 1629 | |
Julia Lawall | b17154c | 2015-11-29 16:36:40 +0100 | [diff] [blame] | 1630 | static const struct action_ops snd_pcm_action_drain_init = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | .pre_action = snd_pcm_pre_drain_init, |
| 1632 | .do_action = snd_pcm_do_drain_init, |
| 1633 | .post_action = snd_pcm_post_drain_init |
| 1634 | }; |
| 1635 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1636 | static int snd_pcm_drop(struct snd_pcm_substream *substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * Drain the stream(s). |
| 1640 | * When the substream is linked, sync until the draining of all playback streams |
| 1641 | * is finished. |
| 1642 | * After this call, all streams are supposed to be either SETUP or DRAINING |
| 1643 | * (capture only) state. |
| 1644 | */ |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 1645 | static int snd_pcm_drain(struct snd_pcm_substream *substream, |
| 1646 | struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1648 | struct snd_card *card; |
| 1649 | struct snd_pcm_runtime *runtime; |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 1650 | struct snd_pcm_substream *s; |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1651 | wait_queue_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | int result = 0; |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 1653 | int nonblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | card = substream->pcm->card; |
| 1656 | runtime = substream->runtime; |
| 1657 | |
| 1658 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 1659 | return -EBADFD; |
| 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | snd_power_lock(card); |
| 1662 | if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) { |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 1663 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1664 | if (result < 0) { |
| 1665 | snd_power_unlock(card); |
| 1666 | return result; |
| 1667 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 1670 | if (file) { |
| 1671 | if (file->f_flags & O_NONBLOCK) |
| 1672 | nonblock = 1; |
| 1673 | } else if (substream->f_flags & O_NONBLOCK) |
| 1674 | nonblock = 1; |
| 1675 | |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1676 | down_read(&snd_pcm_link_rwsem); |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1677 | snd_pcm_stream_lock_irq(substream); |
| 1678 | /* resume pause */ |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1679 | if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1680 | snd_pcm_pause(substream, 0); |
| 1681 | |
| 1682 | /* pre-start/stop - all running streams are changed to DRAINING state */ |
| 1683 | result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0); |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 1684 | if (result < 0) |
| 1685 | goto unlock; |
| 1686 | /* in non-blocking, we don't wait in ioctl but let caller poll */ |
| 1687 | if (nonblock) { |
| 1688 | result = -EAGAIN; |
| 1689 | goto unlock; |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
| 1692 | for (;;) { |
| 1693 | long tout; |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1694 | struct snd_pcm_runtime *to_check; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | if (signal_pending(current)) { |
| 1696 | result = -ERESTARTSYS; |
| 1697 | break; |
| 1698 | } |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1699 | /* find a substream to drain */ |
| 1700 | to_check = NULL; |
| 1701 | snd_pcm_group_for_each_entry(s, substream) { |
| 1702 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 1703 | continue; |
| 1704 | runtime = s->runtime; |
| 1705 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) { |
| 1706 | to_check = runtime; |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1707 | break; |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1708 | } |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1709 | } |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1710 | if (!to_check) |
| 1711 | break; /* all drained */ |
| 1712 | init_waitqueue_entry(&wait, current); |
| 1713 | add_wait_queue(&to_check->sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1715 | up_read(&snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | snd_power_unlock(card); |
Takashi Iwai | f2b3614 | 2011-05-26 08:09:38 +0200 | [diff] [blame] | 1717 | if (runtime->no_period_wakeup) |
| 1718 | tout = MAX_SCHEDULE_TIMEOUT; |
| 1719 | else { |
| 1720 | tout = 10; |
| 1721 | if (runtime->rate) { |
| 1722 | long t = runtime->period_size * 2 / runtime->rate; |
| 1723 | tout = max(t, tout); |
| 1724 | } |
| 1725 | tout = msecs_to_jiffies(tout * 1000); |
| 1726 | } |
| 1727 | tout = schedule_timeout_interruptible(tout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | snd_power_lock(card); |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1729 | down_read(&snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1731 | remove_wait_queue(&to_check->sleep, &wait); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1732 | if (card->shutdown) { |
| 1733 | result = -ENODEV; |
| 1734 | break; |
| 1735 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | if (tout == 0) { |
| 1737 | if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) |
| 1738 | result = -ESTRPIPE; |
| 1739 | else { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 1740 | dev_dbg(substream->pcm->card->dev, |
| 1741 | "playback drain error (DMA or IRQ trouble?)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| 1743 | result = -EIO; |
| 1744 | } |
| 1745 | break; |
| 1746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1748 | |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 1749 | unlock: |
Takashi Iwai | 21cb2a2 | 2005-05-31 14:35:31 +0200 | [diff] [blame] | 1750 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | d3a7dcf | 2009-09-17 18:46:26 +0200 | [diff] [blame] | 1751 | up_read(&snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | snd_power_unlock(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
| 1754 | return result; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * drop ioctl |
| 1759 | * |
| 1760 | * Immediately put all linked substreams into SETUP state. |
| 1761 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1762 | static int snd_pcm_drop(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1764 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | int result = 0; |
| 1766 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1767 | if (PCM_RUNTIME_CHECK(substream)) |
| 1768 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
Takashi Iwai | de1b8b9 | 2006-11-08 15:41:29 +0100 | [diff] [blame] | 1771 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN || |
Takashi Iwai | 24e8fc4 | 2008-09-25 17:51:11 +0200 | [diff] [blame] | 1772 | runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED || |
| 1773 | runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | return -EBADFD; |
| 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | snd_pcm_stream_lock_irq(substream); |
| 1777 | /* resume pause */ |
| 1778 | if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) |
| 1779 | snd_pcm_pause(substream, 0); |
| 1780 | |
| 1781 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| 1782 | /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ |
| 1783 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | 24e8fc4 | 2008-09-25 17:51:11 +0200 | [diff] [blame] | 1784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | return result; |
| 1786 | } |
| 1787 | |
| 1788 | |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1789 | static bool is_pcm_file(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | { |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1791 | struct inode *inode = file_inode(file); |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1792 | unsigned int minor; |
| 1793 | |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1794 | if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major) |
| 1795 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | minor = iminor(inode); |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1797 | return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) || |
| 1798 | snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * PCM link handling |
| 1803 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1804 | static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | { |
| 1806 | int res = 0; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1807 | struct snd_pcm_file *pcm_file; |
| 1808 | struct snd_pcm_substream *substream1; |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1809 | struct snd_pcm_group *group; |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1810 | struct fd f = fdget(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1812 | if (!f.file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return -EBADFD; |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1814 | if (!is_pcm_file(f.file)) { |
| 1815 | res = -EBADFD; |
| 1816 | goto _badf; |
| 1817 | } |
| 1818 | pcm_file = f.file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | substream1 = pcm_file->substream; |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1820 | group = kmalloc(sizeof(*group), GFP_KERNEL); |
| 1821 | if (!group) { |
| 1822 | res = -ENOMEM; |
| 1823 | goto _nolock; |
| 1824 | } |
Takashi Iwai | 67ec1072 | 2016-02-17 14:30:26 +0100 | [diff] [blame] | 1825 | down_write_nonblock(&snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | write_lock_irq(&snd_pcm_link_rwlock); |
| 1827 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 1828 | substream->runtime->status->state != substream1->runtime->status->state || |
| 1829 | substream->pcm->nonatomic != substream1->pcm->nonatomic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | res = -EBADFD; |
| 1831 | goto _end; |
| 1832 | } |
| 1833 | if (snd_pcm_stream_linked(substream1)) { |
| 1834 | res = -EALREADY; |
| 1835 | goto _end; |
| 1836 | } |
| 1837 | if (!snd_pcm_stream_linked(substream)) { |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1838 | substream->group = group; |
Al Viro | dd6c5cd | 2013-06-05 14:07:08 -0400 | [diff] [blame] | 1839 | group = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | spin_lock_init(&substream->group->lock); |
Takashi Iwai | 257f8cc | 2014-08-29 15:32:29 +0200 | [diff] [blame] | 1841 | mutex_init(&substream->group->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | INIT_LIST_HEAD(&substream->group->substreams); |
| 1843 | list_add_tail(&substream->link_list, &substream->group->substreams); |
| 1844 | substream->group->count = 1; |
| 1845 | } |
| 1846 | list_add_tail(&substream1->link_list, &substream->group->substreams); |
| 1847 | substream->group->count++; |
| 1848 | substream1->group = substream->group; |
| 1849 | _end: |
| 1850 | write_unlock_irq(&snd_pcm_link_rwlock); |
| 1851 | up_write(&snd_pcm_link_rwsem); |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1852 | _nolock: |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 1853 | snd_card_unref(substream1->pcm->card); |
Al Viro | dd6c5cd | 2013-06-05 14:07:08 -0400 | [diff] [blame] | 1854 | kfree(group); |
Al Viro | 0888c32 | 2013-06-05 14:09:55 -0400 | [diff] [blame] | 1855 | _badf: |
| 1856 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | return res; |
| 1858 | } |
| 1859 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1860 | static void relink_to_local(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | { |
| 1862 | substream->group = &substream->self_group; |
| 1863 | INIT_LIST_HEAD(&substream->self_group.substreams); |
| 1864 | list_add_tail(&substream->link_list, &substream->self_group.substreams); |
| 1865 | } |
| 1866 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1867 | static int snd_pcm_unlink(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 1869 | struct snd_pcm_substream *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | int res = 0; |
| 1871 | |
Takashi Iwai | 67ec1072 | 2016-02-17 14:30:26 +0100 | [diff] [blame] | 1872 | down_write_nonblock(&snd_pcm_link_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | write_lock_irq(&snd_pcm_link_rwlock); |
| 1874 | if (!snd_pcm_stream_linked(substream)) { |
| 1875 | res = -EALREADY; |
| 1876 | goto _end; |
| 1877 | } |
| 1878 | list_del(&substream->link_list); |
| 1879 | substream->group->count--; |
| 1880 | if (substream->group->count == 1) { /* detach the last stream, too */ |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 1881 | snd_pcm_group_for_each_entry(s, substream) { |
| 1882 | relink_to_local(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | break; |
| 1884 | } |
| 1885 | kfree(substream->group); |
| 1886 | } |
| 1887 | relink_to_local(substream); |
| 1888 | _end: |
| 1889 | write_unlock_irq(&snd_pcm_link_rwlock); |
| 1890 | up_write(&snd_pcm_link_rwsem); |
| 1891 | return res; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * hw configurator |
| 1896 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1897 | static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, |
| 1898 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1900 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), |
| 1902 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1903 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1904 | } |
| 1905 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1906 | static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, |
| 1907 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1909 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | snd_interval_div(hw_param_interval_c(params, rule->deps[0]), |
| 1911 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1912 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1913 | } |
| 1914 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1915 | static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, |
| 1916 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1918 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), |
| 1920 | hw_param_interval_c(params, rule->deps[1]), |
| 1921 | (unsigned long) rule->private, &t); |
| 1922 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1923 | } |
| 1924 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1925 | static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, |
| 1926 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1928 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), |
| 1930 | (unsigned long) rule->private, |
| 1931 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1932 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1933 | } |
| 1934 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1935 | static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, |
| 1936 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | { |
| 1938 | unsigned int k; |
Takashi Sakamoto | b55f9fd | 2017-05-17 08:48:20 +0900 | [diff] [blame] | 1939 | const struct snd_interval *i = |
| 1940 | hw_param_interval_c(params, rule->deps[0]); |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1941 | struct snd_mask m; |
| 1942 | struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | snd_mask_any(&m); |
| 1944 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { |
| 1945 | int bits; |
| 1946 | if (! snd_mask_test(mask, k)) |
| 1947 | continue; |
| 1948 | bits = snd_pcm_format_physical_width(k); |
| 1949 | if (bits <= 0) |
| 1950 | continue; /* ignore invalid formats */ |
| 1951 | if ((unsigned)bits < i->min || (unsigned)bits > i->max) |
| 1952 | snd_mask_reset(&m, k); |
| 1953 | } |
| 1954 | return snd_mask_refine(mask, &m); |
| 1955 | } |
| 1956 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1957 | static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, |
| 1958 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1960 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | unsigned int k; |
| 1962 | t.min = UINT_MAX; |
| 1963 | t.max = 0; |
| 1964 | t.openmin = 0; |
| 1965 | t.openmax = 0; |
| 1966 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { |
| 1967 | int bits; |
| 1968 | if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) |
| 1969 | continue; |
| 1970 | bits = snd_pcm_format_physical_width(k); |
| 1971 | if (bits <= 0) |
| 1972 | continue; /* ignore invalid formats */ |
| 1973 | if (t.min > (unsigned)bits) |
| 1974 | t.min = bits; |
| 1975 | if (t.max < (unsigned)bits) |
| 1976 | t.max = bits; |
| 1977 | } |
| 1978 | t.integer = 1; |
| 1979 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1980 | } |
| 1981 | |
| 1982 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 |
| 1983 | #error "Change this table" |
| 1984 | #endif |
| 1985 | |
Takashi Sakamoto | 8b67430 | 2017-05-17 08:48:17 +0900 | [diff] [blame] | 1986 | static const unsigned int rates[] = { |
| 1987 | 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
| 1988 | 48000, 64000, 88200, 96000, 176400, 192000 |
| 1989 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
Clemens Ladisch | 7653d55 | 2007-08-13 17:38:54 +0200 | [diff] [blame] | 1991 | const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { |
| 1992 | .count = ARRAY_SIZE(rates), |
| 1993 | .list = rates, |
| 1994 | }; |
| 1995 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1996 | static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1997 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1999 | struct snd_pcm_hardware *hw = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | return snd_interval_list(hw_param_interval(params, rule->var), |
Clemens Ladisch | 7653d55 | 2007-08-13 17:38:54 +0200 | [diff] [blame] | 2001 | snd_pcm_known_rates.count, |
| 2002 | snd_pcm_known_rates.list, hw->rates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2005 | static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, |
| 2006 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2008 | struct snd_interval t; |
| 2009 | struct snd_pcm_substream *substream = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | t.min = 0; |
| 2011 | t.max = substream->buffer_bytes_max; |
| 2012 | t.openmin = 0; |
| 2013 | t.openmax = 0; |
| 2014 | t.integer = 1; |
| 2015 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 2016 | } |
| 2017 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2018 | int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2020 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2021 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | int k, err; |
| 2023 | |
| 2024 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { |
| 2025 | snd_mask_any(constrs_mask(constrs, k)); |
| 2026 | } |
| 2027 | |
| 2028 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { |
| 2029 | snd_interval_any(constrs_interval(constrs, k)); |
| 2030 | } |
| 2031 | |
| 2032 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); |
| 2033 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); |
| 2034 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); |
| 2035 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); |
| 2036 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); |
| 2037 | |
| 2038 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 2039 | snd_pcm_hw_rule_format, NULL, |
| 2040 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 2041 | if (err < 0) |
| 2042 | return err; |
| 2043 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 2044 | snd_pcm_hw_rule_sample_bits, NULL, |
| 2045 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 2046 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 2047 | if (err < 0) |
| 2048 | return err; |
| 2049 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 2050 | snd_pcm_hw_rule_div, NULL, |
| 2051 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 2052 | if (err < 0) |
| 2053 | return err; |
| 2054 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 2055 | snd_pcm_hw_rule_mul, NULL, |
| 2056 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 2057 | if (err < 0) |
| 2058 | return err; |
| 2059 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 2060 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 2061 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); |
| 2062 | if (err < 0) |
| 2063 | return err; |
| 2064 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 2065 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 2066 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); |
| 2067 | if (err < 0) |
| 2068 | return err; |
| 2069 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 2070 | snd_pcm_hw_rule_div, NULL, |
| 2071 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 2072 | if (err < 0) |
| 2073 | return err; |
| 2074 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 2075 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 2076 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); |
| 2077 | if (err < 0) |
| 2078 | return err; |
| 2079 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 2080 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 2081 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); |
| 2082 | if (err < 0) |
| 2083 | return err; |
| 2084 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, |
| 2085 | snd_pcm_hw_rule_div, NULL, |
| 2086 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); |
| 2087 | if (err < 0) |
| 2088 | return err; |
| 2089 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 2090 | snd_pcm_hw_rule_div, NULL, |
| 2091 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); |
| 2092 | if (err < 0) |
| 2093 | return err; |
| 2094 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 2095 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 2096 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 2097 | if (err < 0) |
| 2098 | return err; |
| 2099 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 2100 | snd_pcm_hw_rule_muldivk, (void*) 1000000, |
| 2101 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2102 | if (err < 0) |
| 2103 | return err; |
| 2104 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 2105 | snd_pcm_hw_rule_mul, NULL, |
| 2106 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); |
| 2107 | if (err < 0) |
| 2108 | return err; |
| 2109 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 2110 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 2111 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 2112 | if (err < 0) |
| 2113 | return err; |
| 2114 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 2115 | snd_pcm_hw_rule_muldivk, (void*) 1000000, |
| 2116 | SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2117 | if (err < 0) |
| 2118 | return err; |
| 2119 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 2120 | snd_pcm_hw_rule_muldivk, (void*) 8, |
| 2121 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 2122 | if (err < 0) |
| 2123 | return err; |
| 2124 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 2125 | snd_pcm_hw_rule_muldivk, (void*) 8, |
| 2126 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 2127 | if (err < 0) |
| 2128 | return err; |
| 2129 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 2130 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 2131 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2132 | if (err < 0) |
| 2133 | return err; |
| 2134 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, |
| 2135 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 2136 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2137 | if (err < 0) |
| 2138 | return err; |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2142 | int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2144 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2145 | struct snd_pcm_hardware *hw = &runtime->hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | int err; |
| 2147 | unsigned int mask = 0; |
| 2148 | |
| 2149 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
| 2150 | mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED; |
| 2151 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
| 2152 | mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED; |
Takashi Iwai | 63825f3 | 2014-10-22 12:04:46 +0200 | [diff] [blame] | 2153 | if (hw_support_mmap(substream)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
| 2155 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; |
| 2156 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
| 2157 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; |
| 2158 | if (hw->info & SNDRV_PCM_INFO_COMPLEX) |
| 2159 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX; |
| 2160 | } |
| 2161 | err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2162 | if (err < 0) |
| 2163 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
| 2165 | err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2166 | if (err < 0) |
| 2167 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | |
| 2169 | err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2170 | if (err < 0) |
| 2171 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | |
| 2173 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 2174 | hw->channels_min, hw->channels_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2175 | if (err < 0) |
| 2176 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
| 2178 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, |
| 2179 | hw->rate_min, hw->rate_max); |
Guennadi Liakhovetski | 8b90ca0 | 2009-12-24 01:17:46 +0100 | [diff] [blame] | 2180 | if (err < 0) |
| 2181 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
| 2183 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 2184 | hw->period_bytes_min, hw->period_bytes_max); |
Guennadi Liakhovetski | 8b90ca0 | 2009-12-24 01:17:46 +0100 | [diff] [blame] | 2185 | if (err < 0) |
| 2186 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | |
| 2188 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, |
| 2189 | hw->periods_min, hw->periods_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2190 | if (err < 0) |
| 2191 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
| 2193 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 2194 | hw->period_bytes_min, hw->buffer_bytes_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2195 | if (err < 0) |
| 2196 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
| 2198 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 2199 | snd_pcm_hw_rule_buffer_bytes_max, substream, |
| 2200 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); |
| 2201 | if (err < 0) |
| 2202 | return err; |
| 2203 | |
| 2204 | /* FIXME: remove */ |
| 2205 | if (runtime->dma_bytes) { |
| 2206 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2207 | if (err < 0) |
Sachin Kamat | 6cf9515 | 2012-11-21 14:36:55 +0530 | [diff] [blame] | 2208 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { |
| 2212 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 2213 | snd_pcm_hw_rule_rate, hw, |
| 2214 | SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2215 | if (err < 0) |
| 2216 | return err; |
| 2217 | } |
| 2218 | |
| 2219 | /* FIXME: this belong to lowlevel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 2221 | |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2225 | static void pcm_release_private(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | snd_pcm_unlink(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | void snd_pcm_release_substream(struct snd_pcm_substream *substream) |
| 2231 | { |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2232 | substream->ref_count--; |
| 2233 | if (substream->ref_count > 0) |
| 2234 | return; |
| 2235 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2236 | snd_pcm_drop(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2237 | if (substream->hw_opened) { |
Takashi Iwai | 094435d | 2015-09-29 12:57:42 +0200 | [diff] [blame] | 2238 | if (substream->ops->hw_free && |
| 2239 | substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | substream->ops->hw_free(substream); |
| 2241 | substream->ops->close(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2242 | substream->hw_opened = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
Takashi Iwai | 8699a0b | 2010-09-16 22:52:32 +0200 | [diff] [blame] | 2244 | if (pm_qos_request_active(&substream->latency_pm_qos_req)) |
| 2245 | pm_qos_remove_request(&substream->latency_pm_qos_req); |
Takashi Iwai | 1576274 | 2006-04-06 19:47:42 +0200 | [diff] [blame] | 2246 | if (substream->pcm_release) { |
| 2247 | substream->pcm_release(substream); |
| 2248 | substream->pcm_release = NULL; |
| 2249 | } |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2250 | snd_pcm_detach_substream(substream); |
| 2251 | } |
| 2252 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2253 | EXPORT_SYMBOL(snd_pcm_release_substream); |
| 2254 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2255 | int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, |
| 2256 | struct file *file, |
| 2257 | struct snd_pcm_substream **rsubstream) |
| 2258 | { |
| 2259 | struct snd_pcm_substream *substream; |
| 2260 | int err; |
| 2261 | |
| 2262 | err = snd_pcm_attach_substream(pcm, stream, file, &substream); |
| 2263 | if (err < 0) |
| 2264 | return err; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2265 | if (substream->ref_count > 1) { |
| 2266 | *rsubstream = substream; |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2270 | err = snd_pcm_hw_constraints_init(substream); |
| 2271 | if (err < 0) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 2272 | pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2273 | goto error; |
| 2274 | } |
| 2275 | |
| 2276 | if ((err = substream->ops->open(substream)) < 0) |
| 2277 | goto error; |
| 2278 | |
| 2279 | substream->hw_opened = 1; |
| 2280 | |
| 2281 | err = snd_pcm_hw_constraints_complete(substream); |
| 2282 | if (err < 0) { |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 2283 | pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2284 | goto error; |
| 2285 | } |
| 2286 | |
| 2287 | *rsubstream = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | return 0; |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2289 | |
| 2290 | error: |
| 2291 | snd_pcm_release_substream(substream); |
| 2292 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2295 | EXPORT_SYMBOL(snd_pcm_open_substream); |
| 2296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | static int snd_pcm_open_file(struct file *file, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2298 | struct snd_pcm *pcm, |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2299 | int stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2301 | struct snd_pcm_file *pcm_file; |
| 2302 | struct snd_pcm_substream *substream; |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2303 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2305 | err = snd_pcm_open_substream(pcm, stream, file, &substream); |
| 2306 | if (err < 0) |
| 2307 | return err; |
| 2308 | |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 2309 | pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); |
| 2310 | if (pcm_file == NULL) { |
| 2311 | snd_pcm_release_substream(substream); |
| 2312 | return -ENOMEM; |
| 2313 | } |
| 2314 | pcm_file->substream = substream; |
| 2315 | if (substream->ref_count == 1) { |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2316 | substream->file = pcm_file; |
| 2317 | substream->pcm_release = pcm_release_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | file->private_data = pcm_file; |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | return 0; |
| 2322 | } |
| 2323 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2324 | static int snd_pcm_playback_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2326 | struct snd_pcm *pcm; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2327 | int err = nonseekable_open(inode, file); |
| 2328 | if (err < 0) |
| 2329 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2330 | pcm = snd_lookup_minor_data(iminor(inode), |
| 2331 | SNDRV_DEVICE_TYPE_PCM_PLAYBACK); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 2332 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); |
Takashi Iwai | 8bb4d9c | 2012-11-08 14:36:18 +0100 | [diff] [blame] | 2333 | if (pcm) |
| 2334 | snd_card_unref(pcm->card); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 2335 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | static int snd_pcm_capture_open(struct inode *inode, struct file *file) |
| 2339 | { |
| 2340 | struct snd_pcm *pcm; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2341 | int err = nonseekable_open(inode, file); |
| 2342 | if (err < 0) |
| 2343 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2344 | pcm = snd_lookup_minor_data(iminor(inode), |
| 2345 | SNDRV_DEVICE_TYPE_PCM_CAPTURE); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 2346 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); |
Takashi Iwai | 8bb4d9c | 2012-11-08 14:36:18 +0100 | [diff] [blame] | 2347 | if (pcm) |
| 2348 | snd_card_unref(pcm->card); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 2349 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) |
| 2353 | { |
| 2354 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | wait_queue_t wait; |
| 2356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | if (pcm == NULL) { |
| 2358 | err = -ENODEV; |
| 2359 | goto __error1; |
| 2360 | } |
| 2361 | err = snd_card_file_add(pcm->card, file); |
| 2362 | if (err < 0) |
| 2363 | goto __error1; |
| 2364 | if (!try_module_get(pcm->card->module)) { |
| 2365 | err = -EFAULT; |
| 2366 | goto __error2; |
| 2367 | } |
| 2368 | init_waitqueue_entry(&wait, current); |
| 2369 | add_wait_queue(&pcm->open_wait, &wait); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2370 | mutex_lock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | while (1) { |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2372 | err = snd_pcm_open_file(file, pcm, stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if (err >= 0) |
| 2374 | break; |
| 2375 | if (err == -EAGAIN) { |
| 2376 | if (file->f_flags & O_NONBLOCK) { |
| 2377 | err = -EBUSY; |
| 2378 | break; |
| 2379 | } |
| 2380 | } else |
| 2381 | break; |
| 2382 | set_current_state(TASK_INTERRUPTIBLE); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2383 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | schedule(); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2385 | mutex_lock(&pcm->open_mutex); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 2386 | if (pcm->card->shutdown) { |
| 2387 | err = -ENODEV; |
| 2388 | break; |
| 2389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | if (signal_pending(current)) { |
| 2391 | err = -ERESTARTSYS; |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | remove_wait_queue(&pcm->open_wait, &wait); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2396 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | if (err < 0) |
| 2398 | goto __error; |
| 2399 | return err; |
| 2400 | |
| 2401 | __error: |
| 2402 | module_put(pcm->card->module); |
| 2403 | __error2: |
| 2404 | snd_card_file_remove(pcm->card, file); |
| 2405 | __error1: |
| 2406 | return err; |
| 2407 | } |
| 2408 | |
| 2409 | static int snd_pcm_release(struct inode *inode, struct file *file) |
| 2410 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2411 | struct snd_pcm *pcm; |
| 2412 | struct snd_pcm_substream *substream; |
| 2413 | struct snd_pcm_file *pcm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | |
| 2415 | pcm_file = file->private_data; |
| 2416 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2417 | if (snd_BUG_ON(!substream)) |
| 2418 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | pcm = substream->pcm; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2420 | mutex_lock(&pcm->open_mutex); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2421 | snd_pcm_release_substream(substream); |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 2422 | kfree(pcm_file); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2423 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | wake_up(&pcm->open_wait); |
| 2425 | module_put(pcm->card->module); |
| 2426 | snd_card_file_remove(pcm->card, file); |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2430 | /* check and update PCM state; return 0 or a negative error |
| 2431 | * call this inside PCM lock |
| 2432 | */ |
| 2433 | static int do_pcm_hwsync(struct snd_pcm_substream *substream) |
| 2434 | { |
| 2435 | switch (substream->runtime->status->state) { |
| 2436 | case SNDRV_PCM_STATE_DRAINING: |
| 2437 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 2438 | return -EBADFD; |
| 2439 | /* Fall through */ |
| 2440 | case SNDRV_PCM_STATE_RUNNING: |
| 2441 | return snd_pcm_update_hw_ptr(substream); |
| 2442 | case SNDRV_PCM_STATE_PREPARED: |
| 2443 | case SNDRV_PCM_STATE_PAUSED: |
| 2444 | return 0; |
| 2445 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2446 | return -ESTRPIPE; |
| 2447 | case SNDRV_PCM_STATE_XRUN: |
| 2448 | return -EPIPE; |
| 2449 | default: |
| 2450 | return -EBADFD; |
| 2451 | } |
| 2452 | } |
| 2453 | |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2454 | /* update to the given appl_ptr and call ack callback if needed; |
| 2455 | * when an error is returned, take back to the original value |
| 2456 | */ |
| 2457 | static int apply_appl_ptr(struct snd_pcm_substream *substream, |
| 2458 | snd_pcm_uframes_t appl_ptr) |
| 2459 | { |
| 2460 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2461 | snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr; |
| 2462 | int ret; |
| 2463 | |
| 2464 | runtime->control->appl_ptr = appl_ptr; |
| 2465 | if (substream->ops->ack) { |
| 2466 | ret = substream->ops->ack(substream); |
| 2467 | if (ret < 0) { |
| 2468 | runtime->control->appl_ptr = old_appl_ptr; |
| 2469 | return ret; |
| 2470 | } |
| 2471 | } |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
| 2475 | /* increase the appl_ptr; returns the processed frames or a negative error */ |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2476 | static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, |
| 2477 | snd_pcm_uframes_t frames, |
| 2478 | snd_pcm_sframes_t avail) |
| 2479 | { |
| 2480 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2481 | snd_pcm_sframes_t appl_ptr; |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2482 | int ret; |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2483 | |
| 2484 | if (avail <= 0) |
| 2485 | return 0; |
| 2486 | if (frames > (snd_pcm_uframes_t)avail) |
| 2487 | frames = avail; |
| 2488 | appl_ptr = runtime->control->appl_ptr + frames; |
| 2489 | if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) |
| 2490 | appl_ptr -= runtime->boundary; |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2491 | ret = apply_appl_ptr(substream, appl_ptr); |
| 2492 | return ret < 0 ? ret : frames; |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2493 | } |
| 2494 | |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2495 | /* decrease the appl_ptr; returns the processed frames or a negative error */ |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2496 | static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, |
| 2497 | snd_pcm_uframes_t frames, |
| 2498 | snd_pcm_sframes_t avail) |
| 2499 | { |
| 2500 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2501 | snd_pcm_sframes_t appl_ptr; |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2502 | int ret; |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2503 | |
| 2504 | if (avail <= 0) |
| 2505 | return 0; |
| 2506 | if (frames > (snd_pcm_uframes_t)avail) |
| 2507 | frames = avail; |
| 2508 | appl_ptr = runtime->control->appl_ptr - frames; |
| 2509 | if (appl_ptr < 0) |
| 2510 | appl_ptr += runtime->boundary; |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2511 | ret = apply_appl_ptr(substream, appl_ptr); |
| 2512 | return ret < 0 ? ret : frames; |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2515 | static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream, |
| 2516 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2518 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | snd_pcm_sframes_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | |
| 2521 | if (frames == 0) |
| 2522 | return 0; |
| 2523 | |
| 2524 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2525 | ret = do_pcm_hwsync(substream); |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2526 | if (!ret) |
| 2527 | ret = rewind_appl_ptr(substream, frames, |
| 2528 | snd_pcm_playback_hw_avail(runtime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | snd_pcm_stream_unlock_irq(substream); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2533 | static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream, |
| 2534 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2536 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | snd_pcm_sframes_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
| 2539 | if (frames == 0) |
| 2540 | return 0; |
| 2541 | |
| 2542 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2543 | ret = do_pcm_hwsync(substream); |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2544 | if (!ret) |
| 2545 | ret = rewind_appl_ptr(substream, frames, |
| 2546 | snd_pcm_capture_hw_avail(runtime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | snd_pcm_stream_unlock_irq(substream); |
| 2548 | return ret; |
| 2549 | } |
| 2550 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2551 | static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream, |
| 2552 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2554 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | snd_pcm_sframes_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | if (frames == 0) |
| 2558 | return 0; |
| 2559 | |
| 2560 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2561 | ret = do_pcm_hwsync(substream); |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2562 | if (!ret) |
| 2563 | ret = forward_appl_ptr(substream, frames, |
| 2564 | snd_pcm_playback_avail(runtime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | snd_pcm_stream_unlock_irq(substream); |
| 2566 | return ret; |
| 2567 | } |
| 2568 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2569 | static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream, |
| 2570 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2572 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | snd_pcm_sframes_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | |
| 2575 | if (frames == 0) |
| 2576 | return 0; |
| 2577 | |
| 2578 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2579 | ret = do_pcm_hwsync(substream); |
Takashi Iwai | e0327a0 | 2017-05-19 20:16:44 +0200 | [diff] [blame] | 2580 | if (!ret) |
| 2581 | ret = forward_appl_ptr(substream, frames, |
| 2582 | snd_pcm_capture_avail(runtime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | snd_pcm_stream_unlock_irq(substream); |
| 2584 | return ret; |
| 2585 | } |
| 2586 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2587 | static int snd_pcm_hwsync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | int err; |
| 2590 | |
| 2591 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2592 | err = do_pcm_hwsync(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | snd_pcm_stream_unlock_irq(substream); |
| 2594 | return err; |
| 2595 | } |
| 2596 | |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2597 | static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2599 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | int err; |
| 2601 | snd_pcm_sframes_t n = 0; |
| 2602 | |
| 2603 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | f839cc1 | 2017-05-19 20:04:23 +0200 | [diff] [blame] | 2604 | err = do_pcm_hwsync(substream); |
| 2605 | if (!err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 2607 | n = snd_pcm_playback_hw_avail(runtime); |
| 2608 | else |
| 2609 | n = snd_pcm_capture_avail(runtime); |
Takashi Iwai | 4bbe1dd | 2008-10-13 03:07:14 +0200 | [diff] [blame] | 2610 | n += runtime->delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | } |
| 2612 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2613 | return err < 0 ? err : n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2616 | static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, |
| 2617 | struct snd_pcm_sync_ptr __user *_sync_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2619 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2620 | struct snd_pcm_sync_ptr sync_ptr; |
| 2621 | volatile struct snd_pcm_mmap_status *status; |
| 2622 | volatile struct snd_pcm_mmap_control *control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | int err; |
| 2624 | |
| 2625 | memset(&sync_ptr, 0, sizeof(sync_ptr)); |
| 2626 | if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) |
| 2627 | return -EFAULT; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2628 | if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | return -EFAULT; |
| 2630 | status = runtime->status; |
| 2631 | control = runtime->control; |
| 2632 | if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { |
| 2633 | err = snd_pcm_hwsync(substream); |
| 2634 | if (err < 0) |
| 2635 | return err; |
| 2636 | } |
| 2637 | snd_pcm_stream_lock_irq(substream); |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2638 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) { |
| 2639 | err = apply_appl_ptr(substream, sync_ptr.c.control.appl_ptr); |
| 2640 | if (err < 0) { |
| 2641 | snd_pcm_stream_unlock_irq(substream); |
| 2642 | return err; |
| 2643 | } |
| 2644 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | sync_ptr.c.control.appl_ptr = control->appl_ptr; |
Takashi Iwai | 9027c46 | 2017-05-19 20:22:33 +0200 | [diff] [blame] | 2646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) |
| 2648 | control->avail_min = sync_ptr.c.control.avail_min; |
| 2649 | else |
| 2650 | sync_ptr.c.control.avail_min = control->avail_min; |
| 2651 | sync_ptr.s.status.state = status->state; |
| 2652 | sync_ptr.s.status.hw_ptr = status->hw_ptr; |
| 2653 | sync_ptr.s.status.tstamp = status->tstamp; |
| 2654 | sync_ptr.s.status.suspended_state = status->suspended_state; |
| 2655 | snd_pcm_stream_unlock_irq(substream); |
| 2656 | if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) |
| 2657 | return -EFAULT; |
| 2658 | return 0; |
| 2659 | } |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 2660 | |
| 2661 | static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) |
| 2662 | { |
| 2663 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2664 | int arg; |
| 2665 | |
| 2666 | if (get_user(arg, _arg)) |
| 2667 | return -EFAULT; |
| 2668 | if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) |
| 2669 | return -EINVAL; |
Takashi Iwai | 2408c21 | 2014-07-10 09:40:38 +0200 | [diff] [blame] | 2670 | runtime->tstamp_type = arg; |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 2671 | return 0; |
| 2672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2674 | static int snd_pcm_common_ioctl1(struct file *file, |
| 2675 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | unsigned int cmd, void __user *arg) |
| 2677 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | switch (cmd) { |
| 2679 | case SNDRV_PCM_IOCTL_PVERSION: |
| 2680 | return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; |
| 2681 | case SNDRV_PCM_IOCTL_INFO: |
| 2682 | return snd_pcm_info_user(substream, arg); |
Jaroslav Kysela | 28e9e47 | 2007-12-17 09:02:22 +0100 | [diff] [blame] | 2683 | case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ |
| 2684 | return 0; |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 2685 | case SNDRV_PCM_IOCTL_TTSTAMP: |
| 2686 | return snd_pcm_tstamp(substream, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | case SNDRV_PCM_IOCTL_HW_REFINE: |
| 2688 | return snd_pcm_hw_refine_user(substream, arg); |
| 2689 | case SNDRV_PCM_IOCTL_HW_PARAMS: |
| 2690 | return snd_pcm_hw_params_user(substream, arg); |
| 2691 | case SNDRV_PCM_IOCTL_HW_FREE: |
| 2692 | return snd_pcm_hw_free(substream); |
| 2693 | case SNDRV_PCM_IOCTL_SW_PARAMS: |
| 2694 | return snd_pcm_sw_params_user(substream, arg); |
| 2695 | case SNDRV_PCM_IOCTL_STATUS: |
Pierre-Louis Bossart | 38ca2a4 | 2015-02-13 15:14:04 -0600 | [diff] [blame] | 2696 | return snd_pcm_status_user(substream, arg, false); |
| 2697 | case SNDRV_PCM_IOCTL_STATUS_EXT: |
| 2698 | return snd_pcm_status_user(substream, arg, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | case SNDRV_PCM_IOCTL_CHANNEL_INFO: |
| 2700 | return snd_pcm_channel_info_user(substream, arg); |
| 2701 | case SNDRV_PCM_IOCTL_PREPARE: |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2702 | return snd_pcm_prepare(substream, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | case SNDRV_PCM_IOCTL_RESET: |
| 2704 | return snd_pcm_reset(substream); |
| 2705 | case SNDRV_PCM_IOCTL_START: |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2706 | return snd_pcm_start_lock_irq(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | case SNDRV_PCM_IOCTL_LINK: |
| 2708 | return snd_pcm_link(substream, (int)(unsigned long) arg); |
| 2709 | case SNDRV_PCM_IOCTL_UNLINK: |
| 2710 | return snd_pcm_unlink(substream); |
| 2711 | case SNDRV_PCM_IOCTL_RESUME: |
| 2712 | return snd_pcm_resume(substream); |
| 2713 | case SNDRV_PCM_IOCTL_XRUN: |
| 2714 | return snd_pcm_xrun(substream); |
| 2715 | case SNDRV_PCM_IOCTL_HWSYNC: |
| 2716 | return snd_pcm_hwsync(substream); |
| 2717 | case SNDRV_PCM_IOCTL_DELAY: |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2718 | { |
| 2719 | snd_pcm_sframes_t delay = snd_pcm_delay(substream); |
| 2720 | snd_pcm_sframes_t __user *res = arg; |
| 2721 | |
| 2722 | if (delay < 0) |
| 2723 | return delay; |
| 2724 | if (put_user(delay, res)) |
| 2725 | return -EFAULT; |
| 2726 | return 0; |
| 2727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | case SNDRV_PCM_IOCTL_SYNC_PTR: |
| 2729 | return snd_pcm_sync_ptr(substream, arg); |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 2730 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | case SNDRV_PCM_IOCTL_HW_REFINE_OLD: |
| 2732 | return snd_pcm_hw_refine_old_user(substream, arg); |
| 2733 | case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: |
| 2734 | return snd_pcm_hw_params_old_user(substream, arg); |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 2735 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | case SNDRV_PCM_IOCTL_DRAIN: |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 2737 | return snd_pcm_drain(substream, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | case SNDRV_PCM_IOCTL_DROP: |
| 2739 | return snd_pcm_drop(substream); |
Takashi Iwai | e661d0d | 2006-02-21 14:14:50 +0100 | [diff] [blame] | 2740 | case SNDRV_PCM_IOCTL_PAUSE: |
| 2741 | { |
| 2742 | int res; |
| 2743 | snd_pcm_stream_lock_irq(substream); |
| 2744 | res = snd_pcm_pause(substream, (int)(unsigned long)arg); |
| 2745 | snd_pcm_stream_unlock_irq(substream); |
| 2746 | return res; |
| 2747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Takashi Iwai | 09e56df | 2014-02-04 18:19:48 +0100 | [diff] [blame] | 2749 | pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | return -ENOTTY; |
| 2751 | } |
| 2752 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2753 | static int snd_pcm_playback_ioctl1(struct file *file, |
| 2754 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | unsigned int cmd, void __user *arg) |
| 2756 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2757 | if (snd_BUG_ON(!substream)) |
| 2758 | return -ENXIO; |
| 2759 | if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) |
| 2760 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | switch (cmd) { |
| 2762 | case SNDRV_PCM_IOCTL_WRITEI_FRAMES: |
| 2763 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2764 | struct snd_xferi xferi; |
| 2765 | struct snd_xferi __user *_xferi = arg; |
| 2766 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | snd_pcm_sframes_t result; |
| 2768 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2769 | return -EBADFD; |
| 2770 | if (put_user(0, &_xferi->result)) |
| 2771 | return -EFAULT; |
| 2772 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) |
| 2773 | return -EFAULT; |
| 2774 | result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); |
| 2775 | __put_user(result, &_xferi->result); |
| 2776 | return result < 0 ? result : 0; |
| 2777 | } |
| 2778 | case SNDRV_PCM_IOCTL_WRITEN_FRAMES: |
| 2779 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2780 | struct snd_xfern xfern; |
| 2781 | struct snd_xfern __user *_xfern = arg; |
| 2782 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | void __user **bufs; |
| 2784 | snd_pcm_sframes_t result; |
| 2785 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2786 | return -EBADFD; |
| 2787 | if (runtime->channels > 128) |
| 2788 | return -EINVAL; |
| 2789 | if (put_user(0, &_xfern->result)) |
| 2790 | return -EFAULT; |
| 2791 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) |
| 2792 | return -EFAULT; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 2793 | |
| 2794 | bufs = memdup_user(xfern.bufs, |
| 2795 | sizeof(void *) * runtime->channels); |
| 2796 | if (IS_ERR(bufs)) |
| 2797 | return PTR_ERR(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | result = snd_pcm_lib_writev(substream, bufs, xfern.frames); |
| 2799 | kfree(bufs); |
| 2800 | __put_user(result, &_xfern->result); |
| 2801 | return result < 0 ? result : 0; |
| 2802 | } |
| 2803 | case SNDRV_PCM_IOCTL_REWIND: |
| 2804 | { |
| 2805 | snd_pcm_uframes_t frames; |
| 2806 | snd_pcm_uframes_t __user *_frames = arg; |
| 2807 | snd_pcm_sframes_t result; |
| 2808 | if (get_user(frames, _frames)) |
| 2809 | return -EFAULT; |
| 2810 | if (put_user(0, _frames)) |
| 2811 | return -EFAULT; |
| 2812 | result = snd_pcm_playback_rewind(substream, frames); |
| 2813 | __put_user(result, _frames); |
| 2814 | return result < 0 ? result : 0; |
| 2815 | } |
| 2816 | case SNDRV_PCM_IOCTL_FORWARD: |
| 2817 | { |
| 2818 | snd_pcm_uframes_t frames; |
| 2819 | snd_pcm_uframes_t __user *_frames = arg; |
| 2820 | snd_pcm_sframes_t result; |
| 2821 | if (get_user(frames, _frames)) |
| 2822 | return -EFAULT; |
| 2823 | if (put_user(0, _frames)) |
| 2824 | return -EFAULT; |
| 2825 | result = snd_pcm_playback_forward(substream, frames); |
| 2826 | __put_user(result, _frames); |
| 2827 | return result < 0 ? result : 0; |
| 2828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | } |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2830 | return snd_pcm_common_ioctl1(file, substream, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2833 | static int snd_pcm_capture_ioctl1(struct file *file, |
| 2834 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | unsigned int cmd, void __user *arg) |
| 2836 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2837 | if (snd_BUG_ON(!substream)) |
| 2838 | return -ENXIO; |
| 2839 | if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE)) |
| 2840 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | switch (cmd) { |
| 2842 | case SNDRV_PCM_IOCTL_READI_FRAMES: |
| 2843 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2844 | struct snd_xferi xferi; |
| 2845 | struct snd_xferi __user *_xferi = arg; |
| 2846 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | snd_pcm_sframes_t result; |
| 2848 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2849 | return -EBADFD; |
| 2850 | if (put_user(0, &_xferi->result)) |
| 2851 | return -EFAULT; |
| 2852 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) |
| 2853 | return -EFAULT; |
| 2854 | result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); |
| 2855 | __put_user(result, &_xferi->result); |
| 2856 | return result < 0 ? result : 0; |
| 2857 | } |
| 2858 | case SNDRV_PCM_IOCTL_READN_FRAMES: |
| 2859 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2860 | struct snd_xfern xfern; |
| 2861 | struct snd_xfern __user *_xfern = arg; |
| 2862 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | void *bufs; |
| 2864 | snd_pcm_sframes_t result; |
| 2865 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2866 | return -EBADFD; |
| 2867 | if (runtime->channels > 128) |
| 2868 | return -EINVAL; |
| 2869 | if (put_user(0, &_xfern->result)) |
| 2870 | return -EFAULT; |
| 2871 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) |
| 2872 | return -EFAULT; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 2873 | |
| 2874 | bufs = memdup_user(xfern.bufs, |
| 2875 | sizeof(void *) * runtime->channels); |
| 2876 | if (IS_ERR(bufs)) |
| 2877 | return PTR_ERR(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | result = snd_pcm_lib_readv(substream, bufs, xfern.frames); |
| 2879 | kfree(bufs); |
| 2880 | __put_user(result, &_xfern->result); |
| 2881 | return result < 0 ? result : 0; |
| 2882 | } |
| 2883 | case SNDRV_PCM_IOCTL_REWIND: |
| 2884 | { |
| 2885 | snd_pcm_uframes_t frames; |
| 2886 | snd_pcm_uframes_t __user *_frames = arg; |
| 2887 | snd_pcm_sframes_t result; |
| 2888 | if (get_user(frames, _frames)) |
| 2889 | return -EFAULT; |
| 2890 | if (put_user(0, _frames)) |
| 2891 | return -EFAULT; |
| 2892 | result = snd_pcm_capture_rewind(substream, frames); |
| 2893 | __put_user(result, _frames); |
| 2894 | return result < 0 ? result : 0; |
| 2895 | } |
| 2896 | case SNDRV_PCM_IOCTL_FORWARD: |
| 2897 | { |
| 2898 | snd_pcm_uframes_t frames; |
| 2899 | snd_pcm_uframes_t __user *_frames = arg; |
| 2900 | snd_pcm_sframes_t result; |
| 2901 | if (get_user(frames, _frames)) |
| 2902 | return -EFAULT; |
| 2903 | if (put_user(0, _frames)) |
| 2904 | return -EFAULT; |
| 2905 | result = snd_pcm_capture_forward(substream, frames); |
| 2906 | __put_user(result, _frames); |
| 2907 | return result < 0 ? result : 0; |
| 2908 | } |
| 2909 | } |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2910 | return snd_pcm_common_ioctl1(file, substream, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2913 | static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, |
| 2914 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2916 | struct snd_pcm_file *pcm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | |
| 2918 | pcm_file = file->private_data; |
| 2919 | |
| 2920 | if (((cmd >> 8) & 0xff) != 'A') |
| 2921 | return -ENOTTY; |
| 2922 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2923 | return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd, |
| 2924 | (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2927 | static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, |
| 2928 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2930 | struct snd_pcm_file *pcm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | |
| 2932 | pcm_file = file->private_data; |
| 2933 | |
| 2934 | if (((cmd >> 8) & 0xff) != 'A') |
| 2935 | return -ENOTTY; |
| 2936 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2937 | return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd, |
| 2938 | (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2941 | /** |
| 2942 | * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space |
| 2943 | * @substream: PCM substream |
| 2944 | * @cmd: IOCTL cmd |
| 2945 | * @arg: IOCTL argument |
| 2946 | * |
| 2947 | * The function is provided primarily for OSS layer and USB gadget drivers, |
| 2948 | * and it allows only the limited set of ioctls (hw_params, sw_params, |
| 2949 | * prepare, start, drain, drop, forward). |
| 2950 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2951 | int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | unsigned int cmd, void *arg) |
| 2953 | { |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2954 | snd_pcm_uframes_t *frames = arg; |
| 2955 | snd_pcm_sframes_t result; |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2956 | |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2957 | switch (cmd) { |
| 2958 | case SNDRV_PCM_IOCTL_FORWARD: |
| 2959 | { |
| 2960 | /* provided only for OSS; capture-only and no value returned */ |
| 2961 | if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) |
| 2962 | return -EINVAL; |
| 2963 | result = snd_pcm_capture_forward(substream, *frames); |
| 2964 | return result < 0 ? result : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | } |
Takashi Iwai | c2c86a9 | 2017-05-10 14:33:44 +0200 | [diff] [blame] | 2966 | case SNDRV_PCM_IOCTL_HW_PARAMS: |
| 2967 | return snd_pcm_hw_params(substream, arg); |
| 2968 | case SNDRV_PCM_IOCTL_SW_PARAMS: |
| 2969 | return snd_pcm_sw_params(substream, arg); |
| 2970 | case SNDRV_PCM_IOCTL_PREPARE: |
| 2971 | return snd_pcm_prepare(substream, NULL); |
| 2972 | case SNDRV_PCM_IOCTL_START: |
| 2973 | return snd_pcm_start_lock_irq(substream); |
| 2974 | case SNDRV_PCM_IOCTL_DRAIN: |
| 2975 | return snd_pcm_drain(substream, NULL); |
| 2976 | case SNDRV_PCM_IOCTL_DROP: |
| 2977 | return snd_pcm_drop(substream); |
| 2978 | case SNDRV_PCM_IOCTL_DELAY: |
| 2979 | { |
| 2980 | result = snd_pcm_delay(substream); |
| 2981 | if (result < 0) |
| 2982 | return result; |
| 2983 | *frames = result; |
| 2984 | return 0; |
| 2985 | } |
| 2986 | default: |
| 2987 | return -EINVAL; |
| 2988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | } |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2990 | EXPORT_SYMBOL(snd_pcm_kernel_ioctl); |
| 2991 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2992 | static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, |
| 2993 | loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2995 | struct snd_pcm_file *pcm_file; |
| 2996 | struct snd_pcm_substream *substream; |
| 2997 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | snd_pcm_sframes_t result; |
| 2999 | |
| 3000 | pcm_file = file->private_data; |
| 3001 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3002 | if (PCM_RUNTIME_CHECK(substream)) |
| 3003 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | runtime = substream->runtime; |
| 3005 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3006 | return -EBADFD; |
| 3007 | if (!frame_aligned(runtime, count)) |
| 3008 | return -EINVAL; |
| 3009 | count = bytes_to_frames(runtime, count); |
| 3010 | result = snd_pcm_lib_read(substream, buf, count); |
| 3011 | if (result > 0) |
| 3012 | result = frames_to_bytes(runtime, result); |
| 3013 | return result; |
| 3014 | } |
| 3015 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3016 | static ssize_t snd_pcm_write(struct file *file, const char __user *buf, |
| 3017 | size_t count, loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3019 | struct snd_pcm_file *pcm_file; |
| 3020 | struct snd_pcm_substream *substream; |
| 3021 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | snd_pcm_sframes_t result; |
| 3023 | |
| 3024 | pcm_file = file->private_data; |
| 3025 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3026 | if (PCM_RUNTIME_CHECK(substream)) |
| 3027 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | runtime = substream->runtime; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3029 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3030 | return -EBADFD; |
| 3031 | if (!frame_aligned(runtime, count)) |
| 3032 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | count = bytes_to_frames(runtime, count); |
| 3034 | result = snd_pcm_lib_write(substream, buf, count); |
| 3035 | if (result > 0) |
| 3036 | result = frames_to_bytes(runtime, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | return result; |
| 3038 | } |
| 3039 | |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3040 | static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3042 | struct snd_pcm_file *pcm_file; |
| 3043 | struct snd_pcm_substream *substream; |
| 3044 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | snd_pcm_sframes_t result; |
| 3046 | unsigned long i; |
| 3047 | void __user **bufs; |
| 3048 | snd_pcm_uframes_t frames; |
| 3049 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 3050 | pcm_file = iocb->ki_filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3052 | if (PCM_RUNTIME_CHECK(substream)) |
| 3053 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | runtime = substream->runtime; |
| 3055 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3056 | return -EBADFD; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3057 | if (!iter_is_iovec(to)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | return -EINVAL; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3059 | if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | return -EINVAL; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3061 | if (!frame_aligned(runtime, to->iov->iov_len)) |
| 3062 | return -EINVAL; |
| 3063 | frames = bytes_to_samples(runtime, to->iov->iov_len); |
| 3064 | bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | if (bufs == NULL) |
| 3066 | return -ENOMEM; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3067 | for (i = 0; i < to->nr_segs; ++i) |
| 3068 | bufs[i] = to->iov[i].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | result = snd_pcm_lib_readv(substream, bufs, frames); |
| 3070 | if (result > 0) |
| 3071 | result = frames_to_bytes(runtime, result); |
| 3072 | kfree(bufs); |
| 3073 | return result; |
| 3074 | } |
| 3075 | |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3076 | static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3078 | struct snd_pcm_file *pcm_file; |
| 3079 | struct snd_pcm_substream *substream; |
| 3080 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | snd_pcm_sframes_t result; |
| 3082 | unsigned long i; |
| 3083 | void __user **bufs; |
| 3084 | snd_pcm_uframes_t frames; |
| 3085 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 3086 | pcm_file = iocb->ki_filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3088 | if (PCM_RUNTIME_CHECK(substream)) |
| 3089 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | runtime = substream->runtime; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3091 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3092 | return -EBADFD; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3093 | if (!iter_is_iovec(from)) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3094 | return -EINVAL; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3095 | if (from->nr_segs > 128 || from->nr_segs != runtime->channels || |
| 3096 | !frame_aligned(runtime, from->iov->iov_len)) |
| 3097 | return -EINVAL; |
| 3098 | frames = bytes_to_samples(runtime, from->iov->iov_len); |
| 3099 | bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | if (bufs == NULL) |
| 3101 | return -ENOMEM; |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3102 | for (i = 0; i < from->nr_segs; ++i) |
| 3103 | bufs[i] = from->iov[i].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | result = snd_pcm_lib_writev(substream, bufs, frames); |
| 3105 | if (result > 0) |
| 3106 | result = frames_to_bytes(runtime, result); |
| 3107 | kfree(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | return result; |
| 3109 | } |
| 3110 | |
| 3111 | static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait) |
| 3112 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3113 | struct snd_pcm_file *pcm_file; |
| 3114 | struct snd_pcm_substream *substream; |
| 3115 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | unsigned int mask; |
| 3117 | snd_pcm_uframes_t avail; |
| 3118 | |
| 3119 | pcm_file = file->private_data; |
| 3120 | |
| 3121 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3122 | if (PCM_RUNTIME_CHECK(substream)) |
Charles Keepax | e099aee | 2016-05-04 14:59:07 +0100 | [diff] [blame] | 3123 | return POLLOUT | POLLWRNORM | POLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | runtime = substream->runtime; |
| 3125 | |
| 3126 | poll_wait(file, &runtime->sleep, wait); |
| 3127 | |
| 3128 | snd_pcm_stream_lock_irq(substream); |
| 3129 | avail = snd_pcm_playback_avail(runtime); |
| 3130 | switch (runtime->status->state) { |
| 3131 | case SNDRV_PCM_STATE_RUNNING: |
| 3132 | case SNDRV_PCM_STATE_PREPARED: |
| 3133 | case SNDRV_PCM_STATE_PAUSED: |
| 3134 | if (avail >= runtime->control->avail_min) { |
| 3135 | mask = POLLOUT | POLLWRNORM; |
| 3136 | break; |
| 3137 | } |
| 3138 | /* Fall through */ |
| 3139 | case SNDRV_PCM_STATE_DRAINING: |
| 3140 | mask = 0; |
| 3141 | break; |
| 3142 | default: |
| 3143 | mask = POLLOUT | POLLWRNORM | POLLERR; |
| 3144 | break; |
| 3145 | } |
| 3146 | snd_pcm_stream_unlock_irq(substream); |
| 3147 | return mask; |
| 3148 | } |
| 3149 | |
| 3150 | static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait) |
| 3151 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3152 | struct snd_pcm_file *pcm_file; |
| 3153 | struct snd_pcm_substream *substream; |
| 3154 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | unsigned int mask; |
| 3156 | snd_pcm_uframes_t avail; |
| 3157 | |
| 3158 | pcm_file = file->private_data; |
| 3159 | |
| 3160 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3161 | if (PCM_RUNTIME_CHECK(substream)) |
Charles Keepax | e099aee | 2016-05-04 14:59:07 +0100 | [diff] [blame] | 3162 | return POLLIN | POLLRDNORM | POLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | runtime = substream->runtime; |
| 3164 | |
| 3165 | poll_wait(file, &runtime->sleep, wait); |
| 3166 | |
| 3167 | snd_pcm_stream_lock_irq(substream); |
| 3168 | avail = snd_pcm_capture_avail(runtime); |
| 3169 | switch (runtime->status->state) { |
| 3170 | case SNDRV_PCM_STATE_RUNNING: |
| 3171 | case SNDRV_PCM_STATE_PREPARED: |
| 3172 | case SNDRV_PCM_STATE_PAUSED: |
| 3173 | if (avail >= runtime->control->avail_min) { |
| 3174 | mask = POLLIN | POLLRDNORM; |
| 3175 | break; |
| 3176 | } |
| 3177 | mask = 0; |
| 3178 | break; |
| 3179 | case SNDRV_PCM_STATE_DRAINING: |
| 3180 | if (avail > 0) { |
| 3181 | mask = POLLIN | POLLRDNORM; |
| 3182 | break; |
| 3183 | } |
| 3184 | /* Fall through */ |
| 3185 | default: |
| 3186 | mask = POLLIN | POLLRDNORM | POLLERR; |
| 3187 | break; |
| 3188 | } |
| 3189 | snd_pcm_stream_unlock_irq(substream); |
| 3190 | return mask; |
| 3191 | } |
| 3192 | |
| 3193 | /* |
| 3194 | * mmap support |
| 3195 | */ |
| 3196 | |
| 3197 | /* |
| 3198 | * Only on coherent architectures, we can mmap the status and the control records |
| 3199 | * for effcient data transfer. On others, we have to use HWSYNC ioctl... |
| 3200 | */ |
| 3201 | #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) |
| 3202 | /* |
| 3203 | * mmap status record |
| 3204 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3205 | static int snd_pcm_mmap_status_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3207 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3208 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | |
| 3210 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3211 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3213 | vmf->page = virt_to_page(runtime->status); |
| 3214 | get_page(vmf->page); |
| 3215 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3218 | static const struct vm_operations_struct snd_pcm_vm_ops_status = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | { |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3220 | .fault = snd_pcm_mmap_status_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | }; |
| 3222 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3223 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | struct vm_area_struct *area) |
| 3225 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | long size; |
| 3227 | if (!(area->vm_flags & VM_READ)) |
| 3228 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | size = area->vm_end - area->vm_start; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3230 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | return -EINVAL; |
| 3232 | area->vm_ops = &snd_pcm_vm_ops_status; |
| 3233 | area->vm_private_data = substream; |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 3234 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | |
| 3238 | /* |
| 3239 | * mmap control record |
| 3240 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3241 | static int snd_pcm_mmap_control_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3243 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3244 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | |
| 3246 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3247 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3249 | vmf->page = virt_to_page(runtime->control); |
| 3250 | get_page(vmf->page); |
| 3251 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3254 | static const struct vm_operations_struct snd_pcm_vm_ops_control = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | { |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3256 | .fault = snd_pcm_mmap_control_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | }; |
| 3258 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3259 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | struct vm_area_struct *area) |
| 3261 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | long size; |
| 3263 | if (!(area->vm_flags & VM_READ)) |
| 3264 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | size = area->vm_end - area->vm_start; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3266 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | return -EINVAL; |
| 3268 | area->vm_ops = &snd_pcm_vm_ops_control; |
| 3269 | area->vm_private_data = substream; |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 3270 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | return 0; |
| 3272 | } |
| 3273 | #else /* ! coherent mmap */ |
| 3274 | /* |
| 3275 | * don't support mmap for status and control records. |
| 3276 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3277 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | struct vm_area_struct *area) |
| 3279 | { |
| 3280 | return -ENXIO; |
| 3281 | } |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3282 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | struct vm_area_struct *area) |
| 3284 | { |
| 3285 | return -ENXIO; |
| 3286 | } |
| 3287 | #endif /* coherent mmap */ |
| 3288 | |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3289 | static inline struct page * |
| 3290 | snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs) |
| 3291 | { |
| 3292 | void *vaddr = substream->runtime->dma_area + ofs; |
| 3293 | return virt_to_page(vaddr); |
| 3294 | } |
| 3295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | /* |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3297 | * fault callback for mmapping a RAM page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3299 | static int snd_pcm_mmap_data_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3301 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3302 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | unsigned long offset; |
| 3304 | struct page * page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | size_t dma_bytes; |
| 3306 | |
| 3307 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3308 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3310 | offset = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
| 3312 | if (offset > dma_bytes - PAGE_SIZE) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3313 | return VM_FAULT_SIGBUS; |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3314 | if (substream->ops->page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | page = substream->ops->page(substream, offset); |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3316 | else |
| 3317 | page = snd_pcm_default_page_ops(substream, offset); |
| 3318 | if (!page) |
| 3319 | return VM_FAULT_SIGBUS; |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 3320 | get_page(page); |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3321 | vmf->page = page; |
| 3322 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | } |
| 3324 | |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3325 | static const struct vm_operations_struct snd_pcm_vm_ops_data = { |
| 3326 | .open = snd_pcm_mmap_data_open, |
| 3327 | .close = snd_pcm_mmap_data_close, |
| 3328 | }; |
| 3329 | |
| 3330 | static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3331 | .open = snd_pcm_mmap_data_open, |
| 3332 | .close = snd_pcm_mmap_data_close, |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3333 | .fault = snd_pcm_mmap_data_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | }; |
| 3335 | |
| 3336 | /* |
| 3337 | * mmap the DMA buffer on RAM |
| 3338 | */ |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 3339 | |
| 3340 | /** |
| 3341 | * snd_pcm_lib_default_mmap - Default PCM data mmap function |
| 3342 | * @substream: PCM substream |
| 3343 | * @area: VMA |
| 3344 | * |
| 3345 | * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, |
| 3346 | * this function is invoked implicitly. |
| 3347 | */ |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3348 | int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, |
| 3349 | struct vm_area_struct *area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | { |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 3351 | area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 3352 | #ifdef CONFIG_GENERIC_ALLOCATOR |
Nicolin Chen | 0550321 | 2013-10-23 11:47:43 +0800 | [diff] [blame] | 3353 | if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) { |
| 3354 | area->vm_page_prot = pgprot_writecombine(area->vm_page_prot); |
| 3355 | return remap_pfn_range(area, area->vm_start, |
| 3356 | substream->dma_buffer.addr >> PAGE_SHIFT, |
| 3357 | area->vm_end - area->vm_start, area->vm_page_prot); |
| 3358 | } |
Takashi Iwai | a5606f8 | 2013-10-24 14:25:32 +0200 | [diff] [blame] | 3359 | #endif /* CONFIG_GENERIC_ALLOCATOR */ |
Takashi Iwai | 49d776f | 2014-10-24 12:36:23 +0200 | [diff] [blame] | 3360 | #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */ |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3361 | if (!substream->ops->page && |
| 3362 | substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) |
| 3363 | return dma_mmap_coherent(substream->dma_buffer.dev.dev, |
| 3364 | area, |
| 3365 | substream->runtime->dma_area, |
| 3366 | substream->runtime->dma_addr, |
| 3367 | area->vm_end - area->vm_start); |
Takashi Iwai | 49d776f | 2014-10-24 12:36:23 +0200 | [diff] [blame] | 3368 | #endif /* CONFIG_X86 */ |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3369 | /* mmap with fault handler */ |
| 3370 | area->vm_ops = &snd_pcm_vm_ops_data_fault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | return 0; |
| 3372 | } |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3373 | EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | |
| 3375 | /* |
| 3376 | * mmap the DMA buffer on I/O memory area |
| 3377 | */ |
| 3378 | #if SNDRV_PCM_INFO_MMAP_IOMEM |
Takashi Iwai | 30b771c | 2014-10-30 15:02:50 +0100 | [diff] [blame] | 3379 | /** |
| 3380 | * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem |
| 3381 | * @substream: PCM substream |
| 3382 | * @area: VMA |
| 3383 | * |
| 3384 | * When your hardware uses the iomapped pages as the hardware buffer and |
| 3385 | * wants to mmap it, pass this function as mmap pcm_ops. Note that this |
| 3386 | * is supposed to work only on limited architectures. |
| 3387 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3388 | int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, |
| 3389 | struct vm_area_struct *area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | { |
Linus Torvalds | 0fe09a4 | 2013-04-19 10:01:04 -0700 | [diff] [blame] | 3391 | struct snd_pcm_runtime *runtime = substream->runtime;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | area->vm_page_prot = pgprot_noncached(area->vm_page_prot); |
Linus Torvalds | 0fe09a4 | 2013-04-19 10:01:04 -0700 | [diff] [blame] | 3394 | return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | } |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 3396 | |
| 3397 | EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | #endif /* SNDRV_PCM_INFO_MMAP */ |
| 3399 | |
| 3400 | /* |
| 3401 | * mmap DMA buffer |
| 3402 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3403 | int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | struct vm_area_struct *area) |
| 3405 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3406 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | long size; |
| 3408 | unsigned long offset; |
| 3409 | size_t dma_bytes; |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3410 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | |
| 3412 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 3413 | if (!(area->vm_flags & (VM_WRITE|VM_READ))) |
| 3414 | return -EINVAL; |
| 3415 | } else { |
| 3416 | if (!(area->vm_flags & VM_READ)) |
| 3417 | return -EINVAL; |
| 3418 | } |
| 3419 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3421 | return -EBADFD; |
| 3422 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) |
| 3423 | return -ENXIO; |
| 3424 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || |
| 3425 | runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 3426 | return -EINVAL; |
| 3427 | size = area->vm_end - area->vm_start; |
| 3428 | offset = area->vm_pgoff << PAGE_SHIFT; |
| 3429 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
| 3430 | if ((size_t)size > dma_bytes) |
| 3431 | return -EINVAL; |
| 3432 | if (offset > dma_bytes - size) |
| 3433 | return -EINVAL; |
| 3434 | |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3435 | area->vm_ops = &snd_pcm_vm_ops_data; |
| 3436 | area->vm_private_data = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | if (substream->ops->mmap) |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3438 | err = substream->ops->mmap(substream, area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | else |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3440 | err = snd_pcm_lib_default_mmap(substream, area); |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3441 | if (!err) |
| 3442 | atomic_inc(&substream->mmap_count); |
| 3443 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 3446 | EXPORT_SYMBOL(snd_pcm_mmap_data); |
| 3447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) |
| 3449 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3450 | struct snd_pcm_file * pcm_file; |
| 3451 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | unsigned long offset; |
| 3453 | |
| 3454 | pcm_file = file->private_data; |
| 3455 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3456 | if (PCM_RUNTIME_CHECK(substream)) |
| 3457 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
| 3459 | offset = area->vm_pgoff << PAGE_SHIFT; |
| 3460 | switch (offset) { |
| 3461 | case SNDRV_PCM_MMAP_OFFSET_STATUS: |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 3462 | if (pcm_file->no_compat_mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | return -ENXIO; |
| 3464 | return snd_pcm_mmap_status(substream, file, area); |
| 3465 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 3466 | if (pcm_file->no_compat_mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | return -ENXIO; |
| 3468 | return snd_pcm_mmap_control(substream, file, area); |
| 3469 | default: |
| 3470 | return snd_pcm_mmap_data(substream, file, area); |
| 3471 | } |
| 3472 | return 0; |
| 3473 | } |
| 3474 | |
| 3475 | static int snd_pcm_fasync(int fd, struct file * file, int on) |
| 3476 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3477 | struct snd_pcm_file * pcm_file; |
| 3478 | struct snd_pcm_substream *substream; |
| 3479 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | |
| 3481 | pcm_file = file->private_data; |
| 3482 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3483 | if (PCM_RUNTIME_CHECK(substream)) |
Takashi Iwai | d05468b | 2010-04-07 18:29:46 +0200 | [diff] [blame] | 3484 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | runtime = substream->runtime; |
Takashi Iwai | d05468b | 2010-04-07 18:29:46 +0200 | [diff] [blame] | 3486 | return fasync_helper(fd, file, on, &runtime->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | /* |
| 3490 | * ioctl32 compat |
| 3491 | */ |
| 3492 | #ifdef CONFIG_COMPAT |
| 3493 | #include "pcm_compat.c" |
| 3494 | #else |
| 3495 | #define snd_pcm_ioctl_compat NULL |
| 3496 | #endif |
| 3497 | |
| 3498 | /* |
| 3499 | * To be removed helpers to keep binary compatibility |
| 3500 | */ |
| 3501 | |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 3502 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) |
| 3504 | #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) |
| 3505 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3506 | static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, |
| 3507 | struct snd_pcm_hw_params_old *oparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | { |
| 3509 | unsigned int i; |
| 3510 | |
| 3511 | memset(params, 0, sizeof(*params)); |
| 3512 | params->flags = oparams->flags; |
| 3513 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) |
| 3514 | params->masks[i].bits[0] = oparams->masks[i]; |
| 3515 | memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); |
| 3516 | params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); |
| 3517 | params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); |
| 3518 | params->info = oparams->info; |
| 3519 | params->msbits = oparams->msbits; |
| 3520 | params->rate_num = oparams->rate_num; |
| 3521 | params->rate_den = oparams->rate_den; |
| 3522 | params->fifo_size = oparams->fifo_size; |
| 3523 | } |
| 3524 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3525 | static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, |
| 3526 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | { |
| 3528 | unsigned int i; |
| 3529 | |
| 3530 | memset(oparams, 0, sizeof(*oparams)); |
| 3531 | oparams->flags = params->flags; |
| 3532 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) |
| 3533 | oparams->masks[i] = params->masks[i].bits[0]; |
| 3534 | memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); |
| 3535 | oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); |
| 3536 | oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); |
| 3537 | oparams->info = params->info; |
| 3538 | oparams->msbits = params->msbits; |
| 3539 | oparams->rate_num = params->rate_num; |
| 3540 | oparams->rate_den = params->rate_den; |
| 3541 | oparams->fifo_size = params->fifo_size; |
| 3542 | } |
| 3543 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3544 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
| 3545 | struct snd_pcm_hw_params_old __user * _oparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3547 | struct snd_pcm_hw_params *params; |
| 3548 | struct snd_pcm_hw_params_old *oparams = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | int err; |
| 3550 | |
| 3551 | params = kmalloc(sizeof(*params), GFP_KERNEL); |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3552 | if (!params) |
| 3553 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3555 | oparams = memdup_user(_oparams, sizeof(*oparams)); |
| 3556 | if (IS_ERR(oparams)) { |
| 3557 | err = PTR_ERR(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | goto out; |
| 3559 | } |
| 3560 | snd_pcm_hw_convert_from_old_params(params, oparams); |
| 3561 | err = snd_pcm_hw_refine(substream, params); |
| 3562 | snd_pcm_hw_convert_to_old_params(oparams, params); |
| 3563 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) { |
| 3564 | if (!err) |
| 3565 | err = -EFAULT; |
| 3566 | } |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3567 | |
| 3568 | kfree(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | out: |
| 3570 | kfree(params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | return err; |
| 3572 | } |
| 3573 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3574 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, |
| 3575 | struct snd_pcm_hw_params_old __user * _oparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3577 | struct snd_pcm_hw_params *params; |
| 3578 | struct snd_pcm_hw_params_old *oparams = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | int err; |
| 3580 | |
| 3581 | params = kmalloc(sizeof(*params), GFP_KERNEL); |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3582 | if (!params) |
| 3583 | return -ENOMEM; |
| 3584 | |
| 3585 | oparams = memdup_user(_oparams, sizeof(*oparams)); |
| 3586 | if (IS_ERR(oparams)) { |
| 3587 | err = PTR_ERR(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | goto out; |
| 3589 | } |
| 3590 | snd_pcm_hw_convert_from_old_params(params, oparams); |
| 3591 | err = snd_pcm_hw_params(substream, params); |
| 3592 | snd_pcm_hw_convert_to_old_params(oparams, params); |
| 3593 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) { |
| 3594 | if (!err) |
| 3595 | err = -EFAULT; |
| 3596 | } |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3597 | |
| 3598 | kfree(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | out: |
| 3600 | kfree(params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | return err; |
| 3602 | } |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 3603 | #endif /* CONFIG_SND_SUPPORT_OLD_API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3605 | #ifndef CONFIG_MMU |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3606 | static unsigned long snd_pcm_get_unmapped_area(struct file *file, |
| 3607 | unsigned long addr, |
| 3608 | unsigned long len, |
| 3609 | unsigned long pgoff, |
| 3610 | unsigned long flags) |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3611 | { |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3612 | struct snd_pcm_file *pcm_file = file->private_data; |
| 3613 | struct snd_pcm_substream *substream = pcm_file->substream; |
| 3614 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 3615 | unsigned long offset = pgoff << PAGE_SHIFT; |
| 3616 | |
| 3617 | switch (offset) { |
| 3618 | case SNDRV_PCM_MMAP_OFFSET_STATUS: |
| 3619 | return (unsigned long)runtime->status; |
| 3620 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: |
| 3621 | return (unsigned long)runtime->control; |
| 3622 | default: |
| 3623 | return (unsigned long)runtime->dma_area + offset; |
| 3624 | } |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3625 | } |
| 3626 | #else |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3627 | # define snd_pcm_get_unmapped_area NULL |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3628 | #endif |
| 3629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | /* |
| 3631 | * Register section |
| 3632 | */ |
| 3633 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 3634 | const struct file_operations snd_pcm_f_ops[2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | { |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3636 | .owner = THIS_MODULE, |
| 3637 | .write = snd_pcm_write, |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3638 | .write_iter = snd_pcm_writev, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 3639 | .open = snd_pcm_playback_open, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3640 | .release = snd_pcm_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 3641 | .llseek = no_llseek, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3642 | .poll = snd_pcm_playback_poll, |
| 3643 | .unlocked_ioctl = snd_pcm_playback_ioctl, |
| 3644 | .compat_ioctl = snd_pcm_ioctl_compat, |
| 3645 | .mmap = snd_pcm_mmap, |
| 3646 | .fasync = snd_pcm_fasync, |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3647 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | }, |
| 3649 | { |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3650 | .owner = THIS_MODULE, |
| 3651 | .read = snd_pcm_read, |
Al Viro | 1c65d98 | 2015-04-04 00:19:32 -0400 | [diff] [blame] | 3652 | .read_iter = snd_pcm_readv, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 3653 | .open = snd_pcm_capture_open, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3654 | .release = snd_pcm_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 3655 | .llseek = no_llseek, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3656 | .poll = snd_pcm_capture_poll, |
| 3657 | .unlocked_ioctl = snd_pcm_capture_ioctl, |
| 3658 | .compat_ioctl = snd_pcm_ioctl_compat, |
| 3659 | .mmap = snd_pcm_mmap, |
| 3660 | .fasync = snd_pcm_fasync, |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3661 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | } |
| 3663 | }; |