]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/EhciDxe/Ehci.c
Fixed potential issues to release resources when error occurs.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / EhciDxe / Ehci.c
index 0eaa9cce5dec3e0aa9b843f79a6d37002d72b043..e37cf719d7fcb9dc3a2a7a28cacd9b5471949640 100644 (file)
@@ -126,7 +126,7 @@ EhcReset (
     // Host Controller must be Halt when Reset it\r
     //\r
     if (!EhcIsHalt (Ehc)) {\r
-      Status = EhcHaltHC (Ehc, EHC_GENERIC_TIME);\r
+      Status = EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);\r
 \r
       if (EFI_ERROR (Status)) {\r
         Status = EFI_DEVICE_ERROR;\r
@@ -142,7 +142,7 @@ EhcReset (
     EhcAckAllInterrupt (Ehc);\r
     EhcFreeSched (Ehc);\r
 \r
-    Status = EhcResetHC (Ehc, EHC_STALL_1_SECOND);\r
+    Status = EhcResetHC (Ehc, EHC_RESET_TIMEOUT);\r
 \r
     if (EFI_ERROR (Status)) {\r
       goto ON_EXIT;\r
@@ -251,7 +251,7 @@ EhcSetState (
 \r
   switch (State) {\r
   case EfiUsbHcStateHalt:\r
-    Status = EhcHaltHC (Ehc, EHC_GENERIC_TIME);\r
+    Status = EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);\r
     break;\r
 \r
   case EfiUsbHcStateOperational:\r
@@ -260,7 +260,17 @@ EhcSetState (
       break;\r
     }\r
 \r
-    Status = EhcRunHC (Ehc, EHC_GENERIC_TIME);\r
+    //\r
+    // Software must not write a one to this field unless the host controller\r
+    // is in the Halted state. Doing so will yield undefined results.\r
+    // refers to Spec[EHCI1.0-2.3.1]\r
+    //\r
+    if (!EHC_REG_BIT_IS_SET (Ehc, EHC_USBSTS_OFFSET, USBSTS_HALT)) {\r
+      Status = EFI_DEVICE_ERROR;\r
+      break;\r
+    }\r
+\r
+    Status = EhcRunHC (Ehc, EHC_GENERIC_TIMEOUT);\r
     break;\r
 \r
   case EfiUsbHcStateSuspend:\r
@@ -437,7 +447,7 @@ EhcSetRootHubPortFeature (
     // Make sure Host Controller not halt before reset it\r
     //\r
     if (EhcIsHalt (Ehc)) {\r
-      Status = EhcRunHC (Ehc, EHC_GENERIC_TIME);\r
+      Status = EhcRunHC (Ehc, EHC_GENERIC_TIMEOUT);\r
 \r
       if (EFI_ERROR (Status)) {\r
         EHC_DEBUG (("EhcSetRootHubPortFeature :failed to start HC - %r\n", Status));\r
@@ -1295,14 +1305,13 @@ Returns:
 \r
 --*/\r
 {\r
-  return EfiLibInstallAllDriverProtocols (\r
+  return EfiLibInstallDriverBindingComponentName2 (\r
            ImageHandle,\r
            SystemTable,\r
            &gEhciDriverBinding,\r
            ImageHandle,\r
            &gEhciComponentName,\r
-           NULL,\r
-           NULL\r
+           &gEhciComponentName2\r
            );\r
 }\r
 \r
@@ -1386,7 +1395,8 @@ ON_EXIT:
 /**\r
   Create and initialize a USB2_HC_DEV\r
 \r
-  @param  PciIo                The PciIo on this device\r
+  @param  PciIo                  The PciIo on this device\r
+  @param  OriginalPciAttributes  Original PCI attributes\r
 \r
   @return The allocated and initialized USB2_HC_DEV structure\r
   @return if created, otherwise NULL.\r
@@ -1395,7 +1405,8 @@ ON_EXIT:
 STATIC\r
 USB2_HC_DEV *\r
 EhcCreateUsb2Hc (\r
-  IN EFI_PCI_IO_PROTOCOL  *PciIo\r
+  IN EFI_PCI_IO_PROTOCOL  *PciIo,\r
+  IN UINT64               OriginalPciAttributes\r
   )\r
 {\r
   USB2_HC_DEV             *Ehc;\r
@@ -1428,7 +1439,8 @@ EhcCreateUsb2Hc (
   Ehc->Usb2Hc.MajorRevision             = 0x1;\r
   Ehc->Usb2Hc.MinorRevision             = 0x1;\r
 \r
-  Ehc->PciIo = PciIo;\r
+  Ehc->PciIo                 = PciIo;\r
+  Ehc->OriginalPciAttributes = OriginalPciAttributes;\r
 \r
   InitializeListHead (&Ehc->AsyncIntTransfers);\r
 \r
@@ -1482,6 +1494,9 @@ EhcDriverBindingStart (
   EFI_STATUS              Status;\r
   USB2_HC_DEV             *Ehc;\r
   EFI_PCI_IO_PROTOCOL     *PciIo;\r
+  UINT64                  Supports;\r
+  UINT64                  OriginalPciAttributes;\r
+  BOOLEAN                 PciAttributesSaved;\r
 \r
   //\r
   // Open the PciIo Protocol, then enable the USB host controller\r
@@ -1500,12 +1515,37 @@ EhcDriverBindingStart (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  PciAttributesSaved = FALSE;\r
+  //\r
+  // Save original PCI attributes\r
+  //\r
   Status = PciIo->Attributes (\r
                     PciIo,\r
-                    EfiPciIoAttributeOperationEnable,\r
-                    EFI_PCI_DEVICE_ENABLE,\r
-                    NULL\r
+                    EfiPciIoAttributeOperationGet,\r
+                    0,\r
+                    &OriginalPciAttributes\r
+                    );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto CLOSE_PCIIO;\r
+  }\r
+  PciAttributesSaved = TRUE;\r
+\r
+  Status = PciIo->Attributes (\r
+                    PciIo,\r
+                    EfiPciIoAttributeOperationSupported,\r
+                    0,\r
+                    &Supports\r
                     );\r
+  if (!EFI_ERROR (Status)) {\r
+    Supports &= EFI_PCI_DEVICE_ENABLE;\r
+    Status = PciIo->Attributes (\r
+                      PciIo,\r
+                      EfiPciIoAttributeOperationEnable,\r
+                      Supports,\r
+                      NULL\r
+                      );\r
+  }\r
 \r
   if (EFI_ERROR (Status)) {\r
     EHC_ERROR (("EhcDriverBindingStart: failed to enable controller\n"));\r
@@ -1515,7 +1555,7 @@ EhcDriverBindingStart (
   //\r
   // Create then install USB2_HC_PROTOCOL\r
   //\r
-  Ehc = EhcCreateUsb2Hc (PciIo);\r
+  Ehc = EhcCreateUsb2Hc (PciIo, OriginalPciAttributes);\r
 \r
   if (Ehc == NULL) {\r
     EHC_ERROR (("EhcDriverBindingStart: failed to create USB2_HC\n"));\r
@@ -1540,7 +1580,7 @@ EhcDriverBindingStart (
   // Robustnesss improvement such as for UoL\r
   //\r
   EhcClearLegacySupport (Ehc);\r
-  EhcResetHC (Ehc, EHC_STALL_1_SECOND);\r
+  EhcResetHC (Ehc, EHC_RESET_TIMEOUT);\r
 \r
   Status = EhcInitHC (Ehc);\r
 \r
@@ -1552,12 +1592,12 @@ EhcDriverBindingStart (
   //\r
   // Start the asynchronous interrupt monitor\r
   //\r
-  Status = gBS->SetTimer (Ehc->PollTimer, TimerPeriodic, EHC_ASYNC_POLL_TIME);\r
+  Status = gBS->SetTimer (Ehc->PollTimer, TimerPeriodic, EHC_ASYNC_POLL_INTERVAL);\r
 \r
   if (EFI_ERROR (Status)) {\r
     EHC_ERROR (("EhcDriverBindingStart: failed to start async interrupt monitor\n"));\r
 \r
-    EhcHaltHC (Ehc, EHC_GENERIC_TIME);\r
+    EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);\r
     goto UNINSTALL_USBHC;\r
   }\r
 \r
@@ -1565,13 +1605,22 @@ EhcDriverBindingStart (
   // Install the component name protocol, don't fail the start\r
   // because of something for display.\r
   //\r
-  AddUnicodeString (\r
+  AddUnicodeString2 (\r
     "eng",\r
     gEhciComponentName.SupportedLanguages,\r
     &Ehc->ControllerNameTable,\r
-    L"Enhanced Host Controller (USB 2.0)"\r
+    L"Enhanced Host Controller (USB 2.0)",\r
+    TRUE\r
+    );\r
+  AddUnicodeString2 (\r
+    "en",\r
+    gEhciComponentName2.SupportedLanguages,\r
+    &Ehc->ControllerNameTable,\r
+    L"Enhanced Host Controller (USB 2.0)",\r
+    FALSE\r
     );\r
 \r
+\r
   EHC_DEBUG (("EhcDriverBindingStart: EHCI started for controller @ %x\n", Controller));\r
   return EFI_SUCCESS;\r
 \r
@@ -1588,6 +1637,18 @@ FREE_POOL:
   gBS->FreePool (Ehc);\r
 \r
 CLOSE_PCIIO:\r
+  if (PciAttributesSaved == TRUE) {\r
+    //\r
+    // Restore original PCI attributes\r
+    //\r
+    PciIo->Attributes (\r
+                    PciIo,\r
+                    EfiPciIoAttributeOperationSet,\r
+                    OriginalPciAttributes,\r
+                    NULL\r
+                    );\r
+  }\r
+\r
   gBS->CloseProtocol (\r
          Controller,\r
          &gEfiPciIoProtocolGuid,\r
@@ -1651,8 +1712,8 @@ EhcDriverBindingStop (
   // Stop AsyncRequest Polling timer then stop the EHCI driver\r
   // and uninstall the EHCI protocl.\r
   //\r
-  gBS->SetTimer (Ehc->PollTimer, TimerCancel, EHC_ASYNC_POLL_TIME);\r
-  EhcHaltHC (Ehc, EHC_GENERIC_TIME);\r
+  gBS->SetTimer (Ehc->PollTimer, TimerCancel, EHC_ASYNC_POLL_INTERVAL);\r
+  EhcHaltHC (Ehc, EHC_GENERIC_TIMEOUT);\r
 \r
   Status = gBS->UninstallProtocolInterface (\r
                   Controller,\r
@@ -1675,14 +1736,14 @@ EhcDriverBindingStop (
   }\r
 \r
   //\r
-  // Disable the USB Host Controller\r
+  // Restore original PCI attributes\r
   //\r
   PciIo->Attributes (\r
-           PciIo,\r
-           EfiPciIoAttributeOperationDisable,\r
-           EFI_PCI_DEVICE_ENABLE,\r
-           NULL\r
-           );\r
+                  PciIo,\r
+                  EfiPciIoAttributeOperationSet,\r
+                  Ehc->OriginalPciAttributes,\r
+                  NULL\r
+                  );\r
 \r
   gBS->CloseProtocol (\r
          Controller,\r
@@ -1691,8 +1752,9 @@ EhcDriverBindingStop (
          Controller\r
          );\r
 \r
-  gBS->FreePool (Ehc);\r
-  return Status;\r
+  FreePool (Ehc);\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 EFI_DRIVER_BINDING_PROTOCOL\r