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