summaryrefslogtreecommitdiff
path: root/src/smi_driver.c
blob: cbac0fb2e278619f114b7168a2f516c0b633ef76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
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
/* Header:   //Mercury/Projects/archives/XFree86/4.0/smi_driver.c-arc   1.42   03 Jan 2001 13:52:16   Frido  $ */

/*
Copyright (C) 1994-1999 The XFree86 Project, Inc.  All Rights Reserved.
Copyright (C) 2000 Silicon Motion, Inc.  All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of The XFree86 Project and
Silicon Motion shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written
authorization from The XFree86 Project or Silicon Motion.
*/
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/siliconmotion/smi_driver.c,v 1.36tsi Exp $ */

#include "xf86Resources.h"
#include "xf86RAC.h"
#include "xf86DDC.h"
#include "xf86int10.h"
#include "vbe.h"
#include "shadowfb.h"

#include "smi.h"

#include "globals.h"
#define DPMS_SERVER
#include "extensions/dpms.h"

/*
 * Internals
 */
static void SMI_EnableMmio(ScrnInfoPtr pScrn);
static void SMI_DisableMmio(ScrnInfoPtr pScrn);

/*
 * Forward definitions for the functions that make up the driver.
 */

static const OptionInfoRec * SMI_AvailableOptions(int chipid, int busid);
static void SMI_Identify(int flags);
static Bool SMI_Probe(DriverPtr drv, int flags);
static Bool SMI_PreInit(ScrnInfoPtr pScrn, int flags);
static Bool SMI_EnterVT(int scrnIndex, int flags);
static void SMI_LeaveVT(int scrnIndex, int flags);
static void SMI_Save (ScrnInfoPtr pScrn);
static void SMI_WriteMode (ScrnInfoPtr pScrn, vgaRegPtr, SMIRegPtr);
static Bool SMI_ScreenInit(int scrnIndex, ScreenPtr pScreen, int argc,
                           char **argv);
static int SMI_InternalScreenInit(int scrnIndex, ScreenPtr pScreen);
static void SMI_PrintRegs(ScrnInfoPtr);
static ModeStatus SMI_ValidMode(int scrnIndex, DisplayModePtr mode,
                                Bool verbose, int flags);
static void SMI_DisableVideo(ScrnInfoPtr pScrn);
static void SMI_EnableVideo(ScrnInfoPtr pScrn);
static Bool SMI_MapMem(ScrnInfoPtr pScrn);
static void SMI_UnmapMem(ScrnInfoPtr pScrn);
static Bool SMI_ModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
static Bool SMI_CloseScreen(int scrnIndex, ScreenPtr pScreen);
static Bool SMI_SaveScreen(ScreenPtr pScreen, int mode);
static void SMI_LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indicies,
                            LOCO *colors, VisualPtr pVisual);
static void SMI_DisplayPowerManagementSet(ScrnInfoPtr pScrn,
                                          int PowerManagementMode, int flags);
static Bool SMI_ddc1(int scrnIndex);
static unsigned int SMI_ddc1Read(ScrnInfoPtr pScrn);
static void SMI_FreeScreen(int ScrnIndex, int flags);
static void SMI_ProbeDDC(ScrnInfoPtr pScrn, int index);


#define SILICONMOTION_NAME          "Silicon Motion"
#define SILICONMOTION_DRIVER_NAME   "siliconmotion"
#define SILICONMOTION_VERSION_NAME  "1.3.1"
#define SILICONMOTION_VERSION_MAJOR 1
#define SILICONMOTION_VERSION_MINOR 3
#define SILICONMOTION_PATCHLEVEL    1
#define SILICONMOTION_DRIVER_VERSION ((SILICONMOTION_VERSION_MAJOR << 24) | \
                                      (SILICONMOTION_VERSION_MINOR << 16) | \
                                      (SILICONMOTION_PATCHLEVEL))

/*
 * This contains the functions needed by the server after loading the
 * driver module.  It must be supplied, and gets added the driver list by
 * the Module Setup funtion in the dynamic case.  In the static case a
 * reference to this is compiled in, and this requires that the name of
 * this DriverRec be an upper-case version of the driver name.
 */

DriverRec SILICONMOTION =
{
	SILICONMOTION_DRIVER_VERSION,
	SILICONMOTION_DRIVER_NAME,
	SMI_Identify,
	SMI_Probe,
	SMI_AvailableOptions,
	NULL,
	0
};

/* Supported chipsets */
static SymTabRec SMIChipsets[] =
{
	{ PCI_CHIP_SMI910, "Lynx"    },
	{ PCI_CHIP_SMI810, "LynxE"   },
	{ PCI_CHIP_SMI820, "Lynx3D"  },
	{ PCI_CHIP_SMI710, "LynxEM"  },
	{ PCI_CHIP_SMI712, "LynxEM+" },
	{ PCI_CHIP_SMI720, "Lynx3DM" },
	{ PCI_CHIP_SMI731, "Cougar3DR" },
	{ -1,             NULL      }
};

static PciChipsets SMIPciChipsets[] =
{
	/* numChipset,		PciID,				Resource */
	{ PCI_CHIP_SMI910,	PCI_CHIP_SMI910,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI810,	PCI_CHIP_SMI810,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI820,	PCI_CHIP_SMI820,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI710,	PCI_CHIP_SMI710,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI712,	PCI_CHIP_SMI712,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI720,	PCI_CHIP_SMI720,	RES_SHARED_VGA },
	{ PCI_CHIP_SMI731,	PCI_CHIP_SMI731,	RES_SHARED_VGA },
	{ -1,				-1,					RES_UNDEFINED  }
};

typedef enum
{
	OPTION_PCI_BURST,
	OPTION_FIFO_CONSERV,
	OPTION_FIFO_MODERATE,
	OPTION_FIFO_AGGRESSIVE,
	OPTION_PCI_RETRY,
	OPTION_NOACCEL,
	OPTION_MCLK,
	OPTION_SHOWCACHE,
	OPTION_SWCURSOR,
	OPTION_HWCURSOR,
	OPTION_SHADOW_FB,
	OPTION_ROTATE,
	OPTION_VIDEOKEY,
	OPTION_BYTESWAP,
	/* CZ 26.10.2001: interlaced video */
	OPTION_INTERLACED,
	/* end CZ */
	OPTION_USEBIOS,
	OPTION_ZOOMONLCD,
	NUMBER_OF_OPTIONS

} SMIOpts;

static const OptionInfoRec SMIOptions[] =
{
   { OPTION_PCI_BURST,	     "pci_burst",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_FIFO_CONSERV,    "fifo_conservative", OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_FIFO_MODERATE,   "fifo_moderate",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_FIFO_AGGRESSIVE, "fifo_aggressive",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_PCI_RETRY,	     "pci_retry",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_NOACCEL,	     "NoAccel",		  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_MCLK,	     "set_mclk",	  OPTV_FREQ,	{0}, FALSE },
   { OPTION_SHOWCACHE,	     "show_cache",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_HWCURSOR,	     "HWCursor",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_SWCURSOR,	     "SWCursor",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_SHADOW_FB,	     "ShadowFB",	  OPTV_BOOLEAN, {0}, FALSE },
   { OPTION_ROTATE,	     "Rotate",		  OPTV_ANYSTR,  {0}, FALSE },
   { OPTION_VIDEOKEY,	     "VideoKey",	  OPTV_INTEGER, {0}, FALSE },
   { OPTION_BYTESWAP,	     "ByteSwap",	  OPTV_BOOLEAN, {0}, FALSE },
    /* CZ 26.10.2001: interlaced video */
   { OPTION_INTERLACED,	     "Interlaced",        OPTV_BOOLEAN, {0}, FALSE },
   /* end CZ */
   { OPTION_USEBIOS,	     "UseBIOS",		  OPTV_BOOLEAN,	{0}, FALSE },
   { OPTION_ZOOMONLCD,	     "ZoomOnLCD",	  OPTV_BOOLEAN,	{0}, FALSE },
   { -1,		     NULL,		  OPTV_NONE,	{0}, FALSE }
};

/*
 * Lists of symbols that may/may not be required by this driver.
 * This allows the loader to know which ones to issue warnings for.
 *
 * Note that vgahwSymbols and xaaSymbols are referenced outside the
 * XFree86LOADER define in later code, so are defined outside of that
 * define here also.
 */

static const char *vgahwSymbols[] =
{
	"vgaHWCopyReg",
	"vgaHWGetHWRec",
	"vgaHWGetIOBase",
	"vgaHWGetIndex",
	"vgaHWInit",
	"vgaHWLock",
	"vgaHWMapMem",
	"vgaHWProtect",
	"vgaHWRestore",
	"vgaHWSave",
	"vgaHWSaveScreen",
	"vgaHWSetMmioFuncs",
	"vgaHWSetStdFuncs",
	"vgaHWUnmapMem",
	"vgaHWddc1SetSpeed",
	NULL
};

static const char *xaaSymbols[] =
{
	"XAAGetCopyROP",
	"XAACreateInfoRec",
	"XAADestroyInfoRec",
	"XAAGetFallbackOps",
	"XAAInit",
	"XAAGetPatternROP",
	NULL
};

static const char *ramdacSymbols[] =
{
	"xf86CreateCursorInfoRec",
	"xf86DestroyCursorInfoRec",
	"xf86InitCursor",
	NULL
};

static const char *ddcSymbols[] =
{
	"xf86PrintEDID",
	"xf86DoEDID_DDC1",
	"xf86DoEDID_DDC2",
	"xf86SetDDCproperties",
	NULL
};

static const char *i2cSymbols[] =
{
	"xf86CreateI2CBusRec",
	"xf86CreateI2CDevRec",
	"xf86DestroyI2CBusRec",
	"xf86DestroyI2CDevRec",
	"xf86I2CBusInit",
	"xf86I2CDevInit",
	"xf86I2CReadBytes",
	"xf86I2CWriteByte",
	NULL
};

static const char *shadowSymbols[] =
{
	"ShadowFBInit",
	NULL
};

static const char *int10Symbols[] =
{
	"xf86ExecX86int10",
	"xf86FreeInt10",
	"xf86InitInt10",
	NULL
};

static const char *vbeSymbols[] =
{
	"VBEInit",
	"vbeDoEDID",
	"vbeFree",
	NULL
};

static const char *fbSymbols[] =
{
#ifdef USE_FB
	"fbPictureInit",
	"fbScreenInit",
#else
	"cfbScreenInit",
	"cfb16ScreenInit",
	"cfb24ScreenInit",
	"cfb32ScreenInit",
	"cfb16BresS",
	"cfb24BresS",
#endif
	NULL
};

#ifdef XFree86LOADER

static MODULESETUPPROTO(siliconmotionSetup);

static XF86ModuleVersionInfo SMIVersRec =
{
	"siliconmotion",
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
	XORG_VERSION_CURRENT,
	SILICONMOTION_VERSION_MAJOR,
	SILICONMOTION_VERSION_MINOR,
	SILICONMOTION_PATCHLEVEL,
	ABI_CLASS_VIDEODRV,
	ABI_VIDEODRV_VERSION,
	MOD_CLASS_VIDEODRV,
	{0, 0, 0, 0}
};

/*
 * This is the module init data for XFree86 modules.
 *
 * Its name has to be the driver name followed by ModuleData.
 */
XF86ModuleData siliconmotionModuleData =
{
	&SMIVersRec,
	siliconmotionSetup,
	NULL
};

static pointer
siliconmotionSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
	static Bool setupDone = FALSE;

	if (!setupDone)
	{
		setupDone = TRUE;
		xf86AddDriver(&SILICONMOTION, module, 0);

		/*
		 * Modules that this driver always requires can be loaded here
		 * by calling LoadSubModule().
		 */

		/*
		 * Tell the loader about symbols from other modules that this module
		 * might refer to.
		 */
		LoaderRefSymLists(vgahwSymbols, fbSymbols, xaaSymbols, ramdacSymbols,
						  ddcSymbols, i2cSymbols, int10Symbols, vbeSymbols,
						  shadowSymbols, NULL);

		/*
		 * The return value must be non-NULL on success even though there
		 * is no TearDownProc.
		 */
		return (pointer) 1;
	}
	else
	{
		if (errmaj)
		{
			*errmaj = LDR_ONCEONLY;
		}
		return(NULL);
	}
}

#endif /* XFree86LOADER */

static Bool
SMI_GetRec(ScrnInfoPtr pScrn)
{
	ENTER_PROC("SMI_GetRec");

	/*
	 * Allocate an 'Chip'Rec, and hook it into pScrn->driverPrivate.
	 * pScrn->driverPrivate is initialised to NULL, so we can check if
	 * the allocation has already been done.
	 */
	if (pScrn->driverPrivate == NULL)
	{
		pScrn->driverPrivate = xnfcalloc(sizeof(SMIRec), 1);
	}

	LEAVE_PROC("SMI_GetRec");
	return(TRUE);
}

static void
SMI_FreeRec(ScrnInfoPtr pScrn)
{
	ENTER_PROC("SMI_FreeRec");

	if (pScrn->driverPrivate != NULL)
	{
		xfree(pScrn->driverPrivate);
		pScrn->driverPrivate = NULL;
	}

	LEAVE_PROC("SMI_FreeRec");
}

static const OptionInfoRec *
SMI_AvailableOptions(int chipid, int busid)
{
	ENTER_PROC("SMI_AvailableOptions");
	LEAVE_PROC("SMI_AvailableOptions");
	return(SMIOptions);
}

static void
SMI_Identify(int flags)
{
	ENTER_PROC("SMI_Identify");

	xf86PrintChipsets(SILICONMOTION_NAME, "driver (version "
			SILICONMOTION_VERSION_NAME ") for Silicon Motion Lynx chipsets",
			SMIChipsets);

	LEAVE_PROC("SMI_Identify");
}

