mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read
@ 2026-11-17  8:25 Alexey Charkov
  2026-11-20 13:04 ` Heikki Krogerus
  0 siblings, 1 reply; 4+ messages in thread
From: Alexey Charkov @ 2026-11-17  8:25 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu
  Cc: Sebastian Reichel, linux-usb, linux-kernel, Alexey Charkov

Reading INTERRUPT, INTERRUPTA or INTERRUPTB clears its flags in the chip,
so bailing out on an I2C error in a later read loses the flags already
read. On Flipper One, a NACK on INTERRUPTB just as a source raised VBUS
lost VBUSOK. The driver kept treating VBUS as off, so TCPM never attached
as a sink and left every Source_Capabilities unanswered.

Keep the flags pending until they are handled, and retry the reads a few
times, as such errors pass. Otherwise a flag left pending waits for the
next interrupt.

Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/usb/typec/tcpm/fusb302.c | 62 +++++++++++++++++++++++++++++++++-------
 1 file changed, 51 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 83bd287dadc7..9fb76fdd3b7b 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -100,6 +100,10 @@ struct fusb302_chip {
 	bool intr_togdone;
 	bool intr_bc_lvl;
 	bool intr_comp_chng;
+	/* interrupt flags read from the chip, but not handled yet */
+	u8 pending_interrupt;
+	u8 pending_interrupta;
+	u8 pending_interruptb;
 
 	/* port status */
 	bool vconn_on;
@@ -1499,6 +1503,43 @@ static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+/*
+ * Reading an interrupt register clears its flags in the chip, so keep them
+ * pending until handled: a bus error on a later read would otherwise lose
+ * them. Such errors pass, so retry a few times.
+ */
+static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
+{
+	int ret, tries;
+	u8 flags;
+
+	for (tries = 0; tries < 3; tries++) {
+		if (tries)
+			usleep_range(1000, 2000);
+
+		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &flags);
+		if (ret < 0)
+			continue;
+		chip->pending_interrupt |= flags;
+
+		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &flags);
+		if (ret < 0)
+			continue;
+		chip->pending_interrupta |= flags;
+
+		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &flags);
+		if (ret < 0)
+			continue;
+		chip->pending_interruptb |= flags;
+
+		ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, status0);
+		if (ret >= 0)
+			return 0;
+	}
+
+	return ret;
+}
+
 static void fusb302_irq_work(struct work_struct *work)
 {
 	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
@@ -1521,18 +1562,17 @@ static void fusb302_irq_work(struct work_struct *work)
 	intr_bc_lvl = chip->intr_bc_lvl;
 	intr_comp_chng = chip->intr_comp_chng;
 
-	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
-	if (ret < 0)
-		goto done;
-	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
-	if (ret < 0)
-		goto done;
-	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
-	if (ret < 0)
-		goto done;
-	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
-	if (ret < 0)
+	ret = fusb302_read_interrupts(chip, &status0);
+	if (ret < 0) {
+		fusb302_log(chip, "interrupts left pending, ret=%d", ret);
 		goto done;
+	}
+	interrupt = chip->pending_interrupt;
+	interrupta = chip->pending_interrupta;
+	interruptb = chip->pending_interruptb;
+	chip->pending_interrupt = 0;
+	chip->pending_interrupta = 0;
+	chip->pending_interruptb = 0;
 	fusb302_log(chip,
 		    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
 		    interrupt, interrupta, interruptb, status0);

---
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
change-id: 20261116-fusb302-irq-read-error-8a3ca5ef9ebc

Best regards,
--  
Alexey Charkov <alchark@flipper.net>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read
  2026-11-17  8:25 [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read Alexey Charkov
@ 2026-11-20 13:04 ` Heikki Krogerus
  2026-11-20 13:52   ` Alexey Charkov
  0 siblings, 1 reply; 4+ messages in thread
From: Heikki Krogerus @ 2026-11-20 13:04 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu, Sebastian Reichel,
	linux-usb, linux-kernel

Hi Alexey,

On Tue, Nov 17, 2026 at 12:25:31PM +0400, Alexey Charkov wrote:
> Reading INTERRUPT, INTERRUPTA or INTERRUPTB clears its flags in the chip,
> so bailing out on an I2C error in a later read loses the flags already
> read. On Flipper One, a NACK on INTERRUPTB just as a source raised VBUS
> lost VBUSOK. The driver kept treating VBUS as off, so TCPM never attached
> as a sink and left every Source_Capabilities unanswered.
> 
> Keep the flags pending until they are handled, and retry the reads a few
> times, as such errors pass. Otherwise a flag left pending waits for the
> next interrupt.

I did not quite understand this. What's the i2c error? Shouldn't the
i2c adapter driver take care of the retries?

> Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
> Signed-off-by: Alexey Charkov <alchark@flipper.net>

No stable?

Thanks,

> ---
>  drivers/usb/typec/tcpm/fusb302.c | 62 +++++++++++++++++++++++++++++++++-------
>  1 file changed, 51 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
> index 83bd287dadc7..9fb76fdd3b7b 100644
> --- a/drivers/usb/typec/tcpm/fusb302.c
> +++ b/drivers/usb/typec/tcpm/fusb302.c
> @@ -100,6 +100,10 @@ struct fusb302_chip {
>  	bool intr_togdone;
>  	bool intr_bc_lvl;
>  	bool intr_comp_chng;
> +	/* interrupt flags read from the chip, but not handled yet */
> +	u8 pending_interrupt;
> +	u8 pending_interrupta;
> +	u8 pending_interruptb;
>  
>  	/* port status */
>  	bool vconn_on;
> @@ -1499,6 +1503,43 @@ static irqreturn_t fusb302_irq_intn(int irq, void *dev_id)
>  	return IRQ_HANDLED;
>  }
>  
> +/*
> + * Reading an interrupt register clears its flags in the chip, so keep them
> + * pending until handled: a bus error on a later read would otherwise lose
> + * them. Such errors pass, so retry a few times.
> + */
> +static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
> +{
> +	int ret, tries;
> +	u8 flags;
> +
> +	for (tries = 0; tries < 3; tries++) {
> +		if (tries)
> +			usleep_range(1000, 2000);
> +
> +		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &flags);
> +		if (ret < 0)
> +			continue;
> +		chip->pending_interrupt |= flags;
> +
> +		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &flags);
> +		if (ret < 0)
> +			continue;
> +		chip->pending_interrupta |= flags;
> +
> +		ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &flags);
> +		if (ret < 0)
> +			continue;
> +		chip->pending_interruptb |= flags;
> +
> +		ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, status0);
> +		if (ret >= 0)
> +			return 0;
> +	}
> +
> +	return ret;
> +}
> +
>  static void fusb302_irq_work(struct work_struct *work)
>  {
>  	struct fusb302_chip *chip = container_of(work, struct fusb302_chip,
> @@ -1521,18 +1562,17 @@ static void fusb302_irq_work(struct work_struct *work)
>  	intr_bc_lvl = chip->intr_bc_lvl;
>  	intr_comp_chng = chip->intr_comp_chng;
>  
> -	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPT, &interrupt);
> -	if (ret < 0)
> -		goto done;
> -	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTA, &interrupta);
> -	if (ret < 0)
> -		goto done;
> -	ret = fusb302_i2c_read(chip, FUSB_REG_INTERRUPTB, &interruptb);
> -	if (ret < 0)
> -		goto done;
> -	ret = fusb302_i2c_read(chip, FUSB_REG_STATUS0, &status0);
> -	if (ret < 0)
> +	ret = fusb302_read_interrupts(chip, &status0);
> +	if (ret < 0) {
> +		fusb302_log(chip, "interrupts left pending, ret=%d", ret);
>  		goto done;
> +	}
> +	interrupt = chip->pending_interrupt;
> +	interrupta = chip->pending_interrupta;
> +	interruptb = chip->pending_interruptb;
> +	chip->pending_interrupt = 0;
> +	chip->pending_interrupta = 0;
> +	chip->pending_interruptb = 0;
>  	fusb302_log(chip,
>  		    "IRQ: 0x%02x, a: 0x%02x, b: 0x%02x, status0: 0x%02x",
>  		    interrupt, interrupta, interruptb, status0);
> 
> ---
> base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
> change-id: 20261116-fusb302-irq-read-error-8a3ca5ef9ebc
> 
> Best regards,
> --  
> Alexey Charkov <alchark@flipper.net>

-- 
heikki

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read
  2026-11-20 13:04 ` Heikki Krogerus
@ 2026-11-20 13:52   ` Alexey Charkov
  2026-11-20 15:13     ` Heikki Krogerus
  0 siblings, 1 reply; 4+ messages in thread
From: Alexey Charkov @ 2026-11-20 13:52 UTC (permalink / raw)
  To: Heikki Krogerus
  Cc: Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu, Sebastian Reichel,
	linux-usb, linux-kernel

Hi Heikki,

On Fri, Nov 20, 2026 at 5:04 PM Heikki Krogerus
<heikki.krogerus@linux.intel.com> wrote:
>
> Hi Alexey,
>
> On Tue, Nov 17, 2026 at 12:25:31PM +0400, Alexey Charkov wrote:
> > Reading INTERRUPT, INTERRUPTA or INTERRUPTB clears its flags in the chip,
> > so bailing out on an I2C error in a later read loses the flags already
> > read. On Flipper One, a NACK on INTERRUPTB just as a source raised VBUS
> > lost VBUSOK. The driver kept treating VBUS as off, so TCPM never attached
> > as a sink and left every Source_Capabilities unanswered.
> >
> > Keep the flags pending until they are handled, and retry the reads a few
> > times, as such errors pass. Otherwise a flag left pending waits for the
> > next interrupt.
>
> I did not quite understand this. What's the i2c error? Shouldn't the
> i2c adapter driver take care of the retries?

