summaryrefslogtreecommitdiff
path: root/src/via_output.c
blob: 825f401c4c62432e05f2db2d509cbfc74f7c222a (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
/*
 * Copyright 2004-2009 Luc Verhaegen.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
 * via_output.c
 *
 * Output Handling.
 *
 */

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

#include "via_driver.h"

#ifndef _XF86_ANSIC_H
#include <string.h>
#endif

#include "via_vgahw.h"
#include "via_id.h"
#include "via_crtc.h"
#include "via_output.h"
#include "via_mode.h"

/*
 *
 */
static struct ViaOutput *
ViaOutputDestroy(struct ViaOutput *Output)
{
    struct ViaOutput *Next;

    Next = Output->Next;

    if (Output->PrivateDestroy)
        Output->PrivateDestroy(Output);

    ViaModesDestroy(Output->Modes);

    if (Output->I2CDev)
        xf86DestroyI2CDevRec(Output->I2CDev, TRUE);

    if (Output->Options)
        xfree(Output->Options);

    if (Output->MonitorName)
        xfree(Output->MonitorName);

    xfree(Output);

    return Next;
}

/*
 *
 */
void
ViaOutputsDestroy(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output)
        Output = ViaOutputDestroy(Output);
}

/*
 *
 */
static Bool
ViaOutputAdd(ScrnInfoPtr pScrn, struct ViaOutput *Output)
{
    struct ViaOutput *Last = NULL;

    /* Is this Output sound? */
    if (!Output->Name || !Output->ModeValid || !Output->Mode ||
        !Output->Power) {

        if (!Output->Name)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                       "%s: Nameless output device.\n", __func__);

        if (!Output->ModeValid)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: ModeValid "
                       "callback missing\n", __func__, Output->Name);

        if (!Output->Mode)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: Mode "
                       "callback missing\n", __func__, Output->Name);

        if (!Output->Power)
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: %s: Power "
                       "callback missing\n", __func__, Output->Name);

        return FALSE;
    }

    /* add Output to list */
    Last = VIAPTR(pScrn)->Outputs;
    if (Last) {
        while (Last->Next)
            Last = Last->Next;

        Last->Next = Output;
        Output->Prev = Last;
    } else {
        VIAPTR(pScrn)->Outputs = Output;
        Output->Prev = NULL;
    }

    return TRUE;
}

#ifdef UNUSED
/*
 *
 */
static void
ViaOutputRemove(ScrnInfoPtr pScrn, struct ViaOutput *Output)
{
    if (!Output)
        return;

    /* remove from list */
    if (Output->Prev)
        Output->Prev->Next = Output->Next;
    else
        VIAPTR(pScrn)->Outputs = Output->Next;

    if (Output->Next)
        Output->Next->Prev = Output->Prev;

    ViaOutputDestroy(Output);
}
#endif

/*
 *
 * Whine about TODOs.
 *
 */

/*
 * VIAs VT1631 LVDS encoder. (aka VT3191)
 */
static struct ViaOutput *
ViaVT1631Init(ScrnInfoPtr pScrn, I2CDevPtr pDev)
{
    CARD8 buf = 0, Read[4];
    CARD32 ID;

    VIAFUNC(pScrn);

    if (!xf86I2CWriteRead(pDev, &buf, 1, Read, 4)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "%s: Unable to read from %s Slave %d.\n",
                   __func__,  pDev->pI2CBus->BusName, pDev->SlaveAddr);
        return NULL;
    }

    ID = (Read[1] << 24) | (Read[0] << 16) | (Read[3] << 8) | Read[2];

    switch (ID) {
    case 0x11063191: /* VIA VT1631 */
        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
                   "Detected Via Technologies VT1631 LVDS Transmitter.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "VT1631 is not supported yet."
                   " Please contact unichrome-devel@lists.sf.net\n");
        return NULL;
    case 0x11063192: /* VIA VT1632 - Not handled here => Sil164 compatible. */
        return NULL;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "%s: Unknown VIA device "
                   "detected on %s:0x%02X: 0x%08lX.\n", __func__,
                   pDev->pI2CBus->BusName, pDev->SlaveAddr, ID);
        return NULL;
    }

    return NULL;
}

/*
 * Philips TV encoders.
 *
 * SAA7108
 * SAA7109
 */
static struct ViaOutput *
ViaSAA710xInit(ScrnInfoPtr pScrn, I2CDevPtr pDev)
{
    CARD8 buf;

    VIAFUNC(pScrn);

    if (!xf86I2CReadByte(pDev, 0x1C, &buf)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "%s: Unable to read from %s Slave %d.\n",
                   __func__,  pDev->pI2CBus->BusName, pDev->SlaveAddr);
        return NULL;
    }

    switch (buf) {
    case 0x02:
        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
                   "Detected Philips Semiconductors SAA7108E TV Encoder.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "SAA7108E is not supported yet."
                   " Please contact unichrome-devel@lists.sf.net\n");
        return NULL;
    case 0x03:
        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
                   "Detected Philips Semiconductors SAA7109E TV Encoder.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "SAA7109E is not supported yet."
                   " Please contact unichrome-devel@lists.sf.net\n");
        return NULL;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                   "%s: Unknown device detected: 0x%02X.\n", __func__, buf);
        return NULL;
    };

    return NULL;
}

/*
 * Focus enhancements tv encoders.
 */
static struct ViaOutput *
ViaFS45xInit(ScrnInfoPtr pScrn, I2CDevPtr pDev)
{
    CARD8 buf;

    VIAFUNC(pScrn);

    if (!xf86I2CReadByte(pDev, 0x7F, &buf)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "%s: Unable to read from %s Slave %d.\n",
                   __func__,  pDev->pI2CBus->BusName, pDev->SlaveAddr);
        return NULL;
    }

    switch (buf) {
    case 0x20:
        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
                   "Detected Focus Enhancements 454 TV Encoder.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "FS454 is not supported yet."
                   " Please contact unichrome-devel@lists.sf.net\n");
        return NULL;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                   "%s: Unknown device detected: 0x%02X.\n", __func__, buf);
        return NULL;
    };

    return NULL;
}

/*
 * Aitech AIT2139
 */
static struct ViaOutput *
ViaAIT213xInit(ScrnInfoPtr pScrn, I2CDevPtr pDev)
{
    CARD8 buf;

    VIAFUNC(pScrn);

    if (!xf86I2CReadByte(pDev, 0x3D, &buf)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                   "%s: Unable to read from %s Slave %d.\n",
                   __func__,  pDev->pI2CBus->BusName, pDev->SlaveAddr);
        return NULL;
    }

    switch (buf) {
    case 0x11:
        xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
                   "Detected AITech AIT2139 TV Encoder.\n");
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "AIT2139 is not supported yet."
                   " Please contact unichrome-devel@lists.sf.net\n");
        return NULL;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                   "%s: Unknown device detected: 0x%02X.\n", __func__, buf);
        return NULL;

    }
}

static struct {
    CARD8 Address;
    char * Name;
    struct ViaOutput * (*Init) (ScrnInfoPtr pScrn, I2CDevPtr pDev);
} ViaI2CDevices[] = {
    {0x10, "VT1631",  ViaVT1631Init},
    {0x10, "SiI16x",  ViaSiI16xInit},
    {0x40, "VT162x",  ViaVT162xInit},
    {0x42, "VT162x",  ViaVT162xInit},
    {0x70, "SiI16x",  ViaSiI16xInit},
    {0x72, "SiI16x",  ViaSiI16xInit},
    {0x88, "SAA710x", ViaSAA710xInit},
    {0x88, "AIT213x", ViaAIT213xInit},
    {0x8A, "AIT213x", ViaAIT213xInit},
    {0xD4, "FS45x",   ViaFS45xInit},
    {0xEA, "CH7xxx",  ViaCH7xxxInit},
    {0xEC, "CH7xxx",  ViaCH7xxxInit},
    {0x00, NULL, NULL}
};

/*
 *
 */
static void
ViaScanBus(ScrnInfoPtr pScrn, I2CBusPtr I2CBus)
{
    struct ViaOutput *Output;
    CARD8 SkipAddress = 0x00;
    int i;

    ViaDebug(pScrn->scrnIndex, "%s: Scanning %s.\n",
	     __func__, I2CBus->BusName);

    for (i = 0; ViaI2CDevices[i].Init; i++) {
        I2CDevPtr pDev = NULL;

        /* Make sure we don't try to Init a successfully inited device again */
        if (ViaI2CDevices[i].Address == SkipAddress)
            continue;

        if (!xf86I2CProbeAddress(I2CBus, ViaI2CDevices[i].Address))
            continue;

        pDev = xf86CreateI2CDevRec();

        pDev->DevName = ViaI2CDevices[i].Name;
        pDev->SlaveAddr = ViaI2CDevices[i].Address;
        pDev->pI2CBus = I2CBus;

        if (!xf86I2CDevInit(pDev)) {
            xf86DestroyI2CDevRec(pDev, TRUE);
            continue;
        }

        Output = ViaI2CDevices[i].Init(pScrn, pDev);

        /* did we actually find anything? */
        if (!Output) {
            xf86DestroyI2CDevRec(pDev,TRUE);
        } else {
            SkipAddress = ViaI2CDevices[i].Address;

            if (!ViaOutputAdd(pScrn, Output)) {
                while (Output) /* could be a whole list */
                    Output = ViaOutputDestroy(Output);
            }
        }
    }
}

/*
 *
 */
