ALSA: refine rate selection in snd_interval_ratnum()
[linux-2.6/libata-dev.git] / sound / core / pcm_lib.c
blobb07cc361afb115fb2200ddea04488eaf7069f7cb
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 <linux/math64.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
42 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
44 struct snd_pcm_runtime *runtime = substream->runtime;
45 snd_pcm_uframes_t frames, ofs, transfer;
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
51 if (n < 0)
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
55 else
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
59 if (runtime->silence_filled >= runtime->buffer_size)
60 return;
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 return;
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
67 } else {
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
73 runtime->boundary;
74 } else {
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
82 runtime->silence_start = new_hw_ptr;
83 } else {
84 runtime->silence_start = ofs;
87 frames = runtime->buffer_size - runtime->silence_filled;
89 if (snd_BUG_ON(frames > runtime->buffer_size))
90 return;
91 if (frames == 0)
92 return;
93 ofs = runtime->silence_start % runtime->buffer_size;
94 while (frames > 0) {
95 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
96 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
97 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
98 if (substream->ops->silence) {
99 int err;
100 err = substream->ops->silence(substream, -1, ofs, transfer);
101 snd_BUG_ON(err < 0);
102 } else {
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
106 } else {
107 unsigned int c;
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
111 int err;
112 err = substream->ops->silence(substream, c, ofs, transfer);
113 snd_BUG_ON(err < 0);
115 } else {
116 size_t dma_csize = runtime->dma_bytes / channels;
117 for (c = 0; c < channels; ++c) {
118 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
119 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
123 runtime->silence_filled += transfer;
124 frames -= transfer;
125 ofs = 0;
129 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
130 #define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
131 #else
132 #define xrun_debug(substream, mask) 0
133 #endif
135 #define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
137 dump_stack(); \
138 } while (0)
140 static void pcm_debug_name(struct snd_pcm_substream *substream,
141 char *name, size_t len)
143 snprintf(name, len, "pcmC%dD%d%c:%d",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p',
147 substream->number);
150 static void xrun(struct snd_pcm_substream *substream)
152 struct snd_pcm_runtime *runtime = substream->runtime;
154 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
155 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
156 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
157 if (xrun_debug(substream, 1)) {
158 char name[16];
159 pcm_debug_name(substream, name, sizeof(name));
160 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
161 dump_stack_on_xrun(substream);
165 static snd_pcm_uframes_t
166 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
167 struct snd_pcm_runtime *runtime)
169 snd_pcm_uframes_t pos;
171 pos = substream->ops->pointer(substream);
172 if (pos == SNDRV_PCM_POS_XRUN)
173 return pos; /* XRUN */
174 if (pos >= runtime->buffer_size) {
175 if (printk_ratelimit()) {
176 char name[16];
177 pcm_debug_name(substream, name, sizeof(name));
178 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
179 "buffer size = 0x%lx, period size = 0x%lx\n",
180 name, pos, runtime->buffer_size,
181 runtime->period_size);
183 pos = 0;
185 pos -= pos % runtime->min_align;
186 return pos;
189 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
192 snd_pcm_uframes_t avail;
194 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
195 avail = snd_pcm_playback_avail(runtime);
196 else
197 avail = snd_pcm_capture_avail(runtime);
198 if (avail > runtime->avail_max)
199 runtime->avail_max = avail;
200 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
201 if (avail >= runtime->buffer_size) {
202 snd_pcm_drain_done(substream);
203 return -EPIPE;
205 } else {
206 if (avail >= runtime->stop_threshold) {
207 xrun(substream);
208 return -EPIPE;
211 if (avail >= runtime->control->avail_min)
212 wake_up(&runtime->sleep);
213 return 0;
216 #define hw_ptr_error(substream, fmt, args...) \
217 do { \
218 if (xrun_debug(substream, 1)) { \
219 if (printk_ratelimit()) { \
220 snd_printd("PCM: " fmt, ##args); \
222 dump_stack_on_xrun(substream); \
224 } while (0)
226 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
228 struct snd_pcm_runtime *runtime = substream->runtime;
229 snd_pcm_uframes_t pos;
230 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
231 snd_pcm_sframes_t hdelta, delta;
232 unsigned long jdelta;
234 old_hw_ptr = runtime->status->hw_ptr;
235 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
236 if (pos == SNDRV_PCM_POS_XRUN) {
237 xrun(substream);
238 return -EPIPE;
240 if (xrun_debug(substream, 8)) {
241 char name[16];
242 pcm_debug_name(substream, name, sizeof(name));
243 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
244 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
245 name, (unsigned int)pos,
246 (unsigned int)runtime->period_size,
247 (unsigned int)runtime->buffer_size,
248 (unsigned long)old_hw_ptr,
249 (unsigned long)runtime->hw_ptr_base,
250 (unsigned long)runtime->hw_ptr_interrupt);
252 hw_base = runtime->hw_ptr_base;
253 new_hw_ptr = hw_base + pos;
254 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
255 delta = new_hw_ptr - hw_ptr_interrupt;
256 if (hw_ptr_interrupt >= runtime->boundary) {
257 hw_ptr_interrupt -= runtime->boundary;
258 if (hw_base < runtime->boundary / 2)
259 /* hw_base was already lapped; recalc delta */
260 delta = new_hw_ptr - hw_ptr_interrupt;
262 if (delta < 0) {
263 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
264 delta += runtime->buffer_size;
265 if (delta < 0) {
266 hw_ptr_error(substream,
267 "Unexpected hw_pointer value "
268 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
269 substream->stream, (long)pos,
270 (long)hw_ptr_interrupt);
271 #if 1
272 /* simply skipping the hwptr update seems more
273 * robust in some cases, e.g. on VMware with
274 * inaccurate timer source
276 return 0; /* skip this update */
277 #else
278 /* rebase to interrupt position */
279 hw_base = new_hw_ptr = hw_ptr_interrupt;
280 /* align hw_base to buffer_size */
281 hw_base -= hw_base % runtime->buffer_size;
282 delta = 0;
283 #endif
284 } else {
285 hw_base += runtime->buffer_size;
286 if (hw_base >= runtime->boundary)
287 hw_base = 0;
288 new_hw_ptr = hw_base + pos;
292 /* Do jiffies check only in xrun_debug mode */
293 if (!xrun_debug(substream, 4))
294 goto no_jiffies_check;
296 /* Skip the jiffies check for hardwares with BATCH flag.
297 * Such hardware usually just increases the position at each IRQ,
298 * thus it can't give any strange position.
300 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
301 goto no_jiffies_check;
302 hdelta = new_hw_ptr - old_hw_ptr;
303 if (hdelta < runtime->delay)
304 goto no_jiffies_check;
305 hdelta -= runtime->delay;
306 jdelta = jiffies - runtime->hw_ptr_jiffies;
307 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
308 delta = jdelta /
309 (((runtime->period_size * HZ) / runtime->rate)
310 + HZ/100);
311 hw_ptr_error(substream,
312 "hw_ptr skipping! [Q] "
313 "(pos=%ld, delta=%ld, period=%ld, "
314 "jdelta=%lu/%lu/%lu)\n",
315 (long)pos, (long)hdelta,
316 (long)runtime->period_size, jdelta,
317 ((hdelta * HZ) / runtime->rate), delta);
318 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
319 runtime->period_size * delta;
320 if (hw_ptr_interrupt >= runtime->boundary)
321 hw_ptr_interrupt -= runtime->boundary;
322 /* rebase to interrupt position */
323 hw_base = new_hw_ptr = hw_ptr_interrupt;
324 /* align hw_base to buffer_size */
325 hw_base -= hw_base % runtime->buffer_size;
326 delta = 0;
328 no_jiffies_check:
329 if (delta > runtime->period_size + runtime->period_size / 2) {
330 hw_ptr_error(substream,
331 "Lost interrupts? "
332 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
333 substream->stream, (long)delta,
334 (long)hw_ptr_interrupt);
335 /* rebase hw_ptr_interrupt */
336 hw_ptr_interrupt =
337 new_hw_ptr - new_hw_ptr % runtime->period_size;
339 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
341 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
342 runtime->silence_size > 0)
343 snd_pcm_playback_silence(substream, new_hw_ptr);
345 if (runtime->status->hw_ptr == new_hw_ptr)
346 return 0;
348 runtime->hw_ptr_base = hw_base;
349 runtime->status->hw_ptr = new_hw_ptr;
350 runtime->hw_ptr_jiffies = jiffies;
351 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
352 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
354 return snd_pcm_update_hw_ptr_post(substream, runtime);
357 /* CAUTION: call it with irq disabled */
358 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
360 struct snd_pcm_runtime *runtime = substream->runtime;
361 snd_pcm_uframes_t pos;
362 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
363 snd_pcm_sframes_t delta;
364 unsigned long jdelta;
366 old_hw_ptr = runtime->status->hw_ptr;
367 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
368 if (pos == SNDRV_PCM_POS_XRUN) {
369 xrun(substream);
370 return -EPIPE;
372 if (xrun_debug(substream, 16)) {
373 char name[16];
374 pcm_debug_name(substream, name, sizeof(name));
375 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
376 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
377 name, (unsigned int)pos,
378 (unsigned int)runtime->period_size,
379 (unsigned int)runtime->buffer_size,
380 (unsigned long)old_hw_ptr,
381 (unsigned long)runtime->hw_ptr_base,
382 (unsigned long)runtime->hw_ptr_interrupt);
385 hw_base = runtime->hw_ptr_base;
386 new_hw_ptr = hw_base + pos;
388 delta = new_hw_ptr - old_hw_ptr;
389 jdelta = jiffies - runtime->hw_ptr_jiffies;
390 if (delta < 0) {
391 delta += runtime->buffer_size;
392 if (delta < 0) {
393 hw_ptr_error(substream,
394 "Unexpected hw_pointer value [2] "
395 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
396 substream->stream, (long)pos,
397 (long)old_hw_ptr, jdelta);
398 return 0;
400 hw_base += runtime->buffer_size;
401 if (hw_base >= runtime->boundary)
402 hw_base = 0;
403 new_hw_ptr = hw_base + pos;
405 /* Do jiffies check only in xrun_debug mode */
406 if (!xrun_debug(substream, 4))
407 goto no_jiffies_check;
408 if (delta < runtime->delay)
409 goto no_jiffies_check;
410 delta -= runtime->delay;
411 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
412 hw_ptr_error(substream,
413 "hw_ptr skipping! "
414 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
415 (long)pos, (long)delta,
416 (long)runtime->period_size, jdelta,
417 ((delta * HZ) / runtime->rate));
418 return 0;
420 no_jiffies_check:
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
422 runtime->silence_size > 0)
423 snd_pcm_playback_silence(substream, new_hw_ptr);
425 if (runtime->status->hw_ptr == new_hw_ptr)
426 return 0;
428 runtime->hw_ptr_base = hw_base;
429 runtime->status->hw_ptr = new_hw_ptr;
430 runtime->hw_ptr_jiffies = jiffies;
431 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
432 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
434 return snd_pcm_update_hw_ptr_post(substream, runtime);
438 * snd_pcm_set_ops - set the PCM operators
439 * @pcm: the pcm instance
440 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
441 * @ops: the operator table
443 * Sets the given PCM operators to the pcm instance.
445 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
447 struct snd_pcm_str *stream = &pcm->streams[direction];
448 struct snd_pcm_substream *substream;
450 for (substream = stream->substream; substream != NULL; substream = substream->next)
451 substream->ops = ops;
454 EXPORT_SYMBOL(snd_pcm_set_ops);
457 * snd_pcm_sync - set the PCM sync id
458 * @substream: the pcm substream
460 * Sets the PCM sync identifier for the card.
462 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
464 struct snd_pcm_runtime *runtime = substream->runtime;
466 runtime->sync.id32[0] = substream->pcm->card->number;
467 runtime->sync.id32[1] = -1;
468 runtime->sync.id32[2] = -1;
469 runtime->sync.id32[3] = -1;
472 EXPORT_SYMBOL(snd_pcm_set_sync);
475 * Standard ioctl routine
478 static inline unsigned int div32(unsigned int a, unsigned int b,
479 unsigned int *r)
481 if (b == 0) {
482 *r = 0;
483 return UINT_MAX;
485 *r = a % b;
486 return a / b;
489 static inline unsigned int div_down(unsigned int a, unsigned int b)
491 if (b == 0)
492 return UINT_MAX;
493 return a / b;
496 static inline unsigned int div_up(unsigned int a, unsigned int b)
498 unsigned int r;
499 unsigned int q;
500 if (b == 0)
501 return UINT_MAX;
502 q = div32(a, b, &r);
503 if (r)
504 ++q;
505 return q;
508 static inline unsigned int mul(unsigned int a, unsigned int b)
510 if (a == 0)
511 return 0;
512 if (div_down(UINT_MAX, a) < b)
513 return UINT_MAX;
514 return a * b;
517 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
518 unsigned int c, unsigned int *r)
520 u_int64_t n = (u_int64_t) a * b;
521 if (c == 0) {
522 snd_BUG_ON(!n);
523 *r = 0;
524 return UINT_MAX;
526 n = div_u64_rem(n, c, r);
527 if (n >= UINT_MAX) {
528 *r = 0;
529 return UINT_MAX;
531 return n;
535 * snd_interval_refine - refine the interval value of configurator
536 * @i: the interval value to refine
537 * @v: the interval value to refer to
539 * Refines the interval value with the reference value.
540 * The interval is changed to the range satisfying both intervals.
541 * The interval status (min, max, integer, etc.) are evaluated.
543 * Returns non-zero if the value is changed, zero if not changed.
545 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
547 int changed = 0;
548 if (snd_BUG_ON(snd_interval_empty(i)))
549 return -EINVAL;
550 if (i->min < v->min) {
551 i->min = v->min;
552 i->openmin = v->openmin;
553 changed = 1;
554 } else if (i->min == v->min && !i->openmin && v->openmin) {
555 i->openmin = 1;
556 changed = 1;
558 if (i->max > v->max) {
559 i->max = v->max;
560 i->openmax = v->openmax;
561 changed = 1;
562 } else if (i->max == v->max && !i->openmax && v->openmax) {
563 i->openmax = 1;
564 changed = 1;
566 if (!i->integer && v->integer) {
567 i->integer = 1;
568 changed = 1;
570 if (i->integer) {
571 if (i->openmin) {
572 i->min++;
573 i->openmin = 0;
575 if (i->openmax) {
576 i->max--;
577 i->openmax = 0;
579 } else if (!i->openmin && !i->openmax && i->min == i->max)
580 i->integer = 1;
581 if (snd_interval_checkempty(i)) {
582 snd_interval_none(i);
583 return -EINVAL;
585 return changed;
588 EXPORT_SYMBOL(snd_interval_refine);
590 static int snd_interval_refine_first(struct snd_interval *i)
592 if (snd_BUG_ON(snd_interval_empty(i)))
593 return -EINVAL;
594 if (snd_interval_single(i))
595 return 0;
596 i->max = i->min;
597 i->openmax = i->openmin;
598 if (i->openmax)
599 i->max++;
600 return 1;
603 static int snd_interval_refine_last(struct snd_interval *i)
605 if (snd_BUG_ON(snd_interval_empty(i)))
606 return -EINVAL;
607 if (snd_interval_single(i))
608 return 0;
609 i->min = i->max;
610 i->openmin = i->openmax;
611 if (i->openmin)
612 i->min--;
613 return 1;
616 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
618 if (a->empty || b->empty) {
619 snd_interval_none(c);
620 return;
622 c->empty = 0;
623 c->min = mul(a->min, b->min);
624 c->openmin = (a->openmin || b->openmin);
625 c->max = mul(a->max, b->max);
626 c->openmax = (a->openmax || b->openmax);
627 c->integer = (a->integer && b->integer);
631 * snd_interval_div - refine the interval value with division
632 * @a: dividend
633 * @b: divisor
634 * @c: quotient
636 * c = a / b
638 * Returns non-zero if the value is changed, zero if not changed.
640 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
642 unsigned int r;
643 if (a->empty || b->empty) {
644 snd_interval_none(c);
645 return;
647 c->empty = 0;
648 c->min = div32(a->min, b->max, &r);
649 c->openmin = (r || a->openmin || b->openmax);
650 if (b->min > 0) {
651 c->max = div32(a->max, b->min, &r);
652 if (r) {
653 c->max++;
654 c->openmax = 1;
655 } else
656 c->openmax = (a->openmax || b->openmin);
657 } else {
658 c->max = UINT_MAX;
659 c->openmax = 0;
661 c->integer = 0;
665 * snd_interval_muldivk - refine the interval value
666 * @a: dividend 1
667 * @b: dividend 2
668 * @k: divisor (as integer)
669 * @c: result
671 * c = a * b / k
673 * Returns non-zero if the value is changed, zero if not changed.
675 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
676 unsigned int k, struct snd_interval *c)
678 unsigned int r;
679 if (a->empty || b->empty) {
680 snd_interval_none(c);
681 return;
683 c->empty = 0;
684 c->min = muldiv32(a->min, b->min, k, &r);
685 c->openmin = (r || a->openmin || b->openmin);
686 c->max = muldiv32(a->max, b->max, k, &r);
687 if (r) {
688 c->max++;
689 c->openmax = 1;
690 } else
691 c->openmax = (a->openmax || b->openmax);
692 c->integer = 0;
696 * snd_interval_mulkdiv - refine the interval value
697 * @a: dividend 1
698 * @k: dividend 2 (as integer)
699 * @b: divisor
700 * @c: result
702 * c = a * k / b
704 * Returns non-zero if the value is changed, zero if not changed.
706 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
707 const struct snd_interval *b, struct snd_interval *c)
709 unsigned int r;
710 if (a->empty || b->empty) {
711 snd_interval_none(c);
712 return;
714 c->empty = 0;
715 c->min = muldiv32(a->min, k, b->max, &r);
716 c->openmin = (r || a->openmin || b->openmax);
717 if (b->min > 0) {
718 c->max = muldiv32(a->max, k, b->min, &r);
719 if (r) {
720 c->max++;
721 c->openmax = 1;
722 } else
723 c->openmax = (a->openmax || b->openmin);
724 } else {
725 c->max = UINT_MAX;
726 c->openmax = 0;
728 c->integer = 0;
731 /* ---- */
735 * snd_interval_ratnum - refine the interval value
736 * @i: interval to refine
737 * @rats_count: number of ratnum_t
738 * @rats: ratnum_t array
739 * @nump: pointer to store the resultant numerator
740 * @denp: pointer to store the resultant denominator
742 * Returns non-zero if the value is changed, zero if not changed.
744 int snd_interval_ratnum(struct snd_interval *i,
745 unsigned int rats_count, struct snd_ratnum *rats,
746 unsigned int *nump, unsigned int *denp)
748 unsigned int best_num, best_den;
749 int best_diff;
750 unsigned int k;
751 struct snd_interval t;
752 int err;
753 unsigned int result_num, result_den;
754 int result_diff;
756 best_num = best_den = best_diff = 0;
757 for (k = 0; k < rats_count; ++k) {
758 unsigned int num = rats[k].num;
759 unsigned int den;
760 unsigned int q = i->min;
761 int diff;
762 if (q == 0)
763 q = 1;
764 den = div_up(num, q);
765 if (den < rats[k].den_min)
766 continue;
767 if (den > rats[k].den_max)
768 den = rats[k].den_max;
769 else {
770 unsigned int r;
771 r = (den - rats[k].den_min) % rats[k].den_step;
772 if (r != 0)
773 den -= r;
775 diff = num - q * den;
776 if (diff < 0)
777 diff = -diff;
778 if (best_num == 0 ||
779 diff * best_den < best_diff * den) {
780 best_diff = diff;
781 best_den = den;
782 best_num = num;
785 if (best_den == 0) {
786 i->empty = 1;
787 return -EINVAL;
789 t.min = div_down(best_num, best_den);
790 t.openmin = !!(best_num % best_den);
792 result_num = best_num;
793 result_diff = best_diff;
794 result_den = best_den;
795 best_num = best_den = best_diff = 0;
796 for (k = 0; k < rats_count; ++k) {
797 unsigned int num = rats[k].num;
798 unsigned int den;
799 unsigned int q = i->max;
800 int diff;
801 if (q == 0) {
802 i->empty = 1;
803 return -EINVAL;
805 den = div_down(num, q);
806 if (den > rats[k].den_max)
807 continue;
808 if (den < rats[k].den_min)
809 den = rats[k].den_min;
810 else {
811 unsigned int r;
812 r = (den - rats[k].den_min) % rats[k].den_step;
813 if (r != 0)
814 den += rats[k].den_step - r;
816 diff = q * den - num;
817 if (diff < 0)
818 diff = -diff;
819 if (best_num == 0 ||
820 diff * best_den < best_diff * den) {
821 best_diff = diff;
822 best_den = den;
823 best_num = num;
826 if (best_den == 0) {
827 i->empty = 1;
828 return -EINVAL;
830 t.max = div_up(best_num, best_den);
831 t.openmax = !!(best_num % best_den);
832 t.integer = 0;
833 err = snd_interval_refine(i, &t);
834 if (err < 0)
835 return err;
837 if (snd_interval_single(i)) {
838 if (best_diff * result_den < result_diff * best_den) {
839 result_num = best_num;
840 result_den = best_den;
842 if (nump)
843 *nump = result_num;
844 if (denp)
845 *denp = result_den;
847 return err;
850 EXPORT_SYMBOL(snd_interval_ratnum);
853 * snd_interval_ratden - refine the interval value
854 * @i: interval to refine
855 * @rats_count: number of struct ratden
856 * @rats: struct ratden array
857 * @nump: pointer to store the resultant numerator
858 * @denp: pointer to store the resultant denominator
860 * Returns non-zero if the value is changed, zero if not changed.
862 static int snd_interval_ratden(struct snd_interval *i,
863 unsigned int rats_count, struct snd_ratden *rats,
864 unsigned int *nump, unsigned int *denp)
866 unsigned int best_num, best_diff, best_den;
867 unsigned int k;
868 struct snd_interval t;
869 int err;
871 best_num = best_den = best_diff = 0;
872 for (k = 0; k < rats_count; ++k) {
873 unsigned int num;
874 unsigned int den = rats[k].den;
875 unsigned int q = i->min;
876 int diff;
877 num = mul(q, den);
878 if (num > rats[k].num_max)
879 continue;
880 if (num < rats[k].num_min)
881 num = rats[k].num_max;
882 else {
883 unsigned int r;
884 r = (num - rats[k].num_min) % rats[k].num_step;
885 if (r != 0)
886 num += rats[k].num_step - r;
888 diff = num - q * den;
889 if (best_num == 0 ||
890 diff * best_den < best_diff * den) {
891 best_diff = diff;
892 best_den = den;
893 best_num = num;
896 if (best_den == 0) {
897 i->empty = 1;
898 return -EINVAL;
900 t.min = div_down(best_num, best_den);
901 t.openmin = !!(best_num % best_den);
903 best_num = best_den = best_diff = 0;
904 for (k = 0; k < rats_count; ++k) {
905 unsigned int num;
906 unsigned int den = rats[k].den;
907 unsigned int q = i->max;
908 int diff;
909 num = mul(q, den);
910 if (num < rats[k].num_min)
911 continue;
912 if (num > rats[k].num_max)
913 num = rats[k].num_max;
914 else {
915 unsigned int r;
916 r = (num - rats[k].num_min) % rats[k].num_step;
917 if (r != 0)
918 num -= r;
920 diff = q * den - num;
921 if (best_num == 0 ||
922 diff * best_den < best_diff * den) {
923 best_diff = diff;
924 best_den = den;
925 best_num = num;
928 if (best_den == 0) {
929 i->empty = 1;
930 return -EINVAL;
932 t.max = div_up(best_num, best_den);
933 t.openmax = !!(best_num % best_den);
934 t.integer = 0;
935 err = snd_interval_refine(i, &t);
936 if (err < 0)
937 return err;
939 if (snd_interval_single(i)) {
940 if (nump)
941 *nump = best_num;
942 if (denp)
943 *denp = best_den;
945 return err;
949 * snd_interval_list - refine the interval value from the list
950 * @i: the interval value to refine
951 * @count: the number of elements in the list
952 * @list: the value list
953 * @mask: the bit-mask to evaluate
955 * Refines the interval value from the list.
956 * When mask is non-zero, only the elements corresponding to bit 1 are
957 * evaluated.
959 * Returns non-zero if the value is changed, zero if not changed.
961 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
963 unsigned int k;
964 struct snd_interval list_range;
966 if (!count) {
967 i->empty = 1;
968 return -EINVAL;
970 snd_interval_any(&list_range);
971 list_range.min = UINT_MAX;
972 list_range.max = 0;
973 for (k = 0; k < count; k++) {
974 if (mask && !(mask & (1 << k)))
975 continue;
976 if (!snd_interval_test(i, list[k]))
977 continue;
978 list_range.min = min(list_range.min, list[k]);
979 list_range.max = max(list_range.max, list[k]);
981 return snd_interval_refine(i, &list_range);
984 EXPORT_SYMBOL(snd_interval_list);
986 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
988 unsigned int n;
989 int changed = 0;
990 n = (i->min - min) % step;
991 if (n != 0 || i->openmin) {
992 i->min += step - n;
993 changed = 1;
995 n = (i->max - min) % step;
996 if (n != 0 || i->openmax) {
997 i->max -= n;
998 changed = 1;
1000 if (snd_interval_checkempty(i)) {
1001 i->empty = 1;
1002 return -EINVAL;
1004 return changed;
1007 /* Info constraints helpers */
1010 * snd_pcm_hw_rule_add - add the hw-constraint rule
1011 * @runtime: the pcm runtime instance
1012 * @cond: condition bits
1013 * @var: the variable to evaluate
1014 * @func: the evaluation function
1015 * @private: the private data pointer passed to function
1016 * @dep: the dependent variables
1018 * Returns zero if successful, or a negative error code on failure.
1020 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1021 int var,
1022 snd_pcm_hw_rule_func_t func, void *private,
1023 int dep, ...)
1025 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1026 struct snd_pcm_hw_rule *c;
1027 unsigned int k;
1028 va_list args;
1029 va_start(args, dep);
1030 if (constrs->rules_num >= constrs->rules_all) {
1031 struct snd_pcm_hw_rule *new;
1032 unsigned int new_rules = constrs->rules_all + 16;
1033 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1034 if (!new)
1035 return -ENOMEM;
1036 if (constrs->rules) {
1037 memcpy(new, constrs->rules,
1038 constrs->rules_num * sizeof(*c));
1039 kfree(constrs->rules);
1041 constrs->rules = new;
1042 constrs->rules_all = new_rules;
1044 c = &constrs->rules[constrs->rules_num];
1045 c->cond = cond;
1046 c->func = func;
1047 c->var = var;
1048 c->private = private;
1049 k = 0;
1050 while (1) {
1051 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1052 return -EINVAL;
1053 c->deps[k++] = dep;
1054 if (dep < 0)
1055 break;
1056 dep = va_arg(args, int);
1058 constrs->rules_num++;
1059 va_end(args);
1060 return 0;
1063 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1066 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1067 * @runtime: PCM runtime instance
1068 * @var: hw_params variable to apply the mask
1069 * @mask: the bitmap mask
1071 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1073 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1074 u_int32_t mask)
1076 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1077 struct snd_mask *maskp = constrs_mask(constrs, var);
1078 *maskp->bits &= mask;
1079 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1080 if (*maskp->bits == 0)
1081 return -EINVAL;
1082 return 0;
1086 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1087 * @runtime: PCM runtime instance
1088 * @var: hw_params variable to apply the mask
1089 * @mask: the 64bit bitmap mask
1091 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1093 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1094 u_int64_t mask)
1096 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1097 struct snd_mask *maskp = constrs_mask(constrs, var);
1098 maskp->bits[0] &= (u_int32_t)mask;
1099 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1100 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1101 if (! maskp->bits[0] && ! maskp->bits[1])
1102 return -EINVAL;
1103 return 0;
1107 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1108 * @runtime: PCM runtime instance
1109 * @var: hw_params variable to apply the integer constraint
1111 * Apply the constraint of integer to an interval parameter.
1113 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1115 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1116 return snd_interval_setinteger(constrs_interval(constrs, var));
1119 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1122 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1123 * @runtime: PCM runtime instance
1124 * @var: hw_params variable to apply the range
1125 * @min: the minimal value
1126 * @max: the maximal value
1128 * Apply the min/max range constraint to an interval parameter.
1130 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1131 unsigned int min, unsigned int max)
1133 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1134 struct snd_interval t;
1135 t.min = min;
1136 t.max = max;
1137 t.openmin = t.openmax = 0;
1138 t.integer = 0;
1139 return snd_interval_refine(constrs_interval(constrs, var), &t);
1142 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1144 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1145 struct snd_pcm_hw_rule *rule)
1147 struct snd_pcm_hw_constraint_list *list = rule->private;
1148 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1153 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1154 * @runtime: PCM runtime instance
1155 * @cond: condition bits
1156 * @var: hw_params variable to apply the list constraint
1157 * @l: list
1159 * Apply the list of constraints to an interval parameter.
1161 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1162 unsigned int cond,
1163 snd_pcm_hw_param_t var,
1164 struct snd_pcm_hw_constraint_list *l)
1166 return snd_pcm_hw_rule_add(runtime, cond, var,
1167 snd_pcm_hw_rule_list, l,
1168 var, -1);
1171 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1173 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1174 struct snd_pcm_hw_rule *rule)
1176 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1177 unsigned int num = 0, den = 0;
1178 int err;
1179 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1180 r->nrats, r->rats, &num, &den);
1181 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1182 params->rate_num = num;
1183 params->rate_den = den;
1185 return err;
1189 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1190 * @runtime: PCM runtime instance
1191 * @cond: condition bits
1192 * @var: hw_params variable to apply the ratnums constraint
1193 * @r: struct snd_ratnums constriants
1195 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1196 unsigned int cond,
1197 snd_pcm_hw_param_t var,
1198 struct snd_pcm_hw_constraint_ratnums *r)
1200 return snd_pcm_hw_rule_add(runtime, cond, var,
1201 snd_pcm_hw_rule_ratnums, r,
1202 var, -1);
1205 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1207 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1208 struct snd_pcm_hw_rule *rule)
1210 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1211 unsigned int num = 0, den = 0;
1212 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1213 r->nrats, r->rats, &num, &den);
1214 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1215 params->rate_num = num;
1216 params->rate_den = den;
1218 return err;
1222 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1223 * @runtime: PCM runtime instance
1224 * @cond: condition bits
1225 * @var: hw_params variable to apply the ratdens constraint
1226 * @r: struct snd_ratdens constriants
1228 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1229 unsigned int cond,
1230 snd_pcm_hw_param_t var,
1231 struct snd_pcm_hw_constraint_ratdens *r)
1233 return snd_pcm_hw_rule_add(runtime, cond, var,
1234 snd_pcm_hw_rule_ratdens, r,
1235 var, -1);
1238 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1240 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1241 struct snd_pcm_hw_rule *rule)
1243 unsigned int l = (unsigned long) rule->private;
1244 int width = l & 0xffff;
1245 unsigned int msbits = l >> 16;
1246 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1247 if (snd_interval_single(i) && snd_interval_value(i) == width)
1248 params->msbits = msbits;
1249 return 0;
1253 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1254 * @runtime: PCM runtime instance
1255 * @cond: condition bits
1256 * @width: sample bits width
1257 * @msbits: msbits width
1259 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1260 unsigned int cond,
1261 unsigned int width,
1262 unsigned int msbits)
1264 unsigned long l = (msbits << 16) | width;
1265 return snd_pcm_hw_rule_add(runtime, cond, -1,
1266 snd_pcm_hw_rule_msbits,
1267 (void*) l,
1268 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1271 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1273 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1274 struct snd_pcm_hw_rule *rule)
1276 unsigned long step = (unsigned long) rule->private;
1277 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1281 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1282 * @runtime: PCM runtime instance
1283 * @cond: condition bits
1284 * @var: hw_params variable to apply the step constraint
1285 * @step: step size
1287 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1288 unsigned int cond,
1289 snd_pcm_hw_param_t var,
1290 unsigned long step)
1292 return snd_pcm_hw_rule_add(runtime, cond, var,
1293 snd_pcm_hw_rule_step, (void *) step,
1294 var, -1);
1297 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1299 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1301 static unsigned int pow2_sizes[] = {
1302 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1303 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1304 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1305 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1307 return snd_interval_list(hw_param_interval(params, rule->var),
1308 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1312 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1313 * @runtime: PCM runtime instance
1314 * @cond: condition bits
1315 * @var: hw_params variable to apply the power-of-2 constraint
1317 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1318 unsigned int cond,
1319 snd_pcm_hw_param_t var)
1321 return snd_pcm_hw_rule_add(runtime, cond, var,
1322 snd_pcm_hw_rule_pow2, NULL,
1323 var, -1);
1326 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1328 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1329 snd_pcm_hw_param_t var)
1331 if (hw_is_mask(var)) {
1332 snd_mask_any(hw_param_mask(params, var));
1333 params->cmask |= 1 << var;
1334 params->rmask |= 1 << var;
1335 return;
1337 if (hw_is_interval(var)) {
1338 snd_interval_any(hw_param_interval(params, var));
1339 params->cmask |= 1 << var;
1340 params->rmask |= 1 << var;
1341 return;
1343 snd_BUG();
1346 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1348 unsigned int k;
1349 memset(params, 0, sizeof(*params));
1350 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1351 _snd_pcm_hw_param_any(params, k);
1352 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1353 _snd_pcm_hw_param_any(params, k);
1354 params->info = ~0U;
1357 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1360 * snd_pcm_hw_param_value - return @params field @var value
1361 * @params: the hw_params instance
1362 * @var: parameter to retrieve
1363 * @dir: pointer to the direction (-1,0,1) or %NULL
1365 * Return the value for field @var if it's fixed in configuration space
1366 * defined by @params. Return -%EINVAL otherwise.
1368 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1369 snd_pcm_hw_param_t var, int *dir)
1371 if (hw_is_mask(var)) {
1372 const struct snd_mask *mask = hw_param_mask_c(params, var);
1373 if (!snd_mask_single(mask))
1374 return -EINVAL;
1375 if (dir)
1376 *dir = 0;
1377 return snd_mask_value(mask);
1379 if (hw_is_interval(var)) {
1380 const struct snd_interval *i = hw_param_interval_c(params, var);
1381 if (!snd_interval_single(i))
1382 return -EINVAL;
1383 if (dir)
1384 *dir = i->openmin;
1385 return snd_interval_value(i);
1387 return -EINVAL;
1390 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1392 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1393 snd_pcm_hw_param_t var)
1395 if (hw_is_mask(var)) {
1396 snd_mask_none(hw_param_mask(params, var));
1397 params->cmask |= 1 << var;
1398 params->rmask |= 1 << var;
1399 } else if (hw_is_interval(var)) {
1400 snd_interval_none(hw_param_interval(params, var));
1401 params->cmask |= 1 << var;
1402 params->rmask |= 1 << var;
1403 } else {
1404 snd_BUG();
1408 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1410 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1411 snd_pcm_hw_param_t var)
1413 int changed;
1414 if (hw_is_mask(var))
1415 changed = snd_mask_refine_first(hw_param_mask(params, var));
1416 else if (hw_is_interval(var))
1417 changed = snd_interval_refine_first(hw_param_interval(params, var));
1418 else
1419 return -EINVAL;
1420 if (changed) {
1421 params->cmask |= 1 << var;
1422 params->rmask |= 1 << var;
1424 return changed;
1429 * snd_pcm_hw_param_first - refine config space and return minimum value
1430 * @pcm: PCM instance
1431 * @params: the hw_params instance
1432 * @var: parameter to retrieve
1433 * @dir: pointer to the direction (-1,0,1) or %NULL
1435 * Inside configuration space defined by @params remove from @var all
1436 * values > minimum. Reduce configuration space accordingly.
1437 * Return the minimum.
1439 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1440 struct snd_pcm_hw_params *params,
1441 snd_pcm_hw_param_t var, int *dir)
1443 int changed = _snd_pcm_hw_param_first(params, var);
1444 if (changed < 0)
1445 return changed;
1446 if (params->rmask) {
1447 int err = snd_pcm_hw_refine(pcm, params);
1448 if (snd_BUG_ON(err < 0))
1449 return err;
1451 return snd_pcm_hw_param_value(params, var, dir);
1454 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1456 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1457 snd_pcm_hw_param_t var)
1459 int changed;
1460 if (hw_is_mask(var))
1461 changed = snd_mask_refine_last(hw_param_mask(params, var));
1462 else if (hw_is_interval(var))
1463 changed = snd_interval_refine_last(hw_param_interval(params, var));
1464 else
1465 return -EINVAL;
1466 if (changed) {
1467 params->cmask |= 1 << var;
1468 params->rmask |= 1 << var;
1470 return changed;
1475 * snd_pcm_hw_param_last - refine config space and return maximum value
1476 * @pcm: PCM instance
1477 * @params: the hw_params instance
1478 * @var: parameter to retrieve
1479 * @dir: pointer to the direction (-1,0,1) or %NULL
1481 * Inside configuration space defined by @params remove from @var all
1482 * values < maximum. Reduce configuration space accordingly.
1483 * Return the maximum.
1485 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1486 struct snd_pcm_hw_params *params,
1487 snd_pcm_hw_param_t var, int *dir)
1489 int changed = _snd_pcm_hw_param_last(params, var);
1490 if (changed < 0)
1491 return changed;
1492 if (params->rmask) {
1493 int err = snd_pcm_hw_refine(pcm, params);
1494 if (snd_BUG_ON(err < 0))
1495 return err;
1497 return snd_pcm_hw_param_value(params, var, dir);
1500 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1503 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1504 * @pcm: PCM instance
1505 * @params: the hw_params instance
1507 * Choose one configuration from configuration space defined by @params.
1508 * The configuration chosen is that obtained fixing in this order:
1509 * first access, first format, first subformat, min channels,
1510 * min rate, min period time, max buffer size, min tick time
1512 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1513 struct snd_pcm_hw_params *params)
1515 static int vars[] = {
1516 SNDRV_PCM_HW_PARAM_ACCESS,
1517 SNDRV_PCM_HW_PARAM_FORMAT,
1518 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1519 SNDRV_PCM_HW_PARAM_CHANNELS,
1520 SNDRV_PCM_HW_PARAM_RATE,
1521 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1522 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1523 SNDRV_PCM_HW_PARAM_TICK_TIME,
1526 int err, *v;
1528 for (v = vars; *v != -1; v++) {
1529 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1530 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1531 else
1532 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1533 if (snd_BUG_ON(err < 0))
1534 return err;
1536 return 0;
1539 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1540 void *arg)
1542 struct snd_pcm_runtime *runtime = substream->runtime;
1543 unsigned long flags;
1544 snd_pcm_stream_lock_irqsave(substream, flags);
1545 if (snd_pcm_running(substream) &&
1546 snd_pcm_update_hw_ptr(substream) >= 0)
1547 runtime->status->hw_ptr %= runtime->buffer_size;
1548 else
1549 runtime->status->hw_ptr = 0;
1550 snd_pcm_stream_unlock_irqrestore(substream, flags);
1551 return 0;
1554 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1555 void *arg)
1557 struct snd_pcm_channel_info *info = arg;
1558 struct snd_pcm_runtime *runtime = substream->runtime;
1559 int width;
1560 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1561 info->offset = -1;
1562 return 0;
1564 width = snd_pcm_format_physical_width(runtime->format);
1565 if (width < 0)
1566 return width;
1567 info->offset = 0;
1568 switch (runtime->access) {
1569 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1570 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1571 info->first = info->channel * width;
1572 info->step = runtime->channels * width;
1573 break;
1574 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1575 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1577 size_t size = runtime->dma_bytes / runtime->channels;
1578 info->first = info->channel * size * 8;
1579 info->step = width;
1580 break;
1582 default:
1583 snd_BUG();
1584 break;
1586 return 0;
1589 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1590 void *arg)
1592 struct snd_pcm_hw_params *params = arg;
1593 snd_pcm_format_t format;
1594 int channels, width;
1596 params->fifo_size = substream->runtime->hw.fifo_size;
1597 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1598 format = params_format(params);
1599 channels = params_channels(params);
1600 width = snd_pcm_format_physical_width(format);
1601 params->fifo_size /= width * channels;
1603 return 0;
1607 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1608 * @substream: the pcm substream instance
1609 * @cmd: ioctl command
1610 * @arg: ioctl argument
1612 * Processes the generic ioctl commands for PCM.
1613 * Can be passed as the ioctl callback for PCM ops.
1615 * Returns zero if successful, or a negative error code on failure.
1617 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1618 unsigned int cmd, void *arg)
1620 switch (cmd) {
1621 case SNDRV_PCM_IOCTL1_INFO:
1622 return 0;
1623 case SNDRV_PCM_IOCTL1_RESET:
1624 return snd_pcm_lib_ioctl_reset(substream, arg);
1625 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1626 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1627 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1628 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1630 return -ENXIO;
1633 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1636 * snd_pcm_period_elapsed - update the pcm status for the next period
1637 * @substream: the pcm substream instance
1639 * This function is called from the interrupt handler when the
1640 * PCM has processed the period size. It will update the current
1641 * pointer, wake up sleepers, etc.
1643 * Even if more than one periods have elapsed since the last call, you
1644 * have to call this only once.
1646 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1648 struct snd_pcm_runtime *runtime;
1649 unsigned long flags;
1651 if (PCM_RUNTIME_CHECK(substream))
1652 return;
1653 runtime = substream->runtime;
1655 if (runtime->transfer_ack_begin)
1656 runtime->transfer_ack_begin(substream);
1658 snd_pcm_stream_lock_irqsave(substream, flags);
1659 if (!snd_pcm_running(substream) ||
1660 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1661 goto _end;
1663 if (substream->timer_running)
1664 snd_timer_interrupt(substream->timer, 1);
1665 _end:
1666 snd_pcm_stream_unlock_irqrestore(substream, flags);
1667 if (runtime->transfer_ack_end)
1668 runtime->transfer_ack_end(substream);
1669 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1672 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1675 * Wait until avail_min data becomes available
1676 * Returns a negative error code if any error occurs during operation.
1677 * The available space is stored on availp. When err = 0 and avail = 0
1678 * on the capture stream, it indicates the stream is in DRAINING state.
1680 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1681 snd_pcm_uframes_t *availp)
1683 struct snd_pcm_runtime *runtime = substream->runtime;
1684 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1685 wait_queue_t wait;
1686 int err = 0;
1687 snd_pcm_uframes_t avail = 0;
1688 long tout;
1690 init_waitqueue_entry(&wait, current);
1691 add_wait_queue(&runtime->sleep, &wait);
1692 for (;;) {
1693 if (signal_pending(current)) {
1694 err = -ERESTARTSYS;
1695 break;
1697 set_current_state(TASK_INTERRUPTIBLE);
1698 snd_pcm_stream_unlock_irq(substream);
1699 tout = schedule_timeout(msecs_to_jiffies(10000));
1700 snd_pcm_stream_lock_irq(substream);
1701 switch (runtime->status->state) {
1702 case SNDRV_PCM_STATE_SUSPENDED:
1703 err = -ESTRPIPE;
1704 goto _endloop;
1705 case SNDRV_PCM_STATE_XRUN:
1706 err = -EPIPE;
1707 goto _endloop;
1708 case SNDRV_PCM_STATE_DRAINING:
1709 if (is_playback)
1710 err = -EPIPE;
1711 else
1712 avail = 0; /* indicate draining */
1713 goto _endloop;
1714 case SNDRV_PCM_STATE_OPEN:
1715 case SNDRV_PCM_STATE_SETUP:
1716 case SNDRV_PCM_STATE_DISCONNECTED:
1717 err = -EBADFD;
1718 goto _endloop;
1720 if (!tout) {
1721 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1722 is_playback ? "playback" : "capture");
1723 err = -EIO;
1724 break;
1726 if (is_playback)
1727 avail = snd_pcm_playback_avail(runtime);
1728 else
1729 avail = snd_pcm_capture_avail(runtime);
1730 if (avail >= runtime->control->avail_min)
1731 break;
1733 _endloop:
1734 remove_wait_queue(&runtime->sleep, &wait);
1735 *availp = avail;
1736 return err;
1739 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1740 unsigned int hwoff,
1741 unsigned long data, unsigned int off,
1742 snd_pcm_uframes_t frames)
1744 struct snd_pcm_runtime *runtime = substream->runtime;
1745 int err;
1746 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1747 if (substream->ops->copy) {
1748 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1749 return err;
1750 } else {
1751 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1752 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1753 return -EFAULT;
1755 return 0;
1758 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1759 unsigned long data, unsigned int off,
1760 snd_pcm_uframes_t size);
1762 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1763 unsigned long data,
1764 snd_pcm_uframes_t size,
1765 int nonblock,
1766 transfer_f transfer)
1768 struct snd_pcm_runtime *runtime = substream->runtime;
1769 snd_pcm_uframes_t xfer = 0;
1770 snd_pcm_uframes_t offset = 0;
1771 int err = 0;
1773 if (size == 0)
1774 return 0;
1776 snd_pcm_stream_lock_irq(substream);
1777 switch (runtime->status->state) {
1778 case SNDRV_PCM_STATE_PREPARED:
1779 case SNDRV_PCM_STATE_RUNNING:
1780 case SNDRV_PCM_STATE_PAUSED:
1781 break;
1782 case SNDRV_PCM_STATE_XRUN:
1783 err = -EPIPE;
1784 goto _end_unlock;
1785 case SNDRV_PCM_STATE_SUSPENDED:
1786 err = -ESTRPIPE;
1787 goto _end_unlock;
1788 default:
1789 err = -EBADFD;
1790 goto _end_unlock;
1793 while (size > 0) {
1794 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1795 snd_pcm_uframes_t avail;
1796 snd_pcm_uframes_t cont;
1797 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1798 snd_pcm_update_hw_ptr(substream);
1799 avail = snd_pcm_playback_avail(runtime);
1800 if (!avail) {
1801 if (nonblock) {
1802 err = -EAGAIN;
1803 goto _end_unlock;
1805 err = wait_for_avail_min(substream, &avail);
1806 if (err < 0)
1807 goto _end_unlock;
1809 frames = size > avail ? avail : size;
1810 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1811 if (frames > cont)
1812 frames = cont;
1813 if (snd_BUG_ON(!frames)) {
1814 snd_pcm_stream_unlock_irq(substream);
1815 return -EINVAL;
1817 appl_ptr = runtime->control->appl_ptr;
1818 appl_ofs = appl_ptr % runtime->buffer_size;
1819 snd_pcm_stream_unlock_irq(substream);
1820 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1821 goto _end;
1822 snd_pcm_stream_lock_irq(substream);
1823 switch (runtime->status->state) {
1824 case SNDRV_PCM_STATE_XRUN:
1825 err = -EPIPE;
1826 goto _end_unlock;
1827 case SNDRV_PCM_STATE_SUSPENDED:
1828 err = -ESTRPIPE;
1829 goto _end_unlock;
1830 default:
1831 break;
1833 appl_ptr += frames;
1834 if (appl_ptr >= runtime->boundary)
1835 appl_ptr -= runtime->boundary;
1836 runtime->control->appl_ptr = appl_ptr;
1837 if (substream->ops->ack)
1838 substream->ops->ack(substream);
1840 offset += frames;
1841 size -= frames;
1842 xfer += frames;
1843 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1844 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1845 err = snd_pcm_start(substream);
1846 if (err < 0)
1847 goto _end_unlock;
1850 _end_unlock:
1851 snd_pcm_stream_unlock_irq(substream);
1852 _end:
1853 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1856 /* sanity-check for read/write methods */
1857 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1859 struct snd_pcm_runtime *runtime;
1860 if (PCM_RUNTIME_CHECK(substream))
1861 return -ENXIO;
1862 runtime = substream->runtime;
1863 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1864 return -EINVAL;
1865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1866 return -EBADFD;
1867 return 0;
1870 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1872 struct snd_pcm_runtime *runtime;
1873 int nonblock;
1874 int err;
1876 err = pcm_sanity_check(substream);
1877 if (err < 0)
1878 return err;
1879 runtime = substream->runtime;
1880 nonblock = !!(substream->f_flags & O_NONBLOCK);
1882 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1883 runtime->channels > 1)
1884 return -EINVAL;
1885 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1886 snd_pcm_lib_write_transfer);
1889 EXPORT_SYMBOL(snd_pcm_lib_write);
1891 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1892 unsigned int hwoff,
1893 unsigned long data, unsigned int off,
1894 snd_pcm_uframes_t frames)
1896 struct snd_pcm_runtime *runtime = substream->runtime;
1897 int err;
1898 void __user **bufs = (void __user **)data;
1899 int channels = runtime->channels;
1900 int c;
1901 if (substream->ops->copy) {
1902 if (snd_BUG_ON(!substream->ops->silence))
1903 return -EINVAL;
1904 for (c = 0; c < channels; ++c, ++bufs) {
1905 if (*bufs == NULL) {
1906 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1907 return err;
1908 } else {
1909 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1910 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1911 return err;
1914 } else {
1915 /* default transfer behaviour */
1916 size_t dma_csize = runtime->dma_bytes / channels;
1917 for (c = 0; c < channels; ++c, ++bufs) {
1918 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1919 if (*bufs == NULL) {
1920 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1921 } else {
1922 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1923 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1924 return -EFAULT;
1928 return 0;
1931 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1932 void __user **bufs,
1933 snd_pcm_uframes_t frames)
1935 struct snd_pcm_runtime *runtime;
1936 int nonblock;
1937 int err;
1939 err = pcm_sanity_check(substream);
1940 if (err < 0)
1941 return err;
1942 runtime = substream->runtime;
1943 nonblock = !!(substream->f_flags & O_NONBLOCK);
1945 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1946 return -EINVAL;
1947 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1948 nonblock, snd_pcm_lib_writev_transfer);
1951 EXPORT_SYMBOL(snd_pcm_lib_writev);
1953 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1954 unsigned int hwoff,
1955 unsigned long data, unsigned int off,
1956 snd_pcm_uframes_t frames)
1958 struct snd_pcm_runtime *runtime = substream->runtime;
1959 int err;
1960 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1961 if (substream->ops->copy) {
1962 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1963 return err;
1964 } else {
1965 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1966 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1967 return -EFAULT;
1969 return 0;
1972 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1973 unsigned long data,
1974 snd_pcm_uframes_t size,
1975 int nonblock,
1976 transfer_f transfer)
1978 struct snd_pcm_runtime *runtime = substream->runtime;
1979 snd_pcm_uframes_t xfer = 0;
1980 snd_pcm_uframes_t offset = 0;
1981 int err = 0;
1983 if (size == 0)
1984 return 0;
1986 snd_pcm_stream_lock_irq(substream);
1987 switch (runtime->status->state) {
1988 case SNDRV_PCM_STATE_PREPARED:
1989 if (size >= runtime->start_threshold) {
1990 err = snd_pcm_start(substream);
1991 if (err < 0)
1992 goto _end_unlock;
1994 break;
1995 case SNDRV_PCM_STATE_DRAINING:
1996 case SNDRV_PCM_STATE_RUNNING:
1997 case SNDRV_PCM_STATE_PAUSED:
1998 break;
1999 case SNDRV_PCM_STATE_XRUN:
2000 err = -EPIPE;
2001 goto _end_unlock;
2002 case SNDRV_PCM_STATE_SUSPENDED:
2003 err = -ESTRPIPE;
2004 goto _end_unlock;
2005 default:
2006 err = -EBADFD;
2007 goto _end_unlock;
2010 while (size > 0) {
2011 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2012 snd_pcm_uframes_t avail;
2013 snd_pcm_uframes_t cont;
2014 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2015 snd_pcm_update_hw_ptr(substream);
2016 avail = snd_pcm_capture_avail(runtime);
2017 if (!avail) {
2018 if (runtime->status->state ==
2019 SNDRV_PCM_STATE_DRAINING) {
2020 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2021 goto _end_unlock;
2023 if (nonblock) {
2024 err = -EAGAIN;
2025 goto _end_unlock;
2027 err = wait_for_avail_min(substream, &avail);
2028 if (err < 0)
2029 goto _end_unlock;
2030 if (!avail)
2031 continue; /* draining */
2033 frames = size > avail ? avail : size;
2034 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2035 if (frames > cont)
2036 frames = cont;
2037 if (snd_BUG_ON(!frames)) {
2038 snd_pcm_stream_unlock_irq(substream);
2039 return -EINVAL;
2041 appl_ptr = runtime->control->appl_ptr;
2042 appl_ofs = appl_ptr % runtime->buffer_size;
2043 snd_pcm_stream_unlock_irq(substream);
2044 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2045 goto _end;
2046 snd_pcm_stream_lock_irq(substream);
2047 switch (runtime->status->state) {
2048 case SNDRV_PCM_STATE_XRUN:
2049 err = -EPIPE;
2050 goto _end_unlock;
2051 case SNDRV_PCM_STATE_SUSPENDED:
2052 err = -ESTRPIPE;
2053 goto _end_unlock;
2054 default:
2055 break;
2057 appl_ptr += frames;
2058 if (appl_ptr >= runtime->boundary)
2059 appl_ptr -= runtime->boundary;
2060 runtime->control->appl_ptr = appl_ptr;
2061 if (substream->ops->ack)
2062 substream->ops->ack(substream);
2064 offset += frames;
2065 size -= frames;
2066 xfer += frames;
2068 _end_unlock:
2069 snd_pcm_stream_unlock_irq(substream);
2070 _end:
2071 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2074 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2076 struct snd_pcm_runtime *runtime;
2077 int nonblock;
2078 int err;
2080 err = pcm_sanity_check(substream);
2081 if (err < 0)
2082 return err;
2083 runtime = substream->runtime;
2084 nonblock = !!(substream->f_flags & O_NONBLOCK);
2085 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2086 return -EINVAL;
2087 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2090 EXPORT_SYMBOL(snd_pcm_lib_read);
2092 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2093 unsigned int hwoff,
2094 unsigned long data, unsigned int off,
2095 snd_pcm_uframes_t frames)
2097 struct snd_pcm_runtime *runtime = substream->runtime;
2098 int err;
2099 void __user **bufs = (void __user **)data;
2100 int channels = runtime->channels;
2101 int c;
2102 if (substream->ops->copy) {
2103 for (c = 0; c < channels; ++c, ++bufs) {
2104 char __user *buf;
2105 if (*bufs == NULL)
2106 continue;
2107 buf = *bufs + samples_to_bytes(runtime, off);
2108 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2109 return err;
2111 } else {
2112 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2113 for (c = 0; c < channels; ++c, ++bufs) {
2114 char *hwbuf;
2115 char __user *buf;
2116 if (*bufs == NULL)
2117 continue;
2119 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2120 buf = *bufs + samples_to_bytes(runtime, off);
2121 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2122 return -EFAULT;
2125 return 0;
2128 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2129 void __user **bufs,
2130 snd_pcm_uframes_t frames)
2132 struct snd_pcm_runtime *runtime;
2133 int nonblock;
2134 int err;
2136 err = pcm_sanity_check(substream);
2137 if (err < 0)
2138 return err;
2139 runtime = substream->runtime;
2140 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2141 return -EBADFD;
2143 nonblock = !!(substream->f_flags & O_NONBLOCK);
2144 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2145 return -EINVAL;
2146 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2149 EXPORT_SYMBOL(snd_pcm_lib_readv);