2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 snd_assert(runtime->silence_filled <= runtime->buffer_size, return);
62 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
63 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
65 frames = runtime->silence_threshold - noise_dist;
66 if (frames > runtime->silence_size)
67 frames = runtime->silence_size;
69 if (new_hw_ptr == ULONG_MAX) { /* initialization */
70 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
71 runtime->silence_filled = avail > 0 ? avail : 0;
72 runtime->silence_start = (runtime->status->hw_ptr +
73 runtime->silence_filled) %
76 ofs = runtime->status->hw_ptr;
77 frames = new_hw_ptr - ofs;
78 if ((snd_pcm_sframes_t)frames < 0)
79 frames += runtime->boundary;
80 runtime->silence_filled -= frames;
81 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
82 runtime->silence_filled = 0;
83 runtime->silence_start = (ofs + frames) - runtime->buffer_size;
85 runtime->silence_start = ofs - runtime->silence_filled;
87 if ((snd_pcm_sframes_t)runtime->silence_start < 0)
88 runtime->silence_start += runtime->boundary;
90 frames = runtime->buffer_size - runtime->silence_filled;
92 snd_assert(frames <= runtime->buffer_size, return);
95 ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
97 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
98 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
99 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
100 if (substream->ops->silence) {
102 err = substream->ops->silence(substream, -1, ofs, transfer);
103 snd_assert(err >= 0, );
105 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
106 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
110 unsigned int channels = runtime->channels;
111 if (substream->ops->silence) {
112 for (c = 0; c < channels; ++c) {
114 err = substream->ops->silence(substream, c, ofs, transfer);
115 snd_assert(err >= 0, );
118 size_t dma_csize = runtime->dma_bytes / channels;
119 for (c = 0; c < channels; ++c) {
120 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
121 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
125 runtime->silence_filled += transfer;
131 static void xrun(struct snd_pcm_substream *substream)
133 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
134 #ifdef CONFIG_SND_DEBUG
135 if (substream->pstr->xrun_debug) {
136 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
137 substream->pcm->card->number,
138 substream->pcm->device,
139 substream->stream ? 'c' : 'p');
140 if (substream->pstr->xrun_debug > 1)
146 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
147 struct snd_pcm_runtime *runtime)
149 snd_pcm_uframes_t pos;
151 pos = substream->ops->pointer(substream);
152 if (pos == SNDRV_PCM_POS_XRUN)
153 return pos; /* XRUN */
154 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
155 getnstimeofday((struct timespec *)&runtime->status->tstamp);
156 #ifdef CONFIG_SND_DEBUG
157 if (pos >= runtime->buffer_size) {
158 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);
161 pos -= pos % runtime->min_align;
165 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
166 struct snd_pcm_runtime *runtime)
168 snd_pcm_uframes_t avail;
170 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
171 avail = snd_pcm_playback_avail(runtime);
173 avail = snd_pcm_capture_avail(runtime);
174 if (avail > runtime->avail_max)
175 runtime->avail_max = avail;
176 if (avail >= runtime->stop_threshold) {
177 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
178 snd_pcm_drain_done(substream);
183 if (avail >= runtime->control->avail_min)
184 wake_up(&runtime->sleep);
188 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
190 struct snd_pcm_runtime *runtime = substream->runtime;
191 snd_pcm_uframes_t pos;
192 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
193 snd_pcm_sframes_t delta;
195 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
196 if (pos == SNDRV_PCM_POS_XRUN) {
200 if (runtime->period_size == runtime->buffer_size)
202 new_hw_ptr = runtime->hw_ptr_base + pos;
203 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
205 delta = hw_ptr_interrupt - new_hw_ptr;
207 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
208 #ifdef CONFIG_SND_DEBUG
209 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
210 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);
211 if (substream->pstr->xrun_debug > 1)
218 runtime->hw_ptr_base += runtime->buffer_size;
219 if (runtime->hw_ptr_base == runtime->boundary)
220 runtime->hw_ptr_base = 0;
221 new_hw_ptr = runtime->hw_ptr_base + pos;
224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
225 runtime->silence_size > 0)
226 snd_pcm_playback_silence(substream, new_hw_ptr);
228 runtime->status->hw_ptr = new_hw_ptr;
229 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
231 return snd_pcm_update_hw_ptr_post(substream, runtime);
234 /* CAUTION: call it with irq disabled */
235 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
237 struct snd_pcm_runtime *runtime = substream->runtime;
238 snd_pcm_uframes_t pos;
239 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
240 snd_pcm_sframes_t delta;
242 old_hw_ptr = runtime->status->hw_ptr;
243 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
244 if (pos == SNDRV_PCM_POS_XRUN) {
248 new_hw_ptr = runtime->hw_ptr_base + pos;
250 delta = old_hw_ptr - new_hw_ptr;
252 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
253 #ifdef CONFIG_SND_DEBUG
254 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
255 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);
256 if (substream->pstr->xrun_debug > 1)
262 runtime->hw_ptr_base += runtime->buffer_size;
263 if (runtime->hw_ptr_base == runtime->boundary)
264 runtime->hw_ptr_base = 0;
265 new_hw_ptr = runtime->hw_ptr_base + pos;
267 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
268 runtime->silence_size > 0)
269 snd_pcm_playback_silence(substream, new_hw_ptr);
271 runtime->status->hw_ptr = new_hw_ptr;
273 return snd_pcm_update_hw_ptr_post(substream, runtime);
277 * snd_pcm_set_ops - set the PCM operators
278 * @pcm: the pcm instance
279 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
280 * @ops: the operator table
282 * Sets the given PCM operators to the pcm instance.
284 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
286 struct snd_pcm_str *stream = &pcm->streams[direction];
287 struct snd_pcm_substream *substream;
289 for (substream = stream->substream; substream != NULL; substream = substream->next)
290 substream->ops = ops;
295 * snd_pcm_sync - set the PCM sync id
296 * @substream: the pcm substream
298 * Sets the PCM sync identifier for the card.
300 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
302 struct snd_pcm_runtime *runtime = substream->runtime;
304 runtime->sync.id32[0] = substream->pcm->card->number;
305 runtime->sync.id32[1] = -1;
306 runtime->sync.id32[2] = -1;
307 runtime->sync.id32[3] = -1;
311 * Standard ioctl routine
314 /* Code taken from alsa-lib */
315 #define assert(a) snd_assert((a), return -EINVAL)
317 static inline unsigned int div32(unsigned int a, unsigned int b,
328 static inline unsigned int div_down(unsigned int a, unsigned int b)
335 static inline unsigned int div_up(unsigned int a, unsigned int b)
347 static inline unsigned int mul(unsigned int a, unsigned int b)
351 if (div_down(UINT_MAX, a) < b)
356 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
357 unsigned int c, unsigned int *r)
359 u_int64_t n = (u_int64_t) a * b;
373 static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
376 assert(!snd_interval_empty(i));
379 i->openmin = openmin;
381 } else if (i->min == min && !i->openmin && openmin) {
391 if (snd_interval_checkempty(i)) {
392 snd_interval_none(i);
398 static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
401 assert(!snd_interval_empty(i));
404 i->openmax = openmax;
406 } else if (i->max == max && !i->openmax && openmax) {
416 if (snd_interval_checkempty(i)) {
417 snd_interval_none(i);
424 * snd_interval_refine - refine the interval value of configurator
425 * @i: the interval value to refine
426 * @v: the interval value to refer to
428 * Refines the interval value with the reference value.
429 * The interval is changed to the range satisfying both intervals.
430 * The interval status (min, max, integer, etc.) are evaluated.
432 * Returns non-zero if the value is changed, zero if not changed.
434 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
437 assert(!snd_interval_empty(i));
438 if (i->min < v->min) {
440 i->openmin = v->openmin;
442 } else if (i->min == v->min && !i->openmin && v->openmin) {
446 if (i->max > v->max) {
448 i->openmax = v->openmax;
450 } else if (i->max == v->max && !i->openmax && v->openmax) {
454 if (!i->integer && v->integer) {
467 } else if (!i->openmin && !i->openmax && i->min == i->max)
469 if (snd_interval_checkempty(i)) {
470 snd_interval_none(i);
476 static int snd_interval_refine_first(struct snd_interval *i)
478 assert(!snd_interval_empty(i));
479 if (snd_interval_single(i))
482 i->openmax = i->openmin;
488 static int snd_interval_refine_last(struct snd_interval *i)
490 assert(!snd_interval_empty(i));
491 if (snd_interval_single(i))
494 i->openmin = i->openmax;
500 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
502 struct snd_interval t;
505 t.openmin = t.openmax = 0;
507 return snd_interval_refine(i, &t);
510 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
512 if (a->empty || b->empty) {
513 snd_interval_none(c);
517 c->min = mul(a->min, b->min);
518 c->openmin = (a->openmin || b->openmin);
519 c->max = mul(a->max, b->max);
520 c->openmax = (a->openmax || b->openmax);
521 c->integer = (a->integer && b->integer);
525 * snd_interval_div - refine the interval value with division
532 * Returns non-zero if the value is changed, zero if not changed.
534 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
537 if (a->empty || b->empty) {
538 snd_interval_none(c);
542 c->min = div32(a->min, b->max, &r);
543 c->openmin = (r || a->openmin || b->openmax);
545 c->max = div32(a->max, b->min, &r);
550 c->openmax = (a->openmax || b->openmin);
559 * snd_interval_muldivk - refine the interval value
562 * @k: divisor (as integer)
567 * Returns non-zero if the value is changed, zero if not changed.
569 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
570 unsigned int k, struct snd_interval *c)
573 if (a->empty || b->empty) {
574 snd_interval_none(c);
578 c->min = muldiv32(a->min, b->min, k, &r);
579 c->openmin = (r || a->openmin || b->openmin);
580 c->max = muldiv32(a->max, b->max, k, &r);
585 c->openmax = (a->openmax || b->openmax);
590 * snd_interval_mulkdiv - refine the interval value
592 * @k: dividend 2 (as integer)
598 * Returns non-zero if the value is changed, zero if not changed.
600 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
601 const struct snd_interval *b, struct snd_interval *c)
604 if (a->empty || b->empty) {
605 snd_interval_none(c);
609 c->min = muldiv32(a->min, k, b->max, &r);
610 c->openmin = (r || a->openmin || b->openmax);
612 c->max = muldiv32(a->max, k, b->min, &r);
617 c->openmax = (a->openmax || b->openmin);
630 * snd_interval_ratnum - refine the interval value
631 * @i: interval to refine
632 * @rats_count: number of ratnum_t
633 * @rats: ratnum_t array
634 * @nump: pointer to store the resultant numerator
635 * @denp: pointer to store the resultant denominator
637 * Returns non-zero if the value is changed, zero if not changed.
639 int snd_interval_ratnum(struct snd_interval *i,
640 unsigned int rats_count, struct snd_ratnum *rats,
641 unsigned int *nump, unsigned int *denp)
643 unsigned int best_num, best_diff, best_den;
645 struct snd_interval t;
648 best_num = best_den = best_diff = 0;
649 for (k = 0; k < rats_count; ++k) {
650 unsigned int num = rats[k].num;
652 unsigned int q = i->min;
656 den = div_down(num, q);
657 if (den < rats[k].den_min)
659 if (den > rats[k].den_max)
660 den = rats[k].den_max;
663 r = (den - rats[k].den_min) % rats[k].den_step;
667 diff = num - q * den;
669 diff * best_den < best_diff * den) {
679 t.min = div_down(best_num, best_den);
680 t.openmin = !!(best_num % best_den);
682 best_num = best_den = best_diff = 0;
683 for (k = 0; k < rats_count; ++k) {
684 unsigned int num = rats[k].num;
686 unsigned int q = i->max;
692 den = div_up(num, q);
693 if (den > rats[k].den_max)
695 if (den < rats[k].den_min)
696 den = rats[k].den_min;
699 r = (den - rats[k].den_min) % rats[k].den_step;
701 den += rats[k].den_step - r;
703 diff = q * den - num;
705 diff * best_den < best_diff * den) {
715 t.max = div_up(best_num, best_den);
716 t.openmax = !!(best_num % best_den);
718 err = snd_interval_refine(i, &t);
722 if (snd_interval_single(i)) {
732 * snd_interval_ratden - refine the interval value
733 * @i: interval to refine
734 * @rats_count: number of struct ratden
735 * @rats: struct ratden array
736 * @nump: pointer to store the resultant numerator
737 * @denp: pointer to store the resultant denominator
739 * Returns non-zero if the value is changed, zero if not changed.
741 static int snd_interval_ratden(struct snd_interval *i,
742 unsigned int rats_count, struct snd_ratden *rats,
743 unsigned int *nump, unsigned int *denp)
745 unsigned int best_num, best_diff, best_den;
747 struct snd_interval t;
750 best_num = best_den = best_diff = 0;
751 for (k = 0; k < rats_count; ++k) {
753 unsigned int den = rats[k].den;
754 unsigned int q = i->min;
757 if (num > rats[k].num_max)
759 if (num < rats[k].num_min)
760 num = rats[k].num_max;
763 r = (num - rats[k].num_min) % rats[k].num_step;
765 num += rats[k].num_step - r;
767 diff = num - q * den;
769 diff * best_den < best_diff * den) {
779 t.min = div_down(best_num, best_den);
780 t.openmin = !!(best_num % best_den);
782 best_num = best_den = best_diff = 0;
783 for (k = 0; k < rats_count; ++k) {
785 unsigned int den = rats[k].den;
786 unsigned int q = i->max;
789 if (num < rats[k].num_min)
791 if (num > rats[k].num_max)
792 num = rats[k].num_max;
795 r = (num - rats[k].num_min) % rats[k].num_step;
799 diff = q * den - num;
801 diff * best_den < best_diff * den) {
811 t.max = div_up(best_num, best_den);
812 t.openmax = !!(best_num % best_den);
814 err = snd_interval_refine(i, &t);
818 if (snd_interval_single(i)) {
828 * snd_interval_list - refine the interval value from the list
829 * @i: the interval value to refine
830 * @count: the number of elements in the list
831 * @list: the value list
832 * @mask: the bit-mask to evaluate
834 * Refines the interval value from the list.
835 * When mask is non-zero, only the elements corresponding to bit 1 are
838 * Returns non-zero if the value is changed, zero if not changed.
840 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
844 for (k = 0; k < count; k++) {
845 if (mask && !(mask & (1 << k)))
847 if (i->min == list[k] && !i->openmin)
849 if (i->min < list[k]) {
859 for (k = count; k-- > 0;) {
860 if (mask && !(mask & (1 << k)))
862 if (i->max == list[k] && !i->openmax)
864 if (i->max > list[k]) {
874 if (snd_interval_checkempty(i)) {
881 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
885 n = (i->min - min) % step;
886 if (n != 0 || i->openmin) {
890 n = (i->max - min) % step;
891 if (n != 0 || i->openmax) {
895 if (snd_interval_checkempty(i)) {
902 /* Info constraints helpers */
905 * snd_pcm_hw_rule_add - add the hw-constraint rule
906 * @runtime: the pcm runtime instance
907 * @cond: condition bits
908 * @var: the variable to evaluate
909 * @func: the evaluation function
910 * @private: the private data pointer passed to function
911 * @dep: the dependent variables
913 * Returns zero if successful, or a negative error code on failure.
915 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
917 snd_pcm_hw_rule_func_t func, void *private,
920 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
921 struct snd_pcm_hw_rule *c;
925 if (constrs->rules_num >= constrs->rules_all) {
926 struct snd_pcm_hw_rule *new;
927 unsigned int new_rules = constrs->rules_all + 16;
928 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
931 if (constrs->rules) {
932 memcpy(new, constrs->rules,
933 constrs->rules_num * sizeof(*c));
934 kfree(constrs->rules);
936 constrs->rules = new;
937 constrs->rules_all = new_rules;
939 c = &constrs->rules[constrs->rules_num];
943 c->private = private;
946 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
950 dep = va_arg(args, int);
952 constrs->rules_num++;
958 * snd_pcm_hw_constraint_mask
959 * @runtime: PCM runtime instance
960 * @var: hw_params variable to apply the mask
961 * @mask: the bitmap mask
963 * Apply the constraint of the given bitmap mask to a mask parameter.
965 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
968 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
969 struct snd_mask *maskp = constrs_mask(constrs, var);
970 *maskp->bits &= mask;
971 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
972 if (*maskp->bits == 0)
978 * snd_pcm_hw_constraint_mask64
979 * @runtime: PCM runtime instance
980 * @var: hw_params variable to apply the mask
981 * @mask: the 64bit bitmap mask
983 * Apply the constraint of the given bitmap mask to a mask parameter.
985 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
988 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
989 struct snd_mask *maskp = constrs_mask(constrs, var);
990 maskp->bits[0] &= (u_int32_t)mask;
991 maskp->bits[1] &= (u_int32_t)(mask >> 32);
992 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
993 if (! maskp->bits[0] && ! maskp->bits[1])
999 * snd_pcm_hw_constraint_integer
1000 * @runtime: PCM runtime instance
1001 * @var: hw_params variable to apply the integer constraint
1003 * Apply the constraint of integer to an interval parameter.
1005 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1007 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1008 return snd_interval_setinteger(constrs_interval(constrs, var));
1012 * snd_pcm_hw_constraint_minmax
1013 * @runtime: PCM runtime instance
1014 * @var: hw_params variable to apply the range
1015 * @min: the minimal value
1016 * @max: the maximal value
1018 * Apply the min/max range constraint to an interval parameter.
1020 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1021 unsigned int min, unsigned int max)
1023 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1024 struct snd_interval t;
1027 t.openmin = t.openmax = 0;
1029 return snd_interval_refine(constrs_interval(constrs, var), &t);
1032 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1033 struct snd_pcm_hw_rule *rule)
1035 struct snd_pcm_hw_constraint_list *list = rule->private;
1036 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1041 * snd_pcm_hw_constraint_list
1042 * @runtime: PCM runtime instance
1043 * @cond: condition bits
1044 * @var: hw_params variable to apply the list constraint
1047 * Apply the list of constraints to an interval parameter.
1049 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1051 snd_pcm_hw_param_t var,
1052 struct snd_pcm_hw_constraint_list *l)
1054 return snd_pcm_hw_rule_add(runtime, cond, var,
1055 snd_pcm_hw_rule_list, l,
1059 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1060 struct snd_pcm_hw_rule *rule)
1062 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1063 unsigned int num = 0, den = 0;
1065 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1066 r->nrats, r->rats, &num, &den);
1067 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1068 params->rate_num = num;
1069 params->rate_den = den;
1075 * snd_pcm_hw_constraint_ratnums
1076 * @runtime: PCM runtime instance
1077 * @cond: condition bits
1078 * @var: hw_params variable to apply the ratnums constraint
1079 * @r: struct snd_ratnums constriants
1081 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1083 snd_pcm_hw_param_t var,
1084 struct snd_pcm_hw_constraint_ratnums *r)
1086 return snd_pcm_hw_rule_add(runtime, cond, var,
1087 snd_pcm_hw_rule_ratnums, r,
1091 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1092 struct snd_pcm_hw_rule *rule)
1094 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1095 unsigned int num = 0, den = 0;
1096 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1097 r->nrats, r->rats, &num, &den);
1098 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1099 params->rate_num = num;
1100 params->rate_den = den;
1106 * snd_pcm_hw_constraint_ratdens
1107 * @runtime: PCM runtime instance
1108 * @cond: condition bits
1109 * @var: hw_params variable to apply the ratdens constraint
1110 * @r: struct snd_ratdens constriants
1112 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1114 snd_pcm_hw_param_t var,
1115 struct snd_pcm_hw_constraint_ratdens *r)
1117 return snd_pcm_hw_rule_add(runtime, cond, var,
1118 snd_pcm_hw_rule_ratdens, r,
1122 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1123 struct snd_pcm_hw_rule *rule)
1125 unsigned int l = (unsigned long) rule->private;
1126 int width = l & 0xffff;
1127 unsigned int msbits = l >> 16;
1128 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1129 if (snd_interval_single(i) && snd_interval_value(i) == width)
1130 params->msbits = msbits;
1135 * snd_pcm_hw_constraint_msbits
1136 * @runtime: PCM runtime instance
1137 * @cond: condition bits
1138 * @width: sample bits width
1139 * @msbits: msbits width
1141 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1144 unsigned int msbits)
1146 unsigned long l = (msbits << 16) | width;
1147 return snd_pcm_hw_rule_add(runtime, cond, -1,
1148 snd_pcm_hw_rule_msbits,
1150 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1153 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1154 struct snd_pcm_hw_rule *rule)
1156 unsigned long step = (unsigned long) rule->private;
1157 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1161 * snd_pcm_hw_constraint_step
1162 * @runtime: PCM runtime instance
1163 * @cond: condition bits
1164 * @var: hw_params variable to apply the step constraint
1167 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1169 snd_pcm_hw_param_t var,
1172 return snd_pcm_hw_rule_add(runtime, cond, var,
1173 snd_pcm_hw_rule_step, (void *) step,
1177 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1179 static int pow2_sizes[] = {
1180 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1181 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1182 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1183 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1185 return snd_interval_list(hw_param_interval(params, rule->var),
1186 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1190 * snd_pcm_hw_constraint_pow2
1191 * @runtime: PCM runtime instance
1192 * @cond: condition bits
1193 * @var: hw_params variable to apply the power-of-2 constraint
1195 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1197 snd_pcm_hw_param_t var)
1199 return snd_pcm_hw_rule_add(runtime, cond, var,
1200 snd_pcm_hw_rule_pow2, NULL,
1204 /* To use the same code we have in alsa-lib */
1205 #define assert(i) snd_assert((i), return -EINVAL)
1207 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1210 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1211 snd_pcm_hw_param_t var)
1213 if (hw_is_mask(var)) {
1214 snd_mask_any(hw_param_mask(params, var));
1215 params->cmask |= 1 << var;
1216 params->rmask |= 1 << var;
1219 if (hw_is_interval(var)) {
1220 snd_interval_any(hw_param_interval(params, var));
1221 params->cmask |= 1 << var;
1222 params->rmask |= 1 << var;
1230 * snd_pcm_hw_param_any
1232 int snd_pcm_hw_param_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1233 snd_pcm_hw_param_t var)
1235 _snd_pcm_hw_param_any(params, var);
1236 return snd_pcm_hw_refine(pcm, params);
1240 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1243 memset(params, 0, sizeof(*params));
1244 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1245 _snd_pcm_hw_param_any(params, k);
1246 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1247 _snd_pcm_hw_param_any(params, k);
1253 * snd_pcm_hw_params_any
1255 * Fill PARAMS with full configuration space boundaries
1257 int snd_pcm_hw_params_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params)
1259 _snd_pcm_hw_params_any(params);
1260 return snd_pcm_hw_refine(pcm, params);
1265 * snd_pcm_hw_param_value
1266 * @params: the hw_params instance
1267 * @var: parameter to retrieve
1268 * @dir: pointer to the direction (-1,0,1) or NULL
1270 * Return the value for field PAR if it's fixed in configuration space
1271 * defined by PARAMS. Return -EINVAL otherwise
1273 static int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1274 snd_pcm_hw_param_t var, int *dir)
1276 if (hw_is_mask(var)) {
1277 const struct snd_mask *mask = hw_param_mask_c(params, var);
1278 if (!snd_mask_single(mask))
1282 return snd_mask_value(mask);
1284 if (hw_is_interval(var)) {
1285 const struct snd_interval *i = hw_param_interval_c(params, var);
1286 if (!snd_interval_single(i))
1290 return snd_interval_value(i);
1297 * snd_pcm_hw_param_value_min
1298 * @params: the hw_params instance
1299 * @var: parameter to retrieve
1300 * @dir: pointer to the direction (-1,0,1) or NULL
1302 * Return the minimum value for field PAR.
1304 unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
1305 snd_pcm_hw_param_t var, int *dir)
1307 if (hw_is_mask(var)) {
1310 return snd_mask_min(hw_param_mask_c(params, var));
1312 if (hw_is_interval(var)) {
1313 const struct snd_interval *i = hw_param_interval_c(params, var);
1316 return snd_interval_min(i);
1323 * snd_pcm_hw_param_value_max
1324 * @params: the hw_params instance
1325 * @var: parameter to retrieve
1326 * @dir: pointer to the direction (-1,0,1) or NULL
1328 * Return the maximum value for field PAR.
1330 unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
1331 snd_pcm_hw_param_t var, int *dir)
1333 if (hw_is_mask(var)) {
1336 return snd_mask_max(hw_param_mask_c(params, var));
1338 if (hw_is_interval(var)) {
1339 const struct snd_interval *i = hw_param_interval_c(params, var);
1341 *dir = - (int) i->openmax;
1342 return snd_interval_max(i);
1348 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1349 snd_pcm_hw_param_t var)
1351 if (hw_is_mask(var)) {
1352 snd_mask_none(hw_param_mask(params, var));
1353 params->cmask |= 1 << var;
1354 params->rmask |= 1 << var;
1355 } else if (hw_is_interval(var)) {
1356 snd_interval_none(hw_param_interval(params, var));
1357 params->cmask |= 1 << var;
1358 params->rmask |= 1 << var;
1364 int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
1365 snd_pcm_hw_param_t var)
1368 assert(hw_is_interval(var));
1369 changed = snd_interval_setinteger(hw_param_interval(params, var));
1371 params->cmask |= 1 << var;
1372 params->rmask |= 1 << var;
1379 * snd_pcm_hw_param_setinteger
1381 * Inside configuration space defined by PARAMS remove from PAR all
1382 * non integer values. Reduce configuration space accordingly.
1383 * Return -EINVAL if the configuration space is empty
1385 int snd_pcm_hw_param_setinteger(struct snd_pcm_substream *pcm,
1386 struct snd_pcm_hw_params *params,
1387 snd_pcm_hw_param_t var)
1389 int changed = _snd_pcm_hw_param_setinteger(params, var);
1392 if (params->rmask) {
1393 int err = snd_pcm_hw_refine(pcm, params);
1401 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1402 snd_pcm_hw_param_t var)
1405 if (hw_is_mask(var))
1406 changed = snd_mask_refine_first(hw_param_mask(params, var));
1407 else if (hw_is_interval(var))
1408 changed = snd_interval_refine_first(hw_param_interval(params, var));
1414 params->cmask |= 1 << var;
1415 params->rmask |= 1 << var;
1422 * snd_pcm_hw_param_first
1423 * @pcm: PCM instance
1424 * @params: the hw_params instance
1425 * @var: parameter to retrieve
1426 * @dir: pointer to the direction (-1,0,1) or NULL
1428 * Inside configuration space defined by PARAMS remove from PAR all
1429 * values > minimum. Reduce configuration space accordingly.
1430 * Return the minimum.
1432 static int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1433 struct snd_pcm_hw_params *params,
1434 snd_pcm_hw_param_t var, int *dir)
1436 int changed = _snd_pcm_hw_param_first(params, var);
1439 if (params->rmask) {
1440 int err = snd_pcm_hw_refine(pcm, params);
1443 return snd_pcm_hw_param_value(params, var, dir);
1446 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1447 snd_pcm_hw_param_t var)
1450 if (hw_is_mask(var))
1451 changed = snd_mask_refine_last(hw_param_mask(params, var));
1452 else if (hw_is_interval(var))
1453 changed = snd_interval_refine_last(hw_param_interval(params, var));
1459 params->cmask |= 1 << var;
1460 params->rmask |= 1 << var;
1467 * snd_pcm_hw_param_last
1468 * @pcm: PCM instance
1469 * @params: the hw_params instance
1470 * @var: parameter to retrieve
1471 * @dir: pointer to the direction (-1,0,1) or NULL
1473 * Inside configuration space defined by PARAMS remove from PAR all
1474 * values < maximum. Reduce configuration space accordingly.
1475 * Return the maximum.
1477 static int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1478 struct snd_pcm_hw_params *params,
1479 snd_pcm_hw_param_t var, int *dir)
1481 int changed = _snd_pcm_hw_param_last(params, var);
1484 if (params->rmask) {
1485 int err = snd_pcm_hw_refine(pcm, params);
1488 return snd_pcm_hw_param_value(params, var, dir);
1491 int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
1492 snd_pcm_hw_param_t var, unsigned int val, int dir)
1499 } else if (dir < 0) {
1506 if (hw_is_mask(var))
1507 changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open);
1508 else if (hw_is_interval(var))
1509 changed = snd_interval_refine_min(hw_param_interval(params, var), val, open);
1515 params->cmask |= 1 << var;
1516 params->rmask |= 1 << var;
1522 * snd_pcm_hw_param_min
1523 * @pcm: PCM instance
1524 * @params: the hw_params instance
1525 * @var: parameter to retrieve
1526 * @val: minimal value
1527 * @dir: pointer to the direction (-1,0,1) or NULL
1529 * Inside configuration space defined by PARAMS remove from PAR all
1530 * values < VAL. Reduce configuration space accordingly.
1531 * Return new minimum or -EINVAL if the configuration space is empty
1533 static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1534 snd_pcm_hw_param_t var, unsigned int val,
1537 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
1540 if (params->rmask) {
1541 int err = snd_pcm_hw_refine(pcm, params);
1545 return snd_pcm_hw_param_value_min(params, var, dir);
1548 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
1549 snd_pcm_hw_param_t var, unsigned int val,
1557 } else if (dir > 0) {
1562 if (hw_is_mask(var)) {
1563 if (val == 0 && open) {
1564 snd_mask_none(hw_param_mask(params, var));
1567 changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open);
1568 } else if (hw_is_interval(var))
1569 changed = snd_interval_refine_max(hw_param_interval(params, var), val, open);
1575 params->cmask |= 1 << var;
1576 params->rmask |= 1 << var;
1582 * snd_pcm_hw_param_max
1583 * @pcm: PCM instance
1584 * @params: the hw_params instance
1585 * @var: parameter to retrieve
1586 * @val: maximal value
1587 * @dir: pointer to the direction (-1,0,1) or NULL
1589 * Inside configuration space defined by PARAMS remove from PAR all
1590 * values >= VAL + 1. Reduce configuration space accordingly.
1591 * Return new maximum or -EINVAL if the configuration space is empty
1593 static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1594 snd_pcm_hw_param_t var, unsigned int val,
1597 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
1600 if (params->rmask) {
1601 int err = snd_pcm_hw_refine(pcm, params);
1605 return snd_pcm_hw_param_value_max(params, var, dir);
1608 int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
1609 snd_pcm_hw_param_t var, unsigned int val, int dir)
1612 if (hw_is_mask(var)) {
1613 struct snd_mask *m = hw_param_mask(params, var);
1614 if (val == 0 && dir < 0) {
1622 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
1624 } else if (hw_is_interval(var)) {
1625 struct snd_interval *i = hw_param_interval(params, var);
1626 if (val == 0 && dir < 0) {
1628 snd_interval_none(i);
1629 } else if (dir == 0)
1630 changed = snd_interval_refine_set(i, val);
1632 struct snd_interval t;
1644 changed = snd_interval_refine(i, &t);
1651 params->cmask |= 1 << var;
1652 params->rmask |= 1 << var;
1658 * snd_pcm_hw_param_set
1659 * @pcm: PCM instance
1660 * @params: the hw_params instance
1661 * @var: parameter to retrieve
1662 * @val: value to set
1663 * @dir: pointer to the direction (-1,0,1) or NULL
1665 * Inside configuration space defined by PARAMS remove from PAR all
1666 * values != VAL. Reduce configuration space accordingly.
1667 * Return VAL or -EINVAL if the configuration space is empty
1669 int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1670 snd_pcm_hw_param_t var, unsigned int val, int dir)
1672 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
1675 if (params->rmask) {
1676 int err = snd_pcm_hw_refine(pcm, params);
1680 return snd_pcm_hw_param_value(params, var, NULL);
1683 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
1684 snd_pcm_hw_param_t var, const struct snd_mask *val)
1687 assert(hw_is_mask(var));
1688 changed = snd_mask_refine(hw_param_mask(params, var), val);
1690 params->cmask |= 1 << var;
1691 params->rmask |= 1 << var;
1697 * snd_pcm_hw_param_mask
1698 * @pcm: PCM instance
1699 * @params: the hw_params instance
1700 * @var: parameter to retrieve
1701 * @val: mask to apply
1703 * Inside configuration space defined by PARAMS remove from PAR all values
1704 * not contained in MASK. Reduce configuration space accordingly.
1705 * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
1706 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1707 * Return 0 on success or -EINVAL
1708 * if the configuration space is empty
1710 int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1711 snd_pcm_hw_param_t var, const struct snd_mask *val)
1713 int changed = _snd_pcm_hw_param_mask(params, var, val);
1716 if (params->rmask) {
1717 int err = snd_pcm_hw_refine(pcm, params);
1724 static int boundary_sub(int a, int adir,
1728 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
1729 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
1731 *cdir = adir - bdir;
1733 assert(*c > INT_MIN);
1735 } else if (*cdir == 2) {
1736 assert(*c < INT_MAX);
1742 static int boundary_lt(unsigned int a, int adir,
1743 unsigned int b, int bdir)
1745 assert(a > 0 || adir >= 0);
1746 assert(b > 0 || bdir >= 0);
1750 } else if (adir > 0)
1755 } else if (bdir > 0)
1757 return a < b || (a == b && adir < bdir);
1760 /* Return 1 if min is nearer to best than max */
1761 static int boundary_nearer(int min, int mindir,
1762 int best, int bestdir,
1763 int max, int maxdir)
1767 boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
1768 boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
1769 return boundary_lt(dmin, dmindir, dmax, dmaxdir);
1773 * snd_pcm_hw_param_near
1774 * @pcm: PCM instance
1775 * @params: the hw_params instance
1776 * @var: parameter to retrieve
1777 * @best: value to set
1778 * @dir: pointer to the direction (-1,0,1) or NULL
1780 * Inside configuration space defined by PARAMS set PAR to the available value
1781 * nearest to VAL. Reduce configuration space accordingly.
1782 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
1783 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1784 * Return the value found.
1786 int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
1787 snd_pcm_hw_param_t var, unsigned int best, int *dir)
1789 struct snd_pcm_hw_params *save = NULL;
1791 unsigned int saved_min;
1795 int valdir = dir ? *dir : 0;
1800 mindir = maxdir = valdir;
1803 else if (maxdir == 0)
1809 save = kmalloc(sizeof(*save), GFP_KERNEL);
1814 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
1816 struct snd_pcm_hw_params *params1;
1819 if ((unsigned int)min == saved_min && mindir == valdir)
1821 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
1822 if (params1 == NULL) {
1827 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
1832 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
1839 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
1846 v = snd_pcm_hw_param_last(pcm, params, var, dir);
1848 v = snd_pcm_hw_param_first(pcm, params, var, dir);
1854 * snd_pcm_hw_param_choose
1855 * @pcm: PCM instance
1856 * @params: the hw_params instance
1858 * Choose one configuration from configuration space defined by PARAMS
1859 * The configuration chosen is that obtained fixing in this order:
1860 * first access, first format, first subformat, min channels,
1861 * min rate, min period time, max buffer size, min tick time
1863 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params)
1867 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_ACCESS, NULL);
1870 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_FORMAT, NULL);
1873 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_SUBFORMAT, NULL);
1876 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_CHANNELS, NULL);
1879 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_RATE, NULL);
1882 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, NULL);
1885 err = snd_pcm_hw_param_last(pcm, params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL);
1888 err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_TICK_TIME, NULL);
1896 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1899 struct snd_pcm_runtime *runtime = substream->runtime;
1900 unsigned long flags;
1901 snd_pcm_stream_lock_irqsave(substream, flags);
1902 if (snd_pcm_running(substream) &&
1903 snd_pcm_update_hw_ptr(substream) >= 0)
1904 runtime->status->hw_ptr %= runtime->buffer_size;
1906 runtime->status->hw_ptr = 0;
1907 snd_pcm_stream_unlock_irqrestore(substream, flags);
1911 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1914 struct snd_pcm_channel_info *info = arg;
1915 struct snd_pcm_runtime *runtime = substream->runtime;
1917 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1921 width = snd_pcm_format_physical_width(runtime->format);
1925 switch (runtime->access) {
1926 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1927 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1928 info->first = info->channel * width;
1929 info->step = runtime->channels * width;
1931 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1932 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1934 size_t size = runtime->dma_bytes / runtime->channels;
1935 info->first = info->channel * size * 8;
1947 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1948 * @substream: the pcm substream instance
1949 * @cmd: ioctl command
1950 * @arg: ioctl argument
1952 * Processes the generic ioctl commands for PCM.
1953 * Can be passed as the ioctl callback for PCM ops.
1955 * Returns zero if successful, or a negative error code on failure.
1957 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1958 unsigned int cmd, void *arg)
1961 case SNDRV_PCM_IOCTL1_INFO:
1963 case SNDRV_PCM_IOCTL1_RESET:
1964 return snd_pcm_lib_ioctl_reset(substream, arg);
1965 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1966 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1975 static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
1976 unsigned long ticks)
1978 struct snd_pcm_runtime *runtime = substream->runtime;
1980 del_timer(&runtime->tick_timer);
1982 ticks += (1000000 / HZ) - 1;
1983 ticks /= (1000000 / HZ);
1984 mod_timer(&runtime->tick_timer, jiffies + ticks);
1988 /* Temporary alias */
1989 void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
1991 snd_pcm_system_tick_set(substream, ticks);
1994 void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
1996 struct snd_pcm_runtime *runtime = substream->runtime;
1997 snd_pcm_uframes_t frames = ULONG_MAX;
1998 snd_pcm_uframes_t avail, dist;
2002 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2003 if (runtime->silence_size >= runtime->boundary) {
2005 } else if (runtime->silence_size > 0 &&
2006 runtime->silence_filled < runtime->buffer_size) {
2007 snd_pcm_sframes_t noise_dist;
2008 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
2009 snd_assert(noise_dist <= (snd_pcm_sframes_t)runtime->silence_threshold, );
2010 frames = noise_dist - runtime->silence_threshold;
2012 avail = snd_pcm_playback_avail(runtime);
2014 avail = snd_pcm_capture_avail(runtime);
2016 if (avail < runtime->control->avail_min) {
2017 snd_pcm_sframes_t n = runtime->control->avail_min - avail;
2018 if (n > 0 && frames > (snd_pcm_uframes_t)n)
2021 if (avail < runtime->buffer_size) {
2022 snd_pcm_sframes_t n = runtime->buffer_size - avail;
2023 if (n > 0 && frames > (snd_pcm_uframes_t)n)
2026 if (frames == ULONG_MAX) {
2027 snd_pcm_tick_set(substream, 0);
2030 dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
2031 /* Distance to next interrupt */
2032 dist = runtime->period_size - dist % runtime->period_size;
2033 if (dist <= frames) {
2034 snd_pcm_tick_set(substream, 0);
2037 /* the base time is us */
2040 div64_32(&n, runtime->tick_time * runtime->rate, &r);
2041 ticks = n + (r > 0 ? 1 : 0);
2042 if (ticks < runtime->sleep_min)
2043 ticks = runtime->sleep_min;
2044 snd_pcm_tick_set(substream, (unsigned long) ticks);
2047 void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
2049 struct snd_pcm_runtime *runtime;
2050 unsigned long flags;
2052 snd_assert(substream != NULL, return);
2053 runtime = substream->runtime;
2054 snd_assert(runtime != NULL, return);
2056 snd_pcm_stream_lock_irqsave(substream, flags);
2057 if (!snd_pcm_running(substream) ||
2058 snd_pcm_update_hw_ptr(substream) < 0)
2060 if (runtime->sleep_min)
2061 snd_pcm_tick_prepare(substream);
2063 snd_pcm_stream_unlock_irqrestore(substream, flags);
2067 * snd_pcm_period_elapsed - update the pcm status for the next period
2068 * @substream: the pcm substream instance
2070 * This function is called from the interrupt handler when the
2071 * PCM has processed the period size. It will update the current
2072 * pointer, set up the tick, wake up sleepers, etc.
2074 * Even if more than one periods have elapsed since the last call, you
2075 * have to call this only once.
2077 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
2079 struct snd_pcm_runtime *runtime;
2080 unsigned long flags;
2082 snd_assert(substream != NULL, return);
2083 runtime = substream->runtime;
2084 snd_assert(runtime != NULL, return);
2086 if (runtime->transfer_ack_begin)
2087 runtime->transfer_ack_begin(substream);
2089 snd_pcm_stream_lock_irqsave(substream, flags);
2090 if (!snd_pcm_running(substream) ||
2091 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
2094 if (substream->timer_running)
2095 snd_timer_interrupt(substream->timer, 1);
2096 if (runtime->sleep_min)
2097 snd_pcm_tick_prepare(substream);
2099 snd_pcm_stream_unlock_irqrestore(substream, flags);
2100 if (runtime->transfer_ack_end)
2101 runtime->transfer_ack_end(substream);
2102 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
2105 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
2107 unsigned long data, unsigned int off,
2108 snd_pcm_uframes_t frames)
2110 struct snd_pcm_runtime *runtime = substream->runtime;
2112 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2113 if (substream->ops->copy) {
2114 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2117 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2118 snd_assert(runtime->dma_area, return -EFAULT);
2119 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2125 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
2126 unsigned long data, unsigned int off,
2127 snd_pcm_uframes_t size);
2129 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
2131 snd_pcm_uframes_t size,
2133 transfer_f transfer)
2135 struct snd_pcm_runtime *runtime = substream->runtime;
2136 snd_pcm_uframes_t xfer = 0;
2137 snd_pcm_uframes_t offset = 0;
2142 if (size > runtime->xfer_align)
2143 size -= size % runtime->xfer_align;
2145 snd_pcm_stream_lock_irq(substream);
2146 switch (runtime->status->state) {
2147 case SNDRV_PCM_STATE_PREPARED:
2148 case SNDRV_PCM_STATE_RUNNING:
2149 case SNDRV_PCM_STATE_PAUSED:
2151 case SNDRV_PCM_STATE_XRUN:
2154 case SNDRV_PCM_STATE_SUSPENDED:
2163 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2164 snd_pcm_uframes_t avail;
2165 snd_pcm_uframes_t cont;
2166 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2167 snd_pcm_update_hw_ptr(substream);
2168 avail = snd_pcm_playback_avail(runtime);
2169 if (((avail < runtime->control->avail_min && size > avail) ||
2170 (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
2172 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
2180 init_waitqueue_entry(&wait, current);
2181 add_wait_queue(&runtime->sleep, &wait);
2183 if (signal_pending(current)) {
2187 set_current_state(TASK_INTERRUPTIBLE);
2188 snd_pcm_stream_unlock_irq(substream);
2189 tout = schedule_timeout(10 * HZ);
2190 snd_pcm_stream_lock_irq(substream);
2192 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2193 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2194 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2198 switch (runtime->status->state) {
2199 case SNDRV_PCM_STATE_XRUN:
2200 case SNDRV_PCM_STATE_DRAINING:
2203 case SNDRV_PCM_STATE_SUSPENDED:
2206 case SNDRV_PCM_STATE_SETUP:
2212 avail = snd_pcm_playback_avail(runtime);
2213 if (avail >= runtime->control->avail_min) {
2219 remove_wait_queue(&runtime->sleep, &wait);
2232 snd_printd("playback write error (DMA or IRQ trouble?)\n");
2242 if (avail > runtime->xfer_align)
2243 avail -= avail % runtime->xfer_align;
2244 frames = size > avail ? avail : size;
2245 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2248 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2249 appl_ptr = runtime->control->appl_ptr;
2250 appl_ofs = appl_ptr % runtime->buffer_size;
2251 snd_pcm_stream_unlock_irq(substream);
2252 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2254 snd_pcm_stream_lock_irq(substream);
2255 switch (runtime->status->state) {
2256 case SNDRV_PCM_STATE_XRUN:
2259 case SNDRV_PCM_STATE_SUSPENDED:
2266 if (appl_ptr >= runtime->boundary)
2267 appl_ptr -= runtime->boundary;
2268 runtime->control->appl_ptr = appl_ptr;
2269 if (substream->ops->ack)
2270 substream->ops->ack(substream);
2275 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2276 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2277 err = snd_pcm_start(substream);
2281 if (runtime->sleep_min &&
2282 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2283 snd_pcm_tick_prepare(substream);
2286 snd_pcm_stream_unlock_irq(substream);
2288 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2291 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
2293 struct snd_pcm_runtime *runtime;
2296 snd_assert(substream != NULL, return -ENXIO);
2297 runtime = substream->runtime;
2298 snd_assert(runtime != NULL, return -ENXIO);
2299 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2300 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2303 snd_assert(substream->ffile != NULL, return -ENXIO);
2304 nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2305 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2306 if (substream->oss.oss) {
2307 struct snd_pcm_oss_setup *setup = substream->oss.setup;
2308 if (setup != NULL) {
2309 if (setup->nonblock)
2311 else if (setup->block)
2317 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2318 runtime->channels > 1)
2320 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2321 snd_pcm_lib_write_transfer);
2324 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
2326 unsigned long data, unsigned int off,
2327 snd_pcm_uframes_t frames)
2329 struct snd_pcm_runtime *runtime = substream->runtime;
2331 void __user **bufs = (void __user **)data;
2332 int channels = runtime->channels;
2334 if (substream->ops->copy) {
2335 snd_assert(substream->ops->silence != NULL, return -EINVAL);
2336 for (c = 0; c < channels; ++c, ++bufs) {
2337 if (*bufs == NULL) {
2338 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2341 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2342 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2347 /* default transfer behaviour */
2348 size_t dma_csize = runtime->dma_bytes / channels;
2349 snd_assert(runtime->dma_area, return -EFAULT);
2350 for (c = 0; c < channels; ++c, ++bufs) {
2351 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2352 if (*bufs == NULL) {
2353 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2355 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2356 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2364 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
2366 snd_pcm_uframes_t frames)
2368 struct snd_pcm_runtime *runtime;
2371 snd_assert(substream != NULL, return -ENXIO);
2372 runtime = substream->runtime;
2373 snd_assert(runtime != NULL, return -ENXIO);
2374 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2375 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2378 snd_assert(substream->ffile != NULL, return -ENXIO);
2379 nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2380 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2381 if (substream->oss.oss) {
2382 struct snd_pcm_oss_setup *setup = substream->oss.setup;
2383 if (setup != NULL) {
2384 if (setup->nonblock)
2386 else if (setup->block)
2392 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2394 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2395 nonblock, snd_pcm_lib_writev_transfer);
2398 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2400 unsigned long data, unsigned int off,
2401 snd_pcm_uframes_t frames)
2403 struct snd_pcm_runtime *runtime = substream->runtime;
2405 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2406 if (substream->ops->copy) {
2407 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2410 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2411 snd_assert(runtime->dma_area, return -EFAULT);
2412 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2418 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2420 snd_pcm_uframes_t size,
2422 transfer_f transfer)
2424 struct snd_pcm_runtime *runtime = substream->runtime;
2425 snd_pcm_uframes_t xfer = 0;
2426 snd_pcm_uframes_t offset = 0;
2431 if (size > runtime->xfer_align)
2432 size -= size % runtime->xfer_align;
2434 snd_pcm_stream_lock_irq(substream);
2435 switch (runtime->status->state) {
2436 case SNDRV_PCM_STATE_PREPARED:
2437 if (size >= runtime->start_threshold) {
2438 err = snd_pcm_start(substream);
2443 case SNDRV_PCM_STATE_DRAINING:
2444 case SNDRV_PCM_STATE_RUNNING:
2445 case SNDRV_PCM_STATE_PAUSED:
2447 case SNDRV_PCM_STATE_XRUN:
2450 case SNDRV_PCM_STATE_SUSPENDED:
2459 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2460 snd_pcm_uframes_t avail;
2461 snd_pcm_uframes_t cont;
2462 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2463 snd_pcm_update_hw_ptr(substream);
2465 avail = snd_pcm_capture_avail(runtime);
2466 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2467 if (avail < runtime->xfer_align) {
2471 } else if ((avail < runtime->control->avail_min && size > avail) ||
2472 (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
2474 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
2482 init_waitqueue_entry(&wait, current);
2483 add_wait_queue(&runtime->sleep, &wait);
2485 if (signal_pending(current)) {
2489 set_current_state(TASK_INTERRUPTIBLE);
2490 snd_pcm_stream_unlock_irq(substream);
2491 tout = schedule_timeout(10 * HZ);
2492 snd_pcm_stream_lock_irq(substream);
2494 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2495 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2496 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2500 switch (runtime->status->state) {
2501 case SNDRV_PCM_STATE_XRUN:
2504 case SNDRV_PCM_STATE_SUSPENDED:
2507 case SNDRV_PCM_STATE_DRAINING:
2509 case SNDRV_PCM_STATE_SETUP:
2515 avail = snd_pcm_capture_avail(runtime);
2516 if (avail >= runtime->control->avail_min) {
2522 remove_wait_queue(&runtime->sleep, &wait);
2535 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2545 if (avail > runtime->xfer_align)
2546 avail -= avail % runtime->xfer_align;
2547 frames = size > avail ? avail : size;
2548 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2551 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2552 appl_ptr = runtime->control->appl_ptr;
2553 appl_ofs = appl_ptr % runtime->buffer_size;
2554 snd_pcm_stream_unlock_irq(substream);
2555 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2557 snd_pcm_stream_lock_irq(substream);
2558 switch (runtime->status->state) {
2559 case SNDRV_PCM_STATE_XRUN:
2562 case SNDRV_PCM_STATE_SUSPENDED:
2569 if (appl_ptr >= runtime->boundary)
2570 appl_ptr -= runtime->boundary;
2571 runtime->control->appl_ptr = appl_ptr;
2572 if (substream->ops->ack)
2573 substream->ops->ack(substream);
2578 if (runtime->sleep_min &&
2579 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2580 snd_pcm_tick_prepare(substream);
2583 snd_pcm_stream_unlock_irq(substream);
2585 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2588 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2590 struct snd_pcm_runtime *runtime;
2593 snd_assert(substream != NULL, return -ENXIO);
2594 runtime = substream->runtime;
2595 snd_assert(runtime != NULL, return -ENXIO);
2596 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2597 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2600 snd_assert(substream->ffile != NULL, return -ENXIO);
2601 nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2602 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2603 if (substream->oss.oss) {
2604 struct snd_pcm_oss_setup *setup = substream->oss.setup;
2605 if (setup != NULL) {
2606 if (setup->nonblock)
2608 else if (setup->block)
2613 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2615 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2618 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2620 unsigned long data, unsigned int off,
2621 snd_pcm_uframes_t frames)
2623 struct snd_pcm_runtime *runtime = substream->runtime;
2625 void __user **bufs = (void __user **)data;
2626 int channels = runtime->channels;
2628 if (substream->ops->copy) {
2629 for (c = 0; c < channels; ++c, ++bufs) {
2633 buf = *bufs + samples_to_bytes(runtime, off);
2634 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2638 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2639 snd_assert(runtime->dma_area, return -EFAULT);
2640 for (c = 0; c < channels; ++c, ++bufs) {
2646 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2647 buf = *bufs + samples_to_bytes(runtime, off);
2648 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2655 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2657 snd_pcm_uframes_t frames)
2659 struct snd_pcm_runtime *runtime;
2662 snd_assert(substream != NULL, return -ENXIO);
2663 runtime = substream->runtime;
2664 snd_assert(runtime != NULL, return -ENXIO);
2665 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2666 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2669 snd_assert(substream->ffile != NULL, return -ENXIO);
2670 nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2671 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2672 if (substream->oss.oss) {
2673 struct snd_pcm_oss_setup *setup = substream->oss.setup;
2674 if (setup != NULL) {
2675 if (setup->nonblock)
2677 else if (setup->block)
2683 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2685 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2692 EXPORT_SYMBOL(snd_interval_refine);
2693 EXPORT_SYMBOL(snd_interval_list);
2694 EXPORT_SYMBOL(snd_interval_ratnum);
2695 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
2696 EXPORT_SYMBOL(_snd_pcm_hw_param_min);
2697 EXPORT_SYMBOL(_snd_pcm_hw_param_set);
2698 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
2699 EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger);
2700 EXPORT_SYMBOL(snd_pcm_hw_param_value_min);
2701 EXPORT_SYMBOL(snd_pcm_hw_param_value_max);
2702 EXPORT_SYMBOL(snd_pcm_hw_param_mask);
2703 EXPORT_SYMBOL(snd_pcm_hw_param_first);
2704 EXPORT_SYMBOL(snd_pcm_hw_param_last);
2705 EXPORT_SYMBOL(snd_pcm_hw_param_near);
2706 EXPORT_SYMBOL(snd_pcm_hw_param_set);
2707 EXPORT_SYMBOL(snd_pcm_hw_refine);
2708 EXPORT_SYMBOL(snd_pcm_hw_constraints_init);
2709 EXPORT_SYMBOL(snd_pcm_hw_constraints_complete);
2710 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
2711 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
2712 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
2713 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
2714 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
2715 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
2716 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
2717 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
2718 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
2719 EXPORT_SYMBOL(snd_pcm_set_ops);
2720 EXPORT_SYMBOL(snd_pcm_set_sync);
2721 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
2722 EXPORT_SYMBOL(snd_pcm_stop);
2723 EXPORT_SYMBOL(snd_pcm_period_elapsed);
2724 EXPORT_SYMBOL(snd_pcm_lib_write);
2725 EXPORT_SYMBOL(snd_pcm_lib_read);
2726 EXPORT_SYMBOL(snd_pcm_lib_writev);
2727 EXPORT_SYMBOL(snd_pcm_lib_readv);
2728 EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes);
2729 EXPORT_SYMBOL(snd_pcm_lib_period_bytes);
2731 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
2732 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
2733 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
2734 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
2735 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
2736 EXPORT_SYMBOL(snd_pcm_lib_free_pages);