Re: [PATCH v2 4/4] usb: gadget: udc: Decide on connecting in one place

From: Alan Stern

Date: Sun Nov 15 2026 - 21:59:07 EST


On Fri, Nov 13, 2026 at 01:28:19PM +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 the soft_connect
> attribute, and each path applies its own rules. gadget->connected
> records both whether the pullup is enabled and whether to enable it once
> that becomes possible, so the paths misread each other's state: a VBUS
> event before bind kept the pullup off until the previous patch cleared
> the flag on UDC start. soft_connect also starts and stops the UDC behind
> the gadget driver's back, and its "connect" overrides both VBUS and
> deactivation.
>
> Settle the rules in one place. The gadget may connect only while a
> gadget driver is bound, VBUS is present, the driver hasn't deactivated
> the gadget and userspace hasn't disconnected it through soft_connect.
> Have usb_udc_update_connection_locked() record that in
> udc->allow_connect under connect_lock and connect or disconnect the
> gadget to match, and call it wherever one of those conditions changes.
> gadget->connected then only tracks the pullup, as the UDC drivers that
> read it already assume, so the clearing on UDC start can go.
>
> Reduce soft_connect to setting or clearing a disconnect request under
> connect_lock, rather than the gadget's device lock, and forget the
> request at unbind like the deactivation. If the pullup can't follow,
> keep the old request and report the error, so that the request and the
> pullup still agree. Leave starting and stopping the UDC to bind and
> unbind alone: the attribute is documented to control the pullup, not the
> controller.
>
> Let the helper succeed on a UDC without a pullup control, as there is
> nothing to switch there. Activation and deactivation keep working on
> such UDCs, and binding them works again: it has most likely failed with
> -EOPNOTSUPP since commit 0ea39e030a80 ("usb: gadget: udc: Handle
> gadget_connect failure during bind operation"), but I don't have any
> affected hardware to confirm it.
>
> Suggested-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
> ---

Very nice. Aside from what Sashiko said, I have a few small comments.

First, have usb_gadget_{dis}connect_locked() check whether the current
state is already the right one before it checks whether
gadget->ops->pullup is set. No point returning an error when we're not
asking the driver to change anything.

> @@ -1826,35 +1824,37 @@ static ssize_t soft_connect_store(struct device *dev,
> struct device_attribute *attr, const char *buf, size_t n)
> {
> struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
> - ssize_t ret;
> -
> - device_lock(&udc->gadget->dev);
> - if (!udc->driver) {
> - dev_err(dev, "soft-connect without a gadget driver\n");
> - ret = -EOPNOTSUPP;
> - goto out;
> - }
> + bool disconnect, was_disconnected;
> + int ret;
>
> if (sysfs_streq(buf, "connect")) {
> - mutex_lock(&udc->connect_lock);
> - usb_gadget_udc_start_locked(udc);
> - usb_gadget_connect_locked(udc->gadget);
> - mutex_unlock(&udc->connect_lock);
> + disconnect = false;
> } else if (sysfs_streq(buf, "disconnect")) {
> - mutex_lock(&udc->connect_lock);
> - usb_gadget_disconnect_locked(udc->gadget);
> - usb_gadget_udc_stop_locked(udc);
> - mutex_unlock(&udc->connect_lock);
> + disconnect = true;
> } else {
> dev_err(dev, "unsupported command '%s'\n", buf);
> - ret = -EINVAL;
> - goto out;
> + return -EINVAL;
> }
>
> - ret = n;
> -out:
> - device_unlock(&udc->gadget->dev);
> - return ret;
> + guard(mutex)(&udc->connect_lock);
> +
> + if (!udc->bound) {
> + dev_err(dev, "soft-connect without a gadget driver\n");
> + return -EOPNOTSUPP;
> + }
> +
> + /* "connect" only lifts the disconnect, it doesn't override VBUS etc. */
> + was_disconnected = udc->soft_disconnected;
> + udc->soft_disconnected = disconnect;
> + ret = usb_udc_update_connection_locked(udc);
> + if (ret) {
> + /* The pullup didn't change, so neither does the request */
> + udc->soft_disconnected = was_disconnected;
> + udc->allow_connect = usb_udc_may_connect(udc);
> + return ret;
> + }

This last bit is questionable. Even if the pullup doesn't change, we
can say that the write succeeded simply because it did change the value
of udc->soft_disconnected. You can argue for or against this; it's not
clear what behavior is the most desirable.

It might be worthwhile adding a warning to usb_gadget_connect_locked():
If the pullup can't be turned on for any reason other than -EOPNOTSUPP,
the user ought to be informed. The same goes for disconnecting.

One more thing: Every time you call usb_udc_may_connect(), the value it
returns is immediately assigned to udc->allow_connect. You may as well
put that assignment in the function and rename it to something like
usb_udc_update_allow_connect().

Alan Stern