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