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