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