summaryrefslogtreecommitdiff
path: root/src/rhd_randr.c
blob: 954afe974e8fff15f0b4da126c4b45b9c6ea391f (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
/*
 * Copyright 2007, 2008  Luc Verhaegen <libv@exsuse.de>
 * Copyright 2007, 2008  Matthias Hopf <mhopf@novell.com>
 * Copyright 2007, 2008  Egbert Eich   <eich@novell.com>
 * Copyright 2007, 2008  Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
 */

/*
 * RandR interface.
 *
 * Only supports RandR 1.2 ATM or no RandR at all.
 */

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

/* Xserver interface */
#include "xf86.h"

#ifdef RANDR_12_SUPPORT
/* Xserver interface */
# include "randrstr.h"
# include "xf86i2c.h"		/* Missing in old versions of xf86Crtc.h */
# include "xf86Crtc.h"
# include "xf86RandR12.h"
# include "xaa.h"
# include "exa.h"
# define DPMS_SERVER
# include "X11/extensions/dpms.h"
# include "X11/Xatom.h"
#endif /* RANDR_12_SUPPORT */

#if HAVE_XF86_ANSIC_H
# include "xf86_ansic.h"
#endif

/* Driver specific headers */
#include "rhd.h"
#include "rhd_atombios.h"
#include "rhd_randr.h"

#ifdef RANDR_12_SUPPORT
# include "rhd_crtc.h"
# include "rhd_cursor.h"
# include "rhd_connector.h"
# include "rhd_output.h"
# include "rhd_modes.h"
# include "rhd_monitor.h"
# include "rhd_vga.h"
# include "rhd_pll.h"
# include "rhd_lut.h"
# include "rhd_mc.h"
# include "rhd_card.h"
# include "rhd_i2c.h"
# include "rhd_audio.h"

/*
 * Driver internal
 */

# define WRAP_SCRNINFO(func,new)					\
    do {							\
	rhdPtr->randr->func = pScrn->func;			\
	pScrn->func = new;					\
    } while (0)
# define UNWRAP_SCRNINFO(func)					\
	pScrn->func = rhdPtr->randr->func

struct rhdRandr {
    xf86CrtcPtr    RandrCrtc[2];
    xf86OutputPtr *RandrOutput;  /* NULL-terminated */
    void         (*PointerMoved)(int, int, int);
} ;

#define MAX_CONNECTORS_PER_RR_OUTPUT 4
/* Outputs and Connectors are combined for RandR due to missing abstraction */
typedef struct _rhdRandrOutput {
    char                 Name[64];
    struct rhdConnector *Connector;
    struct rhdOutput    *Output;
    DisplayModePtr	ScaledToMode;
    struct rhdCrtc      *Crtc;
    struct rhdConnector *AllConnectors[MAX_CONNECTORS_PER_RR_OUTPUT];
    Bool	        OutputActive;
} rhdRandrOutputRec, *rhdRandrOutputPtr;

struct rhdRandrCrtc {
    struct rhdCrtc    *rhdCrtc;
    union {
	FBLinearPtr MemXAA;
	ExaOffscreenArea *MemEXA;
    } u;
};

#define ATOM_SIGNAL_FORMAT    "SignalFormat"
#define ATOM_CONNECTOR_TYPE   "ConnectorType"
#define ATOM_CONNECTOR_NUMBER "ConnectorNumber"
#define ATOM_OUTPUT_NUMBER    "_OutputNumber"
#define ATOM_PANNING_AREA     "_PanningArea"
#define ATOM_BACKLIGHT        "_Backlight"
#define ATOM_COHERENT         "_Coherent"

static Atom atom_SignalFormat, atom_ConnectorType, atom_ConnectorNumber,
    atom_OutputNumber, atom_PanningArea, atom_Backlight, atom_Coherent;
static Atom atom_unknown, atom_VGA, atom_TMDS, atom_LVDS, atom_DisplayPort, atom_TV;
static Atom atom_DVI, atom_DVII, atom_DVID, atom_DVIA, atom_HDMI, atom_Panel;


/* Get RandR property values */
static Atom
rhdGetSignalFormat(rhdRandrOutputPtr ro)
{
    switch (ro->Output->Id) {
    case RHD_OUTPUT_DACA:
    case RHD_OUTPUT_DACB:
	switch (ro->Connector->Type) {
	case RHD_CONNECTOR_VGA:
	case RHD_CONNECTOR_DVI:
	case RHD_CONNECTOR_DVI_SINGLE:
	    return atom_VGA;
	case RHD_CONNECTOR_TV:
	default:
	    return atom_unknown;	/* TODO */
	}
    case RHD_OUTPUT_LVTMA:
    case RHD_OUTPUT_KLDSKP_LVTMA:
    case RHD_OUTPUT_LVDS:
    case RHD_OUTPUT_UNIPHYA:
    case RHD_OUTPUT_UNIPHYB:
    case RHD_OUTPUT_UNIPHYC:
    case RHD_OUTPUT_UNIPHYD:
    case RHD_OUTPUT_UNIPHYE:
    case RHD_OUTPUT_UNIPHYF:
#if RHD_OUTPUT_LVTMB != RHD_OUTPUT_LVDS
    case RHD_OUTPUT_LVTMB:
#endif
	switch (ro->Connector->Type) {
	case RHD_CONNECTOR_DVI:
	case RHD_CONNECTOR_DVI_SINGLE:
	    return atom_TMDS;
	case RHD_CONNECTOR_PANEL:
	    return atom_LVDS;
	default:
	    return atom_unknown;
	}
    case RHD_OUTPUT_TMDSA:
#if RHD_OUTPUT_TMDSB != RHD_OUTPUT_LVDS
    case RHD_OUTPUT_TMDSB:
#endif
	return atom_TMDS;
    default:
	return atom_unknown;
    }
}
static Atom
rhdGetConnectorType(rhdRandrOutputPtr ro)
{
    switch (ro->Connector->Type) {
    case RHD_CONNECTOR_VGA:
	return atom_VGA;
    case RHD_CONNECTOR_DVI:
    case RHD_CONNECTOR_DVI_SINGLE:
#ifdef NOTYET
	if (ro->Output->Connector == RHD_CONNECTOR_HDMI || ro->Output->Connector == RHD_CONNECTOR_HDMI_DUAL)
	    return atom_HDMI;
#endif
	if (strncmp (ro->Connector->Name, "DVI-I", 5) == 0)
	    return atom_DVII;
	if (strncmp (ro->Connector->Name, "DVI-D", 5) == 0)
	    return atom_DVID;
	if (strncmp (ro->Connector->Name, "DVI-A", 5) == 0)
	    return atom_DVIA;
	return atom_DVI;
    case RHD_CONNECTOR_PANEL:
	return atom_Panel;
    case RHD_CONNECTOR_TV:
	return atom_TV;		/* TODO */
    default:
	return atom_unknown;
    }
}

/* Set crtc pos according to mouse pos and panning information */
static void
rhdUpdateCrtcPos(RHDPtr rhdPtr, struct rhdCrtc *Crtc, int x, int y)
{
    int i;

    if (Crtc->MaxX > 0) {
	int cx = Crtc->X, cy = Crtc->Y;
	int w  = Crtc->CurrentMode->HDisplay;
	int h  = Crtc->CurrentMode->VDisplay;
	if (x < cx)
	    cx = x > Crtc->MinX ? x : Crtc->MinX;
	if (x >= cx + w)
	    cx = x < Crtc->MaxX ? x-w+1 : Crtc->MaxX-w;
	if (y < cy)
	    cy = y > Crtc->MinY ? y : Crtc->MinY;
	if (y >= cy + h)
	    cy = y < Crtc->MaxY ? y-h+1 : Crtc->MaxY-h;
	if (cx != Crtc->X || cy != Crtc->Y)
	    Crtc->FrameSet(Crtc, cx, cy);
	for (i = 0; i < 2; i++) {
	    xf86CrtcPtr crtc = (xf86CrtcPtr) rhdPtr->randr->RandrCrtc[i];
	    if (Crtc == ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc) {
		crtc->x = cx;
		crtc->y = cy;
	    }
	}
    }
}

/* Debug: print out state */
void
RHDDebugRandrState (RHDPtr rhdPtr, const char *msg)
{
    int i;
    xf86OutputPtr *ro;
    RHDDebug(rhdPtr->scrnIndex, "State at %s:\n", msg);

    for (i = 0; i < 2; i++) {
	xf86CrtcPtr    crtc = rhdPtr->randr->RandrCrtc[i];
	struct rhdCrtc *c = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
	RHDDebugCont("   RRCrtc #%d [rhd %s]: active %d [%d]  "
		     "mode %s (%dx%d) +%d+%d\n",
		     i, c->Name, crtc->enabled, c->Active,
		     crtc->mode.name ? crtc->mode.name : "unnamed",
		     crtc->mode.HDisplay, crtc->mode.VDisplay,
		     crtc->x, crtc->y);
    }
    for (ro = rhdPtr->randr->RandrOutput; *ro; ro++) {
	rhdRandrOutputPtr o = (rhdRandrOutputPtr) (*ro)->driver_private;
	ASSERT(!strcmp((*ro)->name,o->Name));
	RHDDebugCont("   RROut  %s [Out %s Conn %s]  Crtc %s [%s]  "
		     "[%sactive]  %s\n",
		     (*ro)->name, o->Output->Name, o->Connector->Name,
		     (*ro)->crtc ? ((struct rhdRandrCrtc *)((*ro)->crtc->driver_private))->rhdCrtc->Name : "null",
		     o->Output->Crtc ? o->Output->Crtc->Name : "null",
		     o->Output->Active ? "" : "in",
		     (*ro)->status == XF86OutputStatusConnected ? "connected" :
		     (*ro)->status == XF86OutputStatusDisconnected ? "disconnected" :
		     (*ro)->status == XF86OutputStatusUnknown ? "unknownState" :
		     "badState" );
    }
}


/*
 * xf86CrtcConfig callback functions
 */

static Bool
rhdRRXF86CrtcResize(ScrnInfoPtr pScrn, int width, int height)
{
    RHDFUNC(pScrn);
    /* This is strange... if we set virtualX/virtualY like the intel driver
     * does, we limit ourself in the future to this maximum size.
     * The check for this is internally in RandR, no idea why the intel driver
     * actually works this way...
     * Even more curious: if we DON'T update virtual, everything seems to
     * work as expected... */
#if 0
    pScrn->virtualX = width;
    pScrn->virtualY = height;
#endif
    return TRUE;
}


/*
 * xf86Crtc callback functions
 */

/* Turns the crtc on/off, or sets intermediate power levels if available. */
static void
rhdRRCrtcDpms(xf86CrtcPtr Crtc, int mode)
{
    RHDPtr rhdPtr        = RHDPTR(Crtc->scrn);
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)(Crtc->driver_private))->rhdCrtc;

    RHDDebug(rhdCrtc->scrnIndex, "%s: %s: %s\n", __func__, rhdCrtc->Name,
	     mode==DPMSModeOn ? "On" : mode==DPMSModeOff ? "Off" : "Other");

    switch (mode) {
    case DPMSModeOn:
	if (rhdCrtc->PLL)
	    rhdCrtc->PLL->Power(rhdCrtc->PLL, RHD_POWER_ON);
	rhdCrtc->Power(rhdCrtc, RHD_POWER_ON);
	rhdCrtc->Active = TRUE;
	break;
    case DPMSModeSuspend:
    case DPMSModeStandby:
	rhdCrtc->Power(rhdCrtc, RHD_POWER_RESET);
	if (rhdCrtc->PLL)
	    rhdCrtc->PLL->Power(rhdCrtc->PLL, RHD_POWER_RESET);
	rhdCrtc->Active = FALSE;
	break;
    case DPMSModeOff:
	rhdCrtc->Power(rhdCrtc, RHD_POWER_SHUTDOWN);
	if (rhdCrtc->PLL)
	    rhdCrtc->PLL->Power(rhdCrtc->PLL, RHD_POWER_SHUTDOWN);
	rhdCrtc->Active = FALSE;
	break;
    default:
	ASSERT(!"Unknown DPMS mode");
    }
    RHDDebugRandrState(rhdPtr, "POST-CrtcDpms");
}