static char *
ViaOutputBusName(int Position)
{
    switch(Position) {
    case OUTPUT_BUS_CRT:
        return "CRT";
    case OUTPUT_BUS_DVP0:
        return "DVP0";
    case OUTPUT_BUS_DVP1:
        return "DVP1";
    case OUTPUT_BUS_DFP:
        return "DFP";
    case OUTPUT_BUS_DFPHIGH:
        return "DFPHigh";
    case OUTPUT_BUS_DFPLOW:
        return "DFPLow";
    default:
        return "Unknown";
    }
}

/*
 *
 */
void
ViaOutputsDetect(ScrnInfoPtr pScrn)
{
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput *Output;
    CARD8 SR12 = hwp->readSeq(hwp, 0x12);
    int i;

    VIAFUNC(pScrn);

    /* CRT */
    Output = ViaCRTInit(pScrn, NULL);
    if (Output && !ViaOutputAdd(pScrn, Output))
        while (Output) /* could be a whole list */
            Output = ViaOutputDestroy(Output);

    /* Panel */
    Output = ViaPanelInit(pScrn, NULL);
    if (Output && !ViaOutputAdd(pScrn, Output))
        while (Output) /* could be a whole list */
            Output = ViaOutputDestroy(Output);

    for (i = 0; i < I2C_BUS_COUNT; i++)
	if (pVia->I2CBus[i])
	    ViaScanBus(pScrn, pVia->I2CBus[i]);

    /* Match outputs to Bus */
    Output = pVia->Outputs;
    while (Output) {
        /* check bus position */
        switch (Output->Type) {
        case OUTPUT_CRT:
            Output->Position = OUTPUT_BUS_CRT; /* Duh! */

            if (!Output->EDID && pVia->I2CBus[0])
                Output->EDID = xf86DoEDID_DDC2(pScrn->scrnIndex, pVia->I2CBus[0]);

            if (Output->EDID)
                ViaOutputEDIDSet(Output, Output->EDID);

            break;
        case OUTPUT_TV:
            if (SR12 & 0x20)
                Output->Position = OUTPUT_BUS_DVP0;
            else
                Output->Position = OUTPUT_BUS_DVP1;
            break;
        case OUTPUT_TMDS:
	    if (pVia->Chipset == VT3122)
		Output->Position = OUTPUT_BUS_DFP;
	    else
		Output->Position = OUTPUT_BUS_DVP1;

            if (!Output->EDID && pVia->I2CBus[1])
                Output->EDID = xf86DoEDID_DDC2(pScrn->scrnIndex, pVia->I2CBus[1]);

            if (Output->EDID)
                ViaOutputEDIDSet(Output, Output->EDID);

            break;
        case OUTPUT_PANEL:
            /* split me! */
            Output->Position = OUTPUT_BUS_DFP;
            break;
        default:
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: Unhandled output device"
                       " type.\n", __func__);
            break;
        }

        Output = Output->Next;
    }
}

/*
 * Save all output devices.
 */
void
ViaOutputsSave(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->Save)
            Output->Save(Output);

        Output = Output->Next;
    }
}

/*
 * Restore all output devices.
 */
void
ViaOutputsRestore(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;

    while (Output) {
        if (Output->Restore)
            Output->Restore(Output);

        Output = Output->Next;
    }
}

/*
 * Check which output devices are active.
 */
static Bool
ViaOutputsSense(ScrnInfoPtr pScrn)
{
    struct ViaOutput *Output = VIAPTR(pScrn)->Outputs;
    Bool Active = FALSE;

    while (Output) {
        if (Output->Sense) {
            Output->Active = Output->Sense(Output);
            if (Output->Active)
                Active = TRUE;
        } else {
            Output->Active = TRUE;
            Active = TRUE;
        }

        Output = Output->Next;
    }

    return Active;
}

/*
 * Copy the modes of a table with final entry name == NULL into a list.
 * Attach list to a MonRec.
 *
 * Stolen from xf86Config.c's addDefaultModes
 */
void
ViaOutputAddModetable(struct ViaOutput *Output, DisplayModePtr Modes)
{
    DisplayModePtr mode;
    DisplayModePtr last;
    int i;

    for (last = Output->Modes; last && last->next; last = last->next)
        ;

    for (i = 0; Modes[i].name; i++) {

        mode = xnfalloc(sizeof(DisplayModeRec));
        memcpy(mode, &Modes[i], sizeof(DisplayModeRec));

        mode->name = xnfstrdup(Modes[i].name);

        if (last) {
            mode->prev = last;
            last->next = mode;
        } else { /* this is the first mode */
            Output->Modes = mode;
            mode->prev = NULL;
        }
        last = mode;
    }
}

/*
 * Does the common checks, and then passes on the Mode to the Outputs
 * own validation.
 */