Any I2C transactions can fail - that's why the accessors return status
codes. And they do, e.g. if another device on a shared bus misbehaves
and hogs the bus down, or if there's any intermittent interference,
etc. The I2C controller won't rerun the failed transaction and will
propagate the error code to the caller instead. In my case it was
-ENXIO when another device on the bus interfered with reading the
interrupt status registers midway, causing the irq_work function to
exit early and drop the flags it has already read without acting on
them. The result was a permanently desynchronized TCPM state machine
vs. what the partner saw on the wire.

> > Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
> > Signed-off-by: Alexey Charkov <alchark@flipper.net>
>
> No stable?

Cc: stable would make sense; my oversight. Thanks for spotting that.
Shall I resend with it added?

Best regards,
Alexey

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read
  2026-11-20 13:52   ` Alexey Charkov
@ 2026-11-20 15:13     ` Heikki Krogerus
  0 siblings, 0 replies; 4+ messages in thread
From: Heikki Krogerus @ 2026-11-20 15:13 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Greg Kroah-Hartman, Guenter Roeck, Yueyao Zhu, Sebastian Reichel,
	linux-usb, linux-kernel

On Fri, Nov 20, 2026 at 05:52:45PM +0400, Alexey Charkov wrote:
> Hi Heikki,
> 
> On Fri, Nov 20, 2026 at 5:04 PM Heikki Krogerus
> <heikki.krogerus@linux.intel.com> wrote:
> >
> > Hi Alexey,
> >
> > On Tue, Nov 17, 2026 at 12:25:31PM +0400, Alexey Charkov wrote:
> > > Reading INTERRUPT, INTERRUPTA or INTERRUPTB clears its flags in the chip,
> > > so bailing out on an I2C error in a later read loses the flags already
> > > read. On Flipper One, a NACK on INTERRUPTB just as a source raised VBUS
> > > lost VBUSOK. The driver kept treating VBUS as off, so TCPM never attached
> > > as a sink and left every Source_Capabilities unanswered.
> > >
> > > Keep the flags pending until they are handled, and retry the reads a few
> > > times, as such errors pass. Otherwise a flag left pending waits for the
> > > next interrupt.
> >
> > I did not quite understand this. What's the i2c error? Shouldn't the
> > i2c adapter driver take care of the retries?
> 
> Any I2C transactions can fail - that's why the accessors return status
> codes. And they do, e.g. if another device on a shared bus misbehaves
> and hogs the bus down, or if there's any intermittent interference,
> etc. The I2C controller won't rerun the failed transaction and will
> propagate the error code to the caller instead. In my case it was
> -ENXIO when another device on the bus interfered with reading the
> interrupt status registers midway, causing the irq_work function to
> exit early and drop the flags it has already read without acting on
> them. The result was a permanently desynchronized TCPM state machine
> vs. what the partner saw on the wire.

Okay, got it.

> > > Fixes: c034a43e72dd ("staging: typec: Fairchild FUSB302 Type-c chip driver")
> > > Signed-off-by: Alexey Charkov <alchark@flipper.net>
> >
> > No stable?
> 
> Cc: stable would make sense; my oversight. Thanks for spotting that.
> Shall I resend with it added?

Yes, please send v2 with it added.

Thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 15:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  8:25 [PATCH] usb: typec: fusb302: Keep interrupt flags across a failed register read Alexey Charkov
2026-11-20 13:04 ` Heikki Krogerus
2026-11-20 13:52   ` Alexey Charkov
2026-11-20 15:13     ` Heikki Krogerus

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®