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