int
ViaModeOutputValid(struct ViaOutput *Output, DisplayModePtr Mode)
{
    int i;

    for (i = 0; i < Output->numHSync; i++)
        if ((Mode->HSync >= (Output->HSync[i].lo * (1.0 - SYNC_TOLERANCE))) &&
            (Mode->HSync <= (Output->HSync[i].hi * (1.0 + SYNC_TOLERANCE))))
            break;
    if (Output->numHSync && (i == Output->numHSync))
        return MODE_HSYNC;

    for (i = 0; i < Output->numVRefresh; i++)
        if ((Mode->VRefresh >= (Output->VRefresh[i].lo * (1.0 - SYNC_TOLERANCE))) &&
            (Mode->VRefresh <= (Output->VRefresh[i].hi * (1.0 + SYNC_TOLERANCE))))
            break;
    if (Output->numVRefresh && (i == Output->numVRefresh))
        return MODE_VSYNC;

    if (Output->MaxClock && (Mode->SynthClock > Output->MaxClock))
        return MODE_CLOCK_HIGH;

    /* Is the horizontal blanking a bit lowish? */
    if (((Mode->CrtcHDisplay * 5 / 4) & ~0x07) > Mode->CrtcHTotal) {
        /* is this a cvt -r Mode, and only a cvt -r Mode? */
        if (((Mode->CrtcHTotal - Mode->CrtcHDisplay) == 160) &&
            ((Mode->CrtcHSyncEnd - Mode->CrtcHDisplay) == 80) &&
            ((Mode->CrtcHSyncEnd - Mode->CrtcHSyncStart) == 32) &&
            ((Mode->CrtcVSyncStart - Mode->CrtcVDisplay) == 3)) {
            if (!Output->ReducedAllowed)
                return MODE_NO_REDUCED;
        } else if ((Mode->CrtcHDisplay * 1.10) > Mode->CrtcHTotal)
            return MODE_HSYNC_NARROW;
    }

    if (Output->ModeValid)
        return Output->ModeValid(Output, Mode);
    else
        return MODE_OK;
}

#ifdef UNUSED
/*
 * Is the CRTC in control of the dotclock, or is an output device?
 */
static Bool
ViaOutputsFindDotclockMaster(ScrnInfoPtr pScrn)
{
    struct ViaOutput  *Output;

    for (Output = VIAPTR(pScrn)->Outputs; Output; Output = Output->Next)
        if (Output->Active && Output->ClockMaster)
            return TRUE;
    return FALSE;
}
#endif

/*
 * Prints out the complete layout.
 */
static void
ViaLayoutPrint(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaOutput *Output = pVia->Outputs;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Listing CRTC to Output Mapping:\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");

    /* Primary */
    if (pVia->Crtc[0]->Active) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  CRTC 1:\n");

        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_PRIMARY))
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "      Output %s on Bus %s.\n",
                           Output->Name, ViaOutputBusName(Output->Position));
    } else
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  CRTC 1: Disabled.\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");

    /* Secondary */
    if (pVia->Crtc[1]->Active) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  CRTC 2:\n");

        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_SECONDARY))
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "      Output %s on Bus %s.\n",
                           Output->Name, ViaOutputBusName(Output->Position));
    } else
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  CRTC 2: Disabled.\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");

    if (pVia->Simultaneous)
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  Simultaneous display: Enabled.\n");
    else
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  Simultaneous display: Disabled.\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "  Disabled outputs:\n");
    for (Output = pVia->Outputs; Output; Output = Output->Next)
        if (!Output->Active)
            xf86DrvMsg(pScrn->scrnIndex, X_INFO, "      Output %s on Bus %s.\n",
                       Output->Name, ViaOutputBusName(Output->Position));
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "\n");
}

/*
 *
 */
static struct ViaOutput *
ViaOutputFindType(struct ViaOutput *Outputs, int Type)
{
    struct ViaOutput *Output;

    for (Output = Outputs; Output; Output = Output->Next)
        if (Output->Type & Type)
            return Output;

    return NULL;
}

/*
 * ActiveDevice is grossly inadequate.
 */
static Bool
ViaOutputsActive(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);

    VIAFUNC(pScrn);

    if (pVia->ActiveDevice) {
        struct ViaOutput *Output;
        Bool Active = FALSE;

        /* Deactivate all first. */
        for (Output = pVia->Outputs; Output; Output = Output->Next)
            Output->Active = FALSE;

        /* Now activate following ActiveDevice */
        if (pVia->ActiveDevice & VIA_DEVICE_LCD) {
            Output = ViaOutputFindType(pVia->Outputs, OUTPUT_PANEL);
            if (Output) {
                Output->Active = TRUE;
                Active = TRUE;
            } else
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Unable to activate"
                           " panel: no panel is present.\n");
        }

        if (pVia->ActiveDevice & VIA_DEVICE_TV) {
            Output = ViaOutputFindType(pVia->Outputs, OUTPUT_TV);
            if (Output) {
                if (Output->Sense) {
                    if (Output->Sense(Output))
                        Output->Active = TRUE;
                    else {
                        xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                                   "Unable to activate TV encoder: "
                                   "no cable attached.\n");

                        Output->Active = FALSE;
                    }
                } else
                    Output->Active = TRUE;

                if (Output->Active)
                    Active = TRUE;
            } else
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Unable to activate"
                           " TV encoder: no TV encoder present.\n");
        }

        if (pVia->ActiveDevice & VIA_DEVICE_CRT) {
            Output = ViaOutputFindType(pVia->Outputs, OUTPUT_CRT);
            if (Output) {
                Output->Active = TRUE;
                Active = TRUE;
            } else /* Dream on. */
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Unable to activate"
                           " CRT: no CRT present.\n");
        }

        return Active;
    } else
        return ViaOutputsSense(pScrn);
}

