[PATCH v3] dm: core: Do not stop uclass iteration on error

Michal Suchanek msuchanek at suse.de
Fri Aug 19 22:23:22 CEST 2022


When probing a device fails NULL pointer is returned, and other devices
cannot be iterated. Skip to next device on error instead.

Fixes: 6494d708bf ("dm: Add base driver model support")
Signed-off-by: Michal Suchanek <msuchanek at suse.de>
---
v2: - Fix up tests
v3: - Fix up API doc
    - Correctly forward error from uclass_get
    - Do not return an error when last device fails to probe
    - Drop redundant initialization
    - Wrap at 80 columns
---
 drivers/core/uclass.c | 32 ++++++++++++++++++++++++--------
 include/dm/uclass.h   | 13 ++++++++-----
 test/dm/test-fdt.c    | 20 ++++++++++++++++----
 3 files changed, 48 insertions(+), 17 deletions(-)

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 08d9ed82de..8f4ecad26c 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -574,16 +574,35 @@ int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
 }
 #endif
 
+/*
+ * Starting from the given device return first device in the uclass that
+ * probes successfully.
+ */
+static int _uclass_next_device(struct udevice *dev, int ret,
+			       struct udevice **devp)
+{
+	if (!dev) {
+		*devp = dev;
+		return ret;
+	}
+	while ((ret = uclass_get_device_tail(dev, ret, devp))) {
+		ret = uclass_find_next_device(&dev);
+		if (!dev) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return 0;
+}
+
 int uclass_first_device(enum uclass_id id, struct udevice **devp)
 {
 	struct udevice *dev;
 	int ret;
 
-	*devp = NULL;
 	ret = uclass_find_first_device(id, &dev);
-	if (!dev)
-		return 0;
-	return uclass_get_device_tail(dev, ret, devp);
+	return _uclass_next_device(dev, ret, devp);
 }
 
 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
@@ -604,11 +623,8 @@ int uclass_next_device(struct udevice **devp)
 	struct udevice *dev = *devp;
 	int ret;
 
-	*devp = NULL;
 	ret = uclass_find_next_device(&dev);
-	if (!dev)
-		return 0;
-	return uclass_get_device_tail(dev, ret, devp);
+	return _uclass_next_device(dev, ret, devp);
 }
 
 int uclass_next_device_err(struct udevice **devp)
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index f6c0110b06..76caa0d2b9 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -320,14 +320,14 @@ int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
  * uclass_first_device() - Get the first device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
+ * Devices that fail to probe are skipped
  *
  * This function is useful to start iterating through a list of devices which
  * are functioning correctly and can be probed.
  *
  * @id: Uclass ID to look up
  * @devp: Returns pointer to the first device in that uclass if no error
- * occurred, or NULL if there is no first device, or an error occurred with
- * that device.
+ * occurred, or NULL if there is no usable device
  * Return: 0 if OK (found or not found), other -ve on error
  */
 int uclass_first_device(enum uclass_id id, struct udevice **devp);
@@ -336,6 +336,7 @@ int uclass_first_device(enum uclass_id id, struct udevice **devp);
  * uclass_first_device_err() - Get the first device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
+ * Devices that fail to probe are skipped
  *
  * @id: Uclass ID to look up
  * @devp: Returns pointer to the first device in that uclass, or NULL if none
@@ -347,14 +348,15 @@ int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
  * uclass_next_device() - Get the next device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
+ * Devices that fail to probe are skipped
  *
  * This function is useful to iterate through a list of devices which
  * are functioning correctly and can be probed.
  *
  * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  * to the next device in the uclass if no error occurred, or NULL if there is
- * no next device, or an error occurred with that next device.
- * Return: 0 if OK (found or not found), other -ve on error
+ * no next device
+ * Return: 0 if OK (found or not found)
  */
 int uclass_next_device(struct udevice **devp);
 
@@ -362,11 +364,12 @@ int uclass_next_device(struct udevice **devp);
  * uclass_next_device_err() - Get the next device in a uclass
  *
  * The device returned is probed if necessary, and ready for use
+ * Devices that fail to probe are skipped
  *
  * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  * to the next device in the uclass if no error occurred, or NULL if
  * there is no next device.
- * Return: 0 if found, -ENODEV if not found, other -ve on error
+ * Return: 0 if found, -ENODEV if not found
  */
 int uclass_next_device_err(struct udevice **devp);
 
diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c
index 6118ad42ca..165b4f5554 100644
--- a/test/dm/test-fdt.c
+++ b/test/dm/test-fdt.c
@@ -417,16 +417,28 @@ static int dm_test_first_next_device(struct unit_test_state *uts)
 	pdata = dev_get_plat(dev);
 	pdata->probe_err = -ENOMEM;
 	device_remove(parent, DM_REMOVE_NORMAL);
-	ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
-	ut_asserteq(-ENOMEM, uclass_next_device(&dev));
-	ut_asserteq_ptr(dev, NULL);
+	for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
+	     dev;
+	     ret = uclass_next_device(&dev)) {
+		count++;
+		parent = dev_get_parent(dev);
+		}
+	ut_assertok(ret);
+	ut_asserteq(3, count);
 
 	/* Now an error on the first one */
 	ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
 	pdata = dev_get_plat(dev);
 	pdata->probe_err = -ENOENT;
 	device_remove(parent, DM_REMOVE_NORMAL);
-	ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
+	for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
+	     dev;
+	     ret = uclass_next_device(&dev)) {
+		count++;
+		parent = dev_get_parent(dev);
+		}
+	ut_assertok(ret);
+	ut_asserteq(2, count);
 
 	return 0;
 }
-- 
2.37.1



More information about the U-Boot mailing list