Skip to contentSkip to conversation

USB: storage: add usbdisk alias for USB storage devices

PATCH Applied Sascha Hauer 1 participant 4 messages Jun 30 – Jul 7

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover USB: storage: add usbdisk alias for USB storage devices

This is an alternative approach for:

https://lore.kernel.org/p/[email protected]

Instead of adding a new Kconfig variable we add a /dev/usbdiskx alias for each USB storage device. This allows us to easily use the first USB storage device instead of stumbling upon other devices which are also registered as /dev/diskx.

---
Sascha Hauer (2):
fs: devfs: make cdev_find_free_index() return 0 instead of an error
usb: storage: add usbdisk alias for USB storage devices

 drivers/ata/disk_ata_drive.c |  2 --
 drivers/hw_random/core.c     | 13 +++----------
 drivers/usb/storage/usb.c    | 17 ++++++++++++-----
 fs/devfs-core.c              | 10 +++++++++-
 4 files changed, 24 insertions(+), 18 deletions(-)

---
base-commit: 18599acfcbf38e95edc605f89e257e32f67d0ac4
change-id: 20260630-usb-disk-alias-85022129192f

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>
#1 lore raw

PATCH 1/2 fs: devfs: make cdev_find_free_index() return 0 instead of an error

We should never run out of free indexes in realistic scenarios. When we do, returning 0 ensures that the caller's cdev registration just fails with -EEXIST, which is a normal error path. Add an error message so the user realizes that something is wrong here and not in the devfs registration itself.

With this the callers no longer need to handle a negative return value, so drop their (incomplete) error handling.

Assisted-by Claude Opus 4.8
---
 drivers/ata/disk_ata_drive.c |  2 --
 drivers/hw_random/core.c     | 13 +++----------
 drivers/usb/storage/usb.c    |  2 --
 fs/devfs-core.c              | 10 +++++++++-
 4 files changed, 12 insertions(+), 15 deletions(-)
drivers/ata/disk_ata_drive.c +0 -2
index c5bf17d863..0509b7dc78 100644
@@ -238,8 +238,6 @@ static int ata_port_init(struct ata_port *port)
 		port->blk.cdev.name = xstrdup(port->devname);
 	} else {
 		rc = cdev_find_free_index("ata");
-		if (rc == -1)
-			pr_err("Cannot find a free index for the disk node\n");
 		port->blk.cdev.name = basprintf("ata%d", rc);
 	}
 
drivers/hw_random/core.c +3 -10
index 9f73aa45bc..ef960b787c 100644
@@ -67,19 +67,12 @@ static int hwrng_register_cdev(struct hwrng *rng)
 	struct device *dev = rng->dev;
 	const char *alias;
 	char *devname;
-	int err;
 
 	alias = of_alias_get(dev->of_node);
-	if (alias) {
+	if (alias)
 		devname = xstrdup(alias);
-	} else {
-		err = cdev_find_free_index("hwrng");
-		if (err < 0) {
-			dev_err(dev, "no index found to name device\n");
-			return err;
-		}
-		devname = xasprintf("hwrng%d", err);
-	}
+	else
+		devname = xasprintf("hwrng%d", cdev_find_free_index("hwrng"));
 
 	rng->cdev.name = devname;
 	rng->cdev.flags = DEVFS_IS_CHARACTER_DEV;
drivers/usb/storage/usb.c +0 -2
index b3116dc6e6..2ad08183f6 100644
@@ -415,8 +415,6 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 		goto BadDevice;
 
 	result = cdev_find_free_index("disk");
-	if (result == -1)
-		pr_err("Cannot find a free number for the disk node\n");
 	dev_info(dev, "registering as disk%d\n", result);
 
 	pblk_dev->blk.cdev.name = basprintf("disk%d", result);
fs/devfs-core.c +9 -1
index 67ce678517..522d883e1c 100644
@@ -223,7 +223,15 @@ int cdev_find_free_index(const char *basename)
 			return i;
 	}
 
-	return -EBUSY;	/* all indexes are used */
+	/*
+	 * We should never run out of free indexes in realistic scenarios.
+	 * Return 0 so that the caller's cdev registration fails with -EEXIST,
+	 * which is a normal error path. The message tells the user that
+	 * something is wrong here and not in the devfs registration itself.
+	 */
+	pr_err("Cannot find a free index for '%s'\n", basename);
+
+	return 0;
 }
 
 static struct cdev *cdev_get_master(struct cdev *cdev)
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 usb: storage: add usbdisk alias for USB storage devices

In reply to Sascha Hauer

Both MMC/SD and USB storage devices can be registered as "disk0" so there is no way to access the first USB storage device. Add an "usbdisk" alias so a user can easily detect the presence of a USB storage device and acces it.

---
 drivers/usb/storage/usb.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/usb/storage/usb.c +12 -3
index 2ad08183f6..6251a6e5a5 100644
@@ -399,6 +399,7 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 	struct device *dev = &us->pusb_dev->dev;
 	struct us_blk_dev *pblk_dev;
 	int result;
+	char *diskname = NULL, *alias = NULL;
 
 	/* allocate a new USB block device */
 	pblk_dev = xzalloc(sizeof(struct us_blk_dev));
@@ -414,10 +415,12 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 	if (result < 0)
 		goto BadDevice;
 
-	result = cdev_find_free_index("disk");
-	dev_info(dev, "registering as disk%d\n", result);
+	diskname = xasprintf("disk%d", cdev_find_free_index("disk"));
+	alias = xasprintf("usbdisk%d", cdev_find_free_index("usbdisk"));
 
-	pblk_dev->blk.cdev.name = basprintf("disk%d", result);
+	dev_info(dev, "registering as %s (%s)\n", diskname, alias);
+
+	pblk_dev->blk.cdev.name = diskname;
 	pblk_dev->blk.blockbits = SECTOR_SHIFT;
 	pblk_dev->blk.type = BLK_TYPE_USB;
 	pblk_dev->blk.removable = true;
@@ -429,6 +432,9 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 		goto BadDevice;
 	}
 
+	devfs_add_alias(&pblk_dev->blk.cdev, alias);
+	free(alias);
+
 	list_add_tail(&pblk_dev->list, &us->blk_dev_list);
 	dev_dbg(dev, "USB disk device successfully added\n");
 
@@ -437,6 +443,9 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 BadDevice:
 	dev_dbg(dev, "%s failed with %d\n", __func__, result);
 	free(pblk_dev);
+	free(diskname);
+	free(alias);
+
 	return result;
 }
 
Signature · 1 line
2.47.3
author Applied
#3 lore raw
In reply to Sascha Hauer
Quoted 10 lines · Sascha Hauer

This is an alternative approach for:

https://lore.kernel.org/p/[email protected]

Instead of adding a new Kconfig variable we add a /dev/usbdiskx alias for each USB storage device. This allows us to easily use the first USB storage device instead of stumbling upon other devices which are also registered as /dev/diskx.

[...]

Applied, thanks!

[1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error

      https://git.pengutronix.de/cgit/barebox/commit/?id=3555017d7539 (link may not be stable)

[2/2] usb: storage: add usbdisk alias for USB storage devices
https://git.pengutronix.de/cgit/barebox/commit/?id=9b7c7f7b18d7 (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>