/*
 *
 */
static void
ViaMatchCrtcsAndOutputs(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaCrtc *Crtc1 = pVia->Crtc[0];
    struct ViaCrtc *Crtc2 = pVia->Crtc[1];
    struct ViaOutput *Output = pVia->Outputs;

    /* Needs to adhere to one or multiple modes from one output */
    Bool Crtc1ModeFixed = FALSE;
    Bool Crtc2ModeFixed = FALSE;

    VIAFUNC(pScrn);

    Crtc1->Active = FALSE;
    Crtc2->Active = FALSE;

    for (Output = pVia->Outputs; Output; Output = Output->Next)
        Output->Owner = VIA_CRTC_UNASSIGNED;


    /* First check fixed position devices */
    if (pVia->Chipset == VT3122) {
        /* DFP is fixed */
        for (Output = pVia->Outputs; Output; Output = Output->Next) {
            if (!Output->Active)
                continue;

            if ((Output->Position == OUTPUT_BUS_DFP) ||
                (Output->Position == OUTPUT_BUS_DFPLOW) ||
                (Output->Position == OUTPUT_BUS_DFPHIGH)) {

                if (Crtc2ModeFixed &&
                    (Output->ModesTimingFixed || Output->ModesExclusive)) {
                    Output->Active = FALSE;

                    if (Output->ModesTimingFixed)
                        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Output %s requires Fixed "
                                   "Timing but CRTC2 is unavailable.\n", Output->Name);
                    else
                        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Output %s has an exclusive"
                                   " modelist but CRTC2 is unavailable.\n", Output->Name);
                } else {
                    Crtc2->Active = TRUE;
                    Output->Owner = Crtc2->ID;
                    Crtc2ModeFixed = Output->ModesTimingFixed || Output->ModesExclusive;
                }
            }
        }
    }

    /* Find Fixed Modes */
    for (Output = pVia->Outputs; Output; Output = Output->Next) {
        if (!Output->Active)
            continue;

        if (Output->Owner != VIA_CRTC_UNASSIGNED)
            continue;

        if (Output->ModesTimingFixed) {
            if (!Crtc2ModeFixed) {
                Crtc2->Active = TRUE;
                Crtc2ModeFixed = TRUE;

                Output->Owner = Crtc2->ID;
            } else if (!Crtc1ModeFixed) {
                Crtc1->Active = TRUE;
                Crtc1ModeFixed = TRUE;

                Output->Owner = Crtc1->ID;
            } else {
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Output %s requires Fixed "
                           "Timing but no CRTC is free.\n", Output->Name);
                Output->Active = FALSE;
            }
        }
    }

    /* Find exclusive Modes */
    for (Output = pVia->Outputs; Output; Output = Output->Next) {
        if (!Output->Active)
            continue;

        if (Output->Owner != VIA_CRTC_UNASSIGNED)
            continue;

        if (Output->ModesExclusive) {
            if (!Crtc1ModeFixed) {
                Crtc1->Active = TRUE;
                Crtc1ModeFixed = TRUE;

                Output->Owner = Crtc1->ID;
            } else if (!Crtc2ModeFixed) {
                Crtc2->Active = TRUE;
                Crtc2ModeFixed = TRUE;

                Output->Owner = Crtc2->ID;
            } else {
                xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Output %s has an exclusive"
                           " modelist but no CRTC is free.\n", Output->Name);
                Output->Active = FALSE;
            }
        }
    }

    /* Now assign the rest */
    for (Output = pVia->Outputs; Output; Output = Output->Next) {
        if (!Output->Active)
            continue;

        if (Output->Owner != VIA_CRTC_UNASSIGNED)
            continue;

        /* Just look for an active one. */
        if (Crtc1->Active) {
            Crtc1->Active = TRUE;
            Output->Owner = Crtc1->ID;
        } else if (Crtc2->Active) {
            Crtc2->Active = TRUE;
            Output->Owner = Crtc2->ID;
        } else { /* Just grab Crtc1 */
            Crtc1->Active = TRUE;
            Output->Owner = Crtc1->ID;
        }
    }
}

/*
 *
 */
