USB: storage: add usbdisk alias for USB storage devices
Subsystems (4)
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]>
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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 2/2 usb: storage: add usbdisk alias for USB storage devices
In reply to Sascha HauerBoth 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
@@ -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
Sascha Hauer
AppliedQuoted 10 lines · Sascha Hauer
This is an alternative approach for:
https://lore.kernel.org/p/[email protected]
Instead of adding a new
Kconfigvariable we add a/dev/usbdiskxalias 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]>