summaryrefslogtreecommitdiff
path: root/src/sis_utility.c
blob: 162468378a1c78c2026bb0807fb44ceb40323674 (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
/*
 * SiS driver utility interface & routines
 *
 * Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1) Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3) The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Thomas Winischhofer <thomas@winischhofer.net>
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "sis.h"
#include <X11/X.h>
#include "dixstruct.h"

#include "sis_videostr.h"

#ifdef TWDEBUG
#define SIS_NEED_orSISIDXREG
#include "sis_regs.h"
#endif

/* Definitions for SISCTRL extension */

#define SISCTRL_PROTOCOL_NAME	"SISCTRL"

#define SISCTRL_MAJOR_VERSION		0	/* current version numbers */
#define SISCTRL_MINOR_VERSION		1

#define SISCTRL_MAX_SCREENS		32	/* number of screens the ext can handle */

#define X_SiSCtrlQueryVersion		0
#define X_SiSCtrlCommand		1

#define SDC_ID				0x53495321

#define SDC_VERSION			1

#define SDC_NUM_PARM_RESULT		20

/* sdc_command */
#define SDC_CMD_GETVERSION 		0x98980001
#define SDC_CMD_GETHWINFO		0x98980002
#define SDC_CMD_GETALLFLAGS		0x98980003
#define SDC_CMD_GETVBFLAGSVERSION	0x98980004
#define SDC_CMD_GETVBFLAGS		0x98980005
#define SDC_CMD_CHECKMODEFORCRT2	0x98980006
#define SDC_CMD_SETVBFLAGS		0x98980007
#define SDC_CMD_GETDETECTEDDEVICES	0x98980008
#define SDC_CMD_REDETECTCRT2DEVICES	0x98980009
#define SDC_CMD_GETCRT1STATUS		0x9898000a
#define SDC_CMD_SETCRT1STATUS		0x9898000b
#define SDC_CMD_GETSDFLAGS		0x9898000c
#define SDC_CMD_GETSD2FLAGS		0x9898000d
#define SDC_CMD_GETLOCKSTATUS		0x9898000e
#define SDC_CMD_SETLOCKSTATUS		0x9898000f
#define SDC_CMD_GETTVANTIFLICKER	0x98980010
#define SDC_CMD_SETTVANTIFLICKER	0x98980011
#define SDC_CMD_GETTVSATURATION		0x98980012
#define SDC_CMD_SETTVSATURATION		0x98980013
#define SDC_CMD_GETTVEDGEENHANCE	0x98980014
#define SDC_CMD_SETTVEDGEENHANCE	0x98980015
#define SDC_CMD_GETTVCFILTER		0x98980016
#define SDC_CMD_SETTVCFILTER		0x98980017
#define SDC_CMD_GETTVYFILTER		0x98980018
#define SDC_CMD_SETTVYFILTER		0x98980019
#define SDC_CMD_GETTVCOLORCALIB		0x9898001a
#define SDC_CMD_SETTVCOLORCALIB		0x9898001b
#define SDC_CMD_GETTVCHCONTRAST		0x9898001c
#define SDC_CMD_SETTVCHCONTRAST		0x9898001d
#define SDC_CMD_GETTVCHTEXTENHANCE	0x9898001e
#define SDC_CMD_SETTVCHTEXTENHANCE	0x9898001f
#define SDC_CMD_GETTVCHCHROMAFLICKERFILTER 0x98980020
#define SDC_CMD_SETTVCHCHROMAFLICKERFILTER 0x98980021
#define SDC_CMD_GETTVCHLUMAFLICKERFILTER 0x98980022
#define SDC_CMD_SETTVCHLUMAFLICKERFILTER 0x98980023
#define SDC_CMD_GETTVCHCVBSCOLOR	0x98980024
#define SDC_CMD_SETTVCHCVBSCOLOR	0x98980025
#define SDC_CMD_GETCHTVOVERSCAN		0x98980026
#define SDC_CMD_SETCHTVOVERSCAN		0x98980027
#define SDC_CMD_GETGAMMASTATUS		0x98980028
#define SDC_CMD_SETGAMMASTATUS		0x98980029
#define SDC_CMD_GETTVXSCALE		0x9898002a
#define SDC_CMD_SETTVXSCALE		0x9898002b
#define SDC_CMD_GETTVYSCALE		0x9898002c
#define SDC_CMD_SETTVYSCALE		0x9898002d
#define SDC_CMD_GETSCREENSIZE		0x9898002e
#define SDC_CMD_GETGAMMABRIGHTNESS	0x9898002f
#define SDC_CMD_SETGAMMABRIGHTNESS	0x98980030
#define SDC_CMD_GETGAMMABRIGHTNESS2	0x98980031
#define SDC_CMD_SETGAMMABRIGHTNESS2	0x98980032
#define SDC_CMD_GETGETGAMMACRT2		0x98980033
#define SDC_CMD_SETGETGAMMACRT2		0x98980034
#define SDC_CMD_GETHWCURSORSTATUS	0x98980035
#define SDC_CMD_SETHWCURSORSTATUS	0x98980036
#define SDC_CMD_GETPANELMODE		0x98980037
#define SDC_CMD_SETPANELMODE		0x98980038
#define SDC_CMD_GETMERGEDMODEDETAILS	0x98980039
#define SDC_CMD_GETDEVICENAME		0x9898003a
#define SDC_CMD_GETMONITORNAME		0x9898003b
#define SDC_CMD_GETDEVICENAME2		0x9898003c
#define SDC_CMD_GETMONITORNAME2		0x9898003d
#define SDC_CMD_SETXVBRIGHTNESS		0x9898003e
#define SDC_CMD_GETXVBRIGHTNESS		0x9898003f
#define SDC_CMD_SETXVCONTRAST		0x98980040
#define SDC_CMD_GETXVCONTRAST		0x98980041
#define SDC_CMD_SETXVHUE		0x98980042
#define SDC_CMD_GETXVHUE		0x98980043
#define SDC_CMD_SETXVSATURATION		0x98980044
#define SDC_CMD_GETXVSATURATION		0x98980045
#define SDC_CMD_SETXVGAMMA		0x98980046
#define SDC_CMD_GETXVGAMMA		0x98980047
#define SDC_CMD_SETXVCOLORKEY		0x98980048
#define SDC_CMD_GETXVCOLORKEY		0x98980049
#define SDC_CMD_SETXVAUTOPAINTCOLORKEY	0x9898004a
#define SDC_CMD_GETXVAUTOPAINTCOLORKEY	0x9898004b
#define SDC_CMD_SETXVDEFAULTS		0x9898004c
#define SDC_CMD_SETXVDISABLEGFX		0x9898004d
#define SDC_CMD_GETXVDISABLEGFX		0x9898004e
#define SDC_CMD_SETXVDISABLEGFXLR	0x9898004f
#define SDC_CMD_GETXVDISABLEGFXLR	0x98980050
#define SDC_CMD_SETXVSWITCHCRT		0x98980051
#define SDC_CMD_GETXVSWITCHCRT		0x98980052
#define SDC_CMD_GETTVXPOS		0x98980053
#define SDC_CMD_SETTVXPOS		0x98980054
#define SDC_CMD_GETTVYPOS		0x98980055
#define SDC_CMD_SETTVYPOS		0x98980056
#define SDC_CMD_SETXVDEINT		0x98980057
#define SDC_CMD_GETXVDEINT		0x98980058
#define SDC_CMD_GETMONGAMMACRT1		0x98980059
#define SDC_CMD_GETMONGAMMACRT2		0x9898005a
#define SDC_CMD_LOGQUIET		0x9898005b
#define SDC_CMD_GETNEWGAMMABRICON	0x9898005c
#define SDC_CMD_SETNEWGAMMABRICON	0x9898005d
#define SDC_CMD_GETNEWGAMMABRICON2	0x9898005e
#define SDC_CMD_SETNEWGAMMABRICON2	0x9898005f
#define SDC_CMD_GETGETNEWGAMMACRT2	0x98980060
#define SDC_CMD_SETGETNEWGAMMACRT2	0x98980061
#define SDC_CMD_NEWSETVBFLAGS		0x98980062
#define SDC_CMD_GETCRT1SATGAIN		0x98980063
#define SDC_CMD_SETCRT1SATGAIN		0x98980064
#define SDC_CMD_GETCRT2SATGAIN		0x98980065
#define SDC_CMD_SETCRT2SATGAIN		0x98980066
/* more to come, adapt MAXCOMMAND! */
#define SDC_MAXCOMMAND			SDC_CMD_SETCRT2SATGAIN

/* in result_header */
#define SDC_RESULT_OK  			0x66670000
#define SDC_RESULT_UNDEFCMD		0x66670001
#define SDC_RESULT_NOPERM		0x66670002
#define SDC_RESULT_INVAL		0x66670003
#define SDC_RESULT_MESSAGEERROR		0x66670004  /* Client side only */
#define SDC_RESULT_NOEXTENSION		0x66670005  /* Client side only */

/* For SDC_CMD_GETHWINFO */
#define SDC_BUS_TYPE_PCI		0
#define SDC_BUS_TYPE_AGP		1
#define SDC_BUS_TYPE_PCIE		2
#define SDC_BUS_TYPE_USB		3

/* For SDC_CMD_GETMERGEDMODEDETAILS */
#define SDC_MMODE_POS_ERROR		0
#define SDC_MMODE_POS_LEFTOF		1
#define SDC_MMODE_POS_RIGHTOF		2
#define SDC_MMODE_POS_ABOVE		3
#define SDC_MMODE_POS_BELOW		4
#define SDC_MMODE_POS_CLONE		5

typedef struct _SiSCtrlQueryVersion {
    CARD8	reqType;		/* always SiSCtrlReqCode */
    CARD8	SiSCtrlReqType;		/* always X_SiSCtrlQueryVersion */
    CARD16	length B16;
} xSiSCtrlQueryVersionReq;
#define sz_xSiSCtrlQueryVersionReq	4

typedef struct {
    BYTE	type;			/* X_Reply */
    BOOL	pad1;
    CARD16	sequenceNumber B16;
    CARD32	length B32;
    CARD16	majorVersion B16;	/* major version of SISCTRL */
    CARD16	minorVersion B16;	/* minor version of SISCTRL */
    CARD32	pad2 B32;
    CARD32	pad3 B32;
    CARD32	pad4 B32;
    CARD32	pad5 B32;
    CARD32	pad6 B32;
} xSiSCtrlQueryVersionReply;
#define sz_xSiSCtrlQueryVersionReply	32

typedef struct {
    CARD8	reqType;		/* always SiSCtrlReqCode */
    CARD8	SiSCtrlReqType;		/* always SiSCtrl_SiSCtrlCommand */
    CARD16	length B16;
    CARD32	pad1 B32;
    CARD32	screen;
    CARD32 	sdc_id;
    CARD32 	sdc_chksum;
    CARD32	sdc_command;
    CARD32	sdc_parm[SDC_NUM_PARM_RESULT];
    CARD32	sdc_result_header;
    CARD32	sdc_result[SDC_NUM_PARM_RESULT];
    char	sdc_buffer[32];
} xSiSCtrlCommandReq;
#define sz_xSiSCtrlCommandReq		(28 + (SDC_NUM_PARM_RESULT * 4 * 2) + 32)

typedef struct {
    BYTE	type;			/* X_Reply */
    BOOL	pad1;
    CARD16	sequenceNumber B16;
    CARD32	length B32;
    CARD32	screen;
    CARD32 	sdc_id;
    CARD32 	sdc_chksum;
    CARD32	sdc_command;
    CARD32	sdc_parm[SDC_NUM_PARM_RESULT];
    CARD32	sdc_result_header;
    CARD32	sdc_result[SDC_NUM_PARM_RESULT];
    char	sdc_buffer[32];
} xSiSCtrlCommandReply;
#define sz_xSiSCtrlCommandReply		(28 + (SDC_NUM_PARM_RESULT * 4 * 2) + 32)

typedef struct {
    unsigned int maxscreens;		/* Max number of entries = SISCTRL_MAX_SCREENS*/
    unsigned int version_major;		/* Extension major version */
    unsigned int version_minor; 	/* Extension minor version */
    int		 (*HandleSiSDirectCommand[SISCTRL_MAX_SCREENS])(xSiSCtrlCommandReply *);
} xSiSCtrlScreenTable;

void		SiSCtrlExtInit(ScrnInfoPtr pScrn);
void		SiSCtrlExtUnregister(SISPtr pSiS, int index);

#ifdef XV_SD_DEPRECATED
int		SISSetPortUtilAttribute(ScrnInfoPtr pScrn, Atom attribute,
					INT32 value, SISPortPrivPtr pPriv);
int		SISGetPortUtilAttribute(ScrnInfoPtr pScrn,  Atom attribute,
					INT32 *value, SISPortPrivPtr pPriv);
#endif

extern Bool 	SISRedetectCRT2Type(ScrnInfoPtr pScrn);
extern UShort	SiS_CheckModeCRT1(ScrnInfoPtr pScrn, DisplayModePtr mode,
				 unsigned int VBFlags, Bool hcm);
extern UShort	SiS_CheckModeCRT2(ScrnInfoPtr pScrn, DisplayModePtr mode,
				 unsigned int VBFlags, Bool hcm);
extern void	SISAdjustFrame(int scrnIndex, int x, int y, int flags);
extern float	SiSCalcVRate(DisplayModePtr mode);
extern void	SiS_UpdateGammaCRT2(ScrnInfoPtr pScrn);
#ifdef SISGAMMARAMP
extern void	SISCalculateGammaRamp(ScreenPtr pScreen, ScrnInfoPtr pScrn);
#endif

extern void	SISSetPortDefaults(ScrnInfoPtr pScrn, SISPortPrivPtr pPriv);
extern void	SISUpdateVideoParms(SISPtr pSiS, SISPortPrivPtr pPriv);
extern void	SiSUpdateXvGamma(SISPtr pSiS, SISPortPrivPtr pPriv);

/***********************************
 *        Low-level routines       *
 ***********************************/

static Bool
SISSwitchCRT1Status(ScrnInfoPtr pScrn, int onoff, Bool quiet)
{
    SISPtr pSiS = SISPTR(pScrn);
    DisplayModePtr mode = pScrn->currentMode;
    ULong vbflags = pSiS->VBFlags;
    ULong vbflags3 = pSiS->VBFlags3;
    int crt1off;

    /* onoff: 0=OFF, 1=ON(VGA), 2=ON(LCDA) */

    /* Switching to LCDA will disable CRT2 if previously LCD or VGA,
     * unless on a dual-vb setup
     */

    /* For usability reasons, the user should not simply "lose" one
     * of his output devices in MergedFB mode. Therefore, a switch
     * which might lead to this situation will not be performed in
     * MergedFB mode. (For example: If CRT2 is either LCD or VGA,
     * don't let the user switch to LCD-via-CRT1 mode, because he
     * would lose one output device since LCD-via-CRT1 is only
     * supported together with TV, not any other CRT2 type.)
     * In Non-MergedFB mode, losing one output device is not
     * considered that harmful.
     * Update: We let the user switch off a device if currently
     * a "clone" mode is active. Same for switching CRT1 to LCD
     * while CRT2 is LCD or VGA.
     */

    /* Do NOT use this to switch from CRT1_LCDA to CRT2_LCD */

    /* Only on 300 and 315/330/340 series */
    if(pSiS->VGAEngine != SIS_300_VGA &&
       pSiS->VGAEngine != SIS_315_VGA) return FALSE;

    /* Off only if at least one CRT2 device is active */
    if((!onoff) && (!(vbflags & CRT2_ENABLE))) return FALSE;

#ifdef SISDUALHEAD
    if(pSiS->DualHeadMode) return FALSE;
#endif

    /* Can't switch to LCDA if not supported (duh!) */
    if(!(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTLCDA)) {
       if(onoff == 2) {
          if(!quiet) {
             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	        "LCD-via-CRT1 not supported by hardware or no panel detected\n");
	  }
          return FALSE;
       }
    }

#ifdef SISMERGED
    if(pSiS->MergedFB) {
       if(((SiSMergedDisplayModePtr)mode->Private)->CRT2Position != sisClone) {
          if(!onoff) {
             if(!quiet) {
                xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	           "CRT1 can't be switched off in MergedFB mode unless a clone mode is active\n");
	     }
             return FALSE;
          } else if(onoff == 2) {
             if(!(pSiS->SiS_SD3_Flags & SiS_SD3_SUPPORTDUALDVI)) {
                if(vbflags & (CRT2_LCD|CRT2_VGA)) {
	           if(!quiet) {
	              xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	   	         "CRT1 type can only be VGA while CRT2 is LCD or VGA\n");
	           }
                   return FALSE;
                }
	     }
          }
       }
       if(mode->Private) {
	  mode = ((SiSMergedDisplayModePtr)mode->Private)->CRT1;
       }
    }
