Re: [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place
From: Alan Stern
Date: Wed Nov 18 2026 - 13:22:41 EST
On Tue, Nov 17, 2026 at 04:48:09PM +0400, Alexey Charkov wrote:
> The UDC core connects or disconnects the gadget on bind and unbind, on
> VBUS events, in usb_gadget_{de,}activate() and through soft_connect,
> each by its own rules. gadget->connected means both "the pullup is on"
> and "turn it on once possible", so the paths misread each other's state.
> soft_connect also starts and stops the UDC behind the gadget driver's
> back, and its "connect" overrides VBUS and deactivation.
>
> Allow the gadget to connect only while a driver is bound, VBUS is
> present, the driver hasn't deactivated it and userspace hasn't
> disconnected it, and re-evaluate that in one helper whenever any of
> these changes. Let gadget->connected track only the pullup, as UDC
> drivers already assume, and clear it at unbind even if the pullup
> refuses to switch off, so that the next driver starts clean. That
> supersedes the clearing on UDC start from the previous patch.
>
> Reduce soft_connect to a disconnect request that is forgotten at unbind
> like the deactivation, and leave the controller alone: the attribute is
> documented to control the pullup. Keep the request if the pullup fails
> to follow, so that the next update retries it, and warn about such
> failures, as nothing else tells the user.
>
> Treat a UDC without a pullup control as having nothing to switch.
> Binding one has most likely failed with -EOPNOTSUPP since
> commit 0ea39e030a80 ("usb: gadget: udc: Handle gadget_connect failure
> during bind operation"), but I have no affected hardware to confirm it.
>
> Suggested-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
> ---
Reviewed-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> @@ -766,26 +773,20 @@ static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
> {
> int ret = 0;
>
> - if (!gadget->ops->pullup) {
> - ret = -EOPNOTSUPP;
> - goto out;
> - }
> -
> if (!gadget->connected)
> goto out;
>
> - if (gadget->deactivated || !gadget->udc->started) {
> - /*
> - * If gadget is deactivated we only save new state.
> - * Gadget will stay disconnected after activation.
> - */
> - gadget->connected = false;
> + if (!gadget->ops->pullup) {
> + ret = -EOPNOTSUPP;
> goto out;
> }
>
> ret = gadget->ops->pullup(gadget, 0);
> if (!ret)
> gadget->connected = 0;
> + else if (ret != -EOPNOTSUPP)
> + dev_warn(&gadget->udc->dev, "failed to disable the pullup: %d\n",
> + ret);
>
> mutex_lock(&udc_lock);
> if (gadget->udc->driver)
> @@ -809,6 +810,10 @@ static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
> * Following a successful disconnect, invoke the ->disconnect() callback
> * for the current gadget driver so that UDC drivers don't need to.
I wonder if we need to look at this. The current code and the code in
this patch both invoke the ->disconnect() callback following
unsuccessful disconnects as well as successful ones. This seems like a
topic for another patch.
Alan Stern