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