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