[ARM] lower overhead with alternative copy_to_user for small copies
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / pcm_lib.c
bloba2a792c18c40e18412ac7e9e015d22c6035b2958
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;
252 /* Skip the jiffies check for hardwares with BATCH flag.
253 * Such hardware usually just increases the position at each IRQ,
254 * thus it can't give any strange position.
256 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
257 goto no_jiffies_check;
258 hdelta = new_hw_ptr - old_hw_ptr;
259 jdelta = jiffies - runtime->hw_ptr_jiffies;
260 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
261 delta = jdelta /
262 (((runtime->period_size * HZ) / runtime->rate)
263 + HZ/100);
264 hw_ptr_error(substream,
265 "hw_ptr skipping! [Q] "
266 "(pos=%ld, delta=%ld, period=%ld, "
267 "jdelta=%lu/%lu/%lu)\n",
268 (long)pos, (long)hdelta,
269 (long)runtime->period_size, jdelta,
270 ((hdelta * HZ) / runtime->rate), delta);
271 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
272 runtime->period_size * delta;
273 if (hw_ptr_interrupt >= runtime->boundary)
274 hw_ptr_interrupt -= runtime->boundary;
275 /* rebase to interrupt position */
276 hw_base = new_hw_ptr = hw_ptr_interrupt;
277 /* align hw_base to buffer_size */
278 hw_base -= hw_base % runtime->buffer_size;
279 delta = 0;
281 no_jiffies_check:
282 if (delta > runtime->period_size + runtime->period_size / 2) {
283 hw_ptr_error(substream,
284 "Lost interrupts? "
285 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
286 substream->stream, (long)delta,
287 (long)hw_ptr_interrupt);
288 /* rebase hw_ptr_interrupt */
289 hw_ptr_interrupt =
290 new_hw_ptr - new_hw_ptr % runtime->period_size;
292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
293 runtime->silence_size > 0)
294 snd_pcm_playback_silence(substream, new_hw_ptr);
296 runtime->hw_ptr_base = hw_base;
297 runtime->status->hw_ptr = new_hw_ptr;
298 runtime->hw_ptr_jiffies = jiffies;
299 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
301 return snd_pcm_update_hw_ptr_post(substream, runtime);
304 /* CAUTION: call it with irq disabled */
305 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
307 struct snd_pcm_runtime *runtime = substream->runtime;
308 snd_pcm_uframes_t pos;
309 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
310 snd_pcm_sframes_t delta;
311 unsigned long jdelta;
313 old_hw_ptr = runtime->status->hw_ptr;
314 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
315 if (pos == SNDRV_PCM_POS_XRUN) {
316 xrun(substream);
317 return -EPIPE;
319 hw_base = runtime->hw_ptr_base;
320 new_hw_ptr = hw_base + pos;
322 delta = new_hw_ptr - old_hw_ptr;
323 jdelta = jiffies - runtime->hw_ptr_jiffies;
324 if (delta < 0) {
325 delta += runtime->buffer_size;
326 if (delta < 0) {
327 hw_ptr_error(substream,
328 "Unexpected hw_pointer value [2] "
329 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
330 substream->stream, (long)pos,
331 (long)old_hw_ptr, jdelta);
332 return 0;
334 hw_base += runtime->buffer_size;
335 if (hw_base >= runtime->boundary)
336 hw_base = 0;
337 new_hw_ptr = hw_base + pos;
339 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
340 hw_ptr_error(substream,
341 "hw_ptr skipping! "
342 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
343 (long)pos, (long)delta,
344 (long)runtime->period_size, jdelta,
345 ((delta * HZ) / runtime->rate));
346 return 0;
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
349 runtime->silence_size > 0)
350 snd_pcm_playback_silence(substream, new_hw_ptr);
352 runtime->hw_ptr_base = hw_base;
353 runtime->status->hw_ptr = new_hw_ptr;
354 runtime->hw_ptr_jiffies = jiffies;
356 return snd_pcm_update_hw_ptr_post(substream, runtime);
360 * snd_pcm_set_ops - set the PCM operators
361 * @pcm: the pcm instance
362 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
363 * @ops: the operator table
365 * Sets the given PCM operators to the pcm instance.
367 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
369 struct snd_pcm_str *stream = &pcm->streams[direction];
370 struct snd_pcm_substream *substream;
372 for (substream = stream->substream; substream != NULL; substream = substream->next)
373 substream->ops = ops;
376 EXPORT_SYMBOL(snd_pcm_set_ops);
379 * snd_pcm_sync - set the PCM sync id
380 * @substream: the pcm substream
382 * Sets the PCM sync identifier for the card.
384 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
386 struct snd_pcm_runtime *runtime = substream->runtime;
388 runtime->sync.id32[0] = substream->pcm->card->number;
389 runtime->sync.id32[1] = -1;
390 runtime->sync.id32[2] = -1;
391 runtime->sync.id32[3] = -1;
394 EXPORT_SYMBOL(snd_pcm_set_sync);
397 * Standard ioctl routine
400 static inline unsigned int div32(unsigned int a, unsigned int b,
401 unsigned int *r)
403 if (b == 0) {
404 *r = 0;
405 return UINT_MAX;
407 *r = a % b;
408 return a / b;
411 static inline unsigned int div_down(unsigned int a, unsigned int b)
413 if (b == 0)
414 return UINT_MAX;
415 return a / b;
418 static inline unsigned int div_up(unsigned int a, unsigned int b)
420 unsigned int r;
421 unsigned int q;
422 if (b == 0)
423 return UINT_MAX;
424 q = div32(a, b, &r);
425 if (r)
426 ++q;
427 return q;
430 static inline unsigned int mul(unsigned int a, unsigned int b)
432 if (a == 0)
433 return 0;
434 if (div_down(UINT_MAX, a) < b)
435 return UINT_MAX;
436 return a * b;
439 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
440 unsigned int c, unsigned int *r)
442 u_int64_t n = (u_int64_t) a * b;
443 if (c == 0) {
444 snd_BUG_ON(!n);
445 *r = 0;
446 return UINT_MAX;
448 div64_32(&n, c, r);
449 if (n >= UINT_MAX) {
450 *r = 0;
451 return UINT_MAX;
453 return n;
457 * snd_interval_refine - refine the interval value of configurator
458 * @i: the interval value to refine
459 * @v: the interval value to refer to
461 * Refines the interval value with the reference value.
462 * The interval is changed to the range satisfying both intervals.
463 * The interval status (min, max, integer, etc.) are evaluated.
465 * Returns non-zero if the value is changed, zero if not changed.
467 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
469 int changed = 0;
470 if (snd_BUG_ON(snd_interval_empty(i)))
471 return -EINVAL;
472 if (i->min < v->min) {
473 i->min = v->min;
474 i->openmin = v->openmin;
475 changed = 1;
476 } else if (i->min == v->min && !i->openmin && v->openmin) {
477 i->openmin = 1;
478 changed = 1;
480 if (i->max > v->max) {
481 i->max = v->max;
482 i->openmax = v->openmax;
483 changed = 1;
484 } else if (i->max == v->max && !i->openmax && v->openmax) {
485 i->openmax = 1;
486 changed = 1;
488 if (!i->integer && v->integer) {
489 i->integer = 1;
490 changed = 1;
492 if (i->integer) {
493 if (i->openmin) {
494 i->min++;
495 i->openmin = 0;
497 if (i->openmax) {
498 i->max--;
499 i->openmax = 0;
501 } else if (!i->openmin && !i->openmax && i->min == i->max)
502 i->integer = 1;
503 if (snd_interval_checkempty(i)) {
504 snd_interval_none(i);
505 return -EINVAL;
507 return changed;
510 EXPORT_SYMBOL(snd_interval_refine);
512 static int snd_interval_refine_first(struct snd_interval *i)
514 if (snd_BUG_ON(snd_interval_empty(i)))
515 return -EINVAL;
516 if (snd_interval_single(i))
517 return 0;
518 i->max = i->min;
519 i->openmax = i->openmin;
520 if (i->openmax)
521 i->max++;
522 return 1;
525 static int snd_interval_refine_last(struct snd_interval *i)
527 if (snd_BUG_ON(snd_interval_empty(i)))
528 return -EINVAL;
529 if (snd_interval_single(i))
530 return 0;
531 i->min = i->max;
532 i->openmin = i->openmax;
533 if (i->openmin)
534 i->min--;
535 return 1;
538 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
540 if (a->empty || b->empty) {
541 snd_interval_none(c);
542 return;
544 c->empty = 0;
545 c->min = mul(a->min, b->min);
546 c->openmin = (a->openmin || b->openmin);
547 c->max = mul(a->max, b->max);
548 c->openmax = (a->openmax || b->openmax);
549 c->integer = (a->integer && b->integer);
553 * snd_interval_div - refine the interval value with division
554 * @a: dividend
555 * @b: divisor
556 * @c: quotient
558 * c = a / b
560 * Returns non-zero if the value is changed, zero if not changed.
562 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
564 unsigned int r;
565 if (a->empty || b->empty) {
566 snd_interval_none(c);
567 return;
569 c->empty = 0;
570 c->min = div32(a->min, b->max, &r);
571 c->openmin = (r || a->openmin || b->openmax);
572 if (b->min > 0) {
573 c->max = div32(a->max, b->min, &r);
574 if (r) {
575 c->max++;
576 c->openmax = 1;
577 } else
578 c->openmax = (a->openmax || b->openmin);
579 } else {
580 c->max = UINT_MAX;
581 c->openmax = 0;
583 c->integer = 0;
587 * snd_interval_muldivk - refine the interval value
588 * @a: dividend 1
589 * @b: dividend 2
590 * @k: divisor (as integer)
591 * @c: result
593 * c = a * b / k
595 * Returns non-zero if the value is changed, zero if not changed.
597 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
598 unsigned int k, struct snd_interval *c)
600 unsigned int r;
601 if (a->empty || b->empty) {
602 snd_interval_none(c);
603 return;
605 c->empty = 0;
606 c->min = muldiv32(a->min, b->min, k, &r);
607 c->openmin = (r || a->openmin || b->openmin);
608 c->max = muldiv32(a->max, b->max, k, &r);
609 if (r) {
610 c->max++;
611 c->openmax = 1;
612 } else
613 c->openmax = (a->openmax || b->openmax);
614 c->integer = 0;
618 * snd_interval_mulkdiv - refine the interval value
619 * @a: dividend 1
620 * @k: dividend 2 (as integer)
621 * @b: divisor
622 * @c: result
624 * c = a * k / b
626 * Returns non-zero if the value is changed, zero if not changed.
628 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
629 const struct snd_interval *b, struct snd_interval *c)
631 unsigned int r;
632 if (a->empty || b->empty) {
633 snd_interval_none(c);
634 return;
636 c->empty = 0;
637 c->min = muldiv32(a->min, k, b->max, &r);
638 c->openmin = (r || a->openmin || b->openmax);
639 if (b->min > 0) {
640 c->max = muldiv32(a->max, k, b->min, &r);
641 if (r) {
642 c->max++;
643 c->openmax = 1;
644 } else
645 c->openmax = (a->openmax || b->openmin);
646 } else {
647 c->max = UINT_MAX;
648 c->openmax = 0;
650 c->integer = 0;
653 /* ---- */
657 * snd_interval_ratnum - refine the interval value
658 * @i: interval to refine
659 * @rats_count: number of ratnum_t
660 * @rats: ratnum_t array
661 * @nump: pointer to store the resultant numerator
662 * @denp: pointer to store the resultant denominator
664 * Returns non-zero if the value is changed, zero if not changed.
666 int snd_interval_ratnum(struct snd_interval *i,
667 unsigned int rats_count, struct snd_ratnum *rats,
668 unsigned int *nump, unsigned int *denp)
670 unsigned int best_num, best_diff, best_den;
671 unsigned int k;
672 struct snd_interval t;
673 int err;
675 best_num = best_den = best_diff = 0;
676 for (k = 0; k < rats_count; ++k) {
677 unsigned int num = rats[k].num;
678 unsigned int den;
679 unsigned int q = i->min;
680 int diff;
681 if (q == 0)
682 q = 1;
683 den = div_down(num, q);
684 if (den < rats[k].den_min)
685 continue;
686 if (den > rats[k].den_max)
687 den = rats[k].den_max;
688 else {
689 unsigned int r;
690 r = (den - rats[k].den_min) % rats[k].den_step;
691 if (r != 0)
692 den -= r;
694 diff = num - q * den;
695 if (best_num == 0 ||
696 diff * best_den < best_diff * den) {
697 best_diff = diff;
698 best_den = den;
699 best_num = num;
702 if (best_den == 0) {
703 i->empty = 1;
704 return -EINVAL;
706 t.min = div_down(best_num, best_den);
707 t.openmin = !!(best_num % best_den);
709 best_num = best_den = best_diff = 0;
710 for (k = 0; k < rats_count; ++k) {
711 unsigned int num = rats[k].num;
712 unsigned int den;
713 unsigned int q = i->max;
714 int diff;
715 if (q == 0) {
716 i->empty = 1;
717 return -EINVAL;
719 den = div_up(num, q);
720 if (den > rats[k].den_max)
721 continue;
722 if (den < rats[k].den_min)
723 den = rats[k].den_min;
724 else {
725 unsigned int r;
726 r = (den - rats[k].den_min) % rats[k].den_step;
727 if (r != 0)
728 den += rats[k].den_step - r;
730 diff = q * den - num;
731 if (best_num == 0 ||
732 diff * best_den < best_diff * den) {
733 best_diff = diff;
734 best_den = den;
735 best_num = num;
738 if (best_den == 0) {
739 i->empty = 1;
740 return -EINVAL;
742 t.max = div_up(best_num, best_den);
743 t.openmax = !!(best_num % best_den);
744 t.integer = 0;
745 err = snd_interval_refine(i, &t);
746 if (err < 0)
747 return err;
749 if (snd_interval_single(i)) {
750 if (nump)
751 *nump = best_num;
752 if (denp)
753 *denp = best_den;
755 return err;
758 EXPORT_SYMBOL(snd_interval_ratnum);
761 * snd_interval_ratden - refine the interval value
762 * @i: interval to refine
763 * @rats_count: number of struct ratden
764 * @rats: struct ratden array
765 * @nump: pointer to store the resultant numerator
766 * @denp: pointer to store the resultant denominator
768 * Returns non-zero if the value is changed, zero if not changed.
770 static int snd_interval_ratden(struct snd_interval *i,
771 unsigned int rats_count, struct snd_ratden *rats,
772 unsigned int *nump, unsigned int *denp)
774 unsigned int best_num, best_diff, best_den;
775 unsigned int k;
776 struct snd_interval t;
777 int err;
779 best_num = best_den = best_diff = 0;
780 for (k = 0; k < rats_count; ++k) {
781 unsigned int num;
782 unsigned int den = rats[k].den;
783 unsigned int q = i->min;
784 int diff;
785 num = mul(q, den);
786 if (num > rats[k].num_max)
787 continue;
788 if (num < rats[k].num_min)
789 num = rats[k].num_max;
790 else {
791 unsigned int r;
792 r = (num - rats[k].num_min) % rats[k].num_step;
793 if (r != 0)
794 num += rats[k].num_step - r;
796 diff = num - q * den;
797 if (best_num == 0 ||
798 diff * best_den < best_diff * den) {
799 best_diff = diff;
800 best_den = den;
801 best_num = num;
804 if (best_den == 0) {
805 i->empty = 1;
806 return -EINVAL;
808 t.min = div_down(best_num, best_den);
809 t.openmin = !!(best_num % best_den);
811 best_num = best_den = best_diff = 0;
812 for (k = 0; k < rats_count; ++k) {
813 unsigned int num;
814 unsigned int den = rats[k].den;
815 unsigned int q = i->max;
816 int diff;
817 num = mul(q, den);
818 if (num < rats[k].num_min)
819 continue;
820 if (num > rats[k].num_max)
821 num = rats[k].num_max;
822 else {
823 unsigned int r;
824 r = (num - rats[k].num_min) % rats[k].num_step;
825 if (r != 0)
826 num -= r;
828 diff = q * den - num;
829 if (best_num == 0 ||
830 diff * best_den < best_diff * den) {
831 best_diff = diff;
832 best_den = den;
833 best_num = num;
836 if (best_den == 0) {
837 i->empty = 1;
838 return -EINVAL;
840 t.max = div_up(best_num, best_den);
841 t.openmax = !!(best_num % best_den);
842 t.integer = 0;
843 err = snd_interval_refine(i, &t);
844 if (err < 0)
845 return err;
847 if (snd_interval_single(i)) {
848 if (nump)
849 *nump = best_num;
850 if (denp)
851 *denp = best_den;
853 return err;
857 * snd_interval_list - refine the interval value from the list
858 * @i: the interval value to refine
859 * @count: the number of elements in the list
860 * @list: the value list
861 * @mask: the bit-mask to evaluate
863 * Refines the interval value from the list.
864 * When mask is non-zero, only the elements corresponding to bit 1 are
865 * evaluated.
867 * Returns non-zero if the value is changed, zero if not changed.
869 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
871 unsigned int k;
872 int changed = 0;
874 if (!count) {
875 i->empty = 1;
876 return -EINVAL;
878 for (k = 0; k < count; k++) {
879 if (mask && !(mask & (1 << k)))
880 continue;
881 if (i->min == list[k] && !i->openmin)
882 goto _l1;
883 if (i->min < list[k]) {
884 i->min = list[k];
885 i->openmin = 0;
886 changed = 1;
887 goto _l1;
890 i->empty = 1;
891 return -EINVAL;
892 _l1:
893 for (k = count; k-- > 0;) {
894 if (mask && !(mask & (1 << k)))
895 continue;
896 if (i->max == list[k] && !i->openmax)
897 goto _l2;
898 if (i->max > list[k]) {
899 i->max = list[k];
900 i->openmax = 0;
901 changed = 1;
902 goto _l2;
905 i->empty = 1;
906 return -EINVAL;
907 _l2:
908 if (snd_interval_checkempty(i)) {
909 i->empty = 1;
910 return -EINVAL;
912 return changed;
915 EXPORT_SYMBOL(snd_interval_list);
917 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
919 unsigned int n;
920 int changed = 0;
921 n = (i->min - min) % step;
922 if (n != 0 || i->openmin) {
923 i->min += step - n;
924 changed = 1;
926 n = (i->max - min) % step;
927 if (n != 0 || i->openmax) {
928 i->max -= n;
929 changed = 1;
931 if (snd_interval_checkempty(i)) {
932 i->empty = 1;
933 return -EINVAL;
935 return changed;
938 /* Info constraints helpers */
941 * snd_pcm_hw_rule_add - add the hw-constraint rule
942 * @runtime: the pcm runtime instance
943 * @cond: condition bits
944 * @var: the variable to evaluate
945 * @func: the evaluation function
946 * @private: the private data pointer passed to function
947 * @dep: the dependent variables
949 * Returns zero if successful, or a negative error code on failure.
951 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
952 int var,
953 snd_pcm_hw_rule_func_t func, void *private,
954 int dep, ...)
956 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
957 struct snd_pcm_hw_rule *c;
958 unsigned int k;
959 va_list args;
960 va_start(args, dep);
961 if (constrs->rules_num >= constrs->rules_all) {
962 struct snd_pcm_hw_rule *new;
963 unsigned int new_rules = constrs->rules_all + 16;
964 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
965 if (!new)
966 return -ENOMEM;
967 if (constrs->rules) {
968 memcpy(new, constrs->rules,
969 constrs->rules_num * sizeof(*c));
970 kfree(constrs->rules);
972 constrs->rules = new;
973 constrs->rules_all = new_rules;
975 c = &constrs->rules[constrs->rules_num];
976 c->cond = cond;
977 c->func = func;
978 c->var = var;
979 c->private = private;
980 k = 0;
981 while (1) {
982 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
983 return -EINVAL;
984 c->deps[k++] = dep;
985 if (dep < 0)
986 break;
987 dep = va_arg(args, int);
989 constrs->rules_num++;
990 va_end(args);
991 return 0;
994 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
997 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
998 * @runtime: PCM runtime instance
999 * @var: hw_params variable to apply the mask
1000 * @mask: the bitmap mask
1002 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1004 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1005 u_int32_t mask)
1007 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1008 struct snd_mask *maskp = constrs_mask(constrs, var);
1009 *maskp->bits &= mask;
1010 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1011 if (*maskp->bits == 0)
1012 return -EINVAL;
1013 return 0;
1017 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1018 * @runtime: PCM runtime instance
1019 * @var: hw_params variable to apply the mask
1020 * @mask: the 64bit bitmap mask
1022 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1024 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1025 u_int64_t mask)
1027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_mask *maskp = constrs_mask(constrs, var);
1029 maskp->bits[0] &= (u_int32_t)mask;
1030 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1031 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1032 if (! maskp->bits[0] && ! maskp->bits[1])
1033 return -EINVAL;
1034 return 0;
1038 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1039 * @runtime: PCM runtime instance
1040 * @var: hw_params variable to apply the integer constraint
1042 * Apply the constraint of integer to an interval parameter.
1044 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1046 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1047 return snd_interval_setinteger(constrs_interval(constrs, var));
1050 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1053 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1054 * @runtime: PCM runtime instance
1055 * @var: hw_params variable to apply the range
1056 * @min: the minimal value
1057 * @max: the maximal value
1059 * Apply the min/max range constraint to an interval parameter.
1061 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1062 unsigned int min, unsigned int max)
1064 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1065 struct snd_interval t;
1066 t.min = min;
1067 t.max = max;
1068 t.openmin = t.openmax = 0;
1069 t.integer = 0;
1070 return snd_interval_refine(constrs_interval(constrs, var), &t);
1073 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1075 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1076 struct snd_pcm_hw_rule *rule)
1078 struct snd_pcm_hw_constraint_list *list = rule->private;
1079 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1084 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1085 * @runtime: PCM runtime instance
1086 * @cond: condition bits
1087 * @var: hw_params variable to apply the list constraint
1088 * @l: list
1090 * Apply the list of constraints to an interval parameter.
1092 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1093 unsigned int cond,
1094 snd_pcm_hw_param_t var,
1095 struct snd_pcm_hw_constraint_list *l)
1097 return snd_pcm_hw_rule_add(runtime, cond, var,
1098 snd_pcm_hw_rule_list, l,
1099 var, -1);
1102 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1104 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1105 struct snd_pcm_hw_rule *rule)
1107 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1108 unsigned int num = 0, den = 0;
1109 int err;
1110 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1111 r->nrats, r->rats, &num, &den);
1112 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1113 params->rate_num = num;
1114 params->rate_den = den;
1116 return err;
1120 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1121 * @runtime: PCM runtime instance
1122 * @cond: condition bits
1123 * @var: hw_params variable to apply the ratnums constraint
1124 * @r: struct snd_ratnums constriants
1126 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1127 unsigned int cond,
1128 snd_pcm_hw_param_t var,
1129 struct snd_pcm_hw_constraint_ratnums *r)
1131 return snd_pcm_hw_rule_add(runtime, cond, var,
1132 snd_pcm_hw_rule_ratnums, r,
1133 var, -1);
1136 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1138 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1139 struct snd_pcm_hw_rule *rule)
1141 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1142 unsigned int num = 0, den = 0;
1143 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1144 r->nrats, r->rats, &num, &den);
1145 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1146 params->rate_num = num;
1147 params->rate_den = den;
1149 return err;
1153 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the ratdens constraint
1157 * @r: struct snd_ratdens constriants
1159 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1160 unsigned int cond,
1161 snd_pcm_hw_param_t var,
1162 struct snd_pcm_hw_constraint_ratdens *r)
1164 return snd_pcm_hw_rule_add(runtime, cond, var,
1165 snd_pcm_hw_rule_ratdens, r,
1166 var, -1);
1169 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1171 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1172 struct snd_pcm_hw_rule *rule)
1174 unsigned int l = (unsigned long) rule->private;
1175 int width = l & 0xffff;
1176 unsigned int msbits = l >> 16;
1177 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1178 if (snd_interval_single(i) && snd_interval_value(i) == width)
1179 params->msbits = msbits;
1180 return 0;
1184 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1185 * @runtime: PCM runtime instance
1186 * @cond: condition bits
1187 * @width: sample bits width
1188 * @msbits: msbits width
1190 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1191 unsigned int cond,
1192 unsigned int width,
1193 unsigned int msbits)
1195 unsigned long l = (msbits << 16) | width;
1196 return snd_pcm_hw_rule_add(runtime, cond, -1,
1197 snd_pcm_hw_rule_msbits,
1198 (void*) l,
1199 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1202 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1204 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1205 struct snd_pcm_hw_rule *rule)
1207 unsigned long step = (unsigned long) rule->private;
1208 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1212 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1213 * @runtime: PCM runtime instance
1214 * @cond: condition bits
1215 * @var: hw_params variable to apply the step constraint
1216 * @step: step size
1218 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1219 unsigned int cond,
1220 snd_pcm_hw_param_t var,
1221 unsigned long step)
1223 return snd_pcm_hw_rule_add(runtime, cond, var,
1224 snd_pcm_hw_rule_step, (void *) step,
1225 var, -1);
1228 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1230 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1232 static unsigned int pow2_sizes[] = {
1233 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1234 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1235 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1236 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1238 return snd_interval_list(hw_param_interval(params, rule->var),
1239 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1243 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1244 * @runtime: PCM runtime instance
1245 * @cond: condition bits
1246 * @var: hw_params variable to apply the power-of-2 constraint
1248 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1249 unsigned int cond,
1250 snd_pcm_hw_param_t var)
1252 return snd_pcm_hw_rule_add(runtime, cond, var,
1253 snd_pcm_hw_rule_pow2, NULL,
1254 var, -1);
1257 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1259 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1260 snd_pcm_hw_param_t var)
1262 if (hw_is_mask(var)) {
1263 snd_mask_any(hw_param_mask(params, var));
1264 params->cmask |= 1 << var;
1265 params->rmask |= 1 << var;
1266 return;
1268 if (hw_is_interval(var)) {
1269 snd_interval_any(hw_param_interval(params, var));
1270 params->cmask |= 1 << var;
1271 params->rmask |= 1 << var;
1272 return;
1274 snd_BUG();
1277 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1279 unsigned int k;
1280 memset(params, 0, sizeof(*params));
1281 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1282 _snd_pcm_hw_param_any(params, k);
1283 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1284 _snd_pcm_hw_param_any(params, k);
1285 params->info = ~0U;
1288 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1291 * snd_pcm_hw_param_value - return @params field @var value
1292 * @params: the hw_params instance
1293 * @var: parameter to retrieve
1294 * @dir: pointer to the direction (-1,0,1) or %NULL
1296 * Return the value for field @var if it's fixed in configuration space
1297 * defined by @params. Return -%EINVAL otherwise.
1299 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1300 snd_pcm_hw_param_t var, int *dir)
1302 if (hw_is_mask(var)) {
1303 const struct snd_mask *mask = hw_param_mask_c(params, var);
1304 if (!snd_mask_single(mask))
1305 return -EINVAL;
1306 if (dir)
1307 *dir = 0;
1308 return snd_mask_value(mask);
1310 if (hw_is_interval(var)) {
1311 const struct snd_interval *i = hw_param_interval_c(params, var);
1312 if (!snd_interval_single(i))
1313 return -EINVAL;
1314 if (dir)
1315 *dir = i->openmin;
1316 return snd_interval_value(i);
1318 return -EINVAL;
1321 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1323 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1324 snd_pcm_hw_param_t var)
1326 if (hw_is_mask(var)) {
1327 snd_mask_none(hw_param_mask(params, var));
1328 params->cmask |= 1 << var;
1329 params->rmask |= 1 << var;
1330 } else if (hw_is_interval(var)) {
1331 snd_interval_none(hw_param_interval(params, var));
1332 params->cmask |= 1 << var;
1333 params->rmask |= 1 << var;
1334 } else {
1335 snd_BUG();
1339 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1341 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1342 snd_pcm_hw_param_t var)
1344 int changed;
1345 if (hw_is_mask(var))
1346 changed = snd_mask_refine_first(hw_param_mask(params, var));
1347 else if (hw_is_interval(var))
1348 changed = snd_interval_refine_first(hw_param_interval(params, var));
1349 else
1350 return -EINVAL;
1351 if (changed) {
1352 params->cmask |= 1 << var;
1353 params->rmask |= 1 << var;
1355 return changed;
1360 * snd_pcm_hw_param_first - refine config space and return minimum value
1361 * @pcm: PCM instance
1362 * @params: the hw_params instance
1363 * @var: parameter to retrieve
1364 * @dir: pointer to the direction (-1,0,1) or %NULL
1366 * Inside configuration space defined by @params remove from @var all
1367 * values > minimum. Reduce configuration space accordingly.
1368 * Return the minimum.
1370 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1371 struct snd_pcm_hw_params *params,
1372 snd_pcm_hw_param_t var, int *dir)
1374 int changed = _snd_pcm_hw_param_first(params, var);
1375 if (changed < 0)
1376 return changed;
1377 if (params->rmask) {
1378 int err = snd_pcm_hw_refine(pcm, params);
1379 if (snd_BUG_ON(err < 0))
1380 return err;
1382 return snd_pcm_hw_param_value(params, var, dir);
1385 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1387 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1388 snd_pcm_hw_param_t var)
1390 int changed;
1391 if (hw_is_mask(var))
1392 changed = snd_mask_refine_last(hw_param_mask(params, var));
1393 else if (hw_is_interval(var))
1394 changed = snd_interval_refine_last(hw_param_interval(params, var));
1395 else
1396 return -EINVAL;
1397 if (changed) {
1398 params->cmask |= 1 << var;
1399 params->rmask |= 1 << var;
1401 return changed;
1406 * snd_pcm_hw_param_last - refine config space and return maximum value
1407 * @pcm: PCM instance
1408 * @params: the hw_params instance
1409 * @var: parameter to retrieve
1410 * @dir: pointer to the direction (-1,0,1) or %NULL
1412 * Inside configuration space defined by @params remove from @var all
1413 * values < maximum. Reduce configuration space accordingly.
1414 * Return the maximum.
1416 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1417 struct snd_pcm_hw_params *params,
1418 snd_pcm_hw_param_t var, int *dir)
1420 int changed = _snd_pcm_hw_param_last(params, var);
1421 if (changed < 0)
1422 return changed;
1423 if (params->rmask) {
1424 int err = snd_pcm_hw_refine(pcm, params);
1425 if (snd_BUG_ON(err < 0))
1426 return err;
1428 return snd_pcm_hw_param_value(params, var, dir);
1431 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1434 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1435 * @pcm: PCM instance
1436 * @params: the hw_params instance
1438 * Choose one configuration from configuration space defined by @params.
1439 * The configuration chosen is that obtained fixing in this order:
1440 * first access, first format, first subformat, min channels,
1441 * min rate, min period time, max buffer size, min tick time
1443 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1444 struct snd_pcm_hw_params *params)
1446 static int vars[] = {
1447 SNDRV_PCM_HW_PARAM_ACCESS,
1448 SNDRV_PCM_HW_PARAM_FORMAT,
1449 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1450 SNDRV_PCM_HW_PARAM_CHANNELS,
1451 SNDRV_PCM_HW_PARAM_RATE,
1452 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1453 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1454 SNDRV_PCM_HW_PARAM_TICK_TIME,
1457 int err, *v;
1459 for (v = vars; *v != -1; v++) {
1460 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1461 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1462 else
1463 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1464 if (snd_BUG_ON(err < 0))
1465 return err;
1467 return 0;
1470 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1471 void *arg)
1473 struct snd_pcm_runtime *runtime = substream->runtime;
1474 unsigned long flags;
1475 snd_pcm_stream_lock_irqsave(substream, flags);
1476 if (snd_pcm_running(substream) &&
1477 snd_pcm_update_hw_ptr(substream) >= 0)
1478 runtime->status->hw_ptr %= runtime->buffer_size;
1479 else
1480 runtime->status->hw_ptr = 0;
1481 runtime->hw_ptr_jiffies = jiffies;
1482 snd_pcm_stream_unlock_irqrestore(substream, flags);
1483 return 0;
1486 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1487 void *arg)
1489 struct snd_pcm_channel_info *info = arg;
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 int width;
1492 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1493 info->offset = -1;
1494 return 0;
1496 width = snd_pcm_format_physical_width(runtime->format);
1497 if (width < 0)
1498 return width;
1499 info->offset = 0;
1500 switch (runtime->access) {
1501 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1502 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1503 info->first = info->channel * width;
1504 info->step = runtime->channels * width;
1505 break;
1506 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1507 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1509 size_t size = runtime->dma_bytes / runtime->channels;
1510 info->first = info->channel * size * 8;
1511 info->step = width;
1512 break;
1514 default:
1515 snd_BUG();
1516 break;
1518 return 0;
1522 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1523 * @substream: the pcm substream instance
1524 * @cmd: ioctl command
1525 * @arg: ioctl argument
1527 * Processes the generic ioctl commands for PCM.
1528 * Can be passed as the ioctl callback for PCM ops.
1530 * Returns zero if successful, or a negative error code on failure.
1532 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1533 unsigned int cmd, void *arg)
1535 switch (cmd) {
1536 case SNDRV_PCM_IOCTL1_INFO:
1537 return 0;
1538 case SNDRV_PCM_IOCTL1_RESET:
1539 return snd_pcm_lib_ioctl_reset(substream, arg);
1540 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1541 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1543 return -ENXIO;
1546 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1549 * snd_pcm_period_elapsed - update the pcm status for the next period
1550 * @substream: the pcm substream instance
1552 * This function is called from the interrupt handler when the
1553 * PCM has processed the period size. It will update the current
1554 * pointer, wake up sleepers, etc.
1556 * Even if more than one periods have elapsed since the last call, you
1557 * have to call this only once.
1559 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1561 struct snd_pcm_runtime *runtime;
1562 unsigned long flags;
1564 if (PCM_RUNTIME_CHECK(substream))
1565 return;
1566 runtime = substream->runtime;
1568 if (runtime->transfer_ack_begin)
1569 runtime->transfer_ack_begin(substream);
1571 snd_pcm_stream_lock_irqsave(substream, flags);
1572 if (!snd_pcm_running(substream) ||
1573 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1574 goto _end;
1576 if (substream->timer_running)
1577 snd_timer_interrupt(substream->timer, 1);
1578 _end:
1579 snd_pcm_stream_unlock_irqrestore(substream, flags);
1580 if (runtime->transfer_ack_end)
1581 runtime->transfer_ack_end(substream);
1582 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1585 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1588 * Wait until avail_min data becomes available
1589 * Returns a negative error code if any error occurs during operation.
1590 * The available space is stored on availp. When err = 0 and avail = 0
1591 * on the capture stream, it indicates the stream is in DRAINING state.
1593 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1594 snd_pcm_uframes_t *availp)
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1598 wait_queue_t wait;
1599 int err = 0;
1600 snd_pcm_uframes_t avail = 0;
1601 long tout;
1603 init_waitqueue_entry(&wait, current);
1604 add_wait_queue(&runtime->sleep, &wait);
1605 for (;;) {
1606 if (signal_pending(current)) {
1607 err = -ERESTARTSYS;
1608 break;
1610 set_current_state(TASK_INTERRUPTIBLE);
1611 snd_pcm_stream_unlock_irq(substream);
1612 tout = schedule_timeout(msecs_to_jiffies(10000));
1613 snd_pcm_stream_lock_irq(substream);
1614 switch (runtime->status->state) {
1615 case SNDRV_PCM_STATE_SUSPENDED:
1616 err = -ESTRPIPE;
1617 goto _endloop;
1618 case SNDRV_PCM_STATE_XRUN:
1619 err = -EPIPE;
1620 goto _endloop;
1621 case SNDRV_PCM_STATE_DRAINING:
1622 if (is_playback)
1623 err = -EPIPE;
1624 else
1625 avail = 0; /* indicate draining */
1626 goto _endloop;
1627 case SNDRV_PCM_STATE_OPEN:
1628 case SNDRV_PCM_STATE_SETUP:
1629 case SNDRV_PCM_STATE_DISCONNECTED:
1630 err = -EBADFD;
1631 goto _endloop;
1633 if (!tout) {
1634 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1635 is_playback ? "playback" : "capture");
1636 err = -EIO;
1637 break;
1639 if (is_playback)
1640 avail = snd_pcm_playback_avail(runtime);
1641 else
1642 avail = snd_pcm_capture_avail(runtime);
1643 if (avail >= runtime->control->avail_min)
1644 break;
1646 _endloop:
1647 remove_wait_queue(&runtime->sleep, &wait);
1648 *availp = avail;
1649 return err;
1652 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1653 unsigned int hwoff,
1654 unsigned long data, unsigned int off,
1655 snd_pcm_uframes_t frames)
1657 struct snd_pcm_runtime *runtime = substream->runtime;
1658 int err;
1659 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1660 if (substream->ops->copy) {
1661 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1662 return err;
1663 } else {
1664 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1665 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1666 return -EFAULT;
1668 return 0;
1671 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1672 unsigned long data, unsigned int off,
1673 snd_pcm_uframes_t size);
1675 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1676 unsigned long data,
1677 snd_pcm_uframes_t size,
1678 int nonblock,
1679 transfer_f transfer)
1681 struct snd_pcm_runtime *runtime = substream->runtime;
1682 snd_pcm_uframes_t xfer = 0;
1683 snd_pcm_uframes_t offset = 0;
1684 int err = 0;
1686 if (size == 0)
1687 return 0;
1689 snd_pcm_stream_lock_irq(substream);
1690 switch (runtime->status->state) {
1691 case SNDRV_PCM_STATE_PREPARED:
1692 case SNDRV_PCM_STATE_RUNNING:
1693 case SNDRV_PCM_STATE_PAUSED:
1694 break;
1695 case SNDRV_PCM_STATE_XRUN:
1696 err = -EPIPE;
1697 goto _end_unlock;
1698 case SNDRV_PCM_STATE_SUSPENDED:
1699 err = -ESTRPIPE;
1700 goto _end_unlock;
1701 default:
1702 err = -EBADFD;
1703 goto _end_unlock;
1706 while (size > 0) {
1707 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1708 snd_pcm_uframes_t avail;
1709 snd_pcm_uframes_t cont;
1710 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1711 snd_pcm_update_hw_ptr(substream);
1712 avail = snd_pcm_playback_avail(runtime);
1713 if (!avail) {
1714 if (nonblock) {
1715 err = -EAGAIN;
1716 goto _end_unlock;
1718 err = wait_for_avail_min(substream, &avail);
1719 if (err < 0)
1720 goto _end_unlock;
1722 frames = size > avail ? avail : size;
1723 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1724 if (frames > cont)
1725 frames = cont;
1726 if (snd_BUG_ON(!frames)) {
1727 snd_pcm_stream_unlock_irq(substream);
1728 return -EINVAL;
1730 appl_ptr = runtime->control->appl_ptr;
1731 appl_ofs = appl_ptr % runtime->buffer_size;
1732 snd_pcm_stream_unlock_irq(substream);
1733 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1734 goto _end;
1735 snd_pcm_stream_lock_irq(substream);
1736 switch (runtime->status->state) {
1737 case SNDRV_PCM_STATE_XRUN:
1738 err = -EPIPE;
1739 goto _end_unlock;
1740 case SNDRV_PCM_STATE_SUSPENDED:
1741 err = -ESTRPIPE;
1742 goto _end_unlock;
1743 default:
1744 break;
1746 appl_ptr += frames;
1747 if (appl_ptr >= runtime->boundary)
1748 appl_ptr -= runtime->boundary;
1749 runtime->control->appl_ptr = appl_ptr;
1750 if (substream->ops->ack)
1751 substream->ops->ack(substream);
1753 offset += frames;
1754 size -= frames;
1755 xfer += frames;
1756 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1757 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1758 err = snd_pcm_start(substream);
1759 if (err < 0)
1760 goto _end_unlock;
1763 _end_unlock:
1764 snd_pcm_stream_unlock_irq(substream);
1765 _end:
1766 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1769 /* sanity-check for read/write methods */
1770 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1772 struct snd_pcm_runtime *runtime;
1773 if (PCM_RUNTIME_CHECK(substream))
1774 return -ENXIO;
1775 runtime = substream->runtime;
1776 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1777 return -EINVAL;
1778 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1779 return -EBADFD;
1780 return 0;
1783 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1785 struct snd_pcm_runtime *runtime;
1786 int nonblock;
1787 int err;
1789 err = pcm_sanity_check(substream);
1790 if (err < 0)
1791 return err;
1792 runtime = substream->runtime;
1793 nonblock = !!(substream->f_flags & O_NONBLOCK);
1795 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1796 runtime->channels > 1)
1797 return -EINVAL;
1798 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1799 snd_pcm_lib_write_transfer);
1802 EXPORT_SYMBOL(snd_pcm_lib_write);
1804 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1805 unsigned int hwoff,
1806 unsigned long data, unsigned int off,
1807 snd_pcm_uframes_t frames)
1809 struct snd_pcm_runtime *runtime = substream->runtime;
1810 int err;
1811 void __user **bufs = (void __user **)data;
1812 int channels = runtime->channels;
1813 int c;
1814 if (substream->ops->copy) {
1815 if (snd_BUG_ON(!substream->ops->silence))
1816 return -EINVAL;
1817 for (c = 0; c < channels; ++c, ++bufs) {
1818 if (*bufs == NULL) {
1819 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1820 return err;
1821 } else {
1822 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1823 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1824 return err;
1827 } else {
1828 /* default transfer behaviour */
1829 size_t dma_csize = runtime->dma_bytes / channels;
1830 for (c = 0; c < channels; ++c, ++bufs) {
1831 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1832 if (*bufs == NULL) {
1833 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1834 } else {
1835 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1836 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1837 return -EFAULT;
1841 return 0;
1844 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1845 void __user **bufs,
1846 snd_pcm_uframes_t frames)
1848 struct snd_pcm_runtime *runtime;
1849 int nonblock;
1850 int err;
1852 err = pcm_sanity_check(substream);
1853 if (err < 0)
1854 return err;
1855 runtime = substream->runtime;
1856 nonblock = !!(substream->f_flags & O_NONBLOCK);
1858 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1859 return -EINVAL;
1860 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1861 nonblock, snd_pcm_lib_writev_transfer);
1864 EXPORT_SYMBOL(snd_pcm_lib_writev);
1866 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1867 unsigned int hwoff,
1868 unsigned long data, unsigned int off,
1869 snd_pcm_uframes_t frames)
1871 struct snd_pcm_runtime *runtime = substream->runtime;
1872 int err;
1873 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1874 if (substream->ops->copy) {
1875 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1876 return err;
1877 } else {
1878 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1879 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1880 return -EFAULT;
1882 return 0;
1885 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1886 unsigned long data,
1887 snd_pcm_uframes_t size,
1888 int nonblock,
1889 transfer_f transfer)
1891 struct snd_pcm_runtime *runtime = substream->runtime;
1892 snd_pcm_uframes_t xfer = 0;
1893 snd_pcm_uframes_t offset = 0;
1894 int err = 0;
1896 if (size == 0)
1897 return 0;
1899 snd_pcm_stream_lock_irq(substream);
1900 switch (runtime->status->state) {
1901 case SNDRV_PCM_STATE_PREPARED:
1902 if (size >= runtime->start_threshold) {
1903 err = snd_pcm_start(substream);
1904 if (err < 0)
1905 goto _end_unlock;
1907 break;
1908 case SNDRV_PCM_STATE_DRAINING:
1909 case SNDRV_PCM_STATE_RUNNING:
1910 case SNDRV_PCM_STATE_PAUSED:
1911 break;
1912 case SNDRV_PCM_STATE_XRUN:
1913 err = -EPIPE;
1914 goto _end_unlock;
1915 case SNDRV_PCM_STATE_SUSPENDED:
1916 err = -ESTRPIPE;
1917 goto _end_unlock;
1918 default:
1919 err = -EBADFD;
1920 goto _end_unlock;
1923 while (size > 0) {
1924 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1925 snd_pcm_uframes_t avail;
1926 snd_pcm_uframes_t cont;
1927 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1928 snd_pcm_update_hw_ptr(substream);
1929 avail = snd_pcm_capture_avail(runtime);
1930 if (!avail) {
1931 if (runtime->status->state ==
1932 SNDRV_PCM_STATE_DRAINING) {
1933 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1934 goto _end_unlock;
1936 if (nonblock) {
1937 err = -EAGAIN;
1938 goto _end_unlock;
1940 err = wait_for_avail_min(substream, &avail);
1941 if (err < 0)
1942 goto _end_unlock;
1943 if (!avail)
1944 continue; /* draining */
1946 frames = size > avail ? avail : size;
1947 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1948 if (frames > cont)
1949 frames = cont;
1950 if (snd_BUG_ON(!frames)) {
1951 snd_pcm_stream_unlock_irq(substream);
1952 return -EINVAL;
1954 appl_ptr = runtime->control->appl_ptr;
1955 appl_ofs = appl_ptr % runtime->buffer_size;
1956 snd_pcm_stream_unlock_irq(substream);
1957 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1958 goto _end;
1959 snd_pcm_stream_lock_irq(substream);
1960 switch (runtime->status->state) {
1961 case SNDRV_PCM_STATE_XRUN:
1962 err = -EPIPE;
1963 goto _end_unlock;
1964 case SNDRV_PCM_STATE_SUSPENDED:
1965 err = -ESTRPIPE;
1966 goto _end_unlock;
1967 default:
1968 break;
1970 appl_ptr += frames;
1971 if (appl_ptr >= runtime->boundary)
1972 appl_ptr -= runtime->boundary;
1973 runtime->control->appl_ptr = appl_ptr;
1974 if (substream->ops->ack)
1975 substream->ops->ack(substream);
1977 offset += frames;
1978 size -= frames;
1979 xfer += frames;
1981 _end_unlock:
1982 snd_pcm_stream_unlock_irq(substream);
1983 _end:
1984 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1987 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1989 struct snd_pcm_runtime *runtime;
1990 int nonblock;
1991 int err;
1993 err = pcm_sanity_check(substream);
1994 if (err < 0)
1995 return err;
1996 runtime = substream->runtime;
1997 nonblock = !!(substream->f_flags & O_NONBLOCK);
1998 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1999 return -EINVAL;
2000 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2003 EXPORT_SYMBOL(snd_pcm_lib_read);
2005 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2006 unsigned int hwoff,
2007 unsigned long data, unsigned int off,
2008 snd_pcm_uframes_t frames)
2010 struct snd_pcm_runtime *runtime = substream->runtime;
2011 int err;
2012 void __user **bufs = (void __user **)data;
2013 int channels = runtime->channels;
2014 int c;
2015 if (substream->ops->copy) {
2016 for (c = 0; c < channels; ++c, ++bufs) {
2017 char __user *buf;
2018 if (*bufs == NULL)
2019 continue;
2020 buf = *bufs + samples_to_bytes(runtime, off);
2021 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2022 return err;
2024 } else {
2025 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2026 for (c = 0; c < channels; ++c, ++bufs) {
2027 char *hwbuf;
2028 char __user *buf;
2029 if (*bufs == NULL)
2030 continue;
2032 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2033 buf = *bufs + samples_to_bytes(runtime, off);
2034 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2035 return -EFAULT;
2038 return 0;
2041 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2042 void __user **bufs,
2043 snd_pcm_uframes_t frames)
2045 struct snd_pcm_runtime *runtime;
2046 int nonblock;
2047 int err;
2049 err = pcm_sanity_check(substream);
2050 if (err < 0)
2051 return err;
2052 runtime = substream->runtime;
2053 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2054 return -EBADFD;
2056 nonblock = !!(substream->f_flags & O_NONBLOCK);
2057 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2058 return -EINVAL;
2059 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2062 EXPORT_SYMBOL(snd_pcm_lib_readv);