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