[PATCH] x86_64: Add cpu_relax to apic_wait_icr_idle
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / pcm_lib.c
blob0bb142a28539fe93c6be231b715b076821a33b3f
1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
24 #include <linux/slab.h>
25 #include <linux/time.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 = (ofs + frames) - runtime->buffer_size;
83 } else {
84 runtime->silence_start = ofs - runtime->silence_filled;
86 if ((snd_pcm_sframes_t)runtime->silence_start < 0)
87 runtime->silence_start += runtime->boundary;
89 frames = runtime->buffer_size - runtime->silence_filled;
91 snd_assert(frames <= runtime->buffer_size, return);
92 if (frames == 0)
93 return;
94 ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
95 while (frames > 0) {
96 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
97 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
98 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
99 if (substream->ops->silence) {
100 int err;
101 err = substream->ops->silence(substream, -1, ofs, transfer);
102 snd_assert(err >= 0, );
103 } else {
104 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
105 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107 } else {
108 unsigned int c;
109 unsigned int channels = runtime->channels;
110 if (substream->ops->silence) {
111 for (c = 0; c < channels; ++c) {
112 int err;
113 err = substream->ops->silence(substream, c, ofs, transfer);
114 snd_assert(err >= 0, );
116 } else {
117 size_t dma_csize = runtime->dma_bytes / channels;
118 for (c = 0; c < channels; ++c) {
119 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
120 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
124 runtime->silence_filled += transfer;
125 frames -= transfer;
126 ofs = 0;
130 static void xrun(struct snd_pcm_substream *substream)
132 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
133 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
134 if (substream->pstr->xrun_debug) {
135 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
136 substream->pcm->card->number,
137 substream->pcm->device,
138 substream->stream ? 'c' : 'p');
139 if (substream->pstr->xrun_debug > 1)
140 dump_stack();
142 #endif
145 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
146 struct snd_pcm_runtime *runtime)
148 snd_pcm_uframes_t pos;
150 pos = substream->ops->pointer(substream);
151 if (pos == SNDRV_PCM_POS_XRUN)
152 return pos; /* XRUN */
153 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
154 getnstimeofday((struct timespec *)&runtime->status->tstamp);
155 #ifdef CONFIG_SND_DEBUG
156 if (pos >= runtime->buffer_size) {
157 snd_printk(KERN_ERR "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size);
159 #endif
160 pos -= pos % runtime->min_align;
161 return pos;
164 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
165 struct snd_pcm_runtime *runtime)
167 snd_pcm_uframes_t avail;
169 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
170 avail = snd_pcm_playback_avail(runtime);
171 else
172 avail = snd_pcm_capture_avail(runtime);
173 if (avail > runtime->avail_max)
174 runtime->avail_max = avail;
175 if (avail >= runtime->stop_threshold) {
176 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
177 snd_pcm_drain_done(substream);
178 else
179 xrun(substream);
180 return -EPIPE;
182 if (avail >= runtime->control->avail_min)
183 wake_up(&runtime->sleep);
184 return 0;
187 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
189 struct snd_pcm_runtime *runtime = substream->runtime;
190 snd_pcm_uframes_t pos;
191 snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
192 snd_pcm_sframes_t delta;
194 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
195 if (pos == SNDRV_PCM_POS_XRUN) {
196 xrun(substream);
197 return -EPIPE;
199 if (runtime->period_size == runtime->buffer_size)
200 goto __next_buf;
201 new_hw_ptr = runtime->hw_ptr_base + pos;
202 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
204 delta = hw_ptr_interrupt - new_hw_ptr;
205 if (delta > 0) {
206 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
207 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
208 if (runtime->periods > 1 && substream->pstr->xrun_debug) {
209 snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
210 if (substream->pstr->xrun_debug > 1)
211 dump_stack();
213 #endif
214 return 0;
216 __next_buf:
217 runtime->hw_ptr_base += runtime->buffer_size;
218 if (runtime->hw_ptr_base == runtime->boundary)
219 runtime->hw_ptr_base = 0;
220 new_hw_ptr = runtime->hw_ptr_base + pos;
223 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
224 runtime->silence_size > 0)
225 snd_pcm_playback_silence(substream, new_hw_ptr);
227 runtime->status->hw_ptr = new_hw_ptr;
228 runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
230 return snd_pcm_update_hw_ptr_post(substream, runtime);
233 /* CAUTION: call it with irq disabled */
234 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
236 struct snd_pcm_runtime *runtime = substream->runtime;
237 snd_pcm_uframes_t pos;
238 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
239 snd_pcm_sframes_t delta;
241 old_hw_ptr = runtime->status->hw_ptr;
242 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
243 if (pos == SNDRV_PCM_POS_XRUN) {
244 xrun(substream);
245 return -EPIPE;
247 new_hw_ptr = runtime->hw_ptr_base + pos;
249 delta = old_hw_ptr - new_hw_ptr;
250 if (delta > 0) {
251 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
252 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
253 if (runtime->periods > 2 && substream->pstr->xrun_debug) {
254 snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
255 if (substream->pstr->xrun_debug > 1)
256 dump_stack();
258 #endif
259 return 0;
261 runtime->hw_ptr_base += runtime->buffer_size;
262 if (runtime->hw_ptr_base == runtime->boundary)
263 runtime->hw_ptr_base = 0;
264 new_hw_ptr = runtime->hw_ptr_base + pos;
266 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
267 runtime->silence_size > 0)
268 snd_pcm_playback_silence(substream, new_hw_ptr);
270 runtime->status->hw_ptr = new_hw_ptr;
272 return snd_pcm_update_hw_ptr_post(substream, runtime);
276 * snd_pcm_set_ops - set the PCM operators
277 * @pcm: the pcm instance
278 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
279 * @ops: the operator table
281 * Sets the given PCM operators to the pcm instance.
283 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
285 struct snd_pcm_str *stream = &pcm->streams[direction];
286 struct snd_pcm_substream *substream;
288 for (substream = stream->substream; substream != NULL; substream = substream->next)
289 substream->ops = ops;
292 EXPORT_SYMBOL(snd_pcm_set_ops);
295 * snd_pcm_sync - set the PCM sync id
296 * @substream: the pcm substream
298 * Sets the PCM sync identifier for the card.
300 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
302 struct snd_pcm_runtime *runtime = substream->runtime;
304 runtime->sync.id32[0] = substream->pcm->card->number;
305 runtime->sync.id32[1] = -1;
306 runtime->sync.id32[2] = -1;
307 runtime->sync.id32[3] = -1;
310 EXPORT_SYMBOL(snd_pcm_set_sync);
313 * Standard ioctl routine
316 static inline unsigned int div32(unsigned int a, unsigned int b,
317 unsigned int *r)
319 if (b == 0) {
320 *r = 0;
321 return UINT_MAX;
323 *r = a % b;
324 return a / b;
327 static inline unsigned int div_down(unsigned int a, unsigned int b)
329 if (b == 0)
330 return UINT_MAX;
331 return a / b;
334 static inline unsigned int div_up(unsigned int a, unsigned int b)
336 unsigned int r;
337 unsigned int q;
338 if (b == 0)
339 return UINT_MAX;
340 q = div32(a, b, &r);
341 if (r)
342 ++q;
343 return q;
346 static inline unsigned int mul(unsigned int a, unsigned int b)
348 if (a == 0)
349 return 0;
350 if (div_down(UINT_MAX, a) < b)
351 return UINT_MAX;
352 return a * b;
355 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
356 unsigned int c, unsigned int *r)
358 u_int64_t n = (u_int64_t) a * b;
359 if (c == 0) {
360 snd_assert(n > 0, );
361 *r = 0;
362 return UINT_MAX;
364 div64_32(&n, c, r);
365 if (n >= UINT_MAX) {
366 *r = 0;
367 return UINT_MAX;
369 return n;
373 * snd_interval_refine - refine the interval value of configurator
374 * @i: the interval value to refine
375 * @v: the interval value to refer to
377 * Refines the interval value with the reference value.
378 * The interval is changed to the range satisfying both intervals.
379 * The interval status (min, max, integer, etc.) are evaluated.
381 * Returns non-zero if the value is changed, zero if not changed.
383 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
385 int changed = 0;
386 snd_assert(!snd_interval_empty(i), return -EINVAL);
387 if (i->min < v->min) {
388 i->min = v->min;
389 i->openmin = v->openmin;
390 changed = 1;
391 } else if (i->min == v->min && !i->openmin && v->openmin) {
392 i->openmin = 1;
393 changed = 1;
395 if (i->max > v->max) {
396 i->max = v->max;
397 i->openmax = v->openmax;
398 changed = 1;
399 } else if (i->max == v->max && !i->openmax && v->openmax) {
400 i->openmax = 1;
401 changed = 1;
403 if (!i->integer && v->integer) {
404 i->integer = 1;
405 changed = 1;
407 if (i->integer) {
408 if (i->openmin) {
409 i->min++;
410 i->openmin = 0;
412 if (i->openmax) {
413 i->max--;
414 i->openmax = 0;
416 } else if (!i->openmin && !i->openmax && i->min == i->max)
417 i->integer = 1;
418 if (snd_interval_checkempty(i)) {
419 snd_interval_none(i);
420 return -EINVAL;
422 return changed;
425 EXPORT_SYMBOL(snd_interval_refine);
427 static int snd_interval_refine_first(struct snd_interval *i)
429 snd_assert(!snd_interval_empty(i), return -EINVAL);
430 if (snd_interval_single(i))
431 return 0;
432 i->max = i->min;
433 i->openmax = i->openmin;
434 if (i->openmax)
435 i->max++;
436 return 1;
439 static int snd_interval_refine_last(struct snd_interval *i)
441 snd_assert(!snd_interval_empty(i), return -EINVAL);
442 if (snd_interval_single(i))
443 return 0;
444 i->min = i->max;
445 i->openmin = i->openmax;
446 if (i->openmin)
447 i->min--;
448 return 1;
451 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
453 if (a->empty || b->empty) {
454 snd_interval_none(c);
455 return;
457 c->empty = 0;
458 c->min = mul(a->min, b->min);
459 c->openmin = (a->openmin || b->openmin);
460 c->max = mul(a->max, b->max);
461 c->openmax = (a->openmax || b->openmax);
462 c->integer = (a->integer && b->integer);
466 * snd_interval_div - refine the interval value with division
467 * @a: dividend
468 * @b: divisor
469 * @c: quotient
471 * c = a / b
473 * Returns non-zero if the value is changed, zero if not changed.
475 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
477 unsigned int r;
478 if (a->empty || b->empty) {
479 snd_interval_none(c);
480 return;
482 c->empty = 0;
483 c->min = div32(a->min, b->max, &r);
484 c->openmin = (r || a->openmin || b->openmax);
485 if (b->min > 0) {
486 c->max = div32(a->max, b->min, &r);
487 if (r) {
488 c->max++;
489 c->openmax = 1;
490 } else
491 c->openmax = (a->openmax || b->openmin);
492 } else {
493 c->max = UINT_MAX;
494 c->openmax = 0;
496 c->integer = 0;
500 * snd_interval_muldivk - refine the interval value
501 * @a: dividend 1
502 * @b: dividend 2
503 * @k: divisor (as integer)
504 * @c: result
506 * c = a * b / k
508 * Returns non-zero if the value is changed, zero if not changed.
510 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
511 unsigned int k, struct snd_interval *c)
513 unsigned int r;
514 if (a->empty || b->empty) {
515 snd_interval_none(c);
516 return;
518 c->empty = 0;
519 c->min = muldiv32(a->min, b->min, k, &r);
520 c->openmin = (r || a->openmin || b->openmin);
521 c->max = muldiv32(a->max, b->max, k, &r);
522 if (r) {
523 c->max++;
524 c->openmax = 1;
525 } else
526 c->openmax = (a->openmax || b->openmax);
527 c->integer = 0;
531 * snd_interval_mulkdiv - refine the interval value
532 * @a: dividend 1
533 * @k: dividend 2 (as integer)
534 * @b: divisor
535 * @c: result
537 * c = a * k / b
539 * Returns non-zero if the value is changed, zero if not changed.
541 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
542 const struct snd_interval *b, struct snd_interval *c)
544 unsigned int r;
545 if (a->empty || b->empty) {
546 snd_interval_none(c);
547 return;
549 c->empty = 0;
550 c->min = muldiv32(a->min, k, b->max, &r);
551 c->openmin = (r || a->openmin || b->openmax);
552 if (b->min > 0) {
553 c->max = muldiv32(a->max, k, b->min, &r);
554 if (r) {
555 c->max++;
556 c->openmax = 1;
557 } else
558 c->openmax = (a->openmax || b->openmin);
559 } else {
560 c->max = UINT_MAX;
561 c->openmax = 0;
563 c->integer = 0;
566 /* ---- */
570 * snd_interval_ratnum - refine the interval value
571 * @i: interval to refine
572 * @rats_count: number of ratnum_t
573 * @rats: ratnum_t array
574 * @nump: pointer to store the resultant numerator
575 * @denp: pointer to store the resultant denominator
577 * Returns non-zero if the value is changed, zero if not changed.
579 int snd_interval_ratnum(struct snd_interval *i,
580 unsigned int rats_count, struct snd_ratnum *rats,
581 unsigned int *nump, unsigned int *denp)
583 unsigned int best_num, best_diff, best_den;
584 unsigned int k;
585 struct snd_interval t;
586 int err;
588 best_num = best_den = best_diff = 0;
589 for (k = 0; k < rats_count; ++k) {
590 unsigned int num = rats[k].num;
591 unsigned int den;
592 unsigned int q = i->min;
593 int diff;
594 if (q == 0)
595 q = 1;
596 den = div_down(num, q);
597 if (den < rats[k].den_min)
598 continue;
599 if (den > rats[k].den_max)
600 den = rats[k].den_max;
601 else {
602 unsigned int r;
603 r = (den - rats[k].den_min) % rats[k].den_step;
604 if (r != 0)
605 den -= r;
607 diff = num - q * den;
608 if (best_num == 0 ||
609 diff * best_den < best_diff * den) {
610 best_diff = diff;
611 best_den = den;
612 best_num = num;
615 if (best_den == 0) {
616 i->empty = 1;
617 return -EINVAL;
619 t.min = div_down(best_num, best_den);
620 t.openmin = !!(best_num % best_den);
622 best_num = best_den = best_diff = 0;
623 for (k = 0; k < rats_count; ++k) {
624 unsigned int num = rats[k].num;
625 unsigned int den;
626 unsigned int q = i->max;
627 int diff;
628 if (q == 0) {
629 i->empty = 1;
630 return -EINVAL;
632 den = div_up(num, q);
633 if (den > rats[k].den_max)
634 continue;
635 if (den < rats[k].den_min)
636 den = rats[k].den_min;
637 else {
638 unsigned int r;
639 r = (den - rats[k].den_min) % rats[k].den_step;
640 if (r != 0)
641 den += rats[k].den_step - r;
643 diff = q * den - num;
644 if (best_num == 0 ||
645 diff * best_den < best_diff * den) {
646 best_diff = diff;
647 best_den = den;
648 best_num = num;
651 if (best_den == 0) {
652 i->empty = 1;
653 return -EINVAL;
655 t.max = div_up(best_num, best_den);
656 t.openmax = !!(best_num % best_den);
657 t.integer = 0;
658 err = snd_interval_refine(i, &t);
659 if (err < 0)
660 return err;
662 if (snd_interval_single(i)) {
663 if (nump)
664 *nump = best_num;
665 if (denp)
666 *denp = best_den;
668 return err;
671 EXPORT_SYMBOL(snd_interval_ratnum);
674 * snd_interval_ratden - refine the interval value
675 * @i: interval to refine
676 * @rats_count: number of struct ratden
677 * @rats: struct ratden array
678 * @nump: pointer to store the resultant numerator
679 * @denp: pointer to store the resultant denominator
681 * Returns non-zero if the value is changed, zero if not changed.
683 static int snd_interval_ratden(struct snd_interval *i,
684 unsigned int rats_count, struct snd_ratden *rats,
685 unsigned int *nump, unsigned int *denp)
687 unsigned int best_num, best_diff, best_den;
688 unsigned int k;
689 struct snd_interval t;
690 int err;
692 best_num = best_den = best_diff = 0;
693 for (k = 0; k < rats_count; ++k) {
694 unsigned int num;
695 unsigned int den = rats[k].den;
696 unsigned int q = i->min;
697 int diff;
698 num = mul(q, den);
699 if (num > rats[k].num_max)
700 continue;
701 if (num < rats[k].num_min)
702 num = rats[k].num_max;
703 else {
704 unsigned int r;
705 r = (num - rats[k].num_min) % rats[k].num_step;
706 if (r != 0)
707 num += rats[k].num_step - r;
709 diff = num - q * den;
710 if (best_num == 0 ||
711 diff * best_den < best_diff * den) {
712 best_diff = diff;
713 best_den = den;
714 best_num = num;
717 if (best_den == 0) {
718 i->empty = 1;
719 return -EINVAL;
721 t.min = div_down(best_num, best_den);
722 t.openmin = !!(best_num % best_den);
724 best_num = best_den = best_diff = 0;
725 for (k = 0; k < rats_count; ++k) {
726 unsigned int num;
727 unsigned int den = rats[k].den;
728 unsigned int q = i->max;
729 int diff;
730 num = mul(q, den);
731 if (num < rats[k].num_min)
732 continue;
733 if (num > rats[k].num_max)
734 num = rats[k].num_max;
735 else {
736 unsigned int r;
737 r = (num - rats[k].num_min) % rats[k].num_step;
738 if (r != 0)
739 num -= r;
741 diff = q * den - num;
742 if (best_num == 0 ||
743 diff * best_den < best_diff * den) {
744 best_diff = diff;
745 best_den = den;
746 best_num = num;
749 if (best_den == 0) {
750 i->empty = 1;
751 return -EINVAL;
753 t.max = div_up(best_num, best_den);
754 t.openmax = !!(best_num % best_den);
755 t.integer = 0;
756 err = snd_interval_refine(i, &t);
757 if (err < 0)
758 return err;
760 if (snd_interval_single(i)) {
761 if (nump)
762 *nump = best_num;
763 if (denp)
764 *denp = best_den;
766 return err;
770 * snd_interval_list - refine the interval value from the list
771 * @i: the interval value to refine
772 * @count: the number of elements in the list
773 * @list: the value list
774 * @mask: the bit-mask to evaluate
776 * Refines the interval value from the list.
777 * When mask is non-zero, only the elements corresponding to bit 1 are
778 * evaluated.
780 * Returns non-zero if the value is changed, zero if not changed.
782 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
784 unsigned int k;
785 int changed = 0;
786 for (k = 0; k < count; k++) {
787 if (mask && !(mask & (1 << k)))
788 continue;
789 if (i->min == list[k] && !i->openmin)
790 goto _l1;
791 if (i->min < list[k]) {
792 i->min = list[k];
793 i->openmin = 0;
794 changed = 1;
795 goto _l1;
798 i->empty = 1;
799 return -EINVAL;
800 _l1:
801 for (k = count; k-- > 0;) {
802 if (mask && !(mask & (1 << k)))
803 continue;
804 if (i->max == list[k] && !i->openmax)
805 goto _l2;
806 if (i->max > list[k]) {
807 i->max = list[k];
808 i->openmax = 0;
809 changed = 1;
810 goto _l2;
813 i->empty = 1;
814 return -EINVAL;
815 _l2:
816 if (snd_interval_checkempty(i)) {
817 i->empty = 1;
818 return -EINVAL;
820 return changed;
823 EXPORT_SYMBOL(snd_interval_list);
825 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
827 unsigned int n;
828 int changed = 0;
829 n = (i->min - min) % step;
830 if (n != 0 || i->openmin) {
831 i->min += step - n;
832 changed = 1;
834 n = (i->max - min) % step;
835 if (n != 0 || i->openmax) {
836 i->max -= n;
837 changed = 1;
839 if (snd_interval_checkempty(i)) {
840 i->empty = 1;
841 return -EINVAL;
843 return changed;
846 /* Info constraints helpers */
849 * snd_pcm_hw_rule_add - add the hw-constraint rule
850 * @runtime: the pcm runtime instance
851 * @cond: condition bits
852 * @var: the variable to evaluate
853 * @func: the evaluation function
854 * @private: the private data pointer passed to function
855 * @dep: the dependent variables
857 * Returns zero if successful, or a negative error code on failure.
859 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
860 int var,
861 snd_pcm_hw_rule_func_t func, void *private,
862 int dep, ...)
864 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
865 struct snd_pcm_hw_rule *c;
866 unsigned int k;
867 va_list args;
868 va_start(args, dep);
869 if (constrs->rules_num >= constrs->rules_all) {
870 struct snd_pcm_hw_rule *new;
871 unsigned int new_rules = constrs->rules_all + 16;
872 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
873 if (!new)
874 return -ENOMEM;
875 if (constrs->rules) {
876 memcpy(new, constrs->rules,
877 constrs->rules_num * sizeof(*c));
878 kfree(constrs->rules);
880 constrs->rules = new;
881 constrs->rules_all = new_rules;
883 c = &constrs->rules[constrs->rules_num];
884 c->cond = cond;
885 c->func = func;
886 c->var = var;
887 c->private = private;
888 k = 0;
889 while (1) {
890 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
891 c->deps[k++] = dep;
892 if (dep < 0)
893 break;
894 dep = va_arg(args, int);
896 constrs->rules_num++;
897 va_end(args);
898 return 0;
901 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
904 * snd_pcm_hw_constraint_mask
905 * @runtime: PCM runtime instance
906 * @var: hw_params variable to apply the mask
907 * @mask: the bitmap mask
909 * Apply the constraint of the given bitmap mask to a mask parameter.
911 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
912 u_int32_t mask)
914 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
915 struct snd_mask *maskp = constrs_mask(constrs, var);
916 *maskp->bits &= mask;
917 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
918 if (*maskp->bits == 0)
919 return -EINVAL;
920 return 0;
924 * snd_pcm_hw_constraint_mask64
925 * @runtime: PCM runtime instance
926 * @var: hw_params variable to apply the mask
927 * @mask: the 64bit bitmap mask
929 * Apply the constraint of the given bitmap mask to a mask parameter.
931 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
932 u_int64_t mask)
934 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
935 struct snd_mask *maskp = constrs_mask(constrs, var);
936 maskp->bits[0] &= (u_int32_t)mask;
937 maskp->bits[1] &= (u_int32_t)(mask >> 32);
938 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
939 if (! maskp->bits[0] && ! maskp->bits[1])
940 return -EINVAL;
941 return 0;
945 * snd_pcm_hw_constraint_integer
946 * @runtime: PCM runtime instance
947 * @var: hw_params variable to apply the integer constraint
949 * Apply the constraint of integer to an interval parameter.
951 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
953 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
954 return snd_interval_setinteger(constrs_interval(constrs, var));
957 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
960 * snd_pcm_hw_constraint_minmax
961 * @runtime: PCM runtime instance
962 * @var: hw_params variable to apply the range
963 * @min: the minimal value
964 * @max: the maximal value
966 * Apply the min/max range constraint to an interval parameter.
968 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
969 unsigned int min, unsigned int max)
971 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
972 struct snd_interval t;
973 t.min = min;
974 t.max = max;
975 t.openmin = t.openmax = 0;
976 t.integer = 0;
977 return snd_interval_refine(constrs_interval(constrs, var), &t);
980 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
982 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
983 struct snd_pcm_hw_rule *rule)
985 struct snd_pcm_hw_constraint_list *list = rule->private;
986 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
991 * snd_pcm_hw_constraint_list
992 * @runtime: PCM runtime instance
993 * @cond: condition bits
994 * @var: hw_params variable to apply the list constraint
995 * @l: list
997 * Apply the list of constraints to an interval parameter.
999 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1000 unsigned int cond,
1001 snd_pcm_hw_param_t var,
1002 struct snd_pcm_hw_constraint_list *l)
1004 return snd_pcm_hw_rule_add(runtime, cond, var,
1005 snd_pcm_hw_rule_list, l,
1006 var, -1);
1009 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1011 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1012 struct snd_pcm_hw_rule *rule)
1014 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1015 unsigned int num = 0, den = 0;
1016 int err;
1017 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1018 r->nrats, r->rats, &num, &den);
1019 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1020 params->rate_num = num;
1021 params->rate_den = den;
1023 return err;
1027 * snd_pcm_hw_constraint_ratnums
1028 * @runtime: PCM runtime instance
1029 * @cond: condition bits
1030 * @var: hw_params variable to apply the ratnums constraint
1031 * @r: struct snd_ratnums constriants
1033 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1034 unsigned int cond,
1035 snd_pcm_hw_param_t var,
1036 struct snd_pcm_hw_constraint_ratnums *r)
1038 return snd_pcm_hw_rule_add(runtime, cond, var,
1039 snd_pcm_hw_rule_ratnums, r,
1040 var, -1);
1043 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1045 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1046 struct snd_pcm_hw_rule *rule)
1048 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1049 unsigned int num = 0, den = 0;
1050 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1051 r->nrats, r->rats, &num, &den);
1052 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1053 params->rate_num = num;
1054 params->rate_den = den;
1056 return err;
1060 * snd_pcm_hw_constraint_ratdens
1061 * @runtime: PCM runtime instance
1062 * @cond: condition bits
1063 * @var: hw_params variable to apply the ratdens constraint
1064 * @r: struct snd_ratdens constriants
1066 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1067 unsigned int cond,
1068 snd_pcm_hw_param_t var,
1069 struct snd_pcm_hw_constraint_ratdens *r)
1071 return snd_pcm_hw_rule_add(runtime, cond, var,
1072 snd_pcm_hw_rule_ratdens, r,
1073 var, -1);
1076 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1078 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1079 struct snd_pcm_hw_rule *rule)
1081 unsigned int l = (unsigned long) rule->private;
1082 int width = l & 0xffff;
1083 unsigned int msbits = l >> 16;
1084 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1085 if (snd_interval_single(i) && snd_interval_value(i) == width)
1086 params->msbits = msbits;
1087 return 0;
1091 * snd_pcm_hw_constraint_msbits
1092 * @runtime: PCM runtime instance
1093 * @cond: condition bits
1094 * @width: sample bits width
1095 * @msbits: msbits width
1097 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1098 unsigned int cond,
1099 unsigned int width,
1100 unsigned int msbits)
1102 unsigned long l = (msbits << 16) | width;
1103 return snd_pcm_hw_rule_add(runtime, cond, -1,
1104 snd_pcm_hw_rule_msbits,
1105 (void*) l,
1106 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1109 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1111 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1112 struct snd_pcm_hw_rule *rule)
1114 unsigned long step = (unsigned long) rule->private;
1115 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1119 * snd_pcm_hw_constraint_step
1120 * @runtime: PCM runtime instance
1121 * @cond: condition bits
1122 * @var: hw_params variable to apply the step constraint
1123 * @step: step size
1125 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1126 unsigned int cond,
1127 snd_pcm_hw_param_t var,
1128 unsigned long step)
1130 return snd_pcm_hw_rule_add(runtime, cond, var,
1131 snd_pcm_hw_rule_step, (void *) step,
1132 var, -1);
1135 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1137 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1139 static int pow2_sizes[] = {
1140 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1141 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1142 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1143 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1145 return snd_interval_list(hw_param_interval(params, rule->var),
1146 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1150 * snd_pcm_hw_constraint_pow2
1151 * @runtime: PCM runtime instance
1152 * @cond: condition bits
1153 * @var: hw_params variable to apply the power-of-2 constraint
1155 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1156 unsigned int cond,
1157 snd_pcm_hw_param_t var)
1159 return snd_pcm_hw_rule_add(runtime, cond, var,
1160 snd_pcm_hw_rule_pow2, NULL,
1161 var, -1);
1164 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1166 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1167 snd_pcm_hw_param_t var)
1169 if (hw_is_mask(var)) {
1170 snd_mask_any(hw_param_mask(params, var));
1171 params->cmask |= 1 << var;
1172 params->rmask |= 1 << var;
1173 return;
1175 if (hw_is_interval(var)) {
1176 snd_interval_any(hw_param_interval(params, var));
1177 params->cmask |= 1 << var;
1178 params->rmask |= 1 << var;
1179 return;
1181 snd_BUG();
1184 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1186 unsigned int k;
1187 memset(params, 0, sizeof(*params));
1188 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1189 _snd_pcm_hw_param_any(params, k);
1190 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1191 _snd_pcm_hw_param_any(params, k);
1192 params->info = ~0U;
1195 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1198 * snd_pcm_hw_param_value
1199 * @params: the hw_params instance
1200 * @var: parameter to retrieve
1201 * @dir: pointer to the direction (-1,0,1) or NULL
1203 * Return the value for field PAR if it's fixed in configuration space
1204 * defined by PARAMS. Return -EINVAL otherwise
1206 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1207 snd_pcm_hw_param_t var, int *dir)
1209 if (hw_is_mask(var)) {
1210 const struct snd_mask *mask = hw_param_mask_c(params, var);
1211 if (!snd_mask_single(mask))
1212 return -EINVAL;
1213 if (dir)
1214 *dir = 0;
1215 return snd_mask_value(mask);
1217 if (hw_is_interval(var)) {
1218 const struct snd_interval *i = hw_param_interval_c(params, var);
1219 if (!snd_interval_single(i))
1220 return -EINVAL;
1221 if (dir)
1222 *dir = i->openmin;
1223 return snd_interval_value(i);
1225 return -EINVAL;
1228 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1230 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1231 snd_pcm_hw_param_t var)
1233 if (hw_is_mask(var)) {
1234 snd_mask_none(hw_param_mask(params, var));
1235 params->cmask |= 1 << var;
1236 params->rmask |= 1 << var;
1237 } else if (hw_is_interval(var)) {
1238 snd_interval_none(hw_param_interval(params, var));
1239 params->cmask |= 1 << var;
1240 params->rmask |= 1 << var;
1241 } else {
1242 snd_BUG();
1246 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1248 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1249 snd_pcm_hw_param_t var)
1251 int changed;
1252 if (hw_is_mask(var))
1253 changed = snd_mask_refine_first(hw_param_mask(params, var));
1254 else if (hw_is_interval(var))
1255 changed = snd_interval_refine_first(hw_param_interval(params, var));
1256 else
1257 return -EINVAL;
1258 if (changed) {
1259 params->cmask |= 1 << var;
1260 params->rmask |= 1 << var;
1262 return changed;
1267 * snd_pcm_hw_param_first
1268 * @pcm: PCM instance
1269 * @params: the hw_params instance
1270 * @var: parameter to retrieve
1271 * @dir: pointer to the direction (-1,0,1) or NULL
1273 * Inside configuration space defined by PARAMS remove from PAR all
1274 * values > minimum. Reduce configuration space accordingly.
1275 * Return the minimum.
1277 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1278 struct snd_pcm_hw_params *params,
1279 snd_pcm_hw_param_t var, int *dir)
1281 int changed = _snd_pcm_hw_param_first(params, var);
1282 if (changed < 0)
1283 return changed;
1284 if (params->rmask) {
1285 int err = snd_pcm_hw_refine(pcm, params);
1286 snd_assert(err >= 0, return err);
1288 return snd_pcm_hw_param_value(params, var, dir);
1291 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1293 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1294 snd_pcm_hw_param_t var)
1296 int changed;
1297 if (hw_is_mask(var))
1298 changed = snd_mask_refine_last(hw_param_mask(params, var));
1299 else if (hw_is_interval(var))
1300 changed = snd_interval_refine_last(hw_param_interval(params, var));
1301 else
1302 return -EINVAL;
1303 if (changed) {
1304 params->cmask |= 1 << var;
1305 params->rmask |= 1 << var;
1307 return changed;
1312 * snd_pcm_hw_param_last
1313 * @pcm: PCM instance
1314 * @params: the hw_params instance
1315 * @var: parameter to retrieve
1316 * @dir: pointer to the direction (-1,0,1) or NULL
1318 * Inside configuration space defined by PARAMS remove from PAR all
1319 * values < maximum. Reduce configuration space accordingly.
1320 * Return the maximum.
1322 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1323 struct snd_pcm_hw_params *params,
1324 snd_pcm_hw_param_t var, int *dir)
1326 int changed = _snd_pcm_hw_param_last(params, var);
1327 if (changed < 0)
1328 return changed;
1329 if (params->rmask) {
1330 int err = snd_pcm_hw_refine(pcm, params);
1331 snd_assert(err >= 0, return err);
1333 return snd_pcm_hw_param_value(params, var, dir);
1336 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1339 * snd_pcm_hw_param_choose
1340 * @pcm: PCM instance
1341 * @params: the hw_params instance
1343 * Choose one configuration from configuration space defined by PARAMS
1344 * The configuration chosen is that obtained fixing in this order:
1345 * first access, first format, first subformat, min channels,
1346 * min rate, min period time, max buffer size, min tick time
1348 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1349 struct snd_pcm_hw_params *params)
1351 static int vars[] = {
1352 SNDRV_PCM_HW_PARAM_ACCESS,
1353 SNDRV_PCM_HW_PARAM_FORMAT,
1354 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1355 SNDRV_PCM_HW_PARAM_CHANNELS,
1356 SNDRV_PCM_HW_PARAM_RATE,
1357 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1358 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1359 SNDRV_PCM_HW_PARAM_TICK_TIME,
1362 int err, *v;
1364 for (v = vars; *v != -1; v++) {
1365 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1366 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1367 else
1368 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1369 snd_assert(err >= 0, return err);
1371 return 0;
1374 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1375 void *arg)
1377 struct snd_pcm_runtime *runtime = substream->runtime;
1378 unsigned long flags;
1379 snd_pcm_stream_lock_irqsave(substream, flags);
1380 if (snd_pcm_running(substream) &&
1381 snd_pcm_update_hw_ptr(substream) >= 0)
1382 runtime->status->hw_ptr %= runtime->buffer_size;
1383 else
1384 runtime->status->hw_ptr = 0;
1385 snd_pcm_stream_unlock_irqrestore(substream, flags);
1386 return 0;
1389 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1390 void *arg)
1392 struct snd_pcm_channel_info *info = arg;
1393 struct snd_pcm_runtime *runtime = substream->runtime;
1394 int width;
1395 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1396 info->offset = -1;
1397 return 0;
1399 width = snd_pcm_format_physical_width(runtime->format);
1400 if (width < 0)
1401 return width;
1402 info->offset = 0;
1403 switch (runtime->access) {
1404 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1405 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1406 info->first = info->channel * width;
1407 info->step = runtime->channels * width;
1408 break;
1409 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1410 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1412 size_t size = runtime->dma_bytes / runtime->channels;
1413 info->first = info->channel * size * 8;
1414 info->step = width;
1415 break;
1417 default:
1418 snd_BUG();
1419 break;
1421 return 0;
1425 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1426 * @substream: the pcm substream instance
1427 * @cmd: ioctl command
1428 * @arg: ioctl argument
1430 * Processes the generic ioctl commands for PCM.
1431 * Can be passed as the ioctl callback for PCM ops.
1433 * Returns zero if successful, or a negative error code on failure.
1435 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1436 unsigned int cmd, void *arg)
1438 switch (cmd) {
1439 case SNDRV_PCM_IOCTL1_INFO:
1440 return 0;
1441 case SNDRV_PCM_IOCTL1_RESET:
1442 return snd_pcm_lib_ioctl_reset(substream, arg);
1443 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1444 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1446 return -ENXIO;
1449 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1452 * Conditions
1455 static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
1456 unsigned long ticks)
1458 struct snd_pcm_runtime *runtime = substream->runtime;
1459 if (ticks == 0)
1460 del_timer(&runtime->tick_timer);
1461 else {
1462 ticks += (1000000 / HZ) - 1;
1463 ticks /= (1000000 / HZ);
1464 mod_timer(&runtime->tick_timer, jiffies + ticks);
1468 /* Temporary alias */
1469 void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
1471 snd_pcm_system_tick_set(substream, ticks);
1474 void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
1476 struct snd_pcm_runtime *runtime = substream->runtime;
1477 snd_pcm_uframes_t frames = ULONG_MAX;
1478 snd_pcm_uframes_t avail, dist;
1479 unsigned int ticks;
1480 u_int64_t n;
1481 u_int32_t r;
1482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1483 if (runtime->silence_size >= runtime->boundary) {
1484 frames = 1;
1485 } else if (runtime->silence_size > 0 &&
1486 runtime->silence_filled < runtime->buffer_size) {
1487 snd_pcm_sframes_t noise_dist;
1488 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
1489 if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold)
1490 frames = noise_dist - runtime->silence_threshold;
1492 avail = snd_pcm_playback_avail(runtime);
1493 } else {
1494 avail = snd_pcm_capture_avail(runtime);
1496 if (avail < runtime->control->avail_min) {
1497 snd_pcm_sframes_t n = runtime->control->avail_min - avail;
1498 if (n > 0 && frames > (snd_pcm_uframes_t)n)
1499 frames = n;
1501 if (avail < runtime->buffer_size) {
1502 snd_pcm_sframes_t n = runtime->buffer_size - avail;
1503 if (n > 0 && frames > (snd_pcm_uframes_t)n)
1504 frames = n;
1506 if (frames == ULONG_MAX) {
1507 snd_pcm_tick_set(substream, 0);
1508 return;
1510 dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
1511 /* Distance to next interrupt */
1512 dist = runtime->period_size - dist % runtime->period_size;
1513 if (dist <= frames) {
1514 snd_pcm_tick_set(substream, 0);
1515 return;
1517 /* the base time is us */
1518 n = frames;
1519 n *= 1000000;
1520 div64_32(&n, runtime->tick_time * runtime->rate, &r);
1521 ticks = n + (r > 0 ? 1 : 0);
1522 if (ticks < runtime->sleep_min)
1523 ticks = runtime->sleep_min;
1524 snd_pcm_tick_set(substream, (unsigned long) ticks);
1527 void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
1529 struct snd_pcm_runtime *runtime;
1530 unsigned long flags;
1532 snd_assert(substream != NULL, return);
1533 runtime = substream->runtime;
1534 snd_assert(runtime != NULL, return);
1536 snd_pcm_stream_lock_irqsave(substream, flags);
1537 if (!snd_pcm_running(substream) ||
1538 snd_pcm_update_hw_ptr(substream) < 0)
1539 goto _end;
1540 if (runtime->sleep_min)
1541 snd_pcm_tick_prepare(substream);
1542 _end:
1543 snd_pcm_stream_unlock_irqrestore(substream, flags);
1547 * snd_pcm_period_elapsed - update the pcm status for the next period
1548 * @substream: the pcm substream instance
1550 * This function is called from the interrupt handler when the
1551 * PCM has processed the period size. It will update the current
1552 * pointer, set up the tick, wake up sleepers, etc.
1554 * Even if more than one periods have elapsed since the last call, you
1555 * have to call this only once.
1557 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1559 struct snd_pcm_runtime *runtime;
1560 unsigned long flags;
1562 snd_assert(substream != NULL, return);
1563 runtime = substream->runtime;
1564 snd_assert(runtime != NULL, return);
1566 if (runtime->transfer_ack_begin)
1567 runtime->transfer_ack_begin(substream);
1569 snd_pcm_stream_lock_irqsave(substream, flags);
1570 if (!snd_pcm_running(substream) ||
1571 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1572 goto _end;
1574 if (substream->timer_running)
1575 snd_timer_interrupt(substream->timer, 1);
1576 if (runtime->sleep_min)
1577 snd_pcm_tick_prepare(substream);
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);
1587 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1588 unsigned int hwoff,
1589 unsigned long data, unsigned int off,
1590 snd_pcm_uframes_t frames)
1592 struct snd_pcm_runtime *runtime = substream->runtime;
1593 int err;
1594 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1595 if (substream->ops->copy) {
1596 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1597 return err;
1598 } else {
1599 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1600 snd_assert(runtime->dma_area, return -EFAULT);
1601 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1602 return -EFAULT;
1604 return 0;
1607 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1608 unsigned long data, unsigned int off,
1609 snd_pcm_uframes_t size);
1611 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1612 unsigned long data,
1613 snd_pcm_uframes_t size,
1614 int nonblock,
1615 transfer_f transfer)
1617 struct snd_pcm_runtime *runtime = substream->runtime;
1618 snd_pcm_uframes_t xfer = 0;
1619 snd_pcm_uframes_t offset = 0;
1620 int err = 0;
1622 if (size == 0)
1623 return 0;
1624 if (size > runtime->xfer_align)
1625 size -= size % runtime->xfer_align;
1627 snd_pcm_stream_lock_irq(substream);
1628 switch (runtime->status->state) {
1629 case SNDRV_PCM_STATE_PREPARED:
1630 case SNDRV_PCM_STATE_RUNNING:
1631 case SNDRV_PCM_STATE_PAUSED:
1632 break;
1633 case SNDRV_PCM_STATE_XRUN:
1634 err = -EPIPE;
1635 goto _end_unlock;
1636 case SNDRV_PCM_STATE_SUSPENDED:
1637 err = -ESTRPIPE;
1638 goto _end_unlock;
1639 default:
1640 err = -EBADFD;
1641 goto _end_unlock;
1644 while (size > 0) {
1645 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1646 snd_pcm_uframes_t avail;
1647 snd_pcm_uframes_t cont;
1648 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1649 snd_pcm_update_hw_ptr(substream);
1650 avail = snd_pcm_playback_avail(runtime);
1651 if (((avail < runtime->control->avail_min && size > avail) ||
1652 (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
1653 wait_queue_t wait;
1654 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
1655 long tout;
1657 if (nonblock) {
1658 err = -EAGAIN;
1659 goto _end_unlock;
1662 init_waitqueue_entry(&wait, current);
1663 add_wait_queue(&runtime->sleep, &wait);
1664 while (1) {
1665 if (signal_pending(current)) {
1666 state = SIGNALED;
1667 break;
1669 set_current_state(TASK_INTERRUPTIBLE);
1670 snd_pcm_stream_unlock_irq(substream);
1671 tout = schedule_timeout(10 * HZ);
1672 snd_pcm_stream_lock_irq(substream);
1673 if (tout == 0) {
1674 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
1675 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
1676 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1677 break;
1680 switch (runtime->status->state) {
1681 case SNDRV_PCM_STATE_XRUN:
1682 case SNDRV_PCM_STATE_DRAINING:
1683 state = ERROR;
1684 goto _end_loop;
1685 case SNDRV_PCM_STATE_SUSPENDED:
1686 state = SUSPENDED;
1687 goto _end_loop;
1688 case SNDRV_PCM_STATE_SETUP:
1689 state = DROPPED;
1690 goto _end_loop;
1691 default:
1692 break;
1694 avail = snd_pcm_playback_avail(runtime);
1695 if (avail >= runtime->control->avail_min) {
1696 state = READY;
1697 break;
1700 _end_loop:
1701 remove_wait_queue(&runtime->sleep, &wait);
1703 switch (state) {
1704 case ERROR:
1705 err = -EPIPE;
1706 goto _end_unlock;
1707 case SUSPENDED:
1708 err = -ESTRPIPE;
1709 goto _end_unlock;
1710 case SIGNALED:
1711 err = -ERESTARTSYS;
1712 goto _end_unlock;
1713 case EXPIRED:
1714 snd_printd("playback write error (DMA or IRQ trouble?)\n");
1715 err = -EIO;
1716 goto _end_unlock;
1717 case DROPPED:
1718 err = -EBADFD;
1719 goto _end_unlock;
1720 default:
1721 break;
1724 if (avail > runtime->xfer_align)
1725 avail -= avail % runtime->xfer_align;
1726 frames = size > avail ? avail : size;
1727 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1728 if (frames > cont)
1729 frames = cont;
1730 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1731 appl_ptr = runtime->control->appl_ptr;
1732 appl_ofs = appl_ptr % runtime->buffer_size;
1733 snd_pcm_stream_unlock_irq(substream);
1734 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1735 goto _end;
1736 snd_pcm_stream_lock_irq(substream);
1737 switch (runtime->status->state) {
1738 case SNDRV_PCM_STATE_XRUN:
1739 err = -EPIPE;
1740 goto _end_unlock;
1741 case SNDRV_PCM_STATE_SUSPENDED:
1742 err = -ESTRPIPE;
1743 goto _end_unlock;
1744 default:
1745 break;
1747 appl_ptr += frames;
1748 if (appl_ptr >= runtime->boundary)
1749 appl_ptr -= runtime->boundary;
1750 runtime->control->appl_ptr = appl_ptr;
1751 if (substream->ops->ack)
1752 substream->ops->ack(substream);
1754 offset += frames;
1755 size -= frames;
1756 xfer += frames;
1757 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1758 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1759 err = snd_pcm_start(substream);
1760 if (err < 0)
1761 goto _end_unlock;
1763 if (runtime->sleep_min &&
1764 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1765 snd_pcm_tick_prepare(substream);
1767 _end_unlock:
1768 snd_pcm_stream_unlock_irq(substream);
1769 _end:
1770 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1773 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1775 struct snd_pcm_runtime *runtime;
1776 int nonblock;
1778 snd_assert(substream != NULL, return -ENXIO);
1779 runtime = substream->runtime;
1780 snd_assert(runtime != NULL, return -ENXIO);
1781 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1782 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1783 return -EBADFD;
1785 nonblock = !!(substream->f_flags & O_NONBLOCK);
1787 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1788 runtime->channels > 1)
1789 return -EINVAL;
1790 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1791 snd_pcm_lib_write_transfer);
1794 EXPORT_SYMBOL(snd_pcm_lib_write);
1796 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1797 unsigned int hwoff,
1798 unsigned long data, unsigned int off,
1799 snd_pcm_uframes_t frames)
1801 struct snd_pcm_runtime *runtime = substream->runtime;
1802 int err;
1803 void __user **bufs = (void __user **)data;
1804 int channels = runtime->channels;
1805 int c;
1806 if (substream->ops->copy) {
1807 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1808 for (c = 0; c < channels; ++c, ++bufs) {
1809 if (*bufs == NULL) {
1810 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1811 return err;
1812 } else {
1813 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1814 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1815 return err;
1818 } else {
1819 /* default transfer behaviour */
1820 size_t dma_csize = runtime->dma_bytes / channels;
1821 snd_assert(runtime->dma_area, return -EFAULT);
1822 for (c = 0; c < channels; ++c, ++bufs) {
1823 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1824 if (*bufs == NULL) {
1825 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1826 } else {
1827 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1828 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1829 return -EFAULT;
1833 return 0;
1836 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1837 void __user **bufs,
1838 snd_pcm_uframes_t frames)
1840 struct snd_pcm_runtime *runtime;
1841 int nonblock;
1843 snd_assert(substream != NULL, return -ENXIO);
1844 runtime = substream->runtime;
1845 snd_assert(runtime != NULL, return -ENXIO);
1846 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1848 return -EBADFD;
1850 nonblock = !!(substream->f_flags & O_NONBLOCK);
1852 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1853 return -EINVAL;
1854 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1855 nonblock, snd_pcm_lib_writev_transfer);
1858 EXPORT_SYMBOL(snd_pcm_lib_writev);
1860 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1861 unsigned int hwoff,
1862 unsigned long data, unsigned int off,
1863 snd_pcm_uframes_t frames)
1865 struct snd_pcm_runtime *runtime = substream->runtime;
1866 int err;
1867 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1868 if (substream->ops->copy) {
1869 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1870 return err;
1871 } else {
1872 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1873 snd_assert(runtime->dma_area, return -EFAULT);
1874 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1875 return -EFAULT;
1877 return 0;
1880 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1881 unsigned long data,
1882 snd_pcm_uframes_t size,
1883 int nonblock,
1884 transfer_f transfer)
1886 struct snd_pcm_runtime *runtime = substream->runtime;
1887 snd_pcm_uframes_t xfer = 0;
1888 snd_pcm_uframes_t offset = 0;
1889 int err = 0;
1891 if (size == 0)
1892 return 0;
1893 if (size > runtime->xfer_align)
1894 size -= size % runtime->xfer_align;
1896 snd_pcm_stream_lock_irq(substream);
1897 switch (runtime->status->state) {
1898 case SNDRV_PCM_STATE_PREPARED:
1899 if (size >= runtime->start_threshold) {
1900 err = snd_pcm_start(substream);
1901 if (err < 0)
1902 goto _end_unlock;
1904 break;
1905 case SNDRV_PCM_STATE_DRAINING:
1906 case SNDRV_PCM_STATE_RUNNING:
1907 case SNDRV_PCM_STATE_PAUSED:
1908 break;
1909 case SNDRV_PCM_STATE_XRUN:
1910 err = -EPIPE;
1911 goto _end_unlock;
1912 case SNDRV_PCM_STATE_SUSPENDED:
1913 err = -ESTRPIPE;
1914 goto _end_unlock;
1915 default:
1916 err = -EBADFD;
1917 goto _end_unlock;
1920 while (size > 0) {
1921 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1922 snd_pcm_uframes_t avail;
1923 snd_pcm_uframes_t cont;
1924 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1925 snd_pcm_update_hw_ptr(substream);
1926 __draining:
1927 avail = snd_pcm_capture_avail(runtime);
1928 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1929 if (avail < runtime->xfer_align) {
1930 err = -EPIPE;
1931 goto _end_unlock;
1933 } else if ((avail < runtime->control->avail_min && size > avail) ||
1934 (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
1935 wait_queue_t wait;
1936 enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
1937 long tout;
1939 if (nonblock) {
1940 err = -EAGAIN;
1941 goto _end_unlock;
1944 init_waitqueue_entry(&wait, current);
1945 add_wait_queue(&runtime->sleep, &wait);
1946 while (1) {
1947 if (signal_pending(current)) {
1948 state = SIGNALED;
1949 break;
1951 set_current_state(TASK_INTERRUPTIBLE);
1952 snd_pcm_stream_unlock_irq(substream);
1953 tout = schedule_timeout(10 * HZ);
1954 snd_pcm_stream_lock_irq(substream);
1955 if (tout == 0) {
1956 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
1957 runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
1958 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1959 break;
1962 switch (runtime->status->state) {
1963 case SNDRV_PCM_STATE_XRUN:
1964 state = ERROR;
1965 goto _end_loop;
1966 case SNDRV_PCM_STATE_SUSPENDED:
1967 state = SUSPENDED;
1968 goto _end_loop;
1969 case SNDRV_PCM_STATE_DRAINING:
1970 goto __draining;
1971 case SNDRV_PCM_STATE_SETUP:
1972 state = DROPPED;
1973 goto _end_loop;
1974 default:
1975 break;
1977 avail = snd_pcm_capture_avail(runtime);
1978 if (avail >= runtime->control->avail_min) {
1979 state = READY;
1980 break;
1983 _end_loop:
1984 remove_wait_queue(&runtime->sleep, &wait);
1986 switch (state) {
1987 case ERROR:
1988 err = -EPIPE;
1989 goto _end_unlock;
1990 case SUSPENDED:
1991 err = -ESTRPIPE;
1992 goto _end_unlock;
1993 case SIGNALED:
1994 err = -ERESTARTSYS;
1995 goto _end_unlock;
1996 case EXPIRED:
1997 snd_printd("capture read error (DMA or IRQ trouble?)\n");
1998 err = -EIO;
1999 goto _end_unlock;
2000 case DROPPED:
2001 err = -EBADFD;
2002 goto _end_unlock;
2003 default:
2004 break;
2007 if (avail > runtime->xfer_align)
2008 avail -= avail % runtime->xfer_align;
2009 frames = size > avail ? avail : size;
2010 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2011 if (frames > cont)
2012 frames = cont;
2013 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2014 appl_ptr = runtime->control->appl_ptr;
2015 appl_ofs = appl_ptr % runtime->buffer_size;
2016 snd_pcm_stream_unlock_irq(substream);
2017 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2018 goto _end;
2019 snd_pcm_stream_lock_irq(substream);
2020 switch (runtime->status->state) {
2021 case SNDRV_PCM_STATE_XRUN:
2022 err = -EPIPE;
2023 goto _end_unlock;
2024 case SNDRV_PCM_STATE_SUSPENDED:
2025 err = -ESTRPIPE;
2026 goto _end_unlock;
2027 default:
2028 break;
2030 appl_ptr += frames;
2031 if (appl_ptr >= runtime->boundary)
2032 appl_ptr -= runtime->boundary;
2033 runtime->control->appl_ptr = appl_ptr;
2034 if (substream->ops->ack)
2035 substream->ops->ack(substream);
2037 offset += frames;
2038 size -= frames;
2039 xfer += frames;
2040 if (runtime->sleep_min &&
2041 runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2042 snd_pcm_tick_prepare(substream);
2044 _end_unlock:
2045 snd_pcm_stream_unlock_irq(substream);
2046 _end:
2047 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2050 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2052 struct snd_pcm_runtime *runtime;
2053 int nonblock;
2055 snd_assert(substream != NULL, return -ENXIO);
2056 runtime = substream->runtime;
2057 snd_assert(runtime != NULL, return -ENXIO);
2058 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2059 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2060 return -EBADFD;
2062 nonblock = !!(substream->f_flags & O_NONBLOCK);
2063 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2064 return -EINVAL;
2065 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2068 EXPORT_SYMBOL(snd_pcm_lib_read);
2070 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2071 unsigned int hwoff,
2072 unsigned long data, unsigned int off,
2073 snd_pcm_uframes_t frames)
2075 struct snd_pcm_runtime *runtime = substream->runtime;
2076 int err;
2077 void __user **bufs = (void __user **)data;
2078 int channels = runtime->channels;
2079 int c;
2080 if (substream->ops->copy) {
2081 for (c = 0; c < channels; ++c, ++bufs) {
2082 char __user *buf;
2083 if (*bufs == NULL)
2084 continue;
2085 buf = *bufs + samples_to_bytes(runtime, off);
2086 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2087 return err;
2089 } else {
2090 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2091 snd_assert(runtime->dma_area, return -EFAULT);
2092 for (c = 0; c < channels; ++c, ++bufs) {
2093 char *hwbuf;
2094 char __user *buf;
2095 if (*bufs == NULL)
2096 continue;
2098 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2099 buf = *bufs + samples_to_bytes(runtime, off);
2100 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2101 return -EFAULT;
2104 return 0;
2107 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2108 void __user **bufs,
2109 snd_pcm_uframes_t frames)
2111 struct snd_pcm_runtime *runtime;
2112 int nonblock;
2114 snd_assert(substream != NULL, return -ENXIO);
2115 runtime = substream->runtime;
2116 snd_assert(runtime != NULL, return -ENXIO);
2117 snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2118 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2119 return -EBADFD;
2121 nonblock = !!(substream->f_flags & O_NONBLOCK);
2122 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2123 return -EINVAL;
2124 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2127 EXPORT_SYMBOL(snd_pcm_lib_readv);