From: Alexey Charkov <alchark@flipper.net>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Guenter Roeck <linux@roeck-us.net>,
Yueyao Zhu <yueyao@google.com>, Heiko Stuebner <heiko@sntech.de>
Cc: Sebastian Reichel <sebastian.reichel@collabora.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org,
linux-kernel@vger.kernel.org,
Alexey Charkov <alchark@flipper.net>,
stable@vger.kernel.org
Subject: [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on
Date: Fri, 27 Nov 2026 14:40:48 +0400 [thread overview]
Message-ID: <20261127-fusb302-getsourcecap-v2-3-6770eb1e7945@flipper.net> (raw)
In-Reply-To: <20261127-fusb302-getsourcecap-v2-0-6770eb1e7945@flipper.net>
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
next prev parent reply other threads:[~2026-11-27 10:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127-fusb302-getsourcecap-v2-3-6770eb1e7945@flipper.net \
--to=alchark@flipper.net \
--cc=bigeasy@linutronix.de \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=heiko@sntech.de \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=sebastian.reichel@collabora.com \
--cc=stable@vger.kernel.org \
--cc=yueyao@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®