First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / ddc / xf86DDC.c
blobe47b8b80ccbadc3b046fc3e17dc5d18206ed9ef5
1 /* xf86DDC.c
2 *
3 * Copyright 1998,1999 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
4 */
5 #ifdef HAVE_XORG_CONFIG_H
6 #include <xorg-config.h>
7 #endif
9 #include "misc.h"
10 #include "xf86.h"
11 #include "xf86_OSproc.h"
12 #include "xf86DDC.h"
13 #include "ddcPriv.h"
14 #include <string.h>
16 static const OptionInfoRec *DDCAvailableOptions(void *unused);
18 #define RETRIES 4
20 static unsigned char *EDIDRead_DDC1(
21 ScrnInfoPtr pScrn,
22 DDC1SetSpeedProc,
23 unsigned int (*)(ScrnInfoPtr)
26 static Bool TestDDC1(
27 ScrnInfoPtr pScrn,
28 unsigned int (*)(ScrnInfoPtr)
31 static unsigned int *FetchEDID_DDC1(
32 ScrnInfoPtr,
33 register unsigned int (*)(ScrnInfoPtr)
36 static unsigned char* EDID1Read_DDC2(
37 int scrnIndex,
38 I2CBusPtr pBus
41 static unsigned char * DDCRead_DDC2(
42 int scrnIndex,
43 I2CBusPtr pBus,
44 int start,
45 int len
48 typedef enum {
49 DDCOPT_NODDC1,
50 DDCOPT_NODDC2,
51 DDCOPT_NODDC
52 } DDCOpts;
54 static const OptionInfoRec DDCOptions[] = {
55 { DDCOPT_NODDC1, "NoDDC1", OPTV_BOOLEAN, {0}, FALSE },
56 { DDCOPT_NODDC2, "NoDDC2", OPTV_BOOLEAN, {0}, FALSE },
57 { DDCOPT_NODDC, "NoDDC", OPTV_BOOLEAN, {0}, FALSE },
58 { -1, NULL, OPTV_NONE, {0}, FALSE },
61 /*ARGSUSED*/
62 static const OptionInfoRec *
63 DDCAvailableOptions(void *unused)
65 return (DDCOptions);
68 /**
69 * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC1 are
70 * unset. EDID information blocks are interpreted and the results returned in
71 * an xf86MonPtr.
73 * This function does not affect the list of modes used by drivers -- it is up
74 * to the driver to decide policy on what to do with EDID information.
76 * @return pointer to a new xf86MonPtr containing the EDID information.
77 * @return NULL if no monitor attached or failure to interpret the EDID.
79 xf86MonPtr
80 xf86DoEDID_DDC1(
81 int scrnIndex, DDC1SetSpeedProc DDC1SetSpeed,
82 unsigned int (*DDC1Read)(ScrnInfoPtr)
85 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
86 unsigned char *EDID_block = NULL;
87 xf86MonPtr tmp = NULL;
88 int sigio;
89 /* Default DDC and DDC1 to enabled. */
90 Bool noddc = FALSE, noddc1 = FALSE;
91 OptionInfoPtr options;
93 options = xnfalloc(sizeof(DDCOptions));
94 (void)memcpy(options, DDCOptions, sizeof(DDCOptions));
95 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
97 xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
98 xf86GetOptValBool(options, DDCOPT_NODDC1, &noddc1);
99 xfree(options);
101 if (noddc || noddc1)
102 return NULL;
104 sigio = xf86BlockSIGIO();
105 EDID_block = EDIDRead_DDC1(pScrn,DDC1SetSpeed,DDC1Read);
106 xf86UnblockSIGIO(sigio);
108 if (EDID_block){
109 tmp = xf86InterpretEDID(scrnIndex,EDID_block);
111 #ifdef DEBUG
112 else ErrorF("No EDID block returned\n");
113 if (!tmp)
114 ErrorF("Cannot interpret EDID block\n");
115 #endif
116 return tmp;
120 * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
121 * unset. EDID information blocks are interpreted and the results returned in
122 * an xf86MonPtr.
124 * This function does not affect the list of modes used by drivers -- it is up
125 * to the driver to decide policy on what to do with EDID information.
127 * @return pointer to a new xf86MonPtr containing the EDID information.
128 * @return NULL if no monitor attached or failure to interpret the EDID.
130 xf86MonPtr
131 xf86DoEDID_DDC2(int scrnIndex, I2CBusPtr pBus)
133 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
134 unsigned char *EDID_block = NULL;
135 xf86MonPtr tmp = NULL;
136 /* Default DDC and DDC2 to enabled. */
137 Bool noddc = FALSE, noddc2 = FALSE;
138 OptionInfoPtr options;
140 options = xnfalloc(sizeof(DDCOptions));
141 (void)memcpy(options, DDCOptions, sizeof(DDCOptions));
142 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
144 xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
145 xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2);
146 xfree(options);
148 if (noddc || noddc2)
149 return NULL;
151 EDID_block = EDID1Read_DDC2(scrnIndex,pBus);
153 if (EDID_block){
154 tmp = xf86InterpretEDID(scrnIndex,EDID_block);
155 } else {
156 #ifdef DEBUG
157 ErrorF("No EDID block returned\n");
158 #endif
159 return NULL;
161 #ifdef DEBUG
162 if (!tmp)
163 ErrorF("Cannot interpret EDID block\n");
164 else
165 ErrorF("Sections to follow: %i\n",tmp->no_sections);
166 #endif
168 return tmp;
172 * read EDID record , pass it to callback function to interpret.
173 * callback function will store it for further use by calling
174 * function; it will also decide if we need to reread it
176 static unsigned char *
177 EDIDRead_DDC1(ScrnInfoPtr pScrn, DDC1SetSpeedProc DDCSpeed,
178 unsigned int (*read_DDC)(ScrnInfoPtr))
180 unsigned char *EDID_block = NULL;
181 int count = RETRIES;
183 if (!read_DDC) {
184 xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
185 "chipset doesn't support DDC1\n");
186 return NULL;
189 if (TestDDC1(pScrn,read_DDC)==-1) {
190 xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "No DDC signal\n");
191 return NULL;
194 if (DDCSpeed) DDCSpeed(pScrn,DDC_FAST);
195 do {
196 EDID_block = GetEDID_DDC1(FetchEDID_DDC1(pScrn,read_DDC));
197 count --;
198 } while (!EDID_block && count);
199 if (DDCSpeed) DDCSpeed(pScrn,DDC_SLOW);
201 return EDID_block;
204 /* test if DDC1 return 0 if not */
205 static Bool
206 TestDDC1(ScrnInfoPtr pScrn, unsigned int (*read_DDC)(ScrnInfoPtr))
208 int old, count;
210 old = read_DDC(pScrn);
211 count = HEADER * BITS_PER_BYTE;
212 do {
213 /* wait for next retrace */
214 if (old != read_DDC(pScrn)) break;
215 } while(count--);
216 return (count);
219 /* fetch entire EDID record; DDC bit needs to be masked */
220 static unsigned int *
221 FetchEDID_DDC1(register ScrnInfoPtr pScrn,
222 register unsigned int (*read_DDC)(ScrnInfoPtr))
224 int count = NUM;
225 unsigned int *ptr, *xp;
227 ptr=xp=xalloc(sizeof(int)*NUM);
229 if (!ptr) return NULL;
230 do {
231 /* wait for next retrace */
232 *xp = read_DDC(pScrn);
233 xp++;
234 } while(--count);
235 return (ptr);
238 static unsigned char*
239 EDID1Read_DDC2(int scrnIndex, I2CBusPtr pBus)
241 return DDCRead_DDC2(scrnIndex, pBus, 0, EDID1_LEN);
244 static unsigned char *
245 DDCRead_DDC2(int scrnIndex, I2CBusPtr pBus, int start, int len)
247 I2CDevPtr dev;
248 unsigned char W_Buffer[2];
249 int w_bytes;
250 unsigned char *R_Buffer;
251 int i;
254 * Slow down the bus so that older monitors don't
255 * miss things.
257 pBus->RiseFallTime = 20;
259 if (!(dev = xf86I2CFindDev(pBus, 0x00A0))) {
260 dev = xf86CreateI2CDevRec();
261 dev->DevName = "ddc2";
262 dev->SlaveAddr = 0xA0;
263 dev->ByteTimeout = 2200; /* VESA DDC spec 3 p. 43 (+10 %) */
264 dev->StartTimeout = 550;
265 dev->BitTimeout = 40;
266 dev->AcknTimeout = 40;
268 dev->pI2CBus = pBus;
269 if (!xf86I2CDevInit(dev)) {
270 xf86DrvMsg(scrnIndex, X_PROBED, "No DDC2 device\n");
271 return NULL;
274 if (start < 0x100) {
275 w_bytes = 1;
276 W_Buffer[0] = start;
277 } else {
278 w_bytes = 2;
279 W_Buffer[0] = start & 0xFF;
280 W_Buffer[1] = (start & 0xFF00) >> 8;
282 R_Buffer = xcalloc(1,sizeof(unsigned char)
283 * (len));
284 for (i=0; i<RETRIES; i++) {
285 if (xf86I2CWriteRead(dev, W_Buffer,w_bytes, R_Buffer,len)) {
286 if (!DDC_checksum(R_Buffer,len))
287 return R_Buffer;
289 #ifdef DEBUG
290 else ErrorF("Checksum error in EDID block\n");
291 #endif
293 #ifdef DEBUG
294 else ErrorF("Error reading EDID block\n");
295 #endif
298 xf86DestroyI2CDevRec(dev,TRUE);
299 xfree(R_Buffer);
300 return NULL;