[PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend

From: Fabrice Gasnier

Date: Wed Nov 25 2026 - 11:33:33 EST


If the power domain is off during system suspend and the OTG ID pin
changes meanwhile, dwc2_resume() sees a register loss but the role that
was active at suspend is never cleaned up. No connector ID interrupt
reports the change, as GINTSTS is cleared by the register restore, and
the register backup belongs to the old role: host registers could be
restored in device mode, or the other way around.

When the core was in device mode at suspend and resumes in host mode, the
gadget driver is never told about the disconnect and the gadget state
stays stale. When it was in host mode and resumes in device mode, URBs and
host flags stay as they were and the external VBUS supply is left enabled.

Record the mode in dwc2_suspend(). On register loss with dr_mode otg,
compare it with the mode after resume. If it differs, skip the register
restore and clean up the old role before initializing the new one:

- device to host: disconnect the gadget, clear stale state, the FIFO map
and set the speed to unknown, without accessing endpoint registers that
are unusable in host mode. Then reinitialize the host with the new
dwc2_hcd_restore(). If it fails, the HCD was not reinitialized, so it is
left inaccessible.
- host to device: fail pending URBs, clear stale state, make the HCD
accessible and release VBUS, then reinitialize the core and resume the
gadget.

This addresses the second finding of the Sashiko review of "usb: dwc2:
fix recovery after PM domain off".

Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@xxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/linux-usb/sashiko-outbox-219256@xxxxxxxxxx/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@xxxxxxxxxxx>
---
Changes in v6:
- New patch added since Sashiko finding
---
drivers/usb/dwc2/core.h | 9 +++++++
drivers/usb/dwc2/gadget.c | 20 ++++++++++++++++
drivers/usb/dwc2/hcd.c | 58 +++++++++++++++++++++++++++++++++++++++++++++
drivers/usb/dwc2/platform.c | 25 ++++++++++++++++---
4 files changed, 109 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 9873ae7b52c2..89a34956bf22 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -876,6 +876,7 @@ struct dwc2_hregs_backup {
* @reset_phy_on_wake: Quirk saying that we should assert PHY reset on a
* remote wakeup.
* @phy_off_for_suspend: Status of whether we turned the PHY off at suspend.
+ * @suspended_in_device_mode: True if the core was in device mode at suspend.
* @need_phy_for_wake: Quirk saying that we should keep the PHY on at
* suspend if we need USB to wake us up.
* @frame_number: Frame number read from the core. For both device
@@ -1074,6 +1075,7 @@ struct dwc2_hsotg {
unsigned int reset_phy_on_wake:1;
unsigned int need_phy_for_wake:1;
unsigned int phy_off_for_suspend:1;
+ unsigned int suspended_in_device_mode:1;
u16 frame_number;

struct phy *phy;
@@ -1419,6 +1421,7 @@ void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost);
+void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg);
int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
bool reset);
@@ -1456,6 +1459,7 @@ static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
{ return 0; }
static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost)
{ return 0; }
+static inline void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg) {}
static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
{ return 0; }
static inline void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
@@ -1505,7 +1509,9 @@ 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);
+int dwc2_hcd_restore(struct dwc2_hsotg *hsotg);
void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg);
+void dwc2_hcd_role_lost(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);
@@ -1533,7 +1539,10 @@ 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 int dwc2_hcd_restore(struct dwc2_hsotg *hsotg)
+{ return 0; }
static inline void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg) {}
+static inline void dwc2_hcd_role_lost(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 295091a87b12..9b12c336e4aa 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -5251,6 +5251,26 @@ int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
return 0;
}

+/**
+ * dwc2_hsotg_role_lost - drop gadget state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * The core came back from register loss in host mode while the gadget was
+ * active at suspend. Disconnect the gadget and clear stale software state
+ * without touching the endpoint registers, which are not usable in host mode.
+ */
+void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hsotg_disconnect(hsotg);
+ dwc2_clear_fifo_map(hsotg);
+ hsotg->gadget.speed = USB_SPEED_UNKNOWN;
+ dwc2_clear_lost_state(hsotg);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+}
+
/**
* dwc2_backup_device_registers() - Backup controller device registers.
* When suspending usb bus, registers needs to be backuped
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 6f2b192f68f8..60b39ccd0cad 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -2324,6 +2324,42 @@ static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
dwc2_core_host_init(hsotg);
}

+/**
+ * dwc2_hcd_restore - reinitialize host state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int dwc2_hcd_restore(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+ int ret;
+
+ clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ dwc2_disable_global_interrupts(hsotg);
+
+ ret = dwc2_vbus_supply_init(hsotg);
+ if (ret)
+ return ret;
+
+ ret = dwc2_core_init(hsotg, true);
+ if (ret) {
+ dwc2_vbus_supply_exit(hsotg);
+ return ret;
+ }
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hcd_reinit(hsotg);
+ 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);
+
+ return 0;
+}
+
/**
* dwc2_hcd_lost_state - drop stale host state after registers were restored
* @hsotg: Programming view of the DWC_otg controller
@@ -2344,6 +2380,28 @@ void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg)
spin_unlock_irqrestore(&hsotg->lock, flags);
}

+/**
+ * dwc2_hcd_role_lost - drop host state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * The core came back from register loss in device mode while the host was
+ * active at suspend. Fail pending URBs, release VBUS and clear stale software
+ * state so the host can be started again on the next role switch.
+ */
+void dwc2_hcd_role_lost(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hcd_disconnect(hsotg, true);
+ dwc2_clear_lost_state(hsotg);
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+
+ dwc2_vbus_supply_exit(hsotg);
+}
+
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 53c34a42f760..d89a30308f86 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -658,6 +658,7 @@ static int __maybe_unused dwc2_suspend(struct device *dev)
return 0;

is_device_mode = dwc2_is_device_mode(dwc2);
+ dwc2->suspended_in_device_mode = is_device_mode;
if (is_device_mode)
dwc2_hsotg_suspend(dwc2);

@@ -738,7 +739,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
unsigned long flags;
- bool lost;
+ bool lost, role_changed;
int ret = 0;

if (!dwc2->ll_hw_enabled)
@@ -790,7 +791,25 @@ static int __maybe_unused dwc2_resume(struct device *dev)
* registers must be restored.
*/
lost = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);
- if (lost) {
+ role_changed = lost && dwc2->dr_mode == USB_DR_MODE_OTG &&
+ dwc2->suspended_in_device_mode != dwc2_is_device_mode(dwc2);
+ if (role_changed) {
+ /*
+ * The ID pin changed while the registers were lost: no
+ * connector ID interrupt reports it and the register backup
+ * belongs to the old role. Clean up the old role and
+ * initialize the new one instead of restoring.
+ */
+ if (dwc2->suspended_in_device_mode) {
+ dwc2_hsotg_role_lost(dwc2);
+ ret = dwc2_hcd_restore(dwc2);
+ } else {
+ dwc2_hcd_role_lost(dwc2);
+ ret = dwc2_core_init(dwc2, true);
+ }
+ if (ret)
+ return ret;
+ } else if (lost) {
spin_lock_irqsave(&dwc2->lock, flags);
ret = dwc2_restore_critical_registers(dwc2);
/*
@@ -806,7 +825,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)

if (dwc2_is_device_mode(dwc2))
ret = dwc2_hsotg_resume(dwc2, lost);
- else if (lost)
+ else if (lost && !role_changed)
dwc2_hcd_lost_state(dwc2);

/* Global interrupts are off after a restore, enable them back */

--
2.43.0