static Bool
SMI_Probe(DriverPtr drv, int flags)
{
	int i;
	GDevPtr *devSections;
	int *usedChips;
	int numDevSections;
	int numUsed;
	Bool foundScreen = FALSE;

	ENTER_PROC("SMI_Probe");

	numDevSections = xf86MatchDevice(SILICONMOTION_DRIVER_NAME, &devSections);
	if (numDevSections <= 0)
	{
		/* There's no matching device section in the config file, so quit now.
		 */
		LEAVE_PROC("SMI_Probe");
		return(FALSE);
	}

	if (xf86GetPciVideoInfo() == NULL)
	{
		LEAVE_PROC("SMI_Probe");
		return(FALSE);
	}

	numUsed = xf86MatchPciInstances(SILICONMOTION_NAME, PCI_SMI_VENDOR_ID,
									SMIChipsets, SMIPciChipsets, devSections,
									numDevSections, drv, &usedChips);

	/* Free it since we don't need that list after this */
	xfree(devSections);
	if (numUsed <= 0)
	{
		LEAVE_PROC("SMI_Probe");
		return(FALSE);
	}

	if (flags & PROBE_DETECT)
	{
		foundScreen = TRUE;
	}
	else
	{
		for (i = 0; i < numUsed; i++)
		{
			/* Allocate a ScrnInfoRec and claim the slot */
			ScrnInfoPtr pScrn = xf86AllocateScreen(drv, 0);

			/* Fill in what we can of the ScrnInfoRec */
			pScrn->driverVersion = SILICONMOTION_DRIVER_VERSION;
			pScrn->driverName	 = SILICONMOTION_DRIVER_NAME;
			pScrn->name			 = SILICONMOTION_NAME;
			pScrn->Probe		 = SMI_Probe;
			pScrn->PreInit		 = SMI_PreInit;
			pScrn->ScreenInit	 = SMI_ScreenInit;
			pScrn->SwitchMode	 = SMI_SwitchMode;
			pScrn->AdjustFrame	 = SMI_AdjustFrame;
			pScrn->EnterVT		 = SMI_EnterVT;
			pScrn->LeaveVT		 = SMI_LeaveVT;
			pScrn->FreeScreen	 = SMI_FreeScreen;
			pScrn->ValidMode	 = SMI_ValidMode;
			foundScreen			 = TRUE;

			xf86ConfigActivePciEntity(pScrn, usedChips[i], SMIPciChipsets, NULL,
									  NULL, NULL, NULL, NULL);
		}
	}
	xfree(usedChips);

	LEAVE_PROC("SMI_Probe");
	return(foundScreen);
}

static Bool
SMI_PreInit(ScrnInfoPtr pScrn, int flags)
{
	EntityInfoPtr pEnt;
	SMIPtr pSmi;
	MessageType from;
	int i;
	double real;
	ClockRangePtr clockRanges;
#ifndef USE_FB
	char *mod = NULL;
	const char *reqSym = NULL;
#endif
	char *s;
	unsigned char config, m, n, shift;
	int mclk;
	vgaHWPtr hwp;
	int vgaCRIndex, vgaIOBase;
	vbeInfoPtr pVbe = NULL;
	
	ENTER_PROC("SMI_PreInit");

	if (flags & PROBE_DETECT)
	{
		SMI_ProbeDDC(pScrn, xf86GetEntityInfo(pScrn->entityList[0])->index);
		LEAVE_PROC("SMI_PreInit");
		return(TRUE);
	}

	/* Ignoring the Type list for now.  It might be needed when multiple cards
	 * are supported.
	 */
	if (pScrn->numEntities > 1)
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* The vgahw module should be loaded here when needed */
	if (!xf86LoadSubModule(pScrn, "vgahw"))
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	xf86LoaderReqSymLists(vgahwSymbols, NULL);

	/*
	 * Allocate a vgaHWRec
	 */
	if (!vgaHWGetHWRec(pScrn))
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* Allocate the SMIRec driverPrivate */
	if (!SMI_GetRec(pScrn))
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}
	pSmi = SMIPTR(pScrn);

	/* Set pScrn->monitor */
	pScrn->monitor = pScrn->confScreen->monitor;

	/*
	 * The first thing we should figure out is the depth, bpp, etc.
	 * We support only 24bpp layouts, so indicate that.
	 */
	if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support24bppFb))
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* Check that the returned depth is one we support */
	switch (pScrn->depth)
	{
		case 8:
		case 16:
		case 24:
			/* OK */
			break;

		default:
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
					"Given depth (%d) is not supported by this driver\n",
					pScrn->depth);
			LEAVE_PROC("SMI_PreInit");
			return(FALSE);
	}
	xf86PrintDepthBpp(pScrn);

	/*
	 * This must happen after pScrn->display has been set because
	 * xf86SetWeight references it.
	 */
	if (pScrn->depth > 8)
	{
		/* The defaults are OK for us */
		rgb zeros = {0, 0, 0};

		if (!xf86SetWeight(pScrn, zeros, zeros))
		{
			LEAVE_PROC("SMI_PreInit");
			return(FALSE);
		}
	}

	if (!xf86SetDefaultVisual(pScrn, -1))
	{
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* We don't currently support DirectColor at > 8bpp */
	if ((pScrn->depth > 8) && (pScrn->defaultVisual != TrueColor))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Given default visual (%s) "
				"is not supported at depth %d\n",
				xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* We use a programmable clock */
	pScrn->progClock = TRUE;

	/* Collect all of the relevant option flags (fill in pScrn->options) */
	xf86CollectOptions(pScrn, NULL);

	/* Set the bits per RGB for 8bpp mode */
	if (pScrn->depth == 8)
	{
		pScrn->rgbBits = 6;
	}

	/* Process the options */
	if (!(pSmi->Options = xalloc(sizeof(SMIOptions))))
		return FALSE;
	memcpy(pSmi->Options, SMIOptions, sizeof(SMIOptions));
	xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pSmi->Options);

	if (xf86ReturnOptValBool(pSmi->Options, OPTION_PCI_BURST, FALSE))
	{
		pSmi->pci_burst = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: pci_burst - PCI burst "
				"read enabled\n");
	}
	else
	{
		pSmi->pci_burst = FALSE;
	}

	pSmi->NoPCIRetry = TRUE;
	if (xf86ReturnOptValBool(pSmi->Options, OPTION_PCI_RETRY, FALSE))
	{
		if (xf86ReturnOptValBool(pSmi->Options, OPTION_PCI_BURST, FALSE))
		{
			pSmi->NoPCIRetry = FALSE;
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: pci_retry\n");
		}
		else
		{
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "\"pci_retry\" option "
					"requires \"pci_burst\".\n");
		}
	}

	if (xf86IsOptionSet(pSmi->Options, OPTION_FIFO_CONSERV))
	{
		pSmi->fifo_conservative = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: fifo_conservative "
				"set\n");
	}
	else
	{
		pSmi->fifo_conservative = FALSE;
	}

	if (xf86IsOptionSet(pSmi->Options, OPTION_FIFO_MODERATE))
	{
		pSmi->fifo_moderate = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: fifo_moderate set\n");
	}
	else
	{
		pSmi->fifo_moderate = FALSE;
	}

	if (xf86IsOptionSet(pSmi->Options, OPTION_FIFO_AGGRESSIVE))
	{
		pSmi->fifo_aggressive = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: fifo_aggressive set\n");
	}
	else
	{
		pSmi->fifo_aggressive = FALSE;
	}

	if (xf86ReturnOptValBool(pSmi->Options, OPTION_NOACCEL, FALSE))
	{
		pSmi->NoAccel = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: NoAccel - Acceleration "
				"disabled\n");
	}
	else
	{
		pSmi->NoAccel = FALSE;
	}

	if (xf86ReturnOptValBool(pSmi->Options, OPTION_SHOWCACHE, FALSE))
	{
		pSmi->ShowCache = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: show_cache set\n");
	}
	else
	{
		pSmi->ShowCache = FALSE;
	}

	if (xf86GetOptValFreq(pSmi->Options, OPTION_MCLK, OPTUNITS_MHZ, &real))
	{
		pSmi->MCLK = (int)(real * 1000.0);
		if (pSmi->MCLK <= 120000)
		{
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: set_mclk set to "
					"%1.3f MHz\n", pSmi->MCLK / 1000.0);
		}
		else
		{
			xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Memory Clock value of "
					"%1.3f MHz is larger than limit of 120 MHz\n",
					pSmi->MCLK / 1000.0);
			pSmi->MCLK = 0;
		}
	}
	else
	{
		pSmi->MCLK = 0;
	}

	from = X_DEFAULT;
	pSmi->hwcursor = TRUE;
	if (xf86GetOptValBool(pSmi->Options, OPTION_HWCURSOR, &pSmi->hwcursor))
	{
		from = X_CONFIG;
	}
	if (xf86ReturnOptValBool(pSmi->Options, OPTION_SWCURSOR, FALSE))
	{
		pSmi->hwcursor = FALSE;
		from = X_CONFIG;
	}
	xf86DrvMsg(pScrn->scrnIndex, from, "Using %s Cursor\n",
			pSmi->hwcursor ? "Hardware" : "Software");

	if (xf86GetOptValBool(pSmi->Options, OPTION_SHADOW_FB, &pSmi->shadowFB))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ShadowFB %s.\n",
				pSmi->shadowFB ? "enabled" : "disabled");
	}

#if 1 /* PDR#932 */
	if ((pScrn->depth == 8) || (pScrn->depth == 16))
