virtio_blk: don't bounce highmem requests
[linux-2.6/mini2440.git] / sound / core / pcm_lib.c
blob2a2c2cab352c8ec1a9b408b6506ef71adec0c026
1 /*
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;
50 if (n < 0)
51 n += runtime->boundary;
52 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53 runtime->silence_filled -= n;
54 else
55 runtime->silence_filled = 0;
56 runtime->silence_start = runtime->control->appl_ptr;
58 if (runtime->silence_filled >= runtime->buffer_size)
59 return;
60 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
62 return;
63 frames = runtime->silence_threshold - noise_dist;
64 if (frames > runtime->silence_size)
65 frames = runtime->silence_size;
66 } else {
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) %
72 runtime->boundary;
73 } else {
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;
82 } else {
83 runtime->silence_start = ofs;
86 frames = runtime->buffer_size - runtime->silence_filled;
88 if (snd_BUG_ON(frames > runtime->buffer_size))
89 return;
90 if (frames == 0)
91 return;
92 ofs = runtime->silence_start % runtime->buffer_size;
93 while (frames > 0) {
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) {
98 int err;
99 err = substream->ops->silence(substream, -1, ofs, transfer);
100 snd_BUG_ON(err < 0);
101 } else {
102 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
103 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
105 } else {
106 unsigned int c;
107 unsigned int channels = runtime->channels;
108 if (substream->ops->silence) {
109 for (c = 0; c < channels; ++c) {
110 int err;
111 err = substream->ops->silence(substream, c, ofs, transfer);
112 snd_BUG_ON(err < 0);
114 } else {
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;
123 frames -= transfer;
124 ofs = 0;
128 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
129 #define xrun_debug(substream) ((substream)->pstr->xrun_debug)
130 #else
131 #define xrun_debug(substream) 0
132 #endif
134 #define dump_stack_on_xrun(substream) do { \
135 if (xrun_debug(substream) > 1) \
136 dump_stack(); \
137 } while (0)
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);
169 pos = 0;
171 pos -= pos % runtime->min_align;
172 return pos;
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);
182 else
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);
189 else
190 xrun(substream);
191 return -EPIPE;
193 if (avail >= runtime->control->avail_min)
194 wake_up(&runtime->sleep);
195 return 0;
198 #define hw_ptr_error(substream, fmt, args...) \
199 do { \
200 if (xrun_debug(substream)) { \
201 if (printk_ratelimit()) { \
202 snd_printd("PCM: " fmt, ##args); \
204 dump_stack_on_xrun(substream); \
206 } while (0)
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) {
219 xrun(substream);
220 return -EPIPE;
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;
232 if (delta < 0) {
233 delta += runtime->buffer_size;
234 if (delta < 0) {
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;
244 delta = 0;
245 } else {
246 hw_base += runtime->buffer_size;
247 if (hw_base >= runtime->boundary)
248 hw_base = 0;
249 new_hw_ptr = hw_base + pos;
253 /* Do jiffies check only in xrun_debug mode */
254 if (!xrun_debug(substream))
255 goto no_jiffies_check;
257 /* Skip the jiffies check for hardwares with BATCH flag.
258 * Such hardware usually just increases the position at each IRQ,
259 * thus it can't give any strange position.
261 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
262 goto no_jiffies_check;
263 hdelta = new_hw_ptr - old_hw_ptr;
264 jdelta = jiffies - runtime->hw_ptr_jiffies;
265 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
266 delta = jdelta /
267 (((runtime->period_size * HZ) / runtime->rate)
268 + HZ/100);
269 hw_ptr_error(substream,
270 "hw_ptr skipping! [Q] "
271 "(pos=%ld, delta=%ld, period=%ld, "
272 "jdelta=%lu/%lu/%lu)\n",
273 (long)pos, (long)hdelta,
274 (long)runtime->period_size, jdelta,
275 ((hdelta * HZ) / runtime->rate), delta);
276 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
277 runtime->period_size * delta;
278 if (hw_ptr_interrupt >= runtime->boundary)
279 hw_ptr_interrupt -= runtime->boundary;
280 /* rebase to interrupt position */
281 hw_base = new_hw_ptr = hw_ptr_interrupt;
282 /* align hw_base to buffer_size */
283 hw_base -= hw_base % runtime->buffer_size;
284 delta = 0;
286 no_jiffies_check:
287 if (delta > runtime->period_size + runtime->period_size / 2) {
288 hw_ptr_error(substream,
289 "Lost interrupts? "
290 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
291 substream->stream, (long)delta,
292 (long)hw_ptr_interrupt);
293 /* rebase hw_ptr_interrupt */
294 hw_ptr_interrupt =
295 new_hw_ptr - new_hw_ptr % runtime->period_size;
297 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
298 runtime->silence_size > 0)
299 snd_pcm_playback_silence(substream, new_hw_ptr);
301 runtime->hw_ptr_base = hw_base;
302 runtime->status->hw_ptr = new_hw_ptr;
303 runtime->hw_ptr_jiffies = jiffies;
304 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
306 return snd_pcm_update_hw_ptr_post(substream, runtime);
309 /* CAUTION: call it with irq disabled */
310 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
312 struct snd_pcm_runtime *runtime = substream->runtime;
313 snd_pcm_uframes_t pos;
314 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
315 snd_pcm_sframes_t delta;
316 unsigned long jdelta;
318 old_hw_ptr = runtime->status->hw_ptr;
319 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
320 if (pos == SNDRV_PCM_POS_XRUN) {
321 xrun(substream);
322 return -EPIPE;
324 hw_base = runtime->hw_ptr_base;
325 new_hw_ptr = hw_base + pos;
327 delta = new_hw_ptr - old_hw_ptr;
328 jdelta = jiffies - runtime->hw_ptr_jiffies;
329 if (delta < 0) {
330 delta += runtime->buffer_size;
331 if (delta < 0) {
332 hw_ptr_error(substream,
333 "Unexpected hw_pointer value [2] "
334 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
335 substream->stream, (long)pos,
336 (long)old_hw_ptr, jdelta);
337 return 0;
339 hw_base += runtime->buffer_size;
340 if (hw_base >= runtime->boundary)
341 hw_base = 0;
342 new_hw_ptr = hw_base + pos;
344 /* Do jiffies check only in xrun_debug mode */
345 if (xrun_debug(substream) &&
346 ((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
347 hw_ptr_error(substream,
348 "hw_ptr skipping! "
349 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
350 (long)pos, (long)delta,
351 (long)runtime->period_size, jdelta,
352 ((delta * HZ) / runtime->rate));
353 return 0;
355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
356 runtime->silence_size > 0)
357 snd_pcm_playback_silence(substream, new_hw_ptr);
359 runtime->hw_ptr_base = hw_base;
360 runtime->status->hw_ptr = new_hw_ptr;
361 runtime->hw_ptr_jiffies = jiffies;
363 return snd_pcm_update_hw_ptr_post(substream, runtime);
367 * snd_pcm_set_ops - set the PCM operators
368 * @pcm: the pcm instance
369 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
370 * @ops: the operator table
372 * Sets the given PCM operators to the pcm instance.
374 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
376 struct snd_pcm_str *stream = &pcm->streams[direction];
377 struct snd_pcm_substream *substream;
379 for (substream = stream->substream; substream != NULL; substream = substream->next)
380 substream->ops = ops;
383 EXPORT_SYMBOL(snd_pcm_set_ops);
386 * snd_pcm_sync - set the PCM sync id
387 * @substream: the pcm substream
389 * Sets the PCM sync identifier for the card.
391 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
393 struct snd_pcm_runtime *runtime = substream->runtime;
395 runtime->sync.id32[0] = substream->pcm->card->number;
396 runtime->sync.id32[1] = -1;
397 runtime->sync.id32[2] = -1;
398 runtime->sync.id32[3] = -1;
401 EXPORT_SYMBOL(snd_pcm_set_sync);
404 * Standard ioctl routine
407 static inline unsigned int div32(unsigned int a, unsigned int b,
408 unsigned int *r)
410 if (b == 0) {
411 *r = 0;
412 return UINT_MAX;
414 *r = a % b;
415 return a / b;
418 static inline unsigned int div_down(unsigned int a, unsigned int b)
420 if (b == 0)
421 return UINT_MAX;
422 return a / b;
425 static inline unsigned int div_up(unsigned int a, unsigned int b)
427 unsigned int r;
428 unsigned int q;
429 if (b == 0)
430 return UINT_MAX;
431 q = div32(a, b, &r);
432 if (r)
433 ++q;
434 return q;
437 static inline unsigned int mul(unsigned int a, unsigned int b)
439 if (a == 0)
440 return 0;
441 if (div_down(UINT_MAX, a) < b)
442 return UINT_MAX;
443 return a * b;
446 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
447 unsigned int c, unsigned int *r)
449 u_int64_t n = (u_int64_t) a * b;
450 if (c == 0) {
451 snd_BUG_ON(!n);
452 *r = 0;
453 return UINT_MAX;
455 div64_32(&n, c, r);
456 if (n >= UINT_MAX) {
457 *r = 0;
458 return UINT_MAX;
460 return n;
464 * snd_interval_refine - refine the interval value of configurator
465 * @i: the interval value to refine
466 * @v: the interval value to refer to
468 * Refines the interval value with the reference value.
469 * The interval is changed to the range satisfying both intervals.
470 * The interval status (min, max, integer, etc.) are evaluated.
472 * Returns non-zero if the value is changed, zero if not changed.
474 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
476 int changed = 0;
477 if (snd_BUG_ON(snd_interval_empty(i)))
478 return -EINVAL;
479 if (i->min < v->min) {
480 i->min = v->min;
481 i->openmin = v->openmin;
482 changed = 1;
483 } else if (i->min == v->min && !i->openmin && v->openmin) {
484 i->openmin = 1;
485 changed = 1;
487 if (i->max > v->max) {
488 i->max = v->max;
489 i->openmax = v->openmax;
490 changed = 1;
491 } else if (i->max == v->max && !i->openmax && v->openmax) {
492 i->openmax = 1;
493 changed = 1;
495 if (!i->integer && v->integer) {
496 i->integer = 1;
497 changed = 1;
499 if (i->integer) {
500 if (i->openmin) {
501 i->min++;
502 i->openmin = 0;
504 if (i->openmax) {
505 i->max--;
506 i->openmax = 0;
508 } else if (!i->openmin && !i->openmax && i->min == i->max)
509 i->integer = 1;
510 if (snd_interval_checkempty(i)) {
511 snd_interval_none(i);
512 return -EINVAL;
514 return changed;
517 EXPORT_SYMBOL(snd_interval_refine);
519 static int snd_interval_refine_first(struct snd_interval *i)
521 if (snd_BUG_ON(snd_interval_empty(i)))
522 return -EINVAL;
523 if (snd_interval_single(i))
524 return 0;
525 i->max = i->min;
526 i->openmax = i->openmin;
527 if (i->openmax)
528 i->max++;
529 return 1;
532 static int snd_interval_refine_last(struct snd_interval *i)
534 if (snd_BUG_ON(snd_interval_empty(i)))
535 return -EINVAL;
536 if (snd_interval_single(i))
537 return 0;
538 i->min = i->max;
539 i->openmin = i->openmax;
540 if (i->openmin)
541 i->min--;
542 return 1;
545 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
547 if (a->empty || b->empty) {
548 snd_interval_none(c);
549 return;
551 c->empty = 0;
552 c->min = mul(a->min, b->min);
553 c->openmin = (a->openmin || b->openmin);
554 c->max = mul(a->max, b->max);
555 c->openmax = (a->openmax || b->openmax);
556 c->integer = (a->integer && b->integer);
560 * snd_interval_div - refine the interval value with division
561 * @a: dividend
562 * @b: divisor
563 * @c: quotient
565 * c = a / b
567 * Returns non-zero if the value is changed, zero if not changed.
569 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
571 unsigned int r;
572 if (a->empty || b->empty) {
573 snd_interval_none(c);
574 return;
576 c->empty = 0;
577 c->min = div32(a->min, b->max, &r);
578 c->openmin = (r || a->openmin || b->openmax);
579 if (b->min > 0) {
580 c->max = div32(a->max, b->min, &r);
581 if (r) {
582 c->max++;
583 c->openmax = 1;
584 } else
585 c->openmax = (a->openmax || b->openmin);
586 } else {
587 c->max = UINT_MAX;
588 c->openmax = 0;
590 c->integer = 0;
594 * snd_interval_muldivk - refine the interval value
595 * @a: dividend 1
596 * @b: dividend 2
597 * @k: divisor (as integer)
598 * @c: result
600 * c = a * b / k
602 * Returns non-zero if the value is changed, zero if not changed.
604 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
605 unsigned int k, struct snd_interval *c)
607 unsigned int r;
608 if (a->empty || b->empty) {
609 snd_interval_none(c);
610 return;
612 c->empty = 0;
613 c->min = muldiv32(a->min, b->min, k, &r);
614 c->openmin = (r || a->openmin || b->openmin);
615 c->max = muldiv32(a->max, b->max, k, &r);
616 if (r) {
617 c->max++;
618 c->openmax = 1;
619 } else
620 c->openmax = (a->openmax || b->openmax);
621 c->integer = 0;
625 * snd_interval_mulkdiv - refine the interval value
626 * @a: dividend 1
627 * @k: dividend 2 (as integer)
628 * @b: divisor
629 * @c: result
631 * c = a * k / b
633 * Returns non-zero if the value is changed, zero if not changed.
635 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
636 const struct snd_interval *b, struct snd_interval *c)
638 unsigned int r;
639 if (a->empty || b->empty) {
640 snd_interval_none(c);
641 return;
643 c->empty = 0;
644 c->min = muldiv32(a->min, k, b->max, &r);
645 c->openmin = (r || a->openmin || b->openmax);
646 if (b->min > 0) {
647 c->max = muldiv32(a->max, k, b->min, &r);
648 if (r) {
649 c->max++;
650 c->openmax = 1;
651 } else
652 c->openmax = (a->openmax || b->openmin);
653 } else {
654 c->max = UINT_MAX;
655 c->openmax = 0;
657 c->integer = 0;
660 /* ---- */
664 * snd_interval_ratnum - refine the interval value
665 * @i: interval to refine
666 * @rats_count: number of ratnum_t
667 * @rats: ratnum_t array
668 * @nump: pointer to store the resultant numerator
669 * @denp: pointer to store the resultant denominator
671 * Returns non-zero if the value is changed, zero if not changed.
673 int snd_interval_ratnum(struct snd_interval *i,
674 unsigned int rats_count, struct snd_ratnum *rats,
675 unsigned int *nump, unsigned int *denp)
677 unsigned int best_num, best_diff, best_den;
678 unsigned int k;
679 struct snd_interval t;
680 int err;
682 best_num = best_den = best_diff = 0;
683 for (k = 0; k < rats_count; ++k) {
684 unsigned int num = rats[k].num;
685 unsigned int den;
686 unsigned int q = i->min;
687 int diff;
688 if (q == 0)
689 q = 1;
690 den = div_down(num, q);
691 if (den < rats[k].den_min)
692 continue;
693 if (den > rats[k].den_max)
694 den = rats[k].den_max;
695 else {
696 unsigned int r;
697 r = (den - rats[k].den_min) % rats[k].den_step;
698 if (r != 0)
699 den -= r;
701 diff = num - q * den;
702 if (best_num == 0 ||
703 diff * best_den < best_diff * den) {
704 best_diff = diff;
705 best_den = den;
706 best_num = num;
709 if (best_den == 0) {
710 i->empty = 1;
711 return -EINVAL;
713 t.min = div_down(best_num, best_den);
714 t.openmin = !!(best_num % best_den);
716 best_num = best_den = best_diff = 0;
717 for (k = 0; k < rats_count; ++k) {
718 unsigned int num = rats[k].num;
719 unsigned int den;
720 unsigned int q = i->max;
721 int diff;
722 if (q == 0) {
723 i->empty = 1;
724 return -EINVAL;
726 den = div_up(num, q);
727 if (den > rats[k].den_max)
728 continue;
729 if (den < rats[k].den_min)
730 den = rats[k].den_min;
731 else {
732 unsigned int r;
733 r = (den - rats[k].den_min) % rats[k].den_step;
734 if (r != 0)
735 den += rats[k].den_step - r;
737 diff = q * den - num;
738 if (best_num == 0 ||
739 diff * best_den < best_diff * den) {
740 best_diff = diff;
741 best_den = den;
742 best_num = num;
745 if (best_den == 0) {
746 i->empty = 1;
747 return -EINVAL;
749 t.max = div_up(best_num, best_den);
750 t.openmax = !!(best_num % best_den);
751 t.integer = 0;
752 err = snd_interval_refine(i, &t);
753 if (err < 0)
754 return err;
756 if (snd_interval_single(i)) {
757 if (nump)
758 *nump = best_num;
759 if (denp)
760 *denp = best_den;
762 return err;
765 EXPORT_SYMBOL(snd_interval_ratnum);
768 * snd_interval_ratden - refine the interval value
769 * @i: interval to refine
770 * @rats_count: number of struct ratden
771 * @rats: struct ratden array
772 * @nump: pointer to store the resultant numerator
773 * @denp: pointer to store the resultant denominator
775 * Returns non-zero if the value is changed, zero if not changed.
777 static int snd_interval_ratden(struct snd_interval *i,
778 unsigned int rats_count, struct snd_ratden *rats,
779 unsigned int *nump, unsigned int *denp)
781 unsigned int best_num, best_diff, best_den;
782 unsigned int k;
783 struct snd_interval t;
784 int err;
786 best_num = best_den = best_diff = 0;
787 for (k = 0; k < rats_count; ++k) {
788 unsigned int num;
789 unsigned int den = rats[k].den;
790 unsigned int q = i->min;
791 int diff;
792 num = mul(q, den);
793 if (num > rats[k].num_max)
794 continue;
795 if (num < rats[k].num_min)
796 num = rats[k].num_max;
797 else {
798 unsigned int r;
799 r = (num - rats[k].num_min) % rats[k].num_step;
800 if (r != 0)
801 num += rats[k].num_step - r;
803 diff = num - q * den;
804 if (best_num == 0 ||
805 diff * best_den < best_diff * den) {
806 best_diff = diff;
807 best_den = den;
808 best_num = num;
811 if (best_den == 0) {
812 i->empty = 1;
813 return -EINVAL;
815 t.min = div_down(best_num, best_den);
816 t.openmin = !!(best_num % best_den);
818 best_num = best_den = best_diff = 0;
819 for (k = 0; k < rats_count; ++k) {
820 unsigned int num;
821 unsigned int den = rats[k].den;
822 unsigned int q = i->max;
823 int diff;
824 num = mul(q, den);
825 if (num < rats[k].num_min)
826 continue;
827 if (num > rats[k].num_max)
828 num = rats[k].num_max;
829 else {
830 unsigned int r;
831 r = (num - rats[k].num_min) % rats[k].num_step;
832 if (r != 0)
833 num -= r;
835 diff = q * den - num;
836 if (best_num == 0 ||
837 diff * best_den < best_diff * den) {
838 best_diff = diff;
839 best_den = den;
840 best_num = num;
843 if (best_den == 0) {
844 i->empty = 1;
845 return -EINVAL;
847 t.max = div_up(best_num, best_den);
848 t.openmax = !!(best_num % best_den);
849 t.integer = 0;
850 err = snd_interval_refine(i, &t);
851 if (err < 0)
852 return err;
854 if (snd_interval_single(i)) {
855 if (nump)
856 *nump = best_num;
857 if (denp)
858 *denp = best_den;
860 return err;
864 * snd_interval_list - refine the interval value from the list
865 * @i: the interval value to refine
866 * @count: the number of elements in the list
867 * @list: the value list
868 * @mask: the bit-mask to evaluate
870 * Refines the interval value from the list.
871 * When mask is non-zero, only the elements corresponding to bit 1 are
872 * evaluated.
874 * Returns non-zero if the value is changed, zero if not changed.
876 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
878 unsigned int k;
879 struct snd_interval list_range;
881 if (!count) {
882 i->empty = 1;
883 return -EINVAL;
885 snd_interval_any(&list_range);
886 list_range.min = UINT_MAX;
887 list_range.max = 0;
888 for (k = 0; k < count; k++) {
889 if (mask && !(mask & (1 << k)))
890 continue;
891 if (!snd_interval_test(i, list[k]))
892 continue;
893 list_range.min = min(list_range.min, list[k]);
894 list_range.max = max(list_range.max, list[k]);
896 return snd_interval_refine(i, &list_range);
899 EXPORT_SYMBOL(snd_interval_list);
901 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
903 unsigned int n;
904 int changed = 0;
905 n = (i->min - min) % step;
906 if (n != 0 || i->openmin) {
907 i->min += step - n;
908 changed = 1;
910 n = (i->max - min) % step;
911 if (n != 0 || i->openmax) {
912 i->max -= n;
913 changed = 1;
915 if (snd_interval_checkempty(i)) {
916 i->empty = 1;
917 return -EINVAL;
919 return changed;
922 /* Info constraints helpers */
925 * snd_pcm_hw_rule_add - add the hw-constraint rule
926 * @runtime: the pcm runtime instance
927 * @cond: condition bits
928 * @var: the variable to evaluate
929 * @func: the evaluation function
930 * @private: the private data pointer passed to function
931 * @dep: the dependent variables
933 * Returns zero if successful, or a negative error code on failure.
935 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
936 int var,
937 snd_pcm_hw_rule_func_t func, void *private,
938 int dep, ...)
940 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
941 struct snd_pcm_hw_rule *c;
942 unsigned int k;
943 va_list args;
944 va_start(args, dep);
945 if (constrs->rules_num >= constrs->rules_all) {
946 struct snd_pcm_hw_rule *new;
947 unsigned int new_rules = constrs->rules_all + 16;
948 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
949 if (!new)
950 return -ENOMEM;
951 if (constrs->rules) {
952 memcpy(new, constrs->rules,
953 constrs->rules_num * sizeof(*c));
954 kfree(constrs->rules);
956 constrs->rules = new;
957 constrs->rules_all = new_rules;
959 c = &constrs->rules[constrs->rules_num];
960 c->cond = cond;
961 c->func = func;
962 c->var = var;
963 c->private = private;
964 k = 0;
965 while (1) {
966 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
967 return -EINVAL;
968 c->deps[k++] = dep;
969 if (dep < 0)
970 break;
971 dep = va_arg(args, int);
973 constrs->rules_num++;
974 va_end(args);
975 return 0;
978 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
981 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
982 * @runtime: PCM runtime instance
983 * @var: hw_params variable to apply the mask
984 * @mask: the bitmap mask
986 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
988 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
989 u_int32_t mask)
991 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
992 struct snd_mask *maskp = constrs_mask(constrs, var);
993 *maskp->bits &= mask;
994 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
995 if (*maskp->bits == 0)
996 return -EINVAL;
997 return 0;
1001 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1002 * @runtime: PCM runtime instance
1003 * @var: hw_params variable to apply the mask
1004 * @mask: the 64bit bitmap mask
1006 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1008 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1009 u_int64_t mask)
1011 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1012 struct snd_mask *maskp = constrs_mask(constrs, var);
1013 maskp->bits[0] &= (u_int32_t)mask;
1014 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1015 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1016 if (! maskp->bits[0] && ! maskp->bits[1])
1017 return -EINVAL;
1018 return 0;
1022 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1023 * @runtime: PCM runtime instance
1024 * @var: hw_params variable to apply the integer constraint
1026 * Apply the constraint of integer to an interval parameter.
1028 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1030 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1031 return snd_interval_setinteger(constrs_interval(constrs, var));
1034 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1037 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1038 * @runtime: PCM runtime instance
1039 * @var: hw_params variable to apply the range
1040 * @min: the minimal value
1041 * @max: the maximal value
1043 * Apply the min/max range constraint to an interval parameter.
1045 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1046 unsigned int min, unsigned int max)
1048 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1049 struct snd_interval t;
1050 t.min = min;
1051 t.max = max;
1052 t.openmin = t.openmax = 0;
1053 t.integer = 0;
1054 return snd_interval_refine(constrs_interval(constrs, var), &t);
1057 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1059 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1060 struct snd_pcm_hw_rule *rule)
1062 struct snd_pcm_hw_constraint_list *list = rule->private;
1063 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1068 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1069 * @runtime: PCM runtime instance
1070 * @cond: condition bits
1071 * @var: hw_params variable to apply the list constraint
1072 * @l: list
1074 * Apply the list of constraints to an interval parameter.
1076 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1077 unsigned int cond,
1078 snd_pcm_hw_param_t var,
1079 struct snd_pcm_hw_constraint_list *l)
1081 return snd_pcm_hw_rule_add(runtime, cond, var,
1082 snd_pcm_hw_rule_list, l,
1083 var, -1);
1086 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1088 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1089 struct snd_pcm_hw_rule *rule)
1091 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1092 unsigned int num = 0, den = 0;
1093 int err;
1094 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1095 r->nrats, r->rats, &num, &den);
1096 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1097 params->rate_num = num;
1098 params->rate_den = den;
1100 return err;
1104 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1105 * @runtime: PCM runtime instance
1106 * @cond: condition bits
1107 * @var: hw_params variable to apply the ratnums constraint
1108 * @r: struct snd_ratnums constriants
1110 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1111 unsigned int cond,
1112 snd_pcm_hw_param_t var,
1113 struct snd_pcm_hw_constraint_ratnums *r)
1115 return snd_pcm_hw_rule_add(runtime, cond, var,
1116 snd_pcm_hw_rule_ratnums, r,
1117 var, -1);
1120 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1122 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1123 struct snd_pcm_hw_rule *rule)
1125 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1126 unsigned int num = 0, den = 0;
1127 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1128 r->nrats, r->rats, &num, &den);
1129 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1130 params->rate_num = num;
1131 params->rate_den = den;
1133 return err;
1137 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1138 * @runtime: PCM runtime instance
1139 * @cond: condition bits
1140 * @var: hw_params variable to apply the ratdens constraint
1141 * @r: struct snd_ratdens constriants
1143 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1144 unsigned int cond,
1145 snd_pcm_hw_param_t var,
1146 struct snd_pcm_hw_constraint_ratdens *r)
1148 return snd_pcm_hw_rule_add(runtime, cond, var,
1149 snd_pcm_hw_rule_ratdens, r,
1150 var, -1);
1153 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1155 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1156 struct snd_pcm_hw_rule *rule)
1158 unsigned int l = (unsigned long) rule->private;
1159 int width = l & 0xffff;
1160 unsigned int msbits = l >> 16;
1161 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1162 if (snd_interval_single(i) && snd_interval_value(i) == width)
1163 params->msbits = msbits;
1164 return 0;
1168 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1169 * @runtime: PCM runtime instance
1170 * @cond: condition bits
1171 * @width: sample bits width
1172 * @msbits: msbits width
1174 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1175 unsigned int cond,
1176 unsigned int width,
1177 unsigned int msbits)
1179 unsigned long l = (msbits << 16) | width;
1180 return snd_pcm_hw_rule_add(runtime, cond, -1,
1181 snd_pcm_hw_rule_msbits,
1182 (void*) l,
1183 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1186 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1188 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1189 struct snd_pcm_hw_rule *rule)
1191 unsigned long step = (unsigned long) rule->private;
1192 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1196 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1197 * @runtime: PCM runtime instance
1198 * @cond: condition bits
1199 * @var: hw_params variable to apply the step constraint
1200 * @step: step size
1202 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1203 unsigned int cond,
1204 snd_pcm_hw_param_t var,
1205 unsigned long step)
1207 return snd_pcm_hw_rule_add(runtime, cond, var,
1208 snd_pcm_hw_rule_step, (void *) step,
1209 var, -1);
1212 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1214 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1216 static unsigned int pow2_sizes[] = {
1217 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1218 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1219 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1220 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1222 return snd_interval_list(hw_param_interval(params, rule->var),
1223 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1227 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1228 * @runtime: PCM runtime instance
1229 * @cond: condition bits
1230 * @var: hw_params variable to apply the power-of-2 constraint
1232 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1233 unsigned int cond,
1234 snd_pcm_hw_param_t var)
1236 return snd_pcm_hw_rule_add(runtime, cond, var,
1237 snd_pcm_hw_rule_pow2, NULL,
1238 var, -1);
1241 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1243 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1244 snd_pcm_hw_param_t var)
1246 if (hw_is_mask(var)) {
1247 snd_mask_any(hw_param_mask(params, var));
1248 params->cmask |= 1 << var;
1249 params->rmask |= 1 << var;
1250 return;
1252 if (hw_is_interval(var)) {
1253 snd_interval_any(hw_param_interval(params, var));
1254 params->cmask |= 1 << var;
1255 params->rmask |= 1 << var;
1256 return;
1258 snd_BUG();
1261 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1263 unsigned int k;
1264 memset(params, 0, sizeof(*params));
1265 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1266 _snd_pcm_hw_param_any(params, k);
1267 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1268 _snd_pcm_hw_param_any(params, k);
1269 params->info = ~0U;
1272 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1275 * snd_pcm_hw_param_value - return @params field @var value
1276 * @params: the hw_params instance
1277 * @var: parameter to retrieve
1278 * @dir: pointer to the direction (-1,0,1) or %NULL
1280 * Return the value for field @var if it's fixed in configuration space
1281 * defined by @params. Return -%EINVAL otherwise.
1283 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1284 snd_pcm_hw_param_t var, int *dir)
1286 if (hw_is_mask(var)) {
1287 const struct snd_mask *mask = hw_param_mask_c(params, var);
1288 if (!snd_mask_single(mask))
1289 return -EINVAL;
1290 if (dir)
1291 *dir = 0;
1292 return snd_mask_value(mask);
1294 if (hw_is_interval(var)) {
1295 const struct snd_interval *i = hw_param_interval_c(params, var);
1296 if (!snd_interval_single(i))
1297 return -EINVAL;
1298 if (dir)
1299 *dir = i->openmin;
1300 return snd_interval_value(i);
1302 return -EINVAL;
1305 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1307 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1308 snd_pcm_hw_param_t var)
1310 if (hw_is_mask(var)) {
1311 snd_mask_none(hw_param_mask(params, var));
1312 params->cmask |= 1 << var;
1313 params->rmask |= 1 << var;
1314 } else if (hw_is_interval(var)) {
1315 snd_interval_none(hw_param_interval(params, var));
1316 params->cmask |= 1 << var;
1317 params->rmask |= 1 << var;
1318 } else {
1319 snd_BUG();
1323 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1325 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1326 snd_pcm_hw_param_t var)
1328 int changed;
1329 if (hw_is_mask(var))
1330 changed = snd_mask_refine_first(hw_param_mask(params, var));
1331 else if (hw_is_interval(var))
1332 changed = snd_interval_refine_first(hw_param_interval(params, var));
1333 else
1334 return -EINVAL;
1335 if (changed) {
1336 params->cmask |= 1 << var;
1337 params->rmask |= 1 << var;
1339 return changed;
1344 * snd_pcm_hw_param_first - refine config space and return minimum value
1345 * @pcm: PCM instance
1346 * @params: the hw_params instance
1347 * @var: parameter to retrieve
1348 * @dir: pointer to the direction (-1,0,1) or %NULL
1350 * Inside configuration space defined by @params remove from @var all
1351 * values > minimum. Reduce configuration space accordingly.
1352 * Return the minimum.
1354 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1355 struct snd_pcm_hw_params *params,
1356 snd_pcm_hw_param_t var, int *dir)
1358 int changed = _snd_pcm_hw_param_first(params, var);
1359 if (changed < 0)
1360 return changed;
1361 if (params->rmask) {
1362 int err = snd_pcm_hw_refine(pcm, params);
1363 if (snd_BUG_ON(err < 0))
1364 return err;
1366 return snd_pcm_hw_param_value(params, var, dir);
1369 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1371 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1372 snd_pcm_hw_param_t var)
1374 int changed;
1375 if (hw_is_mask(var))
1376 changed = snd_mask_refine_last(hw_param_mask(params, var));
1377 else if (hw_is_interval(var))
1378 changed = snd_interval_refine_last(hw_param_interval(params, var));
1379 else
1380 return -EINVAL;
1381 if (changed) {
1382 params->cmask |= 1 << var;
1383 params->rmask |= 1 << var;
1385 return changed;
1390 * snd_pcm_hw_param_last - refine config space and return maximum value
1391 * @pcm: PCM instance
1392 * @params: the hw_params instance
1393 * @var: parameter to retrieve
1394 * @dir: pointer to the direction (-1,0,1) or %NULL
1396 * Inside configuration space defined by @params remove from @var all
1397 * values < maximum. Reduce configuration space accordingly.
1398 * Return the maximum.
1400 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1401 struct snd_pcm_hw_params *params,
1402 snd_pcm_hw_param_t var, int *dir)
1404 int changed = _snd_pcm_hw_param_last(params, var);
1405 if (changed < 0)
1406 return changed;
1407 if (params->rmask) {
1408 int err = snd_pcm_hw_refine(pcm, params);
1409 if (snd_BUG_ON(err < 0))
1410 return err;
1412 return snd_pcm_hw_param_value(params, var, dir);
1415 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1418 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1419 * @pcm: PCM instance
1420 * @params: the hw_params instance
1422 * Choose one configuration from configuration space defined by @params.
1423 * The configuration chosen is that obtained fixing in this order:
1424 * first access, first format, first subformat, min channels,
1425 * min rate, min period time, max buffer size, min tick time
1427 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1428 struct snd_pcm_hw_params *params)
1430 static int vars[] = {
1431 SNDRV_PCM_HW_PARAM_ACCESS,
1432 SNDRV_PCM_HW_PARAM_FORMAT,
1433 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1434 SNDRV_PCM_HW_PARAM_CHANNELS,
1435 SNDRV_PCM_HW_PARAM_RATE,
1436 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1437 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1438 SNDRV_PCM_HW_PARAM_TICK_TIME,
1441 int err, *v;
1443 for (v = vars; *v != -1; v++) {
1444 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1445 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1446 else
1447 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1448 if (snd_BUG_ON(err < 0))
1449 return err;
1451 return 0;
1454 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1455 void *arg)
1457 struct snd_pcm_runtime *runtime = substream->runtime;
1458 unsigned long flags;
1459 snd_pcm_stream_lock_irqsave(substream, flags);
1460 if (snd_pcm_running(substream) &&
1461 snd_pcm_update_hw_ptr(substream) >= 0)
1462 runtime->status->hw_ptr %= runtime->buffer_size;
1463 else
1464 runtime->status->hw_ptr = 0;
1465 snd_pcm_stream_unlock_irqrestore(substream, flags);
1466 return 0;
1469 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1470 void *arg)
1472 struct snd_pcm_channel_info *info = arg;
1473 struct snd_pcm_runtime *runtime = substream->runtime;
1474 int width;
1475 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1476 info->offset = -1;
1477 return 0;
1479 width = snd_pcm_format_physical_width(runtime->format);
1480 if (width < 0)
1481 return width;
1482 info->offset = 0;
1483 switch (runtime->access) {
1484 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1485 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1486 info->first = info->channel * width;
1487 info->step = runtime->channels * width;
1488 break;
1489 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1490 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1492 size_t size = runtime->dma_bytes / runtime->channels;
1493 info->first = info->channel * size * 8;
1494 info->step = width;
1495 break;
1497 default:
1498 snd_BUG();
1499 break;
1501 return 0;
1505 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1506 * @substream: the pcm substream instance
1507 * @cmd: ioctl command
1508 * @arg: ioctl argument
1510 * Processes the generic ioctl commands for PCM.
1511 * Can be passed as the ioctl callback for PCM ops.
1513 * Returns zero if successful, or a negative error code on failure.
1515 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1516 unsigned int cmd, void *arg)
1518 switch (cmd) {
1519 case SNDRV_PCM_IOCTL1_INFO:
1520 return 0;
1521 case SNDRV_PCM_IOCTL1_RESET:
1522 return snd_pcm_lib_ioctl_reset(substream, arg);
1523 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1524 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1526 return -ENXIO;
1529 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1532 * snd_pcm_period_elapsed - update the pcm status for the next period
1533 * @substream: the pcm substream instance
1535 * This function is called from the interrupt handler when the
1536 * PCM has processed the period size. It will update the current
1537 * pointer, wake up sleepers, etc.
1539 * Even if more than one periods have elapsed since the last call, you
1540 * have to call this only once.
1542 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1544 struct snd_pcm_runtime *runtime;
1545 unsigned long flags;
1547 if (PCM_RUNTIME_CHECK(substream))
1548 return;
1549 runtime = substream->runtime;
1551 if (runtime->transfer_ack_begin)
1552 runtime->transfer_ack_begin(substream);
1554 snd_pcm_stream_lock_irqsave(substream, flags);
1555 if (!snd_pcm_running(substream) ||
1556 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1557 goto _end;
1559 if (substream->timer_running)
1560 snd_timer_interrupt(substream->timer, 1);
1561 _end:
1562 snd_pcm_stream_unlock_irqrestore(substream, flags);
1563 if (runtime->transfer_ack_end)
1564 runtime->transfer_ack_end(substream);
1565 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1568 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1571 * Wait until avail_min data becomes available
1572 * Returns a negative error code if any error occurs during operation.
1573 * The available space is stored on availp. When err = 0 and avail = 0
1574 * on the capture stream, it indicates the stream is in DRAINING state.
1576 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1577 snd_pcm_uframes_t *availp)
1579 struct snd_pcm_runtime *runtime = substream->runtime;
1580 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1581 wait_queue_t wait;
1582 int err = 0;
1583 snd_pcm_uframes_t avail = 0;
1584 long tout;
1586 init_waitqueue_entry(&wait, current);
1587 add_wait_queue(&runtime->sleep, &wait);
1588 for (;;) {
1589 if (signal_pending(current)) {
1590 err = -ERESTARTSYS;
1591 break;
1593 set_current_state(TASK_INTERRUPTIBLE);
1594 snd_pcm_stream_unlock_irq(substream);
1595 tout = schedule_timeout(msecs_to_jiffies(10000));
1596 snd_pcm_stream_lock_irq(substream);
1597 switch (runtime->status->state) {
1598 case SNDRV_PCM_STATE_SUSPENDED:
1599 err = -ESTRPIPE;
1600 goto _endloop;
1601 case SNDRV_PCM_STATE_XRUN:
1602 err = -EPIPE;
1603 goto _endloop;
1604 case SNDRV_PCM_STATE_DRAINING:
1605 if (is_playback)
1606 err = -EPIPE;
1607 else
1608 avail = 0; /* indicate draining */
1609 goto _endloop;
1610 case SNDRV_PCM_STATE_OPEN:
1611 case SNDRV_PCM_STATE_SETUP:
1612 case SNDRV_PCM_STATE_DISCONNECTED:
1613 err = -EBADFD;
1614 goto _endloop;
1616 if (!tout) {
1617 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1618 is_playback ? "playback" : "capture");
1619 err = -EIO;
1620 break;
1622 if (is_playback)
1623 avail = snd_pcm_playback_avail(runtime);
1624 else
1625 avail = snd_pcm_capture_avail(runtime);
1626 if (avail >= runtime->control->avail_min)
1627 break;
1629 _endloop:
1630 remove_wait_queue(&runtime->sleep, &wait);
1631 *availp = avail;
1632 return err;
1635 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1636 unsigned int hwoff,
1637 unsigned long data, unsigned int off,
1638 snd_pcm_uframes_t frames)
1640 struct snd_pcm_runtime *runtime = substream->runtime;
1641 int err;
1642 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1643 if (substream->ops->copy) {
1644 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1645 return err;
1646 } else {
1647 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1648 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1649 return -EFAULT;
1651 return 0;
1654 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1655 unsigned long data, unsigned int off,
1656 snd_pcm_uframes_t size);
1658 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1659 unsigned long data,
1660 snd_pcm_uframes_t size,
1661 int nonblock,
1662 transfer_f transfer)
1664 struct snd_pcm_runtime *runtime = substream->runtime;
1665 snd_pcm_uframes_t xfer = 0;
1666 snd_pcm_uframes_t offset = 0;
1667 int err = 0;
1669 if (size == 0)
1670 return 0;
1672 snd_pcm_stream_lock_irq(substream);
1673 switch (runtime->status->state) {
1674 case SNDRV_PCM_STATE_PREPARED:
1675 case SNDRV_PCM_STATE_RUNNING:
1676 case SNDRV_PCM_STATE_PAUSED:
1677 break;
1678 case SNDRV_PCM_STATE_XRUN:
1679 err = -EPIPE;
1680 goto _end_unlock;
1681 case SNDRV_PCM_STATE_SUSPENDED:
1682 err = -ESTRPIPE;
1683 goto _end_unlock;
1684 default:
1685 err = -EBADFD;
1686 goto _end_unlock;
1689 while (size > 0) {
1690 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1691 snd_pcm_uframes_t avail;
1692 snd_pcm_uframes_t cont;
1693 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1694 snd_pcm_update_hw_ptr(substream);
1695 avail = snd_pcm_playback_avail(runtime);
1696 if (!avail) {
1697 if (nonblock) {
1698 err = -EAGAIN;
1699 goto _end_unlock;
1701 err = wait_for_avail_min(substream, &avail);
1702 if (err < 0)
1703 goto _end_unlock;
1705 frames = size > avail ? avail : size;
1706 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1707 if (frames > cont)
1708 frames = cont;
1709 if (snd_BUG_ON(!frames)) {
1710 snd_pcm_stream_unlock_irq(substream);
1711 return -EINVAL;
1713 appl_ptr = runtime->control->appl_ptr;
1714 appl_ofs = appl_ptr % runtime->buffer_size;
1715 snd_pcm_stream_unlock_irq(substream);
1716 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1717 goto _end;
1718 snd_pcm_stream_lock_irq(substream);
1719 switch (runtime->status->state) {
1720 case SNDRV_PCM_STATE_XRUN:
1721 err = -EPIPE;
1722 goto _end_unlock;
1723 case SNDRV_PCM_STATE_SUSPENDED:
1724 err = -ESTRPIPE;
1725 goto _end_unlock;
1726 default:
1727 break;
1729 appl_ptr += frames;
1730 if (appl_ptr >= runtime->boundary)
1731 appl_ptr -= runtime->boundary;
1732 runtime->control->appl_ptr = appl_ptr;
1733 if (substream->ops->ack)
1734 substream->ops->ack(substream);
1736 offset += frames;
1737 size -= frames;
1738 xfer += frames;
1739 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1740 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1741 err = snd_pcm_start(substream);
1742 if (err < 0)
1743 goto _end_unlock;
1746 _end_unlock:
1747 snd_pcm_stream_unlock_irq(substream);
1748 _end:
1749 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1752 /* sanity-check for read/write methods */
1753 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1755 struct snd_pcm_runtime *runtime;
1756 if (PCM_RUNTIME_CHECK(substream))
1757 return -ENXIO;
1758 runtime = substream->runtime;
1759 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1760 return -EINVAL;
1761 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1762 return -EBADFD;
1763 return 0;
1766 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1768 struct snd_pcm_runtime *runtime;
1769 int nonblock;
1770 int err;
1772 err = pcm_sanity_check(substream);
1773 if (err < 0)
1774 return err;
1775 runtime = substream->runtime;
1776 nonblock = !!(substream->f_flags & O_NONBLOCK);
1778 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1779 runtime->channels > 1)
1780 return -EINVAL;
1781 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1782 snd_pcm_lib_write_transfer);
1785 EXPORT_SYMBOL(snd_pcm_lib_write);
1787 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1788 unsigned int hwoff,
1789 unsigned long data, unsigned int off,
1790 snd_pcm_uframes_t frames)
1792 struct snd_pcm_runtime *runtime = substream->runtime;
1793 int err;
1794 void __user **bufs = (void __user **)data;
1795 int channels = runtime->channels;
1796 int c;
1797 if (substream->ops->copy) {
1798 if (snd_BUG_ON(!substream->ops->silence))
1799 return -EINVAL;
1800 for (c = 0; c < channels; ++c, ++bufs) {
1801 if (*bufs == NULL) {
1802 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1803 return err;
1804 } else {
1805 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1806 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1807 return err;
1810 } else {
1811 /* default transfer behaviour */
1812 size_t dma_csize = runtime->dma_bytes / channels;
1813 for (c = 0; c < channels; ++c, ++bufs) {
1814 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1815 if (*bufs == NULL) {
1816 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1817 } else {
1818 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1819 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1820 return -EFAULT;
1824 return 0;
1827 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1828 void __user **bufs,
1829 snd_pcm_uframes_t frames)
1831 struct snd_pcm_runtime *runtime;
1832 int nonblock;
1833 int err;
1835 err = pcm_sanity_check(substream);
1836 if (err < 0)
1837 return err;
1838 runtime = substream->runtime;
1839 nonblock = !!(substream->f_flags & O_NONBLOCK);
1841 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1842 return -EINVAL;
1843 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1844 nonblock, snd_pcm_lib_writev_transfer);
1847 EXPORT_SYMBOL(snd_pcm_lib_writev);
1849 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1850 unsigned int hwoff,
1851 unsigned long data, unsigned int off,
1852 snd_pcm_uframes_t frames)
1854 struct snd_pcm_runtime *runtime = substream->runtime;
1855 int err;
1856 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1857 if (substream->ops->copy) {
1858 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1859 return err;
1860 } else {
1861 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1862 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1863 return -EFAULT;
1865 return 0;
1868 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1869 unsigned long data,
1870 snd_pcm_uframes_t size,
1871 int nonblock,
1872 transfer_f transfer)
1874 struct snd_pcm_runtime *runtime = substream->runtime;
1875 snd_pcm_uframes_t xfer = 0;
1876 snd_pcm_uframes_t offset = 0;
1877 int err = 0;
1879 if (size == 0)
1880 return 0;
1882 snd_pcm_stream_lock_irq(substream);
1883 switch (runtime->status->state) {
1884 case SNDRV_PCM_STATE_PREPARED:
1885 if (size >= runtime->start_threshold) {
1886 err = snd_pcm_start(substream);
1887 if (err < 0)
1888 goto _end_unlock;
1890 break;
1891 case SNDRV_PCM_STATE_DRAINING:
1892 case SNDRV_PCM_STATE_RUNNING:
1893 case SNDRV_PCM_STATE_PAUSED:
1894 break;
1895 case SNDRV_PCM_STATE_XRUN:
1896 err = -EPIPE;
1897 goto _end_unlock;
1898 case SNDRV_PCM_STATE_SUSPENDED:
1899 err = -ESTRPIPE;
1900 goto _end_unlock;
1901 default:
1902 err = -EBADFD;
1903 goto _end_unlock;
1906 while (size > 0) {
1907 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1908 snd_pcm_uframes_t avail;
1909 snd_pcm_uframes_t cont;
1910 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1911 snd_pcm_update_hw_ptr(substream);
1912 avail = snd_pcm_capture_avail(runtime);
1913 if (!avail) {
1914 if (runtime->status->state ==
1915 SNDRV_PCM_STATE_DRAINING) {
1916 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1917 goto _end_unlock;
1919 if (nonblock) {
1920 err = -EAGAIN;
1921 goto _end_unlock;
1923 err = wait_for_avail_min(substream, &avail);
1924 if (err < 0)
1925 goto _end_unlock;
1926 if (!avail)
1927 continue; /* draining */
1929 frames = size > avail ? avail : size;
1930 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1931 if (frames > cont)
1932 frames = cont;
1933 if (snd_BUG_ON(!frames)) {
1934 snd_pcm_stream_unlock_irq(substream);
1935 return -EINVAL;
1937 appl_ptr = runtime->control->appl_ptr;
1938 appl_ofs = appl_ptr % runtime->buffer_size;
1939 snd_pcm_stream_unlock_irq(substream);
1940 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1941 goto _end;
1942 snd_pcm_stream_lock_irq(substream);
1943 switch (runtime->status->state) {
1944 case SNDRV_PCM_STATE_XRUN:
1945 err = -EPIPE;
1946 goto _end_unlock;
1947 case SNDRV_PCM_STATE_SUSPENDED:
1948 err = -ESTRPIPE;
1949 goto _end_unlock;
1950 default:
1951 break;
1953 appl_ptr += frames;
1954 if (appl_ptr >= runtime->boundary)
1955 appl_ptr -= runtime->boundary;
1956 runtime->control->appl_ptr = appl_ptr;
1957 if (substream->ops->ack)
1958 substream->ops->ack(substream);
1960 offset += frames;
1961 size -= frames;
1962 xfer += frames;
1964 _end_unlock:
1965 snd_pcm_stream_unlock_irq(substream);
1966 _end:
1967 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1970 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1972 struct snd_pcm_runtime *runtime;
1973 int nonblock;
1974 int err;
1976 err = pcm_sanity_check(substream);
1977 if (err < 0)
1978 return err;
1979 runtime = substream->runtime;
1980 nonblock = !!(substream->f_flags & O_NONBLOCK);
1981 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1982 return -EINVAL;
1983 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1986 EXPORT_SYMBOL(snd_pcm_lib_read);
1988 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1989 unsigned int hwoff,
1990 unsigned long data, unsigned int off,
1991 snd_pcm_uframes_t frames)
1993 struct snd_pcm_runtime *runtime = substream->runtime;
1994 int err;
1995 void __user **bufs = (void __user **)data;
1996 int channels = runtime->channels;
1997 int c;
1998 if (substream->ops->copy) {
1999 for (c = 0; c < channels; ++c, ++bufs) {
2000 char __user *buf;
2001 if (*bufs == NULL)
2002 continue;
2003 buf = *bufs + samples_to_bytes(runtime, off);
2004 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2005 return err;
2007 } else {
2008 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2009 for (c = 0; c < channels; ++c, ++bufs) {
2010 char *hwbuf;
2011 char __user *buf;
2012 if (*bufs == NULL)
2013 continue;
2015 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2016 buf = *bufs + samples_to_bytes(runtime, off);
2017 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2018 return -EFAULT;
2021 return 0;
2024 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2025 void __user **bufs,
2026 snd_pcm_uframes_t frames)
2028 struct snd_pcm_runtime *runtime;
2029 int nonblock;
2030 int err;
2032 err = pcm_sanity_check(substream);
2033 if (err < 0)
2034 return err;
2035 runtime = substream->runtime;
2036 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2037 return -EBADFD;
2039 nonblock = !!(substream->f_flags & O_NONBLOCK);
2040 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2041 return -EINVAL;
2042 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2045 EXPORT_SYMBOL(snd_pcm_lib_readv);