]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c
Pass ECC check.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / KeyBoard.c
index 0d7d3d1bad2d6ccc7e189ffcf1ebdfce73d0a18f..cf682505b9499924187be40729f9026a6d7ceb76 100644 (file)
@@ -15,10 +15,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "KeyBoard.h"\r
 \r
 //\r
-// Static English keyboard layout\r
+// Default English keyboard layout\r
 // Format:<efi key>, <unicode without shift>, <unicode with shift>, <Modifier>, <AffectedAttribute>\r
 //\r
-UINT8 KeyboardLayoutTable[USB_KEYCODE_MAX_MAKE + 8][5] = {\r
+// According to Universal Serial Bus HID Usage Tables document ver 1.12,\r
+// a Boot Keyboard should support the keycode range from 0x0 to 0x65 and 0xE0 to 0xE7.\r
+// 0x0 to 0x3 are reserved for typical keyboard status or keyboard errors, so they are excluded.\r
+//\r
+UINT8 KeyboardLayoutTable[NUMBER_OF_VALID_USB_KEYCODE][5] = {\r
   {EfiKeyC1,         'a',      'A',   EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},   // 0x04\r
   {EfiKeyB5,         'b',      'B',   EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},   // 0x05\r
   {EfiKeyB3,         'c',      'C',   EFI_NULL_MODIFIER,   EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},   // 0x06\r
@@ -129,9 +133,11 @@ UINT8 KeyboardLayoutTable[USB_KEYCODE_MAX_MAKE + 8][5] = {
 };\r
 \r
 //\r
-// EFI_KEY to USB Scan Code convertion table\r
+// EFI_KEY to USB Keycode convertion table\r
+// EFI_KEY is defined in UEFI spec.\r
+// USB Keycode is defined in USB HID Firmware spec.\r
 //\r
-UINT8 UsbScanCodeConvertionTable[] = {\r
+UINT8 EfiKeyToUsbKeyCodeConvertionTable[] = {\r
   0xe0,  //  EfiKeyLCtrl\r
   0xe3,  //  EfiKeyA0\r
   0xe2,  //  EfiKeyLAlt\r
@@ -240,9 +246,10 @@ UINT8 UsbScanCodeConvertionTable[] = {
 };\r
 \r
 //\r
-// Keyboard Layout Modifier to EFI Scan Code convertion table\r
+// Keyboard modifier value to EFI Scan Code convertion table\r
+// EFI Scan Code and the modifier values are defined in UEFI spec.\r
 //\r
-UINT8 EfiScanCodeConvertionTable[] = {\r
+UINT8 ModifierValueToEfiScanCodeConvertionTable[] = {\r
   SCAN_NULL,       // EFI_NULL_MODIFIER\r
   SCAN_NULL,       // EFI_LEFT_CONTROL_MODIFIER\r
   SCAN_NULL,       // EFI_RIGHT_CONTROL_MODIFIER\r
@@ -279,22 +286,8 @@ UINT8 EfiScanCodeConvertionTable[] = {
   SCAN_F12,        // EFI_FUNCTION_KEY_TWELVE_MODIFIER\r
 };\r
 \r
-EFI_GUID  mKeyboardLayoutEventGuid = EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID;\r
-\r
-\r
-KB_MODIFIER  KB_Mod[8] = {\r
-  { MOD_CONTROL_L,  0xe0 }, // 11100000\r
-  { MOD_CONTROL_R,  0xe4 }, // 11100100\r
-  { MOD_SHIFT_L,    0xe1 }, // 11100001\r
-  { MOD_SHIFT_R,    0xe5 }, // 11100101\r
-  { MOD_ALT_L,      0xe2 }, // 11100010\r
-  { MOD_ALT_R,      0xe6 }, // 11100110\r
-  { MOD_WIN_L,      0xe3 }, // 11100011\r
-  { MOD_WIN_R,      0xe7 }, // 11100111 \r
-};\r
-\r
 /**\r
-  Initialize KeyConvertionTable by using default keyboard layout.\r
+  Initialize Key Convertion Table by using default keyboard layout.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
 \r
@@ -302,7 +295,7 @@ KB_MODIFIER  KB_Mod[8] = {
 VOID\r
 EFIAPI\r
 LoadDefaultKeyboardLayout (\r
-  IN USB_KB_DEV                 *UsbKeyboardDevice\r
+  IN OUT USB_KB_DEV                 *UsbKeyboardDevice\r
   )\r
 {\r
   UINTN               Index;\r
@@ -313,7 +306,7 @@ LoadDefaultKeyboardLayout (
   //\r
   KeyDescriptor = &UsbKeyboardDevice->KeyConvertionTable[0];\r
 \r
-  for (Index = 0; Index < (USB_KEYCODE_MAX_MAKE + 8); Index++) {\r
+  for (Index = 0; Index < (NUMBER_OF_VALID_USB_KEYCODE); Index++) {\r
     KeyDescriptor->Key                 = (EFI_KEY) KeyboardLayoutTable[Index][0];\r
     KeyDescriptor->Unicode             = KeyboardLayoutTable[Index][1];\r
     KeyDescriptor->ShiftedUnicode      = KeyboardLayoutTable[Index][2];\r
@@ -327,9 +320,12 @@ LoadDefaultKeyboardLayout (
 }\r
 \r
 /**\r
-  Uses USB I/O to check whether the device is a USB Keyboard device.\r
+  Uses USB I/O to check whether the device is a USB keyboard device.\r
+\r
+  @param  UsbIo    Pointer to a USB I/O protocol instance.\r
 \r
-  @param  UsbIo    Points to a USB I/O protocol instance.\r
+  @retval TRUE     Device is a USB keyboard device.\r
+  @retval FALSE    Device is a not USB keyboard device.\r
 \r
 **/\r
 BOOLEAN\r
@@ -342,8 +338,7 @@ IsUSBKeyboard (
   EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;\r
 \r
   //\r
-  // Get the Default interface descriptor, currently we\r
-  // assume it is interface 1\r
+  // Get the default interface descriptor\r
   //\r
   Status = UsbIo->UsbGetInterfaceDescriptor (\r
                     UsbIo,\r
@@ -358,7 +353,6 @@ IsUSBKeyboard (
       InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT &&\r
       InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD\r
       ) {\r
-\r
     return TRUE;\r
   }\r
 \r
@@ -368,7 +362,8 @@ IsUSBKeyboard (
 /**\r
   Get current keyboard layout from HII database.\r
 \r
-  @return Pointer to EFI_HII_KEYBOARD_LAYOUT.\r
+  @return Pointer to HII Keyboard Layout.\r
+          NULL means failure occurred while trying to get keyboard layout.\r
 \r
 **/\r
 EFI_HII_KEYBOARD_LAYOUT *\r
@@ -383,7 +378,7 @@ GetCurrentKeyboardLayout (
   UINT16                    Length;\r
 \r
   //\r
-  // Locate Hii database protocol\r
+  // Locate HII Database Protocol\r
   //\r
   Status = gBS->LocateProtocol (\r
                   &gEfiHiiDatabaseProtocolGuid,\r
@@ -416,7 +411,7 @@ GetCurrentKeyboardLayout (
                             KeyboardLayout\r
                             );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (KeyboardLayout);\r
+      FreePool (KeyboardLayout);\r
       KeyboardLayout = NULL;\r
     }\r
   }\r
@@ -425,12 +420,12 @@ GetCurrentKeyboardLayout (
 }\r
 \r
 /**\r
-  Find Key Descriptor in KeyConvertionTable given its scan code.\r
+  Find Key Descriptor in Key Convertion Table given its USB keycode.\r
 \r
   @param  UsbKeyboardDevice   The USB_KB_DEV instance.\r
-  @param  ScanCode            USB scan code.\r
+  @param  KeyCode             USB Keycode.\r
 \r
-  @return The Key descriptor in KeyConvertionTable.\r
+  @return The Key Descriptor in Key Convertion Table.\r
           NULL means not found.\r
 \r
 **/\r
@@ -438,26 +433,32 @@ EFI_KEY_DESCRIPTOR *
 EFIAPI\r
 GetKeyDescriptor (\r
   IN USB_KB_DEV        *UsbKeyboardDevice,\r
-  IN UINT8             ScanCode\r
+  IN UINT8             KeyCode\r
   )\r
 {\r
   UINT8  Index;\r
 \r
-  if (((ScanCode > 0x65) && (ScanCode < 0xe0)) || (ScanCode > 0xe7)) {\r
+  //\r
+  // Make sure KeyCode is in the range of [0x4, 0x65] or [0xe0, 0xe7]\r
+  //\r
+  if ((!USBKBD_VALID_KEYCODE (KeyCode)) || ((KeyCode > 0x65) && (KeyCode < 0xe0)) || (KeyCode > 0xe7)) {\r
     return NULL;\r
   }\r
 \r
-  if (ScanCode <= 0x65) {\r
-    Index = (UINT8) (ScanCode - 4);\r
+  //\r
+  // Calculate the index of Key Descriptor in Key Convertion Table\r
+  //\r
+  if (KeyCode <= 0x65) {\r
+    Index = (UINT8) (KeyCode - 4);\r
   } else {\r
-    Index = (UINT8) (ScanCode - 0xe0 + USB_KEYCODE_MAX_MAKE);\r
+    Index = (UINT8) (KeyCode - 0xe0 + NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE);\r
   }\r
 \r
   return &UsbKeyboardDevice->KeyConvertionTable[Index];\r
 }\r
 \r
 /**\r
-  Find Non-Spacing key for given KeyDescriptor.\r
+  Find Non-Spacing key for given Key descriptor.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
   @param  KeyDescriptor        Key descriptor.\r
@@ -474,29 +475,37 @@ FindUsbNsKey (
   )\r
 {\r
   LIST_ENTRY      *Link;\r
+  LIST_ENTRY      *NsKeyList;\r
   USB_NS_KEY      *UsbNsKey;\r
-\r
-  Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList);\r
-  while (!IsNull (&UsbKeyboardDevice->NsKeyList, Link)) {\r
+  \r
+  NsKeyList = &UsbKeyboardDevice->NsKeyList;\r
+  Link = GetFirstNode (NsKeyList);\r
+  while (!IsNull (NsKeyList, Link)) {\r
     UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link);\r
 \r
     if (UsbNsKey->NsKey[0].Key == KeyDescriptor->Key) {\r
       return UsbNsKey;\r
     }\r
 \r
-    Link = GetNextNode (&UsbKeyboardDevice->NsKeyList, Link);\r
+    Link = GetNextNode (NsKeyList, Link);\r
   }\r
 \r
   return NULL;\r
 }\r
 \r
 /**\r
-  Find physical key definition for a given Key stroke.\r
+  Find physical key definition for a given key descriptor.\r
+\r
+  For a specified non-spacing key, there are a list of physical\r
+  keys following it. This function traverses the list of\r
+  physical keys and tries to find the physical key matching\r
+  the KeyDescriptor.\r
 \r
-  @param  UsbNsKey          The Non-Spacing key information.\r
-  @param  KeyDescriptor     The key stroke.\r
+  @param  UsbNsKey          The non-spacing key information.\r
+  @param  KeyDescriptor     The key descriptor.\r
 \r
   @return The physical key definition.\r
+          If no physical key is found, parameter KeyDescriptor is returned.\r
 \r
 **/\r
 EFI_KEY_DESCRIPTOR *\r
@@ -525,17 +534,21 @@ FindPhysicalKey (
 }\r
 \r
 /**\r
-  The notification function for SET_KEYBOARD_LAYOUT_EVENT.\r
+  The notification function for EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID.\r
 \r
-  @param  Event           The instance of EFI_EVENT.\r
-  @param  Context         passing parameter.\r
+  This function is registered to event of EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID\r
+  group type, which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout().\r
+  It tries to get curent keyboard layout from HII database.\r
+\r
+  @param  Event        Event being signaled.\r
+  @param  Context      Points to USB_KB_DEV instance.\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 SetKeyboardLayoutEvent (\r
-  EFI_EVENT                  Event,\r
-  VOID                       *Context\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
   )\r
 {\r
   USB_KB_DEV                *UsbKeyboardDevice;\r
@@ -548,12 +561,12 @@ SetKeyboardLayoutEvent (
   UINTN                     Index;\r
   UINTN                     Index2;\r
   UINTN                     KeyCount;\r
-  UINT8                     ScanCode;\r
+  UINT8                     KeyCode;\r
 \r
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
 \r
   //\r
-  // Try to get current Keyboard Layout from HII database\r
+  // Try to get current keyboard layout from HII database\r
   //\r
   KeyboardLayout = GetCurrentKeyboardLayout ();\r
   if (KeyboardLayout == NULL) {\r
@@ -564,7 +577,7 @@ SetKeyboardLayoutEvent (
   // Re-allocate resource for KeyConvertionTable\r
   //\r
   ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
-  UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR));\r
+  UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((NUMBER_OF_VALID_USB_KEYCODE) * sizeof (EFI_KEY_DESCRIPTOR));\r
   ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);\r
 \r
   //\r
@@ -578,16 +591,16 @@ SetKeyboardLayoutEvent (
     CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));\r
 \r
     //\r
-    // Fill the key into KeyConvertionTable, whose index is calculated from USB scan code.\r
+    // Fill the key into KeyConvertionTable, whose index is calculated from USB keycode.\r
     //\r
-    ScanCode = UsbScanCodeConvertionTable [(UINT8) (TempKey.Key)];\r
-    TableEntry = GetKeyDescriptor (UsbKeyboardDevice, ScanCode);\r
+    KeyCode = EfiKeyToUsbKeyCodeConvertionTable [(UINT8) (TempKey.Key)];\r
+    TableEntry = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);\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
-      //\r
-      // For non-spacing key, create the list with a non-spacing key followed by physical keys.\r
-      //\r
       UsbNsKey = AllocatePool (sizeof (USB_NS_KEY));\r
       ASSERT (UsbNsKey != NULL);\r
 \r
@@ -598,7 +611,7 @@ SetKeyboardLayoutEvent (
       NsKey = KeyDescriptor + 1;\r
       for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) {\r
         CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR));\r
-        if (TempKey.Modifier & EFI_NS_KEY_DEPENDENCY_MODIFIER) {\r
+        if (TempKey.Modifier == EFI_NS_KEY_DEPENDENCY_MODIFIER) {\r
           KeyCount++;\r
         } else {\r
           break;\r
@@ -625,17 +638,17 @@ SetKeyboardLayoutEvent (
   }\r
 \r
   //\r
-  // There are two EfiKeyEnter, duplicate its Key Descriptor\r
+  // There are two EfiKeyEnter, duplicate its key descriptor\r
   //\r
   TableEntry = GetKeyDescriptor (UsbKeyboardDevice, 0x58);\r
   KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, 0x28);\r
   CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));\r
 \r
-  gBS->FreePool (KeyboardLayout);\r
+  FreePool (KeyboardLayout);\r
 }\r
 \r
 /**\r
-  Destroy resources for Keyboard layout.\r
+  Destroy resources for keyboard layout.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
 \r
@@ -643,7 +656,7 @@ SetKeyboardLayoutEvent (
 VOID\r
 EFIAPI\r
 ReleaseKeyboardLayoutResources (\r
-  IN USB_KB_DEV              *UsbKeyboardDevice\r
+  IN OUT USB_KB_DEV              *UsbKeyboardDevice\r
   )\r
 {\r
   USB_NS_KEY      *UsbNsKey;\r
@@ -665,24 +678,30 @@ ReleaseKeyboardLayoutResources (
 }\r
 \r
 /**\r
-  Initialize USB Keyboard layout.\r
+  Initialize USB keyboard layout.\r
+\r
+  This function initializes Key Convertion Table for the USB keyboard device.\r
+  It first tries to retrieve layout from HII database. If failed and default\r
+  layout is enabled, then it just uses the default layout.\r
 \r
   @param  UsbKeyboardDevice      The USB_KB_DEV instance.\r
 \r
-  @retval EFI_SUCCESS            Initialization Success.\r
-  @retval Other                  Keyboard layout initial failed.\r
+  @retval EFI_SUCCESS            Initialization succeeded.\r
+  @retval EFI_NOT_READY          Keyboard layout cannot be retrieve from HII\r
+                                 database, and default layout is disabled.\r
+  @retval Other                  Fail to register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 InitKeyboardLayout (\r
-  IN USB_KB_DEV   *UsbKeyboardDevice\r
+  OUT USB_KB_DEV   *UsbKeyboardDevice\r
   )\r
 {\r
   EFI_HII_KEYBOARD_LAYOUT   *KeyboardLayout;\r
   EFI_STATUS                Status;\r
 \r
-  UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR));\r
+  UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((NUMBER_OF_VALID_USB_KEYCODE) * sizeof (EFI_KEY_DESCRIPTOR));\r
   ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);\r
 \r
   InitializeListHead (&UsbKeyboardDevice->NsKeyList);\r
@@ -691,14 +710,14 @@ InitKeyboardLayout (
 \r
   //\r
   // Register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group,\r
-  // which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout.\r
+  // which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout().\r
   //\r
   Status = gBS->CreateEventEx (\r
                   EVT_NOTIFY_SIGNAL,\r
                   TPL_NOTIFY,\r
                   SetKeyboardLayoutEvent,\r
                   UsbKeyboardDevice,\r
-                  &mKeyboardLayoutEventGuid,\r
+                  &gEfiHiiKeyBoardLayoutGuid,\r
                   &UsbKeyboardDevice->KeyboardLayoutEvent\r
                   );\r
   if (EFI_ERROR (Status)) {\r
@@ -732,18 +751,18 @@ InitKeyboardLayout (
 \r
 \r
 /**\r
-  Initialize USB Keyboard device and all private data structures.\r
+  Initialize USB keyboard device and all private data structures.\r
 \r
   @param  UsbKeyboardDevice  The USB_KB_DEV instance.\r
 \r
   @retval EFI_SUCCESS        Initialization is successful.\r
-  @retval EFI_DEVICE_ERROR   Configure hardware failed.\r
+  @retval EFI_DEVICE_ERROR   Keyboard initialization failed.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 InitUSBKeyboard (\r
-  IN USB_KB_DEV   *UsbKeyboardDevice\r
+  IN OUT USB_KB_DEV   *UsbKeyboardDevice\r
   )\r
 {\r
   UINT8               ConfigValue;\r
@@ -762,13 +781,9 @@ InitUSBKeyboard (
   InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
 \r
   //\r
-  // default configurations\r
+  // Uses default configuration to configure the USB keyboard device.\r
   //\r
   ConfigValue = 0x01;\r
-\r
-  //\r
-  // Uses default configuration to configure the USB Keyboard device.\r
-  //\r
   Status = UsbSetConfiguration (\r
              UsbKeyboardDevice->UsbIo,\r
              (UINT16) ConfigValue,\r
@@ -797,7 +812,6 @@ InitUSBKeyboard (
   //\r
   // Set boot protocol for the USB Keyboard.\r
   // This driver only supports boot protocol.\r
-  // The device that does not support boot protocol is not supported.\r
   //\r
   if (Protocol != BOOT_PROTOCOL) {\r
     UsbSetProtocolRequest (\r
@@ -812,7 +826,7 @@ InitUSBKeyboard (
   //\r
   ReportId = 0;\r
   //\r
-  // Duration is zero, which means the duration is indefinite.\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
@@ -824,70 +838,78 @@ InitUSBKeyboard (
     Duration\r
     );\r
 \r
-  UsbKeyboardDevice->CtrlOn     = 0;\r
-  UsbKeyboardDevice->AltOn      = 0;\r
-  UsbKeyboardDevice->ShiftOn    = 0;\r
-  UsbKeyboardDevice->NumLockOn  = 0;\r
-  UsbKeyboardDevice->CapsOn     = 0;\r
-  UsbKeyboardDevice->ScrollOn   = 0;\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
-  UsbKeyboardDevice->LeftCtrlOn   = 0;\r
-  UsbKeyboardDevice->LeftAltOn    = 0;\r
-  UsbKeyboardDevice->LeftShiftOn  = 0;\r
-  UsbKeyboardDevice->LeftLogoOn   = 0;\r
-  UsbKeyboardDevice->RightCtrlOn  = 0;\r
-  UsbKeyboardDevice->RightAltOn   = 0;\r
-  UsbKeyboardDevice->RightShiftOn = 0;\r
-  UsbKeyboardDevice->RightLogoOn  = 0;\r
-  UsbKeyboardDevice->MenuKeyOn    = 0;\r
-  UsbKeyboardDevice->SysReqOn     = 0;\r
-\r
-  UsbKeyboardDevice->AltGrOn      = 0;\r
+  UsbKeyboardDevice->LeftCtrlOn   = FALSE;\r
+  UsbKeyboardDevice->LeftAltOn    = FALSE;\r
+  UsbKeyboardDevice->LeftShiftOn  = FALSE;\r
+  UsbKeyboardDevice->LeftLogoOn   = FALSE;\r
+  UsbKeyboardDevice->RightCtrlOn  = FALSE;\r
+  UsbKeyboardDevice->RightAltOn   = FALSE;\r
+  UsbKeyboardDevice->RightShiftOn = FALSE;\r
+  UsbKeyboardDevice->RightLogoOn  = FALSE;\r
+  UsbKeyboardDevice->MenuKeyOn    = FALSE;\r
+  UsbKeyboardDevice->SysReqOn     = FALSE;\r
+\r
+  UsbKeyboardDevice->AltGrOn      = FALSE;\r
 \r
   UsbKeyboardDevice->CurrentNsKey = NULL;\r
 \r
   //\r
-  // Sync the initial state of lights\r
+  // Sync the initial state of lights on keyboard.\r
   //\r
   SetKeyLED (UsbKeyboardDevice);\r
 \r
   ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);\r
 \r
   //\r
-  // Set a timer for repeat keys' generation.\r
+  // Create event for repeat keys' generation.\r
   //\r
   if (UsbKeyboardDevice->RepeatTimer != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
     UsbKeyboardDevice->RepeatTimer = NULL;\r
   }\r
 \r
-  Status = gBS->CreateEvent (\r
-                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  USBKeyboardRepeatHandler,\r
-                  UsbKeyboardDevice,\r
-                  &UsbKeyboardDevice->RepeatTimer\r
-                  );\r
+  gBS->CreateEvent (\r
+         EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+         TPL_NOTIFY,\r
+         USBKeyboardRepeatHandler,\r
+         UsbKeyboardDevice,\r
+         &UsbKeyboardDevice->RepeatTimer\r
+         );\r
 \r
+  //\r
+  // Create event for delayed recovery, which deals with device error.\r
+  //\r
   if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
     UsbKeyboardDevice->DelayedRecoveryEvent = NULL;\r
   }\r
 \r
-  Status = gBS->CreateEvent (\r
-                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  USBKeyboardRecoveryHandler,\r
-                  UsbKeyboardDevice,\r
-                  &UsbKeyboardDevice->DelayedRecoveryEvent\r
-                  );\r
+  gBS->CreateEvent (\r
+         EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+         TPL_NOTIFY,\r
+         USBKeyboardRecoveryHandler,\r
+         UsbKeyboardDevice,\r
+         &UsbKeyboardDevice->DelayedRecoveryEvent\r
+         );\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 \r
 /**\r
-  Handler function for USB Keyboard's asynchronous interrupt transfer.\r
+  Handler function for USB keyboard's asynchronous interrupt transfer.\r
+\r
+  This function is the handler function for USB keyboard's asynchronous interrupt transfer\r
+  to manage the keyboard. It parses the USB keyboard input report, and inserts data to\r
+  keyboard buffer according to state of modifer keys and normal keys. Timer for repeat key\r
+  is also set accordingly.\r
 \r
   @param  Data             A pointer to a buffer that is filled with key data which is\r
                            retrieved via asynchronous interrupt transfer.\r
@@ -895,8 +917,8 @@ InitUSBKeyboard (
   @param  Context          Pointing to USB_KB_DEV instance.\r
   @param  Result           Indicates the result of the asynchronous interrupt transfer.\r
 \r
-  @retval EFI_SUCCESS      Handler is successful.\r
-  @retval EFI_DEVICE_ERROR Hardware Error\r
+  @retval EFI_SUCCESS      Asynchronous interrupt transfer is handled successfully.\r
+  @retval EFI_DEVICE_ERROR Hardware error occurs.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -914,6 +936,7 @@ KeyboardHandler (
   UINT8               *OldKeyCodeBuffer;\r
   UINT8               CurModifierMap;\r
   UINT8               OldModifierMap;\r
+  UINT8               Mask;\r
   UINT8               Index;\r
   UINT8               Index2;\r
   BOOLEAN             Down;\r
@@ -932,7 +955,7 @@ KeyboardHandler (
   UsbIo             = UsbKeyboardDevice->UsbIo;\r
 \r
   //\r
-  // Analyzes the Result and performs corresponding action.\r
+  // Analyzes Result and performs corresponding action.\r
   //\r
   if (Result != EFI_USB_NOERROR) {\r
     //\r
@@ -945,7 +968,7 @@ KeyboardHandler (
       );\r
 \r
     //\r
-    // stop the repeat key generation if any\r
+    // Stop the repeat key generation if any\r
     //\r
     UsbKeyboardDevice->RepeatKey = 0;\r
 \r
@@ -977,7 +1000,7 @@ KeyboardHandler (
              NULL\r
              );\r
     //\r
-    // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling\r
+    // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.\r
     //\r
     gBS->SetTimer (\r
            UsbKeyboardDevice->DelayedRecoveryEvent,\r
@@ -1029,24 +1052,32 @@ KeyboardHandler (
 \r
   //\r
   // Handle modifier key's pressing or releasing situation.\r
+  // According to USB HID Firmware spec, Byte 0 uses folloing map of Modifier keys:\r
+  // Bit0: Left Control,  Keycode: 0xe0\r
+  // Bit1: Left Shift,    Keycode: 0xe1\r
+  // Bit2: Left Alt,      Keycode: 0xe2\r
+  // Bit3: Left GUI,      Keycode: 0xe3\r
+  // Bit4: Right Control, Keycode: 0xe4\r
+  // Bit5: Right Shift,   Keycode: 0xe5\r
+  // Bit6: Right Alt,     Keycode: 0xe6\r
+  // Bit7: Right GUI,     Keycode: 0xe7\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
-\r
-    if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) {\r
+    Mask = (UINT8) (1 << Index);\r
+    if ((CurModifierMap & Mask) != (OldModifierMap & Mask)) {\r
       //\r
-      // If current modifier key is up, then\r
-      // CurModifierMap & KB_Mod[Index].Mask = 0;\r
+      // If current modifier key is up, then CurModifierMap & Mask = 0;\r
       // otherwise it is a non-zero value.\r
-      // Inserts the pressed modifier key into key buffer.\r
+      // Insert the changed modifier key into key buffer.\r
       //\r
-      Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask);\r
-      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), KB_Mod[Index].Key, Down);\r
+      Down = (BOOLEAN) ((CurModifierMap & Mask) != 0);\r
+      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), (UINT8) (0xe0 + Index), Down);\r
     }\r
   }\r
 \r
   //\r
   // Handle normal key's releasing situation\r
-  // Bytes 2 to 7 are normal keycodes\r
+  // Bytes 2 to 7 are for normal keycodes\r
   //\r
   KeyRelease = FALSE;\r
   for (Index = 2; Index < 8; Index++) {\r
@@ -1075,7 +1106,7 @@ KeyboardHandler (
       InsertKeyCode (\r
         &(UsbKeyboardDevice->KeyboardBuffer),\r
         OldKeyCodeBuffer[Index],\r
-        0\r
+        FALSE\r
         );\r
       //\r
       // The original repeat key is released.\r
@@ -1124,12 +1155,14 @@ KeyboardHandler (
     }\r
 \r
     if (KeyPress) {\r
-      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1);\r
+      InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], TRUE);\r
 \r
       //\r
       // Handle repeat key\r
       //\r
       KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);\r
+      ASSERT (KeyDescriptor != NULL);\r
+\r
       if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) {\r
         //\r
         // For NumLock or CapsLock pressed, there is no need to handle repeat key for them.\r
@@ -1146,16 +1179,14 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // Update LastKeycodeArray[] buffer in the\r
-  // Usb Keyboard Device data structure.\r
+  // Update LastKeycodeArray buffer in the UsbKeyboardDevice data structure.\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
     UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index];\r
   }\r
 \r
   //\r
-  // Pre-process KeyboardBuffer. Pop out the Ctrl, Alt, Del key in sequence\r
-  // and judge whether it will invoke reset event.\r
+  // Pre-process KeyboardBuffer to check if Ctrl + Alt + Del is pressed.\r
   //\r
   SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail;\r
   Index     = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
@@ -1163,32 +1194,33 @@ KeyboardHandler (
     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 != 0) {\r
-        UsbKeyboardDevice->CtrlOn = 1;\r
+      if (UsbKey.Down) {\r
+        UsbKeyboardDevice->CtrlOn = TRUE;\r
       } else {\r
-        UsbKeyboardDevice->CtrlOn = 0;\r
+        UsbKeyboardDevice->CtrlOn = FALSE;\r
       }\r
       break;\r
 \r
     case EFI_LEFT_ALT_MODIFIER:\r
     case EFI_RIGHT_ALT_MODIFIER:\r
-      if (UsbKey.Down != 0) {\r
-        UsbKeyboardDevice->AltOn = 1;\r
+      if (UsbKey.Down) {\r
+        UsbKeyboardDevice->AltOn = TRUE;\r
       } else {\r
-        UsbKeyboardDevice->AltOn = 0;\r
+        UsbKeyboardDevice->AltOn = FALSE;\r
       }\r
       break;\r
 \r
     case EFI_ALT_GR_MODIFIER:\r
-      if (UsbKey.Down != 0) {\r
-        UsbKeyboardDevice->AltGrOn = 1;\r
+      if (UsbKey.Down) {\r
+        UsbKeyboardDevice->AltGrOn = TRUE;\r
       } else {\r
-        UsbKeyboardDevice->AltGrOn = 0;\r
+        UsbKeyboardDevice->AltGrOn = FALSE;\r
       }\r
       break;\r
 \r
@@ -1196,8 +1228,8 @@ KeyboardHandler (
     // For Del Key, check if Ctrl + Alt + Del occurs for reset.\r
     //\r
     case EFI_DELETE_MODIFIER:\r
-      if (UsbKey.Down != 0) {\r
-        if ((UsbKeyboardDevice->CtrlOn != 0) && (UsbKeyboardDevice->AltOn != 0)) {\r
+      if (UsbKey.Down) {\r
+        if ((UsbKeyboardDevice->CtrlOn) && (UsbKeyboardDevice->AltOn)) {\r
           gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
         }\r
       }\r
@@ -1220,7 +1252,7 @@ KeyboardHandler (
 \r
   }\r
   //\r
-  // If have new key pressed, update the RepeatKey value, and set the\r
+  // If there is new key pressed, update the RepeatKey value, and set the\r
   // timer to repeate delay timer\r
   //\r
   if (NewRepeatKey != 0) {\r
@@ -1230,10 +1262,10 @@ KeyboardHandler (
     // enough time.\r
     //\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->RepeatTimer,\r
-          TimerRelative,\r
-          USBKBD_REPEAT_DELAY\r
-          );\r
+           UsbKeyboardDevice->RepeatTimer,\r
+           TimerRelative,\r
+           USBKBD_REPEAT_DELAY\r
+           );\r
     UsbKeyboardDevice->RepeatKey = NewRepeatKey;\r
   }\r
 \r
@@ -1242,26 +1274,29 @@ KeyboardHandler (
 \r
 \r
 /**\r
-  Retrieves a key character after parsing the raw data in keyboard buffer.\r
+  Retrieves a USB keycode after parsing the raw data in keyboard buffer.\r
+\r
+  This function parses keyboard buffer. It updates state of modifier key for\r
+  USB_KB_DEV instancem, and returns keycode for output.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
-  @param  KeyChar              Points to the Key character after key parsing.\r
+  @param  KeyCode              Pointer to the USB keycode for output.\r
 \r
-  @retval EFI_SUCCESS          Parse key is successful.\r
-  @retval EFI_NOT_READY        Device is not ready.\r
+  @retval EFI_SUCCESS          Keycode successfully parsed.\r
+  @retval EFI_NOT_READY        Keyboard buffer is not ready for a valid keycode\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 USBParseKey (\r
   IN OUT  USB_KB_DEV  *UsbKeyboardDevice,\r
-  OUT     UINT8       *KeyChar\r
+     OUT  UINT8       *KeyCode\r
   )\r
 {\r
   USB_KEY             UsbKey;\r
   EFI_KEY_DESCRIPTOR  *KeyDescriptor;\r
 \r
-  *KeyChar = 0;\r
+  *KeyCode = 0;\r
 \r
   while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {\r
     //\r
@@ -1270,67 +1305,69 @@ USBParseKey (
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
-    if (UsbKey.Down == 0) {\r
+    ASSERT (KeyDescriptor != NULL);\r
+\r
+    if (!UsbKey.Down) {\r
       //\r
       // Key is released.\r
       //\r
       switch (KeyDescriptor->Modifier) {\r
 \r
       //\r
-      // CTRL release\r
+      // Ctrl release\r
       //\r
       case EFI_LEFT_CONTROL_MODIFIER:\r
-        UsbKeyboardDevice->LeftCtrlOn = 0;\r
-        UsbKeyboardDevice->CtrlOn = 0;\r
+        UsbKeyboardDevice->LeftCtrlOn = FALSE;\r
+        UsbKeyboardDevice->CtrlOn = FALSE;\r
         break;\r
       case EFI_RIGHT_CONTROL_MODIFIER:\r
-        UsbKeyboardDevice->RightCtrlOn = 0;\r
-        UsbKeyboardDevice->CtrlOn = 0;\r
+        UsbKeyboardDevice->RightCtrlOn = FALSE;\r
+        UsbKeyboardDevice->CtrlOn = FALSE;\r
         break;\r
 \r
       //\r
       // Shift release\r
       //\r
       case EFI_LEFT_SHIFT_MODIFIER:\r
-        UsbKeyboardDevice->LeftShiftOn = 0;\r
-        UsbKeyboardDevice->ShiftOn = 0;\r
+        UsbKeyboardDevice->LeftShiftOn = FALSE;\r
+        UsbKeyboardDevice->ShiftOn = FALSE;\r
         break;\r
       case EFI_RIGHT_SHIFT_MODIFIER:\r
-        UsbKeyboardDevice->RightShiftOn = 0;\r
-        UsbKeyboardDevice->ShiftOn = 0;\r
+        UsbKeyboardDevice->RightShiftOn = FALSE;\r
+        UsbKeyboardDevice->ShiftOn = FALSE;\r
         break;\r
 \r
       //\r
       // Alt release\r
       //\r
       case EFI_LEFT_ALT_MODIFIER:\r
-        UsbKeyboardDevice->LeftAltOn = 0;\r
-        UsbKeyboardDevice->AltOn = 0;\r
+        UsbKeyboardDevice->LeftAltOn = FALSE;\r
+        UsbKeyboardDevice->AltOn = FALSE;\r
         break;\r
       case EFI_RIGHT_ALT_MODIFIER:\r
-        UsbKeyboardDevice->RightAltOn = 0;\r
-        UsbKeyboardDevice->AltOn = 0;\r
+        UsbKeyboardDevice->RightAltOn = FALSE;\r
+        UsbKeyboardDevice->AltOn = FALSE;\r
         break;\r
 \r
       //\r
       // Left Logo release\r
       //\r
       case EFI_LEFT_LOGO_MODIFIER:\r
-        UsbKeyboardDevice->LeftLogoOn = 0;\r
+        UsbKeyboardDevice->LeftLogoOn = FALSE;\r
         break;\r
 \r
       //\r
       // Right Logo release\r
       //\r
       case EFI_RIGHT_LOGO_MODIFIER:\r
-        UsbKeyboardDevice->RightLogoOn = 0;\r
+        UsbKeyboardDevice->RightLogoOn = FALSE;\r
         break;\r
 \r
       //\r
       // Menu key release\r
       //\r
       case EFI_MENU_MODIFIER:\r
-        UsbKeyboardDevice->MenuKeyOn = 0;\r
+        UsbKeyboardDevice->MenuKeyOn = FALSE;\r
         break;\r
 \r
       //\r
@@ -1338,14 +1375,14 @@ USBParseKey (
       //\r
       case EFI_PRINT_MODIFIER:\r
       case EFI_SYS_REQUEST_MODIFIER:\r
-        UsbKeyboardDevice->SysReqOn = 0;\r
+        UsbKeyboardDevice->SysReqOn = FALSE;\r
         break;\r
 \r
       //\r
       // AltGr release\r
       //\r
       case EFI_ALT_GR_MODIFIER:\r
-        UsbKeyboardDevice->AltGrOn = 0;\r
+        UsbKeyboardDevice->AltGrOn = FALSE;\r
         break;\r
 \r
       default:\r
@@ -1361,16 +1398,16 @@ USBParseKey (
     switch (KeyDescriptor->Modifier) {\r
 \r
     //\r
-    // CTRL press\r
+    // Ctrl press\r
     //\r
     case EFI_LEFT_CONTROL_MODIFIER:\r
-      UsbKeyboardDevice->LeftCtrlOn = 1;\r
-      UsbKeyboardDevice->CtrlOn = 1;\r
+      UsbKeyboardDevice->LeftCtrlOn = TRUE;\r
+      UsbKeyboardDevice->CtrlOn = TRUE;\r
       continue;\r
       break;\r
     case EFI_RIGHT_CONTROL_MODIFIER:\r
-      UsbKeyboardDevice->RightCtrlOn = 1;\r
-      UsbKeyboardDevice->CtrlOn = 1;\r
+      UsbKeyboardDevice->RightCtrlOn = TRUE;\r
+      UsbKeyboardDevice->CtrlOn = TRUE;\r
       continue;\r
       break;\r
 \r
@@ -1378,13 +1415,13 @@ USBParseKey (
     // Shift press\r
     //\r
     case EFI_LEFT_SHIFT_MODIFIER:\r
-      UsbKeyboardDevice->LeftShiftOn = 1;\r
-      UsbKeyboardDevice->ShiftOn = 1;\r
+      UsbKeyboardDevice->LeftShiftOn = TRUE;\r
+      UsbKeyboardDevice->ShiftOn = TRUE;\r
       continue;\r
       break;\r
     case EFI_RIGHT_SHIFT_MODIFIER:\r
-      UsbKeyboardDevice->RightShiftOn = 1;\r
-      UsbKeyboardDevice->ShiftOn = 1;\r
+      UsbKeyboardDevice->RightShiftOn = TRUE;\r
+      UsbKeyboardDevice->ShiftOn = TRUE;\r
       continue;\r
       break;\r
 \r
@@ -1392,13 +1429,13 @@ USBParseKey (
     // Alt press\r
     //\r
     case EFI_LEFT_ALT_MODIFIER:\r
-      UsbKeyboardDevice->LeftAltOn = 1;\r
-      UsbKeyboardDevice->AltOn = 1;\r
+      UsbKeyboardDevice->LeftAltOn = TRUE;\r
+      UsbKeyboardDevice->AltOn = TRUE;\r
       continue;\r
       break;\r
     case EFI_RIGHT_ALT_MODIFIER:\r
-      UsbKeyboardDevice->RightAltOn = 1;\r
-      UsbKeyboardDevice->AltOn = 1;\r
+      UsbKeyboardDevice->RightAltOn = TRUE;\r
+      UsbKeyboardDevice->AltOn = TRUE;\r
       continue;\r
       break;\r
 \r
@@ -1406,21 +1443,21 @@ USBParseKey (
     // Left Logo press\r
     //\r
     case EFI_LEFT_LOGO_MODIFIER:\r
-      UsbKeyboardDevice->LeftLogoOn = 1;\r
+      UsbKeyboardDevice->LeftLogoOn = TRUE;\r
       break;\r
 \r
     //\r
     // Right Logo press\r
     //\r
     case EFI_RIGHT_LOGO_MODIFIER:\r
-      UsbKeyboardDevice->RightLogoOn = 1;\r
+      UsbKeyboardDevice->RightLogoOn = TRUE;\r
       break;\r
 \r
     //\r
     // Menu key press\r
     //\r
     case EFI_MENU_MODIFIER:\r
-      UsbKeyboardDevice->MenuKeyOn = 1;\r
+      UsbKeyboardDevice->MenuKeyOn = TRUE;\r
       break;\r
 \r
     //\r
@@ -1428,7 +1465,7 @@ USBParseKey (
     //\r
     case EFI_PRINT_MODIFIER:\r
     case EFI_SYS_REQUEST_MODIFIER:\r
-      UsbKeyboardDevice->SysReqOn = 1;\r
+      UsbKeyboardDevice->SysReqOn = TRUE;\r
       continue;\r
       break;\r
 \r
@@ -1436,32 +1473,32 @@ USBParseKey (
     // AltGr press\r
     //\r
     case EFI_ALT_GR_MODIFIER:\r
-      UsbKeyboardDevice->AltGrOn = 1;\r
+      UsbKeyboardDevice->AltGrOn = TRUE;\r
       break;\r
 \r
     case EFI_NUM_LOCK_MODIFIER:\r
-      UsbKeyboardDevice->NumLockOn ^= 1;\r
       //\r
-      // Set the NumLock light on keyboard\r
+      // Toggle NumLock\r
       //\r
+      UsbKeyboardDevice->NumLockOn = (BOOLEAN) (!(UsbKeyboardDevice->NumLockOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
       break;\r
 \r
     case EFI_CAPS_LOCK_MODIFIER:\r
-      UsbKeyboardDevice->CapsOn ^= 1;\r
       //\r
-      // Set the CapsLock light on keyboard\r
+      // Toggle CapsLock\r
       //\r
+      UsbKeyboardDevice->CapsOn = (BOOLEAN) (!(UsbKeyboardDevice->CapsOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
       break;\r
 \r
     case EFI_SCROLL_LOCK_MODIFIER:\r
-      UsbKeyboardDevice->ScrollOn ^= 1;\r
       //\r
-      // Set the ScrollLock light on keyboard\r
+      // Toggle ScrollLock\r
       //\r
+      UsbKeyboardDevice->ScrollOn = (BOOLEAN) (!(UsbKeyboardDevice->ScrollOn));\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
       break;\r
@@ -1488,12 +1525,12 @@ USBParseKey (
     // When encountering Ctrl + Alt + Del, then warm reset.\r
     //\r
     if (KeyDescriptor->Modifier == EFI_DELETE_MODIFIER) {\r
-      if ((UsbKeyboardDevice->CtrlOn != 0) && (UsbKeyboardDevice->AltOn != 0)) {\r
+      if ((UsbKeyboardDevice->CtrlOn) && (UsbKeyboardDevice->AltOn)) {\r
         gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
       }\r
     }\r
 \r
-    *KeyChar = UsbKey.KeyCode;\r
+    *KeyCode = UsbKey.KeyCode;\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -1502,43 +1539,43 @@ USBParseKey (
 \r
 \r
 /**\r
-  Converts USB Keyboard code to EFI_INPUT_KEY.\r
+  Converts USB Keycode ranging from 0x4 to 0x65 to EFI_INPUT_KEY.\r
 \r
-  @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
-  @param  KeyChar              Indicates the key code that will be interpreted.\r
-  @param  Key                  A pointer to a buffer that is filled in with\r
-                               the keystroke information for the key that\r
-                               was pressed.\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
+                                the keystroke information for the key that\r
+                                was pressed.\r
 \r
-  @retval EFI_NOT_READY        Device is not ready\r
-  @retval EFI_SUCCESS          Success.\r
+  @retval EFI_SUCCESS           Success.\r
+  @retval EFI_INVALID_PARAMETER KeyCode is not in the range of 0x4 to 0x65.\r
+  @retval EFI_INVALID_PARAMETER Translated EFI_INPUT_KEY has zero for both ScanCode and UnicodeChar.\r
+  @retval EFI_NOT_READY         KeyCode represents a dead key with EFI_NS_KEY_MODIFIER\r
+  @retval EFI_DEVICE_ERROR      Keyboard layout is invalid.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 UsbKeyCodeToEfiInputKey (\r
   IN  USB_KB_DEV      *UsbKeyboardDevice,\r
-  IN  UINT8           KeyChar,\r
+  IN  UINT8           KeyCode,\r
   OUT EFI_INPUT_KEY   *Key\r
   )\r
 {\r
-  UINT8               Index;\r
   EFI_KEY_DESCRIPTOR  *KeyDescriptor;\r
 \r
-  if (!USBKBD_VALID_KEYCODE (KeyChar)) {\r
-    return EFI_NOT_READY;\r
-  }\r
-\r
   //\r
-  // Valid USB Key Code starts from 4, so it's safe to minus 4.\r
+  // KeyCode must in the range of 0x4 to 0x65\r
   //\r
-  Index = (UINT8) (KeyChar - 4);\r
-\r
-  if (Index >= USB_KEYCODE_MAX_MAKE) {\r
-    return EFI_NOT_READY;\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, KeyChar);\r
+  KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);\r
+  ASSERT (KeyDescriptor != NULL);\r
 \r
   if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {\r
     //\r
@@ -1557,11 +1594,18 @@ UsbKeyCodeToEfiInputKey (
     UsbKeyboardDevice->CurrentNsKey = NULL;\r
   }\r
 \r
-  Key->ScanCode = EfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
+  //\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
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  Key->ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
   Key->UnicodeChar = KeyDescriptor->Unicode;\r
 \r
   if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) {\r
-    if (UsbKeyboardDevice->ShiftOn != 0) {\r
+    if (UsbKeyboardDevice->ShiftOn) {\r
       Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
 \r
       //\r
@@ -1569,11 +1613,11 @@ UsbKeyCodeToEfiInputKey (
       // 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
-        UsbKeyboardDevice->LeftShiftOn = 0;\r
-        UsbKeyboardDevice->RightShiftOn = 0;\r
+        UsbKeyboardDevice->LeftShiftOn = FALSE;\r
+        UsbKeyboardDevice->RightShiftOn = FALSE;\r
       }\r
 \r
-      if (UsbKeyboardDevice->AltGrOn != 0) {\r
+      if (UsbKeyboardDevice->AltGrOn) {\r
         Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode;\r
       }\r
     } else {\r
@@ -1582,23 +1626,18 @@ UsbKeyCodeToEfiInputKey (
       //\r
       Key->UnicodeChar = KeyDescriptor->Unicode;\r
 \r
-      if (UsbKeyboardDevice->AltGrOn != 0) {\r
+      if (UsbKeyboardDevice->AltGrOn) {\r
         Key->UnicodeChar = KeyDescriptor->AltGrUnicode;\r
       }\r
     }\r
   }\r
 \r
   if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
-    if (UsbKeyboardDevice->CapsOn != 0) {\r
-\r
+    if (UsbKeyboardDevice->CapsOn) {\r
       if (Key->UnicodeChar == KeyDescriptor->Unicode) {\r
-\r
         Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
-\r
       } else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) {\r
-\r
         Key->UnicodeChar = KeyDescriptor->Unicode;\r
-\r
       }\r
     }\r
   }\r
@@ -1607,7 +1646,7 @@ UsbKeyCodeToEfiInputKey (
   // Translate the CTRL-Alpha characters to their corresponding control value\r
   // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
   //\r
-  if (UsbKeyboardDevice->CtrlOn != 0) {\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
@@ -1615,13 +1654,13 @@ UsbKeyCodeToEfiInputKey (
     }\r
   }\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) {\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
     // normal key, instead of original control key. So the ScanCode should be cleaned.\r
     // Otherwise, it means control key, so preserve the EFI Scan Code and clear the unicode keycode.\r
     //\r
-    if ((UsbKeyboardDevice->NumLockOn != 0) && (UsbKeyboardDevice->ShiftOn == 0)) {\r
+    if ((UsbKeyboardDevice->NumLockOn) && (!(UsbKeyboardDevice->ShiftOn))) {\r
       Key->ScanCode = SCAN_NULL;\r
     } else {\r
       Key->UnicodeChar = 0x00;\r
@@ -1647,44 +1686,44 @@ UsbKeyCodeToEfiInputKey (
   //\r
   // Save Shift/Toggle state\r
   //\r
-  if (UsbKeyboardDevice->LeftCtrlOn == 1) {\r
+  if (UsbKeyboardDevice->LeftCtrlOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->RightCtrlOn == 1) {\r
+  if (UsbKeyboardDevice->RightCtrlOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->LeftAltOn == 1) {\r
+  if (UsbKeyboardDevice->LeftAltOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->RightAltOn == 1) {\r
+  if (UsbKeyboardDevice->RightAltOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->LeftShiftOn == 1) {\r
+  if (UsbKeyboardDevice->LeftShiftOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->RightShiftOn == 1) {\r
+  if (UsbKeyboardDevice->RightShiftOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->LeftLogoOn == 1) {\r
+  if (UsbKeyboardDevice->LeftLogoOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->RightLogoOn == 1) {\r
+  if (UsbKeyboardDevice->RightLogoOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->MenuKeyOn == 1) {\r
+  if (UsbKeyboardDevice->MenuKeyOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED;\r
   }\r
-  if (UsbKeyboardDevice->SysReqOn == 1) {\r
+  if (UsbKeyboardDevice->SysReqOn) {\r
     UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED;\r
   }\r
 \r
-  if (UsbKeyboardDevice->ScrollOn == 1) {\r
+  if (UsbKeyboardDevice->ScrollOn) {\r
     UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;\r
   }\r
-  if (UsbKeyboardDevice->NumLockOn == 1) {\r
+  if (UsbKeyboardDevice->NumLockOn) {\r
     UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;\r
   }\r
-  if (UsbKeyboardDevice->CapsOn == 1) {\r
+  if (UsbKeyboardDevice->CapsOn) {\r
     UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;\r
   }\r
 \r
@@ -1694,15 +1733,15 @@ UsbKeyCodeToEfiInputKey (
 \r
 \r
 /**\r
-  Resets USB Keyboard Buffer.\r
+  Resets USB keyboard buffer.\r
 \r
-  @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
+  @param  KeyboardBuffer     Points to the USB keyboard buffer.\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 InitUSBKeyBuffer (\r
-  IN OUT  USB_KB_BUFFER   *KeyboardBuffer\r
+  OUT  USB_KB_BUFFER   *KeyboardBuffer\r
   )\r
 {\r
   ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));\r
@@ -1712,12 +1751,12 @@ InitUSBKeyBuffer (
 \r
 \r
 /**\r
-  Check whether USB Keyboard buffer is empty.\r
+  Check whether USB keyboard buffer is empty.\r
 \r
-  @param  KeyboardBuffer     USB Keyboard Buffer.\r
+  @param  KeyboardBuffer     USB keyboard buffer\r
 \r
-  @retval TRUE               Key buffer is empty.\r
-  @retval FALSE              Key buffer is not empty.\r
+  @retval TRUE               Keyboard buffer is empty.\r
+  @retval FALSE              Keyboard buffer is not empty.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1727,19 +1766,19 @@ IsUSBKeyboardBufferEmpty (
   )\r
 {\r
   //\r
-  // meet FIFO empty condition\r
+  // Meet FIFO empty condition\r
   //\r
   return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail);\r
 }\r
 \r
 \r
 /**\r
-  Check whether USB Keyboard buffer is full.\r
+  Check whether USB keyboard buffer is full.\r
 \r
-  @param  KeyboardBuffer     USB Keyboard Buffer.\r
+  @param  KeyboardBuffer     USB keyboard buffer\r
 \r
-  @retval TRUE               Key buffer is full.\r
-  @retval FALSE              Key buffer is not full.\r
+  @retval TRUE               Keyboard buffer is full.\r
+  @retval FALSE              Keyboard buffer is not full.\r
 \r
 **/\r
 BOOLEAN\r
@@ -1753,11 +1792,12 @@ IsUSBKeyboardBufferFull (
 \r
 \r
 /**\r
-  Inserts a key code into keyboard buffer.\r
+  Inserts a keycode into keyboard buffer.\r
 \r
-  @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
-  @param  Key                Key code\r
-  @param  Down               Special key\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
 \r
 **/\r
 VOID\r
@@ -1765,19 +1805,21 @@ EFIAPI
 InsertKeyCode (\r
   IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
   IN      UINT8         Key,\r
-  IN      UINT8         Down\r
+  IN      BOOLEAN       Down\r
   )\r
 {\r
   USB_KEY UsbKey;\r
 \r
   //\r
-  // if keyboard buffer is full, throw the\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
   }\r
 \r
+  ASSERT (KeyboardBuffer->BufferTail <= MAX_KEY_ALLOWED);\r
+\r
   KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;\r
   KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down    = Down;\r
 \r
@@ -1789,12 +1831,12 @@ InsertKeyCode (
 \r
 \r
 /**\r
-  Pops a key code off from keyboard buffer.\r
+  Remove a keycode from keyboard buffer and return it.\r
 \r
-  @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
-  @param  UsbKey             Points to the buffer that contains a usb key code.\r
+  @param  KeyboardBuffer     Points to the USB keyboard buffer.\r
+  @param  UsbKey             Points to the buffer that contains keycode for output.\r
 \r
-  @retval EFI_SUCCESS        Key code Successfully poped from keyboard buffer.\r
+  @retval EFI_SUCCESS        Keycode successfully removed from keyboard buffer.\r
   @retval EFI_DEVICE_ERROR   Keyboard buffer is empty.\r
 \r
 **/\r
@@ -1802,13 +1844,15 @@ EFI_STATUS
 EFIAPI\r
 RemoveKeyCode (\r
   IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
-  OUT     USB_KEY       *UsbKey\r
+     OUT  USB_KEY       *UsbKey\r
   )\r
 {\r
   if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {\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
 \r
@@ -1822,7 +1866,7 @@ RemoveKeyCode (
 \r
 \r
 /**\r
-  Sets USB Keyboard LED state.\r
+  Sets USB keyboard LED state.\r
 \r
   @param  UsbKeyboardDevice  The USB_KB_DEV instance.\r
 \r
@@ -1839,9 +1883,9 @@ SetKeyLED (
   //\r
   // Set each field in Led map.\r
   //\r
-  Led.NumLock    = (UINT8) UsbKeyboardDevice->NumLockOn;\r
-  Led.CapsLock   = (UINT8) UsbKeyboardDevice->CapsOn;\r
-  Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn;\r
+  Led.NumLock    = (UINT8) ((UsbKeyboardDevice->NumLockOn) ? 1 : 0);\r
+  Led.CapsLock   = (UINT8) ((UsbKeyboardDevice->CapsOn) ? 1 : 0);\r
+  Led.ScrollLock = (UINT8) ((UsbKeyboardDevice->ScrollOn) ? 1 : 0);\r
   Led.Resrvd     = 0;\r
 \r
   ReportId       = 0;\r
@@ -1860,7 +1904,13 @@ SetKeyLED (
 \r
 \r
 /**\r
-  Timer handler for Repeat Key timer.\r
+  Handler for Repeat Key event.\r
+\r
+  This function is the handler for Repeat Key event triggered\r
+  by timer.\r
+  After a repeatable key is pressed, the event would be triggered\r
+  with interval of USBKBD_REPEAT_DELAY. Once the event is triggered,\r
+  following trigger will come with interval of USBKBD_REPEAT_RATE.\r
 \r
   @param  Event              The Repeat Key event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r
@@ -1882,16 +1932,16 @@ USBKeyboardRepeatHandler (
   //\r
   if (UsbKeyboardDevice->RepeatKey != 0) {\r
     //\r
-    // Inserts one Repeat key into keyboard buffer,\r
+    // Inserts the repeat key into keyboard buffer,\r
     //\r
     InsertKeyCode (\r
       &(UsbKeyboardDevice->KeyboardBuffer),\r
       UsbKeyboardDevice->RepeatKey,\r
-      1\r
+      TRUE\r
       );\r
 \r
     //\r
-    // Set repeate rate for next repeat key generation.\r
+    // Set repeat rate for next repeat key generation.\r
     //\r
     gBS->SetTimer (\r
            UsbKeyboardDevice->RepeatTimer,\r
@@ -1903,7 +1953,13 @@ USBKeyboardRepeatHandler (
 \r
 \r
 /**\r
-  Timer handler for Delayed Recovery timer.\r
+  Handler for Delayed Recovery event.\r
+\r
+  This function is the handler for Delayed Recovery event triggered\r
+  by timer.\r
+  After a device error occurs, the event would be triggered\r
+  with interval of EFI_USB_INTERRUPT_DELAY. EFI_USB_INTERRUPT_DELAY\r
+  is defined in USB standard for error handling.\r
 \r
   @param  Event              The Delayed Recovery event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r