|
root / base / usr / src / uts / common / io / kb8042
kb8042 Plain Text 3228 lines 80.8 KB
   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
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/kbd.h>
#include <sys/kbtrans.h>
#include <sys/sunddi.h>
#include <sys/consdev.h>
#include <sys/promif.h>
#include "kb8042.h"

/*
 * A note on the use of prom_printf here:  Most of these routines can be
 * called from "polled mode", where we're servicing I/O requests from kmdb.
 * Normal system services are not available from polled mode; cmn_err will
 * not work.  prom_printf is the only safe output mechanism.
 */

#define	KEYBAD		0xff		/* should generate an error */
#define	KEYIGN		0xfe		/* ignore this sequence */

#define	KEY(code)	(code)
#define	INVALID		KEYBAD
#define	IGNORE		KEYIGN

#define	NELEM(a)	(sizeof (a) / sizeof (a)[0])

/*
 * These are the states of our parsing machine:
 */
#define	STATE_IDLE	0x00000001 /* Awaiting the start of a sequence */
#define	STATE_E0	0x00000002 /* Rec'd an E0 */
#define	STATE_E1	0x00000004 /* Rec'd an E1 (Pause key only) */
#define	STATE_E1_1D	0x00000008 /* Rec'd an E1 1D (Pause key only) */
#define	STATE_E1_14	0x00000010 /* Rec'd an E1 14 (Pause key only) */
#define	STATE_E1_14_77			0x00000020
#define	STATE_E1_14_77_E1		0x00000040
#define	STATE_E1_14_77_E1_F0		0x00000080
#define	STATE_E1_14_77_E1_F0_14		0x00000100
#define	STATE_E1_14_77_E1_F0_14_F0	0x00000200

static boolean_t KeyboardConvertScan_set1(struct kb8042	*, unsigned char, int *,
    enum keystate *, boolean_t *);
static boolean_t KeyboardConvertScan_set2(struct kb8042	*, unsigned char, int *,
    enum keystate *, boolean_t *);

static const unsigned char *keytab_base = NULL;
static int keytab_base_length = 0;
static const unsigned char *keytab_e0 = NULL;
static int keytab_e0_length = 0;
static boolean_t (*KeyboardConvertScan_fn)(struct kb8042 *, unsigned char,
    int *, enum keystate *, boolean_t *) = NULL;

static const unsigned char	keytab_base_set1[] = {
/* scan		key number	keycap */
/* 00 */	INVALID,
/* 01 */	KEY(110),	/* Esc */
/* 02 */	KEY(2),		/* 1 */
/* 03 */	KEY(3),		/* 2 */
/* 04 */	KEY(4),		/* 3 */
/* 05 */	KEY(5),		/* 4 */
/* 06 */	KEY(6),		/* 5 */
/* 07 */	KEY(7),		/* 6 */
/* 08 */	KEY(8),		/* 7 */
/* 09 */	KEY(9),		/* 8 */
/* 0a */	KEY(10),	/* 9 */
/* 0b */	KEY(11),	/* 0 */
/* 0c */	KEY(12),	/* - */
/* 0d */	KEY(13),	/* = */
/* 0e */	KEY(15),	/* backspace */
/* 0f */	KEY(16),	/* tab */

/* 10 */	KEY(17),	/* Q */
/* 11 */	KEY(18),	/* W */
/* 12 */	KEY(19),	/* E */
/* 13 */	KEY(20),	/* R */
/* 14 */	KEY(21),	/* T */
/* 15 */	KEY(22),	/* Y */
/* 16 */	KEY(23),	/* U */
/* 17 */	KEY(24),	/* I */
/* 18 */	KEY(25),	/* O */
/* 19 */	KEY(26),	/* P */
/* 1a */	KEY(27),	/* [ */
/* 1b */	KEY(28),	/* ] */
/* 1c */	KEY(43),	/* Enter (main) */
/* 1d */	KEY(58),	/* L Ctrl */
/* 1e */	KEY(31),	/* A */
/* 1f */	KEY(32),	/* S */

/* 20 */	KEY(33),	/* D */
/* 21 */	KEY(34),	/* F */
/* 22 */	KEY(35),	/* G */
/* 23 */	KEY(36),	/* H */
/* 24 */	KEY(37),	/* J */
/* 25 */	KEY(38),	/* K */
/* 26 */	KEY(39),	/* L */
/* 27 */	KEY(40),	/* ; */
/* 28 */	KEY(41),	/* ' */
/* 29 */	KEY(1),		/* ` */
/* 2a */	KEY(44),	/* L Shift */
/* 2b */	KEY(29),	/* \ */
/* 2c */	KEY(46),	/* Z */
/* 2d */	KEY(47),	/* X */
/* 2e */	KEY(48),	/* C */
/* 2f */	KEY(49),	/* V */

/* 30 */	KEY(50),	/* B */
/* 31 */	KEY(51),	/* N */
/* 32 */	KEY(52),	/* M */
/* 33 */	KEY(53),	/* , */
/* 34 */	KEY(54),	/* . */
/* 35 */	KEY(55),	/* / */
/* 36 */	KEY(57),	/* R Shift */
/* 37 */	KEY(100),	/* * (num) */
/* 38 */	KEY(60),	/* L Alt */
/* 39 */	KEY(61),	/* Space */
/* 3a */	KEY(30),	/* CapsLock */
/* 3b */	KEY(112),	/* F1 */
/* 3c */	KEY(113),	/* F2 */
/* 3d */	KEY(114),	/* F3 */
/* 3e */	KEY(115),	/* F4 */
/* 3f */	KEY(116),	/* F5 */

/* 40 */	KEY(117),	/* F6 */
/* 41 */	KEY(118),	/* F7 */
/* 42 */	KEY(119),	/* F8 */
/* 43 */	KEY(120),	/* F9 */
/* 44 */	KEY(121),	/* F10 */
/* 45 */	KEY(90),	/* NumLock */
/* 46 */	KEY(125),	/* Scroll Lock */
/* 47 */	KEY(91),	/* 7 (num) */
/* 48 */	KEY(96),	/* 8 (num) */
/* 49 */	KEY(101),	/* 9 (num) */
/* 4a */	KEY(105),	/* - (num) */
/* 4b */	KEY(92),	/* 4 (num) */
/* 4c */	KEY(97),	/* 5 (num) */
/* 4d */	KEY(102),	/* 6 (num) */
/* 4e */	KEY(106),	/* + (num) */
/* 4f */	KEY(93),	/* 1 (num) */

/* 50 */	KEY(98),	/* 2 (num) */
/* 51 */	KEY(103),	/* 3 (num) */
/* 52 */	KEY(99),	/* 0 (num) */
/* 53 */	KEY(104),	/* . (num) */
/* 54 */	KEY(124),	/* PrintScreen (with Alt) */
/* 55 */	INVALID,
/* 56 */	KEY(45),	/* not labled (102-key only) */
/* 57 */	KEY(122),	/* F11 */
/* 58 */	KEY(123),	/* F12 */
/* 59 */	INVALID,
/* 5a */	INVALID,
/* 5b */	INVALID,
/* 5c */	INVALID,
/* 5d */	INVALID,
/* 5e */	INVALID,
/* 5f */	INVALID,

/* 60 */	INVALID,
/* 61 */	INVALID,
/* 62 */	INVALID,
/* 63 */	INVALID,
/* 64 */	INVALID,
/* 65 */	INVALID,
/* 66 */	INVALID,
/* 67 */	INVALID,
/* 68 */	INVALID,
/* 69 */	INVALID,
/* 6a */	INVALID,
/* 6b */	INVALID,
/* 6c */	INVALID,
/* 6d */	INVALID,
/* 6e */	INVALID,
/* 6f */	INVALID,

/* 70 */	KEY(133),	/* Japanese 106-key keyboard */
/* 71 */	INVALID,
/* 72 */	INVALID,
/* 73 */	KEY(56),	/* Japanese 106-key keyboard */
/* 74 */	INVALID,
/* 75 */	INVALID,
/* 76 */	INVALID,
/* 77 */	INVALID,
/* 78 */	INVALID,
/* 79 */	KEY(132),	/* Japanese 106-key keyboard */
/* 7a */	INVALID,
/* 7b */	KEY(131),	/* Japanese 106-key keyboard */
/* 7c */	INVALID,
/* 7d */	KEY(14),	/* Japanese 106-key keyboard */
/* 7e */	INVALID,
/* 7f */	INVALID,
};

/*
 * Parse table after receiving an E0 prefix code.
 *
 * Generally speaking, keys that were added on the 101-key keyboard are
 * represented as an E0 followed by the code for an 84-key key.  Software
 * ignorant of the 101-key keyboard ignores the E0 and so is handled
 * compatibly.  Many of these variants involve "fake" shift presses
 * and releases for compatibility; these are also prefixed with E0.
 * We ignore these fake shifts.
 */
static const unsigned char	keytab_e0_set1[] = {
/* 00 */	INVALID,
/* 01 */	INVALID,
/* 02 */	INVALID,
/* 03 */	INVALID,
/* 04 */	INVALID,
/* 05 */	INVALID,
/* 06 */	INVALID,
/* 07 */	INVALID,
/* 08 */	INVALID,
/* 09 */	INVALID,
/* 0a */	INVALID,
/* 0b */	INVALID,
/* 0c */	INVALID,
/* 0d */	INVALID,
/* 0e */	INVALID,
/* 0f */	INVALID,

/* 10 */	INVALID,
/* 11 */	INVALID,
/* 12 */	INVALID,
/* 13 */	INVALID,
/* 14 */	INVALID,
/* 15 */	INVALID,
/* 16 */	INVALID,
/* 17 */	INVALID,
/* 18 */	INVALID,
/* 19 */	INVALID,
/* 1a */	INVALID,
/* 1b */	INVALID,
/* 1c */	KEY(108),	/* Enter (num) */
/* 1d */	KEY(64),	/* R Ctrl */
/* 1e */	INVALID,
/* 1f */	INVALID,

/* 20 */	KEY(235),	/* Mute */
/* 21 */	INVALID,
/* 22 */	INVALID,
/* 23 */	INVALID,
/* 24 */	INVALID,
/* 25 */	INVALID,
/* 26 */	INVALID,
/* 27 */	INVALID,
/* 28 */	INVALID,
/* 29 */	INVALID,
/* 2a */	INVALID,
/* 2b */	INVALID,
/* 2c */	INVALID,
/* 2d */	INVALID,
/* 2e */	KEY(234),	/* Volume Down */
/* 2f */	INVALID,

/* 30 */	KEY(233),	/* Volume Up */
/* 31 */	INVALID,
/* 32 */	INVALID,
/* 33 */	INVALID,
/* 34 */	INVALID,
/* 35 */	KEY(95),	/* / (num) */
/* 36 */	INVALID,
/* 37 */	KEY(124),	/* PrintScreen (no Alt) */
/* 38 */	KEY(62),	/* R Alt */
/* 39 */	INVALID,
/* 3a */	INVALID,
/* 3b */	INVALID,
/* 3c */	INVALID,
/* 3d */	INVALID,
/* 3e */	INVALID,
/* 3f */	INVALID,

/* 40 */	INVALID,
/* 41 */	INVALID,
/* 42 */	INVALID,
/* 43 */	INVALID,
/* 44 */	INVALID,
/* 45 */	INVALID,
/* 46 */	KEY(126),	/* Pause (with Cntl) */
/* 47 */	KEY(80),	/* Home (arrow) */
/* 48 */	KEY(83),	/* Up (arrow) */
/* 49 */	KEY(85),	/* PgUp (arrow) */
/* 4a */	INVALID,
/* 4b */	KEY(79),	/* Left (arrow) */
/* 4c */	INVALID,
/* 4d */	KEY(89),	/* Right (arrow) */
/* 4e */	INVALID,
/* 4f */	KEY(81),	/* End (arrow) */

/* 50 */	KEY(84),	/* Down (arrow) */
/* 51 */	KEY(86),	/* PgDn (arrow) */
/* 52 */	KEY(75),	/* Insert (arrow) */
/* 53 */	KEY(76),	/* Delete (arrow) */
/* 54 */	INVALID,
/* 55 */	INVALID,
/* 56 */	INVALID,
/* 57 */	INVALID,
/* 58 */	INVALID,
/* 59 */	INVALID,
/* 5a */	INVALID,
/* 5b */	KEY(59),	/* L Window (104-key) */
/* 5c */	KEY(63),	/* R Window (104-key) */
/* 5d */	KEY(65),	/* Menu (104-key) */
/* 5e */	INVALID,
/* 5f */	INVALID,

/* 60 */	INVALID,
/* 61 */	INVALID,
/* 62 */	INVALID,
/* 63 */	INVALID,
/* 64 */	INVALID,
/* 65 */	INVALID,
/* 66 */	INVALID,
/* 67 */	INVALID,
/* 68 */	INVALID,
/* 69 */	INVALID,
/* 6a */	INVALID,
/* 6b */	INVALID,
/* 6c */	INVALID,
/* 6d */	INVALID,
/* 6e */	INVALID,
/* 6f */	INVALID,

/* 70 */	INVALID,
/* 71 */	INVALID,
/* 72 */	INVALID,
/* 73 */	INVALID,
/* 74 */	INVALID,
/* 75 */	INVALID,
/* 76 */	INVALID,
/* 77 */	INVALID,
/* 78 */	INVALID,
/* 79 */	INVALID,
/* 7a */	INVALID,
/* 7b */	INVALID,
/* 7c */	INVALID,
/* 7d */	INVALID,
/* 7e */	INVALID,
};


/*
 *	Parse table for the base keyboard state.  The index is the start of
 *	a new sequence.
 *
 * Questionable or unusual cases:
 * 02		On some SPARC keyboards, this is the scan code for the STOP
 *		key.  The KEY() value was chosen so that it maps to a
 *		HOLE entry in the keytables in kb8042_keytables.c; therefore,
 *		the STOP key code is only translated properly when kb8042
 *		is "emulating" a USB keyboard (which it is by default--
 *		see conskbd.c).
 * 7f		Old kd code says this is an 84-key SysReq.  Manual says no.
 * 87		Old kd code says 1 (num).  Manual says no.
 * 8c		Old kd code says / (num).  Manual says no.
 * aa		POST OK.  Handled by code.
 * e0		Extend prefix.  Handled by code. (switches to E0 table)
 * e1		Extend prefix.  Handled by code.  (Pause key only)
 * f0		Break prefix.  Handled by code.
 * f1		Korean Hangul/Hanja key.  Handled by code.
 * f2		Korean Hangul key.  Handled by code.
 * ff		Keyboard internal buffer overrun.  Handled by code.
 *
 * Other values past the end of the table are treated as INVALID.
 */

static const unsigned char	keytab_base_set2[] = {
/* scan		state		keycap */
/* 00 */	INVALID,
/* 01 */	KEY(120),	/* F9 */
#if defined(__sparc)
/* 02 */	KEY(K8042_STOP), /* STOP */
#else
/* 02 */	INVALID,	/* F7?  Old code says so but manual doesn't */
#endif
/* 03 */	KEY(116),	/* F5 */
/* 04 */	KEY(114),	/* F3 */
/* 05 */	KEY(112),	/* F1 */
/* 06 */	KEY(113),	/* F2 */
/* 07 */	KEY(123),	/* F12 */
/* 08 */	INVALID,
/* 09 */	KEY(121),	/* F10 */
/* 0a */	KEY(119),	/* F8 */
/* 0b */	KEY(117),	/* F6 */
/* 0c */	KEY(115),	/* F4 */
/* 0d */	KEY(16),	/* tab */
/* 0e */	KEY(1),		/* ` */
/* 0f */	INVALID,
/* 10 */	INVALID,
/* 11 */	KEY(60),	/* L Alt */
/* 12 */	KEY(44),	/* L Shift */
/* 13 */	KEY(133),	/* Japanese 106-key */
/* 14 */	KEY(58),	/* L Ctrl */
/* 15 */	KEY(17),	/* Q */
/* 16 */	KEY(2),		/* 1 */
/* 17 */	INVALID,
/* 18 */	INVALID,
/* 19 */	INVALID,
/* 1a */	KEY(46),	/* Z */
/* 1b */	KEY(32),	/* S */
/* 1c */	KEY(31),	/* A */
/* 1d */	KEY(18),	/* W */
/* 1e */	KEY(3),		/* 2 */
/* 1f */	INVALID,
/* 20 */	INVALID,
/* 21 */	KEY(48),	/* C */
/* 22 */	KEY(47),	/* X */
/* 23 */	KEY(33),	/* D */
/* 24 */	KEY(19),	/* E */
/* 25 */	KEY(5),		/* 4 */
/* 26 */	KEY(4),		/* 3 */
/* 27 */	INVALID,
/* 28 */	INVALID,
/* 29 */	KEY(61),	/* Space */
/* 2a */	KEY(49),	/* V */
/* 2b */	KEY(34),	/* F */
/* 2c */	KEY(21),	/* T */
/* 2d */	KEY(20),	/* R */
/* 2e */	KEY(6),		/* 5 */
/* 2f */	INVALID,
/* 30 */	INVALID,
/* 31 */	KEY(51),	/* N */
/* 32 */	KEY(50),	/* B */
/* 33 */	KEY(36),	/* H */
/* 34 */	KEY(35),	/* G */
/* 35 */	KEY(22),	/* Y */
/* 36 */	KEY(7),		/* 6 */
/* 37 */	INVALID,
/* 38 */	INVALID,
/* 39 */	INVALID,
/* 3a */	KEY(52),	/* M */
/* 3b */	KEY(37),	/* J */
/* 3c */	KEY(23),	/* U */
/* 3d */	KEY(8),		/* 7 */
/* 3e */	KEY(9),		/* 8 */
/* 3f */	INVALID,
/* 40 */	INVALID,
/* 41 */	KEY(53),	/* , */
/* 42 */	KEY(38),	/* K */
/* 43 */	KEY(24),	/* I */
/* 44 */	KEY(25),	/* O */
/* 45 */	KEY(11),	/* 0 */
/* 46 */	KEY(10),	/* 9 */
/* 47 */	INVALID,
/* 48 */	INVALID,
/* 49 */	KEY(54),	/* . */
/* 4a */	KEY(55),	/* / */
/* 4b */	KEY(39),	/* L */
/* 4c */	KEY(40),	/* ; */
/* 4d */	KEY(26),	/* P */
/* 4e */	KEY(12),	/* - */
/* 4f */	INVALID,
/* 50 */	INVALID,
/* 51 */	KEY(56),	/* Japanese 106-key */
/* 52 */	KEY(41),	/* ' */
/* 53 */	INVALID,
/* 54 */	KEY(27),	/* [ */
/* 55 */	KEY(13),	/* = */
/* 56 */	INVALID,
/* 57 */	INVALID,
/* 58 */	KEY(30),	/* CapsLock */
/* 59 */	KEY(57),	/* R Shift */
/* 5a */	KEY(43),	/* Enter (main) */
/* 5b */	KEY(28),	/* ] */
/* 5c */	INVALID,
/* 5d */	KEY(29),	/* \, key 42 for 102-key */
/* 5e */	INVALID,
/* 5f */	INVALID,
/* 60 */	INVALID,
/* 61 */	KEY(45),	/* 102-key only, typically </> */
/* 62 */	INVALID,
/* 63 */	INVALID,
/* 64 */	KEY(132),	/* Japanese 106-key */
/* 65 */	INVALID,
/* 66 */	KEY(15),	/* backspace */
/* 67 */	KEY(131),	/* Japanese 106-key */
/* 68 */	INVALID,
/* 69 */	KEY(93),	/* 1 (num) */
/* 6a */	KEY(14),	/* Japanese 106-key */
/* 6b */	KEY(92),	/* 4 (num) */
/* 6c */	KEY(91),	/* 7 (num) */
/* 6d */	INVALID,
/* 6e */	INVALID,
/* 6f */	INVALID,
/* 70 */	KEY(99),	/* 0 (num) */
/* 71 */	KEY(104),	/* . (num) */
/* 72 */	KEY(98),	/* 2 (num) */
/* 73 */	KEY(97),	/* 5 (num) */
/* 74 */	KEY(102),	/* 6 (num) */
/* 75 */	KEY(96),	/* 8 (num) */
/* 76 */	KEY(110),	/* Esc */
/* 77 */	KEY(90),	/* NumLock */
/* 78 */	KEY(122),	/* F11 */
/* 79 */	KEY(106),	/* + (num) */
/* 7a */	KEY(103),	/* 3 (num) */
/* 7b */	KEY(105),	/* - (num) */
/* 7c */	KEY(100),	/* * (num) */
/* 7d */	KEY(101),	/* 9 (num) */
/* 7e */	KEY(125),	/* Scroll Lock */
/* 7f */	INVALID,	/* 84-key SysReq?  Manual says no. */
/* 80 */	INVALID,
/* 81 */	INVALID,
/* 82 */	INVALID,
/* 83 */	KEY(118),	/* F7 */
/* 84 */	KEY(124),	/* PrintScreen (w/ Alt = SysRq) */
};

/*
 * Parse table after receiving an E0 prefix code.
 *
 * Generally speaking, keys that were added on the 101-key keyboard are
 * represented as an E0 followed by the code for an 84-key key.  Software
 * ignorant of the 101-key keyboard ignores the E0 and so is handled
 * compatibly.  Many of these variants involve "fake" shift presses
 * and releases for compatibility; these are also prefixed with E0.
 * We ignore these fake shifts.
 */
static const unsigned char	keytab_e0_set2[] = {
/* 00 */	INVALID,
/* 01 */	INVALID,
/* 02 */	INVALID,
/* 03 */	INVALID,
/* 04 */	INVALID,
/* 05 */	INVALID,
/* 06 */	INVALID,
/* 07 */	INVALID,
/* 08 */	INVALID,
/* 09 */	INVALID,
/* 0a */	INVALID,
/* 0b */	INVALID,
/* 0c */	INVALID,
/* 0d */	INVALID,
/* 0e */	INVALID,
/* 0f */	INVALID,
/* 10 */	INVALID,
/* 11 */	KEY(62),	/* R Alt */
/* 12 */	IGNORE,		/* Fake L Shift */
/* 13 */	INVALID,
/* 14 */	KEY(64),	/* R Ctrl */
/* 15 */	INVALID,
/* 16 */	INVALID,
/* 17 */	INVALID,
/* 18 */	INVALID,
/* 19 */	INVALID,
/* 1a */	INVALID,
/* 1b */	INVALID,
/* 1c */	INVALID,
/* 1d */	INVALID,
/* 1e */	INVALID,
/* 1f */	KEY(59),	/* L Window (104-key) */
/* 20 */	INVALID,
/* 21 */	INVALID,
/* 22 */	INVALID,
/* 23 */	INVALID,
/* 24 */	INVALID,
/* 25 */	INVALID,
/* 26 */	INVALID,
/* 27 */	KEY(63),	/* R Window (104-key) */
/* 28 */	INVALID,
/* 29 */	INVALID,
/* 2a */	INVALID,
/* 2b */	INVALID,
/* 2c */	INVALID,
/* 2d */	INVALID,
/* 2e */	INVALID,
/* 2f */	KEY(65),	/* Menu (104-key) */
/* 30 */	INVALID,
/* 31 */	INVALID,
/* 32 */	INVALID,
/* 33 */	INVALID,
/* 34 */	INVALID,
/* 35 */	INVALID,
/* 36 */	INVALID,
/* 37 */	INVALID,
/* 38 */	INVALID,
/* 39 */	INVALID,
/* 3a */	INVALID,
/* 3b */	INVALID,
/* 3c */	INVALID,
/* 3d */	INVALID,
/* 3e */	INVALID,
/* 3f */	INVALID,
/* 40 */	INVALID,
/* 41 */	INVALID,
/* 42 */	INVALID,
/* 43 */	INVALID,
/* 44 */	INVALID,
/* 45 */	INVALID,
/* 46 */	INVALID,
/* 47 */	INVALID,
/* 48 */	INVALID,
/* 49 */	INVALID,
/* 4a */	KEY(95),	/* / (num) */
/* 4b */	INVALID,
/* 4c */	INVALID,
/* 4d */	INVALID,
/* 4e */	INVALID,
/* 4f */	INVALID,
/* 50 */	INVALID,
/* 51 */	INVALID,
/* 52 */	INVALID,
/* 53 */	INVALID,
/* 54 */	INVALID,
/* 55 */	INVALID,
/* 56 */	INVALID,
/* 57 */	INVALID,
/* 58 */	INVALID,
/* 59 */	IGNORE,		/* Fake R Shift */
/* 5a */	KEY(108),	/* Enter (num) */
/* 5b */	INVALID,
/* 5c */	INVALID,
/* 5d */	INVALID,
/* 5e */	INVALID,
/* 5f */	INVALID,
/* 60 */	INVALID,
/* 61 */	INVALID,
/* 62 */	INVALID,
/* 63 */	INVALID,
/* 64 */	INVALID,
/* 65 */	INVALID,
/* 66 */	INVALID,
/* 67 */	INVALID,
/* 68 */	INVALID,
/* 69 */	KEY(81),	/* End (arrow) */
/* 6a */	INVALID,
/* 6b */	KEY(79),	/* Left (arrow) */
/* 6c */	KEY(80),	/* Home (arrow) */
/* 6d */	INVALID,
/* 6e */	INVALID,
/* 6f */	INVALID,
/* 70 */	KEY(75),	/* Insert (arrow) */
/* 71 */	KEY(76),	/* Delete (arrow) */
/* 72 */	KEY(84),	/* Down (arrow) */
/* 73 */	INVALID,
/* 74 */	KEY(89),	/* Right (arrow) */
/* 75 */	KEY(83),	/* Up (arrow) */
/* 76 */	INVALID,
/* 77 */	INVALID,
/* 78 */	INVALID,
/* 79 */	INVALID,
/* 7a */	KEY(86),	/* PgDn (arrow) */
/* 7b */	INVALID,
/* 7c */	KEY(124),	/* PrintScreen (no Alt) */
/* 7d */	KEY(85),	/* PgUp (arrow) */
/* 7e */	KEY(126),	/* Pause (w/Ctrl = Break) */
};


