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