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 static inline void update_silence_vars(struct snd_pcm_runtime *runtime,
38 snd_pcm_uframes_t ptr,
39 snd_pcm_uframes_t new_ptr)
41 snd_pcm_sframes_t delta;
43 delta = new_ptr - ptr;
47 delta += runtime->boundary;
48 if ((snd_pcm_uframes_t)delta < runtime->silence_filled)
49 runtime->silence_filled -= delta;
51 runtime->silence_filled = 0;
52 runtime->silence_start = new_ptr;
56 * fill ring buffer with silence
57 * runtime->silence_start: starting pointer to silence area
58 * runtime->silence_filled: size filled with silence
59 * runtime->silence_threshold: threshold from application
60 * runtime->silence_size: maximal size from application
62 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
64 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
66 struct snd_pcm_runtime *runtime = substream->runtime;
67 snd_pcm_uframes_t frames, ofs, transfer;
70 if (runtime->silence_size < runtime->boundary) {
71 snd_pcm_sframes_t noise_dist;
72 snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
73 update_silence_vars(runtime, runtime->silence_start, appl_ptr);
74 /* initialization outside pointer updates */
75 if (new_hw_ptr == ULONG_MAX)
76 new_hw_ptr = runtime->status->hw_ptr;
77 /* get hw_avail with the boundary crossing */
78 noise_dist = appl_ptr - new_hw_ptr;
80 noise_dist += runtime->boundary;
81 /* total noise distance */
82 noise_dist += runtime->silence_filled;
83 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
85 frames = runtime->silence_threshold - noise_dist;
86 if (frames > runtime->silence_size)
87 frames = runtime->silence_size;
90 * This filling mode aims at free-running mode (used for example by dmix),
91 * which doesn't update the application pointer.
93 if (new_hw_ptr == ULONG_MAX) { /* initialization */
94 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
95 if (avail > runtime->buffer_size)
96 avail = runtime->buffer_size;
97 runtime->silence_filled = avail > 0 ? avail : 0;
98 runtime->silence_start = runtime->status->hw_ptr;
100 update_silence_vars(runtime, runtime->status->hw_ptr, new_hw_ptr);
102 frames = runtime->buffer_size - runtime->silence_filled;
104 if (snd_BUG_ON(frames > runtime->buffer_size))
108 ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
110 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
111 err = fill_silence_frames(substream, ofs, transfer);
113 runtime->silence_filled += transfer;
117 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
120 #ifdef CONFIG_SND_DEBUG
121 void snd_pcm_debug_name(struct snd_pcm_substream *substream,
122 char *name, size_t len)
124 snprintf(name, len, "pcmC%dD%d%c:%d",
125 substream->pcm->card->number,
126 substream->pcm->device,
127 substream->stream ? 'c' : 'p',
130 EXPORT_SYMBOL(snd_pcm_debug_name);
133 #define XRUN_DEBUG_BASIC (1<<0)
134 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
135 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
137 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
139 #define xrun_debug(substream, mask) \
140 ((substream)->pstr->xrun_debug & (mask))
142 #define xrun_debug(substream, mask) 0
145 #define dump_stack_on_xrun(substream) do { \
146 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
150 /* call with stream lock held */
151 void __snd_pcm_xrun(struct snd_pcm_substream *substream)
153 struct snd_pcm_runtime *runtime = substream->runtime;
155 trace_xrun(substream);
156 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
157 struct timespec64 tstamp;
159 snd_pcm_gettime(runtime, &tstamp);
160 runtime->status->tstamp.tv_sec = tstamp.tv_sec;
161 runtime->status->tstamp.tv_nsec = tstamp.tv_nsec;
163 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
164 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
166 snd_pcm_debug_name(substream, name, sizeof(name));
167 pcm_warn(substream->pcm, "XRUN: %s\n", name);
168 dump_stack_on_xrun(substream);
172 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
173 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
175 trace_hw_ptr_error(substream, reason); \
176 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
177 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
178 (in_interrupt) ? 'Q' : 'P', ##args); \
179 dump_stack_on_xrun(substream); \
183 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
185 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
189 int snd_pcm_update_state(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
192 snd_pcm_uframes_t avail;
194 avail = snd_pcm_avail(substream);
195 if (avail > runtime->avail_max)
196 runtime->avail_max = avail;
197 if (runtime->state == SNDRV_PCM_STATE_DRAINING) {
198 if (avail >= runtime->buffer_size) {
199 snd_pcm_drain_done(substream);
203 if (avail >= runtime->stop_threshold) {
204 __snd_pcm_xrun(substream);
208 if (runtime->twake) {
209 if (avail >= runtime->twake)
210 wake_up(&runtime->tsleep);
211 } else if (avail >= runtime->control->avail_min)
212 wake_up(&runtime->sleep);
216 static void update_audio_tstamp(struct snd_pcm_substream *substream,
217 struct timespec64 *curr_tstamp,
218 struct timespec64 *audio_tstamp)
220 struct snd_pcm_runtime *runtime = substream->runtime;
221 u64 audio_frames, audio_nsecs;
222 struct timespec64 driver_tstamp;
224 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
227 if (!(substream->ops->get_time_info) ||
228 (runtime->audio_tstamp_report.actual_type ==
229 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
232 * provide audio timestamp derived from pointer position
233 * add delay only if requested
236 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
238 if (runtime->audio_tstamp_config.report_delay) {
239 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
240 audio_frames -= runtime->delay;
242 audio_frames += runtime->delay;
244 audio_nsecs = div_u64(audio_frames * 1000000000LL,
246 *audio_tstamp = ns_to_timespec64(audio_nsecs);
249 if (runtime->status->audio_tstamp.tv_sec != audio_tstamp->tv_sec ||
250 runtime->status->audio_tstamp.tv_nsec != audio_tstamp->tv_nsec) {
251 runtime->status->audio_tstamp.tv_sec = audio_tstamp->tv_sec;
252 runtime->status->audio_tstamp.tv_nsec = audio_tstamp->tv_nsec;
253 runtime->status->tstamp.tv_sec = curr_tstamp->tv_sec;
254 runtime->status->tstamp.tv_nsec = curr_tstamp->tv_nsec;
259 * re-take a driver timestamp to let apps detect if the reference tstamp
260 * read by low-level hardware was provided with a delay
262 snd_pcm_gettime(substream->runtime, &driver_tstamp);
263 runtime->driver_tstamp = driver_tstamp;
266 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
267 unsigned int in_interrupt)
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 snd_pcm_uframes_t pos;
271 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
272 snd_pcm_sframes_t hdelta, delta;
273 unsigned long jdelta;
274 unsigned long curr_jiffies;
275 struct timespec64 curr_tstamp;
276 struct timespec64 audio_tstamp;
277 int crossed_boundary = 0;
279 old_hw_ptr = runtime->status->hw_ptr;
282 * group pointer, time and jiffies reads to allow for more
283 * accurate correlations/corrections.
284 * The values are stored at the end of this routine after
285 * corrections for hw_ptr position
287 pos = substream->ops->pointer(substream);
288 curr_jiffies = jiffies;
289 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
290 if ((substream->ops->get_time_info) &&
291 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
292 substream->ops->get_time_info(substream, &curr_tstamp,
294 &runtime->audio_tstamp_config,
295 &runtime->audio_tstamp_report);
297 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
298 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
299 snd_pcm_gettime(runtime, &curr_tstamp);
301 snd_pcm_gettime(runtime, &curr_tstamp);
304 if (pos == SNDRV_PCM_POS_XRUN) {
305 __snd_pcm_xrun(substream);
308 if (pos >= runtime->buffer_size) {
309 if (printk_ratelimit()) {
311 snd_pcm_debug_name(substream, name, sizeof(name));
312 pcm_err(substream->pcm,
313 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
314 name, pos, runtime->buffer_size,
315 runtime->period_size);
319 pos -= pos % runtime->min_align;
320 trace_hwptr(substream, pos, in_interrupt);
321 hw_base = runtime->hw_ptr_base;
322 new_hw_ptr = hw_base + pos;
324 /* we know that one period was processed */
325 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
326 delta = runtime->hw_ptr_interrupt + runtime->period_size;
327 if (delta > new_hw_ptr) {
328 /* check for double acknowledged interrupts */
329 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
330 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
331 hw_base += runtime->buffer_size;
332 if (hw_base >= runtime->boundary) {
336 new_hw_ptr = hw_base + pos;
341 /* new_hw_ptr might be lower than old_hw_ptr in case when */
342 /* pointer crosses the end of the ring buffer */
343 if (new_hw_ptr < old_hw_ptr) {
344 hw_base += runtime->buffer_size;
345 if (hw_base >= runtime->boundary) {
349 new_hw_ptr = hw_base + pos;
352 delta = new_hw_ptr - old_hw_ptr;
354 delta += runtime->boundary;
356 if (runtime->no_period_wakeup) {
357 snd_pcm_sframes_t xrun_threshold;
359 * Without regular period interrupts, we have to check
360 * the elapsed time to detect xruns.
362 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
363 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
365 hdelta = jdelta - delta * HZ / runtime->rate;
366 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
367 while (hdelta > xrun_threshold) {
368 delta += runtime->buffer_size;
369 hw_base += runtime->buffer_size;
370 if (hw_base >= runtime->boundary) {
374 new_hw_ptr = hw_base + pos;
375 hdelta -= runtime->hw_ptr_buffer_jiffies;
380 /* something must be really wrong */
381 if (delta >= runtime->buffer_size + runtime->period_size) {
382 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
383 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
384 substream->stream, (long)pos,
385 (long)new_hw_ptr, (long)old_hw_ptr);
389 /* Do jiffies check only in xrun_debug mode */
390 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
391 goto no_jiffies_check;
393 /* Skip the jiffies check for hardwares with BATCH flag.
394 * Such hardware usually just increases the position at each IRQ,
395 * thus it can't give any strange position.
397 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
398 goto no_jiffies_check;
400 if (hdelta < runtime->delay)
401 goto no_jiffies_check;
402 hdelta -= runtime->delay;
403 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
404 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
406 (((runtime->period_size * HZ) / runtime->rate)
408 /* move new_hw_ptr according jiffies not pos variable */
409 new_hw_ptr = old_hw_ptr;
411 /* use loop to avoid checks for delta overflows */
412 /* the delta value is small or zero in most cases */
414 new_hw_ptr += runtime->period_size;
415 if (new_hw_ptr >= runtime->boundary) {
416 new_hw_ptr -= runtime->boundary;
421 /* align hw_base to buffer_size */
422 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
423 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
424 (long)pos, (long)hdelta,
425 (long)runtime->period_size, jdelta,
426 ((hdelta * HZ) / runtime->rate), hw_base,
427 (unsigned long)old_hw_ptr,
428 (unsigned long)new_hw_ptr);
429 /* reset values to proper state */
431 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
434 if (delta > runtime->period_size + runtime->period_size / 2) {
435 hw_ptr_error(substream, in_interrupt,
437 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
438 substream->stream, (long)delta,
444 if (runtime->status->hw_ptr == new_hw_ptr) {
445 runtime->hw_ptr_jiffies = curr_jiffies;
446 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
451 runtime->silence_size > 0)
452 snd_pcm_playback_silence(substream, new_hw_ptr);
455 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
457 delta += runtime->boundary;
458 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
459 runtime->hw_ptr_interrupt += delta;
460 if (runtime->hw_ptr_interrupt >= runtime->boundary)
461 runtime->hw_ptr_interrupt -= runtime->boundary;
463 runtime->hw_ptr_base = hw_base;
464 runtime->status->hw_ptr = new_hw_ptr;
465 runtime->hw_ptr_jiffies = curr_jiffies;
466 if (crossed_boundary) {
467 snd_BUG_ON(crossed_boundary != 1);
468 runtime->hw_ptr_wrap += runtime->boundary;
471 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
473 return snd_pcm_update_state(substream, runtime);
476 /* CAUTION: call it with irq disabled */
477 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
479 return snd_pcm_update_hw_ptr0(substream, 0);
483 * snd_pcm_set_ops - set the PCM operators
484 * @pcm: the pcm instance
485 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
486 * @ops: the operator table
488 * Sets the given PCM operators to the pcm instance.
490 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
491 const struct snd_pcm_ops *ops)
493 struct snd_pcm_str *stream = &pcm->streams[direction];
494 struct snd_pcm_substream *substream;
496 for (substream = stream->substream; substream != NULL; substream = substream->next)
497 substream->ops = ops;
499 EXPORT_SYMBOL(snd_pcm_set_ops);
502 * snd_pcm_set_sync - set the PCM sync id
503 * @substream: the pcm substream
505 * Sets the PCM sync identifier for the card.
507 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
509 struct snd_pcm_runtime *runtime = substream->runtime;
511 runtime->sync.id32[0] = substream->pcm->card->number;
512 runtime->sync.id32[1] = -1;
513 runtime->sync.id32[2] = -1;
514 runtime->sync.id32[3] = -1;
516 EXPORT_SYMBOL(snd_pcm_set_sync);
519 * Standard ioctl routine
522 static inline unsigned int div32(unsigned int a, unsigned int b,
533 static inline unsigned int div_down(unsigned int a, unsigned int b)
540 static inline unsigned int div_up(unsigned int a, unsigned int b)
552 static inline unsigned int mul(unsigned int a, unsigned int b)
556 if (div_down(UINT_MAX, a) < b)
561 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
562 unsigned int c, unsigned int *r)
564 u_int64_t n = (u_int64_t) a * b;
569 n = div_u64_rem(n, c, r);
578 * snd_interval_refine - refine the interval value of configurator
579 * @i: the interval value to refine
580 * @v: the interval value to refer to
582 * Refines the interval value with the reference value.
583 * The interval is changed to the range satisfying both intervals.
584 * The interval status (min, max, integer, etc.) are evaluated.
586 * Return: Positive if the value is changed, zero if it's not changed, or a
587 * negative error code.
589 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
592 if (snd_BUG_ON(snd_interval_empty(i)))
594 if (i->min < v->min) {
596 i->openmin = v->openmin;
598 } else if (i->min == v->min && !i->openmin && v->openmin) {
602 if (i->max > v->max) {
604 i->openmax = v->openmax;
606 } else if (i->max == v->max && !i->openmax && v->openmax) {
610 if (!i->integer && v->integer) {
623 } else if (!i->openmin && !i->openmax && i->min == i->max)
625 if (snd_interval_checkempty(i)) {
626 snd_interval_none(i);
631 EXPORT_SYMBOL(snd_interval_refine);
633 static int snd_interval_refine_first(struct snd_interval *i)
635 const unsigned int last_max = i->max;
637 if (snd_BUG_ON(snd_interval_empty(i)))
639 if (snd_interval_single(i))
644 /* only exclude max value if also excluded before refine */
645 i->openmax = (i->openmax && i->max >= last_max);
649 static int snd_interval_refine_last(struct snd_interval *i)
651 const unsigned int last_min = i->min;
653 if (snd_BUG_ON(snd_interval_empty(i)))
655 if (snd_interval_single(i))
660 /* only exclude min value if also excluded before refine */
661 i->openmin = (i->openmin && i->min <= last_min);
665 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
667 if (a->empty || b->empty) {
668 snd_interval_none(c);
672 c->min = mul(a->min, b->min);
673 c->openmin = (a->openmin || b->openmin);
674 c->max = mul(a->max, b->max);
675 c->openmax = (a->openmax || b->openmax);
676 c->integer = (a->integer && b->integer);
680 * snd_interval_div - refine the interval value with division
687 * Returns non-zero if the value is changed, zero if not changed.
689 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
692 if (a->empty || b->empty) {
693 snd_interval_none(c);
697 c->min = div32(a->min, b->max, &r);
698 c->openmin = (r || a->openmin || b->openmax);
700 c->max = div32(a->max, b->min, &r);
705 c->openmax = (a->openmax || b->openmin);
714 * snd_interval_muldivk - refine the interval value
717 * @k: divisor (as integer)
722 * Returns non-zero if the value is changed, zero if not changed.
724 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
725 unsigned int k, struct snd_interval *c)
728 if (a->empty || b->empty) {
729 snd_interval_none(c);
733 c->min = muldiv32(a->min, b->min, k, &r);
734 c->openmin = (r || a->openmin || b->openmin);
735 c->max = muldiv32(a->max, b->max, k, &r);
740 c->openmax = (a->openmax || b->openmax);
745 * snd_interval_mulkdiv - refine the interval value
747 * @k: dividend 2 (as integer)
753 * Returns non-zero if the value is changed, zero if not changed.
755 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
756 const struct snd_interval *b, struct snd_interval *c)
759 if (a->empty || b->empty) {
760 snd_interval_none(c);
764 c->min = muldiv32(a->min, k, b->max, &r);
765 c->openmin = (r || a->openmin || b->openmax);
767 c->max = muldiv32(a->max, k, b->min, &r);
772 c->openmax = (a->openmax || b->openmin);
784 * snd_interval_ratnum - refine the interval value
785 * @i: interval to refine
786 * @rats_count: number of ratnum_t
787 * @rats: ratnum_t array
788 * @nump: pointer to store the resultant numerator
789 * @denp: pointer to store the resultant denominator
791 * Return: Positive if the value is changed, zero if it's not changed, or a
792 * negative error code.
794 int snd_interval_ratnum(struct snd_interval *i,
795 unsigned int rats_count, const struct snd_ratnum *rats,
796 unsigned int *nump, unsigned int *denp)
798 unsigned int best_num, best_den;
801 struct snd_interval t;
803 unsigned int result_num, result_den;
806 best_num = best_den = best_diff = 0;
807 for (k = 0; k < rats_count; ++k) {
808 unsigned int num = rats[k].num;
810 unsigned int q = i->min;
814 den = div_up(num, q);
815 if (den < rats[k].den_min)
817 if (den > rats[k].den_max)
818 den = rats[k].den_max;
821 r = (den - rats[k].den_min) % rats[k].den_step;
825 diff = num - q * den;
829 diff * best_den < best_diff * den) {
839 t.min = div_down(best_num, best_den);
840 t.openmin = !!(best_num % best_den);
842 result_num = best_num;
843 result_diff = best_diff;
844 result_den = best_den;
845 best_num = best_den = best_diff = 0;
846 for (k = 0; k < rats_count; ++k) {
847 unsigned int num = rats[k].num;
849 unsigned int q = i->max;
855 den = div_down(num, q);
856 if (den > rats[k].den_max)
858 if (den < rats[k].den_min)
859 den = rats[k].den_min;
862 r = (den - rats[k].den_min) % rats[k].den_step;
864 den += rats[k].den_step - r;
866 diff = q * den - num;
870 diff * best_den < best_diff * den) {
880 t.max = div_up(best_num, best_den);
881 t.openmax = !!(best_num % best_den);
883 err = snd_interval_refine(i, &t);
887 if (snd_interval_single(i)) {
888 if (best_diff * result_den < result_diff * best_den) {
889 result_num = best_num;
890 result_den = best_den;
899 EXPORT_SYMBOL(snd_interval_ratnum);
902 * snd_interval_ratden - refine the interval value
903 * @i: interval to refine
904 * @rats_count: number of struct ratden
905 * @rats: struct ratden array
906 * @nump: pointer to store the resultant numerator
907 * @denp: pointer to store the resultant denominator
909 * Return: Positive if the value is changed, zero if it's not changed, or a
910 * negative error code.
912 static int snd_interval_ratden(struct snd_interval *i,
913 unsigned int rats_count,
914 const struct snd_ratden *rats,
915 unsigned int *nump, unsigned int *denp)
917 unsigned int best_num, best_diff, best_den;
919 struct snd_interval t;
922 best_num = best_den = best_diff = 0;
923 for (k = 0; k < rats_count; ++k) {
925 unsigned int den = rats[k].den;
926 unsigned int q = i->min;
929 if (num > rats[k].num_max)
931 if (num < rats[k].num_min)
932 num = rats[k].num_max;
935 r = (num - rats[k].num_min) % rats[k].num_step;
937 num += rats[k].num_step - r;
939 diff = num - q * den;
941 diff * best_den < best_diff * den) {
951 t.min = div_down(best_num, best_den);
952 t.openmin = !!(best_num % best_den);
954 best_num = best_den = best_diff = 0;
955 for (k = 0; k < rats_count; ++k) {
957 unsigned int den = rats[k].den;
958 unsigned int q = i->max;
961 if (num < rats[k].num_min)
963 if (num > rats[k].num_max)
964 num = rats[k].num_max;
967 r = (num - rats[k].num_min) % rats[k].num_step;
971 diff = q * den - num;
973 diff * best_den < best_diff * den) {
983 t.max = div_up(best_num, best_den);
984 t.openmax = !!(best_num % best_den);
986 err = snd_interval_refine(i, &t);
990 if (snd_interval_single(i)) {
1000 * snd_interval_list - refine the interval value from the list
1001 * @i: the interval value to refine
1002 * @count: the number of elements in the list
1003 * @list: the value list
1004 * @mask: the bit-mask to evaluate
1006 * Refines the interval value from the list.
1007 * When mask is non-zero, only the elements corresponding to bit 1 are
1010 * Return: Positive if the value is changed, zero if it's not changed, or a
1011 * negative error code.
1013 int snd_interval_list(struct snd_interval *i, unsigned int count,
1014 const unsigned int *list, unsigned int mask)
1017 struct snd_interval list_range;
1023 snd_interval_any(&list_range);
1024 list_range.min = UINT_MAX;
1026 for (k = 0; k < count; k++) {
1027 if (mask && !(mask & (1 << k)))
1029 if (!snd_interval_test(i, list[k]))
1031 list_range.min = min(list_range.min, list[k]);
1032 list_range.max = max(list_range.max, list[k]);
1034 return snd_interval_refine(i, &list_range);
1036 EXPORT_SYMBOL(snd_interval_list);
1039 * snd_interval_ranges - refine the interval value from the list of ranges
1040 * @i: the interval value to refine
1041 * @count: the number of elements in the list of ranges
1042 * @ranges: the ranges list
1043 * @mask: the bit-mask to evaluate
1045 * Refines the interval value from the list of ranges.
1046 * When mask is non-zero, only the elements corresponding to bit 1 are
1049 * Return: Positive if the value is changed, zero if it's not changed, or a
1050 * negative error code.
1052 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1053 const struct snd_interval *ranges, unsigned int mask)
1056 struct snd_interval range_union;
1057 struct snd_interval range;
1060 snd_interval_none(i);
1063 snd_interval_any(&range_union);
1064 range_union.min = UINT_MAX;
1065 range_union.max = 0;
1066 for (k = 0; k < count; k++) {
1067 if (mask && !(mask & (1 << k)))
1069 snd_interval_copy(&range, &ranges[k]);
1070 if (snd_interval_refine(&range, i) < 0)
1072 if (snd_interval_empty(&range))
1075 if (range.min < range_union.min) {
1076 range_union.min = range.min;
1077 range_union.openmin = 1;
1079 if (range.min == range_union.min && !range.openmin)
1080 range_union.openmin = 0;
1081 if (range.max > range_union.max) {
1082 range_union.max = range.max;
1083 range_union.openmax = 1;
1085 if (range.max == range_union.max && !range.openmax)
1086 range_union.openmax = 0;
1088 return snd_interval_refine(i, &range_union);
1090 EXPORT_SYMBOL(snd_interval_ranges);
1092 static int snd_interval_step(struct snd_interval *i, unsigned int step)
1097 if (n != 0 || i->openmin) {
1103 if (n != 0 || i->openmax) {
1108 if (snd_interval_checkempty(i)) {
1115 /* Info constraints helpers */
1118 * snd_pcm_hw_rule_add - add the hw-constraint rule
1119 * @runtime: the pcm runtime instance
1120 * @cond: condition bits
1121 * @var: the variable to evaluate
1122 * @func: the evaluation function
1123 * @private: the private data pointer passed to function
1124 * @dep: the dependent variables
1126 * Return: Zero if successful, or a negative error code on failure.
1128 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1130 snd_pcm_hw_rule_func_t func, void *private,
1133 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1134 struct snd_pcm_hw_rule *c;
1137 va_start(args, dep);
1138 if (constrs->rules_num >= constrs->rules_all) {
1139 struct snd_pcm_hw_rule *new;
1140 unsigned int new_rules = constrs->rules_all + 16;
1141 new = krealloc_array(constrs->rules, new_rules,
1142 sizeof(*c), GFP_KERNEL);
1147 constrs->rules = new;
1148 constrs->rules_all = new_rules;
1150 c = &constrs->rules[constrs->rules_num];
1154 c->private = private;
1157 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1164 dep = va_arg(args, int);
1166 constrs->rules_num++;
1170 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1173 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1174 * @runtime: PCM runtime instance
1175 * @var: hw_params variable to apply the mask
1176 * @mask: the bitmap mask
1178 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1180 * Return: Zero if successful, or a negative error code on failure.
1182 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1185 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1186 struct snd_mask *maskp = constrs_mask(constrs, var);
1187 *maskp->bits &= mask;
1188 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1189 if (*maskp->bits == 0)
1195 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1196 * @runtime: PCM runtime instance
1197 * @var: hw_params variable to apply the mask
1198 * @mask: the 64bit bitmap mask
1200 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1202 * Return: Zero if successful, or a negative error code on failure.
1204 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1207 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1208 struct snd_mask *maskp = constrs_mask(constrs, var);
1209 maskp->bits[0] &= (u_int32_t)mask;
1210 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1211 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1212 if (! maskp->bits[0] && ! maskp->bits[1])
1216 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1219 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1220 * @runtime: PCM runtime instance
1221 * @var: hw_params variable to apply the integer constraint
1223 * Apply the constraint of integer 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_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1230 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1231 return snd_interval_setinteger(constrs_interval(constrs, var));
1233 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1236 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1237 * @runtime: PCM runtime instance
1238 * @var: hw_params variable to apply the range
1239 * @min: the minimal value
1240 * @max: the maximal value
1242 * Apply the min/max range constraint to an interval parameter.
1244 * Return: Positive if the value is changed, zero if it's not changed, or a
1245 * negative error code.
1247 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1248 unsigned int min, unsigned int max)
1250 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1251 struct snd_interval t;
1254 t.openmin = t.openmax = 0;
1256 return snd_interval_refine(constrs_interval(constrs, var), &t);
1258 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1260 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1261 struct snd_pcm_hw_rule *rule)
1263 struct snd_pcm_hw_constraint_list *list = rule->private;
1264 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1269 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1270 * @runtime: PCM runtime instance
1271 * @cond: condition bits
1272 * @var: hw_params variable to apply the list constraint
1275 * Apply the list of constraints to an interval parameter.
1277 * Return: Zero if successful, or a negative error code on failure.
1279 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1281 snd_pcm_hw_param_t var,
1282 const struct snd_pcm_hw_constraint_list *l)
1284 return snd_pcm_hw_rule_add(runtime, cond, var,
1285 snd_pcm_hw_rule_list, (void *)l,
1288 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1290 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1291 struct snd_pcm_hw_rule *rule)
1293 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1294 return snd_interval_ranges(hw_param_interval(params, rule->var),
1295 r->count, r->ranges, r->mask);
1300 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1301 * @runtime: PCM runtime instance
1302 * @cond: condition bits
1303 * @var: hw_params variable to apply the list of range constraints
1306 * Apply the list of range constraints to an interval parameter.
1308 * Return: Zero if successful, or a negative error code on failure.
1310 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1312 snd_pcm_hw_param_t var,
1313 const struct snd_pcm_hw_constraint_ranges *r)
1315 return snd_pcm_hw_rule_add(runtime, cond, var,
1316 snd_pcm_hw_rule_ranges, (void *)r,
1319 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1321 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1322 struct snd_pcm_hw_rule *rule)
1324 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1325 unsigned int num = 0, den = 0;
1327 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1328 r->nrats, r->rats, &num, &den);
1329 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1330 params->rate_num = num;
1331 params->rate_den = den;
1337 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1338 * @runtime: PCM runtime instance
1339 * @cond: condition bits
1340 * @var: hw_params variable to apply the ratnums constraint
1341 * @r: struct snd_ratnums constriants
1343 * Return: Zero if successful, or a negative error code on failure.
1345 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1347 snd_pcm_hw_param_t var,
1348 const struct snd_pcm_hw_constraint_ratnums *r)
1350 return snd_pcm_hw_rule_add(runtime, cond, var,
1351 snd_pcm_hw_rule_ratnums, (void *)r,
1354 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1356 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1357 struct snd_pcm_hw_rule *rule)
1359 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1360 unsigned int num = 0, den = 0;
1361 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1362 r->nrats, r->rats, &num, &den);
1363 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1364 params->rate_num = num;
1365 params->rate_den = den;
1371 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1372 * @runtime: PCM runtime instance
1373 * @cond: condition bits
1374 * @var: hw_params variable to apply the ratdens constraint
1375 * @r: struct snd_ratdens constriants
1377 * Return: Zero if successful, or a negative error code on failure.
1379 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1381 snd_pcm_hw_param_t var,
1382 const struct snd_pcm_hw_constraint_ratdens *r)
1384 return snd_pcm_hw_rule_add(runtime, cond, var,
1385 snd_pcm_hw_rule_ratdens, (void *)r,
1388 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1390 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1391 struct snd_pcm_hw_rule *rule)
1393 unsigned int l = (unsigned long) rule->private;
1394 int width = l & 0xffff;
1395 unsigned int msbits = l >> 16;
1396 const struct snd_interval *i =
1397 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1399 if (!snd_interval_single(i))
1402 if ((snd_interval_value(i) == width) ||
1403 (width == 0 && snd_interval_value(i) > msbits))
1404 params->msbits = min_not_zero(params->msbits, msbits);
1410 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1411 * @runtime: PCM runtime instance
1412 * @cond: condition bits
1413 * @width: sample bits width
1414 * @msbits: msbits width
1416 * This constraint will set the number of most significant bits (msbits) if a
1417 * sample format with the specified width has been select. If width is set to 0
1418 * the msbits will be set for any sample format with a width larger than the
1421 * Return: Zero if successful, or a negative error code on failure.
1423 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1426 unsigned int msbits)
1428 unsigned long l = (msbits << 16) | width;
1429 return snd_pcm_hw_rule_add(runtime, cond, -1,
1430 snd_pcm_hw_rule_msbits,
1432 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1434 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1436 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1437 struct snd_pcm_hw_rule *rule)
1439 unsigned long step = (unsigned long) rule->private;
1440 return snd_interval_step(hw_param_interval(params, rule->var), step);
1444 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1445 * @runtime: PCM runtime instance
1446 * @cond: condition bits
1447 * @var: hw_params variable to apply the step constraint
1450 * Return: Zero if successful, or a negative error code on failure.
1452 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1454 snd_pcm_hw_param_t var,
1457 return snd_pcm_hw_rule_add(runtime, cond, var,
1458 snd_pcm_hw_rule_step, (void *) step,
1461 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1463 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1465 static const unsigned int pow2_sizes[] = {
1466 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1467 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1468 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1469 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1471 return snd_interval_list(hw_param_interval(params, rule->var),
1472 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1476 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1477 * @runtime: PCM runtime instance
1478 * @cond: condition bits
1479 * @var: hw_params variable to apply the power-of-2 constraint
1481 * Return: Zero if successful, or a negative error code on failure.
1483 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1485 snd_pcm_hw_param_t var)
1487 return snd_pcm_hw_rule_add(runtime, cond, var,
1488 snd_pcm_hw_rule_pow2, NULL,
1491 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1493 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1494 struct snd_pcm_hw_rule *rule)
1496 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1497 struct snd_interval *rate;
1499 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1500 return snd_interval_list(rate, 1, &base_rate, 0);
1504 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1505 * @runtime: PCM runtime instance
1506 * @base_rate: the rate at which the hardware does not resample
1508 * Return: Zero if successful, or a negative error code on failure.
1510 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1511 unsigned int base_rate)
1513 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1514 SNDRV_PCM_HW_PARAM_RATE,
1515 snd_pcm_hw_rule_noresample_func,
1516 (void *)(uintptr_t)base_rate,
1517 SNDRV_PCM_HW_PARAM_RATE, -1);
1519 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1521 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1522 snd_pcm_hw_param_t var)
1524 if (hw_is_mask(var)) {
1525 snd_mask_any(hw_param_mask(params, var));
1526 params->cmask |= 1 << var;
1527 params->rmask |= 1 << var;
1530 if (hw_is_interval(var)) {
1531 snd_interval_any(hw_param_interval(params, var));
1532 params->cmask |= 1 << var;
1533 params->rmask |= 1 << var;
1539 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1542 memset(params, 0, sizeof(*params));
1543 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1544 _snd_pcm_hw_param_any(params, k);
1545 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1546 _snd_pcm_hw_param_any(params, k);
1549 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1552 * snd_pcm_hw_param_value - return @params field @var value
1553 * @params: the hw_params instance
1554 * @var: parameter to retrieve
1555 * @dir: pointer to the direction (-1,0,1) or %NULL
1557 * Return: The value for field @var if it's fixed in configuration space
1558 * defined by @params. -%EINVAL otherwise.
1560 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1561 snd_pcm_hw_param_t var, int *dir)
1563 if (hw_is_mask(var)) {
1564 const struct snd_mask *mask = hw_param_mask_c(params, var);
1565 if (!snd_mask_single(mask))
1569 return snd_mask_value(mask);
1571 if (hw_is_interval(var)) {
1572 const struct snd_interval *i = hw_param_interval_c(params, var);
1573 if (!snd_interval_single(i))
1577 return snd_interval_value(i);
1581 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1583 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1584 snd_pcm_hw_param_t var)
1586 if (hw_is_mask(var)) {
1587 snd_mask_none(hw_param_mask(params, var));
1588 params->cmask |= 1 << var;
1589 params->rmask |= 1 << var;
1590 } else if (hw_is_interval(var)) {
1591 snd_interval_none(hw_param_interval(params, var));
1592 params->cmask |= 1 << var;
1593 params->rmask |= 1 << var;
1598 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1600 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1601 snd_pcm_hw_param_t var)
1604 if (hw_is_mask(var))
1605 changed = snd_mask_refine_first(hw_param_mask(params, var));
1606 else if (hw_is_interval(var))
1607 changed = snd_interval_refine_first(hw_param_interval(params, var));
1611 params->cmask |= 1 << var;
1612 params->rmask |= 1 << var;
1619 * snd_pcm_hw_param_first - refine config space and return minimum value
1620 * @pcm: PCM instance
1621 * @params: the hw_params instance
1622 * @var: parameter to retrieve
1623 * @dir: pointer to the direction (-1,0,1) or %NULL
1625 * Inside configuration space defined by @params remove from @var all
1626 * values > minimum. Reduce configuration space accordingly.
1628 * Return: The minimum, or a negative error code on failure.
1630 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1631 struct snd_pcm_hw_params *params,
1632 snd_pcm_hw_param_t var, int *dir)
1634 int changed = _snd_pcm_hw_param_first(params, var);
1637 if (params->rmask) {
1638 int err = snd_pcm_hw_refine(pcm, params);
1642 return snd_pcm_hw_param_value(params, var, dir);
1644 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1646 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1647 snd_pcm_hw_param_t var)
1650 if (hw_is_mask(var))
1651 changed = snd_mask_refine_last(hw_param_mask(params, var));
1652 else if (hw_is_interval(var))
1653 changed = snd_interval_refine_last(hw_param_interval(params, var));
1657 params->cmask |= 1 << var;
1658 params->rmask |= 1 << var;
1665 * snd_pcm_hw_param_last - refine config space and return maximum value
1666 * @pcm: PCM instance
1667 * @params: the hw_params instance
1668 * @var: parameter to retrieve
1669 * @dir: pointer to the direction (-1,0,1) or %NULL
1671 * Inside configuration space defined by @params remove from @var all
1672 * values < maximum. Reduce configuration space accordingly.
1674 * Return: The maximum, or a negative error code on failure.
1676 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1677 struct snd_pcm_hw_params *params,
1678 snd_pcm_hw_param_t var, int *dir)
1680 int changed = _snd_pcm_hw_param_last(params, var);
1683 if (params->rmask) {
1684 int err = snd_pcm_hw_refine(pcm, params);
1688 return snd_pcm_hw_param_value(params, var, dir);
1690 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1692 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1695 struct snd_pcm_runtime *runtime = substream->runtime;
1696 unsigned long flags;
1697 snd_pcm_stream_lock_irqsave(substream, flags);
1698 if (snd_pcm_running(substream) &&
1699 snd_pcm_update_hw_ptr(substream) >= 0)
1700 runtime->status->hw_ptr %= runtime->buffer_size;
1702 runtime->status->hw_ptr = 0;
1703 runtime->hw_ptr_wrap = 0;
1705 snd_pcm_stream_unlock_irqrestore(substream, flags);
1709 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1712 struct snd_pcm_channel_info *info = arg;
1713 struct snd_pcm_runtime *runtime = substream->runtime;
1715 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1719 width = snd_pcm_format_physical_width(runtime->format);
1723 switch (runtime->access) {
1724 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1725 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1726 info->first = info->channel * width;
1727 info->step = runtime->channels * width;
1729 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1730 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1732 size_t size = runtime->dma_bytes / runtime->channels;
1733 info->first = info->channel * size * 8;
1744 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1747 struct snd_pcm_hw_params *params = arg;
1748 snd_pcm_format_t format;
1752 params->fifo_size = substream->runtime->hw.fifo_size;
1753 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1754 format = params_format(params);
1755 channels = params_channels(params);
1756 frame_size = snd_pcm_format_size(format, channels);
1758 params->fifo_size /= frame_size;
1764 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1765 * @substream: the pcm substream instance
1766 * @cmd: ioctl command
1767 * @arg: ioctl argument
1769 * Processes the generic ioctl commands for PCM.
1770 * Can be passed as the ioctl callback for PCM ops.
1772 * Return: Zero if successful, or a negative error code on failure.
1774 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1775 unsigned int cmd, void *arg)
1778 case SNDRV_PCM_IOCTL1_RESET:
1779 return snd_pcm_lib_ioctl_reset(substream, arg);
1780 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1781 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1782 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1783 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1787 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1790 * snd_pcm_period_elapsed_under_stream_lock() - update the status of runtime for the next period
1791 * under acquired lock of PCM substream.
1792 * @substream: the instance of pcm substream.
1794 * This function is called when the batch of audio data frames as the same size as the period of
1795 * buffer is already processed in audio data transmission.
1797 * The call of function updates the status of runtime with the latest position of audio data
1798 * transmission, checks overrun and underrun over buffer, awaken user processes from waiting for
1799 * available audio data frames, sampling audio timestamp, and performs stop or drain the PCM
1800 * substream according to configured threshold.
1802 * The function is intended to use for the case that PCM driver operates audio data frames under
1803 * acquired lock of PCM substream; e.g. in callback of any operation of &snd_pcm_ops in process
1804 * context. In any interrupt context, it's preferrable to use ``snd_pcm_period_elapsed()`` instead
1805 * since lock of PCM substream should be acquired in advance.
1807 * Developer should pay enough attention that some callbacks in &snd_pcm_ops are done by the call of
1810 * - .pointer - to retrieve current position of audio data transmission by frame count or XRUN state.
1811 * - .trigger - with SNDRV_PCM_TRIGGER_STOP at XRUN or DRAINING state.
1812 * - .get_time_info - to retrieve audio time stamp if needed.
1814 * Even if more than one periods have elapsed since the last call, you have to call this only once.
1816 void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream)
1818 struct snd_pcm_runtime *runtime;
1820 if (PCM_RUNTIME_CHECK(substream))
1822 runtime = substream->runtime;
1824 if (!snd_pcm_running(substream) ||
1825 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1828 #ifdef CONFIG_SND_PCM_TIMER
1829 if (substream->timer_running)
1830 snd_timer_interrupt(substream->timer, 1);
1833 snd_kill_fasync(runtime->fasync, SIGIO, POLL_IN);
1835 EXPORT_SYMBOL(snd_pcm_period_elapsed_under_stream_lock);
1838 * snd_pcm_period_elapsed() - update the status of runtime for the next period by acquiring lock of
1840 * @substream: the instance of PCM substream.
1842 * This function is mostly similar to ``snd_pcm_period_elapsed_under_stream_lock()`` except for
1843 * acquiring lock of PCM substream voluntarily.
1845 * It's typically called by any type of IRQ handler when hardware IRQ occurs to notify event that
1846 * the batch of audio data frames as the same size as the period of buffer is already processed in
1847 * audio data transmission.
1849 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1851 unsigned long flags;
1853 if (snd_BUG_ON(!substream))
1856 snd_pcm_stream_lock_irqsave(substream, flags);
1857 snd_pcm_period_elapsed_under_stream_lock(substream);
1858 snd_pcm_stream_unlock_irqrestore(substream, flags);
1860 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1863 * Wait until avail_min data becomes available
1864 * Returns a negative error code if any error occurs during operation.
1865 * The available space is stored on availp. When err = 0 and avail = 0
1866 * on the capture stream, it indicates the stream is in DRAINING state.
1868 static int wait_for_avail(struct snd_pcm_substream *substream,
1869 snd_pcm_uframes_t *availp)
1871 struct snd_pcm_runtime *runtime = substream->runtime;
1872 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1873 wait_queue_entry_t wait;
1875 snd_pcm_uframes_t avail = 0;
1876 long wait_time, tout;
1878 init_waitqueue_entry(&wait, current);
1879 set_current_state(TASK_INTERRUPTIBLE);
1880 add_wait_queue(&runtime->tsleep, &wait);
1882 if (runtime->no_period_wakeup)
1883 wait_time = MAX_SCHEDULE_TIMEOUT;
1885 /* use wait time from substream if available */
1886 if (substream->wait_time) {
1887 wait_time = substream->wait_time;
1891 if (runtime->rate) {
1892 long t = runtime->buffer_size * 1100 / runtime->rate;
1893 wait_time = max(t, wait_time);
1896 wait_time = msecs_to_jiffies(wait_time);
1900 if (signal_pending(current)) {
1906 * We need to check if space became available already
1907 * (and thus the wakeup happened already) first to close
1908 * the race of space already having become available.
1909 * This check must happen after been added to the waitqueue
1910 * and having current state be INTERRUPTIBLE.
1912 avail = snd_pcm_avail(substream);
1913 if (avail >= runtime->twake)
1915 snd_pcm_stream_unlock_irq(substream);
1917 tout = schedule_timeout(wait_time);
1919 snd_pcm_stream_lock_irq(substream);
1920 set_current_state(TASK_INTERRUPTIBLE);
1921 switch (runtime->state) {
1922 case SNDRV_PCM_STATE_SUSPENDED:
1925 case SNDRV_PCM_STATE_XRUN:
1928 case SNDRV_PCM_STATE_DRAINING:
1932 avail = 0; /* indicate draining */
1934 case SNDRV_PCM_STATE_OPEN:
1935 case SNDRV_PCM_STATE_SETUP:
1936 case SNDRV_PCM_STATE_DISCONNECTED:
1939 case SNDRV_PCM_STATE_PAUSED:
1943 pcm_dbg(substream->pcm,
1944 "%s timeout (DMA or IRQ trouble?)\n",
1945 is_playback ? "playback write" : "capture read");
1951 set_current_state(TASK_RUNNING);
1952 remove_wait_queue(&runtime->tsleep, &wait);
1957 typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
1958 int channel, unsigned long hwoff,
1959 void *buf, unsigned long bytes);
1961 typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1962 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
1964 /* calculate the target DMA-buffer position to be written/read */
1965 static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
1966 int channel, unsigned long hwoff)
1968 return runtime->dma_area + hwoff +
1969 channel * (runtime->dma_bytes / runtime->channels);
1972 /* default copy_user ops for write; used for both interleaved and non- modes */
1973 static int default_write_copy(struct snd_pcm_substream *substream,
1974 int channel, unsigned long hwoff,
1975 void *buf, unsigned long bytes)
1977 if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
1978 (void __user *)buf, bytes))
1983 /* default copy_kernel ops for write */
1984 static int default_write_copy_kernel(struct snd_pcm_substream *substream,
1985 int channel, unsigned long hwoff,
1986 void *buf, unsigned long bytes)
1988 memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
1992 /* fill silence instead of copy data; called as a transfer helper
1993 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
1994 * a NULL buffer is passed
1996 static int fill_silence(struct snd_pcm_substream *substream, int channel,
1997 unsigned long hwoff, void *buf, unsigned long bytes)
1999 struct snd_pcm_runtime *runtime = substream->runtime;
2001 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
2003 if (substream->ops->fill_silence)
2004 return substream->ops->fill_silence(substream, channel,
2007 snd_pcm_format_set_silence(runtime->format,
2008 get_dma_ptr(runtime, channel, hwoff),
2009 bytes_to_samples(runtime, bytes));
2013 /* default copy_user ops for read; used for both interleaved and non- modes */
2014 static int default_read_copy(struct snd_pcm_substream *substream,
2015 int channel, unsigned long hwoff,
2016 void *buf, unsigned long bytes)
2018 if (copy_to_user((void __user *)buf,
2019 get_dma_ptr(substream->runtime, channel, hwoff),
2025 /* default copy_kernel ops for read */
2026 static int default_read_copy_kernel(struct snd_pcm_substream *substream,
2027 int channel, unsigned long hwoff,
2028 void *buf, unsigned long bytes)
2030 memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
2034 /* call transfer function with the converted pointers and sizes;
2035 * for interleaved mode, it's one shot for all samples
2037 static int interleaved_copy(struct snd_pcm_substream *substream,
2038 snd_pcm_uframes_t hwoff, void *data,
2039 snd_pcm_uframes_t off,
2040 snd_pcm_uframes_t frames,
2041 pcm_transfer_f transfer)
2043 struct snd_pcm_runtime *runtime = substream->runtime;
2045 /* convert to bytes */
2046 hwoff = frames_to_bytes(runtime, hwoff);
2047 off = frames_to_bytes(runtime, off);
2048 frames = frames_to_bytes(runtime, frames);
2049 return transfer(substream, 0, hwoff, data + off, frames);
2052 /* call transfer function with the converted pointers and sizes for each
2053 * non-interleaved channel; when buffer is NULL, silencing instead of copying
2055 static int noninterleaved_copy(struct snd_pcm_substream *substream,
2056 snd_pcm_uframes_t hwoff, void *data,
2057 snd_pcm_uframes_t off,
2058 snd_pcm_uframes_t frames,
2059 pcm_transfer_f transfer)
2061 struct snd_pcm_runtime *runtime = substream->runtime;
2062 int channels = runtime->channels;
2066 /* convert to bytes; note that it's not frames_to_bytes() here.
2067 * in non-interleaved mode, we copy for each channel, thus
2068 * each copy is n_samples bytes x channels = whole frames.
2070 off = samples_to_bytes(runtime, off);
2071 frames = samples_to_bytes(runtime, frames);
2072 hwoff = samples_to_bytes(runtime, hwoff);
2073 for (c = 0; c < channels; ++c, ++bufs) {
2074 if (!data || !*bufs)
2075 err = fill_silence(substream, c, hwoff, NULL, frames);
2077 err = transfer(substream, c, hwoff, *bufs + off,
2085 /* fill silence on the given buffer position;
2086 * called from snd_pcm_playback_silence()
2088 static int fill_silence_frames(struct snd_pcm_substream *substream,
2089 snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2091 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2092 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2093 return interleaved_copy(substream, off, NULL, 0, frames,
2096 return noninterleaved_copy(substream, off, NULL, 0, frames,
2100 /* sanity-check for read/write methods */
2101 static int pcm_sanity_check(struct snd_pcm_substream *substream)
2103 struct snd_pcm_runtime *runtime;
2104 if (PCM_RUNTIME_CHECK(substream))
2106 runtime = substream->runtime;
2107 if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
2109 if (runtime->state == SNDRV_PCM_STATE_OPEN)
2114 static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
2116 switch (runtime->state) {
2117 case SNDRV_PCM_STATE_PREPARED:
2118 case SNDRV_PCM_STATE_RUNNING:
2119 case SNDRV_PCM_STATE_PAUSED:
2121 case SNDRV_PCM_STATE_XRUN:
2123 case SNDRV_PCM_STATE_SUSPENDED:
2130 /* update to the given appl_ptr and call ack callback if needed;
2131 * when an error is returned, take back to the original value
2133 int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
2134 snd_pcm_uframes_t appl_ptr)
2136 struct snd_pcm_runtime *runtime = substream->runtime;
2137 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2138 snd_pcm_sframes_t diff;
2141 if (old_appl_ptr == appl_ptr)
2144 if (appl_ptr >= runtime->boundary)
2147 * check if a rewind is requested by the application
2149 if (substream->runtime->info & SNDRV_PCM_INFO_NO_REWINDS) {
2150 diff = appl_ptr - old_appl_ptr;
2152 if (diff > runtime->buffer_size)
2155 if (runtime->boundary + diff > runtime->buffer_size)
2160 runtime->control->appl_ptr = appl_ptr;
2161 if (substream->ops->ack) {
2162 ret = substream->ops->ack(substream);
2164 runtime->control->appl_ptr = old_appl_ptr;
2166 __snd_pcm_xrun(substream);
2171 trace_applptr(substream, old_appl_ptr, appl_ptr);
2176 /* the common loop for read/write data */
2177 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2178 void *data, bool interleaved,
2179 snd_pcm_uframes_t size, bool in_kernel)
2181 struct snd_pcm_runtime *runtime = substream->runtime;
2182 snd_pcm_uframes_t xfer = 0;
2183 snd_pcm_uframes_t offset = 0;
2184 snd_pcm_uframes_t avail;
2186 pcm_transfer_f transfer;
2191 err = pcm_sanity_check(substream);
2195 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2197 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2198 runtime->channels > 1)
2200 writer = interleaved_copy;
2202 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2204 writer = noninterleaved_copy;
2209 transfer = fill_silence;
2212 } else if (in_kernel) {
2213 if (substream->ops->copy_kernel)
2214 transfer = substream->ops->copy_kernel;
2216 transfer = is_playback ?
2217 default_write_copy_kernel : default_read_copy_kernel;
2219 if (substream->ops->copy_user)
2220 transfer = (pcm_transfer_f)substream->ops->copy_user;
2222 transfer = is_playback ?
2223 default_write_copy : default_read_copy;
2229 nonblock = !!(substream->f_flags & O_NONBLOCK);
2231 snd_pcm_stream_lock_irq(substream);
2232 err = pcm_accessible_state(runtime);
2236 runtime->twake = runtime->control->avail_min ? : 1;
2237 if (runtime->state == SNDRV_PCM_STATE_RUNNING)
2238 snd_pcm_update_hw_ptr(substream);
2241 * If size < start_threshold, wait indefinitely. Another
2242 * thread may start capture
2245 runtime->state == SNDRV_PCM_STATE_PREPARED &&
2246 size >= runtime->start_threshold) {
2247 err = snd_pcm_start(substream);
2252 avail = snd_pcm_avail(substream);
2255 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2256 snd_pcm_uframes_t cont;
2259 runtime->state == SNDRV_PCM_STATE_DRAINING) {
2260 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2267 runtime->twake = min_t(snd_pcm_uframes_t, size,
2268 runtime->control->avail_min ? : 1);
2269 err = wait_for_avail(substream, &avail);
2273 continue; /* draining */
2275 frames = size > avail ? avail : size;
2276 appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2277 appl_ofs = appl_ptr % runtime->buffer_size;
2278 cont = runtime->buffer_size - appl_ofs;
2281 if (snd_BUG_ON(!frames)) {
2285 if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) {
2289 snd_pcm_stream_unlock_irq(substream);
2291 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
2292 err = writer(substream, appl_ofs, data, offset, frames,
2295 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
2296 snd_pcm_stream_lock_irq(substream);
2297 atomic_dec(&runtime->buffer_accessing);
2300 err = pcm_accessible_state(runtime);
2304 if (appl_ptr >= runtime->boundary)
2305 appl_ptr -= runtime->boundary;
2306 err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2315 runtime->state == SNDRV_PCM_STATE_PREPARED &&
2316 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2317 err = snd_pcm_start(substream);
2324 if (xfer > 0 && err >= 0)
2325 snd_pcm_update_state(substream, runtime);
2326 snd_pcm_stream_unlock_irq(substream);
2327 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2329 EXPORT_SYMBOL(__snd_pcm_lib_xfer);
2332 * standard channel mapping helpers
2335 /* default channel maps for multi-channel playbacks, up to 8 channels */
2336 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2338 .map = { SNDRV_CHMAP_MONO } },
2340 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2342 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2343 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2345 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2346 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2347 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2349 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2350 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2351 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2352 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2355 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2357 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2358 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2360 .map = { SNDRV_CHMAP_MONO } },
2362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2364 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2365 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2367 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2368 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2369 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2371 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2372 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2373 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2374 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2377 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2379 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2381 if (ch > info->max_channels)
2383 return !info->channel_mask || (info->channel_mask & (1U << ch));
2386 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_info *uinfo)
2389 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2391 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2392 uinfo->count = info->max_channels;
2393 uinfo->value.integer.min = 0;
2394 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2398 /* get callback for channel map ctl element
2399 * stores the channel position firstly matching with the current channels
2401 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2404 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2405 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2406 struct snd_pcm_substream *substream;
2407 const struct snd_pcm_chmap_elem *map;
2411 substream = snd_pcm_chmap_substream(info, idx);
2414 memset(ucontrol->value.integer.value, 0,
2415 sizeof(long) * info->max_channels);
2416 if (!substream->runtime)
2417 return 0; /* no channels set */
2418 for (map = info->chmap; map->channels; map++) {
2420 if (map->channels == substream->runtime->channels &&
2421 valid_chmap_channels(info, map->channels)) {
2422 for (i = 0; i < map->channels; i++)
2423 ucontrol->value.integer.value[i] = map->map[i];
2430 /* tlv callback for channel map ctl element
2431 * expands the pre-defined channel maps in a form of TLV
2433 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2434 unsigned int size, unsigned int __user *tlv)
2436 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2437 const struct snd_pcm_chmap_elem *map;
2438 unsigned int __user *dst;
2445 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2449 for (map = info->chmap; map->channels; map++) {
2450 int chs_bytes = map->channels * 4;
2451 if (!valid_chmap_channels(info, map->channels))
2455 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2456 put_user(chs_bytes, dst + 1))
2461 if (size < chs_bytes)
2465 for (c = 0; c < map->channels; c++) {
2466 if (put_user(map->map[c], dst))
2471 if (put_user(count, tlv + 1))
2476 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2478 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2479 info->pcm->streams[info->stream].chmap_kctl = NULL;
2484 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2485 * @pcm: the assigned PCM instance
2486 * @stream: stream direction
2487 * @chmap: channel map elements (for query)
2488 * @max_channels: the max number of channels for the stream
2489 * @private_value: the value passed to each kcontrol's private_value field
2490 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2492 * Create channel-mapping control elements assigned to the given PCM stream(s).
2493 * Return: Zero if successful, or a negative error value.
2495 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2496 const struct snd_pcm_chmap_elem *chmap,
2498 unsigned long private_value,
2499 struct snd_pcm_chmap **info_ret)
2501 struct snd_pcm_chmap *info;
2502 struct snd_kcontrol_new knew = {
2503 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2504 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2505 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2506 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2507 .info = pcm_chmap_ctl_info,
2508 .get = pcm_chmap_ctl_get,
2509 .tlv.c = pcm_chmap_ctl_tlv,
2513 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2515 info = kzalloc(sizeof(*info), GFP_KERNEL);
2519 info->stream = stream;
2520 info->chmap = chmap;
2521 info->max_channels = max_channels;
2522 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2523 knew.name = "Playback Channel Map";
2525 knew.name = "Capture Channel Map";
2526 knew.device = pcm->device;
2527 knew.count = pcm->streams[stream].substream_count;
2528 knew.private_value = private_value;
2529 info->kctl = snd_ctl_new1(&knew, info);
2534 info->kctl->private_free = pcm_chmap_ctl_private_free;
2535 err = snd_ctl_add(pcm->card, info->kctl);
2538 pcm->streams[stream].chmap_kctl = info->kctl;
2543 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);