[PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable

From: Fabrice Gasnier

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


In fixed peripheral mode, the gadget code manages the low-level hardware
on its own: it is disabled after probe, and enabled again when a gadget
driver binds. While it is disabled, dwc2_suspend() and dwc2_resume() return
early since ll_hw_enabled is false. If the DWC2 registers are lost during
system suspend, nothing restores the mode selection when the gadget driver
binds later:

- FORCEDEVMODE is not set, so the core may not come up in device mode.
- With a usb-role-switch, the session overrides set by dwc2_drd_resume()
are not restored either.

Restore them at the end of __dwc2_lowlevel_hw_enable() when dr_mode is
peripheral: call dwc2_drd_resume() if a role switch is registered, or
dwc2_force_dr_mode() otherwise. The dr_mode check leaves the probe-time
enable unchanged, as dr_mode is parsed later and the role switch isn't
registered yet.

As this is now done on low-level enable, skip the peripheral case in
dwc2_resume(). Other modes still restore the forced mode or the role switch
session there.

Fixes: 2b94b054ac49 ("usb: dwc2: fix hang during suspend if set as peripheral")
Cc: stable@xxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/linux-usb/sashiko-outbox-219235@xxxxxxxxxx/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@xxxxxxxxxxx>
---
Changes in v6:
- Also restore the role switch session overrides with dwc2_drd_resume()
when a usb-role-switch is registered in peripheral mode. Issue raised
by Sashiko.

Changes in v5:
- New separate patch added since issue raised by Sashiko on next patch
pre-exists, e.g. missing forced mode restore in peripheral-only mode.
---
drivers/usb/dwc2/platform.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 24ede0b6ab81..38954dee6ad0 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -129,6 +129,18 @@ static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
if (ret)
goto err_dis_clk;

+ if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
+ /*
+ * Gadget code manages low-level hardware on its own. Restore
+ * the mode and role switch session after register loss while
+ * it was disabled.
+ */
+ if (hsotg->role_sw)
+ dwc2_drd_resume(hsotg);
+ else
+ dwc2_force_dr_mode(hsotg);
+ }
+
return 0;

err_dis_clk:
@@ -778,11 +790,12 @@ static int __maybe_unused dwc2_resume(struct device *dev)
spin_unlock_irqrestore(&dwc2->lock, flags);
}

- if (!dwc2->role_sw) {
- /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
- dwc2_force_dr_mode(dwc2);
- } else {
- dwc2_drd_resume(dwc2);
+ if (dwc2->dr_mode != USB_DR_MODE_PERIPHERAL) {
+ if (!dwc2->role_sw)
+ /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
+ dwc2_force_dr_mode(dwc2);
+ else
+ dwc2_drd_resume(dwc2);
}

/*

--
2.43.0