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) ((substream)->pstr->xrun_debug)
132 #define xrun_debug(substream) 0
135 #define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream) > 1) \
140 static void xrun(struct snd_pcm_substream *substream)
142 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
143 if (xrun_debug(substream)) {
144 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
145 substream->pcm->card->number,
146 substream->pcm->device,
147 substream->stream ? 'c' : 'p');
148 dump_stack_on_xrun(substream);
152 static snd_pcm_uframes_t
153 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
154 struct snd_pcm_runtime *runtime)
156 snd_pcm_uframes_t pos;
158 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
159 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
160 pos = substream->ops->pointer(substream);
161 if (pos == SNDRV_PCM_POS_XRUN)
162 return pos; /* XRUN */
163 if (pos >= runtime->buffer_size) {
164 if (printk_ratelimit()) {
165 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
166 "buffer size = 0x%lx, period size = 0x%lx\n",
167 substream->stream, pos, runtime->buffer_size,
168 runtime->period_size);
172 pos -= pos % runtime->min_align;
176 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
177 struct snd_pcm_runtime *runtime)
179 snd_pcm_uframes_t avail;
181 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
182 avail = snd_pcm_playback_avail(runtime);
184 avail = snd_pcm_capture_avail(runtime);
185 if (avail > runtime->avail_max)
186 runtime->avail_max = avail;
187 if (avail >= runtime->stop_threshold) {
188 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
189 snd_pcm_drain_done(substream);
194 if (avail >= runtime->control->avail_min)
195 wake_up(&runtime->sleep);
199 #define hw_ptr_error(substream, fmt, args...) \
201 if (xrun_debug(substream)) { \
202 if (printk_ratelimit()) { \
203 snd_printd("PCM: " fmt, ##args); \
205 dump_stack_on_xrun(substream); \
209 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
211 struct snd_pcm_runtime *runtime = substream->runtime;
212 snd_pcm_uframes_t pos;
213 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
214 snd_pcm_sframes_t hdelta, delta;
215 unsigned long jdelta;
217 old_hw_ptr = runtime->status->hw_ptr;
218 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
219 if (pos == SNDRV_PCM_POS_XRUN) {
223 hw_base = runtime->hw_ptr_base;
224 new_hw_ptr = hw_base + pos;
225 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
226 delta = new_hw_ptr - hw_ptr_interrupt;
227 if (hw_ptr_interrupt >= runtime->boundary) {
228 hw_ptr_interrupt -= runtime->boundary;
229 if (hw_base < runtime->boundary / 2)
230 /* hw_base was already lapped; recalc delta */
231 delta = new_hw_ptr - hw_ptr_interrupt;
234 delta += runtime->buffer_size;
236 hw_ptr_error(substream,
237 "Unexpected hw_pointer value "
238 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
239 substream->stream, (long)pos,
240 (long)hw_ptr_interrupt);
241 /* rebase to interrupt position */
242 hw_base = new_hw_ptr = hw_ptr_interrupt;
243 /* align hw_base to buffer_size */
244 hw_base -= hw_base % runtime->buffer_size;
247 hw_base += runtime->buffer_size;
248 if (hw_base >= runtime->boundary)
250 new_hw_ptr = hw_base + pos;
254 /* Do jiffies check only in xrun_debug mode */
255 if (!xrun_debug(substream))
256 goto no_jiffies_check;
258 /* Skip the jiffies check for hardwares with BATCH flag.
259 * Such hardware usually just increases the position at each IRQ,
260 * thus it can't give any strange position.
262 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
263 goto no_jiffies_check;
264 hdelta = new_hw_ptr - old_hw_ptr;
265 jdelta = jiffies - runtime->hw_ptr_jiffies;
266 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
268 (((runtime->period_size * HZ) / runtime->rate)
270 hw_ptr_error(substream,
271 "hw_ptr skipping! [Q] "
272 "(pos=%ld, delta=%ld, period=%ld, "
273 "jdelta=%lu/%lu/%lu)\n",
274 (long)pos, (long)hdelta,
275 (long)runtime->period_size, jdelta,
276 ((hdelta * HZ) / runtime->rate), delta);
277 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
278 runtime->period_size * delta;
279 if (hw_ptr_interrupt >= runtime->boundary)
280 hw_ptr_interrupt -= runtime->boundary;
281 /* rebase to interrupt position */
282 hw_base = new_hw_ptr = hw_ptr_interrupt;
283 /* align hw_base to buffer_size */
284 hw_base -= hw_base % runtime->buffer_size;
288 if (delta > runtime->period_size + runtime->period_size / 2) {
289 hw_ptr_error(substream,
291 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
292 substream->stream, (long)delta,
293 (long)hw_ptr_interrupt);
294 /* rebase hw_ptr_interrupt */
296 new_hw_ptr - new_hw_ptr % runtime->period_size;
298 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
299 runtime->silence_size > 0)
300 snd_pcm_playback_silence(substream, new_hw_ptr);
302 runtime->hw_ptr_base = hw_base;
303 runtime->status->hw_ptr = new_hw_ptr;
304 runtime->hw_ptr_jiffies = jiffies;
305 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
307 return snd_pcm_update_hw_ptr_post(substream, runtime);
310 /* CAUTION: call it with irq disabled */
311 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 snd_pcm_uframes_t pos;
315 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
316 snd_pcm_sframes_t delta;
317 unsigned long jdelta;
319 old_hw_ptr = runtime->status->hw_ptr;
320 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
321 if (pos == SNDRV_PCM_POS_XRUN) {
325 hw_base = runtime->hw_ptr_base;
326 new_hw_ptr = hw_base + pos;
328 delta = new_hw_ptr - old_hw_ptr;
329 jdelta = jiffies - runtime->hw_ptr_jiffies;
331 delta += runtime->buffer_size;
333 hw_ptr_error(substream,
334 "Unexpected hw_pointer value [2] "
335 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
336 substream->stream, (long)pos,
337 (long)old_hw_ptr, jdelta);
340 hw_base += runtime->buffer_size;
341 if (hw_base >= runtime->boundary)
343 new_hw_ptr = hw_base + pos;
345 /* Do jiffies check only in xrun_debug mode */
346 if (xrun_debug(substream) &&
347 ((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
348 hw_ptr_error(substream,
350 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
351 (long)pos, (long)delta,
352 (long)runtime->period_size, jdelta,
353 ((delta * HZ) / runtime->rate));
356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
357 runtime->silence_size > 0)
358 snd_pcm_playback_silence(substream, new_hw_ptr);
360 runtime->hw_ptr_base = hw_base;
361 runtime->status->hw_ptr = new_hw_ptr;
362 runtime->hw_ptr_jiffies = jiffies;
364 return snd_pcm_update_hw_ptr_post(substream, runtime);
368 * snd_pcm_set_ops - set the PCM operators
369 * @pcm: the pcm instance
370 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
371 * @ops: the operator table
373 * Sets the given PCM operators to the pcm instance.
375 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
377 struct snd_pcm_str *stream = &pcm->streams[direction];
378 struct snd_pcm_substream *substream;
380 for (substream = stream->substream; substream != NULL; substream = substream->next)
381 substream->ops = ops;
384 EXPORT_SYMBOL(snd_pcm_set_ops);
387 * snd_pcm_sync - set the PCM sync id
388 * @substream: the pcm substream
390 * Sets the PCM sync identifier for the card.
392 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
394 struct snd_pcm_runtime *runtime = substream->runtime;
396 runtime->sync.id32[0] = substream->pcm->card->number;
397 runtime->sync.id32[1] = -1;
398 runtime->sync.id32[2] = -1;
399 runtime->sync.id32[3] = -1;
402 EXPORT_SYMBOL(snd_pcm_set_sync);
405 * Standard ioctl routine
408 static inline unsigned int div32(unsigned int a, unsigned int b,
419 static inline unsigned int div_down(unsigned int a, unsigned int b)
426 static inline unsigned int div_up(unsigned int a, unsigned int b)
438 static inline unsigned int mul(unsigned int a, unsigned int b)
442 if (div_down(UINT_MAX, a) < b)
447 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
448 unsigned int c, unsigned int *r)
450 u_int64_t n = (u_int64_t) a * b;
456 n = div_u64_rem(n, c, r);
465 * snd_interval_refine - refine the interval value of configurator
466 * @i: the interval value to refine
467 * @v: the interval value to refer to
469 * Refines the interval value with the reference value.
470 * The interval is changed to the range satisfying both intervals.
471 * The interval status (min, max, integer, etc.) are evaluated.
473 * Returns non-zero if the value is changed, zero if not changed.
475 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
478 if (snd_BUG_ON(snd_interval_empty(i)))
480 if (i->min < v->min) {
482 i->openmin = v->openmin;
484 } else if (i->min == v->min && !i->openmin && v->openmin) {
488 if (i->max > v->max) {
490 i->openmax = v->openmax;
492 } else if (i->max == v->max && !i->openmax && v->openmax) {
496 if (!i->integer && v->integer) {
509 } else if (!i->openmin && !i->openmax && i->min == i->max)
511 if (snd_interval_checkempty(i)) {
512 snd_interval_none(i);
518 EXPORT_SYMBOL(snd_interval_refine);
520 static int snd_interval_refine_first(struct snd_interval *i)
522 if (snd_BUG_ON(snd_interval_empty(i)))
524 if (snd_interval_single(i))
527 i->openmax = i->openmin;
533 static int snd_interval_refine_last(struct snd_interval *i)
535 if (snd_BUG_ON(snd_interval_empty(i)))
537 if (snd_interval_single(i))
540 i->openmin = i->openmax;
546 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
548 if (a->empty || b->empty) {
549 snd_interval_none(c);
553 c->min = mul(a->min, b->min);
554 c->openmin = (a->openmin || b->openmin);
555 c->max = mul(a->max, b->max);
556 c->openmax = (a->openmax || b->openmax);
557 c->integer = (a->integer && b->integer);
561 * snd_interval_div - refine the interval value with division
568 * Returns non-zero if the value is changed, zero if not changed.
570 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
573 if (a->empty || b->empty) {
574 snd_interval_none(c);
578 c->min = div32(a->min, b->max, &r);
579 c->openmin = (r || a->openmin || b->openmax);
581 c->max = div32(a->max, b->min, &r);
586 c->openmax = (a->openmax || b->openmin);
595 * snd_interval_muldivk - refine the interval value
598 * @k: divisor (as integer)
603 * Returns non-zero if the value is changed, zero if not changed.
605 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
606 unsigned int k, struct snd_interval *c)
609 if (a->empty || b->empty) {
610 snd_interval_none(c);
614 c->min = muldiv32(a->min, b->min, k, &r);
615 c->openmin = (r || a->openmin || b->openmin);
616 c->max = muldiv32(a->max, b->max, k, &r);
621 c->openmax = (a->openmax || b->openmax);
626 * snd_interval_mulkdiv - refine the interval value
628 * @k: dividend 2 (as integer)
634 * Returns non-zero if the value is changed, zero if not changed.
636 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
637 const struct snd_interval *b, struct snd_interval *c)
640 if (a->empty || b->empty) {
641 snd_interval_none(c);
645 c->min = muldiv32(a->min, k, b->max, &r);
646 c->openmin = (r || a->openmin || b->openmax);
648 c->max = muldiv32(a->max, k, b->min, &r);
653 c->openmax = (a->openmax || b->openmin);
665 * snd_interval_ratnum - refine the interval value
666 * @i: interval to refine
667 * @rats_count: number of ratnum_t
668 * @rats: ratnum_t array
669 * @nump: pointer to store the resultant numerator
670 * @denp: pointer to store the resultant denominator
672 * Returns non-zero if the value is changed, zero if not changed.
674 int snd_interval_ratnum(struct snd_interval *i,
675 unsigned int rats_count, struct snd_ratnum *rats,
676 unsigned int *nump, unsigned int *denp)
678 unsigned int best_num, best_diff, best_den;
680 struct snd_interval t;
683 best_num = best_den = best_diff = 0;
684 for (k = 0; k < rats_count; ++k) {
685 unsigned int num = rats[k].num;
687 unsigned int q = i->min;
691 den = div_down(num, q);
692 if (den < rats[k].den_min)
694 if (den > rats[k].den_max)
695 den = rats[k].den_max;
698 r = (den - rats[k].den_min) % rats[k].den_step;
702 diff = num - q * den;
704 diff * best_den < best_diff * den) {
714 t.min = div_down(best_num, best_den);
715 t.openmin = !!(best_num % best_den);
717 best_num = best_den = best_diff = 0;
718 for (k = 0; k < rats_count; ++k) {
719 unsigned int num = rats[k].num;
721 unsigned int q = i->max;
727 den = div_up(num, q);
728 if (den > rats[k].den_max)
730 if (den < rats[k].den_min)
731 den = rats[k].den_min;
734 r = (den - rats[k].den_min) % rats[k].den_step;
736 den += rats[k].den_step - r;
738 diff = q * den - num;
740 diff * best_den < best_diff * den) {
750 t.max = div_up(best_num, best_den);
751 t.openmax = !!(best_num % best_den);
753 err = snd_interval_refine(i, &t);
757 if (snd_interval_single(i)) {
766 EXPORT_SYMBOL(snd_interval_ratnum);
769 * snd_interval_ratden - refine the interval value
770 * @i: interval to refine
771 * @rats_count: number of struct ratden
772 * @rats: struct ratden array
773 * @nump: pointer to store the resultant numerator
774 * @denp: pointer to store the resultant denominator
776 * Returns non-zero if the value is changed, zero if not changed.
778 static int snd_interval_ratden(struct snd_interval *i,
779 unsigned int rats_count, struct snd_ratden *rats,
780 unsigned int *nump, unsigned int *denp)
782 unsigned int best_num, best_diff, best_den;
784 struct snd_interval t;
787 best_num = best_den = best_diff = 0;
788 for (k = 0; k < rats_count; ++k) {
790 unsigned int den = rats[k].den;
791 unsigned int q = i->min;
794 if (num > rats[k].num_max)
796 if (num < rats[k].num_min)
797 num = rats[k].num_max;
800 r = (num - rats[k].num_min) % rats[k].num_step;
802 num += rats[k].num_step - r;
804 diff = num - q * den;
806 diff * best_den < best_diff * den) {
816 t.min = div_down(best_num, best_den);
817 t.openmin = !!(best_num % best_den);
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->max;
826 if (num < rats[k].num_min)
828 if (num > rats[k].num_max)
829 num = rats[k].num_max;
832 r = (num - rats[k].num_min) % rats[k].num_step;
836 diff = q * den - num;
838 diff * best_den < best_diff * den) {
848 t.max = div_up(best_num, best_den);
849 t.openmax = !!(best_num % best_den);
851 err = snd_interval_refine(i, &t);
855 if (snd_interval_single(i)) {
865 * snd_interval_list - refine the interval value from the list
866 * @i: the interval value to refine
867 * @count: the number of elements in the list
868 * @list: the value list
869 * @mask: the bit-mask to evaluate
871 * Refines the interval value from the list.
872 * When mask is non-zero, only the elements corresponding to bit 1 are
875 * Returns non-zero if the value is changed, zero if not changed.
877 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
886 for (k = 0; k < count; k++) {
887 if (mask && !(mask & (1 << k)))
889 if (i->min == list[k] && !i->openmin)
891 if (i->min < list[k]) {
901 for (k = count; k-- > 0;) {
902 if (mask && !(mask & (1 << k)))
904 if (i->max == list[k] && !i->openmax)
906 if (i->max > list[k]) {
916 if (snd_interval_checkempty(i)) {
923 EXPORT_SYMBOL(snd_interval_list);
925 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
929 n = (i->min - min) % step;
930 if (n != 0 || i->openmin) {
934 n = (i->max - min) % step;
935 if (n != 0 || i->openmax) {
939 if (snd_interval_checkempty(i)) {
946 /* Info constraints helpers */
949 * snd_pcm_hw_rule_add - add the hw-constraint rule
950 * @runtime: the pcm runtime instance
951 * @cond: condition bits
952 * @var: the variable to evaluate
953 * @func: the evaluation function
954 * @private: the private data pointer passed to function
955 * @dep: the dependent variables
957 * Returns zero if successful, or a negative error code on failure.
959 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
961 snd_pcm_hw_rule_func_t func, void *private,
964 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
965 struct snd_pcm_hw_rule *c;
969 if (constrs->rules_num >= constrs->rules_all) {
970 struct snd_pcm_hw_rule *new;
971 unsigned int new_rules = constrs->rules_all + 16;
972 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
975 if (constrs->rules) {
976 memcpy(new, constrs->rules,
977 constrs->rules_num * sizeof(*c));
978 kfree(constrs->rules);
980 constrs->rules = new;
981 constrs->rules_all = new_rules;
983 c = &constrs->rules[constrs->rules_num];
987 c->private = private;
990 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
995 dep = va_arg(args, int);
997 constrs->rules_num++;
1002 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1005 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1006 * @runtime: PCM runtime instance
1007 * @var: hw_params variable to apply the mask
1008 * @mask: the bitmap mask
1010 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1012 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1015 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1016 struct snd_mask *maskp = constrs_mask(constrs, var);
1017 *maskp->bits &= mask;
1018 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1019 if (*maskp->bits == 0)
1025 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1026 * @runtime: PCM runtime instance
1027 * @var: hw_params variable to apply the mask
1028 * @mask: the 64bit bitmap mask
1030 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1032 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1035 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1036 struct snd_mask *maskp = constrs_mask(constrs, var);
1037 maskp->bits[0] &= (u_int32_t)mask;
1038 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1039 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1040 if (! maskp->bits[0] && ! maskp->bits[1])
1046 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1047 * @runtime: PCM runtime instance
1048 * @var: hw_params variable to apply the integer constraint
1050 * Apply the constraint of integer to an interval parameter.
1052 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1054 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1055 return snd_interval_setinteger(constrs_interval(constrs, var));
1058 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1061 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1062 * @runtime: PCM runtime instance
1063 * @var: hw_params variable to apply the range
1064 * @min: the minimal value
1065 * @max: the maximal value
1067 * Apply the min/max range constraint to an interval parameter.
1069 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1070 unsigned int min, unsigned int max)
1072 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1073 struct snd_interval t;
1076 t.openmin = t.openmax = 0;
1078 return snd_interval_refine(constrs_interval(constrs, var), &t);
1081 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1083 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1084 struct snd_pcm_hw_rule *rule)
1086 struct snd_pcm_hw_constraint_list *list = rule->private;
1087 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1092 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1093 * @runtime: PCM runtime instance
1094 * @cond: condition bits
1095 * @var: hw_params variable to apply the list constraint
1098 * Apply the list of constraints to an interval parameter.
1100 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1102 snd_pcm_hw_param_t var,
1103 struct snd_pcm_hw_constraint_list *l)
1105 return snd_pcm_hw_rule_add(runtime, cond, var,
1106 snd_pcm_hw_rule_list, l,
1110 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1112 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1113 struct snd_pcm_hw_rule *rule)
1115 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1116 unsigned int num = 0, den = 0;
1118 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1119 r->nrats, r->rats, &num, &den);
1120 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1121 params->rate_num = num;
1122 params->rate_den = den;
1128 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1129 * @runtime: PCM runtime instance
1130 * @cond: condition bits
1131 * @var: hw_params variable to apply the ratnums constraint
1132 * @r: struct snd_ratnums constriants
1134 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1136 snd_pcm_hw_param_t var,
1137 struct snd_pcm_hw_constraint_ratnums *r)
1139 return snd_pcm_hw_rule_add(runtime, cond, var,
1140 snd_pcm_hw_rule_ratnums, r,
1144 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1146 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1147 struct snd_pcm_hw_rule *rule)
1149 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1150 unsigned int num = 0, den = 0;
1151 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1152 r->nrats, r->rats, &num, &den);
1153 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1154 params->rate_num = num;
1155 params->rate_den = den;
1161 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1162 * @runtime: PCM runtime instance
1163 * @cond: condition bits
1164 * @var: hw_params variable to apply the ratdens constraint
1165 * @r: struct snd_ratdens constriants
1167 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1169 snd_pcm_hw_param_t var,
1170 struct snd_pcm_hw_constraint_ratdens *r)
1172 return snd_pcm_hw_rule_add(runtime, cond, var,
1173 snd_pcm_hw_rule_ratdens, r,
1177 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1179 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1180 struct snd_pcm_hw_rule *rule)
1182 unsigned int l = (unsigned long) rule->private;
1183 int width = l & 0xffff;
1184 unsigned int msbits = l >> 16;
1185 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1186 if (snd_interval_single(i) && snd_interval_value(i) == width)
1187 params->msbits = msbits;
1192 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1193 * @runtime: PCM runtime instance
1194 * @cond: condition bits
1195 * @width: sample bits width
1196 * @msbits: msbits width
1198 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1201 unsigned int msbits)
1203 unsigned long l = (msbits << 16) | width;
1204 return snd_pcm_hw_rule_add(runtime, cond, -1,
1205 snd_pcm_hw_rule_msbits,
1207 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1210 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1212 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1213 struct snd_pcm_hw_rule *rule)
1215 unsigned long step = (unsigned long) rule->private;
1216 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1220 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1221 * @runtime: PCM runtime instance
1222 * @cond: condition bits
1223 * @var: hw_params variable to apply the step constraint
1226 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1228 snd_pcm_hw_param_t var,
1231 return snd_pcm_hw_rule_add(runtime, cond, var,
1232 snd_pcm_hw_rule_step, (void *) step,
1236 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1238 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1240 static unsigned int pow2_sizes[] = {
1241 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1242 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1243 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1244 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1246 return snd_interval_list(hw_param_interval(params, rule->var),
1247 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1251 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1252 * @runtime: PCM runtime instance
1253 * @cond: condition bits
1254 * @var: hw_params variable to apply the power-of-2 constraint
1256 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1258 snd_pcm_hw_param_t var)
1260 return snd_pcm_hw_rule_add(runtime, cond, var,
1261 snd_pcm_hw_rule_pow2, NULL,
1265 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1267 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1268 snd_pcm_hw_param_t var)
1270 if (hw_is_mask(var)) {
1271 snd_mask_any(hw_param_mask(params, var));
1272 params->cmask |= 1 << var;
1273 params->rmask |= 1 << var;
1276 if (hw_is_interval(var)) {
1277 snd_interval_any(hw_param_interval(params, var));
1278 params->cmask |= 1 << var;
1279 params->rmask |= 1 << var;
1285 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1288 memset(params, 0, sizeof(*params));
1289 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1290 _snd_pcm_hw_param_any(params, k);
1291 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1292 _snd_pcm_hw_param_any(params, k);
1296 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1299 * snd_pcm_hw_param_value - return @params field @var value
1300 * @params: the hw_params instance
1301 * @var: parameter to retrieve
1302 * @dir: pointer to the direction (-1,0,1) or %NULL
1304 * Return the value for field @var if it's fixed in configuration space
1305 * defined by @params. Return -%EINVAL otherwise.
1307 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1308 snd_pcm_hw_param_t var, int *dir)
1310 if (hw_is_mask(var)) {
1311 const struct snd_mask *mask = hw_param_mask_c(params, var);
1312 if (!snd_mask_single(mask))
1316 return snd_mask_value(mask);
1318 if (hw_is_interval(var)) {
1319 const struct snd_interval *i = hw_param_interval_c(params, var);
1320 if (!snd_interval_single(i))
1324 return snd_interval_value(i);
1329 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1331 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1332 snd_pcm_hw_param_t var)
1334 if (hw_is_mask(var)) {
1335 snd_mask_none(hw_param_mask(params, var));
1336 params->cmask |= 1 << var;
1337 params->rmask |= 1 << var;
1338 } else if (hw_is_interval(var)) {
1339 snd_interval_none(hw_param_interval(params, var));
1340 params->cmask |= 1 << var;
1341 params->rmask |= 1 << var;
1347 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1349 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1350 snd_pcm_hw_param_t var)
1353 if (hw_is_mask(var))
1354 changed = snd_mask_refine_first(hw_param_mask(params, var));
1355 else if (hw_is_interval(var))
1356 changed = snd_interval_refine_first(hw_param_interval(params, var));
1360 params->cmask |= 1 << var;
1361 params->rmask |= 1 << var;
1368 * snd_pcm_hw_param_first - refine config space and return minimum value
1369 * @pcm: PCM instance
1370 * @params: the hw_params instance
1371 * @var: parameter to retrieve
1372 * @dir: pointer to the direction (-1,0,1) or %NULL
1374 * Inside configuration space defined by @params remove from @var all
1375 * values > minimum. Reduce configuration space accordingly.
1376 * Return the minimum.
1378 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1379 struct snd_pcm_hw_params *params,
1380 snd_pcm_hw_param_t var, int *dir)
1382 int changed = _snd_pcm_hw_param_first(params, var);
1385 if (params->rmask) {
1386 int err = snd_pcm_hw_refine(pcm, params);
1387 if (snd_BUG_ON(err < 0))
1390 return snd_pcm_hw_param_value(params, var, dir);
1393 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1395 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1396 snd_pcm_hw_param_t var)
1399 if (hw_is_mask(var))
1400 changed = snd_mask_refine_last(hw_param_mask(params, var));
1401 else if (hw_is_interval(var))
1402 changed = snd_interval_refine_last(hw_param_interval(params, var));
1406 params->cmask |= 1 << var;
1407 params->rmask |= 1 << var;
1414 * snd_pcm_hw_param_last - refine config space and return maximum value
1415 * @pcm: PCM instance
1416 * @params: the hw_params instance
1417 * @var: parameter to retrieve
1418 * @dir: pointer to the direction (-1,0,1) or %NULL
1420 * Inside configuration space defined by @params remove from @var all
1421 * values < maximum. Reduce configuration space accordingly.
1422 * Return the maximum.
1424 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1425 struct snd_pcm_hw_params *params,
1426 snd_pcm_hw_param_t var, int *dir)
1428 int changed = _snd_pcm_hw_param_last(params, var);
1431 if (params->rmask) {
1432 int err = snd_pcm_hw_refine(pcm, params);
1433 if (snd_BUG_ON(err < 0))
1436 return snd_pcm_hw_param_value(params, var, dir);
1439 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1442 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1443 * @pcm: PCM instance
1444 * @params: the hw_params instance
1446 * Choose one configuration from configuration space defined by @params.
1447 * The configuration chosen is that obtained fixing in this order:
1448 * first access, first format, first subformat, min channels,
1449 * min rate, min period time, max buffer size, min tick time
1451 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1452 struct snd_pcm_hw_params *params)
1454 static int vars[] = {
1455 SNDRV_PCM_HW_PARAM_ACCESS,
1456 SNDRV_PCM_HW_PARAM_FORMAT,
1457 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1458 SNDRV_PCM_HW_PARAM_CHANNELS,
1459 SNDRV_PCM_HW_PARAM_RATE,
1460 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1461 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1462 SNDRV_PCM_HW_PARAM_TICK_TIME,
1467 for (v = vars; *v != -1; v++) {
1468 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1469 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1471 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1472 if (snd_BUG_ON(err < 0))
1478 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1481 struct snd_pcm_runtime *runtime = substream->runtime;
1482 unsigned long flags;
1483 snd_pcm_stream_lock_irqsave(substream, flags);
1484 if (snd_pcm_running(substream) &&
1485 snd_pcm_update_hw_ptr(substream) >= 0)
1486 runtime->status->hw_ptr %= runtime->buffer_size;
1488 runtime->status->hw_ptr = 0;
1489 snd_pcm_stream_unlock_irqrestore(substream, flags);
1493 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1496 struct snd_pcm_channel_info *info = arg;
1497 struct snd_pcm_runtime *runtime = substream->runtime;
1499 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1503 width = snd_pcm_format_physical_width(runtime->format);
1507 switch (runtime->access) {
1508 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1509 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1510 info->first = info->channel * width;
1511 info->step = runtime->channels * width;
1513 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1514 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1516 size_t size = runtime->dma_bytes / runtime->channels;
1517 info->first = info->channel * size * 8;
1529 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1530 * @substream: the pcm substream instance
1531 * @cmd: ioctl command
1532 * @arg: ioctl argument
1534 * Processes the generic ioctl commands for PCM.
1535 * Can be passed as the ioctl callback for PCM ops.
1537 * Returns zero if successful, or a negative error code on failure.
1539 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1540 unsigned int cmd, void *arg)
1543 case SNDRV_PCM_IOCTL1_INFO:
1545 case SNDRV_PCM_IOCTL1_RESET:
1546 return snd_pcm_lib_ioctl_reset(substream, arg);
1547 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1548 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1553 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1556 * snd_pcm_period_elapsed - update the pcm status for the next period
1557 * @substream: the pcm substream instance
1559 * This function is called from the interrupt handler when the
1560 * PCM has processed the period size. It will update the current
1561 * pointer, wake up sleepers, etc.
1563 * Even if more than one periods have elapsed since the last call, you
1564 * have to call this only once.
1566 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1568 struct snd_pcm_runtime *runtime;
1569 unsigned long flags;
1571 if (PCM_RUNTIME_CHECK(substream))
1573 runtime = substream->runtime;
1575 if (runtime->transfer_ack_begin)
1576 runtime->transfer_ack_begin(substream);
1578 snd_pcm_stream_lock_irqsave(substream, flags);
1579 if (!snd_pcm_running(substream) ||
1580 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1583 if (substream->timer_running)
1584 snd_timer_interrupt(substream->timer, 1);
1586 snd_pcm_stream_unlock_irqrestore(substream, flags);
1587 if (runtime->transfer_ack_end)
1588 runtime->transfer_ack_end(substream);
1589 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1592 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1595 * Wait until avail_min data becomes available
1596 * Returns a negative error code if any error occurs during operation.
1597 * The available space is stored on availp. When err = 0 and avail = 0
1598 * on the capture stream, it indicates the stream is in DRAINING state.
1600 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1601 snd_pcm_uframes_t *availp)
1603 struct snd_pcm_runtime *runtime = substream->runtime;
1604 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1607 snd_pcm_uframes_t avail = 0;
1610 init_waitqueue_entry(&wait, current);
1611 add_wait_queue(&runtime->sleep, &wait);
1613 if (signal_pending(current)) {
1617 set_current_state(TASK_INTERRUPTIBLE);
1618 snd_pcm_stream_unlock_irq(substream);
1619 tout = schedule_timeout(msecs_to_jiffies(10000));
1620 snd_pcm_stream_lock_irq(substream);
1621 switch (runtime->status->state) {
1622 case SNDRV_PCM_STATE_SUSPENDED:
1625 case SNDRV_PCM_STATE_XRUN:
1628 case SNDRV_PCM_STATE_DRAINING:
1632 avail = 0; /* indicate draining */
1634 case SNDRV_PCM_STATE_OPEN:
1635 case SNDRV_PCM_STATE_SETUP:
1636 case SNDRV_PCM_STATE_DISCONNECTED:
1641 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1642 is_playback ? "playback" : "capture");
1647 avail = snd_pcm_playback_avail(runtime);
1649 avail = snd_pcm_capture_avail(runtime);
1650 if (avail >= runtime->control->avail_min)
1654 remove_wait_queue(&runtime->sleep, &wait);
1659 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1661 unsigned long data, unsigned int off,
1662 snd_pcm_uframes_t frames)
1664 struct snd_pcm_runtime *runtime = substream->runtime;
1666 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1667 if (substream->ops->copy) {
1668 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1671 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1672 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1678 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1679 unsigned long data, unsigned int off,
1680 snd_pcm_uframes_t size);
1682 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1684 snd_pcm_uframes_t size,
1686 transfer_f transfer)
1688 struct snd_pcm_runtime *runtime = substream->runtime;
1689 snd_pcm_uframes_t xfer = 0;
1690 snd_pcm_uframes_t offset = 0;
1696 snd_pcm_stream_lock_irq(substream);
1697 switch (runtime->status->state) {
1698 case SNDRV_PCM_STATE_PREPARED:
1699 case SNDRV_PCM_STATE_RUNNING:
1700 case SNDRV_PCM_STATE_PAUSED:
1702 case SNDRV_PCM_STATE_XRUN:
1705 case SNDRV_PCM_STATE_SUSPENDED:
1714 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1715 snd_pcm_uframes_t avail;
1716 snd_pcm_uframes_t cont;
1717 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1718 snd_pcm_update_hw_ptr(substream);
1719 avail = snd_pcm_playback_avail(runtime);
1725 err = wait_for_avail_min(substream, &avail);
1729 frames = size > avail ? avail : size;
1730 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1733 if (snd_BUG_ON(!frames)) {
1734 snd_pcm_stream_unlock_irq(substream);
1737 appl_ptr = runtime->control->appl_ptr;
1738 appl_ofs = appl_ptr % runtime->buffer_size;
1739 snd_pcm_stream_unlock_irq(substream);
1740 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1742 snd_pcm_stream_lock_irq(substream);
1743 switch (runtime->status->state) {
1744 case SNDRV_PCM_STATE_XRUN:
1747 case SNDRV_PCM_STATE_SUSPENDED:
1754 if (appl_ptr >= runtime->boundary)
1755 appl_ptr -= runtime->boundary;
1756 runtime->control->appl_ptr = appl_ptr;
1757 if (substream->ops->ack)
1758 substream->ops->ack(substream);
1763 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1764 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1765 err = snd_pcm_start(substream);
1771 snd_pcm_stream_unlock_irq(substream);
1773 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1776 /* sanity-check for read/write methods */
1777 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1779 struct snd_pcm_runtime *runtime;
1780 if (PCM_RUNTIME_CHECK(substream))
1782 runtime = substream->runtime;
1783 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1785 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1790 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1792 struct snd_pcm_runtime *runtime;
1796 err = pcm_sanity_check(substream);
1799 runtime = substream->runtime;
1800 nonblock = !!(substream->f_flags & O_NONBLOCK);
1802 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1803 runtime->channels > 1)
1805 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1806 snd_pcm_lib_write_transfer);
1809 EXPORT_SYMBOL(snd_pcm_lib_write);
1811 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1813 unsigned long data, unsigned int off,
1814 snd_pcm_uframes_t frames)
1816 struct snd_pcm_runtime *runtime = substream->runtime;
1818 void __user **bufs = (void __user **)data;
1819 int channels = runtime->channels;
1821 if (substream->ops->copy) {
1822 if (snd_BUG_ON(!substream->ops->silence))
1824 for (c = 0; c < channels; ++c, ++bufs) {
1825 if (*bufs == NULL) {
1826 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1829 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1830 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1835 /* default transfer behaviour */
1836 size_t dma_csize = runtime->dma_bytes / channels;
1837 for (c = 0; c < channels; ++c, ++bufs) {
1838 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1839 if (*bufs == NULL) {
1840 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1842 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1843 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1851 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1853 snd_pcm_uframes_t frames)
1855 struct snd_pcm_runtime *runtime;
1859 err = pcm_sanity_check(substream);
1862 runtime = substream->runtime;
1863 nonblock = !!(substream->f_flags & O_NONBLOCK);
1865 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1867 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1868 nonblock, snd_pcm_lib_writev_transfer);
1871 EXPORT_SYMBOL(snd_pcm_lib_writev);
1873 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1875 unsigned long data, unsigned int off,
1876 snd_pcm_uframes_t frames)
1878 struct snd_pcm_runtime *runtime = substream->runtime;
1880 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1881 if (substream->ops->copy) {
1882 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1885 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1886 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1892 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1894 snd_pcm_uframes_t size,
1896 transfer_f transfer)
1898 struct snd_pcm_runtime *runtime = substream->runtime;
1899 snd_pcm_uframes_t xfer = 0;
1900 snd_pcm_uframes_t offset = 0;
1906 snd_pcm_stream_lock_irq(substream);
1907 switch (runtime->status->state) {
1908 case SNDRV_PCM_STATE_PREPARED:
1909 if (size >= runtime->start_threshold) {
1910 err = snd_pcm_start(substream);
1915 case SNDRV_PCM_STATE_DRAINING:
1916 case SNDRV_PCM_STATE_RUNNING:
1917 case SNDRV_PCM_STATE_PAUSED:
1919 case SNDRV_PCM_STATE_XRUN:
1922 case SNDRV_PCM_STATE_SUSPENDED:
1931 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1932 snd_pcm_uframes_t avail;
1933 snd_pcm_uframes_t cont;
1934 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1935 snd_pcm_update_hw_ptr(substream);
1936 avail = snd_pcm_capture_avail(runtime);
1938 if (runtime->status->state ==
1939 SNDRV_PCM_STATE_DRAINING) {
1940 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1947 err = wait_for_avail_min(substream, &avail);
1951 continue; /* draining */
1953 frames = size > avail ? avail : size;
1954 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1957 if (snd_BUG_ON(!frames)) {
1958 snd_pcm_stream_unlock_irq(substream);
1961 appl_ptr = runtime->control->appl_ptr;
1962 appl_ofs = appl_ptr % runtime->buffer_size;
1963 snd_pcm_stream_unlock_irq(substream);
1964 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1966 snd_pcm_stream_lock_irq(substream);
1967 switch (runtime->status->state) {
1968 case SNDRV_PCM_STATE_XRUN:
1971 case SNDRV_PCM_STATE_SUSPENDED:
1978 if (appl_ptr >= runtime->boundary)
1979 appl_ptr -= runtime->boundary;
1980 runtime->control->appl_ptr = appl_ptr;
1981 if (substream->ops->ack)
1982 substream->ops->ack(substream);
1989 snd_pcm_stream_unlock_irq(substream);
1991 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1994 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1996 struct snd_pcm_runtime *runtime;
2000 err = pcm_sanity_check(substream);
2003 runtime = substream->runtime;
2004 nonblock = !!(substream->f_flags & O_NONBLOCK);
2005 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2007 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2010 EXPORT_SYMBOL(snd_pcm_lib_read);
2012 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2014 unsigned long data, unsigned int off,
2015 snd_pcm_uframes_t frames)
2017 struct snd_pcm_runtime *runtime = substream->runtime;
2019 void __user **bufs = (void __user **)data;
2020 int channels = runtime->channels;
2022 if (substream->ops->copy) {
2023 for (c = 0; c < channels; ++c, ++bufs) {
2027 buf = *bufs + samples_to_bytes(runtime, off);
2028 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2032 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2033 for (c = 0; c < channels; ++c, ++bufs) {
2039 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2040 buf = *bufs + samples_to_bytes(runtime, off);
2041 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2048 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2050 snd_pcm_uframes_t frames)
2052 struct snd_pcm_runtime *runtime;
2056 err = pcm_sanity_check(substream);
2059 runtime = substream->runtime;
2060 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2063 nonblock = !!(substream->f_flags & O_NONBLOCK);
2064 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2066 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2069 EXPORT_SYMBOL(snd_pcm_lib_readv);