#endif

    vbflags &= ~(DISPTYPE_CRT1 | SINGLE_MODE | MIRROR_MODE | CRT1_LCDA);
    vbflags3 &= ~(VB3_CRT1_TV | VB3_CRT1_LCD | VB3_CRT1_VGA);
    crt1off = 1;
    if(onoff > 0) {
       vbflags |= DISPTYPE_CRT1;
       crt1off = 0;
       if(onoff == 2) {
	  vbflags |= CRT1_LCDA;
	  vbflags3 |= VB3_CRT1_LCD;
	  if(!(pSiS->SiS_SD3_Flags & SiS_SD3_SUPPORTDUALDVI)) {
	     vbflags &= ~(CRT2_LCD|CRT2_VGA);
	  }
       } else {
          vbflags3 |= VB3_CRT1_VGA;
       }
       /* Remember: Dualhead not supported */
       if(vbflags & CRT2_ENABLE) vbflags |= MIRROR_MODE;
       else vbflags |= SINGLE_MODE;
    } else {
       vbflags |= SINGLE_MODE;
    }

    if(vbflags & CRT1_LCDA) {
       if(SiS_CheckModeCRT1(pScrn, mode, vbflags, pSiS->HaveCustomModes) < 0x14) {
          if(!quiet) {
             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		"Current mode not suitable for LCD-via-CRT1\n");
	  }
          return FALSE;
       }
    }

    pSiS->CRT1off = crt1off;
    pSiS->VBFlags = pSiS->VBFlags_backup = vbflags;
    pSiS->VBFlags3 = pSiS->VBFlags_backup3 = vbflags3;

    /* Sync the accelerators */
    (*pSiS->SyncAccel)(pScrn);

    pSiS->skipswitchcheck = TRUE;
    if(!(pScrn->SwitchMode(pScrn->scrnIndex, pScrn->currentMode, 0))) {
       pSiS->skipswitchcheck = FALSE;
       return FALSE;
    }
    pSiS->skipswitchcheck = FALSE;
    SISAdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
    return TRUE;
}

static Bool
SISRedetectCRT2Devices(ScrnInfoPtr pScrn)
{
    SISPtr pSiS = SISPTR(pScrn);

    if((pSiS->VGAEngine != SIS_300_VGA) && (pSiS->VGAEngine != SIS_315_VGA)) {
       return FALSE;
    }

#ifdef SISDUALHEAD
    if(pSiS->DualHeadMode) return FALSE;
#endif

    /* Sync the accelerators */
    (*pSiS->SyncAccel)(pScrn);

    if(SISRedetectCRT2Type(pScrn)) {
       /* If this returns TRUE, we need to reset the display mode */
       /* Sync the accelerators */
       (*pSiS->SyncAccel)(pScrn);
       pSiS->skipswitchcheck = TRUE;
       if(!(pScrn->SwitchMode(pScrn->scrnIndex, pScrn->currentMode, 0))) {
          pSiS->skipswitchcheck = FALSE;
          return FALSE;
       }
       pSiS->skipswitchcheck = FALSE;
       SISAdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
    }
    return TRUE;
}

static Bool
SISSwitchCRT2Type(ScrnInfoPtr pScrn, ULong newvbflags, Bool quiet)
{
    SISPtr pSiS = SISPTR(pScrn);
    Bool hcm = pSiS->HaveCustomModes;
    DisplayModePtr mode = pScrn->currentMode;

    /* Do NOT use this to switch from CRT2_LCD to CRT1_LCDA */

    /* Switching CRT2 to LCD or VGA will switch CRT1 to VGA if
     * previously LCD-via-CRT1
     */

    /* For usability reasons, the user should not simply "lose" one
     * of his output devices in MergedFB mode. Therefore, a switch
     * which might lead to this situation will not be performed in
     * MergedFB mode. (For example: If CRT1 is LCD-via-CRT1, don't
     * let the user switch CRT2 to LCD or VGA mode, because he
     * would lose one output device since LCD-via-CRT1 is only
     * supported together with TV, not any other CRT2 type.)
     * In Non-MergedFB mode, losing one output device is not
     * considered that harmful.
     * Update: We allow this if currently a "clone" display mode
     * is active.
     */

    /* Only on 300 and 315/330/340 series */
    if(pSiS->VGAEngine != SIS_300_VGA &&
       pSiS->VGAEngine != SIS_315_VGA) return FALSE;

    /* Only if there is a video bridge */
    if(!(pSiS->VBFlags2 & VB2_VIDEOBRIDGE)) return FALSE;

#ifdef SISDUALHEAD
    if(pSiS->DualHeadMode) return FALSE;
#endif

#define SiS_NewVBMask (CRT2_ENABLE|CRT1_LCDA|TV_PAL|TV_NTSC|TV_PALM|TV_PALN|TV_NTSCJ| \
		       TV_AVIDEO|TV_SVIDEO|TV_SCART|TV_HIVISION|TV_YPBPR|TV_YPBPRALL|\
		       TV_YPBPRAR)

    newvbflags &= SiS_NewVBMask;
    newvbflags |= pSiS->VBFlags & ~SiS_NewVBMask;

    if(!(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTLCDA)) {
       newvbflags &= ~CRT1_LCDA;
    }
    if(!(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTHIVISION)) {
       newvbflags &= ~TV_HIVISION;
    }
    if(!(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTYPBPR)) {
       newvbflags &= ~TV_YPBPR;
    }

#ifdef SISMERGED
    if(pSiS->MergedFB) {
       if((mode->Private) &&
          ((SiSMergedDisplayModePtr)mode->Private)->CRT2Position != sisClone) {
          if(!(newvbflags & CRT2_ENABLE)) {
             if(!quiet) {
	        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	           "CRT2 can't be switched off in MergedFB mode unless a clone mode is active\n");
	     }
	     return FALSE;
          }
          if(!(pSiS->SiS_SD3_Flags & SiS_SD3_SUPPORTDUALDVI)) {
             if((newvbflags & (CRT2_LCD|CRT2_VGA)) && (newvbflags & CRT1_LCDA)) {
                if(!quiet) {
	           xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	              "CRT2 type can only be TV while in LCD-via-CRT1 mode\n");
	        }
	        return FALSE;
	     }
          }
       }
       hcm = pSiS->HaveCustomModes2;
       if(mode->Private) {
	  mode = ((SiSMergedDisplayModePtr)mode->Private)->CRT2;
       }
    }
#endif

    if((!(newvbflags & CRT2_ENABLE)) && (!newvbflags & DISPTYPE_CRT1)) {
       if(!quiet) {
          xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
             "CRT2 can't be switched off while CRT1 is off\n");
       }
       return FALSE;
    }

    /* CRT2_LCD and CRT2_VGA overrule LCDA (in non-MergedFB mode) */
    if(!(pSiS->SiS_SD3_Flags & SiS_SD3_SUPPORTDUALDVI)) {
       if(newvbflags & (CRT2_LCD|CRT2_VGA)) {
          newvbflags &= ~CRT1_LCDA;
       }
    }

    /* Check if the current mode is suitable for desired output device (if any) */
    if(SiS_CheckModeCRT2(pScrn, mode, newvbflags, hcm) < 0x14) {
       if(!quiet) {
          xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	     "Current mode not suitable for desired CRT2 output device\n");
       }
       return FALSE;
    }

    /* Remember: Dualhead not supported */
    newvbflags &= ~(SINGLE_MODE | MIRROR_MODE);
    if((newvbflags & DISPTYPE_CRT1) && (newvbflags & CRT2_ENABLE)) {
       newvbflags |= MIRROR_MODE;
    } else {
       newvbflags |= SINGLE_MODE;
    }

    /* Sync the accelerators */
    (*pSiS->SyncAccel)(pScrn);

    pSiS->VBFlags = pSiS->VBFlags_backup = newvbflags;

    pSiS->skipswitchcheck = TRUE;
    if(!(pScrn->SwitchMode(pScrn->scrnIndex, pScrn->currentMode, 0))) {
       pSiS->skipswitchcheck = FALSE;
       return FALSE;
    }
    pSiS->skipswitchcheck = FALSE;
    SISAdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
    return TRUE;
}

static Bool
SISSwitchOutputType(ScrnInfoPtr pScrn, ULong newvbflags, ULong newvbflags3,
		ULong newvbflags4, Bool quiet)
{
    /* For future use */

    return FALSE;
}

static ULong
SiSCondToVBFlags(UShort cond, ULong GivenVBFlags)
{
    ULong vbflags = GivenVBFlags;

    /* No special treatment for NTSC-J here; conditions equal NTSC */
    if(cond) {
       vbflags &= ~(CRT2_ENABLE | CRT1_LCDA | TV_STANDARD | TV_INTERFACE);
       if((cond & SiS_CF2_TYPEMASK) == SiS_CF2_LCD) {
	  vbflags |= CRT2_LCD;
       } else if((cond & SiS_CF2_TYPEMASK) == SiS_CF2_TV) {
	  vbflags |= (CRT2_TV | TV_SVIDEO);
	  if(cond & SiS_CF2_TVPAL)  	  vbflags |= TV_PAL;
	  else if(cond & SiS_CF2_TVPALM)  vbflags |= (TV_PAL | TV_PALM);
	  else if(cond & SiS_CF2_TVPALN)  vbflags |= (TV_PAL | TV_PALN);
	  else if(cond & SiS_CF2_TVNTSC)  vbflags |= TV_NTSC;
       } else if((cond & SiS_CF2_TYPEMASK) == SiS_CF2_TVSPECIAL) {
          vbflags |= CRT2_TV;
	  if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVHIVISION)
		vbflags |= TV_HIVISION;
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR525I)
		vbflags |= (TV_YPBPR | TV_YPBPR525I);
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR525P)
		vbflags |= (TV_YPBPR | TV_YPBPR525P);
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR625I)
		vbflags |= (TV_YPBPR | TV_YPBPR625I);
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR625P)
		vbflags |= (TV_YPBPR | TV_YPBPR625P);
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR750P)
		vbflags |= (TV_YPBPR | TV_YPBPR750P);
	  else if((cond & SiS_CF2_TVSPECMASK) == SiS_CF2_TVYPBPR1080I)
		vbflags |= (TV_YPBPR | TV_YPBPR1080I);
       } else if((cond & SiS_CF2_TYPEMASK) == SiS_CF2_VGA2) {
	  vbflags |= CRT2_VGA;
       } else if((cond & SiS_CF2_TYPEMASK) == SiS_CF2_CRT1LCDA) {
	  vbflags |= CRT1_LCDA;
       }
    }
    return(vbflags);
}

static int
SISCheckModeForCRT2Type(ScrnInfoPtr pScrn, DisplayModePtr mode, ULong vbflags, UShort cond, Bool quiet)
{
    SISPtr pSiS = SISPTR(pScrn);
    DisplayModePtr mastermode;
    Bool hcm = pSiS->HaveCustomModes;
    int result = 0;

    mastermode = mode;

#ifdef SISDUALHEAD
    if((!pSiS->DualHeadMode) || (!pSiS->SecondHead)) {
#endif

       if(vbflags & CRT2_ENABLE) {

#ifdef SISMERGED
          if(pSiS->MergedFB) {
             hcm = pSiS->HaveCustomModes2;
             if(mode->Private) {
	        mode = ((SiSMergedDisplayModePtr)mode->Private)->CRT2;
             }
          }
#endif

          /* For RandR */
          if((mode->HDisplay > pScrn->virtualX) || (mode->VDisplay > pScrn->virtualY)) {
             if(!quiet) {
                xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		    "Desired mode too large for current screen size\n");
             }
             result |= 0x02;
          }

          /* Check if the desired mode is suitable for current CRT2 output device */
          if(SiS_CheckModeCRT2(pScrn, mode, vbflags, hcm) < 0x14) {
             if((!cond) && (!quiet)) {
                xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		    "Desired mode not suitable for current CRT2 output device\n");
             }
             result |= 0x01;
          }

       }

#ifdef SISDUALHEAD
    }
#endif

    mode = mastermode;

#ifdef SISDUALHEAD
    if((!pSiS->DualHeadMode) || (pSiS->SecondHead)) {
#endif

       if(vbflags & CRT1_LCDA) {

#ifdef SISMERGED
          if(pSiS->MergedFB) {
             hcm = pSiS->HaveCustomModes;
             if(mode->Private) {
	        mode = ((SiSMergedDisplayModePtr)mode->Private)->CRT1;
	     }
          }
#endif

	  /* For RandR */
	  if((mode->HDisplay > pScrn->virtualX) || (mode->VDisplay > pScrn->virtualY)) {
	     if(!quiet) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			"Desired mode too large for current screen size\n");
	     }
	     result |= 0x02;
	  }

	  /* Check if the desired mode is suitable for current CRT1 output device */
	  if(SiS_CheckModeCRT1(pScrn, mode, vbflags, hcm) < 0x14) {
	     if((!cond) && (!quiet)) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		      "Desired mode not suitable for current CRT1 output device\n");
	     }
	     result |= 0x01;
	  }

       }

#ifdef SISDUALHEAD
    }
#endif

    return result;
}

#ifdef XV_SD_DEPRECATED
static int
SISCheckModeIndexForCRT2Type(ScrnInfoPtr pScrn, UShort cond, UShort index, Bool quiet)
{
    SISPtr pSiS = SISPTR(pScrn);
    DisplayModePtr mode = pScrn->modes;
    ULong vbflags;
    int i;

    /* Not only CRT2, but also LCDA */

    /* returns 0 if mode ok,
     *         0x01 if mode not ok for CRT2 device,
     *         0x02 if mode too large for current root window
     *         or combinations thereof
     */

    vbflags = SiSCondToVBFlags(cond, pSiS->VBFlags);

    /* Find mode of given index */
    if(index) {
       for(i = 0; i < index; i++) {
          if(!mode) return 0x03;
          mode = mode->next;
       }
    }

    return(SISCheckModeForCRT2Type(pScrn, mode, vbflags, cond, quiet));
}
#endif

static DisplayModePtr
sisFindModeFromTiming(ScrnInfoPtr pScrn, UShort hdisplay,
			      UShort vdisplay, UShort htotal, UShort vtotal,
			      UShort hsyncstart, UShort hsyncend, UShort vsyncstart,
			      UShort vsyncend, int clock)
{
   DisplayModePtr mode = pScrn->modes, pmode = pScrn->modes;
   Bool found = FALSE;

   do {
       if( (mode->HDisplay == hdisplay) &&
           (mode->VDisplay == vdisplay) &&
	   (mode->HTotal == htotal) &&
	   (mode->VTotal == vtotal) &&
	   (mode->HSyncStart == hsyncstart) &&
	   (mode->VSyncStart == vsyncstart) &&
	   (mode->HSyncEnd == hsyncend) &&
	   (mode->VSyncEnd == vsyncend) &&
	   (mode->Clock == clock) ) {
	  found = TRUE;
	  break;
       }
       mode = mode->next;
    } while((mode) && (mode != pmode));

    if(found) return mode;
    else      return NULL;
}

static int
SISCheckModeTimingForCRT2Type(ScrnInfoPtr pScrn, UShort cond, UShort hdisplay,
			      UShort vdisplay, UShort htotal, UShort vtotal,
			      UShort hsyncstart, UShort hsyncend, UShort vsyncstart,
			      UShort vsyncend, int clock, Bool quiet)
{
    SISPtr pSiS = SISPTR(pScrn);
    DisplayModePtr mode;
    ULong vbflags;

    /* Not only CRT2, but also LCDA */

    /* returns 0 if mode ok,
     *         0x01 if mode not ok for CRT2 device,
     *         0x02 if mode too large for current root window
     *         or combinations thereof
     */

    vbflags = SiSCondToVBFlags(cond, pSiS->VBFlags);

    /* Find mode with given timing */
    mode = sisFindModeFromTiming(pScrn, hdisplay, vdisplay, htotal, vtotal,
				hsyncstart, hsyncend, vsyncstart, vsyncend,
				clock);
    if(!mode) return 0x03;

    return(SISCheckModeForCRT2Type(pScrn, mode, vbflags, cond, quiet));
}

#ifdef SISMERGED
static void
SISGetMergedModeDetails(ScrnInfoPtr pScrn,
	   int hd, int vd, int ht, int vt, int hss, int hse, int vss, int vse, int clk,
	   unsigned int *pos, unsigned int *crt1x, unsigned int *crt1y,
	   unsigned int *crt1clk, unsigned int *crt2x, unsigned int *crt2y,
	   unsigned int *crt2clk)
{
    SISPtr pSiS = SISPTR(pScrn);
    DisplayModePtr mode, tmode;

    if(!pSiS->MergedFB) {
       *pos = SDC_MMODE_POS_ERROR;
       return;
    }

    /* Find mode with given timing */
    mode = sisFindModeFromTiming(pScrn, hd, vd, ht, vt, hss, hse, vss, vse, clk);
    if(!mode) {
       *pos = SDC_MMODE_POS_ERROR;
       return;
    }

    switch(((SiSMergedDisplayModePtr)mode->Private)->CRT2Position) {
       case sisLeftOf:  *pos = SDC_MMODE_POS_LEFTOF;  break;
       case sisRightOf: *pos = SDC_MMODE_POS_RIGHTOF; break;
       case sisAbove:   *pos = SDC_MMODE_POS_ABOVE;   break;
       case sisBelow:   *pos = SDC_MMODE_POS_BELOW;   break;
       default:         *pos = SDC_MMODE_POS_CLONE;
    }

    tmode = ((SiSMergedDisplayModePtr)mode->Private)->CRT1;
    *crt1x = tmode->HDisplay;
    *crt1y = tmode->VDisplay;
    *crt1clk = (unsigned int)SiSCalcVRate(tmode);

    tmode = ((SiSMergedDisplayModePtr)mode->Private)->CRT2;
    *crt2x = tmode->HDisplay;
    *crt2y = tmode->VDisplay;
    *crt2clk = (unsigned int)SiSCalcVRate(tmode);
}
#endif

/***********************************
 *   SiSCtrl extension interface   *
 ***********************************/

static void
sisutil_prepare_string(xSiSCtrlCommandReply *sdcbuf, char *mystring)
{
   int slen = 0;
   sdcbuf->sdc_buffer[0] = 0;
   if(mystring) {
      slen = strlen(mystring);
      if(slen > 31) slen = 31;
      strncpy(&sdcbuf->sdc_buffer[0], mystring, slen);
      sdcbuf->sdc_buffer[slen] = 0;
   }
   sdcbuf->sdc_result[0] = slen;
}

