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