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