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