xf86EdidModes.c revision 4642e01f
1/*
2 * Copyright 2006 Luc Verhaegen.
3 * Copyright 2008 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * @file This file covers code to convert a xf86MonPtr containing EDID-probed
27 * information into a list of modes, including applying monitor-specific
28 * quirks to fix broken EDID data.
29 */
30#ifdef HAVE_XORG_CONFIG_H
31#include <xorg-config.h>
32#else
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36#endif
37
38#define _PARSE_EDID_
39#include "xf86.h"
40#include "xf86DDC.h"
41#include <X11/Xatom.h>
42#include "property.h"
43#include "propertyst.h"
44#include "xf86Crtc.h"
45#include <string.h>
46#include <math.h>
47
48static Bool
49xf86MonitorSupportsReducedBlanking(xf86MonPtr DDC)
50{
51    /* EDID 1.4 explicitly defines RB support */
52    if (DDC->ver.revision >= 4) {
53	int i;
54	for (i = 0; i < DET_TIMINGS; i++) {
55	    struct detailed_monitor_section *det_mon = &DDC->det_mon[i];
56	    if (det_mon->type == DS_RANGES)
57		if (det_mon->section.ranges.supported_blanking & CVT_REDUCED)
58		    return TRUE;
59	}
60
61	return FALSE;
62    }
63
64    /* For anything older, assume digital means RB support. Boo. */
65    if (DDC->features.input_type)
66        return TRUE;
67
68    return FALSE;
69}
70
71/*
72 * Quirks to work around broken EDID data from various monitors.
73 */
74
75typedef enum {
76    DDC_QUIRK_NONE = 0,
77    /* First detailed mode is bogus, prefer largest mode at 60hz */
78    DDC_QUIRK_PREFER_LARGE_60 = 1 << 0,
79    /* 135MHz clock is too high, drop a bit */
80    DDC_QUIRK_135_CLOCK_TOO_HIGH = 1 << 1,
81    /* Prefer the largest mode at 75 Hz */
82    DDC_QUIRK_PREFER_LARGE_75 = 1 << 2,
83    /* Convert detailed timing's horizontal from units of cm to mm */
84    DDC_QUIRK_DETAILED_H_IN_CM = 1 << 3,
85    /* Convert detailed timing's vertical from units of cm to mm */
86    DDC_QUIRK_DETAILED_V_IN_CM = 1 << 4,
87    /* Detailed timing descriptors have bogus size values, so just take the
88     * maximum size and use that.
89     */
90    DDC_QUIRK_DETAILED_USE_MAXIMUM_SIZE = 1 << 5,
91    /* Monitor forgot to set the first detailed is preferred bit. */
92    DDC_QUIRK_FIRST_DETAILED_PREFERRED = 1 << 6,
93    /* use +hsync +vsync for detailed mode */
94    DDC_QUIRK_DETAILED_SYNC_PP = 1 << 7,
95    /* Force single-link DVI bandwidth limit */
96    DDC_QUIRK_DVI_SINGLE_LINK = 1 << 8,
97} ddc_quirk_t;
98
99static Bool quirk_prefer_large_60 (int scrnIndex, xf86MonPtr DDC)
100{
101    /* Belinea 10 15 55 */
102    if (memcmp (DDC->vendor.name, "MAX", 4) == 0 &&
103	((DDC->vendor.prod_id == 1516) ||
104	(DDC->vendor.prod_id == 0x77e)))
105	return TRUE;
106
107    /* Acer AL1706 */
108    if (memcmp (DDC->vendor.name, "ACR", 4) == 0 &&
109	DDC->vendor.prod_id == 44358)
110	return TRUE;
111
112    /* Bug #10814: Samsung SyncMaster 225BW */
113    if (memcmp (DDC->vendor.name, "SAM", 4) == 0 &&
114	DDC->vendor.prod_id == 596)
115	return TRUE;
116
117    /* Bug #10545: Samsung SyncMaster 226BW */
118    if (memcmp (DDC->vendor.name, "SAM", 4) == 0 &&
119	DDC->vendor.prod_id == 638)
120	return TRUE;
121
122    /* Acer F51 */
123    if (memcmp (DDC->vendor.name, "API", 4) == 0 &&
124	DDC->vendor.prod_id == 0x7602)
125	return TRUE;
126
127
128    return FALSE;
129}
130
131static Bool quirk_prefer_large_75 (int scrnIndex, xf86MonPtr DDC)
132{
133    /* Bug #11603: Funai Electronics PM36B */
134    if (memcmp (DDC->vendor.name, "FCM", 4) == 0 &&
135	DDC->vendor.prod_id == 13600)
136	return TRUE;
137
138    return FALSE;
139}
140
141static Bool quirk_detailed_h_in_cm (int scrnIndex, xf86MonPtr DDC)
142{
143    /* Bug #11603: Funai Electronics PM36B */
144    if (memcmp (DDC->vendor.name, "FCM", 4) == 0 &&
145	DDC->vendor.prod_id == 13600)
146	return TRUE;
147
148    return FALSE;
149}
150
151static Bool quirk_detailed_v_in_cm (int scrnIndex, xf86MonPtr DDC)
152{
153    /* Bug #11603: Funai Electronics PM36B */
154    if (memcmp (DDC->vendor.name, "FCM", 4) == 0 &&
155	DDC->vendor.prod_id == 13600)
156	return TRUE;
157
158    /* Bug #21000: LGPhilipsLCD LP154W01-TLAJ */
159    if (memcmp (DDC->vendor.name, "LPL", 4) == 0 &&
160	DDC->vendor.prod_id == 47360)
161	return TRUE;
162
163    return FALSE;
164}
165
166static Bool quirk_detailed_use_maximum_size (int scrnIndex, xf86MonPtr DDC)
167{
168    /* Bug #10304: LGPhilipsLCD LP154W01-A5 */
169    if (memcmp (DDC->vendor.name, "LPL", 4) == 0 &&
170	(DDC->vendor.prod_id == 0 || DDC->vendor.prod_id == 0x2a00))
171	return TRUE;
172
173    /* Bug #21324: Iiyama Vision Master 450 */
174    if (memcmp (DDC->vendor.name, "IVM", 4) == 0 &&
175	DDC->vendor.prod_id == 6400)
176	return TRUE;
177
178    return FALSE;
179}
180
181static Bool quirk_135_clock_too_high (int scrnIndex, xf86MonPtr DDC)
182{
183    /* Envision Peripherals, Inc. EN-7100e.  See bug #9550. */
184    if (memcmp (DDC->vendor.name, "EPI", 4) == 0 &&
185	DDC->vendor.prod_id == 59264)
186	return TRUE;
187
188    return FALSE;
189}
190
191static Bool quirk_first_detailed_preferred (int scrnIndex, xf86MonPtr DDC)
192{
193    /* Philips 107p5 CRT. Reported on xorg@ with pastebin. */
194    if (memcmp (DDC->vendor.name, "PHL", 4) == 0 &&
195	DDC->vendor.prod_id == 57364)
196	return TRUE;
197
198    /* Proview AY765C 17" LCD. See bug #15160*/
199    if (memcmp (DDC->vendor.name, "PTS", 4) == 0 &&
200	DDC->vendor.prod_id == 765)
201	return TRUE;
202
203    /* ACR of some sort RH #284231 */
204    if (memcmp (DDC->vendor.name, "ACR", 4) == 0 &&
205	DDC->vendor.prod_id == 2423)
206	return TRUE;
207
208    return FALSE;
209}
210
211static Bool quirk_detailed_sync_pp(int scrnIndex, xf86MonPtr DDC)
212{
213    /* Bug #12439: Samsung SyncMaster 205BW */
214    if (memcmp (DDC->vendor.name, "SAM", 4) == 0 &&
215	DDC->vendor.prod_id == 541)
216	return TRUE;
217    return FALSE;
218}
219
220/* This should probably be made more generic */
221static Bool quirk_dvi_single_link(int scrnIndex, xf86MonPtr DDC)
222{
223    /* Red Hat bug #453106: Apple 23" Cinema Display */
224    if (memcmp (DDC->vendor.name, "APL", 4) == 0 &&
225	DDC->vendor.prod_id == 0x921c)
226	return TRUE;
227    return FALSE;
228}
229
230typedef struct {
231    Bool	(*detect) (int scrnIndex, xf86MonPtr DDC);
232    ddc_quirk_t	quirk;
233    char	*description;
234} ddc_quirk_map_t;
235
236static const ddc_quirk_map_t ddc_quirks[] = {
237    {
238	quirk_prefer_large_60,   DDC_QUIRK_PREFER_LARGE_60,
239	"Detailed timing is not preferred, use largest mode at 60Hz"
240    },
241    {
242	quirk_135_clock_too_high,   DDC_QUIRK_135_CLOCK_TOO_HIGH,
243	"Recommended 135MHz pixel clock is too high"
244    },
245    {
246	quirk_prefer_large_75,   DDC_QUIRK_PREFER_LARGE_75,
247	"Detailed timing is not preferred, use largest mode at 75Hz"
248    },
249    {
250	quirk_detailed_h_in_cm,   DDC_QUIRK_DETAILED_H_IN_CM,
251	"Detailed timings give horizontal size in cm."
252    },
253    {
254	quirk_detailed_v_in_cm,   DDC_QUIRK_DETAILED_V_IN_CM,
255	"Detailed timings give vertical size in cm."
256    },
257    {
258	quirk_detailed_use_maximum_size,   DDC_QUIRK_DETAILED_USE_MAXIMUM_SIZE,
259	"Detailed timings give sizes in cm."
260    },
261    {
262	quirk_first_detailed_preferred, DDC_QUIRK_FIRST_DETAILED_PREFERRED,
263	"First detailed timing was not marked as preferred."
264    },
265    {
266	quirk_detailed_sync_pp, DDC_QUIRK_DETAILED_SYNC_PP,
267	"Use +hsync +vsync for detailed timing."
268    },
269    {
270	quirk_dvi_single_link, DDC_QUIRK_DVI_SINGLE_LINK,
271	"Forcing maximum pixel clock to single DVI link."
272    },
273    {
274	NULL,		DDC_QUIRK_NONE,
275	"No known quirks"
276    },
277};
278
279/*
280 * These more or less come from the DMT spec.  The 720x400 modes are
281 * inferred from historical 80x25 practice.  The 640x480@67 and 832x624@75
282 * modes are old-school Mac modes.  The EDID spec says the 1152x864@75 mode
283 * should be 1152x870, again for the Mac, but instead we use the x864 DMT
284 * mode.
285 *
286 * The DMT modes have been fact-checked; the rest are mild guesses.
287 */
288#define MODEPREFIX NULL, NULL, NULL, 0, M_T_DRIVER
289#define MODESUFFIX 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,FALSE,FALSE,0,NULL,0,0.0,0.0
290
291static const DisplayModeRec DDCEstablishedModes[17] = {
292    { MODEPREFIX,    40000,  800,  840,  968, 1056, 0,  600,  601,  605,  628, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@60Hz */
293    { MODEPREFIX,    36000,  800,  824,  896, 1024, 0,  600,  601,  603,  625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@56Hz */
294    { MODEPREFIX,    31500,  640,  656,  720,  840, 0,  480,  481,  484,  500, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@75Hz */
295    { MODEPREFIX,    31500,  640,  664,  704,  832, 0,  480,  489,  492,  520, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@72Hz */
296    { MODEPREFIX,    30240,  640,  704,  768,  864, 0,  480,  483,  486,  525, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@67Hz */
297    { MODEPREFIX,    25175,  640,  656,  752,  800, 0,  480,  490,  492,  525, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@60Hz */
298    { MODEPREFIX,    35500,  720,  738,  846,  900, 0,  400,  421,  423,  449, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 720x400@88Hz */
299    { MODEPREFIX,    28320,  720,  738,  846,  900, 0,  400,  412,  414,  449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 720x400@70Hz */
300    { MODEPREFIX,   135000, 1280, 1296, 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x1024@75Hz */
301    { MODEPREFIX,    78750, 1024, 1040, 1136, 1312, 0,  768,  769,  772,  800, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1024x768@75Hz */
302    { MODEPREFIX,    75000, 1024, 1048, 1184, 1328, 0,  768,  771,  777,  806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@70Hz */
303    { MODEPREFIX,    65000, 1024, 1048, 1184, 1344, 0,  768,  771,  777,  806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@60Hz */
304    { MODEPREFIX,    44900, 1024, 1032, 1208, 1264, 0,  768,  768,  772,  817, 0, V_PHSYNC | V_PVSYNC | V_INTERLACE, MODESUFFIX }, /* 1024x768@43Hz */
305    { MODEPREFIX,    57284,  832,  864,  928, 1152, 0,  624,  625,  628,  667, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 832x624@75Hz */
306    { MODEPREFIX,    49500,  800,  816,  896, 1056, 0,  600,  601,  604,  625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@75Hz */
307    { MODEPREFIX,    50000,  800,  856,  976, 1040, 0,  600,  637,  643,  666, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@72Hz */
308    { MODEPREFIX,   108000, 1152, 1216, 1344, 1600, 0,  864,  865,  868,  900, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1152x864@75Hz */
309};
310
311static DisplayModePtr
312DDCModesFromEstablished(int scrnIndex, struct established_timings *timing,
313			ddc_quirk_t quirks)
314{
315    DisplayModePtr Modes = NULL, Mode = NULL;
316    CARD32 bits = (timing->t1) | (timing->t2 << 8) |
317        ((timing->t_manu & 0x80) << 9);
318    int i;
319
320    for (i = 0; i < 17; i++) {
321        if (bits & (0x01 << i)) {
322            Mode = xf86DuplicateMode(&DDCEstablishedModes[i]);
323            Modes = xf86ModesAdd(Modes, Mode);
324        }
325    }
326
327    return Modes;
328}
329
330/* Autogenerated from the DMT spec */
331static const DisplayModeRec DMTModes[] = {
332    { MODEPREFIX,    31500,  640,  672,  736,  832, 0,  350,  382,  385,  445, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x350@85Hz */
333    { MODEPREFIX,    31500,  640,  672,  736,  832, 0,  400,  401,  404,  445, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 640x400@85Hz */
334    { MODEPREFIX,    35500,  720,  756,  828,  936, 0,  400,  401,  404,  446, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 720x400@85Hz */
335    { MODEPREFIX,    25175,  640,  656,  752,  800, 0,  480,  490,  492,  525, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@60Hz */
336    { MODEPREFIX,    31500,  640,  664,  704,  832, 0,  480,  489,  492,  520, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@72Hz */
337    { MODEPREFIX,    31500,  640,  656,  720,  840, 0,  480,  481,  484,  500, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@75Hz */
338    { MODEPREFIX,    36000,  640,  696,  752,  832, 0,  480,  481,  484,  509, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@85Hz */
339    { MODEPREFIX,    36000,  800,  824,  896, 1024, 0,  600,  601,  603,  625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@56Hz */
340    { MODEPREFIX,    40000,  800,  840,  968, 1056, 0,  600,  601,  605,  628, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@60Hz */
341    { MODEPREFIX,    50000,  800,  856,  976, 1040, 0,  600,  637,  643,  666, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@72Hz */
342    { MODEPREFIX,    49500,  800,  816,  896, 1056, 0,  600,  601,  604,  625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@75Hz */
343    { MODEPREFIX,    56250,  800,  832,  896, 1048, 0,  600,  601,  604,  631, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@85Hz */
344    { MODEPREFIX,    73250,  800,  848,  880,  960, 0,  600,  603,  607,  636, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 800x600@120Hz RB */
345    { MODEPREFIX,    33750,  848,  864,  976, 1088, 0,  480,  486,  494,  517, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 848x480@60Hz */
346    { MODEPREFIX,    44900, 1024, 1032, 1208, 1264, 0,  768,  768,  772,  817, 0, V_PHSYNC | V_PVSYNC | V_INTERLACE, MODESUFFIX }, /* 1024x768@43Hz (interlaced) */
347    { MODEPREFIX,    65000, 1024, 1048, 1184, 1344, 0,  768,  771,  777,  806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@60Hz */
348    { MODEPREFIX,    75000, 1024, 1048, 1184, 1328, 0,  768,  771,  777,  806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@70Hz */
349    { MODEPREFIX,    78750, 1024, 1040, 1136, 1312, 0,  768,  769,  772,  800, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1024x768@75Hz */
350    { MODEPREFIX,    94500, 1024, 1072, 1168, 1376, 0,  768,  769,  772,  808, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1024x768@85Hz */
351    { MODEPREFIX,   115500, 1024, 1072, 1104, 1184, 0,  768,  771,  775,  813, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@120Hz RB */
352    { MODEPREFIX,   108000, 1152, 1216, 1344, 1600, 0,  864,  865,  868,  900, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1152x864@75Hz */
353    { MODEPREFIX,    68250, 1280, 1328, 1360, 1440, 0,  768,  771,  778,  790, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x768@60Hz RB */
354    { MODEPREFIX,    79500, 1280, 1344, 1472, 1664, 0,  768,  771,  778,  798, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x768@60Hz */
355    { MODEPREFIX,   102250, 1280, 1360, 1488, 1696, 0,  768,  771,  778,  805, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x768@75Hz */
356    { MODEPREFIX,   117500, 1280, 1360, 1496, 1712, 0,  768,  771,  778,  809, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x768@85Hz */
357    { MODEPREFIX,   140250, 1280, 1328, 1360, 1440, 0,  768,  771,  778,  813, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x768@120Hz RB */
358    { MODEPREFIX,    71000, 1280, 1328, 1360, 1440, 0,  800,  803,  809,  823, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x800@60Hz RB */
359    { MODEPREFIX,    83500, 1280, 1352, 1480, 1680, 0,  800,  803,  809,  831, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x800@60Hz */
360    { MODEPREFIX,   106500, 1280, 1360, 1488, 1696, 0,  800,  803,  809,  838, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x800@75Hz */
361    { MODEPREFIX,   122500, 1280, 1360, 1496, 1712, 0,  800,  803,  809,  843, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x800@85Hz */
362    { MODEPREFIX,   146250, 1280, 1328, 1360, 1440, 0,  800,  803,  809,  847, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x800@120Hz RB */
363    { MODEPREFIX,   108000, 1280, 1376, 1488, 1800, 0,  960,  961,  964, 1000, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x960@60Hz */
364    { MODEPREFIX,   148500, 1280, 1344, 1504, 1728, 0,  960,  961,  964, 1011, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x960@85Hz */
365    { MODEPREFIX,   175500, 1280, 1328, 1360, 1440, 0,  960,  963,  967, 1017, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x960@120Hz RB */
366    { MODEPREFIX,   108000, 1280, 1328, 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x1024@60Hz */
367    { MODEPREFIX,   135000, 1280, 1296, 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x1024@75Hz */
368    { MODEPREFIX,   157500, 1280, 1344, 1504, 1728, 0, 1024, 1025, 1028, 1072, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x1024@85Hz */
369    { MODEPREFIX,   187250, 1280, 1328, 1360, 1440, 0, 1024, 1027, 1034, 1084, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1280x1024@120Hz RB */
370    { MODEPREFIX,    85500, 1360, 1424, 1536, 1792, 0,  768,  771,  777,  795, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1360x768@60Hz */
371    { MODEPREFIX,   148250, 1360, 1408, 1440, 1520, 0,  768,  771,  776,  813, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1360x768@120Hz RB */
372    { MODEPREFIX,   101000, 1400, 1448, 1480, 1560, 0, 1050, 1053, 1057, 1080, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1400x1050@60Hz RB */
373    { MODEPREFIX,   121750, 1400, 1488, 1632, 1864, 0, 1050, 1053, 1057, 1089, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1400x1050@60Hz */
374    { MODEPREFIX,   156000, 1400, 1504, 1648, 1896, 0, 1050, 1053, 1057, 1099, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1400x1050@75Hz */
375    { MODEPREFIX,   179500, 1400, 1504, 1656, 1912, 0, 1050, 1053, 1057, 1105, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1400x1050@85Hz */
376    { MODEPREFIX,   208000, 1400, 1448, 1480, 1560, 0, 1050, 1053, 1057, 1112, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1400x1050@120Hz RB */
377    { MODEPREFIX,    88750, 1440, 1488, 1520, 1600, 0,  900,  903,  909,  926, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1440x900@60Hz RB */
378    { MODEPREFIX,   106500, 1440, 1520, 1672, 1904, 0,  900,  903,  909,  934, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1440x900@60Hz */
379    { MODEPREFIX,   136750, 1440, 1536, 1688, 1936, 0,  900,  903,  909,  942, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1440x900@75Hz */
380    { MODEPREFIX,   157000, 1440, 1544, 1696, 1952, 0,  900,  903,  909,  948, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1440x900@85Hz */
381    { MODEPREFIX,   182750, 1440, 1488, 1520, 1600, 0,  900,  903,  909,  953, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1440x900@120Hz RB */
382    { MODEPREFIX,   162000, 1600, 1664, 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1600x1200@60Hz */
383    { MODEPREFIX,   175500, 1600, 1664, 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1600x1200@65Hz */
384    { MODEPREFIX,   189000, 1600, 1664, 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1600x1200@70Hz */
385    { MODEPREFIX,   202500, 1600, 1664, 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1600x1200@75Hz */
386    { MODEPREFIX,   229500, 1600, 1664, 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1600x1200@85Hz */
387    { MODEPREFIX,   268250, 1600, 1648, 1680, 1760, 0, 1200, 1203, 1207, 1271, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1600x1200@120Hz RB */
388    { MODEPREFIX,   119000, 1680, 1728, 1760, 1840, 0, 1050, 1053, 1059, 1080, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1680x1050@60Hz RB */
389    { MODEPREFIX,   146250, 1680, 1784, 1960, 2240, 0, 1050, 1053, 1059, 1089, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1680x1050@60Hz */
390    { MODEPREFIX,   187000, 1680, 1800, 1976, 2272, 0, 1050, 1053, 1059, 1099, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1680x1050@75Hz */
391    { MODEPREFIX,   214750, 1680, 1808, 1984, 2288, 0, 1050, 1053, 1059, 1105, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1680x1050@85Hz */
392    { MODEPREFIX,   245500, 1680, 1728, 1760, 1840, 0, 1050, 1053, 1059, 1112, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1680x1050@120Hz RB */
393    { MODEPREFIX,   204750, 1792, 1920, 2120, 2448, 0, 1344, 1345, 1348, 1394, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1792x1344@60Hz */
394    { MODEPREFIX,   261000, 1792, 1888, 2104, 2456, 0, 1344, 1345, 1348, 1417, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1792x1344@75Hz */
395    { MODEPREFIX,   333250, 1792, 1840, 1872, 1952, 0, 1344, 1347, 1351, 1423, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1792x1344@120Hz RB */
396    { MODEPREFIX,   218250, 1856, 1952, 2176, 2528, 0, 1392, 1393, 1396, 1439, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1856x1392@60Hz */
397    { MODEPREFIX,   288000, 1856, 1984, 2208, 2560, 0, 1392, 1393, 1396, 1500, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1856x1392@75Hz */
398    { MODEPREFIX,   356500, 1856, 1904, 1936, 2016, 0, 1392, 1395, 1399, 1474, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1856x1392@120Hz RB */
399    { MODEPREFIX,   154000, 1920, 1968, 2000, 2080, 0, 1200, 1203, 1209, 1235, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1920x1200@60Hz RB */
400    { MODEPREFIX,   193250, 1920, 2056, 2256, 2592, 0, 1200, 1203, 1209, 1245, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1920x1200@60Hz */
401    { MODEPREFIX,   245250, 1920, 2056, 2264, 2608, 0, 1200, 1203, 1209, 1255, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1920x1200@75Hz */
402    { MODEPREFIX,   281250, 1920, 2064, 2272, 2624, 0, 1200, 1203, 1209, 1262, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1920x1200@85Hz */
403    { MODEPREFIX,   317000, 1920, 1968, 2000, 2080, 0, 1200, 1203, 1209, 1271, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1920x1200@120Hz RB */
404    { MODEPREFIX,   234000, 1920, 2048, 2256, 2600, 0, 1440, 1441, 1444, 1500, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1920x1440@60Hz */
405    { MODEPREFIX,   297000, 1920, 2064, 2288, 2640, 0, 1440, 1441, 1444, 1500, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 1920x1440@75Hz */
406    { MODEPREFIX,   380500, 1920, 1968, 2000, 2080, 0, 1440, 1443, 1447, 1525, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 1920x1440@120Hz RB */
407    { MODEPREFIX,   268500, 2560, 2608, 2640, 2720, 0, 1600, 1603, 1609, 1646, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 2560x1600@60Hz RB */
408    { MODEPREFIX,   348500, 2560, 2752, 3032, 3504, 0, 1600, 1603, 1609, 1658, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 2560x1600@60Hz */
409    { MODEPREFIX,   443250, 2560, 2768, 3048, 3536, 0, 1600, 1603, 1609, 1672, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 2560x1600@75Hz */
410    { MODEPREFIX,   505250, 2560, 2768, 3048, 3536, 0, 1600, 1603, 1609, 1682, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 2560x1600@85Hz */
411    { MODEPREFIX,   552750, 2560, 2608, 2640, 2720, 0, 1600, 1603, 1609, 1694, 0, V_PHSYNC | V_NVSYNC, MODESUFFIX }, /* 2560x1600@120Hz RB */
412};
413
414#define LEVEL_DMT 0
415#define LEVEL_GTF 1
416#define LEVEL_CVT 2
417
418static int
419MonitorStandardTimingLevel(xf86MonPtr DDC)
420{
421    if (DDC->ver.revision >= 2) {
422	if (DDC->ver.revision >= 4 && CVT_SUPPORTED(DDC->features.msc)) {
423	    return LEVEL_CVT;
424	}
425	return LEVEL_GTF;
426    }
427    return LEVEL_DMT;
428}
429
430static int
431ModeRefresh(const DisplayModeRec *mode)
432{
433    return (int)(xf86ModeVRefresh(mode) + 0.5);
434}
435
436/*
437 * If rb is not set, then we'll not consider reduced-blanking modes as
438 * part of the DMT pool.  For the 'standard' EDID mode descriptor there's
439 * no way to specify whether the mode should be RB or not.
440 */
441static DisplayModePtr
442FindDMTMode(int hsize, int vsize, int refresh, Bool rb)
443{
444    int i;
445    const DisplayModeRec *ret;
446
447    for (i = 0; i < sizeof(DMTModes) / sizeof(DisplayModeRec); i++) {
448	ret = &DMTModes[i];
449
450	if (!rb && xf86ModeIsReduced(ret))
451	    continue;
452
453	if (ret->HDisplay == hsize &&
454	    ret->VDisplay == vsize &&
455	    refresh == ModeRefresh(ret))
456	    return xf86DuplicateMode(ret);
457    }
458
459    return NULL;
460}
461
462/*
463 * Appendix B of the EDID 1.4 spec defines the right thing to do here.
464 * If the timing given here matches a mode defined in the VESA DMT standard,
465 * we _must_ use that.  If the device supports CVT modes, then we should
466 * generate a CVT timing.  If both of the above fail, use GTF.
467 *
468 * There are some wrinkles here.  EDID 1.1 and 1.0 sinks can't really
469 * "support" GTF, since it wasn't a standard yet; so if they ask for a
470 * timing in this section that isn't defined in DMT, returning a GTF mode
471 * may not actually be valid.  EDID 1.3 sinks often report support for
472 * some CVT modes, but they are not required to support CVT timings for
473 * modes in the standard timing descriptor, so we should _not_ treat them
474 * as CVT-compliant (unless specified in an extension block I suppose).
475 *
476 * EDID 1.4 requires that all sink devices support both GTF and CVT timings
477 * for modes in this section, but does say that CVT is preferred.
478 */
479static DisplayModePtr
480DDCModesFromStandardTiming(struct std_timings *timing, ddc_quirk_t quirks,
481			   int timing_level, Bool rb)
482{
483    DisplayModePtr Modes = NULL, Mode = NULL;
484    int i;
485
486    for (i = 0; i < STD_TIMINGS; i++) {
487        if (timing[i].hsize && timing[i].vsize && timing[i].refresh) {
488	    Mode = FindDMTMode(timing[i].hsize, timing[i].vsize,
489			       timing[i].refresh, rb);
490
491	    if (!Mode) {
492		if (timing_level == LEVEL_CVT)
493		    /* pass rb here too? */
494		    Mode = xf86CVTMode(timing[i].hsize, timing[i].vsize,
495				       timing[i].refresh, FALSE, FALSE);
496		else if (timing_level == LEVEL_GTF)
497		    Mode = xf86GTFMode(timing[i].hsize, timing[i].vsize,
498				       timing[i].refresh, FALSE, FALSE);
499	    }
500
501	    if (!Mode)
502		continue;
503
504	    Mode->type = M_T_DRIVER;
505            Modes = xf86ModesAdd(Modes, Mode);
506        }
507    }
508
509    return Modes;
510}
511
512/*
513 *
514 */
515static DisplayModePtr
516DDCModeFromDetailedTiming(int scrnIndex, struct detailed_timings *timing,
517			  Bool preferred, ddc_quirk_t quirks)
518{
519    DisplayModePtr Mode;
520
521    /*
522     * Refuse to create modes that are insufficiently large.  64 is a random
523     * number, maybe the spec says something about what the minimum is.  In
524     * particular I see this frequently with _old_ EDID, 1.0 or so, so maybe
525     * our parser is just being too aggresive there.
526     */
527    if (timing->h_active < 64 || timing->v_active < 64) {
528	xf86DrvMsg(scrnIndex, X_INFO,
529		   "%s: Ignoring tiny %dx%d mode\n", __func__,
530		   timing->h_active, timing->v_active);
531	return NULL;
532    }
533
534    /* We don't do stereo */
535    if (timing->stereo) {
536        xf86DrvMsg(scrnIndex, X_INFO,
537		   "%s: Ignoring: We don't handle stereo.\n", __func__);
538        return NULL;
539    }
540
541    /* We only do seperate sync currently */
542    if (timing->sync != 0x03) {
543         xf86DrvMsg(scrnIndex, X_INFO,
544		    "%s: %dx%d Warning: We only handle separate"
545                    " sync.\n", __func__, timing->h_active, timing->v_active);
546    }
547
548    Mode = xnfcalloc(1, sizeof(DisplayModeRec));
549
550    Mode->type = M_T_DRIVER;
551    if (preferred)
552	Mode->type |= M_T_PREFERRED;
553
554    if( ( quirks & DDC_QUIRK_135_CLOCK_TOO_HIGH ) &&
555	timing->clock == 135000000 )
556        Mode->Clock = 108880;
557    else
558        Mode->Clock = timing->clock / 1000.0;
559
560    Mode->HDisplay = timing->h_active;
561    Mode->HSyncStart = timing->h_active + timing->h_sync_off;
562    Mode->HSyncEnd = Mode->HSyncStart + timing->h_sync_width;
563    Mode->HTotal = timing->h_active + timing->h_blanking;
564
565    Mode->VDisplay = timing->v_active;
566    Mode->VSyncStart = timing->v_active + timing->v_sync_off;
567    Mode->VSyncEnd = Mode->VSyncStart + timing->v_sync_width;
568    Mode->VTotal = timing->v_active + timing->v_blanking;
569
570    /* perform basic check on the detail timing */
571    if (Mode->HSyncEnd > Mode->HTotal || Mode->VSyncEnd > Mode->VTotal) {
572	xfree(Mode);
573	return NULL;
574    }
575
576    xf86SetModeDefaultName(Mode);
577
578    /* We ignore h/v_size and h/v_border for now. */
579
580    if (timing->interlaced)
581        Mode->Flags |= V_INTERLACE;
582
583    if (quirks & DDC_QUIRK_DETAILED_SYNC_PP)
584	Mode->Flags |= V_PVSYNC | V_PHSYNC;
585    else {
586	if (timing->misc & 0x02)
587	    Mode->Flags |= V_PVSYNC;
588	else
589	    Mode->Flags |= V_NVSYNC;
590
591	if (timing->misc & 0x01)
592	    Mode->Flags |= V_PHSYNC;
593	else
594	    Mode->Flags |= V_NHSYNC;
595    }
596
597    return Mode;
598}
599
600#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(7,0,0,0,0)
601static DisplayModePtr
602DDCModesFromCVT(int scrnIndex, struct cvt_timings *t)
603{
604    DisplayModePtr modes = NULL;
605    int i;
606
607    for (i = 0; i < 4; i++) {
608	if (t[i].height) {
609	    if (t[i].rates & 0x10)
610		modes = xf86ModesAdd(modes,
611			xf86CVTMode(t[i].width, t[i].height, 50, 0, 0));
612	    if (t[i].rates & 0x08)
613		modes = xf86ModesAdd(modes,
614			xf86CVTMode(t[i].width, t[i].height, 60, 0, 0));
615	    if (t[i].rates & 0x04)
616		modes = xf86ModesAdd(modes,
617			xf86CVTMode(t[i].width, t[i].height, 75, 0, 0));
618	    if (t[i].rates & 0x02)
619		modes = xf86ModesAdd(modes,
620			xf86CVTMode(t[i].width, t[i].height, 85, 0, 0));
621	    if (t[i].rates & 0x01)
622		modes = xf86ModesAdd(modes,
623			xf86CVTMode(t[i].width, t[i].height, 60, 1, 0));
624	} else break;
625    }
626
627    return modes;
628}
629#endif
630
631
632/*
633 * This is only valid when the sink claims to be continuous-frequency
634 * but does not supply a detailed range descriptor.  Such sinks are
635 * arguably broken.  Currently the mode validation code isn't aware of
636 * this; the non-RANDR code even punts the decision of optional sync
637 * range checking to the driver.  Loss.
638 */
639static void
640DDCGuessRangesFromModes(int scrnIndex, MonPtr Monitor, DisplayModePtr Modes)
641{
642    DisplayModePtr Mode = Modes;
643
644    if (!Monitor || !Modes)
645        return;
646
647    /* set up the ranges for scanning through the modes */
648    Monitor->nHsync = 1;
649    Monitor->hsync[0].lo = 1024.0;
650    Monitor->hsync[0].hi = 0.0;
651
652    Monitor->nVrefresh = 1;
653    Monitor->vrefresh[0].lo = 1024.0;
654    Monitor->vrefresh[0].hi = 0.0;
655
656    while (Mode) {
657        if (!Mode->HSync)
658            Mode->HSync = ((float) Mode->Clock ) / ((float) Mode->HTotal);
659
660        if (!Mode->VRefresh)
661            Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
662                ((float) (Mode->HTotal * Mode->VTotal));
663
664        if (Mode->HSync < Monitor->hsync[0].lo)
665            Monitor->hsync[0].lo = Mode->HSync;
666
667        if (Mode->HSync > Monitor->hsync[0].hi)
668            Monitor->hsync[0].hi = Mode->HSync;
669
670        if (Mode->VRefresh < Monitor->vrefresh[0].lo)
671            Monitor->vrefresh[0].lo = Mode->VRefresh;
672
673        if (Mode->VRefresh > Monitor->vrefresh[0].hi)
674            Monitor->vrefresh[0].hi = Mode->VRefresh;
675
676        Mode = Mode->next;
677    }
678}
679
680static ddc_quirk_t
681xf86DDCDetectQuirks(int scrnIndex, xf86MonPtr DDC, Bool verbose)
682{
683    ddc_quirk_t	quirks;
684    int i;
685
686    quirks = DDC_QUIRK_NONE;
687    for (i = 0; ddc_quirks[i].detect; i++) {
688	if (ddc_quirks[i].detect (scrnIndex, DDC)) {
689	    if (verbose) {
690		xf86DrvMsg (scrnIndex, X_INFO, "    EDID quirk: %s\n",
691			    ddc_quirks[i].description);
692	    }
693	    quirks |= ddc_quirks[i].quirk;
694	}
695    }
696
697    return quirks;
698}
699
700/**
701 * Applies monitor-specific quirks to the decoded EDID information.
702 *
703 * Note that some quirks applying to the mode list are still implemented in
704 * xf86DDCGetModes.
705 */
706void
707xf86DDCApplyQuirks(int scrnIndex, xf86MonPtr DDC)
708{
709    ddc_quirk_t quirks = xf86DDCDetectQuirks (scrnIndex, DDC, FALSE);
710    int i;
711
712    for (i = 0; i < DET_TIMINGS; i++) {
713	struct detailed_monitor_section *det_mon = &DDC->det_mon[i];
714
715	if (det_mon->type != DT)
716	    continue;
717
718	if (quirks & DDC_QUIRK_DETAILED_H_IN_CM)
719	    det_mon->section.d_timings.h_size *= 10;
720
721	if (quirks & DDC_QUIRK_DETAILED_V_IN_CM)
722	    det_mon->section.d_timings.v_size *= 10;
723
724	if (quirks & DDC_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
725	    det_mon->section.d_timings.h_size = 10 * DDC->features.hsize;
726	    det_mon->section.d_timings.v_size = 10 * DDC->features.vsize;
727	}
728    }
729}
730
731/**
732 * Walks the modes list, finding the mode with the largest area which is
733 * closest to the target refresh rate, and marks it as the only preferred mode.
734*/
735static void
736xf86DDCSetPreferredRefresh(int scrnIndex, DisplayModePtr modes,
737			   float target_refresh)
738{
739	DisplayModePtr	mode, best = modes;
740
741	for (mode = modes; mode; mode = mode->next)
742	{
743	    mode->type &= ~M_T_PREFERRED;
744
745	    if (mode == best) continue;
746
747	    if (mode->HDisplay * mode->VDisplay >
748		best->HDisplay * best->VDisplay)
749	    {
750		best = mode;
751		continue;
752	    }
753	    if (mode->HDisplay * mode->VDisplay ==
754		best->HDisplay * best->VDisplay)
755	    {
756		double	mode_refresh = xf86ModeVRefresh (mode);
757		double	best_refresh = xf86ModeVRefresh (best);
758		double	mode_dist = fabs(mode_refresh - target_refresh);
759		double	best_dist = fabs(best_refresh - target_refresh);
760
761		if (mode_dist < best_dist)
762		{
763		    best = mode;
764		    continue;
765		}
766	    }
767	}
768	if (best)
769	    best->type |= M_T_PREFERRED;
770}
771
772_X_EXPORT DisplayModePtr
773xf86DDCGetModes(int scrnIndex, xf86MonPtr DDC)
774{
775    int		    i;
776    DisplayModePtr  Modes = NULL, Mode;
777    ddc_quirk_t	    quirks;
778    Bool	    preferred, rb;
779    int		    timing_level;
780
781    xf86DrvMsg (scrnIndex, X_INFO, "EDID vendor \"%s\", prod id %d\n",
782		DDC->vendor.name, DDC->vendor.prod_id);
783
784    quirks = xf86DDCDetectQuirks(scrnIndex, DDC, TRUE);
785
786    preferred = PREFERRED_TIMING_MODE(DDC->features.msc);
787    if (DDC->ver.revision >= 4)
788	preferred = TRUE;
789    if (quirks & DDC_QUIRK_FIRST_DETAILED_PREFERRED)
790	preferred = TRUE;
791    if (quirks & (DDC_QUIRK_PREFER_LARGE_60 | DDC_QUIRK_PREFER_LARGE_75))
792	preferred = FALSE;
793
794    rb = xf86MonitorSupportsReducedBlanking(DDC);
795
796    timing_level = MonitorStandardTimingLevel(DDC);
797
798    for (i = 0; i < DET_TIMINGS; i++) {
799	struct detailed_monitor_section *det_mon = &DDC->det_mon[i];
800
801        switch (det_mon->type) {
802        case DT:
803            Mode = DDCModeFromDetailedTiming(scrnIndex,
804                                             &det_mon->section.d_timings,
805					     preferred,
806					     quirks);
807	    preferred = FALSE;
808            Modes = xf86ModesAdd(Modes, Mode);
809            break;
810        case DS_STD_TIMINGS:
811            Mode = DDCModesFromStandardTiming(det_mon->section.std_t,
812					      quirks, timing_level, rb);
813            Modes = xf86ModesAdd(Modes, Mode);
814            break;
815#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(7,0,0,0,0)
816	case DS_CVT:
817	    Mode = DDCModesFromCVT(scrnIndex, det_mon->section.cvt);
818	    Modes = xf86ModesAdd(Modes, Mode);
819	    break;
820#endif
821        default:
822            break;
823        }
824    }
825
826    /* Add established timings */
827    Mode = DDCModesFromEstablished(scrnIndex, &DDC->timings1, quirks);
828    Modes = xf86ModesAdd(Modes, Mode);
829
830    /* Add standard timings */
831    Mode = DDCModesFromStandardTiming(DDC->timings2, quirks, timing_level, rb);
832    Modes = xf86ModesAdd(Modes, Mode);
833
834    if (quirks & DDC_QUIRK_PREFER_LARGE_60)
835	xf86DDCSetPreferredRefresh(scrnIndex, Modes, 60);
836
837    if (quirks & DDC_QUIRK_PREFER_LARGE_75)
838	xf86DDCSetPreferredRefresh(scrnIndex, Modes, 75);
839
840    return Modes;
841}
842
843/*
844 * Fill out MonPtr with xf86MonPtr information.
845 */
846_X_EXPORT void
847xf86DDCMonitorSet(int scrnIndex, MonPtr Monitor, xf86MonPtr DDC)
848{
849    DisplayModePtr Modes = NULL, Mode;
850    int i, clock;
851    Bool have_hsync = FALSE, have_vrefresh = FALSE, have_maxpixclock = FALSE;
852    ddc_quirk_t quirks;
853
854    if (!Monitor || !DDC)
855        return;
856
857    Monitor->DDC = DDC;
858
859    quirks = xf86DDCDetectQuirks(scrnIndex, DDC, FALSE);
860
861    if (Monitor->widthmm <= 0 && Monitor->heightmm <= 0) {
862	Monitor->widthmm = 10 * DDC->features.hsize;
863	Monitor->heightmm = 10 * DDC->features.vsize;
864    }
865
866    Monitor->reducedblanking = xf86MonitorSupportsReducedBlanking(DDC);
867
868    Modes = xf86DDCGetModes(scrnIndex, DDC);
869
870    /* Skip EDID ranges if they were specified in the config file */
871    have_hsync = (Monitor->nHsync != 0);
872    have_vrefresh = (Monitor->nVrefresh != 0);
873    have_maxpixclock = (Monitor->maxPixClock != 0);
874
875    /* Go through the detailed monitor sections */
876    for (i = 0; i < DET_TIMINGS; i++) {
877        switch (DDC->det_mon[i].type) {
878        case DS_RANGES:
879	    if (!have_hsync) {
880		if (!Monitor->nHsync)
881		    xf86DrvMsg(scrnIndex, X_INFO,
882			    "Using EDID range info for horizontal sync\n");
883		Monitor->hsync[Monitor->nHsync].lo =
884		    DDC->det_mon[i].section.ranges.min_h;
885		Monitor->hsync[Monitor->nHsync].hi =
886		    DDC->det_mon[i].section.ranges.max_h;
887		Monitor->nHsync++;
888	    } else {
889		xf86DrvMsg(scrnIndex, X_INFO,
890			"Using hsync ranges from config file\n");
891	    }
892
893	    if (!have_vrefresh) {
894		if (!Monitor->nVrefresh)
895		    xf86DrvMsg(scrnIndex, X_INFO,
896			    "Using EDID range info for vertical refresh\n");
897		Monitor->vrefresh[Monitor->nVrefresh].lo =
898		    DDC->det_mon[i].section.ranges.min_v;
899		Monitor->vrefresh[Monitor->nVrefresh].hi =
900		    DDC->det_mon[i].section.ranges.max_v;
901		Monitor->nVrefresh++;
902	    } else {
903		xf86DrvMsg(scrnIndex, X_INFO,
904			"Using vrefresh ranges from config file\n");
905	    }
906
907	    clock = DDC->det_mon[i].section.ranges.max_clock * 1000;
908	    if (quirks & DDC_QUIRK_DVI_SINGLE_LINK)
909		clock = min(clock, 165000);
910	    if (!have_maxpixclock && clock > Monitor->maxPixClock)
911		Monitor->maxPixClock = clock;
912
913            break;
914        default:
915            break;
916        }
917    }
918
919    if (Modes) {
920        /* Print Modes */
921        xf86DrvMsg(scrnIndex, X_INFO, "Printing DDC gathered Modelines:\n");
922
923        Mode = Modes;
924        while (Mode) {
925            xf86PrintModeline(scrnIndex, Mode);
926            Mode = Mode->next;
927        }
928
929        /* Do we still need ranges to be filled in? */
930        if (!Monitor->nHsync || !Monitor->nVrefresh)
931            DDCGuessRangesFromModes(scrnIndex, Monitor, Modes);
932
933        /* look for last Mode */
934        Mode = Modes;
935
936        while (Mode->next)
937            Mode = Mode->next;
938
939        /* add to MonPtr */
940        if (Monitor->Modes) {
941            Monitor->Last->next = Modes;
942            Modes->prev = Monitor->Last;
943            Monitor->Last = Mode;
944        } else {
945            Monitor->Modes = Modes;
946            Monitor->Last = Mode;
947        }
948    }
949}
950