mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 3/5] ALSA: quantum: tie the channel count to the sample rate
Date: Tue,  1 Dec 2026 22:22:29 -0600	[thread overview]
Message-ID: <20261202042231.42930-4-miles.ramage@yahoo.com> (raw)
In-Reply-To: <20261202042231.42930-1-miles.ramage@yahoo.com>

The device narrows its frame above 48 kHz. QUANTUM_REG_CHANNEL_COUNTS
reads 0x1a1a at 44.1/48 kHz, and a QEMU trace of the vendor Windows
driver shows 0x1212 at 96 kHz and 0x0808 at 192 kHz: 26, 18 and 8
channels per direction. The vendor driver streamed at those widths.

quantum_pcm_open() accepts only 26 channels and fails with -EIO when
the device reports anything else, so the higher bands cannot be used,
and a device left at 96 kHz cannot be opened at all.

Advertise 8 to 26 channels and add hw rules that tie channels and rate
together. Reject a mismatched pair in hw_params, and wait for the
channel-count register to match the new band after a rate change
before the DMA buffers are sized from it.

Link: https://github.com/jamie-steele/presonus-quantum-linux/pull/54
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Miles Ramage <miles.ramage@yahoo.com>
---
 sound/pci/quantum/quantum.h     | 19 +++++++
 sound/pci/quantum/quantum_pcm.c | 95 ++++++++++++++++++++++++---------
 sound/pci/quantum/quantum_tci.c | 29 ++++++++++
 3 files changed, 117 insertions(+), 26 deletions(-)

diff --git a/sound/pci/quantum/quantum.h b/sound/pci/quantum/quantum.h
index 2c995b6..d2d1527 100644
--- a/sound/pci/quantum/quantum.h
+++ b/sound/pci/quantum/quantum.h
@@ -131,6 +131,24 @@ static inline unsigned int quantum_wire_to_rate(u32 wire)
 	return wire < ARRAY_SIZE(rates) ? rates[wire] : 0;
 }
 
