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