From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69976606A320 for ; Mon, 30 Nov 2026 14:07:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=CjXyF3dd; dkim=fail reason="signature verification failed" (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TQsSGB5E DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=baM58Xxl0/vTiQq6NlNSDZEQYA39XXRcMmDTgSHy3v8=; b=CjXyF3ddKCsdZiBNyz8l1FcgwE C/hTSVRWh3P6O9Yx1285sBQcnzUoVdfcH+m5A2exHppB9aomRS36mdrLR1BQkY/ZXfU+i2LNn8w1P 0JO7/z9Fm0DKcXsUod4XvSs39k2I1MM6CYcafTjgoyV/Unh1Ncvbp4TDDL5BAbJt36ZNfOP3Ruqt+ e++oGx4lcmV9MzmZE8jzpmBlJdG/6aBWCiLu5HidyF3xOhz3ULH6Vp40ryf5d6K7SohxY3iPDDSyY zkhQx2ewAiDxkzgWFcoGVyIgjPZfcA3j/uihZhqOI5KAPwrEE4Dmyn3UAXjw7amfnLhkSMx2SOaYL 04GWzhjw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY21y-00000004pO6-3jYH; Mon, 30 Nov 2026 14:06:58 +0000 Received: from mgamail.intel.com ([192.198.163.14]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xY21v-00000004pNQ-0xEC; Mon, 30 Nov 2026 14:06:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796047615; x=1827583615; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=cmOc63Dq9K1oIAfRALpGS6jt4F9etOHLEHX9CPCgBVg=; b=TQsSGB5EVEHE4ngUMIsEkubk62CZDq7WLLolecv03f/VSv8tlb9JOvR0 CLQmwSMwgqt4RQiPELhrl+h9UiWg0ltgYZiFsc0ve81AlvQW1FrXPQdpJ uMmg9R1CoUCTP3pMiW5nqEiXrNbtuY4Ks9Gvz2jkalqqd0WaMGTpFjnxV kdZGe8q3Pr3H+Ro6NOPQMRL+vNx7F7Y1nMoEJJ2lMBmwu++DbN0ToVMuy e1pWiudAZ9Eza381Ezcn6c1xcTJWzbP8Tx821wvXRMk6VSLx2Ja6MbSFo NzD7sxaBaOV414KCbrTadXhu9+zDx7NLqTewqaFMDrYlkJdGpUDHAyiwa g==; X-CSE-ConnectionGUID: 3K4rOLYqSL2bSsYxipWy9Q== X-CSE-MsgGUID: lW8VNjf7Qr+M7rVV9Ye1og== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4844080" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4844080" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 06:06:52 -0800 X-CSE-ConnectionGUID: QeIdj8enT0Wj8xOfUsA3EQ== X-CSE-MsgGUID: K0j9CWncSQW4O3Pyg10M+g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="13075400" Received: from black.igk.intel.com ([10.91.253.5]) by fmviesa005.fm.intel.com with ESMTP; 30 Nov 2026 06:06:50 -0800 Received: by black.igk.intel.com (Postfix, from userid 1008) id 6BCEC99; Mon, 30 Nov 2026 15:06:48 +0100 (CET) Date: Mon, 30 Nov 2026 15:06:48 +0100 From: Heikki Krogerus To: Alexey Charkov Cc: Greg Kroah-Hartman , Guenter Roeck , Yueyao Zhu , Heiko Stuebner , Sebastian Reichel , Sebastian Andrzej Siewior , linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Message-ID: References: <20261127-fusb302-getsourcecap-v2-0-6770eb1e7945@flipper.net> <20261127-fusb302-getsourcecap-v2-1-6770eb1e7945@flipper.net> X-Archived-By: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20261127-fusb302-getsourcecap-v2-1-6770eb1e7945@flipper.net> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_060655_317226_6253B5B9 X-CRM114-Status: GOOD ( 56.65 ) X-Spam-Score: -4.2 (----) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-rockchip" X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Fri, Nov 27, 2026 at 02:40:46PM +0400, Alexey Charkov wrote: > The chip writes each byte into the RX FIFO as it comes off CC, so a > flush in the middle of a packet leaves the rest of that packet in the > FIFO with no token in front, and the driver reads that tail, padded with > zeros where the FIFO runs dry, as a message. TCPM acts on the junk > header. On a Flipper One attaching as a sink, the flush that turns PD RX > on raced the partner's first message in 2 of about 35 attaches: a > "message" with header 0 made TCPM see a data role mismatch and go to > ERROR_RECOVERY. With the FIFO flushed on purpose 27 times while the > partner sent a Ping every 20ms, 98 junk messages reached TCPM, and it > reset 17 times. > > Flushing the FIFO again once the junk is spotted is no cure, as any > message behind the tail may have been acknowledged already, and the > partner never sends it again. > > Keep the bytes read from the FIFO in a buffer instead, and scan for a full > valid message, including a CRC that checks out, to avoid interpreting junk > bytes as a start of a message. > > In the same test, no junk reaches TCPM now, and only the Pings that the > forced flushes cut short go missing. > > Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver") > Cc: stable@vger.kernel.org > Signed-off-by: Alexey Charkov Acked-by: Heikki Krogerus > --- > 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 > +#include > #include > #include > #include > @@ -27,6 +28,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -72,6 +74,11 @@ static const u8 rd_mda_value[] = { > #define LOG_BUFFER_ENTRIES 1024 > #define LOG_BUFFER_ENTRY_SIZE 128 > > +/* An SOP* token, the header, up to seven data objects and the CRC */ > +#define FUSB302_RX_MSG_MAX (1 + 2 + PD_MAX_PAYLOAD * 4 + 4) > +/* The size of the RX FIFO */ > +#define FUSB302_RX_FIFO_LEN 80 > + > struct fusb302_chip { > struct device *dev; > struct i2c_client *i2c_client; > @@ -110,6 +117,17 @@ struct fusb302_chip { > bool vbus_on; > bool charge_on; > bool pd_rx_on; > + /* > + * Bytes read from the RX FIFO but not handled yet: the start of a > + * message to read on, or bytes to find the next message in > + */ > + u8 rx_buf[FUSB302_RX_MSG_MAX]; > + u8 rx_len; > + /* > + * The next message may not be at the start of the buffer. > + * Look for its start first before processing. > + */ > + bool rx_resync; > bool vbus_present; > enum typec_cc_polarity cc_polarity; > enum typec_cc_status cc1; > @@ -831,6 +849,17 @@ static int fusb302_pd_rx_flush(struct fusb302_chip *chip) > FUSB_REG_CONTROL1_RX_FLUSH); > } > > +/* > + * Forget whatever was read from the RX FIFO but not handled yet. After a flush > + * or a reset, the FIFO may go on with the rest of a packet cut short, so look > + * for the next message then, rather than read on from the first byte. > + */ > +static void fusb302_pd_rx_reset(struct fusb302_chip *chip, bool cut_short) > +{ > + chip->rx_len = 0; > + chip->rx_resync = cut_short; > +} > + > static int fusb302_pd_set_auto_goodcrc(struct fusb302_chip *chip, bool on) > { > if (on) > @@ -885,6 +914,7 @@ static int tcpm_set_pd_rx(struct tcpc_dev *dev, bool on) > fusb302_log(chip, "cannot flush pd rx buffer, ret=%d", ret); > goto done; > } > + fusb302_pd_rx_reset(chip, true); > ret = fusb302_pd_tx_flush(chip); > if (ret < 0) { > fusb302_log(chip, "cannot flush pd tx buffer, ret=%d", ret); > @@ -1477,43 +1507,168 @@ static int fusb302_handle_togdone(struct fusb302_chip *chip) > > static int fusb302_pd_reset(struct fusb302_chip *chip) > { > + /* What was read of a message before the reset is of no use after it */ > + fusb302_pd_rx_reset(chip, true); > + > return fusb302_i2c_set_bits(chip, FUSB_REG_RESET, > FUSB_REG_RESET_PD_RESET); > } > > -static int fusb302_pd_read_message(struct fusb302_chip *chip, > - struct pd_message *msg) > +static void fusb302_pd_rx_drop(struct fusb302_chip *chip, int n) > { > - int ret = 0; > - u8 token; > - u8 crc[4]; > - int len; > + chip->rx_len -= n; > + memmove(chip->rx_buf, chip->rx_buf + n, chip->rx_len); > +} > > - /* first SOP token */ > - ret = fusb302_i2c_read(chip, FUSB_REG_FIFOS, &token); > - if (ret < 0) > - return ret; > - ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 2, > - (u8 *)&msg->header); > - if (ret < 0) > - return ret; > - len = pd_header_cnt_le(msg->header) * 4; > - /* add 4 to length to include the CRC */ > - if (len > PD_MAX_PAYLOAD * 4) { > - fusb302_log(chip, "PD message too long %d", len); > - return -EINVAL; > +static bool fusb302_pd_rx_is_token(u8 byte) > +{ > + byte &= FUSB302_RX_TKN_MASK; > + > + return byte == FUSB302_RX_TKN_SOP || byte == FUSB302_RX_TKN_SOP1; > +} > + > +/* FIFO bytes taken by the message whose token is at rx_buf[off] */ > +static int fusb302_pd_rx_msg_len(struct fusb302_chip *chip, int off) > +{ > + u16 header = get_unaligned_le16(chip->rx_buf + off + 1); > + > + return 1 + 2 + pd_header_cnt(header) * 4 + 4; > +} > + > +/* The chip keeps the CRC it received after the header and data objects */ > +static bool fusb302_pd_rx_crc_ok(struct fusb302_chip *chip, int off, int len) > +{ > + return ~crc32_le(~0U, chip->rx_buf + off + 1, len - 5) == > + get_unaligned_le32(chip->rx_buf + off + len - 4); > +} > + > +/* > + * Return the offset of the first message past the start of rx_buf that it > + * holds in full, with its CRC checking out, or 0 if there is none > + */ > +static int fusb302_pd_rx_find(struct fusb302_chip *chip) > +{ > + int off, len; > + > + for (off = 1; off + 3 <= chip->rx_len; off++) { > + if (!fusb302_pd_rx_is_token(chip->rx_buf[off])) > + continue; > + len = fusb302_pd_rx_msg_len(chip, off); > + if (off + len <= chip->rx_len && > + fusb302_pd_rx_crc_ok(chip, off, len)) > + return off; > } > - if (len > 0) { > - ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, len, > - (u8 *)msg->payload); > + > + return 0; > +} > + > +/* > + * Read up to n more bytes into rx_buf and return how many. When the FIFO is in > + * step, rx_buf starts with a message the chip has received in full, so read the > + * rest in one go. Otherwise read one byte at a time while the FIFO holds data, > + * as a dried out FIFO starts returning zeros instead of received data, and real > + * bytes will be returned to the next read instead once the device receives them > + */ > +static int fusb302_pd_rx_read(struct fusb302_chip *chip, int n) > +{ > + u8 status1; > + int ret; > + > + if (chip->rx_resync) { > + ret = fusb302_i2c_read(chip, FUSB_REG_STATUS1, &status1); > if (ret < 0) > return ret; > + if (status1 & FUSB_REG_STATUS1_RX_EMPTY) > + return 0; > + n = 1; > } > - /* another 4 bytes to read CRC out */ > - ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, 4, crc); > - if (ret < 0) > + ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, n, > + chip->rx_buf + chip->rx_len); > + if (ret < 0) { > + /* Some of it may have left the FIFO all the same */ > + chip->rx_resync = true; > return ret; > - fusb302_log(chip, "PD message header: %x", msg->header); > + } > + chip->rx_len += n; > + > + return n; > +} > + > +/* > + * Read in and handle the next message in the RX FIFO. Return 1 if there was > + * one, 0 if the FIFO ran out first, or a negative error code. > + */ > +static int fusb302_pd_read_message(struct fusb302_chip *chip) > +{ > + struct pd_message msg = {}; > + int dropped = 0; > + int len, off, ret; > + u8 token; > + > + for (;;) { > + /* > + * Already discarded a full FIFO's worth of junk - leave > + * whatever remains to another pass > + */ > + if (dropped >= FUSB302_RX_FIFO_LEN) { > + ret = 0; > + goto out; > + } > + /* > + * The chip writes each byte into the FIFO as it comes off CC, > + * so a flush in the middle of a packet leaves the rest of it > + * there with no token in front. Following bytes may look like a > + * valid token without being one, so only trust them if the CRC > + * of the resulting message checks out, too. If not, look on > + * from the next byte: there may be a next message that the chip > + * has already acknowledged, and we must not miss it. > + */ > + while (chip->rx_len && > + !fusb302_pd_rx_is_token(chip->rx_buf[0])) { > + chip->rx_resync = true; > + fusb302_pd_rx_drop(chip, 1); > + dropped++; > + } > + if (chip->rx_len < 3) > + len = 3; > + else > + len = fusb302_pd_rx_msg_len(chip, 0); > + if (chip->rx_len < len) { > + ret = fusb302_pd_rx_read(chip, len - chip->rx_len); > + if (ret < 0) > + goto out; > + if (ret) > + continue; > + /* > + * The FIFO ran dry, which may be due to a bogus token > + * claiming more bytes than will ever come. See if the > + * following bytes contain a complete valid message. > + */ > + off = fusb302_pd_rx_find(chip); > + if (!off) > + goto out; > + fusb302_pd_rx_drop(chip, off); > + dropped += off; > + continue; > + } > + if (fusb302_pd_rx_crc_ok(chip, 0, len)) > + break; > + if (!chip->rx_resync) > + fusb302_log(chip, "PD message failed CRC check, header: %x", > + get_unaligned_le16(chip->rx_buf + 1)); > + chip->rx_resync = true; > + fusb302_pd_rx_drop(chip, 1); > + dropped++; > + } > + > + chip->rx_resync = false; > + token = chip->rx_buf[0]; > + memcpy(&msg, chip->rx_buf + 1, len - 5); > + fusb302_pd_rx_drop(chip, len); > + len -= 7; > + ret = 1; > + > + fusb302_log(chip, "PD message header: %x", msg.header); > fusb302_log(chip, "PD message len: %d", len); > > /* > @@ -1526,13 +1681,15 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip, > * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need > * to check the message type to ensure correct reporting to TCPM. > */ > - if ((!len) && (pd_header_type_le(msg->header) == PD_CTRL_GOOD_CRC)) > + if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC) > tcpm_pd_transmit_complete(chip->tcpm_port, TCPC_TX_SUCCESS); > else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1) > - tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP_PRIME); > + tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP_PRIME); > else > - tcpm_pd_receive(chip->tcpm_port, msg, TCPC_TX_SOP); > - > + tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP); > +out: > + if (dropped) > + fusb302_log(chip, "dropped %d bytes from the RX FIFO", dropped); > return ret; > } > > @@ -1605,7 +1762,6 @@ static void fusb302_irq_work(struct work_struct *work) > bool intr_togdone; > bool intr_bc_lvl; > bool intr_comp_chng; > - struct pd_message pd_msg; > > mutex_lock(&chip->lock); > /* grab a snapshot of intr flags */ > @@ -1693,7 +1849,7 @@ static void fusb302_irq_work(struct work_struct *work) > > if (interrupta & FUSB_REG_INTERRUPTA_TX_SUCCESS) { > fusb302_log(chip, "IRQ: PD tx success"); > - ret = fusb302_pd_read_message(chip, &pd_msg); > + ret = fusb302_pd_read_message(chip); > if (ret < 0) { > fusb302_log(chip, > "cannot read in PD message, ret=%d", ret); > @@ -1713,7 +1869,7 @@ static void fusb302_irq_work(struct work_struct *work) > > if (interruptb & FUSB_REG_INTERRUPTB_GCRCSENT) { > fusb302_log(chip, "IRQ: PD sent good CRC"); > - ret = fusb302_pd_read_message(chip, &pd_msg); > + ret = fusb302_pd_read_message(chip); > if (ret < 0) { > fusb302_log(chip, > "cannot read in PD message, ret=%d", ret); > > -- > 2.56.0 > -- heikki _______________________________________________ Linux-rockchip mailing list Linux-rockchip@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-rockchip