[U-Boot] [PATCH 40/44] dm: blk: Allow blk_create_device() to allocate the device number

Simon Glass
2016-04-10T02:45:32Z

Allow a devnum parameter of -1 to indicate that the device number should be
alocated automatically. The next highest available device number for that
interface type is used.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

drivers/block/blk-uclass.c | 29 +++++++++++++++++++++++++++++
include/blk.h | 15 ++++++++++++++-
2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index 3687b9a..c947d95 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -411,6 +411,26 @@ int blk_prepare_device(struct udevice *dev)
return 0;
}

+int blk_find_max_devnum(enum if_type if_type)
+{
+ struct udevice *dev;
+ int max_devnum = -ENODEV;
+ struct uclass *uc;
+ int ret;
+
+ ret = uclass_get(UCLASS_BLK, &uc);
+ if (ret)
+ return ret;
+ uclass_foreach_dev(dev, uc) {
+ struct blk_desc *desc = dev_get_uclass_platdata(dev);
+
+ if (desc->if_type == if_type && desc->devnum > max_devnum)
+ max_devnum = desc->devnum;
+ }
+
+ return max_devnum;
+}
+
int blk_create_device(struct udevice *parent, const char *drv_name,
const char *name, int if_type, int devnum, int blksz,
lbaint_t size, struct udevice **devp)
@@ -428,6 +448,15 @@ int blk_create_device(struct udevice *parent, const char *drv_name,
desc->lba = size / blksz;
desc->part_type = PART_TYPE_UNKNOWN;
desc->bdev = dev;
+ if (devnum == -1) {
+ ret = blk_find_max_devnum(if_type);
+ if (ret == -ENODEV)
+ devnum = 0;
+ else if (ret < 0)
+ return ret;
+ else
+ devnum = ret + 1;
+ }
desc->devnum = devnum;
*devp = dev;

diff --git a/include/blk.h b/include/blk.h
index 12be6aa..9f4a63d 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -276,7 +276,8 @@ int blk_next_device(struct udevice **devp);
* @drv_name: Driver name to use for the block device
* @name: Name for the device
* @if_type: Interface type (enum if_type_t)
- * @devnum: Device number, specific to the interface type
+ * @devnum: Device number, specific to the interface type, or -1 to
+ * allocate the next available number
* @blksz: Block size of the device in bytes (typically 512)
* @size: Total size of the device in bytes
* @devp: the new device (which has not been probed)
@@ -305,6 +306,18 @@ int blk_prepare_device(struct udevice *dev);
*/
int blk_unbind_all(int if_type);

+/**
+ * blk_find_max_devnum() - find the maximum device number for an interface type
+ *
+ * Finds the last allocated device number for an interface type @if_type. The
+ * next number is safe to use for a newly allocated device.
+ *
+ * @if_type: Interface type to scan
+ * @return maximum device number found, or -ENODEV if none, or other -ve on
+ * error
+ */
+int blk_find_max_devnum(enum if_type if_type);
+
#else
#include <errno.h>
/*
--
2.8.0.rc3.226.g39d4020