diff mbox

[U-Boot,v2,41/44] dm: blk: Add a easier way to create a named block device

Message ID 1462124192-8748-42-git-send-email-sjg@chromium.org
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass May 1, 2016, 5:36 p.m. UTC
Add a function that automatically builds the device name given the parent
and a supplied string. Most callers will want to do this, so putting this
functionality in one place makes more sense.

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

Changes in v2: None

 common/usb_storage.c       | 13 +++++--------
 drivers/block/blk-uclass.c | 15 +++++++++++++++
 include/blk.h              | 17 +++++++++++++++++
 3 files changed, 37 insertions(+), 8 deletions(-)

Comments

Simon Glass May 14, 2016, 7:36 p.m. UTC | #1
On 1 May 2016 at 11:36, Simon Glass <sjg@chromium.org> wrote:
> Add a function that automatically builds the device name given the parent
> and a supplied string. Most callers will want to do this, so putting this
> functionality in one place makes more sense.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v2: None
>
>  common/usb_storage.c       | 13 +++++--------
>  drivers/block/blk-uclass.c | 15 +++++++++++++++
>  include/blk.h              | 17 +++++++++++++++++
>  3 files changed, 37 insertions(+), 8 deletions(-)

Applied to u-boot-dm.
diff mbox

Patch

diff --git a/common/usb_storage.c b/common/usb_storage.c
index f2da3a3..7e6e52d 100644
--- a/common/usb_storage.c
+++ b/common/usb_storage.c
@@ -200,7 +200,6 @@  static int usb_stor_probe_device(struct usb_device *udev)
 
 #ifdef CONFIG_BLK
 	struct us_data *data;
-	char dev_name[30], *str;
 	int ret;
 #else
 	int start;
@@ -223,14 +222,12 @@  static int usb_stor_probe_device(struct usb_device *udev)
 	for (lun = 0; lun <= max_lun; lun++) {
 		struct blk_desc *blkdev;
 		struct udevice *dev;
+		char str[10];
 
-		snprintf(dev_name, sizeof(dev_name), "%s.lun%d",
-			 udev->dev->name, lun);
-		str = strdup(dev_name);
-		if (!str)
-			return -ENOMEM;
-		ret = blk_create_device(udev->dev, "usb_storage_blk", str,
-				IF_TYPE_USB, usb_max_devs, 512, 0, &dev);
+		snprintf(str, sizeof(str), "lun%d", lun);
+		ret = blk_create_devicef(udev->dev, "usb_storage_blk", str,
+					 IF_TYPE_USB, usb_max_devs, 512, 0,
+					 &dev);
 		if (ret) {
 			debug("Cannot bind driver\n");
 			return ret;
diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index c947d95..6ecbff0 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -463,6 +463,21 @@  int blk_create_device(struct udevice *parent, const char *drv_name,
 	return 0;
 }
 
+int blk_create_devicef(struct udevice *parent, const char *drv_name,
+		       const char *name, int if_type, int devnum, int blksz,
+		       lbaint_t size, struct udevice **devp)
+{
+	char dev_name[30], *str;
+
+	snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
+	str = strdup(dev_name);
+	if (!str)
+		return -ENOMEM;
+
+	return blk_create_device(parent, drv_name, str, if_type, devnum,
+				 blksz, size, devp);
+}
+
 int blk_unbind_all(int if_type)
 {
 	struct uclass *uc;
diff --git a/include/blk.h b/include/blk.h
index 547c3b4..82b2c1a 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -281,6 +281,23 @@  int blk_create_device(struct udevice *parent, const char *drv_name,
 		      lbaint_t size, struct udevice **devp);
 
 /**
+ * blk_create_devicef() - Create a new named block device
+ *
+ * @parent:	Parent of the new device
+ * @drv_name:	Driver name to use for the block device
+ * @name:	Name for the device (parent name is prepended)
+ * @if_type:	Interface type (enum if_type_t)
+ * @devnum:	Device number, specific to the interface type, or -1 to
+ *		allocate the next available number
+ * @blksz:	Block size of the device in bytes (typically 512)
+ * @size:	Total size of the device in bytes
+ * @devp:	the new device (which has not been probed)
+ */
+int blk_create_devicef(struct udevice *parent, const char *drv_name,
+		       const char *name, int if_type, int devnum, int blksz,
+		       lbaint_t size, struct udevice **devp);
+
+/**
  * blk_prepare_device() - Prepare a block device for use
  *
  * This reads partition information from the device if supported.