2 PS/2 keyboard driver header file
4 Copyright (c) 2006 - 2011, 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_
18 #include <FrameworkDxe.h>
20 #include <Protocol/SimpleTextIn.h>
21 #include <Protocol/SimpleTextInEx.h>
22 #include <Protocol/IsaIo.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/Ps2Policy.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/ReportStatusCodeLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/BaseLib.h>
34 #include <Library/BaseMemoryLib.h>
35 #include <Library/TimerLib.h>
40 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver
;
41 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName
;
42 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2
;
45 // Driver Private Data
47 #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
48 #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
50 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY
{
52 EFI_HANDLE NotifyHandle
;
54 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
55 LIST_ENTRY NotifyEntry
;
56 } KEYBOARD_CONSOLE_IN_EX_NOTIFY
;
58 #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
60 UINT8 Buffer
[KEYBOARD_SCAN_CODE_MAX_COUNT
];
65 #define KEYBOARD_EFI_KEY_MAX_COUNT 256
67 EFI_KEY_DATA Buffer
[KEYBOARD_EFI_KEY_MAX_COUNT
];
76 EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn
;
77 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx
;
78 EFI_ISA_IO_PROTOCOL
*IsaIo
;
82 UINT32 DataRegisterAddress
;
83 UINT32 StatusRegisterAddress
;
84 UINT32 CommandRegisterAddress
;
101 // Queue storing key scancodes
103 SCAN_CODE_QUEUE ScancodeQueue
;
104 EFI_KEY_QUEUE EfiKeyQueue
;
111 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
113 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
115 // Notification Function List
117 LIST_ENTRY NotifyList
;
118 } KEYBOARD_CONSOLE_IN_DEV
;
120 #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
121 #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
123 KEYBOARD_CONSOLE_IN_DEV, \
125 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
128 #define TABLE_END 0x0
131 // Driver entry point
134 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
136 @param[in] ImageHandle The firmware allocated handle for the EFI image.
137 @param[in] SystemTable A pointer to the EFI System Table.
139 @retval EFI_SUCCESS The entry point is executed successfully.
140 @retval other Some error occurs when executing this entry point.
145 InstallPs2KeyboardDriver (
146 IN EFI_HANDLE ImageHandle
,
147 IN EFI_SYSTEM_TABLE
*SystemTable
150 #define KEYBOARD_8042_DATA_REGISTER 0x60
151 #define KEYBOARD_8042_STATUS_REGISTER 0x64
152 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
154 #define KEYBOARD_KBEN 0xF4
155 #define KEYBOARD_CMDECHO_ACK 0xFA
157 #define KEYBOARD_MAX_TRY 256 // 256
158 #define KEYBOARD_TIMEOUT 65536 // 0.07s
159 #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
160 #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
161 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
162 #define SCANCODE_EXTENDED 0xE0
163 #define SCANCODE_EXTENDED1 0xE1
164 #define SCANCODE_CTRL_MAKE 0x1D
165 #define SCANCODE_CTRL_BREAK 0x9D
166 #define SCANCODE_ALT_MAKE 0x38
167 #define SCANCODE_ALT_BREAK 0xB8
168 #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
169 #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
170 #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
171 #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
172 #define SCANCODE_CAPS_LOCK_MAKE 0x3A
173 #define SCANCODE_NUM_LOCK_MAKE 0x45
174 #define SCANCODE_SCROLL_LOCK_MAKE 0x46
175 #define SCANCODE_DELETE_MAKE 0x53
176 #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
177 #define SCANCODE_LEFT_LOGO_BREAK 0xDB
178 #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
179 #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
180 #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
181 #define SCANCODE_MENU_BREAK 0xDD
182 #define SCANCODE_SYS_REQ_MAKE 0x37
183 #define SCANCODE_MAX_MAKE 0x60
185 #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
186 #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
187 #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
188 #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
189 #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
190 #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
191 #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
192 #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
194 #define KEYBOARD_8042_COMMAND_READ 0x20
195 #define KEYBOARD_8042_COMMAND_WRITE 0x60
196 #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
197 #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
198 #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
199 #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
200 #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
202 #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
203 #define KEYBOARD_8048_COMMAND_RESET 0xFF
204 #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
206 #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
207 #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
208 #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
212 // Keyboard Controller Status
214 #define KBC_PARE 0x80 // Parity Error
215 #define KBC_TIM 0x40 // General Time Out
218 // Other functions that are used among .c files
221 Show keyboard status lights according to
222 indicators in ConsoleIn.
224 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
231 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
235 write key to keyboard.
237 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
238 @param Data value wanted to be written
240 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
241 @retval EFI_SUCCESS - GC_TODO: Add description for return value
246 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
251 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
253 The function is always called in TPL_NOTIFY.
255 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
260 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
264 Perform 8042 controller and keyboard Initialization.
265 If ExtendedVerification is TRUE, do additional test for
266 the keyboard interface
268 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
269 @param ExtendedVerification - indicates a thorough initialization
271 @retval EFI_DEVICE_ERROR Fail to init keyboard
272 @retval EFI_SUCCESS Success to init keyboard
276 IN OUT KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
,
277 IN BOOLEAN ExtendedVerification
281 Disable the keyboard interface of the 8042 controller.
283 @param ConsoleIn - the device instance
285 @return status of issuing disable command
290 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
294 Timer event handler: read a series of scancodes from 8042
295 and put them into memory scancode buffer.
296 it read as much scancodes to either fill
297 the memory buffer or empty the keyboard buffer.
298 It is registered as running under TPL_NOTIFY
300 @param Event - The timer event
301 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
306 KeyboardTimerHandler (
313 Implement SIMPLE_TEXT_IN.Reset()
314 Perform 8042 controller and keyboard initialization
316 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
317 @param ExtendedVerification Indicate that the driver may perform a more
318 exhaustive verification operation of the device during
319 reset, now this par is ignored in this driver
325 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
326 IN BOOLEAN ExtendedVerification
330 Implement SIMPLE_TEXT_IN.ReadKeyStroke().
331 Retrieve key values for driver user.
333 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
334 @param Key The output buffer for key value
336 @retval EFI_SUCCESS success to read key stroke
340 KeyboardReadKeyStroke (
341 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
342 OUT EFI_INPUT_KEY
*Key
346 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
347 Signal the event if there is key available
349 @param Event the event object
350 @param Context waitting context
361 Read status register.
363 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
365 @return value in status register
369 KeyReadStatusRegister (
370 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
374 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
375 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
376 should not be in system.
378 @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
380 @retval TRUE Keyboard in System.
381 @retval FALSE Keyboard not in System.
385 CheckKeyboardConnect (
386 IN KEYBOARD_CONSOLE_IN_DEV
*ConsoleIn
390 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
391 Signal the event if there is key available
393 @param Event event object
394 @param Context waiting context
399 KeyboardWaitForKeyEx (
405 // Simple Text Input Ex protocol function prototypes
409 Reset the input device and optionaly run diagnostics
411 @param This - Protocol instance pointer.
412 @param ExtendedVerification - Driver may perform diagnostics on reset.
414 @retval EFI_SUCCESS - The device was reset.
415 @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
422 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
423 IN BOOLEAN ExtendedVerification
427 Reads the next keystroke from the input device. The WaitForKey Event can
428 be used to test for existance of a keystroke via WaitForEvent () call.
431 @param This - Protocol instance pointer.
432 @param KeyData - A pointer to a buffer that is filled in with the keystroke
433 state data for the key that was pressed.
435 @retval EFI_SUCCESS - The keystroke information was returned.
436 @retval EFI_NOT_READY - There was no keystroke data availiable.
437 @retval EFI_DEVICE_ERROR - The keystroke information was not returned due to
439 @retval EFI_INVALID_PARAMETER - KeyData is NULL.
444 KeyboardReadKeyStrokeEx (
445 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
446 OUT EFI_KEY_DATA
*KeyData
450 Set certain state for the input device.
452 @param This - Protocol instance pointer.
453 @param KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
454 state for the input device.
456 @retval EFI_SUCCESS - The device state was set successfully.
457 @retval EFI_DEVICE_ERROR - The device is not functioning correctly and could
458 not have the setting adjusted.
459 @retval EFI_UNSUPPORTED - The device does not have the ability to set its state.
460 @retval EFI_INVALID_PARAMETER - KeyToggleState is NULL.
466 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
467 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
471 Register a notification function for a particular keystroke for the input device.
473 @param This - Protocol instance pointer.
474 @param KeyData - A pointer to a buffer that is filled in with the keystroke
475 information data for the key that was pressed.
476 @param KeyNotificationFunction - Points to the function to be called when the key
477 sequence is typed specified by KeyData.
478 @param NotifyHandle - Points to the unique handle assigned to the registered notification.
480 @retval EFI_SUCCESS - The notification function was registered successfully.
481 @retval EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
482 @retval EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
487 KeyboardRegisterKeyNotify (
488 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
489 IN EFI_KEY_DATA
*KeyData
,
490 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
491 OUT EFI_HANDLE
*NotifyHandle
495 Remove a registered notification function from a particular keystroke.
497 @param This - Protocol instance pointer.
498 @param NotificationHandle - The handle of the notification function being unregistered.
501 @retval EFI_SUCCESS - The notification function was unregistered successfully.
502 @retval EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
503 @retval EFI_NOT_FOUND - Can not find the matching entry in database.
508 KeyboardUnregisterKeyNotify (
509 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
510 IN EFI_HANDLE NotificationHandle
514 Push one key data to the EFI key buffer.
516 @param Queue Pointer to instance of EFI_KEY_QUEUE.
517 @param KeyData The key data to push.
521 IN EFI_KEY_QUEUE
*Queue
,
522 IN EFI_KEY_DATA
*KeyData
526 Judge whether is a registed key
528 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
529 state data for the key that was registered.
530 @param InputData A pointer to a buffer that is filled in with the keystroke
531 state data for the key that was pressed.
533 @retval TRUE Key be pressed matches a registered key.
534 @retval FLASE Match failed.
539 IN EFI_KEY_DATA
*RegsiteredData
,
540 IN EFI_KEY_DATA
*InputData