From: Miles Ramage <miles.ramage@yahoo.com>
To: Nicholas Johnson <nicholas.johnson-opensource@outlook.com.au>,
Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
Jamie Steele <steele.jamie1991@gmail.com>,
Miles Ramage <miles.ramage@yahoo.com>
Subject: [RFC PATCH 5/5] ALSA: quantum: let a second direction attach to a running stream
Date: Tue, 1 Dec 2026 22:22:31 -0600 [thread overview]
Message-ID: <20261202042231.42930-6-miles.ramage@yahoo.com> (raw)
In-Reply-To: <20261202042231.42930-1-miles.ramage@yahoo.com>
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@yahoo.com>
---
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
prev parent reply other threads:[~2026-12-02 4:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-20 8:36 [RFC PATCH 0/1] ALSA: add PreSonus Quantum PCI driver Nicholas Johnson
2026-08-20 8:36 ` [RFC PATCH 1/1] ALSA: pci: " Nicholas Johnson
2026-08-21 12:44 ` [RFC PATCH] " Markus Elfring
2026-08-21 13:00 ` Markus Elfring
2026-08-21 13:34 ` [RFC PATCH 1/1] " Markus Elfring
2026-08-21 14:24 ` [RFC PATCH] " Markus Elfring
2026-08-21 18:10 ` Markus Elfring
2026-08-22 14:20 ` Markus Elfring
2026-08-22 15:21 ` [RFC PATCH 1/1] " Matthew Wilcox
2026-09-18 18:34 ` [RFC] " Markus Elfring
2026-10-24 16:13 ` [RFC PATCH 0/1] ALSA: " Jamie Steele
2026-12-02 4:22 ` [RFC PATCH 0/5] ALSA: quantum: fix high sample rates and duplex attach Miles Ramage
2026-12-02 4:22 ` [RFC PATCH 1/5] ALSA: quantum: send the sample-rate wire value when setting the rate Miles Ramage
2026-12-02 4:22 ` [RFC PATCH 2/5] ALSA: quantum: track the device sample rate correctly Miles Ramage
2026-12-02 4:22 ` [RFC PATCH 3/5] ALSA: quantum: tie the channel count to the sample rate Miles Ramage
2026-12-02 4:22 ` [RFC PATCH 4/5] ALSA: quantum: preallocate enough buffer for two full periods Miles Ramage
2026-12-02 4:22 ` Miles Ramage [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202042231.42930-6-miles.ramage@yahoo.com \
--to=miles.ramage@yahoo.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=nicholas.johnson-opensource@outlook.com.au \
--cc=perex@perex.cz \
--cc=steele.jamie1991@gmail.com \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®