]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbBus.c
index 742cdc193124f1745629c2b15853ec7c54cd7b39..6a3ac63c3aa05d640ec3b80b8ead32139e28e8fe 100644 (file)
@@ -2,20 +2,14 @@
 \r
     Usb Bus Driver Binding and Bus IO Protocol.\r
 \r
-Copyright (c) 2004 - 2013, 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
-\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
+Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "UsbBus.h"\r
 \r
-EFI_USB_IO_PROTOCOL mUsbIoProtocol = {\r
+EFI_USB_IO_PROTOCOL  mUsbIoProtocol = {\r
   UsbIoControlTransfer,\r
   UsbIoBulkTransfer,\r
   UsbIoAsyncInterruptTransfer,\r
@@ -31,7 +25,7 @@ EFI_USB_IO_PROTOCOL mUsbIoProtocol = {
   UsbIoPortReset\r
 };\r
 \r
-EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding = {\r
+EFI_DRIVER_BINDING_PROTOCOL  mUsbBusDriverBinding = {\r
   UsbBusControllerDriverSupported,\r
   UsbBusControllerDriverStart,\r
   UsbBusControllerDriverStop,\r
@@ -66,16 +60,17 @@ UsbIoControlTransfer (
   IN  EFI_USB_DEVICE_REQUEST  *Request,\r
   IN  EFI_USB_DATA_DIRECTION  Direction,\r
   IN  UINT32                  Timeout,\r
-  IN  OUT VOID                *Data,      OPTIONAL\r
-  IN  UINTN                   DataLength, OPTIONAL\r
+  IN  OUT VOID                *Data       OPTIONAL,\r
+  IN  UINTN                   DataLength  OPTIONAL,\r
   OUT UINT32                  *UsbStatus\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  USB_ENDPOINT_DESC       *EpDesc;\r
-  EFI_TPL                 OldTpl;\r
-  EFI_STATUS              Status;\r
+  USB_DEVICE         *Dev;\r
+  USB_INTERFACE      *UsbIf;\r
+  USB_ENDPOINT_DESC  *EpDesc;\r
+  EFI_TPL            OldTpl;\r
+  EFI_STATUS         Status;\r
+  UINTN              RequestedDataLength;\r
 \r
   if (UsbStatus == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -83,22 +78,35 @@ UsbIoControlTransfer (
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev    = UsbIf->Device;\r
-\r
-  Status = UsbHcControlTransfer (\r
-             Dev->Bus,\r
-             Dev->Address,\r
-             Dev->Speed,\r
-             Dev->MaxPacket0,\r
-             Request,\r
-             Direction,\r
-             Data,\r
-             &DataLength,\r
-             (UINTN) Timeout,\r
-             &Dev->Translator,\r
-             UsbStatus\r
-             );\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
+\r
+  RequestedDataLength = DataLength;\r
+  Status              = UsbHcControlTransfer (\r
+                          Dev->Bus,\r
+                          Dev->Address,\r
+                          Dev->Speed,\r
+                          Dev->MaxPacket0,\r
+                          Request,\r
+                          Direction,\r
+                          Data,\r
+                          &DataLength,\r
+                          (UINTN)Timeout,\r
+                          &Dev->Translator,\r
+                          UsbStatus\r
+                          );\r
+  //\r
+  // If the request completed successfully and the Direction of the request is\r
+  // EfiUsbDataIn or EfiUsbDataOut, then make sure the actual number of bytes\r
+  // transferred is the same as the number of bytes requested.  If a different\r
+  // number of bytes were transferred, then return EFI_DEVICE_ERROR.\r
+  //\r
+  if (!EFI_ERROR (Status)) {\r
+    if ((Direction != EfiUsbNoData) && (DataLength != RequestedDataLength)) {\r
+      Status = EFI_DEVICE_ERROR;\r
+      goto ON_EXIT;\r
+    }\r
+  }\r
 \r
   if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r
     //\r
@@ -131,11 +139,14 @@ UsbIoControlTransfer (
   // Reset the endpoint toggle when endpoint stall is cleared\r
   //\r
   if ((Request->Request     == USB_REQ_CLEAR_FEATURE) &&\r
-      (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
-                                                 USB_TARGET_ENDPOINT)) &&\r
-      (Request->Value       == USB_FEATURE_ENDPOINT_HALT)) {\r
-\r
-    EpDesc = UsbGetEndpointDesc (UsbIf, (UINT8) Request->Index);\r
+      (Request->RequestType == USB_REQUEST_TYPE (\r
+                                 EfiUsbNoData,\r
+                                 USB_REQ_TYPE_STANDARD,\r
+                                 USB_TARGET_ENDPOINT\r
+                                 )) &&\r
+      (Request->Value       == USB_FEATURE_ENDPOINT_HALT))\r
+  {\r
+    EpDesc = UsbGetEndpointDesc (UsbIf, (UINT8)Request->Index);\r
 \r
     if (EpDesc != NULL) {\r
       EpDesc->Toggle = 0;\r
@@ -150,24 +161,29 @@ UsbIoControlTransfer (
   // completely irrelevant.\r
   //\r
   if ((Request->Request == USB_REQ_SET_CONFIG) &&\r
-      (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
-                                                 USB_TARGET_DEVICE))) {\r
+      (Request->RequestType == USB_REQUEST_TYPE (\r
+                                 EfiUsbNoData,\r
+                                 USB_REQ_TYPE_STANDARD,\r
+                                 USB_TARGET_DEVICE\r
+                                 )))\r
+  {\r
     //\r
     // Don't re-create the USB interfaces if configuration isn't changed.\r
     //\r
     if ((Dev->ActiveConfig != NULL) &&\r
-        (Request->Value == Dev->ActiveConfig->Desc.ConfigurationValue)) {\r
-\r
+        (Request->Value == Dev->ActiveConfig->Desc.ConfigurationValue))\r
+    {\r
       goto ON_EXIT;\r
     }\r
-    DEBUG ((EFI_D_INFO, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));\r
+\r
+    DEBUG ((DEBUG_INFO, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));\r
 \r
     if (Dev->ActiveConfig != NULL) {\r
       UsbRemoveConfig (Dev);\r
     }\r
 \r
     if (Request->Value != 0) {\r
-      Status = UsbSelectConfig (Dev, (UINT8) Request->Value);\r
+      Status = UsbSelectConfig (Dev, (UINT8)Request->Value);\r
     }\r
 \r
     //\r
@@ -183,11 +199,14 @@ UsbIoControlTransfer (
   // should remains the same.\r
   //\r
   if ((Request->Request     == USB_REQ_SET_INTERFACE) &&\r
-      (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r
-                                                 USB_TARGET_INTERFACE)) &&\r
-      (Request->Index       == UsbIf->IfSetting->Desc.InterfaceNumber)) {\r
-\r
-    Status = UsbSelectSetting (UsbIf->IfDesc, (UINT8) Request->Value);\r
+      (Request->RequestType == USB_REQUEST_TYPE (\r
+                                 EfiUsbNoData,\r
+                                 USB_REQ_TYPE_STANDARD,\r
+                                 USB_TARGET_INTERFACE\r
+                                 )) &&\r
+      (Request->Index       == UsbIf->IfSetting->Desc.InterfaceNumber))\r
+  {\r
+    Status = UsbSelectSetting (UsbIf->IfDesc, (UINT8)Request->Value);\r
 \r
     if (!EFI_ERROR (Status)) {\r
       ASSERT (UsbIf->IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);\r
@@ -200,7 +219,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Execute a bulk transfer to the device endpoint.\r
 \r
@@ -220,56 +238,56 @@ ON_EXIT:
 EFI_STATUS\r
 EFIAPI\r
 UsbIoBulkTransfer (\r
-  IN  EFI_USB_IO_PROTOCOL *This,\r
-  IN  UINT8               Endpoint,\r
-  IN  OUT VOID            *Data,\r
-  IN  OUT UINTN           *DataLength,\r
-  IN  UINTN               Timeout,\r
-  OUT UINT32              *UsbStatus\r
+  IN  EFI_USB_IO_PROTOCOL  *This,\r
+  IN  UINT8                Endpoint,\r
+  IN  OUT VOID             *Data,\r
+  IN  OUT UINTN            *DataLength,\r
+  IN  UINTN                Timeout,\r
+  OUT UINT32               *UsbStatus\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  USB_ENDPOINT_DESC       *EpDesc;\r
-  UINT8                   BufNum;\r
-  UINT8                   Toggle;\r
-  EFI_TPL                 OldTpl;\r
-  EFI_STATUS              Status;\r
-\r
-  if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r
-      (UsbStatus == NULL)) {\r
-\r
+  USB_DEVICE         *Dev;\r
+  USB_INTERFACE      *UsbIf;\r
+  USB_ENDPOINT_DESC  *EpDesc;\r
+  UINT8              BufNum;\r
+  UINT8              Toggle;\r
+  EFI_TPL            OldTpl;\r
+  EFI_STATUS         Status;\r
+\r
+  if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR (Endpoint) > 15) ||\r
+      (UsbStatus == NULL))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  OldTpl  = gBS->RaiseTPL (USB_BUS_TPL);\r
+  OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf   = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev     = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
-  EpDesc  = UsbGetEndpointDesc (UsbIf, Endpoint);\r
+  EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
 \r
   if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_BULK)) {\r
     Status = EFI_INVALID_PARAMETER;\r
     goto ON_EXIT;\r
   }\r
 \r
-  BufNum  = 1;\r
-  Toggle  = EpDesc->Toggle;\r
-  Status  = UsbHcBulkTransfer (\r
-              Dev->Bus,\r
-              Dev->Address,\r
-              Endpoint,\r
-              Dev->Speed,\r
-              EpDesc->Desc.MaxPacketSize,\r
-              BufNum,\r
-              &Data,\r
-              DataLength,\r
-              &Toggle,\r
-              Timeout,\r
-              &Dev->Translator,\r
-              UsbStatus\r
-              );\r
+  BufNum = 1;\r
+  Toggle = EpDesc->Toggle;\r
+  Status = UsbHcBulkTransfer (\r
+             Dev->Bus,\r
+             Dev->Address,\r
+             Endpoint,\r
+             Dev->Speed,\r
+             EpDesc->Desc.MaxPacketSize,\r
+             BufNum,\r
+             &Data,\r
+             DataLength,\r
+             &Toggle,\r
+             Timeout,\r
+             &Dev->Translator,\r
+             UsbStatus\r
+             );\r
 \r
   EpDesc->Toggle = Toggle;\r
 \r
@@ -295,7 +313,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Execute a synchronous interrupt transfer.\r
 \r
@@ -315,33 +332,33 @@ ON_EXIT:
 EFI_STATUS\r
 EFIAPI\r
 UsbIoSyncInterruptTransfer (\r
-  IN  EFI_USB_IO_PROTOCOL *This,\r
-  IN  UINT8               Endpoint,\r
-  IN  OUT VOID            *Data,\r
-  IN  OUT UINTN           *DataLength,\r
-  IN  UINTN               Timeout,\r
-  OUT UINT32              *UsbStatus\r
+  IN  EFI_USB_IO_PROTOCOL  *This,\r
+  IN  UINT8                Endpoint,\r
+  IN  OUT VOID             *Data,\r
+  IN  OUT UINTN            *DataLength,\r
+  IN  UINTN                Timeout,\r
+  OUT UINT32               *UsbStatus\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  USB_ENDPOINT_DESC       *EpDesc;\r
-  EFI_TPL                 OldTpl;\r
-  UINT8                   Toggle;\r
-  EFI_STATUS              Status;\r
-\r
-  if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r
-      (UsbStatus == NULL)) {\r
-\r
+  USB_DEVICE         *Dev;\r
+  USB_INTERFACE      *UsbIf;\r
+  USB_ENDPOINT_DESC  *EpDesc;\r
+  EFI_TPL            OldTpl;\r
+  UINT8              Toggle;\r
+  EFI_STATUS         Status;\r
+\r
+  if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR (Endpoint) > 15) ||\r
+      (UsbStatus == NULL))\r
+  {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  OldTpl  = gBS->RaiseTPL (USB_BUS_TPL);\r
+  OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf   = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev     = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
-  EpDesc  = UsbGetEndpointDesc (UsbIf, Endpoint);\r
+  EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
 \r
   if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r
     Status = EFI_INVALID_PARAMETER;\r
@@ -370,7 +387,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Queue a new asynchronous interrupt transfer, or remove the old\r
   request if (IsNewTransfer == FALSE).\r
@@ -381,7 +397,7 @@ ON_EXIT:
                                  the request.\r
   @param  PollInterval           The interval to poll the transfer result, (in ms).\r
   @param  DataLength             The length of perodic data transfer.\r
-  @param  Callback               The function to call periodicaly when transfer is\r
+  @param  Callback               The function to call periodically when transfer is\r
                                  ready.\r
   @param  Context                The context to the callback.\r
 \r
@@ -397,49 +413,49 @@ UsbIoAsyncInterruptTransfer (
   IN EFI_USB_IO_PROTOCOL              *This,\r
   IN UINT8                            Endpoint,\r
   IN BOOLEAN                          IsNewTransfer,\r
-  IN UINTN                            PollInterval,       OPTIONAL\r
-  IN UINTN                            DataLength,         OPTIONAL\r
-  IN EFI_ASYNC_USB_TRANSFER_CALLBACK  Callback,           OPTIONAL\r
+  IN UINTN                            PollInterval        OPTIONAL,\r
+  IN UINTN                            DataLength          OPTIONAL,\r
+  IN EFI_ASYNC_USB_TRANSFER_CALLBACK  Callback            OPTIONAL,\r
   IN VOID                             *Context            OPTIONAL\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  USB_ENDPOINT_DESC       *EpDesc;\r
-  EFI_TPL                 OldTpl;\r
-  UINT8                   Toggle;\r
-  EFI_STATUS              Status;\r
+  USB_DEVICE         *Dev;\r
+  USB_INTERFACE      *UsbIf;\r
+  USB_ENDPOINT_DESC  *EpDesc;\r
+  EFI_TPL            OldTpl;\r
+  UINT8              Toggle;\r
+  EFI_STATUS         Status;\r
 \r
   if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR (Endpoint) > 15)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  OldTpl  = gBS->RaiseTPL (USB_BUS_TPL);\r
-  UsbIf   = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev     = UsbIf->Device;\r
+  OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
+  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev    = UsbIf->Device;\r
 \r
-  EpDesc  = UsbGetEndpointDesc (UsbIf, Endpoint);\r
+  EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r
 \r
   if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r
     Status = EFI_INVALID_PARAMETER;\r
     goto ON_EXIT;\r
   }\r
 \r
-  Toggle  = EpDesc->Toggle;\r
-  Status  = UsbHcAsyncInterruptTransfer (\r
-              Dev->Bus,\r
-              Dev->Address,\r
-              Endpoint,\r
-              Dev->Speed,\r
-              EpDesc->Desc.MaxPacketSize,\r
-              IsNewTransfer,\r
-              &Toggle,\r
-              PollInterval,\r
-              DataLength,\r
-              &Dev->Translator,\r
-              Callback,\r
-              Context\r
-              );\r
+  Toggle = EpDesc->Toggle;\r
+  Status = UsbHcAsyncInterruptTransfer (\r
+             Dev->Bus,\r
+             Dev->Address,\r
+             Endpoint,\r
+             Dev->Speed,\r
+             EpDesc->Desc.MaxPacketSize,\r
+             IsNewTransfer,\r
+             &Toggle,\r
+             PollInterval,\r
+             DataLength,\r
+             &Dev->Translator,\r
+             Callback,\r
+             Context\r
+             );\r
 \r
   EpDesc->Toggle = Toggle;\r
 \r
@@ -448,7 +464,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Execute a synchronous isochronous transfer.\r
 \r
@@ -464,17 +479,16 @@ ON_EXIT:
 EFI_STATUS\r
 EFIAPI\r
 UsbIoIsochronousTransfer (\r
-  IN  EFI_USB_IO_PROTOCOL *This,\r
-  IN  UINT8               DeviceEndpoint,\r
-  IN  OUT VOID            *Data,\r
-  IN  UINTN               DataLength,\r
-  OUT UINT32              *Status\r
+  IN  EFI_USB_IO_PROTOCOL  *This,\r
+  IN  UINT8                DeviceEndpoint,\r
+  IN  OUT VOID             *Data,\r
+  IN  UINTN                DataLength,\r
+  OUT UINT32               *Status\r
   )\r
 {\r
   return EFI_UNSUPPORTED;\r
 }\r
 \r
-\r
 /**\r
   Queue an asynchronous isochronous transfer.\r
 \r
@@ -482,7 +496,7 @@ UsbIoIsochronousTransfer (
   @param  DeviceEndpoint         The device endpoint.\r
   @param  Data                   The data to transfer.\r
   @param  DataLength             The length of perodic data transfer.\r
-  @param  IsochronousCallBack    The function to call periodicaly when transfer is\r
+  @param  IsochronousCallBack    The function to call periodically when transfer is\r
                                  ready.\r
   @param  Context                The context to the callback.\r
 \r
@@ -503,7 +517,6 @@ UsbIoAsyncIsochronousTransfer (
   return EFI_UNSUPPORTED;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the device descriptor of the device.\r
 \r
@@ -517,13 +530,13 @@ UsbIoAsyncIsochronousTransfer (
 EFI_STATUS\r
 EFIAPI\r
 UsbIoGetDeviceDescriptor (\r
-  IN  EFI_USB_IO_PROTOCOL       *This,\r
-  OUT EFI_USB_DEVICE_DESCRIPTOR *Descriptor\r
+  IN  EFI_USB_IO_PROTOCOL        *This,\r
+  OUT EFI_USB_DEVICE_DESCRIPTOR  *Descriptor\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  EFI_TPL                 OldTpl;\r
+  USB_DEVICE     *Dev;\r
+  USB_INTERFACE  *UsbIf;\r
+  EFI_TPL        OldTpl;\r
 \r
   if (Descriptor == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -531,8 +544,8 @@ UsbIoGetDeviceDescriptor (
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev    = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
   CopyMem (Descriptor, &Dev->DevDesc->Desc, sizeof (EFI_USB_DEVICE_DESCRIPTOR));\r
 \r
@@ -540,7 +553,6 @@ UsbIoGetDeviceDescriptor (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Return the configuration descriptor of the current active configuration.\r
 \r
@@ -555,14 +567,14 @@ UsbIoGetDeviceDescriptor (
 EFI_STATUS\r
 EFIAPI\r
 UsbIoGetActiveConfigDescriptor (\r
-  IN  EFI_USB_IO_PROTOCOL       *This,\r
-  OUT EFI_USB_CONFIG_DESCRIPTOR *Descriptor\r
+  IN  EFI_USB_IO_PROTOCOL        *This,\r
+  OUT EFI_USB_CONFIG_DESCRIPTOR  *Descriptor\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  EFI_STATUS              Status;\r
-  EFI_TPL                 OldTpl;\r
+  USB_DEVICE     *Dev;\r
+  USB_INTERFACE  *UsbIf;\r
+  EFI_STATUS     Status;\r
+  EFI_TPL        OldTpl;\r
 \r
   if (Descriptor == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -571,8 +583,8 @@ UsbIoGetActiveConfigDescriptor (
   Status = EFI_SUCCESS;\r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev    = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
   if (Dev->ActiveConfig == NULL) {\r
     Status = EFI_NOT_FOUND;\r
@@ -586,7 +598,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the active interface setting descriptor for this USB IO instance.\r
 \r
@@ -604,8 +615,8 @@ UsbIoGetInterfaceDescriptor (
   OUT EFI_USB_INTERFACE_DESCRIPTOR  *Descriptor\r
   )\r
 {\r
-  USB_INTERFACE           *UsbIf;\r
-  EFI_TPL                 OldTpl;\r
+  USB_INTERFACE  *UsbIf;\r
+  EFI_TPL        OldTpl;\r
 \r
   if (Descriptor == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -613,14 +624,13 @@ UsbIoGetInterfaceDescriptor (
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
   CopyMem (Descriptor, &(UsbIf->IfSetting->Desc), sizeof (EFI_USB_INTERFACE_DESCRIPTOR));\r
 \r
   gBS->RestoreTPL (OldTpl);\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the endpoint descriptor from this interface setting.\r
 \r
@@ -636,17 +646,17 @@ UsbIoGetInterfaceDescriptor (
 EFI_STATUS\r
 EFIAPI\r
 UsbIoGetEndpointDescriptor (\r
-  IN  EFI_USB_IO_PROTOCOL         *This,\r
-  IN  UINT8                       Index,\r
-  OUT EFI_USB_ENDPOINT_DESCRIPTOR *Descriptor\r
+  IN  EFI_USB_IO_PROTOCOL          *This,\r
+  IN  UINT8                        Index,\r
+  OUT EFI_USB_ENDPOINT_DESCRIPTOR  *Descriptor\r
   )\r
 {\r
-  USB_INTERFACE           *UsbIf;\r
-  EFI_TPL                 OldTpl;\r
+  USB_INTERFACE  *UsbIf;\r
+  EFI_TPL        OldTpl;\r
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
 \r
   if ((Descriptor == NULL) || (Index > 15)) {\r
     gBS->RestoreTPL (OldTpl);\r
@@ -668,7 +678,6 @@ UsbIoGetEndpointDescriptor (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Retrieve the supported language ID table from the device.\r
 \r
@@ -682,28 +691,27 @@ UsbIoGetEndpointDescriptor (
 EFI_STATUS\r
 EFIAPI\r
 UsbIoGetSupportedLanguages (\r
-  IN  EFI_USB_IO_PROTOCOL *This,\r
-  OUT UINT16              **LangIDTable,\r
-  OUT UINT16              *TableSize\r
+  IN  EFI_USB_IO_PROTOCOL  *This,\r
+  OUT UINT16               **LangIDTable,\r
+  OUT UINT16               *TableSize\r
   )\r
 {\r
-  USB_DEVICE              *Dev;\r
-  USB_INTERFACE           *UsbIf;\r
-  EFI_TPL                 OldTpl;\r
+  USB_DEVICE     *Dev;\r
+  USB_INTERFACE  *UsbIf;\r
+  EFI_TPL        OldTpl;\r
 \r
-  OldTpl        = gBS->RaiseTPL (USB_BUS_TPL);\r
+  OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf         = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev           = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
-  *LangIDTable  = Dev->LangId;\r
-  *TableSize    = (UINT16) (Dev->TotalLangId * sizeof (UINT16));\r
+  *LangIDTable = Dev->LangId;\r
+  *TableSize   = (UINT16)(Dev->TotalLangId * sizeof (UINT16));\r
 \r
   gBS->RestoreTPL (OldTpl);\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Retrieve an indexed string in the language of LangID.\r
 \r
@@ -719,19 +727,19 @@ UsbIoGetSupportedLanguages (
 EFI_STATUS\r
 EFIAPI\r
 UsbIoGetStringDescriptor (\r
-  IN  EFI_USB_IO_PROTOCOL   *This,\r
-  IN  UINT16                LangID,\r
-  IN  UINT8                 StringIndex,\r
-  OUT CHAR16                **String\r
+  IN  EFI_USB_IO_PROTOCOL  *This,\r
+  IN  UINT16               LangID,\r
+  IN  UINT8                StringIndex,\r
+  OUT CHAR16               **String\r
   )\r
 {\r
-  USB_DEVICE                *Dev;\r
-  USB_INTERFACE             *UsbIf;\r
-  EFI_USB_STRING_DESCRIPTOR *StrDesc;\r
-  EFI_TPL                   OldTpl;\r
-  UINT8                     *Buf;\r
-  UINT8                     Index;\r
-  EFI_STATUS                Status;\r
+  USB_DEVICE                 *Dev;\r
+  USB_INTERFACE              *UsbIf;\r
+  EFI_USB_STRING_DESCRIPTOR  *StrDesc;\r
+  EFI_TPL                    OldTpl;\r
+  UINT8                      *Buf;\r
+  UINT8                      Index;\r
+  EFI_STATUS                 Status;\r
 \r
   if ((StringIndex == 0) || (LangID == 0)) {\r
     return EFI_NOT_FOUND;\r
@@ -739,8 +747,8 @@ UsbIoGetStringDescriptor (
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev    = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
   //\r
   // Check whether language ID is supported\r
@@ -780,7 +788,7 @@ UsbIoGetStringDescriptor (
   }\r
 \r
   CopyMem (Buf, StrDesc->String, StrDesc->Length - 2);\r
-  *String = (CHAR16 *) Buf;\r
+  *String = (CHAR16 *)Buf;\r
   Status  = EFI_SUCCESS;\r
 \r
 FREE_STR:\r
@@ -791,7 +799,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Reset the device, then if that succeeds, reconfigure the\r
   device with its address and current active configuration.\r
@@ -808,16 +815,17 @@ UsbIoPortReset (
   IN EFI_USB_IO_PROTOCOL  *This\r
   )\r
 {\r
-  USB_INTERFACE           *UsbIf;\r
-  USB_INTERFACE           *HubIf;\r
-  USB_DEVICE              *Dev;\r
-  EFI_TPL                 OldTpl;\r
-  EFI_STATUS              Status;\r
+  USB_INTERFACE  *UsbIf;\r
+  USB_INTERFACE  *HubIf;\r
+  USB_DEVICE     *Dev;\r
+  EFI_TPL        OldTpl;\r
+  EFI_STATUS     Status;\r
+  UINT8          DevAddress;\r
 \r
   OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r
 \r
-  UsbIf  = USB_INTERFACE_FROM_USBIO (This);\r
-  Dev    = UsbIf->Device;\r
+  UsbIf = USB_INTERFACE_FROM_USBIO (This);\r
+  Dev   = UsbIf->Device;\r
 \r
   if (UsbIf->IsHub) {\r
     Status = EFI_INVALID_PARAMETER;\r
@@ -828,32 +836,46 @@ UsbIoPortReset (
   Status = HubIf->HubApi->ResetPort (HubIf, Dev->ParentPort);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to reset hub port %d@hub  %d, %r \n",\r
-                Dev->ParentPort, Dev->ParentAddr, Status));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "UsbIoPortReset: failed to reset hub port %d@hub  %d, %r \n",\r
+      Dev->ParentPort,\r
+      Dev->ParentAddr,\r
+      Status\r
+      ));\r
 \r
     goto ON_EXIT;\r
   }\r
 \r
+  HubIf->HubApi->ClearPortChange (HubIf, Dev->ParentPort);\r
+\r
   //\r
   // Reset the device to its current address. The device now has an address\r
   // of ZERO after port reset, so need to set Dev->Address to the device again for\r
   // host to communicate with it.\r
   //\r
-  Status  = UsbSetAddress (Dev, Dev->Address);\r
+  DevAddress   = Dev->Address;\r
+  Dev->Address = 0;\r
+  Status       = UsbSetAddress (Dev, DevAddress);\r
+  Dev->Address = DevAddress;\r
 \r
   gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
-  \r
+\r
   if (EFI_ERROR (Status)) {\r
     //\r
     // It may fail due to device disconnection or other reasons.\r
     //\r
-    DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set address for device %d - %r\n",\r
-                Dev->Address, Status));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "UsbIoPortReset: failed to set address for device %d - %r\n",\r
+      Dev->Address,\r
+      Status\r
+      ));\r
 \r
     goto ON_EXIT;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbIoPortReset: device is now ADDRESSED at %d\n", Dev->Address));\r
+  DEBUG ((DEBUG_INFO, "UsbIoPortReset: device is now ADDRESSED at %d\n", Dev->Address));\r
 \r
   //\r
   // Reset the current active configure, after this device\r
@@ -863,8 +885,12 @@ UsbIoPortReset (
     Status = UsbSetConfig (Dev, Dev->ActiveConfig->Desc.ConfigurationValue);\r
 \r
     if (EFI_ERROR (Status)) {\r
-      DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set configure for device %d - %r\n",\r
-                  Dev->Address, Status));\r
+      DEBUG ((\r
+        DEBUG_ERROR,\r
+        "UsbIoPortReset: failed to set configure for device %d - %r\n",\r
+        Dev->Address,\r
+        Status\r
+        ));\r
     }\r
   }\r
 \r
@@ -873,7 +899,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Install Usb Bus Protocol on host controller, and start the Usb bus.\r
 \r
@@ -894,11 +919,11 @@ UsbBusBuildProtocol (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
-  USB_BUS                 *UsbBus;\r
-  USB_DEVICE              *RootHub;\r
-  USB_INTERFACE           *RootIf;\r
-  EFI_STATUS              Status;\r
-  EFI_STATUS              Status2;\r
+  USB_BUS        *UsbBus;\r
+  USB_DEVICE     *RootHub;\r
+  USB_INTERFACE  *RootIf;\r
+  EFI_STATUS     Status;\r
+  EFI_STATUS     Status2;\r
 \r
   UsbBus = AllocateZeroPool (sizeof (USB_BUS));\r
 \r
@@ -913,14 +938,14 @@ UsbBusBuildProtocol (
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiDevicePathProtocolGuid,\r
-                  (VOID **) &UsbBus->DevicePath,\r
+                  (VOID **)&UsbBus->DevicePath,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to open device path %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbBusStart: Failed to open device path %r\n", Status));\r
 \r
     FreePool (UsbBus);\r
     return Status;\r
@@ -937,7 +962,7 @@ UsbBusBuildProtocol (
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiUsb2HcProtocolGuid,\r
-                  (VOID **) &(UsbBus->Usb2Hc),\r
+                  (VOID **)&(UsbBus->Usb2Hc),\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -946,14 +971,14 @@ UsbBusBuildProtocol (
   Status2 = gBS->OpenProtocol (\r
                    Controller,\r
                    &gEfiUsbHcProtocolGuid,\r
-                   (VOID **) &(UsbBus->UsbHc),\r
+                   (VOID **)&(UsbBus->UsbHc),\r
                    This->DriverBindingHandle,\r
                    Controller,\r
                    EFI_OPEN_PROTOCOL_BY_DRIVER\r
                    );\r
 \r
   if (EFI_ERROR (Status) && EFI_ERROR (Status2)) {\r
-    DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status));\r
 \r
     Status = EFI_DEVICE_ERROR;\r
     goto CLOSE_HC;\r
@@ -970,9 +995,6 @@ UsbBusBuildProtocol (
     }\r
   }\r
 \r
-  UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL);\r
-  UsbHcSetState (UsbBus, EfiUsbHcStateOperational);\r
-\r
   //\r
   // Install an EFI_USB_BUS_PROTOCOL to host controller to identify it.\r
   //\r
@@ -984,7 +1006,7 @@ UsbBusBuildProtocol (
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to install bus protocol %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbBusStart: Failed to install bus protocol %r\n", Status));\r
     goto CLOSE_HC;\r
   }\r
 \r
@@ -1019,7 +1041,7 @@ UsbBusBuildProtocol (
   RootIf->Signature       = USB_INTERFACE_SIGNATURE;\r
   RootIf->Device          = RootHub;\r
   RootIf->DevicePath      = UsbBus->DevicePath;\r
-  \r
+\r
   //\r
   // Report Status Code here since we will enumerate the USB devices\r
   //\r
@@ -1028,23 +1050,24 @@ UsbBusBuildProtocol (
     (EFI_IO_BUS_USB | EFI_IOB_PC_DETECT),\r
     UsbBus->DevicePath\r
     );\r
-  \r
-  Status                  = mUsbRootHubApi.Init (RootIf);\r
+\r
+  Status = mUsbRootHubApi.Init (RootIf);\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to init root hub %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "UsbBusStart: Failed to init root hub %r\n", Status));\r
     goto FREE_ROOTHUB;\r
   }\r
 \r
   UsbBus->Devices[0] = RootHub;\r
 \r
-  DEBUG ((EFI_D_INFO, "UsbBusStart: usb bus started on %p, root hub %p\n", Controller, RootIf));\r
+  DEBUG ((DEBUG_INFO, "UsbBusStart: usb bus started on %p, root hub %p\n", Controller, RootIf));\r
   return EFI_SUCCESS;\r
 \r
 FREE_ROOTHUB:\r
   if (RootIf != NULL) {\r
     FreePool (RootIf);\r
   }\r
+\r
   if (RootHub != NULL) {\r
     FreePool (RootHub);\r
   }\r
@@ -1055,20 +1078,22 @@ UNINSTALL_USBBUS:
 CLOSE_HC:\r
   if (UsbBus->Usb2Hc != NULL) {\r
     gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsb2HcProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
+           Controller,\r
+           &gEfiUsb2HcProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
   }\r
+\r
   if (UsbBus->UsbHc != NULL) {\r
     gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbHcProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
+           Controller,\r
+           &gEfiUsbHcProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
   }\r
+\r
   gBS->CloseProtocol (\r
          Controller,\r
          &gEfiDevicePathProtocolGuid,\r
@@ -1077,11 +1102,10 @@ CLOSE_HC:
          );\r
   FreePool (UsbBus);\r
 \r
-  DEBUG ((EFI_D_ERROR, "UsbBusStart: Failed to start bus driver %r\n", Status));\r
+  DEBUG ((DEBUG_ERROR, "UsbBusStart: Failed to start bus driver %r\n", Status));\r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   The USB bus driver entry pointer.\r
 \r
@@ -1095,8 +1119,8 @@ CLOSE_HC:
 EFI_STATUS\r
 EFIAPI\r
 UsbBusDriverEntryPoint (\r
-  IN EFI_HANDLE           ImageHandle,\r
-  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
   return EfiLibInstallDriverBindingComponentName2 (\r
@@ -1109,7 +1133,6 @@ UsbBusDriverEntryPoint (
            );\r
 }\r
 \r
-\r
 /**\r
   Check whether USB bus driver support this device.\r
 \r
@@ -1140,7 +1163,7 @@ UsbBusControllerDriverSupported (
   //\r
   if (RemainingDevicePath != NULL) {\r
     //\r
-    // Check if RemainingDevicePath is the End of Device Path Node, \r
+    // Check if RemainingDevicePath is the End of Device Path Node,\r
     // if yes, go on checking other conditions\r
     //\r
     if (!IsDevicePathEnd (RemainingDevicePath)) {\r
@@ -1149,13 +1172,13 @@ UsbBusControllerDriverSupported (
       // check its validation\r
       //\r
       DevicePathNode.DevPath = RemainingDevicePath;\r
-      \r
+\r
       if ((DevicePathNode.DevPath->Type    != MESSAGING_DEVICE_PATH) ||\r
-          (DevicePathNode.DevPath->SubType != MSG_USB_DP &&\r
-           DevicePathNode.DevPath->SubType != MSG_USB_CLASS_DP\r
-           && DevicePathNode.DevPath->SubType != MSG_USB_WWID_DP\r
-           )) {\r
-      \r
+          (  (DevicePathNode.DevPath->SubType != MSG_USB_DP) &&\r
+             (DevicePathNode.DevPath->SubType != MSG_USB_CLASS_DP)\r
+          && (DevicePathNode.DevPath->SubType != MSG_USB_WWID_DP)\r
+          ))\r
+      {\r
         return EFI_UNSUPPORTED;\r
       }\r
     }\r
@@ -1167,7 +1190,7 @@ UsbBusControllerDriverSupported (
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiUsb2HcProtocolGuid,\r
-                  (VOID **) &Usb2Hc,\r
+                  (VOID **)&Usb2Hc,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -1183,7 +1206,7 @@ UsbBusControllerDriverSupported (
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiUsbHcProtocolGuid,\r
-                    (VOID **) &UsbHc,\r
+                    (VOID **)&UsbHc,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -1191,7 +1214,7 @@ UsbBusControllerDriverSupported (
     if (Status == EFI_ALREADY_STARTED) {\r
       return EFI_SUCCESS;\r
     }\r
-  \r
+\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -1200,14 +1223,12 @@ UsbBusControllerDriverSupported (
     // Close the USB_HC used to perform the supported test\r
     //\r
     gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiUsbHcProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
-\r
+           Controller,\r
+           &gEfiUsbHcProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
   } else {\r
-\r
     //\r
     // Close the USB_HC2 used to perform the supported test\r
     //\r
@@ -1218,14 +1239,14 @@ UsbBusControllerDriverSupported (
            Controller\r
            );\r
   }\r
\r
+\r
   //\r
   // Open the EFI Device Path protocol needed to perform the supported test\r
   //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiDevicePathProtocolGuid,\r
-                  (VOID **) &ParentDevicePath,\r
+                  (VOID **)&ParentDevicePath,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -1239,11 +1260,11 @@ UsbBusControllerDriverSupported (
     // Close protocol, don't use device path protocol in the Support() function\r
     //\r
     gBS->CloseProtocol (\r
-          Controller,\r
-          &gEfiDevicePathProtocolGuid,\r
-          This->DriverBindingHandle,\r
-          Controller\r
-          );\r
+           Controller,\r
+           &gEfiDevicePathProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
 \r
     return EFI_SUCCESS;\r
   }\r
@@ -1251,7 +1272,6 @@ UsbBusControllerDriverSupported (
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Start to process the controller.\r
 \r
@@ -1273,14 +1293,14 @@ UsbBusControllerDriverStart (
   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath\r
   )\r
 {\r
-  EFI_USB_BUS_PROTOCOL          *UsbBusId;\r
-  EFI_STATUS                    Status;\r
-  EFI_DEVICE_PATH_PROTOCOL      *ParentDevicePath;\r
+  EFI_USB_BUS_PROTOCOL      *UsbBusId;\r
+  EFI_STATUS                Status;\r
+  EFI_DEVICE_PATH_PROTOCOL  *ParentDevicePath;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiDevicePathProtocolGuid,\r
-                  (VOID **) &ParentDevicePath,\r
+                  (VOID **)&ParentDevicePath,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -1303,7 +1323,7 @@ UsbBusControllerDriverStart (
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiCallerIdGuid,\r
-                  (VOID **) &UsbBusId,\r
+                  (VOID **)&UsbBusId,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -1318,13 +1338,14 @@ UsbBusControllerDriverStart (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
+\r
     //\r
     // Try get the Usb Bus protocol interface again\r
     //\r
     Status = gBS->OpenProtocol (\r
                     Controller,\r
                     &gEfiCallerIdGuid,\r
-                    (VOID **) &UsbBusId,\r
+                    (VOID **)&UsbBusId,\r
                     This->DriverBindingHandle,\r
                     Controller,\r
                     EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -1344,8 +1365,8 @@ UsbBusControllerDriverStart (
       if (IsDevicePathEnd (RemainingDevicePath)) {\r
         //\r
         // If RemainingDevicePath is the End of Device Path Node,\r
-        // skip enumerate any device and return EFI_SUCESSS\r
-        // \r
+        // skip enumerate any device and return EFI_SUCCESS\r
+        //\r
         return EFI_SUCCESS;\r
       }\r
     }\r
@@ -1359,11 +1380,9 @@ UsbBusControllerDriverStart (
     ASSERT (!EFI_ERROR (Status));\r
   }\r
 \r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Stop handle the controller by this USB bus driver.\r
 \r
@@ -1396,20 +1415,22 @@ UsbBusControllerDriverStop (
   EFI_TPL               OldTpl;\r
   UINTN                 Index;\r
   EFI_STATUS            Status;\r
+  EFI_STATUS            ReturnStatus;\r
 \r
-  Status  = EFI_SUCCESS;\r
+  Status = EFI_SUCCESS;\r
 \r
   if (NumberOfChildren > 0) {\r
     //\r
     // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict\r
     //\r
-    OldTpl   = gBS->RaiseTPL (TPL_CALLBACK);\r
+    OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
+    ReturnStatus = EFI_SUCCESS;\r
     for (Index = 0; Index < NumberOfChildren; Index++) {\r
       Status = gBS->OpenProtocol (\r
                       ChildHandleBuffer[Index],\r
                       &gEfiUsbIoProtocolGuid,\r
-                      (VOID **) &UsbIo,\r
+                      (VOID **)&UsbIo,\r
                       This->DriverBindingHandle,\r
                       Controller,\r
                       EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -1425,17 +1446,17 @@ UsbBusControllerDriverStop (
         continue;\r
       }\r
 \r
-      UsbIf   = USB_INTERFACE_FROM_USBIO (UsbIo);\r
-      UsbDev  = UsbIf->Device;\r
+      UsbIf  = USB_INTERFACE_FROM_USBIO (UsbIo);\r
+      UsbDev = UsbIf->Device;\r
 \r
-      UsbRemoveDevice (UsbDev);\r
+      ReturnStatus = UsbRemoveDevice (UsbDev);\r
     }\r
 \r
     gBS->RestoreTPL (OldTpl);\r
-    return EFI_SUCCESS;\r
+    return ReturnStatus;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %p\n", Controller));\r
+  DEBUG ((DEBUG_INFO, "UsbBusStop: usb bus stopped on %p\n", Controller));\r
 \r
   //\r
   // Locate USB_BUS for the current host controller\r
@@ -1443,7 +1464,7 @@ UsbBusControllerDriverStop (
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiCallerIdGuid,\r
-                  (VOID **) &BusId,\r
+                  (VOID **)&BusId,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -1460,58 +1481,66 @@ UsbBusControllerDriverStop (
   //\r
   // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict\r
   //\r
-  OldTpl  = gBS->RaiseTPL (TPL_CALLBACK);\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
   RootHub = Bus->Devices[0];\r
   RootIf  = RootHub->Interfaces[0];\r
 \r
-  mUsbRootHubApi.Release (RootIf);\r
-\r
   ASSERT (Bus->MaxDevices <= 256);\r
+  ReturnStatus = EFI_SUCCESS;\r
   for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
     if (Bus->Devices[Index] != NULL) {\r
-      UsbRemoveDevice (Bus->Devices[Index]);\r
+      Status = UsbRemoveDevice (Bus->Devices[Index]);\r
+      if (EFI_ERROR (Status)) {\r
+        ReturnStatus = Status;\r
+      }\r
     }\r
   }\r
 \r
   gBS->RestoreTPL (OldTpl);\r
 \r
-  gBS->FreePool   (RootIf);\r
-  gBS->FreePool   (RootHub);\r
-  Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
-  ASSERT (!EFI_ERROR (Status));\r
+  if (!EFI_ERROR (ReturnStatus)) {\r
+    mUsbRootHubApi.Release (RootIf);\r
+    gBS->FreePool (RootIf);\r
+    gBS->FreePool (RootHub);\r
 \r
-  //\r
-  // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
-  //\r
-  gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &Bus->BusId);\r
+    Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
+    ASSERT (!EFI_ERROR (Status));\r
 \r
-  if (Bus->Usb2Hc != NULL) {\r
-    gBS->CloseProtocol (\r
-           Controller,\r
-           &gEfiUsb2HcProtocolGuid,\r
-           This->DriverBindingHandle,\r
-           Controller\r
-           );\r
-  }\r
+    //\r
+    // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r
+    //\r
+    gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &Bus->BusId);\r
 \r
-  if (Bus->UsbHc != NULL) {\r
-    gBS->CloseProtocol (\r
-           Controller,\r
-           &gEfiUsbHcProtocolGuid,\r
-           This->DriverBindingHandle,\r
-           Controller\r
-           );\r
-  }\r
+    if (Bus->Usb2Hc != NULL) {\r
+      Status = gBS->CloseProtocol (\r
+                      Controller,\r
+                      &gEfiUsb2HcProtocolGuid,\r
+                      This->DriverBindingHandle,\r
+                      Controller\r
+                      );\r
+    }\r
 \r
-  gBS->CloseProtocol (\r
-         Controller,\r
-         &gEfiDevicePathProtocolGuid,\r
-         This->DriverBindingHandle,\r
-         Controller\r
-         );\r
+    if (Bus->UsbHc != NULL) {\r
+      Status = gBS->CloseProtocol (\r
+                      Controller,\r
+                      &gEfiUsbHcProtocolGuid,\r
+                      This->DriverBindingHandle,\r
+                      Controller\r
+                      );\r
+    }\r
 \r
-  gBS->FreePool (Bus);\r
+    if (!EFI_ERROR (Status)) {\r
+      gBS->CloseProtocol (\r
+             Controller,\r
+             &gEfiDevicePathProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
+\r
+      gBS->FreePool (Bus);\r
+    }\r
+  }\r
 \r
   return Status;\r
 }\r