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, mask) ((substream)->pstr->xrun_debug & (mask))
131 #define xrun_debug(substream, mask) 0
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream, 2)) \
139 static void pcm_debug_name(struct snd_pcm_substream *substream,
140 char *name, size_t len)
142 snprintf(name, len, "pcmC%dD%d%c:%d",
143 substream->pcm->card->number,
144 substream->pcm->device,
145 substream->stream ? 'c' : 'p',
149 static void xrun(struct snd_pcm_substream *substream)
151 struct snd_pcm_runtime *runtime = substream->runtime;
153 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
154 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
155 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
156 if (xrun_debug(substream, 1)) {
158 pcm_debug_name(substream, name, sizeof(name));
159 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
160 dump_stack_on_xrun(substream);
164 static snd_pcm_uframes_t
165 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
166 struct snd_pcm_runtime *runtime)
168 snd_pcm_uframes_t pos;
170 pos = substream->ops->pointer(substream);
171 if (pos == SNDRV_PCM_POS_XRUN)
172 return pos; /* XRUN */
173 if (pos >= runtime->buffer_size) {
174 if (printk_ratelimit()) {
176 pcm_debug_name(substream, name, sizeof(name));
177 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
178 "buffer size = 0x%lx, period size = 0x%lx\n",
179 name, pos, runtime->buffer_size,
180 runtime->period_size);
184 pos -= pos % runtime->min_align;
188 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
189 struct snd_pcm_runtime *runtime)
191 snd_pcm_uframes_t avail;
193 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
194 avail = snd_pcm_playback_avail(runtime);
196 avail = snd_pcm_capture_avail(runtime);
197 if (avail > runtime->avail_max)
198 runtime->avail_max = avail;
199 if (avail >= runtime->stop_threshold) {
200 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
201 snd_pcm_drain_done(substream);
206 if (avail >= runtime->control->avail_min)
207 wake_up(&runtime->sleep);
211 #define hw_ptr_error(substream, fmt, args...) \
213 if (xrun_debug(substream, 1)) { \
214 if (printk_ratelimit()) { \
215 snd_printd("PCM: " fmt, ##args); \
217 dump_stack_on_xrun(substream); \
221 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
223 struct snd_pcm_runtime *runtime = substream->runtime;
224 snd_pcm_uframes_t pos;
225 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
226 snd_pcm_sframes_t hdelta, delta;
227 unsigned long jdelta;
229 old_hw_ptr = runtime->status->hw_ptr;
230 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
231 if (pos == SNDRV_PCM_POS_XRUN) {
235 hw_base = runtime->hw_ptr_base;
236 new_hw_ptr = hw_base + pos;
237 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
238 delta = new_hw_ptr - hw_ptr_interrupt;
239 if (hw_ptr_interrupt >= runtime->boundary) {
240 hw_ptr_interrupt -= runtime->boundary;
241 if (hw_base < runtime->boundary / 2)
242 /* hw_base was already lapped; recalc delta */
243 delta = new_hw_ptr - hw_ptr_interrupt;
246 delta += runtime->buffer_size;
248 hw_ptr_error(substream,
249 "Unexpected hw_pointer value "
250 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
251 substream->stream, (long)pos,
252 (long)hw_ptr_interrupt);
253 /* rebase to interrupt position */
254 hw_base = new_hw_ptr = hw_ptr_interrupt;
255 /* align hw_base to buffer_size */
256 hw_base -= hw_base % runtime->buffer_size;
259 hw_base += runtime->buffer_size;
260 if (hw_base >= runtime->boundary)
262 new_hw_ptr = hw_base + pos;
266 /* Do jiffies check only in xrun_debug mode */
267 if (!xrun_debug(substream, 4))
268 goto no_jiffies_check;
270 /* Skip the jiffies check for hardwares with BATCH flag.
271 * Such hardware usually just increases the position at each IRQ,
272 * thus it can't give any strange position.
274 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
275 goto no_jiffies_check;
276 hdelta = new_hw_ptr - old_hw_ptr;
277 if (hdelta < runtime->delay)
278 goto no_jiffies_check;
279 hdelta -= runtime->delay;
280 jdelta = jiffies - runtime->hw_ptr_jiffies;
281 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
283 (((runtime->period_size * HZ) / runtime->rate)
285 hw_ptr_error(substream,
286 "hw_ptr skipping! [Q] "
287 "(pos=%ld, delta=%ld, period=%ld, "
288 "jdelta=%lu/%lu/%lu)\n",
289 (long)pos, (long)hdelta,
290 (long)runtime->period_size, jdelta,
291 ((hdelta * HZ) / runtime->rate), delta);
292 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
293 runtime->period_size * delta;
294 if (hw_ptr_interrupt >= runtime->boundary)
295 hw_ptr_interrupt -= runtime->boundary;
296 /* rebase to interrupt position */
297 hw_base = new_hw_ptr = hw_ptr_interrupt;
298 /* align hw_base to buffer_size */
299 hw_base -= hw_base % runtime->buffer_size;
303 if (delta > runtime->period_size + runtime->period_size / 2) {
304 hw_ptr_error(substream,
306 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
307 substream->stream, (long)delta,
308 (long)hw_ptr_interrupt);
309 /* rebase hw_ptr_interrupt */
311 new_hw_ptr - new_hw_ptr % runtime->period_size;
313 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
316 runtime->silence_size > 0)
317 snd_pcm_playback_silence(substream, new_hw_ptr);
319 if (runtime->status->hw_ptr == new_hw_ptr)
322 runtime->hw_ptr_base = hw_base;
323 runtime->status->hw_ptr = new_hw_ptr;
324 runtime->hw_ptr_jiffies = jiffies;
325 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
326 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
328 return snd_pcm_update_hw_ptr_post(substream, runtime);
331 /* CAUTION: call it with irq disabled */
332 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
334 struct snd_pcm_runtime *runtime = substream->runtime;
335 snd_pcm_uframes_t pos;
336 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
337 snd_pcm_sframes_t delta;
338 unsigned long jdelta;
340 old_hw_ptr = runtime->status->hw_ptr;
341 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
342 if (pos == SNDRV_PCM_POS_XRUN) {
346 hw_base = runtime->hw_ptr_base;
347 new_hw_ptr = hw_base + pos;
349 delta = new_hw_ptr - old_hw_ptr;
350 jdelta = jiffies - runtime->hw_ptr_jiffies;
352 delta += runtime->buffer_size;
354 hw_ptr_error(substream,
355 "Unexpected hw_pointer value [2] "
356 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
357 substream->stream, (long)pos,
358 (long)old_hw_ptr, jdelta);
361 hw_base += runtime->buffer_size;
362 if (hw_base >= runtime->boundary)
364 new_hw_ptr = hw_base + pos;
366 /* Do jiffies check only in xrun_debug mode */
367 if (!xrun_debug(substream, 4))
368 goto no_jiffies_check;
369 if (delta < runtime->delay)
370 goto no_jiffies_check;
371 delta -= runtime->delay;
372 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
373 hw_ptr_error(substream,
375 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
376 (long)pos, (long)delta,
377 (long)runtime->period_size, jdelta,
378 ((delta * HZ) / runtime->rate));
382 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
383 runtime->silence_size > 0)
384 snd_pcm_playback_silence(substream, new_hw_ptr);
386 if (runtime->status->hw_ptr == new_hw_ptr)
389 runtime->hw_ptr_base = hw_base;
390 runtime->status->hw_ptr = new_hw_ptr;
391 runtime->hw_ptr_jiffies = jiffies;
392 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
393 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
395 return snd_pcm_update_hw_ptr_post(substream, runtime);
399 * snd_pcm_set_ops - set the PCM operators
400 * @pcm: the pcm instance
401 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
402 * @ops: the operator table
404 * Sets the given PCM operators to the pcm instance.
406 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
408 struct snd_pcm_str *stream = &pcm->streams[direction];
409 struct snd_pcm_substream *substream;
411 for (substream = stream->substream; substream != NULL; substream = substream->next)
412 substream->ops = ops;
415 EXPORT_SYMBOL(snd_pcm_set_ops);
418 * snd_pcm_sync - set the PCM sync id
419 * @substream: the pcm substream
421 * Sets the PCM sync identifier for the card.
423 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
425 struct snd_pcm_runtime *runtime = substream->runtime;
427 runtime->sync.id32[0] = substream->pcm->card->number;
428 runtime->sync.id32[1] = -1;
429 runtime->sync.id32[2] = -1;
430 runtime->sync.id32[3] = -1;
433 EXPORT_SYMBOL(snd_pcm_set_sync);
436 * Standard ioctl routine
439 static inline unsigned int div32(unsigned int a, unsigned int b,
450 static inline unsigned int div_down(unsigned int a, unsigned int b)
457 static inline unsigned int div_up(unsigned int a, unsigned int b)
469 static inline unsigned int mul(unsigned int a, unsigned int b)
473 if (div_down(UINT_MAX, a) < b)
478 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
479 unsigned int c, unsigned int *r)
481 u_int64_t n = (u_int64_t) a * b;
496 * snd_interval_refine - refine the interval value of configurator
497 * @i: the interval value to refine
498 * @v: the interval value to refer to
500 * Refines the interval value with the reference value.
501 * The interval is changed to the range satisfying both intervals.
502 * The interval status (min, max, integer, etc.) are evaluated.
504 * Returns non-zero if the value is changed, zero if not changed.
506 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
509 if (snd_BUG_ON(snd_interval_empty(i)))
511 if (i->min < v->min) {
513 i->openmin = v->openmin;
515 } else if (i->min == v->min && !i->openmin && v->openmin) {
519 if (i->max > v->max) {
521 i->openmax = v->openmax;
523 } else if (i->max == v->max && !i->openmax && v->openmax) {
527 if (!i->integer && v->integer) {
540 } else if (!i->openmin && !i->openmax && i->min == i->max)
542 if (snd_interval_checkempty(i)) {
543 snd_interval_none(i);
549 EXPORT_SYMBOL(snd_interval_refine);
551 static int snd_interval_refine_first(struct snd_interval *i)
553 if (snd_BUG_ON(snd_interval_empty(i)))
555 if (snd_interval_single(i))
558 i->openmax = i->openmin;
564 static int snd_interval_refine_last(struct snd_interval *i)
566 if (snd_BUG_ON(snd_interval_empty(i)))
568 if (snd_interval_single(i))
571 i->openmin = i->openmax;
577 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
579 if (a->empty || b->empty) {
580 snd_interval_none(c);
584 c->min = mul(a->min, b->min);
585 c->openmin = (a->openmin || b->openmin);
586 c->max = mul(a->max, b->max);
587 c->openmax = (a->openmax || b->openmax);
588 c->integer = (a->integer && b->integer);
592 * snd_interval_div - refine the interval value with division
599 * Returns non-zero if the value is changed, zero if not changed.
601 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
604 if (a->empty || b->empty) {
605 snd_interval_none(c);
609 c->min = div32(a->min, b->max, &r);
610 c->openmin = (r || a->openmin || b->openmax);
612 c->max = div32(a->max, b->min, &r);
617 c->openmax = (a->openmax || b->openmin);
626 * snd_interval_muldivk - refine the interval value
629 * @k: divisor (as integer)
634 * Returns non-zero if the value is changed, zero if not changed.
636 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
637 unsigned int k, struct snd_interval *c)
640 if (a->empty || b->empty) {
641 snd_interval_none(c);
645 c->min = muldiv32(a->min, b->min, k, &r);
646 c->openmin = (r || a->openmin || b->openmin);
647 c->max = muldiv32(a->max, b->max, k, &r);
652 c->openmax = (a->openmax || b->openmax);
657 * snd_interval_mulkdiv - refine the interval value
659 * @k: dividend 2 (as integer)
665 * Returns non-zero if the value is changed, zero if not changed.
667 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
668 const struct snd_interval *b, struct snd_interval *c)
671 if (a->empty || b->empty) {
672 snd_interval_none(c);
676 c->min = muldiv32(a->min, k, b->max, &r);
677 c->openmin = (r || a->openmin || b->openmax);
679 c->max = muldiv32(a->max, k, b->min, &r);
684 c->openmax = (a->openmax || b->openmin);
696 * snd_interval_ratnum - refine the interval value
697 * @i: interval to refine
698 * @rats_count: number of ratnum_t
699 * @rats: ratnum_t array
700 * @nump: pointer to store the resultant numerator
701 * @denp: pointer to store the resultant denominator
703 * Returns non-zero if the value is changed, zero if not changed.
705 int snd_interval_ratnum(struct snd_interval *i,
706 unsigned int rats_count, struct snd_ratnum *rats,
707 unsigned int *nump, unsigned int *denp)
709 unsigned int best_num, best_diff, best_den;
711 struct snd_interval t;
714 best_num = best_den = best_diff = 0;
715 for (k = 0; k < rats_count; ++k) {
716 unsigned int num = rats[k].num;
718 unsigned int q = i->min;
722 den = div_down(num, q);
723 if (den < rats[k].den_min)
725 if (den > rats[k].den_max)
726 den = rats[k].den_max;
729 r = (den - rats[k].den_min) % rats[k].den_step;
733 diff = num - q * den;
735 diff * best_den < best_diff * den) {
745 t.min = div_down(best_num, best_den);
746 t.openmin = !!(best_num % best_den);
748 best_num = best_den = best_diff = 0;
749 for (k = 0; k < rats_count; ++k) {
750 unsigned int num = rats[k].num;
752 unsigned int q = i->max;
758 den = div_up(num, q);
759 if (den > rats[k].den_max)
761 if (den < rats[k].den_min)
762 den = rats[k].den_min;
765 r = (den - rats[k].den_min) % rats[k].den_step;
767 den += rats[k].den_step - r;
769 diff = q * den - num;
771 diff * best_den < best_diff * den) {
781 t.max = div_up(best_num, best_den);
782 t.openmax = !!(best_num % best_den);
784 err = snd_interval_refine(i, &t);
788 if (snd_interval_single(i)) {
797 EXPORT_SYMBOL(snd_interval_ratnum);
800 * snd_interval_ratden - refine the interval value
801 * @i: interval to refine
802 * @rats_count: number of struct ratden
803 * @rats: struct ratden array
804 * @nump: pointer to store the resultant numerator
805 * @denp: pointer to store the resultant denominator
807 * Returns non-zero if the value is changed, zero if not changed.
809 static int snd_interval_ratden(struct snd_interval *i,
810 unsigned int rats_count, struct snd_ratden *rats,
811 unsigned int *nump, unsigned int *denp)
813 unsigned int best_num, best_diff, best_den;
815 struct snd_interval t;
818 best_num = best_den = best_diff = 0;
819 for (k = 0; k < rats_count; ++k) {
821 unsigned int den = rats[k].den;
822 unsigned int q = i->min;
825 if (num > rats[k].num_max)
827 if (num < rats[k].num_min)
828 num = rats[k].num_max;
831 r = (num - rats[k].num_min) % rats[k].num_step;
833 num += rats[k].num_step - r;
835 diff = num - q * den;
837 diff * best_den < best_diff * den) {
847 t.min = div_down(best_num, best_den);
848 t.openmin = !!(best_num % best_den);
850 best_num = best_den = best_diff = 0;
851 for (k = 0; k < rats_count; ++k) {
853 unsigned int den = rats[k].den;
854 unsigned int q = i->max;
857 if (num < rats[k].num_min)
859 if (num > rats[k].num_max)
860 num = rats[k].num_max;
863 r = (num - rats[k].num_min) % rats[k].num_step;
867 diff = q * den - num;
869 diff * best_den < best_diff * den) {
879 t.max = div_up(best_num, best_den);
880 t.openmax = !!(best_num % best_den);
882 err = snd_interval_refine(i, &t);
886 if (snd_interval_single(i)) {
896 * snd_interval_list - refine the interval value from the list
897 * @i: the interval value to refine
898 * @count: the number of elements in the list
899 * @list: the value list
900 * @mask: the bit-mask to evaluate
902 * Refines the interval value from the list.
903 * When mask is non-zero, only the elements corresponding to bit 1 are
906 * Returns non-zero if the value is changed, zero if not changed.
908 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
917 for (k = 0; k < count; k++) {
918 if (mask && !(mask & (1 << k)))
920 if (i->min == list[k] && !i->openmin)
922 if (i->min < list[k]) {
932 for (k = count; k-- > 0;) {
933 if (mask && !(mask & (1 << k)))
935 if (i->max == list[k] && !i->openmax)
937 if (i->max > list[k]) {
947 if (snd_interval_checkempty(i)) {
954 EXPORT_SYMBOL(snd_interval_list);
956 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
960 n = (i->min - min) % step;
961 if (n != 0 || i->openmin) {
965 n = (i->max - min) % step;
966 if (n != 0 || i->openmax) {
970 if (snd_interval_checkempty(i)) {
977 /* Info constraints helpers */
980 * snd_pcm_hw_rule_add - add the hw-constraint rule
981 * @runtime: the pcm runtime instance
982 * @cond: condition bits
983 * @var: the variable to evaluate
984 * @func: the evaluation function
985 * @private: the private data pointer passed to function
986 * @dep: the dependent variables
988 * Returns zero if successful, or a negative error code on failure.
990 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
992 snd_pcm_hw_rule_func_t func, void *private,
995 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
996 struct snd_pcm_hw_rule *c;
1000 if (constrs->rules_num >= constrs->rules_all) {
1001 struct snd_pcm_hw_rule *new;
1002 unsigned int new_rules = constrs->rules_all + 16;
1003 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1006 if (constrs->rules) {
1007 memcpy(new, constrs->rules,
1008 constrs->rules_num * sizeof(*c));
1009 kfree(constrs->rules);
1011 constrs->rules = new;
1012 constrs->rules_all = new_rules;
1014 c = &constrs->rules[constrs->rules_num];
1018 c->private = private;
1021 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1026 dep = va_arg(args, int);
1028 constrs->rules_num++;
1033 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1036 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1037 * @runtime: PCM runtime instance
1038 * @var: hw_params variable to apply the mask
1039 * @mask: the bitmap mask
1041 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1043 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1047 struct snd_mask *maskp = constrs_mask(constrs, var);
1048 *maskp->bits &= mask;
1049 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1050 if (*maskp->bits == 0)
1056 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1057 * @runtime: PCM runtime instance
1058 * @var: hw_params variable to apply the mask
1059 * @mask: the 64bit bitmap mask
1061 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1063 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1066 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1067 struct snd_mask *maskp = constrs_mask(constrs, var);
1068 maskp->bits[0] &= (u_int32_t)mask;
1069 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1070 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1071 if (! maskp->bits[0] && ! maskp->bits[1])
1077 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1078 * @runtime: PCM runtime instance
1079 * @var: hw_params variable to apply the integer constraint
1081 * Apply the constraint of integer to an interval parameter.
1083 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1085 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1086 return snd_interval_setinteger(constrs_interval(constrs, var));
1089 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1092 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1093 * @runtime: PCM runtime instance
1094 * @var: hw_params variable to apply the range
1095 * @min: the minimal value
1096 * @max: the maximal value
1098 * Apply the min/max range constraint to an interval parameter.
1100 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1101 unsigned int min, unsigned int max)
1103 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1104 struct snd_interval t;
1107 t.openmin = t.openmax = 0;
1109 return snd_interval_refine(constrs_interval(constrs, var), &t);
1112 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1114 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1115 struct snd_pcm_hw_rule *rule)
1117 struct snd_pcm_hw_constraint_list *list = rule->private;
1118 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1123 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1124 * @runtime: PCM runtime instance
1125 * @cond: condition bits
1126 * @var: hw_params variable to apply the list constraint
1129 * Apply the list of constraints to an interval parameter.
1131 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1133 snd_pcm_hw_param_t var,
1134 struct snd_pcm_hw_constraint_list *l)
1136 return snd_pcm_hw_rule_add(runtime, cond, var,
1137 snd_pcm_hw_rule_list, l,
1141 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1143 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1144 struct snd_pcm_hw_rule *rule)
1146 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1147 unsigned int num = 0, den = 0;
1149 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1150 r->nrats, r->rats, &num, &den);
1151 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1152 params->rate_num = num;
1153 params->rate_den = den;
1159 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1160 * @runtime: PCM runtime instance
1161 * @cond: condition bits
1162 * @var: hw_params variable to apply the ratnums constraint
1163 * @r: struct snd_ratnums constriants
1165 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1167 snd_pcm_hw_param_t var,
1168 struct snd_pcm_hw_constraint_ratnums *r)
1170 return snd_pcm_hw_rule_add(runtime, cond, var,
1171 snd_pcm_hw_rule_ratnums, r,
1175 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1177 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1178 struct snd_pcm_hw_rule *rule)
1180 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1181 unsigned int num = 0, den = 0;
1182 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1183 r->nrats, r->rats, &num, &den);
1184 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1185 params->rate_num = num;
1186 params->rate_den = den;
1192 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1193 * @runtime: PCM runtime instance
1194 * @cond: condition bits
1195 * @var: hw_params variable to apply the ratdens constraint
1196 * @r: struct snd_ratdens constriants
1198 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1200 snd_pcm_hw_param_t var,
1201 struct snd_pcm_hw_constraint_ratdens *r)
1203 return snd_pcm_hw_rule_add(runtime, cond, var,
1204 snd_pcm_hw_rule_ratdens, r,
1208 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1210 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1211 struct snd_pcm_hw_rule *rule)
1213 unsigned int l = (unsigned long) rule->private;
1214 int width = l & 0xffff;
1215 unsigned int msbits = l >> 16;
1216 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1217 if (snd_interval_single(i) && snd_interval_value(i) == width)
1218 params->msbits = msbits;
1223 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1224 * @runtime: PCM runtime instance
1225 * @cond: condition bits
1226 * @width: sample bits width
1227 * @msbits: msbits width
1229 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1232 unsigned int msbits)
1234 unsigned long l = (msbits << 16) | width;
1235 return snd_pcm_hw_rule_add(runtime, cond, -1,
1236 snd_pcm_hw_rule_msbits,
1238 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1241 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1243 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1244 struct snd_pcm_hw_rule *rule)
1246 unsigned long step = (unsigned long) rule->private;
1247 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1251 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1252 * @runtime: PCM runtime instance
1253 * @cond: condition bits
1254 * @var: hw_params variable to apply the step constraint
1257 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1259 snd_pcm_hw_param_t var,
1262 return snd_pcm_hw_rule_add(runtime, cond, var,
1263 snd_pcm_hw_rule_step, (void *) step,
1267 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1269 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1271 static unsigned int pow2_sizes[] = {
1272 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1273 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1274 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1275 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1277 return snd_interval_list(hw_param_interval(params, rule->var),
1278 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1282 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1283 * @runtime: PCM runtime instance
1284 * @cond: condition bits
1285 * @var: hw_params variable to apply the power-of-2 constraint
1287 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1289 snd_pcm_hw_param_t var)
1291 return snd_pcm_hw_rule_add(runtime, cond, var,
1292 snd_pcm_hw_rule_pow2, NULL,
1296 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1298 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1299 snd_pcm_hw_param_t var)
1301 if (hw_is_mask(var)) {
1302 snd_mask_any(hw_param_mask(params, var));
1303 params->cmask |= 1 << var;
1304 params->rmask |= 1 << var;
1307 if (hw_is_interval(var)) {
1308 snd_interval_any(hw_param_interval(params, var));
1309 params->cmask |= 1 << var;
1310 params->rmask |= 1 << var;
1316 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1319 memset(params, 0, sizeof(*params));
1320 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1321 _snd_pcm_hw_param_any(params, k);
1322 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1323 _snd_pcm_hw_param_any(params, k);
1327 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1330 * snd_pcm_hw_param_value - return @params field @var value
1331 * @params: the hw_params instance
1332 * @var: parameter to retrieve
1333 * @dir: pointer to the direction (-1,0,1) or %NULL
1335 * Return the value for field @var if it's fixed in configuration space
1336 * defined by @params. Return -%EINVAL otherwise.
1338 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1339 snd_pcm_hw_param_t var, int *dir)
1341 if (hw_is_mask(var)) {
1342 const struct snd_mask *mask = hw_param_mask_c(params, var);
1343 if (!snd_mask_single(mask))
1347 return snd_mask_value(mask);
1349 if (hw_is_interval(var)) {
1350 const struct snd_interval *i = hw_param_interval_c(params, var);
1351 if (!snd_interval_single(i))
1355 return snd_interval_value(i);
1360 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1362 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1363 snd_pcm_hw_param_t var)
1365 if (hw_is_mask(var)) {
1366 snd_mask_none(hw_param_mask(params, var));
1367 params->cmask |= 1 << var;
1368 params->rmask |= 1 << var;
1369 } else if (hw_is_interval(var)) {
1370 snd_interval_none(hw_param_interval(params, var));
1371 params->cmask |= 1 << var;
1372 params->rmask |= 1 << var;
1378 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1380 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1381 snd_pcm_hw_param_t var)
1384 if (hw_is_mask(var))
1385 changed = snd_mask_refine_first(hw_param_mask(params, var));
1386 else if (hw_is_interval(var))
1387 changed = snd_interval_refine_first(hw_param_interval(params, var));
1391 params->cmask |= 1 << var;
1392 params->rmask |= 1 << var;
1399 * snd_pcm_hw_param_first - refine config space and return minimum 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 > minimum. Reduce configuration space accordingly.
1407 * Return the minimum.
1409 int snd_pcm_hw_param_first(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_first(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_first);
1426 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1427 snd_pcm_hw_param_t var)
1430 if (hw_is_mask(var))
1431 changed = snd_mask_refine_last(hw_param_mask(params, var));
1432 else if (hw_is_interval(var))
1433 changed = snd_interval_refine_last(hw_param_interval(params, var));
1437 params->cmask |= 1 << var;
1438 params->rmask |= 1 << var;
1445 * snd_pcm_hw_param_last - refine config space and return maximum value
1446 * @pcm: PCM instance
1447 * @params: the hw_params instance
1448 * @var: parameter to retrieve
1449 * @dir: pointer to the direction (-1,0,1) or %NULL
1451 * Inside configuration space defined by @params remove from @var all
1452 * values < maximum. Reduce configuration space accordingly.
1453 * Return the maximum.
1455 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1456 struct snd_pcm_hw_params *params,
1457 snd_pcm_hw_param_t var, int *dir)
1459 int changed = _snd_pcm_hw_param_last(params, var);
1462 if (params->rmask) {
1463 int err = snd_pcm_hw_refine(pcm, params);
1464 if (snd_BUG_ON(err < 0))
1467 return snd_pcm_hw_param_value(params, var, dir);
1470 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1473 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1474 * @pcm: PCM instance
1475 * @params: the hw_params instance
1477 * Choose one configuration from configuration space defined by @params.
1478 * The configuration chosen is that obtained fixing in this order:
1479 * first access, first format, first subformat, min channels,
1480 * min rate, min period time, max buffer size, min tick time
1482 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1483 struct snd_pcm_hw_params *params)
1485 static int vars[] = {
1486 SNDRV_PCM_HW_PARAM_ACCESS,
1487 SNDRV_PCM_HW_PARAM_FORMAT,
1488 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1489 SNDRV_PCM_HW_PARAM_CHANNELS,
1490 SNDRV_PCM_HW_PARAM_RATE,
1491 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1492 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1493 SNDRV_PCM_HW_PARAM_TICK_TIME,
1498 for (v = vars; *v != -1; v++) {
1499 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1500 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1502 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1503 if (snd_BUG_ON(err < 0))
1509 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1512 struct snd_pcm_runtime *runtime = substream->runtime;
1513 unsigned long flags;
1514 snd_pcm_stream_lock_irqsave(substream, flags);
1515 if (snd_pcm_running(substream) &&
1516 snd_pcm_update_hw_ptr(substream) >= 0)
1517 runtime->status->hw_ptr %= runtime->buffer_size;
1519 runtime->status->hw_ptr = 0;
1520 snd_pcm_stream_unlock_irqrestore(substream, flags);
1524 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1527 struct snd_pcm_channel_info *info = arg;
1528 struct snd_pcm_runtime *runtime = substream->runtime;
1530 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1534 width = snd_pcm_format_physical_width(runtime->format);
1538 switch (runtime->access) {
1539 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1540 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1541 info->first = info->channel * width;
1542 info->step = runtime->channels * width;
1544 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1545 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1547 size_t size = runtime->dma_bytes / runtime->channels;
1548 info->first = info->channel * size * 8;
1559 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1562 struct snd_pcm_hw_params *params = arg;
1563 snd_pcm_format_t format;
1564 int channels, width;
1566 params->fifo_size = substream->runtime->hw.fifo_size;
1567 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1568 format = params_format(params);
1569 channels = params_channels(params);
1570 width = snd_pcm_format_physical_width(format);
1571 params->fifo_size /= width * channels;
1577 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1578 * @substream: the pcm substream instance
1579 * @cmd: ioctl command
1580 * @arg: ioctl argument
1582 * Processes the generic ioctl commands for PCM.
1583 * Can be passed as the ioctl callback for PCM ops.
1585 * Returns zero if successful, or a negative error code on failure.
1587 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1588 unsigned int cmd, void *arg)
1591 case SNDRV_PCM_IOCTL1_INFO:
1593 case SNDRV_PCM_IOCTL1_RESET:
1594 return snd_pcm_lib_ioctl_reset(substream, arg);
1595 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1596 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1597 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1598 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1603 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1606 * snd_pcm_period_elapsed - update the pcm status for the next period
1607 * @substream: the pcm substream instance
1609 * This function is called from the interrupt handler when the
1610 * PCM has processed the period size. It will update the current
1611 * pointer, wake up sleepers, etc.
1613 * Even if more than one periods have elapsed since the last call, you
1614 * have to call this only once.
1616 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1618 struct snd_pcm_runtime *runtime;
1619 unsigned long flags;
1621 if (PCM_RUNTIME_CHECK(substream))
1623 runtime = substream->runtime;
1625 if (runtime->transfer_ack_begin)
1626 runtime->transfer_ack_begin(substream);
1628 snd_pcm_stream_lock_irqsave(substream, flags);
1629 if (!snd_pcm_running(substream) ||
1630 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1633 if (substream->timer_running)
1634 snd_timer_interrupt(substream->timer, 1);
1636 snd_pcm_stream_unlock_irqrestore(substream, flags);
1637 if (runtime->transfer_ack_end)
1638 runtime->transfer_ack_end(substream);
1639 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1642 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1645 * Wait until avail_min data becomes available
1646 * Returns a negative error code if any error occurs during operation.
1647 * The available space is stored on availp. When err = 0 and avail = 0
1648 * on the capture stream, it indicates the stream is in DRAINING state.
1650 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1651 snd_pcm_uframes_t *availp)
1653 struct snd_pcm_runtime *runtime = substream->runtime;
1654 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1657 snd_pcm_uframes_t avail = 0;
1660 init_waitqueue_entry(&wait, current);
1661 add_wait_queue(&runtime->sleep, &wait);
1663 if (signal_pending(current)) {
1667 set_current_state(TASK_INTERRUPTIBLE);
1668 snd_pcm_stream_unlock_irq(substream);
1669 tout = schedule_timeout(msecs_to_jiffies(10000));
1670 snd_pcm_stream_lock_irq(substream);
1671 switch (runtime->status->state) {
1672 case SNDRV_PCM_STATE_SUSPENDED:
1675 case SNDRV_PCM_STATE_XRUN:
1678 case SNDRV_PCM_STATE_DRAINING:
1682 avail = 0; /* indicate draining */
1684 case SNDRV_PCM_STATE_OPEN:
1685 case SNDRV_PCM_STATE_SETUP:
1686 case SNDRV_PCM_STATE_DISCONNECTED:
1691 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1692 is_playback ? "playback" : "capture");
1697 avail = snd_pcm_playback_avail(runtime);
1699 avail = snd_pcm_capture_avail(runtime);
1700 if (avail >= runtime->control->avail_min)
1704 remove_wait_queue(&runtime->sleep, &wait);
1709 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1711 unsigned long data, unsigned int off,
1712 snd_pcm_uframes_t frames)
1714 struct snd_pcm_runtime *runtime = substream->runtime;
1716 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1717 if (substream->ops->copy) {
1718 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1721 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1722 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1728 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1729 unsigned long data, unsigned int off,
1730 snd_pcm_uframes_t size);
1732 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1734 snd_pcm_uframes_t size,
1736 transfer_f transfer)
1738 struct snd_pcm_runtime *runtime = substream->runtime;
1739 snd_pcm_uframes_t xfer = 0;
1740 snd_pcm_uframes_t offset = 0;
1746 snd_pcm_stream_lock_irq(substream);
1747 switch (runtime->status->state) {
1748 case SNDRV_PCM_STATE_PREPARED:
1749 case SNDRV_PCM_STATE_RUNNING:
1750 case SNDRV_PCM_STATE_PAUSED:
1752 case SNDRV_PCM_STATE_XRUN:
1755 case SNDRV_PCM_STATE_SUSPENDED:
1764 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1765 snd_pcm_uframes_t avail;
1766 snd_pcm_uframes_t cont;
1767 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1768 snd_pcm_update_hw_ptr(substream);
1769 avail = snd_pcm_playback_avail(runtime);
1775 err = wait_for_avail_min(substream, &avail);
1779 frames = size > avail ? avail : size;
1780 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1783 if (snd_BUG_ON(!frames)) {
1784 snd_pcm_stream_unlock_irq(substream);
1787 appl_ptr = runtime->control->appl_ptr;
1788 appl_ofs = appl_ptr % runtime->buffer_size;
1789 snd_pcm_stream_unlock_irq(substream);
1790 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1792 snd_pcm_stream_lock_irq(substream);
1793 switch (runtime->status->state) {
1794 case SNDRV_PCM_STATE_XRUN:
1797 case SNDRV_PCM_STATE_SUSPENDED:
1804 if (appl_ptr >= runtime->boundary)
1805 appl_ptr -= runtime->boundary;
1806 runtime->control->appl_ptr = appl_ptr;
1807 if (substream->ops->ack)
1808 substream->ops->ack(substream);
1813 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1814 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1815 err = snd_pcm_start(substream);
1821 snd_pcm_stream_unlock_irq(substream);
1823 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1826 /* sanity-check for read/write methods */
1827 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1829 struct snd_pcm_runtime *runtime;
1830 if (PCM_RUNTIME_CHECK(substream))
1832 runtime = substream->runtime;
1833 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1835 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1840 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1842 struct snd_pcm_runtime *runtime;
1846 err = pcm_sanity_check(substream);
1849 runtime = substream->runtime;
1850 nonblock = !!(substream->f_flags & O_NONBLOCK);
1852 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1853 runtime->channels > 1)
1855 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1856 snd_pcm_lib_write_transfer);
1859 EXPORT_SYMBOL(snd_pcm_lib_write);
1861 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1863 unsigned long data, unsigned int off,
1864 snd_pcm_uframes_t frames)
1866 struct snd_pcm_runtime *runtime = substream->runtime;
1868 void __user **bufs = (void __user **)data;
1869 int channels = runtime->channels;
1871 if (substream->ops->copy) {
1872 if (snd_BUG_ON(!substream->ops->silence))
1874 for (c = 0; c < channels; ++c, ++bufs) {
1875 if (*bufs == NULL) {
1876 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1879 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1880 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1885 /* default transfer behaviour */
1886 size_t dma_csize = runtime->dma_bytes / channels;
1887 for (c = 0; c < channels; ++c, ++bufs) {
1888 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1889 if (*bufs == NULL) {
1890 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1892 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1893 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1901 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1903 snd_pcm_uframes_t frames)
1905 struct snd_pcm_runtime *runtime;
1909 err = pcm_sanity_check(substream);
1912 runtime = substream->runtime;
1913 nonblock = !!(substream->f_flags & O_NONBLOCK);
1915 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1917 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1918 nonblock, snd_pcm_lib_writev_transfer);
1921 EXPORT_SYMBOL(snd_pcm_lib_writev);
1923 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1925 unsigned long data, unsigned int off,
1926 snd_pcm_uframes_t frames)
1928 struct snd_pcm_runtime *runtime = substream->runtime;
1930 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1931 if (substream->ops->copy) {
1932 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1935 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1936 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1942 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1944 snd_pcm_uframes_t size,
1946 transfer_f transfer)
1948 struct snd_pcm_runtime *runtime = substream->runtime;
1949 snd_pcm_uframes_t xfer = 0;
1950 snd_pcm_uframes_t offset = 0;
1956 snd_pcm_stream_lock_irq(substream);
1957 switch (runtime->status->state) {
1958 case SNDRV_PCM_STATE_PREPARED:
1959 if (size >= runtime->start_threshold) {
1960 err = snd_pcm_start(substream);
1965 case SNDRV_PCM_STATE_DRAINING:
1966 case SNDRV_PCM_STATE_RUNNING:
1967 case SNDRV_PCM_STATE_PAUSED:
1969 case SNDRV_PCM_STATE_XRUN:
1972 case SNDRV_PCM_STATE_SUSPENDED:
1981 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1982 snd_pcm_uframes_t avail;
1983 snd_pcm_uframes_t cont;
1984 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1985 snd_pcm_update_hw_ptr(substream);
1986 avail = snd_pcm_capture_avail(runtime);
1988 if (runtime->status->state ==
1989 SNDRV_PCM_STATE_DRAINING) {
1990 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1997 err = wait_for_avail_min(substream, &avail);
2001 continue; /* draining */
2003 frames = size > avail ? avail : size;
2004 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2007 if (snd_BUG_ON(!frames)) {
2008 snd_pcm_stream_unlock_irq(substream);
2011 appl_ptr = runtime->control->appl_ptr;
2012 appl_ofs = appl_ptr % runtime->buffer_size;
2013 snd_pcm_stream_unlock_irq(substream);
2014 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2016 snd_pcm_stream_lock_irq(substream);
2017 switch (runtime->status->state) {
2018 case SNDRV_PCM_STATE_XRUN:
2021 case SNDRV_PCM_STATE_SUSPENDED:
2028 if (appl_ptr >= runtime->boundary)
2029 appl_ptr -= runtime->boundary;
2030 runtime->control->appl_ptr = appl_ptr;
2031 if (substream->ops->ack)
2032 substream->ops->ack(substream);
2039 snd_pcm_stream_unlock_irq(substream);
2041 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2044 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2046 struct snd_pcm_runtime *runtime;
2050 err = pcm_sanity_check(substream);
2053 runtime = substream->runtime;
2054 nonblock = !!(substream->f_flags & O_NONBLOCK);
2055 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2057 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2060 EXPORT_SYMBOL(snd_pcm_lib_read);
2062 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2064 unsigned long data, unsigned int off,
2065 snd_pcm_uframes_t frames)
2067 struct snd_pcm_runtime *runtime = substream->runtime;
2069 void __user **bufs = (void __user **)data;
2070 int channels = runtime->channels;
2072 if (substream->ops->copy) {
2073 for (c = 0; c < channels; ++c, ++bufs) {
2077 buf = *bufs + samples_to_bytes(runtime, off);
2078 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2082 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2083 for (c = 0; c < channels; ++c, ++bufs) {
2089 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2090 buf = *bufs + samples_to_bytes(runtime, off);
2091 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2098 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2100 snd_pcm_uframes_t frames)
2102 struct snd_pcm_runtime *runtime;
2106 err = pcm_sanity_check(substream);
2109 runtime = substream->runtime;
2110 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2113 nonblock = !!(substream->f_flags & O_NONBLOCK);
2114 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2116 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2119 EXPORT_SYMBOL(snd_pcm_lib_readv);