/* Lock CRTC prior to mode setting. Returns whether unlock is needed */
static Bool
rhdRRCrtcLock(xf86CrtcPtr crtc)
{
    /* Looks like we don't have to lock for mode setting. Only as long as
     * buffers are fixed, of course */
    return FALSE;
}

/* Unlock CRTC after mode setting */
static void
rhdRRCrtcUnlock (xf86CrtcPtr crtc)
{ }

/* Helper: setup PLL and LUT for Crtc */
static void
setupCrtc(RHDPtr rhdPtr, struct rhdCrtc *Crtc)
{
    int i;

    /* PLL & LUT setup - static at the moment */
    if (Crtc->PLL)
	return;
    for (i = 0; i < 2; i++)
	if (Crtc == rhdPtr->Crtc[i])
	    break;
    ASSERT(i<2);
    Crtc->PLL = rhdPtr->PLLs[i];
    Crtc->LUT = rhdPtr->LUT[i];
}

/* Set video mode.
 * randr calls for Crtc and Output separately, while order should be
 * up to the driver. Well. */
static void
rhdRRCrtcPrepare(xf86CrtcPtr crtc)
{
    RHDPtr          rhdPtr = RHDPTR(crtc->scrn);
    ScrnInfoPtr     pScrn  = xf86Screens[rhdPtr->scrnIndex];
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)(crtc->driver_private))->rhdCrtc;

    RHDFUNC(rhdPtr);
    setupCrtc(rhdPtr, rhdCrtc);
    pScrn->vtSema = TRUE;

    /* Disable CRTCs to stop noise from appearing. */
    rhdCrtc->Power(rhdCrtc, RHD_POWER_RESET);

    /* Verify panning area */
    if (rhdCrtc->MaxX > rhdCrtc->Width)
	rhdCrtc->MaxX = rhdCrtc->Width;
    if (rhdCrtc->MaxY > rhdCrtc->Height)
	rhdCrtc->MaxY = rhdCrtc->Height;
}

static void
rhdRRCrtcModeSet(xf86CrtcPtr  crtc,
		 DisplayModePtr OrigMode, DisplayModePtr Mode,
		 int x, int y)
{
    RHDPtr          rhdPtr = RHDPTR(crtc->scrn);
    ScrnInfoPtr     pScrn  = xf86Screens[rhdPtr->scrnIndex];
    struct rhdCrtc  *rhdCrtc  = ((struct rhdRandrCrtc*) (crtc->driver_private))->rhdCrtc;
    xf86CrtcConfigPtr xf86CrtcConfig = XF86_CRTC_CONFIG_PTR(crtc->scrn);
    CARD32 ScanOutOffset;
    int i;

    /* RandR may give us a mode without a name... (xf86RandRModeConvert) */
    if (!Mode->name && crtc->mode.name)
	Mode->name = xstrdup(crtc->mode.name);

    RHDDebug(rhdPtr->scrnIndex, "%s: %s : %s at %d/%d\n", __func__,
	     rhdCrtc->Name, Mode->name, x, y);

    /*
     * for AtomBIOS SetPixelClock we need information about the outputs.
     * So find the output(s) matching this Crtc and assign the Crtc to it.
     */
    for (i = 0; i < xf86CrtcConfig->num_output; i++) {
	if (xf86CrtcConfig->output[i]->crtc == crtc) {
	    rhdRandrOutputPtr rout = xf86CrtcConfig->output[i]->driver_private;
	    rout->Output->Crtc = rhdCrtc;
	}
    }

    if (rhdPtr->verbosity >= 3) {
	xf86DrvMsg(rhdPtr->scrnIndex, X_INFO, "On Crtc %i Setting %3.1f Hz Mode: ",
		   rhdCrtc->Id, Mode->VRefresh);
	RHDPrintModeline(Mode);
	if (OrigMode->VDisplay != Mode->VDisplay || OrigMode->HDisplay != Mode->HDisplay) {
	    xf86DrvMsg(-1, X_NONE, "Scaled from: ");
	    RHDPrintModeline(OrigMode);
	}
    }
    /* Set up mode */
    if (crtc->rotatedData != NULL) {
	ScanOutOffset = (unsigned long) crtc->rotatedData - (unsigned long)rhdPtr->FbBase;
	x = y = 0;
    } else {
	ScanOutOffset = rhdPtr->FbScanoutStart;
    }
    rhdCrtc->FBSet(rhdCrtc, pScrn->displayWidth, pScrn->virtualX, pScrn->virtualY,
		pScrn->depth, ScanOutOffset);
    rhdCrtc->ModeSet(rhdCrtc, Mode);
    if (OrigMode->VDisplay != Mode->VDisplay || OrigMode->HDisplay != Mode->HDisplay)
	rhdCrtc->ScaleSet(rhdCrtc, rhdCrtc->ScaleType, OrigMode, Mode);
    else
	rhdCrtc->ScaleSet(rhdCrtc, RHD_CRTC_SCALE_TYPE_NONE, Mode, NULL);

    rhdCrtc->FrameSet(rhdCrtc, x, y);
    rhdUpdateCrtcPos(rhdPtr, rhdCrtc, rhdCrtc->Cursor->X, rhdCrtc->Cursor->Y);
    RHDPLLSet(rhdCrtc->PLL, Mode->Clock);		/* This also powers up PLL */
    rhdCrtc->LUTSelect(rhdCrtc, rhdCrtc->LUT);

    /*
     * RandR is able to bring up new Crtcs, but can't be bothered to set up
     * a cmap on them.
     *
     * The pScreen check tells us whether we are still in PreInit. If we are
     * still in PreInit, the xserver will still do the right thing and call
     * LoadPalette accordingly after the modeset. VT switch will also do the
     * right thing still, but at that time no new CRTC gets initialised, so
     * LUT->Initialised is either set, or the current function isn't called.
     */
    if (!rhdCrtc->LUT->Initialised && pScrn->pScreen)
	RHDLUTCopyForRR(rhdCrtc->LUT);
}

static void
rhdRRCrtcCommit(xf86CrtcPtr crtc)
{
    RHDPtr          rhdPtr = RHDPTR(crtc->scrn);
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)(crtc->driver_private))->rhdCrtc;

    RHDFUNC(rhdPtr);

    rhdCrtc->Active = TRUE;
    rhdCrtc->Power(rhdCrtc, RHD_POWER_ON);

    if (crtc->scrn->pScreen != NULL)
	xf86_reload_cursors(crtc->scrn->pScreen);

    RHDDebugRandrState(rhdPtr, rhdCrtc->Name);
}

/*
 * They just had to do NIH again here: Old X functionality provides a size, a
 * list of indices, and a table of RGB unsigned shorts. RandR provides what
 * is below. Apart from horribly breaking any attempt at being backwards
 * compatible, this also pretty much rules out the usage of indexed colours, as
 * each time even a single colour is changed an entirely new table has to be
 * uploaded. Just cute. -- libv.
 */
static void
rhdRRCrtcGammaSet(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
		  int size)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)(crtc->driver_private))->rhdCrtc;
    int indices[0x100]; /* would RandR use a size larger than 256? */
    LOCO colors[0x100];
    int i;

    RHDDebug(rhdCrtc->scrnIndex, "%s: %s.\n", __func__, rhdCrtc->Name);

    /* thanks so very much */
    for (i = 0; i < size; i++) {
	indices[i] = i;
	colors[i].red = red[i] >> 6;
	colors[i].green = green[i] >> 6;
	colors[i].blue = blue[i] >> 6;
    }

    rhdCrtc->LUT->Set(rhdCrtc->LUT, size, indices, colors);
}

/* Dummy, because not tested for NULL */
static Bool
rhdRRCrtcModeFixupDUMMY(xf86CrtcPtr    crtc,
			DisplayModePtr mode,
			DisplayModePtr adjusted_mode)
{
    return TRUE;
}

#if XF86_CRTC_VERSION >= 2
/* Entry point for RandR 1.3 panning */
static void
rhdRRCrtcSetOrigin(xf86CrtcPtr  crtc, int x, int y)
{
    struct rhdCrtc  *rhdCrtc  = ((struct rhdRandrCrtc*) (crtc->driver_private))->rhdCrtc;

    rhdCrtc->FrameSet(rhdCrtc, x, y);
}
#endif


/*
 * xf86Output callback functions
 */

static void
rhdRROutputCreateResources(xf86OutputPtr out)
{
    RHDPtr rhdPtr          = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout = (rhdRandrOutputPtr) out->driver_private;
    struct rhdOutput *o;
    INT32             val;
    CARD32            num;
    int               err;
    INT32             range[2];

    RHDFUNC(rhdPtr);

    /* Create atoms for RandR 1.3 properties */
    atom_SignalFormat    = MakeAtom(ATOM_SIGNAL_FORMAT,
				    sizeof(ATOM_SIGNAL_FORMAT)-1, TRUE);
    atom_ConnectorType   = MakeAtom(ATOM_CONNECTOR_TYPE,
				    sizeof(ATOM_CONNECTOR_TYPE)-1, TRUE);
    atom_ConnectorNumber = MakeAtom(ATOM_CONNECTOR_NUMBER,
				    sizeof(ATOM_CONNECTOR_NUMBER)-1, TRUE);
    atom_OutputNumber    = MakeAtom(ATOM_OUTPUT_NUMBER,
				    sizeof(ATOM_OUTPUT_NUMBER)-1, TRUE);
    atom_PanningArea     = MakeAtom(ATOM_PANNING_AREA,
				    sizeof(ATOM_PANNING_AREA)-1, TRUE);

    /* Create atoms for RandR 1.3 property values */
    atom_unknown         = MakeAtom("unknown", 7, TRUE);
    atom_VGA             = MakeAtom("VGA", 3, TRUE);
    atom_TMDS            = MakeAtom("TMDS", 4, TRUE);
    atom_LVDS            = MakeAtom("LVDS", 4, TRUE);
    atom_DisplayPort     = MakeAtom("DisplayPort", 11, TRUE);
    atom_TV              = MakeAtom("TV", 2, TRUE);
    atom_DVI             = MakeAtom("DVI", 3, TRUE);
    atom_DVII            = MakeAtom("DVI-I", 5, TRUE);
    atom_DVID            = MakeAtom("DVI-D", 5, TRUE);
    atom_DVIA            = MakeAtom("DVI-A", 5, TRUE);
    atom_HDMI            = MakeAtom("HDMI", 4, TRUE);
    atom_Panel           = MakeAtom("Panel", 5, TRUE);

    /* Set up properties */
    val = rhdGetSignalFormat(rout);
    /* TODO: for TV multiple signal formats will be possible */
    RRConfigureOutputProperty(out->randr_output, atom_SignalFormat,
			      FALSE, FALSE, TRUE, 1, &val);
    RRChangeOutputProperty(out->randr_output, atom_SignalFormat,
			   XA_ATOM, 32, PropModeReplace,
			   1, &val, FALSE, FALSE);

    val = rhdGetConnectorType(rout);
    RRConfigureOutputProperty(out->randr_output, atom_ConnectorType,
			      FALSE, FALSE, TRUE, 0, NULL);
    RRChangeOutputProperty(out->randr_output, atom_ConnectorType,
			   XA_ATOM, 32, PropModeReplace,
			   1, &val, FALSE, FALSE);

    for (num = 0; num < RHD_CONNECTORS_MAX; num++)
	if (rout->Connector == rhdPtr->Connector[num])
	    break;
    ASSERT(num < RHD_CONNECTORS_MAX);
    num++;		/* For RANDR_CONNECTOR_NUMBER 0 is unknown */
    RRConfigureOutputProperty(out->randr_output, atom_ConnectorNumber,
			      FALSE, FALSE, TRUE, 0, NULL);
    RRChangeOutputProperty(out->randr_output, atom_ConnectorNumber,
			   XA_INTEGER, 32, PropModeReplace,
			   1, &num, FALSE, FALSE);

    for (num = 1, o = rhdPtr->Outputs; o; num++, o = o->Next)
	if (rout->Output == o)
	    break;
    ASSERT(o);
    RRConfigureOutputProperty(out->randr_output, atom_OutputNumber,
			      FALSE, FALSE, FALSE, 0, NULL);
    RRChangeOutputProperty(out->randr_output, atom_OutputNumber,
			   XA_INTEGER, 32, PropModeReplace,
			   1, &num, FALSE, FALSE);

    RRConfigureOutputProperty(out->randr_output, atom_PanningArea,
			      FALSE, FALSE, FALSE, 0, NULL);
    RRChangeOutputProperty(out->randr_output, atom_PanningArea,
			   XA_STRING, 8, PropModeReplace,
			   0, NULL, FALSE, FALSE);

    if (rout->Output->Property) {
	if (rout->Output->Property(rout->Output, rhdPropertyCheck, RHD_OUTPUT_BACKLIGHT, NULL)) {
	    atom_Backlight = MakeAtom(ATOM_BACKLIGHT,
				      sizeof(ATOM_BACKLIGHT)-1, TRUE);

	    range[0] = 0;
	    range[1] = 255;
	    err = RRConfigureOutputProperty(out->randr_output, atom_Backlight,
					    FALSE, TRUE, FALSE, 2, range);
	    if (err != 0)
		xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR,
			   "RRConfigureOutputProperty error: %d\n", err);
	    else {
		union rhdPropertyData val;

		if (!rout->Output->Property(rout->Output, rhdPropertyGet, RHD_OUTPUT_BACKLIGHT, &val))
		    val.integer = 255;

		err = RRChangeOutputProperty(out->randr_output, atom_Backlight,
					     XA_INTEGER, 32, PropModeReplace,
					     1, &val.integer, FALSE, FALSE);
		if (err != 0)
		    xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR,
			       "In %s RRChangeOutputProperty error: %d\n",
			       __func__, err);
	    }
	}
	if (rout->Output->Property(rout->Output, rhdPropertyCheck, RHD_OUTPUT_COHERENT, NULL)) {
	    atom_Coherent = MakeAtom(ATOM_COHERENT,
				     sizeof(ATOM_COHERENT)-1, TRUE);

	    range[0] = 0;
	    range[1] = 1;
	    err = RRConfigureOutputProperty(out->randr_output, atom_Coherent,
					    FALSE, TRUE, FALSE, 2, range);
	    if (err != 0)
		xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR,
			   "RRConfigureOutputProperty error: %d\n", err);
	    else {
		union rhdPropertyData val;

		if (!rout->Output->Property(rout->Output, rhdPropertyGet, RHD_OUTPUT_COHERENT, &val))
		    val.Bool = 1;
		err = RRChangeOutputProperty(out->randr_output, atom_Coherent,
					     XA_INTEGER, 32, PropModeReplace,
					     1, &val.Bool, FALSE, FALSE);
		if (err != 0)
		    xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR,
			       "In %s RRChangeOutputProperty error: %d\n",
			       __func__, err);
	    }
	}
    }
}

/* Turns the output on/off, or sets intermediate power levels if available. */
/* Something that cannot be solved with the current RandR model is that
 * multiple connectors might be attached to the same output. They cannot be
 * driven with different crtcs then, but RandR sees them as different outputs
 * because of a missing abstraction layer. This implementation will silently
 * set one crtc or the other, and maybe even won't warn. */
static void
rhdRROutputDpms(xf86OutputPtr       out,
		int                 mode)
{
    RHDPtr rhdPtr          = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout = (rhdRandrOutputPtr) out->driver_private;
    xf86OutputPtr    *ro;
    struct rhdCrtc   *rhdCrtc = out->crtc ? ((struct rhdRandrCrtc *)(out->crtc->driver_private))->rhdCrtc : NULL;
    const char *outUsedBy = NULL;

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s : %s\n", __func__, rout->Name,
	     mode==DPMSModeOn ? "On" : mode==DPMSModeOff ? "Off" : "Other");

    for (ro = rhdPtr->randr->RandrOutput; *ro; ro++) {
	rhdRandrOutputPtr o = (rhdRandrOutputPtr) (*ro)->driver_private;
	if (o != rout && o->Output == rout->Output && (*ro)->crtc)
	    outUsedBy = (*ro)->name;
    }
    switch (mode) {
    case DPMSModeOn:
	rout->Output->Power(rout->Output, RHD_POWER_ON);
	rout->Output->Active = TRUE;
	ASSERT(rhdCrtc);
	ASSERT(rhdCrtc == rout->Output->Crtc);
	rout->Crtc = rhdCrtc;
	break;
    case DPMSModeSuspend:
    case DPMSModeStandby:
	if (outUsedBy) {
	    xf86DrvMsg(rhdPtr->scrnIndex, X_WARNING,
		   "RandR: While resetting %s: output %s is also used "
		   "by %s - ignoring\n",
		   out->name, rout->Output->Name, outUsedBy);
	    break;
	}
	rout->Output->Power(rout->Output, RHD_POWER_RESET);
	rout->Output->Active = FALSE;
	rout->Crtc = NULL;
	break;
    case DPMSModeOff:
	if (outUsedBy) {
	    xf86DrvMsg(rhdPtr->scrnIndex, X_WARNING,
		   "RandR: While switching off %s: output %s is also used "
		   "by %s - ignoring\n",
		   out->name, rout->Output->Name, outUsedBy);
	    break;
	}
	rout->Output->Power(rout->Output, RHD_POWER_SHUTDOWN);
	rout->Output->Active = FALSE;
	rout->Crtc = NULL;
	break;
    default:
	ASSERT(!"Unknown DPMS mode");
    }
    RHDDebugRandrState(rhdPtr, "POST-OutputDpms");
}

