* [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
` (5 more replies)
0 siblings, 6 replies; 7+ 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] 7+ 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-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ 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 [flat|nested] 7+ 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
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ 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 [flat|nested] 7+ 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
2026-11-27 10:40 ` [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread Alexey Charkov
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ 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 [flat|nested] 7+ messages in thread
* [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread
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
` (2 preceding siblings ...)
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
@ 2026-11-27 10:40 ` Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time Alexey Charkov
5 siblings, 0 replies; 7+ 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
The threaded IRQ handler only disables the IRQ and queues irq_work, which
then does all the handling. The extra hop costs a context switch, and
leaves the handling at normal priority behind whatever else shares the
system workqueue.
Handle the interrupt in the IRQ thread instead, as suggested in review of
the switch to a threaded handler [1]. IRQF_ONESHOT keeps the line masked
meanwhile. Before resume, the I2C adapter may still be suspended: disable
the IRQ then, and have resume handle it before enabling it again, so that
nothing handles it twice at once and it stays a wakeup source. Make
irq_lock a mutex for that, as disabling an IRQ behind an I2C GPIO
expander may sleep.
Verified both at runtime and across suspend on a Rockchip RK3576 board.
Link: https://lore.kernel.org/r/20260312120418.99U0NPWL@linutronix.de [1]
Suggested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Suggested-by: Sebastian Reichel <sebastian.reichel@collabora.com>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/fusb302.c | 82 +++++++++++++++++++++-------------------
1 file changed, 43 insertions(+), 39 deletions(-)
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 5cf1b78e7669..aa0e4dbe31db 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -87,9 +87,13 @@ struct fusb302_chip {
struct regulator *vbus;
- spinlock_t irq_lock;
- struct work_struct irq_work;
+ /*
+ * A mutex, as disabling an IRQ behind an I2C GPIO expander, which
+ * fusb302_irq_intn() does under it, may sleep
+ */
+ struct mutex irq_lock;
bool irq_suspended;
+ /* The IRQ came before resume, which handles it and enables it again */
bool irq_while_suspended;
struct gpio_desc *gpio_int_n;
int gpio_int_n_irq;
@@ -1943,24 +1947,6 @@ static int fusb302_pd_rx_start(struct fusb302_chip *chip)
return ret;
}
-static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
-{
- struct fusb302_chip *chip = dev_id;
- unsigned long flags;
-
- /* Disable our level triggered IRQ until our irq_work has cleared it */
- disable_irq_nosync(chip->gpio_int_n_irq);
-
- spin_lock_irqsave(&chip->irq_lock, flags);
- if (chip->irq_suspended)
- chip->irq_while_suspended = true;
- else
- schedule_work(&chip->irq_work);
- spin_unlock_irqrestore(&chip->irq_lock, flags);
-
- return IRQ_HANDLED;
-}
-
/*
* Reading an interrupt register clears its flags in the chip, so keep them
* pending until handled: a bus error on a later read would otherwise lose
@@ -1998,10 +1984,8 @@ static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
return ret;
}
-static void fusb302_irq_work(struct work_struct *work)
+static void fusb302_irq_handle(struct fusb302_chip *chip)
{
- struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
- irq_work);
int ret = 0;
u8 interrupt;
u8 interrupta;
@@ -2128,7 +2112,26 @@ static void fusb302_irq_work(struct work_struct *work)
}
done:
mutex_unlock(&chip->lock);
- enable_irq(chip->gpio_int_n_irq);
+}
+
+static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
+{
+ struct fusb302_chip *chip = dev_id;
+
+ mutex_lock(&chip->irq_lock);
+ if (chip->irq_suspended) {
+ /* Only once, as resume enables it once */
+ if (!chip->irq_while_suspended)
+ disable_irq_nosync(chip->gpio_int_n_irq);
+ chip->irq_while_suspended = true;
+ mutex_unlock(&chip->irq_lock);
+ return IRQ_HANDLED;
+ }
+ mutex_unlock(&chip->irq_lock);
+
+ fusb302_irq_handle(chip);
+
+ return IRQ_HANDLED;
}
static int init_gpio(struct fusb302_chip *chip)
@@ -2226,8 +2229,7 @@ static int fusb302_probe(struct i2c_client *client)
if (!chip->wq)
return -ENOMEM;
- spin_lock_init(&chip->irq_lock);
- INIT_WORK(&chip->irq_work, fusb302_irq_work);
+ mutex_init(&chip->irq_lock);
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);
@@ -2294,7 +2296,6 @@ static void fusb302_remove(struct i2c_client *client)
disable_irq_wake(chip->gpio_int_n_irq);
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);
@@ -2306,14 +2307,13 @@ static void fusb302_remove(struct i2c_client *client)
static int fusb302_pm_suspend(struct device *dev)
{
struct fusb302_chip *chip = dev->driver_data;
- unsigned long flags;
- spin_lock_irqsave(&chip->irq_lock, flags);
+ mutex_lock(&chip->irq_lock);
chip->irq_suspended = true;
- spin_unlock_irqrestore(&chip->irq_lock, flags);
+ mutex_unlock(&chip->irq_lock);
- /* Make sure any pending irq_work is finished before the bus suspends */
- flush_work(&chip->irq_work);
+ /* Let an IRQ being handled finish before the bus suspends */
+ synchronize_irq(chip->gpio_int_n_irq);
/* No RX FIFO recheck may run until the bus is back either */
cancel_delayed_work_sync(&chip->rx_recheck);
return 0;
@@ -2322,15 +2322,19 @@ static int fusb302_pm_suspend(struct device *dev)
static int fusb302_pm_resume(struct device *dev)
{
struct fusb302_chip *chip = dev->driver_data;
- unsigned long flags;
+ bool pending;
- spin_lock_irqsave(&chip->irq_lock, flags);
- if (chip->irq_while_suspended) {
- schedule_work(&chip->irq_work);
- chip->irq_while_suspended = false;
- }
+ mutex_lock(&chip->irq_lock);
+ pending = chip->irq_while_suspended;
+ chip->irq_while_suspended = false;
chip->irq_suspended = false;
- spin_unlock_irqrestore(&chip->irq_lock, flags);
+ mutex_unlock(&chip->irq_lock);
+
+ /* The IRQ stays disabled until then, so nothing else handles it */
+ if (pending) {
+ fusb302_irq_handle(chip);
+ enable_irq(chip->gpio_int_n_irq);
+ }
/* Look at the RX FIFO again, in case suspend cut a recheck short */
mutex_lock(&chip->lock);
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver
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
` (3 preceding siblings ...)
2026-11-27 10:40 ` [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread Alexey Charkov
@ 2026-11-27 10:40 ` Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time Alexey Charkov
5 siblings, 0 replies; 7+ 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
The chip 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. A PD 3.0 partner asking for the extended source
capabilities then never sees the message delivered: an Apple Studio
Display retries it, soft resets, and asks again after the new contract,
every 94ms, so it never gets as far as DisplayPort.
Send the GoodCRC from the driver once the message is read and its CRC
checks out, and read the FIFO on CRC_CHK, which such a message still
raises. Over a 400kHz I2C bus that takes 2ms and more, far beyond 195us
(tTransmit), but the partner retries with the same MessageID and may
accept a GoodCRC that comes while it waits for the one to a retry. That
display sends its retries 0.97ms after each attempt ends, which puts most
such late GoodCRCs within the window of its second or third attempt. As
the FIFO is only read while CC is idle, the GoodCRC follows a retry
already under way rather than colliding with it. Pass the message on to
TCPM too, so the partner gets an answer rather than asking again.
Hold the GoodCRC back while TCPM waits for the outcome of a transmission
of its own, which that of the GoodCRC would pass for, unless TCPM has
given up on it already. Keep a collision of the GoodCRC apart from one of
the next transmission TCPM makes, so that the one does not fail that
transmission and the other is not lost, also when the GoodCRC goes out
from outside the IRQ thread, which then handles the interrupt flags read
to tell them apart. Use revision 2.0 in the header, as the chip does for
its own GoodCRCs.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/fusb302.c | 175 +++++++++++++++++++++++++++++++++++++--
1 file changed, 170 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index aa0e4dbe31db..c98a74fc7a2a 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -124,12 +124,19 @@ struct fusb302_chip {
bool vbus_on;
bool charge_on;
bool pd_rx_on;
+ /* TCPM waits for the outcome of a transmission, started at tx_start */
+ bool tx_pending;
+ ktime_t tx_start;
+ /* The driver sent a GoodCRC, for fusb302_pd_check_ack() */
+ bool ack_check;
/*
* 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 driver sent a GoodCRC to the message starting rx_buf */
+ bool rx_acked;
/*
* The next message may not be at the start of the buffer.
* Look for its start first before processing.
@@ -143,6 +150,9 @@ struct fusb302_chip {
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
enum typec_cc_status cc2;
+ /* for a GoodCRC sent by the driver, as the chip does for its own */
+ enum typec_role pwr_role;
+ enum typec_data_role data_role;
u32 snk_pdo[PDO_MAX_OBJECTS];
#ifdef CONFIG_DEBUG_FS
@@ -869,6 +879,7 @@ static void fusb302_pd_rx_reset(struct fusb302_chip *chip, bool cut_short)
{
chip->rx_len = 0;
chip->rx_resync = cut_short;
+ chip->rx_acked = false;
}
static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
@@ -883,7 +894,8 @@ static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
{
int ret = 0;
- u8 mask_interrupts = FUSB_REG_MASK_COLLISION;
+ /* See fusb302_pd_rx_ack() for CRC_CHK */
+ u8 mask_interrupts = FUSB_REG_MASK_COLLISION | FUSB_REG_MASK_CRC_CHK;
u8 maska_interrupts = FUSB_REG_MASKA_RETRYFAIL |
FUSB_REG_MASKA_HARDSENT |
FUSB_REG_MASKA_TX_SUCCESS |
@@ -907,6 +919,7 @@ static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
}
static int fusb302_pd_rx_start(struct fusb302_chip *chip);
+static void fusb302_pd_check_ack_async(struct fusb302_chip *chip);
static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
{
@@ -936,6 +949,8 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret);
goto done;
}
+ /* Nothing flushed reports its outcome */
+ chip->tx_pending = false;
ret = on ? fusb302_pd_rx_start(chip) :
fusb302_pd_set_auto_goodcrc(chip, false);
if (ret < 0) {
@@ -994,6 +1009,8 @@ static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
ret);
goto done;
}
+ chip->pwr_role = pwr;
+ chip->data_role = data;
fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
typec_data_role_name[data]);
done:
@@ -1128,6 +1145,13 @@ static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
int ret = 0;
mutex_lock(&chip->lock);
+ /*
+ * See whether a GoodCRC from the driver collided before this goes out:
+ * a COLLISION from now on is this transmission's, even should the
+ * check fail
+ */
+ fusb302_pd_check_ack_async(chip);
+ chip->ack_check = false;
switch (type) {
case TCPC_TX_SOP:
case TCPC_TX_SOP_PRIME:
@@ -1154,6 +1178,10 @@ static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
transmit_type_name[type]);
ret = -EINVAL;
}
+ if (ret >= 0) {
+ chip->tx_pending = true;
+ chip->tx_start = ktime_get();
+ }
mutex_unlock(&chip->lock);
return ret;
@@ -1531,10 +1559,110 @@ static int fusb302_pd_reset(struct fusb302_chip *chip)
FUSB_REG_RESET_PD_RESET);
}
+static void fusb302_pd_tx_complete(struct fusb302_chip *chip,
+ enum tcpm_transmit_status status)
+{
+ chip->tx_pending = false;
+ tcpm_pd_transmit_complete(chip->tcpm_port, status);
+}
+
+/*
+ * See whether a GoodCRC the driver sent collided before releasing chip->lock:
+ * by the next interrupt, TCPM may have started a transmission, and the
+ * COLLISION would then be reported against it. The chip sets COLLISION when
+ * TXON finds CC busy, so it is there to see by now. Keep the other bits, which
+ * reading the register clears, pending for fusb302_irq_handle().
+ */
+static int fusb302_pd_check_ack(struct fusb302_chip *chip)
+{
+ u8 interrupt;
+ int ret;
+
+ /* Left set on an error, for fusb302_irq_handle() to tell COLLISION */
+ ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
+ if (ret < 0)
+ return ret;
+ chip->ack_check = false;
+ if (interrupt & FUSB_REG_INTERRUPT_COLLISION)
+ fusb302_log(chip, "GoodCRC sent by the driver collided");
+ chip->pending_interrupt |= interrupt & ~FUSB_REG_INTERRUPT_COLLISION;
+
+ return 0;
+}
+
+/*
+ * As fusb302_pd_check_ack(), outside the IRQ thread: reading INTERRUPT clears
+ * the flags that raise the IRQ, so have the IRQ thread handle what it read
+ */
+static void fusb302_pd_check_ack_async(struct fusb302_chip *chip)
+{
+ if (!chip->ack_check || fusb302_pd_check_ack(chip) < 0)
+ return;
+ if (chip->pending_interrupt)
+ irq_wake_thread(chip->gpio_int_n_irq, chip);
+}
+
+/*
+ * TCPM waits for the outcome of its last transmission, unless it gave up after
+ * PD_T_TCPC_TX_TIMEOUT, which it does without telling the driver
+ */
+static bool fusb302_pd_tx_waiting(struct fusb302_chip *chip)
+{
+ return chip->tx_pending &&
+ ktime_ms_delta(ktime_get(), chip->tx_start) <
+ PD_T_TCPC_TX_TIMEOUT;
+}
+
+/*
+ * The chip only decodes the low four bits of the message type, so it mistakes
+ * Get_Source_Cap_Extended (0x11) for a GoodCRC (0x1): it neither acknowledges
+ * it nor raises GCRCSENT, only CRC_CHK, yet still queues it in the RX FIFO.
+ * Send the GoodCRC from here once the message checks out. It goes out well
+ * after 195us (tTransmit), but the partner retries with the same MessageID and
+ * usually accepts it then, rather than soft reset over and over.
+ */
+static void fusb302_pd_rx_ack(struct fusb302_chip *chip)
+{
+ u16 header = get_unaligned_le16(chip->rx_buf + 1);
+ struct pd_message ack = {};
+ int ret;
+
+ if (chip->rx_discard || chip->rx_acked || pd_header_cnt(header) ||
+ pd_header_type(header) != PD_CTRL_GET_SOURCE_CAP_EXT ||
+ (chip->rx_buf[0] & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1)
+ return;
+ chip->rx_acked = true;
+
+ /*
+ * The outcome of a GoodCRC sent now would pass for that of the
+ * transmission TCPM waits for, so leave the partner to retry then
+ */
+ if (fusb302_pd_tx_waiting(chip)) {
+ fusb302_log(chip, "not acknowledging PD message, TX pending");
+ return;
+ }
+
+ /*
+ * The chip's own GoodCRCs go out as revision 2.0. As it decodes only
+ * the low four bits of the message type, it treats any GoodCRC it
+ * sends as one that needs no GoodCRC in return, so it sends this one
+ * once, without retries or RETRYFAIL, whatever the retry count is.
+ */
+ ack.header = PD_HEADER_LE(PD_CTRL_GOOD_CRC, chip->pwr_role,
+ chip->data_role, PD_REV20,
+ pd_header_msgid(header), 0);
+ ret = fusb302_pd_send_message(chip, TCPC_TX_SOP, &ack);
+ if (ret >= 0)
+ chip->ack_check = true;
+ else
+ fusb302_log(chip, "cannot acknowledge PD message, ret=%d", ret);
+}
+
static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n)
{
chip->rx_len -= n;
memmove(chip->rx_buf, chip->rx_buf + n, chip->rx_len);
+ chip->rx_acked = false;
}
static bool fusb302_pd_rx_is_token(u8 byte)
@@ -1680,6 +1808,8 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
dropped++;
}
+ /* The CRC checks out: acknowledge it, should the chip not have */
+ fusb302_pd_rx_ack(chip);
chip->rx_resync = false;
token = chip->rx_buf[0];
memcpy(&msg, chip->rx_buf + 1, len - 5);
@@ -1705,7 +1835,7 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
* to check the message type to ensure correct reporting to TCPM.
*/
if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC)
- tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
+ fusb302_pd_tx_complete(chip, 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);
else
@@ -1847,6 +1977,7 @@ static void fusb302_pd_rx_recheck_work(struct work_struct *work)
if (ret < 0)
fusb302_log(chip, "cannot read in PD message, ret=%d",
ret);
+ fusb302_pd_check_ack_async(chip);
}
mutex_unlock(&chip->lock);
}
@@ -1943,6 +2074,11 @@ static int fusb302_pd_rx_start(struct fusb302_chip *chip)
if (ret < 0 && gcrc_on)
fusb302_pd_set_auto_goodcrc(chip, false);
chip->rx_discard = false;
+ /*
+ * A GoodCRC sent while reading the FIFO out: see whether it collided
+ * before TCPM may start a transmission the collision would pass for
+ */
+ fusb302_pd_check_ack_async(chip);
return ret;
}
@@ -1998,6 +2134,7 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
bool intr_comp_chng;
mutex_lock(&chip->lock);
+again:
/* grab a snapshot of intr flags */
intr_togdone = chip->intr_togdone;
intr_bc_lvl = chip->intr_bc_lvl;
@@ -2014,6 +2151,22 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
chip->pending_interrupt = 0;
chip->pending_interrupta = 0;
chip->pending_interruptb = 0;
+
+ /*
+ * A message the chip didn't acknowledge raises CRC_CHK alone: read it
+ * out first thing, as its GoodCRC is past due already. Those that came
+ * before PD was on, which nothing acknowledged, were dropped then.
+ */
+ if (chip->pd_rx_on && (interrupt & FUSB_REG_INTERRUPT_CRC_CHK) &&
+ !(interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) &&
+ !(interruptb & FUSB_REG_INTERRUPTB_GCRCSENT)) {
+ /* Still handle the other events then */
+ ret = fusb302_pd_rx_drain(chip);
+ if (ret < 0)
+ fusb302_log(chip,
+ "cannot read in PD message, ret=%d", ret);
+ }
+
fusb302_log(chip,
"IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
interrupt, interrupta, interruptb, status0);
@@ -2061,14 +2214,19 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
}
}
+ /* Not for TCPM if it came from a GoodCRC the driver sent */
if (interrupt & FUSB_REG_INTERRUPT_COLLISION) {
fusb302_log(chip, "IRQ: PD collision");
- tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
+ if (chip->ack_check)
+ chip->ack_check = false;
+ else if (chip->tx_pending)
+ fusb302_pd_tx_complete(chip, TCPC_TX_FAILED);
}
if (interrupta & FUSB_REG_INTERRUPTA_RETRYFAIL) {
fusb302_log(chip, "IRQ: PD retry failed");
- tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_FAILED);
+ if (chip->tx_pending)
+ fusb302_pd_tx_complete(chip, TCPC_TX_FAILED);
}
if (interrupta & FUSB_REG_INTERRUPTA_HARDSENT) {
@@ -2078,7 +2236,7 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
fusb302_log(chip, "cannot PD reset, ret=%d", ret);
goto done;
}
- tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS);
+ fusb302_pd_tx_complete(chip, TCPC_TX_SUCCESS);
}
if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) {
@@ -2111,6 +2269,13 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
}
}
done:
+ /*
+ * Not right after the GoodCRC, which would hold up the next one to a
+ * retry queued meanwhile. Then handle what the check read.
+ */
+ if (chip->ack_check && fusb302_pd_check_ack(chip) >= 0 &&
+ chip->pending_interrupt)
+ goto again;
mutex_unlock(&chip->lock);
}
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time
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
` (4 preceding siblings ...)
2026-11-27 10:40 ` [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver Alexey Charkov
@ 2026-11-27 10:40 ` Alexey Charkov
5 siblings, 0 replies; 7+ 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
The GoodCRC the driver sends to a Get_Source_Cap_Extended goes out
2.2-2.8ms after the interrupt, as reading the interrupt and status
registers, waiting for CC to go idle and then reading the RX FIFO over a
400kHz I2C bus takes that long, while it is due within 195us (tTransmit).
Some partners don't accept it as the GoodCRC to a retry either: an iPhone
17 Pro as a source retries 1ms after each attempt ends, and soft resets
after the last one, then hard resets, cutting VBUS each time.
Read STATUS0, STATUS1, INTERRUPT and the head of the RX FIFO in a single
transfer first thing in the interrupt handler, as the register address
increments up to the FIFO and stays there, and acknowledge a
Get_Source_Cap_Extended at once if the FIFO starts with one, before its
CRC is in. Keep the GoodCRC to the partner's next message staged in the
TX FIFO, except the TXON that sends it, so that sending it takes a single
byte write, and flush it before anything else goes into the TX FIFO.
Neither the single read nor the staged GoodCRC gets the GoodCRC out in
time on its own.
The cost is one more I2C transfer per interrupt with PD on, a flush and a
FIFO write after each message received, and a flush before each
transmission with a GoodCRC staged. All of those fall in less time
critical paths, as the PD specification leaves ample time for processing
other than sending out the GoodCRC.
As the FIFO is read without waiting for CC to go idle, a packet may be
landing meanwhile, and an empty FIFO reads as 0 while keeping the bytes
still to come. Keep only the bytes that are not 0 then, as neither the
token nor the first header byte ever is, and check the CRC to distinguish
a genuinely zero second header byte (which only a sink sends), from a
bogus one created by an empty FIFO. That leaves a Get_Source_Cap_Extended
from a sink with MessageID 0 acknowledged only once its CRC is in, should
the read find CC busy again. Don't read beyond the header, as no rule can
distinguish bogus zero bytes from real ones for the rest of the message.
On a Rockchip RK3576 board, the GoodCRC now goes out 0.4-0.9ms after
the interrupt, and the iPhone accepted all 32 of them, where 118 of 119
had drawn a Soft_Reset before. Of over 26,000 Pings flooded at the
driver as a sink and as a source, none got lost.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/fusb302.c | 321 ++++++++++++++++++++++++++++++++++++---
1 file changed, 304 insertions(+), 17 deletions(-)
diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index c98a74fc7a2a..bdba2169ec0f 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -146,6 +146,21 @@ struct fusb302_chip {
bool rx_buf_only;
/* Drop the messages read: no GoodCRC went out for them */
bool rx_discard;
+ /*
+ * rx_buf holds a token and valid header byte[0], while the header
+ * byte[1] got read as 0, which might be bogus. See fusb302_pd_rx_keep()
+ */
+ bool rx_h1_unsure;
+ /* MessageID of the partner's last SOP message, -1 if none since reset */
+ s8 rx_last_msgid;
+ /*
+ * TX FIFO contains a pre-staged GoodCRC to the partner's next message,
+ * with predicted MessageID = staged_msgid. See fusb302_pd_stage_ack()
+ */
+ bool staged;
+ u8 staged_msgid;
+ /* A GoodCRC the driver sent may still be going out on CC */
+ bool ack_on_cc;
bool vbus_present;
enum typec_cc_polarity cc_polarity;
enum typec_cc_status cc1;
@@ -880,6 +895,7 @@ static void fusb302_pd_rx_reset(struct fusb302_chip *chip, bool cut_short)
chip->rx_len = 0;
chip->rx_resync = cut_short;
chip->rx_acked = false;
+ chip->rx_h1_unsure = false;
}
static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on)
@@ -920,6 +936,7 @@ static int fusb302_pd_set_interrupts(struct fusb302_chip *chip, bool on)
static int fusb302_pd_rx_start(struct fusb302_chip *chip);
static void fusb302_pd_check_ack_async(struct fusb302_chip *chip);
+static int fusb302_pd_unstage_ack(struct fusb302_chip *chip);
static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
{
@@ -951,6 +968,9 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on)
}
/* Nothing flushed reports its outcome */
chip->tx_pending = false;
+ chip->staged = false;
+ chip->ack_on_cc = false;
+ chip->rx_last_msgid = -1;
ret = on ? fusb302_pd_rx_start(chip) :
fusb302_pd_set_auto_goodcrc(chip, false);
if (ret < 0) {
@@ -1011,6 +1031,8 @@ static int tcpm_set_roles(struct tcpc_dev *dev, bool attached,
}
chip->pwr_role = pwr;
chip->data_role = data;
+ /* A GoodCRC staged before carries the old roles: never send it */
+ fusb302_pd_unstage_ack(chip);
fusb302_log(chip, "pd header := %s, %s", typec_role_name[pwr],
typec_data_role_name[data]);
done:
@@ -1072,12 +1094,14 @@ static const u8 fusb302_sop_tokens[][4] = {
},
};
-static int fusb302_pd_send_message(struct fusb302_chip *chip,
+/*
+ * Fill buf with the TX FIFO bytes for msg, all but the TXON that sends them,
+ * and return how many
+ */
+static int fusb302_pd_pack_message(struct fusb302_chip *chip,
enum tcpm_transmit_type type,
- const struct pd_message *msg)
+ const struct pd_message *msg, u8 *buf)
{
- int ret = 0;
- u8 buf[40];
u8 pos = 0;
int len;
@@ -1108,6 +1132,21 @@ static int fusb302_pd_send_message(struct fusb302_chip *chip,
buf[pos++] = FUSB302_TKN_EOP;
/* turn tx off after sending message */
buf[pos++] = FUSB302_TKN_TXOFF;
+
+ return pos;
+}
+
+static int fusb302_pd_send_message(struct fusb302_chip *chip,
+ enum tcpm_transmit_type type,
+ const struct pd_message *msg)
+{
+ int ret = 0;
+ u8 buf[40];
+ int pos;
+
+ pos = fusb302_pd_pack_message(chip, type, msg, buf);
+ if (pos < 0)
+ return pos;
/* start transmission */
buf[pos++] = FUSB302_TKN_TXON;
@@ -1115,11 +1154,41 @@ static int fusb302_pd_send_message(struct fusb302_chip *chip,
if (ret < 0)
return ret;
fusb302_log(chip, "sending PD message header: %x", msg->header);
- fusb302_log(chip, "sending PD message len: %d", len);
+ fusb302_log(chip, "sending PD message len: %d",
+ pd_header_cnt_le(msg->header) * 4);
return ret;
}
+/*
+ * For staged_msgid: the TX FIFO may hold some of a GoodCRC, or one gone stale,
+ * which is never to be sent, only flushed
+ */
+#define FUSB302_STAGED_STALE 0xff
+
+/*
+ * Unstage a previously staged GoodCRC from the TX FIFO, as our circumstances
+ * have changed and we no longer want to send it. Should the flush fail, it is
+ * still there, to be flushed before anything else goes into the TX FIFO, but
+ * never sent.
+ */
+static int fusb302_pd_unstage_ack(struct fusb302_chip *chip)
+{
+ int ret;
+
+ if (!chip->staged)
+ return 0;
+ ret = fusb302_pd_tx_flush(chip);
+ if (ret < 0) {
+ chip->staged_msgid = FUSB302_STAGED_STALE;
+ fusb302_log(chip, "cannot flush staged GoodCRC, ret=%d", ret);
+ return ret;
+ }
+ chip->staged = false;
+
+ return 0;
+}
+
static int fusb302_pd_send_hardreset(struct fusb302_chip *chip)
{
return fusb302_i2c_set_bits(chip, FUSB_REG_CONTROL3,
@@ -1152,6 +1221,10 @@ static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
*/
fusb302_pd_check_ack_async(chip);
chip->ack_check = false;
+ /* Didn't need that GoodCRC we pre-staged in the FIFO - get rid of it */
+ ret = fusb302_pd_unstage_ack(chip);
+ if (ret < 0)
+ goto done;
switch (type) {
case TCPC_TX_SOP:
case TCPC_TX_SOP_PRIME:
@@ -1182,6 +1255,7 @@ static int tcpm_pd_transmit(struct tcpc_dev *dev, enum tcpm_transmit_type type,
chip->tx_pending = true;
chip->tx_start = ktime_get();
}
+done:
mutex_unlock(&chip->lock);
return ret;
@@ -1554,6 +1628,12 @@ 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);
+ /*
+ * MessageIDs start over, so the staged GoodCRC is gone or stale. Should
+ * the flush fail, it is never sent at least: see fusb302_pd_unstage_ack()
+ */
+ chip->rx_last_msgid = -1;
+ fusb302_pd_unstage_ack(chip);
return fusb302_i2c_set_bits(chip, FUSB_REG_RESET,
FUSB_REG_RESET_PD_RESET);
@@ -1617,9 +1697,8 @@ static bool fusb302_pd_tx_waiting(struct fusb302_chip *chip)
* The chip only decodes the low four bits of the message type, so it mistakes
* Get_Source_Cap_Extended (0x11) for a GoodCRC (0x1): it neither acknowledges
* it nor raises GCRCSENT, only CRC_CHK, yet still queues it in the RX FIFO.
- * Send the GoodCRC from here once the message checks out. It goes out well
- * after 195us (tTransmit), but the partner retries with the same MessageID and
- * usually accepts it then, rather than soft reset over and over.
+ * Send the GoodCRC from here: from fusb302_pd_rx_fast_ack() as soon as the
+ * header is in, or else once the whole message checks out.
*/
static void fusb302_pd_rx_ack(struct fusb302_chip *chip)
{
@@ -1648,14 +1727,30 @@ static void fusb302_pd_rx_ack(struct fusb302_chip *chip)
* sends as one that needs no GoodCRC in return, so it sends this one
* once, without retries or RETRYFAIL, whatever the retry count is.
*/
- ack.header = PD_HEADER_LE(PD_CTRL_GOOD_CRC, chip->pwr_role,
- chip->data_role, PD_REV20,
- pd_header_msgid(header), 0);
- ret = fusb302_pd_send_message(chip, TCPC_TX_SOP, &ack);
- if (ret >= 0)
+ if (chip->staged && chip->staged_msgid == pd_header_msgid(header)) {
+ ret = fusb302_i2c_write(chip, FUSB_REG_FIFOS,
+ FUSB302_TKN_TXON);
+ if (ret >= 0) {
+ chip->staged = false;
+ fusb302_log(chip, "sending staged GoodCRC, id %u",
+ pd_header_msgid(header));
+ }
+ } else {
+ ret = fusb302_pd_unstage_ack(chip);
+ if (ret >= 0) {
+ ack.header = PD_HEADER_LE(PD_CTRL_GOOD_CRC,
+ chip->pwr_role,
+ chip->data_role, PD_REV20,
+ pd_header_msgid(header), 0);
+ ret = fusb302_pd_send_message(chip, TCPC_TX_SOP, &ack);
+ }
+ }
+ if (ret >= 0) {
chip->ack_check = true;
- else
+ chip->ack_on_cc = true;
+ } else {
fusb302_log(chip, "cannot acknowledge PD message, ret=%d", ret);
+ }
}
static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n)
@@ -1663,6 +1758,7 @@ static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n)
chip->rx_len -= n;
memmove(chip->rx_buf, chip->rx_buf + n, chip->rx_len);
chip->rx_acked = false;
+ chip->rx_h1_unsure = false;
}
static bool fusb302_pd_rx_is_token(u8 byte)
@@ -1707,6 +1803,50 @@ static int fusb302_pd_rx_find(struct fusb302_chip *chip)
return 0;
}
+/*
+ * Append n header bytes that fusb302_pd_rx_fast_ack() read from the FIFO while
+ * a packet may have been landing to rx_buf. Reading an empty FIFO returns 0
+ * and takes nothing, so real bytes may come interleaved with such zeros. As
+ * neither the token nor header byte[0], which holds the message type, is ever
+ * 0, the real bytes are the non-zero ones. The only exception is header
+ * byte[1], which holds the power role or cable plug bit and is non-zero from
+ * a source or a cable plug, but may be 0 from a sink: leave that case to the
+ * CRC, see fusb302_pd_rx_resolve_h1().
+ */
+static void fusb302_pd_rx_keep(struct fusb302_chip *chip, const u8 *fifo,
+ int n)
+{
+ int i;
+
+ for (i = 0; i < n; i++) {
+ if (fifo[i])
+ chip->rx_buf[chip->rx_len++] = fifo[i];
+ else if (chip->rx_len == 2 && chip->pwr_role == TYPEC_SOURCE &&
+ (chip->rx_buf[0] & FUSB302_RX_TKN_MASK) ==
+ FUSB302_RX_TKN_SOP)
+ chip->rx_h1_unsure = true;
+ }
+}
+
+/*
+ * rx_buf holds the token, header byte[0] and the 4 bytes that followed it in
+ * the FIFO. If header byte[1] was a real 0, those are the CRC of a message
+ * with no data objects. Otherwise we need to look for the real header byte[1]
+ * in the first of the bytes that followed the bogus 0.
+ */
+static void fusb302_pd_rx_resolve_h1(struct fusb302_chip *chip)
+{
+ u8 header[2] = { chip->rx_buf[1], 0 };
+
+ chip->rx_h1_unsure = false;
+ if (~crc32_le(~0U, header, sizeof(header)) !=
+ get_unaligned_le32(chip->rx_buf + 2))
+ return;
+ memmove(chip->rx_buf + 3, chip->rx_buf + 2, chip->rx_len - 2);
+ chip->rx_buf[2] = 0;
+ chip->rx_len++;
+}
+
/*
* 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
@@ -1776,6 +1916,18 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
fusb302_pd_rx_drop(chip, 1);
dropped++;
}
+ /* See fusb302_pd_rx_keep() */
+ if (chip->rx_h1_unsure) {
+ if (chip->rx_len < 6) {
+ ret = fusb302_pd_rx_read(chip, 6 - chip->rx_len);
+ if (ret < 0)
+ goto out;
+ if (ret)
+ continue;
+ goto out;
+ }
+ fusb302_pd_rx_resolve_h1(chip);
+ }
if (chip->rx_len < 3)
len = 3;
else
@@ -1834,12 +1986,14 @@ 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) {
fusb302_pd_tx_complete(chip, TCPC_TX_SUCCESS);
- else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1)
+ } else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1) {
tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP_PRIME);
- else
+ } else {
+ chip->rx_last_msgid = pd_header_msgid_le(msg.header);
tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP);
+ }
out:
if (dropped)
fusb302_log(chip, "dropped %d bytes from the RX FIFO", dropped);
@@ -1965,6 +2119,65 @@ static int fusb302_pd_rx_drain(struct fusb302_chip *chip)
return 0;
}
+/*
+ * Prepare a GoodCRC to be sent for the next message from the partner, so that
+ * it can be sent with only a TXON byte on the timing critical path. If it's not
+ * needed, it means we are in a less time constrained path and can afford to
+ * flush the TX FIFO and re-stage the correct message normally.
+ * See fusb302_pd_rx_fast_ack() for why that matters.
+ */
+static void fusb302_pd_stage_ack(struct fusb302_chip *chip)
+{
+ /* From 0 after a reset, as rx_last_msgid is -1 then */
+ u8 msgid = (chip->rx_last_msgid + 1) & 7;
+ struct pd_message ack = {};
+ u8 buf[40];
+ u8 status1;
+ int ret;
+ int pos;
+
+ /* While TCPM waits for its own message, that may still be in there */
+ if (!chip->pd_rx_on || fusb302_pd_tx_waiting(chip) ||
+ (chip->staged && chip->staged_msgid == msgid))
+ return;
+
+ /*
+ * Flushing the TX FIFO would cut short a GoodCRC from the driver still
+ * going out: CC is busy until it is done. Leave staging for later if
+ * CC stays busy.
+ */
+ if (chip->ack_on_cc) {
+ ret = fusb302_wait_cc_idle(chip, &status1);
+ if (ret > 0)
+ return;
+ if (ret < 0)
+ goto err;
+ chip->ack_on_cc = false;
+ }
+
+ /*
+ * Whatever is left in the TX FIFO would go out first, and should the
+ * flush or the write fail, it holds what is never to be sent
+ */
+ chip->staged = true;
+ chip->staged_msgid = FUSB302_STAGED_STALE;
+ ret = fusb302_pd_tx_flush(chip);
+ if (ret < 0)
+ goto err;
+
+ ack.header = PD_HEADER_LE(PD_CTRL_GOOD_CRC, chip->pwr_role,
+ chip->data_role, PD_REV20, msgid, 0);
+ pos = fusb302_pd_pack_message(chip, TCPC_TX_SOP, &ack, buf);
+ ret = fusb302_i2c_block_write(chip, FUSB_REG_FIFOS, pos, buf);
+ if (ret < 0)
+ goto err;
+ chip->staged_msgid = msgid;
+
+ return;
+err:
+ fusb302_log(chip, "cannot stage GoodCRC, ret=%d", ret);
+}
+
static void fusb302_pd_rx_recheck_work(struct work_struct *work)
{
struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
@@ -1978,6 +2191,8 @@ static void fusb302_pd_rx_recheck_work(struct work_struct *work)
fusb302_log(chip, "cannot read in PD message, ret=%d",
ret);
fusb302_pd_check_ack_async(chip);
+ /* A message read here moves the next MessageID on */
+ fusb302_pd_stage_ack(chip);
}
mutex_unlock(&chip->lock);
}
@@ -2120,6 +2335,75 @@ static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
return ret;
}
+/*
+ * A GoodCRC is due within 195us (tTransmit) of the message, and the partner
+ * retries once 0.9-1.1ms (tReceive) pass without one. Reading the interrupt
+ * and status registers, waiting for CC to go idle and only then reading the
+ * RX FIFO takes far longer over I2C, so start by reading STATUS0, STATUS1,
+ * INTERRUPT and the head of the RX FIFO in a single transfer, as the address
+ * increments up to the FIFO and stays there, and acknowledge a
+ * Get_Source_Cap_Extended at once if the FIFO starts with one. Together with
+ * the GoodCRC staged in the TX FIFO, that makes it in time. This reads the
+ * FIFO without waiting for CC to go idle: see fusb302_pd_rx_keep() for the
+ * bytes still landing.
+ *
+ * The GoodCRC then goes out before the CRC of the message is in, as waiting
+ * for it would take past 195us (tTransmit). Should the CRC fail, the message is
+ * dropped as if the GoodCRC had not gone out, and the partner, which
+ * considers it delivered, soft resets when no answer comes, as it does when
+ * the message goes unacknowledged.
+ *
+ * A Get_Source_Cap_Extended from a sink with MessageID 0, every 8th from it,
+ * has a second header byte of 0, which a read that finds CC busy again can't
+ * tell from a byte still to come: that one is acknowledged once its CRC is in.
+ */
+static void fusb302_pd_rx_fast_ack(struct fusb302_chip *chip)
+{
+ u8 regs[6];
+ int ret, n, tries;
+
+ if (!chip->pd_rx_on)
+ return;
+
+ if (!chip->rx_len) {
+ ret = fusb302_i2c_block_read(chip, FUSB_REG_STATUS0,
+ sizeof(regs), regs);
+ if (ret < 0)
+ return;
+ chip->pending_interrupt |= regs[2];
+ /* With ACTIVITY clear, the token is over 250us away */
+ if (regs[0] & FUSB_REG_STATUS0_ACTIVITY) {
+ fusb302_pd_rx_keep(chip, regs + 3, 3);
+ } else if (!(regs[1] & FUSB_REG_STATUS1_RX_EMPTY)) {
+ memcpy(chip->rx_buf, regs + 3, 3);
+ chip->rx_len = 3;
+ }
+ if (chip->rx_len && !fusb302_pd_rx_is_token(chip->rx_buf[0]))
+ chip->rx_resync = true;
+ }
+
+ /*
+ * A read that catches the header as it lands gets only part of it,
+ * and the rest comes in within tens of microseconds: read the rest,
+ * but no further, as fusb302_pd_rx_keep() can only tell what zeros
+ * in the header mean. The packet is still landing then, so skip the
+ * status registers.
+ */
+ for (tries = 0; tries < 2; tries++) {
+ n = 3 - chip->rx_len;
+ if (!chip->rx_len || n <= 0 || chip->rx_resync ||
+ chip->rx_h1_unsure)
+ break;
+ ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, n, regs);
+ if (ret < 0)
+ return;
+ fusb302_pd_rx_keep(chip, regs, n);
+ }
+
+ if (chip->rx_len >= 3 && !chip->rx_resync)
+ fusb302_pd_rx_ack(chip);
+}
+
static void fusb302_irq_handle(struct fusb302_chip *chip)
{
int ret = 0;
@@ -2134,6 +2418,7 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
bool intr_comp_chng;
mutex_lock(&chip->lock);
+ fusb302_pd_rx_fast_ack(chip);
again:
/* grab a snapshot of intr flags */
intr_togdone = chip->intr_togdone;
@@ -2276,6 +2561,8 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
if (chip->ack_check && fusb302_pd_check_ack(chip) >= 0 &&
chip->pending_interrupt)
goto again;
+ /* Get the GoodCRC to the next message ready */
+ fusb302_pd_stage_ack(chip);
mutex_unlock(&chip->lock);
}
--
2.56.0
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-27 10:41 UTC | newest]
Thread overview: 7+ 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-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
2026-11-27 10:40 ` [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time Alexey Charkov
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®