2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/slab.h>
24 #include <linux/sched/signal.h>
25 #include <linux/time.h>
26 #include <linux/math64.h>
27 #include <linux/export.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/tlv.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
36 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
37 #define CREATE_TRACE_POINTS
38 #include "pcm_trace.h"
40 #define trace_hwptr(substream, pos, in_interrupt)
41 #define trace_xrun(substream)
42 #define trace_hw_ptr_error(substream, reason)
46 * fill ring buffer with silence
47 * runtime->silence_start: starting pointer to silence area
48 * runtime->silence_filled: size filled with silence
49 * runtime->silence_threshold: threshold from application
50 * runtime->silence_size: maximal size from application
52 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
54 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
56 struct snd_pcm_runtime *runtime = substream->runtime;
57 snd_pcm_uframes_t frames, ofs, transfer;
59 if (runtime->silence_size < runtime->boundary) {
60 snd_pcm_sframes_t noise_dist, n;
61 if (runtime->silence_start != runtime->control->appl_ptr) {
62 n = runtime->control->appl_ptr - runtime->silence_start;
64 n += runtime->boundary;
65 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
66 runtime->silence_filled -= n;
68 runtime->silence_filled = 0;
69 runtime->silence_start = runtime->control->appl_ptr;
71 if (runtime->silence_filled >= runtime->buffer_size)
73 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
74 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
76 frames = runtime->silence_threshold - noise_dist;
77 if (frames > runtime->silence_size)
78 frames = runtime->silence_size;
80 if (new_hw_ptr == ULONG_MAX) { /* initialization */
81 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
82 if (avail > runtime->buffer_size)
83 avail = runtime->buffer_size;
84 runtime->silence_filled = avail > 0 ? avail : 0;
85 runtime->silence_start = (runtime->status->hw_ptr +
86 runtime->silence_filled) %
89 ofs = runtime->status->hw_ptr;
90 frames = new_hw_ptr - ofs;
91 if ((snd_pcm_sframes_t)frames < 0)
92 frames += runtime->boundary;
93 runtime->silence_filled -= frames;
94 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
95 runtime->silence_filled = 0;
96 runtime->silence_start = new_hw_ptr;
98 runtime->silence_start = ofs;
101 frames = runtime->buffer_size - runtime->silence_filled;
103 if (snd_BUG_ON(frames > runtime->buffer_size))
107 ofs = runtime->silence_start % runtime->buffer_size;
109 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
110 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
111 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
112 if (substream->ops->silence) {
114 err = substream->ops->silence(substream, -1, ofs, transfer);
117 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
122 unsigned int channels = runtime->channels;
123 if (substream->ops->silence) {
124 for (c = 0; c < channels; ++c) {
126 err = substream->ops->silence(substream, c, ofs, transfer);
130 size_t dma_csize = runtime->dma_bytes / channels;
131 for (c = 0; c < channels; ++c) {
132 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
133 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
137 runtime->silence_filled += transfer;
143 #ifdef CONFIG_SND_DEBUG
144 void snd_pcm_debug_name(struct snd_pcm_substream *substream,
145 char *name, size_t len)
147 snprintf(name, len, "pcmC%dD%d%c:%d",
148 substream->pcm->card->number,
149 substream->pcm->device,
150 substream->stream ? 'c' : 'p',
153 EXPORT_SYMBOL(snd_pcm_debug_name);
156 #define XRUN_DEBUG_BASIC (1<<0)
157 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
158 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
160 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
162 #define xrun_debug(substream, mask) \
163 ((substream)->pstr->xrun_debug & (mask))
165 #define xrun_debug(substream, mask) 0
168 #define dump_stack_on_xrun(substream) do { \
169 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
173 static void xrun(struct snd_pcm_substream *substream)
175 struct snd_pcm_runtime *runtime = substream->runtime;
177 trace_xrun(substream);
178 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
179 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
180 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
181 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
183 snd_pcm_debug_name(substream, name, sizeof(name));
184 pcm_warn(substream->pcm, "XRUN: %s\n", name);
185 dump_stack_on_xrun(substream);
189 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
190 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
192 trace_hw_ptr_error(substream, reason); \
193 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
194 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
195 (in_interrupt) ? 'Q' : 'P', ##args); \
196 dump_stack_on_xrun(substream); \
200 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
202 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
206 int snd_pcm_update_state(struct snd_pcm_substream *substream,
207 struct snd_pcm_runtime *runtime)
209 snd_pcm_uframes_t avail;
211 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
212 avail = snd_pcm_playback_avail(runtime);
214 avail = snd_pcm_capture_avail(runtime);
215 if (avail > runtime->avail_max)
216 runtime->avail_max = avail;
217 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
218 if (avail >= runtime->buffer_size) {
219 snd_pcm_drain_done(substream);
223 if (avail >= runtime->stop_threshold) {
228 if (runtime->twake) {
229 if (avail >= runtime->twake)
230 wake_up(&runtime->tsleep);
231 } else if (avail >= runtime->control->avail_min)
232 wake_up(&runtime->sleep);
236 static void update_audio_tstamp(struct snd_pcm_substream *substream,
237 struct timespec *curr_tstamp,
238 struct timespec *audio_tstamp)
240 struct snd_pcm_runtime *runtime = substream->runtime;
241 u64 audio_frames, audio_nsecs;
242 struct timespec driver_tstamp;
244 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
247 if (!(substream->ops->get_time_info) ||
248 (runtime->audio_tstamp_report.actual_type ==
249 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
252 * provide audio timestamp derived from pointer position
253 * add delay only if requested
256 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
258 if (runtime->audio_tstamp_config.report_delay) {
259 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
260 audio_frames -= runtime->delay;
262 audio_frames += runtime->delay;
264 audio_nsecs = div_u64(audio_frames * 1000000000LL,
266 *audio_tstamp = ns_to_timespec(audio_nsecs);
268 runtime->status->audio_tstamp = *audio_tstamp;
269 runtime->status->tstamp = *curr_tstamp;
272 * re-take a driver timestamp to let apps detect if the reference tstamp
273 * read by low-level hardware was provided with a delay
275 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
276 runtime->driver_tstamp = driver_tstamp;
279 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
280 unsigned int in_interrupt)
282 struct snd_pcm_runtime *runtime = substream->runtime;
283 snd_pcm_uframes_t pos;
284 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
285 snd_pcm_sframes_t hdelta, delta;
286 unsigned long jdelta;
287 unsigned long curr_jiffies;
288 struct timespec curr_tstamp;
289 struct timespec audio_tstamp;
290 int crossed_boundary = 0;
292 old_hw_ptr = runtime->status->hw_ptr;
295 * group pointer, time and jiffies reads to allow for more
296 * accurate correlations/corrections.
297 * The values are stored at the end of this routine after
298 * corrections for hw_ptr position
300 pos = substream->ops->pointer(substream);
301 curr_jiffies = jiffies;
302 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
303 if ((substream->ops->get_time_info) &&
304 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
305 substream->ops->get_time_info(substream, &curr_tstamp,
307 &runtime->audio_tstamp_config,
308 &runtime->audio_tstamp_report);
310 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
311 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
312 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
314 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
317 if (pos == SNDRV_PCM_POS_XRUN) {
321 if (pos >= runtime->buffer_size) {
322 if (printk_ratelimit()) {
324 snd_pcm_debug_name(substream, name, sizeof(name));
325 pcm_err(substream->pcm,
326 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
327 name, pos, runtime->buffer_size,
328 runtime->period_size);
332 pos -= pos % runtime->min_align;
333 trace_hwptr(substream, pos, in_interrupt);
334 hw_base = runtime->hw_ptr_base;
335 new_hw_ptr = hw_base + pos;
337 /* we know that one period was processed */
338 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
339 delta = runtime->hw_ptr_interrupt + runtime->period_size;
340 if (delta > new_hw_ptr) {
341 /* check for double acknowledged interrupts */
342 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
343 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
344 hw_base += runtime->buffer_size;
345 if (hw_base >= runtime->boundary) {
349 new_hw_ptr = hw_base + pos;
354 /* new_hw_ptr might be lower than old_hw_ptr in case when */
355 /* pointer crosses the end of the ring buffer */
356 if (new_hw_ptr < old_hw_ptr) {
357 hw_base += runtime->buffer_size;
358 if (hw_base >= runtime->boundary) {
362 new_hw_ptr = hw_base + pos;
365 delta = new_hw_ptr - old_hw_ptr;
367 delta += runtime->boundary;
369 if (runtime->no_period_wakeup) {
370 snd_pcm_sframes_t xrun_threshold;
372 * Without regular period interrupts, we have to check
373 * the elapsed time to detect xruns.
375 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
376 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
378 hdelta = jdelta - delta * HZ / runtime->rate;
379 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
380 while (hdelta > xrun_threshold) {
381 delta += runtime->buffer_size;
382 hw_base += runtime->buffer_size;
383 if (hw_base >= runtime->boundary) {
387 new_hw_ptr = hw_base + pos;
388 hdelta -= runtime->hw_ptr_buffer_jiffies;
393 /* something must be really wrong */
394 if (delta >= runtime->buffer_size + runtime->period_size) {
395 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
396 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
397 substream->stream, (long)pos,
398 (long)new_hw_ptr, (long)old_hw_ptr);
402 /* Do jiffies check only in xrun_debug mode */
403 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
404 goto no_jiffies_check;
406 /* Skip the jiffies check for hardwares with BATCH flag.
407 * Such hardware usually just increases the position at each IRQ,
408 * thus it can't give any strange position.
410 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
411 goto no_jiffies_check;
413 if (hdelta < runtime->delay)
414 goto no_jiffies_check;
415 hdelta -= runtime->delay;
416 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
417 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
419 (((runtime->period_size * HZ) / runtime->rate)
421 /* move new_hw_ptr according jiffies not pos variable */
422 new_hw_ptr = old_hw_ptr;
424 /* use loop to avoid checks for delta overflows */
425 /* the delta value is small or zero in most cases */
427 new_hw_ptr += runtime->period_size;
428 if (new_hw_ptr >= runtime->boundary) {
429 new_hw_ptr -= runtime->boundary;
434 /* align hw_base to buffer_size */
435 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
436 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
437 (long)pos, (long)hdelta,
438 (long)runtime->period_size, jdelta,
439 ((hdelta * HZ) / runtime->rate), hw_base,
440 (unsigned long)old_hw_ptr,
441 (unsigned long)new_hw_ptr);
442 /* reset values to proper state */
444 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
447 if (delta > runtime->period_size + runtime->period_size / 2) {
448 hw_ptr_error(substream, in_interrupt,
450 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
451 substream->stream, (long)delta,
457 if (runtime->status->hw_ptr == new_hw_ptr) {
458 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
462 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
463 runtime->silence_size > 0)
464 snd_pcm_playback_silence(substream, new_hw_ptr);
467 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
469 delta += runtime->boundary;
470 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
471 runtime->hw_ptr_interrupt += delta;
472 if (runtime->hw_ptr_interrupt >= runtime->boundary)
473 runtime->hw_ptr_interrupt -= runtime->boundary;
475 runtime->hw_ptr_base = hw_base;
476 runtime->status->hw_ptr = new_hw_ptr;
477 runtime->hw_ptr_jiffies = curr_jiffies;
478 if (crossed_boundary) {
479 snd_BUG_ON(crossed_boundary != 1);
480 runtime->hw_ptr_wrap += runtime->boundary;
483 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
485 return snd_pcm_update_state(substream, runtime);
488 /* CAUTION: call it with irq disabled */
489 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
491 return snd_pcm_update_hw_ptr0(substream, 0);
495 * snd_pcm_set_ops - set the PCM operators
496 * @pcm: the pcm instance
497 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
498 * @ops: the operator table
500 * Sets the given PCM operators to the pcm instance.
502 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
503 const struct snd_pcm_ops *ops)
505 struct snd_pcm_str *stream = &pcm->streams[direction];
506 struct snd_pcm_substream *substream;
508 for (substream = stream->substream; substream != NULL; substream = substream->next)
509 substream->ops = ops;
512 EXPORT_SYMBOL(snd_pcm_set_ops);
515 * snd_pcm_sync - set the PCM sync id
516 * @substream: the pcm substream
518 * Sets the PCM sync identifier for the card.
520 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
522 struct snd_pcm_runtime *runtime = substream->runtime;
524 runtime->sync.id32[0] = substream->pcm->card->number;
525 runtime->sync.id32[1] = -1;
526 runtime->sync.id32[2] = -1;
527 runtime->sync.id32[3] = -1;
530 EXPORT_SYMBOL(snd_pcm_set_sync);
533 * Standard ioctl routine
536 static inline unsigned int div32(unsigned int a, unsigned int b,
547 static inline unsigned int div_down(unsigned int a, unsigned int b)
554 static inline unsigned int div_up(unsigned int a, unsigned int b)
566 static inline unsigned int mul(unsigned int a, unsigned int b)
570 if (div_down(UINT_MAX, a) < b)
575 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
576 unsigned int c, unsigned int *r)
578 u_int64_t n = (u_int64_t) a * b;
584 n = div_u64_rem(n, c, r);
593 * snd_interval_refine - refine the interval value of configurator
594 * @i: the interval value to refine
595 * @v: the interval value to refer to
597 * Refines the interval value with the reference value.
598 * The interval is changed to the range satisfying both intervals.
599 * The interval status (min, max, integer, etc.) are evaluated.
601 * Return: Positive if the value is changed, zero if it's not changed, or a
602 * negative error code.
604 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
607 if (snd_BUG_ON(snd_interval_empty(i)))
609 if (i->min < v->min) {
611 i->openmin = v->openmin;
613 } else if (i->min == v->min && !i->openmin && v->openmin) {
617 if (i->max > v->max) {
619 i->openmax = v->openmax;
621 } else if (i->max == v->max && !i->openmax && v->openmax) {
625 if (!i->integer && v->integer) {
638 } else if (!i->openmin && !i->openmax && i->min == i->max)
640 if (snd_interval_checkempty(i)) {
641 snd_interval_none(i);
647 EXPORT_SYMBOL(snd_interval_refine);
649 static int snd_interval_refine_first(struct snd_interval *i)
651 if (snd_BUG_ON(snd_interval_empty(i)))
653 if (snd_interval_single(i))
656 i->openmax = i->openmin;
662 static int snd_interval_refine_last(struct snd_interval *i)
664 if (snd_BUG_ON(snd_interval_empty(i)))
666 if (snd_interval_single(i))
669 i->openmin = i->openmax;
675 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
677 if (a->empty || b->empty) {
678 snd_interval_none(c);
682 c->min = mul(a->min, b->min);
683 c->openmin = (a->openmin || b->openmin);
684 c->max = mul(a->max, b->max);
685 c->openmax = (a->openmax || b->openmax);
686 c->integer = (a->integer && b->integer);
690 * snd_interval_div - refine the interval value with division
697 * Returns non-zero if the value is changed, zero if not changed.
699 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
702 if (a->empty || b->empty) {
703 snd_interval_none(c);
707 c->min = div32(a->min, b->max, &r);
708 c->openmin = (r || a->openmin || b->openmax);
710 c->max = div32(a->max, b->min, &r);
715 c->openmax = (a->openmax || b->openmin);
724 * snd_interval_muldivk - refine the interval value
727 * @k: divisor (as integer)
732 * Returns non-zero if the value is changed, zero if not changed.
734 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
735 unsigned int k, struct snd_interval *c)
738 if (a->empty || b->empty) {
739 snd_interval_none(c);
743 c->min = muldiv32(a->min, b->min, k, &r);
744 c->openmin = (r || a->openmin || b->openmin);
745 c->max = muldiv32(a->max, b->max, k, &r);
750 c->openmax = (a->openmax || b->openmax);
755 * snd_interval_mulkdiv - refine the interval value
757 * @k: dividend 2 (as integer)
763 * Returns non-zero if the value is changed, zero if not changed.
765 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
766 const struct snd_interval *b, struct snd_interval *c)
769 if (a->empty || b->empty) {
770 snd_interval_none(c);
774 c->min = muldiv32(a->min, k, b->max, &r);
775 c->openmin = (r || a->openmin || b->openmax);
777 c->max = muldiv32(a->max, k, b->min, &r);
782 c->openmax = (a->openmax || b->openmin);
794 * snd_interval_ratnum - refine the interval value
795 * @i: interval to refine
796 * @rats_count: number of ratnum_t
797 * @rats: ratnum_t array
798 * @nump: pointer to store the resultant numerator
799 * @denp: pointer to store the resultant denominator
801 * Return: Positive if the value is changed, zero if it's not changed, or a
802 * negative error code.
804 int snd_interval_ratnum(struct snd_interval *i,
805 unsigned int rats_count, const struct snd_ratnum *rats,
806 unsigned int *nump, unsigned int *denp)
808 unsigned int best_num, best_den;
811 struct snd_interval t;
813 unsigned int result_num, result_den;
816 best_num = best_den = best_diff = 0;
817 for (k = 0; k < rats_count; ++k) {
818 unsigned int num = rats[k].num;
820 unsigned int q = i->min;
824 den = div_up(num, q);
825 if (den < rats[k].den_min)
827 if (den > rats[k].den_max)
828 den = rats[k].den_max;
831 r = (den - rats[k].den_min) % rats[k].den_step;
835 diff = num - q * den;
839 diff * best_den < best_diff * den) {
849 t.min = div_down(best_num, best_den);
850 t.openmin = !!(best_num % best_den);
852 result_num = best_num;
853 result_diff = best_diff;
854 result_den = best_den;
855 best_num = best_den = best_diff = 0;
856 for (k = 0; k < rats_count; ++k) {
857 unsigned int num = rats[k].num;
859 unsigned int q = i->max;
865 den = div_down(num, q);
866 if (den > rats[k].den_max)
868 if (den < rats[k].den_min)
869 den = rats[k].den_min;
872 r = (den - rats[k].den_min) % rats[k].den_step;
874 den += rats[k].den_step - r;
876 diff = q * den - num;
880 diff * best_den < best_diff * den) {
890 t.max = div_up(best_num, best_den);
891 t.openmax = !!(best_num % best_den);
893 err = snd_interval_refine(i, &t);
897 if (snd_interval_single(i)) {
898 if (best_diff * result_den < result_diff * best_den) {
899 result_num = best_num;
900 result_den = best_den;
910 EXPORT_SYMBOL(snd_interval_ratnum);
913 * snd_interval_ratden - refine the interval value
914 * @i: interval to refine
915 * @rats_count: number of struct ratden
916 * @rats: struct ratden array
917 * @nump: pointer to store the resultant numerator
918 * @denp: pointer to store the resultant denominator
920 * Return: Positive if the value is changed, zero if it's not changed, or a
921 * negative error code.
923 static int snd_interval_ratden(struct snd_interval *i,
924 unsigned int rats_count,
925 const struct snd_ratden *rats,
926 unsigned int *nump, unsigned int *denp)
928 unsigned int best_num, best_diff, best_den;
930 struct snd_interval t;
933 best_num = best_den = best_diff = 0;
934 for (k = 0; k < rats_count; ++k) {
936 unsigned int den = rats[k].den;
937 unsigned int q = i->min;
940 if (num > rats[k].num_max)
942 if (num < rats[k].num_min)
943 num = rats[k].num_max;
946 r = (num - rats[k].num_min) % rats[k].num_step;
948 num += rats[k].num_step - r;
950 diff = num - q * den;
952 diff * best_den < best_diff * den) {
962 t.min = div_down(best_num, best_den);
963 t.openmin = !!(best_num % best_den);
965 best_num = best_den = best_diff = 0;
966 for (k = 0; k < rats_count; ++k) {
968 unsigned int den = rats[k].den;
969 unsigned int q = i->max;
972 if (num < rats[k].num_min)
974 if (num > rats[k].num_max)
975 num = rats[k].num_max;
978 r = (num - rats[k].num_min) % rats[k].num_step;
982 diff = q * den - num;
984 diff * best_den < best_diff * den) {
994 t.max = div_up(best_num, best_den);
995 t.openmax = !!(best_num % best_den);
997 err = snd_interval_refine(i, &t);
1001 if (snd_interval_single(i)) {
1011 * snd_interval_list - refine the interval value from the list
1012 * @i: the interval value to refine
1013 * @count: the number of elements in the list
1014 * @list: the value list
1015 * @mask: the bit-mask to evaluate
1017 * Refines the interval value from the list.
1018 * When mask is non-zero, only the elements corresponding to bit 1 are
1021 * Return: Positive if the value is changed, zero if it's not changed, or a
1022 * negative error code.
1024 int snd_interval_list(struct snd_interval *i, unsigned int count,
1025 const unsigned int *list, unsigned int mask)
1028 struct snd_interval list_range;
1034 snd_interval_any(&list_range);
1035 list_range.min = UINT_MAX;
1037 for (k = 0; k < count; k++) {
1038 if (mask && !(mask & (1 << k)))
1040 if (!snd_interval_test(i, list[k]))
1042 list_range.min = min(list_range.min, list[k]);
1043 list_range.max = max(list_range.max, list[k]);
1045 return snd_interval_refine(i, &list_range);
1048 EXPORT_SYMBOL(snd_interval_list);
1051 * snd_interval_ranges - refine the interval value from the list of ranges
1052 * @i: the interval value to refine
1053 * @count: the number of elements in the list of ranges
1054 * @ranges: the ranges list
1055 * @mask: the bit-mask to evaluate
1057 * Refines the interval value from the list of ranges.
1058 * When mask is non-zero, only the elements corresponding to bit 1 are
1061 * Return: Positive if the value is changed, zero if it's not changed, or a
1062 * negative error code.
1064 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1065 const struct snd_interval *ranges, unsigned int mask)
1068 struct snd_interval range_union;
1069 struct snd_interval range;
1072 snd_interval_none(i);
1075 snd_interval_any(&range_union);
1076 range_union.min = UINT_MAX;
1077 range_union.max = 0;
1078 for (k = 0; k < count; k++) {
1079 if (mask && !(mask & (1 << k)))
1081 snd_interval_copy(&range, &ranges[k]);
1082 if (snd_interval_refine(&range, i) < 0)
1084 if (snd_interval_empty(&range))
1087 if (range.min < range_union.min) {
1088 range_union.min = range.min;
1089 range_union.openmin = 1;
1091 if (range.min == range_union.min && !range.openmin)
1092 range_union.openmin = 0;
1093 if (range.max > range_union.max) {
1094 range_union.max = range.max;
1095 range_union.openmax = 1;
1097 if (range.max == range_union.max && !range.openmax)
1098 range_union.openmax = 0;
1100 return snd_interval_refine(i, &range_union);
1102 EXPORT_SYMBOL(snd_interval_ranges);
1104 static int snd_interval_step(struct snd_interval *i, unsigned int step)
1109 if (n != 0 || i->openmin) {
1115 if (n != 0 || i->openmax) {
1120 if (snd_interval_checkempty(i)) {
1127 /* Info constraints helpers */
1130 * snd_pcm_hw_rule_add - add the hw-constraint rule
1131 * @runtime: the pcm runtime instance
1132 * @cond: condition bits
1133 * @var: the variable to evaluate
1134 * @func: the evaluation function
1135 * @private: the private data pointer passed to function
1136 * @dep: the dependent variables
1138 * Return: Zero if successful, or a negative error code on failure.
1140 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1142 snd_pcm_hw_rule_func_t func, void *private,
1145 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1146 struct snd_pcm_hw_rule *c;
1149 va_start(args, dep);
1150 if (constrs->rules_num >= constrs->rules_all) {
1151 struct snd_pcm_hw_rule *new;
1152 unsigned int new_rules = constrs->rules_all + 16;
1153 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1158 if (constrs->rules) {
1159 memcpy(new, constrs->rules,
1160 constrs->rules_num * sizeof(*c));
1161 kfree(constrs->rules);
1163 constrs->rules = new;
1164 constrs->rules_all = new_rules;
1166 c = &constrs->rules[constrs->rules_num];
1170 c->private = private;
1173 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1180 dep = va_arg(args, int);
1182 constrs->rules_num++;
1187 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1190 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1191 * @runtime: PCM runtime instance
1192 * @var: hw_params variable to apply the mask
1193 * @mask: the bitmap mask
1195 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1197 * Return: Zero if successful, or a negative error code on failure.
1199 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1202 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1203 struct snd_mask *maskp = constrs_mask(constrs, var);
1204 *maskp->bits &= mask;
1205 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1206 if (*maskp->bits == 0)
1212 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1213 * @runtime: PCM runtime instance
1214 * @var: hw_params variable to apply the mask
1215 * @mask: the 64bit bitmap mask
1217 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1219 * Return: Zero if successful, or a negative error code on failure.
1221 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1224 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1225 struct snd_mask *maskp = constrs_mask(constrs, var);
1226 maskp->bits[0] &= (u_int32_t)mask;
1227 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1228 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1229 if (! maskp->bits[0] && ! maskp->bits[1])
1233 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1236 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1237 * @runtime: PCM runtime instance
1238 * @var: hw_params variable to apply the integer constraint
1240 * Apply the constraint of integer to an interval parameter.
1242 * Return: Positive if the value is changed, zero if it's not changed, or a
1243 * negative error code.
1245 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1247 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1248 return snd_interval_setinteger(constrs_interval(constrs, var));
1251 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1254 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1255 * @runtime: PCM runtime instance
1256 * @var: hw_params variable to apply the range
1257 * @min: the minimal value
1258 * @max: the maximal value
1260 * Apply the min/max range constraint to an interval parameter.
1262 * Return: Positive if the value is changed, zero if it's not changed, or a
1263 * negative error code.
1265 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1266 unsigned int min, unsigned int max)
1268 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1269 struct snd_interval t;
1272 t.openmin = t.openmax = 0;
1274 return snd_interval_refine(constrs_interval(constrs, var), &t);
1277 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1279 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1280 struct snd_pcm_hw_rule *rule)
1282 struct snd_pcm_hw_constraint_list *list = rule->private;
1283 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1288 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1289 * @runtime: PCM runtime instance
1290 * @cond: condition bits
1291 * @var: hw_params variable to apply the list constraint
1294 * Apply the list of constraints to an interval parameter.
1296 * Return: Zero if successful, or a negative error code on failure.
1298 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1300 snd_pcm_hw_param_t var,
1301 const struct snd_pcm_hw_constraint_list *l)
1303 return snd_pcm_hw_rule_add(runtime, cond, var,
1304 snd_pcm_hw_rule_list, (void *)l,
1308 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1310 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1311 struct snd_pcm_hw_rule *rule)
1313 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1314 return snd_interval_ranges(hw_param_interval(params, rule->var),
1315 r->count, r->ranges, r->mask);
1320 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1321 * @runtime: PCM runtime instance
1322 * @cond: condition bits
1323 * @var: hw_params variable to apply the list of range constraints
1326 * Apply the list of range constraints to an interval parameter.
1328 * Return: Zero if successful, or a negative error code on failure.
1330 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1332 snd_pcm_hw_param_t var,
1333 const struct snd_pcm_hw_constraint_ranges *r)
1335 return snd_pcm_hw_rule_add(runtime, cond, var,
1336 snd_pcm_hw_rule_ranges, (void *)r,
1339 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1341 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1342 struct snd_pcm_hw_rule *rule)
1344 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1345 unsigned int num = 0, den = 0;
1347 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1348 r->nrats, r->rats, &num, &den);
1349 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1350 params->rate_num = num;
1351 params->rate_den = den;
1357 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1358 * @runtime: PCM runtime instance
1359 * @cond: condition bits
1360 * @var: hw_params variable to apply the ratnums constraint
1361 * @r: struct snd_ratnums constriants
1363 * Return: Zero if successful, or a negative error code on failure.
1365 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1367 snd_pcm_hw_param_t var,
1368 const struct snd_pcm_hw_constraint_ratnums *r)
1370 return snd_pcm_hw_rule_add(runtime, cond, var,
1371 snd_pcm_hw_rule_ratnums, (void *)r,
1375 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1377 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1378 struct snd_pcm_hw_rule *rule)
1380 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1381 unsigned int num = 0, den = 0;
1382 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1383 r->nrats, r->rats, &num, &den);
1384 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1385 params->rate_num = num;
1386 params->rate_den = den;
1392 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1393 * @runtime: PCM runtime instance
1394 * @cond: condition bits
1395 * @var: hw_params variable to apply the ratdens constraint
1396 * @r: struct snd_ratdens constriants
1398 * Return: Zero if successful, or a negative error code on failure.
1400 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1402 snd_pcm_hw_param_t var,
1403 const struct snd_pcm_hw_constraint_ratdens *r)
1405 return snd_pcm_hw_rule_add(runtime, cond, var,
1406 snd_pcm_hw_rule_ratdens, (void *)r,
1410 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1412 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1413 struct snd_pcm_hw_rule *rule)
1415 unsigned int l = (unsigned long) rule->private;
1416 int width = l & 0xffff;
1417 unsigned int msbits = l >> 16;
1418 const struct snd_interval *i =
1419 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1421 if (!snd_interval_single(i))
1424 if ((snd_interval_value(i) == width) ||
1425 (width == 0 && snd_interval_value(i) > msbits))
1426 params->msbits = min_not_zero(params->msbits, msbits);
1432 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1433 * @runtime: PCM runtime instance
1434 * @cond: condition bits
1435 * @width: sample bits width
1436 * @msbits: msbits width
1438 * This constraint will set the number of most significant bits (msbits) if a
1439 * sample format with the specified width has been select. If width is set to 0
1440 * the msbits will be set for any sample format with a width larger than the
1443 * Return: Zero if successful, or a negative error code on failure.
1445 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1448 unsigned int msbits)
1450 unsigned long l = (msbits << 16) | width;
1451 return snd_pcm_hw_rule_add(runtime, cond, -1,
1452 snd_pcm_hw_rule_msbits,
1454 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1457 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1459 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1460 struct snd_pcm_hw_rule *rule)
1462 unsigned long step = (unsigned long) rule->private;
1463 return snd_interval_step(hw_param_interval(params, rule->var), step);
1467 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1468 * @runtime: PCM runtime instance
1469 * @cond: condition bits
1470 * @var: hw_params variable to apply the step constraint
1473 * Return: Zero if successful, or a negative error code on failure.
1475 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1477 snd_pcm_hw_param_t var,
1480 return snd_pcm_hw_rule_add(runtime, cond, var,
1481 snd_pcm_hw_rule_step, (void *) step,
1485 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1487 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1489 static unsigned int pow2_sizes[] = {
1490 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1491 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1492 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1493 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1495 return snd_interval_list(hw_param_interval(params, rule->var),
1496 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1500 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1501 * @runtime: PCM runtime instance
1502 * @cond: condition bits
1503 * @var: hw_params variable to apply the power-of-2 constraint
1505 * Return: Zero if successful, or a negative error code on failure.
1507 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1509 snd_pcm_hw_param_t var)
1511 return snd_pcm_hw_rule_add(runtime, cond, var,
1512 snd_pcm_hw_rule_pow2, NULL,
1516 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1518 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1519 struct snd_pcm_hw_rule *rule)
1521 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1522 struct snd_interval *rate;
1524 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1525 return snd_interval_list(rate, 1, &base_rate, 0);
1529 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1530 * @runtime: PCM runtime instance
1531 * @base_rate: the rate at which the hardware does not resample
1533 * Return: Zero if successful, or a negative error code on failure.
1535 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1536 unsigned int base_rate)
1538 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1539 SNDRV_PCM_HW_PARAM_RATE,
1540 snd_pcm_hw_rule_noresample_func,
1541 (void *)(uintptr_t)base_rate,
1542 SNDRV_PCM_HW_PARAM_RATE, -1);
1544 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1546 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1547 snd_pcm_hw_param_t var)
1549 if (hw_is_mask(var)) {
1550 snd_mask_any(hw_param_mask(params, var));
1551 params->cmask |= 1 << var;
1552 params->rmask |= 1 << var;
1555 if (hw_is_interval(var)) {
1556 snd_interval_any(hw_param_interval(params, var));
1557 params->cmask |= 1 << var;
1558 params->rmask |= 1 << var;
1564 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1567 memset(params, 0, sizeof(*params));
1568 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1569 _snd_pcm_hw_param_any(params, k);
1570 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1571 _snd_pcm_hw_param_any(params, k);
1575 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1578 * snd_pcm_hw_param_value - return @params field @var value
1579 * @params: the hw_params instance
1580 * @var: parameter to retrieve
1581 * @dir: pointer to the direction (-1,0,1) or %NULL
1583 * Return: The value for field @var if it's fixed in configuration space
1584 * defined by @params. -%EINVAL otherwise.
1586 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1587 snd_pcm_hw_param_t var, int *dir)
1589 if (hw_is_mask(var)) {
1590 const struct snd_mask *mask = hw_param_mask_c(params, var);
1591 if (!snd_mask_single(mask))
1595 return snd_mask_value(mask);
1597 if (hw_is_interval(var)) {
1598 const struct snd_interval *i = hw_param_interval_c(params, var);
1599 if (!snd_interval_single(i))
1603 return snd_interval_value(i);
1608 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1610 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1611 snd_pcm_hw_param_t var)
1613 if (hw_is_mask(var)) {
1614 snd_mask_none(hw_param_mask(params, var));
1615 params->cmask |= 1 << var;
1616 params->rmask |= 1 << var;
1617 } else if (hw_is_interval(var)) {
1618 snd_interval_none(hw_param_interval(params, var));
1619 params->cmask |= 1 << var;
1620 params->rmask |= 1 << var;
1626 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1628 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1629 snd_pcm_hw_param_t var)
1632 if (hw_is_mask(var))
1633 changed = snd_mask_refine_first(hw_param_mask(params, var));
1634 else if (hw_is_interval(var))
1635 changed = snd_interval_refine_first(hw_param_interval(params, var));
1639 params->cmask |= 1 << var;
1640 params->rmask |= 1 << var;
1647 * snd_pcm_hw_param_first - refine config space and return minimum value
1648 * @pcm: PCM instance
1649 * @params: the hw_params instance
1650 * @var: parameter to retrieve
1651 * @dir: pointer to the direction (-1,0,1) or %NULL
1653 * Inside configuration space defined by @params remove from @var all
1654 * values > minimum. Reduce configuration space accordingly.
1656 * Return: The minimum, or a negative error code on failure.
1658 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1659 struct snd_pcm_hw_params *params,
1660 snd_pcm_hw_param_t var, int *dir)
1662 int changed = _snd_pcm_hw_param_first(params, var);
1665 if (params->rmask) {
1666 int err = snd_pcm_hw_refine(pcm, params);
1667 if (snd_BUG_ON(err < 0))
1670 return snd_pcm_hw_param_value(params, var, dir);
1673 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1675 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1676 snd_pcm_hw_param_t var)
1679 if (hw_is_mask(var))
1680 changed = snd_mask_refine_last(hw_param_mask(params, var));
1681 else if (hw_is_interval(var))
1682 changed = snd_interval_refine_last(hw_param_interval(params, var));
1686 params->cmask |= 1 << var;
1687 params->rmask |= 1 << var;
1694 * snd_pcm_hw_param_last - refine config space and return maximum value
1695 * @pcm: PCM instance
1696 * @params: the hw_params instance
1697 * @var: parameter to retrieve
1698 * @dir: pointer to the direction (-1,0,1) or %NULL
1700 * Inside configuration space defined by @params remove from @var all
1701 * values < maximum. Reduce configuration space accordingly.
1703 * Return: The maximum, or a negative error code on failure.
1705 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1706 struct snd_pcm_hw_params *params,
1707 snd_pcm_hw_param_t var, int *dir)
1709 int changed = _snd_pcm_hw_param_last(params, var);
1712 if (params->rmask) {
1713 int err = snd_pcm_hw_refine(pcm, params);
1714 if (snd_BUG_ON(err < 0))
1717 return snd_pcm_hw_param_value(params, var, dir);
1720 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1723 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1724 * @pcm: PCM instance
1725 * @params: the hw_params instance
1727 * Choose one configuration from configuration space defined by @params.
1728 * The configuration chosen is that obtained fixing in this order:
1729 * first access, first format, first subformat, min channels,
1730 * min rate, min period time, max buffer size, min tick time
1732 * Return: Zero if successful, or a negative error code on failure.
1734 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1735 struct snd_pcm_hw_params *params)
1737 static int vars[] = {
1738 SNDRV_PCM_HW_PARAM_ACCESS,
1739 SNDRV_PCM_HW_PARAM_FORMAT,
1740 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1741 SNDRV_PCM_HW_PARAM_CHANNELS,
1742 SNDRV_PCM_HW_PARAM_RATE,
1743 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1744 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1745 SNDRV_PCM_HW_PARAM_TICK_TIME,
1750 for (v = vars; *v != -1; v++) {
1751 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1752 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1754 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1755 if (snd_BUG_ON(err < 0))
1761 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1764 struct snd_pcm_runtime *runtime = substream->runtime;
1765 unsigned long flags;
1766 snd_pcm_stream_lock_irqsave(substream, flags);
1767 if (snd_pcm_running(substream) &&
1768 snd_pcm_update_hw_ptr(substream) >= 0)
1769 runtime->status->hw_ptr %= runtime->buffer_size;
1771 runtime->status->hw_ptr = 0;
1772 runtime->hw_ptr_wrap = 0;
1774 snd_pcm_stream_unlock_irqrestore(substream, flags);
1778 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1781 struct snd_pcm_channel_info *info = arg;
1782 struct snd_pcm_runtime *runtime = substream->runtime;
1784 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1788 width = snd_pcm_format_physical_width(runtime->format);
1792 switch (runtime->access) {
1793 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1794 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1795 info->first = info->channel * width;
1796 info->step = runtime->channels * width;
1798 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1799 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1801 size_t size = runtime->dma_bytes / runtime->channels;
1802 info->first = info->channel * size * 8;
1813 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1816 struct snd_pcm_hw_params *params = arg;
1817 snd_pcm_format_t format;
1821 params->fifo_size = substream->runtime->hw.fifo_size;
1822 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1823 format = params_format(params);
1824 channels = params_channels(params);
1825 frame_size = snd_pcm_format_size(format, channels);
1827 params->fifo_size /= (unsigned)frame_size;
1833 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1834 * @substream: the pcm substream instance
1835 * @cmd: ioctl command
1836 * @arg: ioctl argument
1838 * Processes the generic ioctl commands for PCM.
1839 * Can be passed as the ioctl callback for PCM ops.
1841 * Return: Zero if successful, or a negative error code on failure.
1843 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1844 unsigned int cmd, void *arg)
1847 case SNDRV_PCM_IOCTL1_INFO:
1849 case SNDRV_PCM_IOCTL1_RESET:
1850 return snd_pcm_lib_ioctl_reset(substream, arg);
1851 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1852 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1853 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1854 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1859 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1862 * snd_pcm_period_elapsed - update the pcm status for the next period
1863 * @substream: the pcm substream instance
1865 * This function is called from the interrupt handler when the
1866 * PCM has processed the period size. It will update the current
1867 * pointer, wake up sleepers, etc.
1869 * Even if more than one periods have elapsed since the last call, you
1870 * have to call this only once.
1872 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1874 struct snd_pcm_runtime *runtime;
1875 unsigned long flags;
1877 if (PCM_RUNTIME_CHECK(substream))
1879 runtime = substream->runtime;
1881 snd_pcm_stream_lock_irqsave(substream, flags);
1882 if (!snd_pcm_running(substream) ||
1883 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1886 #ifdef CONFIG_SND_PCM_TIMER
1887 if (substream->timer_running)
1888 snd_timer_interrupt(substream->timer, 1);
1891 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1892 snd_pcm_stream_unlock_irqrestore(substream, flags);
1895 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1898 * Wait until avail_min data becomes available
1899 * Returns a negative error code if any error occurs during operation.
1900 * The available space is stored on availp. When err = 0 and avail = 0
1901 * on the capture stream, it indicates the stream is in DRAINING state.
1903 static int wait_for_avail(struct snd_pcm_substream *substream,
1904 snd_pcm_uframes_t *availp)
1906 struct snd_pcm_runtime *runtime = substream->runtime;
1907 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1910 snd_pcm_uframes_t avail = 0;
1911 long wait_time, tout;
1913 init_waitqueue_entry(&wait, current);
1914 set_current_state(TASK_INTERRUPTIBLE);
1915 add_wait_queue(&runtime->tsleep, &wait);
1917 if (runtime->no_period_wakeup)
1918 wait_time = MAX_SCHEDULE_TIMEOUT;
1921 if (runtime->rate) {
1922 long t = runtime->period_size * 2 / runtime->rate;
1923 wait_time = max(t, wait_time);
1925 wait_time = msecs_to_jiffies(wait_time * 1000);
1929 if (signal_pending(current)) {
1935 * We need to check if space became available already
1936 * (and thus the wakeup happened already) first to close
1937 * the race of space already having become available.
1938 * This check must happen after been added to the waitqueue
1939 * and having current state be INTERRUPTIBLE.
1942 avail = snd_pcm_playback_avail(runtime);
1944 avail = snd_pcm_capture_avail(runtime);
1945 if (avail >= runtime->twake)
1947 snd_pcm_stream_unlock_irq(substream);
1949 tout = schedule_timeout(wait_time);
1951 snd_pcm_stream_lock_irq(substream);
1952 set_current_state(TASK_INTERRUPTIBLE);
1953 switch (runtime->status->state) {
1954 case SNDRV_PCM_STATE_SUSPENDED:
1957 case SNDRV_PCM_STATE_XRUN:
1960 case SNDRV_PCM_STATE_DRAINING:
1964 avail = 0; /* indicate draining */
1966 case SNDRV_PCM_STATE_OPEN:
1967 case SNDRV_PCM_STATE_SETUP:
1968 case SNDRV_PCM_STATE_DISCONNECTED:
1971 case SNDRV_PCM_STATE_PAUSED:
1975 pcm_dbg(substream->pcm,
1976 "%s write error (DMA or IRQ trouble?)\n",
1977 is_playback ? "playback" : "capture");
1983 set_current_state(TASK_RUNNING);
1984 remove_wait_queue(&runtime->tsleep, &wait);
1989 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1991 unsigned long data, unsigned int off,
1992 snd_pcm_uframes_t frames)
1994 struct snd_pcm_runtime *runtime = substream->runtime;
1996 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1997 if (substream->ops->copy) {
1998 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2001 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2002 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2008 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
2009 unsigned long data, unsigned int off,
2010 snd_pcm_uframes_t size);
2012 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
2014 snd_pcm_uframes_t size,
2016 transfer_f transfer)
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 snd_pcm_uframes_t xfer = 0;
2020 snd_pcm_uframes_t offset = 0;
2021 snd_pcm_uframes_t avail;
2027 snd_pcm_stream_lock_irq(substream);
2028 switch (runtime->status->state) {
2029 case SNDRV_PCM_STATE_PREPARED:
2030 case SNDRV_PCM_STATE_RUNNING:
2031 case SNDRV_PCM_STATE_PAUSED:
2033 case SNDRV_PCM_STATE_XRUN:
2036 case SNDRV_PCM_STATE_SUSPENDED:
2044 runtime->twake = runtime->control->avail_min ? : 1;
2045 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2046 snd_pcm_update_hw_ptr(substream);
2047 avail = snd_pcm_playback_avail(runtime);
2049 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2050 snd_pcm_uframes_t cont;
2056 runtime->twake = min_t(snd_pcm_uframes_t, size,
2057 runtime->control->avail_min ? : 1);
2058 err = wait_for_avail(substream, &avail);
2062 frames = size > avail ? avail : size;
2063 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2066 if (snd_BUG_ON(!frames)) {
2068 snd_pcm_stream_unlock_irq(substream);
2071 appl_ptr = runtime->control->appl_ptr;
2072 appl_ofs = appl_ptr % runtime->buffer_size;
2073 snd_pcm_stream_unlock_irq(substream);
2074 err = transfer(substream, appl_ofs, data, offset, frames);
2075 snd_pcm_stream_lock_irq(substream);
2078 switch (runtime->status->state) {
2079 case SNDRV_PCM_STATE_XRUN:
2082 case SNDRV_PCM_STATE_SUSPENDED:
2089 if (appl_ptr >= runtime->boundary)
2090 appl_ptr -= runtime->boundary;
2091 runtime->control->appl_ptr = appl_ptr;
2092 if (substream->ops->ack)
2093 substream->ops->ack(substream);
2099 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2100 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2101 err = snd_pcm_start(substream);
2108 if (xfer > 0 && err >= 0)
2109 snd_pcm_update_state(substream, runtime);
2110 snd_pcm_stream_unlock_irq(substream);
2111 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2114 /* sanity-check for read/write methods */
2115 static int pcm_sanity_check(struct snd_pcm_substream *substream)
2117 struct snd_pcm_runtime *runtime;
2118 if (PCM_RUNTIME_CHECK(substream))
2120 runtime = substream->runtime;
2121 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2123 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2128 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
2130 struct snd_pcm_runtime *runtime;
2134 err = pcm_sanity_check(substream);
2137 runtime = substream->runtime;
2138 nonblock = !!(substream->f_flags & O_NONBLOCK);
2140 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2141 runtime->channels > 1)
2143 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2144 snd_pcm_lib_write_transfer);
2147 EXPORT_SYMBOL(snd_pcm_lib_write);
2149 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
2151 unsigned long data, unsigned int off,
2152 snd_pcm_uframes_t frames)
2154 struct snd_pcm_runtime *runtime = substream->runtime;
2156 void __user **bufs = (void __user **)data;
2157 int channels = runtime->channels;
2159 if (substream->ops->copy) {
2160 if (snd_BUG_ON(!substream->ops->silence))
2162 for (c = 0; c < channels; ++c, ++bufs) {
2163 if (*bufs == NULL) {
2164 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2167 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2168 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2173 /* default transfer behaviour */
2174 size_t dma_csize = runtime->dma_bytes / channels;
2175 for (c = 0; c < channels; ++c, ++bufs) {
2176 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2177 if (*bufs == NULL) {
2178 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2180 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2181 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2189 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
2191 snd_pcm_uframes_t frames)
2193 struct snd_pcm_runtime *runtime;
2197 err = pcm_sanity_check(substream);
2200 runtime = substream->runtime;
2201 nonblock = !!(substream->f_flags & O_NONBLOCK);
2203 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2205 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2206 nonblock, snd_pcm_lib_writev_transfer);
2209 EXPORT_SYMBOL(snd_pcm_lib_writev);
2211 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2213 unsigned long data, unsigned int off,
2214 snd_pcm_uframes_t frames)
2216 struct snd_pcm_runtime *runtime = substream->runtime;
2218 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2219 if (substream->ops->copy) {
2220 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2223 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2224 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2230 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2232 snd_pcm_uframes_t size,
2234 transfer_f transfer)
2236 struct snd_pcm_runtime *runtime = substream->runtime;
2237 snd_pcm_uframes_t xfer = 0;
2238 snd_pcm_uframes_t offset = 0;
2239 snd_pcm_uframes_t avail;
2245 snd_pcm_stream_lock_irq(substream);
2246 switch (runtime->status->state) {
2247 case SNDRV_PCM_STATE_PREPARED:
2248 if (size >= runtime->start_threshold) {
2249 err = snd_pcm_start(substream);
2254 case SNDRV_PCM_STATE_DRAINING:
2255 case SNDRV_PCM_STATE_RUNNING:
2256 case SNDRV_PCM_STATE_PAUSED:
2258 case SNDRV_PCM_STATE_XRUN:
2261 case SNDRV_PCM_STATE_SUSPENDED:
2269 runtime->twake = runtime->control->avail_min ? : 1;
2270 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2271 snd_pcm_update_hw_ptr(substream);
2272 avail = snd_pcm_capture_avail(runtime);
2274 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2275 snd_pcm_uframes_t cont;
2277 if (runtime->status->state ==
2278 SNDRV_PCM_STATE_DRAINING) {
2279 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2286 runtime->twake = min_t(snd_pcm_uframes_t, size,
2287 runtime->control->avail_min ? : 1);
2288 err = wait_for_avail(substream, &avail);
2292 continue; /* draining */
2294 frames = size > avail ? avail : size;
2295 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2298 if (snd_BUG_ON(!frames)) {
2300 snd_pcm_stream_unlock_irq(substream);
2303 appl_ptr = runtime->control->appl_ptr;
2304 appl_ofs = appl_ptr % runtime->buffer_size;
2305 snd_pcm_stream_unlock_irq(substream);
2306 err = transfer(substream, appl_ofs, data, offset, frames);
2307 snd_pcm_stream_lock_irq(substream);
2310 switch (runtime->status->state) {
2311 case SNDRV_PCM_STATE_XRUN:
2314 case SNDRV_PCM_STATE_SUSPENDED:
2321 if (appl_ptr >= runtime->boundary)
2322 appl_ptr -= runtime->boundary;
2323 runtime->control->appl_ptr = appl_ptr;
2324 if (substream->ops->ack)
2325 substream->ops->ack(substream);
2334 if (xfer > 0 && err >= 0)
2335 snd_pcm_update_state(substream, runtime);
2336 snd_pcm_stream_unlock_irq(substream);
2337 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2340 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2342 struct snd_pcm_runtime *runtime;
2346 err = pcm_sanity_check(substream);
2349 runtime = substream->runtime;
2350 nonblock = !!(substream->f_flags & O_NONBLOCK);
2351 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2353 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2356 EXPORT_SYMBOL(snd_pcm_lib_read);
2358 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2360 unsigned long data, unsigned int off,
2361 snd_pcm_uframes_t frames)
2363 struct snd_pcm_runtime *runtime = substream->runtime;
2365 void __user **bufs = (void __user **)data;
2366 int channels = runtime->channels;
2368 if (substream->ops->copy) {
2369 for (c = 0; c < channels; ++c, ++bufs) {
2373 buf = *bufs + samples_to_bytes(runtime, off);
2374 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2378 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2379 for (c = 0; c < channels; ++c, ++bufs) {
2385 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2386 buf = *bufs + samples_to_bytes(runtime, off);
2387 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2394 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2396 snd_pcm_uframes_t frames)
2398 struct snd_pcm_runtime *runtime;
2402 err = pcm_sanity_check(substream);
2405 runtime = substream->runtime;
2406 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2409 nonblock = !!(substream->f_flags & O_NONBLOCK);
2410 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2412 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2415 EXPORT_SYMBOL(snd_pcm_lib_readv);
2418 * standard channel mapping helpers
2421 /* default channel maps for multi-channel playbacks, up to 8 channels */
2422 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2424 .map = { SNDRV_CHMAP_MONO } },
2426 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2428 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2429 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2431 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2432 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2433 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2435 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2436 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2437 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2438 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2441 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2443 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2444 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2446 .map = { SNDRV_CHMAP_MONO } },
2448 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2450 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2451 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2454 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2455 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2457 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2458 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2459 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2460 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2463 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2465 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2467 if (ch > info->max_channels)
2469 return !info->channel_mask || (info->channel_mask & (1U << ch));
2472 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_info *uinfo)
2475 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2479 uinfo->count = info->max_channels;
2480 uinfo->value.integer.min = 0;
2481 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2485 /* get callback for channel map ctl element
2486 * stores the channel position firstly matching with the current channels
2488 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2489 struct snd_ctl_elem_value *ucontrol)
2491 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2492 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2493 struct snd_pcm_substream *substream;
2494 const struct snd_pcm_chmap_elem *map;
2496 if (snd_BUG_ON(!info->chmap))
2498 substream = snd_pcm_chmap_substream(info, idx);
2501 memset(ucontrol->value.integer.value, 0,
2502 sizeof(ucontrol->value.integer.value));
2503 if (!substream->runtime)
2504 return 0; /* no channels set */
2505 for (map = info->chmap; map->channels; map++) {
2507 if (map->channels == substream->runtime->channels &&
2508 valid_chmap_channels(info, map->channels)) {
2509 for (i = 0; i < map->channels; i++)
2510 ucontrol->value.integer.value[i] = map->map[i];
2517 /* tlv callback for channel map ctl element
2518 * expands the pre-defined channel maps in a form of TLV
2520 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2521 unsigned int size, unsigned int __user *tlv)
2523 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2524 const struct snd_pcm_chmap_elem *map;
2525 unsigned int __user *dst;
2528 if (snd_BUG_ON(!info->chmap))
2532 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2536 for (map = info->chmap; map->channels; map++) {
2537 int chs_bytes = map->channels * 4;
2538 if (!valid_chmap_channels(info, map->channels))
2542 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2543 put_user(chs_bytes, dst + 1))
2548 if (size < chs_bytes)
2552 for (c = 0; c < map->channels; c++) {
2553 if (put_user(map->map[c], dst))
2558 if (put_user(count, tlv + 1))
2563 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2565 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2566 info->pcm->streams[info->stream].chmap_kctl = NULL;
2571 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2572 * @pcm: the assigned PCM instance
2573 * @stream: stream direction
2574 * @chmap: channel map elements (for query)
2575 * @max_channels: the max number of channels for the stream
2576 * @private_value: the value passed to each kcontrol's private_value field
2577 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2579 * Create channel-mapping control elements assigned to the given PCM stream(s).
2580 * Return: Zero if successful, or a negative error value.
2582 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2583 const struct snd_pcm_chmap_elem *chmap,
2585 unsigned long private_value,
2586 struct snd_pcm_chmap **info_ret)
2588 struct snd_pcm_chmap *info;
2589 struct snd_kcontrol_new knew = {
2590 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2591 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2592 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2593 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2594 .info = pcm_chmap_ctl_info,
2595 .get = pcm_chmap_ctl_get,
2596 .tlv.c = pcm_chmap_ctl_tlv,
2600 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2602 info = kzalloc(sizeof(*info), GFP_KERNEL);
2606 info->stream = stream;
2607 info->chmap = chmap;
2608 info->max_channels = max_channels;
2609 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2610 knew.name = "Playback Channel Map";
2612 knew.name = "Capture Channel Map";
2613 knew.device = pcm->device;
2614 knew.count = pcm->streams[stream].substream_count;
2615 knew.private_value = private_value;
2616 info->kctl = snd_ctl_new1(&knew, info);
2621 info->kctl->private_free = pcm_chmap_ctl_private_free;
2622 err = snd_ctl_add(pcm->card, info->kctl);
2625 pcm->streams[stream].chmap_kctl = info->kctl;
2630 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);