Revert "usb: usb_storage: do not align length of request for CBW to maxp size"

This reverts commit 806e8f8fcc27e1753947bd9f059ba2316cf8f92a.

To quote Alan Stern:
	The necessity for this patch has been under discussion.

	It turns out the UDC that Mian has been working on and Felipe's
	UDC have contradictory requirements.  Mian's UDC driver wants a
	bulk-OUT transfer length to be shorter than the maxpacket size
	if a short packet is expected, whereas Felipe's UDC hardware
	always needs bulk-OUT transfer lengths to be evenly divisible by
	the maxpacket size.

	Mian has agreed to go back over the driver to resolve this
	conflict.  This means we probably will not want this patch after
	all.  (In fact, we may ultimately decide to change the gadget
	framework to require that bulk-OUT transfer lengths _always_ be
	divisible by the maxpacket size -- only the g_file_storage and
	g_mass_storage gadgets would need to be changed.)

Cc: Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
Cc: Michal Nazarewicz <mina86@mina86.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index aebfb81..fcfc77c 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -497,6 +497,19 @@
 	return (fsg->state > FSG_STATE_IDLE);
 }
 
+/* Make bulk-out requests be divisible by the maxpacket size */
+static void set_bulk_out_req_length(struct fsg_dev *fsg,
+		struct fsg_buffhd *bh, unsigned int length)
+{
+	unsigned int	rem;
+
+	bh->bulk_out_intended_length = length;
+	rem = length % fsg->bulk_out_maxpacket;
+	if (rem > 0)
+		length += fsg->bulk_out_maxpacket - rem;
+	bh->outreq->length = length;
+}
+
 static struct fsg_dev			*the_fsg;
 static struct usb_gadget_driver		fsg_driver;
 
@@ -717,9 +730,10 @@
 	struct fsg_buffhd	*bh = req->context;
 
 	dump_msg(fsg, "bulk-out", req->buf, req->actual);
-	if (req->status || req->actual != req->length)
+	if (req->status || req->actual != bh->bulk_out_intended_length)
 		DBG(fsg, "%s --> %d, %u/%u\n", __func__,
-				req->status, req->actual, req->length);
+				req->status, req->actual,
+				bh->bulk_out_intended_length);
 	if (req->status == -ECONNRESET)		// Request was cancelled
 		usb_ep_fifo_flush(ep);
 
@@ -1335,7 +1349,8 @@
 
 			/* amount is always divisible by 512, hence by
 			 * the bulk-out maxpacket size */
-			bh->outreq->length = amount;
+			bh->outreq->length = bh->bulk_out_intended_length =
+					amount;
 			bh->outreq->short_not_ok = 1;
 			start_transfer(fsg, fsg->bulk_out, bh->outreq,
 					&bh->outreq_busy, &bh->state);
@@ -1964,7 +1979,8 @@
 
 			/* amount is always divisible by 512, hence by
 			 * the bulk-out maxpacket size */
-			bh->outreq->length = amount;
+			bh->outreq->length = bh->bulk_out_intended_length =
+					amount;
 			bh->outreq->short_not_ok = 1;
 			start_transfer(fsg, fsg->bulk_out, bh->outreq,
 					&bh->outreq_busy, &bh->state);
@@ -2643,8 +2659,8 @@
 		}
 
 		/* Queue a request to read a Bulk-only CBW */
-		bh->outreq->length = USB_BULK_CB_WRAP_LEN;
-		bh->outreq->short_not_ok = 0;
+		set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
+		bh->outreq->short_not_ok = 1;
 		start_transfer(fsg, fsg->bulk_out, bh->outreq,
 				&bh->outreq_busy, &bh->state);