+/** @file\r
+ Routines that access 8042 keyboard controller\r
+\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "Ps2Keyboard.h"\r
+\r
+struct {\r
+ UINT8 ScanCode; ///< follows value defined in Scan Code Set1\r
+ UINT16 EfiScanCode;\r
+ CHAR16 UnicodeChar;\r
+ CHAR16 ShiftUnicodeChar;\r
+}\r
+ConvertKeyboardScanCodeToEfiKey[] = {\r
+\r
+ {\r
+ 0x01, // Escape\r
+ SCAN_ESC,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ 0x02,\r
+ SCAN_NULL,\r
+ L'1',\r
+ L'!'\r
+ },\r
+ {\r
+ 0x03,\r
+ SCAN_NULL,\r
+ L'2',\r
+ L'@'\r
+ },\r
+ {\r
+ 0x04,\r
+ SCAN_NULL,\r
+ L'3',\r
+ L'#'\r
+ },\r
+ {\r
+ 0x05,\r
+ SCAN_NULL,\r
+ L'4',\r
+ L'$'\r
+ },\r
+ {\r
+ 0x06,\r
+ SCAN_NULL,\r
+ L'5',\r
+ L'%'\r
+ },\r
+ {\r
+ 0x07,\r
+ SCAN_NULL,\r
+ L'6',\r
+ L'^'\r
+ },\r
+ {\r
+ 0x08,\r
+ SCAN_NULL,\r
+ L'7',\r
+ L'&'\r
+ },\r
+ {\r
+ 0x09,\r
+ SCAN_NULL,\r
+ L'8',\r
+ L'*'\r
+ },\r
+ {\r
+ 0x0A,\r
+ SCAN_NULL,\r
+ L'9',\r
+ L'('\r
+ },\r
+ {\r
+ 0x0B,\r
+ SCAN_NULL,\r
+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SCAN_F11,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ 0x58,\r
+ SCAN_F12,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ 0x5B, //Left LOGO\r
+ SCAN_NULL,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ 0x5C, //Right LOGO\r
+ SCAN_NULL,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ 0x5D, //Menu key\r
+ SCAN_NULL,\r
+ 0x0000,\r
+ 0x0000\r
+ },\r
+ {\r
+ TABLE_END,\r
+ TABLE_END,\r
+ SCAN_NULL,\r
+ SCAN_NULL\r
+ },\r
+};\r
+\r
+//\r
+// The WaitForValue time out\r
+//\r
+UINTN mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
+\r
+BOOLEAN mEnableMouseInterface;\r
+\r
+\r
+\r
+/**\r
+ Return the count of scancode in the queue.\r
+\r
+ @param Queue Pointer to instance of SCAN_CODE_QUEUE.\r
+\r
+ @return Count of the scancode.\r
+**/\r
+UINTN\r
+GetScancodeBufCount (\r
+ IN SCAN_CODE_QUEUE *Queue\r
+ )\r
+{\r
+ if (Queue->Head <= Queue->Tail) {\r
+ return Queue->Tail - Queue->Head;\r
+ } else {\r
+ return Queue->Tail + KEYBOARD_SCAN_CODE_MAX_COUNT - Queue->Head;\r
+ }\r
+}\r
+\r
+/**\r
+ Read several bytes from the scancode buffer without removing them.\r
+ This function is called to see if there are enough bytes of scancode\r
+ representing a single key.\r
+\r
+ @param Queue Pointer to instance of SCAN_CODE_QUEUE.\r
+ @param Count Number of bytes to be read\r
+ @param Buf Store the results\r
+\r
+ @retval EFI_SUCCESS success to scan the keyboard code\r
+ @retval EFI_NOT_READY invalid parameter\r
+**/\r
+EFI_STATUS\r
+GetScancodeBufHead (\r
+ IN SCAN_CODE_QUEUE *Queue,\r
+ IN UINTN Count,\r
+ OUT UINT8 *Buf\r
+ )\r
+{\r
+ UINTN Index;\r
+ UINTN Pos;\r
+\r
+ //\r
+ // check the valid range of parameter 'Count'\r
+ //\r
+ if (GetScancodeBufCount (Queue) < Count) {\r
+ return EFI_NOT_READY;\r
+ }\r
+ //\r
+ // retrieve the values\r
+ //\r
+ for (Index = 0, Pos = Queue->Head; Index < Count; Index++, Pos = (Pos + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {\r
+ Buf[Index] = Queue->Buffer[Pos];\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+\r
+ Read & remove several bytes from the scancode buffer.\r
+ This function is usually called after GetScancodeBufHead()\r
+\r
+ @param Queue Pointer to instance of SCAN_CODE_QUEUE.\r
+ @param Count Number of bytes to be read\r
+ @param Buf Store the results\r
+\r
+ @retval EFI_SUCCESS success to scan the keyboard code\r
+ @retval EFI_NOT_READY invalid parameter\r
+**/\r
+EFI_STATUS\r
+PopScancodeBufHead (\r
+ IN SCAN_CODE_QUEUE *Queue,\r
+ IN UINTN Count,\r
+ OUT UINT8 *Buf OPTIONAL\r
+ )\r
+{\r
+ UINTN Index;\r
+\r
+ //\r
+ // Check the valid range of parameter 'Count'\r
+ //\r
+ if (GetScancodeBufCount (Queue) < Count) {\r
+ return EFI_NOT_READY;\r
+ }\r
+ //\r
+ // Retrieve and remove the values\r
+ //\r
+ for (Index = 0; Index < Count; Index++, Queue->Head = (Queue->Head + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT) {\r
+ if (Buf != NULL) {\r
+ Buf[Index] = Queue->Buffer[Queue->Head];\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Push one byte to the scancode buffer.\r
+\r
+ @param Queue Pointer to instance of SCAN_CODE_QUEUE.\r
+ @param Scancode The byte to push.\r
+**/\r
+VOID\r
+PushScancodeBufTail (\r
+ IN SCAN_CODE_QUEUE *Queue,\r
+ IN UINT8 Scancode\r
+ )\r
+{\r
+ if (GetScancodeBufCount (Queue) == KEYBOARD_SCAN_CODE_MAX_COUNT - 1) {\r
+ PopScancodeBufHead (Queue, 1, NULL);\r
+ }\r
+\r
+ Queue->Buffer[Queue->Tail] = Scancode;\r
+ Queue->Tail = (Queue->Tail + 1) % KEYBOARD_SCAN_CODE_MAX_COUNT;\r
+}\r
+\r
+/**\r
+ Read data register .\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+\r
+ @return return the value\r
+\r
+**/\r
+UINT8\r
+KeyReadDataRegister (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+\r
+{\r
+ return IoRead8 (ConsoleIn->DataRegisterAddress);\r
+}\r
+\r
+/**\r
+ Write data register.\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Data value wanted to be written\r
+\r
+**/\r
+VOID\r
+KeyWriteDataRegister (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN UINT8 Data\r
+ )\r
+{\r
+ IoWrite8 (ConsoleIn->DataRegisterAddress, Data);\r
+}\r
+\r
+/**\r
+ Read status register.\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+\r
+ @return value in status register\r
+\r
+**/\r
+UINT8\r
+KeyReadStatusRegister (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+{\r
+ return IoRead8 (ConsoleIn->StatusRegisterAddress);\r
+}\r
+\r
+/**\r
+ Write command register .\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Data The value wanted to be written\r
+\r
+**/\r
+VOID\r
+KeyWriteCommandRegister (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN UINT8 Data\r
+ )\r
+{\r
+ IoWrite8 (ConsoleIn->CommandRegisterAddress, Data);\r
+}\r
+\r
+/**\r
+ Display error message.\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param ErrMsg Unicode string of error message\r
+\r
+**/\r
+VOID\r
+KeyboardError (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN CHAR16 *ErrMsg\r
+ )\r
+{\r
+ ConsoleIn->KeyboardErr = TRUE;\r
+}\r
+\r
+/**\r
+ Timer event handler: read a series of scancodes from 8042\r
+ and put them into memory scancode buffer.\r
+ it read as much scancodes to either fill\r
+ 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
+\r
+**/\r
+VOID\r
+EFIAPI\r
+KeyboardTimerHandler (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+\r
+{\r
+ UINT8 Data;\r
+ EFI_TPL OldTpl;\r
+ KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;\r
+\r
+ ConsoleIn = (KEYBOARD_CONSOLE_IN_DEV *) Context;\r
+\r
+ //\r
+ // Enter critical section\r
+ //\r
+ OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+ if (((KEYBOARD_CONSOLE_IN_DEV *) Context)->KeyboardErr) {\r
+ //\r
+ // Leave critical section and return\r
+ //\r
+ 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
+ // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since\r
+ // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.\r
+ // Just skip the 'resend' process simply.\r
+ //\r
+\r
+ while ((KeyReadStatusRegister (ConsoleIn) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA)) ==\r
+ KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA\r
+ ) {\r
+ //\r
+ // Read one byte of the scan code and store it into the memory buffer\r
+ //\r
+ Data = KeyReadDataRegister (ConsoleIn);\r
+ PushScancodeBufTail (&ConsoleIn->ScancodeQueue, Data);\r
+ }\r
+ KeyGetchar (ConsoleIn);\r
+\r
+ //\r
+ // Leave critical section and return\r
+ //\r
+ gBS->RestoreTPL (OldTpl);\r
+}\r
+\r
+/**\r
+ Read key value .\r
+\r
+ @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Data - Pointer to outof buffer for keeping key value\r
+\r
+ @retval EFI_TIMEOUT Status resigter time out\r
+ @retval EFI_SUCCESS Success to read keyboard\r
+\r
+**/\r
+EFI_STATUS\r
+KeyboardRead (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ OUT UINT8 *Data\r
+ )\r
+\r
+{\r
+ UINT32 TimeOut;\r
+ UINT32 RegFilled;\r
+\r
+ TimeOut = 0;\r
+ RegFilled = 0;\r
+\r
+ //\r
+ // wait till output buffer full then perform the read\r
+ //\r
+ for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
+ if (KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) {\r
+ RegFilled = 1;\r
+ *Data = KeyReadDataRegister (ConsoleIn);\r
+ break;\r
+ }\r
+\r
+ MicroSecondDelay (30);\r
+ }\r
+\r
+ if (RegFilled == 0) {\r
+ return EFI_TIMEOUT;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ write key to keyboard\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Data value wanted to be written\r
+\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
+KeyboardWrite (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN UINT8 Data\r
+ )\r
+{\r
+ UINT32 TimeOut;\r
+ UINT32 RegEmptied;\r
+\r
+ TimeOut = 0;\r
+ RegEmptied = 0;\r
+\r
+ //\r
+ // wait for input buffer empty\r
+ //\r
+ for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
+ if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
+ RegEmptied = 1;\r
+ break;\r
+ }\r
+\r
+ MicroSecondDelay (30);\r
+ }\r
+\r
+ if (RegEmptied == 0) {\r
+ return EFI_TIMEOUT;\r
+ }\r
+ //\r
+ // Write it\r
+ //\r
+ KeyWriteDataRegister (ConsoleIn, Data);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Issue keyboard command.\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Data The buff holding the command\r
+\r
+ @retval EFI_TIMEOUT Keyboard is not ready to issuing\r
+ @retval EFI_SUCCESS Success to issue keyboard command\r
+\r
+**/\r
+EFI_STATUS\r
+KeyboardCommand (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN UINT8 Data\r
+ )\r
+{\r
+ UINT32 TimeOut;\r
+ UINT32 RegEmptied;\r
+\r
+ TimeOut = 0;\r
+ RegEmptied = 0;\r
+\r
+ //\r
+ // Wait For Input Buffer Empty\r
+ //\r
+ for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
+ if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
+ RegEmptied = 1;\r
+ break;\r
+ }\r
+\r
+ MicroSecondDelay (30);\r
+ }\r
+\r
+ if (RegEmptied == 0) {\r
+ return EFI_TIMEOUT;\r
+ }\r
+ //\r
+ // issue the command\r
+ //\r
+ KeyWriteCommandRegister (ConsoleIn, Data);\r
+\r
+ //\r
+ // Wait For Input Buffer Empty again\r
+ //\r
+ RegEmptied = 0;\r
+ for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
+ if ((KeyReadStatusRegister (ConsoleIn) & 0x02) == 0) {\r
+ RegEmptied = 1;\r
+ break;\r
+ }\r
+\r
+ MicroSecondDelay (30);\r
+ }\r
+\r
+ if (RegEmptied == 0) {\r
+ return EFI_TIMEOUT;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ wait for a specific value to be presented on\r
+ 8042 Data register by keyboard and then read it,\r
+ used in keyboard commands ack\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+ @param Value the value wanted to be waited.\r
+\r
+ @retval EFI_TIMEOUT Fail to get specific value in given time\r
+ @retval EFI_SUCCESS Success to get specific value in given time.\r
+\r
+**/\r
+EFI_STATUS\r
+KeyboardWaitForValue (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN UINT8 Value\r
+ )\r
+{\r
+ UINT8 Data;\r
+ UINT32 TimeOut;\r
+ UINT32 SumTimeOut;\r
+ UINT32 GotIt;\r
+\r
+ GotIt = 0;\r
+ TimeOut = 0;\r
+ SumTimeOut = 0;\r
+\r
+ //\r
+ // Make sure the initial value of 'Data' is different from 'Value'\r
+ //\r
+ Data = 0;\r
+ if (Data == Value) {\r
+ Data = 1;\r
+ }\r
+ //\r
+ // Read from 8042 (multiple times if needed)\r
+ // until the expected value appears\r
+ // use SumTimeOut to control the iteration\r
+ //\r
+ while (1) {\r
+ //\r
+ // Perform a read\r
+ //\r
+ for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {\r
+ if (KeyReadStatusRegister (ConsoleIn) & 0x01) {\r
+ Data = KeyReadDataRegister (ConsoleIn);\r
+ break;\r
+ }\r
+\r
+ MicroSecondDelay (30);\r
+ }\r
+\r
+ SumTimeOut += TimeOut;\r
+\r
+ if (Data == Value) {\r
+ GotIt = 1;\r
+ break;\r
+ }\r
+\r
+ if (SumTimeOut >= mWaitForValueTimeOut) {\r
+ break;\r
+ }\r
+ }\r
+ //\r
+ // Check results\r
+ //\r
+ if (GotIt == 1) {\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ return EFI_TIMEOUT;\r
+ }\r
+\r
+}\r
+\r
+/**\r
+ Show keyboard status lights according to\r
+ indicators in ConsoleIn.\r
+\r
+ @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV\r
+\r
+ @return status of updating keyboard register\r
+\r
+**/\r
+EFI_STATUS\r
+UpdateStatusLights (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT8 Command;\r
+\r
+ //\r
+ // Send keyboard command\r
+ //\r
+ Status = KeyboardWrite (ConsoleIn, 0xed);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+\r
+ //\r
+ // Light configuration\r
+ //\r
+ Command = 0;\r
+ if (ConsoleIn->CapsLock) {\r
+ Command |= 4;\r
+ }\r
+\r
+ if (ConsoleIn->NumLock) {\r
+ Command |= 2;\r
+ }\r
+\r
+ if (ConsoleIn->ScrollLock) {\r
+ Command |= 1;\r
+ }\r
+\r
+ Status = KeyboardWrite (ConsoleIn, Command);\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ KeyboardWaitForValue (ConsoleIn, 0xfa);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.\r
+\r
+ The function is always called in TPL_NOTIFY.\r
+\r
+ @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer\r
+\r
+**/\r
+VOID\r
+KeyGetchar (\r
+ IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT16 ScanCode;\r
+ BOOLEAN Extend0;\r
+ BOOLEAN Extend1;\r
+ UINTN Index;\r
+ EFI_KEY_DATA KeyData;\r
+ LIST_ENTRY *Link;\r
+ KEYBOARD_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
+ //\r
+ // 3 bytes most\r
+ //\r
+ UINT8 ScancodeArr[3];\r
+ UINT32 ScancodeArrPos;\r
+\r
+ //\r
+ // Check if there are enough bytes of scancode representing a single key\r
+ // available in the buffer\r
+ //\r
+ while (TRUE) {\r
+ Extend0 = FALSE;\r
+ Extend1 = FALSE;\r
+ ScancodeArrPos = 0;\r
+ Status = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+ if (EFI_ERROR (Status)) {\r
+ return ;\r
+ }\r
+\r
+ if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED0) {\r
+ //\r
+ // E0 to look ahead 2 bytes\r
+ //\r
+ Extend0 = TRUE;\r
+ ScancodeArrPos = 1;\r
+ Status = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+ if (EFI_ERROR (Status)) {\r
+ return ;\r
+ }\r
+ } else if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED1) {\r
+ //\r
+ // E1 to look ahead 3 bytes\r
+ //\r
+ Extend1 = TRUE;\r
+ ScancodeArrPos = 2;\r
+ Status = GetScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+ if (EFI_ERROR (Status)) {\r
+ return ;\r
+ }\r
+ }\r
+ //\r
+ // if we reach this position, scancodes for a key is in buffer now,pop them\r
+ //\r
+ Status = PopScancodeBufHead (&ConsoleIn->ScancodeQueue, ScancodeArrPos + 1, ScancodeArr);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ //\r
+ // store the last available byte, this byte of scancode will be checked\r
+ //\r
+ ScanCode = ScancodeArr[ScancodeArrPos];\r
+\r
+ if (!Extend1) {\r
+ //\r
+ // Check for special keys and update the driver state.\r
+ //\r
+ switch (ScanCode) {\r
+\r
+ case SCANCODE_CTRL_MAKE:\r
+ if (Extend0) {\r
+ ConsoleIn->RightCtrl = TRUE;\r
+ } else {\r
+ ConsoleIn->LeftCtrl = TRUE;\r
+ }\r
+ break;\r
+ case SCANCODE_CTRL_BREAK:\r
+ if (Extend0) {\r
+ ConsoleIn->RightCtrl = FALSE;\r
+ } else {\r
+ ConsoleIn->LeftCtrl = FALSE;\r
+ }\r
+ break;\r
+\r
+ case SCANCODE_ALT_MAKE:\r
+ if (Extend0) {\r
+ ConsoleIn->RightAlt = TRUE;\r
+ } else {\r
+ ConsoleIn->LeftAlt = TRUE;\r
+ }\r
+ break;\r
+ case SCANCODE_ALT_BREAK:\r
+ if (Extend0) {\r
+ ConsoleIn->RightAlt = FALSE;\r
+ } else {\r
+ ConsoleIn->LeftAlt = FALSE;\r
+ }\r
+ break;\r
+\r
+ case SCANCODE_LEFT_SHIFT_MAKE:\r
+ //\r
+ // To avoid recognize PRNT_SCRN key as a L_SHIFT key\r
+ // because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.\r
+ // If it the second byte of the PRNT_ScRN skip it.\r
+ //\r
+ if (!Extend0) {\r
+ ConsoleIn->LeftShift = TRUE;\r
+ break;\r
+ }\r
+ continue;\r
+\r
+ case SCANCODE_LEFT_SHIFT_BREAK:\r
+ if (!Extend0) {\r
+ ConsoleIn->LeftShift = FALSE;\r
+ }\r
+ break;\r
+\r
+ case SCANCODE_RIGHT_SHIFT_MAKE:\r
+ ConsoleIn->RightShift = TRUE;\r
+ break;\r
+ case SCANCODE_RIGHT_SHIFT_BREAK:\r
+ ConsoleIn->RightShift = FALSE;\r
+ break;\r
+\r
+ case SCANCODE_LEFT_LOGO_MAKE:\r
+ ConsoleIn->LeftLogo = TRUE;\r
+ break;\r
+ case SCANCODE_LEFT_LOGO_BREAK:\r
+ ConsoleIn->LeftLogo = FALSE;\r
+ break;\r
+\r
+ case SCANCODE_RIGHT_LOGO_MAKE:\r
+ ConsoleIn->RightLogo = TRUE;\r
+ break;\r
+ case SCANCODE_RIGHT_LOGO_BREAK:\r
+ ConsoleIn->RightLogo = FALSE;\r
+ break;\r
+\r
+ case SCANCODE_MENU_MAKE:\r
+ ConsoleIn->Menu = TRUE;\r
+ break;\r
+ case SCANCODE_MENU_BREAK:\r
+ ConsoleIn->Menu = FALSE;\r
+ break;\r
+\r
+ case SCANCODE_SYS_REQ_MAKE:\r
+ if (Extend0) {\r
+ ConsoleIn->SysReq = TRUE;\r
+ }\r
+ break;\r
+ case SCANCODE_SYS_REQ_BREAK:\r
+ if (Extend0) {\r
+ ConsoleIn->SysReq = FALSE;\r
+ }\r
+ break;\r
+\r
+ case SCANCODE_SYS_REQ_MAKE_WITH_ALT:\r
+ ConsoleIn->SysReq = TRUE;\r
+ break;\r
+ case SCANCODE_SYS_REQ_BREAK_WITH_ALT:\r
+ ConsoleIn->SysReq = FALSE;\r
+ break;\r
+\r
+ case SCANCODE_CAPS_LOCK_MAKE:\r
+ ConsoleIn->CapsLock = (BOOLEAN)!ConsoleIn->CapsLock;\r
+ UpdateStatusLights (ConsoleIn);\r
+ break;\r
+ case SCANCODE_NUM_LOCK_MAKE:\r
+ ConsoleIn->NumLock = (BOOLEAN)!ConsoleIn->NumLock;\r
+ UpdateStatusLights (ConsoleIn);\r
+ break;\r
+ case SCANCODE_SCROLL_LOCK_MAKE:\r
+ if (!Extend0) {\r
+ ConsoleIn->ScrollLock = (BOOLEAN)!ConsoleIn->ScrollLock;\r
+ UpdateStatusLights (ConsoleIn);\r
+ }\r
+ break;\r
+ }\r
+ }\r
+\r
+ //\r
+ // If this is above the valid range, ignore it\r
+ //\r
+ if (ScanCode >= SCANCODE_MAX_MAKE) {\r
+ continue;\r
+ } else {\r
+ break;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Handle Ctrl+Alt+Del hotkey\r
+ //\r
+ if ((ConsoleIn->LeftCtrl || ConsoleIn->RightCtrl) &&\r
+ (ConsoleIn->LeftAlt || ConsoleIn->RightAlt ) &&\r
+ ScanCode == SCANCODE_DELETE_MAKE\r
+ ) {\r
+ gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
+ }\r
+\r
+ //\r
+ // Save the Shift/Toggle state\r
+ //\r
+ KeyData.KeyState.KeyShiftState = (UINT32) (EFI_SHIFT_STATE_VALID\r
+ | (ConsoleIn->LeftCtrl ? EFI_LEFT_CONTROL_PRESSED : 0)\r
+ | (ConsoleIn->RightCtrl ? EFI_RIGHT_CONTROL_PRESSED : 0)\r
+ | (ConsoleIn->LeftAlt ? EFI_LEFT_ALT_PRESSED : 0)\r
+ | (ConsoleIn->RightAlt ? EFI_RIGHT_ALT_PRESSED : 0)\r
+ | (ConsoleIn->LeftShift ? EFI_LEFT_SHIFT_PRESSED : 0)\r
+ | (ConsoleIn->RightShift ? EFI_RIGHT_SHIFT_PRESSED : 0)\r
+ | (ConsoleIn->LeftLogo ? EFI_LEFT_LOGO_PRESSED : 0)\r
+ | (ConsoleIn->RightLogo ? EFI_RIGHT_LOGO_PRESSED : 0)\r
+ | (ConsoleIn->Menu ? EFI_MENU_KEY_PRESSED : 0)\r
+ | (ConsoleIn->SysReq ? EFI_SYS_REQ_PRESSED : 0)\r
+ );\r
+ KeyData.KeyState.KeyToggleState = (EFI_KEY_TOGGLE_STATE) (EFI_TOGGLE_STATE_VALID\r
+ | (ConsoleIn->CapsLock ? EFI_CAPS_LOCK_ACTIVE : 0)\r
+ | (ConsoleIn->NumLock ? EFI_NUM_LOCK_ACTIVE : 0)\r
+ | (ConsoleIn->ScrollLock ? EFI_SCROLL_LOCK_ACTIVE : 0)\r
+ | (ConsoleIn->IsSupportPartialKey ? EFI_KEY_STATE_EXPOSED : 0)\r
+ );\r
+\r
+ KeyData.Key.ScanCode = SCAN_NULL;\r
+ KeyData.Key.UnicodeChar = CHAR_NULL;\r
+\r
+ //\r
+ // Key Pad "/" shares the same scancode as that of "/" except Key Pad "/" has E0 prefix\r
+ //\r
+ if (Extend0 && ScanCode == 0x35) {\r
+ KeyData.Key.UnicodeChar = L'/';\r
+ KeyData.Key.ScanCode = SCAN_NULL;\r
+\r
+ //\r
+ // PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix\r
+ //\r
+ } else if (Extend1 && ScanCode == SCANCODE_NUM_LOCK_MAKE) {\r
+ KeyData.Key.UnicodeChar = CHAR_NULL;\r
+ KeyData.Key.ScanCode = SCAN_PAUSE;\r
+\r
+ //\r
+ // PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix\r
+ //\r
+ } else if (Extend0 && ScanCode == SCANCODE_SCROLL_LOCK_MAKE) {\r
+ KeyData.Key.UnicodeChar = CHAR_NULL;\r
+ KeyData.Key.ScanCode = SCAN_PAUSE;\r
+\r
+ //\r
+ // PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix\r
+ //\r
+ } else if (Extend0 && ScanCode == SCANCODE_SYS_REQ_MAKE) {\r
+ KeyData.Key.UnicodeChar = CHAR_NULL;\r
+ KeyData.Key.ScanCode = SCAN_NULL;\r
+\r
+ //\r
+ // Except the above special case, all others can be handled by convert table\r
+ //\r
+ } else {\r
+ for (Index = 0; ConvertKeyboardScanCodeToEfiKey[Index].ScanCode != TABLE_END; Index++) {\r
+ if (ScanCode == ConvertKeyboardScanCodeToEfiKey[Index].ScanCode) {\r
+ KeyData.Key.ScanCode = ConvertKeyboardScanCodeToEfiKey[Index].EfiScanCode;\r
+ KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;\r
+\r
+ if ((ConsoleIn->LeftShift || ConsoleIn->RightShift) &&\r
+ (ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar != ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar)) {\r
+ KeyData.Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;\r
+ //\r
+ // Need not return associated shift state if a class of printable characters that\r
+ // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'\r
+ //\r
+ KeyData.KeyState.KeyShiftState &= ~(EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED);\r
+ }\r
+ //\r
+ // alphabetic key is affected by CapsLock State\r
+ //\r
+ if (ConsoleIn->CapsLock) {\r
+ if (KeyData.Key.UnicodeChar >= L'a' && KeyData.Key.UnicodeChar <= L'z') {\r
+ KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'a' + L'A');\r
+ } else if (KeyData.Key.UnicodeChar >= L'A' && KeyData.Key.UnicodeChar <= L'Z') {\r
+ KeyData.Key.UnicodeChar = (UINT16) (KeyData.Key.UnicodeChar - L'A' + L'a');\r
+ }\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // distinguish numeric key pad keys' 'up symbol' and 'down symbol'\r
+ //\r
+ if (ScanCode >= 0x47 && ScanCode <= 0x53) {\r
+ if (ConsoleIn->NumLock && !(ConsoleIn->LeftShift || ConsoleIn->RightShift) && !Extend0) {\r
+ KeyData.Key.ScanCode = SCAN_NULL;\r
+ } else if (ScanCode != 0x4a && ScanCode != 0x4e) {\r
+ KeyData.Key.UnicodeChar = CHAR_NULL;\r
+ }\r
+ }\r
+\r
+ //\r
+ // If the key can not be converted then just return.\r
+ //\r
+ if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == CHAR_NULL) {\r
+ if (!ConsoleIn->IsSupportPartialKey) {\r
+ return ;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Invoke notification functions if exist\r
+ //\r
+ for (Link = GetFirstNode (&ConsoleIn->NotifyList); !IsNull (&ConsoleIn->NotifyList, Link); Link = GetNextNode (&ConsoleIn->NotifyList, Link)) {\r
+ CurrentNotify = CR (\r
+ Link,\r
+ KEYBOARD_CONSOLE_IN_EX_NOTIFY,\r
+ NotifyEntry,\r
+ KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
+ );\r
+ if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
+ CurrentNotify->KeyNotificationFn (&KeyData);\r
+ }\r
+ }\r
+\r
+ PushEfikeyBufTail (&ConsoleIn->EfiKeyQueue, &KeyData);\r
+}\r
+\r
+/**\r
+ Perform 8042 controller and keyboard Initialization.\r
+ If ExtendedVerification is TRUE, do additional test for\r
+ the keyboard interface\r
+\r
+ @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer\r
+ @param ExtendedVerification - indicates a thorough initialization\r
+\r
+ @retval EFI_DEVICE_ERROR Fail to init keyboard\r
+ @retval EFI_SUCCESS Success to init keyboard\r
+**/\r
+EFI_STATUS\r
+InitKeyboard (\r
+ IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,\r
+ IN BOOLEAN ExtendedVerification\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_STATUS Status1;\r
+ UINT8 CommandByte;\r
+ EFI_PS2_POLICY_PROTOCOL *Ps2Policy;\r
+ UINT32 TryTime;\r
+\r
+ Status = EFI_SUCCESS;\r
+ mEnableMouseInterface = TRUE;\r
+ TryTime = 0;\r
+\r
+ //\r
+ // Get Ps2 policy to set this\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
+ EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_CLEAR_BUFFER,\r
+ ConsoleIn->DevicePath\r
+ );\r
+\r
+ //\r
+ // Perform a read to cleanup the Status Register's\r
+ // output buffer full bits within MAX TRY times\r
+ //\r
+ if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA) != 0) {\r
+ while (!EFI_ERROR (Status) && TryTime < KEYBOARD_MAX_TRY) {\r
+ Status = KeyboardRead (ConsoleIn, &CommandByte);\r
+ TryTime ++;\r
+ }\r
+ //\r
+ // Exceed the max try times. The device may be error.\r
+ //\r
+ if (TryTime == KEYBOARD_MAX_TRY) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+ }\r
+ //\r
+ // We should disable mouse interface during the initialization process\r
+ // since mouse device output could block keyboard device output in the\r
+ // 60H port of 8042 controller.\r
+ //\r
+ // So if we are not initializing 8042 controller for the\r
+ // first time, we have to remember the previous mouse interface\r
+ // enabling state\r
+ //\r
+ // Test the system flag in to determine whether this is the first\r
+ // time initialization\r
+ //\r
+ if ((KeyReadStatusRegister (ConsoleIn) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG) != 0) {\r
+ if (!PcdGetBool (PcdFastPS2Detection)) {\r
+ //\r
+ // 8042 controller is already setup (by myself or by mouse driver):\r
+ // See whether mouse interface is already enabled\r
+ // which determines whether we should enable it later\r
+ //\r
+ //\r
+ // Read the command byte of 8042 controller\r
+ //\r
+ Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_READ);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"\n\r");\r
+ goto Done;\r
+ }\r
+\r
+ Status = KeyboardRead (ConsoleIn, &CommandByte);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"\n\r");\r
+ goto Done;\r
+ }\r
+ //\r
+ // Test the mouse enabling bit\r
+ //\r
+ if ((CommandByte & 0x20) != 0) {\r
+ mEnableMouseInterface = FALSE;\r
+ } else {\r
+ mEnableMouseInterface = TRUE;\r
+ }\r
+ } else {\r
+ mEnableMouseInterface = FALSE;\r
+ }\r
+ } else {\r
+ //\r
+ // 8042 controller is not setup yet:\r
+ // 8042 controller selftest;\r
+ // Don't enable mouse interface later.\r
+ //\r
+ //\r
+ // Disable keyboard and mouse interfaces\r
+ //\r
+ if (!PcdGetBool (PcdFastPS2Detection)) {\r
+ Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"\n\r");\r
+ goto Done;\r
+ }\r
+\r
+ Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"\n\r");\r
+ goto Done;\r
+ }\r
+\r
+ REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+ EFI_PROGRESS_CODE,\r
+ EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST,\r
+ ConsoleIn->DevicePath\r
+ );\r
+ //\r
+ // 8042 Controller Self Test\r
+ //\r
+ Status = KeyboardCommand (ConsoleIn, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");\r
+ goto Done;\r
+ }\r
+\r
+ Status = KeyboardWaitForValue (ConsoleIn, 0x55);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"8042 controller self test failed!\n\r");\r
+ goto Done;\r
+ }\r
+ }\r
+ //\r
+ // Don't enable mouse interface later\r
+ //\r
+ mEnableMouseInterface = FALSE;\r
+\r
+ }\r
+\r
+ if (Ps2Policy != NULL) {\r
+ Ps2Policy->Ps2InitHardware (ConsoleIn->Handle);\r
+ }\r
+ //\r
+ // Write 8042 Command Byte, set System Flag\r
+ // While at the same time:\r
+ // 1. disable mouse interface,\r
+ // 2. enable kbd interface,\r
+ // 3. enable PC/XT kbd translation mode\r
+ // 4. enable mouse and kbd interrupts\r
+ //\r
+ // ( Command Byte bits:\r
+ // 7: Reserved\r
+ // 6: PC/XT translation mode\r
+ // 5: Disable Auxiliary device interface\r
+ // 4: Disable keyboard interface\r
+ // 3: Reserved\r
+ // 2: System Flag\r
+ // 1: Enable Auxiliary device interrupt\r
+ // 0: Enable Keyboard interrupt )\r
+ //\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
+ }\r
+\r
+ Status = KeyboardWrite (ConsoleIn, 0x67);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Clear Memory Scancode Buffer\r
+ //\r
+ ConsoleIn->ScancodeQueue.Head = 0;\r
+ ConsoleIn->ScancodeQueue.Tail = 0;\r
+ ConsoleIn->EfiKeyQueue.Head = 0;\r
+ ConsoleIn->EfiKeyQueue.Tail = 0;\r
+\r
+ //\r
+ // Reset the status indicators\r
+ //\r
+ ConsoleIn->CapsLock = FALSE;\r
+ ConsoleIn->NumLock = FALSE;\r
+ ConsoleIn->ScrollLock = FALSE;\r
+ ConsoleIn->LeftCtrl = FALSE;\r
+ ConsoleIn->RightCtrl = FALSE;\r
+ ConsoleIn->LeftAlt = FALSE;\r
+ ConsoleIn->RightAlt = FALSE;\r
+ ConsoleIn->LeftShift = FALSE;\r
+ ConsoleIn->RightShift = FALSE;\r
+ ConsoleIn->LeftLogo = FALSE;\r
+ ConsoleIn->RightLogo = FALSE;\r
+ ConsoleIn->Menu = FALSE;\r
+ ConsoleIn->SysReq = FALSE;\r
+\r
+ ConsoleIn->IsSupportPartialKey = FALSE;\r
+ //\r
+ // For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,\r
+ // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected\r
+ // to system. So we only do the real reseting for keyboard when user asks and there is a real KB connected t system,\r
+ // and normally during booting an OS, it's skipped.\r
+ //\r
+ if (ExtendedVerification && CheckKeyboardConnect (ConsoleIn)) {\r
+ //\r
+ // Additional verifications for keyboard interface\r
+ //\r
+ //\r
+ // Keyboard Interface Test\r
+ //\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
+ }\r
+\r
+ Status = KeyboardWaitForValue (ConsoleIn, 0x00);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (\r
+ ConsoleIn,\r
+ L"Some specific value not aquired from 8042 controller!\n\r"\r
+ );\r
+ goto Done;\r
+ }\r
+ //\r
+ // Keyboard reset with a BAT(Basic Assurance Test)\r
+ //\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, 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
+ }\r
+ //\r
+ // wait for BAT completion code\r
+ //\r
+ mWaitForValueTimeOut = KEYBOARD_BAT_TIMEOUT;\r
+\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
+ }\r
+\r
+ mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
+\r
+ //\r
+ // Set Keyboard to use Scan Code Set 2\r
+ //\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, 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
+ }\r
+\r
+ Status = KeyboardWrite (ConsoleIn, 0x02);\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, 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
+ }\r
+\r
+ //\r
+ // Clear Keyboard Scancode Buffer\r
+ //\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, 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
+ }\r
+ //\r
+ if (Ps2Policy != NULL) {\r
+ if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_CAPSLOCK) == EFI_KEYBOARD_CAPSLOCK) {\r
+ ConsoleIn->CapsLock = TRUE;\r
+ }\r
+\r
+ if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_NUMLOCK) == EFI_KEYBOARD_NUMLOCK) {\r
+ ConsoleIn->NumLock = TRUE;\r
+ }\r
+\r
+ if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_SCROLLLOCK) == EFI_KEYBOARD_SCROLLLOCK) {\r
+ ConsoleIn->ScrollLock = TRUE;\r
+ }\r
+ }\r
+ //\r
+ // Update Keyboard Lights\r
+ //\r
+ Status = UpdateStatusLights (ConsoleIn);\r
+ if (EFI_ERROR (Status)) {\r
+ KeyboardError (ConsoleIn, L"Update keyboard status lights error!\n\r");\r
+ goto Done;\r
+ }\r
+ }\r
+ //\r
+ // At last, we can now enable the mouse interface if appropriate\r
+ //\r
+Done:\r
+\r
+ if (mEnableMouseInterface) {\r
+ //\r
+ // Enable mouse interface\r
+ //\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
+ }\r
+ }\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+}\r
+\r
+/**\r
+ Disable the keyboard interface of the 8042 controller.\r
+\r
+ @param ConsoleIn The device instance\r
+\r
+ @return status of issuing disable command\r
+\r
+**/\r
+EFI_STATUS\r
+DisableKeyboard (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Disable keyboard interface\r
+ //\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
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command\r
+ If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device\r
+ should not be in system.\r
+\r
+ @param[in] ConsoleIn Keyboard Private Data Structure\r
+\r
+ @retval TRUE Keyboard in System.\r
+ @retval FALSE Keyboard not in System.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+CheckKeyboardConnect (\r
+ IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN WaitForValueTimeOutBcakup;\r
+\r
+ //\r
+ // enable keyboard itself and wait for its ack\r
+ // If can't receive ack, Keyboard should not be connected.\r
+ //\r
+ if (!PcdGetBool (PcdFastPS2Detection)) {\r
+ Status = KeyboardWrite (\r
+ ConsoleIn,\r
+ KEYBOARD_KBEN\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+ //\r
+ // wait for 1s\r
+ //\r
+ WaitForValueTimeOutBcakup = mWaitForValueTimeOut;\r
+ mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;\r
+ Status = KeyboardWaitForValue (\r
+ ConsoleIn,\r
+ KEYBOARD_CMDECHO_ACK\r
+ );\r
+ mWaitForValueTimeOut = WaitForValueTimeOutBcakup;\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return FALSE;\r
+ }\r
+\r
+ return TRUE;\r
+ } else {\r
+ return TRUE;\r
+ }\r
+}\r
+\r