diff mbox series

[09/20] dm: test: Add a check that all devices have a dev value

Message ID 20201003173142.3213123-10-sjg@chromium.org
State Accepted
Commit 36af37b9367677f16b09c7d57fb84674979a7a2b
Delegated to: Simon Glass
Headers show
Series dm: Enhance of-platdata to support parent devices | expand

Commit Message

Simon Glass Oct. 3, 2020, 5:31 p.m. UTC
With of-platdata, the driver_info struct is updated with the device
pointer when it is bound. This makes it easy for a device to be found by
its driver info with the device_get_by_driver_info() function.

Add a test that all devices (except the root device) have such an entry.
Fix a bug that the function does not set *devp to NULL on failure, which
the documentation asserts.

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

 drivers/core/device.c |  1 +
 test/dm/of_platdata.c | 81 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

Comments

Simon Glass Oct. 30, 2020, 3:34 a.m. UTC | #1
With of-platdata, the driver_info struct is updated with the device
pointer when it is bound. This makes it easy for a device to be found by
its driver info with the device_get_by_driver_info() function.

Add a test that all devices (except the root device) have such an entry.
Fix a bug that the function does not set *devp to NULL on failure, which
the documentation asserts.

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

 drivers/core/device.c |  1 +
 test/dm/of_platdata.c | 81 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 355dbd147a9..aacc6adf02d 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -745,6 +745,7 @@  int device_get_by_driver_info(const struct driver_info *info,
 	struct udevice *dev;
 
 	dev = info->dev;
+	*devp = NULL;
 
 	return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
 }
diff --git a/test/dm/of_platdata.c b/test/dm/of_platdata.c
index 8763005ea9a..80900e416ba 100644
--- a/test/dm/of_platdata.c
+++ b/test/dm/of_platdata.c
@@ -86,3 +86,84 @@  static int dm_test_of_platdata_props(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_of_platdata_props, UT_TESTF_SCAN_PDATA);
+
+/*
+ * find_driver_info - recursively find the driver_info for a device
+ *
+ * This sets found[idx] to true when it finds the driver_info record for a
+ * device, where idx is the index in the driver_info linker list.
+ *
+ * @uts: Test state
+ * @parent: Parent to search
+ * @found: bool array to update
+ * @return 0 if OK, non-zero on error
+ */
+static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
+			    bool found[])
+{
+	struct udevice *dev;
+
+	/* If not the root device, find the entry that caused it to be bound */
+	if (parent->parent) {
+		const struct driver_info *info =
+			ll_entry_start(struct driver_info, driver_info);
+		const int n_ents =
+			ll_entry_count(struct driver_info, driver_info);
+		const struct driver_info *entry;
+		int idx = -1;
+
+		for (entry = info; entry != info + n_ents; entry++) {
+			if (entry->dev == parent) {
+				idx = entry - info;
+				found[idx] = true;
+				break;
+			}
+		}
+
+		ut_assert(idx != -1);
+	}
+
+	device_foreach_child(dev, parent) {
+		int ret;
+
+		ret = find_driver_info(uts, dev, found);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+/* Check that every device is recorded in its driver_info struct */
+static int dm_test_of_platdata_dev(struct unit_test_state *uts)
+{
+	const struct driver_info *info =
+		ll_entry_start(struct driver_info, driver_info);
+	const int n_ents = ll_entry_count(struct driver_info, driver_info);
+	bool found[n_ents];
+	uint i;
+
+	/* Record the indexes that are found */
+	memset(found, '\0', sizeof(found));
+	ut_assertok(find_driver_info(uts, gd->dm_root, found));
+
+	/* Make sure that the driver entries without devices have no ->dev */
+	for (i = 0; i < n_ents; i++) {
+		const struct driver_info *entry = info + i;
+		struct udevice *dev;
+
+		if (found[i]) {
+			/* Make sure we can find it */
+			ut_assertnonnull(entry->dev);
+			ut_assertok(device_get_by_driver_info(entry, &dev));
+			ut_asserteq_ptr(dev, entry->dev);
+		} else {
+			ut_assertnull(entry->dev);
+			ut_asserteq(-ENOENT,
+				    device_get_by_driver_info(entry, &dev));
+		}
+	}
+
+	return 0;
+}
+DM_TEST(dm_test_of_platdata_dev, UT_TESTF_SCAN_PDATA);