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