]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: core: Allow finding a uclass device by partial name
authorSimon Glass <sjg@chromium.org>
Mon, 25 Apr 2022 05:31:00 +0000 (23:31 -0600)
committerTom Rini <trini@konsulko.com>
Mon, 25 Apr 2022 14:00:03 +0000 (10:00 -0400)
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@chromium.org>
drivers/core/uclass.c
include/dm/device.h
include/dm/uclass-internal.h
test/dm/core.c

index 4b9b54f0f159bcca2fcec20c292b769bc6529240..08d9ed82de2d4c45ce3614f24296d9e4607399ff 100644 (file)
@@ -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;
index e0f86f5df9f2681ad099f3f17ee59e9f3ed003a5..b474888d025e7d2c75328f392a37f30e50dfc60e 100644 (file)
@@ -799,7 +799,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
index daf856c03cf281886d67dea1beacbad05c367b44..3ddcdd21439cad4b79464b161d0ada376eaff252 100644 (file)
@@ -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
  *
index 2c73ecf54a0e7d4ef01f59ea69c654377d19fa17..ebd504427d13bfddae4f17b88c215015fa1bac32 100644 (file)
@@ -1260,3 +1260,18 @@ static int dm_test_get_stats(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_get_stats, UT_TESTF_SCAN_FDT);
+
+/* Test uclass_find_device_by_name() */
+static int dm_test_uclass_find_device(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+
+       ut_assertok(uclass_find_device_by_name(UCLASS_I2C, "i2c@0", &dev));
+       ut_asserteq(-ENODEV,
+                   uclass_find_device_by_name(UCLASS_I2C, "i2c@0x", &dev));
+       ut_assertok(uclass_find_device_by_namelen(UCLASS_I2C, "i2c@0x", 5,
+                                                 &dev));
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_find_device, UT_TESTF_SCAN_FDT);