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