[PATCH v2 4/4] usb: gadget: udc: Decide on connecting in one place
From: Alexey Charkov
Date: Fri Nov 13 2026 - 07:00:39 EST
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>
---
drivers/usb/gadget/udc/core.c | 194 +++++++++++++++++++++---------------------
include/linux/usb/gadget.h | 4 +-
2 files changed, 99 insertions(+), 99 deletions(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 84dc0820c2cb..e592705de55e 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -38,14 +38,18 @@ static const struct bus_type gadget_bus_type;
* @vbus: for udcs who care about vbus status, this value is real vbus status;
* for udcs who do not care about vbus status, this value is always true
* @started: the UDC's started state. True if the UDC had started.
- * @allow_connect: Indicates whether UDC is allowed to be pulled up.
- * Set/cleared by gadget_(un)bind_driver() after gadget driver is bound or
- * unbound.
+ * @bound: True while a gadget driver is bound and the UDC is started for it.
+ * @soft_disconnected: True if userspace has disconnected the gadget through
+ * the soft_connect attribute. Forgotten when the gadget driver unbinds.
+ * @allow_connect: True if the gadget may connect to the host: a gadget driver
+ * is bound, VBUS is present, the driver hasn't deactivated the gadget, and
+ * userspace hasn't disconnected it. Only usb_udc_update_connection_locked()
+ * sets it, and it connects or disconnects the gadget to match.
* @vbus_work: work routine to handle VBUS status change notifications.
- * @connect_lock: protects udc->started, gadget->connect,
- * gadget->allow_connect and gadget->deactivate. The routines
+ * @connect_lock: protects udc->started, udc->bound, udc->soft_disconnected,
+ * udc->allow_connect, gadget->connected and gadget->deactivated. The routines
* usb_gadget_connect_locked(), usb_gadget_disconnect_locked(),
- * usb_udc_connect_control_locked(), usb_gadget_udc_start_locked() and
+ * usb_udc_update_connection_locked(), usb_gadget_udc_start_locked() and
* usb_gadget_udc_stop_locked() are called with this lock held.
*
* This represents the internal data structure which is used by the UDC-class
@@ -59,6 +63,8 @@ struct usb_udc {
struct list_head list;
bool vbus;
bool started;
+ bool bound;
+ bool soft_disconnected;
bool allow_connect;
struct work_struct vbus_work;
struct mutex connect_lock;
@@ -715,20 +721,13 @@ static int usb_gadget_connect_locked(struct usb_gadget *gadget)
goto out;
}
- if (gadget->connected)
+ /*
+ * If the gadget may not connect yet, usb_udc_update_connection_locked()
+ * will connect it once it may.
+ */
+ if (gadget->connected || !gadget->udc->allow_connect)
goto out;
- if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) {
- /*
- * If the gadget isn't usable (because it is deactivated,
- * unbound, or not yet started), we only save the new state.
- * The gadget will be connected automatically when it is
- * activated/bound/started.
- */
- gadget->connected = true;
- goto out;
- }
-
ret = gadget->ops->pullup(gadget, 1);
if (!ret)
gadget->connected = 1;
@@ -747,6 +746,10 @@ static int usb_gadget_connect_locked(struct usb_gadget *gadget)
* enumerating this gadget when the pullup is active and a VBUS session
* is active (the link is powered).
*
+ * Does nothing while the gadget may not connect: no gadget driver is bound,
+ * VBUS is absent, the gadget is deactivated or userspace has disconnected it.
+ * The UDC core connects the gadget by itself once all of that changes.
+ *
* Returns zero on success, else negative errno.
*/
int usb_gadget_connect(struct usb_gadget *gadget)
@@ -774,15 +777,6 @@ static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
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;
- goto out;
- }
-
ret = gadget->ops->pullup(gadget, 0);
if (!ret)
gadget->connected = 0;
@@ -809,6 +803,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.
*
+ * The gadget stays disconnected until usb_gadget_connect() is called or the
+ * UDC core connects it again on the next change of VBUS, binding, activation
+ * or soft_connect.
+ *
* Returns zero on success, else negative errno.
*/
int usb_gadget_disconnect(struct usb_gadget *gadget)
@@ -823,6 +821,32 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
}
EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
+static bool usb_udc_may_connect(struct usb_udc *udc)
+ __must_hold(&udc->connect_lock)
+{
+ return udc->bound && udc->vbus && !udc->gadget->deactivated &&
+ !udc->soft_disconnected;
+}
+
+/*
+ * Decide whether the gadget may connect and connect or disconnect it to match.
+ * Call this after changing any of the state that usb_udc_may_connect() checks.
+ */
+static int usb_udc_update_connection_locked(struct usb_udc *udc)
+ __must_hold(&udc->connect_lock)
+{
+ udc->allow_connect = usb_udc_may_connect(udc);
+
+ /* Without a pullup control there is nothing to switch */
+ if (!udc->gadget->ops->pullup)
+ return 0;
+
+ if (udc->allow_connect)
+ return usb_gadget_connect_locked(udc->gadget);
+
+ return usb_gadget_disconnect_locked(udc->gadget);
+}
+
/**
* usb_gadget_deactivate - deactivate function which is not ready to work
* @gadget: the peripheral being deactivated
@@ -845,18 +869,13 @@ int usb_gadget_deactivate(struct usb_gadget *gadget)
if (gadget->deactivated)
goto unlock;
- if (gadget->connected) {
- ret = usb_gadget_disconnect_locked(gadget);
- if (ret)
- goto unlock;
-
- /*
- * If gadget was being connected before deactivation, we want
- * to reconnect it in usb_gadget_activate().
- */
- gadget->connected = true;
- }
gadget->deactivated = true;
+ ret = usb_udc_update_connection_locked(gadget->udc);
+ if (ret) {
+ /* Still connected, so stay active */
+ gadget->deactivated = false;
+ gadget->udc->allow_connect = usb_udc_may_connect(gadget->udc);
+ }
unlock:
mutex_unlock(&gadget->udc->connect_lock);
@@ -871,7 +890,7 @@ EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
* @gadget: the peripheral being activated
*
* This routine activates gadget which was previously deactivated with
- * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
+ * usb_gadget_deactivate() call, and connects it if it may connect.
*
* This routine may sleep; it must not be called in interrupt context.
*
@@ -886,15 +905,7 @@ int usb_gadget_activate(struct usb_gadget *gadget)
goto unlock;
gadget->deactivated = false;
-
- /*
- * If gadget has been connected before deactivation, or became connected
- * while it was being deactivated, we call usb_gadget_connect().
- */
- if (gadget->connected) {
- gadget->connected = false;
- ret = usb_gadget_connect_locked(gadget);
- }
+ ret = usb_udc_update_connection_locked(gadget->udc);
unlock:
mutex_unlock(&gadget->udc->connect_lock);
@@ -1147,21 +1158,12 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
/* ------------------------------------------------------------------------- */
-/* Acquire connect_lock before calling this function. */
-static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock)
-{
- if (udc->vbus)
- return usb_gadget_connect_locked(udc->gadget);
- else
- return usb_gadget_disconnect_locked(udc->gadget);
-}
-
static void vbus_event_work(struct work_struct *work)
{
struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work);
mutex_lock(&udc->connect_lock);
- usb_udc_connect_control_locked(udc);
+ usb_udc_update_connection_locked(udc);
mutex_unlock(&udc->connect_lock);
}
@@ -1175,12 +1177,12 @@ static void vbus_event_work(struct work_struct *work)
* according to vbus status.
*
* This function can be invoked from interrupt context by irq handlers of
- * the gadget drivers, however, usb_udc_connect_control() has to run in
- * non-atomic context due to the following:
+ * the gadget drivers, however, usb_udc_update_connection_locked() has to run
+ * in non-atomic context due to the following:
* a. Some of the gadget driver implementations expect the ->pullup
* callback to be invoked in non-atomic context.
* b. usb_gadget_disconnect() acquires udc_lock which is a mutex.
- * Hence offload invocation of usb_udc_connect_control() to workqueue.
+ * Hence offload invocation of usb_udc_update_connection_locked() to workqueue.
*/
void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
{
@@ -1236,16 +1238,8 @@ static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
}
ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver);
- if (!ret) {
+ if (!ret)
udc->started = true;
- /*
- * The pullup is never enabled while the UDC is stopped, so any
- * connection recorded meanwhile was only a request. Forget it,
- * so that the next usb_gadget_connect_locked() enables the
- * pullup rather than returning early.
- */
- udc->gadget->connected = false;
- }
return ret;
}
@@ -1652,6 +1646,7 @@ static void usb_udc_forget_driver_locked(struct usb_udc *udc)
__must_hold(&udc->connect_lock)
{
udc->gadget->deactivated = false;
+ udc->soft_disconnected = false;
}
static int gadget_bind_driver(struct device *dev)
@@ -1686,8 +1681,8 @@ static int gadget_bind_driver(struct device *dev)
goto err_start;
}
usb_gadget_enable_async_callbacks(udc);
- udc->allow_connect = true;
- ret = usb_udc_connect_control_locked(udc);
+ udc->bound = true;
+ ret = usb_udc_update_connection_locked(udc);
if (ret)
goto err_connect_control;
@@ -1697,6 +1692,7 @@ static int gadget_bind_driver(struct device *dev)
return 0;
err_connect_control:
+ udc->bound = false;
udc->allow_connect = false;
usb_gadget_disable_async_callbacks(udc);
if (gadget->irq)
@@ -1733,15 +1729,17 @@ static void gadget_unbind_driver(struct device *dev)
dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
- udc->allow_connect = false;
- cancel_work_sync(&udc->vbus_work);
mutex_lock(&udc->connect_lock);
- usb_gadget_disconnect_locked(gadget);
+ udc->bound = false;
+ usb_udc_update_connection_locked(udc);
usb_gadget_disable_async_callbacks(udc);
if (gadget->irq)
synchronize_irq(gadget->irq);
mutex_unlock(&udc->connect_lock);
+ /* A VBUS event can no longer connect the gadget, so just wait it out */
+ cancel_work_sync(&udc->vbus_work);
+
udc->driver->unbind(gadget);
mutex_lock(&udc->connect_lock);
@@ -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;
+ }
+
+ return n;
}
static DEVICE_ATTR_WO(soft_connect);
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 8285b19a25e0..53ace954d189 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -412,8 +412,8 @@ struct usb_gadget_ops {
* u_ether.c to improve performance.
* @is_selfpowered: if the gadget is self-powered.
* @deactivated: True if gadget is deactivated - in deactivated state it cannot
- * be connected.
- * @connected: True if gadget is connected.
+ * be connected. Cleared when the gadget driver unbinds.
+ * @connected: True if the gadget's pullup is enabled.
* @lpm_capable: If the gadget max_speed is FULL or HIGH, this flag
* indicates that it supports LPM as per the LPM ECN & errata.
* @wakeup_capable: True if gadget is capable of sending remote wakeup.
--
2.56.0