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 static void xrun(struct snd_pcm_substream *substream)
130 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
131 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
132 if (substream->pstr->xrun_debug) {
133 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
134 substream->pcm->card->number,
135 substream->pcm->device,
136 substream->stream ? 'c' : 'p');
137 if (substream->pstr->xrun_debug > 1)
143 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
144 struct snd_pcm_runtime *runtime)
146 snd_pcm_uframes_t pos;
148 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
149 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
150 pos = substream->ops->pointer(substream);
151 if (pos == SNDRV_PCM_POS_XRUN)
152 return pos; /* XRUN */
153 #ifdef CONFIG_SND_DEBUG
154 if (pos >= runtime->buffer_size) {
155 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);
158 pos -= pos % runtime->min_align;
162 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
163 struct snd_pcm_runtime *runtime)
165 snd_pcm_uframes_t avail;
167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
168 avail = snd_pcm_playback_avail(runtime);
170 avail = snd_pcm_capture_avail(runtime);
171 if (avail > runtime->avail_max)
172 runtime->avail_max = avail;
173 if (avail >= runtime->stop_threshold) {
174 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
175 snd_pcm_drain_done(substream);
180 if (avail >= runtime->control->avail_min)
181 wake_up(&runtime->sleep);
185 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 snd_pcm_uframes_t pos;
189 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
190 snd_pcm_sframes_t delta;
192 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
193 if (pos == SNDRV_PCM_POS_XRUN) {
197 if (runtime->period_size == runtime->buffer_size)
199 new_hw_ptr = runtime->hw_ptr_base + pos;
200 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
202 delta = hw_ptr_interrupt - new_hw_ptr;
204 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
205 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
206 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
207 snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
208 if (substream->pstr->xrun_debug > 1)
215 runtime->hw_ptr_base += runtime->buffer_size;
216 if (runtime->hw_ptr_base == runtime->boundary)
217 runtime->hw_ptr_base = 0;
218 new_hw_ptr = runtime->hw_ptr_base + pos;
221 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
222 runtime->silence_size > 0)
223 snd_pcm_playback_silence(substream, new_hw_ptr);
225 runtime->status->hw_ptr = new_hw_ptr;
226 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
228 return snd_pcm_update_hw_ptr_post(substream, runtime);
231 /* CAUTION: call it with irq disabled */
232 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
234 struct snd_pcm_runtime *runtime = substream->runtime;
235 snd_pcm_uframes_t pos;
236 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
237 snd_pcm_sframes_t delta;
239 old_hw_ptr = runtime->status->hw_ptr;
240 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
241 if (pos == SNDRV_PCM_POS_XRUN) {
245 new_hw_ptr = runtime->hw_ptr_base + pos;
247 delta = old_hw_ptr - new_hw_ptr;
249 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
250 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
251 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
252 snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
253 if (substream->pstr->xrun_debug > 1)
259 runtime->hw_ptr_base += runtime->buffer_size;
260 if (runtime->hw_ptr_base == runtime->boundary)
261 runtime->hw_ptr_base = 0;
262 new_hw_ptr = runtime->hw_ptr_base + pos;
264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
265 runtime->silence_size > 0)
266 snd_pcm_playback_silence(substream, new_hw_ptr);
268 runtime->status->hw_ptr = new_hw_ptr;
270 return snd_pcm_update_hw_ptr_post(substream, runtime);
274 * snd_pcm_set_ops - set the PCM operators
275 * @pcm: the pcm instance
276 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
277 * @ops: the operator table
279 * Sets the given PCM operators to the pcm instance.
281 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
283 struct snd_pcm_str *stream = &pcm->streams[direction];
284 struct snd_pcm_substream *substream;
286 for (substream = stream->substream; substream != NULL; substream = substream->next)
287 substream->ops = ops;
290 EXPORT_SYMBOL(snd_pcm_set_ops);
293 * snd_pcm_sync - set the PCM sync id
294 * @substream: the pcm substream
296 * Sets the PCM sync identifier for the card.
298 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
300 struct snd_pcm_runtime *runtime = substream->runtime;
302 runtime->sync.id32[0] = substream->pcm->card->number;
303 runtime->sync.id32[1] = -1;
304 runtime->sync.id32[2] = -1;
305 runtime->sync.id32[3] = -1;
308 EXPORT_SYMBOL(snd_pcm_set_sync);
311 * Standard ioctl routine
314 static inline unsigned int div32(unsigned int a, unsigned int b,
325 static inline unsigned int div_down(unsigned int a, unsigned int b)
332 static inline unsigned int div_up(unsigned int a, unsigned int b)
344 static inline unsigned int mul(unsigned int a, unsigned int b)
348 if (div_down(UINT_MAX, a) < b)
353 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
354 unsigned int c, unsigned int *r)
356 u_int64_t n = (u_int64_t) a * b;
371 * snd_interval_refine - refine the interval value of configurator
372 * @i: the interval value to refine
373 * @v: the interval value to refer to
375 * Refines the interval value with the reference value.
376 * The interval is changed to the range satisfying both intervals.
377 * The interval status (min, max, integer, etc.) are evaluated.
379 * Returns non-zero if the value is changed, zero if not changed.
381 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
384 if (snd_BUG_ON(snd_interval_empty(i)))
386 if (i->min < v->min) {
388 i->openmin = v->openmin;
390 } else if (i->min == v->min && !i->openmin && v->openmin) {
394 if (i->max > v->max) {
396 i->openmax = v->openmax;
398 } else if (i->max == v->max && !i->openmax && v->openmax) {
402 if (!i->integer && v->integer) {
415 } else if (!i->openmin && !i->openmax && i->min == i->max)
417 if (snd_interval_checkempty(i)) {
418 snd_interval_none(i);
424 EXPORT_SYMBOL(snd_interval_refine);
426 static int snd_interval_refine_first(struct snd_interval *i)
428 if (snd_BUG_ON(snd_interval_empty(i)))
430 if (snd_interval_single(i))
433 i->openmax = i->openmin;
439 static int snd_interval_refine_last(struct snd_interval *i)
441 if (snd_BUG_ON(snd_interval_empty(i)))
443 if (snd_interval_single(i))
446 i->openmin = i->openmax;
452 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
454 if (a->empty || b->empty) {
455 snd_interval_none(c);
459 c->min = mul(a->min, b->min);
460 c->openmin = (a->openmin || b->openmin);
461 c->max = mul(a->max, b->max);
462 c->openmax = (a->openmax || b->openmax);
463 c->integer = (a->integer && b->integer);
467 * snd_interval_div - refine the interval value with division
474 * Returns non-zero if the value is changed, zero if not changed.
476 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
479 if (a->empty || b->empty) {
480 snd_interval_none(c);
484 c->min = div32(a->min, b->max, &r);
485 c->openmin = (r || a->openmin || b->openmax);
487 c->max = div32(a->max, b->min, &r);
492 c->openmax = (a->openmax || b->openmin);
501 * snd_interval_muldivk - refine the interval value
504 * @k: divisor (as integer)
509 * Returns non-zero if the value is changed, zero if not changed.
511 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
512 unsigned int k, struct snd_interval *c)
515 if (a->empty || b->empty) {
516 snd_interval_none(c);
520 c->min = muldiv32(a->min, b->min, k, &r);
521 c->openmin = (r || a->openmin || b->openmin);
522 c->max = muldiv32(a->max, b->max, k, &r);
527 c->openmax = (a->openmax || b->openmax);
532 * snd_interval_mulkdiv - refine the interval value
534 * @k: dividend 2 (as integer)
540 * Returns non-zero if the value is changed, zero if not changed.
542 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
543 const struct snd_interval *b, struct snd_interval *c)
546 if (a->empty || b->empty) {
547 snd_interval_none(c);
551 c->min = muldiv32(a->min, k, b->max, &r);
552 c->openmin = (r || a->openmin || b->openmax);
554 c->max = muldiv32(a->max, k, b->min, &r);
559 c->openmax = (a->openmax || b->openmin);
571 * snd_interval_ratnum - refine the interval value
572 * @i: interval to refine
573 * @rats_count: number of ratnum_t
574 * @rats: ratnum_t array
575 * @nump: pointer to store the resultant numerator
576 * @denp: pointer to store the resultant denominator
578 * Returns non-zero if the value is changed, zero if not changed.
580 int snd_interval_ratnum(struct snd_interval *i,
581 unsigned int rats_count, struct snd_ratnum *rats,
582 unsigned int *nump, unsigned int *denp)
584 unsigned int best_num, best_diff, best_den;
586 struct snd_interval t;
589 best_num = best_den = best_diff = 0;
590 for (k = 0; k < rats_count; ++k) {
591 unsigned int num = rats[k].num;
593 unsigned int q = i->min;
597 den = div_down(num, q);
598 if (den < rats[k].den_min)
600 if (den > rats[k].den_max)
601 den = rats[k].den_max;
604 r = (den - rats[k].den_min) % rats[k].den_step;
608 diff = num - q * den;
610 diff * best_den < best_diff * den) {
620 t.min = div_down(best_num, best_den);
621 t.openmin = !!(best_num % best_den);
623 best_num = best_den = best_diff = 0;
624 for (k = 0; k < rats_count; ++k) {
625 unsigned int num = rats[k].num;
627 unsigned int q = i->max;
633 den = div_up(num, q);
634 if (den > rats[k].den_max)
636 if (den < rats[k].den_min)
637 den = rats[k].den_min;
640 r = (den - rats[k].den_min) % rats[k].den_step;
642 den += rats[k].den_step - r;
644 diff = q * den - num;
646 diff * best_den < best_diff * den) {
656 t.max = div_up(best_num, best_den);
657 t.openmax = !!(best_num % best_den);
659 err = snd_interval_refine(i, &t);
663 if (snd_interval_single(i)) {
672 EXPORT_SYMBOL(snd_interval_ratnum);
675 * snd_interval_ratden - refine the interval value
676 * @i: interval to refine
677 * @rats_count: number of struct ratden
678 * @rats: struct ratden array
679 * @nump: pointer to store the resultant numerator
680 * @denp: pointer to store the resultant denominator
682 * Returns non-zero if the value is changed, zero if not changed.
684 static int snd_interval_ratden(struct snd_interval *i,
685 unsigned int rats_count, struct snd_ratden *rats,
686 unsigned int *nump, unsigned int *denp)
688 unsigned int best_num, best_diff, best_den;
690 struct snd_interval t;
693 best_num = best_den = best_diff = 0;
694 for (k = 0; k < rats_count; ++k) {
696 unsigned int den = rats[k].den;
697 unsigned int q = i->min;
700 if (num > rats[k].num_max)
702 if (num < rats[k].num_min)
703 num = rats[k].num_max;
706 r = (num - rats[k].num_min) % rats[k].num_step;
708 num += rats[k].num_step - r;
710 diff = num - q * den;
712 diff * best_den < best_diff * den) {
722 t.min = div_down(best_num, best_den);
723 t.openmin = !!(best_num % best_den);
725 best_num = best_den = best_diff = 0;
726 for (k = 0; k < rats_count; ++k) {
728 unsigned int den = rats[k].den;
729 unsigned int q = i->max;
732 if (num < rats[k].num_min)
734 if (num > rats[k].num_max)
735 num = rats[k].num_max;
738 r = (num - rats[k].num_min) % rats[k].num_step;
742 diff = q * den - num;
744 diff * best_den < best_diff * den) {
754 t.max = div_up(best_num, best_den);
755 t.openmax = !!(best_num % best_den);
757 err = snd_interval_refine(i, &t);
761 if (snd_interval_single(i)) {
771 * snd_interval_list - refine the interval value from the list
772 * @i: the interval value to refine
773 * @count: the number of elements in the list
774 * @list: the value list
775 * @mask: the bit-mask to evaluate
777 * Refines the interval value from the list.
778 * When mask is non-zero, only the elements corresponding to bit 1 are
781 * Returns non-zero if the value is changed, zero if not changed.
783 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
792 for (k = 0; k < count; k++) {
793 if (mask && !(mask & (1 << k)))
795 if (i->min == list[k] && !i->openmin)
797 if (i->min < list[k]) {
807 for (k = count; k-- > 0;) {
808 if (mask && !(mask & (1 << k)))
810 if (i->max == list[k] && !i->openmax)
812 if (i->max > list[k]) {
822 if (snd_interval_checkempty(i)) {
829 EXPORT_SYMBOL(snd_interval_list);
831 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
835 n = (i->min - min) % step;
836 if (n != 0 || i->openmin) {
840 n = (i->max - min) % step;
841 if (n != 0 || i->openmax) {
845 if (snd_interval_checkempty(i)) {
852 /* Info constraints helpers */
855 * snd_pcm_hw_rule_add - add the hw-constraint rule
856 * @runtime: the pcm runtime instance
857 * @cond: condition bits
858 * @var: the variable to evaluate
859 * @func: the evaluation function
860 * @private: the private data pointer passed to function
861 * @dep: the dependent variables
863 * Returns zero if successful, or a negative error code on failure.
865 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
867 snd_pcm_hw_rule_func_t func, void *private,
870 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
871 struct snd_pcm_hw_rule *c;
875 if (constrs->rules_num >= constrs->rules_all) {
876 struct snd_pcm_hw_rule *new;
877 unsigned int new_rules = constrs->rules_all + 16;
878 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
881 if (constrs->rules) {
882 memcpy(new, constrs->rules,
883 constrs->rules_num * sizeof(*c));
884 kfree(constrs->rules);
886 constrs->rules = new;
887 constrs->rules_all = new_rules;
889 c = &constrs->rules[constrs->rules_num];
893 c->private = private;
896 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
901 dep = va_arg(args, int);
903 constrs->rules_num++;
908 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
911 * snd_pcm_hw_constraint_mask
912 * @runtime: PCM runtime instance
913 * @var: hw_params variable to apply the mask
914 * @mask: the bitmap mask
916 * Apply the constraint of the given bitmap mask to a mask parameter.
918 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
921 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
922 struct snd_mask *maskp = constrs_mask(constrs, var);
923 *maskp->bits &= mask;
924 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
925 if (*maskp->bits == 0)
931 * snd_pcm_hw_constraint_mask64
932 * @runtime: PCM runtime instance
933 * @var: hw_params variable to apply the mask
934 * @mask: the 64bit bitmap mask
936 * Apply the constraint of the given bitmap mask to a mask parameter.
938 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
941 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
942 struct snd_mask *maskp = constrs_mask(constrs, var);
943 maskp->bits[0] &= (u_int32_t)mask;
944 maskp->bits[1] &= (u_int32_t)(mask >> 32);
945 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
946 if (! maskp->bits[0] && ! maskp->bits[1])
952 * snd_pcm_hw_constraint_integer
953 * @runtime: PCM runtime instance
954 * @var: hw_params variable to apply the integer constraint
956 * Apply the constraint of integer to an interval parameter.
958 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
960 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
961 return snd_interval_setinteger(constrs_interval(constrs, var));
964 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
967 * snd_pcm_hw_constraint_minmax
968 * @runtime: PCM runtime instance
969 * @var: hw_params variable to apply the range
970 * @min: the minimal value
971 * @max: the maximal value
973 * Apply the min/max range constraint to an interval parameter.
975 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
976 unsigned int min, unsigned int max)
978 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
979 struct snd_interval t;
982 t.openmin = t.openmax = 0;
984 return snd_interval_refine(constrs_interval(constrs, var), &t);
987 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
989 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
990 struct snd_pcm_hw_rule *rule)
992 struct snd_pcm_hw_constraint_list *list = rule->private;
993 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
998 * snd_pcm_hw_constraint_list
999 * @runtime: PCM runtime instance
1000 * @cond: condition bits
1001 * @var: hw_params variable to apply the list constraint
1004 * Apply the list of constraints to an interval parameter.
1006 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1008 snd_pcm_hw_param_t var,
1009 struct snd_pcm_hw_constraint_list *l)
1011 return snd_pcm_hw_rule_add(runtime, cond, var,
1012 snd_pcm_hw_rule_list, l,
1016 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1018 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1019 struct snd_pcm_hw_rule *rule)
1021 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1022 unsigned int num = 0, den = 0;
1024 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1025 r->nrats, r->rats, &num, &den);
1026 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1027 params->rate_num = num;
1028 params->rate_den = den;
1034 * snd_pcm_hw_constraint_ratnums
1035 * @runtime: PCM runtime instance
1036 * @cond: condition bits
1037 * @var: hw_params variable to apply the ratnums constraint
1038 * @r: struct snd_ratnums constriants
1040 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1042 snd_pcm_hw_param_t var,
1043 struct snd_pcm_hw_constraint_ratnums *r)
1045 return snd_pcm_hw_rule_add(runtime, cond, var,
1046 snd_pcm_hw_rule_ratnums, r,
1050 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1052 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1053 struct snd_pcm_hw_rule *rule)
1055 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1056 unsigned int num = 0, den = 0;
1057 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1058 r->nrats, r->rats, &num, &den);
1059 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1060 params->rate_num = num;
1061 params->rate_den = den;
1067 * snd_pcm_hw_constraint_ratdens
1068 * @runtime: PCM runtime instance
1069 * @cond: condition bits
1070 * @var: hw_params variable to apply the ratdens constraint
1071 * @r: struct snd_ratdens constriants
1073 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1075 snd_pcm_hw_param_t var,
1076 struct snd_pcm_hw_constraint_ratdens *r)
1078 return snd_pcm_hw_rule_add(runtime, cond, var,
1079 snd_pcm_hw_rule_ratdens, r,
1083 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1085 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1086 struct snd_pcm_hw_rule *rule)
1088 unsigned int l = (unsigned long) rule->private;
1089 int width = l & 0xffff;
1090 unsigned int msbits = l >> 16;
1091 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1092 if (snd_interval_single(i) && snd_interval_value(i) == width)
1093 params->msbits = msbits;
1098 * snd_pcm_hw_constraint_msbits
1099 * @runtime: PCM runtime instance
1100 * @cond: condition bits
1101 * @width: sample bits width
1102 * @msbits: msbits width
1104 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1107 unsigned int msbits)
1109 unsigned long l = (msbits << 16) | width;
1110 return snd_pcm_hw_rule_add(runtime, cond, -1,
1111 snd_pcm_hw_rule_msbits,
1113 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1116 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1118 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1119 struct snd_pcm_hw_rule *rule)
1121 unsigned long step = (unsigned long) rule->private;
1122 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1126 * snd_pcm_hw_constraint_step
1127 * @runtime: PCM runtime instance
1128 * @cond: condition bits
1129 * @var: hw_params variable to apply the step constraint
1132 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1134 snd_pcm_hw_param_t var,
1137 return snd_pcm_hw_rule_add(runtime, cond, var,
1138 snd_pcm_hw_rule_step, (void *) step,
1142 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1144 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1146 static unsigned int pow2_sizes[] = {
1147 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1148 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1149 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1150 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1152 return snd_interval_list(hw_param_interval(params, rule->var),
1153 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1157 * snd_pcm_hw_constraint_pow2
1158 * @runtime: PCM runtime instance
1159 * @cond: condition bits
1160 * @var: hw_params variable to apply the power-of-2 constraint
1162 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1164 snd_pcm_hw_param_t var)
1166 return snd_pcm_hw_rule_add(runtime, cond, var,
1167 snd_pcm_hw_rule_pow2, NULL,
1171 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1173 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1174 snd_pcm_hw_param_t var)
1176 if (hw_is_mask(var)) {
1177 snd_mask_any(hw_param_mask(params, var));
1178 params->cmask |= 1 << var;
1179 params->rmask |= 1 << var;
1182 if (hw_is_interval(var)) {
1183 snd_interval_any(hw_param_interval(params, var));
1184 params->cmask |= 1 << var;
1185 params->rmask |= 1 << var;
1191 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1194 memset(params, 0, sizeof(*params));
1195 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1196 _snd_pcm_hw_param_any(params, k);
1197 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1198 _snd_pcm_hw_param_any(params, k);
1202 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1205 * snd_pcm_hw_param_value
1206 * @params: the hw_params instance
1207 * @var: parameter to retrieve
1208 * @dir: pointer to the direction (-1,0,1) or NULL
1210 * Return the value for field PAR if it's fixed in configuration space
1211 * defined by PARAMS. Return -EINVAL otherwise
1213 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1214 snd_pcm_hw_param_t var, int *dir)
1216 if (hw_is_mask(var)) {
1217 const struct snd_mask *mask = hw_param_mask_c(params, var);
1218 if (!snd_mask_single(mask))
1222 return snd_mask_value(mask);
1224 if (hw_is_interval(var)) {
1225 const struct snd_interval *i = hw_param_interval_c(params, var);
1226 if (!snd_interval_single(i))
1230 return snd_interval_value(i);
1235 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1237 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1238 snd_pcm_hw_param_t var)
1240 if (hw_is_mask(var)) {
1241 snd_mask_none(hw_param_mask(params, var));
1242 params->cmask |= 1 << var;
1243 params->rmask |= 1 << var;
1244 } else if (hw_is_interval(var)) {
1245 snd_interval_none(hw_param_interval(params, var));
1246 params->cmask |= 1 << var;
1247 params->rmask |= 1 << var;
1253 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1255 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1256 snd_pcm_hw_param_t var)
1259 if (hw_is_mask(var))
1260 changed = snd_mask_refine_first(hw_param_mask(params, var));
1261 else if (hw_is_interval(var))
1262 changed = snd_interval_refine_first(hw_param_interval(params, var));
1266 params->cmask |= 1 << var;
1267 params->rmask |= 1 << var;
1274 * snd_pcm_hw_param_first
1275 * @pcm: PCM instance
1276 * @params: the hw_params instance
1277 * @var: parameter to retrieve
1278 * @dir: pointer to the direction (-1,0,1) or NULL
1280 * Inside configuration space defined by PARAMS remove from PAR all
1281 * values > minimum. Reduce configuration space accordingly.
1282 * Return the minimum.
1284 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1285 struct snd_pcm_hw_params *params,
1286 snd_pcm_hw_param_t var, int *dir)
1288 int changed = _snd_pcm_hw_param_first(params, var);
1291 if (params->rmask) {
1292 int err = snd_pcm_hw_refine(pcm, params);
1293 if (snd_BUG_ON(err < 0))
1296 return snd_pcm_hw_param_value(params, var, dir);
1299 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1301 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1302 snd_pcm_hw_param_t var)
1305 if (hw_is_mask(var))
1306 changed = snd_mask_refine_last(hw_param_mask(params, var));
1307 else if (hw_is_interval(var))
1308 changed = snd_interval_refine_last(hw_param_interval(params, var));
1312 params->cmask |= 1 << var;
1313 params->rmask |= 1 << var;
1320 * snd_pcm_hw_param_last
1321 * @pcm: PCM instance
1322 * @params: the hw_params instance
1323 * @var: parameter to retrieve
1324 * @dir: pointer to the direction (-1,0,1) or NULL
1326 * Inside configuration space defined by PARAMS remove from PAR all
1327 * values < maximum. Reduce configuration space accordingly.
1328 * Return the maximum.
1330 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1331 struct snd_pcm_hw_params *params,
1332 snd_pcm_hw_param_t var, int *dir)
1334 int changed = _snd_pcm_hw_param_last(params, var);
1337 if (params->rmask) {
1338 int err = snd_pcm_hw_refine(pcm, params);
1339 if (snd_BUG_ON(err < 0))
1342 return snd_pcm_hw_param_value(params, var, dir);
1345 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1348 * snd_pcm_hw_param_choose
1349 * @pcm: PCM instance
1350 * @params: the hw_params instance
1352 * Choose one configuration from configuration space defined by PARAMS
1353 * The configuration chosen is that obtained fixing in this order:
1354 * first access, first format, first subformat, min channels,
1355 * min rate, min period time, max buffer size, min tick time
1357 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1358 struct snd_pcm_hw_params *params)
1360 static int vars[] = {
1361 SNDRV_PCM_HW_PARAM_ACCESS,
1362 SNDRV_PCM_HW_PARAM_FORMAT,
1363 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1364 SNDRV_PCM_HW_PARAM_CHANNELS,
1365 SNDRV_PCM_HW_PARAM_RATE,
1366 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1367 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1368 SNDRV_PCM_HW_PARAM_TICK_TIME,
1373 for (v = vars; *v != -1; v++) {
1374 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1375 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1377 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1378 if (snd_BUG_ON(err < 0))
1384 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1387 struct snd_pcm_runtime *runtime = substream->runtime;
1388 unsigned long flags;
1389 snd_pcm_stream_lock_irqsave(substream, flags);
1390 if (snd_pcm_running(substream) &&
1391 snd_pcm_update_hw_ptr(substream) >= 0)
1392 runtime->status->hw_ptr %= runtime->buffer_size;
1394 runtime->status->hw_ptr = 0;
1395 snd_pcm_stream_unlock_irqrestore(substream, flags);
1399 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1402 struct snd_pcm_channel_info *info = arg;
1403 struct snd_pcm_runtime *runtime = substream->runtime;
1405 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1409 width = snd_pcm_format_physical_width(runtime->format);
1413 switch (runtime->access) {
1414 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1415 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1416 info->first = info->channel * width;
1417 info->step = runtime->channels * width;
1419 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1420 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1422 size_t size = runtime->dma_bytes / runtime->channels;
1423 info->first = info->channel * size * 8;
1435 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1436 * @substream: the pcm substream instance
1437 * @cmd: ioctl command
1438 * @arg: ioctl argument
1440 * Processes the generic ioctl commands for PCM.
1441 * Can be passed as the ioctl callback for PCM ops.
1443 * Returns zero if successful, or a negative error code on failure.
1445 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1446 unsigned int cmd, void *arg)
1449 case SNDRV_PCM_IOCTL1_INFO:
1451 case SNDRV_PCM_IOCTL1_RESET:
1452 return snd_pcm_lib_ioctl_reset(substream, arg);
1453 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1454 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1459 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1462 * snd_pcm_period_elapsed - update the pcm status for the next period
1463 * @substream: the pcm substream instance
1465 * This function is called from the interrupt handler when the
1466 * PCM has processed the period size. It will update the current
1467 * pointer, wake up sleepers, etc.
1469 * Even if more than one periods have elapsed since the last call, you
1470 * have to call this only once.
1472 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1474 struct snd_pcm_runtime *runtime;
1475 unsigned long flags;
1477 if (PCM_RUNTIME_CHECK(substream))
1479 runtime = substream->runtime;
1481 if (runtime->transfer_ack_begin)
1482 runtime->transfer_ack_begin(substream);
1484 snd_pcm_stream_lock_irqsave(substream, flags);
1485 if (!snd_pcm_running(substream) ||
1486 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1489 if (substream->timer_running)
1490 snd_timer_interrupt(substream->timer, 1);
1492 snd_pcm_stream_unlock_irqrestore(substream, flags);
1493 if (runtime->transfer_ack_end)
1494 runtime->transfer_ack_end(substream);
1495 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1498 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1501 * Wait until avail_min data becomes available
1502 * Returns a negative error code if any error occurs during operation.
1503 * The available space is stored on availp. When err = 0 and avail = 0
1504 * on the capture stream, it indicates the stream is in DRAINING state.
1506 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1507 snd_pcm_uframes_t *availp)
1509 struct snd_pcm_runtime *runtime = substream->runtime;
1510 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1513 snd_pcm_uframes_t avail = 0;
1516 init_waitqueue_entry(&wait, current);
1517 add_wait_queue(&runtime->sleep, &wait);
1519 if (signal_pending(current)) {
1523 set_current_state(TASK_INTERRUPTIBLE);
1524 snd_pcm_stream_unlock_irq(substream);
1525 tout = schedule_timeout(msecs_to_jiffies(10000));
1526 snd_pcm_stream_lock_irq(substream);
1527 switch (runtime->status->state) {
1528 case SNDRV_PCM_STATE_SUSPENDED:
1531 case SNDRV_PCM_STATE_XRUN:
1534 case SNDRV_PCM_STATE_DRAINING:
1538 avail = 0; /* indicate draining */
1540 case SNDRV_PCM_STATE_OPEN:
1541 case SNDRV_PCM_STATE_SETUP:
1542 case SNDRV_PCM_STATE_DISCONNECTED:
1547 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1548 is_playback ? "playback" : "capture");
1553 avail = snd_pcm_playback_avail(runtime);
1555 avail = snd_pcm_capture_avail(runtime);
1556 if (avail >= runtime->control->avail_min)
1560 remove_wait_queue(&runtime->sleep, &wait);
1565 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1567 unsigned long data, unsigned int off,
1568 snd_pcm_uframes_t frames)
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1572 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1573 if (substream->ops->copy) {
1574 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1577 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1578 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1584 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1585 unsigned long data, unsigned int off,
1586 snd_pcm_uframes_t size);
1588 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1590 snd_pcm_uframes_t size,
1592 transfer_f transfer)
1594 struct snd_pcm_runtime *runtime = substream->runtime;
1595 snd_pcm_uframes_t xfer = 0;
1596 snd_pcm_uframes_t offset = 0;
1602 snd_pcm_stream_lock_irq(substream);
1603 switch (runtime->status->state) {
1604 case SNDRV_PCM_STATE_PREPARED:
1605 case SNDRV_PCM_STATE_RUNNING:
1606 case SNDRV_PCM_STATE_PAUSED:
1608 case SNDRV_PCM_STATE_XRUN:
1611 case SNDRV_PCM_STATE_SUSPENDED:
1620 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1621 snd_pcm_uframes_t avail;
1622 snd_pcm_uframes_t cont;
1623 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1624 snd_pcm_update_hw_ptr(substream);
1625 avail = snd_pcm_playback_avail(runtime);
1631 err = wait_for_avail_min(substream, &avail);
1635 frames = size > avail ? avail : size;
1636 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1639 if (snd_BUG_ON(!frames)) {
1640 snd_pcm_stream_unlock_irq(substream);
1643 appl_ptr = runtime->control->appl_ptr;
1644 appl_ofs = appl_ptr % runtime->buffer_size;
1645 snd_pcm_stream_unlock_irq(substream);
1646 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1648 snd_pcm_stream_lock_irq(substream);
1649 switch (runtime->status->state) {
1650 case SNDRV_PCM_STATE_XRUN:
1653 case SNDRV_PCM_STATE_SUSPENDED:
1660 if (appl_ptr >= runtime->boundary)
1661 appl_ptr -= runtime->boundary;
1662 runtime->control->appl_ptr = appl_ptr;
1663 if (substream->ops->ack)
1664 substream->ops->ack(substream);
1669 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1670 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1671 err = snd_pcm_start(substream);
1677 snd_pcm_stream_unlock_irq(substream);
1679 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1682 /* sanity-check for read/write methods */
1683 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1685 struct snd_pcm_runtime *runtime;
1686 if (PCM_RUNTIME_CHECK(substream))
1688 runtime = substream->runtime;
1689 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1691 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1696 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1698 struct snd_pcm_runtime *runtime;
1702 err = pcm_sanity_check(substream);
1705 runtime = substream->runtime;
1706 nonblock = !!(substream->f_flags & O_NONBLOCK);
1708 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1709 runtime->channels > 1)
1711 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1712 snd_pcm_lib_write_transfer);
1715 EXPORT_SYMBOL(snd_pcm_lib_write);
1717 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1719 unsigned long data, unsigned int off,
1720 snd_pcm_uframes_t frames)
1722 struct snd_pcm_runtime *runtime = substream->runtime;
1724 void __user **bufs = (void __user **)data;
1725 int channels = runtime->channels;
1727 if (substream->ops->copy) {
1728 if (snd_BUG_ON(!substream->ops->silence))
1730 for (c = 0; c < channels; ++c, ++bufs) {
1731 if (*bufs == NULL) {
1732 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1735 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1736 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1741 /* default transfer behaviour */
1742 size_t dma_csize = runtime->dma_bytes / channels;
1743 for (c = 0; c < channels; ++c, ++bufs) {
1744 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1745 if (*bufs == NULL) {
1746 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1748 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1749 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1757 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1759 snd_pcm_uframes_t frames)
1761 struct snd_pcm_runtime *runtime;
1765 err = pcm_sanity_check(substream);
1768 runtime = substream->runtime;
1769 nonblock = !!(substream->f_flags & O_NONBLOCK);
1771 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1773 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1774 nonblock, snd_pcm_lib_writev_transfer);
1777 EXPORT_SYMBOL(snd_pcm_lib_writev);
1779 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1781 unsigned long data, unsigned int off,
1782 snd_pcm_uframes_t frames)
1784 struct snd_pcm_runtime *runtime = substream->runtime;
1786 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1787 if (substream->ops->copy) {
1788 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1791 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1792 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1798 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1800 snd_pcm_uframes_t size,
1802 transfer_f transfer)
1804 struct snd_pcm_runtime *runtime = substream->runtime;
1805 snd_pcm_uframes_t xfer = 0;
1806 snd_pcm_uframes_t offset = 0;
1812 snd_pcm_stream_lock_irq(substream);
1813 switch (runtime->status->state) {
1814 case SNDRV_PCM_STATE_PREPARED:
1815 if (size >= runtime->start_threshold) {
1816 err = snd_pcm_start(substream);
1821 case SNDRV_PCM_STATE_DRAINING:
1822 case SNDRV_PCM_STATE_RUNNING:
1823 case SNDRV_PCM_STATE_PAUSED:
1825 case SNDRV_PCM_STATE_XRUN:
1828 case SNDRV_PCM_STATE_SUSPENDED:
1837 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1838 snd_pcm_uframes_t avail;
1839 snd_pcm_uframes_t cont;
1840 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1841 snd_pcm_update_hw_ptr(substream);
1842 avail = snd_pcm_capture_avail(runtime);
1844 if (runtime->status->state ==
1845 SNDRV_PCM_STATE_DRAINING) {
1846 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1853 err = wait_for_avail_min(substream, &avail);
1857 continue; /* draining */
1859 frames = size > avail ? avail : size;
1860 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1863 if (snd_BUG_ON(!frames)) {
1864 snd_pcm_stream_unlock_irq(substream);
1867 appl_ptr = runtime->control->appl_ptr;
1868 appl_ofs = appl_ptr % runtime->buffer_size;
1869 snd_pcm_stream_unlock_irq(substream);
1870 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1872 snd_pcm_stream_lock_irq(substream);
1873 switch (runtime->status->state) {
1874 case SNDRV_PCM_STATE_XRUN:
1877 case SNDRV_PCM_STATE_SUSPENDED:
1884 if (appl_ptr >= runtime->boundary)
1885 appl_ptr -= runtime->boundary;
1886 runtime->control->appl_ptr = appl_ptr;
1887 if (substream->ops->ack)
1888 substream->ops->ack(substream);
1895 snd_pcm_stream_unlock_irq(substream);
1897 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1900 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1902 struct snd_pcm_runtime *runtime;
1906 err = pcm_sanity_check(substream);
1909 runtime = substream->runtime;
1910 nonblock = !!(substream->f_flags & O_NONBLOCK);
1911 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1913 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1916 EXPORT_SYMBOL(snd_pcm_lib_read);
1918 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1920 unsigned long data, unsigned int off,
1921 snd_pcm_uframes_t frames)
1923 struct snd_pcm_runtime *runtime = substream->runtime;
1925 void __user **bufs = (void __user **)data;
1926 int channels = runtime->channels;
1928 if (substream->ops->copy) {
1929 for (c = 0; c < channels; ++c, ++bufs) {
1933 buf = *bufs + samples_to_bytes(runtime, off);
1934 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1938 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1939 for (c = 0; c < channels; ++c, ++bufs) {
1945 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1946 buf = *bufs + samples_to_bytes(runtime, off);
1947 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1954 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1956 snd_pcm_uframes_t frames)
1958 struct snd_pcm_runtime *runtime;
1962 err = pcm_sanity_check(substream);
1965 runtime = substream->runtime;
1966 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1969 nonblock = !!(substream->f_flags & O_NONBLOCK);
1970 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1972 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
1975 EXPORT_SYMBOL(snd_pcm_lib_readv);