summaryrefslogtreecommitdiffstats
path: root/sys/isa/videoio.c
blob: c5b84c60f6b90589b6b7da6b7f5d05d1782dc8c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
/*-
 * Copyright (c) 1998 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. 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 REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *
 * $Id$
 * from: i386/isa videoio.c,v 1.1
 */

#include "sc.h"
#include "opt_syscons.h"

#if NSC > 0

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#include <machine/clock.h>
#include <machine/console.h>
#include <machine/md_var.h>
#include <machine/pc/bios.h>

#include <isa/isareg.h>
#include <isa/videoio.h>

/* this should really be in `rtc.h' */
#define RTC_EQUIPMENT           0x14

/* video adapter state buffer */
struct adp_state {
    int			sig;
#define V_STATE_SIG	0x736f6962
    u_char		regs[V_MODE_PARAM_SIZE];
};
typedef struct adp_state adp_state_t;

/* video adapter information */
#define DCC_MONO	0
#define DCC_CGA40	1
#define DCC_CGA80	2
#define DCC_EGAMONO	3
#define DCC_EGA40	4
#define DCC_EGA80	5

/* 
 * NOTE: `va_window' should have a virtual address, but is initialized
 * with a physical address in the following table, as verify_adapter()
 * will perform address conversion at run-time.
 */
static video_adapter_t adapter_init_value[] = {
    { 0, KD_MONO, 0, 	      MONO_BASE,  0xb0000, 32, 32, 0, 0, 0, 7, 0 },
    { 0, KD_CGA, V_ADP_COLOR, COLOR_BASE, 0xb8000, 32, 32, 0, 0, 0, 3, 0 },
    { 0, KD_CGA, V_ADP_COLOR, COLOR_BASE, 0xb8000, 32, 32, 0, 0, 0, 3, 0 },
    { 0, KD_EGA, 0,	      MONO_BASE,  0xb0000, 32, 32, 0, 0, 0, 7, 0 },
    { 0, KD_EGA, V_ADP_COLOR, COLOR_BASE, 0xb8000, 32, 32, 0, 0, 0, 3, 0 },
    { 0, KD_EGA, V_ADP_COLOR, COLOR_BASE, 0xb8000, 32, 32, 0, 0, 0, 3, 0 },
};

static video_adapter_t adapter[V_MAX_ADAPTERS];
static int adapters = 0;

/* VGA function entries */
static vi_init_t		vid_init;
static vi_adapter_t		vid_adapter;
static vi_get_info_t		vid_get_info;
static vi_query_mode_t		vid_query_mode;
static vi_set_mode_t		vid_set_mode;
static vi_save_font_t		vid_save_font;
static vi_load_font_t		vid_load_font;
static vi_show_font_t		vid_show_font;
static vi_save_palette_t	vid_save_palette;
static vi_load_palette_t	vid_load_palette;
static vi_set_border_t		vid_set_border;
static vi_save_state_t		vid_save_state;
static vi_load_state_t		vid_load_state;
static vi_set_win_org_t		vid_set_origin;
static vi_read_hw_cursor_t	vid_read_hw_cursor;
static vi_set_hw_cursor_t	vid_set_hw_cursor;
static vi_diag_t		vid_diag;

struct vidsw biosvidsw = {
	vid_init,	vid_adapter,	vid_get_info,	vid_query_mode,	
	vid_set_mode,	vid_save_font,	vid_load_font,	vid_show_font,
	vid_save_palette,vid_load_palette,vid_set_border,vid_save_state,
	vid_load_state,	vid_set_origin,	vid_read_hw_cursor, vid_set_hw_cursor,
	vid_diag,
};

/* VGA BIOS standard video modes */
#define EOT		(-1)
#define NA		(-2)

static video_info_t bios_vmode[] = {
    /* CGA */
    { M_B40x25,     V_INFO_COLOR, 40, 25, 8,  8, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_C40x25,     V_INFO_COLOR, 40, 25, 8,  8, 4, 1, 0xb8000, 32, 32, 0, 32 },
    { M_B80x25,     V_INFO_COLOR, 80, 25, 8,  8, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_C80x25,     V_INFO_COLOR, 80, 25, 8,  8, 4, 1, 0xb8000, 32, 32, 0, 32 },
    /* EGA */
    { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1, 0xb8000, 32, 32, 0, 32 },
    { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1, 0xb8000, 32, 32, 0, 32 },
    /* VGA */
    { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1, 0xb8000, 32, 32, 0, 32 },
    { M_VGA_M80x25, 0,		  80, 25, 8, 16, 2, 1, 0xb0000, 32, 32, 0, 32 },
    { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1, 0xb8000, 32, 32, 0, 32 },
    /* MDA */
    { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1, 0xb0000, 32, 32, 0, 32 },
    /* EGA */
    { M_ENH_B80x43, V_INFO_COLOR, 80, 43, 8,  8, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8,  8, 4, 1, 0xb8000, 32, 32, 0, 32 },
    /* VGA */
    { M_VGA_M80x30, 0,		  80, 30, 8, 16, 2, 1, 0xb0000, 32, 32, 0, 32 },
    { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1, 0xb8000, 32, 32, 0, 32 },
    { M_VGA_M80x50, 0,		  80, 50, 8,  8, 2, 1, 0xb0000, 32, 32, 0, 32 },
    { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8,  8, 4, 1, 0xb8000, 32, 32, 0, 32 },
    { M_VGA_M80x60, 0,		  80, 60, 8,  8, 2, 1, 0xb0000, 32, 32, 0, 32 },
    { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8,  8, 4, 1, 0xb8000, 32, 32, 0, 32 },
    /* CGA */
    { M_BG320,      V_INFO_COLOR | V_INFO_GRAPHICS,                
      320, 200, 8,  8, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_CG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 
      320, 200, 8,  8, 2, 1, 0xb8000, 32, 32, 0, 32 },
    { M_BG640,      V_INFO_COLOR | V_INFO_GRAPHICS,                
      640, 200, 8,  8, 1, 1, 0xb8000, 32, 32, 0, 32 },
    /* EGA */
    { M_CG320_D,    V_INFO_COLOR | V_INFO_GRAPHICS,
      320, 200, 8,  8, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_CG640_E,    V_INFO_COLOR | V_INFO_GRAPHICS, 
      640, 200, 8,  8, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_EGAMONOAPA, V_INFO_GRAPHICS, 		    
      640, 350, 8, 14, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_ENHMONOAPA2,V_INFO_GRAPHICS, 		    
      640, 350, 8, 14, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_CG640x350,  V_INFO_COLOR | V_INFO_GRAPHICS, 
      640, 350, 8, 14, 2, 2, 0xa0000, 64, 64, 0, 64 },
    { M_ENH_CG640,  V_INFO_COLOR | V_INFO_GRAPHICS, 
      640, 350, 8, 14, 4, 4, 0xa0000, 64, 64, 0, 64 },
    /* VGA */
    { M_BG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS,                
      640, 480, 8, 16, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_CG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 
      640, 480, 8, 16, 4, 4, 0xa0000, 64, 64, 0, 64 },
    { M_VGA_CG320,  V_INFO_COLOR | V_INFO_GRAPHICS, 
      320, 200, 8,  8, 8, 1, 0xa0000, 64, 64, 0, 64 },
    { M_VGA_MODEX,  V_INFO_COLOR | V_INFO_GRAPHICS, 
      320, 240, 8,  8, 8, 1, 0xa0000, 64, 64, 0, 64 },

    { EOT },
};

