[PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume
From: Fabrice Gasnier
Date: Wed Nov 25 2026 - 11:33:11 EST
After a power domain loss, dwc2_resume() restores the registers that were
backed up when entering a low power mode (clock gating, partial power
down), or in dwc2_suspend() when no backup exists yet. The restore only
brings the hardware back to the state it had when the backup was taken.
The software state is still the one of the suspended controller, and events
that occurred while the power domain was off have been missed:
- lx_state is DWC2_L2 and bus_suspended is set, so dwc2_hsotg_resume()
returns early and _dwc2_hcd_resume() tries to resume the port, even
though the controller has been fully restored.
- in_ppd, hibernated and the backup valid flags may be left over.
- In device mode, the gadget speed and the enabled endpoints are stale,
although the host most likely saw a disconnect while the platform was
unpowered.
- In host mode, a connect or disconnect that happened during suspend is not
reported, as GINTSTS is cleared by the restore.
Add dwc2_clear_lost_state() to reset the common software state, and use it
after a register loss is detected (GUSBCFG.TOUTCAL cleared):
- device mode: dwc2_hsotg_resume() gets a "lost" parameter. When set, it
disconnects the gadget, disables the non-EP0 endpoints, sets the speed to
unknown and clears the stale state, then reinitializes the core and
reconnects as for a regular resume.
- host mode: dwc2_hcd_lost_state() clears the stale state and flags a
connect and suspend status change, so the hub re-evaluates the port.
Global interrupts are disabled by the restore under the lock, and only
re-enabled once this cleanup is done, so an interrupt cannot run on stale
state in between.
Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@xxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/all/sashiko-outbox-202003@xxxxxxxxxx/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@xxxxxxxxxxx>
---
Changes in v6:
- patch split from "usb: dwc2: fix recovery after PM domain off" to ease
the review
---
drivers/usb/dwc2/core.c | 17 +++++++++++++++++
drivers/usb/dwc2/core.h | 8 ++++++--
drivers/usb/dwc2/gadget.c | 24 +++++++++++++++++++-----
drivers/usb/dwc2/hcd.c | 20 ++++++++++++++++++++
drivers/usb/dwc2/platform.c | 18 ++++++++++++++++--
5 files changed, 78 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/dwc2/core.c b/drivers/usb/dwc2/core.c
index 6a0135e06b00..5da76f0fbcf6 100644
--- a/drivers/usb/dwc2/core.c
+++ b/drivers/usb/dwc2/core.c
@@ -600,6 +600,23 @@ void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
}
}
+/**
+ * dwc2_clear_lost_state() - Clear software state made stale by register loss
+ * @hsotg: Programming view of DWC_otg controller
+ *
+ * Must be called with the spinlock held.
+ */
+void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg)
+{
+ hsotg->hibernated = 0;
+ hsotg->in_ppd = 0;
+ hsotg->bus_suspended = false;
+ hsotg->gr_backup.valid = false;
+ hsotg->dr_backup.valid = false;
+ hsotg->hr_backup.valid = false;
+ hsotg->lx_state = DWC2_L0;
+}
+
/*
* dwc2_enable_acg - enable active clock gating feature
*/
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 54c040da0cae..9873ae7b52c2 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -1411,12 +1411,14 @@ void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
+void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg);
+
/* Gadget defines */
#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
-int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2);
+int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost);
int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
bool reset);
@@ -1452,7 +1454,7 @@ static inline int dwc2_hsotg_remove(struct dwc2_hsotg *dwc2)
{ return 0; }
static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
{ return 0; }
-static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2)
+static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost)
{ return 0; }
static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
{ return 0; }
@@ -1503,6 +1505,7 @@ static inline void dwc2_clear_fifo_map(struct dwc2_hsotg *hsotg) {}
#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us);
+void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg);
void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
@@ -1530,6 +1533,7 @@ static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
static inline int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg,
int us)
{ return 0; }
+static inline void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 5e75d6975baa..295091a87b12 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -5213,26 +5213,40 @@ int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
return 0;
}
-int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
+int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
{
unsigned long flags;
+ int ep;
- if (hsotg->lx_state == DWC2_L2)
+ if (!lost && hsotg->lx_state == DWC2_L2)
return 0;
- if (hsotg->driver) {
+ if (hsotg->driver)
dev_info(hsotg->dev, "resuming usb gadget %s\n",
hsotg->driver->driver.name);
- spin_lock_irqsave(&hsotg->lock, flags);
+ spin_lock_irqsave(&hsotg->lock, flags);
+ if (lost) {
+ /* Host likely saw a disconnect: drop stale gadget state */
+ dwc2_hsotg_disconnect(hsotg);
+ for (ep = 1; ep < hsotg->num_of_eps; ep++) {
+ if (hsotg->eps_in[ep])
+ dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
+ if (hsotg->eps_out[ep])
+ dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
+ }
+ hsotg->gadget.speed = USB_SPEED_UNKNOWN;
+ dwc2_clear_lost_state(hsotg);
+ }
+ if (hsotg->driver) {
dwc2_hsotg_core_init_disconnected(hsotg, false);
if (hsotg->enabled) {
/* Enable ACG feature in device mode,if supported */
dwc2_enable_acg(hsotg);
dwc2_hsotg_core_connect(hsotg);
}
- spin_unlock_irqrestore(&hsotg->lock, flags);
}
+ spin_unlock_irqrestore(&hsotg->lock, flags);
return 0;
}
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 0d20b9de857a..6f2b192f68f8 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -2324,6 +2324,26 @@ static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
dwc2_core_host_init(hsotg);
}
+/**
+ * dwc2_hcd_lost_state - drop stale host state after registers were restored
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * Make the hub re-evaluate the port, as a connect change may have been lost
+ * while the controller was powered off.
+ */
+void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_clear_lost_state(hsotg);
+ hsotg->flags.b.port_connect_status_change = 1;
+ hsotg->flags.b.port_suspend_change = 1;
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+}
+
static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
struct dwc2_host_chan *chan,
struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 7f3436e6a153..53c34a42f760 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -738,6 +738,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
unsigned long flags;
+ bool lost;
int ret = 0;
if (!dwc2->ll_hw_enabled)
@@ -788,16 +789,29 @@ static int __maybe_unused dwc2_resume(struct device *dev)
* In case the GUSBCFG register is not initialized, it's clear the
* registers must be restored.
*/
- if (!(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK)) {
+ lost = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);
+ if (lost) {
spin_lock_irqsave(&dwc2->lock, flags);
ret = dwc2_restore_critical_registers(dwc2);
+ /*
+ * The restore re-enables global interrupts: keep them off
+ * until the stale state is cleaned up below.
+ */
+ if (!ret)
+ dwc2_disable_global_interrupts(dwc2);
spin_unlock_irqrestore(&dwc2->lock, flags);
if (ret)
return ret;
}
if (dwc2_is_device_mode(dwc2))
- ret = dwc2_hsotg_resume(dwc2);
+ ret = dwc2_hsotg_resume(dwc2, lost);
+ else if (lost)
+ dwc2_hcd_lost_state(dwc2);
+
+ /* Global interrupts are off after a restore, enable them back */
+ if (lost)
+ dwc2_enable_global_interrupts(dwc2);
return ret;
}
--
2.43.0