[U-Boot] [PATCH v7 2/3] common: usb_storage: Make common function for usb_stor_read/usb_stor_write

Rajesh Bhagat rajesh.bhagat at nxp.com
Thu Jun 16 12:35:55 CEST 2016


Performs code cleanup by making common function for usb_stor_read/
usb_stor_write. Currently only difference in these fucntions is call
to usb_read_10/usb_write_10 scsi commands.

Signed-off-by: Rajesh Bhagat <rajesh.bhagat at nxp.com>
---
Changes in v7:
 - Moves unification of usb_read_10/usb_write_10 to other patch

Changes in v6:
 - Removes USB_STOR_OP_TYPE macro and adds __func__ in debug prints
 - Unifies usb_read_10/usb_write_10 functions to usb_read_write_10

Changes in v5:
 - Converts USB_STOR_OPERATION_FUNC macro to a function
 - Corrects the multi line comment accroding to coding style
 - Renames variable to dev to remove code duplication

Changes in v4:
 - Adds code to make common function for read/write

 common/usb_storage.c | 121 ++++++++++++++-------------------------------------
 1 file changed, 32 insertions(+), 89 deletions(-)

diff --git a/common/usb_storage.c b/common/usb_storage.c
index 98798b5..a7d84bf 100644
--- a/common/usb_storage.c
+++ b/common/usb_storage.c
@@ -1087,11 +1087,13 @@ static void usb_bin_fixup(struct usb_device_descriptor descriptor,
 #endif /* CONFIG_USB_BIN_FIXUP */
 
 #ifdef CONFIG_BLK
-static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
-				   lbaint_t blkcnt, void *buffer)
+static unsigned long usb_stor_read_write(struct udevice *dev, lbaint_t blknr,
+					 lbaint_t blkcnt, const void *buffer,
+					 bool is_write)
 #else
-static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
-				   lbaint_t blkcnt, void *buffer)
+static unsigned long usb_stor_read_write(struct blk_desc *block_dev,
+					 lbaint_t blknr, lbaint_t blkcnt,
+					 const void *buffer, bool is_write)
 #endif
 {
 	lbaint_t start, blks;
@@ -1111,9 +1113,9 @@ static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
 #ifdef CONFIG_BLK
 	block_dev = dev_get_uclass_platdata(dev);
 	udev = dev_get_parent_priv(dev_get_parent(dev));
-	debug("\nusb_read: udev %d\n", block_dev->devnum);
+	debug("\n%s: udev %d\n", __func__, block_dev->devnum);
 #else
-	debug("\nusb_read: udev %d\n", block_dev->devnum);
+	debug("\n%s: udev %d\n", __func__, block_dev->devnum);
 	udev = usb_dev_desc[block_dev->devnum].priv;
 	if (!udev) {
 		debug("%s: No device\n", __func__);
@@ -1128,11 +1130,15 @@ static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
 	start = blknr;
 	blks = blkcnt;
 
-	debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %"
-	      PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr);
+	debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %"
+	      PRIxPTR "\n", __func__, block_dev->devnum, start, blks,
+	      buf_addr);
 
 	do {
-		/* XXX need some comment here */
+		/*
+		 * If read/write fails retry for max retry count else
+		 * return with number of blocks written successfully.
+		 */
 		retry = 2;
 		srb->pdata = (unsigned char *)buf_addr;
 		if (blks > USB_MAX_XFER_BLK)
@@ -1144,8 +1150,8 @@ retry_it:
 			usb_show_progress();
 		srb->datalen = block_dev->blksz * smallblks;
 		srb->pdata = (unsigned char *)buf_addr;
-		if (usb_read_10(srb, ss, start, smallblks)) {
-			debug("Read ERROR\n");
+		if (usb_read_write_10(srb, ss, start, smallblks, is_write)) {
+			debug("%s ERROR\n", __func__);
 			usb_request_sense(srb, ss);
 			if (retry--)
 				goto retry_it;
@@ -1158,9 +1164,9 @@ retry_it:
 	} while (blks != 0);
 	ss->flags &= ~USB_READY;
 
-	debug("usb_read: end startblk " LBAF
+	debug("%s: end startblk " LBAF
 	      ", blccnt %x buffer %" PRIxPTR "\n",
-	      start, smallblks, buf_addr);
+	      __func__, start, smallblks, buf_addr);
 
 	usb_disable_asynch(0); /* asynch transfer allowed */
 	if (blkcnt >= USB_MAX_XFER_BLK)
@@ -1168,90 +1174,27 @@ retry_it:
 	return blkcnt;
 }
 
+
 #ifdef CONFIG_BLK
-static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
-				    lbaint_t blkcnt, const void *buffer)
+static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
+				   lbaint_t blkcnt, void *buffer)
 #else