Bool
ViaLayOutCrtcsAndOutputs(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaCrtc *Crtc1 = pVia->Crtc[0];
    struct ViaCrtc *Crtc2 = pVia->Crtc[1];
    struct ViaOutput *Output;

    VIAFUNC(pScrn);

    if (!ViaOutputsActive(pScrn)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No active output found.\n");
        return FALSE;
    }

    ViaMatchCrtcsAndOutputs(pScrn);

    /*
     * Great big load of sanity checks. We're bound to be playing around with
     * ViaMatchCrtcsAndOutputs.
     */
    for (Output = pVia->Outputs; Output; Output = Output->Next) {
        if (Output->Active) {
            if (Output->Owner == VIA_CRTC_UNASSIGNED) {
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                           "Output %s is Active yet unassigned.\n", Output->Name);
                Output->Active = FALSE;
            }
        } else {
            if (Output->Owner != VIA_CRTC_UNASSIGNED) {
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                           "Output %s is assigned but not active.\n", Output->Name);
                Output->Owner = VIA_CRTC_UNASSIGNED;
            }
        }
    }

    if (Crtc1->Active) {
        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_PRIMARY))
                break;
        if (!Output) {
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "CRTC1 has no outputs.\n");
            Crtc1->Active = FALSE;
        }
    } else {
        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_PRIMARY)) {
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                           "Output %s points to CRTC1 which is disabled.\n",
                           Output->Name);
                Output->Active = FALSE;
                Output->Owner = VIA_CRTC_UNASSIGNED;
            }
    }

    if (Crtc2->Active) {
        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_SECONDARY))
                break;
        if (!Output) {
            xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "CRTC2 has no outputs.\n");
            Crtc2->Active = FALSE;
        }
    } else {
        for (Output = pVia->Outputs; Output; Output = Output->Next)
            if (Output->Active && (Output->Owner == VIA_CRTC_SECONDARY)) {
                xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
                           "Output %s points to CRTC2 which is disabled.\n",
                           Output->Name);
                Output->Active = FALSE;
                Output->Owner = VIA_CRTC_UNASSIGNED;
            }
    }

    /* If both got disabled, we also don't have outputs */
    if (!Crtc1->Active && !Crtc2->Active) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No CRTC has been activated.\n");
        return FALSE;
    }

    /* special case */
    if (pVia->Simultaneous) {
        Crtc1->Active = TRUE;
        Crtc2->Active = TRUE;
    }

    ViaLayoutPrint(pScrn);
    return TRUE;
}

/*
 *
 */
void
ViaOutputModesCopyAdd(struct ViaOutput *Output, DisplayModePtr Additions)
{
    DisplayModePtr Mode, Last;

    if (!Additions)
        return;

    Last = Output->Modes;
    Mode = Additions;

    if (Last) {
        while (Last->next)
            Last = Last->next;
    } else {
        Output->Modes = ViaModeCopy(Mode);
        Last = Output->Modes;
        Mode = Mode->next;
    }

    while (Mode) {
        Last->next = ViaModeCopy(Mode);
        Last->next->prev = Last;

        Last = Last->next;
        Mode = Mode->next;
    }
}

/*
 *
 */
void
ViaOutputTimingSetFromConfig(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    MonPtr Config = pScrn->confScreen->monitor;
    struct ViaOutput *Output;
    int i;

    /* find the first CRT */
    for (Output = pVia->Outputs; Output; Output = Output->Next)
        if (Output->Type & OUTPUT_CRT) /* LVDS and TV _always_ know better */
            break;

    if (!Output) /* Our work is done. */
        return;

    if (!Output->MonitorName)
        Output->MonitorName = xnfstrdup(Config->id);

    if (Config->nHsync) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                   "\"%s - %s\": Imposing HSync values from config monitor \"%s\".\n",
                   Output->Name, Output->MonitorName, Config->id);
        Output->numHSync = Config->nHsync;
        for (i = 0; i < Config->nHsync; i++) {
            Output->HSync[i].lo = Config->hsync[i].lo;
            Output->HSync[i].hi = Config->hsync[i].hi;
        }
    } else if (!Output->numHSync) {
        Output->numHSync = 3;
        Output->HSync[0].lo = 31.5;
        Output->HSync[0].hi = 31.5;
        Output->HSync[1].lo = 35.15;
        Output->HSync[1].hi = 35.15;
        Output->HSync[2].lo = 35.5;
        Output->HSync[2].hi = 35.5;
    }

    if (Config->nVrefresh) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                   "\"%s - %s\": Imposing VRefresh values from config monitor \"%s\".\n",
                   Output->Name, Output->MonitorName, Config->id);
        Output->numVRefresh = Config->nVrefresh;
        for (i = 0; i < Config->nVrefresh; i++) {
            Output->VRefresh[i].lo = Config->vrefresh[i].lo;
            Output->VRefresh[i].hi = Config->vrefresh[i].hi;
        }
    } else if (!Output->numVRefresh) {
        Output->numVRefresh = 1;
        Output->VRefresh[0].lo = 50;
        Output->VRefresh[0].hi = 61;
    }

