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