2 Routines that access 8042 keyboard controller
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Ps2Keyboard.h"
18 UINT8 ScanCode
; ///< follows value defined in Scan Code Set1
21 CHAR16 ShiftUnicodeChar
;
23 ConvertKeyboardScanCodeToEfiKey
[] = {
350 0x37, // Numeric Keypad *
356 0x38, //Left Alt/Extended Right Alt
476 0x4c, // Numeric Keypad 5
562 // The WaitForValue time out
564 UINTN mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
566 BOOLEAN mEnableMouseInterface
;
571 Return the count of scancode in the queue.
573 @param Queue Pointer to instance of SCAN_CODE_QUEUE.
575 @return Count of the scancode.
578 GetScancodeBufCount (
579 IN SCAN_CODE_QUEUE
*Queue
582 if (Queue
->Head
<= Queue
->Tail
) {
583 return Queue
->Tail
- Queue
->Head
;
585 return Queue
->Tail
+ KEYBOARD_SCAN_CODE_MAX_COUNT
- Queue
->Head
;
590 Read several bytes from the scancode buffer without removing them.
591 This function is called to see if there are enough bytes of scancode
592 representing a single key.
594 @param Queue Pointer to instance of SCAN_CODE_QUEUE.
595 @param Count Number of bytes to be read
596 @param Buf Store the results
598 @retval EFI_SUCCESS success to scan the keyboard code
599 @retval EFI_NOT_READY invalid parameter
603 IN SCAN_CODE_QUEUE
*Queue
,
612 // check the valid range of parameter 'Count'
614 if (GetScancodeBufCount (Queue
) < Count
) {
615 return EFI_NOT_READY
;
618 // retrieve the values
620 for (Index
= 0, Pos
= Queue
->Head
; Index
< Count
; Index
++, Pos
= (Pos
+ 1) % KEYBOARD_SCAN_CODE_MAX_COUNT
) {
621 Buf
[Index
] = Queue
->Buffer
[Pos
];
629 Read & remove several bytes from the scancode buffer.
630 This function is usually called after GetScancodeBufHead()
632 @param Queue Pointer to instance of SCAN_CODE_QUEUE.
633 @param Count Number of bytes to be read
634 @param Buf Store the results
636 @retval EFI_SUCCESS success to scan the keyboard code
637 @retval EFI_NOT_READY invalid parameter
641 IN SCAN_CODE_QUEUE
*Queue
,
643 OUT UINT8
*Buf OPTIONAL
649 // Check the valid range of parameter 'Count'
651 if (GetScancodeBufCount (Queue
) < Count
) {
652 return EFI_NOT_READY
;
655 // Retrieve and remove the values
657 for (Index
= 0; Index
< Count
; Index
++, Queue
->Head
= (Queue
->Head
+ 1) % KEYBOARD_SCAN_CODE_MAX_COUNT
) {
659 Buf
[Index
] = Queue
->Buffer
[Queue
->Head
];
667 Push one byte to the scancode buffer.
669 @param Queue Pointer to instance of SCAN_CODE_QUEUE.
670 @param Scancode The byte to push.
673 PushScancodeBufTail (
674 IN SCAN_CODE_QUEUE
*Queue
,
678 if (GetScancodeBufCount (Queue
) == KEYBOARD_SCAN_CODE_MAX_COUNT
- 1) {
679 PopScancodeBufHead (Queue
, 1, NULL
);
682 Queue
->Buffer
[Queue
->Tail
] = Scancode
;
683 Queue
->Tail
= (Queue
->Tail
+ 1) % KEYBOARD_SCAN_CODE_MAX_COUNT
;
689 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
691 @return return the value
695 KeyReadDataRegister (
696 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
700 return IoRead8 (ConsoleIn
->DataRegisterAddress
);
706 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
707 @param Data value wanted to be written
711 KeyWriteDataRegister (
712 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
716 IoWrite8 (ConsoleIn
->DataRegisterAddress
, Data
);
720 Read status register.
722 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
724 @return value in status register
728 KeyReadStatusRegister (
729 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
732 return IoRead8 (ConsoleIn
->StatusRegisterAddress
);
736 Write command register .
738 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
739 @param Data The value wanted to be written
743 KeyWriteCommandRegister (
744 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
748 IoWrite8 (ConsoleIn
->CommandRegisterAddress
, Data
);
752 Display error message.
754 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
755 @param ErrMsg Unicode string of error message
760 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
764 ConsoleIn
->KeyboardErr
= TRUE
;
768 Timer event handler: read a series of scancodes from 8042
769 and put them into memory scancode buffer.
770 it read as much scancodes to either fill
771 the memory buffer or empty the keyboard buffer.
772 It is registered as running under TPL_NOTIFY
774 @param Event The timer event
775 @param Context A KEYBOARD_CONSOLE_IN_DEV pointer
780 KeyboardTimerHandler (
788 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
790 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
793 // Enter critical section
795 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
797 if (((KEYBOARD_CONSOLE_IN_DEV
*) Context
)->KeyboardErr
) {
799 // Leave critical section and return
801 gBS
->RestoreTPL (OldTpl
);
806 // To let KB driver support Hot plug, here should skip the 'resend' command for the case that
807 // KB is not connected to system. If KB is not connected to system, driver will find there's something
808 // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since
809 // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.
810 // Just skip the 'resend' process simply.
813 while ((KeyReadStatusRegister (ConsoleIn
) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT
|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
)) ==
814 KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
817 // Read one byte of the scan code and store it into the memory buffer
819 Data
= KeyReadDataRegister (ConsoleIn
);
820 PushScancodeBufTail (&ConsoleIn
->ScancodeQueue
, Data
);
822 KeyGetchar (ConsoleIn
);
825 // Leave critical section and return
827 gBS
->RestoreTPL (OldTpl
);
833 @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
834 @param Data - Pointer to outof buffer for keeping key value
836 @retval EFI_TIMEOUT Status resigter time out
837 @retval EFI_SUCCESS Success to read keyboard
842 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
854 // wait till output buffer full then perform the read
856 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
857 if (KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
) {
859 *Data
= KeyReadDataRegister (ConsoleIn
);
863 MicroSecondDelay (30);
866 if (RegFilled
== 0) {
874 write key to keyboard
876 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
877 @param Data value wanted to be written
879 @retval EFI_TIMEOUT The input buffer register is full for putting new value util timeout
880 @retval EFI_SUCCESS The new value is sucess put into input buffer register.
885 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
896 // wait for input buffer empty
898 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
899 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
904 MicroSecondDelay (30);
907 if (RegEmptied
== 0) {
913 KeyWriteDataRegister (ConsoleIn
, Data
);
919 Issue keyboard command.
921 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
922 @param Data The buff holding the command
924 @retval EFI_TIMEOUT Keyboard is not ready to issuing
925 @retval EFI_SUCCESS Success to issue keyboard command
930 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
941 // Wait For Input Buffer Empty
943 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
944 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
949 MicroSecondDelay (30);
952 if (RegEmptied
== 0) {
958 KeyWriteCommandRegister (ConsoleIn
, Data
);
961 // Wait For Input Buffer Empty again
964 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
965 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
970 MicroSecondDelay (30);
973 if (RegEmptied
== 0) {
981 wait for a specific value to be presented on
982 8042 Data register by keyboard and then read it,
983 used in keyboard commands ack
985 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
986 @param Value the value wanted to be waited.
988 @retval EFI_TIMEOUT Fail to get specific value in given time
989 @retval EFI_SUCCESS Success to get specific value in given time.
993 KeyboardWaitForValue (
994 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1008 // Make sure the initial value of 'Data' is different from 'Value'
1011 if (Data
== Value
) {
1015 // Read from 8042 (multiple times if needed)
1016 // until the expected value appears
1017 // use SumTimeOut to control the iteration
1023 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
1024 if (KeyReadStatusRegister (ConsoleIn
) & 0x01) {
1025 Data
= KeyReadDataRegister (ConsoleIn
);
1029 MicroSecondDelay (30);
1032 SumTimeOut
+= TimeOut
;
1034 if (Data
== Value
) {
1039 if (SumTimeOut
>= mWaitForValueTimeOut
) {
1055 Show keyboard status lights according to
1056 indicators in ConsoleIn.
1058 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1060 @return status of updating keyboard register
1064 UpdateStatusLights (
1065 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1072 // Send keyboard command
1074 Status
= KeyboardWrite (ConsoleIn
, 0xed);
1075 if (EFI_ERROR (Status
)) {
1079 KeyboardWaitForValue (ConsoleIn
, 0xfa);
1082 // Light configuration
1085 if (ConsoleIn
->CapsLock
) {
1089 if (ConsoleIn
->NumLock
) {
1093 if (ConsoleIn
->ScrollLock
) {
1097 Status
= KeyboardWrite (ConsoleIn
, Command
);
1099 if (EFI_ERROR (Status
)) {
1103 KeyboardWaitForValue (ConsoleIn
, 0xfa);
1108 Initialize the key state.
1110 @param ConsoleIn The KEYBOARD_CONSOLE_IN_DEV instance.
1111 @param KeyState A pointer to receive the key state information.
1114 InitializeKeyState (
1115 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1116 OUT EFI_KEY_STATE
*KeyState
1119 KeyState
->KeyShiftState
= EFI_SHIFT_STATE_VALID
1120 | (ConsoleIn
->LeftCtrl
? EFI_LEFT_CONTROL_PRESSED
: 0)
1121 | (ConsoleIn
->RightCtrl
? EFI_RIGHT_CONTROL_PRESSED
: 0)
1122 | (ConsoleIn
->LeftAlt
? EFI_LEFT_ALT_PRESSED
: 0)
1123 | (ConsoleIn
->RightAlt
? EFI_RIGHT_ALT_PRESSED
: 0)
1124 | (ConsoleIn
->LeftShift
? EFI_LEFT_SHIFT_PRESSED
: 0)
1125 | (ConsoleIn
->RightShift
? EFI_RIGHT_SHIFT_PRESSED
: 0)
1126 | (ConsoleIn
->LeftLogo
? EFI_LEFT_LOGO_PRESSED
: 0)
1127 | (ConsoleIn
->RightLogo
? EFI_RIGHT_LOGO_PRESSED
: 0)
1128 | (ConsoleIn
->Menu
? EFI_MENU_KEY_PRESSED
: 0)
1129 | (ConsoleIn
->SysReq
? EFI_SYS_REQ_PRESSED
: 0)
1131 KeyState
->KeyToggleState
= EFI_TOGGLE_STATE_VALID
1132 | (ConsoleIn
->CapsLock
? EFI_CAPS_LOCK_ACTIVE
: 0)
1133 | (ConsoleIn
->NumLock
? EFI_NUM_LOCK_ACTIVE
: 0)
1134 | (ConsoleIn
->ScrollLock
? EFI_SCROLL_LOCK_ACTIVE
: 0)
1135 | (ConsoleIn
->IsSupportPartialKey
? EFI_KEY_STATE_EXPOSED
: 0)
1140 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
1142 The function is always called in TPL_NOTIFY.
1144 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
1149 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1157 EFI_KEY_DATA KeyData
;
1159 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
1163 UINT8 ScancodeArr
[3];
1164 UINT32 ScancodeArrPos
;
1167 // Check if there are enough bytes of scancode representing a single key
1168 // available in the buffer
1174 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1175 if (EFI_ERROR (Status
)) {
1179 if (ScancodeArr
[ScancodeArrPos
] == SCANCODE_EXTENDED0
) {
1181 // E0 to look ahead 2 bytes
1185 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1186 if (EFI_ERROR (Status
)) {
1189 } else if (ScancodeArr
[ScancodeArrPos
] == SCANCODE_EXTENDED1
) {
1191 // E1 to look ahead 3 bytes
1195 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1196 if (EFI_ERROR (Status
)) {
1201 // if we reach this position, scancodes for a key is in buffer now,pop them
1203 Status
= PopScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1204 ASSERT_EFI_ERROR (Status
);
1207 // store the last available byte, this byte of scancode will be checked
1209 ScanCode
= ScancodeArr
[ScancodeArrPos
];
1213 // Check for special keys and update the driver state.
1217 case SCANCODE_CTRL_MAKE
:
1219 ConsoleIn
->RightCtrl
= TRUE
;
1221 ConsoleIn
->LeftCtrl
= TRUE
;
1224 case SCANCODE_CTRL_BREAK
:
1226 ConsoleIn
->RightCtrl
= FALSE
;
1228 ConsoleIn
->LeftCtrl
= FALSE
;
1232 case SCANCODE_ALT_MAKE
:
1234 ConsoleIn
->RightAlt
= TRUE
;
1236 ConsoleIn
->LeftAlt
= TRUE
;
1239 case SCANCODE_ALT_BREAK
:
1241 ConsoleIn
->RightAlt
= FALSE
;
1243 ConsoleIn
->LeftAlt
= FALSE
;
1247 case SCANCODE_LEFT_SHIFT_MAKE
:
1249 // To avoid recognize PRNT_SCRN key as a L_SHIFT key
1250 // because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.
1251 // If it the second byte of the PRNT_ScRN skip it.
1254 ConsoleIn
->LeftShift
= TRUE
;
1259 case SCANCODE_LEFT_SHIFT_BREAK
:
1261 ConsoleIn
->LeftShift
= FALSE
;
1265 case SCANCODE_RIGHT_SHIFT_MAKE
:
1266 ConsoleIn
->RightShift
= TRUE
;
1268 case SCANCODE_RIGHT_SHIFT_BREAK
:
1269 ConsoleIn
->RightShift
= FALSE
;
1272 case SCANCODE_LEFT_LOGO_MAKE
:
1273 ConsoleIn
->LeftLogo
= TRUE
;
1275 case SCANCODE_LEFT_LOGO_BREAK
:
1276 ConsoleIn
->LeftLogo
= FALSE
;
1279 case SCANCODE_RIGHT_LOGO_MAKE
:
1280 ConsoleIn
->RightLogo
= TRUE
;
1282 case SCANCODE_RIGHT_LOGO_BREAK
:
1283 ConsoleIn
->RightLogo
= FALSE
;
1286 case SCANCODE_MENU_MAKE
:
1287 ConsoleIn
->Menu
= TRUE
;
1289 case SCANCODE_MENU_BREAK
:
1290 ConsoleIn
->Menu
= FALSE
;
1293 case SCANCODE_SYS_REQ_MAKE
:
1295 ConsoleIn
->SysReq
= TRUE
;
1298 case SCANCODE_SYS_REQ_BREAK
:
1300 ConsoleIn
->SysReq
= FALSE
;
1304 case SCANCODE_SYS_REQ_MAKE_WITH_ALT
:
1305 ConsoleIn
->SysReq
= TRUE
;
1307 case SCANCODE_SYS_REQ_BREAK_WITH_ALT
:
1308 ConsoleIn
->SysReq
= FALSE
;
1311 case SCANCODE_CAPS_LOCK_MAKE
:
1312 ConsoleIn
->CapsLock
= (BOOLEAN
)!ConsoleIn
->CapsLock
;
1313 UpdateStatusLights (ConsoleIn
);
1315 case SCANCODE_NUM_LOCK_MAKE
:
1316 ConsoleIn
->NumLock
= (BOOLEAN
)!ConsoleIn
->NumLock
;
1317 UpdateStatusLights (ConsoleIn
);
1319 case SCANCODE_SCROLL_LOCK_MAKE
:
1321 ConsoleIn
->ScrollLock
= (BOOLEAN
)!ConsoleIn
->ScrollLock
;
1322 UpdateStatusLights (ConsoleIn
);
1329 // If this is above the valid range, ignore it
1331 if (ScanCode
>= SCANCODE_MAX_MAKE
) {
1339 // Handle Ctrl+Alt+Del hotkey
1341 if ((ConsoleIn
->LeftCtrl
|| ConsoleIn
->RightCtrl
) &&
1342 (ConsoleIn
->LeftAlt
|| ConsoleIn
->RightAlt
) &&
1343 ScanCode
== SCANCODE_DELETE_MAKE
1345 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1349 // Save the Shift/Toggle state
1351 InitializeKeyState (ConsoleIn
, &KeyData
.KeyState
);
1352 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1353 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1356 // Key Pad "/" shares the same scancode as that of "/" except Key Pad "/" has E0 prefix
1358 if (Extend0
&& ScanCode
== 0x35) {
1359 KeyData
.Key
.UnicodeChar
= L
'/';
1360 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1363 // PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix
1365 } else if (Extend1
&& ScanCode
== SCANCODE_NUM_LOCK_MAKE
) {
1366 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1367 KeyData
.Key
.ScanCode
= SCAN_PAUSE
;
1370 // PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix
1372 } else if (Extend0
&& ScanCode
== SCANCODE_SCROLL_LOCK_MAKE
) {
1373 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1374 KeyData
.Key
.ScanCode
= SCAN_PAUSE
;
1377 // PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix
1379 } else if (Extend0
&& ScanCode
== SCANCODE_SYS_REQ_MAKE
) {
1380 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1381 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1384 // Except the above special case, all others can be handled by convert table
1387 for (Index
= 0; ConvertKeyboardScanCodeToEfiKey
[Index
].ScanCode
!= TABLE_END
; Index
++) {
1388 if (ScanCode
== ConvertKeyboardScanCodeToEfiKey
[Index
].ScanCode
) {
1389 KeyData
.Key
.ScanCode
= ConvertKeyboardScanCodeToEfiKey
[Index
].EfiScanCode
;
1390 KeyData
.Key
.UnicodeChar
= ConvertKeyboardScanCodeToEfiKey
[Index
].UnicodeChar
;
1392 if ((ConsoleIn
->LeftShift
|| ConsoleIn
->RightShift
) &&
1393 (ConvertKeyboardScanCodeToEfiKey
[Index
].UnicodeChar
!= ConvertKeyboardScanCodeToEfiKey
[Index
].ShiftUnicodeChar
)) {
1394 KeyData
.Key
.UnicodeChar
= ConvertKeyboardScanCodeToEfiKey
[Index
].ShiftUnicodeChar
;
1396 // Need not return associated shift state if a class of printable characters that
1397 // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'
1399 KeyData
.KeyState
.KeyShiftState
&= ~(EFI_LEFT_SHIFT_PRESSED
| EFI_RIGHT_SHIFT_PRESSED
);
1402 // alphabetic key is affected by CapsLock State
1404 if (ConsoleIn
->CapsLock
) {
1405 if (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z') {
1406 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'a' + L
'A');
1407 } else if (KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') {
1408 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'A' + L
'a');
1417 // distinguish numeric key pad keys' 'up symbol' and 'down symbol'
1419 if (ScanCode
>= 0x47 && ScanCode
<= 0x53) {
1420 if (ConsoleIn
->NumLock
&& !(ConsoleIn
->LeftShift
|| ConsoleIn
->RightShift
) && !Extend0
) {
1421 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1422 } else if (ScanCode
!= 0x4a && ScanCode
!= 0x4e) {
1423 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1428 // If the key can not be converted then just return.
1430 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
1431 if (!ConsoleIn
->IsSupportPartialKey
) {
1437 // Signal KeyNotify process event if this key pressed matches any key registered.
1439 for (Link
= GetFirstNode (&ConsoleIn
->NotifyList
); !IsNull (&ConsoleIn
->NotifyList
, Link
); Link
= GetNextNode (&ConsoleIn
->NotifyList
, Link
)) {
1440 CurrentNotify
= CR (
1442 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1444 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1446 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
1448 // The key notification function needs to run at TPL_CALLBACK
1449 // while current TPL is TPL_NOTIFY. It will be invoked in
1450 // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.
1452 PushEfikeyBufTail (&ConsoleIn
->EfiKeyQueueForNotify
, &KeyData
);
1453 gBS
->SignalEvent (ConsoleIn
->KeyNotifyProcessEvent
);
1457 PushEfikeyBufTail (&ConsoleIn
->EfiKeyQueue
, &KeyData
);
1461 Perform 8042 controller and keyboard Initialization.
1462 If ExtendedVerification is TRUE, do additional test for
1463 the keyboard interface
1465 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
1466 @param ExtendedVerification - indicates a thorough initialization
1468 @retval EFI_DEVICE_ERROR Fail to init keyboard
1469 @retval EFI_SUCCESS Success to init keyboard
1473 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1474 IN BOOLEAN ExtendedVerification
1480 EFI_PS2_POLICY_PROTOCOL
*Ps2Policy
;
1483 Status
= EFI_SUCCESS
;
1484 mEnableMouseInterface
= TRUE
;
1488 // Get Ps2 policy to set this
1490 gBS
->LocateProtocol (
1491 &gEfiPs2PolicyProtocolGuid
,
1493 (VOID
**) &Ps2Policy
1496 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1498 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_CLEAR_BUFFER
,
1499 ConsoleIn
->DevicePath
1503 // Perform a read to cleanup the Status Register's
1504 // output buffer full bits within MAX TRY times
1506 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
) != 0) {
1507 while (!EFI_ERROR (Status
) && TryTime
< KEYBOARD_MAX_TRY
) {
1508 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1512 // Exceed the max try times. The device may be error.
1514 if (TryTime
== KEYBOARD_MAX_TRY
) {
1515 Status
= EFI_DEVICE_ERROR
;
1520 // We should disable mouse interface during the initialization process
1521 // since mouse device output could block keyboard device output in the
1522 // 60H port of 8042 controller.
1524 // So if we are not initializing 8042 controller for the
1525 // first time, we have to remember the previous mouse interface
1528 // Test the system flag in to determine whether this is the first
1529 // time initialization
1531 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG
) != 0) {
1532 if (!PcdGetBool (PcdFastPS2Detection
)) {
1534 // 8042 controller is already setup (by myself or by mouse driver):
1535 // See whether mouse interface is already enabled
1536 // which determines whether we should enable it later
1539 // Read the command byte of 8042 controller
1541 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_READ
);
1542 if (EFI_ERROR (Status
)) {
1543 KeyboardError (ConsoleIn
, L
"\n\r");
1547 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1548 if (EFI_ERROR (Status
)) {
1549 KeyboardError (ConsoleIn
, L
"\n\r");
1553 // Test the mouse enabling bit
1555 if ((CommandByte
& 0x20) != 0) {
1556 mEnableMouseInterface
= FALSE
;
1558 mEnableMouseInterface
= TRUE
;
1561 mEnableMouseInterface
= FALSE
;
1565 // 8042 controller is not setup yet:
1566 // 8042 controller selftest;
1567 // Don't enable mouse interface later.
1570 // Disable keyboard and mouse interfaces
1572 if (!PcdGetBool (PcdFastPS2Detection
)) {
1573 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE
);
1574 if (EFI_ERROR (Status
)) {
1575 KeyboardError (ConsoleIn
, L
"\n\r");
1579 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE
);
1580 if (EFI_ERROR (Status
)) {
1581 KeyboardError (ConsoleIn
, L
"\n\r");
1585 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1587 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_SELF_TEST
,
1588 ConsoleIn
->DevicePath
1591 // 8042 Controller Self Test
1593 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST
);
1594 if (EFI_ERROR (Status
)) {
1595 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1599 Status
= KeyboardWaitForValue (ConsoleIn
, 0x55);
1600 if (EFI_ERROR (Status
)) {
1601 KeyboardError (ConsoleIn
, L
"8042 controller self test failed!\n\r");
1606 // Don't enable mouse interface later
1608 mEnableMouseInterface
= FALSE
;
1612 if (Ps2Policy
!= NULL
) {
1613 Ps2Policy
->Ps2InitHardware (ConsoleIn
->Handle
);
1616 // Write 8042 Command Byte, set System Flag
1617 // While at the same time:
1618 // 1. disable mouse interface,
1619 // 2. enable kbd interface,
1620 // 3. enable PC/XT kbd translation mode
1621 // 4. enable mouse and kbd interrupts
1623 // ( Command Byte bits:
1625 // 6: PC/XT translation mode
1626 // 5: Disable Auxiliary device interface
1627 // 4: Disable keyboard interface
1630 // 1: Enable Auxiliary device interrupt
1631 // 0: Enable Keyboard interrupt )
1633 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_WRITE
);
1634 if (EFI_ERROR (Status
)) {
1635 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1639 Status
= KeyboardWrite (ConsoleIn
, 0x67);
1640 if (EFI_ERROR (Status
)) {
1641 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1646 // Clear Memory Scancode Buffer
1648 ConsoleIn
->ScancodeQueue
.Head
= 0;
1649 ConsoleIn
->ScancodeQueue
.Tail
= 0;
1650 ConsoleIn
->EfiKeyQueue
.Head
= 0;
1651 ConsoleIn
->EfiKeyQueue
.Tail
= 0;
1652 ConsoleIn
->EfiKeyQueueForNotify
.Head
= 0;
1653 ConsoleIn
->EfiKeyQueueForNotify
.Tail
= 0;
1656 // Reset the status indicators
1658 ConsoleIn
->CapsLock
= FALSE
;
1659 ConsoleIn
->NumLock
= FALSE
;
1660 ConsoleIn
->ScrollLock
= FALSE
;
1661 ConsoleIn
->LeftCtrl
= FALSE
;
1662 ConsoleIn
->RightCtrl
= FALSE
;
1663 ConsoleIn
->LeftAlt
= FALSE
;
1664 ConsoleIn
->RightAlt
= FALSE
;
1665 ConsoleIn
->LeftShift
= FALSE
;
1666 ConsoleIn
->RightShift
= FALSE
;
1667 ConsoleIn
->LeftLogo
= FALSE
;
1668 ConsoleIn
->RightLogo
= FALSE
;
1669 ConsoleIn
->Menu
= FALSE
;
1670 ConsoleIn
->SysReq
= FALSE
;
1672 ConsoleIn
->IsSupportPartialKey
= FALSE
;
1674 // For resetting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
1675 // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected
1676 // to system. So we only do the real resetting for keyboard when user asks and there is a real KB connected t system,
1677 // and normally during booting an OS, it's skipped.
1679 if (ExtendedVerification
&& CheckKeyboardConnect (ConsoleIn
)) {
1681 // Additional verifications for keyboard interface
1684 // Keyboard Interface Test
1686 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST
);
1687 if (EFI_ERROR (Status
)) {
1688 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1692 Status
= KeyboardWaitForValue (ConsoleIn
, 0x00);
1693 if (EFI_ERROR (Status
)) {
1696 L
"Some specific value not aquired from 8042 controller!\n\r"
1701 // Keyboard reset with a BAT(Basic Assurance Test)
1703 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_RESET
);
1704 if (EFI_ERROR (Status
)) {
1705 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1709 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1710 if (EFI_ERROR (Status
)) {
1711 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1715 // wait for BAT completion code
1717 mWaitForValueTimeOut
= KEYBOARD_BAT_TIMEOUT
;
1719 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS
);
1720 if (EFI_ERROR (Status
)) {
1721 KeyboardError (ConsoleIn
, L
"Keyboard self test failed!\n\r");
1725 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1728 // Set Keyboard to use Scan Code Set 2
1730 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET
);
1731 if (EFI_ERROR (Status
)) {
1732 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1736 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1737 if (EFI_ERROR (Status
)) {
1738 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1742 Status
= KeyboardWrite (ConsoleIn
, 0x02);
1743 if (EFI_ERROR (Status
)) {
1744 KeyboardError (ConsoleIn
, L
"8042 controller data write error!!\n\r");
1748 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1749 if (EFI_ERROR (Status
)) {
1750 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1755 // Clear Keyboard Scancode Buffer
1757 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA
);
1758 if (EFI_ERROR (Status
)) {
1759 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1763 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1764 if (EFI_ERROR (Status
)) {
1765 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1769 if (Ps2Policy
!= NULL
) {
1770 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_CAPSLOCK
) == EFI_KEYBOARD_CAPSLOCK
) {
1771 ConsoleIn
->CapsLock
= TRUE
;
1774 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_NUMLOCK
) == EFI_KEYBOARD_NUMLOCK
) {
1775 ConsoleIn
->NumLock
= TRUE
;
1778 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_SCROLLLOCK
) == EFI_KEYBOARD_SCROLLLOCK
) {
1779 ConsoleIn
->ScrollLock
= TRUE
;
1783 // Update Keyboard Lights
1785 Status
= UpdateStatusLights (ConsoleIn
);
1786 if (EFI_ERROR (Status
)) {
1787 KeyboardError (ConsoleIn
, L
"Update keyboard status lights error!\n\r");
1792 // At last, we can now enable the mouse interface if appropriate
1796 if (mEnableMouseInterface
) {
1798 // Enable mouse interface
1800 Status1
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE
);
1801 if (EFI_ERROR (Status1
)) {
1802 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1803 return EFI_DEVICE_ERROR
;
1807 if (!EFI_ERROR (Status
)) {
1810 return EFI_DEVICE_ERROR
;
1817 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
1818 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
1819 should not be in system.
1821 @param[in] ConsoleIn Keyboard Private Data Structure
1823 @retval TRUE Keyboard in System.
1824 @retval FALSE Keyboard not in System.
1828 CheckKeyboardConnect (
1829 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1833 UINTN WaitForValueTimeOutBcakup
;
1836 // enable keyboard itself and wait for its ack
1837 // If can't receive ack, Keyboard should not be connected.
1839 if (!PcdGetBool (PcdFastPS2Detection
)) {
1840 Status
= KeyboardWrite (
1845 if (EFI_ERROR (Status
)) {
1851 WaitForValueTimeOutBcakup
= mWaitForValueTimeOut
;
1852 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1853 Status
= KeyboardWaitForValue (
1855 KEYBOARD_CMDECHO_ACK
1857 mWaitForValueTimeOut
= WaitForValueTimeOutBcakup
;
1859 if (EFI_ERROR (Status
)) {