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 inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
152 struct snd_pcm_runtime *runtime)
154 snd_pcm_uframes_t pos;
156 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
157 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
158 pos = substream->ops->pointer(substream);
159 if (pos == SNDRV_PCM_POS_XRUN)
160 return pos; /* XRUN */
161 #ifdef CONFIG_SND_DEBUG
162 if (pos >= runtime->buffer_size) {
163 snd_printk(KERN_ERR "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size);
166 pos -= pos % runtime->min_align;
170 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
171 struct snd_pcm_runtime *runtime)
173 snd_pcm_uframes_t avail;
175 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
176 avail = snd_pcm_playback_avail(runtime);
178 avail = snd_pcm_capture_avail(runtime);
179 if (avail > runtime->avail_max)
180 runtime->avail_max = avail;
181 if (avail >= runtime->stop_threshold) {
182 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
183 snd_pcm_drain_done(substream);
188 if (avail >= runtime->control->avail_min)
189 wake_up(&runtime->sleep);
193 #define hw_ptr_error(substream, fmt, args...) \
195 if (xrun_debug(substream)) { \
196 if (printk_ratelimit()) { \
197 snd_printd("PCM: " fmt, ##args); \
199 dump_stack_on_xrun(substream); \
203 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
205 struct snd_pcm_runtime *runtime = substream->runtime;
206 snd_pcm_uframes_t pos;
207 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
208 snd_pcm_sframes_t delta;
210 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
211 if (pos == SNDRV_PCM_POS_XRUN) {
215 hw_base = runtime->hw_ptr_base;
216 new_hw_ptr = hw_base + pos;
217 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
218 delta = new_hw_ptr - hw_ptr_interrupt;
219 if (hw_ptr_interrupt == runtime->boundary)
220 hw_ptr_interrupt = 0;
222 delta += runtime->buffer_size;
224 hw_ptr_error(substream,
225 "Unexpected hw_pointer value "
226 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
227 substream->stream, (long)pos,
228 (long)hw_ptr_interrupt);
229 /* rebase to interrupt position */
230 hw_base = new_hw_ptr = hw_ptr_interrupt;
233 hw_base += runtime->buffer_size;
234 if (hw_base == runtime->boundary)
236 new_hw_ptr = hw_base + pos;
239 if (delta > runtime->period_size) {
240 hw_ptr_error(substream,
242 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
243 substream->stream, (long)delta,
244 (long)hw_ptr_interrupt);
245 /* rebase hw_ptr_interrupt */
247 new_hw_ptr - new_hw_ptr % runtime->period_size;
249 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
250 runtime->silence_size > 0)
251 snd_pcm_playback_silence(substream, new_hw_ptr);
253 runtime->hw_ptr_base = hw_base;
254 runtime->status->hw_ptr = new_hw_ptr;
255 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
257 return snd_pcm_update_hw_ptr_post(substream, runtime);
260 /* CAUTION: call it with irq disabled */
261 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
263 struct snd_pcm_runtime *runtime = substream->runtime;
264 snd_pcm_uframes_t pos;
265 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
266 snd_pcm_sframes_t delta;
268 old_hw_ptr = runtime->status->hw_ptr;
269 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
270 if (pos == SNDRV_PCM_POS_XRUN) {
274 hw_base = runtime->hw_ptr_base;
275 new_hw_ptr = hw_base + pos;
277 delta = new_hw_ptr - old_hw_ptr;
279 delta += runtime->buffer_size;
281 hw_ptr_error(substream,
282 "Unexpected hw_pointer value [2] "
283 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
284 substream->stream, (long)pos,
288 hw_base += runtime->buffer_size;
289 if (hw_base == runtime->boundary)
291 new_hw_ptr = hw_base + pos;
293 if (delta > runtime->period_size && runtime->periods > 1) {
294 hw_ptr_error(substream,
296 "(pos=%ld, delta=%ld, period=%ld)\n",
297 (long)pos, (long)delta,
298 (long)runtime->period_size);
301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
302 runtime->silence_size > 0)
303 snd_pcm_playback_silence(substream, new_hw_ptr);
305 runtime->hw_ptr_base = hw_base;
306 runtime->status->hw_ptr = new_hw_ptr;
308 return snd_pcm_update_hw_ptr_post(substream, runtime);
312 * snd_pcm_set_ops - set the PCM operators
313 * @pcm: the pcm instance
314 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
315 * @ops: the operator table
317 * Sets the given PCM operators to the pcm instance.
319 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
321 struct snd_pcm_str *stream = &pcm->streams[direction];
322 struct snd_pcm_substream *substream;
324 for (substream = stream->substream; substream != NULL; substream = substream->next)
325 substream->ops = ops;
328 EXPORT_SYMBOL(snd_pcm_set_ops);
331 * snd_pcm_sync - set the PCM sync id
332 * @substream: the pcm substream
334 * Sets the PCM sync identifier for the card.
336 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
338 struct snd_pcm_runtime *runtime = substream->runtime;
340 runtime->sync.id32[0] = substream->pcm->card->number;
341 runtime->sync.id32[1] = -1;
342 runtime->sync.id32[2] = -1;
343 runtime->sync.id32[3] = -1;
346 EXPORT_SYMBOL(snd_pcm_set_sync);
349 * Standard ioctl routine
352 static inline unsigned int div32(unsigned int a, unsigned int b,
363 static inline unsigned int div_down(unsigned int a, unsigned int b)
370 static inline unsigned int div_up(unsigned int a, unsigned int b)
382 static inline unsigned int mul(unsigned int a, unsigned int b)
386 if (div_down(UINT_MAX, a) < b)
391 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
392 unsigned int c, unsigned int *r)
394 u_int64_t n = (u_int64_t) a * b;
409 * snd_interval_refine - refine the interval value of configurator
410 * @i: the interval value to refine
411 * @v: the interval value to refer to
413 * Refines the interval value with the reference value.
414 * The interval is changed to the range satisfying both intervals.
415 * The interval status (min, max, integer, etc.) are evaluated.
417 * Returns non-zero if the value is changed, zero if not changed.
419 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
422 if (snd_BUG_ON(snd_interval_empty(i)))
424 if (i->min < v->min) {
426 i->openmin = v->openmin;
428 } else if (i->min == v->min && !i->openmin && v->openmin) {
432 if (i->max > v->max) {
434 i->openmax = v->openmax;
436 } else if (i->max == v->max && !i->openmax && v->openmax) {
440 if (!i->integer && v->integer) {
453 } else if (!i->openmin && !i->openmax && i->min == i->max)
455 if (snd_interval_checkempty(i)) {
456 snd_interval_none(i);
462 EXPORT_SYMBOL(snd_interval_refine);
464 static int snd_interval_refine_first(struct snd_interval *i)
466 if (snd_BUG_ON(snd_interval_empty(i)))
468 if (snd_interval_single(i))
471 i->openmax = i->openmin;
477 static int snd_interval_refine_last(struct snd_interval *i)
479 if (snd_BUG_ON(snd_interval_empty(i)))
481 if (snd_interval_single(i))
484 i->openmin = i->openmax;
490 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
492 if (a->empty || b->empty) {
493 snd_interval_none(c);
497 c->min = mul(a->min, b->min);
498 c->openmin = (a->openmin || b->openmin);
499 c->max = mul(a->max, b->max);
500 c->openmax = (a->openmax || b->openmax);
501 c->integer = (a->integer && b->integer);
505 * snd_interval_div - refine the interval value with division
512 * Returns non-zero if the value is changed, zero if not changed.
514 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
517 if (a->empty || b->empty) {
518 snd_interval_none(c);
522 c->min = div32(a->min, b->max, &r);
523 c->openmin = (r || a->openmin || b->openmax);
525 c->max = div32(a->max, b->min, &r);
530 c->openmax = (a->openmax || b->openmin);
539 * snd_interval_muldivk - refine the interval value
542 * @k: divisor (as integer)
547 * Returns non-zero if the value is changed, zero if not changed.
549 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
550 unsigned int k, struct snd_interval *c)
553 if (a->empty || b->empty) {
554 snd_interval_none(c);
558 c->min = muldiv32(a->min, b->min, k, &r);
559 c->openmin = (r || a->openmin || b->openmin);
560 c->max = muldiv32(a->max, b->max, k, &r);
565 c->openmax = (a->openmax || b->openmax);
570 * snd_interval_mulkdiv - refine the interval value
572 * @k: dividend 2 (as integer)
578 * Returns non-zero if the value is changed, zero if not changed.
580 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
581 const struct snd_interval *b, struct snd_interval *c)
584 if (a->empty || b->empty) {
585 snd_interval_none(c);
589 c->min = muldiv32(a->min, k, b->max, &r);
590 c->openmin = (r || a->openmin || b->openmax);
592 c->max = muldiv32(a->max, k, b->min, &r);
597 c->openmax = (a->openmax || b->openmin);
609 * snd_interval_ratnum - refine the interval value
610 * @i: interval to refine
611 * @rats_count: number of ratnum_t
612 * @rats: ratnum_t array
613 * @nump: pointer to store the resultant numerator
614 * @denp: pointer to store the resultant denominator
616 * Returns non-zero if the value is changed, zero if not changed.
618 int snd_interval_ratnum(struct snd_interval *i,
619 unsigned int rats_count, struct snd_ratnum *rats,
620 unsigned int *nump, unsigned int *denp)
622 unsigned int best_num, best_diff, best_den;
624 struct snd_interval t;
627 best_num = best_den = best_diff = 0;
628 for (k = 0; k < rats_count; ++k) {
629 unsigned int num = rats[k].num;
631 unsigned int q = i->min;
635 den = div_down(num, q);
636 if (den < rats[k].den_min)
638 if (den > rats[k].den_max)
639 den = rats[k].den_max;
642 r = (den - rats[k].den_min) % rats[k].den_step;
646 diff = num - q * den;
648 diff * best_den < best_diff * den) {
658 t.min = div_down(best_num, best_den);
659 t.openmin = !!(best_num % best_den);
661 best_num = best_den = best_diff = 0;
662 for (k = 0; k < rats_count; ++k) {
663 unsigned int num = rats[k].num;
665 unsigned int q = i->max;
671 den = div_up(num, q);
672 if (den > rats[k].den_max)
674 if (den < rats[k].den_min)
675 den = rats[k].den_min;
678 r = (den - rats[k].den_min) % rats[k].den_step;
680 den += rats[k].den_step - r;
682 diff = q * den - num;
684 diff * best_den < best_diff * den) {
694 t.max = div_up(best_num, best_den);
695 t.openmax = !!(best_num % best_den);
697 err = snd_interval_refine(i, &t);
701 if (snd_interval_single(i)) {
710 EXPORT_SYMBOL(snd_interval_ratnum);
713 * snd_interval_ratden - refine the interval value
714 * @i: interval to refine
715 * @rats_count: number of struct ratden
716 * @rats: struct ratden array
717 * @nump: pointer to store the resultant numerator
718 * @denp: pointer to store the resultant denominator
720 * Returns non-zero if the value is changed, zero if not changed.
722 static int snd_interval_ratden(struct snd_interval *i,
723 unsigned int rats_count, struct snd_ratden *rats,
724 unsigned int *nump, unsigned int *denp)
726 unsigned int best_num, best_diff, best_den;
728 struct snd_interval t;
731 best_num = best_den = best_diff = 0;
732 for (k = 0; k < rats_count; ++k) {
734 unsigned int den = rats[k].den;
735 unsigned int q = i->min;
738 if (num > rats[k].num_max)
740 if (num < rats[k].num_min)
741 num = rats[k].num_max;
744 r = (num - rats[k].num_min) % rats[k].num_step;
746 num += rats[k].num_step - r;
748 diff = num - q * den;
750 diff * best_den < best_diff * den) {
760 t.min = div_down(best_num, best_den);
761 t.openmin = !!(best_num % best_den);
763 best_num = best_den = best_diff = 0;
764 for (k = 0; k < rats_count; ++k) {
766 unsigned int den = rats[k].den;
767 unsigned int q = i->max;
770 if (num < rats[k].num_min)
772 if (num > rats[k].num_max)
773 num = rats[k].num_max;
776 r = (num - rats[k].num_min) % rats[k].num_step;
780 diff = q * den - num;
782 diff * best_den < best_diff * den) {
792 t.max = div_up(best_num, best_den);
793 t.openmax = !!(best_num % best_den);
795 err = snd_interval_refine(i, &t);
799 if (snd_interval_single(i)) {
809 * snd_interval_list - refine the interval value from the list
810 * @i: the interval value to refine
811 * @count: the number of elements in the list
812 * @list: the value list
813 * @mask: the bit-mask to evaluate
815 * Refines the interval value from the list.
816 * When mask is non-zero, only the elements corresponding to bit 1 are
819 * Returns non-zero if the value is changed, zero if not changed.
821 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
830 for (k = 0; k < count; k++) {
831 if (mask && !(mask & (1 << k)))
833 if (i->min == list[k] && !i->openmin)
835 if (i->min < list[k]) {
845 for (k = count; k-- > 0;) {
846 if (mask && !(mask & (1 << k)))
848 if (i->max == list[k] && !i->openmax)
850 if (i->max > list[k]) {
860 if (snd_interval_checkempty(i)) {
867 EXPORT_SYMBOL(snd_interval_list);
869 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
873 n = (i->min - min) % step;
874 if (n != 0 || i->openmin) {
878 n = (i->max - min) % step;
879 if (n != 0 || i->openmax) {
883 if (snd_interval_checkempty(i)) {
890 /* Info constraints helpers */
893 * snd_pcm_hw_rule_add - add the hw-constraint rule
894 * @runtime: the pcm runtime instance
895 * @cond: condition bits
896 * @var: the variable to evaluate
897 * @func: the evaluation function
898 * @private: the private data pointer passed to function
899 * @dep: the dependent variables
901 * Returns zero if successful, or a negative error code on failure.
903 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
905 snd_pcm_hw_rule_func_t func, void *private,
908 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
909 struct snd_pcm_hw_rule *c;
913 if (constrs->rules_num >= constrs->rules_all) {
914 struct snd_pcm_hw_rule *new;
915 unsigned int new_rules = constrs->rules_all + 16;
916 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
919 if (constrs->rules) {
920 memcpy(new, constrs->rules,
921 constrs->rules_num * sizeof(*c));
922 kfree(constrs->rules);
924 constrs->rules = new;
925 constrs->rules_all = new_rules;
927 c = &constrs->rules[constrs->rules_num];
931 c->private = private;
934 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
939 dep = va_arg(args, int);
941 constrs->rules_num++;
946 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
949 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
950 * @runtime: PCM runtime instance
951 * @var: hw_params variable to apply the mask
952 * @mask: the bitmap mask
954 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
956 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
959 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
960 struct snd_mask *maskp = constrs_mask(constrs, var);
961 *maskp->bits &= mask;
962 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
963 if (*maskp->bits == 0)
969 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
970 * @runtime: PCM runtime instance
971 * @var: hw_params variable to apply the mask
972 * @mask: the 64bit bitmap mask
974 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
976 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
979 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
980 struct snd_mask *maskp = constrs_mask(constrs, var);
981 maskp->bits[0] &= (u_int32_t)mask;
982 maskp->bits[1] &= (u_int32_t)(mask >> 32);
983 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
984 if (! maskp->bits[0] && ! maskp->bits[1])
990 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
991 * @runtime: PCM runtime instance
992 * @var: hw_params variable to apply the integer constraint
994 * Apply the constraint of integer to an interval parameter.
996 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
998 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
999 return snd_interval_setinteger(constrs_interval(constrs, var));
1002 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1005 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1006 * @runtime: PCM runtime instance
1007 * @var: hw_params variable to apply the range
1008 * @min: the minimal value
1009 * @max: the maximal value
1011 * Apply the min/max range constraint to an interval parameter.
1013 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1014 unsigned int min, unsigned int max)
1016 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1017 struct snd_interval t;
1020 t.openmin = t.openmax = 0;
1022 return snd_interval_refine(constrs_interval(constrs, var), &t);
1025 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1027 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1028 struct snd_pcm_hw_rule *rule)
1030 struct snd_pcm_hw_constraint_list *list = rule->private;
1031 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1036 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1037 * @runtime: PCM runtime instance
1038 * @cond: condition bits
1039 * @var: hw_params variable to apply the list constraint
1042 * Apply the list of constraints to an interval parameter.
1044 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1046 snd_pcm_hw_param_t var,
1047 struct snd_pcm_hw_constraint_list *l)
1049 return snd_pcm_hw_rule_add(runtime, cond, var,
1050 snd_pcm_hw_rule_list, l,
1054 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1056 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1057 struct snd_pcm_hw_rule *rule)
1059 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1060 unsigned int num = 0, den = 0;
1062 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1063 r->nrats, r->rats, &num, &den);
1064 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1065 params->rate_num = num;
1066 params->rate_den = den;
1072 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1073 * @runtime: PCM runtime instance
1074 * @cond: condition bits
1075 * @var: hw_params variable to apply the ratnums constraint
1076 * @r: struct snd_ratnums constriants
1078 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1080 snd_pcm_hw_param_t var,
1081 struct snd_pcm_hw_constraint_ratnums *r)
1083 return snd_pcm_hw_rule_add(runtime, cond, var,
1084 snd_pcm_hw_rule_ratnums, r,
1088 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1090 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1091 struct snd_pcm_hw_rule *rule)
1093 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1094 unsigned int num = 0, den = 0;
1095 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1096 r->nrats, r->rats, &num, &den);
1097 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1098 params->rate_num = num;
1099 params->rate_den = den;
1105 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1106 * @runtime: PCM runtime instance
1107 * @cond: condition bits
1108 * @var: hw_params variable to apply the ratdens constraint
1109 * @r: struct snd_ratdens constriants
1111 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1113 snd_pcm_hw_param_t var,
1114 struct snd_pcm_hw_constraint_ratdens *r)
1116 return snd_pcm_hw_rule_add(runtime, cond, var,
1117 snd_pcm_hw_rule_ratdens, r,
1121 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1123 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1124 struct snd_pcm_hw_rule *rule)
1126 unsigned int l = (unsigned long) rule->private;
1127 int width = l & 0xffff;
1128 unsigned int msbits = l >> 16;
1129 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1130 if (snd_interval_single(i) && snd_interval_value(i) == width)
1131 params->msbits = msbits;
1136 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1137 * @runtime: PCM runtime instance
1138 * @cond: condition bits
1139 * @width: sample bits width
1140 * @msbits: msbits width
1142 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1145 unsigned int msbits)
1147 unsigned long l = (msbits << 16) | width;
1148 return snd_pcm_hw_rule_add(runtime, cond, -1,
1149 snd_pcm_hw_rule_msbits,
1151 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1154 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1156 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1157 struct snd_pcm_hw_rule *rule)
1159 unsigned long step = (unsigned long) rule->private;
1160 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1164 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1165 * @runtime: PCM runtime instance
1166 * @cond: condition bits
1167 * @var: hw_params variable to apply the step constraint
1170 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1172 snd_pcm_hw_param_t var,
1175 return snd_pcm_hw_rule_add(runtime, cond, var,
1176 snd_pcm_hw_rule_step, (void *) step,
1180 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1182 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1184 static unsigned int pow2_sizes[] = {
1185 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1186 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1187 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1188 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1190 return snd_interval_list(hw_param_interval(params, rule->var),
1191 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1195 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1196 * @runtime: PCM runtime instance
1197 * @cond: condition bits
1198 * @var: hw_params variable to apply the power-of-2 constraint
1200 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1202 snd_pcm_hw_param_t var)
1204 return snd_pcm_hw_rule_add(runtime, cond, var,
1205 snd_pcm_hw_rule_pow2, NULL,
1209 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1211 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1212 snd_pcm_hw_param_t var)
1214 if (hw_is_mask(var)) {
1215 snd_mask_any(hw_param_mask(params, var));
1216 params->cmask |= 1 << var;
1217 params->rmask |= 1 << var;
1220 if (hw_is_interval(var)) {
1221 snd_interval_any(hw_param_interval(params, var));
1222 params->cmask |= 1 << var;
1223 params->rmask |= 1 << var;
1229 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1232 memset(params, 0, sizeof(*params));
1233 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1234 _snd_pcm_hw_param_any(params, k);
1235 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1236 _snd_pcm_hw_param_any(params, k);
1240 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1243 * snd_pcm_hw_param_value - return @params field @var value
1244 * @params: the hw_params instance
1245 * @var: parameter to retrieve
1246 * @dir: pointer to the direction (-1,0,1) or %NULL
1248 * Return the value for field @var if it's fixed in configuration space
1249 * defined by @params. Return -%EINVAL otherwise.
1251 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1252 snd_pcm_hw_param_t var, int *dir)
1254 if (hw_is_mask(var)) {
1255 const struct snd_mask *mask = hw_param_mask_c(params, var);
1256 if (!snd_mask_single(mask))
1260 return snd_mask_value(mask);
1262 if (hw_is_interval(var)) {
1263 const struct snd_interval *i = hw_param_interval_c(params, var);
1264 if (!snd_interval_single(i))
1268 return snd_interval_value(i);
1273 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1275 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1276 snd_pcm_hw_param_t var)
1278 if (hw_is_mask(var)) {
1279 snd_mask_none(hw_param_mask(params, var));
1280 params->cmask |= 1 << var;
1281 params->rmask |= 1 << var;
1282 } else if (hw_is_interval(var)) {
1283 snd_interval_none(hw_param_interval(params, var));
1284 params->cmask |= 1 << var;
1285 params->rmask |= 1 << var;
1291 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1293 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1294 snd_pcm_hw_param_t var)
1297 if (hw_is_mask(var))
1298 changed = snd_mask_refine_first(hw_param_mask(params, var));
1299 else if (hw_is_interval(var))
1300 changed = snd_interval_refine_first(hw_param_interval(params, var));
1304 params->cmask |= 1 << var;
1305 params->rmask |= 1 << var;
1312 * snd_pcm_hw_param_first - refine config space and return minimum value
1313 * @pcm: PCM instance
1314 * @params: the hw_params instance
1315 * @var: parameter to retrieve
1316 * @dir: pointer to the direction (-1,0,1) or %NULL
1318 * Inside configuration space defined by @params remove from @var all
1319 * values > minimum. Reduce configuration space accordingly.
1320 * Return the minimum.
1322 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1323 struct snd_pcm_hw_params *params,
1324 snd_pcm_hw_param_t var, int *dir)
1326 int changed = _snd_pcm_hw_param_first(params, var);
1329 if (params->rmask) {
1330 int err = snd_pcm_hw_refine(pcm, params);
1331 if (snd_BUG_ON(err < 0))
1334 return snd_pcm_hw_param_value(params, var, dir);
1337 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1339 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1340 snd_pcm_hw_param_t var)
1343 if (hw_is_mask(var))
1344 changed = snd_mask_refine_last(hw_param_mask(params, var));
1345 else if (hw_is_interval(var))
1346 changed = snd_interval_refine_last(hw_param_interval(params, var));
1350 params->cmask |= 1 << var;
1351 params->rmask |= 1 << var;
1358 * snd_pcm_hw_param_last - refine config space and return maximum value
1359 * @pcm: PCM instance
1360 * @params: the hw_params instance
1361 * @var: parameter to retrieve
1362 * @dir: pointer to the direction (-1,0,1) or %NULL
1364 * Inside configuration space defined by @params remove from @var all
1365 * values < maximum. Reduce configuration space accordingly.
1366 * Return the maximum.
1368 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1369 struct snd_pcm_hw_params *params,
1370 snd_pcm_hw_param_t var, int *dir)
1372 int changed = _snd_pcm_hw_param_last(params, var);
1375 if (params->rmask) {
1376 int err = snd_pcm_hw_refine(pcm, params);
1377 if (snd_BUG_ON(err < 0))
1380 return snd_pcm_hw_param_value(params, var, dir);
1383 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1386 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1387 * @pcm: PCM instance
1388 * @params: the hw_params instance
1390 * Choose one configuration from configuration space defined by @params.
1391 * The configuration chosen is that obtained fixing in this order:
1392 * first access, first format, first subformat, min channels,
1393 * min rate, min period time, max buffer size, min tick time
1395 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1396 struct snd_pcm_hw_params *params)
1398 static int vars[] = {
1399 SNDRV_PCM_HW_PARAM_ACCESS,
1400 SNDRV_PCM_HW_PARAM_FORMAT,
1401 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1402 SNDRV_PCM_HW_PARAM_CHANNELS,
1403 SNDRV_PCM_HW_PARAM_RATE,
1404 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1405 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1406 SNDRV_PCM_HW_PARAM_TICK_TIME,
1411 for (v = vars; *v != -1; v++) {
1412 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1413 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1415 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1416 if (snd_BUG_ON(err < 0))
1422 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1425 struct snd_pcm_runtime *runtime = substream->runtime;
1426 unsigned long flags;
1427 snd_pcm_stream_lock_irqsave(substream, flags);
1428 if (snd_pcm_running(substream) &&
1429 snd_pcm_update_hw_ptr(substream) >= 0)
1430 runtime->status->hw_ptr %= runtime->buffer_size;
1432 runtime->status->hw_ptr = 0;
1433 snd_pcm_stream_unlock_irqrestore(substream, flags);
1437 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1440 struct snd_pcm_channel_info *info = arg;
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1443 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1447 width = snd_pcm_format_physical_width(runtime->format);
1451 switch (runtime->access) {
1452 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1453 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1454 info->first = info->channel * width;
1455 info->step = runtime->channels * width;
1457 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1458 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1460 size_t size = runtime->dma_bytes / runtime->channels;
1461 info->first = info->channel * size * 8;
1473 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1474 * @substream: the pcm substream instance
1475 * @cmd: ioctl command
1476 * @arg: ioctl argument
1478 * Processes the generic ioctl commands for PCM.
1479 * Can be passed as the ioctl callback for PCM ops.
1481 * Returns zero if successful, or a negative error code on failure.
1483 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1484 unsigned int cmd, void *arg)
1487 case SNDRV_PCM_IOCTL1_INFO:
1489 case SNDRV_PCM_IOCTL1_RESET:
1490 return snd_pcm_lib_ioctl_reset(substream, arg);
1491 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1492 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1497 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1500 * snd_pcm_period_elapsed - update the pcm status for the next period
1501 * @substream: the pcm substream instance
1503 * This function is called from the interrupt handler when the
1504 * PCM has processed the period size. It will update the current
1505 * pointer, wake up sleepers, etc.
1507 * Even if more than one periods have elapsed since the last call, you
1508 * have to call this only once.
1510 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1512 struct snd_pcm_runtime *runtime;
1513 unsigned long flags;
1515 if (PCM_RUNTIME_CHECK(substream))
1517 runtime = substream->runtime;
1519 if (runtime->transfer_ack_begin)
1520 runtime->transfer_ack_begin(substream);
1522 snd_pcm_stream_lock_irqsave(substream, flags);
1523 if (!snd_pcm_running(substream) ||
1524 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1527 if (substream->timer_running)
1528 snd_timer_interrupt(substream->timer, 1);
1530 snd_pcm_stream_unlock_irqrestore(substream, flags);
1531 if (runtime->transfer_ack_end)
1532 runtime->transfer_ack_end(substream);
1533 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1536 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1539 * Wait until avail_min data becomes available
1540 * Returns a negative error code if any error occurs during operation.
1541 * The available space is stored on availp. When err = 0 and avail = 0
1542 * on the capture stream, it indicates the stream is in DRAINING state.
1544 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1545 snd_pcm_uframes_t *availp)
1547 struct snd_pcm_runtime *runtime = substream->runtime;
1548 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1551 snd_pcm_uframes_t avail = 0;
1554 init_waitqueue_entry(&wait, current);
1555 add_wait_queue(&runtime->sleep, &wait);
1557 if (signal_pending(current)) {
1561 set_current_state(TASK_INTERRUPTIBLE);
1562 snd_pcm_stream_unlock_irq(substream);
1563 tout = schedule_timeout(msecs_to_jiffies(10000));
1564 snd_pcm_stream_lock_irq(substream);
1565 switch (runtime->status->state) {
1566 case SNDRV_PCM_STATE_SUSPENDED:
1569 case SNDRV_PCM_STATE_XRUN:
1572 case SNDRV_PCM_STATE_DRAINING:
1576 avail = 0; /* indicate draining */
1578 case SNDRV_PCM_STATE_OPEN:
1579 case SNDRV_PCM_STATE_SETUP:
1580 case SNDRV_PCM_STATE_DISCONNECTED:
1585 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1586 is_playback ? "playback" : "capture");
1591 avail = snd_pcm_playback_avail(runtime);
1593 avail = snd_pcm_capture_avail(runtime);
1594 if (avail >= runtime->control->avail_min)
1598 remove_wait_queue(&runtime->sleep, &wait);
1603 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1605 unsigned long data, unsigned int off,
1606 snd_pcm_uframes_t frames)
1608 struct snd_pcm_runtime *runtime = substream->runtime;
1610 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1611 if (substream->ops->copy) {
1612 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1615 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1616 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1622 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1623 unsigned long data, unsigned int off,
1624 snd_pcm_uframes_t size);
1626 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1628 snd_pcm_uframes_t size,
1630 transfer_f transfer)
1632 struct snd_pcm_runtime *runtime = substream->runtime;
1633 snd_pcm_uframes_t xfer = 0;
1634 snd_pcm_uframes_t offset = 0;
1640 snd_pcm_stream_lock_irq(substream);
1641 switch (runtime->status->state) {
1642 case SNDRV_PCM_STATE_PREPARED:
1643 case SNDRV_PCM_STATE_RUNNING:
1644 case SNDRV_PCM_STATE_PAUSED:
1646 case SNDRV_PCM_STATE_XRUN:
1649 case SNDRV_PCM_STATE_SUSPENDED:
1658 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1659 snd_pcm_uframes_t avail;
1660 snd_pcm_uframes_t cont;
1661 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1662 snd_pcm_update_hw_ptr(substream);
1663 avail = snd_pcm_playback_avail(runtime);
1669 err = wait_for_avail_min(substream, &avail);
1673 frames = size > avail ? avail : size;
1674 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1677 if (snd_BUG_ON(!frames)) {
1678 snd_pcm_stream_unlock_irq(substream);
1681 appl_ptr = runtime->control->appl_ptr;
1682 appl_ofs = appl_ptr % runtime->buffer_size;
1683 snd_pcm_stream_unlock_irq(substream);
1684 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1686 snd_pcm_stream_lock_irq(substream);
1687 switch (runtime->status->state) {
1688 case SNDRV_PCM_STATE_XRUN:
1691 case SNDRV_PCM_STATE_SUSPENDED:
1698 if (appl_ptr >= runtime->boundary)
1699 appl_ptr -= runtime->boundary;
1700 runtime->control->appl_ptr = appl_ptr;
1701 if (substream->ops->ack)
1702 substream->ops->ack(substream);
1707 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1708 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1709 err = snd_pcm_start(substream);
1715 snd_pcm_stream_unlock_irq(substream);
1717 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1720 /* sanity-check for read/write methods */
1721 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1723 struct snd_pcm_runtime *runtime;
1724 if (PCM_RUNTIME_CHECK(substream))
1726 runtime = substream->runtime;
1727 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1729 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1734 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1736 struct snd_pcm_runtime *runtime;
1740 err = pcm_sanity_check(substream);
1743 runtime = substream->runtime;
1744 nonblock = !!(substream->f_flags & O_NONBLOCK);
1746 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1747 runtime->channels > 1)
1749 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1750 snd_pcm_lib_write_transfer);
1753 EXPORT_SYMBOL(snd_pcm_lib_write);
1755 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1757 unsigned long data, unsigned int off,
1758 snd_pcm_uframes_t frames)
1760 struct snd_pcm_runtime *runtime = substream->runtime;
1762 void __user **bufs = (void __user **)data;
1763 int channels = runtime->channels;
1765 if (substream->ops->copy) {
1766 if (snd_BUG_ON(!substream->ops->silence))
1768 for (c = 0; c < channels; ++c, ++bufs) {
1769 if (*bufs == NULL) {
1770 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1773 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1774 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1779 /* default transfer behaviour */
1780 size_t dma_csize = runtime->dma_bytes / channels;
1781 for (c = 0; c < channels; ++c, ++bufs) {
1782 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1783 if (*bufs == NULL) {
1784 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1786 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1787 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1795 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1797 snd_pcm_uframes_t frames)
1799 struct snd_pcm_runtime *runtime;
1803 err = pcm_sanity_check(substream);
1806 runtime = substream->runtime;
1807 nonblock = !!(substream->f_flags & O_NONBLOCK);
1809 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1811 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1812 nonblock, snd_pcm_lib_writev_transfer);
1815 EXPORT_SYMBOL(snd_pcm_lib_writev);
1817 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1819 unsigned long data, unsigned int off,
1820 snd_pcm_uframes_t frames)
1822 struct snd_pcm_runtime *runtime = substream->runtime;
1824 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1825 if (substream->ops->copy) {
1826 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1829 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1830 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1836 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1838 snd_pcm_uframes_t size,
1840 transfer_f transfer)
1842 struct snd_pcm_runtime *runtime = substream->runtime;
1843 snd_pcm_uframes_t xfer = 0;
1844 snd_pcm_uframes_t offset = 0;
1850 snd_pcm_stream_lock_irq(substream);
1851 switch (runtime->status->state) {
1852 case SNDRV_PCM_STATE_PREPARED:
1853 if (size >= runtime->start_threshold) {
1854 err = snd_pcm_start(substream);
1859 case SNDRV_PCM_STATE_DRAINING:
1860 case SNDRV_PCM_STATE_RUNNING:
1861 case SNDRV_PCM_STATE_PAUSED:
1863 case SNDRV_PCM_STATE_XRUN:
1866 case SNDRV_PCM_STATE_SUSPENDED:
1875 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1876 snd_pcm_uframes_t avail;
1877 snd_pcm_uframes_t cont;
1878 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1879 snd_pcm_update_hw_ptr(substream);
1880 avail = snd_pcm_capture_avail(runtime);
1882 if (runtime->status->state ==
1883 SNDRV_PCM_STATE_DRAINING) {
1884 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1891 err = wait_for_avail_min(substream, &avail);
1895 continue; /* draining */
1897 frames = size > avail ? avail : size;
1898 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1901 if (snd_BUG_ON(!frames)) {
1902 snd_pcm_stream_unlock_irq(substream);
1905 appl_ptr = runtime->control->appl_ptr;
1906 appl_ofs = appl_ptr % runtime->buffer_size;
1907 snd_pcm_stream_unlock_irq(substream);
1908 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1910 snd_pcm_stream_lock_irq(substream);
1911 switch (runtime->status->state) {
1912 case SNDRV_PCM_STATE_XRUN:
1915 case SNDRV_PCM_STATE_SUSPENDED:
1922 if (appl_ptr >= runtime->boundary)
1923 appl_ptr -= runtime->boundary;
1924 runtime->control->appl_ptr = appl_ptr;
1925 if (substream->ops->ack)
1926 substream->ops->ack(substream);
1933 snd_pcm_stream_unlock_irq(substream);
1935 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1938 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1940 struct snd_pcm_runtime *runtime;
1944 err = pcm_sanity_check(substream);
1947 runtime = substream->runtime;
1948 nonblock = !!(substream->f_flags & O_NONBLOCK);
1949 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1951 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1954 EXPORT_SYMBOL(snd_pcm_lib_read);
1956 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1958 unsigned long data, unsigned int off,
1959 snd_pcm_uframes_t frames)
1961 struct snd_pcm_runtime *runtime = substream->runtime;
1963 void __user **bufs = (void __user **)data;
1964 int channels = runtime->channels;
1966 if (substream->ops->copy) {
1967 for (c = 0; c < channels; ++c, ++bufs) {
1971 buf = *bufs + samples_to_bytes(runtime, off);
1972 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1976 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1977 for (c = 0; c < channels; ++c, ++bufs) {
1983 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1984 buf = *bufs + samples_to_bytes(runtime, off);
1985 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1992 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1994 snd_pcm_uframes_t frames)
1996 struct snd_pcm_runtime *runtime;
2000 err = pcm_sanity_check(substream);
2003 runtime = substream->runtime;
2004 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2007 nonblock = !!(substream->f_flags & O_NONBLOCK);
2008 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2010 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2013 EXPORT_SYMBOL(snd_pcm_lib_readv);