2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
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 if (printk_ratelimit()) { \
176 snd_printd("PCM: " fmt, ##args); \
178 dump_stack_on_xrun(substream); \
182 #define XRUN_LOG_CNT 10
184 struct hwptr_log_entry {
185 unsigned long jiffies;
186 snd_pcm_uframes_t pos;
187 snd_pcm_uframes_t period_size;
188 snd_pcm_uframes_t buffer_size;
189 snd_pcm_uframes_t old_hw_ptr;
190 snd_pcm_uframes_t hw_ptr_base;
191 snd_pcm_uframes_t hw_ptr_interrupt;
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 entry->hw_ptr_interrupt = runtime->hw_ptr_interrupt;;
224 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
227 static void xrun_log_show(struct snd_pcm_substream *substream)
229 struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
230 struct hwptr_log_entry *entry;
237 if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
239 pcm_debug_name(substream, name, sizeof(name));
240 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
241 entry = &log->entries[idx];
242 if (entry->period_size == 0)
244 snd_printd("hwptr log: %s: j=%lu, pos=0x%lx/0x%lx/0x%lx, "
245 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
246 name, entry->jiffies, (unsigned long)entry->pos,
247 (unsigned long)entry->period_size,
248 (unsigned long)entry->buffer_size,
249 (unsigned long)entry->old_hw_ptr,
250 (unsigned long)entry->hw_ptr_base,
251 (unsigned long)entry->hw_ptr_interrupt);
258 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
260 #define xrun_debug(substream, mask) 0
261 #define xrun(substream) do { } while (0)
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 static snd_pcm_uframes_t
269 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
270 struct snd_pcm_runtime *runtime)
272 snd_pcm_uframes_t pos;
274 pos = substream->ops->pointer(substream);
275 if (pos == SNDRV_PCM_POS_XRUN)
276 return pos; /* XRUN */
277 if (pos >= runtime->buffer_size) {
278 if (printk_ratelimit()) {
280 pcm_debug_name(substream, name, sizeof(name));
281 xrun_log_show(substream);
282 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
283 "buffer size = 0x%lx, period size = 0x%lx\n",
284 name, pos, runtime->buffer_size,
285 runtime->period_size);
289 pos -= pos % runtime->min_align;
290 if (xrun_debug(substream, XRUN_DEBUG_LOG))
291 xrun_log(substream, pos);
295 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
296 struct snd_pcm_runtime *runtime)
298 snd_pcm_uframes_t avail;
300 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
301 avail = snd_pcm_playback_avail(runtime);
303 avail = snd_pcm_capture_avail(runtime);
304 if (avail > runtime->avail_max)
305 runtime->avail_max = avail;
306 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
307 if (avail >= runtime->buffer_size) {
308 snd_pcm_drain_done(substream);
312 if (avail >= runtime->stop_threshold) {
317 if (avail >= runtime->control->avail_min)
318 wake_up(&runtime->sleep);
322 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
324 struct snd_pcm_runtime *runtime = substream->runtime;
325 snd_pcm_uframes_t pos;
326 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
327 snd_pcm_sframes_t hdelta, delta;
328 unsigned long jdelta;
330 old_hw_ptr = runtime->status->hw_ptr;
331 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
332 if (pos == SNDRV_PCM_POS_XRUN) {
336 if (xrun_debug(substream, XRUN_DEBUG_PERIODUPDATE)) {
338 pcm_debug_name(substream, name, sizeof(name));
339 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
340 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
341 name, (unsigned int)pos,
342 (unsigned int)runtime->period_size,
343 (unsigned int)runtime->buffer_size,
344 (unsigned long)old_hw_ptr,
345 (unsigned long)runtime->hw_ptr_base,
346 (unsigned long)runtime->hw_ptr_interrupt);
348 hw_base = runtime->hw_ptr_base;
349 new_hw_ptr = hw_base + pos;
350 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
351 delta = new_hw_ptr - hw_ptr_interrupt;
352 if (hw_ptr_interrupt >= runtime->boundary) {
353 hw_ptr_interrupt -= runtime->boundary;
354 if (hw_base < runtime->boundary / 2)
355 /* hw_base was already lapped; recalc delta */
356 delta = new_hw_ptr - hw_ptr_interrupt;
359 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
360 delta += runtime->buffer_size;
362 xrun_log_show(substream);
363 hw_ptr_error(substream,
364 "Unexpected hw_pointer value "
365 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
366 substream->stream, (long)pos,
367 (long)hw_ptr_interrupt);
369 /* simply skipping the hwptr update seems more
370 * robust in some cases, e.g. on VMware with
371 * inaccurate timer source
373 return 0; /* skip this update */
375 /* rebase to interrupt position */
376 hw_base = new_hw_ptr = hw_ptr_interrupt;
377 /* align hw_base to buffer_size */
378 hw_base -= hw_base % runtime->buffer_size;
382 hw_base += runtime->buffer_size;
383 if (hw_base >= runtime->boundary)
385 new_hw_ptr = hw_base + pos;
389 /* Do jiffies check only in xrun_debug mode */
390 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
391 goto no_jiffies_check;
393 /* Skip the jiffies check for hardwares with BATCH flag.
394 * Such hardware usually just increases the position at each IRQ,
395 * thus it can't give any strange position.
397 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
398 goto no_jiffies_check;
399 hdelta = new_hw_ptr - old_hw_ptr;
400 if (hdelta < runtime->delay)
401 goto no_jiffies_check;
402 hdelta -= runtime->delay;
403 jdelta = jiffies - runtime->hw_ptr_jiffies;
404 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
406 (((runtime->period_size * HZ) / runtime->rate)
408 xrun_log_show(substream);
409 hw_ptr_error(substream,
410 "hw_ptr skipping! [Q] "
411 "(pos=%ld, delta=%ld, period=%ld, "
412 "jdelta=%lu/%lu/%lu)\n",
413 (long)pos, (long)hdelta,
414 (long)runtime->period_size, jdelta,
415 ((hdelta * HZ) / runtime->rate), delta);
416 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
417 runtime->period_size * delta;
418 if (hw_ptr_interrupt >= runtime->boundary)
419 hw_ptr_interrupt -= runtime->boundary;
420 /* rebase to interrupt position */
421 hw_base = new_hw_ptr = hw_ptr_interrupt;
422 /* align hw_base to buffer_size */
423 hw_base -= hw_base % runtime->buffer_size;
427 if (delta > runtime->period_size + runtime->period_size / 2) {
428 xrun_log_show(substream);
429 hw_ptr_error(substream,
431 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
432 substream->stream, (long)delta,
433 (long)hw_ptr_interrupt);
434 /* rebase hw_ptr_interrupt */
436 new_hw_ptr - new_hw_ptr % runtime->period_size;
438 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
441 runtime->silence_size > 0)
442 snd_pcm_playback_silence(substream, new_hw_ptr);
444 if (runtime->status->hw_ptr == new_hw_ptr)
447 runtime->hw_ptr_base = hw_base;
448 runtime->status->hw_ptr = new_hw_ptr;
449 runtime->hw_ptr_jiffies = jiffies;
450 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
451 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
453 return snd_pcm_update_hw_ptr_post(substream, runtime);
456 /* CAUTION: call it with irq disabled */
457 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
459 struct snd_pcm_runtime *runtime = substream->runtime;
460 snd_pcm_uframes_t pos;
461 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
462 snd_pcm_sframes_t delta;
463 unsigned long jdelta;
465 old_hw_ptr = runtime->status->hw_ptr;
466 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
467 if (pos == SNDRV_PCM_POS_XRUN) {
471 if (xrun_debug(substream, XRUN_DEBUG_HWPTRUPDATE)) {
473 pcm_debug_name(substream, name, sizeof(name));
474 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
475 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
476 name, (unsigned int)pos,
477 (unsigned int)runtime->period_size,
478 (unsigned int)runtime->buffer_size,
479 (unsigned long)old_hw_ptr,
480 (unsigned long)runtime->hw_ptr_base,
481 (unsigned long)runtime->hw_ptr_interrupt);
484 hw_base = runtime->hw_ptr_base;
485 new_hw_ptr = hw_base + pos;
487 delta = new_hw_ptr - old_hw_ptr;
488 jdelta = jiffies - runtime->hw_ptr_jiffies;
490 delta += runtime->buffer_size;
492 xrun_log_show(substream);
493 hw_ptr_error(substream,
494 "Unexpected hw_pointer value [2] "
495 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
496 substream->stream, (long)pos,
497 (long)old_hw_ptr, jdelta);
500 hw_base += runtime->buffer_size;
501 if (hw_base >= runtime->boundary)
503 new_hw_ptr = hw_base + pos;
505 /* Do jiffies check only in xrun_debug mode */
506 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
507 goto no_jiffies_check;
508 if (delta < runtime->delay)
509 goto no_jiffies_check;
510 delta -= runtime->delay;
511 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
512 xrun_log_show(substream);
513 hw_ptr_error(substream,
515 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
516 (long)pos, (long)delta,
517 (long)runtime->period_size, jdelta,
518 ((delta * HZ) / runtime->rate));
522 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
523 runtime->silence_size > 0)
524 snd_pcm_playback_silence(substream, new_hw_ptr);
526 if (runtime->status->hw_ptr == new_hw_ptr)
529 runtime->hw_ptr_base = hw_base;
530 runtime->status->hw_ptr = new_hw_ptr;
531 runtime->hw_ptr_jiffies = jiffies;
532 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
533 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
535 return snd_pcm_update_hw_ptr_post(substream, runtime);
539 * snd_pcm_set_ops - set the PCM operators
540 * @pcm: the pcm instance
541 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
542 * @ops: the operator table
544 * Sets the given PCM operators to the pcm instance.
546 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
548 struct snd_pcm_str *stream = &pcm->streams[direction];
549 struct snd_pcm_substream *substream;
551 for (substream = stream->substream; substream != NULL; substream = substream->next)
552 substream->ops = ops;
555 EXPORT_SYMBOL(snd_pcm_set_ops);
558 * snd_pcm_sync - set the PCM sync id
559 * @substream: the pcm substream
561 * Sets the PCM sync identifier for the card.
563 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
565 struct snd_pcm_runtime *runtime = substream->runtime;
567 runtime->sync.id32[0] = substream->pcm->card->number;
568 runtime->sync.id32[1] = -1;
569 runtime->sync.id32[2] = -1;
570 runtime->sync.id32[3] = -1;
573 EXPORT_SYMBOL(snd_pcm_set_sync);
576 * Standard ioctl routine
579 static inline unsigned int div32(unsigned int a, unsigned int b,
590 static inline unsigned int div_down(unsigned int a, unsigned int b)
597 static inline unsigned int div_up(unsigned int a, unsigned int b)
609 static inline unsigned int mul(unsigned int a, unsigned int b)
613 if (div_down(UINT_MAX, a) < b)
618 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
619 unsigned int c, unsigned int *r)
621 u_int64_t n = (u_int64_t) a * b;
627 n = div_u64_rem(n, c, r);
636 * snd_interval_refine - refine the interval value of configurator
637 * @i: the interval value to refine
638 * @v: the interval value to refer to
640 * Refines the interval value with the reference value.
641 * The interval is changed to the range satisfying both intervals.
642 * The interval status (min, max, integer, etc.) are evaluated.
644 * Returns non-zero if the value is changed, zero if not changed.
646 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
649 if (snd_BUG_ON(snd_interval_empty(i)))
651 if (i->min < v->min) {
653 i->openmin = v->openmin;
655 } else if (i->min == v->min && !i->openmin && v->openmin) {
659 if (i->max > v->max) {
661 i->openmax = v->openmax;
663 } else if (i->max == v->max && !i->openmax && v->openmax) {
667 if (!i->integer && v->integer) {
680 } else if (!i->openmin && !i->openmax && i->min == i->max)
682 if (snd_interval_checkempty(i)) {
683 snd_interval_none(i);
689 EXPORT_SYMBOL(snd_interval_refine);
691 static int snd_interval_refine_first(struct snd_interval *i)
693 if (snd_BUG_ON(snd_interval_empty(i)))
695 if (snd_interval_single(i))
698 i->openmax = i->openmin;
704 static int snd_interval_refine_last(struct snd_interval *i)
706 if (snd_BUG_ON(snd_interval_empty(i)))
708 if (snd_interval_single(i))
711 i->openmin = i->openmax;
717 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
719 if (a->empty || b->empty) {
720 snd_interval_none(c);
724 c->min = mul(a->min, b->min);
725 c->openmin = (a->openmin || b->openmin);
726 c->max = mul(a->max, b->max);
727 c->openmax = (a->openmax || b->openmax);
728 c->integer = (a->integer && b->integer);
732 * snd_interval_div - refine the interval value with division
739 * Returns non-zero if the value is changed, zero if not changed.
741 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
744 if (a->empty || b->empty) {
745 snd_interval_none(c);
749 c->min = div32(a->min, b->max, &r);
750 c->openmin = (r || a->openmin || b->openmax);
752 c->max = div32(a->max, b->min, &r);
757 c->openmax = (a->openmax || b->openmin);
766 * snd_interval_muldivk - refine the interval value
769 * @k: divisor (as integer)
774 * Returns non-zero if the value is changed, zero if not changed.
776 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
777 unsigned int k, struct snd_interval *c)
780 if (a->empty || b->empty) {
781 snd_interval_none(c);
785 c->min = muldiv32(a->min, b->min, k, &r);
786 c->openmin = (r || a->openmin || b->openmin);
787 c->max = muldiv32(a->max, b->max, k, &r);
792 c->openmax = (a->openmax || b->openmax);
797 * snd_interval_mulkdiv - refine the interval value
799 * @k: dividend 2 (as integer)
805 * Returns non-zero if the value is changed, zero if not changed.
807 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
808 const struct snd_interval *b, struct snd_interval *c)
811 if (a->empty || b->empty) {
812 snd_interval_none(c);
816 c->min = muldiv32(a->min, k, b->max, &r);
817 c->openmin = (r || a->openmin || b->openmax);
819 c->max = muldiv32(a->max, k, b->min, &r);
824 c->openmax = (a->openmax || b->openmin);
836 * snd_interval_ratnum - refine the interval value
837 * @i: interval to refine
838 * @rats_count: number of ratnum_t
839 * @rats: ratnum_t array
840 * @nump: pointer to store the resultant numerator
841 * @denp: pointer to store the resultant denominator
843 * Returns non-zero if the value is changed, zero if not changed.
845 int snd_interval_ratnum(struct snd_interval *i,
846 unsigned int rats_count, struct snd_ratnum *rats,
847 unsigned int *nump, unsigned int *denp)
849 unsigned int best_num, best_diff, best_den;
851 struct snd_interval t;
854 best_num = best_den = best_diff = 0;
855 for (k = 0; k < rats_count; ++k) {
856 unsigned int num = rats[k].num;
858 unsigned int q = i->min;
862 den = div_down(num, q);
863 if (den < rats[k].den_min)
865 if (den > rats[k].den_max)
866 den = rats[k].den_max;
869 r = (den - rats[k].den_min) % rats[k].den_step;
873 diff = num - q * den;
875 diff * best_den < best_diff * den) {
885 t.min = div_down(best_num, best_den);
886 t.openmin = !!(best_num % best_den);
888 best_num = best_den = best_diff = 0;
889 for (k = 0; k < rats_count; ++k) {
890 unsigned int num = rats[k].num;
892 unsigned int q = i->max;
898 den = div_up(num, q);
899 if (den > rats[k].den_max)
901 if (den < rats[k].den_min)
902 den = rats[k].den_min;
905 r = (den - rats[k].den_min) % rats[k].den_step;
907 den += rats[k].den_step - r;
909 diff = q * den - num;
911 diff * best_den < best_diff * den) {
921 t.max = div_up(best_num, best_den);
922 t.openmax = !!(best_num % best_den);
924 err = snd_interval_refine(i, &t);
928 if (snd_interval_single(i)) {
937 EXPORT_SYMBOL(snd_interval_ratnum);
940 * snd_interval_ratden - refine the interval value
941 * @i: interval to refine
942 * @rats_count: number of struct ratden
943 * @rats: struct ratden array
944 * @nump: pointer to store the resultant numerator
945 * @denp: pointer to store the resultant denominator
947 * Returns non-zero if the value is changed, zero if not changed.
949 static int snd_interval_ratden(struct snd_interval *i,
950 unsigned int rats_count, struct snd_ratden *rats,
951 unsigned int *nump, unsigned int *denp)
953 unsigned int best_num, best_diff, best_den;
955 struct snd_interval t;
958 best_num = best_den = best_diff = 0;
959 for (k = 0; k < rats_count; ++k) {
961 unsigned int den = rats[k].den;
962 unsigned int q = i->min;
965 if (num > rats[k].num_max)
967 if (num < rats[k].num_min)
968 num = rats[k].num_max;
971 r = (num - rats[k].num_min) % rats[k].num_step;
973 num += rats[k].num_step - r;
975 diff = num - q * den;
977 diff * best_den < best_diff * den) {
987 t.min = div_down(best_num, best_den);
988 t.openmin = !!(best_num % best_den);
990 best_num = best_den = best_diff = 0;
991 for (k = 0; k < rats_count; ++k) {
993 unsigned int den = rats[k].den;
994 unsigned int q = i->max;
997 if (num < rats[k].num_min)
999 if (num > rats[k].num_max)
1000 num = rats[k].num_max;
1003 r = (num - rats[k].num_min) % rats[k].num_step;
1007 diff = q * den - num;
1008 if (best_num == 0 ||
1009 diff * best_den < best_diff * den) {
1015 if (best_den == 0) {
1019 t.max = div_up(best_num, best_den);
1020 t.openmax = !!(best_num % best_den);
1022 err = snd_interval_refine(i, &t);
1026 if (snd_interval_single(i)) {
1036 * snd_interval_list - refine the interval value from the list
1037 * @i: the interval value to refine
1038 * @count: the number of elements in the list
1039 * @list: the value list
1040 * @mask: the bit-mask to evaluate
1042 * Refines the interval value from the list.
1043 * When mask is non-zero, only the elements corresponding to bit 1 are
1046 * Returns non-zero if the value is changed, zero if not changed.
1048 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
1051 struct snd_interval list_range;
1057 snd_interval_any(&list_range);
1058 list_range.min = UINT_MAX;
1060 for (k = 0; k < count; k++) {
1061 if (mask && !(mask & (1 << k)))
1063 if (!snd_interval_test(i, list[k]))
1065 list_range.min = min(list_range.min, list[k]);
1066 list_range.max = max(list_range.max, list[k]);
1068 return snd_interval_refine(i, &list_range);
1071 EXPORT_SYMBOL(snd_interval_list);
1073 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
1077 n = (i->min - min) % step;
1078 if (n != 0 || i->openmin) {
1082 n = (i->max - min) % step;
1083 if (n != 0 || i->openmax) {
1087 if (snd_interval_checkempty(i)) {
1094 /* Info constraints helpers */
1097 * snd_pcm_hw_rule_add - add the hw-constraint rule
1098 * @runtime: the pcm runtime instance
1099 * @cond: condition bits
1100 * @var: the variable to evaluate
1101 * @func: the evaluation function
1102 * @private: the private data pointer passed to function
1103 * @dep: the dependent variables
1105 * Returns zero if successful, or a negative error code on failure.
1107 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1109 snd_pcm_hw_rule_func_t func, void *private,
1112 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1113 struct snd_pcm_hw_rule *c;
1116 va_start(args, dep);
1117 if (constrs->rules_num >= constrs->rules_all) {
1118 struct snd_pcm_hw_rule *new;
1119 unsigned int new_rules = constrs->rules_all + 16;
1120 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1123 if (constrs->rules) {
1124 memcpy(new, constrs->rules,
1125 constrs->rules_num * sizeof(*c));
1126 kfree(constrs->rules);
1128 constrs->rules = new;
1129 constrs->rules_all = new_rules;
1131 c = &constrs->rules[constrs->rules_num];
1135 c->private = private;
1138 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1143 dep = va_arg(args, int);
1145 constrs->rules_num++;
1150 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1153 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1154 * @runtime: PCM runtime instance
1155 * @var: hw_params variable to apply the mask
1156 * @mask: the bitmap mask
1158 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1160 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1163 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1164 struct snd_mask *maskp = constrs_mask(constrs, var);
1165 *maskp->bits &= mask;
1166 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1167 if (*maskp->bits == 0)
1173 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1174 * @runtime: PCM runtime instance
1175 * @var: hw_params variable to apply the mask
1176 * @mask: the 64bit bitmap mask
1178 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1180 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1183 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1184 struct snd_mask *maskp = constrs_mask(constrs, var);
1185 maskp->bits[0] &= (u_int32_t)mask;
1186 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1187 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1188 if (! maskp->bits[0] && ! maskp->bits[1])
1194 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1195 * @runtime: PCM runtime instance
1196 * @var: hw_params variable to apply the integer constraint
1198 * Apply the constraint of integer to an interval parameter.
1200 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1202 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1203 return snd_interval_setinteger(constrs_interval(constrs, var));
1206 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1209 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1210 * @runtime: PCM runtime instance
1211 * @var: hw_params variable to apply the range
1212 * @min: the minimal value
1213 * @max: the maximal value
1215 * Apply the min/max range constraint to an interval parameter.
1217 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1218 unsigned int min, unsigned int max)
1220 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1221 struct snd_interval t;
1224 t.openmin = t.openmax = 0;
1226 return snd_interval_refine(constrs_interval(constrs, var), &t);
1229 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1231 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1232 struct snd_pcm_hw_rule *rule)
1234 struct snd_pcm_hw_constraint_list *list = rule->private;
1235 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1240 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1241 * @runtime: PCM runtime instance
1242 * @cond: condition bits
1243 * @var: hw_params variable to apply the list constraint
1246 * Apply the list of constraints to an interval parameter.
1248 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1250 snd_pcm_hw_param_t var,
1251 struct snd_pcm_hw_constraint_list *l)
1253 return snd_pcm_hw_rule_add(runtime, cond, var,
1254 snd_pcm_hw_rule_list, l,
1258 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1260 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1261 struct snd_pcm_hw_rule *rule)
1263 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1264 unsigned int num = 0, den = 0;
1266 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1267 r->nrats, r->rats, &num, &den);
1268 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1269 params->rate_num = num;
1270 params->rate_den = den;
1276 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1277 * @runtime: PCM runtime instance
1278 * @cond: condition bits
1279 * @var: hw_params variable to apply the ratnums constraint
1280 * @r: struct snd_ratnums constriants
1282 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1284 snd_pcm_hw_param_t var,
1285 struct snd_pcm_hw_constraint_ratnums *r)
1287 return snd_pcm_hw_rule_add(runtime, cond, var,
1288 snd_pcm_hw_rule_ratnums, r,
1292 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1294 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1295 struct snd_pcm_hw_rule *rule)
1297 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1298 unsigned int num = 0, den = 0;
1299 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1300 r->nrats, r->rats, &num, &den);
1301 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1302 params->rate_num = num;
1303 params->rate_den = den;
1309 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1310 * @runtime: PCM runtime instance
1311 * @cond: condition bits
1312 * @var: hw_params variable to apply the ratdens constraint
1313 * @r: struct snd_ratdens constriants
1315 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1317 snd_pcm_hw_param_t var,
1318 struct snd_pcm_hw_constraint_ratdens *r)
1320 return snd_pcm_hw_rule_add(runtime, cond, var,
1321 snd_pcm_hw_rule_ratdens, r,
1325 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1327 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1328 struct snd_pcm_hw_rule *rule)
1330 unsigned int l = (unsigned long) rule->private;
1331 int width = l & 0xffff;
1332 unsigned int msbits = l >> 16;
1333 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1334 if (snd_interval_single(i) && snd_interval_value(i) == width)
1335 params->msbits = msbits;
1340 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1341 * @runtime: PCM runtime instance
1342 * @cond: condition bits
1343 * @width: sample bits width
1344 * @msbits: msbits width
1346 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1349 unsigned int msbits)
1351 unsigned long l = (msbits << 16) | width;
1352 return snd_pcm_hw_rule_add(runtime, cond, -1,
1353 snd_pcm_hw_rule_msbits,
1355 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1358 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1360 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1361 struct snd_pcm_hw_rule *rule)
1363 unsigned long step = (unsigned long) rule->private;
1364 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1368 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1369 * @runtime: PCM runtime instance
1370 * @cond: condition bits
1371 * @var: hw_params variable to apply the step constraint
1374 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1376 snd_pcm_hw_param_t var,
1379 return snd_pcm_hw_rule_add(runtime, cond, var,
1380 snd_pcm_hw_rule_step, (void *) step,
1384 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1386 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1388 static unsigned int pow2_sizes[] = {
1389 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1390 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1391 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1392 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1394 return snd_interval_list(hw_param_interval(params, rule->var),
1395 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1399 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1400 * @runtime: PCM runtime instance
1401 * @cond: condition bits
1402 * @var: hw_params variable to apply the power-of-2 constraint
1404 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1406 snd_pcm_hw_param_t var)
1408 return snd_pcm_hw_rule_add(runtime, cond, var,
1409 snd_pcm_hw_rule_pow2, NULL,
1413 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1415 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1416 snd_pcm_hw_param_t var)
1418 if (hw_is_mask(var)) {
1419 snd_mask_any(hw_param_mask(params, var));
1420 params->cmask |= 1 << var;
1421 params->rmask |= 1 << var;
1424 if (hw_is_interval(var)) {
1425 snd_interval_any(hw_param_interval(params, var));
1426 params->cmask |= 1 << var;
1427 params->rmask |= 1 << var;
1433 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1436 memset(params, 0, sizeof(*params));
1437 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1438 _snd_pcm_hw_param_any(params, k);
1439 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1440 _snd_pcm_hw_param_any(params, k);
1444 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1447 * snd_pcm_hw_param_value - return @params field @var value
1448 * @params: the hw_params instance
1449 * @var: parameter to retrieve
1450 * @dir: pointer to the direction (-1,0,1) or %NULL
1452 * Return the value for field @var if it's fixed in configuration space
1453 * defined by @params. Return -%EINVAL otherwise.
1455 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1456 snd_pcm_hw_param_t var, int *dir)
1458 if (hw_is_mask(var)) {
1459 const struct snd_mask *mask = hw_param_mask_c(params, var);
1460 if (!snd_mask_single(mask))
1464 return snd_mask_value(mask);
1466 if (hw_is_interval(var)) {
1467 const struct snd_interval *i = hw_param_interval_c(params, var);
1468 if (!snd_interval_single(i))
1472 return snd_interval_value(i);
1477 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1479 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1480 snd_pcm_hw_param_t var)
1482 if (hw_is_mask(var)) {
1483 snd_mask_none(hw_param_mask(params, var));
1484 params->cmask |= 1 << var;
1485 params->rmask |= 1 << var;
1486 } else if (hw_is_interval(var)) {
1487 snd_interval_none(hw_param_interval(params, var));
1488 params->cmask |= 1 << var;
1489 params->rmask |= 1 << var;
1495 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1497 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1498 snd_pcm_hw_param_t var)
1501 if (hw_is_mask(var))
1502 changed = snd_mask_refine_first(hw_param_mask(params, var));
1503 else if (hw_is_interval(var))
1504 changed = snd_interval_refine_first(hw_param_interval(params, var));
1508 params->cmask |= 1 << var;
1509 params->rmask |= 1 << var;
1516 * snd_pcm_hw_param_first - refine config space and return minimum value
1517 * @pcm: PCM instance
1518 * @params: the hw_params instance
1519 * @var: parameter to retrieve
1520 * @dir: pointer to the direction (-1,0,1) or %NULL
1522 * Inside configuration space defined by @params remove from @var all
1523 * values > minimum. Reduce configuration space accordingly.
1524 * Return the minimum.
1526 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1527 struct snd_pcm_hw_params *params,
1528 snd_pcm_hw_param_t var, int *dir)
1530 int changed = _snd_pcm_hw_param_first(params, var);
1533 if (params->rmask) {
1534 int err = snd_pcm_hw_refine(pcm, params);
1535 if (snd_BUG_ON(err < 0))
1538 return snd_pcm_hw_param_value(params, var, dir);
1541 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1543 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1544 snd_pcm_hw_param_t var)
1547 if (hw_is_mask(var))
1548 changed = snd_mask_refine_last(hw_param_mask(params, var));
1549 else if (hw_is_interval(var))
1550 changed = snd_interval_refine_last(hw_param_interval(params, var));
1554 params->cmask |= 1 << var;
1555 params->rmask |= 1 << var;
1562 * snd_pcm_hw_param_last - refine config space and return maximum value
1563 * @pcm: PCM instance
1564 * @params: the hw_params instance
1565 * @var: parameter to retrieve
1566 * @dir: pointer to the direction (-1,0,1) or %NULL
1568 * Inside configuration space defined by @params remove from @var all
1569 * values < maximum. Reduce configuration space accordingly.
1570 * Return the maximum.
1572 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1573 struct snd_pcm_hw_params *params,
1574 snd_pcm_hw_param_t var, int *dir)
1576 int changed = _snd_pcm_hw_param_last(params, var);
1579 if (params->rmask) {
1580 int err = snd_pcm_hw_refine(pcm, params);
1581 if (snd_BUG_ON(err < 0))
1584 return snd_pcm_hw_param_value(params, var, dir);
1587 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1590 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1591 * @pcm: PCM instance
1592 * @params: the hw_params instance
1594 * Choose one configuration from configuration space defined by @params.
1595 * The configuration chosen is that obtained fixing in this order:
1596 * first access, first format, first subformat, min channels,
1597 * min rate, min period time, max buffer size, min tick time
1599 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1600 struct snd_pcm_hw_params *params)
1602 static int vars[] = {
1603 SNDRV_PCM_HW_PARAM_ACCESS,
1604 SNDRV_PCM_HW_PARAM_FORMAT,
1605 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1606 SNDRV_PCM_HW_PARAM_CHANNELS,
1607 SNDRV_PCM_HW_PARAM_RATE,
1608 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1609 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1610 SNDRV_PCM_HW_PARAM_TICK_TIME,
1615 for (v = vars; *v != -1; v++) {
1616 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1617 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1619 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1620 if (snd_BUG_ON(err < 0))
1626 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1629 struct snd_pcm_runtime *runtime = substream->runtime;
1630 unsigned long flags;
1631 snd_pcm_stream_lock_irqsave(substream, flags);
1632 if (snd_pcm_running(substream) &&
1633 snd_pcm_update_hw_ptr(substream) >= 0)
1634 runtime->status->hw_ptr %= runtime->buffer_size;
1636 runtime->status->hw_ptr = 0;
1637 snd_pcm_stream_unlock_irqrestore(substream, flags);
1641 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1644 struct snd_pcm_channel_info *info = arg;
1645 struct snd_pcm_runtime *runtime = substream->runtime;
1647 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1651 width = snd_pcm_format_physical_width(runtime->format);
1655 switch (runtime->access) {
1656 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1657 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1658 info->first = info->channel * width;
1659 info->step = runtime->channels * width;
1661 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1662 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1664 size_t size = runtime->dma_bytes / runtime->channels;
1665 info->first = info->channel * size * 8;
1676 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1679 struct snd_pcm_hw_params *params = arg;
1680 snd_pcm_format_t format;
1681 int channels, width;
1683 params->fifo_size = substream->runtime->hw.fifo_size;
1684 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1685 format = params_format(params);
1686 channels = params_channels(params);
1687 width = snd_pcm_format_physical_width(format);
1688 params->fifo_size /= width * channels;
1694 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1695 * @substream: the pcm substream instance
1696 * @cmd: ioctl command
1697 * @arg: ioctl argument
1699 * Processes the generic ioctl commands for PCM.
1700 * Can be passed as the ioctl callback for PCM ops.
1702 * Returns zero if successful, or a negative error code on failure.
1704 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1705 unsigned int cmd, void *arg)
1708 case SNDRV_PCM_IOCTL1_INFO:
1710 case SNDRV_PCM_IOCTL1_RESET:
1711 return snd_pcm_lib_ioctl_reset(substream, arg);
1712 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1713 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1714 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1715 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1720 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1723 * snd_pcm_period_elapsed - update the pcm status for the next period
1724 * @substream: the pcm substream instance
1726 * This function is called from the interrupt handler when the
1727 * PCM has processed the period size. It will update the current
1728 * pointer, wake up sleepers, etc.
1730 * Even if more than one periods have elapsed since the last call, you
1731 * have to call this only once.
1733 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1735 struct snd_pcm_runtime *runtime;
1736 unsigned long flags;
1738 if (PCM_RUNTIME_CHECK(substream))
1740 runtime = substream->runtime;
1742 if (runtime->transfer_ack_begin)
1743 runtime->transfer_ack_begin(substream);
1745 snd_pcm_stream_lock_irqsave(substream, flags);
1746 if (!snd_pcm_running(substream) ||
1747 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1750 if (substream->timer_running)
1751 snd_timer_interrupt(substream->timer, 1);
1753 snd_pcm_stream_unlock_irqrestore(substream, flags);
1754 if (runtime->transfer_ack_end)
1755 runtime->transfer_ack_end(substream);
1756 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1759 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1762 * Wait until avail_min data becomes available
1763 * Returns a negative error code if any error occurs during operation.
1764 * The available space is stored on availp. When err = 0 and avail = 0
1765 * on the capture stream, it indicates the stream is in DRAINING state.
1767 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1768 snd_pcm_uframes_t *availp)
1770 struct snd_pcm_runtime *runtime = substream->runtime;
1771 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1774 snd_pcm_uframes_t avail = 0;
1777 init_waitqueue_entry(&wait, current);
1778 add_wait_queue(&runtime->sleep, &wait);
1780 if (signal_pending(current)) {
1784 set_current_state(TASK_INTERRUPTIBLE);
1785 snd_pcm_stream_unlock_irq(substream);
1786 tout = schedule_timeout(msecs_to_jiffies(10000));
1787 snd_pcm_stream_lock_irq(substream);
1788 switch (runtime->status->state) {
1789 case SNDRV_PCM_STATE_SUSPENDED:
1792 case SNDRV_PCM_STATE_XRUN:
1795 case SNDRV_PCM_STATE_DRAINING:
1799 avail = 0; /* indicate draining */
1801 case SNDRV_PCM_STATE_OPEN:
1802 case SNDRV_PCM_STATE_SETUP:
1803 case SNDRV_PCM_STATE_DISCONNECTED:
1808 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1809 is_playback ? "playback" : "capture");
1814 avail = snd_pcm_playback_avail(runtime);
1816 avail = snd_pcm_capture_avail(runtime);
1817 if (avail >= runtime->control->avail_min)
1821 remove_wait_queue(&runtime->sleep, &wait);
1826 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1828 unsigned long data, unsigned int off,
1829 snd_pcm_uframes_t frames)
1831 struct snd_pcm_runtime *runtime = substream->runtime;
1833 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1834 if (substream->ops->copy) {
1835 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1838 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1839 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1845 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1846 unsigned long data, unsigned int off,
1847 snd_pcm_uframes_t size);
1849 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1851 snd_pcm_uframes_t size,
1853 transfer_f transfer)
1855 struct snd_pcm_runtime *runtime = substream->runtime;
1856 snd_pcm_uframes_t xfer = 0;
1857 snd_pcm_uframes_t offset = 0;
1863 snd_pcm_stream_lock_irq(substream);
1864 switch (runtime->status->state) {
1865 case SNDRV_PCM_STATE_PREPARED:
1866 case SNDRV_PCM_STATE_RUNNING:
1867 case SNDRV_PCM_STATE_PAUSED:
1869 case SNDRV_PCM_STATE_XRUN:
1872 case SNDRV_PCM_STATE_SUSPENDED:
1881 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1882 snd_pcm_uframes_t avail;
1883 snd_pcm_uframes_t cont;
1884 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1885 snd_pcm_update_hw_ptr(substream);
1886 avail = snd_pcm_playback_avail(runtime);
1892 err = wait_for_avail_min(substream, &avail);
1896 frames = size > avail ? avail : size;
1897 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1900 if (snd_BUG_ON(!frames)) {
1901 snd_pcm_stream_unlock_irq(substream);
1904 appl_ptr = runtime->control->appl_ptr;
1905 appl_ofs = appl_ptr % runtime->buffer_size;
1906 snd_pcm_stream_unlock_irq(substream);
1907 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1909 snd_pcm_stream_lock_irq(substream);
1910 switch (runtime->status->state) {
1911 case SNDRV_PCM_STATE_XRUN:
1914 case SNDRV_PCM_STATE_SUSPENDED:
1921 if (appl_ptr >= runtime->boundary)
1922 appl_ptr -= runtime->boundary;
1923 runtime->control->appl_ptr = appl_ptr;
1924 if (substream->ops->ack)
1925 substream->ops->ack(substream);
1930 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1931 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1932 err = snd_pcm_start(substream);
1938 snd_pcm_stream_unlock_irq(substream);
1940 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1943 /* sanity-check for read/write methods */
1944 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1946 struct snd_pcm_runtime *runtime;
1947 if (PCM_RUNTIME_CHECK(substream))
1949 runtime = substream->runtime;
1950 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1952 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1957 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1959 struct snd_pcm_runtime *runtime;
1963 err = pcm_sanity_check(substream);
1966 runtime = substream->runtime;
1967 nonblock = !!(substream->f_flags & O_NONBLOCK);
1969 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1970 runtime->channels > 1)
1972 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1973 snd_pcm_lib_write_transfer);
1976 EXPORT_SYMBOL(snd_pcm_lib_write);
1978 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1980 unsigned long data, unsigned int off,
1981 snd_pcm_uframes_t frames)
1983 struct snd_pcm_runtime *runtime = substream->runtime;
1985 void __user **bufs = (void __user **)data;
1986 int channels = runtime->channels;
1988 if (substream->ops->copy) {
1989 if (snd_BUG_ON(!substream->ops->silence))
1991 for (c = 0; c < channels; ++c, ++bufs) {
1992 if (*bufs == NULL) {
1993 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1996 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1997 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2002 /* default transfer behaviour */
2003 size_t dma_csize = runtime->dma_bytes / channels;
2004 for (c = 0; c < channels; ++c, ++bufs) {
2005 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2006 if (*bufs == NULL) {
2007 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2009 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2010 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2018 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
2020 snd_pcm_uframes_t frames)
2022 struct snd_pcm_runtime *runtime;
2026 err = pcm_sanity_check(substream);
2029 runtime = substream->runtime;
2030 nonblock = !!(substream->f_flags & O_NONBLOCK);
2032 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2034 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2035 nonblock, snd_pcm_lib_writev_transfer);
2038 EXPORT_SYMBOL(snd_pcm_lib_writev);
2040 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
2042 unsigned long data, unsigned int off,
2043 snd_pcm_uframes_t frames)
2045 struct snd_pcm_runtime *runtime = substream->runtime;
2047 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2048 if (substream->ops->copy) {
2049 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2052 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2053 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2059 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
2061 snd_pcm_uframes_t size,
2063 transfer_f transfer)
2065 struct snd_pcm_runtime *runtime = substream->runtime;
2066 snd_pcm_uframes_t xfer = 0;
2067 snd_pcm_uframes_t offset = 0;
2073 snd_pcm_stream_lock_irq(substream);
2074 switch (runtime->status->state) {
2075 case SNDRV_PCM_STATE_PREPARED:
2076 if (size >= runtime->start_threshold) {
2077 err = snd_pcm_start(substream);
2082 case SNDRV_PCM_STATE_DRAINING:
2083 case SNDRV_PCM_STATE_RUNNING:
2084 case SNDRV_PCM_STATE_PAUSED:
2086 case SNDRV_PCM_STATE_XRUN:
2089 case SNDRV_PCM_STATE_SUSPENDED:
2098 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2099 snd_pcm_uframes_t avail;
2100 snd_pcm_uframes_t cont;
2101 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2102 snd_pcm_update_hw_ptr(substream);
2103 avail = snd_pcm_capture_avail(runtime);
2105 if (runtime->status->state ==
2106 SNDRV_PCM_STATE_DRAINING) {
2107 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2114 err = wait_for_avail_min(substream, &avail);
2118 continue; /* draining */
2120 frames = size > avail ? avail : size;
2121 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2124 if (snd_BUG_ON(!frames)) {
2125 snd_pcm_stream_unlock_irq(substream);
2128 appl_ptr = runtime->control->appl_ptr;
2129 appl_ofs = appl_ptr % runtime->buffer_size;
2130 snd_pcm_stream_unlock_irq(substream);
2131 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2133 snd_pcm_stream_lock_irq(substream);
2134 switch (runtime->status->state) {
2135 case SNDRV_PCM_STATE_XRUN:
2138 case SNDRV_PCM_STATE_SUSPENDED:
2145 if (appl_ptr >= runtime->boundary)
2146 appl_ptr -= runtime->boundary;
2147 runtime->control->appl_ptr = appl_ptr;
2148 if (substream->ops->ack)
2149 substream->ops->ack(substream);
2156 snd_pcm_stream_unlock_irq(substream);
2158 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2161 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2163 struct snd_pcm_runtime *runtime;
2167 err = pcm_sanity_check(substream);
2170 runtime = substream->runtime;
2171 nonblock = !!(substream->f_flags & O_NONBLOCK);
2172 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2174 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2177 EXPORT_SYMBOL(snd_pcm_lib_read);
2179 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2181 unsigned long data, unsigned int off,
2182 snd_pcm_uframes_t frames)
2184 struct snd_pcm_runtime *runtime = substream->runtime;
2186 void __user **bufs = (void __user **)data;
2187 int channels = runtime->channels;
2189 if (substream->ops->copy) {
2190 for (c = 0; c < channels; ++c, ++bufs) {
2194 buf = *bufs + samples_to_bytes(runtime, off);
2195 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2199 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2200 for (c = 0; c < channels; ++c, ++bufs) {
2206 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2207 buf = *bufs + samples_to_bytes(runtime, off);
2208 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2215 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2217 snd_pcm_uframes_t frames)
2219 struct snd_pcm_runtime *runtime;
2223 err = pcm_sanity_check(substream);
2226 runtime = substream->runtime;
2227 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2230 nonblock = !!(substream->f_flags & O_NONBLOCK);
2231 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2233 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2236 EXPORT_SYMBOL(snd_pcm_lib_readv);