]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2KbdCtrller.c
Clean up the Ps2keyboardDxe module
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2KbdCtrller.c
index 7fa0fb4d21a21cb6d17087a31094a2ab3199def9..eb03c0c836614f90940c8660683f8acb639d0e18 100644 (file)
@@ -1,5 +1,4 @@
 /**@file\r
-  PS/2 Keyboard driver\r
   Routines that access 8042 keyboard controller\r
 \r
 Copyright (c) 2006 - 2007, Intel Corporation\r
@@ -15,66 +14,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "Ps2Keyboard.h"\r
 \r
-//\r
-// Function declarations\r
-//\r
-UINT8\r
-KeyReadDataRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
-  );\r
-\r
-VOID\r
-KeyWriteDataRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
-  );\r
-\r
-VOID\r
-KeyWriteCommandRegister (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
-  );\r
-\r
-VOID\r
-KeyboardError (\r
-  IN KEYBOARD_CONSOLE_IN_DEV*ConsoleIn,\r
-  IN CHAR16                 *ErrMsg // should be a unicode string\r
-  );\r
-\r
-EFI_STATUS\r
-GetScancodeBufHead (\r
-  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
-  IN UINT32                Count,\r
-  OUT UINT8                *Buf\r
-  );\r
-\r
-EFI_STATUS\r
-PopScancodeBufHead (\r
-  KEYBOARD_CONSOLE_IN_DEV  *ConsoleIn,\r
-  IN UINT32                Count,\r
-  OUT UINT8                *Buf\r
-  );\r
-\r
-EFI_STATUS\r
-KeyboardWrite (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
-  );\r
-\r
-EFI_STATUS\r
-KeyboardCommand (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Data\r
-  );\r
-\r
-EFI_STATUS\r
-KeyboardWaitForValue (\r
-  IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
-  IN UINT8                   Value\r
-  );\r
-\r
 struct {\r
-  UINT8  ScanCode;\r
+  UINT8   ScanCode;             ///< follows value defined in Scan Code Set1\r
   UINT16  EfiScanCode;\r
   CHAR16  UnicodeChar;\r
   CHAR16  ShiftUnicodeChar;\r
@@ -84,530 +25,530 @@ ConvertKeyboardScanCodeToEfiKey[] = {
   {\r
     0x01,  //   Escape\r
     SCAN_ESC,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x02,\r
     SCAN_NULL,\r
-    '1',\r
-    '!'\r
+    L'1',\r
+    L'!'\r
   },\r
   {\r
     0x03,\r
     SCAN_NULL,\r
-    '2',\r
-    '@'\r
+    L'2',\r
+    L'@'\r
   },\r
   {\r
     0x04,\r
     SCAN_NULL,\r
-    '3',\r
-    '#'\r
+    L'3',\r
+    L'#'\r
   },\r
   {\r
     0x05,\r
     SCAN_NULL,\r
-    '4',\r
-    '$'\r
+    L'4',\r
+    L'$'\r
   },\r
   {\r
     0x06,\r
     SCAN_NULL,\r
-    '5',\r
-    '%'\r
+    L'5',\r
+    L'%'\r
   },\r
   {\r
     0x07,\r
     SCAN_NULL,\r
-    '6',\r
-    '^'\r
+    L'6',\r
+    L'^'\r
   },\r
   {\r
     0x08,\r
     SCAN_NULL,\r
-    '7',\r
-    '&'\r
+    L'7',\r
+    L'&'\r
   },\r
   {\r
     0x09,\r
     SCAN_NULL,\r
-    '8',\r
-    '*'\r
+    L'8',\r
+    L'*'\r
   },\r
   {\r
     0x0A,\r
     SCAN_NULL,\r
-    '9',\r
-    '('\r
+    L'9',\r
+    L'('\r
   },\r
   {\r
     0x0B,\r
     SCAN_NULL,\r
-    '0',\r
-    ')'\r
+    L'0',\r
+    L')'\r
   },\r
   {\r
     0x0C,\r
     SCAN_NULL,\r
-    '-',\r
-    '_'\r
+    L'-',\r
+    L'_'\r
   },\r
   {\r
     0x0D,\r
     SCAN_NULL,\r
-    '=',\r
-    '+'\r
+    L'=',\r
+    L'+'\r
   },\r
   {\r
     0x0E, //  BackSpace\r
     SCAN_NULL,\r
-    0x08,\r
-    0x08\r
+    0x0008,\r
+    0x0008\r
   },\r
   {\r
     0x0F, //  Tab\r
     SCAN_NULL,\r
-    0x09,\r
-    0x09\r
+    0x0009,\r
+    0x0009\r
   },\r
   {\r
     0x10,\r
     SCAN_NULL,\r
-    'q',\r
-    'Q'\r
+    L'q',\r
+    L'Q'\r
   },\r
   {\r
     0x11,\r
     SCAN_NULL,\r
-    'w',\r
-    'W'\r
+    L'w',\r
+    L'W'\r
   },\r
   {\r
     0x12,\r
     SCAN_NULL,\r
-    'e',\r
-    'E'\r
+    L'e',\r
+    L'E'\r
   },\r
   {\r
     0x13,\r
     SCAN_NULL,\r
-    'r',\r
-    'R'\r
+    L'r',\r
+    L'R'\r
   },\r
   {\r
     0x14,\r
     SCAN_NULL,\r
-    't',\r
-    'T'\r
+    L't',\r
+    L'T'\r
   },\r
   {\r
     0x15,\r
     SCAN_NULL,\r
-    'y',\r
-    'Y'\r
+    L'y',\r
+    L'Y'\r
   },\r
   {\r
     0x16,\r
     SCAN_NULL,\r
-    'u',\r
-    'U'\r
+    L'u',\r
+    L'U'\r
   },\r
   {\r
     0x17,\r
     SCAN_NULL,\r
-    'i',\r
-    'I'\r
+    L'i',\r
+    L'I'\r
   },\r
   {\r
     0x18,\r
     SCAN_NULL,\r
-    'o',\r
-    'O'\r
+    L'o',\r
+    L'O'\r
   },\r
   {\r
     0x19,\r
     SCAN_NULL,\r
-    'p',\r
-    'P'\r
+    L'p',\r
+    L'P'\r
   },\r
   {\r
     0x1a,\r
     SCAN_NULL,\r
-    '[',\r
-    '{'\r
+    L'[',\r
+    L'{'\r
   },\r
   {\r
     0x1b,\r
     SCAN_NULL,\r
-    ']',\r
-    '}'\r
+    L']',\r
+    L'}'\r
   },\r
   {\r
     0x1c, //   Enter\r
     SCAN_NULL,\r
-    0x0d,\r
-    0x0d\r
+    0x000d,\r
+    0x000d\r
   },\r
   {\r
     0x1d,\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x1e,\r
     SCAN_NULL,\r
-    'a',\r
-    'A'\r
+    L'a',\r
+    L'A'\r
   },\r
   {\r
     0x1f,\r
     SCAN_NULL,\r
-    's',\r
-    'S'\r
+    L's',\r
+    L'S'\r
   },\r
   {\r
     0x20,\r
     SCAN_NULL,\r
-    'd',\r
-    'D'\r
+    L'd',\r
+    L'D'\r
   },\r
   {\r
     0x21,\r
     SCAN_NULL,\r
-    'f',\r
-    'F'\r
+    L'f',\r
+    L'F'\r
   },\r
   {\r
     0x22,\r
     SCAN_NULL,\r
-    'g',\r
-    'G'\r
+    L'g',\r
+    L'G'\r
   },\r
   {\r
     0x23,\r
     SCAN_NULL,\r
-    'h',\r
-    'H'\r
+    L'h',\r
+    L'H'\r
   },\r
   {\r
     0x24,\r
     SCAN_NULL,\r
-    'j',\r
-    'J'\r
+    L'j',\r
+    L'J'\r
   },\r
   {\r
     0x25,\r
     SCAN_NULL,\r
-    'k',\r
-    'K'\r
+    L'k',\r
+    L'K'\r
   },\r
   {\r
     0x26,\r
     SCAN_NULL,\r
-    'l',\r
-    'L'\r
+    L'l',\r
+    L'L'\r
   },\r
   {\r
     0x27,\r
     SCAN_NULL,\r
-    ';',\r
-    ':'\r
+    L';',\r
+    L':'\r
   },\r
   {\r
     0x28,\r
     SCAN_NULL,\r
-    '\'',\r
-    '"'\r
+    L'\'',\r
+    L'"'\r
   },\r
   {\r
     0x29,\r
     SCAN_NULL,\r
-    '`',\r
-    '~'\r
+    L'`',\r
+    L'~'\r
   },\r
   {\r
     0x2a, //   Left Shift\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x2b,\r
     SCAN_NULL,\r
-    '\\',\r
-    '|'\r
+    L'\\',\r
+    L'|'\r
   },\r
   {\r
     0x2c,\r
     SCAN_NULL,\r
-    'z',\r
-    'Z'\r
+    L'z',\r
+    L'Z'\r
   },\r
   {\r
     0x2d,\r
     SCAN_NULL,\r
-    'x',\r
-    'X'\r
+    L'x',\r
+    L'X'\r
   },\r
   {\r
     0x2e,\r
     SCAN_NULL,\r
-    'c',\r
-    'C'\r
+    L'c',\r
+    L'C'\r
   },\r
   {\r
     0x2f,\r
     SCAN_NULL,\r
-    'v',\r
-    'V'\r
+    L'v',\r
+    L'V'\r
   },\r
   {\r
     0x30,\r
     SCAN_NULL,\r
-    'b',\r
-    'B'\r
+    L'b',\r
+    L'B'\r
   },\r
   {\r
     0x31,\r
     SCAN_NULL,\r
-    'n',\r
-    'N'\r
+    L'n',\r
+    L'N'\r
   },\r
   {\r
     0x32,\r
     SCAN_NULL,\r
-    'm',\r
-    'M'\r
+    L'm',\r
+    L'M'\r
   },\r
   {\r
     0x33,\r
     SCAN_NULL,\r
-    ',',\r
-    '<'\r
+    L',',\r
+    L'<'\r
   },\r
   {\r
     0x34,\r
     SCAN_NULL,\r
-    '.',\r
-    '>'\r
+    L'.',\r
+    L'>'\r
   },\r
   {\r
     0x35,\r
     SCAN_NULL,\r
-    '/',\r
-    '?'\r
+    L'/',\r
+    L'?'\r
   },\r
   {\r
     0x36, //Right Shift\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x37, // Numeric Keypad *\r
     SCAN_NULL,\r
-    '*',\r
-    '*'\r
+    L'*',\r
+    L'*'\r
   },\r
   {\r
     0x38,  //Left Alt/Extended Right Alt\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x39,\r
     SCAN_NULL,\r
-    ' ',\r
-    ' '\r
+    L' ',\r
+    L' '\r
   },\r
   {\r
     0x3A, //CapsLock\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x3B,\r
     SCAN_F1,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x3C,\r
     SCAN_F2,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x3D,\r
     SCAN_F3,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x3E,\r
     SCAN_F4,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x3F,\r
     SCAN_F5,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x40,\r
     SCAN_F6,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x41,\r
     SCAN_F7,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x42,\r
     SCAN_F8,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x43,\r
     SCAN_F9,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x44,\r
     SCAN_F10,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x45, // NumLock\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x46, //  ScrollLock\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x47,\r
     SCAN_HOME,\r
-    '7',\r
-    '7'\r
+    L'7',\r
+    L'7'\r
   },\r
   {\r
     0x48,\r
     SCAN_UP,\r
-    '8',\r
-    '8'\r
+    L'8',\r
+    L'8'\r
   },\r
   {\r
     0x49,\r
     SCAN_PAGE_UP,\r
-    '9',\r
-    '9'\r
+    L'9',\r
+    L'9'\r
   },\r
   {\r
     0x4a,\r
     SCAN_NULL,\r
-    '-',\r
-    '-'\r
+    L'-',\r
+    L'-'\r
   },\r
   {\r
     0x4b,\r
     SCAN_LEFT,\r
-    '4',\r
-    '4'\r
+    L'4',\r
+    L'4'\r
   },\r
   {\r
     0x4c, //  Numeric Keypad 5\r
     SCAN_NULL,\r
-    '5',\r
-    '5'\r
+    L'5',\r
+    L'5'\r
   },\r
   {\r
     0x4d,\r
     SCAN_RIGHT,\r
-    '6',\r
-    '6'\r
+    L'6',\r
+    L'6'\r
   },\r
   {\r
     0x4e,\r
     SCAN_NULL,\r
-    '+',\r
-    '+'\r
+    L'+',\r
+    L'+'\r
   },\r
   {\r
     0x4f,\r
     SCAN_END,\r
-    '1',\r
-    '1'\r
+    L'1',\r
+    L'1'\r
   },\r
   {\r
     0x50,\r
     SCAN_DOWN,\r
-    '2',\r
-    '2'\r
+    L'2',\r
+    L'2'\r
   },\r
   {\r
     0x51,\r
     SCAN_PAGE_DOWN,\r
-    '3',\r
-    '3'\r
+    L'3',\r
+    L'3'\r
   },\r
   {\r
     0x52,\r
     SCAN_INSERT,\r
-    '0',\r
-    '0'\r
+    L'0',\r
+    L'0'\r
   },\r
   {\r
     0x53,\r
     SCAN_DELETE,\r
-    '.',\r
-    '.'\r
+    L'.',\r
+    L'.'\r
   },\r
   {\r
     0x57,\r
     SCAN_F11,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x58,\r
     SCAN_F12,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },\r
   {\r
     0x5B,  //Left LOGO\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },  \r
   {\r
     0x5C,  //Right LOGO\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },  \r
   {\r
     0x5D,  //Menu key\r
     SCAN_NULL,\r
-    0x00,\r
-    0x00\r
+    0x0000,\r
+    0x0000\r
   },    \r
   {\r
     TABLE_END,\r
@@ -687,9 +628,6 @@ KeyWriteDataRegister (
               &Data\r
               );\r
 \r
-  //\r
-  // outp(ConsoleIn->DataRegisterAddress, Data);\r
-  //\r
 }\r
 \r
 /**\r
@@ -770,11 +708,6 @@ KeyboardError (
   )\r
 {\r
   ConsoleIn->KeyboardErr = TRUE;\r
-\r
-  //\r
-  // gST -> ConOut -> OutputString (gST -> ConOut, L"Keyboard Driver: ");\r
-  // gST -> ConOut -> OutputString (gST -> ConOut, ErrMsg);\r
-  //\r
 }\r
 \r
 /**\r
@@ -784,8 +717,8 @@ KeyboardError (
   the memory buffer or empty the keyboard buffer.\r
   It is registered as running under TPL_NOTIFY\r
 \r
-  @param Event - The timer event\r
-  @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer\r
+  @param Event       The timer event\r
+  @param Context     A KEYBOARD_CONSOLE_IN_DEV pointer\r
 \r
 **/\r
 VOID\r
