]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c
MdeModulePkg/Usb/EfiKey: Fix endpoint selection
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / KeyBoard.c
index a231bd6e3eff4d80b4bb238266f6eacf20fe7bf1..7eb65b8815c18f1e32ae8c8266d840a64fd5d72d 100644 (file)
@@ -1,22 +1,13 @@
 /** @file\r
   Helper functions for USB Keyboard Driver.\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "KeyBoard.h"\r
 \r
-EFI_GUID  mUsbKeyboardLayoutPackageGuid = USB_KEYBOARD_LAYOUT_PACKAGE_GUID;\r
-EFI_GUID  mUsbKeyboardLayoutKeyGuid = USB_KEYBOARD_LAYOUT_KEY_GUID;\r
-\r
 USB_KEYBOARD_LAYOUT_PACK_BIN  mUsbKeyboardLayoutBin = {\r
   sizeof (USB_KEYBOARD_LAYOUT_PACK_BIN),   // Binary size\r
 \r
@@ -82,6 +73,7 @@ USB_KEYBOARD_LAYOUT_PACK_BIN  mUsbKeyboardLayoutBin = {
     {EfiKeyD11,        '[',      '{',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
     {EfiKeyD12,        ']',      '}',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
     {EfiKeyD13,        '\\',     '|',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
+    {EfiKeyC12,        '\\',     '|',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
     {EfiKeyC10,        ';',      ':',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
     {EfiKeyC11,        '\'',     '"',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
     {EfiKeyE0,         '`',      '~',  0, 0,  EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT},\r
@@ -299,6 +291,17 @@ UINT8 ModifierValueToEfiScanCodeConvertionTable[] = {
   SCAN_F10,        // EFI_FUNCTION_KEY_TEN_MODIFIER\r
   SCAN_F11,        // EFI_FUNCTION_KEY_ELEVEN_MODIFIER\r
   SCAN_F12,        // EFI_FUNCTION_KEY_TWELVE_MODIFIER\r
+  //\r
+  // For Partial Keystroke support\r
+  //\r
+  SCAN_NULL,       // EFI_PRINT_MODIFIER\r
+  SCAN_NULL,       // EFI_SYS_REQUEST_MODIFIER\r
+  SCAN_NULL,       // EFI_SCROLL_LOCK_MODIFIER\r
+  SCAN_PAUSE,      // EFI_PAUSE_MODIFIER\r
+  SCAN_NULL,       // EFI_BREAK_MODIFIER\r
+  SCAN_NULL,       // EFI_LEFT_LOGO_MODIFIER\r
+  SCAN_NULL,       // EFI_RIGHT_LOGO_MODIFER\r
+  SCAN_NULL,       // EFI_MENU_MODIFER\r
 };\r
 \r
 /**\r
@@ -310,7 +313,6 @@ UINT8 ModifierValueToEfiScanCodeConvertionTable[] = {
   @retval Others               Failure to install default keyboard layout.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InstallDefaultKeyboardLayout (\r
    IN OUT USB_KB_DEV           *UsbKeyboardDevice\r
   )\r
@@ -335,7 +337,7 @@ InstallDefaultKeyboardLayout (
   // Install Keyboard Layout package to HII database\r
   //\r
   HiiHandle = HiiAddPackages (\r
-                &mUsbKeyboardLayoutPackageGuid,\r
+                &gUsbKeyboardLayoutPackageGuid,\r
                 UsbKeyboardDevice->ControllerHandle,\r
                 &mUsbKeyboardLayoutBin,\r
                 NULL\r
@@ -347,7 +349,7 @@ InstallDefaultKeyboardLayout (
   //\r
   // Set current keyboard layout\r
   //\r
-  Status = HiiDatabase->SetKeyboardLayout (HiiDatabase, &mUsbKeyboardLayoutKeyGuid);\r
+  Status = HiiDatabase->SetKeyboardLayout (HiiDatabase, &gUsbKeyboardLayoutKeyGuid);\r
 \r
   return Status;\r
 }\r
@@ -363,7 +365,6 @@ InstallDefaultKeyboardLayout (
 \r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 IsUSBKeyboard (\r
   IN  EFI_USB_IO_PROTOCOL       *UsbIo\r
   )\r
@@ -401,7 +402,6 @@ IsUSBKeyboard (
 \r
 **/\r
 EFI_HII_KEYBOARD_LAYOUT *\r
-EFIAPI\r
 GetCurrentKeyboardLayout (\r
   VOID\r
   )\r
