patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / core / pcm_lib.c
1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
24 #include <linux/slab.h>
25 #include <linux/time.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(snd_pcm_substream_t *substream, snd_pcm_uframes_t new_hw_ptr)
43 {
44         snd_pcm_runtime_t *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                 snd_assert(runtime->silence_filled <= runtime->buffer_size, return);
62                 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
63                 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
64                         return;
65                 frames = runtime->silence_threshold - noise_dist;
66                 if (frames > runtime->silence_size)
67                         frames = runtime->silence_size;
68         } else {
69                 if (new_hw_ptr == ULONG_MAX) {  /* initialization */
70                         snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
71                         runtime->silence_filled = avail > 0 ? avail : 0;
72                         runtime->silence_start = (runtime->status->hw_ptr +
73                                                   runtime->silence_filled) %
74                                                  runtime->boundary;
75                 } else {
76                         ofs = runtime->status->hw_ptr;
77                         frames = new_hw_ptr - ofs;
78                         if ((snd_pcm_sframes_t)frames < 0)
79                                 frames += runtime->boundary;
80                         runtime->silence_filled -= frames;
81                         if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
82                                 runtime->silence_filled = 0;
83                                 runtime->silence_start = (ofs + frames) - runtime->buffer_size;
84                         } else {
85                                 runtime->silence_start = ofs - runtime->silence_filled;
86                         }
87                         if ((snd_pcm_sframes_t)runtime->silence_start < 0)
88                                 runtime->silence_start += runtime->boundary;
89                 }
90                 frames = runtime->buffer_size - runtime->silence_filled;
91         }
92         snd_assert(frames <= runtime->buffer_size, return);
93         if (frames == 0)
94                 return;
95         ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
96         while (frames > 0) {
97                 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
98                 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
99                     runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
100                         if (substream->ops->silence) {
101                                 int err;
102                                 err = substream->ops->silence(substream, -1, ofs, transfer);
103                                 snd_assert(err >= 0, );
104                         } else {
105                                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
106                                 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
107                         }
108                 } else {
109                         unsigned int c;
110                         unsigned int channels = runtime->channels;
111                         if (substream->ops->silence) {
112                                 for (c = 0; c < channels; ++c) {
113                                         int err;
114                                         err = substream->ops->silence(substream, c, ofs, transfer);
115                                         snd_assert(err >= 0, );
116                                 }
117                         } else {
118                                 size_t dma_csize = runtime->dma_bytes / channels;
119                                 for (c = 0; c < channels; ++c) {
120                                         char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
121                                         snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
122                                 }
123                         }
124                 }
125                 runtime->silence_filled += transfer;
126                 frames -= transfer;
127                 ofs = 0;
128         }
129 }
130
131 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(snd_pcm_substream_t *substream,
132                                                           snd_pcm_runtime_t *runtime)
133 {
134         snd_pcm_uframes_t pos;
135
136         pos = substream->ops->pointer(substream);
137         if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
138                 snd_timestamp_now((snd_timestamp_t*)&runtime->status->tstamp, runtime->tstamp_timespec);
139 #ifdef CONFIG_SND_DEBUG
140         if (pos >= runtime->buffer_size) {
141                 snd_printk(KERN_ERR  "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size);
142         } else
143 #endif
144         snd_runtime_check(pos < runtime->buffer_size, return 0);
145         pos -= pos % runtime->min_align;
146         return pos;
147 }
148
149 static inline int snd_pcm_update_hw_ptr_post(snd_pcm_substream_t *substream,
150                                              snd_pcm_runtime_t *runtime)
151 {
152         snd_pcm_uframes_t avail;
153
154         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155                 avail = snd_pcm_playback_avail(runtime);
156         else
157                 avail = snd_pcm_capture_avail(runtime);
158         if (avail > runtime->avail_max)
159                 runtime->avail_max = avail;
160         if (avail >= runtime->stop_threshold) {
161                 snd_pcm_stop(substream,
162                              runtime->status->state == SNDRV_PCM_STATE_DRAINING ?
163                              SNDRV_PCM_STATE_SETUP : SNDRV_PCM_STATE_XRUN);
164 #ifdef CONFIG_SND_DEBUG
165                 if (substream->pstr->xrun_debug) {
166                         snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
167                                    substream->pcm->card->number,
168                                    substream->pcm->device,
169                                    substream->stream ? 'c' : 'p');
170                         if (substream->pstr->xrun_debug > 1)
171                                 dump_stack();
172                 }
173 #endif
174                 return -EPIPE;
175         }
176         if (avail >= runtime->control->avail_min)
177                 wake_up(&runtime->sleep);
178         return 0;
179 }
180
181 static inline int snd_pcm_update_hw_ptr_interrupt(snd_pcm_substream_t *substream)
182 {
183         snd_pcm_runtime_t *runtime = substream->runtime;
184         snd_pcm_uframes_t pos;
185         snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
186         snd_pcm_sframes_t delta;
187
188         pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
189         if (runtime->period_size == runtime->buffer_size)
190                 goto __next_buf;
191         new_hw_ptr = runtime->hw_ptr_base + pos;
192         hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
193
194         delta = hw_ptr_interrupt - new_hw_ptr;
195         if (delta > 0) {
196                 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
197 #ifdef CONFIG_SND_DEBUG
198                         if (runtime->periods > 1 && substream->pstr->xrun_debug) {
199                                 snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
200                                 if (substream->pstr->xrun_debug > 1)
201                                         dump_stack();
202                         }
203 #endif
204                         return 0;
205                 }
206               __next_buf:
207                 runtime->hw_ptr_base += runtime->buffer_size;
208                 if (runtime->hw_ptr_base == runtime->boundary)
209                         runtime->hw_ptr_base = 0;
210                 new_hw_ptr = runtime->hw_ptr_base + pos;
211         }
212
213         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
214             runtime->silence_size > 0)
215                 snd_pcm_playback_silence(substream, new_hw_ptr);
216
217         runtime->status->hw_ptr = new_hw_ptr;
218         runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
219
220         return snd_pcm_update_hw_ptr_post(substream, runtime);
221 }
222
223 /* CAUTION: call it with irq disabled */
224 int snd_pcm_update_hw_ptr(snd_pcm_substream_t *substream)
225 {
226         snd_pcm_runtime_t *runtime = substream->runtime;
227         snd_pcm_uframes_t pos;
228         snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
229         snd_pcm_sframes_t delta;
230
231         old_hw_ptr = runtime->status->hw_ptr;
232         pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
233         new_hw_ptr = runtime->hw_ptr_base + pos;
234
235         delta = old_hw_ptr - new_hw_ptr;
236         if (delta > 0) {
237                 if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
238 #ifdef CONFIG_SND_DEBUG
239                         if (runtime->periods > 2 && substream->pstr->xrun_debug) {
240                                 snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
241                                 if (substream->pstr->xrun_debug > 1)
242                                         dump_stack();
243                         }
244 #endif
245                         return 0;
246                 }
247                 runtime->hw_ptr_base += runtime->buffer_size;
248                 if (runtime->hw_ptr_base == runtime->boundary)
249                         runtime->hw_ptr_base = 0;
250                 new_hw_ptr = runtime->hw_ptr_base + pos;
251         }
252         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
253             runtime->silence_size > 0)
254                 snd_pcm_playback_silence(substream, new_hw_ptr);
255
256         runtime->status->hw_ptr = new_hw_ptr;
257
258         return snd_pcm_update_hw_ptr_post(substream, runtime);
259 }
260
261 /**
262  * snd_pcm_set_ops - set the PCM operators
263  * @pcm: the pcm instance
264  * @direction: stream direction, SNDRV_PCM_STREAM_XXX
265  * @ops: the operator table
266  *
267  * Sets the given PCM operators to the pcm instance.
268  */
269 void snd_pcm_set_ops(snd_pcm_t *pcm, int direction, snd_pcm_ops_t *ops)
270 {
271         snd_pcm_str_t *stream = &pcm->streams[direction];
272         snd_pcm_substream_t *substream;
273         
274         for (substream = stream->substream; substream != NULL; substream = substream->next)
275                 substream->ops = ops;
276 }
277
278
279 /**
280  * snd_pcm_sync - set the PCM sync id
281  * @substream: the pcm substream
282  *
283  * Sets the PCM sync identifier for the card.
284  */
285 void snd_pcm_set_sync(snd_pcm_substream_t * substream)
286 {
287         snd_pcm_runtime_t *runtime = substream->runtime;
288         
289         runtime->sync.id32[0] = substream->pcm->card->number;
290         runtime->sync.id32[1] = -1;
291         runtime->sync.id32[2] = -1;
292         runtime->sync.id32[3] = -1;
293 }
294
295 /*
296  *  Standard ioctl routine
297  */
298
299 /* Code taken from alsa-lib */
300 #define assert(a) snd_assert((a), return -EINVAL)
301
302 static inline unsigned int div32(unsigned int a, unsigned int b, 
303                                  unsigned int *r)
304 {
305         if (b == 0) {
306                 *r = 0;
307                 return UINT_MAX;
308         }
309         *r = a % b;
310         return a / b;
311 }
312
313 static inline unsigned int div_down(unsigned int a, unsigned int b)
314 {
315         if (b == 0)
316                 return UINT_MAX;
317         return a / b;
318 }
319
320 static inline unsigned int div_up(unsigned int a, unsigned int b)
321 {
322         unsigned int r;
323         unsigned int q;
324         if (b == 0)
325                 return UINT_MAX;
326         q = div32(a, b, &r);
327         if (r)
328                 ++q;
329         return q;
330 }
331
332 static inline unsigned int mul(unsigned int a, unsigned int b)
333 {
334         if (a == 0)
335                 return 0;
336         if (div_down(UINT_MAX, a) < b)
337                 return UINT_MAX;
338         return a * b;
339 }
340
341 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
342                                     unsigned int c, unsigned int *r)
343 {
344         u_int64_t n = (u_int64_t) a * b;
345         if (c == 0) {
346                 snd_assert(n > 0, );
347                 *r = 0;
348                 return UINT_MAX;
349         }
350         div64_32(&n, c, r);
351         if (n >= UINT_MAX) {
352                 *r = 0;
353                 return UINT_MAX;
354         }
355         return n;
356 }
357
358 int snd_interval_refine_min(snd_interval_t *i, unsigned int min, int openmin)
359 {
360         int changed = 0;
361         assert(!snd_interval_empty(i));
362         if (i->min < min) {
363                 i->min = min;
364                 i->openmin = openmin;
365                 changed = 1;
366         } else if (i->min == min && !i->openmin && openmin) {
367                 i->openmin = 1;
368                 changed = 1;
369         }
370         if (i->integer) {
371                 if (i->openmin) {
372                         i->min++;
373                         i->openmin = 0;
374                 }
375         }
376         if (snd_interval_checkempty(i)) {
377                 snd_interval_none(i);
378                 return -EINVAL;
379         }
380         return changed;
381 }
382
383 int snd_interval_refine_max(snd_interval_t *i, unsigned int max, int openmax)
384 {
385         int changed = 0;
386         assert(!snd_interval_empty(i));
387         if (i->max > max) {
388                 i->max = max;
389                 i->openmax = openmax;
390                 changed = 1;
391         } else if (i->max == max && !i->openmax && openmax) {
392                 i->openmax = 1;
393                 changed = 1;
394         }
395         if (i->integer) {
396                 if (i->openmax) {
397                         i->max--;
398                         i->openmax = 0;
399                 }
400         }
401         if (snd_interval_checkempty(i)) {
402                 snd_interval_none(i);
403                 return -EINVAL;
404         }
405         return changed;
406 }
407
408 /**
409  * snd_interval_refine - refine the interval value of configurator
410  * @i: the interval value to refine
411  * @v: the interval value to refer to
412  *
413  * Refines the interval value with the reference value.
414  * The interval is changed to the range satisfying both intervals.
415  * The interval status (min, max, integer, etc.) are evaluated.
416  *
417  * Returns non-zero if the value is changed, zero if not changed.
418  */
419 int snd_interval_refine(snd_interval_t *i, const snd_interval_t *v)
420 {
421         int changed = 0;
422         assert(!snd_interval_empty(i));
423         if (i->min < v->min) {
424                 i->min = v->min;
425                 i->openmin = v->openmin;
426                 changed = 1;
427         } else if (i->min == v->min && !i->openmin && v->openmin) {
428                 i->openmin = 1;
429                 changed = 1;
430         }
431         if (i->max > v->max) {
432                 i->max = v->max;
433                 i->openmax = v->openmax;
434                 changed = 1;
435         } else if (i->max == v->max && !i->openmax && v->openmax) {
436                 i->openmax = 1;
437                 changed = 1;
438         }
439         if (!i->integer && v->integer) {
440                 i->integer = 1;
441                 changed = 1;
442         }
443         if (i->integer) {
444                 if (i->openmin) {
445                         i->min++;
446                         i->openmin = 0;
447                 }
448                 if (i->openmax) {
449                         i->max--;
450                         i->openmax = 0;
451                 }
452         } else if (!i->openmin && !i->openmax && i->min == i->max)
453                 i->integer = 1;
454         if (snd_interval_checkempty(i)) {
455                 snd_interval_none(i);
456                 return -EINVAL;
457         }
458         return changed;
459 }
460
461 int snd_interval_refine_first(snd_interval_t *i)
462 {
463         assert(!snd_interval_empty(i));
464         if (snd_interval_single(i))
465                 return 0;
466         i->max = i->min;
467         i->openmax = i->openmin;
468         if (i->openmax)
469                 i->max++;
470         return 1;
471 }
472
473 int snd_interval_refine_last(snd_interval_t *i)
474 {
475         assert(!snd_interval_empty(i));
476         if (snd_interval_single(i))
477                 return 0;
478         i->min = i->max;
479         i->openmin = i->openmax;
480         if (i->openmin)
481                 i->min--;
482         return 1;
483 }
484
485 int snd_interval_refine_set(snd_interval_t *i, unsigned int val)
486 {
487         snd_interval_t t;
488         t.empty = 0;
489         t.min = t.max = val;
490         t.openmin = t.openmax = 0;
491         t.integer = 1;
492         return snd_interval_refine(i, &t);
493 }
494
495 void snd_interval_mul(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
496 {
497         if (a->empty || b->empty) {
498                 snd_interval_none(c);
499                 return;
500         }
501         c->empty = 0;
502         c->min = mul(a->min, b->min);
503         c->openmin = (a->openmin || b->openmin);
504         c->max = mul(a->max,  b->max);
505         c->openmax = (a->openmax || b->openmax);
506         c->integer = (a->integer && b->integer);
507 }
508
509 /**
510  * snd_interval_div - refine the interval value with division
511  *
512  * c = a / b
513  *
514  * Returns non-zero if the value is changed, zero if not changed.
515  */
516 void snd_interval_div(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
517 {
518         unsigned int r;
519         if (a->empty || b->empty) {
520                 snd_interval_none(c);
521                 return;
522         }
523         c->empty = 0;
524         c->min = div32(a->min, b->max, &r);
525         c->openmin = (r || a->openmin || b->openmax);
526         if (b->min > 0) {
527                 c->max = div32(a->max, b->min, &r);
528                 if (r) {
529                         c->max++;
530                         c->openmax = 1;
531                 } else
532                         c->openmax = (a->openmax || b->openmin);
533         } else {
534                 c->max = UINT_MAX;
535                 c->openmax = 0;
536         }
537         c->integer = 0;
538 }
539
540 /**
541  * snd_interval_muldivk - refine the interval value
542  *
543  * c = a * b / k
544  *
545  * Returns non-zero if the value is changed, zero if not changed.
546  */
547 void snd_interval_muldivk(const snd_interval_t *a, const snd_interval_t *b,
548                       unsigned int k, snd_interval_t *c)
549 {
550         unsigned int r;
551         if (a->empty || b->empty) {
552                 snd_interval_none(c);
553                 return;
554         }
555         c->empty = 0;
556         c->min = muldiv32(a->min, b->min, k, &r);
557         c->openmin = (r || a->openmin || b->openmin);
558         c->max = muldiv32(a->max, b->max, k, &r);
559         if (r) {
560                 c->max++;
561                 c->openmax = 1;
562         } else
563                 c->openmax = (a->openmax || b->openmax);
564         c->integer = 0;
565 }
566
567 /**
568  * snd_interval_mulkdiv - refine the interval value
569  *
570  * c = a * k / b
571  *
572  * Returns non-zero if the value is changed, zero if not changed.
573  */
574 void snd_interval_mulkdiv(const snd_interval_t *a, unsigned int k,
575                       const snd_interval_t *b, snd_interval_t *c)
576 {
577         unsigned int r;
578         if (a->empty || b->empty) {
579                 snd_interval_none(c);
580                 return;
581         }
582         c->empty = 0;
583         c->min = muldiv32(a->min, k, b->max, &r);
584         c->openmin = (r || a->openmin || b->openmax);
585         if (b->min > 0) {
586                 c->max = muldiv32(a->max, k, b->min, &r);
587                 if (r) {
588                         c->max++;
589                         c->openmax = 1;
590                 } else
591                         c->openmax = (a->openmax || b->openmin);
592         } else {
593                 c->max = UINT_MAX;
594                 c->openmax = 0;
595         }
596         c->integer = 0;
597 }
598
599 #undef assert
600 /* ---- */
601
602
603 /**
604  * snd_interval_ratnum - refine the interval value
605  *
606  * Returns non-zero if the value is changed, zero if not changed.
607  */
608 int snd_interval_ratnum(snd_interval_t *i,
609                     unsigned int rats_count, ratnum_t *rats,
610                     unsigned int *nump, unsigned int *denp)
611 {
612         unsigned int best_num, best_diff, best_den;
613         unsigned int k;
614         snd_interval_t t;
615         int err;
616
617         best_num = best_den = best_diff = 0;
618         for (k = 0; k < rats_count; ++k) {
619                 unsigned int num = rats[k].num;
620                 unsigned int den;
621                 unsigned int q = i->min;
622                 int diff;
623                 if (q == 0)
624                         q = 1;
625                 den = div_down(num, q);
626                 if (den < rats[k].den_min)
627                         continue;
628                 if (den > rats[k].den_max)
629                         den = rats[k].den_max;
630                 else {
631                         unsigned int r;
632                         r = (den - rats[k].den_min) % rats[k].den_step;
633                         if (r != 0)
634                                 den -= r;
635                 }
636                 diff = num - q * den;
637                 if (best_num == 0 ||
638                     diff * best_den < best_diff * den) {
639                         best_diff = diff;
640                         best_den = den;
641                         best_num = num;
642                 }
643         }
644         if (best_den == 0) {
645                 i->empty = 1;
646                 return -EINVAL;
647         }
648         t.min = div_down(best_num, best_den);
649         t.openmin = !!(best_num % best_den);
650         
651         best_num = best_den = best_diff = 0;
652         for (k = 0; k < rats_count; ++k) {
653                 unsigned int num = rats[k].num;
654                 unsigned int den;
655                 unsigned int q = i->max;
656                 int diff;
657                 if (q == 0) {
658                         i->empty = 1;
659                         return -EINVAL;
660                 }
661                 den = div_up(num, q);
662                 if (den > rats[k].den_max)
663                         continue;
664                 if (den < rats[k].den_min)
665                         den = rats[k].den_min;
666                 else {
667                         unsigned int r;
668                         r = (den - rats[k].den_min) % rats[k].den_step;
669                         if (r != 0)
670                                 den += rats[k].den_step - r;
671                 }
672                 diff = q * den - num;
673                 if (best_num == 0 ||
674                     diff * best_den < best_diff * den) {
675                         best_diff = diff;
676                         best_den = den;
677                         best_num = num;
678                 }
679         }
680         if (best_den == 0) {
681                 i->empty = 1;
682                 return -EINVAL;
683         }
684         t.max = div_up(best_num, best_den);
685         t.openmax = !!(best_num % best_den);
686         t.integer = 0;
687         err = snd_interval_refine(i, &t);
688         if (err < 0)
689                 return err;
690
691         if (snd_interval_single(i)) {
692                 if (nump)
693                         *nump = best_num;
694                 if (denp)
695                         *denp = best_den;
696         }
697         return err;
698 }
699
700 /**
701  * snd_interval_ratden - refine the interval value
702  *
703  * Returns non-zero if the value is changed, zero if not changed.
704  */
705 int snd_interval_ratden(snd_interval_t *i,
706                     unsigned int rats_count, ratden_t *rats,
707                     unsigned int *nump, unsigned int *denp)
708 {
709         unsigned int best_num, best_diff, best_den;
710         unsigned int k;
711         snd_interval_t t;
712         int err;
713
714         best_num = best_den = best_diff = 0;
715         for (k = 0; k < rats_count; ++k) {
716                 unsigned int num;
717                 unsigned int den = rats[k].den;
718                 unsigned int q = i->min;
719                 int diff;
720                 num = mul(q, den);
721                 if (num > rats[k].num_max)
722                         continue;
723                 if (num < rats[k].num_min)
724                         num = rats[k].num_max;
725                 else {
726                         unsigned int r;
727                         r = (num - rats[k].num_min) % rats[k].num_step;
728                         if (r != 0)
729                                 num += rats[k].num_step - r;
730                 }
731                 diff = num - q * den;
732                 if (best_num == 0 ||
733                     diff * best_den < best_diff * den) {
734                         best_diff = diff;
735                         best_den = den;
736                         best_num = num;
737                 }
738         }
739         if (best_den == 0) {
740                 i->empty = 1;
741                 return -EINVAL;
742         }
743         t.min = div_down(best_num, best_den);
744         t.openmin = !!(best_num % best_den);
745         
746         best_num = best_den = best_diff = 0;
747         for (k = 0; k < rats_count; ++k) {
748                 unsigned int num;
749                 unsigned int den = rats[k].den;
750                 unsigned int q = i->max;
751                 int diff;
752                 num = mul(q, den);
753                 if (num < rats[k].num_min)
754                         continue;
755                 if (num > rats[k].num_max)
756                         num = rats[k].num_max;
757                 else {
758                         unsigned int r;
759                         r = (num - rats[k].num_min) % rats[k].num_step;
760                         if (r != 0)
761                                 num -= r;
762                 }
763                 diff = q * den - num;
764                 if (best_num == 0 ||
765                     diff * best_den < best_diff * den) {
766                         best_diff = diff;
767                         best_den = den;
768                         best_num = num;
769                 }
770         }
771         if (best_den == 0) {
772                 i->empty = 1;
773                 return -EINVAL;
774         }
775         t.max = div_up(best_num, best_den);
776         t.openmax = !!(best_num % best_den);
777         t.integer = 0;
778         err = snd_interval_refine(i, &t);
779         if (err < 0)
780                 return err;
781
782         if (snd_interval_single(i)) {
783                 if (nump)
784                         *nump = best_num;
785                 if (denp)
786                         *denp = best_den;
787         }
788         return err;
789 }
790
791 /**
792  * snd_interval_list - refine the interval value from the list
793  * @i: the interval value to refine
794  * @count: the number of elements in the list
795  * @list: the value list
796  * @mask: the bit-mask to evaluate
797  *
798  * Refines the interval value from the list.
799  * When mask is non-zero, only the elements corresponding to bit 1 are
800  * evaluated.
801  *
802  * Returns non-zero if the value is changed, zero if not changed.
803  */
804 int snd_interval_list(snd_interval_t *i, unsigned int count, unsigned int *list, unsigned int mask)
805 {
806         unsigned int k;
807         int changed = 0;
808         for (k = 0; k < count; k++) {
809                 if (mask && !(mask & (1 << k)))
810                         continue;
811                 if (i->min == list[k] && !i->openmin)
812                         goto _l1;
813                 if (i->min < list[k]) {
814                         i->min = list[k];
815                         i->openmin = 0;
816                         changed = 1;
817                         goto _l1;
818                 }
819         }
820         i->empty = 1;
821         return -EINVAL;
822  _l1:
823         for (k = count; k-- > 0;) {
824                 if (mask && !(mask & (1 << k)))
825                         continue;
826                 if (i->max == list[k] && !i->openmax)
827                         goto _l2;
828                 if (i->max > list[k]) {
829                         i->max = list[k];
830                         i->openmax = 0;
831                         changed = 1;
832                         goto _l2;
833                 }
834         }
835         i->empty = 1;
836         return -EINVAL;
837  _l2:
838         if (snd_interval_checkempty(i)) {
839                 i->empty = 1;
840                 return -EINVAL;
841         }
842         return changed;
843 }
844
845 int snd_interval_step(snd_interval_t *i, unsigned int min, unsigned int step)
846 {
847         unsigned int n;
848         int changed = 0;
849         n = (i->min - min) % step;
850         if (n != 0 || i->openmin) {
851                 i->min += step - n;
852                 changed = 1;
853         }
854         n = (i->max - min) % step;
855         if (n != 0 || i->openmax) {
856                 i->max -= n;
857                 changed = 1;
858         }
859         if (snd_interval_checkempty(i)) {
860                 i->empty = 1;
861                 return -EINVAL;
862         }
863         return changed;
864 }
865
866 /* Info constraints helpers */
867
868 /**
869  * snd_pcm_hw_rule_add - add the hw-constraint rule
870  * @runtime: the pcm runtime instance
871  * @cond: condition bits
872  * @var: the variable to evaluate
873  * @func: the evaluation function
874  * @private: the private data pointer passed to function
875  * @dep: the dependent variables
876  *
877  * Returns zero if successful, or a negative error code on failure.
878  */
879 int snd_pcm_hw_rule_add(snd_pcm_runtime_t *runtime, unsigned int cond,
880                         int var,
881                         snd_pcm_hw_rule_func_t func, void *private,
882                         int dep, ...)
883 {
884         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
885         snd_pcm_hw_rule_t *c;
886         unsigned int k;
887         va_list args;
888         va_start(args, dep);
889         if (constrs->rules_num >= constrs->rules_all) {
890                 snd_pcm_hw_rule_t *old = constrs->rules;
891                 if (constrs->rules_all == 0)
892                         constrs->rules_all = 32;
893                 else {
894                         old = constrs->rules;
895                         constrs->rules_all += 10;
896                 }
897                 constrs->rules = snd_kcalloc(constrs->rules_all * sizeof(*c),
898                                              GFP_KERNEL);
899                 if (!constrs->rules)
900                         return -ENOMEM;
901                 if (old) {
902                         memcpy(constrs->rules, old,
903                                constrs->rules_num * sizeof(*c));
904                         kfree(old);
905                 }
906         }
907         c = &constrs->rules[constrs->rules_num];
908         c->cond = cond;
909         c->func = func;
910         c->var = var;
911         c->private = private;
912         k = 0;
913         while (1) {
914                 snd_assert(k < sizeof(c->deps) / sizeof(c->deps[0]), return -EINVAL);
915                 c->deps[k++] = dep;
916                 if (dep < 0)
917                         break;
918                 dep = va_arg(args, int);
919         }
920         constrs->rules_num++;
921         va_end(args);
922         return 0;
923 }                                   
924
925 /**
926  * snd_pcm_hw_constraint_mask
927  */
928 int snd_pcm_hw_constraint_mask(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
929                                u_int32_t mask)
930 {
931         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
932         snd_mask_t *maskp = constrs_mask(constrs, var);
933         *maskp->bits &= mask;
934         memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
935         if (*maskp->bits == 0)
936                 return -EINVAL;
937         return 0;
938 }
939
940 /**
941  * snd_pcm_hw_constraint_mask64
942  */
943 int snd_pcm_hw_constraint_mask64(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
944                                  u_int64_t mask)
945 {
946         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
947         snd_mask_t *maskp = constrs_mask(constrs, var);
948         maskp->bits[0] &= (u_int32_t)mask;
949         maskp->bits[1] &= (u_int32_t)(mask >> 32);
950         memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
951         if (! maskp->bits[0] && ! maskp->bits[1])
952                 return -EINVAL;
953         return 0;
954 }
955
956 /**
957  * snd_pcm_hw_constraint_integer
958  */
959 int snd_pcm_hw_constraint_integer(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var)
960 {
961         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
962         return snd_interval_setinteger(constrs_interval(constrs, var));
963 }
964
965 /**
966  * snd_pcm_hw_constraint_minmax
967  */
968 int snd_pcm_hw_constraint_minmax(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
969                                  unsigned int min, unsigned int max)
970 {
971         snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
972         snd_interval_t t;
973         t.min = min;
974         t.max = max;
975         t.openmin = t.openmax = 0;
976         t.integer = 0;
977         return snd_interval_refine(constrs_interval(constrs, var), &t);
978 }
979
980 static int snd_pcm_hw_rule_list(snd_pcm_hw_params_t *params,
981                                 snd_pcm_hw_rule_t *rule)
982 {
983         snd_pcm_hw_constraint_list_t *list = rule->private;
984         return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
985 }               
986
987
988 /**
989  * snd_pcm_hw_constraint_list
990  */
991 int snd_pcm_hw_constraint_list(snd_pcm_runtime_t *runtime,
992                                unsigned int cond,
993                                snd_pcm_hw_param_t var,
994                                snd_pcm_hw_constraint_list_t *l)
995 {
996         return snd_pcm_hw_rule_add(runtime, cond, var,
997                                    snd_pcm_hw_rule_list, l,
998                                    var, -1);
999 }
1000
1001 static int snd_pcm_hw_rule_ratnums(snd_pcm_hw_params_t *params,
1002                                    snd_pcm_hw_rule_t *rule)
1003 {
1004         snd_pcm_hw_constraint_ratnums_t *r = rule->private;
1005         unsigned int num = 0, den = 0;
1006         int err;
1007         err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1008                                   r->nrats, r->rats, &num, &den);
1009         if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1010                 params->rate_num = num;
1011                 params->rate_den = den;
1012         }
1013         return err;
1014 }
1015
1016 /**
1017  * snd_pcm_hw_constraint_ratnums
1018  */
1019 int snd_pcm_hw_constraint_ratnums(snd_pcm_runtime_t *runtime, 
1020                                   unsigned int cond,
1021                                   snd_pcm_hw_param_t var,
1022                                   snd_pcm_hw_constraint_ratnums_t *r)
1023 {
1024         return snd_pcm_hw_rule_add(runtime, cond, var,
1025                                    snd_pcm_hw_rule_ratnums, r,
1026                                    var, -1);
1027 }
1028
1029 static int snd_pcm_hw_rule_ratdens(snd_pcm_hw_params_t *params,
1030                                    snd_pcm_hw_rule_t *rule)
1031 {
1032         snd_pcm_hw_constraint_ratdens_t *r = rule->private;
1033         unsigned int num = 0, den = 0;
1034         int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1035                                   r->nrats, r->rats, &num, &den);
1036         if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1037                 params->rate_num = num;
1038                 params->rate_den = den;
1039         }
1040         return err;
1041 }
1042
1043 /**
1044  * snd_pcm_hw_constraint_ratdens
1045  */
1046 int snd_pcm_hw_constraint_ratdens(snd_pcm_runtime_t *runtime, 
1047                                   unsigned int cond,
1048                                   snd_pcm_hw_param_t var,
1049                                   snd_pcm_hw_constraint_ratdens_t *r)
1050 {
1051         return snd_pcm_hw_rule_add(runtime, cond, var,
1052                                    snd_pcm_hw_rule_ratdens, r,
1053                                    var, -1);
1054 }
1055
1056 static int snd_pcm_hw_rule_msbits(snd_pcm_hw_params_t *params,
1057                                   snd_pcm_hw_rule_t *rule)
1058 {
1059         unsigned int l = (unsigned long) rule->private;
1060         int width = l & 0xffff;
1061         unsigned int msbits = l >> 16;
1062         snd_interval_t *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1063         if (snd_interval_single(i) && snd_interval_value(i) == width)
1064                 params->msbits = msbits;
1065         return 0;
1066 }
1067
1068 /**
1069  * snd_pcm_hw_constraint_msbits
1070  */
1071 int snd_pcm_hw_constraint_msbits(snd_pcm_runtime_t *runtime, 
1072                                  unsigned int cond,
1073                                  unsigned int width,
1074                                  unsigned int msbits)
1075 {
1076         unsigned long l = (msbits << 16) | width;
1077         return snd_pcm_hw_rule_add(runtime, cond, -1,
1078                                     snd_pcm_hw_rule_msbits,
1079                                     (void*) l,
1080                                     SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1081 }
1082
1083 static int snd_pcm_hw_rule_step(snd_pcm_hw_params_t *params,
1084                                 snd_pcm_hw_rule_t *rule)
1085 {
1086         unsigned long step = (unsigned long) rule->private;
1087         return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1088 }
1089
1090 /**
1091  * snd_pcm_hw_constraint_step
1092  */
1093 int snd_pcm_hw_constraint_step(snd_pcm_runtime_t *runtime,
1094                                unsigned int cond,
1095                                snd_pcm_hw_param_t var,
1096                                unsigned long step)
1097 {
1098         return snd_pcm_hw_rule_add(runtime, cond, var, 
1099                                    snd_pcm_hw_rule_step, (void *) step,
1100                                    var, -1);
1101 }
1102
1103 static int snd_pcm_hw_rule_pow2(snd_pcm_hw_params_t *params, snd_pcm_hw_rule_t *rule)
1104 {
1105         static int pow2_sizes[] = {
1106                 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1107                 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1108                 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1109                 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1110         };
1111         return snd_interval_list(hw_param_interval(params, rule->var),
1112                                  sizeof(pow2_sizes)/sizeof(int), pow2_sizes, 0);
1113 }               
1114
1115 /**
1116  * snd_pcm_hw_constraint_pow2
1117  */
1118 int snd_pcm_hw_constraint_pow2(snd_pcm_runtime_t *runtime,
1119                                unsigned int cond,
1120                                snd_pcm_hw_param_t var)
1121 {
1122         return snd_pcm_hw_rule_add(runtime, cond, var, 
1123                                    snd_pcm_hw_rule_pow2, NULL,
1124                                    var, -1);
1125 }
1126
1127 /* To use the same code we have in alsa-lib */
1128 #define snd_pcm_t snd_pcm_substream_t
1129 #define assert(i) snd_assert((i), return -EINVAL)
1130 #ifndef INT_MIN
1131 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1132 #endif
1133
1134 void _snd_pcm_hw_param_any(snd_pcm_hw_params_t *params, snd_pcm_hw_param_t var)
1135 {
1136         if (hw_is_mask(var)) {
1137                 snd_mask_any(hw_param_mask(params, var));
1138                 params->cmask |= 1 << var;
1139                 params->rmask |= 1 << var;
1140                 return;
1141         }
1142         if (hw_is_interval(var)) {
1143                 snd_interval_any(hw_param_interval(params, var));
1144                 params->cmask |= 1 << var;
1145                 params->rmask |= 1 << var;
1146                 return;
1147         }
1148         snd_BUG();
1149 }
1150
1151 /**
1152  * snd_pcm_hw_param_any
1153  */
1154 int snd_pcm_hw_param_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1155                          snd_pcm_hw_param_t var)
1156 {
1157         _snd_pcm_hw_param_any(params, var);
1158         return snd_pcm_hw_refine(pcm, params);
1159 }
1160
1161 void _snd_pcm_hw_params_any(snd_pcm_hw_params_t *params)
1162 {
1163         unsigned int k;
1164         memset(params, 0, sizeof(*params));
1165         for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1166                 _snd_pcm_hw_param_any(params, k);
1167         for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1168                 _snd_pcm_hw_param_any(params, k);
1169         params->info = ~0U;
1170 }
1171
1172 /**
1173  * snd_pcm_hw_params_any
1174  *
1175  * Fill PARAMS with full configuration space boundaries
1176  */
1177 int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
1178 {
1179         _snd_pcm_hw_params_any(params);
1180         return snd_pcm_hw_refine(pcm, params);
1181 }
1182
1183 /**
1184  * snd_pcm_hw_param_value
1185  *
1186  * Return the value for field PAR if it's fixed in configuration space 
1187  *  defined by PARAMS. Return -EINVAL otherwise
1188  */
1189 int snd_pcm_hw_param_value(const snd_pcm_hw_params_t *params,
1190                            snd_pcm_hw_param_t var, int *dir)
1191 {
1192         if (hw_is_mask(var)) {
1193                 const snd_mask_t *mask = hw_param_mask_c(params, var);
1194                 if (!snd_mask_single(mask))
1195                         return -EINVAL;
1196                 if (dir)
1197                         *dir = 0;
1198                 return snd_mask_value(mask);
1199         }
1200         if (hw_is_interval(var)) {
1201                 const snd_interval_t *i = hw_param_interval_c(params, var);
1202                 if (!snd_interval_single(i))
1203                         return -EINVAL;
1204                 if (dir)
1205                         *dir = i->openmin;
1206                 return snd_interval_value(i);
1207         }
1208         assert(0);
1209         return -EINVAL;
1210 }
1211
1212 /**
1213  * snd_pcm_hw_param_value_min
1214  *
1215  * Return the minimum value for field PAR.
1216  */
1217 unsigned int snd_pcm_hw_param_value_min(const snd_pcm_hw_params_t *params,
1218                                         snd_pcm_hw_param_t var, int *dir)
1219 {
1220         if (hw_is_mask(var)) {
1221                 if (dir)
1222                         *dir = 0;
1223                 return snd_mask_min(hw_param_mask_c(params, var));
1224         }
1225         if (hw_is_interval(var)) {
1226                 const snd_interval_t *i = hw_param_interval_c(params, var);
1227                 if (dir)
1228                         *dir = i->openmin;
1229                 return snd_interval_min(i);
1230         }
1231         assert(0);
1232         return -EINVAL;
1233 }
1234
1235 /**
1236  * snd_pcm_hw_param_value_max
1237  *
1238  * Return the maximum value for field PAR.
1239  */
1240 unsigned int snd_pcm_hw_param_value_max(const snd_pcm_hw_params_t *params,
1241                                         snd_pcm_hw_param_t var, int *dir)
1242 {
1243         if (hw_is_mask(var)) {
1244                 if (dir)
1245                         *dir = 0;
1246                 return snd_mask_max(hw_param_mask_c(params, var));
1247         }
1248         if (hw_is_interval(var)) {
1249                 const snd_interval_t *i = hw_param_interval_c(params, var);
1250                 if (dir)
1251                         *dir = - (int) i->openmax;
1252                 return snd_interval_max(i);
1253         }
1254         assert(0);
1255         return -EINVAL;
1256 }
1257
1258 void _snd_pcm_hw_param_setempty(snd_pcm_hw_params_t *params,
1259                                 snd_pcm_hw_param_t var)
1260 {
1261         if (hw_is_mask(var)) {
1262                 snd_mask_none(hw_param_mask(params, var));
1263                 params->cmask |= 1 << var;
1264                 params->rmask |= 1 << var;
1265         } else if (hw_is_interval(var)) {
1266                 snd_interval_none(hw_param_interval(params, var));
1267                 params->cmask |= 1 << var;
1268                 params->rmask |= 1 << var;
1269         } else {
1270                 snd_BUG();
1271         }
1272 }
1273
1274 int _snd_pcm_hw_param_setinteger(snd_pcm_hw_params_t *params,
1275                                  snd_pcm_hw_param_t var)
1276 {
1277         int changed;
1278         assert(hw_is_interval(var));
1279         changed = snd_interval_setinteger(hw_param_interval(params, var));
1280         if (changed) {
1281                 params->cmask |= 1 << var;
1282                 params->rmask |= 1 << var;
1283         }
1284         return changed;
1285 }
1286         
1287 /**
1288  * snd_pcm_hw_param_setinteger
1289  *
1290  * Inside configuration space defined by PARAMS remove from PAR all 
1291  * non integer values. Reduce configuration space accordingly.
1292  * Return -EINVAL if the configuration space is empty
1293  */
1294 int snd_pcm_hw_param_setinteger(snd_pcm_t *pcm, 
1295                                 snd_pcm_hw_params_t *params,
1296                                 snd_pcm_hw_param_t var)
1297 {
1298         int changed = _snd_pcm_hw_param_setinteger(params, var);
1299         if (changed < 0)
1300                 return changed;
1301         if (params->rmask) {
1302                 int err = snd_pcm_hw_refine(pcm, params);
1303                 if (err < 0)
1304                         return err;
1305         }
1306         return 0;
1307 }
1308
1309 int _snd_pcm_hw_param_first(snd_pcm_hw_params_t *params,
1310                             snd_pcm_hw_param_t var)
1311 {
1312         int changed;
1313         if (hw_is_mask(var))
1314                 changed = snd_mask_refine_first(hw_param_mask(params, var));
1315         else if (hw_is_interval(var))
1316                 changed = snd_interval_refine_first(hw_param_interval(params, var));
1317         else {
1318                 assert(0);
1319                 return -EINVAL;
1320         }
1321         if (changed) {
1322                 params->cmask |= 1 << var;
1323                 params->rmask |= 1 << var;
1324         }
1325         return changed;
1326 }
1327
1328
1329 /**
1330  * snd_pcm_hw_param_first
1331  *
1332  * Inside configuration space defined by PARAMS remove from PAR all 
1333  * values > minimum. Reduce configuration space accordingly.
1334  * Return the minimum.
1335  */
1336 int snd_pcm_hw_param_first(snd_pcm_t *pcm, 
1337                            snd_pcm_hw_params_t *params, 
1338                            snd_pcm_hw_param_t var, int *dir)
1339 {
1340         int changed = _snd_pcm_hw_param_first(params, var);
1341         if (changed < 0)
1342                 return changed;
1343         if (params->rmask) {
1344                 int err = snd_pcm_hw_refine(pcm, params);
1345                 assert(err >= 0);
1346         }
1347         return snd_pcm_hw_param_value(params, var, dir);
1348 }
1349
1350 int _snd_pcm_hw_param_last(snd_pcm_hw_params_t *params,
1351                            snd_pcm_hw_param_t var)
1352 {
1353         int changed;
1354         if (hw_is_mask(var))
1355                 changed = snd_mask_refine_last(hw_param_mask(params, var));
1356         else if (hw_is_interval(var))
1357                 changed = snd_interval_refine_last(hw_param_interval(params, var));
1358         else {
1359                 assert(0);
1360                 return -EINVAL;
1361         }
1362         if (changed) {
1363                 params->cmask |= 1 << var;
1364                 params->rmask |= 1 << var;
1365         }
1366         return changed;
1367 }
1368
1369
1370 /**
1371  * snd_pcm_hw_param_last
1372  *
1373  * Inside configuration space defined by PARAMS remove from PAR all 
1374  * values < maximum. Reduce configuration space accordingly.
1375  * Return the maximum.
1376  */
1377 int snd_pcm_hw_param_last(snd_pcm_t *pcm, 
1378                           snd_pcm_hw_params_t *params,
1379                           snd_pcm_hw_param_t var, int *dir)
1380 {
1381         int changed = _snd_pcm_hw_param_last(params, var);
1382         if (changed < 0)
1383                 return changed;
1384         if (params->rmask) {
1385                 int err = snd_pcm_hw_refine(pcm, params);
1386                 assert(err >= 0);
1387         }
1388         return snd_pcm_hw_param_value(params, var, dir);
1389 }
1390
1391 int _snd_pcm_hw_param_min(snd_pcm_hw_params_t *params,
1392                           snd_pcm_hw_param_t var, unsigned int val, int dir)
1393 {
1394         int changed;
1395         int open = 0;
1396         if (dir) {
1397                 if (dir > 0) {
1398                         open = 1;
1399                 } else if (dir < 0) {
1400                         if (val > 0) {
1401                                 open = 1;
1402                                 val--;
1403                         }
1404                 }
1405         }
1406         if (hw_is_mask(var))
1407                 changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open);
1408         else if (hw_is_interval(var))
1409                 changed = snd_interval_refine_min(hw_param_interval(params, var), val, open);
1410         else {
1411                 assert(0);
1412                 return -EINVAL;
1413         }
1414         if (changed) {
1415                 params->cmask |= 1 << var;
1416                 params->rmask |= 1 << var;
1417         }
1418         return changed;
1419 }
1420
1421 /**
1422  * snd_pcm_hw_param_min
1423  *
1424  * Inside configuration space defined by PARAMS remove from PAR all 
1425  * values < VAL. Reduce configuration space accordingly.
1426  * Return new minimum or -EINVAL if the configuration space is empty
1427  */
1428 int snd_pcm_hw_param_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1429                          snd_pcm_hw_param_t var, unsigned int val, int *dir)
1430 {
1431         int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
1432         if (changed < 0)
1433                 return changed;
1434         if (params->rmask) {
1435                 int err = snd_pcm_hw_refine(pcm, params);
1436                 if (err < 0)
1437                         return err;
1438         }
1439         return snd_pcm_hw_param_value_min(params, var, dir);
1440 }
1441
1442 int _snd_pcm_hw_param_max(snd_pcm_hw_params_t *params,
1443                            snd_pcm_hw_param_t var, unsigned int val, int dir)
1444 {
1445         int changed;
1446         int open = 0;
1447         if (dir) {
1448                 if (dir < 0) {
1449                         open = 1;
1450                 } else if (dir > 0) {
1451                         open = 1;
1452                         val++;
1453                 }
1454         }
1455         if (hw_is_mask(var)) {
1456                 if (val == 0 && open) {
1457                         snd_mask_none(hw_param_mask(params, var));
1458                         changed = -EINVAL;
1459                 } else
1460                         changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open);
1461         } else if (hw_is_interval(var))
1462                 changed = snd_interval_refine_max(hw_param_interval(params, var), val, open);
1463         else {
1464                 assert(0);
1465                 return -EINVAL;
1466         }
1467         if (changed) {
1468                 params->cmask |= 1 << var;
1469                 params->rmask |= 1 << var;
1470         }
1471         return changed;
1472 }
1473
1474 /**
1475  * snd_pcm_hw_param_max
1476  *
1477  * Inside configuration space defined by PARAMS remove from PAR all 
1478  *  values >= VAL + 1. Reduce configuration space accordingly.
1479  *  Return new maximum or -EINVAL if the configuration space is empty
1480  */
1481 int snd_pcm_hw_param_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1482                           snd_pcm_hw_param_t var, unsigned int val, int *dir)
1483 {
1484         int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
1485         if (changed < 0)
1486                 return changed;
1487         if (params->rmask) {
1488                 int err = snd_pcm_hw_refine(pcm, params);
1489                 if (err < 0)
1490                         return err;
1491         }
1492         return snd_pcm_hw_param_value_max(params, var, dir);
1493 }
1494
1495 int _snd_pcm_hw_param_set(snd_pcm_hw_params_t *params,
1496                           snd_pcm_hw_param_t var, unsigned int val, int dir)
1497 {
1498         int changed;
1499         if (hw_is_mask(var)) {
1500                 snd_mask_t *m = hw_param_mask(params, var);
1501                 if (val == 0 && dir < 0) {
1502                         changed = -EINVAL;
1503                         snd_mask_none(m);
1504                 } else {
1505                         if (dir > 0)
1506                                 val++;
1507                         else if (dir < 0)
1508                                 val--;
1509                         changed = snd_mask_refine_set(hw_param_mask(params, var), val);
1510                 }
1511         } else if (hw_is_interval(var)) {
1512                 snd_interval_t *i = hw_param_interval(params, var);
1513                 if (val == 0 && dir < 0) {
1514                         changed = -EINVAL;
1515                         snd_interval_none(i);
1516                 } else if (dir == 0)
1517                         changed = snd_interval_refine_set(i, val);
1518                 else {
1519                         snd_interval_t t;
1520                         t.openmin = 1;
1521                         t.openmax = 1;
1522                         t.empty = 0;
1523                         t.integer = 0;
1524                         if (dir < 0) {
1525                                 t.min = val - 1;
1526                                 t.max = val;
1527                         } else {
1528                                 t.min = val;
1529                                 t.max = val+1;
1530                         }
1531                         changed = snd_interval_refine(i, &t);
1532                 }
1533         } else {
1534                 assert(0);
1535                 return -EINVAL;
1536         }
1537         if (changed) {
1538                 params->cmask |= 1 << var;
1539                 params->rmask |= 1 << var;
1540         }
1541         return changed;
1542 }
1543
1544 /**
1545  * snd_pcm_hw_param_set
1546  *
1547  * Inside configuration space defined by PARAMS remove from PAR all 
1548  * values != VAL. Reduce configuration space accordingly.
1549  *  Return VAL or -EINVAL if the configuration space is empty
1550  */
1551 int snd_pcm_hw_param_set(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1552                          snd_pcm_hw_param_t var, unsigned int val, int dir)
1553 {
1554         int changed = _snd_pcm_hw_param_set(params, var, val, dir);
1555         if (changed < 0)
1556                 return changed;
1557         if (params->rmask) {
1558                 int err = snd_pcm_hw_refine(pcm, params);
1559                 if (err < 0)
1560                         return err;
1561         }
1562         return snd_pcm_hw_param_value(params, var, 0);
1563 }
1564
1565 int _snd_pcm_hw_param_mask(snd_pcm_hw_params_t *params,
1566                            snd_pcm_hw_param_t var, const snd_mask_t *val)
1567 {
1568         int changed;
1569         assert(hw_is_mask(var));
1570         changed = snd_mask_refine(hw_param_mask(params, var), val);
1571         if (changed) {
1572                 params->cmask |= 1 << var;
1573                 params->rmask |= 1 << var;
1574         }
1575         return changed;
1576 }
1577
1578 /**
1579  * snd_pcm_hw_param_mask
1580  *
1581  * Inside configuration space defined by PARAMS remove from PAR all values
1582  * not contained in MASK. Reduce configuration space accordingly.
1583  * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
1584  * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1585  * Return 0 on success or -EINVAL
1586  * if the configuration space is empty
1587  */
1588 int snd_pcm_hw_param_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1589                           snd_pcm_hw_param_t var, const snd_mask_t *val)
1590 {
1591         int changed = _snd_pcm_hw_param_mask(params, var, val);
1592         if (changed < 0)
1593                 return changed;
1594         if (params->rmask) {
1595                 int err = snd_pcm_hw_refine(pcm, params);
1596                 if (err < 0)
1597                         return err;
1598         }
1599         return 0;
1600 }
1601
1602 static int boundary_sub(int a, int adir,
1603                         int b, int bdir,
1604                         int *c, int *cdir)
1605 {
1606         adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
1607         bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
1608         *c = a - b;
1609         *cdir = adir - bdir;
1610         if (*cdir == -2) {
1611                 assert(*c > INT_MIN);
1612                 (*c)--;
1613         } else if (*cdir == 2) {
1614                 assert(*c < INT_MAX);
1615                 (*c)++;
1616         }
1617         return 0;
1618 }
1619
1620 static int boundary_lt(unsigned int a, int adir,
1621                        unsigned int b, int bdir)
1622 {
1623         assert(a > 0 || adir >= 0);
1624         assert(b > 0 || bdir >= 0);
1625         if (adir < 0) {
1626                 a--;
1627                 adir = 1;
1628         } else if (adir > 0)
1629                 adir = 1;
1630         if (bdir < 0) {
1631                 b--;
1632                 bdir = 1;
1633         } else if (bdir > 0)
1634                 bdir = 1;
1635         return a < b || (a == b && adir < bdir);
1636 }
1637
1638 /* Return 1 if min is nearer to best than max */
1639 static int boundary_nearer(int min, int mindir,
1640                            int best, int bestdir,
1641                            int max, int maxdir)
1642 {
1643         int dmin, dmindir;
1644         int dmax, dmaxdir;
1645         boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
1646         boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
1647         return boundary_lt(dmin, dmindir, dmax, dmaxdir);
1648 }
1649
1650 /**
1651  * snd_pcm_hw_param_near
1652  *
1653  * Inside configuration space defined by PARAMS set PAR to the available value
1654  * nearest to VAL. Reduce configuration space accordingly.
1655  * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
1656  * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1657  * Return the value found.
1658   */
1659 int snd_pcm_hw_param_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1660                           snd_pcm_hw_param_t var, unsigned int best, int *dir)
1661 {
1662         snd_pcm_hw_params_t *save = NULL;
1663         int v;
1664         unsigned int saved_min;
1665         int last = 0;
1666         int min, max;
1667         int mindir, maxdir;
1668         int valdir = dir ? *dir : 0;
1669         /* FIXME */
1670         if (best > INT_MAX)
1671                 best = INT_MAX;
1672         min = max = best;
1673         mindir = maxdir = valdir;
1674         if (maxdir > 0)
1675                 maxdir = 0;
1676         else if (maxdir == 0)
1677                 maxdir = -1;
1678         else {
1679                 maxdir = 1;
1680                 max--;
1681         }
1682         save = kmalloc(sizeof(*save), GFP_KERNEL);
1683         if (save == NULL)
1684                 return -ENOMEM;
1685         *save = *params;
1686         saved_min = min;
1687         min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
1688         if (min >= 0) {
1689                 snd_pcm_hw_params_t *params1;
1690                 if (max < 0)
1691                         goto _end;
1692                 if ((unsigned int)min == saved_min && mindir == valdir)
1693                         goto _end;
1694                 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
1695                 if (params1 == NULL) {
1696                         kfree(save);
1697                         return -ENOMEM;
1698                 }
1699                 *params1 = *save;
1700                 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
1701                 if (max < 0) {
1702                         kfree(params1);
1703                         goto _end;
1704                 }
1705                 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
1706                         *params = *params1;
1707                         last = 1;
1708                 }
1709                 kfree(params1);
1710         } else {
1711                 *params = *save;
1712                 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
1713                 assert(max >= 0);
1714                 last = 1;
1715         }
1716  _end:
1717         kfree(save);
1718         if (last)
1719                 v = snd_pcm_hw_param_last(pcm, params, var, dir);
1720         else
1721                 v = snd_pcm_hw_param_first(pcm, params, var, dir);
1722         assert(v >= 0);
1723         return v;
1724 }
1725
1726 /**
1727  * snd_pcm_hw_param_choose
1728  *
1729  * Choose one configuration from configuration space defined by PARAMS
1730  * The configuration chosen is that obtained fixing in this order:
1731  * first access, first format, first subformat, min channels,
1732  * min rate, min period time, max buffer size, min tick time
1733  */
1734 int snd_pcm_hw_params_choose(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
1735 {
1736         int err;
1737
1738         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_ACCESS, 0);
1739         assert(err >= 0);
1740
1741         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_FORMAT, 0);
1742         assert(err >= 0);
1743
1744         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_SUBFORMAT, 0);
1745         assert(err >= 0);
1746
1747         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_CHANNELS, 0);
1748         assert(err >= 0);
1749
1750         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_RATE, 0);
1751         assert(err >= 0);
1752
1753         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 0);
1754         assert(err >= 0);
1755
1756         err = snd_pcm_hw_param_last(pcm, params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0);
1757         assert(err >= 0);
1758
1759         err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_TICK_TIME, 0);
1760         assert(err >= 0);
1761
1762         return 0;
1763 }
1764
1765 #undef snd_pcm_t
1766 #undef assert
1767
1768 static int snd_pcm_lib_ioctl_reset(snd_pcm_substream_t *substream,
1769                                    void *arg)
1770 {
1771         snd_pcm_runtime_t *runtime = substream->runtime;
1772         if (snd_pcm_running(substream) &&
1773             snd_pcm_update_hw_ptr(substream) >= 0) {
1774                 runtime->status->hw_ptr %= runtime->buffer_size;
1775                 return 0;
1776         }
1777         runtime->status->hw_ptr = 0;
1778         return 0;
1779 }
1780
1781 static int snd_pcm_lib_ioctl_channel_info(snd_pcm_substream_t *substream,
1782                                           void *arg)
1783 {
1784         snd_pcm_channel_info_t *info = arg;
1785         snd_pcm_runtime_t *runtime = substream->runtime;
1786         int width;
1787         if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1788                 info->offset = -1;
1789                 return 0;
1790         }
1791         width = snd_pcm_format_physical_width(runtime->format);
1792         if (width < 0)
1793                 return width;
1794         info->offset = 0;
1795         switch (runtime->access) {
1796         case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1797         case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1798                 info->first = info->channel * width;
1799                 info->step = runtime->channels * width;
1800                 break;
1801         case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1802         case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1803         {
1804                 size_t size = runtime->dma_bytes / runtime->channels;
1805                 info->first = info->channel * size * 8;
1806                 info->step = width;
1807                 break;
1808         }
1809         default:
1810                 snd_BUG();
1811                 break;
1812         }
1813         return 0;
1814 }
1815
1816 /**
1817  * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1818  * @substream: the pcm substream instance
1819  * @cmd: ioctl command
1820  * @arg: ioctl argument
1821  *
1822  * Processes the generic ioctl commands for PCM.
1823  * Can be passed as the ioctl callback for PCM ops.
1824  *
1825  * Returns zero if successful, or a negative error code on failure.
1826  */
1827 int snd_pcm_lib_ioctl(snd_pcm_substream_t *substream,
1828                       unsigned int cmd, void *arg)
1829 {
1830         switch (cmd) {
1831         case SNDRV_PCM_IOCTL1_INFO:
1832                 return 0;
1833         case SNDRV_PCM_IOCTL1_RESET:
1834                 return snd_pcm_lib_ioctl_reset(substream, arg);
1835         case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1836                 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1837         }
1838         return -ENXIO;
1839 }
1840
1841 /*
1842  *  Conditions
1843  */
1844
1845 /**
1846  * snd_pcm_playback_ready - check whether the playback buffer is available
1847  * @substream: the pcm substream instance
1848  *
1849  * Checks whether enough free space is available on the playback buffer.
1850  *
1851  * Returns non-zero if available, or zero if not.
1852  */
1853 int snd_pcm_playback_ready(snd_pcm_substream_t *substream)
1854 {
1855         snd_pcm_runtime_t *runtime = substream->runtime;
1856         return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
1857 }
1858
1859 /**
1860  * snd_pcm_capture_ready - check whether the capture buffer is available
1861  * @substream: the pcm substream instance
1862  *
1863  * Checks whether enough capture data is available on the capture buffer.
1864  *
1865  * Returns non-zero if available, or zero if not.
1866  */
1867 int snd_pcm_capture_ready(snd_pcm_substream_t *substream)
1868 {
1869         snd_pcm_runtime_t *runtime = substream->runtime;
1870         return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
1871 }
1872
1873 /**
1874  * snd_pcm_playback_data - check whether any data exists on the playback buffer
1875  * @substream: the pcm substream instance
1876  *
1877  * Checks whether any data exists on the playback buffer. If stop_threshold
1878  * is bigger or equal to boundary, then this function returns always non-zero.
1879  *
1880  * Returns non-zero if exists, or zero if not.
1881  */
1882 int snd_pcm_playback_data(snd_pcm_substream_t *substream)
1883 {
1884         snd_pcm_runtime_t *runtime = substream->runtime;
1885         
1886         if (runtime->stop_threshold >= runtime->boundary)
1887                 return 1;
1888         return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
1889 }
1890
1891 /**
1892  * snd_pcm_playback_empty - check whether the playback buffer is empty
1893  * @substream: the pcm substream instance
1894  *
1895  * Checks whether the playback buffer is empty.
1896  *
1897  * Returns non-zero if empty, or zero if not.
1898  */
1899 int snd_pcm_playback_empty(snd_pcm_substream_t *substream)
1900 {
1901         snd_pcm_runtime_t *runtime = substream->runtime;
1902         return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
1903 }
1904
1905 /**
1906  * snd_pcm_capture_empty - check whether the capture buffer is empty
1907  * @substream: the pcm substream instance
1908  *
1909  * Checks whether the capture buffer is empty.
1910  *
1911  * Returns non-zero if empty, or zero if not.
1912  */
1913 int snd_pcm_capture_empty(snd_pcm_substream_t *substream)
1914 {
1915         snd_pcm_runtime_t *runtime = substream->runtime;
1916         return snd_pcm_capture_avail(runtime) == 0;
1917 }
1918
1919 static void snd_pcm_system_tick_set(snd_pcm_substream_t *substream, 
1920                                     unsigned long ticks)
1921 {
1922         snd_pcm_runtime_t *runtime = substream->runtime;
1923         if (ticks == 0)
1924                 del_timer(&runtime->tick_timer);
1925         else {
1926                 ticks /= (1000000 / HZ);
1927                 if (ticks % (1000000 / HZ))
1928                         ticks++;
1929                 mod_timer(&runtime->tick_timer, jiffies + ticks);
1930         }
1931 }
1932
1933 /* Temporary alias */
1934 void snd_pcm_tick_set(snd_pcm_substream_t *substream, unsigned long ticks)
1935 {
1936         snd_pcm_system_tick_set(substream, ticks);
1937 }
1938
1939 void snd_pcm_tick_prepare(snd_pcm_substream_t *substream)
1940 {
1941         snd_pcm_runtime_t *runtime = substream->runtime;
1942         snd_pcm_uframes_t frames = ULONG_MAX;
1943         snd_pcm_uframes_t avail, dist;
1944         unsigned int ticks;
1945         u_int64_t n;
1946         u_int32_t r;
1947         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1948                 if (runtime->silence_size >= runtime->boundary) {
1949                         frames = 1;
1950                 } else if (runtime->silence_size > 0 &&
1951                            runtime->silence_filled < runtime->buffer_size) {
1952                         snd_pcm_sframes_t noise_dist;
1953                         noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
1954                         snd_assert(noise_dist <= (snd_pcm_sframes_t)runtime->silence_threshold, );
1955                         frames = noise_dist - runtime->silence_threshold;
1956                 }
1957                 avail = snd_pcm_playback_avail(runtime);
1958         } else {
1959                 avail = snd_pcm_capture_avail(runtime);
1960         }
1961         if (avail < runtime->control->avail_min) {
1962                 snd_pcm_sframes_t n = runtime->control->avail_min - avail;
1963                 if (n > 0 && frames > (snd_pcm_uframes_t)n)
1964                         frames = n;
1965         }
1966         if (avail < runtime->buffer_size) {
1967                 snd_pcm_sframes_t n = runtime->buffer_size - avail;
1968                 if (n > 0 && frames > (snd_pcm_uframes_t)n)
1969                         frames = n;
1970         }
1971         if (frames == ULONG_MAX) {
1972                 snd_pcm_tick_set(substream, 0);
1973                 return;
1974         }
1975         dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
1976         /* Distance to next interrupt */
1977         dist = runtime->period_size - dist % runtime->period_size;
1978         if (dist <= frames) {
1979                 snd_pcm_tick_set(substream, 0);
1980                 return;
1981         }
1982         /* the base time is us */
1983         n = frames;
1984         n *= 1000000;
1985         div64_32(&n, runtime->tick_time * runtime->rate, &r);
1986         ticks = n + (r > 0 ? 1 : 0);
1987         if (ticks < runtime->sleep_min)
1988                 ticks = runtime->sleep_min;
1989         snd_pcm_tick_set(substream, (unsigned long) ticks);
1990 }
1991
1992 void snd_pcm_tick_elapsed(snd_pcm_substream_t *substream)
1993 {
1994         snd_pcm_runtime_t *runtime;
1995         unsigned long flags;
1996         
1997         snd_assert(substream != NULL, return);
1998         runtime = substream->runtime;
1999         snd_assert(runtime != NULL, return);
2000
2001         snd_pcm_stream_lock_irqsave(substream, flags);
2002         if (!snd_pcm_running(substream) ||
2003             snd_pcm_update_hw_ptr(substream) < 0)
2004                 goto _end;
2005         if (runtime->sleep_min)
2006                 snd_pcm_tick_prepare(substream);
2007  _end:
2008         snd_pcm_stream_unlock_irqrestore(substream, flags);
2009 }
2010
2011 /**
2012  * snd_pcm_period_elapsed - update the pcm status for the next period
2013  * @substream: the pcm substream instance
2014  *
2015  * This function is called from the interrupt handler when the
2016  * PCM has processed the period size.  It will update the current
2017  * pointer, set up the tick, wake up sleepers, etc.
2018  *
2019  * Even if more than one periods have elapsed since the last call, you
2020  * have to call this only once.
2021  */
2022 void snd_pcm_period_elapsed(snd_pcm_substream_t *substream)
2023 {
2024         snd_pcm_runtime_t *runtime;
2025         unsigned long flags;
2026
2027         snd_assert(substream != NULL, return);
2028         runtime = substream->runtime;
2029         snd_assert(runtime != NULL, return);
2030
2031         if (runtime->transfer_ack_begin)
2032                 runtime->transfer_ack_begin(substream);
2033
2034         snd_pcm_stream_lock_irqsave(substream, flags);
2035         if (!snd_pcm_running(substream) ||
2036             snd_pcm_update_hw_ptr_interrupt(substream) < 0)
2037                 goto _end;
2038
2039         if (substream->timer_running)
2040                 snd_timer_interrupt(substream->timer, 1);
2041         if (runtime->sleep_min)
2042                 snd_pcm_tick_prepare(substream);
2043  _end:
2044         snd_pcm_stream_unlock_irqrestore(substream, flags);
2045         if (runtime->transfer_ack_end)
2046                 runtime->transfer_ack_end(substream);
2047         kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
2048 }
2049
2050 static int snd_pcm_lib_write_transfer(snd_pcm_substream_t *substream,
2051                                       unsigned int hwoff,
2052                                       unsigned long data, unsigned int off,
2053                                       snd_pcm_uframes_t frames)
2054 {
2055         snd_pcm_runtime_t *runtime = substream->runtime;
2056         int err;
2057         char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2058         if (substream->ops->copy) {
2059                 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2060                         return err;
2061         } else {
2062                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2063                 snd_assert(runtime->dma_area, return -EFAULT);
2064                 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2065                         return -EFAULT;
2066         }
2067         return 0;
2068 }
2069  
2070 typedef int (*transfer_f)(snd_pcm_substream_t *substream, unsigned int hwoff,
2071                           unsigned long data, unsigned int off,
2072                           snd_pcm_uframes_t size);
2073
2074 static snd_pcm_sframes_t snd_pcm_lib_write1(snd_pcm_substream_t *substream, 
2075                                             unsigned long data,
2076                                             snd_pcm_uframes_t size,
2077                                             int nonblock,
2078                                             transfer_f transfer)
2079 {
2080         snd_pcm_runtime_t *runtime = substream->runtime;
2081         snd_pcm_uframes_t xfer = 0;
2082         snd_pcm_uframes_t offset = 0;
2083         int err = 0;
2084
2085         if (size == 0)
2086                 return 0;
2087         if (size > runtime->xfer_align)
2088                 size -= size % runtime->xfer_align;
2089
2090         snd_pcm_stream_lock_irq(substream);
2091         switch (runtime->status->state) {
2092         case SNDRV_PCM_STATE_PREPARED:
2093         case SNDRV_PCM_STATE_RUNNING:
2094         case SNDRV_PCM_STATE_PAUSED:
2095                 break;
2096         case SNDRV_PCM_STATE_XRUN:
2097                 err = -EPIPE;
2098                 goto _end_unlock;
2099         case SNDRV_PCM_STATE_SUSPENDED:
2100                 err = -ESTRPIPE;
2101                 goto _end_unlock;
2102         default:
2103                 err = -EBADFD;
2104                 goto _end_unlock;
2105         }
2106
2107         while (size > 0) {
2108                 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2109                 snd_pcm_uframes_t avail;
2110                 snd_pcm_uframes_t cont;
2111                 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2112                         snd_pcm_update_hw_ptr(substream);
2113                 avail = snd_pcm_playback_avail(runtime);
2114                 if (((avail < runtime->control->avail_min && size > avail) ||
2115                    (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
2116                         wait_queue_t wait;
2117                         enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED } state;
2118                         long tout;
2119
2120                         if (nonblock) {
2121                                 err = -EAGAIN;
2122                                 goto _end_unlock;
2123                         }
2124
2125                         init_waitqueue_entry(&wait, current);
2126                         add_wait_queue(&runtime->sleep, &wait);
2127                         while (1) {
2128                                 if (signal_pending(current)) {
2129                                         state = SIGNALED;
2130                                         break;
2131                                 }
2132                                 set_current_state(TASK_INTERRUPTIBLE);
2133                                 snd_pcm_stream_unlock_irq(substream);
2134                                 tout = schedule_timeout(10 * HZ);
2135                                 snd_pcm_stream_lock_irq(substream);
2136                                 if (tout == 0) {
2137                                         if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2138                                             runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2139                                                 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2140                                                 break;
2141                                         }
2142                                 }
2143                                 switch (runtime->status->state) {
2144                                 case SNDRV_PCM_STATE_XRUN:
2145                                 case SNDRV_PCM_STATE_DRAINING:
2146                                         state = ERROR;
2147                                         goto _end_loop;
2148                                 case SNDRV_PCM_STATE_SUSPENDED:
2149                                         state = SUSPENDED;
2150                                         goto _end_loop;
2151                                 default:
2152                                         break;
2153                                 }
2154                                 avail = snd_pcm_playback_avail(runtime);
2155                                 if (avail >= runtime->control->avail_min) {
2156                                         state = READY;
2157                                         break;
2158                                 }
2159                         }
2160                        _end_loop:
2161                         remove_wait_queue(&runtime->sleep, &wait);
2162
2163                         switch (state) {
2164                         case ERROR:
2165                                 err = -EPIPE;
2166                                 goto _end_unlock;
2167                         case SUSPENDED:
2168                                 err = -ESTRPIPE;
2169                                 goto _end_unlock;
2170                         case SIGNALED:
2171                                 err = -ERESTARTSYS;
2172                                 goto _end_unlock;
2173                         case EXPIRED:
2174                                 snd_printd("playback write error (DMA or IRQ trouble?)\n");
2175                                 err = -EIO;
2176                                 goto _end_unlock;
2177                         default:
2178                                 break;
2179                         }
2180                 }
2181                 if (avail > runtime->xfer_align)
2182                         avail -= avail % runtime->xfer_align;
2183                 frames = size > avail ? avail : size;
2184                 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2185                 if (frames > cont)
2186                         frames = cont;
2187                 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2188                 appl_ptr = runtime->control->appl_ptr;
2189                 appl_ofs = appl_ptr % runtime->buffer_size;
2190                 snd_pcm_stream_unlock_irq(substream);
2191                 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2192                         goto _end;
2193                 snd_pcm_stream_lock_irq(substream);
2194                 switch (runtime->status->state) {
2195                 case SNDRV_PCM_STATE_XRUN:
2196                         err = -EPIPE;
2197                         goto _end_unlock;
2198                 case SNDRV_PCM_STATE_SUSPENDED:
2199                         err = -ESTRPIPE;
2200                         goto _end_unlock;
2201                 default:
2202                         break;
2203                 }
2204                 appl_ptr += frames;
2205                 if (appl_ptr >= runtime->boundary) {
2206                         runtime->control->appl_ptr = 0;
2207                 } else {
2208                         runtime->control->appl_ptr = appl_ptr;
2209                 }
2210                 if (substream->ops->ack)
2211                         substream->ops->ack(substream);
2212
2213                 offset += frames;
2214                 size -= frames;
2215                 xfer += frames;
2216                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2217                     snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2218                         err = snd_pcm_start(substream);
2219                         if (err < 0)
2220                                 goto _end_unlock;
2221                 }
2222                 if (runtime->sleep_min &&
2223                     runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2224                         snd_pcm_tick_prepare(substream);
2225         }
2226  _end_unlock:
2227         snd_pcm_stream_unlock_irq(substream);
2228  _end:
2229         return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2230 }
2231
2232 snd_pcm_sframes_t snd_pcm_lib_write(snd_pcm_substream_t *substream, const void __user *buf, snd_pcm_uframes_t size)
2233 {
2234         snd_pcm_runtime_t *runtime;
2235         int nonblock;
2236
2237         snd_assert(substream != NULL, return -ENXIO);
2238         runtime = substream->runtime;
2239         snd_assert(runtime != NULL, return -ENXIO);
2240         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2241         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2242                 return -EBADFD;
2243
2244         snd_assert(substream->ffile != NULL, return -ENXIO);
2245         nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2246 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2247         if (substream->oss.oss) {
2248                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
2249                 if (setup != NULL) {
2250                         if (setup->nonblock)
2251                                 nonblock = 1;
2252                         else if (setup->block)
2253                                 nonblock = 0;
2254                 }
2255         }
2256 #endif
2257
2258         if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2259             runtime->channels > 1)
2260                 return -EINVAL;
2261         return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2262                                   snd_pcm_lib_write_transfer);
2263 }
2264
2265 static int snd_pcm_lib_writev_transfer(snd_pcm_substream_t *substream,
2266                                        unsigned int hwoff,
2267                                        unsigned long data, unsigned int off,
2268                                        snd_pcm_uframes_t frames)
2269 {
2270         snd_pcm_runtime_t *runtime = substream->runtime;
2271         int err;
2272         void __user **bufs = (void __user **)data;
2273         int channels = runtime->channels;
2274         int c;
2275         if (substream->ops->copy) {
2276                 snd_assert(substream->ops->silence != NULL, return -EINVAL);
2277                 for (c = 0; c < channels; ++c, ++bufs) {
2278                         if (*bufs == NULL) {
2279                                 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2280                                         return err;
2281                         } else {
2282                                 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2283                                 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2284                                         return err;
2285                         }
2286                 }
2287         } else {
2288                 /* default transfer behaviour */
2289                 size_t dma_csize = runtime->dma_bytes / channels;
2290                 snd_assert(runtime->dma_area, return -EFAULT);
2291                 for (c = 0; c < channels; ++c, ++bufs) {
2292                         char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2293                         if (*bufs == NULL) {
2294                                 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2295                         } else {
2296                                 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2297                                 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2298                                         return -EFAULT;
2299                         }
2300                 }
2301         }
2302         return 0;
2303 }
2304  
2305 snd_pcm_sframes_t snd_pcm_lib_writev(snd_pcm_substream_t *substream,
2306                                      void __user **bufs,
2307                                      snd_pcm_uframes_t frames)
2308 {
2309         snd_pcm_runtime_t *runtime;
2310         int nonblock;
2311
2312         snd_assert(substream != NULL, return -ENXIO);
2313         runtime = substream->runtime;
2314         snd_assert(runtime != NULL, return -ENXIO);
2315         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2316         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2317                 return -EBADFD;
2318
2319         snd_assert(substream->ffile != NULL, return -ENXIO);
2320         nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2321 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2322         if (substream->oss.oss) {
2323                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
2324                 if (setup != NULL) {
2325                         if (setup->nonblock)
2326                                 nonblock = 1;
2327                         else if (setup->block)
2328                                 nonblock = 0;
2329                 }
2330         }
2331 #endif
2332
2333         if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2334                 return -EINVAL;
2335         return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2336                                   nonblock, snd_pcm_lib_writev_transfer);
2337 }
2338
2339 static int snd_pcm_lib_read_transfer(snd_pcm_substream_t *substream, 
2340                                      unsigned int hwoff,
2341                                      unsigned long data, unsigned int off,
2342                                      snd_pcm_uframes_t frames)
2343 {
2344         snd_pcm_runtime_t *runtime = substream->runtime;
2345         int err;
2346         char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2347         if (substream->ops->copy) {
2348                 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2349                         return err;
2350         } else {
2351                 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2352                 snd_assert(runtime->dma_area, return -EFAULT);
2353                 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2354                         return -EFAULT;
2355         }
2356         return 0;
2357 }
2358
2359 static snd_pcm_sframes_t snd_pcm_lib_read1(snd_pcm_substream_t *substream,
2360                                            unsigned long data,
2361                                            snd_pcm_uframes_t size,
2362                                            int nonblock,
2363                                            transfer_f transfer)
2364 {
2365         snd_pcm_runtime_t *runtime = substream->runtime;
2366         snd_pcm_uframes_t xfer = 0;
2367         snd_pcm_uframes_t offset = 0;
2368         int err = 0;
2369
2370         if (size == 0)
2371                 return 0;
2372         if (size > runtime->xfer_align)
2373                 size -= size % runtime->xfer_align;
2374
2375         snd_pcm_stream_lock_irq(substream);
2376         switch (runtime->status->state) {
2377         case SNDRV_PCM_STATE_PREPARED:
2378                 if (size >= runtime->start_threshold) {
2379                         err = snd_pcm_start(substream);
2380                         if (err < 0)
2381                                 goto _end_unlock;
2382                 }
2383                 break;
2384         case SNDRV_PCM_STATE_DRAINING:
2385         case SNDRV_PCM_STATE_RUNNING:
2386         case SNDRV_PCM_STATE_PAUSED:
2387                 break;
2388         case SNDRV_PCM_STATE_XRUN:
2389                 err = -EPIPE;
2390                 goto _end_unlock;
2391         case SNDRV_PCM_STATE_SUSPENDED:
2392                 err = -ESTRPIPE;
2393                 goto _end_unlock;
2394         default:
2395                 err = -EBADFD;
2396                 goto _end_unlock;
2397         }
2398
2399         while (size > 0) {
2400                 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2401                 snd_pcm_uframes_t avail;
2402                 snd_pcm_uframes_t cont;
2403                 if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2404                         snd_pcm_update_hw_ptr(substream);
2405               __draining:
2406                 avail = snd_pcm_capture_avail(runtime);
2407                 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2408                         if (avail < runtime->xfer_align) {
2409                                 err = -EPIPE;
2410                                 goto _end_unlock;
2411                         }
2412                 } else if ((avail < runtime->control->avail_min && size > avail) ||
2413                            (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
2414                         wait_queue_t wait;
2415                         enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED } state;
2416                         long tout;
2417
2418                         if (nonblock) {
2419                                 err = -EAGAIN;
2420                                 goto _end_unlock;
2421                         }
2422
2423                         init_waitqueue_entry(&wait, current);
2424                         add_wait_queue(&runtime->sleep, &wait);
2425                         while (1) {
2426                                 if (signal_pending(current)) {
2427                                         state = SIGNALED;
2428                                         break;
2429                                 }
2430                                 set_current_state(TASK_INTERRUPTIBLE);
2431                                 snd_pcm_stream_unlock_irq(substream);
2432                                 tout = schedule_timeout(10 * HZ);
2433                                 snd_pcm_stream_lock_irq(substream);
2434                                 if (tout == 0) {
2435                                         if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2436                                             runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2437                                                 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2438                                                 break;
2439                                         }
2440                                 }
2441                                 switch (runtime->status->state) {
2442                                 case SNDRV_PCM_STATE_XRUN:
2443                                         state = ERROR;
2444                                         goto _end_loop;
2445                                 case SNDRV_PCM_STATE_SUSPENDED:
2446                                         state = SUSPENDED;
2447                                         goto _end_loop;
2448                                 case SNDRV_PCM_STATE_DRAINING:
2449                                         goto __draining;
2450                                 default:
2451                                         break;
2452                                 }
2453                                 avail = snd_pcm_capture_avail(runtime);
2454                                 if (avail >= runtime->control->avail_min) {
2455                                         state = READY;
2456                                         break;
2457                                 }
2458                         }
2459                        _end_loop:
2460                         remove_wait_queue(&runtime->sleep, &wait);
2461
2462                         switch (state) {
2463                         case ERROR:
2464                                 err = -EPIPE;
2465                                 goto _end_unlock;
2466                         case SUSPENDED:
2467                                 err = -ESTRPIPE;
2468                                 goto _end_unlock;
2469                         case SIGNALED:
2470                                 err = -ERESTARTSYS;
2471                                 goto _end_unlock;
2472                         case EXPIRED:
2473                                 snd_printd("capture read error (DMA or IRQ trouble?)\n");
2474                                 err = -EIO;
2475                                 goto _end_unlock;
2476                         default:
2477                                 break;
2478                         }
2479                 }
2480                 if (avail > runtime->xfer_align)
2481                         avail -= avail % runtime->xfer_align;
2482                 frames = size > avail ? avail : size;
2483                 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2484                 if (frames > cont)
2485                         frames = cont;
2486                 snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2487                 appl_ptr = runtime->control->appl_ptr;
2488                 appl_ofs = appl_ptr % runtime->buffer_size;
2489                 snd_pcm_stream_unlock_irq(substream);
2490                 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2491                         goto _end;
2492                 snd_pcm_stream_lock_irq(substream);
2493                 switch (runtime->status->state) {
2494                 case SNDRV_PCM_STATE_XRUN:
2495                         err = -EPIPE;
2496                         goto _end_unlock;
2497                 case SNDRV_PCM_STATE_SUSPENDED:
2498                         err = -ESTRPIPE;
2499                         goto _end_unlock;
2500                 default:
2501                         break;
2502                 }
2503                 appl_ptr += frames;
2504                 if (appl_ptr >= runtime->boundary) {
2505                         runtime->control->appl_ptr = 0;
2506                 } else {
2507                         runtime->control->appl_ptr = appl_ptr;
2508                 }
2509                 if (substream->ops->ack)
2510                         substream->ops->ack(substream);
2511
2512                 offset += frames;
2513                 size -= frames;
2514                 xfer += frames;
2515                 if (runtime->sleep_min &&
2516                     runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2517                         snd_pcm_tick_prepare(substream);
2518         }
2519  _end_unlock:
2520         snd_pcm_stream_unlock_irq(substream);
2521  _end:
2522         return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2523 }
2524
2525 snd_pcm_sframes_t snd_pcm_lib_read(snd_pcm_substream_t *substream, void __user *buf, snd_pcm_uframes_t size)
2526 {
2527         snd_pcm_runtime_t *runtime;
2528         int nonblock;
2529         
2530         snd_assert(substream != NULL, return -ENXIO);
2531         runtime = substream->runtime;
2532         snd_assert(runtime != NULL, return -ENXIO);
2533         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2534         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2535                 return -EBADFD;
2536
2537         snd_assert(substream->ffile != NULL, return -ENXIO);
2538         nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2539 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2540         if (substream->oss.oss) {
2541                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
2542                 if (setup != NULL) {
2543                         if (setup->nonblock)
2544                                 nonblock = 1;
2545                         else if (setup->block)
2546                                 nonblock = 0;
2547                 }
2548         }
2549 #endif
2550         if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2551                 return -EINVAL;
2552         return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2553 }
2554
2555 static int snd_pcm_lib_readv_transfer(snd_pcm_substream_t *substream,
2556                                       unsigned int hwoff,
2557                                       unsigned long data, unsigned int off,
2558                                       snd_pcm_uframes_t frames)
2559 {
2560         snd_pcm_runtime_t *runtime = substream->runtime;
2561         int err;
2562         void __user **bufs = (void __user **)data;
2563         int channels = runtime->channels;
2564         int c;
2565         if (substream->ops->copy) {
2566                 for (c = 0; c < channels; ++c, ++bufs) {
2567                         char __user *buf;
2568                         if (*bufs == NULL)
2569                                 continue;
2570                         buf = *bufs + samples_to_bytes(runtime, off);
2571                         if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2572                                 return err;
2573                 }
2574         } else {
2575                 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2576                 snd_assert(runtime->dma_area, return -EFAULT);
2577                 for (c = 0; c < channels; ++c, ++bufs) {
2578                         char *hwbuf;
2579                         char __user *buf;
2580                         if (*bufs == NULL)
2581                                 continue;
2582
2583                         hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2584                         buf = *bufs + samples_to_bytes(runtime, off);
2585                         if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2586                                 return -EFAULT;
2587                 }
2588         }
2589         return 0;
2590 }
2591  
2592 snd_pcm_sframes_t snd_pcm_lib_readv(snd_pcm_substream_t *substream,
2593                                     void __user **bufs,
2594                                     snd_pcm_uframes_t frames)
2595 {
2596         snd_pcm_runtime_t *runtime;
2597         int nonblock;
2598
2599         snd_assert(substream != NULL, return -ENXIO);
2600         runtime = substream->runtime;
2601         snd_assert(runtime != NULL, return -ENXIO);
2602         snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2603         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2604                 return -EBADFD;
2605
2606         snd_assert(substream->ffile != NULL, return -ENXIO);
2607         nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2608 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2609         if (substream->oss.oss) {
2610                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
2611                 if (setup != NULL) {
2612                         if (setup->nonblock)
2613                                 nonblock = 1;
2614                         else if (setup->block)
2615                                 nonblock = 0;
2616                 }
2617         }
2618 #endif
2619
2620         if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2621                 return -EINVAL;
2622         return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2623 }
2624
2625 /*
2626  *  Exported symbols
2627  */
2628
2629 EXPORT_SYMBOL(snd_interval_refine);
2630 EXPORT_SYMBOL(snd_interval_list);
2631 EXPORT_SYMBOL(snd_interval_ratnum);
2632 EXPORT_SYMBOL(snd_interval_ratden);
2633 EXPORT_SYMBOL(snd_interval_muldivk);
2634 EXPORT_SYMBOL(snd_interval_mulkdiv);
2635 EXPORT_SYMBOL(snd_interval_div);
2636 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
2637 EXPORT_SYMBOL(_snd_pcm_hw_param_min);
2638 EXPORT_SYMBOL(_snd_pcm_hw_param_set);
2639 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
2640 EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger);
2641 EXPORT_SYMBOL(snd_pcm_hw_param_value_min);
2642 EXPORT_SYMBOL(snd_pcm_hw_param_value_max);
2643 EXPORT_SYMBOL(snd_pcm_hw_param_mask);
2644 EXPORT_SYMBOL(snd_pcm_hw_param_first);
2645 EXPORT_SYMBOL(snd_pcm_hw_param_last);
2646 EXPORT_SYMBOL(snd_pcm_hw_param_near);
2647 EXPORT_SYMBOL(snd_pcm_hw_param_set);
2648 EXPORT_SYMBOL(snd_pcm_hw_refine);
2649 EXPORT_SYMBOL(snd_pcm_hw_constraints_init);
2650 EXPORT_SYMBOL(snd_pcm_hw_constraints_complete);
2651 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
2652 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
2653 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
2654 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
2655 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
2656 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
2657 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
2658 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
2659 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
2660 EXPORT_SYMBOL(snd_pcm_set_ops);
2661 EXPORT_SYMBOL(snd_pcm_set_sync);
2662 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
2663 EXPORT_SYMBOL(snd_pcm_playback_ready);
2664 EXPORT_SYMBOL(snd_pcm_capture_ready);
2665 EXPORT_SYMBOL(snd_pcm_playback_data);
2666 EXPORT_SYMBOL(snd_pcm_capture_empty);
2667 EXPORT_SYMBOL(snd_pcm_stop);
2668 EXPORT_SYMBOL(snd_pcm_period_elapsed);
2669 EXPORT_SYMBOL(snd_pcm_lib_write);
2670 EXPORT_SYMBOL(snd_pcm_lib_read);
2671 EXPORT_SYMBOL(snd_pcm_lib_writev);
2672 EXPORT_SYMBOL(snd_pcm_lib_readv);
2673 EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes);
2674 EXPORT_SYMBOL(snd_pcm_lib_period_bytes);
2675 /* pcm_memory.c */
2676 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free);
2677 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
2678 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
2679 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
2680 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
2681 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
2682 EXPORT_SYMBOL(snd_pcm_lib_free_pages);