From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 08/13] usb: reuse the addresses of removed devices
Date: Mon, 31 Aug 2026 15:20:15 +0200 [thread overview]
Message-ID: <20260831-usb-device-lifetime-v1-8-6adf4054b909@pengutronix.de> (raw)
In-Reply-To: <20260831-usb-device-lifetime-v1-0-6adf4054b909@pengutronix.de>
dev_index only ever counted up, so every device that showed up got an
address one higher than the previous one. Without device removal that
was fine - there is a limited number of devices on a bus - but once
devices come and go, the counter keeps climbing. USB addresses are 7 bit
wide, so after 127 devices usb_set_address() starts handing out values
that cannot be represented and enumeration breaks until the next reset.
Keep a bitmap of the addresses in use instead and hand back the address
when the device is freed. As a side effect an exhausted address space is
now reported instead of silently producing broken addresses.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Assisted-by: Claude:claude-opus-5
---
drivers/usb/core/usb.c | 37 +++++++++++++++++++++++++++++++++++--
1 file changed, 35 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index bc80e66fcb..ad0d0965b1 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -38,6 +38,7 @@
#include <init.h>
#include <dma.h>
+#include <linux/bitmap.h>
#include <linux/usb/usb.h>
#include <linux/usb/ch9.h>
@@ -46,7 +47,33 @@
#define USB_BUFSIZ 512
static int dev_count;
-static int dev_index;
+
+/*
+ * USB addresses are 7 bit wide and 0 is reserved for the default address,
+ * so the usable range is 1..127. Track them in a bitmap rather than just
+ * counting up, otherwise a board that sees enough plug/unplug cycles
+ * eventually hands out addresses a device cannot have.
+ */
+static DECLARE_BITMAP(usb_addresses, 128);
+
+static int usb_alloc_address(void)
+{
+ int addr;
+
+ addr = find_next_zero_bit(usb_addresses, 128, 1);
+ if (addr >= 128)
+ return -EADDRNOTAVAIL;
+
+ set_bit(addr, usb_addresses);
+
+ return addr;
+}
+
+static void usb_free_address(int addr)
+{
+ if (addr > 0)
+ clear_bit(addr, usb_addresses);
+}
LIST_HEAD(usb_host_list);
LIST_HEAD(usb_device_list);
@@ -487,7 +514,12 @@ int usb_new_device(struct usb_device *dev)
usb_setup_descriptor(dev, !host->no_desc_before_addr);
- dev->devnum = ++dev_index;
+ err = usb_alloc_address();
+ if (err < 0) {
+ dev_err(&dev->dev, "out of USB addresses\n");
+ goto err_out;
+ }
+ dev->devnum = err;
err = usb_set_address(dev); /* set address */
@@ -605,6 +637,7 @@ int usb_new_device(struct usb_device *dev)
void usb_free_device(struct usb_device *usbdev)
{
+ usb_free_address(usbdev->devnum);
dma_free(usbdev->descriptor);
dma_free(usbdev->setup_packet);
free_device_res(&usbdev->dev);
--
2.47.3
next prev parent reply other threads:[~2026-08-31 13:25 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 13:20 [PATCH 00/13] USB: Make USB devices removable Sascha Hauer
2026-08-31 13:20 ` [PATCH 01/13] usb: don't report device removal after the device name is gone Sascha Hauer
2026-08-31 13:20 ` [PATCH 02/13] fs: devfs: count an open partition as an open device Sascha Hauer
2026-08-31 13:20 ` [PATCH 03/13] block: propagate errors from blockdevice_unregister() Sascha Hauer
2026-08-31 13:20 ` [PATCH 04/13] fs: add cdev_umount_all() Sascha Hauer
2026-08-31 13:20 ` [PATCH 05/13] block: add blockdevice_unregister_removed() Sascha Hauer
2026-08-31 13:20 ` [PATCH 06/13] usb: storage: tear the disk down properly on disconnect Sascha Hauer
2026-08-31 13:20 ` [PATCH 07/13] usb: hub: cancel pending port scans of a removed device Sascha Hauer
2026-08-31 13:20 ` Sascha Hauer [this message]
2026-08-31 13:20 ` [PATCH 09/13] usb: hub: detect disconnected devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 10/13] usb: don't keep a dangling root device on enumeration failure Sascha Hauer
2026-08-31 13:20 ` [PATCH 11/13] usb: hub: limit the number of ports to USB_MAXCHILDREN Sascha Hauer
2026-08-31 13:20 ` [PATCH 12/13] usb: detect unplugged devices on transfer errors Sascha Hauer
2026-08-31 13:20 ` [PATCH 13/13] usb: storage: stop talking to a device that is gone Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260831-usb-device-lifetime-v1-8-6adf4054b909@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox