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