2 PS/2 keyboard driver header file
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #ifndef _PS2KEYBOARD_H_
10 #define _PS2KEYBOARD_H_
14 #include <Protocol/SimpleTextIn.h>
15 #include <Protocol/SimpleTextInEx.h>
16 #include <Protocol/IsaIo.h>
17 #include <Protocol/DevicePath.h>
18 #include <Protocol/Ps2Policy.h>
20 #include <Library/UefiDriverEntryPoint.h>
21 #include <Library/UefiLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/ReportStatusCodeLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiRuntimeServicesTableLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/BaseLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/TimerLib.h>
30 #include <Library/PcdLib.h>
35 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver
;
36 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName
;
37 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2
;
40 // Driver Private Data
42 #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
43 #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
45 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY
{
48 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
49 LIST_ENTRY NotifyEntry
;
50 } KEYBOARD_CONSOLE_IN_EX_NOTIFY
;
52 #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
54 UINT8 Buffer
[KEYBOARD_SCAN_CODE_MAX_COUNT
];
59 #define KEYBOARD_EFI_KEY_MAX_COUNT 256
61 EFI_KEY_DATA Buffer
[KEYBOARD_EFI_KEY_MAX_COUNT
];
70 EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn
;
71 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx
;
72 EFI_ISA_IO_PROTOCOL
*IsaIo
;
76 UINT32 DataRegisterAddress
;
77 UINT32 StatusRegisterAddress
;
78 UINT32 CommandRegisterAddress
;
95 BOOLEAN IsSupportPartialKey
;
97 // Queue storing key scancodes
99 SCAN_CODE_QUEUE ScancodeQueue
;
100 EFI_KEY_QUEUE EfiKeyQueue
;
101 EFI_KEY_QUEUE EfiKeyQueueForNotify
;
108 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
110 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
112 // Notification Function List
114 LIST_ENTRY NotifyList
;
115 EFI_EVENT KeyNotifyProcessEvent
;
116 } KEYBOARD_CONSOLE_IN_DEV
;
118 #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
119 #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
121 KEYBOARD_CONSOLE_IN_DEV, \
123 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
126 #define TABLE_END 0x0
129 // Driver entry point
132 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
134 @param[in] ImageHandle The firmware allocated handle for the EFI image.
135 @param[in] SystemTable A pointer to the EFI System Table.
137 @retval EFI_SUCCESS The entry point is executed successfully.
138 @retval other Some error occurs when executing this entry point.
143 InstallPs2KeyboardDriver (
144 IN EFI_HANDLE ImageHandle
,
145 IN EFI_SYSTEM_TABLE
*SystemTable
148 #define KEYBOARD_8042_DATA_REGISTER 0x60
149 #define KEYBOARD_8042_STATUS_REGISTER 0x64
150 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
152 #define KEYBOARD_KBEN 0xF4
153 #define KEYBOARD_CMDECHO_ACK 0xFA
155 #define KEYBOARD_MAX_TRY 256 // 256
156 #define KEYBOARD_TIMEOUT 65536 // 0.07s
157 #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
158 #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
159 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
160 #define SCANCODE_EXTENDED0 0xE0
161 #define SCANCODE_EXTENDED1 0xE1
162 #define SCANCODE_CTRL_MAKE 0x1D
163 #define SCANCODE_CTRL_BREAK 0x9D
164 #define SCANCODE_ALT_MAKE 0x38
165 #define SCANCODE_ALT_BREAK 0xB8
166 #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
167 #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
168 #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
169 #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
170 #define SCANCODE_CAPS_LOCK_MAKE 0x3A
171 #define SCANCODE_NUM_LOCK_MAKE 0x45
172 #define SCANCODE_SCROLL_LOCK_MAKE 0x46
173 #define SCANCODE_DELETE_MAKE 0x53
174 #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
175 #define SCANCODE_LEFT_LOGO_BREAK 0xDB
176 #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
177 #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
178 #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
179 #define SCANCODE_MENU_BREAK 0xDD
180 #define SCANCODE_SYS_REQ_MAKE 0x37
181 #define SCANCODE_SYS_REQ_BREAK 0xB7
182 #define SCANCODE_SYS_REQ_MAKE_WITH_ALT 0x54
183 #define SCANCODE_SYS_REQ_BREAK_WITH_ALT 0xD4
185 #define SCANCODE_MAX_MAKE 0x60
188 #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
189 #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
190 #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
191 #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
192 #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
193 #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
194 #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
195 #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
197 #define KEYBOARD_8042_COMMAND_READ 0x20
198 #define KEYBOARD_8042_COMMAND_WRITE 0x60
199 #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
200 #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
201 #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
202 #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
203 #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
205 #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
206 #define KEYBOARD_8048_COMMAND_RESET 0xFF
207 #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
209 #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
210 #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
211 #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
215 // Keyboard Controller Status
217 #define KBC_PARE 0x80 // Parity Error
218 #define KBC_TIM 0x40 // General Time Out
221 // Other functions that are used among .c files
224 Show keyboard status lights according to
225 indicators in ConsoleIn.
227 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
234 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
238 write key to keyboard.
240 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
241 @param Data value wanted to be written
243 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
244 @retval EFI_SUCCESS - GC_TODO: Add description for return value
249 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
254 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
256 The function is always called in TPL_NOTIFY.
258 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
263 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
269 @param Event Indicates the event that invoke this function.
270 @param Context Indicates the calling context.
274 KeyNotifyProcessHandler (
280 Perform 8042 controller and keyboard Initialization.
281 If ExtendedVerification is TRUE, do additional test for
282 the keyboard interface
284 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
285 @param ExtendedVerification - indicates a thorough initialization
287 @retval EFI_DEVICE_ERROR Fail to init keyboard
288 @retval EFI_SUCCESS Success to init keyboard
292 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
293 IN BOOLEAN ExtendedVerification
299 Timer event handler: read a series of scancodes from 8042
300 and put them into memory scancode buffer.
301 it read as much scancodes to either fill
302 the memory buffer or empty the keyboard buffer.
303 It is registered as running under TPL_NOTIFY
305 @param Event - The timer event
306 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
311 KeyboardTimerHandler (
318 Implement SIMPLE_TEXT_IN.Reset()
319 Perform 8042 controller and keyboard initialization
321 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
322 @param ExtendedVerification Indicate that the driver may perform a more
323 exhaustive verification operation of the device during
324 reset, now this par is ignored in this driver
330 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
331 IN BOOLEAN ExtendedVerification
335 Implement SIMPLE_TEXT_IN.ReadKeyStroke().
336 Retrieve key values for driver user.
338 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
339 @param Key The output buffer for key value
341 @retval EFI_SUCCESS success to read key stroke
345 KeyboardReadKeyStroke (
346 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
347 OUT EFI_INPUT_KEY
*Key
351 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
352 Signal the event if there is key available
354 @param Event the event object
355 @param Context waitting context
366 Read status register.
368 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
370 @return value in status register
374 KeyReadStatusRegister (
375 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
379 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
380 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
381 should not be in system.
383 @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
385 @retval TRUE Keyboard in System.
386 @retval FALSE Keyboard not in System.
390 CheckKeyboardConnect (
391 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
395 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
396 Signal the event if there is key available
398 @param Event event object
399 @param Context waiting context
404 KeyboardWaitForKeyEx (
410 // Simple Text Input Ex protocol function prototypes
414 Reset the input device and optionaly run diagnostics
416 @param This - Protocol instance pointer.
417 @param ExtendedVerification - Driver may perform diagnostics on reset.
419 @retval EFI_SUCCESS - The device was reset.
420 @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
427 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
428 IN BOOLEAN ExtendedVerification
432 Reads the next keystroke from the input device. The WaitForKey Event can
433 be used to test for existance of a keystroke via WaitForEvent () call.
436 @param This - Protocol instance pointer.
437 @param KeyData - A pointer to a buffer that is filled in with the keystroke
438 state data for the key that was pressed.
440 @retval EFI_SUCCESS - The keystroke information was returned.
441 @retval EFI_NOT_READY - There was no keystroke data availiable.
442 @retval EFI_DEVICE_ERROR - The keystroke information was not returned due to
444 @retval EFI_INVALID_PARAMETER - KeyData is NULL.
449 KeyboardReadKeyStrokeEx (
450 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
451 OUT EFI_KEY_DATA
*KeyData
455 Set certain state for the input device.
457 @param This - Protocol instance pointer.
458 @param KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
459 state for the input device.
461 @retval EFI_SUCCESS - The device state was set successfully.
462 @retval EFI_DEVICE_ERROR - The device is not functioning correctly and could
463 not have the setting adjusted.
464 @retval EFI_UNSUPPORTED - The device does not have the ability to set its state.
465 @retval EFI_INVALID_PARAMETER - KeyToggleState is NULL.
471 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
472 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
476 Register a notification function for a particular keystroke for the input device.
478 @param This - Protocol instance pointer.
479 @param KeyData - A pointer to a buffer that is filled in with the keystroke
480 information data for the key that was pressed. If KeyData.Key,
481 KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState
482 are 0, then any incomplete keystroke will trigger a notification of
483 the KeyNotificationFunction.
484 @param KeyNotificationFunction - Points to the function to be called when the key
485 sequence is typed specified by KeyData. This notification function
486 should be called at <=TPL_CALLBACK.
487 @param NotifyHandle - Points to the unique handle assigned to the registered notification.
489 @retval EFI_SUCCESS - The notification function was registered successfully.
490 @retval EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
491 @retval EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
496 KeyboardRegisterKeyNotify (
497 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
498 IN EFI_KEY_DATA
*KeyData
,
499 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
500 OUT VOID
**NotifyHandle
504 Remove a registered notification function from a particular keystroke.
506 @param This - Protocol instance pointer.
507 @param NotificationHandle - The handle of the notification function being unregistered.
510 @retval EFI_SUCCESS - The notification function was unregistered successfully.
511 @retval EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
512 @retval EFI_NOT_FOUND - Can not find the matching entry in database.
517 KeyboardUnregisterKeyNotify (
518 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
519 IN VOID
*NotificationHandle
523 Push one key data to the EFI key buffer.
525 @param Queue Pointer to instance of EFI_KEY_QUEUE.
526 @param KeyData The key data to push.
530 IN EFI_KEY_QUEUE
*Queue
,
531 IN EFI_KEY_DATA
*KeyData
535 Judge whether is a registed key
537 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
538 state data for the key that was registered.
539 @param InputData A pointer to a buffer that is filled in with the keystroke
540 state data for the key that was pressed.
542 @retval TRUE Key be pressed matches a registered key.
543 @retval FLASE Match failed.
548 IN EFI_KEY_DATA
*RegsiteredData
,
549 IN EFI_KEY_DATA
*InputData
553 Initialize the key state.
555 @param ConsoleIn The KEYBOARD_CONSOLE_IN_DEV instance.
556 @param KeyState A pointer to receive the key state information.
560 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
561 OUT EFI_KEY_STATE
*KeyState