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 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
130 #define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
132 #define xrun_debug(substream, mask) 0
135 #define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
140 static void pcm_debug_name(struct snd_pcm_substream *substream,
141 char *name, size_t len)
143 snprintf(name, len, "pcmC%dD%d%c:%d",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p',
150 static void xrun(struct snd_pcm_substream *substream)
152 struct snd_pcm_runtime *runtime = substream->runtime;
154 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
155 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
156 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
157 if (xrun_debug(substream, 1)) {
159 pcm_debug_name(substream, name, sizeof(name));
160 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
161 dump_stack_on_xrun(substream);
165 static snd_pcm_uframes_t
166 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
167 struct snd_pcm_runtime *runtime)
169 snd_pcm_uframes_t pos;
171 pos = substream->ops->pointer(substream);
172 if (pos == SNDRV_PCM_POS_XRUN)
173 return pos; /* XRUN */
174 if (pos >= runtime->buffer_size) {
175 if (printk_ratelimit()) {
177 pcm_debug_name(substream, name, sizeof(name));
178 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
179 "buffer size = 0x%lx, period size = 0x%lx\n",
180 name, pos, runtime->buffer_size,
181 runtime->period_size);
185 pos -= pos % runtime->min_align;
189 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
192 snd_pcm_uframes_t avail;
194 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
195 avail = snd_pcm_playback_avail(runtime);
197 avail = snd_pcm_capture_avail(runtime);
198 if (avail > runtime->avail_max)
199 runtime->avail_max = avail;
200 if (avail >= runtime->stop_threshold) {
201 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
202 snd_pcm_drain_done(substream);
207 if (avail >= runtime->control->avail_min)
208 wake_up(&runtime->sleep);
212 #define hw_ptr_error(substream, fmt, args...) \
214 if (xrun_debug(substream, 1)) { \
215 if (printk_ratelimit()) { \
216 snd_printd("PCM: " fmt, ##args); \
218 dump_stack_on_xrun(substream); \
222 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
224 struct snd_pcm_runtime *runtime = substream->runtime;
225 snd_pcm_uframes_t pos;
226 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
227 snd_pcm_sframes_t hdelta, delta;
228 unsigned long jdelta;
230 old_hw_ptr = runtime->status->hw_ptr;
231 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
232 if (pos == SNDRV_PCM_POS_XRUN) {
236 hw_base = runtime->hw_ptr_base;
237 new_hw_ptr = hw_base + pos;
238 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
239 delta = new_hw_ptr - hw_ptr_interrupt;
240 if (hw_ptr_interrupt >= runtime->boundary) {
241 hw_ptr_interrupt -= runtime->boundary;
242 if (hw_base < runtime->boundary / 2)
243 /* hw_base was already lapped; recalc delta */
244 delta = new_hw_ptr - hw_ptr_interrupt;
247 if (runtime->periods == 1)
248 delta += runtime->buffer_size;
250 hw_ptr_error(substream,
251 "Unexpected hw_pointer value "
252 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
253 substream->stream, (long)pos,
254 (long)hw_ptr_interrupt);
256 /* simply skipping the hwptr update seems more
257 * robust in some cases, e.g. on VMware with
258 * inaccurate timer source
260 return 0; /* skip this update */
262 /* rebase to interrupt position */
263 hw_base = new_hw_ptr = hw_ptr_interrupt;
264 /* align hw_base to buffer_size */
265 hw_base -= hw_base % runtime->buffer_size;
269 hw_base += runtime->buffer_size;
270 if (hw_base >= runtime->boundary)
272 new_hw_ptr = hw_base + pos;
276 /* Do jiffies check only in xrun_debug mode */
277 if (!xrun_debug(substream, 4))
278 goto no_jiffies_check;
280 /* Skip the jiffies check for hardwares with BATCH flag.
281 * Such hardware usually just increases the position at each IRQ,
282 * thus it can't give any strange position.
284 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
285 goto no_jiffies_check;
286 hdelta = new_hw_ptr - old_hw_ptr;
287 if (hdelta < runtime->delay)
288 goto no_jiffies_check;
289 hdelta -= runtime->delay;
290 jdelta = jiffies - runtime->hw_ptr_jiffies;
291 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
293 (((runtime->period_size * HZ) / runtime->rate)
295 hw_ptr_error(substream,
296 "hw_ptr skipping! [Q] "
297 "(pos=%ld, delta=%ld, period=%ld, "
298 "jdelta=%lu/%lu/%lu)\n",
299 (long)pos, (long)hdelta,
300 (long)runtime->period_size, jdelta,
301 ((hdelta * HZ) / runtime->rate), delta);
302 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
303 runtime->period_size * delta;
304 if (hw_ptr_interrupt >= runtime->boundary)
305 hw_ptr_interrupt -= runtime->boundary;
306 /* rebase to interrupt position */
307 hw_base = new_hw_ptr = hw_ptr_interrupt;
308 /* align hw_base to buffer_size */
309 hw_base -= hw_base % runtime->buffer_size;
313 if (delta > runtime->period_size + runtime->period_size / 2) {
314 hw_ptr_error(substream,
316 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
317 substream->stream, (long)delta,
318 (long)hw_ptr_interrupt);
319 /* rebase hw_ptr_interrupt */
321 new_hw_ptr - new_hw_ptr % runtime->period_size;
323 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
326 runtime->silence_size > 0)
327 snd_pcm_playback_silence(substream, new_hw_ptr);
329 if (runtime->status->hw_ptr == new_hw_ptr)
332 runtime->hw_ptr_base = hw_base;
333 runtime->status->hw_ptr = new_hw_ptr;
334 runtime->hw_ptr_jiffies = jiffies;
335 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
336 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
338 return snd_pcm_update_hw_ptr_post(substream, runtime);
341 /* CAUTION: call it with irq disabled */
342 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
344 struct snd_pcm_runtime *runtime = substream->runtime;
345 snd_pcm_uframes_t pos;
346 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
347 snd_pcm_sframes_t delta;
348 unsigned long jdelta;
350 old_hw_ptr = runtime->status->hw_ptr;
351 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
352 if (pos == SNDRV_PCM_POS_XRUN) {
356 hw_base = runtime->hw_ptr_base;
357 new_hw_ptr = hw_base + pos;
359 delta = new_hw_ptr - old_hw_ptr;
360 jdelta = jiffies - runtime->hw_ptr_jiffies;
362 delta += runtime->buffer_size;
364 hw_ptr_error(substream,
365 "Unexpected hw_pointer value [2] "
366 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
367 substream->stream, (long)pos,
368 (long)old_hw_ptr, jdelta);
371 hw_base += runtime->buffer_size;
372 if (hw_base >= runtime->boundary)
374 new_hw_ptr = hw_base + pos;
376 /* Do jiffies check only in xrun_debug mode */
377 if (!xrun_debug(substream, 4))
378 goto no_jiffies_check;
379 if (delta < runtime->delay)
380 goto no_jiffies_check;
381 delta -= runtime->delay;
382 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
383 hw_ptr_error(substream,
385 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
386 (long)pos, (long)delta,
387 (long)runtime->period_size, jdelta,
388 ((delta * HZ) / runtime->rate));
392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
393 runtime->silence_size > 0)
394 snd_pcm_playback_silence(substream, new_hw_ptr);
396 if (runtime->status->hw_ptr == new_hw_ptr)
399 runtime->hw_ptr_base = hw_base;
400 runtime->status->hw_ptr = new_hw_ptr;
401 runtime->hw_ptr_jiffies = jiffies;
402 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
403 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
405 return snd_pcm_update_hw_ptr_post(substream, runtime);
409 * snd_pcm_set_ops - set the PCM operators
410 * @pcm: the pcm instance
411 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
412 * @ops: the operator table
414 * Sets the given PCM operators to the pcm instance.
416 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
418 struct snd_pcm_str *stream = &pcm->streams[direction];
419 struct snd_pcm_substream *substream;
421 for (substream = stream->substream; substream != NULL; substream = substream->next)
422 substream->ops = ops;
425 EXPORT_SYMBOL(snd_pcm_set_ops);
428 * snd_pcm_sync - set the PCM sync id
429 * @substream: the pcm substream
431 * Sets the PCM sync identifier for the card.
433 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
435 struct snd_pcm_runtime *runtime = substream->runtime;
437 runtime->sync.id32[0] = substream->pcm->card->number;
438 runtime->sync.id32[1] = -1;
439 runtime->sync.id32[2] = -1;
440 runtime->sync.id32[3] = -1;
443 EXPORT_SYMBOL(snd_pcm_set_sync);
446 * Standard ioctl routine
449 static inline unsigned int div32(unsigned int a, unsigned int b,
460 static inline unsigned int div_down(unsigned int a, unsigned int b)
467 static inline unsigned int div_up(unsigned int a, unsigned int b)
479 static inline unsigned int mul(unsigned int a, unsigned int b)
483 if (div_down(UINT_MAX, a) < b)
488 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
489 unsigned int c, unsigned int *r)
491 u_int64_t n = (u_int64_t) a * b;
497 n = div_u64_rem(n, c, r);
506 * snd_interval_refine - refine the interval value of configurator
507 * @i: the interval value to refine
508 * @v: the interval value to refer to
510 * Refines the interval value with the reference value.
511 * The interval is changed to the range satisfying both intervals.
512 * The interval status (min, max, integer, etc.) are evaluated.
514 * Returns non-zero if the value is changed, zero if not changed.
516 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
519 if (snd_BUG_ON(snd_interval_empty(i)))
521 if (i->min < v->min) {
523 i->openmin = v->openmin;
525 } else if (i->min == v->min && !i->openmin && v->openmin) {
529 if (i->max > v->max) {
531 i->openmax = v->openmax;
533 } else if (i->max == v->max && !i->openmax && v->openmax) {
537 if (!i->integer && v->integer) {
550 } else if (!i->openmin && !i->openmax && i->min == i->max)
552 if (snd_interval_checkempty(i)) {
553 snd_interval_none(i);
559 EXPORT_SYMBOL(snd_interval_refine);
561 static int snd_interval_refine_first(struct snd_interval *i)
563 if (snd_BUG_ON(snd_interval_empty(i)))
565 if (snd_interval_single(i))
568 i->openmax = i->openmin;
574 static int snd_interval_refine_last(struct snd_interval *i)
576 if (snd_BUG_ON(snd_interval_empty(i)))
578 if (snd_interval_single(i))
581 i->openmin = i->openmax;
587 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
589 if (a->empty || b->empty) {
590 snd_interval_none(c);
594 c->min = mul(a->min, b->min);
595 c->openmin = (a->openmin || b->openmin);
596 c->max = mul(a->max, b->max);
597 c->openmax = (a->openmax || b->openmax);
598 c->integer = (a->integer && b->integer);
602 * snd_interval_div - refine the interval value with division
609 * Returns non-zero if the value is changed, zero if not changed.
611 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
614 if (a->empty || b->empty) {
615 snd_interval_none(c);
619 c->min = div32(a->min, b->max, &r);
620 c->openmin = (r || a->openmin || b->openmax);
622 c->max = div32(a->max, b->min, &r);
627 c->openmax = (a->openmax || b->openmin);
636 * snd_interval_muldivk - refine the interval value
639 * @k: divisor (as integer)
644 * Returns non-zero if the value is changed, zero if not changed.
646 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
647 unsigned int k, struct snd_interval *c)
650 if (a->empty || b->empty) {
651 snd_interval_none(c);
655 c->min = muldiv32(a->min, b->min, k, &r);
656 c->openmin = (r || a->openmin || b->openmin);
657 c->max = muldiv32(a->max, b->max, k, &r);
662 c->openmax = (a->openmax || b->openmax);
667 * snd_interval_mulkdiv - refine the interval value
669 * @k: dividend 2 (as integer)
675 * Returns non-zero if the value is changed, zero if not changed.
677 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
678 const struct snd_interval *b, struct snd_interval *c)
681 if (a->empty || b->empty) {
682 snd_interval_none(c);
686 c->min = muldiv32(a->min, k, b->max, &r);
687 c->openmin = (r || a->openmin || b->openmax);
689 c->max = muldiv32(a->max, k, b->min, &r);
694 c->openmax = (a->openmax || b->openmin);
706 * snd_interval_ratnum - refine the interval value
707 * @i: interval to refine
708 * @rats_count: number of ratnum_t
709 * @rats: ratnum_t array
710 * @nump: pointer to store the resultant numerator
711 * @denp: pointer to store the resultant denominator
713 * Returns non-zero if the value is changed, zero if not changed.
715 int snd_interval_ratnum(struct snd_interval *i,
716 unsigned int rats_count, struct snd_ratnum *rats,
717 unsigned int *nump, unsigned int *denp)
719 unsigned int best_num, best_diff, best_den;
721 struct snd_interval t;
724 best_num = best_den = best_diff = 0;
725 for (k = 0; k < rats_count; ++k) {
726 unsigned int num = rats[k].num;
728 unsigned int q = i->min;
732 den = div_down(num, q);
733 if (den < rats[k].den_min)
735 if (den > rats[k].den_max)
736 den = rats[k].den_max;
739 r = (den - rats[k].den_min) % rats[k].den_step;
743 diff = num - q * den;
745 diff * best_den < best_diff * den) {
755 t.min = div_down(best_num, best_den);
756 t.openmin = !!(best_num % best_den);
758 best_num = best_den = best_diff = 0;
759 for (k = 0; k < rats_count; ++k) {
760 unsigned int num = rats[k].num;
762 unsigned int q = i->max;
768 den = div_up(num, q);
769 if (den > rats[k].den_max)
771 if (den < rats[k].den_min)
772 den = rats[k].den_min;
775 r = (den - rats[k].den_min) % rats[k].den_step;
777 den += rats[k].den_step - r;
779 diff = q * den - num;
781 diff * best_den < best_diff * den) {
791 t.max = div_up(best_num, best_den);
792 t.openmax = !!(best_num % best_den);
794 err = snd_interval_refine(i, &t);
798 if (snd_interval_single(i)) {
807 EXPORT_SYMBOL(snd_interval_ratnum);
810 * snd_interval_ratden - refine the interval value
811 * @i: interval to refine
812 * @rats_count: number of struct ratden
813 * @rats: struct ratden array
814 * @nump: pointer to store the resultant numerator
815 * @denp: pointer to store the resultant denominator
817 * Returns non-zero if the value is changed, zero if not changed.
819 static int snd_interval_ratden(struct snd_interval *i,
820 unsigned int rats_count, struct snd_ratden *rats,
821 unsigned int *nump, unsigned int *denp)
823 unsigned int best_num, best_diff, best_den;
825 struct snd_interval t;
828 best_num = best_den = best_diff = 0;
829 for (k = 0; k < rats_count; ++k) {
831 unsigned int den = rats[k].den;
832 unsigned int q = i->min;
835 if (num > rats[k].num_max)
837 if (num < rats[k].num_min)
838 num = rats[k].num_max;
841 r = (num - rats[k].num_min) % rats[k].num_step;
843 num += rats[k].num_step - r;
845 diff = num - q * den;
847 diff * best_den < best_diff * den) {
857 t.min = div_down(best_num, best_den);
858 t.openmin = !!(best_num % best_den);
860 best_num = best_den = best_diff = 0;
861 for (k = 0; k < rats_count; ++k) {
863 unsigned int den = rats[k].den;
864 unsigned int q = i->max;
867 if (num < rats[k].num_min)
869 if (num > rats[k].num_max)
870 num = rats[k].num_max;
873 r = (num - rats[k].num_min) % rats[k].num_step;
877 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)) {
906 * snd_interval_list - refine the interval value from the list
907 * @i: the interval value to refine
908 * @count: the number of elements in the list
909 * @list: the value list
910 * @mask: the bit-mask to evaluate
912 * Refines the interval value from the list.
913 * When mask is non-zero, only the elements corresponding to bit 1 are
916 * Returns non-zero if the value is changed, zero if not changed.
918 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
927 for (k = 0; k < count; k++) {
928 if (mask && !(mask & (1 << k)))
930 if (i->min == list[k] && !i->openmin)
932 if (i->min < list[k]) {
942 for (k = count; k-- > 0;) {
943 if (mask && !(mask & (1 << k)))
945 if (i->max == list[k] && !i->openmax)
947 if (i->max > list[k]) {
957 if (snd_interval_checkempty(i)) {
964 EXPORT_SYMBOL(snd_interval_list);
966 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
970 n = (i->min - min) % step;
971 if (n != 0 || i->openmin) {
975 n = (i->max - min) % step;
976 if (n != 0 || i->openmax) {
980 if (snd_interval_checkempty(i)) {
987 /* Info constraints helpers */
990 * snd_pcm_hw_rule_add - add the hw-constraint rule
991 * @runtime: the pcm runtime instance
992 * @cond: condition bits
993 * @var: the variable to evaluate
994 * @func: the evaluation function
995 * @private: the private data pointer passed to function
996 * @dep: the dependent variables
998 * Returns zero if successful, or a negative error code on failure.
1000 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1002 snd_pcm_hw_rule_func_t func, void *private,
1005 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1006 struct snd_pcm_hw_rule *c;
1009 va_start(args, dep);
1010 if (constrs->rules_num >= constrs->rules_all) {
1011 struct snd_pcm_hw_rule *new;
1012 unsigned int new_rules = constrs->rules_all + 16;
1013 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1016 if (constrs->rules) {
1017 memcpy(new, constrs->rules,
1018 constrs->rules_num * sizeof(*c));
1019 kfree(constrs->rules);
1021 constrs->rules = new;
1022 constrs->rules_all = new_rules;
1024 c = &constrs->rules[constrs->rules_num];
1028 c->private = private;
1031 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1036 dep = va_arg(args, int);
1038 constrs->rules_num++;
1043 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1046 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1047 * @runtime: PCM runtime instance
1048 * @var: hw_params variable to apply the mask
1049 * @mask: the bitmap mask
1051 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1053 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1056 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1057 struct snd_mask *maskp = constrs_mask(constrs, var);
1058 *maskp->bits &= mask;
1059 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1060 if (*maskp->bits == 0)
1066 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1067 * @runtime: PCM runtime instance
1068 * @var: hw_params variable to apply the mask
1069 * @mask: the 64bit bitmap mask
1071 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1073 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1076 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1077 struct snd_mask *maskp = constrs_mask(constrs, var);
1078 maskp->bits[0] &= (u_int32_t)mask;
1079 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1080 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1081 if (! maskp->bits[0] && ! maskp->bits[1])
1087 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1088 * @runtime: PCM runtime instance
1089 * @var: hw_params variable to apply the integer constraint
1091 * Apply the constraint of integer to an interval parameter.
1093 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1095 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1096 return snd_interval_setinteger(constrs_interval(constrs, var));
1099 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1102 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1103 * @runtime: PCM runtime instance
1104 * @var: hw_params variable to apply the range
1105 * @min: the minimal value
1106 * @max: the maximal value
1108 * Apply the min/max range constraint to an interval parameter.
1110 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1111 unsigned int min, unsigned int max)
1113 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1114 struct snd_interval t;
1117 t.openmin = t.openmax = 0;
1119 return snd_interval_refine(constrs_interval(constrs, var), &t);
1122 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1124 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1125 struct snd_pcm_hw_rule *rule)
1127 struct snd_pcm_hw_constraint_list *list = rule->private;
1128 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1133 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1134 * @runtime: PCM runtime instance
1135 * @cond: condition bits
1136 * @var: hw_params variable to apply the list constraint
1139 * Apply the list of constraints to an interval parameter.
1141 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1143 snd_pcm_hw_param_t var,
1144 struct snd_pcm_hw_constraint_list *l)
1146 return snd_pcm_hw_rule_add(runtime, cond, var,
1147 snd_pcm_hw_rule_list, l,
1151 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1153 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1154 struct snd_pcm_hw_rule *rule)
1156 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1157 unsigned int num = 0, den = 0;
1159 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1160 r->nrats, r->rats, &num, &den);
1161 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1162 params->rate_num = num;
1163 params->rate_den = den;
1169 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1170 * @runtime: PCM runtime instance
1171 * @cond: condition bits
1172 * @var: hw_params variable to apply the ratnums constraint
1173 * @r: struct snd_ratnums constriants
1175 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1177 snd_pcm_hw_param_t var,
1178 struct snd_pcm_hw_constraint_ratnums *r)
1180 return snd_pcm_hw_rule_add(runtime, cond, var,
1181 snd_pcm_hw_rule_ratnums, r,
1185 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1187 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1188 struct snd_pcm_hw_rule *rule)
1190 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1191 unsigned int num = 0, den = 0;
1192 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1193 r->nrats, r->rats, &num, &den);
1194 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1195 params->rate_num = num;
1196 params->rate_den = den;
1202 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1203 * @runtime: PCM runtime instance
1204 * @cond: condition bits
1205 * @var: hw_params variable to apply the ratdens constraint
1206 * @r: struct snd_ratdens constriants
1208 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1210 snd_pcm_hw_param_t var,
1211 struct snd_pcm_hw_constraint_ratdens *r)
1213 return snd_pcm_hw_rule_add(runtime, cond, var,
1214 snd_pcm_hw_rule_ratdens, r,
1218 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1220 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1221 struct snd_pcm_hw_rule *rule)
1223 unsigned int l = (unsigned long) rule->private;
1224 int width = l & 0xffff;
1225 unsigned int msbits = l >> 16;
1226 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1227 if (snd_interval_single(i) && snd_interval_value(i) == width)
1228 params->msbits = msbits;
1233 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1234 * @runtime: PCM runtime instance
1235 * @cond: condition bits
1236 * @width: sample bits width
1237 * @msbits: msbits width
1239 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1242 unsigned int msbits)
1244 unsigned long l = (msbits << 16) | width;
1245 return snd_pcm_hw_rule_add(runtime, cond, -1,
1246 snd_pcm_hw_rule_msbits,
1248 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1251 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1253 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1254 struct snd_pcm_hw_rule *rule)
1256 unsigned long step = (unsigned long) rule->private;
1257 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1261 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1262 * @runtime: PCM runtime instance
1263 * @cond: condition bits
1264 * @var: hw_params variable to apply the step constraint
1267 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1269 snd_pcm_hw_param_t var,
1272 return snd_pcm_hw_rule_add(runtime, cond, var,
1273 snd_pcm_hw_rule_step, (void *) step,
1277 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1279 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1281 static unsigned int pow2_sizes[] = {
1282 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1283 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1284 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1285 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1287 return snd_interval_list(hw_param_interval(params, rule->var),
1288 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1292 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1293 * @runtime: PCM runtime instance
1294 * @cond: condition bits
1295 * @var: hw_params variable to apply the power-of-2 constraint
1297 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1299 snd_pcm_hw_param_t var)
1301 return snd_pcm_hw_rule_add(runtime, cond, var,
1302 snd_pcm_hw_rule_pow2, NULL,
1306 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1308 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1309 snd_pcm_hw_param_t var)
1311 if (hw_is_mask(var)) {
1312 snd_mask_any(hw_param_mask(params, var));
1313 params->cmask |= 1 << var;
1314 params->rmask |= 1 << var;
1317 if (hw_is_interval(var)) {
1318 snd_interval_any(hw_param_interval(params, var));
1319 params->cmask |= 1 << var;
1320 params->rmask |= 1 << var;
1326 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1329 memset(params, 0, sizeof(*params));
1330 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1331 _snd_pcm_hw_param_any(params, k);
1332 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1333 _snd_pcm_hw_param_any(params, k);
1337 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1340 * snd_pcm_hw_param_value - return @params field @var value
1341 * @params: the hw_params instance
1342 * @var: parameter to retrieve
1343 * @dir: pointer to the direction (-1,0,1) or %NULL
1345 * Return the value for field @var if it's fixed in configuration space
1346 * defined by @params. Return -%EINVAL otherwise.
1348 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1349 snd_pcm_hw_param_t var, int *dir)
1351 if (hw_is_mask(var)) {
1352 const struct snd_mask *mask = hw_param_mask_c(params, var);
1353 if (!snd_mask_single(mask))
1357 return snd_mask_value(mask);
1359 if (hw_is_interval(var)) {
1360 const struct snd_interval *i = hw_param_interval_c(params, var);
1361 if (!snd_interval_single(i))
1365 return snd_interval_value(i);
1370 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1372 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1373 snd_pcm_hw_param_t var)
1375 if (hw_is_mask(var)) {
1376 snd_mask_none(hw_param_mask(params, var));
1377 params->cmask |= 1 << var;
1378 params->rmask |= 1 << var;
1379 } else if (hw_is_interval(var)) {
1380 snd_interval_none(hw_param_interval(params, var));
1381 params->cmask |= 1 << var;
1382 params->rmask |= 1 << var;
1388 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1390 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1391 snd_pcm_hw_param_t var)
1394 if (hw_is_mask(var))
1395 changed = snd_mask_refine_first(hw_param_mask(params, var));
1396 else if (hw_is_interval(var))
1397 changed = snd_interval_refine_first(hw_param_interval(params, var));
1401 params->cmask |= 1 << var;
1402 params->rmask |= 1 << var;
1409 * snd_pcm_hw_param_first - refine config space and return minimum value
1410 * @pcm: PCM instance
1411 * @params: the hw_params instance
1412 * @var: parameter to retrieve
1413 * @dir: pointer to the direction (-1,0,1) or %NULL
1415 * Inside configuration space defined by @params remove from @var all
1416 * values > minimum. Reduce configuration space accordingly.
1417 * Return the minimum.
1419 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1420 struct snd_pcm_hw_params *params,
1421 snd_pcm_hw_param_t var, int *dir)
1423 int changed = _snd_pcm_hw_param_first(params, var);
1426 if (params->rmask) {
1427 int err = snd_pcm_hw_refine(pcm, params);
1428 if (snd_BUG_ON(err < 0))
1431 return snd_pcm_hw_param_value(params, var, dir);
1434 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1436 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1437 snd_pcm_hw_param_t var)
1440 if (hw_is_mask(var))
1441 changed = snd_mask_refine_last(hw_param_mask(params, var));
1442 else if (hw_is_interval(var))
1443 changed = snd_interval_refine_last(hw_param_interval(params, var));
1447 params->cmask |= 1 << var;
1448 params->rmask |= 1 << var;
1455 * snd_pcm_hw_param_last - refine config space and return maximum value
1456 * @pcm: PCM instance
1457 * @params: the hw_params instance
1458 * @var: parameter to retrieve
1459 * @dir: pointer to the direction (-1,0,1) or %NULL
1461 * Inside configuration space defined by @params remove from @var all
1462 * values < maximum. Reduce configuration space accordingly.
1463 * Return the maximum.
1465 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1466 struct snd_pcm_hw_params *params,
1467 snd_pcm_hw_param_t var, int *dir)
1469 int changed = _snd_pcm_hw_param_last(params, var);
1472 if (params->rmask) {
1473 int err = snd_pcm_hw_refine(pcm, params);
1474 if (snd_BUG_ON(err < 0))
1477 return snd_pcm_hw_param_value(params, var, dir);
1480 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1483 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1484 * @pcm: PCM instance
1485 * @params: the hw_params instance
1487 * Choose one configuration from configuration space defined by @params.
1488 * The configuration chosen is that obtained fixing in this order:
1489 * first access, first format, first subformat, min channels,
1490 * min rate, min period time, max buffer size, min tick time
1492 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1493 struct snd_pcm_hw_params *params)
1495 static int vars[] = {
1496 SNDRV_PCM_HW_PARAM_ACCESS,
1497 SNDRV_PCM_HW_PARAM_FORMAT,
1498 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1499 SNDRV_PCM_HW_PARAM_CHANNELS,
1500 SNDRV_PCM_HW_PARAM_RATE,
1501 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1502 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1503 SNDRV_PCM_HW_PARAM_TICK_TIME,
1508 for (v = vars; *v != -1; v++) {
1509 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1510 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1512 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1513 if (snd_BUG_ON(err < 0))
1519 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1522 struct snd_pcm_runtime *runtime = substream->runtime;
1523 unsigned long flags;
1524 snd_pcm_stream_lock_irqsave(substream, flags);
1525 if (snd_pcm_running(substream) &&
1526 snd_pcm_update_hw_ptr(substream) >= 0)
1527 runtime->status->hw_ptr %= runtime->buffer_size;
1529 runtime->status->hw_ptr = 0;
1530 snd_pcm_stream_unlock_irqrestore(substream, flags);
1534 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1537 struct snd_pcm_channel_info *info = arg;
1538 struct snd_pcm_runtime *runtime = substream->runtime;
1540 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1544 width = snd_pcm_format_physical_width(runtime->format);
1548 switch (runtime->access) {
1549 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1550 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1551 info->first = info->channel * width;
1552 info->step = runtime->channels * width;
1554 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1555 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1557 size_t size = runtime->dma_bytes / runtime->channels;
1558 info->first = info->channel * size * 8;
1569 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1572 struct snd_pcm_hw_params *params = arg;
1573 snd_pcm_format_t format;
1574 int channels, width;
1576 params->fifo_size = substream->runtime->hw.fifo_size;
1577 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1578 format = params_format(params);
1579 channels = params_channels(params);
1580 width = snd_pcm_format_physical_width(format);
1581 params->fifo_size /= width * channels;
1587 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1588 * @substream: the pcm substream instance
1589 * @cmd: ioctl command
1590 * @arg: ioctl argument
1592 * Processes the generic ioctl commands for PCM.
1593 * Can be passed as the ioctl callback for PCM ops.
1595 * Returns zero if successful, or a negative error code on failure.
1597 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1598 unsigned int cmd, void *arg)
1601 case SNDRV_PCM_IOCTL1_INFO:
1603 case SNDRV_PCM_IOCTL1_RESET:
1604 return snd_pcm_lib_ioctl_reset(substream, arg);
1605 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1606 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1607 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1608 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1613 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1616 * snd_pcm_period_elapsed - update the pcm status for the next period
1617 * @substream: the pcm substream instance
1619 * This function is called from the interrupt handler when the
1620 * PCM has processed the period size. It will update the current
1621 * pointer, wake up sleepers, etc.
1623 * Even if more than one periods have elapsed since the last call, you
1624 * have to call this only once.
1626 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1628 struct snd_pcm_runtime *runtime;
1629 unsigned long flags;
1631 if (PCM_RUNTIME_CHECK(substream))
1633 runtime = substream->runtime;
1635 if (runtime->transfer_ack_begin)
1636 runtime->transfer_ack_begin(substream);
1638 snd_pcm_stream_lock_irqsave(substream, flags);
1639 if (!snd_pcm_running(substream) ||
1640 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1643 if (substream->timer_running)
1644 snd_timer_interrupt(substream->timer, 1);
1646 snd_pcm_stream_unlock_irqrestore(substream, flags);
1647 if (runtime->transfer_ack_end)
1648 runtime->transfer_ack_end(substream);
1649 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1652 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1655 * Wait until avail_min data becomes available
1656 * Returns a negative error code if any error occurs during operation.
1657 * The available space is stored on availp. When err = 0 and avail = 0
1658 * on the capture stream, it indicates the stream is in DRAINING state.
1660 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1661 snd_pcm_uframes_t *availp)
1663 struct snd_pcm_runtime *runtime = substream->runtime;
1664 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1667 snd_pcm_uframes_t avail = 0;
1670 init_waitqueue_entry(&wait, current);
1671 add_wait_queue(&runtime->sleep, &wait);
1673 if (signal_pending(current)) {
1677 set_current_state(TASK_INTERRUPTIBLE);
1678 snd_pcm_stream_unlock_irq(substream);
1679 tout = schedule_timeout(msecs_to_jiffies(10000));
1680 snd_pcm_stream_lock_irq(substream);
1681 switch (runtime->status->state) {
1682 case SNDRV_PCM_STATE_SUSPENDED:
1685 case SNDRV_PCM_STATE_XRUN:
1688 case SNDRV_PCM_STATE_DRAINING:
1692 avail = 0; /* indicate draining */
1694 case SNDRV_PCM_STATE_OPEN:
1695 case SNDRV_PCM_STATE_SETUP:
1696 case SNDRV_PCM_STATE_DISCONNECTED:
1701 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1702 is_playback ? "playback" : "capture");
1707 avail = snd_pcm_playback_avail(runtime);
1709 avail = snd_pcm_capture_avail(runtime);
1710 if (avail >= runtime->control->avail_min)
1714 remove_wait_queue(&runtime->sleep, &wait);
1719 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1721 unsigned long data, unsigned int off,
1722 snd_pcm_uframes_t frames)
1724 struct snd_pcm_runtime *runtime = substream->runtime;
1726 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1727 if (substream->ops->copy) {
1728 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1731 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1732 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1738 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1739 unsigned long data, unsigned int off,
1740 snd_pcm_uframes_t size);
1742 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1744 snd_pcm_uframes_t size,
1746 transfer_f transfer)
1748 struct snd_pcm_runtime *runtime = substream->runtime;
1749 snd_pcm_uframes_t xfer = 0;
1750 snd_pcm_uframes_t offset = 0;
1756 snd_pcm_stream_lock_irq(substream);
1757 switch (runtime->status->state) {
1758 case SNDRV_PCM_STATE_PREPARED:
1759 case SNDRV_PCM_STATE_RUNNING:
1760 case SNDRV_PCM_STATE_PAUSED:
1762 case SNDRV_PCM_STATE_XRUN:
1765 case SNDRV_PCM_STATE_SUSPENDED:
1774 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1775 snd_pcm_uframes_t avail;
1776 snd_pcm_uframes_t cont;
1777 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1778 snd_pcm_update_hw_ptr(substream);
1779 avail = snd_pcm_playback_avail(runtime);
1785 err = wait_for_avail_min(substream, &avail);
1789 frames = size > avail ? avail : size;
1790 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1793 if (snd_BUG_ON(!frames)) {
1794 snd_pcm_stream_unlock_irq(substream);
1797 appl_ptr = runtime->control->appl_ptr;
1798 appl_ofs = appl_ptr % runtime->buffer_size;
1799 snd_pcm_stream_unlock_irq(substream);
1800 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1802 snd_pcm_stream_lock_irq(substream);
1803 switch (runtime->status->state) {
1804 case SNDRV_PCM_STATE_XRUN:
1807 case SNDRV_PCM_STATE_SUSPENDED:
1814 if (appl_ptr >= runtime->boundary)
1815 appl_ptr -= runtime->boundary;
1816 runtime->control->appl_ptr = appl_ptr;
1817 if (substream->ops->ack)
1818 substream->ops->ack(substream);
1823 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1824 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1825 err = snd_pcm_start(substream);
1831 snd_pcm_stream_unlock_irq(substream);
1833 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1836 /* sanity-check for read/write methods */
1837 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1839 struct snd_pcm_runtime *runtime;
1840 if (PCM_RUNTIME_CHECK(substream))
1842 runtime = substream->runtime;
1843 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1845 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1850 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1852 struct snd_pcm_runtime *runtime;
1856 err = pcm_sanity_check(substream);
1859 runtime = substream->runtime;
1860 nonblock = !!(substream->f_flags & O_NONBLOCK);
1862 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1863 runtime->channels > 1)
1865 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1866 snd_pcm_lib_write_transfer);
1869 EXPORT_SYMBOL(snd_pcm_lib_write);
1871 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1873 unsigned long data, unsigned int off,
1874 snd_pcm_uframes_t frames)
1876 struct snd_pcm_runtime *runtime = substream->runtime;
1878 void __user **bufs = (void __user **)data;
1879 int channels = runtime->channels;
1881 if (substream->ops->copy) {
1882 if (snd_BUG_ON(!substream->ops->silence))
1884 for (c = 0; c < channels; ++c, ++bufs) {
1885 if (*bufs == NULL) {
1886 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1889 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1890 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1895 /* default transfer behaviour */
1896 size_t dma_csize = runtime->dma_bytes / channels;
1897 for (c = 0; c < channels; ++c, ++bufs) {
1898 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1899 if (*bufs == NULL) {
1900 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1902 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1903 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1911 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1913 snd_pcm_uframes_t frames)
1915 struct snd_pcm_runtime *runtime;
1919 err = pcm_sanity_check(substream);
1922 runtime = substream->runtime;
1923 nonblock = !!(substream->f_flags & O_NONBLOCK);
1925 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1927 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1928 nonblock, snd_pcm_lib_writev_transfer);
1931 EXPORT_SYMBOL(snd_pcm_lib_writev);
1933 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1935 unsigned long data, unsigned int off,
1936 snd_pcm_uframes_t frames)
1938 struct snd_pcm_runtime *runtime = substream->runtime;
1940 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1941 if (substream->ops->copy) {
1942 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1945 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1946 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1952 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1954 snd_pcm_uframes_t size,
1956 transfer_f transfer)
1958 struct snd_pcm_runtime *runtime = substream->runtime;
1959 snd_pcm_uframes_t xfer = 0;
1960 snd_pcm_uframes_t offset = 0;
1966 snd_pcm_stream_lock_irq(substream);
1967 switch (runtime->status->state) {
1968 case SNDRV_PCM_STATE_PREPARED:
1969 if (size >= runtime->start_threshold) {
1970 err = snd_pcm_start(substream);
1975 case SNDRV_PCM_STATE_DRAINING:
1976 case SNDRV_PCM_STATE_RUNNING:
1977 case SNDRV_PCM_STATE_PAUSED:
1979 case SNDRV_PCM_STATE_XRUN:
1982 case SNDRV_PCM_STATE_SUSPENDED:
1991 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1992 snd_pcm_uframes_t avail;
1993 snd_pcm_uframes_t cont;
1994 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1995 snd_pcm_update_hw_ptr(substream);
1996 avail = snd_pcm_capture_avail(runtime);
1998 if (runtime->status->state ==
1999 SNDRV_PCM_STATE_DRAINING) {
2000 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2007 err = wait_for_avail_min(substream, &avail);
2011 continue; /* draining */
2013 frames = size > avail ? avail : size;
2014 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2017 if (snd_BUG_ON(!frames)) {
2018 snd_pcm_stream_unlock_irq(substream);
2021 appl_ptr = runtime->control->appl_ptr;
2022 appl_ofs = appl_ptr % runtime->buffer_size;
2023 snd_pcm_stream_unlock_irq(substream);
2024 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2026 snd_pcm_stream_lock_irq(substream);
2027 switch (runtime->status->state) {
2028 case SNDRV_PCM_STATE_XRUN:
2031 case SNDRV_PCM_STATE_SUSPENDED:
2038 if (appl_ptr >= runtime->boundary)
2039 appl_ptr -= runtime->boundary;
2040 runtime->control->appl_ptr = appl_ptr;
2041 if (substream->ops->ack)
2042 substream->ops->ack(substream);
2049 snd_pcm_stream_unlock_irq(substream);
2051 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2054 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2056 struct snd_pcm_runtime *runtime;
2060 err = pcm_sanity_check(substream);
2063 runtime = substream->runtime;
2064 nonblock = !!(substream->f_flags & O_NONBLOCK);
2065 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2067 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2070 EXPORT_SYMBOL(snd_pcm_lib_read);
2072 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2074 unsigned long data, unsigned int off,
2075 snd_pcm_uframes_t frames)
2077 struct snd_pcm_runtime *runtime = substream->runtime;
2079 void __user **bufs = (void __user **)data;
2080 int channels = runtime->channels;
2082 if (substream->ops->copy) {
2083 for (c = 0; c < channels; ++c, ++bufs) {
2087 buf = *bufs + samples_to_bytes(runtime, off);
2088 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2092 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2093 for (c = 0; c < channels; ++c, ++bufs) {
2099 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2100 buf = *bufs + samples_to_bytes(runtime, off);
2101 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2108 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2110 snd_pcm_uframes_t frames)
2112 struct snd_pcm_runtime *runtime;
2116 err = pcm_sanity_check(substream);
2119 runtime = substream->runtime;
2120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2123 nonblock = !!(substream->f_flags & O_NONBLOCK);
2124 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2126 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2129 EXPORT_SYMBOL(snd_pcm_lib_readv);