@@ -814,6 +747,7 @@ KeyboardTimerHandler (
     gBS->RestoreTPL (OldTpl);\r
     return ;\r
   }\r
+\r
   //\r
   // To let KB driver support Hot plug, here should skip the 'resend' command  for the case that\r
   // KB is not connected to system. If KB is not connected to system, driver will find there's  something\r
@@ -827,7 +761,7 @@ KeyboardTimerHandler (
   //\r
   // if there is no key present, just return\r
   //\r
-  if ((KeyReadStatusRegister (Context) & 0x21) != 0x1) {\r
+  if ((KeyReadStatusRegister (Context) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) != KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) {\r
     //\r
     // Leave critical section and return\r
     //\r
@@ -1019,13 +953,13 @@ KeyboardRead (
   // wait till output buffer full then perform the read\r
   //\r
   for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
-    if (KeyReadStatusRegister (ConsoleIn) & 0x01) {\r
+    if (KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) {\r
       RegFilled = 1;\r
       *Data     = KeyReadDataRegister (ConsoleIn);\r
       break;\r
     }\r
 \r
-    gBS->Stall (30);\r
+    MicroSecondDelay (30);\r
   }\r
 \r
   if (!RegFilled) {\r
@@ -1041,8 +975,8 @@ KeyboardRead (
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   @param Data      value wanted to be written\r
 \r
-  @retval EFI_TIMEOUT - GC_TODO: Add description for return value\r
-  @retval EFI_SUCCESS - GC_TODO: Add description for return value\r
+  @retval EFI_TIMEOUT   The input buffer register is full for putting new value util timeout\r
+  @retval EFI_SUCCESS   The new value is sucess put into input buffer register.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1066,7 +1000,7 @@ KeyboardWrite (
       break;\r
     }\r
 \r
-    gBS->Stall (30);\r
+    MicroSecondDelay (30);\r
   }\r
 \r
   if (!RegEmptied) {\r
@@ -1111,7 +1045,7 @@ KeyboardCommand (
       break;\r
     }\r
 \r
-    gBS->Stall (30);\r
+    MicroSecondDelay (30);\r
   }\r
 \r
   if (!RegEmptied) {\r
@@ -1132,7 +1066,7 @@ KeyboardCommand (
       break;\r
     }\r
 \r
-    gBS->Stall (30);\r
+    MicroSecondDelay (30);\r
   }\r
 \r
   if (!RegEmptied) {\r
@@ -1191,7 +1125,7 @@ KeyboardWaitForValue (
         break;\r
       }\r
 \r
-      gBS->Stall (30);\r
+      MicroSecondDelay (30);\r
     }\r
 \r
     SumTimeOut += TimeOut;\r
@@ -1208,7 +1142,7 @@ KeyboardWaitForValue (
   //\r
   // Check results\r
   //\r
-  if (GotIt) {\r
+  if (GotIt == 1) {\r
     return EFI_SUCCESS;\r
   } else {\r
     return EFI_TIMEOUT;\r
@@ -1222,7 +1156,7 @@ KeyboardWaitForValue (
 \r
   @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
   \r
-  @return status\r
+  @return status of updating keyboard register\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1276,8 +1210,8 @@ UpdateStatusLights (
 \r
   @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer\r
 \r
-  @retval EFI_NOT_READY - Input from console not ready yet.\r
-  @retval EFI_SUCCESS   - Function executed successfully.\r
+  @retval EFI_NOT_READY  Input from console not ready yet.\r
+  @retval EFI_SUCCESS    Function executed successfully.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1365,7 +1299,7 @@ KeyGetchar (
   // Check if there are enough bytes of scancode representing a single key\r
   // available in the buffer\r
   //\r
-  while (1) {\r
+  while (TRUE) {\r
 \r
     Status          = GetScancodeBufHead (ConsoleIn, 1, ScancodeArr);\r
     ScancodeArrPos  = 0;\r
@@ -1516,7 +1450,7 @@ KeyGetchar (
   //\r
   if (Extended && ScanCode == 0x35) {\r
     ConsoleIn->Key.ScanCode     = SCAN_NULL;\r
-    ConsoleIn->Key.UnicodeChar  = '/';\r
+    ConsoleIn->Key.UnicodeChar  = L'/';\r
     return EFI_SUCCESS;\r
   }\r
   //\r
@@ -1531,7 +1465,7 @@ KeyGetchar (
         // 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 (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {\r
+        if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
           ConsoleIn->LeftShift  = FALSE;\r
           ConsoleIn->RightShift = FALSE;\r
         }\r
@@ -1542,9 +1476,9 @@ KeyGetchar (
       // alphabetic key is affected by CapsLock State\r
       //\r
       if (ConsoleIn->CapsLock) {\r
-        if (ConsoleIn->Key.UnicodeChar >= 'a' && ConsoleIn->Key.UnicodeChar <= 'z') {\r
+        if (ConsoleIn->Key.UnicodeChar >= L'a' && ConsoleIn->Key.UnicodeChar <= L'z') {\r
           ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
-        } else if (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {\r
+        } else if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
           ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
         }\r
       }\r
@@ -1552,10 +1486,10 @@ KeyGetchar (
       // Translate the CTRL-Alpha characters to their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A)\r
       //\r
       if (ConsoleIn->Ctrled) {\r
-        if (ConsoleIn->Key.UnicodeChar >= 'a' && ConsoleIn->Key.UnicodeChar <= 'z') {\r
-          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - 'a' + 1);\r
-        } else if (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {\r
-          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - 'A' + 1);\r
+        if (ConsoleIn->Key.UnicodeChar >= L'a' && ConsoleIn->Key.UnicodeChar <= L'z') {\r
+          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - L'a' + 1);\r
+        } else if (ConsoleIn->Key.UnicodeChar >= L'A' && ConsoleIn->Key.UnicodeChar <= L'Z') {\r
+          ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - L'A' + 1);\r
         }\r
       }\r
 \r
@@ -1571,13 +1505,13 @@ KeyGetchar (
     if (ConsoleIn->NumLock && !ConsoleIn->Shift && !Extended) {\r
       ConsoleIn->Key.ScanCode = SCAN_NULL;\r
     } else if (ScanCode != 0x4a && ScanCode != 0x4e) {\r
-      ConsoleIn->Key.UnicodeChar = 0x00;\r
+      ConsoleIn->Key.UnicodeChar = 0x0000;\r
     }\r
   }\r
   //\r
   // If the key can not be converted then just return.\r
   //\r
-  if (ConsoleIn->Key.ScanCode == SCAN_NULL && ConsoleIn->Key.UnicodeChar == 0x00) {\r
+  if (ConsoleIn->Key.ScanCode == SCAN_NULL && ConsoleIn->Key.UnicodeChar == 0x0000) {\r
     return EFI_NOT_READY;\r
   }\r
 \r
@@ -1642,18 +1576,20 @@ InitKeyboard (
   EFI_STATUS              Status1;\r
   UINT8                   CommandByte;\r
   EFI_PS2_POLICY_PROTOCOL *Ps2Policy;\r
+  UINT32                  TryTime;\r
 \r
-  Status                = EFI_SUCCESS;\r
+  Status                 = EFI_SUCCESS;\r
   mEnableMouseInterface  = TRUE;\r
+  TryTime                = 0;\r
 \r
   //\r
   // Get Ps2 policy to set this\r
   //\r
-  Status = gBS->LocateProtocol (\r
-                  &gEfiPs2PolicyProtocolGuid,\r
-                  NULL,\r
-                  (VOID **) &Ps2Policy\r
-                  );\r
+  gBS->LocateProtocol (\r
+        &gEfiPs2PolicyProtocolGuid,\r
+        NULL,\r
+        (VOID **) &Ps2Policy\r
+        );\r
 \r
   REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
     EFI_PROGRESS_CODE,\r
@@ -1663,10 +1599,18 @@ InitKeyboard (
 \r
   //\r
   // Perform a read to cleanup the Status Register's\r
-  // output buffer full bits\r
+  // output buffer full bits within MAX TRY times\r
   //\r
-  while (!EFI_ERROR (Status)) {\r
+  while (!EFI_ERROR (Status) && TryTime < KEYBOARD_MAX_TRY) {\r
     Status = KeyboardRead (ConsoleIn, &CommandByte);\r
+    TryTime ++;\r
+  }\r
+  //\r
+  // Exceed the max try times. The device may be error.\r
+  //\r
+  if (TryTime == KEYBOARD_MAX_TRY) {\r
+       Status = EFI_DEVICE_ERROR;\r
+       goto Done;\r
   }\r
   //\r
   // We should disable mouse interface during the initialization process\r
@@ -1680,7 +1624,7 @@ InitKeyboard (
   // Test the system flag in to determine whether this is the first\r
   // time initialization\r
   //\r
-  if ((KeyReadStatusRegister (ConsoleIn) & 0x04)) {\r
+  if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG)) {\r
     //\r
     // 8042 controller is already setup (by myself or by mouse driver):\r
     //   See whether mouse interface is already enabled\r
@@ -1689,7 +1633,7 @@ InitKeyboard (
     //\r
     // Read the command byte of 8042 controller\r
     //\r
-    Status = KeyboardCommand (ConsoleIn, 0x20);\r
+    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_READ);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"\n\r");\r
       goto Done;\r
@@ -1718,13 +1662,13 @@ InitKeyboard (
     //\r
     // Disable keyboard and mouse interfaces\r
     //\r
-    Status = KeyboardCommand (ConsoleIn, 0xad);\r
+    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"\n\r");\r
       goto Done;\r
     }\r
 \r
-    Status = KeyboardCommand (ConsoleIn, 0xa7);\r
+    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"\n\r");\r
       goto Done;\r
@@ -1738,7 +1682,7 @@ InitKeyboard (
     //\r
     // 8042 Controller Self Test\r
     //\r
-    Status = KeyboardCommand (ConsoleIn, 0xaa);\r
+    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
       goto Done;\r
@@ -1777,7 +1721,7 @@ InitKeyboard (
   //  1: Enable Auxiliary device interrupt\r
   //  0: Enable Keyboard interrupt )\r
   //\r
-  Status = KeyboardCommand (ConsoleIn, 0x60);\r
+  Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_WRITE);\r
   if (EFI_ERROR (Status)) {\r
     KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
     goto Done;\r
@@ -1827,7 +1771,7 @@ InitKeyboard (
     //\r
     // Keyboard Interface Test\r
     //\r
-    Status = KeyboardCommand (ConsoleIn, 0xab);\r
+    Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
       goto Done;\r
@@ -1844,13 +1788,13 @@ InitKeyboard (
     //\r
     // Keyboard reset with a BAT(Basic Assurance Test)\r
     //\r
-    Status = KeyboardWrite (ConsoleIn, 0xff);\r
+    Status = KeyboardWrite (ConsoleIn, KEYBOARD_8048_COMMAND_RESET);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
       goto Done;\r
     }\r
 \r
-    Status = KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+    Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
       goto Done;\r
@@ -1860,7 +1804,7 @@ InitKeyboard (
     //\r
     mWaitForValueTimeOut  = KEYBOARD_BAT_TIMEOUT;\r
 \r
-    Status                = KeyboardWaitForValue (ConsoleIn, 0xaa);\r
+    Status                = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"Keyboard self test failed!\n\r");\r
       goto Done;\r
@@ -1871,13 +1815,13 @@ InitKeyboard (
     //\r
     // Set Keyboard to use Scan Code Set 2\r
     //\r
-    Status = KeyboardWrite (ConsoleIn, 0xf0);\r
+    Status = KeyboardWrite (ConsoleIn, KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
       goto Done;\r
     }\r
 \r
-    Status = KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+    Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
       goto Done;\r
@@ -1889,7 +1833,7 @@ InitKeyboard (
       goto Done;\r
     }\r
 \r
-    Status = KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+    Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
     if (EFI_ERROR (Status)) {\r
       KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
       goto Done;\r
@@ -1898,13 +1842,13 @@ InitKeyboard (
   //\r
   // Clear Keyboard Scancode Buffer\r
   //\r
-  Status = KeyboardWrite (ConsoleIn, 0xf4);\r
+  Status = KeyboardWrite (ConsoleIn, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA);\r
   if (EFI_ERROR (Status)) {\r
     KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
     goto Done;\r
   }\r
 \r
-  Status = KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+  Status = KeyboardWaitForValue (ConsoleIn, KEYBOARD_8048_RETURN_8042_ACK);\r
   if (EFI_ERROR (Status)) {\r
     KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");\r
     goto Done;\r
@@ -1941,7 +1885,7 @@ Done:
     //\r
     // Enable mouse interface\r
     //\r
-    Status1 = KeyboardCommand (ConsoleIn, 0xa8);\r
+    Status1 = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE);\r
     if (EFI_ERROR (Status1)) {\r
       KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
       return EFI_DEVICE_ERROR;\r
@@ -1974,7 +1918,7 @@ DisableKeyboard (
   //\r
   // Disable keyboard interface\r
   //\r
-  Status = KeyboardCommand (ConsoleIn, 0xad);\r
+  Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
   if (EFI_ERROR (Status)) {\r
     KeyboardError (ConsoleIn, L"\n\r");\r
     return EFI_DEVICE_ERROR;\r