+/*
+ * Channels per direction in each speed band, as reported by
+ * QUANTUM_REG_CHANNEL_COUNTS once the device runs at that rate.
+ */
+#define QUANTUM_CHANNELS_MIN 8
+#define QUANTUM_CHANNELS_MAX 26
+
+static inline unsigned int quantum_rate_channels(unsigned int rate)
+{
+	if (!rate)
+		return 0;
+	if (rate > 96000)
+		return 8;
+	if (rate > 48000)
+		return 18;
+	return 26;
+}
+
 struct tci_header {
 	__le16 length;
 	u8 channel;
@@ -413,6 +431,7 @@ int quantum_set_sample_rate(struct quantum_chip *chip, unsigned int rate);
 int quantum_get_sample_rate(struct quantum_chip *chip,
 			    unsigned int *device_rate,
 			    unsigned int *clock_rate);
+int quantum_wait_channel_counts(struct quantum_chip *chip, unsigned int rate);
 int quantum_set_clock_source(struct quantum_chip *chip, unsigned int source);
 int quantum_get_clock_source(struct quantum_chip *chip, unsigned int *source);
 int quantum_get_serial_number(struct quantum_chip *chip);
diff --git a/sound/pci/quantum/quantum_pcm.c b/sound/pci/quantum/quantum_pcm.c
index 857d741..5575072 100644
--- a/sound/pci/quantum/quantum_pcm.c
+++ b/sound/pci/quantum/quantum_pcm.c
@@ -1,13 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (C) 2026 Nicholas Johnson */
+#include <sound/pcm_params.h>
 #include "quantum.h"
 
-static const unsigned int quantum_channel_counts[] = { 26 };
-static const struct snd_pcm_hw_constraint_list quantum_channel_list = {
-	.count = ARRAY_SIZE(quantum_channel_counts),
-	.list = quantum_channel_counts,
-};
-
 static const unsigned int quantum_rates[] = {
 	44100, 48000, 88200, 96000, 176400, 192000,
 };
@@ -49,12 +44,55 @@ static int quantum_pcm_hw_rule_period_size(struct snd_pcm_hw_params *params,
 	return snd_interval_list(p, count, list, 0);
 }
 
+/*
+ * The frame width depends on the speed band: 26 channels per direction up
+ * to 48 kHz, 18 up to 96 kHz and 8 up to 192 kHz. Tie channels and rate
+ * together in both directions.
+ */
+static int quantum_pcm_hw_rule_channels(struct snd_pcm_hw_params *params,
+					struct snd_pcm_hw_rule *rule)
+{
+	struct snd_interval *c = hw_param_interval(params,
+						   SNDRV_PCM_HW_PARAM_CHANNELS);
+	const struct snd_interval *r = hw_param_interval_c(params,
+							   SNDRV_PCM_HW_PARAM_RATE);
+	unsigned int list[ARRAY_SIZE(quantum_rates)];
+	unsigned int count = 0;
+	unsigned int i, ch;
+
+	for (i = 0; i < ARRAY_SIZE(quantum_rates); i++) {
+		if (!snd_interval_test(r, quantum_rates[i]))
+			continue;
+		ch = quantum_rate_channels(quantum_rates[i]);
+		if (!count || list[count - 1] != ch)
+			list[count++] = ch;
+	}
+
+	return snd_interval_list(c, count, list, 0);
+}
+
+static int quantum_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
+				    struct snd_pcm_hw_rule *rule)
+{
+	struct snd_interval *r = hw_param_interval(params,
+						   SNDRV_PCM_HW_PARAM_RATE);
+	const struct snd_interval *c = hw_param_interval_c(params,
+							   SNDRV_PCM_HW_PARAM_CHANNELS);
+	unsigned int list[ARRAY_SIZE(quantum_rates)];
+	unsigned int count = 0;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(quantum_rates); i++)
+		if (snd_interval_test(c, quantum_rate_channels(quantum_rates[i])))
+			list[count++] = quantum_rates[i];
+
+	return snd_interval_list(r, count, list, 0);
+}
+
 static int quantum_pcm_open(struct snd_pcm_substream *ss)
 {
 	struct quantum_chip *chip = ss->pcm->private_data;
 	struct snd_pcm_runtime *runtime = ss->runtime;
-	u32 channel_cfg;
-	u32 channels;
 	int err;
 
 	if (quantum_device_gone(chip))
@@ -66,17 +104,6 @@ static int quantum_pcm_open(struct snd_pcm_substream *ss)
 	if (!chip->tci_ready)
 		return -EAGAIN;
 
-	channel_cfg = quantum_read32(chip, QUANTUM_REG_CHANNEL_COUNTS);
-	channels = ss->stream == SNDRV_PCM_STREAM_PLAYBACK ?
-		   ((channel_cfg >> 8) & 0xff) :
-		   (channel_cfg & 0xff);
-
-	if (!channels)
-		return -EIO;
-
-	if (channels != 26)
-		return -EIO;
-
 	runtime->hw.info = SNDRV_PCM_INFO_MMAP |
 			   SNDRV_PCM_INFO_MMAP_VALID |
 			   SNDRV_PCM_INFO_INTERLEAVED |
@@ -90,21 +117,29 @@ static int quantum_pcm_open(struct snd_pcm_substream *ss)
 			    SNDRV_PCM_RATE_192000;
 	runtime->hw.rate_min = 44100;
 	runtime->hw.rate_max = 192000;
-	runtime->hw.channels_min = channels;
-	runtime->hw.channels_max = channels;
-	runtime->hw.period_bytes_min = 32 * channels * sizeof(s32);
-	runtime->hw.period_bytes_max = 512 * channels * sizeof(s32);
+	runtime->hw.channels_min = QUANTUM_CHANNELS_MIN;
+	runtime->hw.channels_max = QUANTUM_CHANNELS_MAX;
+	runtime->hw.period_bytes_min = 32 * QUANTUM_CHANNELS_MIN * sizeof(s32);
+	runtime->hw.period_bytes_max = 512 * QUANTUM_CHANNELS_MAX * sizeof(s32);
 	runtime->hw.periods_min = 1;
 	runtime->hw.periods_max = 1024;
-	runtime->hw.buffer_bytes_max = 65536 * channels * sizeof(s32);
+	runtime->hw.buffer_bytes_max = 65536 * QUANTUM_CHANNELS_MAX *
+				       sizeof(s32);
 
 	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
 					 &quantum_rate_list);
 	if (err < 0)
 		return err;
 
