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