blk_get_uclass_name() turns an interface type into a name, but there is no
public way to go the other way. The driver model block layer already has
the lookup, as a static helper behind blk_get_devnum_by_uclass_idname(),
which resolves a device at the same time.

A caller that wants to accept an interface name from the user, and only
resolve a device later, has no way to tell a bad name from a device that
is not there yet. Export the lookup under a name that mirrors its
counterpart, and add the legacy block layer equivalent.

Signed-off-by: Jan Kehren <[email protected]>
---
Changes in v3:
- New patch: "avb init" now validates an interface name without
  resolving a device, and the block layer only had the reverse lookup

 drivers/block/blk-uclass.c |  4 ++--
 drivers/block/blk_legacy.c |  7 +++++++
 include/blk.h              | 11 +++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index 6d2161cfc7..37d87be3a5 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -40,7 +40,7 @@ static struct {
        { UCLASS_MTD, "ubi" },
 };
 
-static enum uclass_id uclass_name_to_iftype(const char *uclass_idname)
+enum uclass_id blk_get_uclass_id(const char *uclass_idname)
 {
        int i;
 
@@ -115,7 +115,7 @@ struct blk_desc *blk_get_devnum_by_uclass_idname(const char 
*uclass_idname, int
        struct uclass *uc;
        int ret;
 
-       type = uclass_name_to_iftype(uclass_idname);
+       type = blk_get_uclass_id(uclass_idname);
        if (type == UCLASS_INVALID) {
                debug("%s: Unknown interface type '%s'\n", __func__,
                      uclass_idname);
diff --git a/drivers/block/blk_legacy.c b/drivers/block/blk_legacy.c
index f36932183d..7ef7115165 100644
--- a/drivers/block/blk_legacy.c
+++ b/drivers/block/blk_legacy.c
@@ -45,6 +45,13 @@ const char *blk_get_uclass_name(enum uclass_id uclass_id)
        return drv ? drv->uclass_idname : NULL;
 }
 
+enum uclass_id blk_get_uclass_id(const char *uclass_idname)
+{
+       struct blk_driver *drv = blk_driver_lookup_typename(uclass_idname);
+
+       return drv ? drv->uclass_id : UCLASS_INVALID;
+}
+
 /**
  * get_desc() - Get the block device descriptor for the given device number
  *
diff --git a/include/blk.h b/include/blk.h
index 2cb9cb31ac..5ef9129b3e 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -741,6 +741,17 @@ int blk_select_hwpart_devnum(enum uclass_id uclass_id, int 
devnum, int hwpart);
  */
 const char *blk_get_uclass_name(enum uclass_id uclass_id);
 
+/**
+ * blk_get_uclass_id() - Get the interface type for a name
+ *
+ * This does not look up or probe any device, so it can be used to validate
+ * an interface name before a device of that type exists.
+ *
+ * @uclass_idname: Interface name to check, e.g. "mmc"
+ * Return: interface type, or UCLASS_INVALID if the name is not known
+ */
+enum uclass_id blk_get_uclass_id(const char *uclass_idname);
+
 /**
  * blk_common_cmd() - handle common commands with block devices
  *
-- 
2.43.0

Reply via email to