]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Bus/Isa/IsaSerialDxe/Serial.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaSerialDxe / Serial.c
index 4a86257e73266d55186fe3f68b87d5e052c6d7a7..56c2d4dda7158fa654dbd32f0500187c3c47fe94 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Serial driver for standard UARTS on an ISA bus.\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2018, 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
@@ -41,13 +41,13 @@ SERIAL_DEV  gSerialDevTempate = {
     NULL\r
   },\r
   { // SerialMode\r
-    SERIAL_PORT_DEFAULT_CONTROL_MASK,\r
+    SERIAL_PORT_SUPPORT_CONTROL_MASK,\r
     SERIAL_PORT_DEFAULT_TIMEOUT,\r
-    FixedPcdGet64 (PcdUartDefaultBaudRate),     // BaudRate\r
+    0,\r
     SERIAL_PORT_DEFAULT_RECEIVE_FIFO_DEPTH,\r
-    FixedPcdGet8 (PcdUartDefaultDataBits),      // DataBits\r
-    FixedPcdGet8 (PcdUartDefaultParity),        // Parity\r
-    FixedPcdGet8 (PcdUartDefaultStopBits)       // StopBits\r
+    0,\r
+    0,\r
+    0\r
   },\r
   NULL,\r
   NULL,\r
@@ -61,10 +61,10 @@ SERIAL_DEV  gSerialDevTempate = {
       }\r
     },\r
     0,\r
-    FixedPcdGet64 (PcdUartDefaultBaudRate),    \r
-    FixedPcdGet8 (PcdUartDefaultDataBits),\r
-    FixedPcdGet8 (PcdUartDefaultParity),\r
-    FixedPcdGet8 (PcdUartDefaultStopBits)\r
+    0,\r
+    0,\r
+    0,\r
+    0\r
   },\r
   NULL,\r
   0,    //BaseAddress\r
@@ -86,12 +86,57 @@ SERIAL_DEV  gSerialDevTempate = {
   NULL\r
 };\r
 \r
