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