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 <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/info.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/timer.h>
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
43 struct snd_pcm_runtime *runtime = substream->runtime;
44 snd_pcm_uframes_t frames, ofs, transfer;
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
58 if (runtime->silence_filled >= runtime->buffer_size)
60 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
81 runtime->silence_start = new_hw_ptr;
83 runtime->silence_start = ofs;
86 frames = runtime->buffer_size - runtime->silence_filled;
88 if (snd_BUG_ON(frames > runtime->buffer_size))
92 ofs = runtime->silence_start % runtime->buffer_size;
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
99 err = substream->ops->silence(substream, -1, ofs, transfer);
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
111 err = substream->ops->silence(substream, c, ofs, transfer);
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 runtime->silence_filled += transfer;
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129 #define xrun_debug(substream) ((substream)->pstr->xrun_debug)
131 #define xrun_debug(substream) 0
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
139 static void xrun(struct snd_pcm_substream *substream)
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
142 if (xrun_debug(substream)) {
143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
147 dump_stack_on_xrun(substream);
151 static snd_pcm_uframes_t
152 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
155 snd_pcm_uframes_t pos;
157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
162 if (pos >= runtime->buffer_size) {
163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
171 pos -= pos % runtime->min_align;
175 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
178 snd_pcm_uframes_t avail;
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
198 #define hw_ptr_error(substream, fmt, args...) \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
202 snd_printd("PCM: " fmt, ##args); \
204 dump_stack_on_xrun(substream); \
208 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 snd_pcm_uframes_t pos;
212 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
213 snd_pcm_sframes_t delta;
215 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
216 if (pos == SNDRV_PCM_POS_XRUN) {
220 hw_base = runtime->hw_ptr_base;
221 new_hw_ptr = hw_base + pos;
222 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
223 delta = new_hw_ptr - hw_ptr_interrupt;
224 if (hw_ptr_interrupt >= runtime->boundary) {
225 hw_ptr_interrupt %= runtime->boundary;
226 if (!hw_base) /* hw_base was already lapped; recalc delta */
227 delta = new_hw_ptr - hw_ptr_interrupt;
230 delta += runtime->buffer_size;
232 hw_ptr_error(substream,
233 "Unexpected hw_pointer value "
234 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
235 substream->stream, (long)pos,
236 (long)hw_ptr_interrupt);
237 /* rebase to interrupt position */
238 hw_base = new_hw_ptr = hw_ptr_interrupt;
239 /* align hw_base to buffer_size */
240 hw_base -= hw_base % runtime->buffer_size;
243 hw_base += runtime->buffer_size;
244 if (hw_base == runtime->boundary)
246 new_hw_ptr = hw_base + pos;
249 if (delta > runtime->period_size) {
250 hw_ptr_error(substream,
252 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
253 substream->stream, (long)delta,
254 (long)hw_ptr_interrupt);
255 /* rebase hw_ptr_interrupt */
257 new_hw_ptr - new_hw_ptr % runtime->period_size;
259 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
260 runtime->silence_size > 0)
261 snd_pcm_playback_silence(substream, new_hw_ptr);
263 runtime->hw_ptr_base = hw_base;
264 runtime->status->hw_ptr = new_hw_ptr;
265 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
267 return snd_pcm_update_hw_ptr_post(substream, runtime);
270 /* CAUTION: call it with irq disabled */
271 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
273 struct snd_pcm_runtime *runtime = substream->runtime;
274 snd_pcm_uframes_t pos;
275 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
276 snd_pcm_sframes_t delta;
278 old_hw_ptr = runtime->status->hw_ptr;
279 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
280 if (pos == SNDRV_PCM_POS_XRUN) {
284 hw_base = runtime->hw_ptr_base;
285 new_hw_ptr = hw_base + pos;
287 delta = new_hw_ptr - old_hw_ptr;
289 delta += runtime->buffer_size;
291 hw_ptr_error(substream,
292 "Unexpected hw_pointer value [2] "
293 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
294 substream->stream, (long)pos,
298 hw_base += runtime->buffer_size;
299 if (hw_base == runtime->boundary)
301 new_hw_ptr = hw_base + pos;
303 if (delta > runtime->period_size && runtime->periods > 1) {
304 hw_ptr_error(substream,
306 "(pos=%ld, delta=%ld, period=%ld)\n",
307 (long)pos, (long)delta,
308 (long)runtime->period_size);
311 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
312 runtime->silence_size > 0)
313 snd_pcm_playback_silence(substream, new_hw_ptr);
315 runtime->hw_ptr_base = hw_base;
316 runtime->status->hw_ptr = new_hw_ptr;
318 return snd_pcm_update_hw_ptr_post(substream, runtime);
322 * snd_pcm_set_ops - set the PCM operators
323 * @pcm: the pcm instance
324 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
325 * @ops: the operator table
327 * Sets the given PCM operators to the pcm instance.
329 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
331 struct snd_pcm_str *stream = &pcm->streams[direction];
332 struct snd_pcm_substream *substream;
334 for (substream = stream->substream; substream != NULL; substream = substream->next)
335 substream->ops = ops;
338 EXPORT_SYMBOL(snd_pcm_set_ops);
341 * snd_pcm_sync - set the PCM sync id
342 * @substream: the pcm substream
344 * Sets the PCM sync identifier for the card.
346 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
348 struct snd_pcm_runtime *runtime = substream->runtime;
350 runtime->sync.id32[0] = substream->pcm->card->number;
351 runtime->sync.id32[1] = -1;
352 runtime->sync.id32[2] = -1;
353 runtime->sync.id32[3] = -1;
356 EXPORT_SYMBOL(snd_pcm_set_sync);
359 * Standard ioctl routine
362 static inline unsigned int div32(unsigned int a, unsigned int b,
373 static inline unsigned int div_down(unsigned int a, unsigned int b)
380 static inline unsigned int div_up(unsigned int a, unsigned int b)
392 static inline unsigned int mul(unsigned int a, unsigned int b)
396 if (div_down(UINT_MAX, a) < b)
401 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
402 unsigned int c, unsigned int *r)
404 u_int64_t n = (u_int64_t) a * b;
419 * snd_interval_refine - refine the interval value of configurator
420 * @i: the interval value to refine
421 * @v: the interval value to refer to
423 * Refines the interval value with the reference value.
424 * The interval is changed to the range satisfying both intervals.
425 * The interval status (min, max, integer, etc.) are evaluated.
427 * Returns non-zero if the value is changed, zero if not changed.
429 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
432 if (snd_BUG_ON(snd_interval_empty(i)))
434 if (i->min < v->min) {
436 i->openmin = v->openmin;
438 } else if (i->min == v->min && !i->openmin && v->openmin) {
442 if (i->max > v->max) {
444 i->openmax = v->openmax;
446 } else if (i->max == v->max && !i->openmax && v->openmax) {
450 if (!i->integer && v->integer) {
463 } else if (!i->openmin && !i->openmax && i->min == i->max)
465 if (snd_interval_checkempty(i)) {
466 snd_interval_none(i);
472 EXPORT_SYMBOL(snd_interval_refine);
474 static int snd_interval_refine_first(struct snd_interval *i)
476 if (snd_BUG_ON(snd_interval_empty(i)))
478 if (snd_interval_single(i))
481 i->openmax = i->openmin;
487 static int snd_interval_refine_last(struct snd_interval *i)
489 if (snd_BUG_ON(snd_interval_empty(i)))
491 if (snd_interval_single(i))
494 i->openmin = i->openmax;
500 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
502 if (a->empty || b->empty) {
503 snd_interval_none(c);
507 c->min = mul(a->min, b->min);
508 c->openmin = (a->openmin || b->openmin);
509 c->max = mul(a->max, b->max);
510 c->openmax = (a->openmax || b->openmax);
511 c->integer = (a->integer && b->integer);
515 * snd_interval_div - refine the interval value with division
522 * Returns non-zero if the value is changed, zero if not changed.
524 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
527 if (a->empty || b->empty) {
528 snd_interval_none(c);
532 c->min = div32(a->min, b->max, &r);
533 c->openmin = (r || a->openmin || b->openmax);
535 c->max = div32(a->max, b->min, &r);
540 c->openmax = (a->openmax || b->openmin);
549 * snd_interval_muldivk - refine the interval value
552 * @k: divisor (as integer)
557 * Returns non-zero if the value is changed, zero if not changed.
559 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
560 unsigned int k, struct snd_interval *c)
563 if (a->empty || b->empty) {
564 snd_interval_none(c);
568 c->min = muldiv32(a->min, b->min, k, &r);
569 c->openmin = (r || a->openmin || b->openmin);
570 c->max = muldiv32(a->max, b->max, k, &r);
575 c->openmax = (a->openmax || b->openmax);
580 * snd_interval_mulkdiv - refine the interval value
582 * @k: dividend 2 (as integer)
588 * Returns non-zero if the value is changed, zero if not changed.
590 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
591 const struct snd_interval *b, struct snd_interval *c)
594 if (a->empty || b->empty) {
595 snd_interval_none(c);
599 c->min = muldiv32(a->min, k, b->max, &r);
600 c->openmin = (r || a->openmin || b->openmax);
602 c->max = muldiv32(a->max, k, b->min, &r);
607 c->openmax = (a->openmax || b->openmin);
619 * snd_interval_ratnum - refine the interval value
620 * @i: interval to refine
621 * @rats_count: number of ratnum_t
622 * @rats: ratnum_t array
623 * @nump: pointer to store the resultant numerator
624 * @denp: pointer to store the resultant denominator
626 * Returns non-zero if the value is changed, zero if not changed.
628 int snd_interval_ratnum(struct snd_interval *i,
629 unsigned int rats_count, struct snd_ratnum *rats,
630 unsigned int *nump, unsigned int *denp)
632 unsigned int best_num, best_diff, best_den;
634 struct snd_interval t;
637 best_num = best_den = best_diff = 0;
638 for (k = 0; k < rats_count; ++k) {
639 unsigned int num = rats[k].num;
641 unsigned int q = i->min;
645 den = div_down(num, q);
646 if (den < rats[k].den_min)
648 if (den > rats[k].den_max)
649 den = rats[k].den_max;
652 r = (den - rats[k].den_min) % rats[k].den_step;
656 diff = num - q * den;
658 diff * best_den < best_diff * den) {
668 t.min = div_down(best_num, best_den);
669 t.openmin = !!(best_num % best_den);
671 best_num = best_den = best_diff = 0;
672 for (k = 0; k < rats_count; ++k) {
673 unsigned int num = rats[k].num;
675 unsigned int q = i->max;
681 den = div_up(num, q);
682 if (den > rats[k].den_max)
684 if (den < rats[k].den_min)
685 den = rats[k].den_min;
688 r = (den - rats[k].den_min) % rats[k].den_step;
690 den += rats[k].den_step - r;
692 diff = q * den - num;
694 diff * best_den < best_diff * den) {
704 t.max = div_up(best_num, best_den);
705 t.openmax = !!(best_num % best_den);
707 err = snd_interval_refine(i, &t);
711 if (snd_interval_single(i)) {
720 EXPORT_SYMBOL(snd_interval_ratnum);
723 * snd_interval_ratden - refine the interval value
724 * @i: interval to refine
725 * @rats_count: number of struct ratden
726 * @rats: struct ratden array
727 * @nump: pointer to store the resultant numerator
728 * @denp: pointer to store the resultant denominator
730 * Returns non-zero if the value is changed, zero if not changed.
732 static int snd_interval_ratden(struct snd_interval *i,
733 unsigned int rats_count, struct snd_ratden *rats,
734 unsigned int *nump, unsigned int *denp)
736 unsigned int best_num, best_diff, best_den;
738 struct snd_interval t;
741 best_num = best_den = best_diff = 0;
742 for (k = 0; k < rats_count; ++k) {
744 unsigned int den = rats[k].den;
745 unsigned int q = i->min;
748 if (num > rats[k].num_max)
750 if (num < rats[k].num_min)
751 num = rats[k].num_max;
754 r = (num - rats[k].num_min) % rats[k].num_step;
756 num += rats[k].num_step - r;
758 diff = num - q * den;
760 diff * best_den < best_diff * den) {
770 t.min = div_down(best_num, best_den);
771 t.openmin = !!(best_num % best_den);
773 best_num = best_den = best_diff = 0;
774 for (k = 0; k < rats_count; ++k) {
776 unsigned int den = rats[k].den;
777 unsigned int q = i->max;
780 if (num < rats[k].num_min)
782 if (num > rats[k].num_max)
783 num = rats[k].num_max;
786 r = (num - rats[k].num_min) % rats[k].num_step;
790 diff = q * den - num;
792 diff * best_den < best_diff * den) {
802 t.max = div_up(best_num, best_den);
803 t.openmax = !!(best_num % best_den);
805 err = snd_interval_refine(i, &t);
809 if (snd_interval_single(i)) {
819 * snd_interval_list - refine the interval value from the list
820 * @i: the interval value to refine
821 * @count: the number of elements in the list
822 * @list: the value list
823 * @mask: the bit-mask to evaluate
825 * Refines the interval value from the list.
826 * When mask is non-zero, only the elements corresponding to bit 1 are
829 * Returns non-zero if the value is changed, zero if not changed.
831 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
840 for (k = 0; k < count; k++) {
841 if (mask && !(mask & (1 << k)))
843 if (i->min == list[k] && !i->openmin)
845 if (i->min < list[k]) {
855 for (k = count; k-- > 0;) {
856 if (mask && !(mask & (1 << k)))
858 if (i->max == list[k] && !i->openmax)
860 if (i->max > list[k]) {
870 if (snd_interval_checkempty(i)) {
877 EXPORT_SYMBOL(snd_interval_list);
879 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
883 n = (i->min - min) % step;
884 if (n != 0 || i->openmin) {
888 n = (i->max - min) % step;
889 if (n != 0 || i->openmax) {
893 if (snd_interval_checkempty(i)) {
900 /* Info constraints helpers */
903 * snd_pcm_hw_rule_add - add the hw-constraint rule
904 * @runtime: the pcm runtime instance
905 * @cond: condition bits
906 * @var: the variable to evaluate
907 * @func: the evaluation function
908 * @private: the private data pointer passed to function
909 * @dep: the dependent variables
911 * Returns zero if successful, or a negative error code on failure.
913 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
915 snd_pcm_hw_rule_func_t func, void *private,
918 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
919 struct snd_pcm_hw_rule *c;
923 if (constrs->rules_num >= constrs->rules_all) {
924 struct snd_pcm_hw_rule *new;
925 unsigned int new_rules = constrs->rules_all + 16;
926 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
929 if (constrs->rules) {
930 memcpy(new, constrs->rules,
931 constrs->rules_num * sizeof(*c));
932 kfree(constrs->rules);
934 constrs->rules = new;
935 constrs->rules_all = new_rules;
937 c = &constrs->rules[constrs->rules_num];
941 c->private = private;
944 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
949 dep = va_arg(args, int);
951 constrs->rules_num++;
956 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
959 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
960 * @runtime: PCM runtime instance
961 * @var: hw_params variable to apply the mask
962 * @mask: the bitmap mask
964 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
966 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
969 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
970 struct snd_mask *maskp = constrs_mask(constrs, var);
971 *maskp->bits &= mask;
972 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
973 if (*maskp->bits == 0)
979 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
980 * @runtime: PCM runtime instance
981 * @var: hw_params variable to apply the mask
982 * @mask: the 64bit bitmap mask
984 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
986 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
989 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
990 struct snd_mask *maskp = constrs_mask(constrs, var);
991 maskp->bits[0] &= (u_int32_t)mask;
992 maskp->bits[1] &= (u_int32_t)(mask >> 32);
993 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
994 if (! maskp->bits[0] && ! maskp->bits[1])
1000 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1001 * @runtime: PCM runtime instance
1002 * @var: hw_params variable to apply the integer constraint
1004 * Apply the constraint of integer to an interval parameter.
1006 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1008 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1009 return snd_interval_setinteger(constrs_interval(constrs, var));
1012 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1015 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1016 * @runtime: PCM runtime instance
1017 * @var: hw_params variable to apply the range
1018 * @min: the minimal value
1019 * @max: the maximal value
1021 * Apply the min/max range constraint to an interval parameter.
1023 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1024 unsigned int min, unsigned int max)
1026 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1027 struct snd_interval t;
1030 t.openmin = t.openmax = 0;
1032 return snd_interval_refine(constrs_interval(constrs, var), &t);
1035 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1037 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1038 struct snd_pcm_hw_rule *rule)
1040 struct snd_pcm_hw_constraint_list *list = rule->private;
1041 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1046 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1047 * @runtime: PCM runtime instance
1048 * @cond: condition bits
1049 * @var: hw_params variable to apply the list constraint
1052 * Apply the list of constraints to an interval parameter.
1054 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1056 snd_pcm_hw_param_t var,
1057 struct snd_pcm_hw_constraint_list *l)
1059 return snd_pcm_hw_rule_add(runtime, cond, var,
1060 snd_pcm_hw_rule_list, l,
1064 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1066 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1067 struct snd_pcm_hw_rule *rule)
1069 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1070 unsigned int num = 0, den = 0;
1072 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1073 r->nrats, r->rats, &num, &den);
1074 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1075 params->rate_num = num;
1076 params->rate_den = den;
1082 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1083 * @runtime: PCM runtime instance
1084 * @cond: condition bits
1085 * @var: hw_params variable to apply the ratnums constraint
1086 * @r: struct snd_ratnums constriants
1088 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1090 snd_pcm_hw_param_t var,
1091 struct snd_pcm_hw_constraint_ratnums *r)
1093 return snd_pcm_hw_rule_add(runtime, cond, var,
1094 snd_pcm_hw_rule_ratnums, r,
1098 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1100 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1101 struct snd_pcm_hw_rule *rule)
1103 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1104 unsigned int num = 0, den = 0;
1105 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1106 r->nrats, r->rats, &num, &den);
1107 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1108 params->rate_num = num;
1109 params->rate_den = den;
1115 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1116 * @runtime: PCM runtime instance
1117 * @cond: condition bits
1118 * @var: hw_params variable to apply the ratdens constraint
1119 * @r: struct snd_ratdens constriants
1121 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1123 snd_pcm_hw_param_t var,
1124 struct snd_pcm_hw_constraint_ratdens *r)
1126 return snd_pcm_hw_rule_add(runtime, cond, var,
1127 snd_pcm_hw_rule_ratdens, r,
1131 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1133 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1134 struct snd_pcm_hw_rule *rule)
1136 unsigned int l = (unsigned long) rule->private;
1137 int width = l & 0xffff;
1138 unsigned int msbits = l >> 16;
1139 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1140 if (snd_interval_single(i) && snd_interval_value(i) == width)
1141 params->msbits = msbits;
1146 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @width: sample bits width
1150 * @msbits: msbits width
1152 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1155 unsigned int msbits)
1157 unsigned long l = (msbits << 16) | width;
1158 return snd_pcm_hw_rule_add(runtime, cond, -1,
1159 snd_pcm_hw_rule_msbits,
1161 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1164 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1166 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
1169 unsigned long step = (unsigned long) rule->private;
1170 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1174 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1175 * @runtime: PCM runtime instance
1176 * @cond: condition bits
1177 * @var: hw_params variable to apply the step constraint
1180 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1182 snd_pcm_hw_param_t var,
1185 return snd_pcm_hw_rule_add(runtime, cond, var,
1186 snd_pcm_hw_rule_step, (void *) step,
1190 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1192 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1194 static unsigned int pow2_sizes[] = {
1195 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1196 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1197 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1198 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1200 return snd_interval_list(hw_param_interval(params, rule->var),
1201 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1205 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1206 * @runtime: PCM runtime instance
1207 * @cond: condition bits
1208 * @var: hw_params variable to apply the power-of-2 constraint
1210 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1212 snd_pcm_hw_param_t var)
1214 return snd_pcm_hw_rule_add(runtime, cond, var,
1215 snd_pcm_hw_rule_pow2, NULL,
1219 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1221 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1222 snd_pcm_hw_param_t var)
1224 if (hw_is_mask(var)) {
1225 snd_mask_any(hw_param_mask(params, var));
1226 params->cmask |= 1 << var;
1227 params->rmask |= 1 << var;
1230 if (hw_is_interval(var)) {
1231 snd_interval_any(hw_param_interval(params, var));
1232 params->cmask |= 1 << var;
1233 params->rmask |= 1 << var;
1239 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1242 memset(params, 0, sizeof(*params));
1243 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1244 _snd_pcm_hw_param_any(params, k);
1245 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1246 _snd_pcm_hw_param_any(params, k);
1250 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1253 * snd_pcm_hw_param_value - return @params field @var value
1254 * @params: the hw_params instance
1255 * @var: parameter to retrieve
1256 * @dir: pointer to the direction (-1,0,1) or %NULL
1258 * Return the value for field @var if it's fixed in configuration space
1259 * defined by @params. Return -%EINVAL otherwise.
1261 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1262 snd_pcm_hw_param_t var, int *dir)
1264 if (hw_is_mask(var)) {
1265 const struct snd_mask *mask = hw_param_mask_c(params, var);
1266 if (!snd_mask_single(mask))
1270 return snd_mask_value(mask);
1272 if (hw_is_interval(var)) {
1273 const struct snd_interval *i = hw_param_interval_c(params, var);
1274 if (!snd_interval_single(i))
1278 return snd_interval_value(i);
1283 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1285 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1286 snd_pcm_hw_param_t var)
1288 if (hw_is_mask(var)) {
1289 snd_mask_none(hw_param_mask(params, var));
1290 params->cmask |= 1 << var;
1291 params->rmask |= 1 << var;
1292 } else if (hw_is_interval(var)) {
1293 snd_interval_none(hw_param_interval(params, var));
1294 params->cmask |= 1 << var;
1295 params->rmask |= 1 << var;
1301 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1303 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1304 snd_pcm_hw_param_t var)
1307 if (hw_is_mask(var))
1308 changed = snd_mask_refine_first(hw_param_mask(params, var));
1309 else if (hw_is_interval(var))
1310 changed = snd_interval_refine_first(hw_param_interval(params, var));
1314 params->cmask |= 1 << var;
1315 params->rmask |= 1 << var;
1322 * snd_pcm_hw_param_first - refine config space and return minimum value
1323 * @pcm: PCM instance
1324 * @params: the hw_params instance
1325 * @var: parameter to retrieve
1326 * @dir: pointer to the direction (-1,0,1) or %NULL
1328 * Inside configuration space defined by @params remove from @var all
1329 * values > minimum. Reduce configuration space accordingly.
1330 * Return the minimum.
1332 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1333 struct snd_pcm_hw_params *params,
1334 snd_pcm_hw_param_t var, int *dir)
1336 int changed = _snd_pcm_hw_param_first(params, var);
1339 if (params->rmask) {
1340 int err = snd_pcm_hw_refine(pcm, params);
1341 if (snd_BUG_ON(err < 0))
1344 return snd_pcm_hw_param_value(params, var, dir);
1347 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1349 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1350 snd_pcm_hw_param_t var)
1353 if (hw_is_mask(var))
1354 changed = snd_mask_refine_last(hw_param_mask(params, var));
1355 else if (hw_is_interval(var))
1356 changed = snd_interval_refine_last(hw_param_interval(params, var));
1360 params->cmask |= 1 << var;
1361 params->rmask |= 1 << var;
1368 * snd_pcm_hw_param_last - refine config space and return maximum 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 < maximum. Reduce configuration space accordingly.
1376 * Return the maximum.
1378 int snd_pcm_hw_param_last(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_last(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_last);
1396 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1397 * @pcm: PCM instance
1398 * @params: the hw_params instance
1400 * Choose one configuration from configuration space defined by @params.
1401 * The configuration chosen is that obtained fixing in this order:
1402 * first access, first format, first subformat, min channels,
1403 * min rate, min period time, max buffer size, min tick time
1405 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1406 struct snd_pcm_hw_params *params)
1408 static int vars[] = {
1409 SNDRV_PCM_HW_PARAM_ACCESS,
1410 SNDRV_PCM_HW_PARAM_FORMAT,
1411 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1412 SNDRV_PCM_HW_PARAM_CHANNELS,
1413 SNDRV_PCM_HW_PARAM_RATE,
1414 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1415 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1416 SNDRV_PCM_HW_PARAM_TICK_TIME,
1421 for (v = vars; *v != -1; v++) {
1422 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1423 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1425 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1426 if (snd_BUG_ON(err < 0))
1432 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1435 struct snd_pcm_runtime *runtime = substream->runtime;
1436 unsigned long flags;
1437 snd_pcm_stream_lock_irqsave(substream, flags);
1438 if (snd_pcm_running(substream) &&
1439 snd_pcm_update_hw_ptr(substream) >= 0)
1440 runtime->status->hw_ptr %= runtime->buffer_size;
1442 runtime->status->hw_ptr = 0;
1443 snd_pcm_stream_unlock_irqrestore(substream, flags);
1447 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1450 struct snd_pcm_channel_info *info = arg;
1451 struct snd_pcm_runtime *runtime = substream->runtime;
1453 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1457 width = snd_pcm_format_physical_width(runtime->format);
1461 switch (runtime->access) {
1462 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1463 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1464 info->first = info->channel * width;
1465 info->step = runtime->channels * width;
1467 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1468 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1470 size_t size = runtime->dma_bytes / runtime->channels;
1471 info->first = info->channel * size * 8;
1483 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1484 * @substream: the pcm substream instance
1485 * @cmd: ioctl command
1486 * @arg: ioctl argument
1488 * Processes the generic ioctl commands for PCM.
1489 * Can be passed as the ioctl callback for PCM ops.
1491 * Returns zero if successful, or a negative error code on failure.
1493 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1494 unsigned int cmd, void *arg)
1497 case SNDRV_PCM_IOCTL1_INFO:
1499 case SNDRV_PCM_IOCTL1_RESET:
1500 return snd_pcm_lib_ioctl_reset(substream, arg);
1501 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1502 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1507 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1510 * snd_pcm_period_elapsed - update the pcm status for the next period
1511 * @substream: the pcm substream instance
1513 * This function is called from the interrupt handler when the
1514 * PCM has processed the period size. It will update the current
1515 * pointer, wake up sleepers, etc.
1517 * Even if more than one periods have elapsed since the last call, you
1518 * have to call this only once.
1520 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1522 struct snd_pcm_runtime *runtime;
1523 unsigned long flags;
1525 if (PCM_RUNTIME_CHECK(substream))
1527 runtime = substream->runtime;
1529 if (runtime->transfer_ack_begin)
1530 runtime->transfer_ack_begin(substream);
1532 snd_pcm_stream_lock_irqsave(substream, flags);
1533 if (!snd_pcm_running(substream) ||
1534 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1537 if (substream->timer_running)
1538 snd_timer_interrupt(substream->timer, 1);
1540 snd_pcm_stream_unlock_irqrestore(substream, flags);
1541 if (runtime->transfer_ack_end)
1542 runtime->transfer_ack_end(substream);
1543 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1546 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1549 * Wait until avail_min data becomes available
1550 * Returns a negative error code if any error occurs during operation.
1551 * The available space is stored on availp. When err = 0 and avail = 0
1552 * on the capture stream, it indicates the stream is in DRAINING state.
1554 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1555 snd_pcm_uframes_t *availp)
1557 struct snd_pcm_runtime *runtime = substream->runtime;
1558 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1561 snd_pcm_uframes_t avail = 0;
1564 init_waitqueue_entry(&wait, current);
1565 add_wait_queue(&runtime->sleep, &wait);
1567 if (signal_pending(current)) {
1571 set_current_state(TASK_INTERRUPTIBLE);
1572 snd_pcm_stream_unlock_irq(substream);
1573 tout = schedule_timeout(msecs_to_jiffies(10000));
1574 snd_pcm_stream_lock_irq(substream);
1575 switch (runtime->status->state) {
1576 case SNDRV_PCM_STATE_SUSPENDED:
1579 case SNDRV_PCM_STATE_XRUN:
1582 case SNDRV_PCM_STATE_DRAINING:
1586 avail = 0; /* indicate draining */
1588 case SNDRV_PCM_STATE_OPEN:
1589 case SNDRV_PCM_STATE_SETUP:
1590 case SNDRV_PCM_STATE_DISCONNECTED:
1595 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1596 is_playback ? "playback" : "capture");
1601 avail = snd_pcm_playback_avail(runtime);
1603 avail = snd_pcm_capture_avail(runtime);
1604 if (avail >= runtime->control->avail_min)
1608 remove_wait_queue(&runtime->sleep, &wait);
1613 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1615 unsigned long data, unsigned int off,
1616 snd_pcm_uframes_t frames)
1618 struct snd_pcm_runtime *runtime = substream->runtime;
1620 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1621 if (substream->ops->copy) {
1622 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1625 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1626 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1632 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1633 unsigned long data, unsigned int off,
1634 snd_pcm_uframes_t size);
1636 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1638 snd_pcm_uframes_t size,
1640 transfer_f transfer)
1642 struct snd_pcm_runtime *runtime = substream->runtime;
1643 snd_pcm_uframes_t xfer = 0;
1644 snd_pcm_uframes_t offset = 0;
1650 snd_pcm_stream_lock_irq(substream);
1651 switch (runtime->status->state) {
1652 case SNDRV_PCM_STATE_PREPARED:
1653 case SNDRV_PCM_STATE_RUNNING:
1654 case SNDRV_PCM_STATE_PAUSED:
1656 case SNDRV_PCM_STATE_XRUN:
1659 case SNDRV_PCM_STATE_SUSPENDED:
1668 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1669 snd_pcm_uframes_t avail;
1670 snd_pcm_uframes_t cont;
1671 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1672 snd_pcm_update_hw_ptr(substream);
1673 avail = snd_pcm_playback_avail(runtime);
1679 err = wait_for_avail_min(substream, &avail);
1683 frames = size > avail ? avail : size;
1684 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1687 if (snd_BUG_ON(!frames)) {
1688 snd_pcm_stream_unlock_irq(substream);
1691 appl_ptr = runtime->control->appl_ptr;
1692 appl_ofs = appl_ptr % runtime->buffer_size;
1693 snd_pcm_stream_unlock_irq(substream);
1694 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1696 snd_pcm_stream_lock_irq(substream);
1697 switch (runtime->status->state) {
1698 case SNDRV_PCM_STATE_XRUN:
1701 case SNDRV_PCM_STATE_SUSPENDED:
1708 if (appl_ptr >= runtime->boundary)
1709 appl_ptr -= runtime->boundary;
1710 runtime->control->appl_ptr = appl_ptr;
1711 if (substream->ops->ack)
1712 substream->ops->ack(substream);
1717 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1718 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1719 err = snd_pcm_start(substream);
1725 snd_pcm_stream_unlock_irq(substream);
1727 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1730 /* sanity-check for read/write methods */
1731 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1733 struct snd_pcm_runtime *runtime;
1734 if (PCM_RUNTIME_CHECK(substream))
1736 runtime = substream->runtime;
1737 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1739 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1744 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1746 struct snd_pcm_runtime *runtime;
1750 err = pcm_sanity_check(substream);
1753 runtime = substream->runtime;
1754 nonblock = !!(substream->f_flags & O_NONBLOCK);
1756 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1757 runtime->channels > 1)
1759 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1760 snd_pcm_lib_write_transfer);
1763 EXPORT_SYMBOL(snd_pcm_lib_write);
1765 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1767 unsigned long data, unsigned int off,
1768 snd_pcm_uframes_t frames)
1770 struct snd_pcm_runtime *runtime = substream->runtime;
1772 void __user **bufs = (void __user **)data;
1773 int channels = runtime->channels;
1775 if (substream->ops->copy) {
1776 if (snd_BUG_ON(!substream->ops->silence))
1778 for (c = 0; c < channels; ++c, ++bufs) {
1779 if (*bufs == NULL) {
1780 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1783 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1784 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1789 /* default transfer behaviour */
1790 size_t dma_csize = runtime->dma_bytes / channels;
1791 for (c = 0; c < channels; ++c, ++bufs) {
1792 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1793 if (*bufs == NULL) {
1794 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1796 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1797 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1805 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1807 snd_pcm_uframes_t frames)
1809 struct snd_pcm_runtime *runtime;
1813 err = pcm_sanity_check(substream);
1816 runtime = substream->runtime;
1817 nonblock = !!(substream->f_flags & O_NONBLOCK);
1819 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1821 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1822 nonblock, snd_pcm_lib_writev_transfer);
1825 EXPORT_SYMBOL(snd_pcm_lib_writev);
1827 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1829 unsigned long data, unsigned int off,
1830 snd_pcm_uframes_t frames)
1832 struct snd_pcm_runtime *runtime = substream->runtime;
1834 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1835 if (substream->ops->copy) {
1836 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1839 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1840 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1846 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1848 snd_pcm_uframes_t size,
1850 transfer_f transfer)
1852 struct snd_pcm_runtime *runtime = substream->runtime;
1853 snd_pcm_uframes_t xfer = 0;
1854 snd_pcm_uframes_t offset = 0;
1860 snd_pcm_stream_lock_irq(substream);
1861 switch (runtime->status->state) {
1862 case SNDRV_PCM_STATE_PREPARED:
1863 if (size >= runtime->start_threshold) {
1864 err = snd_pcm_start(substream);
1869 case SNDRV_PCM_STATE_DRAINING:
1870 case SNDRV_PCM_STATE_RUNNING:
1871 case SNDRV_PCM_STATE_PAUSED:
1873 case SNDRV_PCM_STATE_XRUN:
1876 case SNDRV_PCM_STATE_SUSPENDED:
1885 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1886 snd_pcm_uframes_t avail;
1887 snd_pcm_uframes_t cont;
1888 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1889 snd_pcm_update_hw_ptr(substream);
1890 avail = snd_pcm_capture_avail(runtime);
1892 if (runtime->status->state ==
1893 SNDRV_PCM_STATE_DRAINING) {
1894 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1901 err = wait_for_avail_min(substream, &avail);
1905 continue; /* draining */
1907 frames = size > avail ? avail : size;
1908 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1911 if (snd_BUG_ON(!frames)) {
1912 snd_pcm_stream_unlock_irq(substream);
1915 appl_ptr = runtime->control->appl_ptr;
1916 appl_ofs = appl_ptr % runtime->buffer_size;
1917 snd_pcm_stream_unlock_irq(substream);
1918 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1920 snd_pcm_stream_lock_irq(substream);
1921 switch (runtime->status->state) {
1922 case SNDRV_PCM_STATE_XRUN:
1925 case SNDRV_PCM_STATE_SUSPENDED:
1932 if (appl_ptr >= runtime->boundary)
1933 appl_ptr -= runtime->boundary;
1934 runtime->control->appl_ptr = appl_ptr;
1935 if (substream->ops->ack)
1936 substream->ops->ack(substream);
1943 snd_pcm_stream_unlock_irq(substream);
1945 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1948 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1950 struct snd_pcm_runtime *runtime;
1954 err = pcm_sanity_check(substream);
1957 runtime = substream->runtime;
1958 nonblock = !!(substream->f_flags & O_NONBLOCK);
1959 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1961 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1964 EXPORT_SYMBOL(snd_pcm_lib_read);
1966 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1968 unsigned long data, unsigned int off,
1969 snd_pcm_uframes_t frames)
1971 struct snd_pcm_runtime *runtime = substream->runtime;
1973 void __user **bufs = (void __user **)data;
1974 int channels = runtime->channels;
1976 if (substream->ops->copy) {
1977 for (c = 0; c < channels; ++c, ++bufs) {
1981 buf = *bufs + samples_to_bytes(runtime, off);
1982 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1986 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1987 for (c = 0; c < channels; ++c, ++bufs) {
1993 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1994 buf = *bufs + samples_to_bytes(runtime, off);
1995 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2002 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2004 snd_pcm_uframes_t frames)
2006 struct snd_pcm_runtime *runtime;
2010 err = pcm_sanity_check(substream);
2013 runtime = substream->runtime;
2014 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2017 nonblock = !!(substream->f_flags & O_NONBLOCK);
2018 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2020 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2023 EXPORT_SYMBOL(snd_pcm_lib_readv);