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 < runtime->boundary / 2)
227 /* hw_base was already lapped; recalc delta */
228 delta = new_hw_ptr - hw_ptr_interrupt;
231 delta += runtime->buffer_size;
233 hw_ptr_error(substream,
234 "Unexpected hw_pointer value "
235 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
236 substream->stream, (long)pos,
237 (long)hw_ptr_interrupt);
238 /* rebase to interrupt position */
239 hw_base = new_hw_ptr = hw_ptr_interrupt;
240 /* align hw_base to buffer_size */
241 hw_base -= hw_base % runtime->buffer_size;
244 hw_base += runtime->buffer_size;
245 if (hw_base >= runtime->boundary)
247 new_hw_ptr = hw_base + pos;
250 if (delta > runtime->period_size) {
251 hw_ptr_error(substream,
253 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
254 substream->stream, (long)delta,
255 (long)hw_ptr_interrupt);
256 /* rebase hw_ptr_interrupt */
258 new_hw_ptr - new_hw_ptr % runtime->period_size;
260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
261 runtime->silence_size > 0)
262 snd_pcm_playback_silence(substream, new_hw_ptr);
264 runtime->hw_ptr_base = hw_base;
265 runtime->status->hw_ptr = new_hw_ptr;
266 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
268 return snd_pcm_update_hw_ptr_post(substream, runtime);
271 /* CAUTION: call it with irq disabled */
272 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 snd_pcm_uframes_t pos;
276 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
277 snd_pcm_sframes_t delta;
279 old_hw_ptr = runtime->status->hw_ptr;
280 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
281 if (pos == SNDRV_PCM_POS_XRUN) {
285 hw_base = runtime->hw_ptr_base;
286 new_hw_ptr = hw_base + pos;
288 delta = new_hw_ptr - old_hw_ptr;
290 delta += runtime->buffer_size;
292 hw_ptr_error(substream,
293 "Unexpected hw_pointer value [2] "
294 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
295 substream->stream, (long)pos,
299 hw_base += runtime->buffer_size;
300 if (hw_base >= runtime->boundary)
302 new_hw_ptr = hw_base + pos;
304 if (delta > runtime->period_size && runtime->periods > 1) {
305 hw_ptr_error(substream,
307 "(pos=%ld, delta=%ld, period=%ld)\n",
308 (long)pos, (long)delta,
309 (long)runtime->period_size);
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
313 runtime->silence_size > 0)
314 snd_pcm_playback_silence(substream, new_hw_ptr);
316 runtime->hw_ptr_base = hw_base;
317 runtime->status->hw_ptr = new_hw_ptr;
319 return snd_pcm_update_hw_ptr_post(substream, runtime);
323 * snd_pcm_set_ops - set the PCM operators
324 * @pcm: the pcm instance
325 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
326 * @ops: the operator table
328 * Sets the given PCM operators to the pcm instance.
330 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
332 struct snd_pcm_str *stream = &pcm->streams[direction];
333 struct snd_pcm_substream *substream;
335 for (substream = stream->substream; substream != NULL; substream = substream->next)
336 substream->ops = ops;
339 EXPORT_SYMBOL(snd_pcm_set_ops);
342 * snd_pcm_sync - set the PCM sync id
343 * @substream: the pcm substream
345 * Sets the PCM sync identifier for the card.
347 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
349 struct snd_pcm_runtime *runtime = substream->runtime;
351 runtime->sync.id32[0] = substream->pcm->card->number;
352 runtime->sync.id32[1] = -1;
353 runtime->sync.id32[2] = -1;
354 runtime->sync.id32[3] = -1;
357 EXPORT_SYMBOL(snd_pcm_set_sync);
360 * Standard ioctl routine
363 static inline unsigned int div32(unsigned int a, unsigned int b,
374 static inline unsigned int div_down(unsigned int a, unsigned int b)
381 static inline unsigned int div_up(unsigned int a, unsigned int b)
393 static inline unsigned int mul(unsigned int a, unsigned int b)
397 if (div_down(UINT_MAX, a) < b)
402 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
403 unsigned int c, unsigned int *r)
405 u_int64_t n = (u_int64_t) a * b;
420 * snd_interval_refine - refine the interval value of configurator
421 * @i: the interval value to refine
422 * @v: the interval value to refer to
424 * Refines the interval value with the reference value.
425 * The interval is changed to the range satisfying both intervals.
426 * The interval status (min, max, integer, etc.) are evaluated.
428 * Returns non-zero if the value is changed, zero if not changed.
430 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
433 if (snd_BUG_ON(snd_interval_empty(i)))
435 if (i->min < v->min) {
437 i->openmin = v->openmin;
439 } else if (i->min == v->min && !i->openmin && v->openmin) {
443 if (i->max > v->max) {
445 i->openmax = v->openmax;
447 } else if (i->max == v->max && !i->openmax && v->openmax) {
451 if (!i->integer && v->integer) {
464 } else if (!i->openmin && !i->openmax && i->min == i->max)
466 if (snd_interval_checkempty(i)) {
467 snd_interval_none(i);
473 EXPORT_SYMBOL(snd_interval_refine);
475 static int snd_interval_refine_first(struct snd_interval *i)
477 if (snd_BUG_ON(snd_interval_empty(i)))
479 if (snd_interval_single(i))
482 i->openmax = i->openmin;
488 static int snd_interval_refine_last(struct snd_interval *i)
490 if (snd_BUG_ON(snd_interval_empty(i)))
492 if (snd_interval_single(i))
495 i->openmin = i->openmax;
501 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
503 if (a->empty || b->empty) {
504 snd_interval_none(c);
508 c->min = mul(a->min, b->min);
509 c->openmin = (a->openmin || b->openmin);
510 c->max = mul(a->max, b->max);
511 c->openmax = (a->openmax || b->openmax);
512 c->integer = (a->integer && b->integer);
516 * snd_interval_div - refine the interval value with division
523 * Returns non-zero if the value is changed, zero if not changed.
525 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
528 if (a->empty || b->empty) {
529 snd_interval_none(c);
533 c->min = div32(a->min, b->max, &r);
534 c->openmin = (r || a->openmin || b->openmax);
536 c->max = div32(a->max, b->min, &r);
541 c->openmax = (a->openmax || b->openmin);
550 * snd_interval_muldivk - refine the interval value
553 * @k: divisor (as integer)
558 * Returns non-zero if the value is changed, zero if not changed.
560 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
561 unsigned int k, struct snd_interval *c)
564 if (a->empty || b->empty) {
565 snd_interval_none(c);
569 c->min = muldiv32(a->min, b->min, k, &r);
570 c->openmin = (r || a->openmin || b->openmin);
571 c->max = muldiv32(a->max, b->max, k, &r);
576 c->openmax = (a->openmax || b->openmax);
581 * snd_interval_mulkdiv - refine the interval value
583 * @k: dividend 2 (as integer)
589 * Returns non-zero if the value is changed, zero if not changed.
591 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
592 const struct snd_interval *b, struct snd_interval *c)
595 if (a->empty || b->empty) {
596 snd_interval_none(c);
600 c->min = muldiv32(a->min, k, b->max, &r);
601 c->openmin = (r || a->openmin || b->openmax);
603 c->max = muldiv32(a->max, k, b->min, &r);
608 c->openmax = (a->openmax || b->openmin);
620 * snd_interval_ratnum - refine the interval value
621 * @i: interval to refine
622 * @rats_count: number of ratnum_t
623 * @rats: ratnum_t array
624 * @nump: pointer to store the resultant numerator
625 * @denp: pointer to store the resultant denominator
627 * Returns non-zero if the value is changed, zero if not changed.
629 int snd_interval_ratnum(struct snd_interval *i,
630 unsigned int rats_count, struct snd_ratnum *rats,
631 unsigned int *nump, unsigned int *denp)
633 unsigned int best_num, best_diff, best_den;
635 struct snd_interval t;
638 best_num = best_den = best_diff = 0;
639 for (k = 0; k < rats_count; ++k) {
640 unsigned int num = rats[k].num;
642 unsigned int q = i->min;
646 den = div_down(num, q);
647 if (den < rats[k].den_min)
649 if (den > rats[k].den_max)
650 den = rats[k].den_max;
653 r = (den - rats[k].den_min) % rats[k].den_step;
657 diff = num - q * den;
659 diff * best_den < best_diff * den) {
669 t.min = div_down(best_num, best_den);
670 t.openmin = !!(best_num % best_den);
672 best_num = best_den = best_diff = 0;
673 for (k = 0; k < rats_count; ++k) {
674 unsigned int num = rats[k].num;
676 unsigned int q = i->max;
682 den = div_up(num, q);
683 if (den > rats[k].den_max)
685 if (den < rats[k].den_min)
686 den = rats[k].den_min;
689 r = (den - rats[k].den_min) % rats[k].den_step;
691 den += rats[k].den_step - r;
693 diff = q * den - num;
695 diff * best_den < best_diff * den) {
705 t.max = div_up(best_num, best_den);
706 t.openmax = !!(best_num % best_den);
708 err = snd_interval_refine(i, &t);
712 if (snd_interval_single(i)) {
721 EXPORT_SYMBOL(snd_interval_ratnum);
724 * snd_interval_ratden - refine the interval value
725 * @i: interval to refine
726 * @rats_count: number of struct ratden
727 * @rats: struct ratden array
728 * @nump: pointer to store the resultant numerator
729 * @denp: pointer to store the resultant denominator
731 * Returns non-zero if the value is changed, zero if not changed.
733 static int snd_interval_ratden(struct snd_interval *i,
734 unsigned int rats_count, struct snd_ratden *rats,
735 unsigned int *nump, unsigned int *denp)
737 unsigned int best_num, best_diff, best_den;
739 struct snd_interval t;
742 best_num = best_den = best_diff = 0;
743 for (k = 0; k < rats_count; ++k) {
745 unsigned int den = rats[k].den;
746 unsigned int q = i->min;
749 if (num > rats[k].num_max)
751 if (num < rats[k].num_min)
752 num = rats[k].num_max;
755 r = (num - rats[k].num_min) % rats[k].num_step;
757 num += rats[k].num_step - r;
759 diff = num - q * den;
761 diff * best_den < best_diff * den) {
771 t.min = div_down(best_num, best_den);
772 t.openmin = !!(best_num % best_den);
774 best_num = best_den = best_diff = 0;
775 for (k = 0; k < rats_count; ++k) {
777 unsigned int den = rats[k].den;
778 unsigned int q = i->max;
781 if (num < rats[k].num_min)
783 if (num > rats[k].num_max)
784 num = rats[k].num_max;
787 r = (num - rats[k].num_min) % rats[k].num_step;
791 diff = q * den - num;
793 diff * best_den < best_diff * den) {
803 t.max = div_up(best_num, best_den);
804 t.openmax = !!(best_num % best_den);
806 err = snd_interval_refine(i, &t);
810 if (snd_interval_single(i)) {
820 * snd_interval_list - refine the interval value from the list
821 * @i: the interval value to refine
822 * @count: the number of elements in the list
823 * @list: the value list
824 * @mask: the bit-mask to evaluate
826 * Refines the interval value from the list.
827 * When mask is non-zero, only the elements corresponding to bit 1 are
830 * Returns non-zero if the value is changed, zero if not changed.
832 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
841 for (k = 0; k < count; k++) {
842 if (mask && !(mask & (1 << k)))
844 if (i->min == list[k] && !i->openmin)
846 if (i->min < list[k]) {
856 for (k = count; k-- > 0;) {
857 if (mask && !(mask & (1 << k)))
859 if (i->max == list[k] && !i->openmax)
861 if (i->max > list[k]) {
871 if (snd_interval_checkempty(i)) {
878 EXPORT_SYMBOL(snd_interval_list);
880 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
884 n = (i->min - min) % step;
885 if (n != 0 || i->openmin) {
889 n = (i->max - min) % step;
890 if (n != 0 || i->openmax) {
894 if (snd_interval_checkempty(i)) {
901 /* Info constraints helpers */
904 * snd_pcm_hw_rule_add - add the hw-constraint rule
905 * @runtime: the pcm runtime instance
906 * @cond: condition bits
907 * @var: the variable to evaluate
908 * @func: the evaluation function
909 * @private: the private data pointer passed to function
910 * @dep: the dependent variables
912 * Returns zero if successful, or a negative error code on failure.
914 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
916 snd_pcm_hw_rule_func_t func, void *private,
919 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
920 struct snd_pcm_hw_rule *c;
924 if (constrs->rules_num >= constrs->rules_all) {
925 struct snd_pcm_hw_rule *new;
926 unsigned int new_rules = constrs->rules_all + 16;
927 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
930 if (constrs->rules) {
931 memcpy(new, constrs->rules,
932 constrs->rules_num * sizeof(*c));
933 kfree(constrs->rules);
935 constrs->rules = new;
936 constrs->rules_all = new_rules;
938 c = &constrs->rules[constrs->rules_num];
942 c->private = private;
945 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
950 dep = va_arg(args, int);
952 constrs->rules_num++;
957 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
960 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
961 * @runtime: PCM runtime instance
962 * @var: hw_params variable to apply the mask
963 * @mask: the bitmap mask
965 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
967 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
970 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
971 struct snd_mask *maskp = constrs_mask(constrs, var);
972 *maskp->bits &= mask;
973 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
974 if (*maskp->bits == 0)
980 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
981 * @runtime: PCM runtime instance
982 * @var: hw_params variable to apply the mask
983 * @mask: the 64bit bitmap mask
985 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
987 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
990 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
991 struct snd_mask *maskp = constrs_mask(constrs, var);
992 maskp->bits[0] &= (u_int32_t)mask;
993 maskp->bits[1] &= (u_int32_t)(mask >> 32);
994 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
995 if (! maskp->bits[0] && ! maskp->bits[1])
1001 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1002 * @runtime: PCM runtime instance
1003 * @var: hw_params variable to apply the integer constraint
1005 * Apply the constraint of integer to an interval parameter.
1007 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1009 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1010 return snd_interval_setinteger(constrs_interval(constrs, var));
1013 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1016 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1017 * @runtime: PCM runtime instance
1018 * @var: hw_params variable to apply the range
1019 * @min: the minimal value
1020 * @max: the maximal value
1022 * Apply the min/max range constraint to an interval parameter.
1024 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1025 unsigned int min, unsigned int max)
1027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_interval t;
1031 t.openmin = t.openmax = 0;
1033 return snd_interval_refine(constrs_interval(constrs, var), &t);
1036 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1038 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1039 struct snd_pcm_hw_rule *rule)
1041 struct snd_pcm_hw_constraint_list *list = rule->private;
1042 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1047 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1048 * @runtime: PCM runtime instance
1049 * @cond: condition bits
1050 * @var: hw_params variable to apply the list constraint
1053 * Apply the list of constraints to an interval parameter.
1055 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1057 snd_pcm_hw_param_t var,
1058 struct snd_pcm_hw_constraint_list *l)
1060 return snd_pcm_hw_rule_add(runtime, cond, var,
1061 snd_pcm_hw_rule_list, l,
1065 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1067 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1068 struct snd_pcm_hw_rule *rule)
1070 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1071 unsigned int num = 0, den = 0;
1073 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1074 r->nrats, r->rats, &num, &den);
1075 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1076 params->rate_num = num;
1077 params->rate_den = den;
1083 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1084 * @runtime: PCM runtime instance
1085 * @cond: condition bits
1086 * @var: hw_params variable to apply the ratnums constraint
1087 * @r: struct snd_ratnums constriants
1089 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1091 snd_pcm_hw_param_t var,
1092 struct snd_pcm_hw_constraint_ratnums *r)
1094 return snd_pcm_hw_rule_add(runtime, cond, var,
1095 snd_pcm_hw_rule_ratnums, r,
1099 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1101 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1102 struct snd_pcm_hw_rule *rule)
1104 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1105 unsigned int num = 0, den = 0;
1106 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1107 r->nrats, r->rats, &num, &den);
1108 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1109 params->rate_num = num;
1110 params->rate_den = den;
1116 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1117 * @runtime: PCM runtime instance
1118 * @cond: condition bits
1119 * @var: hw_params variable to apply the ratdens constraint
1120 * @r: struct snd_ratdens constriants
1122 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1124 snd_pcm_hw_param_t var,
1125 struct snd_pcm_hw_constraint_ratdens *r)
1127 return snd_pcm_hw_rule_add(runtime, cond, var,
1128 snd_pcm_hw_rule_ratdens, r,
1132 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1134 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1135 struct snd_pcm_hw_rule *rule)
1137 unsigned int l = (unsigned long) rule->private;
1138 int width = l & 0xffff;
1139 unsigned int msbits = l >> 16;
1140 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1141 if (snd_interval_single(i) && snd_interval_value(i) == width)
1142 params->msbits = msbits;
1147 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1148 * @runtime: PCM runtime instance
1149 * @cond: condition bits
1150 * @width: sample bits width
1151 * @msbits: msbits width
1153 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1156 unsigned int msbits)
1158 unsigned long l = (msbits << 16) | width;
1159 return snd_pcm_hw_rule_add(runtime, cond, -1,
1160 snd_pcm_hw_rule_msbits,
1162 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1165 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1167 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1168 struct snd_pcm_hw_rule *rule)
1170 unsigned long step = (unsigned long) rule->private;
1171 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1175 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1176 * @runtime: PCM runtime instance
1177 * @cond: condition bits
1178 * @var: hw_params variable to apply the step constraint
1181 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1183 snd_pcm_hw_param_t var,
1186 return snd_pcm_hw_rule_add(runtime, cond, var,
1187 snd_pcm_hw_rule_step, (void *) step,
1191 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1193 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1195 static unsigned int pow2_sizes[] = {
1196 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1197 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1198 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1199 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1201 return snd_interval_list(hw_param_interval(params, rule->var),
1202 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1206 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1207 * @runtime: PCM runtime instance
1208 * @cond: condition bits
1209 * @var: hw_params variable to apply the power-of-2 constraint
1211 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1213 snd_pcm_hw_param_t var)
1215 return snd_pcm_hw_rule_add(runtime, cond, var,
1216 snd_pcm_hw_rule_pow2, NULL,
1220 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1222 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1223 snd_pcm_hw_param_t var)
1225 if (hw_is_mask(var)) {
1226 snd_mask_any(hw_param_mask(params, var));
1227 params->cmask |= 1 << var;
1228 params->rmask |= 1 << var;
1231 if (hw_is_interval(var)) {
1232 snd_interval_any(hw_param_interval(params, var));
1233 params->cmask |= 1 << var;
1234 params->rmask |= 1 << var;
1240 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1243 memset(params, 0, sizeof(*params));
1244 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1245 _snd_pcm_hw_param_any(params, k);
1246 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1247 _snd_pcm_hw_param_any(params, k);
1251 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1254 * snd_pcm_hw_param_value - return @params field @var value
1255 * @params: the hw_params instance
1256 * @var: parameter to retrieve
1257 * @dir: pointer to the direction (-1,0,1) or %NULL
1259 * Return the value for field @var if it's fixed in configuration space
1260 * defined by @params. Return -%EINVAL otherwise.
1262 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1263 snd_pcm_hw_param_t var, int *dir)
1265 if (hw_is_mask(var)) {
1266 const struct snd_mask *mask = hw_param_mask_c(params, var);
1267 if (!snd_mask_single(mask))
1271 return snd_mask_value(mask);
1273 if (hw_is_interval(var)) {
1274 const struct snd_interval *i = hw_param_interval_c(params, var);
1275 if (!snd_interval_single(i))
1279 return snd_interval_value(i);
1284 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1286 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1287 snd_pcm_hw_param_t var)
1289 if (hw_is_mask(var)) {
1290 snd_mask_none(hw_param_mask(params, var));
1291 params->cmask |= 1 << var;
1292 params->rmask |= 1 << var;
1293 } else if (hw_is_interval(var)) {
1294 snd_interval_none(hw_param_interval(params, var));
1295 params->cmask |= 1 << var;
1296 params->rmask |= 1 << var;
1302 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1304 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1305 snd_pcm_hw_param_t var)
1308 if (hw_is_mask(var))
1309 changed = snd_mask_refine_first(hw_param_mask(params, var));
1310 else if (hw_is_interval(var))
1311 changed = snd_interval_refine_first(hw_param_interval(params, var));
1315 params->cmask |= 1 << var;
1316 params->rmask |= 1 << var;
1323 * snd_pcm_hw_param_first - refine config space and return minimum value
1324 * @pcm: PCM instance
1325 * @params: the hw_params instance
1326 * @var: parameter to retrieve
1327 * @dir: pointer to the direction (-1,0,1) or %NULL
1329 * Inside configuration space defined by @params remove from @var all
1330 * values > minimum. Reduce configuration space accordingly.
1331 * Return the minimum.
1333 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1334 struct snd_pcm_hw_params *params,
1335 snd_pcm_hw_param_t var, int *dir)
1337 int changed = _snd_pcm_hw_param_first(params, var);
1340 if (params->rmask) {
1341 int err = snd_pcm_hw_refine(pcm, params);
1342 if (snd_BUG_ON(err < 0))
1345 return snd_pcm_hw_param_value(params, var, dir);
1348 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1350 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1351 snd_pcm_hw_param_t var)
1354 if (hw_is_mask(var))
1355 changed = snd_mask_refine_last(hw_param_mask(params, var));
1356 else if (hw_is_interval(var))
1357 changed = snd_interval_refine_last(hw_param_interval(params, var));
1361 params->cmask |= 1 << var;
1362 params->rmask |= 1 << var;
1369 * snd_pcm_hw_param_last - refine config space and return maximum value
1370 * @pcm: PCM instance
1371 * @params: the hw_params instance
1372 * @var: parameter to retrieve
1373 * @dir: pointer to the direction (-1,0,1) or %NULL
1375 * Inside configuration space defined by @params remove from @var all
1376 * values < maximum. Reduce configuration space accordingly.
1377 * Return the maximum.
1379 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1380 struct snd_pcm_hw_params *params,
1381 snd_pcm_hw_param_t var, int *dir)
1383 int changed = _snd_pcm_hw_param_last(params, var);
1386 if (params->rmask) {
1387 int err = snd_pcm_hw_refine(pcm, params);
1388 if (snd_BUG_ON(err < 0))
1391 return snd_pcm_hw_param_value(params, var, dir);
1394 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1397 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1398 * @pcm: PCM instance
1399 * @params: the hw_params instance
1401 * Choose one configuration from configuration space defined by @params.
1402 * The configuration chosen is that obtained fixing in this order:
1403 * first access, first format, first subformat, min channels,
1404 * min rate, min period time, max buffer size, min tick time
1406 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1407 struct snd_pcm_hw_params *params)
1409 static int vars[] = {
1410 SNDRV_PCM_HW_PARAM_ACCESS,
1411 SNDRV_PCM_HW_PARAM_FORMAT,
1412 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1413 SNDRV_PCM_HW_PARAM_CHANNELS,
1414 SNDRV_PCM_HW_PARAM_RATE,
1415 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1416 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1417 SNDRV_PCM_HW_PARAM_TICK_TIME,
1422 for (v = vars; *v != -1; v++) {
1423 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1424 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1426 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1427 if (snd_BUG_ON(err < 0))
1433 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1436 struct snd_pcm_runtime *runtime = substream->runtime;
1437 unsigned long flags;
1438 snd_pcm_stream_lock_irqsave(substream, flags);
1439 if (snd_pcm_running(substream) &&
1440 snd_pcm_update_hw_ptr(substream) >= 0)
1441 runtime->status->hw_ptr %= runtime->buffer_size;
1443 runtime->status->hw_ptr = 0;
1444 snd_pcm_stream_unlock_irqrestore(substream, flags);
1448 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1451 struct snd_pcm_channel_info *info = arg;
1452 struct snd_pcm_runtime *runtime = substream->runtime;
1454 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1458 width = snd_pcm_format_physical_width(runtime->format);
1462 switch (runtime->access) {
1463 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1464 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1465 info->first = info->channel * width;
1466 info->step = runtime->channels * width;
1468 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1469 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1471 size_t size = runtime->dma_bytes / runtime->channels;
1472 info->first = info->channel * size * 8;
1484 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1485 * @substream: the pcm substream instance
1486 * @cmd: ioctl command
1487 * @arg: ioctl argument
1489 * Processes the generic ioctl commands for PCM.
1490 * Can be passed as the ioctl callback for PCM ops.
1492 * Returns zero if successful, or a negative error code on failure.
1494 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1495 unsigned int cmd, void *arg)
1498 case SNDRV_PCM_IOCTL1_INFO:
1500 case SNDRV_PCM_IOCTL1_RESET:
1501 return snd_pcm_lib_ioctl_reset(substream, arg);
1502 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1503 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1508 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1511 * snd_pcm_period_elapsed - update the pcm status for the next period
1512 * @substream: the pcm substream instance
1514 * This function is called from the interrupt handler when the
1515 * PCM has processed the period size. It will update the current
1516 * pointer, wake up sleepers, etc.
1518 * Even if more than one periods have elapsed since the last call, you
1519 * have to call this only once.
1521 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1523 struct snd_pcm_runtime *runtime;
1524 unsigned long flags;
1526 if (PCM_RUNTIME_CHECK(substream))
1528 runtime = substream->runtime;
1530 if (runtime->transfer_ack_begin)
1531 runtime->transfer_ack_begin(substream);
1533 snd_pcm_stream_lock_irqsave(substream, flags);
1534 if (!snd_pcm_running(substream) ||
1535 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1538 if (substream->timer_running)
1539 snd_timer_interrupt(substream->timer, 1);
1541 snd_pcm_stream_unlock_irqrestore(substream, flags);
1542 if (runtime->transfer_ack_end)
1543 runtime->transfer_ack_end(substream);
1544 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1547 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1550 * Wait until avail_min data becomes available
1551 * Returns a negative error code if any error occurs during operation.
1552 * The available space is stored on availp. When err = 0 and avail = 0
1553 * on the capture stream, it indicates the stream is in DRAINING state.
1555 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1556 snd_pcm_uframes_t *availp)
1558 struct snd_pcm_runtime *runtime = substream->runtime;
1559 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1562 snd_pcm_uframes_t avail = 0;
1565 init_waitqueue_entry(&wait, current);
1566 add_wait_queue(&runtime->sleep, &wait);
1568 if (signal_pending(current)) {
1572 set_current_state(TASK_INTERRUPTIBLE);
1573 snd_pcm_stream_unlock_irq(substream);
1574 tout = schedule_timeout(msecs_to_jiffies(10000));
1575 snd_pcm_stream_lock_irq(substream);
1576 switch (runtime->status->state) {
1577 case SNDRV_PCM_STATE_SUSPENDED:
1580 case SNDRV_PCM_STATE_XRUN:
1583 case SNDRV_PCM_STATE_DRAINING:
1587 avail = 0; /* indicate draining */
1589 case SNDRV_PCM_STATE_OPEN:
1590 case SNDRV_PCM_STATE_SETUP:
1591 case SNDRV_PCM_STATE_DISCONNECTED:
1596 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1597 is_playback ? "playback" : "capture");
1602 avail = snd_pcm_playback_avail(runtime);
1604 avail = snd_pcm_capture_avail(runtime);
1605 if (avail >= runtime->control->avail_min)
1609 remove_wait_queue(&runtime->sleep, &wait);
1614 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1616 unsigned long data, unsigned int off,
1617 snd_pcm_uframes_t frames)
1619 struct snd_pcm_runtime *runtime = substream->runtime;
1621 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1622 if (substream->ops->copy) {
1623 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1626 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1627 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1633 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1634 unsigned long data, unsigned int off,
1635 snd_pcm_uframes_t size);
1637 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1639 snd_pcm_uframes_t size,
1641 transfer_f transfer)
1643 struct snd_pcm_runtime *runtime = substream->runtime;
1644 snd_pcm_uframes_t xfer = 0;
1645 snd_pcm_uframes_t offset = 0;
1651 snd_pcm_stream_lock_irq(substream);
1652 switch (runtime->status->state) {
1653 case SNDRV_PCM_STATE_PREPARED:
1654 case SNDRV_PCM_STATE_RUNNING:
1655 case SNDRV_PCM_STATE_PAUSED:
1657 case SNDRV_PCM_STATE_XRUN:
1660 case SNDRV_PCM_STATE_SUSPENDED:
1669 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1670 snd_pcm_uframes_t avail;
1671 snd_pcm_uframes_t cont;
1672 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1673 snd_pcm_update_hw_ptr(substream);
1674 avail = snd_pcm_playback_avail(runtime);
1680 err = wait_for_avail_min(substream, &avail);
1684 frames = size > avail ? avail : size;
1685 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1688 if (snd_BUG_ON(!frames)) {
1689 snd_pcm_stream_unlock_irq(substream);
1692 appl_ptr = runtime->control->appl_ptr;
1693 appl_ofs = appl_ptr % runtime->buffer_size;
1694 snd_pcm_stream_unlock_irq(substream);
1695 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1697 snd_pcm_stream_lock_irq(substream);
1698 switch (runtime->status->state) {
1699 case SNDRV_PCM_STATE_XRUN:
1702 case SNDRV_PCM_STATE_SUSPENDED:
1709 if (appl_ptr >= runtime->boundary)
1710 appl_ptr -= runtime->boundary;
1711 runtime->control->appl_ptr = appl_ptr;
1712 if (substream->ops->ack)
1713 substream->ops->ack(substream);
1718 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1719 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1720 err = snd_pcm_start(substream);
1726 snd_pcm_stream_unlock_irq(substream);
1728 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1731 /* sanity-check for read/write methods */
1732 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1734 struct snd_pcm_runtime *runtime;
1735 if (PCM_RUNTIME_CHECK(substream))
1737 runtime = substream->runtime;
1738 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1740 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1745 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1747 struct snd_pcm_runtime *runtime;
1751 err = pcm_sanity_check(substream);
1754 runtime = substream->runtime;
1755 nonblock = !!(substream->f_flags & O_NONBLOCK);
1757 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1758 runtime->channels > 1)
1760 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1761 snd_pcm_lib_write_transfer);
1764 EXPORT_SYMBOL(snd_pcm_lib_write);
1766 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1768 unsigned long data, unsigned int off,
1769 snd_pcm_uframes_t frames)
1771 struct snd_pcm_runtime *runtime = substream->runtime;
1773 void __user **bufs = (void __user **)data;
1774 int channels = runtime->channels;
1776 if (substream->ops->copy) {
1777 if (snd_BUG_ON(!substream->ops->silence))
1779 for (c = 0; c < channels; ++c, ++bufs) {
1780 if (*bufs == NULL) {
1781 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1784 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1785 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1790 /* default transfer behaviour */
1791 size_t dma_csize = runtime->dma_bytes / channels;
1792 for (c = 0; c < channels; ++c, ++bufs) {
1793 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1794 if (*bufs == NULL) {
1795 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1797 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1798 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1806 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1808 snd_pcm_uframes_t frames)
1810 struct snd_pcm_runtime *runtime;
1814 err = pcm_sanity_check(substream);
1817 runtime = substream->runtime;
1818 nonblock = !!(substream->f_flags & O_NONBLOCK);
1820 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1822 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1823 nonblock, snd_pcm_lib_writev_transfer);
1826 EXPORT_SYMBOL(snd_pcm_lib_writev);
1828 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1830 unsigned long data, unsigned int off,
1831 snd_pcm_uframes_t frames)
1833 struct snd_pcm_runtime *runtime = substream->runtime;
1835 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1836 if (substream->ops->copy) {
1837 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1840 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1841 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1847 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1849 snd_pcm_uframes_t size,
1851 transfer_f transfer)
1853 struct snd_pcm_runtime *runtime = substream->runtime;
1854 snd_pcm_uframes_t xfer = 0;
1855 snd_pcm_uframes_t offset = 0;
1861 snd_pcm_stream_lock_irq(substream);
1862 switch (runtime->status->state) {
1863 case SNDRV_PCM_STATE_PREPARED:
1864 if (size >= runtime->start_threshold) {
1865 err = snd_pcm_start(substream);
1870 case SNDRV_PCM_STATE_DRAINING:
1871 case SNDRV_PCM_STATE_RUNNING:
1872 case SNDRV_PCM_STATE_PAUSED:
1874 case SNDRV_PCM_STATE_XRUN:
1877 case SNDRV_PCM_STATE_SUSPENDED:
1886 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1887 snd_pcm_uframes_t avail;
1888 snd_pcm_uframes_t cont;
1889 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1890 snd_pcm_update_hw_ptr(substream);
1891 avail = snd_pcm_capture_avail(runtime);
1893 if (runtime->status->state ==
1894 SNDRV_PCM_STATE_DRAINING) {
1895 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1902 err = wait_for_avail_min(substream, &avail);
1906 continue; /* draining */
1908 frames = size > avail ? avail : size;
1909 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1912 if (snd_BUG_ON(!frames)) {
1913 snd_pcm_stream_unlock_irq(substream);
1916 appl_ptr = runtime->control->appl_ptr;
1917 appl_ofs = appl_ptr % runtime->buffer_size;
1918 snd_pcm_stream_unlock_irq(substream);
1919 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1921 snd_pcm_stream_lock_irq(substream);
1922 switch (runtime->status->state) {
1923 case SNDRV_PCM_STATE_XRUN:
1926 case SNDRV_PCM_STATE_SUSPENDED:
1933 if (appl_ptr >= runtime->boundary)
1934 appl_ptr -= runtime->boundary;
1935 runtime->control->appl_ptr = appl_ptr;
1936 if (substream->ops->ack)
1937 substream->ops->ack(substream);
1944 snd_pcm_stream_unlock_irq(substream);
1946 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1949 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1951 struct snd_pcm_runtime *runtime;
1955 err = pcm_sanity_check(substream);
1958 runtime = substream->runtime;
1959 nonblock = !!(substream->f_flags & O_NONBLOCK);
1960 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1962 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1965 EXPORT_SYMBOL(snd_pcm_lib_read);
1967 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1969 unsigned long data, unsigned int off,
1970 snd_pcm_uframes_t frames)
1972 struct snd_pcm_runtime *runtime = substream->runtime;
1974 void __user **bufs = (void __user **)data;
1975 int channels = runtime->channels;
1977 if (substream->ops->copy) {
1978 for (c = 0; c < channels; ++c, ++bufs) {
1982 buf = *bufs + samples_to_bytes(runtime, off);
1983 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1987 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1988 for (c = 0; c < channels; ++c, ++bufs) {
1994 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1995 buf = *bufs + samples_to_bytes(runtime, off);
1996 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2003 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2005 snd_pcm_uframes_t frames)
2007 struct snd_pcm_runtime *runtime;
2011 err = pcm_sanity_check(substream);
2014 runtime = substream->runtime;
2015 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2018 nonblock = !!(substream->f_flags & O_NONBLOCK);
2019 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2021 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2024 EXPORT_SYMBOL(snd_pcm_lib_readv);