[RFC PATCH 1/5] ALSA: quantum: send the sample-rate wire value when setting the rate
From: Miles Ramage
Date: Tue Dec 01 2026 - 23:23:39 EST
quantum_set_sample_rate() sends the rate in Hz as a single 32-bit word
and expects a sample-rate reply. The device expects the clock source
and the sample-rate wire value (1 = 44.1 kHz ... 6 = 192 kHz), two
little-endian 32-bit words, and answers with a status word.
A QEMU trace of the vendor Windows driver supports this: its set-rate
request is 16 bytes long (an 8-byte payload), not 12, and is answered
by a 12-byte status message about 210-250 ms later.
Send the two-word request, check the status, and read the rate back
with TCI_CTRL_GET_SAMPLE_RATE before caching it. Mark the cached rate
unknown while a change is in progress, so that a failed change is
retried rather than skipped on the next hw_params.
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@xxxxxxxxx>
---
sound/pci/quantum/quantum.h | 29 ++++++++++++
sound/pci/quantum/quantum_tci.c | 81 +++++++++++++++++++++++++++++++--
2 files changed, 105 insertions(+), 5 deletions(-)
diff --git a/sound/pci/quantum/quantum.h b/sound/pci/quantum/quantum.h
index 822c1f4..2c995b6 100644
--- a/sound/pci/quantum/quantum.h
+++ b/sound/pci/quantum/quantum.h
@@ -105,6 +105,32 @@ enum quantum_clk_source {
CLK_SOURCE_EXTERNAL_ADAT2 = 4,
};
+/*
+ * TCI sample-rate wire values, used by TCI_CTRL_SET_SAMPLE_RATE requests and
+ * TCI_CTRL_RSP_SAMPLE_RATE replies. Zero means no valid rate.
+ */
+static inline u32 quantum_rate_to_wire(unsigned int rate)
+{
+ switch (rate) {
+ case 44100: return 1;
+ case 48000: return 2;
+ case 88200: return 3;
+ case 96000: return 4;
+ case 176400: return 5;
+ case 192000: return 6;
+ default: return 0;
+ }
+}
+
+static inline unsigned int quantum_wire_to_rate(u32 wire)
+{
+ static const unsigned int rates[] = {
+ 0, 44100, 48000, 88200, 96000, 176400, 192000,
+ };
+
+ return wire < ARRAY_SIZE(rates) ? rates[wire] : 0;
+}
+
struct tci_header {
__le16 length;
u8 channel;
@@ -384,6 +410,9 @@ void quantum_midi_receive(struct quantum_chip *chip, const u8 *payload,
size_t payload_len);
void quantum_midi_tx_work_handler(struct work_struct *work);
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_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_tci.c b/sound/pci/quantum/quantum_tci.c
index 59a697b..80dde86 100644
--- a/sound/pci/quantum/quantum_tci.c
+++ b/sound/pci/quantum/quantum_tci.c
@@ -398,30 +398,101 @@ int quantum_get_serial_number(struct quantum_chip *chip)
return 0;
}
-/* Set the sample rate */
+/* Read the device and clock sample rates for the current clock source */
+
+int quantum_get_sample_rate(struct quantum_chip *chip,
+ unsigned int *device_rate,
+ unsigned int *clock_rate)
+{
+ __le32 req = cpu_to_le32(chip->clock_source + 1);
+ __le32 reply[2] = { 0 };
+ size_t len = sizeof(reply);
+ int ret;
+
+ if (chip->tci_fatal_error)
+ return -EIO;
+
+ ret = quantum_tci_send_wait(chip, TCI_CHAN_CTRL,
+ TCI_CTRL_GET_SAMPLE_RATE,
+ TCI_CTRL_RSP_SAMPLE_RATE,
+ &req, sizeof(req), reply, &len, 1000);
+ if (ret < 0) {
+ dev_err(&chip->pci->dev, "Get sample rate failed: %d\n", ret);
+ return ret;
+ }
+ if (len != sizeof(reply))
+ return -EPROTO;
+
+ *device_rate = quantum_wire_to_rate(le32_to_cpu(reply[0]));
+ *clock_rate = quantum_wire_to_rate(le32_to_cpu(reply[1]));
+ if (!*device_rate)
+ return -EPROTO;
+
+ return 0;
+}
+
+/*
+ * Set the sample rate. The request carries the clock-source and sample-rate
+ * wire values and is answered with a status word, zero on success. The
+ * device takes roughly 250 ms to answer. Read the rate back afterwards
+ * rather than trusting the status alone.
+ */
int quantum_set_sample_rate(struct quantum_chip *chip, unsigned int rate)
{
- __le32 val = cpu_to_le32(rate);
+ struct {
+ __le32 clock_source;
+ __le32 rate;
+ } __packed req;
+ __le32 status = cpu_to_le32(U32_MAX);
+ size_t len = sizeof(status);
+ unsigned int device_rate, clock_rate;
+ u32 wire = quantum_rate_to_wire(rate);
int ret;
if (chip->tci_fatal_error)
return -EIO;
+ if (!wire)
+ return -EINVAL;
+
if (chip->sample_rate == rate)
return 0;
+ req.clock_source = cpu_to_le32(chip->clock_source + 1);
+ req.rate = cpu_to_le32(wire);
+
+ /* From here on the device rate is unknown until it is read back. */
+ chip->sample_rate = 0;
+
ret = quantum_tci_send_wait(chip, TCI_CHAN_CTRL,
TCI_CTRL_SET_SAMPLE_RATE,
- TCI_CTRL_RSP_SAMPLE_RATE,
- &val, sizeof(val), NULL, NULL, 1000);
+ TCI_RSP_STATUS,
+ &req, sizeof(req), &status, &len, 1000);
if (ret < 0) {
dev_err(&chip->pci->dev, "Set sample rate failed: %d\n", ret);
return ret;
}
+ if (len != sizeof(status) || le32_to_cpu(status)) {
+ dev_err(&chip->pci->dev,
+ "Set sample rate to %u Hz rejected: status 0x%08x\n",
+ rate, le32_to_cpu(status));
+ return -EIO;
+ }
+
+ ret = quantum_get_sample_rate(chip, &device_rate, &clock_rate);
+ if (ret < 0)
+ return ret;
+ if (device_rate != rate) {
+ dev_err(&chip->pci->dev,
+ "Sample rate read back as %u Hz, expected %u Hz\n",
+ device_rate, rate);
+ return -EIO;
+ }
chip->sample_rate = rate;
- dev_dbg(&chip->pci->dev, "sample rate set to %u Hz\n", rate);
+ dev_dbg(&chip->pci->dev, "sample rate set to %u Hz (clock %u Hz)\n",
+ rate, clock_rate);
return 0;
}
--
2.56.1