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