2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/info.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/timer.h>
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
43 struct snd_pcm_runtime *runtime = substream->runtime;
44 snd_pcm_uframes_t frames, ofs, transfer;
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
58 if (runtime->silence_filled >= runtime->buffer_size)
60 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
81 runtime->silence_start = new_hw_ptr;
83 runtime->silence_start = ofs;
86 frames = runtime->buffer_size - runtime->silence_filled;
88 if (snd_BUG_ON(frames > runtime->buffer_size))
92 ofs = runtime->silence_start % runtime->buffer_size;
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
99 err = substream->ops->silence(substream, -1, ofs, transfer);
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
111 err = substream->ops->silence(substream, c, ofs, transfer);
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 runtime->silence_filled += transfer;
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129 #define xrun_debug(substream) ((substream)->pstr->xrun_debug)
131 #define xrun_debug(substream) 0
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
139 static void xrun(struct snd_pcm_substream *substream)
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
142 if (xrun_debug(substream)) {
143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
147 dump_stack_on_xrun(substream);
151 static snd_pcm_uframes_t
152 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
155 snd_pcm_uframes_t pos;
157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
162 #ifdef CONFIG_SND_DEBUG
163 if (pos >= runtime->buffer_size) {
164 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);
167 pos -= pos % runtime->min_align;
171 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
172 struct snd_pcm_runtime *runtime)
174 snd_pcm_uframes_t avail;
176 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
177 avail = snd_pcm_playback_avail(runtime);
179 avail = snd_pcm_capture_avail(runtime);
180 if (avail > runtime->avail_max)
181 runtime->avail_max = avail;
182 if (avail >= runtime->stop_threshold) {
183 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
184 snd_pcm_drain_done(substream);
189 if (avail >= runtime->control->avail_min)
190 wake_up(&runtime->sleep);
194 #define hw_ptr_error(substream, fmt, args...) \
196 if (xrun_debug(substream)) { \
197 if (printk_ratelimit()) { \
198 snd_printd("PCM: " fmt, ##args); \
200 dump_stack_on_xrun(substream); \
204 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 snd_pcm_uframes_t pos;
208 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
209 snd_pcm_sframes_t delta;
211 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
212 if (pos == SNDRV_PCM_POS_XRUN) {
216 hw_base = runtime->hw_ptr_base;
217 new_hw_ptr = hw_base + pos;
218 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
219 delta = new_hw_ptr - hw_ptr_interrupt;
220 if (hw_ptr_interrupt == runtime->boundary)
221 hw_ptr_interrupt = 0;
223 delta += runtime->buffer_size;
225 hw_ptr_error(substream,
226 "Unexpected hw_pointer value "
227 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
228 substream->stream, (long)pos,
229 (long)hw_ptr_interrupt);
230 /* rebase to interrupt position */
231 hw_base = new_hw_ptr = hw_ptr_interrupt;
234 hw_base += runtime->buffer_size;
235 if (hw_base == runtime->boundary)
237 new_hw_ptr = hw_base + pos;
240 if (delta > runtime->period_size) {
241 hw_ptr_error(substream,
243 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
244 substream->stream, (long)delta,
245 (long)hw_ptr_interrupt);
246 /* rebase hw_ptr_interrupt */
248 new_hw_ptr - new_hw_ptr % runtime->period_size;
250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
251 runtime->silence_size > 0)
252 snd_pcm_playback_silence(substream, new_hw_ptr);
254 runtime->hw_ptr_base = hw_base;
255 runtime->status->hw_ptr = new_hw_ptr;
256 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
258 return snd_pcm_update_hw_ptr_post(substream, runtime);
261 /* CAUTION: call it with irq disabled */
262 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
264 struct snd_pcm_runtime *runtime = substream->runtime;
265 snd_pcm_uframes_t pos;
266 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
267 snd_pcm_sframes_t delta;
269 old_hw_ptr = runtime->status->hw_ptr;
270 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
271 if (pos == SNDRV_PCM_POS_XRUN) {
275 hw_base = runtime->hw_ptr_base;
276 new_hw_ptr = hw_base + pos;
278 delta = new_hw_ptr - old_hw_ptr;
280 delta += runtime->buffer_size;
282 hw_ptr_error(substream,
283 "Unexpected hw_pointer value [2] "
284 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
285 substream->stream, (long)pos,
289 hw_base += runtime->buffer_size;
290 if (hw_base == runtime->boundary)
292 new_hw_ptr = hw_base + pos;
294 if (delta > runtime->period_size && runtime->periods > 1) {
295 hw_ptr_error(substream,
297 "(pos=%ld, delta=%ld, period=%ld)\n",
298 (long)pos, (long)delta,
299 (long)runtime->period_size);
302 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
303 runtime->silence_size > 0)
304 snd_pcm_playback_silence(substream, new_hw_ptr);
306 runtime->hw_ptr_base = hw_base;
307 runtime->status->hw_ptr = new_hw_ptr;
309 return snd_pcm_update_hw_ptr_post(substream, runtime);
313 * snd_pcm_set_ops - set the PCM operators
314 * @pcm: the pcm instance
315 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
316 * @ops: the operator table
318 * Sets the given PCM operators to the pcm instance.
320 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
322 struct snd_pcm_str *stream = &pcm->streams[direction];
323 struct snd_pcm_substream *substream;
325 for (substream = stream->substream; substream != NULL; substream = substream->next)
326 substream->ops = ops;
329 EXPORT_SYMBOL(snd_pcm_set_ops);
332 * snd_pcm_sync - set the PCM sync id
333 * @substream: the pcm substream
335 * Sets the PCM sync identifier for the card.
337 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
339 struct snd_pcm_runtime *runtime = substream->runtime;
341 runtime->sync.id32[0] = substream->pcm->card->number;
342 runtime->sync.id32[1] = -1;
343 runtime->sync.id32[2] = -1;
344 runtime->sync.id32[3] = -1;
347 EXPORT_SYMBOL(snd_pcm_set_sync);
350 * Standard ioctl routine
353 static inline unsigned int div32(unsigned int a, unsigned int b,
364 static inline unsigned int div_down(unsigned int a, unsigned int b)
371 static inline unsigned int div_up(unsigned int a, unsigned int b)
383 static inline unsigned int mul(unsigned int a, unsigned int b)
387 if (div_down(UINT_MAX, a) < b)
392 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
393 unsigned int c, unsigned int *r)
395 u_int64_t n = (u_int64_t) a * b;
410 * snd_interval_refine - refine the interval value of configurator
411 * @i: the interval value to refine
412 * @v: the interval value to refer to
414 * Refines the interval value with the reference value.
415 * The interval is changed to the range satisfying both intervals.
416 * The interval status (min, max, integer, etc.) are evaluated.
418 * Returns non-zero if the value is changed, zero if not changed.
420 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
423 if (snd_BUG_ON(snd_interval_empty(i)))
425 if (i->min < v->min) {
427 i->openmin = v->openmin;
429 } else if (i->min == v->min && !i->openmin && v->openmin) {
433 if (i->max > v->max) {
435 i->openmax = v->openmax;
437 } else if (i->max == v->max && !i->openmax && v->openmax) {
441 if (!i->integer && v->integer) {
454 } else if (!i->openmin && !i->openmax && i->min == i->max)
456 if (snd_interval_checkempty(i)) {
457 snd_interval_none(i);
463 EXPORT_SYMBOL(snd_interval_refine);
465 static int snd_interval_refine_first(struct snd_interval *i)
467 if (snd_BUG_ON(snd_interval_empty(i)))
469 if (snd_interval_single(i))
472 i->openmax = i->openmin;
478 static int snd_interval_refine_last(struct snd_interval *i)
480 if (snd_BUG_ON(snd_interval_empty(i)))
482 if (snd_interval_single(i))
485 i->openmin = i->openmax;
491 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
493 if (a->empty || b->empty) {
494 snd_interval_none(c);
498 c->min = mul(a->min, b->min);
499 c->openmin = (a->openmin || b->openmin);
500 c->max = mul(a->max, b->max);
501 c->openmax = (a->openmax || b->openmax);
502 c->integer = (a->integer && b->integer);
506 * snd_interval_div - refine the interval value with division
513 * Returns non-zero if the value is changed, zero if not changed.
515 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
518 if (a->empty || b->empty) {
519 snd_interval_none(c);
523 c->min = div32(a->min, b->max, &r);
524 c->openmin = (r || a->openmin || b->openmax);
526 c->max = div32(a->max, b->min, &r);
531 c->openmax = (a->openmax || b->openmin);
540 * snd_interval_muldivk - refine the interval value
543 * @k: divisor (as integer)
548 * Returns non-zero if the value is changed, zero if not changed.
550 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
551 unsigned int k, struct snd_interval *c)
554 if (a->empty || b->empty) {
555 snd_interval_none(c);
559 c->min = muldiv32(a->min, b->min, k, &r);
560 c->openmin = (r || a->openmin || b->openmin);
561 c->max = muldiv32(a->max, b->max, k, &r);
566 c->openmax = (a->openmax || b->openmax);
571 * snd_interval_mulkdiv - refine the interval value
573 * @k: dividend 2 (as integer)
579 * Returns non-zero if the value is changed, zero if not changed.
581 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
582 const struct snd_interval *b, struct snd_interval *c)
585 if (a->empty || b->empty) {
586 snd_interval_none(c);
590 c->min = muldiv32(a->min, k, b->max, &r);
591 c->openmin = (r || a->openmin || b->openmax);
593 c->max = muldiv32(a->max, k, b->min, &r);
598 c->openmax = (a->openmax || b->openmin);
610 * snd_interval_ratnum - refine the interval value
611 * @i: interval to refine
612 * @rats_count: number of ratnum_t
613 * @rats: ratnum_t array
614 * @nump: pointer to store the resultant numerator
615 * @denp: pointer to store the resultant denominator
617 * Returns non-zero if the value is changed, zero if not changed.
619 int snd_interval_ratnum(struct snd_interval *i,
620 unsigned int rats_count, struct snd_ratnum *rats,
621 unsigned int *nump, unsigned int *denp)
623 unsigned int best_num, best_diff, best_den;
625 struct snd_interval t;
628 best_num = best_den = best_diff = 0;
629 for (k = 0; k < rats_count; ++k) {
630 unsigned int num = rats[k].num;
632 unsigned int q = i->min;
636 den = div_down(num, q);
637 if (den < rats[k].den_min)
639 if (den > rats[k].den_max)
640 den = rats[k].den_max;
643 r = (den - rats[k].den_min) % rats[k].den_step;
647 diff = num - q * den;
649 diff * best_den < best_diff * den) {
659 t.min = div_down(best_num, best_den);
660 t.openmin = !!(best_num % best_den);
662 best_num = best_den = best_diff = 0;
663 for (k = 0; k < rats_count; ++k) {
664 unsigned int num = rats[k].num;
666 unsigned int q = i->max;
672 den = div_up(num, q);
673 if (den > rats[k].den_max)
675 if (den < rats[k].den_min)
676 den = rats[k].den_min;
679 r = (den - rats[k].den_min) % rats[k].den_step;
681 den += rats[k].den_step - r;
683 diff = q * den - num;
685 diff * best_den < best_diff * den) {
695 t.max = div_up(best_num, best_den);
696 t.openmax = !!(best_num % best_den);
698 err = snd_interval_refine(i, &t);
702 if (snd_interval_single(i)) {
711 EXPORT_SYMBOL(snd_interval_ratnum);
714 * snd_interval_ratden - refine the interval value
715 * @i: interval to refine
716 * @rats_count: number of struct ratden
717 * @rats: struct ratden array
718 * @nump: pointer to store the resultant numerator
719 * @denp: pointer to store the resultant denominator
721 * Returns non-zero if the value is changed, zero if not changed.
723 static int snd_interval_ratden(struct snd_interval *i,
724 unsigned int rats_count, struct snd_ratden *rats,
725 unsigned int *nump, unsigned int *denp)
727 unsigned int best_num, best_diff, best_den;
729 struct snd_interval t;
732 best_num = best_den = best_diff = 0;
733 for (k = 0; k < rats_count; ++k) {
735 unsigned int den = rats[k].den;
736 unsigned int q = i->min;
739 if (num > rats[k].num_max)
741 if (num < rats[k].num_min)
742 num = rats[k].num_max;
745 r = (num - rats[k].num_min) % rats[k].num_step;
747 num += rats[k].num_step - r;
749 diff = num - q * den;
751 diff * best_den < best_diff * den) {
761 t.min = div_down(best_num, best_den);
762 t.openmin = !!(best_num % best_den);
764 best_num = best_den = best_diff = 0;
765 for (k = 0; k < rats_count; ++k) {
767 unsigned int den = rats[k].den;
768 unsigned int q = i->max;
771 if (num < rats[k].num_min)
773 if (num > rats[k].num_max)
774 num = rats[k].num_max;
777 r = (num - rats[k].num_min) % rats[k].num_step;
781 diff = q * den - num;
783 diff * best_den < best_diff * den) {
793 t.max = div_up(best_num, best_den);
794 t.openmax = !!(best_num % best_den);
796 err = snd_interval_refine(i, &t);
800 if (snd_interval_single(i)) {
810 * snd_interval_list - refine the interval value from the list
811 * @i: the interval value to refine
812 * @count: the number of elements in the list
813 * @list: the value list
814 * @mask: the bit-mask to evaluate
816 * Refines the interval value from the list.
817 * When mask is non-zero, only the elements corresponding to bit 1 are
820 * Returns non-zero if the value is changed, zero if not changed.
822 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
831 for (k = 0; k < count; k++) {
832 if (mask && !(mask & (1 << k)))
834 if (i->min == list[k] && !i->openmin)
836 if (i->min < list[k]) {
846 for (k = count; k-- > 0;) {
847 if (mask && !(mask & (1 << k)))
849 if (i->max == list[k] && !i->openmax)
851 if (i->max > list[k]) {
861 if (snd_interval_checkempty(i)) {
868 EXPORT_SYMBOL(snd_interval_list);
870 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
874 n = (i->min - min) % step;
875 if (n != 0 || i->openmin) {
879 n = (i->max - min) % step;
880 if (n != 0 || i->openmax) {
884 if (snd_interval_checkempty(i)) {
891 /* Info constraints helpers */
894 * snd_pcm_hw_rule_add - add the hw-constraint rule
895 * @runtime: the pcm runtime instance
896 * @cond: condition bits
897 * @var: the variable to evaluate
898 * @func: the evaluation function
899 * @private: the private data pointer passed to function
900 * @dep: the dependent variables
902 * Returns zero if successful, or a negative error code on failure.
904 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
906 snd_pcm_hw_rule_func_t func, void *private,
909 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
910 struct snd_pcm_hw_rule *c;
914 if (constrs->rules_num >= constrs->rules_all) {
915 struct snd_pcm_hw_rule *new;
916 unsigned int new_rules = constrs->rules_all + 16;
917 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
920 if (constrs->rules) {
921 memcpy(new, constrs->rules,
922 constrs->rules_num * sizeof(*c));
923 kfree(constrs->rules);
925 constrs->rules = new;
926 constrs->rules_all = new_rules;
928 c = &constrs->rules[constrs->rules_num];
932 c->private = private;
935 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
940 dep = va_arg(args, int);
942 constrs->rules_num++;
947 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
950 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
951 * @runtime: PCM runtime instance
952 * @var: hw_params variable to apply the mask
953 * @mask: the bitmap mask
955 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
957 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
960 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
961 struct snd_mask *maskp = constrs_mask(constrs, var);
962 *maskp->bits &= mask;
963 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
964 if (*maskp->bits == 0)
970 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
971 * @runtime: PCM runtime instance
972 * @var: hw_params variable to apply the mask
973 * @mask: the 64bit bitmap mask
975 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
977 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
980 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
981 struct snd_mask *maskp = constrs_mask(constrs, var);
982 maskp->bits[0] &= (u_int32_t)mask;
983 maskp->bits[1] &= (u_int32_t)(mask >> 32);
984 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
985 if (! maskp->bits[0] && ! maskp->bits[1])
991 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
992 * @runtime: PCM runtime instance
993 * @var: hw_params variable to apply the integer constraint
995 * Apply the constraint of integer to an interval parameter.
997 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
999 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1000 return snd_interval_setinteger(constrs_interval(constrs, var));
1003 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1006 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1007 * @runtime: PCM runtime instance
1008 * @var: hw_params variable to apply the range
1009 * @min: the minimal value
1010 * @max: the maximal value
1012 * Apply the min/max range constraint to an interval parameter.
1014 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1015 unsigned int min, unsigned int max)
1017 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1018 struct snd_interval t;
1021 t.openmin = t.openmax = 0;
1023 return snd_interval_refine(constrs_interval(constrs, var), &t);
1026 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1028 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1029 struct snd_pcm_hw_rule *rule)
1031 struct snd_pcm_hw_constraint_list *list = rule->private;
1032 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1037 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1038 * @runtime: PCM runtime instance
1039 * @cond: condition bits
1040 * @var: hw_params variable to apply the list constraint
1043 * Apply the list of constraints to an interval parameter.
1045 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1047 snd_pcm_hw_param_t var,
1048 struct snd_pcm_hw_constraint_list *l)
1050 return snd_pcm_hw_rule_add(runtime, cond, var,
1051 snd_pcm_hw_rule_list, l,
1055 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1057 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1058 struct snd_pcm_hw_rule *rule)
1060 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1061 unsigned int num = 0, den = 0;
1063 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1064 r->nrats, r->rats, &num, &den);
1065 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1066 params->rate_num = num;
1067 params->rate_den = den;
1073 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1074 * @runtime: PCM runtime instance
1075 * @cond: condition bits
1076 * @var: hw_params variable to apply the ratnums constraint
1077 * @r: struct snd_ratnums constriants
1079 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1081 snd_pcm_hw_param_t var,
1082 struct snd_pcm_hw_constraint_ratnums *r)
1084 return snd_pcm_hw_rule_add(runtime, cond, var,
1085 snd_pcm_hw_rule_ratnums, r,
1089 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1091 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1092 struct snd_pcm_hw_rule *rule)
1094 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1095 unsigned int num = 0, den = 0;
1096 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1097 r->nrats, r->rats, &num, &den);
1098 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1099 params->rate_num = num;
1100 params->rate_den = den;
1106 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1107 * @runtime: PCM runtime instance
1108 * @cond: condition bits
1109 * @var: hw_params variable to apply the ratdens constraint
1110 * @r: struct snd_ratdens constriants
1112 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1114 snd_pcm_hw_param_t var,
1115 struct snd_pcm_hw_constraint_ratdens *r)
1117 return snd_pcm_hw_rule_add(runtime, cond, var,
1118 snd_pcm_hw_rule_ratdens, r,
1122 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1124 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1125 struct snd_pcm_hw_rule *rule)
1127 unsigned int l = (unsigned long) rule->private;
1128 int width = l & 0xffff;
1129 unsigned int msbits = l >> 16;
1130 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1131 if (snd_interval_single(i) && snd_interval_value(i) == width)
1132 params->msbits = msbits;
1137 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1138 * @runtime: PCM runtime instance
1139 * @cond: condition bits
1140 * @width: sample bits width
1141 * @msbits: msbits width
1143 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1146 unsigned int msbits)
1148 unsigned long l = (msbits << 16) | width;
1149 return snd_pcm_hw_rule_add(runtime, cond, -1,
1150 snd_pcm_hw_rule_msbits,
1152 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1155 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1157 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1158 struct snd_pcm_hw_rule *rule)
1160 unsigned long step = (unsigned long) rule->private;
1161 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1165 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1166 * @runtime: PCM runtime instance
1167 * @cond: condition bits
1168 * @var: hw_params variable to apply the step constraint
1171 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1173 snd_pcm_hw_param_t var,
1176 return snd_pcm_hw_rule_add(runtime, cond, var,
1177 snd_pcm_hw_rule_step, (void *) step,
1181 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1183 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1185 static unsigned int pow2_sizes[] = {
1186 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1187 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1188 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1189 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1191 return snd_interval_list(hw_param_interval(params, rule->var),
1192 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1196 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1197 * @runtime: PCM runtime instance
1198 * @cond: condition bits
1199 * @var: hw_params variable to apply the power-of-2 constraint
1201 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1203 snd_pcm_hw_param_t var)
1205 return snd_pcm_hw_rule_add(runtime, cond, var,
1206 snd_pcm_hw_rule_pow2, NULL,
1210 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1212 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1213 snd_pcm_hw_param_t var)
1215 if (hw_is_mask(var)) {
1216 snd_mask_any(hw_param_mask(params, var));
1217 params->cmask |= 1 << var;
1218 params->rmask |= 1 << var;
1221 if (hw_is_interval(var)) {
1222 snd_interval_any(hw_param_interval(params, var));
1223 params->cmask |= 1 << var;
1224 params->rmask |= 1 << var;
1230 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1233 memset(params, 0, sizeof(*params));
1234 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1235 _snd_pcm_hw_param_any(params, k);
1236 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1237 _snd_pcm_hw_param_any(params, k);
1241 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1244 * snd_pcm_hw_param_value - return @params field @var value
1245 * @params: the hw_params instance
1246 * @var: parameter to retrieve
1247 * @dir: pointer to the direction (-1,0,1) or %NULL
1249 * Return the value for field @var if it's fixed in configuration space
1250 * defined by @params. Return -%EINVAL otherwise.
1252 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1253 snd_pcm_hw_param_t var, int *dir)
1255 if (hw_is_mask(var)) {
1256 const struct snd_mask *mask = hw_param_mask_c(params, var);
1257 if (!snd_mask_single(mask))
1261 return snd_mask_value(mask);
1263 if (hw_is_interval(var)) {
1264 const struct snd_interval *i = hw_param_interval_c(params, var);
1265 if (!snd_interval_single(i))
1269 return snd_interval_value(i);
1274 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1276 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1277 snd_pcm_hw_param_t var)
1279 if (hw_is_mask(var)) {
1280 snd_mask_none(hw_param_mask(params, var));
1281 params->cmask |= 1 << var;
1282 params->rmask |= 1 << var;
1283 } else if (hw_is_interval(var)) {
1284 snd_interval_none(hw_param_interval(params, var));
1285 params->cmask |= 1 << var;
1286 params->rmask |= 1 << var;
1292 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1294 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1295 snd_pcm_hw_param_t var)
1298 if (hw_is_mask(var))
1299 changed = snd_mask_refine_first(hw_param_mask(params, var));
1300 else if (hw_is_interval(var))
1301 changed = snd_interval_refine_first(hw_param_interval(params, var));
1305 params->cmask |= 1 << var;
1306 params->rmask |= 1 << var;
1313 * snd_pcm_hw_param_first - refine config space and return minimum value
1314 * @pcm: PCM instance
1315 * @params: the hw_params instance
1316 * @var: parameter to retrieve
1317 * @dir: pointer to the direction (-1,0,1) or %NULL
1319 * Inside configuration space defined by @params remove from @var all
1320 * values > minimum. Reduce configuration space accordingly.
1321 * Return the minimum.
1323 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1324 struct snd_pcm_hw_params *params,
1325 snd_pcm_hw_param_t var, int *dir)
1327 int changed = _snd_pcm_hw_param_first(params, var);
1330 if (params->rmask) {
1331 int err = snd_pcm_hw_refine(pcm, params);
1332 if (snd_BUG_ON(err < 0))
1335 return snd_pcm_hw_param_value(params, var, dir);
1338 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1340 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1341 snd_pcm_hw_param_t var)
1344 if (hw_is_mask(var))
1345 changed = snd_mask_refine_last(hw_param_mask(params, var));
1346 else if (hw_is_interval(var))
1347 changed = snd_interval_refine_last(hw_param_interval(params, var));
1351 params->cmask |= 1 << var;
1352 params->rmask |= 1 << var;
1359 * snd_pcm_hw_param_last - refine config space and return maximum value
1360 * @pcm: PCM instance
1361 * @params: the hw_params instance
1362 * @var: parameter to retrieve
1363 * @dir: pointer to the direction (-1,0,1) or %NULL
1365 * Inside configuration space defined by @params remove from @var all
1366 * values < maximum. Reduce configuration space accordingly.
1367 * Return the maximum.
1369 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1370 struct snd_pcm_hw_params *params,
1371 snd_pcm_hw_param_t var, int *dir)
1373 int changed = _snd_pcm_hw_param_last(params, var);
1376 if (params->rmask) {
1377 int err = snd_pcm_hw_refine(pcm, params);
1378 if (snd_BUG_ON(err < 0))
1381 return snd_pcm_hw_param_value(params, var, dir);
1384 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1387 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1388 * @pcm: PCM instance
1389 * @params: the hw_params instance
1391 * Choose one configuration from configuration space defined by @params.
1392 * The configuration chosen is that obtained fixing in this order:
1393 * first access, first format, first subformat, min channels,
1394 * min rate, min period time, max buffer size, min tick time
1396 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1397 struct snd_pcm_hw_params *params)
1399 static int vars[] = {
1400 SNDRV_PCM_HW_PARAM_ACCESS,
1401 SNDRV_PCM_HW_PARAM_FORMAT,
1402 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1403 SNDRV_PCM_HW_PARAM_CHANNELS,
1404 SNDRV_PCM_HW_PARAM_RATE,
1405 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1406 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1407 SNDRV_PCM_HW_PARAM_TICK_TIME,
1412 for (v = vars; *v != -1; v++) {
1413 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1414 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1416 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1417 if (snd_BUG_ON(err < 0))
1423 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1426 struct snd_pcm_runtime *runtime = substream->runtime;
1427 unsigned long flags;
1428 snd_pcm_stream_lock_irqsave(substream, flags);
1429 if (snd_pcm_running(substream) &&
1430 snd_pcm_update_hw_ptr(substream) >= 0)
1431 runtime->status->hw_ptr %= runtime->buffer_size;
1433 runtime->status->hw_ptr = 0;
1434 snd_pcm_stream_unlock_irqrestore(substream, flags);
1438 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1441 struct snd_pcm_channel_info *info = arg;
1442 struct snd_pcm_runtime *runtime = substream->runtime;
1444 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1448 width = snd_pcm_format_physical_width(runtime->format);
1452 switch (runtime->access) {
1453 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1454 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1455 info->first = info->channel * width;
1456 info->step = runtime->channels * width;
1458 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1459 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1461 size_t size = runtime->dma_bytes / runtime->channels;
1462 info->first = info->channel * size * 8;
1474 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1475 * @substream: the pcm substream instance
1476 * @cmd: ioctl command
1477 * @arg: ioctl argument
1479 * Processes the generic ioctl commands for PCM.
1480 * Can be passed as the ioctl callback for PCM ops.
1482 * Returns zero if successful, or a negative error code on failure.
1484 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1485 unsigned int cmd, void *arg)
1488 case SNDRV_PCM_IOCTL1_INFO:
1490 case SNDRV_PCM_IOCTL1_RESET:
1491 return snd_pcm_lib_ioctl_reset(substream, arg);
1492 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1493 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1498 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1501 * snd_pcm_period_elapsed - update the pcm status for the next period
1502 * @substream: the pcm substream instance
1504 * This function is called from the interrupt handler when the
1505 * PCM has processed the period size. It will update the current
1506 * pointer, wake up sleepers, etc.
1508 * Even if more than one periods have elapsed since the last call, you
1509 * have to call this only once.
1511 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1513 struct snd_pcm_runtime *runtime;
1514 unsigned long flags;
1516 if (PCM_RUNTIME_CHECK(substream))
1518 runtime = substream->runtime;
1520 if (runtime->transfer_ack_begin)
1521 runtime->transfer_ack_begin(substream);
1523 snd_pcm_stream_lock_irqsave(substream, flags);
1524 if (!snd_pcm_running(substream) ||
1525 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1528 if (substream->timer_running)
1529 snd_timer_interrupt(substream->timer, 1);
1531 snd_pcm_stream_unlock_irqrestore(substream, flags);
1532 if (runtime->transfer_ack_end)
1533 runtime->transfer_ack_end(substream);
1534 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1537 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1540 * Wait until avail_min data becomes available
1541 * Returns a negative error code if any error occurs during operation.
1542 * The available space is stored on availp. When err = 0 and avail = 0
1543 * on the capture stream, it indicates the stream is in DRAINING state.
1545 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1546 snd_pcm_uframes_t *availp)
1548 struct snd_pcm_runtime *runtime = substream->runtime;
1549 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1552 snd_pcm_uframes_t avail = 0;
1555 init_waitqueue_entry(&wait, current);
1556 add_wait_queue(&runtime->sleep, &wait);
1558 if (signal_pending(current)) {
1562 set_current_state(TASK_INTERRUPTIBLE);
1563 snd_pcm_stream_unlock_irq(substream);
1564 tout = schedule_timeout(msecs_to_jiffies(10000));
1565 snd_pcm_stream_lock_irq(substream);
1566 switch (runtime->status->state) {
1567 case SNDRV_PCM_STATE_SUSPENDED:
1570 case SNDRV_PCM_STATE_XRUN:
1573 case SNDRV_PCM_STATE_DRAINING:
1577 avail = 0; /* indicate draining */
1579 case SNDRV_PCM_STATE_OPEN:
1580 case SNDRV_PCM_STATE_SETUP:
1581 case SNDRV_PCM_STATE_DISCONNECTED:
1586 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1587 is_playback ? "playback" : "capture");
1592 avail = snd_pcm_playback_avail(runtime);
1594 avail = snd_pcm_capture_avail(runtime);
1595 if (avail >= runtime->control->avail_min)
1599 remove_wait_queue(&runtime->sleep, &wait);
1604 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1606 unsigned long data, unsigned int off,
1607 snd_pcm_uframes_t frames)
1609 struct snd_pcm_runtime *runtime = substream->runtime;
1611 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1612 if (substream->ops->copy) {
1613 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1616 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1617 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1623 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1624 unsigned long data, unsigned int off,
1625 snd_pcm_uframes_t size);
1627 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1629 snd_pcm_uframes_t size,
1631 transfer_f transfer)
1633 struct snd_pcm_runtime *runtime = substream->runtime;
1634 snd_pcm_uframes_t xfer = 0;
1635 snd_pcm_uframes_t offset = 0;
1641 snd_pcm_stream_lock_irq(substream);
1642 switch (runtime->status->state) {
1643 case SNDRV_PCM_STATE_PREPARED:
1644 case SNDRV_PCM_STATE_RUNNING:
1645 case SNDRV_PCM_STATE_PAUSED:
1647 case SNDRV_PCM_STATE_XRUN:
1650 case SNDRV_PCM_STATE_SUSPENDED:
1659 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1660 snd_pcm_uframes_t avail;
1661 snd_pcm_uframes_t cont;
1662 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1663 snd_pcm_update_hw_ptr(substream);
1664 avail = snd_pcm_playback_avail(runtime);
1670 err = wait_for_avail_min(substream, &avail);
1674 frames = size > avail ? avail : size;
1675 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1678 if (snd_BUG_ON(!frames)) {
1679 snd_pcm_stream_unlock_irq(substream);
1682 appl_ptr = runtime->control->appl_ptr;
1683 appl_ofs = appl_ptr % runtime->buffer_size;
1684 snd_pcm_stream_unlock_irq(substream);
1685 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1687 snd_pcm_stream_lock_irq(substream);
1688 switch (runtime->status->state) {
1689 case SNDRV_PCM_STATE_XRUN:
1692 case SNDRV_PCM_STATE_SUSPENDED:
1699 if (appl_ptr >= runtime->boundary)
1700 appl_ptr -= runtime->boundary;
1701 runtime->control->appl_ptr = appl_ptr;
1702 if (substream->ops->ack)
1703 substream->ops->ack(substream);
1708 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1709 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1710 err = snd_pcm_start(substream);
1716 snd_pcm_stream_unlock_irq(substream);
1718 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1721 /* sanity-check for read/write methods */
1722 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1724 struct snd_pcm_runtime *runtime;
1725 if (PCM_RUNTIME_CHECK(substream))
1727 runtime = substream->runtime;
1728 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1730 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1735 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1737 struct snd_pcm_runtime *runtime;
1741 err = pcm_sanity_check(substream);
1744 runtime = substream->runtime;
1745 nonblock = !!(substream->f_flags & O_NONBLOCK);
1747 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1748 runtime->channels > 1)
1750 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1751 snd_pcm_lib_write_transfer);
1754 EXPORT_SYMBOL(snd_pcm_lib_write);
1756 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1758 unsigned long data, unsigned int off,
1759 snd_pcm_uframes_t frames)
1761 struct snd_pcm_runtime *runtime = substream->runtime;
1763 void __user **bufs = (void __user **)data;
1764 int channels = runtime->channels;
1766 if (substream->ops->copy) {
1767 if (snd_BUG_ON(!substream->ops->silence))
1769 for (c = 0; c < channels; ++c, ++bufs) {
1770 if (*bufs == NULL) {
1771 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1774 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1775 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1780 /* default transfer behaviour */
1781 size_t dma_csize = runtime->dma_bytes / channels;
1782 for (c = 0; c < channels; ++c, ++bufs) {
1783 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1784 if (*bufs == NULL) {
1785 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1787 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1788 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1796 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1798 snd_pcm_uframes_t frames)
1800 struct snd_pcm_runtime *runtime;
1804 err = pcm_sanity_check(substream);
1807 runtime = substream->runtime;
1808 nonblock = !!(substream->f_flags & O_NONBLOCK);
1810 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1812 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1813 nonblock, snd_pcm_lib_writev_transfer);
1816 EXPORT_SYMBOL(snd_pcm_lib_writev);
1818 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1820 unsigned long data, unsigned int off,
1821 snd_pcm_uframes_t frames)
1823 struct snd_pcm_runtime *runtime = substream->runtime;
1825 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1826 if (substream->ops->copy) {
1827 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1830 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1831 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1837 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1839 snd_pcm_uframes_t size,
1841 transfer_f transfer)
1843 struct snd_pcm_runtime *runtime = substream->runtime;
1844 snd_pcm_uframes_t xfer = 0;
1845 snd_pcm_uframes_t offset = 0;
1851 snd_pcm_stream_lock_irq(substream);
1852 switch (runtime->status->state) {
1853 case SNDRV_PCM_STATE_PREPARED:
1854 if (size >= runtime->start_threshold) {
1855 err = snd_pcm_start(substream);
1860 case SNDRV_PCM_STATE_DRAINING:
1861 case SNDRV_PCM_STATE_RUNNING:
1862 case SNDRV_PCM_STATE_PAUSED:
1864 case SNDRV_PCM_STATE_XRUN:
1867 case SNDRV_PCM_STATE_SUSPENDED:
1876 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1877 snd_pcm_uframes_t avail;
1878 snd_pcm_uframes_t cont;
1879 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1880 snd_pcm_update_hw_ptr(substream);
1881 avail = snd_pcm_capture_avail(runtime);
1883 if (runtime->status->state ==
1884 SNDRV_PCM_STATE_DRAINING) {
1885 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1892 err = wait_for_avail_min(substream, &avail);
1896 continue; /* draining */
1898 frames = size > avail ? avail : size;
1899 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1902 if (snd_BUG_ON(!frames)) {
1903 snd_pcm_stream_unlock_irq(substream);
1906 appl_ptr = runtime->control->appl_ptr;
1907 appl_ofs = appl_ptr % runtime->buffer_size;
1908 snd_pcm_stream_unlock_irq(substream);
1909 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1911 snd_pcm_stream_lock_irq(substream);
1912 switch (runtime->status->state) {
1913 case SNDRV_PCM_STATE_XRUN:
1916 case SNDRV_PCM_STATE_SUSPENDED:
1923 if (appl_ptr >= runtime->boundary)
1924 appl_ptr -= runtime->boundary;
1925 runtime->control->appl_ptr = appl_ptr;
1926 if (substream->ops->ack)
1927 substream->ops->ack(substream);
1934 snd_pcm_stream_unlock_irq(substream);
1936 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1939 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1941 struct snd_pcm_runtime *runtime;
1945 err = pcm_sanity_check(substream);
1948 runtime = substream->runtime;
1949 nonblock = !!(substream->f_flags & O_NONBLOCK);
1950 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1952 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1955 EXPORT_SYMBOL(snd_pcm_lib_read);
1957 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1959 unsigned long data, unsigned int off,
1960 snd_pcm_uframes_t frames)
1962 struct snd_pcm_runtime *runtime = substream->runtime;
1964 void __user **bufs = (void __user **)data;
1965 int channels = runtime->channels;
1967 if (substream->ops->copy) {
1968 for (c = 0; c < channels; ++c, ++bufs) {
1972 buf = *bufs + samples_to_bytes(runtime, off);
1973 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1977 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1978 for (c = 0; c < channels; ++c, ++bufs) {
1984 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1985 buf = *bufs + samples_to_bytes(runtime, off);
1986 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1993 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1995 snd_pcm_uframes_t frames)
1997 struct snd_pcm_runtime *runtime;
2001 err = pcm_sanity_check(substream);
2004 runtime = substream->runtime;
2005 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2008 nonblock = !!(substream->f_flags & O_NONBLOCK);
2009 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2011 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2014 EXPORT_SYMBOL(snd_pcm_lib_readv);