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