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