2 Routines that access 8042 keyboard controller
4 Copyright (c) 2006 - 2011, 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 EFI_ISA_IO_PROTOCOL
*IsaIo
;
704 // Use IsaIo protocol to perform IO operations
706 IsaIo
= ConsoleIn
->IsaIo
;
711 ConsoleIn
->DataRegisterAddress
,
722 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
723 @param Data value wanted to be written
727 KeyWriteDataRegister (
728 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
732 ConsoleIn
->IsaIo
->Io
.Write (
735 ConsoleIn
->DataRegisterAddress
,
742 Read status register.
744 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
746 @return value in status register
750 KeyReadStatusRegister (
751 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
755 ConsoleIn
->IsaIo
->Io
.Read (
758 ConsoleIn
->StatusRegisterAddress
,
766 Write command register .
768 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
769 @param Data The value wanted to be written
773 KeyWriteCommandRegister (
774 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
778 ConsoleIn
->IsaIo
->Io
.Write (
781 ConsoleIn
->CommandRegisterAddress
,
788 Display error message.
790 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
791 @param ErrMsg Unicode string of error message
796 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
800 ConsoleIn
->KeyboardErr
= TRUE
;
804 Timer event handler: read a series of scancodes from 8042
805 and put them into memory scancode buffer.
806 it read as much scancodes to either fill
807 the memory buffer or empty the keyboard buffer.
808 It is registered as running under TPL_NOTIFY
810 @param Event The timer event
811 @param Context A KEYBOARD_CONSOLE_IN_DEV pointer
816 KeyboardTimerHandler (
824 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
826 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
829 // Enter critical section
831 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
833 if (((KEYBOARD_CONSOLE_IN_DEV
*) Context
)->KeyboardErr
) {
835 // Leave critical section and return
837 gBS
->RestoreTPL (OldTpl
);
842 // To let KB driver support Hot plug, here should skip the 'resend' command for the case that
843 // KB is not connected to system. If KB is not connected to system, driver will find there's something
844 // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since
845 // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.
846 // Just skip the 'resend' process simply.
849 while ((KeyReadStatusRegister (ConsoleIn
) & (KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT
|KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
)) ==
850 KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
853 // Read one byte of the scan code and store it into the memory buffer
855 Data
= KeyReadDataRegister (ConsoleIn
);
856 PushScancodeBufTail (&ConsoleIn
->ScancodeQueue
, Data
);
858 KeyGetchar (ConsoleIn
);
861 // Leave critical section and return
863 gBS
->RestoreTPL (OldTpl
);
869 @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
870 @param Data - Pointer to outof buffer for keeping key value
872 @retval EFI_TIMEOUT Status resigter time out
873 @retval EFI_SUCCESS Success to read keyboard
878 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
890 // wait till output buffer full then perform the read
892 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
893 if (KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
) {
895 *Data
= KeyReadDataRegister (ConsoleIn
);
899 MicroSecondDelay (30);
902 if (RegFilled
== 0) {
910 write key to keyboard
912 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
913 @param Data value wanted to be written
915 @retval EFI_TIMEOUT The input buffer register is full for putting new value util timeout
916 @retval EFI_SUCCESS The new value is sucess put into input buffer register.
921 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
932 // wait for input buffer empty
934 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
935 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
940 MicroSecondDelay (30);
943 if (RegEmptied
== 0) {
949 KeyWriteDataRegister (ConsoleIn
, Data
);
955 Issue keyboard command.
957 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
958 @param Data The buff holding the command
960 @retval EFI_TIMEOUT Keyboard is not ready to issuing
961 @retval EFI_SUCCESS Success to issue keyboard command
966 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
977 // Wait For Input Buffer Empty
979 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
980 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
985 MicroSecondDelay (30);
988 if (RegEmptied
== 0) {
994 KeyWriteCommandRegister (ConsoleIn
, Data
);
997 // Wait For Input Buffer Empty again
1000 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
1001 if ((KeyReadStatusRegister (ConsoleIn
) & 0x02) == 0) {
1006 MicroSecondDelay (30);
1009 if (RegEmptied
== 0) {
1017 wait for a specific value to be presented on
1018 8042 Data register by keyboard and then read it,
1019 used in keyboard commands ack
1021 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1022 @param Value the value wanted to be waited.
1024 @retval EFI_TIMEOUT Fail to get specific value in given time
1025 @retval EFI_SUCCESS Success to get specific value in given time.
1029 KeyboardWaitForValue (
1030 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1044 // Make sure the initial value of 'Data' is different from 'Value'
1047 if (Data
== Value
) {
1051 // Read from 8042 (multiple times if needed)
1052 // until the expected value appears
1053 // use SumTimeOut to control the iteration
1059 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
1060 if (KeyReadStatusRegister (ConsoleIn
) & 0x01) {
1061 Data
= KeyReadDataRegister (ConsoleIn
);
1065 MicroSecondDelay (30);
1068 SumTimeOut
+= TimeOut
;
1070 if (Data
== Value
) {
1075 if (SumTimeOut
>= mWaitForValueTimeOut
) {
1091 Show keyboard status lights according to
1092 indicators in ConsoleIn.
1094 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1096 @return status of updating keyboard register
1100 UpdateStatusLights (
1101 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1108 // Send keyboard command
1110 Status
= KeyboardWrite (ConsoleIn
, 0xed);
1111 if (EFI_ERROR (Status
)) {
1115 KeyboardWaitForValue (ConsoleIn
, 0xfa);
1118 // Light configuration
1121 if (ConsoleIn
->CapsLock
) {
1125 if (ConsoleIn
->NumLock
) {
1129 if (ConsoleIn
->ScrollLock
) {
1133 Status
= KeyboardWrite (ConsoleIn
, Command
);
1135 if (EFI_ERROR (Status
)) {
1139 KeyboardWaitForValue (ConsoleIn
, 0xfa);
1144 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
1146 The function is always called in TPL_NOTIFY.
1148 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
1153 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1161 EFI_KEY_DATA KeyData
;
1163 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
1167 UINT8 ScancodeArr
[3];
1168 UINT32 ScancodeArrPos
;
1171 // Check if there are enough bytes of scancode representing a single key
1172 // available in the buffer
1178 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1179 if (EFI_ERROR (Status
)) {
1183 if (ScancodeArr
[ScancodeArrPos
] == SCANCODE_EXTENDED0
) {
1185 // E0 to look ahead 2 bytes
1189 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1190 if (EFI_ERROR (Status
)) {
1193 } else if (ScancodeArr
[ScancodeArrPos
] == SCANCODE_EXTENDED1
) {
1195 // E1 to look ahead 3 bytes
1199 Status
= GetScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1200 if (EFI_ERROR (Status
)) {
1205 // if we reach this position, scancodes for a key is in buffer now,pop them
1207 Status
= PopScancodeBufHead (&ConsoleIn
->ScancodeQueue
, ScancodeArrPos
+ 1, ScancodeArr
);
1208 ASSERT_EFI_ERROR (Status
);
1211 // store the last available byte, this byte of scancode will be checked
1213 ScanCode
= ScancodeArr
[ScancodeArrPos
];
1217 // Check for special keys and update the driver state.
1221 case SCANCODE_CTRL_MAKE
:
1223 ConsoleIn
->RightCtrl
= TRUE
;
1225 ConsoleIn
->LeftCtrl
= TRUE
;
1228 case SCANCODE_CTRL_BREAK
:
1230 ConsoleIn
->RightCtrl
= FALSE
;
1232 ConsoleIn
->LeftCtrl
= FALSE
;
1236 case SCANCODE_ALT_MAKE
:
1238 ConsoleIn
->RightAlt
= TRUE
;
1240 ConsoleIn
->LeftAlt
= TRUE
;
1243 case SCANCODE_ALT_BREAK
:
1245 ConsoleIn
->RightAlt
= FALSE
;
1247 ConsoleIn
->LeftAlt
= FALSE
;
1251 case SCANCODE_LEFT_SHIFT_MAKE
:
1253 // To avoid recognize PRNT_SCRN key as a L_SHIFT key
1254 // because PRNT_SCRN key generates E0 followed by L_SHIFT scan code.
1255 // If it the second byte of the PRNT_ScRN skip it.
1258 ConsoleIn
->LeftShift
= TRUE
;
1263 case SCANCODE_LEFT_SHIFT_BREAK
:
1265 ConsoleIn
->LeftShift
= FALSE
;
1269 case SCANCODE_RIGHT_SHIFT_MAKE
:
1270 ConsoleIn
->RightShift
= TRUE
;
1272 case SCANCODE_RIGHT_SHIFT_BREAK
:
1273 ConsoleIn
->RightShift
= FALSE
;
1276 case SCANCODE_LEFT_LOGO_MAKE
:
1277 ConsoleIn
->LeftLogo
= TRUE
;
1279 case SCANCODE_LEFT_LOGO_BREAK
:
1280 ConsoleIn
->LeftLogo
= FALSE
;
1283 case SCANCODE_RIGHT_LOGO_MAKE
:
1284 ConsoleIn
->RightLogo
= TRUE
;
1286 case SCANCODE_RIGHT_LOGO_BREAK
:
1287 ConsoleIn
->RightLogo
= FALSE
;
1290 case SCANCODE_MENU_MAKE
:
1291 ConsoleIn
->Menu
= TRUE
;
1293 case SCANCODE_MENU_BREAK
:
1294 ConsoleIn
->Menu
= FALSE
;
1297 case SCANCODE_SYS_REQ_MAKE
:
1299 ConsoleIn
->SysReq
= TRUE
;
1302 case SCANCODE_SYS_REQ_BREAK
:
1304 ConsoleIn
->SysReq
= FALSE
;
1308 case SCANCODE_SYS_REQ_MAKE_WITH_ALT
:
1309 ConsoleIn
->SysReq
= TRUE
;
1311 case SCANCODE_SYS_REQ_BREAK_WITH_ALT
:
1312 ConsoleIn
->SysReq
= FALSE
;
1315 case SCANCODE_CAPS_LOCK_MAKE
:
1316 ConsoleIn
->CapsLock
= (BOOLEAN
)!ConsoleIn
->CapsLock
;
1317 UpdateStatusLights (ConsoleIn
);
1319 case SCANCODE_NUM_LOCK_MAKE
:
1320 ConsoleIn
->NumLock
= (BOOLEAN
)!ConsoleIn
->NumLock
;
1321 UpdateStatusLights (ConsoleIn
);
1323 case SCANCODE_SCROLL_LOCK_MAKE
:
1325 ConsoleIn
->ScrollLock
= (BOOLEAN
)!ConsoleIn
->ScrollLock
;
1326 UpdateStatusLights (ConsoleIn
);
1333 // If this is above the valid range, ignore it
1335 if (ScanCode
>= SCANCODE_MAX_MAKE
) {
1343 // Handle Ctrl+Alt+Del hotkey
1345 if ((ConsoleIn
->LeftCtrl
|| ConsoleIn
->RightCtrl
) &&
1346 (ConsoleIn
->LeftAlt
|| ConsoleIn
->RightAlt
) &&
1347 ScanCode
== SCANCODE_DELETE_MAKE
1349 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1353 // Save the Shift/Toggle state
1355 KeyData
.KeyState
.KeyShiftState
= (UINT32
) (EFI_SHIFT_STATE_VALID
1356 | (ConsoleIn
->LeftCtrl
? EFI_LEFT_CONTROL_PRESSED
: 0)
1357 | (ConsoleIn
->RightCtrl
? EFI_RIGHT_CONTROL_PRESSED
: 0)
1358 | (ConsoleIn
->LeftAlt
? EFI_LEFT_ALT_PRESSED
: 0)
1359 | (ConsoleIn
->RightAlt
? EFI_RIGHT_ALT_PRESSED
: 0)
1360 | (ConsoleIn
->LeftShift
? EFI_LEFT_SHIFT_PRESSED
: 0)
1361 | (ConsoleIn
->RightShift
? EFI_RIGHT_SHIFT_PRESSED
: 0)
1362 | (ConsoleIn
->LeftLogo
? EFI_LEFT_LOGO_PRESSED
: 0)
1363 | (ConsoleIn
->RightLogo
? EFI_RIGHT_LOGO_PRESSED
: 0)
1364 | (ConsoleIn
->Menu
? EFI_MENU_KEY_PRESSED
: 0)
1365 | (ConsoleIn
->SysReq
? EFI_SYS_REQ_PRESSED
: 0)
1367 KeyData
.KeyState
.KeyToggleState
= (EFI_KEY_TOGGLE_STATE
) (EFI_TOGGLE_STATE_VALID
1368 | (ConsoleIn
->CapsLock
? EFI_CAPS_LOCK_ACTIVE
: 0)
1369 | (ConsoleIn
->NumLock
? EFI_NUM_LOCK_ACTIVE
: 0)
1370 | (ConsoleIn
->ScrollLock
? EFI_SCROLL_LOCK_ACTIVE
: 0)
1371 | (ConsoleIn
->IsSupportPartialKey
? EFI_KEY_STATE_EXPOSED
: 0)
1374 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1375 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1378 // Key Pad "/" shares the same scancode as that of "/" except Key Pad "/" has E0 prefix
1380 if (Extend0
&& ScanCode
== 0x35) {
1381 KeyData
.Key
.UnicodeChar
= L
'/';
1382 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1385 // PAUSE shares the same scancode as that of NUM except PAUSE has E1 prefix
1387 } else if (Extend1
&& ScanCode
== SCANCODE_NUM_LOCK_MAKE
) {
1388 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1389 KeyData
.Key
.ScanCode
= SCAN_PAUSE
;
1392 // PAUSE shares the same scancode as that of SCROLL except PAUSE (CTRL pressed) has E0 prefix
1394 } else if (Extend0
&& ScanCode
== SCANCODE_SCROLL_LOCK_MAKE
) {
1395 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1396 KeyData
.Key
.ScanCode
= SCAN_PAUSE
;
1399 // PRNT_SCRN shares the same scancode as that of Key Pad "*" except PRNT_SCRN has E0 prefix
1401 } else if (Extend0
&& ScanCode
== SCANCODE_SYS_REQ_MAKE
) {
1402 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1403 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1406 // Except the above special case, all others can be handled by convert table
1409 for (Index
= 0; ConvertKeyboardScanCodeToEfiKey
[Index
].ScanCode
!= TABLE_END
; Index
++) {
1410 if (ScanCode
== ConvertKeyboardScanCodeToEfiKey
[Index
].ScanCode
) {
1411 KeyData
.Key
.ScanCode
= ConvertKeyboardScanCodeToEfiKey
[Index
].EfiScanCode
;
1412 KeyData
.Key
.UnicodeChar
= ConvertKeyboardScanCodeToEfiKey
[Index
].UnicodeChar
;
1414 if ((ConsoleIn
->LeftShift
|| ConsoleIn
->RightShift
) &&
1415 (ConvertKeyboardScanCodeToEfiKey
[Index
].UnicodeChar
!= ConvertKeyboardScanCodeToEfiKey
[Index
].ShiftUnicodeChar
)) {
1416 KeyData
.Key
.UnicodeChar
= ConvertKeyboardScanCodeToEfiKey
[Index
].ShiftUnicodeChar
;
1418 // Need not return associated shift state if a class of printable characters that
1419 // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'
1421 KeyData
.KeyState
.KeyShiftState
&= ~(EFI_LEFT_SHIFT_PRESSED
| EFI_RIGHT_SHIFT_PRESSED
);
1424 // alphabetic key is affected by CapsLock State
1426 if (ConsoleIn
->CapsLock
) {
1427 if (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z') {
1428 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'a' + L
'A');
1429 } else if (KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') {
1430 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'A' + L
'a');
1439 // distinguish numeric key pad keys' 'up symbol' and 'down symbol'
1441 if (ScanCode
>= 0x47 && ScanCode
<= 0x53) {
1442 if (ConsoleIn
->NumLock
&& !(ConsoleIn
->LeftShift
|| ConsoleIn
->RightShift
) && !Extend0
) {
1443 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1444 } else if (ScanCode
!= 0x4a && ScanCode
!= 0x4e) {
1445 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1450 // If the key can not be converted then just return.
1452 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
1453 if (!ConsoleIn
->IsSupportPartialKey
) {
1459 // Invoke notification functions if exist
1461 for (Link
= GetFirstNode (&ConsoleIn
->NotifyList
); !IsNull (&ConsoleIn
->NotifyList
, Link
); Link
= GetNextNode (&ConsoleIn
->NotifyList
, Link
)) {
1462 CurrentNotify
= CR (
1464 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1466 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1468 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
1469 CurrentNotify
->KeyNotificationFn (&KeyData
);
1473 PushEfikeyBufTail (&ConsoleIn
->EfiKeyQueue
, &KeyData
);
1477 Perform 8042 controller and keyboard Initialization.
1478 If ExtendedVerification is TRUE, do additional test for
1479 the keyboard interface
1481 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
1482 @param ExtendedVerification - indicates a thorough initialization
1484 @retval EFI_DEVICE_ERROR Fail to init keyboard
1485 @retval EFI_SUCCESS Success to init keyboard
1489 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
1490 IN BOOLEAN ExtendedVerification
1496 EFI_PS2_POLICY_PROTOCOL
*Ps2Policy
;
1499 Status
= EFI_SUCCESS
;
1500 mEnableMouseInterface
= TRUE
;
1504 // Get Ps2 policy to set this
1506 gBS
->LocateProtocol (
1507 &gEfiPs2PolicyProtocolGuid
,
1509 (VOID
**) &Ps2Policy
1512 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1514 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_CLEAR_BUFFER
,
1515 ConsoleIn
->DevicePath
1519 // Perform a read to cleanup the Status Register's
1520 // output buffer full bits within MAX TRY times
1522 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA
) != 0) {
1523 while (!EFI_ERROR (Status
) && TryTime
< KEYBOARD_MAX_TRY
) {
1524 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1528 // Exceed the max try times. The device may be error.
1530 if (TryTime
== KEYBOARD_MAX_TRY
) {
1531 Status
= EFI_DEVICE_ERROR
;
1536 // We should disable mouse interface during the initialization process
1537 // since mouse device output could block keyboard device output in the
1538 // 60H port of 8042 controller.
1540 // So if we are not initializing 8042 controller for the
1541 // first time, we have to remember the previous mouse interface
1544 // Test the system flag in to determine whether this is the first
1545 // time initialization
1547 if ((KeyReadStatusRegister (ConsoleIn
) & KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG
) != 0) {
1548 if (!PcdGetBool (PcdFastPS2Detection
)) {
1550 // 8042 controller is already setup (by myself or by mouse driver):
1551 // See whether mouse interface is already enabled
1552 // which determines whether we should enable it later
1555 // Read the command byte of 8042 controller
1557 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_READ
);
1558 if (EFI_ERROR (Status
)) {
1559 KeyboardError (ConsoleIn
, L
"\n\r");
1563 Status
= KeyboardRead (ConsoleIn
, &CommandByte
);
1564 if (EFI_ERROR (Status
)) {
1565 KeyboardError (ConsoleIn
, L
"\n\r");
1569 // Test the mouse enabling bit
1571 if ((CommandByte
& 0x20) != 0) {
1572 mEnableMouseInterface
= FALSE
;
1574 mEnableMouseInterface
= TRUE
;
1577 mEnableMouseInterface
= FALSE
;
1581 // 8042 controller is not setup yet:
1582 // 8042 controller selftest;
1583 // Don't enable mouse interface later.
1586 // Disable keyboard and mouse interfaces
1588 if (!PcdGetBool (PcdFastPS2Detection
)) {
1589 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE
);
1590 if (EFI_ERROR (Status
)) {
1591 KeyboardError (ConsoleIn
, L
"\n\r");
1595 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE
);
1596 if (EFI_ERROR (Status
)) {
1597 KeyboardError (ConsoleIn
, L
"\n\r");
1601 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1603 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_SELF_TEST
,
1604 ConsoleIn
->DevicePath
1607 // 8042 Controller Self Test
1609 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST
);
1610 if (EFI_ERROR (Status
)) {
1611 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1615 Status
= KeyboardWaitForValue (ConsoleIn
, 0x55);
1616 if (EFI_ERROR (Status
)) {
1617 KeyboardError (ConsoleIn
, L
"8042 controller self test failed!\n\r");
1622 // Don't enable mouse interface later
1624 mEnableMouseInterface
= FALSE
;
1628 if (Ps2Policy
!= NULL
) {
1629 Ps2Policy
->Ps2InitHardware (ConsoleIn
->Handle
);
1632 // Write 8042 Command Byte, set System Flag
1633 // While at the same time:
1634 // 1. disable mouse interface,
1635 // 2. enable kbd interface,
1636 // 3. enable PC/XT kbd translation mode
1637 // 4. enable mouse and kbd interrupts
1639 // ( Command Byte bits:
1641 // 6: PC/XT translation mode
1642 // 5: Disable Auxiliary device interface
1643 // 4: Disable keyboard interface
1646 // 1: Enable Auxiliary device interrupt
1647 // 0: Enable Keyboard interrupt )
1649 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_WRITE
);
1650 if (EFI_ERROR (Status
)) {
1651 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1655 Status
= KeyboardWrite (ConsoleIn
, 0x67);
1656 if (EFI_ERROR (Status
)) {
1657 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1662 // Clear Memory Scancode Buffer
1664 ConsoleIn
->ScancodeQueue
.Head
= 0;
1665 ConsoleIn
->ScancodeQueue
.Tail
= 0;
1666 ConsoleIn
->EfiKeyQueue
.Head
= 0;
1667 ConsoleIn
->EfiKeyQueue
.Tail
= 0;
1670 // Reset the status indicators
1672 ConsoleIn
->CapsLock
= FALSE
;
1673 ConsoleIn
->NumLock
= FALSE
;
1674 ConsoleIn
->ScrollLock
= FALSE
;
1675 ConsoleIn
->LeftCtrl
= FALSE
;
1676 ConsoleIn
->RightCtrl
= FALSE
;
1677 ConsoleIn
->LeftAlt
= FALSE
;
1678 ConsoleIn
->RightAlt
= FALSE
;
1679 ConsoleIn
->LeftShift
= FALSE
;
1680 ConsoleIn
->RightShift
= FALSE
;
1681 ConsoleIn
->LeftLogo
= FALSE
;
1682 ConsoleIn
->RightLogo
= FALSE
;
1683 ConsoleIn
->Menu
= FALSE
;
1684 ConsoleIn
->SysReq
= FALSE
;
1686 ConsoleIn
->IsSupportPartialKey
= FALSE
;
1688 // For resetting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
1689 // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected
1690 // to system. So we only do the real resetting for keyboard when user asks and there is a real KB connected t system,
1691 // and normally during booting an OS, it's skipped.
1693 if (ExtendedVerification
&& CheckKeyboardConnect (ConsoleIn
)) {
1695 // Additional verifications for keyboard interface
1698 // Keyboard Interface Test
1700 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST
);
1701 if (EFI_ERROR (Status
)) {
1702 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1706 Status
= KeyboardWaitForValue (ConsoleIn
, 0x00);
1707 if (EFI_ERROR (Status
)) {
1710 L
"Some specific value not aquired from 8042 controller!\n\r"
1715 // Keyboard reset with a BAT(Basic Assurance Test)
1717 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_RESET
);
1718 if (EFI_ERROR (Status
)) {
1719 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1723 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1724 if (EFI_ERROR (Status
)) {
1725 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1729 // wait for BAT completion code
1731 mWaitForValueTimeOut
= KEYBOARD_BAT_TIMEOUT
;
1733 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_BAT_SUCCESS
);
1734 if (EFI_ERROR (Status
)) {
1735 KeyboardError (ConsoleIn
, L
"Keyboard self test failed!\n\r");
1739 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1742 // Set Keyboard to use Scan Code Set 2
1744 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET
);
1745 if (EFI_ERROR (Status
)) {
1746 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1750 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1751 if (EFI_ERROR (Status
)) {
1752 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1756 Status
= KeyboardWrite (ConsoleIn
, 0x02);
1757 if (EFI_ERROR (Status
)) {
1758 KeyboardError (ConsoleIn
, L
"8042 controller data write error!!\n\r");
1762 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1763 if (EFI_ERROR (Status
)) {
1764 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1769 // Clear Keyboard Scancode Buffer
1771 Status
= KeyboardWrite (ConsoleIn
, KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA
);
1772 if (EFI_ERROR (Status
)) {
1773 KeyboardError (ConsoleIn
, L
"8042 controller data write error!\n\r");
1777 Status
= KeyboardWaitForValue (ConsoleIn
, KEYBOARD_8048_RETURN_8042_ACK
);
1778 if (EFI_ERROR (Status
)) {
1779 KeyboardError (ConsoleIn
, L
"Some specific value not aquired from 8042 controller!\n\r");
1783 if (Ps2Policy
!= NULL
) {
1784 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_CAPSLOCK
) == EFI_KEYBOARD_CAPSLOCK
) {
1785 ConsoleIn
->CapsLock
= TRUE
;
1788 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_NUMLOCK
) == EFI_KEYBOARD_NUMLOCK
) {
1789 ConsoleIn
->NumLock
= TRUE
;
1792 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_SCROLLLOCK
) == EFI_KEYBOARD_SCROLLLOCK
) {
1793 ConsoleIn
->ScrollLock
= TRUE
;
1797 // Update Keyboard Lights
1799 Status
= UpdateStatusLights (ConsoleIn
);
1800 if (EFI_ERROR (Status
)) {
1801 KeyboardError (ConsoleIn
, L
"Update keyboard status lights error!\n\r");
1806 // At last, we can now enable the mouse interface if appropriate
1810 if (mEnableMouseInterface
) {
1812 // Enable mouse interface
1814 Status1
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE
);
1815 if (EFI_ERROR (Status1
)) {
1816 KeyboardError (ConsoleIn
, L
"8042 controller command write error!\n\r");
1817 return EFI_DEVICE_ERROR
;
1821 if (!EFI_ERROR (Status
)) {
1824 return EFI_DEVICE_ERROR
;
1830 Disable the keyboard interface of the 8042 controller.
1832 @param ConsoleIn The device instance
1834 @return status of issuing disable command
1839 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1845 // Disable keyboard interface
1847 Status
= KeyboardCommand (ConsoleIn
, KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE
);
1848 if (EFI_ERROR (Status
)) {
1849 KeyboardError (ConsoleIn
, L
"\n\r");
1850 return EFI_DEVICE_ERROR
;
1857 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
1858 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
1859 should not be in system.
1861 @param[in] ConsoleIn Keyboard Private Data Structure
1863 @retval TRUE Keyboard in System.
1864 @retval FALSE Keyboard not in System.
1868 CheckKeyboardConnect (
1869 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
1873 UINTN WaitForValueTimeOutBcakup
;
1876 // enable keyboard itself and wait for its ack
1877 // If can't receive ack, Keyboard should not be connected.
1879 if (!PcdGetBool (PcdFastPS2Detection
)) {
1880 Status
= KeyboardWrite (
1885 if (EFI_ERROR (Status
)) {
1891 WaitForValueTimeOutBcakup
= mWaitForValueTimeOut
;
1892 mWaitForValueTimeOut
= KEYBOARD_WAITFORVALUE_TIMEOUT
;
1893 Status
= KeyboardWaitForValue (
1895 KEYBOARD_CMDECHO_ACK
1897 mWaitForValueTimeOut
= WaitForValueTimeOutBcakup
;
1899 if (EFI_ERROR (Status
)) {