@@ -464,7 +464,6 @@ GetCurrentKeyboardLayout (
 \r
 **/\r
 EFI_KEY_DESCRIPTOR *\r
-EFIAPI\r
 GetKeyDescriptor (\r
   IN USB_KB_DEV        *UsbKeyboardDevice,\r
   IN UINT8             KeyCode\r
@@ -502,7 +501,6 @@ GetKeyDescriptor (
 \r
 **/\r
 USB_NS_KEY *\r
-EFIAPI\r
 FindUsbNsKey (\r
   IN USB_KB_DEV          *UsbKeyboardDevice,\r
   IN EFI_KEY_DESCRIPTOR  *KeyDescriptor\r
@@ -511,7 +509,7 @@ FindUsbNsKey (
   LIST_ENTRY      *Link;\r
   LIST_ENTRY      *NsKeyList;\r
   USB_NS_KEY      *UsbNsKey;\r
-  \r
+\r
   NsKeyList = &UsbKeyboardDevice->NsKeyList;\r
   Link = GetFirstNode (NsKeyList);\r
   while (!IsNull (NsKeyList, Link)) {\r
@@ -543,7 +541,6 @@ FindUsbNsKey (
 \r
 **/\r
 EFI_KEY_DESCRIPTOR *\r
-EFIAPI\r
 FindPhysicalKey (\r
   IN USB_NS_KEY          *UsbNsKey,\r
   IN EFI_KEY_DESCRIPTOR  *KeyDescriptor\r
@@ -632,13 +629,18 @@ SetKeyboardLayoutEvent (
     //\r
     KeyCode = EfiKeyToUsbKeyCodeConvertionTable [(UINT8) (TempKey.Key)];\r
     TableEntry = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);\r
+    if (TableEntry == NULL) {\r
+      ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
+      FreePool (KeyboardLayout);\r
+      return;\r
+    }\r
     CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));\r
 \r
     //\r
     // For non-spacing key, create the list with a non-spacing key followed by physical keys.\r
     //\r
     if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) {\r
-      UsbNsKey = AllocatePool (sizeof (USB_NS_KEY));\r
+      UsbNsKey = AllocateZeroPool (sizeof (USB_NS_KEY));\r
       ASSERT (UsbNsKey != NULL);\r
 \r
       //\r
@@ -646,7 +648,7 @@ SetKeyboardLayoutEvent (
       //\r
       KeyCount = 0;\r
       NsKey = KeyDescriptor + 1;\r
-      for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) {\r
+      for (Index2 = (UINT8) Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) {\r
         CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR));\r
         if (TempKey.Modifier == EFI_NS_KEY_DEPENDENCY_MODIFIER) {\r
           KeyCount++;\r
@@ -691,7 +693,6 @@ SetKeyboardLayoutEvent (
 \r
 **/\r
 VOID\r
-EFIAPI\r
 ReleaseKeyboardLayoutResources (\r
   IN OUT USB_KB_DEV              *UsbKeyboardDevice\r
   )\r
@@ -730,7 +731,6 @@ ReleaseKeyboardLayoutResources (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InitKeyboardLayout (\r
   OUT USB_KB_DEV   *UsbKeyboardDevice\r
   )\r
@@ -782,7 +782,7 @@ InitKeyboardLayout (
     //\r
     InstallDefaultKeyboardLayout (UsbKeyboardDevice);\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -797,60 +797,58 @@ InitKeyboardLayout (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InitUSBKeyboard (\r
   IN OUT USB_KB_DEV   *UsbKeyboardDevice\r
   )\r
 {\r
   UINT16              ConfigValue;\r
   UINT8               Protocol;\r
-  UINT8               ReportId;\r
-  UINT8               Duration;\r
   EFI_STATUS          Status;\r
   UINT32              TransferResult;\r
 \r
   REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    FixedPcdGet32 (PcdStatusCodeValueKeyboardSelfTest),\r
+    (EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST),\r
     UsbKeyboardDevice->DevicePath\r
     );\r
 \r
-  InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
+  InitQueue (&UsbKeyboardDevice->UsbKeyQueue, sizeof (USB_KEY));\r
+  InitQueue (&UsbKeyboardDevice->EfiKeyQueue, sizeof (EFI_KEY_DATA));\r
+  InitQueue (&UsbKeyboardDevice->EfiKeyQueueForNotify, sizeof (EFI_KEY_DATA));\r
 \r
   //\r
   // Use the config out of the descriptor\r
   // Assumed the first config is the correct one and this is not always the case\r
   //\r
   Status = UsbGetConfiguration (\r
-             UsbKeyboardDevice->UsbIo, \r
-             &ConfigValue, \r
-             &TransferResult\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    ConfigValue = 0x01;\r
-  }\r
-  \r
-  //\r
-  // Uses default configuration to configure the USB Keyboard device.\r
-  //\r
-  Status = UsbSetConfiguration (\r
              UsbKeyboardDevice->UsbIo,\r
-             ConfigValue,\r
+             &ConfigValue,\r
              &TransferResult\r
              );\r
   if (EFI_ERROR (Status)) {\r
+    ConfigValue = 0x01;\r
     //\r
-    // If configuration could not be set here, it means\r
-    // the keyboard interface has some errors and could\r
-    // not be initialized\r
+    // Uses default configuration to configure the USB Keyboard device.\r
     //\r
-    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
-      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      FixedPcdGet32 (PcdStatusCodeValueKeyboardInterfaceError),\r
-      UsbKeyboardDevice->DevicePath\r
-      );\r
+    Status = UsbSetConfiguration (\r
+               UsbKeyboardDevice->UsbIo,\r
+               ConfigValue,\r
+               &TransferResult\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // If configuration could not be set here, it means\r
+      // the keyboard interface has some errors and could\r
+      // not be initialized\r
+      //\r
+      REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+        EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+        (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INTERFACE_ERROR),\r
+        UsbKeyboardDevice->DevicePath\r
+        );\r
 \r
-    return EFI_DEVICE_ERROR;\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
   }\r
 \r
   UsbGetProtocolRequest (\r
@@ -870,30 +868,13 @@ InitUSBKeyboard (
       );\r
   }\r
 \r
-  //\r
-  // ReportId is zero, which means the idle rate applies to all input reports.\r
-  //\r
-  ReportId = 0;\r
-  //\r
-  // Duration is zero, which means the duration is infinite.\r
-  // so the endpoint will inhibit reporting forever,\r
-  // and only reporting when a change is detected in the report data.\r
-  //\r
-  Duration = 0;\r
-  UsbSetIdleRequest (\r
-    UsbKeyboardDevice->UsbIo,\r
-    UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,\r
-    ReportId,\r
-    Duration\r
-    );\r
-\r
   UsbKeyboardDevice->CtrlOn     = FALSE;\r
   UsbKeyboardDevice->AltOn      = FALSE;\r
   UsbKeyboardDevice->ShiftOn    = FALSE;\r
   UsbKeyboardDevice->NumLockOn  = FALSE;\r
   UsbKeyboardDevice->CapsOn     = FALSE;\r
   UsbKeyboardDevice->ScrollOn   = FALSE;\r
-  \r
+\r
   UsbKeyboardDevice->LeftCtrlOn   = FALSE;\r
   UsbKeyboardDevice->LeftAltOn    = FALSE;\r
   UsbKeyboardDevice->LeftShiftOn  = FALSE;\r
@@ -986,12 +967,10 @@ KeyboardHandler (
   UINT8               CurModifierMap;\r
   UINT8               OldModifierMap;\r
   UINT8               Mask;\r
-  UINT8               Index;\r
+  UINTN               Index;\r
   UINT8               Index2;\r
-  BOOLEAN             Down;\r
   BOOLEAN             KeyRelease;\r
   BOOLEAN             KeyPress;\r
-  UINT8               SavedTail;\r
   USB_KEY             UsbKey;\r
   UINT8               NewRepeatKey;\r
   UINT32              UsbStatus;\r
@@ -1012,7 +991,7 @@ KeyboardHandler (
     //\r
     REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      FixedPcdGet32 (PcdStatusCodeValueKeyboardInputError),\r
+      (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INPUT_ERROR),\r
       UsbKeyboardDevice->DevicePath\r
       );\r
 \r
@@ -1037,7 +1016,7 @@ KeyboardHandler (
 \r
     //\r
     // Delete & Submit this interrupt again\r
-    // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. \r
+    // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.\r
     //\r
     UsbIo->UsbAsyncInterruptTransfer (\r
              UsbIo,\r
@@ -1074,6 +1053,10 @@ KeyboardHandler (
   // Byte 1 is reserved.\r
   // Bytes 2 to 7 are keycodes.\r
   //\r
+  if (DataLength < 8) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   CurKeyCodeBuffer  = (UINT8 *) Data;\r
   OldKeyCodeBuffer  = UsbKeyboardDevice->LastKeyCodeArray;\r
 \r
@@ -1119,8 +1102,9 @@ KeyboardHandler (
       // otherwise it is a non-zero value.\r
       // Insert the changed modifier key into key buffer.\r
       //\r
-      Down = (BOOLEAN) ((CurModifierMap & Mask) != 0);\r
-      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), (UINT8) (0xe0 + Index), Down);\r
+      UsbKey.KeyCode = (UINT8) (0xe0 + Index);\r
+      UsbKey.Down    = (BOOLEAN) ((CurModifierMap & Mask) != 0);\r
+      Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey));\r
     }\r
   }\r
 \r
@@ -1152,11 +1136,9 @@ KeyboardHandler (
     }\r
 \r
     if (KeyRelease) {\r
-      InsertKeyCode (\r
-        &(UsbKeyboardDevice->KeyboardBuffer),\r
-        OldKeyCodeBuffer[Index],\r
-        FALSE\r
-        );\r
+      UsbKey.KeyCode = OldKeyCodeBuffer[Index];\r
+      UsbKey.Down    = FALSE;\r
+      Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey));\r
       //\r
       // The original repeat key is released.\r
       //\r
@@ -1204,13 +1186,17 @@ KeyboardHandler (
     }\r
 \r
     if (KeyPress) {\r
-      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], TRUE);\r
+      UsbKey.KeyCode = CurKeyCodeBuffer[Index];\r
+      UsbKey.Down    = TRUE;\r
+      Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey));\r
 \r
       //\r
       // Handle repeat key\r
       //\r
       KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);\r
-      ASSERT (KeyDescriptor != NULL);\r
+      if (KeyDescriptor == NULL) {\r
+        continue;\r
+      }\r
 \r
       if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) {\r
         //\r
@@ -1234,72 +1220,6 @@ KeyboardHandler (
     UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index];\r
   }\r
 \r
-  //\r
-  // Pre-process KeyboardBuffer to check if Ctrl + Alt + Del is pressed.\r
-  //\r
-  SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail;\r
-  Index     = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
-  while (Index != SavedTail) {\r
-    RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
-\r
-    KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
-    ASSERT (KeyDescriptor != NULL);\r
-\r
-    switch (KeyDescriptor->Modifier) {\r
-\r
-    case EFI_LEFT_CONTROL_MODIFIER:\r
-    case EFI_RIGHT_CONTROL_MODIFIER:\r
-      if (UsbKey.Down) {\r
-        UsbKeyboardDevice->CtrlOn = TRUE;\r
-      } else {\r
-        UsbKeyboardDevice->CtrlOn = FALSE;\r
-      }\r
-      break;\r
-\r
-    case EFI_LEFT_ALT_MODIFIER:\r
-    case EFI_RIGHT_ALT_MODIFIER:\r
-      if (UsbKey.Down) {\r
-        UsbKeyboardDevice->AltOn = TRUE;\r
-      } else {\r
-        UsbKeyboardDevice->AltOn = FALSE;\r
-      }\r
-      break;\r
-\r
-    case EFI_ALT_GR_MODIFIER:\r
-      if (UsbKey.Down) {\r
-        UsbKeyboardDevice->AltGrOn = TRUE;\r
-      } else {\r
-        UsbKeyboardDevice->AltGrOn = FALSE;\r
-      }\r
-      break;\r
-\r
-    //\r
-    // For Del Key, check if Ctrl + Alt + Del occurs for reset.\r
-    //\r
-    case EFI_DELETE_MODIFIER:\r
-      if (UsbKey.Down) {\r
-        if ((UsbKeyboardDevice->CtrlOn) && (UsbKeyboardDevice->AltOn)) {\r
-          gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
-        }\r
-      }\r
-      break;\r
-\r
-    default:\r
-      break;\r
-    }\r
-\r
-    //\r
-    // Insert the key back to the buffer,\r
-    // so the key sequence will not be destroyed.\r
-    //\r
-    InsertKeyCode (\r
-      &(UsbKeyboardDevice->KeyboardBuffer),\r
-      UsbKey.KeyCode,\r
-      UsbKey.Down\r
-      );\r
-    Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
-\r
-  }\r
   //\r
   // If there is new key pressed, update the RepeatKey value, and set the\r
   // timer to repeate delay timer\r
@@ -1336,7 +1256,6 @@ KeyboardHandler (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 USBParseKey (\r
   IN OUT  USB_KB_DEV  *UsbKeyboardDevice,\r
      OUT  UINT8       *KeyCode\r
@@ -1347,15 +1266,16 @@ USBParseKey (
 \r
   *KeyCode = 0;\r
 \r
-  while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {\r
+  while (!IsQueueEmpty (&UsbKeyboardDevice->UsbKeyQueue)) {\r
     //\r
     // Pops one raw data off.\r
     //\r
-    RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
+    Dequeue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey));\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
-    ASSERT (KeyDescriptor != NULL);\r
-\r
+    if (KeyDescriptor == NULL) {\r
+      continue;\r
+    }\r
     if (!UsbKey.Down) {\r
       //\r
       // Key is released.\r
@@ -1452,12 +1372,10 @@ USBParseKey (
     case EFI_LEFT_CONTROL_MODIFIER:\r
       UsbKeyboardDevice->LeftCtrlOn = TRUE;\r
       UsbKeyboardDevice->CtrlOn = TRUE;\r
-      continue;\r
       break;\r
     case EFI_RIGHT_CONTROL_MODIFIER:\r
       UsbKeyboardDevice->RightCtrlOn = TRUE;\r
       UsbKeyboardDevice->CtrlOn = TRUE;\r
-      continue;\r
       break;\r
 \r
     //\r
@@ -1466,12 +1384,10 @@ USBParseKey (
     case EFI_LEFT_SHIFT_MODIFIER:\r
       UsbKeyboardDevice->LeftShiftOn = TRUE;\r
       UsbKeyboardDevice->ShiftOn = TRUE;\r
-      continue;\r
       break;\r
     case EFI_RIGHT_SHIFT_MODIFIER:\r
       UsbKeyboardDevice->RightShiftOn = TRUE;\r
       UsbKeyboardDevice->ShiftOn = TRUE;\r
-      continue;\r
       break;\r
 \r
     //\r
@@ -1480,12 +1396,10 @@ USBParseKey (
     case EFI_LEFT_ALT_MODIFIER:\r
       UsbKeyboardDevice->LeftAltOn = TRUE;\r
       UsbKeyboardDevice->AltOn = TRUE;\r
-      continue;\r
       break;\r
     case EFI_RIGHT_ALT_MODIFIER:\r
       UsbKeyboardDevice->RightAltOn = TRUE;\r
       UsbKeyboardDevice->AltOn = TRUE;\r
-      continue;\r
       break;\r
 \r
     //\r
@@ -1515,7 +1429,6 @@ USBParseKey (
     case EFI_PRINT_MODIFIER:\r
     case EFI_SYS_REQUEST_MODIFIER:\r
       UsbKeyboardDevice->SysReqOn = TRUE;\r
-      continue;\r
       break;\r
 \r
     //\r
@@ -1531,7 +1444,6 @@ USBParseKey (
       //\r
       UsbKeyboardDevice->NumLockOn = (BOOLEAN) (!(UsbKeyboardDevice->NumLockOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
-      continue;\r
       break;\r
 \r
     case EFI_CAPS_LOCK_MODIFIER:\r
@@ -1540,7 +1452,6 @@ USBParseKey (
       //\r
       UsbKeyboardDevice->CapsOn = (BOOLEAN) (!(UsbKeyboardDevice->CapsOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
-      continue;\r
       break;\r
 \r
     case EFI_SCROLL_LOCK_MODIFIER:\r
@@ -1549,18 +1460,6 @@ USBParseKey (
       //\r
       UsbKeyboardDevice->ScrollOn = (BOOLEAN) (!(UsbKeyboardDevice->ScrollOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
-      continue;\r
-      break;\r
-\r
-    //\r
-    // PrintScreen, Pause/Break could not be retrieved via SimpleTextInEx protocol\r
-    //\r
-    case EFI_PAUSE_MODIFIER:\r
-    case EFI_BREAK_MODIFIER:\r
-      //\r
-      // Fall through\r
-      //\r
-      continue;\r
       break;\r
 \r
     default:\r
@@ -1583,13 +1482,72 @@ USBParseKey (
   return EFI_NOT_READY;\r
 }\r
 \r
+/**\r
+  Initialize the key state.\r
+\r
+  @param  UsbKeyboardDevice     The USB_KB_DEV instance.\r
+  @param  KeyState              A pointer to receive the key state information.\r
+**/\r
+VOID\r
+InitializeKeyState (\r
+  IN  USB_KB_DEV           *UsbKeyboardDevice,\r
+  OUT EFI_KEY_STATE        *KeyState\r
+  )\r
+{\r
+  KeyState->KeyShiftState  = EFI_SHIFT_STATE_VALID;\r
+  KeyState->KeyToggleState = EFI_TOGGLE_STATE_VALID;\r
+\r
+  if (UsbKeyboardDevice->LeftCtrlOn) {\r
+    KeyState->KeyShiftState |= EFI_LEFT_CONTROL_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->RightCtrlOn) {\r
+    KeyState->KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->LeftAltOn) {\r
+    KeyState->KeyShiftState |= EFI_LEFT_ALT_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->RightAltOn) {\r
+    KeyState->KeyShiftState |= EFI_RIGHT_ALT_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->LeftShiftOn) {\r
+    KeyState->KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->RightShiftOn) {\r
+    KeyState->KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->LeftLogoOn) {\r
+    KeyState->KeyShiftState |= EFI_LEFT_LOGO_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->RightLogoOn) {\r
+    KeyState->KeyShiftState |= EFI_RIGHT_LOGO_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->MenuKeyOn) {\r
+    KeyState->KeyShiftState |= EFI_MENU_KEY_PRESSED;\r
+  }\r
+  if (UsbKeyboardDevice->SysReqOn) {\r
+    KeyState->KeyShiftState |= EFI_SYS_REQ_PRESSED;\r
+  }\r
+\r
+  if (UsbKeyboardDevice->ScrollOn) {\r
+    KeyState->KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
+  }\r
+  if (UsbKeyboardDevice->NumLockOn) {\r
+    KeyState->KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
+  }\r
+  if (UsbKeyboardDevice->CapsOn) {\r
+    KeyState->KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
+  }\r
+  if (UsbKeyboardDevice->IsSupportPartialKey) {\r
+    KeyState->KeyToggleState |= EFI_KEY_STATE_EXPOSED;\r
+  }\r
+}\r
 \r
 /**\r
   Converts USB Keycode ranging from 0x4 to 0x65 to EFI_INPUT_KEY.\r
 \r
   @param  UsbKeyboardDevice     The USB_KB_DEV instance.\r
   @param  KeyCode               Indicates the key code that will be interpreted.\r
-  @param  Key                   A pointer to a buffer that is filled in with\r
+  @param  KeyData               A pointer to a buffer that is filled in with\r
                                 the keystroke information for the key that\r
                                 was pressed.\r
 \r
@@ -1601,27 +1559,24 @@ USBParseKey (
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 UsbKeyCodeToEfiInputKey (\r
-  IN  USB_KB_DEV      *UsbKeyboardDevice,\r
-  IN  UINT8           KeyCode,\r
-  OUT EFI_INPUT_KEY   *Key\r
+  IN  USB_KB_DEV                *UsbKeyboardDevice,\r
+  IN  UINT8                     KeyCode,\r
+  OUT EFI_KEY_DATA              *KeyData\r
   )\r
 {\r
-  EFI_KEY_DESCRIPTOR  *KeyDescriptor;\r
+  EFI_KEY_DESCRIPTOR            *KeyDescriptor;\r
+  LIST_ENTRY                    *Link;\r
+  LIST_ENTRY                    *NotifyList;\r
+  KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
 \r
   //\r
-  // KeyCode must in the range of 0x4 to 0x65\r
+  // KeyCode must in the range of  [0x4, 0x65] or [0xe0, 0xe7].\r
   //\r
-  if (!USBKBD_VALID_KEYCODE (KeyCode)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  if ((KeyCode - 4) >= NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
   KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);\r
-  ASSERT (KeyDescriptor != NULL);\r
+  if (KeyDescriptor == NULL) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
 \r
   if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {\r
     //\r
@@ -1643,63 +1598,52 @@ UsbKeyCodeToEfiInputKey (
   //\r
   // Make sure modifier of Key Descriptor is in the valid range according to UEFI spec.\r
   //\r
-  if (KeyDescriptor->Modifier > EFI_FUNCTION_KEY_TWELVE_MODIFIER) {\r
+  if (KeyDescriptor->Modifier >= (sizeof (ModifierValueToEfiScanCodeConvertionTable) / sizeof (UINT8))) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  Key->ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
-  Key->UnicodeChar = KeyDescriptor->Unicode;\r
+  KeyData->Key.ScanCode    = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
+  KeyData->Key.UnicodeChar = KeyDescriptor->Unicode;\r
 \r
   if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) {\r
     if (UsbKeyboardDevice->ShiftOn) {\r
-      Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
+      KeyData->Key.UnicodeChar = KeyDescriptor->ShiftedUnicode;\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
       //\r
-      if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
+      if ((KeyDescriptor->Unicode != CHAR_NULL) && (KeyDescriptor->ShiftedUnicode != CHAR_NULL) &&\r
+          (KeyDescriptor->Unicode != KeyDescriptor->ShiftedUnicode)) {\r
         UsbKeyboardDevice->LeftShiftOn = FALSE;\r
         UsbKeyboardDevice->RightShiftOn = FALSE;\r
       }\r
 \r
       if (UsbKeyboardDevice->AltGrOn) {\r
-        Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode;\r
+        KeyData->Key.UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode;\r
       }\r
     } else {\r
       //\r
       // Shift off\r
       //\r
-      Key->UnicodeChar = KeyDescriptor->Unicode;\r
+      KeyData->Key.UnicodeChar = KeyDescriptor->Unicode;\r
 \r
       if (UsbKeyboardDevice->AltGrOn) {\r
-        Key->UnicodeChar = KeyDescriptor->AltGrUnicode;\r
+        KeyData->Key.UnicodeChar = KeyDescriptor->AltGrUnicode;\r
       }\r
     }\r
   }\r
 \r
   if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
     if (UsbKeyboardDevice->CapsOn) {\r
-      if (Key->UnicodeChar == KeyDescriptor->Unicode) {\r
-        Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
-      } else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) {\r
-        Key->UnicodeChar = KeyDescriptor->Unicode;\r
+      if (KeyData->Key.UnicodeChar == KeyDescriptor->Unicode) {\r
+        KeyData->Key.UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
+      } else if (KeyData->Key.UnicodeChar == KeyDescriptor->ShiftedUnicode) {\r
+        KeyData->Key.UnicodeChar = KeyDescriptor->Unicode;\r
       }\r
     }\r
   }\r
 \r
-  //\r
-  // Translate the CTRL-Alpha characters to their corresponding control value\r
-  // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
-  //\r
-  if (UsbKeyboardDevice->CtrlOn) {\r
-    if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {\r
-      Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'a' + 1);\r
-    } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') {\r
-      Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'A' + 1);\r
-    }\r
-  }\r
-\r
   if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) != 0) {\r
     //\r
     // For key affected by NumLock, if NumLock is on and Shift is not pressed, then it means\r
@@ -1707,205 +1651,201 @@ UsbKeyCodeToEfiInputKey (
     // Otherwise, it means control key, so preserve the EFI Scan Code and clear the unicode keycode.\r
     //\r
     if ((UsbKeyboardDevice->NumLockOn) && (!(UsbKeyboardDevice->ShiftOn))) {\r
-      Key->ScanCode = SCAN_NULL;\r
+      KeyData->Key.ScanCode = SCAN_NULL;\r
     } else {\r
-      Key->UnicodeChar = 0x00;\r
+      KeyData->Key.UnicodeChar = CHAR_NULL;\r
     }\r
   }\r
 \r
   //\r
   // Translate Unicode 0x1B (ESC) to EFI Scan Code\r
   //\r
-  if (Key->UnicodeChar == 0x1B && Key->ScanCode == SCAN_NULL) {\r
-    Key->ScanCode = SCAN_ESC;\r
-    Key->UnicodeChar = 0x00;\r
+  if (KeyData->Key.UnicodeChar == 0x1B && KeyData->Key.ScanCode == SCAN_NULL) {\r
+    KeyData->Key.ScanCode = SCAN_ESC;\r
+    KeyData->Key.UnicodeChar = CHAR_NULL;\r
   }\r
 \r
   //\r
   // Not valid for key without both unicode key code and EFI Scan Code.\r
   //\r
-  if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {\r
+  if (KeyData->Key.UnicodeChar == 0 && KeyData->Key.ScanCode == SCAN_NULL) {\r
+    if (!UsbKeyboardDevice->IsSupportPartialKey) {\r
     return EFI_NOT_READY;\r
+    }\r
   }\r
 \r
-\r
   //\r
   // Save Shift/Toggle state\r
   //\r
-  if (UsbKeyboardDevice->LeftCtrlOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->RightCtrlOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->LeftAltOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->RightAltOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->LeftShiftOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->RightShiftOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->LeftLogoOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->RightLogoOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->MenuKeyOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED;\r
-  }\r
-  if (UsbKeyboardDevice->SysReqOn) {\r
-    UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED;\r
-  }\r
+  InitializeKeyState (UsbKeyboardDevice, &KeyData->KeyState);\r
 \r
-  if (UsbKeyboardDevice->ScrollOn) {\r
-    UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
-  }\r
-  if (UsbKeyboardDevice->NumLockOn) {\r
-    UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
-  }\r
-  if (UsbKeyboardDevice->CapsOn) {\r
-    UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
+  //\r
+  // Signal KeyNotify process event if this key pressed matches any key registered.\r
+  //\r
+  NotifyList = &UsbKeyboardDevice->NotifyList;\r
+  for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList, Link); Link = GetNextNode (NotifyList, Link)) {\r
+    CurrentNotify = CR (Link, KEYBOARD_CONSOLE_IN_EX_NOTIFY, NotifyEntry, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE);\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
+      Enqueue (&UsbKeyboardDevice->EfiKeyQueueForNotify, KeyData, sizeof (*KeyData));\r
+      gBS->SignalEvent (UsbKeyboardDevice->KeyNotifyProcessEvent);\r
+      break;\r
+    }\r
   }\r
 \r
   return EFI_SUCCESS;\r
-\r
 }\r
 \r
-\r
 /**\r
-  Resets USB keyboard buffer.\r
+  Create the queue.\r
 \r
-  @param  KeyboardBuffer     Points to the USB keyboard buffer.\r
+  @param  Queue     Points to the queue.\r
+  @param  ItemSize  Size of the single item.\r
 \r
 **/\r
 VOID\r
-EFIAPI\r
-InitUSBKeyBuffer (\r
-  OUT  USB_KB_BUFFER   *KeyboardBuffer\r
+InitQueue (\r
+  IN OUT  USB_SIMPLE_QUEUE   *Queue,\r
+  IN      UINTN              ItemSize\r
   )\r
 {\r
-  ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));\r
+  UINTN                      Index;\r
+\r
+  Queue->ItemSize  = ItemSize;\r
+  Queue->Head      = 0;\r
+  Queue->Tail      = 0;\r
+\r
+  if (Queue->Buffer[0] != NULL) {\r
+    FreePool (Queue->Buffer[0]);\r
+  }\r
 \r
-  KeyboardBuffer->BufferHead = KeyboardBuffer->BufferTail;\r
+  Queue->Buffer[0] = AllocatePool (sizeof (Queue->Buffer) / sizeof (Queue->Buffer[0]) * ItemSize);\r
+  ASSERT (Queue->Buffer[0] != NULL);\r
+\r
+  for (Index = 1; Index < sizeof (Queue->Buffer) / sizeof (Queue->Buffer[0]); Index++) {\r
+    Queue->Buffer[Index] = ((UINT8 *) Queue->Buffer[Index - 1]) + ItemSize;\r
+  }\r
+}\r
+\r
+/**\r
+  Destroy the queue\r
+\r
+  @param Queue    Points to the queue.\r
+**/\r
+VOID\r
+DestroyQueue (\r
+  IN OUT USB_SIMPLE_QUEUE   *Queue\r
+  )\r
+{\r
+  FreePool (Queue->Buffer[0]);\r
 }\r
 \r
 \r
 /**\r
-  Check whether USB keyboard buffer is empty.\r
+  Check whether the queue is empty.\r
 \r
-  @param  KeyboardBuffer     USB keyboard buffer\r
+  @param  Queue     Points to the queue.\r
 \r
-  @retval TRUE               Keyboard buffer is empty.\r
-  @retval FALSE              Keyboard buffer is not empty.\r
+  @retval TRUE      Queue is empty.\r
+  @retval FALSE     Queue is not empty.\r
 \r
 **/\r
 BOOLEAN\r
-EFIAPI\r
-IsUSBKeyboardBufferEmpty (\r
-  IN  USB_KB_BUFFER   *KeyboardBuffer\r
+IsQueueEmpty (\r
+  IN  USB_SIMPLE_QUEUE   *Queue\r
   )\r
 {\r
   //\r
   // Meet FIFO empty condition\r
   //\r
-  return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail);\r
+  return (BOOLEAN) (Queue->Head == Queue->Tail);\r
 }\r
 \r
 \r
 /**\r
-  Check whether USB keyboard buffer is full.\r
+  Check whether the queue is full.\r
 \r
-  @param  KeyboardBuffer     USB keyboard buffer\r
+  @param  Queue     Points to the queue.\r
 \r
-  @retval TRUE               Keyboard buffer is full.\r
-  @retval FALSE              Keyboard buffer is not full.\r
+  @retval TRUE      Queue is full.\r
+  @retval FALSE     Queue is not full.\r
 \r
 **/\r
 BOOLEAN\r
-EFIAPI\r
-IsUSBKeyboardBufferFull (\r
-  IN  USB_KB_BUFFER   *KeyboardBuffer\r
+IsQueueFull (\r
+  IN  USB_SIMPLE_QUEUE   *Queue\r
   )\r
 {\r
-  return (BOOLEAN)(((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)) == KeyboardBuffer->BufferHead);\r
+  return (BOOLEAN) (((Queue->Tail + 1) % (MAX_KEY_ALLOWED + 1)) == Queue->Head);\r
 }\r
 \r
 \r
 /**\r
-  Inserts a keycode into keyboard buffer.\r
-\r
-  @param  KeyboardBuffer     Points to the USB keyboard buffer.\r
-  @param  Key                Keycode to insert.\r
-  @param  Down               TRUE means key is pressed.\r
-                             FALSE means key is released.\r
+  Enqueue the item to the queue.\r
 \r
+  @param  Queue     Points to the queue.\r
+  @param  Item      Points to the item to be enqueued.\r
+  @param  ItemSize  Size of the item.\r
 **/\r
 VOID\r
-EFIAPI\r
-InsertKeyCode (\r
-  IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
-  IN      UINT8         Key,\r
-  IN      BOOLEAN       Down\r
+Enqueue (\r
+  IN OUT  USB_SIMPLE_QUEUE *Queue,\r
+  IN      VOID             *Item,\r
+  IN      UINTN            ItemSize\r
   )\r
 {\r
-  USB_KEY UsbKey;\r
-\r
+  ASSERT (ItemSize == Queue->ItemSize);\r
   //\r
   // If keyboard buffer is full, throw the\r
   // first key out of the keyboard buffer.\r
   //\r
-  if (IsUSBKeyboardBufferFull (KeyboardBuffer)) {\r
-    RemoveKeyCode (KeyboardBuffer, &UsbKey);\r
+  if (IsQueueFull (Queue)) {\r
+    Queue->Head = (Queue->Head + 1) % (MAX_KEY_ALLOWED + 1);\r
   }\r
 \r
-  ASSERT (KeyboardBuffer->BufferTail <= MAX_KEY_ALLOWED);\r
-\r
-  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;\r
-  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down    = Down;\r
+  CopyMem (Queue->Buffer[Queue->Tail], Item, ItemSize);\r
 \r
   //\r
   // Adjust the tail pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->BufferTail = (UINT8) ((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1));\r
+  Queue->Tail = (Queue->Tail + 1) % (MAX_KEY_ALLOWED + 1);\r
 }\r
 \r
 \r
 /**\r
-  Remove a keycode from keyboard buffer and return it.\r
+  Dequeue a item from the queue.\r
 \r
-  @param  KeyboardBuffer     Points to the USB keyboard buffer.\r
-  @param  UsbKey             Points to the buffer that contains keycode for output.\r
+  @param  Queue     Points to the queue.\r
+  @param  Item      Receives the item.\r
+  @param  ItemSize  Size of the item.\r
 \r
-  @retval EFI_SUCCESS        Keycode successfully removed from keyboard buffer.\r
-  @retval EFI_DEVICE_ERROR   Keyboard buffer is empty.\r
+  @retval EFI_SUCCESS        Item was successfully dequeued.\r
+  @retval EFI_DEVICE_ERROR   The queue is empty.\r
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-RemoveKeyCode (\r
-  IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
-     OUT  USB_KEY       *UsbKey\r
+Dequeue (\r
+  IN OUT  USB_SIMPLE_QUEUE *Queue,\r
+     OUT  VOID             *Item,\r
+  IN      UINTN            ItemSize\r
   )\r
 {\r
-  if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {\r
+  ASSERT (Queue->ItemSize == ItemSize);\r
+\r
+  if (IsQueueEmpty (Queue)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  ASSERT (KeyboardBuffer->BufferHead <= MAX_KEY_ALLOWED);\r
-\r
-  UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode;\r
-  UsbKey->Down    = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down;\r
+  CopyMem (Item, Queue->Buffer[Queue->Head], ItemSize);\r
 \r
   //\r
   // Adjust the head pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->BufferHead = (UINT8) ((KeyboardBuffer->BufferHead + 1) % (MAX_KEY_ALLOWED + 1));\r
+  Queue->Head = (Queue->Head + 1) % (MAX_KEY_ALLOWED + 1);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1918,7 +1858,6 @@ RemoveKeyCode (
 \r
 **/\r
 VOID\r
-EFIAPI\r
 SetKeyLED (\r
   IN  USB_KB_DEV    *UsbKeyboardDevice\r
   )\r
@@ -1970,6 +1909,7 @@ USBKeyboardRepeatHandler (
   )\r
 {\r
   USB_KB_DEV  *UsbKeyboardDevice;\r
+  USB_KEY     UsbKey;\r
 \r
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
 \r
@@ -1980,11 +1920,9 @@ USBKeyboardRepeatHandler (
     //\r
     // Inserts the repeat key into keyboard buffer,\r
     //\r
-    InsertKeyCode (\r
-      &(UsbKeyboardDevice->KeyboardBuffer),\r
-      UsbKeyboardDevice->RepeatKey,\r
-      TRUE\r
-      );\r
+    UsbKey.KeyCode = UsbKeyboardDevice->RepeatKey;\r
+    UsbKey.Down    = TRUE;\r
+    Enqueue (&UsbKeyboardDevice->UsbKeyQueue, &UsbKey, sizeof (UsbKey));\r
 \r
     //\r
     // Set repeat rate for next repeat key generation.\r