Fix the PS2 keyboard driver to call hotkey callback even no one is calling ReadKeyStroke
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2Keyboard.h
1 /** @file
2 PS/2 keyboard driver header file
3
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
9
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.
12
13 **/
14
15 #ifndef _PS2KEYBOARD_H_
16 #define _PS2KEYBOARD_H_
17
18 #include <FrameworkDxe.h>
19
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>
25
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>
36
37 //
38 // Global Variables
39 //
40 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver;
41 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName;
42 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2;
43
44 //
45 // Driver Private Data
46 //
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')
49
50 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY {
51 UINTN Signature;
52 EFI_HANDLE NotifyHandle;
53 EFI_KEY_DATA KeyData;
54 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn;
55 LIST_ENTRY NotifyEntry;
56 } KEYBOARD_CONSOLE_IN_EX_NOTIFY;
57
58 #define KEYBOARD_SCAN_CODE_MAX_COUNT 32
59 typedef struct {
60 UINT8 Buffer[KEYBOARD_SCAN_CODE_MAX_COUNT];
61 UINTN Head;
62 UINTN Tail;
63 } SCAN_CODE_QUEUE;
64
65 #define KEYBOARD_EFI_KEY_MAX_COUNT 256
66 typedef struct {
67 EFI_KEY_DATA Buffer[KEYBOARD_EFI_KEY_MAX_COUNT];
68 UINTN Head;
69 UINTN Tail;
70 } EFI_KEY_QUEUE;
71
72 typedef struct {
73 UINTN Signature;
74
75 EFI_HANDLE Handle;
76 EFI_SIMPLE_TEXT_INPUT_PROTOCOL ConIn;
77 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL ConInEx;
78 EFI_ISA_IO_PROTOCOL *IsaIo;
79
80 EFI_EVENT TimerEvent;
81
82 UINT32 DataRegisterAddress;
83 UINT32 StatusRegisterAddress;
84 UINT32 CommandRegisterAddress;
85
86 BOOLEAN LeftShift;
87 BOOLEAN RightShift;
88 BOOLEAN LeftLogo;
89 BOOLEAN RightLogo;
90 BOOLEAN Menu;
91 BOOLEAN SysReq;
92
93 BOOLEAN Ctrl;
94 BOOLEAN Alt;
95 BOOLEAN Shift;
96 BOOLEAN CapsLock;
97 BOOLEAN NumLock;
98 BOOLEAN ScrollLock;
99
100 //
101 // Queue storing key scancodes
102 //
103 SCAN_CODE_QUEUE ScancodeQueue;
104 EFI_KEY_QUEUE EfiKeyQueue;
105
106 //
107 // Error state
108 //
109 BOOLEAN KeyboardErr;
110
111 EFI_UNICODE_STRING_TABLE *ControllerNameTable;
112
113 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
114 //
115 // Notification Function List
116 //
117 LIST_ENTRY NotifyList;
118 } KEYBOARD_CONSOLE_IN_DEV;
119
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) \
122 CR (a, \
123 KEYBOARD_CONSOLE_IN_DEV, \
124 ConInEx, \
125 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
126 )
127
128 #define TABLE_END 0x0
129
130 //
131 // Driver entry point
132 //
133 /**
134 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
135
136 @param[in] ImageHandle The firmware allocated handle for the EFI image.
137 @param[in] SystemTable A pointer to the EFI System Table.
138
139 @retval EFI_SUCCESS The entry point is executed successfully.
140 @retval other Some error occurs when executing this entry point.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 InstallPs2KeyboardDriver (
146 IN EFI_HANDLE ImageHandle,
147 IN EFI_SYSTEM_TABLE *SystemTable
148 );
149
150 #define KEYBOARD_8042_DATA_REGISTER 0x60
151 #define KEYBOARD_8042_STATUS_REGISTER 0x64
152 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
153
154 #define KEYBOARD_KBEN 0xF4
155 #define KEYBOARD_CMDECHO_ACK 0xFA
156
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
184
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
193
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
201
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
205
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
209
210
211 //
212 // Keyboard Controller Status
213 //
214 #define KBC_PARE 0x80 // Parity Error
215 #define KBC_TIM 0x40 // General Time Out
216
217 //
218 // Other functions that are used among .c files
219 //
220 /**
221 Show keyboard status lights according to
222 indicators in ConsoleIn.
223
224 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
225
226 @return status
227
228 **/
229 EFI_STATUS
230 UpdateStatusLights (
231 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
232 );
233
234 /**
235 write key to keyboard.
236
237 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
238 @param Data value wanted to be written
239
240 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
241 @retval EFI_SUCCESS - GC_TODO: Add description for return value
242
243 **/
244 EFI_STATUS
245 KeyboardRead (
246 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
247 OUT UINT8 *Data
248 );
249
250 /**
251 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
252
253 The function is always called in TPL_NOTIFY.
254
255 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
256
257 **/
258 VOID
259 KeyGetchar (
260 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
261 );
262
263 /**
264 Perform 8042 controller and keyboard Initialization.
265 If ExtendedVerification is TRUE, do additional test for
266 the keyboard interface
267
268 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
269 @param ExtendedVerification - indicates a thorough initialization
270
271 @retval EFI_DEVICE_ERROR Fail to init keyboard
272 @retval EFI_SUCCESS Success to init keyboard
273 **/
274 EFI_STATUS
275 InitKeyboard (
276 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
277 IN BOOLEAN ExtendedVerification
278 );
279
280 /**
281 Disable the keyboard interface of the 8042 controller.
282
283 @param ConsoleIn - the device instance
284
285 @return status of issuing disable command
286
287 **/
288 EFI_STATUS
289 DisableKeyboard (
290 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
291 );
292
293 /**
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
299
300 @param Event - The timer event
301 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
302
303 **/
304 VOID
305 EFIAPI
306 KeyboardTimerHandler (
307 IN EFI_EVENT Event,
308 IN VOID *Context
309 );
310
311 /**
312 logic reset keyboard
313 Implement SIMPLE_TEXT_IN.Reset()
314 Perform 8042 controller and keyboard initialization
315
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
320
321 **/
322 EFI_STATUS
323 EFIAPI
324 KeyboardEfiReset (
325 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
326 IN BOOLEAN ExtendedVerification
327 );
328
329 /**
330 Implement SIMPLE_TEXT_IN.ReadKeyStroke().
331 Retrieve key values for driver user.
332
333 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
334 @param Key The output buffer for key value
335
336 @retval EFI_SUCCESS success to read key stroke
337 **/
338 EFI_STATUS
339 EFIAPI
340 KeyboardReadKeyStroke (
341 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
342 OUT EFI_INPUT_KEY *Key
343 );
344
345 /**
346 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
347 Signal the event if there is key available
348
349 @param Event the event object
350 @param Context waitting context
351
352 **/
353 VOID
354 EFIAPI
355 KeyboardWaitForKey (
356 IN EFI_EVENT Event,
357 IN VOID *Context
358 );
359
360 /**
361 Read status register.
362
363 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
364
365 @return value in status register
366
367 **/
368 UINT8
369 KeyReadStatusRegister (
370 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
371 );
372
373 /**
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.
377
378 @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
379
380 @retval TRUE Keyboard in System.
381 @retval FALSE Keyboard not in System.
382 **/
383 BOOLEAN
384 EFIAPI
385 CheckKeyboardConnect (
386 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
387 );
388
389 /**
390 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
391 Signal the event if there is key available
392
393 @param Event event object
394 @param Context waiting context
395
396 **/
397 VOID
398 EFIAPI
399 KeyboardWaitForKeyEx (
400 IN EFI_EVENT Event,
401 IN VOID *Context
402 );
403
404 //
405 // Simple Text Input Ex protocol function prototypes
406 //
407
408 /**
409 Reset the input device and optionaly run diagnostics
410
411 @param This - Protocol instance pointer.
412 @param ExtendedVerification - Driver may perform diagnostics on reset.
413
414 @retval EFI_SUCCESS - The device was reset.
415 @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
416 not be reset.
417
418 **/
419 EFI_STATUS
420 EFIAPI
421 KeyboardEfiResetEx (
422 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
423 IN BOOLEAN ExtendedVerification
424 );
425
426 /**
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.
429
430
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.
434
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
438 hardware errors.
439 @retval EFI_INVALID_PARAMETER - KeyData is NULL.
440
441 **/
442 EFI_STATUS
443 EFIAPI
444 KeyboardReadKeyStrokeEx (
445 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
446 OUT EFI_KEY_DATA *KeyData
447 );
448
449 /**
450 Set certain state for the input device.
451
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.
455
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.
461
462 **/
463 EFI_STATUS
464 EFIAPI
465 KeyboardSetState (
466 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
467 IN EFI_KEY_TOGGLE_STATE *KeyToggleState
468 );
469
470 /**
471 Register a notification function for a particular keystroke for the input device.
472
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.
479
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.
483
484 **/
485 EFI_STATUS
486 EFIAPI
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
492 );
493
494 /**
495 Remove a registered notification function from a particular keystroke.
496
497 @param This - Protocol instance pointer.
498 @param NotificationHandle - The handle of the notification function being unregistered.
499
500
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.
504
505 **/
506 EFI_STATUS
507 EFIAPI
508 KeyboardUnregisterKeyNotify (
509 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
510 IN EFI_HANDLE NotificationHandle
511 );
512
513 /**
514 Push one key data to the EFI key buffer.
515
516 @param Queue Pointer to instance of EFI_KEY_QUEUE.
517 @param KeyData The key data to push.
518 **/
519 VOID
520 PushEfikeyBufTail (
521 IN EFI_KEY_QUEUE *Queue,
522 IN EFI_KEY_DATA *KeyData
523 );
524
525 /**
526 Judge whether is a registed key
527
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.
532
533 @retval TRUE Key be pressed matches a registered key.
534 @retval FLASE Match failed.
535
536 **/
537 BOOLEAN
538 IsKeyRegistered (
539 IN EFI_KEY_DATA *RegsiteredData,
540 IN EFI_KEY_DATA *InputData
541 );
542 #endif