#endif /* PDR#932 */
	if ((s = xf86GetOptValString(pSmi->Options, OPTION_ROTATE)))
	{
		if(!xf86NameCmp(s, "CW"))
		{
			pSmi->shadowFB = TRUE;
			pSmi->rotate = SMI_ROTATE_CCW;
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen "
					"clockwise\n");
		}
		else if (!xf86NameCmp(s, "CCW"))
		{
			pSmi->shadowFB = TRUE;
			pSmi->rotate = SMI_ROTATE_CW;
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Rotating screen counter "
					"clockwise\n");
		}
		else
		{
			xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "\"%s\" is not a valid "
					"value for Option \"Rotate\"\n", s);
			xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Valid options are \"CW\" or "
					"\"CCW\"\n");
		}
	}

	if (pSmi->rotate)
	{
		/* Disable the RandR extension, it messes up the internal rotation stuff */
		xf86DisableRandR();
	}
		
	if (xf86GetOptValInteger(pSmi->Options, OPTION_VIDEOKEY, &pSmi->videoKey))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: Video key set to "
				"0x%08X\n", pSmi->videoKey);
	}
	else
	{
		pSmi->videoKey = (1 << pScrn->offset.red) | (1 << pScrn->offset.green)
				| (((pScrn->mask.blue >> pScrn->offset.blue) - 1)
						<< pScrn->offset.blue);
	}

	if (xf86ReturnOptValBool(pSmi->Options, OPTION_BYTESWAP, FALSE))
	{
		pSmi->ByteSwap = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: ByteSwap enabled.\n");
	}
	else
	{
		pSmi->ByteSwap = FALSE;
	}

	/* CZ 26.10.2001: interlaced video */
	if (xf86ReturnOptValBool(pSmi->Options, OPTION_INTERLACED, FALSE))
	{
		pSmi->interlaced = TRUE;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: Interlaced enabled.\n");
	}
	else
	{
		pSmi->interlaced = FALSE;
	}
	/* end CZ */

	if (xf86GetOptValBool(pSmi->Options, OPTION_USEBIOS, &pSmi->useBIOS))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: UseBIOS %s.\n",
				pSmi->useBIOS ? "enabled" : "disabled");
	}
	else
	{
		/* Default to UseBIOS enabled. */
		pSmi->useBIOS = TRUE;
	}

	if (xf86GetOptValBool(pSmi->Options, OPTION_ZOOMONLCD, &pSmi->zoomOnLCD))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Option: ZoomOnLCD %s.\n",
				pSmi->zoomOnLCD ? "enabled" : "disabled");
	}
	else
	{
		/* Default to ZoomOnLCD enabled. */
		pSmi->zoomOnLCD = TRUE;
	}

	/* Find the PCI slot for this screen */
	pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
	if ((pEnt->location.type != BUS_PCI) || (pEnt->resources))
	{
		xfree(pEnt);
		SMI_FreeRec(pScrn);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	if (xf86LoadSubModule(pScrn,"int10")) {
	    xf86LoaderReqSymLists(int10Symbols,NULL);
	    pSmi->pInt10 = xf86InitInt10(pEnt->index);
	}
	
	if (pSmi->pInt10 && xf86LoadSubModule(pScrn, "vbe"))
	{
	    xf86LoaderReqSymLists(vbeSymbols, NULL);
	    pVbe = VBEInit(pSmi->pInt10, pEnt->index);
	}

	pSmi->PciInfo = xf86GetPciInfoForEntity(pEnt->index);
	xf86RegisterResources(pEnt->index, NULL, ResExclusive);
/*	xf86SetOperatingState(resVgaIo, pEnt->index, ResUnusedOpr); */
/*	xf86SetOperatingState(resVgaMem, pEnt->index, ResDisableOpr); */

	/*
	 * Set the Chipset and ChipRev, allowing config file entries to
	 * override.
	 */
	if (pEnt->device->chipset && *pEnt->device->chipset)
	{
		pScrn->chipset = pEnt->device->chipset;
		pSmi->Chipset = xf86StringToToken(SMIChipsets, pScrn->chipset);
		from = X_CONFIG;
	}
	else if (pEnt->device->chipID >= 0)
	{
		pSmi->Chipset = pEnt->device->chipID;
		pScrn->chipset = (char *) xf86TokenToString(SMIChipsets, pSmi->Chipset);
		from = X_CONFIG;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipID override: 0x%04X\n",
				pSmi->Chipset);
	}
	else
	{
		from = X_PROBED;
		pSmi->Chipset = pSmi->PciInfo->chipType;
		pScrn->chipset = (char *) xf86TokenToString(SMIChipsets, pSmi->Chipset);
	}

	if (pEnt->device->chipRev >= 0)
	{
		pSmi->ChipRev = pEnt->device->chipRev;
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipRev override: %d\n",
				pSmi->ChipRev);
	}
	else
	{
		pSmi->ChipRev = pSmi->PciInfo->chipRev;
	}
	xfree(pEnt);

	/*
	 * This shouldn't happen because such problems should be caught in
	 * SMI_Probe(), but check it just in case.
	 */
	if (pScrn->chipset == NULL)
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "ChipID 0x%04X is not "
				"recognised\n", pSmi->Chipset);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}
	if (pSmi->Chipset < 0)
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Chipset \"%s\" is not "
				"recognised\n", pScrn->chipset);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	xf86DrvMsg(pScrn->scrnIndex, from, "Chipset: \"%s\"\n", pScrn->chipset);

	pSmi->PciTag = pciTag(pSmi->PciInfo->bus, pSmi->PciInfo->device,
			pSmi->PciInfo->func);

	SMI_MapMem(pScrn);
	SMI_DisableVideo(pScrn);

	hwp = VGAHWPTR(pScrn);
	vgaIOBase  = hwp->IOBase;
	vgaCRIndex = vgaIOBase + VGA_CRTC_INDEX_OFFSET;
	pSmi->PIOBase = hwp->PIOOffset;

	xf86ErrorFVerb(VERBLEV, "\tSMI_PreInit vgaCRIndex=%x, vgaIOBase=%x, "
			"MMIOBase=%p\n", vgaCRIndex, vgaIOBase, hwp->MMIOBase);

	/* Next go on to detect amount of installed ram */
	config = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x71);

	if (xf86LoadSubModule(pScrn, "i2c"))
	{
		xf86LoaderReqSymLists(i2cSymbols, NULL);
		SMI_I2CInit(pScrn);
	}
	if (xf86LoadSubModule(pScrn, "ddc"))
	{
		xf86MonPtr pMon = NULL;

		xf86LoaderReqSymLists(ddcSymbols, NULL);
#if 1 /* PDR#579 */
		if (pVbe)
		{
			pMon = vbeDoEDID(pVbe, NULL);
			if (pMon != NULL)
			{
				if (   (pMon->rawData[0] == 0x00)
					&& (pMon->rawData[1] == 0xFF)
					&& (pMon->rawData[2] == 0xFF)
					&& (pMon->rawData[3] == 0xFF)
					&& (pMon->rawData[4] == 0xFF)
					&& (pMon->rawData[5] == 0xFF)
					&& (pMon->rawData[6] == 0xFF)
					&& (pMon->rawData[7] == 0x00)
				)
				{
					pMon = xf86PrintEDID(pMon);
					if (pMon != NULL)
					{
						xf86SetDDCproperties(pScrn, pMon);
					}
				}
			}
		}
#else
		if (   (pVbe)
			&& ((pMon = xf86PrintEDID(vbeDoEDID(pVbe, NULL))) != NULL)
		)
		{
			xf86SetDDCproperties(pScrn, pMon);
		}
#endif
		else if (!SMI_ddc1(pScrn->scrnIndex))
		{
			if (pSmi->I2C)
			{
				xf86SetDDCproperties(pScrn,
						xf86PrintEDID(xf86DoEDID_DDC2(pScrn->scrnIndex,
								pSmi->I2C)));
			}
		}
	}

	vbeFree(pVbe);
	xf86FreeInt10(pSmi->pInt10);
	pSmi->pInt10 = NULL;

	/*
	 * If the driver can do gamma correction, it should call xf86SetGamma()
	 * here. (from MGA, no ViRGE gamma support yet, but needed for
	 * xf86HandleColormaps support.)
	 */
	{
		Gamma zeros = { 0.0, 0.0, 0.0 };

		if (!xf86SetGamma(pScrn, zeros))
		{
			LEAVE_PROC("SMI_PreInit");
			SMI_EnableVideo(pScrn);
			SMI_UnmapMem(pScrn);
			return(FALSE);
		}
	}

	/* And compute the amount of video memory and offscreen memory */
	pSmi->videoRAMKBytes = 0;

	if (!pScrn->videoRam)
	{
		switch (pSmi->Chipset)
		{
			default:
			{
				int mem_table[4] = { 1, 2, 4, 0 };
				pSmi->videoRAMKBytes = mem_table[(config >> 6)] * 1024;
				break;
			}

			case SMI_LYNX3D:
			{
				int mem_table[4] = { 0, 2, 4, 6 };
				pSmi->videoRAMKBytes = mem_table[(config >> 6)] * 1024 + 512;
				break;
			}

			case SMI_LYNX3DM:
			{
				int mem_table[4] = { 16, 2, 4, 8 };
				pSmi->videoRAMKBytes = mem_table[(config >> 6)] * 1024;
				break;
			}

			case SMI_COUGAR3DR:
			{
				/* DANGER - Cougar3DR BIOS is broken - hardcode video ram size */
				/* per instructions from Silicon Motion engineers */
				pSmi->videoRAMKBytes = 16 * 1024;
				break;
            }
		}
		pSmi->videoRAMBytes = pSmi->videoRAMKBytes * 1024;
		pScrn->videoRam     = pSmi->videoRAMKBytes;

		xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "videoram: %dkB\n",
				pSmi->videoRAMKBytes);
	}
	else
	{
		pSmi->videoRAMKBytes = pScrn->videoRam;
		pSmi->videoRAMBytes  = pScrn->videoRam * 1024;

		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "videoram: %dk\n",
				pSmi->videoRAMKBytes);
	}

	/* Lynx built-in ramdac speeds */
	pScrn->numClocks = 4;

	if ((pScrn->clock[3] <= 0) && (pScrn->clock[2] > 0))
	{
		pScrn->clock[3] = pScrn->clock[2];
	}

    if ((pSmi->Chipset == SMI_LYNX3DM) || (pSmi->Chipset == SMI_COUGAR3DR))
	{
		if (pScrn->clock[0] <= 0) pScrn->clock[0] = 200000;
		if (pScrn->clock[1] <= 0) pScrn->clock[1] = 200000;
		if (pScrn->clock[2] <= 0) pScrn->clock[2] = 200000;
		if (pScrn->clock[3] <= 0) pScrn->clock[3] = 200000;
	}
	else
	{
		if (pScrn->clock[0] <= 0) pScrn->clock[0] = 135000;
		if (pScrn->clock[1] <= 0) pScrn->clock[1] = 135000;
		if (pScrn->clock[2] <= 0) pScrn->clock[2] = 135000;
		if (pScrn->clock[3] <= 0) pScrn->clock[3] = 135000;
	}

	/* Now set RAMDAC limits */
	switch (pSmi->Chipset)
	{
		default:
			pSmi->minClock = 20000;
			pSmi->maxClock = 135000;
			break;
	}
	xf86ErrorFVerb(VERBLEV, "\tSMI_PreInit minClock=%d, maxClock=%d\n",
			pSmi->minClock, pSmi->maxClock);

	/* Detect current MCLK and print it for user */
	m = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6A);
	n = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6B);
	switch (n >> 6)
	{
		default:
			shift = 1;
			break;

		case 1:
			shift = 4;
			break;

		case 2:
			shift = 2;
			break;
	}
	n &= 0x3F;
	mclk = ((1431818 * m) / n / shift + 50) / 100;
	xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Detected current MCLK value of "
			"%1.3f MHz\n", mclk / 1000.0);

	SMI_EnableVideo(pScrn);
	SMI_UnmapMem(pScrn);

	pScrn->virtualX = pScrn->display->virtualX;

	/*
	 * Setup the ClockRanges, which describe what clock ranges are available,
	 * and what sort of modes they can be used for.
	 */
	clockRanges = xnfcalloc(sizeof(ClockRange),1);
	clockRanges->next = NULL;
	clockRanges->minClock = pSmi->minClock;
	clockRanges->maxClock = pSmi->maxClock;
	clockRanges->clockIndex = -1;
	clockRanges->interlaceAllowed = FALSE;
	clockRanges->doubleScanAllowed = FALSE;
		
	i = xf86ValidateModes(
			pScrn,						/* Screen pointer					  */
			pScrn->monitor->Modes,		/* Available monitor modes			  */
			pScrn->display->modes,		/* req mode names for screen		  */
			clockRanges,				/* list of clock ranges allowed		  */
			NULL,						/* use min/max below				  */
			128,						/* min line pitch (width)			  */
			4096,						/* maximum line pitch (width)		  */
			128,						/* bits of granularity for line pitch */
										/* (width) above					  */
			128,						/* min virtual height				  */
			4096,						/* max virtual height				  */
			pScrn->display->virtualX,	/* force virtual x					  */
			pScrn->display->virtualY,	/* force virtual Y					  */
			pSmi->videoRAMBytes,		/* size of aperture used to access	  */
										/* video memory						  */
			LOOKUP_BEST_REFRESH);		/* how to pick modes				  */

	if (i == -1)
	{
		SMI_FreeRec(pScrn);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	/* Prune the modes marked as invalid */
	xf86PruneDriverModes(pScrn);

	if ((i == 0) || (pScrn->modes == NULL))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
		SMI_FreeRec(pScrn);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}
	xf86SetCrtcForModes(pScrn, 0);

	/* Set the current mode to the first in the list */
	pScrn->currentMode = pScrn->modes;

	/* Print the list of modes being used */
	xf86PrintModes(pScrn);

	/* Set display resolution */
	xf86SetDpi(pScrn, 0, 0);

#ifdef USE_FB
	if ((xf86LoadSubModule(pScrn, "fb") == NULL))
	{
		SMI_FreeRec(pScrn);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	xf86LoaderReqSymLists(fbSymbols, NULL);
#else
	/* Load bpp-specific modules */
	switch (pScrn->bitsPerPixel)
	{
		case 8:
			mod = "cfb";
			reqSym = "cfbScreenInit";
			break;

		case 16:
			mod = "cfb16";
			reqSym = "cfb16ScreenInit";
			break;

		case 24:
			mod = "cfb24";
			reqSym = "cfb24ScreenInit";
			break;
	}

	if (mod && (xf86LoadSubModule(pScrn, mod) == NULL))
	{
		SMI_FreeRec(pScrn);
		LEAVE_PROC("SMI_PreInit");
		return(FALSE);
	}

	xf86LoaderReqSymbols(reqSym, NULL);
#endif
	/* Load XAA if needed */
	if (!pSmi->NoAccel || pSmi->hwcursor)
	{
		if (!xf86LoadSubModule(pScrn, "xaa"))
		{
			SMI_FreeRec(pScrn);
			LEAVE_PROC("SMI_PreInit");
			return(FALSE);
		}
		xf86LoaderReqSymLists(xaaSymbols, NULL);
	}

	/* Load ramdac if needed */
	if (pSmi->hwcursor)
	{
		if (!xf86LoadSubModule(pScrn, "ramdac"))
		{
			SMI_FreeRec(pScrn);
			LEAVE_PROC("SMI_PreInit");
			return(FALSE);
		}
		xf86LoaderReqSymLists(ramdacSymbols, NULL);
	}

	if (pSmi->shadowFB)
	{
		if (!xf86LoadSubModule(pScrn, "shadowfb"))
		{
			SMI_FreeRec(pScrn);
			LEAVE_PROC("SMI_PreInit");
			return(FALSE);
		}
		xf86LoaderReqSymLists(shadowSymbols, NULL);
	}

	LEAVE_PROC("SMI_PreInit");
	return(TRUE);
}

/*
 * This is called when VT switching back to the X server.  Its job is to
 * reinitialise the video mode. We may wish to unmap video/MMIO memory too.
 */

static Bool
SMI_EnterVT(int scrnIndex, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	SMIPtr pSmi = SMIPTR(pScrn);
	Bool ret;

	ENTER_PROC("SMI_EnterVT");

	/* Enable MMIO and map memory */
	SMI_MapMem(pScrn);
	SMI_Save(pScrn);

	/* #670 */
	if (pSmi->shadowFB)
	{
		pSmi->FBOffset = pSmi->savedFBOffset;
		pSmi->FBReserved = pSmi->savedFBReserved;
	}

	ret = SMI_ModeInit(pScrn, pScrn->currentMode);

	/* #670 */
	if (ret && pSmi->shadowFB)
	{
		BoxRec box;

		/* #920 */
		if (pSmi->paletteBuffer)
		{
			int i;

			VGAOUT8(pSmi, VGA_DAC_WRITE_ADDR, 0);
			for(i = 0; i < 256 * 3; i++)
			{
				VGAOUT8(pSmi, VGA_DAC_DATA, pSmi->paletteBuffer[i]);
			}
			xfree(pSmi->paletteBuffer);
			pSmi->paletteBuffer = NULL;
		}

		if (pSmi->pSaveBuffer)
		{
			memcpy(pSmi->FBBase, pSmi->pSaveBuffer, pSmi->saveBufferSize);
			xfree(pSmi->pSaveBuffer);
			pSmi->pSaveBuffer = NULL;
		}

		box.x1 = 0;
		box.y1 = 0;
		box.x2 = pScrn->virtualY;
		box.y2 = pScrn->virtualX;
		if (pSmi->Chipset == SMI_COUGAR3DR)
		{
			SMI_RefreshArea730(pScrn, 1, &box);
		}
		else
		{
			SMI_RefreshArea(pScrn, 1, &box);
		}
	}

	/* Reset the grapics engine */
	if (!pSmi->NoAccel)
	    SMI_EngineReset(pScrn);

	LEAVE_PROC("SMI_EnterVT");
	return(ret);
}

/*
 * This is called when VT switching away from the X server.  Its job is to
 * restore the previous (text) mode. We may wish to remap video/MMIO memory
 * too.
 */

