]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbHub.c
index 795f545b7712eb59865756eeb6b45abbdd2195d5..c7c27a54819f9609f68625f5d5c8939a0e9df7a1 100644 (file)
@@ -1,47 +1,85 @@
 /** @file\r
 \r
-Copyright (c) 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-  Module Name:\r
-\r
-    UsbHub.c\r
-\r
-  Abstract:\r
-\r
-    Unified interface for RootHub and Hub\r
-\r
-  Revision History\r
+    Unified interface for RootHub and Hub.\r
 \r
+Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "UsbBus.h"\r
 \r
+//\r
+// Array that maps the change bit to feature value which is\r
+// used to clear these change bit. USB HUB API will clear\r
+// these change bit automatically. For non-root hub, these\r
+// bits determine whether hub will report the port in changed\r
+// bit maps.\r
+//\r
+USB_CHANGE_FEATURE_MAP  mHubFeatureMap[] = {\r
+  {USB_PORT_STAT_C_CONNECTION,  EfiUsbPortConnectChange},\r
+  {USB_PORT_STAT_C_ENABLE,      EfiUsbPortEnableChange},\r
+  {USB_PORT_STAT_C_SUSPEND,     EfiUsbPortSuspendChange},\r
+  {USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange},\r
+  {USB_PORT_STAT_C_RESET,       EfiUsbPortResetChange}\r
+};\r
+\r
+USB_CHANGE_FEATURE_MAP  mRootHubFeatureMap[] = {\r
+  {USB_PORT_STAT_C_CONNECTION,  EfiUsbPortConnectChange},\r
+  {USB_PORT_STAT_C_ENABLE,      EfiUsbPortEnableChange},\r
+  {USB_PORT_STAT_C_SUSPEND,     EfiUsbPortSuspendChange},\r
+  {USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange},\r
+  {USB_PORT_STAT_C_RESET,       EfiUsbPortResetChange},\r
+};\r
+\r
 //\r
 // USB hub class specific requests. Although USB hub\r
 // is related to an interface, these requests are sent\r
 // to the control endpoint of the device.\r
 //\r
+/**\r
+  USB hub control transfer to set the hub depth.\r
+\r
+  @param  HubDev                The device of the hub.\r
+  @param  Depth                 The depth to set.\r
+\r
+  @retval EFI_SUCCESS           Depth of the hub is set.\r
+  @retval Others                Failed to set the depth.\r
+\r
+**/\r
+EFI_STATUS\r
+UsbHubCtrlSetHubDepth (\r
+  IN  USB_DEVICE          *HubDev,\r
+  IN  UINT16              Depth\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+\r
+  Status = UsbCtrlRequest (\r
+             HubDev,\r
+             EfiUsbNoData,\r
+             USB_REQ_TYPE_CLASS,\r
+             USB_HUB_TARGET_HUB,\r
+             USB_HUB_REQ_SET_DEPTH,\r
+             Depth,\r
+             0,\r
+             NULL,\r
+             0\r
+             );\r
 \r
+  return Status;\r
+}\r
 \r
 /**\r
-  USB hub control transfer to clear the hub feature\r
+  USB hub control transfer to clear the hub feature.\r
 \r
-  @param  HubDev                The device of the hub\r
-  @param  Feature               The feature to clear\r
+  @param  HubDev                The device of the hub.\r
+  @param  Feature               The feature to clear.\r
 \r
-  @retval EFI_SUCCESS           Feature of the hub is cleared\r
-  @retval Others                Failed to clear the feature\r
+  @retval EFI_SUCCESS           Feature of the hub is cleared.\r
+  @retval Others                Failed to clear the feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlClearHubFeature (\r
   IN USB_DEVICE           *HubDev,\r
@@ -67,17 +105,16 @@ UsbHubCtrlClearHubFeature (
 \r
 \r
 /**\r
-  Clear the feature of the device's port\r
+  Clear the feature of the device's port.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  Port                  The port to clear feature\r
-  @param  Feature               The feature to clear\r
+  @param  HubDev                The hub device.\r
+  @param  Port                  The port to clear feature.\r
+  @param  Feature               The feature to clear.\r
 \r
   @retval EFI_SUCCESS           The feature of the port is cleared.\r
   @retval Others                Failed to clear the feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlClearPortFeature (\r
   IN USB_DEVICE           *HubDev,\r
@@ -107,7 +144,6 @@ UsbHubCtrlClearPortFeature (
 }\r
 \r
 \r
-\r
 /**\r
   Clear the transaction translate buffer if full/low\r
   speed control/bulk transfer failed and the transfer\r
@@ -115,14 +151,14 @@ UsbHubCtrlClearPortFeature (
   buffer of transaction translator, not that of the\r
   parent.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  Port                  The port of the hub\r
-  @param  DevAddr               Address of the failed transaction\r
-  @param  EpNum                 The endpoint number of the failed transaction\r
-  @param  EpType                The type of failed transaction\r
+  @param  HubDev                The hub device.\r
+  @param  Port                  The port of the hub.\r
+  @param  DevAddr               Address of the failed transaction.\r
+  @param  EpNum                 The endpoint number of the failed transaction.\r
+  @param  EpType                The type of failed transaction.\r
 \r
-  @retval EFI_SUCCESS           The TT buffer is cleared\r
-  @retval Others                Failed to clear the TT buffer\r
+  @retval EFI_SUCCESS           The TT buffer is cleared.\r
+  @retval Others                Failed to clear the TT buffer.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -158,19 +194,17 @@ UsbHubCtrlClearTTBuffer (
   return Status;\r
 }\r
 \r
-\r
 /**\r
-  Usb hub control transfer to get the hub descriptor\r
+  Usb hub control transfer to get the (super speed) hub descriptor.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  Buf                   The buffer to hold the descriptor\r
-  @param  Len                   The length to retrieve\r
+  @param  HubDev                The hub device.\r
+  @param  Buf                   The buffer to hold the descriptor.\r
+  @param  Len                   The length to retrieve.\r
 \r
-  @retval EFI_SUCCESS           The hub descriptor is retrieved\r
-  @retval Others                Failed to retrieve the hub descriptor\r
+  @retval EFI_SUCCESS           The hub descriptor is retrieved.\r
+  @retval Others                Failed to retrieve the hub descriptor.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlGetHubDesc (\r
   IN  USB_DEVICE          *HubDev,\r
@@ -179,6 +213,11 @@ UsbHubCtrlGetHubDesc (
   )\r
 {\r
   EFI_STATUS              Status;\r
+  UINT8                   DescType;\r
+\r
+  DescType = (HubDev->Speed == EFI_USB_SPEED_SUPER) ?\r
+             USB_DESC_TYPE_HUB_SUPER_SPEED :\r
+             USB_DESC_TYPE_HUB;\r
 \r
   Status = UsbCtrlRequest (\r
              HubDev,\r
@@ -186,7 +225,7 @@ UsbHubCtrlGetHubDesc (
              USB_REQ_TYPE_CLASS,\r
              USB_HUB_TARGET_HUB,\r
              USB_HUB_REQ_GET_DESC,\r
-             (UINT16) (USB_DESC_TYPE_HUB << 8),\r
+             (UINT16) (DescType << 8),\r
              0,\r
              Buf,\r
              Len\r
@@ -197,16 +236,15 @@ UsbHubCtrlGetHubDesc (
 \r
 \r
 /**\r
-  Usb hub control transfer to get the hub status\r
+  Usb hub control transfer to get the hub status.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  State                 The variable to return the status\r
+  @param  HubDev                The hub device.\r
+  @param  State                 The variable to return the status.\r
 \r
-  @retval EFI_SUCCESS           The hub status is returned in State\r
-  @retval Others                Failed to get the hub status\r
+  @retval EFI_SUCCESS           The hub status is returned in State.\r
+  @retval Others                Failed to get the hub status.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlGetHubStatus (\r
   IN  USB_DEVICE          *HubDev,\r
@@ -232,17 +270,16 @@ UsbHubCtrlGetHubStatus (
 \r
 \r
 /**\r
-  Usb hub control transfer to get the port status\r
+  Usb hub control transfer to get the port status.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  Port                  The port of the hub\r
-  @param  State                 Variable to return the hub port state\r
+  @param  HubDev                The hub device.\r
+  @param  Port                  The port of the hub.\r
+  @param  State                 Variable to return the hub port state.\r
 \r
-  @retval EFI_SUCCESS           The port state is returned in State\r
-  @retval Others                Failed to retrive the port state\r
+  @retval EFI_SUCCESS           The port state is returned in State.\r
+  @retval Others                Failed to retrieve the port state.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlGetPortStatus (\r
   IN  USB_DEVICE          *HubDev,\r
@@ -275,85 +312,16 @@ UsbHubCtrlGetPortStatus (
 \r
 \r
 /**\r
-  Usb hub control transfer to reset the TT (Transaction Transaltor)\r
-\r
-  @param  HubDev                The hub device\r
-  @param  Port                  The port of the hub\r
-\r
-  @retval EFI_SUCCESS           The TT of the hub is reset\r
-  @retval Others                Failed to reset the port\r
-\r
-**/\r
-EFI_STATUS\r
-UsbHubCtrlResetTT (\r
-  IN  USB_DEVICE          *HubDev,\r
-  IN  UINT8               Port\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-\r
-  Status = UsbCtrlRequest (\r
-             HubDev,\r
-             EfiUsbNoData,\r
-             USB_REQ_TYPE_CLASS,\r
-             USB_HUB_TARGET_HUB,\r
-             USB_HUB_REQ_RESET_TT,\r
-             0,\r
-             (UINT16) (Port + 1),\r
-             NULL,\r
-             0\r
-             );\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Usb hub control transfer to set the hub feature\r
-\r
-  @param  HubDev                The hub device\r
-  @param  Feature               The feature to set\r
-\r
-  @retval EFI_SUCESS            The feature is set for the hub\r
-  @retval Others                Failed to set the feature\r
-\r
-**/\r
-EFI_STATUS\r
-UsbHubCtrlSetHubFeature (\r
-  IN  USB_DEVICE          *HubDev,\r
-  IN  UINT8               Feature\r
-  )\r
-{\r
-  EFI_STATUS              Status;\r
-\r
-  Status = UsbCtrlRequest (\r
-             HubDev,\r
-             EfiUsbNoData,\r
-             USB_REQ_TYPE_CLASS,\r
-             USB_HUB_TARGET_HUB,\r
-             USB_HUB_REQ_SET_FEATURE,\r
-             Feature,\r
-             0,\r
-             NULL,\r
-             0\r
-             );\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Usb hub control transfer to set the port feature\r
+  Usb hub control transfer to set the port feature.\r
 \r
-  @param  HubDev                The Usb hub device\r
-  @param  Port                  The Usb port to set feature for\r
-  @param  Feature               The feature to set\r
+  @param  HubDev                The Usb hub device.\r
+  @param  Port                  The Usb port to set feature for.\r
+  @param  Feature               The feature to set.\r
 \r
-  @retval EFI_SUCCESS           The feature is set for the port\r
-  @retval Others                Failed to set the feature\r
+  @retval EFI_SUCCESS           The feature is set for the port.\r
+  @retval Others                Failed to set the feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubCtrlSetPortFeature (\r
   IN USB_DEVICE           *HubDev,\r
@@ -386,16 +354,15 @@ UsbHubCtrlSetPortFeature (
 /**\r
   Read the whole usb hub descriptor. It is necessary\r
   to do it in two steps because hub descriptor is of\r
-  variable length\r
+  variable length.\r
 \r
-  @param  HubDev                The hub device\r
-  @param  HubDesc               The variable to return the descriptor\r
+  @param  HubDev                The hub device.\r
+  @param  HubDesc               The variable to return the descriptor.\r
 \r
-  @retval EFI_SUCCESS           The hub descriptor is read\r
-  @retval Others                Failed to read the hub descriptor\r
+  @retval EFI_SUCCESS           The hub descriptor is read.\r
+  @retval Others                Failed to read the hub descriptor.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubReadDesc (\r
   IN  USB_DEVICE              *HubDev,\r
@@ -416,9 +383,7 @@ UsbHubReadDesc (
   //\r
   // Get the whole hub descriptor\r
   //\r
-  Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);\r
-\r
-  return Status;\r
+  return UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);\r
 }\r
 \r
 \r
@@ -427,10 +392,10 @@ UsbHubReadDesc (
   Ack the hub change bits. If these bits are not ACKed, Hub will\r
   always return changed bit map from its interrupt endpoint.\r
 \r
-  @param  HubDev                The hub device\r
+  @param  HubDev                The hub device.\r
 \r
-  @retval EFI_SUCCESS           The hub change status is ACKed\r
-  @retval Others                Failed to ACK the hub status\r
+  @retval EFI_SUCCESS           The hub change status is ACKed.\r
+  @retval Others                Failed to ACK the hub status.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -462,10 +427,10 @@ UsbHubAckHubStatus (
 /**\r
   Test whether the interface is a hub interface.\r
 \r
-  @param  UsbIf                 The interface to test\r
+  @param  UsbIf                 The interface to test.\r
 \r
-  @retval TRUE                  The interface is a hub interface\r
-  @retval FALSE                 The interface isn't a hub interface\r
+  @retval TRUE                  The interface is a hub interface.\r
+  @retval FALSE                 The interface isn't a hub interface.\r
 \r
 **/\r
 BOOLEAN\r
@@ -496,17 +461,17 @@ UsbIsHubInterface (
   interrupt endpoint. It is called periodically by\r
   the underlying host controller.\r
 \r
-  @param  Data                  The data read\r
-  @param  DataLength            The length of the data read\r
-  @param  Context               The context\r
-  @param  Result                The result of the last interrupt transfer\r
+  @param  Data                  The data read.\r
+  @param  DataLength            The length of the data read.\r
+  @param  Context               The context.\r
+  @param  Result                The result of the last interrupt transfer.\r
 \r
-  @retval EFI_SUCCESS           The process is OK\r
-  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource\r
+  @retval EFI_SUCCESS           The process is OK.\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 UsbOnHubInterrupt (\r
   IN  VOID                *Data,\r
   IN  UINTN               DataLength,\r
@@ -594,57 +559,33 @@ UsbOnHubInterrupt (
   return EFI_SUCCESS;\r
 }\r
 \r
-//\r
-// Array that maps the change bit to feature value which is\r
-// used to clear these change bit. USB HUB API will clear\r
-// these change bit automatically. For non-root hub, these\r
-// bits determine whether hub will report the port in changed\r
-// bit maps.\r
-//\r
-#define USB_HUB_MAP_SIZE  5\r
-\r
-USB_CHANGE_FEATURE_MAP  mHubFeatureMap[USB_HUB_MAP_SIZE] = {\r
-  {USB_PORT_STAT_C_CONNECTION,  USB_HUB_C_PORT_CONNECT},\r
-  {USB_PORT_STAT_C_ENABLE,      USB_HUB_C_PORT_ENABLE},\r
-  {USB_PORT_STAT_C_SUSPEND,     USB_HUB_C_PORT_SUSPEND},\r
-  {USB_PORT_STAT_C_OVERCURRENT, USB_HUB_C_PORT_OVER_CURRENT},\r
-  {USB_PORT_STAT_C_RESET,       USB_HUB_C_PORT_RESET},\r
-};\r
-\r
-#define USB_ROOT_HUB_MAP_SIZE 5\r
-\r
-USB_CHANGE_FEATURE_MAP  mRootHubFeatureMap[USB_ROOT_HUB_MAP_SIZE] = {\r
-  {USB_PORT_STAT_C_CONNECTION,  EfiUsbPortConnectChange},\r
-  {USB_PORT_STAT_C_ENABLE,      EfiUsbPortEnableChange},\r
-  {USB_PORT_STAT_C_SUSPEND,     EfiUsbPortSuspendChange},\r
-  {USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange},\r
-  {USB_PORT_STAT_C_RESET,       EfiUsbPortResetChange},\r
-};\r
 \r
 \r
 \r
 /**\r
-  Initialize the device for a non-root hub\r
+  Initialize the device for a non-root hub.\r
 \r
-  @param  HubIf                 The USB hub interface\r
+  @param  HubIf                 The USB hub interface.\r
 \r
-  @retval EFI_SUCCESS           The hub is initialized\r
-  @retval EFI_DEVICE_ERROR      Failed to initialize the hub\r
+  @retval EFI_SUCCESS           The hub is initialized.\r
+  @retval EFI_DEVICE_ERROR      Failed to initialize the hub.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubInit (\r
   IN USB_INTERFACE        *HubIf\r
   )\r
 {\r
-  EFI_USB_HUB_DESCRIPTOR  HubDesc;\r
+  UINT8                   HubDescBuffer[256];\r
+  EFI_USB_HUB_DESCRIPTOR  *HubDesc;\r
   USB_ENDPOINT_DESC       *EpDesc;\r
   USB_INTERFACE_SETTING   *Setting;\r
   EFI_USB_IO_PROTOCOL     *UsbIo;\r
   USB_DEVICE              *HubDev;\r
   EFI_STATUS              Status;\r
   UINT8                   Index;\r
+  UINT8                   NumEndpoints;\r
+  UINT16                  Depth;\r
 \r
   //\r
   // Locate the interrupt endpoint for port change map\r
@@ -653,8 +594,9 @@ UsbHubInit (
   Setting       = HubIf->IfSetting;\r
   HubDev        = HubIf->Device;\r
   EpDesc        = NULL;\r
+  NumEndpoints  = Setting->Desc.NumEndpoints;\r
 \r
-  for (Index = 0; Index < Setting->Desc.NumEndpoints; Index++) {\r
+  for (Index = 0; Index < NumEndpoints; Index++) {\r
     ASSERT ((Setting->Endpoints != NULL) && (Setting->Endpoints[Index] != NULL));\r
 \r
     EpDesc = Setting->Endpoints[Index];\r
@@ -665,22 +607,63 @@ UsbHubInit (
     }\r
   }\r
 \r
-  if (Index == Setting->Desc.NumEndpoints) {\r
+  if (Index == NumEndpoints) {\r
     DEBUG (( EFI_D_ERROR, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev->Address));\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  Status = UsbHubReadDesc (HubDev, &HubDesc);\r
+  //\r
+  // The length field of descriptor is UINT8 type, so the buffer\r
+  // with 256 bytes is enough to hold the descriptor data.\r
+  //\r
+  HubDesc = (EFI_USB_HUB_DESCRIPTOR *) HubDescBuffer;\r
+  Status = UsbHubReadDesc (HubDev, HubDesc);\r
 \r
   if (EFI_ERROR (Status)) {\r
     DEBUG (( EFI_D_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));\r
     return Status;\r
   }\r
 \r
-  HubIf->NumOfPort = HubDesc.NumPorts;\r
+  HubIf->NumOfPort = HubDesc->NumPorts;\r
 \r
   DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address,HubIf->NumOfPort));\r
 \r
+  //\r
+  // OK, set IsHub to TRUE. Now usb bus can handle this device\r
+  // as a working HUB. If failed eariler, bus driver will not\r
+  // recognize it as a hub. Other parts of the bus should be able\r
+  // to work.\r
+  //\r
+  HubIf->IsHub  = TRUE;\r
+  HubIf->HubApi = &mUsbHubApi;\r
+  HubIf->HubEp  = EpDesc;\r
+\r
+  if (HubIf->Device->Speed == EFI_USB_SPEED_SUPER) {\r
+    Depth = (UINT16)(HubIf->Device->Tier - 1);\r
+    DEBUG ((EFI_D_INFO, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth));\r
+    UsbHubCtrlSetHubDepth (HubIf->Device, Depth);\r
+\r
+    for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
+      UsbHubCtrlSetPortFeature (HubIf->Device, Index, USB_HUB_PORT_REMOTE_WAKE_MASK);\r
+    }\r
+  } else {\r
+    //\r
+    // Feed power to all the hub ports. It should be ok\r
+    // for both gang/individual powered hubs.\r
+    //\r
+    for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
+      UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);\r
+    }\r
+\r
+    //\r
+    // Update for the usb hub has no power on delay requirement\r
+    //\r
+    if (HubDesc->PwrOn2PwrGood > 0) {\r
+      gBS->Stall (HubDesc->PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);\r
+    }\r
+    UsbHubAckHubStatus (HubIf->Device);\r
+  }\r
+\r
   //\r
   // Create an event to enumerate the hub's port. On\r
   //\r
@@ -727,27 +710,6 @@ UsbHubInit (
     return Status;\r
   }\r
 \r
-  //\r
-  // OK, set IsHub to TRUE. Now usb bus can handle this device\r
-  // as a working HUB. If failed eariler, bus driver will not\r
-  // recognize it as a hub. Other parts of the bus should be able\r
-  // to work.\r
-  //\r
-  HubIf->IsHub  = TRUE;\r
-  HubIf->HubApi = &mUsbHubApi;\r
-  HubIf->HubEp  = EpDesc;\r
-\r
-  //\r
-  // Feed power to all the hub ports. It should be ok\r
-  // for both gang/individual powered hubs.\r
-  //\r
-  for (Index = 0; Index < HubDesc.NumPorts; Index++) {\r
-    UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);\r
-  }\r
-\r
-  gBS->Stall (HubDesc.PwrOn2PwrGood * 2 * USB_STALL_1_MS);\r
-  UsbHubAckHubStatus (HubIf->Device);\r
-\r
   DEBUG (( EFI_D_INFO, "UsbHubInit: hub %d initialized\n", HubDev->Address));\r
   return Status;\r
 }\r
@@ -760,15 +722,14 @@ UsbHubInit (
   the port changes in PortState. Bus enumeration code\r
   doesn't need to ACK the port change bits.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The port of the hub to get state\r
-  @param  PortState             Variable to return the port state\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The port of the hub to get state.\r
+  @param  PortState             Variable to return the port state.\r
 \r
-  @retval EFI_SUCCESS           The port status is successfully returned\r
-  @retval Others                Failed to return the status\r
+  @retval EFI_SUCCESS           The port status is successfully returned.\r
+  @retval Others                Failed to return the status.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubGetPortStatus (\r
   IN  USB_INTERFACE       *HubIf,\r
@@ -788,13 +749,10 @@ UsbHubGetPortStatus (
 /**\r
   Clear the port change status.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The hub port\r
-\r
-  @return None\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The hub port.\r
 \r
 **/\r
-STATIC\r
 VOID\r
 UsbHubClearPortChange (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -818,11 +776,11 @@ UsbHubClearPortChange (
   // It may lead to extra port state report. USB bus should\r
   // be able to handle this.\r
   //\r
-  for (Index = 0; Index < USB_HUB_MAP_SIZE; Index++) {\r
+  for (Index = 0; Index < ARRAY_SIZE (mHubFeatureMap); Index++) {\r
     Map = &mHubFeatureMap[Index];\r
 \r
     if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {\r
-      UsbHubCtrlClearPortFeature (HubIf->Device, Port, Map->Feature);\r
+      UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT16) Map->Feature);\r
     }\r
   }\r
 }\r
@@ -830,17 +788,16 @@ UsbHubClearPortChange (
 \r
 \r
 /**\r
-  Function to set the port feature for non-root hub\r
+  Function to set the port feature for non-root hub.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The port of the hub\r
-  @param  Feature               The feature of the port to set\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The port of the hub.\r
+  @param  Feature               The feature of the port to set.\r
 \r
-  @retval EFI_SUCCESS           The hub port feature is set\r
-  @retval Others                Failed to set the port feature\r
+  @retval EFI_SUCCESS           The hub port feature is set.\r
+  @retval Others                Failed to set the port feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubSetPortFeature (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -857,17 +814,16 @@ UsbHubSetPortFeature (
 \r
 \r
 /**\r
-  Interface function to clear the port feature for non-root hub\r
+  Interface function to clear the port feature for non-root hub.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The port of the hub to clear feature for\r
-  @param  Feature               The feature to clear\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The port of the hub to clear feature for.\r
+  @param  Feature               The feature to clear.\r
 \r
-  @retval EFI_SUCCESS           The port feature is cleared\r
-  @retval Others                Failed to clear the port feature\r
+  @retval EFI_SUCCESS           The port feature is cleared.\r
+  @retval Others                Failed to clear the port feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubClearPortFeature (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -884,17 +840,16 @@ UsbHubClearPortFeature (
 \r
 \r
 /**\r
-  Interface funtion to reset the port\r
+  Interface function to reset the port.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The port to reset\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The port to reset.\r
 \r
-  @retval EFI_SUCCESS           The hub port is reset\r
-  @retval EFI_TIMEOUT           Failed to reset the port in time\r
-  @retval Others                Failed to reset the port\r
+  @retval EFI_SUCCESS           The hub port is reset.\r
+  @retval EFI_TIMEOUT           Failed to reset the port in time.\r
+  @retval Others                Failed to reset the port.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubResetPort (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -912,26 +867,30 @@ UsbHubResetPort (
   }\r
 \r
   //\r
-  // Drive the reset signal for at least 10ms. Check USB 2.0 Spec\r
+  // Drive the reset signal for worst 20ms. Check USB 2.0 Spec\r
   // section 7.1.7.5 for timing requirements.\r
   //\r
-  gBS->Stall (20 * USB_STALL_1_MS);\r
+  gBS->Stall (USB_SET_PORT_RESET_STALL);\r
 \r
   //\r
-  // USB hub will clear RESET bit if reset is actually finished.\r
+  // Check USB_PORT_STAT_C_RESET bit to see if the resetting state is done.\r
   //\r
   ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));\r
 \r
-  for (Index = 0; Index < 20; Index++) {\r
+  for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {\r
     Status = UsbHubGetPortStatus (HubIf, Port, &PortState);\r
 \r
-    if (!EFI_ERROR (Status) &&\r
-        !USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
 \r
+    if (!EFI_ERROR (Status) &&\r
+        USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET)) {\r
+      gBS->Stall (USB_SET_PORT_RECOVERY_STALL);\r
       return EFI_SUCCESS;\r
     }\r
 \r
-    gBS->Stall (5 * USB_STALL_1_MS);\r
+    gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);\r
   }\r
 \r
   return EFI_TIMEOUT;\r
@@ -939,14 +898,13 @@ UsbHubResetPort (
 \r
 \r
 /**\r
-  Release the hub's control of the interface\r
+  Release the hub's control of the interface.\r
 \r
-  @param  HubIf                 The hub interface\r
+  @param  HubIf                 The hub interface.\r
 \r
-  @retval EFI_SUCCESS           The interface is release of hub control\r
+  @retval EFI_SUCCESS           The interface is release of hub control.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbHubRelease (\r
   IN USB_INTERFACE        *HubIf\r
@@ -984,15 +942,14 @@ UsbHubRelease (
 \r
 \r
 /**\r
-  Initialize the interface for root hub\r
+  Initialize the interface for root hub.\r
 \r
-  @param  HubIf                 The root hub interface\r
+  @param  HubIf                 The root hub interface.\r
 \r
-  @retval EFI_SUCCESS           The interface is initialied for root hub\r
-  @retval Others                Failed to initialize the hub\r
+  @retval EFI_SUCCESS           The interface is initialized for root hub.\r
+  @retval Others                Failed to initialize the hub.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubInit (\r
   IN USB_INTERFACE        *HubIf\r
@@ -1009,7 +966,7 @@ UsbRootHubInit (
     return Status;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbRootHubInit: root hub %x - max speed %d, %d ports\n",\r
+  DEBUG (( EFI_D_INFO, "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",\r
               HubIf, MaxSpeed, NumOfPort));\r
 \r
   HubIf->IsHub      = TRUE;\r
@@ -1054,22 +1011,20 @@ UsbRootHubInit (
 }\r
 \r
 \r
-\r
 /**\r
   Get the port status. This function is required to\r
   ACK the port change bits although it will return\r
   the port changes in PortState. Bus enumeration code\r
   doesn't need to ACK the port change bits.\r
 \r
-  @param  HubIf                 The root hub interface\r
-  @param  Port                  The root hub port to get the state\r
-  @param  PortState             Variable to return the port state\r
+  @param  HubIf                 The root hub interface.\r
+  @param  Port                  The root hub port to get the state.\r
+  @param  PortState             Variable to return the port state.\r
 \r
-  @retval EFI_SUCCESS           The port state is returned\r
-  @retval Others                Failed to retrieve the port state\r
+  @retval EFI_SUCCESS           The port state is returned.\r
+  @retval Others                Failed to retrieve the port state.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubGetPortStatus (\r
   IN  USB_INTERFACE       *HubIf,\r
@@ -1090,14 +1045,10 @@ UsbRootHubGetPortStatus (
 /**\r
   Clear the port change status.\r
 \r
-  @param  HubIf                 The root hub interface\r
-  @param  Port                  The root hub port\r
-\r
-  @retval EFI_SUCCESS           The port state is returned\r
-  @retval Others                Failed to retrieve the port state\r
+  @param  HubIf                 The root hub interface.\r
+  @param  Port                  The root hub port.\r
 \r
 **/\r
-STATIC\r
 VOID\r
 UsbRootHubClearPortChange (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -1121,7 +1072,7 @@ UsbRootHubClearPortChange (
   // It may lead to extra port state report. USB bus should\r
   // be able to handle this.\r
   //\r
-  for (Index = 0; Index < USB_ROOT_HUB_MAP_SIZE; Index++) {\r
+  for (Index = 0; Index < ARRAY_SIZE (mRootHubFeatureMap); Index++) {\r
     Map = &mRootHubFeatureMap[Index];\r
 \r
     if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {\r
@@ -1131,19 +1082,17 @@ UsbRootHubClearPortChange (
 }\r
 \r
 \r
-\r
 /**\r
-  Set the root hub port feature\r
+  Set the root hub port feature.\r
 \r
-  @param  HubIf                 The Usb hub interface\r
-  @param  Port                  The hub port\r
-  @param  Feature               The feature to set\r
+  @param  HubIf                 The Usb hub interface.\r
+  @param  Port                  The hub port.\r
+  @param  Feature               The feature to set.\r
 \r
-  @retval EFI_SUCCESS           The root hub port is set with the feature\r
-  @retval Others                Failed to set the feature\r
+  @retval EFI_SUCCESS           The root hub port is set with the feature.\r
+  @retval Others                Failed to set the feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubSetPortFeature (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -1160,17 +1109,16 @@ UsbRootHubSetPortFeature (
 \r
 \r
 /**\r
-  Clear the root hub port feature\r
+  Clear the root hub port feature.\r
 \r
-  @param  HubIf                 The root hub interface\r
-  @param  Port                  The root hub port\r
-  @param  Feature               The feature to clear\r
+  @param  HubIf                 The root hub interface.\r
+  @param  Port                  The root hub port.\r
+  @param  Feature               The feature to clear.\r
 \r
-  @retval EFI_SUCCESS           The root hub port is cleared of the feature\r
-  @retval Others                Failed to clear the feature\r
+  @retval EFI_SUCCESS           The root hub port is cleared of the feature.\r
+  @retval Others                Failed to clear the feature.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubClearPortFeature (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -1187,19 +1135,18 @@ UsbRootHubClearPortFeature (
 \r
 \r
 /**\r
-  Interface funtion to reset the root hub port\r
+  Interface function to reset the root hub port.\r
 \r
-  @param  RootIf                The root hub interface\r
-  @param  Port                  The port to reset\r
+  @param  RootIf                The root hub interface.\r
+  @param  Port                  The port to reset.\r
 \r
-  @retval EFI_SUCCESS           The hub port is reset\r
-  @retval EFI_TIMEOUT           Failed to reset the port in time\r
-  @retval EFI_NOT_FOUND         The low/full speed device connected to high  speed\r
-                                root hub is released to the companion UHCI\r
-  @retval Others                Failed to reset the port\r
+  @retval EFI_SUCCESS           The hub port is reset.\r
+  @retval EFI_TIMEOUT           Failed to reset the port in time.\r
+  @retval EFI_NOT_FOUND         The low/full speed device connected to high  speed.\r
+                                root hub is released to the companion UHCI.\r
+  @retval Others                Failed to reset the port.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubResetPort (\r
   IN USB_INTERFACE        *RootIf,\r
@@ -1217,6 +1164,7 @@ UsbRootHubResetPort (
   // should be handled in the EHCI driver.\r
   //\r
   Bus     = RootIf->Device->Bus;\r
+\r
   Status  = UsbHcSetRootHubPortFeature (Bus, Port, EfiUsbPortReset);\r
 \r
   if (EFI_ERROR (Status)) {\r
@@ -1228,7 +1176,7 @@ UsbRootHubResetPort (
   // Drive the reset signal for at least 50ms. Check USB 2.0 Spec\r
   // section 7.1.7.5 for timing requirements.\r
   //\r
-  gBS->Stall (50 * USB_STALL_1_MS);\r
+  gBS->Stall (USB_SET_ROOT_PORT_RESET_STALL);\r
 \r
   Status = UsbHcClearRootHubPortFeature (Bus, Port, EfiUsbPortReset);\r
 \r
@@ -1237,7 +1185,7 @@ UsbRootHubResetPort (
     return Status;\r
   }\r
 \r
-  gBS->Stall (USB_STALL_1_MS);\r
+  gBS->Stall (USB_CLR_ROOT_PORT_RESET_STALL);\r
 \r
   //\r
   // USB host controller won't clear the RESET bit until\r
@@ -1245,7 +1193,7 @@ UsbRootHubResetPort (
   //\r
   ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));\r
 \r
-  for (Index = 0; Index < USB_HUB_LOOP; Index++) {\r
+  for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {\r
     Status = UsbHcGetRootHubPortStatus (Bus, Port, &PortState);\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -1256,11 +1204,11 @@ UsbRootHubResetPort (
       break;\r
     }\r
 \r
-    gBS->Stall (10 * USB_STALL_1_MS);\r
+    gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);\r
   }\r
 \r
-  if (Index == USB_HUB_LOOP) {\r
-    DEBUG (( EFI_D_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));\r
+  if (Index == USB_WAIT_PORT_STS_CHANGE_LOOP) {\r
+    DEBUG ((EFI_D_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));\r
     return EFI_TIMEOUT;\r
   }\r
 \r
@@ -1286,7 +1234,7 @@ UsbRootHubResetPort (
         return Status;\r
       }\r
 \r
-      gBS->Stall (20 * USB_STALL_1_MS);\r
+      gBS->Stall (USB_SET_ROOT_PORT_ENABLE_STALL);\r
     }\r
   }\r
 \r
@@ -1295,21 +1243,20 @@ UsbRootHubResetPort (
 \r
 \r
 /**\r
-  Release the root hub's control of the interface\r
+  Release the root hub's control of the interface.\r
 \r
-  @param  HubIf                 The root hub interface\r
+  @param  HubIf                 The root hub interface.\r
 \r
   @retval EFI_SUCCESS           The root hub's control of the interface is\r
                                 released.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbRootHubRelease (\r
   IN USB_INTERFACE        *HubIf\r
   )\r
 {\r
-  DEBUG (( EFI_D_INFO, "UsbRootHubRelease: root hub released for hub %x\n", HubIf));\r
+  DEBUG (( EFI_D_INFO, "UsbRootHubRelease: root hub released for hub %p\n", HubIf));\r
 \r
   gBS->SetTimer (HubIf->HubNotify, TimerCancel, USB_ROOTHUB_POLL_INTERVAL);\r
   gBS->CloseEvent (HubIf->HubNotify);\r