23 #include <linux/slab.h>
24 #include <linux/time.h>
26 #include <linux/export.h>
63 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->
silence_filled;
79 ofs = runtime->
status->hw_ptr;
80 frames = new_hw_ptr - ofs;
102 if (substream->
ops->silence) {
104 err = substream->
ops->silence(substream, -1, ofs, transfer);
107 char *hwbuf = runtime->
dma_area + frames_to_bytes(runtime, ofs);
113 if (substream->
ops->silence) {
116 err = substream->
ops->silence(substream, c, ofs, transfer);
122 char *hwbuf = runtime->
dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
133 #ifdef CONFIG_SND_DEBUG
135 char *
name,
size_t len)
137 snprintf(name, len,
"pcmC%dD%d%c:%d",
138 substream->
pcm->card->number,
139 substream->
pcm->device,
140 substream->
stream ?
'c' :
'p',
146 #define XRUN_DEBUG_BASIC (1<<0)
147 #define XRUN_DEBUG_STACK (1<<1)
148 #define XRUN_DEBUG_JIFFIESCHECK (1<<2)
149 #define XRUN_DEBUG_PERIODUPDATE (1<<3)
150 #define XRUN_DEBUG_HWPTRUPDATE (1<<4)
151 #define XRUN_DEBUG_LOG (1<<5)
152 #define XRUN_DEBUG_LOGONCE (1<<6)
154 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
156 #define xrun_debug(substream, mask) \
157 ((substream)->pstr->xrun_debug & (mask))
159 #define xrun_debug(substream, mask) 0
162 #define dump_stack_on_xrun(substream) do { \
163 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
172 snd_pcm_gettime(runtime, (
struct timespec *)&runtime->
status->tstamp);
176 snd_pcm_debug_name(substream, name,
sizeof(name));
182 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
183 #define hw_ptr_error(substream, fmt, args...) \
185 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
186 xrun_log_show(substream); \
187 if (printk_ratelimit()) { \
188 snd_printd("PCM: " fmt, ##args); \
190 dump_stack_on_xrun(substream); \
194 #define XRUN_LOG_CNT 10
196 struct hwptr_log_entry {
206 struct snd_pcm_hwptr_log {
209 struct hwptr_log_entry
entries[XRUN_LOG_CNT];
216 struct snd_pcm_hwptr_log *
log = runtime->hwptr_log;
217 struct hwptr_log_entry *
entry;
223 runtime->hwptr_log =
log;
228 entry = &log->entries[log->idx];
234 entry->old_hw_ptr = runtime->
status->hw_ptr;
236 log->idx = (log->idx + 1) % XRUN_LOG_CNT;
241 struct snd_pcm_hwptr_log *log = substream->
runtime->hwptr_log;
242 struct hwptr_log_entry *
entry;
251 snd_pcm_debug_name(substream, name,
sizeof(name));
252 for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
253 entry = &log->entries[
idx];
254 if (entry->period_size == 0)
256 snd_printd(
"hwptr log: %s: %sj=%lu, pos=%ld/%ld/%ld, "
258 name, entry->in_interrupt ?
"[Q] " :
"",
260 (
unsigned long)entry->pos,
261 (
unsigned long)entry->period_size,
262 (
unsigned long)entry->buffer_size,
263 (
unsigned long)entry->old_hw_ptr,
264 (
unsigned long)entry->hw_ptr_base);
273 #define hw_ptr_error(substream, fmt, args...) do { } while (0)
274 #define xrun_log(substream, pos, in_interrupt) do { } while (0)
275 #define xrun_log_show(substream) do { } while (0)
285 avail = snd_pcm_playback_avail(runtime);
287 avail = snd_pcm_capture_avail(runtime);
301 if (runtime->
twake) {
302 if (avail >= runtime->
twake)
304 }
else if (avail >= runtime->
control->avail_min)
310 unsigned int in_interrupt)
316 unsigned long jdelta;
317 unsigned long curr_jiffies;
320 old_hw_ptr = runtime->
status->hw_ptr;
328 pos = substream->
ops->pointer(substream);
331 snd_pcm_gettime(runtime, (
struct timespec *)&curr_tstamp);
338 if (printk_ratelimit()) {
340 snd_pcm_debug_name(substream, name,
sizeof(name));
343 "buffer size = %ld, period size = %ld\n",
351 xrun_log(substream, pos, in_interrupt);
353 new_hw_ptr = hw_base +
pos;
358 if (delta > new_hw_ptr) {
365 new_hw_ptr = hw_base +
pos;
372 if (new_hw_ptr < old_hw_ptr) {
376 new_hw_ptr = hw_base +
pos;
379 delta = new_hw_ptr - old_hw_ptr;
385 snd_pcm_debug_name(substream, name,
sizeof(name));
387 "hwptr=%ld/%ld/%ld/%ld\n",
388 in_interrupt ?
"period" :
"hwptr",
393 (
unsigned long)delta,
394 (
unsigned long)old_hw_ptr,
395 (
unsigned long)new_hw_ptr,
406 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
408 hdelta = jdelta - delta *
HZ / runtime->
rate;
410 while (hdelta > xrun_threshold) {
415 new_hw_ptr = hw_base +
pos;
424 "Unexpected hw_pointer value %s"
425 "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
427 in_interrupt ?
"[Q] " :
"[P]",
428 substream->
stream, (
long)pos,
429 (
long)new_hw_ptr, (
long)old_hw_ptr);
435 goto no_jiffies_check;
442 goto no_jiffies_check;
444 if (hdelta < runtime->
delay)
445 goto no_jiffies_check;
446 hdelta -= runtime->
delay;
448 if (((hdelta *
HZ) / runtime->
rate) > jdelta + HZ/100) {
453 new_hw_ptr = old_hw_ptr;
459 if (new_hw_ptr >= runtime->
boundary)
465 "hw_ptr skipping! %s"
466 "(pos=%ld, delta=%ld, period=%ld, "
467 "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
468 in_interrupt ?
"[Q] " :
"",
469 (
long)pos, (
long)hdelta,
471 ((hdelta * HZ) / runtime->
rate), hw_base,
472 (
unsigned long)old_hw_ptr,
473 (
unsigned long)new_hw_ptr);
476 hw_base = new_hw_ptr - (new_hw_ptr % runtime->
buffer_size);
481 "Lost interrupts? %s"
482 "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
484 in_interrupt ?
"[Q] " :
"",
485 substream->
stream, (
long)delta,
491 if (runtime->
status->hw_ptr == new_hw_ptr)
508 runtime->
status->hw_ptr = new_hw_ptr;
511 runtime->
status->tstamp = curr_tstamp;
519 return snd_pcm_update_hw_ptr0(substream, 0);
535 for (substream = stream->
substream; substream !=
NULL; substream = substream->
next)
551 runtime->
sync.
id32[0] = substream->
pcm->card->number;
563 static inline unsigned int div32(
unsigned int a,
unsigned int b,
574 static inline unsigned int div_down(
unsigned int a,
unsigned int b)
581 static inline unsigned int div_up(
unsigned int a,
unsigned int b)
593 static inline unsigned int mul(
unsigned int a,
unsigned int b)
602 static inline unsigned int muldiv32(
unsigned int a,
unsigned int b,
603 unsigned int c,
unsigned int *r)
605 u_int64_t
n = (u_int64_t) a * b;
611 n = div_u64_rem(n, c, r);
666 if (snd_interval_checkempty(i)) {
667 snd_interval_none(i);
679 if (snd_interval_single(i))
688 static int snd_interval_refine_last(
struct snd_interval *i)
692 if (snd_interval_single(i))
704 snd_interval_none(c);
729 snd_interval_none(c);
765 snd_interval_none(c);
769 c->
min = muldiv32(a->
min, b->
min, k, &r);
771 c->
max = muldiv32(a->
max, b->
max, k, &r);
796 snd_interval_none(c);
800 c->
min = muldiv32(a->
min, k, b->
max, &r);
803 c->
max = muldiv32(a->
max, k, b->
min, &r);
830 unsigned int rats_count,
struct snd_ratnum *rats,
831 unsigned int *nump,
unsigned int *denp)
833 unsigned int best_num, best_den;
838 unsigned int result_num, result_den;
841 best_num = best_den = best_diff = 0;
842 for (k = 0; k < rats_count; ++
k) {
843 unsigned int num = rats[
k].
num;
845 unsigned int q = i->
min;
849 den = div_up(num, q);
850 if (den < rats[k].den_min)
852 if (den > rats[k].den_max)
856 r = (den - rats[
k].
den_min) % rats[k].den_step;
860 diff = num - q *
den;
864 diff * best_den < best_diff * den) {
874 t.
min = div_down(best_num, best_den);
875 t.
openmin = !!(best_num % best_den);
877 result_num = best_num;
878 result_diff = best_diff;
879 result_den = best_den;
880 best_num = best_den = best_diff = 0;
881 for (k = 0; k < rats_count; ++
k) {
882 unsigned int num = rats[
k].
num;
884 unsigned int q = i->
max;
890 den = div_down(num, q);
891 if (den > rats[k].den_max)
893 if (den < rats[k].den_min)
897 r = (den - rats[
k].
den_min) % rats[k].den_step;
901 diff = q * den - num;
905 diff * best_den < best_diff * den) {
915 t.
max = div_up(best_num, best_den);
916 t.
openmax = !!(best_num % best_den);
922 if (snd_interval_single(i)) {
923 if (best_diff * result_den < result_diff * best_den) {
924 result_num = best_num;
925 result_den = best_den;
948 unsigned int rats_count,
struct snd_ratden *rats,
949 unsigned int *nump,
unsigned int *denp)
951 unsigned int best_num, best_diff, best_den;
956 best_num = best_den = best_diff = 0;
957 for (k = 0; k < rats_count; ++
k) {
959 unsigned int den = rats[
k].
den;
960 unsigned int q = i->
min;
963 if (num > rats[k].num_max)
965 if (num < rats[k].num_min)
969 r = (num - rats[
k].
num_min) % rats[k].num_step;
973 diff = num - q *
den;
975 diff * best_den < best_diff * den) {
985 t.min = div_down(best_num, best_den);
986 t.openmin = !!(best_num % best_den);
988 best_num = best_den = best_diff = 0;
989 for (k = 0; k < rats_count; ++
k) {
991 unsigned int den = rats[
k].
den;
992 unsigned int q = i->
max;
995 if (num < rats[k].num_min)
997 if (num > rats[k].num_max)
1001 r = (num - rats[
k].
num_min) % rats[k].num_step;
1005 diff = q * den - num;
1006 if (best_num == 0 ||
1007 diff * best_den < best_diff * den) {
1013 if (best_den == 0) {
1017 t.max = div_up(best_num, best_den);
1018 t.openmax = !!(best_num % best_den);
1024 if (snd_interval_single(i)) {
1047 const unsigned int *
list,
unsigned int mask)
1056 snd_interval_any(&list_range);
1059 for (k = 0; k <
count; k++) {
1060 if (mask && !(mask & (1 << k)))
1062 if (!snd_interval_test(i, list[k]))
1064 list_range.
min =
min(list_range.
min, list[k]);
1065 list_range.
max =
max(list_range.
max, list[k]);
1076 n = (i->
min -
min) % step;
1081 n = (i->
max -
min) % step;
1086 if (snd_interval_checkempty(i)) {
1118 unsigned int new_rules = constrs->
rules_all + 16;
1119 new = kcalloc(new_rules,
sizeof(*c),
GFP_KERNEL);
1124 if (constrs->
rules) {
1129 constrs->
rules =
new;
1167 struct snd_mask *maskp = constrs_mask(constrs, var);
1170 if (*maskp->
bits == 0)
1187 struct snd_mask *maskp = constrs_mask(constrs, var);
1191 if (! maskp->
bits[0] && ! maskp->
bits[1])
1206 return snd_interval_setinteger(constrs_interval(constrs, var));
1221 unsigned int min,
unsigned int max)
1257 snd_pcm_hw_rule_list, (
void *)l,
1267 unsigned int num = 0, den = 0;
1291 snd_pcm_hw_rule_ratnums, r,
1301 unsigned int num = 0, den = 0;
1302 int err = snd_interval_ratden(hw_param_interval(params, rule->
var),
1324 snd_pcm_hw_rule_ratdens, r,
1334 int width = l & 0xffff;
1335 unsigned int msbits = l >> 16;
1337 if (snd_interval_single(i) && snd_interval_value(i) ==
width)
1352 unsigned int msbits)
1354 unsigned long l = (msbits << 16) | width;
1356 snd_pcm_hw_rule_msbits,
1366 unsigned long step = (
unsigned long) rule->
private;
1367 return snd_interval_step(hw_param_interval(params, rule->
var), 0,
step);
1383 snd_pcm_hw_rule_step, (
void *) step,
1391 static unsigned int pow2_sizes[] = {
1392 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1393 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1394 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1395 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1412 snd_pcm_hw_rule_pow2,
NULL,
1434 unsigned int base_rate)
1438 snd_pcm_hw_rule_noresample_func,
1447 if (hw_is_mask(var)) {
1448 snd_mask_any(hw_param_mask(params, var));
1449 params->
cmask |= 1 << var;
1450 params->
rmask |= 1 << var;
1453 if (hw_is_interval(var)) {
1454 snd_interval_any(hw_param_interval(params, var));
1455 params->
cmask |= 1 << var;
1456 params->
rmask |= 1 << var;
1465 memset(params, 0,
sizeof(*params));
1467 _snd_pcm_hw_param_any(params, k);
1469 _snd_pcm_hw_param_any(params, k);
1487 if (hw_is_mask(var)) {
1488 const struct snd_mask *
mask = hw_param_mask_c(params, var);
1489 if (!snd_mask_single(mask))
1493 return snd_mask_value(mask);
1495 if (hw_is_interval(var)) {
1496 const struct snd_interval *i = hw_param_interval_c(params, var);
1497 if (!snd_interval_single(i))
1501 return snd_interval_value(i);
1511 if (hw_is_mask(var)) {
1512 snd_mask_none(hw_param_mask(params, var));
1513 params->
cmask |= 1 << var;
1514 params->
rmask |= 1 << var;
1515 }
else if (hw_is_interval(var)) {
1516 snd_interval_none(hw_param_interval(params, var));
1517 params->
cmask |= 1 << var;
1518 params->
rmask |= 1 << var;
1530 if (hw_is_mask(var))
1531 changed = snd_mask_refine_first(hw_param_mask(params, var));
1532 else if (hw_is_interval(var))
1533 changed = snd_interval_refine_first(hw_param_interval(params, var));
1537 params->
cmask |= 1 << var;
1538 params->
rmask |= 1 << var;
1559 int changed = _snd_pcm_hw_param_first(params, var);
1562 if (params->
rmask) {
1576 if (hw_is_mask(var))
1577 changed = snd_mask_refine_last(hw_param_mask(params, var));
1578 else if (hw_is_interval(var))
1579 changed = snd_interval_refine_last(hw_param_interval(params, var));
1583 params->
cmask |= 1 << var;
1584 params->
rmask |= 1 << var;
1605 int changed = _snd_pcm_hw_param_last(params, var);
1608 if (params->
rmask) {
1631 static int vars[] = {
1644 for (v = vars; *v != -1; v++) {
1659 unsigned long flags;
1661 if (snd_pcm_running(substream) &&
1665 runtime->
status->hw_ptr = 0;
1684 switch (runtime->
access) {
1734 unsigned int cmd,
void *arg)
1740 return snd_pcm_lib_ioctl_reset(substream, arg);
1742 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1744 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1765 unsigned long flags;
1775 if (!snd_pcm_running(substream) ||
1776 snd_pcm_update_hw_ptr0(substream, 1) < 0)
1806 init_waitqueue_entry(&wait,
current);
1814 if (runtime->
rate) {
1816 wait_time =
max(t, wait_time);
1822 if (signal_pending(
current)) {
1835 avail = snd_pcm_playback_avail(runtime);
1837 avail = snd_pcm_capture_avail(runtime);
1838 if (avail >= runtime->
twake)
1840 snd_pcm_stream_unlock_irq(substream);
1844 snd_pcm_stream_lock_irq(substream);
1846 switch (runtime->
status->state) {
1866 snd_printd(
"%s write error (DMA or IRQ trouble?)\n",
1867 is_playback ?
"playback" :
"capture");
1881 unsigned long data,
unsigned int off,
1886 char __user *
buf = (
char __user *) data + frames_to_bytes(runtime, off);
1887 if (substream->
ops->copy) {
1888 if ((err = substream->
ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1891 char *hwbuf = runtime->
dma_area + frames_to_bytes(runtime, hwoff);
1892 if (
copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1899 unsigned long data,
unsigned int off,
1917 snd_pcm_stream_lock_irq(substream);
1918 switch (runtime->
status->state) {
1937 avail = snd_pcm_playback_avail(runtime);
1947 runtime->
control->avail_min ? : 1);
1948 err = wait_for_avail(substream, &avail);
1952 frames = size > avail ? avail :
size;
1958 snd_pcm_stream_unlock_irq(substream);
1961 appl_ptr = runtime->
control->appl_ptr;
1963 snd_pcm_stream_unlock_irq(substream);
1964 err = transfer(substream, appl_ofs, data, offset, frames);
1965 snd_pcm_stream_lock_irq(substream);
1968 switch (runtime->
status->state) {
1982 if (substream->
ops->ack)
1983 substream->
ops->ack(substream);
1998 if (xfer > 0 && err >= 0)
2000 snd_pcm_stream_unlock_irq(substream);
2024 err = pcm_sanity_check(substream);
2033 return snd_pcm_lib_write1(substream, (
unsigned long)buf, size, nonblock,
2034 snd_pcm_lib_write_transfer);
2041 unsigned long data,
unsigned int off,
2049 if (substream->
ops->copy) {
2053 if (*bufs ==
NULL) {
2054 if ((err = substream->
ops->silence(substream, c, hwoff, frames)) < 0)
2057 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2058 if ((err = substream->
ops->copy(substream, c, hwoff, buf, frames)) < 0)
2066 char *hwbuf = runtime->
dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2067 if (*bufs ==
NULL) {
2070 char __user *buf = *bufs + samples_to_bytes(runtime, off);
2071 if (
copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2087 err = pcm_sanity_check(substream);
2095 return snd_pcm_lib_write1(substream, (
unsigned long)bufs, frames,
2096 nonblock, snd_pcm_lib_writev_transfer);
2103 unsigned long data,
unsigned int off,
2108 char __user *buf = (
char __user *) data + frames_to_bytes(runtime, off);
2109 if (substream->
ops->copy) {
2110 if ((err = substream->
ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2113 char *hwbuf = runtime->
dma_area + frames_to_bytes(runtime, hwoff);
2114 if (
copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2135 snd_pcm_stream_lock_irq(substream);
2136 switch (runtime->
status->state) {
2162 avail = snd_pcm_capture_avail(runtime);
2167 if (runtime->
status->state ==
2177 runtime->
control->avail_min ? : 1);
2178 err = wait_for_avail(substream, &avail);
2184 frames = size > avail ? avail :
size;
2190 snd_pcm_stream_unlock_irq(substream);
2193 appl_ptr = runtime->
control->appl_ptr;
2195 snd_pcm_stream_unlock_irq(substream);
2196 err = transfer(substream, appl_ofs, data, offset, frames);
2197 snd_pcm_stream_lock_irq(substream);
2200 switch (runtime->
status->state) {
2214 if (substream->
ops->ack)
2215 substream->
ops->ack(substream);
2224 if (xfer > 0 && err >= 0)
2226 snd_pcm_stream_unlock_irq(substream);
2236 err = pcm_sanity_check(substream);
2243 return snd_pcm_lib_read1(substream, (
unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2250 unsigned long data,
unsigned int off,
2258 if (substream->
ops->copy) {
2263 buf = *bufs + samples_to_bytes(runtime, off);
2264 if ((err = substream->
ops->copy(substream, c, hwoff, buf, frames)) < 0)
2275 hwbuf = runtime->
dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2276 buf = *bufs + samples_to_bytes(runtime, off);
2277 if (
copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2292 err = pcm_sanity_check(substream);
2302 return snd_pcm_lib_read1(substream, (
unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2355 static bool valid_chmap_channels(
const struct snd_pcm_chmap *info,
int ch)
2362 static int pcm_chmap_ctl_info(
struct snd_kcontrol *kcontrol,
2378 static int pcm_chmap_ctl_get(
struct snd_kcontrol *kcontrol,
2382 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->
id);
2388 substream = snd_pcm_chmap_substream(info, idx);
2392 sizeof(ucontrol->
value.integer.value));
2398 valid_chmap_channels(info, map->
channels)) {
2399 for (i = 0; i < map->
channels; i++)
2400 ucontrol->
value.integer.value[i] = map->
map[i];
2410 static int pcm_chmap_ctl_tlv(
struct snd_kcontrol *kcontrol,
int op_flag,
2411 unsigned int size,
unsigned int __user *
tlv)
2428 if (!valid_chmap_channels(info, map->
channels))
2438 if (size < chs_bytes)
2442 for (c = 0; c < map->
channels; c++) {
2453 static void pcm_chmap_ctl_private_free(
struct snd_kcontrol *kcontrol)
2475 unsigned long private_value,
2484 .info = pcm_chmap_ctl_info,
2485 .get = pcm_chmap_ctl_get,
2486 .tlv.c = pcm_chmap_ctl_tlv,
2495 info->
chmap = chmap;
2498 knew.
name =
"Playback Channel Map";
2500 knew.
name =
"Capture Channel Map";
2509 info->
kctl->private_free = pcm_chmap_ctl_private_free;