[RFC PATCH 5/5] ALSA: quantum: let a second direction attach to a running stream
From: Miles Ramage
Date: Tue Dec 01 2026 - 23:23:51 EST
When one direction starts while the other is already running, the
joining substream is held at position zero until the IRQ thread sees
the hardware ring at frame zero. The position is sampled in the
threaded handler, a few frames after the interrupt, so it is rarely
exactly zero, and the joining stream can wait indefinitely. ALSA then
times out after 100 ms:
arecord -D hw:Quantum2626 -c 18 -r 96000 -f S32_LE \
--period-size=512 --buffer-size=2048 -d 5 t.wav &
speaker-test -D hw:Quantum2626 -c 18 -r 96000 -F S32_LE -t sine
arecord: pcm_read:2285: read error: Input/output error
snd-quantum: pcm trigger: playback cmd=1 ... configured=0x3 play=1
snd-quantum: pcm trigger: capture cmd=1 ... configured=0x3 play=1 cap=1
snd-quantum: pcm trigger: capture cmd=0 state=3 ... (105 ms later)
Detect the wrap from the cycle counter instead, and start the joining
stream's period accumulator at the frames already past zero.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Miles Ramage <miles.ramage@xxxxxxxxx>
---
sound/pci/quantum/quantum.h | 1 +
sound/pci/quantum/quantum_dma.c | 3 +++
sound/pci/quantum/quantum_main.c | 12 +++++++++---
3 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/sound/pci/quantum/quantum.h b/sound/pci/quantum/quantum.h
index d2d1527..40180ba 100644
--- a/sound/pci/quantum/quantum.h
+++ b/sound/pci/quantum/quantum.h
@@ -278,6 +278,7 @@ struct quantum_chip {
u32 last_dma_pos;
bool dma_position_valid;
u32 dma_cycle_count;
+ bool dma_wrapped; /* last update crossed ring frame zero */
u32 period_frames;
u32 buffer_frames;
u32 current_sample_rate;
diff --git a/sound/pci/quantum/quantum_dma.c b/sound/pci/quantum/quantum_dma.c
index 8d56c29..5ac2fa5 100644
--- a/sound/pci/quantum/quantum_dma.c
+++ b/sound/pci/quantum/quantum_dma.c
@@ -696,6 +696,7 @@ int quantum_start_dma(struct quantum_chip *chip)
*/
chip->last_dma_pos = 0;
chip->dma_cycle_count = 0;
+ chip->dma_wrapped = false;
WRITE_ONCE(chip->dma_position_valid, true);
chip->dma_elapsed_frames = 0;
chip->dma_pending_frames = 0;
@@ -796,6 +797,7 @@ void quantum_update_dma_position(struct quantum_chip *chip)
raw = quantum_read32(chip, QUANTUM_REG_DMA_POSITION);
pos = raw & 0x000fffff;
cycle = raw >> 20;
+ chip->dma_wrapped = false;
if (!chip->dma_ring_frames || pos >= chip->dma_ring_frames) {
dev_warn_ratelimited(&chip->pci->dev,
"invalid DMA position: raw=0x%08x ring=%u\n",
@@ -828,6 +830,7 @@ void quantum_update_dma_position(struct quantum_chip *chip)
raw, delta);
delta = chip->dma_quantum_frames;
}
+ chip->dma_wrapped = cycle_delta != 0;
chip->last_dma_pos = pos;
chip->dma_cycle_count = cycle;
chip->dma_elapsed_frames = delta;
diff --git a/sound/pci/quantum/quantum_main.c b/sound/pci/quantum/quantum_main.c
index c7c73e8..f8efdf6 100644
--- a/sound/pci/quantum/quantum_main.c
+++ b/sound/pci/quantum/quantum_main.c
@@ -426,11 +426,15 @@ static void quantum_process_audio(struct quantum_chip *chip)
* Playback restarted while capture kept the shared DMA
* engine running. Wait until the hardware ring returns
* to frame zero so ALSA and the device agree on the
- * restarted stream's initial position.
+ * restarted stream's initial position. The position is
+ * sampled a few frames late, so detect the wrap from
+ * the cycle counter and count the frames since zero.
*/
chip->playback_period_accum = 0;
- if (!chip->last_dma_pos)
+ if (chip->dma_wrapped) {
chip->playback_attach_pending = false;
+ chip->playback_period_accum = chip->last_dma_pos;
+ }
} else {
chip->playback_period_accum += elapsed;
playback_periods_to_fire =
@@ -447,8 +451,10 @@ static void quantum_process_audio(struct quantum_chip *chip)
if (chip->capture_attach_pending) {
/* Same ring-zero attach logic as playback, above. */
chip->capture_period_accum = 0;
- if (!chip->last_dma_pos)
+ if (chip->dma_wrapped) {
chip->capture_attach_pending = false;
+ chip->capture_period_accum = chip->last_dma_pos;
+ }
} else {
chip->capture_period_accum += elapsed;
capture_periods_to_fire =
--
2.56.1