]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2KbdCtrller.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2KbdCtrller.c
index eb03c0c836614f90940c8660683f8acb639d0e18..78c4e3b3919da5e9754e98cbd406a56bb9554225 100644 (file)
@@ -1,8 +1,8 @@
-/**@file\r
+/** @file\r
   Routines that access 8042 keyboard controller\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\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
@@ -537,19 +537,19 @@ ConvertKeyboardScanCodeToEfiKey[] = {
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
-  },  \r
+  },\r
   {\r
     0x5C,  //Right LOGO\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
-  },  \r
+  },\r
   {\r
     0x5D,  //Menu key\r
     SCAN_NULL,\r
     0x0000,\r
     0x0000\r
-  },    \r
+  },\r
   {\r
     TABLE_END,\r
     TABLE_END,\r
@@ -558,7 +558,6 @@ ConvertKeyboardScanCodeToEfiKey[] = {
   },\r
 };\r
 \r
-\r
 //\r
 // The WaitForValue time out\r
 //\r
@@ -566,12 +565,130 @@ UINTN  mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;
 \r
 BOOLEAN          mEnableMouseInterface;\r
 \r
+\r
+\r
+/**\r
+  Return the count of scancode in the queue.\r
+\r
+  @param Queue     Pointer to instance of SCAN_CODE_QUEUE.\r
+\r
+  @return          Count of the scancode.\r
+**/\r
+UINTN\r
+GetScancodeBufCount (\r
+  IN SCAN_CODE_QUEUE       *Queue\r
+  )\r
+{\r
+  if (Queue->Head <= Queue->Tail) {\r
+    return Queue->Tail - Queue->Head;\r
+  } else {\r
+    return Queue->Tail + KEYBOARD_SCAN_CODE_MAX_COUNT - Queue->Head;\r
+  }\r
+}\r
+\r
+/**\r
+  Read several bytes from the scancode buffer without removing them.\r
+  This function is called to see if there are enough bytes of scancode\r
+  representing a single key.\r
+\r
+  @param Queue     Pointer to instance of SCAN_CODE_QUEUE.\r
+  @param Count     Number of bytes to be read\r
+  @param Buf       Store the results\r
+\r
+  @retval EFI_SUCCESS   success to scan the keyboard code\r
+  @retval EFI_NOT_READY invalid parameter\r
+**/\r
+EFI_STATUS\r
+GetScancodeBufHead (\r
+  IN  SCAN_CODE_QUEUE        *Queue,\r
+  IN  UINTN                  Count,\r
+  OUT UINT8                  *Buf\r
+  )\r
+{\r
+  UINTN                      Index;\r
+  UINTN                      Pos;\r
+\r
+  //\r
+  // check the valid range of parameter 'Count'\r
+  //\r
+  if (GetScancodeBufCount (Queue) < Count) {\r
+    return EFI_NOT_READY;\r
+  }\r
+  //\r
+  // retrieve the values\r
+  //\r
+  for (Index = 0, Pos = Queue->Head; Index < Count; Index++, Pos = (Pos + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {\r
+    Buf[Index] = Queue->Buffer[Pos];\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
-  Read data register \r
+\r
+  Read & remove several bytes from the scancode buffer.\r
+  This function is usually called after GetScancodeBufHead()\r
+\r
+  @param Queue     Pointer to instance of SCAN_CODE_QUEUE.\r
+  @param Count     Number of bytes to be read\r
+  @param Buf       Store the results\r
+\r
+  @retval EFI_SUCCESS success to scan the keyboard code\r
+  @retval EFI_NOT_READY invalid parameter\r
+**/\r
+EFI_STATUS\r
+PopScancodeBufHead (\r
+  IN  SCAN_CODE_QUEUE       *Queue,\r
+  IN  UINTN                 Count,\r
+  OUT UINT8                 *Buf OPTIONAL\r
+  )\r
+{\r
+  UINTN                     Index;\r
+\r
+  //\r
+  // Check the valid range of parameter 'Count'\r
+  //\r
+  if (GetScancodeBufCount (Queue) < Count) {\r
+    return EFI_NOT_READY;\r
+  }\r
+  //\r
+  // Retrieve and remove the values\r
+  //\r
+  for (Index = 0; Index < Count; Index++, Queue->Head = (Queue->Head + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {\r
+    if (Buf != NULL) {\r
+      Buf[Index] = Queue->Buffer[Queue->Head];\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Push one byte to the scancode buffer.\r
+\r
+  @param Queue     Pointer to instance of SCAN_CODE_QUEUE.\r
+  @param Scancode  The byte to push.\r
+**/\r
+VOID\r
+PushScancodeBufTail (\r
+  IN  SCAN_CODE_QUEUE       *Queue,\r
+  IN  UINT8                 Scancode\r
+  )\r
+{\r
+  if (GetScancodeBufCount (Queue) == KEYBOARD_SCAN_CODE_MAX_COUNT - 1) {\r
+    PopScancodeBufHead (Queue, 1, NULL);\r
+  }\r
+\r
+  Queue->Buffer[Queue->Tail] = Scancode;\r
+  Queue->Tail = (Queue->Tail + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT;\r
+}\r
+\r
+/**\r
+  Read data register .\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
 \r
-  @return return the value \r
+  @return return the value\r
 \r
 **/\r
 UINT8\r
@@ -600,7 +717,7 @@ KeyReadDataRegister (
 }\r
 \r
 /**\r
-  Write data register\r
+  Write data register.\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param Data      value wanted to be written\r
@@ -611,27 +728,18 @@ KeyWriteDataRegister (
   IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
   IN UINT8                   Data\r
   )\r
-\r
 {\r
-  EFI_ISA_IO_PROTOCOL                 *IsaIo;\r
-\r
-  //\r
-  // Use IsaIo protocol to perform IO operations\r
-  //\r
-  IsaIo = ConsoleIn->IsaIo;\r
-\r
-  IsaIo->Io.Write (\r
-              IsaIo,\r
-              EfiIsaIoWidthUint8,\r
-              ConsoleIn->DataRegisterAddress,\r
-              1,\r
-              &Data\r
-              );\r
-\r
+  ConsoleIn->IsaIo->Io.Write (\r
+                         ConsoleIn->IsaIo,\r
+                         EfiIsaIoWidthUint8,\r
+                         ConsoleIn->DataRegisterAddress,\r
+                         1,\r
+                         &Data\r
+                         );\r
 }\r
 \r
 /**\r
-  Read status register\r
+  Read status register.\r
 \r
   @param ConsoleIn  Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
 \r
@@ -643,63 +751,45 @@ KeyReadStatusRegister (
   IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
   )\r
 {\r
-  EFI_ISA_IO_PROTOCOL                 *IsaIo;\r
   UINT8                               Data;\r
-\r
-  //\r
-  // Use IsaIo protocol to perform IO operations\r
-  //\r
-  IsaIo = ConsoleIn->IsaIo;\r
-\r
-  IsaIo->Io.Read (\r
-              IsaIo,\r
-              EfiIsaIoWidthUint8,\r
-              ConsoleIn->StatusRegisterAddress,\r
-              1,\r
-              &Data\r
-              );\r
-\r
+  ConsoleIn->IsaIo->Io.Read (\r
+                         ConsoleIn->IsaIo,\r
+                         EfiIsaIoWidthUint8,\r
+                         ConsoleIn->StatusRegisterAddress,\r
+                         1,\r
+                         &Data\r
+                         );\r
   return Data;\r
-\r
 }\r
 \r
 /**\r
-  Write command register \r
+  Write command register .\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param Data      The value wanted to be written\r
 \r
 **/\r
-\r
 VOID\r
 KeyWriteCommandRegister (\r
   IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
   IN UINT8                   Data\r
   )\r
 {\r
-  EFI_ISA_IO_PROTOCOL                 *IsaIo;\r
-\r
-  //\r
-  // Use IsaIo protocol to perform IO operations\r
-  //\r
-  IsaIo = ConsoleIn->IsaIo;\r
-\r
-  IsaIo->Io.Write (\r
-              IsaIo,\r
-              EfiIsaIoWidthUint8,\r
-              ConsoleIn->CommandRegisterAddress,\r
-              1,\r
-              &Data\r
-              );\r
-\r
+  ConsoleIn->IsaIo->Io.Write (\r
+                         ConsoleIn->IsaIo,\r
+                         EfiIsaIoWidthUint8,\r
+                         ConsoleIn->CommandRegisterAddress,\r
+                         1,\r
+                         &Data\r
+                         );\r
 }\r
 \r
 /**\r
-  Display error message\r
+  Display error message.\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param ErrMsg    Unicode string of error message\r
-  \r
+\r
 **/\r
 VOID\r
 KeyboardError (\r
@@ -733,7 +823,7 @@ KeyboardTimerHandler (
   EFI_TPL                 OldTpl;\r
   KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;\r
 \r
-  ConsoleIn = Context;\r
+  ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *) Context;\r
 \r
   //\r
   // Enter critical section\r
@@ -750,184 +840,31 @@ KeyboardTimerHandler (
 \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
+  // 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
   // 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
-  Data = 0;\r
-\r
-  //\r
-  // if there is no key present, just return\r
-  //\r
-  if ((KeyReadStatusRegister (Context) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) != KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) {\r
+  while ((KeyReadStatusRegister (ConsoleIn) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) ==\r
+      KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA\r
+     ) {\r
     //\r
-    // Leave critical section and return\r
+    // Read one byte of the scan code and store it into the memory buffer\r
     //\r
-    gBS->RestoreTPL (OldTpl);\r
-\r
-    return ;\r
+    Data = KeyReadDataRegister (ConsoleIn);\r
+    PushScancodeBufTail (&ConsoleIn->ScancodeQueue, Data);\r
   }\r
-  //\r
-  // Read one byte of the scan code and store it into the memory buffer\r
-  //\r
-  if (ConsoleIn->ScancodeBufCount < KEYBOARD_BUFFER_MAX_COUNT) {\r
-\r
-    Data = KeyReadDataRegister (Context);\r
-    //\r
-    // put the scancode into the memory scancode buffer\r
-    //\r
-    ConsoleIn->ScancodeBufCount++;\r
-    ConsoleIn->ScancodeBufEndPos++;\r
-    if (ConsoleIn->ScancodeBufEndPos >= KEYBOARD_BUFFER_MAX_COUNT) {\r
-      ConsoleIn->ScancodeBufEndPos = 0;\r
-    }\r
-\r
-    ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufEndPos] = Data;\r
-\r
-    //\r
-    // Handle Alt+Ctrl+Del Key combination\r
-    //\r
-    switch (Data) {\r
-    case SCANCODE_CTRL_MAKE:\r
-      ConsoleIn->Ctrled = TRUE;\r
-      break;\r
-\r
-    case SCANCODE_CTRL_BREAK:\r
-      ConsoleIn->Ctrled = FALSE;\r
-      break;\r
-\r
-    case SCANCODE_ALT_MAKE:\r
-      ConsoleIn->Alted = TRUE;\r
-      break;\r
+  KeyGetchar (ConsoleIn);\r
 \r
-    case SCANCODE_ALT_BREAK:\r
-      ConsoleIn->Alted = FALSE;\r
-      break;\r
-    }\r
-    //\r
-    // if Alt+Ctrl+Del, Reboot the System\r
-    //\r
-    if (ConsoleIn->Ctrled && ConsoleIn->Alted && Data == 0x53) {\r
-      gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
-    }\r
-  }\r
   //\r
   // Leave critical section and return\r
   //\r
   gBS->RestoreTPL (OldTpl);\r
-\r
-  return ;\r
-}\r
-\r
-/**\r
-  Read several bytes from the scancode buffer without removing them.\r
-  This function is called to see if there are enough bytes of scancode\r
-  representing a single key.\r
-\r
-  @param Count - Number of bytes to be read\r
-  @param Buf - Store the results\r
-\r
-  @retval EFI_SUCCESS success to scan the keyboard code\r
-  @retval EFI_NOT_READY invalid parameter\r
-**/\r
-EFI_STATUS\r
-GetScancodeBufHead (\r
-  KEYBOARD_CONSOLE_IN_DEV    *ConsoleIn,\r
-  IN UINT32                  Count,\r
-  OUT UINT8                  *Buf\r
-  )\r
-{\r
-  UINT32  Index;\r
-  UINT32  Pos;\r
-\r
-  Index = 0;\r
-  Pos   = 0;\r
-\r
-  //\r
-  // check the valid range of parameter 'Count'\r
-  //\r
-  if (Count <= 0 || ConsoleIn->ScancodeBufCount < Count) {\r
-    return EFI_NOT_READY;\r
-  }\r
-  //\r
-  // retrieve the values\r
-  //\r
-  for (Index = 0; Index < Count; Index++) {\r
-\r
-    if (Index == 0) {\r
-\r
-      Pos = ConsoleIn->ScancodeBufStartPos;\r
-    } else {\r
-\r
-      Pos = Pos + 1;\r
-      if (Pos >= KEYBOARD_BUFFER_MAX_COUNT) {\r
-        Pos = 0;\r
-      }\r
-    }\r
-\r
-    Buf[Index] = ConsoleIn->ScancodeBuf[Pos];\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-\r
-  Read & remove several bytes from the scancode buffer.\r
-  This function is usually called after GetScancodeBufHead()\r
-\r
-  @param Count - Number of bytes to be read\r
-  @param Buf - Store the results\r
-\r
-  @retval EFI_SUCCESS success to scan the keyboard code\r
-  @retval EFI_NOT_READY invalid parameter\r
-**/\r
-EFI_STATUS\r
-PopScancodeBufHead (\r
-  KEYBOARD_CONSOLE_IN_DEV   *ConsoleIn,\r
-  IN UINT32                 Count,\r
-  OUT UINT8                 *Buf\r
-  )\r
-{\r
-  UINT32  Index;\r
-\r
-  Index = 0;\r
-\r
-  //\r
-  // Check the valid range of parameter 'Count'\r
-  //\r
-  if (Count <= 0 || ConsoleIn->ScancodeBufCount < Count) {\r
-    return EFI_NOT_READY;\r
-  }\r
-  //\r
-  // Retrieve and remove the values\r
-  //\r
-  for (Index = 0; Index < Count; Index++) {\r
-\r
-    if (Index != 0) {\r
-\r
-      ConsoleIn->ScancodeBufStartPos++;\r
-      if (ConsoleIn->ScancodeBufStartPos >= KEYBOARD_BUFFER_MAX_COUNT) {\r
-        ConsoleIn->ScancodeBufStartPos = 0;\r
-      }\r
-    }\r
-\r
-    Buf[Index] = ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufStartPos];\r
-    ConsoleIn->ScancodeBufCount--;\r
-  }\r
-\r
-  ConsoleIn->ScancodeBufStartPos++;\r
-  if (ConsoleIn->ScancodeBufStartPos >= KEYBOARD_BUFFER_MAX_COUNT) {\r
-    ConsoleIn->ScancodeBufStartPos = 0;\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
-  Read key value \r
+  Read key value .\r
 \r
   @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param Data      - Pointer to outof buffer for keeping key value\r
@@ -962,7 +899,7 @@ KeyboardRead (
     MicroSecondDelay (30);\r
   }\r
 \r
-  if (!RegFilled) {\r
+  if (RegFilled == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
 \r
@@ -995,7 +932,7 @@ KeyboardWrite (
   // wait for input buffer empty\r
   //\r
   for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
-    if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {\r
+    if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
       RegEmptied = 1;\r
       break;\r
     }\r
@@ -1003,7 +940,7 @@ KeyboardWrite (
     MicroSecondDelay (30);\r
   }\r
 \r
-  if (!RegEmptied) {\r
+  if (RegEmptied == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
   //\r
@@ -1015,12 +952,12 @@ KeyboardWrite (
 }\r
 \r
 /**\r
-  Issue keyboard command\r
+  Issue keyboard command.\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
-  @param Data      The buff holding the command \r
+  @param Data      The buff holding the command\r
 \r
-  @retval EFI_TIMEOUT Keyboard is not ready to issuing \r
+  @retval EFI_TIMEOUT Keyboard is not ready to issuing\r
   @retval EFI_SUCCESS Success to issue keyboard command\r
 \r
 **/\r
@@ -1040,7 +977,7 @@ KeyboardCommand (
   // Wait For Input Buffer Empty\r
   //\r
   for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
-    if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {\r
+    if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
       RegEmptied = 1;\r
       break;\r
     }\r
@@ -1048,7 +985,7 @@ KeyboardCommand (
     MicroSecondDelay (30);\r
   }\r
 \r
-  if (!RegEmptied) {\r
+  if (RegEmptied == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
   //\r
@@ -1061,7 +998,7 @@ KeyboardCommand (
   //\r
   RegEmptied = 0;\r
   for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
-    if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {\r
+    if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
       RegEmptied = 1;\r
       break;\r
     }\r
@@ -1069,7 +1006,7 @@ KeyboardCommand (
     MicroSecondDelay (30);\r
   }\r
 \r
-  if (!RegEmptied) {\r
+  if (RegEmptied == 0) {\r
     return EFI_TIMEOUT;\r
   }\r
 \r
@@ -1086,7 +1023,7 @@ KeyboardCommand (
 \r
   @retval EFI_TIMEOUT Fail to get specific value in given time\r
   @retval EFI_SUCCESS Success to get specific value in given time.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 KeyboardWaitForValue (\r
@@ -1155,7 +1092,7 @@ KeyboardWaitForValue (
   indicators in ConsoleIn.\r
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
-  \r
+\r
   @return status of updating keyboard register\r
 \r
 **/\r
@@ -1204,240 +1141,228 @@ UpdateStatusLights (
 }\r
 \r
 /**\r
-  Get scancode from scancode buffer\r
-  and translate into EFI-scancode and unicode defined by EFI spec\r
-  The function is always called in TPL_NOTIFY\r
+  Initialize the key state.\r
 \r
-  @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer\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
-  @retval EFI_NOT_READY  Input from console not ready yet.\r
-  @retval EFI_SUCCESS    Function executed successfully.\r
+  The function is always called in TPL_NOTIFY.\r
+\r
+  @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer\r
 \r
 **/\r
-EFI_STATUS\r
+VOID\r
 KeyGetchar (\r
   IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  UINT8       ScanCode;\r
-  UINT8       Readed;\r
-  BOOLEAN     Extended;\r
-  UINT8       ScancodeArr[4];\r
-  UINTN       Index;\r
-  //\r
-  // 4 bytes most\r
+  EFI_STATUS                     Status;\r
+  UINT16                         ScanCode;\r
+  BOOLEAN                        Extend0;\r
+  BOOLEAN                        Extend1;\r
+  UINTN                          Index;\r
+  EFI_KEY_DATA                   KeyData;\r
+  LIST_ENTRY                     *Link;\r
+  KEYBOARD_CONSOLE_IN_EX_NOTIFY  *CurrentNotify;\r
   //\r
-  UINT32      ScancodeArrPos;\r
+  // 3 bytes most\r
   //\r
-  // point to the current position in ScancodeArr\r
-  //\r
-\r
-  Readed          = 0;\r
-  Extended        = FALSE;\r
-  ScancodeArrPos  = 0;\r
+  UINT8                          ScancodeArr[3];\r
+  UINT32                         ScancodeArrPos;\r
 \r
-  //\r
-  // Read one byte of the scan code and store it into the memory buffer\r
-  // This block of code is added to insert an action that is equivalent to\r
-  // the timer event handling function, so as to increase the frequency of\r
-  // detecting the availability of keys. Timer event has a max frequency of\r
-  // 18Hz which is insufficient\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
-  // 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
-\r
-  if (((KeyReadStatusRegister (ConsoleIn) & 0x21) == 0x1) && (ConsoleIn->ScancodeBufCount < KEYBOARD_BUFFER_MAX_COUNT)) {\r
-\r
-    Readed = KeyReadDataRegister (ConsoleIn);\r
-    //\r
-    // put the scancode into the memory scancode buffer\r
-    //\r
-    ConsoleIn->ScancodeBufCount++;\r
-    ConsoleIn->ScancodeBufEndPos++;\r
-    if (ConsoleIn->ScancodeBufEndPos >= KEYBOARD_BUFFER_MAX_COUNT) {\r
-      ConsoleIn->ScancodeBufEndPos = 0;\r
-    }\r
-\r
-    ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufEndPos] = Readed;\r
-\r
-    //\r
-    // Handle Alt+Ctrl+Del Key combination\r
-    //\r
-    switch (Readed) {\r
-\r
-    case SCANCODE_CTRL_MAKE:\r
-      ConsoleIn->Ctrled = TRUE;\r
-      break;\r
-\r
-    case SCANCODE_CTRL_BREAK:\r
-      ConsoleIn->Ctrled = FALSE;\r
-      break;\r
-\r
-    case SCANCODE_ALT_MAKE:\r
-      ConsoleIn->Alted = TRUE;\r
-      break;\r
-\r
-    case SCANCODE_ALT_BREAK:\r
-      ConsoleIn->Alted = FALSE;\r
-      break;\r
-    }\r
-    //\r
-    // if Alt+Ctrl+Del, Reboot the System\r
-    //\r
-    if (ConsoleIn->Ctrled && ConsoleIn->Alted && Readed == 0x53) {\r
-      gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
-    }\r
-  }\r
   //\r
   // Check if there are enough bytes of scancode representing a single key\r
   // available in the buffer\r
   //\r
   while (TRUE) {\r
-\r
-    Status          = GetScancodeBufHead (ConsoleIn, 1, ScancodeArr);\r
-    ScancodeArrPos  = 0;\r
+    Extend0        = FALSE;\r
+    Extend1        = FALSE;\r
+    ScancodeArrPos = 0;\r
+    Status  = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
     if (EFI_ERROR (Status)) {\r
-      return EFI_NOT_READY;\r
+      return ;\r
     }\r
 \r
-    if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED) {\r
-      Extended        = TRUE;\r
-      Status          = GetScancodeBufHead (ConsoleIn, 2, ScancodeArr);\r
-      ScancodeArrPos  = 1;\r
-      if (EFI_ERROR (Status)) {\r
-        return EFI_NOT_READY;\r
-      }\r
-    }\r
-    //\r
-    // Checks for key scancode for PAUSE:E1-1D/45-E1/9D-C5\r
-    // if present, ignore them\r
-    //\r
-    if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED1) {\r
-\r
-      Status          = GetScancodeBufHead (ConsoleIn, 2, ScancodeArr);\r
-      ScancodeArrPos  = 1;\r
-\r
+    if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED0) {\r
+      //\r
+      // E0 to look ahead 2 bytes\r
+      //\r
+      Extend0 = TRUE;\r
+      ScancodeArrPos = 1;\r
+      Status         = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
       if (EFI_ERROR (Status)) {\r
-        return EFI_NOT_READY;\r
+        return ;\r
       }\r
-\r
-      Status          = GetScancodeBufHead (ConsoleIn, 3, ScancodeArr);\r
-      ScancodeArrPos  = 2;\r
-\r
+    } else if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED1) {\r
+      //\r
+      // E1 to look ahead 3 bytes\r
+      //\r
+      Extend1 = TRUE;\r
+      ScancodeArrPos = 2;\r
+      Status         = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
       if (EFI_ERROR (Status)) {\r
-        return EFI_NOT_READY;\r
+        return ;\r
       }\r
-\r
-      PopScancodeBufHead (ConsoleIn, 3, ScancodeArr);\r
-      return EFI_NOT_READY;\r
     }\r
     //\r
     // if we reach this position, scancodes for a key is in buffer now,pop them\r
     //\r
-    Status = PopScancodeBufHead (ConsoleIn, ScancodeArrPos + 1, ScancodeArr);\r
-    if (EFI_ERROR (Status)) {\r
-      return EFI_NOT_READY;\r
-    }\r
+    Status = PopScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
     //\r
     // store the last available byte, this byte of scancode will be checked\r
     //\r
     ScanCode = ScancodeArr[ScancodeArrPos];\r
 \r
-    //\r
-    // Check for special keys and update the driver state.\r
-    //\r
-    switch (ScanCode) {\r
+    if (!Extend1) {\r
+      //\r
+      // Check for special keys and update the driver state.\r
+      //\r
+      switch (ScanCode) {\r
 \r
-    case SCANCODE_CTRL_MAKE:\r
-      ConsoleIn->Ctrl = TRUE;\r
-      break;\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
 \r
-    case SCANCODE_CTRL_BREAK:\r
-      ConsoleIn->Ctrl = FALSE;\r
-      break;\r
+      case SCANCODE_ALT_MAKE:\r
+          if (Extend0) {\r
+            ConsoleIn->RightAlt = TRUE;\r
+          } else {\r
+            ConsoleIn->LeftAlt  = TRUE;\r
+          }\r
+        break;\r
+      case SCANCODE_ALT_BREAK:\r
+          if (Extend0) {\r
+            ConsoleIn->RightAlt = FALSE;\r
+          } else {\r
+            ConsoleIn->LeftAlt  = FALSE;\r
+          }\r
+        break;\r
 \r
-    case SCANCODE_ALT_MAKE:\r
-      ConsoleIn->Alt = TRUE;\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
+        continue;\r
 \r
-    case SCANCODE_ALT_BREAK:\r
-      ConsoleIn->Alt = FALSE;\r
-      break;\r
+      case SCANCODE_LEFT_SHIFT_BREAK:\r
+        if (!Extend0) {\r
+          ConsoleIn->LeftShift = FALSE;\r
+        }\r
+        break;\r
 \r
-    case SCANCODE_LEFT_SHIFT_MAKE:\r
-      if (!Extended) {\r
-        ConsoleIn->Shift     = TRUE;\r
-        ConsoleIn->LeftShift = TRUE;\r
-      }      \r
-      break;\r
-    case SCANCODE_RIGHT_SHIFT_MAKE:\r
-      if (!Extended) {\r
-        ConsoleIn->Shift = TRUE;\r
+      case SCANCODE_RIGHT_SHIFT_MAKE:\r
         ConsoleIn->RightShift = TRUE;\r
-      }\r
-      break;\r
-\r
-    case SCANCODE_LEFT_SHIFT_BREAK:\r
-      if (!Extended) {\r
-        ConsoleIn->Shift     = FALSE;\r
-        ConsoleIn->LeftShift = FALSE;\r
-      } else {\r
-        ConsoleIn->SysReq    = FALSE;\r
-      }      \r
-      break;\r
-    case SCANCODE_RIGHT_SHIFT_BREAK:\r
-      if (!Extended) {\r
-        ConsoleIn->Shift = FALSE;\r
+        break;\r
+      case SCANCODE_RIGHT_SHIFT_BREAK:\r
         ConsoleIn->RightShift = FALSE;\r
-      }\r
-      break;\r
+        break;\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_RIGHT_LOGO_MAKE:\r
-      ConsoleIn->RightLogo = TRUE;\r
-      break;\r
-    case SCANCODE_RIGHT_LOGO_BREAK:\r
-      ConsoleIn->RightLogo = FALSE;\r
-      break;      \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_SYS_REQ_MAKE:\r
-      if (Extended) {\r
-        ConsoleIn->SysReq = TRUE;\r
-      }\r
-    case SCANCODE_CAPS_LOCK_MAKE:\r
-      ConsoleIn->CapsLock = (BOOLEAN)!ConsoleIn->CapsLock;\r
-      UpdateStatusLights (ConsoleIn);\r
-      break;\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_NUM_LOCK_MAKE:\r
-      ConsoleIn->NumLock = (BOOLEAN)!ConsoleIn->NumLock;\r
-      UpdateStatusLights (ConsoleIn);\r
-      break;\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_SCROLL_LOCK_MAKE:\r
-      ConsoleIn->ScrollLock = (BOOLEAN)!ConsoleIn->ScrollLock;\r
-      UpdateStatusLights (ConsoleIn);\r
-      break;\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
+        break;\r
+      case SCANCODE_SYS_REQ_BREAK:\r
+        if (Extend0) {\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
+\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
+          UpdateStatusLights (ConsoleIn);\r
+        }\r
+        break;\r
+      }\r
     }\r
+\r
     //\r
-    // If this is a BREAK Key or above the valid range, ignore it\r
+    // If this is above the valid range, ignore it\r
     //\r
     if (ScanCode >= SCANCODE_MAX_MAKE) {\r
       continue;\r
@@ -1445,55 +1370,82 @@ KeyGetchar (
       break;\r
     }\r
   }\r
+\r
   //\r
-  // Treat Numeric Key Pad "/" specially\r
+  // Handle Ctrl+Alt+Del hotkey\r
   //\r
-  if (Extended && ScanCode == 0x35) {\r
-    ConsoleIn->Key.ScanCode     = SCAN_NULL;\r
-    ConsoleIn->Key.UnicodeChar  = L'/';\r
-    return EFI_SUCCESS;\r
+  if ((ConsoleIn->LeftCtrl || ConsoleIn->RightCtrl) &&\r
+      (ConsoleIn->LeftAlt  || ConsoleIn->RightAlt ) &&\r
+      ScanCode == SCANCODE_DELETE_MAKE\r
+     ) {\r
+    gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
   }\r
+\r
+  //\r
+  // Save the Shift/Toggle state\r
+  //\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
+    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
+    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
+    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
+    KeyData.Key.UnicodeChar = CHAR_NULL;\r
+    KeyData.Key.ScanCode    = SCAN_NULL;\r
+\r
   //\r
-  // Convert Keyboard ScanCode into an EFI Key\r
+  // Except the above special case, all others can be handled by convert table\r
   //\r
-  for (Index = 0; ConvertKeyboardScanCodeToEfiKey[Index].ScanCode != TABLE_END; Index += 1) {\r
-    if (ScanCode == ConvertKeyboardScanCodeToEfiKey[Index].ScanCode) {\r
-      ConsoleIn->Key.ScanCode = ConvertKeyboardScanCodeToEfiKey[Index].EfiScanCode;\r
-      if (ConsoleIn->Shift) {\r
-        ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
+  } else {\r
+    for (Index = 0; ConvertKeyboardScanCodeToEfiKey[Index].ScanCode != TABLE_END; Index++) {\r
+      if (ScanCode == ConvertKeyboardScanCodeToEfiKey[Index].ScanCode) {\r
+        KeyData.Key.ScanCode    = ConvertKeyboardScanCodeToEfiKey[Index].EfiScanCode;\r
+        KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
+\r
+        if ((ConsoleIn->LeftShift || ConsoleIn->RightShift) &&\r
+            (ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar != ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar)) {\r
+          KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
+          //\r
+          // Need not return associated shift state if a class of printable characters that\r
+          // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'\r
+          //\r
+          KeyData.KeyState.KeyShiftState &= ~(EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED);\r
+        }\r
         //\r
-        // Need not return associated shift state if a class of printable characters that\r
-        // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'\r
+        // alphabetic key is affected by CapsLock State\r
         //\r
-        if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
-          ConsoleIn->LeftShift  = FALSE;\r
-          ConsoleIn->RightShift = FALSE;\r
-        }\r
-      } else {\r
-        ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
-      }\r
-      //\r
-      // alphabetic key is affected by CapsLock State\r
-      //\r
-      if (ConsoleIn->CapsLock) {\r
-        if (ConsoleIn->Key.UnicodeChar >= L'a' && ConsoleIn->Key.UnicodeChar <= L'z') {\r
-          ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
-        } else if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
-          ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
-        }\r
-      }\r
-      //\r
-      // Translate the CTRL-Alpha characters to their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
-      //\r
-      if (ConsoleIn->Ctrled) {\r
-        if (ConsoleIn->Key.UnicodeChar >= L'a' && ConsoleIn->Key.UnicodeChar <= L'z') {\r
-          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - L'a' + 1);\r
-        } else if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
-          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - L'A' + 1);\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
+          }\r
         }\r
+        break;\r
       }\r
-\r
-      break;\r
     }\r
   }\r
 \r
@@ -1501,62 +1453,48 @@ KeyGetchar (
   // distinguish numeric key pad keys' 'up symbol' and 'down symbol'\r
   //\r
   if (ScanCode >= 0x47 && ScanCode <= 0x53) {\r
-\r
-    if (ConsoleIn->NumLock && !ConsoleIn->Shift && !Extended) {\r
-      ConsoleIn->Key.ScanCode = SCAN_NULL;\r
+    if (ConsoleIn->NumLock && !(ConsoleIn->LeftShift || ConsoleIn->RightShift) && !Extend0) {\r
+      KeyData.Key.ScanCode = SCAN_NULL;\r
     } else if (ScanCode != 0x4a && ScanCode != 0x4e) {\r
-      ConsoleIn->Key.UnicodeChar = 0x0000;\r
+      KeyData.Key.UnicodeChar = CHAR_NULL;\r
     }\r
   }\r
+\r
   //\r
   // If the key can not be converted then just return.\r
   //\r
-  if (ConsoleIn->Key.ScanCode == SCAN_NULL && ConsoleIn->Key.UnicodeChar == 0x0000) {\r
-    return EFI_NOT_READY;\r
+  if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == CHAR_NULL) {\r
+    if (!ConsoleIn->IsSupportPartialKey) {\r
+      return ;\r
+    }\r
   }\r
 \r
   //\r
-  // Save the Shift/Toggle state\r
+  // Signal KeyNotify process event if this key pressed matches any key registered.\r
   //\r
-  if (ConsoleIn->Ctrl) {\r
-    ConsoleIn->KeyState.KeyShiftState  |= (Extended == TRUE) ? EFI_RIGHT_CONTROL_PRESSED : EFI_LEFT_CONTROL_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->Alt) {                \r
-    ConsoleIn->KeyState.KeyShiftState  |= (Extended == TRUE) ? EFI_RIGHT_ALT_PRESSED : EFI_LEFT_ALT_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->LeftShift) {          \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_LEFT_SHIFT_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->RightShift) {         \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_RIGHT_SHIFT_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->LeftLogo) {           \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_LEFT_LOGO_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->RightLogo) {          \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_RIGHT_LOGO_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->Menu) {               \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_MENU_KEY_PRESSED;\r
-  }                                    \r
-  if (ConsoleIn->SysReq) {             \r
-    ConsoleIn->KeyState.KeyShiftState  |= EFI_SYS_REQ_PRESSED;\r
-  }  \r
-  if (ConsoleIn->CapsLock) {\r
-    ConsoleIn->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
-  }\r
-  if (ConsoleIn->NumLock) {\r
-    ConsoleIn->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
-  }\r
-  if (ConsoleIn->ScrollLock) {\r
-    ConsoleIn->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
+  for (Link = GetFirstNode (&ConsoleIn->NotifyList); !IsNull (&ConsoleIn->NotifyList, Link); Link = GetNextNode (&ConsoleIn->NotifyList, Link)) {\r
+    CurrentNotify = CR (\r
+                      Link,\r
+                      KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+                      NotifyEntry,\r
+                      KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+                      );\r
+    if (IsKeyRegistered (&CurrentNotify->KeyData, &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
+    }\r
   }\r
 \r
-  return EFI_SUCCESS;\r
+  PushEfikeyBufTail (&ConsoleIn->EfiKeyQueue, &KeyData);\r
 }\r
 \r
 /**\r
-  Perform 8042 controller and keyboard Initialization\r
+  Perform 8042 controller and keyboard Initialization.\r
   If ExtendedVerification is TRUE, do additional test for\r
   the keyboard interface\r
 \r
@@ -1601,16 +1539,18 @@ InitKeyboard (
   // Perform a read to cleanup the Status Register's\r
   // output buffer full bits within MAX TRY times\r
   //\r
-  while (!EFI_ERROR (Status) && TryTime < KEYBOARD_MAX_TRY) {\r
-    Status = KeyboardRead (ConsoleIn, &CommandByte);\r
-    TryTime ++;\r
-  }\r
-  //\r
-  // Exceed the max try times. The device may be error.\r
-  //\r
-  if (TryTime == KEYBOARD_MAX_TRY) {\r
-       Status = EFI_DEVICE_ERROR;\r
-       goto Done;\r
+  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
+    }\r
+    //\r
+    // Exceed the max try times. The device may be error.\r
+    //\r
+    if (TryTime == KEYBOARD_MAX_TRY) {\r
+      Status = EFI_DEVICE_ERROR;\r
+      goto Done;\r
+    }\r
   }\r
   //\r
   // We should disable mouse interface during the initialization process\r
@@ -1624,35 +1564,38 @@ InitKeyboard (
   // Test the system flag in to determine whether this is the first\r
   // time initialization\r
   //\r
-  if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG)) {\r
-    //\r
-    // 8042 controller is already setup (by myself or by mouse driver):\r
-    //   See whether mouse interface is already enabled\r
-    //   which determines whether we should enable it later\r
-    //\r
-    //\r
-    // Read the command byte of 8042 controller\r
-    //\r
-    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_READ);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"\n\r");\r
-      goto Done;\r
-    }\r
+  if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG) != 0) {\r
+    if (!PcdGetBool (PcdFastPS2Detection)) {\r
+      //\r
+      // 8042 controller is already setup (by myself or by mouse driver):\r
+      //   See whether mouse interface is already enabled\r
+      //   which determines whether we should enable it later\r
+      //\r
+      //\r
+      // Read the command byte of 8042 controller\r
+      //\r
+      Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_READ);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"\n\r");\r
+        goto Done;\r
+      }\r
 \r
-    Status = KeyboardRead (ConsoleIn, &CommandByte);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"\n\r");\r
-      goto Done;\r
-    }\r
-    //\r
-    // Test the mouse enabling bit\r
-    //\r
-    if (CommandByte & 0x20) {\r
-      mEnableMouseInterface = FALSE;\r
+      Status = KeyboardRead (ConsoleIn, &CommandByte);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"\n\r");\r
+        goto Done;\r
+      }\r
+      //\r
+      // Test the mouse enabling bit\r
+      //\r
+      if ((CommandByte & 0x20) != 0) {\r
+        mEnableMouseInterface = FALSE;\r
+      } else {\r
+        mEnableMouseInterface = TRUE;\r
+      }\r
     } else {\r
-      mEnableMouseInterface = TRUE;\r
+      mEnableMouseInterface = FALSE;\r
     }\r
-\r
   } else {\r
     //\r
     // 8042 controller is not setup yet:\r
@@ -1662,36 +1605,38 @@ InitKeyboard (
     //\r
     // Disable keyboard and mouse interfaces\r
     //\r
-    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"\n\r");\r
-      goto Done;\r
-    }\r
+    if (!PcdGetBool (PcdFastPS2Detection)) {\r
+      Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"\n\r");\r
+        goto Done;\r
+      }\r
 \r
-    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"\n\r");\r
-      goto Done;\r
-    }\r
+      Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"\n\r");\r
+        goto Done;\r
+      }\r
 \r
-    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
-      EFI_PROGRESS_CODE,\r
-      EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST,\r
-      ConsoleIn->DevicePath\r
-      );\r
-    //\r
-    // 8042 Controller Self Test\r
-    //\r
-    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
-      goto Done;\r
-    }\r
+      REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+        EFI_PROGRESS_CODE,\r
+        EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST,\r
+        ConsoleIn->DevicePath\r
+        );\r
+      //\r
+      // 8042 Controller Self Test\r
+      //\r
+      Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
+        goto Done;\r
+      }\r
 \r
-    Status = KeyboardWaitForValue (ConsoleIn, 0x55);\r
-    if (EFI_ERROR (Status)) {\r
-      KeyboardError (ConsoleIn, L"8042 controller self test failed!\n\r");\r
-      goto Done;\r
+      Status = KeyboardWaitForValue (ConsoleIn, 0x55);\r
+      if (EFI_ERROR (Status)) {\r
+        KeyboardError (ConsoleIn, L"8042 controller self test failed!\n\r");\r
+        goto Done;\r
+      }\r
     }\r
     //\r
     // Don't enable mouse interface later\r
@@ -1736,32 +1681,35 @@ InitKeyboard (
   //\r
   // Clear Memory Scancode Buffer\r
   //\r
-  ConsoleIn->ScancodeBufStartPos  = 0;\r
-  ConsoleIn->ScancodeBufEndPos    = KEYBOARD_BUFFER_MAX_COUNT - 1;\r
-  ConsoleIn->ScancodeBufCount     = 0;\r
-  ConsoleIn->Ctrled               = FALSE;\r
-  ConsoleIn->Alted                = FALSE;\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
   //\r
-  ConsoleIn->Ctrl       = FALSE;\r
-  ConsoleIn->Alt        = FALSE;\r
-  ConsoleIn->Shift      = FALSE;\r
   ConsoleIn->CapsLock   = FALSE;\r
   ConsoleIn->NumLock    = FALSE;\r
   ConsoleIn->ScrollLock = FALSE;\r
+  ConsoleIn->LeftCtrl   = FALSE;\r
+  ConsoleIn->RightCtrl  = FALSE;\r
+  ConsoleIn->LeftAlt    = FALSE;\r
+  ConsoleIn->RightAlt   = FALSE;\r
   ConsoleIn->LeftShift  = FALSE;\r
   ConsoleIn->RightShift = FALSE;\r
   ConsoleIn->LeftLogo   = FALSE;\r
   ConsoleIn->RightLogo  = FALSE;\r
   ConsoleIn->Menu       = FALSE;\r
-  ConsoleIn->SysReq     = FALSE;  \r
+  ConsoleIn->SysReq     = FALSE;\r
 \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
@@ -1901,9 +1849,9 @@ Done:
 }\r
 \r
 /**\r
-  Disable the keyboard interface of the 8042 controller\r
+  Disable the keyboard interface of the 8042 controller.\r
 \r
-  @param ConsoleIn   - the device instance\r
+  @param ConsoleIn   The device instance\r
 \r
   @return status of issuing disable command\r
 \r
@@ -1946,34 +1894,37 @@ CheckKeyboardConnect (
   EFI_STATUS     Status;\r
   UINTN          WaitForValueTimeOutBcakup;\r
 \r
-  Status = EFI_SUCCESS;\r
   //\r
   // enable keyboard itself and wait for its ack\r
   // If can't receive ack, Keyboard should not be connected.\r
   //\r
-  Status = KeyboardWrite (\r
-             ConsoleIn,\r
-             KEYBOARD_KBEN\r
-             );\r
+  if (!PcdGetBool (PcdFastPS2Detection)) {\r
+    Status = KeyboardWrite (\r
+               ConsoleIn,\r
+               KEYBOARD_KBEN\r
+               );\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    return FALSE;\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 = WaitForValueTimeOutBcakup;\r
+    if (EFI_ERROR (Status)) {\r
+      return FALSE;\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 = WaitForValueTimeOutBcakup;\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    return FALSE;\r
-  }\r
+    if (EFI_ERROR (Status)) {\r
+      return FALSE;\r
+    }\r
 \r
-  return TRUE;\r
+    return TRUE;\r
+  } else {\r
+    return TRUE;\r
+  }\r
 }\r
 \r