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)
912 struct snd_interval list_range;
918 snd_interval_any(&list_range);
919 list_range.min = UINT_MAX;
921 for (k = 0; k < count; k++) {
922 if (mask && !(mask & (1 << k)))
924 if (!snd_interval_test(i, list[k]))
926 list_range.min = min(list_range.min, list[k]);
927 list_range.max = max(list_range.max, list[k]);
929 return snd_interval_refine(i, &list_range);
932 EXPORT_SYMBOL(snd_interval_list);
934 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
938 n = (i->min - min) % step;
939 if (n != 0 || i->openmin) {
943 n = (i->max - min) % step;
944 if (n != 0 || i->openmax) {
948 if (snd_interval_checkempty(i)) {
955 /* Info constraints helpers */
958 * snd_pcm_hw_rule_add - add the hw-constraint rule
959 * @runtime: the pcm runtime instance
960 * @cond: condition bits
961 * @var: the variable to evaluate
962 * @func: the evaluation function
963 * @private: the private data pointer passed to function
964 * @dep: the dependent variables
966 * Returns zero if successful, or a negative error code on failure.
968 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
970 snd_pcm_hw_rule_func_t func, void *private,
973 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
974 struct snd_pcm_hw_rule *c;
978 if (constrs->rules_num >= constrs->rules_all) {
979 struct snd_pcm_hw_rule *new;
980 unsigned int new_rules = constrs->rules_all + 16;
981 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
984 if (constrs->rules) {
985 memcpy(new, constrs->rules,
986 constrs->rules_num * sizeof(*c));
987 kfree(constrs->rules);
989 constrs->rules = new;
990 constrs->rules_all = new_rules;
992 c = &constrs->rules[constrs->rules_num];
996 c->private = private;
999 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1004 dep = va_arg(args, int);
1006 constrs->rules_num++;
1011 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1014 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1015 * @runtime: PCM runtime instance
1016 * @var: hw_params variable to apply the mask
1017 * @mask: the bitmap mask
1019 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1021 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1024 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1025 struct snd_mask *maskp = constrs_mask(constrs, var);
1026 *maskp->bits &= mask;
1027 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1028 if (*maskp->bits == 0)
1034 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1035 * @runtime: PCM runtime instance
1036 * @var: hw_params variable to apply the mask
1037 * @mask: the 64bit bitmap mask
1039 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1041 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1044 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1045 struct snd_mask *maskp = constrs_mask(constrs, var);
1046 maskp->bits[0] &= (u_int32_t)mask;
1047 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1048 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1049 if (! maskp->bits[0] && ! maskp->bits[1])
1055 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1056 * @runtime: PCM runtime instance
1057 * @var: hw_params variable to apply the integer constraint
1059 * Apply the constraint of integer to an interval parameter.
1061 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1063 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1064 return snd_interval_setinteger(constrs_interval(constrs, var));
1067 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1070 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1071 * @runtime: PCM runtime instance
1072 * @var: hw_params variable to apply the range
1073 * @min: the minimal value
1074 * @max: the maximal value
1076 * Apply the min/max range constraint to an interval parameter.
1078 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1079 unsigned int min, unsigned int max)
1081 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1082 struct snd_interval t;
1085 t.openmin = t.openmax = 0;
1087 return snd_interval_refine(constrs_interval(constrs, var), &t);
1090 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1092 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1093 struct snd_pcm_hw_rule *rule)
1095 struct snd_pcm_hw_constraint_list *list = rule->private;
1096 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1101 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1102 * @runtime: PCM runtime instance
1103 * @cond: condition bits
1104 * @var: hw_params variable to apply the list constraint
1107 * Apply the list of constraints to an interval parameter.
1109 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1111 snd_pcm_hw_param_t var,
1112 struct snd_pcm_hw_constraint_list *l)
1114 return snd_pcm_hw_rule_add(runtime, cond, var,
1115 snd_pcm_hw_rule_list, l,
1119 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1121 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1122 struct snd_pcm_hw_rule *rule)
1124 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1125 unsigned int num = 0, den = 0;
1127 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1128 r->nrats, r->rats, &num, &den);
1129 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1130 params->rate_num = num;
1131 params->rate_den = den;
1137 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1138 * @runtime: PCM runtime instance
1139 * @cond: condition bits
1140 * @var: hw_params variable to apply the ratnums constraint
1141 * @r: struct snd_ratnums constriants
1143 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1145 snd_pcm_hw_param_t var,
1146 struct snd_pcm_hw_constraint_ratnums *r)
1148 return snd_pcm_hw_rule_add(runtime, cond, var,
1149 snd_pcm_hw_rule_ratnums, r,
1153 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1155 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1156 struct snd_pcm_hw_rule *rule)
1158 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1159 unsigned int num = 0, den = 0;
1160 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1161 r->nrats, r->rats, &num, &den);
1162 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1163 params->rate_num = num;
1164 params->rate_den = den;
1170 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1171 * @runtime: PCM runtime instance
1172 * @cond: condition bits
1173 * @var: hw_params variable to apply the ratdens constraint
1174 * @r: struct snd_ratdens constriants
1176 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1178 snd_pcm_hw_param_t var,
1179 struct snd_pcm_hw_constraint_ratdens *r)
1181 return snd_pcm_hw_rule_add(runtime, cond, var,
1182 snd_pcm_hw_rule_ratdens, r,
1186 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1188 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1189 struct snd_pcm_hw_rule *rule)
1191 unsigned int l = (unsigned long) rule->private;
1192 int width = l & 0xffff;
1193 unsigned int msbits = l >> 16;
1194 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1195 if (snd_interval_single(i) && snd_interval_value(i) == width)
1196 params->msbits = msbits;
1201 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1202 * @runtime: PCM runtime instance
1203 * @cond: condition bits
1204 * @width: sample bits width
1205 * @msbits: msbits width
1207 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1210 unsigned int msbits)
1212 unsigned long l = (msbits << 16) | width;
1213 return snd_pcm_hw_rule_add(runtime, cond, -1,
1214 snd_pcm_hw_rule_msbits,
1216 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1219 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1221 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1222 struct snd_pcm_hw_rule *rule)
1224 unsigned long step = (unsigned long) rule->private;
1225 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1229 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1230 * @runtime: PCM runtime instance
1231 * @cond: condition bits
1232 * @var: hw_params variable to apply the step constraint
1235 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1237 snd_pcm_hw_param_t var,
1240 return snd_pcm_hw_rule_add(runtime, cond, var,
1241 snd_pcm_hw_rule_step, (void *) step,
1245 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1247 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1249 static unsigned int pow2_sizes[] = {
1250 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1251 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1252 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1253 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1255 return snd_interval_list(hw_param_interval(params, rule->var),
1256 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1260 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1261 * @runtime: PCM runtime instance
1262 * @cond: condition bits
1263 * @var: hw_params variable to apply the power-of-2 constraint
1265 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1267 snd_pcm_hw_param_t var)
1269 return snd_pcm_hw_rule_add(runtime, cond, var,
1270 snd_pcm_hw_rule_pow2, NULL,
1274 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1276 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1277 snd_pcm_hw_param_t var)
1279 if (hw_is_mask(var)) {
1280 snd_mask_any(hw_param_mask(params, var));
1281 params->cmask |= 1 << var;
1282 params->rmask |= 1 << var;
1285 if (hw_is_interval(var)) {
1286 snd_interval_any(hw_param_interval(params, var));
1287 params->cmask |= 1 << var;
1288 params->rmask |= 1 << var;
1294 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1297 memset(params, 0, sizeof(*params));
1298 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1299 _snd_pcm_hw_param_any(params, k);
1300 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1301 _snd_pcm_hw_param_any(params, k);
1305 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1308 * snd_pcm_hw_param_value - return @params field @var value
1309 * @params: the hw_params instance
1310 * @var: parameter to retrieve
1311 * @dir: pointer to the direction (-1,0,1) or %NULL
1313 * Return the value for field @var if it's fixed in configuration space
1314 * defined by @params. Return -%EINVAL otherwise.
1316 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1317 snd_pcm_hw_param_t var, int *dir)
1319 if (hw_is_mask(var)) {
1320 const struct snd_mask *mask = hw_param_mask_c(params, var);
1321 if (!snd_mask_single(mask))
1325 return snd_mask_value(mask);
1327 if (hw_is_interval(var)) {
1328 const struct snd_interval *i = hw_param_interval_c(params, var);
1329 if (!snd_interval_single(i))
1333 return snd_interval_value(i);
1338 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1340 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1341 snd_pcm_hw_param_t var)
1343 if (hw_is_mask(var)) {
1344 snd_mask_none(hw_param_mask(params, var));
1345 params->cmask |= 1 << var;
1346 params->rmask |= 1 << var;
1347 } else if (hw_is_interval(var)) {
1348 snd_interval_none(hw_param_interval(params, var));
1349 params->cmask |= 1 << var;
1350 params->rmask |= 1 << var;
1356 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1358 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1359 snd_pcm_hw_param_t var)
1362 if (hw_is_mask(var))
1363 changed = snd_mask_refine_first(hw_param_mask(params, var));
1364 else if (hw_is_interval(var))
1365 changed = snd_interval_refine_first(hw_param_interval(params, var));
1369 params->cmask |= 1 << var;
1370 params->rmask |= 1 << var;
1377 * snd_pcm_hw_param_first - refine config space and return minimum value
1378 * @pcm: PCM instance
1379 * @params: the hw_params instance
1380 * @var: parameter to retrieve
1381 * @dir: pointer to the direction (-1,0,1) or %NULL
1383 * Inside configuration space defined by @params remove from @var all
1384 * values > minimum. Reduce configuration space accordingly.
1385 * Return the minimum.
1387 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1388 struct snd_pcm_hw_params *params,
1389 snd_pcm_hw_param_t var, int *dir)
1391 int changed = _snd_pcm_hw_param_first(params, var);
1394 if (params->rmask) {
1395 int err = snd_pcm_hw_refine(pcm, params);
1396 if (snd_BUG_ON(err < 0))
1399 return snd_pcm_hw_param_value(params, var, dir);
1402 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1404 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1405 snd_pcm_hw_param_t var)
1408 if (hw_is_mask(var))
1409 changed = snd_mask_refine_last(hw_param_mask(params, var));
1410 else if (hw_is_interval(var))
1411 changed = snd_interval_refine_last(hw_param_interval(params, var));
1415 params->cmask |= 1 << var;
1416 params->rmask |= 1 << var;
1423 * snd_pcm_hw_param_last - refine config space and return maximum value
1424 * @pcm: PCM instance
1425 * @params: the hw_params instance
1426 * @var: parameter to retrieve
1427 * @dir: pointer to the direction (-1,0,1) or %NULL
1429 * Inside configuration space defined by @params remove from @var all
1430 * values < maximum. Reduce configuration space accordingly.
1431 * Return the maximum.
1433 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1434 struct snd_pcm_hw_params *params,
1435 snd_pcm_hw_param_t var, int *dir)
1437 int changed = _snd_pcm_hw_param_last(params, var);
1440 if (params->rmask) {
1441 int err = snd_pcm_hw_refine(pcm, params);
1442 if (snd_BUG_ON(err < 0))
1445 return snd_pcm_hw_param_value(params, var, dir);
1448 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1451 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1452 * @pcm: PCM instance
1453 * @params: the hw_params instance
1455 * Choose one configuration from configuration space defined by @params.
1456 * The configuration chosen is that obtained fixing in this order:
1457 * first access, first format, first subformat, min channels,
1458 * min rate, min period time, max buffer size, min tick time
1460 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1461 struct snd_pcm_hw_params *params)
1463 static int vars[] = {
1464 SNDRV_PCM_HW_PARAM_ACCESS,
1465 SNDRV_PCM_HW_PARAM_FORMAT,
1466 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1467 SNDRV_PCM_HW_PARAM_CHANNELS,
1468 SNDRV_PCM_HW_PARAM_RATE,
1469 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1470 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1471 SNDRV_PCM_HW_PARAM_TICK_TIME,
1476 for (v = vars; *v != -1; v++) {
1477 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1478 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1480 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1481 if (snd_BUG_ON(err < 0))
1487 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 unsigned long flags;
1492 snd_pcm_stream_lock_irqsave(substream, flags);
1493 if (snd_pcm_running(substream) &&
1494 snd_pcm_update_hw_ptr(substream) >= 0)
1495 runtime->status->hw_ptr %= runtime->buffer_size;
1497 runtime->status->hw_ptr = 0;
1498 snd_pcm_stream_unlock_irqrestore(substream, flags);
1502 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1505 struct snd_pcm_channel_info *info = arg;
1506 struct snd_pcm_runtime *runtime = substream->runtime;
1508 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1512 width = snd_pcm_format_physical_width(runtime->format);
1516 switch (runtime->access) {
1517 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1518 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1519 info->first = info->channel * width;
1520 info->step = runtime->channels * width;
1522 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1523 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1525 size_t size = runtime->dma_bytes / runtime->channels;
1526 info->first = info->channel * size * 8;
1537 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1540 struct snd_pcm_hw_params *params = arg;
1541 snd_pcm_format_t format;
1542 int channels, width;
1544 params->fifo_size = substream->runtime->hw.fifo_size;
1545 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1546 format = params_format(params);
1547 channels = params_channels(params);
1548 width = snd_pcm_format_physical_width(format);
1549 params->fifo_size /= width * channels;
1555 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1556 * @substream: the pcm substream instance
1557 * @cmd: ioctl command
1558 * @arg: ioctl argument
1560 * Processes the generic ioctl commands for PCM.
1561 * Can be passed as the ioctl callback for PCM ops.
1563 * Returns zero if successful, or a negative error code on failure.
1565 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1566 unsigned int cmd, void *arg)
1569 case SNDRV_PCM_IOCTL1_INFO:
1571 case SNDRV_PCM_IOCTL1_RESET:
1572 return snd_pcm_lib_ioctl_reset(substream, arg);
1573 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1574 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1575 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1576 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1581 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1584 * snd_pcm_period_elapsed - update the pcm status for the next period
1585 * @substream: the pcm substream instance
1587 * This function is called from the interrupt handler when the
1588 * PCM has processed the period size. It will update the current
1589 * pointer, wake up sleepers, etc.
1591 * Even if more than one periods have elapsed since the last call, you
1592 * have to call this only once.
1594 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1596 struct snd_pcm_runtime *runtime;
1597 unsigned long flags;
1599 if (PCM_RUNTIME_CHECK(substream))
1601 runtime = substream->runtime;
1603 if (runtime->transfer_ack_begin)
1604 runtime->transfer_ack_begin(substream);
1606 snd_pcm_stream_lock_irqsave(substream, flags);
1607 if (!snd_pcm_running(substream) ||
1608 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1611 if (substream->timer_running)
1612 snd_timer_interrupt(substream->timer, 1);
1614 snd_pcm_stream_unlock_irqrestore(substream, flags);
1615 if (runtime->transfer_ack_end)
1616 runtime->transfer_ack_end(substream);
1617 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1620 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1623 * Wait until avail_min data becomes available
1624 * Returns a negative error code if any error occurs during operation.
1625 * The available space is stored on availp. When err = 0 and avail = 0
1626 * on the capture stream, it indicates the stream is in DRAINING state.
1628 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1629 snd_pcm_uframes_t *availp)
1631 struct snd_pcm_runtime *runtime = substream->runtime;
1632 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1635 snd_pcm_uframes_t avail = 0;
1638 init_waitqueue_entry(&wait, current);
1639 add_wait_queue(&runtime->sleep, &wait);
1641 if (signal_pending(current)) {
1645 set_current_state(TASK_INTERRUPTIBLE);
1646 snd_pcm_stream_unlock_irq(substream);
1647 tout = schedule_timeout(msecs_to_jiffies(10000));
1648 snd_pcm_stream_lock_irq(substream);
1649 switch (runtime->status->state) {
1650 case SNDRV_PCM_STATE_SUSPENDED:
1653 case SNDRV_PCM_STATE_XRUN:
1656 case SNDRV_PCM_STATE_DRAINING:
1660 avail = 0; /* indicate draining */
1662 case SNDRV_PCM_STATE_OPEN:
1663 case SNDRV_PCM_STATE_SETUP:
1664 case SNDRV_PCM_STATE_DISCONNECTED:
1669 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1670 is_playback ? "playback" : "capture");
1675 avail = snd_pcm_playback_avail(runtime);
1677 avail = snd_pcm_capture_avail(runtime);
1678 if (avail >= runtime->control->avail_min)
1682 remove_wait_queue(&runtime->sleep, &wait);
1687 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1689 unsigned long data, unsigned int off,
1690 snd_pcm_uframes_t frames)
1692 struct snd_pcm_runtime *runtime = substream->runtime;
1694 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1695 if (substream->ops->copy) {
1696 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1699 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1700 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1706 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1707 unsigned long data, unsigned int off,
1708 snd_pcm_uframes_t size);
1710 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1712 snd_pcm_uframes_t size,
1714 transfer_f transfer)
1716 struct snd_pcm_runtime *runtime = substream->runtime;
1717 snd_pcm_uframes_t xfer = 0;
1718 snd_pcm_uframes_t offset = 0;
1724 snd_pcm_stream_lock_irq(substream);
1725 switch (runtime->status->state) {
1726 case SNDRV_PCM_STATE_PREPARED:
1727 case SNDRV_PCM_STATE_RUNNING:
1728 case SNDRV_PCM_STATE_PAUSED:
1730 case SNDRV_PCM_STATE_XRUN:
1733 case SNDRV_PCM_STATE_SUSPENDED:
1742 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1743 snd_pcm_uframes_t avail;
1744 snd_pcm_uframes_t cont;
1745 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1746 snd_pcm_update_hw_ptr(substream);
1747 avail = snd_pcm_playback_avail(runtime);
1753 err = wait_for_avail_min(substream, &avail);
1757 frames = size > avail ? avail : size;
1758 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1761 if (snd_BUG_ON(!frames)) {
1762 snd_pcm_stream_unlock_irq(substream);
1765 appl_ptr = runtime->control->appl_ptr;
1766 appl_ofs = appl_ptr % runtime->buffer_size;
1767 snd_pcm_stream_unlock_irq(substream);
1768 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1770 snd_pcm_stream_lock_irq(substream);
1771 switch (runtime->status->state) {
1772 case SNDRV_PCM_STATE_XRUN:
1775 case SNDRV_PCM_STATE_SUSPENDED:
1782 if (appl_ptr >= runtime->boundary)
1783 appl_ptr -= runtime->boundary;
1784 runtime->control->appl_ptr = appl_ptr;
1785 if (substream->ops->ack)
1786 substream->ops->ack(substream);
1791 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1792 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1793 err = snd_pcm_start(substream);
1799 snd_pcm_stream_unlock_irq(substream);
1801 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1804 /* sanity-check for read/write methods */
1805 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1807 struct snd_pcm_runtime *runtime;
1808 if (PCM_RUNTIME_CHECK(substream))
1810 runtime = substream->runtime;
1811 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1813 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1818 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1820 struct snd_pcm_runtime *runtime;
1824 err = pcm_sanity_check(substream);
1827 runtime = substream->runtime;
1828 nonblock = !!(substream->f_flags & O_NONBLOCK);
1830 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1831 runtime->channels > 1)
1833 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1834 snd_pcm_lib_write_transfer);
1837 EXPORT_SYMBOL(snd_pcm_lib_write);
1839 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1841 unsigned long data, unsigned int off,
1842 snd_pcm_uframes_t frames)
1844 struct snd_pcm_runtime *runtime = substream->runtime;
1846 void __user **bufs = (void __user **)data;
1847 int channels = runtime->channels;
1849 if (substream->ops->copy) {
1850 if (snd_BUG_ON(!substream->ops->silence))
1852 for (c = 0; c < channels; ++c, ++bufs) {
1853 if (*bufs == NULL) {
1854 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1857 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1858 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1863 /* default transfer behaviour */
1864 size_t dma_csize = runtime->dma_bytes / channels;
1865 for (c = 0; c < channels; ++c, ++bufs) {
1866 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1867 if (*bufs == NULL) {
1868 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1870 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1871 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1879 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1881 snd_pcm_uframes_t frames)
1883 struct snd_pcm_runtime *runtime;
1887 err = pcm_sanity_check(substream);
1890 runtime = substream->runtime;
1891 nonblock = !!(substream->f_flags & O_NONBLOCK);
1893 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1895 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1896 nonblock, snd_pcm_lib_writev_transfer);
1899 EXPORT_SYMBOL(snd_pcm_lib_writev);
1901 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1903 unsigned long data, unsigned int off,
1904 snd_pcm_uframes_t frames)
1906 struct snd_pcm_runtime *runtime = substream->runtime;
1908 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1909 if (substream->ops->copy) {
1910 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1913 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1914 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1920 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1922 snd_pcm_uframes_t size,
1924 transfer_f transfer)
1926 struct snd_pcm_runtime *runtime = substream->runtime;
1927 snd_pcm_uframes_t xfer = 0;
1928 snd_pcm_uframes_t offset = 0;
1934 snd_pcm_stream_lock_irq(substream);
1935 switch (runtime->status->state) {
1936 case SNDRV_PCM_STATE_PREPARED:
1937 if (size >= runtime->start_threshold) {
1938 err = snd_pcm_start(substream);
1943 case SNDRV_PCM_STATE_DRAINING:
1944 case SNDRV_PCM_STATE_RUNNING:
1945 case SNDRV_PCM_STATE_PAUSED:
1947 case SNDRV_PCM_STATE_XRUN:
1950 case SNDRV_PCM_STATE_SUSPENDED:
1959 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1960 snd_pcm_uframes_t avail;
1961 snd_pcm_uframes_t cont;
1962 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1963 snd_pcm_update_hw_ptr(substream);
1964 avail = snd_pcm_capture_avail(runtime);
1966 if (runtime->status->state ==
1967 SNDRV_PCM_STATE_DRAINING) {
1968 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1975 err = wait_for_avail_min(substream, &avail);
1979 continue; /* draining */
1981 frames = size > avail ? avail : size;
1982 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1985 if (snd_BUG_ON(!frames)) {
1986 snd_pcm_stream_unlock_irq(substream);
1989 appl_ptr = runtime->control->appl_ptr;
1990 appl_ofs = appl_ptr % runtime->buffer_size;
1991 snd_pcm_stream_unlock_irq(substream);
1992 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1994 snd_pcm_stream_lock_irq(substream);
1995 switch (runtime->status->state) {
1996 case SNDRV_PCM_STATE_XRUN:
1999 case SNDRV_PCM_STATE_SUSPENDED:
2006 if (appl_ptr >= runtime->boundary)
2007 appl_ptr -= runtime->boundary;
2008 runtime->control->appl_ptr = appl_ptr;
2009 if (substream->ops->ack)
2010 substream->ops->ack(substream);
2017 snd_pcm_stream_unlock_irq(substream);
2019 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2022 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2024 struct snd_pcm_runtime *runtime;
2028 err = pcm_sanity_check(substream);
2031 runtime = substream->runtime;
2032 nonblock = !!(substream->f_flags & O_NONBLOCK);
2033 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2035 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2038 EXPORT_SYMBOL(snd_pcm_lib_read);
2040 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2042 unsigned long data, unsigned int off,
2043 snd_pcm_uframes_t frames)
2045 struct snd_pcm_runtime *runtime = substream->runtime;
2047 void __user **bufs = (void __user **)data;
2048 int channels = runtime->channels;
2050 if (substream->ops->copy) {
2051 for (c = 0; c < channels; ++c, ++bufs) {
2055 buf = *bufs + samples_to_bytes(runtime, off);
2056 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2060 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2061 for (c = 0; c < channels; ++c, ++bufs) {
2067 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2068 buf = *bufs + samples_to_bytes(runtime, off);
2069 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2076 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2078 snd_pcm_uframes_t frames)
2080 struct snd_pcm_runtime *runtime;
2084 err = pcm_sanity_check(substream);
2087 runtime = substream->runtime;
2088 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2091 nonblock = !!(substream->f_flags & O_NONBLOCK);
2092 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2094 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2097 EXPORT_SYMBOL(snd_pcm_lib_readv);