-static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
-				    lbaint_t blkcnt, const void *buffer)
+static unsigned long usb_stor_read(struct blk_desc *dev, lbaint_t blknr,
+				   lbaint_t blkcnt, void *buffer)
 #endif
 {
-	lbaint_t start, blks;
-	uintptr_t buf_addr;
-	unsigned short smallblks;
-	struct usb_device *udev;
-	struct us_data *ss;
-	int retry;
-	ccb *srb = &usb_ccb;
-#ifdef CONFIG_BLK
-	struct blk_desc *block_dev;
-#endif
-
-	if (blkcnt == 0)
-		return 0;
+	return usb_stor_read_write(dev, blknr, blkcnt, buffer, false);
+}
 
-	/* Setup  device */
 #ifdef CONFIG_BLK
-	block_dev = dev_get_uclass_platdata(dev);
-	udev = dev_get_parent_priv(dev_get_parent(dev));
-	debug("\nusb_read: udev %d\n", block_dev->devnum);
+static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
+				    lbaint_t blkcnt, const void *buffer)
 #else
-	debug("\nusb_read: udev %d\n", block_dev->devnum);
-	udev = usb_dev_desc[block_dev->devnum].priv;
-	if (!udev) {
-		debug("%s: No device\n", __func__);
-		return 0;
-	}
+static unsigned long usb_stor_write(struct blk_desc *dev, lbaint_t blknr,
+				    lbaint_t blkcnt, const void *buffer)
 #endif
-	ss = (struct us_data *)udev->privptr;
-
-	usb_disable_asynch(1); /* asynch transfer not allowed */
-
-	srb->lun = block_dev->lun;
-	buf_addr = (uintptr_t)buffer;
-	start = blknr;
-	blks = blkcnt;
-
-	debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF " buffer %"
-	      PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr);
-
-	do {
-		/* If write fails retry for max retry count else
-		 * return with number of blocks written successfully.
-		 */
-		retry = 2;
-		srb->pdata = (unsigned char *)buf_addr;
-		if (blks > USB_MAX_XFER_BLK)
-			smallblks = USB_MAX_XFER_BLK;
-		else
-			smallblks = (unsigned short) blks;
-retry_it:
-		if (smallblks == USB_MAX_XFER_BLK)
-			usb_show_progress();
-		srb->datalen = block_dev->blksz * smallblks;
-		srb->pdata = (unsigned char *)buf_addr;
-		if (usb_write_10(srb, ss, start, smallblks)) {
-			debug("Write ERROR\n");
-			usb_request_sense(srb, ss);
-			if (retry--)
-				goto retry_it;
-			blkcnt -= blks;
-			break;
-		}
-		start += smallblks;
-		blks -= smallblks;
-		buf_addr += srb->datalen;
-	} while (blks != 0);
-	ss->flags &= ~USB_READY;
-
-	debug("usb_write: end startblk " LBAF ", blccnt %x buffer %"
-	      PRIxPTR "\n", start, smallblks, buf_addr);
-
-	usb_disable_asynch(0); /* asynch transfer allowed */
-	if (blkcnt >= USB_MAX_XFER_BLK)
-		debug("\n");
-	return blkcnt;
-
+{
+	return usb_stor_read_write(dev, blknr, blkcnt, buffer, true);
 }
 
 /* Probe to see if a new device is actually a Storage device */
-- 
2.6.2.198.g614a2ac



More information about the U-Boot mailing list