/* RandR has a weird sense of how validation and fixup should be handled:
 * - Fixup and validation can interfere, they would have to be looped
 * - Validation should be done after fixup
 * - There is no crtc validation AT ALL
 * - The necessity of adjusted_mode is questionable
 * - Outputs and crtcs are checked independently, and one at a time.
 *   This can interfere, the driver should know the complete setup for
 *   validation and fixup.
 *   We also cannot emulate, because we never know when validation is finished.
 * Therefore we only use a single function for all and have to work around
 * not knowing what the other crtcs might be needed for. */
static int
rhdRROutputModeValid(xf86OutputPtr  out,
		     DisplayModePtr OrigMode)
{
    RHDPtr             rhdPtr = RHDPTR(out->scrn);
    rhdRandrOutputPtr  rout   = (rhdRandrOutputPtr) out->driver_private;
    DisplayModePtr     Mode   = xf86DuplicateMode(OrigMode);
    int                Status;

    RHDFUNC(rhdPtr);

    /* RandR may give us a mode without a name... (xf86RandRModeConvert)
     * xf86DuplicateMode should fill it up, though */
    if (!Mode->name)
	Mode->name = xstrdup("n/a");

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s : %s\n", __func__,
	     rout->Name, Mode->name);
    if (rhdPtr->verbosity >= LOG_DEBUG)
	RHDPrintModeline(Mode);
    ASSERT(rout->Connector);
    ASSERT(rout->Output);

    /* If out->crtc is not NULL, it is not necessarily the Crtc that will
     * be used, so let's better skip crtc based checks... */
    /* Monitor is handled by RandR */
    if (!rout->Output->Connector)
	return MODE_OUTPUT_UNDEF;
    Status = RHDRRModeFixup(out->scrn, Mode, NULL, rout->Connector,
			    rout->Output, NULL, rout->ScaledToMode ? TRUE : FALSE);
    RHDDebug(rhdPtr->scrnIndex, "%s: %s: %s\n", __func__,
	     Mode->name, RHDModeStatusToString(Status));
    xfree(Mode->name);
    xfree(Mode);
    return Status;
}

static void
rhdRRModeCopy(DisplayModePtr  OrigMode, DisplayModePtr Mode)
{
    memset(Mode, 0, sizeof(DisplayModeRec));
    Mode->name       = xstrdup(OrigMode->name ? OrigMode->name : "n/a");
    Mode->status     = OrigMode->status;
    Mode->type       = OrigMode->type;
    Mode->Clock      = OrigMode->Clock;
    Mode->HDisplay   = OrigMode->HDisplay;
    Mode->HSyncStart = OrigMode->HSyncStart;
    Mode->HSyncEnd   = OrigMode->HSyncEnd;
    Mode->HTotal     = OrigMode->HTotal;
    Mode->HSkew      = OrigMode->HSkew;
    Mode->VDisplay   = OrigMode->VDisplay;
    Mode->VSyncStart = OrigMode->VSyncStart;
    Mode->VSyncEnd   = OrigMode->VSyncEnd;
    Mode->VTotal     = OrigMode->VTotal;
    Mode->VScan      = OrigMode->VScan;
    Mode->Flags      = OrigMode->Flags;

    if ((Mode->type & M_T_CRTC_C) == M_T_BUILTIN) {
	Mode->CrtcHDisplay = OrigMode->CrtcHDisplay;
	Mode->CrtcHBlankStart = OrigMode->CrtcHBlankStart;
	Mode->CrtcHSyncStart = OrigMode->CrtcHSyncStart;
	Mode->CrtcHBlankEnd = OrigMode->CrtcHBlankEnd;
	Mode->CrtcHSyncEnd = OrigMode->CrtcHSyncEnd;
	Mode->CrtcHTotal = OrigMode->CrtcHTotal;
	Mode->CrtcVDisplay = OrigMode->CrtcVDisplay;
	Mode->CrtcVBlankStart = OrigMode->CrtcVBlankStart;
	Mode->CrtcVSyncStart = OrigMode->CrtcVSyncStart;
	Mode->CrtcVSyncEnd = OrigMode->CrtcVSyncEnd;
	Mode->CrtcVBlankEnd = OrigMode->CrtcVBlankEnd;
	Mode->CrtcVTotal = OrigMode->CrtcVTotal;
    }
}

/*
 *
 */
static void
rhdRRSanitizeMode(DisplayModePtr Mode)
{
    if (!Mode->name)
	Mode->name = xstrdup("n/a");
    Mode->status = MODE_OK;
    if ((Mode->type & M_T_CRTC_C) != M_T_BUILTIN) {
	Mode->CrtcHDisplay = Mode->CrtcHBlankStart =
	    Mode->CrtcHSyncStart = Mode->CrtcHBlankEnd =
	    Mode->CrtcHSyncEnd = Mode->CrtcHTotal = 0;
	Mode->CrtcVDisplay = Mode->CrtcVBlankStart =
	    Mode->CrtcVSyncStart = Mode->CrtcVSyncEnd =
	    Mode->CrtcVBlankEnd = Mode->CrtcVTotal = 0;
	Mode->SynthClock = 0;
    }
}

/*
 *
 */
static void
rhdRRFreeOutputs( RHDPtr rhdPtr)
{
    xf86OutputPtr *ro;
    struct rhdOutput *Output;
    /*
     * modesUpdated indicates if we are entering here for a first time after a
     * OutputsModeValid(). In this case a new mode setting round starts, thus
     * we need to free all outputs so that all inactive outputs are freed.
     * We later on allocate each output.
     */
    for (Output = rhdPtr->Outputs; Output; Output = Output->Next) {
	if (Output->AllocFree) {
	    for (ro = rhdPtr->randr->RandrOutput; *ro; ro++) {
		rhdRandrOutputPtr o = (rhdRandrOutputPtr) (*ro)->driver_private;
		if (o->Output == Output) {
		    if (!(*ro)->crtc) {
			Output->AllocFree(Output, RHD_OUTPUT_FREE);
			RHDDebug(rhdPtr->scrnIndex, "%s: Freeing Output: %s\n",__func__,
				 Output->Name);
		    }
		}
	    }
	}
    }
}

/* The crtc is only known on fixup time. Now it's actually to late to reject a
 * mode and give a reasonable answer why (return is bool), but we'll better not
 * set a mode than scrap our hardware */
static Bool
rhdRROutputModeFixup(xf86OutputPtr  out,
		     DisplayModePtr OrigMode,
		     DisplayModePtr Mode)
{
    RHDPtr             rhdPtr = RHDPTR(out->scrn);
    rhdRandrOutputPtr  rout   = (rhdRandrOutputPtr) out->driver_private;
    struct rhdCrtc    *rhdCrtc   = NULL;
    int                Status;
    DisplayModePtr     DisplayedMode;
    Bool               Scaled = FALSE;

    RHDFUNC(rhdPtr);
    ASSERT(out->crtc);
    rhdCrtc = ((struct rhdRandrCrtc *)(out->crtc->driver_private))->rhdCrtc;

    rhdRRFreeOutputs(rhdPtr);

    xfree(Mode->name);
    if (rout->ScaledToMode) {
	DisplayModePtr tmp = RHDModeCopy(rout->ScaledToMode);
	/* validate against CRTC. */
	if ((Status = RHDValidateScaledToMode(rhdCrtc, tmp))!= MODE_OK) {
	    RHDDebug(rhdPtr->scrnIndex, "%s: %s ScaledToMode INVALID: [0x%x] %s\n", __func__,
		     tmp->name, Status, RHDModeStatusToString(Status));
	    xfree(tmp);
	    return FALSE; /* failing here doesn't help */
	}
	memcpy(Mode, tmp, sizeof(DisplayModeRec));
	Mode->name = xstrdup(tmp->name);
	Mode->prev = Mode->next = NULL;
	xfree(tmp->name);
	xfree(tmp);

        /* sanitize OrigMode */
	rhdRRSanitizeMode(OrigMode);
	DisplayedMode = OrigMode;
	rhdCrtc->ScaledToMode = Mode;
	Scaled = TRUE;
    } else {
	/* !@#$ xf86RandRModeConvert doesn't initialize Mode with 0
	 * Fixed in xserver git c6c284e6 */
	rhdRRModeCopy(OrigMode, Mode);

	DisplayedMode = Mode;
    }

    /* RHDRRModeFixup will set up the remaining bits */

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s : %s\n", __func__,
	     rout->Name, Mode->name);
    ASSERT(rout->Connector);
    ASSERT(rout->Output);

    setupCrtc(rhdPtr, rhdCrtc);

    /* Monitor is handled by RandR */
    if (!rout->Output->AllocFree || rout->Output->AllocFree(rout->Output, RHD_OUTPUT_ALLOC)) {
	Status = RHDRRModeFixup(out->scrn, DisplayedMode, rhdCrtc, rout->Connector,
				rout->Output, NULL, Scaled);
    } else
	Status = MODE_NO_ENCODER;

    if (Status != MODE_OK) {
	rout->OutputActive = FALSE;
	RHDDebug(rhdPtr->scrnIndex, "%s: %s FAILED: [0x%x] %s\n", __func__,
		 Mode->name, Status, RHDModeStatusToString(Status));
	return FALSE;
    }
    rout->OutputActive = TRUE;
    return TRUE;
}

/* Set output mode.
 * Again, randr calls for Crtc and Output separately, while order should be
 * up to the driver. There wouldn't be a need for prepare/set/commit then.
 * We cannot do everything in OutputModeSet, because the viewport isn't known
 * here. */
