]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2Keyboard.h
IntelFrameworkModule/Ps2Kb: ReadKeyStrokeEx always return key state
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2Keyboard.h
1 /** @file
2 PS/2 keyboard driver header file
3
4 Copyright (c) 2006 - 2018, 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 <Uefi.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 #include <Library/PcdLib.h>
37
38 //
39 // Global Variables
40 //
41 extern EFI_DRIVER_BINDING_PROTOCOL gKeyboardControllerDriver;
42 extern EFI_COMPONENT_NAME_PROTOCOL gPs2KeyboardComponentName;
43 extern EFI_COMPONENT_NAME2_PROTOCOL gPs2KeyboardComponentName2;
44
45 //
46 // Driver Private Data
47 //
48 #define KEYBOARD_CONSOLE_IN_DEV_SIGNATURE SIGNATURE_32 ('k', 'k', 'e', 'y')
49 #define KEYBOARD_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('k', 'c', 'e', 'n')
50
51 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY {
52 UINTN Signature;
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 LeftCtrl;
87 BOOLEAN RightCtrl;
88 BOOLEAN LeftAlt;
89 BOOLEAN RightAlt;
90 BOOLEAN LeftShift;
91 BOOLEAN RightShift;
92 BOOLEAN LeftLogo;
93 BOOLEAN RightLogo;
94 BOOLEAN Menu;
95 BOOLEAN SysReq;
96
97 BOOLEAN CapsLock;
98 BOOLEAN NumLock;
99 BOOLEAN ScrollLock;
100
101 BOOLEAN IsSupportPartialKey;
102 //
103 // Queue storing key scancodes
104 //
105 SCAN_CODE_QUEUE ScancodeQueue;
106 EFI_KEY_QUEUE EfiKeyQueue;
107 EFI_KEY_QUEUE EfiKeyQueueForNotify;
108
109 //
110 // Error state
111 //
112 BOOLEAN KeyboardErr;
113
114 EFI_UNICODE_STRING_TABLE *ControllerNameTable;
115
116 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
117 //
118 // Notification Function List
119 //
120 LIST_ENTRY NotifyList;
121 EFI_EVENT KeyNotifyProcessEvent;
122 } KEYBOARD_CONSOLE_IN_DEV;
123
124 #define KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) CR (a, KEYBOARD_CONSOLE_IN_DEV, ConIn, KEYBOARD_CONSOLE_IN_DEV_SIGNATURE)
125 #define TEXT_INPUT_EX_KEYBOARD_CONSOLE_IN_DEV_FROM_THIS(a) \
126 CR (a, \
127 KEYBOARD_CONSOLE_IN_DEV, \
128 ConInEx, \
129 KEYBOARD_CONSOLE_IN_DEV_SIGNATURE \
130 )
131
132 #define TABLE_END 0x0
133
134 //
135 // Driver entry point
136 //
137 /**
138 The user Entry Point for module Ps2Keyboard. The user code starts with this function.
139
140 @param[in] ImageHandle The firmware allocated handle for the EFI image.
141 @param[in] SystemTable A pointer to the EFI System Table.
142
143 @retval EFI_SUCCESS The entry point is executed successfully.
144 @retval other Some error occurs when executing this entry point.
145
146 **/
147 EFI_STATUS
148 EFIAPI
149 InstallPs2KeyboardDriver (
150 IN EFI_HANDLE ImageHandle,
151 IN EFI_SYSTEM_TABLE *SystemTable
152 );
153
154 #define KEYBOARD_8042_DATA_REGISTER 0x60
155 #define KEYBOARD_8042_STATUS_REGISTER 0x64
156 #define KEYBOARD_8042_COMMAND_REGISTER 0x64
157
158 #define KEYBOARD_KBEN 0xF4
159 #define KEYBOARD_CMDECHO_ACK 0xFA
160
161 #define KEYBOARD_MAX_TRY 256 // 256
162 #define KEYBOARD_TIMEOUT 65536 // 0.07s
163 #define KEYBOARD_WAITFORVALUE_TIMEOUT 1000000 // 1s
164 #define KEYBOARD_BAT_TIMEOUT 4000000 // 4s
165 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
166 #define SCANCODE_EXTENDED0 0xE0
167 #define SCANCODE_EXTENDED1 0xE1
168 #define SCANCODE_CTRL_MAKE 0x1D
169 #define SCANCODE_CTRL_BREAK 0x9D
170 #define SCANCODE_ALT_MAKE 0x38
171 #define SCANCODE_ALT_BREAK 0xB8
172 #define SCANCODE_LEFT_SHIFT_MAKE 0x2A
173 #define SCANCODE_LEFT_SHIFT_BREAK 0xAA
174 #define SCANCODE_RIGHT_SHIFT_MAKE 0x36
175 #define SCANCODE_RIGHT_SHIFT_BREAK 0xB6
176 #define SCANCODE_CAPS_LOCK_MAKE 0x3A
177 #define SCANCODE_NUM_LOCK_MAKE 0x45
178 #define SCANCODE_SCROLL_LOCK_MAKE 0x46
179 #define SCANCODE_DELETE_MAKE 0x53
180 #define SCANCODE_LEFT_LOGO_MAKE 0x5B //GUI key defined in Keyboard scan code
181 #define SCANCODE_LEFT_LOGO_BREAK 0xDB
182 #define SCANCODE_RIGHT_LOGO_MAKE 0x5C
183 #define SCANCODE_RIGHT_LOGO_BREAK 0xDC
184 #define SCANCODE_MENU_MAKE 0x5D //APPS key defined in Keyboard scan code
185 #define SCANCODE_MENU_BREAK 0xDD
186 #define SCANCODE_SYS_REQ_MAKE 0x37
187 #define SCANCODE_SYS_REQ_BREAK 0xB7
188 #define SCANCODE_SYS_REQ_MAKE_WITH_ALT 0x54
189 #define SCANCODE_SYS_REQ_BREAK_WITH_ALT 0xD4
190
191 #define SCANCODE_MAX_MAKE 0x60
192
193
194 #define KEYBOARD_STATUS_REGISTER_HAS_OUTPUT_DATA BIT0 ///< 0 - Output register has no data; 1 - Output register has data
195 #define KEYBOARD_STATUS_REGISTER_HAS_INPUT_DATA BIT1 ///< 0 - Input register has no data; 1 - Input register has data
196 #define KEYBOARD_STATUS_REGISTER_SYSTEM_FLAG BIT2 ///< Set to 0 after power on reset
197 #define KEYBOARD_STATUS_REGISTER_INPUT_DATA_TYPE BIT3 ///< 0 - Data in input register is data; 1 - Data in input register is command
198 #define KEYBOARD_STATUS_REGISTER_ENABLE_FLAG BIT4 ///< 0 - Keyboard is disable; 1 - Keyboard is enable
199 #define KEYBOARD_STATUS_REGISTER_TRANSMIT_TIMEOUT BIT5 ///< 0 - Transmit is complete without timeout; 1 - Transmit is timeout without complete
200 #define KEYBOARD_STATUS_REGISTER_RECEIVE_TIMEOUT BIT6 ///< 0 - Receive is complete without timeout; 1 - Receive is timeout without complete
201 #define KEYBOARD_STATUS_REGISTER_PARITY BIT7 ///< 0 - Odd parity; 1 - Even parity
202
203 #define KEYBOARD_8042_COMMAND_READ 0x20
204 #define KEYBOARD_8042_COMMAND_WRITE 0x60
205 #define KEYBOARD_8042_COMMAND_DISABLE_MOUSE_INTERFACE 0xA7
206 #define KEYBOARD_8042_COMMAND_ENABLE_MOUSE_INTERFACE 0xA8
207 #define KEYBOARD_8042_COMMAND_CONTROLLER_SELF_TEST 0xAA
208 #define KEYBOARD_8042_COMMAND_KEYBOARD_INTERFACE_SELF_TEST 0xAB
209 #define KEYBOARD_8042_COMMAND_DISABLE_KEYBOARD_INTERFACE 0xAD
210
211 #define KEYBOARD_8048_COMMAND_CLEAR_OUTPUT_DATA 0xF4
212 #define KEYBOARD_8048_COMMAND_RESET 0xFF
213 #define KEYBOARD_8048_COMMAND_SELECT_SCAN_CODE_SET 0xF0
214
215 #define KEYBOARD_8048_RETURN_8042_BAT_SUCCESS 0xAA
216 #define KEYBOARD_8048_RETURN_8042_BAT_ERROR 0xFC
217 #define KEYBOARD_8048_RETURN_8042_ACK 0xFA
218
219
220 //
221 // Keyboard Controller Status
222 //
223 #define KBC_PARE 0x80 // Parity Error
224 #define KBC_TIM 0x40 // General Time Out
225
226 //
227 // Other functions that are used among .c files
228 //
229 /**
230 Show keyboard status lights according to
231 indicators in ConsoleIn.
232
233 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
234
235 @return status
236
237 **/
238 EFI_STATUS
239 UpdateStatusLights (
240 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
241 );
242
243 /**
244 write key to keyboard.
245
246 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
247 @param Data value wanted to be written
248
249 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
250 @retval EFI_SUCCESS - GC_TODO: Add description for return value
251
252 **/
253 EFI_STATUS
254 KeyboardRead (
255 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
256 OUT UINT8 *Data
257 );
258
259 /**
260 Get scancode from scancode buffer and translate into EFI-scancode and unicode defined by EFI spec.
261
262 The function is always called in TPL_NOTIFY.
263
264 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
265
266 **/
267 VOID
268 KeyGetchar (
269 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
270 );
271
272 /**
273 Process key notify.
274
275 @param Event Indicates the event that invoke this function.
276 @param Context Indicates the calling context.
277 **/
278 VOID
279 EFIAPI
280 KeyNotifyProcessHandler (
281 IN EFI_EVENT Event,
282 IN VOID *Context
283 );
284
285 /**
286 Perform 8042 controller and keyboard Initialization.
287 If ExtendedVerification is TRUE, do additional test for
288 the keyboard interface
289
290 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
291 @param ExtendedVerification - indicates a thorough initialization
292
293 @retval EFI_DEVICE_ERROR Fail to init keyboard
294 @retval EFI_SUCCESS Success to init keyboard
295 **/
296 EFI_STATUS
297 InitKeyboard (
298 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
299 IN BOOLEAN ExtendedVerification
300 );
301
302 /**
303 Disable the keyboard interface of the 8042 controller.
304
305 @param ConsoleIn - the device instance
306
307 @return status of issuing disable command
308
309 **/
310 EFI_STATUS
311 DisableKeyboard (
312 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
313 );
314
315 /**
316 Timer event handler: read a series of scancodes from 8042
317 and put them into memory scancode buffer.
318 it read as much scancodes to either fill
319 the memory buffer or empty the keyboard buffer.
320 It is registered as running under TPL_NOTIFY
321
322 @param Event - The timer event
323 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
324
325 **/
326 VOID
327 EFIAPI
328 KeyboardTimerHandler (
329 IN EFI_EVENT Event,
330 IN VOID *Context
331 );
332
333 /**
334 logic reset keyboard
335 Implement SIMPLE_TEXT_IN.Reset()
336 Perform 8042 controller and keyboard initialization
337
338 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
339 @param ExtendedVerification Indicate that the driver may perform a more
340 exhaustive verification operation of the device during
341 reset, now this par is ignored in this driver
342
343 **/
344 EFI_STATUS
345 EFIAPI
346 KeyboardEfiReset (
347 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
348 IN BOOLEAN ExtendedVerification
349 );
350
351 /**
352 Implement SIMPLE_TEXT_IN.ReadKeyStroke().
353 Retrieve key values for driver user.
354
355 @param This Pointer to instance of EFI_SIMPLE_TEXT_INPUT_PROTOCOL
356 @param Key The output buffer for key value
357
358 @retval EFI_SUCCESS success to read key stroke
359 **/
360 EFI_STATUS
361 EFIAPI
362 KeyboardReadKeyStroke (
363 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
364 OUT EFI_INPUT_KEY *Key
365 );
366
367 /**
368 Event notification function for SIMPLE_TEXT_IN.WaitForKey event
369 Signal the event if there is key available
370
371 @param Event the event object
372 @param Context waitting context
373
374 **/
375 VOID
376 EFIAPI
377 KeyboardWaitForKey (
378 IN EFI_EVENT Event,
379 IN VOID *Context
380 );
381
382 /**
383 Read status register.
384
385 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
386
387 @return value in status register
388
389 **/
390 UINT8
391 KeyReadStatusRegister (
392 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
393 );
394
395 /**
396 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
397 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
398 should not be in system.
399
400 @param[in] ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
401
402 @retval TRUE Keyboard in System.
403 @retval FALSE Keyboard not in System.
404 **/
405 BOOLEAN
406 EFIAPI
407 CheckKeyboardConnect (
408 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
409 );
410
411 /**
412 Event notification function for SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
413 Signal the event if there is key available
414
415 @param Event event object
416 @param Context waiting context
417
418 **/
419 VOID
420 EFIAPI
421 KeyboardWaitForKeyEx (
422 IN EFI_EVENT Event,
423 IN VOID *Context
424 );
425
426 //
427 // Simple Text Input Ex protocol function prototypes
428 //
429
430 /**
431 Reset the input device and optionaly run diagnostics
432
433 @param This - Protocol instance pointer.
434 @param ExtendedVerification - Driver may perform diagnostics on reset.
435
436 @retval EFI_SUCCESS - The device was reset.
437 @retval EFI_DEVICE_ERROR - The device is not functioning properly and could
438 not be reset.
439
440 **/
441 EFI_STATUS
442 EFIAPI
443 KeyboardEfiResetEx (
444 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
445 IN BOOLEAN ExtendedVerification
446 );
447
448 /**
449 Reads the next keystroke from the input device. The WaitForKey Event can
450 be used to test for existance of a keystroke via WaitForEvent () call.
451
452
453 @param This - Protocol instance pointer.
454 @param KeyData - A pointer to a buffer that is filled in with the keystroke
455 state data for the key that was pressed.
456
457 @retval EFI_SUCCESS - The keystroke information was returned.
458 @retval EFI_NOT_READY - There was no keystroke data availiable.
459 @retval EFI_DEVICE_ERROR - The keystroke information was not returned due to
460 hardware errors.
461 @retval EFI_INVALID_PARAMETER - KeyData is NULL.
462
463 **/
464 EFI_STATUS
465 EFIAPI
466 KeyboardReadKeyStrokeEx (
467 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
468 OUT EFI_KEY_DATA *KeyData
469 );
470
471 /**
472 Set certain state for the input device.
473
474 @param This - Protocol instance pointer.
475 @param KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
476 state for the input device.
477
478 @retval EFI_SUCCESS - The device state was set successfully.
479 @retval EFI_DEVICE_ERROR - The device is not functioning correctly and could
480 not have the setting adjusted.
481 @retval EFI_UNSUPPORTED - The device does not have the ability to set its state.
482 @retval EFI_INVALID_PARAMETER - KeyToggleState is NULL.
483
484 **/
485 EFI_STATUS
486 EFIAPI
487 KeyboardSetState (
488 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
489 IN EFI_KEY_TOGGLE_STATE *KeyToggleState
490 );
491
492 /**
493 Register a notification function for a particular keystroke for the input device.
494
495 @param This - Protocol instance pointer.
496 @param KeyData - A pointer to a buffer that is filled in with the keystroke
497 information data for the key that was pressed. If KeyData.Key,
498 KeyData.KeyState.KeyToggleState and KeyData.KeyState.KeyShiftState
499 are 0, then any incomplete keystroke will trigger a notification of
500 the KeyNotificationFunction.
501 @param KeyNotificationFunction - Points to the function to be called when the key
502 sequence is typed specified by KeyData. This notification function
503 should be called at <=TPL_CALLBACK.
504 @param NotifyHandle - Points to the unique handle assigned to the registered notification.
505
506 @retval EFI_SUCCESS - The notification function was registered successfully.
507 @retval EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
508 @retval EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
509
510 **/
511 EFI_STATUS
512 EFIAPI
513 KeyboardRegisterKeyNotify (
514 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
515 IN EFI_KEY_DATA *KeyData,
516 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
517 OUT VOID **NotifyHandle
518 );
519
520 /**
521 Remove a registered notification function from a particular keystroke.
522
523 @param This - Protocol instance pointer.
524 @param NotificationHandle - The handle of the notification function being unregistered.
525
526
527 @retval EFI_SUCCESS - The notification function was unregistered successfully.
528 @retval EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
529 @retval EFI_NOT_FOUND - Can not find the matching entry in database.
530
531 **/
532 EFI_STATUS
533 EFIAPI
534 KeyboardUnregisterKeyNotify (
535 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
536 IN VOID *NotificationHandle
537 );
538
539 /**
540 Push one key data to the EFI key buffer.
541
542 @param Queue Pointer to instance of EFI_KEY_QUEUE.
543 @param KeyData The key data to push.
544 **/
545 VOID
546 PushEfikeyBufTail (
547 IN EFI_KEY_QUEUE *Queue,
548 IN EFI_KEY_DATA *KeyData
549 );
550
551 /**
552 Judge whether is a registed key
553
554 @param RegsiteredData A pointer to a buffer that is filled in with the keystroke
555 state data for the key that was registered.
556 @param InputData A pointer to a buffer that is filled in with the keystroke
557 state data for the key that was pressed.
558
559 @retval TRUE Key be pressed matches a registered key.
560 @retval FLASE Match failed.
561
562 **/
563 BOOLEAN
564 IsKeyRegistered (
565 IN EFI_KEY_DATA *RegsiteredData,
566 IN EFI_KEY_DATA *InputData
567 );
568
569 /**
570 Initialize the key state.
571
572 @param ConsoleIn The KEYBOARD_CONSOLE_IN_DEV instance.
573 @param KeyState A pointer to receive the key state information.
574 **/
575 VOID
576 InitializeKeyState (
577 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
578 OUT EFI_KEY_STATE *KeyState
579 );
580
581 #endif