DragonFlyBSD Kernel Audit
sys/dev/misc/syscons/syscons.c
← back
   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
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
/*-
 * Copyright (c) 1992-1998 Søren Schmidt
 * All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Sascha Wildner <saw@online.de>
 *
 * Simple font scaling code by Sascha Wildner and Matthew Dillon
 *
 * 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,
 *    without modification, immediately at the beginning of the file.
 * 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. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 *
 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/dev/syscons/syscons.c,v 1.336.2.17 2004/03/25 08:41:09 ru Exp $
 */

#include "use_splash.h"
#include "opt_syscons.h"
#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/eventhandler.h>
#include <sys/reboot.h>
#include <sys/caps.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <sys/tty.h>
#include <sys/ttydefaults.h>	/* for TTYDEF_* */
#include <sys/kernel.h>
#include <sys/cons.h>
#include <sys/random.h>
#include <ddb/ddb.h>

#include <sys/thread2.h>
#include <sys/mutex2.h>

#include <machine/clock.h>
#include <machine/console.h>
#include <machine/psl.h>
#include <machine/pc/display.h>
#include <machine/frame.h>
#include <machine/framebuffer.h>

#include <vm/pmap.h>

#include <dev/misc/kbd/kbdreg.h>
#include <dev/video/fb/fbreg.h>
#include <dev/video/fb/splashreg.h>
#include "syscons.h"

#define COLD 0
#define WARM 1

#define DEFAULT_BLANKTIME	(5*60)		/* 5 minutes */
#define MAX_BLANKTIME		(7*24*60*60)	/* 7 days!? */

#define SCRN_ASYNCOK		0x0001
#define SCRN_BULKUNLOCK		0x0002

#define KEYCODE_BS		0x0e		/* "<-- Backspace" key, XXX */
#define WANT_UNLOCK(m) do {	  \
	if (m)			  \
		syscons_unlock(); \
} while (0)

#define WANT_LOCK(m) do { 	  \
	if (m)			  \
		syscons_lock();	  \
} while(0)


MALLOC_DEFINE(M_SYSCONS, "syscons", "Syscons");

typedef struct default_attr {
	int		std_color;		/* normal hardware color */
	int		rev_color;		/* reverse hardware color */
} default_attr;

static default_attr user_default = {
    SC_NORM_ATTR,
    SC_NORM_REV_ATTR,
};

static default_attr kernel_default = {
    SC_KERNEL_CONS_ATTR,
    SC_KERNEL_CONS_REV_ATTR,
};

static	int		sc_console_unit = -1;
static  scr_stat    	*sc_console;
static	struct tty	*sc_console_tty;
static	void		*kernel_console_ts;

static  char        	init_done = COLD;
static  char		shutdown_in_progress = FALSE;
static	char		sc_malloc = FALSE;
static	char		sc_filled_border = FALSE; /* filled initial VT border */

static	int		saver_mode = CONS_NO_SAVER; /* LKM/user saver */
static	int		run_scrn_saver = FALSE;	/* should run the saver? */
static	long        	scrn_blank_time = 0;    /* screen saver timeout value */
#if NSPLASH > 0
static	int     	scrn_blanked;		/* # of blanked screen */
static	int		sticky_splash = FALSE;

static	void		none_saver(sc_softc_t *sc, int blank) { }
static	void		(*current_saver)(sc_softc_t *, int) = none_saver;
#endif

/*
 * Lock for asynchronous screen update thread, needed to safely modify the
 * framebuffer information.
 */
static struct lock	sc_asynctd_lk;

#if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
#include "font.h"
#endif

static	bios_values_t	bios_value;

static	int		enable_panic_key;
SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
	   0, "Enable the panic key (CTRL-ALT-SHIFT-ESC)");

static	int		syscons_async;
static	int		enable_bell = TRUE;

static SYSCTL_NODE(_kern, OID_AUTO, syscons, CTLFLAG_RD, 0, "syscons");
SYSCTL_INT(_kern, OID_AUTO, syscons_async, CTLFLAG_RW, &syscons_async,
	   0, "Asynchronous bulk syscons fb updates");
SYSCTL_INT(_kern_syscons, OID_AUTO, enable_bell, CTLFLAG_RW, &enable_bell,
	   0, "Enable bell");

static int desired_cols = 0;
TUNABLE_INT("kern.kms_columns", &desired_cols);

#define SC_CONSOLECTL	255

#define VIRTUAL_TTY(sc, x) ((SC_DEV((sc),(x)) != NULL) ?	\
	(SC_DEV((sc),(x))->si_tty) : NULL)
#define ISTTYOPEN(tp)	((tp) && ((tp)->t_state & TS_ISOPEN))

static	int	debugger;
static	cdev_t	cctl_dev;
static	timeout_t blink_screen_callout;
static  void	sc_blink_screen(scr_stat *scp);
static	struct mtx	syscons_mtx = MTX_INITIALIZER("syscons");
static	struct lock	syscons_blink_lk = LOCK_INITIALIZER("sblnk", 0, 0);

/* prototypes */
static int scvidprobe(int unit, int flags, int cons);
static int sckbdprobe(int unit, int flags, int cons);
static void scmeminit(void *arg);
static int scdevtounit(cdev_t dev);
static kbd_callback_func_t sckbdevent;
static int scparam(struct tty *tp, struct termios *t);
static void scstart(struct tty *tp);
static void scinit(int unit, int flags);
#if 0
static void scterm(int unit, int flags);
#endif
static void scshutdown(void *arg, int howto);
static void sc_puts(scr_stat *scp, u_char *buf, int len);
static u_int scgetc(sc_softc_t *sc, u_int flags);
#define SCGETC_CN	1
#define SCGETC_NONBLOCK	2
static int sccngetch(int flags);
static void sccnupdate(scr_stat *scp);
static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
static timeout_t scrn_timer;
static int and_region(int *s1, int *e1, int s2, int e2);
static void scrn_update(scr_stat *scp, int show_cursor, int flags);
static void scrn_update_thread(void *arg);

static void sc_fb_set_par(void *context, int pending);
#if NSPLASH > 0
static void sc_fb_blank(void *context, int pending);
#endif /* NSPLASH */

#if NSPLASH > 0
static void scframebuffer_saver(sc_softc_t *sc, int show);
static int scsplash_callback(int event, void *arg);
static void scsplash_saver(sc_softc_t *sc, int show);
static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
static int wait_scrn_saver_stop(sc_softc_t *sc, int need_unlock);
#define scsplash_stick(stick)		(sticky_splash = (stick))
#else /* !NSPLASH */
#define scsplash_stick(stick)
#endif /* NSPLASH */

static void do_switch_scr(sc_softc_t *sc);
static int vt_proc_alive(scr_stat *scp);
static int signal_vt_rel(scr_stat *scp);
static int signal_vt_acq(scr_stat *scp);
static int finish_vt_rel(scr_stat *scp, int release);
static int finish_vt_acq(scr_stat *scp);
static void exchange_scr(sc_softc_t *sc);
static void update_cursor_image(scr_stat *scp);
static int save_kbd_state(scr_stat *scp, int unlock);
static int update_kbd_state(scr_stat *scp, int state, int mask, int unlock);
static int update_kbd_leds(scr_stat *scp, int which);
static int sc_allocate_keyboard(sc_softc_t *sc, int unit);

/*
 * Console locking support functions.
 *
 * We use mutex spinlocks here in order to allow reentrancy which should
 * avoid issues during panics.
 *
 * A panic can stop a cpu while it is holding the syscons_mtx.  If this
 * occurs we try for up to 1/2 a second and then blow the spinlock away.
 * The asynchronous update thread makes this happen more often.
 */
static void
syscons_lock(void)
{
	if (panicstr || shutdown_in_progress) {
		int retries = 500000;
		while (mtx_spinlock_try(&syscons_mtx)) {
			tsc_delay(1000);	/* 1uS */
			if (--retries == 0) {
				mtx_init(&syscons_mtx, "syscons");
				retries = 500000;
			}
		}
	} else {
		mtx_spinlock(&syscons_mtx);
	}
}

/*
 * Returns 0 on success, EAGAIN on failure.
 */
static int
syscons_lock_nonblock(void)
{
	return(mtx_spinlock_try(&syscons_mtx));
}

static void
syscons_unlock(void)
{
	mtx_spinunlock(&syscons_mtx);
}

/*
 * Console driver
 */
static cn_probe_t	sccnprobe;
static cn_init_t	sccninit;
static cn_init_t	sccninit_fini;
static cn_getc_t	sccngetc;
static cn_checkc_t	sccncheckc;
static cn_putc_t	sccnputc;
static cn_dbctl_t	sccndbctl;
static cn_term_t	sccnterm;
static cn_poll_t	sccnpoll;

CONS_DRIVER(sc, sccnprobe, sccninit, sccninit_fini, sccnterm,
	    sccngetc, sccncheckc, sccnputc, sccndbctl, sccnpoll);

static	d_open_t	scopen;
static	d_close_t	scclose;
static	d_read_t	scread;
static	d_ioctl_t	scioctl;
static	d_mmap_t	scmmap;

static struct dev_ops sc_ops = {
	{ "sc", 0, D_TTY | D_MPSAFE },
	.d_open =	scopen,
	.d_close =	scclose,
	.d_read =	scread,
	.d_write =	ttywrite,
	.d_ioctl =	scioctl,
	.d_mmap =	scmmap,
	.d_kqfilter =	ttykqfilter,
	.d_revoke =	ttyrevoke
};

int
sc_probe_unit(int unit, int flags)
{
    if ((flags & SC_EFI_FB) && (probe_efi_fb(0) != 0)) {
	if (bootverbose)
	    kprintf("sc%d: no EFI framebuffer found.\n", unit);
	flags &= ~SC_EFI_FB;
    }

    if (!(flags & SC_EFI_FB) && !scvidprobe(unit, flags, FALSE)) {
	if (bootverbose)
	    kprintf("sc%d: no video adapter found.\n", unit);
	return ENXIO;
    }

    /* syscons will be attached even when there is no keyboard */
    sckbdprobe(unit, flags, FALSE);

    return 0;
}

int
register_framebuffer(struct fb_info *info)
{
	sc_softc_t *sc;

	KKASSERT(info->depth <= 32);

	lwkt_gettoken(&vga_token);
	sc = sc_get_softc(0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
	if (sc == NULL) {
		lwkt_reltoken(&vga_token);
		kprintf("%s: sc_get_softc(%d, %d) returned NULL\n", __func__,
		    0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
		return 0;
	}

	/* Ignore this framebuffer if we already switched to KMS framebuffer */
	if (sc->fbi != NULL && sc->fbi != &efi_fb_info &&
	    sc->fbi != sc->dummy_fb_info) {
		lwkt_reltoken(&vga_token);
		return 0;
	}

	if (sc->fb_set_par_task == NULL) {
		sc->fb_set_par_task = kmalloc(sizeof(struct task),
		    M_SYSCONS, M_WAITOK | M_ZERO);
	}
	TASK_INIT(sc->fb_set_par_task, 0, sc_fb_set_par, sc);
#if NSPLASH > 0
	if (sc->fb_blank_task == NULL) {
		sc->fb_blank_task = kmalloc(sizeof(struct task),
		    M_SYSCONS, M_WAITOK | M_ZERO);
	}
	TASK_INIT(sc->fb_blank_task, 0, sc_fb_blank, sc);
#endif /* NSPLASH */

	/*
	 * Make sure that console messages don't interfere too much with
	 * modesetting.
	 */
	atomic_add_int(&sc->videoio_in_progress, 1);

	/* Lock against synchronous and asynchronous screen updates */
	lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
	syscons_lock();
	sc->fbi = info;
	sc->fbi_generation++;
	syscons_unlock();

	kprintf("kms console: xpixels %d ypixels %d depth %d\n",
	    sc->fbi->width, sc->fbi->height, sc->fbi->depth);
	sc_update_render(sc->cur_scp);
	if (info->fbops.fb_set_par != NULL)
		info->fbops.fb_set_par(info);
	atomic_add_int(&sc->videoio_in_progress, -1);

	sc->fb_blanked = FALSE;
	if (sc->unit == sc_console_unit && sc->cur_scp->smode.mode != VT_AUTO)
		cons_unavail = FALSE;
#if NSPLASH > 0
	if (info->fbops.fb_blank != NULL)
		add_scrn_saver(scframebuffer_saver);
#endif /* NSPLASH */

	lockmgr(&sc_asynctd_lk, LK_RELEASE);
	lwkt_reltoken(&vga_token);
	return 0;
}

void
unregister_framebuffer(struct fb_info *info)
{
	sc_softc_t *sc;

	lwkt_gettoken(&vga_token);
	sc = sc_get_softc(0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
	if (sc == NULL) {
		lwkt_reltoken(&vga_token);
		kprintf("%s: sc_get_softc(%d, %d) returned NULL\n", __func__,
		    0, (sc_console_unit == 0) ? SC_KERNEL_CONSOLE : 0);
		return;
	}

	if (sc->fbi != info) {
		lwkt_reltoken(&vga_token);
		return;
	}

#if NSPLASH > 0
	if (info->fbops.fb_blank != NULL)
		remove_scrn_saver(scframebuffer_saver);
#endif /* NSPLASH */

	if (sc->fb_set_par_task != NULL &&
	    taskqueue_cancel(taskqueue_thread[0], sc->fb_set_par_task, NULL)) {
		taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
	}
#if NSPLASH > 0
	if (sc->fb_blank_task != NULL &&
	    taskqueue_cancel(taskqueue_thread[0], sc->fb_blank_task, NULL)) {
		taskqueue_drain(taskqueue_thread[0], sc->fb_blank_task);
	}
#endif /* NSPLASH */
	sc->fb_blanked = TRUE;
	if (sc->unit == sc_console_unit)
		cons_unavail = TRUE;

	if (sc->dummy_fb_info == NULL) {
		sc->dummy_fb_info = kmalloc(sizeof(struct fb_info),
		    M_SYSCONS, M_WAITOK | M_ZERO);
	} else {
		memset(sc->dummy_fb_info, 0, sizeof(struct fb_info));
	}
	*sc->dummy_fb_info = *sc->fbi;
	sc->dummy_fb_info->vaddr = 0;
	sc->dummy_fb_info->fbops.fb_set_par = NULL;
	sc->dummy_fb_info->fbops.fb_blank = NULL;
	sc->dummy_fb_info->fbops.fb_debug_enter = NULL;

	/* Lock against synchronous and asynchronous screen updates */
	lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
	syscons_lock();
	sc->fbi = sc->dummy_fb_info;
	syscons_unlock();
	lockmgr(&sc_asynctd_lk, LK_RELEASE);

	lwkt_reltoken(&vga_token);
}

void
sc_font_scale(scr_stat *scp, int max_cols, int max_rows)
{
	int cols, rows;

	/*
	 * If columns not specified in /boot/loader.conf then
	 * calculate a non-fractional scaling that yields a
	 * reasonable number of rows and columns. If it is <0,
	 * don't scale at all.
	 */
	if (desired_cols == 0) {
		int nomag = 1;
		while (scp->xpixel / (scp->font_width * nomag) >= COL &&
		       scp->ypixel / (scp->font_height * nomag) >= ROW) {
			++nomag;
		}
		if (nomag > 1)
			--nomag;
		cols = scp->xpixel / (scp->font_width * nomag);
	} else if (desired_cols < 0) {
		cols = scp->xpixel / scp->font_width;
	} else {
		cols = desired_cols;
	}
	scp->blk_width = scp->xpixel / cols;
	scp->blk_height = scp->blk_width * scp->font_height / scp->font_width;
	rows = scp->ypixel / scp->blk_height;

	/* scp->xsize = scp->xpixel / scp->blk_width; total possible */
	scp->xsize = max_cols ? imin(max_cols, cols) : cols;
	scp->ysize = max_rows ? imin(max_rows, rows) : rows;
}

/* probe video adapters, return TRUE if found */ 
static int
scvidprobe(int unit, int flags, int cons)
{
    /*
     * Access the video adapter driver through the back door!
     * Video adapter drivers need to be configured before syscons.
     * However, when syscons is being probed as the low-level console,
     * they have not been initialized yet.  We force them to initialize
     * themselves here. XXX
     */
    vid_configure(cons ? VIO_PROBE_ONLY : 0);

    return (vid_find_adapter("*", unit) >= 0);
}

/* probe the keyboard, return TRUE if found */
static int
sckbdprobe(int unit, int flags, int cons)
{
    /* access the keyboard driver through the backdoor! */
    kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);

    return (kbd_find_keyboard("*", unit) >= 0);
}

static char *
adapter_name(video_adapter_t *adp)
{
    static struct {
	int type;
	char *name[2];
    } names[] = {
	{ KD_MONO,	{ "MDA",	"MDA" } },
	{ KD_HERCULES,	{ "Hercules",	"Hercules" } },
	{ KD_CGA,	{ "CGA",	"CGA" } },
	{ KD_EGA,	{ "EGA",	"EGA (mono)" } },
	{ KD_VGA,	{ "VGA",	"VGA (mono)" } },
	{ KD_TGA,	{ "TGA",	"TGA" } },
	{ -1,		{ "Unknown",	"Unknown" } },
    };
    int i;

    for (i = 0; names[i].type != -1; ++i)
	if (names[i].type == adp->va_type)
	    break;
    return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
}

int
sc_attach_unit(int unit, int flags)
{
    sc_softc_t *sc;
    scr_stat *scp;
    int vc;
    cdev_t dev;
    flags &= ~SC_KERNEL_CONSOLE;
    char tbuf[MAKEDEV_MINNBUF];

    if ((flags & SC_EFI_FB) && probe_efi_fb(0) != 0)
	flags &= ~SC_EFI_FB;

    if (sc_console_unit == unit) {
	/*
	 * If this unit is being used as the system console, we need to
	 * adjust some variables and buffers before and after scinit().
	 */
	/* assert(sc_console != NULL) */
	flags |= SC_KERNEL_CONSOLE;
	scmeminit(NULL);

	scinit(unit, flags);

	if (sc_console->tsw->te_size > 0) {
	    /* assert(sc_console->ts != NULL); */
	    kernel_console_ts = sc_console->ts;
	    sc_console->ts = kmalloc(sc_console->tsw->te_size,
				    M_SYSCONS, M_WAITOK);
	    bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
    	    (*sc_console->tsw->te_default_attr)(sc_console,
						user_default.std_color,
						user_default.rev_color);
	}
    } else {
	scinit(unit, flags);
    }

    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);

    /*
     * If this is the console we couldn't setup sc->dev before because
     * malloc wasn't working.  Set it up now.
     */
    if (flags & SC_KERNEL_CONSOLE) {
	KKASSERT(sc->dev == NULL);
	sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK|M_ZERO);
	sc->dev[0] = make_dev(&sc_ops, sc_console_unit*MAXCONS, UID_ROOT,
			      GID_WHEEL, 0600, "ttyv%s", makedev_unit_b32(tbuf,
			      sc_console_unit * MAXCONS));
	ttymalloc(&sc->dev[0]->si_tty);
	sc->dev[0]->si_drv1 = sc_console;
    }

    /*
     * Finish up the standard attach
     */
    sc->config = flags;
    callout_init_mp(&sc->scrn_timer_ch);
    scp = SC_STAT(sc->dev[0]);
    if (sc_console == NULL)	/* sc_console_unit < 0 */
	sc_console = scp;

    /* initialize cursor */
    if (!ISGRAPHSC(scp))
    	update_cursor_image(scp);

    /* get screen update going */
    scrn_timer(sc);

    /* set up the keyboard */
    kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
    update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);

    kprintf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
	    unit, (sc->config & SC_EFI_FB) ? "EFI_FB" : adapter_name(sc->adp),
	    sc->vtys, sc->config);
    if (bootverbose) {
	kprintf("sc%d:", unit);
    	if (sc->adapter >= 0)
	    kprintf(" fb%d", sc->adapter);
	if (sc->keyboard >= 0)
	    kprintf(", kbd%d", sc->keyboard);
	if (scp->tsw)
	    kprintf(", terminal emulator: %s (%s)",
		   scp->tsw->te_name, scp->tsw->te_desc);
	kprintf("\n");
    }

    /* register a shutdown callback for the kernel console */
    if (sc_console_unit == unit)
	EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, 
			      (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);

    /* 
     * create devices.
     *
     * The first vty already has struct tty and scr_stat initialized
     * in scinit().  The other vtys will have these structs when
     * first opened.
     */
    for (vc = 1; vc < sc->vtys; vc++) {
	dev = make_dev(&sc_ops, vc + unit * MAXCONS,
			UID_ROOT, GID_WHEEL, 0600, "ttyv%s",
			makedev_unit_b32(tbuf, vc + unit * MAXCONS));
	sc->dev[vc] = dev;
    }
    cctl_dev = make_dev(&sc_ops, SC_CONSOLECTL,
			UID_ROOT, GID_WHEEL, 0600, "consolectl");
    cctl_dev->si_tty = ttymalloc(&sc_console_tty);
    cctl_dev->si_drv1 = sc_console;
    return 0;
}

static void
scmeminit(void *arg)
{
    if (sc_malloc)
	return;
    sc_malloc = TRUE;

    /*
     * As soon as malloc() becomes functional, we had better allocate
     * various buffers for the kernel console.
     */

    if (sc_console_unit < 0)	/* sc_console == NULL */
	return;

    /* copy the temporary buffer to the final buffer */
    sc_alloc_scr_buffer(sc_console, TRUE, FALSE);

#ifndef SC_NO_CUTPASTE
    sc_alloc_cut_buffer(sc_console, TRUE);
#endif

#ifndef SC_NO_HISTORY
    /* initialize history buffer & pointers */
    sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
#endif

    /* Fill initial terminal border, efi_fb_info.vaddr should be non-NULL now */
    if (!sc_filled_border) {
	sc_filled_border = TRUE;
	sc_set_border(sc_console, sc_console->border);
    }
}

SYSINIT(sc_mem, SI_BOOT1_POST, SI_ORDER_ANY, scmeminit, NULL);

static int
scdevtounit(cdev_t dev)
{
    int vty = SC_VTY(dev);

    if (vty == SC_CONSOLECTL)
	return ((sc_console != NULL) ? sc_console->sc->unit : -1);
    else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
	return -1;
    else
	return vty/MAXCONS;
}

static int
scopen(struct dev_open_args *ap)
{
    cdev_t dev = ap->a_head.a_dev;
    int unit;
    sc_softc_t *sc;
    struct tty *tp;
    scr_stat *scp;
    keyarg_t key;
    int error;

    /*
     * Disallow root access to ttyv* console device if RESTRICTEDROOT
     * because root cred will match inside jails and its too dangerous
     * for that.
     *
     * However, we have to specify __SYSCAP_NOROOTTEST to allow non-root
     * accesses when using RESTRICTROOT (when creds match).
     */
    if (caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT | __SYSCAP_NOROOTTEST))
	return (EPERM);

    lwkt_gettoken(&vga_token);
    unit = scdevtounit(dev);
    DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
		major(dev), minor(dev), unit, SC_VTY(dev)));

    sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
    if (sc == NULL) {
	lwkt_reltoken(&vga_token);
	return ENXIO;
    }

    tp = ttymalloc(&dev->si_tty);
    lwkt_gettoken(&tp->t_token);
    tp->t_oproc = scstart;
    tp->t_param = scparam;
    tp->t_stop = nottystop;

    tp->t_dev = dev;

    if (!ISTTYOPEN(tp)) {
	ttychars(tp);
        /* Use the current setting of the <-- key as default VERASE. */  
        /* If the Delete key is preferable, an stty is necessary     */
	if (sc->kbd != NULL) {
	    key.keynum = KEYCODE_BS;
	    kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
            tp->t_cc[VERASE] = key.key.map[0];
	}
	tp->t_iflag = TTYDEF_IFLAG;
	tp->t_oflag = TTYDEF_OFLAG;
	tp->t_cflag = TTYDEF_CFLAG;
	tp->t_lflag = TTYDEF_LFLAG;
	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
	scparam(tp, &tp->t_termios);
	(*linesw[tp->t_line].l_modem)(tp, 1);
    }
    else
	if ((tp->t_state & TS_XCLUDE) &&
	    caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT))
	{
	    lwkt_reltoken(&tp->t_token);
	    lwkt_reltoken(&vga_token);
	    return(EBUSY);
	}

    error = (*linesw[tp->t_line].l_open)(dev, tp);

    scp = SC_STAT(dev);
    if (scp == NULL) {
	scp = dev->si_drv1 = alloc_scp(sc, SC_VTY(dev));
	syscons_lock();
	if (ISGRAPHSC(scp))
	    sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
	syscons_unlock();
    }
    if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
	tp->t_winsize.ws_col = scp->xsize;
	tp->t_winsize.ws_row = scp->ysize;
    }

    /*
     * Start optional support thread for syscons refreshes.  This thread
     * will execute the refresh without the syscons_lock held and will
     * allow interrupts while it is doing so.
     */
    if (scp->asynctd == NULL) {
	    lwkt_create(scrn_update_thread, scp, &scp->asynctd, NULL, 0, -1,
			"syscons%d", SC_VTY(dev));
    }
    lwkt_reltoken(&tp->t_token);
    lwkt_reltoken(&vga_token);

    return error;
}

static int
scclose(struct dev_close_args *ap)
{
    cdev_t dev = ap->a_head.a_dev;
    struct tty *tp = dev->si_tty;
    scr_stat *scp;

    lwkt_gettoken(&vga_token);
    lwkt_gettoken(&tp->t_token);
    if (SC_VTY(dev) != SC_CONSOLECTL) {
	scp = SC_STAT(tp->t_dev);
	/* were we in the middle of the VT switching process? */
	DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
	if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit) &&
	    !scp->sc->fb_blanked) {
	    cons_unavail = FALSE;
	}
	if (finish_vt_rel(scp, TRUE) == 0)	/* force release */
	    DPRINTF(5, ("reset WAIT_REL, "));
	if (finish_vt_acq(scp) == 0)		/* force acknowledge */
	    DPRINTF(5, ("reset WAIT_ACQ, "));
	syscons_lock();
#if 0 /* notyet */
	if (scp == &main_console) {
	    scp->pid = 0;
	    scp->proc = NULL;
	    scp->smode.mode = VT_AUTO;
	}
	else {
	    sc_vtb_destroy(&scp->vtb);
	    sc_vtb_destroy(&scp->scr);
	    sc_free_history_buffer(scp, scp->ysize);
	    SC_STAT(dev) = NULL;
	    kfree(scp, M_SYSCONS);
	}
#else
	scp->pid = 0;
	scp->proc = NULL;
	scp->smode.mode = VT_AUTO;
#endif
	scp->kbd_mode = K_XLATE;
	syscons_unlock();
	if (scp == scp->sc->cur_scp)
	    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
	DPRINTF(5, ("done.\n"));
    }
    (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
    ttyclose(tp);
    lwkt_reltoken(&tp->t_token);
    lwkt_reltoken(&vga_token);

    return(0);
}

static int
scread(struct dev_read_args *ap)
{
    int ret;

    lwkt_gettoken(&vga_token);
    sc_touch_scrn_saver();
    ret = ttyread(ap);
    lwkt_reltoken(&vga_token);
    return ret;
}

static int
sckbdevent(keyboard_t *thiskbd, int event, void *arg)
{
    sc_softc_t *sc;
    struct tty *cur_tty;
    int c; 
    size_t len;
    u_char *cp;

    lwkt_gettoken(&vga_token);
    /*
     * WARNING: In early boot sc->dev may not be setup yet.
     */
    sc = (sc_softc_t *)arg;
    /* assert(thiskbd == sc->kbd) */

    switch (event) {
    case KBDIO_KEYINPUT:
	if (sc->kbd && sc->kbd->kb_flags & KB_POLLED) {
		lwkt_reltoken(&vga_token);
		return 0;
	}
	break;
    case KBDIO_UNLOADING:
	syscons_lock();
	sc->kbd = NULL;
	sc->keyboard = -1;
	syscons_unlock();
	kbd_release(thiskbd, (void *)&sc->keyboard);
	lwkt_reltoken(&vga_token);
	return 0;
    default:
        lwkt_reltoken(&vga_token);
	return EINVAL;
    }

    /* 
     * Loop while there is still input to get from the keyboard.
     * I don't think this is nessesary, and it doesn't fix
     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
     */
    while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
	cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
	if (!ISTTYOPEN(cur_tty)) {
	    cur_tty = sc_console_tty;
	    if (!ISTTYOPEN(cur_tty))
		continue;
	}

	syscons_lock();
	if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty)) {
	    syscons_unlock();
	    continue;
	}
	syscons_unlock();

	lwkt_gettoken(&cur_tty->t_token);

	switch (KEYFLAGS(c)) {
	case 0x0000: /* normal key */
	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
	    break;
	case FKEY:  /* function key, return string */
	    cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
	    if (cp != NULL) {
	    	while (len-- >  0)
		    (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
	    }
	    break;
	case MKEY:  /* meta is active, prepend ESC */
	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
	    (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
	    break;
	case BKEY:  /* backtab fixed sequence (esc [ Z) */
	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
	    break;
	}
	lwkt_reltoken(&cur_tty->t_token);
    }

    syscons_lock();
    sc->cur_scp->status |= MOUSE_HIDDEN;
    syscons_unlock();

    lwkt_reltoken(&vga_token);
    return 0;
}

static int
scparam(struct tty *tp, struct termios *t)
{
    lwkt_gettoken(&tp->t_token);
    tp->t_ispeed = t->c_ispeed;
    tp->t_ospeed = t->c_ospeed;
    tp->t_cflag = t->c_cflag;
    lwkt_reltoken(&tp->t_token);

    return 0;
}

static int
scioctl(struct dev_ioctl_args *ap)
{
    cdev_t dev = ap->a_head.a_dev;
    u_long cmd = ap->a_cmd;
    caddr_t data = ap->a_data;
    int flag = ap->a_fflag;
    int error;
    int i;
    struct tty *tp;
    sc_softc_t *sc;
    scr_stat *scp;

    lwkt_gettoken(&vga_token);
    tp = dev->si_tty;

    error = sc_vid_ioctl(tp, cmd, data, flag);
    if (error != ENOIOCTL) {
        lwkt_reltoken(&vga_token);
	return error;
    }

#ifndef SC_NO_HISTORY
    error = sc_hist_ioctl(tp, cmd, data, flag);
    if (error != ENOIOCTL) {
        lwkt_reltoken(&vga_token);
	return error;
    }
#endif

#ifndef SC_NO_SYSMOUSE
    error = sc_mouse_ioctl(tp, cmd, data, flag);
    if (error != ENOIOCTL) {
        lwkt_reltoken(&vga_token);
	return error;
    }
#endif

    scp = SC_STAT(tp->t_dev);
    /* assert(scp != NULL) */
    /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
    sc = scp->sc;

    if (scp->tsw) {
	syscons_lock();
	error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag);
	syscons_unlock();
	if (error != ENOIOCTL) {
	    lwkt_reltoken(&vga_token);
	    return error;
	}
    }

    switch (cmd) {  		/* process console hardware related ioctl's */

    case GIO_ATTR:      	/* get current attributes */
	/* this ioctl is not processed here, but in the terminal emulator */
	lwkt_reltoken(&vga_token);
	return ENOTTY;

    case GIO_COLOR:     	/* is this a color console ? */
	if (sc->fbi != NULL)
	    *(int *)data = 1;
	else
	    *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
	if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME) {
	    lwkt_reltoken(&vga_token);
            return EINVAL;
	}
	syscons_lock();
	scrn_blank_time = *(int *)data;
	run_scrn_saver = (scrn_blank_time != 0);
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
	syscons_lock();
	if (!ISGRAPHSC(sc->cur_scp))
	    sc_remove_cursor_image(sc->cur_scp);
	if ((*(int*)data) & 0x01)
	    sc->flags |= SC_BLINK_CURSOR;
	else
	    sc->flags &= ~SC_BLINK_CURSOR;
	if ((*(int*)data) & 0x02) {
	    sc->flags |= SC_CHAR_CURSOR;
	} else
	    sc->flags &= ~SC_CHAR_CURSOR;
	/* 
	 * The cursor shape is global property; all virtual consoles
	 * are affected. Update the cursor in the current console...
	 */
	if (!ISGRAPHSC(sc->cur_scp)) {
	    sc_set_cursor_image(sc->cur_scp);
	    sc_draw_cursor_image(sc->cur_scp);
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_BELLTYPE: 	/* set bell type sound/visual */
	syscons_lock();

	if ((*(int *)data) & 0x01)
	    sc->flags |= SC_VISUAL_BELL;
	else
	    sc->flags &= ~SC_VISUAL_BELL;

	if ((*(int *)data) & 0x02)
	    sc->flags |= SC_QUIET_BELL;
	else
	    sc->flags &= ~SC_QUIET_BELL;

	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_GETINFO:  	/* get current (virtual) console info */
    {
	vid_info_t *ptr = (vid_info_t*)data;
	if (ptr->size == sizeof(struct vid_info)) {
	    ptr->m_num = sc->cur_scp->index;
	    ptr->font_size = scp->font_height;
	    ptr->mv_col = scp->xpos;
	    ptr->mv_row = scp->ypos;
	    ptr->mv_csz = scp->xsize;
	    ptr->mv_rsz = scp->ysize;
	    /*
	     * The following fields are filled by the terminal emulator. XXX
	     *
	     * ptr->mv_norm.fore
	     * ptr->mv_norm.back
	     * ptr->mv_rev.fore
	     * ptr->mv_rev.back
	     */
	    ptr->mv_grfc.fore = 0;      /* not supported */
	    ptr->mv_grfc.back = 0;      /* not supported */
	    ptr->mv_ovscan = scp->border;
	    if (scp == sc->cur_scp)
	        save_kbd_state(scp, FALSE);
	    ptr->mk_keylock = scp->status & LOCK_MASK;
	    lwkt_reltoken(&vga_token);
	    return 0;
	}
	lwkt_reltoken(&vga_token);
	return EINVAL;
    }

    case CONS_GETVERS:  	/* get version number */
	*(int*)data = 0x200;    /* version 2.0 */
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_IDLE:		/* see if the screen has been idle */
	/*
	 * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
	 * the user process may have been writing something on the
	 * screen and syscons is not aware of it. Declare the screen
	 * is NOT idle if it is in one of these modes. But there is
	 * an exception to it; if a screen saver is running in the 
	 * graphics mode in the current screen, we should say that the
	 * screen has been idle.
	 */
	*(int *)data = (sc->flags & SC_SCRN_IDLE)
		       && (!ISGRAPHSC(sc->cur_scp)
			   || (sc->cur_scp->status & SAVER_RUNNING));
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_SAVERMODE:	/* set saver mode */
	switch(*(int *)data) {
	case CONS_NO_SAVER:
	case CONS_USR_SAVER:
	    syscons_lock();
	    /* if a LKM screen saver is running, stop it first. */
	    scsplash_stick(FALSE);
	    saver_mode = *(int *)data;
#if NSPLASH > 0
	    if ((error = wait_scrn_saver_stop(NULL, TRUE))) {
		syscons_unlock();
		lwkt_reltoken(&vga_token);
		return error;
	    }
#endif /* NSPLASH */
	    run_scrn_saver = TRUE;
	    if (saver_mode == CONS_USR_SAVER)
		scp->status |= SAVER_RUNNING;
	    else
		scp->status &= ~SAVER_RUNNING;
	    scsplash_stick(TRUE);
	    syscons_unlock();
	    break;
	case CONS_LKM_SAVER:
	    syscons_lock();
	    if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
		scp->status &= ~SAVER_RUNNING;
	    saver_mode = *(int *)data;
	    syscons_unlock();
	    break;
	default:
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_SAVERSTART:	/* immediately start/stop the screen saver */
	/*
	 * Note that this ioctl does not guarantee the screen saver 
	 * actually starts or stops. It merely attempts to do so...
	 */
	syscons_lock();
	run_scrn_saver = (*(int *)data != 0);
	if (run_scrn_saver)
	    sc->scrn_time_stamp -= scrn_blank_time;
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case CONS_SCRSHOT:		/* get a screen shot */
    {
	scrshot_t *ptr = (scrshot_t*)data;
	syscons_lock();
	if (ISGRAPHSC(scp)) {
	    syscons_unlock();
	    lwkt_reltoken(&vga_token);
	    return EOPNOTSUPP;
	}
	if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
	    syscons_unlock();
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	syscons_unlock();
	copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
		 ptr->xsize * ptr->ysize * sizeof(uint16_t));
	lwkt_reltoken(&vga_token);
	return 0;
    }

    case VT_SETMODE:    	/* set screen switcher mode */
    {
	struct vt_mode *mode;

	mode = (struct vt_mode *)data;
	DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
	if (scp->smode.mode == VT_PROCESS) {
	    if (scp->proc == pfindn(scp->pid) && scp->proc != curproc) {
		DPRINTF(5, ("error EPERM\n"));
		lwkt_reltoken(&vga_token);
		return EPERM;
	    }
	}
	syscons_lock();
	if (mode->mode == VT_AUTO) {
	    scp->smode.mode = VT_AUTO;
	    scp->proc = NULL;
	    scp->pid = 0;
	    DPRINTF(5, ("VT_AUTO, "));
	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit) &&
		!sc->fb_blanked) {
		cons_unavail = FALSE;
	    }
	    if (finish_vt_rel(scp, TRUE) == 0)
		DPRINTF(5, ("reset WAIT_REL, "));
	    if (finish_vt_acq(scp) == 0)
		DPRINTF(5, ("reset WAIT_ACQ, "));
	} else {
	    if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
		|| !ISSIGVALID(mode->frsig)) {
		syscons_unlock();
		DPRINTF(5, ("error EINVAL\n"));
		lwkt_reltoken(&vga_token);
		return EINVAL;
	    }
	    DPRINTF(5, ("VT_PROCESS %d, ", curproc->p_pid));
	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
	    scp->proc = curproc;
	    scp->pid = scp->proc->p_pid;
	    if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
		cons_unavail = TRUE;
	}
	syscons_unlock();
	DPRINTF(5, ("\n"));
	lwkt_reltoken(&vga_token);
	return 0;
    }

    case VT_GETMODE:    	/* get screen switcher mode */
	bcopy(&scp->smode, data, sizeof(struct vt_mode));
	lwkt_reltoken(&vga_token);
	return 0;

    case VT_RELDISP:    	/* screen switcher ioctl */
	/*
	 * This must be the current vty which is in the VT_PROCESS
	 * switching mode...
	 */
	syscons_lock();
	if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
	    syscons_unlock();
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	/* ...and this process is controlling it. */
	if (scp->proc != curproc) {
	    syscons_unlock();
	    lwkt_reltoken(&vga_token);
	    return EPERM;
	}
	error = EINVAL;
	switch(*(int *)data) {
	case VT_FALSE:  	/* user refuses to release screen, abort */
	    if ((error = finish_vt_rel(scp, FALSE)) == 0)
		DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
	    break;
	case VT_TRUE:   	/* user has released screen, go on */
	    if ((error = finish_vt_rel(scp, TRUE)) == 0)
		DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
	    break;
	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
	    if ((error = finish_vt_acq(scp)) == 0)
		DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
	    syscons_unlock();
	    /* Wait for drm modesetting callback to finish. */
	    lwkt_reltoken(&vga_token);
	    if (sc->fb_set_par_task != NULL)
		taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
	    return error;
	default:
	    break;
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return error;

    case VT_OPENQRY:    	/* return free virtual console */
	for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
	    tp = VIRTUAL_TTY(sc, i);
	    if (!ISTTYOPEN(tp)) {
		*(int *)data = i + 1;
		lwkt_reltoken(&vga_token);
		return 0;
	    }
	}
	lwkt_reltoken(&vga_token);
	return EINVAL;

    case VT_ACTIVATE:   	/* switch to screen *data */
	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
	syscons_lock();
	sc_clean_up(sc->cur_scp, TRUE);
	error = sc_switch_scr(sc, i);
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return error;

    case VT_WAITACTIVE: 	/* wait for switch to occur */
	i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
	if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys)) {
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	syscons_lock();
	error = sc_clean_up(sc->cur_scp, TRUE);
	syscons_unlock();
	if (error) {
	    lwkt_reltoken(&vga_token);
	    return error;
	}

	/*
	 * scp might be NULL, we aren't sure why.  Check for NULL.
	 *
	 * http://bugs.dragonflybsd.org/issues/2481
	 */
	scp = SC_STAT(SC_DEV(sc, i));
	if (scp == NULL || scp == scp->sc->cur_scp) {
	    lwkt_reltoken(&vga_token);
	    return 0;
	}
	error = tsleep((caddr_t)&scp->smode, PCATCH, "waitvt", 0);
	/* May return ERESTART */
	lwkt_reltoken(&vga_token);
	/* Wait for drm modesetting callback to finish. */
	if (sc->fb_set_par_task != NULL)
	    taskqueue_drain(taskqueue_thread[0], sc->fb_set_par_task);
	return error;

    case VT_GETACTIVE:		/* get active vty # */
	*(int *)data = sc->cur_scp->index + 1;
	lwkt_reltoken(&vga_token);
	return 0;

    case VT_GETINDEX:		/* get this vty # */
	*(int *)data = scp->index + 1;
	lwkt_reltoken(&vga_token);
	return 0;

    case VT_LOCKSWITCH:		/* prevent vty switching */
	syscons_lock();
	if ((*(int *)data) & 0x01)
	    sc->flags |= SC_SCRN_VTYLOCK;
	else
	    sc->flags &= ~SC_SCRN_VTYLOCK;
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case KDENABIO:      	/* allow io operations */
	error = caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT);
	if (error != 0) {
	    lwkt_reltoken(&vga_token);
	    return error;
	}
	if (securelevel > 0) {
	    lwkt_reltoken(&vga_token);
	    return EPERM;
	}
#if defined(__x86_64__)
	curthread->td_lwp->lwp_md.md_regs->tf_rflags |= PSL_IOPL;
#endif
	lwkt_reltoken(&vga_token);
	return 0;

    case KDDISABIO:     	/* disallow io operations (default) */
#if defined(__x86_64__)
	curthread->td_lwp->lwp_md.md_regs->tf_rflags &= ~PSL_IOPL;
#endif
        lwkt_reltoken(&vga_token);
	return 0;

    case KDSKBSTATE:    	/* set keyboard state (locks) */
	if (*(int *)data & ~LOCK_MASK) {
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	syscons_lock();
	scp->status &= ~LOCK_MASK;
	scp->status |= *(int *)data;
	syscons_unlock();
	if (scp == sc->cur_scp)
	    update_kbd_state(scp, scp->status, LOCK_MASK, FALSE);
	lwkt_reltoken(&vga_token);
	return 0;

    case KDGKBSTATE:    	/* get keyboard state (locks) */
	if (scp == sc->cur_scp)
	    save_kbd_state(scp, FALSE);
	*(int *)data = scp->status & LOCK_MASK;
	lwkt_reltoken(&vga_token);
	return 0;

    case KDGETREPEAT:      	/* get keyboard repeat & delay rates */
    case KDSETREPEAT:      	/* set keyboard repeat & delay rates (new) */
	error = kbd_ioctl(sc->kbd, cmd, data);
	if (error == ENOIOCTL)
	    error = ENODEV;
	lwkt_reltoken(&vga_token);
	return error;

    case KDSKBMODE:     	/* set keyboard mode */
	switch (*(int *)data) {
	case K_XLATE:   	/* switch to XLT ascii mode */
	case K_RAW: 		/* switch to RAW scancode mode */
	case K_CODE: 		/* switch to CODE mode */
	    scp->kbd_mode = *(int *)data;
	    if (scp == sc->cur_scp)
		kbd_ioctl(sc->kbd, cmd, data);
            lwkt_reltoken(&vga_token);
	    return 0;
	default:
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	/* NOT REACHED */

    case KDGKBMODE:     	/* get keyboard mode */
	*(int *)data = scp->kbd_mode;
	lwkt_reltoken(&vga_token);
	return 0;

    case KDGKBINFO:
	error = kbd_ioctl(sc->kbd, cmd, data);
	if (error == ENOIOCTL)
	    error = ENODEV;
	lwkt_reltoken(&vga_token);
	return error;

    case KDMKTONE:      	/* sound the bell */
	syscons_lock();
	if (*(int*)data)
	    sc_bell(scp, (*(int*)data)&0xffff,
		    (((*(int*)data)>>16)&0xffff)*hz/1000);
	else
	    sc_bell(scp, scp->bell_pitch, scp->bell_duration);
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case KIOCSOUND:     	/* make tone (*data) hz */
	syscons_lock();
	if (scp == sc->cur_scp) {
	    if (*(int *)data) {
		error = sc_tone(*(int *)data);
	    } else {
		error = sc_tone(0);
	    }
	} else {
	    error = 0;
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return error;

    case KDGKBTYPE:     	/* get keyboard type */
	error = kbd_ioctl(sc->kbd, cmd, data);
	if (error == ENOIOCTL) {
	    /* always return something? XXX */
	    *(int *)data = 0;
	}
	lwkt_reltoken(&vga_token);
	return 0;

    case KDSETLED:      	/* set keyboard LED status */
	if (*(int *)data & ~LED_MASK) {	/* FIXME: LOCK_MASK? */
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	}
	syscons_lock();
	scp->status &= ~LED_MASK;
	scp->status |= *(int *)data;
	syscons_unlock();
	if (scp == sc->cur_scp)
	    update_kbd_leds(scp, scp->status);
	lwkt_reltoken(&vga_token);
	return 0;

    case KDGETLED:      	/* get keyboard LED status */
	if (scp == sc->cur_scp)
	    save_kbd_state(scp, FALSE);
	*(int *)data = scp->status & LED_MASK;
	lwkt_reltoken(&vga_token);
	return 0;

	case KBADDKBD:              /* add/remove keyboard to/from mux */
	case KBRELKBD:
		error = kbd_ioctl(sc->kbd, cmd, data);
		if (error == ENOIOCTL)
			error = ENODEV;
		lwkt_reltoken(&vga_token);
		return error;

    case CONS_SETKBD: 		/* set the new keyboard */
	{
	    keyboard_t *newkbd;

	    newkbd = kbd_get_keyboard(*(int *)data);
	    if (newkbd == NULL) {
		lwkt_reltoken(&vga_token);
		return EINVAL;
	    }
	    error = 0;
	    if (sc->kbd != newkbd) {
		i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
				 (void *)&sc->keyboard, sckbdevent, sc);
		/* i == newkbd->kb_index */
		if (i >= 0) {
		    if (sc->kbd != NULL) {
			save_kbd_state(sc->cur_scp, FALSE);
			kbd_release(sc->kbd, (void *)&sc->keyboard);
		    }
		    syscons_lock();
		    sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
		    sc->keyboard = i;
		    syscons_unlock();
		    kbd_ioctl(sc->kbd, KDSKBMODE,
			      (caddr_t)&sc->cur_scp->kbd_mode);
		    update_kbd_state(sc->cur_scp, sc->cur_scp->status,
			LOCK_MASK, FALSE);
		} else {
		    error = EPERM;	/* XXX */
		}
	    }
	    lwkt_reltoken(&vga_token);
	    return error;
	}

    case CONS_RELKBD: 		/* release the current keyboard */
	error = 0;
	if (sc->kbd != NULL) {
	    save_kbd_state(sc->cur_scp, FALSE);
	    error = kbd_release(sc->kbd, (void *)&sc->keyboard);
	    if (error == 0) {
		syscons_lock();
		sc->kbd = NULL;
		sc->keyboard = -1;
		syscons_unlock();
	    }
	}
	lwkt_reltoken(&vga_token);
	return error;

    case CONS_GETTERM:		/* get the current terminal emulator info */
	{
	    sc_term_sw_t *sw;

	    if (((term_info_t *)data)->ti_index == 0) {
		sw = scp->tsw;
	    } else {
		sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
	    }
	    if (sw != NULL) {
		strncpy(((term_info_t *)data)->ti_name, sw->te_name, 
			sizeof(((term_info_t *)data)->ti_name));
		strncpy(((term_info_t *)data)->ti_desc, sw->te_desc, 
			sizeof(((term_info_t *)data)->ti_desc));
		((term_info_t *)data)->ti_flags = 0;
		lwkt_reltoken(&vga_token);
		return 0;
	    } else {
		((term_info_t *)data)->ti_name[0] = '\0';
		((term_info_t *)data)->ti_desc[0] = '\0';
		((term_info_t *)data)->ti_flags = 0;
		lwkt_reltoken(&vga_token);
		return EINVAL;
	    }
	}

    case CONS_SETTERM:		/* set the current terminal emulator */
	syscons_lock();
	error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
	/* FIXME: what if scp == sc_console! XXX */
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return error;

    case GIO_SCRNMAP:   	/* get output translation table */
	bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
	lwkt_reltoken(&vga_token);
	return 0;

    case PIO_SCRNMAP:   	/* set output translation table */
	bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
	for (i=0; i<sizeof(sc->scr_map); i++) {
	    sc->scr_rmap[sc->scr_map[i]] = i;
	}
	lwkt_reltoken(&vga_token);
	return 0;

    case GIO_KEYMAP:		/* get keyboard translation table */
    case PIO_KEYMAP:		/* set keyboard translation table */
    case GIO_DEADKEYMAP:	/* get accent key translation table */
    case PIO_DEADKEYMAP:	/* set accent key translation table */
    case GETFKEY:		/* get function key string */
    case SETFKEY:		/* set function key string */
	error = kbd_ioctl(sc->kbd, cmd, data);
	if (error == ENOIOCTL)
	    error = ENODEV;
	lwkt_reltoken(&vga_token);
	return error;

#ifndef SC_NO_FONT_LOADING

    case PIO_FONT8x8:   	/* set 8x8 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	syscons_lock();
	bcopy(data, sc->font_8, 8*256);
	sc->fonts_loaded |= FONT_8;
	/*
	 * FONT KLUDGE
	 * Always use the font page #0. XXX
	 * Don't load if the current font size is not 8x8.
	 */
	if (sc->fbi == NULL
	    && ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height < 14)) {
	    sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case GIO_FONT8x8:   	/* get 8x8 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	if (sc->fonts_loaded & FONT_8) {
	    bcopy(sc->font_8, data, 8*256);
	    lwkt_reltoken(&vga_token);
	    return 0;
	}
	else {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}

    case PIO_FONT8x14:  	/* set 8x14 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	syscons_lock();
	bcopy(data, sc->font_14, 14*256);
	sc->fonts_loaded |= FONT_14;
	/*
	 * FONT KLUDGE
	 * Always use the font page #0. XXX
	 * Don't load if the current font size is not 8x14.
	 */
	if (sc->fbi == NULL
	    && ISTEXTSC(sc->cur_scp)
	    && (sc->cur_scp->font_height >= 14)
	    && (sc->cur_scp->font_height < 16)) {
	    sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case GIO_FONT8x14:  	/* get 8x14 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	if (sc->fonts_loaded & FONT_14) {
	    bcopy(sc->font_14, data, 14*256);
	    lwkt_reltoken(&vga_token);
	    return 0;
	}
	else {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
        }

    case PIO_FONT8x16:  	/* set 8x16 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	syscons_lock();
	bcopy(data, sc->font_16, 16*256);
	sc->fonts_loaded |= FONT_16;
	/*
	 * FONT KLUDGE
	 * Always use the font page #0. XXX
	 * Don't load if the current font size is not 8x16.
	 */
	if (sc->fbi == NULL
	    && ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_height >= 16))  {
	    sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
	}
	syscons_unlock();
	lwkt_reltoken(&vga_token);
	return 0;

    case GIO_FONT8x16:  	/* get 8x16 dot font */
	if (sc->fbi == NULL && !ISFONTAVAIL(sc->adp->va_flags)) {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
	}
	if (sc->fonts_loaded & FONT_16) {
	    bcopy(sc->font_16, data, 16*256);
	    lwkt_reltoken(&vga_token);
	    return 0;
	}
	else {
	    lwkt_reltoken(&vga_token);
	    return ENXIO;
        }

#endif /* SC_NO_FONT_LOADING */

    default:
	break;
    }

    lwkt_gettoken(&tp->t_token);
    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, ap->a_cred);
    if (error != ENOIOCTL) {
	lwkt_reltoken(&tp->t_token);
        lwkt_reltoken(&vga_token);

	return(error);
    }

    error = ttioctl(tp, cmd, data, flag);
    if (error != ENOIOCTL) {
	lwkt_reltoken(&tp->t_token);
        lwkt_reltoken(&vga_token);

	return(error);
    }

    lwkt_reltoken(&tp->t_token);
    lwkt_reltoken(&vga_token);

    return(ENOTTY);
}

static void
scstart(struct tty *tp)
{
    struct clist *rbp;
    int len;
    u_char buf[PCBURST];
    scr_stat *scp = SC_STAT(tp->t_dev);

    syscons_lock();
    if (scp->status & SLKED ||
	(scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
    {
	syscons_unlock();
	return;
    }
    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
	tp->t_state |= TS_BUSY;
	rbp = &tp->t_outq;
	while (rbp->c_cc) {
	    len = clist_qtob(rbp, buf, PCBURST);
	    sc_puts(scp, buf, len);
	}
	tp->t_state &= ~TS_BUSY;
	syscons_unlock();
	ttwwakeup(tp);
    } else {
	syscons_unlock();
    }
}

static void
sccnprobe(struct consdev *cp)
{
    int unit;
    int flags;

    cp->cn_pri = sc_get_cons_priority(&unit, &flags);

    /* a video card is always required */
    if (probe_efi_fb(1) != 0 && !scvidprobe(unit, flags, TRUE))
	cp->cn_pri = CN_DEAD;

    /* syscons will become console even when there is no keyboard */
    sckbdprobe(unit, flags, TRUE);

    if (cp->cn_pri == CN_DEAD) {
	return;
    }

    /* initialize required fields */
    cp->cn_probegood = 1;
}

static void
sccninit(struct consdev *cp)
{
    int unit;
    int flags;

    sc_get_cons_priority(&unit, &flags);
    scinit(unit, flags | SC_KERNEL_CONSOLE);
    sc_console_unit = unit;
    sc_console = sc_get_softc(unit, SC_KERNEL_CONSOLE)->console_scp;
}

static void
sccninit_fini(struct consdev *cp)
{
	if (cctl_dev == NULL)
		kprintf("sccninit_fini: WARNING: cctl_dev is NULL!\n");
	cp->cn_dev = cctl_dev;
}

/*
 * This is called when the console is switched away from syscons to
 * a late-configuring device (such as a PCIe serial port).  Since
 * late-configuring devices basically didn't exist until now, this
 * routine was never called, and SURPRISE!  It doesn't work... causes
 * syscons to implode due to all the funny console-not-console structure
 * selection syscons does.
 *
 * Make it a NOP.  Just leave the console designation intact, even
 * though it is no longer a console.
 */
static void
sccnterm(struct consdev *cp)
{
#if 0
    /* we are not the kernel console any more, release everything */

    if (sc_console_unit < 0)
	return;			/* shouldn't happen */

#if 0 /* XXX */
    syscons_lock();
    sc_clear_screen(sc_console);
    sccnupdate(sc_console);
    syscons_unlock();
#endif
    scterm(sc_console_unit, SC_KERNEL_CONSOLE);
    sc_console_unit = -1;
    sc_console = NULL;
#endif
}

/*
 * Console path - cannot block!
 */
static void
sccnputc(void *private, int c)
{
    u_char buf[1];
    scr_stat *scp = sc_console;
    void *save;
#ifndef SC_NO_HISTORY
#if 0
    struct tty *tp;
#endif
#endif /* !SC_NO_HISTORY */

    /* assert(sc_console != NULL) */

    if (syscons_lock_nonblock() != 0)
	return;
#ifndef SC_NO_HISTORY
    if (scp == scp->sc->cur_scp && scp->status & SLKED) {
	scp->status &= ~SLKED;
#if 0
	/* This can block, illegal in the console path */
	update_kbd_state(scp, scp->status, SLKED, TRUE);
#endif
	if (scp->status & BUFFER_SAVED) {
	    if (!sc_hist_restore(scp))
		sc_remove_cutmarking(scp);
	    scp->status &= ~BUFFER_SAVED;
	    scp->status |= CURSOR_ENABLED;
	    sc_draw_cursor_image(scp);
	}
#if 0
	tp = VIRTUAL_TTY(scp->sc, scp->index);
	/* This can block, illegal in the console path */
	if (ISTTYOPEN(tp)) {
	    scstart(tp);
	}
#endif
    }
#endif /* !SC_NO_HISTORY */

    save = scp->ts;
    if (kernel_console_ts != NULL)
	scp->ts = kernel_console_ts;
    buf[0] = c;
    sc_puts(scp, buf, 1);
    scp->ts = save;

    sccnupdate(scp);
    syscons_unlock();
}

/*
 * Console path - cannot block!
 */
static int
sccngetc(void *private)
{
    return sccngetch(0);
}

/*
 * Console path - cannot block!
 */
static int
sccncheckc(void *private)
{
    return sccngetch(SCGETC_NONBLOCK);
}

/*
 * Set polling mode (also disables the tty feed), use when
 * polling for console key input.
 */
static void
sccnpoll(void *private, int on)
{
    scr_stat *scp;

    syscons_lock();
    sc_touch_scrn_saver();
    scp = sc_console->sc->cur_scp;	/* XXX */
    syscons_unlock();
    if (scp->sc->kbd)
	    kbd_poll(scp->sc->kbd, on);
}

static void
sccndbctl(void *private, int on)
{
    /* assert(sc_console_unit >= 0) */
    /* try to switch to the kernel console screen */
    if (on && debugger == 0) {
	/*
	 * TRY to make sure the screen saver is stopped, 
	 * and the screen is updated before switching to 
	 * the vty0.
	 */
	scrn_timer(NULL);
	if (!cold
	    && sc_console->sc->cur_scp->smode.mode == VT_AUTO
	    && sc_console->smode.mode == VT_AUTO) {
	    sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
	    syscons_lock();
	    sc_switch_scr(sc_console->sc, sc_console->index);
	    syscons_unlock();
	}
    }
    if (on)
	++debugger;
    else
	--debugger;
}

/*
 * Console path - cannot block!
 */
static int
sccngetch(int flags)
{
    static struct fkeytab fkey;
    static int fkeycp;
    scr_stat *scp;
    u_char *p;
    int cur_mode;
    int c;

    syscons_lock();
    /* assert(sc_console != NULL) */

    /* 
     * Stop the screen saver and update the screen if necessary.
     * What if we have been running in the screen saver code... XXX
     */
    sc_touch_scrn_saver();
    scp = sc_console->sc->cur_scp;	/* XXX */
    sccnupdate(scp);
    syscons_unlock();

    if (fkeycp < fkey.len) {
	return fkey.str[fkeycp++];
    }

    if (scp->sc->kbd == NULL) {
	return -1;
    }

    /* 
     * Make sure the keyboard is accessible even when the kbd device
     * driver is disabled.
     */
    crit_enter();
    kbd_enable(scp->sc->kbd);

    /* we shall always use the keyboard in the XLATE mode here */
    cur_mode = scp->kbd_mode;
    scp->kbd_mode = K_XLATE;
    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);

    c = scgetc(scp->sc, SCGETC_CN | flags);

    scp->kbd_mode = cur_mode;
    kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
    kbd_disable(scp->sc->kbd);
    crit_exit();

    switch (KEYFLAGS(c)) {
    case 0:	/* normal char */
	return KEYCHAR(c);
    case FKEY:	/* function key */
	p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
	fkey.len = fkeycp;
	if ((p != NULL) && (fkey.len > 0)) {
	    bcopy(p, fkey.str, fkey.len);
	    fkeycp = 1;
	    return fkey.str[0];
	}
	return c;	/* XXX */
    case NOKEY:
	if (flags & SCGETC_NONBLOCK)
		return c;
	/* fall through */
    case ERRKEY:
    default:
	return -1;
    }
    /* NOT REACHED */
}

static void
sccnupdate(scr_stat *scp)
{
    int ddb_active;

#ifdef DDB
    ddb_active = db_active;
#else
    ddb_active = 0;
#endif

    /* this is a cut-down version of scrn_timer()... */

    /*
     * Don't update if videoio is in progress.  However, if we are paniced
     * or in the debugger it is possible that the variables might be stuck,
     * so ignore it in such cases.
     */
    if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress) {
	if (panicstr == NULL && ddb_active == 0 && shutdown_in_progress == 0)
		return;
    }

    if (debugger > 0 || panicstr || ddb_active || shutdown_in_progress) {
	sc_touch_scrn_saver();
    } else if (scp != scp->sc->cur_scp) {
	return;
    }

    if (!run_scrn_saver)
	scp->sc->flags &= ~SC_SCRN_IDLE;
#if NSPLASH > 0
    /*
     * This is a hard path, we cannot call stop_scrn_saver() here.
     */
    if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
	if (scp->sc->flags & SC_SCRN_BLANKED) {
	    sc_touch_scrn_saver();
            /*stop_scrn_saver(scp->sc, current_saver);*/
	}
#endif /* NSPLASH */

    if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
	|| scp->sc->switch_in_progress) {
	return;
    }

    /*
     * FIXME: unlike scrn_timer(), we call scrn_update() from here even
     * when write_in_progress is non-zero.  XXX
     */
    if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
	scrn_update(scp, TRUE, SCRN_ASYNCOK);
}

static void
scrn_timer(void *arg)
{
    static int kbd_interval = 0;
    struct timeval tv;
    sc_softc_t *sc;
    scr_stat *scp;
    int again;

    /*
     * Setup depending on who called us
     */
    again = (arg != NULL);
    if (arg != NULL) {
	sc = (sc_softc_t *)arg;
    } else if (sc_console != NULL) {
	sc = sc_console->sc;
    } else {
	return;
    }

    /*
     * Don't do anything when we are performing some I/O operations.
     * (These are initiated by the frontend?)
     */
    if (sc->font_loading_in_progress || sc->videoio_in_progress) {
	if (again)
	    callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
	return;
    }

    /*
     * Try to allocate a keyboard automatically
     */
    if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
	if (++kbd_interval >= 25) {
	    sc->keyboard = sc_allocate_keyboard(sc, -1);
	    if (sc->keyboard >= 0) {
		sc->kbd = kbd_get_keyboard(sc->keyboard);
		kbd_ioctl(sc->kbd, KDSKBMODE,
			  (caddr_t)&sc->cur_scp->kbd_mode);
		update_kbd_state(sc->cur_scp, sc->cur_scp->status,
		    LOCK_MASK, FALSE);
	    }
	    kbd_interval = 0;
	}
    }

    /*
     * Should we stop the screen saver?  We need the syscons_lock
     * for most of this stuff.
     */
    getmicrouptime(&tv);

    if (syscons_lock_nonblock() != 0) {
	/* failed to get the lock */
	if (again)
	    callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
	return;
    }
    /* successful lock */

    if (debugger > 0 || panicstr || shutdown_in_progress)
	sc_touch_scrn_saver();
    if (run_scrn_saver) {
	if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
	    sc->flags |= SC_SCRN_IDLE;
	else
	    sc->flags &= ~SC_SCRN_IDLE;
    } else {
	sc->scrn_time_stamp = tv.tv_sec;
	sc->flags &= ~SC_SCRN_IDLE;
	if (scrn_blank_time > 0)
	    run_scrn_saver = TRUE;
    }
#if NSPLASH > 0
    if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE)) {
	if ((sc->flags & SC_SCRN_BLANKED) && !ISGRAPHSC(sc->cur_scp))
            stop_scrn_saver(sc, current_saver);
    }
#endif /* NSPLASH */

    /* should we just return ? */
    if (sc->blink_in_progress || sc->switch_in_progress ||
	sc->write_in_progress)
    {
	syscons_unlock();
	if (again)
	    callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
	return;
    }

    /* Update the screen */
    scp = sc->cur_scp;		/* cur_scp may have changed... */
    if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
	scrn_update(scp, TRUE, SCRN_ASYNCOK);

#if NSPLASH > 0
    /* should we activate the screen saver? */
    if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
	if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
	    (*current_saver)(sc, TRUE);
#endif /* NSPLASH */

    syscons_unlock();
    if (again)
	callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
}

static int
and_region(int *s1, int *e1, int s2, int e2)
{
    if (*e1 < s2 || e2 < *s1)
	return FALSE;
    *s1 = imax(*s1, s2);
    *e1 = imin(*e1, e2);
    return TRUE;
}

/*
 * Refresh modified frame buffer range.  Also used to
 * scroll (entire FB is marked modified).
 *
 * This function is allowed to run without the syscons_lock,
 * so scp fields can change unexpected.  We cache most values
 * that we care about and silently discard illegal ranges.
 */
static void 
scrn_update(scr_stat *scp, int show_cursor, int flags)
{
    int start;
    int end;
    int cpos;
    int copos;
    int s;
    int e;
    int ddb_active;

#ifdef DDB
    ddb_active = db_active;
#else
    ddb_active = 0;
#endif

    /*
     * Try to run large screen updates as an async operation, which will
     * call this function again from a thread with BULKUNLOCK set, allowing
     * interrupts during the update which tends to make the system behave
     * better (no sound glitches, etc).
     */
    if ((flags & SCRN_ASYNCOK) && syscons_async && scp->asynctd &&
	(scp->end - scp->start) > 16 &&
	panicstr == NULL && ddb_active == 0 && shutdown_in_progress == 0) {
	scp->show_cursor = show_cursor;
	scp->queue_update_td = 1;
	wakeup(&scp->asynctd);
	return;
    }
    if (flags & SCRN_BULKUNLOCK)
	syscons_lock();

    /*
     * Synchronous operation or called from
     */

    /* assert(scp == scp->sc->cur_scp) */
    atomic_add_int(&scp->sc->videoio_in_progress, 1);

    cpos = scp->cursor_pos;
    copos = scp->cursor_oldpos;

#ifndef SC_NO_CUTPASTE
    /* remove the previous mouse pointer image if necessary */
    if (scp->status & MOUSE_VISIBLE) {
	s = scp->mouse_pos;
	e = scp->mouse_pos + scp->xsize + 1;
	if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
	    || and_region(&s, &e, scp->start, scp->end)
	    || ((scp->status & CURSOR_ENABLED) && 
		(cpos != copos) &&
		(and_region(&s, &e, cpos, cpos)
		 || and_region(&s, &e, copos, copos)))) {
	    sc_remove_mouse_image(scp);
	    if (scp->end >= scp->xsize*scp->ysize)
		scp->end = scp->xsize*scp->ysize - 1;
	}
    }
#endif /* !SC_NO_CUTPASTE */

    /*
     * WARNING: Don't add debugging kprintf()s with syscons locked.
     *		Or at all.
     */
    if (scp->end >= scp->xsize*scp->ysize) {
	scp->end = scp->xsize*scp->ysize - 1;
    }
    if (scp->start < 0) {
	scp->start = 0;
    }

    /*
     * Main update, extract update range and reset
     * ASAP.
     */
    start = scp->start;
    end = scp->end;
    scp->end = 0;
    scp->start = scp->xsize * scp->ysize - 1;

    if (start <= end)  {
	if (flags & SCRN_BULKUNLOCK) {
	    atomic_add_int(&scp->sc->videoio_in_progress, -1);
	    syscons_unlock();
	}
	(*scp->rndr->draw)(scp, start, end - start + 1, FALSE);
	if (flags & SCRN_BULKUNLOCK) {
	    syscons_lock();
	    atomic_add_int(&scp->sc->videoio_in_progress, 1);
	}

	/*
	 * Handle cut sequence
	 */
	s = scp->mouse_cut_start;
	e = scp->mouse_cut_end;
	cpu_ccfence();		/* allowed to race */

	if (e >= 0) {
	    if (s > e) {
		int r = s;
		s = e;
		e = r;
	    }
	    /* does the cut-mark region overlap with the update region? */
	    if (and_region(&s, &e, start, end)) {
		if (flags & SCRN_BULKUNLOCK) {
		    atomic_add_int(&scp->sc->videoio_in_progress, -1);
		    syscons_unlock();
		}
		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
		if (flags & SCRN_BULKUNLOCK) {
		    syscons_lock();
		    atomic_add_int(&scp->sc->videoio_in_progress, 1);
		}
	    }
	}
    }

    /* we are not to show the cursor and the mouse pointer... */
    if (!show_cursor)
	goto cleanup;

    /* update cursor image */
    if (scp->status & CURSOR_ENABLED) {
	s = start;
	e = end;
        /* did cursor move since last time ? */
        if (cpos != copos) {
            /* do we need to remove old cursor image ? */
            if (!and_region(&s, &e, copos, copos))
                sc_remove_cursor_image(scp);
            sc_draw_cursor_image(scp);
        } else {
            if (s <= e && and_region(&s, &e, cpos, cpos)) {
		/* cursor didn't move, but has been overwritten */
		sc_draw_cursor_image(scp);
	    } else if (scp->sc->flags & SC_BLINK_CURSOR) {
		/* if it's a blinking cursor, update it */
		(*scp->rndr->blink_cursor)(scp, cpos,
					   sc_inside_cutmark(scp, cpos));
	    }
        }
    }

#ifndef SC_NO_CUTPASTE
    /* update "pseudo" mouse pointer image */
    if (scp->sc->flags & SC_MOUSE_ENABLED) {
	if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
	    scp->status &= ~MOUSE_MOVED;
	    sc_draw_mouse_image(scp);
	}
    }
#endif /* SC_NO_CUTPASTE */

    /*
     * Cleanup
     */
cleanup:
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
    if (flags & SCRN_BULKUNLOCK)
	syscons_unlock();
}

/*
 * Thread handles potentially expensive screen updates.   The function is
 * expected to operate safely without locks.
 */
static void
scrn_update_thread(void *arg)
{
	scr_stat *scp = arg;
	for (;;) {
		if (scp->queue_update_td == 0) {
			tsleep_interlock(&scp->asynctd, 0);
			if (scp->queue_update_td == 0)
				tsleep(&scp->asynctd, PINTERLOCKED, "wait", 0);
		}
		scp->queue_update_td = 0;
		lockmgr(&sc_asynctd_lk, LK_EXCLUSIVE);
		atomic_add_int(&scp->sc->videoio_in_progress, 1);
		scrn_update(scp, scp->show_cursor, SCRN_BULKUNLOCK);
		atomic_add_int(&scp->sc->videoio_in_progress, -1);
		lockmgr(&sc_asynctd_lk, LK_RELEASE);
	}
}

static void
sc_fb_set_par(void *context, int pending)
{
	sc_softc_t *sc = context;

	lwkt_gettoken(&vga_token);
	if (sc->fbi != NULL && sc->fbi->fbops.fb_set_par != NULL)
	    sc->fbi->fbops.fb_set_par(sc->fbi);
	lwkt_reltoken(&vga_token);
}

#if NSPLASH > 0
static void
sc_fb_blank(void *context, int pending)
{
	sc_softc_t *sc = context;

	lwkt_gettoken(&vga_token);
	if (sc->fbi != NULL && sc->fbi->fbops.fb_blank != NULL) {
		/* 0 == FB_BLANK_UNBLANK and 4 == FB_BLANK_POWERDOWN */
		if (sc->flags & SC_SCRN_BLANKED) {
			sc->fbi->fbops.fb_blank(4, sc->fbi);
			sc->fb_blanked = TRUE;
			if (sc->unit == sc_console_unit)
				cons_unavail = TRUE;
		} else {
			sc->fbi->fbops.fb_blank(0, sc->fbi);
			sc->fb_blanked = FALSE;
			if (sc->unit == sc_console_unit &&
			    sc->cur_scp->smode.mode == VT_AUTO)
				cons_unavail = FALSE;
		}
	}
	lwkt_reltoken(&vga_token);
}

static void
scframebuffer_saver(sc_softc_t *sc, int show)
{
	scr_stat *scp = sc->cur_scp;

	if (panicstr)
		return;

	/*
	 * Gets called on every screen update while screensaver is active,
	 * so we need to check the SC_SCRN_BLANKED flag ourselves.
	 */
	if ((sc->flags & SC_SCRN_BLANKED) && show)
		return;

	taskqueue_cancel(taskqueue_thread[0], sc->fb_blank_task, NULL);
	crit_enter();
	if (show) {
		scp->status |= SAVER_RUNNING;
		sc->flags |= SC_SCRN_BLANKED;
		++scrn_blanked;
	} else {
		scp->status &= ~SAVER_RUNNING;
		sc->flags &= ~SC_SCRN_BLANKED;
		--scrn_blanked;
	}
	crit_exit();
	taskqueue_enqueue(taskqueue_thread[0], sc->fb_blank_task);
}

static int
scsplash_callback(int event, void *arg)
{
    sc_softc_t *sc;
    int error;

    sc = (sc_softc_t *)arg;

    switch (event) {
    case SPLASH_INIT:
	if (add_scrn_saver(scsplash_saver) == 0) {
	    sc->flags &= ~SC_SAVER_FAILED;
	    run_scrn_saver = TRUE;
	    if (cold) {
		scsplash_stick(TRUE);
		(*current_saver)(sc, TRUE);
	    }
	}
	return 0;

    case SPLASH_TERM:
	if (current_saver == scsplash_saver) {
	    scsplash_stick(FALSE);
	    error = remove_scrn_saver(scsplash_saver);
	    if (error) {
		return error;
            }
	}
	return 0;

    default:
	return EINVAL;
    }
}

static void
scsplash_saver(sc_softc_t *sc, int show)
{
    static int busy = FALSE;
    scr_stat *scp;

    if (busy)
	return;
    busy = TRUE;

    scp = sc->cur_scp;
    if (show) {
	if (!(sc->flags & SC_SAVER_FAILED)) {
	    if (!(sc->flags & SC_SCRN_BLANKED))
		set_scrn_saver_mode(scp, -1, NULL, 0);
	    switch (splash(sc->adp, TRUE)) {
	    case 0:		/* succeeded */
		break;
	    case EAGAIN:	/* try later */
		restore_scrn_saver_mode(scp, FALSE);
		sc_touch_scrn_saver();		/* XXX */
		break;
	    default:
		sc->flags |= SC_SAVER_FAILED;
		scsplash_stick(FALSE);
		restore_scrn_saver_mode(scp, TRUE);
		kprintf("scsplash_saver(): failed to put up the image\n");
		break;
	    }
	}
    } else if (!sticky_splash) {
	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
	    restore_scrn_saver_mode(scp, TRUE);
    }
    busy = FALSE;
}

static int
add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
{
#if 0
    int error;

    if (current_saver != none_saver) {
	error = remove_scrn_saver(current_saver);
	if (error)
	    return error;
    }
#endif
    if (current_saver != none_saver) {
	return EBUSY;
    }

    run_scrn_saver = FALSE;
    saver_mode = CONS_LKM_SAVER;
    current_saver = this_saver;
    return 0;
}

static int
remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
{
    if (current_saver != this_saver)
	return EINVAL;

#if 0
    /*
     * In order to prevent `current_saver' from being called by
     * the timeout routine `scrn_timer()' while we manipulate 
     * the saver list, we shall set `current_saver' to `none_saver' 
     * before stopping the current saver, rather than blocking by `splXX()'.
     */
    current_saver = none_saver;
    if (scrn_blanked)
        stop_scrn_saver(this_saver);
#endif
    /* unblank all blanked screens */
    wait_scrn_saver_stop(NULL, FALSE);
    if (scrn_blanked) {
	return EBUSY;
    }

    current_saver = none_saver;
    return 0;
}

static int
set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
{

    /* assert(scp == scp->sc->cur_scp) */
    crit_enter();
    if (!ISGRAPHSC(scp))
	sc_remove_cursor_image(scp);
    scp->splash_save_mode = scp->mode;
    scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
    scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
    scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
    scp->sc->flags |= SC_SCRN_BLANKED;
    ++scrn_blanked;
    crit_exit();
    if (mode < 0) {
	return 0;
    }
    scp->mode = mode;
    if (set_mode(scp) == 0) {
	if (scp->sc->fbi == NULL &&
	    scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS) {
	    scp->status |= GRAPHICS_MODE;
	}
	if (scp->sc->fbi == NULL && pal != NULL)
	    load_palette(scp->sc->adp, pal);
	sc_set_border(scp, border);
	return 0;
    } else {
	crit_enter();
	scp->mode = scp->splash_save_mode;
	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
	scp->status |= scp->splash_save_status;
	crit_exit();
	return 1;
    }
    /* NOTREACHED */
}

static int
restore_scrn_saver_mode(scr_stat *scp, int changemode)
{
    int mode;
    int status;

    /* assert(scp == scp->sc->cur_scp) */
    crit_enter();
    mode = scp->mode;
    status = scp->status;
    scp->mode = scp->splash_save_mode;
    scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
    scp->status |= scp->splash_save_status;
    scp->sc->flags &= ~SC_SCRN_BLANKED;
    if (!changemode) {
	if (!ISGRAPHSC(scp))
	    sc_draw_cursor_image(scp);
	--scrn_blanked;
	crit_exit();
	return 0;
    }
    if (set_mode(scp) == 0) {
	if (scp->sc->fbi == NULL)
	    load_palette(scp->sc->adp, scp->sc->palette);
	--scrn_blanked;
	crit_exit();
	return 0;
    } else {
	scp->mode = mode;
	scp->status = status;
	crit_exit();
	return 1;
    }
    /* NOTREACHED */
}

static void
stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
{
    (*saver)(sc, FALSE);
    run_scrn_saver = FALSE;
    /* the screen saver may have chosen not to stop after all... */
    if (sc->flags & SC_SCRN_BLANKED) {
	return;
    }

    mark_all(sc->cur_scp);
    if (sc->delayed_next_scr)
	sc_switch_scr(sc, sc->delayed_next_scr - 1);
    wakeup(&scrn_blanked);
}

static int
wait_scrn_saver_stop(sc_softc_t *sc, int need_unlock)
{
    int error = 0;

    while (scrn_blanked > 0) {
	run_scrn_saver = FALSE;
	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
	    error = 0;
	    break;
	}
	if (need_unlock) {
	    tsleep_interlock(&scrn_blanked, PCATCH);
	    syscons_unlock();
	    error = tsleep(&scrn_blanked, PCATCH | PINTERLOCKED, "scrsav", 0);
	    syscons_lock();
	} else {
	    error = tsleep(&scrn_blanked, PCATCH, "scrsav", 0);
	}
	/* May return ERESTART */
	if (error)
		break;
    }
    run_scrn_saver = FALSE;
    return error;
}
#endif /* NSPLASH */

void
sc_start_scrn_saver(sc_softc_t *sc)
{
#if NSPLASH > 0
    (*current_saver)(sc, TRUE);
#endif
}

void
sc_stop_scrn_saver(sc_softc_t *sc)
{
#if NSPLASH > 0
    if (sc->flags & SC_SCRN_BLANKED)
	stop_scrn_saver(sc, current_saver);
#endif
}

void
sc_touch_scrn_saver(void)
{
    scsplash_stick(FALSE);
    run_scrn_saver = FALSE;
}

int
sc_switch_scr(sc_softc_t *sc, u_int next_scr)
{
    scr_stat *cur_scp;
    struct tty *tp;

    DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));

    /* prevent switch if previously requested */
    if (sc->flags & SC_SCRN_VTYLOCK) {
	    sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
		sc->cur_scp->bell_duration);
	    return EPERM;
    }

    /* delay switch if the screen is blanked or being updated */
    if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
	|| sc->blink_in_progress || sc->videoio_in_progress) {
	sc->delayed_next_scr = next_scr + 1;
	sc_touch_scrn_saver();
	DPRINTF(5, ("switch delayed\n"));
	return 0;
    }

    cur_scp = sc->cur_scp;

    /*
     * we are in the middle of the vty switching process...
     *
     * This may be in the console path, be very careful.  pfindn() is
     * still going to use a spinlock but it no longer uses tokens so
     * we should be ok.
     */
    if (sc->switch_in_progress &&
	(cur_scp->smode.mode == VT_PROCESS) &&
	cur_scp->proc) {
	if (cur_scp->proc != pfindn(cur_scp->pid)) {
	    /* 
	     * The controlling process has died!!.  Do some clean up.
	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode' 
	     * are not reset here yet; they will be cleared later.
	     */
	    DPRINTF(5, ("cur_scp controlling process %d died, ", cur_scp->pid));
	    if (cur_scp->status & SWITCH_WAIT_REL) {
		/*
		 * Force the previous switch to finish, but return now 
		 * with error.
		 *
		 */
		DPRINTF(5, ("reset WAIT_REL, "));
		finish_vt_rel(cur_scp, TRUE);
		DPRINTF(5, ("finishing previous switch\n"));
		return EINVAL;
	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
		/* let's assume screen switch has been completed. */
		DPRINTF(5, ("reset WAIT_ACQ, "));
		finish_vt_acq(cur_scp);
	    } else {
		/* 
	 	 * We are in between screen release and acquisition, and
		 * reached here via scgetc() or scrn_timer() which has 
		 * interrupted exchange_scr(). Don't do anything stupid.
		 */
		DPRINTF(5, ("waiting nothing, "));
	    }
	} else {
	    /*
	     * The controlling process is alive, but not responding... 
	     * It is either buggy or it may be just taking time.
	     * The following code is a gross kludge to cope with this
	     * problem for which there is no clean solution. XXX
	     */
	    if (cur_scp->status & SWITCH_WAIT_REL) {
		switch (sc->switch_in_progress++) {
		case 1:
		    break;
		case 2:
		    DPRINTF(5, ("sending relsig again, "));
		    signal_vt_rel(cur_scp);
		    break;
		case 3:
		    break;
		case 4:
		default:
		    /*
		     * Act as if the controlling program returned
		     * VT_FALSE.
		     *
		     */
		    DPRINTF(5, ("force reset WAIT_REL, "));
		    finish_vt_rel(cur_scp, FALSE);
		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
		    return EINVAL;
		}
	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
		switch (sc->switch_in_progress++) {
		case 1:
		    break;
		case 2:
		    DPRINTF(5, ("sending acqsig again, "));
		    signal_vt_acq(cur_scp);
		    break;
		case 3:
		    break;
		case 4:
		default:
		     /* clear the flag and finish the previous switch */
		    DPRINTF(5, ("force reset WAIT_ACQ, "));
		    finish_vt_acq(cur_scp);
		    break;
		}
	    }
	}
    }

    /*
     * Return error if an invalid argument is given, or vty switch
     * is still in progress.
     */
    if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
	|| sc->switch_in_progress) {
	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
	DPRINTF(5, ("error 1\n"));
	return EINVAL;
    }

    /*
     * Don't allow switching away from the graphics mode vty
     * if the switch mode is VT_AUTO, unless the next vty is the same 
     * as the current or the current vty has been closed (but showing).
     */
    tp = VIRTUAL_TTY(sc, cur_scp->index);
    if ((cur_scp->index != next_scr)
	&& ISTTYOPEN(tp)
	&& (cur_scp->smode.mode == VT_AUTO)
	&& ISGRAPHSC(cur_scp)) {
	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
	DPRINTF(5, ("error, graphics mode\n"));
	return EINVAL;
    }

    /*
     * Is the wanted vty open? Don't allow switching to a closed vty.
     * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
     * Note that we always allow the user to switch to the kernel 
     * console even if it is closed.
     */
    if ((sc_console == NULL) || (next_scr != sc_console->index)) {
	tp = VIRTUAL_TTY(sc, next_scr);
	if (!ISTTYOPEN(tp)) {
	    sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
	    return EINVAL;
	}
	if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
	    DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
	    return EINVAL;
	}
    }

    /* this is the start of vty switching process... */
    ++sc->switch_in_progress;
    sc->delayed_next_scr = 0;
    sc->old_scp = cur_scp;
    sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
    if (sc->new_scp == sc->old_scp) {
	sc->switch_in_progress = 0;
	wakeup((caddr_t)&sc->new_scp->smode);
	DPRINTF(5, ("switch done (new == old)\n"));
	return 0;
    }

    /* has controlling process died? */
    vt_proc_alive(sc->old_scp);
    vt_proc_alive(sc->new_scp);

    /* wait for the controlling process to release the screen, if necessary */
    if (signal_vt_rel(sc->old_scp)) {
	return 0;
    }

    /* go set up the new vty screen */
    exchange_scr(sc);

    /* wake up processes waiting for this vty */
    wakeup((caddr_t)&sc->cur_scp->smode);

    /* wait for the controlling process to acknowledge, if necessary */
    if (signal_vt_acq(sc->cur_scp)) {
	return 0;
    }

    sc->switch_in_progress = 0;
    if (sc->unit == sc_console_unit && !sc->fb_blanked)
	cons_unavail = FALSE;
    DPRINTF(5, ("switch done\n"));

    return 0;
}

static void
do_switch_scr(sc_softc_t *sc)
{
    lwkt_gettoken(&vga_token);
    vt_proc_alive(sc->new_scp);

    exchange_scr(sc);
    /* sc->cur_scp == sc->new_scp */
    wakeup((caddr_t)&sc->cur_scp->smode);

    /* wait for the controlling process to acknowledge, if necessary */
    if (!signal_vt_acq(sc->cur_scp)) {
	sc->switch_in_progress = 0;
	if (sc->unit == sc_console_unit && !sc->fb_blanked)
	    cons_unavail = FALSE;
    }
    lwkt_reltoken(&vga_token);
}

static int
vt_proc_alive(scr_stat *scp)
{
    lwkt_gettoken(&vga_token);
    if (scp->proc) {
	if (scp->proc == pfindn(scp->pid)) {
	    lwkt_reltoken(&vga_token);
	    return TRUE;
	}
	scp->proc = NULL;
	scp->smode.mode = VT_AUTO;
	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
    }
    lwkt_reltoken(&vga_token);
    return FALSE;
}

static int
signal_vt_rel(scr_stat *scp)
{
    struct proc *p;

    lwkt_gettoken(&vga_token);
    if (scp->smode.mode != VT_PROCESS) {
        lwkt_reltoken(&vga_token);
	return FALSE;
    }
    scp->status |= SWITCH_WAIT_REL;
    p = scp->proc;
    PHOLD(p);
    ksignal(p, scp->smode.relsig);
    PRELE(p);
    DPRINTF(5, ("sending relsig to %d\n", scp->pid));
    lwkt_reltoken(&vga_token);

    return TRUE;
}

static int
signal_vt_acq(scr_stat *scp)
{
    struct proc *p;

    lwkt_gettoken(&vga_token);
    if (scp->smode.mode != VT_PROCESS) {
        lwkt_reltoken(&vga_token);
	return FALSE;
    }
    if (scp->sc->unit == sc_console_unit)
	cons_unavail = TRUE;
    scp->status |= SWITCH_WAIT_ACQ;
    p = scp->proc;
    PHOLD(p);
    ksignal(p, scp->smode.acqsig);
    PRELE(p);
    DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
    lwkt_reltoken(&vga_token);

    return TRUE;
}

static int
finish_vt_rel(scr_stat *scp, int release)
{
    lwkt_gettoken(&vga_token);
    if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
	scp->status &= ~SWITCH_WAIT_REL;
	if (release)
	    do_switch_scr(scp->sc);
	else
	    scp->sc->switch_in_progress = 0;
	lwkt_reltoken(&vga_token);
	return 0;
    }
    lwkt_reltoken(&vga_token);
    return EINVAL;
}

static int
finish_vt_acq(scr_stat *scp)
{
    lwkt_gettoken(&vga_token);
    if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
	scp->status &= ~SWITCH_WAIT_ACQ;
	scp->sc->switch_in_progress = 0;
	lwkt_reltoken(&vga_token);
	return 0;
    }
    lwkt_reltoken(&vga_token);
    return EINVAL;
}

static void
exchange_scr(sc_softc_t *sc)
{
    scr_stat *scp;

    lwkt_gettoken(&vga_token);
    /* save the current state of video and keyboard */
    sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
    if (!ISGRAPHSC(sc->old_scp))
	sc_remove_cursor_image(sc->old_scp);
    if (sc->old_scp->kbd_mode == K_XLATE)
	save_kbd_state(sc->old_scp, TRUE);

    /* set up the video for the new screen */
    scp = sc->cur_scp = sc->new_scp;
    if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
	set_mode(scp);
    else if (sc->adp != NULL)
	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
		    (void *)sc->adp->va_window, FALSE);
    scp->status |= MOUSE_HIDDEN;
    sc_update_render(scp);     /* Switch to kms renderer if necessary */
    sc_move_cursor(scp, scp->xpos, scp->ypos);
    if (!ISGRAPHSC(scp))
	sc_set_cursor_image(scp);
    if (sc->fbi == NULL && ISGRAPHSC(sc->old_scp))
	load_palette(sc->adp, sc->palette);
    if (!ISGRAPHSC(scp) || sc->fbi == NULL)
	sc_set_border(scp, scp->border);

    /* set up the keyboard for the new screen */
    if (sc->old_scp->kbd_mode != scp->kbd_mode)
	kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
    update_kbd_state(scp, scp->status, LOCK_MASK, TRUE);

    mark_all(scp);

    /*
     * Restore DRM framebuffer console mode if necessary.
     * Especially avoid modesetting when switching to a vt which is in
     * graphics mode, because the fb_set_par() from the taskqueue thread
     * might race with the modesetting ioctl from the userland application.
     */
    if (!ISTEXTSC(sc->old_scp) &&
	sc->fbi != NULL && sc->fbi->fbops.fb_set_par != NULL &&
	sc->fb_set_par_task != NULL) {
	taskqueue_enqueue(taskqueue_thread[0], sc->fb_set_par_task);
    }

    lwkt_reltoken(&vga_token);
}

static void
sc_puts(scr_stat *scp, u_char *buf, int len)
{
#if NSPLASH > 0
    /* make screensaver happy */
    if (!sticky_splash && scp == scp->sc->cur_scp)
	run_scrn_saver = FALSE;
#endif

    if (scp->tsw)
	(*scp->tsw->te_puts)(scp, buf, len);

    if (scp->sc->delayed_next_scr)
	sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);

}

void
sc_draw_cursor_image(scr_stat *scp)
{
    /* assert(scp == scp->sc->cur_scp); */
    atomic_add_int(&scp->sc->videoio_in_progress, 1);
    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
			      scp->sc->flags & SC_BLINK_CURSOR, TRUE,
			      sc_inside_cutmark(scp, scp->cursor_pos));
    scp->cursor_oldpos = scp->cursor_pos;
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
}

void
sc_remove_cursor_image(scr_stat *scp)
{
    /* assert(scp == scp->sc->cur_scp); */
    atomic_add_int(&scp->sc->videoio_in_progress, 1);
    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
			      scp->sc->flags & SC_BLINK_CURSOR, FALSE,
			      sc_inside_cutmark(scp, scp->cursor_oldpos));
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
}

static void
update_cursor_image(scr_stat *scp)
{
    int blink;

    if (scp->sc->flags & SC_CHAR_CURSOR) {
	scp->cursor_base = imax(0, scp->sc->cursor_base);
	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
    } else {
	scp->cursor_base = 0;
	scp->cursor_height = scp->font_height;
    }
    blink = scp->sc->flags & SC_BLINK_CURSOR;

    /* assert(scp == scp->sc->cur_scp); */
    atomic_add_int(&scp->sc->videoio_in_progress, 1);
    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE, 
			      sc_inside_cutmark(scp, scp->cursor_pos));
    (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE, 
			      sc_inside_cutmark(scp, scp->cursor_pos));
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
}

void
sc_set_cursor_image(scr_stat *scp)
{
    if (scp->sc->flags & SC_CHAR_CURSOR) {
	scp->cursor_base = imax(0, scp->sc->cursor_base);
	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_height);
    } else {
	scp->cursor_base = 0;
	scp->cursor_height = scp->font_height;
    }

    /* assert(scp == scp->sc->cur_scp); */
    atomic_add_int(&scp->sc->videoio_in_progress, 1);
    (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
			     scp->sc->flags & SC_BLINK_CURSOR);
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
}

static void
scinit(int unit, int flags)
{
    /*
     * When syscons is being initialized as the kernel console, malloc()
     * is not yet functional, because various kernel structures has not been
     * fully initialized yet.  Therefore, we need to declare the following
     * static buffers for the console.  This is less than ideal, 
     * but is necessry evil for the time being.  XXX
     */
    static scr_stat main_console;
    static u_short sc_buffer[2*ROW*2*COL];	/* XXX */
#ifndef SC_NO_FONT_LOADING
    static u_char font_8[256*8];
    static u_char font_14[256*14];
    static u_char font_16[256*16];
#endif

    sc_softc_t *sc;
    scr_stat *scp;
    video_adapter_t *adp;
    char tbuf[MAKEDEV_MINNBUF];
    int col;
    int row;
    int i;

    /* one time initialization */
    if (init_done == COLD)
	sc_get_bios_values(&bios_value);
    init_done = WARM;

    /*
     * Allocate resources.  Even if we are being called for the second
     * time, we must allocate them again, because they might have 
     * disappeared...
     */
    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
    adp = NULL;
    if (flags & SC_EFI_FB) {
	/* Don't replace already registered drm framebuffer here */
	if (sc->fbi == NULL) {
	    sc->fbi = &efi_fb_info;
	    sc->fbi_generation++;
	}
    } else if (sc->adapter >= 0) {
	vid_release(sc->adp, (void *)&sc->adapter);
	adp = sc->adp;
    }
    sc->adp = NULL;
    if (sc->keyboard >= 0) {
	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
	if (sc->kbd != NULL) {
	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
	}
	sc->kbd = NULL;
    }
    if (!(flags & SC_EFI_FB)) {
	sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
	sc->adp = vid_get_adapter(sc->adapter);
	/* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
    }
    sc->keyboard = sc_allocate_keyboard(sc, unit);
    DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
    sc->kbd = kbd_get_keyboard(sc->keyboard);
    if (sc->kbd != NULL) {
	DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
    }

    if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {

	if (sc->fbi != NULL)
		sc->initial_mode = 0;
	else
		sc->initial_mode = sc->adp->va_initial_mode;

#ifndef SC_NO_FONT_LOADING
	if (flags & SC_KERNEL_CONSOLE) {
	    sc->font_8 = font_8;
	    sc->font_14 = font_14;
	    sc->font_16 = font_16;
	} else if (sc->font_8 == NULL) {
	    /* assert(sc_malloc) */
	    sc->font_8 = kmalloc(sizeof(font_8), M_SYSCONS, M_WAITOK);
	    sc->font_14 = kmalloc(sizeof(font_14), M_SYSCONS, M_WAITOK);
	    sc->font_16 = kmalloc(sizeof(font_16), M_SYSCONS, M_WAITOK);
	}
#endif

	if (sc->fbi != NULL) {
	    col = 0;
	    row = 0;
	} else {
	    lwkt_gettoken(&vga_token);
	    /* extract the hw cursor location and hide the cursor for now */
	    (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
	    (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
	    lwkt_reltoken(&vga_token);
	}

	/* set up the first console */
	sc->first_vty = unit*MAXCONS;
	sc->vtys = MAXCONS;		/* XXX: should be configurable */
	if (flags & SC_KERNEL_CONSOLE) {
	    scp = &main_console;
	    sc->console_scp = scp;
	    init_scp(sc, sc->first_vty, scp);
	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
			(void *)sc_buffer, FALSE);
	    if (sc_init_emulator(scp, SC_DFLT_TERM))
		sc_init_emulator(scp, "*");
	    (*scp->tsw->te_default_attr)(scp,
					 kernel_default.std_color,
					 kernel_default.rev_color);
	} else {
	    /* assert(sc_malloc) */
	    sc->dev = kmalloc(sizeof(cdev_t)*sc->vtys, M_SYSCONS, M_WAITOK | M_ZERO);

	    sc->dev[0] = make_dev(&sc_ops, unit*MAXCONS, UID_ROOT,
				  GID_WHEEL, 0600, "ttyv%s",
				  makedev_unit_b32(tbuf, unit * MAXCONS));

	    ttymalloc(&sc->dev[0]->si_tty);
	    scp = alloc_scp(sc, sc->first_vty);
	    sc->dev[0]->si_drv1 = scp;
	}
	sc->cur_scp = scp;

	/* copy screen to temporary buffer */
	if (scp->sc->fbi == NULL) {
	    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
			(void *)scp->sc->adp->va_window, FALSE);
	    if (ISTEXTSC(scp))
		sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
	}

	/* move cursors to the initial positions */
	if (col >= scp->xsize)
	    col = 0;
	if (row >= scp->ysize)
	    row = scp->ysize - 1;
	scp->xpos = col;
	scp->ypos = row;
	if (sc->fbi != NULL) {
	    scp->cursor_pos = 0;
	    sc->cursor_base = 0;
	    sc->cursor_height = scp->font_height;
	} else {
	    scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
	    if (bios_value.cursor_end < scp->font_height)
		sc->cursor_base = scp->font_height - bios_value.cursor_end - 1;
	    else
		sc->cursor_base = 0;
	    i = bios_value.cursor_end - bios_value.cursor_start + 1;
	    sc->cursor_height = imin(i, scp->font_height);
	}
#ifndef SC_NO_SYSMOUSE
	sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
#endif
	if (!ISGRAPHSC(scp)) {
    	    sc_set_cursor_image(scp);
    	    sc_draw_cursor_image(scp);
	    /* If framebuffer vaddr is still 0, we can't draw the border yet */
	    if (scp->sc->fbi != NULL && scp->sc->fbi->vaddr != 0) {
		sc_filled_border = TRUE;
		sc_set_border(scp, scp->border);
	    }
	}

	/* save font and palette */
#ifndef SC_NO_FONT_LOADING
	sc->fonts_loaded = 0;
#ifdef SC_DFLT_FONT
	bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
	bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
	bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
	sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
#endif /* SC_DFLT_FONT */
	if (sc->adp != NULL && ISFONTAVAIL(sc->adp->va_flags)) {
#ifdef SC_DFLT_FONT
	    if (scp->font_height < 14) {
		sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
	    } else if (scp->font_height >= 16) {
		sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
	    } else {
		sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
	    }
#else /* !SC_DFLT_FONT */
	    if (scp->font_height < 14) {
		sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
		sc->fonts_loaded = FONT_8;
	    } else if (scp->font_height >= 16) {
		sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
		sc->fonts_loaded = FONT_16;
	    } else {
		sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
		sc->fonts_loaded = FONT_14;
	    }
#endif /* SC_DFLT_FONT */
	    /* FONT KLUDGE: always use the font page #0. XXX */
	    sc_show_font(scp, 0);
	}
#endif /* !SC_NO_FONT_LOADING */

	if (!(flags & SC_EFI_FB))
	    save_palette(sc->adp, sc->palette);

#if NSPLASH > 0
	if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
	    /* we are ready to put up the splash image! */
	    splash_init(sc->adp, scsplash_callback, sc);
	    sc->flags |= SC_SPLASH_SCRN;
	}
#endif /* NSPLASH */
    }

    /* the rest is not necessary, if we have done it once */
    if (sc->flags & SC_INIT_DONE) {
	return;
    }

    /* initialize mapscrn arrays to a one to one map */
    for (i = 0; i < sizeof(sc->scr_map); i++)
	sc->scr_map[i] = sc->scr_rmap[i] = i;

    sc->flags |= SC_INIT_DONE;
}

#if 0
static void
scterm(int unit, int flags)
{
    sc_softc_t *sc;
    scr_stat *scp;

    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
    if (sc == NULL)
	return;			/* shouldn't happen */

    lwkt_gettoken(&vga_token);
#if NSPLASH > 0
    /* this console is no longer available for the splash screen */
    if (sc->flags & SC_SPLASH_SCRN) {
	splash_term(sc->adp);
	sc->flags &= ~SC_SPLASH_SCRN;
    }
#endif /* NSPLASH */

#if 0 /* XXX */
    /* move the hardware cursor to the upper-left corner */
    (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
#endif

    /* release the keyboard and the video card */
    if (sc->keyboard >= 0)
	kbd_release(sc->kbd, &sc->keyboard);
    if (sc->adapter >= 0)
	vid_release(sc->adp, &sc->adapter);

    /* 
     * Stop the terminal emulator, if any.  If operating on the
     * kernel console sc->dev may not be setup yet.
     */
    if (flags & SC_KERNEL_CONSOLE)
	scp = sc->console_scp;
    else
	scp = SC_STAT(sc->dev[0]);
    if (scp->tsw)
	(*scp->tsw->te_term)(scp, &scp->ts);
    if (scp->ts != NULL)
	kfree(scp->ts, M_SYSCONS);

    /* clear the structure */
    if (!(flags & SC_KERNEL_CONSOLE)) {
	/* XXX: We need delete_dev() for this */
	kfree(sc->dev, M_SYSCONS);
#if 0
	/* XXX: We need a ttyunregister for this */
	/* XXX: do not race tty token access */
	kfree(sc->tty, M_SYSCONS);
#endif
#ifndef SC_NO_FONT_LOADING
	kfree(sc->font_8, M_SYSCONS);
	kfree(sc->font_14, M_SYSCONS);
	kfree(sc->font_16, M_SYSCONS);
#endif
	/* XXX vtb, history */
    }
    bzero(sc, sizeof(*sc));
    sc->keyboard = -1;
    sc->adapter = -1;
    lwkt_reltoken(&vga_token);
}
#endif

static void
scshutdown(void *arg, int howto)
{
    /* assert(sc_console != NULL) */

    lwkt_gettoken(&vga_token);
    syscons_lock();
    sc_touch_scrn_saver();
    if (!cold && sc_console
	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO 
	&& sc_console->smode.mode == VT_AUTO) {
	sc_switch_scr(sc_console->sc, sc_console->index);
    }
    shutdown_in_progress = TRUE;
    syscons_unlock();
    lwkt_reltoken(&vga_token);
}

int
sc_clean_up(scr_stat *scp, int need_unlock)
{
#if NSPLASH > 0
    int error;
#endif /* NSPLASH */

    lwkt_gettoken(&vga_token);
    if (scp->sc->flags & SC_SCRN_BLANKED) {
	sc_touch_scrn_saver();
#if NSPLASH > 0
	if ((error = wait_scrn_saver_stop(scp->sc, need_unlock))) {
	    lwkt_reltoken(&vga_token);
	    return error;
	}
#endif /* NSPLASH */
    }
    scp->status |= MOUSE_HIDDEN;
    sc_remove_mouse_image(scp);
    sc_remove_cutmarking(scp);
    lwkt_reltoken(&vga_token);
    return 0;
}

void
sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
{
    sc_vtb_t new;
    sc_vtb_t old;

    lwkt_gettoken(&vga_token);
    old = scp->vtb;
    sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
    if (!discard && (old.vtb_flags & VTB_VALID)) {
	/* retain the current cursor position and buffer contants */
	scp->cursor_oldpos = scp->cursor_pos;
	/* 
	 * This works only if the old buffer has the same size as or larger 
	 * than the new one. XXX
	 */
	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
	scp->vtb = new;
    } else {
	scp->vtb = new;
	sc_vtb_destroy(&old);
    }

#ifndef SC_NO_SYSMOUSE
    /* move the mouse cursor at the center of the screen */
    sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
#endif
    lwkt_reltoken(&vga_token);
}

static scr_stat *
alloc_scp(sc_softc_t *sc, int vty)
{
    scr_stat *scp;

    /* assert(sc_malloc) */

    scp = kmalloc(sizeof(scr_stat), M_SYSCONS, M_WAITOK);
    init_scp(sc, vty, scp);

    sc_alloc_scr_buffer(scp, TRUE, TRUE);
    if (sc_init_emulator(scp, SC_DFLT_TERM))
	sc_init_emulator(scp, "*");

#ifndef SC_NO_CUTPASTE
    sc_alloc_cut_buffer(scp, TRUE);
#endif

#ifndef SC_NO_HISTORY
    sc_alloc_history_buffer(scp, 0, 0, TRUE);
#endif
    return scp;
}

/*
 * NOTE: Must be called with vga_token held.
 */
static void
init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
{
    video_info_t info;

    bzero(scp, sizeof(*scp));

    scp->index = vty;
    scp->sc = sc;
    scp->status = 0;
    scp->mode = sc->initial_mode;
    scp->fbi_generation = sc->fbi_generation;
    callout_init_lk(&scp->blink_screen_ch, &syscons_blink_lk);
    if (scp->sc->fbi == NULL) {
	lwkt_gettoken(&vga_token);
	(*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
	lwkt_reltoken(&vga_token);
    }
    if (scp->sc->fbi != NULL) {
	scp->xpixel = sc->fbi->width;
	scp->ypixel = sc->fbi->height;
	scp->font_width = 8;
	scp->font_height = 16;

	/* The first vty uses a statically allocated 160x50 buffer */
	if (vty == sc->first_vty)
		sc_font_scale(scp, 2*COL, 2*ROW);
	else
		sc_font_scale(scp, 0, 0);

#ifndef SC_NO_FONT_LOADING
	scp->font = sc->font_16;
#else
	scp->font = NULL;
#endif
    } else if (info.vi_flags & V_INFO_GRAPHICS) {
	scp->status |= GRAPHICS_MODE;
	scp->xpixel = info.vi_width;
	scp->ypixel = info.vi_height;
	scp->xsize = info.vi_width/8;
	scp->ysize = info.vi_height/info.vi_cheight;
	scp->font_height = 0;
	scp->font_width = 0;
	scp->font = NULL;
    } else {
	scp->xsize = info.vi_width;
	scp->ysize = info.vi_height;
	scp->xpixel = scp->xsize*8;
	scp->ypixel = scp->ysize*info.vi_cheight;
	scp->font_width = 8;
	if (info.vi_cheight < 14) {
	    scp->font_height = 8;
#ifndef SC_NO_FONT_LOADING
	    scp->font = sc->font_8;
#else
	    scp->font = NULL;
#endif
	} else if (info.vi_cheight >= 16) {
	    scp->font_height = 16;
#ifndef SC_NO_FONT_LOADING
	    scp->font = sc->font_16;
#else
	    scp->font = NULL;
#endif
	} else {
	    scp->font_height = 14;
#ifndef SC_NO_FONT_LOADING
	    scp->font = sc->font_14;
#else
	    scp->font = NULL;
#endif
	}
    }
    scp->xoff = scp->yoff = 0;
    scp->xpos = scp->ypos = 0;
    sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
    scp->start = scp->xsize * scp->ysize - 1;
    scp->end = 0;
    scp->tsw = NULL;
    scp->ts = NULL;
    scp->rndr = NULL;
    scp->border = SC_BORDER_COLOR;
    scp->cursor_base = sc->cursor_base;
    scp->cursor_height = imin(sc->cursor_height, scp->font_height);
    scp->mouse_cut_start = scp->xsize * scp->ysize;
    scp->mouse_cut_end = -1;
    scp->mouse_signal = 0;
    scp->mouse_pid = 0;
    scp->mouse_proc = NULL;
    scp->kbd_mode = K_XLATE;
    scp->bell_pitch = bios_value.bell_pitch;
    scp->bell_duration = BELL_DURATION;
    scp->status |= (bios_value.shift_state & NLKED);
    scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
    scp->pid = 0;
    scp->proc = NULL;
    scp->smode.mode = VT_AUTO;
    scp->history = NULL;
    scp->history_pos = 0;
    scp->history_size = 0;
}

int
sc_init_emulator(scr_stat *scp, char *name)
{
    sc_term_sw_t *sw;
    sc_rndr_sw_t *rndr;
    void *p;
    int error;

    if (name == NULL)	/* if no name is given, use the current emulator */
	sw = scp->tsw;
    else		/* ...otherwise find the named emulator */
	sw = sc_term_match(name);
    if (sw == NULL) {
	return EINVAL;
    }

    rndr = NULL;
    if (strcmp(sw->te_renderer, "*") != 0) {
	rndr = sc_render_match(scp, sw->te_renderer, scp->model);
    }
    if (rndr == NULL && scp->sc->fbi != NULL) {
	rndr = sc_render_match(scp, "kms", scp->model);
    }
    if (rndr == NULL) {
	rndr = sc_render_match(scp, scp->sc->adp->va_name, scp->model);
	if (rndr == NULL) {
	    return ENODEV;
	}
    }

    if (sw == scp->tsw) {
	error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
	scp->rndr = rndr;
	sc_clear_screen(scp);
	/* assert(error == 0); */
	return error;
    }

    if (sc_malloc && (sw->te_size > 0))
	p = kmalloc(sw->te_size, M_SYSCONS, M_NOWAIT);
    else
	p = NULL;
    error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
    if (error) {
	return error;
    }

    if (scp->tsw)
	(*scp->tsw->te_term)(scp, &scp->ts);
    if (scp->ts != NULL)
	kfree(scp->ts, M_SYSCONS);
    scp->tsw = sw;
    scp->ts = p;
    scp->rndr = rndr;

    /* XXX */
    (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
    sc_clear_screen(scp);

    return 0;
}

/*
 * scgetc(flags) - get character from keyboard.
 * If flags & SCGETC_CN, then avoid harmful side effects.
 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
 * return NOKEY if there is nothing there.
 */
static u_int
scgetc(sc_softc_t *sc, u_int flags)
{
    scr_stat *scp;
#ifndef SC_NO_HISTORY
    struct tty *tp;
#endif
    u_int c;
    int this_scr;
    int f;
    int i;

    lwkt_gettoken(&vga_token);
    if (sc->kbd == NULL) {
        lwkt_reltoken(&vga_token);
	return NOKEY;
    }

next_code:
#if 1
    /* I don't like this, but... XXX */
    if (flags & SCGETC_CN) {
	syscons_lock();
	sccnupdate(sc->cur_scp);
	syscons_unlock();
    }
#endif
    scp = sc->cur_scp;
    /* first see if there is something in the keyboard port */
    for (;;) {
	c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
	if (c == ERRKEY) {
	    if (!(flags & SCGETC_CN))
		sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
	} else if (c == NOKEY) {
	    lwkt_reltoken(&vga_token);
	    return c;
	} else {
	    break;
	}
    }

    /* make screensaver happy */
    if (!(c & RELKEY))
	sc_touch_scrn_saver();

    if (!(flags & SCGETC_CN))
	/* do the /dev/random device a favour */
	add_keyboard_randomness(c);

    if (scp->kbd_mode != K_XLATE) {
        lwkt_reltoken(&vga_token);
	return KEYCHAR(c);
    }

    /* if scroll-lock pressed allow history browsing */
    if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {

	scp->status &= ~CURSOR_ENABLED;
	sc_remove_cursor_image(scp);

#ifndef SC_NO_HISTORY
	if (!(scp->status & BUFFER_SAVED)) {
	    scp->status |= BUFFER_SAVED;
	    sc_hist_save(scp);
	}
	switch (c) {
	/* FIXME: key codes */
	case SPCLKEY | FKEY | F(49):  /* home key */
	    sc_remove_cutmarking(scp);
	    sc_hist_home(scp);
	    goto next_code;

	case SPCLKEY | FKEY | F(57):  /* end key */
	    sc_remove_cutmarking(scp);
	    sc_hist_end(scp);
	    goto next_code;

	case SPCLKEY | FKEY | F(50):  /* up arrow key */
	    sc_remove_cutmarking(scp);
	    if (sc_hist_up_line(scp))
		if (!(flags & SCGETC_CN))
		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
	    goto next_code;

	case SPCLKEY | FKEY | F(58):  /* down arrow key */
	    sc_remove_cutmarking(scp);
	    if (sc_hist_down_line(scp))
		if (!(flags & SCGETC_CN))
		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
	    goto next_code;

	case SPCLKEY | FKEY | F(51):  /* page up key */
	    sc_remove_cutmarking(scp);
	    for (i=0; i<scp->ysize; i++) {
		if (sc_hist_up_line(scp)) {
		    if (!(flags & SCGETC_CN))
			sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
		    break;
		}
	    }
	    goto next_code;

	case SPCLKEY | FKEY | F(59):  /* page down key */
	    sc_remove_cutmarking(scp);
	    for (i=0; i<scp->ysize; i++) {
		if (sc_hist_down_line(scp)) {
		    if (!(flags & SCGETC_CN))
			sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
		    break;
		}
	    }
	    goto next_code;
	}
#endif /* SC_NO_HISTORY */
    }

    /* 
     * Process and consume special keys here.  Return a plain char code
     * or a char code with the META flag or a function key code.
     */
    if (c & RELKEY) {
	/* key released */
	/* goto next_code */
    } else {
	/* key pressed */
	if (c & SPCLKEY) {
	    c &= ~SPCLKEY;
	    switch (KEYCHAR(c)) {
	    /* LOCKING KEYS */
	    case NLK: case CLK: case ALK:
		break;
	    case SLK:
		kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
		if (f & SLKED) {
		    scp->status |= SLKED;
		} else {
		    if (scp->status & SLKED) {
			scp->status &= ~SLKED;
#ifndef SC_NO_HISTORY
			if (scp->status & BUFFER_SAVED) {
			    if (!sc_hist_restore(scp))
				sc_remove_cutmarking(scp);
			    scp->status &= ~BUFFER_SAVED;
			    scp->status |= CURSOR_ENABLED;
			    sc_draw_cursor_image(scp);
			}
			tp = VIRTUAL_TTY(sc, scp->index);
			if (ISTTYOPEN(tp))
			    scstart(tp);
#endif
		    }
		}
		break;

	    /* NON-LOCKING KEYS */
	    case NOP:
	    case LSH:  case RSH:  case LCTR: case RCTR:
	    case LALT: case RALT: case ASH:  case META:
		break;

	    case BTAB:
		if (!(sc->flags & SC_SCRN_BLANKED)) {
                    lwkt_reltoken(&vga_token);
		    return c;
		}
		break;

	    case SPSC:
#if NSPLASH > 0
		/* force activatation/deactivation of the screen saver */
		if (!(sc->flags & SC_SCRN_BLANKED)) {
		    run_scrn_saver = TRUE;
		    sc->scrn_time_stamp -= scrn_blank_time;
		}
		if (cold) {
		    /*
		     * While devices are being probed, the screen saver need
		     * to be invoked explictly. XXX
		     */
		    if (sc->flags & SC_SCRN_BLANKED) {
			scsplash_stick(FALSE);
			stop_scrn_saver(sc, current_saver);
		    } else {
			if (!ISGRAPHSC(scp)) {
			    scsplash_stick(TRUE);
			    (*current_saver)(sc, TRUE);
			}
		    }
		}
#endif /* NSPLASH */
		break;

	    case RBT:
#ifndef SC_DISABLE_REBOOT
		shutdown_nice(0);
#endif
		break;

	    case HALT:
#ifndef SC_DISABLE_REBOOT
		shutdown_nice(RB_HALT);
#endif
		break;

	    case PDWN:
#ifndef SC_DISABLE_REBOOT
		shutdown_nice(RB_HALT|RB_POWEROFF);
#endif
		break;

	    case SUSP:
	    case STBY:
		break;

	    case DBG:
#ifndef SC_DISABLE_DDBKEY
#ifdef DDB
		lwkt_reltoken(&vga_token);
		Debugger("manual escape to debugger");
		lwkt_gettoken(&vga_token);
#else
		kprintf("No debugger in kernel\n");
#endif
#else /* SC_DISABLE_DDBKEY */
		/* do nothing */
#endif /* SC_DISABLE_DDBKEY */
		break;

	    case PNC:
		if (enable_panic_key)
			panic("Forced by the panic key");
		break;

	    case NEXT:
		this_scr = scp->index;
		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
			sc->first_vty + i != this_scr; 
			i = (i + 1)%sc->vtys) {
		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
		    if (ISTTYOPEN(tp)) {
			syscons_lock();
			sc_switch_scr(scp->sc, sc->first_vty + i);
			syscons_unlock();
			break;
		    }
		}
		break;

	    case PREV:
		this_scr = scp->index;
		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
			sc->first_vty + i != this_scr;
			i = (i + sc->vtys - 1)%sc->vtys) {
		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
		    if (ISTTYOPEN(tp)) {
			syscons_lock();
			sc_switch_scr(scp->sc, sc->first_vty + i);
			syscons_unlock();
			break;
		    }
		}
		break;

	    default:
		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
		    syscons_lock();
		    sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
		    syscons_unlock();
		    break;
		}
		/* assert(c & FKEY) */
		if (!(sc->flags & SC_SCRN_BLANKED)) {
		    lwkt_reltoken(&vga_token);
		    return c;
		}
		break;
	    }
	    /* goto next_code */
	} else {
	    /* regular keys (maybe MKEY is set) */
	    if (!(sc->flags & SC_SCRN_BLANKED)) {
		lwkt_reltoken(&vga_token);
		return c;
	    }
	}
    }

    goto next_code;
}

static int
scmmap(struct dev_mmap_args *ap)
{
    scr_stat *scp;

    lwkt_gettoken(&vga_token);
    scp = SC_STAT(ap->a_head.a_dev);
    if (scp != scp->sc->cur_scp) {
        lwkt_reltoken(&vga_token);
	return EINVAL;
    }
    if (scp->sc->fbi != NULL) {
	size_t sz =
	    roundup(scp->sc->fbi->height * scp->sc->fbi->stride, PAGE_SIZE);
	if (ap->a_offset > sz - PAGE_SIZE) {
	    lwkt_reltoken(&vga_token);
	    return EINVAL;
	} else {
	    /* This works for amdgpu(4) as well. */
	    ap->a_result = atop(vtophys(scp->sc->fbi->vaddr + ap->a_offset));
	}
    } else {
	ap->a_result = (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp,
							ap->a_offset,
							ap->a_nprot);
    }
    lwkt_reltoken(&vga_token);
    return(0);
}

static int
save_kbd_state(scr_stat *scp, int unlock)
{
    int state;
    int error;

    WANT_UNLOCK(unlock);
    error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
    WANT_LOCK(unlock);

    if (error == ENOIOCTL)
	error = ENODEV;
    if (error == 0) {
	scp->status &= ~LOCK_MASK;
	scp->status |= state;
    }
    return error;
}

static int
update_kbd_state(scr_stat *scp, int new_bits, int mask, int unlock)
{
    int state;
    int error;

    if (mask != LOCK_MASK) {
	WANT_UNLOCK(unlock);
	error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
	WANT_LOCK(unlock);

	if (error == ENOIOCTL)
	    error = ENODEV;
	if (error) {
	    return error;
	}
	state &= ~mask;
	state |= new_bits & mask;
    } else {
	state = new_bits & LOCK_MASK;
    }
    WANT_UNLOCK(unlock);
    error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
    WANT_LOCK(unlock);
    if (error == ENOIOCTL)
	error = ENODEV;
    return error;
}

static int
update_kbd_leds(scr_stat *scp, int which)
{
    int error;

    which &= LOCK_MASK;
    error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
    if (error == ENOIOCTL)
	error = ENODEV;
    return error;
}

int
set_mode(scr_stat *scp)
{
    video_info_t info;

    lwkt_gettoken(&vga_token);
    /* reject unsupported mode */
    if (scp->sc->fbi == NULL && (*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info)) {
        lwkt_reltoken(&vga_token);
	return 1;
    }

    /* if this vty is not currently showing, do nothing */
    if (scp != scp->sc->cur_scp) {
        lwkt_reltoken(&vga_token);
	return 0;
    }

    /* setup video hardware for the given mode */
    if (scp->sc->fbi == NULL) {
	(*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
		    (void *)scp->sc->adp->va_window, FALSE);
    } else {
	goto done;
    }

#ifndef SC_NO_FONT_LOADING
    /* load appropriate font */
    if (!(scp->status & GRAPHICS_MODE)) {
	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
	    if (scp->font_height < 14) {
		if (scp->sc->fonts_loaded & FONT_8)
		    sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
	    } else if (scp->font_height >= 16) {
		if (scp->sc->fonts_loaded & FONT_16)
		    sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
	    } else {
		if (scp->sc->fonts_loaded & FONT_14)
		    sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
	    }
	    /*
	     * FONT KLUDGE:
	     * This is an interim kludge to display correct font.
	     * Always use the font page #0 on the video plane 2.
	     * Somehow we cannot show the font in other font pages on
	     * some video cards... XXX
	     */ 
	    sc_show_font(scp, 0);
	}
	mark_all(scp);
    }
#endif /* !SC_NO_FONT_LOADING */

    sc_set_border(scp, scp->border);
    sc_set_cursor_image(scp);

done:
    lwkt_reltoken(&vga_token);
    return 0;
}

void
refresh_ega_palette(scr_stat *scp)
{
    uint32_t r, g, b;
    int reg;
    int rsize, gsize, bsize;
    int rfld, gfld, bfld;
    int i;

    rsize = scp->sc->adp->va_info.vi_pixel_fsizes[0];
    gsize = scp->sc->adp->va_info.vi_pixel_fsizes[1];
    bsize = scp->sc->adp->va_info.vi_pixel_fsizes[2];
    rfld = scp->sc->adp->va_info.vi_pixel_fields[0];
    gfld = scp->sc->adp->va_info.vi_pixel_fields[1];
    bfld = scp->sc->adp->va_info.vi_pixel_fields[2];

    for (i = 0; i < 16; i++) {
	reg = scp->sc->adp->va_palette_regs[i];

	r = scp->sc->palette[reg * 3] >> (8 - rsize);
	g = scp->sc->palette[reg * 3 + 1] >> (8 - gsize);
	b = scp->sc->palette[reg * 3 + 2] >> (8 - bsize);

	scp->ega_palette[i] = (r << rfld) + (g << gfld) + (b << bfld);
    }
}

void
sc_set_border(scr_stat *scp, int color)
{
    atomic_add_int(&scp->sc->videoio_in_progress, 1);
    (*scp->rndr->draw_border)(scp, color);
    atomic_add_int(&scp->sc->videoio_in_progress, -1);
}

#ifndef SC_NO_FONT_LOADING
void
sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
	     int base, int count)
{
    sc_softc_t *sc;

    sc = scp->sc;
    sc->font_loading_in_progress = TRUE;
    (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
    sc->font_loading_in_progress = FALSE;
}

void
sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
	     int base, int count)
{
    sc_softc_t *sc;

    sc = scp->sc;
    sc->font_loading_in_progress = TRUE;
    (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
    sc->font_loading_in_progress = FALSE;
}

void
sc_show_font(scr_stat *scp, int page)
{
    (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
}
#endif /* !SC_NO_FONT_LOADING */

void
sc_paste(scr_stat *scp, u_char *p, int count) 
{
    struct tty *tp;
    u_char *rmap;

    /*
     * Holy hell, don't try to inject a paste buffer if the keyboard
     * is not in ascii mode!
     */
    if (scp->kbd_mode != K_XLATE)
	return;

    lwkt_gettoken(&vga_token);
    if (scp->status & MOUSE_VISIBLE) {
	tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
	lwkt_gettoken(&tp->t_token);
	if (!ISTTYOPEN(tp)) {
	    lwkt_reltoken(&tp->t_token);
	    lwkt_reltoken(&vga_token);
	    return;
	}
	rmap = scp->sc->scr_rmap;
	for (; count > 0; --count)
	    (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
	lwkt_reltoken(&tp->t_token);
    }
    lwkt_reltoken(&vga_token);
}

void
sc_bell(scr_stat *scp, int pitch, int duration)
{
    if (cold || shutdown_in_progress || !enable_bell)
	return;

    if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL)) {
	return;
    }

    if (scp->sc->flags & SC_VISUAL_BELL) {
	if (scp->sc->blink_in_progress) {
	    return;
	}
	scp->sc->blink_in_progress = 3;
	if (scp != scp->sc->cur_scp)
	    scp->sc->blink_in_progress += 2;
	lockmgr(&syscons_blink_lk, LK_EXCLUSIVE);
	sc_blink_screen(scp->sc->cur_scp);
	lockmgr(&syscons_blink_lk, LK_RELEASE);
    } else if (duration != 0 && pitch != 0) {
	if (scp != scp->sc->cur_scp)
	    pitch *= 2;
	sysbeep(pitch, duration);
    }
}

/*
 * Blink the screen, called with the syscons_mtx and syscons_blink_lk
 * held.  Can be called directly or via the callout.
 */
static void
sc_blink_screen(scr_stat *scp)
{
    if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
	scp->sc->blink_in_progress = 0;
	mark_all(scp);
	if (scp->sc->delayed_next_scr)
	    sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
    } else {
	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
			   scp->sc->blink_in_progress & 1);
	scp->sc->blink_in_progress--;
	callout_reset(&scp->blink_screen_ch, hz / 10,
		      blink_screen_callout, scp);
    }
}

/*
 * Screen blink callout.  Note that there is a lock order
 * reversal between syscons_blink_lk and the syscons lock.
 * Acquire the syscons lock non-blocking.
 */
static void
blink_screen_callout(void *arg)
{
    scr_stat *scp = arg;

    if (syscons_lock_nonblock() == 0) {
	sc_blink_screen(scp);
	syscons_unlock();
    } else {
	callout_reset(&scp->blink_screen_ch, hz / 10,
		      blink_screen_callout, scp);
    }
}

/*
 * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
 * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
 * any keyboard.
 */

static int
sc_allocate_keyboard(sc_softc_t *sc, int unit)
{
	int		 idx0, idx;
	keyboard_t	*k0, *k;
	keyboard_info_t	 ki;

	idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
	if (idx0 != -1) {
		k0 = kbd_get_keyboard(idx0);

		for (idx = kbd_find_keyboard2("*", -1, 0, 0);
		     idx != -1;
		     idx = kbd_find_keyboard2("*", -1, idx + 1, 0)) {
			k = kbd_get_keyboard(idx);

			if (idx == idx0 || KBD_IS_BUSY(k))
				continue;

			bzero(&ki, sizeof(ki));
			strcpy(ki.kb_name, k->kb_name);
			ki.kb_unit = k->kb_unit;

			kbd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
		}
	} else
		idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);

	return (idx0);
}