static void
rhdRROutputPrepare(xf86OutputPtr out)
{
    RHDPtr            rhdPtr = RHDPTR(out->scrn);
    ScrnInfoPtr       pScrn  = xf86Screens[rhdPtr->scrnIndex];
    rhdRandrOutputPtr rout   = (rhdRandrOutputPtr) out->driver_private;

    RHDFUNC(rhdPtr);
    pScrn->vtSema = TRUE;

    /* no active output == no mess */
    rout->Output->Power(rout->Output, RHD_POWER_RESET);
    rout->Output->Crtc = rout->Crtc = NULL;
}
static void
rhdRROutputModeSet(xf86OutputPtr  out,
		   DisplayModePtr OrigMode, DisplayModePtr Mode)
{
    RHDPtr            rhdPtr = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout   = (rhdRandrOutputPtr) out->driver_private;
    struct rhdCrtc   *rhdCrtc   = ((struct rhdRandrCrtc *)(out->crtc->driver_private))->rhdCrtc;

    RHDFUNC(rhdPtr);

    /* RandR may give us a mode without a name... (xf86RandRModeConvert) */
    if (!Mode->name && out->crtc->mode.name)
	Mode->name = xstrdup(out->crtc->mode.name);

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s : %s to %s\n", __func__,
	     rout->Name, Mode->name,
	     rhdCrtc->Name);

    /* RandR might want to set up several outputs (RandR speech) with different
     * crtcs, while the outputs differ in fact only by the connector and thus
     * cannot be used by different crtcs */
    if (rout->Crtc && rout->Crtc != rhdCrtc)
	xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR,
		   "RandR: Output %s has already CRTC attached - "
		   "assuming ouput/connector clash\n", rout->Name);
    /* Set up mode */
    rout->Crtc = rhdCrtc;
    ASSERT(rhdCrtc == rout->Output->Crtc);
    rout->Output->Mode(rout->Output, Mode);
}
static void
rhdRROutputCommit(xf86OutputPtr out)
{
    RHDPtr            rhdPtr = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout   = (rhdRandrOutputPtr) out->driver_private;
    CARD32            val;
    char              buf[32];
    struct rhdCrtc   *rhdCrtc   = ((struct rhdRandrCrtc *)(out->crtc->driver_private))->rhdCrtc;

    RHDFUNC(rhdPtr);
    ASSERT(rhdCrtc == rout->Output->Crtc);

    rout->Output->Active    = TRUE;
    rout->Output->Connector = rout->Connector; /* @@@ */
    rout->Output->Power(rout->Output, RHD_POWER_ON);

    /* Some outputs may have physical protocol changes (e.g. TV) */
    val = rhdGetSignalFormat(rout);
    RRChangeOutputProperty(out->randr_output, atom_SignalFormat,
			   XA_ATOM, 32, PropModeReplace,
			   1, &val, TRUE, FALSE);
    /* Should be a crtc property */
    if (rhdCrtc->MaxX > rhdCrtc->MinX && rhdCrtc->MaxY > rhdCrtc->MinY)
	sprintf(buf, "%dx%d+%d+%d", rhdCrtc->MaxX - rhdCrtc->MinX,
		rhdCrtc->MaxY - rhdCrtc->MinY, rhdCrtc->MinX, rhdCrtc->MinY);
    else
	buf[0] = 0;
    RRChangeOutputProperty(out->randr_output, atom_PanningArea,
			   XA_STRING, 8, PropModeReplace,
			   strlen(buf), buf, TRUE, FALSE);

    RHDDebugRandrState(rhdPtr, rout->Name);
}

/*
 * This function looks for other outputs on the connector rout is connected to.
 * If one of those outputs can be sensed and is sensed the function will return
 * one of those.
 */
static rhdRandrOutputPtr
rhdRROtherOutputOnConnectorHelper(RHDPtr rhdPtr, rhdRandrOutputPtr rout)
{
    xf86OutputPtr    *ro;

    for (ro = rhdPtr->randr->RandrOutput; *ro; ro++) {
	rhdRandrOutputPtr o =
	    (rhdRandrOutputPtr) (*ro)->driver_private;
	if (o != rout &&
	    o->Connector == rout->Connector &&
	    o->Output->Sense) {
	    /* Yes, this looks wrong, but is correct */
	    enum rhdSensedOutput SensedType =
		o->Output->Sense(o->Output, o->Connector);
	    if (SensedType != RHD_SENSED_NONE) {
		RHDOutputPrintSensedType(o->Output);
		return o;
	    }
	}
    }
    return NULL;
}

/* Probe for a connected output. */
static xf86OutputStatus
rhdRROutputDetect(xf86OutputPtr output)
{
    RHDPtr            rhdPtr = RHDPTR(output->scrn);
    rhdRandrOutputPtr rout   = (rhdRandrOutputPtr) output->driver_private;

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s\n", __func__, rout->Name);

    /* Assume that a panel is always connected */
    if (rout->Connector->Type == RHD_CONNECTOR_PANEL) {
	rout->Output->Connector = rout->Connector; /* @@@ */
	return XF86OutputStatusConnected;
    } else if (rout->Connector->Type ==  RHD_CONNECTOR_TV) /* until TV_OUT is fixed we bail here */
	return XF86OutputStatusDisconnected;

    if (rout->Connector->HPDCheck) {
	/* Hot Plug Detection available, use it */
	if (rout->Connector->HPDCheck(rout->Connector)) {
	    /*
	     * HPD returned true
	     */
	    if (rout->Output->Sense) {
		if ((rout->Output->SensedType
		     = rout->Output->Sense(rout->Output,
					   rout->Connector)) != RHD_SENSED_NONE) {
		    RHDOutputPrintSensedType(rout->Output);
		    rout->Output->Connector = rout->Connector; /* @@@ */
		    return XF86OutputStatusConnected;
		} else
		    return XF86OutputStatusDisconnected;
	    } else {
		/* HPD returned true, but no Sense() available
		 * Typically the case on TMDSB.
		 * Check if there is another output attached to this connector
		 * and use Sense() on that one to verify whether something
		 * is attached to this one */
		if (rhdRROtherOutputOnConnectorHelper(rhdPtr, rout))
		    return XF86OutputStatusDisconnected;
		rout->Output->Connector = rout->Connector; /* @@@ */
		return XF86OutputStatusConnected;
	    }
	} else {
	    /*
	     * HPD returned false
	     */
	    /* There is the infamous DMS-59 connector, on which HPD returns
	     * false when 'only' VGA is connected. */
	    if (rhdPtr->Card && (rhdPtr->Card->flags & RHD_CARD_FLAG_DMS59)) {
		xf86DrvMsg(rhdPtr->scrnIndex, X_INFO,
			   "RandR: Verifying state of DMS-59 VGA connector.\n");
		if (rout->Output->Sense) {
		    rout->Output->SensedType
			= rout->Output->Sense(rout->Output,
					      rout->Connector);
		    if (rout->Output->SensedType != RHD_SENSED_NONE) {
			rout->Output->Connector = rout->Connector; /* @@@ */
			RHDOutputPrintSensedType(rout->Output);
			return XF86OutputStatusConnected;
		    }
		}
	    }
	    return XF86OutputStatusDisconnected;
	}
    } else {
	/*
	 * No HPD available, Sense() if possible
	 */
	if (rout->Output->Sense) {
	    rout->Output->SensedType
		= rout->Output->Sense(rout->Output, rout->Connector);
	    if (rout->Output->SensedType != RHD_SENSED_NONE) {
		    rout->Output->Connector = rout->Connector; /* @@@ */
		    RHDOutputPrintSensedType(rout->Output);
		    return XF86OutputStatusConnected;
	    } else
		return XF86OutputStatusDisconnected;
	}
	/* Use DDC address probing if possible otherwise */
	if (rout->Connector->DDC) {
	    RHDI2CDataArg i2cRec;
	    i2cRec.probe.slave = 0xa0;
	    i2cRec.probe.i2cBusPtr = rout->Connector->DDC;
	    if (RHDI2CFunc(rhdPtr->scrnIndex, rhdPtr->I2C,RHD_I2C_PROBE_ADDR,&i2cRec)
		== RHD_I2C_SUCCESS) {
		rhdRandrOutputPtr rout_tmp;
		RHDDebug(rout->Output->scrnIndex, "DDC Probing for Output %s returned connected\n",
			 rout->Output->Name);
		if ((rout_tmp = rhdRROtherOutputOnConnectorHelper(rhdPtr, rout))) {
		RHDDebug(rout->Output->scrnIndex, "Output %s on same connector already connected\n",
			 rout_tmp->Output->Name);
		    return XF86OutputStatusDisconnected;
		}
		rout->Output->Connector = rout->Connector; /* @@@ */
		return XF86OutputStatusConnected;
	    } else {
		RHDDebug(rout->Output->scrnIndex, "DDC Probing for Output %s returned disconnected\n",
			 rout->Output->Name);
		return XF86OutputStatusDisconnected;
	    }
	}
	rout->Output->Connector = rout->Connector; /* @@@ */
	return XF86OutputStatusUnknown;
    }
}

/*
 * RHDRRMonitorInit(): adds synthesized modes for scaling to list generated by RHDMonitorInit()
 * as with RandR this is the only chance we have access to the full modes list.
 */
static struct rhdMonitor *
RHDRRMonitorInit(struct rhdConnector *Connector)
{
    struct rhdMonitor *m = RHDMonitorInit(Connector);

    RHDFUNC(Connector);
    if (RHDScalePolicy(m, Connector))
	RHDSynthModes(Connector->scrnIndex, m->Modes);

    return m;
}

