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/time.h>
25 #include <linux/math64.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
42 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
44 struct snd_pcm_runtime *runtime = substream->runtime;
45 snd_pcm_uframes_t frames, ofs, transfer;
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
59 if (runtime->silence_filled >= runtime->buffer_size)
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 if (avail > runtime->buffer_size)
71 avail = runtime->buffer_size;
72 runtime->silence_filled = avail > 0 ? avail : 0;
73 runtime->silence_start = (runtime->status->hw_ptr +
74 runtime->silence_filled) %
77 ofs = runtime->status->hw_ptr;
78 frames = new_hw_ptr - ofs;
79 if ((snd_pcm_sframes_t)frames < 0)
80 frames += runtime->boundary;
81 runtime->silence_filled -= frames;
82 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
83 runtime->silence_filled = 0;
84 runtime->silence_start = new_hw_ptr;
86 runtime->silence_start = ofs;
89 frames = runtime->buffer_size - runtime->silence_filled;
91 if (snd_BUG_ON(frames > runtime->buffer_size))
95 ofs = runtime->silence_start % runtime->buffer_size;
97 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
98 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
99 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
100 if (substream->ops->silence) {
102 err = substream->ops->silence(substream, -1, ofs, transfer);
105 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
106 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
110 unsigned int channels = runtime->channels;
111 if (substream->ops->silence) {
112 for (c = 0; c < channels; ++c) {
114 err = substream->ops->silence(substream, c, ofs, transfer);
118 size_t dma_csize = runtime->dma_bytes / channels;
119 for (c = 0; c < channels; ++c) {
120 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
121 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
125 runtime->silence_filled += transfer;
131 static void pcm_debug_name(struct snd_pcm_substream *substream,
132 char *name, size_t len)
134 snprintf(name, len, "pcmC%dD%d%c:%d",
135 substream->pcm->card->number,
136 substream->pcm->device,
137 substream->stream ? 'c' : 'p',
141 #define XRUN_DEBUG_BASIC (1<<0)
142 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
143 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
144 #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
145 #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
146 #define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
147 #define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
149 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
151 #define xrun_debug(substream, mask) \
152 ((substream)->pstr->xrun_debug & (mask))
154 #define xrun_debug(substream, mask) 0
157 #define dump_stack_on_xrun(substream) do { \
158 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
162 static void xrun(struct snd_pcm_substream *substream)
164 struct snd_pcm_runtime *runtime = substream->runtime;
166 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
167 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
168 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
169 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
171 pcm_debug_name(substream, name, sizeof(name));
172 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
173 dump_stack_on_xrun(substream);
177 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
178 #define hw_ptr_error(substream, fmt, args...) \
180 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
181 xrun_log_show(substream); \
182 if (printk_ratelimit()) { \
183 snd_printd("PCM: " fmt, ##args); \
185 dump_stack_on_xrun(substream); \
189 #define XRUN_LOG_CNT 10
191 struct hwptr_log_entry {
192 unsigned int in_interrupt;
193 unsigned long jiffies;
194 snd_pcm_uframes_t pos;
195 snd_pcm_uframes_t period_size;
196 snd_pcm_uframes_t buffer_size;
197 snd_pcm_uframes_t old_hw_ptr;
198 snd_pcm_uframes_t hw_ptr_base;
201 struct snd_pcm_hwptr_log {
204 struct hwptr_log_entry entries[XRUN_LOG_CNT];
207 static void xrun_log(struct snd_pcm_substream *substream,
208 snd_pcm_uframes_t pos, int in_interrupt)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
212 struct hwptr_log_entry *entry;
215 log = kzalloc(sizeof(*log), GFP_ATOMIC);
218 runtime->hwptr_log = log;
220 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
223 entry = &log->entries[log->idx];
224 entry->in_interrupt = in_interrupt;
225 entry->jiffies = jiffies;
227 entry->period_size = runtime->period_size;
228 entry->buffer_size = runtime->buffer_size;
229 entry->old_hw_ptr = runtime->status->hw_ptr;
230 entry->hw_ptr_base = runtime->hw_ptr_base;
231 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
234 static void xrun_log_show(struct snd_pcm_substream *substream)
236 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
237 struct hwptr_log_entry *entry;
244 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
246 pcm_debug_name(substream, name, sizeof(name));
247 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
248 entry = &log->entries[idx];
249 if (entry->period_size == 0)
251 snd_printd("hwptr log: %s: %sj=%lu, pos=%ld/%ld/%ld, "
253 name, entry->in_interrupt ? "[Q] " : "",
255 (unsigned long)entry->pos,
256 (unsigned long)entry->period_size,
257 (unsigned long)entry->buffer_size,
258 (unsigned long)entry->old_hw_ptr,
259 (unsigned long)entry->hw_ptr_base);
266 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
268 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
269 #define xrun_log(substream, pos, in_interrupt) do { } while (0)
270 #define xrun_log_show(substream) do { } while (0)
274 int snd_pcm_update_state(struct snd_pcm_substream *substream,
275 struct snd_pcm_runtime *runtime)
277 snd_pcm_uframes_t avail;
279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
280 avail = snd_pcm_playback_avail(runtime);
282 avail = snd_pcm_capture_avail(runtime);
283 if (avail > runtime->avail_max)
284 runtime->avail_max = avail;
285 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
286 if (avail >= runtime->buffer_size) {
287 snd_pcm_drain_done(substream);
291 if (avail >= runtime->stop_threshold) {
296 if (runtime->twake) {
297 if (avail >= runtime->twake)
298 wake_up(&runtime->tsleep);
299 } else if (avail >= runtime->control->avail_min)
300 wake_up(&runtime->sleep);
304 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
305 unsigned int in_interrupt)
307 struct snd_pcm_runtime *runtime = substream->runtime;
308 snd_pcm_uframes_t pos;
309 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
310 snd_pcm_sframes_t hdelta, delta;
311 unsigned long jdelta;
313 old_hw_ptr = runtime->status->hw_ptr;
314 pos = substream->ops->pointer(substream);
315 if (pos == SNDRV_PCM_POS_XRUN) {
319 if (pos >= runtime->buffer_size) {
320 if (printk_ratelimit()) {
322 pcm_debug_name(substream, name, sizeof(name));
323 xrun_log_show(substream);
324 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
325 "buffer size = %ld, period size = %ld\n",
326 name, pos, runtime->buffer_size,
327 runtime->period_size);
331 pos -= pos % runtime->min_align;
332 if (xrun_debug(substream, XRUN_DEBUG_LOG))
333 xrun_log(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 = jiffies - runtime->hw_ptr_jiffies;
343 if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
344 hw_base += runtime->buffer_size;
345 if (hw_base >= runtime->boundary)
347 new_hw_ptr = hw_base + pos;
352 /* new_hw_ptr might be lower than old_hw_ptr in case when */
353 /* pointer crosses the end of the ring buffer */
354 if (new_hw_ptr < old_hw_ptr) {
355 hw_base += runtime->buffer_size;
356 if (hw_base >= runtime->boundary)
358 new_hw_ptr = hw_base + pos;
361 delta = new_hw_ptr - old_hw_ptr;
363 delta += runtime->boundary;
364 if (xrun_debug(substream, in_interrupt ?
365 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
367 pcm_debug_name(substream, name, sizeof(name));
368 snd_printd("%s_update: %s: pos=%u/%u/%u, "
369 "hwptr=%ld/%ld/%ld/%ld\n",
370 in_interrupt ? "period" : "hwptr",
373 (unsigned int)runtime->period_size,
374 (unsigned int)runtime->buffer_size,
375 (unsigned long)delta,
376 (unsigned long)old_hw_ptr,
377 (unsigned long)new_hw_ptr,
378 (unsigned long)runtime->hw_ptr_base);
381 if (runtime->no_period_wakeup) {
382 snd_pcm_sframes_t xrun_threshold;
384 * Without regular period interrupts, we have to check
385 * the elapsed time to detect xruns.
387 jdelta = jiffies - runtime->hw_ptr_jiffies;
388 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
390 hdelta = jdelta - delta * HZ / runtime->rate;
391 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
392 while (hdelta > xrun_threshold) {
393 delta += runtime->buffer_size;
394 hw_base += runtime->buffer_size;
395 if (hw_base >= runtime->boundary)
397 new_hw_ptr = hw_base + pos;
398 hdelta -= runtime->hw_ptr_buffer_jiffies;
403 /* something must be really wrong */
404 if (delta >= runtime->buffer_size + runtime->period_size) {
405 hw_ptr_error(substream,
406 "Unexpected hw_pointer value %s"
407 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
409 in_interrupt ? "[Q] " : "[P]",
410 substream->stream, (long)pos,
411 (long)new_hw_ptr, (long)old_hw_ptr);
415 /* Do jiffies check only in xrun_debug mode */
416 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
417 goto no_jiffies_check;
419 /* Skip the jiffies check for hardwares with BATCH flag.
420 * Such hardware usually just increases the position at each IRQ,
421 * thus it can't give any strange position.
423 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
424 goto no_jiffies_check;
426 if (hdelta < runtime->delay)
427 goto no_jiffies_check;
428 hdelta -= runtime->delay;
429 jdelta = jiffies - runtime->hw_ptr_jiffies;
430 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
432 (((runtime->period_size * HZ) / runtime->rate)
434 /* move new_hw_ptr according jiffies not pos variable */
435 new_hw_ptr = old_hw_ptr;
437 /* use loop to avoid checks for delta overflows */
438 /* the delta value is small or zero in most cases */
440 new_hw_ptr += runtime->period_size;
441 if (new_hw_ptr >= runtime->boundary)
442 new_hw_ptr -= runtime->boundary;
445 /* align hw_base to buffer_size */
446 hw_ptr_error(substream,
447 "hw_ptr skipping! %s"
448 "(pos=%ld, delta=%ld, period=%ld, "
449 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
450 in_interrupt ? "[Q] " : "",
451 (long)pos, (long)hdelta,
452 (long)runtime->period_size, jdelta,
453 ((hdelta * HZ) / runtime->rate), hw_base,
454 (unsigned long)old_hw_ptr,
455 (unsigned long)new_hw_ptr);
456 /* reset values to proper state */
458 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
461 if (delta > runtime->period_size + runtime->period_size / 2) {
462 hw_ptr_error(substream,
463 "Lost interrupts? %s"
464 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
466 in_interrupt ? "[Q] " : "",
467 substream->stream, (long)delta,
473 if (runtime->status->hw_ptr == new_hw_ptr)
476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
477 runtime->silence_size > 0)
478 snd_pcm_playback_silence(substream, new_hw_ptr);
481 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
483 delta += runtime->boundary;
484 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
485 runtime->hw_ptr_interrupt += delta;
486 if (runtime->hw_ptr_interrupt >= runtime->boundary)
487 runtime->hw_ptr_interrupt -= runtime->boundary;
489 runtime->hw_ptr_base = hw_base;
490 runtime->status->hw_ptr = new_hw_ptr;
491 runtime->hw_ptr_jiffies = jiffies;
492 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
493 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
495 return snd_pcm_update_state(substream, runtime);
498 /* CAUTION: call it with irq disabled */
499 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
501 return snd_pcm_update_hw_ptr0(substream, 0);
505 * snd_pcm_set_ops - set the PCM operators
506 * @pcm: the pcm instance
507 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
508 * @ops: the operator table
510 * Sets the given PCM operators to the pcm instance.
512 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
514 struct snd_pcm_str *stream = &pcm->streams[direction];
515 struct snd_pcm_substream *substream;
517 for (substream = stream->substream; substream != NULL; substream = substream->next)
518 substream->ops = ops;
521 EXPORT_SYMBOL(snd_pcm_set_ops);
524 * snd_pcm_sync - set the PCM sync id
525 * @substream: the pcm substream
527 * Sets the PCM sync identifier for the card.
529 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
531 struct snd_pcm_runtime *runtime = substream->runtime;
533 runtime->sync.id32[0] = substream->pcm->card->number;
534 runtime->sync.id32[1] = -1;
535 runtime->sync.id32[2] = -1;
536 runtime->sync.id32[3] = -1;
539 EXPORT_SYMBOL(snd_pcm_set_sync);
542 * Standard ioctl routine
545 static inline unsigned int div32(unsigned int a, unsigned int b,
556 static inline unsigned int div_down(unsigned int a, unsigned int b)
563 static inline unsigned int div_up(unsigned int a, unsigned int b)
575 static inline unsigned int mul(unsigned int a, unsigned int b)
579 if (div_down(UINT_MAX, a) < b)
584 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
585 unsigned int c, unsigned int *r)
587 u_int64_t n = (u_int64_t) a * b;
593 n = div_u64_rem(n, c, r);
602 * snd_interval_refine - refine the interval value of configurator
603 * @i: the interval value to refine
604 * @v: the interval value to refer to
606 * Refines the interval value with the reference value.
607 * The interval is changed to the range satisfying both intervals.
608 * The interval status (min, max, integer, etc.) are evaluated.
610 * Returns non-zero if the value is changed, zero if not changed.
612 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
615 if (snd_BUG_ON(snd_interval_empty(i)))
617 if (i->min < v->min) {
619 i->openmin = v->openmin;
621 } else if (i->min == v->min && !i->openmin && v->openmin) {
625 if (i->max > v->max) {
627 i->openmax = v->openmax;
629 } else if (i->max == v->max && !i->openmax && v->openmax) {
633 if (!i->integer && v->integer) {
646 } else if (!i->openmin && !i->openmax && i->min == i->max)
648 if (snd_interval_checkempty(i)) {
649 snd_interval_none(i);
655 EXPORT_SYMBOL(snd_interval_refine);
657 static int snd_interval_refine_first(struct snd_interval *i)
659 if (snd_BUG_ON(snd_interval_empty(i)))
661 if (snd_interval_single(i))
664 i->openmax = i->openmin;
670 static int snd_interval_refine_last(struct snd_interval *i)
672 if (snd_BUG_ON(snd_interval_empty(i)))
674 if (snd_interval_single(i))
677 i->openmin = i->openmax;
683 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
685 if (a->empty || b->empty) {
686 snd_interval_none(c);
690 c->min = mul(a->min, b->min);
691 c->openmin = (a->openmin || b->openmin);
692 c->max = mul(a->max, b->max);
693 c->openmax = (a->openmax || b->openmax);
694 c->integer = (a->integer && b->integer);
698 * snd_interval_div - refine the interval value with division
705 * Returns non-zero if the value is changed, zero if not changed.
707 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
710 if (a->empty || b->empty) {
711 snd_interval_none(c);
715 c->min = div32(a->min, b->max, &r);
716 c->openmin = (r || a->openmin || b->openmax);
718 c->max = div32(a->max, b->min, &r);
723 c->openmax = (a->openmax || b->openmin);
732 * snd_interval_muldivk - refine the interval value
735 * @k: divisor (as integer)
740 * Returns non-zero if the value is changed, zero if not changed.
742 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
743 unsigned int k, struct snd_interval *c)
746 if (a->empty || b->empty) {
747 snd_interval_none(c);
751 c->min = muldiv32(a->min, b->min, k, &r);
752 c->openmin = (r || a->openmin || b->openmin);
753 c->max = muldiv32(a->max, b->max, k, &r);
758 c->openmax = (a->openmax || b->openmax);
763 * snd_interval_mulkdiv - refine the interval value
765 * @k: dividend 2 (as integer)
771 * Returns non-zero if the value is changed, zero if not changed.
773 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
774 const struct snd_interval *b, struct snd_interval *c)
777 if (a->empty || b->empty) {
778 snd_interval_none(c);
782 c->min = muldiv32(a->min, k, b->max, &r);
783 c->openmin = (r || a->openmin || b->openmax);
785 c->max = muldiv32(a->max, k, b->min, &r);
790 c->openmax = (a->openmax || b->openmin);
802 * snd_interval_ratnum - refine the interval value
803 * @i: interval to refine
804 * @rats_count: number of ratnum_t
805 * @rats: ratnum_t array
806 * @nump: pointer to store the resultant numerator
807 * @denp: pointer to store the resultant denominator
809 * Returns non-zero if the value is changed, zero if not changed.
811 int snd_interval_ratnum(struct snd_interval *i,
812 unsigned int rats_count, struct snd_ratnum *rats,
813 unsigned int *nump, unsigned int *denp)
815 unsigned int best_num, best_den;
818 struct snd_interval t;
820 unsigned int result_num, result_den;
823 best_num = best_den = best_diff = 0;
824 for (k = 0; k < rats_count; ++k) {
825 unsigned int num = rats[k].num;
827 unsigned int q = i->min;
831 den = div_up(num, q);
832 if (den < rats[k].den_min)
834 if (den > rats[k].den_max)
835 den = rats[k].den_max;
838 r = (den - rats[k].den_min) % rats[k].den_step;
842 diff = num - q * den;
846 diff * best_den < best_diff * den) {
856 t.min = div_down(best_num, best_den);
857 t.openmin = !!(best_num % best_den);
859 result_num = best_num;
860 result_diff = best_diff;
861 result_den = best_den;
862 best_num = best_den = best_diff = 0;
863 for (k = 0; k < rats_count; ++k) {
864 unsigned int num = rats[k].num;
866 unsigned int q = i->max;
872 den = div_down(num, q);
873 if (den > rats[k].den_max)
875 if (den < rats[k].den_min)
876 den = rats[k].den_min;
879 r = (den - rats[k].den_min) % rats[k].den_step;
881 den += rats[k].den_step - r;
883 diff = q * den - num;
887 diff * best_den < best_diff * den) {
897 t.max = div_up(best_num, best_den);
898 t.openmax = !!(best_num % best_den);
900 err = snd_interval_refine(i, &t);
904 if (snd_interval_single(i)) {
905 if (best_diff * result_den < result_diff * best_den) {
906 result_num = best_num;
907 result_den = best_den;
917 EXPORT_SYMBOL(snd_interval_ratnum);
920 * snd_interval_ratden - refine the interval value
921 * @i: interval to refine
922 * @rats_count: number of struct ratden
923 * @rats: struct ratden array
924 * @nump: pointer to store the resultant numerator
925 * @denp: pointer to store the resultant denominator
927 * Returns non-zero if the value is changed, zero if not changed.
929 static int snd_interval_ratden(struct snd_interval *i,
930 unsigned int rats_count, struct snd_ratden *rats,
931 unsigned int *nump, unsigned int *denp)
933 unsigned int best_num, best_diff, best_den;
935 struct snd_interval t;
938 best_num = best_den = best_diff = 0;
939 for (k = 0; k < rats_count; ++k) {
941 unsigned int den = rats[k].den;
942 unsigned int q = i->min;
945 if (num > rats[k].num_max)
947 if (num < rats[k].num_min)
948 num = rats[k].num_max;
951 r = (num - rats[k].num_min) % rats[k].num_step;
953 num += rats[k].num_step - r;
955 diff = num - q * den;
957 diff * best_den < best_diff * den) {
967 t.min = div_down(best_num, best_den);
968 t.openmin = !!(best_num % best_den);
970 best_num = best_den = best_diff = 0;
971 for (k = 0; k < rats_count; ++k) {
973 unsigned int den = rats[k].den;
974 unsigned int q = i->max;
977 if (num < rats[k].num_min)
979 if (num > rats[k].num_max)
980 num = rats[k].num_max;
983 r = (num - rats[k].num_min) % rats[k].num_step;
987 diff = q * den - num;
989 diff * best_den < best_diff * den) {
999 t.max = div_up(best_num, best_den);
1000 t.openmax = !!(best_num % best_den);
1002 err = snd_interval_refine(i, &t);
1006 if (snd_interval_single(i)) {
1016 * snd_interval_list - refine the interval value from the list
1017 * @i: the interval value to refine
1018 * @count: the number of elements in the list
1019 * @list: the value list
1020 * @mask: the bit-mask to evaluate
1022 * Refines the interval value from the list.
1023 * When mask is non-zero, only the elements corresponding to bit 1 are
1026 * Returns non-zero if the value is changed, zero if not changed.
1028 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
1031 struct snd_interval list_range;
1037 snd_interval_any(&list_range);
1038 list_range.min = UINT_MAX;
1040 for (k = 0; k < count; k++) {
1041 if (mask && !(mask & (1 << k)))
1043 if (!snd_interval_test(i, list[k]))
1045 list_range.min = min(list_range.min, list[k]);
1046 list_range.max = max(list_range.max, list[k]);
1048 return snd_interval_refine(i, &list_range);
1051 EXPORT_SYMBOL(snd_interval_list);
1053 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
1057 n = (i->min - min) % step;
1058 if (n != 0 || i->openmin) {
1062 n = (i->max - min) % step;
1063 if (n != 0 || i->openmax) {
1067 if (snd_interval_checkempty(i)) {
1074 /* Info constraints helpers */
1077 * snd_pcm_hw_rule_add - add the hw-constraint rule
1078 * @runtime: the pcm runtime instance
1079 * @cond: condition bits
1080 * @var: the variable to evaluate
1081 * @func: the evaluation function
1082 * @private: the private data pointer passed to function
1083 * @dep: the dependent variables
1085 * Returns zero if successful, or a negative error code on failure.
1087 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1089 snd_pcm_hw_rule_func_t func, void *private,
1092 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1093 struct snd_pcm_hw_rule *c;
1096 va_start(args, dep);
1097 if (constrs->rules_num >= constrs->rules_all) {
1098 struct snd_pcm_hw_rule *new;
1099 unsigned int new_rules = constrs->rules_all + 16;
1100 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1105 if (constrs->rules) {
1106 memcpy(new, constrs->rules,
1107 constrs->rules_num * sizeof(*c));
1108 kfree(constrs->rules);
1110 constrs->rules = new;
1111 constrs->rules_all = new_rules;
1113 c = &constrs->rules[constrs->rules_num];
1117 c->private = private;
1120 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
1127 dep = va_arg(args, int);
1129 constrs->rules_num++;
1134 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1137 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1138 * @runtime: PCM runtime instance
1139 * @var: hw_params variable to apply the mask
1140 * @mask: the bitmap mask
1142 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1144 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1147 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1148 struct snd_mask *maskp = constrs_mask(constrs, var);
1149 *maskp->bits &= mask;
1150 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1151 if (*maskp->bits == 0)
1157 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1158 * @runtime: PCM runtime instance
1159 * @var: hw_params variable to apply the mask
1160 * @mask: the 64bit bitmap mask
1162 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1164 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1167 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1168 struct snd_mask *maskp = constrs_mask(constrs, var);
1169 maskp->bits[0] &= (u_int32_t)mask;
1170 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1171 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1172 if (! maskp->bits[0] && ! maskp->bits[1])
1178 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1179 * @runtime: PCM runtime instance
1180 * @var: hw_params variable to apply the integer constraint
1182 * Apply the constraint of integer to an interval parameter.
1184 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1186 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1187 return snd_interval_setinteger(constrs_interval(constrs, var));
1190 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1193 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1194 * @runtime: PCM runtime instance
1195 * @var: hw_params variable to apply the range
1196 * @min: the minimal value
1197 * @max: the maximal value
1199 * Apply the min/max range constraint to an interval parameter.
1201 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1202 unsigned int min, unsigned int max)
1204 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1205 struct snd_interval t;
1208 t.openmin = t.openmax = 0;
1210 return snd_interval_refine(constrs_interval(constrs, var), &t);
1213 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1215 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1216 struct snd_pcm_hw_rule *rule)
1218 struct snd_pcm_hw_constraint_list *list = rule->private;
1219 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1224 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1225 * @runtime: PCM runtime instance
1226 * @cond: condition bits
1227 * @var: hw_params variable to apply the list constraint
1230 * Apply the list of constraints to an interval parameter.
1232 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1234 snd_pcm_hw_param_t var,
1235 struct snd_pcm_hw_constraint_list *l)
1237 return snd_pcm_hw_rule_add(runtime, cond, var,
1238 snd_pcm_hw_rule_list, l,
1242 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1244 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1245 struct snd_pcm_hw_rule *rule)
1247 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1248 unsigned int num = 0, den = 0;
1250 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1251 r->nrats, r->rats, &num, &den);
1252 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1253 params->rate_num = num;
1254 params->rate_den = den;
1260 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1261 * @runtime: PCM runtime instance
1262 * @cond: condition bits
1263 * @var: hw_params variable to apply the ratnums constraint
1264 * @r: struct snd_ratnums constriants
1266 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1268 snd_pcm_hw_param_t var,
1269 struct snd_pcm_hw_constraint_ratnums *r)
1271 return snd_pcm_hw_rule_add(runtime, cond, var,
1272 snd_pcm_hw_rule_ratnums, r,
1276 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1278 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1279 struct snd_pcm_hw_rule *rule)
1281 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1282 unsigned int num = 0, den = 0;
1283 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1284 r->nrats, r->rats, &num, &den);
1285 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1286 params->rate_num = num;
1287 params->rate_den = den;
1293 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1294 * @runtime: PCM runtime instance
1295 * @cond: condition bits
1296 * @var: hw_params variable to apply the ratdens constraint
1297 * @r: struct snd_ratdens constriants
1299 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1301 snd_pcm_hw_param_t var,
1302 struct snd_pcm_hw_constraint_ratdens *r)
1304 return snd_pcm_hw_rule_add(runtime, cond, var,
1305 snd_pcm_hw_rule_ratdens, r,
1309 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1311 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1312 struct snd_pcm_hw_rule *rule)
1314 unsigned int l = (unsigned long) rule->private;
1315 int width = l & 0xffff;
1316 unsigned int msbits = l >> 16;
1317 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1318 if (snd_interval_single(i) && snd_interval_value(i) == width)
1319 params->msbits = msbits;
1324 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1325 * @runtime: PCM runtime instance
1326 * @cond: condition bits
1327 * @width: sample bits width
1328 * @msbits: msbits width
1330 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1333 unsigned int msbits)
1335 unsigned long l = (msbits << 16) | width;
1336 return snd_pcm_hw_rule_add(runtime, cond, -1,
1337 snd_pcm_hw_rule_msbits,
1339 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1342 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1344 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1345 struct snd_pcm_hw_rule *rule)
1347 unsigned long step = (unsigned long) rule->private;
1348 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1352 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1353 * @runtime: PCM runtime instance
1354 * @cond: condition bits
1355 * @var: hw_params variable to apply the step constraint
1358 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1360 snd_pcm_hw_param_t var,
1363 return snd_pcm_hw_rule_add(runtime, cond, var,
1364 snd_pcm_hw_rule_step, (void *) step,
1368 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1370 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1372 static unsigned int pow2_sizes[] = {
1373 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1374 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1375 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1376 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1378 return snd_interval_list(hw_param_interval(params, rule->var),
1379 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1383 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1384 * @runtime: PCM runtime instance
1385 * @cond: condition bits
1386 * @var: hw_params variable to apply the power-of-2 constraint
1388 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1390 snd_pcm_hw_param_t var)
1392 return snd_pcm_hw_rule_add(runtime, cond, var,
1393 snd_pcm_hw_rule_pow2, NULL,
1397 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1399 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1400 snd_pcm_hw_param_t var)
1402 if (hw_is_mask(var)) {
1403 snd_mask_any(hw_param_mask(params, var));
1404 params->cmask |= 1 << var;
1405 params->rmask |= 1 << var;
1408 if (hw_is_interval(var)) {
1409 snd_interval_any(hw_param_interval(params, var));
1410 params->cmask |= 1 << var;
1411 params->rmask |= 1 << var;
1417 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1420 memset(params, 0, sizeof(*params));
1421 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1422 _snd_pcm_hw_param_any(params, k);
1423 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1424 _snd_pcm_hw_param_any(params, k);
1428 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1431 * snd_pcm_hw_param_value - return @params field @var value
1432 * @params: the hw_params instance
1433 * @var: parameter to retrieve
1434 * @dir: pointer to the direction (-1,0,1) or %NULL
1436 * Return the value for field @var if it's fixed in configuration space
1437 * defined by @params. Return -%EINVAL otherwise.
1439 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1440 snd_pcm_hw_param_t var, int *dir)
1442 if (hw_is_mask(var)) {
1443 const struct snd_mask *mask = hw_param_mask_c(params, var);
1444 if (!snd_mask_single(mask))
1448 return snd_mask_value(mask);
1450 if (hw_is_interval(var)) {
1451 const struct snd_interval *i = hw_param_interval_c(params, var);
1452 if (!snd_interval_single(i))
1456 return snd_interval_value(i);
1461 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1463 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1464 snd_pcm_hw_param_t var)
1466 if (hw_is_mask(var)) {
1467 snd_mask_none(hw_param_mask(params, var));
1468 params->cmask |= 1 << var;
1469 params->rmask |= 1 << var;
1470 } else if (hw_is_interval(var)) {
1471 snd_interval_none(hw_param_interval(params, var));
1472 params->cmask |= 1 << var;
1473 params->rmask |= 1 << var;
1479 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1481 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1482 snd_pcm_hw_param_t var)
1485 if (hw_is_mask(var))
1486 changed = snd_mask_refine_first(hw_param_mask(params, var));
1487 else if (hw_is_interval(var))
1488 changed = snd_interval_refine_first(hw_param_interval(params, var));
1492 params->cmask |= 1 << var;
1493 params->rmask |= 1 << var;
1500 * snd_pcm_hw_param_first - refine config space and return minimum value
1501 * @pcm: PCM instance
1502 * @params: the hw_params instance
1503 * @var: parameter to retrieve
1504 * @dir: pointer to the direction (-1,0,1) or %NULL
1506 * Inside configuration space defined by @params remove from @var all
1507 * values > minimum. Reduce configuration space accordingly.
1508 * Return the minimum.
1510 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1511 struct snd_pcm_hw_params *params,
1512 snd_pcm_hw_param_t var, int *dir)
1514 int changed = _snd_pcm_hw_param_first(params, var);
1517 if (params->rmask) {
1518 int err = snd_pcm_hw_refine(pcm, params);
1519 if (snd_BUG_ON(err < 0))
1522 return snd_pcm_hw_param_value(params, var, dir);
1525 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1527 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1528 snd_pcm_hw_param_t var)
1531 if (hw_is_mask(var))
1532 changed = snd_mask_refine_last(hw_param_mask(params, var));
1533 else if (hw_is_interval(var))
1534 changed = snd_interval_refine_last(hw_param_interval(params, var));
1538 params->cmask |= 1 << var;
1539 params->rmask |= 1 << var;
1546 * snd_pcm_hw_param_last - refine config space and return maximum value
1547 * @pcm: PCM instance
1548 * @params: the hw_params instance
1549 * @var: parameter to retrieve
1550 * @dir: pointer to the direction (-1,0,1) or %NULL
1552 * Inside configuration space defined by @params remove from @var all
1553 * values < maximum. Reduce configuration space accordingly.
1554 * Return the maximum.
1556 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1557 struct snd_pcm_hw_params *params,
1558 snd_pcm_hw_param_t var, int *dir)
1560 int changed = _snd_pcm_hw_param_last(params, var);
1563 if (params->rmask) {
1564 int err = snd_pcm_hw_refine(pcm, params);
1565 if (snd_BUG_ON(err < 0))
1568 return snd_pcm_hw_param_value(params, var, dir);
1571 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1574 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1575 * @pcm: PCM instance
1576 * @params: the hw_params instance
1578 * Choose one configuration from configuration space defined by @params.
1579 * The configuration chosen is that obtained fixing in this order:
1580 * first access, first format, first subformat, min channels,
1581 * min rate, min period time, max buffer size, min tick time
1583 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1584 struct snd_pcm_hw_params *params)
1586 static int vars[] = {
1587 SNDRV_PCM_HW_PARAM_ACCESS,
1588 SNDRV_PCM_HW_PARAM_FORMAT,
1589 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1590 SNDRV_PCM_HW_PARAM_CHANNELS,
1591 SNDRV_PCM_HW_PARAM_RATE,
1592 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1593 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1594 SNDRV_PCM_HW_PARAM_TICK_TIME,
1599 for (v = vars; *v != -1; v++) {
1600 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1601 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1603 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1604 if (snd_BUG_ON(err < 0))
1610 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1613 struct snd_pcm_runtime *runtime = substream->runtime;
1614 unsigned long flags;
1615 snd_pcm_stream_lock_irqsave(substream, flags);
1616 if (snd_pcm_running(substream) &&
1617 snd_pcm_update_hw_ptr(substream) >= 0)
1618 runtime->status->hw_ptr %= runtime->buffer_size;
1620 runtime->status->hw_ptr = 0;
1621 snd_pcm_stream_unlock_irqrestore(substream, flags);
1625 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1628 struct snd_pcm_channel_info *info = arg;
1629 struct snd_pcm_runtime *runtime = substream->runtime;
1631 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1635 width = snd_pcm_format_physical_width(runtime->format);
1639 switch (runtime->access) {
1640 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1641 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1642 info->first = info->channel * width;
1643 info->step = runtime->channels * width;
1645 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1646 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1648 size_t size = runtime->dma_bytes / runtime->channels;
1649 info->first = info->channel * size * 8;
1660 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1663 struct snd_pcm_hw_params *params = arg;
1664 snd_pcm_format_t format;
1665 int channels, width;
1667 params->fifo_size = substream->runtime->hw.fifo_size;
1668 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1669 format = params_format(params);
1670 channels = params_channels(params);
1671 width = snd_pcm_format_physical_width(format);
1672 params->fifo_size /= width * channels;
1678 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1679 * @substream: the pcm substream instance
1680 * @cmd: ioctl command
1681 * @arg: ioctl argument
1683 * Processes the generic ioctl commands for PCM.
1684 * Can be passed as the ioctl callback for PCM ops.
1686 * Returns zero if successful, or a negative error code on failure.
1688 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1689 unsigned int cmd, void *arg)
1692 case SNDRV_PCM_IOCTL1_INFO:
1694 case SNDRV_PCM_IOCTL1_RESET:
1695 return snd_pcm_lib_ioctl_reset(substream, arg);
1696 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1697 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1698 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1699 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1704 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1707 * snd_pcm_period_elapsed - update the pcm status for the next period
1708 * @substream: the pcm substream instance
1710 * This function is called from the interrupt handler when the
1711 * PCM has processed the period size. It will update the current
1712 * pointer, wake up sleepers, etc.
1714 * Even if more than one periods have elapsed since the last call, you
1715 * have to call this only once.
1717 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1719 struct snd_pcm_runtime *runtime;
1720 unsigned long flags;
1722 if (PCM_RUNTIME_CHECK(substream))
1724 runtime = substream->runtime;
1726 if (runtime->transfer_ack_begin)
1727 runtime->transfer_ack_begin(substream);
1729 snd_pcm_stream_lock_irqsave(substream, flags);
1730 if (!snd_pcm_running(substream) ||
1731 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1734 if (substream->timer_running)
1735 snd_timer_interrupt(substream->timer, 1);
1737 snd_pcm_stream_unlock_irqrestore(substream, flags);
1738 if (runtime->transfer_ack_end)
1739 runtime->transfer_ack_end(substream);
1740 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1743 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1746 * Wait until avail_min data becomes available
1747 * Returns a negative error code if any error occurs during operation.
1748 * The available space is stored on availp. When err = 0 and avail = 0
1749 * on the capture stream, it indicates the stream is in DRAINING state.
1751 static int wait_for_avail(struct snd_pcm_substream *substream,
1752 snd_pcm_uframes_t *availp)
1754 struct snd_pcm_runtime *runtime = substream->runtime;
1755 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1758 snd_pcm_uframes_t avail = 0;
1761 init_waitqueue_entry(&wait, current);
1762 add_wait_queue(&runtime->tsleep, &wait);
1764 if (signal_pending(current)) {
1768 set_current_state(TASK_INTERRUPTIBLE);
1769 snd_pcm_stream_unlock_irq(substream);
1770 tout = schedule_timeout(msecs_to_jiffies(10000));
1771 snd_pcm_stream_lock_irq(substream);
1772 switch (runtime->status->state) {
1773 case SNDRV_PCM_STATE_SUSPENDED:
1776 case SNDRV_PCM_STATE_XRUN:
1779 case SNDRV_PCM_STATE_DRAINING:
1783 avail = 0; /* indicate draining */
1785 case SNDRV_PCM_STATE_OPEN:
1786 case SNDRV_PCM_STATE_SETUP:
1787 case SNDRV_PCM_STATE_DISCONNECTED:
1792 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1793 is_playback ? "playback" : "capture");
1798 avail = snd_pcm_playback_avail(runtime);
1800 avail = snd_pcm_capture_avail(runtime);
1801 if (avail >= runtime->twake)
1805 remove_wait_queue(&runtime->tsleep, &wait);
1810 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1812 unsigned long data, unsigned int off,
1813 snd_pcm_uframes_t frames)
1815 struct snd_pcm_runtime *runtime = substream->runtime;
1817 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1818 if (substream->ops->copy) {
1819 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1822 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1823 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1829 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1830 unsigned long data, unsigned int off,
1831 snd_pcm_uframes_t size);
1833 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1835 snd_pcm_uframes_t size,
1837 transfer_f transfer)
1839 struct snd_pcm_runtime *runtime = substream->runtime;
1840 snd_pcm_uframes_t xfer = 0;
1841 snd_pcm_uframes_t offset = 0;
1847 snd_pcm_stream_lock_irq(substream);
1848 switch (runtime->status->state) {
1849 case SNDRV_PCM_STATE_PREPARED:
1850 case SNDRV_PCM_STATE_RUNNING:
1851 case SNDRV_PCM_STATE_PAUSED:
1853 case SNDRV_PCM_STATE_XRUN:
1856 case SNDRV_PCM_STATE_SUSPENDED:
1864 runtime->twake = runtime->control->avail_min ? : 1;
1866 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1867 snd_pcm_uframes_t avail;
1868 snd_pcm_uframes_t cont;
1869 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1870 snd_pcm_update_hw_ptr(substream);
1871 avail = snd_pcm_playback_avail(runtime);
1877 runtime->twake = min_t(snd_pcm_uframes_t, size,
1878 runtime->control->avail_min ? : 1);
1879 err = wait_for_avail(substream, &avail);
1883 frames = size > avail ? avail : size;
1884 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1887 if (snd_BUG_ON(!frames)) {
1889 snd_pcm_stream_unlock_irq(substream);
1892 appl_ptr = runtime->control->appl_ptr;
1893 appl_ofs = appl_ptr % runtime->buffer_size;
1894 snd_pcm_stream_unlock_irq(substream);
1895 err = transfer(substream, appl_ofs, data, offset, frames);
1896 snd_pcm_stream_lock_irq(substream);
1899 switch (runtime->status->state) {
1900 case SNDRV_PCM_STATE_XRUN:
1903 case SNDRV_PCM_STATE_SUSPENDED:
1910 if (appl_ptr >= runtime->boundary)
1911 appl_ptr -= runtime->boundary;
1912 runtime->control->appl_ptr = appl_ptr;
1913 if (substream->ops->ack)
1914 substream->ops->ack(substream);
1919 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1920 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1921 err = snd_pcm_start(substream);
1928 if (xfer > 0 && err >= 0)
1929 snd_pcm_update_state(substream, runtime);
1930 snd_pcm_stream_unlock_irq(substream);
1931 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1934 /* sanity-check for read/write methods */
1935 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1937 struct snd_pcm_runtime *runtime;
1938 if (PCM_RUNTIME_CHECK(substream))
1940 runtime = substream->runtime;
1941 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1943 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1948 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1950 struct snd_pcm_runtime *runtime;
1954 err = pcm_sanity_check(substream);
1957 runtime = substream->runtime;
1958 nonblock = !!(substream->f_flags & O_NONBLOCK);
1960 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1961 runtime->channels > 1)
1963 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1964 snd_pcm_lib_write_transfer);
1967 EXPORT_SYMBOL(snd_pcm_lib_write);
1969 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1971 unsigned long data, unsigned int off,
1972 snd_pcm_uframes_t frames)
1974 struct snd_pcm_runtime *runtime = substream->runtime;
1976 void __user **bufs = (void __user **)data;
1977 int channels = runtime->channels;
1979 if (substream->ops->copy) {
1980 if (snd_BUG_ON(!substream->ops->silence))
1982 for (c = 0; c < channels; ++c, ++bufs) {
1983 if (*bufs == NULL) {
1984 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1987 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1988 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1993 /* default transfer behaviour */
1994 size_t dma_csize = runtime->dma_bytes / channels;
1995 for (c = 0; c < channels; ++c, ++bufs) {
1996 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1997 if (*bufs == NULL) {
1998 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2000 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2001 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2009 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
2011 snd_pcm_uframes_t frames)
2013 struct snd_pcm_runtime *runtime;
2017 err = pcm_sanity_check(substream);
2020 runtime = substream->runtime;
2021 nonblock = !!(substream->f_flags & O_NONBLOCK);
2023 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2025 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2026 nonblock, snd_pcm_lib_writev_transfer);
2029 EXPORT_SYMBOL(snd_pcm_lib_writev);
2031 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2033 unsigned long data, unsigned int off,
2034 snd_pcm_uframes_t frames)
2036 struct snd_pcm_runtime *runtime = substream->runtime;
2038 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2039 if (substream->ops->copy) {
2040 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2043 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2044 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2050 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2052 snd_pcm_uframes_t size,
2054 transfer_f transfer)
2056 struct snd_pcm_runtime *runtime = substream->runtime;
2057 snd_pcm_uframes_t xfer = 0;
2058 snd_pcm_uframes_t offset = 0;
2064 snd_pcm_stream_lock_irq(substream);
2065 switch (runtime->status->state) {
2066 case SNDRV_PCM_STATE_PREPARED:
2067 if (size >= runtime->start_threshold) {
2068 err = snd_pcm_start(substream);
2073 case SNDRV_PCM_STATE_DRAINING:
2074 case SNDRV_PCM_STATE_RUNNING:
2075 case SNDRV_PCM_STATE_PAUSED:
2077 case SNDRV_PCM_STATE_XRUN:
2080 case SNDRV_PCM_STATE_SUSPENDED:
2088 runtime->twake = runtime->control->avail_min ? : 1;
2090 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2091 snd_pcm_uframes_t avail;
2092 snd_pcm_uframes_t cont;
2093 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2094 snd_pcm_update_hw_ptr(substream);
2095 avail = snd_pcm_capture_avail(runtime);
2097 if (runtime->status->state ==
2098 SNDRV_PCM_STATE_DRAINING) {
2099 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2106 runtime->twake = min_t(snd_pcm_uframes_t, size,
2107 runtime->control->avail_min ? : 1);
2108 err = wait_for_avail(substream, &avail);
2112 continue; /* draining */
2114 frames = size > avail ? avail : size;
2115 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2118 if (snd_BUG_ON(!frames)) {
2120 snd_pcm_stream_unlock_irq(substream);
2123 appl_ptr = runtime->control->appl_ptr;
2124 appl_ofs = appl_ptr % runtime->buffer_size;
2125 snd_pcm_stream_unlock_irq(substream);
2126 err = transfer(substream, appl_ofs, data, offset, frames);
2127 snd_pcm_stream_lock_irq(substream);
2130 switch (runtime->status->state) {
2131 case SNDRV_PCM_STATE_XRUN:
2134 case SNDRV_PCM_STATE_SUSPENDED:
2141 if (appl_ptr >= runtime->boundary)
2142 appl_ptr -= runtime->boundary;
2143 runtime->control->appl_ptr = appl_ptr;
2144 if (substream->ops->ack)
2145 substream->ops->ack(substream);
2153 if (xfer > 0 && err >= 0)
2154 snd_pcm_update_state(substream, runtime);
2155 snd_pcm_stream_unlock_irq(substream);
2156 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2159 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2161 struct snd_pcm_runtime *runtime;
2165 err = pcm_sanity_check(substream);
2168 runtime = substream->runtime;
2169 nonblock = !!(substream->f_flags & O_NONBLOCK);
2170 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2172 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2175 EXPORT_SYMBOL(snd_pcm_lib_read);
2177 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2179 unsigned long data, unsigned int off,
2180 snd_pcm_uframes_t frames)
2182 struct snd_pcm_runtime *runtime = substream->runtime;
2184 void __user **bufs = (void __user **)data;
2185 int channels = runtime->channels;
2187 if (substream->ops->copy) {
2188 for (c = 0; c < channels; ++c, ++bufs) {
2192 buf = *bufs + samples_to_bytes(runtime, off);
2193 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2197 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2198 for (c = 0; c < channels; ++c, ++bufs) {
2204 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2205 buf = *bufs + samples_to_bytes(runtime, off);
2206 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2213 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2215 snd_pcm_uframes_t frames)
2217 struct snd_pcm_runtime *runtime;
2221 err = pcm_sanity_check(substream);
2224 runtime = substream->runtime;
2225 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2228 nonblock = !!(substream->f_flags & O_NONBLOCK);
2229 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2231 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2234 EXPORT_SYMBOL(snd_pcm_lib_readv);