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 delta += runtime->buffer_size;
249 hw_ptr_error(substream,
250 "Unexpected hw_pointer value "
251 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
252 substream->stream, (long)pos,
253 (long)hw_ptr_interrupt);
254 /* rebase to interrupt position */
255 hw_base = new_hw_ptr = hw_ptr_interrupt;
256 /* align hw_base to buffer_size */
257 hw_base -= hw_base % runtime->buffer_size;
260 hw_base += runtime->buffer_size;
261 if (hw_base >= runtime->boundary)
263 new_hw_ptr = hw_base + pos;
267 /* Do jiffies check only in xrun_debug mode */
268 if (!xrun_debug(substream, 4))
269 goto no_jiffies_check;
271 /* Skip the jiffies check for hardwares with BATCH flag.
272 * Such hardware usually just increases the position at each IRQ,
273 * thus it can't give any strange position.
275 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
276 goto no_jiffies_check;
277 hdelta = new_hw_ptr - old_hw_ptr;
278 if (hdelta < runtime->delay)
279 goto no_jiffies_check;
280 hdelta -= runtime->delay;
281 jdelta = jiffies - runtime->hw_ptr_jiffies;
282 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
284 (((runtime->period_size * HZ) / runtime->rate)
286 hw_ptr_error(substream,
287 "hw_ptr skipping! [Q] "
288 "(pos=%ld, delta=%ld, period=%ld, "
289 "jdelta=%lu/%lu/%lu)\n",
290 (long)pos, (long)hdelta,
291 (long)runtime->period_size, jdelta,
292 ((hdelta * HZ) / runtime->rate), delta);
293 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
294 runtime->period_size * delta;
295 if (hw_ptr_interrupt >= runtime->boundary)
296 hw_ptr_interrupt -= runtime->boundary;
297 /* rebase to interrupt position */
298 hw_base = new_hw_ptr = hw_ptr_interrupt;
299 /* align hw_base to buffer_size */
300 hw_base -= hw_base % runtime->buffer_size;
304 if (delta > runtime->period_size + runtime->period_size / 2) {
305 hw_ptr_error(substream,
307 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
308 substream->stream, (long)delta,
309 (long)hw_ptr_interrupt);
310 /* rebase hw_ptr_interrupt */
312 new_hw_ptr - new_hw_ptr % runtime->period_size;
314 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
316 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
317 runtime->silence_size > 0)
318 snd_pcm_playback_silence(substream, new_hw_ptr);
320 if (runtime->status->hw_ptr == new_hw_ptr)
323 runtime->hw_ptr_base = hw_base;
324 runtime->status->hw_ptr = new_hw_ptr;
325 runtime->hw_ptr_jiffies = jiffies;
326 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
327 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
329 return snd_pcm_update_hw_ptr_post(substream, runtime);
332 /* CAUTION: call it with irq disabled */
333 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
335 struct snd_pcm_runtime *runtime = substream->runtime;
336 snd_pcm_uframes_t pos;
337 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
338 snd_pcm_sframes_t delta;
339 unsigned long jdelta;
341 old_hw_ptr = runtime->status->hw_ptr;
342 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
343 if (pos == SNDRV_PCM_POS_XRUN) {
347 hw_base = runtime->hw_ptr_base;
348 new_hw_ptr = hw_base + pos;
350 delta = new_hw_ptr - old_hw_ptr;
351 jdelta = jiffies - runtime->hw_ptr_jiffies;
353 delta += runtime->buffer_size;
355 hw_ptr_error(substream,
356 "Unexpected hw_pointer value [2] "
357 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
358 substream->stream, (long)pos,
359 (long)old_hw_ptr, jdelta);
362 hw_base += runtime->buffer_size;
363 if (hw_base >= runtime->boundary)
365 new_hw_ptr = hw_base + pos;
367 /* Do jiffies check only in xrun_debug mode */
368 if (!xrun_debug(substream, 4))
369 goto no_jiffies_check;
370 if (delta < runtime->delay)
371 goto no_jiffies_check;
372 delta -= runtime->delay;
373 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
374 hw_ptr_error(substream,
376 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
377 (long)pos, (long)delta,
378 (long)runtime->period_size, jdelta,
379 ((delta * HZ) / runtime->rate));
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
384 runtime->silence_size > 0)
385 snd_pcm_playback_silence(substream, new_hw_ptr);
387 if (runtime->status->hw_ptr == new_hw_ptr)
390 runtime->hw_ptr_base = hw_base;
391 runtime->status->hw_ptr = new_hw_ptr;
392 runtime->hw_ptr_jiffies = jiffies;
393 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
394 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
396 return snd_pcm_update_hw_ptr_post(substream, runtime);
400 * snd_pcm_set_ops - set the PCM operators
401 * @pcm: the pcm instance
402 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
403 * @ops: the operator table
405 * Sets the given PCM operators to the pcm instance.
407 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
409 struct snd_pcm_str *stream = &pcm->streams[direction];
410 struct snd_pcm_substream *substream;
412 for (substream = stream->substream; substream != NULL; substream = substream->next)
413 substream->ops = ops;
416 EXPORT_SYMBOL(snd_pcm_set_ops);
419 * snd_pcm_sync - set the PCM sync id
420 * @substream: the pcm substream
422 * Sets the PCM sync identifier for the card.
424 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
426 struct snd_pcm_runtime *runtime = substream->runtime;
428 runtime->sync.id32[0] = substream->pcm->card->number;
429 runtime->sync.id32[1] = -1;
430 runtime->sync.id32[2] = -1;
431 runtime->sync.id32[3] = -1;
434 EXPORT_SYMBOL(snd_pcm_set_sync);
437 * Standard ioctl routine
440 static inline unsigned int div32(unsigned int a, unsigned int b,
451 static inline unsigned int div_down(unsigned int a, unsigned int b)
458 static inline unsigned int div_up(unsigned int a, unsigned int b)
470 static inline unsigned int mul(unsigned int a, unsigned int b)
474 if (div_down(UINT_MAX, a) < b)
479 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
480 unsigned int c, unsigned int *r)
482 u_int64_t n = (u_int64_t) a * b;
488 n = div_u64_rem(n, c, r);
497 * snd_interval_refine - refine the interval value of configurator
498 * @i: the interval value to refine
499 * @v: the interval value to refer to
501 * Refines the interval value with the reference value.
502 * The interval is changed to the range satisfying both intervals.
503 * The interval status (min, max, integer, etc.) are evaluated.
505 * Returns non-zero if the value is changed, zero if not changed.
507 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
510 if (snd_BUG_ON(snd_interval_empty(i)))
512 if (i->min < v->min) {
514 i->openmin = v->openmin;
516 } else if (i->min == v->min && !i->openmin && v->openmin) {
520 if (i->max > v->max) {
522 i->openmax = v->openmax;
524 } else if (i->max == v->max && !i->openmax && v->openmax) {
528 if (!i->integer && v->integer) {
541 } else if (!i->openmin && !i->openmax && i->min == i->max)
543 if (snd_interval_checkempty(i)) {
544 snd_interval_none(i);
550 EXPORT_SYMBOL(snd_interval_refine);
552 static int snd_interval_refine_first(struct snd_interval *i)
554 if (snd_BUG_ON(snd_interval_empty(i)))
556 if (snd_interval_single(i))
559 i->openmax = i->openmin;
565 static int snd_interval_refine_last(struct snd_interval *i)
567 if (snd_BUG_ON(snd_interval_empty(i)))
569 if (snd_interval_single(i))
572 i->openmin = i->openmax;
578 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
580 if (a->empty || b->empty) {
581 snd_interval_none(c);
585 c->min = mul(a->min, b->min);
586 c->openmin = (a->openmin || b->openmin);
587 c->max = mul(a->max, b->max);
588 c->openmax = (a->openmax || b->openmax);
589 c->integer = (a->integer && b->integer);
593 * snd_interval_div - refine the interval value with division
600 * Returns non-zero if the value is changed, zero if not changed.
602 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
605 if (a->empty || b->empty) {
606 snd_interval_none(c);
610 c->min = div32(a->min, b->max, &r);
611 c->openmin = (r || a->openmin || b->openmax);
613 c->max = div32(a->max, b->min, &r);
618 c->openmax = (a->openmax || b->openmin);
627 * snd_interval_muldivk - refine the interval value
630 * @k: divisor (as integer)
635 * Returns non-zero if the value is changed, zero if not changed.
637 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
638 unsigned int k, struct snd_interval *c)
641 if (a->empty || b->empty) {
642 snd_interval_none(c);
646 c->min = muldiv32(a->min, b->min, k, &r);
647 c->openmin = (r || a->openmin || b->openmin);
648 c->max = muldiv32(a->max, b->max, k, &r);
653 c->openmax = (a->openmax || b->openmax);
658 * snd_interval_mulkdiv - refine the interval value
660 * @k: dividend 2 (as integer)
666 * Returns non-zero if the value is changed, zero if not changed.
668 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
669 const struct snd_interval *b, struct snd_interval *c)
672 if (a->empty || b->empty) {
673 snd_interval_none(c);
677 c->min = muldiv32(a->min, k, b->max, &r);
678 c->openmin = (r || a->openmin || b->openmax);
680 c->max = muldiv32(a->max, k, b->min, &r);
685 c->openmax = (a->openmax || b->openmin);
697 * snd_interval_ratnum - refine the interval value
698 * @i: interval to refine
699 * @rats_count: number of ratnum_t
700 * @rats: ratnum_t array
701 * @nump: pointer to store the resultant numerator
702 * @denp: pointer to store the resultant denominator
704 * Returns non-zero if the value is changed, zero if not changed.
706 int snd_interval_ratnum(struct snd_interval *i,
707 unsigned int rats_count, struct snd_ratnum *rats,
708 unsigned int *nump, unsigned int *denp)
710 unsigned int best_num, best_diff, best_den;
712 struct snd_interval t;
715 best_num = best_den = best_diff = 0;
716 for (k = 0; k < rats_count; ++k) {
717 unsigned int num = rats[k].num;
719 unsigned int q = i->min;
723 den = div_down(num, q);
724 if (den < rats[k].den_min)
726 if (den > rats[k].den_max)
727 den = rats[k].den_max;
730 r = (den - rats[k].den_min) % rats[k].den_step;
734 diff = num - q * den;
736 diff * best_den < best_diff * den) {
746 t.min = div_down(best_num, best_den);
747 t.openmin = !!(best_num % best_den);
749 best_num = best_den = best_diff = 0;
750 for (k = 0; k < rats_count; ++k) {
751 unsigned int num = rats[k].num;
753 unsigned int q = i->max;
759 den = div_up(num, q);
760 if (den > rats[k].den_max)
762 if (den < rats[k].den_min)
763 den = rats[k].den_min;
766 r = (den - rats[k].den_min) % rats[k].den_step;
768 den += rats[k].den_step - r;
770 diff = q * den - num;
772 diff * best_den < best_diff * den) {
782 t.max = div_up(best_num, best_den);
783 t.openmax = !!(best_num % best_den);
785 err = snd_interval_refine(i, &t);
789 if (snd_interval_single(i)) {
798 EXPORT_SYMBOL(snd_interval_ratnum);
801 * snd_interval_ratden - refine the interval value
802 * @i: interval to refine
803 * @rats_count: number of struct ratden
804 * @rats: struct ratden array
805 * @nump: pointer to store the resultant numerator
806 * @denp: pointer to store the resultant denominator
808 * Returns non-zero if the value is changed, zero if not changed.
810 static int snd_interval_ratden(struct snd_interval *i,
811 unsigned int rats_count, struct snd_ratden *rats,
812 unsigned int *nump, unsigned int *denp)
814 unsigned int best_num, best_diff, best_den;
816 struct snd_interval t;
819 best_num = best_den = best_diff = 0;
820 for (k = 0; k < rats_count; ++k) {
822 unsigned int den = rats[k].den;
823 unsigned int q = i->min;
826 if (num > rats[k].num_max)
828 if (num < rats[k].num_min)
829 num = rats[k].num_max;
832 r = (num - rats[k].num_min) % rats[k].num_step;
834 num += rats[k].num_step - r;
836 diff = num - q * den;
838 diff * best_den < best_diff * den) {
848 t.min = div_down(best_num, best_den);
849 t.openmin = !!(best_num % best_den);
851 best_num = best_den = best_diff = 0;
852 for (k = 0; k < rats_count; ++k) {
854 unsigned int den = rats[k].den;
855 unsigned int q = i->max;
858 if (num < rats[k].num_min)
860 if (num > rats[k].num_max)
861 num = rats[k].num_max;
864 r = (num - rats[k].num_min) % rats[k].num_step;
868 diff = q * den - num;
870 diff * best_den < best_diff * den) {
880 t.max = div_up(best_num, best_den);
881 t.openmax = !!(best_num % best_den);
883 err = snd_interval_refine(i, &t);
887 if (snd_interval_single(i)) {
897 * snd_interval_list - refine the interval value from the list
898 * @i: the interval value to refine
899 * @count: the number of elements in the list
900 * @list: the value list
901 * @mask: the bit-mask to evaluate
903 * Refines the interval value from the list.
904 * When mask is non-zero, only the elements corresponding to bit 1 are
907 * Returns non-zero if the value is changed, zero if not changed.
909 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
918 for (k = 0; k < count; k++) {
919 if (mask && !(mask & (1 << k)))
921 if (i->min == list[k] && !i->openmin)
923 if (i->min < list[k]) {
933 for (k = count; k-- > 0;) {
934 if (mask && !(mask & (1 << k)))
936 if (i->max == list[k] && !i->openmax)
938 if (i->max > list[k]) {
948 if (snd_interval_checkempty(i)) {
955 EXPORT_SYMBOL(snd_interval_list);
957 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
961 n = (i->min - min) % step;
962 if (n != 0 || i->openmin) {
966 n = (i->max - min) % step;
967 if (n != 0 || i->openmax) {
971 if (snd_interval_checkempty(i)) {
978 /* Info constraints helpers */
981 * snd_pcm_hw_rule_add - add the hw-constraint rule
982 * @runtime: the pcm runtime instance
983 * @cond: condition bits
984 * @var: the variable to evaluate
985 * @func: the evaluation function
986 * @private: the private data pointer passed to function
987 * @dep: the dependent variables
989 * Returns zero if successful, or a negative error code on failure.
991 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
993 snd_pcm_hw_rule_func_t func, void *private,
996 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
997 struct snd_pcm_hw_rule *c;
1000 va_start(args, dep);
1001 if (constrs->rules_num >= constrs->rules_all) {
1002 struct snd_pcm_hw_rule *new;
1003 unsigned int new_rules = constrs->rules_all + 16;
1004 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1007 if (constrs->rules) {
1008 memcpy(new, constrs->rules,
1009 constrs->rules_num * sizeof(*c));
1010 kfree(constrs->rules);
1012 constrs->rules = new;
1013 constrs->rules_all = new_rules;
1015 c = &constrs->rules[constrs->rules_num];
1019 c->private = private;
1022 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1027 dep = va_arg(args, int);
1029 constrs->rules_num++;
1034 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1037 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1038 * @runtime: PCM runtime instance
1039 * @var: hw_params variable to apply the mask
1040 * @mask: the bitmap mask
1042 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1044 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1047 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1048 struct snd_mask *maskp = constrs_mask(constrs, var);
1049 *maskp->bits &= mask;
1050 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1051 if (*maskp->bits == 0)
1057 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1058 * @runtime: PCM runtime instance
1059 * @var: hw_params variable to apply the mask
1060 * @mask: the 64bit bitmap mask
1062 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1064 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1067 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1068 struct snd_mask *maskp = constrs_mask(constrs, var);
1069 maskp->bits[0] &= (u_int32_t)mask;
1070 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1071 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1072 if (! maskp->bits[0] && ! maskp->bits[1])
1078 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1079 * @runtime: PCM runtime instance
1080 * @var: hw_params variable to apply the integer constraint
1082 * Apply the constraint of integer to an interval parameter.
1084 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1086 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1087 return snd_interval_setinteger(constrs_interval(constrs, var));
1090 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1093 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1094 * @runtime: PCM runtime instance
1095 * @var: hw_params variable to apply the range
1096 * @min: the minimal value
1097 * @max: the maximal value
1099 * Apply the min/max range constraint to an interval parameter.
1101 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1102 unsigned int min, unsigned int max)
1104 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1105 struct snd_interval t;
1108 t.openmin = t.openmax = 0;
1110 return snd_interval_refine(constrs_interval(constrs, var), &t);
1113 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1115 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1116 struct snd_pcm_hw_rule *rule)
1118 struct snd_pcm_hw_constraint_list *list = rule->private;
1119 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1124 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1125 * @runtime: PCM runtime instance
1126 * @cond: condition bits
1127 * @var: hw_params variable to apply the list constraint
1130 * Apply the list of constraints to an interval parameter.
1132 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1134 snd_pcm_hw_param_t var,
1135 struct snd_pcm_hw_constraint_list *l)
1137 return snd_pcm_hw_rule_add(runtime, cond, var,
1138 snd_pcm_hw_rule_list, l,
1142 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1144 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1145 struct snd_pcm_hw_rule *rule)
1147 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1148 unsigned int num = 0, den = 0;
1150 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1151 r->nrats, r->rats, &num, &den);
1152 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1153 params->rate_num = num;
1154 params->rate_den = den;
1160 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1161 * @runtime: PCM runtime instance
1162 * @cond: condition bits
1163 * @var: hw_params variable to apply the ratnums constraint
1164 * @r: struct snd_ratnums constriants
1166 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1168 snd_pcm_hw_param_t var,
1169 struct snd_pcm_hw_constraint_ratnums *r)
1171 return snd_pcm_hw_rule_add(runtime, cond, var,
1172 snd_pcm_hw_rule_ratnums, r,
1176 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1178 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1179 struct snd_pcm_hw_rule *rule)
1181 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1182 unsigned int num = 0, den = 0;
1183 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1184 r->nrats, r->rats, &num, &den);
1185 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1186 params->rate_num = num;
1187 params->rate_den = den;
1193 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1194 * @runtime: PCM runtime instance
1195 * @cond: condition bits
1196 * @var: hw_params variable to apply the ratdens constraint
1197 * @r: struct snd_ratdens constriants
1199 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1201 snd_pcm_hw_param_t var,
1202 struct snd_pcm_hw_constraint_ratdens *r)
1204 return snd_pcm_hw_rule_add(runtime, cond, var,
1205 snd_pcm_hw_rule_ratdens, r,
1209 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1211 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1212 struct snd_pcm_hw_rule *rule)
1214 unsigned int l = (unsigned long) rule->private;
1215 int width = l & 0xffff;
1216 unsigned int msbits = l >> 16;
1217 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1218 if (snd_interval_single(i) && snd_interval_value(i) == width)
1219 params->msbits = msbits;
1224 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1225 * @runtime: PCM runtime instance
1226 * @cond: condition bits
1227 * @width: sample bits width
1228 * @msbits: msbits width
1230 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1233 unsigned int msbits)
1235 unsigned long l = (msbits << 16) | width;
1236 return snd_pcm_hw_rule_add(runtime, cond, -1,
1237 snd_pcm_hw_rule_msbits,
1239 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1242 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1244 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1245 struct snd_pcm_hw_rule *rule)
1247 unsigned long step = (unsigned long) rule->private;
1248 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1252 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1253 * @runtime: PCM runtime instance
1254 * @cond: condition bits
1255 * @var: hw_params variable to apply the step constraint
1258 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1260 snd_pcm_hw_param_t var,
1263 return snd_pcm_hw_rule_add(runtime, cond, var,
1264 snd_pcm_hw_rule_step, (void *) step,
1268 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1270 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1272 static unsigned int pow2_sizes[] = {
1273 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1274 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1275 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1276 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1278 return snd_interval_list(hw_param_interval(params, rule->var),
1279 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1283 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1284 * @runtime: PCM runtime instance
1285 * @cond: condition bits
1286 * @var: hw_params variable to apply the power-of-2 constraint
1288 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1290 snd_pcm_hw_param_t var)
1292 return snd_pcm_hw_rule_add(runtime, cond, var,
1293 snd_pcm_hw_rule_pow2, NULL,
1297 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1299 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1300 snd_pcm_hw_param_t var)
1302 if (hw_is_mask(var)) {
1303 snd_mask_any(hw_param_mask(params, var));
1304 params->cmask |= 1 << var;
1305 params->rmask |= 1 << var;
1308 if (hw_is_interval(var)) {
1309 snd_interval_any(hw_param_interval(params, var));
1310 params->cmask |= 1 << var;
1311 params->rmask |= 1 << var;
1317 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1320 memset(params, 0, sizeof(*params));
1321 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1322 _snd_pcm_hw_param_any(params, k);
1323 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1324 _snd_pcm_hw_param_any(params, k);
1328 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1331 * snd_pcm_hw_param_value - return @params field @var value
1332 * @params: the hw_params instance
1333 * @var: parameter to retrieve
1334 * @dir: pointer to the direction (-1,0,1) or %NULL
1336 * Return the value for field @var if it's fixed in configuration space
1337 * defined by @params. Return -%EINVAL otherwise.
1339 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1340 snd_pcm_hw_param_t var, int *dir)
1342 if (hw_is_mask(var)) {
1343 const struct snd_mask *mask = hw_param_mask_c(params, var);
1344 if (!snd_mask_single(mask))
1348 return snd_mask_value(mask);
1350 if (hw_is_interval(var)) {
1351 const struct snd_interval *i = hw_param_interval_c(params, var);
1352 if (!snd_interval_single(i))
1356 return snd_interval_value(i);
1361 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1363 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1364 snd_pcm_hw_param_t var)
1366 if (hw_is_mask(var)) {
1367 snd_mask_none(hw_param_mask(params, var));
1368 params->cmask |= 1 << var;
1369 params->rmask |= 1 << var;
1370 } else if (hw_is_interval(var)) {
1371 snd_interval_none(hw_param_interval(params, var));
1372 params->cmask |= 1 << var;
1373 params->rmask |= 1 << var;
1379 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1381 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1382 snd_pcm_hw_param_t var)
1385 if (hw_is_mask(var))
1386 changed = snd_mask_refine_first(hw_param_mask(params, var));
1387 else if (hw_is_interval(var))
1388 changed = snd_interval_refine_first(hw_param_interval(params, var));
1392 params->cmask |= 1 << var;
1393 params->rmask |= 1 << var;
1400 * snd_pcm_hw_param_first - refine config space and return minimum value
1401 * @pcm: PCM instance
1402 * @params: the hw_params instance
1403 * @var: parameter to retrieve
1404 * @dir: pointer to the direction (-1,0,1) or %NULL
1406 * Inside configuration space defined by @params remove from @var all
1407 * values > minimum. Reduce configuration space accordingly.
1408 * Return the minimum.
1410 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1411 struct snd_pcm_hw_params *params,
1412 snd_pcm_hw_param_t var, int *dir)
1414 int changed = _snd_pcm_hw_param_first(params, var);
1417 if (params->rmask) {
1418 int err = snd_pcm_hw_refine(pcm, params);
1419 if (snd_BUG_ON(err < 0))
1422 return snd_pcm_hw_param_value(params, var, dir);
1425 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1427 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1428 snd_pcm_hw_param_t var)
1431 if (hw_is_mask(var))
1432 changed = snd_mask_refine_last(hw_param_mask(params, var));
1433 else if (hw_is_interval(var))
1434 changed = snd_interval_refine_last(hw_param_interval(params, var));
1438 params->cmask |= 1 << var;
1439 params->rmask |= 1 << var;
1446 * snd_pcm_hw_param_last - refine config space and return maximum value
1447 * @pcm: PCM instance
1448 * @params: the hw_params instance
1449 * @var: parameter to retrieve
1450 * @dir: pointer to the direction (-1,0,1) or %NULL
1452 * Inside configuration space defined by @params remove from @var all
1453 * values < maximum. Reduce configuration space accordingly.
1454 * Return the maximum.
1456 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1457 struct snd_pcm_hw_params *params,
1458 snd_pcm_hw_param_t var, int *dir)
1460 int changed = _snd_pcm_hw_param_last(params, var);
1463 if (params->rmask) {
1464 int err = snd_pcm_hw_refine(pcm, params);
1465 if (snd_BUG_ON(err < 0))
1468 return snd_pcm_hw_param_value(params, var, dir);
1471 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1474 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1475 * @pcm: PCM instance
1476 * @params: the hw_params instance
1478 * Choose one configuration from configuration space defined by @params.
1479 * The configuration chosen is that obtained fixing in this order:
1480 * first access, first format, first subformat, min channels,
1481 * min rate, min period time, max buffer size, min tick time
1483 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1484 struct snd_pcm_hw_params *params)
1486 static int vars[] = {
1487 SNDRV_PCM_HW_PARAM_ACCESS,
1488 SNDRV_PCM_HW_PARAM_FORMAT,
1489 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1490 SNDRV_PCM_HW_PARAM_CHANNELS,
1491 SNDRV_PCM_HW_PARAM_RATE,
1492 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1493 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1494 SNDRV_PCM_HW_PARAM_TICK_TIME,
1499 for (v = vars; *v != -1; v++) {
1500 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1501 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1503 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1504 if (snd_BUG_ON(err < 0))
1510 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1513 struct snd_pcm_runtime *runtime = substream->runtime;
1514 unsigned long flags;
1515 snd_pcm_stream_lock_irqsave(substream, flags);
1516 if (snd_pcm_running(substream) &&
1517 snd_pcm_update_hw_ptr(substream) >= 0)
1518 runtime->status->hw_ptr %= runtime->buffer_size;
1520 runtime->status->hw_ptr = 0;
1521 snd_pcm_stream_unlock_irqrestore(substream, flags);
1525 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1528 struct snd_pcm_channel_info *info = arg;
1529 struct snd_pcm_runtime *runtime = substream->runtime;
1531 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1535 width = snd_pcm_format_physical_width(runtime->format);
1539 switch (runtime->access) {
1540 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1541 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1542 info->first = info->channel * width;
1543 info->step = runtime->channels * width;
1545 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1546 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1548 size_t size = runtime->dma_bytes / runtime->channels;
1549 info->first = info->channel * size * 8;
1560 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1563 struct snd_pcm_hw_params *params = arg;
1564 snd_pcm_format_t format;
1565 int channels, width;
1567 params->fifo_size = substream->runtime->hw.fifo_size;
1568 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1569 format = params_format(params);
1570 channels = params_channels(params);
1571 width = snd_pcm_format_physical_width(format);
1572 params->fifo_size /= width * channels;
1578 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1579 * @substream: the pcm substream instance
1580 * @cmd: ioctl command
1581 * @arg: ioctl argument
1583 * Processes the generic ioctl commands for PCM.
1584 * Can be passed as the ioctl callback for PCM ops.
1586 * Returns zero if successful, or a negative error code on failure.
1588 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1589 unsigned int cmd, void *arg)
1592 case SNDRV_PCM_IOCTL1_INFO:
1594 case SNDRV_PCM_IOCTL1_RESET:
1595 return snd_pcm_lib_ioctl_reset(substream, arg);
1596 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1597 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1598 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1599 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1604 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1607 * snd_pcm_period_elapsed - update the pcm status for the next period
1608 * @substream: the pcm substream instance
1610 * This function is called from the interrupt handler when the
1611 * PCM has processed the period size. It will update the current
1612 * pointer, wake up sleepers, etc.
1614 * Even if more than one periods have elapsed since the last call, you
1615 * have to call this only once.
1617 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1619 struct snd_pcm_runtime *runtime;
1620 unsigned long flags;
1622 if (PCM_RUNTIME_CHECK(substream))
1624 runtime = substream->runtime;
1626 if (runtime->transfer_ack_begin)
1627 runtime->transfer_ack_begin(substream);
1629 snd_pcm_stream_lock_irqsave(substream, flags);
1630 if (!snd_pcm_running(substream) ||
1631 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1634 if (substream->timer_running)
1635 snd_timer_interrupt(substream->timer, 1);
1637 snd_pcm_stream_unlock_irqrestore(substream, flags);
1638 if (runtime->transfer_ack_end)
1639 runtime->transfer_ack_end(substream);
1640 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1643 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1646 * Wait until avail_min data becomes available
1647 * Returns a negative error code if any error occurs during operation.
1648 * The available space is stored on availp. When err = 0 and avail = 0
1649 * on the capture stream, it indicates the stream is in DRAINING state.
1651 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1652 snd_pcm_uframes_t *availp)
1654 struct snd_pcm_runtime *runtime = substream->runtime;
1655 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1658 snd_pcm_uframes_t avail = 0;
1661 init_waitqueue_entry(&wait, current);
1662 add_wait_queue(&runtime->sleep, &wait);
1664 if (signal_pending(current)) {
1668 set_current_state(TASK_INTERRUPTIBLE);
1669 snd_pcm_stream_unlock_irq(substream);
1670 tout = schedule_timeout(msecs_to_jiffies(10000));
1671 snd_pcm_stream_lock_irq(substream);
1672 switch (runtime->status->state) {
1673 case SNDRV_PCM_STATE_SUSPENDED:
1676 case SNDRV_PCM_STATE_XRUN:
1679 case SNDRV_PCM_STATE_DRAINING:
1683 avail = 0; /* indicate draining */
1685 case SNDRV_PCM_STATE_OPEN:
1686 case SNDRV_PCM_STATE_SETUP:
1687 case SNDRV_PCM_STATE_DISCONNECTED:
1692 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1693 is_playback ? "playback" : "capture");
1698 avail = snd_pcm_playback_avail(runtime);
1700 avail = snd_pcm_capture_avail(runtime);
1701 if (avail >= runtime->control->avail_min)
1705 remove_wait_queue(&runtime->sleep, &wait);
1710 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1712 unsigned long data, unsigned int off,
1713 snd_pcm_uframes_t frames)
1715 struct snd_pcm_runtime *runtime = substream->runtime;
1717 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1718 if (substream->ops->copy) {
1719 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1722 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1723 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1729 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1730 unsigned long data, unsigned int off,
1731 snd_pcm_uframes_t size);
1733 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1735 snd_pcm_uframes_t size,
1737 transfer_f transfer)
1739 struct snd_pcm_runtime *runtime = substream->runtime;
1740 snd_pcm_uframes_t xfer = 0;
1741 snd_pcm_uframes_t offset = 0;
1747 snd_pcm_stream_lock_irq(substream);
1748 switch (runtime->status->state) {
1749 case SNDRV_PCM_STATE_PREPARED:
1750 case SNDRV_PCM_STATE_RUNNING:
1751 case SNDRV_PCM_STATE_PAUSED:
1753 case SNDRV_PCM_STATE_XRUN:
1756 case SNDRV_PCM_STATE_SUSPENDED:
1765 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1766 snd_pcm_uframes_t avail;
1767 snd_pcm_uframes_t cont;
1768 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1769 snd_pcm_update_hw_ptr(substream);
1770 avail = snd_pcm_playback_avail(runtime);
1776 err = wait_for_avail_min(substream, &avail);
1780 frames = size > avail ? avail : size;
1781 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1784 if (snd_BUG_ON(!frames)) {
1785 snd_pcm_stream_unlock_irq(substream);
1788 appl_ptr = runtime->control->appl_ptr;
1789 appl_ofs = appl_ptr % runtime->buffer_size;
1790 snd_pcm_stream_unlock_irq(substream);
1791 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1793 snd_pcm_stream_lock_irq(substream);
1794 switch (runtime->status->state) {
1795 case SNDRV_PCM_STATE_XRUN:
1798 case SNDRV_PCM_STATE_SUSPENDED:
1805 if (appl_ptr >= runtime->boundary)
1806 appl_ptr -= runtime->boundary;
1807 runtime->control->appl_ptr = appl_ptr;
1808 if (substream->ops->ack)
1809 substream->ops->ack(substream);
1814 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1815 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1816 err = snd_pcm_start(substream);
1822 snd_pcm_stream_unlock_irq(substream);
1824 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1827 /* sanity-check for read/write methods */
1828 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1830 struct snd_pcm_runtime *runtime;
1831 if (PCM_RUNTIME_CHECK(substream))
1833 runtime = substream->runtime;
1834 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1841 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1843 struct snd_pcm_runtime *runtime;
1847 err = pcm_sanity_check(substream);
1850 runtime = substream->runtime;
1851 nonblock = !!(substream->f_flags & O_NONBLOCK);
1853 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1854 runtime->channels > 1)
1856 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1857 snd_pcm_lib_write_transfer);
1860 EXPORT_SYMBOL(snd_pcm_lib_write);
1862 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1864 unsigned long data, unsigned int off,
1865 snd_pcm_uframes_t frames)
1867 struct snd_pcm_runtime *runtime = substream->runtime;
1869 void __user **bufs = (void __user **)data;
1870 int channels = runtime->channels;
1872 if (substream->ops->copy) {
1873 if (snd_BUG_ON(!substream->ops->silence))
1875 for (c = 0; c < channels; ++c, ++bufs) {
1876 if (*bufs == NULL) {
1877 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1880 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1881 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1886 /* default transfer behaviour */
1887 size_t dma_csize = runtime->dma_bytes / channels;
1888 for (c = 0; c < channels; ++c, ++bufs) {
1889 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1890 if (*bufs == NULL) {
1891 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1893 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1894 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1902 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1904 snd_pcm_uframes_t frames)
1906 struct snd_pcm_runtime *runtime;
1910 err = pcm_sanity_check(substream);
1913 runtime = substream->runtime;
1914 nonblock = !!(substream->f_flags & O_NONBLOCK);
1916 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1918 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1919 nonblock, snd_pcm_lib_writev_transfer);
1922 EXPORT_SYMBOL(snd_pcm_lib_writev);
1924 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1926 unsigned long data, unsigned int off,
1927 snd_pcm_uframes_t frames)
1929 struct snd_pcm_runtime *runtime = substream->runtime;
1931 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1932 if (substream->ops->copy) {
1933 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1936 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1937 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1943 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1945 snd_pcm_uframes_t size,
1947 transfer_f transfer)
1949 struct snd_pcm_runtime *runtime = substream->runtime;
1950 snd_pcm_uframes_t xfer = 0;
1951 snd_pcm_uframes_t offset = 0;
1957 snd_pcm_stream_lock_irq(substream);
1958 switch (runtime->status->state) {
1959 case SNDRV_PCM_STATE_PREPARED:
1960 if (size >= runtime->start_threshold) {
1961 err = snd_pcm_start(substream);
1966 case SNDRV_PCM_STATE_DRAINING:
1967 case SNDRV_PCM_STATE_RUNNING:
1968 case SNDRV_PCM_STATE_PAUSED:
1970 case SNDRV_PCM_STATE_XRUN:
1973 case SNDRV_PCM_STATE_SUSPENDED:
1982 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1983 snd_pcm_uframes_t avail;
1984 snd_pcm_uframes_t cont;
1985 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1986 snd_pcm_update_hw_ptr(substream);
1987 avail = snd_pcm_capture_avail(runtime);
1989 if (runtime->status->state ==
1990 SNDRV_PCM_STATE_DRAINING) {
1991 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1998 err = wait_for_avail_min(substream, &avail);
2002 continue; /* draining */
2004 frames = size > avail ? avail : size;
2005 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2008 if (snd_BUG_ON(!frames)) {
2009 snd_pcm_stream_unlock_irq(substream);
2012 appl_ptr = runtime->control->appl_ptr;
2013 appl_ofs = appl_ptr % runtime->buffer_size;
2014 snd_pcm_stream_unlock_irq(substream);
2015 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2017 snd_pcm_stream_lock_irq(substream);
2018 switch (runtime->status->state) {
2019 case SNDRV_PCM_STATE_XRUN:
2022 case SNDRV_PCM_STATE_SUSPENDED:
2029 if (appl_ptr >= runtime->boundary)
2030 appl_ptr -= runtime->boundary;
2031 runtime->control->appl_ptr = appl_ptr;
2032 if (substream->ops->ack)
2033 substream->ops->ack(substream);
2040 snd_pcm_stream_unlock_irq(substream);
2042 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2045 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2047 struct snd_pcm_runtime *runtime;
2051 err = pcm_sanity_check(substream);
2054 runtime = substream->runtime;
2055 nonblock = !!(substream->f_flags & O_NONBLOCK);
2056 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2058 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2061 EXPORT_SYMBOL(snd_pcm_lib_read);
2063 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2065 unsigned long data, unsigned int off,
2066 snd_pcm_uframes_t frames)
2068 struct snd_pcm_runtime *runtime = substream->runtime;
2070 void __user **bufs = (void __user **)data;
2071 int channels = runtime->channels;
2073 if (substream->ops->copy) {
2074 for (c = 0; c < channels; ++c, ++bufs) {
2078 buf = *bufs + samples_to_bytes(runtime, off);
2079 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2083 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2084 for (c = 0; c < channels; ++c, ++bufs) {
2090 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2091 buf = *bufs + samples_to_bytes(runtime, off);
2092 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2099 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2101 snd_pcm_uframes_t frames)
2103 struct snd_pcm_runtime *runtime;
2107 err = pcm_sanity_check(substream);
2110 runtime = substream->runtime;
2111 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2114 nonblock = !!(substream->f_flags & O_NONBLOCK);
2115 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2117 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2120 EXPORT_SYMBOL(snd_pcm_lib_readv);