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