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