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 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
82 runtime->silence_start = new_hw_ptr;
84 runtime->silence_start = ofs;
87 frames = runtime->buffer_size - runtime->silence_filled;
89 if (snd_BUG_ON(frames > runtime->buffer_size))
93 ofs = runtime->silence_start % runtime->buffer_size;
95 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
96 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
97 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
98 if (substream->ops->silence) {
100 err = substream->ops->silence(substream, -1, ofs, transfer);
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
112 err = substream->ops->silence(substream, c, ofs, transfer);
116 size_t dma_csize = runtime->dma_bytes / channels;
117 for (c = 0; c < channels; ++c) {
118 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
119 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
123 runtime->silence_filled += transfer;
129 #define XRUN_DEBUG_BASIC (1<<0)
130 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
131 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
132 #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
133 #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
135 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
136 #define xrun_debug(substream, mask) \
137 ((substream)->pstr->xrun_debug & (mask))
139 #define xrun_debug(substream, mask) 0
142 #define dump_stack_on_xrun(substream) do { \
143 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
147 static void pcm_debug_name(struct snd_pcm_substream *substream,
148 char *name, size_t len)
150 snprintf(name, len, "pcmC%dD%d%c:%d",
151 substream->pcm->card->number,
152 substream->pcm->device,
153 substream->stream ? 'c' : 'p',
157 static void xrun(struct snd_pcm_substream *substream)
159 struct snd_pcm_runtime *runtime = substream->runtime;
161 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
162 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
163 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
164 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
166 pcm_debug_name(substream, name, sizeof(name));
167 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
168 dump_stack_on_xrun(substream);
172 static snd_pcm_uframes_t
173 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
174 struct snd_pcm_runtime *runtime)
176 snd_pcm_uframes_t pos;
178 pos = substream->ops->pointer(substream);
179 if (pos == SNDRV_PCM_POS_XRUN)
180 return pos; /* XRUN */
181 if (pos >= runtime->buffer_size) {
182 if (printk_ratelimit()) {
184 pcm_debug_name(substream, name, sizeof(name));
185 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
186 "buffer size = 0x%lx, period size = 0x%lx\n",
187 name, pos, runtime->buffer_size,
188 runtime->period_size);
192 pos -= pos % runtime->min_align;
196 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
197 struct snd_pcm_runtime *runtime)
199 snd_pcm_uframes_t avail;
201 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
202 avail = snd_pcm_playback_avail(runtime);
204 avail = snd_pcm_capture_avail(runtime);
205 if (avail > runtime->avail_max)
206 runtime->avail_max = avail;
207 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
208 if (avail >= runtime->buffer_size) {
209 snd_pcm_drain_done(substream);
213 if (avail >= runtime->stop_threshold) {
218 if (avail >= runtime->control->avail_min)
219 wake_up(&runtime->sleep);
223 #define hw_ptr_error(substream, fmt, args...) \
225 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
226 if (printk_ratelimit()) { \
227 snd_printd("PCM: " fmt, ##args); \
229 dump_stack_on_xrun(substream); \
233 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
235 struct snd_pcm_runtime *runtime = substream->runtime;
236 snd_pcm_uframes_t pos;
237 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
238 snd_pcm_sframes_t hdelta, delta;
239 unsigned long jdelta;
241 old_hw_ptr = runtime->status->hw_ptr;
242 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
243 if (pos == SNDRV_PCM_POS_XRUN) {
247 if (xrun_debug(substream, XRUN_DEBUG_PERIODUPDATE)) {
249 pcm_debug_name(substream, name, sizeof(name));
250 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
251 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
252 name, (unsigned int)pos,
253 (unsigned int)runtime->period_size,
254 (unsigned int)runtime->buffer_size,
255 (unsigned long)old_hw_ptr,
256 (unsigned long)runtime->hw_ptr_base,
257 (unsigned long)runtime->hw_ptr_interrupt);
259 hw_base = runtime->hw_ptr_base;
260 new_hw_ptr = hw_base + pos;
261 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
262 delta = new_hw_ptr - hw_ptr_interrupt;
263 if (hw_ptr_interrupt >= runtime->boundary) {
264 hw_ptr_interrupt -= runtime->boundary;
265 if (hw_base < runtime->boundary / 2)
266 /* hw_base was already lapped; recalc delta */
267 delta = new_hw_ptr - hw_ptr_interrupt;
270 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
271 delta += runtime->buffer_size;
273 hw_ptr_error(substream,
274 "Unexpected hw_pointer value "
275 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
276 substream->stream, (long)pos,
277 (long)hw_ptr_interrupt);
279 /* simply skipping the hwptr update seems more
280 * robust in some cases, e.g. on VMware with
281 * inaccurate timer source
283 return 0; /* skip this update */
285 /* rebase to interrupt position */
286 hw_base = new_hw_ptr = hw_ptr_interrupt;
287 /* align hw_base to buffer_size */
288 hw_base -= hw_base % runtime->buffer_size;
292 hw_base += runtime->buffer_size;
293 if (hw_base >= runtime->boundary)
295 new_hw_ptr = hw_base + pos;
299 /* Do jiffies check only in xrun_debug mode */
300 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
301 goto no_jiffies_check;
303 /* Skip the jiffies check for hardwares with BATCH flag.
304 * Such hardware usually just increases the position at each IRQ,
305 * thus it can't give any strange position.
307 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
308 goto no_jiffies_check;
309 hdelta = new_hw_ptr - old_hw_ptr;
310 if (hdelta < runtime->delay)
311 goto no_jiffies_check;
312 hdelta -= runtime->delay;
313 jdelta = jiffies - runtime->hw_ptr_jiffies;
314 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
316 (((runtime->period_size * HZ) / runtime->rate)
318 hw_ptr_error(substream,
319 "hw_ptr skipping! [Q] "
320 "(pos=%ld, delta=%ld, period=%ld, "
321 "jdelta=%lu/%lu/%lu)\n",
322 (long)pos, (long)hdelta,
323 (long)runtime->period_size, jdelta,
324 ((hdelta * HZ) / runtime->rate), delta);
325 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
326 runtime->period_size * delta;
327 if (hw_ptr_interrupt >= runtime->boundary)
328 hw_ptr_interrupt -= runtime->boundary;
329 /* rebase to interrupt position */
330 hw_base = new_hw_ptr = hw_ptr_interrupt;
331 /* align hw_base to buffer_size */
332 hw_base -= hw_base % runtime->buffer_size;
336 if (delta > runtime->period_size + runtime->period_size / 2) {
337 hw_ptr_error(substream,
339 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
340 substream->stream, (long)delta,
341 (long)hw_ptr_interrupt);
342 /* rebase hw_ptr_interrupt */
344 new_hw_ptr - new_hw_ptr % runtime->period_size;
346 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
349 runtime->silence_size > 0)
350 snd_pcm_playback_silence(substream, new_hw_ptr);
352 if (runtime->status->hw_ptr == new_hw_ptr)
355 runtime->hw_ptr_base = hw_base;
356 runtime->status->hw_ptr = new_hw_ptr;
357 runtime->hw_ptr_jiffies = jiffies;
358 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
359 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
361 return snd_pcm_update_hw_ptr_post(substream, runtime);
364 /* CAUTION: call it with irq disabled */
365 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
367 struct snd_pcm_runtime *runtime = substream->runtime;
368 snd_pcm_uframes_t pos;
369 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
370 snd_pcm_sframes_t delta;
371 unsigned long jdelta;
373 old_hw_ptr = runtime->status->hw_ptr;
374 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
375 if (pos == SNDRV_PCM_POS_XRUN) {
379 if (xrun_debug(substream, XRUN_DEBUG_HWPTRUPDATE)) {
381 pcm_debug_name(substream, name, sizeof(name));
382 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
383 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
384 name, (unsigned int)pos,
385 (unsigned int)runtime->period_size,
386 (unsigned int)runtime->buffer_size,
387 (unsigned long)old_hw_ptr,
388 (unsigned long)runtime->hw_ptr_base,
389 (unsigned long)runtime->hw_ptr_interrupt);
392 hw_base = runtime->hw_ptr_base;
393 new_hw_ptr = hw_base + pos;
395 delta = new_hw_ptr - old_hw_ptr;
396 jdelta = jiffies - runtime->hw_ptr_jiffies;
398 delta += runtime->buffer_size;
400 hw_ptr_error(substream,
401 "Unexpected hw_pointer value [2] "
402 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
403 substream->stream, (long)pos,
404 (long)old_hw_ptr, jdelta);
407 hw_base += runtime->buffer_size;
408 if (hw_base >= runtime->boundary)
410 new_hw_ptr = hw_base + pos;
412 /* Do jiffies check only in xrun_debug mode */
413 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
414 goto no_jiffies_check;
415 if (delta < runtime->delay)
416 goto no_jiffies_check;
417 delta -= runtime->delay;
418 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
419 hw_ptr_error(substream,
421 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
422 (long)pos, (long)delta,
423 (long)runtime->period_size, jdelta,
424 ((delta * HZ) / runtime->rate));
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
429 runtime->silence_size > 0)
430 snd_pcm_playback_silence(substream, new_hw_ptr);
432 if (runtime->status->hw_ptr == new_hw_ptr)
435 runtime->hw_ptr_base = hw_base;
436 runtime->status->hw_ptr = new_hw_ptr;
437 runtime->hw_ptr_jiffies = jiffies;
438 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
439 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
441 return snd_pcm_update_hw_ptr_post(substream, runtime);
445 * snd_pcm_set_ops - set the PCM operators
446 * @pcm: the pcm instance
447 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
448 * @ops: the operator table
450 * Sets the given PCM operators to the pcm instance.
452 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
454 struct snd_pcm_str *stream = &pcm->streams[direction];
455 struct snd_pcm_substream *substream;
457 for (substream = stream->substream; substream != NULL; substream = substream->next)
458 substream->ops = ops;
461 EXPORT_SYMBOL(snd_pcm_set_ops);
464 * snd_pcm_sync - set the PCM sync id
465 * @substream: the pcm substream
467 * Sets the PCM sync identifier for the card.
469 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
471 struct snd_pcm_runtime *runtime = substream->runtime;
473 runtime->sync.id32[0] = substream->pcm->card->number;
474 runtime->sync.id32[1] = -1;
475 runtime->sync.id32[2] = -1;
476 runtime->sync.id32[3] = -1;
479 EXPORT_SYMBOL(snd_pcm_set_sync);
482 * Standard ioctl routine
485 static inline unsigned int div32(unsigned int a, unsigned int b,
496 static inline unsigned int div_down(unsigned int a, unsigned int b)
503 static inline unsigned int div_up(unsigned int a, unsigned int b)
515 static inline unsigned int mul(unsigned int a, unsigned int b)
519 if (div_down(UINT_MAX, a) < b)
524 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
525 unsigned int c, unsigned int *r)
527 u_int64_t n = (u_int64_t) a * b;
533 n = div_u64_rem(n, c, r);
542 * snd_interval_refine - refine the interval value of configurator
543 * @i: the interval value to refine
544 * @v: the interval value to refer to
546 * Refines the interval value with the reference value.
547 * The interval is changed to the range satisfying both intervals.
548 * The interval status (min, max, integer, etc.) are evaluated.
550 * Returns non-zero if the value is changed, zero if not changed.
552 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
555 if (snd_BUG_ON(snd_interval_empty(i)))
557 if (i->min < v->min) {
559 i->openmin = v->openmin;
561 } else if (i->min == v->min && !i->openmin && v->openmin) {
565 if (i->max > v->max) {
567 i->openmax = v->openmax;
569 } else if (i->max == v->max && !i->openmax && v->openmax) {
573 if (!i->integer && v->integer) {
586 } else if (!i->openmin && !i->openmax && i->min == i->max)
588 if (snd_interval_checkempty(i)) {
589 snd_interval_none(i);
595 EXPORT_SYMBOL(snd_interval_refine);
597 static int snd_interval_refine_first(struct snd_interval *i)
599 if (snd_BUG_ON(snd_interval_empty(i)))
601 if (snd_interval_single(i))
604 i->openmax = i->openmin;
610 static int snd_interval_refine_last(struct snd_interval *i)
612 if (snd_BUG_ON(snd_interval_empty(i)))
614 if (snd_interval_single(i))
617 i->openmin = i->openmax;
623 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
625 if (a->empty || b->empty) {
626 snd_interval_none(c);
630 c->min = mul(a->min, b->min);
631 c->openmin = (a->openmin || b->openmin);
632 c->max = mul(a->max, b->max);
633 c->openmax = (a->openmax || b->openmax);
634 c->integer = (a->integer && b->integer);
638 * snd_interval_div - refine the interval value with division
645 * Returns non-zero if the value is changed, zero if not changed.
647 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
650 if (a->empty || b->empty) {
651 snd_interval_none(c);
655 c->min = div32(a->min, b->max, &r);
656 c->openmin = (r || a->openmin || b->openmax);
658 c->max = div32(a->max, b->min, &r);
663 c->openmax = (a->openmax || b->openmin);
672 * snd_interval_muldivk - refine the interval value
675 * @k: divisor (as integer)
680 * Returns non-zero if the value is changed, zero if not changed.
682 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
683 unsigned int k, struct snd_interval *c)
686 if (a->empty || b->empty) {
687 snd_interval_none(c);
691 c->min = muldiv32(a->min, b->min, k, &r);
692 c->openmin = (r || a->openmin || b->openmin);
693 c->max = muldiv32(a->max, b->max, k, &r);
698 c->openmax = (a->openmax || b->openmax);
703 * snd_interval_mulkdiv - refine the interval value
705 * @k: dividend 2 (as integer)
711 * Returns non-zero if the value is changed, zero if not changed.
713 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
714 const struct snd_interval *b, struct snd_interval *c)
717 if (a->empty || b->empty) {
718 snd_interval_none(c);
722 c->min = muldiv32(a->min, k, b->max, &r);
723 c->openmin = (r || a->openmin || b->openmax);
725 c->max = muldiv32(a->max, k, b->min, &r);
730 c->openmax = (a->openmax || b->openmin);
742 * snd_interval_ratnum - refine the interval value
743 * @i: interval to refine
744 * @rats_count: number of ratnum_t
745 * @rats: ratnum_t array
746 * @nump: pointer to store the resultant numerator
747 * @denp: pointer to store the resultant denominator
749 * Returns non-zero if the value is changed, zero if not changed.
751 int snd_interval_ratnum(struct snd_interval *i,
752 unsigned int rats_count, struct snd_ratnum *rats,
753 unsigned int *nump, unsigned int *denp)
755 unsigned int best_num, best_diff, best_den;
757 struct snd_interval t;
760 best_num = best_den = best_diff = 0;
761 for (k = 0; k < rats_count; ++k) {
762 unsigned int num = rats[k].num;
764 unsigned int q = i->min;
768 den = div_down(num, q);
769 if (den < rats[k].den_min)
771 if (den > rats[k].den_max)
772 den = rats[k].den_max;
775 r = (den - rats[k].den_min) % rats[k].den_step;
779 diff = num - q * den;
781 diff * best_den < best_diff * den) {
791 t.min = div_down(best_num, best_den);
792 t.openmin = !!(best_num % best_den);
794 best_num = best_den = best_diff = 0;
795 for (k = 0; k < rats_count; ++k) {
796 unsigned int num = rats[k].num;
798 unsigned int q = i->max;
804 den = div_up(num, q);
805 if (den > rats[k].den_max)
807 if (den < rats[k].den_min)
808 den = rats[k].den_min;
811 r = (den - rats[k].den_min) % rats[k].den_step;
813 den += rats[k].den_step - r;
815 diff = q * den - num;
817 diff * best_den < best_diff * den) {
827 t.max = div_up(best_num, best_den);
828 t.openmax = !!(best_num % best_den);
830 err = snd_interval_refine(i, &t);
834 if (snd_interval_single(i)) {
843 EXPORT_SYMBOL(snd_interval_ratnum);
846 * snd_interval_ratden - refine the interval value
847 * @i: interval to refine
848 * @rats_count: number of struct ratden
849 * @rats: struct ratden array
850 * @nump: pointer to store the resultant numerator
851 * @denp: pointer to store the resultant denominator
853 * Returns non-zero if the value is changed, zero if not changed.
855 static int snd_interval_ratden(struct snd_interval *i,
856 unsigned int rats_count, struct snd_ratden *rats,
857 unsigned int *nump, unsigned int *denp)
859 unsigned int best_num, best_diff, best_den;
861 struct snd_interval t;
864 best_num = best_den = best_diff = 0;
865 for (k = 0; k < rats_count; ++k) {
867 unsigned int den = rats[k].den;
868 unsigned int q = i->min;
871 if (num > rats[k].num_max)
873 if (num < rats[k].num_min)
874 num = rats[k].num_max;
877 r = (num - rats[k].num_min) % rats[k].num_step;
879 num += rats[k].num_step - r;
881 diff = num - q * den;
883 diff * best_den < best_diff * den) {
893 t.min = div_down(best_num, best_den);
894 t.openmin = !!(best_num % best_den);
896 best_num = best_den = best_diff = 0;
897 for (k = 0; k < rats_count; ++k) {
899 unsigned int den = rats[k].den;
900 unsigned int q = i->max;
903 if (num < rats[k].num_min)
905 if (num > rats[k].num_max)
906 num = rats[k].num_max;
909 r = (num - rats[k].num_min) % rats[k].num_step;
913 diff = q * den - num;
915 diff * best_den < best_diff * den) {
925 t.max = div_up(best_num, best_den);
926 t.openmax = !!(best_num % best_den);
928 err = snd_interval_refine(i, &t);
932 if (snd_interval_single(i)) {
942 * snd_interval_list - refine the interval value from the list
943 * @i: the interval value to refine
944 * @count: the number of elements in the list
945 * @list: the value list
946 * @mask: the bit-mask to evaluate
948 * Refines the interval value from the list.
949 * When mask is non-zero, only the elements corresponding to bit 1 are
952 * Returns non-zero if the value is changed, zero if not changed.
954 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
957 struct snd_interval list_range;
963 snd_interval_any(&list_range);
964 list_range.min = UINT_MAX;
966 for (k = 0; k < count; k++) {
967 if (mask && !(mask & (1 << k)))
969 if (!snd_interval_test(i, list[k]))
971 list_range.min = min(list_range.min, list[k]);
972 list_range.max = max(list_range.max, list[k]);
974 return snd_interval_refine(i, &list_range);
977 EXPORT_SYMBOL(snd_interval_list);
979 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
983 n = (i->min - min) % step;
984 if (n != 0 || i->openmin) {
988 n = (i->max - min) % step;
989 if (n != 0 || i->openmax) {
993 if (snd_interval_checkempty(i)) {
1000 /* Info constraints helpers */
1003 * snd_pcm_hw_rule_add - add the hw-constraint rule
1004 * @runtime: the pcm runtime instance
1005 * @cond: condition bits
1006 * @var: the variable to evaluate
1007 * @func: the evaluation function
1008 * @private: the private data pointer passed to function
1009 * @dep: the dependent variables
1011 * Returns zero if successful, or a negative error code on failure.
1013 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1015 snd_pcm_hw_rule_func_t func, void *private,
1018 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1019 struct snd_pcm_hw_rule *c;
1022 va_start(args, dep);
1023 if (constrs->rules_num >= constrs->rules_all) {
1024 struct snd_pcm_hw_rule *new;
1025 unsigned int new_rules = constrs->rules_all + 16;
1026 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1029 if (constrs->rules) {
1030 memcpy(new, constrs->rules,
1031 constrs->rules_num * sizeof(*c));
1032 kfree(constrs->rules);
1034 constrs->rules = new;
1035 constrs->rules_all = new_rules;
1037 c = &constrs->rules[constrs->rules_num];
1041 c->private = private;
1044 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1049 dep = va_arg(args, int);
1051 constrs->rules_num++;
1056 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1059 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1060 * @runtime: PCM runtime instance
1061 * @var: hw_params variable to apply the mask
1062 * @mask: the bitmap mask
1064 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1066 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1069 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1070 struct snd_mask *maskp = constrs_mask(constrs, var);
1071 *maskp->bits &= mask;
1072 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1073 if (*maskp->bits == 0)
1079 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1080 * @runtime: PCM runtime instance
1081 * @var: hw_params variable to apply the mask
1082 * @mask: the 64bit bitmap mask
1084 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1086 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1089 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1090 struct snd_mask *maskp = constrs_mask(constrs, var);
1091 maskp->bits[0] &= (u_int32_t)mask;
1092 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1093 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1094 if (! maskp->bits[0] && ! maskp->bits[1])
1100 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1101 * @runtime: PCM runtime instance
1102 * @var: hw_params variable to apply the integer constraint
1104 * Apply the constraint of integer to an interval parameter.
1106 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1108 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1109 return snd_interval_setinteger(constrs_interval(constrs, var));
1112 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1115 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1116 * @runtime: PCM runtime instance
1117 * @var: hw_params variable to apply the range
1118 * @min: the minimal value
1119 * @max: the maximal value
1121 * Apply the min/max range constraint to an interval parameter.
1123 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1124 unsigned int min, unsigned int max)
1126 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1127 struct snd_interval t;
1130 t.openmin = t.openmax = 0;
1132 return snd_interval_refine(constrs_interval(constrs, var), &t);
1135 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1137 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1138 struct snd_pcm_hw_rule *rule)
1140 struct snd_pcm_hw_constraint_list *list = rule->private;
1141 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1146 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @var: hw_params variable to apply the list constraint
1152 * Apply the list of constraints to an interval parameter.
1154 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1156 snd_pcm_hw_param_t var,
1157 struct snd_pcm_hw_constraint_list *l)
1159 return snd_pcm_hw_rule_add(runtime, cond, var,
1160 snd_pcm_hw_rule_list, l,
1164 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1166 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
1169 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1170 unsigned int num = 0, den = 0;
1172 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1173 r->nrats, r->rats, &num, &den);
1174 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1175 params->rate_num = num;
1176 params->rate_den = den;
1182 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1183 * @runtime: PCM runtime instance
1184 * @cond: condition bits
1185 * @var: hw_params variable to apply the ratnums constraint
1186 * @r: struct snd_ratnums constriants
1188 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1190 snd_pcm_hw_param_t var,
1191 struct snd_pcm_hw_constraint_ratnums *r)
1193 return snd_pcm_hw_rule_add(runtime, cond, var,
1194 snd_pcm_hw_rule_ratnums, r,
1198 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1200 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1201 struct snd_pcm_hw_rule *rule)
1203 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1204 unsigned int num = 0, den = 0;
1205 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1206 r->nrats, r->rats, &num, &den);
1207 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1208 params->rate_num = num;
1209 params->rate_den = den;
1215 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1216 * @runtime: PCM runtime instance
1217 * @cond: condition bits
1218 * @var: hw_params variable to apply the ratdens constraint
1219 * @r: struct snd_ratdens constriants
1221 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1223 snd_pcm_hw_param_t var,
1224 struct snd_pcm_hw_constraint_ratdens *r)
1226 return snd_pcm_hw_rule_add(runtime, cond, var,
1227 snd_pcm_hw_rule_ratdens, r,
1231 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1233 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1234 struct snd_pcm_hw_rule *rule)
1236 unsigned int l = (unsigned long) rule->private;
1237 int width = l & 0xffff;
1238 unsigned int msbits = l >> 16;
1239 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1240 if (snd_interval_single(i) && snd_interval_value(i) == width)
1241 params->msbits = msbits;
1246 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1247 * @runtime: PCM runtime instance
1248 * @cond: condition bits
1249 * @width: sample bits width
1250 * @msbits: msbits width
1252 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1255 unsigned int msbits)
1257 unsigned long l = (msbits << 16) | width;
1258 return snd_pcm_hw_rule_add(runtime, cond, -1,
1259 snd_pcm_hw_rule_msbits,
1261 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1264 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1266 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1267 struct snd_pcm_hw_rule *rule)
1269 unsigned long step = (unsigned long) rule->private;
1270 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1274 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1275 * @runtime: PCM runtime instance
1276 * @cond: condition bits
1277 * @var: hw_params variable to apply the step constraint
1280 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1282 snd_pcm_hw_param_t var,
1285 return snd_pcm_hw_rule_add(runtime, cond, var,
1286 snd_pcm_hw_rule_step, (void *) step,
1290 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1292 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1294 static unsigned int pow2_sizes[] = {
1295 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1296 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1297 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1298 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1300 return snd_interval_list(hw_param_interval(params, rule->var),
1301 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1305 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1306 * @runtime: PCM runtime instance
1307 * @cond: condition bits
1308 * @var: hw_params variable to apply the power-of-2 constraint
1310 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1312 snd_pcm_hw_param_t var)
1314 return snd_pcm_hw_rule_add(runtime, cond, var,
1315 snd_pcm_hw_rule_pow2, NULL,
1319 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1321 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1322 snd_pcm_hw_param_t var)
1324 if (hw_is_mask(var)) {
1325 snd_mask_any(hw_param_mask(params, var));
1326 params->cmask |= 1 << var;
1327 params->rmask |= 1 << var;
1330 if (hw_is_interval(var)) {
1331 snd_interval_any(hw_param_interval(params, var));
1332 params->cmask |= 1 << var;
1333 params->rmask |= 1 << var;
1339 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1342 memset(params, 0, sizeof(*params));
1343 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1344 _snd_pcm_hw_param_any(params, k);
1345 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1346 _snd_pcm_hw_param_any(params, k);
1350 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1353 * snd_pcm_hw_param_value - return @params field @var value
1354 * @params: the hw_params instance
1355 * @var: parameter to retrieve
1356 * @dir: pointer to the direction (-1,0,1) or %NULL
1358 * Return the value for field @var if it's fixed in configuration space
1359 * defined by @params. Return -%EINVAL otherwise.
1361 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1362 snd_pcm_hw_param_t var, int *dir)
1364 if (hw_is_mask(var)) {
1365 const struct snd_mask *mask = hw_param_mask_c(params, var);
1366 if (!snd_mask_single(mask))
1370 return snd_mask_value(mask);
1372 if (hw_is_interval(var)) {
1373 const struct snd_interval *i = hw_param_interval_c(params, var);
1374 if (!snd_interval_single(i))
1378 return snd_interval_value(i);
1383 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1385 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1386 snd_pcm_hw_param_t var)
1388 if (hw_is_mask(var)) {
1389 snd_mask_none(hw_param_mask(params, var));
1390 params->cmask |= 1 << var;
1391 params->rmask |= 1 << var;
1392 } else if (hw_is_interval(var)) {
1393 snd_interval_none(hw_param_interval(params, var));
1394 params->cmask |= 1 << var;
1395 params->rmask |= 1 << var;
1401 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1403 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1404 snd_pcm_hw_param_t var)
1407 if (hw_is_mask(var))
1408 changed = snd_mask_refine_first(hw_param_mask(params, var));
1409 else if (hw_is_interval(var))
1410 changed = snd_interval_refine_first(hw_param_interval(params, var));
1414 params->cmask |= 1 << var;
1415 params->rmask |= 1 << var;
1422 * snd_pcm_hw_param_first - refine config space and return minimum value
1423 * @pcm: PCM instance
1424 * @params: the hw_params instance
1425 * @var: parameter to retrieve
1426 * @dir: pointer to the direction (-1,0,1) or %NULL
1428 * Inside configuration space defined by @params remove from @var all
1429 * values > minimum. Reduce configuration space accordingly.
1430 * Return the minimum.
1432 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1433 struct snd_pcm_hw_params *params,
1434 snd_pcm_hw_param_t var, int *dir)
1436 int changed = _snd_pcm_hw_param_first(params, var);
1439 if (params->rmask) {
1440 int err = snd_pcm_hw_refine(pcm, params);
1441 if (snd_BUG_ON(err < 0))
1444 return snd_pcm_hw_param_value(params, var, dir);
1447 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1449 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1450 snd_pcm_hw_param_t var)
1453 if (hw_is_mask(var))
1454 changed = snd_mask_refine_last(hw_param_mask(params, var));
1455 else if (hw_is_interval(var))
1456 changed = snd_interval_refine_last(hw_param_interval(params, var));
1460 params->cmask |= 1 << var;
1461 params->rmask |= 1 << var;
1468 * snd_pcm_hw_param_last - refine config space and return maximum value
1469 * @pcm: PCM instance
1470 * @params: the hw_params instance
1471 * @var: parameter to retrieve
1472 * @dir: pointer to the direction (-1,0,1) or %NULL
1474 * Inside configuration space defined by @params remove from @var all
1475 * values < maximum. Reduce configuration space accordingly.
1476 * Return the maximum.
1478 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1479 struct snd_pcm_hw_params *params,
1480 snd_pcm_hw_param_t var, int *dir)
1482 int changed = _snd_pcm_hw_param_last(params, var);
1485 if (params->rmask) {
1486 int err = snd_pcm_hw_refine(pcm, params);
1487 if (snd_BUG_ON(err < 0))
1490 return snd_pcm_hw_param_value(params, var, dir);
1493 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1496 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1497 * @pcm: PCM instance
1498 * @params: the hw_params instance
1500 * Choose one configuration from configuration space defined by @params.
1501 * The configuration chosen is that obtained fixing in this order:
1502 * first access, first format, first subformat, min channels,
1503 * min rate, min period time, max buffer size, min tick time
1505 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1506 struct snd_pcm_hw_params *params)
1508 static int vars[] = {
1509 SNDRV_PCM_HW_PARAM_ACCESS,
1510 SNDRV_PCM_HW_PARAM_FORMAT,
1511 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1512 SNDRV_PCM_HW_PARAM_CHANNELS,
1513 SNDRV_PCM_HW_PARAM_RATE,
1514 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1515 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1516 SNDRV_PCM_HW_PARAM_TICK_TIME,
1521 for (v = vars; *v != -1; v++) {
1522 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1523 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1525 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1526 if (snd_BUG_ON(err < 0))
1532 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1535 struct snd_pcm_runtime *runtime = substream->runtime;
1536 unsigned long flags;
1537 snd_pcm_stream_lock_irqsave(substream, flags);
1538 if (snd_pcm_running(substream) &&
1539 snd_pcm_update_hw_ptr(substream) >= 0)
1540 runtime->status->hw_ptr %= runtime->buffer_size;
1542 runtime->status->hw_ptr = 0;
1543 snd_pcm_stream_unlock_irqrestore(substream, flags);
1547 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1550 struct snd_pcm_channel_info *info = arg;
1551 struct snd_pcm_runtime *runtime = substream->runtime;
1553 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1557 width = snd_pcm_format_physical_width(runtime->format);
1561 switch (runtime->access) {
1562 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1563 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1564 info->first = info->channel * width;
1565 info->step = runtime->channels * width;
1567 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1568 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1570 size_t size = runtime->dma_bytes / runtime->channels;
1571 info->first = info->channel * size * 8;
1582 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1585 struct snd_pcm_hw_params *params = arg;
1586 snd_pcm_format_t format;
1587 int channels, width;
1589 params->fifo_size = substream->runtime->hw.fifo_size;
1590 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1591 format = params_format(params);
1592 channels = params_channels(params);
1593 width = snd_pcm_format_physical_width(format);
1594 params->fifo_size /= width * channels;
1600 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1601 * @substream: the pcm substream instance
1602 * @cmd: ioctl command
1603 * @arg: ioctl argument
1605 * Processes the generic ioctl commands for PCM.
1606 * Can be passed as the ioctl callback for PCM ops.
1608 * Returns zero if successful, or a negative error code on failure.
1610 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1611 unsigned int cmd, void *arg)
1614 case SNDRV_PCM_IOCTL1_INFO:
1616 case SNDRV_PCM_IOCTL1_RESET:
1617 return snd_pcm_lib_ioctl_reset(substream, arg);
1618 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1619 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1620 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1621 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1626 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1629 * snd_pcm_period_elapsed - update the pcm status for the next period
1630 * @substream: the pcm substream instance
1632 * This function is called from the interrupt handler when the
1633 * PCM has processed the period size. It will update the current
1634 * pointer, wake up sleepers, etc.
1636 * Even if more than one periods have elapsed since the last call, you
1637 * have to call this only once.
1639 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1641 struct snd_pcm_runtime *runtime;
1642 unsigned long flags;
1644 if (PCM_RUNTIME_CHECK(substream))
1646 runtime = substream->runtime;
1648 if (runtime->transfer_ack_begin)
1649 runtime->transfer_ack_begin(substream);
1651 snd_pcm_stream_lock_irqsave(substream, flags);
1652 if (!snd_pcm_running(substream) ||
1653 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1656 if (substream->timer_running)
1657 snd_timer_interrupt(substream->timer, 1);
1659 snd_pcm_stream_unlock_irqrestore(substream, flags);
1660 if (runtime->transfer_ack_end)
1661 runtime->transfer_ack_end(substream);
1662 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1665 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1668 * Wait until avail_min data becomes available
1669 * Returns a negative error code if any error occurs during operation.
1670 * The available space is stored on availp. When err = 0 and avail = 0
1671 * on the capture stream, it indicates the stream is in DRAINING state.
1673 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1674 snd_pcm_uframes_t *availp)
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1680 snd_pcm_uframes_t avail = 0;
1683 init_waitqueue_entry(&wait, current);
1684 add_wait_queue(&runtime->sleep, &wait);
1686 if (signal_pending(current)) {
1690 set_current_state(TASK_INTERRUPTIBLE);
1691 snd_pcm_stream_unlock_irq(substream);
1692 tout = schedule_timeout(msecs_to_jiffies(10000));
1693 snd_pcm_stream_lock_irq(substream);
1694 switch (runtime->status->state) {
1695 case SNDRV_PCM_STATE_SUSPENDED:
1698 case SNDRV_PCM_STATE_XRUN:
1701 case SNDRV_PCM_STATE_DRAINING:
1705 avail = 0; /* indicate draining */
1707 case SNDRV_PCM_STATE_OPEN:
1708 case SNDRV_PCM_STATE_SETUP:
1709 case SNDRV_PCM_STATE_DISCONNECTED:
1714 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1715 is_playback ? "playback" : "capture");
1720 avail = snd_pcm_playback_avail(runtime);
1722 avail = snd_pcm_capture_avail(runtime);
1723 if (avail >= runtime->control->avail_min)
1727 remove_wait_queue(&runtime->sleep, &wait);
1732 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1734 unsigned long data, unsigned int off,
1735 snd_pcm_uframes_t frames)
1737 struct snd_pcm_runtime *runtime = substream->runtime;
1739 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1740 if (substream->ops->copy) {
1741 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1744 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1745 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1751 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1752 unsigned long data, unsigned int off,
1753 snd_pcm_uframes_t size);
1755 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1757 snd_pcm_uframes_t size,
1759 transfer_f transfer)
1761 struct snd_pcm_runtime *runtime = substream->runtime;
1762 snd_pcm_uframes_t xfer = 0;
1763 snd_pcm_uframes_t offset = 0;
1769 snd_pcm_stream_lock_irq(substream);
1770 switch (runtime->status->state) {
1771 case SNDRV_PCM_STATE_PREPARED:
1772 case SNDRV_PCM_STATE_RUNNING:
1773 case SNDRV_PCM_STATE_PAUSED:
1775 case SNDRV_PCM_STATE_XRUN:
1778 case SNDRV_PCM_STATE_SUSPENDED:
1787 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1788 snd_pcm_uframes_t avail;
1789 snd_pcm_uframes_t cont;
1790 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1791 snd_pcm_update_hw_ptr(substream);
1792 avail = snd_pcm_playback_avail(runtime);
1798 err = wait_for_avail_min(substream, &avail);
1802 frames = size > avail ? avail : size;
1803 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1806 if (snd_BUG_ON(!frames)) {
1807 snd_pcm_stream_unlock_irq(substream);
1810 appl_ptr = runtime->control->appl_ptr;
1811 appl_ofs = appl_ptr % runtime->buffer_size;
1812 snd_pcm_stream_unlock_irq(substream);
1813 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1815 snd_pcm_stream_lock_irq(substream);
1816 switch (runtime->status->state) {
1817 case SNDRV_PCM_STATE_XRUN:
1820 case SNDRV_PCM_STATE_SUSPENDED:
1827 if (appl_ptr >= runtime->boundary)
1828 appl_ptr -= runtime->boundary;
1829 runtime->control->appl_ptr = appl_ptr;
1830 if (substream->ops->ack)
1831 substream->ops->ack(substream);
1836 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1837 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1838 err = snd_pcm_start(substream);
1844 snd_pcm_stream_unlock_irq(substream);
1846 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1849 /* sanity-check for read/write methods */
1850 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1852 struct snd_pcm_runtime *runtime;
1853 if (PCM_RUNTIME_CHECK(substream))
1855 runtime = substream->runtime;
1856 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1858 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1863 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1865 struct snd_pcm_runtime *runtime;
1869 err = pcm_sanity_check(substream);
1872 runtime = substream->runtime;
1873 nonblock = !!(substream->f_flags & O_NONBLOCK);
1875 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1876 runtime->channels > 1)
1878 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1879 snd_pcm_lib_write_transfer);
1882 EXPORT_SYMBOL(snd_pcm_lib_write);
1884 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1886 unsigned long data, unsigned int off,
1887 snd_pcm_uframes_t frames)
1889 struct snd_pcm_runtime *runtime = substream->runtime;
1891 void __user **bufs = (void __user **)data;
1892 int channels = runtime->channels;
1894 if (substream->ops->copy) {
1895 if (snd_BUG_ON(!substream->ops->silence))
1897 for (c = 0; c < channels; ++c, ++bufs) {
1898 if (*bufs == NULL) {
1899 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1902 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1903 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1908 /* default transfer behaviour */
1909 size_t dma_csize = runtime->dma_bytes / channels;
1910 for (c = 0; c < channels; ++c, ++bufs) {
1911 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1912 if (*bufs == NULL) {
1913 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1915 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1916 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1924 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1926 snd_pcm_uframes_t frames)
1928 struct snd_pcm_runtime *runtime;
1932 err = pcm_sanity_check(substream);
1935 runtime = substream->runtime;
1936 nonblock = !!(substream->f_flags & O_NONBLOCK);
1938 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1940 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1941 nonblock, snd_pcm_lib_writev_transfer);
1944 EXPORT_SYMBOL(snd_pcm_lib_writev);
1946 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1948 unsigned long data, unsigned int off,
1949 snd_pcm_uframes_t frames)
1951 struct snd_pcm_runtime *runtime = substream->runtime;
1953 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1954 if (substream->ops->copy) {
1955 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1958 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1959 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1965 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1967 snd_pcm_uframes_t size,
1969 transfer_f transfer)
1971 struct snd_pcm_runtime *runtime = substream->runtime;
1972 snd_pcm_uframes_t xfer = 0;
1973 snd_pcm_uframes_t offset = 0;
1979 snd_pcm_stream_lock_irq(substream);
1980 switch (runtime->status->state) {
1981 case SNDRV_PCM_STATE_PREPARED:
1982 if (size >= runtime->start_threshold) {
1983 err = snd_pcm_start(substream);
1988 case SNDRV_PCM_STATE_DRAINING:
1989 case SNDRV_PCM_STATE_RUNNING:
1990 case SNDRV_PCM_STATE_PAUSED:
1992 case SNDRV_PCM_STATE_XRUN:
1995 case SNDRV_PCM_STATE_SUSPENDED:
2004 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2005 snd_pcm_uframes_t avail;
2006 snd_pcm_uframes_t cont;
2007 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2008 snd_pcm_update_hw_ptr(substream);
2009 avail = snd_pcm_capture_avail(runtime);
2011 if (runtime->status->state ==
2012 SNDRV_PCM_STATE_DRAINING) {
2013 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2020 err = wait_for_avail_min(substream, &avail);
2024 continue; /* draining */
2026 frames = size > avail ? avail : size;
2027 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2030 if (snd_BUG_ON(!frames)) {
2031 snd_pcm_stream_unlock_irq(substream);
2034 appl_ptr = runtime->control->appl_ptr;
2035 appl_ofs = appl_ptr % runtime->buffer_size;
2036 snd_pcm_stream_unlock_irq(substream);
2037 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2039 snd_pcm_stream_lock_irq(substream);
2040 switch (runtime->status->state) {
2041 case SNDRV_PCM_STATE_XRUN:
2044 case SNDRV_PCM_STATE_SUSPENDED:
2051 if (appl_ptr >= runtime->boundary)
2052 appl_ptr -= runtime->boundary;
2053 runtime->control->appl_ptr = appl_ptr;
2054 if (substream->ops->ack)
2055 substream->ops->ack(substream);
2062 snd_pcm_stream_unlock_irq(substream);
2064 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2067 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2069 struct snd_pcm_runtime *runtime;
2073 err = pcm_sanity_check(substream);
2076 runtime = substream->runtime;
2077 nonblock = !!(substream->f_flags & O_NONBLOCK);
2078 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2080 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2083 EXPORT_SYMBOL(snd_pcm_lib_read);
2085 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2087 unsigned long data, unsigned int off,
2088 snd_pcm_uframes_t frames)
2090 struct snd_pcm_runtime *runtime = substream->runtime;
2092 void __user **bufs = (void __user **)data;
2093 int channels = runtime->channels;
2095 if (substream->ops->copy) {
2096 for (c = 0; c < channels; ++c, ++bufs) {
2100 buf = *bufs + samples_to_bytes(runtime, off);
2101 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2105 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2106 for (c = 0; c < channels; ++c, ++bufs) {
2112 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2113 buf = *bufs + samples_to_bytes(runtime, off);
2114 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2121 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2123 snd_pcm_uframes_t frames)
2125 struct snd_pcm_runtime *runtime;
2129 err = pcm_sanity_check(substream);
2132 runtime = substream->runtime;
2133 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2136 nonblock = !!(substream->f_flags & O_NONBLOCK);
2137 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2139 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2142 EXPORT_SYMBOL(snd_pcm_lib_readv);