]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbKbDxe/KeyBoard.c
Code Scrub for UsbKbDxe module.
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / KeyBoard.c
index 9af221daaddeaf0a98e0e7b7635953603b95baec..0d7d3d1bad2d6ccc7e189ffcf1ebdfce73d0a18f 100644 (file)
@@ -1,5 +1,4 @@
 /** @file\r
-\r
   Helper functions for USB Keyboard Driver.\r
 \r
 Copyright (c) 2004 - 2008, Intel Corporation\r
@@ -14,7 +13,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "KeyBoard.h"\r
-#include <Library/UefiUsbLib.h>\r
 \r
 //\r
 // Static English keyboard layout\r
@@ -295,12 +293,10 @@ KB_MODIFIER  KB_Mod[8] = {
   { MOD_WIN_R,      0xe7 }, // 11100111 \r
 };\r
 \r
-\r
 /**\r
   Initialize KeyConvertionTable by using default keyboard layout.\r
 \r
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
-  @retval None.\r
 \r
 **/\r
 VOID\r
@@ -330,12 +326,10 @@ LoadDefaultKeyboardLayout (
   }\r
 }\r
 \r
-\r
 /**\r
   Uses USB I/O to check whether the device is a USB Keyboard device.\r
 \r
   @param  UsbIo    Points to a USB I/O protocol instance.\r
-  @retval None\r
 \r
 **/\r
 BOOLEAN\r