#ifdef MONREC_HAS_REDUCED
    if (Config->reducedblanking)
        Output->ReducedAllowed = TRUE;
#endif

    ViaOutputModesCopyAdd(Output, Config->Modes);

    /* other stuff will not be needed for us here */
}

/*
 *
 * Output Bus handling.
 *
 */
/*
 *
 */
static void
ViaOutputBusPower(struct ViaCrtc *Crtc, int Bus, Bool On)
{
    ScrnInfoPtr pScrn = xf86Screens[Crtc->scrnIndex];
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    VIAPtr pVia = VIAPTR(pScrn);

    ViaDebug(Crtc->scrnIndex, "%s: Bus %s %s.\n", __func__,
             ViaOutputBusName(Bus), On ? "On" : "Off");

    switch (Bus) {
    case OUTPUT_BUS_CRT:
        ;
        break;
    case OUTPUT_BUS_DVP0:
        if (On)
            ViaSeqMask(hwp, 0x1E, 0xC0, 0xC0);
        else
            ViaSeqMask(hwp, 0x1E, 0x00, 0xC0);
	if (!On && (pVia->Chipset == VT7205)) /* VT7205 has issues with this */
            ViaCrtcMask(hwp, 0x6C, 0, 0x01); /* TV CLK input disable */
        break;
    case OUTPUT_BUS_DVP1:
        if (On)
            ViaSeqMask(hwp, 0x1E, 0x30, 0x30);
        else
            ViaSeqMask(hwp, 0x1E, 0x00, 0x30);
        break;
    case OUTPUT_BUS_DFP:
        if (pVia->Chipset == VT3122) {
            if (On) {
                ViaSeqMask(hwp, 0x1E, 0x30, 0x30); /* Power on DI1 on */
                ViaCrtcMask(hwp, 0x93, 0x01, 0x01); /* Enable DI1 */
            } else {
                ViaSeqMask(hwp, 0x1E, 0x00, 0x30);
                ViaCrtcMask(hwp, 0x93, 0x00, 0x01);
            }
        } else {
            if (On)
                ViaSeqMask(hwp, 0x2A, 0x0F, 0x0F);
            else
                ViaSeqMask(hwp, 0x2A, 0x00, 0x0F);
        }

        /* DFP Display Enable */
        if (On)
            ViaCrtcMask(hwp, 0x91, 0x00, 0x80);
        else
            ViaCrtcMask(hwp, 0x91, 0x80, 0x80);
        break;
    case OUTPUT_BUS_DFPHIGH:
        if (On)
            ViaSeqMask(hwp, 0x2A, 0x0C, 0x0C);
        else
            ViaSeqMask(hwp, 0x2A, 0x00, 0x0C);
        break;
    case OUTPUT_BUS_DFPLOW:
        if (On)
            ViaSeqMask(hwp, 0x2A, 0x03, 0x03);
        else
            ViaSeqMask(hwp, 0x2A, 0x00, 0x03);
        break;
    default:
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%s: unhandled bus: %d\n",
                   __func__, Bus);
        break;
    }
}

/*
 * Prepare the bus for this output.
 *
 */
