31004e
From 45b13f9d1572538ea6dc89017767abb9676e9426 Mon Sep 17 00:00:00 2001
a5bd9f
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f
Date: Tue, 19 Mar 2013 08:17:51 +0100
f74b50
Subject: [PATCH 211/482] 	Better estimate the maximum USB transfer size.
a5bd9f
a5bd9f
---
a5bd9f
 ChangeLog                    |  4 ++
a5bd9f
 grub-core/bus/usb/ehci.c     |  4 +-
a5bd9f
 grub-core/bus/usb/ohci.c     |  4 +-
a5bd9f
 grub-core/bus/usb/uhci.c     |  4 +-
a5bd9f
 grub-core/bus/usb/usbtrans.c | 96 +++++++++++++++++++++++++++-----------------
a5bd9f
 include/grub/usb.h           |  7 ++++
a5bd9f
 6 files changed, 80 insertions(+), 39 deletions(-)
a5bd9f
a5bd9f
diff --git a/ChangeLog b/ChangeLog
a5bd9f
index ad84d27..d331cb4 100644
a5bd9f
--- a/ChangeLog
a5bd9f
+++ b/ChangeLog
a5bd9f
@@ -1,3 +1,7 @@
a5bd9f
+2013-03-19  AleŇ° Nesrsta  <starous@volny.cz>
a5bd9f
+
a5bd9f
+	Better estimate the maximum USB transfer size.
a5bd9f
+
a5bd9f
 2013-03-17  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f
 
a5bd9f
 	Resend a packet if we got the wrong buffer in status.
a5bd9f
diff --git a/grub-core/bus/usb/ehci.c b/grub-core/bus/usb/ehci.c
a5bd9f
index 9215866..c60873d 100644
a5bd9f
--- a/grub-core/bus/usb/ehci.c
a5bd9f
+++ b/grub-core/bus/usb/ehci.c
a5bd9f
@@ -1902,7 +1902,9 @@ static struct grub_usb_controller_dev usb_controller = {
a5bd9f
   .cancel_transfer = grub_ehci_cancel_transfer,
a5bd9f
   .hubports = grub_ehci_hubports,
a5bd9f
   .portstatus = grub_ehci_portstatus,
a5bd9f
-  .detect_dev = grub_ehci_detect_dev
a5bd9f
+  .detect_dev = grub_ehci_detect_dev,
a5bd9f
+  /* estimated max. count of TDs for one bulk transfer */
a5bd9f
+  .max_bulk_tds = GRUB_EHCI_N_TD * 3 / 4 
a5bd9f
 };
a5bd9f
 
a5bd9f
 GRUB_MOD_INIT (ehci)
a5bd9f
diff --git a/grub-core/bus/usb/ohci.c b/grub-core/bus/usb/ohci.c
a5bd9f
index 835bb15..2f3fd91 100644
a5bd9f
--- a/grub-core/bus/usb/ohci.c
a5bd9f
+++ b/grub-core/bus/usb/ohci.c
a5bd9f
@@ -1431,7 +1431,9 @@ static struct grub_usb_controller_dev usb_controller =
a5bd9f
   .cancel_transfer = grub_ohci_cancel_transfer,
a5bd9f
   .hubports = grub_ohci_hubports,
a5bd9f
   .portstatus = grub_ohci_portstatus,
a5bd9f
-  .detect_dev = grub_ohci_detect_dev
a5bd9f
+  .detect_dev = grub_ohci_detect_dev,
a5bd9f
+  /* estimated max. count of TDs for one bulk transfer */
a5bd9f
+  .max_bulk_tds = GRUB_OHCI_TDS * 3 / 4
a5bd9f
 };
a5bd9f
 
a5bd9f
 static struct grub_preboot *fini_hnd;
a5bd9f
diff --git a/grub-core/bus/usb/uhci.c b/grub-core/bus/usb/uhci.c
a5bd9f
index 74de392..3639c42 100644
a5bd9f
--- a/grub-core/bus/usb/uhci.c
a5bd9f
+++ b/grub-core/bus/usb/uhci.c
a5bd9f
@@ -823,7 +823,9 @@ static struct grub_usb_controller_dev usb_controller =
a5bd9f
   .cancel_transfer = grub_uhci_cancel_transfer,
a5bd9f
   .hubports = grub_uhci_hubports,
a5bd9f
   .portstatus = grub_uhci_portstatus,
a5bd9f
-  .detect_dev = grub_uhci_detect_dev
a5bd9f
+  .detect_dev = grub_uhci_detect_dev,
a5bd9f
+  /* estimated max. count of TDs for one bulk transfer */
a5bd9f
+  .max_bulk_tds = N_TD * 3 / 4
a5bd9f
 };
