2 Header file for USB Keyboard Driver's Data Structures.
4 Copyright (c) 2004 - 2011, 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
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.
14 #ifndef _EFI_USB_KB_H_
15 #define _EFI_USB_KB_H_
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>
26 #include <Guid/HiiKeyBoardLayout.h>
27 #include <Guid/UsbKeyBoardLayout.h>
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>
41 #include <IndustryStandard/Usb.h>
43 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
45 #define MAX_KEY_ALLOWED 32
47 #define HZ 1000 * 1000 * 10
48 #define USBKBD_REPEAT_DELAY ((HZ) / 2)
49 #define USBKBD_REPEAT_RATE ((HZ) / 50)
52 #define SUBCLASS_BOOT 1
53 #define PROTOCOL_KEYBOARD 1
55 #define BOOT_PROTOCOL 0
56 #define REPORT_PROTOCOL 1
64 VOID
*Buffer
[MAX_KEY_ALLOWED
+ 1];
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')
73 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY
{
75 EFI_HANDLE NotifyHandle
;
77 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
78 LIST_ENTRY NotifyEntry
;
79 } KEYBOARD_CONSOLE_IN_EX_NOTIFY
;
81 #define USB_NS_KEY_SIGNATURE SIGNATURE_32 ('u', 'n', 's', 'k')
88 // The number of EFI_NS_KEY_MODIFIER children definitions
93 // NsKey[0] : Non-spacing key
94 // NsKey[1] ~ NsKey[KeyCount] : Physical keys
96 EFI_KEY_DESCRIPTOR
*NsKey
;
99 #define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE)
102 /// Structure to describe USB keyboard device
106 EFI_HANDLE ControllerHandle
;
107 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
108 EFI_EVENT DelayedRecoveryEvent
;
109 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput
;
110 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx
;
111 EFI_USB_IO_PROTOCOL
*UsbIo
;
113 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
114 EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor
;
116 USB_SIMPLE_QUEUE UsbKeyQueue
;
117 USB_SIMPLE_QUEUE EfiKeyQueue
;
124 UINT8 LastKeyCodeArray
[8];
127 EFI_EVENT TimerEvent
;
130 EFI_EVENT RepeatTimer
;
132 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
140 BOOLEAN RightShiftOn
;
146 BOOLEAN IsSupportPartialKey
;
148 EFI_KEY_STATE KeyState
;
150 // Notification function list
152 LIST_ENTRY NotifyList
;
155 // Non-spacing key list
157 LIST_ENTRY NsKeyList
;
158 USB_NS_KEY
*CurrentNsKey
;
159 EFI_KEY_DESCRIPTOR
*KeyConvertionTable
;
160 EFI_EVENT KeyboardLayoutEvent
;
166 extern EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding
;
167 extern EFI_COMPONENT_NAME_PROTOCOL gUsbKeyboardComponentName
;
168 extern EFI_COMPONENT_NAME2_PROTOCOL gUsbKeyboardComponentName2
;
170 #define USB_KB_DEV_FROM_THIS(a) \
171 CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE)
172 #define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \
173 CR(a, USB_KB_DEV, SimpleInputEx, USB_KB_DEV_SIGNATURE)
176 // According to Universal Serial Bus HID Usage Tables document ver 1.12,
177 // a Boot Keyboard should support the keycode range from 0x0 to 0x65 and 0xE0 to 0xE7.
178 // 0xE0 to 0xE7 are for modifier keys, and 0x0 to 0x3 are reserved for typical
179 // keyboard status or keyboard errors.
180 // So the number of valid non-modifier USB keycodes is 0x62, and the number of
181 // valid keycodes is 0x6A.
183 #define NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE 0x62
184 #define NUMBER_OF_VALID_USB_KEYCODE 0x6A
186 // 0x0 to 0x3 are reserved for typical keyboard status or keyboard errors.
188 #define USBKBD_VALID_KEYCODE(Key) ((UINT8) (Key) > 3)
193 UINT8 ScrollLock
: 1;
198 // Functions of Driver Binding Protocol
201 Check whether USB keyboard driver supports this device.
203 @param This The USB keyboard driver binding protocol.
204 @param Controller The controller handle to check.
205 @param RemainingDevicePath The remaining device path.
207 @retval EFI_SUCCESS The driver supports this controller.
208 @retval other This device isn't supported.
213 USBKeyboardDriverBindingSupported (
214 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
215 IN EFI_HANDLE Controller
,
216 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
220 Starts the keyboard device with this driver.
222 This function produces Simple Text Input Protocol and Simple Text Input Ex Protocol,
223 initializes the keyboard device, and submit Asynchronous Interrupt Transfer to manage
224 this keyboard device.
226 @param This The USB keyboard driver binding instance.
227 @param Controller Handle of device to bind driver to.
228 @param RemainingDevicePath Optional parameter use to pick a specific child
231 @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver.
232 @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
233 @retval Other This controller cannot be started.
238 USBKeyboardDriverBindingStart (
239 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
240 IN EFI_HANDLE Controller
,
241 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
245 Stop the USB keyboard device handled by this driver.
247 @param This The USB keyboard driver binding protocol.
248 @param Controller The controller to release.
249 @param NumberOfChildren The number of handles in ChildHandleBuffer.
250 @param ChildHandleBuffer The array of child handle.
252 @retval EFI_SUCCESS The device was stopped.
253 @retval EFI_UNSUPPORTED Simple Text In Protocol or Simple Text In Ex Protocol
254 is not installed on Controller.
255 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
256 @retval Others Fail to uninstall protocols attached on the device.
261 USBKeyboardDriverBindingStop (
262 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
263 IN EFI_HANDLE Controller
,
264 IN UINTN NumberOfChildren
,
265 IN EFI_HANDLE
*ChildHandleBuffer
269 // EFI Component Name Functions
272 Retrieves a Unicode string that is the user readable name of the driver.
274 This function retrieves the user readable name of a driver in the form of a
275 Unicode string. If the driver specified by This has a user readable name in
276 the language specified by Language, then a pointer to the driver name is
277 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
278 by This does not support the language specified by Language,
279 then EFI_UNSUPPORTED is returned.
281 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
282 EFI_COMPONENT_NAME_PROTOCOL instance.
283 @param Language A pointer to a Null-terminated ASCII string
284 array indicating the language. This is the
285 language of the driver name that the caller is
286 requesting, and it must match one of the
287 languages specified in SupportedLanguages. The
288 number of languages supported by a driver is up
289 to the driver writer. Language is specified
290 in RFC 4646 or ISO 639-2 language code format.
291 @param DriverName A pointer to the Unicode string to return.
292 This Unicode string is the name of the
293 driver specified by This in the language
294 specified by Language.
296 @retval EFI_SUCCESS The Unicode string for the Driver specified by
297 This and the language specified by Language was
298 returned in DriverName.
299 @retval EFI_INVALID_PARAMETER Language is NULL.
300 @retval EFI_INVALID_PARAMETER DriverName is NULL.
301 @retval EFI_UNSUPPORTED The driver specified by This does not support
302 the language specified by Language.
307 UsbKeyboardComponentNameGetDriverName (
308 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
310 OUT CHAR16
**DriverName
314 Retrieves a Unicode string that is the user readable name of the controller
315 that is being managed by a driver.
317 This function retrieves the user readable name of the controller specified by
318 ControllerHandle and ChildHandle in the form of a Unicode string. If the
319 driver specified by This has a user readable name in the language specified by
320 Language, then a pointer to the controller name is returned in ControllerName,
321 and EFI_SUCCESS is returned. If the driver specified by This is not currently
322 managing the controller specified by ControllerHandle and ChildHandle,
323 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
324 support the language specified by Language, then EFI_UNSUPPORTED is returned.
326 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
327 EFI_COMPONENT_NAME_PROTOCOL instance.
328 @param ControllerHandle The handle of a controller that the driver
329 specified by This is managing. This handle
330 specifies the controller whose name is to be
332 @param ChildHandle The handle of the child controller to retrieve
333 the name of. This is an optional parameter that
334 may be NULL. It will be NULL for device
335 drivers. It will also be NULL for a bus drivers
336 that wish to retrieve the name of the bus
337 controller. It will not be NULL for a bus
338 driver that wishes to retrieve the name of a
340 @param Language A pointer to a Null-terminated ASCII string
341 array indicating the language. This is the
342 language of the driver name that the caller is
343 requesting, and it must match one of the
344 languages specified in SupportedLanguages. The
345 number of languages supported by a driver is up
346 to the driver writer. Language is specified in
347 RFC 4646 or ISO 639-2 language code format.
348 @param ControllerName A pointer to the Unicode string to return.
349 This Unicode string is the name of the
350 controller specified by ControllerHandle and
351 ChildHandle in the language specified by
352 Language from the point of view of the driver
355 @retval EFI_SUCCESS The Unicode string for the user readable name in
356 the language specified by Language for the
357 driver specified by This was returned in
359 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
360 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
362 @retval EFI_INVALID_PARAMETER Language is NULL.
363 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
364 @retval EFI_UNSUPPORTED The driver specified by This is not currently
365 managing the controller specified by
366 ControllerHandle and ChildHandle.
367 @retval EFI_UNSUPPORTED The driver specified by This does not support
368 the language specified by Language.
373 UsbKeyboardComponentNameGetControllerName (
374 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
375 IN EFI_HANDLE ControllerHandle
,
376 IN EFI_HANDLE ChildHandle OPTIONAL
,
378 OUT CHAR16
**ControllerName
382 // Functions of Simple Text Input Protocol
385 Reset the input device and optionaly run diagnostics
387 There are 2 types of reset for USB keyboard.
388 For non-exhaustive reset, only keyboard buffer is cleared.
389 For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status
390 is also re-initialized.
392 @param This Protocol instance pointer.
393 @param ExtendedVerification Driver may perform diagnostics on reset.
395 @retval EFI_SUCCESS The device was reset.
396 @retval EFI_DEVICE_ERROR The device is not functioning properly and could not be reset.
402 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
403 IN BOOLEAN ExtendedVerification
407 Reads the next keystroke from the input device.
409 @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance.
410 @param Key A pointer to a buffer that is filled in with the keystroke
411 information for the key that was pressed.
413 @retval EFI_SUCCESS The keystroke information was returned.
414 @retval EFI_NOT_READY There was no keystroke data availiable.
415 @retval EFI_DEVICE_ERROR The keydtroke information was not returned due to
421 USBKeyboardReadKeyStroke (
422 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
423 OUT EFI_INPUT_KEY
*Key
427 // Simple Text Input Ex protocol functions
430 Resets the input device hardware.
432 The Reset() function resets the input device hardware. As part
433 of initialization process, the firmware/device will make a quick
434 but reasonable attempt to verify that the device is functioning.
435 If the ExtendedVerification flag is TRUE the firmware may take
436 an extended amount of time to verify the device is operating on
437 reset. Otherwise the reset operation is to occur as quickly as
438 possible. The hardware verification process is not defined by
439 this specification and is left up to the platform firmware or
442 @param This A pointer to the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL instance.
444 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
445 verification operation of the device during reset.
447 @retval EFI_SUCCESS The device was reset.
448 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
454 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
455 IN BOOLEAN ExtendedVerification
459 Reads the next keystroke from the input device.
461 @param This Protocol instance pointer.
462 @param KeyData A pointer to a buffer that is filled in with the keystroke
463 state data for the key that was pressed.
465 @retval EFI_SUCCESS The keystroke information was returned.
466 @retval EFI_NOT_READY There was no keystroke data available.
467 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
469 @retval EFI_INVALID_PARAMETER KeyData is NULL.
474 USBKeyboardReadKeyStrokeEx (
475 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
476 OUT EFI_KEY_DATA
*KeyData
480 Set certain state for the input device.
482 @param This Protocol instance pointer.
483 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
484 state for the input device.
486 @retval EFI_SUCCESS The device state was set appropriately.
487 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
488 not have the setting adjusted.
489 @retval EFI_UNSUPPORTED The device does not support the ability to have its state set.
490 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
495 USBKeyboardSetState (
496 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
497 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
501 Register a notification function for a particular keystroke for the input device.
503 @param This Protocol instance pointer.
504 @param KeyData A pointer to a buffer that is filled in with the keystroke
505 information data for the key that was pressed.
506 @param KeyNotificationFunction Points to the function to be called when the key
507 sequence is typed specified by KeyData.
508 @param NotifyHandle Points to the unique handle assigned to the registered notification.
510 @retval EFI_SUCCESS The notification function was registered successfully.
511 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
512 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
517 USBKeyboardRegisterKeyNotify (
518 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
519 IN EFI_KEY_DATA
*KeyData
,
520 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
521 OUT EFI_HANDLE
*NotifyHandle
525 Remove a registered notification function from a particular keystroke.
527 @param This Protocol instance pointer.
528 @param NotificationHandle The handle of the notification function being unregistered.
530 @retval EFI_SUCCESS The notification function was unregistered successfully.
531 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid
532 @retval EFI_NOT_FOUND Cannot find the matching entry in database.
537 USBKeyboardUnregisterKeyNotify (
538 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
539 IN EFI_HANDLE NotificationHandle
543 Event notification function registered for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
544 and EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey.
546 @param Event Event to be signaled when a key is pressed.
547 @param Context Points to USB_KB_DEV instance.
552 USBKeyboardWaitForKey (
558 Free keyboard notify list.
560 @param NotifyList The keyboard notify list to free.
562 @retval EFI_SUCCESS Free the notify list successfully.
563 @retval EFI_INVALID_PARAMETER NotifyList is NULL.
568 IN OUT LIST_ENTRY
*NotifyList
572 Check whether the pressed key matches a registered key or not.
574 @param RegsiteredData A pointer to keystroke data for the key that was registered.
575 @param InputData A pointer to keystroke data for the key that was pressed.
577 @retval TRUE Key pressed matches a registered key.
578 @retval FLASE Key pressed does not matche a registered key.
583 IN EFI_KEY_DATA
*RegsiteredData
,
584 IN EFI_KEY_DATA
*InputData
588 Timer handler to convert the key from USB.
590 @param Event Indicates the event that invoke this function.
591 @param Context Indicates the calling context.
595 USBKeyboardTimerHandler (