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