dm: blk: Add a way to obtain a block device from its parent
authorSimon Glass <sjg@chromium.org>
Sat, 27 May 2017 17:37:17 +0000 (11:37 -0600)
committerJaehoon Chung <jh80.chung@samsung.com>
Fri, 9 Jun 2017 11:25:16 +0000 (20:25 +0900)
Many devices support a child block device (e.g. MMC, USB). Add a
convenient way to get this device given the parent device.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>
drivers/block/blk-uclass.c
include/blk.h
test/dm/blk.c

index 614567527182956ba5ff9e260087dfcbc76d5679..23f131b7ad74b0a363038ef5bcd0b667e871c082 100644 (file)
@@ -453,6 +453,32 @@ int blk_prepare_device(struct udevice *dev)
        return 0;
 }
 
+int blk_get_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) {
+               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;
+       }
+       ret = device_probe(dev);
+       if (ret)
+               return ret;
+       *devp = dev;
+
+       return 0;
+}
+
 int blk_find_max_devnum(enum if_type if_type)
 {
        struct udevice *dev;
index a128ee484145f306e1e994249626ef80ac915367..4d60987f61b459c5edd819e3bf2767c19ea6644c 100644 (file)
@@ -616,4 +616,11 @@ ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
  */
 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
 
+/**
+ * blk_get_from_parent() - obtain a block device by looking up its parent
+ *
+ * All devices with
+ */
+int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
+
 #endif
index 5c5eb829a023379f606ed4623973f2cafb93161c..923e8d95f06f8a4f89cfc63ef1e7178d31d49e94 100644 (file)
@@ -150,3 +150,21 @@ static int dm_test_blk_devnum(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_blk_devnum, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that we can get a block from its parent */
+static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
+{
+       struct udevice *dev, *blk;
+
+       ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
+       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_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
+       ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
+
+       return 0;
+}
+DM_TEST(dm_test_blk_get_from_parent, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);