* [PATCH v2] usb: gadget: udc: skip pullup() if already connected
@ 2026-04-23 9:53 Xu Yang
2026-04-23 14:00 ` Alan Stern
0 siblings, 1 reply; 2+ messages in thread
From: Xu Yang @ 2026-04-23 9:53 UTC (permalink / raw)
To: gregkh, khtsai, kexinsun, hhhuuu, kees
Cc: linux-usb, linux-kernel, imx, jun.li
The device controller may update vbus status via usb_udc_vbus_handler(),
which tries to connect the gadget even though gadget_bind_driver() has
already called usb_udc_connect_control_locked(). This causes pullup() to
be called twice. Avoid this by checking if gadget->connected is true.
This also set gadget->connected as false in usb_gadget_activate() if it
became connected while it was being deactivated. Otherwise,
usb_gadget_connect_locked will return early and pullup() won't be called.
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
Changes in v2:
- reset gadget->connected as false before calling usb_gadget_connect_locked()
---
drivers/usb/gadget/udc/core.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index e8861eaad907..60340ff9edbf 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -712,6 +712,9 @@ static int usb_gadget_connect_locked(struct usb_gadget *gadget)
goto out;
}
+ if (gadget->connected)
+ goto out;
+
if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) {
/*
* If the gadget isn't usable (because it is deactivated,
@@ -885,8 +888,10 @@ int usb_gadget_activate(struct usb_gadget *gadget)
* If gadget has been connected before deactivation, or became connected
* while it was being deactivated, we call usb_gadget_connect().
*/
- if (gadget->connected)
+ if (gadget->connected) {
+ gadget->connected = false;
ret = usb_gadget_connect_locked(gadget);
+ }
unlock:
mutex_unlock(&gadget->udc->connect_lock);
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] usb: gadget: udc: skip pullup() if already connected
2026-04-23 9:53 [PATCH v2] usb: gadget: udc: skip pullup() if already connected Xu Yang
@ 2026-04-23 14:00 ` Alan Stern
0 siblings, 0 replies; 2+ messages in thread
From: Alan Stern @ 2026-04-23 14:00 UTC (permalink / raw)
To: Xu Yang
Cc: gregkh, khtsai, kexinsun, hhhuuu, kees, linux-usb, linux-kernel,
imx, jun.li
On Thu, Apr 23, 2026 at 05:53:55PM +0800, Xu Yang wrote:
> The device controller may update vbus status via usb_udc_vbus_handler(),
> which tries to connect the gadget even though gadget_bind_driver() has
> already called usb_udc_connect_control_locked(). This causes pullup() to
> be called twice. Avoid this by checking if gadget->connected is true.
>
> This also set gadget->connected as false in usb_gadget_activate() if it
> became connected while it was being deactivated. Otherwise,
> usb_gadget_connect_locked will return early and pullup() won't be called.
>
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
Reviewed-by: Alan Stern <stern@rowland.harvard.edu>
> ---
> Changes in v2:
> - reset gadget->connected as false before calling usb_gadget_connect_locked()
> ---
> drivers/usb/gadget/udc/core.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index e8861eaad907..60340ff9edbf 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
> @@ -712,6 +712,9 @@ static int usb_gadget_connect_locked(struct usb_gadget *gadget)
> goto out;
> }
>
> + if (gadget->connected)
> + goto out;
> +
> if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) {
> /*
> * If the gadget isn't usable (because it is deactivated,
> @@ -885,8 +888,10 @@ int usb_gadget_activate(struct usb_gadget *gadget)
> * If gadget has been connected before deactivation, or became connected
> * while it was being deactivated, we call usb_gadget_connect().
> */
> - if (gadget->connected)
> + if (gadget->connected) {
> + gadget->connected = false;
> ret = usb_gadget_connect_locked(gadget);
> + }
>
> unlock:
> mutex_unlock(&gadget->udc->connect_lock);
> --
> 2.34.1
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-04-23 14:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-23 9:53 [PATCH v2] usb: gadget: udc: skip pullup() if already connected Xu Yang
2026-04-23 14:00 ` Alan Stern
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox