[PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds

From: Alexey Charkov

Date: Tue Nov 17 2026 - 07:48:30 EST


A gadget driver deactivates the gadget through usb_gadget_deactivate()
until its functions are ready, so the deactivation means nothing to the
next driver. Yet the UDC core keeps gadget->deactivated set across
unbind. Only configfs resets it, since commit bf95c4d4630c ("usb: gadget:
configfs: clear deactivation flag in configfs_composite_unbind()"), and
it does so from its own unbind callback without connect_lock, which
guards the flag everywhere else. A legacy gadget driver that unbinds
while deactivated leaves the next driver unable to connect.

Clear the flag in the UDC core under connect_lock once the driver has
unbound, including when the bind fails after the driver has deactivated
the gadget, and drop the unlocked write from configfs.

Suggested-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
---
drivers/usb/gadget/configfs.c | 1 -
drivers/usb/gadget/udc/core.c | 7 +++++++
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 51df6d1d1487..c0f9e6625b64 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1849,7 +1849,6 @@ static void configfs_composite_unbind(struct usb_gadget *gadget)
spin_lock_irqsave(&gi->spinlock, flags);
cdev->gadget = NULL;
cdev->deactivations = 0;
- gadget->deactivated = false;
set_gadget_data(gadget, NULL);
spin_unlock_irqrestore(&gi->spinlock, flags);
}
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 577bc15f61a1..449b303c2f81 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1705,6 +1705,11 @@ static int gadget_bind_driver(struct device *dev)
dev_err(&udc->dev, "failed to start %s: %d\n",
driver->function, ret);

+ /* The driver may have deactivated the gadget before failing */
+ mutex_lock(&udc->connect_lock);
+ gadget->deactivated = false;
+ mutex_unlock(&udc->connect_lock);
+
mutex_lock(&udc_lock);
udc->driver = NULL;
driver->is_bound = false;
@@ -1734,6 +1739,8 @@ static void gadget_unbind_driver(struct device *dev)

mutex_lock(&udc->connect_lock);
usb_gadget_udc_stop_locked(udc);
+ /* The next gadget driver decides on its own deactivation */
+ gadget->deactivated = false;
mutex_unlock(&udc->connect_lock);

mutex_lock(&udc_lock);

--
2.56.0