]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbKbDxe/EfiKey.h
Clean up the private GUID definition in module Level.
[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 #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_HANDLE NotifyHandle;
76 EFI_KEY_DATA KeyData;
77 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn;
78 LIST_ENTRY NotifyEntry;
79 } KEYBOARD_CONSOLE_IN_EX_NOTIFY;
80
81 #define USB_NS_KEY_SIGNATURE SIGNATURE_32 ('u', 'n', 's', 'k')
82
83 typedef struct {
84 UINTN Signature;
85 LIST_ENTRY Link;
86
87 //
88 // The number of EFI_NS_KEY_MODIFIER children definitions
89 //
90 UINTN KeyCount;
91
92 //
93 // NsKey[0] : Non-spacing key
94 // NsKey[1] ~ NsKey[KeyCount] : Physical keys
95 //
96 EFI_KEY_DESCRIPTOR *NsKey;
97 } USB_NS_KEY;
98
99 #define USB_NS_KEY_FORM_FROM_LINK(a) CR (a, USB_NS_KEY, Link, USB_NS_KEY_SIGNATURE)
100
101 ///
102 /// Structure to describe USB keyboard device
103 ///
104 typedef struct {
105 UINTN Signature;
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;
112
113 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
114 EFI_USB_ENDPOINT_DESCRIPTOR IntEndpointDescriptor;
115
116 USB_SIMPLE_QUEUE UsbKeyQueue;
117 USB_SIMPLE_QUEUE EfiKeyQueue;
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 // Notification function list
147 //
148 LIST_ENTRY NotifyList;
149
150 //
151 // Non-spacing key list
152 //
153 LIST_ENTRY NsKeyList;
154 USB_NS_KEY *CurrentNsKey;
155 EFI_KEY_DESCRIPTOR *KeyConvertionTable;
156 EFI_EVENT KeyboardLayoutEvent;
157 } USB_KB_DEV;
158
159 //
160 // Global Variables
161 //
162 extern EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding;
163 extern EFI_COMPONENT_NAME_PROTOCOL gUsbKeyboardComponentName;
164 extern EFI_COMPONENT_NAME2_PROTOCOL gUsbKeyboardComponentName2;
165
166 #define USB_KB_DEV_FROM_THIS(a) \
167 CR(a, USB_KB_DEV, SimpleInput, USB_KB_DEV_SIGNATURE)
168 #define TEXT_INPUT_EX_USB_KB_DEV_FROM_THIS(a) \
169 CR(a, USB_KB_DEV, SimpleInputEx, USB_KB_DEV_SIGNATURE)
170
171 //
172 // According to Universal Serial Bus HID Usage Tables document ver 1.12,
173 // a Boot Keyboard should support the keycode range from 0x0 to 0x65 and 0xE0 to 0xE7.
174 // 0xE0 to 0xE7 are for modifier keys, and 0x0 to 0x3 are reserved for typical
175 // keyboard status or keyboard errors.
176 // So the number of valid non-modifier USB keycodes is 0x62, and the number of
177 // valid keycodes is 0x6A.
178 //
179 #define NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE 0x62
180 #define NUMBER_OF_VALID_USB_KEYCODE 0x6A
181 //
182 // 0x0 to 0x3 are reserved for typical keyboard status or keyboard errors.
183 //
184 #define USBKBD_VALID_KEYCODE(Key) ((UINT8) (Key) > 3)
185
186 typedef struct {
187 UINT8 NumLock : 1;
188 UINT8 CapsLock : 1;
189 UINT8 ScrollLock : 1;
190 UINT8 Resrvd : 5;
191 } LED_MAP;
192
193 //
194 // Functions of Driver Binding Protocol
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 Retrieves a Unicode string that is the user readable name of the driver.
269
270 This function retrieves the user readable name of a driver in the form of a
271 Unicode string. If the driver specified by This has a user readable name in
272 the language specified by Language, then a pointer to the driver name is
273 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
274 by This does not support the language specified by Language,
275 then EFI_UNSUPPORTED is returned.
276
277 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
278 EFI_COMPONENT_NAME_PROTOCOL instance.
279 @param Language A pointer to a Null-terminated ASCII string
280 array indicating the language. This is the
281 language of the driver name that the caller is
282 requesting, and it must match one of the
283 languages specified in SupportedLanguages. The
284 number of languages supported by a driver is up
285 to the driver writer. Language is specified
286 in RFC 4646 or ISO 639-2 language code format.
287 @param DriverName A pointer to the Unicode string to return.
288 This Unicode string is the name of the
289 driver specified by This in the language
290 specified by Language.
291
292 @retval EFI_SUCCESS The Unicode string for the Driver specified by
293 This and the language specified by Language was
294 returned in DriverName.
295 @retval EFI_INVALID_PARAMETER Language is NULL.
296 @retval EFI_INVALID_PARAMETER DriverName is NULL.
297 @retval EFI_UNSUPPORTED The driver specified by This does not support
298 the language specified by Language.
299
300 **/
301 EFI_STATUS
302 EFIAPI
303 UsbKeyboardComponentNameGetDriverName (
304 IN EFI_COMPONENT_NAME_PROTOCOL *This,
305 IN CHAR8 *Language,
306 OUT CHAR16 **DriverName
307 );
308
309 /**
310 Retrieves a Unicode string that is the user readable name of the controller
311 that is being managed by a driver.
312
313 This function retrieves the user readable name of the controller specified by
314 ControllerHandle and ChildHandle in the form of a Unicode string. If the
315 driver specified by This has a user readable name in the language specified by
316 Language, then a pointer to the controller name is returned in ControllerName,
317 and EFI_SUCCESS is returned. If the driver specified by This is not currently
318 managing the controller specified by ControllerHandle and ChildHandle,
319 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
320 support the language specified by Language, then EFI_UNSUPPORTED is returned.
321
322 @param This A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
323 EFI_COMPONENT_NAME_PROTOCOL instance.
324 @param ControllerHandle The handle of a controller that the driver
325 specified by This is managing. This handle
326 specifies the controller whose name is to be
327 returned.
328 @param ChildHandle The handle of the child controller to retrieve
329 the name of. This is an optional parameter that
330 may be NULL. It will be NULL for device
331 drivers. It will also be NULL for a bus drivers
332 that wish to retrieve the name of the bus
333 controller. It will not be NULL for a bus
334 driver that wishes to retrieve the name of a
335 child controller.
336 @param Language A pointer to a Null-terminated ASCII string
337 array indicating the language. This is the
338 language of the driver name that the caller is
339 requesting, and it must match one of the
340 languages specified in SupportedLanguages. The
341 number of languages supported by a driver is up
342 to the driver writer. Language is specified in
343 RFC 4646 or ISO 639-2 language code format.
344 @param ControllerName A pointer to the Unicode string to return.
345 This Unicode string is the name of the
346 controller specified by ControllerHandle and
347 ChildHandle in the language specified by
348 Language from the point of view of the driver
349 specified by This.
350
351 @retval EFI_SUCCESS The Unicode string for the user readable name in
352 the language specified by Language for the
353 driver specified by This was returned in
354 DriverName.
355 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
356 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
357 EFI_HANDLE.
358 @retval EFI_INVALID_PARAMETER Language is NULL.
359 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
360 @retval EFI_UNSUPPORTED The driver specified by This is not currently
361 managing the controller specified by
362 ControllerHandle and ChildHandle.
363 @retval EFI_UNSUPPORTED The driver specified by This does not support
364 the language specified by Language.
365
366 **/
367 EFI_STATUS
368 EFIAPI
369 UsbKeyboardComponentNameGetControllerName (
370 IN EFI_COMPONENT_NAME_PROTOCOL *This,
371 IN EFI_HANDLE ControllerHandle,
372 IN EFI_HANDLE ChildHandle OPTIONAL,
373 IN CHAR8 *Language,
374 OUT CHAR16 **ControllerName
375 );
376
377 //
378 // Functions of Simple Text Input Protocol
379 //
380 /**
381 Reset the input device and optionaly run diagnostics
382
383 There are 2 types of reset for USB keyboard.
384 For non-exhaustive reset, only keyboard buffer is cleared.
385 For exhaustive reset, in addition to clearance of keyboard buffer, the hardware status
386 is also re-initialized.
387
388 @param This Protocol instance pointer.
389 @param ExtendedVerification Driver may perform diagnostics on reset.
390
391 @retval EFI_SUCCESS The device was reset.
392 @retval EFI_DEVICE_ERROR The device is not functioning properly and could not be reset.
393
394 **/
395 EFI_STATUS
396 EFIAPI
397 USBKeyboardReset (
398 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
399 IN BOOLEAN ExtendedVerification
400 );
401
402 /**
403 Reads the next keystroke from the input device.
404
405 @param This The EFI_SIMPLE_TEXT_INPUT_PROTOCOL instance.
406 @param Key A pointer to a buffer that is filled in with the keystroke
407 information for the key that was pressed.
408
409 @retval EFI_SUCCESS The keystroke information was returned.
410 @retval EFI_NOT_READY There was no keystroke data availiable.
411 @retval EFI_DEVICE_ERROR The keydtroke information was not returned due to
412 hardware errors.
413
414 **/
415 EFI_STATUS
416 EFIAPI
417 USBKeyboardReadKeyStroke (
418 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,
419 OUT EFI_INPUT_KEY *Key
420 );
421
422 //
423 // Simple Text Input Ex protocol functions
424 //
425 /**
426 Resets the input device hardware.
427
428 The Reset() function resets the input device hardware. As part
429 of initialization process, the firmware/device will make a quick
430 but reasonable attempt to verify that the device is functioning.
431 If the ExtendedVerification flag is TRUE the firmware may take
432 an extended amount of time to verify the device is operating on
433 reset. Otherwise the reset operation is to occur as quickly as
434 possible. The hardware verification process is not defined by
435 this specification and is left up to the platform firmware or
436 driver to implement.
437
438 @param This A pointer to the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL instance.
439
440 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
441 verification operation of the device during reset.
442
443 @retval EFI_SUCCESS The device was reset.
444 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could not be reset.
445
446 **/
447 EFI_STATUS
448 EFIAPI
449 USBKeyboardResetEx (
450 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
451 IN BOOLEAN ExtendedVerification
452 );
453
454 /**
455 Reads the next keystroke from the input device.
456
457 @param This Protocol instance pointer.
458 @param KeyData A pointer to a buffer that is filled in with the keystroke
459 state data for the key that was pressed.
460
461 @retval EFI_SUCCESS The keystroke information was returned.
462 @retval EFI_NOT_READY There was no keystroke data available.
463 @retval EFI_DEVICE_ERROR The keystroke information was not returned due to
464 hardware errors.
465 @retval EFI_INVALID_PARAMETER KeyData is NULL.
466
467 **/
468 EFI_STATUS
469 EFIAPI
470 USBKeyboardReadKeyStrokeEx (
471 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
472 OUT EFI_KEY_DATA *KeyData
473 );
474
475 /**
476 Set certain state for the input device.
477
478 @param This Protocol instance pointer.
479 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
480 state for the input device.
481
482 @retval EFI_SUCCESS The device state was set appropriately.
483 @retval EFI_DEVICE_ERROR The device is not functioning correctly and could
484 not have the setting adjusted.
485 @retval EFI_UNSUPPORTED The device does not support the ability to have its state set.
486 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
487
488 **/
489 EFI_STATUS
490 EFIAPI
491 USBKeyboardSetState (
492 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
493 IN EFI_KEY_TOGGLE_STATE *KeyToggleState
494 );
495
496 /**
497 Register a notification function for a particular keystroke for the input device.
498
499 @param This Protocol instance pointer.
500 @param KeyData A pointer to a buffer that is filled in with the keystroke
501 information data for the key that was pressed.
502 @param KeyNotificationFunction Points to the function to be called when the key
503 sequence is typed specified by KeyData.
504 @param NotifyHandle Points to the unique handle assigned to the registered notification.
505
506 @retval EFI_SUCCESS The notification function was registered successfully.
507 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necesssary data structures.
508 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle or KeyNotificationFunction is NULL.
509
510 **/
511 EFI_STATUS
512 EFIAPI
513 USBKeyboardRegisterKeyNotify (
514 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
515 IN EFI_KEY_DATA *KeyData,
516 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction,
517 OUT EFI_HANDLE *NotifyHandle
518 );
519
520 /**
521 Remove a registered notification function from a particular keystroke.
522
523 @param This Protocol instance pointer.
524 @param NotificationHandle The handle of the notification function being unregistered.
525
526 @retval EFI_SUCCESS The notification function was unregistered successfully.
527 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid
528 @retval EFI_NOT_FOUND Cannot find the matching entry in database.
529
530 **/
531 EFI_STATUS
532 EFIAPI
533 USBKeyboardUnregisterKeyNotify (
534 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
535 IN EFI_HANDLE NotificationHandle
536 );
537
538 /**
539 Event notification function registered for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx
540 and EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey.
541
542 @param Event Event to be signaled when a key is pressed.
543 @param Context Points to USB_KB_DEV instance.
544
545 **/
546 VOID
547 EFIAPI
548 USBKeyboardWaitForKey (
549 IN EFI_EVENT Event,
550 IN VOID *Context
551 );
552
553 /**
554 Free keyboard notify list.
555
556 @param NotifyList The keyboard notify list to free.
557
558 @retval EFI_SUCCESS Free the notify list successfully.
559 @retval EFI_INVALID_PARAMETER NotifyList is NULL.
560
561 **/
562 EFI_STATUS
563 KbdFreeNotifyList (
564 IN OUT LIST_ENTRY *NotifyList
565 );
566
567 /**
568 Check whether the pressed key matches a registered key or not.
569
570 @param RegsiteredData A pointer to keystroke data for the key that was registered.
571 @param InputData A pointer to keystroke data for the key that was pressed.
572
573 @retval TRUE Key pressed matches a registered key.
574 @retval FLASE Key pressed does not matche a registered key.
575
576 **/
577 BOOLEAN
578 IsKeyRegistered (
579 IN EFI_KEY_DATA *RegsiteredData,
580 IN EFI_KEY_DATA *InputData
581 );
582
583 /**
584 Timer handler to convert the key from USB.
585
586 @param Event Indicates the event that invoke this function.
587 @param Context Indicates the calling context.
588 **/
589 VOID
590 EFIAPI
591 USBKeyboardTimerHandler (
592 IN EFI_EVENT Event,
593 IN VOID *Context
594 );
595
596 #endif
597