Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[linux-2.6] / sound / core / pcm_lib.c
1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *                   Abramo Bagnara <abramo@alsa-project.org>
5  *
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/info.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/timer.h>
31
32 /*
33  * fill ring buffer with silence
34  * runtime->silence_start: starting pointer to silence area
35  * runtime->silence_filled: size filled with silence
36  * runtime->silence_threshold: threshold from application
37  * runtime->silence_size: maximal size from application
38  *
39  * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
40  */
41 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
42 {
43         struct snd_pcm_runtime *runtime = substream->runtime;
44         snd_pcm_uframes_t frames, ofs, transfer;
45
46         if (runtime->silence_size < runtime->boundary) {
47                 snd_pcm_sframes_t noise_dist, n;
48                 if (runtime->silence_start != runtime->control->appl_ptr) {
49                         n = runtime->control->appl_ptr - runtime->silence_start;
50                         if (n < 0)
51                                 n += runtime->boundary;
52                         if ((snd_pcm_uframes_t)n < runtime->silence_filled)
53                                 runtime->silence_filled -= n;
54                         else
55                                 runtime->silence_filled = 0;
56                         runtime->silence_start = runtime->control->appl_ptr;
57                 }
58                 if (runtime->silence_filled >= runtime->buffer_size)
59                         return;
60                 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
61                 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
62                         return;
63                 frames = runtime->silence_threshold - noise_dist;
64                 if (frames > runtime->silence_size)
65                         frames = runtime->silence_size;
66         } else {
67                 if (new_hw_ptr == ULONG_MAX) {  /* initialization */
68                         snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
69                         runtime->silence_filled = avail > 0 ? avail : 0;
70                         runtime->silence_start = (runtime->status->hw_ptr +
71                                                   runtime->silence_filled) %
72                                                  runtime->boundary;
73                 } else {
74                         ofs = runtime->status->hw_ptr;
75                         frames = new_hw_ptr - ofs;
76                         if ((snd_pcm_sframes_t)frames < 0)
77                                 frames += runtime->boundary;
78                         runtime->silence_filled -= frames;
79                         if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
80                                 runtime->silence_filled = 0;
81                                 runtime->silence_start = new_hw_ptr;
82                         } else {
83                                 runtime->silence_start = ofs;
84                         }
85                 }
86                 frames = runtime->buffer_size - runtime->silence_filled;
87         }
88         snd_assert(frames <= runtime->buffer_size, return);
89         if (frames == 0)
90                 return;
91         ofs = runtime->silence_start % runtime->buffer_size;
92         while (frames > 0) {
93                 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
94                 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
95                     runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
96                         if (substream->ops->silence) {
97                                 int err;
98                                 err = substream->ops->silence(substream, -1, ofs, transfer);
99                                 snd_assert(err >= 0, );
100                         } else {
101                                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
102                                 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
103                         }
104                 } else {
105                         unsigned int c;
106                         unsigned int channels = runtime->channels;
107                         if (substream->ops->silence) {
108                                 for (c = 0; c < channels; ++c) {
109                                         int err;
110                                         err = substream->ops->silence(substream, c, ofs, transfer);
111                                         snd_assert(err >= 0, );
112                                 }
113                         } else {
114                                 size_t dma_csize = runtime->dma_bytes / channels;
115                                 for (c = 0; c < channels; ++c) {
116                                         char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
117                                         snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
118                                 }
119                         }
120                 }
121                 runtime->silence_filled += transfer;
122                 frames -= transfer;
123                 ofs = 0;
124         }
125 }
126
127 static void xrun(struct snd_pcm_substream *substream)
128 {
129         snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
130 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
131         if (substream->pstr->xrun_debug) {
132                 snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
133                            substream->pcm->card->number,
134                            substream->pcm->device,
135                            substream->stream ? 'c' : 'p');
136                 if (substream->pstr->xrun_debug > 1)
137                         dump_stack();
138         }
139 #endif
140 }
141
142 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
143                                                           struct snd_pcm_runtime *runtime)
144 {
145         snd_pcm_uframes_t pos;
146
147         if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
148                 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
149         pos = substream->ops->pointer(substream);
150         if (pos == SNDRV_PCM_POS_XRUN)
151                 return pos; /* XRUN */
152 #ifdef CONFIG_SND_DEBUG
153         if (pos >= runtime->buffer_size) {
154                 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);
155         }
156 #endif
157         pos -= pos % runtime->min_align;
158         return pos;
159 }
160
161 static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
162                                              struct snd_pcm_runtime *runtime)
163 {
164         snd_pcm_uframes_t avail;
165
166         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
167                 avail = snd_pcm_playback_avail(runtime);
168         else
169                 avail = snd_pcm_capture_avail(runtime);
170         if (avail > runtime->avail_max)
171                 runtime->avail_max = avail;
172         if (avail >= runtime->stop_threshold) {
173                 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
174                         snd_pcm_drain_done(substream);
175                 else
176                         xrun(substream);
177                 return -EPIPE;
178         }
179         if (avail >= runtime->control->avail_min)
180                 wake_up(&runtime->sleep);
181         return 0;
182 }
183
184 static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
185 {
186         struct snd_pcm_runtime *runtime = substream->runtime;
187         snd_pcm_uframes_t pos;
188         snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
189         snd_pcm_sframes_t delta;
190
191         pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
192         if (pos == SNDRV_PCM_POS_XRUN) {
193                 xrun(substream);
194                 return -EPIPE;
195         }
196         if (runtime->period_size == runtime->buffer_size)
197                 goto __next_buf;
198         new_hw_ptr = runtime->hw_ptr_base + pos;
199         hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
200
201         delta = hw_ptr_interrupt - new_hw_ptr;
202         if (delta > 0) {
203                 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
204 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
205                         if (runtime->periods > 1 && substream->pstr->xrun_debug) {
206                                 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);
207                                 if (substream->pstr->xrun_debug > 1)
208                                         dump_stack();
209                         }
210 #endif
211                         return 0;
212                 }
213               __next_buf:
214                 runtime->hw_ptr_base += runtime->buffer_size;
215                 if (runtime->hw_ptr_base == runtime->boundary)
216                         runtime->hw_ptr_base = 0;
217                 new_hw_ptr = runtime->hw_ptr_base + pos;
218         }
219
220         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
221             runtime->silence_size > 0)
222                 snd_pcm_playback_silence(substream, new_hw_ptr);
223
224         runtime->status->hw_ptr = new_hw_ptr;
225         runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
226
227         return snd_pcm_update_hw_ptr_post(substream, runtime);
228 }
229
230 /* CAUTION: call it with irq disabled */
231 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
232 {
233         struct snd_pcm_runtime *runtime = substream->runtime;
234         snd_pcm_uframes_t pos;
235         snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
236         snd_pcm_sframes_t delta;
237
238         old_hw_ptr = runtime->status->hw_ptr;
239         pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
240         if (pos == SNDRV_PCM_POS_XRUN) {
241                 xrun(substream);
242                 return -EPIPE;
243         }
244         new_hw_ptr = runtime->hw_ptr_base + pos;
245
246         delta = old_hw_ptr - new_hw_ptr;
247         if (delta > 0) {
248                 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
249 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
250                         if (runtime->periods > 2 && substream->pstr->xrun_debug) {
251                                 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);
252                                 if (substream->pstr->xrun_debug > 1)
253                                         dump_stack();
254                         }
255 #endif
256                         return 0;
257                 }
258                 runtime->hw_ptr_base += runtime->buffer_size;
259                 if (runtime->hw_ptr_base == runtime->boundary)
260                         runtime->hw_ptr_base = 0;
261                 new_hw_ptr = runtime->hw_ptr_base + pos;
262         }
263         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
264             runtime->silence_size > 0)
265                 snd_pcm_playback_silence(substream, new_hw_ptr);
266
267         runtime->status->hw_ptr = new_hw_ptr;
268
269         return snd_pcm_update_hw_ptr_post(substream, runtime);
270 }
271
272 /**
273  * snd_pcm_set_ops - set the PCM operators
274  * @pcm: the pcm instance
275  * @direction: stream direction, SNDRV_PCM_STREAM_XXX
276  * @ops: the operator table
277  *
278  * Sets the given PCM operators to the pcm instance.
279  */
280 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
281 {
282         struct snd_pcm_str *stream = &pcm->streams[direction];
283         struct snd_pcm_substream *substream;
284         
285         for (substream = stream->substream; substream != NULL; substream = substream->next)
286                 substream->ops = ops;
287 }
288
289 EXPORT_SYMBOL(snd_pcm_set_ops);
290
291 /**
292  * snd_pcm_sync - set the PCM sync id
293  * @substream: the pcm substream
294  *
295  * Sets the PCM sync identifier for the card.
296  */
297 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
298 {
299         struct snd_pcm_runtime *runtime = substream->runtime;
300         
301         runtime->sync.id32[0] = substream->pcm->card->number;
302         runtime->sync.id32[1] = -1;
303         runtime->sync.id32[2] = -1;
304         runtime->sync.id32[3] = -1;
305 }
306
307 EXPORT_SYMBOL(snd_pcm_set_sync);
308
309 /*
310  *  Standard ioctl routine
311  */
312
313 static inline unsigned int div32(unsigned int a, unsigned int b, 
314                                  unsigned int *r)
315 {
316         if (b == 0) {
317                 *r = 0;
318                 return UINT_MAX;
319         }
320         *r = a % b;
321         return a / b;
322 }
323
324 static inline unsigned int div_down(unsigned int a, unsigned int b)
325 {
326         if (b == 0)
327                 return UINT_MAX;
328         return a / b;
329 }
330
331 static inline unsigned int div_up(unsigned int a, unsigned int b)
332 {
333         unsigned int r;
334         unsigned int q;
335         if (b == 0)
336                 return UINT_MAX;
337         q = div32(a, b, &r);
338         if (r)
339                 ++q;
340         return q;
341 }
342
343 static inline unsigned int mul(unsigned int a, unsigned int b)
344 {
345         if (a == 0)
346                 return 0;
347         if (div_down(UINT_MAX, a) < b)
348                 return UINT_MAX;
349         return a * b;
350 }
351
352 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
353                                     unsigned int c, unsigned int *r)
354 {
355         u_int64_t n = (u_int64_t) a * b;
356         if (c == 0) {
357                 snd_assert(n > 0, );
358                 *r = 0;
359                 return UINT_MAX;
360         }
361         div64_32(&n, c, r);
362         if (n >= UINT_MAX) {
363                 *r = 0;
364                 return UINT_MAX;
365         }
366         return n;
367 }
368
369 /**
370  * snd_interval_refine - refine the interval value of configurator
371  * @i: the interval value to refine
372  * @v: the interval value to refer to
373  *
374  * Refines the interval value with the reference value.
375  * The interval is changed to the range satisfying both intervals.
376  * The interval status (min, max, integer, etc.) are evaluated.
377  *
378  * Returns non-zero if the value is changed, zero if not changed.
379  */
380 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
381 {
382         int changed = 0;
383         snd_assert(!snd_interval_empty(i), return -EINVAL);
384         if (i->min < v->min) {
385                 i->min = v->min;
386                 i->openmin = v->openmin;
387                 changed = 1;
388         } else if (i->min == v->min && !i->openmin && v->openmin) {
389                 i->openmin = 1;
390                 changed = 1;
391         }
392         if (i->max > v->max) {
393                 i->max = v->max;
394                 i->openmax = v->openmax;
395                 changed = 1;
396         } else if (i->max == v->max && !i->openmax && v->openmax) {
397                 i->openmax = 1;
398                 changed = 1;
399         }
400         if (!i->integer && v->integer) {
401                 i->integer = 1;
402                 changed = 1;
403         }
404         if (i->integer) {
405                 if (i->openmin) {
406                         i->min++;
407                         i->openmin = 0;
408                 }
409                 if (i->openmax) {
410                         i->max--;
411                         i->openmax = 0;
412                 }
413         } else if (!i->openmin && !i->openmax && i->min == i->max)
414                 i->integer = 1;
415         if (snd_interval_checkempty(i)) {
416                 snd_interval_none(i);
417                 return -EINVAL;
418         }
419         return changed;
420 }
421
422 EXPORT_SYMBOL(snd_interval_refine);
423
424 static int snd_interval_refine_first(struct snd_interval *i)
425 {
426         snd_assert(!snd_interval_empty(i), return -EINVAL);
427         if (snd_interval_single(i))
428                 return 0;
429         i->max = i->min;
430         i->openmax = i->openmin;
431         if (i->openmax)
432                 i->max++;
433         return 1;
434 }
435
436 static int snd_interval_refine_last(struct snd_interval *i)
437 {
438         snd_assert(!snd_interval_empty(i), return -EINVAL);
439         if (snd_interval_single(i))
440                 return 0;
441         i->min = i->max;
442         i->openmin = i->openmax;
443         if (i->openmin)
444                 i->min--;
445         return 1;
446 }
447
448 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
449 {
450         if (a->empty || b->empty) {
451                 snd_interval_none(c);
452                 return;
453         }
454         c->empty = 0;
455         c->min = mul(a->min, b->min);
456         c->openmin = (a->openmin || b->openmin);
457         c->max = mul(a->max,  b->max);
458         c->openmax = (a->openmax || b->openmax);
459         c->integer = (a->integer && b->integer);
460 }
461
462 /**
463  * snd_interval_div - refine the interval value with division
464  * @a: dividend
465  * @b: divisor
466  * @c: quotient
467  *
468  * c = a / b
469  *
470  * Returns non-zero if the value is changed, zero if not changed.
471  */
472 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
473 {
474         unsigned int r;
475         if (a->empty || b->empty) {
476                 snd_interval_none(c);
477                 return;
478         }
479         c->empty = 0;
480         c->min = div32(a->min, b->max, &r);
481         c->openmin = (r || a->openmin || b->openmax);
482         if (b->min > 0) {
483                 c->max = div32(a->max, b->min, &r);
484                 if (r) {
485                         c->max++;
486                         c->openmax = 1;
487                 } else
488                         c->openmax = (a->openmax || b->openmin);
489         } else {
490                 c->max = UINT_MAX;
491                 c->openmax = 0;
492         }
493         c->integer = 0;
494 }
495
496 /**
497  * snd_interval_muldivk - refine the interval value
498  * @a: dividend 1
499  * @b: dividend 2
500  * @k: divisor (as integer)
501  * @c: result
502   *
503  * c = a * b / k
504  *
505  * Returns non-zero if the value is changed, zero if not changed.
506  */
507 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
508                       unsigned int k, struct snd_interval *c)
509 {
510         unsigned int r;
511         if (a->empty || b->empty) {
512                 snd_interval_none(c);
513                 return;
514         }
515         c->empty = 0;
516         c->min = muldiv32(a->min, b->min, k, &r);
517         c->openmin = (r || a->openmin || b->openmin);
518         c->max = muldiv32(a->max, b->max, k, &r);
519         if (r) {
520                 c->max++;
521                 c->openmax = 1;
522         } else
523                 c->openmax = (a->openmax || b->openmax);
524         c->integer = 0;
525 }
526
527 /**
528  * snd_interval_mulkdiv - refine the interval value
529  * @a: dividend 1
530  * @k: dividend 2 (as integer)
531  * @b: divisor
532  * @c: result
533  *
534  * c = a * k / b
535  *
536  * Returns non-zero if the value is changed, zero if not changed.
537  */
538 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
539                       const struct snd_interval *b, struct snd_interval *c)
540 {
541         unsigned int r;
542         if (a->empty || b->empty) {
543                 snd_interval_none(c);
544                 return;
545         }
546         c->empty = 0;
547         c->min = muldiv32(a->min, k, b->max, &r);
548         c->openmin = (r || a->openmin || b->openmax);
549         if (b->min > 0) {
550                 c->max = muldiv32(a->max, k, b->min, &r);
551                 if (r) {
552                         c->max++;
553                         c->openmax = 1;
554                 } else
555                         c->openmax = (a->openmax || b->openmin);
556         } else {
557                 c->max = UINT_MAX;
558                 c->openmax = 0;
559         }
560         c->integer = 0;
561 }
562
563 /* ---- */
564
565
566 /**
567  * snd_interval_ratnum - refine the interval value
568  * @i: interval to refine
569  * @rats_count: number of ratnum_t 
570  * @rats: ratnum_t array
571  * @nump: pointer to store the resultant numerator
572  * @denp: pointer to store the resultant denominator
573  *
574  * Returns non-zero if the value is changed, zero if not changed.
575  */
576 int snd_interval_ratnum(struct snd_interval *i,
577                         unsigned int rats_count, struct snd_ratnum *rats,
578                         unsigned int *nump, unsigned int *denp)
579 {
580         unsigned int best_num, best_diff, best_den;
581         unsigned int k;
582         struct snd_interval t;
583         int err;
584
585         best_num = best_den = best_diff = 0;
586         for (k = 0; k < rats_count; ++k) {
587                 unsigned int num = rats[k].num;
588                 unsigned int den;
589                 unsigned int q = i->min;
590                 int diff;
591                 if (q == 0)
592                         q = 1;
593                 den = div_down(num, q);
594                 if (den < rats[k].den_min)
595                         continue;
596                 if (den > rats[k].den_max)
597                         den = rats[k].den_max;
598                 else {
599                         unsigned int r;
600                         r = (den - rats[k].den_min) % rats[k].den_step;
601                         if (r != 0)
602                                 den -= r;
603                 }
604                 diff = num - q * den;
605                 if (best_num == 0 ||
606                     diff * best_den < best_diff * den) {
607                         best_diff = diff;
608                         best_den = den;
609                         best_num = num;
610                 }
611         }
612         if (best_den == 0) {
613                 i->empty = 1;
614                 return -EINVAL;
615         }
616         t.min = div_down(best_num, best_den);
617         t.openmin = !!(best_num % best_den);
618         
619         best_num = best_den = best_diff = 0;
620         for (k = 0; k < rats_count; ++k) {
621                 unsigned int num = rats[k].num;
622                 unsigned int den;
623                 unsigned int q = i->max;
624                 int diff;
625                 if (q == 0) {
626                         i->empty = 1;
627                         return -EINVAL;
628                 }
629                 den = div_up(num, q);
630                 if (den > rats[k].den_max)
631                         continue;
632                 if (den < rats[k].den_min)
633                         den = rats[k].den_min;
634                 else {
635                         unsigned int r;
636                         r = (den - rats[k].den_min) % rats[k].den_step;
637                         if (r != 0)
638                                 den += rats[k].den_step - r;
639                 }
640                 diff = q * den - num;
641                 if (best_num == 0 ||
642                     diff * best_den < best_diff * den) {
643                         best_diff = diff;
644                         best_den = den;
645                         best_num = num;
646                 }
647         }
648         if (best_den == 0) {
649                 i->empty = 1;
650                 return -EINVAL;
651         }
652         t.max = div_up(best_num, best_den);
653         t.openmax = !!(best_num % best_den);
654         t.integer = 0;
655         err = snd_interval_refine(i, &t);
656         if (err < 0)
657                 return err;
658
659         if (snd_interval_single(i)) {
660                 if (nump)
661                         *nump = best_num;
662                 if (denp)
663                         *denp = best_den;
664         }
665         return err;
666 }
667
668 EXPORT_SYMBOL(snd_interval_ratnum);
669
670 /**
671  * snd_interval_ratden - refine the interval value
672  * @i: interval to refine
673  * @rats_count: number of struct ratden
674  * @rats: struct ratden array
675  * @nump: pointer to store the resultant numerator
676  * @denp: pointer to store the resultant denominator
677  *
678  * Returns non-zero if the value is changed, zero if not changed.
679  */
680 static int snd_interval_ratden(struct snd_interval *i,
681                                unsigned int rats_count, struct snd_ratden *rats,
682                                unsigned int *nump, unsigned int *denp)
683 {
684         unsigned int best_num, best_diff, best_den;
685         unsigned int k;
686         struct snd_interval t;
687         int err;
688
689         best_num = best_den = best_diff = 0;
690         for (k = 0; k < rats_count; ++k) {
691                 unsigned int num;
692                 unsigned int den = rats[k].den;
693                 unsigned int q = i->min;
694                 int diff;
695                 num = mul(q, den);
696                 if (num > rats[k].num_max)
697                         continue;
698                 if (num < rats[k].num_min)
699                         num = rats[k].num_max;
700                 else {
701                         unsigned int r;
702                         r = (num - rats[k].num_min) % rats[k].num_step;
703                         if (r != 0)
704                                 num += rats[k].num_step - r;
705                 }
706                 diff = num - q * den;
707                 if (best_num == 0 ||
708                     diff * best_den < best_diff * den) {
709                         best_diff = diff;
710                         best_den = den;
711                         best_num = num;
712                 }
713         }
714         if (best_den == 0) {
715                 i->empty = 1;
716                 return -EINVAL;
717         }
718         t.min = div_down(best_num, best_den);
719         t.openmin = !!(best_num % best_den);
720         
721         best_num = best_den = best_diff = 0;
722         for (k = 0; k < rats_count; ++k) {
723                 unsigned int num;
724                 unsigned int den = rats[k].den;
725                 unsigned int q = i->max;
726                 int diff;
727                 num = mul(q, den);
728                 if (num < rats[k].num_min)
729                         continue;
730                 if (num > rats[k].num_max)
731                         num = rats[k].num_max;
732                 else {
733                         unsigned int r;
734                         r = (num - rats[k].num_min) % rats[k].num_step;
735                         if (r != 0)
736                                 num -= r;
737                 }
738                 diff = q * den - num;
739                 if (best_num == 0 ||
740                     diff * best_den < best_diff * den) {
741                         best_diff = diff;
742                         best_den = den;
743                         best_num = num;
744                 }
745         }
746         if (best_den == 0) {
747                 i->empty = 1;
748                 return -EINVAL;
749         }
750         t.max = div_up(best_num, best_den);
751         t.openmax = !!(best_num % best_den);
752         t.integer = 0;
753         err = snd_interval_refine(i, &t);
754         if (err < 0)
755                 return err;
756
757         if (snd_interval_single(i)) {
758                 if (nump)
759                         *nump = best_num;
760                 if (denp)
761                         *denp = best_den;
762         }
763         return err;
764 }
765
766 /**
767  * snd_interval_list - refine the interval value from the list
768  * @i: the interval value to refine
769  * @count: the number of elements in the list
770  * @list: the value list
771  * @mask: the bit-mask to evaluate
772  *
773  * Refines the interval value from the list.
774  * When mask is non-zero, only the elements corresponding to bit 1 are
775  * evaluated.
776  *
777  * Returns non-zero if the value is changed, zero if not changed.
778  */
779 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
780 {
781         unsigned int k;
782         int changed = 0;
783
784         if (!count) {
785                 i->empty = 1;
786                 return -EINVAL;
787         }
788         for (k = 0; k < count; k++) {
789                 if (mask && !(mask & (1 << k)))
790                         continue;
791                 if (i->min == list[k] && !i->openmin)
792                         goto _l1;
793                 if (i->min < list[k]) {
794                         i->min = list[k];
795                         i->openmin = 0;
796                         changed = 1;
797                         goto _l1;
798                 }
799         }
800         i->empty = 1;
801         return -EINVAL;
802  _l1:
803         for (k = count; k-- > 0;) {
804                 if (mask && !(mask & (1 << k)))
805                         continue;
806                 if (i->max == list[k] && !i->openmax)
807                         goto _l2;
808                 if (i->max > list[k]) {
809                         i->max = list[k];
810                         i->openmax = 0;
811                         changed = 1;
812                         goto _l2;
813                 }
814         }
815         i->empty = 1;
816         return -EINVAL;
817  _l2:
818         if (snd_interval_checkempty(i)) {
819                 i->empty = 1;
820                 return -EINVAL;
821         }
822         return changed;
823 }
824
825 EXPORT_SYMBOL(snd_interval_list);
826
827 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
828 {
829         unsigned int n;
830         int changed = 0;
831         n = (i->min - min) % step;
832         if (n != 0 || i->openmin) {
833                 i->min += step - n;
834                 changed = 1;
835         }
836         n = (i->max - min) % step;
837         if (n != 0 || i->openmax) {
838                 i->max -= n;
839                 changed = 1;
840         }
841         if (snd_interval_checkempty(i)) {
842                 i->empty = 1;
843                 return -EINVAL;
844         }
845         return changed;
846 }
847
848 /* Info constraints helpers */
849
850 /**
851  * snd_pcm_hw_rule_add - add the hw-constraint rule
852  * @runtime: the pcm runtime instance
853  * @cond: condition bits
854  * @var: the variable to evaluate
855  * @func: the evaluation function
856  * @private: the private data pointer passed to function
857  * @dep: the dependent variables
858  *
859  * Returns zero if successful, or a negative error code on failure.
860  */
861 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
862                         int var,
863                         snd_pcm_hw_rule_func_t func, void *private,
864                         int dep, ...)
865 {
866         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
867         struct snd_pcm_hw_rule *c;
868         unsigned int k;
869         va_list args;
870         va_start(args, dep);
871         if (constrs->rules_num >= constrs->rules_all) {
872                 struct snd_pcm_hw_rule *new;
873                 unsigned int new_rules = constrs->rules_all + 16;
874                 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
875                 if (!new)
876                         return -ENOMEM;
877                 if (constrs->rules) {
878                         memcpy(new, constrs->rules,
879                                constrs->rules_num * sizeof(*c));
880                         kfree(constrs->rules);
881                 }
882                 constrs->rules = new;
883                 constrs->rules_all = new_rules;
884         }
885         c = &constrs->rules[constrs->rules_num];
886         c->cond = cond;
887         c->func = func;
888         c->var = var;
889         c->private = private;
890         k = 0;
891         while (1) {
892                 snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
893                 c->deps[k++] = dep;
894                 if (dep < 0)
895                         break;
896                 dep = va_arg(args, int);
897         }
898         constrs->rules_num++;
899         va_end(args);
900         return 0;
901 }                                   
902
903 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
904
905 /**
906  * snd_pcm_hw_constraint_mask
907  * @runtime: PCM runtime instance
908  * @var: hw_params variable to apply the mask
909  * @mask: the bitmap mask
910  *
911  * Apply the constraint of the given bitmap mask to a mask parameter.
912  */
913 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
914                                u_int32_t mask)
915 {
916         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
917         struct snd_mask *maskp = constrs_mask(constrs, var);
918         *maskp->bits &= mask;
919         memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
920         if (*maskp->bits == 0)
921                 return -EINVAL;
922         return 0;
923 }
924
925 /**
926  * snd_pcm_hw_constraint_mask64
927  * @runtime: PCM runtime instance
928  * @var: hw_params variable to apply the mask
929  * @mask: the 64bit bitmap mask
930  *
931  * Apply the constraint of the given bitmap mask to a mask parameter.
932  */
933 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
934                                  u_int64_t mask)
935 {
936         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
937         struct snd_mask *maskp = constrs_mask(constrs, var);
938         maskp->bits[0] &= (u_int32_t)mask;
939         maskp->bits[1] &= (u_int32_t)(mask >> 32);
940         memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
941         if (! maskp->bits[0] && ! maskp->bits[1])
942                 return -EINVAL;
943         return 0;
944 }
945
946 /**
947  * snd_pcm_hw_constraint_integer
948  * @runtime: PCM runtime instance
949  * @var: hw_params variable to apply the integer constraint
950  *
951  * Apply the constraint of integer to an interval parameter.
952  */
953 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
954 {
955         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
956         return snd_interval_setinteger(constrs_interval(constrs, var));
957 }
958
959 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
960
961 /**
962  * snd_pcm_hw_constraint_minmax
963  * @runtime: PCM runtime instance
964  * @var: hw_params variable to apply the range
965  * @min: the minimal value
966  * @max: the maximal value
967  * 
968  * Apply the min/max range constraint to an interval parameter.
969  */
970 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
971                                  unsigned int min, unsigned int max)
972 {
973         struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
974         struct snd_interval t;
975         t.min = min;
976         t.max = max;
977         t.openmin = t.openmax = 0;
978         t.integer = 0;
979         return snd_interval_refine(constrs_interval(constrs, var), &t);
980 }
981
982 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
983
984 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
985                                 struct snd_pcm_hw_rule *rule)
986 {
987         struct snd_pcm_hw_constraint_list *list = rule->private;
988         return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
989 }               
990
991
992 /**
993  * snd_pcm_hw_constraint_list
994  * @runtime: PCM runtime instance
995  * @cond: condition bits
996  * @var: hw_params variable to apply the list constraint
997  * @l: list
998  * 
999  * Apply the list of constraints to an interval parameter.
1000  */
1001 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1002                                unsigned int cond,
1003                                snd_pcm_hw_param_t var,
1004                                struct snd_pcm_hw_constraint_list *l)
1005 {
1006         return snd_pcm_hw_rule_add(runtime, cond, var,
1007                                    snd_pcm_hw_rule_list, l,
1008                                    var, -1);
1009 }
1010
1011 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1012
1013 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1014                                    struct snd_pcm_hw_rule *rule)
1015 {
1016         struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1017         unsigned int num = 0, den = 0;
1018         int err;
1019         err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1020                                   r->nrats, r->rats, &num, &den);
1021         if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1022                 params->rate_num = num;
1023                 params->rate_den = den;
1024         }
1025         return err;
1026 }
1027
1028 /**
1029  * snd_pcm_hw_constraint_ratnums
1030  * @runtime: PCM runtime instance
1031  * @cond: condition bits
1032  * @var: hw_params variable to apply the ratnums constraint
1033  * @r: struct snd_ratnums constriants
1034  */
1035 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 
1036                                   unsigned int cond,
1037                                   snd_pcm_hw_param_t var,
1038                                   struct snd_pcm_hw_constraint_ratnums *r)
1039 {
1040         return snd_pcm_hw_rule_add(runtime, cond, var,
1041                                    snd_pcm_hw_rule_ratnums, r,
1042                                    var, -1);
1043 }
1044
1045 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1046
1047 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1048                                    struct snd_pcm_hw_rule *rule)
1049 {
1050         struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1051         unsigned int num = 0, den = 0;
1052         int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1053                                   r->nrats, r->rats, &num, &den);
1054         if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1055                 params->rate_num = num;
1056                 params->rate_den = den;
1057         }
1058         return err;
1059 }
1060
1061 /**
1062  * snd_pcm_hw_constraint_ratdens
1063  * @runtime: PCM runtime instance
1064  * @cond: condition bits
1065  * @var: hw_params variable to apply the ratdens constraint
1066  * @r: struct snd_ratdens constriants
1067  */
1068 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 
1069                                   unsigned int cond,
1070                                   snd_pcm_hw_param_t var,
1071                                   struct snd_pcm_hw_constraint_ratdens *r)
1072 {
1073         return snd_pcm_hw_rule_add(runtime, cond, var,
1074                                    snd_pcm_hw_rule_ratdens, r,
1075                                    var, -1);
1076 }
1077
1078 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1079
1080 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1081                                   struct snd_pcm_hw_rule *rule)
1082 {
1083         unsigned int l = (unsigned long) rule->private;
1084         int width = l & 0xffff;
1085         unsigned int msbits = l >> 16;
1086         struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1087         if (snd_interval_single(i) && snd_interval_value(i) == width)
1088                 params->msbits = msbits;
1089         return 0;
1090 }
1091
1092 /**
1093  * snd_pcm_hw_constraint_msbits
1094  * @runtime: PCM runtime instance
1095  * @cond: condition bits
1096  * @width: sample bits width
1097  * @msbits: msbits width
1098  */
1099 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 
1100                                  unsigned int cond,
1101                                  unsigned int width,
1102                                  unsigned int msbits)
1103 {
1104         unsigned long l = (msbits << 16) | width;
1105         return snd_pcm_hw_rule_add(runtime, cond, -1,
1106                                     snd_pcm_hw_rule_msbits,
1107                                     (void*) l,
1108                                     SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1109 }
1110
1111 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1112
1113 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1114                                 struct snd_pcm_hw_rule *rule)
1115 {
1116         unsigned long step = (unsigned long) rule->private;
1117         return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1118 }
1119
1120 /**
1121  * snd_pcm_hw_constraint_step
1122  * @runtime: PCM runtime instance
1123  * @cond: condition bits
1124  * @var: hw_params variable to apply the step constraint
1125  * @step: step size
1126  */
1127 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1128                                unsigned int cond,
1129                                snd_pcm_hw_param_t var,
1130                                unsigned long step)
1131 {
1132         return snd_pcm_hw_rule_add(runtime, cond, var, 
1133                                    snd_pcm_hw_rule_step, (void *) step,
1134                                    var, -1);
1135 }
1136
1137 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1138
1139 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1140 {
1141         static unsigned int pow2_sizes[] = {
1142                 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1143                 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1144                 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1145                 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1146         };
1147         return snd_interval_list(hw_param_interval(params, rule->var),
1148                                  ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1149 }               
1150
1151 /**
1152  * snd_pcm_hw_constraint_pow2
1153  * @runtime: PCM runtime instance
1154  * @cond: condition bits
1155  * @var: hw_params variable to apply the power-of-2 constraint
1156  */
1157 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1158                                unsigned int cond,
1159                                snd_pcm_hw_param_t var)
1160 {
1161         return snd_pcm_hw_rule_add(runtime, cond, var, 
1162                                    snd_pcm_hw_rule_pow2, NULL,
1163                                    var, -1);
1164 }
1165
1166 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1167
1168 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1169                                   snd_pcm_hw_param_t var)
1170 {
1171         if (hw_is_mask(var)) {
1172                 snd_mask_any(hw_param_mask(params, var));
1173                 params->cmask |= 1 << var;
1174                 params->rmask |= 1 << var;
1175                 return;
1176         }
1177         if (hw_is_interval(var)) {
1178                 snd_interval_any(hw_param_interval(params, var));
1179                 params->cmask |= 1 << var;
1180                 params->rmask |= 1 << var;
1181                 return;
1182         }
1183         snd_BUG();
1184 }
1185
1186 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1187 {
1188         unsigned int k;
1189         memset(params, 0, sizeof(*params));
1190         for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1191                 _snd_pcm_hw_param_any(params, k);
1192         for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1193                 _snd_pcm_hw_param_any(params, k);
1194         params->info = ~0U;
1195 }
1196
1197 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1198
1199 /**
1200  * snd_pcm_hw_param_value
1201  * @params: the hw_params instance
1202  * @var: parameter to retrieve
1203  * @dir: pointer to the direction (-1,0,1) or NULL
1204  *
1205  * Return the value for field PAR if it's fixed in configuration space 
1206  *  defined by PARAMS. Return -EINVAL otherwise
1207  */
1208 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1209                            snd_pcm_hw_param_t var, int *dir)
1210 {
1211         if (hw_is_mask(var)) {
1212                 const struct snd_mask *mask = hw_param_mask_c(params, var);
1213                 if (!snd_mask_single(mask))
1214                         return -EINVAL;
1215                 if (dir)
1216                         *dir = 0;
1217                 return snd_mask_value(mask);
1218         }
1219         if (hw_is_interval(var)) {
1220                 const struct snd_interval *i = hw_param_interval_c(params, var);
1221                 if (!snd_interval_single(i))
1222                         return -EINVAL;
1223                 if (dir)
1224                         *dir = i->openmin;
1225                 return snd_interval_value(i);
1226         }
1227         return -EINVAL;
1228 }
1229
1230 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1231
1232 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1233                                 snd_pcm_hw_param_t var)
1234 {
1235         if (hw_is_mask(var)) {
1236                 snd_mask_none(hw_param_mask(params, var));
1237                 params->cmask |= 1 << var;
1238                 params->rmask |= 1 << var;
1239         } else if (hw_is_interval(var)) {
1240                 snd_interval_none(hw_param_interval(params, var));
1241                 params->cmask |= 1 << var;
1242                 params->rmask |= 1 << var;
1243         } else {
1244                 snd_BUG();
1245         }
1246 }
1247
1248 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1249
1250 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1251                                    snd_pcm_hw_param_t var)
1252 {
1253         int changed;
1254         if (hw_is_mask(var))
1255                 changed = snd_mask_refine_first(hw_param_mask(params, var));
1256         else if (hw_is_interval(var))
1257                 changed = snd_interval_refine_first(hw_param_interval(params, var));
1258         else
1259                 return -EINVAL;
1260         if (changed) {
1261                 params->cmask |= 1 << var;
1262                 params->rmask |= 1 << var;
1263         }
1264         return changed;
1265 }
1266
1267
1268 /**
1269  * snd_pcm_hw_param_first
1270  * @pcm: PCM instance
1271  * @params: the hw_params instance
1272  * @var: parameter to retrieve
1273  * @dir: pointer to the direction (-1,0,1) or NULL
1274  *
1275  * Inside configuration space defined by PARAMS remove from PAR all 
1276  * values > minimum. Reduce configuration space accordingly.
1277  * Return the minimum.
1278  */
1279 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, 
1280                            struct snd_pcm_hw_params *params, 
1281                            snd_pcm_hw_param_t var, int *dir)
1282 {
1283         int changed = _snd_pcm_hw_param_first(params, var);
1284         if (changed < 0)
1285                 return changed;
1286         if (params->rmask) {
1287                 int err = snd_pcm_hw_refine(pcm, params);
1288                 snd_assert(err >= 0, return err);
1289         }
1290         return snd_pcm_hw_param_value(params, var, dir);
1291 }
1292
1293 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1294
1295 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1296                                   snd_pcm_hw_param_t var)
1297 {
1298         int changed;
1299         if (hw_is_mask(var))
1300                 changed = snd_mask_refine_last(hw_param_mask(params, var));
1301         else if (hw_is_interval(var))
1302                 changed = snd_interval_refine_last(hw_param_interval(params, var));
1303         else
1304                 return -EINVAL;
1305         if (changed) {
1306                 params->cmask |= 1 << var;
1307                 params->rmask |= 1 << var;
1308         }
1309         return changed;
1310 }
1311
1312
1313 /**
1314  * snd_pcm_hw_param_last
1315  * @pcm: PCM instance
1316  * @params: the hw_params instance
1317  * @var: parameter to retrieve
1318  * @dir: pointer to the direction (-1,0,1) or NULL
1319  *
1320  * Inside configuration space defined by PARAMS remove from PAR all 
1321  * values < maximum. Reduce configuration space accordingly.
1322  * Return the maximum.
1323  */
1324 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, 
1325                           struct snd_pcm_hw_params *params,
1326                           snd_pcm_hw_param_t var, int *dir)
1327 {
1328         int changed = _snd_pcm_hw_param_last(params, var);
1329         if (changed < 0)
1330                 return changed;
1331         if (params->rmask) {
1332                 int err = snd_pcm_hw_refine(pcm, params);
1333                 snd_assert(err >= 0, return err);
1334         }
1335         return snd_pcm_hw_param_value(params, var, dir);
1336 }
1337
1338 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1339
1340 /**
1341  * snd_pcm_hw_param_choose
1342  * @pcm: PCM instance
1343  * @params: the hw_params instance
1344  *
1345  * Choose one configuration from configuration space defined by PARAMS
1346  * The configuration chosen is that obtained fixing in this order:
1347  * first access, first format, first subformat, min channels,
1348  * min rate, min period time, max buffer size, min tick time
1349  */
1350 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1351                              struct snd_pcm_hw_params *params)
1352 {
1353         static int vars[] = {
1354                 SNDRV_PCM_HW_PARAM_ACCESS,
1355                 SNDRV_PCM_HW_PARAM_FORMAT,
1356                 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1357                 SNDRV_PCM_HW_PARAM_CHANNELS,
1358                 SNDRV_PCM_HW_PARAM_RATE,
1359                 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1360                 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1361                 SNDRV_PCM_HW_PARAM_TICK_TIME,
1362                 -1
1363         };
1364         int err, *v;
1365
1366         for (v = vars; *v != -1; v++) {
1367                 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1368                         err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1369                 else
1370                         err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1371                 snd_assert(err >= 0, return err);
1372         }
1373         return 0;
1374 }
1375
1376 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1377                                    void *arg)
1378 {
1379         struct snd_pcm_runtime *runtime = substream->runtime;
1380         unsigned long flags;
1381         snd_pcm_stream_lock_irqsave(substream, flags);
1382         if (snd_pcm_running(substream) &&
1383             snd_pcm_update_hw_ptr(substream) >= 0)
1384                 runtime->status->hw_ptr %= runtime->buffer_size;
1385         else
1386                 runtime->status->hw_ptr = 0;
1387         snd_pcm_stream_unlock_irqrestore(substream, flags);
1388         return 0;
1389 }
1390
1391 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1392                                           void *arg)
1393 {
1394         struct snd_pcm_channel_info *info = arg;
1395         struct snd_pcm_runtime *runtime = substream->runtime;
1396         int width;
1397         if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1398                 info->offset = -1;
1399                 return 0;
1400         }
1401         width = snd_pcm_format_physical_width(runtime->format);
1402         if (width < 0)
1403                 return width;
1404         info->offset = 0;
1405         switch (runtime->access) {
1406         case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1407         case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1408                 info->first = info->channel * width;
1409                 info->step = runtime->channels * width;
1410                 break;
1411         case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1412         case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1413         {
1414                 size_t size = runtime->dma_bytes / runtime->channels;
1415                 info->first = info->channel * size * 8;
1416                 info->step = width;
1417                 break;
1418         }
1419         default:
1420                 snd_BUG();
1421                 break;
1422         }
1423         return 0;
1424 }
1425
1426 /**
1427  * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1428  * @substream: the pcm substream instance
1429  * @cmd: ioctl command
1430  * @arg: ioctl argument
1431  *
1432  * Processes the generic ioctl commands for PCM.
1433  * Can be passed as the ioctl callback for PCM ops.
1434  *
1435  * Returns zero if successful, or a negative error code on failure.
1436  */
1437 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1438                       unsigned int cmd, void *arg)
1439 {
1440         switch (cmd) {
1441         case SNDRV_PCM_IOCTL1_INFO:
1442                 return 0;
1443         case SNDRV_PCM_IOCTL1_RESET:
1444                 return snd_pcm_lib_ioctl_reset(substream, arg);
1445         case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1446                 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1447         }
1448         return -ENXIO;
1449 }
1450
1451 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1452
1453 /**
1454  * snd_pcm_period_elapsed - update the pcm status for the next period
1455  * @substream: the pcm substream instance
1456  *
1457  * This function is called from the interrupt handler when the
1458  * PCM has processed the period size.  It will update the current
1459  * pointer, wake up sleepers, etc.
1460  *
1461  * Even if more than one periods have elapsed since the last call, you
1462  * have to call this only once.
1463  */
1464 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1465 {
1466         struct snd_pcm_runtime *runtime;
1467         unsigned long flags;
1468
1469         snd_assert(substream != NULL, return);
1470         runtime = substream->runtime;
1471         snd_assert(runtime != NULL, return);
1472
1473         if (runtime->transfer_ack_begin)
1474                 runtime->transfer_ack_begin(substream);
1475
1476         snd_pcm_stream_lock_irqsave(substream, flags);
1477         if (!snd_pcm_running(substream) ||
1478             snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1479                 goto _end;
1480
1481         if (substream->timer_running)
1482                 snd_timer_interrupt(substream->timer, 1);
1483  _end:
1484         snd_pcm_stream_unlock_irqrestore(substream, flags);
1485         if (runtime->transfer_ack_end)
1486                 runtime->transfer_ack_end(substream);
1487         kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1488 }
1489
1490 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1491
1492 /*
1493  * Wait until avail_min data becomes available
1494  * Returns a negative error code if any error occurs during operation.
1495  * The available space is stored on availp.  When err = 0 and avail = 0
1496  * on the capture stream, it indicates the stream is in DRAINING state.
1497  */
1498 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1499                               snd_pcm_uframes_t *availp)
1500 {
1501         struct snd_pcm_runtime *runtime = substream->runtime;
1502         int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1503         wait_queue_t wait;
1504         int err = 0;
1505         snd_pcm_uframes_t avail = 0;
1506         long tout;
1507
1508         init_waitqueue_entry(&wait, current);
1509         add_wait_queue(&runtime->sleep, &wait);
1510         for (;;) {
1511                 if (signal_pending(current)) {
1512                         err = -ERESTARTSYS;
1513                         break;
1514                 }
1515                 set_current_state(TASK_INTERRUPTIBLE);
1516                 snd_pcm_stream_unlock_irq(substream);
1517                 tout = schedule_timeout(msecs_to_jiffies(10000));
1518                 snd_pcm_stream_lock_irq(substream);
1519                 switch (runtime->status->state) {
1520                 case SNDRV_PCM_STATE_SUSPENDED:
1521                         err = -ESTRPIPE;
1522                         goto _endloop;
1523                 case SNDRV_PCM_STATE_XRUN:
1524                         err = -EPIPE;
1525                         goto _endloop;
1526                 case SNDRV_PCM_STATE_DRAINING:
1527                         if (is_playback)
1528                                 err = -EPIPE;
1529                         else 
1530                                 avail = 0; /* indicate draining */
1531                         goto _endloop;
1532                 case SNDRV_PCM_STATE_OPEN:
1533                 case SNDRV_PCM_STATE_SETUP:
1534                 case SNDRV_PCM_STATE_DISCONNECTED:
1535                         err = -EBADFD;
1536                         goto _endloop;
1537                 }
1538                 if (!tout) {
1539                         snd_printd("%s write error (DMA or IRQ trouble?)\n",
1540                                    is_playback ? "playback" : "capture");
1541                         err = -EIO;
1542                         break;
1543                 }
1544                 if (is_playback)
1545                         avail = snd_pcm_playback_avail(runtime);
1546                 else
1547                         avail = snd_pcm_capture_avail(runtime);
1548                 if (avail >= runtime->control->avail_min)
1549                         break;
1550         }
1551  _endloop:
1552         remove_wait_queue(&runtime->sleep, &wait);
1553         *availp = avail;
1554         return err;
1555 }
1556         
1557 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1558                                       unsigned int hwoff,
1559                                       unsigned long data, unsigned int off,
1560                                       snd_pcm_uframes_t frames)
1561 {
1562         struct snd_pcm_runtime *runtime = substream->runtime;
1563         int err;
1564         char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1565         if (substream->ops->copy) {
1566                 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1567                         return err;
1568         } else {
1569                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1570                 snd_assert(runtime->dma_area, return -EFAULT);
1571                 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1572                         return -EFAULT;
1573         }
1574         return 0;
1575 }
1576  
1577 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1578                           unsigned long data, unsigned int off,
1579                           snd_pcm_uframes_t size);
1580
1581 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, 
1582                                             unsigned long data,
1583                                             snd_pcm_uframes_t size,
1584                                             int nonblock,
1585                                             transfer_f transfer)
1586 {
1587         struct snd_pcm_runtime *runtime = substream->runtime;
1588         snd_pcm_uframes_t xfer = 0;
1589         snd_pcm_uframes_t offset = 0;
1590         int err = 0;
1591
1592         if (size == 0)
1593                 return 0;
1594
1595         snd_pcm_stream_lock_irq(substream);
1596         switch (runtime->status->state) {
1597         case SNDRV_PCM_STATE_PREPARED:
1598         case SNDRV_PCM_STATE_RUNNING:
1599         case SNDRV_PCM_STATE_PAUSED:
1600                 break;
1601         case SNDRV_PCM_STATE_XRUN:
1602                 err = -EPIPE;
1603                 goto _end_unlock;
1604         case SNDRV_PCM_STATE_SUSPENDED:
1605                 err = -ESTRPIPE;
1606                 goto _end_unlock;
1607         default:
1608                 err = -EBADFD;
1609                 goto _end_unlock;
1610         }
1611
1612         while (size > 0) {
1613                 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1614                 snd_pcm_uframes_t avail;
1615                 snd_pcm_uframes_t cont;
1616                 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1617                         snd_pcm_update_hw_ptr(substream);
1618                 avail = snd_pcm_playback_avail(runtime);
1619                 if (!avail) {
1620                         if (nonblock) {
1621                                 err = -EAGAIN;
1622                                 goto _end_unlock;
1623                         }
1624                         err = wait_for_avail_min(substream, &avail);
1625                         if (err < 0)
1626                                 goto _end_unlock;
1627                 }
1628                 frames = size > avail ? avail : size;
1629                 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1630                 if (frames > cont)
1631                         frames = cont;
1632                 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1633                 appl_ptr = runtime->control->appl_ptr;
1634                 appl_ofs = appl_ptr % runtime->buffer_size;
1635                 snd_pcm_stream_unlock_irq(substream);
1636                 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1637                         goto _end;
1638                 snd_pcm_stream_lock_irq(substream);
1639                 switch (runtime->status->state) {
1640                 case SNDRV_PCM_STATE_XRUN:
1641                         err = -EPIPE;
1642                         goto _end_unlock;
1643                 case SNDRV_PCM_STATE_SUSPENDED:
1644                         err = -ESTRPIPE;
1645                         goto _end_unlock;
1646                 default:
1647                         break;
1648                 }
1649                 appl_ptr += frames;
1650                 if (appl_ptr >= runtime->boundary)
1651                         appl_ptr -= runtime->boundary;
1652                 runtime->control->appl_ptr = appl_ptr;
1653                 if (substream->ops->ack)
1654                         substream->ops->ack(substream);
1655
1656                 offset += frames;
1657                 size -= frames;
1658                 xfer += frames;
1659                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1660                     snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1661                         err = snd_pcm_start(substream);
1662                         if (err < 0)
1663                                 goto _end_unlock;
1664                 }
1665         }
1666  _end_unlock:
1667         snd_pcm_stream_unlock_irq(substream);
1668  _end:
1669         return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1670 }
1671
1672 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1673 {
1674         struct snd_pcm_runtime *runtime;
1675         int nonblock;
1676
1677         snd_assert(substream != NULL, return -ENXIO);
1678         runtime = substream->runtime;
1679         snd_assert(runtime != NULL, return -ENXIO);
1680         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1681         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1682                 return -EBADFD;
1683
1684         nonblock = !!(substream->f_flags & O_NONBLOCK);
1685
1686         if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1687             runtime->channels > 1)
1688                 return -EINVAL;
1689         return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1690                                   snd_pcm_lib_write_transfer);
1691 }
1692
1693 EXPORT_SYMBOL(snd_pcm_lib_write);
1694
1695 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1696                                        unsigned int hwoff,
1697                                        unsigned long data, unsigned int off,
1698                                        snd_pcm_uframes_t frames)
1699 {
1700         struct snd_pcm_runtime *runtime = substream->runtime;
1701         int err;
1702         void __user **bufs = (void __user **)data;
1703         int channels = runtime->channels;
1704         int c;
1705         if (substream->ops->copy) {
1706                 snd_assert(substream->ops->silence != NULL, return -EINVAL);
1707                 for (c = 0; c < channels; ++c, ++bufs) {
1708                         if (*bufs == NULL) {
1709                                 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1710                                         return err;
1711                         } else {
1712                                 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1713                                 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1714                                         return err;
1715                         }
1716                 }
1717         } else {
1718                 /* default transfer behaviour */
1719                 size_t dma_csize = runtime->dma_bytes / channels;
1720                 snd_assert(runtime->dma_area, return -EFAULT);
1721                 for (c = 0; c < channels; ++c, ++bufs) {
1722                         char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1723                         if (*bufs == NULL) {
1724                                 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1725                         } else {
1726                                 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1727                                 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1728                                         return -EFAULT;
1729                         }
1730                 }
1731         }
1732         return 0;
1733 }
1734  
1735 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1736                                      void __user **bufs,
1737                                      snd_pcm_uframes_t frames)
1738 {
1739         struct snd_pcm_runtime *runtime;
1740         int nonblock;
1741
1742         snd_assert(substream != NULL, return -ENXIO);
1743         runtime = substream->runtime;
1744         snd_assert(runtime != NULL, return -ENXIO);
1745         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1746         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1747                 return -EBADFD;
1748
1749         nonblock = !!(substream->f_flags & O_NONBLOCK);
1750
1751         if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1752                 return -EINVAL;
1753         return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1754                                   nonblock, snd_pcm_lib_writev_transfer);
1755 }
1756
1757 EXPORT_SYMBOL(snd_pcm_lib_writev);
1758
1759 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, 
1760                                      unsigned int hwoff,
1761                                      unsigned long data, unsigned int off,
1762                                      snd_pcm_uframes_t frames)
1763 {
1764         struct snd_pcm_runtime *runtime = substream->runtime;
1765         int err;
1766         char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1767         if (substream->ops->copy) {
1768                 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1769                         return err;
1770         } else {
1771                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1772                 snd_assert(runtime->dma_area, return -EFAULT);
1773                 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1774                         return -EFAULT;
1775         }
1776         return 0;
1777 }
1778
1779 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1780                                            unsigned long data,
1781                                            snd_pcm_uframes_t size,
1782                                            int nonblock,
1783                                            transfer_f transfer)
1784 {
1785         struct snd_pcm_runtime *runtime = substream->runtime;
1786         snd_pcm_uframes_t xfer = 0;
1787         snd_pcm_uframes_t offset = 0;
1788         int err = 0;
1789
1790         if (size == 0)
1791                 return 0;
1792
1793         snd_pcm_stream_lock_irq(substream);
1794         switch (runtime->status->state) {
1795         case SNDRV_PCM_STATE_PREPARED:
1796                 if (size >= runtime->start_threshold) {
1797                         err = snd_pcm_start(substream);
1798                         if (err < 0)
1799                                 goto _end_unlock;
1800                 }
1801                 break;
1802         case SNDRV_PCM_STATE_DRAINING:
1803         case SNDRV_PCM_STATE_RUNNING:
1804         case SNDRV_PCM_STATE_PAUSED:
1805                 break;
1806         case SNDRV_PCM_STATE_XRUN:
1807                 err = -EPIPE;
1808                 goto _end_unlock;
1809         case SNDRV_PCM_STATE_SUSPENDED:
1810                 err = -ESTRPIPE;
1811                 goto _end_unlock;
1812         default:
1813                 err = -EBADFD;
1814                 goto _end_unlock;
1815         }
1816
1817         while (size > 0) {
1818                 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1819                 snd_pcm_uframes_t avail;
1820                 snd_pcm_uframes_t cont;
1821                 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1822                         snd_pcm_update_hw_ptr(substream);
1823                 avail = snd_pcm_capture_avail(runtime);
1824                 if (!avail) {
1825                         if (runtime->status->state ==
1826                             SNDRV_PCM_STATE_DRAINING) {
1827                                 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1828                                 goto _end_unlock;
1829                         }
1830                         if (nonblock) {
1831                                 err = -EAGAIN;
1832                                 goto _end_unlock;
1833                         }
1834                         err = wait_for_avail_min(substream, &avail);
1835                         if (err < 0)
1836                                 goto _end_unlock;
1837                         if (!avail)
1838                                 continue; /* draining */
1839                 }
1840                 frames = size > avail ? avail : size;
1841                 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1842                 if (frames > cont)
1843                         frames = cont;
1844                 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
1845                 appl_ptr = runtime->control->appl_ptr;
1846                 appl_ofs = appl_ptr % runtime->buffer_size;
1847                 snd_pcm_stream_unlock_irq(substream);
1848                 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1849                         goto _end;
1850                 snd_pcm_stream_lock_irq(substream);
1851                 switch (runtime->status->state) {
1852                 case SNDRV_PCM_STATE_XRUN:
1853                         err = -EPIPE;
1854                         goto _end_unlock;
1855                 case SNDRV_PCM_STATE_SUSPENDED:
1856                         err = -ESTRPIPE;
1857                         goto _end_unlock;
1858                 default:
1859                         break;
1860                 }
1861                 appl_ptr += frames;
1862                 if (appl_ptr >= runtime->boundary)
1863                         appl_ptr -= runtime->boundary;
1864                 runtime->control->appl_ptr = appl_ptr;
1865                 if (substream->ops->ack)
1866                         substream->ops->ack(substream);
1867
1868                 offset += frames;
1869                 size -= frames;
1870                 xfer += frames;
1871         }
1872  _end_unlock:
1873         snd_pcm_stream_unlock_irq(substream);
1874  _end:
1875         return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1876 }
1877
1878 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
1879 {
1880         struct snd_pcm_runtime *runtime;
1881         int nonblock;
1882         
1883         snd_assert(substream != NULL, return -ENXIO);
1884         runtime = substream->runtime;
1885         snd_assert(runtime != NULL, return -ENXIO);
1886         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1887         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1888                 return -EBADFD;
1889
1890         nonblock = !!(substream->f_flags & O_NONBLOCK);
1891         if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1892                 return -EINVAL;
1893         return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
1894 }
1895
1896 EXPORT_SYMBOL(snd_pcm_lib_read);
1897
1898 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
1899                                       unsigned int hwoff,
1900                                       unsigned long data, unsigned int off,
1901                                       snd_pcm_uframes_t frames)
1902 {
1903         struct snd_pcm_runtime *runtime = substream->runtime;
1904         int err;
1905         void __user **bufs = (void __user **)data;
1906         int channels = runtime->channels;
1907         int c;
1908         if (substream->ops->copy) {
1909                 for (c = 0; c < channels; ++c, ++bufs) {
1910                         char __user *buf;
1911                         if (*bufs == NULL)
1912                                 continue;
1913                         buf = *bufs + samples_to_bytes(runtime, off);
1914                         if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1915                                 return err;
1916                 }
1917         } else {
1918                 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
1919                 snd_assert(runtime->dma_area, return -EFAULT);
1920                 for (c = 0; c < channels; ++c, ++bufs) {
1921                         char *hwbuf;
1922                         char __user *buf;
1923                         if (*bufs == NULL)
1924                                 continue;
1925
1926                         hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1927                         buf = *bufs + samples_to_bytes(runtime, off);
1928                         if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
1929                                 return -EFAULT;
1930                 }
1931         }
1932         return 0;
1933 }
1934  
1935 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1936                                     void __user **bufs,
1937                                     snd_pcm_uframes_t frames)
1938 {
1939         struct snd_pcm_runtime *runtime;
1940         int nonblock;
1941
1942         snd_assert(substream != NULL, return -ENXIO);
1943         runtime = substream->runtime;
1944         snd_assert(runtime != NULL, return -ENXIO);
1945         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
1946         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1947                 return -EBADFD;
1948
1949         nonblock = !!(substream->f_flags & O_NONBLOCK);
1950         if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1951                 return -EINVAL;
1952         return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
1953 }
1954
1955 EXPORT_SYMBOL(snd_pcm_lib_readv);