Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302
@ 2026-11-27 10:40 Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alexey Charkov @ 2026-11-27 10:40 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu,
	Heiko Stuebner
  Cc: Sebastian Reichel, Sebastian Andrzej Siewior, linux-usb,
	linux-arm-kernel, linux-rockchip, linux-kernel, Alexey Charkov,
	stable

The FUSB302 implements PD 2.0, whose Message Type field is four bits wide,
so it decodes the PD 3.0 Get_Source_Cap_Extended (0x11) as a GoodCRC (0x1)
and never acknowledges it. v1 dropped the message, which keeps TCPM in
step with the partner, but leaves partners such as an Apple Studio Display
soft resetting over and over. This version acknowledges it from the driver
instead, resulting in a proper PD 3.0 contract with no resets even with
strict partners.

Patches 1-3 keep the RX FIFO in step, which the rest relies on. Patch 1
resyncs the RX FIFO by CRC after a flush cuts a packet short, patch 2
drains the FIFO on each interrupt, reading only while CC is idle, and
patch 3 reads the FIFO out rather than flush it as PD RX goes on. They
fix behaviour as old as the driver, which also causes resets on attach
with no Get_Source_Cap_Extended involved, so they carry Fixes: and
Cc: stable. Each builds on the one before, so they go to stable together
and in this order.

Patch 4 handles interrupts in the IRQ thread rather than a work item, as
suggested in review of the switch to a threaded handler [4]. This has a
nice side effect of slightly reducing the latency, as it cuts out a
context switch and makes the actual IRQ handling code run at realtime
priority rather than in a normal kworker thread (which sometimes queued
behind other tasks in my testing, causing missed protocol timings).

Patches 5 and 6 are new functionality for PD 3.0 partners. Patch 5 sends
the GoodCRC from the driver, 2.2-2.8ms after the interrupt over a 400kHz
I2C bus. That is too late for strict partners (such as an iPhone 17 Pro
as the source), but still often produces a PD 3.0 contract, because the
partners retry unacknowledged messages three times, and the software ack
sent for the first one lands in the window of one of the retries. Patch 6
is a pile of black magic to make it work reliably in protocol specified
timings, as long as the I2C bus runs at 400kHz or more. It reads the
status and the head of the RX FIFO in one transfer and keeps the GoodCRC
pre-staged in the TX FIFO, all but the TXON. The GoodCRC then goes out
0.4-0.9ms after the interrupt, and gives 100% success rate in PD 3.0
negotiation with the iPhone in my testing, with no resets at all.

Patch 2 was sent on its own before [1] and is folded in here, as the
rest builds on it. The issue Sashiko raised there, an unacknowledged
Get_Source_Cap_Extended reaching TCPM only ahead of the Soft_Reset that
follows it, goes away with patch 5, which reads and answers the message
as it arrives.

The series applies on top of [2] and [3] (also recorded in b4 metadata).

The verbose code comments are there to keep Sashiko from suggesting,
over and over, things that are not true of this chip or the broader setup.
Happy to trim them down if required. Note that I ran the local Sashiko
over Claude Sonnet 5.5 CLI, so it probably raised slightly different
issues than the upstream Sashiko would have (it runs over Gemini 4).

Tested on the FUSB302B ports of two Flipper One boards (RK3576), one
sending Get_Source_Cap_Extended, Ping floods and other messages to the
other at either power role, with RX FIFO flushes forced mid-packet, PD
RX turned on late, and suspend to RAM with interrupts arriving through
it, and against an iPhone 17 Pro, which is the strictest PD 3.0 partner
I have seen so far.

[1] https://lore.kernel.org/r/20261102-fusb302-drain-rxfifo-v1-1-bd9b232ab345@flipper.net
[2] https://lore.kernel.org/r/20261103-husb311-cable-comm-v1-0-6217dabc1a85@flipper.net
[3] https://lore.kernel.org/r/20261123-fusb302-irq-read-error-v2-1-5539a09127d8@flipper.net
[4] https://lore.kernel.org/r/20260312120418.99U0NPWL@linutronix.de

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Changes in v2:
- Acknowledge Get_Source_Cap_Extended from the driver instead of
  dropping it, so the partner sees it delivered (dropped Heikki's Ack,
  as the patch is new), and in time for the partner to accept it (new
  patches 4 and 6)
