]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/Ps2KeyboardDxe/Ps2KbdCtrller.c
Change functional static variable to Global variable. No STATIC modifier is recommend...
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / Ps2KeyboardDxe / Ps2KbdCtrller.c
1 /**@file
2 PS/2 Keyboard driver
3 Routines that access 8042 keyboard controller
4
5 Copyright (c) 2006 - 2007, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Ps2Keyboard.h"
17
18 //
19 // Function declarations
20 //
21 UINT8
22 KeyReadDataRegister (
23 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
24 );
25
26 VOID
27 KeyWriteDataRegister (
28 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
29 IN UINT8 Data
30 );
31
32 VOID
33 KeyWriteCommandRegister (
34 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
35 IN UINT8 Data
36 );
37
38 VOID
39 KeyboardError (
40 IN KEYBOARD_CONSOLE_IN_DEV*ConsoleIn,
41 IN CHAR16 *ErrMsg // should be a unicode string
42 );
43
44 EFI_STATUS
45 GetScancodeBufHead (
46 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
47 IN UINT32 Count,
48 OUT UINT8 *Buf
49 );
50
51 EFI_STATUS
52 PopScancodeBufHead (
53 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
54 IN UINT32 Count,
55 OUT UINT8 *Buf
56 );
57
58 EFI_STATUS
59 KeyboardWrite (
60 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
61 IN UINT8 Data
62 );
63
64 EFI_STATUS
65 KeyboardCommand (
66 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
67 IN UINT8 Data
68 );
69
70 EFI_STATUS
71 KeyboardWaitForValue (
72 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
73 IN UINT8 Value
74 );
75
76 struct {
77 UINT8 ScanCode;
78 UINT16 EfiScanCode;
79 CHAR16 UnicodeChar;
80 CHAR16 ShiftUnicodeChar;
81 }
82 ConvertKeyboardScanCodeToEfiKey[] = {
83
84 {
85 0x01, // Escape
86 SCAN_ESC,
87 0x00,
88 0x00
89 },
90 {
91 0x02,
92 SCAN_NULL,
93 '1',
94 '!'
95 },
96 {
97 0x03,
98 SCAN_NULL,
99 '2',
100 '@'
101 },
102 {
103 0x04,
104 SCAN_NULL,
105 '3',
106 '#'
107 },
108 {
109 0x05,
110 SCAN_NULL,
111 '4',
112 '$'
113 },
114 {
115 0x06,
116 SCAN_NULL,
117 '5',
118 '%'
119 },
120 {
121 0x07,
122 SCAN_NULL,
123 '6',
124 '^'
125 },
126 {
127 0x08,
128 SCAN_NULL,
129 '7',
130 '&'
131 },
132 {
133 0x09,
134 SCAN_NULL,
135 '8',
136 '*'
137 },
138 {
139 0x0A,
140 SCAN_NULL,
141 '9',
142 '('
143 },
144 {
145 0x0B,
146 SCAN_NULL,
147 '0',
148 ')'
149 },
150 {
151 0x0C,
152 SCAN_NULL,
153 '-',
154 '_'
155 },
156 {
157 0x0D,
158 SCAN_NULL,
159 '=',
160 '+'
161 },
162 {
163 0x0E, // BackSpace
164 SCAN_NULL,
165 0x08,
166 0x08
167 },
168 {
169 0x0F, // Tab
170 SCAN_NULL,
171 0x09,
172 0x09
173 },
174 {
175 0x10,
176 SCAN_NULL,
177 'q',
178 'Q'
179 },
180 {
181 0x11,
182 SCAN_NULL,
183 'w',
184 'W'
185 },
186 {
187 0x12,
188 SCAN_NULL,
189 'e',
190 'E'
191 },
192 {
193 0x13,
194 SCAN_NULL,
195 'r',
196 'R'
197 },
198 {
199 0x14,
200 SCAN_NULL,
201 't',
202 'T'
203 },
204 {
205 0x15,
206 SCAN_NULL,
207 'y',
208 'Y'
209 },
210 {
211 0x16,
212 SCAN_NULL,
213 'u',
214 'U'
215 },
216 {
217 0x17,
218 SCAN_NULL,
219 'i',
220 'I'
221 },
222 {
223 0x18,
224 SCAN_NULL,
225 'o',
226 'O'
227 },
228 {
229 0x19,
230 SCAN_NULL,
231 'p',
232 'P'
233 },
234 {
235 0x1a,
236 SCAN_NULL,
237 '[',
238 '{'
239 },
240 {
241 0x1b,
242 SCAN_NULL,
243 ']',
244 '}'
245 },
246 {
247 0x1c, // Enter
248 SCAN_NULL,
249 0x0d,
250 0x0d
251 },
252 {
253 0x1d,
254 SCAN_NULL,
255 0x00,
256 0x00
257 },
258 {
259 0x1e,
260 SCAN_NULL,
261 'a',
262 'A'
263 },
264 {
265 0x1f,
266 SCAN_NULL,
267 's',
268 'S'
269 },
270 {
271 0x20,
272 SCAN_NULL,
273 'd',
274 'D'
275 },
276 {
277 0x21,
278 SCAN_NULL,
279 'f',
280 'F'
281 },
282 {
283 0x22,
284 SCAN_NULL,
285 'g',
286 'G'
287 },
288 {
289 0x23,
290 SCAN_NULL,
291 'h',
292 'H'
293 },
294 {
295 0x24,
296 SCAN_NULL,
297 'j',
298 'J'
299 },
300 {
301 0x25,
302 SCAN_NULL,
303 'k',
304 'K'
305 },
306 {
307 0x26,
308 SCAN_NULL,
309 'l',
310 'L'
311 },
312 {
313 0x27,
314 SCAN_NULL,
315 ';',
316 ':'
317 },
318 {
319 0x28,
320 SCAN_NULL,
321 '\'',
322 '"'
323 },
324 {
325 0x29,
326 SCAN_NULL,
327 '`',
328 '~'
329 },
330 {
331 0x2a, // Left Shift
332 SCAN_NULL,
333 0x00,
334 0x00
335 },
336 {
337 0x2b,
338 SCAN_NULL,
339 '\\',
340 '|'
341 },
342 {
343 0x2c,
344 SCAN_NULL,
345 'z',
346 'Z'
347 },
348 {
349 0x2d,
350 SCAN_NULL,
351 'x',
352 'X'
353 },
354 {
355 0x2e,
356 SCAN_NULL,
357 'c',
358 'C'
359 },
360 {
361 0x2f,
362 SCAN_NULL,
363 'v',
364 'V'
365 },
366 {
367 0x30,
368 SCAN_NULL,
369 'b',
370 'B'
371 },
372 {
373 0x31,
374 SCAN_NULL,
375 'n',
376 'N'
377 },
378 {
379 0x32,
380 SCAN_NULL,
381 'm',
382 'M'
383 },
384 {
385 0x33,
386 SCAN_NULL,
387 ',',
388 '<'
389 },
390 {
391 0x34,
392 SCAN_NULL,
393 '.',
394 '>'
395 },
396 {
397 0x35,
398 SCAN_NULL,
399 '/',
400 '?'
401 },
402 {
403 0x36, //Right Shift
404 SCAN_NULL,
405 0x00,
406 0x00
407 },
408 {
409 0x37, // Numeric Keypad *
410 SCAN_NULL,
411 '*',
412 '*'
413 },
414 {
415 0x38, //Left Alt/Extended Right Alt
416 SCAN_NULL,
417 0x00,
418 0x00
419 },
420 {
421 0x39,
422 SCAN_NULL,
423 ' ',
424 ' '
425 },
426 {
427 0x3A, //CapsLock
428 SCAN_NULL,
429 0x00,
430 0x00
431 },
432 {
433 0x3B,
434 SCAN_F1,
435 0x00,
436 0x00
437 },
438 {
439 0x3C,
440 SCAN_F2,
441 0x00,
442 0x00
443 },
444 {
445 0x3D,
446 SCAN_F3,
447 0x00,
448 0x00
449 },
450 {
451 0x3E,
452 SCAN_F4,
453 0x00,
454 0x00
455 },
456 {
457 0x3F,
458 SCAN_F5,
459 0x00,
460 0x00
461 },
462 {
463 0x40,
464 SCAN_F6,
465 0x00,
466 0x00
467 },
468 {
469 0x41,
470 SCAN_F7,
471 0x00,
472 0x00
473 },
474 {
475 0x42,
476 SCAN_F8,
477 0x00,
478 0x00
479 },
480 {
481 0x43,
482 SCAN_F9,
483 0x00,
484 0x00
485 },
486 {
487 0x44,
488 SCAN_F10,
489 0x00,
490 0x00
491 },
492 {
493 0x45, // NumLock
494 SCAN_NULL,
495 0x00,
496 0x00
497 },
498 {
499 0x46, // ScrollLock
500 SCAN_NULL,
501 0x00,
502 0x00
503 },
504 {
505 0x47,
506 SCAN_HOME,
507 '7',
508 '7'
509 },
510 {
511 0x48,
512 SCAN_UP,
513 '8',
514 '8'
515 },
516 {
517 0x49,
518 SCAN_PAGE_UP,
519 '9',
520 '9'
521 },
522 {
523 0x4a,
524 SCAN_NULL,
525 '-',
526 '-'
527 },
528 {
529 0x4b,
530 SCAN_LEFT,
531 '4',
532 '4'
533 },
534 {
535 0x4c, // Numeric Keypad 5
536 SCAN_NULL,
537 '5',
538 '5'
539 },
540 {
541 0x4d,
542 SCAN_RIGHT,
543 '6',
544 '6'
545 },
546 {
547 0x4e,
548 SCAN_NULL,
549 '+',
550 '+'
551 },
552 {
553 0x4f,
554 SCAN_END,
555 '1',
556 '1'
557 },
558 {
559 0x50,
560 SCAN_DOWN,
561 '2',
562 '2'
563 },
564 {
565 0x51,
566 SCAN_PAGE_DOWN,
567 '3',
568 '3'
569 },
570 {
571 0x52,
572 SCAN_INSERT,
573 '0',
574 '0'
575 },
576 {
577 0x53,
578 SCAN_DELETE,
579 '.',
580 '.'
581 },
582 {
583 0x57,
584 SCAN_F11,
585 0x00,
586 0x00
587 },
588 {
589 0x58,
590 SCAN_F12,
591 0x00,
592 0x00
593 },
594 {
595 0x5B, //Left LOGO
596 SCAN_NULL,
597 0x00,
598 0x00
599 },
600 {
601 0x5C, //Right LOGO
602 SCAN_NULL,
603 0x00,
604 0x00
605 },
606 {
607 0x5D, //Menu key
608 SCAN_NULL,
609 0x00,
610 0x00
611 },
612 {
613 TABLE_END,
614 TABLE_END,
615 SCAN_NULL,
616 SCAN_NULL
617 },
618 };
619
620
621 //
622 // The WaitForValue time out
623 //
624 UINTN mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;
625
626 BOOLEAN mEnableMouseInterface;
627
628 /**
629 Read data register
630
631 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
632
633 @return return the value
634
635 **/
636 UINT8
637 KeyReadDataRegister (
638 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
639 )
640
641 {
642 EFI_ISA_IO_PROTOCOL *IsaIo;
643 UINT8 Data;
644
645 //
646 // Use IsaIo protocol to perform IO operations
647 //
648 IsaIo = ConsoleIn->IsaIo;
649
650 IsaIo->Io.Read (
651 IsaIo,
652 EfiIsaIoWidthUint8,
653 ConsoleIn->DataRegisterAddress,
654 1,
655 &Data
656 );
657
658 return Data;
659 }
660
661 /**
662 Write data register
663
664 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
665 @param Data value wanted to be written
666
667 **/
668 VOID
669 KeyWriteDataRegister (
670 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
671 IN UINT8 Data
672 )
673
674 {
675 EFI_ISA_IO_PROTOCOL *IsaIo;
676
677 //
678 // Use IsaIo protocol to perform IO operations
679 //
680 IsaIo = ConsoleIn->IsaIo;
681
682 IsaIo->Io.Write (
683 IsaIo,
684 EfiIsaIoWidthUint8,
685 ConsoleIn->DataRegisterAddress,
686 1,
687 &Data
688 );
689
690 //
691 // outp(ConsoleIn->DataRegisterAddress, Data);
692 //
693 }
694
695 /**
696 Read status register
697
698 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
699
700 @return value in status register
701
702 **/
703 UINT8
704 KeyReadStatusRegister (
705 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
706 )
707 {
708 EFI_ISA_IO_PROTOCOL *IsaIo;
709 UINT8 Data;
710
711 //
712 // Use IsaIo protocol to perform IO operations
713 //
714 IsaIo = ConsoleIn->IsaIo;
715
716 IsaIo->Io.Read (
717 IsaIo,
718 EfiIsaIoWidthUint8,
719 ConsoleIn->StatusRegisterAddress,
720 1,
721 &Data
722 );
723
724 return Data;
725
726 }
727
728 /**
729 Write command register
730
731 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
732 @param Data The value wanted to be written
733
734 **/
735
736 VOID
737 KeyWriteCommandRegister (
738 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
739 IN UINT8 Data
740 )
741 {
742 EFI_ISA_IO_PROTOCOL *IsaIo;
743
744 //
745 // Use IsaIo protocol to perform IO operations
746 //
747 IsaIo = ConsoleIn->IsaIo;
748
749 IsaIo->Io.Write (
750 IsaIo,
751 EfiIsaIoWidthUint8,
752 ConsoleIn->CommandRegisterAddress,
753 1,
754 &Data
755 );
756
757 }
758
759 /**
760 Display error message
761
762 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
763 @param ErrMsg Unicode string of error message
764
765 **/
766 VOID
767 KeyboardError (
768 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
769 IN CHAR16 *ErrMsg
770 )
771 {
772 ConsoleIn->KeyboardErr = TRUE;
773
774 //
775 // gST -> ConOut -> OutputString (gST -> ConOut, L"Keyboard Driver: ");
776 // gST -> ConOut -> OutputString (gST -> ConOut, ErrMsg);
777 //
778 }
779
780 /**
781 Timer event handler: read a series of scancodes from 8042
782 and put them into memory scancode buffer.
783 it read as much scancodes to either fill
784 the memory buffer or empty the keyboard buffer.
785 It is registered as running under TPL_NOTIFY
786
787 @param Event - The timer event
788 @param Context - A KEYBOARD_CONSOLE_IN_DEV pointer
789
790 **/
791 VOID
792 EFIAPI
793 KeyboardTimerHandler (
794 IN EFI_EVENT Event,
795 IN VOID *Context
796 )
797
798 {
799 UINT8 Data;
800 EFI_TPL OldTpl;
801 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn;
802
803 ConsoleIn = Context;
804
805 //
806 // Enter critical section
807 //
808 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
809
810 if (((KEYBOARD_CONSOLE_IN_DEV *) Context)->KeyboardErr) {
811 //
812 // Leave critical section and return
813 //
814 gBS->RestoreTPL (OldTpl);
815 return ;
816 }
817 //
818 // To let KB driver support Hot plug, here should skip the 'resend' command for the case that
819 // KB is not connected to system. If KB is not connected to system, driver will find there's something
820 // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since
821 // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.
822 // Just skip the 'resend' process simply.
823 //
824
825 Data = 0;
826
827 //
828 // if there is no key present, just return
829 //
830 if ((KeyReadStatusRegister (Context) & 0x21) != 0x1) {
831 //
832 // Leave critical section and return
833 //
834 gBS->RestoreTPL (OldTpl);
835
836 return ;
837 }
838 //
839 // Read one byte of the scan code and store it into the memory buffer
840 //
841 if (ConsoleIn->ScancodeBufCount < KEYBOARD_BUFFER_MAX_COUNT) {
842
843 Data = KeyReadDataRegister (Context);
844 //
845 // put the scancode into the memory scancode buffer
846 //
847 ConsoleIn->ScancodeBufCount++;
848 ConsoleIn->ScancodeBufEndPos++;
849 if (ConsoleIn->ScancodeBufEndPos >= KEYBOARD_BUFFER_MAX_COUNT) {
850 ConsoleIn->ScancodeBufEndPos = 0;
851 }
852
853 ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufEndPos] = Data;
854
855 //
856 // Handle Alt+Ctrl+Del Key combination
857 //
858 switch (Data) {
859 case SCANCODE_CTRL_MAKE:
860 ConsoleIn->Ctrled = TRUE;
861 break;
862
863 case SCANCODE_CTRL_BREAK:
864 ConsoleIn->Ctrled = FALSE;
865 break;
866
867 case SCANCODE_ALT_MAKE:
868 ConsoleIn->Alted = TRUE;
869 break;
870
871 case SCANCODE_ALT_BREAK:
872 ConsoleIn->Alted = FALSE;
873 break;
874 }
875 //
876 // if Alt+Ctrl+Del, Reboot the System
877 //
878 if (ConsoleIn->Ctrled && ConsoleIn->Alted && Data == 0x53) {
879 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
880 }
881 }
882 //
883 // Leave critical section and return
884 //
885 gBS->RestoreTPL (OldTpl);
886
887 return ;
888 }
889
890 /**
891 Read several bytes from the scancode buffer without removing them.
892 This function is called to see if there are enough bytes of scancode
893 representing a single key.
894
895 @param Count - Number of bytes to be read
896 @param Buf - Store the results
897
898 @retval EFI_SUCCESS success to scan the keyboard code
899 @retval EFI_NOT_READY invalid parameter
900 **/
901 EFI_STATUS
902 GetScancodeBufHead (
903 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
904 IN UINT32 Count,
905 OUT UINT8 *Buf
906 )
907 {
908 UINT32 Index;
909 UINT32 Pos;
910
911 Index = 0;
912 Pos = 0;
913
914 //
915 // check the valid range of parameter 'Count'
916 //
917 if (Count <= 0 || ConsoleIn->ScancodeBufCount < Count) {
918 return EFI_NOT_READY;
919 }
920 //
921 // retrieve the values
922 //
923 for (Index = 0; Index < Count; Index++) {
924
925 if (Index == 0) {
926
927 Pos = ConsoleIn->ScancodeBufStartPos;
928 } else {
929
930 Pos = Pos + 1;
931 if (Pos >= KEYBOARD_BUFFER_MAX_COUNT) {
932 Pos = 0;
933 }
934 }
935
936 Buf[Index] = ConsoleIn->ScancodeBuf[Pos];
937 }
938
939 return EFI_SUCCESS;
940 }
941
942 /**
943
944 Read & remove several bytes from the scancode buffer.
945 This function is usually called after GetScancodeBufHead()
946
947 @param Count - Number of bytes to be read
948 @param Buf - Store the results
949
950 @retval EFI_SUCCESS success to scan the keyboard code
951 @retval EFI_NOT_READY invalid parameter
952 **/
953 EFI_STATUS
954 PopScancodeBufHead (
955 KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
956 IN UINT32 Count,
957 OUT UINT8 *Buf
958 )
959 {
960 UINT32 Index;
961
962 Index = 0;
963
964 //
965 // Check the valid range of parameter 'Count'
966 //
967 if (Count <= 0 || ConsoleIn->ScancodeBufCount < Count) {
968 return EFI_NOT_READY;
969 }
970 //
971 // Retrieve and remove the values
972 //
973 for (Index = 0; Index < Count; Index++) {
974
975 if (Index != 0) {
976
977 ConsoleIn->ScancodeBufStartPos++;
978 if (ConsoleIn->ScancodeBufStartPos >= KEYBOARD_BUFFER_MAX_COUNT) {
979 ConsoleIn->ScancodeBufStartPos = 0;
980 }
981 }
982
983 Buf[Index] = ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufStartPos];
984 ConsoleIn->ScancodeBufCount--;
985 }
986
987 ConsoleIn->ScancodeBufStartPos++;
988 if (ConsoleIn->ScancodeBufStartPos >= KEYBOARD_BUFFER_MAX_COUNT) {
989 ConsoleIn->ScancodeBufStartPos = 0;
990 }
991
992 return EFI_SUCCESS;
993 }
994
995 /**
996 Read key value
997
998 @param ConsoleIn - Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
999 @param Data - Pointer to outof buffer for keeping key value
1000
1001 @retval EFI_TIMEOUT Status resigter time out
1002 @retval EFI_SUCCESS Success to read keyboard
1003
1004 **/
1005 EFI_STATUS
1006 KeyboardRead (
1007 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
1008 OUT UINT8 *Data
1009 )
1010
1011 {
1012 UINT32 TimeOut;
1013 UINT32 RegFilled;
1014
1015 TimeOut = 0;
1016 RegFilled = 0;
1017
1018 //
1019 // wait till output buffer full then perform the read
1020 //
1021 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {
1022 if (KeyReadStatusRegister (ConsoleIn) & 0x01) {
1023 RegFilled = 1;
1024 *Data = KeyReadDataRegister (ConsoleIn);
1025 break;
1026 }
1027
1028 gBS->Stall (30);
1029 }
1030
1031 if (!RegFilled) {
1032 return EFI_TIMEOUT;
1033 }
1034
1035 return EFI_SUCCESS;
1036 }
1037
1038 /**
1039 write key to keyboard
1040
1041 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1042 @param Data value wanted to be written
1043
1044 @retval EFI_TIMEOUT - GC_TODO: Add description for return value
1045 @retval EFI_SUCCESS - GC_TODO: Add description for return value
1046
1047 **/
1048 EFI_STATUS
1049 KeyboardWrite (
1050 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
1051 IN UINT8 Data
1052 )
1053 {
1054 UINT32 TimeOut;
1055 UINT32 RegEmptied;
1056
1057 TimeOut = 0;
1058 RegEmptied = 0;
1059
1060 //
1061 // wait for input buffer empty
1062 //
1063 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {
1064 if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {
1065 RegEmptied = 1;
1066 break;
1067 }
1068
1069 gBS->Stall (30);
1070 }
1071
1072 if (!RegEmptied) {
1073 return EFI_TIMEOUT;
1074 }
1075 //
1076 // Write it
1077 //
1078 KeyWriteDataRegister (ConsoleIn, Data);
1079
1080 return EFI_SUCCESS;
1081 }
1082
1083 /**
1084 Issue keyboard command
1085
1086 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1087 @param Data The buff holding the command
1088
1089 @retval EFI_TIMEOUT Keyboard is not ready to issuing
1090 @retval EFI_SUCCESS Success to issue keyboard command
1091
1092 **/
1093 EFI_STATUS
1094 KeyboardCommand (
1095 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
1096 IN UINT8 Data
1097 )
1098 {
1099 UINT32 TimeOut;
1100 UINT32 RegEmptied;
1101
1102 TimeOut = 0;
1103 RegEmptied = 0;
1104
1105 //
1106 // Wait For Input Buffer Empty
1107 //
1108 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {
1109 if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {
1110 RegEmptied = 1;
1111 break;
1112 }
1113
1114 gBS->Stall (30);
1115 }
1116
1117 if (!RegEmptied) {
1118 return EFI_TIMEOUT;
1119 }
1120 //
1121 // issue the command
1122 //
1123 KeyWriteCommandRegister (ConsoleIn, Data);
1124
1125 //
1126 // Wait For Input Buffer Empty again
1127 //
1128 RegEmptied = 0;
1129 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {
1130 if (!(KeyReadStatusRegister (ConsoleIn) & 0x02)) {
1131 RegEmptied = 1;
1132 break;
1133 }
1134
1135 gBS->Stall (30);
1136 }
1137
1138 if (!RegEmptied) {
1139 return EFI_TIMEOUT;
1140 }
1141
1142 return EFI_SUCCESS;
1143 }
1144
1145 /**
1146 wait for a specific value to be presented on
1147 8042 Data register by keyboard and then read it,
1148 used in keyboard commands ack
1149
1150 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1151 @param Value the value wanted to be waited.
1152
1153 @retval EFI_TIMEOUT Fail to get specific value in given time
1154 @retval EFI_SUCCESS Success to get specific value in given time.
1155
1156 **/
1157 EFI_STATUS
1158 KeyboardWaitForValue (
1159 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
1160 IN UINT8 Value
1161 )
1162 {
1163 UINT8 Data;
1164 UINT32 TimeOut;
1165 UINT32 SumTimeOut;
1166 UINT32 GotIt;
1167
1168 GotIt = 0;
1169 TimeOut = 0;
1170 SumTimeOut = 0;
1171
1172 //
1173 // Make sure the initial value of 'Data' is different from 'Value'
1174 //
1175 Data = 0;
1176 if (Data == Value) {
1177 Data = 1;
1178 }
1179 //
1180 // Read from 8042 (multiple times if needed)
1181 // until the expected value appears
1182 // use SumTimeOut to control the iteration
1183 //
1184 while (1) {
1185 //
1186 // Perform a read
1187 //
1188 for (TimeOut = 0; TimeOut < KEYBOARD_TIMEOUT; TimeOut += 30) {
1189 if (KeyReadStatusRegister (ConsoleIn) & 0x01) {
1190 Data = KeyReadDataRegister (ConsoleIn);
1191 break;
1192 }
1193
1194 gBS->Stall (30);
1195 }
1196
1197 SumTimeOut += TimeOut;
1198
1199 if (Data == Value) {
1200 GotIt = 1;
1201 break;
1202 }
1203
1204 if (SumTimeOut >= mWaitForValueTimeOut) {
1205 break;
1206 }
1207 }
1208 //
1209 // Check results
1210 //
1211 if (GotIt) {
1212 return EFI_SUCCESS;
1213 } else {
1214 return EFI_TIMEOUT;
1215 }
1216
1217 }
1218
1219 /**
1220 Show keyboard status lights according to
1221 indicators in ConsoleIn.
1222
1223 @param ConsoleIn Pointer to instance of KEYBOARD_CONSOLE_IN_DEV
1224
1225 @return status
1226
1227 **/
1228 EFI_STATUS
1229 UpdateStatusLights (
1230 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
1231 )
1232 {
1233 EFI_STATUS Status;
1234 UINT8 Command;
1235
1236 //
1237 // Send keyboard command
1238 //
1239 Status = KeyboardWrite (ConsoleIn, 0xed);
1240 if (EFI_ERROR (Status)) {
1241 return Status;
1242 }
1243
1244 KeyboardWaitForValue (ConsoleIn, 0xfa);
1245
1246 //
1247 // Light configuration
1248 //
1249 Command = 0;
1250 if (ConsoleIn->CapsLock) {
1251 Command |= 4;
1252 }
1253
1254 if (ConsoleIn->NumLock) {
1255 Command |= 2;
1256 }
1257
1258 if (ConsoleIn->ScrollLock) {
1259 Command |= 1;
1260 }
1261
1262 Status = KeyboardWrite (ConsoleIn, Command);
1263
1264 if (EFI_ERROR (Status)) {
1265 return Status;
1266 }
1267
1268 KeyboardWaitForValue (ConsoleIn, 0xfa);
1269 return Status;
1270 }
1271
1272 /**
1273 Get scancode from scancode buffer
1274 and translate into EFI-scancode and unicode defined by EFI spec
1275 The function is always called in TPL_NOTIFY
1276
1277 @param ConsoleIn KEYBOARD_CONSOLE_IN_DEV instance pointer
1278
1279 @retval EFI_NOT_READY - Input from console not ready yet.
1280 @retval EFI_SUCCESS - Function executed successfully.
1281
1282 **/
1283 EFI_STATUS
1284 KeyGetchar (
1285 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
1286 )
1287 {
1288 EFI_STATUS Status;
1289 UINT8 ScanCode;
1290 UINT8 Readed;
1291 BOOLEAN Extended;
1292 UINT8 ScancodeArr[4];
1293 UINTN Index;
1294 //
1295 // 4 bytes most
1296 //
1297 UINT32 ScancodeArrPos;
1298 //
1299 // point to the current position in ScancodeArr
1300 //
1301
1302 Readed = 0;
1303 Extended = FALSE;
1304 ScancodeArrPos = 0;
1305
1306 //
1307 // Read one byte of the scan code and store it into the memory buffer
1308 // This block of code is added to insert an action that is equivalent to
1309 // the timer event handling function, so as to increase the frequency of
1310 // detecting the availability of keys. Timer event has a max frequency of
1311 // 18Hz which is insufficient
1312 //
1313 //
1314 // To let KB driver support Hot plug, here should skip the 'resend' command for the case that
1315 // KB is not connected to system. If KB is not connected to system, driver will find there's something
1316 // error in the following code and wait for the input buffer empty, this waiting time shoulb be short enough since
1317 // this is a NOTIFY TPL period function, or the system performance will degrade hardly when KB is not connected.
1318 // Just skip the 'resend' process simply.
1319 //
1320
1321
1322 if (((KeyReadStatusRegister (ConsoleIn) & 0x21) == 0x1) && (ConsoleIn->ScancodeBufCount < KEYBOARD_BUFFER_MAX_COUNT)) {
1323
1324 Readed = KeyReadDataRegister (ConsoleIn);
1325 //
1326 // put the scancode into the memory scancode buffer
1327 //
1328 ConsoleIn->ScancodeBufCount++;
1329 ConsoleIn->ScancodeBufEndPos++;
1330 if (ConsoleIn->ScancodeBufEndPos >= KEYBOARD_BUFFER_MAX_COUNT) {
1331 ConsoleIn->ScancodeBufEndPos = 0;
1332 }
1333
1334 ConsoleIn->ScancodeBuf[ConsoleIn->ScancodeBufEndPos] = Readed;
1335
1336 //
1337 // Handle Alt+Ctrl+Del Key combination
1338 //
1339 switch (Readed) {
1340
1341 case SCANCODE_CTRL_MAKE:
1342 ConsoleIn->Ctrled = TRUE;
1343 break;
1344
1345 case SCANCODE_CTRL_BREAK:
1346 ConsoleIn->Ctrled = FALSE;
1347 break;
1348
1349 case SCANCODE_ALT_MAKE:
1350 ConsoleIn->Alted = TRUE;
1351 break;
1352
1353 case SCANCODE_ALT_BREAK:
1354 ConsoleIn->Alted = FALSE;
1355 break;
1356 }
1357 //
1358 // if Alt+Ctrl+Del, Reboot the System
1359 //
1360 if (ConsoleIn->Ctrled && ConsoleIn->Alted && Readed == 0x53) {
1361 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
1362 }
1363 }
1364 //
1365 // Check if there are enough bytes of scancode representing a single key
1366 // available in the buffer
1367 //
1368 while (1) {
1369
1370 Status = GetScancodeBufHead (ConsoleIn, 1, ScancodeArr);
1371 ScancodeArrPos = 0;
1372 if (EFI_ERROR (Status)) {
1373 return EFI_NOT_READY;
1374 }
1375
1376 if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED) {
1377 Extended = TRUE;
1378 Status = GetScancodeBufHead (ConsoleIn, 2, ScancodeArr);
1379 ScancodeArrPos = 1;
1380 if (EFI_ERROR (Status)) {
1381 return EFI_NOT_READY;
1382 }
1383 }
1384 //
1385 // Checks for key scancode for PAUSE:E1-1D/45-E1/9D-C5
1386 // if present, ignore them
1387 //
1388 if (ScancodeArr[ScancodeArrPos] == SCANCODE_EXTENDED1) {
1389
1390 Status = GetScancodeBufHead (ConsoleIn, 2, ScancodeArr);
1391 ScancodeArrPos = 1;
1392
1393 if (EFI_ERROR (Status)) {
1394 return EFI_NOT_READY;
1395 }
1396
1397 Status = GetScancodeBufHead (ConsoleIn, 3, ScancodeArr);
1398 ScancodeArrPos = 2;
1399
1400 if (EFI_ERROR (Status)) {
1401 return EFI_NOT_READY;
1402 }
1403
1404 PopScancodeBufHead (ConsoleIn, 3, ScancodeArr);
1405 return EFI_NOT_READY;
1406 }
1407 //
1408 // if we reach this position, scancodes for a key is in buffer now,pop them
1409 //
1410 Status = PopScancodeBufHead (ConsoleIn, ScancodeArrPos + 1, ScancodeArr);
1411 if (EFI_ERROR (Status)) {
1412 return EFI_NOT_READY;
1413 }
1414 //
1415 // store the last available byte, this byte of scancode will be checked
1416 //
1417 ScanCode = ScancodeArr[ScancodeArrPos];
1418
1419 //
1420 // Check for special keys and update the driver state.
1421 //
1422 switch (ScanCode) {
1423
1424 case SCANCODE_CTRL_MAKE:
1425 ConsoleIn->Ctrl = TRUE;
1426 break;
1427
1428 case SCANCODE_CTRL_BREAK:
1429 ConsoleIn->Ctrl = FALSE;
1430 break;
1431
1432 case SCANCODE_ALT_MAKE:
1433 ConsoleIn->Alt = TRUE;
1434 break;
1435
1436 case SCANCODE_ALT_BREAK:
1437 ConsoleIn->Alt = FALSE;
1438 break;
1439
1440 case SCANCODE_LEFT_SHIFT_MAKE:
1441 if (!Extended) {
1442 ConsoleIn->Shift = TRUE;
1443 ConsoleIn->LeftShift = TRUE;
1444 }
1445 break;
1446 case SCANCODE_RIGHT_SHIFT_MAKE:
1447 if (!Extended) {
1448 ConsoleIn->Shift = TRUE;
1449 ConsoleIn->RightShift = TRUE;
1450 }
1451 break;
1452
1453 case SCANCODE_LEFT_SHIFT_BREAK:
1454 if (!Extended) {
1455 ConsoleIn->Shift = FALSE;
1456 ConsoleIn->LeftShift = FALSE;
1457 } else {
1458 ConsoleIn->SysReq = FALSE;
1459 }
1460 break;
1461 case SCANCODE_RIGHT_SHIFT_BREAK:
1462 if (!Extended) {
1463 ConsoleIn->Shift = FALSE;
1464 ConsoleIn->RightShift = FALSE;
1465 }
1466 break;
1467
1468 case SCANCODE_LEFT_LOGO_MAKE:
1469 ConsoleIn->LeftLogo = TRUE;
1470 break;
1471 case SCANCODE_LEFT_LOGO_BREAK:
1472 ConsoleIn->LeftLogo = FALSE;
1473 break;
1474 case SCANCODE_RIGHT_LOGO_MAKE:
1475 ConsoleIn->RightLogo = TRUE;
1476 break;
1477 case SCANCODE_RIGHT_LOGO_BREAK:
1478 ConsoleIn->RightLogo = FALSE;
1479 break;
1480 case SCANCODE_MENU_MAKE:
1481 ConsoleIn->Menu = TRUE;
1482 break;
1483 case SCANCODE_MENU_BREAK:
1484 ConsoleIn->Menu = FALSE;
1485 break;
1486 case SCANCODE_SYS_REQ_MAKE:
1487 if (Extended) {
1488 ConsoleIn->SysReq = TRUE;
1489 }
1490 case SCANCODE_CAPS_LOCK_MAKE:
1491 ConsoleIn->CapsLock = (BOOLEAN)!ConsoleIn->CapsLock;
1492 UpdateStatusLights (ConsoleIn);
1493 break;
1494
1495 case SCANCODE_NUM_LOCK_MAKE:
1496 ConsoleIn->NumLock = (BOOLEAN)!ConsoleIn->NumLock;
1497 UpdateStatusLights (ConsoleIn);
1498 break;
1499
1500 case SCANCODE_SCROLL_LOCK_MAKE:
1501 ConsoleIn->ScrollLock = (BOOLEAN)!ConsoleIn->ScrollLock;
1502 UpdateStatusLights (ConsoleIn);
1503 break;
1504 }
1505 //
1506 // If this is a BREAK Key or above the valid range, ignore it
1507 //
1508 if (ScanCode >= SCANCODE_MAX_MAKE) {
1509 continue;
1510 } else {
1511 break;
1512 }
1513 }
1514 //
1515 // Treat Numeric Key Pad "/" specially
1516 //
1517 if (Extended && ScanCode == 0x35) {
1518 ConsoleIn->Key.ScanCode = SCAN_NULL;
1519 ConsoleIn->Key.UnicodeChar = '/';
1520 return EFI_SUCCESS;
1521 }
1522 //
1523 // Convert Keyboard ScanCode into an EFI Key
1524 //
1525 for (Index = 0; ConvertKeyboardScanCodeToEfiKey[Index].ScanCode != TABLE_END; Index += 1) {
1526 if (ScanCode == ConvertKeyboardScanCodeToEfiKey[Index].ScanCode) {
1527 ConsoleIn->Key.ScanCode = ConvertKeyboardScanCodeToEfiKey[Index].EfiScanCode;
1528 if (ConsoleIn->Shift) {
1529 ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;
1530 //
1531 // Need not return associated shift state if a class of printable characters that
1532 // are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'
1533 //
1534 if (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {
1535 ConsoleIn->LeftShift = FALSE;
1536 ConsoleIn->RightShift = FALSE;
1537 }
1538 } else {
1539 ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;
1540 }
1541 //
1542 // alphabetic key is affected by CapsLock State
1543 //
1544 if (ConsoleIn->CapsLock) {
1545 if (ConsoleIn->Key.UnicodeChar >= 'a' && ConsoleIn->Key.UnicodeChar <= 'z') {
1546 ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].ShiftUnicodeChar;
1547 } else if (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {
1548 ConsoleIn->Key.UnicodeChar = ConvertKeyboardScanCodeToEfiKey[Index].UnicodeChar;
1549 }
1550 }
1551 //
1552 // Translate the CTRL-Alpha characters to their corresponding control value (ctrl-a = 0x0001 through ctrl-Z = 0x001A)
1553 //
1554 if (ConsoleIn->Ctrled) {
1555 if (ConsoleIn->Key.UnicodeChar >= 'a' && ConsoleIn->Key.UnicodeChar <= 'z') {
1556 ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - 'a' + 1);
1557 } else if (ConsoleIn->Key.UnicodeChar >= 'A' && ConsoleIn->Key.UnicodeChar <= 'Z') {
1558 ConsoleIn->Key.UnicodeChar = (UINT16) (ConsoleIn->Key.UnicodeChar - 'A' + 1);
1559 }
1560 }
1561
1562 break;
1563 }
1564 }
1565
1566 //
1567 // distinguish numeric key pad keys' 'up symbol' and 'down symbol'
1568 //
1569 if (ScanCode >= 0x47 && ScanCode <= 0x53) {
1570
1571 if (ConsoleIn->NumLock && !ConsoleIn->Shift && !Extended) {
1572 ConsoleIn->Key.ScanCode = SCAN_NULL;
1573 } else if (ScanCode != 0x4a && ScanCode != 0x4e) {
1574 ConsoleIn->Key.UnicodeChar = 0x00;
1575 }
1576 }
1577 //
1578 // If the key can not be converted then just return.
1579 //
1580 if (ConsoleIn->Key.ScanCode == SCAN_NULL && ConsoleIn->Key.UnicodeChar == 0x00) {
1581 return EFI_NOT_READY;
1582 }
1583
1584 //
1585 // Save the Shift/Toggle state
1586 //
1587 if (ConsoleIn->Ctrl) {
1588 ConsoleIn->KeyState.KeyShiftState |= (Extended == TRUE) ? EFI_RIGHT_CONTROL_PRESSED : EFI_LEFT_CONTROL_PRESSED;
1589 }
1590 if (ConsoleIn->Alt) {
1591 ConsoleIn->KeyState.KeyShiftState |= (Extended == TRUE) ? EFI_RIGHT_ALT_PRESSED : EFI_LEFT_ALT_PRESSED;
1592 }
1593 if (ConsoleIn->LeftShift) {
1594 ConsoleIn->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;
1595 }
1596 if (ConsoleIn->RightShift) {
1597 ConsoleIn->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;
1598 }
1599 if (ConsoleIn->LeftLogo) {
1600 ConsoleIn->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED;
1601 }
1602 if (ConsoleIn->RightLogo) {
1603 ConsoleIn->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED;
1604 }
1605 if (ConsoleIn->Menu) {
1606 ConsoleIn->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED;
1607 }
1608 if (ConsoleIn->SysReq) {
1609 ConsoleIn->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED;
1610 }
1611 if (ConsoleIn->CapsLock) {
1612 ConsoleIn->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;
1613 }
1614 if (ConsoleIn->NumLock) {
1615 ConsoleIn->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;
1616 }
1617 if (ConsoleIn->ScrollLock) {
1618 ConsoleIn->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;
1619 }
1620
1621 return EFI_SUCCESS;
1622 }
1623
1624 /**
1625 Perform 8042 controller and keyboard Initialization
1626 If ExtendedVerification is TRUE, do additional test for
1627 the keyboard interface
1628
1629 @param ConsoleIn - KEYBOARD_CONSOLE_IN_DEV instance pointer
1630 @param ExtendedVerification - indicates a thorough initialization
1631
1632 @retval EFI_DEVICE_ERROR Fail to init keyboard
1633 @retval EFI_SUCCESS Success to init keyboard
1634 **/
1635 EFI_STATUS
1636 InitKeyboard (
1637 IN OUT KEYBOARD_CONSOLE_IN_DEV *ConsoleIn,
1638 IN BOOLEAN ExtendedVerification
1639 )
1640 {
1641 EFI_STATUS Status;
1642 EFI_STATUS Status1;
1643 UINT8 CommandByte;
1644 EFI_PS2_POLICY_PROTOCOL *Ps2Policy;
1645
1646 Status = EFI_SUCCESS;
1647 mEnableMouseInterface = TRUE;
1648
1649 //
1650 // Get Ps2 policy to set this
1651 //
1652 Status = gBS->LocateProtocol (
1653 &gEfiPs2PolicyProtocolGuid,
1654 NULL,
1655 (VOID **) &Ps2Policy
1656 );
1657
1658 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1659 EFI_PROGRESS_CODE,
1660 EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_CLEAR_BUFFER,
1661 ConsoleIn->DevicePath
1662 );
1663
1664 //
1665 // Perform a read to cleanup the Status Register's
1666 // output buffer full bits
1667 //
1668 while (!EFI_ERROR (Status)) {
1669 Status = KeyboardRead (ConsoleIn, &CommandByte);
1670 }
1671 //
1672 // We should disable mouse interface during the initialization process
1673 // since mouse device output could block keyboard device output in the
1674 // 60H port of 8042 controller.
1675 //
1676 // So if we are not initializing 8042 controller for the
1677 // first time, we have to remember the previous mouse interface
1678 // enabling state
1679 //
1680 // Test the system flag in to determine whether this is the first
1681 // time initialization
1682 //
1683 if ((KeyReadStatusRegister (ConsoleIn) & 0x04)) {
1684 //
1685 // 8042 controller is already setup (by myself or by mouse driver):
1686 // See whether mouse interface is already enabled
1687 // which determines whether we should enable it later
1688 //
1689 //
1690 // Read the command byte of 8042 controller
1691 //
1692 Status = KeyboardCommand (ConsoleIn, 0x20);
1693 if (EFI_ERROR (Status)) {
1694 KeyboardError (ConsoleIn, L"\n\r");
1695 goto Done;
1696 }
1697
1698 Status = KeyboardRead (ConsoleIn, &CommandByte);
1699 if (EFI_ERROR (Status)) {
1700 KeyboardError (ConsoleIn, L"\n\r");
1701 goto Done;
1702 }
1703 //
1704 // Test the mouse enabling bit
1705 //
1706 if (CommandByte & 0x20) {
1707 mEnableMouseInterface = FALSE;
1708 } else {
1709 mEnableMouseInterface = TRUE;
1710 }
1711
1712 } else {
1713 //
1714 // 8042 controller is not setup yet:
1715 // 8042 controller selftest;
1716 // Don't enable mouse interface later.
1717 //
1718 //
1719 // Disable keyboard and mouse interfaces
1720 //
1721 Status = KeyboardCommand (ConsoleIn, 0xad);
1722 if (EFI_ERROR (Status)) {
1723 KeyboardError (ConsoleIn, L"\n\r");
1724 goto Done;
1725 }
1726
1727 Status = KeyboardCommand (ConsoleIn, 0xa7);
1728 if (EFI_ERROR (Status)) {
1729 KeyboardError (ConsoleIn, L"\n\r");
1730 goto Done;
1731 }
1732
1733 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1734 EFI_PROGRESS_CODE,
1735 EFI_PERIPHERAL_KEYBOARD | EFI_P_KEYBOARD_PC_SELF_TEST,
1736 ConsoleIn->DevicePath
1737 );
1738 //
1739 // 8042 Controller Self Test
1740 //
1741 Status = KeyboardCommand (ConsoleIn, 0xaa);
1742 if (EFI_ERROR (Status)) {
1743 KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");
1744 goto Done;
1745 }
1746
1747 Status = KeyboardWaitForValue (ConsoleIn, 0x55);
1748 if (EFI_ERROR (Status)) {
1749 KeyboardError (ConsoleIn, L"8042 controller self test failed!\n\r");
1750 goto Done;
1751 }
1752 //
1753 // Don't enable mouse interface later
1754 //
1755 mEnableMouseInterface = FALSE;
1756
1757 }
1758
1759 if (Ps2Policy != NULL) {
1760 Ps2Policy->Ps2InitHardware (ConsoleIn->Handle);
1761 }
1762 //
1763 // Write 8042 Command Byte, set System Flag
1764 // While at the same time:
1765 // 1. disable mouse interface,
1766 // 2. enable kbd interface,
1767 // 3. enable PC/XT kbd translation mode
1768 // 4. enable mouse and kbd interrupts
1769 //
1770 // ( Command Byte bits:
1771 // 7: Reserved
1772 // 6: PC/XT translation mode
1773 // 5: Disable Auxiliary device interface
1774 // 4: Disable keyboard interface
1775 // 3: Reserved
1776 // 2: System Flag
1777 // 1: Enable Auxiliary device interrupt
1778 // 0: Enable Keyboard interrupt )
1779 //
1780 Status = KeyboardCommand (ConsoleIn, 0x60);
1781 if (EFI_ERROR (Status)) {
1782 KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");
1783 goto Done;
1784 }
1785
1786 Status = KeyboardWrite (ConsoleIn, 0x67);
1787 if (EFI_ERROR (Status)) {
1788 KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");
1789 goto Done;
1790 }
1791
1792 //
1793 // Clear Memory Scancode Buffer
1794 //
1795 ConsoleIn->ScancodeBufStartPos = 0;
1796 ConsoleIn->ScancodeBufEndPos = KEYBOARD_BUFFER_MAX_COUNT - 1;
1797 ConsoleIn->ScancodeBufCount = 0;
1798 ConsoleIn->Ctrled = FALSE;
1799 ConsoleIn->Alted = FALSE;
1800
1801 //
1802 // Reset the status indicators
1803 //
1804 ConsoleIn->Ctrl = FALSE;
1805 ConsoleIn->Alt = FALSE;
1806 ConsoleIn->Shift = FALSE;
1807 ConsoleIn->CapsLock = FALSE;
1808 ConsoleIn->NumLock = FALSE;
1809 ConsoleIn->ScrollLock = FALSE;
1810 ConsoleIn->LeftShift = FALSE;
1811 ConsoleIn->RightShift = FALSE;
1812 ConsoleIn->LeftLogo = FALSE;
1813 ConsoleIn->RightLogo = FALSE;
1814 ConsoleIn->Menu = FALSE;
1815 ConsoleIn->SysReq = FALSE;
1816
1817 //
1818 // For reseting keyboard is not mandatory before booting OS and sometimes keyboard responses very slow,
1819 // and to support KB hot plug, we need to let the InitKB succeed no matter whether there is a KB device connected
1820 // to system. So we only do the real reseting for keyboard when user asks and there is a real KB connected t system,
1821 // and normally during booting an OS, it's skipped.
1822 //
1823 if (ExtendedVerification && CheckKeyboardConnect (ConsoleIn)) {
1824 //
1825 // Additional verifications for keyboard interface
1826 //
1827 //
1828 // Keyboard Interface Test
1829 //
1830 Status = KeyboardCommand (ConsoleIn, 0xab);
1831 if (EFI_ERROR (Status)) {
1832 KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");
1833 goto Done;
1834 }
1835
1836 Status = KeyboardWaitForValue (ConsoleIn, 0x00);
1837 if (EFI_ERROR (Status)) {
1838 KeyboardError (
1839 ConsoleIn,
1840 L"Some specific value not aquired from 8042 controller!\n\r"
1841 );
1842 goto Done;
1843 }
1844 //
1845 // Keyboard reset with a BAT(Basic Assurance Test)
1846 //
1847 Status = KeyboardWrite (ConsoleIn, 0xff);
1848 if (EFI_ERROR (Status)) {
1849 KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");
1850 goto Done;
1851 }
1852
1853 Status = KeyboardWaitForValue (ConsoleIn, 0xfa);
1854 if (EFI_ERROR (Status)) {
1855 KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");
1856 goto Done;
1857 }
1858 //
1859 // wait for BAT completion code
1860 //
1861 mWaitForValueTimeOut = KEYBOARD_BAT_TIMEOUT;
1862
1863 Status = KeyboardWaitForValue (ConsoleIn, 0xaa);
1864 if (EFI_ERROR (Status)) {
1865 KeyboardError (ConsoleIn, L"Keyboard self test failed!\n\r");
1866 goto Done;
1867 }
1868
1869 mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;
1870
1871 //
1872 // Set Keyboard to use Scan Code Set 2
1873 //
1874 Status = KeyboardWrite (ConsoleIn, 0xf0);
1875 if (EFI_ERROR (Status)) {
1876 KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");
1877 goto Done;
1878 }
1879
1880 Status = KeyboardWaitForValue (ConsoleIn, 0xfa);
1881 if (EFI_ERROR (Status)) {
1882 KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");
1883 goto Done;
1884 }
1885
1886 Status = KeyboardWrite (ConsoleIn, 0x02);
1887 if (EFI_ERROR (Status)) {
1888 KeyboardError (ConsoleIn, L"8042 controller data write error!!\n\r");
1889 goto Done;
1890 }
1891
1892 Status = KeyboardWaitForValue (ConsoleIn, 0xfa);
1893 if (EFI_ERROR (Status)) {
1894 KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");
1895 goto Done;
1896 }
1897
1898 //
1899 // Clear Keyboard Scancode Buffer
1900 //
1901 Status = KeyboardWrite (ConsoleIn, 0xf4);
1902 if (EFI_ERROR (Status)) {
1903 KeyboardError (ConsoleIn, L"8042 controller data write error!\n\r");
1904 goto Done;
1905 }
1906
1907 Status = KeyboardWaitForValue (ConsoleIn, 0xfa);
1908 if (EFI_ERROR (Status)) {
1909 KeyboardError (ConsoleIn, L"Some specific value not aquired from 8042 controller!\n\r");
1910 goto Done;
1911 }
1912 //
1913 if (Ps2Policy != NULL) {
1914 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_CAPSLOCK) == EFI_KEYBOARD_CAPSLOCK) {
1915 ConsoleIn->CapsLock = TRUE;
1916 }
1917
1918 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_NUMLOCK) == EFI_KEYBOARD_NUMLOCK) {
1919 ConsoleIn->NumLock = TRUE;
1920 }
1921
1922 if ((Ps2Policy->KeyboardLight & EFI_KEYBOARD_SCROLLLOCK) == EFI_KEYBOARD_SCROLLLOCK) {
1923 ConsoleIn->ScrollLock = TRUE;
1924 }
1925 }
1926 //
1927 // Update Keyboard Lights
1928 //
1929 Status = UpdateStatusLights (ConsoleIn);
1930 if (EFI_ERROR (Status)) {
1931 KeyboardError (ConsoleIn, L"Update keyboard status lights error!\n\r");
1932 goto Done;
1933 }
1934 }
1935 //
1936 // At last, we can now enable the mouse interface if appropriate
1937 //
1938 Done:
1939
1940 if (mEnableMouseInterface) {
1941 //
1942 // Enable mouse interface
1943 //
1944 Status1 = KeyboardCommand (ConsoleIn, 0xa8);
1945 if (EFI_ERROR (Status1)) {
1946 KeyboardError (ConsoleIn, L"8042 controller command write error!\n\r");
1947 return EFI_DEVICE_ERROR;
1948 }
1949 }
1950
1951 if (!EFI_ERROR (Status)) {
1952 return EFI_SUCCESS;
1953 } else {
1954 return EFI_DEVICE_ERROR;
1955 }
1956
1957 }
1958
1959 /**
1960 Disable the keyboard interface of the 8042 controller
1961
1962 @param ConsoleIn - the device instance
1963
1964 @return status of issuing disable command
1965
1966 **/
1967 EFI_STATUS
1968 DisableKeyboard (
1969 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
1970 )
1971 {
1972 EFI_STATUS Status;
1973
1974 //
1975 // Disable keyboard interface
1976 //
1977 Status = KeyboardCommand (ConsoleIn, 0xad);
1978 if (EFI_ERROR (Status)) {
1979 KeyboardError (ConsoleIn, L"\n\r");
1980 return EFI_DEVICE_ERROR;
1981 }
1982
1983 return Status;
1984 }
1985
1986 /**
1987 Check whether there is Ps/2 Keyboard device in system by 0xF4 Keyboard Command
1988 If Keyboard receives 0xF4, it will respond with 'ACK'. If it doesn't respond, the device
1989 should not be in system.
1990
1991 @param[in] ConsoleIn Keyboard Private Data Structure
1992
1993 @retval TRUE Keyboard in System.
1994 @retval FALSE Keyboard not in System.
1995 **/
1996 BOOLEAN
1997 EFIAPI
1998 CheckKeyboardConnect (
1999 IN KEYBOARD_CONSOLE_IN_DEV *ConsoleIn
2000 )
2001 {
2002 EFI_STATUS Status;
2003 UINTN WaitForValueTimeOutBcakup;
2004
2005 Status = EFI_SUCCESS;
2006 //
2007 // enable keyboard itself and wait for its ack
2008 // If can't receive ack, Keyboard should not be connected.
2009 //
2010 Status = KeyboardWrite (
2011 ConsoleIn,
2012 KEYBOARD_KBEN
2013 );
2014
2015 if (EFI_ERROR (Status)) {
2016 return FALSE;
2017 }
2018 //
2019 // wait for 1s
2020 //
2021 WaitForValueTimeOutBcakup = mWaitForValueTimeOut;
2022 mWaitForValueTimeOut = KEYBOARD_WAITFORVALUE_TIMEOUT;
2023 Status = KeyboardWaitForValue (
2024 ConsoleIn,
2025 KEYBOARD_CMDECHO_ACK
2026 );
2027 mWaitForValueTimeOut = WaitForValueTimeOutBcakup;
2028
2029 if (EFI_ERROR (Status)) {
2030 return FALSE;
2031 }
2032
2033 return TRUE;
2034 }
2035