a5bd9f
 
a5bd9f
 GRUB_MOD_INIT(uhci)
a5bd9f
diff --git a/grub-core/bus/usb/usbtrans.c b/grub-core/bus/usb/usbtrans.c
a5bd9f
index 154c72d..4c4d8b4 100644
a5bd9f
--- a/grub-core/bus/usb/usbtrans.c
a5bd9f
+++ b/grub-core/bus/usb/usbtrans.c
a5bd9f
@@ -25,6 +25,26 @@
a5bd9f
 #include <grub usbtrans.h="">
a5bd9f
 #include <grub time.h="">
a5bd9f
 
a5bd9f
+
a5bd9f
+static inline unsigned int
a5bd9f
+grub_usb_bulk_maxpacket (grub_usb_device_t dev, int endpoint)
a5bd9f
+{
a5bd9f
+  unsigned int max = 64;
a5bd9f
+
a5bd9f
+  /* Use the maximum packet size given in the endpoint descriptor.  */
a5bd9f
+  if (dev->initialized)
a5bd9f
+    {
a5bd9f
+      struct grub_usb_desc_endp *endpdesc;
a5bd9f
+      endpdesc = grub_usb_get_endpdescriptor (dev, endpoint);
a5bd9f
+
a5bd9f
+      if (endpdesc)
a5bd9f
+	max = endpdesc->maxpacket;
a5bd9f
+    }
a5bd9f
+
a5bd9f
+  return max;
a5bd9f
+}
a5bd9f
+
a5bd9f
+
a5bd9f
 static grub_usb_err_t
