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