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