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