3 * Copyright 1998,1999 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
7 * A note on terminology. DDC1 is the original dumb serial protocol, and
8 * can only do up to 128 bytes of EDID. DDC2 is I2C-encapsulated and
9 * introduces extension blocks. EDID is the old display identification
10 * block, DisplayID is the new one.
13 #ifdef HAVE_XORG_CONFIG_H
14 #include <xorg-config.h>
19 #include "xf86_OSproc.h"
31 static const OptionInfoRec DDCOptions
[] = {
32 {DDCOPT_NODDC1
, "NoDDC1", OPTV_BOOLEAN
, {0}, FALSE
},
33 {DDCOPT_NODDC2
, "NoDDC2", OPTV_BOOLEAN
, {0}, FALSE
},
34 {DDCOPT_NODDC
, "NoDDC", OPTV_BOOLEAN
, {0}, FALSE
},
35 {-1, NULL
, OPTV_NONE
, {0}, FALSE
},
41 find_start(unsigned int *ptr
)
43 unsigned int comp
[9], test
[9];
46 for (i
= 0; i
< 9; i
++) {
50 for (i
= 0; i
< 127; i
++) {
51 for (j
= 0; j
< 9; j
++) {
52 test
[j
] = test
[j
] & !(comp
[j
] ^ *(ptr
++));
55 for (i
= 0; i
< 9; i
++)
61 static unsigned char *
62 find_header(unsigned char *block
)
64 unsigned char *ptr
, *head_ptr
, *end
;
65 unsigned char header
[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
68 end
= block
+ EDID1_LEN
;
73 for (i
= 0; i
< 8; i
++) {
74 if (header
[i
] != *(head_ptr
++))
88 static unsigned char *
89 resort(unsigned char *s_block
)
91 unsigned char *d_new
, *d_ptr
, *d_end
, *s_ptr
, *s_end
;
94 s_ptr
= find_header(s_block
);
97 s_end
= s_block
+ EDID1_LEN
;
99 d_new
= malloc(EDID1_LEN
);
102 d_end
= d_new
+ EDID1_LEN
;
104 for (d_ptr
= d_new
; d_ptr
< d_end
; d_ptr
++) {
115 DDC_checksum(const unsigned char *block
, int len
)
120 for (i
= 0; i
< len
; i
++) {
121 not_null
|= block
[i
];
127 ErrorF("DDC checksum not correct\n");
129 ErrorF("DDC read all Null\n");
132 /* catch the trivial case where all bytes are 0 */
136 return result
& 0xFF;
139 static unsigned char *
140 GetEDID_DDC1(unsigned int *s_ptr
)
142 unsigned char *d_block
, *d_pos
;
143 unsigned int *s_pos
, *s_end
;
147 s_start
= find_start(s_ptr
);
151 s_pos
= s_ptr
+ s_start
;
152 d_block
= malloc(EDID1_LEN
);
156 for (i
= 0; i
< EDID1_LEN
; i
++) {
157 for (j
= 0; j
< 8; j
++) {
172 if (d_block
&& DDC_checksum(d_block
, EDID1_LEN
)) {
176 return (resort(d_block
));
179 /* fetch entire EDID record; DDC bit needs to be masked */
180 static unsigned int *
181 FetchEDID_DDC1(register ScrnInfoPtr pScrn
,
182 register unsigned int (*read_DDC
) (ScrnInfoPtr
))
185 unsigned int *ptr
, *xp
;
187 ptr
= xp
= malloc(sizeof(int) * NUM
);
192 /* wait for next retrace */
193 *xp
= read_DDC(pScrn
);
199 /* test if DDC1 return 0 if not */
201 TestDDC1(ScrnInfoPtr pScrn
, unsigned int (*read_DDC
) (ScrnInfoPtr
))
205 old
= read_DDC(pScrn
);
206 count
= HEADER
* BITS_PER_BYTE
;
208 /* wait for next retrace */
209 if (old
!= read_DDC(pScrn
))
216 * read EDID record , pass it to callback function to interpret.
217 * callback function will store it for further use by calling
218 * function; it will also decide if we need to reread it
220 static unsigned char *
221 EDIDRead_DDC1(ScrnInfoPtr pScrn
, DDC1SetSpeedProc DDCSpeed
,
222 unsigned int (*read_DDC
) (ScrnInfoPtr
))
224 unsigned char *EDID_block
= NULL
;
228 xf86DrvMsg(pScrn
->scrnIndex
, X_PROBED
,
229 "chipset doesn't support DDC1\n");
233 if (TestDDC1(pScrn
, read_DDC
) == -1) {
234 xf86DrvMsg(pScrn
->scrnIndex
, X_PROBED
, "No DDC signal\n");
239 DDCSpeed(pScrn
, DDC_FAST
);
241 EDID_block
= GetEDID_DDC1(FetchEDID_DDC1(pScrn
, read_DDC
));
243 } while (!EDID_block
&& count
);
245 DDCSpeed(pScrn
, DDC_SLOW
);
251 * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC1 are
252 * unset. EDID information blocks are interpreted and the results returned in
255 * This function does not affect the list of modes used by drivers -- it is up
256 * to the driver to decide policy on what to do with EDID information.
258 * @return pointer to a new xf86MonPtr containing the EDID information.
259 * @return NULL if no monitor attached or failure to interpret the EDID.
262 xf86DoEDID_DDC1(ScrnInfoPtr pScrn
, DDC1SetSpeedProc DDC1SetSpeed
,
263 unsigned int (*DDC1Read
) (ScrnInfoPtr
))
265 unsigned char *EDID_block
= NULL
;
266 xf86MonPtr tmp
= NULL
;
268 /* Default DDC and DDC1 to enabled. */
269 Bool noddc
= FALSE
, noddc1
= FALSE
;
270 OptionInfoPtr options
;
272 options
= xnfalloc(sizeof(DDCOptions
));
273 (void) memcpy(options
, DDCOptions
, sizeof(DDCOptions
));
274 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, options
);
276 xf86GetOptValBool(options
, DDCOPT_NODDC
, &noddc
);
277 xf86GetOptValBool(options
, DDCOPT_NODDC1
, &noddc1
);
284 EDID_block
= EDIDRead_DDC1(pScrn
, DDC1SetSpeed
, DDC1Read
);
288 tmp
= xf86InterpretEDID(pScrn
->scrnIndex
, EDID_block
);
292 ErrorF("No EDID block returned\n");
294 ErrorF("Cannot interpret EDID block\n");
302 DDC2MakeDevice(I2CBusPtr pBus
, int address
, char *name
)
304 I2CDevPtr dev
= NULL
;
306 if (!(dev
= xf86I2CFindDev(pBus
, address
))) {
307 dev
= xf86CreateI2CDevRec();
309 dev
->SlaveAddr
= address
;
310 dev
->ByteTimeout
= 2200; /* VESA DDC spec 3 p. 43 (+10 %) */
311 dev
->StartTimeout
= 550;
312 dev
->BitTimeout
= 40;
313 dev
->AcknTimeout
= 40;
316 if (!xf86I2CDevInit(dev
)) {
317 xf86DrvMsg(pBus
->scrnIndex
, X_PROBED
, "No DDC2 device\n");
326 DDC2Init(I2CBusPtr pBus
)
328 I2CDevPtr dev
= NULL
;
331 * Slow down the bus so that older monitors don't
334 pBus
->RiseFallTime
= 20;
336 dev
= DDC2MakeDevice(pBus
, 0x00A0, "ddc2");
337 if (xf86I2CProbeAddress(pBus
, 0x0060))
338 DDC2MakeDevice(pBus
, 0x0060, "E-EDID segment register");
343 /* Mmmm, smell the hacks */
345 EEDIDStop(I2CDevPtr d
)
349 /* block is the EDID block number. a segment is two blocks. */
351 DDC2Read(I2CDevPtr dev
, int block
, unsigned char *R_Buffer
)
353 unsigned char W_Buffer
[1];
356 void (*stop
) (I2CDevPtr
);
358 for (i
= 0; i
< RETRIES
; i
++) {
359 /* Stop bits reset the segment pointer to 0, so be careful here. */
360 segment
= block
>> 1;
364 if (!(seg
= xf86I2CFindDev(dev
->pI2CBus
, 0x0060)))
367 W_Buffer
[0] = segment
;
369 stop
= dev
->pI2CBus
->I2CStop
;
370 dev
->pI2CBus
->I2CStop
= EEDIDStop
;
372 b
= xf86I2CWriteRead(seg
, W_Buffer
, 1, NULL
, 0);
374 dev
->pI2CBus
->I2CStop
= stop
;
376 dev
->pI2CBus
->I2CStop(dev
);
381 W_Buffer
[0] = (block
& 0x01) * EDID1_LEN
;
383 if (xf86I2CWriteRead(dev
, W_Buffer
, 1, R_Buffer
, EDID1_LEN
)) {
384 if (!DDC_checksum(R_Buffer
, EDID1_LEN
))
393 * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
394 * unset. EDID information blocks are interpreted and the results returned in
395 * an xf86MonPtr. Unlike xf86DoEDID_DDC[12](), this function will return
396 * the complete EDID data, including all extension blocks, if the 'complete'
399 * This function does not affect the list of modes used by drivers -- it is up
400 * to the driver to decide policy on what to do with EDID information.
402 * @return pointer to a new xf86MonPtr containing the EDID information.
403 * @return NULL if no monitor attached or failure to interpret the EDID.
406 xf86DoEEDID(ScrnInfoPtr pScrn
, I2CBusPtr pBus
, Bool complete
)
408 unsigned char *EDID_block
= NULL
;
409 xf86MonPtr tmp
= NULL
;
410 I2CDevPtr dev
= NULL
;
412 /* Default DDC and DDC2 to enabled. */
413 Bool noddc
= FALSE
, noddc2
= FALSE
;
414 OptionInfoPtr options
;
416 options
= malloc(sizeof(DDCOptions
));
419 memcpy(options
, DDCOptions
, sizeof(DDCOptions
));
420 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, options
);
422 xf86GetOptValBool(options
, DDCOPT_NODDC
, &noddc
);
423 xf86GetOptValBool(options
, DDCOPT_NODDC2
, &noddc2
);
429 if (!(dev
= DDC2Init(pBus
)))
432 EDID_block
= calloc(1, EDID1_LEN
);
436 if (DDC2Read(dev
, 0, EDID_block
)) {
437 int i
, n
= EDID_block
[0x7e];
440 EDID_block
= realloc(EDID_block
, EDID1_LEN
* (1 + n
));
442 for (i
= 0; i
< n
; i
++)
443 DDC2Read(dev
, i
+ 1, EDID_block
+ (EDID1_LEN
* (1 + i
)));
446 tmp
= xf86InterpretEEDID(pScrn
->scrnIndex
, EDID_block
);
450 tmp
->flags
|= MONITOR_EDID_COMPLETE_RAWDATA
;
456 * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
457 * unset. EDID information blocks are interpreted and the results returned in
460 * This function does not affect the list of modes used by drivers -- it is up
461 * to the driver to decide policy on what to do with EDID information.
463 * @return pointer to a new xf86MonPtr containing the EDID information.
464 * @return NULL if no monitor attached or failure to interpret the EDID.
467 xf86DoEDID_DDC2(ScrnInfoPtr pScrn
, I2CBusPtr pBus
)
469 return xf86DoEEDID(pScrn
, pBus
, FALSE
);
474 DDC2ReadDisplayID(void)
480 * Attempts to probe the monitor for DisplayID information, if NoDDC and
481 * NoDDC2 are unset. DisplayID blocks are interpreted and the results
482 * returned in an xf86MonPtr.
484 * This function does not affect the list of modes used by drivers -- it is up
485 * to the driver to decide policy on what to do with DisplayID information.
487 * @return pointer to a new xf86MonPtr containing the DisplayID information.
488 * @return NULL if no monitor attached or failure to interpret the DisplayID.
491 xf86DoDisplayID(ScrnInfoPtr pScrn
, I2CBusPtr pBus
)
493 unsigned char *did
= NULL
;
494 xf86MonPtr tmp
= NULL
;
495 I2CDevPtr dev
= NULL
;
497 /* Default DDC and DDC2 to enabled. */
498 Bool noddc
= FALSE
, noddc2
= FALSE
;
499 OptionInfoPtr options
;
501 options
= malloc(sizeof(DDCOptions
));
504 memcpy(options
, DDCOptions
, sizeof(DDCOptions
));
505 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, options
);
507 xf86GetOptValBool(options
, DDCOPT_NODDC
, &noddc
);
508 xf86GetOptValBool(options
, DDCOPT_NODDC2
, &noddc2
);
514 if (!(dev
= DDC2Init(pBus
)))
517 if ((did
= DDC2ReadDisplayID())) {
518 tmp
= calloc(1, sizeof(*tmp
));
522 tmp
->scrnIndex
= pScrn
->scrnIndex
;
523 tmp
->flags
|= MONITOR_DISPLAYID
;