]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbEnumer.c
index 9fbaf6b417d628c85bf2b99328e794b2404e8cc6..a6dc1c0c29d52f5d1d7c29d7bfc9068a917e5b4c 100644 (file)
@@ -1,7 +1,9 @@
 /** @file\r
 \r
-Copyright (c) 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+    Usb bus enumeration support.\r
+\r
+Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>\r
+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
@@ -9,29 +11,18 @@ http://opensource.org/licenses/bsd-license.php
 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
-    UsbEnumer.c\r
-\r
-  Abstract:\r
-\r
-    Usb bus enumeration support\r
-\r
-  Revision History\r
-\r
-\r
 **/\r
 \r
 #include "UsbBus.h"\r
 \r
 \r
 /**\r
-  Return the endpoint descriptor in this interface\r
+  Return the endpoint descriptor in this interface.\r
 \r
-  @param  UsbIf                 The interface to search in\r
-  @param  EpAddr                The address of the endpoint to return\r
+  @param  UsbIf                 The interface to search in.\r
+  @param  EpAddr                The address of the endpoint to return.\r
 \r
-  @return The endpoint descriptor or NULL\r
+  @return The endpoint descriptor or NULL.\r
 \r
 **/\r
 USB_ENDPOINT_DESC *\r
@@ -41,9 +32,12 @@ UsbGetEndpointDesc (
   )\r
 {\r
   USB_ENDPOINT_DESC       *EpDesc;\r
-  UINTN                   Index;\r
-\r
-  for (Index = 0; Index < UsbIf->IfSetting->Desc.NumEndpoints; Index++) {\r
+  UINT8                   Index;\r
+  UINT8                   NumEndpoints;\r
+  \r
+  NumEndpoints = UsbIf->IfSetting->Desc.NumEndpoints;\r
+  \r
+  for (Index = 0; Index < NumEndpoints; Index++) {\r
     EpDesc = UsbIf->IfSetting->Endpoints[Index];\r
 \r
     if (EpDesc->Desc.EndpointAddress == EpAddr) {\r
@@ -56,14 +50,11 @@ UsbGetEndpointDesc (
 \r
 \r
 /**\r
-  Free the resource used by USB interface\r
+  Free the resource used by USB interface.\r
 \r
-  @param  UsbIf                 The USB interface to free\r
-\r
-  @return None\r
+  @param  UsbIf                 The USB interface to free.\r
 \r
 **/\r
-STATIC\r
 VOID\r
 UsbFreeInterface (\r
   IN USB_INTERFACE        *UsbIf\r
@@ -81,10 +72,10 @@ UsbFreeInterface (
          );\r
 \r
   if (UsbIf->DevicePath != NULL) {\r
-    gBS->FreePool (UsbIf->DevicePath);\r
+    FreePool (UsbIf->DevicePath);\r
   }\r
 \r
-  gBS->FreePool (UsbIf);\r
+  FreePool (UsbIf);\r
 }\r
 \r
 \r
@@ -92,13 +83,12 @@ UsbFreeInterface (
   Create an interface for the descriptor IfDesc. Each\r
   device's configuration can have several interfaces.\r
 \r
-  @param  Device                The device has the interface descriptor\r
-  @param  IfDesc                The interface descriptor\r
+  @param  Device                The device has the interface descriptor.\r
+  @param  IfDesc                The interface descriptor.\r
 \r
   @return The created USB interface for the descriptor, or NULL.\r
 \r
 **/\r
-STATIC\r
 USB_INTERFACE *\r
 UsbCreateInterface (\r
   IN USB_DEVICE           *Device,\r
@@ -119,6 +109,7 @@ UsbCreateInterface (
   UsbIf->Signature  = USB_INTERFACE_SIGNATURE;\r
   UsbIf->Device     = Device;\r
   UsbIf->IfDesc     = IfDesc;\r
+  ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);\r
   UsbIf->IfSetting  = IfDesc->Settings[IfDesc->ActiveIndex];\r
 \r
   CopyMem (\r
@@ -143,7 +134,7 @@ UsbCreateInterface (
   UsbIf->DevicePath = AppendDevicePathNode (HubIf->DevicePath, &UsbNode.Header);\r
 \r
   if (UsbIf->DevicePath == NULL) {\r
-    USB_ERROR (("UsbCreateInterface: failed to create device path\n"));\r
+    DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to create device path\n"));\r
 \r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto ON_ERROR;\r
@@ -159,7 +150,7 @@ UsbCreateInterface (
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbCreateInterface: failed to install UsbIo - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to install UsbIo - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
 \r
@@ -178,31 +169,28 @@ UsbCreateInterface (
            NULL\r
            );\r
 \r
-    USB_ERROR (("UsbCreateInterface: failed to open host for child - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbCreateInterface: failed to open host for child - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
 \r
   return UsbIf;\r
 \r
 ON_ERROR:\r
-  if (UsbIf->DevicePath) {\r
-    gBS->FreePool (UsbIf->DevicePath);\r
+  if (UsbIf->DevicePath != NULL) {\r
+    FreePool (UsbIf->DevicePath);\r
   }\r
 \r
-  gBS->FreePool (UsbIf);\r
+  FreePool (UsbIf);\r
   return NULL;\r
 }\r
 \r
 \r
 /**\r
-  Free the resource used by this USB device\r
+  Free the resource used by this USB device.\r
 \r
-  @param  Device                The USB device to free\r
-\r
-  @return None\r
+  @param  Device                The USB device to free.\r
 \r
 **/\r
-STATIC\r
 VOID\r
 UsbFreeDevice (\r
   IN USB_DEVICE           *Device\r
@@ -219,13 +207,12 @@ UsbFreeDevice (
 /**\r
   Create a device which is on the parent's ParentPort port.\r
 \r
-  @param  ParentIf              The parent HUB interface\r
-  @param  ParentPort            The port on the HUB this device is connected to\r
+  @param  ParentIf              The parent HUB interface.\r
+  @param  ParentPort            The port on the HUB this device is connected to.\r
 \r
-  @return Created USB device\r
+  @return Created USB device, Or NULL.\r
 \r
 **/\r
-STATIC\r
 USB_DEVICE *\r
 UsbCreateDevice (\r
   IN USB_INTERFACE        *ParentIf,\r
@@ -253,15 +240,14 @@ UsbCreateDevice (
 \r
 /**\r
   Connect the USB interface with its driver. EFI USB bus will\r
-  create a USB interface for each seperate interface descriptor.\r
+  create a USB interface for each separate interface descriptor.\r
 \r
-  @param  UsbIf                 The interface to connect driver to\r
+  @param  UsbIf             The interface to connect driver to.\r
 \r
-  @return EFI_SUCCESS : Interface is managed by some driver\r
-  @return Others      : Failed to locate a driver for this interface\r
+  @return EFI_SUCCESS       Interface is managed by some driver.\r
+  @return Others            Failed to locate a driver for this interface.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbConnectDriver (\r
   IN USB_INTERFACE        *UsbIf\r
@@ -277,7 +263,7 @@ UsbConnectDriver (
   // connect drivers with this interface\r
   //\r
   if (UsbIsHubInterface (UsbIf)) {\r
-    USB_DEBUG (("UsbConnectDriver: found a hub device\n"));\r
+    DEBUG ((EFI_D_INFO, "UsbConnectDriver: found a hub device\n"));\r
     Status = mUsbHubApi.Init (UsbIf);\r
 \r
   } else {\r
@@ -288,18 +274,24 @@ UsbConnectDriver (
     // twisted TPL used. It should be no problem for us to connect\r
     // or disconnect at CALLBACK.\r
     //\r
-    OldTpl            = UsbGetCurrentTpl ();\r
-    USB_DEBUG (("UsbConnectDriver: TPL before connect is %d\n", OldTpl));\r
+    \r
+    //\r
+    // Only recursively wanted usb child device\r
+    //\r
+    if (UsbBusIsWantedUsbIO (UsbIf->Device->Bus, UsbIf)) {\r
+      OldTpl            = UsbGetCurrentTpl ();\r
+      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
 \r
-    gBS->RestoreTPL (TPL_CALLBACK);\r
+      gBS->RestoreTPL (TPL_CALLBACK);\r
 \r
-    Status            = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
-    UsbIf->IsManaged  = (BOOLEAN)!EFI_ERROR (Status);\r
+      Status            = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
+      UsbIf->IsManaged  = (BOOLEAN)!EFI_ERROR (Status);\r
 \r
-    USB_DEBUG (("UsbConnectDriver: TPL after connect is %d\n", UsbGetCurrentTpl()));\r
-    ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
+      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
+      ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
 \r
-    gBS->RaiseTPL (OldTpl);\r
+      gBS->RaiseTPL (OldTpl);\r
+    }\r
   }\r
 \r
   return Status;\r
@@ -312,10 +304,10 @@ UsbConnectDriver (
   settings. Only one setting is active. It will\r
   also reset its endpoints' toggle to zero.\r
 \r
-  @param  IfDesc                The interface descriptor to set\r
-  @param  Alternate             The alternate setting number to locate\r
+  @param  IfDesc                The interface descriptor to set.\r
+  @param  Alternate             The alternate setting number to locate.\r
 \r
-  @retval EFI_NOT_FOUND         There is no setting with this alternate index\r
+  @retval EFI_NOT_FOUND         There is no setting with this alternate index.\r
   @retval EFI_SUCCESS           The interface is set to Alternate setting.\r
 \r
 **/\r
@@ -334,6 +326,7 @@ UsbSelectSetting (
   Setting = NULL;\r
 \r
   for (Index = 0; Index < IfDesc->NumOfSetting; Index++) {\r
+    ASSERT (Index < USB_MAX_INTERFACE_SETTING);\r
     Setting = IfDesc->Settings[Index];\r
 \r
     if (Setting->Desc.AlternateSetting == Alternate) {\r
@@ -347,7 +340,8 @@ UsbSelectSetting (
 \r
   IfDesc->ActiveIndex = Index;\r
 \r
-  USB_DEBUG (("UsbSelectSetting: setting %d selected for interface %d\n",\r
+  ASSERT (Setting != NULL);\r
+  DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",\r
               Alternate, Setting->Desc.InterfaceNumber));\r
 \r
   //\r
@@ -365,11 +359,11 @@ UsbSelectSetting (
   Select a new configuration for the device. Each\r
   device may support several configurations.\r
 \r
-  @param  Device                The device to select configuration\r
-  @param  ConfigValue           The index of the configuration ( != 0)\r
+  @param  Device                The device to select configuration.\r
+  @param  ConfigValue           The index of the configuration ( != 0).\r
 \r
-  @retval EFI_NOT_FOUND         There is no configuration with the index\r
-  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource\r
+  @retval EFI_NOT_FOUND         There is no configuration with the index.\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource.\r
   @retval EFI_SUCCESS           The configuration is selected.\r
 \r
 **/\r
@@ -406,7 +400,7 @@ UsbSelectConfig (
 \r
   Device->ActiveConfig = ConfigDesc;\r
 \r
-  USB_DEBUG (("UsbSelectConfig: config %d selected for device %d\n",\r
+  DEBUG ((EFI_D_INFO, "UsbSelectConfig: config %d selected for device %d\n",\r
               ConfigValue, Device->Address));\r
 \r
   //\r
@@ -429,6 +423,7 @@ UsbSelectConfig (
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
+    ASSERT (Index < USB_MAX_INTERFACE);\r
     Device->Interfaces[Index] = UsbIf;\r
 \r
     //\r
@@ -439,7 +434,7 @@ UsbSelectConfig (
     Status = UsbConnectDriver (UsbIf);\r
 \r
     if (EFI_ERROR (Status)) {\r
-      USB_ERROR (("UsbSelectConfig: failed to connect driver %r, ignored\n", Status));\r
+      DEBUG ((EFI_D_ERROR, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status));\r
     }\r
   }\r
 \r
@@ -449,22 +444,19 @@ UsbSelectConfig (
 }\r
 \r
 \r
-\r
 /**\r
   Disconnect the USB interface with its driver.\r
 \r
-  @param  UsbIf                 The interface to disconnect driver from\r
-\r
-  @return None\r
+  @param  UsbIf                 The interface to disconnect driver from.\r
 \r
 **/\r
-STATIC\r
 VOID\r
 UsbDisconnectDriver (\r
   IN USB_INTERFACE        *UsbIf\r
   )\r
 {\r
   EFI_TPL                 OldTpl;\r
+  EFI_STATUS              Status;\r
 \r
   //\r
   // Release the hub if it's a hub controller, otherwise\r
@@ -482,14 +474,14 @@ UsbDisconnectDriver (
     // or disconnect at CALLBACK.\r
     //\r
     OldTpl           = UsbGetCurrentTpl ();\r
-    USB_DEBUG (("UsbDisconnectDriver: old TPL is %d\n", OldTpl));\r
+    DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
 \r
     gBS->RestoreTPL (TPL_CALLBACK);\r
 \r
-    gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
+    Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
     UsbIf->IsManaged = FALSE;\r
 \r
-    USB_DEBUG (("UsbDisconnectDriver: TPL after disconnect is %d\n", UsbGetCurrentTpl()));\r
+    DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));\r
     ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
 \r
     gBS->RaiseTPL (OldTpl);\r
@@ -497,13 +489,10 @@ UsbDisconnectDriver (
 }\r
 \r
 \r
-\r
 /**\r
-  Remove the current device configuration\r
+  Remove the current device configuration.\r
 \r
-  @param  Device                The USB device to remove configuration from\r
-\r
-  @return None\r
+  @param  Device                The USB device to remove configuration from.\r
 \r
 **/\r
 VOID\r
@@ -517,7 +506,8 @@ UsbRemoveConfig (
   //\r
   // Remove each interface of the device\r
   //\r
-  for (Index = 0; Index < Device->NumOfInterface; Index++) {\r
+  for (Index = 0; Index < Device->NumOfInterface; Index++) {    \r
+    ASSERT (Index < USB_MAX_INTERFACE);\r
     UsbIf = Device->Interfaces[Index];\r
 \r
     if (UsbIf == NULL) {\r
@@ -534,13 +524,12 @@ UsbRemoveConfig (
 }\r
 \r
 \r
-\r
 /**\r
   Remove the device and all its children from the bus.\r
 \r
-  @param  Device                The device to remove\r
+  @param  Device                The device to remove.\r
 \r
-  @retval EFI_SUCCESS           The device is removed\r
+  @retval EFI_SUCCESS           The device is removed.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -569,15 +558,16 @@ UsbRemoveDevice (
     Status = UsbRemoveDevice (Child);\r
 \r
     if (EFI_ERROR (Status)) {\r
-      USB_ERROR (("UsbRemoveDevice: failed to remove child, ignore error\n"));\r
+      DEBUG ((EFI_D_ERROR, "UsbRemoveDevice: failed to remove child, ignore error\n"));\r
       Bus->Devices[Index] = NULL;\r
     }\r
   }\r
 \r
   UsbRemoveConfig (Device);\r
 \r
-  USB_DEBUG (("UsbRemoveDevice: device %d removed\n", Device->Address));\r
+  DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
 \r
+  ASSERT (Device->Address < USB_MAX_DEVICES);\r
   Bus->Devices[Device->Address] = NULL;\r
   UsbFreeDevice (Device);\r
 \r
@@ -586,15 +576,14 @@ UsbRemoveDevice (
 \r
 \r
 /**\r
-  Find the child device on the hub's port\r
+  Find the child device on the hub's port.\r
 \r
-  @param  HubIf                 The hub interface\r
-  @param  Port                  The port of the hub this child is connected to\r
+  @param  HubIf                 The hub interface.\r
+  @param  Port                  The port of the hub this child is connected to.\r
 \r
-  @return The device on the hub's port, or NULL if there is none\r
+  @return The device on the hub's port, or NULL if there is none.\r
 \r
 **/\r
-STATIC\r
 USB_DEVICE *\r
 UsbFindChild (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -624,19 +613,17 @@ UsbFindChild (
 }\r
 \r
 \r
-\r
 /**\r
   Enumerate and configure the new device on the port of this HUB interface.\r
 \r
-  @param  HubIf                 The HUB that has the device connected\r
-  @param  Port                  The port index of the hub (started with zero)\r
+  @param  HubIf                 The HUB that has the device connected.\r
+  @param  Port                  The port index of the hub (started with zero).\r
 \r
-  @retval EFI_SUCCESS           The device is enumerated (added or removed)\r
-  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource for the device\r
-  @retval Others                Failed to enumerate the device\r
+  @retval EFI_SUCCESS           The device is enumerated (added or removed).\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource for the device.\r
+  @retval Others                Failed to enumerate the device.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbEnumerateNewDev (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -656,13 +643,9 @@ UsbEnumerateNewDev (
   Parent  = HubIf->Device;\r
   Bus     = Parent->Bus;\r
   HubApi  = HubIf->HubApi;\r
-\r
-\r
-  //\r
-  // Wait at least 100 ms for the power on port to stable\r
-  //\r
-  gBS->Stall (100 * USB_STALL_1_MS);\r
-\r
+  \r
+  gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
+  \r
   //\r
   // Hub resets the device for at least 10 milliseconds.\r
   // Host learns device speed. If device is of low/full speed\r
@@ -672,12 +655,12 @@ UsbEnumerateNewDev (
   Status = HubApi->ResetPort (HubIf, Port);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status));\r
 \r
     return Status;\r
   }\r
 \r
-  USB_DEBUG (("UsbEnumerateNewDev: hub port %d is reset\n", Port));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port));\r
 \r
   Child = UsbCreateDevice (HubIf, Port);\r
 \r
@@ -692,7 +675,7 @@ UsbEnumerateNewDev (
   Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port));\r
     goto ON_ERROR;\r
   }\r
 \r
@@ -706,47 +689,48 @@ UsbEnumerateNewDev (
     Child->Speed = EFI_USB_SPEED_FULL;\r
   }\r
 \r
-  USB_DEBUG (("UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
 \r
   if (Child->Speed != EFI_USB_SPEED_HIGH) {\r
     //\r
     // If the child isn't a high speed device, it is necessary to\r
-    // set the transaction translator. This is quite simple:\r
+    // set the transaction translator. Port TT is 1-based.\r
+    // This is quite simple:\r
     //  1. if parent is of high speed, then parent is our translator\r
     //  2. otherwise use parent's translator.\r
     //\r
     if (Parent->Speed == EFI_USB_SPEED_HIGH) {\r
       Child->Translator.TranslatorHubAddress  = Parent->Address;\r
-      Child->Translator.TranslatorPortNumber  = Port;\r
+      Child->Translator.TranslatorPortNumber  = (UINT8) (Port + 1);\r
 \r
     } else {\r
       Child->Translator = Parent->Translator;\r
     }\r
 \r
-    USB_DEBUG (("UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
+    DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
                 Child->Translator.TranslatorHubAddress,\r
                 Child->Translator.TranslatorPortNumber));\r
   }\r
 \r
   //\r
   // After port is reset, hub establishes a signal path between\r
-  // the device and host (DEFALUT state). Device¡¯s registers are\r
+  // the device and host (DEFALUT state). Device's registers are\r
   // reset, use default address 0 (host enumerates one device at\r
   // a time) , and ready to respond to control transfer at EP 0.\r
   //\r
 \r
   //\r
   // Host sends a Get_Descriptor request to learn the max packet\r
-  // size of default pipe (only part of the device¡¯s descriptor).\r
+  // size of default pipe (only part of the device's descriptor).\r
   //\r
   Status = UsbGetMaxPacketSize0 (Child);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
 \r
-  USB_DEBUG (("UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
 \r
   //\r
   // Host assigns an address to the device. Device completes the\r
@@ -760,7 +744,7 @@ UsbEnumerateNewDev (
   }\r
 \r
   if (Address == USB_MAX_DEVICES) {\r
-    USB_ERROR (("UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
 \r
     Status = EFI_ACCESS_DENIED;\r
     goto ON_ERROR;\r
@@ -771,25 +755,22 @@ UsbEnumerateNewDev (
   Child->Address        = Address;\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
+  \r
+  gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
 \r
-  //\r
-  // Wait 20ms for set address to complete\r
-  //\r
-  gBS->Stall (20 * USB_STALL_1_MS);\r
-\r
-  USB_DEBUG (("UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
+  DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
 \r
   //\r
-  // Host learns about the device¡¯s abilities by requesting device's\r
+  // Host learns about the device's abilities by requesting device's\r
   // entire descriptions.\r
   //\r
   Status = UsbBuildDescTable (Child);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
 \r
@@ -801,11 +782,11 @@ UsbEnumerateNewDev (
   Status = UsbSetConfig (Child, Config);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config, Status));\r
     goto ON_ERROR;\r
   }\r
 \r
-  USB_DEBUG (("UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address));\r
 \r
   //\r
   // Host assigns and loads a device driver.\r
@@ -813,7 +794,7 @@ UsbEnumerateNewDev (
   Status = UsbSelectConfig (Child, Config);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
 \r
@@ -832,19 +813,17 @@ ON_ERROR:
 }\r
 \r
 \r
-\r
 /**\r
   Process the events on the port.\r
 \r
-  @param  HubIf                 The HUB that has the device connected\r
-  @param  Port                  The port index of the hub (started with zero)\r
+  @param  HubIf                 The HUB that has the device connected.\r
+  @param  Port                  The port index of the hub (started with zero).\r
 \r
-  @retval EFI_SUCCESS           The device is enumerated (added or removed)\r
-  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource for the device\r
-  @retval Others                Failed to enumerate the device\r
+  @retval EFI_SUCCESS           The device is enumerated (added or removed).\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate resource for the device.\r
+  @retval Others                Failed to enumerate the device.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 UsbEnumeratePort (\r
   IN USB_INTERFACE        *HubIf,\r
@@ -865,7 +844,7 @@ UsbEnumeratePort (
   Status = HubApi->GetPortStatus (HubIf, Port, &PortState);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    USB_ERROR (("UsbEnumeratePort: failed to get state of port %d\n", Port));\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: failed to get state of port %d\n", Port));\r
     return Status;\r
   }\r
 \r
@@ -873,63 +852,90 @@ UsbEnumeratePort (
     return EFI_SUCCESS;\r
   }\r
 \r
-  USB_DEBUG (("UsbEnumeratePort: port %d state - %x, change - %x\n",\r
-              Port, PortState.PortStatus, PortState.PortChangeStatus));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %x, change - %x on %p\n",\r
+              Port, PortState.PortStatus, PortState.PortChangeStatus, HubIf));\r
 \r
   //\r
   // This driver only process two kinds of events now: over current and\r
   // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.\r
   // ENABLE/RESET is used to reset port. SUSPEND isn't supported.\r
   //\r
-  Status = EFI_SUCCESS;\r
+  \r
+  if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) {     \r
 \r
-  if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_OVERCURRENT)) {\r
+    if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
+      //\r
+      // Case1:\r
+      //   Both OverCurrent and OverCurrentChange set, means over current occurs, \r
+      //   which probably is caused by short circuit. It has to wait system hardware\r
+      //   to perform recovery.\r
+      //\r
+      DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: Critical Over Current\n", Port));\r
+      return EFI_DEVICE_ERROR;\r
+      \r
+    } \r
     //\r
-    // If overcurrent condition is cleared, enable the port again\r
+    // Case2:\r
+    //   Only OverCurrentChange set, means system has been recoveried from \r
+    //   over current. As a result, all ports are nearly power-off, so\r
+    //   it's necessary to detach and enumerate all ports again. \r
     //\r
-    if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_OVERCURRENT)) {\r
-      HubApi->SetPortFeature (HubIf, Port, (EFI_USB_PORT_FEATURE) USB_HUB_PORT_POWER);\r
-    }\r
+    DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port)); \r
+  }\r
 \r
-  } else if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
+  if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_ENABLE)) {  \r
     //\r
-    // Device connected or disconnected. Either way, if there is\r
-    // already a device present in the bus, need to remove it.\r
+    // Case3:\r
+    //   1.1 roothub port reg doesn't reflect over-current state, while its counterpart\r
+    //   on 2.0 roothub does. When over-current has influence on 1.1 device, the port \r
+    //   would be disabled, so it's also necessary to detach and enumerate again.\r
     //\r
-    Child = UsbFindChild (HubIf, Port);\r
-\r
-    if (Child != NULL) {\r
-      USB_DEBUG (("UsbEnumeratePort: device at port %d removed from system\n", Port));\r
-      UsbRemoveDevice (Child);\r
-    }\r
-\r
-    if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
-      //\r
-      // Now, new device connected, enumerate and configure the device\r
-      //\r
-      USB_DEBUG (("UsbEnumeratePort: new device connected at port %d\n", Port));\r
-      Status = UsbEnumerateNewDev (HubIf, Port);\r
-\r
-    } else {\r
-      USB_DEBUG (("UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
-    }\r
+    DEBUG (( EFI_D_ERROR, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port));\r
+  }\r
+  \r
+  if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_CONNECTION)) {\r
+    //\r
+    // Case4:\r
+    //   Device connected or disconnected normally. \r
+    //\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: Device Connect/Discount Normally\n", Port));\r
   }\r
 \r
+  // \r
+  // Following as the above cases, it's safety to remove and create again.\r
+  //\r
+  Child = UsbFindChild (HubIf, Port);\r
+  \r
+  if (Child != NULL) {\r
+    DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));\r
+    UsbRemoveDevice (Child);\r
+  }\r
+  \r
+  if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
+    //\r
+    // Now, new device connected, enumerate and configure the device \r
+    //\r
+    DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port));\r
+    Status = UsbEnumerateNewDev (HubIf, Port);\r
+  \r
+  } else {\r
+    DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port));\r
+  }\r
+  \r
   HubApi->ClearPortChange (HubIf, Port);\r
   return Status;\r
 }\r
 \r
 \r
 /**\r
-  Enumerate all the changed hub ports\r
-\r
-  @param  Event                 The event that is triggered\r
-  @param  Context               The context to the event\r
+  Enumerate all the changed hub ports.\r
 \r
-  @return None\r
+  @param  Event                 The event that is triggered.\r
+  @param  Context               The context to the event.\r
 \r
 **/\r
 VOID\r
+EFIAPI\r
 UsbHubEnumeration (\r
   IN EFI_EVENT            Event,\r
   IN VOID                 *Context\r
@@ -940,7 +946,7 @@ UsbHubEnumeration (
   UINT8                   Bit;\r
   UINT8                   Index;\r
 \r
-  ASSERT (Context);\r
+  ASSERT (Context != NULL);\r
 \r
   HubIf = (USB_INTERFACE *) Context;\r
 \r
@@ -971,15 +977,14 @@ UsbHubEnumeration (
 \r
 \r
 /**\r
-  Enumerate all the changed hub ports\r
-\r
-  @param  Event                 The event that is triggered\r
-  @param  Context               The context to the event\r
+  Enumerate all the changed hub ports.\r
 \r
-  @return None\r
+  @param  Event                 The event that is triggered.\r
+  @param  Context               The context to the event.\r
 \r
 **/\r
 VOID\r
+EFIAPI\r
 UsbRootHubEnumeration (\r
   IN EFI_EVENT            Event,\r
   IN VOID                 *Context\r