2 ConsoleOut Routines that speak VGA.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "BiosKeyboard.h"
20 // EFI Driver Binding Protocol Instance
22 EFI_DRIVER_BINDING_PROTOCOL gBiosKeyboardDriverBinding
= {
23 BiosKeyboardDriverBindingSupported
,
24 BiosKeyboardDriverBindingStart
,
25 BiosKeyboardDriverBindingStop
,
35 @param Queue The queue to be enqueued.
36 @param KeyData The key data to be enqueued.
38 @retval EFI_NOT_READY The queue is full.
39 @retval EFI_SUCCESS Successfully enqueued the key data.
44 IN SIMPLE_QUEUE
*Queue
,
45 IN EFI_KEY_DATA
*KeyData
48 if ((Queue
->Rear
+ 1) % QUEUE_MAX_COUNT
== Queue
->Front
) {
52 CopyMem (&Queue
->Buffer
[Queue
->Rear
], KeyData
, sizeof (EFI_KEY_DATA
));
53 Queue
->Rear
= (Queue
->Rear
+ 1) % QUEUE_MAX_COUNT
;
62 @param Queue The queue to be dequeued.
63 @param KeyData The key data to be dequeued.
65 @retval EFI_NOT_READY The queue is empty.
66 @retval EFI_SUCCESS Successfully dequeued the key data.
71 IN SIMPLE_QUEUE
*Queue
,
72 IN EFI_KEY_DATA
*KeyData
75 if (Queue
->Front
== Queue
->Rear
) {
79 CopyMem (KeyData
, &Queue
->Buffer
[Queue
->Front
], sizeof (EFI_KEY_DATA
));
80 Queue
->Front
= (Queue
->Front
+ 1) % QUEUE_MAX_COUNT
;
87 Check whether the queue is empty.
89 @param Queue The queue to be checked.
91 @retval EFI_NOT_READY The queue is empty.
92 @retval EFI_SUCCESS The queue is not empty.
97 IN SIMPLE_QUEUE
*Queue
100 if (Queue
->Front
== Queue
->Rear
) {
101 return EFI_NOT_READY
;
108 // EFI Driver Binding Protocol Functions
112 Check whether the driver supports this device.
114 @param This The Udriver binding protocol.
115 @param Controller The controller handle to check.
116 @param RemainingDevicePath The remaining device path.
118 @retval EFI_SUCCESS The driver supports this controller.
119 @retval other This device isn't supported.
124 BiosKeyboardDriverBindingSupported (
125 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
126 IN EFI_HANDLE Controller
,
127 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
131 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
132 EFI_ISA_IO_PROTOCOL
*IsaIo
;
135 // See if the Legacy BIOS Protocol is available
137 Status
= gBS
->LocateProtocol (
138 &gEfiLegacyBiosProtocolGuid
,
140 (VOID
**) &LegacyBios
143 if (EFI_ERROR (Status
)) {
147 // Open the IO Abstraction(s) needed to perform the supported test
149 Status
= gBS
->OpenProtocol (
151 &gEfiIsaIoProtocolGuid
,
153 This
->DriverBindingHandle
,
155 EFI_OPEN_PROTOCOL_BY_DRIVER
158 if (EFI_ERROR (Status
)) {
162 // Use the ISA I/O Protocol to see if Controller is the Keyboard controller
164 if (IsaIo
->ResourceList
->Device
.HID
!= EISA_PNP_ID (0x303) || IsaIo
->ResourceList
->Device
.UID
!= 0) {
165 Status
= EFI_UNSUPPORTED
;
170 &gEfiIsaIoProtocolGuid
,
171 This
->DriverBindingHandle
,
179 Starts the device with this driver.
181 @param This The driver binding instance.
182 @param Controller Handle of device to bind driver to.
183 @param RemainingDevicePath Optional parameter use to pick a specific child
186 @retval EFI_SUCCESS The controller is controlled by the driver.
187 @retval Other This controller cannot be started.
192 BiosKeyboardDriverBindingStart (
193 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
194 IN EFI_HANDLE Controller
,
195 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
199 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
200 EFI_ISA_IO_PROTOCOL
*IsaIo
;
201 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
202 EFI_IA32_REGISTER_SET Regs
;
204 EFI_PS2_POLICY_PROTOCOL
*Ps2Policy
;
206 EFI_STATUS_CODE_VALUE StatusCode
;
208 BiosKeyboardPrivate
= NULL
;
213 // Get Ps2 policy to set. Will be use if present.
215 gBS
->LocateProtocol (
216 &gEfiPs2PolicyProtocolGuid
,
222 // See if the Legacy BIOS Protocol is available
224 Status
= gBS
->LocateProtocol (
225 &gEfiLegacyBiosProtocolGuid
,
227 (VOID
**) &LegacyBios
230 if (EFI_ERROR (Status
)) {
234 // Open the IO Abstraction(s) needed
236 Status
= gBS
->OpenProtocol (
238 &gEfiIsaIoProtocolGuid
,
240 This
->DriverBindingHandle
,
242 EFI_OPEN_PROTOCOL_BY_DRIVER
244 if (EFI_ERROR (Status
)) {
249 // Allocate the private device structure
251 BiosKeyboardPrivate
= (BIOS_KEYBOARD_DEV
*) AllocateZeroPool (sizeof (BIOS_KEYBOARD_DEV
));
252 if (NULL
== BiosKeyboardPrivate
) {
253 Status
= EFI_OUT_OF_RESOURCES
;
258 // Initialize the private device structure
260 BiosKeyboardPrivate
->Signature
= BIOS_KEYBOARD_DEV_SIGNATURE
;
261 BiosKeyboardPrivate
->Handle
= Controller
;
262 BiosKeyboardPrivate
->LegacyBios
= LegacyBios
;
263 BiosKeyboardPrivate
->IsaIo
= IsaIo
;
265 BiosKeyboardPrivate
->SimpleTextIn
.Reset
= BiosKeyboardReset
;
266 BiosKeyboardPrivate
->SimpleTextIn
.ReadKeyStroke
= BiosKeyboardReadKeyStroke
;
268 BiosKeyboardPrivate
->DataRegisterAddress
= KEYBOARD_8042_DATA_REGISTER
;
269 BiosKeyboardPrivate
->StatusRegisterAddress
= KEYBOARD_8042_STATUS_REGISTER
;
270 BiosKeyboardPrivate
->CommandRegisterAddress
= KEYBOARD_8042_COMMAND_REGISTER
;
271 BiosKeyboardPrivate
->ExtendedKeyboard
= TRUE
;
273 BiosKeyboardPrivate
->Queue
.Front
= 0;
274 BiosKeyboardPrivate
->Queue
.Rear
= 0;
275 BiosKeyboardPrivate
->SimpleTextInputEx
.Reset
= BiosKeyboardResetEx
;
276 BiosKeyboardPrivate
->SimpleTextInputEx
.ReadKeyStrokeEx
= BiosKeyboardReadKeyStrokeEx
;
277 BiosKeyboardPrivate
->SimpleTextInputEx
.SetState
= BiosKeyboardSetState
;
278 BiosKeyboardPrivate
->SimpleTextInputEx
.RegisterKeyNotify
= BiosKeyboardRegisterKeyNotify
;
279 BiosKeyboardPrivate
->SimpleTextInputEx
.UnregisterKeyNotify
= BiosKeyboardUnregisterKeyNotify
;
280 InitializeListHead (&BiosKeyboardPrivate
->NotifyList
);
283 // Report that the keyboard is being enabled
287 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_ENABLE
291 // Setup the WaitForKey event
293 Status
= gBS
->CreateEvent (
296 BiosKeyboardWaitForKey
,
297 &(BiosKeyboardPrivate
->SimpleTextIn
),
298 &((BiosKeyboardPrivate
->SimpleTextIn
).WaitForKey
)
300 if (EFI_ERROR (Status
)) {
301 (BiosKeyboardPrivate
->SimpleTextIn
).WaitForKey
= NULL
;
304 Status
= gBS
->CreateEvent (
307 BiosKeyboardWaitForKeyEx
,
308 &(BiosKeyboardPrivate
->SimpleTextInputEx
),
309 &(BiosKeyboardPrivate
->SimpleTextInputEx
.WaitForKeyEx
)
311 if (EFI_ERROR (Status
)) {
312 BiosKeyboardPrivate
->SimpleTextInputEx
.WaitForKeyEx
= NULL
;
317 // Setup a periodic timer, used for reading keystrokes at a fixed interval
319 Status
= gBS
->CreateEvent (
320 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
322 BiosKeyboardTimerHandler
,
324 &BiosKeyboardPrivate
->TimerEvent
326 if (EFI_ERROR (Status
)) {
327 Status
= EFI_OUT_OF_RESOURCES
;
328 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
332 Status
= gBS
->SetTimer (
333 BiosKeyboardPrivate
->TimerEvent
,
335 KEYBOARD_TIMER_INTERVAL
337 if (EFI_ERROR (Status
)) {
338 Status
= EFI_OUT_OF_RESOURCES
;
339 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
;
344 // Report a Progress Code for an attempt to detect the precense of the keyboard device in the system
348 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_PRESENCE_DETECT
352 // Reset the keyboard device
354 Status
= BiosKeyboardPrivate
->SimpleTextInputEx
.Reset (
355 &BiosKeyboardPrivate
->SimpleTextInputEx
,
356 FeaturePcdGet (PcdPs2KbdExtendedVerification
)
358 if (EFI_ERROR (Status
)) {
359 DEBUG ((EFI_D_ERROR
, "[KBD]Reset Failed. Status - %r\n", Status
));
360 StatusCode
= EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_NOT_DETECTED
;
364 // Do platform specific policy like port swapping and keyboard light default
366 if (Ps2Policy
!= NULL
) {
368 Ps2Policy
->Ps2InitHardware (Controller
);
371 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_CAPSLOCK
) == EFI_KEYBOARD_CAPSLOCK
) {
375 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_NUMLOCK
) == EFI_KEYBOARD_NUMLOCK
) {
379 if ((Ps2Policy
->KeyboardLight
& EFI_KEYBOARD_SCROLLLOCK
) == EFI_KEYBOARD_SCROLLLOCK
) {
383 KeyboardWrite (BiosKeyboardPrivate
, 0xed);
384 KeyboardWaitForValue (BiosKeyboardPrivate
, 0xfa, KEYBOARD_WAITFORVALUE_TIMEOUT
);
385 KeyboardWrite (BiosKeyboardPrivate
, Command
);
387 // Call Legacy BIOS Protocol to set whatever is necessary
389 LegacyBios
->UpdateKeyboardLedStatus (LegacyBios
, Command
);
395 CarryFlag
= BiosKeyboardPrivate
->LegacyBios
->Int86 (
396 BiosKeyboardPrivate
->LegacyBios
,
403 // Check bit 6 of Feature Byte 2.
404 // If it is set, then Int 16 Func 09 is supported
406 if (*(UINT8
*)(UINTN
) ((Regs
.X
.ES
<< 4) + Regs
.X
.BX
+ 0x06) & 0x40) {
408 // Get Keyboard Functionality
411 CarryFlag
= BiosKeyboardPrivate
->LegacyBios
->Int86 (
412 BiosKeyboardPrivate
->LegacyBios
,
419 // Check bit 5 of AH.
420 // If it is set, then INT 16 Finc 10-12 are supported.
422 if ((Regs
.H
.AL
& 0x40) != 0) {
424 // Set the flag to use INT 16 Func 10-12
426 BiosKeyboardPrivate
->ExtendedKeyboard
= TRUE
;
431 DEBUG ((EFI_D_INFO
, "[KBD]Extended keystrokes supported by CSM16 - %02x\n", (UINTN
)BiosKeyboardPrivate
->ExtendedKeyboard
));
433 // Install protocol interfaces for the keyboard device.
435 Status
= gBS
->InstallMultipleProtocolInterfaces (
437 &gEfiSimpleTextInProtocolGuid
,
438 &BiosKeyboardPrivate
->SimpleTextIn
,
439 &gEfiSimpleTextInputExProtocolGuid
,
440 &BiosKeyboardPrivate
->SimpleTextInputEx
,
445 if (StatusCode
!= 0) {
447 // Report an Error Code for failing to start the keyboard device
450 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
455 if (EFI_ERROR (Status
)) {
457 if (BiosKeyboardPrivate
!= NULL
) {
458 if ((BiosKeyboardPrivate
->SimpleTextIn
).WaitForKey
!= NULL
) {
459 gBS
->CloseEvent ((BiosKeyboardPrivate
->SimpleTextIn
).WaitForKey
);
462 if ((BiosKeyboardPrivate
->SimpleTextInputEx
).WaitForKeyEx
!= NULL
) {
463 gBS
->CloseEvent ((BiosKeyboardPrivate
->SimpleTextInputEx
).WaitForKeyEx
);
465 BiosKeyboardFreeNotifyList (&BiosKeyboardPrivate
->NotifyList
);
467 if (BiosKeyboardPrivate
->TimerEvent
!= NULL
) {
468 gBS
->CloseEvent (BiosKeyboardPrivate
->TimerEvent
);
471 FreePool (BiosKeyboardPrivate
);
477 &gEfiIsaIoProtocolGuid
,
478 This
->DriverBindingHandle
,
488 Stop the device handled by this driver.
490 @param This The driver binding protocol.
491 @param Controller The controller to release.
492 @param NumberOfChildren The number of handles in ChildHandleBuffer.
493 @param ChildHandleBuffer The array of child handle.
495 @retval EFI_SUCCESS The device was stopped.
496 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
497 @retval Others Fail to uninstall protocols attached on the device.
502 BiosKeyboardDriverBindingStop (
503 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
504 IN EFI_HANDLE Controller
,
505 IN UINTN NumberOfChildren
,
506 IN EFI_HANDLE
*ChildHandleBuffer
510 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*SimpleTextIn
;
511 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
516 Status
= gBS
->OpenProtocol (
518 &gEfiSimpleTextInProtocolGuid
,
519 (VOID
**) &SimpleTextIn
,
520 This
->DriverBindingHandle
,
522 EFI_OPEN_PROTOCOL_GET_PROTOCOL
524 if (EFI_ERROR (Status
)) {
528 Status
= gBS
->OpenProtocol (
530 &gEfiSimpleTextInputExProtocolGuid
,
532 This
->DriverBindingHandle
,
534 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
536 if (EFI_ERROR (Status
)) {
540 BiosKeyboardPrivate
= BIOS_KEYBOARD_DEV_FROM_THIS (SimpleTextIn
);
542 Status
= gBS
->UninstallMultipleProtocolInterfaces (
544 &gEfiSimpleTextInProtocolGuid
,
545 &BiosKeyboardPrivate
->SimpleTextIn
,
546 &gEfiSimpleTextInputExProtocolGuid
,
547 &BiosKeyboardPrivate
->SimpleTextInputEx
,
550 if (EFI_ERROR (Status
)) {
554 // Release the IsaIo protocol on the controller handle
558 &gEfiIsaIoProtocolGuid
,
559 This
->DriverBindingHandle
,
564 // Free other resources
566 gBS
->CloseEvent ((BiosKeyboardPrivate
->SimpleTextIn
).WaitForKey
);
567 gBS
->CloseEvent (BiosKeyboardPrivate
->TimerEvent
);
568 gBS
->CloseEvent (BiosKeyboardPrivate
->SimpleTextInputEx
.WaitForKeyEx
);
569 BiosKeyboardFreeNotifyList (&BiosKeyboardPrivate
->NotifyList
);
571 FreePool (BiosKeyboardPrivate
);
577 Read data byte from output buffer of Keyboard Controller without delay and waiting for buffer-empty state.
579 @param BiosKeyboardPrivate Keyboard instance pointer.
581 @return The data byte read from output buffer of Keyboard Controller from data port which often is port 60H.
585 KeyReadDataRegister (
586 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
592 // Use IsaIo protocol to perform IO operations
594 BiosKeyboardPrivate
->IsaIo
->Io
.Read (
595 BiosKeyboardPrivate
->IsaIo
,
597 BiosKeyboardPrivate
->DataRegisterAddress
,
606 Read status byte from status register of Keyboard Controller without delay and waiting for buffer-empty state.
608 @param BiosKeyboardPrivate Keyboard instance pointer.
610 @return The status byte read from status register of Keyboard Controller from command port which often is port 64H.
614 KeyReadStatusRegister (
615 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
621 // Use IsaIo protocol to perform IO operations
623 BiosKeyboardPrivate
->IsaIo
->Io
.Read (
624 BiosKeyboardPrivate
->IsaIo
,
626 BiosKeyboardPrivate
->StatusRegisterAddress
,
635 Write command byte to control register of Keyboard Controller without delay and waiting for buffer-empty state.
637 @param BiosKeyboardPrivate Keyboard instance pointer.
638 @param Data Data byte to write.
642 KeyWriteCommandRegister (
643 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
648 // Use IsaIo protocol to perform IO operations
650 BiosKeyboardPrivate
->IsaIo
->Io
.Write (
651 BiosKeyboardPrivate
->IsaIo
,
653 BiosKeyboardPrivate
->CommandRegisterAddress
,
660 Write data byte to input buffer or input/output ports of Keyboard Controller without delay and waiting for buffer-empty state.
662 @param BiosKeyboardPrivate Keyboard instance pointer.
663 @param Data Data byte to write.
667 KeyWriteDataRegister (
668 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
673 // Use IsaIo protocol to perform IO operations
675 BiosKeyboardPrivate
->IsaIo
->Io
.Write (
676 BiosKeyboardPrivate
->IsaIo
,
678 BiosKeyboardPrivate
->DataRegisterAddress
,
685 Read data byte from output buffer of Keyboard Controller with delay and waiting for buffer-empty state.
687 @param BiosKeyboardPrivate Keyboard instance pointer.
688 @param Data The pointer for data that being read out.
690 @retval EFI_SUCCESS The data byte read out successfully.
691 @retval EFI_TIMEOUT Timeout occurred during reading out data byte.
696 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
707 // wait till output buffer full then perform the read
709 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
710 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_OUTB
) != 0) {
712 *Data
= KeyReadDataRegister (BiosKeyboardPrivate
);
719 if (RegFilled
== 0) {
727 Write data byte to input buffer or input/output ports of Keyboard Controller with delay and waiting for buffer-empty state.
729 @param BiosKeyboardPrivate Keyboard instance pointer.
730 @param Data Data byte to write.
732 @retval EFI_SUCCESS The data byte is written successfully.
733 @retval EFI_TIMEOUT Timeout occurred during writing.
738 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
749 // wait for input buffer empty
751 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
752 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_INPB
) == 0) {
760 if (RegEmptied
== 0) {
766 KeyWriteDataRegister (BiosKeyboardPrivate
, Data
);
772 Write command byte to control register of Keyboard Controller with delay and waiting for buffer-empty state.
774 @param BiosKeyboardPrivate Keyboard instance pointer.
775 @param Data Command byte to write.
777 @retval EFI_SUCCESS The command byte is written successfully.
778 @retval EFI_TIMEOUT Timeout occurred during writing.
783 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
794 // Wait For Input Buffer Empty
796 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
797 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_INPB
) == 0) {
805 if (RegEmptied
== 0) {
811 KeyWriteCommandRegister (BiosKeyboardPrivate
, Data
);
814 // Wait For Input Buffer Empty again
817 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
818 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_INPB
) == 0) {
826 if (RegEmptied
== 0) {
834 Wait for a specific value to be presented in
835 Data register of Keyboard Controller by keyboard and then read it,
836 used in keyboard commands ack
838 @param BiosKeyboardPrivate Keyboard instance pointer.
839 @param Value The value to be waited for
840 @param WaitForValueTimeOut The limit of microseconds for timeout
842 @retval EFI_SUCCESS The command byte is written successfully.
843 @retval EFI_TIMEOUT Timeout occurred during writing.
847 KeyboardWaitForValue (
848 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
850 IN UINTN WaitForValueTimeOut
863 // Make sure the initial value of 'Data' is different from 'Value'
870 // Read from 8042 (multiple times if needed)
871 // until the expected value appears
872 // use SumTimeOut to control the iteration
878 for (TimeOut
= 0; TimeOut
< KEYBOARD_TIMEOUT
; TimeOut
+= 30) {
879 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_OUTB
) != 0) {
880 Data
= KeyReadDataRegister (BiosKeyboardPrivate
);
887 SumTimeOut
+= TimeOut
;
894 if (SumTimeOut
>= WaitForValueTimeOut
) {
910 Reads the next keystroke from the input device. The WaitForKey Event can
911 be used to test for existance of a keystroke via WaitForEvent () call.
913 @param BiosKeyboardPrivate Bioskeyboard driver private structure.
914 @param KeyData A pointer to a buffer that is filled in with the keystroke
915 state data for the key that was pressed.
917 @retval EFI_SUCCESS The keystroke information was returned.
918 @retval EFI_NOT_READY There was no keystroke data availiable.
919 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
921 @retval EFI_INVALID_PARAMETER KeyData is NULL.
925 KeyboardReadKeyStrokeWorker (
926 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
,
927 OUT EFI_KEY_DATA
*KeyData
932 if (KeyData
== NULL
) {
933 return EFI_INVALID_PARAMETER
;
937 // Use TimerEvent callback funciton to check whether there's any key pressed
941 // Stall 1ms to give a chance to let other driver interrupt this routine for their timer event.
942 // Csm will be used to check whether there is a key pending, but the csm will disable all
943 // interrupt before switch to compatibility16, which mean all the efiCompatibility timer
944 // event will stop work during the compatibility16. And If a caller recursivly invoke this function,
945 // e.g. OS loader, other drivers which are driven by timer event will have a bad performance during this period,
946 // e.g. usb keyboard driver.
947 // Add a stall period can greatly increate other driver performance during the WaitForKey is recursivly invoked.
948 // 1ms delay will make little impact to the thunk keyboard driver, and user can not feel the delay at all when input.
952 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
954 BiosKeyboardTimerHandler (NULL
, BiosKeyboardPrivate
);
956 // If there's no key, just return
958 Status
= CheckQueue (&BiosKeyboardPrivate
->Queue
);
959 if (EFI_ERROR (Status
)) {
960 gBS
->RestoreTPL (OldTpl
);
961 return EFI_NOT_READY
;
964 Status
= Dequeue (&BiosKeyboardPrivate
->Queue
, KeyData
);
966 gBS
->RestoreTPL (OldTpl
);
972 // EFI Simple Text In Protocol Functions
975 Reset the Keyboard and do BAT test for it, if (ExtendedVerification == TRUE) then do some extra keyboard validations.
977 @param This Pointer of simple text Protocol.
978 @param ExtendedVerification Whether perform the extra validation of keyboard. True: perform; FALSE: skip.
980 @retval EFI_SUCCESS The command byte is written successfully.
981 @retval EFI_DEVICE_ERROR Errors occurred during reseting keyboard.
987 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
988 IN BOOLEAN ExtendedVerification
991 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
999 BiosKeyboardPrivate
= BIOS_KEYBOARD_DEV_FROM_THIS (This
);
1003 // Report reset progress code
1005 REPORT_STATUS_CODE (
1007 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_RESET
1011 // Report a Progress Code for clearing the keyboard buffer
1013 REPORT_STATUS_CODE (
1015 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_CLEAR_BUFFER
1020 // Raise TPL to avoid mouse operation impact
1022 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1026 // Exhaust output buffer data
1029 Status
= BiosKeyboardReadKeyStroke (
1033 } while (!EFI_ERROR (Status
));
1036 // check for KBC itself firstly for setted-up already or not by reading SYSF (bit2) of status register via 64H
1037 // if not skip step 4&5 and jump to step 6 to selftest KBC and report this
1040 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & KBC_STSREG_VIA64_SYSF
) != 0) {
1043 // CheckMouseStatus to decide enable it later or not
1046 // Read the command byte of KBC
1048 Status
= KeyboardCommand (
1049 BiosKeyboardPrivate
,
1050 KBC_CMDREG_VIA64_CMDBYTE_R
1053 if (EFI_ERROR (Status
)) {
1054 Status
= EFI_DEVICE_ERROR
;
1058 Status
= KeyboardRead (
1059 BiosKeyboardPrivate
,
1063 if (EFI_ERROR (Status
)) {
1064 Status
= EFI_DEVICE_ERROR
;
1068 // Check mouse enabled or not before
1070 if ((CommandByte
& KB_CMMBYTE_DISABLE_AUX
) != 0) {
1071 MouseEnable
= FALSE
;
1077 // disable mouse (via KBC) and Keyborad device
1079 Status
= KeyboardCommand (
1080 BiosKeyboardPrivate
,
1081 KBC_CMDREG_VIA64_AUX_DISABLE
1084 if (EFI_ERROR (Status
)) {
1085 Status
= EFI_DEVICE_ERROR
;
1089 Status
= KeyboardCommand (
1090 BiosKeyboardPrivate
,
1091 KBC_CMDREG_VIA64_KB_DISABLE
1094 if (EFI_ERROR (Status
)) {
1095 Status
= EFI_DEVICE_ERROR
;
1105 // Report a Progress Code for performing a self test on the keyboard controller
1107 REPORT_STATUS_CODE (
1109 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_SELF_TEST
1112 Status
= KeyboardCommand (
1113 BiosKeyboardPrivate
,
1114 KBC_CMDREG_VIA64_KBC_SLFTEST
1116 if (EFI_ERROR (Status
)) {
1117 Status
= EFI_DEVICE_ERROR
;
1121 Status
= KeyboardWaitForValue (
1122 BiosKeyboardPrivate
,
1123 KBC_CMDECHO_KBCSLFTEST_OK
,
1124 KEYBOARD_WAITFORVALUE_TIMEOUT
1126 if (EFI_ERROR (Status
)) {
1127 Status
= EFI_DEVICE_ERROR
;
1133 // Disable Mouse interface, enable Keyboard interface and declare selftest success
1135 // Mouse device will block keyboard interface before it be configured, so we should disable mouse first.
1137 Status
= KeyboardCommand (
1138 BiosKeyboardPrivate
,
1139 KBC_CMDREG_VIA64_CMDBYTE_W
1142 if (EFI_ERROR (Status
)) {
1143 Status
= EFI_DEVICE_ERROR
;
1148 // Write 8042 Command Byte, set System Flag
1149 // While at the same time:
1150 // 1. disable mouse interface,
1151 // 2. enable kbd interface,
1152 // 3. enable PC/XT kbd translation mode
1153 // 4. enable mouse and kbd interrupts
1155 //Command Byte bits:
1157 // 6: PC/XT translation mode
1158 // 5: Disable Auxiliary device interface
1159 // 4: Disable keyboard interface
1162 // 1: Enable Auxiliary device interrupt
1163 // 0: Enable Keyboard interrupt
1166 Status
= KeyboardWrite (
1167 BiosKeyboardPrivate
,
1168 (UINT8
) ((CommandByte
&
1169 (~KB_CMMBYTE_DISABLE_KB
)) |
1170 KB_CMMBYTE_KSCAN2UNI_COV
|
1171 KB_CMMBYTE_ENABLE_AUXINT
|
1172 KB_CMMBYTE_ENABLE_KBINT
|
1173 KB_CMMBYTE_SLFTEST_SUCC
|
1174 KB_CMMBYTE_DISABLE_AUX
)
1178 // For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
1179 // so we only do the real reseting for keyboard when user asks, and normally during booting an OS, it's skipped.
1180 // Call CheckKeyboardConnect() to check whether keyboard is connected, if it is not connected,
1181 // Real reset will not do.
1183 if (ExtendedVerification
&& CheckKeyboardConnect (BiosKeyboardPrivate
)) {
1186 // Send keyboard reset command then read ACK
1188 Status
= KeyboardWrite (
1189 BiosKeyboardPrivate
,
1190 KBC_INPBUF_VIA60_KBRESET
1193 if (EFI_ERROR (Status
)) {
1194 Status
= EFI_DEVICE_ERROR
;
1198 Status
= KeyboardWaitForValue (
1199 BiosKeyboardPrivate
,
1201 KEYBOARD_WAITFORVALUE_TIMEOUT
1204 if (EFI_ERROR (Status
)) {
1205 Status
= EFI_DEVICE_ERROR
;
1210 // Wait for keyboard return test OK.
1212 Status
= KeyboardWaitForValue (
1213 BiosKeyboardPrivate
,
1214 KBC_CMDECHO_BATTEST_OK
,
1215 KEYBOARD_WAITFORVALUE_TIMEOUT
1218 if (EFI_ERROR (Status
)) {
1219 Status
= EFI_DEVICE_ERROR
;
1224 // set keyboard scan code set = 02 (standard configuration)
1226 Status
= KeyboardWrite (
1227 BiosKeyboardPrivate
,
1228 KBC_INPBUF_VIA60_KBSCODE
1230 if (EFI_ERROR (Status
)) {
1231 Status
= EFI_DEVICE_ERROR
;
1235 Status
= KeyboardWaitForValue (
1236 BiosKeyboardPrivate
,
1238 KEYBOARD_WAITFORVALUE_TIMEOUT
1241 if (EFI_ERROR (Status
)) {
1242 Status
= EFI_DEVICE_ERROR
;
1246 Status
= KeyboardWrite (
1247 BiosKeyboardPrivate
,
1248 KBC_INPBUF_VIA60_SCODESET2
1250 if (EFI_ERROR (Status
)) {
1251 Status
= EFI_DEVICE_ERROR
;
1255 Status
= KeyboardWaitForValue (
1256 BiosKeyboardPrivate
,
1258 KEYBOARD_WAITFORVALUE_TIMEOUT
1261 if (EFI_ERROR (Status
)) {
1262 Status
= EFI_DEVICE_ERROR
;
1267 // enable keyboard itself (not via KBC) by writing CMD F4 via 60H
1269 Status
= KeyboardWrite (
1270 BiosKeyboardPrivate
,
1271 KBC_INPBUF_VIA60_KBEN
1273 if (EFI_ERROR (Status
)) {
1274 Status
= EFI_DEVICE_ERROR
;
1278 Status
= KeyboardWaitForValue (
1279 BiosKeyboardPrivate
,
1281 KEYBOARD_WAITFORVALUE_TIMEOUT
1284 if (EFI_ERROR (Status
)) {
1285 Status
= EFI_DEVICE_ERROR
;
1290 // Additional validation, do it as follow:
1291 // 1). check for status register of PARE && TIM via 64H
1292 // 2). perform KB checking by writing ABh via 64H
1294 if ((KeyReadStatusRegister (BiosKeyboardPrivate
) & (KBC_STSREG_VIA64_PARE
| KBC_STSREG_VIA64_TIM
)) != 0) {
1295 Status
= EFI_DEVICE_ERROR
;
1299 Status
= KeyboardCommand (
1300 BiosKeyboardPrivate
,
1301 KBC_CMDREG_VIA64_KB_CKECK
1303 if (EFI_ERROR (Status
)) {
1304 Status
= EFI_DEVICE_ERROR
;
1308 Status
= KeyboardWaitForValue (
1309 BiosKeyboardPrivate
,
1310 KBC_CMDECHO_KBCHECK_OK
,
1311 KEYBOARD_WAITFORVALUE_TIMEOUT
1314 if (EFI_ERROR (Status
)) {
1315 Status
= EFI_DEVICE_ERROR
;
1321 // Done for validating keyboard. Enable keyboard (via KBC)
1322 // and recover the command byte to proper value
1324 Status
= KeyboardCommand (
1325 BiosKeyboardPrivate
,
1326 KBC_CMDREG_VIA64_KB_ENABLE
1329 if (EFI_ERROR (Status
)) {
1330 Status
= EFI_DEVICE_ERROR
;
1336 // conditionally enable mouse (via KBC)
1339 Status
= KeyboardCommand (
1340 BiosKeyboardPrivate
,
1341 KBC_CMDREG_VIA64_AUX_ENABLE
1344 if (EFI_ERROR (Status
)) {
1345 Status
= EFI_DEVICE_ERROR
;
1353 // resume priority of task level
1355 gBS
->RestoreTPL (OldTpl
);
1362 Read out the scan code of the key that has just been stroked.
1364 @param This Pointer of simple text Protocol.
1365 @param Key Pointer for store the key that read out.
1367 @retval EFI_SUCCESS The key is read out successfully.
1368 @retval other The key reading failed.
1373 BiosKeyboardReadKeyStroke (
1374 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
1375 OUT EFI_INPUT_KEY
*Key
1378 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
1380 EFI_KEY_DATA KeyData
;
1382 BiosKeyboardPrivate
= BIOS_KEYBOARD_DEV_FROM_THIS (This
);
1384 Status
= KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate
, &KeyData
);
1385 if (EFI_ERROR (Status
)) {
1389 CopyMem (Key
, &KeyData
.Key
, sizeof (EFI_INPUT_KEY
));
1395 Waiting on the keyboard event, if there's any key pressed by the user, signal the event
1397 @param Event The event that be siganlled when any key has been stroked.
1398 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
1403 BiosKeyboardWaitForKey (
1409 // Stall 1ms to give a chance to let other driver interrupt this routine for their timer event.
1410 // Csm will be used to check whether there is a key pending, but the csm will disable all
1411 // interrupt before switch to compatibility16, which mean all the efiCompatibility timer
1412 // event will stop work during the compatibility16. And If a caller recursivly invoke this function,
1413 // e.g. UI setup or Shell, other drivers which are driven by timer event will have a bad performance during this period,
1414 // e.g. usb keyboard driver.
1415 // Add a stall period can greatly increate other driver performance during the WaitForKey is recursivly invoked.
1416 // 1ms delay will make little impact to the thunk keyboard driver, and user can not feel the delay at all when input.
1420 // Use TimerEvent callback funciton to check whether there's any key pressed
1422 BiosKeyboardTimerHandler (NULL
, BIOS_KEYBOARD_DEV_FROM_THIS (Context
));
1424 if (!EFI_ERROR (BiosKeyboardCheckForKey (Context
))) {
1425 gBS
->SignalEvent (Event
);
1430 Check key buffer to get the key stroke status.
1432 @param This Pointer of the protocol EFI_SIMPLE_TEXT_IN_PROTOCOL.
1434 @retval EFI_SUCCESS A key is being pressed now.
1435 @retval Other No key is now pressed.
1440 BiosKeyboardCheckForKey (
1441 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
1444 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
1446 BiosKeyboardPrivate
= BIOS_KEYBOARD_DEV_FROM_THIS (This
);
1448 return CheckQueue (&BiosKeyboardPrivate
->Queue
);
1451 // Private worker functions
1453 #define TABLE_END 0x0
1455 typedef struct _CONVERT_TABLE_ENTRY
{
1458 } CONVERT_TABLE_ENTRY
;
1460 CONVERT_TABLE_ENTRY mConvertTable
[] = {
1502 // Function Keys are only valid if KeyChar == 0x00
1503 // This function does not require KeyChar to be 0x00
1554 // Convert ALT + Fn keys
1603 Convert unicode combined with scan code of key to the counterpart of EFIScancode of it.
1605 @param KeyChar Unicode of key.
1606 @param ScanCode Scan code of key.
1608 @return The value of EFI Scancode for the key.
1609 @retval SCAN_NULL No corresponding value in the EFI convert table is found for the key.
1613 ConvertToEFIScanCode (
1621 if (KeyChar
== CHAR_ESC
) {
1622 EfiScanCode
= SCAN_ESC
;
1623 } else if (KeyChar
== 0x00 || KeyChar
== 0xe0) {
1625 // Movement & Function Keys
1627 for (Index
= 0; (Index
< sizeof (mConvertTable
) / sizeof (CONVERT_TABLE_ENTRY
)) && (mConvertTable
[Index
].ScanCode
!= TABLE_END
); Index
+= 1) {
1628 if (ScanCode
== mConvertTable
[Index
].ScanCode
) {
1629 return mConvertTable
[Index
].EfiScanCode
;
1633 // Reach Table end, return default value
1644 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
1645 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
1646 should not be in system.
1648 @param BiosKeyboardPrivate Keyboard Private Data Struture
1650 @retval TRUE Keyboard in System.
1651 @retval FALSE Keyboard not in System.
1655 CheckKeyboardConnect (
1656 IN BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
1661 Status
= EFI_SUCCESS
;
1663 // enable keyboard itself and wait for its ack
1664 // If can't receive ack, Keyboard should not be connected.
1666 Status
= KeyboardWrite (
1667 BiosKeyboardPrivate
,
1668 KBC_INPBUF_VIA60_KBEN
1670 if (EFI_ERROR (Status
)) {
1671 DEBUG ((EFI_D_ERROR
, "[KBD]CheckKeyboardConnect - Keyboard enable failed!\n"));
1672 REPORT_STATUS_CODE (
1673 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1674 EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
1679 Status
= KeyboardWaitForValue (
1680 BiosKeyboardPrivate
,
1682 KEYBOARD_WAITFORVALUE_TIMEOUT
1685 if (EFI_ERROR (Status
)) {
1686 DEBUG ((EFI_D_ERROR
, "[KBD]CheckKeyboardConnect - Timeout!\n"));
1687 REPORT_STATUS_CODE (
1688 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1689 EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_CONTROLLER_ERROR
1698 Timer event handler: read a series of key stroke from 8042
1699 and put them into memory key buffer.
1700 It is registered as running under TPL_NOTIFY
1702 @param Event The timer event
1703 @param Context A BIOS_KEYBOARD_DEV pointer
1708 BiosKeyboardTimerHandler (
1714 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
1715 EFI_IA32_REGISTER_SET Regs
;
1716 UINT8 KbFlag1
; // 0040h:0017h - KEYBOARD - STATUS FLAGS 1
1717 UINT8 KbFlag2
; // 0040h:0018h - KEYBOARD - STATUS FLAGS 2
1718 EFI_KEY_DATA KeyData
;
1720 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
1722 BiosKeyboardPrivate
= Context
;
1725 // Enter critical section
1727 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1730 // if there is no key present, just return
1732 if (BiosKeyboardPrivate
->ExtendedKeyboard
) {
1738 BiosKeyboardPrivate
->LegacyBios
->Int86 (
1739 BiosKeyboardPrivate
->LegacyBios
,
1743 if (Regs
.X
.Flags
.ZF
!= 0) {
1744 gBS
->RestoreTPL (OldTpl
);
1751 if (BiosKeyboardPrivate
->ExtendedKeyboard
) {
1757 BiosKeyboardPrivate
->LegacyBios
->Int86 (
1758 BiosKeyboardPrivate
->LegacyBios
,
1763 KeyData
.Key
.ScanCode
= (UINT16
) Regs
.H
.AH
;
1764 KeyData
.Key
.UnicodeChar
= (UINT16
) Regs
.H
.AL
;
1767 "[KBD]INT16 returns EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",
1768 KeyData
.Key
.ScanCode
,
1769 KeyData
.Key
.UnicodeChar
1772 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
;
1773 KeyData
.KeyState
.KeyToggleState
= EFI_TOGGLE_STATE_VALID
;
1775 // Leagcy Bios use Int 9 which is IRQ1 interrupt handler to get keystroke scancode to KB buffer in BDA (BIOS DATE AREA), then
1776 // Int 16 depend KB buffer and some key bits in BDA to translate the scancode to ASCII code, and return both the scancode and ASCII
1777 // code to Int 16 caller. This translation process works well if the Int 9 could response user input in time. But in Tiano enviorment, the Int 9
1778 // will be disabled after the thunk call finish, which means if user crazy input during int 9 being disabled, some keystrokes will be lost when
1779 // KB device own hardware buffer overflows. And if the lost keystroke code is CTRL or ALT or SHIFT release code, these function key flags bit
1780 // in BDA will not be updated. So the Int 16 will believe the CTRL or ALT or SHIFT is still pressed, and Int 16 will translate later scancode
1781 // to wrong ASCII code. We can increase the Thunk frequence to let Int 9 response in time, but this way will much hurt other dirvers
1782 // performance, like USB.
1784 // 1. If CTRL or ALT release code is missed, all later input keys will be translated to wrong ASCII codes which the Tiano cannot support. In
1785 // this case, the KB input seems fail to work, and user input is blocked. To solve the problem, we can help to clear the CTRL or ALT flag in BDA
1786 // after every Int 16 finish. Thus persist to press CTRL or ALT has same effection as only press one time. It is Ok, since user not often use the
1789 // 2. If SHIFT release code is missed, all later lowercase input will become capital. This is ugly, but not block user input. If user press the lost
1790 // SHIFT again, the lowercase will come back to normal. Since user often use the SHIFT, it is not reasonable to help to clear the SHIFT flag in BDA,
1791 // which will let persist to press SHIFT has same effection as only press one time.
1793 //0040h:0017h - KEYBOARD - STATUS FLAGS 1
1795 // 6 Caps Lock active
1796 // 5 Num Lock active
1797 // 4 Scroll Lock active
1798 // 3 either Alt pressed
1799 // 2 either Ctrl pressed
1800 // 1 Left Shift pressed
1801 // 0 Right Shift pressed
1805 // Clear the CTRL and ALT BDA flag
1807 KbFlag1
= *((UINT8
*) (UINTN
) 0x417); // read the STATUS FLAGS 1
1808 KbFlag2
= *((UINT8
*) (UINTN
) 0x418); // read STATUS FLAGS 2
1812 if ((KbFlag1
& KB_CAPS_LOCK_BIT
) == KB_CAPS_LOCK_BIT
) {
1813 DEBUG ((EFI_D_INFO
, "[KBD]Caps Lock Key is pressed.\n"));
1815 if ((KbFlag1
& KB_NUM_LOCK_BIT
) == KB_NUM_LOCK_BIT
) {
1816 DEBUG ((EFI_D_INFO
, "[KBD]Num Lock Key is pressed.\n"));
1818 if ((KbFlag1
& KB_SCROLL_LOCK_BIT
) == KB_SCROLL_LOCK_BIT
) {
1819 DEBUG ((EFI_D_INFO
, "[KBD]Scroll Lock Key is pressed.\n"));
1821 if ((KbFlag1
& KB_ALT_PRESSED
) == KB_ALT_PRESSED
) {
1822 if ((KbFlag2
& KB_LEFT_ALT_PRESSED
) == KB_LEFT_ALT_PRESSED
) {
1823 DEBUG ((EFI_D_INFO
, "[KBD]Left Alt Key is pressed.\n"));
1825 DEBUG ((EFI_D_INFO
, "[KBD]Right Alt Key is pressed.\n"));
1828 if ((KbFlag1
& KB_CTRL_PRESSED
) == KB_CTRL_PRESSED
) {
1829 if ((KbFlag2
& KB_LEFT_CTRL_PRESSED
) == KB_LEFT_CTRL_PRESSED
) {
1830 DEBUG ((EFI_D_INFO
, "[KBD]Left Ctrl Key is pressed.\n"));
1832 DEBUG ((EFI_D_INFO
, "[KBD]Right Ctrl Key is pressed.\n"));
1835 if ((KbFlag1
& KB_LEFT_SHIFT_PRESSED
) == KB_LEFT_SHIFT_PRESSED
) {
1836 DEBUG ((EFI_D_INFO
, "[KBD]Left Shift Key is pressed.\n"));
1838 if ((KbFlag1
& KB_RIGHT_SHIFT_PRESSED
) == KB_RIGHT_SHIFT_PRESSED
) {
1839 DEBUG ((EFI_D_INFO
, "[KBD]Right Shift Key is pressed.\n"));
1845 // Record toggle state
1847 if ((KbFlag1
& KB_CAPS_LOCK_BIT
) == KB_CAPS_LOCK_BIT
) {
1848 KeyData
.KeyState
.KeyToggleState
|= EFI_CAPS_LOCK_ACTIVE
;
1850 if ((KbFlag1
& KB_NUM_LOCK_BIT
) == KB_NUM_LOCK_BIT
) {
1851 KeyData
.KeyState
.KeyToggleState
|= EFI_NUM_LOCK_ACTIVE
;
1853 if ((KbFlag1
& KB_SCROLL_LOCK_BIT
) == KB_SCROLL_LOCK_BIT
) {
1854 KeyData
.KeyState
.KeyToggleState
|= EFI_SCROLL_LOCK_ACTIVE
;
1857 // Record shift state
1858 // BUGBUG: Need add Menu key and Left/Right Logo key state in the future
1860 if ((KbFlag1
& KB_ALT_PRESSED
) == KB_ALT_PRESSED
) {
1861 KeyData
.KeyState
.KeyShiftState
|= ((KbFlag2
& KB_LEFT_ALT_PRESSED
) == KB_LEFT_ALT_PRESSED
) ? EFI_LEFT_ALT_PRESSED
: EFI_RIGHT_ALT_PRESSED
;
1863 if ((KbFlag1
& KB_CTRL_PRESSED
) == KB_CTRL_PRESSED
) {
1864 KeyData
.KeyState
.KeyShiftState
|= ((KbFlag2
& KB_LEFT_CTRL_PRESSED
) == KB_LEFT_CTRL_PRESSED
) ? EFI_LEFT_CONTROL_PRESSED
: EFI_RIGHT_CONTROL_PRESSED
;
1866 if ((KbFlag1
& KB_LEFT_SHIFT_PRESSED
) == KB_LEFT_SHIFT_PRESSED
) {
1867 KeyData
.KeyState
.KeyShiftState
|= EFI_LEFT_SHIFT_PRESSED
;
1869 if ((KbFlag1
& KB_RIGHT_SHIFT_PRESSED
) == KB_RIGHT_SHIFT_PRESSED
) {
1870 KeyData
.KeyState
.KeyShiftState
|= EFI_RIGHT_SHIFT_PRESSED
;
1874 // Clear left alt and left ctrl BDA flag
1876 KbFlag2
&= ~(KB_LEFT_ALT_PRESSED
| KB_LEFT_CTRL_PRESSED
);
1877 *((UINT8
*) (UINTN
) 0x418) = KbFlag2
;
1879 *((UINT8
*) (UINTN
) 0x417) = KbFlag1
;
1883 // Output EFI input key and shift/toggle state
1885 if (KeyData
.Key
.UnicodeChar
== CHAR_NULL
|| KeyData
.Key
.UnicodeChar
== CHAR_SCANCODE
|| KeyData
.Key
.UnicodeChar
== CHAR_ESC
) {
1886 KeyData
.Key
.ScanCode
= ConvertToEFIScanCode (KeyData
.Key
.UnicodeChar
, KeyData
.Key
.ScanCode
);
1887 KeyData
.Key
.UnicodeChar
= CHAR_NULL
;
1889 KeyData
.Key
.ScanCode
= SCAN_NULL
;
1893 // CSM16 has converted the Ctrl+[a-z] to [1-26], converted it back.
1895 if ((KeyData
.KeyState
.KeyShiftState
& (EFI_LEFT_CONTROL_PRESSED
| EFI_RIGHT_CONTROL_PRESSED
)) != 0) {
1896 if (KeyData
.Key
.UnicodeChar
>= 1 && KeyData
.Key
.UnicodeChar
<= 26) {
1897 if (((KeyData
.KeyState
.KeyShiftState
& (EFI_LEFT_SHIFT_PRESSED
| EFI_RIGHT_SHIFT_PRESSED
)) != 0) ==
1898 ((KeyData
.KeyState
.KeyToggleState
& EFI_CAPS_LOCK_ACTIVE
) != 0)
1900 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
+ L
'a' - 1);
1902 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
+ L
'A' - 1);
1909 "[KBD]Convert to EFI Scan Code, EFI_INPUT_KEY.ScanCode - %x, EFI_INPUT_KEY.UnicodeChar - %x\n",
1910 KeyData
.Key
.ScanCode
,
1911 KeyData
.Key
.UnicodeChar
1915 // Need not return associated shift state if a class of printable characters that
1916 // are normally adjusted by shift modifiers.
1917 // e.g. Shift Key + 'f' key = 'F'; Shift Key + 'F' key = 'f'.
1919 if ((KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') ||
1920 (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z')
1922 DEBUG ((EFI_D_INFO
, "[KBD]Shift key with a~z are pressed, remove shift state in EFI_KEY_STATE.\n"));
1923 KeyData
.KeyState
.KeyShiftState
&= ~(EFI_LEFT_SHIFT_PRESSED
| EFI_RIGHT_SHIFT_PRESSED
);
1927 // Invoke notification functions if exist
1929 for (Link
= BiosKeyboardPrivate
->NotifyList
.ForwardLink
; Link
!= &BiosKeyboardPrivate
->NotifyList
; Link
= Link
->ForwardLink
) {
1930 CurrentNotify
= CR (
1932 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1934 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1936 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
1937 CurrentNotify
->KeyNotificationFn (&KeyData
);
1942 // Convert the Ctrl+[a-z] to Ctrl+[1-26]
1944 if ((KeyData
.KeyState
.KeyShiftState
& (EFI_LEFT_CONTROL_PRESSED
| EFI_RIGHT_CONTROL_PRESSED
)) != 0) {
1945 if (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z') {
1946 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'a' + 1);
1947 } else if (KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') {
1948 KeyData
.Key
.UnicodeChar
= (UINT16
) (KeyData
.Key
.UnicodeChar
- L
'A' + 1);
1951 Enqueue (&BiosKeyboardPrivate
->Queue
, &KeyData
);
1953 // Leave critical section and return
1955 gBS
->RestoreTPL (OldTpl
);
1961 Free keyboard notify list.
1963 @param ListHead The list head
1965 @retval EFI_SUCCESS Free the notify list successfully
1966 @retval EFI_INVALID_PARAMETER ListHead is invalid.
1970 BiosKeyboardFreeNotifyList (
1971 IN OUT LIST_ENTRY
*ListHead
1974 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NotifyNode
;
1976 if (ListHead
== NULL
) {
1977 return EFI_INVALID_PARAMETER
;
1979 while (!IsListEmpty (ListHead
)) {
1981 ListHead
->ForwardLink
,
1982 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1984 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1986 RemoveEntryList (ListHead
->ForwardLink
);
1987 gBS
->FreePool (NotifyNode
);
1994 Check if key is registered.
1996 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
1997 state data for the key that was registered.
1998 @param InputData A pointer to a buffer that is filled in with the keystroke
1999 state data for the key that was pressed.
2001 @retval TRUE Key be pressed matches a registered key.
2002 @retval FLASE Match failed.
2007 IN EFI_KEY_DATA
*RegsiteredData
,
2008 IN EFI_KEY_DATA
*InputData
2011 ASSERT (RegsiteredData
!= NULL
&& InputData
!= NULL
);
2013 if ((RegsiteredData
->Key
.ScanCode
!= InputData
->Key
.ScanCode
) ||
2014 (RegsiteredData
->Key
.UnicodeChar
!= InputData
->Key
.UnicodeChar
)) {
2019 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.
2021 if (RegsiteredData
->KeyState
.KeyShiftState
!= 0 &&
2022 RegsiteredData
->KeyState
.KeyShiftState
!= InputData
->KeyState
.KeyShiftState
) {
2025 if (RegsiteredData
->KeyState
.KeyToggleState
!= 0 &&
2026 RegsiteredData
->KeyState
.KeyToggleState
!= InputData
->KeyState
.KeyToggleState
) {
2035 Waiting on the keyboard event, if there's any key pressed by the user, signal the event
2037 @param Event The event that be siganlled when any key has been stroked.
2038 @param Context Pointer of the protocol EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
2043 BiosKeyboardWaitForKeyEx (
2048 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2050 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (Context
);
2051 BiosKeyboardWaitForKey (Event
, &BiosKeyboardPrivate
->SimpleTextIn
);
2056 Reset the input device and optionaly run diagnostics
2058 @param This Protocol instance pointer.
2059 @param ExtendedVerification Driver may perform diagnostics on reset.
2061 @retval EFI_SUCCESS The device was reset.
2062 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
2068 BiosKeyboardResetEx (
2069 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
2070 IN BOOLEAN ExtendedVerification
2073 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2077 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This
);
2079 Status
= BiosKeyboardPrivate
->SimpleTextIn
.Reset (
2080 &BiosKeyboardPrivate
->SimpleTextIn
,
2081 ExtendedVerification
2083 if (EFI_ERROR (Status
)) {
2084 return EFI_DEVICE_ERROR
;
2087 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
2089 gBS
->RestoreTPL (OldTpl
);
2096 Reads the next keystroke from the input device. The WaitForKey Event can
2097 be used to test for existance of a keystroke via WaitForEvent () call.
2099 @param This Protocol instance pointer.
2100 @param KeyData A pointer to a buffer that is filled in with the keystroke
2101 state data for the key that was pressed.
2103 @retval EFI_SUCCESS The keystroke information was returned.
2104 @retval EFI_NOT_READY There was no keystroke data availiable.
2105 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
2107 @retval EFI_INVALID_PARAMETER KeyData is NULL.
2112 BiosKeyboardReadKeyStrokeEx (
2113 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
2114 OUT EFI_KEY_DATA
*KeyData
2117 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2119 if (KeyData
== NULL
) {
2120 return EFI_INVALID_PARAMETER
;
2123 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This
);
2125 return KeyboardReadKeyStrokeWorker (BiosKeyboardPrivate
, KeyData
);
2130 Set certain state for the input device.
2132 @param This Protocol instance pointer.
2133 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
2134 state for the input device.
2136 @retval EFI_SUCCESS The device state was set successfully.
2137 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
2138 not have the setting adjusted.
2139 @retval EFI_UNSUPPORTED The device does not have the ability to set its state.
2140 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
2145 BiosKeyboardSetState (
2146 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
2147 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
2151 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2153 EFI_LEGACY_BIOS_PROTOCOL
*LegacyBios
;
2156 if (KeyToggleState
== NULL
) {
2157 return EFI_INVALID_PARAMETER
;
2161 // Thunk keyboard driver doesn't support partial keystroke.
2163 if ((*KeyToggleState
& EFI_TOGGLE_STATE_VALID
) != EFI_TOGGLE_STATE_VALID
||
2164 (*KeyToggleState
& EFI_KEY_STATE_EXPOSED
) == EFI_KEY_STATE_EXPOSED
2166 return EFI_UNSUPPORTED
;
2169 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This
);
2171 // See if the Legacy BIOS Protocol is available
2173 Status
= gBS
->LocateProtocol (
2174 &gEfiLegacyBiosProtocolGuid
,
2176 (VOID
**) &LegacyBios
2179 ASSERT_EFI_ERROR (Status
);
2181 // Enter critical section
2183 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
2186 if ((*KeyToggleState
& EFI_CAPS_LOCK_ACTIVE
) == EFI_CAPS_LOCK_ACTIVE
) {
2189 if ((*KeyToggleState
& EFI_NUM_LOCK_ACTIVE
) == EFI_NUM_LOCK_ACTIVE
) {
2192 if ((*KeyToggleState
& EFI_SCROLL_LOCK_ACTIVE
) == EFI_SCROLL_LOCK_ACTIVE
) {
2196 Status
= KeyboardWrite (BiosKeyboardPrivate
, 0xed);
2197 if (EFI_ERROR (Status
)) {
2198 return EFI_DEVICE_ERROR
;
2200 Status
= KeyboardWaitForValue (BiosKeyboardPrivate
, 0xfa, KEYBOARD_WAITFORVALUE_TIMEOUT
);
2201 if (EFI_ERROR (Status
)) {
2202 return EFI_DEVICE_ERROR
;
2204 Status
= KeyboardWrite (BiosKeyboardPrivate
, Command
);
2205 if (EFI_ERROR (Status
)) {
2206 return EFI_DEVICE_ERROR
;
2209 // Call Legacy BIOS Protocol to set whatever is necessary
2211 LegacyBios
->UpdateKeyboardLedStatus (LegacyBios
, Command
);
2213 Status
= EFI_SUCCESS
;
2216 // Leave critical section and return
2218 gBS
->RestoreTPL (OldTpl
);
2225 Register a notification function for a particular keystroke for the input device.
2227 @param This Protocol instance pointer.
2228 @param KeyData A pointer to a buffer that is filled in with the keystroke
2229 information data for the key that was pressed.
2230 @param KeyNotificationFunction Points to the function to be called when the key
2231 sequence is typed specified by KeyData.
2232 @param NotifyHandle Points to the unique handle assigned to the registered notification.
2235 @retval EFI_SUCCESS The notification function was registered successfully.
2236 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
2237 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.
2242 BiosKeyboardRegisterKeyNotify (
2243 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
2244 IN EFI_KEY_DATA
*KeyData
,
2245 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
2246 OUT EFI_HANDLE
*NotifyHandle
2250 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2252 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NewNotify
;
2254 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
2256 if (KeyData
== NULL
|| NotifyHandle
== NULL
|| KeyNotificationFunction
== NULL
) {
2257 return EFI_INVALID_PARAMETER
;
2260 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This
);
2263 // Enter critical section
2265 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
2268 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
2270 for (Link
= BiosKeyboardPrivate
->NotifyList
.ForwardLink
; Link
!= &BiosKeyboardPrivate
->NotifyList
; Link
= Link
->ForwardLink
) {
2271 CurrentNotify
= CR (
2273 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
2275 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
2277 if (IsKeyRegistered (&CurrentNotify
->KeyData
, KeyData
)) {
2278 if (CurrentNotify
->KeyNotificationFn
== KeyNotificationFunction
) {
2279 *NotifyHandle
= CurrentNotify
->NotifyHandle
;
2280 Status
= EFI_SUCCESS
;
2287 // Allocate resource to save the notification function
2290 NewNotify
= (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*) AllocateZeroPool (sizeof (BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
));
2291 if (NewNotify
== NULL
) {
2292 Status
= EFI_OUT_OF_RESOURCES
;
2296 NewNotify
->Signature
= BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
;
2297 NewNotify
->KeyNotificationFn
= KeyNotificationFunction
;
2298 NewNotify
->NotifyHandle
= (EFI_HANDLE
) NewNotify
;
2299 CopyMem (&NewNotify
->KeyData
, KeyData
, sizeof (EFI_KEY_DATA
));
2300 InsertTailList (&BiosKeyboardPrivate
->NotifyList
, &NewNotify
->NotifyEntry
);
2302 *NotifyHandle
= NewNotify
->NotifyHandle
;
2303 Status
= EFI_SUCCESS
;
2307 // Leave critical section and return
2309 gBS
->RestoreTPL (OldTpl
);
2314 Remove a registered notification function from a particular keystroke.
2316 @param This Protocol instance pointer.
2317 @param NotificationHandle The handle of the notification function being unregistered.
2319 @retval EFI_SUCCESS The notification function was unregistered successfully.
2320 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
2325 BiosKeyboardUnregisterKeyNotify (
2326 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
2327 IN EFI_HANDLE NotificationHandle
2331 BIOS_KEYBOARD_DEV
*BiosKeyboardPrivate
;
2334 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
2337 // Check incoming notification handle
2339 if (NotificationHandle
== NULL
) {
2340 return EFI_INVALID_PARAMETER
;
2343 if (((BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
*) NotificationHandle
)->Signature
!= BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
) {
2344 return EFI_INVALID_PARAMETER
;
2347 BiosKeyboardPrivate
= TEXT_INPUT_EX_BIOS_KEYBOARD_DEV_FROM_THIS (This
);
2350 // Enter critical section
2352 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
2354 for (Link
= BiosKeyboardPrivate
->NotifyList
.ForwardLink
; Link
!= &BiosKeyboardPrivate
->NotifyList
; Link
= Link
->ForwardLink
) {
2355 CurrentNotify
= CR (
2357 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
2359 BIOS_KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
2361 if (CurrentNotify
->NotifyHandle
== NotificationHandle
) {
2363 // Remove the notification function from NotifyList and free resources
2365 RemoveEntryList (&CurrentNotify
->NotifyEntry
);
2367 Status
= EFI_SUCCESS
;
2373 // Can not find the specified Notification Handle
2375 Status
= EFI_INVALID_PARAMETER
;
2379 // Leave critical section and return
2381 gBS
->RestoreTPL (OldTpl
);
2386 The user Entry Point for module BiosKeyboard. The user code starts with this function.
2388 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2389 @param[in] SystemTable A pointer to the EFI System Table.
2391 @retval EFI_SUCCESS The entry point is executed successfully.
2392 @retval other Some error occurs when executing this entry point.
2397 InitializeBiosKeyboard(
2398 IN EFI_HANDLE ImageHandle
,
2399 IN EFI_SYSTEM_TABLE
*SystemTable
2405 // Install driver model protocol(s).
2407 Status
= EfiLibInstallDriverBindingComponentName2 (
2410 &gBiosKeyboardDriverBinding
,
2412 &gBiosKeyboardComponentName
,
2413 &gBiosKeyboardComponentName2
2415 ASSERT_EFI_ERROR (Status
);