* [PATCH] usb: gadget: udc: Fix permanent block caused by 'deactivated' flag
@ 2025-11-14 8:08 Jimmy Hu
2025-11-14 15:33 ` Alan Stern
0 siblings, 1 reply; 2+ messages in thread
From: Jimmy Hu @ 2025-11-14 8:08 UTC (permalink / raw)
To: Greg Kroah-Hartman, linux-usb
Cc: Jimmy Hu, Kuen-Han Tsai, Alan Stern, Thinh Nguyen, Roy Luo,
Robert Baldyga, Felipe Balbi, linux-kernel, badhri
When setting a new USB configuration, some functions (e.g., UVC) can
call usb_function_deactivate() to set the `gadget->deactivated` flag
to `true`, intentionally blocking enumeration until a userspace
daemon (e.g., UVC service) is ready.
This `deactivated=true` state becomes a permanent block if this flag
is not cleared by usb_function_activate() (e.g., UVC service failure
or config set while unplugged). This blocked state persists even if
the cable is plugged/unplugged, as the vbus_event_work() handler is
still blocked by the `gadget->deactivated` flag, preventing pullup()
from being called.
This patch fixes this by modifying vbus_event_work() to clear the
`gadget->deactivated` flag *before* usb_udc_connect_control_locked().
This breaks the permanent block, so pullup() can now be called.
Fixes: ccdf138fe3e2 ("usb: gadget: add usb_gadget_activate/deactivate functions")
Signed-off-by: Jimmy Hu <hhhuuu@google.com>
---
drivers/usb/gadget/udc/core.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 8dbe79bdc0f9..0195540d511a 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1151,8 +1151,17 @@ static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc-
static void vbus_event_work(struct work_struct *work)
{
struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work);
+ struct usb_gadget *gadget = udc->gadget;
mutex_lock(&udc->connect_lock);
+
+ /*
+ * Clear the 'deactivated' flag on a VBUS event
+ * to break the blocked state.
+ */
+ if (gadget->deactivated)
+ gadget->deactivated = false;
+
usb_udc_connect_control_locked(udc);
mutex_unlock(&udc->connect_lock);
}
--
2.52.0.rc1.455.g30608eb744-goog
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] usb: gadget: udc: Fix permanent block caused by 'deactivated' flag
2025-11-14 8:08 [PATCH] usb: gadget: udc: Fix permanent block caused by 'deactivated' flag Jimmy Hu
@ 2025-11-14 15:33 ` Alan Stern
0 siblings, 0 replies; 2+ messages in thread
From: Alan Stern @ 2025-11-14 15:33 UTC (permalink / raw)
To: Jimmy Hu
Cc: Greg Kroah-Hartman, linux-usb, Kuen-Han Tsai, Thinh Nguyen,
Roy Luo, Robert Baldyga, Felipe Balbi, linux-kernel, badhri
On Fri, Nov 14, 2025 at 04:08:41PM +0800, Jimmy Hu wrote:
> When setting a new USB configuration, some functions (e.g., UVC) can
> call usb_function_deactivate() to set the `gadget->deactivated` flag
> to `true`, intentionally blocking enumeration until a userspace
> daemon (e.g., UVC service) is ready.
>
> This `deactivated=true` state becomes a permanent block if this flag
> is not cleared by usb_function_activate() (e.g., UVC service failure
> or config set while unplugged). This blocked state persists even if
> the cable is plugged/unplugged, as the vbus_event_work() handler is
> still blocked by the `gadget->deactivated` flag, preventing pullup()
> from being called.
>
> This patch fixes this by modifying vbus_event_work() to clear the
> `gadget->deactivated` flag *before* usb_udc_connect_control_locked().
> This breaks the permanent block, so pullup() can now be called.
I do not believe this is the right approach. It will fail in situations
where the cable is plugged in and then unplugged before the function
driver is ready -- the deactivated flag will be cleared while the driver
still is not ready.
The proper fix is to make sure that the situation never arises. In
other words, if the ucv driver calls usb_function_deactivate() then it
must guarantee that it will call usb_function_activate() at an
appropriate time later.
Another approach would be for the composite driver to keep track of
which function drivers have called usb_function_deactivate(), and have
it automatically clear the deactivated flag once those drivers are all
unbound. But this wouldn't be necessary if the function drivers all
behaved properly.
Alan Stern
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-11-14 15:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-14 8:08 [PATCH] usb: gadget: udc: Fix permanent block caused by 'deactivated' flag Jimmy Hu
2025-11-14 15:33 ` Alan Stern
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®