static void
SMI_LeaveVT(int scrnIndex, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	vgaRegPtr vgaSavePtr = &hwp->SavedReg;
	SMIRegPtr SMISavePtr = &pSmi->SavedReg;

	ENTER_PROC("SMI_LeaveVT");

	/* #670 */
	if (pSmi->shadowFB)
	{
		pSmi->pSaveBuffer = xnfalloc(pSmi->saveBufferSize);
		if (pSmi->pSaveBuffer)
		{
			memcpy(pSmi->pSaveBuffer, pSmi->FBBase, pSmi->saveBufferSize);
		}

		pSmi->savedFBOffset = pSmi->FBOffset;
		pSmi->savedFBReserved = pSmi->FBReserved;

		/* #920 */
		if (pSmi->Bpp == 1)
		{
			pSmi->paletteBuffer = xnfalloc(256 * 3);
			if (pSmi->paletteBuffer)
			{
				int i;

				VGAOUT8(pSmi, VGA_DAC_READ_ADDR, 0);
				for (i = 0; i < 256 * 3; i++)
				{
					pSmi->paletteBuffer[i] = VGAIN8(pSmi, VGA_DAC_DATA);
				}
			}
		}
	}

	memset(pSmi->FBBase, 0, 256 * 1024);	/* #689 */
	SMI_WriteMode(pScrn, vgaSavePtr, SMISavePtr);
	SMI_UnmapMem(pScrn);

	LEAVE_PROC("SMI_LeaveVT");
}

/*
 * This function performs the inverse of the restore function: It saves all the
 * standard and extended registers that we are going to modify to set up a video
 * mode.
 */

static void
SMI_Save(ScrnInfoPtr pScrn)
{
	int i;
	CARD32 offset;

	vgaHWPtr hwp         = VGAHWPTR(pScrn);
	vgaRegPtr vgaSavePtr = &hwp->SavedReg;
	SMIPtr pSmi          = SMIPTR(pScrn);
	SMIRegPtr save       = &pSmi->SavedReg;

	int vgaIOBase  = hwp->IOBase;
	int vgaCRIndex = vgaIOBase + VGA_CRTC_INDEX_OFFSET;
	int vgaCRData  = vgaIOBase + VGA_CRTC_DATA_OFFSET;

	ENTER_PROC("SMI_Save");

	/* Save the standard VGA registers */
	vgaHWSave(pScrn, vgaSavePtr, VGA_SR_ALL);
	save->smiDACMask = VGAIN8(pSmi, VGA_DAC_MASK);
	VGAOUT8(pSmi, VGA_DAC_READ_ADDR, 0);
	for (i = 0; i < 256; i++)
	{
		save->smiDacRegs[i][0] = VGAIN8(pSmi, VGA_DAC_DATA);
		save->smiDacRegs[i][1] = VGAIN8(pSmi, VGA_DAC_DATA);
		save->smiDacRegs[i][2] = VGAIN8(pSmi, VGA_DAC_DATA);
	}
	for (i = 0, offset = 2; i < 8192; i++, offset += 8)
	{
		save->smiFont[i] = *(pSmi->FBBase + offset);
	}

	/* Now we save all the extended registers we need. */
	save->SR17 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x17);
	save->SR18 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x18);
	save->SR21 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21);
	save->SR31 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31);
	save->SR32 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x32);
	save->SR6A = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6A);
	save->SR6B = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6B);
	save->SR81 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x81);
	save->SRA0 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0xA0);

	if (SMI_LYNXM_SERIES(pSmi->Chipset))
	{
		/* Save primary registers */
		save->CR90[14] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E);
		VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E,
				save->CR90[14] & ~0x20);

		for (i = 0; i < 16; i++)
		{
			save->CR90[i] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x90 + i);
		}
		save->CR33 = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33);
		save->CR3A = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x3A);
		for (i = 0; i < 14; i++)
		{
			save->CR40[i] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x40 + i);
		}

		/* Save secondary registers */
		VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E, save->CR90[14] | 0x20);
		save->CR33_2 = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33);
		for (i = 0; i < 14; i++)
		{
			save->CR40_2[i] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData,
					0x40 + i);
		}
		save->CR9F_2 = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9F);

		/* Save common registers */
		for (i = 0; i < 14; i++)
		{
			save->CRA0[i] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0xA0 + i);
		}

		/* PDR#1069 */
		VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E, save->CR90[14]);
	}
	else
	{
		save->CR33 = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33);
		save->CR3A = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x3A);
		for (i = 0; i < 14; i++)
		{
			save->CR40[i] = VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x40 + i);
		}
	}

	/* CZ 2.11.2001: for gamma correction (TODO: other chipsets?) */
	if ((pSmi->Chipset == SMI_LYNX3DM) || (pSmi->Chipset == SMI_COUGAR3DR)) {
		save->CCR66 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x66);
	}
	/* end CZ */

	save->DPR10 = READ_DPR(pSmi, 0x10);
	save->DPR1C = READ_DPR(pSmi, 0x1C);
	save->DPR20 = READ_DPR(pSmi, 0x20);
	save->DPR24 = READ_DPR(pSmi, 0x24);
	save->DPR28 = READ_DPR(pSmi, 0x28);
	save->DPR2C = READ_DPR(pSmi, 0x2C);
	save->DPR30 = READ_DPR(pSmi, 0x30);
	save->DPR3C = READ_DPR(pSmi, 0x3C);
	save->DPR40 = READ_DPR(pSmi, 0x40);
	save->DPR44 = READ_DPR(pSmi, 0x44);

	save->VPR00 = READ_VPR(pSmi, 0x00);
	save->VPR0C = READ_VPR(pSmi, 0x0C);
	save->VPR10 = READ_VPR(pSmi, 0x10);

	if (pSmi->Chipset == SMI_COUGAR3DR)
	{
		save->FPR00_ = READ_FPR(pSmi, FPR00); 
		save->FPR0C_ = READ_FPR(pSmi, FPR0C);
		save->FPR10_ = READ_FPR(pSmi, FPR10);
	}

	save->CPR00 = READ_CPR(pSmi, 0x00);

	if (!pSmi->ModeStructInit)
	{
		/* XXX Should check the return value of vgaHWCopyReg() */
		vgaHWCopyReg(&hwp->ModeReg, vgaSavePtr);
		memcpy(&pSmi->ModeReg, save, sizeof(SMIRegRec));
		pSmi->ModeStructInit = TRUE;
	}

	if (pSmi->useBIOS && (pSmi->pInt10 != NULL))
	{
		pSmi->pInt10->num = 0x10;
		pSmi->pInt10->ax = 0x0F00;
		xf86ExecX86int10(pSmi->pInt10);
		save->mode = pSmi->pInt10->ax & 0x007F;
		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Current mode 0x%02X.\n",
				save->mode);
	}

	if (xf86GetVerbosity() > 1)
	{
		xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
				"Saved current video mode.  Register dump:\n");
		SMI_PrintRegs(pScrn);
	}

	LEAVE_PROC("SMI_Save");
}

/*
 * This function is used to restore a video mode. It writes out all of the
 * standard VGA and extended registers needed to setup a video mode.
 */

static void
SMI_WriteMode(ScrnInfoPtr pScrn, vgaRegPtr vgaSavePtr, SMIRegPtr restore)
{
	int i;
	CARD8 tmp;
	CARD32 offset;

	vgaHWPtr hwp   = VGAHWPTR(pScrn);
	SMIPtr pSmi    = SMIPTR(pScrn);
	int vgaIOBase  = hwp->IOBase;
	int vgaCRIndex = vgaIOBase + VGA_CRTC_INDEX_OFFSET;
	int vgaCRData  = vgaIOBase + VGA_CRTC_DATA_OFFSET;

	ENTER_PROC("SMI_WriteMode");

	vgaHWProtect(pScrn, TRUE);

	/* Wait for engine to become idle */
	WaitIdle();

	if (pSmi->useBIOS && (pSmi->pInt10 != NULL)
	    && (restore->mode != 0))
	{
		pSmi->pInt10->num = 0x10;
		pSmi->pInt10->ax = restore->mode | 0x80;
		xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Setting mode 0x%02X\n",
				restore->mode);
		xf86ExecX86int10(pSmi->pInt10);

		/* Enable linear mode. */
		outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x18);
		tmp = inb(pSmi->PIOBase + VGA_SEQ_DATA);
		outb(pSmi->PIOBase + VGA_SEQ_DATA, tmp | 0x01);

		/* Enable DPR/VPR registers. */
		tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21);
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21, tmp & ~0x03);
	}
	else
	{
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x17, restore->SR17);
		tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x18) & ~0x1F;
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x18, tmp |
				(restore->SR18 & 0x1F));
		tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21);
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21, tmp & ~0x03);
		tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31) & ~0xC0;
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31, tmp |
				(restore->SR31 & 0xC0));
		tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x32) & ~0x07;
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x32, tmp |
				(restore->SR32 & 0x07));
		if (restore->SR6B != 0xFF)
		{
			VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6A,
					restore->SR6A);
			VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x6B,
					restore->SR6B);
		}
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x81, restore->SR81);
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0xA0, restore->SRA0);

		/* Restore the standard VGA registers */
		vgaHWRestore(pScrn, vgaSavePtr, VGA_SR_ALL);
		if (restore->smiDACMask)
		{
			VGAOUT8(pSmi, VGA_DAC_MASK, restore->smiDACMask);
		}
		else
		{
			VGAOUT8(pSmi, VGA_DAC_MASK, 0xFF);
		}
		VGAOUT8(pSmi, VGA_DAC_WRITE_ADDR, 0);
		for (i = 0; i < 256; i++)
		{
			VGAOUT8(pSmi, VGA_DAC_DATA, restore->smiDacRegs[i][0]);
			VGAOUT8(pSmi, VGA_DAC_DATA, restore->smiDacRegs[i][1]);
			VGAOUT8(pSmi, VGA_DAC_DATA, restore->smiDacRegs[i][2]);
		}
		for (i = 0, offset = 2; i < 8192; i++, offset += 8)
		{
			*(pSmi->FBBase + offset) = restore->smiFont[i];
		}

		if (SMI_LYNXM_SERIES(pSmi->Chipset))
		{
			/* Restore secondary registers */
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E,
					restore->CR90[14] | 0x20);

			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33, restore->CR33_2);
			for (i = 0; i < 14; i++)
			{
				VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x40 + i,
						restore->CR40_2[i]);
			}
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9F, restore->CR9F_2);

			/* Restore primary registers */
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E,
					restore->CR90[14] & ~0x20);

			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33, restore->CR33);
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x3A, restore->CR3A);
			for (i = 0; i < 14; i++)
			{
				VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x40 + i,
						restore->CR40[i]);
			}
			for (i = 0; i < 16; i++)
			{
				if (i != 14)
				{
					VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x90 + i,
							restore->CR90[i]);
				}
			}
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x9E, restore->CR90[14]);

			/* Restore common registers */
			for (i = 0; i < 14; i++)
			{
				VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0xA0 + i,
						restore->CRA0[i]);
			}
		}

		/* Restore the standard VGA registers */
		if (xf86IsPrimaryPci(pSmi->PciInfo)) {
		    vgaHWRestore(pScrn, vgaSavePtr, VGA_SR_CMAP 
				 | VGA_SR_FONTS);
		} 

		if (restore->modeInit)
		    vgaHWRestore(pScrn, vgaSavePtr, VGA_SR_ALL);

		if (!SMI_LYNXM_SERIES(pSmi->Chipset))
		{
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x33, restore->CR33);
			VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x3A, restore->CR3A);
			for (i = 0; i < 14; i++)
			{
				VGAOUT8_INDEX(pSmi, vgaCRIndex, vgaCRData, 0x40 + i,
						restore->CR40[i]);
			}
		}
	}

	/* CZ 2.11.2001: for gamma correction (TODO: other chipsets?) */
	if ((pSmi->Chipset == SMI_LYNX3DM) || (pSmi->Chipset == SMI_COUGAR3DR)) {
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x66, restore->CCR66);
	}
	/* end CZ */

	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x81, 0x00);

	WRITE_DPR(pSmi, 0x10, restore->DPR10);
	WRITE_DPR(pSmi, 0x1C, restore->DPR1C);
	WRITE_DPR(pSmi, 0x20, restore->DPR20);
	WRITE_DPR(pSmi, 0x24, restore->DPR24);
	WRITE_DPR(pSmi, 0x28, restore->DPR28);
	WRITE_DPR(pSmi, 0x2C, restore->DPR2C);
	WRITE_DPR(pSmi, 0x30, restore->DPR30);
	WRITE_DPR(pSmi, 0x3C, restore->DPR3C);
	WRITE_DPR(pSmi, 0x40, restore->DPR40);
	WRITE_DPR(pSmi, 0x44, restore->DPR44);

	WRITE_VPR(pSmi, 0x00, restore->VPR00);
	WRITE_VPR(pSmi, 0x0C, restore->VPR0C);
	WRITE_VPR(pSmi, 0x10, restore->VPR10);

	if(pSmi->Chipset == SMI_COUGAR3DR)
	{
		WRITE_FPR(pSmi, FPR00, restore->FPR00_);
		WRITE_FPR(pSmi, FPR0C, restore->FPR0C_);
		WRITE_FPR(pSmi, FPR10, restore->FPR10_);
	}

	WRITE_CPR(pSmi, 0x00, restore->CPR00);

	if (xf86GetVerbosity() > 1)
	{
		xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
				"Done restoring mode.  Register dump:\n");
		SMI_PrintRegs(pScrn);
	}

	vgaHWProtect(pScrn, FALSE);

	LEAVE_PROC("SMI_WriteMode");
}