- Fold in "Drain the RX FIFO on each PD interrupt" [1], now reading only
  while CC is idle (dropped Sebastian's Reviewed-by, as the code changed)
- New patches 1 and 3 to keep the RX FIFO in step, which the rest
  relies on
- Added Fixes: and Cc: stable to patches 1-3
- Rebased onto next-20261124, on top of [2] and [3]
- Link to v1: https://patch.msgid.link/20261102-fusb302-getsourcecap-v1-1-26b5bfea4b2b@flipper.net

---
Alexey Charkov (6):
      usb: typec: fusb302: Find the next message when the RX FIFO is out of step
      usb: typec: fusb302: Drain the RX FIFO on each PD interrupt
      usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on
      usb: typec: fusb302: Handle interrupts in the IRQ thread
      usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver
      usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time

 drivers/usb/typec/tcpm/Kconfig   |    1 +
 drivers/usb/typec/tcpm/fusb302.c | 1036 +++++++++++++++++++++++++++++++++++---
 2 files changed, 955 insertions(+), 82 deletions(-)
---
base-commit: 3dd8c790a7b7027abdd568e36db5e82ffaea7c77
change-id: 20261102-fusb302-getsourcecap-01c38dccb230
prerequisite-change-id: 20261103-husb311-cable-comm-46f2e77e4c18:v1
prerequisite-patch-id: 2ea442b68dfeee84c38efb27749d2f40487d2fff
prerequisite-patch-id: 00acfb9e5b26f7f594c64b6ec87fbc5badcb59ca
prerequisite-patch-id: 398e6c15de4f0c6fe4751176bc4ffe0fd1a4e58f
prerequisite-patch-id: 134a18c9c2e06dc019aba39387bff53c94dc415b
prerequisite-patch-id: b2e7a13203fd30a8c71b71a4856e7cee7a1bef19
prerequisite-change-id: 20261116-fusb302-irq-read-error-8a3ca5ef9ebc:v2
prerequisite-patch-id: b484440b7ee2dc68c6e5561a71804b5de607f178

Best regards,
--  
Alexey Charkov <alchark@flipper.net>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step
  2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
@ 2026-11-27 10:40 ` Alexey Charkov
  2026-11-30 14:06   ` Heikki Krogerus
  2026-11-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on Alexey Charkov
  2 siblings, 1 reply; 5+ messages in thread
From: Alexey Charkov @ 2026-11-27 10:40 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu,
	Heiko Stuebner
  Cc: Sebastian Reichel, Sebastian Andrzej Siewior, linux-usb,
	linux-arm-kernel, linux-rockchip, linux-kernel, Alexey Charkov,
	stable

The chip writes each byte into the RX FIFO as it comes off CC, so a
flush in the middle of a packet leaves the rest of that packet in the
FIFO with no token in front, and the driver reads that tail, padded with
zeros where the FIFO runs dry, as a message. TCPM acts on the junk
header. On a Flipper One attaching as a sink, the flush that turns PD RX
on raced the partner's first message in 2 of about 35 attaches: a
"message" with header 0 made TCPM see a data role mismatch and go to
ERROR_RECOVERY. With the FIFO flushed on purpose 27 times while the
partner sent a Ping every 20ms, 98 junk messages reached TCPM, and it
reset 17 times.

Flushing the FIFO again once the junk is spotted is no cure, as any
message behind the tail may have been acknowledged already, and the
partner never sends it again.

Keep the bytes read from the FIFO in a buffer instead, and scan for a full
valid message, including a CRC that checks out, to avoid interpreting junk
bytes as a start of a message.

In the same test, no junk reaches TCPM now, and only the Pings that the
forced flushes cut short go missing.

Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
Cc: stable@vger.kernel.org
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/usb/typec/tcpm/Kconfig   |   1 +
 drivers/usb/typec/tcpm/fusb302.c | 222 +++++++++++++++++++++++++++++++++------
 2 files changed, 190 insertions(+), 33 deletions(-)

diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
index 00baa7503d45..e551cb63a775 100644
--- a/drivers/usb/typec/tcpm/Kconfig
+++ b/drivers/usb/typec/tcpm/Kconfig
@@ -59,6 +59,7 @@ config TYPEC_FUSB302
 	depends on I2C
 	depends on EXTCON || !EXTCON
 	depends on DRM || DRM=n
+	select CRC32
 	select DRM_AUX_HPD_BRIDGE if DRM_BRIDGE && OF
 	help
 	  The Fairchild FUSB302 Type-C chip driver that works with
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 1cbe272ad686..d2d0e8ffc308 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -6,6 +6,7 @@
  */
 
 #include <drm/bridge/aux-bridge.h>
+#include <linux/crc32.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
@@ -27,6 +28,7 @@
 #include <linux/string.h>
 #include <linux/string_choices.h>
 #include <linux/types.h>
+#include <linux/unaligned.h>
 #include <linux/usb.h>
 #include <linux/usb/typec.h>
 #include <linux/usb/tcpm.h>
@@ -72,6 +74,11 @@ static const u8 rd_mda_value[] = {
 #define LOG_BUFFER_ENTRIES	1024
 #define LOG_BUFFER_ENTRY_SIZE	128
 
+/* An SOP* token, the header, up to seven data objects and the CRC */
+#define FUSB302_RX_MSG_MAX	(1 + 2 + PD_MAX_PAYLOAD * 4 + 4)
+/* The size of the RX FIFO */
+#define FUSB302_RX_FIFO_LEN	80
+
 struct fusb302_chip {
 	struct device *dev;
 	struct i2c_client *i2c_client;
@@ -110,6 +117,17 @@ struct fusb302_chip {
 	bool vbus_on;
 	bool charge_on;
 	bool pd_rx_on;
+	/*
+	 * Bytes read from the RX FIFO but not handled yet: the start of a
+	 * message to read on, or bytes to find the next message in
+	 */
+	u8 rx_buf[FUSB302_RX_MSG_MAX];
+	u8 rx_len;
+	/*
+	 * The next message may not be at the start of the buffer.
+	 * Look for its start first before processing.
+	 */
+	bool rx_resync;
 	bool vbus_present;
 	enum typec_cc_polarity cc_polarity;
 	enum typec_cc_status cc1;
@@ -831,6 +849,17 @@ static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
 				    FUSB_REG_CONTROL1_RX_FLUSH);
 }
 
+/*
+ * Forget whatever was read from the RX FIFO but not handled yet. After a flush
+ * or a reset, the FIFO may go on with the rest of a packet cut short, so look
+ * for the next message then, rather than read on from the first byte.
+ */
+static void fusb302_pd_rx_reset(struct fusb302_chip *chip, bool cut_short)
+{
+	chip->rx_len = 0;
+	chip->rx_resync = cut_short;
+}
+
 static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
 {
 	if (on)
@@ -885,6 +914,7 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
 		fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
 		goto done;
 	}
+	fusb302_pd_rx_reset(chip, true);
 	ret = fusb302_pd_tx_flush(chip);
 	if (ret < 0) {
 		fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
@@ -1477,43 +1507,168 @@ static int fusb302_handle_togdone(struct fusb302_chip *chip)
 
 static int fusb302_pd_reset(struct fusb302_chip *chip)
 {
+	/* What was read of a message before the reset is of no use after it */
+	fusb302_pd_rx_reset(chip, true);
+
 	return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
 				    FUSB_REG_RESET_PD_RESET);
 }
 
-static int fusb302_pd_read_message(struct fusb302_chip *chip,
-				   struct pd_message *msg)
+static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n)
 {
-	int ret = 0;
-	u8 token;
-	u8 crc[4];
-	int len;
+	chip->rx_len -= n;
+	memmove(chip->rx_buf, chip->rx_buf + n, chip->rx_len);
+}
 
-	/* first SOP token */
-	ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
-	if (ret < 0)
-		return ret;
-	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
-				     (u8 *)&msg->header);
-	if (ret < 0)
-		return ret;
-	len = pd_header_cnt_le(msg->header) * 4;
-	/* add 4 to length to include the CRC */
-	if (len > PD_MAX_PAYLOAD * 4) {
-		fusb302_log(chip, "PD message too long %d", len);
-		return -EINVAL;
+static bool fusb302_pd_rx_is_token(u8 byte)
+{
+	byte &= FUSB302_RX_TKN_MASK;
+
+	return byte == FUSB302_RX_TKN_SOP || byte == FUSB302_RX_TKN_SOP1;
+}
+
+/* FIFO bytes taken by the message whose token is at rx_buf[off] */
+static int fusb302_pd_rx_msg_len(struct fusb302_chip *chip, int off)
+{
+	u16 header = get_unaligned_le16(chip->rx_buf + off + 1);
+
+	return 1 + 2 + pd_header_cnt(header) * 4 + 4;
+}
+
+/* The chip keeps the CRC it received after the header and data objects */
+static bool fusb302_pd_rx_crc_ok(struct fusb302_chip *chip, int off, int len)
+{
+	return ~crc32_le(~0U, chip->rx_buf + off + 1, len - 5) ==
+	       get_unaligned_le32(chip->rx_buf + off + len - 4);
+}
+
+/*
+ * Return the offset of the first message past the start of rx_buf that it
+ * holds in full, with its CRC checking out, or 0 if there is none
+ */
+static int fusb302_pd_rx_find(struct fusb302_chip *chip)
+{
+	int off, len;
+
+	for (off = 1; off + 3 <= chip->rx_len; off++) {
+		if (!fusb302_pd_rx_is_token(chip->rx_buf[off]))
+			continue;
+		len = fusb302_pd_rx_msg_len(chip, off);
+		if (off + len <= chip->rx_len &&
+		    fusb302_pd_rx_crc_ok(chip, off, len))
+			return off;
 	}
-	if (len > 0) {
-		ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
-					     (u8 *)msg->payload);
+
+	return 0;
+}
+
+/*
+ * Read up to n more bytes into rx_buf and return how many. When the FIFO is in
+ * step, rx_buf starts with a message the chip has received in full, so read the
+ * rest in one go. Otherwise read one byte at a time while the FIFO holds data,
+ * as a dried out FIFO starts returning zeros instead of received data, and real
+ * bytes will be returned to the next read instead once the device receives them
+ */
+static int fusb302_pd_rx_read(struct fusb302_chip *chip, int n)
+{
+	u8 status1;
+	int ret;
+
+	if (chip->rx_resync) {
+		ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1, &status1);
 		if (ret < 0)
 			return ret;
+		if (status1 & FUSB_REG_STATUS1_RX_EMPTY)
+			return 0;
+		n = 1;
 	}
-	/* another 4 bytes to read CRC out */
-	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
-	if (ret < 0)
+	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, n,
+				     chip->rx_buf + chip->rx_len);
+	if (ret < 0) {
+		/* Some of it may have left the FIFO all the same */
+		chip->rx_resync = true;
 		return ret;
-	fusb302_log(chip, "PD message header: %x", msg->header);
+	}
+	chip->rx_len += n;
+
+	return n;
+}
+
+/*
+ * Read in and handle the next message in the RX FIFO. Return 1 if there was
+ * one, 0 if the FIFO ran out first, or a negative error code.
+ */
+static int fusb302_pd_read_message(struct fusb302_chip *chip)
+{
+	struct pd_message msg = {};
+	int dropped = 0;
+	int len, off, ret;
+	u8 token;
+
+	for (;;) {
+		/*
+		 * Already discarded a full FIFO's worth of junk - leave
+		 * whatever remains to another pass
+		 */
+		if (dropped >= FUSB302_RX_FIFO_LEN) {
+			ret = 0;
+			goto out;
+		}
+		/*
+		 * The chip writes each byte into the FIFO as it comes off CC,
+		 * so a flush in the middle of a packet leaves the rest of it
+		 * there with no token in front. Following bytes may look like a
+		 * valid token without being one, so only trust them if the CRC
+		 * of the resulting message checks out, too. If not, look on
+		 * from the next byte: there may be a next message that the chip
+		 * has already acknowledged, and we must not miss it.
+		 */
+		while (chip->rx_len &&
+		       !fusb302_pd_rx_is_token(chip->rx_buf[0])) {
+			chip->rx_resync = true;
+			fusb302_pd_rx_drop(chip, 1);
+			dropped++;
+		}
+		if (chip->rx_len < 3)
+			len = 3;
+		else
+			len = fusb302_pd_rx_msg_len(chip, 0);
+		if (chip->rx_len < len) {
+			ret = fusb302_pd_rx_read(chip, len - chip->rx_len);
+			if (ret < 0)
+				goto out;
+			if (ret)
+				continue;
+			/*
+			 * The FIFO ran dry, which may be due to a bogus token
+			 * claiming more bytes than will ever come. See if the
+			 * following bytes contain a complete valid message.
+			 */
+			off = fusb302_pd_rx_find(chip);
+			if (!off)
+				goto out;
+			fusb302_pd_rx_drop(chip, off);
+			dropped += off;
+			continue;
+		}
+		if (fusb302_pd_rx_crc_ok(chip, 0, len))
+			break;
+		if (!chip->rx_resync)
+			fusb302_log(chip, "PD message failed CRC check, header: %x",
+				    get_unaligned_le16(chip->rx_buf + 1));
+		chip->rx_resync = true;
+		fusb302_pd_rx_drop(chip, 1);
+		dropped++;
+	}
+
+	chip->rx_resync = false;
+	token = chip->rx_buf[0];
+	memcpy(&msg, chip->rx_buf + 1, len - 5);
+	fusb302_pd_rx_drop(chip, len);
+	len -= 7;
+	ret = 1;
+
+	fusb302_log(chip, "PD message header: %x", msg.header);
 	fusb302_log(chip, "PD message len: %d", len);
 
 	/*
@@ -1526,13 +1681,15 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip,
 	 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
 	 * to check the message type to ensure correct reporting to TCPM.
 	 */
-	if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
+	if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC)
 		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
 	else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1)
