[PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on
From: Alexey Charkov
Date: Fri Nov 27 2026 - 05:41:36 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
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