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 struct timespec64 tstamp;
150 snd_pcm_gettime(runtime, &tstamp);
151 runtime->status->tstamp.tv_sec = tstamp.tv_sec;
152 runtime->status->tstamp.tv_nsec = tstamp.tv_nsec;
154 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
155 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
157 snd_pcm_debug_name(substream, name, sizeof(name));
158 pcm_warn(substream->pcm, "XRUN: %s\n", name);
159 dump_stack_on_xrun(substream);
163 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
164 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
166 trace_hw_ptr_error(substream, reason); \
167 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
168 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
169 (in_interrupt) ? 'Q' : 'P', ##args); \
170 dump_stack_on_xrun(substream); \
174 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
176 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
180 int snd_pcm_update_state(struct snd_pcm_substream *substream,
181 struct snd_pcm_runtime *runtime)
183 snd_pcm_uframes_t avail;
185 avail = snd_pcm_avail(substream);
186 if (avail > runtime->avail_max)
187 runtime->avail_max = avail;
188 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
189 if (avail >= runtime->buffer_size) {
190 snd_pcm_drain_done(substream);
194 if (avail >= runtime->stop_threshold) {
195 __snd_pcm_xrun(substream);
199 if (runtime->twake) {
200 if (avail >= runtime->twake)
201 wake_up(&runtime->tsleep);
202 } else if (avail >= runtime->control->avail_min)
203 wake_up(&runtime->sleep);
207 static void update_audio_tstamp(struct snd_pcm_substream *substream,
208 struct timespec64 *curr_tstamp,
209 struct timespec64 *audio_tstamp)
211 struct snd_pcm_runtime *runtime = substream->runtime;
212 u64 audio_frames, audio_nsecs;
213 struct timespec64 driver_tstamp;
215 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
218 if (!(substream->ops->get_time_info) ||
219 (runtime->audio_tstamp_report.actual_type ==
220 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
223 * provide audio timestamp derived from pointer position
224 * add delay only if requested
227 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
229 if (runtime->audio_tstamp_config.report_delay) {
230 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
231 audio_frames -= runtime->delay;
233 audio_frames += runtime->delay;
235 audio_nsecs = div_u64(audio_frames * 1000000000LL,
237 *audio_tstamp = ns_to_timespec64(audio_nsecs);
240 if (runtime->status->audio_tstamp.tv_sec != audio_tstamp->tv_sec ||
241 runtime->status->audio_tstamp.tv_nsec != audio_tstamp->tv_nsec) {
242 runtime->status->audio_tstamp.tv_sec = audio_tstamp->tv_sec;
243 runtime->status->audio_tstamp.tv_nsec = audio_tstamp->tv_nsec;
244 runtime->status->tstamp.tv_sec = curr_tstamp->tv_sec;
245 runtime->status->tstamp.tv_nsec = curr_tstamp->tv_nsec;
250 * re-take a driver timestamp to let apps detect if the reference tstamp
251 * read by low-level hardware was provided with a delay
253 snd_pcm_gettime(substream->runtime, &driver_tstamp);
254 runtime->driver_tstamp = driver_tstamp;
257 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
258 unsigned int in_interrupt)
260 struct snd_pcm_runtime *runtime = substream->runtime;
261 snd_pcm_uframes_t pos;
262 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
263 snd_pcm_sframes_t hdelta, delta;
264 unsigned long jdelta;
265 unsigned long curr_jiffies;
266 struct timespec64 curr_tstamp;
267 struct timespec64 audio_tstamp;
268 int crossed_boundary = 0;
270 old_hw_ptr = runtime->status->hw_ptr;
273 * group pointer, time and jiffies reads to allow for more
274 * accurate correlations/corrections.
275 * The values are stored at the end of this routine after
276 * corrections for hw_ptr position
278 pos = substream->ops->pointer(substream);
279 curr_jiffies = jiffies;
280 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
281 if ((substream->ops->get_time_info) &&
282 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
283 substream->ops->get_time_info(substream, &curr_tstamp,
285 &runtime->audio_tstamp_config,
286 &runtime->audio_tstamp_report);
288 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
289 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
290 snd_pcm_gettime(runtime, &curr_tstamp);
292 snd_pcm_gettime(runtime, &curr_tstamp);
295 if (pos == SNDRV_PCM_POS_XRUN) {
296 __snd_pcm_xrun(substream);
299 if (pos >= runtime->buffer_size) {
300 if (printk_ratelimit()) {
302 snd_pcm_debug_name(substream, name, sizeof(name));
303 pcm_err(substream->pcm,
304 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
305 name, pos, runtime->buffer_size,
306 runtime->period_size);
310 pos -= pos % runtime->min_align;
311 trace_hwptr(substream, pos, in_interrupt);
312 hw_base = runtime->hw_ptr_base;
313 new_hw_ptr = hw_base + pos;
315 /* we know that one period was processed */
316 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
317 delta = runtime->hw_ptr_interrupt + runtime->period_size;
318 if (delta > new_hw_ptr) {
319 /* check for double acknowledged interrupts */
320 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
321 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
322 hw_base += runtime->buffer_size;
323 if (hw_base >= runtime->boundary) {
327 new_hw_ptr = hw_base + pos;
332 /* new_hw_ptr might be lower than old_hw_ptr in case when */
333 /* pointer crosses the end of the ring buffer */
334 if (new_hw_ptr < old_hw_ptr) {
335 hw_base += runtime->buffer_size;
336 if (hw_base >= runtime->boundary) {
340 new_hw_ptr = hw_base + pos;
343 delta = new_hw_ptr - old_hw_ptr;
345 delta += runtime->boundary;
347 if (runtime->no_period_wakeup) {
348 snd_pcm_sframes_t xrun_threshold;
350 * Without regular period interrupts, we have to check
351 * the elapsed time to detect xruns.
353 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
354 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
356 hdelta = jdelta - delta * HZ / runtime->rate;
357 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
358 while (hdelta > xrun_threshold) {
359 delta += runtime->buffer_size;
360 hw_base += runtime->buffer_size;
361 if (hw_base >= runtime->boundary) {
365 new_hw_ptr = hw_base + pos;
366 hdelta -= runtime->hw_ptr_buffer_jiffies;
371 /* something must be really wrong */
372 if (delta >= runtime->buffer_size + runtime->period_size) {
373 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
374 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
375 substream->stream, (long)pos,
376 (long)new_hw_ptr, (long)old_hw_ptr);
380 /* Do jiffies check only in xrun_debug mode */
381 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
382 goto no_jiffies_check;
384 /* Skip the jiffies check for hardwares with BATCH flag.
385 * Such hardware usually just increases the position at each IRQ,
386 * thus it can't give any strange position.
388 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
389 goto no_jiffies_check;
391 if (hdelta < runtime->delay)
392 goto no_jiffies_check;
393 hdelta -= runtime->delay;
394 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
395 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
397 (((runtime->period_size * HZ) / runtime->rate)
399 /* move new_hw_ptr according jiffies not pos variable */
400 new_hw_ptr = old_hw_ptr;
402 /* use loop to avoid checks for delta overflows */
403 /* the delta value is small or zero in most cases */
405 new_hw_ptr += runtime->period_size;
406 if (new_hw_ptr >= runtime->boundary) {
407 new_hw_ptr -= runtime->boundary;
412 /* align hw_base to buffer_size */
413 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
414 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
415 (long)pos, (long)hdelta,
416 (long)runtime->period_size, jdelta,
417 ((hdelta * HZ) / runtime->rate), hw_base,
418 (unsigned long)old_hw_ptr,
419 (unsigned long)new_hw_ptr);
420 /* reset values to proper state */
422 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
425 if (delta > runtime->period_size + runtime->period_size / 2) {
426 hw_ptr_error(substream, in_interrupt,
428 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
429 substream->stream, (long)delta,
435 if (runtime->status->hw_ptr == new_hw_ptr) {
436 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
441 runtime->silence_size > 0)
442 snd_pcm_playback_silence(substream, new_hw_ptr);
445 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
447 delta += runtime->boundary;
448 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
449 runtime->hw_ptr_interrupt += delta;
450 if (runtime->hw_ptr_interrupt >= runtime->boundary)
451 runtime->hw_ptr_interrupt -= runtime->boundary;
453 runtime->hw_ptr_base = hw_base;
454 runtime->status->hw_ptr = new_hw_ptr;
455 runtime->hw_ptr_jiffies = curr_jiffies;
456 if (crossed_boundary) {
457 snd_BUG_ON(crossed_boundary != 1);
458 runtime->hw_ptr_wrap += runtime->boundary;
461 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
463 return snd_pcm_update_state(substream, runtime);
466 /* CAUTION: call it with irq disabled */
467 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
469 return snd_pcm_update_hw_ptr0(substream, 0);
473 * snd_pcm_set_ops - set the PCM operators
474 * @pcm: the pcm instance
475 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
476 * @ops: the operator table
478 * Sets the given PCM operators to the pcm instance.
480 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
481 const struct snd_pcm_ops *ops)
483 struct snd_pcm_str *stream = &pcm->streams[direction];
484 struct snd_pcm_substream *substream;
486 for (substream = stream->substream; substream != NULL; substream = substream->next)
487 substream->ops = ops;
489 EXPORT_SYMBOL(snd_pcm_set_ops);
492 * snd_pcm_sync - set the PCM sync id
493 * @substream: the pcm substream
495 * Sets the PCM sync identifier for the card.
497 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
499 struct snd_pcm_runtime *runtime = substream->runtime;
501 runtime->sync.id32[0] = substream->pcm->card->number;
502 runtime->sync.id32[1] = -1;
503 runtime->sync.id32[2] = -1;
504 runtime->sync.id32[3] = -1;
506 EXPORT_SYMBOL(snd_pcm_set_sync);
509 * Standard ioctl routine
512 static inline unsigned int div32(unsigned int a, unsigned int b,
523 static inline unsigned int div_down(unsigned int a, unsigned int b)
530 static inline unsigned int div_up(unsigned int a, unsigned int b)
542 static inline unsigned int mul(unsigned int a, unsigned int b)
546 if (div_down(UINT_MAX, a) < b)
551 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
552 unsigned int c, unsigned int *r)
554 u_int64_t n = (u_int64_t) a * b;
559 n = div_u64_rem(n, c, r);
568 * snd_interval_refine - refine the interval value of configurator
569 * @i: the interval value to refine
570 * @v: the interval value to refer to
572 * Refines the interval value with the reference value.
573 * The interval is changed to the range satisfying both intervals.
574 * The interval status (min, max, integer, etc.) are evaluated.
576 * Return: Positive if the value is changed, zero if it's not changed, or a
577 * negative error code.
579 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
582 if (snd_BUG_ON(snd_interval_empty(i)))
584 if (i->min < v->min) {
586 i->openmin = v->openmin;
588 } else if (i->min == v->min && !i->openmin && v->openmin) {
592 if (i->max > v->max) {
594 i->openmax = v->openmax;
596 } else if (i->max == v->max && !i->openmax && v->openmax) {
600 if (!i->integer && v->integer) {
613 } else if (!i->openmin && !i->openmax && i->min == i->max)
615 if (snd_interval_checkempty(i)) {
616 snd_interval_none(i);
621 EXPORT_SYMBOL(snd_interval_refine);
623 static int snd_interval_refine_first(struct snd_interval *i)
625 const unsigned int last_max = i->max;
627 if (snd_BUG_ON(snd_interval_empty(i)))
629 if (snd_interval_single(i))
634 /* only exclude max value if also excluded before refine */
635 i->openmax = (i->openmax && i->max >= last_max);
639 static int snd_interval_refine_last(struct snd_interval *i)
641 const unsigned int last_min = i->min;
643 if (snd_BUG_ON(snd_interval_empty(i)))
645 if (snd_interval_single(i))
650 /* only exclude min value if also excluded before refine */
651 i->openmin = (i->openmin && i->min <= last_min);
655 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
657 if (a->empty || b->empty) {
658 snd_interval_none(c);
662 c->min = mul(a->min, b->min);
663 c->openmin = (a->openmin || b->openmin);
664 c->max = mul(a->max, b->max);
665 c->openmax = (a->openmax || b->openmax);
666 c->integer = (a->integer && b->integer);
670 * snd_interval_div - refine the interval value with division
677 * Returns non-zero if the value is changed, zero if not changed.
679 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
682 if (a->empty || b->empty) {
683 snd_interval_none(c);
687 c->min = div32(a->min, b->max, &r);
688 c->openmin = (r || a->openmin || b->openmax);
690 c->max = div32(a->max, b->min, &r);
695 c->openmax = (a->openmax || b->openmin);
704 * snd_interval_muldivk - refine the interval value
707 * @k: divisor (as integer)
712 * Returns non-zero if the value is changed, zero if not changed.
714 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
715 unsigned int k, struct snd_interval *c)
718 if (a->empty || b->empty) {
719 snd_interval_none(c);
723 c->min = muldiv32(a->min, b->min, k, &r);
724 c->openmin = (r || a->openmin || b->openmin);
725 c->max = muldiv32(a->max, b->max, k, &r);
730 c->openmax = (a->openmax || b->openmax);
735 * snd_interval_mulkdiv - refine the interval value
737 * @k: dividend 2 (as integer)
743 * Returns non-zero if the value is changed, zero if not changed.
745 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
746 const struct snd_interval *b, struct snd_interval *c)
749 if (a->empty || b->empty) {
750 snd_interval_none(c);
754 c->min = muldiv32(a->min, k, b->max, &r);
755 c->openmin = (r || a->openmin || b->openmax);
757 c->max = muldiv32(a->max, k, b->min, &r);
762 c->openmax = (a->openmax || b->openmin);
774 * snd_interval_ratnum - refine the interval value
775 * @i: interval to refine
776 * @rats_count: number of ratnum_t
777 * @rats: ratnum_t array
778 * @nump: pointer to store the resultant numerator
779 * @denp: pointer to store the resultant denominator
781 * Return: Positive if the value is changed, zero if it's not changed, or a
782 * negative error code.
784 int snd_interval_ratnum(struct snd_interval *i,
785 unsigned int rats_count, const struct snd_ratnum *rats,
786 unsigned int *nump, unsigned int *denp)
788 unsigned int best_num, best_den;
791 struct snd_interval t;
793 unsigned int result_num, result_den;
796 best_num = best_den = best_diff = 0;
797 for (k = 0; k < rats_count; ++k) {
798 unsigned int num = rats[k].num;
800 unsigned int q = i->min;
804 den = div_up(num, q);
805 if (den < rats[k].den_min)
807 if (den > rats[k].den_max)
808 den = rats[k].den_max;
811 r = (den - rats[k].den_min) % rats[k].den_step;
815 diff = num - q * den;
819 diff * best_den < best_diff * den) {
829 t.min = div_down(best_num, best_den);
830 t.openmin = !!(best_num % best_den);
832 result_num = best_num;
833 result_diff = best_diff;
834 result_den = best_den;
835 best_num = best_den = best_diff = 0;
836 for (k = 0; k < rats_count; ++k) {
837 unsigned int num = rats[k].num;
839 unsigned int q = i->max;
845 den = div_down(num, q);
846 if (den > rats[k].den_max)
848 if (den < rats[k].den_min)
849 den = rats[k].den_min;
852 r = (den - rats[k].den_min) % rats[k].den_step;
854 den += rats[k].den_step - r;
856 diff = q * den - num;
860 diff * best_den < best_diff * den) {
870 t.max = div_up(best_num, best_den);
871 t.openmax = !!(best_num % best_den);
873 err = snd_interval_refine(i, &t);
877 if (snd_interval_single(i)) {
878 if (best_diff * result_den < result_diff * best_den) {
879 result_num = best_num;
880 result_den = best_den;
889 EXPORT_SYMBOL(snd_interval_ratnum);
892 * snd_interval_ratden - refine the interval value
893 * @i: interval to refine
894 * @rats_count: number of struct ratden
895 * @rats: struct ratden array
896 * @nump: pointer to store the resultant numerator
897 * @denp: pointer to store the resultant denominator
899 * Return: Positive if the value is changed, zero if it's not changed, or a
900 * negative error code.
902 static int snd_interval_ratden(struct snd_interval *i,
903 unsigned int rats_count,
904 const struct snd_ratden *rats,
905 unsigned int *nump, unsigned int *denp)
907 unsigned int best_num, best_diff, best_den;
909 struct snd_interval t;
912 best_num = best_den = best_diff = 0;
913 for (k = 0; k < rats_count; ++k) {
915 unsigned int den = rats[k].den;
916 unsigned int q = i->min;
919 if (num > rats[k].num_max)
921 if (num < rats[k].num_min)
922 num = rats[k].num_max;
925 r = (num - rats[k].num_min) % rats[k].num_step;
927 num += rats[k].num_step - r;
929 diff = num - q * den;
931 diff * best_den < best_diff * den) {
941 t.min = div_down(best_num, best_den);
942 t.openmin = !!(best_num % best_den);
944 best_num = best_den = best_diff = 0;
945 for (k = 0; k < rats_count; ++k) {
947 unsigned int den = rats[k].den;
948 unsigned int q = i->max;
951 if (num < rats[k].num_min)
953 if (num > rats[k].num_max)
954 num = rats[k].num_max;
957 r = (num - rats[k].num_min) % rats[k].num_step;
961 diff = q * den - num;
963 diff * best_den < best_diff * den) {
973 t.max = div_up(best_num, best_den);
974 t.openmax = !!(best_num % best_den);
976 err = snd_interval_refine(i, &t);
980 if (snd_interval_single(i)) {
990 * snd_interval_list - refine the interval value from the list
991 * @i: the interval value to refine
992 * @count: the number of elements in the list
993 * @list: the value list
994 * @mask: the bit-mask to evaluate
996 * Refines the interval value from the list.
997 * When mask is non-zero, only the elements corresponding to bit 1 are
1000 * Return: Positive if the value is changed, zero if it's not changed, or a
1001 * negative error code.
1003 int snd_interval_list(struct snd_interval *i, unsigned int count,
1004 const unsigned int *list, unsigned int mask)
1007 struct snd_interval list_range;
1013 snd_interval_any(&list_range);
1014 list_range.min = UINT_MAX;
1016 for (k = 0; k < count; k++) {
1017 if (mask && !(mask & (1 << k)))
1019 if (!snd_interval_test(i, list[k]))
1021 list_range.min = min(list_range.min, list[k]);
1022 list_range.max = max(list_range.max, list[k]);
1024 return snd_interval_refine(i, &list_range);
1026 EXPORT_SYMBOL(snd_interval_list);
1029 * snd_interval_ranges - refine the interval value from the list of ranges
1030 * @i: the interval value to refine
1031 * @count: the number of elements in the list of ranges
1032 * @ranges: the ranges list
1033 * @mask: the bit-mask to evaluate
1035 * Refines the interval value from the list of ranges.
1036 * When mask is non-zero, only the elements corresponding to bit 1 are
1039 * Return: Positive if the value is changed, zero if it's not changed, or a
1040 * negative error code.
1042 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1043 const struct snd_interval *ranges, unsigned int mask)
1046 struct snd_interval range_union;
1047 struct snd_interval range;
1050 snd_interval_none(i);
1053 snd_interval_any(&range_union);
1054 range_union.min = UINT_MAX;
1055 range_union.max = 0;
1056 for (k = 0; k < count; k++) {
1057 if (mask && !(mask & (1 << k)))
1059 snd_interval_copy(&range, &ranges[k]);
1060 if (snd_interval_refine(&range, i) < 0)
1062 if (snd_interval_empty(&range))
1065 if (range.min < range_union.min) {
1066 range_union.min = range.min;
1067 range_union.openmin = 1;
1069 if (range.min == range_union.min && !range.openmin)
1070 range_union.openmin = 0;
1071 if (range.max > range_union.max) {
1072 range_union.max = range.max;
1073 range_union.openmax = 1;
1075 if (range.max == range_union.max && !range.openmax)
1076 range_union.openmax = 0;
1078 return snd_interval_refine(i, &range_union);
1080 EXPORT_SYMBOL(snd_interval_ranges);
1082 static int snd_interval_step(struct snd_interval *i, unsigned int step)
1087 if (n != 0 || i->openmin) {
1093 if (n != 0 || i->openmax) {
1098 if (snd_interval_checkempty(i)) {
1105 /* Info constraints helpers */
1108 * snd_pcm_hw_rule_add - add the hw-constraint rule
1109 * @runtime: the pcm runtime instance
1110 * @cond: condition bits
1111 * @var: the variable to evaluate
1112 * @func: the evaluation function
1113 * @private: the private data pointer passed to function
1114 * @dep: the dependent variables
1116 * Return: Zero if successful, or a negative error code on failure.
1118 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1120 snd_pcm_hw_rule_func_t func, void *private,
1123 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1124 struct snd_pcm_hw_rule *c;
1127 va_start(args, dep);
1128 if (constrs->rules_num >= constrs->rules_all) {
1129 struct snd_pcm_hw_rule *new;
1130 unsigned int new_rules = constrs->rules_all + 16;
1131 new = krealloc(constrs->rules, new_rules * sizeof(*c),
1137 constrs->rules = new;
1138 constrs->rules_all = new_rules;
1140 c = &constrs->rules[constrs->rules_num];
1144 c->private = private;
1147 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1154 dep = va_arg(args, int);
1156 constrs->rules_num++;
1160 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1163 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1164 * @runtime: PCM runtime instance
1165 * @var: hw_params variable to apply the mask
1166 * @mask: the bitmap mask
1168 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1170 * Return: Zero if successful, or a negative error code on failure.
1172 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1175 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1176 struct snd_mask *maskp = constrs_mask(constrs, var);
1177 *maskp->bits &= mask;
1178 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1179 if (*maskp->bits == 0)
1185 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1186 * @runtime: PCM runtime instance
1187 * @var: hw_params variable to apply the mask
1188 * @mask: the 64bit bitmap mask
1190 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1192 * Return: Zero if successful, or a negative error code on failure.
1194 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1197 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1198 struct snd_mask *maskp = constrs_mask(constrs, var);
1199 maskp->bits[0] &= (u_int32_t)mask;
1200 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1201 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1202 if (! maskp->bits[0] && ! maskp->bits[1])
1206 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1209 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1210 * @runtime: PCM runtime instance
1211 * @var: hw_params variable to apply the integer constraint
1213 * Apply the constraint of integer to an interval parameter.
1215 * Return: Positive if the value is changed, zero if it's not changed, or a
1216 * negative error code.
1218 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1220 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1221 return snd_interval_setinteger(constrs_interval(constrs, var));
1223 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1226 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1227 * @runtime: PCM runtime instance
1228 * @var: hw_params variable to apply the range
1229 * @min: the minimal value
1230 * @max: the maximal value
1232 * Apply the min/max range constraint to an interval parameter.
1234 * Return: Positive if the value is changed, zero if it's not changed, or a
1235 * negative error code.
1237 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1238 unsigned int min, unsigned int max)
1240 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1241 struct snd_interval t;
1244 t.openmin = t.openmax = 0;
1246 return snd_interval_refine(constrs_interval(constrs, var), &t);
1248 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1250 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1251 struct snd_pcm_hw_rule *rule)
1253 struct snd_pcm_hw_constraint_list *list = rule->private;
1254 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1259 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1260 * @runtime: PCM runtime instance
1261 * @cond: condition bits
1262 * @var: hw_params variable to apply the list constraint
1265 * Apply the list of constraints to an interval parameter.
1267 * Return: Zero if successful, or a negative error code on failure.
1269 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1271 snd_pcm_hw_param_t var,
1272 const struct snd_pcm_hw_constraint_list *l)
1274 return snd_pcm_hw_rule_add(runtime, cond, var,
1275 snd_pcm_hw_rule_list, (void *)l,
1278 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1280 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1281 struct snd_pcm_hw_rule *rule)
1283 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1284 return snd_interval_ranges(hw_param_interval(params, rule->var),
1285 r->count, r->ranges, r->mask);
1290 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1291 * @runtime: PCM runtime instance
1292 * @cond: condition bits
1293 * @var: hw_params variable to apply the list of range constraints
1296 * Apply the list of range constraints to an interval parameter.
1298 * Return: Zero if successful, or a negative error code on failure.
1300 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1302 snd_pcm_hw_param_t var,
1303 const struct snd_pcm_hw_constraint_ranges *r)
1305 return snd_pcm_hw_rule_add(runtime, cond, var,
1306 snd_pcm_hw_rule_ranges, (void *)r,
1309 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1311 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1312 struct snd_pcm_hw_rule *rule)
1314 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1315 unsigned int num = 0, den = 0;
1317 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1318 r->nrats, r->rats, &num, &den);
1319 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1320 params->rate_num = num;
1321 params->rate_den = den;
1327 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1328 * @runtime: PCM runtime instance
1329 * @cond: condition bits
1330 * @var: hw_params variable to apply the ratnums constraint
1331 * @r: struct snd_ratnums constriants
1333 * Return: Zero if successful, or a negative error code on failure.
1335 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1337 snd_pcm_hw_param_t var,
1338 const struct snd_pcm_hw_constraint_ratnums *r)
1340 return snd_pcm_hw_rule_add(runtime, cond, var,
1341 snd_pcm_hw_rule_ratnums, (void *)r,
1344 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1346 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1347 struct snd_pcm_hw_rule *rule)
1349 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1350 unsigned int num = 0, den = 0;
1351 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1352 r->nrats, r->rats, &num, &den);
1353 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1354 params->rate_num = num;
1355 params->rate_den = den;
1361 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1362 * @runtime: PCM runtime instance
1363 * @cond: condition bits
1364 * @var: hw_params variable to apply the ratdens constraint
1365 * @r: struct snd_ratdens constriants
1367 * Return: Zero if successful, or a negative error code on failure.
1369 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1371 snd_pcm_hw_param_t var,
1372 const struct snd_pcm_hw_constraint_ratdens *r)
1374 return snd_pcm_hw_rule_add(runtime, cond, var,
1375 snd_pcm_hw_rule_ratdens, (void *)r,
1378 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1380 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1381 struct snd_pcm_hw_rule *rule)
1383 unsigned int l = (unsigned long) rule->private;
1384 int width = l & 0xffff;
1385 unsigned int msbits = l >> 16;
1386 const struct snd_interval *i =
1387 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1389 if (!snd_interval_single(i))
1392 if ((snd_interval_value(i) == width) ||
1393 (width == 0 && snd_interval_value(i) > msbits))
1394 params->msbits = min_not_zero(params->msbits, msbits);
1400 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1401 * @runtime: PCM runtime instance
1402 * @cond: condition bits
1403 * @width: sample bits width
1404 * @msbits: msbits width
1406 * This constraint will set the number of most significant bits (msbits) if a
1407 * sample format with the specified width has been select. If width is set to 0
1408 * the msbits will be set for any sample format with a width larger than the
1411 * Return: Zero if successful, or a negative error code on failure.
1413 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1416 unsigned int msbits)
1418 unsigned long l = (msbits << 16) | width;
1419 return snd_pcm_hw_rule_add(runtime, cond, -1,
1420 snd_pcm_hw_rule_msbits,
1422 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1424 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1426 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1427 struct snd_pcm_hw_rule *rule)
1429 unsigned long step = (unsigned long) rule->private;
1430 return snd_interval_step(hw_param_interval(params, rule->var), step);
1434 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1435 * @runtime: PCM runtime instance
1436 * @cond: condition bits
1437 * @var: hw_params variable to apply the step constraint
1440 * Return: Zero if successful, or a negative error code on failure.
1442 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1444 snd_pcm_hw_param_t var,
1447 return snd_pcm_hw_rule_add(runtime, cond, var,
1448 snd_pcm_hw_rule_step, (void *) step,
1451 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1453 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1455 static const unsigned int pow2_sizes[] = {
1456 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1457 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1458 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1459 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1461 return snd_interval_list(hw_param_interval(params, rule->var),
1462 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1466 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1467 * @runtime: PCM runtime instance
1468 * @cond: condition bits
1469 * @var: hw_params variable to apply the power-of-2 constraint
1471 * Return: Zero if successful, or a negative error code on failure.
1473 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1475 snd_pcm_hw_param_t var)
1477 return snd_pcm_hw_rule_add(runtime, cond, var,
1478 snd_pcm_hw_rule_pow2, NULL,
1481 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1483 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1484 struct snd_pcm_hw_rule *rule)
1486 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1487 struct snd_interval *rate;
1489 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1490 return snd_interval_list(rate, 1, &base_rate, 0);
1494 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1495 * @runtime: PCM runtime instance
1496 * @base_rate: the rate at which the hardware does not resample
1498 * Return: Zero if successful, or a negative error code on failure.
1500 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1501 unsigned int base_rate)
1503 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1504 SNDRV_PCM_HW_PARAM_RATE,
1505 snd_pcm_hw_rule_noresample_func,
1506 (void *)(uintptr_t)base_rate,
1507 SNDRV_PCM_HW_PARAM_RATE, -1);
1509 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1511 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1512 snd_pcm_hw_param_t var)
1514 if (hw_is_mask(var)) {
1515 snd_mask_any(hw_param_mask(params, var));
1516 params->cmask |= 1 << var;
1517 params->rmask |= 1 << var;
1520 if (hw_is_interval(var)) {
1521 snd_interval_any(hw_param_interval(params, var));
1522 params->cmask |= 1 << var;
1523 params->rmask |= 1 << var;
1529 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1532 memset(params, 0, sizeof(*params));
1533 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1534 _snd_pcm_hw_param_any(params, k);
1535 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1536 _snd_pcm_hw_param_any(params, k);
1539 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1542 * snd_pcm_hw_param_value - return @params field @var value
1543 * @params: the hw_params instance
1544 * @var: parameter to retrieve
1545 * @dir: pointer to the direction (-1,0,1) or %NULL
1547 * Return: The value for field @var if it's fixed in configuration space
1548 * defined by @params. -%EINVAL otherwise.
1550 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1551 snd_pcm_hw_param_t var, int *dir)
1553 if (hw_is_mask(var)) {
1554 const struct snd_mask *mask = hw_param_mask_c(params, var);
1555 if (!snd_mask_single(mask))
1559 return snd_mask_value(mask);
1561 if (hw_is_interval(var)) {
1562 const struct snd_interval *i = hw_param_interval_c(params, var);
1563 if (!snd_interval_single(i))
1567 return snd_interval_value(i);
1571 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1573 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1574 snd_pcm_hw_param_t var)
1576 if (hw_is_mask(var)) {
1577 snd_mask_none(hw_param_mask(params, var));
1578 params->cmask |= 1 << var;
1579 params->rmask |= 1 << var;
1580 } else if (hw_is_interval(var)) {
1581 snd_interval_none(hw_param_interval(params, var));
1582 params->cmask |= 1 << var;
1583 params->rmask |= 1 << var;
1588 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1590 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1591 snd_pcm_hw_param_t var)
1594 if (hw_is_mask(var))
1595 changed = snd_mask_refine_first(hw_param_mask(params, var));
1596 else if (hw_is_interval(var))
1597 changed = snd_interval_refine_first(hw_param_interval(params, var));
1601 params->cmask |= 1 << var;
1602 params->rmask |= 1 << var;
1609 * snd_pcm_hw_param_first - refine config space and return minimum value
1610 * @pcm: PCM instance
1611 * @params: the hw_params instance
1612 * @var: parameter to retrieve
1613 * @dir: pointer to the direction (-1,0,1) or %NULL
1615 * Inside configuration space defined by @params remove from @var all
1616 * values > minimum. Reduce configuration space accordingly.
1618 * Return: The minimum, or a negative error code on failure.
1620 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1621 struct snd_pcm_hw_params *params,
1622 snd_pcm_hw_param_t var, int *dir)
1624 int changed = _snd_pcm_hw_param_first(params, var);
1627 if (params->rmask) {
1628 int err = snd_pcm_hw_refine(pcm, params);
1632 return snd_pcm_hw_param_value(params, var, dir);
1634 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1636 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1637 snd_pcm_hw_param_t var)
1640 if (hw_is_mask(var))
1641 changed = snd_mask_refine_last(hw_param_mask(params, var));
1642 else if (hw_is_interval(var))
1643 changed = snd_interval_refine_last(hw_param_interval(params, var));
1647 params->cmask |= 1 << var;
1648 params->rmask |= 1 << var;
1655 * snd_pcm_hw_param_last - refine config space and return maximum value
1656 * @pcm: PCM instance
1657 * @params: the hw_params instance
1658 * @var: parameter to retrieve
1659 * @dir: pointer to the direction (-1,0,1) or %NULL
1661 * Inside configuration space defined by @params remove from @var all
1662 * values < maximum. Reduce configuration space accordingly.
1664 * Return: The maximum, or a negative error code on failure.
1666 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1667 struct snd_pcm_hw_params *params,
1668 snd_pcm_hw_param_t var, int *dir)
1670 int changed = _snd_pcm_hw_param_last(params, var);
1673 if (params->rmask) {
1674 int err = snd_pcm_hw_refine(pcm, params);
1678 return snd_pcm_hw_param_value(params, var, dir);
1680 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1682 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1685 struct snd_pcm_runtime *runtime = substream->runtime;
1686 unsigned long flags;
1687 snd_pcm_stream_lock_irqsave(substream, flags);
1688 if (snd_pcm_running(substream) &&
1689 snd_pcm_update_hw_ptr(substream) >= 0)
1690 runtime->status->hw_ptr %= runtime->buffer_size;
1692 runtime->status->hw_ptr = 0;
1693 runtime->hw_ptr_wrap = 0;
1695 snd_pcm_stream_unlock_irqrestore(substream, flags);
1699 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1702 struct snd_pcm_channel_info *info = arg;
1703 struct snd_pcm_runtime *runtime = substream->runtime;
1705 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1709 width = snd_pcm_format_physical_width(runtime->format);
1713 switch (runtime->access) {
1714 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1715 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1716 info->first = info->channel * width;
1717 info->step = runtime->channels * width;
1719 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1720 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1722 size_t size = runtime->dma_bytes / runtime->channels;
1723 info->first = info->channel * size * 8;
1734 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1737 struct snd_pcm_hw_params *params = arg;
1738 snd_pcm_format_t format;
1742 params->fifo_size = substream->runtime->hw.fifo_size;
1743 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1744 format = params_format(params);
1745 channels = params_channels(params);
1746 frame_size = snd_pcm_format_size(format, channels);
1748 params->fifo_size /= (unsigned)frame_size;
1754 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1755 * @substream: the pcm substream instance
1756 * @cmd: ioctl command
1757 * @arg: ioctl argument
1759 * Processes the generic ioctl commands for PCM.
1760 * Can be passed as the ioctl callback for PCM ops.
1762 * Return: Zero if successful, or a negative error code on failure.
1764 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1765 unsigned int cmd, void *arg)
1768 case SNDRV_PCM_IOCTL1_RESET:
1769 return snd_pcm_lib_ioctl_reset(substream, arg);
1770 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1771 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1772 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1773 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1777 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1780 * snd_pcm_period_elapsed - update the pcm status for the next period
1781 * @substream: the pcm substream instance
1783 * This function is called from the interrupt handler when the
1784 * PCM has processed the period size. It will update the current
1785 * pointer, wake up sleepers, etc.
1787 * Even if more than one periods have elapsed since the last call, you
1788 * have to call this only once.
1790 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1792 struct snd_pcm_runtime *runtime;
1793 unsigned long flags;
1795 if (snd_BUG_ON(!substream))
1798 snd_pcm_stream_lock_irqsave(substream, flags);
1799 if (PCM_RUNTIME_CHECK(substream))
1801 runtime = substream->runtime;
1803 if (!snd_pcm_running(substream) ||
1804 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1807 #ifdef CONFIG_SND_PCM_TIMER
1808 if (substream->timer_running)
1809 snd_timer_interrupt(substream->timer, 1);
1812 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1814 snd_pcm_stream_unlock_irqrestore(substream, flags);
1816 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1819 * Wait until avail_min data becomes available
1820 * Returns a negative error code if any error occurs during operation.
1821 * The available space is stored on availp. When err = 0 and avail = 0
1822 * on the capture stream, it indicates the stream is in DRAINING state.
1824 static int wait_for_avail(struct snd_pcm_substream *substream,
1825 snd_pcm_uframes_t *availp)
1827 struct snd_pcm_runtime *runtime = substream->runtime;
1828 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1829 wait_queue_entry_t wait;
1831 snd_pcm_uframes_t avail = 0;
1832 long wait_time, tout;
1834 init_waitqueue_entry(&wait, current);
1835 set_current_state(TASK_INTERRUPTIBLE);
1836 add_wait_queue(&runtime->tsleep, &wait);
1838 if (runtime->no_period_wakeup)
1839 wait_time = MAX_SCHEDULE_TIMEOUT;
1841 /* use wait time from substream if available */
1842 if (substream->wait_time) {
1843 wait_time = substream->wait_time;
1847 if (runtime->rate) {
1848 long t = runtime->period_size * 2 /
1850 wait_time = max(t, wait_time);
1852 wait_time = msecs_to_jiffies(wait_time * 1000);
1857 if (signal_pending(current)) {
1863 * We need to check if space became available already
1864 * (and thus the wakeup happened already) first to close
1865 * the race of space already having become available.
1866 * This check must happen after been added to the waitqueue
1867 * and having current state be INTERRUPTIBLE.
1869 avail = snd_pcm_avail(substream);
1870 if (avail >= runtime->twake)
1872 snd_pcm_stream_unlock_irq(substream);
1874 tout = schedule_timeout(wait_time);
1876 snd_pcm_stream_lock_irq(substream);
1877 set_current_state(TASK_INTERRUPTIBLE);
1878 switch (runtime->status->state) {
1879 case SNDRV_PCM_STATE_SUSPENDED:
1882 case SNDRV_PCM_STATE_XRUN:
1885 case SNDRV_PCM_STATE_DRAINING:
1889 avail = 0; /* indicate draining */
1891 case SNDRV_PCM_STATE_OPEN:
1892 case SNDRV_PCM_STATE_SETUP:
1893 case SNDRV_PCM_STATE_DISCONNECTED:
1896 case SNDRV_PCM_STATE_PAUSED:
1900 pcm_dbg(substream->pcm,
1901 "%s write error (DMA or IRQ trouble?)\n",
1902 is_playback ? "playback" : "capture");
1908 set_current_state(TASK_RUNNING);
1909 remove_wait_queue(&runtime->tsleep, &wait);
1914 typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
1915 int channel, unsigned long hwoff,
1916 void *buf, unsigned long bytes);
1918 typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
1919 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
1921 /* calculate the target DMA-buffer position to be written/read */
1922 static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
1923 int channel, unsigned long hwoff)
1925 return runtime->dma_area + hwoff +
1926 channel * (runtime->dma_bytes / runtime->channels);
1929 /* default copy_user ops for write; used for both interleaved and non- modes */
1930 static int default_write_copy(struct snd_pcm_substream *substream,
1931 int channel, unsigned long hwoff,
1932 void *buf, unsigned long bytes)
1934 if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
1935 (void __user *)buf, bytes))
1940 /* default copy_kernel ops for write */
1941 static int default_write_copy_kernel(struct snd_pcm_substream *substream,
1942 int channel, unsigned long hwoff,
1943 void *buf, unsigned long bytes)
1945 memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
1949 /* fill silence instead of copy data; called as a transfer helper
1950 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
1951 * a NULL buffer is passed
1953 static int fill_silence(struct snd_pcm_substream *substream, int channel,
1954 unsigned long hwoff, void *buf, unsigned long bytes)
1956 struct snd_pcm_runtime *runtime = substream->runtime;
1958 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
1960 if (substream->ops->fill_silence)
1961 return substream->ops->fill_silence(substream, channel,
1964 snd_pcm_format_set_silence(runtime->format,
1965 get_dma_ptr(runtime, channel, hwoff),
1966 bytes_to_samples(runtime, bytes));
1970 /* default copy_user ops for read; used for both interleaved and non- modes */
1971 static int default_read_copy(struct snd_pcm_substream *substream,
1972 int channel, unsigned long hwoff,
1973 void *buf, unsigned long bytes)
1975 if (copy_to_user((void __user *)buf,
1976 get_dma_ptr(substream->runtime, channel, hwoff),
1982 /* default copy_kernel ops for read */
1983 static int default_read_copy_kernel(struct snd_pcm_substream *substream,
1984 int channel, unsigned long hwoff,
1985 void *buf, unsigned long bytes)
1987 memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
1991 /* call transfer function with the converted pointers and sizes;
1992 * for interleaved mode, it's one shot for all samples
1994 static int interleaved_copy(struct snd_pcm_substream *substream,
1995 snd_pcm_uframes_t hwoff, void *data,
1996 snd_pcm_uframes_t off,
1997 snd_pcm_uframes_t frames,
1998 pcm_transfer_f transfer)
2000 struct snd_pcm_runtime *runtime = substream->runtime;
2002 /* convert to bytes */
2003 hwoff = frames_to_bytes(runtime, hwoff);
2004 off = frames_to_bytes(runtime, off);
2005 frames = frames_to_bytes(runtime, frames);
2006 return transfer(substream, 0, hwoff, data + off, frames);
2009 /* call transfer function with the converted pointers and sizes for each
2010 * non-interleaved channel; when buffer is NULL, silencing instead of copying
2012 static int noninterleaved_copy(struct snd_pcm_substream *substream,
2013 snd_pcm_uframes_t hwoff, void *data,
2014 snd_pcm_uframes_t off,
2015 snd_pcm_uframes_t frames,
2016 pcm_transfer_f transfer)
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 int channels = runtime->channels;
2023 /* convert to bytes; note that it's not frames_to_bytes() here.
2024 * in non-interleaved mode, we copy for each channel, thus
2025 * each copy is n_samples bytes x channels = whole frames.
2027 off = samples_to_bytes(runtime, off);
2028 frames = samples_to_bytes(runtime, frames);
2029 hwoff = samples_to_bytes(runtime, hwoff);
2030 for (c = 0; c < channels; ++c, ++bufs) {
2031 if (!data || !*bufs)
2032 err = fill_silence(substream, c, hwoff, NULL, frames);
2034 err = transfer(substream, c, hwoff, *bufs + off,
2042 /* fill silence on the given buffer position;
2043 * called from snd_pcm_playback_silence()
2045 static int fill_silence_frames(struct snd_pcm_substream *substream,
2046 snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
2048 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2049 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
2050 return interleaved_copy(substream, off, NULL, 0, frames,
2053 return noninterleaved_copy(substream, off, NULL, 0, frames,
2057 /* sanity-check for read/write methods */
2058 static int pcm_sanity_check(struct snd_pcm_substream *substream)
2060 struct snd_pcm_runtime *runtime;
2061 if (PCM_RUNTIME_CHECK(substream))
2063 runtime = substream->runtime;
2064 if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
2066 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2071 static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
2073 switch (runtime->status->state) {
2074 case SNDRV_PCM_STATE_PREPARED:
2075 case SNDRV_PCM_STATE_RUNNING:
2076 case SNDRV_PCM_STATE_PAUSED:
2078 case SNDRV_PCM_STATE_XRUN:
2080 case SNDRV_PCM_STATE_SUSPENDED:
2087 /* update to the given appl_ptr and call ack callback if needed;
2088 * when an error is returned, take back to the original value
2090 int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
2091 snd_pcm_uframes_t appl_ptr)
2093 struct snd_pcm_runtime *runtime = substream->runtime;
2094 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
2097 if (old_appl_ptr == appl_ptr)
2100 runtime->control->appl_ptr = appl_ptr;
2101 if (substream->ops->ack) {
2102 ret = substream->ops->ack(substream);
2104 runtime->control->appl_ptr = old_appl_ptr;
2109 trace_applptr(substream, old_appl_ptr, appl_ptr);
2114 /* the common loop for read/write data */
2115 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
2116 void *data, bool interleaved,
2117 snd_pcm_uframes_t size, bool in_kernel)
2119 struct snd_pcm_runtime *runtime = substream->runtime;
2120 snd_pcm_uframes_t xfer = 0;
2121 snd_pcm_uframes_t offset = 0;
2122 snd_pcm_uframes_t avail;
2124 pcm_transfer_f transfer;
2129 err = pcm_sanity_check(substream);
2133 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2135 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2136 runtime->channels > 1)
2138 writer = interleaved_copy;
2140 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2142 writer = noninterleaved_copy;
2147 transfer = fill_silence;
2150 } else if (in_kernel) {
2151 if (substream->ops->copy_kernel)
2152 transfer = substream->ops->copy_kernel;
2154 transfer = is_playback ?
2155 default_write_copy_kernel : default_read_copy_kernel;
2157 if (substream->ops->copy_user)
2158 transfer = (pcm_transfer_f)substream->ops->copy_user;
2160 transfer = is_playback ?
2161 default_write_copy : default_read_copy;
2167 nonblock = !!(substream->f_flags & O_NONBLOCK);
2169 snd_pcm_stream_lock_irq(substream);
2170 err = pcm_accessible_state(runtime);
2174 runtime->twake = runtime->control->avail_min ? : 1;
2175 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2176 snd_pcm_update_hw_ptr(substream);
2179 * If size < start_threshold, wait indefinitely. Another
2180 * thread may start capture
2183 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2184 size >= runtime->start_threshold) {
2185 err = snd_pcm_start(substream);
2190 avail = snd_pcm_avail(substream);
2193 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2194 snd_pcm_uframes_t cont;
2197 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2198 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2205 runtime->twake = min_t(snd_pcm_uframes_t, size,
2206 runtime->control->avail_min ? : 1);
2207 err = wait_for_avail(substream, &avail);
2211 continue; /* draining */
2213 frames = size > avail ? avail : size;
2214 appl_ptr = READ_ONCE(runtime->control->appl_ptr);
2215 appl_ofs = appl_ptr % runtime->buffer_size;
2216 cont = runtime->buffer_size - appl_ofs;
2219 if (snd_BUG_ON(!frames)) {
2223 snd_pcm_stream_unlock_irq(substream);
2224 err = writer(substream, appl_ofs, data, offset, frames,
2226 snd_pcm_stream_lock_irq(substream);
2229 err = pcm_accessible_state(runtime);
2233 if (appl_ptr >= runtime->boundary)
2234 appl_ptr -= runtime->boundary;
2235 err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2244 runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2245 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2246 err = snd_pcm_start(substream);
2253 if (xfer > 0 && err >= 0)
2254 snd_pcm_update_state(substream, runtime);
2255 snd_pcm_stream_unlock_irq(substream);
2256 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2258 EXPORT_SYMBOL(__snd_pcm_lib_xfer);
2261 * standard channel mapping helpers
2264 /* default channel maps for multi-channel playbacks, up to 8 channels */
2265 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2267 .map = { SNDRV_CHMAP_MONO } },
2269 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2271 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2272 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2274 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2275 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2276 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2278 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2279 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2280 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2281 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2284 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2286 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2287 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2289 .map = { SNDRV_CHMAP_MONO } },
2291 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2293 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2294 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2296 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2297 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2298 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2300 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2301 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2302 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2303 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2306 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2308 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2310 if (ch > info->max_channels)
2312 return !info->channel_mask || (info->channel_mask & (1U << ch));
2315 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_info *uinfo)
2318 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2320 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2321 uinfo->count = info->max_channels;
2322 uinfo->value.integer.min = 0;
2323 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2327 /* get callback for channel map ctl element
2328 * stores the channel position firstly matching with the current channels
2330 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2333 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2334 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2335 struct snd_pcm_substream *substream;
2336 const struct snd_pcm_chmap_elem *map;
2340 substream = snd_pcm_chmap_substream(info, idx);
2343 memset(ucontrol->value.integer.value, 0,
2344 sizeof(long) * info->max_channels);
2345 if (!substream->runtime)
2346 return 0; /* no channels set */
2347 for (map = info->chmap; map->channels; map++) {
2349 if (map->channels == substream->runtime->channels &&
2350 valid_chmap_channels(info, map->channels)) {
2351 for (i = 0; i < map->channels; i++)
2352 ucontrol->value.integer.value[i] = map->map[i];
2359 /* tlv callback for channel map ctl element
2360 * expands the pre-defined channel maps in a form of TLV
2362 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2363 unsigned int size, unsigned int __user *tlv)
2365 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2366 const struct snd_pcm_chmap_elem *map;
2367 unsigned int __user *dst;
2374 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2378 for (map = info->chmap; map->channels; map++) {
2379 int chs_bytes = map->channels * 4;
2380 if (!valid_chmap_channels(info, map->channels))
2384 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2385 put_user(chs_bytes, dst + 1))
2390 if (size < chs_bytes)
2394 for (c = 0; c < map->channels; c++) {
2395 if (put_user(map->map[c], dst))
2400 if (put_user(count, tlv + 1))
2405 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2407 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2408 info->pcm->streams[info->stream].chmap_kctl = NULL;
2413 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2414 * @pcm: the assigned PCM instance
2415 * @stream: stream direction
2416 * @chmap: channel map elements (for query)
2417 * @max_channels: the max number of channels for the stream
2418 * @private_value: the value passed to each kcontrol's private_value field
2419 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2421 * Create channel-mapping control elements assigned to the given PCM stream(s).
2422 * Return: Zero if successful, or a negative error value.
2424 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2425 const struct snd_pcm_chmap_elem *chmap,
2427 unsigned long private_value,
2428 struct snd_pcm_chmap **info_ret)
2430 struct snd_pcm_chmap *info;
2431 struct snd_kcontrol_new knew = {
2432 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2433 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2434 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2435 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2436 .info = pcm_chmap_ctl_info,
2437 .get = pcm_chmap_ctl_get,
2438 .tlv.c = pcm_chmap_ctl_tlv,
2442 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2444 info = kzalloc(sizeof(*info), GFP_KERNEL);
2448 info->stream = stream;
2449 info->chmap = chmap;
2450 info->max_channels = max_channels;
2451 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2452 knew.name = "Playback Channel Map";
2454 knew.name = "Capture Channel Map";
2455 knew.device = pcm->device;
2456 knew.count = pcm->streams[stream].substream_count;
2457 knew.private_value = private_value;
2458 info->kctl = snd_ctl_new1(&knew, info);
2463 info->kctl->private_free = pcm_chmap_ctl_private_free;
2464 err = snd_ctl_add(pcm->card, info->kctl);
2467 pcm->streams[stream].chmap_kctl = info->kctl;
2472 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);