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