/* Query the device for the modes it provides. Set MonInfo, mm_width/height. */
static DisplayModePtr
rhdRROutputGetModes(xf86OutputPtr output)
{
    RHDPtr            rhdPtr = RHDPTR(output->scrn);
    rhdRandrOutputPtr rout = (rhdRandrOutputPtr) output->driver_private;
    struct rhdOutput  *o;

    RHDDebug(rhdPtr->scrnIndex, "%s: Output %s\n", __func__, rout->Name);
    /* TODO: per-output options ForceReduced & UseXF86Edid */

    /* Nuke old monitor */
    if (rout->Connector->Monitor) {
	/* EDID is already freed by RandR (OutputSetEDID+return) */
	rout->Connector->Monitor->EDID = NULL;
	RHDMonitorDestroy(rout->Connector->Monitor);
    }

    /* Get new one */
    if (! (rout->Connector->Monitor = RHDRRMonitorInit(rout->Connector)) ) {
	xf86OutputSetEDID (output, NULL);
	return NULL;
    }

    ASSERT(rout->Output);
    o = rout->Output;

    if (RHDScalePolicy(rout->Connector->Monitor, rout->Connector)) {
	if (o->Connector->Monitor) {
	    rout->ScaledToMode = RHDModeCopy(o->Connector->Monitor->NativeMode);
	    xf86DrvMsg(rhdPtr->scrnIndex, X_INFO, "Found native mode: ");
	    RHDPrintModeline(rout->ScaledToMode);
	    if (RHDRRValidateScaledToMode(rout->Output, rout->ScaledToMode) != MODE_OK) {
		xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR, "Native mode doesn't validate: deleting\n");
		xfree(rout->ScaledToMode->name);
		xfree(rout->ScaledToMode);
		rout->ScaledToMode = NULL;
	    }
	}
    } else
	rout->ScaledToMode = NULL;

    /* If digitally attached, enable reduced blanking */
    if (rout->Output->Id == RHD_OUTPUT_TMDSA ||
	rout->Output->Id == RHD_OUTPUT_LVTMA ||
	rout->Output->Id == RHD_OUTPUT_KLDSKP_LVTMA ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYA ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYB ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYC ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYD ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYE ||
	rout->Output->Id == RHD_OUTPUT_UNIPHYF
	)
	rout->Connector->Monitor->ReducedAllowed = TRUE;
    /* Allow user overrides */
    if (rhdPtr->forceReduced.set)
	rout->Connector->Monitor->ReducedAllowed =
	    rhdPtr->forceReduced.val.bool;

    xf86OutputSetEDID (output, rout->Connector->Monitor->EDID);

    /* If no EDID data is available, calculate mm_size by assuming 96dpi
     * for the preferred (or first if no preferred) mode */
    if (! rout->Connector->Monitor->EDID) {
	DisplayModePtr m;
	for (m = rout->Connector->Monitor->Modes; m; m = m->next)
	    if (m->type & M_T_PREFERRED)
		break;
	if (! m)
	    m = rout->Connector->Monitor->Modes;
	if (m) {
#define DEFAULT_MM_PER_DOT (25.4 / 96)
	    output->mm_width  = m->HDisplay * DEFAULT_MM_PER_DOT;
	    output->mm_height = m->VDisplay * DEFAULT_MM_PER_DOT;
	    xf86DrvMsg(rhdPtr->scrnIndex, X_WARNING,
		       "No monitor size info, assuming 96dpi.\n");
	}
    }
    RHDDebug(rhdPtr->scrnIndex, "%s: Adding Output Modes:\n",__func__);
    if (rhdPtr->verbosity >= 7) {
	DisplayModePtr mode =  rout->Connector->Monitor->Modes;
	while (mode) {
	    RHDPrintModeline(mode);
	    mode = mode->next;
	}
    }
    return xf86DuplicateModes(output->scrn, rout->Connector->Monitor->Modes);
}

/* An output's property has changed. */
static Bool
rhdRROutputSetProperty(xf86OutputPtr out, Atom property,
		       RRPropertyValuePtr value)
{
    RHDPtr            rhdPtr = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout   = (rhdRandrOutputPtr) out->driver_private;

    RHDFUNC(rhdPtr);

    if (property == atom_PanningArea) {
	int w = 0, h = 0, x = 0, y = 0;
	struct rhdCrtc *Crtc = rout->Output->Crtc;
	int i;

	if (!Crtc)
	    return FALSE;
	for (i = 0; i < 2; i++) {
	    xf86CrtcPtr crtc = (xf86CrtcPtr) rhdPtr->randr->RandrCrtc[i];
	    if (Crtc == ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc) {
		/* Don't allow panning while rotated */
		if (crtc->rotation != RR_Rotate_0)
		    return FALSE;
		else
		    break;
	    }
	}
	if (value->type != XA_STRING || value->format != 8)
	    return FALSE;
	switch (sscanf(value->data, "%dx%d+%d+%d", &w, &h, &x, &y)) {
	case 0:
	case 2:
	case 4:
	    if (w < 0 || h < 0 || x < 0 || y < 0 ||
		x+w > Crtc->Width || y+h > Crtc->Height)
		return FALSE;
	    Crtc->MinX = x;
	    Crtc->MinY = y;
	    Crtc->MaxX = x + w;
	    Crtc->MaxY = y + h;
	    rhdUpdateCrtcPos(rhdPtr, Crtc, Crtc->Cursor->X, Crtc->Cursor->Y);
	    RHDDebug(rhdPtr->scrnIndex, "%s: PanningArea %d/%d - %d/%d\n",
		     x, y, x+w, y+h);
	    return TRUE;
	default:
	    return FALSE;
	}
    } else if (property == atom_Backlight) {
	if (value->type != XA_INTEGER || value->format != 32) {
	    xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR, "%s: wrong value\n", __func__);
	    return FALSE;
	}
	if (rout->Output->Property) {
	    union rhdPropertyData val;
	    val.integer = *(int*)(value->data);
	    return rout->Output->Property(rout->Output, rhdPropertySet,
					  RHD_OUTPUT_BACKLIGHT, &val);
	}
	return FALSE;
    } else if (property == atom_Coherent) {
	if (value->type != XA_INTEGER || value->format != 32) {
	    xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR, "%s: wrong value\n", __func__);
	    return FALSE;
	}
	if (rout->Output->Property) {
	    union rhdPropertyData val;
	    val.Bool = *(int*)(value->data);
	    return rout->Output->Property(rout->Output, rhdPropertySet,
					  RHD_OUTPUT_COHERENT, &val);
	}
	return FALSE;
    }

    return FALSE;	/* Others are not mutable */
}

/*
 *
 */
static void *
rhdRRCrtcShadowAllocate(xf86CrtcPtr crtc, int Width, int Height)
{
    ScrnInfoPtr	      pScrn = crtc->scrn;
    RHDPtr            rhdPtr = RHDPTR(crtc->scrn);
    ScreenPtr         pScreen = screenInfo.screens[pScrn->scrnIndex];
    struct rhdRandrCrtc *rhdRRCrtc = (struct rhdRandrCrtc*) crtc->driver_private;
    int		      OctPerPixel = pScrn->bitsPerPixel >> 3;
    int               Size = (pScrn->displayWidth * OctPerPixel) * Height;

    if (rhdPtr->AccelMethod == RHD_ACCEL_SHADOWFB
	|| rhdPtr->AccelMethod == RHD_ACCEL_NONE)
	return NULL;

#ifdef USE_EXA
    if (rhdPtr->AccelMethod == RHD_ACCEL_EXA) {

	ASSERT(rhdRRCrtc->u.MemEXA == NULL);

	rhdRRCrtc->u.MemEXA = exaOffscreenAlloc(pScreen, Size, 4096,
						TRUE, NULL, NULL);
	if (rhdRRCrtc->u.MemEXA == NULL) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "Unable to allocate shadow memory for rotated CRTC\n");
	    return NULL;
	}
	return ((char *)rhdPtr->FbBase
		+ rhdRRCrtc->u.MemEXA->offset);
    }

#endif /* USE_EXA */
    if (rhdPtr->AccelMethod == RHD_ACCEL_XAA) {
	int Align = (4096 + OctPerPixel - 1) / OctPerPixel;
	Size = (Size + OctPerPixel - 1) / OctPerPixel;

	ASSERT(rhdRRCrtc->u.MemXAA == NULL);

	rhdRRCrtc->u.MemXAA =
	    xf86AllocateOffscreenLinear(pScreen, Size, Align,  /* @@@ */
					       NULL, NULL, NULL);
	if (rhdRRCrtc->u.MemXAA == NULL) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "Unable to allocate shadow memory for rotated CRTC\n");
	    return NULL;
	}

	return  ((char *)rhdPtr->FbBase
		 + rhdPtr->FbScanoutStart
		 + rhdRRCrtc->u.MemXAA->offset * OctPerPixel);
    }

    return NULL;
}

/*
 *
 */
static PixmapPtr
rhdRRCrtcShadowCreate(xf86CrtcPtr Crtc, void *Data, int Width, int Height)
{
    ScrnInfoPtr pScrn = Crtc->scrn;
    PixmapPtr RPixmap;

    if (!Data)
	Data = rhdRRCrtcShadowAllocate(Crtc, Width, Height);

    RPixmap = GetScratchPixmapHeader(pScrn->pScreen,
					   Width, Height,
					   pScrn->depth,
					   pScrn->bitsPerPixel,
					   pScrn->displayWidth * pScrn->bitsPerPixel >> 3,
					   Data);

    if (RPixmap == NULL)
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "Unable to allocate shadow pixmap for rotated CRTC\n");

    return RPixmap;
}

/*
 *
 */
static void
rhdRRCrtcShadowDestroy(xf86CrtcPtr crtc, PixmapPtr RPixmap, void *Data)
{

    ScrnInfoPtr pScrn = crtc->scrn;
    RHDPtr rhdPtr = RHDPTR(crtc->scrn);
    struct rhdRandrCrtc *rhdRRCrtc = (struct rhdRandrCrtc*) crtc->driver_private;

    if (RPixmap)
	FreeScratchPixmapHeader(RPixmap);

    if (Data) {
#ifdef USE_EXA
	if (rhdPtr->AccelMethod == RHD_ACCEL_EXA) {
	    exaOffscreenFree(pScrn->pScreen, rhdRRCrtc->u.MemEXA);
	    rhdRRCrtc->u.MemEXA = NULL;
	}

#endif /* USE_EXA */
	if (rhdPtr->AccelMethod == RHD_ACCEL_XAA) {
	    xf86FreeOffscreenLinear(rhdRRCrtc->u.MemXAA);
	    rhdRRCrtc->u.MemXAA = NULL;
	}
    }
}