static Bool
SMI_MapMem(ScrnInfoPtr pScrn)
{
	SMIPtr pSmi = SMIPTR(pScrn);
	vgaHWPtr hwp;
	CARD32 memBase;

	ENTER_PROC("SMI_MapMem");

	/* Map the Lynx register space */
	switch (pSmi->Chipset)
	{
		default:
			memBase = pSmi->PciInfo->memBase[0] + 0x400000;
			pSmi->MapSize = 0x10000;
			break;

		case SMI_COUGAR3DR:
			memBase = pSmi->PciInfo->memBase[1];
			pSmi->MapSize = 0x200000;
			break;

		case SMI_LYNX3D:
			memBase = pSmi->PciInfo->memBase[0] + 0x680000;
			pSmi->MapSize = 0x180000;
			break;

		case SMI_LYNXEM:
		case SMI_LYNXEMplus:
			memBase = pSmi->PciInfo->memBase[0] + 0x400000;
			pSmi->MapSize = 0x400000;
			break;

		case SMI_LYNX3DM:
			memBase = pSmi->PciInfo->memBase[0];
			pSmi->MapSize = 0x200000;
			break;
	}
	pSmi->MapBase = xf86MapPciMem(pScrn->scrnIndex, VIDMEM_MMIO, pSmi->PciTag,
			memBase, pSmi->MapSize);

	if (pSmi->MapBase == NULL)
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Internal error: could not map "
				"MMIO registers.\n");
		LEAVE_PROC("SMI_MapMem");
		return(FALSE);
	}

	switch (pSmi->Chipset)
	{
		default:
			pSmi->DPRBase = pSmi->MapBase + 0x8000;
			pSmi->VPRBase = pSmi->MapBase + 0xC000;
			pSmi->CPRBase = pSmi->MapBase + 0xE000;
			pSmi->IOBase  = NULL;
			pSmi->DataPortBase = pSmi->MapBase;
			pSmi->DataPortSize = 0x8000;
			break;

		case SMI_COUGAR3DR:
			pSmi->DPRBase = pSmi->MapBase + 0x000000;
			pSmi->VPRBase = pSmi->MapBase + 0x000800;
			pSmi->CPRBase = pSmi->MapBase + 0x001000;
			pSmi->FPRBase = pSmi->MapBase + 0x005800;
			pSmi->IOBase  = pSmi->MapBase + 0x0C0000;
			pSmi->DataPortBase = pSmi->MapBase + 0x100000;
			pSmi->DataPortSize = 0x100000;
			break;

		case SMI_LYNX3D:
			pSmi->DPRBase = pSmi->MapBase + 0x000000;
			pSmi->VPRBase = pSmi->MapBase + 0x000800;
			pSmi->CPRBase = pSmi->MapBase + 0x001000;
			pSmi->IOBase  = pSmi->MapBase + 0x040000;
			pSmi->DataPortBase = pSmi->MapBase + 0x080000;
			pSmi->DataPortSize = 0x100000;
			break;

		case SMI_LYNXEM:
		case SMI_LYNXEMplus:
			pSmi->DPRBase = pSmi->MapBase + 0x008000;
			pSmi->VPRBase = pSmi->MapBase + 0x00C000;
			pSmi->CPRBase = pSmi->MapBase + 0x00E000;
			pSmi->IOBase  = pSmi->MapBase + 0x300000;
			pSmi->DataPortBase = pSmi->MapBase /*+ 0x100000*/;
			pSmi->DataPortSize = 0x8000 /*0x200000*/;
			break;

		case SMI_LYNX3DM:
			pSmi->DPRBase = pSmi->MapBase + 0x000000;
			pSmi->VPRBase = pSmi->MapBase + 0x000800;
			pSmi->CPRBase = pSmi->MapBase + 0x001000;
			pSmi->IOBase  = pSmi->MapBase + 0x0C0000;
			pSmi->DataPortBase = pSmi->MapBase + 0x100000;
			pSmi->DataPortSize = 0x100000;
			break;
	}
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"Physical MMIO at 0x%08lX\n", (unsigned long)memBase);
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"Logical MMIO at %p - %p\n", pSmi->MapBase,
			pSmi->MapBase + pSmi->MapSize - 1);
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"DPR=%p, VPR=%p, IOBase=%p\n",
			pSmi->DPRBase, pSmi->VPRBase, pSmi->IOBase);
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"DataPort=%p - %p\n", pSmi->DataPortBase,
			pSmi->DataPortBase + pSmi->DataPortSize - 1);

	pScrn->memPhysBase = pSmi->PciInfo->memBase[0];
	
	/* Map the frame buffer */
	if (pSmi->Chipset == SMI_LYNX3DM) 
	    pSmi->fbMapOffset = 0x200000;
	else
	    pSmi->fbMapOffset = 0x0;
	
	if (pSmi->videoRAMBytes) {
	    pSmi->FBBase = xf86MapPciMem(pScrn->scrnIndex,
					 VIDMEM_FRAMEBUFFER,
					 pSmi->PciTag,
					 pScrn->memPhysBase + pSmi->fbMapOffset,
					 pSmi->videoRAMBytes);
	    
		if (pSmi->FBBase == NULL)
		{
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Internal error: could not "
					"map framebuffer.\n");
			LEAVE_PROC("SMI_MapMem");
			return(FALSE);
		}
	}
	pSmi->FBOffset = 0;
	pScrn->fbOffset = pSmi->FBOffset + pSmi->fbMapOffset;
	
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"Physical frame buffer at 0x%08lX offset: 0x%08lX\n",
		       pScrn->memPhysBase, pScrn->fbOffset);
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"Logical frame buffer at %p - %p\n", pSmi->FBBase,
			pSmi->FBBase + pSmi->videoRAMBytes - 1);

	SMI_EnableMmio(pScrn);

	/* Set up offset to hwcursor memory area.  It's a 1K chunk at the end of
	 * the frame buffer.  Also set up the reserved memory space.
	 */
	pSmi->FBCursorOffset = pSmi->videoRAMBytes - 1024;
	if (VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x30) & 0x01)/* #1074 */
	{
		CARD32 fifoOffset = 0;
		fifoOffset |= VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x46)
				<< 3;
		fifoOffset |= VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x47)
				<< 11;
		fifoOffset |= (VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x49)
				& 0x1C) << 17;
		pSmi->FBReserved = fifoOffset;	/* PDR#1074 */
	}
	else
	{
		pSmi->FBReserved = pSmi->videoRAMBytes - 2048;
	}
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			"Cursor Offset: %08lX Reserved: %08lX\n",
			(unsigned long)pSmi->FBCursorOffset,
			   (unsigned long)pSmi->FBReserved);
	/* panel size detection ... requires BIOS call on 730 hardware */
	if (pSmi->Chipset == SMI_COUGAR3DR)
	{
		if (pSmi->pInt10 != NULL)
		{
			pSmi->pInt10->num = 0x10;
			pSmi->pInt10->ax  = 0x5F00;
			pSmi->pInt10->bx  = 0;
			pSmi->pInt10->cx  = 0;
			pSmi->pInt10->dx  = 0;
			xf86ExecX86int10(pSmi->pInt10);
			if (pSmi->pInt10->ax == 0x005F)
			{
				switch (pSmi->pInt10->cx & 0x0F)
				{
					case PANEL_640x480:
						pSmi->lcdWidth  = 640;
						pSmi->lcdHeight = 480;
						break;
					
					case PANEL_800x600:
						pSmi->lcdWidth  = 800;
						pSmi->lcdHeight = 600;
						break;
					
					case PANEL_1024x768:
						pSmi->lcdWidth  = 1024;
						pSmi->lcdHeight = 768;
						break;
					
					case PANEL_1280x1024:
						pSmi->lcdWidth  = 1280;
						pSmi->lcdHeight = 1024;
						break;
					
					case PANEL_1600x1200:
						pSmi->lcdWidth  = 1600;
						pSmi->lcdHeight = 1200;
						break;
					
					case PANEL_1400x1050:
						pSmi->lcdWidth  = 1400;
						pSmi->lcdHeight = 1050;
						break;
				}
	
				xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Detected panel size via BIOS: %d x %d\n",
					   pSmi->lcdWidth, pSmi->lcdHeight);
			}
			else
			{
				xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIOS error during 730 panel detection!\n");
				pSmi->lcdWidth  = pScrn->virtualX;
				pSmi->lcdHeight = pScrn->virtualY;
			}
		}
		else 
		{
			/* int10 support isn't setup on the second call to this function,
			   so if this is the second call, don't do detection again */
			if (pSmi->lcd == 0)
			{
				/* If we get here, int10 support is not loaded or not working */ 
				xf86DrvMsg(pScrn->scrnIndex, X_INFO, "No BIOS support for 730 panel detection!\n");
				pSmi->lcdWidth  = pScrn->virtualX;
				pSmi->lcdHeight = pScrn->virtualY;
			}
		}

		/* Set this to indicate that we've done the detection */
		pSmi->lcd = 1;
	}
	else /* panel size detection for hardware other than 730 */
	{
		pSmi->lcd = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31) & 0x01;
	
		if (VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x30) & 0x01)
		{
			pSmi->lcd <<= 1;
		}
		switch (VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x30) & 0x0C)
		{
			case 0x00:
				pSmi->lcdWidth  = 640;
				pSmi->lcdHeight = 480;
				break;
	
			case 0x04:
				pSmi->lcdWidth  = 800;
				pSmi->lcdHeight = 600;
				break;
	
			case 0x08:
				if (VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x74) & 0x02)
				{
					pSmi->lcdWidth  = 1024;
					pSmi->lcdHeight = 600;
				}
				else
				{
					pSmi->lcdWidth  = 1024;
					pSmi->lcdHeight = 768;
				}
				break;
	
			case 0x0C:
				pSmi->lcdWidth  = 1280;
				pSmi->lcdHeight = 1024;
				break;
		}
	}

	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s Panel Size = %dx%d\n",
			(pSmi->lcd == 0) ? "OFF" : (pSmi->lcd == 1) ? "TFT" : "DSTN",
			pSmi->lcdWidth, pSmi->lcdHeight);

	/* Assign hwp->MemBase & IOBase here */
	hwp = VGAHWPTR(pScrn);
	if (pSmi->IOBase != NULL)
	{
		vgaHWSetMmioFuncs(hwp, pSmi->MapBase, pSmi->IOBase - pSmi->MapBase);
	}
	vgaHWGetIOBase(hwp);

	/* Map the VGA memory when the primary video */
	if (xf86IsPrimaryPci(pSmi->PciInfo))
	{
		hwp->MapSize = 0x10000;
		if (!vgaHWMapMem(pScrn))
		{
			LEAVE_PROC("SMI_MapMem");
			return(FALSE);
		}
		pSmi->PrimaryVidMapped = TRUE;
	}

	LEAVE_PROC("SMI_MapMem");
	return(TRUE);
}

/* UnMapMem - contains half of pre-4.0 EnterLeave function.  The EnterLeave
 * function which en/disable access to IO ports and ext. regs
 */

static void
SMI_UnmapMem(ScrnInfoPtr pScrn)
{
	SMIPtr pSmi = SMIPTR(pScrn);

	ENTER_PROC("SMI_UnmapMem");

	/* Unmap VGA mem if mapped. */
	if (pSmi->PrimaryVidMapped)
	{
		vgaHWUnmapMem(pScrn);
		pSmi->PrimaryVidMapped = FALSE;
	}

	SMI_DisableMmio(pScrn);

	xf86UnMapVidMem(pScrn->scrnIndex, (pointer) pSmi->MapBase, pSmi->MapSize);
	if (pSmi->FBBase != NULL)
	{
		xf86UnMapVidMem(pScrn->scrnIndex, (pointer) pSmi->FBBase,
				pSmi->videoRAMBytes);
	}

	LEAVE_PROC("SMI_UnmapMem");
}

/* This gets called at the start of each server generation. */

static Bool
SMI_ScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	SMIPtr pSmi = SMIPTR(pScrn);
	EntityInfoPtr pEnt;
	
	ENTER_PROC("SMI_ScreenInit");

	/* Map MMIO regs and framebuffer */
	if (!SMI_MapMem(pScrn))
	{
		LEAVE_PROC("SMI_ScreenInit");
		return(FALSE);
	}

	pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
	
	if (!pSmi->pInt10) {
	    pSmi->pInt10 = xf86InitInt10(pEnt->index);
	}

	/* Save the chip/graphics state */
	SMI_Save(pScrn);
	
	/* Zero the frame buffer, #258 */
	memset(pSmi->FBBase, 0, pSmi->videoRAMBytes);

	/* Initialize the first mode */
	if (!SMI_ModeInit(pScrn, pScrn->currentMode))
	{
		LEAVE_PROC("SMI_ScreenInit");
		return(FALSE);
	}

	/*
	 * The next step is to setup the screen's visuals, and initialise the
	 * framebuffer code.  In cases where the framebuffer's default choises for
	 * things like visual layouts and bits per RGB are OK, this may be as simple
	 * as calling the framebuffer's ScreenInit() function.  If not, the visuals
	 * will need to be setup before calling a fb ScreenInit() function and fixed
	 * up after.
	 *
	 * For most PC hardware at depths >= 8, the defaults that cfb uses are not
	 * appropriate.  In this driver, we fixup the visuals after.
	 */

	/*
	 * Reset the visual list.
	 */
	miClearVisualTypes();

	/* Setup the visuals we support. */

	/*
	 * For bpp > 8, the default visuals are not acceptable because we only
	 * support TrueColor and not DirectColor.  To deal with this, call
	 * miSetVisualTypes with the appropriate visual mask.
	 */
#ifndef USE_FB
	if (pScrn->bitsPerPixel > 8)
	{
		if (!miSetVisualTypes(pScrn->depth, TrueColorMask, pScrn->rgbBits,
				pScrn->defaultVisual))
		{
			LEAVE_PROC("SMI_ScreenInit");
			return(FALSE);
		}
	}
	else
#endif
	{
		if (!miSetVisualTypes(pScrn->depth,
				miGetDefaultVisualMask(pScrn->depth), pScrn->rgbBits,
				pScrn->defaultVisual))
		{
			LEAVE_PROC("SMI_ScreenInit");
			return(FALSE);
		}
	}
#ifdef USE_FB
	if (!miSetPixmapDepths ()) return FALSE;