@@ -374,7 +368,7 @@ IsUSBKeyboard (
 /**\r
   Get current keyboard layout from HII database.\r
 \r
-  @retval Pointer to EFI_HII_KEYBOARD_LAYOUT.\r
+  @return Pointer to EFI_HII_KEYBOARD_LAYOUT.\r
 \r
 **/\r
 EFI_HII_KEYBOARD_LAYOUT *\r
@@ -437,6 +431,7 @@ GetCurrentKeyboardLayout (
   @param  ScanCode            USB scan code.\r
 \r
   @return The Key descriptor in KeyConvertionTable.\r
+          NULL means not found.\r
 \r
 **/\r
 EFI_KEY_DESCRIPTOR *\r
@@ -467,8 +462,8 @@ GetKeyDescriptor (
   @param  UsbKeyboardDevice    The USB_KB_DEV instance.\r
   @param  KeyDescriptor        Key descriptor.\r
 \r
-  @retval NULL                 Key list is empty.\r
-  @return Other                The Non-Spacing key.\r
+  @return The Non-Spacing key corresponding to KeyDescriptor\r
+          NULL means not found.\r
 \r
 **/\r
 USB_NS_KEY *\r
@@ -566,12 +561,15 @@ SetKeyboardLayoutEvent (
   }\r
 \r
   //\r
-  // Allocate resource for KeyConvertionTable\r
+  // Re-allocate resource for KeyConvertionTable\r
   //\r
   ReleaseKeyboardLayoutResources (UsbKeyboardDevice);\r
   UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((USB_KEYCODE_MAX_MAKE + 8) * sizeof (EFI_KEY_DESCRIPTOR));\r
   ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);\r
 \r
+  //\r
+  // Traverse the list of key descriptors following the header of EFI_HII_KEYBOARD_LAYOUT\r
+  //\r
   KeyDescriptor = (EFI_KEY_DESCRIPTOR *) (((UINT8 *) KeyboardLayout) + sizeof (EFI_HII_KEYBOARD_LAYOUT));\r
   for (Index = 0; Index < KeyboardLayout->DescriptorCount; Index++) {\r
     //\r
@@ -580,7 +578,7 @@ SetKeyboardLayoutEvent (
     CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));\r
 \r
     //\r
-    // Fill the key into KeyConvertionTable (which use USB Scan Code as index)\r
+    // Fill the key into KeyConvertionTable, whose index is calculated from USB scan code.\r
     //\r
     ScanCode = UsbScanCodeConvertionTable [(UINT8) (TempKey.Key)];\r
     TableEntry = GetKeyDescriptor (UsbKeyboardDevice, ScanCode);\r
@@ -588,7 +586,7 @@ SetKeyboardLayoutEvent (
 \r
     if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) {\r
       //\r
-      // Non-spacing key\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
@@ -692,7 +690,8 @@ InitKeyboardLayout (
   UsbKeyboardDevice->KeyboardLayoutEvent = NULL;\r
 \r
   //\r
-  // Register SET_KEYBOARD_LAYOUT_EVENT notification\r
+  // Register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group,\r
+  // which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout.\r
   //\r
   Status = gBS->CreateEventEx (\r
                   EVT_NOTIFY_SIGNAL,\r
@@ -706,26 +705,26 @@ InitKeyboardLayout (
     return Status;\r
   }\r
 \r
-  //\r
-  // Try to get current keyboard layout from HII database\r
-  //\r
   KeyboardLayout = GetCurrentKeyboardLayout ();\r
   if (KeyboardLayout != NULL) {\r
     //\r
-    // Force to initialize the keyboard layout\r
+    // If current keyboard layout is successfully retrieved from HII database,\r
+    // force to initialize the keyboard layout.\r
     //\r
     gBS->SignalEvent (UsbKeyboardDevice->KeyboardLayoutEvent);\r
   } else {\r
     if (FeaturePcdGet (PcdDisableDefaultKeyboardLayoutInUsbKbDriver)) {\r
-      return EFI_NOT_READY;\r
-    } else {\r
-\r
       //\r
-      // Fail to get keyboard layout from HII database,\r
-      // use default keyboard layout\r
+      // If no keyboard layout can be retrieved from HII database, and default layout\r
+      // is disabled, then return EFI_NOT_READY.\r
       //\r
-      LoadDefaultKeyboardLayout (UsbKeyboardDevice);\r
+      return EFI_NOT_READY;\r
     }\r
+    //\r
+    // If no keyboard layout can be retrieved from HII database, and default layout\r
+    // is enabled, then load the default keyboard layout.\r
+    //\r
+    LoadDefaultKeyboardLayout (UsbKeyboardDevice);\r
   }\r
   \r
   return EFI_SUCCESS;\r
@@ -754,10 +753,10 @@ InitUSBKeyboard (
   EFI_STATUS          Status;\r
   UINT32              TransferResult;\r
 \r
-  KbdReportStatusCode (\r
-    UsbKeyboardDevice->DevicePath,\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
-    PcdGet32 (PcdStatusCodeValueKeyboardSelfTest)\r
+    PcdGet32 (PcdStatusCodeValueKeyboardSelfTest),\r
+    UsbKeyboardDevice->DevicePath\r
     );\r
 \r
   InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));\r
@@ -771,20 +770,20 @@ InitUSBKeyboard (
   // Uses default configuration to configure the USB Keyboard device.\r
   //\r
   Status = UsbSetConfiguration (\r
-            UsbKeyboardDevice->UsbIo,\r
-            (UINT16) ConfigValue,\r
-            &TransferResult\r
-            );\r
+             UsbKeyboardDevice->UsbIo,\r
+             (UINT16) 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
-    KbdReportStatusCode (\r
-      UsbKeyboardDevice->DevicePath,\r
+    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError)\r
+      PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError),\r
+      UsbKeyboardDevice->DevicePath\r
       );\r
 \r
     return EFI_DEVICE_ERROR;\r
@@ -796,9 +795,9 @@ InitUSBKeyboard (
     &Protocol\r
     );\r
   //\r
-  // Sets boot protocol for the USB Keyboard.\r
+  // Set boot protocol for the USB Keyboard.\r
   // This driver only supports boot protocol.\r
-  // !!BugBug: How about the device that does not support boot protocol?\r
+  // The device that does not support boot protocol is not supported.\r
   //\r
   if (Protocol != BOOT_PROTOCOL) {\r
     UsbSetProtocolRequest (\r
@@ -807,17 +806,15 @@ InitUSBKeyboard (
       BOOT_PROTOCOL\r
       );\r
   }\r
-  //\r
-  // the duration is indefinite, so the endpoint will inhibit reporting forever,\r
-  // and only reporting when a change is detected in the report data.\r
-  //\r
 \r
   //\r
-  // idle value for all report ID\r
+  // ReportId is zero, which means the idle rate applies to all input reports.\r
   //\r
   ReportId = 0;\r
   //\r
-  // idle forever until there is a key pressed and released.\r
+  // Duration is zero, which means the duration is indefinite.\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
@@ -861,7 +858,7 @@ InitUSBKeyboard (
   //\r
   if (UsbKeyboardDevice->RepeatTimer != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);\r
-    UsbKeyboardDevice->RepeatTimer = 0;\r
+    UsbKeyboardDevice->RepeatTimer = NULL;\r
   }\r
 \r
   Status = gBS->CreateEvent (\r
@@ -874,7 +871,7 @@ InitUSBKeyboard (
 \r
   if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) {\r
     gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);\r
-    UsbKeyboardDevice->DelayedRecoveryEvent = 0;\r
+    UsbKeyboardDevice->DelayedRecoveryEvent = NULL;\r
   }\r
 \r
   Status = gBS->CreateEvent (\r
@@ -928,7 +925,7 @@ KeyboardHandler (
   UINT32              UsbStatus;\r
   EFI_KEY_DESCRIPTOR  *KeyDescriptor;\r
 \r
-  ASSERT (Context);\r
+  ASSERT (Context != NULL);\r
 \r
   NewRepeatKey      = 0;\r
   UsbKeyboardDevice = (USB_KB_DEV *) Context;\r
@@ -941,10 +938,10 @@ KeyboardHandler (
     //\r
     // Some errors happen during the process\r
     //\r
-    KbdReportStatusCode (\r
-      UsbKeyboardDevice->DevicePath,\r
+    REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
       EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
-      PcdGet32 (PcdStatusCodeValueKeyboardInputError)\r
+      PcdGet32 (PcdStatusCodeValueKeyboardInputError),\r
+      UsbKeyboardDevice->DevicePath\r
       );\r
 \r
     //\r
@@ -968,37 +965,48 @@ KeyboardHandler (
 \r
     //\r
     // Delete & Submit this interrupt again\r
+    // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. \r
     //\r
-\r
     UsbIo->UsbAsyncInterruptTransfer (\r
-                      UsbIo,\r
-                      UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
-                      FALSE,\r
-                      0,\r
-                      0,\r
-                      NULL,\r
-                      NULL\r
-                      );\r
-\r
+             UsbIo,\r
+             UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
+             FALSE,\r
+             0,\r
+             0,\r
+             NULL,\r
+             NULL\r
+             );\r
+    //\r
+    // EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling\r
+    //\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->DelayedRecoveryEvent,\r
-          TimerRelative,\r
-          EFI_USB_INTERRUPT_DELAY\r
-          );\r
+           UsbKeyboardDevice->DelayedRecoveryEvent,\r
+           TimerRelative,\r
+           EFI_USB_INTERRUPT_DELAY\r
+           );\r
 \r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  //\r
+  // If no error and no data, just return EFI_SUCCESS.\r
+  //\r
   if (DataLength == 0 || Data == NULL) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
+  //\r
+  // Following code checks current keyboard input report against old key code buffer.\r
+  // According to USB HID Firmware Specification, the report consists of 8 bytes.\r
+  // Byte 0 is map of Modifier keys.\r
+  // Byte 1 is reserved.\r
+  // Bytes 2 to 7 are keycodes.\r
+  //\r
   CurKeyCodeBuffer  = (UINT8 *) Data;\r
   OldKeyCodeBuffer  = UsbKeyboardDevice->LastKeyCodeArray;\r
 \r
   //\r
-  // checks for new key stroke.\r
-  // if no new key got, return immediately.\r
+  // Checks for new key stroke.\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
     if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) {\r
@@ -1006,26 +1014,29 @@ KeyboardHandler (
     }\r
   }\r
 \r
+  //\r
+  // If no new key, return EFI_SUCCESS immediately.\r
+  //\r
   if (Index == 8) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
   //\r
-  // Parse the modifier key\r
+  // Parse the modifier key, which is the first byte of keyboard input report.\r
   //\r
   CurModifierMap  = CurKeyCodeBuffer[0];\r
   OldModifierMap  = OldKeyCodeBuffer[0];\r
 \r
   //\r
-  // handle modifier key's pressing or releasing situation.\r
+  // Handle modifier key's pressing or releasing situation.\r
   //\r
   for (Index = 0; Index < 8; Index++) {\r
 \r
     if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) {\r
       //\r
-      // if current modifier key is up, then\r
+      // If current modifier key is up, then\r
       // CurModifierMap & KB_Mod[Index].Mask = 0;\r
-      // otherwize it is a non-zero value.\r
+      // otherwise it is a non-zero value.\r
       // Inserts the pressed modifier key into key buffer.\r
       //\r
       Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask);\r
@@ -1034,7 +1045,8 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // handle normal key's releasing situation\r
+  // Handle normal key's releasing situation\r
+  // Bytes 2 to 7 are normal keycodes\r
   //\r
   KeyRelease = FALSE;\r
   for (Index = 2; Index < 8; Index++) {\r
@@ -1042,7 +1054,10 @@ KeyboardHandler (
     if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) {\r
       continue;\r
     }\r
-\r
+    //\r
+    // For any key in old keycode buffer, if it is not in current keycode buffer,\r
+    // then it is released. Otherwise, it is not released.\r
+    //\r
     KeyRelease = TRUE;\r
     for (Index2 = 2; Index2 < 8; Index2++) {\r
 \r
@@ -1063,7 +1078,7 @@ KeyboardHandler (
         0\r
         );\r
       //\r
-      // the original reapeat key is released.\r
+      // The original repeat key is released.\r
       //\r
       if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) {\r
         UsbKeyboardDevice->RepeatKey = 0;\r
@@ -1072,18 +1087,18 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // original repeat key is released, cancel the repeat timer\r
+  // If original repeat key is released, cancel the repeat timer\r
   //\r
   if (UsbKeyboardDevice->RepeatKey == 0) {\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->RepeatTimer,\r
-          TimerCancel,\r
-          USBKBD_REPEAT_RATE\r
-          );\r
+           UsbKeyboardDevice->RepeatTimer,\r
+           TimerCancel,\r
+           USBKBD_REPEAT_RATE\r
+           );\r
   }\r
 \r
   //\r
-  // handle normal key's pressing situation\r
+  // Handle normal key's pressing situation\r
   //\r
   KeyPress = FALSE;\r
   for (Index = 2; Index < 8; Index++) {\r
@@ -1091,7 +1106,10 @@ KeyboardHandler (
     if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) {\r
       continue;\r
     }\r
-\r
+    //\r
+    // For any key in current keycode buffer, if it is not in old keycode buffer,\r
+    // then it is pressed. Otherwise, it is not pressed.\r
+    //\r
     KeyPress = TRUE;\r
     for (Index2 = 2; Index2 < 8; Index2++) {\r
 \r
@@ -1107,17 +1125,21 @@ KeyboardHandler (
 \r
     if (KeyPress) {\r
       InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1);\r
+\r
       //\r
-      // NumLock pressed or CapsLock pressed\r
+      // Handle repeat key\r
       //\r
       KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);\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
+        //\r
         UsbKeyboardDevice->RepeatKey = 0;\r
       } else {\r
-        NewRepeatKey = CurKeyCodeBuffer[Index];\r
         //\r
-        // do not repeat the original repeated key\r
+        // Prepare new repeat key, and clear the original one.\r
         //\r
+        NewRepeatKey = CurKeyCodeBuffer[Index];\r
         UsbKeyboardDevice->RepeatKey = 0;\r
       }\r
     }\r
@@ -1132,11 +1154,11 @@ KeyboardHandler (
   }\r
 \r
   //\r
-  // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence\r
+  // Pre-process KeyboardBuffer. Pop out the Ctrl, Alt, Del key in sequence\r
   // and judge whether it will invoke reset event.\r
   //\r
-  SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail;\r
-  Index     = UsbKeyboardDevice->KeyboardBuffer.bHead;\r
+  SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail;\r
+  Index     = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
   while (Index != SavedTail) {\r
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
@@ -1171,7 +1193,7 @@ KeyboardHandler (
       break;\r
 \r
     //\r
-    // Del Key Code\r
+    // For Del Key, check if Ctrl + Alt + Del occurs for reset.\r
     //\r
     case EFI_DELETE_MODIFIER:\r
       if (UsbKey.Down != 0) {\r
@@ -1186,7 +1208,7 @@ KeyboardHandler (
     }\r
 \r
     //\r
-    // insert the key back to the buffer.\r
+    // Insert the key back to the buffer,\r
     // so the key sequence will not be destroyed.\r
     //\r
     InsertKeyCode (\r
@@ -1194,7 +1216,7 @@ KeyboardHandler (
       UsbKey.KeyCode,\r
       UsbKey.Down\r
       );\r
-    Index = UsbKeyboardDevice->KeyboardBuffer.bHead;\r
+    Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead;\r
 \r
   }\r
   //\r
@@ -1203,7 +1225,7 @@ KeyboardHandler (
   //\r
   if (NewRepeatKey != 0) {\r
     //\r
-    // sets trigger time to "Repeat Delay Time",\r
+    // Sets trigger time to "Repeat Delay Time",\r
     // to trigger the repeat timer when the key is hold long\r
     // enough time.\r
     //\r
@@ -1243,12 +1265,15 @@ USBParseKey (
 \r
   while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {\r
     //\r
-    // pops one raw data off.\r
+    // Pops one raw data off.\r
     //\r
     RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);\r
 \r
     KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);\r
     if (UsbKey.Down == 0) {\r
+      //\r
+      // Key is released.\r
+      //\r
       switch (KeyDescriptor->Modifier) {\r
 \r
       //\r
@@ -1417,7 +1442,7 @@ USBParseKey (
     case EFI_NUM_LOCK_MODIFIER:\r
       UsbKeyboardDevice->NumLockOn ^= 1;\r
       //\r
-      // Turn on the NumLock light on KB\r
+      // Set the NumLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
@@ -1426,7 +1451,7 @@ USBParseKey (
     case EFI_CAPS_LOCK_MODIFIER:\r
       UsbKeyboardDevice->CapsOn ^= 1;\r
       //\r
-      // Turn on the CapsLock light on KB\r
+      // Set the CapsLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
@@ -1435,22 +1460,22 @@ USBParseKey (
     case EFI_SCROLL_LOCK_MODIFIER:\r
       UsbKeyboardDevice->ScrollOn ^= 1;\r
       //\r
-      // Turn on the ScrollLock light on KB\r
+      // Set the ScrollLock light on keyboard\r
       //\r
       SetKeyLED (UsbKeyboardDevice);\r
       continue;\r
       break;\r
 \r
     //\r
-    // F11,F12,PrintScreen,Pause/Break\r
-    // could not be retrieved via SimpleTxtInEx protocol\r
+    // F11, F12, PrintScreen, Pause/Break\r
+    // could not be retrieved via SimpleTextInEx protocol\r
     //\r
     case EFI_FUNCTION_KEY_ELEVEN_MODIFIER:\r
     case EFI_FUNCTION_KEY_TWELVE_MODIFIER:\r
     case EFI_PAUSE_MODIFIER:\r
     case EFI_BREAK_MODIFIER:\r
       //\r
-      // fall through\r
+      // Fall through\r
       //\r
       continue;\r
       break;\r
@@ -1460,7 +1485,7 @@ USBParseKey (
     }\r
 \r
     //\r
-    // When encountered Del Key...\r
+    // 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
@@ -1477,7 +1502,7 @@ USBParseKey (
 \r
 \r
 /**\r
-  Converts USB Keyboard code to EFI Scan Code.\r
+  Converts USB Keyboard code 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
@@ -1491,7 +1516,7 @@ USBParseKey (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-USBKeyCodeToEFIScanCode (\r
+UsbKeyCodeToEfiInputKey (\r
   IN  USB_KB_DEV      *UsbKeyboardDevice,\r
   IN  UINT8           KeyChar,\r
   OUT EFI_INPUT_KEY   *Key\r
@@ -1505,7 +1530,7 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   //\r
-  // valid USB Key Code starts from 4\r
+  // Valid USB Key Code starts from 4, so it's safe to minus 4.\r
   //\r
   Index = (UINT8) (KeyChar - 4);\r
 \r
@@ -1515,18 +1540,19 @@ USBKeyCodeToEFIScanCode (
 \r
   KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyChar);\r
 \r
-  //\r
-  // Check for Non-spacing key\r
-  //\r
   if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {\r
+    //\r
+    // If this is a dead key with EFI_NS_KEY_MODIFIER, then record it and return.\r
+    //\r
     UsbKeyboardDevice->CurrentNsKey = FindUsbNsKey (UsbKeyboardDevice, KeyDescriptor);\r
     return EFI_NOT_READY;\r
   }\r
 \r
-  //\r
-  // Check whether this keystroke follows a Non-spacing key\r
-  //\r
   if (UsbKeyboardDevice->CurrentNsKey != NULL) {\r
+    //\r
+    // If this keystroke follows a non-spacing key, then find the descriptor for corresponding\r
+    // physical key.\r
+    //\r
     KeyDescriptor = FindPhysicalKey (UsbKeyboardDevice->CurrentNsKey, KeyDescriptor);\r
     UsbKeyboardDevice->CurrentNsKey = NULL;\r
   }\r
@@ -1534,7 +1560,7 @@ USBKeyCodeToEFIScanCode (
   Key->ScanCode = EfiScanCodeConvertionTable[KeyDescriptor->Modifier];\r
   Key->UnicodeChar = KeyDescriptor->Unicode;\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT) {\r
+  if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) {\r
     if (UsbKeyboardDevice->ShiftOn != 0) {\r
       Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;\r
 \r
@@ -1542,7 +1568,7 @@ USBKeyCodeToEFIScanCode (
       // 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) {\r
+      if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
         UsbKeyboardDevice->LeftShiftOn = 0;\r
         UsbKeyboardDevice->RightShiftOn = 0;\r
       }\r
@@ -1562,7 +1588,7 @@ USBKeyCodeToEFIScanCode (
     }\r
   }\r
 \r
-  if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) {\r
+  if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {\r
     if (UsbKeyboardDevice->CapsOn != 0) {\r
 \r
       if (Key->UnicodeChar == KeyDescriptor->Unicode) {\r
@@ -1578,7 +1604,8 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   //\r
-  // Translate the CTRL-Alpha characters to their corresponding control value  (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
+  // 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 (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {\r
@@ -1589,11 +1616,13 @@ USBKeyCodeToEFIScanCode (
   }\r
 \r
   if (KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) {\r
-\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
-\r
       Key->ScanCode = SCAN_NULL;\r
-\r
     } else {\r
       Key->UnicodeChar = 0x00;\r
     }\r
@@ -1607,6 +1636,9 @@ USBKeyCodeToEFIScanCode (
     Key->UnicodeChar = 0x00;\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
     return EFI_NOT_READY;\r
   }\r
@@ -1666,10 +1698,8 @@ USBKeyCodeToEFIScanCode (
 \r
   @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
 \r
-  @retval EFI_SUCCESS        Init key buffer successfully.\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InitUSBKeyBuffer (\r
   IN OUT  USB_KB_BUFFER   *KeyboardBuffer\r
@@ -1677,9 +1707,7 @@ InitUSBKeyBuffer (
 {\r
   ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));\r
 \r
-  KeyboardBuffer->bHead = KeyboardBuffer->bTail;\r
-\r
-  return EFI_SUCCESS;\r
+  KeyboardBuffer->BufferHead = KeyboardBuffer->BufferTail;\r
 }\r
 \r
 \r
@@ -1701,7 +1729,7 @@ IsUSBKeyboardBufferEmpty (
   //\r
   // meet FIFO empty condition\r
   //\r
-  return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail);\r
+  return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail);\r
 }\r
 \r
 \r
@@ -1720,8 +1748,7 @@ IsUSBKeyboardBufferFull (
   IN  USB_KB_BUFFER   *KeyboardBuffer\r
   )\r
 {\r
-  return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) ==\r
-                                                        KeyboardBuffer->bHead);\r
+  return (BOOLEAN)(((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)) == KeyboardBuffer->BufferHead);\r
 }\r
 \r
 \r
@@ -1732,10 +1759,8 @@ IsUSBKeyboardBufferFull (
   @param  Key                Key code\r
   @param  Down               Special key\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 InsertKeyCode (\r
   IN OUT  USB_KB_BUFFER *KeyboardBuffer,\r
@@ -1753,15 +1778,13 @@ InsertKeyCode (
     RemoveKeyCode (KeyboardBuffer, &UsbKey);\r
   }\r
 \r
-  KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key;\r
-  KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down    = Down;\r
+  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;\r
+  KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down    = Down;\r
 \r
   //\r
-  // adjust the tail pointer of the FIFO keyboard buffer.\r
+  // Adjust the tail pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1));\r
-\r
-  return EFI_SUCCESS;\r
+  KeyboardBuffer->BufferTail = (UINT8) ((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1));\r
 }\r
 \r
 \r
@@ -1771,8 +1794,8 @@ InsertKeyCode (
   @param  KeyboardBuffer     Points to the USB Keyboard Buffer.\r
   @param  UsbKey             Points to the buffer that contains a usb key code.\r
 \r
-  @retval EFI_SUCCESS        Success\r
-  @retval EFI_DEVICE_ERROR   Hardware Error\r
+  @retval EFI_SUCCESS        Key code Successfully poped from keyboard buffer.\r
+  @retval EFI_DEVICE_ERROR   Keyboard buffer is empty.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1786,13 +1809,13 @@ RemoveKeyCode (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode;\r
-  UsbKey->Down    = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down;\r
+  UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode;\r
+  UsbKey->Down    = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down;\r
 \r
   //\r
-  // adjust the head pointer of the FIFO keyboard buffer.\r
+  // Adjust the head pointer of the FIFO keyboard buffer.\r
   //\r
-  KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1));\r
+  KeyboardBuffer->BufferHead = (UINT8) ((KeyboardBuffer->BufferHead + 1) % (MAX_KEY_ALLOWED + 1));\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1803,10 +1826,8 @@ RemoveKeyCode (
 \r
   @param  UsbKeyboardDevice  The USB_KB_DEV instance.\r
 \r
-  @retval EFI_SUCCESS        Success\r
-\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
 SetKeyLED (\r
   IN  USB_KB_DEV    *UsbKeyboardDevice\r
@@ -1825,7 +1846,7 @@ SetKeyLED (
 \r
   ReportId       = 0;\r
   //\r
-  // call Set Report Request to lighten the LED.\r
+  // Call Set_Report Request to lighten the LED.\r
   //\r
   UsbSetReportRequest (\r
     UsbKeyboardDevice->UsbIo,\r
@@ -1835,8 +1856,6 @@ SetKeyLED (
     1,\r
     (UINT8 *) &Led\r
     );\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -1846,7 +1865,6 @@ SetKeyLED (
   @param  Event              The Repeat Key event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r
 \r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1873,14 +1891,13 @@ USBKeyboardRepeatHandler (
       );\r
 \r
     //\r
-    // set repeate rate for repeat key generation.\r
+    // Set repeate rate for next repeat key generation.\r
     //\r
     gBS->SetTimer (\r
-          UsbKeyboardDevice->RepeatTimer,\r
-          TimerRelative,\r
-          USBKBD_REPEAT_RATE\r
-          );\r
-\r
+           UsbKeyboardDevice->RepeatTimer,\r
+           TimerRelative,\r
+           USBKBD_REPEAT_RATE\r
+           );\r
   }\r
 }\r
 \r
@@ -1891,7 +1908,6 @@ USBKeyboardRepeatHandler (
   @param  Event              The Delayed Recovery event.\r
   @param  Context            Points to the USB_KB_DEV instance.\r
 \r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1911,13 +1927,16 @@ USBKeyboardRecoveryHandler (
 \r
   PacketSize        = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);\r
 \r
+  //\r
+  // Re-submit Asynchronous Interrupt Transfer for recovery.\r
+  //\r
   UsbIo->UsbAsyncInterruptTransfer (\r
-          UsbIo,\r
-          UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
-          TRUE,\r
-          UsbKeyboardDevice->IntEndpointDescriptor.Interval,\r
-          PacketSize,\r
-          KeyboardHandler,\r
-          UsbKeyboardDevice\r
-          );\r
+           UsbIo,\r
+           UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,\r
+           TRUE,\r
+           UsbKeyboardDevice->IntEndpointDescriptor.Interval,\r
+           PacketSize,\r
+           KeyboardHandler,\r
+           UsbKeyboardDevice\r
+           );\r
 }\r