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
);
1458 PushEfikeyBufTail (&ConsoleIn
->EfiKeyQueue
, &KeyData
);
1462 Perform 8042 controller and keyboard Initialization.
1463 If ExtendedVerification is TRUE, do additional test for
1464 the keyboard interface
1466 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
1467 @param ExtendedVerification - indicates a thorough initialization
1469 @retval EFI_DEVICE_ERROR Fail to init keyboard
1470 @retval EFI_SUCCESS Success to init keyboard
1474 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1475 IN BOOLEAN ExtendedVerification
1481 EFI_PS2_POLICY_PROTOCOL
*Ps2Policy
;
1484 Status
= EFI_SUCCESS
;
1485 mEnableMouseInterface
= TRUE
;
1489 // Get Ps2 policy to set this
1491 gBS
->LocateProtocol (
1492 &gEfiPs2PolicyProtocolGuid
,
1494 (VOID
**) &Ps2Policy
1497 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1499 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_CLEAR_BUFFER
,
1500 ConsoleIn
->DevicePath
1504 // Perform a read to cleanup the Status Register's
1505 // output buffer full bits within MAX TRY times
1507 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
) != 0) {
1508 while (!EFI_ERROR (Status
) && TryTime
< KEYBOARD_MAX_TRY
) {
1509 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1513 // Exceed the max try times. The device may be error.
1515 if (TryTime
== KEYBOARD_MAX_TRY
) {
1516 Status
= EFI_DEVICE_ERROR
;
1521 // We should disable mouse interface during the initialization process
1522 // since mouse device output could block keyboard device output in the
1523 // 60H port of 8042 controller.
1525 // So if we are not initializing 8042 controller for the
1526 // first time, we have to remember the previous mouse interface
1529 // Test the system flag in to determine whether this is the first
1530 // time initialization
1532 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG
) != 0) {
1533 if (!PcdGetBool (PcdFastPS2Detection
)) {
1535 // 8042 controller is already setup (by myself or by mouse driver):
1536 // See whether mouse interface is already enabled
1537 // which determines whether we should enable it later
1540 // Read the command byte of 8042 controller
1542 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_READ
);
1543 if (EFI_ERROR (Status
)) {
1544 KeyboardError (ConsoleIn
, L
"\n\r");
1548 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1549 if (EFI_ERROR (Status
)) {
1550 KeyboardError (ConsoleIn
, L
"\n\r");
1554 // Test the mouse enabling bit
1556 if ((CommandByte
& 0x20) != 0) {
1557 mEnableMouseInterface
= FALSE
;
1559 mEnableMouseInterface
= TRUE
;
1562 mEnableMouseInterface
= FALSE
;
1566 // 8042 controller is not setup yet:
1567 // 8042 controller selftest;
1568 // Don't enable mouse interface later.
1571 // Disable keyboard and mouse interfaces
1573 if (!PcdGetBool (PcdFastPS2Detection
)) {
1574 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE
);
1575 if (EFI_ERROR (Status
)) {
1576 KeyboardError (ConsoleIn
, L
"\n\r");
1580 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE
);
1581 if (EFI_ERROR (Status
)) {
1582 KeyboardError (ConsoleIn
, L
"\n\r");
1586 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1588 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_SELF_TEST
,
1589 ConsoleIn
->DevicePath
1592 // 8042 Controller Self Test
1594 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST
);
1595 if (EFI_ERROR (Status
)) {
1596 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1600 Status
= KeyboardWaitForValue (ConsoleIn
, 0x55);
1601 if (EFI_ERROR (Status
)) {
1602 KeyboardError (ConsoleIn
, L
"8042 controller self test failed!\n\r");
1607 // Don't enable mouse interface later
1609 mEnableMouseInterface
= FALSE
;
1613 if (Ps2Policy
!= NULL
) {
1614 Ps2Policy
->Ps2InitHardware (ConsoleIn
->Handle
);
1617 // Write 8042 Command Byte, set System Flag
1618 // While at the same time:
1619 // 1. disable mouse interface,
1620 // 2. enable kbd interface,
1621 // 3. enable PC/XT kbd translation mode
1622 // 4. enable mouse and kbd interrupts
1624 // ( Command Byte bits:
1626 // 6: PC/XT translation mode
1627 // 5: Disable Auxiliary device interface
1628 // 4: Disable keyboard interface
1631 // 1: Enable Auxiliary device interrupt
1632 // 0: Enable Keyboard interrupt )
1634 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_WRITE
);
1635 if (EFI_ERROR (Status
)) {
1636 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1640 Status
= KeyboardWrite (ConsoleIn
, 0x67);
1641 if (EFI_ERROR (Status
)) {
1642 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1647 // Clear Memory Scancode Buffer
1649 ConsoleIn
->ScancodeQueue
.Head
= 0;
1650 ConsoleIn
->ScancodeQueue
.Tail
= 0;
1651 ConsoleIn
->EfiKeyQueue
.Head
= 0;
1652 ConsoleIn
->EfiKeyQueue
.Tail
= 0;
1653 ConsoleIn
->EfiKeyQueueForNotify
.Head
= 0;
1654 ConsoleIn
->EfiKeyQueueForNotify
.Tail
= 0;
1657 // Reset the status indicators
1659 ConsoleIn
->CapsLock
= FALSE
;
1660 ConsoleIn
->NumLock
= FALSE
;
1661 ConsoleIn
->ScrollLock
= FALSE
;
1662 ConsoleIn
->LeftCtrl
= FALSE
;
1663 ConsoleIn
->RightCtrl
= FALSE
;
1664 ConsoleIn
->LeftAlt
= FALSE
;
1665 ConsoleIn
->RightAlt
= FALSE
;
1666 ConsoleIn
->LeftShift
= FALSE
;
1667 ConsoleIn
->RightShift
= FALSE
;
1668 ConsoleIn
->LeftLogo
= FALSE
;
1669 ConsoleIn
->RightLogo
= FALSE
;
1670 ConsoleIn
->Menu
= FALSE
;
1671 ConsoleIn
->SysReq
= FALSE
;
1673 ConsoleIn
->IsSupportPartialKey
= FALSE
;
1675 // For resetting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
1676 // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected
1677 // to system. So we only do the real resetting for keyboard when user asks and there is a real KB connected t system,
1678 // and normally during booting an OS, it's skipped.
1680 if (ExtendedVerification
&& CheckKeyboardConnect (ConsoleIn
)) {
1682 // Additional verifications for keyboard interface
1685 // Keyboard Interface Test
1687 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST
);
1688 if (EFI_ERROR (Status
)) {
1689 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1693 Status
= KeyboardWaitForValue (ConsoleIn
, 0x00);
1694 if (EFI_ERROR (Status
)) {
1697 L
"Some specific value not aquired from 8042 controller!\n\r"
1702 // Keyboard reset with a BAT(Basic Assurance Test)
1704 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_RESET
);
1705 if (EFI_ERROR (Status
)) {
1706 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1710 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1711 if (EFI_ERROR (Status
)) {
1712 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1716 // wait for BAT completion code
1718 mWaitForValueTimeOut
= KEYBOARD_BAT_TIMEOUT
;
1720 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS
);
1721 if (EFI_ERROR (Status
)) {
1722 KeyboardError (ConsoleIn
, L
"Keyboard self test failed!\n\r");
1726 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1729 // Set Keyboard to use Scan Code Set 2
1731 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET
);
1732 if (EFI_ERROR (Status
)) {
1733 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1737 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1738 if (EFI_ERROR (Status
)) {
1739 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1743 Status
= KeyboardWrite (ConsoleIn
, 0x02);
1744 if (EFI_ERROR (Status
)) {
1745 KeyboardError (ConsoleIn
, L
"8042 controller data write error!!\n\r");
1749 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1750 if (EFI_ERROR (Status
)) {
1751 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1756 // Clear Keyboard Scancode Buffer
1758 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA
);
1759 if (EFI_ERROR (Status
)) {
1760 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1764 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1765 if (EFI_ERROR (Status
)) {
1766 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1770 if (Ps2Policy
!= NULL
) {
1771 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_CAPSLOCK
) == EFI_KEYBOARD_CAPSLOCK
) {
1772 ConsoleIn
->CapsLock
= TRUE
;
1775 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_NUMLOCK
) == EFI_KEYBOARD_NUMLOCK
) {
1776 ConsoleIn
->NumLock
= TRUE
;
1779 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_SCROLLLOCK
) == EFI_KEYBOARD_SCROLLLOCK
) {
1780 ConsoleIn
->ScrollLock
= TRUE
;
1784 // Update Keyboard Lights
1786 Status
= UpdateStatusLights (ConsoleIn
);
1787 if (EFI_ERROR (Status
)) {
1788 KeyboardError (ConsoleIn
, L
"Update keyboard status lights error!\n\r");
1793 // At last, we can now enable the mouse interface if appropriate
1797 if (mEnableMouseInterface
) {
1799 // Enable mouse interface
1801 Status1
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE
);
1802 if (EFI_ERROR (Status1
)) {
1803 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1804 return EFI_DEVICE_ERROR
;
1808 if (!EFI_ERROR (Status
)) {
1811 return EFI_DEVICE_ERROR
;
1818 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
1819 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
1820 should not be in system.
1822 @param[in] ConsoleIn Keyboard Private Data Structure
1824 @retval TRUE Keyboard in System.
1825 @retval FALSE Keyboard not in System.
1829 CheckKeyboardConnect (
1830 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1834 UINTN WaitForValueTimeOutBcakup
;
1837 // enable keyboard itself and wait for its ack
1838 // If can't receive ack, Keyboard should not be connected.
1840 if (!PcdGetBool (PcdFastPS2Detection
)) {
1841 Status
= KeyboardWrite (
1846 if (EFI_ERROR (Status
)) {
1852 WaitForValueTimeOutBcakup
= mWaitForValueTimeOut
;
1853 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1854 Status
= KeyboardWaitForValue (
1856 KEYBOARD_CMDECHO_ACK
1858 mWaitForValueTimeOut
= WaitForValueTimeOutBcakup
;
1860 if (EFI_ERROR (Status
)) {