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