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 snd_assert(frames <= runtime->buffer_size, return);
91 ofs = runtime->silence_start % runtime->buffer_size;
93 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
94 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
95 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
96 if (substream->ops->silence) {
98 err = substream->ops->silence(substream, -1, ofs, transfer);
99 snd_assert(err >= 0, );
101 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
102 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
106 unsigned int channels = runtime->channels;
107 if (substream->ops->silence) {
108 for (c = 0; c < channels; ++c) {
110 err = substream->ops->silence(substream, c, ofs, transfer);
111 snd_assert(err >= 0, );
114 size_t dma_csize = runtime->dma_bytes / channels;
115 for (c = 0; c < channels; ++c) {
116 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
117 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
121 runtime->silence_filled += transfer;
127 static void xrun(struct snd_pcm_substream *substream)
129 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
130 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
131 if (substream->pstr->xrun_debug) {
132 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
133 substream->pcm->card->number,
134 substream->pcm->device,
135 substream->stream ? 'c' : 'p');
136 if (substream->pstr->xrun_debug > 1)
142 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
143 struct snd_pcm_runtime *runtime)
145 snd_pcm_uframes_t pos;
147 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
148 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
149 pos = substream->ops->pointer(substream);
150 if (pos == SNDRV_PCM_POS_XRUN)
151 return pos; /* XRUN */
152 #ifdef CONFIG_SND_DEBUG
153 if (pos >= runtime->buffer_size) {
154 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);
157 pos -= pos % runtime->min_align;
161 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
162 struct snd_pcm_runtime *runtime)
164 snd_pcm_uframes_t avail;
166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
167 avail = snd_pcm_playback_avail(runtime);
169 avail = snd_pcm_capture_avail(runtime);
170 if (avail > runtime->avail_max)
171 runtime->avail_max = avail;
172 if (avail >= runtime->stop_threshold) {
173 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
174 snd_pcm_drain_done(substream);
179 if (avail >= runtime->control->avail_min)
180 wake_up(&runtime->sleep);
184 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
186 struct snd_pcm_runtime *runtime = substream->runtime;
187 snd_pcm_uframes_t pos;
188 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
189 snd_pcm_sframes_t delta;
191 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
192 if (pos == SNDRV_PCM_POS_XRUN) {
196 if (runtime->period_size == runtime->buffer_size)
198 new_hw_ptr = runtime->hw_ptr_base + pos;
199 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
201 delta = hw_ptr_interrupt - new_hw_ptr;
203 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
204 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
205 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
206 snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
207 if (substream->pstr->xrun_debug > 1)
214 runtime->hw_ptr_base += runtime->buffer_size;
215 if (runtime->hw_ptr_base == runtime->boundary)
216 runtime->hw_ptr_base = 0;
217 new_hw_ptr = runtime->hw_ptr_base + pos;
220 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
221 runtime->silence_size > 0)
222 snd_pcm_playback_silence(substream, new_hw_ptr);
224 runtime->status->hw_ptr = new_hw_ptr;
225 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
227 return snd_pcm_update_hw_ptr_post(substream, runtime);
230 /* CAUTION: call it with irq disabled */
231 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
233 struct snd_pcm_runtime *runtime = substream->runtime;
234 snd_pcm_uframes_t pos;
235 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
236 snd_pcm_sframes_t delta;
238 old_hw_ptr = runtime->status->hw_ptr;
239 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
240 if (pos == SNDRV_PCM_POS_XRUN) {
244 new_hw_ptr = runtime->hw_ptr_base + pos;
246 delta = old_hw_ptr - new_hw_ptr;
248 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
249 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
250 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
251 snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
252 if (substream->pstr->xrun_debug > 1)
258 runtime->hw_ptr_base += runtime->buffer_size;
259 if (runtime->hw_ptr_base == runtime->boundary)
260 runtime->hw_ptr_base = 0;
261 new_hw_ptr = runtime->hw_ptr_base + pos;
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
264 runtime->silence_size > 0)
265 snd_pcm_playback_silence(substream, new_hw_ptr);
267 runtime->status->hw_ptr = new_hw_ptr;
269 return snd_pcm_update_hw_ptr_post(substream, runtime);
273 * snd_pcm_set_ops - set the PCM operators
274 * @pcm: the pcm instance
275 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
276 * @ops: the operator table
278 * Sets the given PCM operators to the pcm instance.
280 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
282 struct snd_pcm_str *stream = &pcm->streams[direction];
283 struct snd_pcm_substream *substream;
285 for (substream = stream->substream; substream != NULL; substream = substream->next)
286 substream->ops = ops;
289 EXPORT_SYMBOL(snd_pcm_set_ops);
292 * snd_pcm_sync - set the PCM sync id
293 * @substream: the pcm substream
295 * Sets the PCM sync identifier for the card.
297 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
299 struct snd_pcm_runtime *runtime = substream->runtime;
301 runtime->sync.id32[0] = substream->pcm->card->number;
302 runtime->sync.id32[1] = -1;
303 runtime->sync.id32[2] = -1;
304 runtime->sync.id32[3] = -1;
307 EXPORT_SYMBOL(snd_pcm_set_sync);
310 * Standard ioctl routine
313 static inline unsigned int div32(unsigned int a, unsigned int b,
324 static inline unsigned int div_down(unsigned int a, unsigned int b)
331 static inline unsigned int div_up(unsigned int a, unsigned int b)
343 static inline unsigned int mul(unsigned int a, unsigned int b)
347 if (div_down(UINT_MAX, a) < b)
352 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
353 unsigned int c, unsigned int *r)
355 u_int64_t n = (u_int64_t) a * b;
370 * snd_interval_refine - refine the interval value of configurator
371 * @i: the interval value to refine
372 * @v: the interval value to refer to
374 * Refines the interval value with the reference value.
375 * The interval is changed to the range satisfying both intervals.
376 * The interval status (min, max, integer, etc.) are evaluated.
378 * Returns non-zero if the value is changed, zero if not changed.
380 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
383 snd_assert(!snd_interval_empty(i), return -EINVAL);
384 if (i->min < v->min) {
386 i->openmin = v->openmin;
388 } else if (i->min == v->min && !i->openmin && v->openmin) {
392 if (i->max > v->max) {
394 i->openmax = v->openmax;
396 } else if (i->max == v->max && !i->openmax && v->openmax) {
400 if (!i->integer && v->integer) {
413 } else if (!i->openmin && !i->openmax && i->min == i->max)
415 if (snd_interval_checkempty(i)) {
416 snd_interval_none(i);
422 EXPORT_SYMBOL(snd_interval_refine);
424 static int snd_interval_refine_first(struct snd_interval *i)
426 snd_assert(!snd_interval_empty(i), return -EINVAL);
427 if (snd_interval_single(i))
430 i->openmax = i->openmin;
436 static int snd_interval_refine_last(struct snd_interval *i)
438 snd_assert(!snd_interval_empty(i), return -EINVAL);
439 if (snd_interval_single(i))
442 i->openmin = i->openmax;
448 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
450 if (a->empty || b->empty) {
451 snd_interval_none(c);
455 c->min = mul(a->min, b->min);
456 c->openmin = (a->openmin || b->openmin);
457 c->max = mul(a->max, b->max);
458 c->openmax = (a->openmax || b->openmax);
459 c->integer = (a->integer && b->integer);
463 * snd_interval_div - refine the interval value with division
470 * Returns non-zero if the value is changed, zero if not changed.
472 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
475 if (a->empty || b->empty) {
476 snd_interval_none(c);
480 c->min = div32(a->min, b->max, &r);
481 c->openmin = (r || a->openmin || b->openmax);
483 c->max = div32(a->max, b->min, &r);
488 c->openmax = (a->openmax || b->openmin);
497 * snd_interval_muldivk - refine the interval value
500 * @k: divisor (as integer)
505 * Returns non-zero if the value is changed, zero if not changed.
507 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
508 unsigned int k, struct snd_interval *c)
511 if (a->empty || b->empty) {
512 snd_interval_none(c);
516 c->min = muldiv32(a->min, b->min, k, &r);
517 c->openmin = (r || a->openmin || b->openmin);
518 c->max = muldiv32(a->max, b->max, k, &r);
523 c->openmax = (a->openmax || b->openmax);
528 * snd_interval_mulkdiv - refine the interval value
530 * @k: dividend 2 (as integer)
536 * Returns non-zero if the value is changed, zero if not changed.
538 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
539 const struct snd_interval *b, struct snd_interval *c)
542 if (a->empty || b->empty) {
543 snd_interval_none(c);
547 c->min = muldiv32(a->min, k, b->max, &r);
548 c->openmin = (r || a->openmin || b->openmax);
550 c->max = muldiv32(a->max, k, b->min, &r);
555 c->openmax = (a->openmax || b->openmin);
567 * snd_interval_ratnum - refine the interval value
568 * @i: interval to refine
569 * @rats_count: number of ratnum_t
570 * @rats: ratnum_t array
571 * @nump: pointer to store the resultant numerator
572 * @denp: pointer to store the resultant denominator
574 * Returns non-zero if the value is changed, zero if not changed.
576 int snd_interval_ratnum(struct snd_interval *i,
577 unsigned int rats_count, struct snd_ratnum *rats,
578 unsigned int *nump, unsigned int *denp)
580 unsigned int best_num, best_diff, best_den;
582 struct snd_interval t;
585 best_num = best_den = best_diff = 0;
586 for (k = 0; k < rats_count; ++k) {
587 unsigned int num = rats[k].num;
589 unsigned int q = i->min;
593 den = div_down(num, q);
594 if (den < rats[k].den_min)
596 if (den > rats[k].den_max)
597 den = rats[k].den_max;
600 r = (den - rats[k].den_min) % rats[k].den_step;
604 diff = num - q * den;
606 diff * best_den < best_diff * den) {
616 t.min = div_down(best_num, best_den);
617 t.openmin = !!(best_num % best_den);
619 best_num = best_den = best_diff = 0;
620 for (k = 0; k < rats_count; ++k) {
621 unsigned int num = rats[k].num;
623 unsigned int q = i->max;
629 den = div_up(num, q);
630 if (den > rats[k].den_max)
632 if (den < rats[k].den_min)
633 den = rats[k].den_min;
636 r = (den - rats[k].den_min) % rats[k].den_step;
638 den += rats[k].den_step - r;
640 diff = q * den - num;
642 diff * best_den < best_diff * den) {
652 t.max = div_up(best_num, best_den);
653 t.openmax = !!(best_num % best_den);
655 err = snd_interval_refine(i, &t);
659 if (snd_interval_single(i)) {
668 EXPORT_SYMBOL(snd_interval_ratnum);
671 * snd_interval_ratden - refine the interval value
672 * @i: interval to refine
673 * @rats_count: number of struct ratden
674 * @rats: struct ratden array
675 * @nump: pointer to store the resultant numerator
676 * @denp: pointer to store the resultant denominator
678 * Returns non-zero if the value is changed, zero if not changed.
680 static int snd_interval_ratden(struct snd_interval *i,
681 unsigned int rats_count, struct snd_ratden *rats,
682 unsigned int *nump, unsigned int *denp)
684 unsigned int best_num, best_diff, best_den;
686 struct snd_interval t;
689 best_num = best_den = best_diff = 0;
690 for (k = 0; k < rats_count; ++k) {
692 unsigned int den = rats[k].den;
693 unsigned int q = i->min;
696 if (num > rats[k].num_max)
698 if (num < rats[k].num_min)
699 num = rats[k].num_max;
702 r = (num - rats[k].num_min) % rats[k].num_step;
704 num += rats[k].num_step - r;
706 diff = num - q * den;
708 diff * best_den < best_diff * den) {
718 t.min = div_down(best_num, best_den);
719 t.openmin = !!(best_num % best_den);
721 best_num = best_den = best_diff = 0;
722 for (k = 0; k < rats_count; ++k) {
724 unsigned int den = rats[k].den;
725 unsigned int q = i->max;
728 if (num < rats[k].num_min)
730 if (num > rats[k].num_max)
731 num = rats[k].num_max;
734 r = (num - rats[k].num_min) % rats[k].num_step;
738 diff = q * den - num;
740 diff * best_den < best_diff * den) {
750 t.max = div_up(best_num, best_den);
751 t.openmax = !!(best_num % best_den);
753 err = snd_interval_refine(i, &t);
757 if (snd_interval_single(i)) {
767 * snd_interval_list - refine the interval value from the list
768 * @i: the interval value to refine
769 * @count: the number of elements in the list
770 * @list: the value list
771 * @mask: the bit-mask to evaluate
773 * Refines the interval value from the list.
774 * When mask is non-zero, only the elements corresponding to bit 1 are
777 * Returns non-zero if the value is changed, zero if not changed.
779 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
788 for (k = 0; k < count; k++) {
789 if (mask && !(mask & (1 << k)))
791 if (i->min == list[k] && !i->openmin)
793 if (i->min < list[k]) {
803 for (k = count; k-- > 0;) {
804 if (mask && !(mask & (1 << k)))
806 if (i->max == list[k] && !i->openmax)
808 if (i->max > list[k]) {
818 if (snd_interval_checkempty(i)) {
825 EXPORT_SYMBOL(snd_interval_list);
827 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
831 n = (i->min - min) % step;
832 if (n != 0 || i->openmin) {
836 n = (i->max - min) % step;
837 if (n != 0 || i->openmax) {
841 if (snd_interval_checkempty(i)) {
848 /* Info constraints helpers */
851 * snd_pcm_hw_rule_add - add the hw-constraint rule
852 * @runtime: the pcm runtime instance
853 * @cond: condition bits
854 * @var: the variable to evaluate
855 * @func: the evaluation function
856 * @private: the private data pointer passed to function
857 * @dep: the dependent variables
859 * Returns zero if successful, or a negative error code on failure.
861 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
863 snd_pcm_hw_rule_func_t func, void *private,
866 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
867 struct snd_pcm_hw_rule *c;
871 if (constrs->rules_num >= constrs->rules_all) {
872 struct snd_pcm_hw_rule *new;
873 unsigned int new_rules = constrs->rules_all + 16;
874 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
877 if (constrs->rules) {
878 memcpy(new, constrs->rules,
879 constrs->rules_num * sizeof(*c));
880 kfree(constrs->rules);
882 constrs->rules = new;
883 constrs->rules_all = new_rules;
885 c = &constrs->rules[constrs->rules_num];
889 c->private = private;
892 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
896 dep = va_arg(args, int);
898 constrs->rules_num++;
903 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
906 * snd_pcm_hw_constraint_mask
907 * @runtime: PCM runtime instance
908 * @var: hw_params variable to apply the mask
909 * @mask: the bitmap mask
911 * Apply the constraint of the given bitmap mask to a mask parameter.
913 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
916 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
917 struct snd_mask *maskp = constrs_mask(constrs, var);
918 *maskp->bits &= mask;
919 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
920 if (*maskp->bits == 0)
926 * snd_pcm_hw_constraint_mask64
927 * @runtime: PCM runtime instance
928 * @var: hw_params variable to apply the mask
929 * @mask: the 64bit bitmap mask
931 * Apply the constraint of the given bitmap mask to a mask parameter.
933 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
936 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
937 struct snd_mask *maskp = constrs_mask(constrs, var);
938 maskp->bits[0] &= (u_int32_t)mask;
939 maskp->bits[1] &= (u_int32_t)(mask >> 32);
940 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
941 if (! maskp->bits[0] && ! maskp->bits[1])
947 * snd_pcm_hw_constraint_integer
948 * @runtime: PCM runtime instance
949 * @var: hw_params variable to apply the integer constraint
951 * Apply the constraint of integer to an interval parameter.
953 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
955 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
956 return snd_interval_setinteger(constrs_interval(constrs, var));
959 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
962 * snd_pcm_hw_constraint_minmax
963 * @runtime: PCM runtime instance
964 * @var: hw_params variable to apply the range
965 * @min: the minimal value
966 * @max: the maximal value
968 * Apply the min/max range constraint to an interval parameter.
970 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
971 unsigned int min, unsigned int max)
973 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
974 struct snd_interval t;
977 t.openmin = t.openmax = 0;
979 return snd_interval_refine(constrs_interval(constrs, var), &t);
982 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
984 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
985 struct snd_pcm_hw_rule *rule)
987 struct snd_pcm_hw_constraint_list *list = rule->private;
988 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
993 * snd_pcm_hw_constraint_list
994 * @runtime: PCM runtime instance
995 * @cond: condition bits
996 * @var: hw_params variable to apply the list constraint
999 * Apply the list of constraints to an interval parameter.
1001 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1003 snd_pcm_hw_param_t var,
1004 struct snd_pcm_hw_constraint_list *l)
1006 return snd_pcm_hw_rule_add(runtime, cond, var,
1007 snd_pcm_hw_rule_list, l,
1011 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1013 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1014 struct snd_pcm_hw_rule *rule)
1016 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1017 unsigned int num = 0, den = 0;
1019 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1020 r->nrats, r->rats, &num, &den);
1021 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1022 params->rate_num = num;
1023 params->rate_den = den;
1029 * snd_pcm_hw_constraint_ratnums
1030 * @runtime: PCM runtime instance
1031 * @cond: condition bits
1032 * @var: hw_params variable to apply the ratnums constraint
1033 * @r: struct snd_ratnums constriants
1035 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1037 snd_pcm_hw_param_t var,
1038 struct snd_pcm_hw_constraint_ratnums *r)
1040 return snd_pcm_hw_rule_add(runtime, cond, var,
1041 snd_pcm_hw_rule_ratnums, r,
1045 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1047 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1048 struct snd_pcm_hw_rule *rule)
1050 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1051 unsigned int num = 0, den = 0;
1052 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1053 r->nrats, r->rats, &num, &den);
1054 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1055 params->rate_num = num;
1056 params->rate_den = den;
1062 * snd_pcm_hw_constraint_ratdens
1063 * @runtime: PCM runtime instance
1064 * @cond: condition bits
1065 * @var: hw_params variable to apply the ratdens constraint
1066 * @r: struct snd_ratdens constriants
1068 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1070 snd_pcm_hw_param_t var,
1071 struct snd_pcm_hw_constraint_ratdens *r)
1073 return snd_pcm_hw_rule_add(runtime, cond, var,
1074 snd_pcm_hw_rule_ratdens, r,
1078 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1080 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1081 struct snd_pcm_hw_rule *rule)
1083 unsigned int l = (unsigned long) rule->private;
1084 int width = l & 0xffff;
1085 unsigned int msbits = l >> 16;
1086 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1087 if (snd_interval_single(i) && snd_interval_value(i) == width)
1088 params->msbits = msbits;
1093 * snd_pcm_hw_constraint_msbits
1094 * @runtime: PCM runtime instance
1095 * @cond: condition bits
1096 * @width: sample bits width
1097 * @msbits: msbits width
1099 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1102 unsigned int msbits)
1104 unsigned long l = (msbits << 16) | width;
1105 return snd_pcm_hw_rule_add(runtime, cond, -1,
1106 snd_pcm_hw_rule_msbits,
1108 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1111 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1113 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1114 struct snd_pcm_hw_rule *rule)
1116 unsigned long step = (unsigned long) rule->private;
1117 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1121 * snd_pcm_hw_constraint_step
1122 * @runtime: PCM runtime instance
1123 * @cond: condition bits
1124 * @var: hw_params variable to apply the step constraint
1127 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1129 snd_pcm_hw_param_t var,
1132 return snd_pcm_hw_rule_add(runtime, cond, var,
1133 snd_pcm_hw_rule_step, (void *) step,
1137 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1139 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1141 static unsigned int pow2_sizes[] = {
1142 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1143 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1144 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1145 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1147 return snd_interval_list(hw_param_interval(params, rule->var),
1148 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1152 * snd_pcm_hw_constraint_pow2
1153 * @runtime: PCM runtime instance
1154 * @cond: condition bits
1155 * @var: hw_params variable to apply the power-of-2 constraint
1157 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1159 snd_pcm_hw_param_t var)
1161 return snd_pcm_hw_rule_add(runtime, cond, var,
1162 snd_pcm_hw_rule_pow2, NULL,
1166 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1168 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1169 snd_pcm_hw_param_t var)
1171 if (hw_is_mask(var)) {
1172 snd_mask_any(hw_param_mask(params, var));
1173 params->cmask |= 1 << var;
1174 params->rmask |= 1 << var;
1177 if (hw_is_interval(var)) {
1178 snd_interval_any(hw_param_interval(params, var));
1179 params->cmask |= 1 << var;
1180 params->rmask |= 1 << var;
1186 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1189 memset(params, 0, sizeof(*params));
1190 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1191 _snd_pcm_hw_param_any(params, k);
1192 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1193 _snd_pcm_hw_param_any(params, k);
1197 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1200 * snd_pcm_hw_param_value
1201 * @params: the hw_params instance
1202 * @var: parameter to retrieve
1203 * @dir: pointer to the direction (-1,0,1) or NULL
1205 * Return the value for field PAR if it's fixed in configuration space
1206 * defined by PARAMS. Return -EINVAL otherwise
1208 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1209 snd_pcm_hw_param_t var, int *dir)
1211 if (hw_is_mask(var)) {
1212 const struct snd_mask *mask = hw_param_mask_c(params, var);
1213 if (!snd_mask_single(mask))
1217 return snd_mask_value(mask);
1219 if (hw_is_interval(var)) {
1220 const struct snd_interval *i = hw_param_interval_c(params, var);
1221 if (!snd_interval_single(i))
1225 return snd_interval_value(i);
1230 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1232 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1233 snd_pcm_hw_param_t var)
1235 if (hw_is_mask(var)) {
1236 snd_mask_none(hw_param_mask(params, var));
1237 params->cmask |= 1 << var;
1238 params->rmask |= 1 << var;
1239 } else if (hw_is_interval(var)) {
1240 snd_interval_none(hw_param_interval(params, var));
1241 params->cmask |= 1 << var;
1242 params->rmask |= 1 << var;
1248 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1250 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1251 snd_pcm_hw_param_t var)
1254 if (hw_is_mask(var))
1255 changed = snd_mask_refine_first(hw_param_mask(params, var));
1256 else if (hw_is_interval(var))
1257 changed = snd_interval_refine_first(hw_param_interval(params, var));
1261 params->cmask |= 1 << var;
1262 params->rmask |= 1 << var;
1269 * snd_pcm_hw_param_first
1270 * @pcm: PCM instance
1271 * @params: the hw_params instance
1272 * @var: parameter to retrieve
1273 * @dir: pointer to the direction (-1,0,1) or NULL
1275 * Inside configuration space defined by PARAMS remove from PAR all
1276 * values > minimum. Reduce configuration space accordingly.
1277 * Return the minimum.
1279 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1280 struct snd_pcm_hw_params *params,
1281 snd_pcm_hw_param_t var, int *dir)
1283 int changed = _snd_pcm_hw_param_first(params, var);
1286 if (params->rmask) {
1287 int err = snd_pcm_hw_refine(pcm, params);
1288 snd_assert(err >= 0, return err);
1290 return snd_pcm_hw_param_value(params, var, dir);
1293 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1295 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1296 snd_pcm_hw_param_t var)
1299 if (hw_is_mask(var))
1300 changed = snd_mask_refine_last(hw_param_mask(params, var));
1301 else if (hw_is_interval(var))
1302 changed = snd_interval_refine_last(hw_param_interval(params, var));
1306 params->cmask |= 1 << var;
1307 params->rmask |= 1 << var;
1314 * snd_pcm_hw_param_last
1315 * @pcm: PCM instance
1316 * @params: the hw_params instance
1317 * @var: parameter to retrieve
1318 * @dir: pointer to the direction (-1,0,1) or NULL
1320 * Inside configuration space defined by PARAMS remove from PAR all
1321 * values < maximum. Reduce configuration space accordingly.
1322 * Return the maximum.
1324 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1325 struct snd_pcm_hw_params *params,
1326 snd_pcm_hw_param_t var, int *dir)
1328 int changed = _snd_pcm_hw_param_last(params, var);
1331 if (params->rmask) {
1332 int err = snd_pcm_hw_refine(pcm, params);
1333 snd_assert(err >= 0, return err);
1335 return snd_pcm_hw_param_value(params, var, dir);
1338 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1341 * snd_pcm_hw_param_choose
1342 * @pcm: PCM instance
1343 * @params: the hw_params instance
1345 * Choose one configuration from configuration space defined by PARAMS
1346 * The configuration chosen is that obtained fixing in this order:
1347 * first access, first format, first subformat, min channels,
1348 * min rate, min period time, max buffer size, min tick time
1350 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1351 struct snd_pcm_hw_params *params)
1353 static int vars[] = {
1354 SNDRV_PCM_HW_PARAM_ACCESS,
1355 SNDRV_PCM_HW_PARAM_FORMAT,
1356 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1357 SNDRV_PCM_HW_PARAM_CHANNELS,
1358 SNDRV_PCM_HW_PARAM_RATE,
1359 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1360 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1361 SNDRV_PCM_HW_PARAM_TICK_TIME,
1366 for (v = vars; *v != -1; v++) {
1367 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1368 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1370 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1371 snd_assert(err >= 0, return err);
1376 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1379 struct snd_pcm_runtime *runtime = substream->runtime;
1380 unsigned long flags;
1381 snd_pcm_stream_lock_irqsave(substream, flags);
1382 if (snd_pcm_running(substream) &&
1383 snd_pcm_update_hw_ptr(substream) >= 0)
1384 runtime->status->hw_ptr %= runtime->buffer_size;
1386 runtime->status->hw_ptr = 0;
1387 snd_pcm_stream_unlock_irqrestore(substream, flags);
1391 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1394 struct snd_pcm_channel_info *info = arg;
1395 struct snd_pcm_runtime *runtime = substream->runtime;
1397 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1401 width = snd_pcm_format_physical_width(runtime->format);
1405 switch (runtime->access) {
1406 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1407 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1408 info->first = info->channel * width;
1409 info->step = runtime->channels * width;
1411 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1412 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1414 size_t size = runtime->dma_bytes / runtime->channels;
1415 info->first = info->channel * size * 8;
1427 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1428 * @substream: the pcm substream instance
1429 * @cmd: ioctl command
1430 * @arg: ioctl argument
1432 * Processes the generic ioctl commands for PCM.
1433 * Can be passed as the ioctl callback for PCM ops.
1435 * Returns zero if successful, or a negative error code on failure.
1437 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1438 unsigned int cmd, void *arg)
1441 case SNDRV_PCM_IOCTL1_INFO:
1443 case SNDRV_PCM_IOCTL1_RESET:
1444 return snd_pcm_lib_ioctl_reset(substream, arg);
1445 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1446 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1451 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1454 * snd_pcm_period_elapsed - update the pcm status for the next period
1455 * @substream: the pcm substream instance
1457 * This function is called from the interrupt handler when the
1458 * PCM has processed the period size. It will update the current
1459 * pointer, wake up sleepers, etc.
1461 * Even if more than one periods have elapsed since the last call, you
1462 * have to call this only once.
1464 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1466 struct snd_pcm_runtime *runtime;
1467 unsigned long flags;
1469 snd_assert(substream != NULL, return);
1470 runtime = substream->runtime;
1471 snd_assert(runtime != NULL, return);
1473 if (runtime->transfer_ack_begin)
1474 runtime->transfer_ack_begin(substream);
1476 snd_pcm_stream_lock_irqsave(substream, flags);
1477 if (!snd_pcm_running(substream) ||
1478 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1481 if (substream->timer_running)
1482 snd_timer_interrupt(substream->timer, 1);
1484 snd_pcm_stream_unlock_irqrestore(substream, flags);
1485 if (runtime->transfer_ack_end)
1486 runtime->transfer_ack_end(substream);
1487 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1490 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1493 * Wait until avail_min data becomes available
1494 * Returns a negative error code if any error occurs during operation.
1495 * The available space is stored on availp. When err = 0 and avail = 0
1496 * on the capture stream, it indicates the stream is in DRAINING state.
1498 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1499 snd_pcm_uframes_t *availp)
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1505 snd_pcm_uframes_t avail = 0;
1508 init_waitqueue_entry(&wait, current);
1509 add_wait_queue(&runtime->sleep, &wait);
1511 if (signal_pending(current)) {
1515 set_current_state(TASK_INTERRUPTIBLE);
1516 snd_pcm_stream_unlock_irq(substream);
1517 tout = schedule_timeout(msecs_to_jiffies(10000));
1518 snd_pcm_stream_lock_irq(substream);
1519 switch (runtime->status->state) {
1520 case SNDRV_PCM_STATE_SUSPENDED:
1523 case SNDRV_PCM_STATE_XRUN:
1526 case SNDRV_PCM_STATE_DRAINING:
1530 avail = 0; /* indicate draining */
1532 case SNDRV_PCM_STATE_OPEN:
1533 case SNDRV_PCM_STATE_SETUP:
1534 case SNDRV_PCM_STATE_DISCONNECTED:
1539 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1540 is_playback ? "playback" : "capture");
1545 avail = snd_pcm_playback_avail(runtime);
1547 avail = snd_pcm_capture_avail(runtime);
1548 if (avail >= runtime->control->avail_min)
1552 remove_wait_queue(&runtime->sleep, &wait);
1557 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1559 unsigned long data, unsigned int off,
1560 snd_pcm_uframes_t frames)
1562 struct snd_pcm_runtime *runtime = substream->runtime;
1564 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1565 if (substream->ops->copy) {
1566 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1569 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1570 snd_assert(runtime->dma_area, return -EFAULT);
1571 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1577 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1578 unsigned long data, unsigned int off,
1579 snd_pcm_uframes_t size);
1581 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1583 snd_pcm_uframes_t size,
1585 transfer_f transfer)
1587 struct snd_pcm_runtime *runtime = substream->runtime;
1588 snd_pcm_uframes_t xfer = 0;
1589 snd_pcm_uframes_t offset = 0;
1595 snd_pcm_stream_lock_irq(substream);
1596 switch (runtime->status->state) {
1597 case SNDRV_PCM_STATE_PREPARED:
1598 case SNDRV_PCM_STATE_RUNNING:
1599 case SNDRV_PCM_STATE_PAUSED:
1601 case SNDRV_PCM_STATE_XRUN:
1604 case SNDRV_PCM_STATE_SUSPENDED:
1613 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1614 snd_pcm_uframes_t avail;
1615 snd_pcm_uframes_t cont;
1616 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1617 snd_pcm_update_hw_ptr(substream);
1618 avail = snd_pcm_playback_avail(runtime);
1624 err = wait_for_avail_min(substream, &avail);
1628 frames = size > avail ? avail : size;
1629 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1632 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1633 appl_ptr = runtime->control->appl_ptr;
1634 appl_ofs = appl_ptr % runtime->buffer_size;
1635 snd_pcm_stream_unlock_irq(substream);
1636 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1638 snd_pcm_stream_lock_irq(substream);
1639 switch (runtime->status->state) {
1640 case SNDRV_PCM_STATE_XRUN:
1643 case SNDRV_PCM_STATE_SUSPENDED:
1650 if (appl_ptr >= runtime->boundary)
1651 appl_ptr -= runtime->boundary;
1652 runtime->control->appl_ptr = appl_ptr;
1653 if (substream->ops->ack)
1654 substream->ops->ack(substream);
1659 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1660 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1661 err = snd_pcm_start(substream);
1667 snd_pcm_stream_unlock_irq(substream);
1669 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1672 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1674 struct snd_pcm_runtime *runtime;
1677 snd_assert(substream != NULL, return -ENXIO);
1678 runtime = substream->runtime;
1679 snd_assert(runtime != NULL, return -ENXIO);
1680 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1681 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1684 nonblock = !!(substream->f_flags & O_NONBLOCK);
1686 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1687 runtime->channels > 1)
1689 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1690 snd_pcm_lib_write_transfer);
1693 EXPORT_SYMBOL(snd_pcm_lib_write);
1695 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1697 unsigned long data, unsigned int off,
1698 snd_pcm_uframes_t frames)
1700 struct snd_pcm_runtime *runtime = substream->runtime;
1702 void __user **bufs = (void __user **)data;
1703 int channels = runtime->channels;
1705 if (substream->ops->copy) {
1706 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1707 for (c = 0; c < channels; ++c, ++bufs) {
1708 if (*bufs == NULL) {
1709 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1712 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1713 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1718 /* default transfer behaviour */
1719 size_t dma_csize = runtime->dma_bytes / channels;
1720 snd_assert(runtime->dma_area, return -EFAULT);
1721 for (c = 0; c < channels; ++c, ++bufs) {
1722 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1723 if (*bufs == NULL) {
1724 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1726 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1727 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1735 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1737 snd_pcm_uframes_t frames)
1739 struct snd_pcm_runtime *runtime;
1742 snd_assert(substream != NULL, return -ENXIO);
1743 runtime = substream->runtime;
1744 snd_assert(runtime != NULL, return -ENXIO);
1745 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1746 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1749 nonblock = !!(substream->f_flags & O_NONBLOCK);
1751 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1753 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1754 nonblock, snd_pcm_lib_writev_transfer);
1757 EXPORT_SYMBOL(snd_pcm_lib_writev);
1759 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1761 unsigned long data, unsigned int off,
1762 snd_pcm_uframes_t frames)
1764 struct snd_pcm_runtime *runtime = substream->runtime;
1766 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1767 if (substream->ops->copy) {
1768 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1771 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1772 snd_assert(runtime->dma_area, return -EFAULT);
1773 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1779 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1781 snd_pcm_uframes_t size,
1783 transfer_f transfer)
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1786 snd_pcm_uframes_t xfer = 0;
1787 snd_pcm_uframes_t offset = 0;
1793 snd_pcm_stream_lock_irq(substream);
1794 switch (runtime->status->state) {
1795 case SNDRV_PCM_STATE_PREPARED:
1796 if (size >= runtime->start_threshold) {
1797 err = snd_pcm_start(substream);
1802 case SNDRV_PCM_STATE_DRAINING:
1803 case SNDRV_PCM_STATE_RUNNING:
1804 case SNDRV_PCM_STATE_PAUSED:
1806 case SNDRV_PCM_STATE_XRUN:
1809 case SNDRV_PCM_STATE_SUSPENDED:
1818 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1819 snd_pcm_uframes_t avail;
1820 snd_pcm_uframes_t cont;
1821 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1822 snd_pcm_update_hw_ptr(substream);
1823 avail = snd_pcm_capture_avail(runtime);
1825 if (runtime->status->state ==
1826 SNDRV_PCM_STATE_DRAINING) {
1827 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1834 err = wait_for_avail_min(substream, &avail);
1838 continue; /* draining */
1840 frames = size > avail ? avail : size;
1841 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1844 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1845 appl_ptr = runtime->control->appl_ptr;
1846 appl_ofs = appl_ptr % runtime->buffer_size;
1847 snd_pcm_stream_unlock_irq(substream);
1848 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1850 snd_pcm_stream_lock_irq(substream);
1851 switch (runtime->status->state) {
1852 case SNDRV_PCM_STATE_XRUN:
1855 case SNDRV_PCM_STATE_SUSPENDED:
1862 if (appl_ptr >= runtime->boundary)
1863 appl_ptr -= runtime->boundary;
1864 runtime->control->appl_ptr = appl_ptr;
1865 if (substream->ops->ack)
1866 substream->ops->ack(substream);
1873 snd_pcm_stream_unlock_irq(substream);
1875 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1878 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1880 struct snd_pcm_runtime *runtime;
1883 snd_assert(substream != NULL, return -ENXIO);
1884 runtime = substream->runtime;
1885 snd_assert(runtime != NULL, return -ENXIO);
1886 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1887 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1890 nonblock = !!(substream->f_flags & O_NONBLOCK);
1891 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1893 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1896 EXPORT_SYMBOL(snd_pcm_lib_read);
1898 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1900 unsigned long data, unsigned int off,
1901 snd_pcm_uframes_t frames)
1903 struct snd_pcm_runtime *runtime = substream->runtime;
1905 void __user **bufs = (void __user **)data;
1906 int channels = runtime->channels;
1908 if (substream->ops->copy) {
1909 for (c = 0; c < channels; ++c, ++bufs) {
1913 buf = *bufs + samples_to_bytes(runtime, off);
1914 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1918 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1919 snd_assert(runtime->dma_area, return -EFAULT);
1920 for (c = 0; c < channels; ++c, ++bufs) {
1926 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1927 buf = *bufs + samples_to_bytes(runtime, off);
1928 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1935 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1937 snd_pcm_uframes_t frames)
1939 struct snd_pcm_runtime *runtime;
1942 snd_assert(substream != NULL, return -ENXIO);
1943 runtime = substream->runtime;
1944 snd_assert(runtime != NULL, return -ENXIO);
1945 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1946 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1949 nonblock = !!(substream->f_flags & O_NONBLOCK);
1950 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1952 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
1955 EXPORT_SYMBOL(snd_pcm_lib_readv);