a5bd9f
 grub_usb_execute_and_wait_transfer (grub_usb_device_t dev, 
a5bd9f
 				    grub_usb_transfer_t transfer,
a5bd9f
@@ -224,20 +244,6 @@ grub_usb_bulk_setup_readwrite (grub_usb_device_t dev,
a5bd9f
   if (type == GRUB_USB_TRANSFER_TYPE_OUT)
a5bd9f
     grub_memcpy ((char *) data, data_in, size);
a5bd9f
 
a5bd9f
-  /* Use the maximum packet size given in the endpoint descriptor.  */
a5bd9f
-  if (dev->initialized)
a5bd9f
-    {
a5bd9f
-      struct grub_usb_desc_endp *endpdesc;
a5bd9f
-      endpdesc = grub_usb_get_endpdescriptor (dev, endpoint);
a5bd9f
-
a5bd9f
-      if (endpdesc)
a5bd9f
-	max = endpdesc->maxpacket;
a5bd9f
-      else
a5bd9f
-	max = 64;
a5bd9f
-    }
a5bd9f
-  else
a5bd9f
-    max = 64;
a5bd9f
-
a5bd9f
   /* Create a transfer.  */
a5bd9f
   transfer = grub_malloc (sizeof (struct grub_usb_transfer));
a5bd9f
   if (! transfer)
a5bd9f
@@ -246,6 +252,8 @@ grub_usb_bulk_setup_readwrite (grub_usb_device_t dev,
a5bd9f
       return NULL;
a5bd9f
     }
a5bd9f
 
a5bd9f
+  max = grub_usb_bulk_maxpacket (dev, endpoint);
a5bd9f
+
a5bd9f
   datablocks = ((size + max - 1) / max);
a5bd9f
   transfer->transcnt = datablocks;
a5bd9f
   transfer->size = size - 1;
a5bd9f
@@ -333,38 +341,36 @@ grub_usb_bulk_readwrite (grub_usb_device_t dev,
a5bd9f
   return err;
a5bd9f
 }
a5bd9f
 
a5bd9f
-grub_usb_err_t
a5bd9f
-grub_usb_bulk_write (grub_usb_device_t dev,
a5bd9f
-		     int endpoint, grub_size_t size, char *data)
a5bd9f
-{
a5bd9f
-  grub_size_t actual;
a5bd9f
-  grub_usb_err_t err;
a5bd9f
-
a5bd9f
-  err = grub_usb_bulk_readwrite (dev, endpoint, size, data,
a5bd9f
-				 GRUB_USB_TRANSFER_TYPE_OUT, 1000, &actual);
a5bd9f
-  if (!err && actual != size)
a5bd9f
-    err = GRUB_USB_ERR_DATA;
a5bd9f
-  return err;
a5bd9f
-}
a5bd9f
-
a5bd9f
-grub_usb_err_t
a5bd9f
-grub_usb_bulk_read (grub_usb_device_t dev,
a5bd9f
-		    int endpoint, grub_size_t size, char *data)
a5bd9f
+static grub_usb_err_t
a5bd9f
+grub_usb_bulk_readwrite_packetize (grub_usb_device_t dev,
a5bd9f
+				   int endpoint,
a5bd9f
+				   grub_transfer_type_t type,
a5bd9f
+				   grub_size_t size, char *data)
a5bd9f
 {
a5bd9f
   grub_size_t actual, transferred;
a5bd9f
   grub_usb_err_t err;
a5bd9f
   grub_size_t current_size, position;
a5bd9f
+  grub_size_t max_bulk_transfer_len = MAX_USB_TRANSFER_LEN;
a5bd9f
+  grub_size_t max;
a5bd9f
+
a5bd9f
+  if (dev->controller.dev->max_bulk_tds)
a5bd9f
+    {
a5bd9f
+      max = grub_usb_bulk_maxpacket (dev, endpoint);
a5bd9f
+
a5bd9f
+      /* Calculate max. possible length of bulk transfer */
a5bd9f
+      max_bulk_transfer_len = dev->controller.dev->max_bulk_tds * max;
a5bd9f
+    }
a5bd9f
 
a5bd9f
   for (position = 0, transferred = 0;
a5bd9f
-       position < size; position += MAX_USB_TRANSFER_LEN)
a5bd9f
+       position < size; position += max_bulk_transfer_len)
a5bd9f
     {
a5bd9f
       current_size = size - position;
a5bd9f
-      if (current_size >= MAX_USB_TRANSFER_LEN)
a5bd9f
-	current_size = MAX_USB_TRANSFER_LEN;
a5bd9f
+      if (current_size >= max_bulk_transfer_len)
a5bd9f
+	current_size = max_bulk_transfer_len;
a5bd9f
       err = grub_usb_bulk_readwrite (dev, endpoint, current_size,
a5bd9f
-              &data[position], GRUB_USB_TRANSFER_TYPE_IN, 1000, &actual);
a5bd9f
+              &data[position], type, 1000, &actual);
a5bd9f
       transferred += actual;
a5bd9f
-      if (err || (current_size != actual) ) break;
a5bd9f
+      if (err || (current_size != actual)) break;
a5bd9f
     }
a5bd9f
 
a5bd9f
   if (!err && transferred != size)
a5bd9f
@@ -373,6 +379,24 @@ grub_usb_bulk_read (grub_usb_device_t dev,
a5bd9f
 }
a5bd9f
 
a5bd9f
 grub_usb_err_t
a5bd9f
+grub_usb_bulk_write (grub_usb_device_t dev,
a5bd9f
+		     int endpoint, grub_size_t size, char *data)
a5bd9f
+{
a5bd9f
+  return grub_usb_bulk_readwrite_packetize (dev, endpoint,
a5bd9f
+					    GRUB_USB_TRANSFER_TYPE_OUT,
a5bd9f
+					    size, data);
a5bd9f
+}
a5bd9f
+
a5bd9f
+grub_usb_err_t
a5bd9f
+grub_usb_bulk_read (grub_usb_device_t dev,
a5bd9f
+		    int endpoint, grub_size_t size, char *data)
a5bd9f
+{
a5bd9f
+  return grub_usb_bulk_readwrite_packetize (dev, endpoint,
a5bd9f
+					    GRUB_USB_TRANSFER_TYPE_IN,
a5bd9f
+					    size, data);
a5bd9f
+}
a5bd9f
+
a5bd9f
+grub_usb_err_t
a5bd9f
 grub_usb_check_transfer (grub_usb_transfer_t transfer, grub_size_t *actual)
a5bd9f
 {
a5bd9f
   grub_usb_err_t err;
a5bd9f
diff --git a/include/grub/usb.h b/include/grub/usb.h
a5bd9f
index cefa8b6..55f65f7 100644
a5bd9f
--- a/include/grub/usb.h
a5bd9f
+++ b/include/grub/usb.h
a5bd9f
@@ -124,6 +124,13 @@ struct grub_usb_controller_dev
a5bd9f
 
a5bd9f
   /* Per controller flag - port reset pending, don't do another reset */
a5bd9f
   grub_uint64_t pending_reset;
a5bd9f
+
a5bd9f
+  /* Max. number of transfer descriptors used per one bulk transfer */
a5bd9f
+  /* The reason is to prevent "exhausting" of TD by large bulk */
a5bd9f
+  /* transfer - number of TD is limited in USB host driver */
a5bd9f
+  /* Value is calculated/estimated in driver - some TDs should be */
a5bd9f
+  /* reserved for posible concurrent control or "interrupt" transfers */
a5bd9f
+  grub_size_t max_bulk_tds;
a5bd9f
   
a5bd9f
   /* The next host controller.  */
a5bd9f
   struct grub_usb_controller_dev *next;
a5bd9f
-- 
31004e
1.8.2.1
a5bd9f