static int
SiSHandleSiSDirectCommand(xSiSCtrlCommandReply *sdcbuf)
{
   ScrnInfoPtr pScrn = xf86Screens[sdcbuf->screen];
   SISPtr pSiS = SISPTR(pScrn);
#ifdef SISDUALHEAD
   SISEntPtr pSiSEnt = pSiS->entityPrivate;
#endif
   SISPortPrivPtr pPriv = NULL;
   int i;
   ULong j;

   if(sdcbuf->sdc_id != SDC_ID) return BadMatch;

   if(pSiS->adaptor) {
      pPriv = GET_PORT_PRIVATE(pScrn);
   }

   j = sdcbuf->sdc_command;
   for(i = 0; i < SDC_NUM_PARM_RESULT; i++) {
      j += sdcbuf->sdc_parm[i];
   }

   if(j != sdcbuf->sdc_chksum) return BadMatch;

   sdcbuf->sdc_result_header = SDC_RESULT_OK;

   switch(sdcbuf->sdc_command) {

   case SDC_CMD_GETVERSION:
      sdcbuf->sdc_result[0] = SDC_VERSION;
      sdcbuf->sdc_result[1] = SDC_MAXCOMMAND;
      break;

   case SDC_CMD_GETHWINFO:
      {
	 UShort romptr = 0;
	 char *biosvers = NULL;
	 int slen = 0;

	 if(pSiS->BIOS) {
	    romptr = pSiS->BIOS[0x16] | (pSiS->BIOS[0x17] << 8);
	    if(romptr < 65530) {
	       biosvers = (char *)&pSiS->BIOS[romptr];
	       slen = strlen(biosvers);
	       if(slen > 10) slen = 10;
	    }
	 }
	 sdcbuf->sdc_result[0]  = pSiS->IsAGPCard ? SDC_BUS_TYPE_AGP : SDC_BUS_TYPE_PCI;
	 if(pSiS->IsPCIExpress) sdcbuf->sdc_result[0] = SDC_BUS_TYPE_PCIE;
	 sdcbuf->sdc_result[1]  = pSiS->PciBus;
	 sdcbuf->sdc_result[2]  = pSiS->PciDevice;
	 sdcbuf->sdc_result[3]  = pSiS->PciFunc;
	 sdcbuf->sdc_result[4]  = pSiS->ROM661New ? 1 : (pSiS->HaveXGIBIOS ? 2 : 0),
	 sdcbuf->sdc_result[5]  = pSiS->ChipFlags;
	 sdcbuf->sdc_result[6]  = pSiS->ChipType;
	 sdcbuf->sdc_result[7]  = pSiS->ChipRev;
	 sdcbuf->sdc_result[8]  = SISDRIVERVERSIONYEAR;
	 sdcbuf->sdc_result[9]  = SISDRIVERVERSIONMONTH;
	 sdcbuf->sdc_result[10] = SISDRIVERVERSIONDAY;
	 sdcbuf->sdc_result[11] = SISDRIVERREVISION;
	 sdcbuf->sdc_result[12] = pScrn->videoRam;
	 sdcbuf->sdc_result[13] = pSiS->UMAsize;
	 sdcbuf->sdc_result[14] = pSiS->LFBsize;
	 if(slen) strncpy(&sdcbuf->sdc_buffer[0], biosvers, slen);
	 sdcbuf->sdc_buffer[slen] = 0;
      }
      break;

   case SDC_CMD_GETALLFLAGS:
      sdcbuf->sdc_result[0] = SIS_VBFlagsVersion;
      sdcbuf->sdc_result[1] = pSiS->VBFlags;
      sdcbuf->sdc_result[2] = pSiS->SiS_SD_Flags;
      sdcbuf->sdc_result[3] = pSiS->SiS_SD2_Flags;
      sdcbuf->sdc_result[4] = pSiS->detectedCRT2Devices;
      sdcbuf->sdc_result[5] = pSiS->VBFlags2;
      sdcbuf->sdc_result[6] = pSiS->SiS_SD3_Flags;
      sdcbuf->sdc_result[7] = pSiS->SiS_SD4_Flags;
      sdcbuf->sdc_result[8] = pSiS->VBFlags3;
      sdcbuf->sdc_result[9] = pSiS->VBFlags4;
      break;

   case SDC_CMD_GETVBFLAGSVERSION:
      sdcbuf->sdc_result[0] = SIS_VBFlagsVersion;
      break;

   case SDC_CMD_GETVBFLAGS:
      sdcbuf->sdc_result[0] = pSiS->VBFlags;
      sdcbuf->sdc_result[1] = pSiS->VBFlags2;
      sdcbuf->sdc_result[2] = pSiS->VBFlags3;
      sdcbuf->sdc_result[3] = pSiS->VBFlags4;
      break;

   case SDC_CMD_CHECKMODEFORCRT2:
      {
	 int clk, hd, hss, hse, ht, vd, vss, vse, vt, result;
	 ULong flags;

	 flags = sdcbuf->sdc_parm[0];
	 clk   = sdcbuf->sdc_parm[1];
	 hd    = sdcbuf->sdc_parm[2];
	 hss   = sdcbuf->sdc_parm[3];
	 hse   = sdcbuf->sdc_parm[4];
	 ht    = sdcbuf->sdc_parm[5];
	 vd    = sdcbuf->sdc_parm[6];
	 vss   = sdcbuf->sdc_parm[7];
	 vse   = sdcbuf->sdc_parm[8];
	 vt    = sdcbuf->sdc_parm[9];

	 result = SISCheckModeTimingForCRT2Type(pScrn, (UShort)(flags & 0xff),
			hd, vd, ht, vt, hss, hse, vss, vse, clk, pSiS->SCLogQuiet) & 0xff;
	 sdcbuf->sdc_result[0] = result;
      }
      break;

   case SDC_CMD_SETVBFLAGS:
#ifdef SISDUALHEAD
      if(!pSiS->DualHeadMode) {
#endif
	 if(pSiS->xv_sisdirectunlocked) {
	    SISSwitchCRT2Type(pScrn, (ULong)sdcbuf->sdc_parm[0], pSiS->SCLogQuiet);
	    if(pPriv) SISUpdateVideoParms(pSiS, pPriv);
	 } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#ifdef SISDUALHEAD
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#endif
      break;

   case SDC_CMD_NEWSETVBFLAGS:
#ifdef SISDUALHEAD
      if(!pSiS->DualHeadMode) {
#endif
	 if(pSiS->xv_sisdirectunlocked) {
	    SISSwitchOutputType(pScrn, (ULong)sdcbuf->sdc_parm[0], (ULong)sdcbuf->sdc_parm[1],
	    		(ULong)sdcbuf->sdc_parm[2], pSiS->SCLogQuiet);
	    if(pPriv) SISUpdateVideoParms(pSiS, pPriv);
	 } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#ifdef SISDUALHEAD
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#endif
      break;

   case SDC_CMD_GETDETECTEDDEVICES:
      sdcbuf->sdc_result[0] = pSiS->detectedCRT2Devices;
      break;

   case SDC_CMD_REDETECTCRT2DEVICES:
#ifdef SISDUALHEAD
      if(!pSiS->DualHeadMode) {
#endif
	 if(pSiS->xv_sisdirectunlocked) {
	    SISRedetectCRT2Devices(pScrn);
	 } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#ifdef SISDUALHEAD
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#endif
      break;

   case SDC_CMD_GETCRT1STATUS:
      sdcbuf->sdc_result[0] = pSiS->CRT1isoff ? 0 : 1;
      break;

   case SDC_CMD_SETCRT1STATUS:
#ifdef SISDUALHEAD
      if(!pSiS->DualHeadMode) {
#endif
	 if(pSiS->xv_sisdirectunlocked) {
	    SISSwitchCRT1Status(pScrn, (ULong)sdcbuf->sdc_parm[0], pSiS->SCLogQuiet);
	    if(pPriv) SISUpdateVideoParms(pSiS, pPriv);
	 } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#ifdef SISDUALHEAD
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
#endif
      break;

   case SDC_CMD_GETSDFLAGS:
      sdcbuf->sdc_result[0] = pSiS->SiS_SD_Flags;
      break;

   case SDC_CMD_GETSD2FLAGS:
      sdcbuf->sdc_result[0] = pSiS->SiS_SD2_Flags;
      break;

   case SDC_CMD_GETLOCKSTATUS:
      sdcbuf->sdc_result[0] = pSiS->xv_sisdirectunlocked;
      break;

   case SDC_CMD_SETLOCKSTATUS:
      if(pSiS->enablesisctrl) {
	 if(sdcbuf->sdc_parm[0] == SIS_DIRECTKEY) {
	    pSiS->xv_sisdirectunlocked++;
	 } else if(pSiS->xv_sisdirectunlocked) {
	    pSiS->xv_sisdirectunlocked--;
	 }
      } else {
	 pSiS->xv_sisdirectunlocked = 0;
	 sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      }
      break;

   case SDC_CMD_GETTVANTIFLICKER:
      sdcbuf->sdc_result[0] = SiS_GetSISTVantiflicker(pScrn);
      break;

   case SDC_CMD_SETTVANTIFLICKER:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISTVantiflicker(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVSATURATION:
      sdcbuf->sdc_result[0] = SiS_GetSISTVsaturation(pScrn);
      break;

   case SDC_CMD_SETTVSATURATION:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISTVsaturation(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVEDGEENHANCE:
      sdcbuf->sdc_result[0] = SiS_GetSISTVedgeenhance(pScrn);;
      break;

   case SDC_CMD_SETTVEDGEENHANCE:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISTVedgeenhance(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCFILTER:
      sdcbuf->sdc_result[0] = SiS_GetSISTVcfilter(pScrn);
      break;

   case SDC_CMD_SETTVCFILTER:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISTVcfilter(pScrn, sdcbuf->sdc_parm[0] ? 1 : 0);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVYFILTER:
      sdcbuf->sdc_result[0] = SiS_GetSISTVyfilter(pScrn);
      break;

   case SDC_CMD_SETTVYFILTER:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISTVyfilter(pScrn, sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCOLORCALIB:
      sdcbuf->sdc_result[0] = SiS_GetSISTVcolcalib(pScrn, TRUE) + 32768;
      sdcbuf->sdc_result[1] = SiS_GetSISTVcolcalib(pScrn, FALSE) + 32768;
      break;

   case SDC_CMD_SETTVCOLORCALIB:
      if(pSiS->xv_sisdirectunlocked) {
         SiS_SetSISTVcolcalib(pScrn, (int)sdcbuf->sdc_parm[0] - 32768, TRUE);
	 SiS_SetSISTVcolcalib(pScrn, (int)sdcbuf->sdc_parm[1] - 32768, FALSE);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCHCONTRAST:
      sdcbuf->sdc_result[0] = SiS_GetCHTVcontrast(pScrn);
      break;

   case SDC_CMD_SETTVCHCONTRAST:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetCHTVcontrast(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCHTEXTENHANCE:
      sdcbuf->sdc_result[0] = SiS_GetCHTVtextenhance(pScrn);
      break;

   case SDC_CMD_SETTVCHTEXTENHANCE:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetCHTVtextenhance(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCHCHROMAFLICKERFILTER:
      sdcbuf->sdc_result[0] = SiS_GetCHTVchromaflickerfilter(pScrn);
      break;

   case SDC_CMD_SETTVCHCHROMAFLICKERFILTER:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetCHTVchromaflickerfilter(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCHLUMAFLICKERFILTER:
      sdcbuf->sdc_result[0] = SiS_GetCHTVlumaflickerfilter(pScrn);
      break;

   case SDC_CMD_SETTVCHLUMAFLICKERFILTER:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetCHTVlumaflickerfilter(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVCHCVBSCOLOR:
      sdcbuf->sdc_result[0] = SiS_GetCHTVcvbscolor(pScrn);
      break;

   case SDC_CMD_SETTVCHCVBSCOLOR:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetCHTVcvbscolor(pScrn, sdcbuf->sdc_parm[0] ? 1 : 0);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETCHTVOVERSCAN:
      {  /* Changing of CRT2 settings not supported in DHM! */
	 int i = 0;
	 if(pSiS->OptTVSOver == 1)         i = 3;
	 else if(pSiS->UseCHOverScan == 1) i = 2;
	 else if(pSiS->UseCHOverScan == 0) i = 1;
	 sdcbuf->sdc_result[0] = i;
      }
      break;

   case SDC_CMD_SETCHTVOVERSCAN:
      if(pSiS->xv_sisdirectunlocked) {
	 pSiS->UseCHOverScan = -1;
	 pSiS->OptTVSOver = FALSE;
	 if(sdcbuf->sdc_parm[0] == 3) {
	    if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSOVER) {
	       pSiS->OptTVSOver = TRUE;
	    }
	    pSiS->UseCHOverScan = 1;
	 } else if(sdcbuf->sdc_parm[0] == 2) pSiS->UseCHOverScan = 1;
	 else if(sdcbuf->sdc_parm[0] == 1)   pSiS->UseCHOverScan = 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETGAMMASTATUS:
      {
         int i = 0;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    if(pSiSEnt->CRT1gamma) i |= 0x01;
	    if(pSiSEnt->CRT2gamma) i |= 0x02;
	 } else {
#endif
	    if(pSiS->CRT1gamma)    i |= 0x01;
	    if(pSiS->CRT2gamma)    i |= 0x02;
	    if(pSiS->CRT2SepGamma) i |= 0x08;
#ifdef SISDUALHEAD
	 }
#endif
	 if(pSiS->XvGamma) i |= 0x04;
         sdcbuf->sdc_result[0] = i;
      }
      break;

   case SDC_CMD_SETGAMMASTATUS:
      if(pSiS->xv_sisdirectunlocked) {
         int value = sdcbuf->sdc_parm[0];
	 Bool backup = pSiS->XvGamma;
	 Bool backup2 = pSiS->CRT2SepGamma;
	 pSiS->CRT1gamma = (value & 0x01) ? TRUE : FALSE;
	 pSiS->CRT2gamma = (value & 0x02) ? TRUE : FALSE;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    pSiSEnt->CRT1gamma = pSiS->CRT1gamma;
	    pSiSEnt->CRT2gamma = pSiS->CRT2gamma;
	 }
#endif
	 if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSGRCRT2) {
	    pSiS->CRT2SepGamma = (value & 0x08) ? TRUE : FALSE;
	    if(pSiS->CRT2SepGamma != backup2) {
	       if(pSiS->CRT2SepGamma) {
		  SiS_UpdateGammaCRT2(pScrn);
	       }
#ifdef SISGAMMARAMP
	       else {
		  SISCalculateGammaRamp(pScrn->pScreen, pScrn);
	       }
#endif
	      /* ATTN: When disabling CRT2 sep gamma,
	       * as long as SISGAMMARAMP is not defined,
	       * application needs to reset palette using
	       * vidmodextension! (See SiS_SD_CANSETGAMMA)
	       *
	       * Note: Difference between setting gamma values
	       * for CRT1 and CRT2: For CRT1, driver only
	       * stores the values, but does nothing with them;
	       * App needs to calc gamma ramp itself for CRT1,
	       * and set it using the VidModExtention.
	       * For CRT2, the driver will IMMEDIATELY recalc
	       * the palette.
	       */
	    }
	 } else pSiS->CRT2SepGamma = FALSE;

	 pSiS->XvGamma = (value & 0x04) ? TRUE : FALSE;
	 if(pSiS->VGAEngine == SIS_315_VGA) {
	    if(backup != pSiS->XvGamma) {
	       if(pPriv) SiSUpdateXvGamma(pSiS, pPriv);
	    }
	 }
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETTVXSCALE:
      sdcbuf->sdc_result[0] = SiS_GetTVxscale(pScrn) + 32768;
      break;

   case SDC_CMD_SETTVXSCALE:
      {
         int value = sdcbuf->sdc_parm[0] - 32768;
         if((value < -16) || (value > 16)) {
	    sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
         } else if(pSiS->xv_sisdirectunlocked) {
	    SiS_SetTVxscale(pScrn, value);
         } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      }
      break;

   case SDC_CMD_GETTVYSCALE:
      sdcbuf->sdc_result[0] = SiS_GetTVyscale(pScrn) + 32768;
      break;

   case SDC_CMD_SETTVYSCALE:
      {
         int value = sdcbuf->sdc_parm[0] - 32768;
         if((value < -4) || (value > 3)) {
	    sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
         } else if(pSiS->xv_sisdirectunlocked) {
	    SiS_SetTVyscale(pScrn, value);
         } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      }
      break;

   case SDC_CMD_GETSCREENSIZE:
      sdcbuf->sdc_result[0] = (pScrn->virtualX << 16) | pScrn->virtualY;
      break;

   case SDC_CMD_GETGAMMABRIGHTNESS:  /* xv_BRx, xv_PBx */
      sdcbuf->sdc_result[0] = pSiS->GammaBriR;
      sdcbuf->sdc_result[1] = pSiS->GammaBriG;
      sdcbuf->sdc_result[2] = pSiS->GammaBriB;
      break;

   case SDC_CMD_GETNEWGAMMABRICON:  /* no xv pendant */
      sdcbuf->sdc_result[0] = (CARD32)(((int)(pSiS->NewGammaBriR * 1000.0)) + 1000);
      sdcbuf->sdc_result[1] = (CARD32)(((int)(pSiS->NewGammaBriG * 1000.0)) + 1000);
      sdcbuf->sdc_result[2] = (CARD32)(((int)(pSiS->NewGammaBriB * 1000.0)) + 1000);
      sdcbuf->sdc_result[3] = (CARD32)(((int)(pSiS->NewGammaConR * 1000.0)) + 1000);
      sdcbuf->sdc_result[4] = (CARD32)(((int)(pSiS->NewGammaConG * 1000.0)) + 1000);
      sdcbuf->sdc_result[5] = (CARD32)(((int)(pSiS->NewGammaConB * 1000.0)) + 1000);
      break;

   case SDC_CMD_SETGAMMABRIGHTNESS:  /* xv_BRx, xv_PBx */
      if(sdcbuf->sdc_parm[0] < 100 || sdcbuf->sdc_parm[0] > 10000 ||
	 sdcbuf->sdc_parm[1] < 100 || sdcbuf->sdc_parm[1] > 10000 ||
	 sdcbuf->sdc_parm[2] < 100 || sdcbuf->sdc_parm[2] > 10000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
	 pSiS->GammaBriR = sdcbuf->sdc_parm[0];
	 pSiS->GammaBriG = sdcbuf->sdc_parm[1];
	 pSiS->GammaBriB = sdcbuf->sdc_parm[2];
	 pSiS->NewGammaBriR = pSiS->NewGammaBriG = pSiS->NewGammaBriB = 0.0;
	 pSiS->NewGammaConR = pSiS->NewGammaConG = pSiS->NewGammaConB = 0.0;
	 pSiS->SiS_SD3_Flags |= SiS_SD3_OLDGAMMAINUSE;
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_SETNEWGAMMABRICON:  /* no xv pendant */
      if(sdcbuf->sdc_parm[0] > 2000 || sdcbuf->sdc_parm[1] > 2000 ||
	 sdcbuf->sdc_parm[2] > 2000 || sdcbuf->sdc_parm[3] > 2000 ||
	 sdcbuf->sdc_parm[4] > 2000 || sdcbuf->sdc_parm[5] > 2000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
	 pSiS->NewGammaBriR = ((float)((int)sdcbuf->sdc_parm[0] - 1000)) / 1000.0;
	 pSiS->NewGammaBriG = ((float)((int)sdcbuf->sdc_parm[1] - 1000)) / 1000.0;
	 pSiS->NewGammaBriB = ((float)((int)sdcbuf->sdc_parm[2] - 1000)) / 1000.0;
	 pSiS->NewGammaConR = ((float)((int)sdcbuf->sdc_parm[3] - 1000)) / 1000.0;
	 pSiS->NewGammaConG = ((float)((int)sdcbuf->sdc_parm[4] - 1000)) / 1000.0;
	 pSiS->NewGammaConB = ((float)((int)sdcbuf->sdc_parm[5] - 1000)) / 1000.0;
	 pSiS->GammaBriR = pSiS->GammaBriG = pSiS->GammaBriB = 1000;
	 pSiS->SiS_SD3_Flags &= ~SiS_SD3_OLDGAMMAINUSE;
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETGAMMABRIGHTNESS2: /* xv_BRx2, xv_PBx2 */
#ifdef SISDUALHEAD
      if(pSiS->DualHeadMode) {
         sdcbuf->sdc_result[0] = pSiSEnt->GammaBriR;
	 sdcbuf->sdc_result[1] = pSiSEnt->GammaBriG;
	 sdcbuf->sdc_result[2] = pSiSEnt->GammaBriB;
	 break;
      }
#endif
      sdcbuf->sdc_result[0] = pSiS->GammaBriR;
      sdcbuf->sdc_result[1] = pSiS->GammaBriG;
      sdcbuf->sdc_result[2] = pSiS->GammaBriB;
      break;

   case SDC_CMD_GETNEWGAMMABRICON2: /* no xv pendant */
#ifdef SISDUALHEAD
      if(pSiS->DualHeadMode) {
         sdcbuf->sdc_result[0] = (CARD32)(((int)(pSiSEnt->NewGammaBriR * 1000.0)) + 1000);
	 sdcbuf->sdc_result[1] = (CARD32)(((int)(pSiSEnt->NewGammaBriG * 1000.0)) + 1000);
	 sdcbuf->sdc_result[2] = (CARD32)(((int)(pSiSEnt->NewGammaBriB * 1000.0)) + 1000);
	 sdcbuf->sdc_result[3] = (CARD32)(((int)(pSiSEnt->NewGammaConR * 1000.0)) + 1000);
	 sdcbuf->sdc_result[4] = (CARD32)(((int)(pSiSEnt->NewGammaConG * 1000.0)) + 1000);
	 sdcbuf->sdc_result[5] = (CARD32)(((int)(pSiSEnt->NewGammaConB * 1000.0)) + 1000);
	 break;
      }
#endif
      sdcbuf->sdc_result[0] = (CARD32)(((int)(pSiS->NewGammaBriR * 1000.0)) + 1000);
      sdcbuf->sdc_result[1] = (CARD32)(((int)(pSiS->NewGammaBriG * 1000.0)) + 1000);
      sdcbuf->sdc_result[2] = (CARD32)(((int)(pSiS->NewGammaBriB * 1000.0)) + 1000);
      sdcbuf->sdc_result[3] = (CARD32)(((int)(pSiS->NewGammaConR * 1000.0)) + 1000);
      sdcbuf->sdc_result[4] = (CARD32)(((int)(pSiS->NewGammaConG * 1000.0)) + 1000);
      sdcbuf->sdc_result[5] = (CARD32)(((int)(pSiS->NewGammaConB * 1000.0)) + 1000);
      break;

   case SDC_CMD_SETGAMMABRIGHTNESS2: /* xv_BRx2, xv_PBx2 */
      if(sdcbuf->sdc_parm[0] < 100 || sdcbuf->sdc_parm[0] > 10000 ||
	 sdcbuf->sdc_parm[1] < 100 || sdcbuf->sdc_parm[1] > 10000 ||
	 sdcbuf->sdc_parm[2] < 100 || sdcbuf->sdc_parm[2] > 10000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
         pSiS->SiS_SD3_Flags |= SiS_SD3_OLDGAMMAINUSE;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    pSiSEnt->GammaBriR = sdcbuf->sdc_parm[0];
	    pSiSEnt->GammaBriG = sdcbuf->sdc_parm[1];
	    pSiSEnt->GammaBriB = sdcbuf->sdc_parm[2];
	    pSiSEnt->NewGammaBriR = pSiSEnt->NewGammaBriG = pSiSEnt->NewGammaBriB = 0.0;
	    pSiSEnt->NewGammaConR = pSiSEnt->NewGammaConG = pSiSEnt->NewGammaConB = 0.0;
	 }
#endif
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_SETNEWGAMMABRICON2: /* no xv pendant */
      if(sdcbuf->sdc_parm[0] > 2000 || sdcbuf->sdc_parm[1] > 2000 ||
	 sdcbuf->sdc_parm[2] > 2000 || sdcbuf->sdc_parm[3] > 2000 ||
	 sdcbuf->sdc_parm[4] > 2000 || sdcbuf->sdc_parm[5] > 2000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
         pSiS->SiS_SD3_Flags &= ~SiS_SD3_OLDGAMMAINUSE;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    pSiSEnt->NewGammaBriR = ((float)((int)sdcbuf->sdc_parm[0] - 1000)) / 1000.0;
	    pSiSEnt->NewGammaBriG = ((float)((int)sdcbuf->sdc_parm[1] - 1000)) / 1000.0;
	    pSiSEnt->NewGammaBriB = ((float)((int)sdcbuf->sdc_parm[2] - 1000)) / 1000.0;
	    pSiSEnt->NewGammaConR = ((float)((int)sdcbuf->sdc_parm[3] - 1000)) / 1000.0;
	    pSiSEnt->NewGammaConG = ((float)((int)sdcbuf->sdc_parm[4] - 1000)) / 1000.0;
	    pSiSEnt->NewGammaConB = ((float)((int)sdcbuf->sdc_parm[5] - 1000)) / 1000.0;
	    pSiSEnt->GammaBriR = pSiSEnt->GammaBriG = pSiSEnt->GammaBriB = 1000;
	 }
#endif
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETGETGAMMACRT2:
      sdcbuf->sdc_result[0] = (ULong)(pSiS->GammaR2 * 1000);
      sdcbuf->sdc_result[1] = (ULong)(pSiS->GammaG2 * 1000);
      sdcbuf->sdc_result[2] = (ULong)(pSiS->GammaB2 * 1000);
      sdcbuf->sdc_result[3] = pSiS->GammaBriR2;
      sdcbuf->sdc_result[4] = pSiS->GammaBriG2;
      sdcbuf->sdc_result[5] = pSiS->GammaBriB2;
      break;

   case SDC_CMD_GETGETNEWGAMMACRT2:
      sdcbuf->sdc_result[0] = (ULong)(pSiS->GammaR2 * 1000);
      sdcbuf->sdc_result[1] = (ULong)(pSiS->GammaG2 * 1000);
      sdcbuf->sdc_result[2] = (ULong)(pSiS->GammaB2 * 1000);
      sdcbuf->sdc_result[3] = (CARD32)(((int)(pSiS->NewGammaBriR2 * 1000.0)) + 1000);
      sdcbuf->sdc_result[4] = (CARD32)(((int)(pSiS->NewGammaBriG2 * 1000.0)) + 1000);
      sdcbuf->sdc_result[5] = (CARD32)(((int)(pSiS->NewGammaBriB2 * 1000.0)) + 1000);
      sdcbuf->sdc_result[6] = (CARD32)(((int)(pSiS->NewGammaConR2 * 1000.0)) + 1000);
      sdcbuf->sdc_result[7] = (CARD32)(((int)(pSiS->NewGammaConG2 * 1000.0)) + 1000);
      sdcbuf->sdc_result[8] = (CARD32)(((int)(pSiS->NewGammaConB2 * 1000.0)) + 1000);
      break;

   case SDC_CMD_SETGETGAMMACRT2:
      if(sdcbuf->sdc_parm[0] < 100 || sdcbuf->sdc_parm[0] > 10000 ||
	 sdcbuf->sdc_parm[1] < 100 || sdcbuf->sdc_parm[1] > 10000 ||
	 sdcbuf->sdc_parm[2] < 100 || sdcbuf->sdc_parm[2] > 10000 ||
	 sdcbuf->sdc_parm[3] < 100 || sdcbuf->sdc_parm[3] > 10000 ||
	 sdcbuf->sdc_parm[4] < 100 || sdcbuf->sdc_parm[4] > 10000 ||
	 sdcbuf->sdc_parm[5] < 100 || sdcbuf->sdc_parm[5] > 10000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
         pSiS->GammaR2 = (float)sdcbuf->sdc_parm[0] / (float)1000;
	 pSiS->GammaG2 = (float)sdcbuf->sdc_parm[1] / (float)1000;
	 pSiS->GammaB2 = (float)sdcbuf->sdc_parm[2] / (float)1000;
	 pSiS->GammaBriR2 = sdcbuf->sdc_parm[3];
	 pSiS->GammaBriG2 = sdcbuf->sdc_parm[4];
	 pSiS->GammaBriB2 = sdcbuf->sdc_parm[5];
	 pSiS->NewGammaBriR2 = pSiS->NewGammaBriG2 = pSiS->NewGammaBriB2 = 0.0;
	 pSiS->NewGammaConR2 = pSiS->NewGammaConG2 = pSiS->NewGammaConB2 = 0.0;
	 pSiS->SiS_SD3_Flags |= SiS_SD3_OLDGAMMAINUSE;
	 SiS_UpdateGammaCRT2(pScrn);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_SETGETNEWGAMMACRT2:
      if(sdcbuf->sdc_parm[0] < 100 || sdcbuf->sdc_parm[0] > 10000 ||
	 sdcbuf->sdc_parm[1] < 100 || sdcbuf->sdc_parm[1] > 10000 ||
	 sdcbuf->sdc_parm[2] < 100 || sdcbuf->sdc_parm[2] > 10000 ||
	 sdcbuf->sdc_parm[3] > 2000 || sdcbuf->sdc_parm[4] > 2000 ||
	 sdcbuf->sdc_parm[5] > 2000 || sdcbuf->sdc_parm[6] > 2000 ||
	 sdcbuf->sdc_parm[7] > 2000 || sdcbuf->sdc_parm[8] > 2000) {
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else if(pSiS->xv_sisdirectunlocked) {
         pSiS->GammaR2 = (float)sdcbuf->sdc_parm[0] / (float)1000;
	 pSiS->GammaG2 = (float)sdcbuf->sdc_parm[1] / (float)1000;
	 pSiS->GammaB2 = (float)sdcbuf->sdc_parm[2] / (float)1000;
	 pSiS->NewGammaBriR2 = ((float)((int)sdcbuf->sdc_parm[3] - 1000)) / 1000.0;
	 pSiS->NewGammaBriG2 = ((float)((int)sdcbuf->sdc_parm[4] - 1000)) / 1000.0;
	 pSiS->NewGammaBriB2 = ((float)((int)sdcbuf->sdc_parm[5] - 1000)) / 1000.0;
	 pSiS->NewGammaConR2 = ((float)((int)sdcbuf->sdc_parm[6] - 1000)) / 1000.0;
	 pSiS->NewGammaConG2 = ((float)((int)sdcbuf->sdc_parm[7] - 1000)) / 1000.0;
	 pSiS->NewGammaConB2 = ((float)((int)sdcbuf->sdc_parm[8] - 1000)) / 1000.0;
	 pSiS->GammaBriR2 = pSiS->GammaBriG2 = pSiS->GammaBriB2 = 1000;
	 pSiS->SiS_SD3_Flags &= ~SiS_SD3_OLDGAMMAINUSE;
	 SiS_UpdateGammaCRT2(pScrn);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETHWCURSORSTATUS:
      sdcbuf->sdc_result[0] = pSiS->HideHWCursor ? 1 : 0;
      break;

   case SDC_CMD_SETHWCURSORSTATUS:
      if(pSiS->xv_sisdirectunlocked) {
	 Bool VisibleBackup = pSiS->HWCursorIsVisible;
	 pSiS->HideHWCursor = sdcbuf->sdc_parm[0] ? TRUE : FALSE;
	 if(pSiS->CursorInfoPtr) {
	    if(VisibleBackup) {
	       if(sdcbuf->sdc_parm[0]) {
	 	  (pSiS->CursorInfoPtr->HideCursor)(pScrn);
	       } else {
		  (pSiS->CursorInfoPtr->ShowCursor)(pScrn);
	       }
	    }
	    pSiS->HWCursorIsVisible = VisibleBackup;
	 }
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETPANELMODE:
      {
	 int i = 0;
	 if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSCALE) {
	    switch(pSiS->SiS_Pr->UsePanelScaler) {
	    case -1: i |= 0x01; break;
	    case 1:  i |= 0x02; break;
	    }
	    if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTCENTER) {
	       switch(pSiS->SiS_Pr->CenterScreen) {
	       case -1: i |= 0x04; break;
	       case 1:  i |= 0x08; break;
	       }
	    }
	 }
	 sdcbuf->sdc_result[0] = i;
      }
      break;

   case SDC_CMD_SETPANELMODE:
      if(pSiS->xv_sisdirectunlocked) {
         int value = sdcbuf->sdc_parm[0];
         if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSCALE) {
	    if(value & 0x01)      pSiS->SiS_Pr->UsePanelScaler = -1;
	    else if(value & 0x02) pSiS->SiS_Pr->UsePanelScaler = 1;
	    else		  pSiS->SiS_Pr->UsePanelScaler = 0;
	    if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTCENTER) {
	       if(value & 0x04)      pSiS->SiS_Pr->CenterScreen = -1;
	       else if(value & 0x08) pSiS->SiS_Pr->CenterScreen = 1;
	       else		     pSiS->SiS_Pr->CenterScreen = 0;
	    }
         }
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   case SDC_CMD_GETMERGEDMODEDETAILS:
#ifdef SISMERGED
      if(pSiS->MergedFB) {
         int clk, hd, hss, hse, ht, vd, vss, vse, vt;
	 unsigned int pos, crt1x, crt1y, crt1clk, crt2x, crt2y, crt2clk;

	 clk   = sdcbuf->sdc_parm[0];
	 hd    = sdcbuf->sdc_parm[1];
	 hss   = sdcbuf->sdc_parm[2];
	 hse   = sdcbuf->sdc_parm[3];
	 ht    = sdcbuf->sdc_parm[4];
	 vd    = sdcbuf->sdc_parm[5];
	 vss   = sdcbuf->sdc_parm[6];
	 vse   = sdcbuf->sdc_parm[7];
	 vt    = sdcbuf->sdc_parm[8];

	 SISGetMergedModeDetails(pScrn,
		hd, vd, ht, vt, hss, hse, vss, vse, clk,
		&pos, &crt1x, &crt1y, &crt1clk, &crt2x, &crt2y, &crt2clk);

	 sdcbuf->sdc_result[0] = pos;
	 sdcbuf->sdc_result[1] = crt1x;
	 sdcbuf->sdc_result[2] = crt1y;
	 sdcbuf->sdc_result[3] = crt1clk;
	 sdcbuf->sdc_result[4] = crt2x;
	 sdcbuf->sdc_result[5] = crt2y;
	 sdcbuf->sdc_result[6] = crt2clk;

	 switch(pSiS->CRT2Position) {
	 case sisLeftOf:  sdcbuf->sdc_result[7] = SDC_MMODE_POS_LEFTOF; break;
	 case sisRightOf: sdcbuf->sdc_result[7] = SDC_MMODE_POS_RIGHTOF; break;
	 case sisAbove:   sdcbuf->sdc_result[7] = SDC_MMODE_POS_ABOVE; break;
	 case sisBelow:   sdcbuf->sdc_result[7] = SDC_MMODE_POS_BELOW; break;
	 default:         sdcbuf->sdc_result[7] = SDC_MMODE_POS_CLONE;
	 }

      } else
#endif
         sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETDEVICENAME:		/* In DualHead mode, this returns CRT1 data */
      {
         SISPtr	mypSiS = pSiS;
	 sdcbuf->sdc_result[0] = 0;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    if(pSiSEnt->pScrn_2) mypSiS = SISPTR(pSiSEnt->pScrn_2);
	 }
#endif
	 sisutil_prepare_string(sdcbuf, mypSiS->devsectname);
      }
      break;

   case SDC_CMD_GETMONITORNAME:		/* In DualHead mode, this returns CRT1 data */
      {
         ScrnInfoPtr mypScrn = pScrn;
         sdcbuf->sdc_result[0] = 0;
#ifdef SISDUALHEAD
	 if(pSiS->DualHeadMode) {
	    if(pSiSEnt->pScrn_2) mypScrn = pSiSEnt->pScrn_2;
	 }
#endif
         if(mypScrn->monitor) {
            sisutil_prepare_string(sdcbuf, mypScrn->monitor->id);
         }
      }
      break;

   case SDC_CMD_GETDEVICENAME2:		/* In DualHead mode, this returns CRT2 data */
      sdcbuf->sdc_result[0] = 0;
#ifdef SISDUALHEAD
      if(pSiS->DualHeadMode) {
         if(pSiSEnt->pScrn_1) {
	    sisutil_prepare_string(sdcbuf, SISPTR(pSiSEnt->pScrn_1)->devsectname);
	 }
      } else
#endif
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETMONITORNAME2:	/* In DualHead mode, this returns CRT2 data */
      sdcbuf->sdc_result[0] = 0;
#ifdef SISDUALHEAD
      if(pSiS->DualHeadMode) {
         if(pSiSEnt->pScrn_1) {
	    if(pSiSEnt->pScrn_1->monitor) {
               sisutil_prepare_string(sdcbuf, pSiSEnt->pScrn_1->monitor->id);
            }
	 }
      } else
#endif
	 sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVBRIGHTNESS:
      if(pPriv) {
         int val = sdcbuf->sdc_parm[0] - 32768;
         if(val >= -128 && val <= 127) pPriv->brightness = val;
	 else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVBRIGHTNESS:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->brightness + 32768;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVCONTRAST:
      if(pPriv) {
         int val = sdcbuf->sdc_parm[0] - 32768;
         if(val >= 0 && val <= 7) pPriv->contrast = val;
	 else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVCONTRAST:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->contrast + 32768;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVHUE:
      if((pPriv) && (pSiS->SiS_SD2_Flags & SiS_SD2_SUPPORTXVHUESAT)) {
         int val = sdcbuf->sdc_parm[0] - 32768;
         if(val >= -8 && val <= 7) pPriv->hue = val;
	 else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVHUE:
      if((pPriv) && (pSiS->SiS_SD2_Flags & SiS_SD2_SUPPORTXVHUESAT)) {
         sdcbuf->sdc_result[0] = pPriv->hue + 32768;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVSATURATION:
      if((pPriv) && (pSiS->SiS_SD2_Flags & SiS_SD2_SUPPORTXVHUESAT)) {
         int val = sdcbuf->sdc_parm[0] - 32768;
         if(val >= -7 && val <= 7) pPriv->saturation = val;
	 else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVSATURATION:
      if((pPriv) && (pSiS->SiS_SD2_Flags & SiS_SD2_SUPPORTXVHUESAT)) {
         sdcbuf->sdc_result[0] = pPriv->saturation + 32768;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVGAMMA:
      if((pPriv) && (pSiS->SiS_SD_Flags & SiS_SD_SUPPORTXVGAMMA1)) {
         if(sdcbuf->sdc_parm[0] < 100 || sdcbuf->sdc_parm[0] > 10000 ||
	    sdcbuf->sdc_parm[1] < 100 || sdcbuf->sdc_parm[1] > 10000 ||
	    sdcbuf->sdc_parm[2] < 100 || sdcbuf->sdc_parm[2] > 10000) {
	    sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
	 } else {
	    pSiS->XvGammaRed = sdcbuf->sdc_parm[0];
	    pSiS->XvGammaGreen = sdcbuf->sdc_parm[1];
	    pSiS->XvGammaBlue = sdcbuf->sdc_parm[2];
	    SiSUpdateXvGamma(pSiS, pPriv);
	 }
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVGAMMA:
      if((pPriv) && (pSiS->SiS_SD_Flags & SiS_SD_SUPPORTXVGAMMA1)) {
         sdcbuf->sdc_result[0] = pSiS->XvGammaRed;
	 sdcbuf->sdc_result[1] = pSiS->XvGammaGreen;
	 sdcbuf->sdc_result[2] = pSiS->XvGammaBlue;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVCOLORKEY:
      if(pPriv) {
         pPriv->colorKey = pSiS->colorKey = sdcbuf->sdc_parm[0];
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVCOLORKEY:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->colorKey;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVAUTOPAINTCOLORKEY:
      if(pPriv) {
         pPriv->autopaintColorKey = sdcbuf->sdc_parm[0] ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVAUTOPAINTCOLORKEY:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->autopaintColorKey ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVDEFAULTS:
      if(pPriv) {
         SISSetPortDefaults(pScrn, pPriv);
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVDISABLEGFX:
      if(pPriv) {
         pPriv->disablegfx = sdcbuf->sdc_parm[0] ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVDISABLEGFX:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->disablegfx ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVDISABLEGFXLR:
      if(pPriv) {
         pPriv->disablegfxlr = sdcbuf->sdc_parm[0] ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVDISABLEGFXLR:
      if(pPriv) {
         sdcbuf->sdc_result[0] = pPriv->disablegfxlr ? 1 : 0;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_SETXVSWITCHCRT:
      if((pPriv) && (pSiS->VGAEngine == SIS_315_VGA)) {
         if(pPriv->AllowSwitchCRT) {
	    pPriv->crtnum = sdcbuf->sdc_parm[0] ? 1 : 0;
#ifdef SISDUALHEAD
            if(pPriv->dualHeadMode) pSiSEnt->curxvcrtnum = pPriv->crtnum;
#endif
	 }
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETXVSWITCHCRT:
      if((pPriv) && (pSiS->VGAEngine == SIS_315_VGA)) {
#ifdef SISDUALHEAD
         if(pPriv->dualHeadMode)
            sdcbuf->sdc_result[0] = pSiSEnt->curxvcrtnum;
         else
#endif
            sdcbuf->sdc_result[0] = pPriv->crtnum;
      } else sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;


   case SDC_CMD_SETTVXPOS:
      {
	 int value = sdcbuf->sdc_parm[0] - 32768;
	 if((value < -32) || (value > 32)) {
	    sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
	 } else if(pSiS->xv_sisdirectunlocked) {
	    pPriv->tvxpos = value;
	    SiS_SetTVxposoffset(pScrn, pPriv->tvxpos);
	    pPriv->updatetvxpos = FALSE;
         } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      }
      break;

   case SDC_CMD_GETTVXPOS:
      sdcbuf->sdc_result[0] = SiS_GetTVxposoffset(pScrn) + 32768;
      break;

   case SDC_CMD_SETTVYPOS:
      {
	 int value = sdcbuf->sdc_parm[0] - 32768;
	 if((value < -32) || (value > 32)) {
	    sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
	 } else if(pSiS->xv_sisdirectunlocked) {
	    pPriv->tvypos = value;
	    SiS_SetTVyposoffset(pScrn, pPriv->tvypos);
	    pPriv->updatetvypos = FALSE;
	 } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      }
      break;

   case SDC_CMD_GETTVYPOS:
      sdcbuf->sdc_result[0] = SiS_GetTVyposoffset(pScrn) + 32768;
      break;

   case SDC_CMD_SETXVDEINT:
   case SDC_CMD_GETXVDEINT:
      sdcbuf->sdc_result_header = SDC_RESULT_INVAL;
      break;

   case SDC_CMD_GETMONGAMMACRT1:
      sdcbuf->sdc_result[0] = pSiS->CRT1MonGamma;
      break;

   case SDC_CMD_GETMONGAMMACRT2:
      sdcbuf->sdc_result[0] = pSiS->CRT2MonGamma;
      break;

   case SDC_CMD_LOGQUIET:
      pSiS->SCLogQuiet = sdcbuf->sdc_parm[0] ? TRUE : FALSE;
      break;

   case SDC_CMD_GETCRT1SATGAIN:
      sdcbuf->sdc_result[0] = SiS_GetSISCRT1SaturationGain(pScrn);
      break;

   case SDC_CMD_SETCRT1SATGAIN:
      if(pSiS->xv_sisdirectunlocked) {
	 SiS_SetSISCRT1SaturationGain(pScrn, (int)sdcbuf->sdc_parm[0]);
      } else sdcbuf->sdc_result_header = SDC_RESULT_NOPERM;
      break;

   default:
      sdcbuf->sdc_result_header = SDC_RESULT_UNDEFCMD;
   }

   return Success;
}

/* Proc */

static int
SiSProcSiSCtrlQueryVersion(ClientPtr client)
{
    xSiSCtrlQueryVersionReply	  rep;
    register int		  n;

    REQUEST_SIZE_MATCH(xSiSCtrlQueryVersionReq);
    rep.type = X_Reply;
    rep.length = 0;
    rep.sequenceNumber = client->sequence;
    rep.majorVersion = SISCTRL_MAJOR_VERSION;
    rep.minorVersion = SISCTRL_MINOR_VERSION;
    if(client->swapped) {
        swaps(&rep.sequenceNumber, n);
        swapl(&rep.length, n);
        swaps(&rep.majorVersion, n);
        swaps(&rep.minorVersion, n);
    }
    WriteToClient(client, sizeof(xSiSCtrlQueryVersionReply), (char *)&rep);
    return (client->noClientException);
}

static int
SiSProcSiSCtrlCommand(ClientPtr client)
{
    REQUEST(xSiSCtrlCommandReq);
    xSiSCtrlCommandReply rep;
    ExtensionEntry 	 *myext;
    xSiSCtrlScreenTable  *myctrl;
    register int	 n;
    int 		 i, ret;

    REQUEST_SIZE_MATCH(xSiSCtrlCommandReq);

    memcpy(&rep, stuff, sizeof(xSiSCtrlCommandReply));

    /* Get pointer to ExtensionEntry */
    if(!(myext = CheckExtension(SISCTRL_PROTOCOL_NAME))) return BadMatch;

    /* Get pointer to our private */
    if(!(myctrl = (xSiSCtrlScreenTable *)myext->extPrivate)) return BadMatch;

    /* Check if screen index is within out limits */
    if(rep.screen > myctrl->maxscreens) return BadMatch;

    /* Check if this screen has added itself */
    if(!(myctrl->HandleSiSDirectCommand[rep.screen])) return BadMatch;

    /* Finally, execute the command */
    if((ret = (myctrl->HandleSiSDirectCommand[rep.screen])(&rep)) != Success)
       return ret;

    rep.type = X_Reply;
    rep.length = (sizeof(xSiSCtrlCommandReply) - sizeof(xGenericReply)) >> 2;
    rep.sequenceNumber = client->sequence;

    if(client->swapped) {
       swaps(&rep.sequenceNumber, n);
       swapl(&rep.length, n);
       swapl(&rep.screen, n);
       swapl(&rep.sdc_id, n);
       swapl(&rep.sdc_command, n);
       swapl(&rep.sdc_result_header, n);
       for(i = 0; i < SDC_NUM_PARM_RESULT; i++) {
	  swapl(&rep.sdc_parm[i], n);
	  swapl(&rep.sdc_result[i], n);
       }
    }
    WriteToClient(client, sizeof(xSiSCtrlCommandReply), (char *)&rep);
    return client->noClientException;
}

static int
SiSProcSiSCtrlDispatch(ClientPtr client)
{
    REQUEST(xReq);
    switch(stuff->data) {
	case X_SiSCtrlQueryVersion:
	     return SiSProcSiSCtrlQueryVersion(client);
	case X_SiSCtrlCommand:
	     return SiSProcSiSCtrlCommand(client);
    }
    return BadRequest;
}

/* SProc */

static int
SiSSProcSiSCtrlQueryVersion(ClientPtr client)
{
    REQUEST(xSiSCtrlQueryVersionReq);
    register int n;
    swaps(&stuff->length, n);
    REQUEST_SIZE_MATCH(xSiSCtrlQueryVersionReq);
    return SiSProcSiSCtrlQueryVersion(client);
}

static int
SiSSProcSiSCtrlCommand(ClientPtr client)
{
    REQUEST(xSiSCtrlCommandReq);
    register int n;
    int i;
    swaps(&stuff->length, n);
    swapl(&stuff->screen, n);
    swapl(&stuff->sdc_id, n);
    swapl(&stuff->sdc_command, n);
    swapl(&stuff->sdc_result_header, n);
    for(i = 0; i < SDC_NUM_PARM_RESULT; i++) {
       swapl(&stuff->sdc_parm[i], n);
       swapl(&stuff->sdc_result[i], n);
    }
    REQUEST_SIZE_MATCH(xSiSCtrlCommandReq);
    return SiSProcSiSCtrlCommand(client);
}

static int
SiSSProcSiSCtrlDispatch(ClientPtr client)
{
    REQUEST(xReq);
    switch(stuff->data) {
       case X_SiSCtrlQueryVersion:
	  return SiSSProcSiSCtrlQueryVersion(client);
       case X_SiSCtrlCommand:
	  return SiSSProcSiSCtrlCommand(client);
    }
    return BadRequest;
}

static void
SiSCtrlResetProc(ExtensionEntry* extEntry)
{
    /* Called by CloseDownExtensions() */
    /* (CloseDownExtensions() is called BEFORE CloseScreen,
     * which is why we must not use pSiS->SiSCtrlExtEntry
     * in SiSCtrlExtUnregister())
     */
    if(extEntry->extPrivate) {
       xfree(extEntry->extPrivate);
       extEntry->extPrivate = NULL;
    }
}

void
SiSCtrlExtInit(ScrnInfoPtr pScrn)
{
   SISPtr pSiS = SISPTR(pScrn);
   ExtensionEntry  *myext;
   xSiSCtrlScreenTable *myctrl;
   unsigned int version, revision;

   if((pSiS->VGAEngine != SIS_300_VGA) &&
      (pSiS->VGAEngine != SIS_315_VGA))
      return;

   pSiS->SCLogQuiet = FALSE;

   if(!(myext = CheckExtension(SISCTRL_PROTOCOL_NAME))) {

      if(!(myctrl = xcalloc(sizeof(xSiSCtrlScreenTable), 1)))
         return;

      if(!(myext = AddExtension(SISCTRL_PROTOCOL_NAME, 0, 0,
				SiSProcSiSCtrlDispatch,
				SiSSProcSiSCtrlDispatch,
				SiSCtrlResetProc,
				StandardMinorOpcode))) {
         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	 		"Failed to add SISCTRL extension\n");
	 xfree(myctrl);
	 return;
      }

      myext->extPrivate = (pointer)myctrl;

      myctrl->maxscreens = SISCTRL_MAX_SCREENS;
      myctrl->version_major = version = SISCTRL_MAJOR_VERSION;
      myctrl->version_minor = revision = SISCTRL_MINOR_VERSION;

      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		"Initialized SISCTRL extension version %d.%d\n",
		version, revision);

   } else {

      if(!(myctrl = (xSiSCtrlScreenTable *)myext->extPrivate)) {
         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		"Internal error: Found SISCTRL extension with NULL-private!\n");
	 return;
      }

      version = myctrl->version_major;
      revision = myctrl->version_minor;
   }

   if(pScrn->scrnIndex < myctrl->maxscreens) {

      myctrl->HandleSiSDirectCommand[pScrn->scrnIndex] = SiSHandleSiSDirectCommand;

      pSiS->SiSCtrlExtEntry = myext;

      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		"Registered screen %d with SISCTRL extension version %d.%d\n",
		pScrn->scrnIndex, version, revision);
   } else {

      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		"Screen number (%d) too high for SISCTRL extension %d.%d\n",
		pScrn->scrnIndex, version, revision);

   }

}

void
SiSCtrlExtUnregister(SISPtr pSiS, int index)
{
   ExtensionEntry  *myext;
   xSiSCtrlScreenTable *myctrl;

   if(!pSiS->SiSCtrlExtEntry) return;

   /* Since CloseDownExtensions() is called before
    * our CloseScreen(), we must not use the saved
    * ptr here, but instead check for the extension.
    */

   if((myext = CheckExtension(SISCTRL_PROTOCOL_NAME))) {
      if((myctrl = (xSiSCtrlScreenTable *)myext->extPrivate)) {
         myctrl->HandleSiSDirectCommand[index] = NULL;
      }
   }
}

/***********************************
 *     Xv attribute interface      *
 ***********************************/

#ifdef XV_SD_DEPRECATED

int
SISSetPortUtilAttribute(ScrnInfoPtr pScrn, Atom attribute,
			INT32 value, SISPortPrivPtr pPriv)
{
  SISPtr pSiS = SISPTR(pScrn);
#ifdef SISDUALHEAD
  SISEntPtr pSiSEnt = pSiS->entityPrivate;;
#endif

  if(attribute == pSiS->xv_USD) {
     if(pSiS->enablesisctrl) {
	 if(value == SIS_DIRECTKEY) {
	   pSiS->xv_sisdirectunlocked++;
	} else if(pSiS->xv_sisdirectunlocked) {
	   pSiS->xv_sisdirectunlocked--;
	}
     } else {
	pSiS->xv_sisdirectunlocked = 0;
     }
  } else if(attribute == pSiS->xv_SVF) {
#ifdef SISDUALHEAD
     if(!pPriv->dualHeadMode)
#endif
	if(pSiS->xv_sisdirectunlocked) {
	   SISSwitchCRT2Type(pScrn, (ULong)value, FALSE);
	   SISUpdateVideoParms(pSiS, pPriv);
	}
  } else if(attribute == pSiS->xv_CT1) {
#ifdef SISDUALHEAD
     if(!pPriv->dualHeadMode)
#endif
	if(pSiS->xv_sisdirectunlocked) {
	   SISSwitchCRT1Status(pScrn, (ULong)value, FALSE);
	   SISUpdateVideoParms(pSiS, pPriv);
        }
  } else if(attribute == pSiS->xv_RDT) {
#ifdef SISDUALHEAD
     if(!pPriv->dualHeadMode)
#endif
	if(pSiS->xv_sisdirectunlocked) {
	   SISRedetectCRT2Devices(pScrn);
	}
  } else if(attribute == pSiS->xv_TAF) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVantiflicker(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TSA) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVsaturation(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TEE) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVedgeenhance(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_CFI) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVcfilter(pScrn, value ? 1 : 0);
     }
  } else if(attribute == pSiS->xv_YFI) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVyfilter(pScrn, value);
     }
  } else if(attribute == pSiS->xv_COC) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVcolcalib(pScrn, (int)value, TRUE);
     }
  } else if(attribute == pSiS->xv_COF) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetSISTVcolcalib(pScrn, (int)value, FALSE);
     }
  } else if(attribute == pSiS->xv_TCO) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetCHTVcontrast(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TTE) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetCHTVtextenhance(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TCF) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetCHTVchromaflickerfilter(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TLF) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetCHTVlumaflickerfilter(pScrn, (int)value);
     }
  } else if(attribute == pSiS->xv_TCC) {
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetCHTVcvbscolor(pScrn, value ? 1 : 0);
     }
  } else if(attribute == pSiS->xv_OVR) {
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->UseCHOverScan = -1;
	pSiS->OptTVSOver = FALSE;
	if(value == 3) {
	   if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSOVER) {
	      pSiS->OptTVSOver = TRUE;
	   }
	   pSiS->UseCHOverScan = 1;
	} else if(value == 2) pSiS->UseCHOverScan = 1;
	else if(value == 1)   pSiS->UseCHOverScan = 0;
     }
  } else if(attribute == pSiS->xv_CMD) {
     if(pSiS->xv_sisdirectunlocked) {
	int result = 0;
	pSiS->xv_sd_result = (value & 0xffffff00);
	result = SISCheckModeIndexForCRT2Type(pScrn, (UShort)(value & 0xff),
					      (UShort)((value >> 8) & 0xff),
					      FALSE);
	pSiS->xv_sd_result |= (result & 0xff);
     }
  } else if(attribute == pSiS->xv_SGA) {
     if(pSiS->xv_sisdirectunlocked) {
	Bool backup = pSiS->XvGamma;
	Bool backup2 = pSiS->CRT2SepGamma;
	pSiS->CRT1gamma = (value & 0x01) ? TRUE : FALSE;
	pSiS->CRT2gamma = (value & 0x02) ? TRUE : FALSE;
#ifdef SISDUALHEAD
	if(pPriv->dualHeadMode) {
	   pSiSEnt->CRT1gamma = pSiS->CRT1gamma;
	   pSiSEnt->CRT2gamma = pSiS->CRT2gamma;
	}
#endif
	if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSGRCRT2) {
	   pSiS->CRT2SepGamma = (value & 0x08) ? TRUE : FALSE;
	   if(pSiS->CRT2SepGamma != backup2) {
	      if(pSiS->CRT2SepGamma) {
		 SiS_UpdateGammaCRT2(pScrn);
	      }
#ifdef SISGAMMARAMP
	      else {
		 SISCalculateGammaRamp(pScrn->pScreen, pScrn);
	      }
#endif
	      /* ATTN: When disabling CRT2 sep gamma,
	       * as long as SISGAMMARAMP is not defined,
	       * application needs to reset palette using
	       * vidmodextension! (See SiS_SD_CANSETGAMMA)
	       *
	       * Note: Difference between setting gamma values
	       * for CRT1 and CRT2: For CRT1, driver only
	       * stores the values, but does nothing with them.
	       * For CRT2, the driver will IMMEDIATELY recalc
	       * the palette. App needs to calc gamma ramp
	       * itself for CRT1, and set it using the
	       * VidModExtention.
	       */
	   }
	} else pSiS->CRT2SepGamma = FALSE;
	pSiS->XvGamma = (value & 0x04) ? TRUE : FALSE;
	if(pSiS->VGAEngine == SIS_315_VGA) {
	   if(backup != pSiS->XvGamma) {
	      SiSUpdateXvGamma(pSiS, pPriv);
	   }
	}
     }
  } else if(attribute == pSiS->xv_TXS) {
     if((value < -16) || (value > 16)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetTVxscale(pScrn, value);
     }
  } else if(attribute == pSiS->xv_TYS) {
     if((value < -4) || (value > 3)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	SiS_SetTVyscale(pScrn, value);
     }
  } else if(attribute == pSiS->xv_BRR) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriR = value;
     }
  } else if(attribute == pSiS->xv_BRG) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriG = value;
     }
  } else if(attribute == pSiS->xv_BRB) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriB = value;
     }
  } else if(attribute == pSiS->xv_PBR) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBG) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBB) {
     /* Nop */
  } else if(attribute == pSiS->xv_BRR2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
#ifdef SISDUALHEAD
	if(pPriv->dualHeadMode) pSiSEnt->GammaBriR = value;
#endif
     }
  } else if(attribute == pSiS->xv_BRG2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
#ifdef SISDUALHEAD
	if(pPriv->dualHeadMode) pSiSEnt->GammaBriG = value;
#endif
     }
  } else if(attribute == pSiS->xv_BRB2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
#ifdef SISDUALHEAD
	if(pPriv->dualHeadMode) pSiSEnt->GammaBriB = value;
#endif
     }
  } else if(attribute == pSiS->xv_PBR2) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBG2) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBB2) {
     /* Nop */
  } else if(attribute == pSiS->xv_GARC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaR2 = (float)value / (float)1000;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_GAGC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaG2 = (float)value / (float)1000;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_GABC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaB2 = (float)value / (float)1000;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_BRRC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriR2 = value;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_BRGC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriG2 = value;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_BRBC2) {
     if((value < 100) || (value > 10000)) return BadValue;
     if(pSiS->xv_sisdirectunlocked) {
	pSiS->GammaBriB2 = value;
	SiS_UpdateGammaCRT2(pScrn);
     }
  } else if(attribute == pSiS->xv_PBRC2) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBGC2) {
     /* Nop */
  } else if(attribute == pSiS->xv_PBBC2) {
     /* Nop */
  } else if(attribute == pSiS->xv_SHC) {
     if(pSiS->xv_sisdirectunlocked) {
	Bool VisibleBackup = pSiS->HWCursorIsVisible;
	pSiS->HideHWCursor = value ? TRUE : FALSE;
	if(pSiS->CursorInfoPtr) {
	   if(VisibleBackup) {
	      if(value) {
		 (pSiS->CursorInfoPtr->HideCursor)(pScrn);
	      } else {
		 (pSiS->CursorInfoPtr->ShowCursor)(pScrn);
	      }
	   }
	   pSiS->HWCursorIsVisible = VisibleBackup;
	}
     }
  } else if(attribute == pSiS->xv_PMD) {
     if(pSiS->xv_sisdirectunlocked) {
        if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSCALE) {
	   if(value & 0x01)      pSiS->SiS_Pr->UsePanelScaler = -1;
	   else if(value & 0x02) pSiS->SiS_Pr->UsePanelScaler = 1;
	   else			 pSiS->SiS_Pr->UsePanelScaler = 0;
	   if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTCENTER) {
	      if(value & 0x04)      pSiS->SiS_Pr->CenterScreen = -1;
	      else if(value & 0x08) pSiS->SiS_Pr->CenterScreen = 1;
	      else		    pSiS->SiS_Pr->CenterScreen = 0;
	   }
        }
     }
