]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbKbDxe/keyboard.c
44914afb541400a01e6e8fb0b5a2e028ef3a490b
[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
210 KbdReportStatusCode (
211 UsbKeyboardDevice->DevicePath,
212 EFI_PROGRESS_CODE,
213 PcdGet32 (PcdStatusCodeValueKeyboardSelfTest)
214 );
215
216 InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));
217
218 //
219 // default configurations
220 //
221 ConfigValue = 0x01;
222
223 //
224 // Uses default configuration to configure the USB Keyboard device.
225 //
226 Status = UsbSetConfiguration (
227 UsbKeyboardDevice->UsbIo,
228 (UINT16) ConfigValue,
229 &TransferResult
230 );
231 if (EFI_ERROR (Status)) {
232 //
233 // If configuration could not be set here, it means
234 // the keyboard interface has some errors and could
235 // not be initialized
236 //
237 KbdReportStatusCode (
238 UsbKeyboardDevice->DevicePath,
239 EFI_ERROR_CODE | EFI_ERROR_MINOR,
240 PcdGet32 (PcdStatusCodeValueKeyboardInterfaceError)
241 );
242
243 return EFI_DEVICE_ERROR;
244 }
245
246 UsbGetProtocolRequest (
247 UsbKeyboardDevice->UsbIo,
248 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
249 &Protocol
250 );
251 //
252 // Sets boot protocol for the USB Keyboard.
253 // This driver only supports boot protocol.
254 // !!BugBug: How about the device that does not support boot protocol?
255 //
256 if (Protocol != BOOT_PROTOCOL) {
257 UsbSetProtocolRequest (
258 UsbKeyboardDevice->UsbIo,
259 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
260 BOOT_PROTOCOL
261 );
262 }
263 //
264 // the duration is indefinite, so the endpoint will inhibit reporting forever,
265 // and only reporting when a change is detected in the report data.
266 //
267
268 //
269 // idle value for all report ID
270 //
271 ReportId = 0;
272 //
273 // idle forever until there is a key pressed and released.
274 //
275 Duration = 0;
276 UsbSetIdleRequest (
277 UsbKeyboardDevice->UsbIo,
278 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
279 ReportId,
280 Duration
281 );
282
283 UsbKeyboardDevice->CtrlOn = 0;
284 UsbKeyboardDevice->AltOn = 0;
285 UsbKeyboardDevice->ShiftOn = 0;
286 UsbKeyboardDevice->NumLockOn = 0;
287 UsbKeyboardDevice->CapsOn = 0;
288 UsbKeyboardDevice->ScrollOn = 0;
289
290 //
291 // Sync the initial state of lights
292 //
293 SetKeyLED (UsbKeyboardDevice);
294
295 ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);
296
297 //
298 // Set a timer for repeat keys' generation.
299 //
300 if (UsbKeyboardDevice->RepeatTimer) {
301 gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);
302 UsbKeyboardDevice->RepeatTimer = 0;
303 }
304
305 Status = gBS->CreateEvent (
306 EVT_TIMER | EVT_NOTIFY_SIGNAL,
307 TPL_NOTIFY,
308 USBKeyboardRepeatHandler,
309 UsbKeyboardDevice,
310 &UsbKeyboardDevice->RepeatTimer
311 );
312
313 if (UsbKeyboardDevice->DelayedRecoveryEvent) {
314 gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);
315 UsbKeyboardDevice->DelayedRecoveryEvent = 0;
316 }
317
318 Status = gBS->CreateEvent (
319 EVT_TIMER | EVT_NOTIFY_SIGNAL,
320 TPL_NOTIFY,
321 USBKeyboardRecoveryHandler,
322 UsbKeyboardDevice,
323 &UsbKeyboardDevice->DelayedRecoveryEvent
324 );
325
326 return EFI_SUCCESS;
327 }
328
329
330 /**
331 Handler function for USB Keyboard's asynchronous interrupt transfer.
332
333 Data A pointer to a buffer that is filled with key data which is
334 retrieved via asynchronous interrupt transfer.
335 DataLength Indicates the size of the data buffer.
336 Context Pointing to USB_KB_DEV instance.
337 Result Indicates the result of the asynchronous interrupt transfer.
338
339 @retval EFI_SUCCESS Success
340 @retval EFI_DEVICE_ERROR Hardware Error
341
342 **/
343 EFI_STATUS
344 EFIAPI
345 KeyboardHandler (
346 IN VOID *Data,
347 IN UINTN DataLength,
348 IN VOID *Context,
349 IN UINT32 Result
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 PcdGet32 (PcdStatusCodeValueKeyboardInputError)
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
649 /**
650 Retrieves a key character after parsing the raw data in keyboard buffer.
651
652 UsbKeyboardDevice The USB_KB_DEV instance.
653 KeyChar Points to the Key character after key parsing.
654
655 @retval EFI_SUCCESS Success
656 @retval EFI_NOT_READY Device is not ready
657
658 **/
659 EFI_STATUS
660 USBParseKey (
661 IN OUT USB_KB_DEV *UsbKeyboardDevice,
662 OUT UINT8 *KeyChar
663 )
664 {
665 USB_KEY UsbKey;
666
667 *KeyChar = 0;
668
669 while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {
670 //
671 // pops one raw data off.
672 //
673 RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
674
675 if (!UsbKey.Down) {
676 switch (UsbKey.KeyCode) {
677
678 case 0xe0:
679 case 0xe4:
680 UsbKeyboardDevice->CtrlOn = 0;
681 break;
682
683 case 0xe1:
684 case 0xe5:
685 UsbKeyboardDevice->ShiftOn = 0;
686 break;
687
688 case 0xe2:
689 case 0xe6:
690 UsbKeyboardDevice->AltOn = 0;
691 break;
692
693 default:
694 break;
695 }
696
697 continue;
698 }
699
700 //
701 // Analyzes key pressing situation
702 //
703 switch (UsbKey.KeyCode) {
704
705 case 0xe0:
706 case 0xe4:
707 UsbKeyboardDevice->CtrlOn = 1;
708 continue;
709 break;
710
711 case 0xe1:
712 case 0xe5:
713 UsbKeyboardDevice->ShiftOn = 1;
714 continue;
715 break;
716
717 case 0xe2:
718 case 0xe6:
719 UsbKeyboardDevice->AltOn = 1;
720 continue;
721 break;
722
723 case 0xe3:
724 case 0xe7:
725 continue;
726 break;
727
728 case 0x53:
729 UsbKeyboardDevice->NumLockOn ^= 1;
730 //
731 // Turn on the NumLock light on KB
732 //
733 SetKeyLED (UsbKeyboardDevice);
734 continue;
735 break;
736
737 case 0x39:
738 UsbKeyboardDevice->CapsOn ^= 1;
739 //
740 // Turn on the CapsLock light on KB
741 //
742 SetKeyLED (UsbKeyboardDevice);
743 continue;
744 break;
745
746 case 0x47:
747 UsbKeyboardDevice->ScrollOn ^= 1;
748 //
749 // Turn on the ScrollLock light on KB
750 //
751 SetKeyLED (UsbKeyboardDevice);
752 continue;
753 break;
754
755 //
756 // PrintScreen,Pause,Application,Power
757 // keys are not valid EFI key
758 //
759
760 case 0x46:
761 //
762 // fall through
763 //
764 case 0x48:
765 //
766 // fall through
767 //
768 case 0x65:
769 //
770 // fall through
771 //
772 case 0x66:
773 //
774 // fall through
775 //
776 continue;
777 break;
778
779 default:
780 break;
781 }
782
783 //
784 // When encountered Del Key...
785 //
786 if (UsbKey.KeyCode == 0x4c || UsbKey.KeyCode == 0x63) {
787 if (UsbKeyboardDevice->CtrlOn && UsbKeyboardDevice->AltOn) {
788 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
789 }
790 }
791
792 *KeyChar = UsbKey.KeyCode;
793 return EFI_SUCCESS;
794 }
795
796 return EFI_NOT_READY;
797
798 }
799
800
801
802 /**
803 Converts USB Keyboard code to EFI Scan Code.
804
805 UsbKeyboardDevice The USB_KB_DEV instance.
806 KeyChar Indicates the key code that will be interpreted.
807 Key A pointer to a buffer that is filled in with
808 the keystroke information for the key that
809 was pressed.
810
811 @retval EFI_NOT_READY Device is not ready
812 @retval EFI_SUCCESS Success
813
814 **/
815 EFI_STATUS
816 USBKeyCodeToEFIScanCode (
817 IN USB_KB_DEV *UsbKeyboardDevice,
818 IN UINT8 KeyChar,
819 OUT EFI_INPUT_KEY *Key
820 )
821 {
822 UINT8 Index;
823
824 if (!USBKBD_VALID_KEYCODE (KeyChar)) {
825 return EFI_NOT_READY;
826 }
827
828 //
829 // valid USB Key Code starts from 4
830 //
831 Index = (UINT8) (KeyChar - 4);
832
833 if (Index >= USB_KEYCODE_MAX_MAKE) {
834 return EFI_NOT_READY;
835 }
836
837 Key->ScanCode = KeyConvertionTable[Index][0];
838
839 if (UsbKeyboardDevice->ShiftOn) {
840
841 Key->UnicodeChar = KeyConvertionTable[Index][2];
842
843 } else {
844
845 Key->UnicodeChar = KeyConvertionTable[Index][1];
846 }
847
848 if (UsbKeyboardDevice->CapsOn) {
849
850 if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {
851
852 Key->UnicodeChar = KeyConvertionTable[Index][2];
853
854 } else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') {
855
856 Key->UnicodeChar = KeyConvertionTable[Index][1];
857
858 }
859 }
860
861 if (KeyChar >= 0x59 && KeyChar <= 0x63) {
862
863 if (UsbKeyboardDevice->NumLockOn && !UsbKeyboardDevice->ShiftOn) {
864
865 Key->ScanCode = SCAN_NULL;
866
867 } else {
868
869 Key->UnicodeChar = 0x00;
870 }
871 }
872
873 if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {
874 return EFI_NOT_READY;
875 }
876
877 return EFI_SUCCESS;
878
879 }
880
881
882
883 /**
884 Resets USB Keyboard Buffer.
885
886 @param KeyboardBuffer Points to the USB Keyboard Buffer.
887
888 @retval EFI_SUCCESS Success
889
890 **/
891 EFI_STATUS
892 InitUSBKeyBuffer (
893 IN OUT USB_KB_BUFFER *KeyboardBuffer
894 )
895 {
896 ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));
897
898 KeyboardBuffer->bHead = KeyboardBuffer->bTail;
899
900 return EFI_SUCCESS;
901 }
902
903
904 /**
905 Check whether USB Keyboard buffer is empty.
906
907 @param KeyboardBuffer USB Keyboard Buffer.
908
909
910 **/
911 BOOLEAN
912 IsUSBKeyboardBufferEmpty (
913 IN USB_KB_BUFFER *KeyboardBuffer
914 )
915 {
916 //
917 // meet FIFO empty condition
918 //
919 return (BOOLEAN) (KeyboardBuffer->bHead == KeyboardBuffer->bTail);
920 }
921
922
923
924 /**
925 Check whether USB Keyboard buffer is full.
926
927 @param KeyboardBuffer USB Keyboard Buffer.
928
929
930 **/
931 BOOLEAN
932 IsUSBKeyboardBufferFull (
933 IN USB_KB_BUFFER *KeyboardBuffer
934 )
935 {
936 return (BOOLEAN)(((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1)) ==
937 KeyboardBuffer->bHead);
938 }
939
940
941
942 /**
943 Inserts a key code into keyboard buffer.
944
945 @param KeyboardBuffer Points to the USB Keyboard Buffer.
946 @param Key Key code
947 @param Down Special key
948
949 @retval EFI_SUCCESS Success
950
951 **/
952 EFI_STATUS
953 InsertKeyCode (
954 IN OUT USB_KB_BUFFER *KeyboardBuffer,
955 IN UINT8 Key,
956 IN UINT8 Down
957 )
958 {
959 USB_KEY UsbKey;
960
961 //
962 // if keyboard buffer is full, throw the
963 // first key out of the keyboard buffer.
964 //
965 if (IsUSBKeyboardBufferFull (KeyboardBuffer)) {
966 RemoveKeyCode (KeyboardBuffer, &UsbKey);
967 }
968
969 KeyboardBuffer->buffer[KeyboardBuffer->bTail].KeyCode = Key;
970 KeyboardBuffer->buffer[KeyboardBuffer->bTail].Down = Down;
971
972 //
973 // adjust the tail pointer of the FIFO keyboard buffer.
974 //
975 KeyboardBuffer->bTail = (UINT8) ((KeyboardBuffer->bTail + 1) % (MAX_KEY_ALLOWED + 1));
976
977 return EFI_SUCCESS;
978 }
979
980
981 /**
982 Pops a key code off from keyboard buffer.
983
984 @param KeyboardBuffer Points to the USB Keyboard Buffer.
985 @param UsbKey Points to the buffer that contains a usb key code.
986
987 @retval EFI_SUCCESS Success
988 @retval EFI_DEVICE_ERROR Hardware Error
989
990 **/
991 EFI_STATUS
992 RemoveKeyCode (
993 IN OUT USB_KB_BUFFER *KeyboardBuffer,
994 OUT USB_KEY *UsbKey
995 )
996 {
997 if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {
998 return EFI_DEVICE_ERROR;
999 }
1000
1001 UsbKey->KeyCode = KeyboardBuffer->buffer[KeyboardBuffer->bHead].KeyCode;
1002 UsbKey->Down = KeyboardBuffer->buffer[KeyboardBuffer->bHead].Down;
1003
1004 //
1005 // adjust the head pointer of the FIFO keyboard buffer.
1006 //
1007 KeyboardBuffer->bHead = (UINT8) ((KeyboardBuffer->bHead + 1) % (MAX_KEY_ALLOWED + 1));
1008
1009 return EFI_SUCCESS;
1010 }
1011
1012
1013 /**
1014 Sets USB Keyboard LED state.
1015
1016 @param UsbKeyboardDevice The USB_KB_DEV instance.
1017
1018 @retval EFI_SUCCESS Success
1019
1020 **/
1021 EFI_STATUS
1022 SetKeyLED (
1023 IN USB_KB_DEV *UsbKeyboardDevice
1024 )
1025 {
1026 LED_MAP Led;
1027 UINT8 ReportId;
1028
1029 //
1030 // Set each field in Led map.
1031 //
1032 Led.NumLock = (UINT8) UsbKeyboardDevice->NumLockOn;
1033 Led.CapsLock = (UINT8) UsbKeyboardDevice->CapsOn;
1034 Led.ScrollLock = (UINT8) UsbKeyboardDevice->ScrollOn;
1035 Led.Resrvd = 0;
1036
1037 ReportId = 0;
1038 //
1039 // call Set Report Request to lighten the LED.
1040 //
1041 UsbSetReportRequest (
1042 UsbKeyboardDevice->UsbIo,
1043 UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
1044 ReportId,
1045 HID_OUTPUT_REPORT,
1046 1,
1047 (UINT8 *) &Led
1048 );
1049
1050 return EFI_SUCCESS;
1051 }
1052
1053
1054 /**
1055 Timer handler for Repeat Key timer.
1056
1057 @param Event The Repeat Key event.
1058 @param Context Points to the USB_KB_DEV instance.
1059
1060
1061 **/
1062 VOID
1063 EFIAPI
1064 USBKeyboardRepeatHandler (
1065 IN EFI_EVENT Event,
1066 IN VOID *Context
1067 )
1068 {
1069 USB_KB_DEV *UsbKeyboardDevice;
1070
1071 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1072
1073 //
1074 // Do nothing when there is no repeat key.
1075 //
1076 if (UsbKeyboardDevice->RepeatKey != 0) {
1077 //
1078 // Inserts one Repeat key into keyboard buffer,
1079 //
1080 InsertKeyCode (
1081 &(UsbKeyboardDevice->KeyboardBuffer),
1082 UsbKeyboardDevice->RepeatKey,
1083 1
1084 );
1085
1086 //
1087 // set repeate rate for repeat key generation.
1088 //
1089 gBS->SetTimer (
1090 UsbKeyboardDevice->RepeatTimer,
1091 TimerRelative,
1092 USBKBD_REPEAT_RATE
1093 );
1094
1095 }
1096 }
1097
1098
1099 /**
1100 Timer handler for Delayed Recovery timer.
1101
1102 @param Event The Delayed Recovery event.
1103 @param Context Points to the USB_KB_DEV instance.
1104
1105
1106 **/
1107 VOID
1108 EFIAPI
1109 USBKeyboardRecoveryHandler (
1110 IN EFI_EVENT Event,
1111 IN VOID *Context
1112 )
1113 {
1114
1115 USB_KB_DEV *UsbKeyboardDevice;
1116 EFI_USB_IO_PROTOCOL *UsbIo;
1117 UINT8 PacketSize;
1118
1119 UsbKeyboardDevice = (USB_KB_DEV *) Context;
1120
1121 UsbIo = UsbKeyboardDevice->UsbIo;
1122
1123 PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);
1124
1125 UsbIo->UsbAsyncInterruptTransfer (
1126 UsbIo,
1127 UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
1128 TRUE,
1129 UsbKeyboardDevice->IntEndpointDescriptor.Interval,
1130 PacketSize,
1131 KeyboardHandler,
1132 UsbKeyboardDevice
1133 );
1134 }