[PATCH 12/13] usb: detect unplugged devices on transfer errors

Sascha Hauer s.hauer at pengutronix.de
Mon Aug 31 06:20:19 PDT 2026


Removing a device is only done when somebody runs "usb". Until then I/O
to a device that has been unplugged in the meantime keeps failing, and
neither the USB core nor the drivers realise why. Reading from a
filesystem that was mounted off a stick that is gone results in an
endless stream of

	usb-storage usb1-0-1: Resetting EP 1...
	ERROR: xHCI xHCI0: halted endpoint, not queueing URB.

as the storage driver keeps resetting endpoints of a device that is not
there and retries the command.

When a transfer fails, ask the hub the device is attached to whether the
port still reports a connection. That is cheap - the hub is still there -
and it answers the question the transfer error cannot: is this a flaky
device or no device at all. Remember the answer, so the bus is asked once
and every further transfer fails with -ENODEV right away.

Only a hub that positively reports an empty port counts. If the hub
cannot be asked we have learned nothing, and marking a device gone
because some unrelated transfer failed would be worse than leaving it
alone. A device behind a hub that turns out to be gone is marked as well,
along with everything below it.

Removal itself is left to the next scan. Tearing the device down from
inside a transfer would pull the block device and the cdevs out from
under the filesystem that is asking for the data.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
Assisted-by: Claude:claude-opus-5
---
 drivers/usb/core/hub.c  | 22 ++++++++++++
 drivers/usb/core/usb.c  | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 drivers/usb/core/usb.h  |  1 +
 include/linux/usb/usb.h | 16 +++++++++
 4 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index f897740cbf..6520cd43d5 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -260,6 +260,28 @@ static int hub_port_reset(struct usb_device *hub, int port,
 }
 
 
+/**
+ * usb_hub_port_connected - ask a hub whether a port still has a device
+ * @hub: the hub to ask
+ * @port: 1-based port number
+ *
+ * Return: 1 if the port reports a device, 0 if it doesn't and a negative
+ * error code when the hub could not be asked. Do not treat the latter as
+ * "no device": a hub that doesn't answer tells us nothing about what is
+ * plugged into it.
+ */
+int usb_hub_port_connected(struct usb_device *hub, int port)
+{
+	struct usb_port_status portsts;
+	int ret;
+
+	ret = usb_get_port_status(hub, port, &portsts);
+	if (ret < 0)
+		return ret;
+
+	return !!(le16_to_cpu(portsts.wPortStatus) & USB_PORT_STAT_CONNECTION);
+}
+
 /**
  * usb_hub_cancel_scans - drop pending port scans of a hub that goes away
  * @dev: the USB device that is about to be removed
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 55c57e6334..71b72c3efb 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -751,6 +751,73 @@ int usb_rescan(void)
  *
  */
 
+/**
+ * usb_mark_disconnected - remember that a device and its children are gone
+ * @dev: the device that has been unplugged
+ */
+static void usb_mark_disconnected(struct usb_device *dev)
+{
+	int i;
+
+	if (usb_device_disconnected(dev))
+		return;
+
+	dev->disconnected = true;
+	dev_info(&dev->dev, "disconnected\n");
+
+	/* everything behind an unplugged hub is gone as well */
+	for (i = 0; i < dev->maxchild; i++)
+		if (dev->children[i])
+			usb_mark_disconnected(dev->children[i]);
+}
+
+/**
+ * usb_device_check_gone - find out whether a failed transfer means "unplugged"
+ * @dev: the device a transfer just failed on
+ *
+ * A transfer can fail for all kinds of reasons, so ask the hub the device
+ * is attached to whether the port still sees a device. Without this the
+ * only thing telling a stale device from a flaky one is a scan, and until
+ * somebody runs one we keep resetting endpoints of a device that is not
+ * there anymore.
+ *
+ * The result is remembered, so this asks the bus once and all further
+ * transfers fail right away.
+ */
+static bool usb_device_check_gone(struct usb_device *dev)
+{
+	struct usb_device *parent = dev->parent;
+
+	if (usb_device_disconnected(dev))
+		return true;
+
+	/*
+	 * A root hub has no hub to ask. It goes away with its controller,
+	 * which is not something we can detect here.
+	 */
+	if (!parent)
+		return false;
+
+	/* If the hub itself is gone, so is everything below it */
+	if (usb_device_check_gone(parent)) {
+		usb_mark_disconnected(dev);
+		return true;
+	}
+
+	/*
+	 * Only believe a hub that positively says the port is empty. If it
+	 * doesn't answer we have learned nothing, and declaring the device
+	 * gone on a transfer that failed for some other reason would be
+	 * worse than leaving it alone.
+	 */
+	if (usb_hub_port_connected(parent, dev->portnr) != 0)
+		return false;
+
+	usb_mark_disconnected(dev);
+
+	return true;
+}
+
 /*
  * submits an Interrupt Message
  */
