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