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 #include "pcm_local.h"
38 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
39 #define CREATE_TRACE_POINTS
40 #include "pcm_trace.h"
42 #define trace_hwptr(substream, pos, in_interrupt)
43 #define trace_xrun(substream)
44 #define trace_hw_ptr_error(substream, reason)
48 * fill ring buffer with silence
49 * runtime->silence_start: starting pointer to silence area
50 * runtime->silence_filled: size filled with silence
51 * runtime->silence_threshold: threshold from application
52 * runtime->silence_size: maximal size from application
54 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
56 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
58 struct snd_pcm_runtime *runtime = substream->runtime;
59 snd_pcm_uframes_t frames, ofs, transfer;
61 if (runtime->silence_size < runtime->boundary) {
62 snd_pcm_sframes_t noise_dist, n;
63 if (runtime->silence_start != runtime->control->appl_ptr) {
64 n = runtime->control->appl_ptr - runtime->silence_start;
66 n += runtime->boundary;
67 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
68 runtime->silence_filled -= n;
70 runtime->silence_filled = 0;
71 runtime->silence_start = runtime->control->appl_ptr;
73 if (runtime->silence_filled >= runtime->buffer_size)
75 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
76 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
78 frames = runtime->silence_threshold - noise_dist;
79 if (frames > runtime->silence_size)
80 frames = runtime->silence_size;
82 if (new_hw_ptr == ULONG_MAX) { /* initialization */
83 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
84 if (avail > runtime->buffer_size)
85 avail = runtime->buffer_size;
86 runtime->silence_filled = avail > 0 ? avail : 0;
87 runtime->silence_start = (runtime->status->hw_ptr +
88 runtime->silence_filled) %
91 ofs = runtime->status->hw_ptr;
92 frames = new_hw_ptr - ofs;
93 if ((snd_pcm_sframes_t)frames < 0)
94 frames += runtime->boundary;
95 runtime->silence_filled -= frames;
96 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
97 runtime->silence_filled = 0;
98 runtime->silence_start = new_hw_ptr;
100 runtime->silence_start = ofs;
103 frames = runtime->buffer_size - runtime->silence_filled;
105 if (snd_BUG_ON(frames > runtime->buffer_size))
109 ofs = runtime->silence_start % runtime->buffer_size;
111 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
112 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
113 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
114 if (substream->ops->silence) {
116 err = substream->ops->silence(substream, -1, ofs, transfer);
119 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
120 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
124 unsigned int channels = runtime->channels;
125 if (substream->ops->silence) {
126 for (c = 0; c < channels; ++c) {
128 err = substream->ops->silence(substream, c, ofs, transfer);
132 size_t dma_csize = runtime->dma_bytes / channels;
133 for (c = 0; c < channels; ++c) {
134 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
135 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
139 runtime->silence_filled += transfer;
145 #ifdef CONFIG_SND_DEBUG
146 void snd_pcm_debug_name(struct snd_pcm_substream *substream,
147 char *name, size_t len)
149 snprintf(name, len, "pcmC%dD%d%c:%d",
150 substream->pcm->card->number,
151 substream->pcm->device,
152 substream->stream ? 'c' : 'p',
155 EXPORT_SYMBOL(snd_pcm_debug_name);
158 #define XRUN_DEBUG_BASIC (1<<0)
159 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
160 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
162 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
164 #define xrun_debug(substream, mask) \
165 ((substream)->pstr->xrun_debug & (mask))
167 #define xrun_debug(substream, mask) 0
170 #define dump_stack_on_xrun(substream) do { \
171 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
175 static void xrun(struct snd_pcm_substream *substream)
177 struct snd_pcm_runtime *runtime = substream->runtime;
179 trace_xrun(substream);
180 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
181 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
182 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
183 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
185 snd_pcm_debug_name(substream, name, sizeof(name));
186 pcm_warn(substream->pcm, "XRUN: %s\n", name);
187 dump_stack_on_xrun(substream);
191 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
192 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
194 trace_hw_ptr_error(substream, reason); \
195 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
196 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
197 (in_interrupt) ? 'Q' : 'P', ##args); \
198 dump_stack_on_xrun(substream); \
202 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
204 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
208 int snd_pcm_update_state(struct snd_pcm_substream *substream,
209 struct snd_pcm_runtime *runtime)
211 snd_pcm_uframes_t avail;
213 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
214 avail = snd_pcm_playback_avail(runtime);
216 avail = snd_pcm_capture_avail(runtime);
217 if (avail > runtime->avail_max)
218 runtime->avail_max = avail;
219 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
220 if (avail >= runtime->buffer_size) {
221 snd_pcm_drain_done(substream);
225 if (avail >= runtime->stop_threshold) {
230 if (runtime->twake) {
231 if (avail >= runtime->twake)
232 wake_up(&runtime->tsleep);
233 } else if (avail >= runtime->control->avail_min)
234 wake_up(&runtime->sleep);
238 static void update_audio_tstamp(struct snd_pcm_substream *substream,
239 struct timespec *curr_tstamp,
240 struct timespec *audio_tstamp)
242 struct snd_pcm_runtime *runtime = substream->runtime;
243 u64 audio_frames, audio_nsecs;
244 struct timespec driver_tstamp;
246 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
249 if (!(substream->ops->get_time_info) ||
250 (runtime->audio_tstamp_report.actual_type ==
251 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
254 * provide audio timestamp derived from pointer position
255 * add delay only if requested
258 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
260 if (runtime->audio_tstamp_config.report_delay) {
261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
262 audio_frames -= runtime->delay;
264 audio_frames += runtime->delay;
266 audio_nsecs = div_u64(audio_frames * 1000000000LL,
268 *audio_tstamp = ns_to_timespec(audio_nsecs);
270 runtime->status->audio_tstamp = *audio_tstamp;
271 runtime->status->tstamp = *curr_tstamp;
274 * re-take a driver timestamp to let apps detect if the reference tstamp
275 * read by low-level hardware was provided with a delay
277 snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
278 runtime->driver_tstamp = driver_tstamp;
281 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
282 unsigned int in_interrupt)
284 struct snd_pcm_runtime *runtime = substream->runtime;
285 snd_pcm_uframes_t pos;
286 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
287 snd_pcm_sframes_t hdelta, delta;
288 unsigned long jdelta;
289 unsigned long curr_jiffies;
290 struct timespec curr_tstamp;
291 struct timespec audio_tstamp;
292 int crossed_boundary = 0;
294 old_hw_ptr = runtime->status->hw_ptr;
297 * group pointer, time and jiffies reads to allow for more
298 * accurate correlations/corrections.
299 * The values are stored at the end of this routine after
300 * corrections for hw_ptr position
302 pos = substream->ops->pointer(substream);
303 curr_jiffies = jiffies;
304 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
305 if ((substream->ops->get_time_info) &&
306 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
307 substream->ops->get_time_info(substream, &curr_tstamp,
309 &runtime->audio_tstamp_config,
310 &runtime->audio_tstamp_report);
312 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
313 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
314 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
316 snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
319 if (pos == SNDRV_PCM_POS_XRUN) {
323 if (pos >= runtime->buffer_size) {
324 if (printk_ratelimit()) {
326 snd_pcm_debug_name(substream, name, sizeof(name));
327 pcm_err(substream->pcm,
328 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
329 name, pos, runtime->buffer_size,
330 runtime->period_size);
334 pos -= pos % runtime->min_align;
335 trace_hwptr(substream, pos, in_interrupt);
336 hw_base = runtime->hw_ptr_base;
337 new_hw_ptr = hw_base + pos;
339 /* we know that one period was processed */
340 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
341 delta = runtime->hw_ptr_interrupt + runtime->period_size;
342 if (delta > new_hw_ptr) {
343 /* check for double acknowledged interrupts */
344 hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
345 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
346 hw_base += runtime->buffer_size;
347 if (hw_base >= runtime->boundary) {
351 new_hw_ptr = hw_base + pos;
356 /* new_hw_ptr might be lower than old_hw_ptr in case when */
357 /* pointer crosses the end of the ring buffer */
358 if (new_hw_ptr < old_hw_ptr) {
359 hw_base += runtime->buffer_size;
360 if (hw_base >= runtime->boundary) {
364 new_hw_ptr = hw_base + pos;
367 delta = new_hw_ptr - old_hw_ptr;
369 delta += runtime->boundary;
371 if (runtime->no_period_wakeup) {
372 snd_pcm_sframes_t xrun_threshold;
374 * Without regular period interrupts, we have to check
375 * the elapsed time to detect xruns.
377 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
378 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
380 hdelta = jdelta - delta * HZ / runtime->rate;
381 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
382 while (hdelta > xrun_threshold) {
383 delta += runtime->buffer_size;
384 hw_base += runtime->buffer_size;
385 if (hw_base >= runtime->boundary) {
389 new_hw_ptr = hw_base + pos;
390 hdelta -= runtime->hw_ptr_buffer_jiffies;
395 /* something must be really wrong */
396 if (delta >= runtime->buffer_size + runtime->period_size) {
397 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
398 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
399 substream->stream, (long)pos,
400 (long)new_hw_ptr, (long)old_hw_ptr);
404 /* Do jiffies check only in xrun_debug mode */
405 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
406 goto no_jiffies_check;
408 /* Skip the jiffies check for hardwares with BATCH flag.
409 * Such hardware usually just increases the position at each IRQ,
410 * thus it can't give any strange position.
412 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
413 goto no_jiffies_check;
415 if (hdelta < runtime->delay)
416 goto no_jiffies_check;
417 hdelta -= runtime->delay;
418 jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
419 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
421 (((runtime->period_size * HZ) / runtime->rate)
423 /* move new_hw_ptr according jiffies not pos variable */
424 new_hw_ptr = old_hw_ptr;
426 /* use loop to avoid checks for delta overflows */
427 /* the delta value is small or zero in most cases */
429 new_hw_ptr += runtime->period_size;
430 if (new_hw_ptr >= runtime->boundary) {
431 new_hw_ptr -= runtime->boundary;
436 /* align hw_base to buffer_size */
437 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
438 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
439 (long)pos, (long)hdelta,
440 (long)runtime->period_size, jdelta,
441 ((hdelta * HZ) / runtime->rate), hw_base,
442 (unsigned long)old_hw_ptr,
443 (unsigned long)new_hw_ptr);
444 /* reset values to proper state */
446 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
449 if (delta > runtime->period_size + runtime->period_size / 2) {
450 hw_ptr_error(substream, in_interrupt,
452 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
453 substream->stream, (long)delta,
459 if (runtime->status->hw_ptr == new_hw_ptr) {
460 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
464 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
465 runtime->silence_size > 0)
466 snd_pcm_playback_silence(substream, new_hw_ptr);
469 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
471 delta += runtime->boundary;
472 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
473 runtime->hw_ptr_interrupt += delta;
474 if (runtime->hw_ptr_interrupt >= runtime->boundary)
475 runtime->hw_ptr_interrupt -= runtime->boundary;
477 runtime->hw_ptr_base = hw_base;
478 runtime->status->hw_ptr = new_hw_ptr;
479 runtime->hw_ptr_jiffies = curr_jiffies;
480 if (crossed_boundary) {
481 snd_BUG_ON(crossed_boundary != 1);
482 runtime->hw_ptr_wrap += runtime->boundary;
485 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
487 return snd_pcm_update_state(substream, runtime);
490 /* CAUTION: call it with irq disabled */
491 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
493 return snd_pcm_update_hw_ptr0(substream, 0);
497 * snd_pcm_set_ops - set the PCM operators
498 * @pcm: the pcm instance
499 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
500 * @ops: the operator table
502 * Sets the given PCM operators to the pcm instance.
504 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
505 const struct snd_pcm_ops *ops)
507 struct snd_pcm_str *stream = &pcm->streams[direction];
508 struct snd_pcm_substream *substream;
510 for (substream = stream->substream; substream != NULL; substream = substream->next)
511 substream->ops = ops;
514 EXPORT_SYMBOL(snd_pcm_set_ops);
517 * snd_pcm_sync - set the PCM sync id
518 * @substream: the pcm substream
520 * Sets the PCM sync identifier for the card.
522 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
524 struct snd_pcm_runtime *runtime = substream->runtime;
526 runtime->sync.id32[0] = substream->pcm->card->number;
527 runtime->sync.id32[1] = -1;
528 runtime->sync.id32[2] = -1;
529 runtime->sync.id32[3] = -1;
532 EXPORT_SYMBOL(snd_pcm_set_sync);
535 * Standard ioctl routine
538 static inline unsigned int div32(unsigned int a, unsigned int b,
549 static inline unsigned int div_down(unsigned int a, unsigned int b)
556 static inline unsigned int div_up(unsigned int a, unsigned int b)
568 static inline unsigned int mul(unsigned int a, unsigned int b)
572 if (div_down(UINT_MAX, a) < b)
577 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
578 unsigned int c, unsigned int *r)
580 u_int64_t n = (u_int64_t) a * b;
586 n = div_u64_rem(n, c, r);
595 * snd_interval_refine - refine the interval value of configurator
596 * @i: the interval value to refine
597 * @v: the interval value to refer to
599 * Refines the interval value with the reference value.
600 * The interval is changed to the range satisfying both intervals.
601 * The interval status (min, max, integer, etc.) are evaluated.
603 * Return: Positive if the value is changed, zero if it's not changed, or a
604 * negative error code.
606 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
609 if (snd_BUG_ON(snd_interval_empty(i)))
611 if (i->min < v->min) {
613 i->openmin = v->openmin;
615 } else if (i->min == v->min && !i->openmin && v->openmin) {
619 if (i->max > v->max) {
621 i->openmax = v->openmax;
623 } else if (i->max == v->max && !i->openmax && v->openmax) {
627 if (!i->integer && v->integer) {
640 } else if (!i->openmin && !i->openmax && i->min == i->max)
642 if (snd_interval_checkempty(i)) {
643 snd_interval_none(i);
649 EXPORT_SYMBOL(snd_interval_refine);
651 static int snd_interval_refine_first(struct snd_interval *i)
653 if (snd_BUG_ON(snd_interval_empty(i)))
655 if (snd_interval_single(i))
658 i->openmax = i->openmin;
664 static int snd_interval_refine_last(struct snd_interval *i)
666 if (snd_BUG_ON(snd_interval_empty(i)))
668 if (snd_interval_single(i))
671 i->openmin = i->openmax;
677 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
679 if (a->empty || b->empty) {
680 snd_interval_none(c);
684 c->min = mul(a->min, b->min);
685 c->openmin = (a->openmin || b->openmin);
686 c->max = mul(a->max, b->max);
687 c->openmax = (a->openmax || b->openmax);
688 c->integer = (a->integer && b->integer);
692 * snd_interval_div - refine the interval value with division
699 * Returns non-zero if the value is changed, zero if not changed.
701 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
704 if (a->empty || b->empty) {
705 snd_interval_none(c);
709 c->min = div32(a->min, b->max, &r);
710 c->openmin = (r || a->openmin || b->openmax);
712 c->max = div32(a->max, b->min, &r);
717 c->openmax = (a->openmax || b->openmin);
726 * snd_interval_muldivk - refine the interval value
729 * @k: divisor (as integer)
734 * Returns non-zero if the value is changed, zero if not changed.
736 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
737 unsigned int k, struct snd_interval *c)
740 if (a->empty || b->empty) {
741 snd_interval_none(c);
745 c->min = muldiv32(a->min, b->min, k, &r);
746 c->openmin = (r || a->openmin || b->openmin);
747 c->max = muldiv32(a->max, b->max, k, &r);
752 c->openmax = (a->openmax || b->openmax);
757 * snd_interval_mulkdiv - refine the interval value
759 * @k: dividend 2 (as integer)
765 * Returns non-zero if the value is changed, zero if not changed.
767 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
768 const struct snd_interval *b, struct snd_interval *c)
771 if (a->empty || b->empty) {
772 snd_interval_none(c);
776 c->min = muldiv32(a->min, k, b->max, &r);
777 c->openmin = (r || a->openmin || b->openmax);
779 c->max = muldiv32(a->max, k, b->min, &r);
784 c->openmax = (a->openmax || b->openmin);
796 * snd_interval_ratnum - refine the interval value
797 * @i: interval to refine
798 * @rats_count: number of ratnum_t
799 * @rats: ratnum_t array
800 * @nump: pointer to store the resultant numerator
801 * @denp: pointer to store the resultant denominator
803 * Return: Positive if the value is changed, zero if it's not changed, or a
804 * negative error code.
806 int snd_interval_ratnum(struct snd_interval *i,
807 unsigned int rats_count, const struct snd_ratnum *rats,
808 unsigned int *nump, unsigned int *denp)
810 unsigned int best_num, best_den;
813 struct snd_interval t;
815 unsigned int result_num, result_den;
818 best_num = best_den = best_diff = 0;
819 for (k = 0; k < rats_count; ++k) {
820 unsigned int num = rats[k].num;
822 unsigned int q = i->min;
826 den = div_up(num, q);
827 if (den < rats[k].den_min)
829 if (den > rats[k].den_max)
830 den = rats[k].den_max;
833 r = (den - rats[k].den_min) % rats[k].den_step;
837 diff = num - q * den;
841 diff * best_den < best_diff * den) {
851 t.min = div_down(best_num, best_den);
852 t.openmin = !!(best_num % best_den);
854 result_num = best_num;
855 result_diff = best_diff;
856 result_den = best_den;
857 best_num = best_den = best_diff = 0;
858 for (k = 0; k < rats_count; ++k) {
859 unsigned int num = rats[k].num;
861 unsigned int q = i->max;
867 den = div_down(num, q);
868 if (den > rats[k].den_max)
870 if (den < rats[k].den_min)
871 den = rats[k].den_min;
874 r = (den - rats[k].den_min) % rats[k].den_step;
876 den += rats[k].den_step - r;
878 diff = q * den - num;
882 diff * best_den < best_diff * den) {
892 t.max = div_up(best_num, best_den);
893 t.openmax = !!(best_num % best_den);
895 err = snd_interval_refine(i, &t);
899 if (snd_interval_single(i)) {
900 if (best_diff * result_den < result_diff * best_den) {
901 result_num = best_num;
902 result_den = best_den;
912 EXPORT_SYMBOL(snd_interval_ratnum);
915 * snd_interval_ratden - refine the interval value
916 * @i: interval to refine
917 * @rats_count: number of struct ratden
918 * @rats: struct ratden array
919 * @nump: pointer to store the resultant numerator
920 * @denp: pointer to store the resultant denominator
922 * Return: Positive if the value is changed, zero if it's not changed, or a
923 * negative error code.
925 static int snd_interval_ratden(struct snd_interval *i,
926 unsigned int rats_count,
927 const struct snd_ratden *rats,
928 unsigned int *nump, unsigned int *denp)
930 unsigned int best_num, best_diff, best_den;
932 struct snd_interval t;
935 best_num = best_den = best_diff = 0;
936 for (k = 0; k < rats_count; ++k) {
938 unsigned int den = rats[k].den;
939 unsigned int q = i->min;
942 if (num > rats[k].num_max)
944 if (num < rats[k].num_min)
945 num = rats[k].num_max;
948 r = (num - rats[k].num_min) % rats[k].num_step;
950 num += rats[k].num_step - r;
952 diff = num - q * den;
954 diff * best_den < best_diff * den) {
964 t.min = div_down(best_num, best_den);
965 t.openmin = !!(best_num % best_den);
967 best_num = best_den = best_diff = 0;
968 for (k = 0; k < rats_count; ++k) {
970 unsigned int den = rats[k].den;
971 unsigned int q = i->max;
974 if (num < rats[k].num_min)
976 if (num > rats[k].num_max)
977 num = rats[k].num_max;
980 r = (num - rats[k].num_min) % rats[k].num_step;
984 diff = q * den - num;
986 diff * best_den < best_diff * den) {
996 t.max = div_up(best_num, best_den);
997 t.openmax = !!(best_num % best_den);
999 err = snd_interval_refine(i, &t);
1003 if (snd_interval_single(i)) {
1013 * snd_interval_list - refine the interval value from the list
1014 * @i: the interval value to refine
1015 * @count: the number of elements in the list
1016 * @list: the value list
1017 * @mask: the bit-mask to evaluate
1019 * Refines the interval value from the list.
1020 * When mask is non-zero, only the elements corresponding to bit 1 are
1023 * Return: Positive if the value is changed, zero if it's not changed, or a
1024 * negative error code.
1026 int snd_interval_list(struct snd_interval *i, unsigned int count,
1027 const unsigned int *list, unsigned int mask)
1030 struct snd_interval list_range;
1036 snd_interval_any(&list_range);
1037 list_range.min = UINT_MAX;
1039 for (k = 0; k < count; k++) {
1040 if (mask && !(mask & (1 << k)))
1042 if (!snd_interval_test(i, list[k]))
1044 list_range.min = min(list_range.min, list[k]);
1045 list_range.max = max(list_range.max, list[k]);
1047 return snd_interval_refine(i, &list_range);
1050 EXPORT_SYMBOL(snd_interval_list);
1053 * snd_interval_ranges - refine the interval value from the list of ranges
1054 * @i: the interval value to refine
1055 * @count: the number of elements in the list of ranges
1056 * @ranges: the ranges list
1057 * @mask: the bit-mask to evaluate
1059 * Refines the interval value from the list of ranges.
1060 * When mask is non-zero, only the elements corresponding to bit 1 are
1063 * Return: Positive if the value is changed, zero if it's not changed, or a
1064 * negative error code.
1066 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1067 const struct snd_interval *ranges, unsigned int mask)
1070 struct snd_interval range_union;
1071 struct snd_interval range;
1074 snd_interval_none(i);
1077 snd_interval_any(&range_union);
1078 range_union.min = UINT_MAX;
1079 range_union.max = 0;
1080 for (k = 0; k < count; k++) {
1081 if (mask && !(mask & (1 << k)))
1083 snd_interval_copy(&range, &ranges[k]);
1084 if (snd_interval_refine(&range, i) < 0)
1086 if (snd_interval_empty(&range))
1089 if (range.min < range_union.min) {
1090 range_union.min = range.min;
1091 range_union.openmin = 1;
1093 if (range.min == range_union.min && !range.openmin)
1094 range_union.openmin = 0;
1095 if (range.max > range_union.max) {
1096 range_union.max = range.max;
1097 range_union.openmax = 1;
1099 if (range.max == range_union.max && !range.openmax)
1100 range_union.openmax = 0;
1102 return snd_interval_refine(i, &range_union);
1104 EXPORT_SYMBOL(snd_interval_ranges);
1106 static int snd_interval_step(struct snd_interval *i, unsigned int step)
1111 if (n != 0 || i->openmin) {
1117 if (n != 0 || i->openmax) {
1122 if (snd_interval_checkempty(i)) {
1129 /* Info constraints helpers */
1132 * snd_pcm_hw_rule_add - add the hw-constraint rule
1133 * @runtime: the pcm runtime instance
1134 * @cond: condition bits
1135 * @var: the variable to evaluate
1136 * @func: the evaluation function
1137 * @private: the private data pointer passed to function
1138 * @dep: the dependent variables
1140 * Return: Zero if successful, or a negative error code on failure.
1142 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1144 snd_pcm_hw_rule_func_t func, void *private,
1147 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1148 struct snd_pcm_hw_rule *c;
1151 va_start(args, dep);
1152 if (constrs->rules_num >= constrs->rules_all) {
1153 struct snd_pcm_hw_rule *new;
1154 unsigned int new_rules = constrs->rules_all + 16;
1155 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1160 if (constrs->rules) {
1161 memcpy(new, constrs->rules,
1162 constrs->rules_num * sizeof(*c));
1163 kfree(constrs->rules);
1165 constrs->rules = new;
1166 constrs->rules_all = new_rules;
1168 c = &constrs->rules[constrs->rules_num];
1172 c->private = private;
1175 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1182 dep = va_arg(args, int);
1184 constrs->rules_num++;
1189 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1192 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1193 * @runtime: PCM runtime instance
1194 * @var: hw_params variable to apply the mask
1195 * @mask: the bitmap mask
1197 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1199 * Return: Zero if successful, or a negative error code on failure.
1201 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1204 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1205 struct snd_mask *maskp = constrs_mask(constrs, var);
1206 *maskp->bits &= mask;
1207 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1208 if (*maskp->bits == 0)
1214 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1215 * @runtime: PCM runtime instance
1216 * @var: hw_params variable to apply the mask
1217 * @mask: the 64bit bitmap mask
1219 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1221 * Return: Zero if successful, or a negative error code on failure.
1223 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1226 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1227 struct snd_mask *maskp = constrs_mask(constrs, var);
1228 maskp->bits[0] &= (u_int32_t)mask;
1229 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1230 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1231 if (! maskp->bits[0] && ! maskp->bits[1])
1235 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
1238 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1239 * @runtime: PCM runtime instance
1240 * @var: hw_params variable to apply the integer constraint
1242 * Apply the constraint of integer to an interval parameter.
1244 * Return: Positive if the value is changed, zero if it's not changed, or a
1245 * negative error code.
1247 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1249 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1250 return snd_interval_setinteger(constrs_interval(constrs, var));
1253 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1256 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1257 * @runtime: PCM runtime instance
1258 * @var: hw_params variable to apply the range
1259 * @min: the minimal value
1260 * @max: the maximal value
1262 * Apply the min/max range constraint to an interval parameter.
1264 * Return: Positive if the value is changed, zero if it's not changed, or a
1265 * negative error code.
1267 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1268 unsigned int min, unsigned int max)
1270 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1271 struct snd_interval t;
1274 t.openmin = t.openmax = 0;
1276 return snd_interval_refine(constrs_interval(constrs, var), &t);
1279 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1281 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1282 struct snd_pcm_hw_rule *rule)
1284 struct snd_pcm_hw_constraint_list *list = rule->private;
1285 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1290 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1291 * @runtime: PCM runtime instance
1292 * @cond: condition bits
1293 * @var: hw_params variable to apply the list constraint
1296 * Apply the list of constraints to an interval parameter.
1298 * Return: Zero if successful, or a negative error code on failure.
1300 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1302 snd_pcm_hw_param_t var,
1303 const struct snd_pcm_hw_constraint_list *l)
1305 return snd_pcm_hw_rule_add(runtime, cond, var,
1306 snd_pcm_hw_rule_list, (void *)l,
1310 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1312 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
1313 struct snd_pcm_hw_rule *rule)
1315 struct snd_pcm_hw_constraint_ranges *r = rule->private;
1316 return snd_interval_ranges(hw_param_interval(params, rule->var),
1317 r->count, r->ranges, r->mask);
1322 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
1323 * @runtime: PCM runtime instance
1324 * @cond: condition bits
1325 * @var: hw_params variable to apply the list of range constraints
1328 * Apply the list of range constraints to an interval parameter.
1330 * Return: Zero if successful, or a negative error code on failure.
1332 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1334 snd_pcm_hw_param_t var,
1335 const struct snd_pcm_hw_constraint_ranges *r)
1337 return snd_pcm_hw_rule_add(runtime, cond, var,
1338 snd_pcm_hw_rule_ranges, (void *)r,
1341 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
1343 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1344 struct snd_pcm_hw_rule *rule)
1346 const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1347 unsigned int num = 0, den = 0;
1349 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1350 r->nrats, r->rats, &num, &den);
1351 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1352 params->rate_num = num;
1353 params->rate_den = den;
1359 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1360 * @runtime: PCM runtime instance
1361 * @cond: condition bits
1362 * @var: hw_params variable to apply the ratnums constraint
1363 * @r: struct snd_ratnums constriants
1365 * Return: Zero if successful, or a negative error code on failure.
1367 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1369 snd_pcm_hw_param_t var,
1370 const struct snd_pcm_hw_constraint_ratnums *r)
1372 return snd_pcm_hw_rule_add(runtime, cond, var,
1373 snd_pcm_hw_rule_ratnums, (void *)r,
1377 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1379 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1380 struct snd_pcm_hw_rule *rule)
1382 const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1383 unsigned int num = 0, den = 0;
1384 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1385 r->nrats, r->rats, &num, &den);
1386 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1387 params->rate_num = num;
1388 params->rate_den = den;
1394 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1395 * @runtime: PCM runtime instance
1396 * @cond: condition bits
1397 * @var: hw_params variable to apply the ratdens constraint
1398 * @r: struct snd_ratdens constriants
1400 * Return: Zero if successful, or a negative error code on failure.
1402 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1404 snd_pcm_hw_param_t var,
1405 const struct snd_pcm_hw_constraint_ratdens *r)
1407 return snd_pcm_hw_rule_add(runtime, cond, var,
1408 snd_pcm_hw_rule_ratdens, (void *)r,
1412 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1414 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1415 struct snd_pcm_hw_rule *rule)
1417 unsigned int l = (unsigned long) rule->private;
1418 int width = l & 0xffff;
1419 unsigned int msbits = l >> 16;
1420 const struct snd_interval *i =
1421 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1423 if (!snd_interval_single(i))
1426 if ((snd_interval_value(i) == width) ||
1427 (width == 0 && snd_interval_value(i) > msbits))
1428 params->msbits = min_not_zero(params->msbits, msbits);
1434 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1435 * @runtime: PCM runtime instance
1436 * @cond: condition bits
1437 * @width: sample bits width
1438 * @msbits: msbits width
1440 * This constraint will set the number of most significant bits (msbits) if a
1441 * sample format with the specified width has been select. If width is set to 0
1442 * the msbits will be set for any sample format with a width larger than the
1445 * Return: Zero if successful, or a negative error code on failure.
1447 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1450 unsigned int msbits)
1452 unsigned long l = (msbits << 16) | width;
1453 return snd_pcm_hw_rule_add(runtime, cond, -1,
1454 snd_pcm_hw_rule_msbits,
1456 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1459 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1461 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1462 struct snd_pcm_hw_rule *rule)
1464 unsigned long step = (unsigned long) rule->private;
1465 return snd_interval_step(hw_param_interval(params, rule->var), step);
1469 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1470 * @runtime: PCM runtime instance
1471 * @cond: condition bits
1472 * @var: hw_params variable to apply the step constraint
1475 * Return: Zero if successful, or a negative error code on failure.
1477 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1479 snd_pcm_hw_param_t var,
1482 return snd_pcm_hw_rule_add(runtime, cond, var,
1483 snd_pcm_hw_rule_step, (void *) step,
1487 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1489 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1491 static unsigned int pow2_sizes[] = {
1492 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1493 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1494 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1495 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1497 return snd_interval_list(hw_param_interval(params, rule->var),
1498 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1502 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1503 * @runtime: PCM runtime instance
1504 * @cond: condition bits
1505 * @var: hw_params variable to apply the power-of-2 constraint
1507 * Return: Zero if successful, or a negative error code on failure.
1509 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1511 snd_pcm_hw_param_t var)
1513 return snd_pcm_hw_rule_add(runtime, cond, var,
1514 snd_pcm_hw_rule_pow2, NULL,
1518 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1520 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
1521 struct snd_pcm_hw_rule *rule)
1523 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
1524 struct snd_interval *rate;
1526 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1527 return snd_interval_list(rate, 1, &base_rate, 0);
1531 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
1532 * @runtime: PCM runtime instance
1533 * @base_rate: the rate at which the hardware does not resample
1535 * Return: Zero if successful, or a negative error code on failure.
1537 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1538 unsigned int base_rate)
1540 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
1541 SNDRV_PCM_HW_PARAM_RATE,
1542 snd_pcm_hw_rule_noresample_func,
1543 (void *)(uintptr_t)base_rate,
1544 SNDRV_PCM_HW_PARAM_RATE, -1);
1546 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
1548 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1549 snd_pcm_hw_param_t var)
1551 if (hw_is_mask(var)) {
1552 snd_mask_any(hw_param_mask(params, var));
1553 params->cmask |= 1 << var;
1554 params->rmask |= 1 << var;
1557 if (hw_is_interval(var)) {
1558 snd_interval_any(hw_param_interval(params, var));
1559 params->cmask |= 1 << var;
1560 params->rmask |= 1 << var;
1566 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1569 memset(params, 0, sizeof(*params));
1570 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1571 _snd_pcm_hw_param_any(params, k);
1572 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1573 _snd_pcm_hw_param_any(params, k);
1577 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1580 * snd_pcm_hw_param_value - return @params field @var value
1581 * @params: the hw_params instance
1582 * @var: parameter to retrieve
1583 * @dir: pointer to the direction (-1,0,1) or %NULL
1585 * Return: The value for field @var if it's fixed in configuration space
1586 * defined by @params. -%EINVAL otherwise.
1588 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1589 snd_pcm_hw_param_t var, int *dir)
1591 if (hw_is_mask(var)) {
1592 const struct snd_mask *mask = hw_param_mask_c(params, var);
1593 if (!snd_mask_single(mask))
1597 return snd_mask_value(mask);
1599 if (hw_is_interval(var)) {
1600 const struct snd_interval *i = hw_param_interval_c(params, var);
1601 if (!snd_interval_single(i))
1605 return snd_interval_value(i);
1610 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1612 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1613 snd_pcm_hw_param_t var)
1615 if (hw_is_mask(var)) {
1616 snd_mask_none(hw_param_mask(params, var));
1617 params->cmask |= 1 << var;
1618 params->rmask |= 1 << var;
1619 } else if (hw_is_interval(var)) {
1620 snd_interval_none(hw_param_interval(params, var));
1621 params->cmask |= 1 << var;
1622 params->rmask |= 1 << var;
1628 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1630 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1631 snd_pcm_hw_param_t var)
1634 if (hw_is_mask(var))
1635 changed = snd_mask_refine_first(hw_param_mask(params, var));
1636 else if (hw_is_interval(var))
1637 changed = snd_interval_refine_first(hw_param_interval(params, var));
1641 params->cmask |= 1 << var;
1642 params->rmask |= 1 << var;
1649 * snd_pcm_hw_param_first - refine config space and return minimum value
1650 * @pcm: PCM instance
1651 * @params: the hw_params instance
1652 * @var: parameter to retrieve
1653 * @dir: pointer to the direction (-1,0,1) or %NULL
1655 * Inside configuration space defined by @params remove from @var all
1656 * values > minimum. Reduce configuration space accordingly.
1658 * Return: The minimum, or a negative error code on failure.
1660 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1661 struct snd_pcm_hw_params *params,
1662 snd_pcm_hw_param_t var, int *dir)
1664 int changed = _snd_pcm_hw_param_first(params, var);
1667 if (params->rmask) {
1668 int err = snd_pcm_hw_refine(pcm, params);
1669 if (snd_BUG_ON(err < 0))
1672 return snd_pcm_hw_param_value(params, var, dir);
1675 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1677 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1678 snd_pcm_hw_param_t var)
1681 if (hw_is_mask(var))
1682 changed = snd_mask_refine_last(hw_param_mask(params, var));
1683 else if (hw_is_interval(var))
1684 changed = snd_interval_refine_last(hw_param_interval(params, var));
1688 params->cmask |= 1 << var;
1689 params->rmask |= 1 << var;
1696 * snd_pcm_hw_param_last - refine config space and return maximum value
1697 * @pcm: PCM instance
1698 * @params: the hw_params instance
1699 * @var: parameter to retrieve
1700 * @dir: pointer to the direction (-1,0,1) or %NULL
1702 * Inside configuration space defined by @params remove from @var all
1703 * values < maximum. Reduce configuration space accordingly.
1705 * Return: The maximum, or a negative error code on failure.
1707 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1708 struct snd_pcm_hw_params *params,
1709 snd_pcm_hw_param_t var, int *dir)
1711 int changed = _snd_pcm_hw_param_last(params, var);
1714 if (params->rmask) {
1715 int err = snd_pcm_hw_refine(pcm, params);
1716 if (snd_BUG_ON(err < 0))
1719 return snd_pcm_hw_param_value(params, var, dir);
1722 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1725 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1726 * @pcm: PCM instance
1727 * @params: the hw_params instance
1729 * Choose one configuration from configuration space defined by @params.
1730 * The configuration chosen is that obtained fixing in this order:
1731 * first access, first format, first subformat, min channels,
1732 * min rate, min period time, max buffer size, min tick time
1734 * Return: Zero if successful, or a negative error code on failure.
1736 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1737 struct snd_pcm_hw_params *params)
1739 static const int vars[] = {
1740 SNDRV_PCM_HW_PARAM_ACCESS,
1741 SNDRV_PCM_HW_PARAM_FORMAT,
1742 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1743 SNDRV_PCM_HW_PARAM_CHANNELS,
1744 SNDRV_PCM_HW_PARAM_RATE,
1745 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1746 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1747 SNDRV_PCM_HW_PARAM_TICK_TIME,
1753 for (v = vars; *v != -1; v++) {
1754 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1755 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1757 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1758 if (snd_BUG_ON(err < 0))
1764 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1767 struct snd_pcm_runtime *runtime = substream->runtime;
1768 unsigned long flags;
1769 snd_pcm_stream_lock_irqsave(substream, flags);
1770 if (snd_pcm_running(substream) &&
1771 snd_pcm_update_hw_ptr(substream) >= 0)
1772 runtime->status->hw_ptr %= runtime->buffer_size;
1774 runtime->status->hw_ptr = 0;
1775 runtime->hw_ptr_wrap = 0;
1777 snd_pcm_stream_unlock_irqrestore(substream, flags);
1781 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1784 struct snd_pcm_channel_info *info = arg;
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1787 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1791 width = snd_pcm_format_physical_width(runtime->format);
1795 switch (runtime->access) {
1796 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1797 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1798 info->first = info->channel * width;
1799 info->step = runtime->channels * width;
1801 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1802 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1804 size_t size = runtime->dma_bytes / runtime->channels;
1805 info->first = info->channel * size * 8;
1816 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1819 struct snd_pcm_hw_params *params = arg;
1820 snd_pcm_format_t format;
1824 params->fifo_size = substream->runtime->hw.fifo_size;
1825 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1826 format = params_format(params);
1827 channels = params_channels(params);
1828 frame_size = snd_pcm_format_size(format, channels);
1830 params->fifo_size /= (unsigned)frame_size;
1836 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1837 * @substream: the pcm substream instance
1838 * @cmd: ioctl command
1839 * @arg: ioctl argument
1841 * Processes the generic ioctl commands for PCM.
1842 * Can be passed as the ioctl callback for PCM ops.
1844 * Return: Zero if successful, or a negative error code on failure.
1846 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1847 unsigned int cmd, void *arg)
1850 case SNDRV_PCM_IOCTL1_INFO:
1852 case SNDRV_PCM_IOCTL1_RESET:
1853 return snd_pcm_lib_ioctl_reset(substream, arg);
1854 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1855 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1856 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1857 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1862 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1865 * snd_pcm_period_elapsed - update the pcm status for the next period
1866 * @substream: the pcm substream instance
1868 * This function is called from the interrupt handler when the
1869 * PCM has processed the period size. It will update the current
1870 * pointer, wake up sleepers, etc.
1872 * Even if more than one periods have elapsed since the last call, you
1873 * have to call this only once.
1875 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1877 struct snd_pcm_runtime *runtime;
1878 unsigned long flags;
1880 if (PCM_RUNTIME_CHECK(substream))
1882 runtime = substream->runtime;
1884 snd_pcm_stream_lock_irqsave(substream, flags);
1885 if (!snd_pcm_running(substream) ||
1886 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1889 #ifdef CONFIG_SND_PCM_TIMER
1890 if (substream->timer_running)
1891 snd_timer_interrupt(substream->timer, 1);
1894 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1895 snd_pcm_stream_unlock_irqrestore(substream, flags);
1898 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1901 * Wait until avail_min data becomes available
1902 * Returns a negative error code if any error occurs during operation.
1903 * The available space is stored on availp. When err = 0 and avail = 0
1904 * on the capture stream, it indicates the stream is in DRAINING state.
1906 static int wait_for_avail(struct snd_pcm_substream *substream,
1907 snd_pcm_uframes_t *availp)
1909 struct snd_pcm_runtime *runtime = substream->runtime;
1910 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1913 snd_pcm_uframes_t avail = 0;
1914 long wait_time, tout;
1916 init_waitqueue_entry(&wait, current);
1917 set_current_state(TASK_INTERRUPTIBLE);
1918 add_wait_queue(&runtime->tsleep, &wait);
1920 if (runtime->no_period_wakeup)
1921 wait_time = MAX_SCHEDULE_TIMEOUT;
1924 if (runtime->rate) {
1925 long t = runtime->period_size * 2 / runtime->rate;
1926 wait_time = max(t, wait_time);
1928 wait_time = msecs_to_jiffies(wait_time * 1000);
1932 if (signal_pending(current)) {
1938 * We need to check if space became available already
1939 * (and thus the wakeup happened already) first to close
1940 * the race of space already having become available.
1941 * This check must happen after been added to the waitqueue
1942 * and having current state be INTERRUPTIBLE.
1945 avail = snd_pcm_playback_avail(runtime);
1947 avail = snd_pcm_capture_avail(runtime);
1948 if (avail >= runtime->twake)
1950 snd_pcm_stream_unlock_irq(substream);
1952 tout = schedule_timeout(wait_time);
1954 snd_pcm_stream_lock_irq(substream);
1955 set_current_state(TASK_INTERRUPTIBLE);
1956 switch (runtime->status->state) {
1957 case SNDRV_PCM_STATE_SUSPENDED:
1960 case SNDRV_PCM_STATE_XRUN:
1963 case SNDRV_PCM_STATE_DRAINING:
1967 avail = 0; /* indicate draining */
1969 case SNDRV_PCM_STATE_OPEN:
1970 case SNDRV_PCM_STATE_SETUP:
1971 case SNDRV_PCM_STATE_DISCONNECTED:
1974 case SNDRV_PCM_STATE_PAUSED:
1978 pcm_dbg(substream->pcm,
1979 "%s write error (DMA or IRQ trouble?)\n",
1980 is_playback ? "playback" : "capture");
1986 set_current_state(TASK_RUNNING);
1987 remove_wait_queue(&runtime->tsleep, &wait);
1992 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1994 unsigned long data, unsigned int off,
1995 snd_pcm_uframes_t frames)
1997 struct snd_pcm_runtime *runtime = substream->runtime;
1999 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2000 if (substream->ops->copy) {
2001 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2004 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2005 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2011 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
2012 unsigned long data, unsigned int off,
2013 snd_pcm_uframes_t size);
2015 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
2017 snd_pcm_uframes_t size,
2019 transfer_f transfer)
2021 struct snd_pcm_runtime *runtime = substream->runtime;
2022 snd_pcm_uframes_t xfer = 0;
2023 snd_pcm_uframes_t offset = 0;
2024 snd_pcm_uframes_t avail;
2030 snd_pcm_stream_lock_irq(substream);
2031 switch (runtime->status->state) {
2032 case SNDRV_PCM_STATE_PREPARED:
2033 case SNDRV_PCM_STATE_RUNNING:
2034 case SNDRV_PCM_STATE_PAUSED:
2036 case SNDRV_PCM_STATE_XRUN:
2039 case SNDRV_PCM_STATE_SUSPENDED:
2047 runtime->twake = runtime->control->avail_min ? : 1;
2048 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2049 snd_pcm_update_hw_ptr(substream);
2050 avail = snd_pcm_playback_avail(runtime);
2052 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2053 snd_pcm_uframes_t cont;
2059 runtime->twake = min_t(snd_pcm_uframes_t, size,
2060 runtime->control->avail_min ? : 1);
2061 err = wait_for_avail(substream, &avail);
2065 frames = size > avail ? avail : size;
2066 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2069 if (snd_BUG_ON(!frames)) {
2071 snd_pcm_stream_unlock_irq(substream);
2074 appl_ptr = runtime->control->appl_ptr;
2075 appl_ofs = appl_ptr % runtime->buffer_size;
2076 snd_pcm_stream_unlock_irq(substream);
2077 err = transfer(substream, appl_ofs, data, offset, frames);
2078 snd_pcm_stream_lock_irq(substream);
2081 switch (runtime->status->state) {
2082 case SNDRV_PCM_STATE_XRUN:
2085 case SNDRV_PCM_STATE_SUSPENDED:
2092 if (appl_ptr >= runtime->boundary)
2093 appl_ptr -= runtime->boundary;
2094 runtime->control->appl_ptr = appl_ptr;
2095 if (substream->ops->ack)
2096 substream->ops->ack(substream);
2102 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2103 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2104 err = snd_pcm_start(substream);
2111 if (xfer > 0 && err >= 0)
2112 snd_pcm_update_state(substream, runtime);
2113 snd_pcm_stream_unlock_irq(substream);
2114 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2117 /* sanity-check for read/write methods */
2118 static int pcm_sanity_check(struct snd_pcm_substream *substream)
2120 struct snd_pcm_runtime *runtime;
2121 if (PCM_RUNTIME_CHECK(substream))
2123 runtime = substream->runtime;
2124 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
2126 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2131 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
2133 struct snd_pcm_runtime *runtime;
2137 err = pcm_sanity_check(substream);
2140 runtime = substream->runtime;
2141 nonblock = !!(substream->f_flags & O_NONBLOCK);
2143 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2144 runtime->channels > 1)
2146 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2147 snd_pcm_lib_write_transfer);
2150 EXPORT_SYMBOL(snd_pcm_lib_write);
2152 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
2154 unsigned long data, unsigned int off,
2155 snd_pcm_uframes_t frames)
2157 struct snd_pcm_runtime *runtime = substream->runtime;
2159 void __user **bufs = (void __user **)data;
2160 int channels = runtime->channels;
2162 if (substream->ops->copy) {
2163 if (snd_BUG_ON(!substream->ops->silence))
2165 for (c = 0; c < channels; ++c, ++bufs) {
2166 if (*bufs == NULL) {
2167 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2170 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2171 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2176 /* default transfer behaviour */
2177 size_t dma_csize = runtime->dma_bytes / channels;
2178 for (c = 0; c < channels; ++c, ++bufs) {
2179 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2180 if (*bufs == NULL) {
2181 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2183 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2184 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2192 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
2194 snd_pcm_uframes_t frames)
2196 struct snd_pcm_runtime *runtime;
2200 err = pcm_sanity_check(substream);
2203 runtime = substream->runtime;
2204 nonblock = !!(substream->f_flags & O_NONBLOCK);
2206 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2208 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2209 nonblock, snd_pcm_lib_writev_transfer);
2212 EXPORT_SYMBOL(snd_pcm_lib_writev);
2214 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2216 unsigned long data, unsigned int off,
2217 snd_pcm_uframes_t frames)
2219 struct snd_pcm_runtime *runtime = substream->runtime;
2221 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2222 if (substream->ops->copy) {
2223 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2226 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2227 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2233 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2235 snd_pcm_uframes_t size,
2237 transfer_f transfer)
2239 struct snd_pcm_runtime *runtime = substream->runtime;
2240 snd_pcm_uframes_t xfer = 0;
2241 snd_pcm_uframes_t offset = 0;
2242 snd_pcm_uframes_t avail;
2248 snd_pcm_stream_lock_irq(substream);
2249 switch (runtime->status->state) {
2250 case SNDRV_PCM_STATE_PREPARED:
2251 if (size >= runtime->start_threshold) {
2252 err = snd_pcm_start(substream);
2257 case SNDRV_PCM_STATE_DRAINING:
2258 case SNDRV_PCM_STATE_RUNNING:
2259 case SNDRV_PCM_STATE_PAUSED:
2261 case SNDRV_PCM_STATE_XRUN:
2264 case SNDRV_PCM_STATE_SUSPENDED:
2272 runtime->twake = runtime->control->avail_min ? : 1;
2273 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2274 snd_pcm_update_hw_ptr(substream);
2275 avail = snd_pcm_capture_avail(runtime);
2277 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2278 snd_pcm_uframes_t cont;
2280 if (runtime->status->state ==
2281 SNDRV_PCM_STATE_DRAINING) {
2282 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2289 runtime->twake = min_t(snd_pcm_uframes_t, size,
2290 runtime->control->avail_min ? : 1);
2291 err = wait_for_avail(substream, &avail);
2295 continue; /* draining */
2297 frames = size > avail ? avail : size;
2298 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2301 if (snd_BUG_ON(!frames)) {
2303 snd_pcm_stream_unlock_irq(substream);
2306 appl_ptr = runtime->control->appl_ptr;
2307 appl_ofs = appl_ptr % runtime->buffer_size;
2308 snd_pcm_stream_unlock_irq(substream);
2309 err = transfer(substream, appl_ofs, data, offset, frames);
2310 snd_pcm_stream_lock_irq(substream);
2313 switch (runtime->status->state) {
2314 case SNDRV_PCM_STATE_XRUN:
2317 case SNDRV_PCM_STATE_SUSPENDED:
2324 if (appl_ptr >= runtime->boundary)
2325 appl_ptr -= runtime->boundary;
2326 runtime->control->appl_ptr = appl_ptr;
2327 if (substream->ops->ack)
2328 substream->ops->ack(substream);
2337 if (xfer > 0 && err >= 0)
2338 snd_pcm_update_state(substream, runtime);
2339 snd_pcm_stream_unlock_irq(substream);
2340 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2343 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2345 struct snd_pcm_runtime *runtime;
2349 err = pcm_sanity_check(substream);
2352 runtime = substream->runtime;
2353 nonblock = !!(substream->f_flags & O_NONBLOCK);
2354 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2356 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2359 EXPORT_SYMBOL(snd_pcm_lib_read);
2361 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2363 unsigned long data, unsigned int off,
2364 snd_pcm_uframes_t frames)
2366 struct snd_pcm_runtime *runtime = substream->runtime;
2368 void __user **bufs = (void __user **)data;
2369 int channels = runtime->channels;
2371 if (substream->ops->copy) {
2372 for (c = 0; c < channels; ++c, ++bufs) {
2376 buf = *bufs + samples_to_bytes(runtime, off);
2377 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2381 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2382 for (c = 0; c < channels; ++c, ++bufs) {
2388 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2389 buf = *bufs + samples_to_bytes(runtime, off);
2390 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2397 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2399 snd_pcm_uframes_t frames)
2401 struct snd_pcm_runtime *runtime;
2405 err = pcm_sanity_check(substream);
2408 runtime = substream->runtime;
2409 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2412 nonblock = !!(substream->f_flags & O_NONBLOCK);
2413 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2415 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2418 EXPORT_SYMBOL(snd_pcm_lib_readv);
2421 * standard channel mapping helpers
2424 /* default channel maps for multi-channel playbacks, up to 8 channels */
2425 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2427 .map = { SNDRV_CHMAP_MONO } },
2429 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2431 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2432 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2434 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2435 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2436 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2438 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2439 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2440 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2441 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2444 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2446 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2447 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2449 .map = { SNDRV_CHMAP_MONO } },
2451 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2453 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2454 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2456 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2457 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2458 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2460 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2461 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2462 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2463 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2466 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2468 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2470 if (ch > info->max_channels)
2472 return !info->channel_mask || (info->channel_mask & (1U << ch));
2475 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2476 struct snd_ctl_elem_info *uinfo)
2478 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2480 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2482 uinfo->count = info->max_channels;
2483 uinfo->value.integer.min = 0;
2484 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2488 /* get callback for channel map ctl element
2489 * stores the channel position firstly matching with the current channels
2491 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2494 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2495 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2496 struct snd_pcm_substream *substream;
2497 const struct snd_pcm_chmap_elem *map;
2499 if (snd_BUG_ON(!info->chmap))
2501 substream = snd_pcm_chmap_substream(info, idx);
2504 memset(ucontrol->value.integer.value, 0,
2505 sizeof(ucontrol->value.integer.value));
2506 if (!substream->runtime)
2507 return 0; /* no channels set */
2508 for (map = info->chmap; map->channels; map++) {
2510 if (map->channels == substream->runtime->channels &&
2511 valid_chmap_channels(info, map->channels)) {
2512 for (i = 0; i < map->channels; i++)
2513 ucontrol->value.integer.value[i] = map->map[i];
2520 /* tlv callback for channel map ctl element
2521 * expands the pre-defined channel maps in a form of TLV
2523 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2524 unsigned int size, unsigned int __user *tlv)
2526 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2527 const struct snd_pcm_chmap_elem *map;
2528 unsigned int __user *dst;
2531 if (snd_BUG_ON(!info->chmap))
2535 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2539 for (map = info->chmap; map->channels; map++) {
2540 int chs_bytes = map->channels * 4;
2541 if (!valid_chmap_channels(info, map->channels))
2545 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2546 put_user(chs_bytes, dst + 1))
2551 if (size < chs_bytes)
2555 for (c = 0; c < map->channels; c++) {
2556 if (put_user(map->map[c], dst))
2561 if (put_user(count, tlv + 1))
2566 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2568 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2569 info->pcm->streams[info->stream].chmap_kctl = NULL;
2574 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2575 * @pcm: the assigned PCM instance
2576 * @stream: stream direction
2577 * @chmap: channel map elements (for query)
2578 * @max_channels: the max number of channels for the stream
2579 * @private_value: the value passed to each kcontrol's private_value field
2580 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2582 * Create channel-mapping control elements assigned to the given PCM stream(s).
2583 * Return: Zero if successful, or a negative error value.
2585 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2586 const struct snd_pcm_chmap_elem *chmap,
2588 unsigned long private_value,
2589 struct snd_pcm_chmap **info_ret)
2591 struct snd_pcm_chmap *info;
2592 struct snd_kcontrol_new knew = {
2593 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2594 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2595 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2596 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2597 .info = pcm_chmap_ctl_info,
2598 .get = pcm_chmap_ctl_get,
2599 .tlv.c = pcm_chmap_ctl_tlv,
2603 if (WARN_ON(pcm->streams[stream].chmap_kctl))
2605 info = kzalloc(sizeof(*info), GFP_KERNEL);
2609 info->stream = stream;
2610 info->chmap = chmap;
2611 info->max_channels = max_channels;
2612 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2613 knew.name = "Playback Channel Map";
2615 knew.name = "Capture Channel Map";
2616 knew.device = pcm->device;
2617 knew.count = pcm->streams[stream].substream_count;
2618 knew.private_value = private_value;
2619 info->kctl = snd_ctl_new1(&knew, info);
2624 info->kctl->private_free = pcm_chmap_ctl_private_free;
2625 err = snd_ctl_add(pcm->card, info->kctl);
2628 pcm->streams[stream].chmap_kctl = info->kctl;
2633 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);