]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #ifndef _EFI_USB_KB_H_
10 #define _EFI_USB_KB_H_
11
12 #include <Uefi.h>
13
14 #include <Protocol/SimpleTextIn.h>
15 #include <Protocol/SimpleTextInEx.h>
16 #include <Protocol/HiiDatabase.h>
17 #include <Protocol/UsbIo.h>
18 #include <Protocol/DevicePath.h>
19
20 #include <Guid/HiiKeyBoardLayout.h>
21 #include <Guid/UsbKeyBoardLayout.h>
22
23 #include <Library/DebugLib.h>
24 #include <Library/ReportStatusCodeLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/UefiRuntimeServicesTableLib.h>
27 #include <Library/UefiDriverEntryPoint.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/UefiUsbLib.h>
33 #include <Library/HiiLib.h>
34
35 #include <IndustryStandard/Usb.h>
36
37 #define KEYBOARD_TIMER_INTERVAL 200000 // 0.02s
38
39 #define MAX_KEY_ALLOWED 32
40
41 #define HZ 1000 * 1000 * 10
42 #define USBKBD_REPEAT_DELAY ((HZ) / 2)
43 #define USBKBD_REPEAT_RATE ((HZ) / 50)
44
45 #define CLASS_HID 3
46 #define SUBCLASS_BOOT 1
47 #define PROTOCOL_KEYBOARD 1
48
49 #define BOOT_PROTOCOL 0
50 #define REPORT_PROTOCOL 1
51
52 typedef struct {
53 BOOLEAN Down;
54 UINT8 KeyCode;
55 } USB_KEY;
56
57 typedef struct {
58 VOID *Buffer[MAX_KEY_ALLOWED + 1];
59 UINTN Head;
60 UINTN Tail;
61 UINTN ItemSize;
62 } USB_SIMPLE_QUEUE;
63
64 #define USB_KB_DEV_SIGNATURE SIGNATURE_32 ('u', 'k', 'b', 'd')
65 #define USB_KB_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('u', 'k', 'b', 'x')
66
67 typedef struct _KEYBOARD_CONSOLE_IN_EX_NOTIFY {
68 UINTN Signature;
69 EFI_KEY_DATA KeyData;
70 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn;
71 LIST_ENTRY NotifyEntry;
72 } KEYBOARD_CONSOLE_IN_EX_NOTIFY;
73
74 #define USB_NS_KEY_SIGNATURE SIGNATURE_32 ('u', 'n', 's', 'k')
75
76 typedef struct {
77 UINTN Signature;
78 LIST_ENTRY Link;
79
80 //
81 // The number of EFI_NS_KEY_MODIFIER children definitions
82 //
83 UINTN KeyCount;
84
85 //
86 // NsKey[0] : Non-spacing key
87 // NsKey[1] ~ NsKey[KeyCount] : Physical keys
88 //
89 EFI_KEY_DESCRIPTOR *NsKey;
90 } USB_NS_KEY;
91
92 #define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE)
93
94 ///
95 /// Structure to describe USB keyboard device
96 ///
97 typedef struct {
98 UINTN Signature;
99 EFI_HANDLE ControllerHandle;
100 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
101 EFI_EVENT DelayedRecoveryEvent;
102 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput;
103 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx;
104 EFI_USB_IO_PROTOCOL *UsbIo;
105
106 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
107 EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor;
108
109 USB_SIMPLE_QUEUE UsbKeyQueue;
110 USB_SIMPLE_QUEUE EfiKeyQueue;
111 USB_SIMPLE_QUEUE EfiKeyQueueForNotify;
112 BOOLEAN CtrlOn;
113 BOOLEAN AltOn;
114 BOOLEAN ShiftOn;
115 BOOLEAN NumLockOn;
116 BOOLEAN CapsOn;
117 BOOLEAN ScrollOn;
118 UINT8 LastKeyCodeArray[8];
119 UINT8 CurKeyCode;
120
121 EFI_EVENT TimerEvent;
122
123 UINT8 RepeatKey;
124 EFI_EVENT RepeatTimer;
125
126 EFI_UNICODE_STRING_TABLE *ControllerNameTable;
127
128 BOOLEAN LeftCtrlOn;
129 BOOLEAN LeftAltOn;
130 BOOLEAN LeftShiftOn;
131 BOOLEAN LeftLogoOn;
132 BOOLEAN RightCtrlOn;
133 BOOLEAN RightAltOn;
134 BOOLEAN RightShiftOn;
135 BOOLEAN RightLogoOn;
136 BOOLEAN MenuKeyOn;
137 BOOLEAN SysReqOn;
138 BOOLEAN AltGrOn;
139
140 BOOLEAN IsSupportPartialKey;
141
142 EFI_KEY_STATE KeyState;
143 //
144 // Notification function list
145 //
146 LIST_ENTRY NotifyList;
147 EFI_EVENT KeyNotifyProcessEvent;
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 /**
197 Check whether USB keyboard driver supports this device.
198
199 @param This The USB keyboard driver binding protocol.
200 @param Controller The controller handle to check.
201 @param RemainingDevicePath The remaining device path.
202
203 @retval EFI_SUCCESS The driver supports this controller.
204 @retval other This device isn't supported.
205
206 **/
207 EFI_STATUS
208 EFIAPI
209 USBKeyboardDriverBindingSupported (
210 IN EFI_DRIVER_BINDING_PROTOCOL *This,
211 IN EFI_HANDLE Controller,
212 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
213 );
214
215 /**
216 Starts the keyboard device with this driver.
217
218 This function produces Simple Text Input Protocol and Simple Text Input Ex Protocol,
219 initializes the keyboard device, and submit Asynchronous Interrupt Transfer to manage
220 this keyboard device.
221
222 @param This The USB keyboard driver binding instance.
223 @param Controller Handle of device to bind driver to.
224 @param RemainingDevicePath Optional parameter use to pick a specific child
225 device to start.
226
227 @retval EFI_SUCCESS The controller is controlled by the usb keyboard driver.
228 @retval EFI_UNSUPPORTED No interrupt endpoint can be found.
229 @retval Other This controller cannot be started.
230
231 **/
232 EFI_STATUS
233 EFIAPI
234 USBKeyboardDriverBindingStart (
235 IN EFI_DRIVER_BINDING_PROTOCOL *This,
236 IN EFI_HANDLE Controller,
237 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
238 );
239
240 /**
241 Stop the USB keyboard device handled by this driver.
242
243 @param This The USB keyboard driver binding protocol.
244 @param Controller The controller to release.
245 @param NumberOfChildren The number of handles in ChildHandleBuffer.
246 @param ChildHandleBuffer The array of child handle.
247
248 @retval EFI_SUCCESS The device was stopped.
249 @retval EFI_UNSUPPORTED Simple Text In Protocol or Simple Text In Ex Protocol
250 is not installed on Controller.
251 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
252 @retval Others Fail to uninstall protocols attached on the device.
253
254 **/
255 EFI_STATUS
256 EFIAPI
257 USBKeyboardDriverBindingStop (
258 IN EFI_DRIVER_BINDING_PROTOCOL *This,
259 IN EFI_HANDLE Controller,
260 IN UINTN NumberOfChildren,
261 IN EFI_HANDLE *ChildHandleBuffer
262 );
263
264 //
265 // EFI Component Name Functions
266 //
267
268 /**
269 Retrieves a Unicode string that is the user readable name of the driver.
270
271 This function retrieves the user readable name of a driver in the form of a
272 Unicode string. If the driver specified by This has a user readable name in
273 the language specified by Language, then a pointer to the driver name is
274 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
275 by This does not support the language specified by Language,
276 then EFI_UNSUPPORTED is returned.
277
278 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
279 EFI_COMPONENT_NAME_PROTOCOL instance.
280 @param Language A pointer to a Null-terminated ASCII string
281 array indicating the language. This is the
282 language of the driver name that the caller is
283 requesting, and it must match one of the
284 languages specified in SupportedLanguages. The
285 number of languages supported by a driver is up
286 to the driver writer. Language is specified
287 in RFC 4646 or ISO 639-2 language code format.
288 @param DriverName A pointer to the Unicode string to return.
289 This Unicode string is the name of the
290 driver specified by This in the language
291 specified by Language.
292
293 @retval EFI_SUCCESS The Unicode string for the Driver specified by
294 This and the language specified by Language was
295 returned in DriverName.
296 @retval EFI_INVALID_PARAMETER Language is NULL.
297 @retval EFI_INVALID_PARAMETER DriverName is NULL.
298 @retval EFI_UNSUPPORTED The driver specified by This does not support
299 the language specified by Language.
300
301 **/
302 EFI_STATUS
303 EFIAPI
304 UsbKeyboardComponentNameGetDriverName (
305 IN EFI_COMPONENT_NAME_PROTOCOL *This,
306 IN CHAR8 *Language,
307 OUT CHAR16 **DriverName
308 );
309
310 /**
311 Retrieves a Unicode string that is the user readable name of the controller
312 that is being managed by a driver.
313
314 This function retrieves the user readable name of the controller specified by
315 ControllerHandle and ChildHandle in the form of a Unicode string. If the
316 driver specified by This has a user readable name in the language specified by
317 Language, then a pointer to the controller name is returned in ControllerName,
318 and EFI_SUCCESS is returned. If the driver specified by This is not currently
319 managing the controller specified by ControllerHandle and ChildHandle,
320 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
321 support the language specified by Language, then EFI_UNSUPPORTED is returned.
322
323 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
324 EFI_COMPONENT_NAME_PROTOCOL instance.
325 @param ControllerHandle The handle of a controller that the driver
326 specified by This is managing. This handle
327 specifies the controller whose name is to be
328 returned.
329 @param ChildHandle The handle of the child controller to retrieve
330 the name of. This is an optional parameter that
331 may be NULL. It will be NULL for device
332 drivers. It will also be NULL for a bus drivers
333 that wish to retrieve the name of the bus
334 controller. It will not be NULL for a bus
335 driver that wishes to retrieve the name of a
336 child controller.
337 @param Language A pointer to a Null-terminated ASCII string
338 array indicating the language. This is the
339 language of the driver name that the caller is
340 requesting, and it must match one of the
341 languages specified in SupportedLanguages. The
342 number of languages supported by a driver is up
343 to the driver writer. Language is specified in
344 RFC 4646 or ISO 639-2 language code format.
345 @param ControllerName A pointer to the Unicode string to return.
346 This Unicode string is the name of the
347 controller specified by ControllerHandle and
348 ChildHandle in the language specified by
349 Language from the point of view of the driver
350 specified by This.
351
352 @retval EFI_SUCCESS The Unicode string for the user readable name in
353 the language specified by Language for the
354 driver specified by This was returned in
355 DriverName.
356 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
357 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
358 EFI_HANDLE.
359 @retval EFI_INVALID_PARAMETER Language is NULL.
360 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
361 @retval EFI_UNSUPPORTED The driver specified by This is not currently
362 managing the controller specified by
363 ControllerHandle and ChildHandle.
364 @retval EFI_UNSUPPORTED The driver specified by This does not support
365 the language specified by Language.
366
367 **/
368 EFI_STATUS
369 EFIAPI
370 UsbKeyboardComponentNameGetControllerName (
371 IN EFI_COMPONENT_NAME_PROTOCOL *This,
372 IN EFI_HANDLE ControllerHandle,
373 IN EFI_HANDLE ChildHandle OPTIONAL,
374 IN CHAR8 *Language,
375 OUT CHAR16 **ControllerName
376 );
377
378 //
379 // Functions of Simple Text Input Protocol
380 //
381
382 /**
383 Reset the input device and optionally run diagnostics
384
385 There are 2 types of reset for USB keyboard.
386 For non-exhaustive reset, only keyboard buffer is cleared.
387 For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status
388 is also re-initialized.
389
390 @param This Protocol instance pointer.
391 @param ExtendedVerification Driver may perform diagnostics on reset.
392
393 @retval EFI_SUCCESS The device was reset.
394 @retval EFI_DEVICE_ERROR The device is not functioning properly and could not be reset.
395
396 **/
397 EFI_STATUS
398 EFIAPI
399 USBKeyboardReset (
400 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
401 IN BOOLEAN ExtendedVerification
402 );
403
404 /**
405 Reads the next keystroke from the input device.
406
407 @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance.
408 @param Key A pointer to a buffer that is filled in with the keystroke
409 information for the key that was pressed.
410
411 @retval EFI_SUCCESS The keystroke information was returned.
412 @retval EFI_NOT_READY There was no keystroke data available.
413 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
414 hardware errors.
415
416 **/
417 EFI_STATUS
418 EFIAPI
419 USBKeyboardReadKeyStroke (
420 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
421 OUT EFI_INPUT_KEY *Key
422 );
423
424 //
425 // Simple Text Input Ex protocol functions
426 //
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
504 the keystroke information for the key that was
505 pressed. If KeyData.Key, KeyData.KeyState.KeyToggleState
506 and KeyData.KeyState.KeyShiftState are 0, then any incomplete
507 keystroke will trigger a notification of the KeyNotificationFunction.
508 @param KeyNotificationFunction Points to the function to be called when the key
509 sequence is typed specified by KeyData. This notification function
510 should be called at <=TPL_CALLBACK.
511 @param NotifyHandle Points to the unique handle assigned to the registered notification.
512
513 @retval EFI_SUCCESS The notification function was registered successfully.
514 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necessary data structures.
515 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
516
517 **/
518 EFI_STATUS
519 EFIAPI
520 USBKeyboardRegisterKeyNotify (
521 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
522 IN EFI_KEY_DATA *KeyData,
523 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
524 OUT VOID **NotifyHandle
525 );
526
527 /**
528 Remove a registered notification function from a particular keystroke.
529
530 @param This Protocol instance pointer.
531 @param NotificationHandle The handle of the notification function being unregistered.
532
533 @retval EFI_SUCCESS The notification function was unregistered successfully.
534 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid
535 @retval EFI_NOT_FOUND Cannot find the matching entry in database.
536
537 **/
538 EFI_STATUS
539 EFIAPI
540 USBKeyboardUnregisterKeyNotify (
541 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
542 IN VOID *NotificationHandle
543 );
544
545 /**
546 Event notification function registered for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
547 and EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey.
548
549 @param Event Event to be signaled when a key is pressed.
550 @param Context Points to USB_KB_DEV instance.
551
552 **/
553 VOID
554 EFIAPI
555 USBKeyboardWaitForKey (
556 IN EFI_EVENT Event,
557 IN VOID *Context
558 );
559
560 /**
561 Free keyboard notify list.
562
563 @param NotifyList The keyboard notify list to free.
564
565 @retval EFI_SUCCESS Free the notify list successfully.
566 @retval EFI_INVALID_PARAMETER NotifyList is NULL.
567
568 **/
569 EFI_STATUS
570 KbdFreeNotifyList (
571 IN OUT LIST_ENTRY *NotifyList
572 );
573
574 /**
575 Check whether the pressed key matches a registered key or not.
576
577 @param RegsiteredData A pointer to keystroke data for the key that was registered.
578 @param InputData A pointer to keystroke data for the key that was pressed.
579
580 @retval TRUE Key pressed matches a registered key.
581 @retval FALSE Key pressed does not match a registered key.
582
583 **/
584 BOOLEAN
585 IsKeyRegistered (
586 IN EFI_KEY_DATA *RegsiteredData,
587 IN EFI_KEY_DATA *InputData
588 );
589
590 /**
591 Timer handler to convert the key from USB.
592
593 @param Event Indicates the event that invoke this function.
594 @param Context Indicates the calling context.
595 **/
596 VOID
597 EFIAPI
598 USBKeyboardTimerHandler (
599 IN EFI_EVENT Event,
600 IN VOID *Context
601 );
602
603 /**
604 Process key notify.
605
606 @param Event Indicates the event that invoke this function.
607 @param Context Indicates the calling context.
608 **/
609 VOID
610 EFIAPI
611 KeyNotifyProcessHandler (
612 IN EFI_EVENT Event,
613 IN VOID *Context
614 );
615
616 #endif