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