]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
MdeModulePkg/UsbBus: Fix system hang when failed to uninstall UsbIo
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbEnumer.c
index ea54d37c9350a292d8c866c5e101e804663681dd..b0e6b835ac24f03c1aedc8e73ff9bb731eb5279d 100644 (file)
@@ -2,7 +2,7 @@
 \r
     Usb bus enumeration support.\r
 \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2017, 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
@@ -53,28 +53,33 @@ UsbGetEndpointDesc (
 \r
   @param  UsbIf                 The USB interface to free.\r
 \r
+  @retval EFI_ACCESS_DENIED     The interface is still occupied.\r
+  @retval EFI_SUCCESS           The interface is freed.\r
 **/\r
-VOID\r
+EFI_STATUS\r
 UsbFreeInterface (\r
   IN USB_INTERFACE        *UsbIf\r
   )\r
 {\r
-  UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);\r
+  EFI_STATUS              Status;\r
 \r
-  gBS->UninstallMultipleProtocolInterfaces (\r
-         UsbIf->Handle,\r
-         &gEfiDevicePathProtocolGuid,\r
-         UsbIf->DevicePath,\r
-         &gEfiUsbIoProtocolGuid,\r
-         &UsbIf->UsbIo,\r
-         NULL\r
-         );\r
+  UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);\r
 \r
-  if (UsbIf->DevicePath != NULL) {\r
-    FreePool (UsbIf->DevicePath);\r
+  Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                  UsbIf->Handle,\r
+                  &gEfiDevicePathProtocolGuid, UsbIf->DevicePath,\r
+                  &gEfiUsbIoProtocolGuid,      &UsbIf->UsbIo,\r
+                  NULL\r
+                  );\r
+  if (!EFI_ERROR (Status)) {\r
+    if (UsbIf->DevicePath != NULL) {\r
+      FreePool (UsbIf->DevicePath);\r
+    }\r
+    FreePool (UsbIf);\r
+  } else {\r
+    UsbOpenHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle);\r
   }\r
-\r
-  FreePool (UsbIf);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -525,7 +530,13 @@ UsbRemoveConfig (
 \r
     Status = UsbDisconnectDriver (UsbIf);\r
     if (!EFI_ERROR (Status)) {\r
-      UsbFreeInterface (UsbIf);\r
+      Status = UsbFreeInterface (UsbIf);\r
+      if (EFI_ERROR (Status)) {\r
+        UsbConnectDriver (UsbIf);\r
+      }\r
+    }\r
+\r
+    if (!EFI_ERROR (Status)) {\r
       Device->Interfaces[Index] = NULL;\r
     } else {\r
       ReturnStatus = Status;\r