2 PS/2 keyboard driver header file
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #ifndef _PS2KEYBOARD_H_
16 #define _PS2KEYBOARD_H_
20 #include <Protocol/SuperIo.h>
21 #include <Protocol/SimpleTextIn.h>
22 #include <Protocol/SimpleTextInEx.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/Ps2Policy.h>
26 #include <Library/IoLib.h>
27 #include <Library/DevicePathLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UefiLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/ReportStatusCodeLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/UefiRuntimeServicesTableLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/BaseLib.h>
36 #include <Library/BaseMemoryLib.h>
37 #include <Library/TimerLib.h>
38 #include <Library/PcdLib.h>
43 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver
;
44 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName
;
45 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2
;
48 // Driver Private Data
50 #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
51 #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
53 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY
{
56 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
57 LIST_ENTRY NotifyEntry
;
58 } KEYBOARD_CONSOLE_IN_EX_NOTIFY
;
60 #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
62 UINT8 Buffer
[KEYBOARD_SCAN_CODE_MAX_COUNT
];
67 #define KEYBOARD_EFI_KEY_MAX_COUNT 256
69 EFI_KEY_DATA Buffer
[KEYBOARD_EFI_KEY_MAX_COUNT
];
78 EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn
;
79 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx
;
83 UINT32 DataRegisterAddress
;
84 UINT32 StatusRegisterAddress
;
85 UINT32 CommandRegisterAddress
;
102 BOOLEAN IsSupportPartialKey
;
104 // Queue storing key scancodes
106 SCAN_CODE_QUEUE ScancodeQueue
;
107 EFI_KEY_QUEUE EfiKeyQueue
;
114 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
116 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
118 // Notification Function List
120 LIST_ENTRY NotifyList
;
121 } KEYBOARD_CONSOLE_IN_DEV
;
123 #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
124 #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
126 KEYBOARD_CONSOLE_IN_DEV, \
128 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
131 #define TABLE_END 0x0
134 // Driver entry point
137 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
139 @param[in] ImageHandle The firmware allocated handle for the EFI image.
140 @param[in] SystemTable A pointer to the EFI System Table.
142 @retval EFI_SUCCESS The entry point is executed successfully.
143 @retval other Some error occurs when executing this entry point.
148 InstallPs2KeyboardDriver (
149 IN EFI_HANDLE ImageHandle
,
150 IN EFI_SYSTEM_TABLE
*SystemTable
153 #define KEYBOARD_8042_DATA_REGISTER 0x60
154 #define KEYBOARD_8042_STATUS_REGISTER 0x64
155 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
157 #define KEYBOARD_KBEN 0xF4
158 #define KEYBOARD_CMDECHO_ACK 0xFA
160 #define KEYBOARD_MAX_TRY 256 // 256
161 #define KEYBOARD_TIMEOUT 65536 // 0.07s
162 #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
163 #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
164 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
165 #define SCANCODE_EXTENDED0 0xE0
166 #define SCANCODE_EXTENDED1 0xE1
167 #define SCANCODE_CTRL_MAKE 0x1D
168 #define SCANCODE_CTRL_BREAK 0x9D
169 #define SCANCODE_ALT_MAKE 0x38
170 #define SCANCODE_ALT_BREAK 0xB8
171 #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
172 #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
173 #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
174 #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
175 #define SCANCODE_CAPS_LOCK_MAKE 0x3A
176 #define SCANCODE_NUM_LOCK_MAKE 0x45
177 #define SCANCODE_SCROLL_LOCK_MAKE 0x46
178 #define SCANCODE_DELETE_MAKE 0x53
179 #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
180 #define SCANCODE_LEFT_LOGO_BREAK 0xDB
181 #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
182 #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
183 #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
184 #define SCANCODE_MENU_BREAK 0xDD
185 #define SCANCODE_SYS_REQ_MAKE 0x37
186 #define SCANCODE_SYS_REQ_BREAK 0xB7
187 #define SCANCODE_SYS_REQ_MAKE_WITH_ALT 0x54
188 #define SCANCODE_SYS_REQ_BREAK_WITH_ALT 0xD4
190 #define SCANCODE_MAX_MAKE 0x60
193 #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
194 #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
195 #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
196 #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
197 #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
198 #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
199 #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
200 #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
202 #define KEYBOARD_8042_COMMAND_READ 0x20
203 #define KEYBOARD_8042_COMMAND_WRITE 0x60
204 #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
205 #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
206 #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
207 #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
208 #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
210 #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
211 #define KEYBOARD_8048_COMMAND_RESET 0xFF
212 #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
214 #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
215 #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
216 #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
220 // Keyboard Controller Status
222 #define KBC_PARE 0x80 // Parity Error
223 #define KBC_TIM 0x40 // General Time Out
226 // Other functions that are used among .c files
229 Show keyboard status lights according to
230 indicators in ConsoleIn.
232 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
239 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
243 write key to keyboard.
245 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
246 @param Data value wanted to be written
248 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
249 @retval EFI_SUCCESS - GC_TODO: Add description for return value
254 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
259 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
261 The function is always called in TPL_NOTIFY.
263 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
268 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
272 Perform 8042 controller and keyboard Initialization.
273 If ExtendedVerification is TRUE, do additional test for
274 the keyboard interface
276 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
277 @param ExtendedVerification - indicates a thorough initialization
279 @retval EFI_DEVICE_ERROR Fail to init keyboard
280 @retval EFI_SUCCESS Success to init keyboard
284 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
285 IN BOOLEAN ExtendedVerification
289 Disable the keyboard interface of the 8042 controller.
291 @param ConsoleIn - the device instance
293 @return status of issuing disable command
298 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
302 Timer event handler: read a series of scancodes from 8042
303 and put them into memory scancode buffer.
304 it read as much scancodes to either fill
305 the memory buffer or empty the keyboard buffer.
306 It is registered as running under TPL_NOTIFY
308 @param Event - The timer event
309 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
314 KeyboardTimerHandler (
321 Implement SIMPLE_TEXT_IN.Reset()
322 Perform 8042 controller and keyboard initialization
324 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
325 @param ExtendedVerification Indicate that the driver may perform a more
326 exhaustive verification operation of the device during
327 reset, now this par is ignored in this driver
333 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
334 IN BOOLEAN ExtendedVerification
338 Implement SIMPLE_TEXT_IN.ReadKeyStroke().
339 Retrieve key values for driver user.
341 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
342 @param Key The output buffer for key value
344 @retval EFI_SUCCESS success to read key stroke
348 KeyboardReadKeyStroke (
349 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
350 OUT EFI_INPUT_KEY
*Key
354 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
355 Signal the event if there is key available
357 @param Event the event object
358 @param Context waitting context
369 Read status register.
371 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
373 @return value in status register
377 KeyReadStatusRegister (
378 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
382 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
383 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
384 should not be in system.
386 @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
388 @retval TRUE Keyboard in System.
389 @retval FALSE Keyboard not in System.
393 CheckKeyboardConnect (
394 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
398 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
399 Signal the event if there is key available
401 @param Event event object
402 @param Context waiting context
407 KeyboardWaitForKeyEx (
413 // Simple Text Input Ex protocol function prototypes
417 Reset the input device and optionaly run diagnostics
419 @param This - Protocol instance pointer.
420 @param ExtendedVerification - Driver may perform diagnostics on reset.
422 @retval EFI_SUCCESS - The device was reset.
423 @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
430 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
431 IN BOOLEAN ExtendedVerification
435 Reads the next keystroke from the input device. The WaitForKey Event can
436 be used to test for existance of a keystroke via WaitForEvent () call.
439 @param This - Protocol instance pointer.
440 @param KeyData - A pointer to a buffer that is filled in with the keystroke
441 state data for the key that was pressed.
443 @retval EFI_SUCCESS - The keystroke information was returned.
444 @retval EFI_NOT_READY - There was no keystroke data availiable.
445 @retval EFI_DEVICE_ERROR - The keystroke information was not returned due to
447 @retval EFI_INVALID_PARAMETER - KeyData is NULL.
452 KeyboardReadKeyStrokeEx (
453 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
454 OUT EFI_KEY_DATA
*KeyData
458 Set certain state for the input device.
460 @param This - Protocol instance pointer.
461 @param KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
462 state for the input device.
464 @retval EFI_SUCCESS - The device state was set successfully.
465 @retval EFI_DEVICE_ERROR - The device is not functioning correctly and could
466 not have the setting adjusted.
467 @retval EFI_UNSUPPORTED - The device does not have the ability to set its state.
468 @retval EFI_INVALID_PARAMETER - KeyToggleState is NULL.
474 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
475 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
479 Register a notification function for a particular keystroke for the input device.
481 @param This - Protocol instance pointer.
482 @param KeyData - A pointer to a buffer that is filled in with the keystroke
483 information data for the key that was pressed.
484 @param KeyNotificationFunction - Points to the function to be called when the key
485 sequence is typed specified by KeyData.
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 necesssary 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 registed 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 FLASE Match failed.
547 IN EFI_KEY_DATA
*RegsiteredData
,
548 IN EFI_KEY_DATA
*InputData