[PATCH 12/17] dm: blk: Tidy up obtaining a block device from its parent
Simon Glass
sjg at chromium.org
Sun Oct 30 02:47:14 CET 2022
This function now finds its block-device child by looking for a child
device of the correct uclass (UCLASS_BLK). It cannot produce a device of
any other type, so drop the superfluous check.
Provide a version which does not probe the device, since that is often
needed when setting up the device's platdata.
Also fix up the function's comment.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
drivers/block/blk-uclass.c | 26 +++++++++++++++-----------
include/blk.h | 28 +++++++++++++++++++++++++++-
test/dm/blk.c | 2 +-
3 files changed, 43 insertions(+), 13 deletions(-)
diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index bcc14a684be..66f9940b648 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -491,24 +491,28 @@ unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
return ops->erase(dev, start, blkcnt);
}
-int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
+int blk_find_from_parent(struct udevice *parent, struct udevice **devp)
{
struct udevice *dev;
- enum uclass_id id;
- int ret;
- device_find_first_child(parent, &dev);
- if (!dev) {
+ if (device_find_first_child_by_uclass(parent, UCLASS_BLK, &dev)) {
debug("%s: No block device found for parent '%s'\n", __func__,
parent->name);
return -ENODEV;
}
- id = device_get_uclass_id(dev);
- if (id != UCLASS_BLK) {
- debug("%s: Incorrect uclass %s for block device '%s'\n",
- __func__, uclass_get_name(id), dev->name);
- return -ENOTBLK;
- }
+ *devp = dev;
+
+ return 0;
+}
+
+int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
+{
+ struct udevice *dev;
+ int ret;
+
+ ret = blk_find_from_parent(parent, &dev);
+ if (ret)
+ return ret;
ret = device_probe(dev);
if (ret)
return ret;
diff --git a/include/blk.h b/include/blk.h
index 58322c39eec..2f5d5121b14 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -416,10 +416,36 @@ int blk_next_free_devnum(enum uclass_id uclass_id);
*/
int blk_select_hwpart(struct udevice *dev, int hwpart);
+/**
+ * blk_find_from_parent() - find a block device by looking up its parent
+ *
+ * All block devices have a parent 'media' device which provides the block
+ * driver for the block device, ensuring that access to the underlying medium
+ * is available.
+ *
+ * The block device is not activated by this function. See
+ * blk_get_from_parent() for that.
+ *
+ * @parent: Media device
+ * @devp: Returns the associated block device, if any
+ * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
+ * UCLASS_BLK child
+ */
+int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
+
/**
* blk_get_from_parent() - obtain a block device by looking up its parent
*
- * All devices with
+ * All block devices have a parent 'media' device which provides the block
+ * driver for the block device, ensuring that access to the underlying medium
+ * is available.
+ *
+ * The block device is probed and ready for use.
+ *
+ * @parent: Media device
+ * @devp: Returns the associated block device, if any
+ * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
+ * UCLASS_BLK child
*/
int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
diff --git a/test/dm/blk.c b/test/dm/blk.c
index 35bd5318f0b..6f0cb98c861 100644
--- a/test/dm/blk.c
+++ b/test/dm/blk.c
@@ -160,7 +160,7 @@ static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
ut_assertok(blk_get_from_parent(dev, &blk));
ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
- ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
+ ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
--
2.38.1.273.g43a17bfeac-goog
More information about the U-Boot
mailing list