#ifdef TWDEBUG
  } else if(attribute == pSiS->xv_STR) {
     ULong port;
     CARD8 reg;
     switch((value & 0xff000000) >> 24) {
     case 0x00: port = SISSR;    break;
     case 0x01: port = SISPART1; break;
     case 0x02: port = SISPART2; break;
     case 0x03: port = SISPART3; break;
     case 0x04: port = SISPART4; break;
     case 0x05: port = SISCR;    break;
     case 0x06: port = SISVID;   break;
     default:   return BadValue;
     }
     outSISIDXREG(port,((value & 0x00ff0000) >> 16), ((value & 0x0000ff00) >> 8));
     inSISIDXREG(port, ((value & 0x00ff0000) >> 16), reg);
     xf86DrvMsg(0, 0, "SetREG %x -> %x -> %x\n", ((value & 0x00ff0000) >> 16), ((value & 0x0000ff00) >> 8), reg);
#endif
  } else {
     return BadMatch;
  }

  return Success;
}

int
SISGetPortUtilAttribute(ScrnInfoPtr pScrn,  Atom attribute,
			INT32 *value, SISPortPrivPtr pPriv)
{
  SISPtr pSiS = SISPTR(pScrn);
#ifdef SISDUALHEAD
  SISEntPtr pSiSEnt = pSiS->entityPrivate;;
#endif

  if(attribute == pSiS->xv_QVF) {
     *value = pSiS->VBFlags;
  } else if(attribute == pSiS->xv_GDV) {
     *value = SISDRIVERIVERSION;
  } else if(attribute == pSiS->xv_GHI) {
     *value = (pSiS->ChipFlags & 0xffff) | (pSiS->ChipType << 16) | (pSiS->ChipRev << 24);
  } else if(attribute == pSiS->xv_GBI) {
     *value = (pSiS->PciBus << 16) | (pSiS->PciDevice << 8) | pSiS->PciFunc;
  } else if(attribute == pSiS->xv_QVV) {
     *value = SIS_VBFlagsVersion;
  } else if(attribute == pSiS->xv_QDD) {
     *value = pSiS->detectedCRT2Devices;
  } else if(attribute == pSiS->xv_CT1) {
     *value = pSiS->CRT1isoff ? 0 : 1;
  } else if(attribute == pSiS->xv_GSF) {
     *value = pSiS->SiS_SD_Flags;
  } else if(attribute == pSiS->xv_GSF2) {
     *value = pSiS->SiS_SD2_Flags;
  } else if(attribute == pSiS->xv_USD) {
     *value = pSiS->xv_sisdirectunlocked;
  } else if(attribute == pSiS->xv_TAF) {
     *value = SiS_GetSISTVantiflicker(pScrn);
  } else if(attribute == pSiS->xv_TSA) {
     *value = SiS_GetSISTVsaturation(pScrn);
  } else if(attribute == pSiS->xv_TEE) {
     *value = SiS_GetSISTVedgeenhance(pScrn);
  } else if(attribute == pSiS->xv_CFI) {
     *value = SiS_GetSISTVcfilter(pScrn);
  } else if(attribute == pSiS->xv_YFI) {
     *value = SiS_GetSISTVyfilter(pScrn);
  } else if(attribute == pSiS->xv_COC) {
     *value = SiS_GetSISTVcolcalib(pScrn, TRUE);
  } else if(attribute == pSiS->xv_COF) {
     *value = SiS_GetSISTVcolcalib(pScrn, FALSE);
  } else if(attribute == pSiS->xv_TCO) {
     *value = SiS_GetCHTVcontrast(pScrn);
  } else if(attribute == pSiS->xv_TTE) {
     *value = SiS_GetCHTVtextenhance(pScrn);
  } else if(attribute == pSiS->xv_TCF) {
     *value = SiS_GetCHTVchromaflickerfilter(pScrn);
  } else if(attribute == pSiS->xv_TLF) {
     *value = SiS_GetCHTVlumaflickerfilter(pScrn);
  } else if(attribute == pSiS->xv_TCC) {
     *value = SiS_GetCHTVcvbscolor(pScrn);
  } else if(attribute == pSiS->xv_CMDR) {
     *value = pSiS->xv_sd_result;
  } else if(attribute == pSiS->xv_OVR) {
     /* Changing of CRT2 settings not supported in DHM! */
     *value = 0;
     if(pSiS->OptTVSOver == 1)         *value = 3;
     else if(pSiS->UseCHOverScan == 1) *value = 2;
     else if(pSiS->UseCHOverScan == 0) *value = 1;
  } else if(attribute == pSiS->xv_SGA) {
     *value = 0;
#ifdef SISDUALHEAD
     if(pPriv->dualHeadMode) {
        if(pSiSEnt->CRT1gamma) *value |= 0x01;
	if(pSiSEnt->CRT2gamma) *value |= 0x02;
     } else {
#endif
	if(pSiS->CRT1gamma)    *value |= 0x01;
	if(pSiS->CRT2gamma)    *value |= 0x02;
	if(pSiS->CRT2SepGamma) *value |= 0x08;
#ifdef SISDUALHEAD
     }
#endif
     if(pSiS->XvGamma) *value |= 0x04;
  } else if(attribute == pSiS->xv_TXS) {
     *value = SiS_GetTVxscale(pScrn);
  } else if(attribute == pSiS->xv_TYS) {
     *value = SiS_GetTVyscale(pScrn);
  } else if(attribute == pSiS->xv_GSS) {
     *value = (pScrn->virtualX << 16) | pScrn->virtualY;
  } else if(attribute == pSiS->xv_BRR) {
     *value = pSiS->GammaBriR;
  } else if(attribute == pSiS->xv_BRG) {
     *value = pSiS->GammaBriG;
  } else if(attribute == pSiS->xv_BRB) {
     *value = pSiS->GammaBriB;
  } else if(attribute == pSiS->xv_PBR) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBG) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBB) {
     *value = 1000;
  } else if(attribute == pSiS->xv_BRR2) {
#ifdef SISDUALHEAD
     if(pPriv->dualHeadMode) *value = pSiSEnt->GammaBriR;
     else
#endif
          *value = pSiS->GammaBriR;
  } else if(attribute == pSiS->xv_BRG2) {
#ifdef SISDUALHEAD
     if(pPriv->dualHeadMode) *value = pSiSEnt->GammaBriG;
     else
#endif
          *value = pSiS->GammaBriG;
  } else if(attribute == pSiS->xv_BRB2) {
#ifdef SISDUALHEAD
     if(pPriv->dualHeadMode) *value = pSiSEnt->GammaBriB;
     else
#endif
          *value = pSiS->GammaBriB;
  } else if(attribute == pSiS->xv_PBR2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBG2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBB2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_GARC2) {
     *value = (int)(pSiS->GammaR2 * 1000);
  } else if(attribute == pSiS->xv_GAGC2) {
     *value = (int)(pSiS->GammaG2 * 1000);
  } else if(attribute == pSiS->xv_GABC2) {
     *value = (int)(pSiS->GammaB2 * 1000);
  } else if(attribute == pSiS->xv_BRRC2) {
     *value = pSiS->GammaBriR2;
  } else if(attribute == pSiS->xv_BRGC2) {
     *value = pSiS->GammaBriG2;
  } else if(attribute == pSiS->xv_BRBC2) {
     *value = pSiS->GammaBriB2;
  } else if(attribute == pSiS->xv_PBRC2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBGC2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_PBBC2) {
     *value = 1000;
  } else if(attribute == pSiS->xv_SHC) {
     *value = pSiS->HideHWCursor ? 1 : 0;
  } else if(attribute == pSiS->xv_PMD) {
     *value = 0;
     if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTSCALE) {
        switch(pSiS->SiS_Pr->UsePanelScaler) {
           case -1: *value |= 0x01; break;
           case 1:  *value |= 0x02; break;
        }
	if(pSiS->SiS_SD_Flags & SiS_SD_SUPPORTCENTER) {
           switch(pSiS->SiS_Pr->CenterScreen) {
              case -1: *value |= 0x04; break;
              case 1:  *value |= 0x08; break;
           }
	}
     }
  } else {
     return BadMatch;
  }

  return Success;
}

#endif /* XV_SD_DEPRECATED */