@@ -760,6 +827,9 @@ int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
 	struct usb_host *host = dev->host;
 	int ret;
 
+	if (usb_device_disconnected(dev))
+		return -ENODEV;
+
 	ret = usb_host_acquire(host);
 	if (ret)
 		return ret;
@@ -768,6 +838,9 @@ int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
 
 	usb_host_release(host);
 
+	if (ret && usb_device_check_gone(dev))
+		return -ENODEV;
+
 	return ret;
 }
 
@@ -788,6 +861,9 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
 	int ret;
 	struct devrequest *setup_packet = dev->setup_packet;
 
+	if (usb_device_disconnected(dev))
+		return -ENODEV;
+
 	ret = usb_host_acquire(host);
 	if (ret)
 		return ret;
@@ -808,8 +884,11 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
 
 	usb_host_release(host);
 
-	if (ret)
+	if (ret) {
+		if (usb_device_check_gone(dev))
+			return -ENODEV;
 		return ret;
+	}
 
 	return dev->act_len;
 }
@@ -828,6 +907,9 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
 	if (len < 0)
 		return -1;
 
+	if (usb_device_disconnected(dev))
+		return -ENODEV;
+
 	ret = usb_host_acquire(host);
 	if (ret)
 		return ret;
@@ -837,8 +919,11 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
 
 	usb_host_release(host);
 
-	if (ret)
+	if (ret) {
+		if (usb_device_check_gone(dev))
+			return -ENODEV;
 		return ret;
+	}
 
 	*actual_length = dev->act_len;
 
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index b3c224d88f..2529bcfec1 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -7,5 +7,6 @@ void usb_free_device(struct usb_device *dev);
 int usb_new_device(struct usb_device *dev);
 void usb_remove_device(struct usb_device *dev);
 void usb_hub_cancel_scans(struct usb_device *dev);
+int usb_hub_port_connected(struct usb_device *hub, int port);
 
 #endif /* __CORE_USB_H */
diff --git a/include/linux/usb/usb.h b/include/linux/usb/usb.h
index c25f3d73c3..784786dad5 100644
--- a/include/linux/usb/usb.h
+++ b/include/linux/usb/usb.h
@@ -126,8 +126,24 @@ struct usb_device {
 
 	/* slot_id - for xHCI enabled devices */
 	unsigned int slot_id;
+
+	/* device is physically gone, don't talk to it anymore */
+	bool disconnected;
 };
 
+/**
+ * usb_device_disconnected - has the device been unplugged?
+ * @dev: the USB device
+ *
+ * Set once a transfer to the device failed and the hub it is attached to
+ * confirmed that the port has no connection anymore. Transfers to such a
+ * device fail with -ENODEV without touching the bus.
+ */
+static inline bool usb_device_disconnected(struct usb_device *dev)
+{
+	return dev->disconnected;
+}
+
 struct usb_device_id;
 
 struct usb_driver {

-- 
2.47.3




More information about the barebox mailing list