static int		init_done = FALSE;
static u_char		*video_mode_ptr = NULL;		/* EGA/VGA */
static u_char		*video_mode_ptr2 = NULL;	/* CGA/MDA */
static u_char		*mode_map[V_MODE_MAP_SIZE];
static adp_state_t	adpstate;
static adp_state_t	adpstate2;
static int		rows_offset = 1;

/* local macros and functions */
#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))

static void map_mode_table(u_char *map[], u_char *table, int max);
static void clear_mode_map(int ad, u_char *map[], int max, int color);
static int map_mode_num(int mode);
static int map_bios_mode_num(int type, int color, int bios_mode);
static u_char *get_mode_param(int mode);
static void fill_adapter_param(int code, video_adapter_t *adp);
static int verify_adapter(video_adapter_t *adp);
#define COMP_IDENTICAL	0
#define COMP_SIMILAR	1
#define COMP_DIFFERENT	2
static int comp_adpregs(u_char *buf1, u_char *buf2);

#define PARAM_BUFSIZE	6
static void set_font_mode(video_adapter_t *adp, u_char *buf);
static void set_normal_mode(video_adapter_t *adp, u_char *buf);

static char *adapter_name(int type);
static void dump_adp_info(int ad, video_adapter_t *adp, int level);
static void dump_mode_info(int ad, video_info_t *info, int level);
static void dump_buffer(u_char *buf, size_t len);

extern void generic_bcopy(const void *, void *, size_t);

#define	ISMAPPED(pa, width) \
	(((pa) <= (u_long)0x1000 - (width)) 		\
	 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))

#define	prologue(ad, flag, err)	\
	if (!init_done					\
	    || ((ad) < 0) || ((ad) >= adapters)		\
	    || !(adapter[(ad)].va_flags & (flag)))	\
	    return (err)

/* construct the mode parameter map */
static void
map_mode_table(u_char *map[], u_char *table, int max)
{
    int i;

    for(i = 0; i < max; ++i)
	map[i] = table + i*V_MODE_PARAM_SIZE;
    for(; i < V_MODE_MAP_SIZE; ++i)
	map[i] = NULL;
}

static void
clear_mode_map(int ad, u_char *map[], int max, int color)
{
    video_info_t info;
    int i;

    /*
     * NOTE: we don't touch `bios_vmode[]' because it is shared
     * by all adapters.
     */
    for(i = 0; i < max; ++i) {
	if (vid_get_info(ad, i, &info))
	    continue;
	if ((info.vi_flags & V_INFO_COLOR) != color)
	    map[i] = NULL;
    }
}

/* the non-standard video mode is based on a standard mode... */
static int
map_mode_num(int mode)
{
    static struct {
        int from;
        int to;
    } mode_map[] = {
        { M_ENH_B80x43, M_ENH_B80x25 },
        { M_ENH_C80x43, M_ENH_C80x25 },
        { M_VGA_M80x30, M_VGA_M80x25 },
        { M_VGA_C80x30, M_VGA_C80x25 },
        { M_VGA_M80x50, M_VGA_M80x25 },
        { M_VGA_C80x50, M_VGA_C80x25 },
        { M_VGA_M80x60, M_VGA_M80x25 },
        { M_VGA_C80x60, M_VGA_C80x25 },
        { M_VGA_MODEX,  M_VGA_CG320 },
    };
    int i;

    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
        if (mode_map[i].from == mode)
            return mode_map[i].to;
    }
    return mode;
}

/* turn the BIOS video number into our video mode number */
static int
map_bios_mode_num(int type, int color, int bios_mode)
{
    static int cga_modes[7] = {
	M_B40x25, M_C40x25,		/* 0, 1 */
	M_B80x25, M_C80x25,		/* 2, 3 */
	M_BG320, M_CG320,
	M_BG640,
    };
    static int ega_modes[17] = {
	M_ENH_B40x25, M_ENH_C40x25,	/* 0, 1 */
	M_ENH_B80x25, M_ENH_C80x25,	/* 2, 3 */
	M_BG320, M_CG320,
	M_BG640,
	M_EGAMONO80x25,			/* 7 */
	8, 9, 10, 11, 12,
	M_CG320_D,
	M_CG640_E,
	M_ENHMONOAPA2,			/* XXX: video momery > 64K */
	M_ENH_CG640,			/* XXX: video momery > 64K */
    };
    static int vga_modes[20] = {
	M_VGA_C40x25, M_VGA_C40x25,	/* 0, 1 */
	M_VGA_C80x25, M_VGA_C80x25,	/* 2, 3 */
	M_BG320, M_CG320,
	M_BG640,
	M_VGA_M80x25,			/* 7 */
	8, 9, 10, 11, 12,
	M_CG320_D,
	M_CG640_E,
	M_ENHMONOAPA2,
	M_ENH_CG640,
	M_BG640x480, M_CG640x480, 
	M_VGA_CG320,
    };

    switch (type) {

    case KD_VGA:
	if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
	    return vga_modes[bios_mode];
	else if (color)
	    return M_VGA_C80x25;
	else
	    return M_VGA_M80x25;
	break;

    case KD_EGA:
	if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
	    return ega_modes[bios_mode];
	else if (color)
	    return M_ENH_C80x25;
	else
	    return M_EGAMONO80x25;
	break;

    case KD_CGA:
	if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
	    return cga_modes[bios_mode];
	else
	    return M_C80x25;
	break;

    case KD_MONO:
    case KD_HERCULES:
	return M_EGAMONO80x25;		/* XXX: this name is confusing */

    default:
	break;
    }
    return -1;
}

/* look up a parameter table entry */
static u_char 
*get_mode_param(int mode)
{
    if (mode >= V_MODE_MAP_SIZE)
	mode = map_mode_num(mode);
    if (mode < V_MODE_MAP_SIZE)
	return mode_map[mode];
    else
	return NULL;
}

