3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Helper functions for USB Keyboard Driver
27 // USB Key Code to Efi key mapping table
28 // Format:<efi scan code>, <unicode without shift>, <unicode with shift>
31 UINT8 KeyConvertionTable
[USB_KEYCODE_MAX_MAKE
][3] = {
32 { SCAN_NULL
, 'a', 'A' }, // 0x04
33 { SCAN_NULL
, 'b', 'B' }, // 0x05
34 { SCAN_NULL
, 'c', 'C' }, // 0x06
35 { SCAN_NULL
, 'd', 'D' }, // 0x07
36 { SCAN_NULL
, 'e', 'E' }, // 0x08
37 { SCAN_NULL
, 'f', 'F' }, // 0x09
38 { SCAN_NULL
, 'g', 'G' }, // 0x0A
39 { SCAN_NULL
, 'h', 'H' }, // 0x0B
40 { SCAN_NULL
, 'i', 'I' }, // 0x0C
41 { SCAN_NULL
, 'j', 'J' }, // 0x0D
42 { SCAN_NULL
, 'k', 'K' }, // 0x0E
43 { SCAN_NULL
, 'l', 'L' }, // 0x0F
44 { SCAN_NULL
, 'm', 'M' }, // 0x10
45 { SCAN_NULL
, 'n', 'N' }, // 0x11
46 { SCAN_NULL
, 'o', 'O' }, // 0x12
47 { SCAN_NULL
, 'p', 'P' }, // 0x13
48 { SCAN_NULL
, 'q', 'Q' }, // 0x14
49 { SCAN_NULL
, 'r', 'R' }, // 0x15
50 { SCAN_NULL
, 's', 'S' }, // 0x16
51 { SCAN_NULL
, 't', 'T' }, // 0x17
52 { SCAN_NULL
, 'u', 'U' }, // 0x18
53 { SCAN_NULL
, 'v', 'V' }, // 0x19
54 { SCAN_NULL
, 'w', 'W' }, // 0x1A
55 { SCAN_NULL
, 'x', 'X' }, // 0x1B
56 { SCAN_NULL
, 'y', 'Y' }, // 0x1C
57 { SCAN_NULL
, 'z', 'Z' }, // 0x1D
58 { SCAN_NULL
, '1', '!' }, // 0x1E
59 { SCAN_NULL
, '2', '@' }, // 0x1F
60 { SCAN_NULL
, '3', '#' }, // 0x20
61 { SCAN_NULL
, '4', '$' }, // 0x21
62 { SCAN_NULL
, '5', '%' }, // 0x22
63 { SCAN_NULL
, '6', '^' }, // 0x23
64 { SCAN_NULL
, '7', '&' }, // 0x24
65 { SCAN_NULL
, '8', '*' }, // 0x25
66 { SCAN_NULL
, '9', '(' }, // 0x26
67 { SCAN_NULL
, '0', ')' }, // 0x27
68 { SCAN_NULL
, 0x0d, 0x0d }, // 0x28 Enter
69 { SCAN_ESC
, 0x00, 0x00 }, // 0x29 Esc
70 { SCAN_NULL
, 0x08, 0x08 }, // 0x2A Backspace
71 { SCAN_NULL
, 0x09, 0x09 }, // 0x2B Tab
72 { SCAN_NULL
, ' ', ' ' }, // 0x2C Spacebar
73 { SCAN_NULL
, '-', '_' }, // 0x2D
74 { SCAN_NULL
, '=', '+' }, // 0x2E
75 { SCAN_NULL
, '[', '{' }, // 0x2F
76 { SCAN_NULL
, ']', '}' }, // 0x30
77 { SCAN_NULL
, '\\', '|' }, // 0x31
78 { SCAN_NULL
, '\\', '|' }, // 0x32 Keyboard US \ and |
79 { SCAN_NULL
, ';', ':' }, // 0x33
80 { SCAN_NULL
, '\'', '"' }, // 0x34
81 { SCAN_NULL
, '`', '~' }, // 0x35 Keyboard Grave Accent and Tlide
82 { SCAN_NULL
, ',', '<' }, // 0x36
83 { SCAN_NULL
, '.', '>' }, // 0x37
84 { SCAN_NULL
, '/', '?' }, // 0x38
85 { SCAN_NULL
, 0x00, 0x00 }, // 0x39 CapsLock
86 { SCAN_F1
, 0x00, 0x00 }, // 0x3A
87 { SCAN_F2
, 0x00, 0x00 }, // 0x3B
88 { SCAN_F3
, 0x00, 0x00 }, // 0x3C
89 { SCAN_F4
, 0x00, 0x00 }, // 0x3D
90 { SCAN_F5
, 0x00, 0x00 }, // 0x3E
91 { SCAN_F6
, 0x00, 0x00 }, // 0x3F
92 { SCAN_F7
, 0x00, 0x00 }, // 0x40
93 { SCAN_F8
, 0x00, 0x00 }, // 0x41
94 { SCAN_F9
, 0x00, 0x00 }, // 0x42
95 { SCAN_F10
, 0x00, 0x00 }, // 0x43
96 { SCAN_F11
, 0x00, 0x00 }, // 0x44 F11
97 { SCAN_F12
, 0x00, 0x00 }, // 0x45 F12
98 { SCAN_NULL
, 0x00, 0x00 }, // 0x46 PrintScreen
99 { SCAN_NULL
, 0x00, 0x00 }, // 0x47 Scroll Lock
100 { SCAN_NULL
, 0x00, 0x00 }, // 0x48 Pause
101 { SCAN_INSERT
, 0x00, 0x00 }, // 0x49
102 { SCAN_HOME
, 0x00, 0x00 }, // 0x4A
103 { SCAN_PAGE_UP
, 0x00, 0x00 }, // 0x4B
104 { SCAN_DELETE
, 0x00, 0x00 }, // 0x4C
105 { SCAN_END
, 0x00, 0x00 }, // 0x4D
106 { SCAN_PAGE_DOWN
, 0x00, 0x00 }, // 0x4E
107 { SCAN_RIGHT
, 0x00, 0x00 }, // 0x4F
108 { SCAN_LEFT
, 0x00, 0x00 }, // 0x50
109 { SCAN_DOWN
, 0x00, 0x00 }, // 0x51
110 { SCAN_UP
, 0x00, 0x00 }, // 0x52
111 { SCAN_NULL
, 0x00, 0x00 }, // 0x53 NumLock
112 { SCAN_NULL
, '/', '/' }, // 0x54
113 { SCAN_NULL
, '*', '*' }, // 0x55
114 { SCAN_NULL
, '-', '-' }, // 0x56
115 { SCAN_NULL
, '+', '+' }, // 0x57
116 { SCAN_NULL
, 0x0d, 0x0d }, // 0x58
117 { SCAN_END
, '1', '1' }, // 0x59
118 { SCAN_DOWN
, '2', '2' }, // 0x5A
119 { SCAN_PAGE_DOWN
, '3', '3' }, // 0x5B
120 { SCAN_LEFT
, '4', '4' }, // 0x5C
121 { SCAN_NULL
, '5', '5' }, // 0x5D
122 { SCAN_RIGHT
, '6', '6' }, // 0x5E
123 { SCAN_HOME
, '7', '7' }, // 0x5F
124 { SCAN_UP
, '8', '8' }, // 0x60
125 { SCAN_PAGE_UP
, '9', '9' }, // 0x61
126 { SCAN_INSERT
, '0', '0' }, // 0x62
127 { SCAN_DELETE
, '.', '.' }, // 0x63
128 { SCAN_NULL
, '\\', '|' }, // 0x64 Keyboard Non-US \ and |
129 { SCAN_NULL
, 0x00, 0x00 }, // 0x65 Keyboard Application
130 { SCAN_NULL
, 0x00, 0x00 }, // 0x66 Keyboard Power
131 { SCAN_NULL
, '=' , '=' } // 0x67 Keypad =
134 STATIC KB_MODIFIER KB_Mod
[8] = {
135 { MOD_CONTROL_L
, 0xe0 }, // 11100000
136 { MOD_CONTROL_R
, 0xe4 }, // 11100100
137 { MOD_SHIFT_L
, 0xe1 }, // 11100001
138 { MOD_SHIFT_R
, 0xe5 }, // 11100101
139 { MOD_ALT_L
, 0xe2 }, // 11100010
140 { MOD_ALT_R
, 0xe6 }, // 11100110
141 { MOD_WIN_L
, 0xe3 }, // 11100011
142 { MOD_WIN_R
, 0xe7 } // 11100111
148 IN EFI_USB_IO_PROTOCOL
*UsbIo
153 Uses USB I/O to check whether the device is a USB Keyboard device.
156 UsbIo: Points to a USB I/O protocol instance.
163 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor
;
166 // Get the Default interface descriptor, currently we
167 // assume it is interface 1
169 Status
= UsbIo
->UsbGetInterfaceDescriptor (
174 if (EFI_ERROR (Status
)) {
178 if (InterfaceDescriptor
.InterfaceClass
== CLASS_HID
&&
179 InterfaceDescriptor
.InterfaceSubClass
== SUBCLASS_BOOT
&&
180 InterfaceDescriptor
.InterfaceProtocol
== PROTOCOL_KEYBOARD
192 IN USB_KB_DEV
*UsbKeyboardDevice
197 Initialize USB Keyboard device and all private data structures.
200 UsbKeyboardDevice The USB_KB_DEV instance.
203 EFI_SUCCESS - Success
204 EFI_DEVICE_ERROR - Hardware Error
212 UINT32 TransferResult
;
214 KbdReportStatusCode (
215 UsbKeyboardDevice
->DevicePath
,
217 (EFI_PERIPHERAL_KEYBOARD
| EFI_P_KEYBOARD_PC_SELF_TEST
)
220 InitUSBKeyBuffer (&(UsbKeyboardDevice
->KeyboardBuffer
));
223 // default configurations
228 // Uses default configuration to configure the USB Keyboard device.
230 Status
= UsbSetDeviceConfiguration (
231 UsbKeyboardDevice
->UsbIo
,
232 (UINT16
) ConfigValue
,
235 if (EFI_ERROR (Status
)) {
237 // If configuration could not be set here, it means
238 // the keyboard interface has some errors and could
239 // not be initialized
241 KbdReportStatusCode (
242 UsbKeyboardDevice
->DevicePath
,
243 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
244 (EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_INTERFACE_ERROR
)
247 return EFI_DEVICE_ERROR
;
250 UsbGetProtocolRequest (
251 UsbKeyboardDevice
->UsbIo
,
252 UsbKeyboardDevice
->InterfaceDescriptor
.InterfaceNumber
,
256 // Sets boot protocol for the USB Keyboard.
257 // This driver only supports boot protocol.
258 // !!BugBug: How about the device that does not support boot protocol?
260 if (Protocol
!= BOOT_PROTOCOL
) {
261 UsbSetProtocolRequest (
262 UsbKeyboardDevice
->UsbIo
,
263 UsbKeyboardDevice
->InterfaceDescriptor
.InterfaceNumber
,
268 // the duration is indefinite, so the endpoint will inhibit reporting forever,
269 // and only reporting when a change is detected in the report data.
273 // idle value for all report ID
277 // idle forever until there is a key pressed and released.
281 UsbKeyboardDevice
->UsbIo
,
282 UsbKeyboardDevice
->InterfaceDescriptor
.InterfaceNumber
,
287 UsbKeyboardDevice
->CtrlOn
= 0;
288 UsbKeyboardDevice
->AltOn
= 0;
289 UsbKeyboardDevice
->ShiftOn
= 0;
290 UsbKeyboardDevice
->NumLockOn
= 0;
291 UsbKeyboardDevice
->CapsOn
= 0;
292 UsbKeyboardDevice
->ScrollOn
= 0;
293 ZeroMem (UsbKeyboardDevice
->LastKeyCodeArray
, sizeof (UINT8
) * 8);
296 // Set a timer for repeat keys' generation.
298 if (UsbKeyboardDevice
->RepeatTimer
) {
299 gBS
->CloseEvent (UsbKeyboardDevice
->RepeatTimer
);
300 UsbKeyboardDevice
->RepeatTimer
= 0;
303 Status
= gBS
->CreateEvent (
304 EFI_EVENT_TIMER
| EFI_EVENT_NOTIFY_SIGNAL
,
306 USBKeyboardRepeatHandler
,
308 &UsbKeyboardDevice
->RepeatTimer
311 if (UsbKeyboardDevice
->DelayedRecoveryEvent
) {
312 gBS
->CloseEvent (UsbKeyboardDevice
->DelayedRecoveryEvent
);
313 UsbKeyboardDevice
->DelayedRecoveryEvent
= 0;
316 Status
= gBS
->CreateEvent (
317 EFI_EVENT_TIMER
| EFI_EVENT_NOTIFY_SIGNAL
,
319 USBKeyboardRecoveryHandler
,
321 &UsbKeyboardDevice
->DelayedRecoveryEvent
338 Handler function for USB Keyboard's asynchronous interrupt transfer.
341 Data A pointer to a buffer that is filled with key data which is
342 retrieved via asynchronous interrupt transfer.
343 DataLength Indicates the size of the data buffer.
344 Context Pointing to USB_KB_DEV instance.
345 Result Indicates the result of the asynchronous interrupt transfer.
348 EFI_SUCCESS - Success
349 EFI_DEVICE_ERROR - Hardware Error
352 USB_KB_DEV
*UsbKeyboardDevice
;
353 EFI_USB_IO_PROTOCOL
*UsbIo
;
354 UINT8
*CurKeyCodeBuffer
;
355 UINT8
*OldKeyCodeBuffer
;
356 UINT8 CurModifierMap
;
357 UINT8 OldModifierMap
;
371 UsbKeyboardDevice
= (USB_KB_DEV
*) Context
;
372 UsbIo
= UsbKeyboardDevice
->UsbIo
;
375 // Analyzes the Result and performs corresponding action.
377 if (Result
!= EFI_USB_NOERROR
) {
379 // Some errors happen during the process
381 KbdReportStatusCode (
382 UsbKeyboardDevice
->DevicePath
,
383 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
384 (EFI_PERIPHERAL_KEYBOARD
| EFI_P_EC_INPUT_ERROR
)
388 // stop the repeat key generation if any
390 UsbKeyboardDevice
->RepeatKey
= 0;
393 UsbKeyboardDevice
->RepeatTimer
,
398 if ((Result
& EFI_USB_ERR_STALL
) == EFI_USB_ERR_STALL
) {
399 UsbClearEndpointHalt (
401 UsbKeyboardDevice
->IntEndpointDescriptor
.EndpointAddress
,
407 // Delete & Submit this interrupt again
410 UsbIo
->UsbAsyncInterruptTransfer (
412 UsbKeyboardDevice
->IntEndpointDescriptor
.EndpointAddress
,
421 UsbKeyboardDevice
->DelayedRecoveryEvent
,
423 EFI_USB_INTERRUPT_DELAY
426 return EFI_DEVICE_ERROR
;
429 if (DataLength
== 0 || Data
== NULL
) {
433 CurKeyCodeBuffer
= (UINT8
*) Data
;
434 OldKeyCodeBuffer
= UsbKeyboardDevice
->LastKeyCodeArray
;
437 // checks for new key stroke.
438 // if no new key got, return immediately.
440 for (Index
= 0; Index
< 8; Index
++) {
441 if (OldKeyCodeBuffer
[Index
] != CurKeyCodeBuffer
[Index
]) {
451 // Parse the modifier key
453 CurModifierMap
= CurKeyCodeBuffer
[0];
454 OldModifierMap
= OldKeyCodeBuffer
[0];
457 // handle modifier key's pressing or releasing situation.
459 for (Index
= 0; Index
< 8; Index
++) {
461 if ((CurModifierMap
& KB_Mod
[Index
].Mask
) != (OldModifierMap
& KB_Mod
[Index
].Mask
)) {
463 // if current modifier key is up, then
464 // CurModifierMap & KB_Mod[Index].Mask = 0;
465 // otherwize it is a non-zero value.
466 // Inserts the pressed modifier key into key buffer.
468 Down
= (UINT8
) (CurModifierMap
& KB_Mod
[Index
].Mask
);
469 InsertKeyCode (&(UsbKeyboardDevice
->KeyboardBuffer
), KB_Mod
[Index
].Key
, Down
);
474 // handle normal key's releasing situation
477 for (Index
= 2; Index
< 8; Index
++) {
479 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer
[Index
])) {
484 for (Index2
= 2; Index2
< 8; Index2
++) {
486 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer
[Index2
])) {
490 if (OldKeyCodeBuffer
[Index
] == CurKeyCodeBuffer
[Index2
]) {
498 &(UsbKeyboardDevice
->KeyboardBuffer
),
499 OldKeyCodeBuffer
[Index
],
503 // the original reapeat key is released.
505 if (OldKeyCodeBuffer
[Index
] == UsbKeyboardDevice
->RepeatKey
) {
506 UsbKeyboardDevice
->RepeatKey
= 0;
512 // original repeat key is released, cancel the repeat timer
514 if (UsbKeyboardDevice
->RepeatKey
== 0) {
516 UsbKeyboardDevice
->RepeatTimer
,
523 // handle normal key's pressing situation
526 for (Index
= 2; Index
< 8; Index
++) {
528 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer
[Index
])) {
533 for (Index2
= 2; Index2
< 8; Index2
++) {
535 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer
[Index2
])) {
539 if (CurKeyCodeBuffer
[Index
] == OldKeyCodeBuffer
[Index2
]) {
546 InsertKeyCode (&(UsbKeyboardDevice
->KeyboardBuffer
), CurKeyCodeBuffer
[Index
], 1);
548 // NumLock pressed or CapsLock pressed
550 if (CurKeyCodeBuffer
[Index
] == 0x53 || CurKeyCodeBuffer
[Index
] == 0x39) {
551 UsbKeyboardDevice
->RepeatKey
= 0;
553 NewRepeatKey
= CurKeyCodeBuffer
[Index
];
555 // do not repeat the original repeated key
557 UsbKeyboardDevice
->RepeatKey
= 0;
563 // Update LastKeycodeArray[] buffer in the
564 // Usb Keyboard Device data structure.
566 for (Index
= 0; Index
< 8; Index
++) {
567 UsbKeyboardDevice
->LastKeyCodeArray
[Index
] = CurKeyCodeBuffer
[Index
];
571 // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence
572 // and judge whether it will invoke reset event.
574 SavedTail
= UsbKeyboardDevice
->KeyboardBuffer
.bTail
;
575 Index
= UsbKeyboardDevice
->KeyboardBuffer
.bHead
;
576 while (Index
!= SavedTail
) {
577 RemoveKeyCode (&(UsbKeyboardDevice
->KeyboardBuffer
), &UsbKey
);
579 switch (UsbKey
.KeyCode
) {
584 UsbKeyboardDevice
->CtrlOn
= 1;
586 UsbKeyboardDevice
->CtrlOn
= 0;
593 UsbKeyboardDevice
->AltOn
= 1;
595 UsbKeyboardDevice
->AltOn
= 0;
605 if (UsbKeyboardDevice
->CtrlOn
&& UsbKeyboardDevice
->AltOn
) {
606 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
616 // insert the key back to the buffer.
617 // so the key sequence will not be destroyed.
620 &(UsbKeyboardDevice
->KeyboardBuffer
),
624 Index
= UsbKeyboardDevice
->KeyboardBuffer
.bHead
;
628 // If have new key pressed, update the RepeatKey value, and set the
629 // timer to repeate delay timer
631 if (NewRepeatKey
!= 0) {
633 // sets trigger time to "Repeat Delay Time",
634 // to trigger the repeat timer when the key is hold long
638 UsbKeyboardDevice
->RepeatTimer
,
642 UsbKeyboardDevice
->RepeatKey
= NewRepeatKey
;
650 IN OUT USB_KB_DEV
*UsbKeyboardDevice
,
656 Retrieves a key character after parsing the raw data in keyboard buffer.
659 UsbKeyboardDevice The USB_KB_DEV instance.
660 KeyChar Points to the Key character after key parsing.
663 EFI_SUCCESS - Success
664 EFI_NOT_READY - Device is not ready
671 while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice
->KeyboardBuffer
)) {
673 // pops one raw data off.
675 RemoveKeyCode (&(UsbKeyboardDevice
->KeyboardBuffer
), &UsbKey
);
678 switch (UsbKey
.KeyCode
) {
682 UsbKeyboardDevice
->CtrlOn
= 0;
687 UsbKeyboardDevice
->ShiftOn
= 0;
692 UsbKeyboardDevice
->AltOn
= 0;
703 // Analyzes key pressing situation
705 switch (UsbKey
.KeyCode
) {
709 UsbKeyboardDevice
->CtrlOn
= 1;
715 UsbKeyboardDevice
->ShiftOn
= 1;
721 UsbKeyboardDevice
->AltOn
= 1;
731 UsbKeyboardDevice
->NumLockOn
^= 1;
732 SetKeyLED (UsbKeyboardDevice
);
737 UsbKeyboardDevice
->CapsOn
^= 1;
738 SetKeyLED (UsbKeyboardDevice
);
743 // F11,F12,PrintScreen,ScrollLock,Pause,Application,Power
744 // keys are not valid EFI key
760 // Turn on the ScrollLock light on KB
762 UsbKeyboardDevice
->ScrollOn
^= 1;
763 SetKeyLED (UsbKeyboardDevice
);
780 // When encountered Del Key...
782 if (UsbKey
.KeyCode
== 0x4c || UsbKey
.KeyCode
== 0x63) {
783 if (UsbKeyboardDevice
->CtrlOn
&& UsbKeyboardDevice
->AltOn
) {
784 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
788 *KeyChar
= UsbKey
.KeyCode
;
792 return EFI_NOT_READY
;
798 USBKeyCodeToEFIScanCode (
799 IN USB_KB_DEV
*UsbKeyboardDevice
,
801 OUT EFI_INPUT_KEY
*Key
806 Converts USB Keyboard code to EFI Scan Code.
809 UsbKeyboardDevice The USB_KB_DEV instance.
810 KeyChar Indicates the key code that will be interpreted.
811 Key A pointer to a buffer that is filled in with
812 the keystroke information for the key that
815 EFI_NOT_READY - Device is not ready
816 EFI_SUCCESS - Success
821 if (!USBKBD_VALID_KEYCODE (KeyChar
)) {
822 return EFI_NOT_READY
;
826 // valid USB Key Code starts from 4
828 Index
= (UINT8
) (KeyChar
- 4);
830 if (Index
>= USB_KEYCODE_MAX_MAKE
) {
831 return EFI_NOT_READY
;
834 Key
->ScanCode
= KeyConvertionTable
[Index
][0];
836 if (UsbKeyboardDevice
->ShiftOn
) {
838 Key
->UnicodeChar
= KeyConvertionTable
[Index
][2];
842 Key
->UnicodeChar
= KeyConvertionTable
[Index
][1];
845 if (UsbKeyboardDevice
->CapsOn
) {
847 if (Key
->UnicodeChar
>= 'a' && Key
->UnicodeChar
<= 'z') {
849 Key
->UnicodeChar
= KeyConvertionTable
[Index
][2];
851 } else if (Key
->UnicodeChar
>= 'A' && Key
->UnicodeChar
<= 'Z') {
853 Key
->UnicodeChar
= KeyConvertionTable
[Index
][1];
858 if (KeyChar
>= 0x59 && KeyChar
<= 0x63) {
860 if (UsbKeyboardDevice
->NumLockOn
&& !UsbKeyboardDevice
->ShiftOn
) {
862 Key
->ScanCode
= SCAN_NULL
;
866 Key
->UnicodeChar
= 0x00;
870 if (Key
->UnicodeChar
== 0 && Key
->ScanCode
== SCAN_NULL
) {
871 return EFI_NOT_READY
;
881 IN OUT USB_KB_BUFFER
*KeyboardBuffer
886 Resets USB Keyboard Buffer.
889 KeyboardBuffer - Points to the USB Keyboard Buffer.
892 EFI_SUCCESS - Success
895 ZeroMem (KeyboardBuffer
, sizeof (USB_KB_BUFFER
));
897 KeyboardBuffer
->bHead
= KeyboardBuffer
->bTail
;
903 IsUSBKeyboardBufferEmpty (
904 IN USB_KB_BUFFER
*KeyboardBuffer
909 Check whether USB Keyboard buffer is empty.
912 KeyboardBuffer - USB Keyboard Buffer.
919 // meet FIFO empty condition
921 return (BOOLEAN
) (KeyboardBuffer
->bHead
== KeyboardBuffer
->bTail
);
926 IsUSBKeyboardBufferFull (
927 IN USB_KB_BUFFER
*KeyboardBuffer
932 Check whether USB Keyboard buffer is full.
935 KeyboardBuffer - USB Keyboard Buffer.
941 return (BOOLEAN
)(((KeyboardBuffer
->bTail
+ 1) % (MAX_KEY_ALLOWED
+ 1)) ==
942 KeyboardBuffer
->bHead
);
948 IN OUT USB_KB_BUFFER
*KeyboardBuffer
,
955 Inserts a key code into keyboard buffer.
958 KeyboardBuffer - Points to the USB Keyboard Buffer.
962 EFI_SUCCESS - Success
968 // if keyboard buffer is full, throw the
969 // first key out of the keyboard buffer.
971 if (IsUSBKeyboardBufferFull (KeyboardBuffer
)) {
972 RemoveKeyCode (KeyboardBuffer
, &UsbKey
);
975 KeyboardBuffer
->buffer
[KeyboardBuffer
->bTail
].KeyCode
= Key
;
976 KeyboardBuffer
->buffer
[KeyboardBuffer
->bTail
].Down
= Down
;
979 // adjust the tail pointer of the FIFO keyboard buffer.
981 KeyboardBuffer
->bTail
= (UINT8
) ((KeyboardBuffer
->bTail
+ 1) % (MAX_KEY_ALLOWED
+ 1));
988 IN OUT USB_KB_BUFFER
*KeyboardBuffer
,
994 Pops a key code off from keyboard buffer.
997 KeyboardBuffer - Points to the USB Keyboard Buffer.
998 UsbKey - Points to the buffer that contains a usb key code.
1001 EFI_SUCCESS - Success
1002 EFI_DEVICE_ERROR - Hardware Error
1005 if (IsUSBKeyboardBufferEmpty (KeyboardBuffer
)) {
1006 return EFI_DEVICE_ERROR
;
1009 UsbKey
->KeyCode
= KeyboardBuffer
->buffer
[KeyboardBuffer
->bHead
].KeyCode
;
1010 UsbKey
->Down
= KeyboardBuffer
->buffer
[KeyboardBuffer
->bHead
].Down
;
1013 // adjust the head pointer of the FIFO keyboard buffer.
1015 KeyboardBuffer
->bHead
= (UINT8
) ((KeyboardBuffer
->bHead
+ 1) % (MAX_KEY_ALLOWED
+ 1));
1022 IN USB_KB_DEV
*UsbKeyboardDevice
1026 Routine Description:
1027 Sets USB Keyboard LED state.
1030 UsbKeyboardDevice - The USB_KB_DEV instance.
1033 EFI_SUCCESS - Success
1040 // Set each field in Led map.
1042 Led
.NumLock
= (UINT8
) UsbKeyboardDevice
->NumLockOn
;
1043 Led
.CapsLock
= (UINT8
) UsbKeyboardDevice
->CapsOn
;
1044 Led
.ScrollLock
= (UINT8
) UsbKeyboardDevice
->ScrollOn
;
1049 // call Set Report Request to lighten the LED.
1051 UsbSetReportRequest (
1052 UsbKeyboardDevice
->UsbIo
,
1053 UsbKeyboardDevice
->InterfaceDescriptor
.InterfaceNumber
,
1065 USBKeyboardRepeatHandler (
1071 Routine Description:
1072 Timer handler for Repeat Key timer.
1075 Event - The Repeat Key event.
1076 Context - Points to the USB_KB_DEV instance.
1082 USB_KB_DEV
*UsbKeyboardDevice
;
1084 UsbKeyboardDevice
= (USB_KB_DEV
*) Context
;
1087 // Do nothing when there is no repeat key.
1089 if (UsbKeyboardDevice
->RepeatKey
!= 0) {
1091 // Inserts one Repeat key into keyboard buffer,
1094 &(UsbKeyboardDevice
->KeyboardBuffer
),
1095 UsbKeyboardDevice
->RepeatKey
,
1100 // set repeate rate for repeat key generation.
1103 UsbKeyboardDevice
->RepeatTimer
,
1113 USBKeyboardRecoveryHandler (
1119 Routine Description:
1120 Timer handler for Delayed Recovery timer.
1123 Event - The Delayed Recovery event.
1124 Context - Points to the USB_KB_DEV instance.
1131 USB_KB_DEV
*UsbKeyboardDevice
;
1132 EFI_USB_IO_PROTOCOL
*UsbIo
;
1135 UsbKeyboardDevice
= (USB_KB_DEV
*) Context
;
1137 UsbIo
= UsbKeyboardDevice
->UsbIo
;
1139 PacketSize
= (UINT8
) (UsbKeyboardDevice
->IntEndpointDescriptor
.MaxPacketSize
);
1141 UsbIo
->UsbAsyncInterruptTransfer (
1143 UsbKeyboardDevice
->IntEndpointDescriptor
.EndpointAddress
,
1145 UsbKeyboardDevice
->IntEndpointDescriptor
.Interval
,