2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/info.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/timer.h>
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
43 struct snd_pcm_runtime *runtime = substream->runtime;
44 snd_pcm_uframes_t frames, ofs, transfer;
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
58 if (runtime->silence_filled >= runtime->buffer_size)
60 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
81 runtime->silence_start = new_hw_ptr;
83 runtime->silence_start = ofs;
86 frames = runtime->buffer_size - runtime->silence_filled;
88 if (snd_BUG_ON(frames > runtime->buffer_size))
92 ofs = runtime->silence_start % runtime->buffer_size;
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
99 err = substream->ops->silence(substream, -1, ofs, transfer);
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
111 err = substream->ops->silence(substream, c, ofs, transfer);
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 runtime->silence_filled += transfer;
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129 #define xrun_debug(substream) ((substream)->pstr->xrun_debug)
131 #define xrun_debug(substream) 0
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
139 static void xrun(struct snd_pcm_substream *substream)
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
142 if (xrun_debug(substream)) {
143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
147 dump_stack_on_xrun(substream);
151 static snd_pcm_uframes_t
152 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
155 snd_pcm_uframes_t pos;
157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
162 if (pos >= runtime->buffer_size) {
163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
171 pos -= pos % runtime->min_align;
175 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
178 snd_pcm_uframes_t avail;
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
198 #define hw_ptr_error(substream, fmt, args...) \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
202 snd_printd("PCM: " fmt, ##args); \
204 dump_stack_on_xrun(substream); \
208 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 snd_pcm_uframes_t pos;
212 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt, hw_base;
213 snd_pcm_sframes_t delta;
215 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
216 if (pos == SNDRV_PCM_POS_XRUN) {
220 hw_base = runtime->hw_ptr_base;
221 new_hw_ptr = hw_base + pos;
222 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
223 delta = new_hw_ptr - hw_ptr_interrupt;
224 if (hw_ptr_interrupt == runtime->boundary)
225 hw_ptr_interrupt = 0;
227 delta += runtime->buffer_size;
229 hw_ptr_error(substream,
230 "Unexpected hw_pointer value "
231 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
232 substream->stream, (long)pos,
233 (long)hw_ptr_interrupt);
234 /* rebase to interrupt position */
235 hw_base = new_hw_ptr = hw_ptr_interrupt;
238 hw_base += runtime->buffer_size;
239 if (hw_base == runtime->boundary)
241 new_hw_ptr = hw_base + pos;
244 if (delta > runtime->period_size) {
245 hw_ptr_error(substream,
247 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
248 substream->stream, (long)delta,
249 (long)hw_ptr_interrupt);
250 /* rebase hw_ptr_interrupt */
252 new_hw_ptr - new_hw_ptr % runtime->period_size;
254 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
255 runtime->silence_size > 0)
256 snd_pcm_playback_silence(substream, new_hw_ptr);
258 runtime->hw_ptr_base = hw_base;
259 runtime->status->hw_ptr = new_hw_ptr;
260 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
262 return snd_pcm_update_hw_ptr_post(substream, runtime);
265 /* CAUTION: call it with irq disabled */
266 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
268 struct snd_pcm_runtime *runtime = substream->runtime;
269 snd_pcm_uframes_t pos;
270 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
271 snd_pcm_sframes_t delta;
273 old_hw_ptr = runtime->status->hw_ptr;
274 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
275 if (pos == SNDRV_PCM_POS_XRUN) {
279 hw_base = runtime->hw_ptr_base;
280 new_hw_ptr = hw_base + pos;
282 delta = new_hw_ptr - old_hw_ptr;
284 delta += runtime->buffer_size;
286 hw_ptr_error(substream,
287 "Unexpected hw_pointer value [2] "
288 "(stream=%i, pos=%ld, old_ptr=%ld)\n",
289 substream->stream, (long)pos,
293 hw_base += runtime->buffer_size;
294 if (hw_base == runtime->boundary)
296 new_hw_ptr = hw_base + pos;
298 if (delta > runtime->period_size && runtime->periods > 1) {
299 hw_ptr_error(substream,
301 "(pos=%ld, delta=%ld, period=%ld)\n",
302 (long)pos, (long)delta,
303 (long)runtime->period_size);
306 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
307 runtime->silence_size > 0)
308 snd_pcm_playback_silence(substream, new_hw_ptr);
310 runtime->hw_ptr_base = hw_base;
311 runtime->status->hw_ptr = new_hw_ptr;
313 return snd_pcm_update_hw_ptr_post(substream, runtime);
317 * snd_pcm_set_ops - set the PCM operators
318 * @pcm: the pcm instance
319 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
320 * @ops: the operator table
322 * Sets the given PCM operators to the pcm instance.
324 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
326 struct snd_pcm_str *stream = &pcm->streams[direction];
327 struct snd_pcm_substream *substream;
329 for (substream = stream->substream; substream != NULL; substream = substream->next)
330 substream->ops = ops;
333 EXPORT_SYMBOL(snd_pcm_set_ops);
336 * snd_pcm_sync - set the PCM sync id
337 * @substream: the pcm substream
339 * Sets the PCM sync identifier for the card.
341 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
343 struct snd_pcm_runtime *runtime = substream->runtime;
345 runtime->sync.id32[0] = substream->pcm->card->number;
346 runtime->sync.id32[1] = -1;
347 runtime->sync.id32[2] = -1;
348 runtime->sync.id32[3] = -1;
351 EXPORT_SYMBOL(snd_pcm_set_sync);
354 * Standard ioctl routine
357 static inline unsigned int div32(unsigned int a, unsigned int b,
368 static inline unsigned int div_down(unsigned int a, unsigned int b)
375 static inline unsigned int div_up(unsigned int a, unsigned int b)
387 static inline unsigned int mul(unsigned int a, unsigned int b)
391 if (div_down(UINT_MAX, a) < b)
396 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
397 unsigned int c, unsigned int *r)
399 u_int64_t n = (u_int64_t) a * b;
414 * snd_interval_refine - refine the interval value of configurator
415 * @i: the interval value to refine
416 * @v: the interval value to refer to
418 * Refines the interval value with the reference value.
419 * The interval is changed to the range satisfying both intervals.
420 * The interval status (min, max, integer, etc.) are evaluated.
422 * Returns non-zero if the value is changed, zero if not changed.
424 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
427 if (snd_BUG_ON(snd_interval_empty(i)))
429 if (i->min < v->min) {
431 i->openmin = v->openmin;
433 } else if (i->min == v->min && !i->openmin && v->openmin) {
437 if (i->max > v->max) {
439 i->openmax = v->openmax;
441 } else if (i->max == v->max && !i->openmax && v->openmax) {
445 if (!i->integer && v->integer) {
458 } else if (!i->openmin && !i->openmax && i->min == i->max)
460 if (snd_interval_checkempty(i)) {
461 snd_interval_none(i);
467 EXPORT_SYMBOL(snd_interval_refine);
469 static int snd_interval_refine_first(struct snd_interval *i)
471 if (snd_BUG_ON(snd_interval_empty(i)))
473 if (snd_interval_single(i))
476 i->openmax = i->openmin;
482 static int snd_interval_refine_last(struct snd_interval *i)
484 if (snd_BUG_ON(snd_interval_empty(i)))
486 if (snd_interval_single(i))
489 i->openmin = i->openmax;
495 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
497 if (a->empty || b->empty) {
498 snd_interval_none(c);
502 c->min = mul(a->min, b->min);
503 c->openmin = (a->openmin || b->openmin);
504 c->max = mul(a->max, b->max);
505 c->openmax = (a->openmax || b->openmax);
506 c->integer = (a->integer && b->integer);
510 * snd_interval_div - refine the interval value with division
517 * Returns non-zero if the value is changed, zero if not changed.
519 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
522 if (a->empty || b->empty) {
523 snd_interval_none(c);
527 c->min = div32(a->min, b->max, &r);
528 c->openmin = (r || a->openmin || b->openmax);
530 c->max = div32(a->max, b->min, &r);
535 c->openmax = (a->openmax || b->openmin);
544 * snd_interval_muldivk - refine the interval value
547 * @k: divisor (as integer)
552 * Returns non-zero if the value is changed, zero if not changed.
554 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
555 unsigned int k, struct snd_interval *c)
558 if (a->empty || b->empty) {
559 snd_interval_none(c);
563 c->min = muldiv32(a->min, b->min, k, &r);
564 c->openmin = (r || a->openmin || b->openmin);
565 c->max = muldiv32(a->max, b->max, k, &r);
570 c->openmax = (a->openmax || b->openmax);
575 * snd_interval_mulkdiv - refine the interval value
577 * @k: dividend 2 (as integer)
583 * Returns non-zero if the value is changed, zero if not changed.
585 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
586 const struct snd_interval *b, struct snd_interval *c)
589 if (a->empty || b->empty) {
590 snd_interval_none(c);
594 c->min = muldiv32(a->min, k, b->max, &r);
595 c->openmin = (r || a->openmin || b->openmax);
597 c->max = muldiv32(a->max, k, b->min, &r);
602 c->openmax = (a->openmax || b->openmin);
614 * snd_interval_ratnum - refine the interval value
615 * @i: interval to refine
616 * @rats_count: number of ratnum_t
617 * @rats: ratnum_t array
618 * @nump: pointer to store the resultant numerator
619 * @denp: pointer to store the resultant denominator
621 * Returns non-zero if the value is changed, zero if not changed.
623 int snd_interval_ratnum(struct snd_interval *i,
624 unsigned int rats_count, struct snd_ratnum *rats,
625 unsigned int *nump, unsigned int *denp)
627 unsigned int best_num, best_diff, best_den;
629 struct snd_interval t;
632 best_num = best_den = best_diff = 0;
633 for (k = 0; k < rats_count; ++k) {
634 unsigned int num = rats[k].num;
636 unsigned int q = i->min;
640 den = div_down(num, q);
641 if (den < rats[k].den_min)
643 if (den > rats[k].den_max)
644 den = rats[k].den_max;
647 r = (den - rats[k].den_min) % rats[k].den_step;
651 diff = num - q * den;
653 diff * best_den < best_diff * den) {
663 t.min = div_down(best_num, best_den);
664 t.openmin = !!(best_num % best_den);
666 best_num = best_den = best_diff = 0;
667 for (k = 0; k < rats_count; ++k) {
668 unsigned int num = rats[k].num;
670 unsigned int q = i->max;
676 den = div_up(num, q);
677 if (den > rats[k].den_max)
679 if (den < rats[k].den_min)
680 den = rats[k].den_min;
683 r = (den - rats[k].den_min) % rats[k].den_step;
685 den += rats[k].den_step - r;
687 diff = q * den - num;
689 diff * best_den < best_diff * den) {
699 t.max = div_up(best_num, best_den);
700 t.openmax = !!(best_num % best_den);
702 err = snd_interval_refine(i, &t);
706 if (snd_interval_single(i)) {
715 EXPORT_SYMBOL(snd_interval_ratnum);
718 * snd_interval_ratden - refine the interval value
719 * @i: interval to refine
720 * @rats_count: number of struct ratden
721 * @rats: struct ratden array
722 * @nump: pointer to store the resultant numerator
723 * @denp: pointer to store the resultant denominator
725 * Returns non-zero if the value is changed, zero if not changed.
727 static int snd_interval_ratden(struct snd_interval *i,
728 unsigned int rats_count, struct snd_ratden *rats,
729 unsigned int *nump, unsigned int *denp)
731 unsigned int best_num, best_diff, best_den;
733 struct snd_interval t;
736 best_num = best_den = best_diff = 0;
737 for (k = 0; k < rats_count; ++k) {
739 unsigned int den = rats[k].den;
740 unsigned int q = i->min;
743 if (num > rats[k].num_max)
745 if (num < rats[k].num_min)
746 num = rats[k].num_max;
749 r = (num - rats[k].num_min) % rats[k].num_step;
751 num += rats[k].num_step - r;
753 diff = num - q * den;
755 diff * best_den < best_diff * den) {
765 t.min = div_down(best_num, best_den);
766 t.openmin = !!(best_num % best_den);
768 best_num = best_den = best_diff = 0;
769 for (k = 0; k < rats_count; ++k) {
771 unsigned int den = rats[k].den;
772 unsigned int q = i->max;
775 if (num < rats[k].num_min)
777 if (num > rats[k].num_max)
778 num = rats[k].num_max;
781 r = (num - rats[k].num_min) % rats[k].num_step;
785 diff = q * den - num;
787 diff * best_den < best_diff * den) {
797 t.max = div_up(best_num, best_den);
798 t.openmax = !!(best_num % best_den);
800 err = snd_interval_refine(i, &t);
804 if (snd_interval_single(i)) {
814 * snd_interval_list - refine the interval value from the list
815 * @i: the interval value to refine
816 * @count: the number of elements in the list
817 * @list: the value list
818 * @mask: the bit-mask to evaluate
820 * Refines the interval value from the list.
821 * When mask is non-zero, only the elements corresponding to bit 1 are
824 * Returns non-zero if the value is changed, zero if not changed.
826 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
835 for (k = 0; k < count; k++) {
836 if (mask && !(mask & (1 << k)))
838 if (i->min == list[k] && !i->openmin)
840 if (i->min < list[k]) {
850 for (k = count; k-- > 0;) {
851 if (mask && !(mask & (1 << k)))
853 if (i->max == list[k] && !i->openmax)
855 if (i->max > list[k]) {
865 if (snd_interval_checkempty(i)) {
872 EXPORT_SYMBOL(snd_interval_list);
874 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
878 n = (i->min - min) % step;
879 if (n != 0 || i->openmin) {
883 n = (i->max - min) % step;
884 if (n != 0 || i->openmax) {
888 if (snd_interval_checkempty(i)) {
895 /* Info constraints helpers */
898 * snd_pcm_hw_rule_add - add the hw-constraint rule
899 * @runtime: the pcm runtime instance
900 * @cond: condition bits
901 * @var: the variable to evaluate
902 * @func: the evaluation function
903 * @private: the private data pointer passed to function
904 * @dep: the dependent variables
906 * Returns zero if successful, or a negative error code on failure.
908 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
910 snd_pcm_hw_rule_func_t func, void *private,
913 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
914 struct snd_pcm_hw_rule *c;
918 if (constrs->rules_num >= constrs->rules_all) {
919 struct snd_pcm_hw_rule *new;
920 unsigned int new_rules = constrs->rules_all + 16;
921 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
924 if (constrs->rules) {
925 memcpy(new, constrs->rules,
926 constrs->rules_num * sizeof(*c));
927 kfree(constrs->rules);
929 constrs->rules = new;
930 constrs->rules_all = new_rules;
932 c = &constrs->rules[constrs->rules_num];
936 c->private = private;
939 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
944 dep = va_arg(args, int);
946 constrs->rules_num++;
951 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
954 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
955 * @runtime: PCM runtime instance
956 * @var: hw_params variable to apply the mask
957 * @mask: the bitmap mask
959 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
961 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
964 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
965 struct snd_mask *maskp = constrs_mask(constrs, var);
966 *maskp->bits &= mask;
967 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
968 if (*maskp->bits == 0)
974 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
975 * @runtime: PCM runtime instance
976 * @var: hw_params variable to apply the mask
977 * @mask: the 64bit bitmap mask
979 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
981 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
984 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
985 struct snd_mask *maskp = constrs_mask(constrs, var);
986 maskp->bits[0] &= (u_int32_t)mask;
987 maskp->bits[1] &= (u_int32_t)(mask >> 32);
988 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
989 if (! maskp->bits[0] && ! maskp->bits[1])
995 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
996 * @runtime: PCM runtime instance
997 * @var: hw_params variable to apply the integer constraint
999 * Apply the constraint of integer to an interval parameter.
1001 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1003 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1004 return snd_interval_setinteger(constrs_interval(constrs, var));
1007 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1010 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1011 * @runtime: PCM runtime instance
1012 * @var: hw_params variable to apply the range
1013 * @min: the minimal value
1014 * @max: the maximal value
1016 * Apply the min/max range constraint to an interval parameter.
1018 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1019 unsigned int min, unsigned int max)
1021 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1022 struct snd_interval t;
1025 t.openmin = t.openmax = 0;
1027 return snd_interval_refine(constrs_interval(constrs, var), &t);
1030 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1032 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1033 struct snd_pcm_hw_rule *rule)
1035 struct snd_pcm_hw_constraint_list *list = rule->private;
1036 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1041 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1042 * @runtime: PCM runtime instance
1043 * @cond: condition bits
1044 * @var: hw_params variable to apply the list constraint
1047 * Apply the list of constraints to an interval parameter.
1049 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1051 snd_pcm_hw_param_t var,
1052 struct snd_pcm_hw_constraint_list *l)
1054 return snd_pcm_hw_rule_add(runtime, cond, var,
1055 snd_pcm_hw_rule_list, l,
1059 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1061 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1062 struct snd_pcm_hw_rule *rule)
1064 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1065 unsigned int num = 0, den = 0;
1067 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1068 r->nrats, r->rats, &num, &den);
1069 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1070 params->rate_num = num;
1071 params->rate_den = den;
1077 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1078 * @runtime: PCM runtime instance
1079 * @cond: condition bits
1080 * @var: hw_params variable to apply the ratnums constraint
1081 * @r: struct snd_ratnums constriants
1083 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1085 snd_pcm_hw_param_t var,
1086 struct snd_pcm_hw_constraint_ratnums *r)
1088 return snd_pcm_hw_rule_add(runtime, cond, var,
1089 snd_pcm_hw_rule_ratnums, r,
1093 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1095 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1096 struct snd_pcm_hw_rule *rule)
1098 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1099 unsigned int num = 0, den = 0;
1100 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1101 r->nrats, r->rats, &num, &den);
1102 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1103 params->rate_num = num;
1104 params->rate_den = den;
1110 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1111 * @runtime: PCM runtime instance
1112 * @cond: condition bits
1113 * @var: hw_params variable to apply the ratdens constraint
1114 * @r: struct snd_ratdens constriants
1116 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1118 snd_pcm_hw_param_t var,
1119 struct snd_pcm_hw_constraint_ratdens *r)
1121 return snd_pcm_hw_rule_add(runtime, cond, var,
1122 snd_pcm_hw_rule_ratdens, r,
1126 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1128 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1129 struct snd_pcm_hw_rule *rule)
1131 unsigned int l = (unsigned long) rule->private;
1132 int width = l & 0xffff;
1133 unsigned int msbits = l >> 16;
1134 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1135 if (snd_interval_single(i) && snd_interval_value(i) == width)
1136 params->msbits = msbits;
1141 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1142 * @runtime: PCM runtime instance
1143 * @cond: condition bits
1144 * @width: sample bits width
1145 * @msbits: msbits width
1147 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1150 unsigned int msbits)
1152 unsigned long l = (msbits << 16) | width;
1153 return snd_pcm_hw_rule_add(runtime, cond, -1,
1154 snd_pcm_hw_rule_msbits,
1156 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1159 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1161 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1162 struct snd_pcm_hw_rule *rule)
1164 unsigned long step = (unsigned long) rule->private;
1165 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1169 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1170 * @runtime: PCM runtime instance
1171 * @cond: condition bits
1172 * @var: hw_params variable to apply the step constraint
1175 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1177 snd_pcm_hw_param_t var,
1180 return snd_pcm_hw_rule_add(runtime, cond, var,
1181 snd_pcm_hw_rule_step, (void *) step,
1185 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1187 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1189 static unsigned int pow2_sizes[] = {
1190 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1191 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1192 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1193 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1195 return snd_interval_list(hw_param_interval(params, rule->var),
1196 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1200 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1201 * @runtime: PCM runtime instance
1202 * @cond: condition bits
1203 * @var: hw_params variable to apply the power-of-2 constraint
1205 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1207 snd_pcm_hw_param_t var)
1209 return snd_pcm_hw_rule_add(runtime, cond, var,
1210 snd_pcm_hw_rule_pow2, NULL,
1214 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1216 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1217 snd_pcm_hw_param_t var)
1219 if (hw_is_mask(var)) {
1220 snd_mask_any(hw_param_mask(params, var));
1221 params->cmask |= 1 << var;
1222 params->rmask |= 1 << var;
1225 if (hw_is_interval(var)) {
1226 snd_interval_any(hw_param_interval(params, var));
1227 params->cmask |= 1 << var;
1228 params->rmask |= 1 << var;
1234 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1237 memset(params, 0, sizeof(*params));
1238 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1239 _snd_pcm_hw_param_any(params, k);
1240 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1241 _snd_pcm_hw_param_any(params, k);
1245 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1248 * snd_pcm_hw_param_value - return @params field @var value
1249 * @params: the hw_params instance
1250 * @var: parameter to retrieve
1251 * @dir: pointer to the direction (-1,0,1) or %NULL
1253 * Return the value for field @var if it's fixed in configuration space
1254 * defined by @params. Return -%EINVAL otherwise.
1256 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1257 snd_pcm_hw_param_t var, int *dir)
1259 if (hw_is_mask(var)) {
1260 const struct snd_mask *mask = hw_param_mask_c(params, var);
1261 if (!snd_mask_single(mask))
1265 return snd_mask_value(mask);
1267 if (hw_is_interval(var)) {
1268 const struct snd_interval *i = hw_param_interval_c(params, var);
1269 if (!snd_interval_single(i))
1273 return snd_interval_value(i);
1278 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1280 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1281 snd_pcm_hw_param_t var)
1283 if (hw_is_mask(var)) {
1284 snd_mask_none(hw_param_mask(params, var));
1285 params->cmask |= 1 << var;
1286 params->rmask |= 1 << var;
1287 } else if (hw_is_interval(var)) {
1288 snd_interval_none(hw_param_interval(params, var));
1289 params->cmask |= 1 << var;
1290 params->rmask |= 1 << var;
1296 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1298 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1299 snd_pcm_hw_param_t var)
1302 if (hw_is_mask(var))
1303 changed = snd_mask_refine_first(hw_param_mask(params, var));
1304 else if (hw_is_interval(var))
1305 changed = snd_interval_refine_first(hw_param_interval(params, var));
1309 params->cmask |= 1 << var;
1310 params->rmask |= 1 << var;
1317 * snd_pcm_hw_param_first - refine config space and return minimum value
1318 * @pcm: PCM instance
1319 * @params: the hw_params instance
1320 * @var: parameter to retrieve
1321 * @dir: pointer to the direction (-1,0,1) or %NULL
1323 * Inside configuration space defined by @params remove from @var all
1324 * values > minimum. Reduce configuration space accordingly.
1325 * Return the minimum.
1327 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1328 struct snd_pcm_hw_params *params,
1329 snd_pcm_hw_param_t var, int *dir)
1331 int changed = _snd_pcm_hw_param_first(params, var);
1334 if (params->rmask) {
1335 int err = snd_pcm_hw_refine(pcm, params);
1336 if (snd_BUG_ON(err < 0))
1339 return snd_pcm_hw_param_value(params, var, dir);
1342 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1344 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1345 snd_pcm_hw_param_t var)
1348 if (hw_is_mask(var))
1349 changed = snd_mask_refine_last(hw_param_mask(params, var));
1350 else if (hw_is_interval(var))
1351 changed = snd_interval_refine_last(hw_param_interval(params, var));
1355 params->cmask |= 1 << var;
1356 params->rmask |= 1 << var;
1363 * snd_pcm_hw_param_last - refine config space and return maximum value
1364 * @pcm: PCM instance
1365 * @params: the hw_params instance
1366 * @var: parameter to retrieve
1367 * @dir: pointer to the direction (-1,0,1) or %NULL
1369 * Inside configuration space defined by @params remove from @var all
1370 * values < maximum. Reduce configuration space accordingly.
1371 * Return the maximum.
1373 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1374 struct snd_pcm_hw_params *params,
1375 snd_pcm_hw_param_t var, int *dir)
1377 int changed = _snd_pcm_hw_param_last(params, var);
1380 if (params->rmask) {
1381 int err = snd_pcm_hw_refine(pcm, params);
1382 if (snd_BUG_ON(err < 0))
1385 return snd_pcm_hw_param_value(params, var, dir);
1388 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1391 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1392 * @pcm: PCM instance
1393 * @params: the hw_params instance
1395 * Choose one configuration from configuration space defined by @params.
1396 * The configuration chosen is that obtained fixing in this order:
1397 * first access, first format, first subformat, min channels,
1398 * min rate, min period time, max buffer size, min tick time
1400 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1401 struct snd_pcm_hw_params *params)
1403 static int vars[] = {
1404 SNDRV_PCM_HW_PARAM_ACCESS,
1405 SNDRV_PCM_HW_PARAM_FORMAT,
1406 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1407 SNDRV_PCM_HW_PARAM_CHANNELS,
1408 SNDRV_PCM_HW_PARAM_RATE,
1409 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1410 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1411 SNDRV_PCM_HW_PARAM_TICK_TIME,
1416 for (v = vars; *v != -1; v++) {
1417 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1418 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1420 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1421 if (snd_BUG_ON(err < 0))
1427 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1430 struct snd_pcm_runtime *runtime = substream->runtime;
1431 unsigned long flags;
1432 snd_pcm_stream_lock_irqsave(substream, flags);
1433 if (snd_pcm_running(substream) &&
1434 snd_pcm_update_hw_ptr(substream) >= 0)
1435 runtime->status->hw_ptr %= runtime->buffer_size;
1437 runtime->status->hw_ptr = 0;
1438 snd_pcm_stream_unlock_irqrestore(substream, flags);
1442 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1445 struct snd_pcm_channel_info *info = arg;
1446 struct snd_pcm_runtime *runtime = substream->runtime;
1448 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1452 width = snd_pcm_format_physical_width(runtime->format);
1456 switch (runtime->access) {
1457 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1458 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1459 info->first = info->channel * width;
1460 info->step = runtime->channels * width;
1462 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1463 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1465 size_t size = runtime->dma_bytes / runtime->channels;
1466 info->first = info->channel * size * 8;
1478 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1479 * @substream: the pcm substream instance
1480 * @cmd: ioctl command
1481 * @arg: ioctl argument
1483 * Processes the generic ioctl commands for PCM.
1484 * Can be passed as the ioctl callback for PCM ops.
1486 * Returns zero if successful, or a negative error code on failure.
1488 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1489 unsigned int cmd, void *arg)
1492 case SNDRV_PCM_IOCTL1_INFO:
1494 case SNDRV_PCM_IOCTL1_RESET:
1495 return snd_pcm_lib_ioctl_reset(substream, arg);
1496 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1497 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1502 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1505 * snd_pcm_period_elapsed - update the pcm status for the next period
1506 * @substream: the pcm substream instance
1508 * This function is called from the interrupt handler when the
1509 * PCM has processed the period size. It will update the current
1510 * pointer, wake up sleepers, etc.
1512 * Even if more than one periods have elapsed since the last call, you
1513 * have to call this only once.
1515 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1517 struct snd_pcm_runtime *runtime;
1518 unsigned long flags;
1520 if (PCM_RUNTIME_CHECK(substream))
1522 runtime = substream->runtime;
1524 if (runtime->transfer_ack_begin)
1525 runtime->transfer_ack_begin(substream);
1527 snd_pcm_stream_lock_irqsave(substream, flags);
1528 if (!snd_pcm_running(substream) ||
1529 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1532 if (substream->timer_running)
1533 snd_timer_interrupt(substream->timer, 1);
1535 snd_pcm_stream_unlock_irqrestore(substream, flags);
1536 if (runtime->transfer_ack_end)
1537 runtime->transfer_ack_end(substream);
1538 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1541 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1544 * Wait until avail_min data becomes available
1545 * Returns a negative error code if any error occurs during operation.
1546 * The available space is stored on availp. When err = 0 and avail = 0
1547 * on the capture stream, it indicates the stream is in DRAINING state.
1549 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1550 snd_pcm_uframes_t *availp)
1552 struct snd_pcm_runtime *runtime = substream->runtime;
1553 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1556 snd_pcm_uframes_t avail = 0;
1559 init_waitqueue_entry(&wait, current);
1560 add_wait_queue(&runtime->sleep, &wait);
1562 if (signal_pending(current)) {
1566 set_current_state(TASK_INTERRUPTIBLE);
1567 snd_pcm_stream_unlock_irq(substream);
1568 tout = schedule_timeout(msecs_to_jiffies(10000));
1569 snd_pcm_stream_lock_irq(substream);
1570 switch (runtime->status->state) {
1571 case SNDRV_PCM_STATE_SUSPENDED:
1574 case SNDRV_PCM_STATE_XRUN:
1577 case SNDRV_PCM_STATE_DRAINING:
1581 avail = 0; /* indicate draining */
1583 case SNDRV_PCM_STATE_OPEN:
1584 case SNDRV_PCM_STATE_SETUP:
1585 case SNDRV_PCM_STATE_DISCONNECTED:
1590 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1591 is_playback ? "playback" : "capture");
1596 avail = snd_pcm_playback_avail(runtime);
1598 avail = snd_pcm_capture_avail(runtime);
1599 if (avail >= runtime->control->avail_min)
1603 remove_wait_queue(&runtime->sleep, &wait);
1608 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1610 unsigned long data, unsigned int off,
1611 snd_pcm_uframes_t frames)
1613 struct snd_pcm_runtime *runtime = substream->runtime;
1615 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1616 if (substream->ops->copy) {
1617 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1620 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1621 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1627 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1628 unsigned long data, unsigned int off,
1629 snd_pcm_uframes_t size);
1631 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1633 snd_pcm_uframes_t size,
1635 transfer_f transfer)
1637 struct snd_pcm_runtime *runtime = substream->runtime;
1638 snd_pcm_uframes_t xfer = 0;
1639 snd_pcm_uframes_t offset = 0;
1645 snd_pcm_stream_lock_irq(substream);
1646 switch (runtime->status->state) {
1647 case SNDRV_PCM_STATE_PREPARED:
1648 case SNDRV_PCM_STATE_RUNNING:
1649 case SNDRV_PCM_STATE_PAUSED:
1651 case SNDRV_PCM_STATE_XRUN:
1654 case SNDRV_PCM_STATE_SUSPENDED:
1663 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1664 snd_pcm_uframes_t avail;
1665 snd_pcm_uframes_t cont;
1666 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1667 snd_pcm_update_hw_ptr(substream);
1668 avail = snd_pcm_playback_avail(runtime);
1674 err = wait_for_avail_min(substream, &avail);
1678 frames = size > avail ? avail : size;
1679 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1682 if (snd_BUG_ON(!frames)) {
1683 snd_pcm_stream_unlock_irq(substream);
1686 appl_ptr = runtime->control->appl_ptr;
1687 appl_ofs = appl_ptr % runtime->buffer_size;
1688 snd_pcm_stream_unlock_irq(substream);
1689 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1691 snd_pcm_stream_lock_irq(substream);
1692 switch (runtime->status->state) {
1693 case SNDRV_PCM_STATE_XRUN:
1696 case SNDRV_PCM_STATE_SUSPENDED:
1703 if (appl_ptr >= runtime->boundary)
1704 appl_ptr -= runtime->boundary;
1705 runtime->control->appl_ptr = appl_ptr;
1706 if (substream->ops->ack)
1707 substream->ops->ack(substream);
1712 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1713 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1714 err = snd_pcm_start(substream);
1720 snd_pcm_stream_unlock_irq(substream);
1722 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1725 /* sanity-check for read/write methods */
1726 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1728 struct snd_pcm_runtime *runtime;
1729 if (PCM_RUNTIME_CHECK(substream))
1731 runtime = substream->runtime;
1732 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1734 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1739 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1741 struct snd_pcm_runtime *runtime;
1745 err = pcm_sanity_check(substream);
1748 runtime = substream->runtime;
1749 nonblock = !!(substream->f_flags & O_NONBLOCK);
1751 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1752 runtime->channels > 1)
1754 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1755 snd_pcm_lib_write_transfer);
1758 EXPORT_SYMBOL(snd_pcm_lib_write);
1760 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1762 unsigned long data, unsigned int off,
1763 snd_pcm_uframes_t frames)
1765 struct snd_pcm_runtime *runtime = substream->runtime;
1767 void __user **bufs = (void __user **)data;
1768 int channels = runtime->channels;
1770 if (substream->ops->copy) {
1771 if (snd_BUG_ON(!substream->ops->silence))
1773 for (c = 0; c < channels; ++c, ++bufs) {
1774 if (*bufs == NULL) {
1775 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1778 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1779 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1784 /* default transfer behaviour */
1785 size_t dma_csize = runtime->dma_bytes / channels;
1786 for (c = 0; c < channels; ++c, ++bufs) {
1787 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1788 if (*bufs == NULL) {
1789 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1791 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1792 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1800 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1802 snd_pcm_uframes_t frames)
1804 struct snd_pcm_runtime *runtime;
1808 err = pcm_sanity_check(substream);
1811 runtime = substream->runtime;
1812 nonblock = !!(substream->f_flags & O_NONBLOCK);
1814 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1816 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1817 nonblock, snd_pcm_lib_writev_transfer);
1820 EXPORT_SYMBOL(snd_pcm_lib_writev);
1822 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1824 unsigned long data, unsigned int off,
1825 snd_pcm_uframes_t frames)
1827 struct snd_pcm_runtime *runtime = substream->runtime;
1829 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1830 if (substream->ops->copy) {
1831 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1834 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1835 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1841 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1843 snd_pcm_uframes_t size,
1845 transfer_f transfer)
1847 struct snd_pcm_runtime *runtime = substream->runtime;
1848 snd_pcm_uframes_t xfer = 0;
1849 snd_pcm_uframes_t offset = 0;
1855 snd_pcm_stream_lock_irq(substream);
1856 switch (runtime->status->state) {
1857 case SNDRV_PCM_STATE_PREPARED:
1858 if (size >= runtime->start_threshold) {
1859 err = snd_pcm_start(substream);
1864 case SNDRV_PCM_STATE_DRAINING:
1865 case SNDRV_PCM_STATE_RUNNING:
1866 case SNDRV_PCM_STATE_PAUSED:
1868 case SNDRV_PCM_STATE_XRUN:
1871 case SNDRV_PCM_STATE_SUSPENDED:
1880 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1881 snd_pcm_uframes_t avail;
1882 snd_pcm_uframes_t cont;
1883 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1884 snd_pcm_update_hw_ptr(substream);
1885 avail = snd_pcm_capture_avail(runtime);
1887 if (runtime->status->state ==
1888 SNDRV_PCM_STATE_DRAINING) {
1889 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1896 err = wait_for_avail_min(substream, &avail);
1900 continue; /* draining */
1902 frames = size > avail ? avail : size;
1903 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1906 if (snd_BUG_ON(!frames)) {
1907 snd_pcm_stream_unlock_irq(substream);
1910 appl_ptr = runtime->control->appl_ptr;
1911 appl_ofs = appl_ptr % runtime->buffer_size;
1912 snd_pcm_stream_unlock_irq(substream);
1913 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1915 snd_pcm_stream_lock_irq(substream);
1916 switch (runtime->status->state) {
1917 case SNDRV_PCM_STATE_XRUN:
1920 case SNDRV_PCM_STATE_SUSPENDED:
1927 if (appl_ptr >= runtime->boundary)
1928 appl_ptr -= runtime->boundary;
1929 runtime->control->appl_ptr = appl_ptr;
1930 if (substream->ops->ack)
1931 substream->ops->ack(substream);
1938 snd_pcm_stream_unlock_irq(substream);
1940 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1943 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1945 struct snd_pcm_runtime *runtime;
1949 err = pcm_sanity_check(substream);
1952 runtime = substream->runtime;
1953 nonblock = !!(substream->f_flags & O_NONBLOCK);
1954 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1956 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1959 EXPORT_SYMBOL(snd_pcm_lib_read);
1961 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1963 unsigned long data, unsigned int off,
1964 snd_pcm_uframes_t frames)
1966 struct snd_pcm_runtime *runtime = substream->runtime;
1968 void __user **bufs = (void __user **)data;
1969 int channels = runtime->channels;
1971 if (substream->ops->copy) {
1972 for (c = 0; c < channels; ++c, ++bufs) {
1976 buf = *bufs + samples_to_bytes(runtime, off);
1977 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1981 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1982 for (c = 0; c < channels; ++c, ++bufs) {
1988 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1989 buf = *bufs + samples_to_bytes(runtime, off);
1990 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1997 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1999 snd_pcm_uframes_t frames)
2001 struct snd_pcm_runtime *runtime;
2005 err = pcm_sanity_check(substream);
2008 runtime = substream->runtime;
2009 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2012 nonblock = !!(substream->f_flags & O_NONBLOCK);
2013 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2015 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2018 EXPORT_SYMBOL(snd_pcm_lib_readv);