static void
fill_adapter_param(int code, video_adapter_t *adp)
{
    static struct {
	int primary;
	int secondary;
    } dcc[] = {
	{ DCC_MONO, 			DCC_EGA40 /* CGA monitor */ },
	{ DCC_MONO, 			DCC_EGA80 /* CGA monitor */ },
	{ DCC_MONO, 			DCC_EGA80 /* CGA emulation */ },	
	{ DCC_MONO, 			DCC_EGA80 },
	{ DCC_CGA40, 			DCC_EGAMONO },
	{ DCC_CGA80, 			DCC_EGAMONO },
	{ DCC_EGA40 /* CGA monitor */, 	DCC_MONO},
	{ DCC_EGA80 /* CGA monitor */, 	DCC_MONO},
	{ DCC_EGA80 /* CGA emulation */,DCC_MONO },	
	{ DCC_EGA80, 			DCC_MONO },
	{ DCC_EGAMONO, 			DCC_CGA40 },
	{ DCC_EGAMONO, 			DCC_CGA40 },
    };

    if ((code < 0) || (code >= sizeof(dcc)/sizeof(dcc[0]))) {
	adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
	adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
    } else {
	adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
	adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
    }
}

static int
verify_adapter(video_adapter_t *adp)
{
    u_int32_t buf;
    u_short v;
    u_int32_t p;

    buf = BIOS_PADDRTOVADDR(adp->va_window);
    v = readw(buf);
    writew(buf, 0xA55A);
    if (readw(buf) != 0xA55A)
	return 1;
    writew(buf, v);

    switch (adp->va_type) {

    case KD_EGA:
	outb(adp->va_crtc_addr, 7);
	if (inb(adp->va_crtc_addr) == 7) {
	    adp->va_type = KD_VGA;
	    adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
	}
	adp->va_flags |= V_ADP_STATELOAD | V_ADP_FONT | V_ADP_BORDER;
	/* the color adapter may be in the 40x25 mode... XXX */

#ifdef __i386__
	/* get the BIOS video mode pointer */
	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
	p = BIOS_SADDRTOLADDR(p);
	if (ISMAPPED(p, sizeof(u_int32_t))) {
	    p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
	    p = BIOS_SADDRTOLADDR(p);
	    if (ISMAPPED(p, V_MODE_PARAM_SIZE))
		video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
	}
#endif
	break;

    case KD_CGA:
	adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
	/* may be in the 40x25 mode... XXX */
	break;

    case KD_MONO:
	break;
    }

    return 0;
}

/* compare two parameter table entries */
static int 
comp_adpregs(u_char *buf1, u_char *buf2)
{
    static struct {
        u_char mask;
    } params[V_MODE_PARAM_SIZE] = {
	0xff, 0x00, 0xff, 		/* COLS, ROWS, POINTS */
	0x00, 0x00, 			/* page length */
	0xfe, 0xff, 0xff, 0xff,		/* sequencer registers */
	0xf3,				/* misc register */
	0xff, 0xff, 0xff, 0x7f, 0xff,	/* CRTC */
	0xff, 0xff, 0xff, 0x7f, 0xff,
	0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0xff, 0x7f, 0xff, 0xff,
	0x7f, 0xff, 0xff, 0xef, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff,	/* attribute controller registers */
	0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xf0,
	0xff, 0xff, 0xff, 0xff, 0xff,	/* GDC register */
	0xff, 0xff, 0xff, 0xff, 
    }; 
    int identical = TRUE;
    int i;

    if ((buf1 == NULL) || (buf2 == NULL))
	return COMP_DIFFERENT;

    for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
	if (params[i].mask == 0)	/* don't care */
	    continue;
	if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
	    return COMP_DIFFERENT;
	if (buf1[i] != buf2[i])
	    identical = FALSE;
    }
    return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
}

/* exported functions */

/* all adapters */
static int
vid_init(void)
{
    video_adapter_t *adp;
    video_info_t info;
    u_char *mp;
    int i;

    /* do this test only once */
    if (init_done)
	return adapters;
    init_done = TRUE;

    /* 
     * Locate display adapters. 
     * The AT architecture supports upto two adapters. `syscons' allows
     * the following combinations of adapters: 
     *     1) MDA + CGA
     *     2) MDA + EGA/VGA color 
     *     3) CGA + EGA/VGA mono
     * Note that `syscons' doesn't bother with MCGA as it is only
     * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
     * thus, they are not running FreeBSD!
     * When there are two adapaters in the system, one becomes `primary'
     * and the other `secondary'. The EGA adapter has a set of DIP 
     * switches on board for this information and the EGA BIOS copies 
     * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88). 
     * The VGA BIOS has more sophisticated mechanism and has this 
     * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains 
     * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
     */

    /* check rtc and BIOS date area */
    /* 
     * XXX: don't use BIOSDATA_EQUIPMENT, it is not a dead copy
     * of RTC_EQUIPMENT. The bit 4 and 5 of the ETC_EQUIPMENT are
     * zeros for EGA and VGA. However, the EGA/VGA BIOS will set 
     * these bits in BIOSDATA_EQUIPMENT according to the monitor
     * type detected.
     */
#ifdef __i386__
    switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {	/* bit 4 and 5 */
    case 0:
	/* EGA/VGA */
	fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f, 
			   adapter);
	break;
    case 1:
	/* CGA 40x25 */
	/* FIXME: switch to the 80x25 mode? XXX */
	adapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
	adapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
	break;
    case 2:
	/* CGA 80x25 */
	adapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
	adapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
	break;
    case 3:
	/* MDA */
	adapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
	adapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
	break;
    }
#else
    /* EGA/VGA */
    fill_adapter_param(-1, adapter);
#endif

    adapters = 0;
    if (verify_adapter(&adapter[V_ADP_SECONDARY]) == 0) {
	++adapters;
	adapter[V_ADP_SECONDARY].va_mode = 
	    adapter[V_ADP_SECONDARY].va_initial_mode =
	    map_bios_mode_num(adapter[V_ADP_SECONDARY].va_type, 
			      adapter[V_ADP_SECONDARY].va_flags & V_ADP_COLOR,
			      adapter[V_ADP_SECONDARY].va_initial_bios_mode);
    } else {
	adapter[V_ADP_SECONDARY].va_type = -1;
    }
    if (verify_adapter(&adapter[V_ADP_PRIMARY]) == 0) {
	++adapters;
#ifdef __i386__
	adapter[V_ADP_PRIMARY].va_initial_bios_mode = 
	    *(u_int8_t *)BIOS_PADDRTOVADDR(0x449);
#else
	adapter[V_ADP_PRIMARY].va_initial_bios_mode = 3;
#endif
	adapter[V_ADP_PRIMARY].va_mode = 
	    adapter[V_ADP_PRIMARY].va_initial_mode =
	    map_bios_mode_num(adapter[V_ADP_PRIMARY].va_type, 
			      adapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
			      adapter[V_ADP_PRIMARY].va_initial_bios_mode);
    } else {
	adapter[V_ADP_PRIMARY] = adapter[V_ADP_SECONDARY];
	adapter[V_ADP_SECONDARY].va_type = -1;
    }
    if (adapters == 0)
	return adapters;
    adapter[V_ADP_PRIMARY].va_index = V_ADP_PRIMARY;
    adapter[V_ADP_SECONDARY].va_index = V_ADP_SECONDARY;

#if 0
    /*
     * We cannot have two video adapter of the same type; there must be
     * only one of color or mono adapter, or one each of them.
     */
    if (adapters > 1) {
	if (!((adapter[0].va_flags ^ adapter[1].va_flags) & V_ADP_COLOR))
	    /* we have two mono or color adapters!! */
	    return (adapters = 0);
    }
#endif

    /*
     * Ensure a zero start address.  This is mainly to recover after
     * switching from pcvt using userconfig().  The registers are w/o
     * for old hardware so it's too hard to relocate the active screen
     * memory.
     * This must be done before vid_save_state() for VGA.
     */
    outb(adapter[V_ADP_PRIMARY].va_crtc_addr, 12);
    outb(adapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
    outb(adapter[V_ADP_PRIMARY].va_crtc_addr, 13);
    outb(adapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);

    /* the video mode parameter table in EGA/VGA BIOS */
    /* NOTE: there can be only one EGA/VGA, wheather color or mono,
     * recognized by the video BIOS.
     */
    if ((adapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
	(adapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
	adp = &adapter[V_ADP_PRIMARY];
    } else if ((adapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
	       (adapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
	adp = &adapter[V_ADP_SECONDARY];
    } else {
	adp = NULL;
    }
    bzero(mode_map, sizeof(mode_map));
    if (adp != NULL) {
	if (adp->va_type == KD_VGA) {
	    vid_save_state(adp - adapter, &adpstate, sizeof(adpstate));
	    if (video_mode_ptr == NULL) {
		mode_map[map_mode_num(adp->va_initial_mode)] = adpstate.regs;
		rows_offset = 1;
	    } else {
		/* discard the table if we are not familiar with it... */
		map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
		mp = get_mode_param(adp->va_initial_mode);
		if (mp != NULL)
		    bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
		switch (comp_adpregs(adpstate.regs, mp)) {
		case COMP_IDENTICAL:
		    /*
		     * OK, this parameter table looks reasonably familiar
		     * to us...
		     */
		    /* 
		     * This is a kludge for Toshiba DynaBook SS433 
		     * whose BIOS video mode table entry has the actual # 
		     * of rows at the offset 1; BIOSes from other 
		     * manufacturers store the # of rows - 1 there. XXX
		     */
		    rows_offset = adpstate.regs[1] + 1 - mp[1];
		    break;

		case COMP_SIMILAR:
		    /*
		     * Not exactly the same, but similar enough to be
		     * trusted. However, use the saved register values
		     * for the initial mode and other modes which are
		     * based on the initial mode.
		     */
		    mode_map[map_mode_num(adp->va_initial_mode)] = 
			adpstate.regs;
		    rows_offset = adpstate.regs[1] + 1 - mp[1];
		    adpstate.regs[1] -= rows_offset - 1;
		    break;

		case COMP_DIFFERENT:
		default:
		    /*
		     * Don't use the paramter table in BIOS. It doesn't
		     * look familiar to us. Video mode switching is allowed
		     * only if the new mode is the same as or based on
		     * the initial mode. 
		     */
		    video_mode_ptr = NULL;
		    bzero(mode_map, sizeof(mode_map));
		    mode_map[map_mode_num(adp->va_initial_mode)] = 
			adpstate.regs;
		    rows_offset = 1;
		    break;
		}
	    }
	    adp->va_flags |= V_ADP_MODECHANGE;
	} else if (adp->va_type == KD_EGA) {
	    if (video_mode_ptr == NULL) {
		adp->va_flags &= ~V_ADP_FONT;
		rows_offset = 1;
	    } else {
		map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
		/* XXX how can one validate the EGA table... */
		mp = get_mode_param(adp->va_initial_mode);
		if (mp != NULL) {
		    adp->va_flags |= V_ADP_MODECHANGE;
		    rows_offset = 1;
		} else {
		    /*
		     * This is serious. We will not be able to switch video
		     * modes at all...
		     */
		    adp->va_flags &= ~V_ADP_FONT;
		    video_mode_ptr = NULL;
		    bzero(mode_map, sizeof(mode_map));
		    rows_offset = 1;
                }
	    }
	}
    }

    /* remove conflicting modes if we have more than one adapter */
    if (adapters > 1) {
	for (i = 0; i < adapters; ++i) {
	    if (!(adapter[i].va_flags & V_ADP_MODECHANGE))
		continue;
	    clear_mode_map(i, mode_map, M_VGA_CG320 + 1,
			   (adapter[i].va_flags & V_ADP_COLOR) ? 
			       V_INFO_COLOR : 0);
	}
    }

    /* buffer address */
    vid_get_info(V_ADP_PRIMARY, 
		 adapter[V_ADP_PRIMARY].va_initial_mode, &info);
    adapter[V_ADP_PRIMARY].va_window = BIOS_PADDRTOVADDR(info.vi_window);
    adapter[V_ADP_PRIMARY].va_window_size = info.vi_window_size;
    adapter[V_ADP_PRIMARY].va_window_gran = info.vi_window_gran;
    adapter[V_ADP_PRIMARY].va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
    adapter[V_ADP_PRIMARY].va_buffer_size = info.vi_buffer_size;
    if (adapters > 1) {
	vid_get_info(V_ADP_SECONDARY, 
		     adapter[V_ADP_SECONDARY].va_initial_mode, &info);
	adapter[V_ADP_SECONDARY].va_window = BIOS_PADDRTOVADDR(info.vi_window);
	adapter[V_ADP_SECONDARY].va_window_size = info.vi_window_size;
	adapter[V_ADP_SECONDARY].va_window_gran = info.vi_window_gran;
	adapter[V_ADP_SECONDARY].va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
	adapter[V_ADP_SECONDARY].va_buffer_size = info.vi_buffer_size;
    }

    /*
     * XXX: we should verify the following values for the primary adapter...
     * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
     * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02) 
     *                     ? 0 : V_ADP_COLOR;
     * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
     * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
     * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
     * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
     */

    return adapters;
}

/* all adapters */
static video_adapter_t
*vid_adapter(int ad)
{
    if (!init_done)
	return NULL;
    if ((ad < 0) || (ad >= adapters))
	return NULL;
    return &adapter[ad];
}

/* all adapters */
static int
vid_get_info(int ad, int mode, video_info_t *info)
{
    int i;

    if (!init_done)
	return 1;
    if ((ad < 0) || (ad >= adapters))
	return 1;

    if (adapter[ad].va_flags & V_ADP_MODECHANGE) {
	/*
	 * If the parameter table entry for this mode is not found, 
	 * the mode is not supported...
	 */
	if (get_mode_param(mode) == NULL)
	    return 1;
    } else {
	/* 
	 * Even if we don't support video mode switching on this adapter,
	 * the information on the initial (thus current) video mode 
	 * should be made available.
	 */
	if (mode != adapter[ad].va_initial_mode)
	    return 1;
    }

    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
	if (bios_vmode[i].vi_mode == NA)
	    continue;
	if (mode == bios_vmode[i].vi_mode) {
	    *info = bios_vmode[i];
	    return 0;
	}
    }
    return 1;
}

/* all adapters */
static int
vid_query_mode(int ad, video_info_t *info)
{
    video_info_t buf;
    int i;

    if (!init_done)
	return -1;
    if ((ad < 0) || (ad >= adapters))
	return -1;

    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
	if (bios_vmode[i].vi_mode == NA)
	    continue;

	if ((info->vi_width != 0)
	    && (info->vi_width != bios_vmode[i].vi_width))
		continue;
	if ((info->vi_height != 0)
	    && (info->vi_height != bios_vmode[i].vi_height))
		continue;
	if ((info->vi_cwidth != 0)
	    && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
		continue;
	if ((info->vi_cheight != 0)
	    && (info->vi_cheight != bios_vmode[i].vi_cheight))
		continue;
	if ((info->vi_depth != 0)
	    && (info->vi_depth != bios_vmode[i].vi_depth))
		continue;
	if ((info->vi_planes != 0)
	    && (info->vi_planes != bios_vmode[i].vi_planes))
		continue;
	/* XXX: should check pixel format, memory model */
	if ((info->vi_flags != 0)
	    && (info->vi_flags != bios_vmode[i].vi_flags))
		continue;

	/* verify if this mode is supported on this adapter */
	if (vid_get_info(ad, bios_vmode[i].vi_mode, &buf))
		continue;
	return bios_vmode[i].vi_mode;
    }
    return -1;
}

/* EGA/VGA */
static int
vid_set_mode(int ad, int mode)
{
    video_info_t info;
    adp_state_t params;

    prologue(ad, V_ADP_MODECHANGE, 1);

    if (vid_get_info(ad, mode, &info))
	return 1;
    params.sig = V_STATE_SIG;
    bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));

    switch (mode) {
    case M_VGA_C80x60: case M_VGA_M80x60:
	params.regs[2]  = 0x08;
	params.regs[19] = 0x47;
	goto special_480l;

    case M_VGA_C80x30: case M_VGA_M80x30:
	params.regs[19] = 0x4f;
special_480l:
	params.regs[9] |= 0xc0;
	params.regs[16] = 0x08;
	params.regs[17] = 0x3e;
	params.regs[26] = 0xea;
	params.regs[28] = 0xdf;
	params.regs[31] = 0xe7;
	params.regs[32] = 0x04;
	goto setup_mode;

    case M_ENH_C80x43: case M_ENH_B80x43:
	params.regs[28] = 87;
	goto special_80x50;

    case M_VGA_C80x50: case M_VGA_M80x50:
special_80x50:
	params.regs[2] = 8;
	params.regs[19] = 7;
	goto setup_mode;

    case M_VGA_C40x25: case M_VGA_C80x25:
    case M_VGA_M80x25:
    case M_B40x25:     case M_C40x25:
    case M_B80x25:     case M_C80x25:
    case M_ENH_B40x25: case M_ENH_C40x25:
    case M_ENH_B80x25: case M_ENH_C80x25:
    case M_EGAMONO80x25:

setup_mode:
	vid_load_state(ad, &params);
	break;

    case M_VGA_MODEX:
	/* "unchain" the VGA mode */
	params.regs[5-1+0x04] &= 0xf7;
	params.regs[5-1+0x04] |= 0x04;
	/* turn off doubleword mode */
	params.regs[10+0x14] &= 0xbf;
	/* turn off word adressing */
	params.regs[10+0x17] |= 0x40;
	/* set logical screen width */
	params.regs[10+0x13] = 80;
	/* set 240 lines */
	params.regs[10+0x11] = 0x2c;
	params.regs[10+0x06] = 0x0d;
	params.regs[10+0x07] = 0x3e;
	params.regs[10+0x10] = 0xea;
	params.regs[10+0x11] = 0xac;
	params.regs[10+0x12] = 0xdf;
	params.regs[10+0x15] = 0xe7;
	params.regs[10+0x16] = 0x06;
	/* set vertical sync polarity to reflect aspect ratio */
	params.regs[9] = 0xe3;
	goto setup_grmode;

    case M_BG320:     case M_CG320:     case M_BG640:
    case M_CG320_D:   case M_CG640_E:
    case M_CG640x350: case M_ENH_CG640:
    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:

setup_grmode:
	vid_load_state(ad, &params);
	break;

    default:
	return 1;
    }

    adapter[ad].va_mode = mode;
    adapter[ad].va_flags &= ~V_ADP_COLOR;
    adapter[ad].va_flags |= 
	(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
    adapter[ad].va_crtc_addr =
	(adapter[ad].va_flags & V_ADP_COLOR) ? COLOR_BASE : MONO_BASE;
    adapter[ad].va_window = BIOS_PADDRTOVADDR(info.vi_window);
    adapter[ad].va_window_size = info.vi_window_size;
    adapter[ad].va_window_gran = info.vi_window_gran;
    if (info.vi_buffer_size == 0) {
    	adapter[ad].va_buffer = 0;
    	adapter[ad].va_buffer_size = 0;
    } else {
    	adapter[ad].va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
    	adapter[ad].va_buffer_size = info.vi_buffer_size;
    }

    return 0;
}

static void
set_font_mode(video_adapter_t *adp, u_char *buf)
{
    u_char *mp;
    int s;

    s = splhigh();

    /* save register values */
    if (adp->va_type == KD_VGA) {
	outb(TSIDX, 0x02); buf[0] = inb(TSREG);
	outb(TSIDX, 0x04); buf[1] = inb(TSREG);
	outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
	outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
	outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
	inb(adp->va_crtc_addr + 6);
	outb(ATC, 0x10); buf[5] = inb(ATC + 1);
    } else /* if (adp->va_type == KD_EGA) */ {
	/* 
	 * EGA cannot be read; copy parameters from the mode parameter 
	 * table. 
	 */
	mp = get_mode_param(adp->va_mode);
	buf[0] = mp[5 + 0x02 - 1];
	buf[1] = mp[5 + 0x04 - 1];
	buf[2] = mp[55 + 0x04];
	buf[3] = mp[55 + 0x05];
	buf[4] = mp[55 + 0x06];
	buf[5] = mp[35 + 0x10];
    }

    /* setup vga for loading fonts */
    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
    outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
    outb(ATC, 0x20);				/* enable palette */

#if SLOW_VGA
#ifndef SC_BAD_FLICKER
    outb(TSIDX, 0x00); outb(TSREG, 0x01);
#endif
    outb(TSIDX, 0x02); outb(TSREG, 0x04);
    outb(TSIDX, 0x04); outb(TSREG, 0x07);
#ifndef SC_BAD_FLICKER
    outb(TSIDX, 0x00); outb(TSREG, 0x03);
#endif
    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
    outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
#else
#ifndef SC_BAD_FLICKER
    outw(TSIDX, 0x0100);
#endif
    outw(TSIDX, 0x0402);
    outw(TSIDX, 0x0704);
#ifndef SC_BAD_FLICKER
    outw(TSIDX, 0x0300);
#endif
    outw(GDCIDX, 0x0204);
    outw(GDCIDX, 0x0005);
    outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
#endif

    splx(s);
}

static void
set_normal_mode(video_adapter_t *adp, u_char *buf)
{
    int s;

    s = splhigh();

    /* setup vga for normal operation mode again */
    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
    outb(ATC, 0x10); outb(ATC, buf[5]);
    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
    outb(ATC, 0x20);				/* enable palette */

#if SLOW_VGA
#ifndef SC_BAD_FLICKER
    outb(TSIDX, 0x00); outb(TSREG, 0x01);
#endif
    outb(TSIDX, 0x02); outb(TSREG, buf[0]);
    outb(TSIDX, 0x04); outb(TSREG, buf[1]);
#ifndef SC_BAD_FLICKER
    outb(TSIDX, 0x00); outb(TSREG, 0x03);
#endif
    outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
    outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
    if (adp->va_crtc_addr == MONO_BASE) {
	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
    } else {
	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
    }
#else
#ifndef SC_BAD_FLICKER
    outw(TSIDX, 0x0100);
#endif
    outw(TSIDX, 0x0002 | (buf[0] << 8));
    outw(TSIDX, 0x0004 | (buf[1] << 8));
#ifndef SC_BAD_FLICKER
    outw(TSIDX, 0x0300);
#endif
    outw(GDCIDX, 0x0004 | (buf[2] << 8));
    outw(GDCIDX, 0x0005 | (buf[3] << 8));
    if (adp->va_crtc_addr == MONO_BASE)
        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
    else
        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
#endif

    splx(s);
}

/* EGA/VGA */
static int
vid_save_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
{
    u_char buf[PARAM_BUFSIZE];
    u_char val = 0;
    u_int32_t segment;
    int c;
    int s;

    prologue(ad, V_ADP_FONT, 1);

    if (fontsize < 14) {
	/* FONT_8 */
	fontsize = 8;
    } else if (fontsize >= 32) {
	fontsize = 32;
    } else if (fontsize >= 16) {
	/* FONT_16 */
	fontsize = 16;
    } else {
	/* FONT_14 */
	fontsize = 14;
    }

    if (page < 0 || page >= 8)
	return 1;
    segment = VIDEOMEM + 0x4000*page;
    if (page > 3)
	segment -= 0xe000;

    if (adapter[ad].va_type == KD_VGA) {	/* what about EGA? XXX */
	s = splhigh();
	outb(TSIDX, 0x00); outb(TSREG, 0x01);
	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
	outb(TSIDX, 0x00); outb(TSREG, 0x03);
	splx(s);
    }

    set_font_mode(&adapter[ad], buf);
    if (fontsize == 32) {
	memcpy_fromio(data, BIOS_PADDRTOVADDR(segment + ch*32),
		      fontsize*count);
    } else {
	for (c = ch; count > 0; ++c, --count) {
	    memcpy_fromio(data, BIOS_PADDRTOVADDR(segment + c*32),
			  fontsize);
	    data += fontsize;
	}
    }
    set_normal_mode(&adapter[ad], buf);

    if (adapter[ad].va_type == KD_VGA) {
	s = splhigh();
	outb(TSIDX, 0x00); outb(TSREG, 0x01);
	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
	outb(TSIDX, 0x00); outb(TSREG, 0x03);
	splx(s);
    }

    return 0;
}

/* EGA/VGA */
static int
vid_load_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
{
    u_char buf[PARAM_BUFSIZE];
    u_char val = 0;
    u_int32_t segment;
    int c;
    int s;

    prologue(ad, V_ADP_FONT, 1);

    if (fontsize < 14) {
	/* FONT_8 */
	fontsize = 8;
    } else if (fontsize >= 32) {
	fontsize = 32;
    } else if (fontsize >= 16) {
	/* FONT_16 */
	fontsize = 16;
    } else {
	/* FONT_14 */
	fontsize = 14;
    }

    if (page < 0 || page >= 8)
	return 1;
    segment = VIDEOMEM + 0x4000*page;
    if (page > 3)
	segment -= 0xe000;

    if (adapter[ad].va_type == KD_VGA) {	/* what about EGA? XXX */
	s = splhigh();
	outb(TSIDX, 0x00); outb(TSREG, 0x01);
	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
	outb(TSIDX, 0x00); outb(TSREG, 0x03);
	splx(s);
    }

    set_font_mode(&adapter[ad], buf);
    if (fontsize == 32) {
	memcpy_toio(BIOS_PADDRTOVADDR(segment + ch*32), data,
		    fontsize*count);
    } else {
	for (c = ch; count > 0; ++c, --count) {
	    memcpy_toio(BIOS_PADDRTOVADDR(segment + c*32), data, 
			fontsize);
	    data += fontsize;
	}
    }
    set_normal_mode(&adapter[ad], buf);

    if (adapter[ad].va_type == KD_VGA) {
	s = splhigh();
	outb(TSIDX, 0x00); outb(TSREG, 0x01);
	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
	outb(TSIDX, 0x00); outb(TSREG, 0x03);
	splx(s);
    }

    return 0;
}

/* EGA/VGA */
static int
vid_show_font(int ad, int page)
{
    static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
    int s;

    prologue(ad, V_ADP_FONT, 1);
    if (page < 0 || page >= 8)
	return 1;

    s = splhigh();
    outb(TSIDX, 0x03); outb(TSREG, cg[page]);
    splx(s);

    return 0;
}

/* VGA */
static int
vid_save_palette(int ad, u_char *palette)
{
    int i;

    prologue(ad, V_ADP_PALETTE, 1);

    /* 
     * We store 8 bit values in the palette buffer, while the standard
     * VGA has 6 bit DAC .
     */
    outb(PALRADR, 0x00);
    for (i = 0; i < 256*3; ++i)
	palette[i] = inb(PALDATA) << 2; 
    inb(adapter[ad].va_crtc_addr + 6);	/* reset flip/flop */
    return 0;
}

/* VGA */
static int
vid_load_palette(int ad, u_char *palette)
{
    int i;

    prologue(ad, V_ADP_PALETTE, 1);

    outb(PIXMASK, 0xff);		/* no pixelmask */
    outb(PALWADR, 0x00);
    for (i = 0; i < 256*3; ++i)
	outb(PALDATA, palette[i] >> 2);
    inb(adapter[ad].va_crtc_addr + 6);	/* reset flip/flop */
    outb(ATC, 0x20);			/* enable palette */
    return 0;
}

/* CGA/EGA/VGA */
static int
vid_set_border(int ad, int color)
{
    prologue(ad, V_ADP_BORDER, 1);

    switch (adapter[ad].va_type) {
    case KD_EGA:
    case KD_VGA:    
	inb(adapter[ad].va_crtc_addr + 6);	/* reset flip-flop */
	outb(ATC, 0x31); outb(ATC, color & 0xff); 
	break;  
    case KD_CGA:    
	outb(adapter[ad].va_crtc_addr + 5, color & 0x0f); /* color select register */
	break;  
    case KD_MONO:   
    case KD_HERCULES:
    default:
	break;  
    }
    return 0;
}

/* VGA */
static int
vid_save_state(int ad, void *p, size_t size)
{
    video_info_t info;
    u_char *buf;
    int crtc_addr;
    int i, j;
    int s;

    if (size == 0) {
	/* return the required buffer size */
	prologue(ad, V_ADP_STATESAVE, 0);
	return sizeof(adp_state_t);
    } else {
	prologue(ad, V_ADP_STATESAVE, 1);
	if (size < sizeof(adp_state_t))
	    return 1;
    }

    ((adp_state_t *)p)->sig = V_STATE_SIG;
    buf = ((adp_state_t *)p)->regs;
    bzero(buf, V_MODE_PARAM_SIZE);
    crtc_addr = adapter[ad].va_crtc_addr;

    s = splhigh();

    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
    for (i = 0, j = 5; i < 4; i++) {           
	outb(TSIDX, i + 1);
	buf[j++]  =  inb(TSREG);
    }
    buf[9]  =  inb(MISC + 10);			/* dot-clock */
    outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */

    for (i = 0, j = 10; i < 25; i++) {		/* crtc */
	outb(crtc_addr, i);
	buf[j++]  =  inb(crtc_addr + 1);
    }
    for (i = 0, j = 35; i < 20; i++) {		/* attribute ctrl */
        inb(crtc_addr + 6);			/* reset flip-flop */
	outb(ATC, i);
	buf[j++]  =  inb(ATC + 1);
    }
    for (i = 0, j = 55; i < 9; i++) {		/* graph data ctrl */
	outb(GDCIDX, i);
	buf[j++]  =  inb(GDCREG);
    }
    inb(crtc_addr + 6);				/* reset flip-flop */
    outb(ATC, 0x20);				/* enable palette */

    splx(s);

#if 1
    if (vid_get_info(ad, adapter[ad].va_mode, &info) == 0) {
	if (info.vi_flags & V_INFO_GRAPHICS) {
	    buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
	    buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
	} else {
	    buf[0] = info.vi_width;		/* COLS */
	    buf[1] = info.vi_height - 1;	/* ROWS */
	}
	buf[2] = info.vi_cheight;		/* POINTS */
    } else {
	/* XXX: shouldn't be happening... */
	printf("video#%d: failed to obtain mode info. (vid_save_state())\n",
	       ad);
    }
#else
    buf[0] = *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);	/* COLS */
    buf[1] = *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);	/* ROWS */
    buf[2] = *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);	/* POINTS */
    buf[3] = *(u_int8_t *)BIOS_PADDRTOVADDR(0x44c);
    buf[4] = *(u_int8_t *)BIOS_PADDRTOVADDR(0x44d);
#endif

    return 0;
}

/* EGA/VGA */
static int
vid_load_state(int ad, void *p)
{
    u_char *buf;
    int crtc_addr;
    int s;
    int i;

    prologue(ad, V_ADP_STATELOAD, 1);
    if (((adp_state_t *)p)->sig != V_STATE_SIG)
	return 1;

    buf = ((adp_state_t *)p)->regs;
    crtc_addr = adapter[ad].va_crtc_addr;

    s = splhigh();

    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
    for (i = 0; i < 4; ++i) {			/* program sequencer */
	outb(TSIDX, i + 1);
	outb(TSREG, buf[i + 5]);
    }
    outb(MISC, buf[9]);				/* set dot-clock */
    outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
    outb(crtc_addr, 0x11);
    outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
    for (i = 0; i < 25; ++i) {			/* program crtc */
	outb(crtc_addr, i);
	outb(crtc_addr + 1, buf[i + 10]);
    }
    inb(crtc_addr+6);				/* reset flip-flop */
    for (i = 0; i < 20; ++i) {			/* program attribute ctrl */
	outb(ATC, i);
	outb(ATC, buf[i + 35]);
    }
    for (i = 0; i < 9; ++i) {			/* program graph data ctrl */
	outb(GDCIDX, i);
	outb(GDCREG, buf[i + 55]);
    }
    inb(crtc_addr + 6);				/* reset flip-flop */
    outb(ATC, 0x20);				/* enable palette */

#ifdef __i386__
    if (ad == V_ADP_PRIMARY) {
	*(u_int8_t *)BIOS_PADDRTOVADDR(0x44a) = buf[0];	/* COLS */
	*(u_int8_t *)BIOS_PADDRTOVADDR(0x484) = buf[1] + rows_offset - 1; /* ROWS */
	*(u_int8_t *)BIOS_PADDRTOVADDR(0x485) = buf[2];	/* POINTS */
#if 0
	*(u_int8_t *)BIOS_PADDRTOVADDR(0x44c) = buf[3];
	*(u_int8_t *)BIOS_PADDRTOVADDR(0x44d) = buf[4];
#endif
    }
#endif

    splx(s);
    return 0;
}

/* all */
static int
vid_set_origin(int ad, off_t offset)
{
    /* 
     * The standard video modes do not require window mapping; 
     * always return error.
     */
    return 1;
}

/* all */
static int
vid_read_hw_cursor(int ad, int *col, int *row)
{
    video_info_t info;
    u_int16_t off;

    if (!init_done)
	return 1;
    if ((ad < 0) || (ad >= adapters))
	return 1;

    (*biosvidsw.get_info)(ad, adapter[ad].va_mode, &info);
    if (info.vi_flags & V_INFO_GRAPHICS)
	return 1;

    outb(adapter[ad].va_crtc_addr, 14);
    off = inb(adapter[ad].va_crtc_addr + 1);
    outb(adapter[ad].va_crtc_addr, 15);
    off = (off << 8) | inb(adapter[ad].va_crtc_addr + 1);

    *row = off / info.vi_width;
    *col = off % info.vi_width;

    return 0;
}

/* all */
static int
vid_set_hw_cursor(int ad, int col, int row)
{
    video_info_t info;
    u_int16_t off;

    if (!init_done)
	return 1;
    if ((ad < 0) || (ad >= adapters))
	return 1;

    (*biosvidsw.get_info)(ad, adapter[ad].va_mode, &info);
    if (info.vi_flags & V_INFO_GRAPHICS)
	return 1;

    if ((col == -1) || (row == -1))
	off = 0xffff;
    else
	off = row*info.vi_width + col;
    outb(adapter[ad].va_crtc_addr, 14);
    outb(adapter[ad].va_crtc_addr + 1, off >> 8);
    outb(adapter[ad].va_crtc_addr, 15);
    outb(adapter[ad].va_crtc_addr + 1, off & 0x00ff);

    return 0;
}

static char
*adapter_name(int type)
{
    static struct {
	int type;
	char *name;
    } names[] = {
	{ KD_MONO,	"MDA" },
	{ KD_HERCULES,	"Hercules" },
	{ KD_CGA,	"CGA" },
	{ KD_EGA,	"EGA" },
	{ KD_VGA,	"VGA" },
	{ KD_PC98,	"PC-98xx" },
	{ -1,		"Unknown" },
    };
    int i;

    for (i = 0; names[i].type != -1; ++i)
	if (names[i].type == type)
	    break;
    return names[i].name;
}

static void
dump_adp_info(int ad, video_adapter_t *adp, int level)
{
    if (level <= 0)
	return;

    printf("video#%d: adapter type:%s (%d), flags:0x%x, CRTC:0x%x\n", 
	   ad, adapter_name(adp->va_type), adp->va_type, 
	   adp->va_flags, adp->va_crtc_addr);
    printf("video#%d: init mode:%d, bios mode:%d, current mode:%d\n",
	   ad, adp->va_initial_mode, adp->va_initial_bios_mode, adp->va_mode);
    printf("video#%d: window:0x%x size:%dk gran:%dk, buf:0x%x size:%dk\n",
	   ad, 
	   adp->va_window, (int)adp->va_window_size, (int)adp->va_window_gran,
	   adp->va_buffer, (int)adp->va_buffer_size);
}

