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 xrun_debug(substream, mask) 0
155 #define dump_stack_on_xrun(substream) do { \
156 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
160 static void xrun(struct snd_pcm_substream *substream)
162 struct snd_pcm_runtime *runtime = substream->runtime;
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)) {
169 pcm_debug_name(substream, name, sizeof(name));
170 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
171 dump_stack_on_xrun(substream);
175 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
176 #define hw_ptr_error(substream, fmt, args...) \
178 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
179 xrun_log_show(substream); \
180 if (printk_ratelimit()) { \
181 snd_printd("PCM: " fmt, ##args); \
183 dump_stack_on_xrun(substream); \
187 #define XRUN_LOG_CNT 10
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;
198 struct snd_pcm_hwptr_log {
201 struct hwptr_log_entry entries[XRUN_LOG_CNT];
204 static void xrun_log(struct snd_pcm_substream *substream,
205 snd_pcm_uframes_t pos)
207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
209 struct hwptr_log_entry *entry;
212 log = kzalloc(sizeof(*log), GFP_ATOMIC);
215 runtime->hwptr_log = log;
217 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
220 entry = &log->entries[log->idx];
221 entry->jiffies = jiffies;
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;
230 static void xrun_log_show(struct snd_pcm_substream *substream)
232 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
233 struct hwptr_log_entry *entry;
240 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
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)
247 snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
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);
260 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
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)
268 int snd_pcm_update_state(struct snd_pcm_substream *substream,
269 struct snd_pcm_runtime *runtime)
271 snd_pcm_uframes_t avail;
273 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
274 avail = snd_pcm_playback_avail(runtime);
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);
285 if (avail >= runtime->stop_threshold) {
290 if (avail >= runtime->control->avail_min)
291 wake_up(runtime->twake ? &runtime->tsleep : &runtime->sleep);
295 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
296 unsigned int in_interrupt)
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;
304 old_hw_ptr = runtime->status->hw_ptr;
305 pos = substream->ops->pointer(substream);
306 if (pos == SNDRV_PCM_POS_XRUN) {
310 if (pos >= runtime->buffer_size) {
311 if (printk_ratelimit()) {
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);
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;
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)
338 new_hw_ptr = hw_base + pos;
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)
349 new_hw_ptr = hw_base + pos;
352 delta = new_hw_ptr - old_hw_ptr;
354 delta += runtime->boundary;
355 if (xrun_debug(substream, in_interrupt ?
356 XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
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",
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);
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, "
377 in_interrupt ? "[Q] " : "[P]",
378 substream->stream, (long)pos,
379 (long)new_hw_ptr, (long)old_hw_ptr);
383 /* Do jiffies check only in xrun_debug mode */
384 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
385 goto no_jiffies_check;
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.
391 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
392 goto no_jiffies_check;
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) {
400 (((runtime->period_size * HZ) / runtime->rate)
402 /* move new_hw_ptr according jiffies not pos variable */
403 new_hw_ptr = old_hw_ptr;
405 /* use loop to avoid checks for delta overflows */
406 /* the delta value is small or zero in most cases */
408 new_hw_ptr += runtime->period_size;
409 if (new_hw_ptr >= runtime->boundary)
410 new_hw_ptr -= runtime->boundary;
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 */
426 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
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, "
434 in_interrupt ? "[Q] " : "",
435 substream->stream, (long)delta,
440 if (runtime->status->hw_ptr == new_hw_ptr)
443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
444 runtime->silence_size > 0)
445 snd_pcm_playback_silence(substream, new_hw_ptr);
448 delta = new_hw_ptr - runtime->hw_ptr_interrupt;
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;
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);
462 return snd_pcm_update_state(substream, runtime);
465 /* CAUTION: call it with irq disabled */
466 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
468 return snd_pcm_update_hw_ptr0(substream, 0);
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
477 * Sets the given PCM operators to the pcm instance.
479 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
481 struct snd_pcm_str *stream = &pcm->streams[direction];
482 struct snd_pcm_substream *substream;
484 for (substream = stream->substream; substream != NULL; substream = substream->next)
485 substream->ops = ops;
488 EXPORT_SYMBOL(snd_pcm_set_ops);
491 * snd_pcm_sync - set the PCM sync id
492 * @substream: the pcm substream
494 * Sets the PCM sync identifier for the card.
496 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
498 struct snd_pcm_runtime *runtime = substream->runtime;
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;
506 EXPORT_SYMBOL(snd_pcm_set_sync);
509 * Standard ioctl routine
512 static inline unsigned int div32(unsigned int a, unsigned int b,
523 static inline unsigned int div_down(unsigned int a, unsigned int b)
530 static inline unsigned int div_up(unsigned int a, unsigned int b)
542 static inline unsigned int mul(unsigned int a, unsigned int b)
546 if (div_down(UINT_MAX, a) < b)
551 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
552 unsigned int c, unsigned int *r)
554 u_int64_t n = (u_int64_t) a * b;
560 n = div_u64_rem(n, c, r);
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
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.
577 * Returns non-zero if the value is changed, zero if not changed.
579 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
582 if (snd_BUG_ON(snd_interval_empty(i)))
584 if (i->min < v->min) {
586 i->openmin = v->openmin;
588 } else if (i->min == v->min && !i->openmin && v->openmin) {
592 if (i->max > v->max) {
594 i->openmax = v->openmax;
596 } else if (i->max == v->max && !i->openmax && v->openmax) {
600 if (!i->integer && v->integer) {
613 } else if (!i->openmin && !i->openmax && i->min == i->max)
615 if (snd_interval_checkempty(i)) {
616 snd_interval_none(i);
622 EXPORT_SYMBOL(snd_interval_refine);
624 static int snd_interval_refine_first(struct snd_interval *i)
626 if (snd_BUG_ON(snd_interval_empty(i)))
628 if (snd_interval_single(i))
631 i->openmax = i->openmin;
637 static int snd_interval_refine_last(struct snd_interval *i)
639 if (snd_BUG_ON(snd_interval_empty(i)))
641 if (snd_interval_single(i))
644 i->openmin = i->openmax;
650 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
652 if (a->empty || b->empty) {
653 snd_interval_none(c);
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);
665 * snd_interval_div - refine the interval value with division
672 * Returns non-zero if the value is changed, zero if not changed.
674 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
677 if (a->empty || b->empty) {
678 snd_interval_none(c);
682 c->min = div32(a->min, b->max, &r);
683 c->openmin = (r || a->openmin || b->openmax);
685 c->max = div32(a->max, b->min, &r);
690 c->openmax = (a->openmax || b->openmin);
699 * snd_interval_muldivk - refine the interval value
702 * @k: divisor (as integer)
707 * Returns non-zero if the value is changed, zero if not changed.
709 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
710 unsigned int k, struct snd_interval *c)
713 if (a->empty || b->empty) {
714 snd_interval_none(c);
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);
725 c->openmax = (a->openmax || b->openmax);
730 * snd_interval_mulkdiv - refine the interval value
732 * @k: dividend 2 (as integer)
738 * Returns non-zero if the value is changed, zero if not changed.
740 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
741 const struct snd_interval *b, struct snd_interval *c)
744 if (a->empty || b->empty) {
745 snd_interval_none(c);
749 c->min = muldiv32(a->min, k, b->max, &r);
750 c->openmin = (r || a->openmin || b->openmax);
752 c->max = muldiv32(a->max, k, b->min, &r);
757 c->openmax = (a->openmax || b->openmin);
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
776 * Returns non-zero if the value is changed, zero if not changed.
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)
782 unsigned int best_num, best_den;
785 struct snd_interval t;
787 unsigned int result_num, result_den;
790 best_num = best_den = best_diff = 0;
791 for (k = 0; k < rats_count; ++k) {
792 unsigned int num = rats[k].num;
794 unsigned int q = i->min;
798 den = div_up(num, q);
799 if (den < rats[k].den_min)
801 if (den > rats[k].den_max)
802 den = rats[k].den_max;
805 r = (den - rats[k].den_min) % rats[k].den_step;
809 diff = num - q * den;
813 diff * best_den < best_diff * den) {
823 t.min = div_down(best_num, best_den);
824 t.openmin = !!(best_num % best_den);
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;
833 unsigned int q = i->max;
839 den = div_down(num, q);
840 if (den > rats[k].den_max)
842 if (den < rats[k].den_min)
843 den = rats[k].den_min;
846 r = (den - rats[k].den_min) % rats[k].den_step;
848 den += rats[k].den_step - r;
850 diff = q * den - num;
854 diff * best_den < best_diff * den) {
864 t.max = div_up(best_num, best_den);
865 t.openmax = !!(best_num % best_den);
867 err = snd_interval_refine(i, &t);
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;
884 EXPORT_SYMBOL(snd_interval_ratnum);
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
894 * Returns non-zero if the value is changed, zero if not changed.
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)
900 unsigned int best_num, best_diff, best_den;
902 struct snd_interval t;
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->min;
912 if (num > rats[k].num_max)
914 if (num < rats[k].num_min)
915 num = rats[k].num_max;
918 r = (num - rats[k].num_min) % rats[k].num_step;
920 num += rats[k].num_step - r;
922 diff = num - q * den;
924 diff * best_den < best_diff * den) {
934 t.min = div_down(best_num, best_den);
935 t.openmin = !!(best_num % best_den);
937 best_num = best_den = best_diff = 0;
938 for (k = 0; k < rats_count; ++k) {
940 unsigned int den = rats[k].den;
941 unsigned int q = i->max;
944 if (num < rats[k].num_min)
946 if (num > rats[k].num_max)
947 num = rats[k].num_max;
950 r = (num - rats[k].num_min) % rats[k].num_step;
954 diff = q * den - num;
956 diff * best_den < best_diff * den) {
966 t.max = div_up(best_num, best_den);
967 t.openmax = !!(best_num % best_den);
969 err = snd_interval_refine(i, &t);
973 if (snd_interval_single(i)) {
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
989 * Refines the interval value from the list.
990 * When mask is non-zero, only the elements corresponding to bit 1 are
993 * Returns non-zero if the value is changed, zero if not changed.
995 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
998 struct snd_interval list_range;
1004 snd_interval_any(&list_range);
1005 list_range.min = UINT_MAX;
1007 for (k = 0; k < count; k++) {
1008 if (mask && !(mask & (1 << k)))
1010 if (!snd_interval_test(i, list[k]))
1012 list_range.min = min(list_range.min, list[k]);
1013 list_range.max = max(list_range.max, list[k]);
1015 return snd_interval_refine(i, &list_range);
1018 EXPORT_SYMBOL(snd_interval_list);
1020 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
1024 n = (i->min - min) % step;
1025 if (n != 0 || i->openmin) {
1029 n = (i->max - min) % step;
1030 if (n != 0 || i->openmax) {
1034 if (snd_interval_checkempty(i)) {
1041 /* Info constraints helpers */
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
1052 * Returns zero if successful, or a negative error code on failure.
1054 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1056 snd_pcm_hw_rule_func_t func, void *private,
1059 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1060 struct snd_pcm_hw_rule *c;
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);
1070 if (constrs->rules) {
1071 memcpy(new, constrs->rules,
1072 constrs->rules_num * sizeof(*c));
1073 kfree(constrs->rules);
1075 constrs->rules = new;
1076 constrs->rules_all = new_rules;
1078 c = &constrs->rules[constrs->rules_num];
1082 c->private = private;
1085 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1090 dep = va_arg(args, int);
1092 constrs->rules_num++;
1097 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
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
1105 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1107 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
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)
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
1125 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1127 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
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])
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
1145 * Apply the constraint of integer to an interval parameter.
1147 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1149 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1150 return snd_interval_setinteger(constrs_interval(constrs, var));
1153 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
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
1162 * Apply the min/max range constraint to an interval parameter.
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)
1167 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1168 struct snd_interval t;
1171 t.openmin = t.openmax = 0;
1173 return snd_interval_refine(constrs_interval(constrs, var), &t);
1176 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1178 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1179 struct snd_pcm_hw_rule *rule)
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);
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
1193 * Apply the list of constraints to an interval parameter.
1195 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1197 snd_pcm_hw_param_t var,
1198 struct snd_pcm_hw_constraint_list *l)
1200 return snd_pcm_hw_rule_add(runtime, cond, var,
1201 snd_pcm_hw_rule_list, l,
1205 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1207 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1208 struct snd_pcm_hw_rule *rule)
1210 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1211 unsigned int num = 0, den = 0;
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;
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
1229 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1231 snd_pcm_hw_param_t var,
1232 struct snd_pcm_hw_constraint_ratnums *r)
1234 return snd_pcm_hw_rule_add(runtime, cond, var,
1235 snd_pcm_hw_rule_ratnums, r,
1239 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1241 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1242 struct snd_pcm_hw_rule *rule)
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;
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
1262 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1264 snd_pcm_hw_param_t var,
1265 struct snd_pcm_hw_constraint_ratdens *r)
1267 return snd_pcm_hw_rule_add(runtime, cond, var,
1268 snd_pcm_hw_rule_ratdens, r,
1272 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1274 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1275 struct snd_pcm_hw_rule *rule)
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;
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
1293 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1296 unsigned int msbits)
1298 unsigned long l = (msbits << 16) | width;
1299 return snd_pcm_hw_rule_add(runtime, cond, -1,
1300 snd_pcm_hw_rule_msbits,
1302 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1305 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1307 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1308 struct snd_pcm_hw_rule *rule)
1310 unsigned long step = (unsigned long) rule->private;
1311 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
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
1321 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1323 snd_pcm_hw_param_t var,
1326 return snd_pcm_hw_rule_add(runtime, cond, var,
1327 snd_pcm_hw_rule_step, (void *) step,
1331 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1333 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
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
1341 return snd_interval_list(hw_param_interval(params, rule->var),
1342 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
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
1351 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1353 snd_pcm_hw_param_t var)
1355 return snd_pcm_hw_rule_add(runtime, cond, var,
1356 snd_pcm_hw_rule_pow2, NULL,
1360 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1362 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1363 snd_pcm_hw_param_t var)
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;
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;
1380 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
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);
1391 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
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
1399 * Return the value for field @var if it's fixed in configuration space
1400 * defined by @params. Return -%EINVAL otherwise.
1402 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1403 snd_pcm_hw_param_t var, int *dir)
1405 if (hw_is_mask(var)) {
1406 const struct snd_mask *mask = hw_param_mask_c(params, var);
1407 if (!snd_mask_single(mask))
1411 return snd_mask_value(mask);
1413 if (hw_is_interval(var)) {
1414 const struct snd_interval *i = hw_param_interval_c(params, var);
1415 if (!snd_interval_single(i))
1419 return snd_interval_value(i);
1424 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1426 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1427 snd_pcm_hw_param_t var)
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;
1442 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1444 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1445 snd_pcm_hw_param_t var)
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));
1455 params->cmask |= 1 << var;
1456 params->rmask |= 1 << var;
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
1469 * Inside configuration space defined by @params remove from @var all
1470 * values > minimum. Reduce configuration space accordingly.
1471 * Return the minimum.
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)
1477 int changed = _snd_pcm_hw_param_first(params, var);
1480 if (params->rmask) {
1481 int err = snd_pcm_hw_refine(pcm, params);
1482 if (snd_BUG_ON(err < 0))
1485 return snd_pcm_hw_param_value(params, var, dir);
1488 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1490 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1491 snd_pcm_hw_param_t var)
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));
1501 params->cmask |= 1 << var;
1502 params->rmask |= 1 << var;
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
1515 * Inside configuration space defined by @params remove from @var all
1516 * values < maximum. Reduce configuration space accordingly.
1517 * Return the maximum.
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)
1523 int changed = _snd_pcm_hw_param_last(params, var);
1526 if (params->rmask) {
1527 int err = snd_pcm_hw_refine(pcm, params);
1528 if (snd_BUG_ON(err < 0))
1531 return snd_pcm_hw_param_value(params, var, dir);
1534 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1537 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1538 * @pcm: PCM instance
1539 * @params: the hw_params instance
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
1546 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1547 struct snd_pcm_hw_params *params)
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,
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);
1566 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1567 if (snd_BUG_ON(err < 0))
1573 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
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;
1583 runtime->status->hw_ptr = 0;
1584 snd_pcm_stream_unlock_irqrestore(substream, flags);
1588 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1591 struct snd_pcm_channel_info *info = arg;
1592 struct snd_pcm_runtime *runtime = substream->runtime;
1594 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1598 width = snd_pcm_format_physical_width(runtime->format);
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;
1608 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1609 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1611 size_t size = runtime->dma_bytes / runtime->channels;
1612 info->first = info->channel * size * 8;
1623 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1626 struct snd_pcm_hw_params *params = arg;
1627 snd_pcm_format_t format;
1628 int channels, width;
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;
1641 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1642 * @substream: the pcm substream instance
1643 * @cmd: ioctl command
1644 * @arg: ioctl argument
1646 * Processes the generic ioctl commands for PCM.
1647 * Can be passed as the ioctl callback for PCM ops.
1649 * Returns zero if successful, or a negative error code on failure.
1651 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1652 unsigned int cmd, void *arg)
1655 case SNDRV_PCM_IOCTL1_INFO:
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);
1667 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1670 * snd_pcm_period_elapsed - update the pcm status for the next period
1671 * @substream: the pcm substream instance
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.
1677 * Even if more than one periods have elapsed since the last call, you
1678 * have to call this only once.
1680 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1682 struct snd_pcm_runtime *runtime;
1683 unsigned long flags;
1685 if (PCM_RUNTIME_CHECK(substream))
1687 runtime = substream->runtime;
1689 if (runtime->transfer_ack_begin)
1690 runtime->transfer_ack_begin(substream);
1692 snd_pcm_stream_lock_irqsave(substream, flags);
1693 if (!snd_pcm_running(substream) ||
1694 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1697 if (substream->timer_running)
1698 snd_timer_interrupt(substream->timer, 1);
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);
1706 EXPORT_SYMBOL(snd_pcm_period_elapsed);
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.
1714 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1715 snd_pcm_uframes_t *availp)
1717 struct snd_pcm_runtime *runtime = substream->runtime;
1718 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1721 snd_pcm_uframes_t avail = 0;
1724 init_waitqueue_entry(&wait, current);
1725 add_wait_queue(&runtime->tsleep, &wait);
1727 if (signal_pending(current)) {
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:
1739 case SNDRV_PCM_STATE_XRUN:
1742 case SNDRV_PCM_STATE_DRAINING:
1746 avail = 0; /* indicate draining */
1748 case SNDRV_PCM_STATE_OPEN:
1749 case SNDRV_PCM_STATE_SETUP:
1750 case SNDRV_PCM_STATE_DISCONNECTED:
1755 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1756 is_playback ? "playback" : "capture");
1761 avail = snd_pcm_playback_avail(runtime);
1763 avail = snd_pcm_capture_avail(runtime);
1764 if (avail >= runtime->control->avail_min)
1768 remove_wait_queue(&runtime->tsleep, &wait);
1773 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1775 unsigned long data, unsigned int off,
1776 snd_pcm_uframes_t frames)
1778 struct snd_pcm_runtime *runtime = substream->runtime;
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)
1785 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1786 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
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);
1796 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1798 snd_pcm_uframes_t size,
1800 transfer_f transfer)
1802 struct snd_pcm_runtime *runtime = substream->runtime;
1803 snd_pcm_uframes_t xfer = 0;
1804 snd_pcm_uframes_t offset = 0;
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:
1816 case SNDRV_PCM_STATE_XRUN:
1819 case SNDRV_PCM_STATE_SUSPENDED:
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);
1840 err = wait_for_avail_min(substream, &avail);
1844 frames = size > avail ? avail : size;
1845 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1848 if (snd_BUG_ON(!frames)) {
1850 snd_pcm_stream_unlock_irq(substream);
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);
1860 switch (runtime->status->state) {
1861 case SNDRV_PCM_STATE_XRUN:
1864 case SNDRV_PCM_STATE_SUSPENDED:
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);
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);
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;
1895 /* sanity-check for read/write methods */
1896 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1898 struct snd_pcm_runtime *runtime;
1899 if (PCM_RUNTIME_CHECK(substream))
1901 runtime = substream->runtime;
1902 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1909 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1911 struct snd_pcm_runtime *runtime;
1915 err = pcm_sanity_check(substream);
1918 runtime = substream->runtime;
1919 nonblock = !!(substream->f_flags & O_NONBLOCK);
1921 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1922 runtime->channels > 1)
1924 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1925 snd_pcm_lib_write_transfer);
1928 EXPORT_SYMBOL(snd_pcm_lib_write);
1930 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1932 unsigned long data, unsigned int off,
1933 snd_pcm_uframes_t frames)
1935 struct snd_pcm_runtime *runtime = substream->runtime;
1937 void __user **bufs = (void __user **)data;
1938 int channels = runtime->channels;
1940 if (substream->ops->copy) {
1941 if (snd_BUG_ON(!substream->ops->silence))
1943 for (c = 0; c < channels; ++c, ++bufs) {
1944 if (*bufs == NULL) {
1945 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1948 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1949 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
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);
1961 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1962 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1970 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1972 snd_pcm_uframes_t frames)
1974 struct snd_pcm_runtime *runtime;
1978 err = pcm_sanity_check(substream);
1981 runtime = substream->runtime;
1982 nonblock = !!(substream->f_flags & O_NONBLOCK);
1984 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1986 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1987 nonblock, snd_pcm_lib_writev_transfer);
1990 EXPORT_SYMBOL(snd_pcm_lib_writev);
1992 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1994 unsigned long data, unsigned int off,
1995 snd_pcm_uframes_t frames)
1997 struct snd_pcm_runtime *runtime = substream->runtime;
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)
2004 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2005 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2011 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2013 snd_pcm_uframes_t size,
2015 transfer_f transfer)
2017 struct snd_pcm_runtime *runtime = substream->runtime;
2018 snd_pcm_uframes_t xfer = 0;
2019 snd_pcm_uframes_t offset = 0;
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);
2034 case SNDRV_PCM_STATE_DRAINING:
2035 case SNDRV_PCM_STATE_RUNNING:
2036 case SNDRV_PCM_STATE_PAUSED:
2038 case SNDRV_PCM_STATE_XRUN:
2041 case SNDRV_PCM_STATE_SUSPENDED:
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);
2058 if (runtime->status->state ==
2059 SNDRV_PCM_STATE_DRAINING) {
2060 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2067 err = wait_for_avail_min(substream, &avail);
2071 continue; /* draining */
2073 frames = size > avail ? avail : size;
2074 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2077 if (snd_BUG_ON(!frames)) {
2079 snd_pcm_stream_unlock_irq(substream);
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);
2089 switch (runtime->status->state) {
2090 case SNDRV_PCM_STATE_XRUN:
2093 case SNDRV_PCM_STATE_SUSPENDED:
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);
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;
2118 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2120 struct snd_pcm_runtime *runtime;
2124 err = pcm_sanity_check(substream);
2127 runtime = substream->runtime;
2128 nonblock = !!(substream->f_flags & O_NONBLOCK);
2129 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2131 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2134 EXPORT_SYMBOL(snd_pcm_lib_read);
2136 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2138 unsigned long data, unsigned int off,
2139 snd_pcm_uframes_t frames)
2141 struct snd_pcm_runtime *runtime = substream->runtime;
2143 void __user **bufs = (void __user **)data;
2144 int channels = runtime->channels;
2146 if (substream->ops->copy) {
2147 for (c = 0; c < channels; ++c, ++bufs) {
2151 buf = *bufs + samples_to_bytes(runtime, off);
2152 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2156 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2157 for (c = 0; c < channels; ++c, ++bufs) {
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)))
2172 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2174 snd_pcm_uframes_t frames)
2176 struct snd_pcm_runtime *runtime;
2180 err = pcm_sanity_check(substream);
2183 runtime = substream->runtime;
2184 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2187 nonblock = !!(substream->f_flags & O_NONBLOCK);
2188 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2190 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2193 EXPORT_SYMBOL(snd_pcm_lib_readv);