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