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