summaryrefslogtreecommitdiff
path: root/sys/arch/i386/isa/pcvt/pcvt_kbd.c
blob: 454030f28ce8f4e0c8988b169111936fc1b1c098 (plain)
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
/*	$OpenBSD: pcvt_kbd.c,v 1.30 1999/11/25 21:00:36 aaron Exp $	*/

/*
 * Copyright (c) 1992, 1995 Hellmuth Michaelis and Joerg Wunsch.
 *
 * Copyright (c) 1992, 1993 Brian Dunford-Shore and Holger Veit.
 *
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz and Don Ahn.
 *
 * This code is derived from software contributed to 386BSD by
 * Holger Veit.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Hellmuth Michaelis,
 *	Brian Dunford-Shore and Joerg Wunsch.
 * 4. The name authors may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * @(#)pcvt_kbd.c, 3.32, Last Edit-Date: [Tue Oct  3 11:19:48 1995]
 *
 */

/*---------------------------------------------------------------------------*
 *
 *	pcvt_kbd.c	VT220 Driver Keyboard Interface Code
 *	----------------------------------------------------
 *	-hm	------------ Release 3.00 --------------
 *	-hm	integrating NetBSD-current patches
 *	-jw	introduced kbd_emulate_pc() if scanset > 1
 *	-hm	patch from joerg for timeout in kbd_emulate_pc()
 *	-hm	starting to implement alt-shift/ctrl key mappings
 *	-hm	Gateway 2000 Keyboard fix from Brian Moore
 *	-hm	some #if adjusting for NetBSD 0.9
 *	-hm	split off pcvt_kbd.h
 *	-hm	applying Joerg's patches for FreeBSD 2.0
 *	-hm	patch from Martin, PCVT_NO_LED_UPDATE
 *	-hm	PCVT_VT220KEYB patches from Lon Willet
 *	-hm	PR #399, patch from Bill Sommerfeld: Return with PCVT_META_ESC
 *	-hm	allow keyboard-less kernel boot for serial consoles and such ..
 *	-hm	patch from Lon Willett for led-update and showkey()
 *	-hm	patch from Lon Willett to fix mapping of Control-R scancode
 *	-hm	delay patch from Martin Husemann after port-i386 ml-discussion
 *	-hm	added PCVT_NONRESP_KEYB_TRY definition to doreset()
 *	-hm	keyboard code bugfix from Jukka A. Ukkonen (ukkonen@csc.fi)
 *	-hm	---------------- Release 3.30 -----------------------
 *	-hm	patch from Frank van der Linden for keyboard state per VT
 *	-hm	removed KBDGLEDS and KBDSLEDS ioctls
 *	-hm	---------------- Release 3.32 -----------------------
 *
 *---------------------------------------------------------------------------*/

#include "vt.h"
/* #if NVT > 0 */

#include "pcvt_hdr.h"		/* global include */

#define LEDSTATE_UPDATE_PENDING (1 << 3)

static void fkey1(void), fkey2(void),  fkey3(void),  fkey4(void);
static void fkey5(void), fkey6(void),  fkey7(void),  fkey8(void);
static void fkey9(void), fkey10(void), fkey11(void), fkey12(void);

static void sfkey1(void), sfkey2(void),  sfkey3(void),  sfkey4(void);
static void sfkey5(void), sfkey6(void),  sfkey7(void),  sfkey8(void);
static void sfkey9(void), sfkey10(void), sfkey11(void), sfkey12(void);

static void cfkey1(void), cfkey2(void),  cfkey3(void),  cfkey4(void);
static void cfkey5(void), cfkey6(void),  cfkey7(void),  cfkey8(void);
static void cfkey9(void), cfkey10(void), cfkey11(void), cfkey12(void);

static inline int kbd_wait_output(void);
static inline int kbd_wait_input(void);
int kbd_response(void);

static void	doreset ( void );
static void	ovlinit ( int force );
static void 	settpmrate ( int rate );
static void	setlockkeys ( int snc );
static int	kbc_8042cmd ( int val );
static int	getokeydef ( unsigned key, struct kbd_ovlkey *thisdef );
static int 	getckeydef ( unsigned key, struct kbd_ovlkey *thisdef );
static int	rmkeydef ( int key );
static int	setkeydef ( struct kbd_ovlkey *data );
static u_char *	xlatkey2ascii( U_short key );

#if !PCVT_NO_LED_UPDATE
static int	ledstate  = LEDSTATE_UPDATE_PENDING;	/* keyboard led's */
#endif
static int	tpmrate   = KBD_TPD500|KBD_TPM100;
static u_char	altkpflag = 0;
static u_short	altkpval  = 0;

static u_short *scrollback_savedscreen = (u_short *)0;
static size_t scrnsv_size = (size_t)-1;
static void scrollback_save_screen ( void );
static void scrollback_restore_screen ( void );

extern int kbd_reset;

#include "pcvt_kbd.h"		/* tables etc */

/*---------------------------------------------------------------------------*
 *	function to switch to another virtual screen
 *---------------------------------------------------------------------------*/
static void
do_vgapage(int page)
{
	if (critical_scroll)		/* executing critical region ? */
		switch_page = page;	/* yes, auto switch later */
	else
		vgapage(page);		/* no, switch now */
}

/*
 * This code from Lon Willett enclosed in #if PCVT_UPDLED_LOSES_INTR is
 * disabled because it crashes FreeBSD 1.1.5.1 at boot time.
 * The cause is obviously that the timeout queue is not yet initialized
 * timeout is called from here the first time.
 * Anyway it is a pointer in the right direction so it is included for
 * reference here.
 */

#define PCVT_UPDLED_LOSES_INTR	0	/* disabled for now */

#if PCVT_UPDLED_LOSES_INTR

/*---------------------------------------------------------------------------*
 *	check for lost keyboard interrupts
 *---------------------------------------------------------------------------*/

/*
 * The two commands to change the LEDs generate two KEYB_R_ACK responses
 * from the keyboard, which aren't explicitly checked for (maybe they
 * should be?).  However, when a lot of other I/O is happening, one of
 * the interrupts sometimes gets lost (I'm not sure of the details of
 * how and why and what hardware this happens with).
 *
 * This is a real problem, because normally the keyboard is only polled
 * by pcrint(), and no more interrupts will be generated until the ACK
 * has been read.  So the keyboard is hung.  This code polls a little
 * while after changing the LEDs to make sure that this hasn't happened.
 *
 * XXX Quite possibly we should poll the kbd on a regular basis anyway,
 * in the interest of robustness.  It may be possible that interrupts
 * get lost other times as well.
 */

static int lost_intr_timeout_queued = 0;

static void
check_for_lost_intr (void *arg)
{
	lost_intr_timeout_queued = 0;
	if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF) {
		int opri = spltty();
		(void)pcrint();
		splx(opri);
	}
}

#endif /* PCVT_UPDLED_LOSES_INTR */

/*---------------------------------------------------------------------------*
 *	update keyboard led's
 *---------------------------------------------------------------------------*/
void
update_led(void)
{
#if !PCVT_NO_LED_UPDATE
	/* Don't update LED's unless necessary. */

	int opri, new_ledstate, response1, response2;

	opri = spltty();
	new_ledstate = ((vsp->scroll_lock) | (vsp->num_lock * 2) |
			(vsp->caps_lock * 4));

	if (new_ledstate != ledstate) {
		ledstate = LEDSTATE_UPDATE_PENDING;

		if (kbd_cmd(KEYB_C_LEDS) != 0) {
			printf("Keyboard LED command timeout\n");
			splx(opri);
			return;
		}

		/*
		 * For some keyboards or keyboard controllers, it is an
		 * error to issue a command without waiting long enough
		 * for an ACK for the previous command.  The keyboard
		 * gets confused, and responds with KEYB_R_RESEND, but
		 * we ignore that.  Wait for the ACK here.  The busy
		 * waiting doesn't matter much, since we lose anyway by
		 * busy waiting to send the command.
		 *
		 * XXX actually wait for any response, since we can't
		 * handle normal scancodes here.
		 *
		 * XXX all this should be interrupt driven.  Issue only
		 * one command at a time wait for a ACK before proceeding.
		 * Retry after a timeout or on receipt of a KEYB_R_RESEND.
		 * KEYB_R_RESENDs seem to be guaranteed by working
		 * keyboard controllers with broken (or disconnected)
		 * keyboards.  There is another code for keyboard
		 * reconnects.  The keyboard hardware is very simple and
		 * well designed :-).
		 */
		response1 = kbd_response();

		if (kbd_cmd(new_ledstate) != 0) {
			printf("Keyboard LED data timeout\n");
			splx(opri);
			return;
		}
		response2 = kbd_response();

		if (response1 == KEYB_R_ACK && response2 == KEYB_R_ACK)
			ledstate = new_ledstate;
		else
			printf(
			"Keyboard LED command not ACKed (responses %#x %#x)\n",
				response1, response2);

#if PCVT_UPDLED_LOSES_INTR
		if (lost_intr_timeout_queued)
			untimeout (check_for_lost_intr, (void *)NULL);

		timeout (check_for_lost_intr, (void *)NULL, hz);
		lost_intr_timeout_queued = 1;
#endif /* PCVT_UPDLED_LOSES_INTR */

	}
	splx(opri);
#endif /* !PCVT_NO_LED_UPDATE */
}

/*---------------------------------------------------------------------------*
 *	set typematic rate
 *---------------------------------------------------------------------------*/
static void
settpmrate(int rate)
{
	int opri, response1, response2;

	opri = spltty();
	tpmrate = rate & 0x7f;
	if (kbd_cmd(KEYB_C_TYPEM) != 0) {
		printf("Keyboard TYPEMATIC command timeout\n");
		goto fail;
	}
	response1 = kbd_response();		/* wait for ACK */

	if (kbd_cmd(tpmrate) != 0) {
		printf("Keyboard TYPEMATIC data timeout\n");
		goto fail;
	}
	response2 = kbd_response();		/* wait for ACK */

	if (response1 != KEYB_R_ACK || response2 != KEYB_R_ACK) {
		printf(
		   "Keyboard TYPEMATIC command not ACKed (responses %#x %#x)\n",
		   response1, response2);
	}
fail:
	splx(opri);
	return;
}

/*---------------------------------------------------------------------------*
 *	Pass command to keyboard controller (8042)
 *---------------------------------------------------------------------------*/
static inline int
kbd_wait_output()
{
	u_int i;

	/* > 100 msec */
	for (i = 100; i; i--) {
		if ((inb(CONTROLLER_CTRL) & STATUS_INPBF) == 0) {
			PCVT_KBD_DELAY();
			return 1;
		}
		DELAY(1000);
	}
	return 0;
}

static inline int
kbd_wait_input()
{
	u_int i;

	/* > 500 msec */
	for (i = 500; i; i--) {
		if ((inb(CONTROLLER_CTRL) & STATUS_OUTPBF) != 0) {
			PCVT_KBD_DELAY();
			return 1;
		}
		DELAY(1000);
	}
	return 0;
}

static int
kbc_8042cmd(int val)
{
	if (!kbd_wait_output())
		return (-1);
	outb(CONTROLLER_CTRL, val);

	return (0);
}

/*---------------------------------------------------------------------------*
 *	Pass command to keyboard itself
 *---------------------------------------------------------------------------*/
int
kbd_cmd(int val)
{
	if (!kbd_wait_output())
		return (-1);
	outb(CONTROLLER_DATA, val);

	return (0);
}

/*---------------------------------------------------------------------------*
 *	Read response from keyboard
 *	NB: make sure to call spltty() before kbd_cmd(), kbd_response().
 *---------------------------------------------------------------------------*/
int
kbd_response(void)
{
	u_char ch;

	if (!kbd_wait_input())
		return (-1);
	ch = inb(CONTROLLER_DATA);

	return ch;
}

/*---------------------------------------------------------------------------*
 *	switch to/from PC scan code emulation mode
 *---------------------------------------------------------------------------*/
void
kbd_setmode(int mode)
{
#if PCVT_SCANSET > 1		/* switch only if we are running scancode 2 */
	int cmd;
#if PCVT_USEKBDSEC
	cmd = COMMAND_SYSFLG | COMMAND_IRQEN;
#else
	cmd = COMMAND_INHOVR | COMMAND_SYSFLG | COMMAND_IRQEN;
#endif

	if (mode == K_RAW)		/* switch to scancode 1 ? */
		cmd |= COMMAND_PCSCAN;	/*     yes, setup command */

	kbc_8042cmd(CONTR_WRITE);
	kbd_cmd(cmd);
	
#endif /* PCVT_SCANSET > 1 */

	if (mode == K_RAW)
		shift_down = meta_down = altgr_down = ctrl_down = 0;
}


#ifndef PCVT_NONRESP_KEYB_TRY
#define PCVT_NONRESP_KEYB_TRY	25	/* no of times to try to detect	*/
#endif					/* a nonresponding keyboard	*/

/*---------------------------------------------------------------------------*
 *	try to force keyboard into a known state ..
 *---------------------------------------------------------------------------*/
static
void doreset(void)
{
	int again = 0;
	int once = 0;
	int response, opri;
	unsigned int wait_retries, seen_negative_response;

	/* Enable interrupts and keyboard, etc. */
	if (kbc_8042cmd(CONTR_WRITE) != 0)
		printf("pcvt: doreset() - timeout controller write command\n");

#if PCVT_USEKBDSEC		/* security enabled */

#  if PCVT_SCANSET == 2
#    define KBDINITCMD COMMAND_SYSFLG|COMMAND_IRQEN
#  else /* PCVT_SCANSET != 2 */
#    define KBDINITCMD COMMAND_PCSCAN|COMMAND_SYSFLG|COMMAND_IRQEN
#  endif /* PCVT_SCANSET == 2 */

#else /* ! PCVT_USEKBDSEC */	/* security disabled */

#  if PCVT_SCANSET == 2
#    define KBDINITCMD COMMAND_INHOVR|COMMAND_SYSFLG|COMMAND_IRQEN
#  else /* PCVT_SCANSET != 2 */
#    define KBDINITCMD COMMAND_PCSCAN|COMMAND_INHOVR|COMMAND_SYSFLG\
	|COMMAND_IRQEN
#  endif /* PCVT_SCANSET == 2 */

#endif /* PCVT_USEKBDSEC */

	if (kbd_cmd(KBDINITCMD) != 0)
		printf("pcvt: doreset() - timeout writing keyboard init command\n");

	/*
	 * Discard any stale keyboard activity.  The 0.1 boot code isn't
	 * very careful and sometimes leaves a KEYB_R_RESEND.
	 */
	while (1) {
		if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF)
			kbd_response();
		else {
			DELAY(10000);
			if (!(inb(CONTROLLER_CTRL) & STATUS_OUTPBF))
				break;
		}
	}

	/* Start keyboard reset */
	opri = spltty();

	if (kbd_cmd(KEYB_C_RESET) != 0)
		printf("pcvt: doreset() - keyboard reset command timeout\n");

	/* Wait for the first response to reset and handle retries */
	while ((response = kbd_response()) != KEYB_R_ACK) {
		if (response < 0) {
			if (!again)	/* print message only once ! */
				printf("pcvt: doreset() - response != ack and response < 0 [one time only msg]\n");
			response = KEYB_R_RESEND;
		}
		if (response == KEYB_R_RESEND) {
			if (!again)	/* print message only once ! */
				printf("pcvt: doreset() - got KEYB_R_RESEND response ... [one time only msg]\n");

			if (++again > PCVT_NONRESP_KEYB_TRY) {
				printf("pcvt: doreset() - Caution - no PC keyboard detected!\n");
				keyboard_type = KB_UNKNOWN;
				splx(opri);
				return;
			}

			if ((kbd_cmd(KEYB_C_RESET) != 0) && (once == 0)) {
				once++;		/* print message only once ! */
				printf("pcvt: doreset() - timeout for loop keyboard reset command [one time only msg]\n");
			}
		}
	}

	/* Wait for the second response to reset */

	wait_retries = seen_negative_response = 0;

	while ((response = kbd_response()) != KEYB_R_SELFOK) {
		/*
		 *  Let's be a little more tolerant here...
		 *  Receiving a -1 could indicate that the keyboard
		 *  is not ready to answer just yet.
		 *  Such cases have been noticed with e.g. Alps Membrane.
		 */

		if (response < 0)
			seen_negative_response = 1;

		if (seen_negative_response && (wait_retries >= 10)) {
			printf("pcvt: doreset() - response != OK and response < 0\n");

			/*
			 * If KEYB_R_SELFOK never arrives, the loop will
			 * finish here unless the keyboard babbles or
			 * STATUS_OUTPBF gets stuck.
			 */
			break;
		}
		wait_retries++;
	}

#if PCVT_SCANSET == 1
	/* 
	 * Pcvt has been compiled for scanset 1, which requires that
	 * the mainboard controller translates. If it is not able to,
	 * try to set the keyboard to XT mode so that pcvt will see AT
	 * scan codes after all. If it fails, we're out of luck.
	 */
	kbc_8042cmd(CONTR_READ);
	response = kbd_response();

	if (!(response & COMMAND_PCSCAN)) {
		if (kbd_cmd(KEYB_C_SCANSET) != 0)
			printf("pcvt: doreset() - keyboard SCANSET command timeout\n");
		else if (kbd_cmd(1) != 0)
			printf("pcvt: doreset() - keyboard SCANSET data timeout\n");
		else
			printf("pcvt: doreset() - keyboard set to XT mode\n");
	 }
#endif

	splx(opri);

#if PCVT_KEYBDID

query_kbd_id:

	opri = spltty();

	if (kbd_cmd(KEYB_C_ID) != 0) {
		printf("pcvt: doreset() - timeout for keyboard ID command\n");
		keyboard_type = KB_UNKNOWN;
	}
	else {
r_entry:
		if ((response = kbd_response()) == KEYB_R_MF2ID1) {
			switch ((response = kbd_response())) {
			case KEYB_R_RESEND:
				/*
				 *  Let's give other priority levels
				 *  a chance instead of blocking at
				 *  tty level.
				 */
				splx(opri);
				goto query_kbd_id;
					
			case KEYB_R_MF2ID2:
			case KEYB_R_MF2ID2HP:
			case KEYB_R_MF2ID2TP:
			case KEYB_R_MF2ID2TP2:
				keyboard_type = KB_MFII;
				break;

			default:
				printf("\npcvt: doreset() - kbdid, response 2 = [%d]\n", response);
				keyboard_type = KB_UNKNOWN;
				break;
			}
		}
		else if (response == KEYB_R_ACK)
			goto r_entry;
		else if (response == -1)
			keyboard_type = KB_AT;
		else {
			printf("\npcvt: doreset() - kbdid, response 1 = [%d]\n",
			    response);
		}
	}
	splx(opri);

#else /* PCVT_KEYBDID */
	keyboard_type = KB_MFII;	/* force it .. */
#endif /* PCVT_KEYBDID */
}

/*---------------------------------------------------------------------------*
 *	init keyboard code
 *---------------------------------------------------------------------------*/
void
kbd_code_init(void)
{
	doreset();
	ovlinit(0);
	keyboard_is_initialized = 1;
}

/*---------------------------------------------------------------------------*
 *	init keyboard code, this initializes the keyboard subsystem
 *	just "a bit" so the very very first ddb session is able to
 *	get proper keystrokes - in other words, it's a hack ....
 *---------------------------------------------------------------------------*/
void
kbd_code_init1(void)
{
	doreset();
	keyboard_is_initialized = 1;
}

/*---------------------------------------------------------------------------*
 *	init keyboard overlay table
 *---------------------------------------------------------------------------*/
static
void ovlinit(int force)
{
	int i;

	if (force || ovlinitflag == 0) {
		if (ovlinitflag == 0 &&
		   (ovltbl = (Ovl_tbl *)malloc(sizeof(Ovl_tbl) * OVLTBL_SIZE,
					       M_DEVBUF, M_WAITOK)) == NULL)
			panic("pcvt_kbd: malloc of Ovl_tbl failed");

		for(i = 0; i < OVLTBL_SIZE; i++) {
			ovltbl[i].keynum =
			ovltbl[i].type = 0;
			ovltbl[i].unshift[0] =
			ovltbl[i].shift[0] =
			ovltbl[i].ctrl[0] =
			ovltbl[i].altgr[0] = 0;
			ovltbl[i].subu =
			ovltbl[i].subs =
			ovltbl[i].subc =
			ovltbl[i].suba = KBD_SUBT_STR;	/* just strings .. */
		}
		for(i=0; i<=MAXKEYNUM; i++)
			key2ascii[i].type &= KBD_MASK;
		ovlinitflag = 1;
	}
}

/*---------------------------------------------------------------------------*
 *	get original key definition
 *---------------------------------------------------------------------------*/
static int
getokeydef(unsigned key, Ovl_tbl *thisdef)
{
	if (key == 0 || key > MAXKEYNUM)
		return EINVAL;

	thisdef->keynum = key;
	thisdef->type = key2ascii[key].type;

	if (key2ascii[key].unshift.subtype == STR) {
		bcopy((u_char *)(key2ascii[key].unshift.what.string),
		    thisdef->unshift, CODE_SIZE);
		thisdef->subu = KBD_SUBT_STR;
	}
	else {
		bcopy("", thisdef->unshift, CODE_SIZE);
		thisdef->subu = KBD_SUBT_FNC;
	}

	if (key2ascii[key].shift.subtype == STR) {
		bcopy((u_char *)(key2ascii[key].shift.what.string),
		    thisdef->shift, CODE_SIZE);
		thisdef->subs = KBD_SUBT_STR;
	}
	else {
		bcopy("", thisdef->shift,CODE_SIZE);
		thisdef->subs = KBD_SUBT_FNC;
	}

	if (key2ascii[key].ctrl.subtype == STR) {
		bcopy((u_char *)(key2ascii[key].ctrl.what.string),
		    thisdef->ctrl, CODE_SIZE);
		thisdef->subc = KBD_SUBT_STR;
	}
	else {
		bcopy("",thisdef->ctrl,CODE_SIZE);
		thisdef->subc = KBD_SUBT_FNC;
	}

	/* deliver at least anything for ALTGR settings ... */

	if (key2ascii[key].unshift.subtype == STR) {
		bcopy((u_char *)(key2ascii[key].unshift.what.string),
		    thisdef->altgr, CODE_SIZE);
		thisdef->suba = KBD_SUBT_STR;
	}
	else {
		bcopy("", thisdef->altgr, CODE_SIZE);
		thisdef->suba = KBD_SUBT_FNC;
	}
	return 0;
}

/*---------------------------------------------------------------------------*
 *	get current key definition
 *---------------------------------------------------------------------------*/
static int
getckeydef(unsigned key, Ovl_tbl *thisdef)
{
	u_short type = key2ascii[key].type;

	if (key > MAXKEYNUM)
		return EINVAL;

	if (type & KBD_OVERLOAD)
		*thisdef = ovltbl[key2ascii[key].ovlindex];
	else
		getokeydef(key,thisdef);

	return 0;
}

/*---------------------------------------------------------------------------*
 *	translate keynumber and returns ptr to associated ascii string
 *	if key is bound to a function, executes it, and ret empty ptr
 *---------------------------------------------------------------------------*/
static u_char *
xlatkey2ascii(U_short key)
{
	int		n;
	static u_char	capchar[2] = {0, 0};
#if PCVT_META_ESC
	static u_char	metachar[3] = {0x1b, 0, 0};
#else
	static u_char	metachar[2] = {0, 0};
#endif
	static Ovl_tbl	thisdef;
	static u_char altgr_shft_key[KBDMAXOVLKEYSIZE];
	
	void		(*fnc)(void);

	/* ignore the NON-KEY */
	if (key == 0)
		return 0;

	/* get the current ASCII value */
	getckeydef(key & 0x7F, &thisdef);

	thisdef.type &= KBD_MASK;

	if (key & 0x80) {		/* special handling of ALT-KEYPAD */
		/* is the ALT Key released? */
		if (thisdef.type == KBD_META || thisdef.type == KBD_ALTGR) {
			if (altkpflag) { /* have we been in altkp mode? */
				capchar[0] = altkpval;
				altkpflag = 0;
				altkpval = 0;
				return capchar;
			}
		}
		return 0;
	}

	switch (thisdef.type) {		/* convert the keys */
	case KBD_BREAK:
	case KBD_ASCII:
	case KBD_FUNC:
		fnc = NULL;
		more_chars = NULL;

		if (altgr_down) {
			/* XXX this is hack to support simple AltGr + Shift
	 	 	 * remapping. This should work for KOI-8 keymap style.
			 */
			if (shift_down) {
                		altgr_shft_key[0] = *(u_char*)thisdef.altgr+040;
                		more_chars = (u_char*)altgr_shft_key;
                	}
                	else
				more_chars = (u_char *)thisdef.altgr;
		}
		else if (!ctrl_down && (shift_down || vsp->shift_lock)) {
			if (key2ascii[key].shift.subtype == STR)
				more_chars = (u_char *)thisdef.shift;
			else
				fnc = key2ascii[key].shift.what.func;
		     }
		else if (ctrl_down) {
			if (key2ascii[key].ctrl.subtype == STR)
				more_chars = (u_char *)thisdef.ctrl;
			else
				fnc = key2ascii[key].ctrl.what.func;
		     }
		else {
			if (key2ascii[key].unshift.subtype == STR)
				more_chars = (u_char *)thisdef.unshift;
			else
				fnc = key2ascii[key].unshift.what.func;
		}

		if (fnc)
			(*fnc)();	/* execute function */

		if ((more_chars != NULL) && (more_chars[1] == 0)) {
			if (vsp->caps_lock && more_chars[0] >= 'a'
			   && more_chars[0] <= 'z') {
				capchar[0] = *more_chars - ('a' - 'A');
				more_chars = capchar;
			}
			if (meta_down) {
#if PCVT_META_ESC
				metachar[1] = *more_chars;
#else
				metachar[0] = *more_chars | 0x80;
#endif
				more_chars = metachar;
			}
		}
		return (more_chars);

	case KBD_KP:
		fnc = NULL;
		more_chars = NULL;

		if (meta_down) {
			switch (key) {
			case 95:	/* / */
				altkpflag = 0;
				more_chars = (u_char *)"\033OQ";
				return (more_chars);

			case 100:	/* * */
				altkpflag = 0;
				more_chars = (u_char *)"\033OR";
				return (more_chars);

			case 105:	/* - */
				altkpflag = 0;
		 		more_chars = (u_char *)"\033OS";
				return (more_chars);
			}
		}

		if (meta_down || altgr_down) {
			if ((n = keypad2num[key-91]) >= 0) {
				if (!altkpflag) {
					/* start ALT-KP mode */
					altkpflag = 1;
					altkpval = 0;
				}
				altkpval *= 10;
				altkpval += n;
			}
			else
				altkpflag = 0;
			return 0;
		}

		if (!vsp->num_lock) {
			if (key2ascii[key].shift.subtype == STR)
				more_chars = (u_char *)thisdef.shift;
			else
				fnc = key2ascii[key].shift.what.func;
		}
		else {
			if (key2ascii[key].unshift.subtype == STR)
				more_chars = (u_char *)thisdef.unshift;
			else
				fnc = key2ascii[key].unshift.what.func;
		}

		if (fnc)
			(*fnc)();	/* execute function */

		return (more_chars);

	case KBD_CURSOR:
		fnc = NULL;
		more_chars = NULL;

		if (vsp->ckm) {
			if (key2ascii[key].shift.subtype == STR)
				more_chars = (u_char *)thisdef.shift;
			else
				fnc = key2ascii[key].shift.what.func;
		}
		else {
			if (key2ascii[key].unshift.subtype == STR)
				more_chars = (u_char *)thisdef.unshift;
			else
				fnc = key2ascii[key].unshift.what.func;
		}

		if (fnc)
			(*fnc)();	/* execute function */

		return (more_chars);

	case KBD_NUM:		/*  special kp-num handling */
		more_chars = NULL;

		if (meta_down)
			more_chars = (u_char *)"\033OP"; /* PF1 */
		else {
			vsp->num_lock ^= 1;
			update_led();
		}
		return (more_chars);

	case KBD_RETURN:
		more_chars = NULL;

		if (!vsp->num_lock)
			more_chars = (u_char *)thisdef.shift;
		else
			more_chars = (u_char *)thisdef.unshift;

		if (vsp->lnm && (*more_chars == '\r'))
			more_chars = (u_char *)"\r\n"; /* CR LF */

		if (meta_down) {
#if PCVT_META_ESC
			metachar[1] = *more_chars;
#else
			metachar[0] = *more_chars | 0x80;
#endif
			more_chars = metachar;
		}
		return (more_chars);

	case KBD_META:		/* these keys are	*/
	case KBD_ALTGR:		/*  handled directly	*/
	case KBD_SCROLL:	/*  by the keyboard	*/
	case KBD_CAPS:		/*  handler - they are	*/
	case KBD_SHFTLOCK:	/*  ignored here	*/
	case KBD_CTL:
	case KBD_NONE:
	default:
		return 0;
	}
}

/*---------------------------------------------------------------------------*
 *	get keystrokes from the keyboard.
 *	if noblock = 0, wait until a key is pressed.
 *	else return NULL if no characters present.
 *---------------------------------------------------------------------------*/

#if PCVT_KBD_FIFO
extern	u_char	pcvt_kbd_fifo[];
extern	int	pcvt_kbd_rptr;
extern	short	pcvt_kbd_count;
#endif

u_char *
sgetc(int noblock)
{
#if PCVT_KBD_FIFO
	int		s;
#endif
	u_char *cp, dt, key;
	u_short	type;
	static u_char kbd_lastkey = 0; /* last keystroke */
#ifdef XSERVER
	static char	keybuf[2] = {0}; /* the second 0 is a delimiter! */
#endif /* XSERVER */

	static struct {
		u_char extended: 1;	/* extended prefix seen */
		u_char ext1: 1;		/* extended prefix 1 seen */
		u_char breakseen: 1;	/* break code seen */
		u_char vshift: 1;	/* virtual shift pending */
		u_char vcontrol: 1;	/* virtual control pending */
		u_char sysrq: 1;	/* sysrq pressed */
	} kbd_status = {0};

loop:

#ifdef XSERVER

#if PCVT_KBD_FIFO

	if (noblock == 31337) {
		vsp->scrolling = 1;
		goto scroll_reset;
	}

	/* see if there is data from the keyboard available either from */
	/* the keyboard fifo or from the 8042 keyboard controller	*/

	if ((( noblock) && (pcvt_kbd_count)) ||
	    ((!noblock) && (inb(CONTROLLER_CTRL) & STATUS_OUTPBF))) {
		if (!noblock) {		/* source = 8042 */
			PCVT_KBD_DELAY();	/* 7 us delay */
			dt = inb(CONTROLLER_DATA);	/* get from obuf */
		}
		else {			/* source = keyboard fifo */
			dt = pcvt_kbd_fifo[pcvt_kbd_rptr++];
			s = spltty();
			pcvt_kbd_count--;
			splx(s);
			if (pcvt_kbd_rptr >= PCVT_KBD_FIFO_SZ)
				pcvt_kbd_rptr = 0;
		}

#else /* !PCVT_KB_FIFO */

	/* see if there is data from the keyboard available from the 8042 */
	if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF) {
		PCVT_KBD_DELAY();		/* 7 us delay */
		dt = inb(CONTROLLER_DATA);	/* yes, get data */
#endif /* !PCVT_KBD_FIFO */

		/*
		 * If x mode is active, only care for locking keys, then
		 * return the scan code instead of any key translation.
		 * Additionally, this prevents us from any attempts to
		 * execute pcvt internal functions caused by keys (such
		 * as screen flipping).
		 */
 		if (vsp->kbd_state == K_RAW) {
			keybuf[0] = dt;
			return ((u_char *)keybuf);
		}
	}

#else /* !XSERVER */

#  if PCVT_KBD_FIFO

	/* see if there is data from the keyboard available either from */
	/* the keyboard fifo or from the 8042 keyboard controller	*/

	if ((( noblock) && (pcvt_kbd_count)) ||
	    ((!noblock) && (inb(CONTROLLER_CTRL) & STATUS_OUTPBF))) {
		if (!noblock) {		/* source = 8042 */
			PCVT_KBD_DELAY();	/* 7 us delay */
			dt = inb(CONTROLLER_DATA);
		}
		else {			/* source = keyboard fifo */
			dt = pcvt_kbd_fifo[pcvt_kbd_rptr++]; /* yes, get it ! */
			s = spltty();
			pcvt_kbd_count--;
			splx(s);
			if (pcvt_kbd_rptr >= PCVT_KBD_FIFO_SZ)
				pcvt_kbd_rptr = 0;
		}
	}

#else /* !PCVT_KBD_FIFO */

	/* see if there is data from the keyboard available from the 8042 */
	if (inb(CONTROLLER_CTRL) & STATUS_OUTPBF) {
		PCVT_KBD_DELAY();		/* 7 us delay */
		dt = inb(CONTROLLER_DATA);	/* yes, get data ! */
	}

#endif /* !PCVT_KBD_FIFO */

#endif /* !XSERVER */

	else {
		if (noblock)
			return NULL;
		else
			goto loop;
	}

	/* lets look what we got */
	switch (dt) {
	case KEYB_R_OVERRUN0:	/* keyboard buffer overflow */
#if PCVT_SCANSET == 2
	case KEYB_R_SELFOK:	/* keyboard selftest ok */
#endif /* PCVT_SCANSET == 2 */
	case KEYB_R_ECHO:	/* keyboard response to KEYB_C_ECHO */
	case KEYB_R_ACK:	/* acknowledge after command has rx'd*/
	case KEYB_R_SELFBAD:	/* keyboard selftest FAILED */
	case KEYB_R_DIAGBAD:	/* keyboard self diagnostic failure */
	case KEYB_R_RESEND:	/* keyboard wants us to resend cmnd */
	case KEYB_R_OVERRUN1:	/* keyboard buffer overflow */
		break;

	case KEYB_R_EXT1:	/* keyboard extended scancode pfx 2 */
		kbd_status.ext1 = 1;
		/* FALLTHROUGH */

	case KEYB_R_EXT0:	/* keyboard extended scancode pfx 1 */
		kbd_status.extended = 1;
		break;

#if PCVT_SCANSET == 2
	case KEYB_R_BREAKPFX:	/* break code prefix for set 2 and 3 */
		kbd_status.breakseen = 1;
		break;
#endif /* PCVT_SCANSET == 2 */

	default:
		goto regular;	/* regular key */
	}

	if (noblock)
		return NULL;
	else
		goto loop;

	/* got a normal scan key */
regular:
#if PCVT_SCANSET == 1
	kbd_status.breakseen = dt & 0x80 ? 1 : 0;
	dt &= 0x7f;
#endif	/* PCVT_SCANSET == 1 */

	/*   make a keycode from scan code	*/
	if (dt >= sizeof scantokey / sizeof(u_char))
		key = 0;
	else
		key = kbd_status.extended ? extscantokey[dt] : scantokey[dt];

	if (kbd_status.ext1 && key == 64)
		/* virtual control key */
		key = 129;

	kbd_status.extended = kbd_status.ext1 = 0;

	if ((key == 85) && shift_down &&
	    (kbd_lastkey != 85 || !kbd_status.breakseen)) {
		if (vsp->scr_offset > vsp->row) {
			if (!vsp->scrolling) {
				vsp->scrolling += vsp->row - 1;
				if (vsp->Scrollback) {
					scrollback_save_screen();
					if (vsp->scr_offset == vsp->max_off) {
						bcopy(vsp->Scrollback +
						      vsp->maxcol,
						      vsp->Scrollback,
						      vsp->maxcol *
						      vsp->max_off * CHR);
						vsp->scr_offset--;
					}
					bcopy(vsp->Crtat + vsp->cur_offset -
					      vsp->col, vsp->Scrollback +
				      	      (vsp->scr_offset + 1) *
					      vsp->maxcol, vsp->maxcol * CHR);
				}

				if (vsp->cursor_on)
					sw_cursor(0);
			}

			vsp->scrolling += vsp->screen_rows - 1;
			if (vsp->scrolling > vsp->scr_offset)
				vsp->scrolling = vsp->scr_offset;

			bcopy(vsp->Scrollback + ((vsp->scr_offset -
			      vsp->scrolling) * vsp->maxcol), vsp->Crtat,
			      vsp->screen_rows * vsp->maxcol * CHR);
		}

		kbd_lastkey = 85;
		goto loop;
	}
	else if ((key == 86) && shift_down &&
		(kbd_lastkey != 86 || !kbd_status.breakseen)) {
scroll_reset:
		if (vsp->scrolling > 0) {
			vsp->scrolling -= vsp->screen_rows - 1;
			if (vsp->scrolling < 0)
				vsp->scrolling = 0;

			if (vsp->scrolling <= vsp->row) {
				vsp->scrolling = 0;
				scrollback_restore_screen();
			}
			else {
				if (vsp->scrolling + 2 < vsp->screen_rows)
					fillw(user_attr | ' ',
					      (caddr_t)vsp->Crtat,
					      vsp->screen_rows * vsp->maxcol);

				bcopy(vsp->Scrollback + ((vsp->scr_offset -
			      	      vsp->scrolling) * vsp->maxcol),
			              vsp->Crtat, (vsp->scrolling + 2 <
				      vsp->screen_rows ? vsp->scrolling + 2 :
				      vsp->screen_rows) * vsp->maxcol * CHR);
			}
		}

		if (vsp->scrolling == 0) {
			if (vsp->cursor_on)
				sw_cursor(1);
		}

		if (noblock == 31337)
			return NULL;

		if (key != 86)
			goto regular;
		else {
			kbd_lastkey = 86;
			goto loop;
		}
	}
	else if (vsp->scrolling && key != 128 && key != 44 && key != 85 &&
		 key != 86) {
			vsp->scrolling = 1;
			goto scroll_reset;
	     }

	if (kbd_reset && (key == 76) && ctrl_down && (meta_down||altgr_down)) {
		printf("\nconsole halt requested: going down.\n");
		kbd_reset = 0;
		psignal(initproc, SIGUSR1);
	}

#if NDDB > 0 || defined(DDB)		 /*   Check for cntl-alt-esc	*/
  	if ((key == 110) && ctrl_down && (meta_down || altgr_down)) {
 		static u_char in_Debugger;

 		if (!in_Debugger) {
 			in_Debugger = 1;
			if (db_console)
	 			Debugger();
 			in_Debugger = 0;

 			if (noblock)
 				return NULL;
 			else
 				goto loop;
 		}
 	}
#endif /* NDDB > 0 || defined(DDB) */

	/* look for keys with special handling */
	if (key == 128) {
		/*
		 * virtual shift; sent around PrtScr, and around the arrow
		 * keys if the NumLck LED is on
		 */
		kbd_status.vshift = !kbd_status.breakseen;
		key = 0;	/* no key */
	}
	else if (key == 129) {
		/*
		 * virtual control - the most ugly thingie at all
		 * the Pause key sends:
		 * <virtual control make> <numlock make> <virtual control
		 * break> <numlock break>
		 */
		if (!kbd_status.breakseen)
			kbd_status.vcontrol = 1;
		/* else: let the numlock hook clear this */
		key = 0;	/* no key */
	}
	else if (key == 90) {
		/* NumLock, look whether this is rather a Pause */
		if (kbd_status.vcontrol)
			key = 126;
		/*
		 * if this is the final break code of a Pause key,
		 * clear the virtual control status, too
		 */
		if (kbd_status.vcontrol && kbd_status.breakseen)
			kbd_status.vcontrol = 0;
	}
	else if (key == 127) {
		/*
		 * a SysRq; some keyboards are brain-dead enough to
		 * repeat the SysRq key make code by sending PrtScr
		 * make codes; other keyboards do not repeat SysRq
		 * at all. We keep track of the SysRq state here.
		 */
		kbd_status.sysrq = !kbd_status.breakseen;
	}
	else if (key == 124) {
		/*
		 * PrtScr; look whether this is really PrtScr or rather
		 * a silly repeat of a SysRq key
		 */
		if (kbd_status.sysrq)
			/* ignore the garbage */
			key = 0;
	}

	/* in NOREPEAT MODE ignore the key if it was the same as before */

	if (!kbrepflag && key == kbd_lastkey && !kbd_status.breakseen) {
		if (noblock)
			return NULL;
		else
			goto loop;
	}

	type = key2ascii[key].type;

	if (type & KBD_OVERLOAD)
		type = ovltbl[key2ascii[key].ovlindex].type;

	type &= KBD_MASK;

	switch (type) {
	case KBD_SHFTLOCK:
		if (!kbd_status.breakseen && key != kbd_lastkey)
			vsp->shift_lock ^= 1;
		break;

	case KBD_CAPS:
		if (!kbd_status.breakseen && key != kbd_lastkey) {
			vsp->caps_lock ^= 1;
			update_led();
		}
		break;

	case KBD_SCROLL:
		if (!kbd_status.breakseen && key != kbd_lastkey) {
			vsp->scroll_lock ^= 1;
			update_led();

			if (!(vsp->scroll_lock))
				wakeup((caddr_t)&(vsp->scroll_lock));
		}
		break;

	case KBD_SHIFT:
		shift_down = kbd_status.breakseen ? 0 : 1;
		break;

	case KBD_META:
		meta_down = kbd_status.breakseen ? 0 : 0x80;
		break;

	case KBD_ALTGR:
		altgr_down = kbd_status.breakseen ? 0 : 1;
		break;

	case KBD_CTL:
		ctrl_down = kbd_status.breakseen ? 0 : 1;
		break;

	case KBD_NONE:
	default:
		break;			/* deliver a key */
	}

	if (kbd_status.breakseen) {
		key |= 0x80;
		kbd_status.breakseen = 0;
		kbd_lastkey = 0; /* -hv- I know this is a bug with */
	}			 /* N-Key-Rollover, but I ignore that */
	else			 /* because avoidance is too complicated */
		kbd_lastkey = key;

	cp = xlatkey2ascii(key);	/* have a key */

	if (cp == NULL && !noblock)
		goto loop;

	return cp;
}

/*---------------------------------------------------------------------------*
 *	reflect status of locking keys & set led's
 *---------------------------------------------------------------------------*/
static void
setlockkeys(int snc)
{
	vsp->scroll_lock = snc & 1;
	vsp->num_lock	 = (snc & 2) ? 1 : 0;
	vsp->caps_lock	 = (snc & 4) ? 1 : 0;
	update_led();
}

/*---------------------------------------------------------------------------*
 *	remove a key definition
 *---------------------------------------------------------------------------*/
static int
rmkeydef(int key)
{
	register Ovl_tbl *ref;

	if (key == 0 || key > MAXKEYNUM)
		return EINVAL;

	if (key2ascii[key].type & KBD_OVERLOAD) {
		ref = &ovltbl[key2ascii[key].ovlindex];
		ref->keynum = 0;
		ref->type = 0;
		ref->unshift[0] =
		ref->shift[0] =
		ref->ctrl[0] =
		ref->altgr[0] = 0;
		key2ascii[key].type &= KBD_MASK;
	}
	return 0;
}

/*---------------------------------------------------------------------------*
 *	overlay a key
 *---------------------------------------------------------------------------*/
static int
setkeydef(Ovl_tbl *data)
{
	int i;

	if ( data->keynum > MAXKEYNUM		 ||
	    (data->type & KBD_MASK) == KBD_BREAK ||
	    (data->type & KBD_MASK) > KBD_SHFTLOCK)
		return EINVAL;

	data->unshift[KBDMAXOVLKEYSIZE] =
	data->shift[KBDMAXOVLKEYSIZE] =
	data->ctrl[KBDMAXOVLKEYSIZE] =
	data->altgr[KBDMAXOVLKEYSIZE] = 0;

	data->subu =
	data->subs =
	data->subc =
	data->suba = KBD_SUBT_STR;		/* just strings .. */

	data->type |= KBD_OVERLOAD;		/* mark overloaded */

	/* if key already overloaded, use that slot else find free slot */

	if (key2ascii[data->keynum].type & KBD_OVERLOAD)
		i = key2ascii[data->keynum].ovlindex;
	else {
		for (i = 0; i < OVLTBL_SIZE; i++)
			if (ovltbl[i].keynum==0)
				break;

		if (i == OVLTBL_SIZE)
			return ENOSPC;	/* no space, abuse of ENOSPC(!) */
	}

	ovltbl[i] = *data;		/* copy new data string */

	key2ascii[data->keynum].type |= KBD_OVERLOAD; 	/* mark key */
	key2ascii[data->keynum].ovlindex = i;

	return 0;
}

/*---------------------------------------------------------------------------*
 *	keyboard ioctl's entry
 *---------------------------------------------------------------------------*/
int
kbdioctl(Dev_t dev, u_long cmd, caddr_t data, int flag)
{
	int key;

	switch (cmd) {
	case KBDRESET:
		doreset();
		ovlinit(1);
		settpmrate(KBD_TPD500 | KBD_TPM100);
		setlockkeys(0);
		break;

	case KBDGTPMAT:
		*(int *)data = tpmrate;
		break;

	case KBDSTPMAT:
		settpmrate(*(int *)data);
		break;

	case KBDGREPSW:
		*(int *)data = kbrepflag;
		break;

	case KBDSREPSW:
		kbrepflag = (*(int *)data) & 1;
		break;

	case KBDGLOCK:
		*(int *)data = ( (vsp->scroll_lock) | (vsp->num_lock * 2) |
				 (vsp->caps_lock * 4));
		break;

	case KBDSLOCK:
		setlockkeys(*(int *)data);
		break;

	case KBDGCKEY:
		key = ((Ovl_tbl *)data)->keynum;
		return getckeydef(key,(Ovl_tbl *)data);

	case KBDSCKEY:
		key = ((Ovl_tbl *)data)->keynum;
		return setkeydef((Ovl_tbl *)data);

	case KBDGOKEY:
		key = ((Ovl_tbl *)data)->keynum;
		return getokeydef(key,(Ovl_tbl *)data);

	case KBDRMKEY:
		key = *(int *)data;
		return rmkeydef(key);

	case KBDDEFAULT:
		ovlinit(1);
		break;

	default:
		/* proceed with vga ioctls */
		return -1;
	}
	return 0;
}

/*---------------------------------------------------------------------------*
 *	convert ISO-8859 style keycode into IBM 437
 *---------------------------------------------------------------------------*/
static inline u_char
iso2ibm(u_char c)
{
	if (c < 0x80)
		return c;
	return iso2ibm437[c - 0x80];
}

/*---------------------------------------------------------------------------*
 *	build up a USL style keyboard map
 *---------------------------------------------------------------------------*/
void
get_usl_keymap(keymap_t *map)
{
	int i;

	bzero((caddr_t)map, sizeof(keymap_t));

	map->n_keys = 0x59;	/* that many keys we know about */

	for(i = 1; i < N_KEYNUMS; i++) {
		Ovl_tbl kdef;
		u_char c;
		int j;
		int idx = key2scan1[i];

		if (idx == 0 || idx >= map->n_keys)
			continue;

		getckeydef(i, &kdef);
		kdef.type &= KBD_MASK;

		switch (kdef.type) {
		case KBD_ASCII:
		case KBD_RETURN:
			map->key[idx].map[0] = iso2ibm(kdef.unshift[0]);
			map->key[idx].map[1] = iso2ibm(kdef.shift[0]);
			map->key[idx].map[2] = map->key[idx].map[3] =
				iso2ibm(kdef.ctrl[0]);
			map->key[idx].map[4] = map->key[idx].map[5] =
				iso2ibm(c = kdef.altgr[0]);
			/*
			 * XXX this is a hack
			 * since we currently do not map strings to AltGr +
			 * shift, we attempt to use the unshifted AltGr
			 * definition here and try to toggle the case
			 * this should at least work for ISO8859 letters,
			 * but also for (e.g.) russian KOI-8 style
			 */
			if ((c & 0x7f) >= 0x40)
				map->key[idx].map[5] = iso2ibm(c ^ 0x20);
			break;

		case KBD_FUNC:
			/* we are only interested in F1 thru F12 here */
			if (i >= 112 && i <= 123) {
				map->key[idx].map[0] = i - 112 + 27;
				map->key[idx].spcl = 0x80;
			}
			break;

		case KBD_SHIFT:
			c = i == 44 ? 2 /* lSh */ : 3 /* rSh */;
			goto special;

		case KBD_CAPS:
			c = 4;
			goto special;

		case KBD_NUM:
			c = 5;
			goto special;

		case KBD_SCROLL:
			c = 6;
			goto special;

		case KBD_META:
			c = 7;
			goto special;

		case KBD_CTL:
			c = 9;
			goto special;
		special:
			for(j = 0; j < NUM_STATES; j++)
				map->key[idx].map[j] = c;
			map->key[idx].spcl = 0xff;
			break;

		default:
			break;
		}
	}
}

/*---------------------------------------------------------------------------*
 *	switch keypad to numeric mode
 *---------------------------------------------------------------------------*/
void
vt_keynum(struct video_state *svsp)
{
	svsp->num_lock = 1;
	update_led();
}

/*---------------------------------------------------------------------------*
 *	switch keypad to application mode
 *---------------------------------------------------------------------------*/
void
vt_keyappl(struct video_state *svsp)
{
	svsp->num_lock = 0;
	update_led();
}

#if !PCVT_VT220KEYB	/* !PCVT_VT220KEYB, HP-like Keyboard layout */

/*---------------------------------------------------------------------------*
 *	function bound to function key 1
 *---------------------------------------------------------------------------*/
static void
fkey1(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[17~";	/* F6 */
	else
		more_chars = (u_char *)"\033[26~";	/* F14 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 2
 *---------------------------------------------------------------------------*/
static void
fkey2(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[18~";	/* F7 */
	else
		more_chars = (u_char *)"\033[28~";	/* HELP */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 3
 *---------------------------------------------------------------------------*/
static void
fkey3(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[19~";	/* F8 */
	else
		more_chars = (u_char *)"\033[29~";	/* DO */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 4
 *---------------------------------------------------------------------------*/
static void
fkey4(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[20~";	/* F9 */
	else
		more_chars = (u_char *)"\033[31~";	/* F17 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 5
 *---------------------------------------------------------------------------*/
static void
fkey5(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[21~";	/* F10 */
	else
		more_chars = (u_char *)"\033[32~";	/* F18 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 6
 *---------------------------------------------------------------------------*/
static void
fkey6(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[23~";	/* F11 */
	else
		more_chars = (u_char *)"\033[33~";	/* F19 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 7
 *---------------------------------------------------------------------------*/
static void
fkey7(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[24~";	/* F12 */
	else
		more_chars = (u_char *)"\033[34~";	/* F20 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 8
 *---------------------------------------------------------------------------*/
static void
fkey8(void)
{
	if (!meta_down)
		more_chars = (u_char *)"\033[25~";	/* F13 */
	else
		more_chars = (u_char *)"\033[35~";	/* F21 ??!! */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 9
 *---------------------------------------------------------------------------*/
static void
fkey9(void)
{
	if (meta_down)
		return;
	else
		do_vgapage(0);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 10
 *---------------------------------------------------------------------------*/
static void
fkey10(void)
{
	if (!meta_down)
		do_vgapage(1);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 11
 *---------------------------------------------------------------------------*/
static void
fkey11(void)
{
	do_vgapage(2);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 12
 *---------------------------------------------------------------------------*/
static void
fkey12(void)
{
	if (meta_down) {
		if (current_video_screen + 1 > totalscreens-1)
			do_vgapage(0);
		else
			do_vgapage(current_video_screen + 1);
	}
	else {
		do_vgapage(3);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 1
 *---------------------------------------------------------------------------*/
static void
sfkey1(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[0])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[0]]);
	}
	else {
		if (vsp->ukt.length[9])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[9]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 2
 *---------------------------------------------------------------------------*/
static void
sfkey2(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[1])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[1]]);
	}
	else {
		if (vsp->ukt.length[11])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[11]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 3
 *---------------------------------------------------------------------------*/
static void
sfkey3(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[2])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[2]]);
	}
	else {
		if (vsp->ukt.length[12])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[12]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 4
 *---------------------------------------------------------------------------*/
static void
sfkey4(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[3])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[3]]);
	}
	else {
		if (vsp->ukt.length[13])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[13]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 5
 *---------------------------------------------------------------------------*/
static void
sfkey5(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[4])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[4]]);
	}
	else {
		if (vsp->ukt.length[14])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[14]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 6
 *---------------------------------------------------------------------------*/
static void
sfkey6(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[6])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[6]]);
	}
	else {
		if (vsp->ukt.length[15])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[15]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 7
 *---------------------------------------------------------------------------*/
static void
sfkey7(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[7])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[7]]);
	}
	else {
		if (vsp->ukt.length[16])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[16]]);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 8
 *---------------------------------------------------------------------------*/
static void
sfkey8(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[8])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[8]]);
	}
	else {
		if (vsp->ukt.length[17])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[17]]);
	}
}
/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 9
 *---------------------------------------------------------------------------*/
static void
sfkey9(void)
{
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 10
 *---------------------------------------------------------------------------*/
static void
sfkey10(void)
{
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 11
 *---------------------------------------------------------------------------*/
static void
sfkey11(void)
{
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 12
 *---------------------------------------------------------------------------*/
static void
sfkey12(void)
{
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 1
 *---------------------------------------------------------------------------*/
static void
cfkey1(void)
{
	if (meta_down)
		do_vgapage(0);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 2
 *---------------------------------------------------------------------------*/
static void
cfkey2(void)
{
	if (meta_down)
		do_vgapage(1);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 3
 *---------------------------------------------------------------------------*/
static void
cfkey3(void)
{
	if (meta_down)
		do_vgapage(2);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 4
 *---------------------------------------------------------------------------*/
static void
cfkey4(void)
{
	if (meta_down)
		do_vgapage(3);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 5
 *---------------------------------------------------------------------------*/
static void
cfkey5(void)
{
	if (meta_down)
		do_vgapage(4);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 6
 *---------------------------------------------------------------------------*/
static void
cfkey6(void)
{
	if (meta_down)
		do_vgapage(5);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 7
 *---------------------------------------------------------------------------*/
static void
cfkey7(void)
{
	if (meta_down)
		do_vgapage(6);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 8
 *---------------------------------------------------------------------------*/
static void
cfkey8(void)
{
	if (meta_down)
		do_vgapage(7);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 9
 *---------------------------------------------------------------------------*/
static void
cfkey9(void)
{
	if (meta_down)
		do_vgapage(8);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 10
 *---------------------------------------------------------------------------*/
static void
cfkey10(void)
{
	if (meta_down)
		do_vgapage(9);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 11
 *---------------------------------------------------------------------------*/
static void
cfkey11(void)
{
	if (meta_down)
		do_vgapage(10);
}

/*---------------------------------------------------------------------------*
 *	function bound to control function key 12
 *---------------------------------------------------------------------------*/
static void
cfkey12(void)
{
	if (meta_down)
		do_vgapage(11);
}

#else	/* PCVT_VT220  -  VT220-like Keyboard layout */

/*---------------------------------------------------------------------------*
 *	function bound to function key 1
 *---------------------------------------------------------------------------*/
static void
fkey1(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[23~"; /* F11 */
	else
		do_vgapage(0);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 2
 *---------------------------------------------------------------------------*/
static void
fkey2(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[24~"; /* F12 */
	else
		do_vgapage(1);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 3
 *---------------------------------------------------------------------------*/
static void
fkey3(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[25~"; /* F13 */
	else
		do_vgapage(2);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 4
 *---------------------------------------------------------------------------*/
static void
fkey4(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[26~"; /* F14 */
	else
		do_vgapage(3);
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 5
 *---------------------------------------------------------------------------*/
static void
fkey5(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[28~"; /* Help */
	else {
		if ((current_video_screen + 1) > totalscreens-1)
			do_vgapage(0);
		else
			do_vgapage(current_video_screen + 1);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 6
 *---------------------------------------------------------------------------*/
static void
fkey6(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[29~"; /* DO */
	else
		more_chars = (u_char *)"\033[17~"; /* F6 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 7
 *---------------------------------------------------------------------------*/
static void
fkey7(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[31~"; /* F17 */
	else
		more_chars = (u_char *)"\033[18~"; /* F7 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 8
 *---------------------------------------------------------------------------*/
static void
fkey8(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[32~"; /* F18 */
	else
		more_chars = (u_char *)"\033[19~"; /* F8 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 9
 *---------------------------------------------------------------------------*/
static void
fkey9(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[33~"; /* F19 */
	else
		more_chars = (u_char *)"\033[20~"; /* F9 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 10
 *---------------------------------------------------------------------------*/
static void
fkey10(void)
{
	if (meta_down)
		more_chars = (u_char *)"\033[34~"; /* F20 */
	else
		more_chars = (u_char *)"\033[21~"; /* F10 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 11
 *---------------------------------------------------------------------------*/
static void
fkey11(void)
{
	if (meta_down)
		more_chars = (u_char *)"\0x8FP"; /* PF1 */
	else
		more_chars = (u_char *)"\033[23~"; /* F11 */
}

/*---------------------------------------------------------------------------*
 *	function bound to function key 12
 *---------------------------------------------------------------------------*/
static void
fkey12(void)
{
	if (meta_down)
		more_chars = (u_char *)"\0x8FQ"; /* PF2 */
	else
		more_chars = (u_char *)"\033[24~"; /* F12 */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 1
 *---------------------------------------------------------------------------*/
static void
sfkey1(void)
{
	if (meta_down) {
		if (vsp->ukt.length[6])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[6]]);
		else
			more_chars = (u_char *)"\033[23~"; /* F11 */
	}
	else
		do_vgapage(4);
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 2
 *---------------------------------------------------------------------------*/
static void
sfkey2(void)
{
	if (meta_down) {
		if (vsp->ukt.length[7])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[7]]);
		else
			more_chars = (u_char *)"\033[24~"; /* F12 */
	}
	else
		do_vgapage(5);
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 3
 *---------------------------------------------------------------------------*/
static void
sfkey3(void)
{
	if (meta_down) {
		if (vsp->ukt.length[8])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[8]]);
		else
			more_chars = (u_char *)"\033[25~"; /* F13 */
	}
	else
		do_vgapage(6);
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 4
 *---------------------------------------------------------------------------*/
static void
sfkey4(void)
{
	if (meta_down) {
		if (vsp->ukt.length[9])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[9]]);
		else
			more_chars = (u_char *)"\033[26~"; /* F14 */
	}
	else
		do_vgapage(7);
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 5
 *---------------------------------------------------------------------------*/
static void
sfkey5(void)
{
	if (meta_down) {
		if (vsp->ukt.length[11])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[11]]);
		else
			more_chars = (u_char *)"\033[28~"; /* Help */
	}
	else {
		if (current_video_screen <= 0)
			do_vgapage(totalscreens-1);
		else
			do_vgapage(current_video_screen - 1);
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 6
 *---------------------------------------------------------------------------*/
static void
sfkey6(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[0])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[0]]);
		else
			more_chars = (u_char *)"\033[17~"; /* F6 */
	}
	else if (vsp->ukt.length[12])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[12]]);
	     else
			more_chars = (u_char *)"\033[29~"; /* DO */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 7
 *---------------------------------------------------------------------------*/
static void
sfkey7(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[1])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[1]]);
		else
			more_chars = (u_char *)"\033[18~"; /* F7 */
	}
	else if (vsp->ukt.length[14])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[14]]);
	     else
			more_chars = (u_char *)"\033[31~"; /* F17 */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 8
 *---------------------------------------------------------------------------*/
static void
sfkey8(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[2])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[2]]);
		else
			more_chars = (u_char *)"\033[19~"; /* F8 */
	}
	else if (vsp->ukt.length[14])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[15]]);
	     else
			more_chars = (u_char *)"\033[32~"; /* F18 */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 9
 *---------------------------------------------------------------------------*/
static void
sfkey9(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[3])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[3]]);
		else
			more_chars = (u_char *)"\033[20~"; /* F9 */
	}
	else if (vsp->ukt.length[16])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[16]]);
	     else
			more_chars = (u_char *)"\033[33~"; /* F19 */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 10
 *---------------------------------------------------------------------------*/
static void
sfkey10(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[4])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[4]]);
		else
			more_chars = (u_char *)"\033[21~"; /* F10 */
	}
	else if (vsp->ukt.length[17])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[17]]);
	     else
			more_chars = (u_char *)"\033[34~"; /* F20 */
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 11
 *---------------------------------------------------------------------------*/
static void
sfkey11(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[6])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[6]]);
		else
			more_chars = (u_char *)"\033[23~"; /* F11 */
	}
}

/*---------------------------------------------------------------------------*
 *	function bound to SHIFTED function key 12
 *---------------------------------------------------------------------------*/
static void
sfkey12(void)
{
	if (!meta_down) {
		if (vsp->ukt.length[7])	/* entry available ? */
			more_chars = (u_char *)
				&(vsp->udkbuf[vsp->ukt.first[7]]);
		else
			more_chars = (u_char *)"\033[24~"; /* F12 */
	}
}

#endif	/* NVT > 0 */

static void
scrollback_save_screen(void)
{
	int x = spltty();
	register size_t s;

	s = sizeof(u_short) * vsp->screen_rowsize * vsp->maxcol;

	if (scrollback_savedscreen)
		free(scrollback_savedscreen, M_TEMP);

	scrnsv_size = s;

	if (!(scrollback_savedscreen = (u_short *)malloc(s, M_TEMP, M_NOWAIT))){
		splx(x);
		return;
	}
	bcopy(vsp->Crtat, scrollback_savedscreen, scrnsv_size);
	splx(x);
}

static void
scrollback_restore_screen(void)
{
	if (scrollback_savedscreen)
		bcopy(scrollback_savedscreen, vsp->Crtat, scrnsv_size);
}

/* ------------------------------- EOF -------------------------------------*/