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