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