/*
 * Initialize the translation state machine.
 */
int
KeyboardConvertScan_init(struct kb8042 *kb8042, int scanset)
{
	kb8042->parse_scan_state = STATE_IDLE;
	kb8042->break_received = 0;

	if (scanset == 1) {
		KeyboardConvertScan_fn = &KeyboardConvertScan_set1;
		keytab_base = keytab_base_set1;
		keytab_base_length = NELEM(keytab_base_set1);
		keytab_e0 = keytab_e0_set1;
		keytab_e0_length = NELEM(keytab_e0_set1);
	} else if (scanset == 2) {
		KeyboardConvertScan_fn = &KeyboardConvertScan_set2;
		keytab_base = keytab_base_set2;
		keytab_base_length = NELEM(keytab_base_set2);
		keytab_e0 = keytab_e0_set2;
		keytab_e0_length = NELEM(keytab_e0_set2);
	} else {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 *	KeyboardConvertScan(*kb8042, scan, *keynum, *state
 *		*synthetic_release_needed)
 *
 *	State machine that takes scan codes from the keyboard and resolves
 *	them to key numbers using the above tables.  Returns B_TRUE if this
 *	scan code completes a scan code sequence, in which case "keynum",
 *	"state", and "synthetic_release_needed" will be filled in correctly.
 *
 *	"synthetic_release_needed" is a hack to handle the additional two
 *	keys on a Korean keyboard.  They report press only, so we tell the
 *	upper layer to synthesize the release.
 */
boolean_t
KeyboardConvertScan(
    struct kb8042	*kb8042,
    unsigned char	scan,
    int			*keynum,
    enum keystate	*state,
    boolean_t		*synthetic_release_needed)
{
	ASSERT(KeyboardConvertScan_fn != NULL);

	return ((*KeyboardConvertScan_fn)(kb8042, scan, keynum, state,
	    synthetic_release_needed));
}

boolean_t
KeyboardConvertScan_set1(
    struct kb8042	*kb8042,
    unsigned char	scan,
    int			*keynum,
    enum keystate	*state,
    boolean_t		*synthetic_release_needed)
{
	*synthetic_release_needed = B_FALSE;
	*state = KEY_PRESSED;

	switch (scan) {
	/*
	 * First, handle special cases.
	 * ACK has already been handled by our caller.
	 */
	case KB_ERROR:
		/*
		 * Perhaps we should reset state here,
		 * since we no longer know what's going on.
		 */
		return (B_FALSE);
	case KB_POST_FAIL:
		/*
		 * Perhaps we should reset the LEDs now.
		 * If so, this check should probably be in the main line.
		 * Perhaps we should tell the higher layers that the
		 * keyboard has been reset.
		 */
		/*
		 * Reset to idle
		 */
		kb8042->parse_scan_state = STATE_IDLE;
		return (B_FALSE);

	case KXT_EXTEND:
	case KXT_EXTEND2:
	case KXT_HANGUL_HANJA:
	case KXT_HANGUL:
		/*
		 * Exclude these keys from the "default" test below.
		 */
		break;

	default:
		/*
		 * See if it was a key release.
		 */
		if (scan > 0x80) {
			*state = KEY_RELEASED;
			scan -= 0x80;
		}
		break;
	}

	if (kb8042->break_received) {
		*state = KEY_RELEASED;
		kb8042->break_received = 0;
	}

	switch (kb8042->parse_scan_state) {
	case STATE_IDLE:
		switch (scan) {
		case KXT_EXTEND:
			kb8042->parse_scan_state = STATE_E0;
			return (B_FALSE);

		case KXT_EXTEND2:
			kb8042->parse_scan_state = STATE_E1;
			return (B_FALSE);

		/*
		 * We could do the next two in the table, but it would
		 * require nearly doubling the size of the table.
		 *
		 * Also, for some stupid reason these two report presses
		 * only.  We tell the upper layer to synthesize a release.
		 */
		case KXT_HANGUL_HANJA:
			*keynum = KEY(150);
			*synthetic_release_needed = B_TRUE;
			break;

		case KXT_HANGUL:
			*keynum = KEY(151);
			*synthetic_release_needed = B_TRUE;
			break;

		default:
			/*
			 * Regular scan code
			 */
			if (scan < keytab_base_length)
				*keynum = keytab_base[scan];
			else
				*keynum = INVALID;
			break;
		}
		break;

	case STATE_E0:		/* Mostly 101-key additions */
		if (scan < keytab_e0_length)
			*keynum = keytab_e0[scan];
		else
			*keynum = INVALID;
		break;

	case STATE_E1:		/* Pause key only */
		switch (scan) {
		case 0x1d:
			kb8042->parse_scan_state = STATE_E1_1D;
			return (B_FALSE);
		default:
			*keynum = INVALID;
			break;
		}
		break;

	case STATE_E1_1D:	/* Pause key only */
		switch (scan) {
		case 0x45:
			*keynum = KEY(126);	/* Pause */
			break;
		default:
			*keynum = INVALID;
			break;
		}
		break;
	}

	/*
	 * The results (*keynum, *state, and *synthetic_release_needed)
	 * have been filled in, but they are valid only if we return
	 * B_TRUE which is only done below.  If we make it to here, we
	 * have completed a scan code sequence, so reset parse_scan_state.
	 */

	kb8042->parse_scan_state = STATE_IDLE;

	switch (*keynum) {
	case KEYIGN:				/* not a key, nor an error */
		return (B_FALSE);		/* also not a final keycode */

	case KEYBAD:		/* not part of a legit sequence? */
		return (B_FALSE);	/* and return not a final keycode */

	default:
		/*
		 * If we're here, it's a valid keycode.  We've already
		 * filled in the return values; return success.
		 */
		return (B_TRUE);		/* resolved to a key */
	}
}

/*
 *	KeyboardConvertScan(*kb8042, scan, *keynum, *state
 *		*synthetic_release_needed)
 *
 *	State machine that takes scan codes from the keyboard and resolves
 *	them to key numbers using the above tables.  Returns B_TRUE if this
 *	scan code completes a scan code sequence, in which case "keynum",
 *	"state", and "synthetic_release_needed" will be filled in correctly.
 *
 *	"synthetic_release_needed" is a hack to handle the additional two
 *	keys on a Korean keyboard.  They report press only, so we tell the
 *	upper layer to synthesize the release.
 */
boolean_t
KeyboardConvertScan_set2(
    struct kb8042	*kb8042,
    unsigned char	scan,
    int			*keynum,
    enum keystate	*state,
    boolean_t		*synthetic_release_needed)
{
	*synthetic_release_needed = B_FALSE;
	*state = KEY_PRESSED;

	switch (scan) {
	/*
	 * First, handle special cases.
	 * ACK has already been handled by our caller.
	 */

	/*
	 * KAT_BREAK is 0xF0. It is the same as the break code for Japanese
	 * key 133.
	 * Therefore we don't treat it specially here.
	 */
	case KAT_BREAK:
		/* Switch states so we can recognize the code that follows */
		kb8042->break_received = 1;
		return (B_FALSE);	/* not a final keycode */

	case KB_ERROR:
		/*
		 * Perhaps we should reset state here,
		 * since we no longer know what's going on.
		 */
		return (B_FALSE);

	case KB_POST_OK:
	case KB_POST_FAIL:
		/*
		 * Perhaps we should reset the LEDs now.
		 * If so, this check should probably be in the main line.
		 * Perhaps we should tell the higher layers that the
		 * keyboard has been reset.
		 */
		/*
		 * Reset to idle
		 */
		kb8042->parse_scan_state = STATE_IDLE;
		return (B_FALSE);
	}

	if (kb8042->break_received) {
		*state = KEY_RELEASED;
		kb8042->break_received = 0;
	}

	switch (kb8042->parse_scan_state) {
	case STATE_IDLE:
		switch (scan) {
		case KXT_EXTEND:
			kb8042->parse_scan_state = STATE_E0;
			return (B_FALSE);

		case KXT_EXTEND2:
			kb8042->parse_scan_state = STATE_E1;
			return (B_FALSE);

		/*
		 * We could do the next two in the table, but it would
		 * require nearly doubling the size of the table.
		 *
		 * Also, for some stupid reason these two report presses
		 * only.  We tell the upper layer to synthesize a release.
		 */
		case KXT_HANGUL_HANJA:
			*keynum = KEY(150);
			*synthetic_release_needed = B_TRUE;
			break;

		case KXT_HANGUL:
			*keynum = KEY(151);
			*synthetic_release_needed = B_TRUE;
			break;

		default:
			/*
			 * Regular scan code
			 */
			if (scan < keytab_base_length)
				*keynum = keytab_base[scan];
			else
				*keynum = INVALID;
			break;
		}
		break;

	case STATE_E0:		/* Mostly 101-key additions */
		if (scan < keytab_e0_length)
			*keynum = keytab_e0[scan];
		else
			*keynum = INVALID;
		break;

	case STATE_E1:		/* Pause key only */
		switch (scan) {
		case 0x14:
			kb8042->parse_scan_state = STATE_E1_14;
			return (B_FALSE);
		default:
			*keynum = INVALID;
			break;
		}
		break;

	case STATE_E1_14:	/* Pause key only */
		if (scan == 0x77) {
			kb8042->parse_scan_state = STATE_E1_14_77;
			return (B_FALSE);
		} else {
			*keynum = INVALID;
		}
		break;

	case STATE_E1_14_77:
		if (scan == 0xE1) {
			kb8042->parse_scan_state = STATE_E1_14_77_E1;
			return (B_FALSE);
		} else {
			*keynum = INVALID;
		}
		break;

	case STATE_E1_14_77_E1:
		if (scan == 0xF0) {
			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0;
			return (B_FALSE);
		} else {
			*keynum = INVALID;
		}
		break;

	case STATE_E1_14_77_E1_F0:
		if (scan == 0x14) {
			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0_14;
			return (B_FALSE);
		} else {
			*keynum = INVALID;
		}
		break;

	case STATE_E1_14_77_E1_F0_14:
		if (scan == 0xF0) {
			kb8042->parse_scan_state = STATE_E1_14_77_E1_F0_14_F0;
			return (B_FALSE);
		} else {
			*keynum = INVALID;
		}
		break;

	case STATE_E1_14_77_E1_F0_14_F0:
		if (scan == 0x77) {
			*keynum = KEY(126);	/* Pause */
		} else {
			*keynum = INVALID;
		}
		break;
	}

	/*
	 * The results (*keynum, *state, and *synthetic_release_needed)
	 * have been filled in, but they are valid only if we return
	 * B_TRUE which is only done below.  If we make it to here, we
	 * have completed a scan code sequence, so reset parse_scan_state.
	 */

	if (kb8042->break_received) {
		*state = KEY_RELEASED;
		kb8042->break_received = 0;
	}

	kb8042->parse_scan_state = STATE_IDLE;

	switch (*keynum) {
	case KEYIGN:				/* not a key, nor an error */
		return (B_FALSE);		/* also not a final keycode */

	case KEYBAD:		/* not part of a legit sequence? */
		return (B_FALSE);	/* and return not a final keycode */

	default:
		/*
		 * If we're here, it's a valid keycode.  We've already
		 * filled in the return values; return success.
		 */
		return (B_TRUE);		/* resolved to a key */
	}
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#define	KEYMAP_SIZE_VARIABLE

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/inline.h>
#include <sys/termio.h>
#include <sys/stropts.h>
#include <sys/termios.h>
#include <sys/stream.h>
#include <sys/strtty.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/note.h>
#include "sys/consdev.h"
#include <sys/kbd.h>
#include <sys/kbtrans.h>
#include "kb8042.h"

#include <sys/i8042.h>

#include "sys/kbio.h"		/* for KIOCSLAYOUT */
#include "sys/stat.h"
#include "sys/reboot.h"
#include <sys/promif.h>
#include <sys/beep.h>
#include <sys/inttypes.h>
#include <sys/policy.h>

/*
 * For any keyboard, there is a unique code describing the position
 * of the key on a keyboard. We refer to the code as "station number".
 * The following table is used to map the station numbers from ps2
 * AT/XT keyboards to that of a USB one.
 *
 * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
 * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
 * when in KB_PC mode.  Note that this does not need to be made conditional
 * on the architecture for which this module is complied because there are no
 * keys that map to K8042_STOP on non-SPARC platforms.
 */
static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
/*  0 */	0,	53,	30,	31,	32,	33,	34,	35,
/*  8 */	36,	37,	38,	39,	45,	46,	137,	42,
/* 16 */	43,	20,	26,	8,	21,	23,	28,	24,
/* 24 */	12,	18,	19,	47,	48,	49,	57,	4,
/* 32 */	22,	7,	9,	10,	11,	13,	14,	15,
/* 40 */	51,	52,	50,	40,	225,	100,	29,	27,
/* 48 */	6,	25,	5,	17,	16,	54,	55,	56,
/* 56 */	135,	229,	224,	227,	226,	44,	230,	231,
/* 64 */	228,	101,	0,	0,	0,	0,	0,	0,
/* 72 */	0,	0,	0,	73,	76,	0,	0,	80,
/* 80 */	74,	77,	0,	82,	81,	75,	78,	0,
/* 88 */	0,	79,	83,	95,	92,	89,	0,	84,
/* 96 */	96,	93,	90,	98,	85,	97,	94,	91,
/* 104 */	99,	86,	87,	133,	88,	0,	41,	0,
/* 112 */	58,	59,	60,	61,	62,	63,	64,	65,
/* 120 */	66,	67,	68,	69,	70,	71,	72,	0,
/* 128 */	0,	0,	0,	139,	138,	136,	0,	0,
/* 136 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 144 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 152 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 160 */	120,	0,	0,	0,	0,	0,	0,	0,
/* 168 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 176 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 184 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 192 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 200 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 208 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 216 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 224 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 232 */	0,	128,	129,	127,	0,	0,	0,	0,
/* 240 */	0,	0,	0,	0,	0,	0,	0,	0,
/* 248 */	0,	0,	0,	0
};

#ifdef __sparc
#define	USECS_PER_WAIT 100
#define	MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
#define	MIN_DELAY_USECS USECS_PER_WAIT

boolean_t kb8042_warn_unknown_scanset = B_TRUE;
int kb8042_default_scanset = 2;

#endif

#define	MAX_KB8042_WAIT_MAX_MS	500		/* 500ms total */
#define	MAX_KB8042_RETRIES	5

enum state_return { STATE_NORMAL, STATE_INTERNAL };

static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
static uint_t kb8042_intr(caddr_t arg);
static void kb8042_wait_poweron(struct kb8042 *kb8042);
static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
static int kb8042_xlate_leds(int);
static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
static boolean_t kb8042_polled_keycheck(
			struct kbtrans_hardware *hw, int *key,
			enum keystate *state);
static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
			enum keystate state);
static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
static void kb8042_iocdatamsg(queue_t *, mblk_t *);
static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
static int kb8042_polled_ischar(cons_polledio_arg_t arg);
static int kb8042_polled_getchar(cons_polledio_arg_t arg);
static void kb8042_cleanup(struct kb8042 *kb8042);
static void kb8042_received_byte(struct kb8042 *, int);

static struct kbtrans_callbacks kb8042_callbacks = {
	kb8042_streams_setled,
	kb8042_polled_setled,
	kb8042_polled_keycheck,
};

extern struct keyboard keyindex_pc;

#define	DRIVER_NAME(dip) ddi_driver_name(dip)

static	char	module_name[] = "kb8042";

static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
			cred_t *credp);