+/**\r
+  Check the device path node whether it's the Flow Control node or not.\r
+\r
+  @param[in] FlowControl    The device path node to be checked.\r
+\r
+  @retval TRUE              It's the Flow Control node.\r
+  @retval FALSE             It's not.\r
+\r
+**/\r
+BOOLEAN\r
+IsUartFlowControlNode (\r
+  IN UART_FLOW_CONTROL_DEVICE_PATH *FlowControl\r
+  )\r
+{\r
+  return (BOOLEAN) (\r
+           (DevicePathType (FlowControl) == MESSAGING_DEVICE_PATH) &&\r
+           (DevicePathSubType (FlowControl) == MSG_VENDOR_DP) &&\r
+           (CompareGuid (&FlowControl->Guid, &gEfiUartDevicePathGuid))\r
+           );\r
+}\r
+\r
+/**\r
+  Check the device path node whether it contains Flow Control node or not.\r
+\r
+  @param[in] DevicePath     The device path to be checked.\r
+\r
+  @retval TRUE              It contains the Flow Control node.\r
+  @retval FALSE             It doesn't.\r
+\r
+**/\r
+BOOLEAN\r
+ContainsFlowControl (\r
+  IN EFI_DEVICE_PATH_PROTOCOL      *DevicePath\r
+  )\r
+{\r
+  while (!IsDevicePathEnd (DevicePath)) {\r
+    if (IsUartFlowControlNode ((UART_FLOW_CONTROL_DEVICE_PATH *) DevicePath)) {\r
+      return TRUE;\r
+    }\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   The user Entry Point for module IsaSerial. The user code starts with this function.\r
 \r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \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
   @retval EFI_SUCCESS       The entry point is executed successfully.\r
   @retval other             Some error occurs when executing this entry point.\r
 \r
@@ -118,6 +163,17 @@ InitializeIsaSerial (
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Initialize UART default setting in gSerialDevTempate\r
+  //\r
+  gSerialDevTempate.SerialMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
+  gSerialDevTempate.SerialMode.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
+  gSerialDevTempate.SerialMode.Parity   = PcdGet8 (PcdUartDefaultParity);\r
+  gSerialDevTempate.SerialMode.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
+  gSerialDevTempate.UartDevicePath.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
+  gSerialDevTempate.UartDevicePath.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
+  gSerialDevTempate.UartDevicePath.Parity   = PcdGet8 (PcdUartDefaultParity);\r
+  gSerialDevTempate.UartDevicePath.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
 \r
   return Status;\r
 }\r
@@ -144,46 +200,154 @@ SerialControllerDriverSupported (
   EFI_STATUS                                Status;\r
   EFI_DEVICE_PATH_PROTOCOL                  *ParentDevicePath;\r
   EFI_ISA_IO_PROTOCOL                       *IsaIo;\r
-  UART_DEVICE_PATH                          UartNode;\r
+  UART_DEVICE_PATH                          *UartNode;\r
+  EFI_DEVICE_PATH_PROTOCOL                  *DevicePath;\r
+  UART_FLOW_CONTROL_DEVICE_PATH             *FlowControlNode;\r
+  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY       *OpenInfoBuffer;\r
+  UINTN                                     EntryCount;\r
+  UINTN                                     Index;\r
+  BOOLEAN                                   HasFlowControl;\r
 \r
   //\r
-  // Ignore the RemainingDevicePath\r
+  // Check RemainingDevicePath validation\r
   //\r
+  if (RemainingDevicePath != NULL) {\r
+    //\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
+      //\r
+      // If RemainingDevicePath isn't the End of Device Path Node,\r
+      // check its validation\r
+      //\r
+      Status = EFI_UNSUPPORTED;\r
+\r
+      UartNode = (UART_DEVICE_PATH *) RemainingDevicePath;\r
+      if (UartNode->Header.Type != MESSAGING_DEVICE_PATH ||\r
+          UartNode->Header.SubType != MSG_UART_DP ||\r
+          sizeof (UART_DEVICE_PATH) != DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *) UartNode)\r
+                                        ) {\r
+        goto Error;\r
+      }\r
+\r
+      if (UartNode->BaudRate > SERIAL_PORT_MAX_BAUD_RATE) {\r
+        goto Error;\r
+      }\r
+\r
+      if (UartNode->Parity < NoParity || UartNode->Parity > SpaceParity) {\r
+        goto Error;\r
+      }\r
+\r
+      if (UartNode->DataBits < 5 || UartNode->DataBits > 8) {\r
+        goto Error;\r
+      }\r
+\r
+      if (UartNode->StopBits < OneStopBit || UartNode->StopBits > TwoStopBits) {\r
+        goto Error;\r
+      }\r
+\r
+      if ((UartNode->DataBits == 5) && (UartNode->StopBits == TwoStopBits)) {\r
+        goto Error;\r
+      }\r
+\r
+      if ((UartNode->DataBits >= 6) && (UartNode->DataBits <= 8) && (UartNode->StopBits == OneFiveStopBits)) {\r
+        goto Error;\r
+      }\r
+\r
+      FlowControlNode = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (UartNode);\r
+      if (IsUartFlowControlNode (FlowControlNode)) {\r
+        //\r
+        // If the second node is Flow Control Node,\r
+        //   return error when it request other than hardware flow control.\r
+        //\r
+        if ((ReadUnaligned32 (&FlowControlNode->FlowControlMap) & ~UART_FLOW_CONTROL_HARDWARE) != 0) {\r
+          goto Error;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   //\r
   // Open the IO Abstraction(s) needed to perform the supported test\r
   //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID **) &ParentDevicePath,\r
+                  &gEfiIsaIoProtocolGuid,\r
+                  (VOID **) &IsaIo,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
   if (Status == EFI_ALREADY_STARTED) {\r
-    return EFI_SUCCESS;\r
+    if (RemainingDevicePath == NULL || IsDevicePathEnd (RemainingDevicePath)) {\r
+      //\r
+      // If RemainingDevicePath is NULL or is the End of Device Path Node\r
+      //\r
+      return EFI_SUCCESS;\r
+    }\r
+    //\r
+    // When the driver has produced device path with flow control node but RemainingDevicePath only contains UART node,\r
+    //   return unsupported, and vice versa.\r
+    //\r
+    Status = gBS->OpenProtocolInformation (\r
+                    Controller,\r
+                    &gEfiIsaIoProtocolGuid,\r
+                    &OpenInfoBuffer,\r
+                    &EntryCount\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    for (Index = 0; Index < EntryCount; Index++) {\r
+      if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
+        Status = gBS->OpenProtocol (\r
+                        OpenInfoBuffer[Index].ControllerHandle,\r
+                        &gEfiDevicePathProtocolGuid,\r
+                        (VOID **) &DevicePath,\r
+                        This->DriverBindingHandle,\r
+                        Controller,\r
+                        EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                        );\r
+        if (!EFI_ERROR (Status)) {\r
+          HasFlowControl = ContainsFlowControl (RemainingDevicePath);\r
+          if (HasFlowControl ^ ContainsFlowControl (DevicePath)) {\r
+            Status = EFI_UNSUPPORTED;\r
+          }\r
+        }\r
+        break;\r
+      }\r
+    }\r
+    FreePool (OpenInfoBuffer);\r
+    return Status;\r
   }\r
 \r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
+  //\r
+  // Close the I/O Abstraction(s) used to perform the supported test\r
+  //\r
   gBS->CloseProtocol (\r
          Controller,\r
-         &gEfiDevicePathProtocolGuid,\r
+         &gEfiIsaIoProtocolGuid,\r
          This->DriverBindingHandle,\r
          Controller\r
          );\r
 \r
+  //\r
+  // Open the EFI Device Path protocol needed to perform the supported test\r
+  //\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
-                  &gEfiIsaIoProtocolGuid,\r
-                  (VOID **) &IsaIo,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &ParentDevicePath,\r
                   This->DriverBindingHandle,\r
                   Controller,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
-\r
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
   }\r
@@ -200,57 +364,14 @@ SerialControllerDriverSupported (
     Status = EFI_UNSUPPORTED;\r
     goto Error;\r
   }\r
-  //\r
-  // Make sure RemainingDevicePath is valid\r
-  //\r
-  if (RemainingDevicePath != NULL) {\r
-    Status = EFI_UNSUPPORTED;\r
-    CopyMem (\r
-      &UartNode,\r
-      (UART_DEVICE_PATH *) RemainingDevicePath,\r
-      sizeof (UART_DEVICE_PATH)\r
-      );\r
-    if (UartNode.Header.Type != MESSAGING_DEVICE_PATH ||\r
-        UartNode.Header.SubType != MSG_UART_DP ||\r
-        sizeof (UART_DEVICE_PATH) != DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *) &UartNode)\r
-                                      ) {\r
-      goto Error;\r
-    }\r
-\r
-    if (UartNode.BaudRate > SERIAL_PORT_MAX_BAUD_RATE) {\r
-      goto Error;\r
-    }\r
-\r
-    if (UartNode.Parity < NoParity || UartNode.Parity > SpaceParity) {\r
-      goto Error;\r
-    }\r
-\r
-    if (UartNode.DataBits < 5 || UartNode.DataBits > 8) {\r
-      goto Error;\r
-    }\r
-\r
-    if (UartNode.StopBits < OneStopBit || UartNode.StopBits > TwoStopBits) {\r
-      goto Error;\r
-    }\r
-\r
-    if ((UartNode.DataBits == 5) && (UartNode.StopBits == TwoStopBits)) {\r
-      goto Error;\r
-    }\r
-\r
-    if ((UartNode.DataBits >= 6) && (UartNode.DataBits <= 8) && (UartNode.StopBits == OneFiveStopBits)) {\r
-      goto Error;\r
-    }\r
-\r
-    Status = EFI_SUCCESS;\r
-  }\r
 \r
 Error:\r
   //\r
-  // Close the I/O Abstraction(s) used to perform the supported test\r
+  // Close protocol, don't use device path protocol in the Support() function\r
   //\r
   gBS->CloseProtocol (\r
          Controller,\r
-         &gEfiIsaIoProtocolGuid,\r
+         &gEfiDevicePathProtocolGuid,\r
          This->DriverBindingHandle,\r
          Controller\r
          );\r
@@ -281,11 +402,15 @@ SerialControllerDriverStart (
   EFI_ISA_IO_PROTOCOL                 *IsaIo;\r
   SERIAL_DEV                          *SerialDevice;\r
   UINTN                               Index;\r
-  UART_DEVICE_PATH                    Node;\r
   EFI_DEVICE_PATH_PROTOCOL            *ParentDevicePath;\r
   EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r
   UINTN                               EntryCount;\r
   EFI_SERIAL_IO_PROTOCOL              *SerialIo;\r
+  UART_DEVICE_PATH                    *Uart;\r
+  UINT32                              FlowControlMap;\r
+  UART_FLOW_CONTROL_DEVICE_PATH       *FlowControl;\r
+  EFI_DEVICE_PATH_PROTOCOL            *TempDevicePath;\r
+  UINT32                              Control;\r
 \r
   SerialDevice = NULL;\r
   //\r
@@ -328,9 +453,13 @@ SerialControllerDriverStart (
 \r
   if (Status == EFI_ALREADY_STARTED) {\r
 \r
-    if (RemainingDevicePath == NULL) {\r
+    if (RemainingDevicePath == NULL || IsDevicePathEnd (RemainingDevicePath)) {\r
+      //\r
+      // If RemainingDevicePath is NULL or is the End of Device Path Node\r
+      //\r
       return EFI_SUCCESS;\r
     }\r
+\r
     //\r
     // Make sure a child handle does not already exist.  This driver can only\r
     // produce one child per serial port.\r
@@ -347,7 +476,7 @@ SerialControllerDriverStart (
 \r
     Status = EFI_ALREADY_STARTED;\r
     for (Index = 0; Index < EntryCount; Index++) {\r
-      if (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {\r
+      if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
         Status = gBS->OpenProtocol (\r
                         OpenInfoBuffer[Index].ControllerHandle,\r
                         &gEfiSerialIoProtocolGuid,\r
@@ -357,24 +486,54 @@ SerialControllerDriverStart (
                         EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                         );\r
         if (!EFI_ERROR (Status)) {\r
-          CopyMem (&Node, RemainingDevicePath, sizeof (UART_DEVICE_PATH));\r
+          Uart   = (UART_DEVICE_PATH *) RemainingDevicePath;\r
           Status = SerialIo->SetAttributes (\r
                                SerialIo,\r
-                               Node.BaudRate,\r
+                               Uart->BaudRate,\r
                                SerialIo->Mode->ReceiveFifoDepth,\r
                                SerialIo->Mode->Timeout,\r
-                               (EFI_PARITY_TYPE) Node.Parity,\r
-                               Node.DataBits,\r
-                               (EFI_STOP_BITS_TYPE) Node.StopBits\r
+                               (EFI_PARITY_TYPE) Uart->Parity,\r
+                               Uart->DataBits,\r
+                               (EFI_STOP_BITS_TYPE) Uart->StopBits\r
                                );\r
+\r
+          FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (Uart);\r
+          if (!EFI_ERROR (Status) && IsUartFlowControlNode (FlowControl)) {\r
+            Status = SerialIo->GetControl (SerialIo, &Control);\r
+            if (!EFI_ERROR (Status)) {\r
+              if (ReadUnaligned32 (&FlowControl->FlowControlMap) == UART_FLOW_CONTROL_HARDWARE) {\r
+                Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;\r
+              } else {\r
+                Control &= ~EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;\r
+              }\r
+              //\r
+              // Clear the bits that are not allowed to pass to SetControl\r
+              //\r
+              Control &= (EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINAL_READY |\r
+                          EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE | EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE |\r
+                          EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE);\r
+              Status = SerialIo->SetControl (SerialIo, Control);\r
+            }\r
+          }\r
         }\r
         break;\r
       }\r
     }\r
 \r
-    gBS->FreePool (OpenInfoBuffer);\r
+    FreePool (OpenInfoBuffer);\r
     return Status;\r
   }\r
+\r
+  if (RemainingDevicePath != NULL) {\r
+    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
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
   //\r
   // Initialize the serial device instance\r
   //\r
@@ -387,6 +546,29 @@ SerialControllerDriverStart (
   SerialDevice->SerialIo.Mode       = &(SerialDevice->SerialMode);\r
   SerialDevice->IsaIo               = IsaIo;\r
   SerialDevice->ParentDevicePath    = ParentDevicePath;\r
+  FlowControl                       = NULL;\r
+  FlowControlMap                    = 0;\r
+\r
+  //\r
+  // Check if RemainingDevicePath is NULL,\r
+  // if yes, use the values from the gSerialDevTempate as no remaining device path was\r
+  // passed in.\r
+  //\r
+  if (RemainingDevicePath != NULL) {\r
+    //\r
+    // If RemainingDevicePath isn't NULL,\r
+    // match the configuration of the RemainingDevicePath. IsHandleSupported()\r
+    // already checked to make sure the RemainingDevicePath contains settings\r
+    // that we can support.\r
+    //\r
+    CopyMem (&SerialDevice->UartDevicePath, RemainingDevicePath, sizeof (UART_DEVICE_PATH));\r
+    FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) NextDevicePathNode (RemainingDevicePath);\r
+    if (IsUartFlowControlNode (FlowControl)) {\r
+      FlowControlMap = ReadUnaligned32 (&FlowControl->FlowControlMap);\r
+    } else {\r
+      FlowControl    = NULL;\r
+    }\r
+  }\r
 \r
   AddName (SerialDevice, IsaIo);\r
 \r
@@ -395,6 +577,9 @@ SerialControllerDriverStart (
       SerialDevice->BaseAddress = (UINT16) SerialDevice->IsaIo->ResourceList->ResourceItem[Index].StartRange;\r
     }\r
   }\r
+\r
+  SerialDevice->HardwareFlowControl = (BOOLEAN) (FlowControlMap == UART_FLOW_CONTROL_HARDWARE);\r
+\r
   //\r
   // Report status code the serial present\r
   //\r
@@ -414,30 +599,30 @@ SerialControllerDriverStart (
     goto Error;\r
   }\r
 \r
-  if (RemainingDevicePath != NULL) {\r
-    //\r
-    // Match the configuration of the RemainingDevicePath. IsHandleSupported()\r
-    // already checked to make sure the RemainingDevicePath contains settings\r
-    // that we can support.\r
-    //\r
-    CopyMem (&SerialDevice->UartDevicePath, RemainingDevicePath, sizeof (UART_DEVICE_PATH));\r
-  } else {\r
-    //\r
-    // Use the values from the gSerialDevTempate as no remaining device path was\r
-    // passed in.\r
-    //\r
-  }\r
   //\r
-  // Build the device path by appending the UART node to the ParentDevicePath\r
-  // from the WinNtIo handle. The Uart setings are zero here, since\r
-  // SetAttribute() will update them to match the current setings.\r
+  // Build the device path by appending the UART node to the ParentDevicePath.\r
+  // The Uart setings are zero here, since  SetAttribute() will update them to match\r
+  // the default setings.\r
   //\r
   SerialDevice->DevicePath = AppendDevicePathNode (\r
                                ParentDevicePath,\r
-                               (EFI_DEVICE_PATH_PROTOCOL *)&SerialDevice->UartDevicePath\r
+                               (EFI_DEVICE_PATH_PROTOCOL *) &SerialDevice->UartDevicePath\r
                                );\r
+  //\r
+  // Only produce the Flow Control node when remaining device path has it\r
+  //\r
+  if (FlowControl != NULL) {\r
+    TempDevicePath = SerialDevice->DevicePath;\r
+    if (TempDevicePath != NULL) {\r
+      SerialDevice->DevicePath = AppendDevicePathNode (\r
+                                   TempDevicePath,\r
+                                   (EFI_DEVICE_PATH_PROTOCOL *) FlowControl\r
+                                   );\r
+      FreePool (TempDevicePath);\r
+    }\r
+  }\r
   if (SerialDevice->DevicePath == NULL) {\r
-    Status = EFI_DEVICE_ERROR;\r
+    Status = EFI_OUT_OF_RESOURCES;\r
     goto Error;\r
   }\r
 \r
@@ -668,7 +853,7 @@ IsaSerialFifoFull (
 \r
 /**\r
   Detect whether specific FIFO is empty or not.\r
\r
+\r
   @param  Fifo    A pointer to the Data Structure SERIAL_DEV_FIFO\r
 \r
   @return whether specific FIFO is empty or not\r
@@ -837,7 +1022,7 @@ IsaSerialReceiveTransmit (
           Data = READ_RBR (SerialDevice->IsaIo, SerialDevice->BaseAddress);\r
 \r
           IsaSerialFifoAdd (&SerialDevice->Receive, Data);\r
-          \r
+\r
           //\r
           // For full handshake flow control, if receive buffer full\r
           // tell the peer to stop sending data.\r
@@ -882,7 +1067,7 @@ IsaSerialReceiveTransmit (
           //\r
           TimeOut   = 0;\r
           Msr.Data  = READ_MSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);\r
-          while (Msr.Bits.Dcd == 1 && (!Msr.Bits.Cts ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {\r
+          while ((Msr.Bits.Dcd == 1) && ((Msr.Bits.Cts == 0) ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {\r
             gBS->Stall (TIMEOUT_STALL_INTERVAL);\r
             TimeOut++;\r
             if (TimeOut > 5) {\r
@@ -892,7 +1077,7 @@ IsaSerialReceiveTransmit (
             Msr.Data = READ_MSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);\r
           }\r
 \r
-          if (Msr.Bits.Dcd== 0 || (Msr.Bits.Cts ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {\r
+          if ((Msr.Bits.Dcd == 0) || ((Msr.Bits.Cts == 1) ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {\r
             IsaSerialFifoRemove (&SerialDevice->Transmit, &Data);\r
             WRITE_THR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Data);\r
           }\r
@@ -941,6 +1126,7 @@ IsaSerialReset (
   SERIAL_PORT_MCR Mcr;\r
   SERIAL_PORT_FCR Fcr;\r
   EFI_TPL         Tpl;\r
+  UINT32          Control;\r
 \r
   SerialDevice = SERIAL_DEV_FROM_THIS (This);\r
 \r
@@ -1012,9 +1198,16 @@ IsaSerialReset (
   //\r
   // Go set the current control bits\r
   //\r
+  Control = 0;\r
+  if (SerialDevice->HardwareFlowControl) {\r
+    Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;\r
+  }\r
+  if (SerialDevice->SoftwareLoopbackEnable) {\r
+    Control |= EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE;\r
+  }\r
   Status = This->SetControl (\r
                    This,\r
-                   This->Mode->ControlMask\r
+                   Control\r
                    );\r
 \r
   if (EFI_ERROR (Status)) {\r
@@ -1081,7 +1274,7 @@ IsaSerialSetAttributes (
   UINT32                    Divisor;\r
   UINT32                    Remained;\r
   SERIAL_PORT_LCR           Lcr;\r
-  EFI_DEVICE_PATH_PROTOCOL  *NewDevicePath;\r
+  UART_DEVICE_PATH          *Uart;\r
   EFI_TPL                   Tpl;\r
 \r
   SerialDevice = SERIAL_DEV_FROM_THIS (This);\r
@@ -1090,7 +1283,7 @@ IsaSerialSetAttributes (
   // Check for default settings and fill in actual values.\r
   //\r
   if (BaudRate == 0) {\r
-    BaudRate = FixedPcdGet64 (PcdUartDefaultBaudRate);\r
+    BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
   }\r
 \r
   if (ReceiveFifoDepth == 0) {\r
@@ -1102,15 +1295,15 @@ IsaSerialSetAttributes (
   }\r
 \r
   if (Parity == DefaultParity) {\r
-    Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);\r
+    Parity = (EFI_PARITY_TYPE)PcdGet8 (PcdUartDefaultParity);\r
   }\r
 \r
   if (DataBits == 0) {\r
-    DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);\r
+    DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
   }\r
 \r
   if (StopBits == DefaultStopBits) {\r
-    StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);\r
+    StopBits = (EFI_STOP_BITS_TYPE) PcdGet8 (PcdUartDefaultStopBits);\r
   }\r
   //\r
   // 5 and 6 data bits can not be verified on a 16550A UART\r
@@ -1188,12 +1381,7 @@ IsaSerialSetAttributes (
   if ((StopBits < OneStopBit) || (StopBits > TwoStopBits)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  //\r
-  // for DataBits = 5, StopBits can not set TwoStopBits\r
-  //\r
-  // if ((DataBits == 5) && (StopBits == TwoStopBits)) {\r
-  //  return EFI_INVALID_PARAMETER;\r
-  // }\r
+\r
   //\r
   // for DataBits = 6,7,8, StopBits can not set OneFiveStopBits\r
   //\r
@@ -1205,11 +1393,11 @@ IsaSerialSetAttributes (
   // Compute divisor use to program the baud rate using a round determination\r
   //\r
   Divisor = (UINT32) DivU64x32Remainder (\r
-                       SERIAL_PORT_INPUT_CLOCK,\r
+                       PcdGet32 (PcdSerialClockRate),\r
                        ((UINT32) BaudRate * 16),\r
                        &Remained\r
                        );\r
-  if (Remained != 0) {\r
+  if (Remained >= ((UINT32) BaudRate * 8)) {\r
     Divisor += 1;\r
   }\r
 \r
@@ -1222,7 +1410,7 @@ IsaSerialSetAttributes (
   //\r
   // Compute the actual baud rate that the serial port will be programmed for.\r
   //\r
-  BaudRate = SERIAL_PORT_INPUT_CLOCK / Divisor / 16;\r
+  BaudRate = PcdGet32 (PcdSerialClockRate) / Divisor / 16;\r
 \r
   //\r
   // Put serial port on Divisor Latch Mode\r
@@ -1325,37 +1513,25 @@ IsaSerialSetAttributes (
   SerialDevice->UartDevicePath.Parity   = (UINT8) Parity;\r
   SerialDevice->UartDevicePath.StopBits = (UINT8) StopBits;\r
 \r
-  NewDevicePath = AppendDevicePathNode (\r
-                    SerialDevice->ParentDevicePath,\r
-                    (EFI_DEVICE_PATH_PROTOCOL *) &SerialDevice->UartDevicePath\r
-                    );\r
-  if (NewDevicePath == NULL) {\r
-    gBS->RestoreTPL (Tpl);\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
+  Status = EFI_SUCCESS;\r
   if (SerialDevice->Handle != NULL) {\r
+    Uart = (UART_DEVICE_PATH *) (\r
+             (UINTN) SerialDevice->DevicePath\r
+             + GetDevicePathSize (SerialDevice->ParentDevicePath)\r
+             - END_DEVICE_PATH_LENGTH\r
+             );\r
+    CopyMem (Uart, &SerialDevice->UartDevicePath, sizeof (UART_DEVICE_PATH));\r
     Status = gBS->ReinstallProtocolInterface (\r
                     SerialDevice->Handle,\r
                     &gEfiDevicePathProtocolGuid,\r
                     SerialDevice->DevicePath,\r
-                    NewDevicePath\r
+                    SerialDevice->DevicePath\r
                     );\r
-    if (EFI_ERROR (Status)) {\r
-      gBS->RestoreTPL (Tpl);\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  if (SerialDevice->DevicePath != NULL) {\r
-    gBS->FreePool (SerialDevice->DevicePath);\r
   }\r
 \r
-  SerialDevice->DevicePath = NewDevicePath;\r
-\r
   gBS->RestoreTPL (Tpl);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1375,9 +1551,11 @@ IsaSerialSetControl (
   IN UINT32                  Control\r
   )\r
 {\r
-  SERIAL_DEV      *SerialDevice;\r
-  SERIAL_PORT_MCR Mcr;\r
-  EFI_TPL         Tpl;\r
+  SERIAL_DEV                    *SerialDevice;\r
+  SERIAL_PORT_MCR               Mcr;\r
+  EFI_TPL                       Tpl;\r
+  UART_FLOW_CONTROL_DEVICE_PATH *FlowControl;\r
+  EFI_STATUS                    Status;\r
 \r
   //\r
   // The control bits that can be set are :\r
@@ -1385,13 +1563,16 @@ IsaSerialSetControl (
   //     EFI_SERIAL_REQUEST_TO_SEND: 0x0002  // WO\r
   //     EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE: 0x1000  // RW\r
   //     EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE: 0x2000  // RW\r
+  //     EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE: 0x4000 // RW\r
   //\r
   SerialDevice = SERIAL_DEV_FROM_THIS (This);\r
 \r
   //\r
   // first determine the parameter is invalid\r
   //\r
-  if ((Control & 0xffff8ffc) != 0) {\r
+  if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINAL_READY |\r
+                    EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE | EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE |\r
+                    EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1426,9 +1607,32 @@ IsaSerialSetControl (
     SerialDevice->SoftwareLoopbackEnable = TRUE;\r
   }\r
 \r
+  Status = EFI_SUCCESS;\r
+  if (SerialDevice->Handle != NULL) {\r
+    FlowControl = (UART_FLOW_CONTROL_DEVICE_PATH *) (\r
+                    (UINTN) SerialDevice->DevicePath\r
+                    + GetDevicePathSize (SerialDevice->ParentDevicePath)\r
+                    - END_DEVICE_PATH_LENGTH\r
+                    + sizeof (UART_DEVICE_PATH)\r
+                    );\r
+    if (IsUartFlowControlNode (FlowControl) &&\r
+        ((ReadUnaligned32 (&FlowControl->FlowControlMap) == UART_FLOW_CONTROL_HARDWARE) ^ SerialDevice->HardwareFlowControl)) {\r
+      //\r
+      // Flow Control setting is changed, need to reinstall device path protocol\r
+      //\r
+      WriteUnaligned32 (&FlowControl->FlowControlMap, SerialDevice->HardwareFlowControl ? UART_FLOW_CONTROL_HARDWARE : 0);\r
+      Status = gBS->ReinstallProtocolInterface (\r
+                      SerialDevice->Handle,\r
+                      &gEfiDevicePathProtocolGuid,\r
+                      SerialDevice->DevicePath,\r
+                      SerialDevice->DevicePath\r
+                      );\r
+    }\r
+  }\r
+\r
   gBS->RestoreTPL (Tpl);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1551,6 +1755,8 @@ IsaSerialWrite (
   UINTN       Elapsed;\r
   UINTN       ActualWrite;\r
   EFI_TPL     Tpl;\r
+  UINTN       Timeout;\r
+  UINTN       BitsPerCharacter;\r
 \r
   SerialDevice  = SERIAL_DEV_FROM_THIS (This);\r
   Elapsed       = 0;\r
@@ -1574,6 +1780,36 @@ IsaSerialWrite (
 \r
   CharBuffer  = (UINT8 *) Buffer;\r
 \r
+  //\r
+  // Compute the number of bits in a single character.  This is a start bit,\r
+  // followed by the number of data bits, followed by the number of stop bits.\r
+  // The number of stop bits is specified by an enumeration that includes\r
+  // support for 1.5 stop bits.  Treat 1.5 stop bits as 2 stop bits.\r
+  //\r
+  BitsPerCharacter =\r
+    1 +\r
+    This->Mode->DataBits +\r
+    ((This->Mode->StopBits == TwoStopBits) ? 2 : This->Mode->StopBits);\r
+\r
+  //\r
+  // Compute the timeout in microseconds to wait for a single byte to be\r
+  // transmitted.  The Mode structure contans a Timeout field that is the\r
+  // maximum time to transmit or receive a character.  However, many UARTs\r
+  // have a FIFO for transmits, so the time required to add one new character\r
+  // to the transmit FIFO may be the time required to flush a full FIFO.  If\r
+  // the Timeout in the Mode structure is smaller than the time required to\r
+  // flush a full FIFO at the current baud rate, then use a timeout value that\r
+  // is required to flush a full transmit FIFO.\r
+  //\r
+  Timeout = MAX (\r
+              This->Mode->Timeout,\r
+              (UINTN)DivU64x64Remainder (\r
+                BitsPerCharacter * (SERIAL_PORT_MAX_RECEIVE_FIFO_DEPTH + 1) * 1000000,\r
+                This->Mode->BaudRate,\r
+                NULL\r
+                )\r
+              );\r
+\r
   for (Index = 0; Index < *BufferSize; Index++) {\r
     IsaSerialFifoAdd (&SerialDevice->Transmit, CharBuffer[Index]);\r
 \r
@@ -1582,7 +1818,7 @@ IsaSerialWrite (
       //  Unsuccessful write so check if timeout has expired, if not,\r
       //  stall for a bit, increment time elapsed, and try again\r
       //\r
-      if (Elapsed >= This->Mode->Timeout) {\r
+      if (Elapsed >= Timeout) {\r
         *BufferSize = ActualWrite;\r
         gBS->RestoreTPL (Tpl);\r
         return EFI_TIMEOUT;\r