]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: core: Add a function to see if a device exists
authorSimon Glass <sjg@chromium.org>
Sat, 19 Oct 2024 15:21:49 +0000 (09:21 -0600)
committerSimon Glass <sjg@chromium.org>
Sat, 2 Nov 2024 17:13:59 +0000 (11:13 -0600)
All the uclass functions for finding a device end up creating a uclass
if it doesn't exist. Add a function which instead returns NULL in this
case.

This is useful when in the 'unbind' path, since we don't want to undo
any unbinding which has already happened.

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

index 7ae0884a75eccc6a0705839b9b03d829673a35e1..f846a35d6b2537bab56bca158cedfb134b37f477 100644 (file)
@@ -304,6 +304,17 @@ int uclass_find_device_by_name(enum uclass_id id, const char *name,
        return uclass_find_device_by_namelen(id, name, strlen(name), devp);
 }
 
+struct udevice *uclass_try_first_device(enum uclass_id id)
+{
+       struct uclass *uc;
+
+       uc = uclass_find(id);
+       if (!uc || list_empty(&uc->dev_head))
+               return NULL;
+
+       return list_first_entry(&uc->dev_head, struct udevice, uclass_node);
+}
+
 int uclass_find_next_free_seq(struct uclass *uc)
 {
        struct udevice *dev;
index 456eef7f2f319ec07132d9e567b2d958efe58013..c279304092352200d5297ad2d17f527173ec506b 100644 (file)
@@ -435,6 +435,17 @@ int uclass_next_device_check(struct udevice **devp);
 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
                                struct udevice **devp);
 
+/**
+ * uclass_try_first_device()- See if there is a device for a uclass
+ *
+ * If the uclass exists, this returns the first device on that uclass, without
+ * probing it. If the uclass does not exist, it gives up
+ *
+ * @id: Uclass ID to check
+ * Return: Pointer to device, if found, else NULL
+ */
+struct udevice *uclass_try_first_device(enum uclass_id id);
+
 /**
  * uclass_probe_all() - Probe all devices based on an uclass ID
  *
index e0c5b9e0017c760dc800253ab4e9edd93a688101..7371d3ff42695b4eabaf8ff4b31e4197b3a0c8f8 100644 (file)
@@ -1351,3 +1351,25 @@ static int dm_test_dev_get_mem(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_dev_get_mem, UTF_SCAN_FDT);
+
+/* Test uclass_try_first_device() */
+static int dm_test_try_first_device(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+
+       /* Check that it doesn't create a device or uclass */
+       ut_assertnull(uclass_find(UCLASS_TEST));
+       ut_assertnull(uclass_try_first_device(UCLASS_TEST));
+       ut_assertnull(uclass_try_first_device(UCLASS_TEST));
+       ut_assertnull(uclass_find(UCLASS_TEST));
+
+       /* Create a test device */
+       ut_assertok(device_bind_by_name(uts->root, false, &driver_info_manual,
+                                       &dev));
+       dev = uclass_try_first_device(UCLASS_TEST);
+       ut_assertnonnull(dev);
+       ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
+
+       return 0;
+}
+DM_TEST(dm_test_try_first_device, 0);