static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
static int kb8042_rsrv(queue_t *);
static int kb8042_wsrv(queue_t *);

struct module_info kb8042_sinfo = {
	.mi_idnum = 42,			/* Module ID */
	.mi_idname = module_name,	/* Module name */
	.mi_minpsz = 0,			/* Minimum packet size */
	.mi_maxpsz = 32,		/* Maximum packet size */
	.mi_hiwat = 256,		/* High water mark */
	.mi_lowat = 128			/* Low water mark */
};

static struct qinit kb8042_rinit = {
	.qi_putp = NULL,
	.qi_srvp = kb8042_rsrv,
	.qi_qopen = kb8042_open,
	.qi_qclose = kb8042_close,
	.qi_qadmin = NULL,
	.qi_minfo = &kb8042_sinfo,
	.qi_mstat = NULL,
	.qi_rwp = NULL,
	.qi_infop = NULL,
	.qi_struiot = 0
};

static struct qinit kb8042_winit = {
	.qi_putp = putq,
	.qi_srvp = kb8042_wsrv,
	.qi_qopen = kb8042_open,
	.qi_qclose = kb8042_close,
	.qi_qadmin = NULL,
	.qi_minfo = &kb8042_sinfo,
	.qi_mstat = NULL,
	.qi_rwp = NULL,
	.qi_infop = NULL,
	.qi_struiot = 0
};

struct streamtab kb8042_str_info = {
	.st_rdinit = &kb8042_rinit,
	.st_wrinit = &kb8042_winit,
	.st_muxrinit = NULL,
	.st_muxwinit = NULL
};

struct kb8042	Kdws = {0};
static dev_info_t *kb8042_dip = NULL;

static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
		void **result);
static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);

static struct cb_ops cb_kb8042_ops = {
	.cb_open = nulldev,
	.cb_close = nulldev,
	.cb_strategy = nodev,
	.cb_print = nodev,
	.cb_dump = nodev,
	.cb_read = nodev,
	.cb_write = nodev,
	.cb_ioctl = nodev,
	.cb_devmap = nodev,
	.cb_mmap = nodev,
	.cb_segmap = nodev,
	.cb_chpoll = nochpoll,
	.cb_prop_op = ddi_prop_op,
	.cb_str = &kb8042_str_info,
	.cb_flag = D_MP,
	.cb_rev = CB_REV,
	.cb_aread = nodev,
	.cb_awrite = nodev
};

struct dev_ops kb8042_ops = {
	.devo_rev = DEVO_REV,
	.devo_refcnt = 0,
	.devo_getinfo = kb8042_getinfo,
	.devo_identify = nulldev,
	.devo_probe = nulldev,
	.devo_attach = kb8042_attach,
	.devo_detach = kb8042_detach,
	.devo_reset = nodev,
	.devo_cb_ops = &cb_kb8042_ops,
	.devo_bus_ops = NULL,
	.devo_power = NULL,
	.devo_quiesce = ddi_quiesce_not_needed
};


/*
 * This is the loadable module wrapper.
 */
#include <sys/modctl.h>

/*
 * Module linkage information for the kernel.
 */
static struct modldrv modldrv = {
	.drv_modops = &mod_driverops,	/* Type of module. */
	.drv_linkinfo = "PS/2 keyboard driver",
	.drv_dev_ops = &kb8042_ops,	/* driver ops */
};

static struct modlinkage modlinkage = {
	.ml_rev = MODREV_1,
	.ml_linkage = { &modldrv, NULL }
};

int
_init(void)
{
	int	rv;

	rv = mod_install(&modlinkage);
	return (rv);
}


int
_fini(void)
{
	return (mod_remove(&modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

#ifdef __sparc
static boolean_t
kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
{
	int i;
	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
	    I8042_INT_INPUT_AVAIL;
	int reps = timeout_usec / USECS_PER_WAIT;

	for (i = 0; i < reps; i++) {
		if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
			return (B_TRUE);

		if (i < (reps - 1))
			drv_usecwait(USECS_PER_WAIT);
	}
	return (B_FALSE);
}

static void
kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
{
	int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
	    I8042_INT_INPUT_DATA;

	while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
		(void) ddi_get8(kb8042->handle, kb8042->addr + port);
	}
}

/*
 * kb8042_send_and_expect does all its I/O via polling interfaces
 */
static boolean_t
kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
    int timeout, int *error, uint8_t *got)
{
	uint8_t datab;
	int err;
	boolean_t rval;

	ddi_put8(kb8042->handle,
	    kb8042->addr + I8042_POLL_OUTPUT_DATA, send);

	if (kb8042_is_input_avail(kb8042, timeout, B_TRUE)) {
		err = 0;
		datab = ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_POLL_INPUT_DATA);
		rval = ((datab == expect) ? B_TRUE : B_FALSE);
	} else {
		err = EAGAIN;
		rval = B_FALSE;
	}

	if (error != NULL)
		*error = err;
	if (got != NULL)
		*got = datab;
	return (rval);
}

static const char *
kb8042_error_string(int errcode)
{
	switch (errcode) {
	case EAGAIN:
		return ("Timed out");
	default:
		return ("Unknown error");
	}
}

/*
 * kb8042_read_scanset works properly because it is called before ddi_add_intr
 * (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
 * instead of just storing the data that comes in from the keyboard, which
 * would prevent the code here from getting it.)
 */
static int
kb8042_read_scanset(struct kb8042 *kb8042)
{
	int scanset = -1;
	int err;
	uint8_t got;

	kb8042_clear_input_buffer(kb8042, B_TRUE);

	/*
	 * Send a "change scan code set" command to the keyboard.
	 * It should respond with an ACK.
	 */
	if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, MAX_WAIT_USECS,
	    &err, &got) != B_TRUE) {
		goto fail_read_scanset;
	}

	/*
	 * Send a 0.  The keyboard should ACK the 0, then it should send the
	 * scan code set in use.
	 */
	if (kb8042_send_and_expect(kb8042, 0, KB_ACK, MAX_WAIT_USECS, &err,
	    &got) != B_TRUE) {
		goto fail_read_scanset;
	}

	/*
	 * The next input byte from the keyboard should be the scan code
	 * set in use, though some keyboards like to send a few more acks
	 * just for fun, so blow past those to get the keyboard scan code.
	 */
	while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
	    (scanset = ddi_get8(kb8042->handle,
	    kb8042->addr + I8042_POLL_INPUT_DATA)) == KB_ACK)
		;

#ifdef DEBUG
	cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
	    scanset);
#endif

	return (scanset);

fail_read_scanset:
#ifdef DEBUG
	if (err == 0)
		cmn_err(CE_NOTE, "Could not read current scan set from "
		    "keyboard: %s. (Expected 0x%x, but got 0x%x).",
		    kb8042_error_string(err), KB_ACK, got);
	else
		cmn_err(CE_NOTE, "Could not read current scan set from "
		    "keyboard: %s.", kb8042_error_string(err));
#endif
	return (-1);
}
#endif

static int
kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	int	rc;
	int	scanset;
	int	leds;

	struct kb8042	*kb8042 = &Kdws;
	static ddi_device_acc_attr_t attr = {
		DDI_DEVICE_ATTR_V0,
		DDI_NEVERSWAP_ACC,
		DDI_STRICTORDER_ACC,
	};

	switch (cmd) {
	case DDI_RESUME:
		leds = kb8042->leds.commanded;
		kb8042->w_init = 0;
		kb8042_init(kb8042, B_TRUE);
		kb8042_setled(kb8042, leds, B_FALSE);
		mutex_enter(&kb8042->w_hw_mutex);
		kb8042->suspended = B_FALSE;
		if (kb8042->w_qp != NULL) {
			enableok(WR(kb8042->w_qp));
			qenable(WR(kb8042->w_qp));
		}
		cv_broadcast(&kb8042->suspend_cv);
		mutex_exit(&kb8042->w_hw_mutex);
		return (DDI_SUCCESS);

	case DDI_ATTACH:
		if (kb8042_dip != NULL)
			return (DDI_FAILURE);
		/* The rest of the function is for attach */
		break;

	default:
		return (DDI_FAILURE);
	}

	kb8042->debugger.mod1 = 58;	/* Left Ctrl */
	kb8042->debugger.mod2 = 60;	/* Left Alt */
	kb8042->debugger.trigger = 33;	/* D */
	kb8042->debugger.mod1_down = B_FALSE;
	kb8042->debugger.mod2_down = B_FALSE;
	kb8042->debugger.enabled = B_FALSE;

	kb8042_dip = devi;
	kb8042->init_state = KB8042_UNINITIALIZED;

	kb8042->polled_synthetic_release_pending = B_FALSE;

	if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
	    DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
		goto failure;
	}

	kb8042->init_state |= KB8042_MINOR_NODE_CREATED;

	rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
	    (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
	if (rc != DDI_SUCCESS) {
#ifdef DEBUG
		cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
#endif
		goto failure;
	}

	kb8042->init_state |= KB8042_REGS_MAPPED;

	if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
	    DDI_SUCCESS) {
		cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
		goto failure;
	}

	mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
	cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
	cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
	cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
	kb8042->init_state |= KB8042_HW_MUTEX_INITTED;

	kb8042_init(kb8042, B_FALSE);

#ifdef __sparc
	/* Detect the scan code set currently in use */
	scanset = kb8042_read_scanset(kb8042);

	if (scanset < 0 && kb8042_warn_unknown_scanset) {

		cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
		cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
		cmn_err(CE_CONT, "Defaulting to scan code set %d.  If the "
		    "keyboard does not ", kb8042_default_scanset);
		cmn_err(CE_CONT, "work properly, add "
		    "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
		    (kb8042_default_scanset == 1) ? 2 : 1);
		cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
		    "restart the system.  If you ");
		cmn_err(CE_CONT, "do not want to see this message in the "
		    "future, add ");
		cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
		    "to /etc/system.\n");

		/* Use the default scan code set. */
		scanset = kb8042_default_scanset;
	}
#else
	/* x86 systems use scan code set 1 -- no detection required */
	scanset = 1;
#endif
	if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
		    "Unknown scan code set `%d'.", scanset);
		/* Scan code set is not supported */
		goto failure;
	}

	/*
	 * Turn on interrupts...
	 */
	if (ddi_add_intr(devi, 0,
	    &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
	    kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
		goto failure;
	}

	kb8042->init_state |= KB8042_INTR_ADDED;

	ddi_report_dev(devi);

#ifdef DEBUG
	cmn_err(CE_CONT, "?%s instance #%d READY\n",
	    DRIVER_NAME(devi), ddi_get_instance(devi));
#endif

	return (DDI_SUCCESS);

failure:
	kb8042_cleanup(kb8042);
	return (DDI_FAILURE);
}

static int
kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	struct kb8042 *kb8042 = &Kdws;

	switch (cmd) {
	case DDI_SUSPEND:
		mutex_enter(&kb8042->w_hw_mutex);
		ASSERT(kb8042->ops >= 0);
		while (kb8042->ops > 0)
			cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
		kb8042->suspended = B_TRUE;
		mutex_exit(&kb8042->w_hw_mutex);
		return (DDI_SUCCESS);

	case DDI_DETACH:
		/* If someone has a stream open, fail to detach */
		if (kb8042->w_qp != NULL)
			return (DDI_FAILURE);

		ASSERT(kb8042_dip == dip);

		kb8042_cleanup(kb8042);

		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}
}

static int
kb8042_getinfo(dev_info_t *dip __unused, ddi_info_cmd_t infocmd,
    void *arg __unused, void **result)
{
	int error;

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if (kb8042_dip == NULL) {
			error = DDI_FAILURE;
		} else {
			*result = (void *) kb8042_dip;
			error = DDI_SUCCESS;
		}
		break;
	case DDI_INFO_DEVT2INSTANCE:
		if (kb8042_dip == NULL) {
			error = DDI_FAILURE;
		} else {
			int instance;

			instance = ddi_get_instance(kb8042_dip);
			*result = (void *)(uintptr_t)instance;
			error = DDI_SUCCESS;
		}
		break;
	default:
		error = DDI_FAILURE;
		break;
	}
	return (error);
}

static void
kb8042_cleanup(struct kb8042 *kb8042)
{
	ASSERT(kb8042_dip != NULL);

	if (kb8042->init_state & KB8042_INTR_ADDED)
		ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);

	if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
		cv_destroy(&kb8042->cmd_cv);
		cv_destroy(&kb8042->suspend_cv);
		cv_destroy(&kb8042->ops_cv);
		mutex_destroy(&kb8042->w_hw_mutex);
	}

	if (kb8042->init_state & KB8042_REGS_MAPPED)
		ddi_regs_map_free(&kb8042->handle);

	if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
		ddi_remove_minor_node(kb8042_dip, NULL);

	kb8042->init_state = KB8042_UNINITIALIZED;
	kb8042_dip = NULL;
}

static void
kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
{
	if (kb8042->w_init)
		return;

	if (!from_resume) {
		kb8042->w_kblayout = 0;	/* Default to US */
		kb8042->w_qp = (queue_t *)NULL;
		kb8042->simulated_kbd_type = KB_PC;
		kb8042->leds.commanded = -1;	/* Unknown initial state */
		kb8042->leds.desired = -1;	/* Unknown initial state */
	}

	kb8042_wait_poweron(kb8042);

	kb8042->kb_old_key_pos = 0;

	/*
	 * Explicitly grab and release the 8042 lock outside of
	 * kb8042_send_to_keyboard, because this is the only situation
	 * where a polling interface is used with locking required.
	 */
	(void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
	/* Set up the command state machine and start it running. */
	kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
	(void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);

	kb8042->w_init++;

	(void) drv_setparm(SYSRINT, 1);	/* reset keyboard interrupts */
}

/*ARGSUSED2*/
static int
kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
{
	struct kb8042	*kb8042;
	int err = 0;
	int initial_leds;
	int initial_led_mask;

	kb8042 = &Kdws;

	mutex_enter(&kb8042->w_hw_mutex);
	if (qp->q_ptr) {
		kb8042->w_dev = *devp;
		mutex_exit(&kb8042->w_hw_mutex);
		return (0);
	}

	if (secpolicy_console(credp) != 0) {
		mutex_exit(&kb8042->w_hw_mutex);
		return (EPERM);
	}

	while (kb8042->suspended) {
		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
		    0) {
			mutex_exit(&kb8042->w_hw_mutex);
			return (EINTR);
		}
	}

	kb8042->w_dev = *devp;
	qp->q_ptr = (caddr_t)kb8042;
	WR(qp)->q_ptr = qp->q_ptr;
	if (!kb8042->w_qp)
		kb8042->w_qp = qp;

	ASSERT(kb8042->ops >= 0);
	kb8042->ops++;
	mutex_exit(&kb8042->w_hw_mutex);

	kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
	err = kbtrans_streams_init(qp, sflag,
	    (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
	    &kb8042->hw_kbtrans,
	    initial_leds, initial_led_mask);
	if (err != 0)
		goto out;

	kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);

	kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
	kb8042->polledio.cons_polledio_argument =
	    (cons_polledio_arg_t)kb8042;
	kb8042->polledio.cons_polledio_putchar = NULL;
	kb8042->polledio.cons_polledio_getchar =
	    (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
	kb8042->polledio.cons_polledio_ischar =
	    (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
	kb8042->polledio.cons_polledio_enter = NULL;
	kb8042->polledio.cons_polledio_exit = NULL;
	kb8042->polledio.cons_polledio_setled =
	    (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
	kb8042->polledio.cons_polledio_keycheck =
	    (boolean_t (*)(cons_polledio_arg_t, int *,
	    enum keystate *))kb8042_polled_keycheck;

	qprocson(qp);

	kbtrans_streams_enable(kb8042->hw_kbtrans);

out:
	mutex_enter(&kb8042->w_hw_mutex);
	ASSERT(kb8042->ops > 0);
	kb8042->ops--;
	if (kb8042->ops == 0)
		cv_broadcast(&kb8042->ops_cv);
	mutex_exit(&kb8042->w_hw_mutex);

	return (err);
}

/*ARGSUSED1*/
static int
kb8042_close(queue_t *qp, int flag, cred_t *credp)
{
	struct kb8042	*kb8042;

	/* If a beep is in progress, stop that */
	(void) beeper_off();

	kb8042 = (struct kb8042 *)qp->q_ptr;

	mutex_enter(&kb8042->w_hw_mutex);
	while (kb8042->suspended) {
		if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
		    0) {
			mutex_exit(&kb8042->w_hw_mutex);
			return (EINTR);
		}
	}

	ASSERT(kb8042->ops >= 0);
	kb8042->ops++;
	mutex_exit(&kb8042->w_hw_mutex);

	(void) kbtrans_streams_fini(kb8042->hw_kbtrans);

	kb8042->w_qp = (queue_t *)NULL;
	qprocsoff(qp);

	mutex_enter(&kb8042->w_hw_mutex);
	ASSERT(kb8042->ops > 0);
	kb8042->ops--;
	if (kb8042->ops == 0)
		cv_broadcast(&kb8042->ops_cv);
	mutex_exit(&kb8042->w_hw_mutex);

	return (0);
}

static int
kb8042_rsrv(queue_t *qp)
{
	mblk_t *mp;
	struct kb8042 *kb8042;

	while ((mp = getq(qp)) != NULL) {
		if (mp->b_datap->db_type == M_DATA) {
			kb8042 = (struct kb8042 *)qp->q_ptr;
			kb8042_received_byte(kb8042, *mp->b_rptr);
		}
		freemsg(mp);
	}
	return (0);
}

static int
kb8042_wsrv(queue_t *qp)
{
	struct kb8042 *kb8042;

	mblk_t	*mp;
	boolean_t suspended;

	kb8042 = (struct kb8042 *)qp->q_ptr;

	mutex_enter(&kb8042->w_hw_mutex);
	suspended = kb8042->suspended;
	ASSERT(kb8042->ops >= 0);
	if (!suspended)
		kb8042->ops++;
	mutex_exit(&kb8042->w_hw_mutex);

#ifdef NO_KB_DEBUG
	while (!suspended && (mp = getq(qp)) != NULL) {
#else
	/*
	 * Not taking keyboard input while suspending can make debugging
	 * difficult.  However, we still do the ops counting so that we
	 * don't suspend at a bad time.
	 */
	while ((mp = getq(qp))) {
#endif
		switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
		case KBTRANS_MESSAGE_HANDLED:
			continue;
		case KBTRANS_MESSAGE_NOT_HANDLED:
			break;
		}
		switch (mp->b_datap->db_type) {
		case M_IOCTL:
			kb8042_ioctlmsg(kb8042, qp, mp);
			continue;
		case M_IOCDATA:
			kb8042_iocdatamsg(qp, mp);
			continue;
		case M_DELAY:
		case M_STARTI:
		case M_STOPI:
		case M_READ:	/* ignore, no buffered data */
			freemsg(mp);
			continue;
		case M_FLUSH:
			*mp->b_rptr &= ~FLUSHW;
			if (*mp->b_rptr & FLUSHR)
				qreply(qp, mp);
			else
				freemsg(mp);
			continue;
		default:
			cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
			    mp->b_datap->db_type);
			freemsg(mp);
			continue;
		}
	}

	mutex_enter(&kb8042->w_hw_mutex);
	if (!suspended) {
		ASSERT(kb8042->ops > 0);
		kb8042->ops--;
		if (kb8042->ops == 0)
			cv_broadcast(&kb8042->ops_cv);
	}
	mutex_exit(&kb8042->w_hw_mutex);

	return (0);
}

static void
kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
{
	struct iocblk	*iocp;
	mblk_t		*datap;
	int		error;
	int		tmp;
	int		cycles;
	int		frequency;
	int		msecs;

	iocp = (struct iocblk *)mp->b_rptr;

	switch (iocp->ioc_cmd) {

	case CONSOPENPOLLEDIO:
		error = miocpullup(mp, sizeof (struct cons_polledio *));
		if (error != 0) {
			miocnak(qp, mp, 0, error);
			return;
		}

		/*
		 * We are given an appropriate-sized data block,
		 * and return a pointer to our structure in it.
		 */
		*(struct cons_polledio **)mp->b_cont->b_rptr =
		    &kb8042->polledio;
		mp->b_datap->db_type = M_IOCACK;
		iocp->ioc_error = 0;
		qreply(qp, mp);
		break;

	case CONSCLOSEPOLLEDIO:
		miocack(qp, mp, 0, 0);
		break;

	case CONSSETABORTENABLE:
		if (iocp->ioc_count != TRANSPARENT) {
			miocnak(qp, mp, 0, EINVAL);
			return;
		}

		kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
		miocack(qp, mp, 0, 0);
		break;

	/*
	 * Valid only in TR_UNTRANS_MODE mode.
	 */
	case CONSSETKBDTYPE:
		error = miocpullup(mp, sizeof (int));
		if (error != 0) {
			miocnak(qp, mp, 0, error);
			return;
		}
		tmp =  *(int *)mp->b_cont->b_rptr;
		if (tmp != KB_PC && tmp != KB_USB) {
			miocnak(qp, mp, 0, EINVAL);
			break;
		}
		kb8042->simulated_kbd_type = tmp;
		miocack(qp, mp, 0, 0);
		break;

	case KIOCLAYOUT:
		if (kb8042->w_kblayout == -1) {
			miocnak(qp, mp, 0, EINVAL);
			return;
		}

		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
			miocnak(qp, mp, 0, ENOMEM);
			return;
		}

		if (kb8042->simulated_kbd_type == KB_USB)
			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
		else
			*(int *)datap->b_wptr = kb8042->w_kblayout;

		datap->b_wptr += sizeof (int);
		if (mp->b_cont)
			freemsg(mp->b_cont);
		mp->b_cont = datap;
		iocp->ioc_count = sizeof (int);
		mp->b_datap->db_type = M_IOCACK;
		iocp->ioc_error = 0;
		qreply(qp, mp);
		break;

	case KIOCSLAYOUT:
		if (iocp->ioc_count != TRANSPARENT) {
			miocnak(qp, mp, 0, EINVAL);
			return;
		}

		kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
		miocack(qp, mp, 0, 0);
		break;

	case KIOCCMD:
		error = miocpullup(mp, sizeof (int));
		if (error != 0) {
			miocnak(qp, mp, 0, error);
			return;
		}

		kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
		miocack(qp, mp, 0, 0);
		break;

	case KIOCMKTONE:
		if (iocp->ioc_count != TRANSPARENT) {
			miocnak(qp, mp, 0, EINVAL);
			return;
		}

		tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
		cycles = tmp & 0xffff;
		msecs = (tmp >> 16) & 0xffff;

		if (cycles == 0)
			frequency = UINT16_MAX;
		else if (cycles == UINT16_MAX)
			frequency = 0;
		else {
			frequency = (PIT_HZ + cycles / 2) / cycles;
			if (frequency > UINT16_MAX)
				frequency = UINT16_MAX;
		}

		error = beep_mktone(frequency, msecs);
		if (error != 0)
			miocnak(qp, mp, 0, error);
		else
			miocack(qp, mp, 0, 0);
		break;

	default:
#ifdef DEBUG1
		cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
#endif
		miocnak(qp, mp, 0, EINVAL);
		return;
	}
}

/*
 * Process a byte received from the keyboard
 */
static void
kb8042_received_byte(
	struct kb8042	*kb8042,
	int		scancode)	/* raw scan code */
{
	boolean_t	legit;		/* is this a legit key pos'n? */
	int		key_pos = -1;
	enum keystate	state;
	boolean_t	synthetic_release_needed;

	if (!kb8042->w_init)	/* can't do anything anyway */
		return;

	legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
	    &synthetic_release_needed);

	if (legit == 0) {
		/* Eaten by translation */
		return;
	}

	/*
	 * Don't know if we want this permanently, but it seems interesting
	 * for the moment.
	 */
	if (key_pos == kb8042->debugger.mod1) {
		kb8042->debugger.mod1_down = (state == KEY_PRESSED);
	}
	if (key_pos == kb8042->debugger.mod2) {
		kb8042->debugger.mod2_down = (state == KEY_PRESSED);
	}
	if (kb8042->debugger.enabled &&
	    key_pos == kb8042->debugger.trigger &&
	    kb8042->debugger.mod1_down &&
	    kb8042->debugger.mod2_down) {
		/*
		 * Require new presses of the modifiers.
		 */
		kb8042->debugger.mod1_down = B_FALSE;
		kb8042->debugger.mod2_down = B_FALSE;
		abort_sequence_enter(NULL);
		return;
	}

	/*
	 * If there's no queue above us - as can happen if we've been
	 * attached but not opened - drop the keystroke.
	 * Note that we do this here instead of above so that
	 * Ctrl-Alt-D still works.
	 */
	if (kb8042->w_qp == NULL) {
		return;
	}

	/*
	 * This is to filter out auto repeat since it can't be
	 * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
	 * can.  Don't know whether they've taken over the world.
	 * Don't think so.)
	 */
	if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
		return;
	}


	kb8042_process_key(kb8042, key_pos, state);

	/*
	 * This is a total hack.  For some stupid reason, the two additional
	 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
	 * only.  We synthesize a release immediately.
	 */
	if (synthetic_release_needed) {
		(void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
		kb8042_process_key(kb8042, key_pos, state);
	}
}


static void
kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
    enum keystate state)
{
	kbtrans_key_t key;

	ASSERT(key_pos >= 0 && key_pos <= 255);
	if (kb8042->simulated_kbd_type == KB_PC) {
		kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
	} else if (kb8042->simulated_kbd_type == KB_USB) {
		key = keytab_pc2usb[key_pos];
		if (key != 0) {
			kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
		}
	}
}

/*
 * Called from interrupt handler when keyboard interrupt occurs.
 */
static uint_t
kb8042_intr(caddr_t arg)
{
	uchar_t scancode;	/* raw scan code */
	int rc;
	struct kb8042 *kb8042 = (struct kb8042 *)arg;

	rc = DDI_INTR_UNCLAIMED;

	if (kb8042->init_state == KB8042_UNINITIALIZED)
		return (DDI_INTR_UNCLAIMED);

	/* don't care if drv_setparm succeeds */
	(void) drv_setparm(SYSRINT, 1);

	while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
	    != 0) {
		mblk_t *mp;

		rc = DDI_INTR_CLAIMED;

		scancode = ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_INT_INPUT_DATA);

		/*
		 * Intercept ACK and RESEND and signal the condition that
		 * kb8042_send_and_wait is waiting for.
		 */
		switch (scancode) {
		case KB_ACK:
			mutex_enter(&kb8042->w_hw_mutex);
			kb8042->acked = 1;
			cv_signal(&kb8042->cmd_cv);
			mutex_exit(&kb8042->w_hw_mutex);
			return (rc);
		case KB_RESEND:
			mutex_enter(&kb8042->w_hw_mutex);
			kb8042->need_retry = 1;
			cv_signal(&kb8042->cmd_cv);
			mutex_exit(&kb8042->w_hw_mutex);
			return (rc);
		default:
			break;
		}

		if ((mp = allocb(sizeof (scancode), BPRI_HI)) == NULL)
			return (rc);
		*mp->b_wptr++ = scancode;
		if (putq(RD(kb8042->w_qp), mp) == 0)
			freemsg(mp);
	}

	return (rc);
}

static void
kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
{
	struct copyresp	*csp;

	csp = (struct copyresp *)mp->b_rptr;
	if (csp->cp_rval) {
		freemsg(mp);
		return;
	}

	switch (csp->cp_cmd) {
	default:
		miocack(qp, mp, 0, 0);
		break;
	}
}

static boolean_t
kb8042_polled_keycheck(
    struct kbtrans_hardware *hw,
    int *key,
    enum keystate *state)
{
	struct kb8042 *kb8042 = (struct kb8042 *)hw;
	int	scancode;
	boolean_t	legit;
	boolean_t	synthetic_release_needed;

	if (kb8042->polled_synthetic_release_pending) {
		*key = kb8042->polled_synthetic_release_key;
		*state = KEY_RELEASED;
		kb8042->polled_synthetic_release_pending = B_FALSE;
		(void) kb8042_autorepeat_detect(kb8042, *key, *state);
		return (B_TRUE);
	}

	for (;;) {
		if (ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
			return (B_FALSE);
		}

		scancode = ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_POLL_INPUT_DATA);

		legit = KeyboardConvertScan(kb8042, scancode, key, state,
		    &synthetic_release_needed);
		if (!legit) {
			continue;
		}
		/*
		 * For the moment at least, we rely on hardware autorepeat
		 * for polled I/O autorepeat.  However, for coordination
		 * with the interrupt-driven code, maintain the last key
		 * pressed.
		 */
		(void) kb8042_autorepeat_detect(kb8042, *key, *state);

		/*
		 * This is a total hack to support two additional keys
		 * on Korean keyboards.  They report only on press, and
		 * so we synthesize a release.  Most likely this will
		 * never be important to polled  I/O, but if I do it
		 * "right" the first time it _won't_ be an issue.
		 */
		if (synthetic_release_needed) {
			kb8042->polled_synthetic_release_pending = B_TRUE;
			kb8042->polled_synthetic_release_key = *key;
		}

		if (kb8042->simulated_kbd_type == KB_USB) {
			*key = keytab_pc2usb[*key];
		}
		return (B_TRUE);
	}
}

static void
kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
{
	kb8042->leds.desired = led_state;

	if (!polled)
		mutex_enter(&kb8042->w_hw_mutex);

	if (kb8042->leds.desired != kb8042->leds.commanded) {
		kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
	}

	if (!polled)
		mutex_exit(&kb8042->w_hw_mutex);
}

static void
kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
{
	struct kb8042 *kb8042 = (struct kb8042 *)hw;
	kb8042_setled(kb8042, led_state, B_TRUE);
}

static void
kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
{
	struct kb8042 *kb8042 = (struct kb8042 *)hw;
	kb8042_setled(kb8042, led_state, B_FALSE);
}


