2 Routines implements SIMPLE_TEXT_IN protocol's interfaces based on 8042 interfaces
3 provided by Ps2KbdCtrller.c.
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "Ps2Keyboard.h"
14 Check whether the EFI key buffer is empty.
16 @param Queue Pointer to instance of EFI_KEY_QUEUE.
18 @retval TRUE The EFI key buffer is empty.
19 @retval FALSE The EFI key buffer isn't empty.
23 IN EFI_KEY_QUEUE
*Queue
26 return (BOOLEAN
) (Queue
->Head
== Queue
->Tail
);
30 Read & remove one key data from the EFI key buffer.
32 @param Queue Pointer to instance of EFI_KEY_QUEUE.
33 @param KeyData Receive the key data.
35 @retval EFI_SUCCESS The key data is popped successfully.
36 @retval EFI_NOT_READY There is no key data available.
40 IN EFI_KEY_QUEUE
*Queue
,
41 OUT EFI_KEY_DATA
*KeyData OPTIONAL
44 if (IsEfikeyBufEmpty (Queue
)) {
48 // Retrieve and remove the values
50 if (KeyData
!= NULL
) {
51 CopyMem (KeyData
, &Queue
->Buffer
[Queue
->Head
], sizeof (EFI_KEY_DATA
));
53 Queue
->Head
= (Queue
->Head
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
;
58 Push one key data to the EFI key buffer.
60 @param Queue Pointer to instance of EFI_KEY_QUEUE.
61 @param KeyData The key data to push.
65 IN EFI_KEY_QUEUE
*Queue
,
66 IN EFI_KEY_DATA
*KeyData
69 if ((Queue
->Tail
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
== Queue
->Head
) {
71 // If Queue is full, pop the one from head.
73 PopEfikeyBufHead (Queue
, NULL
);
75 CopyMem (&Queue
->Buffer
[Queue
->Tail
], KeyData
, sizeof (EFI_KEY_DATA
));
76 Queue
->Tail
= (Queue
->Tail
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
;
80 Judge whether is a registed key
82 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
83 state data for the key that was registered.
84 @param InputData A pointer to a buffer that is filled in with the keystroke
85 state data for the key that was pressed.
87 @retval TRUE Key be pressed matches a registered key.
88 @retval FLASE Match failed.
93 IN EFI_KEY_DATA
*RegsiteredData
,
94 IN EFI_KEY_DATA
*InputData
98 ASSERT (RegsiteredData
!= NULL
&& InputData
!= NULL
);
100 if ((RegsiteredData
->Key
.ScanCode
!= InputData
->Key
.ScanCode
) ||
101 (RegsiteredData
->Key
.UnicodeChar
!= InputData
->Key
.UnicodeChar
)) {
106 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.
108 if (RegsiteredData
->KeyState
.KeyShiftState
!= 0 &&
109 RegsiteredData
->KeyState
.KeyShiftState
!= InputData
->KeyState
.KeyShiftState
) {
112 if (RegsiteredData
->KeyState
.KeyToggleState
!= 0 &&
113 RegsiteredData
->KeyState
.KeyToggleState
!= InputData
->KeyState
.KeyToggleState
) {
122 Reads the next keystroke from the input device. The WaitForKey Event can
123 be used to test for existance of a keystroke via WaitForEvent () call.
125 @param ConsoleInDev Ps2 Keyboard private structure
126 @param KeyData A pointer to a buffer that is filled in with the keystroke
127 state data for the key that was pressed.
130 @retval EFI_SUCCESS The keystroke information was returned.
131 @retval EFI_NOT_READY There was no keystroke data availiable.
132 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
134 @retval EFI_INVALID_PARAMETER KeyData is NULL.
138 KeyboardReadKeyStrokeWorker (
139 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
,
140 OUT EFI_KEY_DATA
*KeyData
147 if (KeyData
== NULL
) {
148 return EFI_INVALID_PARAMETER
;
152 // Enter critical section
154 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
156 KeyboardTimerHandler (NULL
, ConsoleInDev
);
158 if (ConsoleInDev
->KeyboardErr
) {
159 Status
= EFI_DEVICE_ERROR
;
161 Status
= PopEfikeyBufHead (&ConsoleInDev
->EfiKeyQueue
, KeyData
);
162 if (Status
== EFI_NOT_READY
) {
163 ZeroMem (&KeyData
->Key
, sizeof (KeyData
->Key
));
164 InitializeKeyState (ConsoleInDev
, &KeyData
->KeyState
);
168 gBS
->RestoreTPL (OldTpl
);
173 Perform 8042 controller and keyboard initialization which implement SIMPLE_TEXT_IN.Reset()
175 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
176 @param ExtendedVerification Indicate that the driver may perform a more
177 exhaustive verification operation of the device during
178 reset, now this par is ignored in this driver
184 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
185 IN BOOLEAN ExtendedVerification
189 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
192 ConsoleIn
= KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
193 if (ConsoleIn
->KeyboardErr
) {
194 return EFI_DEVICE_ERROR
;
197 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
199 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_RESET
,
200 ConsoleIn
->DevicePath
204 // Enter critical section
206 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
209 // Call InitKeyboard to initialize the keyboard
211 Status
= InitKeyboard (ConsoleIn
, ExtendedVerification
);
212 if (EFI_ERROR (Status
)) {
214 // Leave critical section and return
216 gBS
->RestoreTPL (OldTpl
);
217 return EFI_DEVICE_ERROR
;
221 // Leave critical section and return
223 gBS
->RestoreTPL (OldTpl
);
226 // Report the status If a stuck key was detected
228 if (KeyReadStatusRegister (ConsoleIn
) & 0x01) {
229 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
230 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
231 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_EC_STUCK_KEY
,
232 ConsoleIn
->DevicePath
236 // Report the status If keyboard is locked
238 if ((KeyReadStatusRegister (ConsoleIn
) & 0x10) == 0) {
239 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
240 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
241 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_EC_LOCKED
,
242 ConsoleIn
->DevicePath
250 Retrieve key values for driver user which implement SIMPLE_TEXT_IN.ReadKeyStroke().
252 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
253 @param Key The output buffer for key value
255 @retval EFI_SUCCESS success to read key stroke
259 KeyboardReadKeyStroke (
260 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
261 OUT EFI_INPUT_KEY
*Key
265 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
266 EFI_KEY_DATA KeyData
;
268 ConsoleIn
= KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
271 // Considering if the partial keystroke is enabled, there maybe a partial
272 // keystroke in the queue, so here skip the partial keystroke and get the
273 // next key from the queue
277 // If there is no pending key, then return.
279 Status
= KeyboardReadKeyStrokeWorker (ConsoleIn
, &KeyData
);
280 if (EFI_ERROR (Status
)) {
284 // If it is partial keystroke, skip it.
286 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
290 // Translate the CTRL-Alpha characters to their corresponding control value
291 // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)
293 if ((KeyData
.KeyState
.KeyShiftState
& (EFI_LEFT_CONTROL_PRESSED
| EFI_RIGHT_CONTROL_PRESSED
)) != 0) {
294 if (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z') {
295 KeyData
.Key
.UnicodeChar
= (CHAR16
) (KeyData
.Key
.UnicodeChar
- L
'a' + 1);
296 } else if (KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') {
297 KeyData
.Key
.UnicodeChar
= (CHAR16
) (KeyData
.Key
.UnicodeChar
- L
'A' + 1);
301 CopyMem (Key
, &KeyData
.Key
, sizeof (EFI_INPUT_KEY
));
307 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
308 Signal the event if there is key available
310 @param Event the event object
311 @param Context waitting context
322 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
323 EFI_KEY_DATA KeyData
;
325 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
328 // Enter critical section
330 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
332 KeyboardTimerHandler (NULL
, ConsoleIn
);
334 if (!ConsoleIn
->KeyboardErr
) {
336 // WaitforKey doesn't suppor the partial key.
337 // Considering if the partial keystroke is enabled, there maybe a partial
338 // keystroke in the queue, so here skip the partial keystroke and get the
339 // next key from the queue
341 while (!IsEfikeyBufEmpty (&ConsoleIn
->EfiKeyQueue
)) {
344 &(ConsoleIn
->EfiKeyQueue
.Buffer
[ConsoleIn
->EfiKeyQueue
.Head
]),
345 sizeof (EFI_KEY_DATA
)
347 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
348 PopEfikeyBufHead (&ConsoleIn
->EfiKeyQueue
, &KeyData
);
352 // if there is pending value key, signal the event.
354 gBS
->SignalEvent (Event
);
359 // Leave critical section and return
361 gBS
->RestoreTPL (OldTpl
);
365 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
366 Signal the event if there is key available
368 @param Event event object
369 @param Context waiting context
374 KeyboardWaitForKeyEx (
380 KeyboardWaitForKey (Event
, Context
);
384 Reset the input device and optionaly run diagnostics
386 @param This Protocol instance pointer.
387 @param ExtendedVerification Driver may perform diagnostics on reset.
389 @retval EFI_SUCCESS The device was reset.
390 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
397 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
398 IN BOOLEAN ExtendedVerification
402 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
404 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
406 return ConsoleInDev
->ConIn
.Reset (
407 &ConsoleInDev
->ConIn
,
413 Reads the next keystroke from the input device. The WaitForKey Event can
414 be used to test for existance of a keystroke via WaitForEvent () call.
417 @param This Protocol instance pointer.
418 @param KeyData A pointer to a buffer that is filled in with the keystroke
419 state data for the key that was pressed.
421 @retval EFI_SUCCESS The keystroke information was returned.
422 @retval EFI_NOT_READY There was no keystroke data availiable.
423 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
425 @retval EFI_INVALID_PARAMETER KeyData is NULL.
430 KeyboardReadKeyStrokeEx (
431 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
432 OUT EFI_KEY_DATA
*KeyData
436 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
438 if (KeyData
== NULL
) {
439 return EFI_INVALID_PARAMETER
;
442 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
443 return KeyboardReadKeyStrokeWorker (ConsoleInDev
, KeyData
);
447 Set certain state for the input device.
449 @param This Protocol instance pointer.
450 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
451 state for the input device.
453 @retval EFI_SUCCESS The device state was set successfully.
454 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
455 not have the setting adjusted.
456 @retval EFI_UNSUPPORTED The device does not have the ability to set its state.
457 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
463 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
464 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
469 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
472 if (KeyToggleState
== NULL
) {
473 return EFI_INVALID_PARAMETER
;
476 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
479 // Enter critical section
481 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
483 if (ConsoleInDev
->KeyboardErr
) {
484 Status
= EFI_DEVICE_ERROR
;
488 if ((*KeyToggleState
& EFI_TOGGLE_STATE_VALID
) != EFI_TOGGLE_STATE_VALID
) {
489 Status
= EFI_UNSUPPORTED
;
494 // Update the status light
496 ConsoleInDev
->ScrollLock
= FALSE
;
497 ConsoleInDev
->NumLock
= FALSE
;
498 ConsoleInDev
->CapsLock
= FALSE
;
499 ConsoleInDev
->IsSupportPartialKey
= FALSE
;
501 if ((*KeyToggleState
& EFI_SCROLL_LOCK_ACTIVE
) == EFI_SCROLL_LOCK_ACTIVE
) {
502 ConsoleInDev
->ScrollLock
= TRUE
;
504 if ((*KeyToggleState
& EFI_NUM_LOCK_ACTIVE
) == EFI_NUM_LOCK_ACTIVE
) {
505 ConsoleInDev
->NumLock
= TRUE
;
507 if ((*KeyToggleState
& EFI_CAPS_LOCK_ACTIVE
) == EFI_CAPS_LOCK_ACTIVE
) {
508 ConsoleInDev
->CapsLock
= TRUE
;
510 if ((*KeyToggleState
& EFI_KEY_STATE_EXPOSED
) == EFI_KEY_STATE_EXPOSED
) {
511 ConsoleInDev
->IsSupportPartialKey
= TRUE
;
514 Status
= UpdateStatusLights (ConsoleInDev
);
515 if (EFI_ERROR (Status
)) {
516 Status
= EFI_DEVICE_ERROR
;
521 // Leave critical section and return
523 gBS
->RestoreTPL (OldTpl
);
530 Register a notification function for a particular keystroke for the input device.
532 @param This Protocol instance pointer.
533 @param KeyData A pointer to a buffer that is filled in with the keystroke
534 information data for the key that was pressed. If KeyData.Key,
535 KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState
536 are 0, then any incomplete keystroke will trigger a notification of
537 the KeyNotificationFunction.
538 @param KeyNotificationFunction Points to the function to be called when the key
539 sequence is typed specified by KeyData. This notification function
540 should be called at <=TPL_CALLBACK.
541 @param NotifyHandle Points to the unique handle assigned to the registered notification.
543 @retval EFI_SUCCESS The notification function was registered successfully.
544 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
545 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
550 KeyboardRegisterKeyNotify (
551 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
552 IN EFI_KEY_DATA
*KeyData
,
553 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
554 OUT VOID
**NotifyHandle
558 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
561 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
562 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NewNotify
;
564 if (KeyData
== NULL
|| NotifyHandle
== NULL
|| KeyNotificationFunction
== NULL
) {
565 return EFI_INVALID_PARAMETER
;
568 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
571 // Enter critical section
573 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
576 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
578 for (Link
= ConsoleInDev
->NotifyList
.ForwardLink
; Link
!= &ConsoleInDev
->NotifyList
; Link
= Link
->ForwardLink
) {
581 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
583 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
585 if (IsKeyRegistered (&CurrentNotify
->KeyData
, KeyData
)) {
586 if (CurrentNotify
->KeyNotificationFn
== KeyNotificationFunction
) {
587 *NotifyHandle
= CurrentNotify
;
588 Status
= EFI_SUCCESS
;
595 // Allocate resource to save the notification function
597 NewNotify
= (KEYBOARD_CONSOLE_IN_EX_NOTIFY
*) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY
));
598 if (NewNotify
== NULL
) {
599 Status
= EFI_OUT_OF_RESOURCES
;
603 NewNotify
->Signature
= KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
;
604 NewNotify
->KeyNotificationFn
= KeyNotificationFunction
;
605 CopyMem (&NewNotify
->KeyData
, KeyData
, sizeof (EFI_KEY_DATA
));
606 InsertTailList (&ConsoleInDev
->NotifyList
, &NewNotify
->NotifyEntry
);
608 *NotifyHandle
= NewNotify
;
609 Status
= EFI_SUCCESS
;
613 // Leave critical section and return
615 gBS
->RestoreTPL (OldTpl
);
621 Remove a registered notification function from a particular keystroke.
623 @param This Protocol instance pointer.
624 @param NotificationHandle The handle of the notification function being unregistered.
627 @retval EFI_SUCCESS The notification function was unregistered successfully.
628 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
633 KeyboardUnregisterKeyNotify (
634 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
635 IN VOID
*NotificationHandle
639 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
642 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
644 if (NotificationHandle
== NULL
) {
645 return EFI_INVALID_PARAMETER
;
648 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
651 // Enter critical section
653 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
655 for (Link
= ConsoleInDev
->NotifyList
.ForwardLink
; Link
!= &ConsoleInDev
->NotifyList
; Link
= Link
->ForwardLink
) {
658 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
660 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
662 if (CurrentNotify
== NotificationHandle
) {
664 // Remove the notification function from NotifyList and free resources
666 RemoveEntryList (&CurrentNotify
->NotifyEntry
);
668 gBS
->FreePool (CurrentNotify
);
669 Status
= EFI_SUCCESS
;
675 // Can not find the specified Notification Handle
677 Status
= EFI_INVALID_PARAMETER
;
680 // Leave critical section and return
682 gBS
->RestoreTPL (OldTpl
);
689 @param Event Indicates the event that invoke this function.
690 @param Context Indicates the calling context.
694 KeyNotifyProcessHandler (
700 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
701 EFI_KEY_DATA KeyData
;
703 LIST_ENTRY
*NotifyList
;
704 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
707 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
710 // Invoke notification functions.
712 NotifyList
= &ConsoleIn
->NotifyList
;
715 // Enter critical section
717 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
718 Status
= PopEfikeyBufHead (&ConsoleIn
->EfiKeyQueueForNotify
, &KeyData
);
720 // Leave critical section
722 gBS
->RestoreTPL (OldTpl
);
723 if (EFI_ERROR (Status
)) {
726 for (Link
= GetFirstNode (NotifyList
); !IsNull (NotifyList
, Link
); Link
= GetNextNode (NotifyList
, Link
)) {
727 CurrentNotify
= CR (Link
, KEYBOARD_CONSOLE_IN_EX_NOTIFY
, NotifyEntry
, KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
);
728 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
729 CurrentNotify
->KeyNotificationFn (&KeyData
);