-	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
-					 &quantum_channel_list);
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
+				  quantum_pcm_hw_rule_channels, NULL,
+				  SNDRV_PCM_HW_PARAM_RATE, -1);
+	if (err < 0)
+		return err;
+
+	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+				  quantum_pcm_hw_rule_rate, NULL,
+				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
 	if (err < 0)
 		return err;
 
@@ -149,6 +184,9 @@ static int quantum_pcm_hw_params(struct snd_pcm_substream *ss,
 	if (!chip->tci_ready)
 		return -EAGAIN;
 
+	if (params_channels(params) != quantum_rate_channels(rate))
+		return -EINVAL;
+
 	pages = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(params));
 	if (pages < 0)
 		return pages;
@@ -186,6 +224,11 @@ static int quantum_pcm_hw_params(struct snd_pcm_substream *ss,
 		goto out_free_pages;
 	}
 
+	/* Also catches a cached rate that no longer matches the device. */
+	err = quantum_wait_channel_counts(chip, rate);
+	if (err < 0)
+		goto out_free_pages;
+
 	err = quantum_allocate_dma_resources(chip, rate,
 					     params_period_size(params),
 					     params_buffer_size(params),
diff --git a/sound/pci/quantum/quantum_tci.c b/sound/pci/quantum/quantum_tci.c
index ee7d6e7..5f5a425 100644
--- a/sound/pci/quantum/quantum_tci.c
+++ b/sound/pci/quantum/quantum_tci.c
@@ -491,12 +491,41 @@ int quantum_set_sample_rate(struct quantum_chip *chip, unsigned int rate)
 		return -EIO;
 	}
 
+	ret = quantum_wait_channel_counts(chip, rate);
+	if (ret < 0)
+		return ret;
+
 	chip->sample_rate = rate;
 	dev_dbg(&chip->pci->dev, "sample rate set to %u Hz (clock %u Hz)\n",
 		rate, clock_rate);
 	return 0;
 }
 
+/*
+ * Wait for QUANTUM_REG_CHANNEL_COUNTS to report the channel count of the
+ * band containing @rate, in both directions.
+ */
+int quantum_wait_channel_counts(struct quantum_chip *chip, unsigned int rate)
+{
+	unsigned int want = quantum_rate_channels(rate);
+	u32 cfg;
+	int i;
+
+	for (i = 0; i < 100; i++) {
+		cfg = quantum_read32(chip, QUANTUM_REG_CHANNEL_COUNTS);
+		if ((cfg & 0xff) == want && ((cfg >> 8) & 0xff) == want)
+			return 0;
+		if (quantum_device_gone(chip))
+			return -ENODEV;
+		usleep_range(1000, 2000);
+	}
+
+	dev_err(&chip->pci->dev,
+		"channel counts 0x%08x do not match %u channels at %u Hz\n",
+		cfg, want, rate);
+	return -EIO;
+}
+
 /* Set the clock source */
 
 int quantum_set_clock_source(struct quantum_chip *chip, unsigned int source)
-- 
2.56.1


  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     ` Miles Ramage [this message]
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     ` [RFC PATCH 5/5] ALSA: quantum: let a second direction attach to a running stream Miles Ramage

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-4-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®