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/SuperIo.h>
15 #include <Protocol/SimpleTextIn.h>
16 #include <Protocol/SimpleTextInEx.h>
17 #include <Protocol/DevicePath.h>
18 #include <Protocol/Ps2Policy.h>
20 #include <Library/IoLib.h>
21 #include <Library/DevicePathLib.h>
22 #include <Library/UefiDriverEntryPoint.h>
23 #include <Library/UefiLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/ReportStatusCodeLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiRuntimeServicesTableLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/PcdLib.h>
37 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver
;
38 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName
;
39 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2
;
42 // Driver Private Data
44 #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
45 #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
47 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY
{
50 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
51 LIST_ENTRY NotifyEntry
;
52 } KEYBOARD_CONSOLE_IN_EX_NOTIFY
;
54 #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
56 UINT8 Buffer
[KEYBOARD_SCAN_CODE_MAX_COUNT
];
61 #define KEYBOARD_EFI_KEY_MAX_COUNT 256
63 EFI_KEY_DATA Buffer
[KEYBOARD_EFI_KEY_MAX_COUNT
];
72 EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn
;
73 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx
;
77 UINT32 DataRegisterAddress
;
78 UINT32 StatusRegisterAddress
;
79 UINT32 CommandRegisterAddress
;
96 BOOLEAN IsSupportPartialKey
;
98 // Queue storing key scancodes
100 SCAN_CODE_QUEUE ScancodeQueue
;
101 EFI_KEY_QUEUE EfiKeyQueue
;
102 EFI_KEY_QUEUE EfiKeyQueueForNotify
;
109 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
111 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
113 // Notification Function List
115 LIST_ENTRY NotifyList
;
116 EFI_EVENT KeyNotifyProcessEvent
;
117 } KEYBOARD_CONSOLE_IN_DEV
;
119 #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
120 #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
122 KEYBOARD_CONSOLE_IN_DEV, \
124 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
127 #define TABLE_END 0x0
130 // Driver entry point
133 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
135 @param[in] ImageHandle The firmware allocated handle for the EFI image.
136 @param[in] SystemTable A pointer to the EFI System Table.
138 @retval EFI_SUCCESS The entry point is executed successfully.
139 @retval other Some error occurs when executing this entry point.
144 InstallPs2KeyboardDriver (
145 IN EFI_HANDLE ImageHandle
,
146 IN EFI_SYSTEM_TABLE
*SystemTable
149 #define KEYBOARD_8042_DATA_REGISTER 0x60
150 #define KEYBOARD_8042_STATUS_REGISTER 0x64
151 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
153 #define KEYBOARD_KBEN 0xF4
154 #define KEYBOARD_CMDECHO_ACK 0xFA
156 #define KEYBOARD_MAX_TRY 256 // 256
157 #define KEYBOARD_TIMEOUT 65536 // 0.07s
158 #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
159 #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
160 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
161 #define SCANCODE_EXTENDED0 0xE0
162 #define SCANCODE_EXTENDED1 0xE1
163 #define SCANCODE_CTRL_MAKE 0x1D
164 #define SCANCODE_CTRL_BREAK 0x9D
165 #define SCANCODE_ALT_MAKE 0x38
166 #define SCANCODE_ALT_BREAK 0xB8
167 #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
168 #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
169 #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
170 #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
171 #define SCANCODE_CAPS_LOCK_MAKE 0x3A
172 #define SCANCODE_NUM_LOCK_MAKE 0x45
173 #define SCANCODE_SCROLL_LOCK_MAKE 0x46
174 #define SCANCODE_DELETE_MAKE 0x53
175 #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
176 #define SCANCODE_LEFT_LOGO_BREAK 0xDB
177 #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
178 #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
179 #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
180 #define SCANCODE_MENU_BREAK 0xDD
181 #define SCANCODE_SYS_REQ_MAKE 0x37
182 #define SCANCODE_SYS_REQ_BREAK 0xB7
183 #define SCANCODE_SYS_REQ_MAKE_WITH_ALT 0x54
184 #define SCANCODE_SYS_REQ_BREAK_WITH_ALT 0xD4
186 #define SCANCODE_MAX_MAKE 0x60
189 #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
190 #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
191 #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
192 #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
193 #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
194 #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
195 #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
196 #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
198 #define KEYBOARD_8042_COMMAND_READ 0x20
199 #define KEYBOARD_8042_COMMAND_WRITE 0x60
200 #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
201 #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
202 #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
203 #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
204 #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
206 #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
207 #define KEYBOARD_8048_COMMAND_RESET 0xFF
208 #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
210 #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
211 #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
212 #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
216 // Keyboard Controller Status
218 #define KBC_PARE 0x80 // Parity Error
219 #define KBC_TIM 0x40 // General Time Out
222 // Other functions that are used among .c files
225 Show keyboard status lights according to
226 indicators in ConsoleIn.
228 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
235 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
239 write key to keyboard.
241 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
242 @param Data value wanted to be written
244 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
245 @retval EFI_SUCCESS - GC_TODO: Add description for return value
250 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
255 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
257 The function is always called in TPL_NOTIFY.
259 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
264 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
270 @param Event Indicates the event that invoke this function.
271 @param Context Indicates the calling context.
275 KeyNotifyProcessHandler (
281 Perform 8042 controller and keyboard Initialization.
282 If ExtendedVerification is TRUE, do additional test for
283 the keyboard interface
285 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
286 @param ExtendedVerification - indicates a thorough initialization
288 @retval EFI_DEVICE_ERROR Fail to init keyboard
289 @retval EFI_SUCCESS Success to init keyboard
293 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
294 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 waiting 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 optionally 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 existence 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 available.
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 are 0,
482 then any incomplete keystroke will trigger a notification of the KeyNotificationFunction.
483 @param KeyNotificationFunction - Points to the function to be called when the key
484 sequence is typed specified by KeyData. This notification function
485 should be called at <=TPL_CALLBACK.
486 @param NotifyHandle - Points to the unique handle assigned to the registered notification.
488 @retval EFI_SUCCESS - The notification function was registered successfully.
489 @retval EFI_OUT_OF_RESOURCES - Unable to allocate resources for necessary data structures.
490 @retval EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
495 KeyboardRegisterKeyNotify (
496 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
497 IN EFI_KEY_DATA
*KeyData
,
498 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
499 OUT VOID
**NotifyHandle
503 Remove a registered notification function from a particular keystroke.
505 @param This - Protocol instance pointer.
506 @param NotificationHandle - The handle of the notification function being unregistered.
509 @retval EFI_SUCCESS - The notification function was unregistered successfully.
510 @retval EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
511 @retval EFI_NOT_FOUND - Can not find the matching entry in database.
516 KeyboardUnregisterKeyNotify (
517 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
518 IN VOID
*NotificationHandle
522 Push one key data to the EFI key buffer.
524 @param Queue Pointer to instance of EFI_KEY_QUEUE.
525 @param KeyData The key data to push.
529 IN EFI_KEY_QUEUE
*Queue
,
530 IN EFI_KEY_DATA
*KeyData
534 Judge whether is a registered key
536 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
537 state data for the key that was registered.
538 @param InputData A pointer to a buffer that is filled in with the keystroke
539 state data for the key that was pressed.
541 @retval TRUE Key be pressed matches a registered key.
542 @retval FALSE Match failed.
547 IN EFI_KEY_DATA
*RegsiteredData
,
548 IN EFI_KEY_DATA
*InputData
552 Initialize the key state.
554 @param ConsoleIn The KEYBOARD_CONSOLE_IN_DEV instance.
555 @param KeyState A pointer to receive the key state information.
559 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
560 OUT EFI_KEY_STATE
*KeyState