]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2KbdCtrller.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2KbdCtrller.c
index 4f064f76513a95bf12f7abd0158243c6884f580e..981309f8b02be4a1f9bc34278f33ff5a4224ec25 100644 (file)
@@ -1,27 +1,19 @@
 /** @file\r
   Routines that access 8042 keyboard controller\r
 \r
-Copyright (c) 2006 - 2016, 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) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "Ps2Keyboard.h"\r
 \r
 struct {\r
-  UINT8   ScanCode;             ///< follows value defined in Scan Code Set1\r
-  UINT16  EfiScanCode;\r
-  CHAR16  UnicodeChar;\r
-  CHAR16  ShiftUnicodeChar;\r
-}\r
-ConvertKeyboardScanCodeToEfiKey[] = {\r
-\r
+  UINT8     ScanCode;           ///< follows value defined in Scan Code Set1\r
+  UINT16    EfiScanCode;\r
+  CHAR16    UnicodeChar;\r
+  CHAR16    ShiftUnicodeChar;\r
+} ConvertKeyboardScanCodeToEfiKey[] = {\r
   {\r
     0x01,  //   Escape\r
     SCAN_ESC,\r
@@ -341,7 +333,7 @@ ConvertKeyboardScanCodeToEfiKey[] = {
     L'?'\r
   },\r
   {\r
-    0x36, //Right Shift\r
+    0x36, // Right Shift\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
@@ -353,7 +345,7 @@ ConvertKeyboardScanCodeToEfiKey[] = {
     L'*'\r
   },\r
   {\r
-    0x38,  //Left Alt/Extended Right Alt\r
+    0x38,  // Left Alt/Extended Right Alt\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
@@ -365,7 +357,7 @@ ConvertKeyboardScanCodeToEfiKey[] = {
     L' '\r
   },\r
   {\r
-    0x3A, //CapsLock\r
+    0x3A, // CapsLock\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
@@ -533,19 +525,19 @@ ConvertKeyboardScanCodeToEfiKey[] = {
     0x0000\r
   },\r
   {\r
-    0x5B,  //Left LOGO\r
+    0x5B,  // Left LOGO\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
   },\r
   {\r
-    0x5C,  //Right LOGO\r
+    0x5C,  // Right LOGO\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
   },\r
   {\r
-    0x5D,  //Menu key\r
+    0x5D,  // Menu key\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
@@ -563,9 +555,7 @@ ConvertKeyboardScanCodeToEfiKey[] = {
 //\r
 UINTN  mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
 \r
-BOOLEAN          mEnableMouseInterface;\r
-\r
-\r
+BOOLEAN  mEnableMouseInterface;\r
 \r
 /**\r
   Return the count of scancode in the queue.\r
@@ -576,7 +566,7 @@ BOOLEAN          mEnableMouseInterface;
 **/\r
 UINTN\r
 GetScancodeBufCount (\r
-  IN SCAN_CODE_QUEUE       *Queue\r
+  IN SCAN_CODE_QUEUE  *Queue\r
   )\r
 {\r
   if (Queue->Head <= Queue->Tail) {\r
@@ -600,13 +590,13 @@ GetScancodeBufCount (
 **/\r
 EFI_STATUS\r
 GetScancodeBufHead (\r
-  IN  SCAN_CODE_QUEUE        *Queue,\r
-  IN  UINTN                  Count,\r
-  OUT UINT8                  *Buf\r
+  IN  SCAN_CODE_QUEUE  *Queue,\r
+  IN  UINTN            Count,\r
+  OUT UINT8            *Buf\r
   )\r
 {\r
-  UINTN                      Index;\r
-  UINTN                      Pos;\r
+  UINTN  Index;\r
+  UINTN  Pos;\r
 \r
   //\r
   // check the valid range of parameter 'Count'\r
@@ -614,6 +604,7 @@ GetScancodeBufHead (
   if (GetScancodeBufCount (Queue) < Count) {\r
     return EFI_NOT_READY;\r
   }\r
+\r
   //\r
   // retrieve the values\r
   //\r
@@ -638,12 +629,12 @@ GetScancodeBufHead (
 **/\r
 EFI_STATUS\r
 PopScancodeBufHead (\r
-  IN  SCAN_CODE_QUEUE       *Queue,\r
-  IN  UINTN                 Count,\r
-  OUT UINT8                 *Buf OPTIONAL\r
+  IN  SCAN_CODE_QUEUE  *Queue,\r
+  IN  UINTN            Count,\r
+  OUT UINT8            *Buf OPTIONAL\r
   )\r
 {\r
-  UINTN                     Index;\r
+  UINTN  Index;\r
 \r
   //\r
   // Check the valid range of parameter 'Count'\r
@@ -651,6 +642,7 @@ PopScancodeBufHead (
   if (GetScancodeBufCount (Queue) < Count) {\r
     return EFI_NOT_READY;\r
   }\r
+\r
   //\r
   // Retrieve and remove the values\r
   //\r
@@ -671,8 +663,8 @@ PopScancodeBufHead (
 **/\r
 VOID\r
 PushScancodeBufTail (\r
-  IN  SCAN_CODE_QUEUE       *Queue,\r
-  IN  UINT8                 Scancode\r
+  IN  SCAN_CODE_QUEUE  *Queue,\r
+  IN  UINT8            Scancode\r
   )\r
 {\r
   if (GetScancodeBufCount (Queue) == KEYBOARD_SCAN_CODE_MAX_COUNT - 1) {\r
@@ -680,7 +672,7 @@ PushScancodeBufTail (
   }\r
 \r
   Queue->Buffer[Queue->Tail] = Scancode;\r
-  Queue->Tail = (Queue->Tail + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT;\r
+  Queue->Tail                = (Queue->Tail + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT;\r
 }\r
 \r
 /**\r
@@ -693,7 +685,7 @@ PushScancodeBufTail (
 **/\r
 UINT8\r
 KeyReadDataRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn\r
   )\r
 \r
 {\r
@@ -709,8 +701,8 @@ KeyReadDataRegister (
 **/\r
 VOID\r
 KeyWriteDataRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN UINT8                    Data\r
   )\r
 {\r
   IoWrite8 (ConsoleIn->DataRegisterAddress, Data);\r
@@ -726,7 +718,7 @@ KeyWriteDataRegister (
 **/\r
 UINT8\r
 KeyReadStatusRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn\r
   )\r
 {\r
   return IoRead8 (ConsoleIn->StatusRegisterAddress);\r
@@ -741,8 +733,8 @@ KeyReadStatusRegister (
 **/\r
 VOID\r
 KeyWriteCommandRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN UINT8                    Data\r
   )\r
 {\r
   IoWrite8 (ConsoleIn->CommandRegisterAddress, Data);\r
@@ -757,8 +749,8 @@ KeyWriteCommandRegister (
 **/\r
 VOID\r
 KeyboardError (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN CHAR16                  *ErrMsg\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN CHAR16                   *ErrMsg\r
   )\r
 {\r
   ConsoleIn->KeyboardErr = TRUE;\r
@@ -778,47 +770,49 @@ KeyboardError (
 VOID\r
 EFIAPI\r
 KeyboardTimerHandler (\r
-  IN EFI_EVENT    Event,\r
-  IN VOID         *Context\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
   )\r
 \r
 {\r
-  UINT8                   Data;\r
-  EFI_TPL                 OldTpl;\r
-  KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;\r
+  UINT8                    Data;\r
+  EFI_TPL                  OldTpl;\r
+  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn;\r
 \r
-  ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *) Context;\r
+  ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *)Context;\r
 \r
   //\r
   // Enter critical section\r
   //\r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
 \r
-  if (((KEYBOARD_CONSOLE_IN_DEV *) Context)->KeyboardErr) {\r
+  if (((KEYBOARD_CONSOLE_IN_DEV *)Context)->KeyboardErr) {\r
     //\r
     // Leave critical section and return\r
     //\r
     gBS->RestoreTPL (OldTpl);\r
-    return ;\r
+    return;\r
   }\r
 \r
   //\r
   // To let KB driver support Hot plug, here should skip the 'resend' command  for the case that\r
   // KB is not connected to system. If KB is not connected to system, driver will find there's something\r
-  // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since\r
+  // error in the following code and wait for the input buffer empty, this waiting time should be short enough since\r
   // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.\r
   // Just skip the 'resend' process simply.\r
   //\r
 \r
   while ((KeyReadStatusRegister (ConsoleIn) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) ==\r
-      KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA\r
-     ) {\r
+         KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA\r
+         )\r
+  {\r
     //\r
     // Read one byte of the scan code and store it into the memory buffer\r
     //\r
     Data = KeyReadDataRegister (ConsoleIn);\r
     PushScancodeBufTail (&ConsoleIn->ScancodeQueue, Data);\r
   }\r
+\r
   KeyGetchar (ConsoleIn);\r
 \r
   //\r
@@ -833,7 +827,7 @@ KeyboardTimerHandler (
   @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param Data      - Pointer to outof buffer for keeping key value\r
 \r
-  @retval EFI_TIMEOUT Status resigter time out\r
+  @retval EFI_TIMEOUT Status register time out\r
   @retval EFI_SUCCESS Success to read keyboard\r
 \r
 **/\r
@@ -877,20 +871,20 @@ KeyboardRead (
   @param Data      value wanted to be written\r
 \r
   @retval EFI_TIMEOUT   The input buffer register is full for putting new value util timeout\r
-  @retval EFI_SUCCESS   The new value is sucess put into input buffer register.\r
+  @retval EFI_SUCCESS   The new value is success put into input buffer register.\r
 \r
 **/\r
 EFI_STATUS\r
 KeyboardWrite (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN UINT8                    Data\r
   )\r
 {\r
   UINT32  TimeOut;\r
   UINT32  RegEmptied;\r
 \r
-  TimeOut     = 0;\r
-  RegEmptied  = 0;\r
+  TimeOut    = 0;\r
+  RegEmptied = 0;\r
 \r
   //\r
   // wait for input buffer empty\r
@@ -907,6 +901,7 @@ KeyboardWrite (
   if (RegEmptied == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
+\r
   //\r
   // Write it\r
   //\r
@@ -927,15 +922,15 @@ KeyboardWrite (
 **/\r
 EFI_STATUS\r
 KeyboardCommand (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN UINT8                    Data\r
   )\r
 {\r
   UINT32  TimeOut;\r
   UINT32  RegEmptied;\r
 \r
-  TimeOut     = 0;\r
-  RegEmptied  = 0;\r
+  TimeOut    = 0;\r
+  RegEmptied = 0;\r
 \r
   //\r
   // Wait For Input Buffer Empty\r
@@ -952,6 +947,7 @@ KeyboardCommand (
   if (RegEmptied == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
+\r
   //\r
   // issue the command\r
   //\r
@@ -991,8 +987,8 @@ KeyboardCommand (
 **/\r
 EFI_STATUS\r
 KeyboardWaitForValue (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Value\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN UINT8                    Value\r
   )\r
 {\r
   UINT8   Data;\r
@@ -1000,9 +996,9 @@ KeyboardWaitForValue (
   UINT32  SumTimeOut;\r
   UINT32  GotIt;\r
 \r
-  GotIt       = 0;\r
-  TimeOut     = 0;\r
-  SumTimeOut  = 0;\r
+  GotIt      = 0;\r
+  TimeOut    = 0;\r
+  SumTimeOut = 0;\r
 \r
   //\r
   // Make sure the initial value of 'Data' is different from 'Value'\r
@@ -1011,6 +1007,7 @@ KeyboardWaitForValue (
   if (Data == Value) {\r
     Data = 1;\r
   }\r
+\r
   //\r
   // Read from 8042 (multiple times if needed)\r
   // until the expected value appears\r
@@ -1040,6 +1037,7 @@ KeyboardWaitForValue (
       break;\r
     }\r
   }\r
+\r
   //\r
   // Check results\r
   //\r
@@ -1048,7 +1046,6 @@ KeyboardWaitForValue (
   } else {\r
     return EFI_TIMEOUT;\r
   }\r
-\r
 }\r
 \r
 /**\r
@@ -1062,7 +1059,7 @@ KeyboardWaitForValue (
 **/\r
 EFI_STATUS\r
 UpdateStatusLights (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -1104,6 +1101,38 @@ UpdateStatusLights (
   return Status;\r
 }\r
 \r
+/**\r
+  Initialize the key state.\r
+\r
+  @param  ConsoleIn     The KEYBOARD_CONSOLE_IN_DEV instance.\r
+  @param  KeyState      A pointer to receive the key state information.\r
+**/\r
+VOID\r
+InitializeKeyState (\r
+  IN  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  OUT EFI_KEY_STATE            *KeyState\r
+  )\r
+{\r
+  KeyState->KeyShiftState = EFI_SHIFT_STATE_VALID\r
+                            | (ConsoleIn->LeftCtrl   ? EFI_LEFT_CONTROL_PRESSED  : 0)\r
+                            | (ConsoleIn->RightCtrl  ? EFI_RIGHT_CONTROL_PRESSED : 0)\r
+                            | (ConsoleIn->LeftAlt    ? EFI_LEFT_ALT_PRESSED      : 0)\r
+                            | (ConsoleIn->RightAlt   ? EFI_RIGHT_ALT_PRESSED     : 0)\r
+                            | (ConsoleIn->LeftShift  ? EFI_LEFT_SHIFT_PRESSED    : 0)\r
+                            | (ConsoleIn->RightShift ? EFI_RIGHT_SHIFT_PRESSED   : 0)\r
+                            | (ConsoleIn->LeftLogo   ? EFI_LEFT_LOGO_PRESSED     : 0)\r
+                            | (ConsoleIn->RightLogo  ? EFI_RIGHT_LOGO_PRESSED    : 0)\r
+                            | (ConsoleIn->Menu       ? EFI_MENU_KEY_PRESSED      : 0)\r
+                            | (ConsoleIn->SysReq     ? EFI_SYS_REQ_PRESSED       : 0)\r
+  ;\r
+  KeyState->KeyToggleState = EFI_TOGGLE_STATE_VALID\r
+                             | (ConsoleIn->CapsLock   ? EFI_CAPS_LOCK_ACTIVE :   0)\r
+                             | (ConsoleIn->NumLock    ? EFI_NUM_LOCK_ACTIVE :    0)\r
+                             | (ConsoleIn->ScrollLock ? EFI_SCROLL_LOCK_ACTIVE : 0)\r
+                             | (ConsoleIn->IsSupportPartialKey ? EFI_KEY_STATE_EXPOSED : 0)\r
+  ;\r
+}\r
+\r
 /**\r
   Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.\r
 \r
@@ -1114,7 +1143,7 @@ UpdateStatusLights (
 **/\r
 VOID\r
 KeyGetchar (\r
-  IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+  IN OUT KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn\r
   )\r
 {\r
   EFI_STATUS                     Status;\r
@@ -1128,8 +1157,8 @@ KeyGetchar (
   //\r
   // 3 bytes most\r
   //\r
-  UINT8                          ScancodeArr[3];\r
-  UINT32                         ScancodeArrPos;\r
+  UINT8   ScancodeArr[3];\r
+  UINT32  ScancodeArrPos;\r
 \r
   //\r
   // Check if there are enough bytes of scancode representing a single key\r
@@ -1139,32 +1168,33 @@ KeyGetchar (
     Extend0        = FALSE;\r
     Extend1        = FALSE;\r
     ScancodeArrPos = 0;\r
-    Status  = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+    Status         = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
     if (EFI_ERROR (Status)) {\r
-      return ;\r
+      return;\r
     }\r
 \r
     if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED0) {\r
       //\r
       // E0 to look ahead 2 bytes\r
       //\r
-      Extend0 = TRUE;\r
+      Extend0        = TRUE;\r
       ScancodeArrPos = 1;\r
       Status         = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
       if (EFI_ERROR (Status)) {\r
-        return ;\r
+        return;\r
       }\r
     } else if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED1) {\r
       //\r
       // E1 to look ahead 3 bytes\r
       //\r
-      Extend1 = TRUE;\r
+      Extend1        = TRUE;\r
       ScancodeArrPos = 2;\r
       Status         = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
       if (EFI_ERROR (Status)) {\r
-        return ;\r
+        return;\r
       }\r
     }\r
+\r
     //\r
     // if we reach this position, scancodes for a key is in buffer now,pop them\r
     //\r
@@ -1181,115 +1211,123 @@ KeyGetchar (
       // Check for special keys and update the driver state.\r
       //\r
       switch (ScanCode) {\r
+        case SCANCODE_CTRL_MAKE:\r
+          if (Extend0) {\r
+            ConsoleIn->RightCtrl = TRUE;\r
+          } else {\r
+            ConsoleIn->LeftCtrl = TRUE;\r
+          }\r
 \r
-      case SCANCODE_CTRL_MAKE:\r
-        if (Extend0) {\r
-          ConsoleIn->RightCtrl = TRUE;\r
-        } else {\r
-          ConsoleIn->LeftCtrl  = TRUE;\r
-        }\r
-        break;\r
-      case SCANCODE_CTRL_BREAK:\r
-        if (Extend0) {\r
-          ConsoleIn->RightCtrl = FALSE;\r
-        } else {\r
-          ConsoleIn->LeftCtrl  = FALSE;\r
-        }\r
-        break;\r
+          break;\r
+        case SCANCODE_CTRL_BREAK:\r
+          if (Extend0) {\r
+            ConsoleIn->RightCtrl = FALSE;\r
+          } else {\r
+            ConsoleIn->LeftCtrl = FALSE;\r
+          }\r
 \r
-      case SCANCODE_ALT_MAKE:\r
+          break;\r
+\r
+        case SCANCODE_ALT_MAKE:\r
           if (Extend0) {\r
             ConsoleIn->RightAlt = TRUE;\r
           } else {\r
-            ConsoleIn->LeftAlt  = TRUE;\r
+            ConsoleIn->LeftAlt = TRUE;\r
           }\r
-        break;\r
-      case SCANCODE_ALT_BREAK:\r
+\r
+          break;\r
+        case SCANCODE_ALT_BREAK:\r
           if (Extend0) {\r
             ConsoleIn->RightAlt = FALSE;\r
           } else {\r
-            ConsoleIn->LeftAlt  = FALSE;\r
+            ConsoleIn->LeftAlt = FALSE;\r
           }\r
-        break;\r
 \r
-      case SCANCODE_LEFT_SHIFT_MAKE:\r
-        //\r
-        // To avoid recognize PRNT_SCRN key as a L_SHIFT key\r
-        // because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.\r
-        // If it the second byte of the PRNT_ScRN skip it.\r
-        //\r
-        if (!Extend0) {\r
-          ConsoleIn->LeftShift  = TRUE;\r
           break;\r
-        }\r
-        continue;\r
 \r
-      case SCANCODE_LEFT_SHIFT_BREAK:\r
-        if (!Extend0) {\r
-          ConsoleIn->LeftShift = FALSE;\r
-        }\r
-        break;\r
+        case SCANCODE_LEFT_SHIFT_MAKE:\r
+          //\r
+          // To avoid recognize PRNT_SCRN key as a L_SHIFT key\r
+          // because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.\r
+          // If it the second byte of the PRNT_ScRN skip it.\r
+          //\r
+          if (!Extend0) {\r
+            ConsoleIn->LeftShift = TRUE;\r
+            break;\r
+          }\r
 \r
-      case SCANCODE_RIGHT_SHIFT_MAKE:\r
-        ConsoleIn->RightShift = TRUE;\r
-        break;\r
-      case SCANCODE_RIGHT_SHIFT_BREAK:\r
-        ConsoleIn->RightShift = FALSE;\r
-        break;\r
+          continue;\r
 \r
-      case SCANCODE_LEFT_LOGO_MAKE:\r
-        ConsoleIn->LeftLogo = TRUE;\r
-        break;\r
-      case SCANCODE_LEFT_LOGO_BREAK:\r
-        ConsoleIn->LeftLogo = FALSE;\r
-        break;\r
+        case SCANCODE_LEFT_SHIFT_BREAK:\r
+          if (!Extend0) {\r
+            ConsoleIn->LeftShift = FALSE;\r
+          }\r
 \r
-      case SCANCODE_RIGHT_LOGO_MAKE:\r
-        ConsoleIn->RightLogo = TRUE;\r
-        break;\r
-      case SCANCODE_RIGHT_LOGO_BREAK:\r
-        ConsoleIn->RightLogo = FALSE;\r
-        break;\r
+          break;\r
 \r
-      case SCANCODE_MENU_MAKE:\r
-        ConsoleIn->Menu = TRUE;\r
-        break;\r
-      case SCANCODE_MENU_BREAK:\r
-        ConsoleIn->Menu = FALSE;\r
-        break;\r
+        case SCANCODE_RIGHT_SHIFT_MAKE:\r
+          ConsoleIn->RightShift = TRUE;\r
+          break;\r
+        case SCANCODE_RIGHT_SHIFT_BREAK:\r
+          ConsoleIn->RightShift = FALSE;\r
+          break;\r
 \r
-      case SCANCODE_SYS_REQ_MAKE:\r
-        if (Extend0) {\r
+        case SCANCODE_LEFT_LOGO_MAKE:\r
+          ConsoleIn->LeftLogo = TRUE;\r
+          break;\r
+        case SCANCODE_LEFT_LOGO_BREAK:\r
+          ConsoleIn->LeftLogo = FALSE;\r
+          break;\r
+\r
+        case SCANCODE_RIGHT_LOGO_MAKE:\r
+          ConsoleIn->RightLogo = TRUE;\r
+          break;\r
+        case SCANCODE_RIGHT_LOGO_BREAK:\r
+          ConsoleIn->RightLogo = FALSE;\r
+          break;\r
+\r
+        case SCANCODE_MENU_MAKE:\r
+          ConsoleIn->Menu = TRUE;\r
+          break;\r
+        case SCANCODE_MENU_BREAK:\r
+          ConsoleIn->Menu = FALSE;\r
+          break;\r
+\r
+        case SCANCODE_SYS_REQ_MAKE:\r
+          if (Extend0) {\r
+            ConsoleIn->SysReq = TRUE;\r
+          }\r
+\r
+          break;\r
+        case SCANCODE_SYS_REQ_BREAK:\r
+          if (Extend0) {\r
+            ConsoleIn->SysReq = FALSE;\r
+          }\r
+\r
+          break;\r
+\r
+        case SCANCODE_SYS_REQ_MAKE_WITH_ALT:\r
           ConsoleIn->SysReq = TRUE;\r
-        }\r
-        break;\r
-      case SCANCODE_SYS_REQ_BREAK:\r
-        if (Extend0) {\r
+          break;\r
+        case SCANCODE_SYS_REQ_BREAK_WITH_ALT:\r
           ConsoleIn->SysReq = FALSE;\r
-        }\r
-        break;\r
-\r
-      case SCANCODE_SYS_REQ_MAKE_WITH_ALT:\r
-        ConsoleIn->SysReq = TRUE;\r
-        break;\r
-      case SCANCODE_SYS_REQ_BREAK_WITH_ALT:\r
-        ConsoleIn->SysReq = FALSE;\r
-        break;\r
+          break;\r
 \r
-      case SCANCODE_CAPS_LOCK_MAKE:\r
-        ConsoleIn->CapsLock = (BOOLEAN)!ConsoleIn->CapsLock;\r
-        UpdateStatusLights (ConsoleIn);\r
-        break;\r
-      case SCANCODE_NUM_LOCK_MAKE:\r
-        ConsoleIn->NumLock = (BOOLEAN)!ConsoleIn->NumLock;\r
-        UpdateStatusLights (ConsoleIn);\r
-        break;\r
-      case SCANCODE_SCROLL_LOCK_MAKE:\r
-        if (!Extend0) {\r
-          ConsoleIn->ScrollLock = (BOOLEAN)!ConsoleIn->ScrollLock;\r
+        case SCANCODE_CAPS_LOCK_MAKE:\r
+          ConsoleIn->CapsLock = (BOOLEAN) !ConsoleIn->CapsLock;\r
           UpdateStatusLights (ConsoleIn);\r
-        }\r
-        break;\r
+          break;\r
+        case SCANCODE_NUM_LOCK_MAKE:\r
+          ConsoleIn->NumLock = (BOOLEAN) !ConsoleIn->NumLock;\r
+          UpdateStatusLights (ConsoleIn);\r
+          break;\r
+        case SCANCODE_SCROLL_LOCK_MAKE:\r
+          if (!Extend0) {\r
+            ConsoleIn->ScrollLock = (BOOLEAN) !ConsoleIn->ScrollLock;\r
+            UpdateStatusLights (ConsoleIn);\r
+          }\r
+\r
+          break;\r
       }\r
     }\r
 \r
@@ -1307,68 +1345,51 @@ KeyGetchar (
   // Handle Ctrl+Alt+Del hotkey\r
   //\r
   if ((ConsoleIn->LeftCtrl || ConsoleIn->RightCtrl) &&\r
-      (ConsoleIn->LeftAlt  || ConsoleIn->RightAlt ) &&\r
-      ScanCode == SCANCODE_DELETE_MAKE\r
-     ) {\r
+      (ConsoleIn->LeftAlt  || ConsoleIn->RightAlt) &&\r
+      (ScanCode == SCANCODE_DELETE_MAKE)\r
+      )\r
+  {\r
     gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
   }\r
 \r
   //\r
   // Save the Shift/Toggle state\r
   //\r
-  KeyData.KeyState.KeyShiftState = (UINT32) (EFI_SHIFT_STATE_VALID\r
-                                 | (ConsoleIn->LeftCtrl   ? EFI_LEFT_CONTROL_PRESSED  : 0)\r
-                                 | (ConsoleIn->RightCtrl  ? EFI_RIGHT_CONTROL_PRESSED : 0)\r
-                                 | (ConsoleIn->LeftAlt    ? EFI_LEFT_ALT_PRESSED      : 0)\r
-                                 | (ConsoleIn->RightAlt   ? EFI_RIGHT_ALT_PRESSED     : 0)\r
-                                 | (ConsoleIn->LeftShift  ? EFI_LEFT_SHIFT_PRESSED    : 0)\r
-                                 | (ConsoleIn->RightShift ? EFI_RIGHT_SHIFT_PRESSED   : 0)\r
-                                 | (ConsoleIn->LeftLogo   ? EFI_LEFT_LOGO_PRESSED     : 0)\r
-                                 | (ConsoleIn->RightLogo  ? EFI_RIGHT_LOGO_PRESSED    : 0)\r
-                                 | (ConsoleIn->Menu       ? EFI_MENU_KEY_PRESSED      : 0)\r
-                                 | (ConsoleIn->SysReq     ? EFI_SYS_REQ_PRESSED       : 0)\r
-                                 );\r
-  KeyData.KeyState.KeyToggleState = (EFI_KEY_TOGGLE_STATE) (EFI_TOGGLE_STATE_VALID\r
-                                  | (ConsoleIn->CapsLock   ? EFI_CAPS_LOCK_ACTIVE :   0)\r
-                                  | (ConsoleIn->NumLock    ? EFI_NUM_LOCK_ACTIVE :    0)\r
-                                  | (ConsoleIn->ScrollLock ? EFI_SCROLL_LOCK_ACTIVE : 0)\r
-                                  | (ConsoleIn->IsSupportPartialKey ? EFI_KEY_STATE_EXPOSED : 0)\r
-                                  );\r
-\r
-  KeyData.Key.ScanCode            = SCAN_NULL;\r
-  KeyData.Key.UnicodeChar         = CHAR_NULL;\r
+  InitializeKeyState (ConsoleIn, &KeyData.KeyState);\r
+  KeyData.Key.ScanCode    = SCAN_NULL;\r
+  KeyData.Key.UnicodeChar = CHAR_NULL;\r
 \r
   //\r
   // Key Pad "/" shares the same scancode as that of "/" except Key Pad "/" has E0 prefix\r
   //\r
-  if (Extend0 && ScanCode == 0x35) {\r
+  if (Extend0 && (ScanCode == 0x35)) {\r
     KeyData.Key.UnicodeChar = L'/';\r
     KeyData.Key.ScanCode    = SCAN_NULL;\r
 \r
-  //\r
-  // PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix\r
-  //\r
-  } else if (Extend1 && ScanCode == SCANCODE_NUM_LOCK_MAKE) {\r
+    //\r
+    // PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix\r
+    //\r
+  } else if (Extend1 && (ScanCode == SCANCODE_NUM_LOCK_MAKE)) {\r
     KeyData.Key.UnicodeChar = CHAR_NULL;\r
     KeyData.Key.ScanCode    = SCAN_PAUSE;\r
 \r
-  //\r
-  // PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix\r
-  //\r
-  } else if (Extend0 && ScanCode == SCANCODE_SCROLL_LOCK_MAKE) {\r
+    //\r
+    // PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix\r
+    //\r
+  } else if (Extend0 && (ScanCode == SCANCODE_SCROLL_LOCK_MAKE)) {\r
     KeyData.Key.UnicodeChar = CHAR_NULL;\r
     KeyData.Key.ScanCode    = SCAN_PAUSE;\r
 \r
-  //\r
-  // PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix\r
-  //\r
-  } else if (Extend0 && ScanCode == SCANCODE_SYS_REQ_MAKE) {\r
+    //\r
+    // PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix\r
+    //\r
+  } else if (Extend0 && (ScanCode == SCANCODE_SYS_REQ_MAKE)) {\r
     KeyData.Key.UnicodeChar = CHAR_NULL;\r
     KeyData.Key.ScanCode    = SCAN_NULL;\r
 \r
-  //\r
-  // Except the above special case, all others can be handled by convert table\r
-  //\r
+    //\r
+    // Except the above special case, all others can be handled by convert table\r
+    //\r
   } else {\r
     for (Index = 0; ConvertKeyboardScanCodeToEfiKey[Index].ScanCode != TABLE_END; Index++) {\r
       if (ScanCode == ConvertKeyboardScanCodeToEfiKey[Index].ScanCode) {\r
@@ -1376,7 +1397,8 @@ KeyGetchar (
         KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
 \r
         if ((ConsoleIn->LeftShift || ConsoleIn->RightShift) &&\r
-            (ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar != ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar)) {\r
+            (ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar != ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar))\r
+        {\r
           KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
           //\r
           // Need not return associated shift state if a class of printable characters that\r
@@ -1384,16 +1406,18 @@ KeyGetchar (
           //\r
           KeyData.KeyState.KeyShiftState &= ~(EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED);\r
         }\r
+\r
         //\r
         // alphabetic key is affected by CapsLock State\r
         //\r
         if (ConsoleIn->CapsLock) {\r
-          if (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z') {\r
-            KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'a' + L'A');\r
-          } else if (KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') {\r
-            KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'A' + L'a');\r
+          if ((KeyData.Key.UnicodeChar >= L'a') && (KeyData.Key.UnicodeChar <= L'z')) {\r
+            KeyData.Key.UnicodeChar = (UINT16)(KeyData.Key.UnicodeChar - L'a' + L'A');\r
+          } else if ((KeyData.Key.UnicodeChar >= L'A') && (KeyData.Key.UnicodeChar <= L'Z')) {\r
+            KeyData.Key.UnicodeChar = (UINT16)(KeyData.Key.UnicodeChar - L'A' + L'a');\r
           }\r
         }\r
+\r
         break;\r
       }\r
     }\r
@@ -1402,10 +1426,10 @@ KeyGetchar (
   //\r
   // distinguish numeric key pad keys' 'up symbol' and 'down symbol'\r
   //\r
-  if (ScanCode >= 0x47 && ScanCode <= 0x53) {\r
+  if ((ScanCode >= 0x47) && (ScanCode <= 0x53)) {\r
     if (ConsoleIn->NumLock && !(ConsoleIn->LeftShift || ConsoleIn->RightShift) && !Extend0) {\r
       KeyData.Key.ScanCode = SCAN_NULL;\r
-    } else if (ScanCode != 0x4a && ScanCode != 0x4e) {\r
+    } else if ((ScanCode != 0x4a) && (ScanCode != 0x4e)) {\r
       KeyData.Key.UnicodeChar = CHAR_NULL;\r
     }\r
   }\r
@@ -1413,14 +1437,14 @@ KeyGetchar (
   //\r
   // If the key can not be converted then just return.\r
   //\r
-  if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == CHAR_NULL) {\r
+  if ((KeyData.Key.ScanCode == SCAN_NULL) && (KeyData.Key.UnicodeChar == CHAR_NULL)) {\r
     if (!ConsoleIn->IsSupportPartialKey) {\r
-      return ;\r
+      return;\r
     }\r
   }\r
 \r
   //\r
-  // Invoke notification functions if exist\r
+  // Signal KeyNotify process event if this key pressed matches any key registered.\r
   //\r
   for (Link = GetFirstNode (&ConsoleIn->NotifyList); !IsNull (&ConsoleIn->NotifyList, Link); Link = GetNextNode (&ConsoleIn->NotifyList, Link)) {\r
     CurrentNotify = CR (\r
@@ -1430,7 +1454,14 @@ KeyGetchar (
                       KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
                       );\r
     if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
-      CurrentNotify->KeyNotificationFn (&KeyData);\r
+      //\r
+      // The key notification function needs to run at TPL_CALLBACK\r
+      // while current TPL is TPL_NOTIFY. It will be invoked in\r
+      // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.\r
+      //\r
+      PushEfikeyBufTail (&ConsoleIn->EfiKeyQueueForNotify, &KeyData);\r
+      gBS->SignalEvent (ConsoleIn->KeyNotifyProcessEvent);\r
+      break;\r
     }\r
   }\r
 \r
@@ -1450,28 +1481,28 @@ KeyGetchar (
 **/\r
 EFI_STATUS\r
 InitKeyboard (\r
-  IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN BOOLEAN                     ExtendedVerification\r
+  IN OUT KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
+  IN BOOLEAN                      ExtendedVerification\r
   )\r
 {\r
-  EFI_STATUS              Status;\r
-  EFI_STATUS              Status1;\r
-  UINT8                   CommandByte;\r
-  EFI_PS2_POLICY_PROTOCOL *Ps2Policy;\r
-  UINT32                  TryTime;\r
+  EFI_STATUS               Status;\r
+  EFI_STATUS               Status1;\r
+  UINT8                    CommandByte;\r
+  EFI_PS2_POLICY_PROTOCOL  *Ps2Policy;\r
+  UINT32                   TryTime;\r
 \r
-  Status                 = EFI_SUCCESS;\r
-  mEnableMouseInterface  = TRUE;\r
-  TryTime                = 0;\r
+  Status                = EFI_SUCCESS;\r
+  mEnableMouseInterface = TRUE;\r
+  TryTime               = 0;\r
 \r
   //\r
   // Get Ps2 policy to set this\r
   //\r
   gBS->LocateProtocol (\r
-        &gEfiPs2PolicyProtocolGuid,\r
-        NULL,\r
-        (VOID **) &Ps2Policy\r
-        );\r
+         &gEfiPs2PolicyProtocolGuid,\r
+         NULL,\r
+         (VOID **)&Ps2Policy\r
+         );\r
 \r
   REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
@@ -1486,8 +1517,9 @@ InitKeyboard (
   if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) != 0) {\r
     while (!EFI_ERROR (Status) && TryTime < KEYBOARD_MAX_TRY) {\r
       Status = KeyboardRead (ConsoleIn, &CommandByte);\r
-      TryTime ++;\r
+      TryTime++;\r
     }\r
+\r
     //\r
     // Exceed the max try times. The device may be error.\r
     //\r
@@ -1496,6 +1528,7 @@ InitKeyboard (
       goto Done;\r
     }\r
   }\r
+\r
   //\r
   // We should disable mouse interface during the initialization process\r
   // since mouse device output could block keyboard device output in the\r
@@ -1529,6 +1562,7 @@ InitKeyboard (
         KeyboardError (ConsoleIn, L"\n\r");\r
         goto Done;\r
       }\r
+\r
       //\r
       // Test the mouse enabling bit\r
       //\r
@@ -1582,16 +1616,17 @@ InitKeyboard (
         goto Done;\r
       }\r
     }\r
+\r
     //\r
     // Don't enable mouse interface later\r
     //\r
     mEnableMouseInterface = FALSE;\r
-\r
   }\r
 \r
   if (Ps2Policy != NULL) {\r
     Ps2Policy->Ps2InitHardware (ConsoleIn->Handle);\r
   }\r
+\r
   //\r
   // Write 8042 Command Byte, set System Flag\r
   // While at the same time:\r
@@ -1625,10 +1660,12 @@ InitKeyboard (
   //\r
   // Clear Memory Scancode Buffer\r
   //\r
-  ConsoleIn->ScancodeQueue.Head = 0;\r
-  ConsoleIn->ScancodeQueue.Tail = 0;\r
-  ConsoleIn->EfiKeyQueue.Head   = 0;\r
-  ConsoleIn->EfiKeyQueue.Tail   = 0;\r
+  ConsoleIn->ScancodeQueue.Head        = 0;\r
+  ConsoleIn->ScancodeQueue.Tail        = 0;\r
+  ConsoleIn->EfiKeyQueue.Head          = 0;\r
+  ConsoleIn->EfiKeyQueue.Tail          = 0;\r
+  ConsoleIn->EfiKeyQueueForNotify.Head = 0;\r
+  ConsoleIn->EfiKeyQueueForNotify.Tail = 0;\r
 \r
   //\r
   // Reset the status indicators\r
@@ -1649,9 +1686,9 @@ InitKeyboard (
 \r
   ConsoleIn->IsSupportPartialKey = FALSE;\r
   //\r
-  // For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,\r
+  // For resetting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,\r
   // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected\r
-  // to system. So we only do the real reseting for keyboard when user asks and there is a real KB connected t system,\r
+  // to system. So we only do the real resetting for keyboard when user asks and there is a real KB connected t system,\r
   // and normally during booting an OS, it's skipped.\r
   //\r
   if (ExtendedVerification && CheckKeyboardConnect (ConsoleIn)) {\r
@@ -1671,10 +1708,11 @@ InitKeyboard (
     if (EFI_ERROR (Status)) {\r
       KeyboardError (\r
         ConsoleIn,\r
-        L"Some specific value not aquired from 8042 controller!\n\r"\r
+        L"Some specific value not acquired from 8042 controller!\n\r"\r
         );\r
       goto Done;\r
     }\r
+\r
     //\r
     // Keyboard reset with a BAT(Basic Assurance Test)\r
     //\r
@@ -1686,19 +1724,14 @@ InitKeyboard (
 \r
     Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
+      KeyboardError (ConsoleIn, L"Some specific value not acquired from 8042 controller!\n\r");\r
       goto Done;\r
     }\r
+\r
     //\r
     // wait for BAT completion code\r
     //\r
-    mWaitForValueTimeOut  = KEYBOARD_BAT_TIMEOUT;\r
-\r
-    Status                = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"Keyboard self test failed!\n\r");\r
-      goto Done;\r
-    }\r
+    KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS);\r
 \r
     mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
 \r
@@ -1713,7 +1746,7 @@ InitKeyboard (
 \r
     Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
+      KeyboardError (ConsoleIn, L"Some specific value not acquired from 8042 controller!\n\r");\r
       goto Done;\r
     }\r
 \r
@@ -1725,47 +1758,50 @@ InitKeyboard (
 \r
     Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
+      KeyboardError (ConsoleIn, L"Some specific value not acquired from 8042 controller!\n\r");\r
       goto Done;\r
     }\r
 \r
-  //\r
-  // Clear Keyboard Scancode Buffer\r
-  //\r
-  Status = KeyboardWrite (ConsoleIn, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA);\r
-  if (EFI_ERROR (Status)) {\r
-    KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
-    goto Done;\r
-  }\r
-\r
-  Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
-  if (EFI_ERROR (Status)) {\r
-    KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
-    goto Done;\r
-  }\r
-  //\r
-  if (Ps2Policy != NULL) {\r
-    if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_CAPSLOCK) == EFI_KEYBOARD_CAPSLOCK) {\r
-      ConsoleIn->CapsLock = TRUE;\r
+    //\r
+    // Clear Keyboard Scancode Buffer\r
+    //\r
+    Status = KeyboardWrite (ConsoleIn, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA);\r
+    if (EFI_ERROR (Status)) {\r
+      KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
+      goto Done;\r
     }\r
 \r
-    if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_NUMLOCK) == EFI_KEYBOARD_NUMLOCK) {\r
-      ConsoleIn->NumLock = TRUE;\r
+    Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
+    if (EFI_ERROR (Status)) {\r
+      KeyboardError (ConsoleIn, L"Some specific value not acquired from 8042 controller!\n\r");\r
+      goto Done;\r
     }\r
 \r
-    if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_SCROLLLOCK) == EFI_KEYBOARD_SCROLLLOCK) {\r
-      ConsoleIn->ScrollLock = TRUE;\r
+    //\r
+    if (Ps2Policy != NULL) {\r
+      if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_CAPSLOCK) == EFI_KEYBOARD_CAPSLOCK) {\r
+        ConsoleIn->CapsLock = TRUE;\r
+      }\r
+\r
+      if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_NUMLOCK) == EFI_KEYBOARD_NUMLOCK) {\r
+        ConsoleIn->NumLock = TRUE;\r
+      }\r
+\r
+      if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_SCROLLLOCK) == EFI_KEYBOARD_SCROLLLOCK) {\r
+        ConsoleIn->ScrollLock = TRUE;\r
+      }\r
     }\r
-  }\r
-  //\r
-  // Update Keyboard Lights\r
-  //\r
-  Status = UpdateStatusLights (ConsoleIn);\r
-  if (EFI_ERROR (Status)) {\r
-    KeyboardError (ConsoleIn, L"Update keyboard status lights error!\n\r");\r
-    goto Done;\r
+\r
+    //\r
+    // Update Keyboard Lights\r
+    //\r
+    Status = UpdateStatusLights (ConsoleIn);\r
+    if (EFI_ERROR (Status)) {\r
+      KeyboardError (ConsoleIn, L"Update keyboard status lights error!\n\r");\r
+      goto Done;\r
     }\r
   }\r
+\r
   //\r
   // At last, we can now enable the mouse interface if appropriate\r
   //\r
@@ -1787,34 +1823,6 @@ Done:
   } else {\r
     return EFI_DEVICE_ERROR;\r
   }\r
-\r
-}\r
-\r
-/**\r
-  Disable the keyboard interface of the 8042 controller.\r
-\r
-  @param ConsoleIn   The device instance\r
-\r
-  @return status of issuing disable command\r
-\r
-**/\r
-EFI_STATUS\r
-DisableKeyboard (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
-  )\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  //\r
-  // Disable keyboard interface\r
-  //\r
-  Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
-  if (EFI_ERROR (Status)) {\r
-    KeyboardError (ConsoleIn, L"\n\r");\r
-    return EFI_DEVICE_ERROR;\r
-  }\r
-\r
-  return Status;\r
 }\r
 \r
 /**\r
@@ -1830,11 +1838,11 @@ DisableKeyboard (
 BOOLEAN\r
 EFIAPI\r
 CheckKeyboardConnect (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+  IN KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn\r
   )\r
 {\r
-  EFI_STATUS     Status;\r
-  UINTN          WaitForValueTimeOutBcakup;\r
+  EFI_STATUS  Status;\r
+  UINTN       WaitForValueTimeOutBcakup;\r
 \r
   //\r
   // enable keyboard itself and wait for its ack\r
@@ -1849,15 +1857,16 @@ CheckKeyboardConnect (
     if (EFI_ERROR (Status)) {\r
       return FALSE;\r
     }\r
+\r
     //\r
     // wait for 1s\r
     //\r
     WaitForValueTimeOutBcakup = mWaitForValueTimeOut;\r
-    mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
-    Status = KeyboardWaitForValue (\r
-               ConsoleIn,\r
-               KEYBOARD_CMDECHO_ACK\r
-               );\r
+    mWaitForValueTimeOut      = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
+    Status                    = KeyboardWaitForValue (\r
+                                  ConsoleIn,\r
+                                  KEYBOARD_CMDECHO_ACK\r
+                                  );\r
     mWaitForValueTimeOut = WaitForValueTimeOutBcakup;\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -1869,4 +1878,3 @@ CheckKeyboardConnect (
     return TRUE;\r
   }\r
 }\r
-\r