[PATCH v3 06/31] dm: core: Allow finding a uclass device by partial name

Simon Glass sjg at chromium.org
Wed Jan 19 02:42:50 CET 2022


In some cases two devices are related and the only way to tell is to
check that the names partially patch. Add a way to check this without
needing to create a new string for the comparison.

Fix the comment for device_find_child_by_namelen() while we are here.

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

Changes in v3:
- Add new patch to support finding a uclass device by partial name

 drivers/core/uclass.c        | 13 ++++++++++---
 include/dm/device.h          |  2 +-
 include/dm/uclass-internal.h | 16 ++++++++++++++++
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 563ff9b2856..a0d3840802b 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -274,8 +274,8 @@ int uclass_find_next_device(struct udevice **devp)
 	return 0;
 }
 
-int uclass_find_device_by_name(enum uclass_id id, const char *name,
-			       struct udevice **devp)
+int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
+				  struct udevice **devp)
 {
 	struct uclass *uc;
 	struct udevice *dev;
@@ -289,7 +289,8 @@ int uclass_find_device_by_name(enum uclass_id id, const char *name,
 		return ret;
 
 	uclass_foreach_dev(dev, uc) {
-		if (!strcmp(dev->name, name)) {
+		if (!strncmp(dev->name, name, len) &&
+		    strlen(dev->name) == len) {
 			*devp = dev;
 			return 0;
 		}
@@ -298,6 +299,12 @@ int uclass_find_device_by_name(enum uclass_id id, const char *name,
 	return -ENODEV;
 }
 
+int uclass_find_device_by_name(enum uclass_id id, const char *name,
+			       struct udevice **devp)
+{
+	return uclass_find_device_by_namelen(id, name, strlen(name), devp);
+}
+
 int uclass_find_next_free_seq(struct uclass *uc)
 {
 	struct udevice *dev;
diff --git a/include/dm/device.h b/include/dm/device.h
index 435a1114f1c..30703af96e4 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -791,7 +791,7 @@ int device_find_first_child_by_uclass(const struct udevice *parent,
 				      struct udevice **devp);
 
 /**
- * device_find_child_by_name() - Find a child by device name
+ * device_find_child_by_namelen() - Find a child by device name
  *
  * @parent:	Parent device to search
  * @name:	Name to look for
diff --git a/include/dm/uclass-internal.h b/include/dm/uclass-internal.h
index fb0edcc2969..1118f64bdfd 100644
--- a/include/dm/uclass-internal.h
+++ b/include/dm/uclass-internal.h
@@ -154,6 +154,22 @@ int uclass_find_first_device(enum uclass_id id, struct udevice **devp);
  */
 int uclass_find_next_device(struct udevice **devp);
 
+/**
+ * uclass_find_device_by_namelen() - Find uclass device based on ID and name
+ *
+ * This searches for a device with the exactly given name.
+ *
+ * The device is NOT probed, it is merely returned.
+ *
+ * @id: ID to look up
+ * @name: name of a device to find
+ * @len: Length of @name (the uclass driver name must have the same length)
+ * @devp: Returns pointer to device (the first one with the name)
+ * @return 0 if OK, -ve on error
+ */
+int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
+				  struct udevice **devp);
+
 /**
  * uclass_find_device_by_name() - Find uclass device based on ID and name
  *
-- 
2.34.1.703.g22d0c6ccf7-goog



More information about the U-Boot mailing list