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