]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c
41286d6866801db6fbe3bc01f26c590781d81b5d
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbKbDxe / keyboard.c
1 /** @file
2
3 Copyright (c) 2004 - 2005, 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
25 #include "keyboard.h"
26 #include <Library/UsbLib.h>
27 //
28 // USB Key Code to Efi key mapping table
29 // Format:<efi scan code>, <unicode without shift>, <unicode with shift>
30 //
31 STATIC
32 UINT8 KeyConvertionTable[USB_KEYCODE_MAX_MAKE][3] = {
33 { SCAN_NULL, 'a', 'A' }, // 0x04
34 { SCAN_NULL, 'b', 'B' }, // 0x05
35 { SCAN_NULL, 'c', 'C' }, // 0x06
36 { SCAN_NULL, 'd', 'D' }, // 0x07
37 { SCAN_NULL, 'e', 'E' }, // 0x08
38 { SCAN_NULL, 'f', 'F' }, // 0x09
39 { SCAN_NULL, 'g', 'G' }, // 0x0A
40 { SCAN_NULL, 'h', 'H' }, // 0x0B
41 { SCAN_NULL, 'i', 'I' }, // 0x0C
42 { SCAN_NULL, 'j', 'J' }, // 0x0D
43 { SCAN_NULL, 'k', 'K' }, // 0x0E
44 { SCAN_NULL, 'l', 'L' }, // 0x0F
45 { SCAN_NULL, 'm', 'M' }, // 0x10
46 { SCAN_NULL, 'n', 'N' }, // 0x11
47 { SCAN_NULL, 'o', 'O' }, // 0x12
48 { SCAN_NULL, 'p', 'P' }, // 0x13
49 { SCAN_NULL, 'q', 'Q' }, // 0x14
50 { SCAN_NULL, 'r', 'R' }, // 0x15
51 { SCAN_NULL, 's', 'S' }, // 0x16
52 { SCAN_NULL, 't', 'T' }, // 0x17
53 { SCAN_NULL, 'u', 'U' }, // 0x18
54 { SCAN_NULL, 'v', 'V' }, // 0x19
55 { SCAN_NULL, 'w', 'W' }, // 0x1A
56 { SCAN_NULL, 'x', 'X' }, // 0x1B
57 { SCAN_NULL, 'y', 'Y' }, // 0x1C
58 { SCAN_NULL, 'z', 'Z' }, // 0x1D
59 { SCAN_NULL, '1', '!' }, // 0x1E
60 { SCAN_NULL, '2', '@' }, // 0x1F
61 { SCAN_NULL, '3', '#' }, // 0x20
62 { SCAN_NULL, '4', '$' }, // 0x21
63 { SCAN_NULL, '5', '%' }, // 0x22
64 { SCAN_NULL, '6', '^' }, // 0x23
65 { SCAN_NULL, '7', '&' }, // 0x24
66 { SCAN_NULL, '8', '*' }, // 0x25
67 { SCAN_NULL, '9', '(' }, // 0x26
68 { SCAN_NULL, '0', ')' }, // 0x27
69 { SCAN_NULL, 0x0d, 0x0d }, // 0x28 Enter
70 { SCAN_ESC, 0x00, 0x00 }, // 0x29 Esc
71 { SCAN_NULL, 0x08, 0x08 }, // 0x2A Backspace
72 { SCAN_NULL, 0x09, 0x09 }, // 0x2B Tab
73 { SCAN_NULL, ' ', ' ' }, // 0x2C Spacebar
74 { SCAN_NULL, '-', '_' }, // 0x2D
75 { SCAN_NULL, '=', '+' }, // 0x2E
76 { SCAN_NULL, '[', '{' }, // 0x2F
77 { SCAN_NULL, ']', '}' }, // 0x30
78 { SCAN_NULL, '\\', '|' }, // 0x31
79 { SCAN_NULL, '\\', '|' }, // 0x32 Keyboard US \ and |
80 { SCAN_NULL, ';', ':' }, // 0x33
81 { SCAN_NULL, '\'', '"' }, // 0x34
82 { SCAN_NULL, '`', '~' }, // 0x35 Keyboard Grave Accent and Tlide
83 { SCAN_NULL, ',', '<' }, // 0x36
84 { SCAN_NULL, '.', '>' }, // 0x37
85 { SCAN_NULL, '/', '?' }, // 0x38
86 { SCAN_NULL, 0x00, 0x00 }, // 0x39 CapsLock
87 { SCAN_F1, 0x00, 0x00 }, // 0x3A
88 { SCAN_F2, 0x00, 0x00 }, // 0x3B
89 { SCAN_F3, 0x00, 0x00 }, // 0x3C
90 { SCAN_F4, 0x00, 0x00 }, // 0x3D
91 { SCAN_F5, 0x00, 0x00 }, // 0x3E
92 { SCAN_F6, 0x00, 0x00 }, // 0x3F
93 { SCAN_F7, 0x00, 0x00 }, // 0x40
94 { SCAN_F8, 0x00, 0x00 }, // 0x41
95 { SCAN_F9, 0x00, 0x00 }, // 0x42
96 { SCAN_F10, 0x00, 0x00 }, // 0x43
97 { SCAN_F11, 0x00, 0x00 }, // 0x44 F11
98 { SCAN_F12, 0x00, 0x00 }, // 0x45 F12
99 { SCAN_NULL, 0x00, 0x00 }, // 0x46 PrintScreen
100 { SCAN_NULL, 0x00, 0x00 }, // 0x47 Scroll Lock
101 { SCAN_NULL, 0x00, 0x00 }, // 0x48 Pause
102 { SCAN_INSERT, 0x00, 0x00 }, // 0x49
103 { SCAN_HOME, 0x00, 0x00 }, // 0x4A
104 { SCAN_PAGE_UP, 0x00, 0x00 }, // 0x4B
105 { SCAN_DELETE, 0x00, 0x00 }, // 0x4C
106 { SCAN_END, 0x00, 0x00 }, // 0x4D
107 { SCAN_PAGE_DOWN, 0x00, 0x00 }, // 0x4E
108 { SCAN_RIGHT, 0x00, 0x00 }, // 0x4F
109 { SCAN_LEFT, 0x00, 0x00 }, // 0x50
110 { SCAN_DOWN, 0x00, 0x00 }, // 0x51
111 { SCAN_UP, 0x00, 0x00 }, // 0x52
112 { SCAN_NULL, 0x00, 0x00 }, // 0x53 NumLock
113 { SCAN_NULL, '/', '/' }, // 0x54
114 { SCAN_NULL, '*', '*' }, // 0x55
115 { SCAN_NULL, '-', '-' }, // 0x56
116 { SCAN_NULL, '+', '+' }, // 0x57
117 { SCAN_NULL, 0x0d, 0x0d }, // 0x58
118 { SCAN_END, '1', '1' }, // 0x59
119 { SCAN_DOWN, '2', '2' }, // 0x5A
120 { SCAN_PAGE_DOWN, '3', '3' }, // 0x5B
121 { SCAN_LEFT, '4', '4' }, // 0x5C
122 { SCAN_NULL, '5', '5' }, // 0x5D
123 { SCAN_RIGHT, '6', '6' }, // 0x5E
124 { SCAN_HOME, '7', '7' }, // 0x5F
125 { SCAN_UP, '8', '8' }, // 0x60
126 { SCAN_PAGE_UP, '9', '9' }, // 0x61
127 { SCAN_INSERT, '0', '0' }, // 0x62
128 { SCAN_DELETE, '.', '.' }, // 0x63
129 { SCAN_NULL, '\\', '|' }, // 0x64 Keyboard Non-US \ and |
130 { SCAN_NULL, 0x00, 0x00 }, // 0x65 Keyboard Application
131 { SCAN_NULL, 0x00, 0x00 }, // 0x66 Keyboard Power
132 { SCAN_NULL, '=' , '=' } // 0x67 Keypad =
133 };
134
135 STATIC KB_MODIFIER KB_Mod[8] = {
136 { MOD_CONTROL_L, 0xe0 }, // 11100000
137 { MOD_CONTROL_R, 0xe4 }, // 11100100
138 { MOD_SHIFT_L, 0xe1 }, // 11100001
139 { MOD_SHIFT_R, 0xe5 }, // 11100101
140 { MOD_ALT_L, 0xe2 }, // 11100010
141 { MOD_ALT_R, 0xe6 }, // 11100110
142 { MOD_WIN_L, 0xe3 }, // 11100011
143 { MOD_WIN_R, 0xe7 } // 11100111
144 };
145
146
147
148 /**
149 Uses USB I/O to check whether the device is a USB Keyboard device.
150
151 UsbIo: Points to a USB I/O protocol instance.
152
153
154 **/
155 BOOLEAN
156 IsUSBKeyboard (
157 IN EFI_USB_IO_PROTOCOL *UsbIo
158 )
159 {
160 EFI_STATUS Status;
161 EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
162
163 //
164 // Get the Default interface descriptor, currently we
165 // assume it is interface 1
166 //
167 Status = UsbIo->UsbGetInterfaceDescriptor (
168 UsbIo,
169 &InterfaceDescriptor
170 );
171
172 if (EFI_ERROR (Status)) {
173 return FALSE;
174 }
175
176 if (InterfaceDescriptor.InterfaceClass == CLASS_HID &&
177 InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT &&
178 InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD
179 ) {
180
181 return TRUE;
182 }
183
184 return FALSE;
185 }
186
187
188
189 /**
190 Initialize USB Keyboard device and all private data structures.
191
192 UsbKeyboardDevice The USB_KB_DEV instance.
193
194 @retval EFI_SUCCESS Success
195 @retval EFI_DEVICE_ERROR Hardware Error
196
197 **/
198 EFI_STATUS
199 InitUSBKeyboard (
200 IN USB_KB_DEV *UsbKeyboardDevice
201 )
202 {
203 UINT8 ConfigValue;
204 UINT8 Protocol;
205 UINT8 ReportId;
206 UINT8 Duration;
207 EFI_STATUS Status;
208 UINT32 TransferResult;
209 EFI_USB_IO_PROTOCOL *UsbIo;
210
211 UsbIo = UsbKeyboardDevice->UsbIo;
212
213 KbdReportStatusCode (
214 UsbKeyboardDevice->DevicePath,
215 EFI_PROGRESS_CODE,
216 PcdGet32 (PcdStatusCodeValueKeyboardSelfTest)
217 );
218
219 InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));
220
221 //
222 // default configurations
223 //
224 ConfigValue = 0x01;
225
226 //
227 // Uses default configuration to configure the USB Keyboard device.
228 //
229 Status = UsbSetConfiguration (
230 UsbKeyboardDevice->UsbIo,
231 (UINT16) ConfigValue,
232 &TransferResult
233 );
234 if (EFI_ERROR (Status)) {
235 //
236 // If configuration could not be set here, it means
237 // the keyboard interface has some errors and could
238 // not be initialized
239 //
240 KbdReportStatusCode (
241 UsbKeyboardDevice->DevicePath,
242 EFI_ERROR_CODE | EFI_ERROR_MINOR,
243 PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError)
244 );
245
246 return EFI_DEVICE_ERROR;
247 }
248
249 UsbGetProtocolRequest (
250 UsbKeyboardDevice->UsbIo,
251 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
252 &Protocol
253 );
254 //
255 // Sets boot protocol for the USB Keyboard.
256 // This driver only supports boot protocol.
257 // !!BugBug: How about the device that does not support boot protocol?
258 //
259 if (Protocol != BOOT_PROTOCOL) {
260 UsbSetProtocolRequest (
261 UsbKeyboardDevice->UsbIo,
262 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
263 BOOT_PROTOCOL
264 );
265 }
266 //
267 // the duration is indefinite, so the endpoint will inhibit reporting forever,
268 // and only reporting when a change is detected in the report data.
269 //
270
271 //
272 // idle value for all report ID
273 //
274 ReportId = 0;
275 //
276 // idle forever until there is a key pressed and released.
277 //
278 Duration = 0;
279 UsbSetIdleRequest (
280 UsbKeyboardDevice->UsbIo,
281 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
282 ReportId,
283 Duration
284 );
285
286 UsbKeyboardDevice->CtrlOn = 0;
287 UsbKeyboardDevice->AltOn = 0;
288 UsbKeyboardDevice->ShiftOn = 0;
289 UsbKeyboardDevice->NumLockOn = 0;
290 UsbKeyboardDevice->CapsOn = 0;
291 UsbKeyboardDevice->ScrollOn = 0;
292
293 //
294 // Sync the initial state of lights
295 //
296 SetKeyLED (UsbKeyboardDevice);
297
298 ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);
299
300 //
301 // Set a timer for repeat keys' generation.
302 //
303 if (UsbKeyboardDevice->RepeatTimer) {
304 gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);
305 UsbKeyboardDevice->RepeatTimer = 0;
306 }
307
308 Status = gBS->CreateEvent (
309 EVT_TIMER | EVT_NOTIFY_SIGNAL,
310 TPL_NOTIFY,
311 USBKeyboardRepeatHandler,
312 UsbKeyboardDevice,
313 &UsbKeyboardDevice->RepeatTimer
314 );
315
316 if (UsbKeyboardDevice->DelayedRecoveryEvent) {
317 gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);
318 UsbKeyboardDevice->DelayedRecoveryEvent = 0;
319 }
320
321 Status = gBS->CreateEvent (
322 EVT_TIMER | EVT_NOTIFY_SIGNAL,
323 TPL_NOTIFY,
324 USBKeyboardRecoveryHandler,
325 UsbKeyboardDevice,
326 &UsbKeyboardDevice->DelayedRecoveryEvent
327 );
328
329 return EFI_SUCCESS;
330 }
331
332
333 /**
334 Handler function for USB Keyboard's asynchronous interrupt transfer.
335
336 Data A pointer to a buffer that is filled with key data which is
337 retrieved via asynchronous interrupt transfer.
338 DataLength Indicates the size of the data buffer.
339 Context Pointing to USB_KB_DEV instance.
340 Result Indicates the result of the asynchronous interrupt transfer.
341
342 @retval EFI_SUCCESS Success
343 @retval EFI_DEVICE_ERROR Hardware Error
344
345 **/
346 EFI_STATUS
347 EFIAPI
348 KeyboardHandler (
349 IN VOID *Data,
350 IN UINTN DataLength,
351 IN VOID *Context,
352 IN UINT32 Result
353 )
354 {
355 USB_KB_DEV *UsbKeyboardDevice;
356 EFI_USB_IO_PROTOCOL *UsbIo;
357 UINT8 *CurKeyCodeBuffer;
358 UINT8 *OldKeyCodeBuffer;
359 UINT8 CurModifierMap;
360 UINT8 OldModifierMap;
361 UINT8 Index;
362 UINT8 Index2;
363 BOOLEAN Down;
364 EFI_STATUS Status;
365 BOOLEAN KeyRelease;
366 BOOLEAN KeyPress;
367 UINT8 SavedTail;
368 USB_KEY UsbKey;
369 UINT8 NewRepeatKey;
370 UINT32 UsbStatus;
371 UINT8 *DataPtr;
372
373 ASSERT (Context);
374
375 NewRepeatKey = 0;
376 DataPtr = (UINT8 *) Data;
377 UsbKeyboardDevice = (USB_KB_DEV *) Context;
378 UsbIo = UsbKeyboardDevice->UsbIo;
379
380 //
381 // Analyzes the Result and performs corresponding action.
382 //
383 if (Result != EFI_USB_NOERROR) {
384 //
385 // Some errors happen during the process
386 //
387 KbdReportStatusCode (
388 UsbKeyboardDevice->DevicePath,
389 EFI_ERROR_CODE | EFI_ERROR_MINOR,
390 PcdGet32 (PcdStatusCodeValueKeyboardInputError)
391 );
392
393 //
394 // stop the repeat key generation if any
395 //
396 UsbKeyboardDevice->RepeatKey = 0;
397
398 gBS->SetTimer (
399 UsbKeyboardDevice->RepeatTimer,
400 TimerCancel,
401 USBKBD_REPEAT_RATE
402 );
403
404 if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {
405 UsbClearEndpointHalt (
406 UsbIo,
407 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
408 &UsbStatus
409 );
410 }
411
412 //
413 // Delete & Submit this interrupt again
414 //
415
416 Status = UsbIo->UsbAsyncInterruptTransfer (
417 UsbIo,
418 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
419 FALSE,
420 0,
421 0,
422 NULL,
423 NULL
424 );
425
426 gBS->SetTimer (
427 UsbKeyboardDevice->DelayedRecoveryEvent,
428 TimerRelative,
429 EFI_USB_INTERRUPT_DELAY
430 );
431
432 return EFI_DEVICE_ERROR;
433 }
434
435 if (DataLength == 0 || Data == NULL) {
436 return EFI_SUCCESS;
437 }
438
439 CurKeyCodeBuffer = (UINT8 *) Data;
440 OldKeyCodeBuffer = UsbKeyboardDevice->LastKeyCodeArray;
441
442 //
443 // checks for new key stroke.
444 // if no new key got, return immediately.
445 //
446 for (Index = 0; Index < 8; Index++) {
447 if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) {
448 break;
449 }
450 }
451
452 if (Index == 8) {
453 return EFI_SUCCESS;
454 }
455
456 //
457 // Parse the modifier key
458 //
459 CurModifierMap = CurKeyCodeBuffer[0];
460 OldModifierMap = OldKeyCodeBuffer[0];
461
462 //
463 // handle modifier key's pressing or releasing situation.
464 //
465 for (Index = 0; Index < 8; Index++) {
466
467 if ((CurModifierMap & KB_Mod[Index].Mask) != (OldModifierMap & KB_Mod[Index].Mask)) {
468 //
469 // if current modifier key is up, then
470 // CurModifierMap & KB_Mod[Index].Mask = 0;
471 // otherwize it is a non-zero value.
472 // Inserts the pressed modifier key into key buffer.
473 //
474 Down = (UINT8) (CurModifierMap & KB_Mod[Index].Mask);
475 InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), KB_Mod[Index].Key, Down);
476 }
477 }
478
479 //
480 // handle normal key's releasing situation
481 //
482 KeyRelease = FALSE;
483 for (Index = 2; Index < 8; Index++) {
484
485 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) {
486 continue;
487 }
488
489 KeyRelease = TRUE;
490 for (Index2 = 2; Index2 < 8; Index2++) {
491
492 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index2])) {
493 continue;
494 }
495
496 if (OldKeyCodeBuffer[Index] == CurKeyCodeBuffer[Index2]) {
497 KeyRelease = FALSE;
498 break;
499 }
500 }
501
502 if (KeyRelease) {
503 InsertKeyCode (
504 &(UsbKeyboardDevice->KeyboardBuffer),
505 OldKeyCodeBuffer[Index],
506 0
507 );
508 //
509 // the original reapeat key is released.
510 //
511 if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) {
512 UsbKeyboardDevice->RepeatKey = 0;
513 }
514 }
515 }
516
517 //
518 // original repeat key is released, cancel the repeat timer
519 //
520 if (UsbKeyboardDevice->RepeatKey == 0) {
521 gBS->SetTimer (
522 UsbKeyboardDevice->RepeatTimer,
523 TimerCancel,
524 USBKBD_REPEAT_RATE
525 );
526 }
527
528 //
529 // handle normal key's pressing situation
530 //
531 KeyPress = FALSE;
532 for (Index = 2; Index < 8; Index++) {
533
534 if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) {
535 continue;
536 }
537
538 KeyPress = TRUE;
539 for (Index2 = 2; Index2 < 8; Index2++) {
540
541 if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index2])) {
542 continue;
543 }
544
545 if (CurKeyCodeBuffer[Index] == OldKeyCodeBuffer[Index2]) {
546 KeyPress = FALSE;
547 break;
548 }
549 }
550
551 if (KeyPress) {
552 InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], 1);
553 //
554 // NumLock pressed or CapsLock pressed
555 //
556 if (CurKeyCodeBuffer[Index] == 0x53 || CurKeyCodeBuffer[Index] == 0x39) {
557 UsbKeyboardDevice->RepeatKey = 0;
558 } else {
559 NewRepeatKey = CurKeyCodeBuffer[Index];
560 //
561 // do not repeat the original repeated key
562 //
563 UsbKeyboardDevice->RepeatKey = 0;
564 }
565 }
566 }
567
568 //
569 // Update LastKeycodeArray[] buffer in the
570 // Usb Keyboard Device data structure.
571 //
572 for (Index = 0; Index < 8; Index++) {
573 UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index];
574 }
575
576 //
577 // pre-process KeyboardBuffer, pop out the ctrl,alt,del key in sequence
578 // and judge whether it will invoke reset event.
579 //
580 SavedTail = UsbKeyboardDevice->KeyboardBuffer.bTail;
581 Index = UsbKeyboardDevice->KeyboardBuffer.bHead;
582 while (Index != SavedTail) {
583 RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
584
585 switch (UsbKey.KeyCode) {
586
587 case 0xe0:
588 case 0xe4:
589 if (UsbKey.Down) {
590 UsbKeyboardDevice->CtrlOn = 1;
591 } else {
592 UsbKeyboardDevice->CtrlOn = 0;
593 }
594 break;
595
596 case 0xe2:
597 case 0xe6:
598 if (UsbKey.Down) {
599 UsbKeyboardDevice->AltOn = 1;
600 } else {
601 UsbKeyboardDevice->AltOn = 0;
602 }
603 break;
604
605 //
606 // Del Key Code
607 //
608 case 0x4c:
609 case 0x63:
610 if (UsbKey.Down) {
611 if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) {
612 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
613 }
614 }
615 break;
616
617 default:
618 break;
619 }
620
621 //
622 // insert the key back to the buffer.
623 // so the key sequence will not be destroyed.
624 //
625 InsertKeyCode (
626 &(UsbKeyboardDevice->KeyboardBuffer),
627 UsbKey.KeyCode,
628 UsbKey.Down
629 );
630 Index = UsbKeyboardDevice->KeyboardBuffer.bHead;
631
632 }
633 //
634 // If have new key pressed, update the RepeatKey value, and set the
635 // timer to repeate delay timer
636 //
637 if (NewRepeatKey != 0) {
638 //
639 // sets trigger time to "Repeat Delay Time",
640 // to trigger the repeat timer when the key is hold long
641 // enough time.
642 //
643 gBS->SetTimer (
644 UsbKeyboardDevice->RepeatTimer,
645 TimerRelative,
646 USBKBD_REPEAT_DELAY
647 );
648 UsbKeyboardDevice->RepeatKey = NewRepeatKey;
649 }
650
651 return EFI_SUCCESS;
652 }
653
654
655 /**
656 Retrieves a key character after parsing the raw data in keyboard buffer.
657
658 UsbKeyboardDevice The USB_KB_DEV instance.
659 KeyChar Points to the Key character after key parsing.
660
661 @retval EFI_SUCCESS Success
662 @retval EFI_NOT_READY Device is not ready
663
664 **/
665 EFI_STATUS
666 USBParseKey (
667 IN OUT USB_KB_DEV *UsbKeyboardDevice,
668 OUT UINT8 *KeyChar
669 )
670 {
671 USB_KEY UsbKey;
672
673 *KeyChar = 0;
674
675 while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {
676 //
677 // pops one raw data off.
678 //
679 RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
680
681 if (!UsbKey.Down) {
682 switch (UsbKey.KeyCode) {
683
684 case 0xe0:
685 case 0xe4:
686 UsbKeyboardDevice->CtrlOn = 0;
687 break;
688
689 case 0xe1:
690 case 0xe5:
691 UsbKeyboardDevice->ShiftOn = 0;
692 break;
693
694 case 0xe2:
695 case 0xe6:
696 UsbKeyboardDevice->AltOn = 0;
697 break;
698
699 default:
700 break;
701 }
702
703 continue;
704 }
705
706 //
707 // Analyzes key pressing situation
708 //
709 switch (UsbKey.KeyCode) {
710
711 case 0xe0:
712 case 0xe4:
713 UsbKeyboardDevice->CtrlOn = 1;
714 continue;
715 break;
716
717 case 0xe1:
718 case 0xe5:
719 UsbKeyboardDevice->ShiftOn = 1;
720 continue;
721 break;
722
723 case 0xe2:
724 case 0xe6:
725 UsbKeyboardDevice->AltOn = 1;
726 continue;
727 break;
728
729 case 0xe3:
730 case 0xe7:
731 continue;
732 break;
733
734 case 0x53:
735 UsbKeyboardDevice->NumLockOn ^= 1;
736 //
737 // Turn on the NumLock light on KB
738 //
739 SetKeyLED (UsbKeyboardDevice);
740 continue;
741 break;
742
743 case 0x39:
744 UsbKeyboardDevice->CapsOn ^= 1;
745 //
746 // Turn on the CapsLock light on KB
747 //
748 SetKeyLED (UsbKeyboardDevice);
749 continue;
750 break;
751
752 case 0x47:
753 UsbKeyboardDevice->ScrollOn ^= 1;
754 //
755 // Turn on the ScrollLock light on KB
756 //
757 SetKeyLED (UsbKeyboardDevice);
758 continue;
759 break;
760
761 //
762 // PrintScreen,Pause,Application,Power
763 // keys are not valid EFI key
764 //
765
766 case 0x46:
767 //
768 // fall through
769 //
770 case 0x48:
771 //
772 // fall through
773 //
774 case 0x65:
775 //
776 // fall through
777 //
778 case 0x66:
779 //
780 // fall through
781 //
782 continue;
783 break;
784
785 default:
786 break;
787 }
788
789 //
790 // When encountered Del Key...
791 //
792 if (UsbKey.KeyCode == 0x4c || UsbKey.KeyCode == 0x63) {
793 if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) {
794 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
795 }
796 }
797
798 *KeyChar = UsbKey.KeyCode;
799 return EFI_SUCCESS;
800 }
801
802 return EFI_NOT_READY;
803
804 }
805
806
807
808 /**
809 Converts USB Keyboard code to EFI Scan Code.
810
811 UsbKeyboardDevice The USB_KB_DEV instance.
812 KeyChar Indicates the key code that will be interpreted.
813 Key A pointer to a buffer that is filled in with
814 the keystroke information for the key that
815 was pressed.
816
817 @retval EFI_NOT_READY Device is not ready
818 @retval EFI_SUCCESS Success
819
820 **/
821 EFI_STATUS
822 USBKeyCodeToEFIScanCode (
823 IN USB_KB_DEV *UsbKeyboardDevice,
824 IN UINT8 KeyChar,
825 OUT EFI_INPUT_KEY *Key
826 )
827 {
828 UINT8 Index;
829
830 if (!USBKBD_VALID_KEYCODE (KeyChar)) {
831 return EFI_NOT_READY;
832 }
833
834 //
835 // valid USB Key Code starts from 4
836 //
837 Index = (UINT8) (KeyChar - 4);
838
839 if (Index >= USB_KEYCODE_MAX_MAKE) {
840 return EFI_NOT_READY;
841 }
842
843 Key->ScanCode = KeyConvertionTable[Index][0];
844
845 if (UsbKeyboardDevice->ShiftOn) {
846
847 Key->UnicodeChar = KeyConvertionTable[Index][2];
848
849 } else {
850
851 Key->UnicodeChar = KeyConvertionTable[Index][1];
852 }
853
854 if (UsbKeyboardDevice->CapsOn) {
855
856 if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {
857
858 Key->UnicodeChar = KeyConvertionTable[Index][2];
859
860 } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') {
861
862 Key->UnicodeChar = KeyConvertionTable[Index][1];
863
864 }
865 }
866
867 if (KeyChar >= 0x59 && KeyChar <= 0x63) {
868
869 if (UsbKeyboardDevice->NumLockOn && !UsbKeyboardDevice->ShiftOn) {
870
871 Key->ScanCode = SCAN_NULL;
872
873 } else {
874
875 Key->UnicodeChar = 0x00;
876 }
877 }
878
879 if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {
880 return EFI_NOT_READY;
881 }
882
883 return EFI_SUCCESS;
884
885 }
886
887
888
889 /**
890 Resets USB Keyboard Buffer.
891
892 @param KeyboardBuffer Points to the USB Keyboard Buffer.
893
894 @retval EFI_SUCCESS Success
895
896 **/
897 EFI_STATUS
898 InitUSBKeyBuffer (
899 IN OUT USB_KB_BUFFER *KeyboardBuffer
900 )
901 {
902 ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));
903
904 KeyboardBuffer->bHead = KeyboardBuffer->bTail;
905
906 return EFI_SUCCESS;
907 }
908
909
910 /**
911 Check whether USB Keyboard buffer is empty.
912
913 @param KeyboardBuffer USB Keyboard Buffer.
914
915
916 **/
917 BOOLEAN
918 IsUSBKeyboardBufferEmpty (
919 IN USB_KB_BUFFER *KeyboardBuffer
920 )
921 {
922 //
923 // meet FIFO empty condition
924 //
925 return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail);
926 }
927
928
929
930 /**
931 Check whether USB Keyboard buffer is full.
932
933 @param KeyboardBuffer USB Keyboard Buffer.
934
935
936 **/
937 BOOLEAN
938 IsUSBKeyboardBufferFull (
939 IN USB_KB_BUFFER *KeyboardBuffer
940 )
941 {
942 return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) ==
943 KeyboardBuffer->bHead);
944 }
945
946
947
948 /**
949 Inserts a key code into keyboard buffer.
950
951 @param KeyboardBuffer Points to the USB Keyboard Buffer.
952 @param Key Key code
953 @param Down Special key
954
955 @retval EFI_SUCCESS Success
956
957 **/
958 EFI_STATUS
959 InsertKeyCode (
960 IN OUT USB_KB_BUFFER *KeyboardBuffer,
961 IN UINT8 Key,
962 IN UINT8 Down
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
987 /**
988 Pops a key code off from keyboard buffer.
989
990 @param KeyboardBuffer Points to the USB Keyboard Buffer.
991 @param UsbKey Points to the buffer that contains a usb key code.
992
993 @retval EFI_SUCCESS Success
994 @retval EFI_DEVICE_ERROR Hardware Error
995
996 **/
997 EFI_STATUS
998 RemoveKeyCode (
999 IN OUT USB_KB_BUFFER *KeyboardBuffer,
1000 OUT USB_KEY *UsbKey
1001 )
1002 {
1003 if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {
1004 return EFI_DEVICE_ERROR;
1005 }
1006
1007 UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode;
1008 UsbKey->Down = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down;
1009
1010 //
1011 // adjust the head pointer of the FIFO keyboard buffer.
1012 //
1013 KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1));
1014
1015 return EFI_SUCCESS;
1016 }
1017
1018
1019 /**
1020 Sets USB Keyboard LED state.
1021
1022 @param UsbKeyboardDevice The USB_KB_DEV instance.
1023
1024 @retval EFI_SUCCESS Success
1025
1026 **/
1027 EFI_STATUS
1028 SetKeyLED (
1029 IN USB_KB_DEV *UsbKeyboardDevice
1030 )
1031 {
1032 LED_MAP Led;
1033 UINT8 ReportId;
1034
1035 //
1036 // Set each field in Led map.
1037 //
1038 Led.NumLock = (UINT8) UsbKeyboardDevice->NumLockOn;
1039 Led.CapsLock = (UINT8) UsbKeyboardDevice->CapsOn;
1040 Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn;
1041 Led.Resrvd = 0;
1042
1043 ReportId = 0;
1044 //
1045 // call Set Report Request to lighten the LED.
1046 //
1047 UsbSetReportRequest (
1048 UsbKeyboardDevice->UsbIo,
1049 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
1050 ReportId,
1051 HID_OUTPUT_REPORT,
1052 1,
1053 (UINT8 *) &Led
1054 );
1055
1056 return EFI_SUCCESS;
1057 }
1058
1059
1060 /**
1061 Timer handler for Repeat Key timer.
1062
1063 @param Event The Repeat Key event.
1064 @param Context Points to the USB_KB_DEV instance.
1065
1066
1067 **/
1068 VOID
1069 EFIAPI
1070 USBKeyboardRepeatHandler (
1071 IN EFI_EVENT Event,
1072 IN VOID *Context
1073 )
1074 {
1075 USB_KB_DEV *UsbKeyboardDevice;
1076
1077 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1078
1079 //
1080 // Do nothing when there is no repeat key.
1081 //
1082 if (UsbKeyboardDevice->RepeatKey != 0) {
1083 //
1084 // Inserts one Repeat key into keyboard buffer,
1085 //
1086 InsertKeyCode (
1087 &(UsbKeyboardDevice->KeyboardBuffer),
1088 UsbKeyboardDevice->RepeatKey,
1089 1
1090 );
1091
1092 //
1093 // set repeate rate for repeat key generation.
1094 //
1095 gBS->SetTimer (
1096 UsbKeyboardDevice->RepeatTimer,
1097 TimerRelative,
1098 USBKBD_REPEAT_RATE
1099 );
1100
1101 }
1102 }
1103
1104
1105 /**
1106 Timer handler for Delayed Recovery timer.
1107
1108 @param Event The Delayed Recovery event.
1109 @param Context Points to the USB_KB_DEV instance.
1110
1111
1112 **/
1113 VOID
1114 EFIAPI
1115 USBKeyboardRecoveryHandler (
1116 IN EFI_EVENT Event,
1117 IN VOID *Context
1118 )
1119 {
1120
1121 USB_KB_DEV *UsbKeyboardDevice;
1122 EFI_USB_IO_PROTOCOL *UsbIo;
1123 UINT8 PacketSize;
1124
1125 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1126
1127 UsbIo = UsbKeyboardDevice->UsbIo;
1128
1129 PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);
1130
1131 UsbIo->UsbAsyncInterruptTransfer (
1132 UsbIo,
1133 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
1134 TRUE,
1135 UsbKeyboardDevice->IntEndpointDescriptor.Interval,
1136 PacketSize,
1137 KeyboardHandler,
1138 UsbKeyboardDevice
1139 );
1140 }