#ifdef RANDR_13_INTERFACE
static Bool
rhdRROutputGetProperty(xf86OutputPtr out, Atom property)
{
    RHDPtr rhdPtr          = RHDPTR(out->scrn);
    rhdRandrOutputPtr rout = (rhdRandrOutputPtr) out->driver_private;
    int err = BadValue;
    union rhdPropertyData val;

    xf86DrvMsg(rhdPtr->scrnIndex, X_INFO, "In %s\n", __func__);

    if (property == atom_Backlight) {
	if (rout->Output->Property == NULL)
	    return FALSE;

	if (!rout->Output->Property(rout->Output, rhdPropertyGet,
				    RHD_OUTPUT_BACKLIGHT, &val))
	    return FALSE;
	err = RRChangeOutputProperty(out->randr_output, atom_Backlight,
				     XA_INTEGER, 32, PropModeReplace,
				     1, &val.integer, FALSE, FALSE);

    } else if (property == atom_Coherent) {
	if (rout->Output->Property == NULL)
	    return FALSE;

	if (!rout->Output->Property(rout->Output, rhdPropertyGet,
				    RHD_OUTPUT_COHERENT, &val))
	    return FALSE;

	err = RRChangeOutputProperty(out->randr_output, atom_Coherent,
				     XA_INTEGER, 32, PropModeReplace,
				     1, &val.Bool, FALSE, FALSE);
    }

    if (err != 0) {
	xf86DrvMsg(rhdPtr->scrnIndex, X_ERROR, "In %s RRChangeOutputProperty error: %d\n", __func__, err);
	return FALSE;
    }
    return TRUE;
}

#endif

/*
 *
 */
Bool
RHDRRInitCursor(ScreenPtr pScreen)
{
    RHDFUNCI(pScreen->myNum);

    /* still need to alloc fb mem for cursors */
    return xf86_cursors_init(pScreen, MAX_CURSOR_WIDTH, MAX_CURSOR_HEIGHT,
			     HARDWARE_CURSOR_TRUECOLOR_AT_8BPP
			     | HARDWARE_CURSOR_UPDATE_UNHIDDEN
			     | HARDWARE_CURSOR_AND_SOURCE_WITH_MASK
			     | HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1
			     | HARDWARE_CURSOR_ARGB);
}

/*
 *
 */
static void
rhdRRShowCursor(xf86CrtcPtr crtc)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
    rhdCrtcShowCursor(rhdCrtc);
}

/*
 *
 */
static void
rhdRRHideCursor(xf86CrtcPtr crtc)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
    rhdCrtcHideCursor(rhdCrtc);
}

/*
 *
 */
static void
rhdRRLoadCursorARGB(xf86CrtcPtr crtc, CARD32 *Image)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
    rhdCrtcLoadCursorARGB(rhdCrtc, Image);
}

/*
 *
 */
static void
rhdRRSetCursorColors(xf86CrtcPtr crtc, int bg, int fg)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
    rhdCrtcSetCursorColors(rhdCrtc, bg, fg);
}

/*
 *
 */
static void
rhdRRSetCursorPosition(xf86CrtcPtr crtc, int x, int y)
{
    struct rhdCrtc *rhdCrtc = ((struct rhdRandrCrtc *)crtc->driver_private)->rhdCrtc;
    /*
     * Given cursor pos is always relative to frame - make absolute
     * NOTE: This is hardware specific, it doesn't really fit here,
     * but it's the only place where the relevant information is
     * available.
     */
    if (!crtc->rotatedData) {
	x += crtc->x;
	y += crtc->y;
    }
    rhdCrtcSetCursorPosition(rhdCrtc, x, y);
}

/*
 * Xorg Interface
 */

static const xf86CrtcConfigFuncsRec rhdRRCrtcConfigFuncs = {
    rhdRRXF86CrtcResize
};

static xf86CrtcFuncsRec rhdRRCrtcFuncs = {
    rhdRRCrtcDpms,
    NULL, NULL,						/* Save,Restore */
    rhdRRCrtcLock, rhdRRCrtcUnlock,
    rhdRRCrtcModeFixupDUMMY,
    rhdRRCrtcPrepare, rhdRRCrtcModeSet, rhdRRCrtcCommit,
    rhdRRCrtcGammaSet,
    rhdRRCrtcShadowAllocate, rhdRRCrtcShadowCreate, rhdRRCrtcShadowDestroy,
    rhdRRSetCursorColors, rhdRRSetCursorPosition, rhdRRShowCursor, rhdRRHideCursor,
    NULL, rhdRRLoadCursorARGB, NULL
    /* SetCursorColors,SetCursorPosition,ShowCursor,HideCursor,
     * LoadCursorImage,LoadCursorArgb,CrtcDestroy */
#ifdef XF86CRTCFUNCS_HAS_SETMODEMAJOR
    /* set_mode_major */
    , NULL
#endif
#if XF86_CRTC_VERSION >= 2
    /* set_origin */
    , rhdRRCrtcSetOrigin
#endif
};

/*
 *
 */
void
RHDRRFreeShadow(ScrnInfoPtr pScrn)
{
#ifndef HAVE_FREE_SHADOW
    int i;
    xf86CrtcConfigPtr   CrtcConfig = XF86_CRTC_CONFIG_PTR(pScrn);

    for (i = 0; i < CrtcConfig->num_crtc; i++) {
	xf86CrtcPtr Crtc = CrtcConfig->crtc[i];
	if (Crtc->rotatedPixmap || Crtc->rotatedData) {
	    Crtc->funcs->shadow_destroy(Crtc, Crtc->rotatedPixmap,
					Crtc->rotatedData);
	    Crtc->rotatedPixmap = NULL;
	    Crtc->rotatedData = NULL;
	}
    }
#else
    xf86RotateFreeShadow(pScrn);
#endif
}

static const xf86OutputFuncsRec rhdRROutputFuncs = {
    rhdRROutputCreateResources, rhdRROutputDpms,
    NULL, NULL,						/* Save,Restore */
    rhdRROutputModeValid, rhdRROutputModeFixup,
    rhdRROutputPrepare, rhdRROutputCommit,
    rhdRROutputModeSet, rhdRROutputDetect, rhdRROutputGetModes,
#ifdef RANDR_12_INTERFACE
    rhdRROutputSetProperty,		       /* Only(!) RANDR_12_INTERFACE */
#endif
#ifdef RANDR_13_INTERFACE
    rhdRROutputGetProperty,  /* get_property */
#endif
#ifdef RANDR_GET_CRTC_INTERFACE
    NULL,
#endif
    NULL						/* Destroy */
};


/* Helper: Create rhdRandrOutput with unique name per Connector/Output combo */
static rhdRandrOutputPtr
createRandrOutput(ScrnInfoPtr pScrn,
		  struct rhdConnector *conn, struct rhdOutput *out)
{
    rhdRandrOutputPtr rro;
    char *c;

    rro = xnfcalloc(1, sizeof(rhdRandrOutputRec));
    rro->Connector = conn;
    rro->Output    = out;
    sprintf(rro->Name, "%.30s", conn->Name);
    for (c = rro->Name; *c; c++)
	if (isspace(*c))
	    *c='_';
    return rro;
}

/* Helper: Consolidate names, improve duplicates */
static void
consolidateRandrOutputNames(rhdRandrOutputPtr *rop, int num)
{
    int i, j, changed;
    const char *outname;
    char *c;
    /* First try to come up with something sensible */
    for (i = 0; i < num; i++) {
	for (j = i+1; j < num; j++)
	    if (strcmp(rop[i]->Name, rop[j]->Name) == 0)
		break;
	if (j < num) {
	    for (j = num-1; j >= i; j--)	/* Include i */
		if (strcmp(rop[i]->Name, rop[j]->Name) == 0) {
		    switch (rop[j]->Output->Id) {
		    case RHD_OUTPUT_DACA:
		    case RHD_OUTPUT_DACB:
			outname = "analog";
			break;
		    case RHD_OUTPUT_TMDSA:
		    case RHD_OUTPUT_LVTMA:
		    case RHD_OUTPUT_KLDSKP_LVTMA:
		    case RHD_OUTPUT_UNIPHYA:
		    case RHD_OUTPUT_UNIPHYB:
		    case RHD_OUTPUT_UNIPHYC:
		    case RHD_OUTPUT_UNIPHYD:
		    case RHD_OUTPUT_UNIPHYE:
		    case RHD_OUTPUT_UNIPHYF:
			outname = "digital";
			break;
		    default:
			outname = rop[j]->Output->Name;
		    }
		    sprintf(rop[j]->Name, "%.30s/%.30s",
			    rop[j]->Connector->Name, outname);
		    for (c = rop[j]->Name; *c; c++)
			if (isspace(*c))
			    *c='_';
		}
	}
    }
    /* This shouldn't happen, but better be safe:
     * fix up every remaining name with the old naming scheme */
    for (i = 0; i < num; i++) {
	changed=0;
	for (j = i+1; j < num; j++)
	    if (strcmp(rop[i]->Name, rop[j]->Name) == 0) {
		changed++;
		sprintf(rop[j]->Name, "%.30s/%.30s",
			rop[j]->Connector->Name, rop[j]->Output->Name);
		for (c = rop[j]->Name; *c; c++)
		    if (isspace(*c))
			*c='_';
	    }
	if (changed) {
	    sprintf(rop[i]->Name, "%.30s/%.30s",
		    rop[i]->Connector->Name, rop[i]->Output->Name);
	    for (c = rop[i]->Name; *c; c++)
		if (isspace(*c))
		    *c='_';
	}
    }
}

