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