#endif

	if (!SMI_InternalScreenInit(scrnIndex, pScreen))
	{
		LEAVE_PROC("SMI_ScreenInit");
		return(FALSE);
	}

	xf86SetBlackWhitePixels(pScreen);

	if (pScrn->bitsPerPixel > 8)
	{
		VisualPtr visual;
		/* Fixup RGB ordering */
		visual = pScreen->visuals + pScreen->numVisuals;
		while (--visual >= pScreen->visuals)
		{
			if ((visual->class | DynamicClass) == DirectColor)
			{
				visual->offsetRed   = pScrn->offset.red;
				visual->offsetGreen = pScrn->offset.green;
				visual->offsetBlue  = pScrn->offset.blue;
				visual->redMask     = pScrn->mask.red;
				visual->greenMask   = pScrn->mask.green;
				visual->blueMask    = pScrn->mask.blue;
			}
		}
	}

#ifdef USE_FB
	/* must be after RGB ordering fixed */
	fbPictureInit(pScreen, 0, 0);
#endif
 
	/* CZ 18.06.2001: moved here from smi_accel.c to have offscreen
	   framebuffer in NoAccel mode */
	{
	    int numLines, maxLines;
	    BoxRec AvailFBArea;
 
	    maxLines = pSmi->FBReserved / (pSmi->width * pSmi->Bpp);
	    if (pSmi->rotate) {
		numLines = maxLines;
	    } else {
		/* CZ 3.11.2001: What does the following code? see also smi_video.c aaa line 1226 */
/*#if SMI_USE_VIDEO */
#if 0
		numLines = ((pSmi->FBReserved - pSmi->width * pSmi->Bpp
			     * pSmi->height) * 25 / 100 + pSmi->width
			    * pSmi->Bpp - 1) / (pSmi->width * pSmi->Bpp);
		numLines += pSmi->height;
#else
		numLines = maxLines;
#endif
	    }
 
	    AvailFBArea.x1 = 0;
	    AvailFBArea.y1 = 0;
	    AvailFBArea.x2 = pSmi->width;
	    AvailFBArea.y2 = numLines;
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "FrameBuffer Box: %d,%d - %d,%d\n",
		       AvailFBArea.x1, AvailFBArea.y1, AvailFBArea.x2,
		       AvailFBArea.y2);
	    xf86InitFBManager(pScreen, &AvailFBArea);
	}
	/* end CZ */
	
	
	/* Initialize acceleration layer */
	if (!pSmi->NoAccel) {
		if (!SMI_AccelInit(pScreen)) {
		    LEAVE_PROC("SMI_ScreenInit");
		    return(FALSE);
		}
	}
	
	miInitializeBackingStore(pScreen);
	
	/* hardware cursor needs to wrap this layer */
  	SMI_DGAInit(pScreen);
	
	/* Initialise cursor functions */
	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

	/* Initialize HW cursor layer.  Must follow software cursor
	 * initialization.
	 */
	if (pSmi->hwcursor)
	{
		if (!SMI_HWCursorInit(pScreen))
		{
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Hardware cursor "
					"initialization failed\n");
		}
	}

	if (pSmi->shadowFB)
	{
		RefreshAreaFuncPtr refreshArea;

		if (pSmi->Chipset == SMI_COUGAR3DR)
		{
			refreshArea = SMI_RefreshArea730;
		}
		else
		{
			refreshArea = SMI_RefreshArea;
		}

		if (pSmi->rotate)
		{
			if (pSmi->PointerMoved == NULL)
			{
				pSmi->PointerMoved  = pScrn->PointerMoved;
				pScrn->PointerMoved = SMI_PointerMoved;
			}
		}

		ShadowFBInit(pScreen, refreshArea);
	}

	/* Initialise default colormap */
	if (!miCreateDefColormap(pScreen))
	{
		LEAVE_PROC("SMI_ScreenInit");
		return(FALSE);
	}

	/* Initialize colormap layer.  Must follow initialization of the default
	 * colormap.  And SetGamma call, else it will load palette with solid white.
	 */
    /* CZ 2.11.2001: CMAP_PALETTED_TRUECOLOR for gamma correction */
    if (!xf86HandleColormaps(pScreen, 256, pScrn->rgbBits, SMI_LoadPalette, NULL,
            CMAP_RELOAD_ON_MODE_SWITCH | CMAP_PALETTED_TRUECOLOR))
	{
		LEAVE_PROC("SMI_ScreenInit");
		return(FALSE);
	}

	pScreen->SaveScreen = SMI_SaveScreen;
	pSmi->CloseScreen = pScreen->CloseScreen;
	pScreen->CloseScreen = SMI_CloseScreen;

	if (!xf86DPMSInit(pScreen, SMI_DisplayPowerManagementSet, 0))
	{
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "DPMS initialization failed!\n");
	}

  	SMI_InitVideo(pScreen);

	/* Report any unused options (only for the first generation) */
	if (serverGeneration == 1)
	{
		xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
	}
	
	LEAVE_PROC("SMI_ScreenInit");
	return(TRUE);
}

/* Common init routines needed in EnterVT and ScreenInit */

static int
SMI_InternalScreenInit(int scrnIndex, ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	SMIPtr pSmi = SMIPTR(pScrn);
	int width, height, displayWidth;
	int bytesPerPixel = pScrn->bitsPerPixel / 8;
	int xDpi, yDpi;
	int ret;

	ENTER_PROC("SMI_InternalScreenInit");

	if (pSmi->rotate)
	{
		width        = pScrn->virtualY;
		height       = pScrn->virtualX;
		xDpi         = pScrn->yDpi;
		yDpi         = pScrn->xDpi;
		displayWidth = ((width * bytesPerPixel + 15) & ~15) / bytesPerPixel;
	}
	else
	{
		width        = pScrn->virtualX;
		height       = pScrn->virtualY;
		xDpi		 = pScrn->xDpi;
		yDpi		 = pScrn->yDpi;
		displayWidth = pScrn->displayWidth;
	}

	if (pSmi->shadowFB)
	{
		pSmi->ShadowWidth      = width;
		pSmi->ShadowHeight     = height;
		pSmi->ShadowWidthBytes = (width * bytesPerPixel + 15) & ~15;
		if (bytesPerPixel == 3)
		{
			pSmi->ShadowPitch = ((height * 3) << 16)
							  | pSmi->ShadowWidthBytes;
		}
		else
		{
			pSmi->ShadowPitch = (height << 16)
							  | (pSmi->ShadowWidthBytes / bytesPerPixel);
		}

		pSmi->saveBufferSize = pSmi->ShadowWidthBytes * pSmi->ShadowHeight;
		pSmi->FBReserved -= pSmi->saveBufferSize;
		pSmi->FBReserved &= ~0x15;
		WRITE_VPR(pSmi, 0x0C, (pSmi->FBOffset = pSmi->FBReserved) >> 3);
		if(pSmi->Chipset == SMI_COUGAR3DR)
		{
			WRITE_FPR(pSmi, FPR0C, (pSmi->FBOffset = pSmi->FBReserved) >> 3);
		}
		pScrn->fbOffset = pSmi->FBOffset + pSmi->fbMapOffset;
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
				"Shadow: width=%d height=%d "
				"offset=0x%08lX pitch=0x%08X\n",
				pSmi->ShadowWidth, pSmi->ShadowHeight,
				(unsigned long)pSmi->FBOffset,
				pSmi->ShadowPitch);
	}
	else
	{
		pSmi->FBOffset = 0;
		pScrn->fbOffset = pSmi->FBOffset + pSmi->fbMapOffset;
	}

	/*
	 * Call the framebuffer layer's ScreenInit function, and fill in other
	 * pScreen fields.
	 */

	DEBUG((VERBLEV, "\tInitializing FB @ 0x%08X for %dx%d (%d)\n",
			pSmi->FBBase, width, height, displayWidth));
	switch (pScrn->bitsPerPixel)
	{
#ifdef USE_FB
	case 8:
	case 16:
	case 24:
	  ret = fbScreenInit(pScreen, pSmi->FBBase, width, height, xDpi,
			     yDpi, displayWidth,pScrn->bitsPerPixel);
	  break;
#else
	case 8:
	  ret = cfbScreenInit(pScreen, pSmi->FBBase, width, height, xDpi,
			      yDpi, displayWidth);
	  break;

	case 16:
	  ret = cfb16ScreenInit(pScreen, pSmi->FBBase, width, height, xDpi,
					yDpi, displayWidth);
	  break;

	case 24:
	  ret = cfb24ScreenInit(pScreen, pSmi->FBBase, width, height, xDpi,
				yDpi, displayWidth);
	  break;
#endif
	default:
	  xf86DrvMsg(scrnIndex, X_ERROR, "Internal error: invalid bpp (%d) "
		     "in SMI_InternalScreenInit\n", pScrn->bitsPerPixel);
	  LEAVE_PROC("SMI_InternalScreenInit");
	  return(FALSE);
	}
	
	LEAVE_PROC("SMI_InternalScreenInit");
	return(ret);
}

/* Checks if a mode is suitable for the selected configuration. */
static ModeStatus
SMI_ValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	SMIPtr pSmi = SMIPTR(pScrn);
	float refresh;

	ENTER_PROC("SMI_ValidMode");
	refresh = (mode->VRefresh > 0) ? mode->VRefresh 
	  : mode->Clock * 1000.0 / mode->VTotal / mode->HTotal;
	xf86DrvMsg(scrnIndex, X_INFO, "Mode: %dx%d %d-bpp, %fHz\n", mode->HDisplay,
			mode->VDisplay, pScrn->bitsPerPixel, refresh);

	if (pSmi->shadowFB)
	{
		int mem;

		if (pScrn->bitsPerPixel == 24)
		{
			LEAVE_PROC("SMI_ValidMode");
			return(MODE_BAD);
		}

		mem  = (pScrn->virtualX * pScrn->bitsPerPixel / 8 + 15) & ~15;
		mem *= pScrn->virtualY * 2;

		if (mem > pSmi->FBReserved)	/* PDR#1074 */
		{
			LEAVE_PROC("SMI_ValidMode");
			return(MODE_MEM);
		}
	}

	if (!pSmi->useBIOS || pSmi->lcd)
	{
#if 1 /* PDR#983 */
		if (pSmi->zoomOnLCD)
		{
			if (   (mode->HDisplay > pSmi->lcdWidth)
				|| (mode->VDisplay > pSmi->lcdHeight)
			)
			{
				LEAVE_PROC("SMI_ValidMode");
				return(MODE_PANEL);
			}
		}
		else
#endif
		{
			if (   (mode->HDisplay != pSmi->lcdWidth)
				|| (mode->VDisplay != pSmi->lcdHeight)
			)
			{
				LEAVE_PROC("SMI_ValidMode");
				return(MODE_PANEL);
			}
		}

	}

#if 1 /* PDR#944 */
	if (pSmi->rotate)
	{
		if (   (mode->HDisplay != pSmi->lcdWidth)
			|| (mode->VDisplay != pSmi->lcdHeight)
		)
		{
			LEAVE_PROC("SMI_ValidMode");
			return(MODE_PANEL);
		}
	}
#endif

	LEAVE_PROC("SMI_ValidMode");
	return(MODE_OK);
}

