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