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 1/5] ALSA: quantum: send the sample-rate wire value when setting the rate
Date: Tue,  1 Dec 2026 22:22:27 -0600	[thread overview]
Message-ID: <20261202042231.42930-2-miles.ramage@yahoo.com> (raw)
In-Reply-To: <20261202042231.42930-1-miles.ramage@yahoo.com>

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@yahoo.com>
---
 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


  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     ` Miles Ramage [this message]
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     ` [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-2-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®