-		tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP_PRIME);
+		tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP_PRIME);
 	else
-		tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP);
-
+		tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP);
+out:
+	if (dropped)
+		fusb302_log(chip, "dropped %d bytes from the RX FIFO", dropped);
 	return ret;
 }
 
@@ -1605,7 +1762,6 @@ static void fusb302_irq_work(struct work_struct *work)
 	bool intr_togdone;
 	bool intr_bc_lvl;
 	bool intr_comp_chng;
-	struct pd_message pd_msg;
 
 	mutex_lock(&chip->lock);
 	/* grab a snapshot of intr flags */
@@ -1693,7 +1849,7 @@ static void fusb302_irq_work(struct work_struct *work)
 
 	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
 		fusb302_log(chip, "IRQ: PD tx success");
-		ret = fusb302_pd_read_message(chip, &pd_msg);
+		ret = fusb302_pd_read_message(chip);
 		if (ret < 0) {
 			fusb302_log(chip,
 				    "cannot read in PD message, ret=%d", ret);
@@ -1713,7 +1869,7 @@ static void fusb302_irq_work(struct work_struct *work)
 
 	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
 		fusb302_log(chip, "IRQ: PD sent good CRC");
-		ret = fusb302_pd_read_message(chip, &pd_msg);
+		ret = fusb302_pd_read_message(chip);
 		if (ret < 0) {
 			fusb302_log(chip,
 				    "cannot read in PD message, ret=%d", ret);

-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt
  2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
@ 2026-11-27 10:40 ` Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on Alexey Charkov
  2 siblings, 0 replies; 5+ messages in thread
From: Alexey Charkov @ 2026-11-27 10:40 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu,
	Heiko Stuebner
  Cc: Sebastian Reichel, Sebastian Andrzej Siewior, linux-usb,
	linux-arm-kernel, linux-rockchip, linux-kernel, Alexey Charkov,
	stable

The driver reads one message from the RX FIFO per GCRCSENT or TX_SUCCESS
interrupt, assuming every queued message raises one. The chip, however,
queues some messages without acknowledging them or raising any interrupt,
e.g. it decodes only the low four bits of the message type and mistakes
Get_Source_Cap_Extended (0x11) for a GoodCRC (0x1). From then on every read
returns the message queued before the one that raised the interrupt, so
replies answer the wrong message and a Soft_Reset is never seen. The
partner escalates to a Hard Reset, which an Apple Studio Display repeats
every 1.5s.

Furthermore, two different messages received in quick succession may be
queued without raising separate interrupts, causing all subsequent PD
communications to be permanently out of sync, which usually also leads to
a hard reset eventually.

Read messages until the FIFO reports empty, so that each one is handled on
the event it arrived with, whatever the chip left unannounced before it.

Draining reads past the message that raised the interrupt, though, and may
reach one still coming in: STATUS1.RX_EMPTY clears as soon as the token of
a packet lands in the RX FIFO, with the rest of the packet still on CC,
whereas the driver reads a whole message in one go once it has the header.
Reading ahead of the bytes still to come returns 0x00 for each, as
measured on the FUSB302B of a Flipper One, and those bytes then land with
no token in front, so the message is lost although the chip acknowledges
it. A partner's retry of a message whose GoodCRC it missed, about 1ms
after the first attempt, is one such packet.

So wait for STATUS0.ACTIVITY to clear before reading each message,
reading STATUS1 along with it at the cost of the one register read. With
CC idle, the FIFO only holds packets received in full, and anything new
lands behind them: on the same chip, the token of a packet lands at least
250us after ACTIVITY rises. Once CC is idle and the FIFO is empty, no new
message can appear until the next transaction, so pass all fully validated
messages on to TCPM and discard whatever incomplete remains which might
trail them, because with nothing on CC they will never be completed. If CC
stays busy, look at the FIFO again shortly after so as not to miss a
packet which might not raise GCRCSENT.

Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
Cc: stable@vger.kernel.org
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/usb/typec/tcpm/fusb302.c | 156 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 154 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index d2d0e8ffc308..d57f9ae6621d 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -97,6 +97,9 @@ struct fusb302_chip {
 
 	struct workqueue_struct *wq;
 	struct delayed_work bc_lvl_handler;
+	struct delayed_work rx_recheck;
+	/* Rechecks since the FIFO was last seen empty */
+	unsigned int rx_rechecks;
 
 	/* lock for sharing chip states */
 	struct mutex lock;
@@ -128,6 +131,8 @@ struct fusb302_chip {
 	 * Look for its start first before processing.
 	 */
 	bool rx_resync;
+	/* Only look in rx_buf, as the FIFO has nothing more of what it holds */
+	bool rx_buf_only;
 	bool vbus_present;
 	enum typec_cc_polarity cc_polarity;
 	enum typec_cc_status cc1;
@@ -1574,6 +1579,8 @@ static int fusb302_pd_rx_read(struct fusb302_chip *chip, int n)
 	u8 status1;
 	int ret;
 
+	if (chip->rx_buf_only)
+		return 0;
 	if (chip->rx_resync) {
 		ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1, &status1);
 		if (ret < 0)
@@ -1693,6 +1700,141 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
 	return ret;
 }
 
+/* The 80-byte RX FIFO holds at most 11 messages without data objects */
+#define FUSB302_RX_FIFO_MAX_MSGS	11
+
+/* The longest packet with the GoodCRC to it, and then some */
+#define FUSB302_CC_BUSY_MAX_US		2000
+
+/*
+ * Wait for CC to go quiet, reading STATUS1 along with STATUS0. Return 0 once
+ * it has, 1 if it stays busy, or a negative error code.
+ */
+static int fusb302_wait_cc_idle(struct fusb302_chip *chip, u8 *status1)
+{
+	ktime_t start = ktime_get();
+	u8 status[2];
+	int ret;
+
+	for (;;) {
+		ret = fusb302_i2c_block_read(chip, FUSB_REG_STATUS0, 2, status);
+		if (ret < 0)
+			return ret;
+		if (!(status[0] & FUSB_REG_STATUS0_ACTIVITY))
+			break;
+		if (ktime_us_delta(ktime_get(), start) > FUSB302_CC_BUSY_MAX_US)
+			return 1;
+		/* Leave the I2C bus to others meanwhile */
+		usleep_range(100, 200);
+	}
+	*status1 = status[1];
+
+	return 0;
+}
+
+/*
+ * Read in and handle every message in the RX FIFO. Return 0 once it is empty,
+ * 1 if it may not be, as CC stayed busy or more messages came in meanwhile than
+ * the FIFO holds, or a negative error code.
+ */
+static int fusb302_pd_read_messages(struct fusb302_chip *chip)
+{
+	u8 status1;
+	int ret;
+	int i;
+
+	/*
+	 * Drain the whole RX FIFO rather than one message per interrupt:
+	 * messages the chip does not acknowledge raise no GCRCSENT, and
+	 * reading one message per event would then leave every later read
+	 * returning the message queued before the one that raised it.
+	 */
+	for (i = 0; i < FUSB302_RX_FIFO_MAX_MSGS; i++) {
+		/*
+		 * Wait for CC to go idle to ensure the FIFO only holds packets
+		 * received in full. Otherwise we may eat up the token opening
+		 * the next packet, which is still on the wire now.
+		 */
+		ret = fusb302_wait_cc_idle(chip, &status1);
+		if (ret) {
+			if (ret > 0)
+				fusb302_log(chip, "CC busy, RX FIFO left for later");
+			return ret;
+		}
+		if (status1 & FUSB_REG_STATUS1_RX_EMPTY) {
+			/*
+			 * Nothing more will come: pass on any message still
+			 * held in full, then drop what is left of another.
+			 * Read no further, as what lands in the FIFO from now
+			 * on starts a packet that is still on CC.
+			 */
+			chip->rx_buf_only = true;
+			while (chip->rx_len) {
+				chip->rx_resync = true;
+				ret = fusb302_pd_read_message(chip);
+				if (ret <= 0)
+					break;
+			}
+			chip->rx_buf_only = false;
+			if (ret < 0)
+				return ret;
+			if (chip->rx_len)
+				fusb302_log(chip, "dropped %d bytes from the RX FIFO",
+					    chip->rx_len);
+			fusb302_pd_rx_reset(chip, false);
+			return 0;
+		}
+		ret = fusb302_pd_read_message(chip);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 1;
+}
+
+/* About 200ms of CC staying busy, longer than any PD traffic but noise */
+#define FUSB302_CC_BUSY_RECHECKS	50
+
+/*
+ * Drain the RX FIFO, and if it may not be empty, look again shortly after: a
+ * packet the chip does not acknowledge raises no interrupt for it to be read
+ * on. Leave CC that stays busy for longer to the next interrupt, rather than
+ * keep the I2C bus and chip->lock busy with it.
+ */
+static int fusb302_pd_rx_drain(struct fusb302_chip *chip)
+{
+	int ret;
+
+	ret = fusb302_pd_read_messages(chip);
+	if (ret <= 0) {
+		chip->rx_rechecks = 0;
+		return ret;
+	}
+	if (chip->rx_rechecks++ < FUSB302_CC_BUSY_RECHECKS)
+		mod_delayed_work(chip->wq, &chip->rx_recheck,
+				 usecs_to_jiffies(FUSB302_CC_BUSY_MAX_US));
+	else
+		fusb302_log(chip, "CC stays busy, RX FIFO left for the next interrupt");
+
+	return 0;
+}
+
+static void fusb302_pd_rx_recheck_work(struct work_struct *work)
+{
+	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
+						 rx_recheck.work);
+	int ret;
+
+	mutex_lock(&chip->lock);
+	if (chip->pd_rx_on) {
+		ret = fusb302_pd_rx_drain(chip);
+		if (ret < 0)
+			fusb302_log(chip, "cannot read in PD message, ret=%d",
+				    ret);
+	}
+	mutex_unlock(&chip->lock);
+}
+
 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
 {
 	struct fusb302_chip *chip = dev_id;
@@ -1849,7 +1991,7 @@ static void fusb302_irq_work(struct work_struct *work)
 
 	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
 		fusb302_log(chip, "IRQ: PD tx success");
-		ret = fusb302_pd_read_message(chip);
+		ret = fusb302_pd_rx_drain(chip);
 		if (ret < 0) {
 			fusb302_log(chip,
 				    "cannot read in PD message, ret=%d", ret);
@@ -1869,7 +2011,7 @@ static void fusb302_irq_work(struct work_struct *work)
 
 	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
 		fusb302_log(chip, "IRQ: PD sent good CRC");
-		ret = fusb302_pd_read_message(chip);
+		ret = fusb302_pd_rx_drain(chip);
 		if (ret < 0) {
 			fusb302_log(chip,
 				    "cannot read in PD message, ret=%d", ret);
@@ -1979,6 +2121,7 @@ static int fusb302_probe(struct i2c_client *client)
 	spin_lock_init(&chip->irq_lock);
 	INIT_WORK(&chip->irq_work, fusb302_irq_work);
 	INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
+	INIT_DELAYED_WORK(&chip->rx_recheck, fusb302_pd_rx_recheck_work);
 	init_tcpc_dev(&chip->tcpc_dev);
 	fusb302_debugfs_init(chip);
 
@@ -2045,6 +2188,7 @@ static void fusb302_remove(struct i2c_client *client)
 	free_irq(chip->gpio_int_n_irq, chip);
 	cancel_work_sync(&chip->irq_work);
 	cancel_delayed_work_sync(&chip->bc_lvl_handler);
+	cancel_delayed_work_sync(&chip->rx_recheck);
 	tcpm_unregister_port(chip->tcpm_port);
 	fwnode_handle_put(chip->tcpc_dev.fwnode);
 	destroy_workqueue(chip->wq);
@@ -2062,6 +2206,8 @@ static int fusb302_pm_suspend(struct device *dev)
 
 	/* Make sure any pending irq_work is finished before the bus suspends */
 	flush_work(&chip->irq_work);
+	/* No RX FIFO recheck may run until the bus is back either */
+	cancel_delayed_work_sync(&chip->rx_recheck);
 	return 0;
 }
 
@@ -2078,6 +2224,12 @@ static int fusb302_pm_resume(struct device *dev)
 	chip->irq_suspended = false;
 	spin_unlock_irqrestore(&chip->irq_lock, flags);
 
+	/* Look at the RX FIFO again, in case suspend cut a recheck short */
+	mutex_lock(&chip->lock);
+	if (chip->pd_rx_on)
+		mod_delayed_work(chip->wq, &chip->rx_recheck, 0);
+	mutex_unlock(&chip->lock);
+
 	return 0;
 }
 

-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on
  2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
  2026-11-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
@ 2026-11-27 10:40 ` Alexey Charkov
  2 siblings, 0 replies; 5+ messages in thread
From: Alexey Charkov @ 2026-11-27 10:40 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu,
	Heiko Stuebner
  Cc: Sebastian Reichel, Sebastian Andrzej Siewior, linux-usb,
	linux-arm-kernel, linux-rockchip, linux-kernel, Alexey Charkov,
	stable

Messages that come in while PD RX is off go unacknowledged, yet the chip
still queues them in the RX FIFO. Turning PD RX on flushes the FIFO and
only then turns automatic GoodCRCs on, four I2C transactions later. A
message that lands in between stays in the FIFO with no GoodCRC sent,
and the driver hands it to TCPM all the same. The partner, failing to
deliver it, keeps its MessageID for the next message, and TCPM drops
that as a retry. On a Flipper One attaching as a sink, this happened in
1 of 30 attaches: it took in the first Source_Capabilities that it
never acknowledged, dropped the Accept to its Request, and soft reset on
the PS_RDY that followed. The flush can also cut short a packet coming
in, leaving the rest of it to be read as junk.

Read out and drop whatever is in the FIFO while CC is idle, turn
automatic GoodCRCs on, and then pass on a packet that was on CC at that
moment only if the chip acknowledged it, and any that came after it in
any case. The chip decides that at the end of the packet, and ACTIVITY
drops between a packet and the GoodCRC to it, so wait for CC to stay
idle for longer than 195us (tTransmit) before looking at GCRCSENT. Should
CC stay busy instead, flush the FIFO after all, as what the chip
acknowledged is then unknown, and should anything fail, turn automatic
GoodCRCs off again, as TCPM then treats PD RX as off.

With PD RX turned on 0-30ms late in 39 attaches to a partner sending
Source_Capabilities, every copy that came in before is dropped now, the
3 caught on CC as GoodCRCs came on were acknowledged and passed on, and
every attach reached a contract with no reset.

Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
Cc: stable@vger.kernel.org
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/usb/typec/tcpm/fusb302.c | 124 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 116 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index d57f9ae6621d..5cf1b78e7669 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -133,6 +133,8 @@ struct fusb302_chip {
 	bool rx_resync;
 	/* Only look in rx_buf, as the FIFO has nothing more of what it holds */
 	bool rx_buf_only;
+	/* Drop the messages read: no GoodCRC went out for them */
+	bool rx_discard;
 	bool vbus_present;
 	enum typec_cc_polarity cc_polarity;
 	enum typec_cc_status cc1;
@@ -900,6 +902,8 @@ static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
 	return ret;
 }
 
+static int fusb302_pd_rx_start(struct fusb302_chip *chip);
+
 static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
 {
 	struct fusb302_chip *chip = container_of(dev, struct fusb302_chip,
@@ -912,22 +916,26 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
 		goto done;
 	}
 
-	if (!on)
+	if (!on) {
 		fusb302_pd_set_sop_prime(chip, false);
-	ret = fusb302_pd_rx_flush(chip);
-	if (ret < 0) {
-		fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
-		goto done;
+		/* What this cuts short is read out and dropped on the way on */
+		ret = fusb302_pd_rx_flush(chip);
+		if (ret < 0) {
+			fusb302_log(chip, "cannot flush pd rx buffer, ret=%d",
+				    ret);
+			goto done;
+		}
+		fusb302_pd_rx_reset(chip, true);
 	}
-	fusb302_pd_rx_reset(chip, true);
 	ret = fusb302_pd_tx_flush(chip);
 	if (ret < 0) {
 		fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
 		goto done;
 	}
-	ret = fusb302_pd_set_auto_goodcrc(chip, on);
+	ret = on ? fusb302_pd_rx_start(chip) :
+		   fusb302_pd_set_auto_goodcrc(chip, false);
 	if (ret < 0) {
-		fusb302_log(chip, "cannot turn %s auto GCRC, ret=%d",
+		fusb302_log(chip, "cannot turn pd rx %s, ret=%d",
 			    str_on_off(on), ret);
 		goto done;
 	}
@@ -1677,6 +1685,10 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
 
 	fusb302_log(chip, "PD message header: %x", msg.header);
 	fusb302_log(chip, "PD message len: %d", len);
+	if (chip->rx_discard) {
+		fusb302_log(chip, "PD message not acknowledged, dropped");
+		goto out;
+	}
 
 	/*
 	 * Check if we've read off a GoodCRC message. If so then indicate to
@@ -1835,6 +1847,102 @@ static void fusb302_pd_rx_recheck_work(struct work_struct *work)
 	mutex_unlock(&chip->lock);
 }
 
+/* Tries at reading out the RX FIFO, before turning PD RX on */
+#define FUSB302_RX_DISCARD_TRIES	4
+
+/*
+ * Turn automatic GoodCRCs on, and keep messages that came in before from TCPM:
+ * the partner, failing to deliver one, keeps its MessageID for the next, which
+ * TCPM would then drop as a retry. Read them out rather than flush the FIFO,
+ * which would cut short a packet coming in, so that the chip may acknowledge
+ * it and leave the rest of it behind. A packet on CC as GoodCRCs come on goes
+ * to TCPM only if it gets one.
+ */
+static int fusb302_pd_rx_start(struct fusb302_chip *chip)
+{
+	bool gcrc_on = false;
+	u8 status1, interruptb;
+	int ret, i;
+
+	chip->rx_discard = true;
+	for (i = 0; i < FUSB302_RX_DISCARD_TRIES; i++) {
+		ret = fusb302_pd_read_messages(chip);
+		if (ret < 0)
+			goto out;
+		ret = fusb302_wait_cc_idle(chip, &status1);
+		if (ret < 0)
+			goto out;
+		if (!ret && (status1 & FUSB_REG_STATUS1_RX_EMPTY) &&
+		    !chip->rx_len)
+			break;
+	}
+	if (i == FUSB302_RX_DISCARD_TRIES) {
+		fusb302_log(chip, "RX FIFO keeps filling, flushing it");
+		ret = fusb302_pd_rx_flush(chip);
+		if (ret < 0)
+			goto out;
+		fusb302_pd_rx_reset(chip, true);
+	}
+
+	/* Clear GCRCSENT: nothing sends GoodCRCs yet */
+	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
+	if (ret < 0)
+		goto out;
+
+	ret = fusb302_pd_set_auto_goodcrc(chip, true);
+	if (ret < 0)
+		goto out;
+	gcrc_on = true;
+
+	ret = fusb302_wait_cc_idle(chip, &status1);
+	if (ret)
+		goto out;
+	if (status1 & FUSB_REG_STATUS1_RX_EMPTY)
+		goto out;
+
+	/*
+	 * ACTIVITY drops between the end of a packet and the GoodCRC to it,
+	 * which goes out within 195us (tTransmit): see that none follows
+	 */
+	usleep_range(300, 400);
+	ret = fusb302_wait_cc_idle(chip, &status1);
+	if (ret)
+		goto out;
+	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
+	if (ret < 0)
+		goto out;
+	/*
+	 * Only the packet that was on CC as GoodCRCs came on can have gone
+	 * without one, if it is in the FIFO by now: whatever lands after it
+	 * got a GoodCRC from the chip
+	 */
+	chip->rx_discard = !(status1 & FUSB_REG_STATUS1_RX_EMPTY) &&
+			   !(interruptb & FUSB_REG_INTERRUPTB_GCRCSENT);
+	if (chip->rx_discard) {
+		ret = fusb302_pd_read_message(chip);
+		chip->rx_discard = false;
+		if (ret < 0)
+			goto out;
+	}
+	ret = fusb302_pd_read_messages(chip);
+out:
+	if (ret > 0) {
+		/*
+		 * CC stayed busy with GoodCRCs on, so which packets in the FIFO
+		 * the chip acknowledged is unknown: flush them all
+		 */
+		fusb302_log(chip, "CC busy as PD RX went on, flushing RX FIFO");
+		ret = fusb302_pd_rx_flush(chip);
+		fusb302_pd_rx_reset(chip, true);
+	}
+	/* TCPM then treats PD RX as off */
+	if (ret < 0 && gcrc_on)
+		fusb302_pd_set_auto_goodcrc(chip, false);
+	chip->rx_discard = false;
+
+	return ret;
+}
+
 static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
 {
 	struct fusb302_chip *chip = dev_id;

-- 
2.56.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step
  2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
@ 2026-11-30 14:06   ` Heikki Krogerus
  0 siblings, 0 replies; 5+ messages in thread
From: Heikki Krogerus @ 2026-11-30 14:06 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu, Heiko Stuebner,
	Sebastian Reichel, Sebastian Andrzej Siewior, linux-usb,
	linux-arm-kernel, linux-rockchip, linux-kernel, stable

On Fri, Nov 27, 2026 at 02:40:46PM +0400, Alexey Charkov wrote:
> The chip writes each byte into the RX FIFO as it comes off CC, so a
> flush in the middle of a packet leaves the rest of that packet in the
> FIFO with no token in front, and the driver reads that tail, padded with
> zeros where the FIFO runs dry, as a message. TCPM acts on the junk
> header. On a Flipper One attaching as a sink, the flush that turns PD RX
> on raced the partner's first message in 2 of about 35 attaches: a
> "message" with header 0 made TCPM see a data role mismatch and go to
> ERROR_RECOVERY. With the FIFO flushed on purpose 27 times while the
> partner sent a Ping every 20ms, 98 junk messages reached TCPM, and it
> reset 17 times.
> 
> Flushing the FIFO again once the junk is spotted is no cure, as any
> message behind the tail may have been acknowledged already, and the
> partner never sends it again.
> 
> Keep the bytes read from the FIFO in a buffer instead, and scan for a full
> valid message, including a CRC that checks out, to avoid interpreting junk
> bytes as a start of a message.
> 
> In the same test, no junk reaches TCPM now, and only the Pings that the
> forced flushes cut short go missing.
> 
> Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Alexey Charkov <alchark@flipper.net>

Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
>  drivers/usb/typec/tcpm/Kconfig   |   1 +
>  drivers/usb/typec/tcpm/fusb302.c | 222 +++++++++++++++++++++++++++++++++------
>  2 files changed, 190 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/Kconfig b/drivers/usb/typec/tcpm/Kconfig
> index 00baa7503d45..e551cb63a775 100644
> --- a/drivers/usb/typec/tcpm/Kconfig
> +++ b/drivers/usb/typec/tcpm/Kconfig
> @@ -59,6 +59,7 @@ config TYPEC_FUSB302
>  	depends on I2C
>  	depends on EXTCON || !EXTCON
>  	depends on DRM || DRM=n
> +	select CRC32
>  	select DRM_AUX_HPD_BRIDGE if DRM_BRIDGE && OF
>  	help
>  	  The Fairchild FUSB302 Type-C chip driver that works with
> diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
> index 1cbe272ad686..d2d0e8ffc308 100644
> --- a/drivers/usb/typec/tcpm/fusb302.c
> +++ b/drivers/usb/typec/tcpm/fusb302.c
> @@ -6,6 +6,7 @@
>   */
>  
>  #include <drm/bridge/aux-bridge.h>
> +#include <linux/crc32.h>
>  #include <linux/debugfs.h>
>  #include <linux/delay.h>
>  #include <linux/errno.h>
> @@ -27,6 +28,7 @@
>  #include <linux/string.h>
>  #include <linux/string_choices.h>
>  #include <linux/types.h>
> +#include <linux/unaligned.h>
>  #include <linux/usb.h>
>  #include <linux/usb/typec.h>
>  #include <linux/usb/tcpm.h>
> @@ -72,6 +74,11 @@ static const u8 rd_mda_value[] = {
>  #define LOG_BUFFER_ENTRIES	1024
>  #define LOG_BUFFER_ENTRY_SIZE	128
>  
> +/* An SOP* token, the header, up to seven data objects and the CRC */
> +#define FUSB302_RX_MSG_MAX	(1 + 2 + PD_MAX_PAYLOAD * 4 + 4)
> +/* The size of the RX FIFO */
> +#define FUSB302_RX_FIFO_LEN	80
> +
>  struct fusb302_chip {
>  	struct device *dev;
>  	struct i2c_client *i2c_client;
> @@ -110,6 +117,17 @@ struct fusb302_chip {
>  	bool vbus_on;
>  	bool charge_on;
>  	bool pd_rx_on;
> +	/*
> +	 * Bytes read from the RX FIFO but not handled yet: the start of a
> +	 * message to read on, or bytes to find the next message in
> +	 */
> +	u8 rx_buf[FUSB302_RX_MSG_MAX];
> +	u8 rx_len;
> +	/*
> +	 * The next message may not be at the start of the buffer.
> +	 * Look for its start first before processing.
> +	 */
> +	bool rx_resync;
>  	bool vbus_present;
>  	enum typec_cc_polarity cc_polarity;
>  	enum typec_cc_status cc1;
> @@ -831,6 +849,17 @@ static int fusb302_pd_rx_flush(struct fusb302_chip *chip)
>  				    FUSB_REG_CONTROL1_RX_FLUSH);
>  }
>  
> +/*
> + * Forget whatever was read from the RX FIFO but not handled yet. After a flush
> + * or a reset, the FIFO may go on with the rest of a packet cut short, so look
> + * for the next message then, rather than read on from the first byte.
> + */
> +static void fusb302_pd_rx_reset(struct fusb302_chip *chip, bool cut_short)
> +{
> +	chip->rx_len = 0;
> +	chip->rx_resync = cut_short;
> +}
> +
>  static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
>  {
>  	if (on)
> @@ -885,6 +914,7 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
>  		fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret);
>  		goto done;
>  	}
> +	fusb302_pd_rx_reset(chip, true);
>  	ret = fusb302_pd_tx_flush(chip);
>  	if (ret < 0) {
>  		fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
> @@ -1477,43 +1507,168 @@ static int fusb302_handle_togdone(struct fusb302_chip *chip)
>  
>  static int fusb302_pd_reset(struct fusb302_chip *chip)
>  {
> +	/* What was read of a message before the reset is of no use after it */
> +	fusb302_pd_rx_reset(chip, true);
> +
>  	return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
>  				    FUSB_REG_RESET_PD_RESET);
>  }
>  
> -static int fusb302_pd_read_message(struct fusb302_chip *chip,
> -				   struct pd_message *msg)
> +static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n)
>  {
> -	int ret = 0;
> -	u8 token;
> -	u8 crc[4];
> -	int len;
> +	chip->rx_len -= n;
> +	memmove(chip->rx_buf, chip->rx_buf + n, chip->rx_len);
> +}
>  
> -	/* first SOP token */
> -	ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token);
> -	if (ret < 0)
> -		return ret;
> -	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2,
> -				     (u8 *)&msg->header);
> -	if (ret < 0)
> -		return ret;
> -	len = pd_header_cnt_le(msg->header) * 4;
> -	/* add 4 to length to include the CRC */
> -	if (len > PD_MAX_PAYLOAD * 4) {
> -		fusb302_log(chip, "PD message too long %d", len);
> -		return -EINVAL;
> +static bool fusb302_pd_rx_is_token(u8 byte)
> +{
> +	byte &= FUSB302_RX_TKN_MASK;
> +
> +	return byte == FUSB302_RX_TKN_SOP || byte == FUSB302_RX_TKN_SOP1;
> +}
> +
> +/* FIFO bytes taken by the message whose token is at rx_buf[off] */
> +static int fusb302_pd_rx_msg_len(struct fusb302_chip *chip, int off)
> +{
> +	u16 header = get_unaligned_le16(chip->rx_buf + off + 1);
> +
> +	return 1 + 2 + pd_header_cnt(header) * 4 + 4;
> +}
> +
> +/* The chip keeps the CRC it received after the header and data objects */
> +static bool fusb302_pd_rx_crc_ok(struct fusb302_chip *chip, int off, int len)
> +{
> +	return ~crc32_le(~0U, chip->rx_buf + off + 1, len - 5) ==
> +	       get_unaligned_le32(chip->rx_buf + off + len - 4);
> +}
> +
> +/*
> + * Return the offset of the first message past the start of rx_buf that it
> + * holds in full, with its CRC checking out, or 0 if there is none
> + */
> +static int fusb302_pd_rx_find(struct fusb302_chip *chip)
> +{
> +	int off, len;
> +
> +	for (off = 1; off + 3 <= chip->rx_len; off++) {
> +		if (!fusb302_pd_rx_is_token(chip->rx_buf[off]))
> +			continue;
> +		len = fusb302_pd_rx_msg_len(chip, off);
> +		if (off + len <= chip->rx_len &&
> +		    fusb302_pd_rx_crc_ok(chip, off, len))
> +			return off;
>  	}
> -	if (len > 0) {
> -		ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len,
> -					     (u8 *)msg->payload);
> +
> +	return 0;
> +}
> +
> +/*
> + * Read up to n more bytes into rx_buf and return how many. When the FIFO is in
> + * step, rx_buf starts with a message the chip has received in full, so read the
> + * rest in one go. Otherwise read one byte at a time while the FIFO holds data,
> + * as a dried out FIFO starts returning zeros instead of received data, and real
> + * bytes will be returned to the next read instead once the device receives them
> + */
> +static int fusb302_pd_rx_read(struct fusb302_chip *chip, int n)
> +{
> +	u8 status1;
> +	int ret;
> +
> +	if (chip->rx_resync) {
> +		ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1, &status1);
>  		if (ret < 0)
>  			return ret;
> +		if (status1 & FUSB_REG_STATUS1_RX_EMPTY)
> +			return 0;
> +		n = 1;
>  	}
> -	/* another 4 bytes to read CRC out */
> -	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc);
> -	if (ret < 0)
> +	ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, n,
> +				     chip->rx_buf + chip->rx_len);
> +	if (ret < 0) {
> +		/* Some of it may have left the FIFO all the same */
> +		chip->rx_resync = true;
>  		return ret;
> -	fusb302_log(chip, "PD message header: %x", msg->header);
> +	}
> +	chip->rx_len += n;
> +
> +	return n;
> +}
> +
> +/*
> + * Read in and handle the next message in the RX FIFO. Return 1 if there was
> + * one, 0 if the FIFO ran out first, or a negative error code.
> + */
> +static int fusb302_pd_read_message(struct fusb302_chip *chip)
> +{
> +	struct pd_message msg = {};
> +	int dropped = 0;
> +	int len, off, ret;
> +	u8 token;
> +
> +	for (;;) {
> +		/*
> +		 * Already discarded a full FIFO's worth of junk - leave
> +		 * whatever remains to another pass
> +		 */
> +		if (dropped >= FUSB302_RX_FIFO_LEN) {
> +			ret = 0;
> +			goto out;
> +		}
> +		/*
> +		 * The chip writes each byte into the FIFO as it comes off CC,
> +		 * so a flush in the middle of a packet leaves the rest of it
> +		 * there with no token in front. Following bytes may look like a
> +		 * valid token without being one, so only trust them if the CRC
> +		 * of the resulting message checks out, too. If not, look on
> +		 * from the next byte: there may be a next message that the chip
> +		 * has already acknowledged, and we must not miss it.
> +		 */
> +		while (chip->rx_len &&
> +		       !fusb302_pd_rx_is_token(chip->rx_buf[0])) {
> +			chip->rx_resync = true;
> +			fusb302_pd_rx_drop(chip, 1);
> +			dropped++;
> +		}
> +		if (chip->rx_len < 3)
> +			len = 3;
> +		else
> +			len = fusb302_pd_rx_msg_len(chip, 0);
> +		if (chip->rx_len < len) {
> +			ret = fusb302_pd_rx_read(chip, len - chip->rx_len);
> +			if (ret < 0)
> +				goto out;
> +			if (ret)
> +				continue;
> +			/*
> +			 * The FIFO ran dry, which may be due to a bogus token
> +			 * claiming more bytes than will ever come. See if the
> +			 * following bytes contain a complete valid message.
> +			 */
> +			off = fusb302_pd_rx_find(chip);
> +			if (!off)
> +				goto out;
> +			fusb302_pd_rx_drop(chip, off);
> +			dropped += off;
> +			continue;
> +		}
> +		if (fusb302_pd_rx_crc_ok(chip, 0, len))
> +			break;
> +		if (!chip->rx_resync)
> +			fusb302_log(chip, "PD message failed CRC check, header: %x",
> +				    get_unaligned_le16(chip->rx_buf + 1));
> +		chip->rx_resync = true;
> +		fusb302_pd_rx_drop(chip, 1);
> +		dropped++;
> +	}
> +
> +	chip->rx_resync = false;
> +	token = chip->rx_buf[0];
> +	memcpy(&msg, chip->rx_buf + 1, len - 5);
> +	fusb302_pd_rx_drop(chip, len);
> +	len -= 7;
> +	ret = 1;
> +
> +	fusb302_log(chip, "PD message header: %x", msg.header);
>  	fusb302_log(chip, "PD message len: %d", len);
>  
>  	/*
> @@ -1526,13 +1681,15 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip,
>  	 * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
>  	 * to check the message type to ensure correct reporting to TCPM.
>  	 */
> -	if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC))
> +	if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC)
>  		tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
>  	else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1)
> -		tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP_PRIME);
> +		tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP_PRIME);
>  	else
> -		tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP);
> -
> +		tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP);
> +out:
> +	if (dropped)
> +		fusb302_log(chip, "dropped %d bytes from the RX FIFO", dropped);
>  	return ret;
>  }
>  
> @@ -1605,7 +1762,6 @@ static void fusb302_irq_work(struct work_struct *work)
>  	bool intr_togdone;
>  	bool intr_bc_lvl;
>  	bool intr_comp_chng;
> -	struct pd_message pd_msg;
>  
>  	mutex_lock(&chip->lock);
>  	/* grab a snapshot of intr flags */
> @@ -1693,7 +1849,7 @@ static void fusb302_irq_work(struct work_struct *work)
>  
>  	if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
>  		fusb302_log(chip, "IRQ: PD tx success");
> -		ret = fusb302_pd_read_message(chip, &pd_msg);
> +		ret = fusb302_pd_read_message(chip);
>  		if (ret < 0) {
>  			fusb302_log(chip,
>  				    "cannot read in PD message, ret=%d", ret);
> @@ -1713,7 +1869,7 @@ static void fusb302_irq_work(struct work_struct *work)
>  
>  	if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) {
>  		fusb302_log(chip, "IRQ: PD sent good CRC");
> -		ret = fusb302_pd_read_message(chip, &pd_msg);
> +		ret = fusb302_pd_read_message(chip);
>  		if (ret < 0) {
>  			fusb302_log(chip,
>  				    "cannot read in PD message, ret=%d", ret);
> 
> -- 
> 2.56.0
> 

-- 
heikki

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-30 14:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
2026-11-30 14:06   ` Heikki Krogerus
2026-11-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on Alexey Charkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox