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