dm: core: Allow finding children / uclasses by partial name
authorSimon Glass <sjg@chromium.org>
Sat, 23 Oct 2021 23:26:08 +0000 (17:26 -0600)
committerSimon Glass <sjg@chromium.org>
Sun, 28 Nov 2021 23:51:51 +0000 (16:51 -0700)
In some cases it is useful to search just by a partial name, such as
when looking for a sibling device that has a common name substring. Add
helper functions to handle these requirements.

Signed-off-by: Simon Glass <sjg@chromium.org>
drivers/core/device.c
drivers/core/uclass.c
include/dm/device.h
include/dm/uclass.h

index efd07176e37485b4cb910a2dda231e50dda7aa8e..aed093c2af139032c3e52136d4eaea980d10a138 100644 (file)
@@ -902,15 +902,16 @@ int device_find_first_child_by_uclass(const struct udevice *parent,
        return -ENODEV;
 }
 
-int device_find_child_by_name(const struct udevice *parent, const char *name,
-                             struct udevice **devp)
+int device_find_child_by_namelen(const struct udevice *parent, const char *name,
+                                int len, struct udevice **devp)
 {
        struct udevice *dev;
 
        *devp = NULL;
 
        list_for_each_entry(dev, &parent->child_head, sibling_node) {
-               if (!strcmp(dev->name, name)) {
+               if (!strncmp(dev->name, name, len) &&
+                   strlen(dev->name) == len) {
                        *devp = dev;
                        return 0;
                }
@@ -919,6 +920,12 @@ int device_find_child_by_name(const struct udevice *parent, const char *name,
        return -ENODEV;
 }
 
+int device_find_child_by_name(const struct udevice *parent, const char *name,
+                             struct udevice **devp)
+{
+       return device_find_child_by_namelen(parent, name, strlen(name), devp);
+}
+
 int device_first_child_err(struct udevice *parent, struct udevice **devp)
 {
        struct udevice *dev;
index 2fede896bfb5f19b16772d4602b6e20601301ac4..3de5f27fe4ae819249b6900e39e2caeb74c07a78 100644 (file)
@@ -180,20 +180,25 @@ void uclass_set_priv(struct uclass *uc, void *priv)
        uc->priv_ = priv;
 }
 
-enum uclass_id uclass_get_by_name(const char *name)
+enum uclass_id uclass_get_by_name_len(const char *name, int len)
 {
        int i;
 
        for (i = 0; i < UCLASS_COUNT; i++) {
                struct uclass_driver *uc_drv = lists_uclass_lookup(i);
 
-               if (uc_drv && !strcmp(uc_drv->name, name))
+               if (uc_drv && !strncmp(uc_drv->name, name, len))
                        return i;
        }
 
        return UCLASS_INVALID;
 }
 
+enum uclass_id uclass_get_by_name(const char *name)
+{
+       return uclass_get_by_name_len(name, strlen(name));
+}
+
 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
 {
        struct udevice *iter;
index 3028d002ab0dbda9eb25c23efe9ebb0acc256ca4..daf28a0a457e6b5b27d53cd403c91bac70150cdf 100644 (file)
@@ -758,6 +758,18 @@ int device_find_first_child_by_uclass(const struct udevice *parent,
                                      enum uclass_id uclass_id,
                                      struct udevice **devp);
 
+/**
+ * device_find_child_by_name() - Find a child by device name
+ *
+ * @parent:    Parent device to search
+ * @name:      Name to look for
+ * @len:       Length of the name
+ * @devp:      Returns device found, if any
+ * @return 0 if found, else -ENODEV
+ */
+int device_find_child_by_namelen(const struct udevice *parent, const char *name,
+                                int len, struct udevice **devp);
+
 /**
  * device_find_child_by_name() - Find a child by device name
  *
index 15e5f9ef5bcd9a60a9f1e62d848d68bad599f78a..aea2f34fce14b7022f9073825e60cdc7e0909368 100644 (file)
@@ -172,6 +172,15 @@ int uclass_get(enum uclass_id key, struct uclass **ucp);
  */
 const char *uclass_get_name(enum uclass_id id);
 
+/**
+ * uclass_get_by_name() - Look up a uclass by its driver name
+ *
+ * @name: Name to look up
+ * @len: Length of name
+ * @returns the associated uclass ID, or UCLASS_INVALID if not found
+ */
+enum uclass_id uclass_get_by_name_len(const char *name, int len);
+
 /**
  * uclass_get_by_name() - Look up a uclass by its driver name
  *