static Bool
SMI_ModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	unsigned char tmp;
	int panelIndex, modeIndex, i;
	int xyAddress[] = { 320, 400, 512, 640, 800, 1024, 1280, 1600, 2048 };
	CARD32 DEDataFormat = 0;

	/* Store values to current mode register structs */
	SMIRegPtr new = &pSmi->ModeReg;
	vgaRegPtr vganew = &hwp->ModeReg;
	
	ENTER_PROC("SMI_ModeInit");

	if(!vgaHWInit(pScrn, mode))
	{
		LEAVE_PROC("SMI_ModeInit");
		return(FALSE);
	}

	new->modeInit = TRUE;

	if (pSmi->rotate)
	{
		pSmi->width  = pScrn->virtualY;
		pSmi->height = pScrn->virtualX;
	}
	else
	{
		pSmi->width  = pScrn->virtualX;
		pSmi->height = pScrn->virtualY;
	}
	pSmi->Bpp    = pScrn->bitsPerPixel / 8;

	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x17);
	tmp = inb(pSmi->PIOBase + VGA_SEQ_DATA);
	if (pSmi->pci_burst)
	{
		new->SR17 = tmp | 0x20;
	}
	else
	{
		new->SR17 = tmp & ~0x20;
	}

	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x18);
	new->SR18 = inb(pSmi->PIOBase + VGA_SEQ_DATA) | 0x11;

	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x21);
	new->SR21 = inb(pSmi->PIOBase + VGA_SEQ_DATA) & ~0x03;

	if (pSmi->Chipset != SMI_COUGAR3DR)
	{
		outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x31);
		new->SR31 = inb(pSmi->PIOBase + VGA_SEQ_DATA) & ~0xC0;

		outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x32);
		new->SR32 = inb(pSmi->PIOBase + VGA_SEQ_DATA) & ~0x07;

		if (SMI_LYNXM_SERIES(pSmi->Chipset))
		{
			new->SR32 |= 0x04;
		}
	}

	new->SRA0 = new->CR33 = new->CR3A = 0x00;

	if (pSmi->lcdWidth == 640)
	{
		panelIndex = 0;
	}
	else if (pSmi->lcdWidth == 800)
	{
		panelIndex = 1;
	}
	else
	{
		panelIndex = 2;
	}

	if (mode->HDisplay == 640)
	{
		modeIndex = 0;
	}
	else if (mode->HDisplay == 800)
	{
		modeIndex = 1;
	}
	else
	{
		modeIndex = 2;
	}

	if (SMI_LYNXM_SERIES(pSmi->Chipset))
	{
		static unsigned char PanelTable[3][14] =
		{
			{ 0x5F, 0x4F, 0x00, 0x52, 0x1E, 0x0B, 0xDF, 0x00, 0xE9, 0x0B, 0x2E,
			  0x00, 0x4F, 0xDF },
			{ 0x7F, 0x63, 0x00, 0x69, 0x19, 0x72, 0x57, 0x00, 0x58, 0x0C, 0xA2,
			  0x20, 0x4F, 0xDF },
			{ 0xA3, 0x7F, 0x00, 0x83, 0x14, 0x24, 0xFF, 0x00, 0x02, 0x08, 0xA7,
			  0xE0, 0x4F, 0xDF },
		};

		for (i = 0; i < 14; i++)
		{
			new->CR40[i] = PanelTable[panelIndex][i];
		}
		new->CR90[14] = 0x03;
		new->CR90[15] = 0x00;
		if (mode->VDisplay < pSmi->lcdHeight)
		{
			new->CRA0[6] = (pSmi->lcdHeight - mode->VDisplay) / 8;
		}
		else
		{
			new->CRA0[6] = 0;
		}

		if (mode->HDisplay < pSmi->lcdWidth)
		{
			new->CRA0[7] = (pSmi->lcdWidth - mode->HDisplay) / 16;
		}
		else
		{
			new->CRA0[7] = 0;
		}
	}
	else
	{
		static unsigned char PanelTable[3][3][14] =
		{
			{ /* 640x480 panel */
		        { 0x5F, 0x4F, 0x00, 0x53, 0x00, 0x0B, 0xDF, 0x00, 0xEA, 0x0C,
		          0x2E, 0x00, 0x4F, 0xDF },
		        { 0x5F, 0x4F, 0x00, 0x53, 0x00, 0x0B, 0xDF, 0x00, 0xEA, 0x0C,
		          0x2E, 0x00, 0x4F, 0xDF },
		        { 0x5F, 0x4F, 0x00, 0x53, 0x00, 0x0B, 0xDF, 0x00, 0xEA, 0x0C,
		          0x2E, 0x00, 0x4F, 0xDF },
			},
			{ /* 800x600 panel */
		        { 0x7F, 0x59, 0x19, 0x5E, 0x8E, 0x72, 0x1C, 0x37, 0x1D, 0x00,
		          0xA2, 0x20, 0x4F, 0xDF },
		        { 0x7F, 0x63, 0x00, 0x68, 0x18, 0x72, 0x58, 0x00, 0x59, 0x0C,
		          0xE0, 0x20, 0x63, 0x57 },
		        { 0x7F, 0x63, 0x00, 0x68, 0x18, 0x72, 0x58, 0x00, 0x59, 0x0C,
		          0xE0, 0x20, 0x63, 0x57 },
			},
			{ /* 1024x768 panel */
		        { 0xA3, 0x67, 0x0F, 0x6D, 0x1D, 0x24, 0x70, 0x95, 0x72, 0x07,
		          0xA3, 0x20, 0x4F, 0xDF },
		        { 0xA3, 0x71, 0x19, 0x77, 0x07, 0x24, 0xAC, 0xD1, 0xAE, 0x03,
		          0xE1, 0x20, 0x63, 0x57 },
		        { 0xA3, 0x7F, 0x00, 0x85, 0x15, 0x24, 0xFF, 0x00, 0x01, 0x07,
		          0xE5, 0x20, 0x7F, 0xFF },
			},
		};

		for (i = 0; i < 14; i++)
		{
			new->CR40[i] = PanelTable[panelIndex][modeIndex][i];
		}
	}

	/* CZ 2.11.2001: for gamma correction (TODO: other chipsets?) */
	new->CCR66 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x66);
	if ((pSmi->Chipset == SMI_LYNX3DM) || (pSmi->Chipset == SMI_COUGAR3DR)){
	    switch (pScrn->bitsPerPixel) {
	    case 8:
		new->CCR66 = (new->CCR66 & 0xF3) | 0x00; /* 6 bits-RAM */
		break;
	    case 16:
		new->CCR66 = (new->CCR66 & 0xF3) | 0x00; /* 6 bits-RAM */
		/* no Gamma correction in 16 Bit mode (s. Release.txt 1.3.1) */
		break;
	    case 24:
	    case 32:
		new->CCR66 = (new->CCR66 & 0xF3) | 0x04; /* Gamma correct ON */
		break;
	    default:
		LEAVE_PROC("SMI_ModeInit");
		return(FALSE);
	    }
	}

	if (pSmi->Chipset != SMI_COUGAR3DR)
	{
		outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x30);
		if (inb(pSmi->PIOBase + VGA_SEQ_DATA) & 0x01)
		{
			new->SR21 = 0x00;
		}
	}

	if (pSmi->MCLK > 0)
	{
		SMI_CommonCalcClock(pScrn->scrnIndex, pSmi->MCLK,
				    1, 1, 31, 0, 2, pSmi->minClock,
				    pSmi->maxClock, &new->SR6A, &new->SR6B);
	}
	else
	{
		new->SR6B = 0xFF;
	}

	if ((mode->HDisplay == 640) && SMI_LYNXM_SERIES(pSmi->Chipset))
	{
		vganew->MiscOutReg &= ~0x0C;
	}
	else
	{
		vganew->MiscOutReg |= 0x0C;
	}
	vganew->MiscOutReg |= 0xE0;
	if (mode->HDisplay == 800)
	{
		vganew->MiscOutReg &= ~0xC0;
	}
	if ((mode->HDisplay == 1024) && SMI_LYNXM_SERIES(pSmi->Chipset))
	{
		vganew->MiscOutReg &= ~0xC0;
	}

	/* Set DPR registers */
	pSmi->Stride = (pSmi->width * pSmi->Bpp + 15) & ~15;
	switch (pScrn->bitsPerPixel)
	{
		case 8:
			DEDataFormat = 0x00000000;
			break;

		case 16:
			pSmi->Stride >>= 1;
			DEDataFormat = 0x00100000;
			break;

		case 24:
			DEDataFormat = 0x00300000;
			break;

		case 32:
			pSmi->Stride >>= 2;
			DEDataFormat = 0x00200000;
			break;
	}
	for (i = 0; i < sizeof(xyAddress) / sizeof(xyAddress[0]); i++)
	{
		if (pSmi->rotate)
		{
			if (xyAddress[i] == pSmi->height)
			{
				DEDataFormat |= i << 16;
				break;
			}
		}
		else
		{
			if (xyAddress[i] == pSmi->width)
			{
				DEDataFormat |= i << 16;
				break;
			}
		}
	}
	new->DPR10 = (pSmi->Stride << 16) | pSmi->Stride;
	new->DPR1C = DEDataFormat;
	new->DPR20 = 0;
	new->DPR24 = 0xFFFFFFFF;
	new->DPR28 = 0xFFFFFFFF;
	new->DPR2C = 0;
	new->DPR30 = 0;
	new->DPR3C = (pSmi->Stride << 16) | pSmi->Stride;
	new->DPR40 = 0;
	new->DPR44 = 0;

	/* Set VPR registers (and FPR registers for SM731) */
	switch (pScrn->bitsPerPixel)
	{
		case 8:
			new->VPR00 = 0x00000000;
			new->FPR00_= 0x00080000;
			break;

		case 16:
			new->VPR00 = 0x00020000;
			new->FPR00_= 0x000A0000;
			break;

		case 24:
			new->VPR00 = 0x00040000;
			new->FPR00_= 0x000C0000;
			break;

		case 32:
			new->VPR00 = 0x00030000;
			new->FPR00_= 0x000B0000;
			break;
	}
	new->VPR0C = pSmi->FBOffset >> 3;
	if (pSmi->rotate)
	{
		new->VPR10 = (((( pSmi->height * pSmi->Bpp) >> 3)
				+ 2) << 16) | ((pSmi->height * pSmi->Bpp) >> 3);
	}
	else
	{
		new->VPR10 = ((((pSmi->width * pSmi->Bpp) >> 3)
				+ 2) << 16) | ((pSmi->width * pSmi->Bpp) >> 3);
	}

	new->FPR0C_ = new->VPR0C;
	new->FPR10_ = new->VPR10;

	/* Set CPR registers */
	new->CPR00 = 0x00000000;

	pScrn->vtSema = TRUE;

	/* Find the INT 10 mode number */
	{
		static struct
		{
			int x, y, bpp;
			CARD16 mode;

		} modeTable[] =
		{
			{  640,  480,  8, 0x50 },
			{  640,  480, 16, 0x52 },
			{  640,  480, 24, 0x53 },
			{  640,  480, 32, 0x54 },
			{  800,  600,  8, 0x55 },
			{  800,  600, 16, 0x57 },
			{  800,  600, 24, 0x58 },
			{  800,  600, 32, 0x59 },
			{ 1024,  768,  8, 0x60 },
			{ 1024,  768, 16, 0x62 },
			{ 1024,  768, 24, 0x63 },
			{ 1024,  768, 32, 0x64 },
			{ 1280, 1024,  8, 0x65 },
			{ 1280, 1024, 16, 0x67 },
			{ 1280, 1024, 24, 0x68 },
			{ 1280, 1024, 32, 0x69 },
		};

		new->mode = 0;
		for (i = 0; i < sizeof(modeTable) / sizeof(modeTable[0]); i++)
		{
			if (   (modeTable[i].x == mode->HDisplay)
				&& (modeTable[i].y == mode->VDisplay)
				&& (modeTable[i].bpp == pScrn->bitsPerPixel)
			)
			{
				new->mode = modeTable[i].mode;
				break;
			}
		}
	}

	/* Zero the font memory */
	memset(new->smiFont, 0, sizeof(new->smiFont));

	/* Write the mode registers to hardware */
	SMI_WriteMode(pScrn, vganew, new);

	/* Adjust the viewport */
	SMI_AdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);

	LEAVE_PROC("SMI_ModeInit");
	return(TRUE);
}

/*
 * This is called at the end of each server generation.  It restores the
 * original (text) mode.  It should also unmap the video memory, and free any
 * per-generation data allocated by the driver.  It should finish by unwrapping
 * and calling the saved CloseScreen function.
 */

static Bool
SMI_CloseScreen(int scrnIndex, ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	vgaRegPtr vgaSavePtr = &hwp->SavedReg;
	SMIRegPtr SMISavePtr = &pSmi->SavedReg;
	Bool ret;
	
	ENTER_PROC("SMI_CloseScreen");

	if (pScrn->vtSema)
	{
		SMI_WriteMode(pScrn, vgaSavePtr, SMISavePtr);
		vgaHWLock(hwp);
		SMI_UnmapMem(pScrn);
	}

	if (pSmi->AccelInfoRec != NULL)
	{
		XAADestroyInfoRec(pSmi->AccelInfoRec);
	}
	if (pSmi->CursorInfoRec != NULL)
	{
		xf86DestroyCursorInfoRec(pSmi->CursorInfoRec);
	}
	if (pSmi->DGAModes != NULL)
	{
		xfree(pSmi->DGAModes);
	}
	if (pSmi->pInt10 != NULL)
	{
		xf86FreeInt10(pSmi->pInt10);
		pSmi->pInt10 = NULL;
	}
	if (pSmi->ptrAdaptor != NULL)
	{
		xfree(pSmi->ptrAdaptor);
	}
	if (pSmi->BlockHandler != NULL)
	{
		pScreen->BlockHandler = pSmi->BlockHandler;
	}
	/* #670 */
	if (pSmi->pSaveBuffer)
	{
		xfree(pSmi->pSaveBuffer);
	}
	/* #920 */
	if (pSmi->paletteBuffer)
	{
		xfree(pSmi->paletteBuffer);
	}

	pScrn->vtSema = FALSE;
	pScreen->CloseScreen = pSmi->CloseScreen;
	ret = (*pScreen->CloseScreen)(scrnIndex, pScreen);

	LEAVE_PROC("SMI_CloseScreen");
	return(ret);
}

static void
SMI_FreeScreen(int scrnIndex, int flags)
{
	SMI_FreeRec(xf86Screens[scrnIndex]);
}

static Bool
SMI_SaveScreen(ScreenPtr pScreen, int mode)
{
	Bool ret;

	ENTER_PROC("SMI_SaveScreen");

	ret = vgaHWSaveScreen(pScreen, mode);

	LEAVE_PROC("SMI_SaveScreen");
	return(ret);
}

void
SMI_AdjustFrame(int scrnIndex, int x, int y, int flags)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	SMIPtr pSmi = SMIPTR(pScrn);
	CARD32 Base;

	ENTER_PROC("SMI_AdjustFrame");

	if (pSmi->ShowCache && y)
	{
		y += pScrn->virtualY - 1;
	}

	Base = pSmi->FBOffset + (x + y * pScrn->virtualX) * pSmi->Bpp;
	if (SMI_LYNX3D_SERIES(pSmi->Chipset) || SMI_COUGAR_SERIES(pSmi->Chipset))
	{
		Base = (Base + 15) & ~15;
#if 1 /* PDR#1058 */
		while ((Base % pSmi->Bpp) > 0)
		{
			Base -= 16;
		}
#endif
	}
	else
	{
		Base = (Base + 7) & ~7;
#if 1 /* PDR#1058 */
		while ((Base % pSmi->Bpp) > 0)
		{
			Base -= 8;
		}
#endif
	}

	WRITE_VPR(pSmi, 0x0C, Base >> 3);
	if(pSmi->Chipset == SMI_COUGAR3DR)
	{
		WRITE_FPR(pSmi, FPR0C, Base >> 3);
	}

	LEAVE_PROC("SMI_AdjustFrame");
}

Bool
SMI_SwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
{
	Bool ret;

	ENTER_PROC("SMI_SwitchMode");

	ret = SMI_ModeInit(xf86Screens[scrnIndex], mode);

	LEAVE_PROC("SMI_SwitchMode");
	return(ret);
}

void
SMI_LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indicies, LOCO *colors,
				VisualPtr pVisual)
{
	SMIPtr pSmi = SMIPTR(pScrn);
	int i;

	ENTER_PROC("SMI_LoadPalette");

	/* Enable both the CRT and LCD DAC RAM paths, so both palettes are updated */
    if ((pSmi->Chipset == SMI_LYNX3DM) || (pSmi->Chipset == SMI_COUGAR3DR)) 
	{
		CARD8 ccr66;

		ccr66  = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x66);
		ccr66 &= 0x0f;
		VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x66, ccr66);
	}

	for(i = 0; i < numColors; i++)
	{
        DEBUG((VERBLEV, "pal[%d] = %d %d %d\n", indicies[i],
           colors[indicies[i]].red, colors[indicies[i]].green, colors[indicies[i]].blue));
		VGAOUT8(pSmi, VGA_DAC_WRITE_ADDR, indicies[i]);
		VGAOUT8(pSmi, VGA_DAC_DATA, colors[indicies[i]].red);
		VGAOUT8(pSmi, VGA_DAC_DATA, colors[indicies[i]].green);
		VGAOUT8(pSmi, VGA_DAC_DATA, colors[indicies[i]].blue);
	}

	LEAVE_PROC("SMI_LoadPalette");
}

static void
SMI_DisableVideo(ScrnInfoPtr pScrn)
{
	SMIPtr pSmi = SMIPTR(pScrn);
	CARD8 tmp;

	if (!(tmp = VGAIN8(pSmi, VGA_DAC_MASK)))
	  return;
	pSmi->DACmask = tmp;
	VGAOUT8(pSmi, VGA_DAC_MASK, 0);
}

static void
SMI_EnableVideo(ScrnInfoPtr pScrn)
{
	SMIPtr pSmi = SMIPTR(pScrn);

	VGAOUT8(pSmi, VGA_DAC_MASK, pSmi->DACmask);
}


void
SMI_EnableMmio(ScrnInfoPtr pScrn)
{
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	CARD8 tmp;

	ENTER_PROC("SMI_EnableMmio");

	/*
	 * Enable chipset (seen on uninitialized secondary cards) might not be
	 * needed once we use the VGA softbooter
	 */
	vgaHWSetStdFuncs(hwp);

	/* Enable linear mode */
	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x18);
	tmp = inb(pSmi->PIOBase + VGA_SEQ_DATA);
	pSmi->SR18Value = tmp;					/* PDR#521 */
	outb(pSmi->PIOBase + VGA_SEQ_DATA, tmp | 0x11);

	/* Enable 2D/3D Engine and Video Processor */
	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x21);
	tmp = inb(pSmi->PIOBase + VGA_SEQ_DATA);
	pSmi->SR21Value = tmp;					/* PDR#521 */
	outb(pSmi->PIOBase + VGA_SEQ_DATA, tmp & ~0x03);

	LEAVE_PROC("SMI_EnableMmio");
}

void
SMI_DisableMmio(ScrnInfoPtr pScrn)
{
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);

	ENTER_PROC("SMI_DisableMmio");

	vgaHWSetStdFuncs(hwp);

	/* Disable 2D/3D Engine and Video Processor */
	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x21);
	outb(pSmi->PIOBase + VGA_SEQ_DATA, pSmi->SR21Value);	/* PDR#521 */

	/* Disable linear mode */
	outb(pSmi->PIOBase + VGA_SEQ_INDEX, 0x18);
	outb(pSmi->PIOBase + VGA_SEQ_DATA, pSmi->SR18Value);	/* PDR#521 */

	LEAVE_PROC("SMI_DisableMmio");
}

/* This function is used to debug, it prints out the contents of Lynx regs */
static void
SMI_PrintRegs(ScrnInfoPtr pScrn)
{
	unsigned char i;
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	int vgaCRIndex = hwp->IOBase + VGA_CRTC_INDEX_OFFSET;
	int vgaCRReg   = hwp->IOBase + VGA_CRTC_DATA_OFFSET;
	int vgaStatus  = hwp->IOBase + VGA_IN_STAT_1_OFFSET;

	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"START register dump ------------------\n");

	xf86ErrorFVerb(VERBLEV, "MISCELLANEOUS OUTPUT\n    %02X\n",
			VGAIN8(pSmi, VGA_MISC_OUT_R));

	xf86ErrorFVerb(VERBLEV, "\nSEQUENCER\n"
			"    x0 x1 x2 x3  x4 x5 x6 x7  x8 x9 xA xB  xC xD xE xF");
	for (i = 0x00; i <= 0xAF; i++)
	{
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		if ((i & 0x3) == 0x0) xf86ErrorFVerb(VERBLEV, " ");
		xf86ErrorFVerb(VERBLEV, "%02X ",
				VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\nCRT CONTROLLER\n"
			"    x0 x1 x2 x3  x4 x5 x6 x7  x8 x9 xA xB  xC xD xE xF");
	for (i = 0x00; i <= 0xAD; i++)
	{
		if (i == 0x20) i = 0x30;
		if (i == 0x50) i = 0x90;
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		if ((i & 0x3) == 0x0) xf86ErrorFVerb(VERBLEV, " ");
		xf86ErrorFVerb(VERBLEV, "%02X ",
				VGAIN8_INDEX(pSmi, vgaCRIndex, vgaCRReg, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\nGRAPHICS CONTROLLER\n"
			"    x0 x1 x2 x3  x4 x5 x6 x7  x8 x9 xA xB  xC xD xE xF");
	for (i = 0x00; i <= 0x08; i++)
	{
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		if ((i & 0x3) == 0x0) xf86ErrorFVerb(VERBLEV, " ");
		xf86ErrorFVerb(VERBLEV, "%02X ",
				VGAIN8_INDEX(pSmi, VGA_GRAPH_INDEX, VGA_GRAPH_DATA, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\nATTRIBUTE 0CONTROLLER\n"
			"    x0 x1 x2 x3  x4 x5 x6 x7  x8 x9 xA xB  xC xD xE xF");
	for (i = 0x00; i <= 0x14; i++)
	{
		(void) VGAIN8(pSmi, vgaStatus);
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		if ((i & 0x3) == 0x0) xf86ErrorFVerb(VERBLEV, " ");
		xf86ErrorFVerb(VERBLEV, "%02X ",
				VGAIN8_INDEX(pSmi, VGA_ATTR_INDEX, VGA_ATTR_DATA_R, i));
	}
	(void) VGAIN8(pSmi, vgaStatus);
	VGAOUT8(pSmi, VGA_ATTR_INDEX, 0x20);

	xf86ErrorFVerb(VERBLEV, "\n\nDPR    x0       x4       x8       xC");
	for (i = 0x00; i <= 0x44; i += 4)
	{
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		xf86ErrorFVerb(VERBLEV, " %08lX", (unsigned long)READ_DPR(pSmi, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\nVPR    x0       x4       x8       xC");
	for (i = 0x00; i <= 0x60; i += 4)
	{
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		xf86ErrorFVerb(VERBLEV, " %08lX", (unsigned long)READ_VPR(pSmi, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\nCPR    x0       x4       x8       xC");
	for (i = 0x00; i <= 0x18; i += 4)
	{
		if ((i & 0xF) == 0x0) xf86ErrorFVerb(VERBLEV, "\n%02X|", i);
		xf86ErrorFVerb(VERBLEV, " %08lX", (unsigned long)READ_CPR(pSmi, i));
	}

	xf86ErrorFVerb(VERBLEV, "\n\n");
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, VERBLEV,
			"END register dump --------------------\n");
}

/*
 * SMI_DisplayPowerManagementSet -- Sets VESA Display Power Management
 * Signaling (DPMS) Mode.
 */
static void
SMI_DisplayPowerManagementSet(ScrnInfoPtr pScrn, int PowerManagementMode,
							  int flags)
{
	vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	CARD8 SR01, SR20, SR21, SR22, SR23, SR24, SR31, SR34;

	ENTER_PROC("SMI_DisplayPowerManagementSet");

	/* If we already are in the requested DPMS mode, just return */
	if (pSmi->CurrentDPMS == PowerManagementMode)
	{
		LEAVE_PROC("SMI_DisplayPowerManagementSet");
		return;
	}

#if 1 /* PDR#735 */
	if (pSmi->pInt10 != NULL)
	{
		pSmi->pInt10->ax = 0x4F10;
		switch (PowerManagementMode)
		{
			case DPMSModeOn:
				pSmi->pInt10->bx = 0x0001;
				break;

			case DPMSModeStandby:
				pSmi->pInt10->bx = 0x0101;
				break;

			case DPMSModeSuspend:
				pSmi->pInt10->bx = 0x0201;
				break;

			case DPMSModeOff:
				pSmi->pInt10->bx = 0x0401;
				break;
		}
		pSmi->pInt10->cx = 0x0000;
		pSmi->pInt10->num = 0x10;
		xf86ExecX86int10(pSmi->pInt10);
		if (pSmi->pInt10->ax == 0x004F)
		{
			pSmi->CurrentDPMS = PowerManagementMode;
#if 1 /* PDR#835 */
			if (PowerManagementMode == DPMSModeOn)
			{
				SR01 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x01);
				VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x01,
						SR01 & ~0x20);
			}
#endif
			LEAVE_PROC("SMI_DisplayPowerManagementSet");
			return;
		}
	}
#endif

	/* Save the current SR registers */
	if (pSmi->CurrentDPMS == DPMSModeOn)
	{
		pSmi->DPMS_SR20 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x20);
		pSmi->DPMS_SR21 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21);
		pSmi->DPMS_SR31 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31);
		pSmi->DPMS_SR34 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x34);
	}

	/* Read the required SR registers for the DPMS handler */
	SR01 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x01);
	SR20 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x20);
	SR21 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21);
	SR22 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x22);
	SR23 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x23);
	SR24 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x24);
	SR31 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31);
	SR34 = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x34);

	switch (PowerManagementMode)
	{
		case DPMSModeOn:
			/* Screen On: HSync: On, VSync : On */
			SR01 &= ~0x20;
			SR20  = pSmi->DPMS_SR20;
			SR21  = pSmi->DPMS_SR21;
			SR22 &= ~0x30;
			SR23 &= ~0xC0;
			SR24 |= 0x01;
			SR31  = pSmi->DPMS_SR31;
			SR34  = pSmi->DPMS_SR34;
			break;

		case DPMSModeStandby:
			/* Screen: Off; HSync: Off, VSync: On */
			SR01 |= 0x20;
			SR20  = (SR20 & ~0xB0) | 0x10;
			SR21 |= 0x88;
			SR22  = (SR22 & ~0x30) | 0x10;
			SR23  = (SR23 & ~0x07) | 0xD8;
			SR24 &= ~0x01;
			SR31  = (SR31 & ~0x07) | 0x00;
			SR34 |= 0x80;
			break;

		case DPMSModeSuspend:
			/* Screen: Off; HSync: On, VSync: Off */
			SR01 |= 0x20;
			SR20  = (SR20 & ~0xB0) | 0x10;
			SR21 |= 0x88;
			SR22  = (SR22 & ~0x30) | 0x20;
			SR23  = (SR23 & ~0x07) | 0xD8;
			SR24 &= ~0x01;
			SR31  = (SR31 & ~0x07) | 0x00;
			SR34 |= 0x80;
			break;

		case DPMSModeOff:
			/* Screen: Off; HSync: Off, VSync: Off */
			SR01 |= 0x20;
			SR20  = (SR20 & ~0xB0) | 0x10;
			SR21 |= 0x88;
			SR22  = (SR22 & ~0x30) | 0x30;
			SR23  = (SR23 & ~0x07) | 0xD8;
			SR24 &= ~0x01;
			SR31  = (SR31 & ~0x07) | 0x00;
			SR34 |= 0x80;
			break;

		default:
			xf86ErrorFVerb(VERBLEV, "Invalid PowerManagementMode %d passed to "
					"SMI_DisplayPowerManagementSet\n", PowerManagementMode);
			LEAVE_PROC("SMI_DisplayPowerManagementSet");
			return;
	}

	/* Wait for vertical retrace */
	while (hwp->readST01(hwp) & 0x8) ;
	while (!(hwp->readST01(hwp) & 0x8)) ;

	/* Write the registers */
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x01, SR01);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x34, SR34);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x31, SR31);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x20, SR20);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x22, SR22);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x23, SR23);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x21, SR21);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x24, SR24);

	/* Save the current power state */
	pSmi->CurrentDPMS = PowerManagementMode;

	LEAVE_PROC("SMI_DisplayPowerManagementSet");
}

static void
SMI_ProbeDDC(ScrnInfoPtr pScrn, int index)
{
	vbeInfoPtr pVbe;
	if (xf86LoadSubModule(pScrn, "vbe"))
	{
		pVbe = VBEInit(NULL, index);
		ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
		vbeFree(pVbe);
	}
}

static unsigned int
SMI_ddc1Read(ScrnInfoPtr pScrn)
{
	register vgaHWPtr hwp = VGAHWPTR(pScrn);
	SMIPtr pSmi = SMIPTR(pScrn);
	unsigned int ret;

	ENTER_PROC("SMI_ddc1Read");

	while (hwp->readST01(hwp) & 0x8) ;
	while (!(hwp->readST01(hwp) & 0x8)) ;

	ret = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x72) & 0x08;

	LEAVE_PROC("SMI_ddc1Read");
	return(ret);
}

static Bool
SMI_ddc1(int scrnIndex)
{
	ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
	SMIPtr pSmi = SMIPTR(pScrn);
	Bool success = FALSE;
	xf86MonPtr pMon;
	unsigned char tmp;

	ENTER_PROC("SMI_ddc1");

	tmp = VGAIN8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x72);
	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x72, tmp | 0x20);

	pMon = xf86PrintEDID(xf86DoEDID_DDC1(scrnIndex,
					     LoaderSymbol("vgaHWddc1SetSpeed"),
					     SMI_ddc1Read));
	if (pMon != NULL)
	{
		success = TRUE;
	}
	xf86SetDDCproperties(pScrn, pMon);

	VGAOUT8_INDEX(pSmi, VGA_SEQ_INDEX, VGA_SEQ_DATA, 0x72, tmp);

	LEAVE_PROC("SMI_ddc1");
	return(success);
}