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 long jiffies;
193 snd_pcm_uframes_t pos;
194 snd_pcm_uframes_t period_size;
195 snd_pcm_uframes_t buffer_size;
196 snd_pcm_uframes_t old_hw_ptr;
197 snd_pcm_uframes_t hw_ptr_base;
200 struct snd_pcm_hwptr_log {
203 struct hwptr_log_entry entries[XRUN_LOG_CNT];
206 static void xrun_log(struct snd_pcm_substream *substream,
207 snd_pcm_uframes_t pos)
209 struct snd_pcm_runtime *runtime = substream->runtime;
210 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
211 struct hwptr_log_entry *entry;
214 log = kzalloc(sizeof(*log), GFP_ATOMIC);
217 runtime->hwptr_log = log;
219 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
222 entry = &log->entries[log->idx];
223 entry->jiffies = jiffies;
225 entry->period_size = runtime->period_size;
226 entry->buffer_size = runtime->buffer_size;
227 entry->old_hw_ptr = runtime->status->hw_ptr;
228 entry->hw_ptr_base = runtime->hw_ptr_base;
229 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
232 static void xrun_log_show(struct snd_pcm_substream *substream)
234 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
235 struct hwptr_log_entry *entry;
242 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
244 pcm_debug_name(substream, name, sizeof(name));
245 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
246 entry = &log->entries[idx];
247 if (entry->period_size == 0)
249 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
251 name, entry->jiffies, (unsigned long)entry->pos,
252 (unsigned long)entry->period_size,
253 (unsigned long)entry->buffer_size,
254 (unsigned long)entry->old_hw_ptr,
255 (unsigned long)entry->hw_ptr_base);
262 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
264 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
265 #define xrun_log(substream, pos) do { } while (0)
266 #define xrun_log_show(substream) do { } while (0)
270 int snd_pcm_update_state(struct snd_pcm_substream *substream,
271 struct snd_pcm_runtime *runtime)
273 snd_pcm_uframes_t avail;
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
276 avail = snd_pcm_playback_avail(runtime);
278 avail = snd_pcm_capture_avail(runtime);
279 if (avail > runtime->avail_max)
280 runtime->avail_max = avail;
281 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
282 if (avail >= runtime->buffer_size) {
283 snd_pcm_drain_done(substream);
287 if (avail >= runtime->stop_threshold) {
292 if (runtime->twake) {
293 if (avail >= runtime->twake)
294 wake_up(&runtime->tsleep);
295 } else if (avail >= runtime->control->avail_min)
296 wake_up(&runtime->sleep);
300 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
301 unsigned int in_interrupt)
303 struct snd_pcm_runtime *runtime = substream->runtime;
304 snd_pcm_uframes_t pos;
305 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
306 snd_pcm_sframes_t hdelta, delta;
307 unsigned long jdelta;
309 old_hw_ptr = runtime->status->hw_ptr;
310 pos = substream->ops->pointer(substream);
311 if (pos == SNDRV_PCM_POS_XRUN) {
315 if (pos >= runtime->buffer_size) {
316 if (printk_ratelimit()) {
318 pcm_debug_name(substream, name, sizeof(name));
319 xrun_log_show(substream);
320 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
321 "buffer size = %ld, period size = %ld\n",
322 name, pos, runtime->buffer_size,
323 runtime->period_size);
327 pos -= pos % runtime->min_align;
328 if (xrun_debug(substream, XRUN_DEBUG_LOG))
329 xrun_log(substream, pos);
330 hw_base = runtime->hw_ptr_base;
331 new_hw_ptr = hw_base + pos;
333 /* we know that one period was processed */
334 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
335 delta = runtime->hw_ptr_interrupt + runtime->period_size;
336 if (delta > new_hw_ptr) {
337 /* check for double acknowledged interrupts */
338 hdelta = jiffies - runtime->hw_ptr_jiffies;
339 if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
340 hw_base += runtime->buffer_size;
341 if (hw_base >= runtime->boundary)
343 new_hw_ptr = hw_base + pos;
348 /* new_hw_ptr might be lower than old_hw_ptr in case when */
349 /* pointer crosses the end of the ring buffer */
350 if (new_hw_ptr < old_hw_ptr) {
351 hw_base += runtime->buffer_size;
352 if (hw_base >= runtime->boundary)
354 new_hw_ptr = hw_base + pos;
357 delta = new_hw_ptr - old_hw_ptr;
359 delta += runtime->boundary;
360 if (xrun_debug(substream, in_interrupt ?
361 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
363 pcm_debug_name(substream, name, sizeof(name));
364 snd_printd("%s_update: %s: pos=%u/%u/%u, "
365 "hwptr=%ld/%ld/%ld/%ld\n",
366 in_interrupt ? "period" : "hwptr",
369 (unsigned int)runtime->period_size,
370 (unsigned int)runtime->buffer_size,
371 (unsigned long)delta,
372 (unsigned long)old_hw_ptr,
373 (unsigned long)new_hw_ptr,
374 (unsigned long)runtime->hw_ptr_base);
377 if (runtime->no_period_wakeup) {
379 * Without regular period interrupts, we have to check
380 * the elapsed time to detect xruns.
382 jdelta = jiffies - runtime->hw_ptr_jiffies;
383 hdelta = jdelta - delta * HZ / runtime->rate;
384 while (hdelta > runtime->hw_ptr_buffer_jiffies / 2 + 1) {
385 delta += runtime->buffer_size;
386 hw_base += runtime->buffer_size;
387 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,
398 "Unexpected hw_pointer value %s"
399 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
401 in_interrupt ? "[Q] " : "[P]",
402 substream->stream, (long)pos,
403 (long)new_hw_ptr, (long)old_hw_ptr);
407 /* Do jiffies check only in xrun_debug mode */
408 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
409 goto no_jiffies_check;
411 /* Skip the jiffies check for hardwares with BATCH flag.
412 * Such hardware usually just increases the position at each IRQ,
413 * thus it can't give any strange position.
415 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
416 goto no_jiffies_check;
418 if (hdelta < runtime->delay)
419 goto no_jiffies_check;
420 hdelta -= runtime->delay;
421 jdelta = jiffies - runtime->hw_ptr_jiffies;
422 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
424 (((runtime->period_size * HZ) / runtime->rate)
426 /* move new_hw_ptr according jiffies not pos variable */
427 new_hw_ptr = old_hw_ptr;
429 /* use loop to avoid checks for delta overflows */
430 /* the delta value is small or zero in most cases */
432 new_hw_ptr += runtime->period_size;
433 if (new_hw_ptr >= runtime->boundary)
434 new_hw_ptr -= runtime->boundary;
437 /* align hw_base to buffer_size */
438 hw_ptr_error(substream,
439 "hw_ptr skipping! %s"
440 "(pos=%ld, delta=%ld, period=%ld, "
441 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
442 in_interrupt ? "[Q] " : "",
443 (long)pos, (long)hdelta,
444 (long)runtime->period_size, jdelta,
445 ((hdelta * HZ) / runtime->rate), hw_base,
446 (unsigned long)old_hw_ptr,
447 (unsigned long)new_hw_ptr);
448 /* reset values to proper state */
450 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
453 if (delta > runtime->period_size + runtime->period_size / 2) {
454 hw_ptr_error(substream,
455 "Lost interrupts? %s"
456 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
458 in_interrupt ? "[Q] " : "",
459 substream->stream, (long)delta,
465 if (runtime->status->hw_ptr == new_hw_ptr)
468 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
469 runtime->silence_size > 0)
470 snd_pcm_playback_silence(substream, new_hw_ptr);
473 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
475 delta += runtime->boundary;
476 delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
477 runtime->hw_ptr_interrupt += delta;
478 if (runtime->hw_ptr_interrupt >= runtime->boundary)
479 runtime->hw_ptr_interrupt -= runtime->boundary;
481 runtime->hw_ptr_base = hw_base;
482 runtime->status->hw_ptr = new_hw_ptr;
483 runtime->hw_ptr_jiffies = jiffies;
484 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
485 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->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, struct snd_pcm_ops *ops)
506 struct snd_pcm_str *stream = &pcm->streams[direction];
507 struct snd_pcm_substream *substream;
509 for (substream = stream->substream; substream != NULL; substream = substream->next)
510 substream->ops = ops;
513 EXPORT_SYMBOL(snd_pcm_set_ops);
516 * snd_pcm_sync - set the PCM sync id
517 * @substream: the pcm substream
519 * Sets the PCM sync identifier for the card.
521 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
523 struct snd_pcm_runtime *runtime = substream->runtime;
525 runtime->sync.id32[0] = substream->pcm->card->number;
526 runtime->sync.id32[1] = -1;
527 runtime->sync.id32[2] = -1;
528 runtime->sync.id32[3] = -1;
531 EXPORT_SYMBOL(snd_pcm_set_sync);
534 * Standard ioctl routine
537 static inline unsigned int div32(unsigned int a, unsigned int b,
548 static inline unsigned int div_down(unsigned int a, unsigned int b)
555 static inline unsigned int div_up(unsigned int a, unsigned int b)
567 static inline unsigned int mul(unsigned int a, unsigned int b)
571 if (div_down(UINT_MAX, a) < b)
576 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
577 unsigned int c, unsigned int *r)
579 u_int64_t n = (u_int64_t) a * b;
585 n = div_u64_rem(n, c, r);
594 * snd_interval_refine - refine the interval value of configurator
595 * @i: the interval value to refine
596 * @v: the interval value to refer to
598 * Refines the interval value with the reference value.
599 * The interval is changed to the range satisfying both intervals.
600 * The interval status (min, max, integer, etc.) are evaluated.
602 * Returns non-zero if the value is changed, zero if not changed.
604 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
607 if (snd_BUG_ON(snd_interval_empty(i)))
609 if (i->min < v->min) {
611 i->openmin = v->openmin;
613 } else if (i->min == v->min && !i->openmin && v->openmin) {
617 if (i->max > v->max) {
619 i->openmax = v->openmax;
621 } else if (i->max == v->max && !i->openmax && v->openmax) {
625 if (!i->integer && v->integer) {
638 } else if (!i->openmin && !i->openmax && i->min == i->max)
640 if (snd_interval_checkempty(i)) {
641 snd_interval_none(i);
647 EXPORT_SYMBOL(snd_interval_refine);
649 static int snd_interval_refine_first(struct snd_interval *i)
651 if (snd_BUG_ON(snd_interval_empty(i)))
653 if (snd_interval_single(i))
656 i->openmax = i->openmin;
662 static int snd_interval_refine_last(struct snd_interval *i)
664 if (snd_BUG_ON(snd_interval_empty(i)))
666 if (snd_interval_single(i))
669 i->openmin = i->openmax;
675 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
677 if (a->empty || b->empty) {
678 snd_interval_none(c);
682 c->min = mul(a->min, b->min);
683 c->openmin = (a->openmin || b->openmin);
684 c->max = mul(a->max, b->max);
685 c->openmax = (a->openmax || b->openmax);
686 c->integer = (a->integer && b->integer);
690 * snd_interval_div - refine the interval value with division
697 * Returns non-zero if the value is changed, zero if not changed.
699 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
702 if (a->empty || b->empty) {
703 snd_interval_none(c);
707 c->min = div32(a->min, b->max, &r);
708 c->openmin = (r || a->openmin || b->openmax);
710 c->max = div32(a->max, b->min, &r);
715 c->openmax = (a->openmax || b->openmin);
724 * snd_interval_muldivk - refine the interval value
727 * @k: divisor (as integer)
732 * Returns non-zero if the value is changed, zero if not changed.
734 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
735 unsigned int k, struct snd_interval *c)
738 if (a->empty || b->empty) {
739 snd_interval_none(c);
743 c->min = muldiv32(a->min, b->min, k, &r);
744 c->openmin = (r || a->openmin || b->openmin);
745 c->max = muldiv32(a->max, b->max, k, &r);
750 c->openmax = (a->openmax || b->openmax);
755 * snd_interval_mulkdiv - refine the interval value
757 * @k: dividend 2 (as integer)
763 * Returns non-zero if the value is changed, zero if not changed.
765 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
766 const struct snd_interval *b, struct snd_interval *c)
769 if (a->empty || b->empty) {
770 snd_interval_none(c);
774 c->min = muldiv32(a->min, k, b->max, &r);
775 c->openmin = (r || a->openmin || b->openmax);
777 c->max = muldiv32(a->max, k, b->min, &r);
782 c->openmax = (a->openmax || b->openmin);
794 * snd_interval_ratnum - refine the interval value
795 * @i: interval to refine
796 * @rats_count: number of ratnum_t
797 * @rats: ratnum_t array
798 * @nump: pointer to store the resultant numerator
799 * @denp: pointer to store the resultant denominator
801 * Returns non-zero if the value is changed, zero if not changed.
803 int snd_interval_ratnum(struct snd_interval *i,
804 unsigned int rats_count, struct snd_ratnum *rats,
805 unsigned int *nump, unsigned int *denp)
807 unsigned int best_num, best_den;
810 struct snd_interval t;
812 unsigned int result_num, result_den;
815 best_num = best_den = best_diff = 0;
816 for (k = 0; k < rats_count; ++k) {
817 unsigned int num = rats[k].num;
819 unsigned int q = i->min;
823 den = div_up(num, q);
824 if (den < rats[k].den_min)
826 if (den > rats[k].den_max)
827 den = rats[k].den_max;
830 r = (den - rats[k].den_min) % rats[k].den_step;
834 diff = num - q * den;
838 diff * best_den < best_diff * den) {
848 t.min = div_down(best_num, best_den);
849 t.openmin = !!(best_num % best_den);
851 result_num = best_num;
852 result_diff = best_diff;
853 result_den = best_den;
854 best_num = best_den = best_diff = 0;
855 for (k = 0; k < rats_count; ++k) {
856 unsigned int num = rats[k].num;
858 unsigned int q = i->max;
864 den = div_down(num, q);
865 if (den > rats[k].den_max)
867 if (den < rats[k].den_min)
868 den = rats[k].den_min;
871 r = (den - rats[k].den_min) % rats[k].den_step;
873 den += rats[k].den_step - r;
875 diff = q * den - num;
879 diff * best_den < best_diff * den) {
889 t.max = div_up(best_num, best_den);
890 t.openmax = !!(best_num % best_den);
892 err = snd_interval_refine(i, &t);
896 if (snd_interval_single(i)) {
897 if (best_diff * result_den < result_diff * best_den) {
898 result_num = best_num;
899 result_den = best_den;
909 EXPORT_SYMBOL(snd_interval_ratnum);
912 * snd_interval_ratden - refine the interval value
913 * @i: interval to refine
914 * @rats_count: number of struct ratden
915 * @rats: struct ratden array
916 * @nump: pointer to store the resultant numerator
917 * @denp: pointer to store the resultant denominator
919 * Returns non-zero if the value is changed, zero if not changed.
921 static int snd_interval_ratden(struct snd_interval *i,
922 unsigned int rats_count, struct snd_ratden *rats,
923 unsigned int *nump, unsigned int *denp)
925 unsigned int best_num, best_diff, best_den;
927 struct snd_interval t;
930 best_num = best_den = best_diff = 0;
931 for (k = 0; k < rats_count; ++k) {
933 unsigned int den = rats[k].den;
934 unsigned int q = i->min;
937 if (num > rats[k].num_max)
939 if (num < rats[k].num_min)
940 num = rats[k].num_max;
943 r = (num - rats[k].num_min) % rats[k].num_step;
945 num += rats[k].num_step - r;
947 diff = num - q * den;
949 diff * best_den < best_diff * den) {
959 t.min = div_down(best_num, best_den);
960 t.openmin = !!(best_num % best_den);
962 best_num = best_den = best_diff = 0;
963 for (k = 0; k < rats_count; ++k) {
965 unsigned int den = rats[k].den;
966 unsigned int q = i->max;
969 if (num < rats[k].num_min)
971 if (num > rats[k].num_max)
972 num = rats[k].num_max;
975 r = (num - rats[k].num_min) % rats[k].num_step;
979 diff = q * den - num;
981 diff * best_den < best_diff * den) {
991 t.max = div_up(best_num, best_den);
992 t.openmax = !!(best_num % best_den);
994 err = snd_interval_refine(i, &t);
998 if (snd_interval_single(i)) {
1008 * snd_interval_list - refine the interval value from the list
1009 * @i: the interval value to refine
1010 * @count: the number of elements in the list
1011 * @list: the value list
1012 * @mask: the bit-mask to evaluate
1014 * Refines the interval value from the list.
1015 * When mask is non-zero, only the elements corresponding to bit 1 are
1018 * Returns non-zero if the value is changed, zero if not changed.
1020 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
1023 struct snd_interval list_range;
1029 snd_interval_any(&list_range);
1030 list_range.min = UINT_MAX;
1032 for (k = 0; k < count; k++) {
1033 if (mask && !(mask & (1 << k)))
1035 if (!snd_interval_test(i, list[k]))
1037 list_range.min = min(list_range.min, list[k]);
1038 list_range.max = max(list_range.max, list[k]);
1040 return snd_interval_refine(i, &list_range);
1043 EXPORT_SYMBOL(snd_interval_list);
1045 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
1049 n = (i->min - min) % step;
1050 if (n != 0 || i->openmin) {
1054 n = (i->max - min) % step;
1055 if (n != 0 || i->openmax) {
1059 if (snd_interval_checkempty(i)) {
1066 /* Info constraints helpers */
1069 * snd_pcm_hw_rule_add - add the hw-constraint rule
1070 * @runtime: the pcm runtime instance
1071 * @cond: condition bits
1072 * @var: the variable to evaluate
1073 * @func: the evaluation function
1074 * @private: the private data pointer passed to function
1075 * @dep: the dependent variables
1077 * Returns zero if successful, or a negative error code on failure.
1079 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1081 snd_pcm_hw_rule_func_t func, void *private,
1084 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1085 struct snd_pcm_hw_rule *c;
1088 va_start(args, dep);
1089 if (constrs->rules_num >= constrs->rules_all) {
1090 struct snd_pcm_hw_rule *new;
1091 unsigned int new_rules = constrs->rules_all + 16;
1092 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1095 if (constrs->rules) {
1096 memcpy(new, constrs->rules,
1097 constrs->rules_num * sizeof(*c));
1098 kfree(constrs->rules);
1100 constrs->rules = new;
1101 constrs->rules_all = new_rules;
1103 c = &constrs->rules[constrs->rules_num];
1107 c->private = private;
1110 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1115 dep = va_arg(args, int);
1117 constrs->rules_num++;
1122 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1125 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1126 * @runtime: PCM runtime instance
1127 * @var: hw_params variable to apply the mask
1128 * @mask: the bitmap mask
1130 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1132 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1135 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1136 struct snd_mask *maskp = constrs_mask(constrs, var);
1137 *maskp->bits &= mask;
1138 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1139 if (*maskp->bits == 0)
1145 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1146 * @runtime: PCM runtime instance
1147 * @var: hw_params variable to apply the mask
1148 * @mask: the 64bit bitmap mask
1150 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1152 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1155 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1156 struct snd_mask *maskp = constrs_mask(constrs, var);
1157 maskp->bits[0] &= (u_int32_t)mask;
1158 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1159 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1160 if (! maskp->bits[0] && ! maskp->bits[1])
1166 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1167 * @runtime: PCM runtime instance
1168 * @var: hw_params variable to apply the integer constraint
1170 * Apply the constraint of integer to an interval parameter.
1172 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1174 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1175 return snd_interval_setinteger(constrs_interval(constrs, var));
1178 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1181 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1182 * @runtime: PCM runtime instance
1183 * @var: hw_params variable to apply the range
1184 * @min: the minimal value
1185 * @max: the maximal value
1187 * Apply the min/max range constraint to an interval parameter.
1189 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1190 unsigned int min, unsigned int max)
1192 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1193 struct snd_interval t;
1196 t.openmin = t.openmax = 0;
1198 return snd_interval_refine(constrs_interval(constrs, var), &t);
1201 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1203 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1204 struct snd_pcm_hw_rule *rule)
1206 struct snd_pcm_hw_constraint_list *list = rule->private;
1207 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1212 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the list constraint
1218 * Apply the list of constraints to an interval parameter.
1220 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1222 snd_pcm_hw_param_t var,
1223 struct snd_pcm_hw_constraint_list *l)
1225 return snd_pcm_hw_rule_add(runtime, cond, var,
1226 snd_pcm_hw_rule_list, l,
1230 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1232 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1233 struct snd_pcm_hw_rule *rule)
1235 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1236 unsigned int num = 0, den = 0;
1238 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1239 r->nrats, r->rats, &num, &den);
1240 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1241 params->rate_num = num;
1242 params->rate_den = den;
1248 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1249 * @runtime: PCM runtime instance
1250 * @cond: condition bits
1251 * @var: hw_params variable to apply the ratnums constraint
1252 * @r: struct snd_ratnums constriants
1254 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1256 snd_pcm_hw_param_t var,
1257 struct snd_pcm_hw_constraint_ratnums *r)
1259 return snd_pcm_hw_rule_add(runtime, cond, var,
1260 snd_pcm_hw_rule_ratnums, r,
1264 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1266 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1267 struct snd_pcm_hw_rule *rule)
1269 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1270 unsigned int num = 0, den = 0;
1271 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1272 r->nrats, r->rats, &num, &den);
1273 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1274 params->rate_num = num;
1275 params->rate_den = den;
1281 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1282 * @runtime: PCM runtime instance
1283 * @cond: condition bits
1284 * @var: hw_params variable to apply the ratdens constraint
1285 * @r: struct snd_ratdens constriants
1287 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1289 snd_pcm_hw_param_t var,
1290 struct snd_pcm_hw_constraint_ratdens *r)
1292 return snd_pcm_hw_rule_add(runtime, cond, var,
1293 snd_pcm_hw_rule_ratdens, r,
1297 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1299 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1300 struct snd_pcm_hw_rule *rule)
1302 unsigned int l = (unsigned long) rule->private;
1303 int width = l & 0xffff;
1304 unsigned int msbits = l >> 16;
1305 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1306 if (snd_interval_single(i) && snd_interval_value(i) == width)
1307 params->msbits = msbits;
1312 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1313 * @runtime: PCM runtime instance
1314 * @cond: condition bits
1315 * @width: sample bits width
1316 * @msbits: msbits width
1318 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1321 unsigned int msbits)
1323 unsigned long l = (msbits << 16) | width;
1324 return snd_pcm_hw_rule_add(runtime, cond, -1,
1325 snd_pcm_hw_rule_msbits,
1327 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1330 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1332 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1333 struct snd_pcm_hw_rule *rule)
1335 unsigned long step = (unsigned long) rule->private;
1336 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1340 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1341 * @runtime: PCM runtime instance
1342 * @cond: condition bits
1343 * @var: hw_params variable to apply the step constraint
1346 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1348 snd_pcm_hw_param_t var,
1351 return snd_pcm_hw_rule_add(runtime, cond, var,
1352 snd_pcm_hw_rule_step, (void *) step,
1356 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1358 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1360 static unsigned int pow2_sizes[] = {
1361 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1362 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1363 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1364 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1366 return snd_interval_list(hw_param_interval(params, rule->var),
1367 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1371 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1372 * @runtime: PCM runtime instance
1373 * @cond: condition bits
1374 * @var: hw_params variable to apply the power-of-2 constraint
1376 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1378 snd_pcm_hw_param_t var)
1380 return snd_pcm_hw_rule_add(runtime, cond, var,
1381 snd_pcm_hw_rule_pow2, NULL,
1385 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1387 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1388 snd_pcm_hw_param_t var)
1390 if (hw_is_mask(var)) {
1391 snd_mask_any(hw_param_mask(params, var));
1392 params->cmask |= 1 << var;
1393 params->rmask |= 1 << var;
1396 if (hw_is_interval(var)) {
1397 snd_interval_any(hw_param_interval(params, var));
1398 params->cmask |= 1 << var;
1399 params->rmask |= 1 << var;
1405 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1408 memset(params, 0, sizeof(*params));
1409 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1410 _snd_pcm_hw_param_any(params, k);
1411 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1412 _snd_pcm_hw_param_any(params, k);
1416 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1419 * snd_pcm_hw_param_value - return @params field @var value
1420 * @params: the hw_params instance
1421 * @var: parameter to retrieve
1422 * @dir: pointer to the direction (-1,0,1) or %NULL
1424 * Return the value for field @var if it's fixed in configuration space
1425 * defined by @params. Return -%EINVAL otherwise.
1427 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1428 snd_pcm_hw_param_t var, int *dir)
1430 if (hw_is_mask(var)) {
1431 const struct snd_mask *mask = hw_param_mask_c(params, var);
1432 if (!snd_mask_single(mask))
1436 return snd_mask_value(mask);
1438 if (hw_is_interval(var)) {
1439 const struct snd_interval *i = hw_param_interval_c(params, var);
1440 if (!snd_interval_single(i))
1444 return snd_interval_value(i);
1449 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1451 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1452 snd_pcm_hw_param_t var)
1454 if (hw_is_mask(var)) {
1455 snd_mask_none(hw_param_mask(params, var));
1456 params->cmask |= 1 << var;
1457 params->rmask |= 1 << var;
1458 } else if (hw_is_interval(var)) {
1459 snd_interval_none(hw_param_interval(params, var));
1460 params->cmask |= 1 << var;
1461 params->rmask |= 1 << var;
1467 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1469 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1470 snd_pcm_hw_param_t var)
1473 if (hw_is_mask(var))
1474 changed = snd_mask_refine_first(hw_param_mask(params, var));
1475 else if (hw_is_interval(var))
1476 changed = snd_interval_refine_first(hw_param_interval(params, var));
1480 params->cmask |= 1 << var;
1481 params->rmask |= 1 << var;
1488 * snd_pcm_hw_param_first - refine config space and return minimum value
1489 * @pcm: PCM instance
1490 * @params: the hw_params instance
1491 * @var: parameter to retrieve
1492 * @dir: pointer to the direction (-1,0,1) or %NULL
1494 * Inside configuration space defined by @params remove from @var all
1495 * values > minimum. Reduce configuration space accordingly.
1496 * Return the minimum.
1498 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1499 struct snd_pcm_hw_params *params,
1500 snd_pcm_hw_param_t var, int *dir)
1502 int changed = _snd_pcm_hw_param_first(params, var);
1505 if (params->rmask) {
1506 int err = snd_pcm_hw_refine(pcm, params);
1507 if (snd_BUG_ON(err < 0))
1510 return snd_pcm_hw_param_value(params, var, dir);
1513 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1515 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1516 snd_pcm_hw_param_t var)
1519 if (hw_is_mask(var))
1520 changed = snd_mask_refine_last(hw_param_mask(params, var));
1521 else if (hw_is_interval(var))
1522 changed = snd_interval_refine_last(hw_param_interval(params, var));
1526 params->cmask |= 1 << var;
1527 params->rmask |= 1 << var;
1534 * snd_pcm_hw_param_last - refine config space and return maximum value
1535 * @pcm: PCM instance
1536 * @params: the hw_params instance
1537 * @var: parameter to retrieve
1538 * @dir: pointer to the direction (-1,0,1) or %NULL
1540 * Inside configuration space defined by @params remove from @var all
1541 * values < maximum. Reduce configuration space accordingly.
1542 * Return the maximum.
1544 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1545 struct snd_pcm_hw_params *params,
1546 snd_pcm_hw_param_t var, int *dir)
1548 int changed = _snd_pcm_hw_param_last(params, var);
1551 if (params->rmask) {
1552 int err = snd_pcm_hw_refine(pcm, params);
1553 if (snd_BUG_ON(err < 0))
1556 return snd_pcm_hw_param_value(params, var, dir);
1559 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1562 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1563 * @pcm: PCM instance
1564 * @params: the hw_params instance
1566 * Choose one configuration from configuration space defined by @params.
1567 * The configuration chosen is that obtained fixing in this order:
1568 * first access, first format, first subformat, min channels,
1569 * min rate, min period time, max buffer size, min tick time
1571 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1572 struct snd_pcm_hw_params *params)
1574 static int vars[] = {
1575 SNDRV_PCM_HW_PARAM_ACCESS,
1576 SNDRV_PCM_HW_PARAM_FORMAT,
1577 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1578 SNDRV_PCM_HW_PARAM_CHANNELS,
1579 SNDRV_PCM_HW_PARAM_RATE,
1580 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1581 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1582 SNDRV_PCM_HW_PARAM_TICK_TIME,
1587 for (v = vars; *v != -1; v++) {
1588 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1589 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1591 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1592 if (snd_BUG_ON(err < 0))
1598 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1601 struct snd_pcm_runtime *runtime = substream->runtime;
1602 unsigned long flags;
1603 snd_pcm_stream_lock_irqsave(substream, flags);
1604 if (snd_pcm_running(substream) &&
1605 snd_pcm_update_hw_ptr(substream) >= 0)
1606 runtime->status->hw_ptr %= runtime->buffer_size;
1608 runtime->status->hw_ptr = 0;
1609 snd_pcm_stream_unlock_irqrestore(substream, flags);
1613 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1616 struct snd_pcm_channel_info *info = arg;
1617 struct snd_pcm_runtime *runtime = substream->runtime;
1619 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1623 width = snd_pcm_format_physical_width(runtime->format);
1627 switch (runtime->access) {
1628 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1629 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1630 info->first = info->channel * width;
1631 info->step = runtime->channels * width;
1633 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1634 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1636 size_t size = runtime->dma_bytes / runtime->channels;
1637 info->first = info->channel * size * 8;
1648 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1651 struct snd_pcm_hw_params *params = arg;
1652 snd_pcm_format_t format;
1653 int channels, width;
1655 params->fifo_size = substream->runtime->hw.fifo_size;
1656 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1657 format = params_format(params);
1658 channels = params_channels(params);
1659 width = snd_pcm_format_physical_width(format);
1660 params->fifo_size /= width * channels;
1666 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1667 * @substream: the pcm substream instance
1668 * @cmd: ioctl command
1669 * @arg: ioctl argument
1671 * Processes the generic ioctl commands for PCM.
1672 * Can be passed as the ioctl callback for PCM ops.
1674 * Returns zero if successful, or a negative error code on failure.
1676 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1677 unsigned int cmd, void *arg)
1680 case SNDRV_PCM_IOCTL1_INFO:
1682 case SNDRV_PCM_IOCTL1_RESET:
1683 return snd_pcm_lib_ioctl_reset(substream, arg);
1684 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1685 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1686 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1687 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1692 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1695 * snd_pcm_period_elapsed - update the pcm status for the next period
1696 * @substream: the pcm substream instance
1698 * This function is called from the interrupt handler when the
1699 * PCM has processed the period size. It will update the current
1700 * pointer, wake up sleepers, etc.
1702 * Even if more than one periods have elapsed since the last call, you
1703 * have to call this only once.
1705 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1707 struct snd_pcm_runtime *runtime;
1708 unsigned long flags;
1710 if (PCM_RUNTIME_CHECK(substream))
1712 runtime = substream->runtime;
1714 if (runtime->transfer_ack_begin)
1715 runtime->transfer_ack_begin(substream);
1717 snd_pcm_stream_lock_irqsave(substream, flags);
1718 if (!snd_pcm_running(substream) ||
1719 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1722 if (substream->timer_running)
1723 snd_timer_interrupt(substream->timer, 1);
1725 snd_pcm_stream_unlock_irqrestore(substream, flags);
1726 if (runtime->transfer_ack_end)
1727 runtime->transfer_ack_end(substream);
1728 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1731 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1734 * Wait until avail_min data becomes available
1735 * Returns a negative error code if any error occurs during operation.
1736 * The available space is stored on availp. When err = 0 and avail = 0
1737 * on the capture stream, it indicates the stream is in DRAINING state.
1739 static int wait_for_avail(struct snd_pcm_substream *substream,
1740 snd_pcm_uframes_t *availp)
1742 struct snd_pcm_runtime *runtime = substream->runtime;
1743 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1746 snd_pcm_uframes_t avail = 0;
1749 init_waitqueue_entry(&wait, current);
1750 add_wait_queue(&runtime->tsleep, &wait);
1752 if (signal_pending(current)) {
1756 set_current_state(TASK_INTERRUPTIBLE);
1757 snd_pcm_stream_unlock_irq(substream);
1758 tout = schedule_timeout(msecs_to_jiffies(10000));
1759 snd_pcm_stream_lock_irq(substream);
1760 switch (runtime->status->state) {
1761 case SNDRV_PCM_STATE_SUSPENDED:
1764 case SNDRV_PCM_STATE_XRUN:
1767 case SNDRV_PCM_STATE_DRAINING:
1771 avail = 0; /* indicate draining */
1773 case SNDRV_PCM_STATE_OPEN:
1774 case SNDRV_PCM_STATE_SETUP:
1775 case SNDRV_PCM_STATE_DISCONNECTED:
1780 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1781 is_playback ? "playback" : "capture");
1786 avail = snd_pcm_playback_avail(runtime);
1788 avail = snd_pcm_capture_avail(runtime);
1789 if (avail >= runtime->twake)
1793 remove_wait_queue(&runtime->tsleep, &wait);
1798 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1800 unsigned long data, unsigned int off,
1801 snd_pcm_uframes_t frames)
1803 struct snd_pcm_runtime *runtime = substream->runtime;
1805 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1806 if (substream->ops->copy) {
1807 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1810 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1811 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1817 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1818 unsigned long data, unsigned int off,
1819 snd_pcm_uframes_t size);
1821 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1823 snd_pcm_uframes_t size,
1825 transfer_f transfer)
1827 struct snd_pcm_runtime *runtime = substream->runtime;
1828 snd_pcm_uframes_t xfer = 0;
1829 snd_pcm_uframes_t offset = 0;
1835 snd_pcm_stream_lock_irq(substream);
1836 switch (runtime->status->state) {
1837 case SNDRV_PCM_STATE_PREPARED:
1838 case SNDRV_PCM_STATE_RUNNING:
1839 case SNDRV_PCM_STATE_PAUSED:
1841 case SNDRV_PCM_STATE_XRUN:
1844 case SNDRV_PCM_STATE_SUSPENDED:
1852 runtime->twake = runtime->control->avail_min ? : 1;
1854 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1855 snd_pcm_uframes_t avail;
1856 snd_pcm_uframes_t cont;
1857 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1858 snd_pcm_update_hw_ptr(substream);
1859 avail = snd_pcm_playback_avail(runtime);
1865 runtime->twake = min_t(snd_pcm_uframes_t, size,
1866 runtime->control->avail_min ? : 1);
1867 err = wait_for_avail(substream, &avail);
1871 frames = size > avail ? avail : size;
1872 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1875 if (snd_BUG_ON(!frames)) {
1877 snd_pcm_stream_unlock_irq(substream);
1880 appl_ptr = runtime->control->appl_ptr;
1881 appl_ofs = appl_ptr % runtime->buffer_size;
1882 snd_pcm_stream_unlock_irq(substream);
1883 err = transfer(substream, appl_ofs, data, offset, frames);
1884 snd_pcm_stream_lock_irq(substream);
1887 switch (runtime->status->state) {
1888 case SNDRV_PCM_STATE_XRUN:
1891 case SNDRV_PCM_STATE_SUSPENDED:
1898 if (appl_ptr >= runtime->boundary)
1899 appl_ptr -= runtime->boundary;
1900 runtime->control->appl_ptr = appl_ptr;
1901 if (substream->ops->ack)
1902 substream->ops->ack(substream);
1907 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1908 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1909 err = snd_pcm_start(substream);
1916 if (xfer > 0 && err >= 0)
1917 snd_pcm_update_state(substream, runtime);
1918 snd_pcm_stream_unlock_irq(substream);
1919 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1922 /* sanity-check for read/write methods */
1923 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1925 struct snd_pcm_runtime *runtime;
1926 if (PCM_RUNTIME_CHECK(substream))
1928 runtime = substream->runtime;
1929 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1931 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1936 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1938 struct snd_pcm_runtime *runtime;
1942 err = pcm_sanity_check(substream);
1945 runtime = substream->runtime;
1946 nonblock = !!(substream->f_flags & O_NONBLOCK);
1948 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1949 runtime->channels > 1)
1951 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1952 snd_pcm_lib_write_transfer);
1955 EXPORT_SYMBOL(snd_pcm_lib_write);
1957 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1959 unsigned long data, unsigned int off,
1960 snd_pcm_uframes_t frames)
1962 struct snd_pcm_runtime *runtime = substream->runtime;
1964 void __user **bufs = (void __user **)data;
1965 int channels = runtime->channels;
1967 if (substream->ops->copy) {
1968 if (snd_BUG_ON(!substream->ops->silence))
1970 for (c = 0; c < channels; ++c, ++bufs) {
1971 if (*bufs == NULL) {
1972 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1975 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1976 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1981 /* default transfer behaviour */
1982 size_t dma_csize = runtime->dma_bytes / channels;
1983 for (c = 0; c < channels; ++c, ++bufs) {
1984 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1985 if (*bufs == NULL) {
1986 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1988 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1989 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1997 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1999 snd_pcm_uframes_t frames)
2001 struct snd_pcm_runtime *runtime;
2005 err = pcm_sanity_check(substream);
2008 runtime = substream->runtime;
2009 nonblock = !!(substream->f_flags & O_NONBLOCK);
2011 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2013 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2014 nonblock, snd_pcm_lib_writev_transfer);
2017 EXPORT_SYMBOL(snd_pcm_lib_writev);
2019 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2021 unsigned long data, unsigned int off,
2022 snd_pcm_uframes_t frames)
2024 struct snd_pcm_runtime *runtime = substream->runtime;
2026 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2027 if (substream->ops->copy) {
2028 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2031 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2032 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2038 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2040 snd_pcm_uframes_t size,
2042 transfer_f transfer)
2044 struct snd_pcm_runtime *runtime = substream->runtime;
2045 snd_pcm_uframes_t xfer = 0;
2046 snd_pcm_uframes_t offset = 0;
2052 snd_pcm_stream_lock_irq(substream);
2053 switch (runtime->status->state) {
2054 case SNDRV_PCM_STATE_PREPARED:
2055 if (size >= runtime->start_threshold) {
2056 err = snd_pcm_start(substream);
2061 case SNDRV_PCM_STATE_DRAINING:
2062 case SNDRV_PCM_STATE_RUNNING:
2063 case SNDRV_PCM_STATE_PAUSED:
2065 case SNDRV_PCM_STATE_XRUN:
2068 case SNDRV_PCM_STATE_SUSPENDED:
2076 runtime->twake = runtime->control->avail_min ? : 1;
2078 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2079 snd_pcm_uframes_t avail;
2080 snd_pcm_uframes_t cont;
2081 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2082 snd_pcm_update_hw_ptr(substream);
2083 avail = snd_pcm_capture_avail(runtime);
2085 if (runtime->status->state ==
2086 SNDRV_PCM_STATE_DRAINING) {
2087 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2094 runtime->twake = min_t(snd_pcm_uframes_t, size,
2095 runtime->control->avail_min ? : 1);
2096 err = wait_for_avail(substream, &avail);
2100 continue; /* draining */
2102 frames = size > avail ? avail : size;
2103 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2106 if (snd_BUG_ON(!frames)) {
2108 snd_pcm_stream_unlock_irq(substream);
2111 appl_ptr = runtime->control->appl_ptr;
2112 appl_ofs = appl_ptr % runtime->buffer_size;
2113 snd_pcm_stream_unlock_irq(substream);
2114 err = transfer(substream, appl_ofs, data, offset, frames);
2115 snd_pcm_stream_lock_irq(substream);
2118 switch (runtime->status->state) {
2119 case SNDRV_PCM_STATE_XRUN:
2122 case SNDRV_PCM_STATE_SUSPENDED:
2129 if (appl_ptr >= runtime->boundary)
2130 appl_ptr -= runtime->boundary;
2131 runtime->control->appl_ptr = appl_ptr;
2132 if (substream->ops->ack)
2133 substream->ops->ack(substream);
2141 if (xfer > 0 && err >= 0)
2142 snd_pcm_update_state(substream, runtime);
2143 snd_pcm_stream_unlock_irq(substream);
2144 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2147 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2149 struct snd_pcm_runtime *runtime;
2153 err = pcm_sanity_check(substream);
2156 runtime = substream->runtime;
2157 nonblock = !!(substream->f_flags & O_NONBLOCK);
2158 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2160 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2163 EXPORT_SYMBOL(snd_pcm_lib_read);
2165 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2167 unsigned long data, unsigned int off,
2168 snd_pcm_uframes_t frames)
2170 struct snd_pcm_runtime *runtime = substream->runtime;
2172 void __user **bufs = (void __user **)data;
2173 int channels = runtime->channels;
2175 if (substream->ops->copy) {
2176 for (c = 0; c < channels; ++c, ++bufs) {
2180 buf = *bufs + samples_to_bytes(runtime, off);
2181 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2185 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2186 for (c = 0; c < channels; ++c, ++bufs) {
2192 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2193 buf = *bufs + samples_to_bytes(runtime, off);
2194 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2201 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2203 snd_pcm_uframes_t frames)
2205 struct snd_pcm_runtime *runtime;
2209 err = pcm_sanity_check(substream);
2212 runtime = substream->runtime;
2213 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2216 nonblock = !!(substream->f_flags & O_NONBLOCK);
2217 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2219 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2222 EXPORT_SYMBOL(snd_pcm_lib_readv);