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 | } |
Joonwoo Park | ed3bab0 | 2013-02-13 15:59:44 -0800 | [diff] [blame] | 1537 | |
| 1538 | static int snd_user_ioctl(struct snd_pcm_substream *substream, |
| 1539 | unsigned int cmd, void __user *arg) |
| 1540 | { |
| 1541 | struct snd_pcm_runtime *runtime; |
| 1542 | int err = 0; |
| 1543 | |
| 1544 | if (PCM_RUNTIME_CHECK(substream)) |
| 1545 | return -ENXIO; |
| 1546 | runtime = substream->runtime; |
| 1547 | err = substream->ops->ioctl(substream, cmd, arg); |
| 1548 | return err; |
| 1549 | } |
| 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* |
| 1552 | * drop ioctl |
| 1553 | * |
| 1554 | * Immediately put all linked substreams into SETUP state. |
| 1555 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1556 | static int snd_pcm_drop(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1558 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | int result = 0; |
| 1560 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1561 | if (PCM_RUNTIME_CHECK(substream)) |
| 1562 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Takashi Iwai | de1b8b9 | 2006-11-08 15:41:29 +0100 | [diff] [blame] | 1565 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN || |
Takashi Iwai | 24e8fc4 | 2008-09-25 17:51:11 +0200 | [diff] [blame] | 1566 | runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED || |
| 1567 | runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | return -EBADFD; |
| 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | snd_pcm_stream_lock_irq(substream); |
| 1571 | /* resume pause */ |
| 1572 | if (runtime->status->state == SNDRV_PCM_STATE_PAUSED) |
| 1573 | snd_pcm_pause(substream, 0); |
| 1574 | |
| 1575 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| 1576 | /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ |
| 1577 | snd_pcm_stream_unlock_irq(substream); |
Takashi Iwai | 24e8fc4 | 2008-09-25 17:51:11 +0200 | [diff] [blame] | 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | return result; |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | /* WARNING: Don't forget to fput back the file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | static struct file *snd_pcm_file_fd(int fd) |
| 1585 | { |
| 1586 | struct file *file; |
| 1587 | struct inode *inode; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1588 | unsigned int minor; |
| 1589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | file = fget(fd); |
| 1591 | if (!file) |
| 1592 | return NULL; |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 1593 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | if (!S_ISCHR(inode->i_mode) || |
| 1595 | imajor(inode) != snd_major) { |
| 1596 | fput(file); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | minor = iminor(inode); |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1600 | if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) && |
| 1601 | !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | fput(file); |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | return file; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * PCM link handling |
| 1610 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1611 | static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
| 1613 | int res = 0; |
| 1614 | struct file *file; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1615 | struct snd_pcm_file *pcm_file; |
| 1616 | struct snd_pcm_substream *substream1; |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1617 | struct snd_pcm_group *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | file = snd_pcm_file_fd(fd); |
| 1620 | if (!file) |
| 1621 | return -EBADFD; |
| 1622 | pcm_file = file->private_data; |
| 1623 | substream1 = pcm_file->substream; |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1624 | group = kmalloc(sizeof(*group), GFP_KERNEL); |
| 1625 | if (!group) { |
| 1626 | res = -ENOMEM; |
| 1627 | goto _nolock; |
| 1628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | down_write(&snd_pcm_link_rwsem); |
| 1630 | write_lock_irq(&snd_pcm_link_rwlock); |
| 1631 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN || |
| 1632 | substream->runtime->status->state != substream1->runtime->status->state) { |
| 1633 | res = -EBADFD; |
| 1634 | goto _end; |
| 1635 | } |
| 1636 | if (snd_pcm_stream_linked(substream1)) { |
| 1637 | res = -EALREADY; |
| 1638 | goto _end; |
| 1639 | } |
| 1640 | if (!snd_pcm_stream_linked(substream)) { |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1641 | substream->group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | spin_lock_init(&substream->group->lock); |
| 1643 | INIT_LIST_HEAD(&substream->group->substreams); |
| 1644 | list_add_tail(&substream->link_list, &substream->group->substreams); |
| 1645 | substream->group->count = 1; |
| 1646 | } |
| 1647 | list_add_tail(&substream1->link_list, &substream->group->substreams); |
| 1648 | substream->group->count++; |
| 1649 | substream1->group = substream->group; |
| 1650 | _end: |
| 1651 | write_unlock_irq(&snd_pcm_link_rwlock); |
| 1652 | up_write(&snd_pcm_link_rwsem); |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1653 | _nolock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | fput(file); |
Takashi Iwai | 1662591 | 2012-03-13 15:55:43 +0100 | [diff] [blame] | 1655 | if (res < 0) |
| 1656 | kfree(group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | return res; |
| 1658 | } |
| 1659 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1660 | static void relink_to_local(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | { |
| 1662 | substream->group = &substream->self_group; |
| 1663 | INIT_LIST_HEAD(&substream->self_group.substreams); |
| 1664 | list_add_tail(&substream->link_list, &substream->self_group.substreams); |
| 1665 | } |
| 1666 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1667 | static int snd_pcm_unlink(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | { |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 1669 | struct snd_pcm_substream *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | int res = 0; |
| 1671 | |
| 1672 | down_write(&snd_pcm_link_rwsem); |
| 1673 | write_lock_irq(&snd_pcm_link_rwlock); |
| 1674 | if (!snd_pcm_stream_linked(substream)) { |
| 1675 | res = -EALREADY; |
| 1676 | goto _end; |
| 1677 | } |
| 1678 | list_del(&substream->link_list); |
| 1679 | substream->group->count--; |
| 1680 | if (substream->group->count == 1) { /* detach the last stream, too */ |
Takashi Iwai | ef991b9 | 2007-02-22 12:52:53 +0100 | [diff] [blame] | 1681 | snd_pcm_group_for_each_entry(s, substream) { |
| 1682 | relink_to_local(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | break; |
| 1684 | } |
| 1685 | kfree(substream->group); |
| 1686 | } |
| 1687 | relink_to_local(substream); |
| 1688 | _end: |
| 1689 | write_unlock_irq(&snd_pcm_link_rwlock); |
| 1690 | up_write(&snd_pcm_link_rwsem); |
| 1691 | return res; |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * hw configurator |
| 1696 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1697 | static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, |
| 1698 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1700 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), |
| 1702 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1703 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1704 | } |
| 1705 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1706 | static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, |
| 1707 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1709 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | snd_interval_div(hw_param_interval_c(params, rule->deps[0]), |
| 1711 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1712 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1713 | } |
| 1714 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1715 | static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, |
| 1716 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1718 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), |
| 1720 | hw_param_interval_c(params, rule->deps[1]), |
| 1721 | (unsigned long) rule->private, &t); |
| 1722 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1723 | } |
| 1724 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1725 | static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, |
| 1726 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1728 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), |
| 1730 | (unsigned long) rule->private, |
| 1731 | hw_param_interval_c(params, rule->deps[1]), &t); |
| 1732 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1733 | } |
| 1734 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1735 | static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, |
| 1736 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
| 1738 | unsigned int k; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1739 | struct snd_interval *i = hw_param_interval(params, rule->deps[0]); |
| 1740 | struct snd_mask m; |
| 1741 | 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] | 1742 | snd_mask_any(&m); |
| 1743 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { |
| 1744 | int bits; |
| 1745 | if (! snd_mask_test(mask, k)) |
| 1746 | continue; |
| 1747 | bits = snd_pcm_format_physical_width(k); |
| 1748 | if (bits <= 0) |
| 1749 | continue; /* ignore invalid formats */ |
| 1750 | if ((unsigned)bits < i->min || (unsigned)bits > i->max) |
| 1751 | snd_mask_reset(&m, k); |
| 1752 | } |
| 1753 | return snd_mask_refine(mask, &m); |
| 1754 | } |
| 1755 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1756 | static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, |
| 1757 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1759 | struct snd_interval t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | unsigned int k; |
| 1761 | t.min = UINT_MAX; |
| 1762 | t.max = 0; |
| 1763 | t.openmin = 0; |
| 1764 | t.openmax = 0; |
| 1765 | for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) { |
| 1766 | int bits; |
| 1767 | if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) |
| 1768 | continue; |
| 1769 | bits = snd_pcm_format_physical_width(k); |
| 1770 | if (bits <= 0) |
| 1771 | continue; /* ignore invalid formats */ |
| 1772 | if (t.min > (unsigned)bits) |
| 1773 | t.min = bits; |
| 1774 | if (t.max < (unsigned)bits) |
| 1775 | t.max = bits; |
| 1776 | } |
| 1777 | t.integer = 1; |
| 1778 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1779 | } |
| 1780 | |
| 1781 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 |
| 1782 | #error "Change this table" |
| 1783 | #endif |
| 1784 | |
| 1785 | static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
| 1786 | 48000, 64000, 88200, 96000, 176400, 192000 }; |
| 1787 | |
Clemens Ladisch | 7653d55 | 2007-08-13 17:38:54 +0200 | [diff] [blame] | 1788 | const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { |
| 1789 | .count = ARRAY_SIZE(rates), |
| 1790 | .list = rates, |
| 1791 | }; |
| 1792 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1793 | static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1794 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1796 | struct snd_pcm_hardware *hw = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | return snd_interval_list(hw_param_interval(params, rule->var), |
Clemens Ladisch | 7653d55 | 2007-08-13 17:38:54 +0200 | [diff] [blame] | 1798 | snd_pcm_known_rates.count, |
| 1799 | snd_pcm_known_rates.list, hw->rates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1802 | static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, |
| 1803 | struct snd_pcm_hw_rule *rule) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1805 | struct snd_interval t; |
| 1806 | struct snd_pcm_substream *substream = rule->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | t.min = 0; |
| 1808 | t.max = substream->buffer_bytes_max; |
| 1809 | t.openmin = 0; |
| 1810 | t.openmax = 0; |
| 1811 | t.integer = 1; |
| 1812 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 1813 | } |
| 1814 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1815 | int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1817 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1818 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | int k, err; |
| 1820 | |
| 1821 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { |
| 1822 | snd_mask_any(constrs_mask(constrs, k)); |
| 1823 | } |
| 1824 | |
| 1825 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { |
| 1826 | snd_interval_any(constrs_interval(constrs, k)); |
| 1827 | } |
| 1828 | |
| 1829 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); |
| 1830 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); |
| 1831 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); |
| 1832 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); |
| 1833 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); |
| 1834 | |
| 1835 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 1836 | snd_pcm_hw_rule_format, NULL, |
| 1837 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1838 | if (err < 0) |
| 1839 | return err; |
| 1840 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 1841 | snd_pcm_hw_rule_sample_bits, NULL, |
| 1842 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1843 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1844 | if (err < 0) |
| 1845 | return err; |
| 1846 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 1847 | snd_pcm_hw_rule_div, NULL, |
| 1848 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 1849 | if (err < 0) |
| 1850 | return err; |
| 1851 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 1852 | snd_pcm_hw_rule_mul, NULL, |
| 1853 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 1854 | if (err < 0) |
| 1855 | return err; |
| 1856 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 1857 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 1858 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); |
| 1859 | if (err < 0) |
| 1860 | return err; |
| 1861 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 1862 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 1863 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); |
| 1864 | if (err < 0) |
| 1865 | return err; |
| 1866 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1867 | snd_pcm_hw_rule_div, NULL, |
| 1868 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); |
| 1869 | if (err < 0) |
| 1870 | return err; |
| 1871 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1872 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 1873 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); |
| 1874 | if (err < 0) |
| 1875 | return err; |
| 1876 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1877 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 1878 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); |
| 1879 | if (err < 0) |
| 1880 | return err; |
| 1881 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, |
| 1882 | snd_pcm_hw_rule_div, NULL, |
| 1883 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); |
| 1884 | if (err < 0) |
| 1885 | return err; |
| 1886 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1887 | snd_pcm_hw_rule_div, NULL, |
| 1888 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); |
| 1889 | if (err < 0) |
| 1890 | return err; |
| 1891 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1892 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 1893 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 1894 | if (err < 0) |
| 1895 | return err; |
| 1896 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1897 | snd_pcm_hw_rule_muldivk, (void*) 1000000, |
| 1898 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 1899 | if (err < 0) |
| 1900 | return err; |
| 1901 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1902 | snd_pcm_hw_rule_mul, NULL, |
| 1903 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); |
| 1904 | if (err < 0) |
| 1905 | return err; |
| 1906 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1907 | snd_pcm_hw_rule_mulkdiv, (void*) 8, |
| 1908 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 1909 | if (err < 0) |
| 1910 | return err; |
| 1911 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 1912 | snd_pcm_hw_rule_muldivk, (void*) 1000000, |
| 1913 | SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 1914 | if (err < 0) |
| 1915 | return err; |
| 1916 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 1917 | snd_pcm_hw_rule_muldivk, (void*) 8, |
| 1918 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 1919 | if (err < 0) |
| 1920 | return err; |
| 1921 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 1922 | snd_pcm_hw_rule_muldivk, (void*) 8, |
| 1923 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); |
| 1924 | if (err < 0) |
| 1925 | return err; |
| 1926 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1927 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 1928 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 1929 | if (err < 0) |
| 1930 | return err; |
| 1931 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, |
| 1932 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, |
| 1933 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); |
| 1934 | if (err < 0) |
| 1935 | return err; |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1939 | int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 1941 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1942 | struct snd_pcm_hardware *hw = &runtime->hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | int err; |
| 1944 | unsigned int mask = 0; |
| 1945 | |
| 1946 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
| 1947 | mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED; |
| 1948 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
| 1949 | mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED; |
| 1950 | if (hw->info & SNDRV_PCM_INFO_MMAP) { |
| 1951 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
| 1952 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED; |
| 1953 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
| 1954 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED; |
| 1955 | if (hw->info & SNDRV_PCM_INFO_COMPLEX) |
| 1956 | mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX; |
| 1957 | } |
| 1958 | 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] | 1959 | if (err < 0) |
| 1960 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
| 1962 | 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] | 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_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1967 | if (err < 0) |
| 1968 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
| 1970 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1971 | hw->channels_min, hw->channels_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1972 | if (err < 0) |
| 1973 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | |
| 1975 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, |
| 1976 | hw->rate_min, hw->rate_max); |
Guennadi Liakhovetski | 8b90ca0 | 2009-12-24 01:17:46 +0100 | [diff] [blame] | 1977 | if (err < 0) |
| 1978 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
| 1980 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 1981 | hw->period_bytes_min, hw->period_bytes_max); |
Guennadi Liakhovetski | 8b90ca0 | 2009-12-24 01:17:46 +0100 | [diff] [blame] | 1982 | if (err < 0) |
| 1983 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | |
| 1985 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, |
| 1986 | hw->periods_min, hw->periods_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1987 | if (err < 0) |
| 1988 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
| 1990 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 1991 | hw->period_bytes_min, hw->buffer_bytes_max); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1992 | if (err < 0) |
| 1993 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
| 1995 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 1996 | snd_pcm_hw_rule_buffer_bytes_max, substream, |
| 1997 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); |
| 1998 | if (err < 0) |
| 1999 | return err; |
| 2000 | |
| 2001 | /* FIXME: remove */ |
| 2002 | if (runtime->dma_bytes) { |
| 2003 | 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] | 2004 | if (err < 0) |
| 2005 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { |
| 2009 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 2010 | snd_pcm_hw_rule_rate, hw, |
| 2011 | SNDRV_PCM_HW_PARAM_RATE, -1); |
| 2012 | if (err < 0) |
| 2013 | return err; |
| 2014 | } |
| 2015 | |
| 2016 | /* FIXME: this belong to lowlevel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 2018 | |
| 2019 | return 0; |
| 2020 | } |
| 2021 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2022 | static void pcm_release_private(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | snd_pcm_unlink(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | void snd_pcm_release_substream(struct snd_pcm_substream *substream) |
| 2028 | { |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2029 | substream->ref_count--; |
| 2030 | if (substream->ref_count > 0) |
| 2031 | return; |
| 2032 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2033 | snd_pcm_drop(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2034 | if (substream->hw_opened) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | if (substream->ops->hw_free != NULL) |
| 2036 | substream->ops->hw_free(substream); |
| 2037 | substream->ops->close(substream); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2038 | substream->hw_opened = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | } |
Takashi Iwai | 8699a0b | 2010-09-16 22:52:32 +0200 | [diff] [blame] | 2040 | if (pm_qos_request_active(&substream->latency_pm_qos_req)) |
| 2041 | pm_qos_remove_request(&substream->latency_pm_qos_req); |
Takashi Iwai | 1576274 | 2006-04-06 19:47:42 +0200 | [diff] [blame] | 2042 | if (substream->pcm_release) { |
| 2043 | substream->pcm_release(substream); |
| 2044 | substream->pcm_release = NULL; |
| 2045 | } |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2046 | snd_pcm_detach_substream(substream); |
| 2047 | } |
| 2048 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2049 | EXPORT_SYMBOL(snd_pcm_release_substream); |
| 2050 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2051 | int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, |
| 2052 | struct file *file, |
| 2053 | struct snd_pcm_substream **rsubstream) |
| 2054 | { |
| 2055 | struct snd_pcm_substream *substream; |
| 2056 | int err; |
| 2057 | |
| 2058 | err = snd_pcm_attach_substream(pcm, stream, file, &substream); |
| 2059 | if (err < 0) |
| 2060 | return err; |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2061 | if (substream->ref_count > 1) { |
| 2062 | *rsubstream = substream; |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2066 | err = snd_pcm_hw_constraints_init(substream); |
| 2067 | if (err < 0) { |
| 2068 | snd_printd("snd_pcm_hw_constraints_init failed\n"); |
| 2069 | goto error; |
| 2070 | } |
| 2071 | |
Liam Girdwood | 8ccfb4a | 2011-02-16 16:50:12 +0000 | [diff] [blame] | 2072 | if (substream->ops == NULL) { |
| 2073 | snd_printd("cannot open back end PCMs directly\n"); |
| 2074 | err = -ENODEV; |
| 2075 | goto error; |
| 2076 | } |
| 2077 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2078 | if ((err = substream->ops->open(substream)) < 0) |
| 2079 | goto error; |
| 2080 | |
| 2081 | substream->hw_opened = 1; |
| 2082 | |
| 2083 | err = snd_pcm_hw_constraints_complete(substream); |
| 2084 | if (err < 0) { |
| 2085 | snd_printd("snd_pcm_hw_constraints_complete failed\n"); |
| 2086 | goto error; |
| 2087 | } |
| 2088 | |
| 2089 | *rsubstream = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | return 0; |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2091 | |
| 2092 | error: |
| 2093 | snd_pcm_release_substream(substream); |
| 2094 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2097 | EXPORT_SYMBOL(snd_pcm_open_substream); |
| 2098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | static int snd_pcm_open_file(struct file *file, |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2100 | struct snd_pcm *pcm, |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2101 | int stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2103 | struct snd_pcm_file *pcm_file; |
| 2104 | struct snd_pcm_substream *substream; |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2105 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2107 | err = snd_pcm_open_substream(pcm, stream, file, &substream); |
| 2108 | if (err < 0) |
| 2109 | return err; |
| 2110 | |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 2111 | pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); |
| 2112 | if (pcm_file == NULL) { |
| 2113 | snd_pcm_release_substream(substream); |
| 2114 | return -ENOMEM; |
| 2115 | } |
| 2116 | pcm_file->substream = substream; |
| 2117 | if (substream->ref_count == 1) { |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2118 | substream->file = pcm_file; |
| 2119 | substream->pcm_release = pcm_release_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | file->private_data = pcm_file; |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | return 0; |
| 2124 | } |
| 2125 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2126 | static int snd_pcm_playback_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2128 | struct snd_pcm *pcm; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2129 | int err = nonseekable_open(inode, file); |
| 2130 | if (err < 0) |
| 2131 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2132 | pcm = snd_lookup_minor_data(iminor(inode), |
| 2133 | SNDRV_DEVICE_TYPE_PCM_PLAYBACK); |
| 2134 | return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); |
| 2135 | } |
| 2136 | |
| 2137 | static int snd_pcm_capture_open(struct inode *inode, struct file *file) |
| 2138 | { |
| 2139 | struct snd_pcm *pcm; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2140 | int err = nonseekable_open(inode, file); |
| 2141 | if (err < 0) |
| 2142 | return err; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 2143 | pcm = snd_lookup_minor_data(iminor(inode), |
| 2144 | SNDRV_DEVICE_TYPE_PCM_CAPTURE); |
| 2145 | return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); |
| 2146 | } |
| 2147 | |
| 2148 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) |
| 2149 | { |
| 2150 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | wait_queue_t wait; |
| 2152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | if (pcm == NULL) { |
| 2154 | err = -ENODEV; |
| 2155 | goto __error1; |
| 2156 | } |
| 2157 | err = snd_card_file_add(pcm->card, file); |
| 2158 | if (err < 0) |
| 2159 | goto __error1; |
| 2160 | if (!try_module_get(pcm->card->module)) { |
| 2161 | err = -EFAULT; |
| 2162 | goto __error2; |
| 2163 | } |
| 2164 | init_waitqueue_entry(&wait, current); |
| 2165 | add_wait_queue(&pcm->open_wait, &wait); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2166 | mutex_lock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | while (1) { |
Feng Tang | ffd3d5c | 2011-10-10 10:31:48 +0800 | [diff] [blame] | 2168 | err = snd_pcm_open_file(file, pcm, stream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | if (err >= 0) |
| 2170 | break; |
| 2171 | if (err == -EAGAIN) { |
| 2172 | if (file->f_flags & O_NONBLOCK) { |
| 2173 | err = -EBUSY; |
| 2174 | break; |
| 2175 | } |
| 2176 | } else |
| 2177 | break; |
| 2178 | set_current_state(TASK_INTERRUPTIBLE); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2179 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | schedule(); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2181 | mutex_lock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | if (signal_pending(current)) { |
| 2183 | err = -ERESTARTSYS; |
| 2184 | break; |
| 2185 | } |
| 2186 | } |
| 2187 | remove_wait_queue(&pcm->open_wait, &wait); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2188 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | if (err < 0) |
| 2190 | goto __error; |
| 2191 | return err; |
| 2192 | |
| 2193 | __error: |
| 2194 | module_put(pcm->card->module); |
| 2195 | __error2: |
| 2196 | snd_card_file_remove(pcm->card, file); |
| 2197 | __error1: |
| 2198 | return err; |
| 2199 | } |
| 2200 | |
| 2201 | static int snd_pcm_release(struct inode *inode, struct file *file) |
| 2202 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2203 | struct snd_pcm *pcm; |
| 2204 | struct snd_pcm_substream *substream; |
| 2205 | struct snd_pcm_file *pcm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
| 2207 | pcm_file = file->private_data; |
| 2208 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2209 | if (snd_BUG_ON(!substream)) |
| 2210 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | pcm = substream->pcm; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2212 | mutex_lock(&pcm->open_mutex); |
Takashi Iwai | 3bf75f9 | 2006-03-27 16:40:49 +0200 | [diff] [blame] | 2213 | snd_pcm_release_substream(substream); |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 2214 | kfree(pcm_file); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 2215 | mutex_unlock(&pcm->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | wake_up(&pcm->open_wait); |
| 2217 | module_put(pcm->card->module); |
| 2218 | snd_card_file_remove(pcm->card, file); |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2222 | static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream, |
| 2223 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2225 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | snd_pcm_sframes_t appl_ptr; |
| 2227 | snd_pcm_sframes_t ret; |
| 2228 | snd_pcm_sframes_t hw_avail; |
| 2229 | |
| 2230 | if (frames == 0) |
| 2231 | return 0; |
| 2232 | |
| 2233 | snd_pcm_stream_lock_irq(substream); |
| 2234 | switch (runtime->status->state) { |
| 2235 | case SNDRV_PCM_STATE_PREPARED: |
| 2236 | break; |
| 2237 | case SNDRV_PCM_STATE_DRAINING: |
| 2238 | case SNDRV_PCM_STATE_RUNNING: |
| 2239 | if (snd_pcm_update_hw_ptr(substream) >= 0) |
| 2240 | break; |
| 2241 | /* Fall through */ |
| 2242 | case SNDRV_PCM_STATE_XRUN: |
| 2243 | ret = -EPIPE; |
| 2244 | goto __end; |
Lubomir Rintel | 5184040 | 2009-08-02 18:14:44 +0200 | [diff] [blame] | 2245 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2246 | ret = -ESTRPIPE; |
| 2247 | goto __end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | default: |
| 2249 | ret = -EBADFD; |
| 2250 | goto __end; |
| 2251 | } |
| 2252 | |
| 2253 | hw_avail = snd_pcm_playback_hw_avail(runtime); |
| 2254 | if (hw_avail <= 0) { |
| 2255 | ret = 0; |
| 2256 | goto __end; |
| 2257 | } |
| 2258 | if (frames > (snd_pcm_uframes_t)hw_avail) |
| 2259 | frames = hw_avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | appl_ptr = runtime->control->appl_ptr - frames; |
| 2261 | if (appl_ptr < 0) |
| 2262 | appl_ptr += runtime->boundary; |
| 2263 | runtime->control->appl_ptr = appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | ret = frames; |
| 2265 | __end: |
| 2266 | snd_pcm_stream_unlock_irq(substream); |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2270 | static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream, |
| 2271 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2273 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | snd_pcm_sframes_t appl_ptr; |
| 2275 | snd_pcm_sframes_t ret; |
| 2276 | snd_pcm_sframes_t hw_avail; |
| 2277 | |
| 2278 | if (frames == 0) |
| 2279 | return 0; |
| 2280 | |
| 2281 | snd_pcm_stream_lock_irq(substream); |
| 2282 | switch (runtime->status->state) { |
| 2283 | case SNDRV_PCM_STATE_PREPARED: |
| 2284 | case SNDRV_PCM_STATE_DRAINING: |
| 2285 | break; |
| 2286 | case SNDRV_PCM_STATE_RUNNING: |
| 2287 | if (snd_pcm_update_hw_ptr(substream) >= 0) |
| 2288 | break; |
| 2289 | /* Fall through */ |
| 2290 | case SNDRV_PCM_STATE_XRUN: |
| 2291 | ret = -EPIPE; |
| 2292 | goto __end; |
Lubomir Rintel | 5184040 | 2009-08-02 18:14:44 +0200 | [diff] [blame] | 2293 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2294 | ret = -ESTRPIPE; |
| 2295 | goto __end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | default: |
| 2297 | ret = -EBADFD; |
| 2298 | goto __end; |
| 2299 | } |
| 2300 | |
| 2301 | hw_avail = snd_pcm_capture_hw_avail(runtime); |
| 2302 | if (hw_avail <= 0) { |
| 2303 | ret = 0; |
| 2304 | goto __end; |
| 2305 | } |
| 2306 | if (frames > (snd_pcm_uframes_t)hw_avail) |
| 2307 | frames = hw_avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | appl_ptr = runtime->control->appl_ptr - frames; |
| 2309 | if (appl_ptr < 0) |
| 2310 | appl_ptr += runtime->boundary; |
| 2311 | runtime->control->appl_ptr = appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | ret = frames; |
| 2313 | __end: |
| 2314 | snd_pcm_stream_unlock_irq(substream); |
| 2315 | return ret; |
| 2316 | } |
| 2317 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2318 | static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream, |
| 2319 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2321 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | snd_pcm_sframes_t appl_ptr; |
| 2323 | snd_pcm_sframes_t ret; |
| 2324 | snd_pcm_sframes_t avail; |
| 2325 | |
| 2326 | if (frames == 0) |
| 2327 | return 0; |
| 2328 | |
| 2329 | snd_pcm_stream_lock_irq(substream); |
| 2330 | switch (runtime->status->state) { |
| 2331 | case SNDRV_PCM_STATE_PREPARED: |
| 2332 | case SNDRV_PCM_STATE_PAUSED: |
| 2333 | break; |
| 2334 | case SNDRV_PCM_STATE_DRAINING: |
| 2335 | case SNDRV_PCM_STATE_RUNNING: |
| 2336 | if (snd_pcm_update_hw_ptr(substream) >= 0) |
| 2337 | break; |
| 2338 | /* Fall through */ |
| 2339 | case SNDRV_PCM_STATE_XRUN: |
| 2340 | ret = -EPIPE; |
| 2341 | goto __end; |
Lubomir Rintel | 5184040 | 2009-08-02 18:14:44 +0200 | [diff] [blame] | 2342 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2343 | ret = -ESTRPIPE; |
| 2344 | goto __end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | default: |
| 2346 | ret = -EBADFD; |
| 2347 | goto __end; |
| 2348 | } |
| 2349 | |
| 2350 | avail = snd_pcm_playback_avail(runtime); |
| 2351 | if (avail <= 0) { |
| 2352 | ret = 0; |
| 2353 | goto __end; |
| 2354 | } |
| 2355 | if (frames > (snd_pcm_uframes_t)avail) |
| 2356 | frames = avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | appl_ptr = runtime->control->appl_ptr + frames; |
| 2358 | if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) |
| 2359 | appl_ptr -= runtime->boundary; |
| 2360 | runtime->control->appl_ptr = appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | ret = frames; |
| 2362 | __end: |
| 2363 | snd_pcm_stream_unlock_irq(substream); |
| 2364 | return ret; |
| 2365 | } |
| 2366 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2367 | static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream, |
| 2368 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2370 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | snd_pcm_sframes_t appl_ptr; |
| 2372 | snd_pcm_sframes_t ret; |
| 2373 | snd_pcm_sframes_t avail; |
| 2374 | |
| 2375 | if (frames == 0) |
| 2376 | return 0; |
| 2377 | |
| 2378 | snd_pcm_stream_lock_irq(substream); |
| 2379 | switch (runtime->status->state) { |
| 2380 | case SNDRV_PCM_STATE_PREPARED: |
| 2381 | case SNDRV_PCM_STATE_DRAINING: |
| 2382 | case SNDRV_PCM_STATE_PAUSED: |
| 2383 | break; |
| 2384 | case SNDRV_PCM_STATE_RUNNING: |
| 2385 | if (snd_pcm_update_hw_ptr(substream) >= 0) |
| 2386 | break; |
| 2387 | /* Fall through */ |
| 2388 | case SNDRV_PCM_STATE_XRUN: |
| 2389 | ret = -EPIPE; |
| 2390 | goto __end; |
Lubomir Rintel | 5184040 | 2009-08-02 18:14:44 +0200 | [diff] [blame] | 2391 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2392 | ret = -ESTRPIPE; |
| 2393 | goto __end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | default: |
| 2395 | ret = -EBADFD; |
| 2396 | goto __end; |
| 2397 | } |
| 2398 | |
| 2399 | avail = snd_pcm_capture_avail(runtime); |
| 2400 | if (avail <= 0) { |
| 2401 | ret = 0; |
| 2402 | goto __end; |
| 2403 | } |
| 2404 | if (frames > (snd_pcm_uframes_t)avail) |
| 2405 | frames = avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | appl_ptr = runtime->control->appl_ptr + frames; |
| 2407 | if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) |
| 2408 | appl_ptr -= runtime->boundary; |
| 2409 | runtime->control->appl_ptr = appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | ret = frames; |
| 2411 | __end: |
| 2412 | snd_pcm_stream_unlock_irq(substream); |
| 2413 | return ret; |
| 2414 | } |
| 2415 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2416 | static int snd_pcm_hwsync(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2418 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | int err; |
| 2420 | |
| 2421 | snd_pcm_stream_lock_irq(substream); |
| 2422 | switch (runtime->status->state) { |
| 2423 | case SNDRV_PCM_STATE_DRAINING: |
| 2424 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 2425 | goto __badfd; |
| 2426 | case SNDRV_PCM_STATE_RUNNING: |
| 2427 | if ((err = snd_pcm_update_hw_ptr(substream)) < 0) |
| 2428 | break; |
| 2429 | /* Fall through */ |
| 2430 | case SNDRV_PCM_STATE_PREPARED: |
| 2431 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2432 | err = 0; |
| 2433 | break; |
| 2434 | case SNDRV_PCM_STATE_XRUN: |
| 2435 | err = -EPIPE; |
| 2436 | break; |
| 2437 | default: |
| 2438 | __badfd: |
| 2439 | err = -EBADFD; |
| 2440 | break; |
| 2441 | } |
| 2442 | snd_pcm_stream_unlock_irq(substream); |
| 2443 | return err; |
| 2444 | } |
| 2445 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2446 | static int snd_pcm_delay(struct snd_pcm_substream *substream, |
| 2447 | snd_pcm_sframes_t __user *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2449 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | int err; |
| 2451 | snd_pcm_sframes_t n = 0; |
| 2452 | |
| 2453 | snd_pcm_stream_lock_irq(substream); |
| 2454 | switch (runtime->status->state) { |
| 2455 | case SNDRV_PCM_STATE_DRAINING: |
| 2456 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 2457 | goto __badfd; |
| 2458 | case SNDRV_PCM_STATE_RUNNING: |
| 2459 | if ((err = snd_pcm_update_hw_ptr(substream)) < 0) |
| 2460 | break; |
| 2461 | /* Fall through */ |
| 2462 | case SNDRV_PCM_STATE_PREPARED: |
| 2463 | case SNDRV_PCM_STATE_SUSPENDED: |
| 2464 | err = 0; |
| 2465 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 2466 | n = snd_pcm_playback_hw_avail(runtime); |
| 2467 | else |
| 2468 | n = snd_pcm_capture_avail(runtime); |
Takashi Iwai | 4bbe1dd | 2008-10-13 03:07:14 +0200 | [diff] [blame] | 2469 | n += runtime->delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | break; |
| 2471 | case SNDRV_PCM_STATE_XRUN: |
| 2472 | err = -EPIPE; |
| 2473 | break; |
| 2474 | default: |
| 2475 | __badfd: |
| 2476 | err = -EBADFD; |
| 2477 | break; |
| 2478 | } |
| 2479 | snd_pcm_stream_unlock_irq(substream); |
| 2480 | if (!err) |
| 2481 | if (put_user(n, res)) |
| 2482 | err = -EFAULT; |
| 2483 | return err; |
| 2484 | } |
| 2485 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2486 | static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, |
| 2487 | struct snd_pcm_sync_ptr __user *_sync_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2489 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2490 | struct snd_pcm_sync_ptr sync_ptr; |
| 2491 | volatile struct snd_pcm_mmap_status *status; |
| 2492 | volatile struct snd_pcm_mmap_control *control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | int err; |
Santosh Mardi | c4fac02 | 2012-10-09 10:32:42 +0530 | [diff] [blame] | 2494 | snd_pcm_uframes_t hw_avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
| 2496 | memset(&sync_ptr, 0, sizeof(sync_ptr)); |
| 2497 | if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) |
| 2498 | return -EFAULT; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2499 | 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] | 2500 | return -EFAULT; |
| 2501 | status = runtime->status; |
| 2502 | control = runtime->control; |
| 2503 | if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { |
| 2504 | err = snd_pcm_hwsync(substream); |
| 2505 | if (err < 0) |
| 2506 | return err; |
| 2507 | } |
| 2508 | snd_pcm_stream_lock_irq(substream); |
| 2509 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) |
| 2510 | control->appl_ptr = sync_ptr.c.control.appl_ptr; |
| 2511 | else |
| 2512 | sync_ptr.c.control.appl_ptr = control->appl_ptr; |
| 2513 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) |
| 2514 | control->avail_min = sync_ptr.c.control.avail_min; |
| 2515 | else |
| 2516 | sync_ptr.c.control.avail_min = control->avail_min; |
Santosh Mardi | c4fac02 | 2012-10-09 10:32:42 +0530 | [diff] [blame] | 2517 | |
| 2518 | if (runtime->render_flag & SNDRV_NON_DMA_MODE) { |
| 2519 | hw_avail = snd_pcm_playback_hw_avail(runtime); |
| 2520 | if ((hw_avail >= runtime->start_threshold) |
| 2521 | && (runtime->render_flag & |
| 2522 | SNDRV_RENDER_STOPPED)) { |
| 2523 | if (substream->ops->restart) |
| 2524 | substream->ops->restart(substream); |
| 2525 | } |
| 2526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | sync_ptr.s.status.state = status->state; |
| 2528 | sync_ptr.s.status.hw_ptr = status->hw_ptr; |
| 2529 | sync_ptr.s.status.tstamp = status->tstamp; |
| 2530 | sync_ptr.s.status.suspended_state = status->suspended_state; |
| 2531 | snd_pcm_stream_unlock_irq(substream); |
| 2532 | if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) |
| 2533 | return -EFAULT; |
| 2534 | return 0; |
| 2535 | } |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 2536 | |
| 2537 | static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) |
| 2538 | { |
| 2539 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 2540 | int arg; |
| 2541 | |
| 2542 | if (get_user(arg, _arg)) |
| 2543 | return -EFAULT; |
| 2544 | if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) |
| 2545 | return -EINVAL; |
| 2546 | runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY; |
| 2547 | if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC) |
| 2548 | runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC; |
| 2549 | return 0; |
| 2550 | } |
Krishnankutty Kolathappilly | 6f52d71 | 2012-08-28 15:47:48 -0700 | [diff] [blame] | 2551 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2552 | static int snd_pcm_common_ioctl1(struct file *file, |
| 2553 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | unsigned int cmd, void __user *arg) |
| 2555 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | switch (cmd) { |
| 2557 | case SNDRV_PCM_IOCTL_PVERSION: |
| 2558 | return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; |
| 2559 | case SNDRV_PCM_IOCTL_INFO: |
| 2560 | return snd_pcm_info_user(substream, arg); |
Jaroslav Kysela | 28e9e47 | 2007-12-17 09:02:22 +0100 | [diff] [blame] | 2561 | case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ |
| 2562 | return 0; |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 2563 | case SNDRV_PCM_IOCTL_TTSTAMP: |
| 2564 | return snd_pcm_tstamp(substream, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | case SNDRV_PCM_IOCTL_HW_REFINE: |
| 2566 | return snd_pcm_hw_refine_user(substream, arg); |
| 2567 | case SNDRV_PCM_IOCTL_HW_PARAMS: |
| 2568 | return snd_pcm_hw_params_user(substream, arg); |
| 2569 | case SNDRV_PCM_IOCTL_HW_FREE: |
| 2570 | return snd_pcm_hw_free(substream); |
| 2571 | case SNDRV_PCM_IOCTL_SW_PARAMS: |
| 2572 | return snd_pcm_sw_params_user(substream, arg); |
| 2573 | case SNDRV_PCM_IOCTL_STATUS: |
| 2574 | return snd_pcm_status_user(substream, arg); |
| 2575 | case SNDRV_PCM_IOCTL_CHANNEL_INFO: |
| 2576 | return snd_pcm_channel_info_user(substream, arg); |
| 2577 | case SNDRV_PCM_IOCTL_PREPARE: |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2578 | return snd_pcm_prepare(substream, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | case SNDRV_PCM_IOCTL_RESET: |
| 2580 | return snd_pcm_reset(substream); |
| 2581 | case SNDRV_PCM_IOCTL_START: |
| 2582 | return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING); |
| 2583 | case SNDRV_PCM_IOCTL_LINK: |
| 2584 | return snd_pcm_link(substream, (int)(unsigned long) arg); |
| 2585 | case SNDRV_PCM_IOCTL_UNLINK: |
| 2586 | return snd_pcm_unlink(substream); |
| 2587 | case SNDRV_PCM_IOCTL_RESUME: |
| 2588 | return snd_pcm_resume(substream); |
| 2589 | case SNDRV_PCM_IOCTL_XRUN: |
| 2590 | return snd_pcm_xrun(substream); |
| 2591 | case SNDRV_PCM_IOCTL_HWSYNC: |
| 2592 | return snd_pcm_hwsync(substream); |
| 2593 | case SNDRV_PCM_IOCTL_DELAY: |
| 2594 | return snd_pcm_delay(substream, arg); |
| 2595 | case SNDRV_PCM_IOCTL_SYNC_PTR: |
| 2596 | return snd_pcm_sync_ptr(substream, arg); |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 2597 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | case SNDRV_PCM_IOCTL_HW_REFINE_OLD: |
| 2599 | return snd_pcm_hw_refine_old_user(substream, arg); |
| 2600 | case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: |
| 2601 | return snd_pcm_hw_params_old_user(substream, arg); |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 2602 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | case SNDRV_PCM_IOCTL_DRAIN: |
Takashi Iwai | 4cdc115 | 2009-08-20 16:40:16 +0200 | [diff] [blame] | 2604 | return snd_pcm_drain(substream, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | case SNDRV_PCM_IOCTL_DROP: |
| 2606 | return snd_pcm_drop(substream); |
Takashi Iwai | e661d0d | 2006-02-21 14:14:50 +0100 | [diff] [blame] | 2607 | case SNDRV_PCM_IOCTL_PAUSE: |
| 2608 | { |
| 2609 | int res; |
| 2610 | snd_pcm_stream_lock_irq(substream); |
| 2611 | res = snd_pcm_pause(substream, (int)(unsigned long)arg); |
| 2612 | snd_pcm_stream_unlock_irq(substream); |
| 2613 | return res; |
| 2614 | } |
Asish Bhattacharya | 9048f75 | 2011-11-01 20:33:44 +0530 | [diff] [blame] | 2615 | case SNDRV_COMPRESS_GET_CAPS: |
| 2616 | case SNDRV_COMPRESS_GET_CODEC_CAPS: |
| 2617 | case SNDRV_COMPRESS_SET_PARAMS: |
| 2618 | case SNDRV_COMPRESS_GET_PARAMS: |
| 2619 | case SNDRV_COMPRESS_TSTAMP: |
Krishnankutty Kolathappilly | 6f52d71 | 2012-08-28 15:47:48 -0700 | [diff] [blame] | 2620 | case SNDRV_COMPRESS_DRAIN: |
Asish Bhattacharya | 9048f75 | 2011-11-01 20:33:44 +0530 | [diff] [blame] | 2621 | return snd_compressed_ioctl(substream, cmd, arg); |
Joonwoo Park | ed3bab0 | 2013-02-13 15:59:44 -0800 | [diff] [blame] | 2622 | default: |
| 2623 | if (((cmd >> 8) & 0xff) == 'U') |
| 2624 | return snd_user_ioctl(substream, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | } |
| 2626 | snd_printd("unknown ioctl = 0x%x\n", cmd); |
| 2627 | return -ENOTTY; |
| 2628 | } |
| 2629 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2630 | static int snd_pcm_playback_ioctl1(struct file *file, |
| 2631 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | unsigned int cmd, void __user *arg) |
| 2633 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2634 | if (snd_BUG_ON(!substream)) |
| 2635 | return -ENXIO; |
| 2636 | if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) |
| 2637 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | switch (cmd) { |
| 2639 | case SNDRV_PCM_IOCTL_WRITEI_FRAMES: |
| 2640 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2641 | struct snd_xferi xferi; |
| 2642 | struct snd_xferi __user *_xferi = arg; |
| 2643 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | snd_pcm_sframes_t result; |
| 2645 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2646 | return -EBADFD; |
| 2647 | if (put_user(0, &_xferi->result)) |
| 2648 | return -EFAULT; |
| 2649 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) |
| 2650 | return -EFAULT; |
| 2651 | result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); |
| 2652 | __put_user(result, &_xferi->result); |
| 2653 | return result < 0 ? result : 0; |
| 2654 | } |
| 2655 | case SNDRV_PCM_IOCTL_WRITEN_FRAMES: |
| 2656 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2657 | struct snd_xfern xfern; |
| 2658 | struct snd_xfern __user *_xfern = arg; |
| 2659 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | void __user **bufs; |
| 2661 | snd_pcm_sframes_t result; |
| 2662 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2663 | return -EBADFD; |
| 2664 | if (runtime->channels > 128) |
| 2665 | return -EINVAL; |
| 2666 | if (put_user(0, &_xfern->result)) |
| 2667 | return -EFAULT; |
| 2668 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) |
| 2669 | return -EFAULT; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 2670 | |
| 2671 | bufs = memdup_user(xfern.bufs, |
| 2672 | sizeof(void *) * runtime->channels); |
| 2673 | if (IS_ERR(bufs)) |
| 2674 | return PTR_ERR(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | result = snd_pcm_lib_writev(substream, bufs, xfern.frames); |
| 2676 | kfree(bufs); |
| 2677 | __put_user(result, &_xfern->result); |
| 2678 | return result < 0 ? result : 0; |
| 2679 | } |
| 2680 | case SNDRV_PCM_IOCTL_REWIND: |
| 2681 | { |
| 2682 | snd_pcm_uframes_t frames; |
| 2683 | snd_pcm_uframes_t __user *_frames = arg; |
| 2684 | snd_pcm_sframes_t result; |
| 2685 | if (get_user(frames, _frames)) |
| 2686 | return -EFAULT; |
| 2687 | if (put_user(0, _frames)) |
| 2688 | return -EFAULT; |
| 2689 | result = snd_pcm_playback_rewind(substream, frames); |
| 2690 | __put_user(result, _frames); |
| 2691 | return result < 0 ? result : 0; |
| 2692 | } |
| 2693 | case SNDRV_PCM_IOCTL_FORWARD: |
| 2694 | { |
| 2695 | snd_pcm_uframes_t frames; |
| 2696 | snd_pcm_uframes_t __user *_frames = arg; |
| 2697 | snd_pcm_sframes_t result; |
| 2698 | if (get_user(frames, _frames)) |
| 2699 | return -EFAULT; |
| 2700 | if (put_user(0, _frames)) |
| 2701 | return -EFAULT; |
| 2702 | result = snd_pcm_playback_forward(substream, frames); |
| 2703 | __put_user(result, _frames); |
| 2704 | return result < 0 ? result : 0; |
| 2705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | } |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2707 | return snd_pcm_common_ioctl1(file, substream, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2710 | static int snd_pcm_capture_ioctl1(struct file *file, |
| 2711 | struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | unsigned int cmd, void __user *arg) |
| 2713 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2714 | if (snd_BUG_ON(!substream)) |
| 2715 | return -ENXIO; |
| 2716 | if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE)) |
| 2717 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | switch (cmd) { |
| 2719 | case SNDRV_PCM_IOCTL_READI_FRAMES: |
| 2720 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2721 | struct snd_xferi xferi; |
| 2722 | struct snd_xferi __user *_xferi = arg; |
| 2723 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | snd_pcm_sframes_t result; |
| 2725 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2726 | return -EBADFD; |
| 2727 | if (put_user(0, &_xferi->result)) |
| 2728 | return -EFAULT; |
| 2729 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) |
| 2730 | return -EFAULT; |
| 2731 | result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); |
| 2732 | __put_user(result, &_xferi->result); |
| 2733 | return result < 0 ? result : 0; |
| 2734 | } |
| 2735 | case SNDRV_PCM_IOCTL_READN_FRAMES: |
| 2736 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2737 | struct snd_xfern xfern; |
| 2738 | struct snd_xfern __user *_xfern = arg; |
| 2739 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | void *bufs; |
| 2741 | snd_pcm_sframes_t result; |
| 2742 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2743 | return -EBADFD; |
| 2744 | if (runtime->channels > 128) |
| 2745 | return -EINVAL; |
| 2746 | if (put_user(0, &_xfern->result)) |
| 2747 | return -EFAULT; |
| 2748 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) |
| 2749 | return -EFAULT; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 2750 | |
| 2751 | bufs = memdup_user(xfern.bufs, |
| 2752 | sizeof(void *) * runtime->channels); |
| 2753 | if (IS_ERR(bufs)) |
| 2754 | return PTR_ERR(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | result = snd_pcm_lib_readv(substream, bufs, xfern.frames); |
| 2756 | kfree(bufs); |
| 2757 | __put_user(result, &_xfern->result); |
| 2758 | return result < 0 ? result : 0; |
| 2759 | } |
| 2760 | case SNDRV_PCM_IOCTL_REWIND: |
| 2761 | { |
| 2762 | snd_pcm_uframes_t frames; |
| 2763 | snd_pcm_uframes_t __user *_frames = arg; |
| 2764 | snd_pcm_sframes_t result; |
| 2765 | if (get_user(frames, _frames)) |
| 2766 | return -EFAULT; |
| 2767 | if (put_user(0, _frames)) |
| 2768 | return -EFAULT; |
| 2769 | result = snd_pcm_capture_rewind(substream, frames); |
| 2770 | __put_user(result, _frames); |
| 2771 | return result < 0 ? result : 0; |
| 2772 | } |
| 2773 | case SNDRV_PCM_IOCTL_FORWARD: |
| 2774 | { |
| 2775 | snd_pcm_uframes_t frames; |
| 2776 | snd_pcm_uframes_t __user *_frames = arg; |
| 2777 | snd_pcm_sframes_t result; |
| 2778 | if (get_user(frames, _frames)) |
| 2779 | return -EFAULT; |
| 2780 | if (put_user(0, _frames)) |
| 2781 | return -EFAULT; |
| 2782 | result = snd_pcm_capture_forward(substream, frames); |
| 2783 | __put_user(result, _frames); |
| 2784 | return result < 0 ? result : 0; |
| 2785 | } |
| 2786 | } |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2787 | return snd_pcm_common_ioctl1(file, substream, cmd, 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 | static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, |
| 2791 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2793 | struct snd_pcm_file *pcm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | |
| 2795 | pcm_file = file->private_data; |
| 2796 | |
Asish Bhattacharya | 9048f75 | 2011-11-01 20:33:44 +0530 | [diff] [blame] | 2797 | if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | return -ENOTTY; |
| 2799 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2800 | return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd, |
| 2801 | (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2804 | static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, |
| 2805 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2807 | struct snd_pcm_file *pcm_file; |
Joonwoo Park | ed3bab0 | 2013-02-13 15:59:44 -0800 | [diff] [blame] | 2808 | unsigned char ioctl_magic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
| 2810 | pcm_file = file->private_data; |
Joonwoo Park | ed3bab0 | 2013-02-13 15:59:44 -0800 | [diff] [blame] | 2811 | ioctl_magic = ((cmd >> 8) & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | |
Joonwoo Park | ed3bab0 | 2013-02-13 15:59:44 -0800 | [diff] [blame] | 2813 | if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | return -ENOTTY; |
| 2815 | |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2816 | return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd, |
| 2817 | (void __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2820 | int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | unsigned int cmd, void *arg) |
| 2822 | { |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2823 | mm_segment_t fs; |
| 2824 | int result; |
| 2825 | |
| 2826 | fs = snd_enter_user(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | switch (substream->stream) { |
| 2828 | case SNDRV_PCM_STREAM_PLAYBACK: |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2829 | result = snd_pcm_playback_ioctl1(NULL, substream, cmd, |
| 2830 | (void __user *)arg); |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2831 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | case SNDRV_PCM_STREAM_CAPTURE: |
Takashi Iwai | 0df63e4 | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 2833 | result = snd_pcm_capture_ioctl1(NULL, substream, cmd, |
| 2834 | (void __user *)arg); |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2835 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | default: |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2837 | result = -EINVAL; |
| 2838 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | } |
Takashi Iwai | bf1bbb5 | 2006-03-27 16:22:45 +0200 | [diff] [blame] | 2840 | snd_leave_user(fs); |
| 2841 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 2844 | EXPORT_SYMBOL(snd_pcm_kernel_ioctl); |
| 2845 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2846 | static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, |
| 2847 | loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2849 | struct snd_pcm_file *pcm_file; |
| 2850 | struct snd_pcm_substream *substream; |
| 2851 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | snd_pcm_sframes_t result; |
| 2853 | |
| 2854 | pcm_file = file->private_data; |
| 2855 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2856 | if (PCM_RUNTIME_CHECK(substream)) |
| 2857 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | runtime = substream->runtime; |
| 2859 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2860 | return -EBADFD; |
| 2861 | if (!frame_aligned(runtime, count)) |
| 2862 | return -EINVAL; |
| 2863 | count = bytes_to_frames(runtime, count); |
| 2864 | result = snd_pcm_lib_read(substream, buf, count); |
| 2865 | if (result > 0) |
| 2866 | result = frames_to_bytes(runtime, result); |
| 2867 | return result; |
| 2868 | } |
| 2869 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2870 | static ssize_t snd_pcm_write(struct file *file, const char __user *buf, |
| 2871 | size_t count, loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2873 | struct snd_pcm_file *pcm_file; |
| 2874 | struct snd_pcm_substream *substream; |
| 2875 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | snd_pcm_sframes_t result; |
| 2877 | |
| 2878 | pcm_file = file->private_data; |
| 2879 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2880 | if (PCM_RUNTIME_CHECK(substream)) |
| 2881 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | runtime = substream->runtime; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2883 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2884 | return -EBADFD; |
| 2885 | if (!frame_aligned(runtime, count)) |
| 2886 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | count = bytes_to_frames(runtime, count); |
| 2888 | result = snd_pcm_lib_write(substream, buf, count); |
| 2889 | if (result > 0) |
| 2890 | result = frames_to_bytes(runtime, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | return result; |
| 2892 | } |
| 2893 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2894 | static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 2895 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | |
| 2897 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2898 | struct snd_pcm_file *pcm_file; |
| 2899 | struct snd_pcm_substream *substream; |
| 2900 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | snd_pcm_sframes_t result; |
| 2902 | unsigned long i; |
| 2903 | void __user **bufs; |
| 2904 | snd_pcm_uframes_t frames; |
| 2905 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2906 | pcm_file = iocb->ki_filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2908 | if (PCM_RUNTIME_CHECK(substream)) |
| 2909 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | runtime = substream->runtime; |
| 2911 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2912 | return -EBADFD; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2913 | if (nr_segs > 1024 || nr_segs != runtime->channels) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | return -EINVAL; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2915 | if (!frame_aligned(runtime, iov->iov_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | return -EINVAL; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2917 | frames = bytes_to_samples(runtime, iov->iov_len); |
| 2918 | bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | if (bufs == NULL) |
| 2920 | return -ENOMEM; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2921 | for (i = 0; i < nr_segs; ++i) |
| 2922 | bufs[i] = iov[i].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | result = snd_pcm_lib_readv(substream, bufs, frames); |
| 2924 | if (result > 0) |
| 2925 | result = frames_to_bytes(runtime, result); |
| 2926 | kfree(bufs); |
| 2927 | return result; |
| 2928 | } |
| 2929 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2930 | static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 2931 | unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2933 | struct snd_pcm_file *pcm_file; |
| 2934 | struct snd_pcm_substream *substream; |
| 2935 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | snd_pcm_sframes_t result; |
| 2937 | unsigned long i; |
| 2938 | void __user **bufs; |
| 2939 | snd_pcm_uframes_t frames; |
| 2940 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2941 | pcm_file = iocb->ki_filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2943 | if (PCM_RUNTIME_CHECK(substream)) |
| 2944 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | runtime = substream->runtime; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2946 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 2947 | return -EBADFD; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2948 | if (nr_segs > 128 || nr_segs != runtime->channels || |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2949 | !frame_aligned(runtime, iov->iov_len)) |
| 2950 | return -EINVAL; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2951 | frames = bytes_to_samples(runtime, iov->iov_len); |
| 2952 | bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | if (bufs == NULL) |
| 2954 | return -ENOMEM; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 2955 | for (i = 0; i < nr_segs; ++i) |
| 2956 | bufs[i] = iov[i].iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | result = snd_pcm_lib_writev(substream, bufs, frames); |
| 2958 | if (result > 0) |
| 2959 | result = frames_to_bytes(runtime, result); |
| 2960 | kfree(bufs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | return result; |
| 2962 | } |
| 2963 | |
| 2964 | static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait) |
| 2965 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 2966 | struct snd_pcm_file *pcm_file; |
| 2967 | struct snd_pcm_substream *substream; |
| 2968 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | unsigned int mask; |
| 2970 | snd_pcm_uframes_t avail; |
| 2971 | |
| 2972 | pcm_file = file->private_data; |
| 2973 | |
| 2974 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 2975 | if (PCM_RUNTIME_CHECK(substream)) |
| 2976 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | runtime = substream->runtime; |
| 2978 | |
| 2979 | poll_wait(file, &runtime->sleep, wait); |
| 2980 | |
| 2981 | snd_pcm_stream_lock_irq(substream); |
| 2982 | avail = snd_pcm_playback_avail(runtime); |
| 2983 | switch (runtime->status->state) { |
| 2984 | case SNDRV_PCM_STATE_RUNNING: |
| 2985 | case SNDRV_PCM_STATE_PREPARED: |
| 2986 | case SNDRV_PCM_STATE_PAUSED: |
| 2987 | if (avail >= runtime->control->avail_min) { |
| 2988 | mask = POLLOUT | POLLWRNORM; |
| 2989 | break; |
| 2990 | } |
| 2991 | /* Fall through */ |
| 2992 | case SNDRV_PCM_STATE_DRAINING: |
| 2993 | mask = 0; |
| 2994 | break; |
| 2995 | default: |
| 2996 | mask = POLLOUT | POLLWRNORM | POLLERR; |
| 2997 | break; |
| 2998 | } |
| 2999 | snd_pcm_stream_unlock_irq(substream); |
| 3000 | return mask; |
| 3001 | } |
| 3002 | |
| 3003 | static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait) |
| 3004 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3005 | struct snd_pcm_file *pcm_file; |
| 3006 | struct snd_pcm_substream *substream; |
| 3007 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | unsigned int mask; |
| 3009 | snd_pcm_uframes_t avail; |
| 3010 | |
| 3011 | pcm_file = file->private_data; |
| 3012 | |
| 3013 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3014 | if (PCM_RUNTIME_CHECK(substream)) |
| 3015 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | runtime = substream->runtime; |
| 3017 | |
| 3018 | poll_wait(file, &runtime->sleep, wait); |
| 3019 | |
| 3020 | snd_pcm_stream_lock_irq(substream); |
| 3021 | avail = snd_pcm_capture_avail(runtime); |
| 3022 | switch (runtime->status->state) { |
| 3023 | case SNDRV_PCM_STATE_RUNNING: |
| 3024 | case SNDRV_PCM_STATE_PREPARED: |
| 3025 | case SNDRV_PCM_STATE_PAUSED: |
| 3026 | if (avail >= runtime->control->avail_min) { |
| 3027 | mask = POLLIN | POLLRDNORM; |
| 3028 | break; |
| 3029 | } |
| 3030 | mask = 0; |
| 3031 | break; |
| 3032 | case SNDRV_PCM_STATE_DRAINING: |
| 3033 | if (avail > 0) { |
| 3034 | mask = POLLIN | POLLRDNORM; |
| 3035 | break; |
| 3036 | } |
| 3037 | /* Fall through */ |
| 3038 | default: |
| 3039 | mask = POLLIN | POLLRDNORM | POLLERR; |
| 3040 | break; |
| 3041 | } |
| 3042 | snd_pcm_stream_unlock_irq(substream); |
| 3043 | return mask; |
| 3044 | } |
| 3045 | |
| 3046 | /* |
| 3047 | * mmap support |
| 3048 | */ |
| 3049 | |
| 3050 | /* |
| 3051 | * Only on coherent architectures, we can mmap the status and the control records |
| 3052 | * for effcient data transfer. On others, we have to use HWSYNC ioctl... |
| 3053 | */ |
| 3054 | #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) |
| 3055 | /* |
| 3056 | * mmap status record |
| 3057 | */ |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3058 | static int snd_pcm_mmap_status_fault(struct vm_area_struct *area, |
| 3059 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3061 | struct snd_pcm_substream *substream = area->vm_private_data; |
| 3062 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
| 3064 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3065 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3067 | vmf->page = virt_to_page(runtime->status); |
| 3068 | get_page(vmf->page); |
| 3069 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3072 | static const struct vm_operations_struct snd_pcm_vm_ops_status = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | { |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3074 | .fault = snd_pcm_mmap_status_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | }; |
| 3076 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3077 | 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] | 3078 | struct vm_area_struct *area) |
| 3079 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | long size; |
| 3081 | if (!(area->vm_flags & VM_READ)) |
| 3082 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | size = area->vm_end - area->vm_start; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3084 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | return -EINVAL; |
| 3086 | area->vm_ops = &snd_pcm_vm_ops_status; |
| 3087 | area->vm_private_data = substream; |
| 3088 | area->vm_flags |= VM_RESERVED; |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * mmap control record |
| 3094 | */ |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3095 | static int snd_pcm_mmap_control_fault(struct vm_area_struct *area, |
| 3096 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3098 | struct snd_pcm_substream *substream = area->vm_private_data; |
| 3099 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | |
| 3101 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3102 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3104 | vmf->page = virt_to_page(runtime->control); |
| 3105 | get_page(vmf->page); |
| 3106 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3109 | static const struct vm_operations_struct snd_pcm_vm_ops_control = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | { |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3111 | .fault = snd_pcm_mmap_control_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | }; |
| 3113 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3114 | 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] | 3115 | struct vm_area_struct *area) |
| 3116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | long size; |
| 3118 | if (!(area->vm_flags & VM_READ)) |
| 3119 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | size = area->vm_end - area->vm_start; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3121 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | return -EINVAL; |
| 3123 | area->vm_ops = &snd_pcm_vm_ops_control; |
| 3124 | area->vm_private_data = substream; |
| 3125 | area->vm_flags |= VM_RESERVED; |
| 3126 | return 0; |
| 3127 | } |
| 3128 | #else /* ! coherent mmap */ |
| 3129 | /* |
| 3130 | * don't support mmap for status and control records. |
| 3131 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3132 | 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] | 3133 | struct vm_area_struct *area) |
| 3134 | { |
| 3135 | return -ENXIO; |
| 3136 | } |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3137 | 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] | 3138 | struct vm_area_struct *area) |
| 3139 | { |
| 3140 | return -ENXIO; |
| 3141 | } |
| 3142 | #endif /* coherent mmap */ |
| 3143 | |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3144 | static inline struct page * |
| 3145 | snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs) |
| 3146 | { |
| 3147 | void *vaddr = substream->runtime->dma_area + ofs; |
Takashi Iwai | 66b6cfa | 2009-11-26 12:50:01 +0100 | [diff] [blame] | 3148 | #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT) |
| 3149 | if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) |
| 3150 | return virt_to_page(CAC_ADDR(vaddr)); |
| 3151 | #endif |
Takashi Iwai | 6985c88 | 2009-11-26 15:04:24 +0100 | [diff] [blame] | 3152 | #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE) |
| 3153 | if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) { |
| 3154 | dma_addr_t addr = substream->runtime->dma_addr + ofs; |
| 3155 | addr -= get_dma_offset(substream->dma_buffer.dev.dev); |
| 3156 | /* assume dma_handle set via pfn_to_phys() in |
| 3157 | * mm/dma-noncoherent.c |
| 3158 | */ |
| 3159 | return pfn_to_page(addr >> PAGE_SHIFT); |
| 3160 | } |
| 3161 | #endif |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3162 | return virt_to_page(vaddr); |
| 3163 | } |
| 3164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | /* |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3166 | * fault callback for mmapping a RAM page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | */ |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3168 | static int snd_pcm_mmap_data_fault(struct vm_area_struct *area, |
| 3169 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3171 | struct snd_pcm_substream *substream = area->vm_private_data; |
| 3172 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | unsigned long offset; |
| 3174 | struct page * page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | size_t dma_bytes; |
| 3176 | |
| 3177 | if (substream == NULL) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3178 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | runtime = substream->runtime; |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3180 | offset = vmf->pgoff << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
| 3182 | if (offset > dma_bytes - PAGE_SIZE) |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3183 | return VM_FAULT_SIGBUS; |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3184 | if (substream->ops->page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | page = substream->ops->page(substream, offset); |
Takashi Iwai | 9eb4a06 | 2009-11-26 12:43:39 +0100 | [diff] [blame] | 3186 | else |
| 3187 | page = snd_pcm_default_page_ops(substream, offset); |
| 3188 | if (!page) |
| 3189 | return VM_FAULT_SIGBUS; |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 3190 | get_page(page); |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3191 | vmf->page = page; |
| 3192 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3195 | static const struct vm_operations_struct snd_pcm_vm_ops_data = { |
| 3196 | .open = snd_pcm_mmap_data_open, |
| 3197 | .close = snd_pcm_mmap_data_close, |
| 3198 | }; |
| 3199 | |
| 3200 | static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | .open = snd_pcm_mmap_data_open, |
| 3202 | .close = snd_pcm_mmap_data_close, |
Nick Piggin | 3ad5afc | 2007-12-13 16:15:00 +0100 | [diff] [blame] | 3203 | .fault = snd_pcm_mmap_data_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | }; |
| 3205 | |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3206 | #ifndef ARCH_HAS_DMA_MMAP_COHERENT |
| 3207 | /* This should be defined / handled globally! */ |
| 3208 | #ifdef CONFIG_ARM |
| 3209 | #define ARCH_HAS_DMA_MMAP_COHERENT |
| 3210 | #endif |
| 3211 | #endif |
| 3212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | /* |
| 3214 | * mmap the DMA buffer on RAM |
| 3215 | */ |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3216 | int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, |
| 3217 | struct vm_area_struct *area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | area->vm_flags |= VM_RESERVED; |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3220 | #ifdef ARCH_HAS_DMA_MMAP_COHERENT |
| 3221 | if (!substream->ops->page && |
| 3222 | substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) |
| 3223 | return dma_mmap_coherent(substream->dma_buffer.dev.dev, |
| 3224 | area, |
| 3225 | substream->runtime->dma_area, |
| 3226 | substream->runtime->dma_addr, |
| 3227 | area->vm_end - area->vm_start); |
Takashi Iwai | 9fe17b5 | 2010-05-12 10:32:42 +0200 | [diff] [blame] | 3228 | #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT) |
| 3229 | if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV && |
| 3230 | !plat_device_is_coherent(substream->dma_buffer.dev.dev)) |
| 3231 | area->vm_page_prot = pgprot_noncached(area->vm_page_prot); |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3232 | #endif /* ARCH_HAS_DMA_MMAP_COHERENT */ |
| 3233 | /* mmap with fault handler */ |
| 3234 | area->vm_ops = &snd_pcm_vm_ops_data_fault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3237 | EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | |
| 3239 | /* |
| 3240 | * mmap the DMA buffer on I/O memory area |
| 3241 | */ |
| 3242 | #if SNDRV_PCM_INFO_MMAP_IOMEM |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3243 | int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, |
| 3244 | struct vm_area_struct *area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | { |
| 3246 | long size; |
| 3247 | unsigned long offset; |
| 3248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | area->vm_page_prot = pgprot_noncached(area->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | area->vm_flags |= VM_IO; |
| 3251 | size = area->vm_end - area->vm_start; |
| 3252 | offset = area->vm_pgoff << PAGE_SHIFT; |
| 3253 | if (io_remap_pfn_range(area, area->vm_start, |
| 3254 | (substream->runtime->dma_addr + offset) >> PAGE_SHIFT, |
| 3255 | size, area->vm_page_prot)) |
| 3256 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | return 0; |
| 3258 | } |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 3259 | |
| 3260 | EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | #endif /* SNDRV_PCM_INFO_MMAP */ |
| 3262 | |
| 3263 | /* |
| 3264 | * mmap DMA buffer |
| 3265 | */ |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3266 | 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] | 3267 | struct vm_area_struct *area) |
| 3268 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3269 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | long size; |
| 3271 | unsigned long offset; |
| 3272 | size_t dma_bytes; |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3273 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | |
| 3275 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 3276 | if (!(area->vm_flags & (VM_WRITE|VM_READ))) |
| 3277 | return -EINVAL; |
| 3278 | } else { |
| 3279 | if (!(area->vm_flags & VM_READ)) |
| 3280 | return -EINVAL; |
| 3281 | } |
| 3282 | runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | if (runtime->status->state == SNDRV_PCM_STATE_OPEN) |
| 3284 | return -EBADFD; |
| 3285 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) |
| 3286 | return -ENXIO; |
| 3287 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || |
| 3288 | runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) |
| 3289 | return -EINVAL; |
| 3290 | size = area->vm_end - area->vm_start; |
| 3291 | offset = area->vm_pgoff << PAGE_SHIFT; |
| 3292 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
| 3293 | if ((size_t)size > dma_bytes) |
| 3294 | return -EINVAL; |
| 3295 | if (offset > dma_bytes - size) |
| 3296 | return -EINVAL; |
| 3297 | |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3298 | area->vm_ops = &snd_pcm_vm_ops_data; |
| 3299 | area->vm_private_data = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | if (substream->ops->mmap) |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3301 | err = substream->ops->mmap(substream, area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | else |
Takashi Iwai | 18a2b96 | 2011-09-28 17:12:59 +0200 | [diff] [blame] | 3303 | err = snd_pcm_lib_default_mmap(substream, area); |
Takashi Iwai | 657b198 | 2009-11-26 12:40:21 +0100 | [diff] [blame] | 3304 | if (!err) |
| 3305 | atomic_inc(&substream->mmap_count); |
| 3306 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
Takashi Iwai | e88e8ae6 | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 3309 | EXPORT_SYMBOL(snd_pcm_mmap_data); |
| 3310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) |
| 3312 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3313 | struct snd_pcm_file * pcm_file; |
| 3314 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | unsigned long offset; |
| 3316 | |
| 3317 | pcm_file = file->private_data; |
| 3318 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3319 | if (PCM_RUNTIME_CHECK(substream)) |
| 3320 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | |
| 3322 | offset = area->vm_pgoff << PAGE_SHIFT; |
| 3323 | switch (offset) { |
| 3324 | case SNDRV_PCM_MMAP_OFFSET_STATUS: |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 3325 | if (pcm_file->no_compat_mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | return -ENXIO; |
| 3327 | return snd_pcm_mmap_status(substream, file, area); |
| 3328 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: |
Takashi Iwai | 548a648 | 2006-07-31 16:51:51 +0200 | [diff] [blame] | 3329 | if (pcm_file->no_compat_mmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | return -ENXIO; |
| 3331 | return snd_pcm_mmap_control(substream, file, area); |
| 3332 | default: |
| 3333 | return snd_pcm_mmap_data(substream, file, area); |
| 3334 | } |
| 3335 | return 0; |
| 3336 | } |
| 3337 | |
| 3338 | static int snd_pcm_fasync(int fd, struct file * file, int on) |
| 3339 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3340 | struct snd_pcm_file * pcm_file; |
| 3341 | struct snd_pcm_substream *substream; |
| 3342 | struct snd_pcm_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
| 3344 | pcm_file = file->private_data; |
| 3345 | substream = pcm_file->substream; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 3346 | if (PCM_RUNTIME_CHECK(substream)) |
Takashi Iwai | d05468b | 2010-04-07 18:29:46 +0200 | [diff] [blame] | 3347 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | runtime = substream->runtime; |
Takashi Iwai | d05468b | 2010-04-07 18:29:46 +0200 | [diff] [blame] | 3349 | return fasync_helper(fd, file, on, &runtime->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* |
| 3353 | * ioctl32 compat |
| 3354 | */ |
| 3355 | #ifdef CONFIG_COMPAT |
| 3356 | #include "pcm_compat.c" |
| 3357 | #else |
| 3358 | #define snd_pcm_ioctl_compat NULL |
| 3359 | #endif |
| 3360 | |
| 3361 | /* |
| 3362 | * To be removed helpers to keep binary compatibility |
| 3363 | */ |
| 3364 | |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 3365 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) |
| 3367 | #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) |
| 3368 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3369 | static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, |
| 3370 | struct snd_pcm_hw_params_old *oparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | { |
| 3372 | unsigned int i; |
| 3373 | |
| 3374 | memset(params, 0, sizeof(*params)); |
| 3375 | params->flags = oparams->flags; |
| 3376 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) |
| 3377 | params->masks[i].bits[0] = oparams->masks[i]; |
| 3378 | memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); |
| 3379 | params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); |
| 3380 | params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); |
| 3381 | params->info = oparams->info; |
| 3382 | params->msbits = oparams->msbits; |
| 3383 | params->rate_num = oparams->rate_num; |
| 3384 | params->rate_den = oparams->rate_den; |
| 3385 | params->fifo_size = oparams->fifo_size; |
| 3386 | } |
| 3387 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3388 | static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, |
| 3389 | struct snd_pcm_hw_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | { |
| 3391 | unsigned int i; |
| 3392 | |
| 3393 | memset(oparams, 0, sizeof(*oparams)); |
| 3394 | oparams->flags = params->flags; |
| 3395 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) |
| 3396 | oparams->masks[i] = params->masks[i].bits[0]; |
| 3397 | memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); |
| 3398 | oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); |
| 3399 | oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); |
| 3400 | oparams->info = params->info; |
| 3401 | oparams->msbits = params->msbits; |
| 3402 | oparams->rate_num = params->rate_num; |
| 3403 | oparams->rate_den = params->rate_den; |
| 3404 | oparams->fifo_size = params->fifo_size; |
| 3405 | } |
| 3406 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3407 | static int snd_pcm_hw_refine_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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 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_refine(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 | } |
| 3436 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3437 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, |
| 3438 | struct snd_pcm_hw_params_old __user * _oparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 3440 | struct snd_pcm_hw_params *params; |
| 3441 | struct snd_pcm_hw_params_old *oparams = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | int err; |
| 3443 | |
| 3444 | params = kmalloc(sizeof(*params), GFP_KERNEL); |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3445 | if (!params) |
| 3446 | return -ENOMEM; |
| 3447 | |
| 3448 | oparams = memdup_user(_oparams, sizeof(*oparams)); |
| 3449 | if (IS_ERR(oparams)) { |
| 3450 | err = PTR_ERR(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | goto out; |
| 3452 | } |
| 3453 | snd_pcm_hw_convert_from_old_params(params, oparams); |
| 3454 | err = snd_pcm_hw_params(substream, params); |
| 3455 | snd_pcm_hw_convert_to_old_params(oparams, params); |
| 3456 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) { |
| 3457 | if (!err) |
| 3458 | err = -EFAULT; |
| 3459 | } |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 3460 | |
| 3461 | kfree(oparams); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | out: |
| 3463 | kfree(params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | return err; |
| 3465 | } |
Takashi Iwai | 59d4858 | 2005-12-01 10:51:58 +0100 | [diff] [blame] | 3466 | #endif /* CONFIG_SND_SUPPORT_OLD_API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3468 | #ifndef CONFIG_MMU |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3469 | static unsigned long snd_pcm_get_unmapped_area(struct file *file, |
| 3470 | unsigned long addr, |
| 3471 | unsigned long len, |
| 3472 | unsigned long pgoff, |
| 3473 | unsigned long flags) |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3474 | { |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3475 | struct snd_pcm_file *pcm_file = file->private_data; |
| 3476 | struct snd_pcm_substream *substream = pcm_file->substream; |
| 3477 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 3478 | unsigned long offset = pgoff << PAGE_SHIFT; |
| 3479 | |
| 3480 | switch (offset) { |
| 3481 | case SNDRV_PCM_MMAP_OFFSET_STATUS: |
| 3482 | return (unsigned long)runtime->status; |
| 3483 | case SNDRV_PCM_MMAP_OFFSET_CONTROL: |
| 3484 | return (unsigned long)runtime->control; |
| 3485 | default: |
| 3486 | return (unsigned long)runtime->dma_area + offset; |
| 3487 | } |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3488 | } |
| 3489 | #else |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3490 | # define snd_pcm_get_unmapped_area NULL |
Cliff Cai | 7003609 | 2008-09-03 10:54:36 +0200 | [diff] [blame] | 3491 | #endif |
| 3492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | /* |
| 3494 | * Register section |
| 3495 | */ |
| 3496 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 3497 | const struct file_operations snd_pcm_f_ops[2] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | { |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3499 | .owner = THIS_MODULE, |
| 3500 | .write = snd_pcm_write, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 3501 | .aio_write = snd_pcm_aio_write, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 3502 | .open = snd_pcm_playback_open, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3503 | .release = snd_pcm_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 3504 | .llseek = no_llseek, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3505 | .poll = snd_pcm_playback_poll, |
| 3506 | .unlocked_ioctl = snd_pcm_playback_ioctl, |
| 3507 | .compat_ioctl = snd_pcm_ioctl_compat, |
| 3508 | .mmap = snd_pcm_mmap, |
| 3509 | .fasync = snd_pcm_fasync, |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3510 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | }, |
| 3512 | { |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3513 | .owner = THIS_MODULE, |
| 3514 | .read = snd_pcm_read, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 3515 | .aio_read = snd_pcm_aio_read, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 3516 | .open = snd_pcm_capture_open, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3517 | .release = snd_pcm_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 3518 | .llseek = no_llseek, |
Clemens Ladisch | 2af677f | 2005-11-20 14:03:48 +0100 | [diff] [blame] | 3519 | .poll = snd_pcm_capture_poll, |
| 3520 | .unlocked_ioctl = snd_pcm_capture_ioctl, |
| 3521 | .compat_ioctl = snd_pcm_ioctl_compat, |
| 3522 | .mmap = snd_pcm_mmap, |
| 3523 | .fasync = snd_pcm_fasync, |
Daniel Glöckner | 55c63bd | 2010-03-09 12:57:52 -0500 | [diff] [blame] | 3524 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | } |
| 3526 | }; |