/* Helper: Create and set up xf86Output */
static xf86OutputPtr
createXF86Output(ScrnInfoPtr pScrn, rhdRandrOutputPtr rro)
{
    xf86OutputPtr xo;
    xo = xf86OutputCreate(pScrn, &rhdRROutputFuncs, rro->Name);
    ASSERT(xo);
    xo->driver_private = rro;
    xo->possible_crtcs  = ~0;				/* No limitations */
    xo->possible_clones = ~0;				/* No limitations */
    xo->interlaceAllowed = TRUE;
    xo->doubleScanAllowed = TRUE;
    xo->subpixel_order = SubPixelUnknown;
    xo->use_screen_monitor = FALSE;
    return xo;
}

/* Call in PreInit after memory + io has been set up */
Bool
RHDRandrPreInit(ScrnInfoPtr pScrn)
{
    RHDPtr rhdPtr = RHDPTR(pScrn);
    struct rhdRandr *randr;
    int i, j, k, numCombined = 0;
    rhdRandrOutputPtr *RandrOutput, *r;
    char *outputorder;

    RHDFUNC(rhdPtr);
    if (rhdPtr->noRandr.val.bool) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "RandR 1.2 support disabled due to configuration\n");
	return FALSE;
    }

    randr = xnfcalloc(1, sizeof(struct rhdRandr));
    xf86CrtcConfigInit(pScrn, &rhdRRCrtcConfigFuncs);
    /* Maximum for D1GRPH_X/Y_END: 8k */
    xf86CrtcSetSizeRange (pScrn, 320, 200, 8000, 8000);

    for (i = 0; i < 2; i++) {
	randr->RandrCrtc[i] = xf86CrtcCreate(pScrn, &rhdRRCrtcFuncs);
	ASSERT(randr->RandrCrtc[i]);
	randr->RandrCrtc[i]->driver_private = xnfcalloc(sizeof(struct rhdRandrCrtc),1);
	((struct rhdRandrCrtc*) randr->RandrCrtc[i]->driver_private)->rhdCrtc = rhdPtr->Crtc[i]; /* TODO: not cleaning up correctly */
    }

    /* First count, then allocate */
    for (i = 0; i < RHD_CONNECTORS_MAX; i++) {
	struct rhdConnector *conn = rhdPtr->Connector[i];
	if (conn)
	    for (j = 0; j < MAX_OUTPUTS_PER_CONNECTOR; j++) {
		struct rhdOutput *out = conn->Output[j];
		if (out)
		    numCombined++;
	    }
    }
    RandrOutput = r =
	xnfcalloc(numCombined+1, sizeof(struct rhdRandrOutputPtr *));

    /* Create combined unique output names */
    for (i = 0; i < RHD_CONNECTORS_MAX; i++) {
	struct rhdConnector *conn = rhdPtr->Connector[i];
	if (conn) {
	    for (j = 0; j < MAX_OUTPUTS_PER_CONNECTOR; j++) {
		struct rhdOutput *out = conn->Output[j];
		if (out)
		    *r++ = createRandrOutput(pScrn, conn, out);
	    }
	}
    }

    /*
     * Each rhdRandrOutputRec carries a list of all connectors this output belongs to.
     * Since the output can only drive one connector but RandR doesn't seem to have
     * any notion of 'shared outputs' we need to probe them all and decide which
     * output that reports connected we pass to RandR as connected.
     */
    for (i = 0; i < numCombined; i++) {
	int cnt = 0;
	for (j = 0; j < RHD_CONNECTORS_MAX; j++) {
	    struct rhdConnector *conn = rhdPtr->Connector[j];
	    if (!conn) continue;
	    for (k = 0; k < MAX_OUTPUTS_PER_CONNECTOR; k++) {
		if (conn->Output[k] == RandrOutput[i]->Output) {
		    if (cnt >= MAX_CONNECTORS_PER_RR_OUTPUT)
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "%s: Number of Connectors for Output %s exceeds %i\n",
				   __func__,RandrOutput[i]->Name, MAX_CONNECTORS_PER_RR_OUTPUT);
		    else
			RandrOutput[i]->AllConnectors[cnt++] = conn;
		    break;
		}
	    }
	}
    }
    consolidateRandrOutputNames(RandrOutput, numCombined);
    for (i = 0; i < numCombined; i++)
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "RandR: Adding RRoutput %s for Output %s\n",
		   RandrOutput[i]->Name, RandrOutput[i]->Output->Name);

    /* Reorder for xinerama screen enumeration if requested */
    outputorder = rhdPtr->rrOutputOrder.val.string;
    if (outputorder && *outputorder) {
    	rhdRandrOutputPtr *RandrOutputReorder = r =
	    xnfcalloc(numCombined+1, sizeof(struct xf86OutputPtr *));
	while (*outputorder) {
	    char *end = strchr(outputorder, ' ');
	    int len = end ? end-outputorder : (signed) strlen(outputorder);
	    end = strchr(outputorder, ',');
	    if (end)
		len = end-outputorder < len ? end-outputorder : len;
	    for (i = 0; i < numCombined; i++) {
		if (RandrOutput[i] &&
		    strncmp(RandrOutput[i]->Name, outputorder, len) == 0 &&
		    RandrOutput[i]->Name[len] == 0) {
		    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			       "RandR: Reordering output %s\n",
			       RandrOutput[i]->Name);
		    *r++ = RandrOutput[i];
		    RandrOutput[i] = NULL;
		}
	    }
	    outputorder += len;
	    while (*outputorder == ' ' || *outputorder == ',')
		outputorder++;
	}
	for (i = 0; i < numCombined; i++)
	    if (RandrOutput[i])
		*r++ = RandrOutput[i];
	ASSERT(r - RandrOutputReorder == numCombined);
	xfree(RandrOutput);
	RandrOutput = RandrOutputReorder;
    }

    /* Initialize RandR structures */
    randr->RandrOutput =
	xnfcalloc(numCombined+1, sizeof(struct xf86OutputPtr *));
    for (i = 0; i < numCombined; i++) {
	randr->RandrOutput[i] = createXF86Output(pScrn, RandrOutput[i]);
    }
    xfree(RandrOutput);
    rhdPtr->randr = randr;

    if (!xf86InitialConfiguration(pScrn, FALSE)) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "RandR: No valid modes. Disabling RandR support.\n");
	for (i = 0; i < 2; i++)
	    xfree(randr->RandrCrtc[i]->driver_private);
	xfree(randr);
	rhdPtr->randr = NULL;		/* TODO: not cleaning up correctly */
	return FALSE;
    }
    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	       "RandR 1.2 support enabled\n");

    /* No docs, but radeon, avivo, and nv drivers calling this
     * after(!) xf86InitialConfiguration */
    if (!xf86RandR12PreInit (pScrn)) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "RandR: xf86RandR12PreInit failed. Disabled.\n");
	for (i = 0; i < 2; i++)
	    xfree(randr->RandrCrtc[i]->driver_private);
	xfree(randr);
	rhdPtr->randr = NULL;		/* TODO: not cleaning up correctly */
	return FALSE;
    }

    return TRUE;
}

/* Wrapped PointerMoved for driver-level panning */
static void
rhdRRPointerMoved(int scrnIndex, int x, int y)
{
    ScrnInfoPtr pScrn  = xf86Screens[scrnIndex];
    RHDPtr      rhdPtr = RHDPTR(xf86Screens[scrnIndex]);
    int i;

    for (i = 0; i < 2; i++) {
	struct rhdCrtc *Crtc = rhdPtr->Crtc[i];
	if (Crtc->scrnIndex == scrnIndex && Crtc->Active)
	    rhdUpdateCrtcPos(rhdPtr, Crtc, x + pScrn->frameX0, y + pScrn->frameY0);
    }
    UNWRAP_SCRNINFO(PointerMoved);
    pScrn->PointerMoved(scrnIndex, x, y);
    WRAP_SCRNINFO(PointerMoved, rhdRRPointerMoved);
}

/* Call in ScreenInit after frame buffer + acceleration layers */
Bool
RHDRandrScreenInit(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);

    RHDFUNC(rhdPtr);

    if (rhdPtr->AccelMethod == RHD_ACCEL_NONE || rhdPtr->AccelMethod == RHD_ACCEL_SHADOWFB) {
	rhdRRCrtcFuncs.shadow_allocate = NULL;
	rhdRRCrtcFuncs.shadow_create = NULL;
	rhdRRCrtcFuncs.shadow_destroy = NULL;
    }

    if (!xf86CrtcScreenInit(pScreen))
	return FALSE;
    /* Wrap cursor for driver-level panning */
    WRAP_SCRNINFO(PointerMoved, rhdRRPointerMoved);

    RHDDebugRandrState(rhdPtr, "POST-ScreenInit");
    return TRUE;
}

/* ModeInit: Set modes according to current layout */
Bool
RHDRandrModeInit(ScrnInfoPtr pScrn)
{
    Bool ret;
    RHDPtr rhdPtr = RHDPTR(pScrn);

    RHDFUNC(rhdPtr);

    /* Stop crap from being shown: gets reenabled through SaveScreen */
    rhdPtr->Crtc[0]->Blank(rhdPtr->Crtc[0], TRUE);
    rhdPtr->Crtc[1]->Blank(rhdPtr->Crtc[1], TRUE);

    RHDPrepareMode(rhdPtr);
    ret = xf86SetDesiredModes(pScrn);
    RHDDebugRandrState(rhdPtr, "POST-ModeInit");

    return ret;
}

/* SwitchMode: Legacy: Set one mode */
Bool
RHDRandrSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
{
    return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
}


#else /* RANDR_12_SUPPORT */

Bool
RHDRandrPreInit(ScrnInfoPtr pScrn)
{
    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	       "RandR 1.2 support disabled (not available at compile time)\n");
    return FALSE;
}

Bool
RHDRandrScreenInit(ScreenPtr pScreen)
{ ASSERT(0); return FALSE; }

Bool
RHDRandrModeInit(ScrnInfoPtr pScrn)
{ ASSERT(0); return FALSE; }

Bool
RHDRandrSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
{ ASSERT(0); return FALSE; }


#endif /* RANDR_12_SUPPORT */