[PATCH 05/19] usb: cdns3: switch the role unlocked on resume
From: Greg Kroah-Hartman
Date: Tue Oct 27 2026 - 11:04:45 EST
From: Josef Bacik <josef@xxxxxxxxxxxxxx>
cdns3_controller_resume() calls cdns_resume() with cdns->lock held and
interrupts off. If the controller has lost its power during suspend and
the role which the ID and VBUS inputs ask for is not the current one any
more, cdns_resume() calls cdns_hw_role_switch(). That stops one role
and starts another, which takes a mutex, allocates memory with
GFP_KERNEL, requests interrupts and registers or unregisters the xHCI
platform device or the UDC:
BUG: sleeping function called from invalid context
To get there, change the cable of a dual role port without a role switch
device while the system is asleep, on a SoC which powers the controller
down.
Move the switch to a function of its own, cdns_resume_role(), which is
called before the lock is taken. What remains of cdns_resume() is still
called with the lock held, and skipped if the role has been switched or
the switch has failed, as before.
cdns_hw_role_switch() also calls pm_runtime_get_sync() for the
controller. That cannot work from its own runtime resume callback, where
it would wait for itself, so the role is only switched on system resume
now. After a runtime resume the old role is resumed, like after a
suspend without loss of power.
Fixes: 2cf2581cd229 ("usb: cdns3: add power lost support for system resume")
Cc: stable <stable@xxxxxxxxxx>
Cc: Peter Chen <peter.chen@xxxxxxxxxx>
Cc: Pawel Laszczak <pawell@xxxxxxxxxxx>
Cc: Roger Quadros <rogerq@xxxxxxxxxx>
Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
drivers/usb/cdns3/cdns3-plat.c | 8 ++++--
drivers/usb/cdns3/core.c | 55 ++++++++++++++++++++++++++----------------
drivers/usb/cdns3/core.h | 3 +++
3 files changed, 43 insertions(+), 23 deletions(-)
diff --git a/drivers/usb/cdns3/cdns3-plat.c b/drivers/usb/cdns3/cdns3-plat.c
index e3f32c3e9535..674625cdd4d4 100644
--- a/drivers/usb/cdns3/cdns3-plat.c
+++ b/drivers/usb/cdns3/cdns3-plat.c
@@ -268,7 +268,7 @@ static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
{
struct cdns *cdns = dev_get_drvdata(dev);
- int ret;
+ int ret, role_changed;
unsigned long flags;
if (!cdns->in_lpm)
@@ -292,8 +292,12 @@ static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
cdns3_set_platform_suspend(cdns->dev, false, false);
+ /* It resumes the device, which cannot be done from runtime resume */
+ role_changed = PMSG_IS_AUTO(msg) ? 0 : cdns_resume_role(cdns);
+
spin_lock_irqsave(&cdns->lock, flags);
- cdns_resume(cdns);
+ if (!role_changed)
+ cdns_resume(cdns);
cdns->in_lpm = false;
spin_unlock_irqrestore(&cdns->lock, flags);
cdns_set_active(cdns, !PMSG_IS_AUTO(msg));
diff --git a/drivers/usb/cdns3/core.c b/drivers/usb/cdns3/core.c
index 87122fb5bdb8..3b2b8f231a92 100644
--- a/drivers/usb/cdns3/core.c
+++ b/drivers/usb/cdns3/core.c
@@ -504,36 +504,49 @@ int cdns_suspend(struct cdns *cdns)
}
EXPORT_SYMBOL_GPL(cdns_suspend);
+/**
+ * cdns_resume_role - switch the role if it has changed while the power was off
+ * @cdns: controller
+ *
+ * Stopping and starting a role sleeps, so this has to be called before
+ * cdns->lock is taken for cdns_resume().
+ *
+ * Returns 1 if the role has been switched: it has been started from scratch
+ * and must not be resumed. 0 if there was nothing to do, negative errno if the
+ * switch has failed.
+ */
+int cdns_resume_role(struct cdns *cdns)
+{
+ int ret;
+
+ if (cdns->role_sw || !cdns_power_is_lost(cdns))
+ return 0;
+
+ if (cdns_hw_role_state_machine(cdns) == cdns->role)
+ return 0;
+
+ ret = cdns_hw_role_switch(cdns);
+
+ return ret ? ret : 1;
+}
+EXPORT_SYMBOL_GPL(cdns_resume_role);
+
int cdns_resume(struct cdns *cdns)
{
bool power_lost = cdns_power_is_lost(cdns);
- enum usb_role real_role;
- bool role_changed = false;
int ret = 0;
if (power_lost) {
- if (!cdns->role_sw) {
- real_role = cdns_hw_role_state_machine(cdns);
- if (real_role != cdns->role) {
- ret = cdns_hw_role_switch(cdns);
- if (ret)
- return ret;
- role_changed = true;
- }
- }
+ if (cdns->role == USB_ROLE_HOST)
+ ret = cdns_drd_host_on(cdns);
+ else if (cdns->role == USB_ROLE_DEVICE)
+ ret = cdns_drd_gadget_on(cdns);
- if (!role_changed) {
- if (cdns->role == USB_ROLE_HOST)
- ret = cdns_drd_host_on(cdns);
- else if (cdns->role == USB_ROLE_DEVICE)
- ret = cdns_drd_gadget_on(cdns);
-
- if (ret)
- return ret;
- }
+ if (ret)
+ return ret;
}
- if (!role_changed && cdns->roles[cdns->role]->resume)
+ if (cdns->roles[cdns->role]->resume)
cdns->roles[cdns->role]->resume(cdns, power_lost);
return 0;
diff --git a/drivers/usb/cdns3/core.h b/drivers/usb/cdns3/core.h
index 8c492fda924c..cfc3be9ddf9e 100644
--- a/drivers/usb/cdns3/core.h
+++ b/drivers/usb/cdns3/core.h
@@ -136,10 +136,13 @@ int cdns_remove(struct cdns *cdns);
int cdns_core_init_role(struct cdns *cdns);
#ifdef CONFIG_PM_SLEEP
+int cdns_resume_role(struct cdns *cdns);
int cdns_resume(struct cdns *cdns);
int cdns_suspend(struct cdns *cdns);
void cdns_set_active(struct cdns *cdns, u8 set_active);
#else /* CONFIG_PM_SLEEP */
+static inline int cdns_resume_role(struct cdns *cdns)
+{ return 0; }
static inline int cdns_resume(struct cdns *cdns)
{ return 0; }
static inline void cdns_set_active(struct cdns *cdns, u8 set_active) { }
--
2.56.0