This source file includes following definitions.
- snd_pcm_playback_silence
- snd_pcm_debug_name
- __snd_pcm_xrun
- snd_pcm_update_state
- update_audio_tstamp
- snd_pcm_update_hw_ptr0
- snd_pcm_update_hw_ptr
- snd_pcm_set_ops
- snd_pcm_set_sync
- div32
- div_down
- div_up
- mul
- muldiv32
- snd_interval_refine
- snd_interval_refine_first
- snd_interval_refine_last
- snd_interval_mul
- snd_interval_div
- snd_interval_muldivk
- snd_interval_mulkdiv
- snd_interval_ratnum
- snd_interval_ratden
- snd_interval_list
- snd_interval_ranges
- snd_interval_step
- snd_pcm_hw_rule_add
- snd_pcm_hw_constraint_mask
- snd_pcm_hw_constraint_mask64
- snd_pcm_hw_constraint_integer
- snd_pcm_hw_constraint_minmax
- snd_pcm_hw_rule_list
- snd_pcm_hw_constraint_list
- snd_pcm_hw_rule_ranges
- snd_pcm_hw_constraint_ranges
- snd_pcm_hw_rule_ratnums
- snd_pcm_hw_constraint_ratnums
- snd_pcm_hw_rule_ratdens
- snd_pcm_hw_constraint_ratdens
- snd_pcm_hw_rule_msbits
- snd_pcm_hw_constraint_msbits
- snd_pcm_hw_rule_step
- snd_pcm_hw_constraint_step
- snd_pcm_hw_rule_pow2
- snd_pcm_hw_constraint_pow2
- snd_pcm_hw_rule_noresample_func
- snd_pcm_hw_rule_noresample
- _snd_pcm_hw_param_any
- _snd_pcm_hw_params_any
- snd_pcm_hw_param_value
- _snd_pcm_hw_param_setempty
- _snd_pcm_hw_param_first
- snd_pcm_hw_param_first
- _snd_pcm_hw_param_last
- snd_pcm_hw_param_last
- snd_pcm_lib_ioctl_reset
- snd_pcm_lib_ioctl_channel_info
- snd_pcm_lib_ioctl_fifo_size
- snd_pcm_lib_ioctl
- snd_pcm_period_elapsed
- wait_for_avail
- get_dma_ptr
- default_write_copy
- default_write_copy_kernel
- fill_silence
- default_read_copy
- default_read_copy_kernel
- interleaved_copy
- noninterleaved_copy
- fill_silence_frames
- pcm_sanity_check
- pcm_accessible_state
- pcm_lib_apply_appl_ptr
- __snd_pcm_lib_xfer
- valid_chmap_channels
- pcm_chmap_ctl_info
- pcm_chmap_ctl_get
- pcm_chmap_ctl_tlv
- pcm_chmap_ctl_private_free
- snd_pcm_add_chmap_ctls
1
2
3
4
5
6
7
8 #include <linux/slab.h>
9 #include <linux/sched/signal.h>
10 #include <linux/time.h>
11 #include <linux/math64.h>
12 #include <linux/export.h>
13 #include <sound/core.h>
14 #include <sound/control.h>
15 #include <sound/tlv.h>
16 #include <sound/info.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/timer.h>
20
21 #include "pcm_local.h"
22
23 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
24 #define CREATE_TRACE_POINTS
25 #include "pcm_trace.h"
26 #else
27 #define trace_hwptr(substream, pos, in_interrupt)
28 #define trace_xrun(substream)
29 #define trace_hw_ptr_error(substream, reason)
30 #define trace_applptr(substream, prev, curr)
31 #endif
32
33 static int fill_silence_frames(struct snd_pcm_substream *substream,
34 snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
35
36
37
38
39
40
41
42
43
44
45 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
46 {
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 snd_pcm_uframes_t frames, ofs, transfer;
49 int err;
50
51 if (runtime->silence_size < runtime->boundary) {
52 snd_pcm_sframes_t noise_dist, n;
53 snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
54 if (runtime->silence_start != appl_ptr) {
55 n = appl_ptr - runtime->silence_start;
56 if (n < 0)
57 n += runtime->boundary;
58 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
59 runtime->silence_filled -= n;
60 else
61 runtime->silence_filled = 0;
62 runtime->silence_start = appl_ptr;
63 }
64 if (runtime->silence_filled >= runtime->buffer_size)
65 return;
66 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
67 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
68 return;
69 frames = runtime->silence_threshold - noise_dist;
70 if (frames > runtime->silence_size)
71 frames = runtime->silence_size;
72 } else {
73 if (new_hw_ptr == ULONG_MAX) {
74 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
75 if (avail > runtime->buffer_size)
76 avail = runtime->buffer_size;
77 runtime->silence_filled = avail > 0 ? avail : 0;
78 runtime->silence_start = (runtime->status->hw_ptr +
79 runtime->silence_filled) %
80 runtime->boundary;
81 } else {
82 ofs = runtime->status->hw_ptr;
83 frames = new_hw_ptr - ofs;
84 if ((snd_pcm_sframes_t)frames < 0)
85 frames += runtime->boundary;
86 runtime->silence_filled -= frames;
87 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
88 runtime->silence_filled = 0;
89 runtime->silence_start = new_hw_ptr;
90 } else {
91 runtime->silence_start = ofs;
92 }
93 }
94 frames = runtime->buffer_size - runtime->silence_filled;
95 }
96 if (snd_BUG_ON(frames > runtime->buffer_size))
97 return;
98 if (frames == 0)
99 return;
100 ofs = runtime->silence_start % runtime->buffer_size;
101 while (frames > 0) {
102 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
103 err = fill_silence_frames(substream, ofs, transfer);
104 snd_BUG_ON(err < 0);
105 runtime->silence_filled += transfer;
106 frames -= transfer;
107 ofs = 0;
108 }
109 }
110
111 #ifdef CONFIG_SND_DEBUG
112 void snd_pcm_debug_name(struct snd_pcm_substream *substream,
113 char *name, size_t len)
114 {
115 snprintf(name, len, "pcmC%dD%d%c:%d",
116 substream->pcm->card->number,
117 substream->pcm->device,
118 substream->stream ? 'c' : 'p',
119 substream->number);
120 }
121 EXPORT_SYMBOL(snd_pcm_debug_name);
122 #endif
123
124 #define XRUN_DEBUG_BASIC (1<<0)
125 #define XRUN_DEBUG_STACK (1<<1)
126 #define XRUN_DEBUG_JIFFIESCHECK (1<<2)
127
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129
130 #define xrun_debug(substream, mask) \
131 ((substream)->pstr->xrun_debug & (mask))
132 #else
133 #define xrun_debug(substream, mask) 0
134 #endif
135
136 #define dump_stack_on_xrun(substream) do { \
137 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
138 dump_stack(); \
139 } while (0)
140
141
142 void __snd_pcm_xrun(struct snd_pcm_substream *substream)
143 {
144 struct snd_pcm_runtime *runtime = substream->runtime;
145
146 trace_xrun(substream);
147 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
148 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
149 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
150 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
151 char name[16];
152 snd_pcm_debug_name(substream, name, sizeof(name));
153 pcm_warn(substream->pcm, "XRUN: %s\n", name);
154 dump_stack_on_xrun(substream);
155 }
156 }
157
158 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
159 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
160 do { \
161 trace_hw_ptr_error(substream, reason); \
162 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
163 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
164 (in_interrupt) ? 'Q' : 'P', ##args); \
165 dump_stack_on_xrun(substream); \
166 } \
167 } while (0)
168
169 #else
170
171 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
172
173 #endif
174
175 int snd_pcm_update_state(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
177 {
178 snd_pcm_uframes_t avail;
179
180 avail = snd_pcm_avail(substream);
181 if (avail > runtime->avail_max)
182 runtime->avail_max = avail;
183 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
184 if (avail >= runtime->buffer_size) {
185 snd_pcm_drain_done(substream);
186 return -EPIPE;
187 }
188 } else {
189 if (avail >= runtime->stop_threshold) {
190 __snd_pcm_xrun(substream);
191 return -EPIPE;
192 }
193 }
194 if (runtime->twake) {
195 if (avail >= runtime->twake)
196 wake_up(&runtime->tsleep);
197 } else if (avail >= runtime->control->avail_min)
198 wake_up(&runtime->sleep);
199 return 0;
200 }
201
202 static void update_audio_tstamp(struct snd_pcm_substream *substream,
203 struct timespec *curr_tstamp,
204 struct timespec *audio_tstamp)
205 {
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 u64 audio_frames, audio_nsecs;
208 struct timespec driver_tstamp;
209
210 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
211 return;
212
213 if (!(substream->ops->get_time_info) ||
214 (runtime->audio_tstamp_report.actual_type ==
215 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
216
217
218
219
220
221
222 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
223
224 if (runtime->audio_tstamp_config.report_delay) {
225 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
226 audio_frames -= runtime->delay;
227 else
228 audio_frames += runtime->delay;
229 }
230 audio_nsecs = div_u64(audio_frames * 1000000000LL,
231 runtime->rate);
232 *audio_tstamp = ns_to_timespec(audio_nsecs);
233 }
234 if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
235 runtime->status->audio_tstamp = *audio_tstamp;
236 runtime->status->tstamp = *curr_tstamp;
237 }
238
239
240
241
242
243 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
244 runtime->driver_tstamp = driver_tstamp;
245 }
246
247 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
248 unsigned int in_interrupt)
249 {
250 struct snd_pcm_runtime *runtime = substream->runtime;
251 snd_pcm_uframes_t pos;
252 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
253 snd_pcm_sframes_t hdelta, delta;
254 unsigned long jdelta;
255 unsigned long curr_jiffies;
256 struct timespec curr_tstamp;
257 struct timespec audio_tstamp;
258 int crossed_boundary = 0;
259
260 old_hw_ptr = runtime->status->hw_ptr;
261
262
263
264
265
266
267
268 pos = substream->ops->pointer(substream);
269 curr_jiffies = jiffies;
270 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
271 if ((substream->ops->get_time_info) &&
272 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
273 substream->ops->get_time_info(substream, &curr_tstamp,
274 &audio_tstamp,
275 &runtime->audio_tstamp_config,
276 &runtime->audio_tstamp_report);
277
278
279 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
280 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
281 } else
282 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
283 }
284
285 if (pos == SNDRV_PCM_POS_XRUN) {
286 __snd_pcm_xrun(substream);
287 return -EPIPE;
288 }
289 if (pos >= runtime->buffer_size) {
290 if (printk_ratelimit()) {
291 char name[16];
292 snd_pcm_debug_name(substream, name, sizeof(name));
293 pcm_err(substream->pcm,
294 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
295 name, pos, runtime->buffer_size,
296 runtime->period_size);
297 }
298 pos = 0;
299 }
300 pos -= pos % runtime->min_align;
301 trace_hwptr(substream, pos, in_interrupt);
302 hw_base = runtime->hw_ptr_base;
303 new_hw_ptr = hw_base + pos;
304 if (in_interrupt) {
305
306
307 delta = runtime->hw_ptr_interrupt + runtime->period_size;
308 if (delta > new_hw_ptr) {
309
310 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
311 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
312 hw_base += runtime->buffer_size;
313 if (hw_base >= runtime->boundary) {
314 hw_base = 0;
315 crossed_boundary++;
316 }
317 new_hw_ptr = hw_base + pos;
318 goto __delta;
319 }
320 }
321 }
322
323
324 if (new_hw_ptr < old_hw_ptr) {
325 hw_base += runtime->buffer_size;
326 if (hw_base >= runtime->boundary) {
327 hw_base = 0;
328 crossed_boundary++;
329 }
330 new_hw_ptr = hw_base + pos;
331 }
332 __delta:
333 delta = new_hw_ptr - old_hw_ptr;
334 if (delta < 0)
335 delta += runtime->boundary;
336
337 if (runtime->no_period_wakeup) {
338 snd_pcm_sframes_t xrun_threshold;
339
340
341
342
343 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
344 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
345 goto no_delta_check;
346 hdelta = jdelta - delta * HZ / runtime->rate;
347 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
348 while (hdelta > xrun_threshold) {
349 delta += runtime->buffer_size;
350 hw_base += runtime->buffer_size;
351 if (hw_base >= runtime->boundary) {
352 hw_base = 0;
353 crossed_boundary++;
354 }
355 new_hw_ptr = hw_base + pos;
356 hdelta -= runtime->hw_ptr_buffer_jiffies;
357 }
358 goto no_delta_check;
359 }
360
361
362 if (delta >= runtime->buffer_size + runtime->period_size) {
363 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
364 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
365 substream->stream, (long)pos,
366 (long)new_hw_ptr, (long)old_hw_ptr);
367 return 0;
368 }
369
370
371 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
372 goto no_jiffies_check;
373
374
375
376
377
378 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
379 goto no_jiffies_check;
380 hdelta = delta;
381 if (hdelta < runtime->delay)
382 goto no_jiffies_check;
383 hdelta -= runtime->delay;
384 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
385 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
386 delta = jdelta /
387 (((runtime->period_size * HZ) / runtime->rate)
388 + HZ/100);
389
390 new_hw_ptr = old_hw_ptr;
391 hw_base = delta;
392
393
394 while (delta > 0) {
395 new_hw_ptr += runtime->period_size;
396 if (new_hw_ptr >= runtime->boundary) {
397 new_hw_ptr -= runtime->boundary;
398 crossed_boundary--;
399 }
400 delta--;
401 }
402
403 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
404 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
405 (long)pos, (long)hdelta,
406 (long)runtime->period_size, jdelta,
407 ((hdelta * HZ) / runtime->rate), hw_base,
408 (unsigned long)old_hw_ptr,
409 (unsigned long)new_hw_ptr);
410
411 delta = 0;
412 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
413 }
414 no_jiffies_check:
415 if (delta > runtime->period_size + runtime->period_size / 2) {
416 hw_ptr_error(substream, in_interrupt,
417 "Lost interrupts?",
418 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
419 substream->stream, (long)delta,
420 (long)new_hw_ptr,
421 (long)old_hw_ptr);
422 }
423
424 no_delta_check:
425 if (runtime->status->hw_ptr == new_hw_ptr) {
426 runtime->hw_ptr_jiffies = curr_jiffies;
427 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
428 return 0;
429 }
430
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
432 runtime->silence_size > 0)
433 snd_pcm_playback_silence(substream, new_hw_ptr);
434
435 if (in_interrupt) {
436 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
437 if (delta < 0)
438 delta += runtime->boundary;
439 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
440 runtime->hw_ptr_interrupt += delta;
441 if (runtime->hw_ptr_interrupt >= runtime->boundary)
442 runtime->hw_ptr_interrupt -= runtime->boundary;
443 }
444 runtime->hw_ptr_base = hw_base;
445 runtime->status->hw_ptr = new_hw_ptr;
446 runtime->hw_ptr_jiffies = curr_jiffies;
447 if (crossed_boundary) {
448 snd_BUG_ON(crossed_boundary != 1);
449 runtime->hw_ptr_wrap += runtime->boundary;
450 }
451
452 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
453
454 return snd_pcm_update_state(substream, runtime);
455 }
456
457
458 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
459 {
460 return snd_pcm_update_hw_ptr0(substream, 0);
461 }
462
463
464
465
466
467
468
469
470
471 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
472 const struct snd_pcm_ops *ops)
473 {
474 struct snd_pcm_str *stream = &pcm->streams[direction];
475 struct snd_pcm_substream *substream;
476
477 for (substream = stream->substream; substream != NULL; substream = substream->next)
478 substream->ops = ops;
479 }
480 EXPORT_SYMBOL(snd_pcm_set_ops);
481
482
483
484
485
486
487
488 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
489 {
490 struct snd_pcm_runtime *runtime = substream->runtime;
491
492 runtime->sync.id32[0] = substream->pcm->card->number;
493 runtime->sync.id32[1] = -1;
494 runtime->sync.id32[2] = -1;
495 runtime->sync.id32[3] = -1;
496 }
497 EXPORT_SYMBOL(snd_pcm_set_sync);
498
499
500
501
502
503 static inline unsigned int div32(unsigned int a, unsigned int b,
504 unsigned int *r)
505 {
506 if (b == 0) {
507 *r = 0;
508 return UINT_MAX;
509 }
510 *r = a % b;
511 return a / b;
512 }
513
514 static inline unsigned int div_down(unsigned int a, unsigned int b)
515 {
516 if (b == 0)
517 return UINT_MAX;
518 return a / b;
519 }
520
521 static inline unsigned int div_up(unsigned int a, unsigned int b)
522 {
523 unsigned int r;
524 unsigned int q;
525 if (b == 0)
526 return UINT_MAX;
527 q = div32(a, b, &r);
528 if (r)
529 ++q;
530 return q;
531 }
532
533 static inline unsigned int mul(unsigned int a, unsigned int b)
534 {
535 if (a == 0)
536 return 0;
537 if (div_down(UINT_MAX, a) < b)
538 return UINT_MAX;
539 return a * b;
540 }
541
542 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
543 unsigned int c, unsigned int *r)
544 {
545 u_int64_t n = (u_int64_t) a * b;
546 if (c == 0) {
547 *r = 0;
548 return UINT_MAX;
549 }
550 n = div_u64_rem(n, c, r);
551 if (n >= UINT_MAX) {
552 *r = 0;
553 return UINT_MAX;
554 }
555 return n;
556 }
557
558
559
560
561
562
563
564
565
566
567
568
569
570 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
571 {
572 int changed = 0;
573 if (snd_BUG_ON(snd_interval_empty(i)))
574 return -EINVAL;
575 if (i->min < v->min) {
576 i->min = v->min;
577 i->openmin = v->openmin;
578 changed = 1;
579 } else if (i->min == v->min && !i->openmin && v->openmin) {
580 i->openmin = 1;
581 changed = 1;
582 }
583 if (i->max > v->max) {
584 i->max = v->max;
585 i->openmax = v->openmax;
586 changed = 1;
587 } else if (i->max == v->max && !i->openmax && v->openmax) {
588 i->openmax = 1;
589 changed = 1;
590 }
591 if (!i->integer && v->integer) {
592 i->integer = 1;
593 changed = 1;
594 }
595 if (i->integer) {
596 if (i->openmin) {
597 i->min++;
598 i->openmin = 0;
599 }
600 if (i->openmax) {
601 i->max--;
602 i->openmax = 0;
603 }
604 } else if (!i->openmin && !i->openmax && i->min == i->max)
605 i->integer = 1;
606 if (snd_interval_checkempty(i)) {
607 snd_interval_none(i);
608 return -EINVAL;
609 }
610 return changed;
611 }
612 EXPORT_SYMBOL(snd_interval_refine);
613
614 static int snd_interval_refine_first(struct snd_interval *i)
615 {
616 const unsigned int last_max = i->max;
617
618 if (snd_BUG_ON(snd_interval_empty(i)))
619 return -EINVAL;
620 if (snd_interval_single(i))
621 return 0;
622 i->max = i->min;
623 if (i->openmin)
624 i->max++;
625
626 i->openmax = (i->openmax && i->max >= last_max);
627 return 1;
628 }
629
630 static int snd_interval_refine_last(struct snd_interval *i)
631 {
632 const unsigned int last_min = i->min;
633
634 if (snd_BUG_ON(snd_interval_empty(i)))
635 return -EINVAL;
636 if (snd_interval_single(i))
637 return 0;
638 i->min = i->max;
639 if (i->openmax)
640 i->min--;
641
642 i->openmin = (i->openmin && i->min <= last_min);
643 return 1;
644 }
645
646 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
647 {
648 if (a->empty || b->empty) {
649 snd_interval_none(c);
650 return;
651 }
652 c->empty = 0;
653 c->min = mul(a->min, b->min);
654 c->openmin = (a->openmin || b->openmin);
655 c->max = mul(a->max, b->max);
656 c->openmax = (a->openmax || b->openmax);
657 c->integer = (a->integer && b->integer);
658 }
659
660
661
662
663
664
665
666
667
668
669
670 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
671 {
672 unsigned int r;
673 if (a->empty || b->empty) {
674 snd_interval_none(c);
675 return;
676 }
677 c->empty = 0;
678 c->min = div32(a->min, b->max, &r);
679 c->openmin = (r || a->openmin || b->openmax);
680 if (b->min > 0) {
681 c->max = div32(a->max, b->min, &r);
682 if (r) {
683 c->max++;
684 c->openmax = 1;
685 } else
686 c->openmax = (a->openmax || b->openmin);
687 } else {
688 c->max = UINT_MAX;
689 c->openmax = 0;
690 }
691 c->integer = 0;
692 }
693
694
695
696
697
698
699
700
701
702
703
704
705 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
706 unsigned int k, struct snd_interval *c)
707 {
708 unsigned int r;
709 if (a->empty || b->empty) {
710 snd_interval_none(c);
711 return;
712 }
713 c->empty = 0;
714 c->min = muldiv32(a->min, b->min, k, &r);
715 c->openmin = (r || a->openmin || b->openmin);
716 c->max = muldiv32(a->max, b->max, k, &r);
717 if (r) {
718 c->max++;
719 c->openmax = 1;
720 } else
721 c->openmax = (a->openmax || b->openmax);
722 c->integer = 0;
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
737 const struct snd_interval *b, struct snd_interval *c)
738 {
739 unsigned int r;
740 if (a->empty || b->empty) {
741 snd_interval_none(c);
742 return;
743 }
744 c->empty = 0;
745 c->min = muldiv32(a->min, k, b->max, &r);
746 c->openmin = (r || a->openmin || b->openmax);
747 if (b->min > 0) {
748 c->max = muldiv32(a->max, k, b->min, &r);
749 if (r) {
750 c->max++;
751 c->openmax = 1;
752 } else
753 c->openmax = (a->openmax || b->openmin);
754 } else {
755 c->max = UINT_MAX;
756 c->openmax = 0;
757 }
758 c->integer = 0;
759 }
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775 int snd_interval_ratnum(struct snd_interval *i,
776 unsigned int rats_count, const struct snd_ratnum *rats,
777 unsigned int *nump, unsigned int *denp)
778 {
779 unsigned int best_num, best_den;
780 int best_diff;
781 unsigned int k;
782 struct snd_interval t;
783 int err;
784 unsigned int result_num, result_den;
785 int result_diff;
786
787 best_num = best_den = best_diff = 0;
788 for (k = 0; k < rats_count; ++k) {
789 unsigned int num = rats[k].num;
790 unsigned int den;
791 unsigned int q = i->min;
792 int diff;
793 if (q == 0)
794 q = 1;
795 den = div_up(num, q);
796 if (den < rats[k].den_min)
797 continue;
798 if (den > rats[k].den_max)
799 den = rats[k].den_max;
800 else {
801 unsigned int r;
802 r = (den - rats[k].den_min) % rats[k].den_step;
803 if (r != 0)
804 den -= r;
805 }
806 diff = num - q * den;
807 if (diff < 0)
808 diff = -diff;
809 if (best_num == 0 ||
810 diff * best_den < best_diff * den) {
811 best_diff = diff;
812 best_den = den;
813 best_num = num;
814 }
815 }
816 if (best_den == 0) {
817 i->empty = 1;
818 return -EINVAL;
819 }
820 t.min = div_down(best_num, best_den);
821 t.openmin = !!(best_num % best_den);
822
823 result_num = best_num;
824 result_diff = best_diff;
825 result_den = best_den;
826 best_num = best_den = best_diff = 0;
827 for (k = 0; k < rats_count; ++k) {
828 unsigned int num = rats[k].num;
829 unsigned int den;
830 unsigned int q = i->max;
831 int diff;
832 if (q == 0) {
833 i->empty = 1;
834 return -EINVAL;
835 }
836 den = div_down(num, q);
837 if (den > rats[k].den_max)
838 continue;
839 if (den < rats[k].den_min)
840 den = rats[k].den_min;
841 else {
842 unsigned int r;
843 r = (den - rats[k].den_min) % rats[k].den_step;
844 if (r != 0)
845 den += rats[k].den_step - r;
846 }
847 diff = q * den - num;
848 if (diff < 0)
849 diff = -diff;
850 if (best_num == 0 ||
851 diff * best_den < best_diff * den) {
852 best_diff = diff;
853 best_den = den;
854 best_num = num;
855 }
856 }
857 if (best_den == 0) {
858 i->empty = 1;
859 return -EINVAL;
860 }
861 t.max = div_up(best_num, best_den);
862 t.openmax = !!(best_num % best_den);
863 t.integer = 0;
864 err = snd_interval_refine(i, &t);
865 if (err < 0)
866 return err;
867
868 if (snd_interval_single(i)) {
869 if (best_diff * result_den < result_diff * best_den) {
870 result_num = best_num;
871 result_den = best_den;
872 }
873 if (nump)
874 *nump = result_num;
875 if (denp)
876 *denp = result_den;
877 }
878 return err;
879 }
880 EXPORT_SYMBOL(snd_interval_ratnum);
881
882
883
884
885
886
887
888
889
890
891
892
893 static int snd_interval_ratden(struct snd_interval *i,
894 unsigned int rats_count,
895 const struct snd_ratden *rats,
896 unsigned int *nump, unsigned int *denp)
897 {
898 unsigned int best_num, best_diff, best_den;
899 unsigned int k;
900 struct snd_interval t;
901 int err;
902
903 best_num = best_den = best_diff = 0;
904 for (k = 0; k < rats_count; ++k) {
905 unsigned int num;
906 unsigned int den = rats[k].den;
907 unsigned int q = i->min;
908 int diff;
909 num = mul(q, den);
910 if (num > rats[k].num_max)
911 continue;
912 if (num < rats[k].num_min)
913 num = rats[k].num_max;
914 else {
915 unsigned int r;
916 r = (num - rats[k].num_min) % rats[k].num_step;
917 if (r != 0)
918 num += rats[k].num_step - r;
919 }
920 diff = num - q * den;
921 if (best_num == 0 ||
922 diff * best_den < best_diff * den) {
923 best_diff = diff;
924 best_den = den;
925 best_num = num;
926 }
927 }
928 if (best_den == 0) {
929 i->empty = 1;
930 return -EINVAL;
931 }
932 t.min = div_down(best_num, best_den);
933 t.openmin = !!(best_num % best_den);
934
935 best_num = best_den = best_diff = 0;
936 for (k = 0; k < rats_count; ++k) {
937 unsigned int num;
938 unsigned int den = rats[k].den;
939 unsigned int q = i->max;
940 int diff;
941 num = mul(q, den);
942 if (num < rats[k].num_min)
943 continue;
944 if (num > rats[k].num_max)
945 num = rats[k].num_max;
946 else {
947 unsigned int r;
948 r = (num - rats[k].num_min) % rats[k].num_step;
949 if (r != 0)
950 num -= r;
951 }
952 diff = q * den - num;
953 if (best_num == 0 ||
954 diff * best_den < best_diff * den) {
955 best_diff = diff;
956 best_den = den;
957 best_num = num;
958 }
959 }
960 if (best_den == 0) {
961 i->empty = 1;
962 return -EINVAL;
963 }
964 t.max = div_up(best_num, best_den);
965 t.openmax = !!(best_num % best_den);
966 t.integer = 0;
967 err = snd_interval_refine(i, &t);
968 if (err < 0)
969 return err;
970
971 if (snd_interval_single(i)) {
972 if (nump)
973 *nump = best_num;
974 if (denp)
975 *denp = best_den;
976 }
977 return err;
978 }
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994 int snd_interval_list(struct snd_interval *i, unsigned int count,
995 const unsigned int *list, unsigned int mask)
996 {
997 unsigned int k;
998 struct snd_interval list_range;
999
1000 if (!count) {
1001 i->empty = 1;
1002 return -EINVAL;
1003 }
1004 snd_interval_any(&list_range);
1005 list_range.min = UINT_MAX;
1006 list_range.max = 0;
1007 for (k = 0; k < count; k++) {
1008 if (mask && !(mask & (1 << k)))
1009 continue;
1010 if (!snd_interval_test(i, list[k]))
1011 continue;
1012 list_range.min = min(list_range.min, list[k]);
1013 list_range.max = max(list_range.max, list[k]);
1014 }
1015 return snd_interval_refine(i, &list_range);
1016 }
1017 EXPORT_SYMBOL(snd_interval_list);
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1034 const struct snd_interval *ranges, unsigned int mask)
1035 {
1036 unsigned int k;
1037 struct snd_interval range_union;
1038 struct snd_interval range;
1039
1040 if (!count) {
1041 snd_interval_none(i);
1042 return -EINVAL;
1043 }
1044 snd_interval_any(&range_union);
1045 range_union.min = UINT_MAX;
1046 range_union.max = 0;
1047 for (k = 0; k < count; k++) {
1048 if (mask && !(mask & (1 << k)))
1049 continue;
1050 snd_interval_copy(&range, &ranges[k]);
1051 if (snd_interval_refine(&range, i) < 0)
1052 continue;
1053 if (snd_interval_empty(&range))
1054 continue;
1055
1056 if (range.min < range_union.min) {
1057 range_union.min = range.min;
1058 range_union.openmin = 1;
1059 }
1060 if (range.min == range_union.min && !range.openmin)
1061 range_union.openmin = 0;
1062 if (range.max > range_union.max) {
1063 range_union.max = range.max;
1064 range_union.openmax = 1;
1065 }
1066 if (range.max == range_union.max && !range.openmax)
1067 range_union.openmax = 0;
1068 }
1069 return snd_interval_refine(i, &range_union);
1070 }
1071 EXPORT_SYMBOL(snd_interval_ranges);
1072
1073 static int snd_interval_step(struct snd_interval *i, unsigned int step)
1074 {
1075 unsigned int n;
1076 int changed = 0;
1077 n = i->min % step;
1078 if (n != 0 || i->openmin) {
1079 i->min += step - n;
1080 i->openmin = 0;
1081 changed = 1;
1082 }
1083 n = i->max % step;
1084 if (n != 0 || i->openmax) {
1085 i->max -= n;
1086 i->openmax = 0;
1087 changed = 1;
1088 }
1089 if (snd_interval_checkempty(i)) {
1090 i->empty = 1;
1091 return -EINVAL;
1092 }
1093 return changed;
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1110 int var,
1111 snd_pcm_hw_rule_func_t func, void *private,
1112 int dep, ...)
1113 {
1114 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1115 struct snd_pcm_hw_rule *c;
1116 unsigned int k;
1117 va_list args;
1118 va_start(args, dep);
1119 if (constrs->rules_num >= constrs->rules_all) {
1120 struct snd_pcm_hw_rule *new;
1121 unsigned int new_rules = constrs->rules_all + 16;
1122 new = krealloc(constrs->rules, new_rules * sizeof(*c),
1123 GFP_KERNEL);
1124 if (!new) {
1125 va_end(args);
1126 return -ENOMEM;
1127 }
1128 constrs->rules = new;
1129 constrs->rules_all = new_rules;
1130 }
1131 c = &constrs->rules[constrs->rules_num];
1132 c->cond = cond;
1133 c->func = func;
1134 c->var = var;
1135 c->private = private;
1136 k = 0;
1137 while (1) {
1138 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1139 va_end(args);
1140 return -EINVAL;
1141 }
1142 c->deps[k++] = dep;
1143 if (dep < 0)
1144 break;
1145 dep = va_arg(args, int);
1146 }
1147 constrs->rules_num++;
1148 va_end(args);
1149 return 0;
1150 }
1151 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1164 u_int32_t mask)
1165 {
1166 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1167 struct snd_mask *maskp = constrs_mask(constrs, var);
1168 *maskp->bits &= mask;
1169 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8);
1170 if (*maskp->bits == 0)
1171 return -EINVAL;
1172 return 0;
1173 }
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1186 u_int64_t mask)
1187 {
1188 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1189 struct snd_mask *maskp = constrs_mask(constrs, var);
1190 maskp->bits[0] &= (u_int32_t)mask;
1191 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1192 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8);
1193 if (! maskp->bits[0] && ! maskp->bits[1])
1194 return -EINVAL;
1195 return 0;
1196 }
1197 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1210 {
1211 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1212 return snd_interval_setinteger(constrs_interval(constrs, var));
1213 }
1214 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1229 unsigned int min, unsigned int max)
1230 {
1231 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1232 struct snd_interval t;
1233 t.min = min;
1234 t.max = max;
1235 t.openmin = t.openmax = 0;
1236 t.integer = 0;
1237 return snd_interval_refine(constrs_interval(constrs, var), &t);
1238 }
1239 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1240
1241 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1242 struct snd_pcm_hw_rule *rule)
1243 {
1244 struct snd_pcm_hw_constraint_list *list = rule->private;
1245 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1246 }
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1261 unsigned int cond,
1262 snd_pcm_hw_param_t var,
1263 const struct snd_pcm_hw_constraint_list *l)
1264 {
1265 return snd_pcm_hw_rule_add(runtime, cond, var,
1266 snd_pcm_hw_rule_list, (void *)l,
1267 var, -1);
1268 }
1269 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1270
1271 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1272 struct snd_pcm_hw_rule *rule)
1273 {
1274 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1275 return snd_interval_ranges(hw_param_interval(params, rule->var),
1276 r->count, r->ranges, r->mask);
1277 }
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1292 unsigned int cond,
1293 snd_pcm_hw_param_t var,
1294 const struct snd_pcm_hw_constraint_ranges *r)
1295 {
1296 return snd_pcm_hw_rule_add(runtime, cond, var,
1297 snd_pcm_hw_rule_ranges, (void *)r,
1298 var, -1);
1299 }
1300 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1301
1302 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1303 struct snd_pcm_hw_rule *rule)
1304 {
1305 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1306 unsigned int num = 0, den = 0;
1307 int err;
1308 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1309 r->nrats, r->rats, &num, &den);
1310 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1311 params->rate_num = num;
1312 params->rate_den = den;
1313 }
1314 return err;
1315 }
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1327 unsigned int cond,
1328 snd_pcm_hw_param_t var,
1329 const struct snd_pcm_hw_constraint_ratnums *r)
1330 {
1331 return snd_pcm_hw_rule_add(runtime, cond, var,
1332 snd_pcm_hw_rule_ratnums, (void *)r,
1333 var, -1);
1334 }
1335 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1336
1337 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1338 struct snd_pcm_hw_rule *rule)
1339 {
1340 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1341 unsigned int num = 0, den = 0;
1342 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1343 r->nrats, r->rats, &num, &den);
1344 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1345 params->rate_num = num;
1346 params->rate_den = den;
1347 }
1348 return err;
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1361 unsigned int cond,
1362 snd_pcm_hw_param_t var,
1363 const struct snd_pcm_hw_constraint_ratdens *r)
1364 {
1365 return snd_pcm_hw_rule_add(runtime, cond, var,
1366 snd_pcm_hw_rule_ratdens, (void *)r,
1367 var, -1);
1368 }
1369 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1370
1371 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1372 struct snd_pcm_hw_rule *rule)
1373 {
1374 unsigned int l = (unsigned long) rule->private;
1375 int width = l & 0xffff;
1376 unsigned int msbits = l >> 16;
1377 const struct snd_interval *i =
1378 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1379
1380 if (!snd_interval_single(i))
1381 return 0;
1382
1383 if ((snd_interval_value(i) == width) ||
1384 (width == 0 && snd_interval_value(i) > msbits))
1385 params->msbits = min_not_zero(params->msbits, msbits);
1386
1387 return 0;
1388 }
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1405 unsigned int cond,
1406 unsigned int width,
1407 unsigned int msbits)
1408 {
1409 unsigned long l = (msbits << 16) | width;
1410 return snd_pcm_hw_rule_add(runtime, cond, -1,
1411 snd_pcm_hw_rule_msbits,
1412 (void*) l,
1413 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1414 }
1415 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1416
1417 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1418 struct snd_pcm_hw_rule *rule)
1419 {
1420 unsigned long step = (unsigned long) rule->private;
1421 return snd_interval_step(hw_param_interval(params, rule->var), step);
1422 }
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1434 unsigned int cond,
1435 snd_pcm_hw_param_t var,
1436 unsigned long step)
1437 {
1438 return snd_pcm_hw_rule_add(runtime, cond, var,
1439 snd_pcm_hw_rule_step, (void *) step,
1440 var, -1);
1441 }
1442 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1443
1444 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1445 {
1446 static unsigned int pow2_sizes[] = {
1447 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1448 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1449 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1450 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1451 };
1452 return snd_interval_list(hw_param_interval(params, rule->var),
1453 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1454 }
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1465 unsigned int cond,
1466 snd_pcm_hw_param_t var)
1467 {
1468 return snd_pcm_hw_rule_add(runtime, cond, var,
1469 snd_pcm_hw_rule_pow2, NULL,
1470 var, -1);
1471 }
1472 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1473
1474 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1475 struct snd_pcm_hw_rule *rule)
1476 {
1477 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1478 struct snd_interval *rate;
1479
1480 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1481 return snd_interval_list(rate, 1, &base_rate, 0);
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1492 unsigned int base_rate)
1493 {
1494 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1495 SNDRV_PCM_HW_PARAM_RATE,
1496 snd_pcm_hw_rule_noresample_func,
1497 (void *)(uintptr_t)base_rate,
1498 SNDRV_PCM_HW_PARAM_RATE, -1);
1499 }
1500 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1501
1502 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1503 snd_pcm_hw_param_t var)
1504 {
1505 if (hw_is_mask(var)) {
1506 snd_mask_any(hw_param_mask(params, var));
1507 params->cmask |= 1 << var;
1508 params->rmask |= 1 << var;
1509 return;
1510 }
1511 if (hw_is_interval(var)) {
1512 snd_interval_any(hw_param_interval(params, var));
1513 params->cmask |= 1 << var;
1514 params->rmask |= 1 << var;
1515 return;
1516 }
1517 snd_BUG();
1518 }
1519
1520 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1521 {
1522 unsigned int k;
1523 memset(params, 0, sizeof(*params));
1524 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1525 _snd_pcm_hw_param_any(params, k);
1526 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1527 _snd_pcm_hw_param_any(params, k);
1528 params->info = ~0U;
1529 }
1530 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1542 snd_pcm_hw_param_t var, int *dir)
1543 {
1544 if (hw_is_mask(var)) {
1545 const struct snd_mask *mask = hw_param_mask_c(params, var);
1546 if (!snd_mask_single(mask))
1547 return -EINVAL;
1548 if (dir)
1549 *dir = 0;
1550 return snd_mask_value(mask);
1551 }
1552 if (hw_is_interval(var)) {
1553 const struct snd_interval *i = hw_param_interval_c(params, var);
1554 if (!snd_interval_single(i))
1555 return -EINVAL;
1556 if (dir)
1557 *dir = i->openmin;
1558 return snd_interval_value(i);
1559 }
1560 return -EINVAL;
1561 }
1562 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1563
1564 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1565 snd_pcm_hw_param_t var)
1566 {
1567 if (hw_is_mask(var)) {
1568 snd_mask_none(hw_param_mask(params, var));
1569 params->cmask |= 1 << var;
1570 params->rmask |= 1 << var;
1571 } else if (hw_is_interval(var)) {
1572 snd_interval_none(hw_param_interval(params, var));
1573 params->cmask |= 1 << var;
1574 params->rmask |= 1 << var;
1575 } else {
1576 snd_BUG();
1577 }
1578 }
1579 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1580
1581 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1582 snd_pcm_hw_param_t var)
1583 {
1584 int changed;
1585 if (hw_is_mask(var))
1586 changed = snd_mask_refine_first(hw_param_mask(params, var));
1587 else if (hw_is_interval(var))
1588 changed = snd_interval_refine_first(hw_param_interval(params, var));
1589 else
1590 return -EINVAL;
1591 if (changed > 0) {
1592 params->cmask |= 1 << var;
1593 params->rmask |= 1 << var;
1594 }
1595 return changed;
1596 }
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1612 struct snd_pcm_hw_params *params,
1613 snd_pcm_hw_param_t var, int *dir)
1614 {
1615 int changed = _snd_pcm_hw_param_first(params, var);
1616 if (changed < 0)
1617 return changed;
1618 if (params->rmask) {
1619 int err = snd_pcm_hw_refine(pcm, params);
1620 if (err < 0)
1621 return err;
1622 }
1623 return snd_pcm_hw_param_value(params, var, dir);
1624 }
1625 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1626
1627 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1628 snd_pcm_hw_param_t var)
1629 {
1630 int changed;
1631 if (hw_is_mask(var))
1632 changed = snd_mask_refine_last(hw_param_mask(params, var));
1633 else if (hw_is_interval(var))
1634 changed = snd_interval_refine_last(hw_param_interval(params, var));
1635 else
1636 return -EINVAL;
1637 if (changed > 0) {
1638 params->cmask |= 1 << var;
1639 params->rmask |= 1 << var;
1640 }
1641 return changed;
1642 }
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1658 struct snd_pcm_hw_params *params,
1659 snd_pcm_hw_param_t var, int *dir)
1660 {
1661 int changed = _snd_pcm_hw_param_last(params, var);
1662 if (changed < 0)
1663 return changed;
1664 if (params->rmask) {
1665 int err = snd_pcm_hw_refine(pcm, params);
1666 if (err < 0)
1667 return err;
1668 }
1669 return snd_pcm_hw_param_value(params, var, dir);
1670 }
1671 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1672
1673 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1674 void *arg)
1675 {
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 unsigned long flags;
1678 snd_pcm_stream_lock_irqsave(substream, flags);
1679 if (snd_pcm_running(substream) &&
1680 snd_pcm_update_hw_ptr(substream) >= 0)
1681 runtime->status->hw_ptr %= runtime->buffer_size;
1682 else {
1683 runtime->status->hw_ptr = 0;
1684 runtime->hw_ptr_wrap = 0;
1685 }
1686 snd_pcm_stream_unlock_irqrestore(substream, flags);
1687 return 0;
1688 }
1689
1690 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1691 void *arg)
1692 {
1693 struct snd_pcm_channel_info *info = arg;
1694 struct snd_pcm_runtime *runtime = substream->runtime;
1695 int width;
1696 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1697 info->offset = -1;
1698 return 0;
1699 }
1700 width = snd_pcm_format_physical_width(runtime->format);
1701 if (width < 0)
1702 return width;
1703 info->offset = 0;
1704 switch (runtime->access) {
1705 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1706 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1707 info->first = info->channel * width;
1708 info->step = runtime->channels * width;
1709 break;
1710 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1711 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1712 {
1713 size_t size = runtime->dma_bytes / runtime->channels;
1714 info->first = info->channel * size * 8;
1715 info->step = width;
1716 break;
1717 }
1718 default:
1719 snd_BUG();
1720 break;
1721 }
1722 return 0;
1723 }
1724
1725 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1726 void *arg)
1727 {
1728 struct snd_pcm_hw_params *params = arg;
1729 snd_pcm_format_t format;
1730 int channels;
1731 ssize_t frame_size;
1732
1733 params->fifo_size = substream->runtime->hw.fifo_size;
1734 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1735 format = params_format(params);
1736 channels = params_channels(params);
1737 frame_size = snd_pcm_format_size(format, channels);
1738 if (frame_size > 0)
1739 params->fifo_size /= (unsigned)frame_size;
1740 }
1741 return 0;
1742 }
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1756 unsigned int cmd, void *arg)
1757 {
1758 switch (cmd) {
1759 case SNDRV_PCM_IOCTL1_RESET:
1760 return snd_pcm_lib_ioctl_reset(substream, arg);
1761 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1762 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1763 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1764 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1765 }
1766 return -ENXIO;
1767 }
1768 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1782 {
1783 struct snd_pcm_runtime *runtime;
1784 unsigned long flags;
1785
1786 if (snd_BUG_ON(!substream))
1787 return;
1788
1789 snd_pcm_stream_lock_irqsave(substream, flags);
1790 if (PCM_RUNTIME_CHECK(substream))
1791 goto _unlock;
1792 runtime = substream->runtime;
1793
1794 if (!snd_pcm_running(substream) ||
1795 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1796 goto _end;
1797
1798 #ifdef CONFIG_SND_PCM_TIMER
1799 if (substream->timer_running)
1800 snd_timer_interrupt(substream->timer, 1);
1801 #endif
1802 _end:
1803 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1804 _unlock:
1805 snd_pcm_stream_unlock_irqrestore(substream, flags);
1806 }
1807 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1808
1809
1810
1811
1812
1813
1814
1815 static int wait_for_avail(struct snd_pcm_substream *substream,
1816 snd_pcm_uframes_t *availp)
1817 {
1818 struct snd_pcm_runtime *runtime = substream->runtime;
1819 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1820 wait_queue_entry_t wait;
1821 int err = 0;
1822 snd_pcm_uframes_t avail = 0;
1823 long wait_time, tout;
1824
1825 init_waitqueue_entry(&wait, current);
1826 set_current_state(TASK_INTERRUPTIBLE);
1827 add_wait_queue(&runtime->tsleep, &wait);
1828
1829 if (runtime->no_period_wakeup)
1830 wait_time = MAX_SCHEDULE_TIMEOUT;
1831 else {
1832
1833 if (substream->wait_time) {
1834 wait_time = substream->wait_time;
1835 } else {
1836 wait_time = 10;
1837
1838 if (runtime->rate) {
1839 long t = runtime->period_size * 2 /
1840 runtime->rate;
1841 wait_time = max(t, wait_time);
1842 }
1843 wait_time = msecs_to_jiffies(wait_time * 1000);
1844 }
1845 }
1846
1847 for (;;) {
1848 if (signal_pending(current)) {
1849 err = -ERESTARTSYS;
1850 break;
1851 }
1852
1853
1854
1855
1856
1857
1858
1859
1860 avail = snd_pcm_avail(substream);
1861 if (avail >= runtime->twake)
1862 break;
1863 snd_pcm_stream_unlock_irq(substream);
1864
1865 tout = schedule_timeout(wait_time);
1866
1867 snd_pcm_stream_lock_irq(substream);
1868 set_current_state(TASK_INTERRUPTIBLE);
1869 switch (runtime->status->state) {
1870 case SNDRV_PCM_STATE_SUSPENDED:
1871 err = -ESTRPIPE;
1872 goto _endloop;
1873 case SNDRV_PCM_STATE_XRUN:
1874 err = -EPIPE;
1875 goto _endloop;
1876 case SNDRV_PCM_STATE_DRAINING:
1877 if (is_playback)
1878 err = -EPIPE;
1879 else
1880 avail = 0;
1881 goto _endloop;
1882 case SNDRV_PCM_STATE_OPEN:
1883 case SNDRV_PCM_STATE_SETUP:
1884 case SNDRV_PCM_STATE_DISCONNECTED:
1885 err = -EBADFD;
1886 goto _endloop;
1887 case SNDRV_PCM_STATE_PAUSED:
1888 continue;
1889 }
1890 if (!tout) {
1891 pcm_dbg(substream->pcm,
1892 "%s write error (DMA or IRQ trouble?)\n",
1893 is_playback ? "playback" : "capture");
1894 err = -EIO;
1895 break;
1896 }
1897 }
1898 _endloop:
1899 set_current_state(TASK_RUNNING);
1900 remove_wait_queue(&runtime->tsleep, &wait);
1901 *availp = avail;
1902 return err;
1903 }
1904
1905 typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
1906 int channel, unsigned long hwoff,
1907 void *buf, unsigned long bytes);
1908
1909 typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1910 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
1911
1912
1913 static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
1914 int channel, unsigned long hwoff)
1915 {
1916 return runtime->dma_area + hwoff +
1917 channel * (runtime->dma_bytes / runtime->channels);
1918 }
1919
1920
1921 static int default_write_copy(struct snd_pcm_substream *substream,
1922 int channel, unsigned long hwoff,
1923 void *buf, unsigned long bytes)
1924 {
1925 if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
1926 (void __user *)buf, bytes))
1927 return -EFAULT;
1928 return 0;
1929 }
1930
1931
1932 static int default_write_copy_kernel(struct snd_pcm_substream *substream,
1933 int channel, unsigned long hwoff,
1934 void *buf, unsigned long bytes)
1935 {
1936 memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
1937 return 0;
1938 }
1939
1940
1941
1942
1943
1944 static int fill_silence(struct snd_pcm_substream *substream, int channel,
1945 unsigned long hwoff, void *buf, unsigned long bytes)
1946 {
1947 struct snd_pcm_runtime *runtime = substream->runtime;
1948
1949 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1950 return 0;
1951 if (substream->ops->fill_silence)
1952 return substream->ops->fill_silence(substream, channel,
1953 hwoff, bytes);
1954
1955 snd_pcm_format_set_silence(runtime->format,
1956 get_dma_ptr(runtime, channel, hwoff),
1957 bytes_to_samples(runtime, bytes));
1958 return 0;
1959 }
1960
1961
1962 static int default_read_copy(struct snd_pcm_substream *substream,
1963 int channel, unsigned long hwoff,
1964 void *buf, unsigned long bytes)
1965 {
1966 if (copy_to_user((void __user *)buf,
1967 get_dma_ptr(substream->runtime, channel, hwoff),
1968 bytes))
1969 return -EFAULT;
1970 return 0;
1971 }
1972
1973
1974 static int default_read_copy_kernel(struct snd_pcm_substream *substream,
1975 int channel, unsigned long hwoff,
1976 void *buf, unsigned long bytes)
1977 {
1978 memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
1979 return 0;
1980 }
1981
1982
1983
1984
1985 static int interleaved_copy(struct snd_pcm_substream *substream,
1986 snd_pcm_uframes_t hwoff, void *data,
1987 snd_pcm_uframes_t off,
1988 snd_pcm_uframes_t frames,
1989 pcm_transfer_f transfer)
1990 {
1991 struct snd_pcm_runtime *runtime = substream->runtime;
1992
1993
1994 hwoff = frames_to_bytes(runtime, hwoff);
1995 off = frames_to_bytes(runtime, off);
1996 frames = frames_to_bytes(runtime, frames);
1997 return transfer(substream, 0, hwoff, data + off, frames);
1998 }
1999
2000
2001
2002
2003 static int noninterleaved_copy(struct snd_pcm_substream *substream,
2004 snd_pcm_uframes_t hwoff, void *data,
2005 snd_pcm_uframes_t off,
2006 snd_pcm_uframes_t frames,
2007 pcm_transfer_f transfer)
2008 {
2009 struct snd_pcm_runtime *runtime = substream->runtime;
2010 int channels = runtime->channels;
2011 void **bufs = data;
2012 int c, err;
2013
2014
2015
2016
2017
2018 off = samples_to_bytes(runtime, off);
2019 frames = samples_to_bytes(runtime, frames);
2020 hwoff = samples_to_bytes(runtime, hwoff);
2021 for (c = 0; c < channels; ++c, ++bufs) {
2022 if (!data || !*bufs)
2023 err = fill_silence(substream, c, hwoff, NULL, frames);
2024 else
2025 err = transfer(substream, c, hwoff, *bufs + off,
2026 frames);
2027 if (err < 0)
2028 return err;
2029 }
2030 return 0;
2031 }
2032
2033
2034
2035
2036 static int fill_silence_frames(struct snd_pcm_substream *substream,
2037 snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2038 {
2039 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2040 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2041 return interleaved_copy(substream, off, NULL, 0, frames,
2042 fill_silence);
2043 else
2044 return noninterleaved_copy(substream, off, NULL, 0, frames,
2045 fill_silence);
2046 }
2047
2048
2049 static int pcm_sanity_check(struct snd_pcm_substream *substream)
2050 {
2051 struct snd_pcm_runtime *runtime;
2052 if (PCM_RUNTIME_CHECK(substream))
2053 return -ENXIO;
2054 runtime = substream->runtime;
2055 if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
2056 return -EINVAL;
2057 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2058 return -EBADFD;
2059 return 0;
2060 }
2061
2062 static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
2063 {
2064 switch (runtime->status->state) {
2065 case SNDRV_PCM_STATE_PREPARED:
2066 case SNDRV_PCM_STATE_RUNNING:
2067 case SNDRV_PCM_STATE_PAUSED:
2068 return 0;
2069 case SNDRV_PCM_STATE_XRUN:
2070 return -EPIPE;
2071 case SNDRV_PCM_STATE_SUSPENDED:
2072 return -ESTRPIPE;
2073 default:
2074 return -EBADFD;
2075 }
2076 }
2077
2078
2079
2080
2081 int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
2082 snd_pcm_uframes_t appl_ptr)
2083 {
2084 struct snd_pcm_runtime *runtime = substream->runtime;
2085 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2086 int ret;
2087
2088 if (old_appl_ptr == appl_ptr)
2089 return 0;
2090
2091 runtime->control->appl_ptr = appl_ptr;
2092 if (substream->ops->ack) {
2093 ret = substream->ops->ack(substream);
2094 if (ret < 0) {
2095 runtime->control->appl_ptr = old_appl_ptr;
2096 return ret;
2097 }
2098 }
2099
2100 trace_applptr(substream, old_appl_ptr, appl_ptr);
2101
2102 return 0;
2103 }
2104
2105
2106 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2107 void *data, bool interleaved,
2108 snd_pcm_uframes_t size, bool in_kernel)
2109 {
2110 struct snd_pcm_runtime *runtime = substream->runtime;
2111 snd_pcm_uframes_t xfer = 0;
2112 snd_pcm_uframes_t offset = 0;
2113 snd_pcm_uframes_t avail;
2114 pcm_copy_f writer;
2115 pcm_transfer_f transfer;
2116 bool nonblock;
2117 bool is_playback;
2118 int err;
2119
2120 err = pcm_sanity_check(substream);
2121 if (err < 0)
2122 return err;
2123
2124 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2125 if (interleaved) {
2126 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2127 runtime->channels > 1)
2128 return -EINVAL;
2129 writer = interleaved_copy;
2130 } else {
2131 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2132 return -EINVAL;
2133 writer = noninterleaved_copy;
2134 }
2135
2136 if (!data) {
2137 if (is_playback)
2138 transfer = fill_silence;
2139 else
2140 return -EINVAL;
2141 } else if (in_kernel) {
2142 if (substream->ops->copy_kernel)
2143 transfer = substream->ops->copy_kernel;
2144 else
2145 transfer = is_playback ?
2146 default_write_copy_kernel : default_read_copy_kernel;
2147 } else {
2148 if (substream->ops->copy_user)
2149 transfer = (pcm_transfer_f)substream->ops->copy_user;
2150 else
2151 transfer = is_playback ?
2152 default_write_copy : default_read_copy;
2153 }
2154
2155 if (size == 0)
2156 return 0;
2157
2158 nonblock = !!(substream->f_flags & O_NONBLOCK);
2159
2160 snd_pcm_stream_lock_irq(substream);
2161 err = pcm_accessible_state(runtime);
2162 if (err < 0)
2163 goto _end_unlock;
2164
2165 runtime->twake = runtime->control->avail_min ? : 1;
2166 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2167 snd_pcm_update_hw_ptr(substream);
2168
2169
2170
2171
2172
2173 if (!is_playback &&
2174 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2175 size >= runtime->start_threshold) {
2176 err = snd_pcm_start(substream);
2177 if (err < 0)
2178 goto _end_unlock;
2179 }
2180
2181 avail = snd_pcm_avail(substream);
2182
2183 while (size > 0) {
2184 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2185 snd_pcm_uframes_t cont;
2186 if (!avail) {
2187 if (!is_playback &&
2188 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2189 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2190 goto _end_unlock;
2191 }
2192 if (nonblock) {
2193 err = -EAGAIN;
2194 goto _end_unlock;
2195 }
2196 runtime->twake = min_t(snd_pcm_uframes_t, size,
2197 runtime->control->avail_min ? : 1);
2198 err = wait_for_avail(substream, &avail);
2199 if (err < 0)
2200 goto _end_unlock;
2201 if (!avail)
2202 continue;
2203 }
2204 frames = size > avail ? avail : size;
2205 appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2206 appl_ofs = appl_ptr % runtime->buffer_size;
2207 cont = runtime->buffer_size - appl_ofs;
2208 if (frames > cont)
2209 frames = cont;
2210 if (snd_BUG_ON(!frames)) {
2211 err = -EINVAL;
2212 goto _end_unlock;
2213 }
2214 snd_pcm_stream_unlock_irq(substream);
2215 err = writer(substream, appl_ofs, data, offset, frames,
2216 transfer);
2217 snd_pcm_stream_lock_irq(substream);
2218 if (err < 0)
2219 goto _end_unlock;
2220 err = pcm_accessible_state(runtime);
2221 if (err < 0)
2222 goto _end_unlock;
2223 appl_ptr += frames;
2224 if (appl_ptr >= runtime->boundary)
2225 appl_ptr -= runtime->boundary;
2226 err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2227 if (err < 0)
2228 goto _end_unlock;
2229
2230 offset += frames;
2231 size -= frames;
2232 xfer += frames;
2233 avail -= frames;
2234 if (is_playback &&
2235 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2236 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2237 err = snd_pcm_start(substream);
2238 if (err < 0)
2239 goto _end_unlock;
2240 }
2241 }
2242 _end_unlock:
2243 runtime->twake = 0;
2244 if (xfer > 0 && err >= 0)
2245 snd_pcm_update_state(substream, runtime);
2246 snd_pcm_stream_unlock_irq(substream);
2247 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2248 }
2249 EXPORT_SYMBOL(__snd_pcm_lib_xfer);
2250
2251
2252
2253
2254
2255
2256 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2257 { .channels = 1,
2258 .map = { SNDRV_CHMAP_MONO } },
2259 { .channels = 2,
2260 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2261 { .channels = 4,
2262 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2263 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2264 { .channels = 6,
2265 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2266 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2267 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2268 { .channels = 8,
2269 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2270 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2271 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2272 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2273 { }
2274 };
2275 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2276
2277
2278 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2279 { .channels = 1,
2280 .map = { SNDRV_CHMAP_MONO } },
2281 { .channels = 2,
2282 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2283 { .channels = 4,
2284 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2285 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2286 { .channels = 6,
2287 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2288 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2289 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2290 { .channels = 8,
2291 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2292 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2293 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2294 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2295 { }
2296 };
2297 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2298
2299 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2300 {
2301 if (ch > info->max_channels)
2302 return false;
2303 return !info->channel_mask || (info->channel_mask & (1U << ch));
2304 }
2305
2306 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_info *uinfo)
2308 {
2309 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2310
2311 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2312 uinfo->count = 0;
2313 uinfo->count = info->max_channels;
2314 uinfo->value.integer.min = 0;
2315 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2316 return 0;
2317 }
2318
2319
2320
2321
2322 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324 {
2325 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2326 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2327 struct snd_pcm_substream *substream;
2328 const struct snd_pcm_chmap_elem *map;
2329
2330 if (!info->chmap)
2331 return -EINVAL;
2332 substream = snd_pcm_chmap_substream(info, idx);
2333 if (!substream)
2334 return -ENODEV;
2335 memset(ucontrol->value.integer.value, 0,
2336 sizeof(ucontrol->value.integer.value));
2337 if (!substream->runtime)
2338 return 0;
2339 for (map = info->chmap; map->channels; map++) {
2340 int i;
2341 if (map->channels == substream->runtime->channels &&
2342 valid_chmap_channels(info, map->channels)) {
2343 for (i = 0; i < map->channels; i++)
2344 ucontrol->value.integer.value[i] = map->map[i];
2345 return 0;
2346 }
2347 }
2348 return -EINVAL;
2349 }
2350
2351
2352
2353
2354 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2355 unsigned int size, unsigned int __user *tlv)
2356 {
2357 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2358 const struct snd_pcm_chmap_elem *map;
2359 unsigned int __user *dst;
2360 int c, count = 0;
2361
2362 if (!info->chmap)
2363 return -EINVAL;
2364 if (size < 8)
2365 return -ENOMEM;
2366 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2367 return -EFAULT;
2368 size -= 8;
2369 dst = tlv + 2;
2370 for (map = info->chmap; map->channels; map++) {
2371 int chs_bytes = map->channels * 4;
2372 if (!valid_chmap_channels(info, map->channels))
2373 continue;
2374 if (size < 8)
2375 return -ENOMEM;
2376 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2377 put_user(chs_bytes, dst + 1))
2378 return -EFAULT;
2379 dst += 2;
2380 size -= 8;
2381 count += 8;
2382 if (size < chs_bytes)
2383 return -ENOMEM;
2384 size -= chs_bytes;
2385 count += chs_bytes;
2386 for (c = 0; c < map->channels; c++) {
2387 if (put_user(map->map[c], dst))
2388 return -EFAULT;
2389 dst++;
2390 }
2391 }
2392 if (put_user(count, tlv + 1))
2393 return -EFAULT;
2394 return 0;
2395 }
2396
2397 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2398 {
2399 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2400 info->pcm->streams[info->stream].chmap_kctl = NULL;
2401 kfree(info);
2402 }
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2417 const struct snd_pcm_chmap_elem *chmap,
2418 int max_channels,
2419 unsigned long private_value,
2420 struct snd_pcm_chmap **info_ret)
2421 {
2422 struct snd_pcm_chmap *info;
2423 struct snd_kcontrol_new knew = {
2424 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2425 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2426 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2427 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2428 .info = pcm_chmap_ctl_info,
2429 .get = pcm_chmap_ctl_get,
2430 .tlv.c = pcm_chmap_ctl_tlv,
2431 };
2432 int err;
2433
2434 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2435 return -EBUSY;
2436 info = kzalloc(sizeof(*info), GFP_KERNEL);
2437 if (!info)
2438 return -ENOMEM;
2439 info->pcm = pcm;
2440 info->stream = stream;
2441 info->chmap = chmap;
2442 info->max_channels = max_channels;
2443 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2444 knew.name = "Playback Channel Map";
2445 else
2446 knew.name = "Capture Channel Map";
2447 knew.device = pcm->device;
2448 knew.count = pcm->streams[stream].substream_count;
2449 knew.private_value = private_value;
2450 info->kctl = snd_ctl_new1(&knew, info);
2451 if (!info->kctl) {
2452 kfree(info);
2453 return -ENOMEM;
2454 }
2455 info->kctl->private_free = pcm_chmap_ctl_private_free;
2456 err = snd_ctl_add(pcm->card, info->kctl);
2457 if (err < 0)
2458 return err;
2459 pcm->streams[stream].chmap_kctl = info->kctl;
2460 if (info_ret)
2461 *info_ret = info;
2462 return 0;
2463 }
2464 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);