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 /* Skip the jiffies check for hardwares with BATCH flag.
253 * Such hardware usually just increases the position at each IRQ,
254 * thus it can't give any strange position.
256 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
257 goto no_jiffies_check;
258 hdelta = new_hw_ptr - old_hw_ptr;
259 jdelta = jiffies - runtime->hw_ptr_jiffies;
260 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
262 (((runtime->period_size * HZ) / runtime->rate)
264 hw_ptr_error(substream,
265 "hw_ptr skipping! [Q] "
266 "(pos=%ld, delta=%ld, period=%ld, "
267 "jdelta=%lu/%lu/%lu)\n",
268 (long)pos, (long)hdelta,
269 (long)runtime->period_size, jdelta,
270 ((hdelta * HZ) / runtime->rate), delta);
271 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
272 runtime->period_size * delta;
273 if (hw_ptr_interrupt >= runtime->boundary)
274 hw_ptr_interrupt -= runtime->boundary;
275 /* rebase to interrupt position */
276 hw_base = new_hw_ptr = hw_ptr_interrupt;
277 /* align hw_base to buffer_size */
278 hw_base -= hw_base % runtime->buffer_size;
282 if (delta > runtime->period_size + runtime->period_size / 2) {
283 hw_ptr_error(substream,
285 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
286 substream->stream, (long)delta,
287 (long)hw_ptr_interrupt);
288 /* rebase hw_ptr_interrupt */
290 new_hw_ptr - new_hw_ptr % runtime->period_size;
292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
293 runtime->silence_size > 0)
294 snd_pcm_playback_silence(substream, new_hw_ptr);
296 runtime->hw_ptr_base = hw_base;
297 runtime->status->hw_ptr = new_hw_ptr;
298 runtime->hw_ptr_jiffies = jiffies;
299 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
301 return snd_pcm_update_hw_ptr_post(substream, runtime);
304 /* CAUTION: call it with irq disabled */
305 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
307 struct snd_pcm_runtime *runtime = substream->runtime;
308 snd_pcm_uframes_t pos;
309 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
310 snd_pcm_sframes_t delta;
311 unsigned long jdelta;
313 old_hw_ptr = runtime->status->hw_ptr;
314 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
315 if (pos == SNDRV_PCM_POS_XRUN) {
319 hw_base = runtime->hw_ptr_base;
320 new_hw_ptr = hw_base + pos;
322 delta = new_hw_ptr - old_hw_ptr;
323 jdelta = jiffies - runtime->hw_ptr_jiffies;
325 delta += runtime->buffer_size;
327 hw_ptr_error(substream,
328 "Unexpected hw_pointer value [2] "
329 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
330 substream->stream, (long)pos,
331 (long)old_hw_ptr, jdelta);
334 hw_base += runtime->buffer_size;
335 if (hw_base >= runtime->boundary)
337 new_hw_ptr = hw_base + pos;
339 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
340 hw_ptr_error(substream,
342 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
343 (long)pos, (long)delta,
344 (long)runtime->period_size, jdelta,
345 ((delta * HZ) / runtime->rate));
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
349 runtime->silence_size > 0)
350 snd_pcm_playback_silence(substream, new_hw_ptr);
352 runtime->hw_ptr_base = hw_base;
353 runtime->status->hw_ptr = new_hw_ptr;
354 runtime->hw_ptr_jiffies = jiffies;
356 return snd_pcm_update_hw_ptr_post(substream, runtime);
360 * snd_pcm_set_ops - set the PCM operators
361 * @pcm: the pcm instance
362 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
363 * @ops: the operator table
365 * Sets the given PCM operators to the pcm instance.
367 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
369 struct snd_pcm_str *stream = &pcm->streams[direction];
370 struct snd_pcm_substream *substream;
372 for (substream = stream->substream; substream != NULL; substream = substream->next)
373 substream->ops = ops;
376 EXPORT_SYMBOL(snd_pcm_set_ops);
379 * snd_pcm_sync - set the PCM sync id
380 * @substream: the pcm substream
382 * Sets the PCM sync identifier for the card.
384 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
386 struct snd_pcm_runtime *runtime = substream->runtime;
388 runtime->sync.id32[0] = substream->pcm->card->number;
389 runtime->sync.id32[1] = -1;
390 runtime->sync.id32[2] = -1;
391 runtime->sync.id32[3] = -1;
394 EXPORT_SYMBOL(snd_pcm_set_sync);
397 * Standard ioctl routine
400 static inline unsigned int div32(unsigned int a, unsigned int b,
411 static inline unsigned int div_down(unsigned int a, unsigned int b)
418 static inline unsigned int div_up(unsigned int a, unsigned int b)
430 static inline unsigned int mul(unsigned int a, unsigned int b)
434 if (div_down(UINT_MAX, a) < b)
439 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
440 unsigned int c, unsigned int *r)
442 u_int64_t n = (u_int64_t) a * b;
457 * snd_interval_refine - refine the interval value of configurator
458 * @i: the interval value to refine
459 * @v: the interval value to refer to
461 * Refines the interval value with the reference value.
462 * The interval is changed to the range satisfying both intervals.
463 * The interval status (min, max, integer, etc.) are evaluated.
465 * Returns non-zero if the value is changed, zero if not changed.
467 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
470 if (snd_BUG_ON(snd_interval_empty(i)))
472 if (i->min < v->min) {
474 i->openmin = v->openmin;
476 } else if (i->min == v->min && !i->openmin && v->openmin) {
480 if (i->max > v->max) {
482 i->openmax = v->openmax;
484 } else if (i->max == v->max && !i->openmax && v->openmax) {
488 if (!i->integer && v->integer) {
501 } else if (!i->openmin && !i->openmax && i->min == i->max)
503 if (snd_interval_checkempty(i)) {
504 snd_interval_none(i);
510 EXPORT_SYMBOL(snd_interval_refine);
512 static int snd_interval_refine_first(struct snd_interval *i)
514 if (snd_BUG_ON(snd_interval_empty(i)))
516 if (snd_interval_single(i))
519 i->openmax = i->openmin;
525 static int snd_interval_refine_last(struct snd_interval *i)
527 if (snd_BUG_ON(snd_interval_empty(i)))
529 if (snd_interval_single(i))
532 i->openmin = i->openmax;
538 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
540 if (a->empty || b->empty) {
541 snd_interval_none(c);
545 c->min = mul(a->min, b->min);
546 c->openmin = (a->openmin || b->openmin);
547 c->max = mul(a->max, b->max);
548 c->openmax = (a->openmax || b->openmax);
549 c->integer = (a->integer && b->integer);
553 * snd_interval_div - refine the interval value with division
560 * Returns non-zero if the value is changed, zero if not changed.
562 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
565 if (a->empty || b->empty) {
566 snd_interval_none(c);
570 c->min = div32(a->min, b->max, &r);
571 c->openmin = (r || a->openmin || b->openmax);
573 c->max = div32(a->max, b->min, &r);
578 c->openmax = (a->openmax || b->openmin);
587 * snd_interval_muldivk - refine the interval value
590 * @k: divisor (as integer)
595 * Returns non-zero if the value is changed, zero if not changed.
597 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
598 unsigned int k, struct snd_interval *c)
601 if (a->empty || b->empty) {
602 snd_interval_none(c);
606 c->min = muldiv32(a->min, b->min, k, &r);
607 c->openmin = (r || a->openmin || b->openmin);
608 c->max = muldiv32(a->max, b->max, k, &r);
613 c->openmax = (a->openmax || b->openmax);
618 * snd_interval_mulkdiv - refine the interval value
620 * @k: dividend 2 (as integer)
626 * Returns non-zero if the value is changed, zero if not changed.
628 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
629 const struct snd_interval *b, struct snd_interval *c)
632 if (a->empty || b->empty) {
633 snd_interval_none(c);
637 c->min = muldiv32(a->min, k, b->max, &r);
638 c->openmin = (r || a->openmin || b->openmax);
640 c->max = muldiv32(a->max, k, b->min, &r);
645 c->openmax = (a->openmax || b->openmin);
657 * snd_interval_ratnum - refine the interval value
658 * @i: interval to refine
659 * @rats_count: number of ratnum_t
660 * @rats: ratnum_t array
661 * @nump: pointer to store the resultant numerator
662 * @denp: pointer to store the resultant denominator
664 * Returns non-zero if the value is changed, zero if not changed.
666 int snd_interval_ratnum(struct snd_interval *i,
667 unsigned int rats_count, struct snd_ratnum *rats,
668 unsigned int *nump, unsigned int *denp)
670 unsigned int best_num, best_diff, best_den;
672 struct snd_interval t;
675 best_num = best_den = best_diff = 0;
676 for (k = 0; k < rats_count; ++k) {
677 unsigned int num = rats[k].num;
679 unsigned int q = i->min;
683 den = div_down(num, q);
684 if (den < rats[k].den_min)
686 if (den > rats[k].den_max)
687 den = rats[k].den_max;
690 r = (den - rats[k].den_min) % rats[k].den_step;
694 diff = num - q * den;
696 diff * best_den < best_diff * den) {
706 t.min = div_down(best_num, best_den);
707 t.openmin = !!(best_num % best_den);
709 best_num = best_den = best_diff = 0;
710 for (k = 0; k < rats_count; ++k) {
711 unsigned int num = rats[k].num;
713 unsigned int q = i->max;
719 den = div_up(num, q);
720 if (den > rats[k].den_max)
722 if (den < rats[k].den_min)
723 den = rats[k].den_min;
726 r = (den - rats[k].den_min) % rats[k].den_step;
728 den += rats[k].den_step - r;
730 diff = q * den - num;
732 diff * best_den < best_diff * den) {
742 t.max = div_up(best_num, best_den);
743 t.openmax = !!(best_num % best_den);
745 err = snd_interval_refine(i, &t);
749 if (snd_interval_single(i)) {
758 EXPORT_SYMBOL(snd_interval_ratnum);
761 * snd_interval_ratden - refine the interval value
762 * @i: interval to refine
763 * @rats_count: number of struct ratden
764 * @rats: struct ratden array
765 * @nump: pointer to store the resultant numerator
766 * @denp: pointer to store the resultant denominator
768 * Returns non-zero if the value is changed, zero if not changed.
770 static int snd_interval_ratden(struct snd_interval *i,
771 unsigned int rats_count, struct snd_ratden *rats,
772 unsigned int *nump, unsigned int *denp)
774 unsigned int best_num, best_diff, best_den;
776 struct snd_interval t;
779 best_num = best_den = best_diff = 0;
780 for (k = 0; k < rats_count; ++k) {
782 unsigned int den = rats[k].den;
783 unsigned int q = i->min;
786 if (num > rats[k].num_max)
788 if (num < rats[k].num_min)
789 num = rats[k].num_max;
792 r = (num - rats[k].num_min) % rats[k].num_step;
794 num += rats[k].num_step - r;
796 diff = num - q * den;
798 diff * best_den < best_diff * den) {
808 t.min = div_down(best_num, best_den);
809 t.openmin = !!(best_num % best_den);
811 best_num = best_den = best_diff = 0;
812 for (k = 0; k < rats_count; ++k) {
814 unsigned int den = rats[k].den;
815 unsigned int q = i->max;
818 if (num < rats[k].num_min)
820 if (num > rats[k].num_max)
821 num = rats[k].num_max;
824 r = (num - rats[k].num_min) % rats[k].num_step;
828 diff = q * den - num;
830 diff * best_den < best_diff * den) {
840 t.max = div_up(best_num, best_den);
841 t.openmax = !!(best_num % best_den);
843 err = snd_interval_refine(i, &t);
847 if (snd_interval_single(i)) {
857 * snd_interval_list - refine the interval value from the list
858 * @i: the interval value to refine
859 * @count: the number of elements in the list
860 * @list: the value list
861 * @mask: the bit-mask to evaluate
863 * Refines the interval value from the list.
864 * When mask is non-zero, only the elements corresponding to bit 1 are
867 * Returns non-zero if the value is changed, zero if not changed.
869 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
878 for (k = 0; k < count; k++) {
879 if (mask && !(mask & (1 << k)))
881 if (i->min == list[k] && !i->openmin)
883 if (i->min < list[k]) {
893 for (k = count; k-- > 0;) {
894 if (mask && !(mask & (1 << k)))
896 if (i->max == list[k] && !i->openmax)
898 if (i->max > list[k]) {
908 if (snd_interval_checkempty(i)) {
915 EXPORT_SYMBOL(snd_interval_list);
917 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
921 n = (i->min - min) % step;
922 if (n != 0 || i->openmin) {
926 n = (i->max - min) % step;
927 if (n != 0 || i->openmax) {
931 if (snd_interval_checkempty(i)) {
938 /* Info constraints helpers */
941 * snd_pcm_hw_rule_add - add the hw-constraint rule
942 * @runtime: the pcm runtime instance
943 * @cond: condition bits
944 * @var: the variable to evaluate
945 * @func: the evaluation function
946 * @private: the private data pointer passed to function
947 * @dep: the dependent variables
949 * Returns zero if successful, or a negative error code on failure.
951 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
953 snd_pcm_hw_rule_func_t func, void *private,
956 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
957 struct snd_pcm_hw_rule *c;
961 if (constrs->rules_num >= constrs->rules_all) {
962 struct snd_pcm_hw_rule *new;
963 unsigned int new_rules = constrs->rules_all + 16;
964 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
967 if (constrs->rules) {
968 memcpy(new, constrs->rules,
969 constrs->rules_num * sizeof(*c));
970 kfree(constrs->rules);
972 constrs->rules = new;
973 constrs->rules_all = new_rules;
975 c = &constrs->rules[constrs->rules_num];
979 c->private = private;
982 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
987 dep = va_arg(args, int);
989 constrs->rules_num++;
994 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
997 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
998 * @runtime: PCM runtime instance
999 * @var: hw_params variable to apply the mask
1000 * @mask: the bitmap mask
1002 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1004 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1007 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1008 struct snd_mask *maskp = constrs_mask(constrs, var);
1009 *maskp->bits &= mask;
1010 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1011 if (*maskp->bits == 0)
1017 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1018 * @runtime: PCM runtime instance
1019 * @var: hw_params variable to apply the mask
1020 * @mask: the 64bit bitmap mask
1022 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1024 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_mask *maskp = constrs_mask(constrs, var);
1029 maskp->bits[0] &= (u_int32_t)mask;
1030 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1031 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1032 if (! maskp->bits[0] && ! maskp->bits[1])
1038 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1039 * @runtime: PCM runtime instance
1040 * @var: hw_params variable to apply the integer constraint
1042 * Apply the constraint of integer to an interval parameter.
1044 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1047 return snd_interval_setinteger(constrs_interval(constrs, var));
1050 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1053 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1054 * @runtime: PCM runtime instance
1055 * @var: hw_params variable to apply the range
1056 * @min: the minimal value
1057 * @max: the maximal value
1059 * Apply the min/max range constraint to an interval parameter.
1061 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1062 unsigned int min, unsigned int max)
1064 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1065 struct snd_interval t;
1068 t.openmin = t.openmax = 0;
1070 return snd_interval_refine(constrs_interval(constrs, var), &t);
1073 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1075 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1076 struct snd_pcm_hw_rule *rule)
1078 struct snd_pcm_hw_constraint_list *list = rule->private;
1079 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1084 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1085 * @runtime: PCM runtime instance
1086 * @cond: condition bits
1087 * @var: hw_params variable to apply the list constraint
1090 * Apply the list of constraints to an interval parameter.
1092 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1094 snd_pcm_hw_param_t var,
1095 struct snd_pcm_hw_constraint_list *l)
1097 return snd_pcm_hw_rule_add(runtime, cond, var,
1098 snd_pcm_hw_rule_list, l,
1102 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1104 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1105 struct snd_pcm_hw_rule *rule)
1107 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1108 unsigned int num = 0, den = 0;
1110 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1111 r->nrats, r->rats, &num, &den);
1112 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1113 params->rate_num = num;
1114 params->rate_den = den;
1120 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1121 * @runtime: PCM runtime instance
1122 * @cond: condition bits
1123 * @var: hw_params variable to apply the ratnums constraint
1124 * @r: struct snd_ratnums constriants
1126 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1128 snd_pcm_hw_param_t var,
1129 struct snd_pcm_hw_constraint_ratnums *r)
1131 return snd_pcm_hw_rule_add(runtime, cond, var,
1132 snd_pcm_hw_rule_ratnums, r,
1136 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1138 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1139 struct snd_pcm_hw_rule *rule)
1141 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1142 unsigned int num = 0, den = 0;
1143 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1144 r->nrats, r->rats, &num, &den);
1145 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1146 params->rate_num = num;
1147 params->rate_den = den;
1153 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the ratdens constraint
1157 * @r: struct snd_ratdens constriants
1159 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1161 snd_pcm_hw_param_t var,
1162 struct snd_pcm_hw_constraint_ratdens *r)
1164 return snd_pcm_hw_rule_add(runtime, cond, var,
1165 snd_pcm_hw_rule_ratdens, r,
1169 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1171 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1172 struct snd_pcm_hw_rule *rule)
1174 unsigned int l = (unsigned long) rule->private;
1175 int width = l & 0xffff;
1176 unsigned int msbits = l >> 16;
1177 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1178 if (snd_interval_single(i) && snd_interval_value(i) == width)
1179 params->msbits = msbits;
1184 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1185 * @runtime: PCM runtime instance
1186 * @cond: condition bits
1187 * @width: sample bits width
1188 * @msbits: msbits width
1190 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1193 unsigned int msbits)
1195 unsigned long l = (msbits << 16) | width;
1196 return snd_pcm_hw_rule_add(runtime, cond, -1,
1197 snd_pcm_hw_rule_msbits,
1199 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1202 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1204 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1205 struct snd_pcm_hw_rule *rule)
1207 unsigned long step = (unsigned long) rule->private;
1208 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1212 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the step constraint
1218 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1220 snd_pcm_hw_param_t var,
1223 return snd_pcm_hw_rule_add(runtime, cond, var,
1224 snd_pcm_hw_rule_step, (void *) step,
1228 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1230 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1232 static unsigned int pow2_sizes[] = {
1233 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1234 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1235 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1236 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1238 return snd_interval_list(hw_param_interval(params, rule->var),
1239 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1243 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1244 * @runtime: PCM runtime instance
1245 * @cond: condition bits
1246 * @var: hw_params variable to apply the power-of-2 constraint
1248 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1250 snd_pcm_hw_param_t var)
1252 return snd_pcm_hw_rule_add(runtime, cond, var,
1253 snd_pcm_hw_rule_pow2, NULL,
1257 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1259 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1260 snd_pcm_hw_param_t var)
1262 if (hw_is_mask(var)) {
1263 snd_mask_any(hw_param_mask(params, var));
1264 params->cmask |= 1 << var;
1265 params->rmask |= 1 << var;
1268 if (hw_is_interval(var)) {
1269 snd_interval_any(hw_param_interval(params, var));
1270 params->cmask |= 1 << var;
1271 params->rmask |= 1 << var;
1277 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1280 memset(params, 0, sizeof(*params));
1281 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1282 _snd_pcm_hw_param_any(params, k);
1283 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1284 _snd_pcm_hw_param_any(params, k);
1288 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1291 * snd_pcm_hw_param_value - return @params field @var value
1292 * @params: the hw_params instance
1293 * @var: parameter to retrieve
1294 * @dir: pointer to the direction (-1,0,1) or %NULL
1296 * Return the value for field @var if it's fixed in configuration space
1297 * defined by @params. Return -%EINVAL otherwise.
1299 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1300 snd_pcm_hw_param_t var, int *dir)
1302 if (hw_is_mask(var)) {
1303 const struct snd_mask *mask = hw_param_mask_c(params, var);
1304 if (!snd_mask_single(mask))
1308 return snd_mask_value(mask);
1310 if (hw_is_interval(var)) {
1311 const struct snd_interval *i = hw_param_interval_c(params, var);
1312 if (!snd_interval_single(i))
1316 return snd_interval_value(i);
1321 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1323 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1324 snd_pcm_hw_param_t var)
1326 if (hw_is_mask(var)) {
1327 snd_mask_none(hw_param_mask(params, var));
1328 params->cmask |= 1 << var;
1329 params->rmask |= 1 << var;
1330 } else if (hw_is_interval(var)) {
1331 snd_interval_none(hw_param_interval(params, var));
1332 params->cmask |= 1 << var;
1333 params->rmask |= 1 << var;
1339 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1341 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1342 snd_pcm_hw_param_t var)
1345 if (hw_is_mask(var))
1346 changed = snd_mask_refine_first(hw_param_mask(params, var));
1347 else if (hw_is_interval(var))
1348 changed = snd_interval_refine_first(hw_param_interval(params, var));
1352 params->cmask |= 1 << var;
1353 params->rmask |= 1 << var;
1360 * snd_pcm_hw_param_first - refine config space and return minimum value
1361 * @pcm: PCM instance
1362 * @params: the hw_params instance
1363 * @var: parameter to retrieve
1364 * @dir: pointer to the direction (-1,0,1) or %NULL
1366 * Inside configuration space defined by @params remove from @var all
1367 * values > minimum. Reduce configuration space accordingly.
1368 * Return the minimum.
1370 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1371 struct snd_pcm_hw_params *params,
1372 snd_pcm_hw_param_t var, int *dir)
1374 int changed = _snd_pcm_hw_param_first(params, var);
1377 if (params->rmask) {
1378 int err = snd_pcm_hw_refine(pcm, params);
1379 if (snd_BUG_ON(err < 0))
1382 return snd_pcm_hw_param_value(params, var, dir);
1385 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1387 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1388 snd_pcm_hw_param_t var)
1391 if (hw_is_mask(var))
1392 changed = snd_mask_refine_last(hw_param_mask(params, var));
1393 else if (hw_is_interval(var))
1394 changed = snd_interval_refine_last(hw_param_interval(params, var));
1398 params->cmask |= 1 << var;
1399 params->rmask |= 1 << var;
1406 * snd_pcm_hw_param_last - refine config space and return maximum value
1407 * @pcm: PCM instance
1408 * @params: the hw_params instance
1409 * @var: parameter to retrieve
1410 * @dir: pointer to the direction (-1,0,1) or %NULL
1412 * Inside configuration space defined by @params remove from @var all
1413 * values < maximum. Reduce configuration space accordingly.
1414 * Return the maximum.
1416 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1417 struct snd_pcm_hw_params *params,
1418 snd_pcm_hw_param_t var, int *dir)
1420 int changed = _snd_pcm_hw_param_last(params, var);
1423 if (params->rmask) {
1424 int err = snd_pcm_hw_refine(pcm, params);
1425 if (snd_BUG_ON(err < 0))
1428 return snd_pcm_hw_param_value(params, var, dir);
1431 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1434 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1435 * @pcm: PCM instance
1436 * @params: the hw_params instance
1438 * Choose one configuration from configuration space defined by @params.
1439 * The configuration chosen is that obtained fixing in this order:
1440 * first access, first format, first subformat, min channels,
1441 * min rate, min period time, max buffer size, min tick time
1443 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1444 struct snd_pcm_hw_params *params)
1446 static int vars[] = {
1447 SNDRV_PCM_HW_PARAM_ACCESS,
1448 SNDRV_PCM_HW_PARAM_FORMAT,
1449 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1450 SNDRV_PCM_HW_PARAM_CHANNELS,
1451 SNDRV_PCM_HW_PARAM_RATE,
1452 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1453 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1454 SNDRV_PCM_HW_PARAM_TICK_TIME,
1459 for (v = vars; *v != -1; v++) {
1460 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1461 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1463 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1464 if (snd_BUG_ON(err < 0))
1470 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1473 struct snd_pcm_runtime *runtime = substream->runtime;
1474 unsigned long flags;
1475 snd_pcm_stream_lock_irqsave(substream, flags);
1476 if (snd_pcm_running(substream) &&
1477 snd_pcm_update_hw_ptr(substream) >= 0)
1478 runtime->status->hw_ptr %= runtime->buffer_size;
1480 runtime->status->hw_ptr = 0;
1481 runtime->hw_ptr_jiffies = jiffies;
1482 snd_pcm_stream_unlock_irqrestore(substream, flags);
1486 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1489 struct snd_pcm_channel_info *info = arg;
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1492 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1496 width = snd_pcm_format_physical_width(runtime->format);
1500 switch (runtime->access) {
1501 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1502 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1503 info->first = info->channel * width;
1504 info->step = runtime->channels * width;
1506 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1507 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1509 size_t size = runtime->dma_bytes / runtime->channels;
1510 info->first = info->channel * size * 8;
1522 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1523 * @substream: the pcm substream instance
1524 * @cmd: ioctl command
1525 * @arg: ioctl argument
1527 * Processes the generic ioctl commands for PCM.
1528 * Can be passed as the ioctl callback for PCM ops.
1530 * Returns zero if successful, or a negative error code on failure.
1532 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1533 unsigned int cmd, void *arg)
1536 case SNDRV_PCM_IOCTL1_INFO:
1538 case SNDRV_PCM_IOCTL1_RESET:
1539 return snd_pcm_lib_ioctl_reset(substream, arg);
1540 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1541 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1546 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1549 * snd_pcm_period_elapsed - update the pcm status for the next period
1550 * @substream: the pcm substream instance
1552 * This function is called from the interrupt handler when the
1553 * PCM has processed the period size. It will update the current
1554 * pointer, wake up sleepers, etc.
1556 * Even if more than one periods have elapsed since the last call, you
1557 * have to call this only once.
1559 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1561 struct snd_pcm_runtime *runtime;
1562 unsigned long flags;
1564 if (PCM_RUNTIME_CHECK(substream))
1566 runtime = substream->runtime;
1568 if (runtime->transfer_ack_begin)
1569 runtime->transfer_ack_begin(substream);
1571 snd_pcm_stream_lock_irqsave(substream, flags);
1572 if (!snd_pcm_running(substream) ||
1573 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1576 if (substream->timer_running)
1577 snd_timer_interrupt(substream->timer, 1);
1579 snd_pcm_stream_unlock_irqrestore(substream, flags);
1580 if (runtime->transfer_ack_end)
1581 runtime->transfer_ack_end(substream);
1582 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1585 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1588 * Wait until avail_min data becomes available
1589 * Returns a negative error code if any error occurs during operation.
1590 * The available space is stored on availp. When err = 0 and avail = 0
1591 * on the capture stream, it indicates the stream is in DRAINING state.
1593 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1594 snd_pcm_uframes_t *availp)
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1600 snd_pcm_uframes_t avail = 0;
1603 init_waitqueue_entry(&wait, current);
1604 add_wait_queue(&runtime->sleep, &wait);
1606 if (signal_pending(current)) {
1610 set_current_state(TASK_INTERRUPTIBLE);
1611 snd_pcm_stream_unlock_irq(substream);
1612 tout = schedule_timeout(msecs_to_jiffies(10000));
1613 snd_pcm_stream_lock_irq(substream);
1614 switch (runtime->status->state) {
1615 case SNDRV_PCM_STATE_SUSPENDED:
1618 case SNDRV_PCM_STATE_XRUN:
1621 case SNDRV_PCM_STATE_DRAINING:
1625 avail = 0; /* indicate draining */
1627 case SNDRV_PCM_STATE_OPEN:
1628 case SNDRV_PCM_STATE_SETUP:
1629 case SNDRV_PCM_STATE_DISCONNECTED:
1634 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1635 is_playback ? "playback" : "capture");
1640 avail = snd_pcm_playback_avail(runtime);
1642 avail = snd_pcm_capture_avail(runtime);
1643 if (avail >= runtime->control->avail_min)
1647 remove_wait_queue(&runtime->sleep, &wait);
1652 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1654 unsigned long data, unsigned int off,
1655 snd_pcm_uframes_t frames)
1657 struct snd_pcm_runtime *runtime = substream->runtime;
1659 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1660 if (substream->ops->copy) {
1661 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1664 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1665 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1671 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1672 unsigned long data, unsigned int off,
1673 snd_pcm_uframes_t size);
1675 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1677 snd_pcm_uframes_t size,
1679 transfer_f transfer)
1681 struct snd_pcm_runtime *runtime = substream->runtime;
1682 snd_pcm_uframes_t xfer = 0;
1683 snd_pcm_uframes_t offset = 0;
1689 snd_pcm_stream_lock_irq(substream);
1690 switch (runtime->status->state) {
1691 case SNDRV_PCM_STATE_PREPARED:
1692 case SNDRV_PCM_STATE_RUNNING:
1693 case SNDRV_PCM_STATE_PAUSED:
1695 case SNDRV_PCM_STATE_XRUN:
1698 case SNDRV_PCM_STATE_SUSPENDED:
1707 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1708 snd_pcm_uframes_t avail;
1709 snd_pcm_uframes_t cont;
1710 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1711 snd_pcm_update_hw_ptr(substream);
1712 avail = snd_pcm_playback_avail(runtime);
1718 err = wait_for_avail_min(substream, &avail);
1722 frames = size > avail ? avail : size;
1723 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1726 if (snd_BUG_ON(!frames)) {
1727 snd_pcm_stream_unlock_irq(substream);
1730 appl_ptr = runtime->control->appl_ptr;
1731 appl_ofs = appl_ptr % runtime->buffer_size;
1732 snd_pcm_stream_unlock_irq(substream);
1733 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1735 snd_pcm_stream_lock_irq(substream);
1736 switch (runtime->status->state) {
1737 case SNDRV_PCM_STATE_XRUN:
1740 case SNDRV_PCM_STATE_SUSPENDED:
1747 if (appl_ptr >= runtime->boundary)
1748 appl_ptr -= runtime->boundary;
1749 runtime->control->appl_ptr = appl_ptr;
1750 if (substream->ops->ack)
1751 substream->ops->ack(substream);
1756 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1757 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1758 err = snd_pcm_start(substream);
1764 snd_pcm_stream_unlock_irq(substream);
1766 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1769 /* sanity-check for read/write methods */
1770 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1772 struct snd_pcm_runtime *runtime;
1773 if (PCM_RUNTIME_CHECK(substream))
1775 runtime = substream->runtime;
1776 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1778 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1783 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1785 struct snd_pcm_runtime *runtime;
1789 err = pcm_sanity_check(substream);
1792 runtime = substream->runtime;
1793 nonblock = !!(substream->f_flags & O_NONBLOCK);
1795 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1796 runtime->channels > 1)
1798 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1799 snd_pcm_lib_write_transfer);
1802 EXPORT_SYMBOL(snd_pcm_lib_write);
1804 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1806 unsigned long data, unsigned int off,
1807 snd_pcm_uframes_t frames)
1809 struct snd_pcm_runtime *runtime = substream->runtime;
1811 void __user **bufs = (void __user **)data;
1812 int channels = runtime->channels;
1814 if (substream->ops->copy) {
1815 if (snd_BUG_ON(!substream->ops->silence))
1817 for (c = 0; c < channels; ++c, ++bufs) {
1818 if (*bufs == NULL) {
1819 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1822 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1823 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1828 /* default transfer behaviour */
1829 size_t dma_csize = runtime->dma_bytes / channels;
1830 for (c = 0; c < channels; ++c, ++bufs) {
1831 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1832 if (*bufs == NULL) {
1833 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1835 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1836 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1844 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1846 snd_pcm_uframes_t frames)
1848 struct snd_pcm_runtime *runtime;
1852 err = pcm_sanity_check(substream);
1855 runtime = substream->runtime;
1856 nonblock = !!(substream->f_flags & O_NONBLOCK);
1858 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1860 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1861 nonblock, snd_pcm_lib_writev_transfer);
1864 EXPORT_SYMBOL(snd_pcm_lib_writev);
1866 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1868 unsigned long data, unsigned int off,
1869 snd_pcm_uframes_t frames)
1871 struct snd_pcm_runtime *runtime = substream->runtime;
1873 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1874 if (substream->ops->copy) {
1875 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1878 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1879 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1885 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1887 snd_pcm_uframes_t size,
1889 transfer_f transfer)
1891 struct snd_pcm_runtime *runtime = substream->runtime;
1892 snd_pcm_uframes_t xfer = 0;
1893 snd_pcm_uframes_t offset = 0;
1899 snd_pcm_stream_lock_irq(substream);
1900 switch (runtime->status->state) {
1901 case SNDRV_PCM_STATE_PREPARED:
1902 if (size >= runtime->start_threshold) {
1903 err = snd_pcm_start(substream);
1908 case SNDRV_PCM_STATE_DRAINING:
1909 case SNDRV_PCM_STATE_RUNNING:
1910 case SNDRV_PCM_STATE_PAUSED:
1912 case SNDRV_PCM_STATE_XRUN:
1915 case SNDRV_PCM_STATE_SUSPENDED:
1924 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1925 snd_pcm_uframes_t avail;
1926 snd_pcm_uframes_t cont;
1927 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1928 snd_pcm_update_hw_ptr(substream);
1929 avail = snd_pcm_capture_avail(runtime);
1931 if (runtime->status->state ==
1932 SNDRV_PCM_STATE_DRAINING) {
1933 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1940 err = wait_for_avail_min(substream, &avail);
1944 continue; /* draining */
1946 frames = size > avail ? avail : size;
1947 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1950 if (snd_BUG_ON(!frames)) {
1951 snd_pcm_stream_unlock_irq(substream);
1954 appl_ptr = runtime->control->appl_ptr;
1955 appl_ofs = appl_ptr % runtime->buffer_size;
1956 snd_pcm_stream_unlock_irq(substream);
1957 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1959 snd_pcm_stream_lock_irq(substream);
1960 switch (runtime->status->state) {
1961 case SNDRV_PCM_STATE_XRUN:
1964 case SNDRV_PCM_STATE_SUSPENDED:
1971 if (appl_ptr >= runtime->boundary)
1972 appl_ptr -= runtime->boundary;
1973 runtime->control->appl_ptr = appl_ptr;
1974 if (substream->ops->ack)
1975 substream->ops->ack(substream);
1982 snd_pcm_stream_unlock_irq(substream);
1984 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1987 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1989 struct snd_pcm_runtime *runtime;
1993 err = pcm_sanity_check(substream);
1996 runtime = substream->runtime;
1997 nonblock = !!(substream->f_flags & O_NONBLOCK);
1998 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2000 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2003 EXPORT_SYMBOL(snd_pcm_lib_read);
2005 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2007 unsigned long data, unsigned int off,
2008 snd_pcm_uframes_t frames)
2010 struct snd_pcm_runtime *runtime = substream->runtime;
2012 void __user **bufs = (void __user **)data;
2013 int channels = runtime->channels;
2015 if (substream->ops->copy) {
2016 for (c = 0; c < channels; ++c, ++bufs) {
2020 buf = *bufs + samples_to_bytes(runtime, off);
2021 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2025 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2026 for (c = 0; c < channels; ++c, ++bufs) {
2032 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2033 buf = *bufs + samples_to_bytes(runtime, off);
2034 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2041 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2043 snd_pcm_uframes_t frames)
2045 struct snd_pcm_runtime *runtime;
2049 err = pcm_sanity_check(substream);
2052 runtime = substream->runtime;
2053 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2056 nonblock = !!(substream->f_flags & O_NONBLOCK);
2057 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2059 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2062 EXPORT_SYMBOL(snd_pcm_lib_readv);