2 USB Keyboard Driver that manages USB keyboard and produces Simple Text Input
3 Protocol and Simple Text Input Ex Protocol.
5 Copyright (c) 2004 - 2008, Intel Corporation
6 All rights reserved. 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.
20 // USB Keyboard Driver Global Variables
22 EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding
= {
23 USBKeyboardDriverBindingSupported
,
24 USBKeyboardDriverBindingStart
,
25 USBKeyboardDriverBindingStop
,
32 Entrypoint of USB Keyboard Driver.
34 This function is the entrypoint of USB Keyboard Driver. It installs Driver Binding
35 Protocols together with Component Name Protocols.
37 @param ImageHandle The firmware allocated handle for the EFI image.
38 @param SystemTable A pointer to the EFI System Table.
40 @retval EFI_SUCCESS The entry point is executed successfully.
45 USBKeyboardDriverBindingEntryPoint (
46 IN EFI_HANDLE ImageHandle
,
47 IN EFI_SYSTEM_TABLE
*SystemTable
52 Status
= EfiLibInstallDriverBindingComponentName2 (
55 &gUsbKeyboardDriverBinding
,
57 &gUsbKeyboardComponentName
,
58 &gUsbKeyboardComponentName2
60 ASSERT_EFI_ERROR (Status
);
66 Check whether USB keyboard driver supports this device.
68 @param This The USB keyboard driver binding protocol.
69 @param Controller The controller handle to check.
70 @param RemainingDevicePath The remaining device path.
72 @retval EFI_SUCCESS The driver supports this controller.
73 @retval other This device isn't supported.
78 USBKeyboardDriverBindingSupported (
79 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
80 IN EFI_HANDLE Controller
,
81 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
85 EFI_USB_IO_PROTOCOL
*UsbIo
;
88 // Check if USB I/O Protocol is attached on the controller handle.
90 Status
= gBS
->OpenProtocol (
92 &gEfiUsbIoProtocolGuid
,
94 This
->DriverBindingHandle
,
96 EFI_OPEN_PROTOCOL_BY_DRIVER
98 if (EFI_ERROR (Status
)) {
103 // Use the USB I/O Protocol interface to check whether Controller is
104 // a keyboard device that can be managed by this driver.
106 Status
= EFI_SUCCESS
;
108 if (!IsUSBKeyboard (UsbIo
)) {
109 Status
= EFI_UNSUPPORTED
;
114 &gEfiUsbIoProtocolGuid
,
115 This
->DriverBindingHandle
,
123 Starts the device with this driver.
125 This function produces Simple Text Input Protocol and Simple Text Input Ex Protocol,
126 initializes the keyboard device, and submit Asynchronous Interrupt Transfer to manage
127 this keyboard device.
129 @param This The USB keyboard driver binding instance.
130 @param Controller Handle of device to bind driver to.
131 @param RemainingDevicePath Optional parameter use to pick a specific child
134 @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver.
135 @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
136 @retval Other This controller cannot be started.
141 USBKeyboardDriverBindingStart (
142 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
143 IN EFI_HANDLE Controller
,
144 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
148 EFI_USB_IO_PROTOCOL
*UsbIo
;
149 USB_KB_DEV
*UsbKeyboardDevice
;
150 UINT8 EndpointNumber
;
151 EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor
;
154 UINT8 PollingInterval
;
159 // Open USB I/O Protocol
161 Status
= gBS
->OpenProtocol (
163 &gEfiUsbIoProtocolGuid
,
165 This
->DriverBindingHandle
,
167 EFI_OPEN_PROTOCOL_BY_DRIVER
169 if (EFI_ERROR (Status
)) {
173 UsbKeyboardDevice
= AllocateZeroPool (sizeof (USB_KB_DEV
));
174 ASSERT (UsbKeyboardDevice
!= NULL
);
177 // Get the Device Path Protocol on Controller's handle
179 Status
= gBS
->OpenProtocol (
181 &gEfiDevicePathProtocolGuid
,
182 (VOID
**) &UsbKeyboardDevice
->DevicePath
,
183 This
->DriverBindingHandle
,
185 EFI_OPEN_PROTOCOL_GET_PROTOCOL
188 if (EFI_ERROR (Status
)) {
192 // Report that the USB keyboard is being enabled
194 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
196 PcdGet32 (PcdStatusCodeValueKeyboardEnable
),
197 UsbKeyboardDevice
->DevicePath
201 // This is pretty close to keyboard detection, so log progress
203 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
205 PcdGet32 (PcdStatusCodeValueKeyboardPresenceDetect
),
206 UsbKeyboardDevice
->DevicePath
209 UsbKeyboardDevice
->UsbIo
= UsbIo
;
212 // Get interface & endpoint descriptor
214 UsbIo
->UsbGetInterfaceDescriptor (
216 &UsbKeyboardDevice
->InterfaceDescriptor
219 EndpointNumber
= UsbKeyboardDevice
->InterfaceDescriptor
.NumEndpoints
;
222 // Traverse endpoints to find interrupt endpoints
225 for (Index
= 0; Index
< EndpointNumber
; Index
++) {
227 UsbIo
->UsbGetEndpointDescriptor (
233 if ((EndpointDescriptor
.Attributes
& 0x03) == USB_ENDPOINT_INTERRUPT
) {
235 // We only care interrupt endpoint here
237 CopyMem(&UsbKeyboardDevice
->IntEndpointDescriptor
, &EndpointDescriptor
, sizeof(EndpointDescriptor
));
245 // No interrupt endpoint found, then return unsupported.
247 Status
= EFI_UNSUPPORTED
;
251 UsbKeyboardDevice
->Signature
= USB_KB_DEV_SIGNATURE
;
252 UsbKeyboardDevice
->SimpleInput
.Reset
= USBKeyboardReset
;
253 UsbKeyboardDevice
->SimpleInput
.ReadKeyStroke
= USBKeyboardReadKeyStroke
;
255 UsbKeyboardDevice
->SimpleInputEx
.Reset
= USBKeyboardResetEx
;
256 UsbKeyboardDevice
->SimpleInputEx
.ReadKeyStrokeEx
= USBKeyboardReadKeyStrokeEx
;
257 UsbKeyboardDevice
->SimpleInputEx
.SetState
= USBKeyboardSetState
;
258 UsbKeyboardDevice
->SimpleInputEx
.RegisterKeyNotify
= USBKeyboardRegisterKeyNotify
;
259 UsbKeyboardDevice
->SimpleInputEx
.UnregisterKeyNotify
= USBKeyboardUnregisterKeyNotify
;
261 InitializeListHead (&UsbKeyboardDevice
->NotifyList
);
263 Status
= gBS
->CreateEvent (
266 USBKeyboardWaitForKey
,
268 &(UsbKeyboardDevice
->SimpleInputEx
.WaitForKeyEx
)
271 if (EFI_ERROR (Status
)) {
275 Status
= gBS
->CreateEvent (
278 USBKeyboardWaitForKey
,
280 &(UsbKeyboardDevice
->SimpleInput
.WaitForKey
)
283 if (EFI_ERROR (Status
)) {
287 Status
= InitKeyboardLayout (UsbKeyboardDevice
);
288 if (EFI_ERROR (Status
)) {
293 // Install Simple Text Input Protocol and Simple Text Input Ex Protocol
294 // for the USB keyboard device.
295 // USB keyboard is a hot plug device, and expected to work immediately
296 // when plugging into system, so a HotPlugDeviceGuid is installed onto
297 // the usb keyboard device handle, to distinguish it from other conventional
300 Status
= gBS
->InstallMultipleProtocolInterfaces (
302 &gEfiSimpleTextInProtocolGuid
,
303 &UsbKeyboardDevice
->SimpleInput
,
304 &gEfiSimpleTextInputExProtocolGuid
,
305 &UsbKeyboardDevice
->SimpleInputEx
,
306 &gEfiHotPlugDeviceGuid
,
310 if (EFI_ERROR (Status
)) {
315 // Reset USB Keyboard Device exhaustively.
317 Status
= UsbKeyboardDevice
->SimpleInput
.Reset (
318 &UsbKeyboardDevice
->SimpleInput
,
321 if (EFI_ERROR (Status
)) {
322 gBS
->UninstallMultipleProtocolInterfaces (
324 &gEfiSimpleTextInProtocolGuid
,
325 &UsbKeyboardDevice
->SimpleInput
,
326 &gEfiSimpleTextInputExProtocolGuid
,
327 &UsbKeyboardDevice
->SimpleInputEx
,
328 &gEfiHotPlugDeviceGuid
,
336 // Submit Asynchronous Interrupt Transfer to manage this device.
338 EndpointAddr
= UsbKeyboardDevice
->IntEndpointDescriptor
.EndpointAddress
;
339 PollingInterval
= UsbKeyboardDevice
->IntEndpointDescriptor
.Interval
;
340 PacketSize
= (UINT8
) (UsbKeyboardDevice
->IntEndpointDescriptor
.MaxPacketSize
);
342 Status
= UsbIo
->UsbAsyncInterruptTransfer (
352 if (EFI_ERROR (Status
)) {
353 gBS
->UninstallMultipleProtocolInterfaces (
355 &gEfiSimpleTextInProtocolGuid
,
356 &UsbKeyboardDevice
->SimpleInput
,
357 &gEfiSimpleTextInputExProtocolGuid
,
358 &UsbKeyboardDevice
->SimpleInputEx
,
359 &gEfiHotPlugDeviceGuid
,
366 UsbKeyboardDevice
->ControllerNameTable
= NULL
;
369 gUsbKeyboardComponentName
.SupportedLanguages
,
370 &UsbKeyboardDevice
->ControllerNameTable
,
371 L
"Generic Usb Keyboard",
376 gUsbKeyboardComponentName2
.SupportedLanguages
,
377 &UsbKeyboardDevice
->ControllerNameTable
,
378 L
"Generic Usb Keyboard",
388 if (UsbKeyboardDevice
!= NULL
) {
389 if (UsbKeyboardDevice
->SimpleInput
.WaitForKey
!= NULL
) {
390 gBS
->CloseEvent (UsbKeyboardDevice
->SimpleInput
.WaitForKey
);
392 if (UsbKeyboardDevice
->SimpleInputEx
.WaitForKeyEx
!= NULL
) {
393 gBS
->CloseEvent (UsbKeyboardDevice
->SimpleInputEx
.WaitForKeyEx
);
395 gBS
->FreePool (UsbKeyboardDevice
);
396 UsbKeyboardDevice
= NULL
;
400 &gEfiUsbIoProtocolGuid
,
401 This
->DriverBindingHandle
,
410 Stop the USB keyboard device handled by this driver.
412 @param This The USB keyboard driver binding protocol.
413 @param Controller The controller to release.
414 @param NumberOfChildren The number of handles in ChildHandleBuffer.
415 @param ChildHandleBuffer The array of child handle.
417 @retval EFI_SUCCESS The device was stopped.
418 @retval EFI_UNSUPPORTED Simple Text In Protocol or Simple Text In Ex Protocol
419 is not installed on Controller.
420 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
421 @retval Others Fail to uninstall protocols attached on the device.
426 USBKeyboardDriverBindingStop (
427 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
428 IN EFI_HANDLE Controller
,
429 IN UINTN NumberOfChildren
,
430 IN EFI_HANDLE
*ChildHandleBuffer
434 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*SimpleInput
;
435 USB_KB_DEV
*UsbKeyboardDevice
;
437 Status
= gBS
->OpenProtocol (
439 &gEfiSimpleTextInProtocolGuid
,
440 (VOID
**) &SimpleInput
,
441 This
->DriverBindingHandle
,
443 EFI_OPEN_PROTOCOL_GET_PROTOCOL
445 if (EFI_ERROR (Status
)) {
446 return EFI_UNSUPPORTED
;
449 Status
= gBS
->OpenProtocol (
451 &gEfiSimpleTextInputExProtocolGuid
,
453 This
->DriverBindingHandle
,
455 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
457 if (EFI_ERROR (Status
)) {
458 return EFI_UNSUPPORTED
;
461 UsbKeyboardDevice
= USB_KB_DEV_FROM_THIS (SimpleInput
);
464 // The key data input from this device will be disabled.
466 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
468 PcdGet32 (PcdStatusCodeValueKeyboardDisable
),
469 UsbKeyboardDevice
->DevicePath
473 // Delete the Asynchronous Interrupt Transfer from this device
475 UsbKeyboardDevice
->UsbIo
->UsbAsyncInterruptTransfer (
476 UsbKeyboardDevice
->UsbIo
,
477 UsbKeyboardDevice
->IntEndpointDescriptor
.EndpointAddress
,
479 UsbKeyboardDevice
->IntEndpointDescriptor
.Interval
,
487 &gEfiUsbIoProtocolGuid
,
488 This
->DriverBindingHandle
,
492 Status
= gBS
->UninstallMultipleProtocolInterfaces (
494 &gEfiSimpleTextInProtocolGuid
,
495 &UsbKeyboardDevice
->SimpleInput
,
496 &gEfiSimpleTextInputExProtocolGuid
,
497 &UsbKeyboardDevice
->SimpleInputEx
,
498 &gEfiHotPlugDeviceGuid
,
503 // Free all resources.
505 gBS
->CloseEvent (UsbKeyboardDevice
->RepeatTimer
);
506 gBS
->CloseEvent (UsbKeyboardDevice
->DelayedRecoveryEvent
);
507 gBS
->CloseEvent ((UsbKeyboardDevice
->SimpleInput
).WaitForKey
);
508 gBS
->CloseEvent (UsbKeyboardDevice
->SimpleInputEx
.WaitForKeyEx
);
509 KbdFreeNotifyList (&UsbKeyboardDevice
->NotifyList
);
511 ReleaseKeyboardLayoutResources (UsbKeyboardDevice
);
512 gBS
->CloseEvent (UsbKeyboardDevice
->KeyboardLayoutEvent
);
514 if (UsbKeyboardDevice
->ControllerNameTable
!= NULL
) {
515 FreeUnicodeStringTable (UsbKeyboardDevice
->ControllerNameTable
);
518 gBS
->FreePool (UsbKeyboardDevice
);
524 Internal function to read the next keystroke from the keyboard buffer.
526 @param UsbKeyboardDevice USB keyboard's private structure.
527 @param KeyData A pointer to buffer to hold the keystroke
528 data for the key that was pressed.
530 @retval EFI_SUCCESS The keystroke information was returned.
531 @retval EFI_NOT_READY There was no keystroke data availiable.
532 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
534 @retval EFI_INVALID_PARAMETER KeyData is NULL.
535 @retval Others Fail to translate keycode into EFI_INPUT_KEY
540 USBKeyboardReadKeyStrokeWorker (
541 IN OUT USB_KB_DEV
*UsbKeyboardDevice
,
542 OUT EFI_KEY_DATA
*KeyData
548 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
549 EFI_KEY_DATA OriginalKeyData
;
551 if (KeyData
== NULL
) {
552 return EFI_INVALID_PARAMETER
;
556 // If there is no saved USB keycode, fetch it
557 // by calling USBKeyboardCheckForKey().
559 if (UsbKeyboardDevice
->CurKeyCode
== 0) {
560 Status
= USBKeyboardCheckForKey (UsbKeyboardDevice
);
561 if (EFI_ERROR (Status
)) {
562 return EFI_NOT_READY
;
566 KeyData
->Key
.UnicodeChar
= 0;
567 KeyData
->Key
.ScanCode
= SCAN_NULL
;
570 // Store the current keycode and clear it.
572 KeyCode
= UsbKeyboardDevice
->CurKeyCode
;
573 UsbKeyboardDevice
->CurKeyCode
= 0;
576 // Translate saved USB keycode into EFI_INPUT_KEY
578 Status
= UsbKeyCodeToEfiInputKey (UsbKeyboardDevice
, KeyCode
, &KeyData
->Key
);
579 if (EFI_ERROR (Status
)) {
584 // Get current state of various toggled attributes as well as input modifier values,
585 // and set them as valid.
587 CopyMem (&KeyData
->KeyState
, &UsbKeyboardDevice
->KeyState
, sizeof (KeyData
->KeyState
));
589 UsbKeyboardDevice
->KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
;
590 UsbKeyboardDevice
->KeyState
.KeyToggleState
= EFI_TOGGLE_STATE_VALID
;
593 // Switch the control value to their original characters.
594 // In UsbKeyCodeToEfiInputKey() the CTRL-Alpha characters have been switched to
595 // their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A),
596 // here switch them back for notification function.
598 CopyMem (&OriginalKeyData
, KeyData
, sizeof (EFI_KEY_DATA
));
599 if (UsbKeyboardDevice
->CtrlOn
) {
600 if (OriginalKeyData
.Key
.UnicodeChar
>= 0x01 && OriginalKeyData
.Key
.UnicodeChar
<= 0x1A) {
601 if (UsbKeyboardDevice
->CapsOn
) {
602 OriginalKeyData
.Key
.UnicodeChar
= (CHAR16
)(OriginalKeyData
.Key
.UnicodeChar
+ 'A' - 1);
604 OriginalKeyData
.Key
.UnicodeChar
= (CHAR16
)(OriginalKeyData
.Key
.UnicodeChar
+ 'a' - 1);
610 // Invoke notification functions if the key is registered.
612 for (Link
= GetFirstNode (&UsbKeyboardDevice
->NotifyList
);
613 !IsNull (&UsbKeyboardDevice
->NotifyList
, Link
);
614 Link
= GetNextNode (&UsbKeyboardDevice
->NotifyList
, Link
)) {
615 CurrentNotify
= CR (Link
, KEYBOARD_CONSOLE_IN_EX_NOTIFY
, NotifyEntry
, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE
);
616 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &OriginalKeyData
)) {
617 CurrentNotify
->KeyNotificationFn (&OriginalKeyData
);
625 Reset the input device and optionaly run diagnostics
627 There are 2 types of reset for USB keyboard.
628 For non-exhaustive reset, only keyboard buffer is cleared.
629 For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status
630 is also re-initialized.
632 @param This Protocol instance pointer.
633 @param ExtendedVerification Driver may perform diagnostics on reset.
635 @retval EFI_SUCCESS The device was reset.
636 @retval EFI_DEVICE_ERROR The device is not functioning properly and could not be reset.
642 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
643 IN BOOLEAN ExtendedVerification
647 USB_KB_DEV
*UsbKeyboardDevice
;
649 UsbKeyboardDevice
= USB_KB_DEV_FROM_THIS (This
);
651 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
653 PcdGet32 (PcdStatusCodeValueKeyboardReset
),
654 UsbKeyboardDevice
->DevicePath
658 // Non-exhaustive reset:
659 // only reset private data structures.
661 if (!ExtendedVerification
) {
662 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
664 PcdGet32 (PcdStatusCodeValueKeyboardClearBuffer
),
665 UsbKeyboardDevice
->DevicePath
668 // Clear the key buffer of this USB keyboard
670 InitUSBKeyBuffer (&(UsbKeyboardDevice
->KeyboardBuffer
));
671 UsbKeyboardDevice
->CurKeyCode
= 0;
679 Status
= InitUSBKeyboard (UsbKeyboardDevice
);
680 UsbKeyboardDevice
->CurKeyCode
= 0;
681 if (EFI_ERROR (Status
)) {
682 return EFI_DEVICE_ERROR
;
690 Reads the next keystroke from the input device.
692 @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance.
693 @param Key A pointer to a buffer that is filled in with the keystroke
694 information for the key that was pressed.
696 @retval EFI_SUCCESS The keystroke information was returned.
697 @retval EFI_NOT_READY There was no keystroke data availiable.
698 @retval EFI_DEVICE_ERROR The keydtroke information was not returned due to
704 USBKeyboardReadKeyStroke (
705 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
706 OUT EFI_INPUT_KEY
*Key
709 USB_KB_DEV
*UsbKeyboardDevice
;
711 EFI_KEY_DATA KeyData
;
713 UsbKeyboardDevice
= USB_KB_DEV_FROM_THIS (This
);
715 Status
= USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice
, &KeyData
);
716 if (EFI_ERROR (Status
)) {
720 CopyMem (Key
, &KeyData
.Key
, sizeof (EFI_INPUT_KEY
));
727 Event notification function registered for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
728 and EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey.
730 @param Event Event to be signaled when a key is pressed.
731 @param Context Points to USB_KB_DEV instance.
736 USBKeyboardWaitForKey (
741 USB_KB_DEV
*UsbKeyboardDevice
;
743 UsbKeyboardDevice
= (USB_KB_DEV
*) Context
;
745 if (UsbKeyboardDevice
->CurKeyCode
== 0) {
746 if (EFI_ERROR (USBKeyboardCheckForKey (UsbKeyboardDevice
))) {
748 // If no pending key, simply return.
754 // If there is pending key, signal the event.
756 gBS
->SignalEvent (Event
);
761 Check whether there is key pending in the keyboard buffer.
763 @param UsbKeyboardDevice The USB_KB_DEV instance.
765 @retval EFI_SUCCESS There is pending key to read.
766 @retval EFI_NOT_READY No pending key to read.
771 USBKeyboardCheckForKey (
772 IN OUT USB_KB_DEV
*UsbKeyboardDevice
779 // Fetch raw data from the USB keyboard buffer,
780 // and translate it into USB keycode.
782 Status
= USBParseKey (UsbKeyboardDevice
, &KeyCode
);
783 if (EFI_ERROR (Status
)) {
784 return EFI_NOT_READY
;
787 UsbKeyboardDevice
->CurKeyCode
= KeyCode
;
792 Free keyboard notify list.
794 @param NotifyList The keyboard notify list to free.
796 @retval EFI_SUCCESS Free the notify list successfully.
797 @retval EFI_INVALID_PARAMETER NotifyList is NULL.
803 IN OUT LIST_ENTRY
*NotifyList
806 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NotifyNode
;
809 if (NotifyList
== NULL
) {
810 return EFI_INVALID_PARAMETER
;
812 while (!IsListEmpty (NotifyList
)) {
813 Link
= GetFirstNode (NotifyList
);
814 NotifyNode
= CR (Link
, KEYBOARD_CONSOLE_IN_EX_NOTIFY
, NotifyEntry
, USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE
);
815 RemoveEntryList (Link
);
816 gBS
->FreePool (NotifyNode
);
823 Check whether the pressed key matches a registered key or not.
825 @param RegsiteredData A pointer to keystroke data for the key that was registered.
826 @param InputData A pointer to keystroke data for the key that was pressed.
828 @retval TRUE Key pressed matches a registered key.
829 @retval FLASE Key pressed does not matche a registered key.
835 IN EFI_KEY_DATA
*RegsiteredData
,
836 IN EFI_KEY_DATA
*InputData
839 ASSERT (RegsiteredData
!= NULL
&& InputData
!= NULL
);
841 if ((RegsiteredData
->Key
.ScanCode
!= InputData
->Key
.ScanCode
) ||
842 (RegsiteredData
->Key
.UnicodeChar
!= InputData
->Key
.UnicodeChar
)) {
847 // Assume KeyShiftState/KeyToggleState = 0 in Registered key data means these state could be ignored.
849 if (RegsiteredData
->KeyState
.KeyShiftState
!= 0 &&
850 RegsiteredData
->KeyState
.KeyShiftState
!= InputData
->KeyState
.KeyShiftState
) {
853 if (RegsiteredData
->KeyState
.KeyToggleState
!= 0 &&
854 RegsiteredData
->KeyState
.KeyToggleState
!= InputData
->KeyState
.KeyToggleState
) {
862 // Simple Text Input Ex protocol functions
865 Resets the input device hardware.
867 The Reset() function resets the input device hardware. As part
868 of initialization process, the firmware/device will make a quick
869 but reasonable attempt to verify that the device is functioning.
870 If the ExtendedVerification flag is TRUE the firmware may take
871 an extended amount of time to verify the device is operating on
872 reset. Otherwise the reset operation is to occur as quickly as
873 possible. The hardware verification process is not defined by
874 this specification and is left up to the platform firmware or
877 @param This A pointer to the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL instance.
879 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
880 verification operation of the device during reset.
882 @retval EFI_SUCCESS The device was reset.
883 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
889 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
890 IN BOOLEAN ExtendedVerification
894 USB_KB_DEV
*UsbKeyboardDevice
;
898 UsbKeyboardDevice
= TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This
);
900 Status
= UsbKeyboardDevice
->SimpleInput
.Reset (&UsbKeyboardDevice
->SimpleInput
, ExtendedVerification
);
901 if (EFI_ERROR (Status
)) {
902 return EFI_DEVICE_ERROR
;
905 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
906 UsbKeyboardDevice
->KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
;
907 UsbKeyboardDevice
->KeyState
.KeyToggleState
= EFI_TOGGLE_STATE_VALID
;
908 gBS
->RestoreTPL (OldTpl
);
915 Reads the next keystroke from the input device.
917 @param This Protocol instance pointer.
918 @param KeyData A pointer to a buffer that is filled in with the keystroke
919 state data for the key that was pressed.
921 @retval EFI_SUCCESS The keystroke information was returned.
922 @retval EFI_NOT_READY There was no keystroke data available.
923 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
925 @retval EFI_INVALID_PARAMETER KeyData is NULL.
930 USBKeyboardReadKeyStrokeEx (
931 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
932 OUT EFI_KEY_DATA
*KeyData
935 USB_KB_DEV
*UsbKeyboardDevice
;
937 if (KeyData
== NULL
) {
938 return EFI_INVALID_PARAMETER
;
941 UsbKeyboardDevice
= TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This
);
943 return USBKeyboardReadKeyStrokeWorker (UsbKeyboardDevice
, KeyData
);
948 Set certain state for the input device.
950 @param This Protocol instance pointer.
951 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
952 state for the input device.
954 @retval EFI_SUCCESS The device state was set appropriately.
955 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
956 not have the setting adjusted.
957 @retval EFI_UNSUPPORTED The device does not support the ability to have its state set.
958 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
963 USBKeyboardSetState (
964 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
965 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
968 USB_KB_DEV
*UsbKeyboardDevice
;
970 if (KeyToggleState
== NULL
) {
971 return EFI_INVALID_PARAMETER
;
974 UsbKeyboardDevice
= TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This
);
976 if (((UsbKeyboardDevice
->KeyState
.KeyToggleState
& EFI_TOGGLE_STATE_VALID
) != EFI_TOGGLE_STATE_VALID
) ||
977 ((*KeyToggleState
& EFI_TOGGLE_STATE_VALID
) != EFI_TOGGLE_STATE_VALID
)) {
978 return EFI_UNSUPPORTED
;
982 // Update the status light
985 UsbKeyboardDevice
->ScrollOn
= FALSE
;
986 UsbKeyboardDevice
->NumLockOn
= FALSE
;
987 UsbKeyboardDevice
->CapsOn
= FALSE
;
989 if ((*KeyToggleState
& EFI_SCROLL_LOCK_ACTIVE
) == EFI_SCROLL_LOCK_ACTIVE
) {
990 UsbKeyboardDevice
->ScrollOn
= TRUE
;
992 if ((*KeyToggleState
& EFI_NUM_LOCK_ACTIVE
) == EFI_NUM_LOCK_ACTIVE
) {
993 UsbKeyboardDevice
->NumLockOn
= TRUE
;
995 if ((*KeyToggleState
& EFI_CAPS_LOCK_ACTIVE
) == EFI_CAPS_LOCK_ACTIVE
) {
996 UsbKeyboardDevice
->CapsOn
= TRUE
;
999 SetKeyLED (UsbKeyboardDevice
);
1001 UsbKeyboardDevice
->KeyState
.KeyToggleState
= *KeyToggleState
;
1008 Register a notification function for a particular keystroke for the input device.
1010 @param This Protocol instance pointer.
1011 @param KeyData A pointer to a buffer that is filled in with the keystroke
1012 information data for the key that was pressed.
1013 @param KeyNotificationFunction Points to the function to be called when the key
1014 sequence is typed specified by KeyData.
1015 @param NotifyHandle Points to the unique handle assigned to the registered notification.
1017 @retval EFI_SUCCESS The notification function was registered successfully.
1018 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
1019 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
1024 USBKeyboardRegisterKeyNotify (
1025 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1026 IN EFI_KEY_DATA
*KeyData
,
1027 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
1028 OUT EFI_HANDLE
*NotifyHandle
1031 USB_KB_DEV
*UsbKeyboardDevice
;
1033 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*NewNotify
;
1035 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
1037 if (KeyData
== NULL
|| NotifyHandle
== NULL
|| KeyNotificationFunction
== NULL
) {
1038 return EFI_INVALID_PARAMETER
;
1041 UsbKeyboardDevice
= TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This
);
1044 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
1046 for (Link
= GetFirstNode (&UsbKeyboardDevice
->NotifyList
);
1047 !IsNull (&UsbKeyboardDevice
->NotifyList
, Link
);
1048 Link
= GetNextNode (&UsbKeyboardDevice
->NotifyList
, Link
)) {
1049 CurrentNotify
= CR (
1051 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1053 USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1055 if (IsKeyRegistered (&CurrentNotify
->KeyData
, KeyData
)) {
1056 if (CurrentNotify
->KeyNotificationFn
== KeyNotificationFunction
) {
1057 *NotifyHandle
= CurrentNotify
->NotifyHandle
;
1064 // Allocate resource to save the notification function
1066 NewNotify
= (KEYBOARD_CONSOLE_IN_EX_NOTIFY
*) AllocateZeroPool (sizeof (KEYBOARD_CONSOLE_IN_EX_NOTIFY
));
1067 if (NewNotify
== NULL
) {
1068 return EFI_OUT_OF_RESOURCES
;
1071 NewNotify
->Signature
= USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE
;
1072 NewNotify
->KeyNotificationFn
= KeyNotificationFunction
;
1073 CopyMem (&NewNotify
->KeyData
, KeyData
, sizeof (EFI_KEY_DATA
));
1074 InsertTailList (&UsbKeyboardDevice
->NotifyList
, &NewNotify
->NotifyEntry
);
1077 // Use gSimpleTextInExNotifyGuid to get a valid EFI_HANDLE
1079 Status
= gBS
->InstallMultipleProtocolInterfaces (
1080 &NewNotify
->NotifyHandle
,
1081 &gSimpleTextInExNotifyGuid
,
1085 ASSERT_EFI_ERROR (Status
);
1087 *NotifyHandle
= NewNotify
->NotifyHandle
;
1094 Remove a registered notification function from a particular keystroke.
1096 @param This Protocol instance pointer.
1097 @param NotificationHandle The handle of the notification function being unregistered.
1099 @retval EFI_SUCCESS The notification function was unregistered successfully.
1100 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid
1101 @retval EFI_NOT_FOUND Cannot find the matching entry in database.
1106 USBKeyboardUnregisterKeyNotify (
1107 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
1108 IN EFI_HANDLE NotificationHandle
1111 USB_KB_DEV
*UsbKeyboardDevice
;
1113 KEYBOARD_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
1116 if (NotificationHandle
== NULL
) {
1117 return EFI_INVALID_PARAMETER
;
1120 UsbKeyboardDevice
= TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS (This
);
1123 // Check if NotificationHandle is returned from RegisterKeyNotify().
1125 Status
= gBS
->OpenProtocol (
1127 &gSimpleTextInExNotifyGuid
,
1131 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
1133 if (EFI_ERROR (Status
)) {
1134 return EFI_INVALID_PARAMETER
;
1138 // Traverse notify list of USB keyboard and remove the entry of NotificationHandle.
1140 for (Link
= GetFirstNode (&UsbKeyboardDevice
->NotifyList
);
1141 !IsNull (&UsbKeyboardDevice
->NotifyList
, Link
);
1142 Link
= GetNextNode (&UsbKeyboardDevice
->NotifyList
, Link
)) {
1143 CurrentNotify
= CR (
1145 KEYBOARD_CONSOLE_IN_EX_NOTIFY
,
1147 USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE
1149 if (CurrentNotify
->NotifyHandle
== NotificationHandle
) {
1151 // Remove the notification function from NotifyList and free resources
1153 RemoveEntryList (&CurrentNotify
->NotifyEntry
);
1154 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1155 CurrentNotify
->NotifyHandle
,
1156 &gSimpleTextInExNotifyGuid
,
1160 ASSERT_EFI_ERROR (Status
);
1161 gBS
->FreePool (CurrentNotify
);
1166 return EFI_NOT_FOUND
;