Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fabrice Gasnier" <fabrice.gasnier@foss.st.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v5 1/7] usb: dwc2: fix recovery after PM domain off
Date: Tue, 17 Nov 2026 17:42:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-219256@kernel.org> (raw)
In-Reply-To: <20261117-dwc2-low-power-fixes-stm32-v5-1-0f197e0e6362@foss.st.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] usb: dwc2: GUSBCFG_TOUTCAL canary unset or cleared before
  dwc2_resume()
- [Medium] usb: dwc2: stale role state in dwc2_resume() if OTG mode
  changes in suspend
--

commit f376168d518b0027023927337bdb68e136a8890b
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>

usb: dwc2: fix recovery after PM domain off

This commit replaces critical register backup and restore during system
suspend/resume with a GUSBCFG_TOUTCAL check on resume that reinitializes
the controller and cleans up host or gadget state when registers were lost.

> diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
> index ef0d730770347..7dec3842ba2c6 100644
> --- a/drivers/usb/dwc2/platform.c
> +++ b/drivers/usb/dwc2/platform.c

[ ... ]

> @@ -786,8 +749,30 @@ static int __maybe_unused dwc2_resume(struct device *dev)
>  		dwc2_drd_resume(dwc2);
>  	}
>  
> +	/*
> +	 * During suspend it's possible that the power domain for the
> +	 * DWC2 controller is disabled and all register values get lost.
> +	 * In case the GUSBCFG register is not initialized, it's clear the
> +	 * registers must be restored. So invoke here the necessary init
> +	 * as done by dwc2_hcd_init() during probe.
> +	 */
> +	need_restore = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);

[Severity: Medium]
Can GUSBCFG_TOUTCAL still be zero here even when the power domain stayed on,
causing a false-positive need_restore and an unnecessary controller reset on
every resume?

GUSBCFG_TOUTCAL is only programmed in dwc2_core_host_init() and
dwc2_hsotg_core_init_disconnected(), not in dwc2_core_init() or
dwc2_gusbcfg_init().  In dual-role device mode with no gadget driver bound
(hsotg->driver is NULL), neither _dwc2_hcd_start() nor dwc2_hsotg_resume()
calls those functions, so GUSBCFG_TOUTCAL remains 0 after probe and after
resume.

In addition, dwc2_hsotg_core_init_disconnected() writes GUSBCFG_TOUTCAL(7)
before calling dwc2_phy_init(hsotg, true):

drivers/usb/dwc2/gadget.c:dwc2_hsotg_core_init_disconnected() {
    ...
	usbcfg = dwc2_readl(hsotg, GUSBCFG);
	usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
	usbcfg |= GUSBCFG_TOUTCAL(7);
    ...
	dwc2_writel(hsotg, usbcfg, GUSBCFG);

	dwc2_phy_init(hsotg, true);
    ...
}

In peripheral-only mode (dr_mode == USB_DR_MODE_PERIPHERAL), dwc2_core_init()
has not run beforehand; if dwc2_phy_init() changes the PHY selection bits in
GUSBCFG from their power-on defaults, it issues dwc2_core_reset(), which
clears GUSBCFG_TOUTCAL back to 0.

> +	if (need_restore) {
> +		if (dwc2->dr_mode != USB_DR_MODE_PERIPHERAL) {
> +			if (dwc2_is_device_mode(dwc2)) {
> +				ret = dwc2_core_init(dwc2, true);
> +				if (ret)
> +					return ret;
> +			} else {
> +				ret = dwc2_hcd_restore(dwc2);
> +				if (ret)
> +					return ret;
> +			}
> +		}
> +	}
> +
>  	if (dwc2_is_device_mode(dwc2))
> -		ret = dwc2_hsotg_resume(dwc2);
> +		ret = dwc2_hsotg_resume(dwc2, need_restore);

[Severity: Medium]
What happens if the OTG ID pin changes while the system is suspended and the
power domain is powered off?

dwc2_resume() branches only on the post-resume dwc2_is_device_mode(dwc2), and
dwc2_core_init() clears GINTSTS (including GINTSTS_CONIDSTSCHNG) in
dwc2_enable_common_interrupts() and overwrites hsotg->op_state with the new
mode.  If the controller was in device mode with hsotg->lx_state == DWC2_L2
before suspend (so dwc2_hsotg_suspend() returned early) and resumes in host
mode, dwc2_hsotg_resume() is skipped: the gadget is never disconnected,
non-EP0 endpoints stay enabled in hsotg->fifo_map, and later calls to
dwc2_hsotg_ep_disable() fail with -EINVAL because hsotg->op_state is now
OTG_STATE_A_HOST.  Conversely, if the controller was in host mode before
suspend and resumes in device mode, both dwc2_hcd_restore() and
_dwc2_hcd_resume() are skipped, leaving HCD_FLAG_HW_ACCESSIBLE cleared and
host state unrecovered.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-dwc2-low-power-fixes-stm32-v5-0-0f197e0e6362@foss.st.com?part=1

  reply	other threads:[~2026-11-17 17:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 17:06 [PATCH v5 0/7] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
2026-11-17 17:06 ` [PATCH v5 1/7] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
2026-11-17 17:42   ` sashiko-bot [this message]
2026-11-17 17:06 ` [PATCH v5 2/7] usb: dwc2: fix a possible interrupt storm in hcd resume Fabrice Gasnier
2026-11-17 17:14   ` sashiko-bot
2026-11-17 17:06 ` [PATCH v5 3/7] usb: dwc2: fix a possible interrupt storm on stm32mp1 Fabrice Gasnier
2026-11-17 17:15   ` sashiko-bot
2026-11-17 17:06 ` [PATCH v5 4/7] usb: dwc2: hcd: fix possible successive vbus supply enables Fabrice Gasnier
2026-11-17 17:15   ` sashiko-bot
2026-11-17 17:06 ` [PATCH v5 5/7] usb: dwc2: hcd: don't disable external vbus in system suspend Fabrice Gasnier
2026-11-17 17:15   ` sashiko-bot
2026-11-17 17:06 ` [PATCH v5 6/7] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
2026-11-17 17:31   ` sashiko-bot
2026-11-17 17:06 ` [PATCH v5 7/7] usb: dwc2: platform: fix stm32mp15 detection logic power management Fabrice Gasnier
2026-11-17 17:32   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-219256@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fabrice.gasnier@foss.st.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox