]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Bus/Usb/UsbKb/Dxe/keyboard.c
Partially make EdkModulePkg pass intel IPF compiler with /W4 /WX switched on.
[mirror_edk2.git] / EdkModulePkg / Bus / Usb / UsbKb / Dxe / keyboard.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 Keyboard.c
15
16 Abstract:
17
18 Helper functions for USB Keyboard Driver
19
20 Revision History
21
22 --*/
23
24 #include "keyboard.h"
25
26 //
27 // USB Key Code to Efi key mapping table
28 // Format:<efi scan code>, <unicode without shift>, <unicode with shift>
29 //
30 STATIC
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_NULL, 0x00, 0x00 }, // 0x44 F11
97 { SCAN_NULL, 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 =
132 };
133
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
143 };
144
145
146 BOOLEAN
147 IsUSBKeyboard (
148 IN EFI_USB_IO_PROTOCOL *UsbIo
149 )
150 /*++
151
152 Routine Description:
153 Uses USB I/O to check whether the device is a USB Keyboard device.
154
155 Arguments:
156 UsbIo: Points to a USB I/O protocol instance.
157
158 Returns:
159
160 --*/
161 {
162 EFI_STATUS Status;
163 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
164
165 //
166 // Get the Default interface descriptor, currently we
167 // assume it is interface 1
168 //
169 Status = UsbIo->UsbGetInterfaceDescriptor (
170 UsbIo,
171 &InterfaceDescriptor
172 );
173
174 if (EFI_ERROR (Status)) {
175 return FALSE;
176 }
177
178 if (InterfaceDescriptor.InterfaceClass == CLASS_HID &&
179 InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT &&
180 InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD
181 ) {
182
183 return TRUE;
184 }
185
186 return FALSE;
187 }
188
189
190 EFI_STATUS
191 InitUSBKeyboard (
192 IN USB_KB_DEV *UsbKeyboardDevice
193 )
194 /*++
195
196 Routine Description:
197 Initialize USB Keyboard device and all private data structures.
198
199 Arguments:
200 UsbKeyboardDevice The USB_KB_DEV instance.
201
202 Returns:
203 EFI_SUCCESS - Success
204 EFI_DEVICE_ERROR - Hardware Error
205 --*/
206 {
207 UINT8 ConfigValue;
208 UINT8 Protocol;
209 UINT8 ReportId;
210 UINT8 Duration;
211 EFI_STATUS Status;
212 UINT32 TransferResult;
213
214 KbdReportStatusCode (
215 UsbKeyboardDevice->DevicePath,
216 EFI_PROGRESS_CODE,
217 (EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST)
218 );
219
220 InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));
221
222 //
223 // default configurations
224 //
225 ConfigValue = 0x01;
226
227 //
228 // Uses default configuration to configure the USB Keyboard device.
229 //
230 Status = UsbSetDeviceConfiguration (
231 UsbKeyboardDevice->UsbIo,
232 (UINT16) ConfigValue,
233 &TransferResult
234 );
235 if (EFI_ERROR (Status)) {
236 //
237 // If configuration could not be set here, it means
238 // the keyboard interface has some errors and could
239 // not be initialized
240 //
241 KbdReportStatusCode (
242 UsbKeyboardDevice->DevicePath,
243 EFI_ERROR_CODE | EFI_ERROR_MINOR,
244 (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INTERFACE_ERROR)
245 );
246
247 return EFI_DEVICE_ERROR;
248 }
249
250 UsbGetProtocolRequest (
251 UsbKeyboardDevice->UsbIo,
252 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
253 &Protocol
254 );
255 //
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?
259 //
260 if (Protocol != BOOT_PROTOCOL) {
261 UsbSetProtocolRequest (
262 UsbKeyboardDevice->UsbIo,
263 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
264 BOOT_PROTOCOL
265 );
266 }
267 //
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.
270 //
271
272 //
273 // idle value for all report ID
274 //
275 ReportId = 0;
276 //
277 // idle forever until there is a key pressed and released.
278 //
279 Duration = 0;
280 UsbSetIdleRequest (
281 UsbKeyboardDevice->UsbIo,
282 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
283 ReportId,
284 Duration
285 );
286
287 UsbKeyboardDevice->CtrlOn = 0;
288 UsbKeyboardDevice->AltOn = 0;
289 UsbKeyboardDevice->ShiftOn = 0;
290 UsbKeyboardDevice->NumLockOn = 0;
291 UsbKeyboardDevice->CapsOn = 0;
292 ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);
293
294 //
295 // Set a timer for repeat keys' generation.
296 //
297 if (UsbKeyboardDevice->RepeatTimer) {
298 gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);
299 UsbKeyboardDevice->RepeatTimer = 0;
300 }
301
302 Status = gBS->CreateEvent (
303 EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,
304 EFI_TPL_NOTIFY,
305 USBKeyboardRepeatHandler,
306 UsbKeyboardDevice,
307 &UsbKeyboardDevice->RepeatTimer
308 );
309
310 if (UsbKeyboardDevice->DelayedRecoveryEvent) {
311 gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);
312 UsbKeyboardDevice->DelayedRecoveryEvent = 0;
313 }
314
315 Status = gBS->CreateEvent (
316 EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,
317 EFI_TPL_NOTIFY,
318 USBKeyboardRecoveryHandler,
319 UsbKeyboardDevice,
320 &UsbKeyboardDevice->DelayedRecoveryEvent
321 );
322
323 return EFI_SUCCESS;
324 }
325
326 EFI_STATUS
327 EFIAPI
328 KeyboardHandler (
329 IN VOID *Data,
330 IN UINTN DataLength,
331 IN VOID *Context,
332 IN UINT32 Result
333 )
334 /*++
335
336 Routine Description:
337 Handler function for USB Keyboard's asynchronous interrupt transfer.
338
339 Arguments:
340 Data A pointer to a buffer that is filled with key data which is
341 retrieved via asynchronous interrupt transfer.
342 DataLength Indicates the size of the data buffer.
343 Context Pointing to USB_KB_DEV instance.
344 Result Indicates the result of the asynchronous interrupt transfer.
345
346 Returns:
347 EFI_SUCCESS - Success
348 EFI_DEVICE_ERROR - Hardware Error
349 --*/
350 {
351 USB_KB_DEV *UsbKeyboardDevice;
352 EFI_USB_IO_PROTOCOL *UsbIo;
353 UINT8 *CurKeyCodeBuffer;
354 UINT8 *OldKeyCodeBuffer;
355 UINT8 CurModifierMap;
356 UINT8 OldModifierMap;
357 UINT8 Index;
358 UINT8 Index2;
359 BOOLEAN Down;
360 BOOLEAN KeyRelease;
361 BOOLEAN KeyPress;
362 UINT8 SavedTail;
363 USB_KEY UsbKey;
364 UINT8 NewRepeatKey;
365 UINT32 UsbStatus;
366
367 ASSERT (Context);
368
369 NewRepeatKey = 0;
370 UsbKeyboardDevice = (USB_KB_DEV *) Context;
371 UsbIo = UsbKeyboardDevice->UsbIo;
372
373 //
374 // Analyzes the Result and performs corresponding action.
375 //
376 if (Result != EFI_USB_NOERROR) {
377 //
378 // Some errors happen during the process
379 //
380 KbdReportStatusCode (
381 UsbKeyboardDevice->DevicePath,
382 EFI_ERROR_CODE | EFI_ERROR_MINOR,
383 (EFI_PERIPHERAL_KEYBOARD | EFI_P_EC_INPUT_ERROR)
384 );
385
386 //
387 // stop the repeat key generation if any
388 //
389 UsbKeyboardDevice->RepeatKey = 0;
390
391 gBS->SetTimer (
392 UsbKeyboardDevice->RepeatTimer,
393 TimerCancel,
394 USBKBD_REPEAT_RATE
395 );
396
397 if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {
398 UsbClearEndpointHalt (
399 UsbIo,
400 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
401 &UsbStatus
402 );
403 }
404
405 //
406 // Delete & Submit this interrupt again
407 //
408
409 UsbIo->UsbAsyncInterruptTransfer (
410 UsbIo,
411 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
412 FALSE,
413 0,
414 0,
415 NULL,
416 NULL
417 );
418
419 gBS->SetTimer (
420 UsbKeyboardDevice->DelayedRecoveryEvent,
421 TimerRelative,
422 EFI_USB_INTERRUPT_DELAY
423 );
424
425 return EFI_DEVICE_ERROR;
426 }
427
428 if (DataLength == 0 || Data == NULL) {
429 return EFI_SUCCESS;
430 }
431
432 CurKeyCodeBuffer = (UINT8 *) Data;
433 OldKeyCodeBuffer = UsbKeyboardDevice->LastKeyCodeArray;
434
435 //
436 // checks for new key stroke.
437 // if no new key got, return immediately.
438 //
439 for (Index = 0; Index < 8; Index++) {
440 if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) {
441 break;
442 }
443 }
444
445 if (Index == 8) {
446 return EFI_SUCCESS;
447 }
448
449 //
450 // Parse the modifier key
451 //
452 CurModifierMap = CurKeyCodeBuffer[0];
453 OldModifierMap = OldKeyCodeBuffer[0];
454
455 //
456 // handle modifier key's pressing or releasing situation.
457 //
458 for (Index = 0; Index < 8; Index++) {
459
460 if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) {
461 //
462 // if current modifier key is up, then
463 // CurModifierMap & KB_Mod[Index].Mask = 0;
464 // otherwize it is a non-zero value.
465 // Inserts the pressed modifier key into key buffer.
466 //
467 Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask);
468 InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), KB_Mod[Index].Key, Down);
469 }
470 }
471
472 //
473 // handle normal key's releasing situation
474 //
475 KeyRelease = FALSE;
476 for (Index = 2; Index < 8; Index++) {
477
478 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) {
479 continue;
480 }
481
482 KeyRelease = TRUE;
483 for (Index2 = 2; Index2 < 8; Index2++) {
484
485 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index2])) {
486 continue;
487 }
488
489 if (OldKeyCodeBuffer[Index] == CurKeyCodeBuffer[Index2]) {
490 KeyRelease = FALSE;
491 break;
492 }
493 }
494
495 if (KeyRelease) {
496 InsertKeyCode (
497 &(UsbKeyboardDevice->KeyboardBuffer),
498 OldKeyCodeBuffer[Index],
499 0
500 );
501 //
502 // the original reapeat key is released.
503 //
504 if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) {
505 UsbKeyboardDevice->RepeatKey = 0;
506 }
507 }
508 }
509
510 //
511 // original repeat key is released, cancel the repeat timer
512 //
513 if (UsbKeyboardDevice->RepeatKey == 0) {
514 gBS->SetTimer (
515 UsbKeyboardDevice->RepeatTimer,
516 TimerCancel,
517 USBKBD_REPEAT_RATE
518 );
519 }
520
521 //
522 // handle normal key's pressing situation
523 //
524 KeyPress = FALSE;
525 for (Index = 2; Index < 8; Index++) {
526
527 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) {
528 continue;
529 }
530
531 KeyPress = TRUE;
532 for (Index2 = 2; Index2 < 8; Index2++) {
533
534 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index2])) {
535 continue;
536 }
537
538 if (CurKeyCodeBuffer[Index] == OldKeyCodeBuffer[Index2]) {
539 KeyPress = FALSE;
540 break;
541 }
542 }
543
544 if (KeyPress) {
545 InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1);
546 //
547 // NumLock pressed or CapsLock pressed
548 //
549 if (CurKeyCodeBuffer[Index] == 0x53 || CurKeyCodeBuffer[Index] == 0x39) {
550 UsbKeyboardDevice->RepeatKey = 0;
551 } else {
552 NewRepeatKey = CurKeyCodeBuffer[Index];
553 //
554 // do not repeat the original repeated key
555 //
556 UsbKeyboardDevice->RepeatKey = 0;
557 }
558 }
559 }
560
561 //
562 // Update LastKeycodeArray[] buffer in the
563 // Usb Keyboard Device data structure.
564 //
565 for (Index = 0; Index < 8; Index++) {
566 UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index];
567 }
568
569 //
570 // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence
571 // and judge whether it will invoke reset event.
572 //
573 SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail;
574 Index = UsbKeyboardDevice->KeyboardBuffer.bHead;
575 while (Index != SavedTail) {
576 RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
577
578 switch (UsbKey.KeyCode) {
579
580 case 0xe0:
581 case 0xe4:
582 if (UsbKey.Down) {
583 UsbKeyboardDevice->CtrlOn = 1;
584 } else {
585 UsbKeyboardDevice->CtrlOn = 0;
586 }
587 break;
588
589 case 0xe2:
590 case 0xe6:
591 if (UsbKey.Down) {
592 UsbKeyboardDevice->AltOn = 1;
593 } else {
594 UsbKeyboardDevice->AltOn = 0;
595 }
596 break;
597
598 //
599 // Del Key Code
600 //
601 case 0x4c:
602 case 0x63:
603 if (UsbKey.Down) {
604 if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) {
605 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
606 }
607 }
608 break;
609
610 default:
611 break;
612 }
613
614 //
615 // insert the key back to the buffer.
616 // so the key sequence will not be destroyed.
617 //
618 InsertKeyCode (
619 &(UsbKeyboardDevice->KeyboardBuffer),
620 UsbKey.KeyCode,
621 UsbKey.Down
622 );
623 Index = UsbKeyboardDevice->KeyboardBuffer.bHead;
624
625 }
626 //
627 // If have new key pressed, update the RepeatKey value, and set the
628 // timer to repeate delay timer
629 //
630 if (NewRepeatKey != 0) {
631 //
632 // sets trigger time to "Repeat Delay Time",
633 // to trigger the repeat timer when the key is hold long
634 // enough time.
635 //
636 gBS->SetTimer (
637 UsbKeyboardDevice->RepeatTimer,
638 TimerRelative,
639 USBKBD_REPEAT_DELAY
640 );
641 UsbKeyboardDevice->RepeatKey = NewRepeatKey;
642 }
643
644 return EFI_SUCCESS;
645 }
646
647 EFI_STATUS
648 USBParseKey (
649 IN OUT USB_KB_DEV *UsbKeyboardDevice,
650 OUT UINT8 *KeyChar
651 )
652 /*++
653
654 Routine Description:
655 Retrieves a key character after parsing the raw data in keyboard buffer.
656
657 Arguments:
658 UsbKeyboardDevice The USB_KB_DEV instance.
659 KeyChar Points to the Key character after key parsing.
660
661 Returns:
662 EFI_SUCCESS - Success
663 EFI_NOT_READY - Device is not ready
664 --*/
665 {
666 USB_KEY UsbKey;
667
668 *KeyChar = 0;
669
670 while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {
671 //
672 // pops one raw data off.
673 //
674 RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
675
676 if (!UsbKey.Down) {
677 switch (UsbKey.KeyCode) {
678
679 case 0xe0:
680 case 0xe4:
681 UsbKeyboardDevice->CtrlOn = 0;
682 break;
683
684 case 0xe1:
685 case 0xe5:
686 UsbKeyboardDevice->ShiftOn = 0;
687 break;
688
689 case 0xe2:
690 case 0xe6:
691 UsbKeyboardDevice->AltOn = 0;
692 break;
693
694 default:
695 break;
696 }
697
698 continue;
699 }
700
701 //
702 // Analyzes key pressing situation
703 //
704 switch (UsbKey.KeyCode) {
705
706 case 0xe0:
707 case 0xe4:
708 UsbKeyboardDevice->CtrlOn = 1;
709 continue;
710 break;
711
712 case 0xe1:
713 case 0xe5:
714 UsbKeyboardDevice->ShiftOn = 1;
715 continue;
716 break;
717
718 case 0xe2:
719 case 0xe6:
720 UsbKeyboardDevice->AltOn = 1;
721 continue;
722 break;
723
724 case 0xe3:
725 case 0xe7:
726 continue;
727 break;
728
729 case 0x53:
730 UsbKeyboardDevice->NumLockOn ^= 1;
731 SetKeyLED (UsbKeyboardDevice);
732 continue;
733 break;
734
735 case 0x39:
736 UsbKeyboardDevice->CapsOn ^= 1;
737 SetKeyLED (UsbKeyboardDevice);
738 continue;
739 break;
740
741 //
742 // F11,F12,PrintScreen,ScrollLock,Pause,Application,Power
743 // keys are not valid EFI key
744 //
745 case 0x44:
746 //
747 // fall through
748 //
749 case 0x45:
750 //
751 // fall through
752 //
753 case 0x46:
754 //
755 // fall through
756 //
757 case 0x47:
758 //
759 // Turn on the ScrollLock light on KB
760 //
761 UsbKeyboardDevice->ScrollOn ^= 1;
762 SetKeyLED (UsbKeyboardDevice);
763 continue;
764 break;
765 case 0x48:
766 //
767 // fall through
768 //
769 case 0x65:
770 case 0x66:
771 continue;
772 break;
773
774 default:
775 break;
776 }
777
778 //
779 // When encountered Del Key...
780 //
781 if (UsbKey.KeyCode == 0x4c || UsbKey.KeyCode == 0x63) {
782 if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) {
783 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
784 }
785 }
786
787 *KeyChar = UsbKey.KeyCode;
788 return EFI_SUCCESS;
789 }
790
791 return EFI_NOT_READY;
792
793 }
794
795
796 EFI_STATUS
797 USBKeyCodeToEFIScanCode (
798 IN USB_KB_DEV *UsbKeyboardDevice,
799 IN UINT8 KeyChar,
800 OUT EFI_INPUT_KEY *Key
801 )
802 /*++
803
804 Routine Description:
805 Converts USB Keyboard code to EFI Scan Code.
806
807 Arguments:
808 UsbKeyboardDevice The USB_KB_DEV instance.
809 KeyChar Indicates the key code that will be interpreted.
810 Key A pointer to a buffer that is filled in with
811 the keystroke information for the key that
812 was pressed.
813 Returns:
814 EFI_NOT_READY - Device is not ready
815 EFI_SUCCESS - Success
816 --*/
817 {
818 UINT8 Index;
819
820 if (!USBKBD_VALID_KEYCODE (KeyChar)) {
821 return EFI_NOT_READY;
822 }
823
824 //
825 // valid USB Key Code starts from 4
826 //
827 Index = (UINT8) (KeyChar - 4);
828
829 if (Index >= USB_KEYCODE_MAX_MAKE) {
830 return EFI_NOT_READY;
831 }
832
833 Key->ScanCode = KeyConvertionTable[Index][0];
834
835 if (UsbKeyboardDevice->ShiftOn) {
836
837 Key->UnicodeChar = KeyConvertionTable[Index][2];
838
839 } else {
840
841 Key->UnicodeChar = KeyConvertionTable[Index][1];
842 }
843
844 if (UsbKeyboardDevice->CapsOn) {
845
846 if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {
847
848 Key->UnicodeChar = KeyConvertionTable[Index][2];
849
850 } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') {
851
852 Key->UnicodeChar = KeyConvertionTable[Index][1];
853
854 }
855 }
856
857 if (KeyChar >= 0x59 && KeyChar <= 0x63) {
858
859 if (UsbKeyboardDevice->NumLockOn && !UsbKeyboardDevice->ShiftOn) {
860
861 Key->ScanCode = SCAN_NULL;
862
863 } else {
864
865 Key->UnicodeChar = 0x00;
866 }
867 }
868
869 if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {
870 return EFI_NOT_READY;
871 }
872
873 return EFI_SUCCESS;
874
875 }
876
877
878 EFI_STATUS
879 InitUSBKeyBuffer (
880 IN OUT USB_KB_BUFFER *KeyboardBuffer
881 )
882 /*++
883
884 Routine Description:
885 Resets USB Keyboard Buffer.
886
887 Arguments:
888 KeyboardBuffer - Points to the USB Keyboard Buffer.
889
890 Returns:
891 EFI_SUCCESS - Success
892 --*/
893 {
894 ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));
895
896 KeyboardBuffer->bHead = KeyboardBuffer->bTail;
897
898 return EFI_SUCCESS;
899 }
900
901 BOOLEAN
902 IsUSBKeyboardBufferEmpty (
903 IN USB_KB_BUFFER *KeyboardBuffer
904 )
905 /*++
906
907 Routine Description:
908 Check whether USB Keyboard buffer is empty.
909
910 Arguments:
911 KeyboardBuffer - USB Keyboard Buffer.
912
913 Returns:
914
915 --*/
916 {
917 //
918 // meet FIFO empty condition
919 //
920 return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail);
921 }
922
923
924 BOOLEAN
925 IsUSBKeyboardBufferFull (
926 IN USB_KB_BUFFER *KeyboardBuffer
927 )
928 /*++
929
930 Routine Description:
931 Check whether USB Keyboard buffer is full.
932
933 Arguments:
934 KeyboardBuffer - USB Keyboard Buffer.
935
936 Returns:
937
938 --*/
939 {
940 return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) ==
941 KeyboardBuffer->bHead);
942 }
943
944
945 EFI_STATUS
946 InsertKeyCode (
947 IN OUT USB_KB_BUFFER *KeyboardBuffer,
948 IN UINT8 Key,
949 IN UINT8 Down
950 )
951 /*++
952
953 Routine Description:
954 Inserts a key code into keyboard buffer.
955
956 Arguments:
957 KeyboardBuffer - Points to the USB Keyboard Buffer.
958 Key - Key code
959 Down - Special key
960 Returns:
961 EFI_SUCCESS - Success
962 --*/
963 {
964 USB_KEY UsbKey;
965
966 //
967 // if keyboard buffer is full, throw the
968 // first key out of the keyboard buffer.
969 //
970 if (IsUSBKeyboardBufferFull (KeyboardBuffer)) {
971 RemoveKeyCode (KeyboardBuffer, &UsbKey);
972 }
973
974 KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key;
975 KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down = Down;
976
977 //
978 // adjust the tail pointer of the FIFO keyboard buffer.
979 //
980 KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1));
981
982 return EFI_SUCCESS;
983 }
984
985 EFI_STATUS
986 RemoveKeyCode (
987 IN OUT USB_KB_BUFFER *KeyboardBuffer,
988 OUT USB_KEY *UsbKey
989 )
990 /*++
991
992 Routine Description:
993 Pops a key code off from keyboard buffer.
994
995 Arguments:
996 KeyboardBuffer - Points to the USB Keyboard Buffer.
997 UsbKey - Points to the buffer that contains a usb key code.
998
999 Returns:
1000 EFI_SUCCESS - Success
1001 EFI_DEVICE_ERROR - Hardware Error
1002 --*/
1003 {
1004 if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {
1005 return EFI_DEVICE_ERROR;
1006 }
1007
1008 UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode;
1009 UsbKey->Down = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down;
1010
1011 //
1012 // adjust the head pointer of the FIFO keyboard buffer.
1013 //
1014 KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1));
1015
1016 return EFI_SUCCESS;
1017 }
1018
1019 EFI_STATUS
1020 SetKeyLED (
1021 IN USB_KB_DEV *UsbKeyboardDevice
1022 )
1023 /*++
1024
1025 Routine Description:
1026 Sets USB Keyboard LED state.
1027
1028 Arguments:
1029 UsbKeyboardDevice - The USB_KB_DEV instance.
1030
1031 Returns:
1032 EFI_SUCCESS - Success
1033 --*/
1034 {
1035 LED_MAP Led;
1036 UINT8 ReportId;
1037
1038 //
1039 // Set each field in Led map.
1040 //
1041 Led.NumLock = (UINT8) UsbKeyboardDevice->NumLockOn;
1042 Led.CapsLock = (UINT8) UsbKeyboardDevice->CapsOn;
1043 Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn;
1044 Led.Resrvd = 0;
1045
1046 ReportId = 0;
1047 //
1048 // call Set Report Request to lighten the LED.
1049 //
1050 UsbSetReportRequest (
1051 UsbKeyboardDevice->UsbIo,
1052 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
1053 ReportId,
1054 HID_OUTPUT_REPORT,
1055 1,
1056 (UINT8 *) &Led
1057 );
1058
1059 return EFI_SUCCESS;
1060 }
1061
1062 VOID
1063 EFIAPI
1064 USBKeyboardRepeatHandler (
1065 IN EFI_EVENT Event,
1066 IN VOID *Context
1067 )
1068 /*++
1069
1070 Routine Description:
1071 Timer handler for Repeat Key timer.
1072
1073 Arguments:
1074 Event - The Repeat Key event.
1075 Context - Points to the USB_KB_DEV instance.
1076
1077 Returns:
1078
1079 --*/
1080 {
1081 USB_KB_DEV *UsbKeyboardDevice;
1082
1083 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1084
1085 //
1086 // Do nothing when there is no repeat key.
1087 //
1088 if (UsbKeyboardDevice->RepeatKey != 0) {
1089 //
1090 // Inserts one Repeat key into keyboard buffer,
1091 //
1092 InsertKeyCode (
1093 &(UsbKeyboardDevice->KeyboardBuffer),
1094 UsbKeyboardDevice->RepeatKey,
1095 1
1096 );
1097
1098 //
1099 // set repeate rate for repeat key generation.
1100 //
1101 gBS->SetTimer (
1102 UsbKeyboardDevice->RepeatTimer,
1103 TimerRelative,
1104 USBKBD_REPEAT_RATE
1105 );
1106
1107 }
1108 }
1109
1110 VOID
1111 EFIAPI
1112 USBKeyboardRecoveryHandler (
1113 IN EFI_EVENT Event,
1114 IN VOID *Context
1115 )
1116 /*++
1117
1118 Routine Description:
1119 Timer handler for Delayed Recovery timer.
1120
1121 Arguments:
1122 Event - The Delayed Recovery event.
1123 Context - Points to the USB_KB_DEV instance.
1124
1125 Returns:
1126
1127 --*/
1128 {
1129
1130 USB_KB_DEV *UsbKeyboardDevice;
1131 EFI_USB_IO_PROTOCOL *UsbIo;
1132 UINT8 PacketSize;
1133
1134 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1135
1136 UsbIo = UsbKeyboardDevice->UsbIo;
1137
1138 PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);
1139
1140 UsbIo->UsbAsyncInterruptTransfer (
1141 UsbIo,
1142 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
1143 TRUE,
1144 UsbKeyboardDevice->IntEndpointDescriptor.Interval,
1145 PacketSize,
1146 KeyboardHandler,
1147 UsbKeyboardDevice
1148 );
1149 }