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