1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 * Abramo Bagnara <abramo@alsa-project.org>
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>
21 #include "pcm_local.h"
23 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
24 #define CREATE_TRACE_POINTS
25 #include "pcm_trace.h"
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)
33 static int fill_silence_frames(struct snd_pcm_substream
*substream
,
34 snd_pcm_uframes_t off
, snd_pcm_uframes_t frames
);
37 * fill ring buffer with silence
38 * runtime->silence_start: starting pointer to silence area
39 * runtime->silence_filled: size filled with silence
40 * runtime->silence_threshold: threshold from application
41 * runtime->silence_size: maximal size from application
43 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
45 void snd_pcm_playback_silence(struct snd_pcm_substream
*substream
, snd_pcm_uframes_t new_hw_ptr
)
47 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
48 snd_pcm_uframes_t frames
, ofs
, transfer
;
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
;
57 n
+= runtime
->boundary
;
58 if ((snd_pcm_uframes_t
)n
< runtime
->silence_filled
)
59 runtime
->silence_filled
-= n
;
61 runtime
->silence_filled
= 0;
62 runtime
->silence_start
= appl_ptr
;
64 if (runtime
->silence_filled
>= runtime
->buffer_size
)
66 noise_dist
= snd_pcm_playback_hw_avail(runtime
) + runtime
->silence_filled
;
67 if (noise_dist
>= (snd_pcm_sframes_t
) runtime
->silence_threshold
)
69 frames
= runtime
->silence_threshold
- noise_dist
;
70 if (frames
> runtime
->silence_size
)
71 frames
= runtime
->silence_size
;
73 if (new_hw_ptr
== ULONG_MAX
) { /* initialization */
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
) %
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
;
91 runtime
->silence_start
= ofs
;
94 frames
= runtime
->buffer_size
- runtime
->silence_filled
;
96 if (snd_BUG_ON(frames
> runtime
->buffer_size
))
100 ofs
= runtime
->silence_start
% runtime
->buffer_size
;
102 transfer
= ofs
+ frames
> runtime
->buffer_size
? runtime
->buffer_size
- ofs
: frames
;
103 err
= fill_silence_frames(substream
, ofs
, transfer
);
105 runtime
->silence_filled
+= transfer
;
111 #ifdef CONFIG_SND_DEBUG
112 void snd_pcm_debug_name(struct snd_pcm_substream
*substream
,
113 char *name
, size_t len
)
115 snprintf(name
, len
, "pcmC%dD%d%c:%d",
116 substream
->pcm
->card
->number
,
117 substream
->pcm
->device
,
118 substream
->stream
? 'c' : 'p',
121 EXPORT_SYMBOL(snd_pcm_debug_name
);
124 #define XRUN_DEBUG_BASIC (1<<0)
125 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
126 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
130 #define xrun_debug(substream, mask) \
131 ((substream)->pstr->xrun_debug & (mask))
133 #define xrun_debug(substream, mask) 0
136 #define dump_stack_on_xrun(substream) do { \
137 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
141 /* call with stream lock held */
142 void __snd_pcm_xrun(struct snd_pcm_substream
*substream
)
144 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
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
)) {
152 snd_pcm_debug_name(substream
, name
, sizeof(name
));
153 pcm_warn(substream
->pcm
, "XRUN: %s\n", name
);
154 dump_stack_on_xrun(substream
);
158 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
159 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
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); \
169 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
171 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
175 int snd_pcm_update_state(struct snd_pcm_substream
*substream
,
176 struct snd_pcm_runtime
*runtime
)
178 snd_pcm_uframes_t avail
;
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
);
189 if (avail
>= runtime
->stop_threshold
) {
190 __snd_pcm_xrun(substream
);
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
);
202 static void update_audio_tstamp(struct snd_pcm_substream
*substream
,
203 struct timespec
*curr_tstamp
,
204 struct timespec
*audio_tstamp
)
206 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
207 u64 audio_frames
, audio_nsecs
;
208 struct timespec driver_tstamp
;
210 if (runtime
->tstamp_mode
!= SNDRV_PCM_TSTAMP_ENABLE
)
213 if (!(substream
->ops
->get_time_info
) ||
214 (runtime
->audio_tstamp_report
.actual_type
==
215 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
)) {
218 * provide audio timestamp derived from pointer position
219 * add delay only if requested
222 audio_frames
= runtime
->hw_ptr_wrap
+ runtime
->status
->hw_ptr
;
224 if (runtime
->audio_tstamp_config
.report_delay
) {
225 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
226 audio_frames
-= runtime
->delay
;
228 audio_frames
+= runtime
->delay
;
230 audio_nsecs
= div_u64(audio_frames
* 1000000000LL,
232 *audio_tstamp
= ns_to_timespec(audio_nsecs
);
234 if (!timespec_equal(&runtime
->status
->audio_tstamp
, audio_tstamp
)) {
235 runtime
->status
->audio_tstamp
= *audio_tstamp
;
236 runtime
->status
->tstamp
= *curr_tstamp
;
240 * re-take a driver timestamp to let apps detect if the reference tstamp
241 * read by low-level hardware was provided with a delay
243 snd_pcm_gettime(substream
->runtime
, (struct timespec
*)&driver_tstamp
);
244 runtime
->driver_tstamp
= driver_tstamp
;
247 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream
*substream
,
248 unsigned int in_interrupt
)
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;
260 old_hw_ptr
= runtime
->status
->hw_ptr
;
263 * group pointer, time and jiffies reads to allow for more
264 * accurate correlations/corrections.
265 * The values are stored at the end of this routine after
266 * corrections for hw_ptr position
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
,
275 &runtime
->audio_tstamp_config
,
276 &runtime
->audio_tstamp_report
);
278 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
279 if (runtime
->audio_tstamp_report
.actual_type
== SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
)
280 snd_pcm_gettime(runtime
, (struct timespec
*)&curr_tstamp
);
282 snd_pcm_gettime(runtime
, (struct timespec
*)&curr_tstamp
);
285 if (pos
== SNDRV_PCM_POS_XRUN
) {
286 __snd_pcm_xrun(substream
);
289 if (pos
>= runtime
->buffer_size
) {
290 if (printk_ratelimit()) {
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
);
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
;
305 /* we know that one period was processed */
306 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
307 delta
= runtime
->hw_ptr_interrupt
+ runtime
->period_size
;
308 if (delta
> new_hw_ptr
) {
309 /* check for double acknowledged interrupts */
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
) {
317 new_hw_ptr
= hw_base
+ pos
;
322 /* new_hw_ptr might be lower than old_hw_ptr in case when */
323 /* pointer crosses the end of the ring buffer */
324 if (new_hw_ptr
< old_hw_ptr
) {
325 hw_base
+= runtime
->buffer_size
;
326 if (hw_base
>= runtime
->boundary
) {
330 new_hw_ptr
= hw_base
+ pos
;
333 delta
= new_hw_ptr
- old_hw_ptr
;
335 delta
+= runtime
->boundary
;
337 if (runtime
->no_period_wakeup
) {
338 snd_pcm_sframes_t xrun_threshold
;
340 * Without regular period interrupts, we have to check
341 * the elapsed time to detect xruns.
343 jdelta
= curr_jiffies
- runtime
->hw_ptr_jiffies
;
344 if (jdelta
< runtime
->hw_ptr_buffer_jiffies
/ 2)
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
) {
355 new_hw_ptr
= hw_base
+ pos
;
356 hdelta
-= runtime
->hw_ptr_buffer_jiffies
;
361 /* something must be really wrong */
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
);
370 /* Do jiffies check only in xrun_debug mode */
371 if (!xrun_debug(substream
, XRUN_DEBUG_JIFFIESCHECK
))
372 goto no_jiffies_check
;
374 /* Skip the jiffies check for hardwares with BATCH flag.
375 * Such hardware usually just increases the position at each IRQ,
376 * thus it can't give any strange position.
378 if (runtime
->hw
.info
& SNDRV_PCM_INFO_BATCH
)
379 goto no_jiffies_check
;
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) {
387 (((runtime
->period_size
* HZ
) / runtime
->rate
)
389 /* move new_hw_ptr according jiffies not pos variable */
390 new_hw_ptr
= old_hw_ptr
;
392 /* use loop to avoid checks for delta overflows */
393 /* the delta value is small or zero in most cases */
395 new_hw_ptr
+= runtime
->period_size
;
396 if (new_hw_ptr
>= runtime
->boundary
) {
397 new_hw_ptr
-= runtime
->boundary
;
402 /* align hw_base to buffer_size */
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 /* reset values to proper state */
412 hw_base
= new_hw_ptr
- (new_hw_ptr
% runtime
->buffer_size
);
415 if (delta
> runtime
->period_size
+ runtime
->period_size
/ 2) {
416 hw_ptr_error(substream
, in_interrupt
,
418 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
419 substream
->stream
, (long)delta
,
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
);
431 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
432 runtime
->silence_size
> 0)
433 snd_pcm_playback_silence(substream
, new_hw_ptr
);
436 delta
= new_hw_ptr
- runtime
->hw_ptr_interrupt
;
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
;
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
;
452 update_audio_tstamp(substream
, &curr_tstamp
, &audio_tstamp
);
454 return snd_pcm_update_state(substream
, runtime
);
457 /* CAUTION: call it with irq disabled */
458 int snd_pcm_update_hw_ptr(struct snd_pcm_substream
*substream
)
460 return snd_pcm_update_hw_ptr0(substream
, 0);
464 * snd_pcm_set_ops - set the PCM operators
465 * @pcm: the pcm instance
466 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
467 * @ops: the operator table
469 * Sets the given PCM operators to the pcm instance.
471 void snd_pcm_set_ops(struct snd_pcm
*pcm
, int direction
,
472 const struct snd_pcm_ops
*ops
)
474 struct snd_pcm_str
*stream
= &pcm
->streams
[direction
];
475 struct snd_pcm_substream
*substream
;
477 for (substream
= stream
->substream
; substream
!= NULL
; substream
= substream
->next
)
478 substream
->ops
= ops
;
480 EXPORT_SYMBOL(snd_pcm_set_ops
);
483 * snd_pcm_sync - set the PCM sync id
484 * @substream: the pcm substream
486 * Sets the PCM sync identifier for the card.
488 void snd_pcm_set_sync(struct snd_pcm_substream
*substream
)
490 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
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;
497 EXPORT_SYMBOL(snd_pcm_set_sync
);
500 * Standard ioctl routine
503 static inline unsigned int div32(unsigned int a
, unsigned int b
,
514 static inline unsigned int div_down(unsigned int a
, unsigned int b
)
521 static inline unsigned int div_up(unsigned int a
, unsigned int b
)
533 static inline unsigned int mul(unsigned int a
, unsigned int b
)
537 if (div_down(UINT_MAX
, a
) < b
)
542 static inline unsigned int muldiv32(unsigned int a
, unsigned int b
,
543 unsigned int c
, unsigned int *r
)
545 u_int64_t n
= (u_int64_t
) a
* b
;
550 n
= div_u64_rem(n
, c
, r
);
559 * snd_interval_refine - refine the interval value of configurator
560 * @i: the interval value to refine
561 * @v: the interval value to refer to
563 * Refines the interval value with the reference value.
564 * The interval is changed to the range satisfying both intervals.
565 * The interval status (min, max, integer, etc.) are evaluated.
567 * Return: Positive if the value is changed, zero if it's not changed, or a
568 * negative error code.
570 int snd_interval_refine(struct snd_interval
*i
, const struct snd_interval
*v
)
573 if (snd_BUG_ON(snd_interval_empty(i
)))
575 if (i
->min
< v
->min
) {
577 i
->openmin
= v
->openmin
;
579 } else if (i
->min
== v
->min
&& !i
->openmin
&& v
->openmin
) {
583 if (i
->max
> v
->max
) {
585 i
->openmax
= v
->openmax
;
587 } else if (i
->max
== v
->max
&& !i
->openmax
&& v
->openmax
) {
591 if (!i
->integer
&& v
->integer
) {
604 } else if (!i
->openmin
&& !i
->openmax
&& i
->min
== i
->max
)
606 if (snd_interval_checkempty(i
)) {
607 snd_interval_none(i
);
612 EXPORT_SYMBOL(snd_interval_refine
);
614 static int snd_interval_refine_first(struct snd_interval
*i
)
616 const unsigned int last_max
= i
->max
;
618 if (snd_BUG_ON(snd_interval_empty(i
)))
620 if (snd_interval_single(i
))
625 /* only exclude max value if also excluded before refine */
626 i
->openmax
= (i
->openmax
&& i
->max
>= last_max
);
630 static int snd_interval_refine_last(struct snd_interval
*i
)
632 const unsigned int last_min
= i
->min
;
634 if (snd_BUG_ON(snd_interval_empty(i
)))
636 if (snd_interval_single(i
))
641 /* only exclude min value if also excluded before refine */
642 i
->openmin
= (i
->openmin
&& i
->min
<= last_min
);
646 void snd_interval_mul(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
648 if (a
->empty
|| b
->empty
) {
649 snd_interval_none(c
);
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
);
661 * snd_interval_div - refine the interval value with division
668 * Returns non-zero if the value is changed, zero if not changed.
670 void snd_interval_div(const struct snd_interval
*a
, const struct snd_interval
*b
, struct snd_interval
*c
)
673 if (a
->empty
|| b
->empty
) {
674 snd_interval_none(c
);
678 c
->min
= div32(a
->min
, b
->max
, &r
);
679 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
681 c
->max
= div32(a
->max
, b
->min
, &r
);
686 c
->openmax
= (a
->openmax
|| b
->openmin
);
695 * snd_interval_muldivk - refine the interval value
698 * @k: divisor (as integer)
703 * Returns non-zero if the value is changed, zero if not changed.
705 void snd_interval_muldivk(const struct snd_interval
*a
, const struct snd_interval
*b
,
706 unsigned int k
, struct snd_interval
*c
)
709 if (a
->empty
|| b
->empty
) {
710 snd_interval_none(c
);
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
);
721 c
->openmax
= (a
->openmax
|| b
->openmax
);
726 * snd_interval_mulkdiv - refine the interval value
728 * @k: dividend 2 (as integer)
734 * Returns non-zero if the value is changed, zero if not changed.
736 void snd_interval_mulkdiv(const struct snd_interval
*a
, unsigned int k
,
737 const struct snd_interval
*b
, struct snd_interval
*c
)
740 if (a
->empty
|| b
->empty
) {
741 snd_interval_none(c
);
745 c
->min
= muldiv32(a
->min
, k
, b
->max
, &r
);
746 c
->openmin
= (r
|| a
->openmin
|| b
->openmax
);
748 c
->max
= muldiv32(a
->max
, k
, b
->min
, &r
);
753 c
->openmax
= (a
->openmax
|| b
->openmin
);
765 * snd_interval_ratnum - refine the interval value
766 * @i: interval to refine
767 * @rats_count: number of ratnum_t
768 * @rats: ratnum_t array
769 * @nump: pointer to store the resultant numerator
770 * @denp: pointer to store the resultant denominator
772 * Return: Positive if the value is changed, zero if it's not changed, or a
773 * negative error code.
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
)
779 unsigned int best_num
, best_den
;
782 struct snd_interval t
;
784 unsigned int result_num
, result_den
;
787 best_num
= best_den
= best_diff
= 0;
788 for (k
= 0; k
< rats_count
; ++k
) {
789 unsigned int num
= rats
[k
].num
;
791 unsigned int q
= i
->min
;
795 den
= div_up(num
, q
);
796 if (den
< rats
[k
].den_min
)
798 if (den
> rats
[k
].den_max
)
799 den
= rats
[k
].den_max
;
802 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
806 diff
= num
- q
* den
;
810 diff
* best_den
< best_diff
* den
) {
820 t
.min
= div_down(best_num
, best_den
);
821 t
.openmin
= !!(best_num
% best_den
);
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
;
830 unsigned int q
= i
->max
;
836 den
= div_down(num
, q
);
837 if (den
> rats
[k
].den_max
)
839 if (den
< rats
[k
].den_min
)
840 den
= rats
[k
].den_min
;
843 r
= (den
- rats
[k
].den_min
) % rats
[k
].den_step
;
845 den
+= rats
[k
].den_step
- r
;
847 diff
= q
* den
- num
;
851 diff
* best_den
< best_diff
* den
) {
861 t
.max
= div_up(best_num
, best_den
);
862 t
.openmax
= !!(best_num
% best_den
);
864 err
= snd_interval_refine(i
, &t
);
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
;
880 EXPORT_SYMBOL(snd_interval_ratnum
);
883 * snd_interval_ratden - refine the interval value
884 * @i: interval to refine
885 * @rats_count: number of struct ratden
886 * @rats: struct ratden array
887 * @nump: pointer to store the resultant numerator
888 * @denp: pointer to store the resultant denominator
890 * Return: Positive if the value is changed, zero if it's not changed, or a
891 * negative error code.
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
)
898 unsigned int best_num
, best_diff
, best_den
;
900 struct snd_interval t
;
903 best_num
= best_den
= best_diff
= 0;
904 for (k
= 0; k
< rats_count
; ++k
) {
906 unsigned int den
= rats
[k
].den
;
907 unsigned int q
= i
->min
;
910 if (num
> rats
[k
].num_max
)
912 if (num
< rats
[k
].num_min
)
913 num
= rats
[k
].num_max
;
916 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
918 num
+= rats
[k
].num_step
- r
;
920 diff
= num
- q
* den
;
922 diff
* best_den
< best_diff
* den
) {
932 t
.min
= div_down(best_num
, best_den
);
933 t
.openmin
= !!(best_num
% best_den
);
935 best_num
= best_den
= best_diff
= 0;
936 for (k
= 0; k
< rats_count
; ++k
) {
938 unsigned int den
= rats
[k
].den
;
939 unsigned int q
= i
->max
;
942 if (num
< rats
[k
].num_min
)
944 if (num
> rats
[k
].num_max
)
945 num
= rats
[k
].num_max
;
948 r
= (num
- rats
[k
].num_min
) % rats
[k
].num_step
;
952 diff
= q
* den
- num
;
954 diff
* best_den
< best_diff
* den
) {
964 t
.max
= div_up(best_num
, best_den
);
965 t
.openmax
= !!(best_num
% best_den
);
967 err
= snd_interval_refine(i
, &t
);
971 if (snd_interval_single(i
)) {
981 * snd_interval_list - refine the interval value from the list
982 * @i: the interval value to refine
983 * @count: the number of elements in the list
984 * @list: the value list
985 * @mask: the bit-mask to evaluate
987 * Refines the interval value from the list.
988 * When mask is non-zero, only the elements corresponding to bit 1 are
991 * Return: Positive if the value is changed, zero if it's not changed, or a
992 * negative error code.
994 int snd_interval_list(struct snd_interval
*i
, unsigned int count
,
995 const unsigned int *list
, unsigned int mask
)
998 struct snd_interval list_range
;
1004 snd_interval_any(&list_range
);
1005 list_range
.min
= UINT_MAX
;
1007 for (k
= 0; k
< count
; k
++) {
1008 if (mask
&& !(mask
& (1 << k
)))
1010 if (!snd_interval_test(i
, list
[k
]))
1012 list_range
.min
= min(list_range
.min
, list
[k
]);
1013 list_range
.max
= max(list_range
.max
, list
[k
]);
1015 return snd_interval_refine(i
, &list_range
);
1017 EXPORT_SYMBOL(snd_interval_list
);
1020 * snd_interval_ranges - refine the interval value from the list of ranges
1021 * @i: the interval value to refine
1022 * @count: the number of elements in the list of ranges
1023 * @ranges: the ranges list
1024 * @mask: the bit-mask to evaluate
1026 * Refines the interval value from the list of ranges.
1027 * When mask is non-zero, only the elements corresponding to bit 1 are
1030 * Return: Positive if the value is changed, zero if it's not changed, or a
1031 * negative error code.
1033 int snd_interval_ranges(struct snd_interval
*i
, unsigned int count
,
1034 const struct snd_interval
*ranges
, unsigned int mask
)
1037 struct snd_interval range_union
;
1038 struct snd_interval range
;
1041 snd_interval_none(i
);
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
)))
1050 snd_interval_copy(&range
, &ranges
[k
]);
1051 if (snd_interval_refine(&range
, i
) < 0)
1053 if (snd_interval_empty(&range
))
1056 if (range
.min
< range_union
.min
) {
1057 range_union
.min
= range
.min
;
1058 range_union
.openmin
= 1;
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;
1066 if (range
.max
== range_union
.max
&& !range
.openmax
)
1067 range_union
.openmax
= 0;
1069 return snd_interval_refine(i
, &range_union
);
1071 EXPORT_SYMBOL(snd_interval_ranges
);
1073 static int snd_interval_step(struct snd_interval
*i
, unsigned int step
)
1078 if (n
!= 0 || i
->openmin
) {
1084 if (n
!= 0 || i
->openmax
) {
1089 if (snd_interval_checkempty(i
)) {
1096 /* Info constraints helpers */
1099 * snd_pcm_hw_rule_add - add the hw-constraint rule
1100 * @runtime: the pcm runtime instance
1101 * @cond: condition bits
1102 * @var: the variable to evaluate
1103 * @func: the evaluation function
1104 * @private: the private data pointer passed to function
1105 * @dep: the dependent variables
1107 * Return: Zero if successful, or a negative error code on failure.
1109 int snd_pcm_hw_rule_add(struct snd_pcm_runtime
*runtime
, unsigned int cond
,
1111 snd_pcm_hw_rule_func_t func
, void *private,
1114 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1115 struct snd_pcm_hw_rule
*c
;
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
),
1128 constrs
->rules
= new;
1129 constrs
->rules_all
= new_rules
;
1131 c
= &constrs
->rules
[constrs
->rules_num
];
1135 c
->private = private;
1138 if (snd_BUG_ON(k
>= ARRAY_SIZE(c
->deps
))) {
1145 dep
= va_arg(args
, int);
1147 constrs
->rules_num
++;
1151 EXPORT_SYMBOL(snd_pcm_hw_rule_add
);
1154 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1155 * @runtime: PCM runtime instance
1156 * @var: hw_params variable to apply the mask
1157 * @mask: the bitmap mask
1159 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1161 * Return: Zero if successful, or a negative error code on failure.
1163 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
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); /* clear rest */
1170 if (*maskp
->bits
== 0)
1176 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1177 * @runtime: PCM runtime instance
1178 * @var: hw_params variable to apply the mask
1179 * @mask: the 64bit bitmap mask
1181 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1183 * Return: Zero if successful, or a negative error code on failure.
1185 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
,
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); /* clear rest */
1193 if (! maskp
->bits
[0] && ! maskp
->bits
[1])
1197 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64
);
1200 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1201 * @runtime: PCM runtime instance
1202 * @var: hw_params variable to apply the integer constraint
1204 * Apply the constraint of integer to an interval parameter.
1206 * Return: Positive if the value is changed, zero if it's not changed, or a
1207 * negative error code.
1209 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime
*runtime
, snd_pcm_hw_param_t var
)
1211 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1212 return snd_interval_setinteger(constrs_interval(constrs
, var
));
1214 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer
);
1217 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1218 * @runtime: PCM runtime instance
1219 * @var: hw_params variable to apply the range
1220 * @min: the minimal value
1221 * @max: the maximal value
1223 * Apply the min/max range constraint to an interval parameter.
1225 * Return: Positive if the value is changed, zero if it's not changed, or a
1226 * negative error code.
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
)
1231 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1232 struct snd_interval t
;
1235 t
.openmin
= t
.openmax
= 0;
1237 return snd_interval_refine(constrs_interval(constrs
, var
), &t
);
1239 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax
);
1241 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params
*params
,
1242 struct snd_pcm_hw_rule
*rule
)
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
);
1250 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1251 * @runtime: PCM runtime instance
1252 * @cond: condition bits
1253 * @var: hw_params variable to apply the list constraint
1256 * Apply the list of constraints to an interval parameter.
1258 * Return: Zero if successful, or a negative error code on failure.
1260 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime
*runtime
,
1262 snd_pcm_hw_param_t var
,
1263 const struct snd_pcm_hw_constraint_list
*l
)
1265 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1266 snd_pcm_hw_rule_list
, (void *)l
,
1269 EXPORT_SYMBOL(snd_pcm_hw_constraint_list
);
1271 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params
*params
,
1272 struct snd_pcm_hw_rule
*rule
)
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
);
1281 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1282 * @runtime: PCM runtime instance
1283 * @cond: condition bits
1284 * @var: hw_params variable to apply the list of range constraints
1287 * Apply the list of range constraints to an interval parameter.
1289 * Return: Zero if successful, or a negative error code on failure.
1291 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime
*runtime
,
1293 snd_pcm_hw_param_t var
,
1294 const struct snd_pcm_hw_constraint_ranges
*r
)
1296 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1297 snd_pcm_hw_rule_ranges
, (void *)r
,
1300 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges
);
1302 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params
*params
,
1303 struct snd_pcm_hw_rule
*rule
)
1305 const struct snd_pcm_hw_constraint_ratnums
*r
= rule
->private;
1306 unsigned int num
= 0, den
= 0;
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
;
1318 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1319 * @runtime: PCM runtime instance
1320 * @cond: condition bits
1321 * @var: hw_params variable to apply the ratnums constraint
1322 * @r: struct snd_ratnums constriants
1324 * Return: Zero if successful, or a negative error code on failure.
1326 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime
*runtime
,
1328 snd_pcm_hw_param_t var
,
1329 const struct snd_pcm_hw_constraint_ratnums
*r
)
1331 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1332 snd_pcm_hw_rule_ratnums
, (void *)r
,
1335 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums
);
1337 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params
*params
,
1338 struct snd_pcm_hw_rule
*rule
)
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
;
1352 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1353 * @runtime: PCM runtime instance
1354 * @cond: condition bits
1355 * @var: hw_params variable to apply the ratdens constraint
1356 * @r: struct snd_ratdens constriants
1358 * Return: Zero if successful, or a negative error code on failure.
1360 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime
*runtime
,
1362 snd_pcm_hw_param_t var
,
1363 const struct snd_pcm_hw_constraint_ratdens
*r
)
1365 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1366 snd_pcm_hw_rule_ratdens
, (void *)r
,
1369 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens
);
1371 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params
*params
,
1372 struct snd_pcm_hw_rule
*rule
)
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
);
1380 if (!snd_interval_single(i
))
1383 if ((snd_interval_value(i
) == width
) ||
1384 (width
== 0 && snd_interval_value(i
) > msbits
))
1385 params
->msbits
= min_not_zero(params
->msbits
, msbits
);
1391 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1392 * @runtime: PCM runtime instance
1393 * @cond: condition bits
1394 * @width: sample bits width
1395 * @msbits: msbits width
1397 * This constraint will set the number of most significant bits (msbits) if a
1398 * sample format with the specified width has been select. If width is set to 0
1399 * the msbits will be set for any sample format with a width larger than the
1402 * Return: Zero if successful, or a negative error code on failure.
1404 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime
*runtime
,
1407 unsigned int msbits
)
1409 unsigned long l
= (msbits
<< 16) | width
;
1410 return snd_pcm_hw_rule_add(runtime
, cond
, -1,
1411 snd_pcm_hw_rule_msbits
,
1413 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1415 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits
);
1417 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params
*params
,
1418 struct snd_pcm_hw_rule
*rule
)
1420 unsigned long step
= (unsigned long) rule
->private;
1421 return snd_interval_step(hw_param_interval(params
, rule
->var
), step
);
1425 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1426 * @runtime: PCM runtime instance
1427 * @cond: condition bits
1428 * @var: hw_params variable to apply the step constraint
1431 * Return: Zero if successful, or a negative error code on failure.
1433 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime
*runtime
,
1435 snd_pcm_hw_param_t var
,
1438 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1439 snd_pcm_hw_rule_step
, (void *) step
,
1442 EXPORT_SYMBOL(snd_pcm_hw_constraint_step
);
1444 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params
*params
, struct snd_pcm_hw_rule
*rule
)
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
1452 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1453 ARRAY_SIZE(pow2_sizes
), pow2_sizes
, 0);
1457 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1458 * @runtime: PCM runtime instance
1459 * @cond: condition bits
1460 * @var: hw_params variable to apply the power-of-2 constraint
1462 * Return: Zero if successful, or a negative error code on failure.
1464 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime
*runtime
,
1466 snd_pcm_hw_param_t var
)
1468 return snd_pcm_hw_rule_add(runtime
, cond
, var
,
1469 snd_pcm_hw_rule_pow2
, NULL
,
1472 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2
);
1474 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params
*params
,
1475 struct snd_pcm_hw_rule
*rule
)
1477 unsigned int base_rate
= (unsigned int)(uintptr_t)rule
->private;
1478 struct snd_interval
*rate
;
1480 rate
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
1481 return snd_interval_list(rate
, 1, &base_rate
, 0);
1485 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1486 * @runtime: PCM runtime instance
1487 * @base_rate: the rate at which the hardware does not resample
1489 * Return: Zero if successful, or a negative error code on failure.
1491 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime
*runtime
,
1492 unsigned int base_rate
)
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);
1500 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample
);
1502 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params
*params
,
1503 snd_pcm_hw_param_t var
)
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
;
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
;
1520 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params
*params
)
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
);
1530 EXPORT_SYMBOL(_snd_pcm_hw_params_any
);
1533 * snd_pcm_hw_param_value - return @params field @var value
1534 * @params: the hw_params instance
1535 * @var: parameter to retrieve
1536 * @dir: pointer to the direction (-1,0,1) or %NULL
1538 * Return: The value for field @var if it's fixed in configuration space
1539 * defined by @params. -%EINVAL otherwise.
1541 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params
*params
,
1542 snd_pcm_hw_param_t var
, int *dir
)
1544 if (hw_is_mask(var
)) {
1545 const struct snd_mask
*mask
= hw_param_mask_c(params
, var
);
1546 if (!snd_mask_single(mask
))
1550 return snd_mask_value(mask
);
1552 if (hw_is_interval(var
)) {
1553 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
1554 if (!snd_interval_single(i
))
1558 return snd_interval_value(i
);
1562 EXPORT_SYMBOL(snd_pcm_hw_param_value
);
1564 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params
*params
,
1565 snd_pcm_hw_param_t var
)
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
;
1579 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty
);
1581 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params
*params
,
1582 snd_pcm_hw_param_t var
)
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
));
1592 params
->cmask
|= 1 << var
;
1593 params
->rmask
|= 1 << var
;
1600 * snd_pcm_hw_param_first - refine config space and return minimum value
1601 * @pcm: PCM instance
1602 * @params: the hw_params instance
1603 * @var: parameter to retrieve
1604 * @dir: pointer to the direction (-1,0,1) or %NULL
1606 * Inside configuration space defined by @params remove from @var all
1607 * values > minimum. Reduce configuration space accordingly.
1609 * Return: The minimum, or a negative error code on failure.
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
)
1615 int changed
= _snd_pcm_hw_param_first(params
, var
);
1618 if (params
->rmask
) {
1619 int err
= snd_pcm_hw_refine(pcm
, params
);
1623 return snd_pcm_hw_param_value(params
, var
, dir
);
1625 EXPORT_SYMBOL(snd_pcm_hw_param_first
);
1627 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params
*params
,
1628 snd_pcm_hw_param_t var
)
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
));
1638 params
->cmask
|= 1 << var
;
1639 params
->rmask
|= 1 << var
;
1646 * snd_pcm_hw_param_last - refine config space and return maximum value
1647 * @pcm: PCM instance
1648 * @params: the hw_params instance
1649 * @var: parameter to retrieve
1650 * @dir: pointer to the direction (-1,0,1) or %NULL
1652 * Inside configuration space defined by @params remove from @var all
1653 * values < maximum. Reduce configuration space accordingly.
1655 * Return: The maximum, or a negative error code on failure.
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
)
1661 int changed
= _snd_pcm_hw_param_last(params
, var
);
1664 if (params
->rmask
) {
1665 int err
= snd_pcm_hw_refine(pcm
, params
);
1669 return snd_pcm_hw_param_value(params
, var
, dir
);
1671 EXPORT_SYMBOL(snd_pcm_hw_param_last
);
1673 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream
*substream
,
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
;
1683 runtime
->status
->hw_ptr
= 0;
1684 runtime
->hw_ptr_wrap
= 0;
1686 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1690 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream
*substream
,
1693 struct snd_pcm_channel_info
*info
= arg
;
1694 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1696 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
)) {
1700 width
= snd_pcm_format_physical_width(runtime
->format
);
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
;
1710 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
:
1711 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
:
1713 size_t size
= runtime
->dma_bytes
/ runtime
->channels
;
1714 info
->first
= info
->channel
* size
* 8;
1725 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream
*substream
,
1728 struct snd_pcm_hw_params
*params
= arg
;
1729 snd_pcm_format_t format
;
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
);
1739 params
->fifo_size
/= (unsigned)frame_size
;
1745 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1746 * @substream: the pcm substream instance
1747 * @cmd: ioctl command
1748 * @arg: ioctl argument
1750 * Processes the generic ioctl commands for PCM.
1751 * Can be passed as the ioctl callback for PCM ops.
1753 * Return: Zero if successful, or a negative error code on failure.
1755 int snd_pcm_lib_ioctl(struct snd_pcm_substream
*substream
,
1756 unsigned int cmd
, void *arg
)
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
);
1768 EXPORT_SYMBOL(snd_pcm_lib_ioctl
);
1771 * snd_pcm_period_elapsed - update the pcm status for the next period
1772 * @substream: the pcm substream instance
1774 * This function is called from the interrupt handler when the
1775 * PCM has processed the period size. It will update the current
1776 * pointer, wake up sleepers, etc.
1778 * Even if more than one periods have elapsed since the last call, you
1779 * have to call this only once.
1781 void snd_pcm_period_elapsed(struct snd_pcm_substream
*substream
)
1783 struct snd_pcm_runtime
*runtime
;
1784 unsigned long flags
;
1786 if (snd_BUG_ON(!substream
))
1789 snd_pcm_stream_lock_irqsave(substream
, flags
);
1790 if (PCM_RUNTIME_CHECK(substream
))
1792 runtime
= substream
->runtime
;
1794 if (!snd_pcm_running(substream
) ||
1795 snd_pcm_update_hw_ptr0(substream
, 1) < 0)
1798 #ifdef CONFIG_SND_PCM_TIMER
1799 if (substream
->timer_running
)
1800 snd_timer_interrupt(substream
->timer
, 1);
1803 kill_fasync(&runtime
->fasync
, SIGIO
, POLL_IN
);
1805 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1807 EXPORT_SYMBOL(snd_pcm_period_elapsed
);
1810 * Wait until avail_min data becomes available
1811 * Returns a negative error code if any error occurs during operation.
1812 * The available space is stored on availp. When err = 0 and avail = 0
1813 * on the capture stream, it indicates the stream is in DRAINING state.
1815 static int wait_for_avail(struct snd_pcm_substream
*substream
,
1816 snd_pcm_uframes_t
*availp
)
1818 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1819 int is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
1820 wait_queue_entry_t wait
;
1822 snd_pcm_uframes_t avail
= 0;
1823 long wait_time
, tout
;
1825 init_waitqueue_entry(&wait
, current
);
1826 set_current_state(TASK_INTERRUPTIBLE
);
1827 add_wait_queue(&runtime
->tsleep
, &wait
);
1829 if (runtime
->no_period_wakeup
)
1830 wait_time
= MAX_SCHEDULE_TIMEOUT
;
1832 /* use wait time from substream if available */
1833 if (substream
->wait_time
) {
1834 wait_time
= substream
->wait_time
;
1838 if (runtime
->rate
) {
1839 long t
= runtime
->period_size
* 2 /
1841 wait_time
= max(t
, wait_time
);
1843 wait_time
= msecs_to_jiffies(wait_time
* 1000);
1848 if (signal_pending(current
)) {
1854 * We need to check if space became available already
1855 * (and thus the wakeup happened already) first to close
1856 * the race of space already having become available.
1857 * This check must happen after been added to the waitqueue
1858 * and having current state be INTERRUPTIBLE.
1860 avail
= snd_pcm_avail(substream
);
1861 if (avail
>= runtime
->twake
)
1863 snd_pcm_stream_unlock_irq(substream
);
1865 tout
= schedule_timeout(wait_time
);
1867 snd_pcm_stream_lock_irq(substream
);
1868 set_current_state(TASK_INTERRUPTIBLE
);
1869 switch (runtime
->status
->state
) {
1870 case SNDRV_PCM_STATE_SUSPENDED
:
1873 case SNDRV_PCM_STATE_XRUN
:
1876 case SNDRV_PCM_STATE_DRAINING
:
1880 avail
= 0; /* indicate draining */
1882 case SNDRV_PCM_STATE_OPEN
:
1883 case SNDRV_PCM_STATE_SETUP
:
1884 case SNDRV_PCM_STATE_DISCONNECTED
:
1887 case SNDRV_PCM_STATE_PAUSED
:
1891 pcm_dbg(substream
->pcm
,
1892 "%s write error (DMA or IRQ trouble?)\n",
1893 is_playback
? "playback" : "capture");
1899 set_current_state(TASK_RUNNING
);
1900 remove_wait_queue(&runtime
->tsleep
, &wait
);
1905 typedef int (*pcm_transfer_f
)(struct snd_pcm_substream
*substream
,
1906 int channel
, unsigned long hwoff
,
1907 void *buf
, unsigned long bytes
);
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
);
1912 /* calculate the target DMA-buffer position to be written/read */
1913 static void *get_dma_ptr(struct snd_pcm_runtime
*runtime
,
1914 int channel
, unsigned long hwoff
)
1916 return runtime
->dma_area
+ hwoff
+
1917 channel
* (runtime
->dma_bytes
/ runtime
->channels
);
1920 /* default copy_user ops for write; used for both interleaved and non- modes */
1921 static int default_write_copy(struct snd_pcm_substream
*substream
,
1922 int channel
, unsigned long hwoff
,
1923 void *buf
, unsigned long bytes
)
1925 if (copy_from_user(get_dma_ptr(substream
->runtime
, channel
, hwoff
),
1926 (void __user
*)buf
, bytes
))
1931 /* default copy_kernel ops for write */
1932 static int default_write_copy_kernel(struct snd_pcm_substream
*substream
,
1933 int channel
, unsigned long hwoff
,
1934 void *buf
, unsigned long bytes
)
1936 memcpy(get_dma_ptr(substream
->runtime
, channel
, hwoff
), buf
, bytes
);
1940 /* fill silence instead of copy data; called as a transfer helper
1941 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
1942 * a NULL buffer is passed
1944 static int fill_silence(struct snd_pcm_substream
*substream
, int channel
,
1945 unsigned long hwoff
, void *buf
, unsigned long bytes
)
1947 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1949 if (substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1951 if (substream
->ops
->fill_silence
)
1952 return substream
->ops
->fill_silence(substream
, channel
,
1955 snd_pcm_format_set_silence(runtime
->format
,
1956 get_dma_ptr(runtime
, channel
, hwoff
),
1957 bytes_to_samples(runtime
, bytes
));
1961 /* default copy_user ops for read; used for both interleaved and non- modes */
1962 static int default_read_copy(struct snd_pcm_substream
*substream
,
1963 int channel
, unsigned long hwoff
,
1964 void *buf
, unsigned long bytes
)
1966 if (copy_to_user((void __user
*)buf
,
1967 get_dma_ptr(substream
->runtime
, channel
, hwoff
),
1973 /* default copy_kernel ops for read */
1974 static int default_read_copy_kernel(struct snd_pcm_substream
*substream
,
1975 int channel
, unsigned long hwoff
,
1976 void *buf
, unsigned long bytes
)
1978 memcpy(buf
, get_dma_ptr(substream
->runtime
, channel
, hwoff
), bytes
);
1982 /* call transfer function with the converted pointers and sizes;
1983 * for interleaved mode, it's one shot for all samples
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
)
1991 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1993 /* convert to bytes */
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
);
2000 /* call transfer function with the converted pointers and sizes for each
2001 * non-interleaved channel; when buffer is NULL, silencing instead of copying
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
)
2009 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2010 int channels
= runtime
->channels
;
2014 /* convert to bytes; note that it's not frames_to_bytes() here.
2015 * in non-interleaved mode, we copy for each channel, thus
2016 * each copy is n_samples bytes x channels = whole frames.
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
);
2025 err
= transfer(substream
, c
, hwoff
, *bufs
+ off
,
2033 /* fill silence on the given buffer position;
2034 * called from snd_pcm_playback_silence()
2036 static int fill_silence_frames(struct snd_pcm_substream
*substream
,
2037 snd_pcm_uframes_t off
, snd_pcm_uframes_t frames
)
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
,
2044 return noninterleaved_copy(substream
, off
, NULL
, 0, frames
,
2048 /* sanity-check for read/write methods */
2049 static int pcm_sanity_check(struct snd_pcm_substream
*substream
)
2051 struct snd_pcm_runtime
*runtime
;
2052 if (PCM_RUNTIME_CHECK(substream
))
2054 runtime
= substream
->runtime
;
2055 if (snd_BUG_ON(!substream
->ops
->copy_user
&& !runtime
->dma_area
))
2057 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2062 static int pcm_accessible_state(struct snd_pcm_runtime
*runtime
)
2064 switch (runtime
->status
->state
) {
2065 case SNDRV_PCM_STATE_PREPARED
:
2066 case SNDRV_PCM_STATE_RUNNING
:
2067 case SNDRV_PCM_STATE_PAUSED
:
2069 case SNDRV_PCM_STATE_XRUN
:
2071 case SNDRV_PCM_STATE_SUSPENDED
:
2078 /* update to the given appl_ptr and call ack callback if needed;
2079 * when an error is returned, take back to the original value
2081 int pcm_lib_apply_appl_ptr(struct snd_pcm_substream
*substream
,
2082 snd_pcm_uframes_t appl_ptr
)
2084 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2085 snd_pcm_uframes_t old_appl_ptr
= runtime
->control
->appl_ptr
;
2088 if (old_appl_ptr
== appl_ptr
)
2091 runtime
->control
->appl_ptr
= appl_ptr
;
2092 if (substream
->ops
->ack
) {
2093 ret
= substream
->ops
->ack(substream
);
2095 runtime
->control
->appl_ptr
= old_appl_ptr
;
2100 trace_applptr(substream
, old_appl_ptr
, appl_ptr
);
2105 /* the common loop for read/write data */
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
)
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
;
2115 pcm_transfer_f transfer
;
2120 err
= pcm_sanity_check(substream
);
2124 is_playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
2126 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_INTERLEAVED
&&
2127 runtime
->channels
> 1)
2129 writer
= interleaved_copy
;
2131 if (runtime
->access
!= SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2133 writer
= noninterleaved_copy
;
2138 transfer
= fill_silence
;
2141 } else if (in_kernel
) {
2142 if (substream
->ops
->copy_kernel
)
2143 transfer
= substream
->ops
->copy_kernel
;
2145 transfer
= is_playback
?
2146 default_write_copy_kernel
: default_read_copy_kernel
;
2148 if (substream
->ops
->copy_user
)
2149 transfer
= (pcm_transfer_f
)substream
->ops
->copy_user
;
2151 transfer
= is_playback
?
2152 default_write_copy
: default_read_copy
;
2158 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
2160 snd_pcm_stream_lock_irq(substream
);
2161 err
= pcm_accessible_state(runtime
);
2165 runtime
->twake
= runtime
->control
->avail_min
? : 1;
2166 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
2167 snd_pcm_update_hw_ptr(substream
);
2170 * If size < start_threshold, wait indefinitely. Another
2171 * thread may start capture
2174 runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
&&
2175 size
>= runtime
->start_threshold
) {
2176 err
= snd_pcm_start(substream
);
2181 avail
= snd_pcm_avail(substream
);
2184 snd_pcm_uframes_t frames
, appl_ptr
, appl_ofs
;
2185 snd_pcm_uframes_t cont
;
2188 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
2189 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
2196 runtime
->twake
= min_t(snd_pcm_uframes_t
, size
,
2197 runtime
->control
->avail_min
? : 1);
2198 err
= wait_for_avail(substream
, &avail
);
2202 continue; /* draining */
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
;
2210 if (snd_BUG_ON(!frames
)) {
2214 snd_pcm_stream_unlock_irq(substream
);
2215 err
= writer(substream
, appl_ofs
, data
, offset
, frames
,
2217 snd_pcm_stream_lock_irq(substream
);
2220 err
= pcm_accessible_state(runtime
);
2224 if (appl_ptr
>= runtime
->boundary
)
2225 appl_ptr
-= runtime
->boundary
;
2226 err
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
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
);
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
;
2249 EXPORT_SYMBOL(__snd_pcm_lib_xfer
);
2252 * standard channel mapping helpers
2255 /* default channel maps for multi-channel playbacks, up to 8 channels */
2256 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps
[] = {
2258 .map
= { SNDRV_CHMAP_MONO
} },
2260 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
2262 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
2263 SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
} },
2265 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
2266 SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
,
2267 SNDRV_CHMAP_FC
, SNDRV_CHMAP_LFE
} },
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
} },
2275 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps
);
2277 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2278 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps
[] = {
2280 .map
= { SNDRV_CHMAP_MONO
} },
2282 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
2284 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
2285 SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
} },
2287 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
2288 SNDRV_CHMAP_FC
, SNDRV_CHMAP_LFE
,
2289 SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
} },
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
} },
2297 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps
);
2299 static bool valid_chmap_channels(const struct snd_pcm_chmap
*info
, int ch
)
2301 if (ch
> info
->max_channels
)
2303 return !info
->channel_mask
|| (info
->channel_mask
& (1U << ch
));
2306 static int pcm_chmap_ctl_info(struct snd_kcontrol
*kcontrol
,
2307 struct snd_ctl_elem_info
*uinfo
)
2309 struct snd_pcm_chmap
*info
= snd_kcontrol_chip(kcontrol
);
2311 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2313 uinfo
->count
= info
->max_channels
;
2314 uinfo
->value
.integer
.min
= 0;
2315 uinfo
->value
.integer
.max
= SNDRV_CHMAP_LAST
;
2319 /* get callback for channel map ctl element
2320 * stores the channel position firstly matching with the current channels
2322 static int pcm_chmap_ctl_get(struct snd_kcontrol
*kcontrol
,
2323 struct snd_ctl_elem_value
*ucontrol
)
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
;
2332 substream
= snd_pcm_chmap_substream(info
, idx
);
2335 memset(ucontrol
->value
.integer
.value
, 0,
2336 sizeof(ucontrol
->value
.integer
.value
));
2337 if (!substream
->runtime
)
2338 return 0; /* no channels set */
2339 for (map
= info
->chmap
; map
->channels
; map
++) {
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
];
2351 /* tlv callback for channel map ctl element
2352 * expands the pre-defined channel maps in a form of TLV
2354 static int pcm_chmap_ctl_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2355 unsigned int size
, unsigned int __user
*tlv
)
2357 struct snd_pcm_chmap
*info
= snd_kcontrol_chip(kcontrol
);
2358 const struct snd_pcm_chmap_elem
*map
;
2359 unsigned int __user
*dst
;
2366 if (put_user(SNDRV_CTL_TLVT_CONTAINER
, tlv
))
2370 for (map
= info
->chmap
; map
->channels
; map
++) {
2371 int chs_bytes
= map
->channels
* 4;
2372 if (!valid_chmap_channels(info
, map
->channels
))
2376 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED
, dst
) ||
2377 put_user(chs_bytes
, dst
+ 1))
2382 if (size
< chs_bytes
)
2386 for (c
= 0; c
< map
->channels
; c
++) {
2387 if (put_user(map
->map
[c
], dst
))
2392 if (put_user(count
, tlv
+ 1))
2397 static void pcm_chmap_ctl_private_free(struct snd_kcontrol
*kcontrol
)
2399 struct snd_pcm_chmap
*info
= snd_kcontrol_chip(kcontrol
);
2400 info
->pcm
->streams
[info
->stream
].chmap_kctl
= NULL
;
2405 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2406 * @pcm: the assigned PCM instance
2407 * @stream: stream direction
2408 * @chmap: channel map elements (for query)
2409 * @max_channels: the max number of channels for the stream
2410 * @private_value: the value passed to each kcontrol's private_value field
2411 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2413 * Create channel-mapping control elements assigned to the given PCM stream(s).
2414 * Return: Zero if successful, or a negative error value.
2416 int snd_pcm_add_chmap_ctls(struct snd_pcm
*pcm
, int stream
,
2417 const struct snd_pcm_chmap_elem
*chmap
,
2419 unsigned long private_value
,
2420 struct snd_pcm_chmap
**info_ret
)
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
,
2434 if (WARN_ON(pcm
->streams
[stream
].chmap_kctl
))
2436 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
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";
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
);
2455 info
->kctl
->private_free
= pcm_chmap_ctl_private_free
;
2456 err
= snd_ctl_add(pcm
->card
, info
->kctl
);
2459 pcm
->streams
[stream
].chmap_kctl
= info
->kctl
;
2464 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls
);