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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "Ps2Keyboard.h"
20 Check whether the EFI key buffer is empty.
22 @param Queue Pointer to instance of EFI_KEY_QUEUE.
24 @retval TRUE The EFI key buffer is empty.
25 @retval FALSE The EFI key buffer isn't empty.
29 IN EFI_KEY_QUEUE
*Queue
32 return (BOOLEAN
) (Queue
->Head
== Queue
->Tail
);
36 Read & remove one key data from the EFI key buffer.
38 @param Queue Pointer to instance of EFI_KEY_QUEUE.
39 @param KeyData Receive the key data.
41 @retval EFI_SUCCESS The key data is popped successfully.
42 @retval EFI_NOT_READY There is no key data available.
46 IN EFI_KEY_QUEUE
*Queue
,
47 OUT EFI_KEY_DATA
*KeyData OPTIONAL
50 if (IsEfikeyBufEmpty (Queue
)) {
54 // Retrieve and remove the values
56 if (KeyData
!= NULL
) {
57 CopyMem (KeyData
, &Queue
->Buffer
[Queue
->Head
], sizeof (EFI_KEY_DATA
));
59 Queue
->Head
= (Queue
->Head
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
;
64 Push one key data to the EFI key buffer.
66 @param Queue Pointer to instance of EFI_KEY_QUEUE.
67 @param KeyData The key data to push.
71 IN EFI_KEY_QUEUE
*Queue
,
72 IN EFI_KEY_DATA
*KeyData
75 if ((Queue
->Tail
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
== Queue
->Head
) {
77 // If Queue is full, pop the one from head.
79 PopEfikeyBufHead (Queue
, NULL
);
81 CopyMem (&Queue
->Buffer
[Queue
->Tail
], KeyData
, sizeof (EFI_KEY_DATA
));
82 Queue
->Tail
= (Queue
->Tail
+ 1) % KEYBOARD_EFI_KEY_MAX_COUNT
;
86 Judge whether is a registed key
88 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
89 state data for the key that was registered.
90 @param InputData A pointer to a buffer that is filled in with the keystroke
91 state data for the key that was pressed.
93 @retval TRUE Key be pressed matches a registered key.
94 @retval FLASE Match failed.
99 IN EFI_KEY_DATA
*RegsiteredData
,
100 IN EFI_KEY_DATA
*InputData
104 ASSERT (RegsiteredData
!= NULL
&& InputData
!= NULL
);
106 if ((RegsiteredData
->Key
.ScanCode
!= InputData
->Key
.ScanCode
) ||
107 (RegsiteredData
->Key
.UnicodeChar
!= InputData
->Key
.UnicodeChar
)) {
112 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.
114 if (RegsiteredData
->KeyState
.KeyShiftState
!= 0 &&
115 RegsiteredData
->KeyState
.KeyShiftState
!= InputData
->KeyState
.KeyShiftState
) {
118 if (RegsiteredData
->KeyState
.KeyToggleState
!= 0 &&
119 RegsiteredData
->KeyState
.KeyToggleState
!= InputData
->KeyState
.KeyToggleState
) {
128 Reads the next keystroke from the input device. The WaitForKey Event can
129 be used to test for existance of a keystroke via WaitForEvent () call.
131 @param ConsoleInDev Ps2 Keyboard private structure
132 @param KeyData A pointer to a buffer that is filled in with the keystroke
133 state data for the key that was pressed.
136 @retval EFI_SUCCESS The keystroke information was returned.
137 @retval EFI_NOT_READY There was no keystroke data availiable.
138 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
140 @retval EFI_INVALID_PARAMETER KeyData is NULL.
144 KeyboardReadKeyStrokeWorker (
145 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
,
146 OUT EFI_KEY_DATA
*KeyData
153 if (KeyData
== NULL
) {
154 return EFI_INVALID_PARAMETER
;
158 // Enter critical section
160 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
162 KeyboardTimerHandler (NULL
, ConsoleInDev
);
164 if (ConsoleInDev
->KeyboardErr
) {
165 Status
= EFI_DEVICE_ERROR
;
167 Status
= PopEfikeyBufHead (&ConsoleInDev
->EfiKeyQueue
, KeyData
);
168 if (Status
== EFI_NOT_READY
) {
169 ZeroMem (&KeyData
->Key
, sizeof (KeyData
->Key
));
170 InitializeKeyState (ConsoleInDev
, &KeyData
->KeyState
);
174 gBS
->RestoreTPL (OldTpl
);
179 Perform 8042 controller and keyboard initialization which implement SIMPLE_TEXT_IN.Reset()
181 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
182 @param ExtendedVerification Indicate that the driver may perform a more
183 exhaustive verification operation of the device during
184 reset, now this par is ignored in this driver
190 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
191 IN BOOLEAN ExtendedVerification
195 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
198 ConsoleIn
= KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
199 if (ConsoleIn
->KeyboardErr
) {
200 return EFI_DEVICE_ERROR
;
203 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
205 EFI_PERIPHERAL_KEYBOARD
| EFI_P_PC_RESET
,
206 ConsoleIn
->DevicePath
210 // Enter critical section
212 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
215 // Call InitKeyboard to initialize the keyboard
217 Status
= InitKeyboard (ConsoleIn
, ExtendedVerification
);
218 if (EFI_ERROR (Status
)) {
220 // Leave critical section and return
222 gBS
->RestoreTPL (OldTpl
);
223 return EFI_DEVICE_ERROR
;
227 // Leave critical section and return
229 gBS
->RestoreTPL (OldTpl
);
232 // Report the status If a stuck key was detected
234 if (KeyReadStatusRegister (ConsoleIn
) & 0x01) {
235 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
236 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
237 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_EC_STUCK_KEY
,
238 ConsoleIn
->DevicePath
242 // Report the status If keyboard is locked
244 if ((KeyReadStatusRegister (ConsoleIn
) & 0x10) == 0) {
245 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
246 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
247 EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_EC_LOCKED
,
248 ConsoleIn
->DevicePath
256 Retrieve key values for driver user which implement SIMPLE_TEXT_IN.ReadKeyStroke().
258 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
259 @param Key The output buffer for key value
261 @retval EFI_SUCCESS success to read key stroke
265 KeyboardReadKeyStroke (
266 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
267 OUT EFI_INPUT_KEY
*Key
271 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
272 EFI_KEY_DATA KeyData
;
274 ConsoleIn
= KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
277 // Considering if the partial keystroke is enabled, there maybe a partial
278 // keystroke in the queue, so here skip the partial keystroke and get the
279 // next key from the queue
283 // If there is no pending key, then return.
285 Status
= KeyboardReadKeyStrokeWorker (ConsoleIn
, &KeyData
);
286 if (EFI_ERROR (Status
)) {
290 // If it is partial keystroke, skip it.
292 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
296 // Translate the CTRL-Alpha characters to their corresponding control value
297 // (ctrl-a = 0x0001 through ctrl-Z = 0x001A)
299 if ((KeyData
.KeyState
.KeyShiftState
& (EFI_LEFT_CONTROL_PRESSED
| EFI_RIGHT_CONTROL_PRESSED
)) != 0) {
300 if (KeyData
.Key
.UnicodeChar
>= L
'a' && KeyData
.Key
.UnicodeChar
<= L
'z') {
301 KeyData
.Key
.UnicodeChar
= (CHAR16
) (KeyData
.Key
.UnicodeChar
- L
'a' + 1);
302 } else if (KeyData
.Key
.UnicodeChar
>= L
'A' && KeyData
.Key
.UnicodeChar
<= L
'Z') {
303 KeyData
.Key
.UnicodeChar
= (CHAR16
) (KeyData
.Key
.UnicodeChar
- L
'A' + 1);
307 CopyMem (Key
, &KeyData
.Key
, sizeof (EFI_INPUT_KEY
));
313 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
314 Signal the event if there is key available
316 @param Event the event object
317 @param Context waitting context
328 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
329 EFI_KEY_DATA KeyData
;
331 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
334 // Enter critical section
336 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
338 KeyboardTimerHandler (NULL
, ConsoleIn
);
340 if (!ConsoleIn
->KeyboardErr
) {
342 // WaitforKey doesn't suppor the partial key.
343 // Considering if the partial keystroke is enabled, there maybe a partial
344 // keystroke in the queue, so here skip the partial keystroke and get the
345 // next key from the queue
347 while (!IsEfikeyBufEmpty (&ConsoleIn
->EfiKeyQueue
)) {
350 &(ConsoleIn
->EfiKeyQueue
.Buffer
[ConsoleIn
->EfiKeyQueue
.Head
]),
351 sizeof (EFI_KEY_DATA
)
353 if (KeyData
.Key
.ScanCode
== SCAN_NULL
&& KeyData
.Key
.UnicodeChar
== CHAR_NULL
) {
354 PopEfikeyBufHead (&ConsoleIn
->EfiKeyQueue
, &KeyData
);
358 // if there is pending value key, signal the event.
360 gBS
->SignalEvent (Event
);
365 // Leave critical section and return
367 gBS
->RestoreTPL (OldTpl
);
371 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
372 Signal the event if there is key available
374 @param Event event object
375 @param Context waiting context
380 KeyboardWaitForKeyEx (
386 KeyboardWaitForKey (Event
, Context
);
390 Reset the input device and optionaly run diagnostics
392 @param This Protocol instance pointer.
393 @param ExtendedVerification Driver may perform diagnostics on reset.
395 @retval EFI_SUCCESS The device was reset.
396 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
403 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
404 IN BOOLEAN ExtendedVerification
408 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
410 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
412 return ConsoleInDev
->ConIn
.Reset (
413 &ConsoleInDev
->ConIn
,
419 Reads the next keystroke from the input device. The WaitForKey Event can
420 be used to test for existance of a keystroke via WaitForEvent () call.
423 @param This Protocol instance pointer.
424 @param KeyData A pointer to a buffer that is filled in with the keystroke
425 state data for the key that was pressed.
427 @retval EFI_SUCCESS The keystroke information was returned.
428 @retval EFI_NOT_READY There was no keystroke data availiable.
429 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
431 @retval EFI_INVALID_PARAMETER KeyData is NULL.
436 KeyboardReadKeyStrokeEx (
437 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
438 OUT EFI_KEY_DATA
*KeyData
442 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
444 if (KeyData
== NULL
) {
445 return EFI_INVALID_PARAMETER
;
448 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
449 return KeyboardReadKeyStrokeWorker (ConsoleInDev
, KeyData
);
453 Set certain state for the input device.
455 @param This Protocol instance pointer.
456 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
457 state for the input device.
459 @retval EFI_SUCCESS The device state was set successfully.
460 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
461 not have the setting adjusted.
462 @retval EFI_UNSUPPORTED The device does not have the ability to set its state.
463 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
469 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
470 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
475 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
478 if (KeyToggleState
== NULL
) {
479 return EFI_INVALID_PARAMETER
;
482 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
485 // Enter critical section
487 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
489 if (ConsoleInDev
->KeyboardErr
) {
490 Status
= EFI_DEVICE_ERROR
;
494 if ((*KeyToggleState
& EFI_TOGGLE_STATE_VALID
) != EFI_TOGGLE_STATE_VALID
) {
495 Status
= EFI_UNSUPPORTED
;
500 // Update the status light
502 ConsoleInDev
->ScrollLock
= FALSE
;
503 ConsoleInDev
->NumLock
= FALSE
;
504 ConsoleInDev
->CapsLock
= FALSE
;
505 ConsoleInDev
->IsSupportPartialKey
= FALSE
;
507 if ((*KeyToggleState
& EFI_SCROLL_LOCK_ACTIVE
) == EFI_SCROLL_LOCK_ACTIVE
) {
508 ConsoleInDev
->ScrollLock
= TRUE
;
510 if ((*KeyToggleState
& EFI_NUM_LOCK_ACTIVE
) == EFI_NUM_LOCK_ACTIVE
) {
511 ConsoleInDev
->NumLock
= TRUE
;
513 if ((*KeyToggleState
& EFI_CAPS_LOCK_ACTIVE
) == EFI_CAPS_LOCK_ACTIVE
) {
514 ConsoleInDev
->CapsLock
= TRUE
;
516 if ((*KeyToggleState
& EFI_KEY_STATE_EXPOSED
) == EFI_KEY_STATE_EXPOSED
) {
517 ConsoleInDev
->IsSupportPartialKey
= TRUE
;
520 Status
= UpdateStatusLights (ConsoleInDev
);
521 if (EFI_ERROR (Status
)) {
522 Status
= EFI_DEVICE_ERROR
;
527 // Leave critical section and return
529 gBS
->RestoreTPL (OldTpl
);
536 Register a notification function for a particular keystroke for the input device.
538 @param This Protocol instance pointer.
539 @param KeyData A pointer to a buffer that is filled in with the keystroke
540 information data for the key that was pressed. If KeyData.Key,
541 KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState
542 are 0, then any incomplete keystroke will trigger a notification of
543 the KeyNotificationFunction.
544 @param KeyNotificationFunction Points to the function to be called when the key
545 sequence is typed specified by KeyData. This notification function
546 should be called at <=TPL_CALLBACK.
547 @param NotifyHandle Points to the unique handle assigned to the registered notification.
549 @retval EFI_SUCCESS The notification function was registered successfully.
550 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
551 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
556 KeyboardRegisterKeyNotify (
557 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
558 IN EFI_KEY_DATA
*KeyData
,
559 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
560 OUT VOID
**NotifyHandle
564 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
567 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
568 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NewNotify
;
570 if (KeyData
== NULL
|| NotifyHandle
== NULL
|| KeyNotificationFunction
== NULL
) {
571 return EFI_INVALID_PARAMETER
;
574 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
577 // Enter critical section
579 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
582 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
584 for (Link
= ConsoleInDev
->NotifyList
.ForwardLink
; Link
!= &ConsoleInDev
->NotifyList
; Link
= Link
->ForwardLink
) {
587 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
589 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
591 if (IsKeyRegistered (&CurrentNotify
->KeyData
, KeyData
)) {
592 if (CurrentNotify
->KeyNotificationFn
== KeyNotificationFunction
) {
593 *NotifyHandle
= CurrentNotify
;
594 Status
= EFI_SUCCESS
;
601 // Allocate resource to save the notification function
603 NewNotify
= (KEYBOARD_CONSOLE_IN_EX_NOTIFY
*) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY
));
604 if (NewNotify
== NULL
) {
605 Status
= EFI_OUT_OF_RESOURCES
;
609 NewNotify
->Signature
= KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
;
610 NewNotify
->KeyNotificationFn
= KeyNotificationFunction
;
611 CopyMem (&NewNotify
->KeyData
, KeyData
, sizeof (EFI_KEY_DATA
));
612 InsertTailList (&ConsoleInDev
->NotifyList
, &NewNotify
->NotifyEntry
);
614 *NotifyHandle
= NewNotify
;
615 Status
= EFI_SUCCESS
;
619 // Leave critical section and return
621 gBS
->RestoreTPL (OldTpl
);
627 Remove a registered notification function from a particular keystroke.
629 @param This Protocol instance pointer.
630 @param NotificationHandle The handle of the notification function being unregistered.
633 @retval EFI_SUCCESS The notification function was unregistered successfully.
634 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
639 KeyboardUnregisterKeyNotify (
640 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
641 IN VOID
*NotificationHandle
645 KEYBOARD_CONSOLE_IN_DEV
*ConsoleInDev
;
648 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
650 if (NotificationHandle
== NULL
) {
651 return EFI_INVALID_PARAMETER
;
654 ConsoleInDev
= TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS (This
);
657 // Enter critical section
659 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
661 for (Link
= ConsoleInDev
->NotifyList
.ForwardLink
; Link
!= &ConsoleInDev
->NotifyList
; Link
= Link
->ForwardLink
) {
664 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
666 KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
668 if (CurrentNotify
== NotificationHandle
) {
670 // Remove the notification function from NotifyList and free resources
672 RemoveEntryList (&CurrentNotify
->NotifyEntry
);
674 gBS
->FreePool (CurrentNotify
);
675 Status
= EFI_SUCCESS
;
681 // Can not find the specified Notification Handle
683 Status
= EFI_INVALID_PARAMETER
;
686 // Leave critical section and return
688 gBS
->RestoreTPL (OldTpl
);
695 @param Event Indicates the event that invoke this function.
696 @param Context Indicates the calling context.
700 KeyNotifyProcessHandler (
706 KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
;
707 EFI_KEY_DATA KeyData
;
709 LIST_ENTRY
*NotifyList
;
710 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
713 ConsoleIn
= (KEYBOARD_CONSOLE_IN_DEV
*) Context
;
716 // Invoke notification functions.
718 NotifyList
= &ConsoleIn
->NotifyList
;
721 // Enter critical section
723 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
724 Status
= PopEfikeyBufHead (&ConsoleIn
->EfiKeyQueueForNotify
, &KeyData
);
726 // Leave critical section
728 gBS
->RestoreTPL (OldTpl
);
729 if (EFI_ERROR (Status
)) {
732 for (Link
= GetFirstNode (NotifyList
); !IsNull (NotifyList
, Link
); Link
= GetNextNode (NotifyList
, Link
)) {
733 CurrentNotify
= CR (Link
, KEYBOARD_CONSOLE_IN_EX_NOTIFY
, NotifyEntry
, KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE
);
734 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
735 CurrentNotify
->KeyNotificationFn (&KeyData
);