Add a function to count the available children of a device.

Signed-off-by: Lokesh Vutla <lokeshvu...@ti.com>
---
 drivers/core/device.c | 11 +++++++++++
 include/dm/device.h   |  9 +++++++++
 test/dm/bus.c         | 41 +++++++++++++++--------------------------
 3 files changed, 35 insertions(+), 26 deletions(-)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 474c1642ee..beff52009d 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -565,6 +565,17 @@ int device_get_child(struct udevice *parent, int index, 
struct udevice **devp)
        return -ENODEV;
 }
 
+int device_get_child_count(struct udevice *parent)
+{
+       struct udevice *dev;
+       int count = 0;
+
+       list_for_each_entry(dev, &parent->child_head, sibling_node)
+               count++;
+
+       return count;
+}
+
 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
                             bool find_req_seq, struct udevice **devp)
 {
diff --git a/include/dm/device.h b/include/dm/device.h
index 27a6d7b9fd..7e2a097de2 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -404,6 +404,15 @@ const char *dev_get_uclass_name(const struct udevice *dev);
  */
 int device_get_child(struct udevice *parent, int index, struct udevice **devp);
 
+/**
+ * device_get_child_count() - Get the available child count of a device
+ *
+ * Returns the number of children to a device.
+ *
+ * @parent:    Parent device to check
+ */
+int device_get_child_count(struct udevice *parent);
+
 /**
  * device_find_child_by_seq() - Find a child device based on a sequence
  *
diff --git a/test/dm/bus.c b/test/dm/bus.c
index 93f3acd430..1ad45adb60 100644
--- a/test/dm/bus.c
+++ b/test/dm/bus.c
@@ -8,6 +8,7 @@
 #include <os.h>
 #endif
 #include <dm.h>
+#include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/test.h>
 #include <dm/uclass-internal.h>
@@ -371,7 +372,6 @@ static int test_bus_parent_platdata(struct unit_test_state 
*uts)
 {
        struct dm_test_parent_platdata *plat;
        struct udevice *bus, *dev;
-       int child_count;
 
        /* Check that the bus has no children */
        ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
@@ -380,7 +380,7 @@ static int test_bus_parent_platdata(struct unit_test_state 
*uts)
 
        ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
 
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                /* Check that platform data is allocated */
@@ -399,22 +399,20 @@ static int test_bus_parent_platdata(struct 
unit_test_state *uts)
                ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
                ut_asserteq(1, plat->count);
                ut_assertok(device_probe(dev));
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        /* Removing the bus should also have no effect (it is still bound) */
        device_remove(bus, DM_REMOVE_NORMAL);
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                /* Check that platform data is allocated */
                plat = dev_get_parent_platdata(dev);
                ut_assert(plat != NULL);
                ut_asserteq(1, plat->count);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        /* Unbind all the children */
        do {
@@ -425,16 +423,15 @@ static int test_bus_parent_platdata(struct 
unit_test_state *uts)
 
        /* Now the child platdata should be removed and re-added */
        device_probe(bus);
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                /* Check that platform data is allocated */
                plat = dev_get_parent_platdata(dev);
                ut_assert(plat != NULL);
                ut_asserteq(0, plat->count);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        return 0;
 }
@@ -480,19 +477,17 @@ static int dm_test_bus_child_post_bind(struct 
unit_test_state *uts)
 {
        struct dm_test_parent_platdata *plat;
        struct udevice *bus, *dev;
-       int child_count;
 
        ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                /* Check that platform data is allocated */
                plat = dev_get_parent_platdata(dev);
                ut_assert(plat != NULL);
                ut_asserteq(1, plat->bind_flag);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        return 0;
 }
@@ -503,19 +498,17 @@ static int dm_test_bus_child_post_bind_uclass(struct 
unit_test_state *uts)
 {
        struct dm_test_parent_platdata *plat;
        struct udevice *bus, *dev;
-       int child_count;
 
        ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                /* Check that platform data is allocated */
                plat = dev_get_parent_platdata(dev);
                ut_assert(plat != NULL);
                ut_asserteq(2, plat->uclass_bind_flag);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        return 0;
 }
@@ -529,14 +522,13 @@ DM_TEST(dm_test_bus_child_post_bind_uclass,
 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
 {
        struct udevice *bus, *dev;
-       int child_count;
 
        /*
         * See testfdt_drv_probe() which effectively checks that the uclass
         * flag is set before that method is called
         */
        ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                struct dm_test_priv *priv = dev_get_priv(dev);
@@ -549,9 +541,8 @@ static int dm_test_bus_child_pre_probe_uclass(struct 
unit_test_state *uts)
                ut_assert(priv != NULL);
                ut_asserteq(1, priv->uclass_flag);
                ut_asserteq(1, priv->uclass_total);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        return 0;
 }
@@ -565,14 +556,13 @@ DM_TEST(dm_test_bus_child_pre_probe_uclass,
 static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
 {
        struct udevice *bus, *dev;
-       int child_count;
 
        /*
         * See testfdt_drv_probe() which effectively initializes that
         * the uclass postp flag is set to a value
         */
        ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
-       for (device_find_first_child(bus, &dev), child_count = 0;
+       for (device_find_first_child(bus, &dev);
             dev;
             device_find_next_child(&dev)) {
                struct dm_test_priv *priv = dev_get_priv(dev);
@@ -584,9 +574,8 @@ static int dm_test_bus_child_post_probe_uclass(struct 
unit_test_state *uts)
                priv = dev_get_priv(dev);
                ut_assert(priv != NULL);
                ut_asserteq(0, priv->uclass_postp);
-               child_count++;
        }
-       ut_asserteq(3, child_count);
+       ut_asserteq(3, device_get_child_count(bus));
 
        return 0;
 }
-- 
2.22.0

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to