* [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects
@ 2026-11-17 12:48 Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 12:48 UTC (permalink / raw)
To: Neill Kapron, Greg Kroah-Hartman, Xu Yang, Alan Stern
Cc: linux-usb, linux-kernel, Sebastian Reichel, Alexey Charkov,
stable
The UDC core decides whether to enable the gadget's pullup in several
places: bind and unbind, VBUS events, usb_gadget_{de,}activate() and the
soft_connect attribute. Each applies its own rules, and
gadget->connected records both the pullup state and a request to connect
later. Since commit 62911bc82b03 ("usb: gadget: udc: skip pullup() if
already connected"), a VBUS event before bind leaves the gadget
invisible to the host until it is rebound. That happens on every boot
of an RK3576 board whose Type-C port controller sets the device role
before userspace binds a configfs gadget.
Patch 1 is the v1 fix, small enough for stable. Patches 2-3 implement
Alan's scheme: the gadget connects only while a driver is bound, VBUS is
present, the driver hasn't deactivated it and userspace hasn't
disconnected it through soft_connect. One helper records that in
udc->allow_connect under connect_lock and switches the pullup to match.
The UDC starts and stops only at bind and unbind, and the deactivation
and soft-disconnect requests are forgotten at unbind.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Changes in v3:
- Patch 1: collect Alan's Acked-by.
- Squash v2 patch 3 (configfs) into patch 2 and clear the deactivation
inline rather than through a helper (Alan).
- Patch 3: clear gadget->connected at unbind even if the pullup refuses
to switch off (Sashiko).
- Patch 3: check the pullup state before the ->pullup operation, so that
a call which changes nothing succeeds (Alan).
- Patch 3: warn when the pullup fails to switch, unless the UDC reports
-EOPNOTSUPP (Alan).
- Patch 3: set udc->allow_connect inside the renamed
usb_udc_update_allow_connect() (Alan).
- Patch 3: keep a soft_connect request even if the pullup fails to follow
it, so the next update retries it; the warning reports the failure.
- Link to v2: https://patch.msgid.link/20261113-udc-connect-v2-0-f51b11f07eb8@flipper.net
Changes in v2:
- Add patches 2-4, reworking the connection control as Alan suggested.
- Link to v1: https://patch.msgid.link/20261112-udc-connect-v1-1-03c73259642b@flipper.net
---
Alexey Charkov (3):
usb: gadget: udc: Clear a pending connection when starting the UDC
usb: gadget: udc: Forget a deactivation when the gadget driver unbinds
usb: gadget: udc: Decide on connecting in one place
drivers/usb/gadget/configfs.c | 1 -
drivers/usb/gadget/udc/core.c | 214 ++++++++++++++++++++++++------------------
include/linux/usb/gadget.h | 4 +-
3 files changed, 123 insertions(+), 96 deletions(-)
---
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
change-id: 20261112-udc-connect-61e0f11cf93b
Best regards,
--
Alexey Charkov <alchark@flipper.net>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC
2026-11-17 12:48 [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects Alexey Charkov
@ 2026-11-17 12:48 ` Alexey Charkov
2026-11-17 13:03 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place Alexey Charkov
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 12:48 UTC (permalink / raw)
To: Neill Kapron, Greg Kroah-Hartman, Xu Yang, Alan Stern
Cc: linux-usb, linux-kernel, Sebastian Reichel, Alexey Charkov,
stable
Since commit 62911bc82b03 ("usb: gadget: udc: skip pullup() if already
connected"), usb_gadget_connect_locked() returns early when
gadget->connected is set. A connection requested while the UDC is
stopped sets that flag too, without enabling the pullup. A later
gadget_bind_driver() or soft_connect "connect" then finds it set and
never enables the pullup, so the host doesn't see the gadget until it is
unbound and bound again.
This happens whenever a UDC driver reports VBUS through
usb_udc_vbus_handler() while no gadget driver is bound, e.g. when a
Type-C port controller switches the controller to the device role at
boot, seconds before userspace binds a configfs gadget.
The pullup is never enabled while the UDC is stopped, so clear
gadget->connected once it starts. The bind connects according to the
VBUS state that the core tracks anyway, and a function that deactivates
the gadget during bind still has the connection recorded for
usb_gadget_activate().
Fixes: 62911bc82b03 ("usb: gadget: udc: skip pullup() if already connected")
Cc: stable@vger.kernel.org
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/gadget/udc/core.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index d5844384c368..577bc15f61a1 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1236,8 +1236,16 @@ 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;
}
--
2.56.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds
2026-11-17 12:48 [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
@ 2026-11-17 12:48 ` Alexey Charkov
2026-11-17 13:06 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place Alexey Charkov
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 12:48 UTC (permalink / raw)
To: Neill Kapron, Greg Kroah-Hartman, Xu Yang, Alan Stern
Cc: linux-usb, linux-kernel, Sebastian Reichel, Alexey Charkov
A gadget driver deactivates the gadget through usb_gadget_deactivate()
until its functions are ready, so the deactivation means nothing to the
next driver. Yet the UDC core keeps gadget->deactivated set across
unbind. Only configfs resets it, since commit bf95c4d4630c ("usb: gadget:
configfs: clear deactivation flag in configfs_composite_unbind()"), and
it does so from its own unbind callback without connect_lock, which
guards the flag everywhere else. A legacy gadget driver that unbinds
while deactivated leaves the next driver unable to connect.
Clear the flag in the UDC core under connect_lock once the driver has
unbound, including when the bind fails after the driver has deactivated
the gadget, and drop the unlocked write from configfs.
Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/gadget/configfs.c | 1 -
drivers/usb/gadget/udc/core.c | 7 +++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 51df6d1d1487..c0f9e6625b64 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -1849,7 +1849,6 @@ static void configfs_composite_unbind(struct usb_gadget *gadget)
spin_lock_irqsave(&gi->spinlock, flags);
cdev->gadget = NULL;
cdev->deactivations = 0;
- gadget->deactivated = false;
set_gadget_data(gadget, NULL);
spin_unlock_irqrestore(&gi->spinlock, flags);
}
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 577bc15f61a1..449b303c2f81 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1705,6 +1705,11 @@ static int gadget_bind_driver(struct device *dev)
dev_err(&udc->dev, "failed to start %s: %d\n",
driver->function, ret);
+ /* The driver may have deactivated the gadget before failing */
+ mutex_lock(&udc->connect_lock);
+ gadget->deactivated = false;
+ mutex_unlock(&udc->connect_lock);
+
mutex_lock(&udc_lock);
udc->driver = NULL;
driver->is_bound = false;
@@ -1734,6 +1739,8 @@ static void gadget_unbind_driver(struct device *dev)
mutex_lock(&udc->connect_lock);
usb_gadget_udc_stop_locked(udc);
+ /* The next gadget driver decides on its own deactivation */
+ gadget->deactivated = false;
mutex_unlock(&udc->connect_lock);
mutex_lock(&udc_lock);
--
2.56.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place
2026-11-17 12:48 [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds Alexey Charkov
@ 2026-11-17 12:48 ` Alexey Charkov
2026-11-17 13:19 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 12:48 UTC (permalink / raw)
To: Neill Kapron, Greg Kroah-Hartman, Xu Yang, Alan Stern
Cc: linux-usb, linux-kernel, Sebastian Reichel, Alexey Charkov
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@rowland.harvard.edu>
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/gadget/udc/core.c | 217 ++++++++++++++++++++++--------------------
include/linux/usb/gadget.h | 4 +-
2 files changed, 117 insertions(+), 104 deletions(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 449b303c2f81..496083045a99 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -38,14 +38,19 @@ 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_allow_connect() sets
+ * it, and usb_udc_update_connection_locked() 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 +64,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;
@@ -710,28 +717,24 @@ static int usb_gadget_connect_locked(struct usb_gadget *gadget)
{
int ret = 0;
- if (!gadget->ops->pullup) {
- ret = -EOPNOTSUPP;
- 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;
+ if (!gadget->ops->pullup) {
+ ret = -EOPNOTSUPP;
goto out;
}
ret = gadget->ops->pullup(gadget, 1);
if (!ret)
gadget->connected = 1;
+ else if (ret != -EOPNOTSUPP)
+ dev_warn(&gadget->udc->dev, "failed to enable the pullup: %d\n",
+ ret);
out:
trace_usb_gadget_connect(gadget, ret);
@@ -747,6 +750,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)
@@ -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.
*
+ * 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 +828,34 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
}
EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
+static void usb_udc_update_allow_connect(struct usb_udc *udc)
+ __must_hold(&udc->connect_lock)
+{
+ udc->allow_connect = 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_update_allow_connect() checks.
+ */
+static int usb_udc_update_connection_locked(struct usb_udc *udc)
+ __must_hold(&udc->connect_lock)
+{
+ usb_udc_update_allow_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 +878,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;
+ usb_udc_update_allow_connect(gadget->udc);
+ }
unlock:
mutex_unlock(&gadget->udc->connect_lock);
@@ -871,7 +899,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 +914,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 +1167,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 +1186,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 +1247,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;
}
@@ -1679,8 +1682,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;
@@ -1690,7 +1693,8 @@ static int gadget_bind_driver(struct device *dev)
return 0;
err_connect_control:
- udc->allow_connect = false;
+ udc->bound = false;
+ usb_udc_update_allow_connect(udc);
usb_gadget_disable_async_callbacks(udc);
if (gadget->irq)
synchronize_irq(gadget->irq);
@@ -1726,21 +1730,31 @@ 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);
+ /*
+ * The UDC stops below even if the pullup refused to switch off (some
+ * UDCs refuse without VBUS), so treat the gadget as disconnected from
+ * now on. Neither a late VBUS event nor the next driver's bind should
+ * act on a connection that ends with this driver.
+ */
+ gadget->connected = false;
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);
usb_gadget_udc_stop_locked(udc);
/* The next gadget driver decides on its own deactivation */
gadget->deactivated = false;
+ udc->soft_disconnected = false;
mutex_unlock(&udc->connect_lock);
mutex_lock(&udc_lock);
@@ -1820,35 +1834,34 @@ 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;
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.
+ * The request stands even if the pullup fails to follow it, which
+ * usb_gadget_{dis,}connect_locked() warn about, and the next update
+ * retries it.
+ */
+ udc->soft_disconnected = disconnect;
+ usb_udc_update_connection_locked(udc);
+
+ 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
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
@ 2026-11-17 13:03 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 13:03 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
> Since commit 62911bc82b03 ("usb: gadget: udc: skip pullup() if already
> connected"), usb_gadget_connect_locked() returns early when
> gadget->connected is set. A connection requested while the UDC is
> stopped sets that flag too, without enabling the pullup. A later
> gadget_bind_driver() or soft_connect "connect" then finds it set and
> never enables the pullup, so the host doesn't see the gadget until it is
> unbound and bound again.
>
> This happens whenever a UDC driver reports VBUS through
> usb_udc_vbus_handler() while no gadget driver is bound, e.g. when a
> Type-C port controller switches the controller to the device role at
> boot, seconds before userspace binds a configfs gadget.
>
> The pullup is never enabled while the UDC is stopped, so clear
> gadget->connected once it starts. The bind connects according to the
> VBUS state that the core tracks anyway, and a function that deactivates
> the gadget during bind still has the connection recorded for
> usb_gadget_activate().
>
> Fixes: 62911bc82b03 ("usb: gadget: udc: skip pullup() if already connected")
> Cc: stable@vger.kernel.org
> Acked-by: Alan Stern <stern@rowland.harvard.edu>
> Signed-off-by: Alexey Charkov <alchark@flipper.net>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-udc-connect-v3-0-9413b491d213@flipper.net?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds
2026-11-17 12:48 ` [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds Alexey Charkov
@ 2026-11-17 13:06 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 13:06 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: configfs: stale cdev->deactivations after failed
bind
--
commit 08b8abaff4ff275aaa5fed522a9a3de79db20527
Author: Alexey Charkov <alchark@flipper.net>
usb: gadget: udc: Forget a deactivation when the gadget driver unbinds
This commit clears gadget->deactivated under connect_lock in the UDC core
upon driver unbind and bind failure, removing the unlocked reset from
configfs_composite_unbind().
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index 577bc15f61a15..449b303c2f813 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
> @@ -1705,6 +1705,11 @@ static int gadget_bind_driver(struct device *dev)
> dev_err(&udc->dev, "failed to start %s: %d\n",
> driver->function, ret);
>
> + /* The driver may have deactivated the gadget before failing */
> + mutex_lock(&udc->connect_lock);
> + gadget->deactivated = false;
> + mutex_unlock(&udc->connect_lock);
> +
> mutex_lock(&udc_lock);
> udc->driver = NULL;
> driver->is_bound = false;
[Severity: Medium]
Can clearing gadget->deactivated on the err_bind path leave it out of sync
with cdev->deactivations for a configfs gadget?
In configfs_composite_bind() -> usb_add_function(), if a function has
bind_deactivated set (such as f_uvc or f_obex), usb_function_deactivate()
is called before function->bind():
drivers/usb/gadget/composite.c:usb_function_deactivate() {
...
if (cdev->deactivations == 0) {
spin_unlock_irqrestore(&cdev->lock, flags);
status = usb_gadget_deactivate(cdev->gadget);
spin_lock_irqsave(&cdev->lock, flags);
}
if (status == 0)
cdev->deactivations++;
...
}
If function->bind() or a later step in configfs_composite_bind() fails,
driver->bind() returns an error to gadget_bind_driver() and jumps to
err_bind without calling configfs_composite_unbind(), which is the only
place that resets cdev->deactivations = 0. Clearing gadget->deactivated
here leaves gi->cdev.deactivations non-zero on the configfs gadget_info
instance.
When binding the same configfs gadget is retried, usb_function_deactivate()
sees cdev->deactivations != 0 and skips usb_gadget_deactivate(), so the UDC
enables pullup before userspace activates the function, and subsequent
usb_function_activate() calls never decrement cdev->deactivations back to
zero.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-udc-connect-v3-0-9413b491d213@flipper.net?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place
2026-11-17 12:48 ` [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place Alexey Charkov
@ 2026-11-17 13:19 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 13:19 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: gadget: udc: missing driver->disconnect() when
!ops->pullup
- [Medium] usb: gadget: udc: -EOPNOTSUPP from pullup() breaks bind and
activation
- [Medium] usb: gadget: udc: driver->disconnect() called when pullup
disable fails
- [Low] usb: gadget: udc: unreachable !gadget->ops->pullup check in
disconnect
--
commit f20c9b97618b74d0e27972a1d4cf419b12e02088
Author: Alexey Charkov <alchark@flipper.net>
usb: gadget: udc: Decide on connecting in one place
This commit centralizes UDC connection state evaluation in
usb_udc_update_connection_locked() so that pullup control depends on driver
binding, VBUS presence, activation state, and sysfs soft_connect.
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index 449b303c2f813..496083045a993 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
[ ... ]
> @@ -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;
> }
[Severity: Low]
Is this !gadget->ops->pullup check reachable? gadget->connected is only set
in usb_gadget_connect_locked() after checking that gadget->ops->pullup is
non-NULL, so when ops->pullup is NULL, !gadget->connected always jumps to out
first and usb_gadget_disconnect() returns 0 instead of -EOPNOTSUPP.
>
> 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)
> gadget->udc->driver->disconnect(gadget);
> mutex_unlock(&udc_lock);
[Severity: Medium]
Should gadget->udc->driver->disconnect(gadget) still run when
gadget->ops->pullup(gadget, 0) fails?
When disabling the pullup fails, gadget->connected stays 1 and
usb_gadget_deactivate() rolls back gadget->deactivated = false under the
assumption that the gadget remains connected and active. However,
driver->disconnect() has already reset the composite configuration and
disabled the function endpoints while the hardware pullup is still enabled,
and any later retry (such as from soft_connect_store() or vbus_event_work())
will call driver->disconnect() again.
[ ... ]
> @@ -823,6 +828,34 @@ int usb_gadget_disconnect(struct usb_gadget *gadget)
> }
> EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
>
> +static void usb_udc_update_allow_connect(struct usb_udc *udc)
> + __must_hold(&udc->connect_lock)
> +{
> + udc->allow_connect = 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_update_allow_connect() checks.
> + */
> +static int usb_udc_update_connection_locked(struct usb_udc *udc)
> + __must_hold(&udc->connect_lock)
> +{
> + usb_udc_update_allow_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);
> +}
[Severity: Medium]
What happens when a UDC driver defines ops->pullup, but that callback returns
-EOPNOTSUPP?
usb_gadget_connect_locked() and usb_gadget_disconnect_locked() suppress the
warning when ops->pullup() returns -EOPNOTSUPP, but still return -EOPNOTSUPP
to usb_udc_update_connection_locked(). UDC drivers such as pxa27x_udc (when
no pullup GPIO is configured) and fsl_udc_core (when VBUS is inactive, since
it does not use usb_udc_vbus_handler()) return -EOPNOTSUPP from their pullup
callbacks, which causes gadget_bind_driver(), usb_gadget_activate(), and
usb_gadget_deactivate() to fail with -EOPNOTSUPP.
[ ... ]
> @@ -1726,21 +1730,31 @@ 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);
> + /*
> + * The UDC stops below even if the pullup refused to switch off (some
> + * UDCs refuse without VBUS), so treat the gadget as disconnected from
> + * now on. Neither a late VBUS event nor the next driver's bind should
> + * act on a connection that ends with this driver.
> + */
> + gadget->connected = false;
> 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);
[Severity: High]
On a UDC without ops->pullup (such as max3420_udc or fotg210-udc), does
udc->driver->disconnect(gadget) ever get called before udc->driver->unbind()
or on VBUS loss?
With usb_udc_update_connection_locked() returning 0 early when
!udc->gadget->ops->pullup, gadget_bind_driver() now succeeds and starts the
UDC without setting gadget->connected. Once the host enumerates and
configures the gadget, unbinding the driver (or losing VBUS in
vbus_event_work()) skips usb_gadget_disconnect_locked() and never invokes
udc->driver->disconnect(gadget). udc->driver->unbind(gadget) then runs while
cdev->config is still active and function->disable() has never been called,
triggering WARN_ON(cdev->config) in __composite_unbind() or tearing down
active functions in configfs_composite_unbind() with enabled endpoints and
dangling function state (such as use-after-free on dev->port_usb in u_ether).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-udc-connect-v3-0-9413b491d213@flipper.net?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-17 13:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 12:48 [PATCH v3 0/3] usb: gadget: udc: Rework when the gadget connects Alexey Charkov
2026-11-17 12:48 ` [PATCH v3 1/3] usb: gadget: udc: Clear a pending connection when starting the UDC Alexey Charkov
2026-11-17 13:03 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 2/3] usb: gadget: udc: Forget a deactivation when the gadget driver unbinds Alexey Charkov
2026-11-17 13:06 ` sashiko-bot
2026-11-17 12:48 ` [PATCH v3 3/3] usb: gadget: udc: Decide on connecting in one place Alexey Charkov
2026-11-17 13:19 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox