1 /** @file 2 Header file for USB Keyboard Driver's Data Structures. 3 4 Copyright (c) 2004 - 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 #ifndef _EFI_USB_KB_H_ 15 #define _EFI_USB_KB_H_ 16 17 18 #include <Uefi.h> 19 20 #include <Protocol/SimpleTextIn.h> 21 #include <Protocol/SimpleTextInEx.h> 22 #include <Protocol/HiiDatabase.h> 23 #include <Protocol/UsbIo.h> 24 #include <Protocol/DevicePath.h> 25 26 #include <Guid/HiiKeyBoardLayout.h> 27 #include <Guid/UsbKeyBoardLayout.h> 28 29 #include <Library/DebugLib.h> 30 #include <Library/ReportStatusCodeLib.h> 31 #include <Library/BaseMemoryLib.h> 32 #include <Library/UefiRuntimeServicesTableLib.h> 33 #include <Library/UefiDriverEntryPoint.h> 34 #include <Library/UefiBootServicesTableLib.h> 35 #include <Library/UefiLib.h> 36 #include <Library/MemoryAllocationLib.h> 37 #include <Library/PcdLib.h> 38 #include <Library/UefiUsbLib.h> 39 #include <Library/HiiLib.h> 40 41 #include <IndustryStandard/Usb.h> 42 43 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s 44 45 #define MAX_KEY_ALLOWED 32 46 47 #define HZ 1000 * 1000 * 10 48 #define USBKBD_REPEAT_DELAY ((HZ) / 2) 49 #define USBKBD_REPEAT_RATE ((HZ) / 50) 50 51 #define CLASS_HID 3 52 #define SUBCLASS_BOOT 1 53 #define PROTOCOL_KEYBOARD 1 54 55 #define BOOT_PROTOCOL 0 56 #define REPORT_PROTOCOL 1 57 58 typedef struct { 59 BOOLEAN Down; 60 UINT8 KeyCode; 61 } USB_KEY; 62 63 typedef struct { 64 VOID *Buffer[MAX_KEY_ALLOWED + 1]; 65 UINTN Head; 66 UINTN Tail; 67 UINTN ItemSize; 68 } USB_SIMPLE_QUEUE; 69 70 #define USB_KB_DEV_SIGNATURE SIGNATURE_32 ('u', 'k', 'b', 'd') 71 #define USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('u', 'k', 'b', 'x') 72 73 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY { 74 UINTN Signature; 75 EFI_KEY_DATA KeyData; 76 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn; 77 LIST_ENTRY NotifyEntry; 78 } KEYBOARD_CONSOLE_IN_EX_NOTIFY; 79 80 #define USB_NS_KEY_SIGNATURE SIGNATURE_32 ('u', 'n', 's', 'k') 81 82 typedef struct { 83 UINTN Signature; 84 LIST_ENTRY Link; 85 86 // 87 // The number of EFI_NS_KEY_MODIFIER children definitions 88 // 89 UINTN KeyCount; 90 91 // 92 // NsKey[0] : Non-spacing key 93 // NsKey[1] ~ NsKey[KeyCount] : Physical keys 94 // 95 EFI_KEY_DESCRIPTOR *NsKey; 96 } USB_NS_KEY; 97 98 #define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE) 99 100 /// 101 /// Structure to describe USB keyboard device 102 /// 103 typedef struct { 104 UINTN Signature; 105 EFI_HANDLE ControllerHandle; 106 EFI_DEVICE_PATH_PROTOCOL *DevicePath; 107 EFI_EVENT DelayedRecoveryEvent; 108 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput; 109 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx; 110 EFI_USB_IO_PROTOCOL *UsbIo; 111 112 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; 113 EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor; 114 115 USB_SIMPLE_QUEUE UsbKeyQueue; 116 USB_SIMPLE_QUEUE EfiKeyQueue; 117 USB_SIMPLE_QUEUE EfiKeyQueueForNotify; 118 BOOLEAN CtrlOn; 119 BOOLEAN AltOn; 120 BOOLEAN ShiftOn; 121 BOOLEAN NumLockOn; 122 BOOLEAN CapsOn; 123 BOOLEAN ScrollOn; 124 UINT8 LastKeyCodeArray[8]; 125 UINT8 CurKeyCode; 126 127 EFI_EVENT TimerEvent; 128 129 UINT8 RepeatKey; 130 EFI_EVENT RepeatTimer; 131 132 EFI_UNICODE_STRING_TABLE *ControllerNameTable; 133 134 BOOLEAN LeftCtrlOn; 135 BOOLEAN LeftAltOn; 136 BOOLEAN LeftShiftOn; 137 BOOLEAN LeftLogoOn; 138 BOOLEAN RightCtrlOn; 139 BOOLEAN RightAltOn; 140 BOOLEAN RightShiftOn; 141 BOOLEAN RightLogoOn; 142 BOOLEAN MenuKeyOn; 143 BOOLEAN SysReqOn; 144 BOOLEAN AltGrOn; 145 146 BOOLEAN IsSupportPartialKey; 147 148 EFI_KEY_STATE KeyState; 149 // 150 // Notification function list 151 // 152 LIST_ENTRY NotifyList; 153 EFI_EVENT KeyNotifyProcessEvent; 154 155 // 156 // Non-spacing key list 157 // 158 LIST_ENTRY NsKeyList; 159 USB_NS_KEY *CurrentNsKey; 160 EFI_KEY_DESCRIPTOR *KeyConvertionTable; 161 EFI_EVENT KeyboardLayoutEvent; 162 } USB_KB_DEV; 163 164 // 165 // Global Variables 166 // 167 extern EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding; 168 extern EFI_COMPONENT_NAME_PROTOCOL gUsbKeyboardComponentName; 169 extern EFI_COMPONENT_NAME2_PROTOCOL gUsbKeyboardComponentName2; 170 171 #define USB_KB_DEV_FROM_THIS(a) \ 172 CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE) 173 #define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \ 174 CR(a, USB_KB_DEV, SimpleInputEx, USB_KB_DEV_SIGNATURE) 175 176 // 177 // According to Universal Serial Bus HID Usage Tables document ver 1.12, 178 // a Boot Keyboard should support the keycode range from 0x0 to 0x65 and 0xE0 to 0xE7. 179 // 0xE0 to 0xE7 are for modifier keys, and 0x0 to 0x3 are reserved for typical 180 // keyboard status or keyboard errors. 181 // So the number of valid non-modifier USB keycodes is 0x62, and the number of 182 // valid keycodes is 0x6A. 183 // 184 #define NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE 0x62 185 #define NUMBER_OF_VALID_USB_KEYCODE 0x6A 186 // 187 // 0x0 to 0x3 are reserved for typical keyboard status or keyboard errors. 188 // 189 #define USBKBD_VALID_KEYCODE(Key) ((UINT8) (Key) > 3) 190 191 typedef struct { 192 UINT8 NumLock : 1; 193 UINT8 CapsLock : 1; 194 UINT8 ScrollLock : 1; 195 UINT8 Resrvd : 5; 196 } LED_MAP; 197 198 // 199 // Functions of Driver Binding Protocol 200 // 201 /** 202 Check whether USB keyboard driver supports this device. 203 204 @param This The USB keyboard driver binding protocol. 205 @param Controller The controller handle to check. 206 @param RemainingDevicePath The remaining device path. 207 208 @retval EFI_SUCCESS The driver supports this controller. 209 @retval other This device isn't supported. 210 211 **/ 212 EFI_STATUS 213 EFIAPI 214 USBKeyboardDriverBindingSupported ( 215 IN EFI_DRIVER_BINDING_PROTOCOL *This, 216 IN EFI_HANDLE Controller, 217 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath 218 ); 219 220 /** 221 Starts the keyboard device with this driver. 222 223 This function produces Simple Text Input Protocol and Simple Text Input Ex Protocol, 224 initializes the keyboard device, and submit Asynchronous Interrupt Transfer to manage 225 this keyboard device. 226 227 @param This The USB keyboard driver binding instance. 228 @param Controller Handle of device to bind driver to. 229 @param RemainingDevicePath Optional parameter use to pick a specific child 230 device to start. 231 232 @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver. 233 @retval EFI_UNSUPPORTED No interrupt endpoint can be found. 234 @retval Other This controller cannot be started. 235 236 **/ 237 EFI_STATUS 238 EFIAPI 239 USBKeyboardDriverBindingStart ( 240 IN EFI_DRIVER_BINDING_PROTOCOL *This, 241 IN EFI_HANDLE Controller, 242 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath 243 ); 244 245 /** 246 Stop the USB keyboard device handled by this driver. 247 248 @param This The USB keyboard driver binding protocol. 249 @param Controller The controller to release. 250 @param NumberOfChildren The number of handles in ChildHandleBuffer. 251 @param ChildHandleBuffer The array of child handle. 252 253 @retval EFI_SUCCESS The device was stopped. 254 @retval EFI_UNSUPPORTED Simple Text In Protocol or Simple Text In Ex Protocol 255 is not installed on Controller. 256 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error. 257 @retval Others Fail to uninstall protocols attached on the device. 258 259 **/ 260 EFI_STATUS 261 EFIAPI 262 USBKeyboardDriverBindingStop ( 263 IN EFI_DRIVER_BINDING_PROTOCOL *This, 264 IN EFI_HANDLE Controller, 265 IN UINTN NumberOfChildren, 266 IN EFI_HANDLE *ChildHandleBuffer 267 ); 268 269 // 270 // EFI Component Name Functions 271 // 272 /** 273 Retrieves a Unicode string that is the user readable name of the driver. 274 275 This function retrieves the user readable name of a driver in the form of a 276 Unicode string. If the driver specified by This has a user readable name in 277 the language specified by Language, then a pointer to the driver name is 278 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified 279 by This does not support the language specified by Language, 280 then EFI_UNSUPPORTED is returned. 281 282 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or 283 EFI_COMPONENT_NAME_PROTOCOL instance. 284 @param Language A pointer to a Null-terminated ASCII string 285 array indicating the language. This is the 286 language of the driver name that the caller is 287 requesting, and it must match one of the 288 languages specified in SupportedLanguages. The 289 number of languages supported by a driver is up 290 to the driver writer. Language is specified 291 in RFC 4646 or ISO 639-2 language code format. 292 @param DriverName A pointer to the Unicode string to return. 293 This Unicode string is the name of the 294 driver specified by This in the language 295 specified by Language. 296 297 @retval EFI_SUCCESS The Unicode string for the Driver specified by 298 This and the language specified by Language was 299 returned in DriverName. 300 @retval EFI_INVALID_PARAMETER Language is NULL. 301 @retval EFI_INVALID_PARAMETER DriverName is NULL. 302 @retval EFI_UNSUPPORTED The driver specified by This does not support 303 the language specified by Language. 304 305 **/ 306 EFI_STATUS 307 EFIAPI 308 UsbKeyboardComponentNameGetDriverName ( 309 IN EFI_COMPONENT_NAME_PROTOCOL *This, 310 IN CHAR8 *Language, 311 OUT CHAR16 **DriverName 312 ); 313 314 /** 315 Retrieves a Unicode string that is the user readable name of the controller 316 that is being managed by a driver. 317 318 This function retrieves the user readable name of the controller specified by 319 ControllerHandle and ChildHandle in the form of a Unicode string. If the 320 driver specified by This has a user readable name in the language specified by 321 Language, then a pointer to the controller name is returned in ControllerName, 322 and EFI_SUCCESS is returned. If the driver specified by This is not currently 323 managing the controller specified by ControllerHandle and ChildHandle, 324 then EFI_UNSUPPORTED is returned. If the driver specified by This does not 325 support the language specified by Language, then EFI_UNSUPPORTED is returned. 326 327 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or 328 EFI_COMPONENT_NAME_PROTOCOL instance. 329 @param ControllerHandle The handle of a controller that the driver 330 specified by This is managing. This handle 331 specifies the controller whose name is to be 332 returned. 333 @param ChildHandle The handle of the child controller to retrieve 334 the name of. This is an optional parameter that 335 may be NULL. It will be NULL for device 336 drivers. It will also be NULL for a bus drivers 337 that wish to retrieve the name of the bus 338 controller. It will not be NULL for a bus 339 driver that wishes to retrieve the name of a 340 child controller. 341 @param Language A pointer to a Null-terminated ASCII string 342 array indicating the language. This is the 343 language of the driver name that the caller is 344 requesting, and it must match one of the 345 languages specified in SupportedLanguages. The 346 number of languages supported by a driver is up 347 to the driver writer. Language is specified in 348 RFC 4646 or ISO 639-2 language code format. 349 @param ControllerName A pointer to the Unicode string to return. 350 This Unicode string is the name of the 351 controller specified by ControllerHandle and 352 ChildHandle in the language specified by 353 Language from the point of view of the driver 354 specified by This. 355 356 @retval EFI_SUCCESS The Unicode string for the user readable name in 357 the language specified by Language for the 358 driver specified by This was returned in 359 DriverName. 360 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE. 361 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid 362 EFI_HANDLE. 363 @retval EFI_INVALID_PARAMETER Language is NULL. 364 @retval EFI_INVALID_PARAMETER ControllerName is NULL. 365 @retval EFI_UNSUPPORTED The driver specified by This is not currently 366 managing the controller specified by 367 ControllerHandle and ChildHandle. 368 @retval EFI_UNSUPPORTED The driver specified by This does not support 369 the language specified by Language. 370 371 **/ 372 EFI_STATUS 373 EFIAPI 374 UsbKeyboardComponentNameGetControllerName ( 375 IN EFI_COMPONENT_NAME_PROTOCOL *This, 376 IN EFI_HANDLE ControllerHandle, 377 IN EFI_HANDLE ChildHandle OPTIONAL, 378 IN CHAR8 *Language, 379 OUT CHAR16 **ControllerName 380 ); 381 382 // 383 // Functions of Simple Text Input Protocol 384 // 385 /** 386 Reset the input device and optionaly run diagnostics 387 388 There are 2 types of reset for USB keyboard. 389 For non-exhaustive reset, only keyboard buffer is cleared. 390 For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status 391 is also re-initialized. 392 393 @param This Protocol instance pointer. 394 @param ExtendedVerification Driver may perform diagnostics on reset. 395 396 @retval EFI_SUCCESS The device was reset. 397 @retval EFI_DEVICE_ERROR The device is not functioning properly and could not be reset. 398 399 **/ 400 EFI_STATUS 401 EFIAPI 402 USBKeyboardReset ( 403 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, 404 IN BOOLEAN ExtendedVerification 405 ); 406 407 /** 408 Reads the next keystroke from the input device. 409 410 @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance. 411 @param Key A pointer to a buffer that is filled in with the keystroke 412 information for the key that was pressed. 413 414 @retval EFI_SUCCESS The keystroke information was returned. 415 @retval EFI_NOT_READY There was no keystroke data availiable. 416 @retval EFI_DEVICE_ERROR The keydtroke information was not returned due to 417 hardware errors. 418 419 **/ 420 EFI_STATUS 421 EFIAPI 422 USBKeyboardReadKeyStroke ( 423 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, 424 OUT EFI_INPUT_KEY *Key 425 ); 426 427 // 428 // Simple Text Input Ex protocol functions 429 // 430 /** 431 Resets the input device hardware. 432 433 The Reset() function resets the input device hardware. As part 434 of initialization process, the firmware/device will make a quick 435 but reasonable attempt to verify that the device is functioning. 436 If the ExtendedVerification flag is TRUE the firmware may take 437 an extended amount of time to verify the device is operating on 438 reset. Otherwise the reset operation is to occur as quickly as 439 possible. The hardware verification process is not defined by 440 this specification and is left up to the platform firmware or 441 driver to implement. 442 443 @param This A pointer to the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL instance. 444 445 @param ExtendedVerification Indicates that the driver may perform a more exhaustive 446 verification operation of the device during reset. 447 448 @retval EFI_SUCCESS The device was reset. 449 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset. 450 451 **/ 452 EFI_STATUS 453 EFIAPI 454 USBKeyboardResetEx ( 455 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 456 IN BOOLEAN ExtendedVerification 457 ); 458 459 /** 460 Reads the next keystroke from the input device. 461 462 @param This Protocol instance pointer. 463 @param KeyData A pointer to a buffer that is filled in with the keystroke 464 state data for the key that was pressed. 465 466 @retval EFI_SUCCESS The keystroke information was returned. 467 @retval EFI_NOT_READY There was no keystroke data available. 468 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to 469 hardware errors. 470 @retval EFI_INVALID_PARAMETER KeyData is NULL. 471 472 **/ 473 EFI_STATUS 474 EFIAPI 475 USBKeyboardReadKeyStrokeEx ( 476 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 477 OUT EFI_KEY_DATA *KeyData 478 ); 479 480 /** 481 Set certain state for the input device. 482 483 @param This Protocol instance pointer. 484 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the 485 state for the input device. 486 487 @retval EFI_SUCCESS The device state was set appropriately. 488 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could 489 not have the setting adjusted. 490 @retval EFI_UNSUPPORTED The device does not support the ability to have its state set. 491 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL. 492 493 **/ 494 EFI_STATUS 495 EFIAPI 496 USBKeyboardSetState ( 497 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 498 IN EFI_KEY_TOGGLE_STATE *KeyToggleState 499 ); 500 501 /** 502 Register a notification function for a particular keystroke for the input device. 503 504 @param This Protocol instance pointer. 505 @param KeyData A pointer to a buffer that is filled in with the keystroke 506 information data for the key that was pressed. 507 @param KeyNotificationFunction Points to the function to be called when the key 508 sequence is typed specified by KeyData. 509 @param NotifyHandle Points to the unique handle assigned to the registered notification. 510 511 @retval EFI_SUCCESS The notification function was registered successfully. 512 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures. 513 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL. 514 515 **/ 516 EFI_STATUS 517 EFIAPI 518 USBKeyboardRegisterKeyNotify ( 519 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 520 IN EFI_KEY_DATA *KeyData, 521 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction, 522 OUT VOID **NotifyHandle 523 ); 524 525 /** 526 Remove a registered notification function from a particular keystroke. 527 528 @param This Protocol instance pointer. 529 @param NotificationHandle The handle of the notification function being unregistered. 530 531 @retval EFI_SUCCESS The notification function was unregistered successfully. 532 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid 533 @retval EFI_NOT_FOUND Cannot find the matching entry in database. 534 535 **/ 536 EFI_STATUS 537 EFIAPI 538 USBKeyboardUnregisterKeyNotify ( 539 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This, 540 IN VOID *NotificationHandle 541 ); 542 543 /** 544 Event notification function registered for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx 545 and EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey. 546 547 @param Event Event to be signaled when a key is pressed. 548 @param Context Points to USB_KB_DEV instance. 549 550 **/ 551 VOID 552 EFIAPI 553 USBKeyboardWaitForKey ( 554 IN EFI_EVENT Event, 555 IN VOID *Context 556 ); 557 558 /** 559 Free keyboard notify list. 560 561 @param NotifyList The keyboard notify list to free. 562 563 @retval EFI_SUCCESS Free the notify list successfully. 564 @retval EFI_INVALID_PARAMETER NotifyList is NULL. 565 566 **/ 567 EFI_STATUS 568 KbdFreeNotifyList ( 569 IN OUT LIST_ENTRY *NotifyList 570 ); 571 572 /** 573 Check whether the pressed key matches a registered key or not. 574 575 @param RegsiteredData A pointer to keystroke data for the key that was registered. 576 @param InputData A pointer to keystroke data for the key that was pressed. 577 578 @retval TRUE Key pressed matches a registered key. 579 @retval FLASE Key pressed does not matche a registered key. 580 581 **/ 582 BOOLEAN 583 IsKeyRegistered ( 584 IN EFI_KEY_DATA *RegsiteredData, 585 IN EFI_KEY_DATA *InputData 586 ); 587 588 /** 589 Timer handler to convert the key from USB. 590 591 @param Event Indicates the event that invoke this function. 592 @param Context Indicates the calling context. 593 **/ 594 VOID 595 EFIAPI 596 USBKeyboardTimerHandler ( 597 IN EFI_EVENT Event, 598 IN VOID *Context 599 ); 600 601 /** 602 Process key notify. 603 604 @param Event Indicates the event that invoke this function. 605 @param Context Indicates the calling context. 606 **/ 607 VOID 608 EFIAPI 609 KeyNotifyProcessHandler ( 610 IN EFI_EVENT Event, 611 IN VOID *Context 612 ); 613 614 #endif 615 616