2 * Digital Audio (PCM) abstract layer
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.
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.
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
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>
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
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
42 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
44 struct snd_pcm_runtime *runtime = substream->runtime;
45 snd_pcm_uframes_t frames, ofs, transfer;
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;
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
59 if (runtime->silence_filled >= runtime->buffer_size)
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
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) %
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;
84 runtime->silence_start = ofs;
87 frames = runtime->buffer_size - runtime->silence_filled;
89 if (snd_BUG_ON(frames > runtime->buffer_size))
93 ofs = runtime->silence_start % runtime->buffer_size;
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) {
100 err = substream->ops->silence(substream, -1, ofs, transfer);
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
112 err = substream->ops->silence(substream, c, ofs, transfer);
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);
123 runtime->silence_filled += transfer;
129 static void pcm_debug_name(struct snd_pcm_substream *substream,
130 char *name, size_t len)
132 snprintf(name, len, "pcmC%dD%d%c:%d",
133 substream->pcm->card->number,
134 substream->pcm->device,
135 substream->stream ? 'c' : 'p',
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 */
147 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
149 #define xrun_debug(substream, mask) \
150 ((substream)->pstr->xrun_debug & (mask))
152 #define dump_stack_on_xrun(substream) do { \
153 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
157 static void xrun(struct snd_pcm_substream *substream)
159 struct snd_pcm_runtime *runtime = substream->runtime;
161 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
162 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
163 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
164 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
166 pcm_debug_name(substream, name, sizeof(name));
167 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
168 dump_stack_on_xrun(substream);
172 #define hw_ptr_error(substream, fmt, args...) \
174 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
175 xrun_log_show(substream); \
176 if (printk_ratelimit()) { \
177 snd_printd("PCM: " fmt, ##args); \
179 dump_stack_on_xrun(substream); \
183 #define XRUN_LOG_CNT 10
185 struct hwptr_log_entry {
186 unsigned long jiffies;
187 snd_pcm_uframes_t pos;
188 snd_pcm_uframes_t period_size;
189 snd_pcm_uframes_t buffer_size;
190 snd_pcm_uframes_t old_hw_ptr;
191 snd_pcm_uframes_t hw_ptr_base;
194 struct snd_pcm_hwptr_log {
197 struct hwptr_log_entry entries[XRUN_LOG_CNT];
200 static void xrun_log(struct snd_pcm_substream *substream,
201 snd_pcm_uframes_t pos)
203 struct snd_pcm_runtime *runtime = substream->runtime;
204 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
205 struct hwptr_log_entry *entry;
208 log = kzalloc(sizeof(*log), GFP_ATOMIC);
211 runtime->hwptr_log = log;
213 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
216 entry = &log->entries[log->idx];
217 entry->jiffies = jiffies;
219 entry->period_size = runtime->period_size;
220 entry->buffer_size = runtime->buffer_size;;
221 entry->old_hw_ptr = runtime->status->hw_ptr;
222 entry->hw_ptr_base = runtime->hw_ptr_base;
223 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
226 static void xrun_log_show(struct snd_pcm_substream *substream)
228 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
229 struct hwptr_log_entry *entry;
236 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
238 pcm_debug_name(substream, name, sizeof(name));
239 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
240 entry = &log->entries[idx];
241 if (entry->period_size == 0)
243 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
245 name, entry->jiffies, (unsigned long)entry->pos,
246 (unsigned long)entry->period_size,
247 (unsigned long)entry->buffer_size,
248 (unsigned long)entry->old_hw_ptr,
249 (unsigned long)entry->hw_ptr_base);
256 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
258 #define xrun_debug(substream, mask) 0
259 #define xrun(substream) do { } while (0)
260 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
261 #define xrun_log(substream, pos) do { } while (0)
262 #define xrun_log_show(substream) do { } while (0)
266 int snd_pcm_update_state(struct snd_pcm_substream *substream,
267 struct snd_pcm_runtime *runtime)
269 snd_pcm_uframes_t avail;
271 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
272 avail = snd_pcm_playback_avail(runtime);
274 avail = snd_pcm_capture_avail(runtime);
275 if (avail > runtime->avail_max)
276 runtime->avail_max = avail;
277 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
278 if (avail >= runtime->buffer_size) {
279 snd_pcm_drain_done(substream);
283 if (avail >= runtime->stop_threshold) {
288 if (!runtime->nowake && avail >= runtime->control->avail_min)
289 wake_up(&runtime->sleep);
293 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
294 unsigned int in_interrupt)
296 struct snd_pcm_runtime *runtime = substream->runtime;
297 snd_pcm_uframes_t pos;
298 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
299 snd_pcm_sframes_t hdelta, delta;
300 unsigned long jdelta;
302 old_hw_ptr = runtime->status->hw_ptr;
303 pos = substream->ops->pointer(substream);
304 if (pos == SNDRV_PCM_POS_XRUN) {
308 if (pos >= runtime->buffer_size) {
309 if (printk_ratelimit()) {
311 pcm_debug_name(substream, name, sizeof(name));
312 xrun_log_show(substream);
313 snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
314 "buffer size = %ld, period size = %ld\n",
315 name, pos, runtime->buffer_size,
316 runtime->period_size);
320 pos -= pos % runtime->min_align;
321 if (xrun_debug(substream, XRUN_DEBUG_LOG))
322 xrun_log(substream, pos);
323 hw_base = runtime->hw_ptr_base;
324 new_hw_ptr = hw_base + pos;
326 /* we know that one period was processed */
327 /* delta = "expected next hw_ptr" for in_interrupt != 0 */
328 delta = old_hw_ptr - (old_hw_ptr % runtime->period_size)
329 + runtime->period_size;
330 if (delta > new_hw_ptr) {
331 hw_base += runtime->buffer_size;
332 if (hw_base >= runtime->boundary)
334 new_hw_ptr = hw_base + pos;
338 /* new_hw_ptr might be lower than old_hw_ptr in case when */
339 /* pointer crosses the end of the ring buffer */
340 if (new_hw_ptr < old_hw_ptr) {
341 hw_base += runtime->buffer_size;
342 if (hw_base >= runtime->boundary)
344 new_hw_ptr = hw_base + pos;
347 delta = (new_hw_ptr - old_hw_ptr) % runtime->boundary;
348 if (xrun_debug(substream, in_interrupt ?
349 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
351 pcm_debug_name(substream, name, sizeof(name));
352 snd_printd("%s_update: %s: pos=%u/%u/%u, "
353 "hwptr=%ld/%ld/%ld/%ld\n",
354 in_interrupt ? "period" : "hwptr",
357 (unsigned int)runtime->period_size,
358 (unsigned int)runtime->buffer_size,
359 (unsigned long)delta,
360 (unsigned long)old_hw_ptr,
361 (unsigned long)new_hw_ptr,
362 (unsigned long)runtime->hw_ptr_base);
364 /* something must be really wrong */
365 if (delta >= runtime->buffer_size + runtime->period_size) {
366 hw_ptr_error(substream,
367 "Unexpected hw_pointer value %s"
368 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
370 in_interrupt ? "[Q] " : "[P]",
371 substream->stream, (long)pos,
372 (long)new_hw_ptr, (long)old_hw_ptr);
376 /* Do jiffies check only in xrun_debug mode */
377 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
378 goto no_jiffies_check;
380 /* Skip the jiffies check for hardwares with BATCH flag.
381 * Such hardware usually just increases the position at each IRQ,
382 * thus it can't give any strange position.
384 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
385 goto no_jiffies_check;
387 if (hdelta < runtime->delay)
388 goto no_jiffies_check;
389 hdelta -= runtime->delay;
390 jdelta = jiffies - runtime->hw_ptr_jiffies;
391 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
393 (((runtime->period_size * HZ) / runtime->rate)
395 /* move new_hw_ptr according jiffies not pos variable */
396 new_hw_ptr = old_hw_ptr;
397 /* use loop to avoid checks for delta overflows */
398 /* the delta value is small or zero in most cases */
400 new_hw_ptr += runtime->period_size;
401 if (new_hw_ptr >= runtime->boundary)
402 new_hw_ptr -= runtime->boundary;
405 /* align hw_base to buffer_size */
406 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
408 hw_ptr_error(substream,
409 "hw_ptr skipping! %s"
410 "(pos=%ld, delta=%ld, period=%ld, "
411 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
412 in_interrupt ? "[Q] " : "",
413 (long)pos, (long)hdelta,
414 (long)runtime->period_size, jdelta,
415 ((hdelta * HZ) / runtime->rate), delta,
416 (unsigned long)old_hw_ptr,
417 (unsigned long)new_hw_ptr);
420 if (delta > runtime->period_size + runtime->period_size / 2) {
421 hw_ptr_error(substream,
422 "Lost interrupts? %s"
423 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
425 in_interrupt ? "[Q] " : "",
426 substream->stream, (long)delta,
431 if (runtime->status->hw_ptr == new_hw_ptr)
434 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
435 runtime->silence_size > 0)
436 snd_pcm_playback_silence(substream, new_hw_ptr);
438 runtime->hw_ptr_base = hw_base;
439 runtime->status->hw_ptr = new_hw_ptr;
440 runtime->hw_ptr_jiffies = jiffies;
441 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
442 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
444 return snd_pcm_update_state(substream, runtime);
447 /* CAUTION: call it with irq disabled */
448 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
450 return snd_pcm_update_hw_ptr0(substream, 0);
454 * snd_pcm_set_ops - set the PCM operators
455 * @pcm: the pcm instance
456 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
457 * @ops: the operator table
459 * Sets the given PCM operators to the pcm instance.
461 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
463 struct snd_pcm_str *stream = &pcm->streams[direction];
464 struct snd_pcm_substream *substream;
466 for (substream = stream->substream; substream != NULL; substream = substream->next)
467 substream->ops = ops;
470 EXPORT_SYMBOL(snd_pcm_set_ops);
473 * snd_pcm_sync - set the PCM sync id
474 * @substream: the pcm substream
476 * Sets the PCM sync identifier for the card.
478 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
480 struct snd_pcm_runtime *runtime = substream->runtime;
482 runtime->sync.id32[0] = substream->pcm->card->number;
483 runtime->sync.id32[1] = -1;
484 runtime->sync.id32[2] = -1;
485 runtime->sync.id32[3] = -1;
488 EXPORT_SYMBOL(snd_pcm_set_sync);
491 * Standard ioctl routine
494 static inline unsigned int div32(unsigned int a, unsigned int b,
505 static inline unsigned int div_down(unsigned int a, unsigned int b)
512 static inline unsigned int div_up(unsigned int a, unsigned int b)
524 static inline unsigned int mul(unsigned int a, unsigned int b)
528 if (div_down(UINT_MAX, a) < b)
533 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
534 unsigned int c, unsigned int *r)
536 u_int64_t n = (u_int64_t) a * b;
542 n = div_u64_rem(n, c, r);
551 * snd_interval_refine - refine the interval value of configurator
552 * @i: the interval value to refine
553 * @v: the interval value to refer to
555 * Refines the interval value with the reference value.
556 * The interval is changed to the range satisfying both intervals.
557 * The interval status (min, max, integer, etc.) are evaluated.
559 * Returns non-zero if the value is changed, zero if not changed.
561 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
564 if (snd_BUG_ON(snd_interval_empty(i)))
566 if (i->min < v->min) {
568 i->openmin = v->openmin;
570 } else if (i->min == v->min && !i->openmin && v->openmin) {
574 if (i->max > v->max) {
576 i->openmax = v->openmax;
578 } else if (i->max == v->max && !i->openmax && v->openmax) {
582 if (!i->integer && v->integer) {
595 } else if (!i->openmin && !i->openmax && i->min == i->max)
597 if (snd_interval_checkempty(i)) {
598 snd_interval_none(i);
604 EXPORT_SYMBOL(snd_interval_refine);
606 static int snd_interval_refine_first(struct snd_interval *i)
608 if (snd_BUG_ON(snd_interval_empty(i)))
610 if (snd_interval_single(i))
613 i->openmax = i->openmin;
619 static int snd_interval_refine_last(struct snd_interval *i)
621 if (snd_BUG_ON(snd_interval_empty(i)))
623 if (snd_interval_single(i))
626 i->openmin = i->openmax;
632 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
634 if (a->empty || b->empty) {
635 snd_interval_none(c);
639 c->min = mul(a->min, b->min);
640 c->openmin = (a->openmin || b->openmin);
641 c->max = mul(a->max, b->max);
642 c->openmax = (a->openmax || b->openmax);
643 c->integer = (a->integer && b->integer);
647 * snd_interval_div - refine the interval value with division
654 * Returns non-zero if the value is changed, zero if not changed.
656 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
659 if (a->empty || b->empty) {
660 snd_interval_none(c);
664 c->min = div32(a->min, b->max, &r);
665 c->openmin = (r || a->openmin || b->openmax);
667 c->max = div32(a->max, b->min, &r);
672 c->openmax = (a->openmax || b->openmin);
681 * snd_interval_muldivk - refine the interval value
684 * @k: divisor (as integer)
689 * Returns non-zero if the value is changed, zero if not changed.
691 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
692 unsigned int k, struct snd_interval *c)
695 if (a->empty || b->empty) {
696 snd_interval_none(c);
700 c->min = muldiv32(a->min, b->min, k, &r);
701 c->openmin = (r || a->openmin || b->openmin);
702 c->max = muldiv32(a->max, b->max, k, &r);
707 c->openmax = (a->openmax || b->openmax);
712 * snd_interval_mulkdiv - refine the interval value
714 * @k: dividend 2 (as integer)
720 * Returns non-zero if the value is changed, zero if not changed.
722 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
723 const struct snd_interval *b, struct snd_interval *c)
726 if (a->empty || b->empty) {
727 snd_interval_none(c);
731 c->min = muldiv32(a->min, k, b->max, &r);
732 c->openmin = (r || a->openmin || b->openmax);
734 c->max = muldiv32(a->max, k, b->min, &r);
739 c->openmax = (a->openmax || b->openmin);
751 * snd_interval_ratnum - refine the interval value
752 * @i: interval to refine
753 * @rats_count: number of ratnum_t
754 * @rats: ratnum_t array
755 * @nump: pointer to store the resultant numerator
756 * @denp: pointer to store the resultant denominator
758 * Returns non-zero if the value is changed, zero if not changed.
760 int snd_interval_ratnum(struct snd_interval *i,
761 unsigned int rats_count, struct snd_ratnum *rats,
762 unsigned int *nump, unsigned int *denp)
764 unsigned int best_num, best_diff, best_den;
766 struct snd_interval t;
769 best_num = best_den = best_diff = 0;
770 for (k = 0; k < rats_count; ++k) {
771 unsigned int num = rats[k].num;
773 unsigned int q = i->min;
777 den = div_up(num, q);
778 if (den < rats[k].den_min)
780 if (den > rats[k].den_max)
781 den = rats[k].den_max;
784 r = (den - rats[k].den_min) % rats[k].den_step;
788 diff = num - q * den;
790 diff * best_den < best_diff * den) {
800 t.min = div_down(best_num, best_den);
801 t.openmin = !!(best_num % best_den);
803 best_num = best_den = best_diff = 0;
804 for (k = 0; k < rats_count; ++k) {
805 unsigned int num = rats[k].num;
807 unsigned int q = i->max;
813 den = div_down(num, q);
814 if (den > rats[k].den_max)
816 if (den < rats[k].den_min)
817 den = rats[k].den_min;
820 r = (den - rats[k].den_min) % rats[k].den_step;
822 den += rats[k].den_step - r;
824 diff = q * den - num;
826 diff * best_den < best_diff * den) {
836 t.max = div_up(best_num, best_den);
837 t.openmax = !!(best_num % best_den);
839 err = snd_interval_refine(i, &t);
843 if (snd_interval_single(i)) {
852 EXPORT_SYMBOL(snd_interval_ratnum);
855 * snd_interval_ratden - refine the interval value
856 * @i: interval to refine
857 * @rats_count: number of struct ratden
858 * @rats: struct ratden array
859 * @nump: pointer to store the resultant numerator
860 * @denp: pointer to store the resultant denominator
862 * Returns non-zero if the value is changed, zero if not changed.
864 static int snd_interval_ratden(struct snd_interval *i,
865 unsigned int rats_count, struct snd_ratden *rats,
866 unsigned int *nump, unsigned int *denp)
868 unsigned int best_num, best_diff, best_den;
870 struct snd_interval t;
873 best_num = best_den = best_diff = 0;
874 for (k = 0; k < rats_count; ++k) {
876 unsigned int den = rats[k].den;
877 unsigned int q = i->min;
880 if (num > rats[k].num_max)
882 if (num < rats[k].num_min)
883 num = rats[k].num_max;
886 r = (num - rats[k].num_min) % rats[k].num_step;
888 num += rats[k].num_step - r;
890 diff = num - q * den;
892 diff * best_den < best_diff * den) {
902 t.min = div_down(best_num, best_den);
903 t.openmin = !!(best_num % best_den);
905 best_num = best_den = best_diff = 0;
906 for (k = 0; k < rats_count; ++k) {
908 unsigned int den = rats[k].den;
909 unsigned int q = i->max;
912 if (num < rats[k].num_min)
914 if (num > rats[k].num_max)
915 num = rats[k].num_max;
918 r = (num - rats[k].num_min) % rats[k].num_step;
922 diff = q * den - num;
924 diff * best_den < best_diff * den) {
934 t.max = div_up(best_num, best_den);
935 t.openmax = !!(best_num % best_den);
937 err = snd_interval_refine(i, &t);
941 if (snd_interval_single(i)) {
951 * snd_interval_list - refine the interval value from the list
952 * @i: the interval value to refine
953 * @count: the number of elements in the list
954 * @list: the value list
955 * @mask: the bit-mask to evaluate
957 * Refines the interval value from the list.
958 * When mask is non-zero, only the elements corresponding to bit 1 are
961 * Returns non-zero if the value is changed, zero if not changed.
963 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
966 struct snd_interval list_range;
972 snd_interval_any(&list_range);
973 list_range.min = UINT_MAX;
975 for (k = 0; k < count; k++) {
976 if (mask && !(mask & (1 << k)))
978 if (!snd_interval_test(i, list[k]))
980 list_range.min = min(list_range.min, list[k]);
981 list_range.max = max(list_range.max, list[k]);
983 return snd_interval_refine(i, &list_range);
986 EXPORT_SYMBOL(snd_interval_list);
988 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
992 n = (i->min - min) % step;
993 if (n != 0 || i->openmin) {
997 n = (i->max - min) % step;
998 if (n != 0 || i->openmax) {
1002 if (snd_interval_checkempty(i)) {
1009 /* Info constraints helpers */
1012 * snd_pcm_hw_rule_add - add the hw-constraint rule
1013 * @runtime: the pcm runtime instance
1014 * @cond: condition bits
1015 * @var: the variable to evaluate
1016 * @func: the evaluation function
1017 * @private: the private data pointer passed to function
1018 * @dep: the dependent variables
1020 * Returns zero if successful, or a negative error code on failure.
1022 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1024 snd_pcm_hw_rule_func_t func, void *private,
1027 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1028 struct snd_pcm_hw_rule *c;
1031 va_start(args, dep);
1032 if (constrs->rules_num >= constrs->rules_all) {
1033 struct snd_pcm_hw_rule *new;
1034 unsigned int new_rules = constrs->rules_all + 16;
1035 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1038 if (constrs->rules) {
1039 memcpy(new, constrs->rules,
1040 constrs->rules_num * sizeof(*c));
1041 kfree(constrs->rules);
1043 constrs->rules = new;
1044 constrs->rules_all = new_rules;
1046 c = &constrs->rules[constrs->rules_num];
1050 c->private = private;
1053 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1058 dep = va_arg(args, int);
1060 constrs->rules_num++;
1065 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1068 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1069 * @runtime: PCM runtime instance
1070 * @var: hw_params variable to apply the mask
1071 * @mask: the bitmap mask
1073 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1075 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1078 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1079 struct snd_mask *maskp = constrs_mask(constrs, var);
1080 *maskp->bits &= mask;
1081 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1082 if (*maskp->bits == 0)
1088 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1089 * @runtime: PCM runtime instance
1090 * @var: hw_params variable to apply the mask
1091 * @mask: the 64bit bitmap mask
1093 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1095 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1098 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1099 struct snd_mask *maskp = constrs_mask(constrs, var);
1100 maskp->bits[0] &= (u_int32_t)mask;
1101 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1102 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1103 if (! maskp->bits[0] && ! maskp->bits[1])
1109 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1110 * @runtime: PCM runtime instance
1111 * @var: hw_params variable to apply the integer constraint
1113 * Apply the constraint of integer to an interval parameter.
1115 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1117 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1118 return snd_interval_setinteger(constrs_interval(constrs, var));
1121 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1124 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1125 * @runtime: PCM runtime instance
1126 * @var: hw_params variable to apply the range
1127 * @min: the minimal value
1128 * @max: the maximal value
1130 * Apply the min/max range constraint to an interval parameter.
1132 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1133 unsigned int min, unsigned int max)
1135 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1136 struct snd_interval t;
1139 t.openmin = t.openmax = 0;
1141 return snd_interval_refine(constrs_interval(constrs, var), &t);
1144 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1146 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1147 struct snd_pcm_hw_rule *rule)
1149 struct snd_pcm_hw_constraint_list *list = rule->private;
1150 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1155 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1156 * @runtime: PCM runtime instance
1157 * @cond: condition bits
1158 * @var: hw_params variable to apply the list constraint
1161 * Apply the list of constraints to an interval parameter.
1163 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1165 snd_pcm_hw_param_t var,
1166 struct snd_pcm_hw_constraint_list *l)
1168 return snd_pcm_hw_rule_add(runtime, cond, var,
1169 snd_pcm_hw_rule_list, l,
1173 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1175 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1176 struct snd_pcm_hw_rule *rule)
1178 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1179 unsigned int num = 0, den = 0;
1181 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1182 r->nrats, r->rats, &num, &den);
1183 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1184 params->rate_num = num;
1185 params->rate_den = den;
1191 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1192 * @runtime: PCM runtime instance
1193 * @cond: condition bits
1194 * @var: hw_params variable to apply the ratnums constraint
1195 * @r: struct snd_ratnums constriants
1197 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1199 snd_pcm_hw_param_t var,
1200 struct snd_pcm_hw_constraint_ratnums *r)
1202 return snd_pcm_hw_rule_add(runtime, cond, var,
1203 snd_pcm_hw_rule_ratnums, r,
1207 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1209 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1210 struct snd_pcm_hw_rule *rule)
1212 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1213 unsigned int num = 0, den = 0;
1214 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1215 r->nrats, r->rats, &num, &den);
1216 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1217 params->rate_num = num;
1218 params->rate_den = den;
1224 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1225 * @runtime: PCM runtime instance
1226 * @cond: condition bits
1227 * @var: hw_params variable to apply the ratdens constraint
1228 * @r: struct snd_ratdens constriants
1230 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1232 snd_pcm_hw_param_t var,
1233 struct snd_pcm_hw_constraint_ratdens *r)
1235 return snd_pcm_hw_rule_add(runtime, cond, var,
1236 snd_pcm_hw_rule_ratdens, r,
1240 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1242 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1243 struct snd_pcm_hw_rule *rule)
1245 unsigned int l = (unsigned long) rule->private;
1246 int width = l & 0xffff;
1247 unsigned int msbits = l >> 16;
1248 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1249 if (snd_interval_single(i) && snd_interval_value(i) == width)
1250 params->msbits = msbits;
1255 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1256 * @runtime: PCM runtime instance
1257 * @cond: condition bits
1258 * @width: sample bits width
1259 * @msbits: msbits width
1261 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1264 unsigned int msbits)
1266 unsigned long l = (msbits << 16) | width;
1267 return snd_pcm_hw_rule_add(runtime, cond, -1,
1268 snd_pcm_hw_rule_msbits,
1270 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1273 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1275 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1276 struct snd_pcm_hw_rule *rule)
1278 unsigned long step = (unsigned long) rule->private;
1279 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1283 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1284 * @runtime: PCM runtime instance
1285 * @cond: condition bits
1286 * @var: hw_params variable to apply the step constraint
1289 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1291 snd_pcm_hw_param_t var,
1294 return snd_pcm_hw_rule_add(runtime, cond, var,
1295 snd_pcm_hw_rule_step, (void *) step,
1299 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1301 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1303 static unsigned int pow2_sizes[] = {
1304 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1305 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1306 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1307 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1309 return snd_interval_list(hw_param_interval(params, rule->var),
1310 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1314 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1315 * @runtime: PCM runtime instance
1316 * @cond: condition bits
1317 * @var: hw_params variable to apply the power-of-2 constraint
1319 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1321 snd_pcm_hw_param_t var)
1323 return snd_pcm_hw_rule_add(runtime, cond, var,
1324 snd_pcm_hw_rule_pow2, NULL,
1328 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1330 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1331 snd_pcm_hw_param_t var)
1333 if (hw_is_mask(var)) {
1334 snd_mask_any(hw_param_mask(params, var));
1335 params->cmask |= 1 << var;
1336 params->rmask |= 1 << var;
1339 if (hw_is_interval(var)) {
1340 snd_interval_any(hw_param_interval(params, var));
1341 params->cmask |= 1 << var;
1342 params->rmask |= 1 << var;
1348 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1351 memset(params, 0, sizeof(*params));
1352 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1353 _snd_pcm_hw_param_any(params, k);
1354 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1355 _snd_pcm_hw_param_any(params, k);
1359 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1362 * snd_pcm_hw_param_value - return @params field @var value
1363 * @params: the hw_params instance
1364 * @var: parameter to retrieve
1365 * @dir: pointer to the direction (-1,0,1) or %NULL
1367 * Return the value for field @var if it's fixed in configuration space
1368 * defined by @params. Return -%EINVAL otherwise.
1370 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1371 snd_pcm_hw_param_t var, int *dir)
1373 if (hw_is_mask(var)) {
1374 const struct snd_mask *mask = hw_param_mask_c(params, var);
1375 if (!snd_mask_single(mask))
1379 return snd_mask_value(mask);
1381 if (hw_is_interval(var)) {
1382 const struct snd_interval *i = hw_param_interval_c(params, var);
1383 if (!snd_interval_single(i))
1387 return snd_interval_value(i);
1392 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1394 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1395 snd_pcm_hw_param_t var)
1397 if (hw_is_mask(var)) {
1398 snd_mask_none(hw_param_mask(params, var));
1399 params->cmask |= 1 << var;
1400 params->rmask |= 1 << var;
1401 } else if (hw_is_interval(var)) {
1402 snd_interval_none(hw_param_interval(params, var));
1403 params->cmask |= 1 << var;
1404 params->rmask |= 1 << var;
1410 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1412 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1413 snd_pcm_hw_param_t var)
1416 if (hw_is_mask(var))
1417 changed = snd_mask_refine_first(hw_param_mask(params, var));
1418 else if (hw_is_interval(var))
1419 changed = snd_interval_refine_first(hw_param_interval(params, var));
1423 params->cmask |= 1 << var;
1424 params->rmask |= 1 << var;
1431 * snd_pcm_hw_param_first - refine config space and return minimum value
1432 * @pcm: PCM instance
1433 * @params: the hw_params instance
1434 * @var: parameter to retrieve
1435 * @dir: pointer to the direction (-1,0,1) or %NULL
1437 * Inside configuration space defined by @params remove from @var all
1438 * values > minimum. Reduce configuration space accordingly.
1439 * Return the minimum.
1441 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1442 struct snd_pcm_hw_params *params,
1443 snd_pcm_hw_param_t var, int *dir)
1445 int changed = _snd_pcm_hw_param_first(params, var);
1448 if (params->rmask) {
1449 int err = snd_pcm_hw_refine(pcm, params);
1450 if (snd_BUG_ON(err < 0))
1453 return snd_pcm_hw_param_value(params, var, dir);
1456 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1458 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1459 snd_pcm_hw_param_t var)
1462 if (hw_is_mask(var))
1463 changed = snd_mask_refine_last(hw_param_mask(params, var));
1464 else if (hw_is_interval(var))
1465 changed = snd_interval_refine_last(hw_param_interval(params, var));
1469 params->cmask |= 1 << var;
1470 params->rmask |= 1 << var;
1477 * snd_pcm_hw_param_last - refine config space and return maximum value
1478 * @pcm: PCM instance
1479 * @params: the hw_params instance
1480 * @var: parameter to retrieve
1481 * @dir: pointer to the direction (-1,0,1) or %NULL
1483 * Inside configuration space defined by @params remove from @var all
1484 * values < maximum. Reduce configuration space accordingly.
1485 * Return the maximum.
1487 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1488 struct snd_pcm_hw_params *params,
1489 snd_pcm_hw_param_t var, int *dir)
1491 int changed = _snd_pcm_hw_param_last(params, var);
1494 if (params->rmask) {
1495 int err = snd_pcm_hw_refine(pcm, params);
1496 if (snd_BUG_ON(err < 0))
1499 return snd_pcm_hw_param_value(params, var, dir);
1502 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1505 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1506 * @pcm: PCM instance
1507 * @params: the hw_params instance
1509 * Choose one configuration from configuration space defined by @params.
1510 * The configuration chosen is that obtained fixing in this order:
1511 * first access, first format, first subformat, min channels,
1512 * min rate, min period time, max buffer size, min tick time
1514 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1515 struct snd_pcm_hw_params *params)
1517 static int vars[] = {
1518 SNDRV_PCM_HW_PARAM_ACCESS,
1519 SNDRV_PCM_HW_PARAM_FORMAT,
1520 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1521 SNDRV_PCM_HW_PARAM_CHANNELS,
1522 SNDRV_PCM_HW_PARAM_RATE,
1523 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1524 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1525 SNDRV_PCM_HW_PARAM_TICK_TIME,
1530 for (v = vars; *v != -1; v++) {
1531 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1532 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1534 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1535 if (snd_BUG_ON(err < 0))
1541 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1544 struct snd_pcm_runtime *runtime = substream->runtime;
1545 unsigned long flags;
1546 snd_pcm_stream_lock_irqsave(substream, flags);
1547 if (snd_pcm_running(substream) &&
1548 snd_pcm_update_hw_ptr(substream) >= 0)
1549 runtime->status->hw_ptr %= runtime->buffer_size;
1551 runtime->status->hw_ptr = 0;
1552 snd_pcm_stream_unlock_irqrestore(substream, flags);
1556 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1559 struct snd_pcm_channel_info *info = arg;
1560 struct snd_pcm_runtime *runtime = substream->runtime;
1562 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1566 width = snd_pcm_format_physical_width(runtime->format);
1570 switch (runtime->access) {
1571 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1572 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1573 info->first = info->channel * width;
1574 info->step = runtime->channels * width;
1576 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1577 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1579 size_t size = runtime->dma_bytes / runtime->channels;
1580 info->first = info->channel * size * 8;
1591 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1594 struct snd_pcm_hw_params *params = arg;
1595 snd_pcm_format_t format;
1596 int channels, width;
1598 params->fifo_size = substream->runtime->hw.fifo_size;
1599 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1600 format = params_format(params);
1601 channels = params_channels(params);
1602 width = snd_pcm_format_physical_width(format);
1603 params->fifo_size /= width * channels;
1609 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1610 * @substream: the pcm substream instance
1611 * @cmd: ioctl command
1612 * @arg: ioctl argument
1614 * Processes the generic ioctl commands for PCM.
1615 * Can be passed as the ioctl callback for PCM ops.
1617 * Returns zero if successful, or a negative error code on failure.
1619 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1620 unsigned int cmd, void *arg)
1623 case SNDRV_PCM_IOCTL1_INFO:
1625 case SNDRV_PCM_IOCTL1_RESET:
1626 return snd_pcm_lib_ioctl_reset(substream, arg);
1627 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1628 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1629 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1630 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1635 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1638 * snd_pcm_period_elapsed - update the pcm status for the next period
1639 * @substream: the pcm substream instance
1641 * This function is called from the interrupt handler when the
1642 * PCM has processed the period size. It will update the current
1643 * pointer, wake up sleepers, etc.
1645 * Even if more than one periods have elapsed since the last call, you
1646 * have to call this only once.
1648 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1650 struct snd_pcm_runtime *runtime;
1651 unsigned long flags;
1653 if (PCM_RUNTIME_CHECK(substream))
1655 runtime = substream->runtime;
1657 if (runtime->transfer_ack_begin)
1658 runtime->transfer_ack_begin(substream);
1660 snd_pcm_stream_lock_irqsave(substream, flags);
1661 if (!snd_pcm_running(substream) ||
1662 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1665 if (substream->timer_running)
1666 snd_timer_interrupt(substream->timer, 1);
1668 snd_pcm_stream_unlock_irqrestore(substream, flags);
1669 if (runtime->transfer_ack_end)
1670 runtime->transfer_ack_end(substream);
1671 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1674 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1677 * Wait until avail_min data becomes available
1678 * Returns a negative error code if any error occurs during operation.
1679 * The available space is stored on availp. When err = 0 and avail = 0
1680 * on the capture stream, it indicates the stream is in DRAINING state.
1682 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1683 snd_pcm_uframes_t *availp)
1685 struct snd_pcm_runtime *runtime = substream->runtime;
1686 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1689 snd_pcm_uframes_t avail = 0;
1692 init_waitqueue_entry(&wait, current);
1693 add_wait_queue(&runtime->sleep, &wait);
1695 if (signal_pending(current)) {
1699 set_current_state(TASK_INTERRUPTIBLE);
1700 snd_pcm_stream_unlock_irq(substream);
1701 tout = schedule_timeout(msecs_to_jiffies(10000));
1702 snd_pcm_stream_lock_irq(substream);
1703 switch (runtime->status->state) {
1704 case SNDRV_PCM_STATE_SUSPENDED:
1707 case SNDRV_PCM_STATE_XRUN:
1710 case SNDRV_PCM_STATE_DRAINING:
1714 avail = 0; /* indicate draining */
1716 case SNDRV_PCM_STATE_OPEN:
1717 case SNDRV_PCM_STATE_SETUP:
1718 case SNDRV_PCM_STATE_DISCONNECTED:
1723 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1724 is_playback ? "playback" : "capture");
1729 avail = snd_pcm_playback_avail(runtime);
1731 avail = snd_pcm_capture_avail(runtime);
1732 if (avail >= runtime->control->avail_min)
1736 remove_wait_queue(&runtime->sleep, &wait);
1741 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1743 unsigned long data, unsigned int off,
1744 snd_pcm_uframes_t frames)
1746 struct snd_pcm_runtime *runtime = substream->runtime;
1748 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1749 if (substream->ops->copy) {
1750 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1753 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1754 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1760 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1761 unsigned long data, unsigned int off,
1762 snd_pcm_uframes_t size);
1764 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1766 snd_pcm_uframes_t size,
1768 transfer_f transfer)
1770 struct snd_pcm_runtime *runtime = substream->runtime;
1771 snd_pcm_uframes_t xfer = 0;
1772 snd_pcm_uframes_t offset = 0;
1778 snd_pcm_stream_lock_irq(substream);
1779 switch (runtime->status->state) {
1780 case SNDRV_PCM_STATE_PREPARED:
1781 case SNDRV_PCM_STATE_RUNNING:
1782 case SNDRV_PCM_STATE_PAUSED:
1784 case SNDRV_PCM_STATE_XRUN:
1787 case SNDRV_PCM_STATE_SUSPENDED:
1795 runtime->nowake = 1;
1797 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1798 snd_pcm_uframes_t avail;
1799 snd_pcm_uframes_t cont;
1800 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1801 snd_pcm_update_hw_ptr(substream);
1802 avail = snd_pcm_playback_avail(runtime);
1808 err = wait_for_avail_min(substream, &avail);
1812 frames = size > avail ? avail : size;
1813 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1816 if (snd_BUG_ON(!frames)) {
1817 runtime->nowake = 0;
1818 snd_pcm_stream_unlock_irq(substream);
1821 appl_ptr = runtime->control->appl_ptr;
1822 appl_ofs = appl_ptr % runtime->buffer_size;
1823 snd_pcm_stream_unlock_irq(substream);
1824 err = transfer(substream, appl_ofs, data, offset, frames);
1825 snd_pcm_stream_lock_irq(substream);
1828 switch (runtime->status->state) {
1829 case SNDRV_PCM_STATE_XRUN:
1832 case SNDRV_PCM_STATE_SUSPENDED:
1839 if (appl_ptr >= runtime->boundary)
1840 appl_ptr -= runtime->boundary;
1841 runtime->control->appl_ptr = appl_ptr;
1842 if (substream->ops->ack)
1843 substream->ops->ack(substream);
1848 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1849 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1850 err = snd_pcm_start(substream);
1856 runtime->nowake = 0;
1857 if (xfer > 0 && err >= 0)
1858 snd_pcm_update_state(substream, runtime);
1859 snd_pcm_stream_unlock_irq(substream);
1860 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1863 /* sanity-check for read/write methods */
1864 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1866 struct snd_pcm_runtime *runtime;
1867 if (PCM_RUNTIME_CHECK(substream))
1869 runtime = substream->runtime;
1870 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1872 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1877 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1879 struct snd_pcm_runtime *runtime;
1883 err = pcm_sanity_check(substream);
1886 runtime = substream->runtime;
1887 nonblock = !!(substream->f_flags & O_NONBLOCK);
1889 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1890 runtime->channels > 1)
1892 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1893 snd_pcm_lib_write_transfer);
1896 EXPORT_SYMBOL(snd_pcm_lib_write);
1898 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1900 unsigned long data, unsigned int off,
1901 snd_pcm_uframes_t frames)
1903 struct snd_pcm_runtime *runtime = substream->runtime;
1905 void __user **bufs = (void __user **)data;
1906 int channels = runtime->channels;
1908 if (substream->ops->copy) {
1909 if (snd_BUG_ON(!substream->ops->silence))
1911 for (c = 0; c < channels; ++c, ++bufs) {
1912 if (*bufs == NULL) {
1913 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1916 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1917 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1922 /* default transfer behaviour */
1923 size_t dma_csize = runtime->dma_bytes / channels;
1924 for (c = 0; c < channels; ++c, ++bufs) {
1925 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1926 if (*bufs == NULL) {
1927 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1929 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1930 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1938 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1940 snd_pcm_uframes_t frames)
1942 struct snd_pcm_runtime *runtime;
1946 err = pcm_sanity_check(substream);
1949 runtime = substream->runtime;
1950 nonblock = !!(substream->f_flags & O_NONBLOCK);
1952 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1954 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1955 nonblock, snd_pcm_lib_writev_transfer);
1958 EXPORT_SYMBOL(snd_pcm_lib_writev);
1960 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1962 unsigned long data, unsigned int off,
1963 snd_pcm_uframes_t frames)
1965 struct snd_pcm_runtime *runtime = substream->runtime;
1967 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1968 if (substream->ops->copy) {
1969 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1972 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1973 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1979 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1981 snd_pcm_uframes_t size,
1983 transfer_f transfer)
1985 struct snd_pcm_runtime *runtime = substream->runtime;
1986 snd_pcm_uframes_t xfer = 0;
1987 snd_pcm_uframes_t offset = 0;
1993 snd_pcm_stream_lock_irq(substream);
1994 switch (runtime->status->state) {
1995 case SNDRV_PCM_STATE_PREPARED:
1996 if (size >= runtime->start_threshold) {
1997 err = snd_pcm_start(substream);
2002 case SNDRV_PCM_STATE_DRAINING:
2003 case SNDRV_PCM_STATE_RUNNING:
2004 case SNDRV_PCM_STATE_PAUSED:
2006 case SNDRV_PCM_STATE_XRUN:
2009 case SNDRV_PCM_STATE_SUSPENDED:
2017 runtime->nowake = 1;
2019 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2020 snd_pcm_uframes_t avail;
2021 snd_pcm_uframes_t cont;
2022 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2023 snd_pcm_update_hw_ptr(substream);
2024 avail = snd_pcm_capture_avail(runtime);
2026 if (runtime->status->state ==
2027 SNDRV_PCM_STATE_DRAINING) {
2028 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2035 err = wait_for_avail_min(substream, &avail);
2039 continue; /* draining */
2041 frames = size > avail ? avail : size;
2042 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2045 if (snd_BUG_ON(!frames)) {
2046 runtime->nowake = 0;
2047 snd_pcm_stream_unlock_irq(substream);
2050 appl_ptr = runtime->control->appl_ptr;
2051 appl_ofs = appl_ptr % runtime->buffer_size;
2052 snd_pcm_stream_unlock_irq(substream);
2053 err = transfer(substream, appl_ofs, data, offset, frames);
2054 snd_pcm_stream_lock_irq(substream);
2057 switch (runtime->status->state) {
2058 case SNDRV_PCM_STATE_XRUN:
2061 case SNDRV_PCM_STATE_SUSPENDED:
2068 if (appl_ptr >= runtime->boundary)
2069 appl_ptr -= runtime->boundary;
2070 runtime->control->appl_ptr = appl_ptr;
2071 if (substream->ops->ack)
2072 substream->ops->ack(substream);
2079 runtime->nowake = 0;
2080 if (xfer > 0 && err >= 0)
2081 snd_pcm_update_state(substream, runtime);
2082 snd_pcm_stream_unlock_irq(substream);
2083 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2086 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2088 struct snd_pcm_runtime *runtime;
2092 err = pcm_sanity_check(substream);
2095 runtime = substream->runtime;
2096 nonblock = !!(substream->f_flags & O_NONBLOCK);
2097 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2099 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2102 EXPORT_SYMBOL(snd_pcm_lib_read);
2104 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2106 unsigned long data, unsigned int off,
2107 snd_pcm_uframes_t frames)
2109 struct snd_pcm_runtime *runtime = substream->runtime;
2111 void __user **bufs = (void __user **)data;
2112 int channels = runtime->channels;
2114 if (substream->ops->copy) {
2115 for (c = 0; c < channels; ++c, ++bufs) {
2119 buf = *bufs + samples_to_bytes(runtime, off);
2120 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2124 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2125 for (c = 0; c < channels; ++c, ++bufs) {
2131 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2132 buf = *bufs + samples_to_bytes(runtime, off);
2133 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2140 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2142 snd_pcm_uframes_t frames)
2144 struct snd_pcm_runtime *runtime;
2148 err = pcm_sanity_check(substream);
2151 runtime = substream->runtime;
2152 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2155 nonblock = !!(substream->f_flags & O_NONBLOCK);
2156 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2158 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2161 EXPORT_SYMBOL(snd_pcm_lib_readv);