From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047614; b=ypHL69h0Mf+/hmdlFL74BCnR7+LXBInU9mPlbMfAOV+AEATsXOeBGx5XiWm3YUC2D6L3 zIfZCPqSIY0XJ4NqqbKMSKEOQrJ5EWgpqncTJ0mjYCY/3Qeu5na9z+IIwn/3lCkrkIxYj YKvBVKYHlzIGTxMG/cwbKFKFGu77IjpMswP0eC8m4tI8VWpHXfge4TYcqCNweotS3DN0D 7vjCffEFhNYNkmP2l93VjVyA/S0ttmuJDBDx+46Ha9lCXAzhYPCwURyEJQvFx09SI71Uv Ud+3+n0D6G8RplAoeCbQ6WADqTnoG+2aj+OsXQk2Ux6yz1bYizbuMb738poYjdOmAsg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047614; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:Date:From:To: Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=cmOc63Dq9K1oIAfRALpGS6jt4F9etOHLEHX9CPCgBVg=; b=S5lKtuwdgxWVt5O8IQuHifaTNXYZMGg9KcBbcs/+5h91UdccYG51A2w0kYuVi9WtDDFn Dw7wOvxIThLg04VFxTNIBlFn3pEbETDcIuqn2qJk7Q54llzcprbwtl7ZKIKBzZeN0lvDr ToUbGlLhT5QalvHKuzok36ytLKneeUvLkOwKEx1mHotmntJyFcsGrv0Q5Ecy04fSy7GIw bNXOiuj+WsHmyabMy3cjpoRj2jc68vKPd2yB1gkF/v7IkDn6GJvJpNvHqe0Qyz7gsqkpr KuwqX+ybvemr200jXmc2zWrt3rP4UjLPRp3komoM9hJGRLroGF3EU5bUn3ZHt+p9KNg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=SsQkoJIf; arc=none smtp.remote-ip=192.198.163.14 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3704C606A325; Mon, 30 Nov 2026 14:06:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3704C606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=SsQkoJIf DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796047613; x=1827583613; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=cmOc63Dq9K1oIAfRALpGS6jt4F9etOHLEHX9CPCgBVg=; b=SsQkoJIf3WRbcllXxX3ETKAgKLQ3j8NS35v76F2t3LKYGFog049iUF2n NtFtMV8rf8JXLpbP2bamO27QATB4ZupFAdLdBkOj9XkmqiG+6t1zGVjJO vMtHG3mUY9NEUaLzCQgZ3Lh1RH26Uz5E196kEhjMGdZZz98HOB0+T+b+7 NMVScmaw1D0FTfUvSNiorIaeDByA5Fb4hBNPJ5ICyx9maozStoC6PzToD XsAaUDN0xamfMQDQDGspU8tN3PVmLCf9mYUTKZFLCRDkPZK366kjJixd8 VgUxyI6rrSM/bvyhC1jQqF+wv57Wr3wT4535cUGTku02C34dLOGHbSLXx g==; X-CSE-ConnectionGUID: SS1e7WzOSkKINLX3OApyVw== X-CSE-MsgGUID: 052lMRJDQMqVYukFHHQFOQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4844081" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4844081" 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-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261127-fusb302-getsourcecap-v2-1-6770eb1e7945@flipper.net> 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