static void
dump_mode_info(int ad, video_info_t *info, int level)
{
    if (level <= 0)
	return;

    printf("video#%d: mode:%d, flags:0x%x ", 
	   ad, info->vi_mode, info->vi_flags);
    if (info->vi_flags & V_INFO_GRAPHICS)
	printf("G %dx%dx%d, %d plane(s), font:%dx%d, ",
	       info->vi_width, info->vi_height, 
	       info->vi_depth, info->vi_planes, 
	       info->vi_cwidth, info->vi_cheight); 
    else
	printf("T %dx%d, font:%dx%d, ",
	       info->vi_width, info->vi_height, 
	       info->vi_cwidth, info->vi_cheight); 
    printf("win:0x%x\n", info->vi_window);
}

static void
dump_buffer(u_char *buf, size_t len)
{
    int i;

    for(i = 0; i < len;) {
	printf("%02x ", buf[i]);
	if ((++i % 16) == 0)
	    printf("\n");
    }
}

static int
vid_diag(int level)
{
    video_info_t info;
    u_char *mp;
    int ad;
    int i;

    if (!init_done)
	return 1;

#ifdef __i386__
    if (level > 0) {
	printf("video: RTC equip. code:0x%02x, DCC code:0x%02x\n",
	       rtcin(RTC_EQUIPMENT), *(u_int8_t *)BIOS_PADDRTOVADDR(0x488));
	printf("video: CRTC:0x%x, video option:0x%02x, ",
	       *(u_int16_t *)BIOS_PADDRTOVADDR(0x463),
	       *(u_int8_t *)BIOS_PADDRTOVADDR(0x487));
	printf("rows:%d, cols:%d, font height:%d\n",
	       *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a),
	       *(u_int8_t *)BIOS_PADDRTOVADDR(0x484) + 1,
	       *(u_int8_t *)BIOS_PADDRTOVADDR(0x485));
	printf("video: param table EGA/VGA:%p, CGA/MDA:%p\n", 
	       video_mode_ptr, video_mode_ptr2);
	printf("video: rows_offset:%d\n", rows_offset);
    }
#endif

    for (ad = 0; ad < adapters; ++ad) {
	dump_adp_info(ad, &adapter[ad], level);

	if (!(adapter[ad].va_flags & V_ADP_MODECHANGE)) {
	    vid_get_info(ad, adapter[ad].va_initial_mode, &info);
	    dump_mode_info(ad, &info, level);
	} else {
	    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
		if (bios_vmode[i].vi_mode == NA)
		    continue;
		if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
		    continue;
		dump_mode_info(ad, &bios_vmode[i], level);
	    }
	}
	if ((adapter[ad].va_type != KD_EGA) && (adapter[ad].va_type != KD_VGA))
	    continue;

	if (video_mode_ptr == NULL)
	    printf("video#%d: WARNING: video mode switching is not fully supported on this adapter\n", ad);

	if (level <= 0)
	    continue;

	if (adapter[ad].va_type == KD_VGA) {
	    printf("VGA parameters upon power-up\n");
	    dump_buffer(adpstate.regs, sizeof(adpstate.regs));
	    printf("VGA parameters in BIOS for mode %d\n", 
		adapter[ad].va_initial_mode);
	    dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
	}

	mp = get_mode_param(adapter[ad].va_initial_mode);
	if (mp == NULL)	/* this shouldn't be happening */
	    continue;
	printf("EGA/VGA parameters to be used for mode %d\n", 
	    adapter[ad].va_initial_mode);
	dump_buffer(mp, V_MODE_PARAM_SIZE);
    }

    return 0;
}

#endif /* NSC > 0 */
OpenPOWER on IntegriCloud