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