static int
kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
{
	uint8_t *outp = kb8042->addr +
	    (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
	uint8_t *inavp = kb8042->addr +
	    (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
	uint8_t *inp = kb8042->addr +
	    (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
	uint8_t b;
	int ms_waited;
	int timedout;
	int expire;
	int retries = 0;

	do {
		kb8042->acked = 0;
		kb8042->need_retry = 0;
		ms_waited = 0;		/* Zero it whether polled or not */
		timedout = 0;

		ddi_put8(kb8042->handle, outp, u8);

		while (!kb8042->acked && !kb8042->need_retry && !timedout) {

			if (polled) {
				if (ddi_get8(kb8042->handle, inavp)) {
					b = ddi_get8(kb8042->handle, inp);
					switch (b) {
					case KB_ACK:
						kb8042->acked = 1;
						break;
					case KB_RESEND:
						kb8042->need_retry = 1;
						break;
					default:
						/*
						 * drop it: We should never
						 * get scancodes while
						 * we're in the middle of a
						 * command anyway.
						 */
#ifdef DEBUG
						cmn_err(CE_WARN, "!Unexpected "
						    " byte 0x%x", b);
#endif
						break;
					}
				}

				/*
				 * Wait 1ms if an ACK wasn't received yet
				 */
				if (!kb8042->acked) {
					drv_usecwait(1000);
					ms_waited++;
					if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
						timedout = B_TRUE;
				}
			} else {
				/* Interrupt-driven */
				expire = ddi_get_lbolt() +
				    drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);

				/*
				 * If cv_timedwait returned -1 and we neither
				 * received an ACK nor a RETRY response, then
				 * we timed out.
				 */
				if (cv_timedwait(&kb8042->cmd_cv,
				    &kb8042->w_hw_mutex, expire) == -1 &&
				    !kb8042->acked && !kb8042->need_retry) {
					timedout = B_TRUE;
				}
			}

		}
	} while ((kb8042->need_retry || timedout) &&
	    ++retries < MAX_KB8042_RETRIES);

	return (kb8042->acked);
}

/*
 * kb8042_send_to_keyboard should be called with w_hw_mutex held if
 * polled is FALSE.
 */
static void
kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
{

	/*
	 * KB_SET_LED and KB_ENABLE are special commands which require blocking
	 * other 8042 consumers while executing.
	 *
	 * Other commands/data are sent using the single put8 I/O access
	 * function.
	 */
	if (byte == KB_SET_LED) {

		if (!polled) {
			(void) ddi_get8(kb8042->handle, kb8042->addr +
			    I8042_LOCK);
		}

		if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
			/*
			 * Ignore return value, as there's nothing we can
			 * do about it if the SET LED command fails.
			 */
			(void) kb8042_send_and_wait(kb8042,
			    kb8042_xlate_leds(kb8042->leds.desired), polled);
		}

		if (!polled) {
			(void) ddi_get8(kb8042->handle, kb8042->addr +
			    I8042_UNLOCK);
		}
		kb8042->leds.commanded = kb8042->leds.desired;

	} else if (byte == KB_ENABLE) {

		if (!polled) {
			(void) ddi_get8(kb8042->handle, kb8042->addr +
			    I8042_LOCK);
		}

		(void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);

		if (!polled) {
			(void) ddi_get8(kb8042->handle, kb8042->addr +
			    I8042_UNLOCK);
		}

	} else {
		/* All other commands use the "normal" virtual output port */
		if (polled) {
			ddi_put8(kb8042->handle,
			    kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
		} else {
			ddi_put8(kb8042->handle,
			    kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
		}
	}
}

/*
 * Wait until the keyboard is fully up, maybe.
 * We may be the first person to talk to the keyboard, in which case
 * it's patiently waiting to say "AA" to us to tell us it's up.
 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
 * good bet that we've already spent that long getting to that point,
 * so we'll only wait long enough for the communications electronics to
 * run.
 */
static void
kb8042_wait_poweron(struct kb8042 *kb8042)
{
	int cnt;
	int ready;

	/* wait for up to 250 ms for a response */
	for (cnt = 0; cnt < 250; cnt++) {
		ready = ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_INT_INPUT_AVAIL);
		if (ready != 0)
			break;
		drv_usecwait(1000);
	}

	/*
	 * If there's something pending, read and discard it.  If not,
	 * assume things are OK anyway - maybe somebody else ate it
	 * already.  (On a PC, the BIOS almost certainly did.)
	 */
	if (ready != 0) {
		(void) ddi_get8(kb8042->handle,
		    kb8042->addr + I8042_INT_INPUT_DATA);
	}
}

static int
kb8042_xlate_leds(int led)
{
	int res;

	res = 0;

	if (led & LED_NUM_LOCK)
		res |= LED_NUM;
	if (led & LED_SCROLL_LOCK)
		res |= LED_SCR;
	if (led & LED_CAPS_LOCK)
		res |= LED_CAP;

	return (res);
}

/*ARGSUSED*/
static void
kb8042_get_initial_leds(
    struct kb8042 *kb8042,
    int *initial_leds,
    int *initial_led_mask)
{
#if defined(__x86)
	extern caddr_t	p0_va;
	uint8_t		bios_kb_flag;

	bios_kb_flag = p0_va[BIOS_KB_FLAG];

	*initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
	*initial_leds = 0;
	if (bios_kb_flag & BIOS_CAPS_STATE)
		*initial_leds |= LED_CAPS_LOCK;
	if (bios_kb_flag & BIOS_NUM_STATE)
		*initial_leds |= LED_NUM_LOCK;
	if (bios_kb_flag & BIOS_SCROLL_STATE)
		*initial_leds |= LED_SCROLL_LOCK;
#else
	*initial_leds = 0;
	*initial_led_mask = 0;
#endif
}

static boolean_t
kb8042_autorepeat_detect(
    struct kb8042 *kb8042,
    int key_pos,
    enum keystate state)
{
	if (state == KEY_RELEASED) {
		if (kb8042->kb_old_key_pos == key_pos)
			kb8042->kb_old_key_pos = 0;
	} else {
		if (kb8042->kb_old_key_pos == key_pos) {
			return (B_TRUE);
		}
		kb8042->kb_old_key_pos = key_pos;
	}
	return (B_FALSE);
}

/* ARGSUSED */
static void
kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
{
	switch (cmd) {
	case KBD_CMD_BELL:
		(void) beeper_on(BEEP_TYPE4);
		break;
	case KBD_CMD_NOBELL:
		(void) beeper_off();
		break;
	}
}


/*
 * This is a pass-thru routine to get a character at poll time.
 */
static int
kb8042_polled_getchar(cons_polledio_arg_t arg)
{
	struct kb8042	*kb8042;

	kb8042 = (struct kb8042 *)arg;

	return (kbtrans_getchar(kb8042->hw_kbtrans));
}

/*
 * This is a pass-thru routine to get a character at poll time.
 */
static int
kb8042_polled_ischar(cons_polledio_arg_t arg)
{
	struct kb8042	*kb8042;

	kb8042 = (struct kb8042 *)arg;

	return (kbtrans_ischar(kb8042->hw_kbtrans));
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_KB8042_H
#define	_KB8042_H

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * Messages from keyboard.
 */
#define	KB_ERROR	0x00	/* Keyboard overrun or detection error */
#define	KB_POST_OK	0xAA	/* Sent at completion of poweron */
#define	KB_ECHO		0xEE	/* Response to Echo command (EE)  */
#define	KB_ACK		0xFA	/* Acknowledgement byte from keyboard */
#define	KB_POST_FAIL	0xFC	/* Power On Self Test failed */
#define	KB_RESEND	0xFE	/* response from keyboard to resend data */
#define	KB_REPLY_MAXLEN	8	/* Maximum # of bytes the keyboard can reply */
/*
 * Commands to keyboard.
 */
#define	KB_SET_LED	0xED	/* Tell kbd that following byte is led status */
#define	KB_READID	0xF2	/* command to read keyboard id */
#define	KB_ENABLE	0xF4	/* command to to enable keyboard */
#define	KB_RESET	0xFF	/* command to reset keyboard */
#define	KB_SET_TYPE	0xF3	/* command--next byte is typematic values */
#define	KB_SET_SCAN	0xF0	/* kbd command to set scan code set */

/*
 * LED bits
 */
#define	LED_SCR		0x01	/* Flag bit for scroll lock */
#define	LED_CAP		0x04	/* Flag bit for cap lock */
#define	LED_NUM		0x02	/* Flag bit for num lock */

/*
 * Keyboard scan code prefixes
 */
#define	KAT_BREAK	0xf0	/* first byte in two byte break sequence */
#define	KXT_EXTEND	0xe0	/* first byte in two byte extended sequence */
#define	KXT_EXTEND2	0xe1	/* Used in "Pause" sequence */

/*
 * Korean keyboard keys.  We handle these specially to avoid having to
 * dramatically extend the table.
 */
#define	KXT_HANGUL_HANJA	0xf1
#define	KXT_HANGUL		0xf2

#ifdef _KERNEL

struct kb8042 {
	kmutex_t	w_hw_mutex;	/* hardware mutex */
	int	w_init;		/* workstation has been initialized */
	queue_t	*w_qp;		/* pointer to queue for this minor device */
	int	w_kblayout;	/* keyboard layout code */
	dev_t	w_dev;		/* major/minor for this device */
	ddi_iblock_cookie_t	w_iblock;
	ddi_acc_handle_t	handle;
	uint8_t			*addr;
	int	kb_old_key_pos;	/* scancode for autorepeat filtering */
	struct {
		int desired;
		int commanded;
	}	leds;
	int	parse_scan_state;
	struct kbtrans	*hw_kbtrans;
	struct cons_polledio	polledio;
	struct {
		unsigned char mod1;
		unsigned char mod2;
		unsigned char trigger;
		boolean_t mod1_down;
		boolean_t mod2_down;
		boolean_t enabled;
	}		debugger;
	boolean_t	polled_synthetic_release_pending;
	int		polled_synthetic_release_key;
	int		simulated_kbd_type;
	uint32_t	init_state;
	int		break_received;
	boolean_t	suspended;
	int		ops;
	kcondvar_t	suspend_cv;
	kcondvar_t	ops_cv;
	int		acked;
	int		need_retry;
	kcondvar_t	cmd_cv;
};

extern boolean_t KeyboardConvertScan(struct kb8042 *, unsigned char scan,
			int *keynum, enum keystate *, boolean_t *);
extern int KeyboardConvertScan_init(struct kb8042 *, int scanset);

#if defined(__i386) || defined(__amd64)
/*
 * We pick up the initial state of the keyboard from the BIOS state.
 */
#define	BIOS_KB_FLAG		0x417	/* address of BIOS keyboard state */
#define	BIOS_SCROLL_STATE	0x10
#define	BIOS_NUM_STATE		0x20
#define	BIOS_CAPS_STATE		0x40
#endif

/*
 * Initialization states
 */
#define	KB8042_UNINITIALIZED		0x00000000
#define	KB8042_MINOR_NODE_CREATED	0x00000001
#define	KB8042_REGS_MAPPED		0x00000002
#define	KB8042_HW_MUTEX_INITTED		0x00000004
#define	KB8042_INTR_ADDED		0x00000008

/*
 * Key values that map into the USB translation table in kb8042.c
 */
#define	K8042_STOP	160

#endif

#ifdef	__cplusplus
}
#endif

#endif /* _KB8042_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This module contains the translation tables for PS/2 style keyboards.
 */
#define	KEYMAP_SIZE_VARIABLE

#include <sys/param.h>
#include <sys/kbd.h>

/* handy way to define control characters in the tables */
#define	c(ch)	(ch&0x1F)
#define	ESC 0x1B
#define	DEL 0x7F

#define	KEYMAP_SIZE_PC	160

/* ***************************** */
/*  PC-101 keyboard definitions  */
/* ***************************** */
/* Unshifted keyboard table for PC keyboard */

/* BEGIN CSTYLED */
static keymap_entry_t keytab_pc_lc[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	'`',	'1',	'2',	'3',	'4',	'5',	'6',
/*  8 */	'7', 	'8',	'9',	'0',	'-',	'=',	HOLE,	'\b',
/* 16 */	'\t',	'q',	'w',	'e',	'r',	't',	'y',	'u',
/* 24 */	'i',	'o', 	'p', 	'[',	']',	'\\',
							SHIFTKEYS+CAPSLOCK,
									'a',
/* 32 */	's',	'd',	'f',	'g',	'h',	'j',	'k',	'l',
/* 40 */	';',	'\'',	'\\',	'\r',
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	'z',	'x',
/* 48 */	'c',	'v',	'b',	'n',	'm',	',',	'.',	'/',
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,	SHIFTKEYS+LEFTALT,
							' ',	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	BF(8),	DEL,
							NOP,	HOLE,
							    STRING+LEFTARROW,
/* 80 */	RF(7),	RF(13),	HOLE,
				    STRING+UPARROW,
					    STRING+DOWNARROW,
							RF(9),	RF(15),	HOLE,
/* 88 */	HOLE,
		STRING+RIGHTARROW,
			    SHIFTKEYS+NUMLOCK,
					RF(7),	STRING+LEFTARROW,
							RF(13),	HOLE,
								PADSLASH,
/* 96 */	STRING+UPARROW,
			RF(11),	STRING+DOWNARROW,
					BF(8),	PADSTAR,
							RF(9),
							  STRING+RIGHTARROW,
									RF(15),
/*104 */	DEL,	PADMINUS,
				PADPLUS,
					HOLE,	PADENTER,
							HOLE,	ESC,	HOLE,
/*112 */	TF(1),	TF(2),	TF(3),	TF(4),	TF(5),	TF(6),	TF(7),	TF(8),
/*120 */	TF(9),	TF(10),	TF(11),	TF(12),	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* Shifted keyboard table for PC keyboard */

static keymap_entry_t keytab_pc_uc[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	'~',	'!',	'@',	'#',	'$',	'%',	'^',
/*  8 */	'&', 	'*',	'(',	')',	'_',	'+',	HOLE,	'\b',
/* 16 */	'\t',	'Q',	'W',	'E',	'R',	'T',	'Y',	'U',
/* 24 */	'I',	'O', 	'P', 	'{',	'}',	'|',
							SHIFTKEYS+CAPSLOCK,
									'A',
/* 32 */	'S',	'D',	'F',	'G',	'H',	'J',	'K',	'L',
/* 40 */	':',	'"',	'|',	'\r',
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	'Z',	'X',
/* 48 */	'C',	'V',	'B',	'N',	'M',	'<',	'>',	'?',
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,
						SHIFTKEYS+LEFTALT,
							' ',	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	BF(8),	DEL,	NOP,	HOLE,
							    STRING+LEFTARROW,
/* 80 */	RF(7),	RF(13),	HOLE,	STRING+UPARROW,
						STRING+DOWNARROW,
							RF(9),	RF(15),	HOLE,
/* 88 */	HOLE,
		STRING+RIGHTARROW,
			    SHIFTKEYS+NUMLOCK,
					'7',	'4',	'1',	HOLE,	'/',
/* 96 */	'8',	'5',	'2',	'0',	'*',	'9',	'6',	'3',
/*104 */	'.',	'-',	'+',	HOLE,	'\n',	HOLE,	ESC,	HOLE,
/*112 */	TF(1),	TF(2),	TF(3),	TF(4),	TF(5),	TF(6),	TF(7),	TF(8),
/*120 */	TF(9),	TF(10),	TF(11),	TF(12),	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* Caps Locked keyboard table for PC keyboard */

static keymap_entry_t keytab_pc_cl[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	'`',	'1',	'2',	'3',	'4',	'5',	'6',
/*  8 */	'7', 	'8',	'9',	'0',	'-',	'=',	HOLE,	'\b',
/* 16 */	'\t',	'Q',	'W',	'E',	'R',	'T',	'Y',	'U',
/* 24 */	'I',	'O', 	'P', 	'[',	']',	'\\',
							SHIFTKEYS+CAPSLOCK,
									'A',
/* 32 */	'S',	'D',	'F',	'G',	'H',	'J',	'K',	'L',
/* 40 */	';',	'\'',	'\\',	'\r',
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	'Z',	'X',
/* 48 */	'C',	'V',	'B',	'N',	'M',	',',	'.',	'/',
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,
						SHIFTKEYS+LEFTALT,
							' ',	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	BF(8),	DEL,	NOP,	HOLE,
							    STRING+LEFTARROW,
/* 80 */	RF(7),
			RF(13),	HOLE,
				    STRING+UPARROW,
					    STRING+DOWNARROW,
							RF(9),	RF(15),	HOLE,
/* 88 */	HOLE,
		STRING+RIGHTARROW,
			    SHIFTKEYS+NUMLOCK,
					RF(7),	STRING+LEFTARROW,
							RF(13),	HOLE, PADSLASH,
/* 96 */	STRING+UPARROW,
			RF(11),	STRING+DOWNARROW,
					BF(8),	PADSTAR,
							RF(9),
							   STRING+RIGHTARROW,
									RF(15),
/*104 */	DEL,	PADMINUS,
				PADPLUS,
					HOLE,	PADENTER,
							HOLE,	ESC,	HOLE,
/*112 */	TF(1),	TF(2),	TF(3),	TF(4),	TF(5),	TF(6),	TF(7),	TF(8),
/*120 */	TF(9),	TF(10),	TF(11),	TF(12),	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* Alt Graph keyboard table for PC keyboard */

static keymap_entry_t keytab_pc_ag[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/*  8 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	HOLE,	NOP,
/* 16 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 24 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
							SHIFTKEYS+CAPSLOCK,
									NOP,
/* 32 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 40 */	NOP,	NOP,	NOP,	NOP,
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	NOP,	NOP,
/* 48 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,
						SHIFTKEYS+LEFTALT,
							' ',	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	BF(8),	DEL,	NOP,	HOLE,
									STRING+
								     LEFTARROW,
/* 80 */	RF(7),	RF(13),	HOLE,	STRING+
					UPARROW,STRING+
					      DOWNARROW,RF(9),	RF(15),	HOLE,
/* 88 */	HOLE,	STRING+
		    RIGHTARROW,
			SHIFTKEYS+NUMLOCK,
					NOP,	NOP,	NOP,	HOLE,	NOP,
/* 96 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/*104 */	NOP,	NOP,	NOP,	HOLE,	NOP,	HOLE,	ESC,	HOLE,
/*112 */	TF(1),	TF(2),	TF(3),	TF(4),	TF(5),	TF(6),	TF(7),	TF(8),
/*120 */	TF(9),	TF(10),	TF(11),	TF(12),	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* Num Locked keyboard table for PC keyboard */

static keymap_entry_t keytab_pc_nl[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/*  8 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	HOLE,	NONL,
/* 16 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/* 24 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/* 32 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/* 40 */	NONL,	NONL,	NONL,	NONL,	NONL,	HOLE,	NONL,	NONL,
/* 48 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/* 56 */	NONL,	NONL,	NONL,	HOLE,	NONL,	NONL,	NONL,	HOLE,
/* 64 */	NONL,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	NONL,	NONL,	NONL,	HOLE,	NONL,
/* 80 */	NONL,	NONL,	HOLE,	NONL,	NONL,	NONL,	NONL,	HOLE,
/* 88 */	HOLE,	NONL,	NONL,	PAD7,	PAD4,	PAD1,	HOLE,	NONL,
/* 96 */	PAD8,	PAD5,	PAD2,	PAD0,	NONL,	PAD9,	PAD6,	PAD3,
/*104 */	PADDOT,	NONL,	NONL,	HOLE,	NONL,	HOLE,	NONL,	HOLE,
/*112 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,
/*120 */	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	NONL,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* Controlled keyboard table for PC keyboard */

static keymap_entry_t keytab_pc_ct[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	c('^'),	'1',	c('@'),	'3',	'4',	'5',	c('^'),
/*  8 */	'7', 	'8',	'9',	'0',	c('_'),	'=',	HOLE,	'\b',
/* 16 */	'\t',	c('q'),	c('w'),	c('e'),	c('r'),	c('t'),	c('y'),	c('u'),
/* 24 */	c('i'),	c('o'), c('p'), c('['),	c(']'),	c('\\'),
							SHIFTKEYS+CAPSLOCK,
									c('a'),
/* 32 */	c('s'),	c('d'),	c('f'),	c('g'),	c('h'),	c('j'),	c('k'),	c('l'),
/* 40 */	';',	'\'',	'\\',	'\r',
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	c('z'),	c('x'),
/* 48 */	c('c'),	c('v'),	c('b'),	c('n'),	c('m'),	',',	'.',	c('_'),
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,
						SHIFTKEYS+LEFTALT,
							' ',	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	BF(8),	DEL,	NOP,	HOLE,
							    STRING+LEFTARROW,
/* 80 */	RF(7),	RF(13),	HOLE,
				    STRING+UPARROW,
					    STRING+DOWNARROW,
							RF(9),	RF(15),	HOLE,
/* 88 */	HOLE,
		STRING+RIGHTARROW,
			    SHIFTKEYS+NUMLOCK,
					PAD7,	PAD4,	PAD1,	HOLE,
								PADSLASH,
/* 96 */	PAD8,	PAD5,	PAD2,	PAD0,	PADSTAR,
							PAD9,	PAD6,	PAD3,
/*104 */	PADDOT,	PADMINUS,
				PADPLUS,
					HOLE,	PADENTER,
							HOLE,	ESC,	HOLE,
/*112 */	TF(1),	TF(2),	TF(3),	TF(4),	TF(5),	TF(6),	TF(7),	TF(8),
/*120 */	TF(9),	TF(10),	TF(11),	TF(12),	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* "Key Up" keyboard table for PC keyboard */


static keymap_entry_t keytab_pc_up[KEYMAP_SIZE_PC] = {
/*  0 */	HOLE,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/*  8 */	NOP, 	NOP,	NOP,	NOP,	NOP,	NOP,	HOLE,	NOP,
/* 16 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 24 */	NOP,	NOP, 	NOP, 	NOP,	NOP,	NOP,	NOP,	NOP,
/* 32 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 40 */	NOP,	NOP,	NOP,	NOP,
					SHIFTKEYS+LEFTSHIFT,
							HOLE,	NOP,	NOP,
/* 48 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/* 56 */	NOP,	SHIFTKEYS+RIGHTSHIFT,
				SHIFTKEYS+LEFTCTRL,
					HOLE,	SHIFTKEYS+LEFTALT,
							NOP,	SHIFTKEYS+
								RIGHTALT,
									HOLE,
/* 64 */        SHIFTKEYS+RIGHTCTRL,
			HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/* 72 */	HOLE,	HOLE,	HOLE,	NOP,	NOP,	NOP,	HOLE,	NOP,
/* 80 */	NOP,	NOP,	HOLE,	NOP,	NOP,	NOP,	NOP,	HOLE,
/* 88 */	HOLE,	NOP,	NOP,	NOP,	NOP,	NOP,	HOLE,	NOP,
/* 96 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/*104 */	NOP,	NOP,	NOP,	HOLE,	NOP,	HOLE,	NOP,	HOLE,
/*112 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,
/*120 */	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	NOP,	HOLE,
/*128 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*136 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*144 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*152 */	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,	HOLE,
/*160 */
};

/* END CSTYLED */

#define	M(x)	(1 << (x))
#define	MASK_ALL	(M(LEFTSHIFT) |	\
			M(RIGHTSHIFT) |	\
			CTRLMASK |	\
			ALTMASK |	\
			ALTGRAPHMASK)

/*
 * Make Ctrl+Shift+F1 be Compose.  This is SOOOO hokey.
 */
static struct exception_map exceptions_pc[] = {
	{ MASK_ALL, M(LEFTSHIFT)|M(LEFTCTRL),   112, COMPOSE, },
	{ MASK_ALL, M(LEFTSHIFT)|M(RIGHTCTRL),  112, COMPOSE, },
	{ MASK_ALL, M(RIGHTSHIFT)|M(LEFTCTRL),  112, COMPOSE, },
	{ MASK_ALL, M(RIGHTSHIFT)|M(RIGHTCTRL), 112, COMPOSE, },
	{ 0, },
};

/* Index to keymaps for PC keyboard */
struct keyboard keyindex_pc = {
	KEYMAP_SIZE_PC,
	keytab_pc_lc,
	keytab_pc_uc,
	keytab_pc_cl,
	keytab_pc_ag,
	keytab_pc_nl,
	keytab_pc_ct,
	keytab_pc_up,
	0x0000,		/* Shift bits which stay on with idle keyboard */
	0x0000,		/* Bucky bits which stay on with idle keyboard */
	112, 0,	31,	/* abort keys: F1+A */
	CAPSMASK|NUMLOCKMASK,	/* Shift bits which toggle on down event */
	exceptions_pc,	/* Exceptions */
	44, 57, 126,	/* new abort keys: Shift+Pause */
};