From 0ab5aead0c34237c989aee1c14967e8f723fd812 Mon Sep 17 00:00:00 2001
From: Alan Stern <stern@rowland.harvard.edu>
Date: Mon, 8 Dec 2003 19:44:56 -0800
Subject: [PATCH] [PATCH] USB storage: Update scatter-gather handling in the
 shuttle-usbat

This patch updates the shuttle_usbat driver to use the new scatter-gather
transfer routines.  The small set of changes needed speaks well for the
original organization of the code.

This has not been tested.
---
 drivers/usb/storage/shuttle_usbat.c | 32 ++++++++++++++---------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index f2e6715798c2..f2be592f20c8 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -40,7 +40,6 @@
  */
 
 #include "transport.h"
-#include "raw_bulk.h"
 #include "protocol.h"
 #include "usb.h"
 #include "debug.h"
@@ -529,9 +528,8 @@ int usbat_handle_read10(struct us_data *us,
 	unsigned char *buffer;
 	unsigned int len;
 	unsigned int sector;
-	struct scatterlist *sg = NULL;
-	int sg_segment = 0;
-	int sg_offset = 0;
+	unsigned int sg_segment = 0;
+	unsigned int sg_offset = 0;
 
 	US_DEBUGP("handle_read10: transfersize %d\n",
 		srb->transfersize);
@@ -572,19 +570,20 @@ int usbat_handle_read10(struct us_data *us,
 
 	len = (65535/srb->transfersize) * srb->transfersize;
 	US_DEBUGP("Max read is %d bytes\n", len);
-	buffer = kmalloc(len, GFP_NOIO);
-	if (buffer == NULL) // bloody hell!
-		return USB_STOR_TRANSPORT_FAILED;
+	len = min(len, srb->request_bufflen);
+	if (srb->use_sg) {
+		buffer = kmalloc(len, GFP_NOIO);
+		if (buffer == NULL) // bloody hell!
+			return USB_STOR_TRANSPORT_FAILED;
+	} else
+		buffer = srb->request_buffer;
 	sector = short_pack(data[7+3], data[7+2]);
 	sector <<= 16;
 	sector |= short_pack(data[7+5], data[7+4]);
 	transferred = 0;
 
-	if (srb->use_sg) {
-		sg = (struct scatterlist *)srb->request_buffer;
-		sg_segment = 0; // for keeping track of where we are in
-		sg_offset = 0;  // the scatter/gather list
-	}
+	sg_segment = 0; // for keeping track of where we are in
+	sg_offset = 0;  // the scatter/gather list
 
 	while (transferred != srb->request_bufflen) {
 
@@ -618,10 +617,10 @@ int usbat_handle_read10(struct us_data *us,
 		// Transfer the received data into the srb buffer
 
 		if (srb->use_sg)
-			us_copy_to_sgbuf(buffer, len, sg,
-					 &sg_segment, &sg_offset, srb->use_sg);
+			usb_stor_access_xfer_buf(buffer, len, srb,
+					 &sg_segment, &sg_offset, TO_XFER_BUF);
 		else
-			memcpy(srb->request_buffer+transferred, buffer, len);
+			buffer += len;
 
 		// Update the amount transferred and the sector number
 
@@ -630,7 +629,8 @@ int usbat_handle_read10(struct us_data *us,
 
 	} // while transferred != srb->request_bufflen
 
-	kfree(buffer);
+	if (srb->use_sg)
+		kfree(buffer);
 	return result;
 }
 
-- 
2.30.9