mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt
Date: Fri, 27 Nov 2026 14:40:47 +0400	[thread overview]
Message-ID: <20261127-fusb302-getsourcecap-v2-2-6770eb1e7945@flipper.net> (raw)
In-Reply-To: <20261127-fusb302-getsourcecap-v2-0-6770eb1e7945@flipper.net>

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


  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 ` Alexey Charkov [this message]
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

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-2-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®