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