2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/info.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/timer.h>
33 * fill ring buffer with silence
34 * runtime->silence_start: starting pointer to silence area
35 * runtime->silence_filled: size filled with silence
36 * runtime->silence_threshold: threshold from application
37 * runtime->silence_size: maximal size from application
39 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
43 struct snd_pcm_runtime *runtime = substream->runtime;
44 snd_pcm_uframes_t frames, ofs, transfer;
46 if (runtime->silence_size < runtime->boundary) {
47 snd_pcm_sframes_t noise_dist, n;
48 if (runtime->silence_start != runtime->control->appl_ptr) {
49 n = runtime->control->appl_ptr - runtime->silence_start;
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
58 if (runtime->silence_filled >= runtime->buffer_size)
60 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
67 if (new_hw_ptr == ULONG_MAX) { /* initialization */
68 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69 runtime->silence_filled = avail > 0 ? avail : 0;
70 runtime->silence_start = (runtime->status->hw_ptr +
71 runtime->silence_filled) %
74 ofs = runtime->status->hw_ptr;
75 frames = new_hw_ptr - ofs;
76 if ((snd_pcm_sframes_t)frames < 0)
77 frames += runtime->boundary;
78 runtime->silence_filled -= frames;
79 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80 runtime->silence_filled = 0;
81 runtime->silence_start = new_hw_ptr;
83 runtime->silence_start = ofs;
86 frames = runtime->buffer_size - runtime->silence_filled;
88 if (snd_BUG_ON(frames > runtime->buffer_size))
92 ofs = runtime->silence_start % runtime->buffer_size;
94 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
95 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
96 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
97 if (substream->ops->silence) {
99 err = substream->ops->silence(substream, -1, ofs, transfer);
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
111 err = substream->ops->silence(substream, c, ofs, transfer);
115 size_t dma_csize = runtime->dma_bytes / channels;
116 for (c = 0; c < channels; ++c) {
117 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
118 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122 runtime->silence_filled += transfer;
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129 #define xrun_debug(substream) ((substream)->pstr->xrun_debug)
131 #define xrun_debug(substream) 0
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
139 static void xrun(struct snd_pcm_substream *substream)
141 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
142 if (xrun_debug(substream)) {
143 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p');
147 dump_stack_on_xrun(substream);
151 static snd_pcm_uframes_t
152 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
153 struct snd_pcm_runtime *runtime)
155 snd_pcm_uframes_t pos;
157 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
158 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
159 pos = substream->ops->pointer(substream);
160 if (pos == SNDRV_PCM_POS_XRUN)
161 return pos; /* XRUN */
162 if (pos >= runtime->buffer_size) {
163 if (printk_ratelimit()) {
164 snd_printd(KERN_ERR "BUG: stream = %i, pos = 0x%lx, "
165 "buffer size = 0x%lx, period size = 0x%lx\n",
166 substream->stream, pos, runtime->buffer_size,
167 runtime->period_size);
171 pos -= pos % runtime->min_align;
175 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
176 struct snd_pcm_runtime *runtime)
178 snd_pcm_uframes_t avail;
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
181 avail = snd_pcm_playback_avail(runtime);
183 avail = snd_pcm_capture_avail(runtime);
184 if (avail > runtime->avail_max)
185 runtime->avail_max = avail;
186 if (avail >= runtime->stop_threshold) {
187 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
188 snd_pcm_drain_done(substream);
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
198 #define hw_ptr_error(substream, fmt, args...) \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
202 snd_printd("PCM: " fmt, ##args); \
204 dump_stack_on_xrun(substream); \
208 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 snd_pcm_uframes_t pos;
212 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
213 snd_pcm_sframes_t hdelta, delta;
214 unsigned long jdelta;
216 old_hw_ptr = runtime->status->hw_ptr;
217 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
218 if (pos == SNDRV_PCM_POS_XRUN) {
222 hw_base = runtime->hw_ptr_base;
223 new_hw_ptr = hw_base + pos;
224 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
225 delta = new_hw_ptr - hw_ptr_interrupt;
226 if (hw_ptr_interrupt >= runtime->boundary) {
227 hw_ptr_interrupt -= runtime->boundary;
228 if (hw_base < runtime->boundary / 2)
229 /* hw_base was already lapped; recalc delta */
230 delta = new_hw_ptr - hw_ptr_interrupt;
233 delta += runtime->buffer_size;
235 hw_ptr_error(substream,
236 "Unexpected hw_pointer value "
237 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
238 substream->stream, (long)pos,
239 (long)hw_ptr_interrupt);
240 /* rebase to interrupt position */
241 hw_base = new_hw_ptr = hw_ptr_interrupt;
242 /* align hw_base to buffer_size */
243 hw_base -= hw_base % runtime->buffer_size;
246 hw_base += runtime->buffer_size;
247 if (hw_base >= runtime->boundary)
249 new_hw_ptr = hw_base + pos;
252 hdelta = new_hw_ptr - old_hw_ptr;
253 jdelta = jiffies - runtime->hw_ptr_jiffies;
254 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
256 (((runtime->period_size * HZ) / runtime->rate)
258 hw_ptr_error(substream,
259 "hw_ptr skipping! [Q] "
260 "(pos=%ld, delta=%ld, period=%ld, "
261 "jdelta=%lu/%lu/%lu)\n",
262 (long)pos, (long)hdelta,
263 (long)runtime->period_size, jdelta,
264 ((hdelta * HZ) / runtime->rate), delta);
265 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
266 runtime->period_size * delta;
267 if (hw_ptr_interrupt >= runtime->boundary)
268 hw_ptr_interrupt -= runtime->boundary;
269 /* rebase to interrupt position */
270 hw_base = new_hw_ptr = hw_ptr_interrupt;
271 /* align hw_base to buffer_size */
272 hw_base -= hw_base % runtime->buffer_size;
275 if (delta > runtime->period_size + runtime->period_size / 2) {
276 hw_ptr_error(substream,
278 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
279 substream->stream, (long)delta,
280 (long)hw_ptr_interrupt);
281 /* rebase hw_ptr_interrupt */
283 new_hw_ptr - new_hw_ptr % runtime->period_size;
285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
286 runtime->silence_size > 0)
287 snd_pcm_playback_silence(substream, new_hw_ptr);
289 runtime->hw_ptr_base = hw_base;
290 runtime->status->hw_ptr = new_hw_ptr;
291 runtime->hw_ptr_jiffies = jiffies;
292 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
294 return snd_pcm_update_hw_ptr_post(substream, runtime);
297 /* CAUTION: call it with irq disabled */
298 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
300 struct snd_pcm_runtime *runtime = substream->runtime;
301 snd_pcm_uframes_t pos;
302 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
303 snd_pcm_sframes_t delta;
304 unsigned long jdelta;
306 old_hw_ptr = runtime->status->hw_ptr;
307 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
308 if (pos == SNDRV_PCM_POS_XRUN) {
312 hw_base = runtime->hw_ptr_base;
313 new_hw_ptr = hw_base + pos;
315 delta = new_hw_ptr - old_hw_ptr;
316 jdelta = jiffies - runtime->hw_ptr_jiffies;
318 delta += runtime->buffer_size;
320 hw_ptr_error(substream,
321 "Unexpected hw_pointer value [2] "
322 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
323 substream->stream, (long)pos,
324 (long)old_hw_ptr, jdelta);
327 hw_base += runtime->buffer_size;
328 if (hw_base >= runtime->boundary)
330 new_hw_ptr = hw_base + pos;
332 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
333 hw_ptr_error(substream,
335 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
336 (long)pos, (long)delta,
337 (long)runtime->period_size, jdelta,
338 ((delta * HZ) / runtime->rate));
341 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
342 runtime->silence_size > 0)
343 snd_pcm_playback_silence(substream, new_hw_ptr);
345 runtime->hw_ptr_base = hw_base;
346 runtime->status->hw_ptr = new_hw_ptr;
347 runtime->hw_ptr_jiffies = jiffies;
349 return snd_pcm_update_hw_ptr_post(substream, runtime);
353 * snd_pcm_set_ops - set the PCM operators
354 * @pcm: the pcm instance
355 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
356 * @ops: the operator table
358 * Sets the given PCM operators to the pcm instance.
360 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
362 struct snd_pcm_str *stream = &pcm->streams[direction];
363 struct snd_pcm_substream *substream;
365 for (substream = stream->substream; substream != NULL; substream = substream->next)
366 substream->ops = ops;
369 EXPORT_SYMBOL(snd_pcm_set_ops);
372 * snd_pcm_sync - set the PCM sync id
373 * @substream: the pcm substream
375 * Sets the PCM sync identifier for the card.
377 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
379 struct snd_pcm_runtime *runtime = substream->runtime;
381 runtime->sync.id32[0] = substream->pcm->card->number;
382 runtime->sync.id32[1] = -1;
383 runtime->sync.id32[2] = -1;
384 runtime->sync.id32[3] = -1;
387 EXPORT_SYMBOL(snd_pcm_set_sync);
390 * Standard ioctl routine
393 static inline unsigned int div32(unsigned int a, unsigned int b,
404 static inline unsigned int div_down(unsigned int a, unsigned int b)
411 static inline unsigned int div_up(unsigned int a, unsigned int b)
423 static inline unsigned int mul(unsigned int a, unsigned int b)
427 if (div_down(UINT_MAX, a) < b)
432 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
433 unsigned int c, unsigned int *r)
435 u_int64_t n = (u_int64_t) a * b;
450 * snd_interval_refine - refine the interval value of configurator
451 * @i: the interval value to refine
452 * @v: the interval value to refer to
454 * Refines the interval value with the reference value.
455 * The interval is changed to the range satisfying both intervals.
456 * The interval status (min, max, integer, etc.) are evaluated.
458 * Returns non-zero if the value is changed, zero if not changed.
460 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
463 if (snd_BUG_ON(snd_interval_empty(i)))
465 if (i->min < v->min) {
467 i->openmin = v->openmin;
469 } else if (i->min == v->min && !i->openmin && v->openmin) {
473 if (i->max > v->max) {
475 i->openmax = v->openmax;
477 } else if (i->max == v->max && !i->openmax && v->openmax) {
481 if (!i->integer && v->integer) {
494 } else if (!i->openmin && !i->openmax && i->min == i->max)
496 if (snd_interval_checkempty(i)) {
497 snd_interval_none(i);
503 EXPORT_SYMBOL(snd_interval_refine);
505 static int snd_interval_refine_first(struct snd_interval *i)
507 if (snd_BUG_ON(snd_interval_empty(i)))
509 if (snd_interval_single(i))
512 i->openmax = i->openmin;
518 static int snd_interval_refine_last(struct snd_interval *i)
520 if (snd_BUG_ON(snd_interval_empty(i)))
522 if (snd_interval_single(i))
525 i->openmin = i->openmax;
531 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
533 if (a->empty || b->empty) {
534 snd_interval_none(c);
538 c->min = mul(a->min, b->min);
539 c->openmin = (a->openmin || b->openmin);
540 c->max = mul(a->max, b->max);
541 c->openmax = (a->openmax || b->openmax);
542 c->integer = (a->integer && b->integer);
546 * snd_interval_div - refine the interval value with division
553 * Returns non-zero if the value is changed, zero if not changed.
555 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
558 if (a->empty || b->empty) {
559 snd_interval_none(c);
563 c->min = div32(a->min, b->max, &r);
564 c->openmin = (r || a->openmin || b->openmax);
566 c->max = div32(a->max, b->min, &r);
571 c->openmax = (a->openmax || b->openmin);
580 * snd_interval_muldivk - refine the interval value
583 * @k: divisor (as integer)
588 * Returns non-zero if the value is changed, zero if not changed.
590 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
591 unsigned int k, struct snd_interval *c)
594 if (a->empty || b->empty) {
595 snd_interval_none(c);
599 c->min = muldiv32(a->min, b->min, k, &r);
600 c->openmin = (r || a->openmin || b->openmin);
601 c->max = muldiv32(a->max, b->max, k, &r);
606 c->openmax = (a->openmax || b->openmax);
611 * snd_interval_mulkdiv - refine the interval value
613 * @k: dividend 2 (as integer)
619 * Returns non-zero if the value is changed, zero if not changed.
621 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
622 const struct snd_interval *b, struct snd_interval *c)
625 if (a->empty || b->empty) {
626 snd_interval_none(c);
630 c->min = muldiv32(a->min, k, b->max, &r);
631 c->openmin = (r || a->openmin || b->openmax);
633 c->max = muldiv32(a->max, k, b->min, &r);
638 c->openmax = (a->openmax || b->openmin);
650 * snd_interval_ratnum - refine the interval value
651 * @i: interval to refine
652 * @rats_count: number of ratnum_t
653 * @rats: ratnum_t array
654 * @nump: pointer to store the resultant numerator
655 * @denp: pointer to store the resultant denominator
657 * Returns non-zero if the value is changed, zero if not changed.
659 int snd_interval_ratnum(struct snd_interval *i,
660 unsigned int rats_count, struct snd_ratnum *rats,
661 unsigned int *nump, unsigned int *denp)
663 unsigned int best_num, best_diff, best_den;
665 struct snd_interval t;
668 best_num = best_den = best_diff = 0;
669 for (k = 0; k < rats_count; ++k) {
670 unsigned int num = rats[k].num;
672 unsigned int q = i->min;
676 den = div_down(num, q);
677 if (den < rats[k].den_min)
679 if (den > rats[k].den_max)
680 den = rats[k].den_max;
683 r = (den - rats[k].den_min) % rats[k].den_step;
687 diff = num - q * den;
689 diff * best_den < best_diff * den) {
699 t.min = div_down(best_num, best_den);
700 t.openmin = !!(best_num % best_den);
702 best_num = best_den = best_diff = 0;
703 for (k = 0; k < rats_count; ++k) {
704 unsigned int num = rats[k].num;
706 unsigned int q = i->max;
712 den = div_up(num, q);
713 if (den > rats[k].den_max)
715 if (den < rats[k].den_min)
716 den = rats[k].den_min;
719 r = (den - rats[k].den_min) % rats[k].den_step;
721 den += rats[k].den_step - r;
723 diff = q * den - num;
725 diff * best_den < best_diff * den) {
735 t.max = div_up(best_num, best_den);
736 t.openmax = !!(best_num % best_den);
738 err = snd_interval_refine(i, &t);
742 if (snd_interval_single(i)) {
751 EXPORT_SYMBOL(snd_interval_ratnum);
754 * snd_interval_ratden - refine the interval value
755 * @i: interval to refine
756 * @rats_count: number of struct ratden
757 * @rats: struct ratden array
758 * @nump: pointer to store the resultant numerator
759 * @denp: pointer to store the resultant denominator
761 * Returns non-zero if the value is changed, zero if not changed.
763 static int snd_interval_ratden(struct snd_interval *i,
764 unsigned int rats_count, struct snd_ratden *rats,
765 unsigned int *nump, unsigned int *denp)
767 unsigned int best_num, best_diff, best_den;
769 struct snd_interval t;
772 best_num = best_den = best_diff = 0;
773 for (k = 0; k < rats_count; ++k) {
775 unsigned int den = rats[k].den;
776 unsigned int q = i->min;
779 if (num > rats[k].num_max)
781 if (num < rats[k].num_min)
782 num = rats[k].num_max;
785 r = (num - rats[k].num_min) % rats[k].num_step;
787 num += rats[k].num_step - r;
789 diff = num - q * den;
791 diff * best_den < best_diff * den) {
801 t.min = div_down(best_num, best_den);
802 t.openmin = !!(best_num % best_den);
804 best_num = best_den = best_diff = 0;
805 for (k = 0; k < rats_count; ++k) {
807 unsigned int den = rats[k].den;
808 unsigned int q = i->max;
811 if (num < rats[k].num_min)
813 if (num > rats[k].num_max)
814 num = rats[k].num_max;
817 r = (num - rats[k].num_min) % rats[k].num_step;
821 diff = q * den - num;
823 diff * best_den < best_diff * den) {
833 t.max = div_up(best_num, best_den);
834 t.openmax = !!(best_num % best_den);
836 err = snd_interval_refine(i, &t);
840 if (snd_interval_single(i)) {
850 * snd_interval_list - refine the interval value from the list
851 * @i: the interval value to refine
852 * @count: the number of elements in the list
853 * @list: the value list
854 * @mask: the bit-mask to evaluate
856 * Refines the interval value from the list.
857 * When mask is non-zero, only the elements corresponding to bit 1 are
860 * Returns non-zero if the value is changed, zero if not changed.
862 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
871 for (k = 0; k < count; k++) {
872 if (mask && !(mask & (1 << k)))
874 if (i->min == list[k] && !i->openmin)
876 if (i->min < list[k]) {
886 for (k = count; k-- > 0;) {
887 if (mask && !(mask & (1 << k)))
889 if (i->max == list[k] && !i->openmax)
891 if (i->max > list[k]) {
901 if (snd_interval_checkempty(i)) {
908 EXPORT_SYMBOL(snd_interval_list);
910 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
914 n = (i->min - min) % step;
915 if (n != 0 || i->openmin) {
919 n = (i->max - min) % step;
920 if (n != 0 || i->openmax) {
924 if (snd_interval_checkempty(i)) {
931 /* Info constraints helpers */
934 * snd_pcm_hw_rule_add - add the hw-constraint rule
935 * @runtime: the pcm runtime instance
936 * @cond: condition bits
937 * @var: the variable to evaluate
938 * @func: the evaluation function
939 * @private: the private data pointer passed to function
940 * @dep: the dependent variables
942 * Returns zero if successful, or a negative error code on failure.
944 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
946 snd_pcm_hw_rule_func_t func, void *private,
949 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
950 struct snd_pcm_hw_rule *c;
954 if (constrs->rules_num >= constrs->rules_all) {
955 struct snd_pcm_hw_rule *new;
956 unsigned int new_rules = constrs->rules_all + 16;
957 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
960 if (constrs->rules) {
961 memcpy(new, constrs->rules,
962 constrs->rules_num * sizeof(*c));
963 kfree(constrs->rules);
965 constrs->rules = new;
966 constrs->rules_all = new_rules;
968 c = &constrs->rules[constrs->rules_num];
972 c->private = private;
975 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
980 dep = va_arg(args, int);
982 constrs->rules_num++;
987 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
990 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
991 * @runtime: PCM runtime instance
992 * @var: hw_params variable to apply the mask
993 * @mask: the bitmap mask
995 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
997 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1000 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1001 struct snd_mask *maskp = constrs_mask(constrs, var);
1002 *maskp->bits &= mask;
1003 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1004 if (*maskp->bits == 0)
1010 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1011 * @runtime: PCM runtime instance
1012 * @var: hw_params variable to apply the mask
1013 * @mask: the 64bit bitmap mask
1015 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1017 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1020 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1021 struct snd_mask *maskp = constrs_mask(constrs, var);
1022 maskp->bits[0] &= (u_int32_t)mask;
1023 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1024 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1025 if (! maskp->bits[0] && ! maskp->bits[1])
1031 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1032 * @runtime: PCM runtime instance
1033 * @var: hw_params variable to apply the integer constraint
1035 * Apply the constraint of integer to an interval parameter.
1037 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1039 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1040 return snd_interval_setinteger(constrs_interval(constrs, var));
1043 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1046 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1047 * @runtime: PCM runtime instance
1048 * @var: hw_params variable to apply the range
1049 * @min: the minimal value
1050 * @max: the maximal value
1052 * Apply the min/max range constraint to an interval parameter.
1054 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1055 unsigned int min, unsigned int max)
1057 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1058 struct snd_interval t;
1061 t.openmin = t.openmax = 0;
1063 return snd_interval_refine(constrs_interval(constrs, var), &t);
1066 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1068 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1069 struct snd_pcm_hw_rule *rule)
1071 struct snd_pcm_hw_constraint_list *list = rule->private;
1072 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1077 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1078 * @runtime: PCM runtime instance
1079 * @cond: condition bits
1080 * @var: hw_params variable to apply the list constraint
1083 * Apply the list of constraints to an interval parameter.
1085 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1087 snd_pcm_hw_param_t var,
1088 struct snd_pcm_hw_constraint_list *l)
1090 return snd_pcm_hw_rule_add(runtime, cond, var,
1091 snd_pcm_hw_rule_list, l,
1095 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1097 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1098 struct snd_pcm_hw_rule *rule)
1100 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1101 unsigned int num = 0, den = 0;
1103 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1104 r->nrats, r->rats, &num, &den);
1105 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1106 params->rate_num = num;
1107 params->rate_den = den;
1113 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1114 * @runtime: PCM runtime instance
1115 * @cond: condition bits
1116 * @var: hw_params variable to apply the ratnums constraint
1117 * @r: struct snd_ratnums constriants
1119 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1121 snd_pcm_hw_param_t var,
1122 struct snd_pcm_hw_constraint_ratnums *r)
1124 return snd_pcm_hw_rule_add(runtime, cond, var,
1125 snd_pcm_hw_rule_ratnums, r,
1129 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1131 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1132 struct snd_pcm_hw_rule *rule)
1134 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1135 unsigned int num = 0, den = 0;
1136 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1137 r->nrats, r->rats, &num, &den);
1138 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1139 params->rate_num = num;
1140 params->rate_den = den;
1146 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1147 * @runtime: PCM runtime instance
1148 * @cond: condition bits
1149 * @var: hw_params variable to apply the ratdens constraint
1150 * @r: struct snd_ratdens constriants
1152 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1154 snd_pcm_hw_param_t var,
1155 struct snd_pcm_hw_constraint_ratdens *r)
1157 return snd_pcm_hw_rule_add(runtime, cond, var,
1158 snd_pcm_hw_rule_ratdens, r,
1162 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1164 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1165 struct snd_pcm_hw_rule *rule)
1167 unsigned int l = (unsigned long) rule->private;
1168 int width = l & 0xffff;
1169 unsigned int msbits = l >> 16;
1170 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1171 if (snd_interval_single(i) && snd_interval_value(i) == width)
1172 params->msbits = msbits;
1177 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1178 * @runtime: PCM runtime instance
1179 * @cond: condition bits
1180 * @width: sample bits width
1181 * @msbits: msbits width
1183 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1186 unsigned int msbits)
1188 unsigned long l = (msbits << 16) | width;
1189 return snd_pcm_hw_rule_add(runtime, cond, -1,
1190 snd_pcm_hw_rule_msbits,
1192 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1195 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1197 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1198 struct snd_pcm_hw_rule *rule)
1200 unsigned long step = (unsigned long) rule->private;
1201 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1205 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1206 * @runtime: PCM runtime instance
1207 * @cond: condition bits
1208 * @var: hw_params variable to apply the step constraint
1211 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1213 snd_pcm_hw_param_t var,
1216 return snd_pcm_hw_rule_add(runtime, cond, var,
1217 snd_pcm_hw_rule_step, (void *) step,
1221 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1223 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1225 static unsigned int pow2_sizes[] = {
1226 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1227 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1228 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1229 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1231 return snd_interval_list(hw_param_interval(params, rule->var),
1232 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1236 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1237 * @runtime: PCM runtime instance
1238 * @cond: condition bits
1239 * @var: hw_params variable to apply the power-of-2 constraint
1241 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1243 snd_pcm_hw_param_t var)
1245 return snd_pcm_hw_rule_add(runtime, cond, var,
1246 snd_pcm_hw_rule_pow2, NULL,
1250 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1252 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1253 snd_pcm_hw_param_t var)
1255 if (hw_is_mask(var)) {
1256 snd_mask_any(hw_param_mask(params, var));
1257 params->cmask |= 1 << var;
1258 params->rmask |= 1 << var;
1261 if (hw_is_interval(var)) {
1262 snd_interval_any(hw_param_interval(params, var));
1263 params->cmask |= 1 << var;
1264 params->rmask |= 1 << var;
1270 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1273 memset(params, 0, sizeof(*params));
1274 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1275 _snd_pcm_hw_param_any(params, k);
1276 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1277 _snd_pcm_hw_param_any(params, k);
1281 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1284 * snd_pcm_hw_param_value - return @params field @var value
1285 * @params: the hw_params instance
1286 * @var: parameter to retrieve
1287 * @dir: pointer to the direction (-1,0,1) or %NULL
1289 * Return the value for field @var if it's fixed in configuration space
1290 * defined by @params. Return -%EINVAL otherwise.
1292 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1293 snd_pcm_hw_param_t var, int *dir)
1295 if (hw_is_mask(var)) {
1296 const struct snd_mask *mask = hw_param_mask_c(params, var);
1297 if (!snd_mask_single(mask))
1301 return snd_mask_value(mask);
1303 if (hw_is_interval(var)) {
1304 const struct snd_interval *i = hw_param_interval_c(params, var);
1305 if (!snd_interval_single(i))
1309 return snd_interval_value(i);
1314 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1316 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1317 snd_pcm_hw_param_t var)
1319 if (hw_is_mask(var)) {
1320 snd_mask_none(hw_param_mask(params, var));
1321 params->cmask |= 1 << var;
1322 params->rmask |= 1 << var;
1323 } else if (hw_is_interval(var)) {
1324 snd_interval_none(hw_param_interval(params, var));
1325 params->cmask |= 1 << var;
1326 params->rmask |= 1 << var;
1332 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1334 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1335 snd_pcm_hw_param_t var)
1338 if (hw_is_mask(var))
1339 changed = snd_mask_refine_first(hw_param_mask(params, var));
1340 else if (hw_is_interval(var))
1341 changed = snd_interval_refine_first(hw_param_interval(params, var));
1345 params->cmask |= 1 << var;
1346 params->rmask |= 1 << var;
1353 * snd_pcm_hw_param_first - refine config space and return minimum value
1354 * @pcm: PCM instance
1355 * @params: the hw_params instance
1356 * @var: parameter to retrieve
1357 * @dir: pointer to the direction (-1,0,1) or %NULL
1359 * Inside configuration space defined by @params remove from @var all
1360 * values > minimum. Reduce configuration space accordingly.
1361 * Return the minimum.
1363 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1364 struct snd_pcm_hw_params *params,
1365 snd_pcm_hw_param_t var, int *dir)
1367 int changed = _snd_pcm_hw_param_first(params, var);
1370 if (params->rmask) {
1371 int err = snd_pcm_hw_refine(pcm, params);
1372 if (snd_BUG_ON(err < 0))
1375 return snd_pcm_hw_param_value(params, var, dir);
1378 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1380 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1381 snd_pcm_hw_param_t var)
1384 if (hw_is_mask(var))
1385 changed = snd_mask_refine_last(hw_param_mask(params, var));
1386 else if (hw_is_interval(var))
1387 changed = snd_interval_refine_last(hw_param_interval(params, var));
1391 params->cmask |= 1 << var;
1392 params->rmask |= 1 << var;
1399 * snd_pcm_hw_param_last - refine config space and return maximum value
1400 * @pcm: PCM instance
1401 * @params: the hw_params instance
1402 * @var: parameter to retrieve
1403 * @dir: pointer to the direction (-1,0,1) or %NULL
1405 * Inside configuration space defined by @params remove from @var all
1406 * values < maximum. Reduce configuration space accordingly.
1407 * Return the maximum.
1409 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1410 struct snd_pcm_hw_params *params,
1411 snd_pcm_hw_param_t var, int *dir)
1413 int changed = _snd_pcm_hw_param_last(params, var);
1416 if (params->rmask) {
1417 int err = snd_pcm_hw_refine(pcm, params);
1418 if (snd_BUG_ON(err < 0))
1421 return snd_pcm_hw_param_value(params, var, dir);
1424 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1427 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1428 * @pcm: PCM instance
1429 * @params: the hw_params instance
1431 * Choose one configuration from configuration space defined by @params.
1432 * The configuration chosen is that obtained fixing in this order:
1433 * first access, first format, first subformat, min channels,
1434 * min rate, min period time, max buffer size, min tick time
1436 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1437 struct snd_pcm_hw_params *params)
1439 static int vars[] = {
1440 SNDRV_PCM_HW_PARAM_ACCESS,
1441 SNDRV_PCM_HW_PARAM_FORMAT,
1442 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1443 SNDRV_PCM_HW_PARAM_CHANNELS,
1444 SNDRV_PCM_HW_PARAM_RATE,
1445 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1446 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1447 SNDRV_PCM_HW_PARAM_TICK_TIME,
1452 for (v = vars; *v != -1; v++) {
1453 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1454 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1456 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1457 if (snd_BUG_ON(err < 0))
1463 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1466 struct snd_pcm_runtime *runtime = substream->runtime;
1467 unsigned long flags;
1468 snd_pcm_stream_lock_irqsave(substream, flags);
1469 if (snd_pcm_running(substream) &&
1470 snd_pcm_update_hw_ptr(substream) >= 0)
1471 runtime->status->hw_ptr %= runtime->buffer_size;
1473 runtime->status->hw_ptr = 0;
1474 runtime->hw_ptr_jiffies = jiffies;
1475 snd_pcm_stream_unlock_irqrestore(substream, flags);
1479 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1482 struct snd_pcm_channel_info *info = arg;
1483 struct snd_pcm_runtime *runtime = substream->runtime;
1485 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1489 width = snd_pcm_format_physical_width(runtime->format);
1493 switch (runtime->access) {
1494 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1495 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1496 info->first = info->channel * width;
1497 info->step = runtime->channels * width;
1499 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1500 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1502 size_t size = runtime->dma_bytes / runtime->channels;
1503 info->first = info->channel * size * 8;
1515 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1516 * @substream: the pcm substream instance
1517 * @cmd: ioctl command
1518 * @arg: ioctl argument
1520 * Processes the generic ioctl commands for PCM.
1521 * Can be passed as the ioctl callback for PCM ops.
1523 * Returns zero if successful, or a negative error code on failure.
1525 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1526 unsigned int cmd, void *arg)
1529 case SNDRV_PCM_IOCTL1_INFO:
1531 case SNDRV_PCM_IOCTL1_RESET:
1532 return snd_pcm_lib_ioctl_reset(substream, arg);
1533 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1534 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1539 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1542 * snd_pcm_period_elapsed - update the pcm status for the next period
1543 * @substream: the pcm substream instance
1545 * This function is called from the interrupt handler when the
1546 * PCM has processed the period size. It will update the current
1547 * pointer, wake up sleepers, etc.
1549 * Even if more than one periods have elapsed since the last call, you
1550 * have to call this only once.
1552 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1554 struct snd_pcm_runtime *runtime;
1555 unsigned long flags;
1557 if (PCM_RUNTIME_CHECK(substream))
1559 runtime = substream->runtime;
1561 if (runtime->transfer_ack_begin)
1562 runtime->transfer_ack_begin(substream);
1564 snd_pcm_stream_lock_irqsave(substream, flags);
1565 if (!snd_pcm_running(substream) ||
1566 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1569 if (substream->timer_running)
1570 snd_timer_interrupt(substream->timer, 1);
1572 snd_pcm_stream_unlock_irqrestore(substream, flags);
1573 if (runtime->transfer_ack_end)
1574 runtime->transfer_ack_end(substream);
1575 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1578 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1581 * Wait until avail_min data becomes available
1582 * Returns a negative error code if any error occurs during operation.
1583 * The available space is stored on availp. When err = 0 and avail = 0
1584 * on the capture stream, it indicates the stream is in DRAINING state.
1586 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1587 snd_pcm_uframes_t *availp)
1589 struct snd_pcm_runtime *runtime = substream->runtime;
1590 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1593 snd_pcm_uframes_t avail = 0;
1596 init_waitqueue_entry(&wait, current);
1597 add_wait_queue(&runtime->sleep, &wait);
1599 if (signal_pending(current)) {
1603 set_current_state(TASK_INTERRUPTIBLE);
1604 snd_pcm_stream_unlock_irq(substream);
1605 tout = schedule_timeout(msecs_to_jiffies(10000));
1606 snd_pcm_stream_lock_irq(substream);
1607 switch (runtime->status->state) {
1608 case SNDRV_PCM_STATE_SUSPENDED:
1611 case SNDRV_PCM_STATE_XRUN:
1614 case SNDRV_PCM_STATE_DRAINING:
1618 avail = 0; /* indicate draining */
1620 case SNDRV_PCM_STATE_OPEN:
1621 case SNDRV_PCM_STATE_SETUP:
1622 case SNDRV_PCM_STATE_DISCONNECTED:
1627 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1628 is_playback ? "playback" : "capture");
1633 avail = snd_pcm_playback_avail(runtime);
1635 avail = snd_pcm_capture_avail(runtime);
1636 if (avail >= runtime->control->avail_min)
1640 remove_wait_queue(&runtime->sleep, &wait);
1645 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1647 unsigned long data, unsigned int off,
1648 snd_pcm_uframes_t frames)
1650 struct snd_pcm_runtime *runtime = substream->runtime;
1652 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1653 if (substream->ops->copy) {
1654 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1657 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1658 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1664 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1665 unsigned long data, unsigned int off,
1666 snd_pcm_uframes_t size);
1668 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1670 snd_pcm_uframes_t size,
1672 transfer_f transfer)
1674 struct snd_pcm_runtime *runtime = substream->runtime;
1675 snd_pcm_uframes_t xfer = 0;
1676 snd_pcm_uframes_t offset = 0;
1682 snd_pcm_stream_lock_irq(substream);
1683 switch (runtime->status->state) {
1684 case SNDRV_PCM_STATE_PREPARED:
1685 case SNDRV_PCM_STATE_RUNNING:
1686 case SNDRV_PCM_STATE_PAUSED:
1688 case SNDRV_PCM_STATE_XRUN:
1691 case SNDRV_PCM_STATE_SUSPENDED:
1700 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1701 snd_pcm_uframes_t avail;
1702 snd_pcm_uframes_t cont;
1703 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1704 snd_pcm_update_hw_ptr(substream);
1705 avail = snd_pcm_playback_avail(runtime);
1711 err = wait_for_avail_min(substream, &avail);
1715 frames = size > avail ? avail : size;
1716 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1719 if (snd_BUG_ON(!frames)) {
1720 snd_pcm_stream_unlock_irq(substream);
1723 appl_ptr = runtime->control->appl_ptr;
1724 appl_ofs = appl_ptr % runtime->buffer_size;
1725 snd_pcm_stream_unlock_irq(substream);
1726 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1728 snd_pcm_stream_lock_irq(substream);
1729 switch (runtime->status->state) {
1730 case SNDRV_PCM_STATE_XRUN:
1733 case SNDRV_PCM_STATE_SUSPENDED:
1740 if (appl_ptr >= runtime->boundary)
1741 appl_ptr -= runtime->boundary;
1742 runtime->control->appl_ptr = appl_ptr;
1743 if (substream->ops->ack)
1744 substream->ops->ack(substream);
1749 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1750 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1751 err = snd_pcm_start(substream);
1757 snd_pcm_stream_unlock_irq(substream);
1759 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1762 /* sanity-check for read/write methods */
1763 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1765 struct snd_pcm_runtime *runtime;
1766 if (PCM_RUNTIME_CHECK(substream))
1768 runtime = substream->runtime;
1769 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1771 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1776 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1778 struct snd_pcm_runtime *runtime;
1782 err = pcm_sanity_check(substream);
1785 runtime = substream->runtime;
1786 nonblock = !!(substream->f_flags & O_NONBLOCK);
1788 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1789 runtime->channels > 1)
1791 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1792 snd_pcm_lib_write_transfer);
1795 EXPORT_SYMBOL(snd_pcm_lib_write);
1797 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1799 unsigned long data, unsigned int off,
1800 snd_pcm_uframes_t frames)
1802 struct snd_pcm_runtime *runtime = substream->runtime;
1804 void __user **bufs = (void __user **)data;
1805 int channels = runtime->channels;
1807 if (substream->ops->copy) {
1808 if (snd_BUG_ON(!substream->ops->silence))
1810 for (c = 0; c < channels; ++c, ++bufs) {
1811 if (*bufs == NULL) {
1812 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1815 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1816 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1821 /* default transfer behaviour */
1822 size_t dma_csize = runtime->dma_bytes / channels;
1823 for (c = 0; c < channels; ++c, ++bufs) {
1824 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1825 if (*bufs == NULL) {
1826 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1828 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1829 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1837 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1839 snd_pcm_uframes_t frames)
1841 struct snd_pcm_runtime *runtime;
1845 err = pcm_sanity_check(substream);
1848 runtime = substream->runtime;
1849 nonblock = !!(substream->f_flags & O_NONBLOCK);
1851 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1853 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1854 nonblock, snd_pcm_lib_writev_transfer);
1857 EXPORT_SYMBOL(snd_pcm_lib_writev);
1859 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1861 unsigned long data, unsigned int off,
1862 snd_pcm_uframes_t frames)
1864 struct snd_pcm_runtime *runtime = substream->runtime;
1866 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1867 if (substream->ops->copy) {
1868 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1871 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1872 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1878 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1880 snd_pcm_uframes_t size,
1882 transfer_f transfer)
1884 struct snd_pcm_runtime *runtime = substream->runtime;
1885 snd_pcm_uframes_t xfer = 0;
1886 snd_pcm_uframes_t offset = 0;
1892 snd_pcm_stream_lock_irq(substream);
1893 switch (runtime->status->state) {
1894 case SNDRV_PCM_STATE_PREPARED:
1895 if (size >= runtime->start_threshold) {
1896 err = snd_pcm_start(substream);
1901 case SNDRV_PCM_STATE_DRAINING:
1902 case SNDRV_PCM_STATE_RUNNING:
1903 case SNDRV_PCM_STATE_PAUSED:
1905 case SNDRV_PCM_STATE_XRUN:
1908 case SNDRV_PCM_STATE_SUSPENDED:
1917 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1918 snd_pcm_uframes_t avail;
1919 snd_pcm_uframes_t cont;
1920 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1921 snd_pcm_update_hw_ptr(substream);
1922 avail = snd_pcm_capture_avail(runtime);
1924 if (runtime->status->state ==
1925 SNDRV_PCM_STATE_DRAINING) {
1926 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1933 err = wait_for_avail_min(substream, &avail);
1937 continue; /* draining */
1939 frames = size > avail ? avail : size;
1940 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1943 if (snd_BUG_ON(!frames)) {
1944 snd_pcm_stream_unlock_irq(substream);
1947 appl_ptr = runtime->control->appl_ptr;
1948 appl_ofs = appl_ptr % runtime->buffer_size;
1949 snd_pcm_stream_unlock_irq(substream);
1950 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1952 snd_pcm_stream_lock_irq(substream);
1953 switch (runtime->status->state) {
1954 case SNDRV_PCM_STATE_XRUN:
1957 case SNDRV_PCM_STATE_SUSPENDED:
1964 if (appl_ptr >= runtime->boundary)
1965 appl_ptr -= runtime->boundary;
1966 runtime->control->appl_ptr = appl_ptr;
1967 if (substream->ops->ack)
1968 substream->ops->ack(substream);
1975 snd_pcm_stream_unlock_irq(substream);
1977 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1980 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1982 struct snd_pcm_runtime *runtime;
1986 err = pcm_sanity_check(substream);
1989 runtime = substream->runtime;
1990 nonblock = !!(substream->f_flags & O_NONBLOCK);
1991 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1993 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1996 EXPORT_SYMBOL(snd_pcm_lib_read);
1998 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2000 unsigned long data, unsigned int off,
2001 snd_pcm_uframes_t frames)
2003 struct snd_pcm_runtime *runtime = substream->runtime;
2005 void __user **bufs = (void __user **)data;
2006 int channels = runtime->channels;
2008 if (substream->ops->copy) {
2009 for (c = 0; c < channels; ++c, ++bufs) {
2013 buf = *bufs + samples_to_bytes(runtime, off);
2014 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2018 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2019 for (c = 0; c < channels; ++c, ++bufs) {
2025 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2026 buf = *bufs + samples_to_bytes(runtime, off);
2027 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2034 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2036 snd_pcm_uframes_t frames)
2038 struct snd_pcm_runtime *runtime;
2042 err = pcm_sanity_check(substream);
2045 runtime = substream->runtime;
2046 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2049 nonblock = !!(substream->f_flags & O_NONBLOCK);
2050 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2052 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2055 EXPORT_SYMBOL(snd_pcm_lib_readv);