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