static void
ViaOutputBusSet(struct ViaCrtc *Crtc, struct ViaOutput *Output)
{
    vgaHWPtr hwp = VGAHWPTR(xf86Screens[Crtc->scrnIndex]);
    VIAPtr pVia = VIAPTR(xf86Screens[Crtc->scrnIndex]);

    VIAFUNC(Crtc);

    switch (Output->Position) {
    case OUTPUT_BUS_CRT:
        if (Crtc->ID == VIA_CRTC_SECONDARY)
            ViaSeqMask(hwp, 0x16, 0x40, 0x40);
        else
            ViaSeqMask(hwp, 0x16, 0x00, 0x40);

        break;
    case OUTPUT_BUS_DVP0:
        if (pVia->Chipset == VT3122) {
            Crtc->PLLFlags |= PLL_FLAG_EXTERNAL;

            if ((pVia->Chipset == VT3122) && VT3122_REV_IS_AX(pVia->HostRev)) {
                Crtc->PLLFlags |= PLL_FLAG_HALVE;
                if (Crtc->ID == VIA_CRTC_SECONDARY) /* polarity maybe?  */
                    ViaCrtcMask(hwp, 0x6C, 0x10, 0x10);

                if (pVia->HostRev != 0x02)
                    ViaCrtcMask(hwp, 0x6C, 0x00, 0x0E);
            }

            if (Crtc->ID == VIA_CRTC_SECONDARY) /* TV CLK source */
                ViaCrtcMask(hwp, 0x6C, 0x80, 0x80);
            else
                ViaCrtcMask(hwp, 0x6C, 0x00, 0x80);

            ViaCrtcMask(hwp, 0x6C, 0x01, 0x01); /* TV CLK input enable */
        } else {
            if (Crtc->ID == VIA_CRTC_SECONDARY)
                ViaCrtcMask(hwp, 0x96, 0x10, 0x10);
            else
                ViaCrtcMask(hwp, 0x96, 0x00, 0x10);
        }

        break;
    case OUTPUT_BUS_DVP1:
        if (Crtc->ID == VIA_CRTC_SECONDARY)
            ViaCrtcMask(hwp, 0x9B, 0x10, 0x10);
        else
            ViaCrtcMask(hwp, 0x9B, 0x00, 0x10);

        break;
    case OUTPUT_BUS_DFP:
        if (pVia->Chipset == VT3122) {
            if (Crtc->ID == VIA_CRTC_SECONDARY)
                ViaCrtcMask(hwp, 0x93, 0x80, 0x80); /* Set Source */
            else
                ViaCrtcMask(hwp, 0x93, 0x00, 0x80);
	    if (Output->Type == OUTPUT_TMDS)
		ViaCrtcMask(hwp, 0x88, 0x00, 0x40); /* ??? 18 vs 24bit maybe? */
        } else {
            if (Crtc->ID == VIA_CRTC_SECONDARY) { /* Set DFP source */
                ViaCrtcMask(hwp, 0x97, 0x10, 0x10);
                ViaCrtcMask(hwp, 0x99, 0x10, 0x10);
            } else {
                ViaCrtcMask(hwp, 0x97, 0x00, 0x10);
                ViaCrtcMask(hwp, 0x99, 0x00, 0x10);
            }
        }

        ViaCrtcMask(hwp, 0x6A, 0x08, 0x08);

        break;
    default:
        xf86DrvMsg(Crtc->scrnIndex, X_ERROR, "%s: unhandled bus: %d\n",
                   __func__, Output->Position);
        return;
    }
}

/*
 * Power according to On all active devices, power off when inactive.
 *
 */
void
ViaOutputsPower(struct ViaCrtc *Crtc, Bool On)
{
    struct ViaOutput *Output = VIAPTR(xf86Screens[Crtc->scrnIndex])->Outputs;

    ViaDebug(Crtc->scrnIndex, "%s: %s.\n", __func__, On ? "On" : "Off");

    for (; Output; Output = Output->Next) {
        if (Output->Owner != Crtc->ID)
            continue;

        if (Output->Active && On) {
            ViaOutputBusPower(Crtc, Output->Position, TRUE);

            if (Output->Power)
                Output->Power(Output, TRUE);
        } else {
            if (Output->Power)
                Output->Power(Output, FALSE);

            ViaOutputBusPower(Crtc, Output->Position, FALSE);
        }
    }
}

/*
 * Handle Grammar Correction. ;)
 */
static void
ViaOutputBusGammaEnable(struct ViaOutput *Output, Bool Enable)
{
    ScrnInfoPtr pScrn = xf86Screens[Output->scrnIndex];
    vgaHWPtr hwp = VGAHWPTR(pScrn);

    switch (Output->Position) {
    case OUTPUT_BUS_CRT:
    case OUTPUT_BUS_DFP:
        break;
    case OUTPUT_BUS_DVP0:
    case OUTPUT_BUS_DVP1:
        if (Enable)
            ViaCrtcMask(hwp, 0x32, 0x04, 0x04);
        else
            ViaCrtcMask(hwp, 0x32, 0x00, 0x04);
        break;
    default:
        xf86DrvMsg(Output->scrnIndex, X_ERROR, "%s: unhandled bus: %d\n",
                   __func__, Output->Position);
        break;
    }
}

/*
 *
 */
void
ViaOutputsModeSet(struct ViaCrtc *Crtc, DisplayModeRec *Mode)
{
    VIAPtr pVia = VIAPTR(xf86Screens[Crtc->scrnIndex]);
    struct ViaOutput *Output;

    VIAFUNC(Crtc);

    for (Output = pVia->Outputs; Output; Output = Output->Next) {
        if ((Output->Owner == Crtc->ID) && Output->Active) {
            ViaOutputBusPower(Crtc, Output->Position, TRUE);
            ViaOutputBusSet(Crtc, Output);

            if (Crtc->bpp == 8) /* palette, not gamma */
                ViaOutputBusGammaEnable(Output, FALSE);
            else
                ViaOutputBusGammaEnable(Output, TRUE);

            if (Output->Mode)
                Output->Mode(Output, Mode);
            if (Output->Power)
                Output->Power(Output, TRUE);
        } else if ((Output->Owner == Crtc->ID) ||
		   (Output->Owner == VIA_CRTC_UNASSIGNED)) {
            if (Output->Power)
                Output->Power(Output, FALSE);
            ViaOutputBusPower(Crtc, Output->Position, FALSE);
        }
    }
}