]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
Fix GCC build failure.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugPortDxe / DebugPort.c
index e3e47302a3086df2d1e336a2fe55e179d991854a..00889a246d8fc801a6ad4b274d51877f6ef0fdae 100644 (file)
-/*++\r
+/** @file\r
+  Top level C file for debugport driver.  Contains initialization function.\r
+  This driver layers on top of SerialIo.\r
+  ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
+  INTERRUPT CONTEXT\r
 \r
-Copyright (c) 2006, 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
+Copyright (c) 2006 - 2014, 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
-Module Name:\r
-\r
-    DebugPort.c\r
-\r
-Abstract:\r
-\r
-    Top level C file for debugport driver.  Contains initialization function.\r
-    This driver layers on top of SerialIo.\r
-    \r
-    ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
-    INTERRUPT CONTEXT.\r
-\r
-Revision History\r
-\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
+**/\r
 \r
 #include "DebugPort.h"\r
 \r
 //\r
-// Misc. functions local to this module..\r
+// Globals\r
 //\r
-STATIC\r
-VOID\r
-GetDebugPortVariable (\r
-  DEBUGPORT_DEVICE            *DebugPortDevice\r
-  )\r
-/*++\r
+EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {\r
+  DebugPortSupported,\r
+  DebugPortStart,\r
+  DebugPortStop,\r
+  DEBUGPORT_DRIVER_VERSION,\r
+  NULL,\r
+  NULL\r
+};\r
 \r
-Routine Description:\r
-  Local worker function to obtain device path information from DebugPort variable.\r
-  Records requested settings in DebugPort device structure.\r
-  \r
-Arguments:\r
-  DEBUGPORT_DEVICE  *DebugPortDevice,\r
+DEBUGPORT_DEVICE mDebugPortDevice = {\r
+  DEBUGPORT_DEVICE_SIGNATURE,\r
+  (EFI_HANDLE) 0,\r
+  (EFI_HANDLE) 0,\r
+  (EFI_DEVICE_PATH_PROTOCOL *) NULL,\r
+  {\r
+    DebugPortReset,\r
+    DebugPortWrite,\r
+    DebugPortRead,\r
+    DebugPortPoll\r
+  },\r
+  (EFI_HANDLE) 0,\r
+  (EFI_SERIAL_IO_PROTOCOL *) NULL,\r
+  DEBUGPORT_UART_DEFAULT_BAUDRATE,\r
+  DEBUGPORT_UART_DEFAULT_FIFO_DEPTH,\r
+  DEBUGPORT_UART_DEFAULT_TIMEOUT,\r
+  (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY,\r
+  DEBUGPORT_UART_DEFAULT_DATA_BITS,\r
+  (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS\r
+};\r
 \r
-Returns:\r
+/**\r
+  Local worker function to obtain device path information from DebugPort variable.\r
 \r
-  Nothing\r
+  Records requested settings in DebugPort device structure.\r
 \r
---*/\r
+**/\r
+EFI_DEVICE_PATH_PROTOCOL *\r
+GetDebugPortVariable (\r
+  VOID\r
+  )\r
 {\r
   UINTN                     DataSize;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DebugPortVariable;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
-  EFI_STATUS                Status;\r
 \r
-  DataSize = 0;\r
-\r
-  Status = gRT->GetVariable (\r
-                  (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
-                  &gEfiDebugPortVariableGuid,\r
-                  NULL,\r
-                  &DataSize,\r
-                  DebugPortDevice->DebugPortVariable\r
-                  );\r
-\r
-  if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    if (gDebugPortDevice->DebugPortVariable != NULL) {\r
-      FreePool (gDebugPortDevice->DebugPortVariable);\r
-    }\r
+  GetVariable2 (EFI_DEBUGPORT_VARIABLE_NAME, &gEfiDebugPortVariableGuid, (VOID **) &DebugPortVariable, &DataSize);\r
+  if (DebugPortVariable == NULL) {\r
+    return NULL;\r
+  }\r
 \r
-    DebugPortDevice->DebugPortVariable = AllocatePool (DataSize);\r
-    if (DebugPortDevice->DebugPortVariable != NULL) {\r
-      gRT->GetVariable (\r
-            (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
-            &gEfiDebugPortVariableGuid,\r
-            NULL,\r
-            &DataSize,\r
-            DebugPortDevice->DebugPortVariable\r
-            );\r
-      DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DebugPortDevice->DebugPortVariable;\r
-      while (!EfiIsDevicePathEnd (DevicePath) && !EfiIsUartDevicePath (DevicePath)) {\r
-        DevicePath = EfiNextDevicePathNode (DevicePath);\r
-      }\r
+  DevicePath = DebugPortVariable;\r
+  while (!IsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
+  }\r
 \r
-      if (EfiIsDevicePathEnd (DevicePath)) {\r
-        FreePool (gDebugPortDevice->DebugPortVariable);\r
-        DebugPortDevice->DebugPortVariable = NULL;\r
-      } else {\r
-        CopyMem (\r
-          &DebugPortDevice->BaudRate,\r
-          &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
-          );\r
-        DebugPortDevice->ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
-        DebugPortDevice->Timeout          = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
-        CopyMem (\r
-          &DebugPortDevice->Parity,\r
-          &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
-          );\r
-        CopyMem (\r
-          &DebugPortDevice->DataBits,\r
-          &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
-          );\r
-        CopyMem (\r
-          &DebugPortDevice->StopBits,\r
-          &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
-          sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
-          );\r
-      }\r
-    }\r
+  if (IsDevicePathEnd (DevicePath)) {\r
+    FreePool (DebugPortVariable);\r
+    return NULL;\r
+  } else {\r
+    CopyMem (\r
+      &mDebugPortDevice.BaudRate,\r
+      &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
+      );\r
+    mDebugPortDevice.ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
+    mDebugPortDevice.Timeout          = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
+    CopyMem (\r
+      &mDebugPortDevice.Parity,\r
+      &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
+      );\r
+    CopyMem (\r
+      &mDebugPortDevice.DataBits,\r
+      &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
+      );\r
+    CopyMem (\r
+      &mDebugPortDevice.StopBits,\r
+      &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
+      sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
+      );\r
+    return DebugPortVariable;\r
   }\r
 }\r
 \r
-//\r
-// Globals\r
-//\r
+/**\r
+  Debug Port Driver entry point.\r
 \r
-EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {\r
-  DebugPortSupported,\r
-  DebugPortStart,\r
-  DebugPortStop,\r
-  DEBUGPORT_DRIVER_VERSION,\r
-  NULL,\r
-  NULL\r
-};\r
+  Reads DebugPort variable to determine what device and settings to use as the\r
+  debug port.  Binds exclusively to SerialIo. Reverts to defaults if no variable\r
+  is found.\r
 \r
-DEBUGPORT_DEVICE  *gDebugPortDevice;\r
+  @param[in] ImageHandle       The firmware allocated handle for the EFI image.\r
+  @param[in] SystemTable       A pointer to the EFI System Table.\r
 \r
-//\r
-// implementation code\r
-//\r
+  @retval EFI_SUCCESS          The entry point is executed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
+  @retval other                Some error occurs when executing this entry point.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 InitializeDebugPortDriver (\r
   IN EFI_HANDLE             ImageHandle,\r
   IN EFI_SYSTEM_TABLE       *SystemTable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Driver entry point.  Reads DebugPort variable to determine what device and settings\r
-  to use as the debug port.  Binds exclusively to SerialIo. Reverts to defaults \\r
-  if no variable is found.  \r
-  \r
-  Creates debugport and devicepath protocols on new handle.\r
-\r
-Arguments:\r
-  ImageHandle,\r
-  SystemTable\r
-\r
-Returns:\r
-\r
-  EFI_UNSUPPORTED\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
   EFI_STATUS    Status;\r
 \r
@@ -174,78 +141,49 @@ Returns:
              &gDebugPortComponentName2\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
-  //\r
-  // Allocate and Initialize dev structure\r
-  //\r
-  gDebugPortDevice = AllocateZeroPool (sizeof (DEBUGPORT_DEVICE));\r
-  if (gDebugPortDevice == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Fill in static and default pieces of device structure first.\r
-  //\r
-  gDebugPortDevice->Signature = DEBUGPORT_DEVICE_SIGNATURE;\r
-\r
-  gDebugPortDevice->DebugPortInterface.Reset = DebugPortReset;\r
-  gDebugPortDevice->DebugPortInterface.Read = DebugPortRead;\r
-  gDebugPortDevice->DebugPortInterface.Write = DebugPortWrite;\r
-  gDebugPortDevice->DebugPortInterface.Poll = DebugPortPoll;\r
-\r
-  gDebugPortDevice->BaudRate = DEBUGPORT_UART_DEFAULT_BAUDRATE;\r
-  gDebugPortDevice->ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
-  gDebugPortDevice->Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
-  gDebugPortDevice->Parity = (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY;\r
-  gDebugPortDevice->DataBits = DEBUGPORT_UART_DEFAULT_DATA_BITS;\r
-  gDebugPortDevice->StopBits = (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS;\r
 \r
   return EFI_SUCCESS;\r
 }\r
-//\r
-// DebugPort driver binding member functions...\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-DebugPortSupported (\r
-  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
-  IN EFI_HANDLE                     ControllerHandle,\r
-  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
-  )\r
-/*++\r
 \r
-Routine Description:\r
-  Checks to see that there's not already a DebugPort interface somewhere.  If so,\r
-  fail.\r
-  \r
+/**\r
+  Checks to see if there's not already a DebugPort interface somewhere.\r
+\r
   If there's a DEBUGPORT variable, the device path must match exactly.  If there's\r
   no DEBUGPORT variable, then device path is not checked and does not matter.\r
-  \r
   Checks to see that there's a serial io interface on the controller handle\r
   that can be bound BY_DRIVER | EXCLUSIVE.\r
-  \r
   If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED\r
   or other error returned by OpenProtocol.\r
 \r
-Arguments:\r
-  This\r
-  ControllerHandle\r
-  RemainingDevicePath\r
-  \r
-Returns:\r
-  EFI_UNSUPPORTED\r
-  EFI_OUT_OF_RESOURCES\r
-  EFI_SUCCESS\r
-  \r
---*/\r
+  @param  This                 Protocol instance pointer.\r
+  @param  ControllerHandle     Handle of device to test.\r
+  @param  RemainingDevicePath  Optional parameter use to pick a specific child\r
+                               device to start.\r
+\r
+  @retval EFI_SUCCESS          This driver supports this device.\r
+  @retval EFI_UNSUPPORTED      Debug Port device is not supported.\r
+  @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
+  @retval others               Some error occurs.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DebugPortSupported (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL    *This,\r
+  IN EFI_HANDLE                     ControllerHandle,\r
+  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
+  )\r
 {\r
   EFI_STATUS                Status;\r
-  EFI_DEVICE_PATH_PROTOCOL  *Dp1;\r
-  EFI_DEVICE_PATH_PROTOCOL  *Dp2;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *DebugPortVariable;\r
   EFI_SERIAL_IO_PROTOCOL    *SerialIo;\r
   EFI_DEBUGPORT_PROTOCOL    *DebugPortInterface;\r
   EFI_HANDLE                TempHandle;\r
 \r
   //\r
-  // Check to see that there's not a debugport protocol already published\r
+  // Check to see that there's not a debugport protocol already published,\r
+  // since only one standard UART serial port could be supported by this driver.\r
   //\r
   if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {\r
     return EFI_UNSUPPORTED;\r
@@ -253,25 +191,19 @@ Returns:
   //\r
   // Read DebugPort variable to determine debug port selection and parameters\r
   //\r
-  GetDebugPortVariable (gDebugPortDevice);\r
+  DebugPortVariable = GetDebugPortVariable ();\r
 \r
-  if (gDebugPortDevice->DebugPortVariable != NULL) {\r
+  if (DebugPortVariable != NULL) {\r
     //\r
     // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if\r
     // the closest matching handle matches the controller handle, and if it does,\r
     // check to see that the remaining device path has the DebugPort GUIDed messaging\r
     // device path only.  Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.\r
     //\r
-    Dp1 = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) gDebugPortDevice->DebugPortVariable);\r
-    if (Dp1 == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-\r
-    Dp2 = Dp1;\r
-\r
+    DevicePath = DebugPortVariable;\r
     Status = gBS->LocateDevicePath (\r
                     &gEfiSerialIoProtocolGuid,\r
-                    &Dp2,\r
+                    &DevicePath,\r
                     &TempHandle\r
                     );\r
 \r
@@ -279,15 +211,19 @@ Returns:
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    if (Status == EFI_SUCCESS && (Dp2->Type != 3 || Dp2->SubType != 10 || *((UINT16 *) Dp2->Length) != 20)) {\r
+    if (Status == EFI_SUCCESS &&\r
+        (DevicePath->Type != MESSAGING_DEVICE_PATH ||\r
+         DevicePath->SubType != MSG_VENDOR_DP ||\r
+         *((UINT16 *) DevicePath->Length) != sizeof (DEBUGPORT_DEVICE_PATH))) {\r
+\r
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    if (Status == EFI_SUCCESS && CompareMem (&gEfiDebugPortDevicePathGuid, Dp2 + 1, sizeof (EFI_GUID))) {\r
+    if (Status == EFI_SUCCESS && !CompareGuid (&gEfiDebugPortDevicePathGuid, (GUID *) (DevicePath + 1))) {\r
       Status = EFI_UNSUPPORTED;\r
     }\r
 \r
-    FreePool (Dp1);\r
+    FreePool (DebugPortVariable);\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -315,6 +251,20 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Binds exclusively to serial io on the controller handle, Produces DebugPort\r
+  protocol and DevicePath on new handle.\r
+\r
+  @param  This                 Protocol instance pointer.\r
+  @param  ControllerHandle     Handle of device to bind driver to.\r
+  @param  RemainingDevicePath  Optional parameter use to pick a specific child\r
+                               device to start.\r
+\r
+  @retval EFI_SUCCESS          This driver is added to ControllerHandle.\r
+  @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
+  @retval others               Some error occurs.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortStart (\r
@@ -322,21 +272,6 @@ DebugPortStart (
   IN EFI_HANDLE                     ControllerHandle,\r
   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Binds exclusively to serial io on the controller handle.  Produces DebugPort\r
-  protocol and DevicePath on new handle.\r
-\r
-Arguments:\r
-  This\r
-  ControllerHandle\r
-  RemainingDevicePath\r
-  \r
-Returns:\r
-  EFI_OUT_OF_RESOURCES\r
-  EFI_SUCCESS\r
---*/\r
 {\r
   EFI_STATUS                Status;\r
   DEBUGPORT_DEVICE_PATH     DebugPortDP;\r
@@ -346,7 +281,7 @@ Returns:
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiSerialIoProtocolGuid,\r
-                  (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+                  (VOID **) &mDebugPortDevice.SerialIoBinding,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
@@ -355,34 +290,34 @@ Returns:
     return Status;\r
   }\r
 \r
-  gDebugPortDevice->SerialIoDeviceHandle = ControllerHandle;\r
+  mDebugPortDevice.SerialIoDeviceHandle = ControllerHandle;\r
 \r
   //\r
   // Initialize the Serial Io interface...\r
   //\r
-  Status = gDebugPortDevice->SerialIoBinding->SetAttributes (\r
-                                                gDebugPortDevice->SerialIoBinding,\r
-                                                gDebugPortDevice->BaudRate,\r
-                                                gDebugPortDevice->ReceiveFifoDepth,\r
-                                                gDebugPortDevice->Timeout,\r
-                                                gDebugPortDevice->Parity,\r
-                                                gDebugPortDevice->DataBits,\r
-                                                gDebugPortDevice->StopBits\r
+  Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
+                                                mDebugPortDevice.SerialIoBinding,\r
+                                                mDebugPortDevice.BaudRate,\r
+                                                mDebugPortDevice.ReceiveFifoDepth,\r
+                                                mDebugPortDevice.Timeout,\r
+                                                mDebugPortDevice.Parity,\r
+                                                mDebugPortDevice.DataBits,\r
+                                                mDebugPortDevice.StopBits\r
                                                 );\r
   if (EFI_ERROR (Status)) {\r
-    gDebugPortDevice->BaudRate          = 0;\r
-    gDebugPortDevice->Parity            = DefaultParity;\r
-    gDebugPortDevice->DataBits          = 0;\r
-    gDebugPortDevice->StopBits          = DefaultStopBits;\r
-    gDebugPortDevice->ReceiveFifoDepth  = 0;\r
-    Status = gDebugPortDevice->SerialIoBinding->SetAttributes (\r
-                                                  gDebugPortDevice->SerialIoBinding,\r
-                                                  gDebugPortDevice->BaudRate,\r
-                                                  gDebugPortDevice->ReceiveFifoDepth,\r
-                                                  gDebugPortDevice->Timeout,\r
-                                                  gDebugPortDevice->Parity,\r
-                                                  gDebugPortDevice->DataBits,\r
-                                                  gDebugPortDevice->StopBits\r
+    mDebugPortDevice.BaudRate          = 0;\r
+    mDebugPortDevice.Parity            = DefaultParity;\r
+    mDebugPortDevice.DataBits          = 0;\r
+    mDebugPortDevice.StopBits          = DefaultStopBits;\r
+    mDebugPortDevice.ReceiveFifoDepth  = 0;\r
+    Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
+                                                  mDebugPortDevice.SerialIoBinding,\r
+                                                  mDebugPortDevice.BaudRate,\r
+                                                  mDebugPortDevice.ReceiveFifoDepth,\r
+                                                  mDebugPortDevice.Timeout,\r
+                                                  mDebugPortDevice.Parity,\r
+                                                  mDebugPortDevice.DataBits,\r
+                                                  mDebugPortDevice.StopBits\r
                                                   );\r
     if (EFI_ERROR (Status)) {\r
       gBS->CloseProtocol (\r
@@ -395,7 +330,7 @@ Returns:
     }\r
   }\r
 \r
-  gDebugPortDevice->SerialIoBinding->Reset (gDebugPortDevice->SerialIoBinding);\r
+  mDebugPortDevice.SerialIoBinding->Reset (mDebugPortDevice.SerialIoBinding);\r
 \r
   //\r
   // Create device path instance for DebugPort\r
@@ -403,7 +338,7 @@ Returns:
   DebugPortDP.Header.Type     = MESSAGING_DEVICE_PATH;\r
   DebugPortDP.Header.SubType  = MSG_VENDOR_DP;\r
   SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));\r
-  CopyMem (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid, sizeof (EFI_GUID));\r
+  CopyGuid (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid);\r
 \r
   Dp1 = DevicePathFromHandle (ControllerHandle);\r
   if (Dp1 == NULL) {\r
@@ -411,19 +346,19 @@ Returns:
     SetDevicePathEndNode (Dp1);\r
   }\r
 \r
-  gDebugPortDevice->DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
-  if (gDebugPortDevice->DebugPortDevicePath == NULL) {\r
+  mDebugPortDevice.DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
+  if (mDebugPortDevice.DebugPortDevicePath == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
   // Publish DebugPort and Device Path protocols\r
   //\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &gDebugPortDevice->DebugPortDeviceHandle,\r
+                  &mDebugPortDevice.DebugPortDeviceHandle,\r
                   &gEfiDevicePathProtocolGuid,\r
-                  gDebugPortDevice->DebugPortDevicePath,\r
+                  mDebugPortDevice.DebugPortDevicePath,\r
                   &gEfiDebugPortProtocolGuid,\r
-                  &gDebugPortDevice->DebugPortInterface,\r
+                  &mDebugPortDevice.DebugPortInterface,\r
                   NULL\r
                   );\r
 \r
@@ -442,25 +377,13 @@ Returns:
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiSerialIoProtocolGuid,\r
-                  (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+                  (VOID **) &mDebugPortDevice.SerialIoBinding,\r
                   This->DriverBindingHandle,\r
-                  gDebugPortDevice->DebugPortDeviceHandle,\r
+                  mDebugPortDevice.DebugPortDeviceHandle,\r
                   EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    DEBUG_CODE_BEGIN ();\r
-      UINTN  BufferSize;\r
-\r
-      BufferSize = 48;\r
-      DebugPortWrite (\r
-        &gDebugPortDevice->DebugPortInterface,\r
-        0,\r
-        &BufferSize,\r
-        "DebugPort driver failed to open child controller\n\n"\r
-        );\r
-    DEBUG_CODE_END ();\r
-\r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
           &gEfiSerialIoProtocolGuid,\r
@@ -470,21 +393,23 @@ Returns:
     return Status;\r
   }\r
 \r
-  DEBUG_CODE_BEGIN ();\r
-    UINTN  BufferSize;\r
-\r
-    BufferSize = 38;\r
-    DebugPortWrite (\r
-      &gDebugPortDevice->DebugPortInterface,\r
-      0,\r
-      &BufferSize,\r
-      "Hello World from the DebugPort driver\n\n"\r
-      );\r
-  DEBUG_CODE_END ();\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Stop this driver on ControllerHandle by removing Serial IO protocol on\r
+  the ControllerHandle.\r
+\r
+  @param  This              Protocol instance pointer.\r
+  @param  ControllerHandle  Handle of device to stop driver on\r
+  @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of\r
+                            children is zero stop the entire bus driver.\r
+  @param  ChildHandleBuffer List of Child Handles to Stop.\r
+\r
+  @retval EFI_SUCCESS       This driver is removed ControllerHandle.\r
+  @retval other             This driver was not removed from this device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortStop (\r
@@ -493,20 +418,6 @@ DebugPortStop (
   IN  UINTN                          NumberOfChildren,\r
   IN  EFI_HANDLE                     *ChildHandleBuffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  We're never intending to be stopped via the driver model so this just returns\r
-  EFI_UNSUPPORTED\r
-\r
-Arguments:\r
-  Per UEFI 2.0 driver model\r
-  \r
-Returns:\r
-  EFI_UNSUPPORTED\r
-  EFI_SUCCESS\r
-  \r
---*/\r
 {\r
   EFI_STATUS  Status;\r
 \r
@@ -521,7 +432,7 @@ Returns:
           ControllerHandle\r
           );\r
 \r
-    gDebugPortDevice->SerialIoBinding = NULL;\r
+    mDebugPortDevice.SerialIoBinding = NULL;\r
 \r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
@@ -530,7 +441,7 @@ Returns:
           ControllerHandle\r
           );\r
 \r
-    FreePool (gDebugPortDevice->DebugPortDevicePath);\r
+    FreePool (mDebugPortDevice.DebugPortDevicePath);\r
 \r
     return EFI_SUCCESS;\r
   } else {\r
@@ -538,10 +449,10 @@ Returns:
     // Disconnect SerialIo child handle\r
     //\r
     Status = gBS->CloseProtocol (\r
-                    gDebugPortDevice->SerialIoDeviceHandle,\r
+                    mDebugPortDevice.SerialIoDeviceHandle,\r
                     &gEfiSerialIoProtocolGuid,\r
                     This->DriverBindingHandle,\r
-                    gDebugPortDevice->DebugPortDeviceHandle\r
+                    mDebugPortDevice.DebugPortDeviceHandle\r
                     );\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -551,11 +462,11 @@ Returns:
     // Unpublish our protocols (DevicePath, DebugPort)\r
     //\r
     Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                    gDebugPortDevice->DebugPortDeviceHandle,\r
+                    mDebugPortDevice.DebugPortDeviceHandle,\r
                     &gEfiDevicePathProtocolGuid,\r
-                    gDebugPortDevice->DebugPortDevicePath,\r
+                    mDebugPortDevice.DebugPortDevicePath,\r
                     &gEfiDebugPortProtocolGuid,\r
-                    &gDebugPortDevice->DebugPortInterface,\r
+                    &mDebugPortDevice.DebugPortInterface,\r
                     NULL\r
                     );\r
 \r
@@ -563,44 +474,37 @@ Returns:
       gBS->OpenProtocol (\r
             ControllerHandle,\r
             &gEfiSerialIoProtocolGuid,\r
-            (VOID **) &gDebugPortDevice->SerialIoBinding,\r
+            (VOID **) &mDebugPortDevice.SerialIoBinding,\r
             This->DriverBindingHandle,\r
-            gDebugPortDevice->DebugPortDeviceHandle,\r
+            mDebugPortDevice.DebugPortDeviceHandle,\r
             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
             );\r
     } else {\r
-      gDebugPortDevice->DebugPortDeviceHandle = NULL;\r
+      mDebugPortDevice.DebugPortDeviceHandle = NULL;\r
     }\r
   }\r
 \r
   return Status;\r
 }\r
-//\r
-// Debugport protocol member functions\r
-//\r
-EFI_STATUS\r
-EFIAPI\r
-DebugPortReset (\r
-  IN EFI_DEBUGPORT_PROTOCOL   *This\r
-  )\r
-/*++\r
 \r
-Routine Description:\r
+/**\r
   DebugPort protocol member function.  Calls SerialIo:GetControl to flush buffer.\r
   We cannot call SerialIo:SetAttributes because it uses pool services, which use\r
   locks, which affect TPL, so it's not interrupt context safe or re-entrant.\r
   SerialIo:Reset() calls SetAttributes, so it can't be used either.\r
-  \r
-  The port itself should be fine since it was set up during initialization.  \r
-  \r
-Arguments:\r
-  This\r
 \r
-Returns:\r
+  The port itself should be fine since it was set up during initialization.\r
+\r
+  @param  This              Protocol instance pointer.\r
 \r
-  EFI_SUCCESS\r
+  @return EFI_SUCCESS       Always.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DebugPortReset (\r
+  IN EFI_DEBUGPORT_PROTOCOL   *This\r
+  )\r
 {\r
   UINTN             BufferSize;\r
   UINTN             BitBucket;\r
@@ -613,6 +517,20 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  DebugPort protocol member function.  Calls SerialIo:Read() after setting\r
+  if it's different than the last SerialIo access.\r
+\r
+  @param  This                Pointer to DebugPort protocol.\r
+  @param  Timeout             Timeout value.\r
+  @param  BufferSize          On input, the size of Buffer.\r
+                              On output, the amount of data actually written.\r
+  @param  Buffer              Pointer to buffer to read.\r
+\r
+  @retval EFI_SUCCESS\r
+  @retval others\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortRead (\r
@@ -621,23 +539,6 @@ DebugPortRead (
   IN OUT UINTN                *BufferSize,\r
   IN VOID                     *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  DebugPort protocol member function.  Calls SerialIo:Read() after setting\r
-  if it's different than the last SerialIo access.\r
-  \r
-Arguments:\r
-  IN EFI_DEBUGPORT_PROTOCOL   *This\r
-  IN UINT32                   Timeout,\r
-  IN OUT UINTN                *BufferSize,\r
-  IN VOID                     *Buffer\r
-\r
-Returns:\r
-\r
-  EFI_STATUS\r
-\r
---*/\r
 {\r
   DEBUGPORT_DEVICE  *DebugPortDevice;\r
   UINTN             LocalBufferSize;\r
@@ -647,6 +548,7 @@ Returns:
   DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
   BufferPtr       = Buffer;\r
   LocalBufferSize = *BufferSize;\r
+\r
   do {\r
     Status = DebugPortDevice->SerialIoBinding->Read (\r
                                                 DebugPortDevice->SerialIoBinding,\r
@@ -672,6 +574,21 @@ Returns:
   return Status;\r
 }\r
 \r
+/**\r
+  DebugPort protocol member function.  Calls SerialIo:Write() Writes 8 bytes at\r
+  a time and does a GetControl between 8 byte writes to help insure reads are\r
+  interspersed This is poor-man's flow control.\r
+\r
+  @param  This                Pointer to DebugPort protocol.\r
+  @param  Timeout             Timeout value.\r
+  @param  BufferSize          On input, the size of Buffer.\r
+                              On output, the amount of data actually written.\r
+  @param  Buffer              Pointer to buffer to read.\r
+\r
+  @retval EFI_SUCCESS         The data was written.\r
+  @retval others              Fails when writting datas to debug port device.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortWrite (\r
@@ -680,26 +597,6 @@ DebugPortWrite (
   IN OUT UINTN                *BufferSize,\r
   OUT VOID                    *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  DebugPort protocol member function.  Calls SerialIo:Write() Writes 8 bytes at\r
-  a time and does a GetControl between 8 byte writes to help insure reads are\r
-  interspersed This is poor-man's flow control..\r
-  \r
-Arguments:\r
-  This               - Pointer to DebugPort protocol\r
-  Timeout            - Timeout value\r
-  BufferSize         - On input, the size of Buffer. \r
-                       On output, the amount of data actually written.\r
-  Buffer             - Pointer to buffer to write\r
-\r
-Returns:\r
-  EFI_SUCCESS        - The data was written.\r
-  EFI_DEVICE_ERROR   - The device reported an error.\r
-  EFI_TIMEOUT        - The data write was stopped due to a timeout.\r
-\r
---*/\r
 {\r
   DEBUGPORT_DEVICE  *DebugPortDevice;\r
   UINTN             Position;\r
@@ -731,26 +628,24 @@ Returns:
   return Status;\r
 }\r
 \r
+/**\r
+  DebugPort protocol member function.  Calls SerialIo:Write() after setting\r
+  if it's different than the last SerialIo access.\r
+\r
+  @param  This                Pointer to DebugPort protocol.\r
+\r
+  @retval EFI_SUCCESS         At least 1 character is ready to be read from\r
+                              the DebugPort interface.\r
+  @retval EFI_NOT_READY       There are no characters ready to read from the\r
+                              DebugPort interface\r
+  @retval EFI_DEVICE_ERROR    A hardware failure occured... (from SerialIo)\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 DebugPortPoll (\r
   IN EFI_DEBUGPORT_PROTOCOL   *This\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  DebugPort protocol member function.  Calls SerialIo:Write() after setting\r
-  if it's different than the last SerialIo access.\r
-  \r
-Arguments:\r
-  IN EFI_DEBUGPORT_PROTOCOL   *This\r
-\r
-Returns:\r
-  EFI_SUCCESS - At least 1 character is ready to be read from the DebugPort interface\r
-  EFI_NOT_READY - There are no characters ready to read from the DebugPort interface\r
-  EFI_DEVICE_ERROR - A hardware failure occured... (from SerialIo)\r
-\r
---*/\r
 {\r
   EFI_STATUS        Status;\r
   UINT32            SerialControl;\r
@@ -764,7 +659,7 @@ Returns:
                                               );\r
 \r
   if (!EFI_ERROR (Status)) {\r
-    if (SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) {\r
+    if ((SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) {\r
       Status = EFI_NOT_READY;\r
     } else {\r
       Status = EFI_SUCCESS;\r
@@ -774,53 +669,77 @@ Returns:
   return Status;\r
 }\r
 \r
-EFI_STATUS\r
-EFIAPI\r
-ImageUnloadHandler (\r
-  EFI_HANDLE ImageHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
   Unload function that is registered in the LoadImage protocol.  It un-installs\r
   protocols produced and deallocates pool used by the driver.  Called by the core\r
   when unloading the driver.\r
-  \r
-Arguments:\r
-  EFI_HANDLE ImageHandle\r
 \r
-Returns:\r
+  @param  ImageHandle\r
 \r
-  EFI_SUCCESS\r
+  @retval EFI_SUCCESS     Unload Debug Port driver successfully.\r
+  @retval EFI_ABORTED     Serial IO is still binding.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ImageUnloadHandler (\r
+  EFI_HANDLE ImageHandle\r
+  )\r
 {\r
   EFI_STATUS  Status;\r
+  VOID        *ComponentName;\r
+  VOID        *ComponentName2;\r
 \r
-  if (gDebugPortDevice->SerialIoBinding != NULL) {\r
+  if (mDebugPortDevice.SerialIoBinding != NULL) {\r
     return EFI_ABORTED;\r
   }\r
 \r
-  Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                  ImageHandle,\r
-                  &gEfiDriverBindingProtocolGuid,\r
-                  &gDebugPortDevice->DriverBindingInterface,\r
-                  &gEfiComponentNameProtocolGuid,\r
-                  &gDebugPortDevice->ComponentNameInterface,\r
-                  NULL\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
   //\r
-  // Clean up allocations\r
+  // Driver is stopped already.\r
   //\r
-  if (gDebugPortDevice->DebugPortVariable != NULL) {\r
-    FreePool (gDebugPortDevice->DebugPortVariable);\r
+  Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentNameProtocolGuid, &ComponentName);\r
+  if (EFI_ERROR (Status)) {\r
+    ComponentName = NULL;\r
   }\r
 \r
-  FreePool (gDebugPortDevice);\r
+  Status = gBS->HandleProtocol (ImageHandle, &gEfiComponentName2ProtocolGuid, &ComponentName2);\r
+  if (EFI_ERROR (Status)) {\r
+    ComponentName2 = NULL;\r
+  }\r
 \r
-  return EFI_SUCCESS;\r
+  if (ComponentName == NULL) {\r
+    if (ComponentName2 == NULL) {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      NULL\r
+                      );\r
+    } else {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                      NULL\r
+                      );\r
+    }\r
+  } else {\r
+    if (ComponentName2 == NULL) {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                      NULL\r
+                      );\r
+    } else {\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                      ImageHandle,\r
+                      &gEfiDriverBindingProtocolGuid,  &gDebugPortDriverBinding,\r
+                      &gEfiComponentNameProtocolGuid,  ComponentName,\r
+                      &gEfiComponentName2ProtocolGuid, ComponentName2,\r
+                      NULL\r
+                      );\r
+    }\r
+  }\r
+\r
+  return Status;\r
 }\r