1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
|
/** @file
Helper functions for USB Keyboard Driver.
Copyright (c) 2004 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "KeyBoard.h"
EFI_GUID mUsbKeyboardLayoutPackageGuid = USB_KEYBOARD_LAYOUT_PACKAGE_GUID;
EFI_GUID mUsbKeyboardLayoutKeyGuid = USB_KEYBOARD_LAYOUT_KEY_GUID;
USB_KEYBOARD_LAYOUT_PACK_BIN mUsbKeyboardLayoutBin = {
sizeof (USB_KEYBOARD_LAYOUT_PACK_BIN), // Binary size
//
// EFI_HII_PACKAGE_HEADER
//
{
sizeof (USB_KEYBOARD_LAYOUT_PACK_BIN) - sizeof (UINT32),
EFI_HII_PACKAGE_KEYBOARD_LAYOUT
},
1, // LayoutCount
sizeof (USB_KEYBOARD_LAYOUT_PACK_BIN) - sizeof (UINT32) - sizeof (EFI_HII_PACKAGE_HEADER) - sizeof (UINT16), // LayoutLength
USB_KEYBOARD_LAYOUT_KEY_GUID, // KeyGuid
sizeof (UINT16) + sizeof (EFI_GUID) + sizeof (UINT32) + sizeof (UINT8) + (USB_KEYBOARD_KEY_COUNT * sizeof (EFI_KEY_DESCRIPTOR)), // LayoutDescriptorStringOffset
USB_KEYBOARD_KEY_COUNT, // DescriptorCount
{
//
// EFI_KEY_DESCRIPTOR (total number is USB_KEYBOARD_KEY_COUNT)
//
{EfiKeyC1, 'a', 'A', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB5, 'b', 'B', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB3, 'c', 'C', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC3, 'd', 'D', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD3, 'e', 'E', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC4, 'f', 'F', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC5, 'g', 'G', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC6, 'h', 'H', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD8, 'i', 'I', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC7, 'j', 'J', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC8, 'k', 'K', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC9, 'l', 'L', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB7, 'm', 'M', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB6, 'n', 'N', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD9, 'o', 'O', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD10, 'p', 'P', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD1, 'q', 'Q', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD4, 'r', 'R', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyC2, 's', 'S', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD5, 't', 'T', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD7, 'u', 'U', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB4, 'v', 'V', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD2, 'w', 'W', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB2, 'x', 'X', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyD6, 'y', 'Y', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyB1, 'z', 'Z', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_CAPS_LOCK},
{EfiKeyE1, '1', '!', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE2, '2', '@', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE3, '3', '#', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE4, '4', '$', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE5, '5', '%', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE6, '6', '^', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE7, '7', '&', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE8, '8', '*', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE9, '9', '(', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE10, '0', ')', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyEnter, 0x0d, 0x0d, 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyEsc, 0x1b, 0x1b, 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyBackSpace, 0x08, 0x08, 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyTab, 0x09, 0x09, 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeySpaceBar, ' ', ' ', 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyE11, '-', '_', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE12, '=', '+', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyD11, '[', '{', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyD12, ']', '}', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyD13, '\\', '|', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyC10, ';', ':', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyC11, '\'', '"', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyE0, '`', '~', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyB8, ',', '<', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyB9, '.', '>', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyB10, '/', '?', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT},
{EfiKeyCapsLock, 0x00, 0x00, 0, 0, EFI_CAPS_LOCK_MODIFIER, 0},
{EfiKeyF1, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_ONE_MODIFIER, 0},
{EfiKeyF2, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_TWO_MODIFIER, 0},
{EfiKeyF3, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_THREE_MODIFIER, 0},
{EfiKeyF4, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_FOUR_MODIFIER, 0},
{EfiKeyF5, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_FIVE_MODIFIER, 0},
{EfiKeyF6, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_SIX_MODIFIER, 0},
{EfiKeyF7, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_SEVEN_MODIFIER, 0},
{EfiKeyF8, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_EIGHT_MODIFIER, 0},
{EfiKeyF9, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_NINE_MODIFIER, 0},
{EfiKeyF10, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_TEN_MODIFIER, 0},
{EfiKeyF11, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_ELEVEN_MODIFIER, 0},
{EfiKeyF12, 0x00, 0x00, 0, 0, EFI_FUNCTION_KEY_TWELVE_MODIFIER, 0},
{EfiKeyPrint, 0x00, 0x00, 0, 0, EFI_PRINT_MODIFIER, 0},
{EfiKeySLck, 0x00, 0x00, 0, 0, EFI_SCROLL_LOCK_MODIFIER, 0},
{EfiKeyPause, 0x00, 0x00, 0, 0, EFI_PAUSE_MODIFIER, 0},
{EfiKeyIns, 0x00, 0x00, 0, 0, EFI_INSERT_MODIFIER, 0},
{EfiKeyHome, 0x00, 0x00, 0, 0, EFI_HOME_MODIFIER, 0},
{EfiKeyPgUp, 0x00, 0x00, 0, 0, EFI_PAGE_UP_MODIFIER, 0},
{EfiKeyDel, 0x00, 0x00, 0, 0, EFI_DELETE_MODIFIER, 0},
{EfiKeyEnd, 0x00, 0x00, 0, 0, EFI_END_MODIFIER, 0},
{EfiKeyPgDn, 0x00, 0x00, 0, 0, EFI_PAGE_DOWN_MODIFIER, 0},
{EfiKeyRightArrow, 0x00, 0x00, 0, 0, EFI_RIGHT_ARROW_MODIFIER, 0},
{EfiKeyLeftArrow, 0x00, 0x00, 0, 0, EFI_LEFT_ARROW_MODIFIER, 0},
{EfiKeyDownArrow, 0x00, 0x00, 0, 0, EFI_DOWN_ARROW_MODIFIER, 0},
{EfiKeyUpArrow, 0x00, 0x00, 0, 0, EFI_UP_ARROW_MODIFIER, 0},
{EfiKeyNLck, 0x00, 0x00, 0, 0, EFI_NUM_LOCK_MODIFIER, 0},
{EfiKeySlash, '/', '/', 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyAsterisk, '*', '*', 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyMinus, '-', '-', 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyPlus, '+', '+', 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyEnter, 0x0d, 0x0d, 0, 0, EFI_NULL_MODIFIER, 0},
{EfiKeyOne, '1', '1', 0, 0, EFI_END_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyTwo, '2', '2', 0, 0, EFI_DOWN_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyThree, '3', '3', 0, 0, EFI_PAGE_DOWN_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyFour, '4', '4', 0, 0, EFI_LEFT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyFive, '5', '5', 0, 0, EFI_NULL_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeySix, '6', '6', 0, 0, EFI_RIGHT_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeySeven, '7', '7', 0, 0, EFI_HOME_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyEight, '8', '8', 0, 0, EFI_UP_ARROW_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyNine, '9', '9', 0, 0, EFI_PAGE_UP_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyZero, '0', '0', 0, 0, EFI_INSERT_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyPeriod, '.', '.', 0, 0, EFI_DELETE_MODIFIER, EFI_AFFECTED_BY_STANDARD_SHIFT | EFI_AFFECTED_BY_NUM_LOCK},
{EfiKeyA4, 0x00, 0x00, 0, 0, EFI_MENU_MODIFIER, 0},
{EfiKeyLCtrl, 0, 0, 0, 0, EFI_LEFT_CONTROL_MODIFIER, 0},
{EfiKeyLShift, 0, 0, 0, 0, EFI_LEFT_SHIFT_MODIFIER, 0},
{EfiKeyLAlt, 0, 0, 0, 0, EFI_LEFT_ALT_MODIFIER, 0},
{EfiKeyA0, 0, 0, 0, 0, EFI_LEFT_LOGO_MODIFIER, 0},
{EfiKeyRCtrl, 0, 0, 0, 0, EFI_RIGHT_CONTROL_MODIFIER, 0},
{EfiKeyRShift, 0, 0, 0, 0, EFI_RIGHT_SHIFT_MODIFIER, 0},
{EfiKeyA2, 0, 0, 0, 0, EFI_RIGHT_ALT_MODIFIER, 0},
{EfiKeyA3, 0, 0, 0, 0, EFI_RIGHT_LOGO_MODIFIER, 0},
},
1, // DescriptionCount
{'e', 'n', '-', 'U', 'S'}, // RFC4646 language code
' ', // Space
{'E', 'n', 'g', 'l', 'i', 's', 'h', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd', '\0'}, // DescriptionString[]
};
//
// EFI_KEY to USB Keycode conversion table
// EFI_KEY is defined in UEFI spec.
// USB Keycode is defined in USB HID Firmware spec.
//
UINT8 EfiKeyToUsbKeyCodeConvertionTable[] = {
0xe0, // EfiKeyLCtrl
0xe3, // EfiKeyA0
0xe2, // EfiKeyLAlt
0x2c, // EfiKeySpaceBar
0xe6, // EfiKeyA2
0xe7, // EfiKeyA3
0x65, // EfiKeyA4
0xe4, // EfiKeyRCtrl
0x50, // EfiKeyLeftArrow
0x51, // EfiKeyDownArrow
0x4F, // EfiKeyRightArrow
0x62, // EfiKeyZero
0x63, // EfiKeyPeriod
0x28, // EfiKeyEnter
0xe1, // EfiKeyLShift
0x64, // EfiKeyB0
0x1D, // EfiKeyB1
0x1B, // EfiKeyB2
0x06, // EfiKeyB3
0x19, // EfiKeyB4
0x05, // EfiKeyB5
0x11, // EfiKeyB6
0x10, // EfiKeyB7
0x36, // EfiKeyB8
0x37, // EfiKeyB9
0x38, // EfiKeyB10
0xe5, // EfiKeyRShift
0x52, // EfiKeyUpArrow
0x59, // EfiKeyOne
0x5A, // EfiKeyTwo
0x5B, // EfiKeyThree
0x39, // EfiKeyCapsLock
0x04, // EfiKeyC1
0x16, // EfiKeyC2
0x07, // EfiKeyC3
0x09, // EfiKeyC4
0x0A, // EfiKeyC5
0x0B, // EfiKeyC6
0x0D, // EfiKeyC7
0x0E, // EfiKeyC8
0x0F, // EfiKeyC9
0x33, // EfiKeyC10
0x34, // EfiKeyC11
0x32, // EfiKeyC12
0x5C, // EfiKeyFour
0x5D, // EfiKeyFive
0x5E, // EfiKeySix
0x57, // EfiKeyPlus
0x2B, // EfiKeyTab
0x14, // EfiKeyD1
0x1A, // EfiKeyD2
0x08, // EfiKeyD3
0x15, // EfiKeyD4
0x17, // EfiKeyD5
0x1C, // EfiKeyD6
0x18, // EfiKeyD7
0x0C, // EfiKeyD8
0x12, // EfiKeyD9
0x13, // EfiKeyD10
0x2F, // EfiKeyD11
0x30, // EfiKeyD12
0x31, // EfiKeyD13
0x4C, // EfiKeyDel
0x4D, // EfiKeyEnd
0x4E, // EfiKeyPgDn
0x5F, // EfiKeySeven
0x60, // EfiKeyEight
0x61, // EfiKeyNine
0x35, // EfiKeyE0
0x1E, // EfiKeyE1
0x1F, // EfiKeyE2
0x20, // EfiKeyE3
0x21, // EfiKeyE4
0x22, // EfiKeyE5
0x23, // EfiKeyE6
0x24, // EfiKeyE7
0x25, // EfiKeyE8
0x26, // EfiKeyE9
0x27, // EfiKeyE10
0x2D, // EfiKeyE11
0x2E, // EfiKeyE12
0x2A, // EfiKeyBackSpace
0x49, // EfiKeyIns
0x4A, // EfiKeyHome
0x4B, // EfiKeyPgUp
0x53, // EfiKeyNLck
0x54, // EfiKeySlash
0x55, // EfiKeyAsterisk
0x56, // EfiKeyMinus
0x29, // EfiKeyEsc
0x3A, // EfiKeyF1
0x3B, // EfiKeyF2
0x3C, // EfiKeyF3
0x3D, // EfiKeyF4
0x3E, // EfiKeyF5
0x3F, // EfiKeyF6
0x40, // EfiKeyF7
0x41, // EfiKeyF8
0x42, // EfiKeyF9
0x43, // EfiKeyF10
0x44, // EfiKeyF11
0x45, // EfiKeyF12
0x46, // EfiKeyPrint
0x47, // EfiKeySLck
0x48 // EfiKeyPause
};
//
// Keyboard modifier value to EFI Scan Code convertion table
// EFI Scan Code and the modifier values are defined in UEFI spec.
//
UINT8 ModifierValueToEfiScanCodeConvertionTable[] = {
SCAN_NULL, // EFI_NULL_MODIFIER
SCAN_NULL, // EFI_LEFT_CONTROL_MODIFIER
SCAN_NULL, // EFI_RIGHT_CONTROL_MODIFIER
SCAN_NULL, // EFI_LEFT_ALT_MODIFIER
SCAN_NULL, // EFI_RIGHT_ALT_MODIFIER
SCAN_NULL, // EFI_ALT_GR_MODIFIER
SCAN_INSERT, // EFI_INSERT_MODIFIER
SCAN_DELETE, // EFI_DELETE_MODIFIER
SCAN_PAGE_DOWN, // EFI_PAGE_DOWN_MODIFIER
SCAN_PAGE_UP, // EFI_PAGE_UP_MODIFIER
SCAN_HOME, // EFI_HOME_MODIFIER
SCAN_END, // EFI_END_MODIFIER
SCAN_NULL, // EFI_LEFT_SHIFT_MODIFIER
SCAN_NULL, // EFI_RIGHT_SHIFT_MODIFIER
SCAN_NULL, // EFI_CAPS_LOCK_MODIFIER
SCAN_NULL, // EFI_NUM_LOCK_MODIFIER
SCAN_LEFT, // EFI_LEFT_ARROW_MODIFIER
SCAN_RIGHT, // EFI_RIGHT_ARROW_MODIFIER
SCAN_DOWN, // EFI_DOWN_ARROW_MODIFIER
SCAN_UP, // EFI_UP_ARROW_MODIFIER
SCAN_NULL, // EFI_NS_KEY_MODIFIER
SCAN_NULL, // EFI_NS_KEY_DEPENDENCY_MODIFIER
SCAN_F1, // EFI_FUNCTION_KEY_ONE_MODIFIER
SCAN_F2, // EFI_FUNCTION_KEY_TWO_MODIFIER
SCAN_F3, // EFI_FUNCTION_KEY_THREE_MODIFIER
SCAN_F4, // EFI_FUNCTION_KEY_FOUR_MODIFIER
SCAN_F5, // EFI_FUNCTION_KEY_FIVE_MODIFIER
SCAN_F6, // EFI_FUNCTION_KEY_SIX_MODIFIER
SCAN_F7, // EFI_FUNCTION_KEY_SEVEN_MODIFIER
SCAN_F8, // EFI_FUNCTION_KEY_EIGHT_MODIFIER
SCAN_F9, // EFI_FUNCTION_KEY_NINE_MODIFIER
SCAN_F10, // EFI_FUNCTION_KEY_TEN_MODIFIER
SCAN_F11, // EFI_FUNCTION_KEY_ELEVEN_MODIFIER
SCAN_F12, // EFI_FUNCTION_KEY_TWELVE_MODIFIER
};
/**
Initialize Key Convention Table by using default keyboard layout.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@retval EFI_SUCCESS The default keyboard layout was installed successfully
@retval Others Failure to install default keyboard layout.
**/
EFI_STATUS
EFIAPI
InstallDefaultKeyboardLayout (
IN OUT USB_KB_DEV *UsbKeyboardDevice
)
{
EFI_STATUS Status;
EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
EFI_HII_HANDLE HiiHandle;
//
// Locate Hii database protocol
//
Status = gBS->LocateProtocol (
&gEfiHiiDatabaseProtocolGuid,
NULL,
(VOID **) &HiiDatabase
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Install Keyboard Layout package to HII database
//
HiiHandle = HiiAddPackages (
&mUsbKeyboardLayoutPackageGuid,
UsbKeyboardDevice->ControllerHandle,
&mUsbKeyboardLayoutBin,
NULL
);
if (HiiHandle == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Set current keyboard layout
//
Status = HiiDatabase->SetKeyboardLayout (HiiDatabase, &mUsbKeyboardLayoutKeyGuid);
return Status;
}
/**
Uses USB I/O to check whether the device is a USB keyboard device.
@param UsbIo Pointer to a USB I/O protocol instance.
@retval TRUE Device is a USB keyboard device.
@retval FALSE Device is a not USB keyboard device.
**/
BOOLEAN
EFIAPI
IsUSBKeyboard (
IN EFI_USB_IO_PROTOCOL *UsbIo
)
{
EFI_STATUS Status;
EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
//
// Get the default interface descriptor
//
Status = UsbIo->UsbGetInterfaceDescriptor (
UsbIo,
&InterfaceDescriptor
);
if (EFI_ERROR (Status)) {
return FALSE;
}
if (InterfaceDescriptor.InterfaceClass == CLASS_HID &&
InterfaceDescriptor.InterfaceSubClass == SUBCLASS_BOOT &&
InterfaceDescriptor.InterfaceProtocol == PROTOCOL_KEYBOARD
) {
return TRUE;
}
return FALSE;
}
/**
Get current keyboard layout from HII database.
@return Pointer to HII Keyboard Layout.
NULL means failure occurred while trying to get keyboard layout.
**/
EFI_HII_KEYBOARD_LAYOUT *
EFIAPI
GetCurrentKeyboardLayout (
VOID
)
{
EFI_STATUS Status;
EFI_HII_DATABASE_PROTOCOL *HiiDatabase;
EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout;
UINT16 Length;
//
// Locate HII Database Protocol
//
Status = gBS->LocateProtocol (
&gEfiHiiDatabaseProtocolGuid,
NULL,
(VOID **) &HiiDatabase
);
if (EFI_ERROR (Status)) {
return NULL;
}
//
// Get current keyboard layout from HII database
//
Length = 0;
KeyboardLayout = NULL;
Status = HiiDatabase->GetKeyboardLayout (
HiiDatabase,
NULL,
&Length,
KeyboardLayout
);
if (Status == EFI_BUFFER_TOO_SMALL) {
KeyboardLayout = AllocatePool (Length);
ASSERT (KeyboardLayout != NULL);
Status = HiiDatabase->GetKeyboardLayout (
HiiDatabase,
NULL,
&Length,
KeyboardLayout
);
if (EFI_ERROR (Status)) {
FreePool (KeyboardLayout);
KeyboardLayout = NULL;
}
}
return KeyboardLayout;
}
/**
Find Key Descriptor in Key Convertion Table given its USB keycode.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@param KeyCode USB Keycode.
@return The Key Descriptor in Key Convertion Table.
NULL means not found.
**/
EFI_KEY_DESCRIPTOR *
EFIAPI
GetKeyDescriptor (
IN USB_KB_DEV *UsbKeyboardDevice,
IN UINT8 KeyCode
)
{
UINT8 Index;
//
// Make sure KeyCode is in the range of [0x4, 0x65] or [0xe0, 0xe7]
//
if ((!USBKBD_VALID_KEYCODE (KeyCode)) || ((KeyCode > 0x65) && (KeyCode < 0xe0)) || (KeyCode > 0xe7)) {
return NULL;
}
//
// Calculate the index of Key Descriptor in Key Convertion Table
//
if (KeyCode <= 0x65) {
Index = (UINT8) (KeyCode - 4);
} else {
Index = (UINT8) (KeyCode - 0xe0 + NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE);
}
return &UsbKeyboardDevice->KeyConvertionTable[Index];
}
/**
Find Non-Spacing key for given Key descriptor.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@param KeyDescriptor Key descriptor.
@return The Non-Spacing key corresponding to KeyDescriptor
NULL means not found.
**/
USB_NS_KEY *
EFIAPI
FindUsbNsKey (
IN USB_KB_DEV *UsbKeyboardDevice,
IN EFI_KEY_DESCRIPTOR *KeyDescriptor
)
{
LIST_ENTRY *Link;
LIST_ENTRY *NsKeyList;
USB_NS_KEY *UsbNsKey;
NsKeyList = &UsbKeyboardDevice->NsKeyList;
Link = GetFirstNode (NsKeyList);
while (!IsNull (NsKeyList, Link)) {
UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link);
if (UsbNsKey->NsKey[0].Key == KeyDescriptor->Key) {
return UsbNsKey;
}
Link = GetNextNode (NsKeyList, Link);
}
return NULL;
}
/**
Find physical key definition for a given key descriptor.
For a specified non-spacing key, there are a list of physical
keys following it. This function traverses the list of
physical keys and tries to find the physical key matching
the KeyDescriptor.
@param UsbNsKey The non-spacing key information.
@param KeyDescriptor The key descriptor.
@return The physical key definition.
If no physical key is found, parameter KeyDescriptor is returned.
**/
EFI_KEY_DESCRIPTOR *
EFIAPI
FindPhysicalKey (
IN USB_NS_KEY *UsbNsKey,
IN EFI_KEY_DESCRIPTOR *KeyDescriptor
)
{
UINTN Index;
EFI_KEY_DESCRIPTOR *PhysicalKey;
PhysicalKey = &UsbNsKey->NsKey[1];
for (Index = 0; Index < UsbNsKey->KeyCount; Index++) {
if (KeyDescriptor->Key == PhysicalKey->Key) {
return PhysicalKey;
}
PhysicalKey++;
}
//
// No children definition matched, return original key
//
return KeyDescriptor;
}
/**
The notification function for EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID.
This function is registered to event of EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID
group type, which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout().
It tries to get curent keyboard layout from HII database.
@param Event Event being signaled.
@param Context Points to USB_KB_DEV instance.
**/
VOID
EFIAPI
SetKeyboardLayoutEvent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
USB_KB_DEV *UsbKeyboardDevice;
EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout;
EFI_KEY_DESCRIPTOR TempKey;
EFI_KEY_DESCRIPTOR *KeyDescriptor;
EFI_KEY_DESCRIPTOR *TableEntry;
EFI_KEY_DESCRIPTOR *NsKey;
USB_NS_KEY *UsbNsKey;
UINTN Index;
UINTN Index2;
UINTN KeyCount;
UINT8 KeyCode;
UsbKeyboardDevice = (USB_KB_DEV *) Context;
if (UsbKeyboardDevice->Signature != USB_KB_DEV_SIGNATURE) {
return;
}
//
// Try to get current keyboard layout from HII database
//
KeyboardLayout = GetCurrentKeyboardLayout ();
if (KeyboardLayout == NULL) {
return;
}
//
// Re-allocate resource for KeyConvertionTable
//
ReleaseKeyboardLayoutResources (UsbKeyboardDevice);
UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((NUMBER_OF_VALID_USB_KEYCODE) * sizeof (EFI_KEY_DESCRIPTOR));
ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);
//
// Traverse the list of key descriptors following the header of EFI_HII_KEYBOARD_LAYOUT
//
KeyDescriptor = (EFI_KEY_DESCRIPTOR *) (((UINT8 *) KeyboardLayout) + sizeof (EFI_HII_KEYBOARD_LAYOUT));
for (Index = 0; Index < KeyboardLayout->DescriptorCount; Index++) {
//
// Copy from HII keyboard layout package binary for alignment
//
CopyMem (&TempKey, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));
//
// Fill the key into KeyConvertionTable, whose index is calculated from USB keycode.
//
KeyCode = EfiKeyToUsbKeyCodeConvertionTable [(UINT8) (TempKey.Key)];
TableEntry = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);
CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));
//
// For non-spacing key, create the list with a non-spacing key followed by physical keys.
//
if (TempKey.Modifier == EFI_NS_KEY_MODIFIER) {
UsbNsKey = AllocatePool (sizeof (USB_NS_KEY));
ASSERT (UsbNsKey != NULL);
//
// Search for sequential children physical key definitions
//
KeyCount = 0;
NsKey = KeyDescriptor + 1;
for (Index2 = Index + 1; Index2 < KeyboardLayout->DescriptorCount; Index2++) {
CopyMem (&TempKey, NsKey, sizeof (EFI_KEY_DESCRIPTOR));
if (TempKey.Modifier == EFI_NS_KEY_DEPENDENCY_MODIFIER) {
KeyCount++;
} else {
break;
}
NsKey++;
}
UsbNsKey->Signature = USB_NS_KEY_SIGNATURE;
UsbNsKey->KeyCount = KeyCount;
UsbNsKey->NsKey = AllocateCopyPool (
(KeyCount + 1) * sizeof (EFI_KEY_DESCRIPTOR),
KeyDescriptor
);
InsertTailList (&UsbKeyboardDevice->NsKeyList, &UsbNsKey->Link);
//
// Skip over the child physical keys
//
Index += KeyCount;
KeyDescriptor += KeyCount;
}
KeyDescriptor++;
}
//
// There are two EfiKeyEnter, duplicate its key descriptor
//
TableEntry = GetKeyDescriptor (UsbKeyboardDevice, 0x58);
KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, 0x28);
CopyMem (TableEntry, KeyDescriptor, sizeof (EFI_KEY_DESCRIPTOR));
FreePool (KeyboardLayout);
}
/**
Destroy resources for keyboard layout.
@param UsbKeyboardDevice The USB_KB_DEV instance.
**/
VOID
EFIAPI
ReleaseKeyboardLayoutResources (
IN OUT USB_KB_DEV *UsbKeyboardDevice
)
{
USB_NS_KEY *UsbNsKey;
LIST_ENTRY *Link;
if (UsbKeyboardDevice->KeyConvertionTable != NULL) {
FreePool (UsbKeyboardDevice->KeyConvertionTable);
}
UsbKeyboardDevice->KeyConvertionTable = NULL;
while (!IsListEmpty (&UsbKeyboardDevice->NsKeyList)) {
Link = GetFirstNode (&UsbKeyboardDevice->NsKeyList);
UsbNsKey = USB_NS_KEY_FORM_FROM_LINK (Link);
RemoveEntryList (&UsbNsKey->Link);
FreePool (UsbNsKey->NsKey);
FreePool (UsbNsKey);
}
}
/**
Initialize USB keyboard layout.
This function initializes Key Convertion Table for the USB keyboard device.
It first tries to retrieve layout from HII database. If failed and default
layout is enabled, then it just uses the default layout.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@retval EFI_SUCCESS Initialization succeeded.
@retval EFI_NOT_READY Keyboard layout cannot be retrieve from HII
database, and default layout is disabled.
@retval Other Fail to register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group.
**/
EFI_STATUS
EFIAPI
InitKeyboardLayout (
OUT USB_KB_DEV *UsbKeyboardDevice
)
{
EFI_HII_KEYBOARD_LAYOUT *KeyboardLayout;
EFI_STATUS Status;
UsbKeyboardDevice->KeyConvertionTable = AllocateZeroPool ((NUMBER_OF_VALID_USB_KEYCODE) * sizeof (EFI_KEY_DESCRIPTOR));
ASSERT (UsbKeyboardDevice->KeyConvertionTable != NULL);
InitializeListHead (&UsbKeyboardDevice->NsKeyList);
UsbKeyboardDevice->CurrentNsKey = NULL;
UsbKeyboardDevice->KeyboardLayoutEvent = NULL;
//
// Register event to EFI_HII_SET_KEYBOARD_LAYOUT_EVENT_GUID group,
// which will be triggered by EFI_HII_DATABASE_PROTOCOL.SetKeyboardLayout().
//
Status = gBS->CreateEventEx (
EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
SetKeyboardLayoutEvent,
UsbKeyboardDevice,
&gEfiHiiKeyBoardLayoutGuid,
&UsbKeyboardDevice->KeyboardLayoutEvent
);
if (EFI_ERROR (Status)) {
return Status;
}
KeyboardLayout = GetCurrentKeyboardLayout ();
if (KeyboardLayout != NULL) {
//
// If current keyboard layout is successfully retrieved from HII database,
// force to initialize the keyboard layout.
//
gBS->SignalEvent (UsbKeyboardDevice->KeyboardLayoutEvent);
} else {
if (FeaturePcdGet (PcdDisableDefaultKeyboardLayoutInUsbKbDriver)) {
//
// If no keyboard layout can be retrieved from HII database, and default layout
// is disabled, then return EFI_NOT_READY.
//
return EFI_NOT_READY;
}
//
// If no keyboard layout can be retrieved from HII database, and default layout
// is enabled, then load the default keyboard layout.
//
InstallDefaultKeyboardLayout (UsbKeyboardDevice);
}
return EFI_SUCCESS;
}
/**
Initialize USB keyboard device and all private data structures.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@retval EFI_SUCCESS Initialization is successful.
@retval EFI_DEVICE_ERROR Keyboard initialization failed.
**/
EFI_STATUS
EFIAPI
InitUSBKeyboard (
IN OUT USB_KB_DEV *UsbKeyboardDevice
)
{
UINT16 ConfigValue;
UINT8 Protocol;
UINT8 ReportId;
UINT8 Duration;
EFI_STATUS Status;
UINT32 TransferResult;
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_PROGRESS_CODE,
FixedPcdGet32 (PcdStatusCodeValueKeyboardSelfTest),
UsbKeyboardDevice->DevicePath
);
InitUSBKeyBuffer (&(UsbKeyboardDevice->KeyboardBuffer));
//
// Use the config out of the descriptor
// Assumed the first config is the correct one and this is not always the case
//
Status = UsbGetConfiguration (
UsbKeyboardDevice->UsbIo,
&ConfigValue,
&TransferResult
);
if (EFI_ERROR (Status)) {
ConfigValue = 0x01;
}
//
// Uses default configuration to configure the USB Keyboard device.
//
Status = UsbSetConfiguration (
UsbKeyboardDevice->UsbIo,
ConfigValue,
&TransferResult
);
if (EFI_ERROR (Status)) {
//
// If configuration could not be set here, it means
// the keyboard interface has some errors and could
// not be initialized
//
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_ERROR_CODE | EFI_ERROR_MINOR,
FixedPcdGet32 (PcdStatusCodeValueKeyboardInterfaceError),
UsbKeyboardDevice->DevicePath
);
return EFI_DEVICE_ERROR;
}
UsbGetProtocolRequest (
UsbKeyboardDevice->UsbIo,
UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
&Protocol
);
//
// Set boot protocol for the USB Keyboard.
// This driver only supports boot protocol.
//
if (Protocol != BOOT_PROTOCOL) {
UsbSetProtocolRequest (
UsbKeyboardDevice->UsbIo,
UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
BOOT_PROTOCOL
);
}
//
// ReportId is zero, which means the idle rate applies to all input reports.
//
ReportId = 0;
//
// Duration is zero, which means the duration is infinite.
// so the endpoint will inhibit reporting forever,
// and only reporting when a change is detected in the report data.
//
Duration = 0;
UsbSetIdleRequest (
UsbKeyboardDevice->UsbIo,
UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
ReportId,
Duration
);
UsbKeyboardDevice->CtrlOn = FALSE;
UsbKeyboardDevice->AltOn = FALSE;
UsbKeyboardDevice->ShiftOn = FALSE;
UsbKeyboardDevice->NumLockOn = FALSE;
UsbKeyboardDevice->CapsOn = FALSE;
UsbKeyboardDevice->ScrollOn = FALSE;
UsbKeyboardDevice->LeftCtrlOn = FALSE;
UsbKeyboardDevice->LeftAltOn = FALSE;
UsbKeyboardDevice->LeftShiftOn = FALSE;
UsbKeyboardDevice->LeftLogoOn = FALSE;
UsbKeyboardDevice->RightCtrlOn = FALSE;
UsbKeyboardDevice->RightAltOn = FALSE;
UsbKeyboardDevice->RightShiftOn = FALSE;
UsbKeyboardDevice->RightLogoOn = FALSE;
UsbKeyboardDevice->MenuKeyOn = FALSE;
UsbKeyboardDevice->SysReqOn = FALSE;
UsbKeyboardDevice->AltGrOn = FALSE;
UsbKeyboardDevice->CurrentNsKey = NULL;
//
// Sync the initial state of lights on keyboard.
//
SetKeyLED (UsbKeyboardDevice);
ZeroMem (UsbKeyboardDevice->LastKeyCodeArray, sizeof (UINT8) * 8);
//
// Create event for repeat keys' generation.
//
if (UsbKeyboardDevice->RepeatTimer != NULL) {
gBS->CloseEvent (UsbKeyboardDevice->RepeatTimer);
UsbKeyboardDevice->RepeatTimer = NULL;
}
gBS->CreateEvent (
EVT_TIMER | EVT_NOTIFY_SIGNAL,
TPL_CALLBACK,
USBKeyboardRepeatHandler,
UsbKeyboardDevice,
&UsbKeyboardDevice->RepeatTimer
);
//
// Create event for delayed recovery, which deals with device error.
//
if (UsbKeyboardDevice->DelayedRecoveryEvent != NULL) {
gBS->CloseEvent (UsbKeyboardDevice->DelayedRecoveryEvent);
UsbKeyboardDevice->DelayedRecoveryEvent = NULL;
}
gBS->CreateEvent (
EVT_TIMER | EVT_NOTIFY_SIGNAL,
TPL_NOTIFY,
USBKeyboardRecoveryHandler,
UsbKeyboardDevice,
&UsbKeyboardDevice->DelayedRecoveryEvent
);
return EFI_SUCCESS;
}
/**
Handler function for USB keyboard's asynchronous interrupt transfer.
This function is the handler function for USB keyboard's asynchronous interrupt transfer
to manage the keyboard. It parses the USB keyboard input report, and inserts data to
keyboard buffer according to state of modifer keys and normal keys. Timer for repeat key
is also set accordingly.
@param Data A pointer to a buffer that is filled with key data which is
retrieved via asynchronous interrupt transfer.
@param DataLength Indicates the size of the data buffer.
@param Context Pointing to USB_KB_DEV instance.
@param Result Indicates the result of the asynchronous interrupt transfer.
@retval EFI_SUCCESS Asynchronous interrupt transfer is handled successfully.
@retval EFI_DEVICE_ERROR Hardware error occurs.
**/
EFI_STATUS
EFIAPI
KeyboardHandler (
IN VOID *Data,
IN UINTN DataLength,
IN VOID *Context,
IN UINT32 Result
)
{
USB_KB_DEV *UsbKeyboardDevice;
EFI_USB_IO_PROTOCOL *UsbIo;
UINT8 *CurKeyCodeBuffer;
UINT8 *OldKeyCodeBuffer;
UINT8 CurModifierMap;
UINT8 OldModifierMap;
UINT8 Mask;
UINT8 Index;
UINT8 Index2;
BOOLEAN Down;
BOOLEAN KeyRelease;
BOOLEAN KeyPress;
UINT8 SavedTail;
USB_KEY UsbKey;
UINT8 NewRepeatKey;
UINT32 UsbStatus;
EFI_KEY_DESCRIPTOR *KeyDescriptor;
ASSERT (Context != NULL);
NewRepeatKey = 0;
UsbKeyboardDevice = (USB_KB_DEV *) Context;
UsbIo = UsbKeyboardDevice->UsbIo;
//
// Analyzes Result and performs corresponding action.
//
if (Result != EFI_USB_NOERROR) {
//
// Some errors happen during the process
//
REPORT_STATUS_CODE_WITH_DEVICE_PATH (
EFI_ERROR_CODE | EFI_ERROR_MINOR,
FixedPcdGet32 (PcdStatusCodeValueKeyboardInputError),
UsbKeyboardDevice->DevicePath
);
//
// Stop the repeat key generation if any
//
UsbKeyboardDevice->RepeatKey = 0;
gBS->SetTimer (
UsbKeyboardDevice->RepeatTimer,
TimerCancel,
USBKBD_REPEAT_RATE
);
if ((Result & EFI_USB_ERR_STALL) == EFI_USB_ERR_STALL) {
UsbClearEndpointHalt (
UsbIo,
UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
&UsbStatus
);
}
//
// Delete & Submit this interrupt again
// Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.
//
UsbIo->UsbAsyncInterruptTransfer (
UsbIo,
UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
FALSE,
0,
0,
NULL,
NULL
);
//
// EFI_USB_INTERRUPT_DELAY is defined in USB standard for error handling.
//
gBS->SetTimer (
UsbKeyboardDevice->DelayedRecoveryEvent,
TimerRelative,
EFI_USB_INTERRUPT_DELAY
);
return EFI_DEVICE_ERROR;
}
//
// If no error and no data, just return EFI_SUCCESS.
//
if (DataLength == 0 || Data == NULL) {
return EFI_SUCCESS;
}
//
// Following code checks current keyboard input report against old key code buffer.
// According to USB HID Firmware Specification, the report consists of 8 bytes.
// Byte 0 is map of Modifier keys.
// Byte 1 is reserved.
// Bytes 2 to 7 are keycodes.
//
CurKeyCodeBuffer = (UINT8 *) Data;
OldKeyCodeBuffer = UsbKeyboardDevice->LastKeyCodeArray;
//
// Checks for new key stroke.
//
for (Index = 0; Index < 8; Index++) {
if (OldKeyCodeBuffer[Index] != CurKeyCodeBuffer[Index]) {
break;
}
}
//
// If no new key, return EFI_SUCCESS immediately.
//
if (Index == 8) {
return EFI_SUCCESS;
}
//
// Parse the modifier key, which is the first byte of keyboard input report.
//
CurModifierMap = CurKeyCodeBuffer[0];
OldModifierMap = OldKeyCodeBuffer[0];
//
// Handle modifier key's pressing or releasing situation.
// According to USB HID Firmware spec, Byte 0 uses folloing map of Modifier keys:
// Bit0: Left Control, Keycode: 0xe0
// Bit1: Left Shift, Keycode: 0xe1
// Bit2: Left Alt, Keycode: 0xe2
// Bit3: Left GUI, Keycode: 0xe3
// Bit4: Right Control, Keycode: 0xe4
// Bit5: Right Shift, Keycode: 0xe5
// Bit6: Right Alt, Keycode: 0xe6
// Bit7: Right GUI, Keycode: 0xe7
//
for (Index = 0; Index < 8; Index++) {
Mask = (UINT8) (1 << Index);
if ((CurModifierMap & Mask) != (OldModifierMap & Mask)) {
//
// If current modifier key is up, then CurModifierMap & Mask = 0;
// otherwise it is a non-zero value.
// Insert the changed modifier key into key buffer.
//
Down = (BOOLEAN) ((CurModifierMap & Mask) != 0);
InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), (UINT8) (0xe0 + Index), Down);
}
}
//
// Handle normal key's releasing situation
// Bytes 2 to 7 are for normal keycodes
//
KeyRelease = FALSE;
for (Index = 2; Index < 8; Index++) {
if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index])) {
continue;
}
//
// For any key in old keycode buffer, if it is not in current keycode buffer,
// then it is released. Otherwise, it is not released.
//
KeyRelease = TRUE;
for (Index2 = 2; Index2 < 8; Index2++) {
if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index2])) {
continue;
}
if (OldKeyCodeBuffer[Index] == CurKeyCodeBuffer[Index2]) {
KeyRelease = FALSE;
break;
}
}
if (KeyRelease) {
InsertKeyCode (
&(UsbKeyboardDevice->KeyboardBuffer),
OldKeyCodeBuffer[Index],
FALSE
);
//
// The original repeat key is released.
//
if (OldKeyCodeBuffer[Index] == UsbKeyboardDevice->RepeatKey) {
UsbKeyboardDevice->RepeatKey = 0;
}
}
}
//
// If original repeat key is released, cancel the repeat timer
//
if (UsbKeyboardDevice->RepeatKey == 0) {
gBS->SetTimer (
UsbKeyboardDevice->RepeatTimer,
TimerCancel,
USBKBD_REPEAT_RATE
);
}
//
// Handle normal key's pressing situation
//
KeyPress = FALSE;
for (Index = 2; Index < 8; Index++) {
if (!USBKBD_VALID_KEYCODE (CurKeyCodeBuffer[Index])) {
continue;
}
//
// For any key in current keycode buffer, if it is not in old keycode buffer,
// then it is pressed. Otherwise, it is not pressed.
//
KeyPress = TRUE;
for (Index2 = 2; Index2 < 8; Index2++) {
if (!USBKBD_VALID_KEYCODE (OldKeyCodeBuffer[Index2])) {
continue;
}
if (CurKeyCodeBuffer[Index] == OldKeyCodeBuffer[Index2]) {
KeyPress = FALSE;
break;
}
}
if (KeyPress) {
InsertKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), CurKeyCodeBuffer[Index], TRUE);
//
// Handle repeat key
//
KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, CurKeyCodeBuffer[Index]);
ASSERT (KeyDescriptor != NULL);
if (KeyDescriptor->Modifier == EFI_NUM_LOCK_MODIFIER || KeyDescriptor->Modifier == EFI_CAPS_LOCK_MODIFIER) {
//
// For NumLock or CapsLock pressed, there is no need to handle repeat key for them.
//
UsbKeyboardDevice->RepeatKey = 0;
} else {
//
// Prepare new repeat key, and clear the original one.
//
NewRepeatKey = CurKeyCodeBuffer[Index];
UsbKeyboardDevice->RepeatKey = 0;
}
}
}
//
// Update LastKeycodeArray buffer in the UsbKeyboardDevice data structure.
//
for (Index = 0; Index < 8; Index++) {
UsbKeyboardDevice->LastKeyCodeArray[Index] = CurKeyCodeBuffer[Index];
}
//
// Pre-process KeyboardBuffer to check if Ctrl + Alt + Del is pressed.
//
SavedTail = UsbKeyboardDevice->KeyboardBuffer.BufferTail;
Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead;
while (Index != SavedTail) {
RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);
ASSERT (KeyDescriptor != NULL);
switch (KeyDescriptor->Modifier) {
case EFI_LEFT_CONTROL_MODIFIER:
case EFI_RIGHT_CONTROL_MODIFIER:
if (UsbKey.Down) {
UsbKeyboardDevice->CtrlOn = TRUE;
} else {
UsbKeyboardDevice->CtrlOn = FALSE;
}
break;
case EFI_LEFT_ALT_MODIFIER:
case EFI_RIGHT_ALT_MODIFIER:
if (UsbKey.Down) {
UsbKeyboardDevice->AltOn = TRUE;
} else {
UsbKeyboardDevice->AltOn = FALSE;
}
break;
case EFI_ALT_GR_MODIFIER:
if (UsbKey.Down) {
UsbKeyboardDevice->AltGrOn = TRUE;
} else {
UsbKeyboardDevice->AltGrOn = FALSE;
}
break;
//
// For Del Key, check if Ctrl + Alt + Del occurs for reset.
//
case EFI_DELETE_MODIFIER:
if (UsbKey.Down) {
if ((UsbKeyboardDevice->CtrlOn) && (UsbKeyboardDevice->AltOn)) {
gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
}
break;
default:
break;
}
//
// Insert the key back to the buffer,
// so the key sequence will not be destroyed.
//
InsertKeyCode (
&(UsbKeyboardDevice->KeyboardBuffer),
UsbKey.KeyCode,
UsbKey.Down
);
Index = UsbKeyboardDevice->KeyboardBuffer.BufferHead;
}
//
// If there is new key pressed, update the RepeatKey value, and set the
// timer to repeate delay timer
//
if (NewRepeatKey != 0) {
//
// Sets trigger time to "Repeat Delay Time",
// to trigger the repeat timer when the key is hold long
// enough time.
//
gBS->SetTimer (
UsbKeyboardDevice->RepeatTimer,
TimerRelative,
USBKBD_REPEAT_DELAY
);
UsbKeyboardDevice->RepeatKey = NewRepeatKey;
}
return EFI_SUCCESS;
}
/**
Retrieves a USB keycode after parsing the raw data in keyboard buffer.
This function parses keyboard buffer. It updates state of modifier key for
USB_KB_DEV instancem, and returns keycode for output.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@param KeyCode Pointer to the USB keycode for output.
@retval EFI_SUCCESS Keycode successfully parsed.
@retval EFI_NOT_READY Keyboard buffer is not ready for a valid keycode
**/
EFI_STATUS
EFIAPI
USBParseKey (
IN OUT USB_KB_DEV *UsbKeyboardDevice,
OUT UINT8 *KeyCode
)
{
USB_KEY UsbKey;
EFI_KEY_DESCRIPTOR *KeyDescriptor;
*KeyCode = 0;
while (!IsUSBKeyboardBufferEmpty (&UsbKeyboardDevice->KeyboardBuffer)) {
//
// Pops one raw data off.
//
RemoveKeyCode (&(UsbKeyboardDevice->KeyboardBuffer), &UsbKey);
KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, UsbKey.KeyCode);
ASSERT (KeyDescriptor != NULL);
if (!UsbKey.Down) {
//
// Key is released.
//
switch (KeyDescriptor->Modifier) {
//
// Ctrl release
//
case EFI_LEFT_CONTROL_MODIFIER:
UsbKeyboardDevice->LeftCtrlOn = FALSE;
UsbKeyboardDevice->CtrlOn = FALSE;
break;
case EFI_RIGHT_CONTROL_MODIFIER:
UsbKeyboardDevice->RightCtrlOn = FALSE;
UsbKeyboardDevice->CtrlOn = FALSE;
break;
//
// Shift release
//
case EFI_LEFT_SHIFT_MODIFIER:
UsbKeyboardDevice->LeftShiftOn = FALSE;
UsbKeyboardDevice->ShiftOn = FALSE;
break;
case EFI_RIGHT_SHIFT_MODIFIER:
UsbKeyboardDevice->RightShiftOn = FALSE;
UsbKeyboardDevice->ShiftOn = FALSE;
break;
//
// Alt release
//
case EFI_LEFT_ALT_MODIFIER:
UsbKeyboardDevice->LeftAltOn = FALSE;
UsbKeyboardDevice->AltOn = FALSE;
break;
case EFI_RIGHT_ALT_MODIFIER:
UsbKeyboardDevice->RightAltOn = FALSE;
UsbKeyboardDevice->AltOn = FALSE;
break;
//
// Left Logo release
//
case EFI_LEFT_LOGO_MODIFIER:
UsbKeyboardDevice->LeftLogoOn = FALSE;
break;
//
// Right Logo release
//
case EFI_RIGHT_LOGO_MODIFIER:
UsbKeyboardDevice->RightLogoOn = FALSE;
break;
//
// Menu key release
//
case EFI_MENU_MODIFIER:
UsbKeyboardDevice->MenuKeyOn = FALSE;
break;
//
// SysReq release
//
case EFI_PRINT_MODIFIER:
case EFI_SYS_REQUEST_MODIFIER:
UsbKeyboardDevice->SysReqOn = FALSE;
break;
//
// AltGr release
//
case EFI_ALT_GR_MODIFIER:
UsbKeyboardDevice->AltGrOn = FALSE;
break;
default:
break;
}
continue;
}
//
// Analyzes key pressing situation
//
switch (KeyDescriptor->Modifier) {
//
// Ctrl press
//
case EFI_LEFT_CONTROL_MODIFIER:
UsbKeyboardDevice->LeftCtrlOn = TRUE;
UsbKeyboardDevice->CtrlOn = TRUE;
continue;
break;
case EFI_RIGHT_CONTROL_MODIFIER:
UsbKeyboardDevice->RightCtrlOn = TRUE;
UsbKeyboardDevice->CtrlOn = TRUE;
continue;
break;
//
// Shift press
//
case EFI_LEFT_SHIFT_MODIFIER:
UsbKeyboardDevice->LeftShiftOn = TRUE;
UsbKeyboardDevice->ShiftOn = TRUE;
continue;
break;
case EFI_RIGHT_SHIFT_MODIFIER:
UsbKeyboardDevice->RightShiftOn = TRUE;
UsbKeyboardDevice->ShiftOn = TRUE;
continue;
break;
//
// Alt press
//
case EFI_LEFT_ALT_MODIFIER:
UsbKeyboardDevice->LeftAltOn = TRUE;
UsbKeyboardDevice->AltOn = TRUE;
continue;
break;
case EFI_RIGHT_ALT_MODIFIER:
UsbKeyboardDevice->RightAltOn = TRUE;
UsbKeyboardDevice->AltOn = TRUE;
continue;
break;
//
// Left Logo press
//
case EFI_LEFT_LOGO_MODIFIER:
UsbKeyboardDevice->LeftLogoOn = TRUE;
break;
//
// Right Logo press
//
case EFI_RIGHT_LOGO_MODIFIER:
UsbKeyboardDevice->RightLogoOn = TRUE;
break;
//
// Menu key press
//
case EFI_MENU_MODIFIER:
UsbKeyboardDevice->MenuKeyOn = TRUE;
break;
//
// SysReq press
//
case EFI_PRINT_MODIFIER:
case EFI_SYS_REQUEST_MODIFIER:
UsbKeyboardDevice->SysReqOn = TRUE;
continue;
break;
//
// AltGr press
//
case EFI_ALT_GR_MODIFIER:
UsbKeyboardDevice->AltGrOn = TRUE;
break;
case EFI_NUM_LOCK_MODIFIER:
//
// Toggle NumLock
//
UsbKeyboardDevice->NumLockOn = (BOOLEAN) (!(UsbKeyboardDevice->NumLockOn));
SetKeyLED (UsbKeyboardDevice);
continue;
break;
case EFI_CAPS_LOCK_MODIFIER:
//
// Toggle CapsLock
//
UsbKeyboardDevice->CapsOn = (BOOLEAN) (!(UsbKeyboardDevice->CapsOn));
SetKeyLED (UsbKeyboardDevice);
continue;
break;
case EFI_SCROLL_LOCK_MODIFIER:
//
// Toggle ScrollLock
//
UsbKeyboardDevice->ScrollOn = (BOOLEAN) (!(UsbKeyboardDevice->ScrollOn));
SetKeyLED (UsbKeyboardDevice);
continue;
break;
//
// PrintScreen, Pause/Break could not be retrieved via SimpleTextInEx protocol
//
case EFI_PAUSE_MODIFIER:
case EFI_BREAK_MODIFIER:
//
// Fall through
//
continue;
break;
default:
break;
}
//
// When encountering Ctrl + Alt + Del, then warm reset.
//
if (KeyDescriptor->Modifier == EFI_DELETE_MODIFIER) {
if ((UsbKeyboardDevice->CtrlOn) && (UsbKeyboardDevice->AltOn)) {
gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
}
*KeyCode = UsbKey.KeyCode;
return EFI_SUCCESS;
}
return EFI_NOT_READY;
}
/**
Converts USB Keycode ranging from 0x4 to 0x65 to EFI_INPUT_KEY.
@param UsbKeyboardDevice The USB_KB_DEV instance.
@param KeyCode Indicates the key code that will be interpreted.
@param Key A pointer to a buffer that is filled in with
the keystroke information for the key that
was pressed.
@retval EFI_SUCCESS Success.
@retval EFI_INVALID_PARAMETER KeyCode is not in the range of 0x4 to 0x65.
@retval EFI_INVALID_PARAMETER Translated EFI_INPUT_KEY has zero for both ScanCode and UnicodeChar.
@retval EFI_NOT_READY KeyCode represents a dead key with EFI_NS_KEY_MODIFIER
@retval EFI_DEVICE_ERROR Keyboard layout is invalid.
**/
EFI_STATUS
EFIAPI
UsbKeyCodeToEfiInputKey (
IN USB_KB_DEV *UsbKeyboardDevice,
IN UINT8 KeyCode,
OUT EFI_INPUT_KEY *Key
)
{
EFI_KEY_DESCRIPTOR *KeyDescriptor;
//
// KeyCode must in the range of 0x4 to 0x65
//
if (!USBKBD_VALID_KEYCODE (KeyCode)) {
return EFI_INVALID_PARAMETER;
}
if ((KeyCode - 4) >= NUMBER_OF_VALID_NON_MODIFIER_USB_KEYCODE) {
return EFI_INVALID_PARAMETER;
}
KeyDescriptor = GetKeyDescriptor (UsbKeyboardDevice, KeyCode);
ASSERT (KeyDescriptor != NULL);
if (KeyDescriptor->Modifier == EFI_NS_KEY_MODIFIER) {
//
// If this is a dead key with EFI_NS_KEY_MODIFIER, then record it and return.
//
UsbKeyboardDevice->CurrentNsKey = FindUsbNsKey (UsbKeyboardDevice, KeyDescriptor);
return EFI_NOT_READY;
}
if (UsbKeyboardDevice->CurrentNsKey != NULL) {
//
// If this keystroke follows a non-spacing key, then find the descriptor for corresponding
// physical key.
//
KeyDescriptor = FindPhysicalKey (UsbKeyboardDevice->CurrentNsKey, KeyDescriptor);
UsbKeyboardDevice->CurrentNsKey = NULL;
}
//
// Make sure modifier of Key Descriptor is in the valid range according to UEFI spec.
//
if (KeyDescriptor->Modifier > EFI_FUNCTION_KEY_TWELVE_MODIFIER) {
return EFI_DEVICE_ERROR;
}
Key->ScanCode = ModifierValueToEfiScanCodeConvertionTable[KeyDescriptor->Modifier];
Key->UnicodeChar = KeyDescriptor->Unicode;
if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_STANDARD_SHIFT)!= 0) {
if (UsbKeyboardDevice->ShiftOn) {
Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;
//
// Need not return associated shift state if a class of printable characters that
// are normally adjusted by shift modifiers. e.g. Shift Key + 'f' key = 'F'
//
if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {
UsbKeyboardDevice->LeftShiftOn = FALSE;
UsbKeyboardDevice->RightShiftOn = FALSE;
}
if (UsbKeyboardDevice->AltGrOn) {
Key->UnicodeChar = KeyDescriptor->ShiftedAltGrUnicode;
}
} else {
//
// Shift off
//
Key->UnicodeChar = KeyDescriptor->Unicode;
if (UsbKeyboardDevice->AltGrOn) {
Key->UnicodeChar = KeyDescriptor->AltGrUnicode;
}
}
}
if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_CAPS_LOCK) != 0) {
if (UsbKeyboardDevice->CapsOn) {
if (Key->UnicodeChar == KeyDescriptor->Unicode) {
Key->UnicodeChar = KeyDescriptor->ShiftedUnicode;
} else if (Key->UnicodeChar == KeyDescriptor->ShiftedUnicode) {
Key->UnicodeChar = KeyDescriptor->Unicode;
}
}
}
//
// Translate the CTRL-Alpha characters to their corresponding control value
// (ctrl-a = 0x0001 through ctrl-Z = 0x001A)
//
if (UsbKeyboardDevice->CtrlOn) {
if (Key->UnicodeChar >= 'a' && Key->UnicodeChar <= 'z') {
Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'a' + 1);
} else if (Key->UnicodeChar >= 'A' && Key->UnicodeChar <= 'Z') {
Key->UnicodeChar = (UINT8) (Key->UnicodeChar - 'A' + 1);
}
}
if ((KeyDescriptor->AffectedAttribute & EFI_AFFECTED_BY_NUM_LOCK) != 0) {
//
// For key affected by NumLock, if NumLock is on and Shift is not pressed, then it means
// normal key, instead of original control key. So the ScanCode should be cleaned.
// Otherwise, it means control key, so preserve the EFI Scan Code and clear the unicode keycode.
//
if ((UsbKeyboardDevice->NumLockOn) && (!(UsbKeyboardDevice->ShiftOn))) {
Key->ScanCode = SCAN_NULL;
} else {
Key->UnicodeChar = 0x00;
}
}
//
// Translate Unicode 0x1B (ESC) to EFI Scan Code
//
if (Key->UnicodeChar == 0x1B && Key->ScanCode == SCAN_NULL) {
Key->ScanCode = SCAN_ESC;
Key->UnicodeChar = 0x00;
}
//
// Not valid for key without both unicode key code and EFI Scan Code.
//
if (Key->UnicodeChar == 0 && Key->ScanCode == SCAN_NULL) {
return EFI_NOT_READY;
}
//
// Save Shift/Toggle state
//
if (UsbKeyboardDevice->LeftCtrlOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_CONTROL_PRESSED;
}
if (UsbKeyboardDevice->RightCtrlOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_CONTROL_PRESSED;
}
if (UsbKeyboardDevice->LeftAltOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_ALT_PRESSED;
}
if (UsbKeyboardDevice->RightAltOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_ALT_PRESSED;
}
if (UsbKeyboardDevice->LeftShiftOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_SHIFT_PRESSED;
}
if (UsbKeyboardDevice->RightShiftOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_SHIFT_PRESSED;
}
if (UsbKeyboardDevice->LeftLogoOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_LEFT_LOGO_PRESSED;
}
if (UsbKeyboardDevice->RightLogoOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_RIGHT_LOGO_PRESSED;
}
if (UsbKeyboardDevice->MenuKeyOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_MENU_KEY_PRESSED;
}
if (UsbKeyboardDevice->SysReqOn) {
UsbKeyboardDevice->KeyState.KeyShiftState |= EFI_SYS_REQ_PRESSED;
}
if (UsbKeyboardDevice->ScrollOn) {
UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_SCROLL_LOCK_ACTIVE;
}
if (UsbKeyboardDevice->NumLockOn) {
UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_NUM_LOCK_ACTIVE;
}
if (UsbKeyboardDevice->CapsOn) {
UsbKeyboardDevice->KeyState.KeyToggleState |= EFI_CAPS_LOCK_ACTIVE;
}
return EFI_SUCCESS;
}
/**
Resets USB keyboard buffer.
@param KeyboardBuffer Points to the USB keyboard buffer.
**/
VOID
EFIAPI
InitUSBKeyBuffer (
OUT USB_KB_BUFFER *KeyboardBuffer
)
{
ZeroMem (KeyboardBuffer, sizeof (USB_KB_BUFFER));
KeyboardBuffer->BufferHead = KeyboardBuffer->BufferTail;
}
/**
Check whether USB keyboard buffer is empty.
@param KeyboardBuffer USB keyboard buffer
@retval TRUE Keyboard buffer is empty.
@retval FALSE Keyboard buffer is not empty.
**/
BOOLEAN
EFIAPI
IsUSBKeyboardBufferEmpty (
IN USB_KB_BUFFER *KeyboardBuffer
)
{
//
// Meet FIFO empty condition
//
return (BOOLEAN) (KeyboardBuffer->BufferHead == KeyboardBuffer->BufferTail);
}
/**
Check whether USB keyboard buffer is full.
@param KeyboardBuffer USB keyboard buffer
@retval TRUE Keyboard buffer is full.
@retval FALSE Keyboard buffer is not full.
**/
BOOLEAN
EFIAPI
IsUSBKeyboardBufferFull (
IN USB_KB_BUFFER *KeyboardBuffer
)
{
return (BOOLEAN)(((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1)) == KeyboardBuffer->BufferHead);
}
/**
Inserts a keycode into keyboard buffer.
@param KeyboardBuffer Points to the USB keyboard buffer.
@param Key Keycode to insert.
@param Down TRUE means key is pressed.
FALSE means key is released.
**/
VOID
EFIAPI
InsertKeyCode (
IN OUT USB_KB_BUFFER *KeyboardBuffer,
IN UINT8 Key,
IN BOOLEAN Down
)
{
USB_KEY UsbKey;
//
// If keyboard buffer is full, throw the
// first key out of the keyboard buffer.
//
if (IsUSBKeyboardBufferFull (KeyboardBuffer)) {
RemoveKeyCode (KeyboardBuffer, &UsbKey);
}
ASSERT (KeyboardBuffer->BufferTail <= MAX_KEY_ALLOWED);
KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].KeyCode = Key;
KeyboardBuffer->Buffer[KeyboardBuffer->BufferTail].Down = Down;
//
// Adjust the tail pointer of the FIFO keyboard buffer.
//
KeyboardBuffer->BufferTail = (UINT8) ((KeyboardBuffer->BufferTail + 1) % (MAX_KEY_ALLOWED + 1));
}
/**
Remove a keycode from keyboard buffer and return it.
@param KeyboardBuffer Points to the USB keyboard buffer.
@param UsbKey Points to the buffer that contains keycode for output.
@retval EFI_SUCCESS Keycode successfully removed from keyboard buffer.
@retval EFI_DEVICE_ERROR Keyboard buffer is empty.
**/
EFI_STATUS
EFIAPI
RemoveKeyCode (
IN OUT USB_KB_BUFFER *KeyboardBuffer,
OUT USB_KEY *UsbKey
)
{
if (IsUSBKeyboardBufferEmpty (KeyboardBuffer)) {
return EFI_DEVICE_ERROR;
}
ASSERT (KeyboardBuffer->BufferHead <= MAX_KEY_ALLOWED);
UsbKey->KeyCode = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].KeyCode;
UsbKey->Down = KeyboardBuffer->Buffer[KeyboardBuffer->BufferHead].Down;
//
// Adjust the head pointer of the FIFO keyboard buffer.
//
KeyboardBuffer->BufferHead = (UINT8) ((KeyboardBuffer->BufferHead + 1) % (MAX_KEY_ALLOWED + 1));
return EFI_SUCCESS;
}
/**
Sets USB keyboard LED state.
@param UsbKeyboardDevice The USB_KB_DEV instance.
**/
VOID
EFIAPI
SetKeyLED (
IN USB_KB_DEV *UsbKeyboardDevice
)
{
LED_MAP Led;
UINT8 ReportId;
//
// Set each field in Led map.
//
Led.NumLock = (UINT8) ((UsbKeyboardDevice->NumLockOn) ? 1 : 0);
Led.CapsLock = (UINT8) ((UsbKeyboardDevice->CapsOn) ? 1 : 0);
Led.ScrollLock = (UINT8) ((UsbKeyboardDevice->ScrollOn) ? 1 : 0);
Led.Resrvd = 0;
ReportId = 0;
//
// Call Set_Report Request to lighten the LED.
//
UsbSetReportRequest (
UsbKeyboardDevice->UsbIo,
UsbKeyboardDevice->InterfaceDescriptor.InterfaceNumber,
ReportId,
HID_OUTPUT_REPORT,
1,
(UINT8 *) &Led
);
}
/**
Handler for Repeat Key event.
This function is the handler for Repeat Key event triggered
by timer.
After a repeatable key is pressed, the event would be triggered
with interval of USBKBD_REPEAT_DELAY. Once the event is triggered,
following trigger will come with interval of USBKBD_REPEAT_RATE.
@param Event The Repeat Key event.
@param Context Points to the USB_KB_DEV instance.
**/
VOID
EFIAPI
USBKeyboardRepeatHandler (
IN EFI_EVENT Event,
IN VOID *Context
)
{
USB_KB_DEV *UsbKeyboardDevice;
UsbKeyboardDevice = (USB_KB_DEV *) Context;
//
// Do nothing when there is no repeat key.
//
if (UsbKeyboardDevice->RepeatKey != 0) {
//
// Inserts the repeat key into keyboard buffer,
//
InsertKeyCode (
&(UsbKeyboardDevice->KeyboardBuffer),
UsbKeyboardDevice->RepeatKey,
TRUE
);
//
// Set repeat rate for next repeat key generation.
//
gBS->SetTimer (
UsbKeyboardDevice->RepeatTimer,
TimerRelative,
USBKBD_REPEAT_RATE
);
}
}
/**
Handler for Delayed Recovery event.
This function is the handler for Delayed Recovery event triggered
by timer.
After a device error occurs, the event would be triggered
with interval of EFI_USB_INTERRUPT_DELAY. EFI_USB_INTERRUPT_DELAY
is defined in USB standard for error handling.
@param Event The Delayed Recovery event.
@param Context Points to the USB_KB_DEV instance.
**/
VOID
EFIAPI
USBKeyboardRecoveryHandler (
IN EFI_EVENT Event,
IN VOID *Context
)
{
USB_KB_DEV *UsbKeyboardDevice;
EFI_USB_IO_PROTOCOL *UsbIo;
UINT8 PacketSize;
UsbKeyboardDevice = (USB_KB_DEV *) Context;
UsbIo = UsbKeyboardDevice->UsbIo;
PacketSize = (UINT8) (UsbKeyboardDevice->IntEndpointDescriptor.MaxPacketSize);
//
// Re-submit Asynchronous Interrupt Transfer for recovery.
//
UsbIo->UsbAsyncInterruptTransfer (
UsbIo,
UsbKeyboardDevice->IntEndpointDescriptor.EndpointAddress,
TRUE,
UsbKeyboardDevice->IntEndpointDescriptor.Interval,
PacketSize,
KeyboardHandler,
UsbKeyboardDevice
);
}
|