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 <sound/driver.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
42 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
44 struct snd_pcm_runtime *runtime = substream->runtime;
45 snd_pcm_uframes_t frames, ofs, transfer;
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
59 if (runtime->silence_filled >= runtime->buffer_size)
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
82 runtime->silence_start = new_hw_ptr;
84 runtime->silence_start = ofs;
87 frames = runtime->buffer_size - runtime->silence_filled;
89 snd_assert(frames <= runtime->buffer_size, return);
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);
100 snd_assert(err >= 0, );
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);
112 snd_assert(err >= 0, );
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 pos = substream->ops->pointer(substream);
149 if (pos == SNDRV_PCM_POS_XRUN)
150 return pos; /* XRUN */
151 #ifdef CONFIG_SND_DEBUG
152 if (pos >= runtime->buffer_size) {
153 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);
156 pos -= pos % runtime->min_align;
160 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
161 struct snd_pcm_runtime *runtime)
163 snd_pcm_uframes_t avail;
165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
166 avail = snd_pcm_playback_avail(runtime);
168 avail = snd_pcm_capture_avail(runtime);
169 if (avail > runtime->avail_max)
170 runtime->avail_max = avail;
171 if (avail >= runtime->stop_threshold) {
172 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
173 snd_pcm_drain_done(substream);
178 if (avail >= runtime->control->avail_min)
179 wake_up(&runtime->sleep);
183 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
185 struct snd_pcm_runtime *runtime = substream->runtime;
186 snd_pcm_uframes_t pos;
187 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
188 snd_pcm_sframes_t delta;
190 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_MMAP)
191 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
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 snd_assert(!snd_interval_empty(i), return -EINVAL);
385 if (i->min < v->min) {
387 i->openmin = v->openmin;
389 } else if (i->min == v->min && !i->openmin && v->openmin) {
393 if (i->max > v->max) {
395 i->openmax = v->openmax;
397 } else if (i->max == v->max && !i->openmax && v->openmax) {
401 if (!i->integer && v->integer) {
414 } else if (!i->openmin && !i->openmax && i->min == i->max)
416 if (snd_interval_checkempty(i)) {
417 snd_interval_none(i);
423 EXPORT_SYMBOL(snd_interval_refine);
425 static int snd_interval_refine_first(struct snd_interval *i)
427 snd_assert(!snd_interval_empty(i), return -EINVAL);
428 if (snd_interval_single(i))
431 i->openmax = i->openmin;
437 static int snd_interval_refine_last(struct snd_interval *i)
439 snd_assert(!snd_interval_empty(i), return -EINVAL);
440 if (snd_interval_single(i))
443 i->openmin = i->openmax;
449 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
451 if (a->empty || b->empty) {
452 snd_interval_none(c);
456 c->min = mul(a->min, b->min);
457 c->openmin = (a->openmin || b->openmin);
458 c->max = mul(a->max, b->max);
459 c->openmax = (a->openmax || b->openmax);
460 c->integer = (a->integer && b->integer);
464 * snd_interval_div - refine the interval value with division
471 * Returns non-zero if the value is changed, zero if not changed.
473 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
476 if (a->empty || b->empty) {
477 snd_interval_none(c);
481 c->min = div32(a->min, b->max, &r);
482 c->openmin = (r || a->openmin || b->openmax);
484 c->max = div32(a->max, b->min, &r);
489 c->openmax = (a->openmax || b->openmin);
498 * snd_interval_muldivk - refine the interval value
501 * @k: divisor (as integer)
506 * Returns non-zero if the value is changed, zero if not changed.
508 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
509 unsigned int k, struct snd_interval *c)
512 if (a->empty || b->empty) {
513 snd_interval_none(c);
517 c->min = muldiv32(a->min, b->min, k, &r);
518 c->openmin = (r || a->openmin || b->openmin);
519 c->max = muldiv32(a->max, b->max, k, &r);
524 c->openmax = (a->openmax || b->openmax);
529 * snd_interval_mulkdiv - refine the interval value
531 * @k: dividend 2 (as integer)
537 * Returns non-zero if the value is changed, zero if not changed.
539 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
540 const struct snd_interval *b, struct snd_interval *c)
543 if (a->empty || b->empty) {
544 snd_interval_none(c);
548 c->min = muldiv32(a->min, k, b->max, &r);
549 c->openmin = (r || a->openmin || b->openmax);
551 c->max = muldiv32(a->max, k, b->min, &r);
556 c->openmax = (a->openmax || b->openmin);
568 * snd_interval_ratnum - refine the interval value
569 * @i: interval to refine
570 * @rats_count: number of ratnum_t
571 * @rats: ratnum_t array
572 * @nump: pointer to store the resultant numerator
573 * @denp: pointer to store the resultant denominator
575 * Returns non-zero if the value is changed, zero if not changed.
577 int snd_interval_ratnum(struct snd_interval *i,
578 unsigned int rats_count, struct snd_ratnum *rats,
579 unsigned int *nump, unsigned int *denp)
581 unsigned int best_num, best_diff, best_den;
583 struct snd_interval t;
586 best_num = best_den = best_diff = 0;
587 for (k = 0; k < rats_count; ++k) {
588 unsigned int num = rats[k].num;
590 unsigned int q = i->min;
594 den = div_down(num, q);
595 if (den < rats[k].den_min)
597 if (den > rats[k].den_max)
598 den = rats[k].den_max;
601 r = (den - rats[k].den_min) % rats[k].den_step;
605 diff = num - q * den;
607 diff * best_den < best_diff * den) {
617 t.min = div_down(best_num, best_den);
618 t.openmin = !!(best_num % best_den);
620 best_num = best_den = best_diff = 0;
621 for (k = 0; k < rats_count; ++k) {
622 unsigned int num = rats[k].num;
624 unsigned int q = i->max;
630 den = div_up(num, q);
631 if (den > rats[k].den_max)
633 if (den < rats[k].den_min)
634 den = rats[k].den_min;
637 r = (den - rats[k].den_min) % rats[k].den_step;
639 den += rats[k].den_step - r;
641 diff = q * den - num;
643 diff * best_den < best_diff * den) {
653 t.max = div_up(best_num, best_den);
654 t.openmax = !!(best_num % best_den);
656 err = snd_interval_refine(i, &t);
660 if (snd_interval_single(i)) {
669 EXPORT_SYMBOL(snd_interval_ratnum);
672 * snd_interval_ratden - refine the interval value
673 * @i: interval to refine
674 * @rats_count: number of struct ratden
675 * @rats: struct ratden array
676 * @nump: pointer to store the resultant numerator
677 * @denp: pointer to store the resultant denominator
679 * Returns non-zero if the value is changed, zero if not changed.
681 static int snd_interval_ratden(struct snd_interval *i,
682 unsigned int rats_count, struct snd_ratden *rats,
683 unsigned int *nump, unsigned int *denp)
685 unsigned int best_num, best_diff, best_den;
687 struct snd_interval t;
690 best_num = best_den = best_diff = 0;
691 for (k = 0; k < rats_count; ++k) {
693 unsigned int den = rats[k].den;
694 unsigned int q = i->min;
697 if (num > rats[k].num_max)
699 if (num < rats[k].num_min)
700 num = rats[k].num_max;
703 r = (num - rats[k].num_min) % rats[k].num_step;
705 num += rats[k].num_step - r;
707 diff = num - q * den;
709 diff * best_den < best_diff * den) {
719 t.min = div_down(best_num, best_den);
720 t.openmin = !!(best_num % best_den);
722 best_num = best_den = best_diff = 0;
723 for (k = 0; k < rats_count; ++k) {
725 unsigned int den = rats[k].den;
726 unsigned int q = i->max;
729 if (num < rats[k].num_min)
731 if (num > rats[k].num_max)
732 num = rats[k].num_max;
735 r = (num - rats[k].num_min) % rats[k].num_step;
739 diff = q * den - num;
741 diff * best_den < best_diff * den) {
751 t.max = div_up(best_num, best_den);
752 t.openmax = !!(best_num % best_den);
754 err = snd_interval_refine(i, &t);
758 if (snd_interval_single(i)) {
768 * snd_interval_list - refine the interval value from the list
769 * @i: the interval value to refine
770 * @count: the number of elements in the list
771 * @list: the value list
772 * @mask: the bit-mask to evaluate
774 * Refines the interval value from the list.
775 * When mask is non-zero, only the elements corresponding to bit 1 are
778 * Returns non-zero if the value is changed, zero if not changed.
780 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
789 for (k = 0; k < count; k++) {
790 if (mask && !(mask & (1 << k)))
792 if (i->min == list[k] && !i->openmin)
794 if (i->min < list[k]) {
804 for (k = count; k-- > 0;) {
805 if (mask && !(mask & (1 << k)))
807 if (i->max == list[k] && !i->openmax)
809 if (i->max > list[k]) {
819 if (snd_interval_checkempty(i)) {
826 EXPORT_SYMBOL(snd_interval_list);
828 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
832 n = (i->min - min) % step;
833 if (n != 0 || i->openmin) {
837 n = (i->max - min) % step;
838 if (n != 0 || i->openmax) {
842 if (snd_interval_checkempty(i)) {
849 /* Info constraints helpers */
852 * snd_pcm_hw_rule_add - add the hw-constraint rule
853 * @runtime: the pcm runtime instance
854 * @cond: condition bits
855 * @var: the variable to evaluate
856 * @func: the evaluation function
857 * @private: the private data pointer passed to function
858 * @dep: the dependent variables
860 * Returns zero if successful, or a negative error code on failure.
862 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
864 snd_pcm_hw_rule_func_t func, void *private,
867 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
868 struct snd_pcm_hw_rule *c;
872 if (constrs->rules_num >= constrs->rules_all) {
873 struct snd_pcm_hw_rule *new;
874 unsigned int new_rules = constrs->rules_all + 16;
875 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
878 if (constrs->rules) {
879 memcpy(new, constrs->rules,
880 constrs->rules_num * sizeof(*c));
881 kfree(constrs->rules);
883 constrs->rules = new;
884 constrs->rules_all = new_rules;
886 c = &constrs->rules[constrs->rules_num];
890 c->private = private;
893 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
897 dep = va_arg(args, int);
899 constrs->rules_num++;
904 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
907 * snd_pcm_hw_constraint_mask
908 * @runtime: PCM runtime instance
909 * @var: hw_params variable to apply the mask
910 * @mask: the bitmap mask
912 * Apply the constraint of the given bitmap mask to a mask parameter.
914 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
917 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
918 struct snd_mask *maskp = constrs_mask(constrs, var);
919 *maskp->bits &= mask;
920 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
921 if (*maskp->bits == 0)
927 * snd_pcm_hw_constraint_mask64
928 * @runtime: PCM runtime instance
929 * @var: hw_params variable to apply the mask
930 * @mask: the 64bit bitmap mask
932 * Apply the constraint of the given bitmap mask to a mask parameter.
934 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
937 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
938 struct snd_mask *maskp = constrs_mask(constrs, var);
939 maskp->bits[0] &= (u_int32_t)mask;
940 maskp->bits[1] &= (u_int32_t)(mask >> 32);
941 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
942 if (! maskp->bits[0] && ! maskp->bits[1])
948 * snd_pcm_hw_constraint_integer
949 * @runtime: PCM runtime instance
950 * @var: hw_params variable to apply the integer constraint
952 * Apply the constraint of integer to an interval parameter.
954 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
956 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
957 return snd_interval_setinteger(constrs_interval(constrs, var));
960 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
963 * snd_pcm_hw_constraint_minmax
964 * @runtime: PCM runtime instance
965 * @var: hw_params variable to apply the range
966 * @min: the minimal value
967 * @max: the maximal value
969 * Apply the min/max range constraint to an interval parameter.
971 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
972 unsigned int min, unsigned int max)
974 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
975 struct snd_interval t;
978 t.openmin = t.openmax = 0;
980 return snd_interval_refine(constrs_interval(constrs, var), &t);
983 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
985 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
986 struct snd_pcm_hw_rule *rule)
988 struct snd_pcm_hw_constraint_list *list = rule->private;
989 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
994 * snd_pcm_hw_constraint_list
995 * @runtime: PCM runtime instance
996 * @cond: condition bits
997 * @var: hw_params variable to apply the list constraint
1000 * Apply the list of constraints to an interval parameter.
1002 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1004 snd_pcm_hw_param_t var,
1005 struct snd_pcm_hw_constraint_list *l)
1007 return snd_pcm_hw_rule_add(runtime, cond, var,
1008 snd_pcm_hw_rule_list, l,
1012 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1014 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1015 struct snd_pcm_hw_rule *rule)
1017 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1018 unsigned int num = 0, den = 0;
1020 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1021 r->nrats, r->rats, &num, &den);
1022 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1023 params->rate_num = num;
1024 params->rate_den = den;
1030 * snd_pcm_hw_constraint_ratnums
1031 * @runtime: PCM runtime instance
1032 * @cond: condition bits
1033 * @var: hw_params variable to apply the ratnums constraint
1034 * @r: struct snd_ratnums constriants
1036 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1038 snd_pcm_hw_param_t var,
1039 struct snd_pcm_hw_constraint_ratnums *r)
1041 return snd_pcm_hw_rule_add(runtime, cond, var,
1042 snd_pcm_hw_rule_ratnums, r,
1046 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1048 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1049 struct snd_pcm_hw_rule *rule)
1051 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1052 unsigned int num = 0, den = 0;
1053 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1054 r->nrats, r->rats, &num, &den);
1055 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1056 params->rate_num = num;
1057 params->rate_den = den;
1063 * snd_pcm_hw_constraint_ratdens
1064 * @runtime: PCM runtime instance
1065 * @cond: condition bits
1066 * @var: hw_params variable to apply the ratdens constraint
1067 * @r: struct snd_ratdens constriants
1069 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1071 snd_pcm_hw_param_t var,
1072 struct snd_pcm_hw_constraint_ratdens *r)
1074 return snd_pcm_hw_rule_add(runtime, cond, var,
1075 snd_pcm_hw_rule_ratdens, r,
1079 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1081 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1082 struct snd_pcm_hw_rule *rule)
1084 unsigned int l = (unsigned long) rule->private;
1085 int width = l & 0xffff;
1086 unsigned int msbits = l >> 16;
1087 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1088 if (snd_interval_single(i) && snd_interval_value(i) == width)
1089 params->msbits = msbits;
1094 * snd_pcm_hw_constraint_msbits
1095 * @runtime: PCM runtime instance
1096 * @cond: condition bits
1097 * @width: sample bits width
1098 * @msbits: msbits width
1100 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1103 unsigned int msbits)
1105 unsigned long l = (msbits << 16) | width;
1106 return snd_pcm_hw_rule_add(runtime, cond, -1,
1107 snd_pcm_hw_rule_msbits,
1109 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1112 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1114 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1115 struct snd_pcm_hw_rule *rule)
1117 unsigned long step = (unsigned long) rule->private;
1118 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1122 * snd_pcm_hw_constraint_step
1123 * @runtime: PCM runtime instance
1124 * @cond: condition bits
1125 * @var: hw_params variable to apply the step constraint
1128 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1130 snd_pcm_hw_param_t var,
1133 return snd_pcm_hw_rule_add(runtime, cond, var,
1134 snd_pcm_hw_rule_step, (void *) step,
1138 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1140 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1142 static unsigned int pow2_sizes[] = {
1143 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1144 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1145 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1146 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1148 return snd_interval_list(hw_param_interval(params, rule->var),
1149 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1153 * snd_pcm_hw_constraint_pow2
1154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the power-of-2 constraint
1158 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1160 snd_pcm_hw_param_t var)
1162 return snd_pcm_hw_rule_add(runtime, cond, var,
1163 snd_pcm_hw_rule_pow2, NULL,
1167 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1169 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1170 snd_pcm_hw_param_t var)
1172 if (hw_is_mask(var)) {
1173 snd_mask_any(hw_param_mask(params, var));
1174 params->cmask |= 1 << var;
1175 params->rmask |= 1 << var;
1178 if (hw_is_interval(var)) {
1179 snd_interval_any(hw_param_interval(params, var));
1180 params->cmask |= 1 << var;
1181 params->rmask |= 1 << var;
1187 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1190 memset(params, 0, sizeof(*params));
1191 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1192 _snd_pcm_hw_param_any(params, k);
1193 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1194 _snd_pcm_hw_param_any(params, k);
1198 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1201 * snd_pcm_hw_param_value
1202 * @params: the hw_params instance
1203 * @var: parameter to retrieve
1204 * @dir: pointer to the direction (-1,0,1) or NULL
1206 * Return the value for field PAR if it's fixed in configuration space
1207 * defined by PARAMS. Return -EINVAL otherwise
1209 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1210 snd_pcm_hw_param_t var, int *dir)
1212 if (hw_is_mask(var)) {
1213 const struct snd_mask *mask = hw_param_mask_c(params, var);
1214 if (!snd_mask_single(mask))
1218 return snd_mask_value(mask);
1220 if (hw_is_interval(var)) {
1221 const struct snd_interval *i = hw_param_interval_c(params, var);
1222 if (!snd_interval_single(i))
1226 return snd_interval_value(i);
1231 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1233 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1234 snd_pcm_hw_param_t var)
1236 if (hw_is_mask(var)) {
1237 snd_mask_none(hw_param_mask(params, var));
1238 params->cmask |= 1 << var;
1239 params->rmask |= 1 << var;
1240 } else if (hw_is_interval(var)) {
1241 snd_interval_none(hw_param_interval(params, var));
1242 params->cmask |= 1 << var;
1243 params->rmask |= 1 << var;
1249 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1251 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1252 snd_pcm_hw_param_t var)
1255 if (hw_is_mask(var))
1256 changed = snd_mask_refine_first(hw_param_mask(params, var));
1257 else if (hw_is_interval(var))
1258 changed = snd_interval_refine_first(hw_param_interval(params, var));
1262 params->cmask |= 1 << var;
1263 params->rmask |= 1 << var;
1270 * snd_pcm_hw_param_first
1271 * @pcm: PCM instance
1272 * @params: the hw_params instance
1273 * @var: parameter to retrieve
1274 * @dir: pointer to the direction (-1,0,1) or NULL
1276 * Inside configuration space defined by PARAMS remove from PAR all
1277 * values > minimum. Reduce configuration space accordingly.
1278 * Return the minimum.
1280 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1281 struct snd_pcm_hw_params *params,
1282 snd_pcm_hw_param_t var, int *dir)
1284 int changed = _snd_pcm_hw_param_first(params, var);
1287 if (params->rmask) {
1288 int err = snd_pcm_hw_refine(pcm, params);
1289 snd_assert(err >= 0, return err);
1291 return snd_pcm_hw_param_value(params, var, dir);
1294 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1296 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1297 snd_pcm_hw_param_t var)
1300 if (hw_is_mask(var))
1301 changed = snd_mask_refine_last(hw_param_mask(params, var));
1302 else if (hw_is_interval(var))
1303 changed = snd_interval_refine_last(hw_param_interval(params, var));
1307 params->cmask |= 1 << var;
1308 params->rmask |= 1 << var;
1315 * snd_pcm_hw_param_last
1316 * @pcm: PCM instance
1317 * @params: the hw_params instance
1318 * @var: parameter to retrieve
1319 * @dir: pointer to the direction (-1,0,1) or NULL
1321 * Inside configuration space defined by PARAMS remove from PAR all
1322 * values < maximum. Reduce configuration space accordingly.
1323 * Return the maximum.
1325 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1326 struct snd_pcm_hw_params *params,
1327 snd_pcm_hw_param_t var, int *dir)
1329 int changed = _snd_pcm_hw_param_last(params, var);
1332 if (params->rmask) {
1333 int err = snd_pcm_hw_refine(pcm, params);
1334 snd_assert(err >= 0, return err);
1336 return snd_pcm_hw_param_value(params, var, dir);
1339 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1342 * snd_pcm_hw_param_choose
1343 * @pcm: PCM instance
1344 * @params: the hw_params instance
1346 * Choose one configuration from configuration space defined by PARAMS
1347 * The configuration chosen is that obtained fixing in this order:
1348 * first access, first format, first subformat, min channels,
1349 * min rate, min period time, max buffer size, min tick time
1351 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1352 struct snd_pcm_hw_params *params)
1354 static int vars[] = {
1355 SNDRV_PCM_HW_PARAM_ACCESS,
1356 SNDRV_PCM_HW_PARAM_FORMAT,
1357 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1358 SNDRV_PCM_HW_PARAM_CHANNELS,
1359 SNDRV_PCM_HW_PARAM_RATE,
1360 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1361 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1362 SNDRV_PCM_HW_PARAM_TICK_TIME,
1367 for (v = vars; *v != -1; v++) {
1368 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1369 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1371 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1372 snd_assert(err >= 0, return err);
1377 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1380 struct snd_pcm_runtime *runtime = substream->runtime;
1381 unsigned long flags;
1382 snd_pcm_stream_lock_irqsave(substream, flags);
1383 if (snd_pcm_running(substream) &&
1384 snd_pcm_update_hw_ptr(substream) >= 0)
1385 runtime->status->hw_ptr %= runtime->buffer_size;
1387 runtime->status->hw_ptr = 0;
1388 snd_pcm_stream_unlock_irqrestore(substream, flags);
1392 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1395 struct snd_pcm_channel_info *info = arg;
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1398 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1402 width = snd_pcm_format_physical_width(runtime->format);
1406 switch (runtime->access) {
1407 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1408 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1409 info->first = info->channel * width;
1410 info->step = runtime->channels * width;
1412 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1413 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1415 size_t size = runtime->dma_bytes / runtime->channels;
1416 info->first = info->channel * size * 8;
1428 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1429 * @substream: the pcm substream instance
1430 * @cmd: ioctl command
1431 * @arg: ioctl argument
1433 * Processes the generic ioctl commands for PCM.
1434 * Can be passed as the ioctl callback for PCM ops.
1436 * Returns zero if successful, or a negative error code on failure.
1438 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1439 unsigned int cmd, void *arg)
1442 case SNDRV_PCM_IOCTL1_INFO:
1444 case SNDRV_PCM_IOCTL1_RESET:
1445 return snd_pcm_lib_ioctl_reset(substream, arg);
1446 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1447 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1452 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1458 static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
1459 unsigned long ticks)
1461 struct snd_pcm_runtime *runtime = substream->runtime;
1463 del_timer(&runtime->tick_timer);
1465 ticks += (1000000 / HZ) - 1;
1466 ticks /= (1000000 / HZ);
1467 mod_timer(&runtime->tick_timer, jiffies + ticks);
1471 /* Temporary alias */
1472 void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
1474 snd_pcm_system_tick_set(substream, ticks);
1477 void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
1479 struct snd_pcm_runtime *runtime = substream->runtime;
1480 snd_pcm_uframes_t frames = ULONG_MAX;
1481 snd_pcm_uframes_t avail, dist;
1485 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1486 if (runtime->silence_size >= runtime->boundary) {
1488 } else if (runtime->silence_size > 0 &&
1489 runtime->silence_filled < runtime->buffer_size) {
1490 snd_pcm_sframes_t noise_dist;
1491 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
1492 if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold)
1493 frames = noise_dist - runtime->silence_threshold;
1495 avail = snd_pcm_playback_avail(runtime);
1497 avail = snd_pcm_capture_avail(runtime);
1499 if (avail < runtime->control->avail_min) {
1500 snd_pcm_sframes_t to_avail_min =
1501 runtime->control->avail_min - avail;
1502 if (to_avail_min > 0 &&
1503 frames > (snd_pcm_uframes_t)to_avail_min)
1504 frames = to_avail_min;
1506 if (avail < runtime->buffer_size) {
1507 snd_pcm_sframes_t to_buffer_size =
1508 runtime->buffer_size - avail;
1509 if (to_buffer_size > 0 &&
1510 frames > (snd_pcm_uframes_t)to_buffer_size)
1511 frames = to_buffer_size;
1513 if (frames == ULONG_MAX) {
1514 snd_pcm_tick_set(substream, 0);
1517 dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
1518 /* Distance to next interrupt */
1519 dist = runtime->period_size - dist % runtime->period_size;
1520 if (dist <= frames) {
1521 snd_pcm_tick_set(substream, 0);
1524 /* the base time is us */
1527 div64_32(&n, runtime->tick_time * runtime->rate, &r);
1528 ticks = n + (r > 0 ? 1 : 0);
1529 if (ticks < runtime->sleep_min)
1530 ticks = runtime->sleep_min;
1531 snd_pcm_tick_set(substream, (unsigned long) ticks);
1534 void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
1536 struct snd_pcm_runtime *runtime;
1537 unsigned long flags;
1539 snd_assert(substream != NULL, return);
1540 runtime = substream->runtime;
1541 snd_assert(runtime != NULL, return);
1543 snd_pcm_stream_lock_irqsave(substream, flags);
1544 if (!snd_pcm_running(substream) ||
1545 snd_pcm_update_hw_ptr(substream) < 0)
1547 if (runtime->sleep_min)
1548 snd_pcm_tick_prepare(substream);
1550 snd_pcm_stream_unlock_irqrestore(substream, flags);
1554 * snd_pcm_period_elapsed - update the pcm status for the next period
1555 * @substream: the pcm substream instance
1557 * This function is called from the interrupt handler when the
1558 * PCM has processed the period size. It will update the current
1559 * pointer, set up the tick, wake up sleepers, etc.
1561 * Even if more than one periods have elapsed since the last call, you
1562 * have to call this only once.
1564 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1566 struct snd_pcm_runtime *runtime;
1567 unsigned long flags;
1569 snd_assert(substream != NULL, return);
1570 runtime = substream->runtime;
1571 snd_assert(runtime != NULL, return);
1573 if (runtime->transfer_ack_begin)
1574 runtime->transfer_ack_begin(substream);
1576 snd_pcm_stream_lock_irqsave(substream, flags);
1577 if (!snd_pcm_running(substream) ||
1578 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1581 if (substream->timer_running)
1582 snd_timer_interrupt(substream->timer, 1);
1583 if (runtime->sleep_min)
1584 snd_pcm_tick_prepare(substream);
1586 snd_pcm_stream_unlock_irqrestore(substream, flags);
1587 if (runtime->transfer_ack_end)
1588 runtime->transfer_ack_end(substream);
1589 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1592 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1594 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1596 unsigned long data, unsigned int off,
1597 snd_pcm_uframes_t frames)
1599 struct snd_pcm_runtime *runtime = substream->runtime;
1601 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1602 if (substream->ops->copy) {
1603 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1606 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1607 snd_assert(runtime->dma_area, return -EFAULT);
1608 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1614 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1615 unsigned long data, unsigned int off,
1616 snd_pcm_uframes_t size);
1618 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1620 snd_pcm_uframes_t size,
1622 transfer_f transfer)
1624 struct snd_pcm_runtime *runtime = substream->runtime;
1625 snd_pcm_uframes_t xfer = 0;
1626 snd_pcm_uframes_t offset = 0;
1631 if (size > runtime->xfer_align)
1632 size -= size % runtime->xfer_align;
1634 snd_pcm_stream_lock_irq(substream);
1635 switch (runtime->status->state) {
1636 case SNDRV_PCM_STATE_PREPARED:
1637 case SNDRV_PCM_STATE_RUNNING:
1638 case SNDRV_PCM_STATE_PAUSED:
1640 case SNDRV_PCM_STATE_XRUN:
1643 case SNDRV_PCM_STATE_SUSPENDED:
1652 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1653 snd_pcm_uframes_t avail;
1654 snd_pcm_uframes_t cont;
1655 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1656 snd_pcm_update_hw_ptr(substream);
1657 avail = snd_pcm_playback_avail(runtime);
1659 (snd_pcm_running(substream) &&
1660 ((avail < runtime->control->avail_min && size > avail) ||
1661 (size >= runtime->xfer_align &&
1662 avail < runtime->xfer_align)))) {
1664 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
1672 init_waitqueue_entry(&wait, current);
1673 add_wait_queue(&runtime->sleep, &wait);
1675 if (signal_pending(current)) {
1679 set_current_state(TASK_INTERRUPTIBLE);
1680 snd_pcm_stream_unlock_irq(substream);
1681 tout = schedule_timeout(10 * HZ);
1682 snd_pcm_stream_lock_irq(substream);
1684 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
1685 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
1686 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1690 switch (runtime->status->state) {
1691 case SNDRV_PCM_STATE_XRUN:
1692 case SNDRV_PCM_STATE_DRAINING:
1695 case SNDRV_PCM_STATE_SUSPENDED:
1698 case SNDRV_PCM_STATE_SETUP:
1704 avail = snd_pcm_playback_avail(runtime);
1705 if (avail >= runtime->control->avail_min) {
1711 remove_wait_queue(&runtime->sleep, &wait);
1724 snd_printd("playback write error (DMA or IRQ trouble?)\n");
1734 if (avail > runtime->xfer_align)
1735 avail -= avail % runtime->xfer_align;
1736 frames = size > avail ? avail : size;
1737 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1740 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1741 appl_ptr = runtime->control->appl_ptr;
1742 appl_ofs = appl_ptr % runtime->buffer_size;
1743 snd_pcm_stream_unlock_irq(substream);
1744 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1746 snd_pcm_stream_lock_irq(substream);
1747 switch (runtime->status->state) {
1748 case SNDRV_PCM_STATE_XRUN:
1751 case SNDRV_PCM_STATE_SUSPENDED:
1758 if (appl_ptr >= runtime->boundary)
1759 appl_ptr -= runtime->boundary;
1760 runtime->control->appl_ptr = appl_ptr;
1761 if (substream->ops->ack)
1762 substream->ops->ack(substream);
1767 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1768 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1769 err = snd_pcm_start(substream);
1773 if (runtime->sleep_min &&
1774 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1775 snd_pcm_tick_prepare(substream);
1778 snd_pcm_stream_unlock_irq(substream);
1780 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1783 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1785 struct snd_pcm_runtime *runtime;
1788 snd_assert(substream != NULL, return -ENXIO);
1789 runtime = substream->runtime;
1790 snd_assert(runtime != NULL, return -ENXIO);
1791 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1792 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1795 nonblock = !!(substream->f_flags & O_NONBLOCK);
1797 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1798 runtime->channels > 1)
1800 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1801 snd_pcm_lib_write_transfer);
1804 EXPORT_SYMBOL(snd_pcm_lib_write);
1806 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1808 unsigned long data, unsigned int off,
1809 snd_pcm_uframes_t frames)
1811 struct snd_pcm_runtime *runtime = substream->runtime;
1813 void __user **bufs = (void __user **)data;
1814 int channels = runtime->channels;
1816 if (substream->ops->copy) {
1817 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1818 for (c = 0; c < channels; ++c, ++bufs) {
1819 if (*bufs == NULL) {
1820 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1823 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1824 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1829 /* default transfer behaviour */
1830 size_t dma_csize = runtime->dma_bytes / channels;
1831 snd_assert(runtime->dma_area, return -EFAULT);
1832 for (c = 0; c < channels; ++c, ++bufs) {
1833 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1834 if (*bufs == NULL) {
1835 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1837 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1838 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1846 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1848 snd_pcm_uframes_t frames)
1850 struct snd_pcm_runtime *runtime;
1853 snd_assert(substream != NULL, return -ENXIO);
1854 runtime = substream->runtime;
1855 snd_assert(runtime != NULL, return -ENXIO);
1856 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1857 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1860 nonblock = !!(substream->f_flags & O_NONBLOCK);
1862 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1864 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1865 nonblock, snd_pcm_lib_writev_transfer);
1868 EXPORT_SYMBOL(snd_pcm_lib_writev);
1870 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1872 unsigned long data, unsigned int off,
1873 snd_pcm_uframes_t frames)
1875 struct snd_pcm_runtime *runtime = substream->runtime;
1877 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1878 if (substream->ops->copy) {
1879 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1882 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1883 snd_assert(runtime->dma_area, return -EFAULT);
1884 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1890 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1892 snd_pcm_uframes_t size,
1894 transfer_f transfer)
1896 struct snd_pcm_runtime *runtime = substream->runtime;
1897 snd_pcm_uframes_t xfer = 0;
1898 snd_pcm_uframes_t offset = 0;
1903 if (size > runtime->xfer_align)
1904 size -= size % runtime->xfer_align;
1906 snd_pcm_stream_lock_irq(substream);
1907 switch (runtime->status->state) {
1908 case SNDRV_PCM_STATE_PREPARED:
1909 if (size >= runtime->start_threshold) {
1910 err = snd_pcm_start(substream);
1915 case SNDRV_PCM_STATE_DRAINING:
1916 case SNDRV_PCM_STATE_RUNNING:
1917 case SNDRV_PCM_STATE_PAUSED:
1919 case SNDRV_PCM_STATE_XRUN:
1922 case SNDRV_PCM_STATE_SUSPENDED:
1931 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1932 snd_pcm_uframes_t avail;
1933 snd_pcm_uframes_t cont;
1934 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1935 snd_pcm_update_hw_ptr(substream);
1937 avail = snd_pcm_capture_avail(runtime);
1938 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1939 if (avail < runtime->xfer_align) {
1943 } else if ((avail < runtime->control->avail_min && size > avail) ||
1944 (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
1946 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
1954 init_waitqueue_entry(&wait, current);
1955 add_wait_queue(&runtime->sleep, &wait);
1957 if (signal_pending(current)) {
1961 set_current_state(TASK_INTERRUPTIBLE);
1962 snd_pcm_stream_unlock_irq(substream);
1963 tout = schedule_timeout(10 * HZ);
1964 snd_pcm_stream_lock_irq(substream);
1966 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
1967 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
1968 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1972 switch (runtime->status->state) {
1973 case SNDRV_PCM_STATE_XRUN:
1976 case SNDRV_PCM_STATE_SUSPENDED:
1979 case SNDRV_PCM_STATE_DRAINING:
1981 case SNDRV_PCM_STATE_SETUP:
1987 avail = snd_pcm_capture_avail(runtime);
1988 if (avail >= runtime->control->avail_min) {
1994 remove_wait_queue(&runtime->sleep, &wait);
2007 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2017 if (avail > runtime->xfer_align)
2018 avail -= avail % runtime->xfer_align;
2019 frames = size > avail ? avail : size;
2020 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2023 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2024 appl_ptr = runtime->control->appl_ptr;
2025 appl_ofs = appl_ptr % runtime->buffer_size;
2026 snd_pcm_stream_unlock_irq(substream);
2027 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2029 snd_pcm_stream_lock_irq(substream);
2030 switch (runtime->status->state) {
2031 case SNDRV_PCM_STATE_XRUN:
2034 case SNDRV_PCM_STATE_SUSPENDED:
2041 if (appl_ptr >= runtime->boundary)
2042 appl_ptr -= runtime->boundary;
2043 runtime->control->appl_ptr = appl_ptr;
2044 if (substream->ops->ack)
2045 substream->ops->ack(substream);
2050 if (runtime->sleep_min &&
2051 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2052 snd_pcm_tick_prepare(substream);
2055 snd_pcm_stream_unlock_irq(substream);
2057 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2060 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2062 struct snd_pcm_runtime *runtime;
2065 snd_assert(substream != NULL, return -ENXIO);
2066 runtime = substream->runtime;
2067 snd_assert(runtime != NULL, return -ENXIO);
2068 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2069 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2072 nonblock = !!(substream->f_flags & O_NONBLOCK);
2073 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2075 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2078 EXPORT_SYMBOL(snd_pcm_lib_read);
2080 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2082 unsigned long data, unsigned int off,
2083 snd_pcm_uframes_t frames)
2085 struct snd_pcm_runtime *runtime = substream->runtime;
2087 void __user **bufs = (void __user **)data;
2088 int channels = runtime->channels;
2090 if (substream->ops->copy) {
2091 for (c = 0; c < channels; ++c, ++bufs) {
2095 buf = *bufs + samples_to_bytes(runtime, off);
2096 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2100 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2101 snd_assert(runtime->dma_area, return -EFAULT);
2102 for (c = 0; c < channels; ++c, ++bufs) {
2108 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2109 buf = *bufs + samples_to_bytes(runtime, off);
2110 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2117 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2119 snd_pcm_uframes_t frames)
2121 struct snd_pcm_runtime *runtime;
2124 snd_assert(substream != NULL, return -ENXIO);
2125 runtime = substream->runtime;
2126 snd_assert(runtime != NULL, return -ENXIO);
2127 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2128 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2131 nonblock = !!(substream->f_flags & O_NONBLOCK);
2132 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2134 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2137 EXPORT_SYMBOL(snd_pcm_lib_readv);