aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xfree86/ddc
diff options
context:
space:
mode:
Diffstat (limited to 'xorg-server/hw/xfree86/ddc')
-rw-r--r--xorg-server/hw/xfree86/ddc/ddc.c1014
-rw-r--r--xorg-server/hw/xfree86/ddc/ddcProperty.c250
-rw-r--r--xorg-server/hw/xfree86/ddc/interpret_edid.c1370
3 files changed, 1317 insertions, 1317 deletions
diff --git a/xorg-server/hw/xfree86/ddc/ddc.c b/xorg-server/hw/xfree86/ddc/ddc.c
index 6fad9fbbc..ec6465818 100644
--- a/xorg-server/hw/xfree86/ddc/ddc.c
+++ b/xorg-server/hw/xfree86/ddc/ddc.c
@@ -1,507 +1,507 @@
-/* xf86DDC.c
- *
- * Copyright 1998,1999 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
- */
-
-/*
- * A note on terminology. DDC1 is the original dumb serial protocol, and
- * can only do up to 128 bytes of EDID. DDC2 is I2C-encapsulated and
- * introduces extension blocks. EDID is the old display identification
- * block, DisplayID is the new one.
- */
-
-#ifdef HAVE_XORG_CONFIG_H
-#include <xorg-config.h>
-#endif
-
-#include "misc.h"
-#include "xf86.h"
-#include "xf86_OSproc.h"
-#include "xf86DDC.h"
-#include <string.h>
-
-#define RETRIES 4
-
-typedef enum {
- DDCOPT_NODDC1,
- DDCOPT_NODDC2,
- DDCOPT_NODDC
-} DDCOpts;
-
-static const OptionInfoRec DDCOptions[] = {
- { DDCOPT_NODDC1, "NoDDC1", OPTV_BOOLEAN, {0}, FALSE },
- { DDCOPT_NODDC2, "NoDDC2", OPTV_BOOLEAN, {0}, FALSE },
- { DDCOPT_NODDC, "NoDDC", OPTV_BOOLEAN, {0}, FALSE },
- { -1, NULL, OPTV_NONE, {0}, FALSE },
-};
-
-/* DDC1 */
-
-static int
-find_start(unsigned int *ptr)
-{
- unsigned int comp[9], test[9];
- int i,j;
-
- for (i=0;i<9;i++){
- comp[i] = *(ptr++);
- test[i] = 1;
- }
- for (i=0;i<127;i++){
- for (j=0;j<9;j++){
- test[j] = test[j] & !(comp[j] ^ *(ptr++));
- }
- }
- for (i=0;i<9;i++)
- if (test[i]) return (i+1);
- return (-1);
-}
-
-static unsigned char *
-find_header(unsigned char *block)
-{
- unsigned char *ptr, *head_ptr, *end;
- unsigned char header[]={0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
-
- ptr = block;
- end = block + EDID1_LEN;
- while (ptr<end) {
- int i;
- head_ptr = ptr;
- for (i=0;i<8;i++){
- if (header[i] != *(head_ptr++)) break;
- if (head_ptr == end) head_ptr = block;
- }
- if (i==8) break;
- ptr++;
- }
- if (ptr == end) return (NULL);
- return (ptr);
-}
-
-static unsigned char *
-resort(unsigned char *s_block)
-{
- unsigned char *d_new, *d_ptr, *d_end, *s_ptr, *s_end;
- unsigned char tmp;
-
- s_end = s_block + EDID1_LEN;
- d_new = xalloc(EDID1_LEN);
- if (!d_new) return NULL;
- d_end = d_new + EDID1_LEN;
-
- s_ptr = find_header(s_block);
- if (!s_ptr) return NULL;
- for (d_ptr=d_new;d_ptr<d_end;d_ptr++){
- tmp = *(s_ptr++);
- *d_ptr = tmp;
- if (s_ptr == s_end) s_ptr = s_block;
- }
- xfree(s_block);
- return (d_new);
-}
-
-static int
-DDC_checksum(unsigned char *block, int len)
-{
- int i, result = 0;
- int not_null = 0;
-
- for (i=0;i<len;i++) {
- not_null |= block[i];
- result += block[i];
- }
-
-#ifdef DEBUG
- if (result & 0xFF) ErrorF("DDC checksum not correct\n");
- if (!not_null) ErrorF("DDC read all Null\n");
-#endif
-
- /* catch the trivial case where all bytes are 0 */
- if (!not_null) return 1;
-
- return (result&0xFF);
-}
-
-static unsigned char *
-GetEDID_DDC1(unsigned int *s_ptr)
-{
- unsigned char *d_block, *d_pos;
- unsigned int *s_pos, *s_end;
- int s_start;
- int i,j;
- s_start = find_start(s_ptr);
- if (s_start==-1) return NULL;
- s_end = s_ptr + NUM;
- s_pos = s_ptr + s_start;
- d_block=xalloc(EDID1_LEN);
- if (!d_block) return NULL;
- d_pos = d_block;
- for (i=0;i<EDID1_LEN;i++) {
- for (j=0;j<8;j++) {
- *d_pos <<= 1;
- if (*s_pos) {
- *d_pos |= 0x01;
- }
- s_pos++; if (s_pos == s_end) s_pos=s_ptr;
- };
- s_pos++; if (s_pos == s_end) s_pos=s_ptr;
- d_pos++;
- }
- xfree(s_ptr);
- if (d_block && DDC_checksum(d_block,EDID1_LEN)) return NULL;
- return (resort(d_block));
-}
-
-/* fetch entire EDID record; DDC bit needs to be masked */
-static unsigned int *
-FetchEDID_DDC1(register ScrnInfoPtr pScrn,
- register unsigned int (*read_DDC)(ScrnInfoPtr))
-{
- int count = NUM;
- unsigned int *ptr, *xp;
-
- ptr=xp=xalloc(sizeof(int)*NUM);
-
- if (!ptr) return NULL;
- do {
- /* wait for next retrace */
- *xp = read_DDC(pScrn);
- xp++;
- } while(--count);
- return (ptr);
-}
-
-/* test if DDC1 return 0 if not */
-static Bool
-TestDDC1(ScrnInfoPtr pScrn, unsigned int (*read_DDC)(ScrnInfoPtr))
-{
- int old, count;
-
- old = read_DDC(pScrn);
- count = HEADER * BITS_PER_BYTE;
- do {
- /* wait for next retrace */
- if (old != read_DDC(pScrn)) break;
- } while(count--);
- return (count);
-}
-
-/*
- * read EDID record , pass it to callback function to interpret.
- * callback function will store it for further use by calling
- * function; it will also decide if we need to reread it
- */
-static unsigned char *
-EDIDRead_DDC1(ScrnInfoPtr pScrn, DDC1SetSpeedProc DDCSpeed,
- unsigned int (*read_DDC)(ScrnInfoPtr))
-{
- unsigned char *EDID_block = NULL;
- int count = RETRIES;
-
- if (!read_DDC) {
- xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
- "chipset doesn't support DDC1\n");
- return NULL;
- };
-
- if (TestDDC1(pScrn,read_DDC)==-1) {
- xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "No DDC signal\n");
- return NULL;
- };
-
- if (DDCSpeed) DDCSpeed(pScrn,DDC_FAST);
- do {
- EDID_block = GetEDID_DDC1(FetchEDID_DDC1(pScrn,read_DDC));
- count --;
- } while (!EDID_block && count);
- if (DDCSpeed) DDCSpeed(pScrn,DDC_SLOW);
-
- return EDID_block;
-}
-
-/**
- * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC1 are
- * unset. EDID information blocks are interpreted and the results returned in
- * an xf86MonPtr.
- *
- * This function does not affect the list of modes used by drivers -- it is up
- * to the driver to decide policy on what to do with EDID information.
- *
- * @return pointer to a new xf86MonPtr containing the EDID information.
- * @return NULL if no monitor attached or failure to interpret the EDID.
- */
-xf86MonPtr
-xf86DoEDID_DDC1(
- int scrnIndex, DDC1SetSpeedProc DDC1SetSpeed,
- unsigned int (*DDC1Read)(ScrnInfoPtr)
-)
-{
- ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
- unsigned char *EDID_block = NULL;
- xf86MonPtr tmp = NULL;
- int sigio;
- /* Default DDC and DDC1 to enabled. */
- Bool noddc = FALSE, noddc1 = FALSE;
- OptionInfoPtr options;
-
- options = xnfalloc(sizeof(DDCOptions));
- (void)memcpy(options, DDCOptions, sizeof(DDCOptions));
- xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
-
- xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
- xf86GetOptValBool(options, DDCOPT_NODDC1, &noddc1);
- xfree(options);
-
- if (noddc || noddc1)
- return NULL;
-
- sigio = xf86BlockSIGIO();
- EDID_block = EDIDRead_DDC1(pScrn,DDC1SetSpeed,DDC1Read);
- xf86UnblockSIGIO(sigio);
-
- if (EDID_block){
- tmp = xf86InterpretEDID(scrnIndex,EDID_block);
- }
-#ifdef DEBUG
- else ErrorF("No EDID block returned\n");
- if (!tmp)
- ErrorF("Cannot interpret EDID block\n");
-#endif
- return tmp;
-}
-
-/* DDC2 */
-
-static I2CDevPtr
-DDC2MakeDevice(I2CBusPtr pBus, int address, char *name)
-{
- I2CDevPtr dev = NULL;
-
- if (!(dev = xf86I2CFindDev(pBus, address))) {
- dev = xf86CreateI2CDevRec();
- dev->DevName = name;
- dev->SlaveAddr = address;
- dev->ByteTimeout = 2200; /* VESA DDC spec 3 p. 43 (+10 %) */
- dev->StartTimeout = 550;
- dev->BitTimeout = 40;
- dev->AcknTimeout = 40;
-
- dev->pI2CBus = pBus;
- if (!xf86I2CDevInit(dev)) {
- xf86DrvMsg(pBus->scrnIndex, X_PROBED, "No DDC2 device\n");
- return NULL;
- }
- }
-
- return dev;
-}
-
-static I2CDevPtr
-DDC2Init(int scrnIndex, I2CBusPtr pBus)
-{
- I2CDevPtr dev = NULL;
-
- /*
- * Slow down the bus so that older monitors don't
- * miss things.
- */
- pBus->RiseFallTime = 20;
-
- dev = DDC2MakeDevice(pBus, 0x00A0, "ddc2");
- if (xf86I2CProbeAddress(pBus, 0x0060))
- DDC2MakeDevice(pBus, 0x0060, "E-EDID segment register");
- if (xf86I2CProbeAddress(pBus, 0x0062))
- DDC2MakeDevice(pBus, 0x0062, "EDID EEPROM interface");
- if (xf86I2CProbeAddress(pBus, 0x006E))
- DDC2MakeDevice(pBus, 0x006E, "DDC control interface");
-
- return dev;
-}
-
-/* Mmmm, smell the hacks */
-static void
-EEDIDStop(I2CDevPtr d)
-{
-}
-
-/* block is the EDID block number. a segment is two blocks. */
-static Bool
-DDC2Read(I2CDevPtr dev, int block, unsigned char *R_Buffer)
-{
- unsigned char W_Buffer[1];
- int i, segment;
- I2CDevPtr seg;
- void (*stop)(I2CDevPtr);
-
- for (i = 0; i < RETRIES; i++) {
- /* Stop bits reset the segment pointer to 0, so be careful here. */
- segment = block >> 1;
- if (segment) {
- Bool b;
-
- if (!(seg = xf86I2CFindDev(dev->pI2CBus, 0x0060)))
- return FALSE;
-
- W_Buffer[0] = segment;
-
- stop = dev->pI2CBus->I2CStop;
- dev->pI2CBus->I2CStop = EEDIDStop;
-
- b = xf86I2CWriteRead(seg, W_Buffer, 1, NULL, 0);
-
- dev->pI2CBus->I2CStop = stop;
- if (!b) {
- dev->pI2CBus->I2CStop(dev);
- continue;
- }
- }
-
- W_Buffer[0] = (block & 0x01) * EDID1_LEN;
-
- if (xf86I2CWriteRead(dev, W_Buffer, 1, R_Buffer, EDID1_LEN)) {
- if (!DDC_checksum(R_Buffer, EDID1_LEN))
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
-/**
- * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
- * unset. EDID information blocks are interpreted and the results returned in
- * an xf86MonPtr. Unlike xf86DoEDID_DDC[12](), this function will return
- * the complete EDID data, including all extension blocks, if the 'complete'
- * parameter is TRUE;
- *
- * This function does not affect the list of modes used by drivers -- it is up
- * to the driver to decide policy on what to do with EDID information.
- *
- * @return pointer to a new xf86MonPtr containing the EDID information.
- * @return NULL if no monitor attached or failure to interpret the EDID.
- */
-xf86MonPtr
-xf86DoEEDID(int scrnIndex, I2CBusPtr pBus, Bool complete)
-{
- ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
- unsigned char *EDID_block = NULL;
- xf86MonPtr tmp = NULL;
- I2CDevPtr dev = NULL;
- /* Default DDC and DDC2 to enabled. */
- Bool noddc = FALSE, noddc2 = FALSE;
- OptionInfoPtr options;
-
- options = xalloc(sizeof(DDCOptions));
- if (!options)
- return NULL;
- memcpy(options, DDCOptions, sizeof(DDCOptions));
- xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
-
- xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
- xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2);
- xfree(options);
-
- if (noddc || noddc2)
- return NULL;
-
- if (!(dev = DDC2Init(scrnIndex, pBus)))
- return NULL;
-
- EDID_block = xcalloc(1, EDID1_LEN);
- if (!EDID_block)
- return NULL;
-
- if (DDC2Read(dev, 0, EDID_block)) {
- int i, n = EDID_block[0x7e];
-
- if (complete && n) {
- EDID_block = xrealloc(EDID_block, EDID1_LEN * (1+n));
-
- for (i = 0; i < n; i++)
- DDC2Read(dev, i+1, EDID_block + (EDID1_LEN * (1+i)));
- }
-
- tmp = xf86InterpretEEDID(scrnIndex, EDID_block);
- }
-
- if (tmp && complete)
- tmp->flags |= MONITOR_EDID_COMPLETE_RAWDATA;
-
- return tmp;
-}
-
-/**
- * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
- * unset. EDID information blocks are interpreted and the results returned in
- * an xf86MonPtr.
- *
- * This function does not affect the list of modes used by drivers -- it is up
- * to the driver to decide policy on what to do with EDID information.
- *
- * @return pointer to a new xf86MonPtr containing the EDID information.
- * @return NULL if no monitor attached or failure to interpret the EDID.
- */
-xf86MonPtr
-xf86DoEDID_DDC2(int scrnIndex, I2CBusPtr pBus)
-{
- return xf86DoEEDID(scrnIndex, pBus, FALSE);
-}
-
-/* XXX write me */
-static void *
-DDC2ReadDisplayID(void)
-{
- return FALSE;
-}
-
-/**
- * Attempts to probe the monitor for DisplayID information, if NoDDC and
- * NoDDC2 are unset. DisplayID blocks are interpreted and the results
- * returned in an xf86MonPtr.
- *
- * This function does not affect the list of modes used by drivers -- it is up
- * to the driver to decide policy on what to do with DisplayID information.
- *
- * @return pointer to a new xf86MonPtr containing the DisplayID information.
- * @return NULL if no monitor attached or failure to interpret the DisplayID.
- */
-xf86MonPtr
-xf86DoDisplayID(int scrnIndex, I2CBusPtr pBus)
-{
- ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
- unsigned char *did = NULL;
- xf86MonPtr tmp = NULL;
- I2CDevPtr dev = NULL;
- /* Default DDC and DDC2 to enabled. */
- Bool noddc = FALSE, noddc2 = FALSE;
- OptionInfoPtr options;
-
- options = xalloc(sizeof(DDCOptions));
- if (!options)
- return NULL;
- memcpy(options, DDCOptions, sizeof(DDCOptions));
- xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
-
- xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
- xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2);
- xfree(options);
-
- if (noddc || noddc2)
- return NULL;
-
- if (!(dev = DDC2Init(scrnIndex, pBus)))
- return NULL;
-
- if ((did = DDC2ReadDisplayID())) {
- tmp = xcalloc(1, sizeof(*tmp));
- if (!tmp)
- return NULL;
-
- tmp->scrnIndex = scrnIndex;
- tmp->flags |= MONITOR_DISPLAYID;
- tmp->rawData = did;
- }
-
- return tmp;
-}
+/* xf86DDC.c
+ *
+ * Copyright 1998,1999 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
+ */
+
+/*
+ * A note on terminology. DDC1 is the original dumb serial protocol, and
+ * can only do up to 128 bytes of EDID. DDC2 is I2C-encapsulated and
+ * introduces extension blocks. EDID is the old display identification
+ * block, DisplayID is the new one.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "misc.h"
+#include "xf86.h"
+#include "xf86_OSproc.h"
+#include "xf86DDC.h"
+#include <string.h>
+
+#define RETRIES 4
+
+typedef enum {
+ DDCOPT_NODDC1,
+ DDCOPT_NODDC2,
+ DDCOPT_NODDC
+} DDCOpts;
+
+static const OptionInfoRec DDCOptions[] = {
+ { DDCOPT_NODDC1, "NoDDC1", OPTV_BOOLEAN, {0}, FALSE },
+ { DDCOPT_NODDC2, "NoDDC2", OPTV_BOOLEAN, {0}, FALSE },
+ { DDCOPT_NODDC, "NoDDC", OPTV_BOOLEAN, {0}, FALSE },
+ { -1, NULL, OPTV_NONE, {0}, FALSE },
+};
+
+/* DDC1 */
+
+static int
+find_start(unsigned int *ptr)
+{
+ unsigned int comp[9], test[9];
+ int i,j;
+
+ for (i=0;i<9;i++){
+ comp[i] = *(ptr++);
+ test[i] = 1;
+ }
+ for (i=0;i<127;i++){
+ for (j=0;j<9;j++){
+ test[j] = test[j] & !(comp[j] ^ *(ptr++));
+ }
+ }
+ for (i=0;i<9;i++)
+ if (test[i]) return (i+1);
+ return (-1);
+}
+
+static unsigned char *
+find_header(unsigned char *block)
+{
+ unsigned char *ptr, *head_ptr, *end;
+ unsigned char header[]={0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
+
+ ptr = block;
+ end = block + EDID1_LEN;
+ while (ptr<end) {
+ int i;
+ head_ptr = ptr;
+ for (i=0;i<8;i++){
+ if (header[i] != *(head_ptr++)) break;
+ if (head_ptr == end) head_ptr = block;
+ }
+ if (i==8) break;
+ ptr++;
+ }
+ if (ptr == end) return (NULL);
+ return (ptr);
+}
+
+static unsigned char *
+resort(unsigned char *s_block)
+{
+ unsigned char *d_new, *d_ptr, *d_end, *s_ptr, *s_end;
+ unsigned char tmp;
+
+ s_end = s_block + EDID1_LEN;
+ d_new = malloc(EDID1_LEN);
+ if (!d_new) return NULL;
+ d_end = d_new + EDID1_LEN;
+
+ s_ptr = find_header(s_block);
+ if (!s_ptr) return NULL;
+ for (d_ptr=d_new;d_ptr<d_end;d_ptr++){
+ tmp = *(s_ptr++);
+ *d_ptr = tmp;
+ if (s_ptr == s_end) s_ptr = s_block;
+ }
+ free(s_block);
+ return (d_new);
+}
+
+static int
+DDC_checksum(unsigned char *block, int len)
+{
+ int i, result = 0;
+ int not_null = 0;
+
+ for (i=0;i<len;i++) {
+ not_null |= block[i];
+ result += block[i];
+ }
+
+#ifdef DEBUG
+ if (result & 0xFF) ErrorF("DDC checksum not correct\n");
+ if (!not_null) ErrorF("DDC read all Null\n");
+#endif
+
+ /* catch the trivial case where all bytes are 0 */
+ if (!not_null) return 1;
+
+ return (result&0xFF);
+}
+
+static unsigned char *
+GetEDID_DDC1(unsigned int *s_ptr)
+{
+ unsigned char *d_block, *d_pos;
+ unsigned int *s_pos, *s_end;
+ int s_start;
+ int i,j;
+ s_start = find_start(s_ptr);
+ if (s_start==-1) return NULL;
+ s_end = s_ptr + NUM;
+ s_pos = s_ptr + s_start;
+ d_block=malloc(EDID1_LEN);
+ if (!d_block) return NULL;
+ d_pos = d_block;
+ for (i=0;i<EDID1_LEN;i++) {
+ for (j=0;j<8;j++) {
+ *d_pos <<= 1;
+ if (*s_pos) {
+ *d_pos |= 0x01;
+ }
+ s_pos++; if (s_pos == s_end) s_pos=s_ptr;
+ };
+ s_pos++; if (s_pos == s_end) s_pos=s_ptr;
+ d_pos++;
+ }
+ free(s_ptr);
+ if (d_block && DDC_checksum(d_block,EDID1_LEN)) return NULL;
+ return (resort(d_block));
+}
+
+/* fetch entire EDID record; DDC bit needs to be masked */
+static unsigned int *
+FetchEDID_DDC1(register ScrnInfoPtr pScrn,
+ register unsigned int (*read_DDC)(ScrnInfoPtr))
+{
+ int count = NUM;
+ unsigned int *ptr, *xp;
+
+ ptr=xp=malloc(sizeof(int)*NUM);
+
+ if (!ptr) return NULL;
+ do {
+ /* wait for next retrace */
+ *xp = read_DDC(pScrn);
+ xp++;
+ } while(--count);
+ return (ptr);
+}
+
+/* test if DDC1 return 0 if not */
+static Bool
+TestDDC1(ScrnInfoPtr pScrn, unsigned int (*read_DDC)(ScrnInfoPtr))
+{
+ int old, count;
+
+ old = read_DDC(pScrn);
+ count = HEADER * BITS_PER_BYTE;
+ do {
+ /* wait for next retrace */
+ if (old != read_DDC(pScrn)) break;
+ } while(count--);
+ return (count);
+}
+
+/*
+ * read EDID record , pass it to callback function to interpret.
+ * callback function will store it for further use by calling
+ * function; it will also decide if we need to reread it
+ */
+static unsigned char *
+EDIDRead_DDC1(ScrnInfoPtr pScrn, DDC1SetSpeedProc DDCSpeed,
+ unsigned int (*read_DDC)(ScrnInfoPtr))
+{
+ unsigned char *EDID_block = NULL;
+ int count = RETRIES;
+
+ if (!read_DDC) {
+ xf86DrvMsg(pScrn->scrnIndex, X_PROBED,
+ "chipset doesn't support DDC1\n");
+ return NULL;
+ };
+
+ if (TestDDC1(pScrn,read_DDC)==-1) {
+ xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "No DDC signal\n");
+ return NULL;
+ };
+
+ if (DDCSpeed) DDCSpeed(pScrn,DDC_FAST);
+ do {
+ EDID_block = GetEDID_DDC1(FetchEDID_DDC1(pScrn,read_DDC));
+ count --;
+ } while (!EDID_block && count);
+ if (DDCSpeed) DDCSpeed(pScrn,DDC_SLOW);
+
+ return EDID_block;
+}
+
+/**
+ * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC1 are
+ * unset. EDID information blocks are interpreted and the results returned in
+ * an xf86MonPtr.
+ *
+ * This function does not affect the list of modes used by drivers -- it is up
+ * to the driver to decide policy on what to do with EDID information.
+ *
+ * @return pointer to a new xf86MonPtr containing the EDID information.
+ * @return NULL if no monitor attached or failure to interpret the EDID.
+ */
+xf86MonPtr
+xf86DoEDID_DDC1(
+ int scrnIndex, DDC1SetSpeedProc DDC1SetSpeed,
+ unsigned int (*DDC1Read)(ScrnInfoPtr)
+)
+{
+ ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
+ unsigned char *EDID_block = NULL;
+ xf86MonPtr tmp = NULL;
+ int sigio;
+ /* Default DDC and DDC1 to enabled. */
+ Bool noddc = FALSE, noddc1 = FALSE;
+ OptionInfoPtr options;
+
+ options = xnfalloc(sizeof(DDCOptions));
+ (void)memcpy(options, DDCOptions, sizeof(DDCOptions));
+ xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
+
+ xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
+ xf86GetOptValBool(options, DDCOPT_NODDC1, &noddc1);
+ free(options);
+
+ if (noddc || noddc1)
+ return NULL;
+
+ sigio = xf86BlockSIGIO();
+ EDID_block = EDIDRead_DDC1(pScrn,DDC1SetSpeed,DDC1Read);
+ xf86UnblockSIGIO(sigio);
+
+ if (EDID_block){
+ tmp = xf86InterpretEDID(scrnIndex,EDID_block);
+ }
+#ifdef DEBUG
+ else ErrorF("No EDID block returned\n");
+ if (!tmp)
+ ErrorF("Cannot interpret EDID block\n");
+#endif
+ return tmp;
+}
+
+/* DDC2 */
+
+static I2CDevPtr
+DDC2MakeDevice(I2CBusPtr pBus, int address, char *name)
+{
+ I2CDevPtr dev = NULL;
+
+ if (!(dev = xf86I2CFindDev(pBus, address))) {
+ dev = xf86CreateI2CDevRec();
+ dev->DevName = name;
+ dev->SlaveAddr = address;
+ dev->ByteTimeout = 2200; /* VESA DDC spec 3 p. 43 (+10 %) */
+ dev->StartTimeout = 550;
+ dev->BitTimeout = 40;
+ dev->AcknTimeout = 40;
+
+ dev->pI2CBus = pBus;
+ if (!xf86I2CDevInit(dev)) {
+ xf86DrvMsg(pBus->scrnIndex, X_PROBED, "No DDC2 device\n");
+ return NULL;
+ }
+ }
+
+ return dev;
+}
+
+static I2CDevPtr
+DDC2Init(int scrnIndex, I2CBusPtr pBus)
+{
+ I2CDevPtr dev = NULL;
+
+ /*
+ * Slow down the bus so that older monitors don't
+ * miss things.
+ */
+ pBus->RiseFallTime = 20;
+
+ dev = DDC2MakeDevice(pBus, 0x00A0, "ddc2");
+ if (xf86I2CProbeAddress(pBus, 0x0060))
+ DDC2MakeDevice(pBus, 0x0060, "E-EDID segment register");
+ if (xf86I2CProbeAddress(pBus, 0x0062))
+ DDC2MakeDevice(pBus, 0x0062, "EDID EEPROM interface");
+ if (xf86I2CProbeAddress(pBus, 0x006E))
+ DDC2MakeDevice(pBus, 0x006E, "DDC control interface");
+
+ return dev;
+}
+
+/* Mmmm, smell the hacks */
+static void
+EEDIDStop(I2CDevPtr d)
+{
+}
+
+/* block is the EDID block number. a segment is two blocks. */
+static Bool
+DDC2Read(I2CDevPtr dev, int block, unsigned char *R_Buffer)
+{
+ unsigned char W_Buffer[1];
+ int i, segment;
+ I2CDevPtr seg;
+ void (*stop)(I2CDevPtr);
+
+ for (i = 0; i < RETRIES; i++) {
+ /* Stop bits reset the segment pointer to 0, so be careful here. */
+ segment = block >> 1;
+ if (segment) {
+ Bool b;
+
+ if (!(seg = xf86I2CFindDev(dev->pI2CBus, 0x0060)))
+ return FALSE;
+
+ W_Buffer[0] = segment;
+
+ stop = dev->pI2CBus->I2CStop;
+ dev->pI2CBus->I2CStop = EEDIDStop;
+
+ b = xf86I2CWriteRead(seg, W_Buffer, 1, NULL, 0);
+
+ dev->pI2CBus->I2CStop = stop;
+ if (!b) {
+ dev->pI2CBus->I2CStop(dev);
+ continue;
+ }
+ }
+
+ W_Buffer[0] = (block & 0x01) * EDID1_LEN;
+
+ if (xf86I2CWriteRead(dev, W_Buffer, 1, R_Buffer, EDID1_LEN)) {
+ if (!DDC_checksum(R_Buffer, EDID1_LEN))
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+/**
+ * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
+ * unset. EDID information blocks are interpreted and the results returned in
+ * an xf86MonPtr. Unlike xf86DoEDID_DDC[12](), this function will return
+ * the complete EDID data, including all extension blocks, if the 'complete'
+ * parameter is TRUE;
+ *
+ * This function does not affect the list of modes used by drivers -- it is up
+ * to the driver to decide policy on what to do with EDID information.
+ *
+ * @return pointer to a new xf86MonPtr containing the EDID information.
+ * @return NULL if no monitor attached or failure to interpret the EDID.
+ */
+xf86MonPtr
+xf86DoEEDID(int scrnIndex, I2CBusPtr pBus, Bool complete)
+{
+ ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
+ unsigned char *EDID_block = NULL;
+ xf86MonPtr tmp = NULL;
+ I2CDevPtr dev = NULL;
+ /* Default DDC and DDC2 to enabled. */
+ Bool noddc = FALSE, noddc2 = FALSE;
+ OptionInfoPtr options;
+
+ options = malloc(sizeof(DDCOptions));
+ if (!options)
+ return NULL;
+ memcpy(options, DDCOptions, sizeof(DDCOptions));
+ xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
+
+ xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
+ xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2);
+ free(options);
+
+ if (noddc || noddc2)
+ return NULL;
+
+ if (!(dev = DDC2Init(scrnIndex, pBus)))
+ return NULL;
+
+ EDID_block = calloc(1, EDID1_LEN);
+ if (!EDID_block)
+ return NULL;
+
+ if (DDC2Read(dev, 0, EDID_block)) {
+ int i, n = EDID_block[0x7e];
+
+ if (complete && n) {
+ EDID_block = realloc(EDID_block, EDID1_LEN * (1+n));
+
+ for (i = 0; i < n; i++)
+ DDC2Read(dev, i+1, EDID_block + (EDID1_LEN * (1+i)));
+ }
+
+ tmp = xf86InterpretEEDID(scrnIndex, EDID_block);
+ }
+
+ if (tmp && complete)
+ tmp->flags |= MONITOR_EDID_COMPLETE_RAWDATA;
+
+ return tmp;
+}
+
+/**
+ * Attempts to probe the monitor for EDID information, if NoDDC and NoDDC2 are
+ * unset. EDID information blocks are interpreted and the results returned in
+ * an xf86MonPtr.
+ *
+ * This function does not affect the list of modes used by drivers -- it is up
+ * to the driver to decide policy on what to do with EDID information.
+ *
+ * @return pointer to a new xf86MonPtr containing the EDID information.
+ * @return NULL if no monitor attached or failure to interpret the EDID.
+ */
+xf86MonPtr
+xf86DoEDID_DDC2(int scrnIndex, I2CBusPtr pBus)
+{
+ return xf86DoEEDID(scrnIndex, pBus, FALSE);
+}
+
+/* XXX write me */
+static void *
+DDC2ReadDisplayID(void)
+{
+ return FALSE;
+}
+
+/**
+ * Attempts to probe the monitor for DisplayID information, if NoDDC and
+ * NoDDC2 are unset. DisplayID blocks are interpreted and the results
+ * returned in an xf86MonPtr.
+ *
+ * This function does not affect the list of modes used by drivers -- it is up
+ * to the driver to decide policy on what to do with DisplayID information.
+ *
+ * @return pointer to a new xf86MonPtr containing the DisplayID information.
+ * @return NULL if no monitor attached or failure to interpret the DisplayID.
+ */
+xf86MonPtr
+xf86DoDisplayID(int scrnIndex, I2CBusPtr pBus)
+{
+ ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
+ unsigned char *did = NULL;
+ xf86MonPtr tmp = NULL;
+ I2CDevPtr dev = NULL;
+ /* Default DDC and DDC2 to enabled. */
+ Bool noddc = FALSE, noddc2 = FALSE;
+ OptionInfoPtr options;
+
+ options = malloc(sizeof(DDCOptions));
+ if (!options)
+ return NULL;
+ memcpy(options, DDCOptions, sizeof(DDCOptions));
+ xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options);
+
+ xf86GetOptValBool(options, DDCOPT_NODDC, &noddc);
+ xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2);
+ free(options);
+
+ if (noddc || noddc2)
+ return NULL;
+
+ if (!(dev = DDC2Init(scrnIndex, pBus)))
+ return NULL;
+
+ if ((did = DDC2ReadDisplayID())) {
+ tmp = calloc(1, sizeof(*tmp));
+ if (!tmp)
+ return NULL;
+
+ tmp->scrnIndex = scrnIndex;
+ tmp->flags |= MONITOR_DISPLAYID;
+ tmp->rawData = did;
+ }
+
+ return tmp;
+}
diff --git a/xorg-server/hw/xfree86/ddc/ddcProperty.c b/xorg-server/hw/xfree86/ddc/ddcProperty.c
index 329a63964..d4ae1006d 100644
--- a/xorg-server/hw/xfree86/ddc/ddcProperty.c
+++ b/xorg-server/hw/xfree86/ddc/ddcProperty.c
@@ -1,125 +1,125 @@
-/*
- * Copyright 2006 Luc Verhaegen.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sub license,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-#ifdef HAVE_XORG_CONFIG_H
-#include <xorg-config.h>
-#endif
-
-#include "xf86.h"
-#include "xf86DDC.h"
-#include <X11/Xatom.h>
-#include "property.h"
-#include "propertyst.h"
-#include "xf86DDC.h"
-#include <string.h>
-
-#define EDID1_ATOM_NAME "XFree86_DDC_EDID1_RAWDATA"
-#define EDID2_ATOM_NAME "XFree86_DDC_EDID2_RAWDATA"
-
-static void
-edidMakeAtom(int i, const char *name, CARD8 *data, int size)
-{
- Atom atom;
- unsigned char *atom_data;
-
- if (!(atom_data = xalloc(size*sizeof(CARD8))))
- return;
-
- atom = MakeAtom(name, strlen(name), TRUE);
- memcpy(atom_data, data, size);
- xf86RegisterRootWindowProperty(i, atom, XA_INTEGER, 8, size, atom_data);
-}
-
-static void
-addRootWindowProperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
-{
- int i, scrnIndex = pScrn->scrnIndex;
- Bool makeEDID1prop = FALSE;
- Bool makeEDID2prop = FALSE;
-
- if (DDC->flags & MONITOR_DISPLAYID) {
- /* Don't bother, use RANDR already */
- return;
- } else if (DDC->ver.version == 1) {
- makeEDID1prop = TRUE;
- } else if (DDC->ver.version == 2) {
- int checksum1;
- int checksum2;
- makeEDID2prop = TRUE;
-
- /* Some monitors (eg Panasonic PanaSync4)
- * report version==2 because they used EDID v2 spec document,
- * although they use EDID v1 data structure :-(
- *
- * Try using checksum to determine when we have such a monitor.
- */
- checksum2 = 0;
- for (i = 0; i < 256; i++)
- checksum2 += DDC->rawData[i];
- if (checksum2 % 256) {
- xf86DrvMsg(scrnIndex, X_INFO, "Monitor EDID v2 checksum failed\n");
- xf86DrvMsg(scrnIndex, X_INFO,
- "XFree86_DDC_EDID2_RAWDATA property may be bad\n");
- checksum1 = 0;
- for (i = 0; i < 128; i++)
- checksum1 += DDC->rawData[i];
- if (!(checksum1 % 256)) {
- xf86DrvMsg(scrnIndex, X_INFO,
- "Monitor EDID v1 checksum passed,\n");
- xf86DrvMsg(scrnIndex, X_INFO,
- "XFree86_DDC_EDID1_RAWDATA property created\n");
- makeEDID1prop = TRUE;
- }
- }
- } else {
- xf86DrvMsg(scrnIndex, X_PROBED, "unexpected EDID version %d.%d\n",
- DDC->ver.version, DDC->ver.revision);
- return;
- }
-
- if (makeEDID1prop) {
- int size = 128 +
- (DDC->flags & EDID_COMPLETE_RAWDATA ? DDC->no_sections * 128 : 0);
-
- edidMakeAtom(scrnIndex, EDID1_ATOM_NAME, DDC->rawData, size);
- }
-
- if (makeEDID2prop)
- edidMakeAtom(scrnIndex, EDID2_ATOM_NAME, DDC->rawData, 256);
-}
-
-Bool
-xf86SetDDCproperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
-{
- if (!pScrn || !pScrn->monitor || !DDC)
- return FALSE;
-
- if (DDC->flags & MONITOR_DISPLAYID)
- ;
- else
- xf86EdidMonitorSet(pScrn->scrnIndex, pScrn->monitor, DDC);
-
- addRootWindowProperties(pScrn, DDC);
-
- return TRUE;
-}
+/*
+ * Copyright 2006 Luc Verhaegen.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sub license,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "xf86.h"
+#include "xf86DDC.h"
+#include <X11/Xatom.h>
+#include "property.h"
+#include "propertyst.h"
+#include "xf86DDC.h"
+#include <string.h>
+
+#define EDID1_ATOM_NAME "XFree86_DDC_EDID1_RAWDATA"
+#define EDID2_ATOM_NAME "XFree86_DDC_EDID2_RAWDATA"
+
+static void
+edidMakeAtom(int i, const char *name, CARD8 *data, int size)
+{
+ Atom atom;
+ unsigned char *atom_data;
+
+ if (!(atom_data = malloc(size*sizeof(CARD8))))
+ return;
+
+ atom = MakeAtom(name, strlen(name), TRUE);
+ memcpy(atom_data, data, size);
+ xf86RegisterRootWindowProperty(i, atom, XA_INTEGER, 8, size, atom_data);
+}
+
+static void
+addRootWindowProperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
+{
+ int i, scrnIndex = pScrn->scrnIndex;
+ Bool makeEDID1prop = FALSE;
+ Bool makeEDID2prop = FALSE;
+
+ if (DDC->flags & MONITOR_DISPLAYID) {
+ /* Don't bother, use RANDR already */
+ return;
+ } else if (DDC->ver.version == 1) {
+ makeEDID1prop = TRUE;
+ } else if (DDC->ver.version == 2) {
+ int checksum1;
+ int checksum2;
+ makeEDID2prop = TRUE;
+
+ /* Some monitors (eg Panasonic PanaSync4)
+ * report version==2 because they used EDID v2 spec document,
+ * although they use EDID v1 data structure :-(
+ *
+ * Try using checksum to determine when we have such a monitor.
+ */
+ checksum2 = 0;
+ for (i = 0; i < 256; i++)
+ checksum2 += DDC->rawData[i];
+ if (checksum2 % 256) {
+ xf86DrvMsg(scrnIndex, X_INFO, "Monitor EDID v2 checksum failed\n");
+ xf86DrvMsg(scrnIndex, X_INFO,
+ "XFree86_DDC_EDID2_RAWDATA property may be bad\n");
+ checksum1 = 0;
+ for (i = 0; i < 128; i++)
+ checksum1 += DDC->rawData[i];
+ if (!(checksum1 % 256)) {
+ xf86DrvMsg(scrnIndex, X_INFO,
+ "Monitor EDID v1 checksum passed,\n");
+ xf86DrvMsg(scrnIndex, X_INFO,
+ "XFree86_DDC_EDID1_RAWDATA property created\n");
+ makeEDID1prop = TRUE;
+ }
+ }
+ } else {
+ xf86DrvMsg(scrnIndex, X_PROBED, "unexpected EDID version %d.%d\n",
+ DDC->ver.version, DDC->ver.revision);
+ return;
+ }
+
+ if (makeEDID1prop) {
+ int size = 128 +
+ (DDC->flags & EDID_COMPLETE_RAWDATA ? DDC->no_sections * 128 : 0);
+
+ edidMakeAtom(scrnIndex, EDID1_ATOM_NAME, DDC->rawData, size);
+ }
+
+ if (makeEDID2prop)
+ edidMakeAtom(scrnIndex, EDID2_ATOM_NAME, DDC->rawData, 256);
+}
+
+Bool
+xf86SetDDCproperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
+{
+ if (!pScrn || !pScrn->monitor || !DDC)
+ return FALSE;
+
+ if (DDC->flags & MONITOR_DISPLAYID)
+ ;
+ else
+ xf86EdidMonitorSet(pScrn->scrnIndex, pScrn->monitor, DDC);
+
+ addRootWindowProperties(pScrn, DDC);
+
+ return TRUE;
+}
diff --git a/xorg-server/hw/xfree86/ddc/interpret_edid.c b/xorg-server/hw/xfree86/ddc/interpret_edid.c
index f3e593aec..1a2d2f487 100644
--- a/xorg-server/hw/xfree86/ddc/interpret_edid.c
+++ b/xorg-server/hw/xfree86/ddc/interpret_edid.c
@@ -1,685 +1,685 @@
-/*
- * Copyright 1998 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
- * Copyright 2007 Red Hat, 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 (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- *
- * interpret_edid.c: interpret a primary EDID block
- */
-
-#ifdef HAVE_XORG_CONFIG_H
-#include <xorg-config.h>
-#endif
-
-#include "misc.h"
-#include "xf86.h"
-#include "xf86_OSproc.h"
-#define _PARSE_EDID_
-#include "xf86DDC.h"
-#include <string.h>
-
-static void get_vendor_section(Uchar*, struct vendor *);
-static void get_version_section(Uchar*, struct edid_version *);
-static void get_display_section(Uchar*, struct disp_features *,
- struct edid_version *);
-static void get_established_timing_section(Uchar*, struct established_timings *);
-static void get_std_timing_section(Uchar*, struct std_timings *,
- struct edid_version *);
-static void fetch_detailed_block(Uchar *c, struct edid_version *ver,
- struct detailed_monitor_section *det_mon);
-static void get_dt_md_section(Uchar *, struct edid_version *,
- struct detailed_monitor_section *det_mon);
-static void copy_string(Uchar *, Uchar *);
-static void get_dst_timing_section(Uchar *, struct std_timings *,
- struct edid_version *);
-static void get_monitor_ranges(Uchar *, struct monitor_ranges *);
-static void get_whitepoint_section(Uchar *, struct whitePoints *);
-static void get_detailed_timing_section(Uchar*, struct detailed_timings *);
-static Bool validate_version(int scrnIndex, struct edid_version *);
-
-static void
-find_ranges_section(struct detailed_monitor_section *det, void *ranges)
-{
- if (det->type == DS_RANGES && det->section.ranges.max_clock)
- *(struct monitor_ranges **)ranges = &det->section.ranges;
-}
-
-static void
-find_max_detailed_clock(struct detailed_monitor_section *det, void *ret)
-{
- if (det->type == DT) {
- *(int *)ret = max(*((int *)ret),
- det->section.d_timings.clock);
- }
-}
-
-static void
-handle_edid_quirks(xf86MonPtr m)
-{
- struct monitor_ranges *ranges = NULL;
-
- /*
- * max_clock is only encoded in EDID in tens of MHz, so occasionally we
- * find a monitor claiming a max of 160 with a mode requiring 162, or
- * similar. Strictly we should refuse to round up too far, but let's
- * see how well this works.
- */
-
- /* Try to find Monitor Range and max clock, then re-set range value*/
- xf86ForEachDetailedBlock(m, find_ranges_section, &ranges);
- if (ranges && ranges->max_clock) {
- int clock = 0;
- xf86ForEachDetailedBlock(m, find_max_detailed_clock, &clock);
- if (clock && (ranges->max_clock * 1e6 < clock)) {
- xf86Msg(X_WARNING, "EDID timing clock %.2f exceeds claimed max "
- "%dMHz, fixing\n", clock / 1.0e6, ranges->max_clock);
- ranges->max_clock = (clock+999999)/1e6;
- }
- }
-}
-
-struct det_hv_parameter {
- int real_hsize;
- int real_vsize;
- float target_aspect;
-};
-
-static void handle_detailed_hvsize(struct detailed_monitor_section *det_mon,
- void *data)
-{
- struct det_hv_parameter *p = (struct det_hv_parameter *)data;
- float timing_aspect;
-
- if (det_mon->type == DT) {
- struct detailed_timings *timing;
- timing = &det_mon->section.d_timings;
-
- if (!timing->v_size)
- return;
-
- timing_aspect = (float)timing->h_size / timing->v_size;
- if (fabs(1 - (timing_aspect / p->target_aspect)) < 0.05) {
- p->real_hsize = max(p->real_hsize, timing->h_size);
- p->real_vsize = max(p->real_vsize, timing->v_size);
- }
- }
-}
-
-static void encode_aspect_ratio(xf86MonPtr m)
-{
- /*
- * some monitors encode the aspect ratio instead of the physical size.
- * try to find the largest detailed timing that matches that aspect
- * ratio and use that to fill in the feature section.
- */
- if ((m->features.hsize == 16 && m->features.vsize == 9) ||
- (m->features.hsize == 16 && m->features.vsize == 10) ||
- (m->features.hsize == 4 && m->features.vsize == 3) ||
- (m->features.hsize == 5 && m->features.vsize == 4)) {
-
- struct det_hv_parameter p;
- p.real_hsize = 0;
- p.real_vsize = 0;
- p.target_aspect = (float)m->features.hsize /m->features.vsize;
-
- xf86ForEachDetailedBlock(m, handle_detailed_hvsize, &p);
-
- if (!p.real_hsize || !p.real_vsize) {
- m->features.hsize = m->features.vsize = 0;
- } else if ((m->features.hsize * 10 == p.real_hsize) &&
- (m->features.vsize * 10 == p.real_vsize)) {
- /* exact match is just unlikely, should do a better check though */
- m->features.hsize = m->features.vsize = 0;
- } else {
- /* convert mm to cm */
- m->features.hsize = (p.real_hsize + 5) / 10;
- m->features.vsize = (p.real_vsize + 5) / 10;
- }
-
- xf86Msg(X_INFO, "Quirked EDID physical size to %dx%d cm\n",
- m->features.hsize, m->features.vsize);
- }
-}
-
-xf86MonPtr
-xf86InterpretEDID(int scrnIndex, Uchar *block)
-{
- xf86MonPtr m;
-
- if (!block) return NULL;
- if (! (m = xnfcalloc(sizeof(xf86Monitor),1))) return NULL;
- m->scrnIndex = scrnIndex;
- m->rawData = block;
-
- get_vendor_section(SECTION(VENDOR_SECTION,block),&m->vendor);
- get_version_section(SECTION(VERSION_SECTION,block),&m->ver);
- if (!validate_version(scrnIndex, &m->ver)) goto error;
- get_display_section(SECTION(DISPLAY_SECTION,block),&m->features,
- &m->ver);
- get_established_timing_section(SECTION(ESTABLISHED_TIMING_SECTION,block),
- &m->timings1);
- get_std_timing_section(SECTION(STD_TIMING_SECTION,block),m->timings2,
- &m->ver);
- get_dt_md_section(SECTION(DET_TIMING_SECTION,block),&m->ver, m->det_mon);
- m->no_sections = (int)*(char *)SECTION(NO_EDID,block);
-
- handle_edid_quirks(m);
- encode_aspect_ratio(m);
-
- return (m);
-
- error:
- xfree(m);
- return NULL;
-}
-
-static int get_cea_detail_timing(Uchar *blk, xf86MonPtr mon,
- struct detailed_monitor_section *det_mon)
-{
- int dt_num;
- int dt_offset = ((struct cea_ext_body *)blk)->dt_offset;
-
- dt_num = 0;
-
- if (dt_offset < CEA_EXT_MIN_DATA_OFFSET)
- return dt_num;
-
- for (; dt_offset < (CEA_EXT_MAX_DATA_OFFSET - DET_TIMING_INFO_LEN) &&
- dt_num < CEA_EXT_DET_TIMING_NUM;
- _NEXT_DT_MD_SECTION(dt_offset)) {
-
- fetch_detailed_block(blk + dt_offset, &mon->ver, det_mon + dt_num);
- dt_num = dt_num + 1 ;
- }
-
- return dt_num;
-}
-
-static void handle_cea_detail_block(Uchar *ext, xf86MonPtr mon,
- handle_detailed_fn fn,
- void *data)
-{
- int i;
- struct detailed_monitor_section det_mon[CEA_EXT_DET_TIMING_NUM];
- int det_mon_num;
-
- det_mon_num = get_cea_detail_timing(ext, mon, det_mon);
-
- for (i = 0; i < det_mon_num; i++)
- fn(det_mon + i, data);
-}
-
-void xf86ForEachDetailedBlock(xf86MonPtr mon,
- handle_detailed_fn fn,
- void *data)
-{
- int i;
- Uchar *ext;
-
- if (mon == NULL)
- return;
-
- for (i = 0; i < DET_TIMINGS; i++)
- fn(mon->det_mon + i, data);
-
- for (i = 0; i < mon->no_sections; i++) {
- ext = mon->rawData + EDID1_LEN * (i + 1);
- switch (ext[EXT_TAG]){
- case CEA_EXT:
- handle_cea_detail_block(ext, mon, fn, data);
- break;
- case VTB_EXT:
- case DI_EXT:
- case LS_EXT:
- case MI_EXT:
- break;
- }
- }
-}
-
-static struct cea_data_block *
-extract_cea_data_block(Uchar *ext, int data_type)
-{
- struct cea_ext_body *cea;
- struct cea_data_block *data_collection;
- struct cea_data_block *data_end;
-
- cea = (struct cea_ext_body *)ext;
-
- if (cea->dt_offset <= CEA_EXT_MIN_DATA_OFFSET)
- return NULL;
-
- data_collection = &cea->data_collection;
- data_end = (struct cea_data_block *)(cea->dt_offset + ext);
-
- for ( ;data_collection < data_end;) {
-
- if (data_type == data_collection->tag) {
- return data_collection;
- }
- data_collection = (void *)((unsigned char *)data_collection +
- data_collection->len + 1);
- }
-
- return NULL;
-}
-
-static void handle_cea_video_block(Uchar *ext, handle_video_fn fn, void *data)
-{
- struct cea_video_block *video;
- struct cea_video_block *video_end;
- struct cea_data_block *data_collection;
-
- data_collection = extract_cea_data_block(ext, CEA_VIDEO_BLK);
- if (data_collection == NULL)
- return;
-
- video = &data_collection->u.video;
- video_end = (struct cea_video_block *)
- ((Uchar *)video + data_collection->len);
-
- for (; video < video_end; video = video + 1) {
- fn(video, data);
- }
-}
-
-void xf86ForEachVideoBlock(xf86MonPtr mon,
- handle_video_fn fn,
- void *data)
-{
- int i;
- Uchar *ext;
-
- if (mon == NULL)
- return;
-
- for (i = 0; i < mon->no_sections; i++) {
- ext = mon->rawData + EDID1_LEN * (i + 1);
- switch (ext[EXT_TAG]) {
- case CEA_EXT:
- handle_cea_video_block(ext, fn, data);
- break;
- case VTB_EXT:
- case DI_EXT:
- case LS_EXT:
- case MI_EXT:
- break;
- }
- }
-}
-
-xf86MonPtr
-xf86InterpretEEDID(int scrnIndex, Uchar *block)
-{
- xf86MonPtr m;
-
- m = xf86InterpretEDID(scrnIndex, block);
- if (!m)
- return NULL;
-
- /* extension parse */
-
- return m;
-}
-
-static void
-get_vendor_section(Uchar *c, struct vendor *r)
-{
- r->name[0] = L1;
- r->name[1] = L2;
- r->name[2] = L3;
- r->name[3] = '\0';
-
- r->prod_id = PROD_ID;
- r->serial = SERIAL_NO;
- r->week = WEEK;
- r->year = YEAR;
-}
-
-static void
-get_version_section(Uchar *c, struct edid_version *r)
-{
- r->version = VERSION;
- r->revision = REVISION;
-}
-
-static void
-get_display_section(Uchar *c, struct disp_features *r,
- struct edid_version *v)
-{
- r->input_type = INPUT_TYPE;
- if (!DIGITAL(r->input_type)) {
- r->input_voltage = INPUT_VOLTAGE;
- r->input_setup = SETUP;
- r->input_sync = SYNC;
- } else if (v->revision == 2 || v->revision == 3) {
- r->input_dfp = DFP;
- } else if (v->revision >= 4) {
- r->input_bpc = BPC;
- r->input_interface = DIGITAL_INTERFACE;
- }
- r->hsize = HSIZE_MAX;
- r->vsize = VSIZE_MAX;
- r->gamma = GAMMA;
- r->dpms = DPMS;
- r->display_type = DISPLAY_TYPE;
- r->msc = MSC;
- r->redx = REDX;
- r->redy = REDY;
- r->greenx = GREENX;
- r->greeny = GREENY;
- r->bluex = BLUEX;
- r->bluey = BLUEY;
- r->whitex = WHITEX;
- r->whitey = WHITEY;
-}
-
-static void
-get_established_timing_section(Uchar *c, struct established_timings *r)
-{
- r->t1 = T1;
- r->t2 = T2;
- r->t_manu = T_MANU;
-}
-
-static void
-get_cvt_timing_section(Uchar *c, struct cvt_timings *r)
-{
- int i;
-
- for (i = 0; i < 4; i++) {
- if (c[0] && c[1] && c[2]) {
- r[i].height = (c[0] + ((c[1] & 0xF0) << 8) + 1) * 2;
- switch (c[1] & 0xc0) {
- case 0x00: r[i].width = r[i].height * 4 / 3; break;
- case 0x40: r[i].width = r[i].height * 16 / 9; break;
- case 0x80: r[i].width = r[i].height * 16 / 10; break;
- case 0xc0: r[i].width = r[i].height * 15 / 9; break;
- }
- switch (c[2] & 0x60) {
- case 0x00: r[i].rate = 50; break;
- case 0x20: r[i].rate = 60; break;
- case 0x40: r[i].rate = 75; break;
- case 0x60: r[i].rate = 85; break;
- }
- r[i].rates = c[2] & 0x1f;
- } else {
- return;
- }
- c += 3;
- }
-}
-
-static void
-get_std_timing_section(Uchar *c, struct std_timings *r,
- struct edid_version *v)
-{
- int i;
-
- for (i=0;i<STD_TIMINGS;i++){
- if (VALID_TIMING) {
- r[i].hsize = HSIZE1;
- VSIZE1(r[i].vsize);
- r[i].refresh = REFRESH_R;
- r[i].id = STD_TIMING_ID;
- } else {
- r[i].hsize = r[i].vsize = r[i].refresh = r[i].id = 0;
- }
- NEXT_STD_TIMING;
- }
-}
-
-static const unsigned char empty_block[18];
-
-static void
-fetch_detailed_block(Uchar *c, struct edid_version *ver,
- struct detailed_monitor_section *det_mon)
-{
- if (ver->version == 1 && ver->revision >= 1 && IS_MONITOR_DESC) {
- switch (MONITOR_DESC_TYPE) {
- case SERIAL_NUMBER:
- det_mon->type = DS_SERIAL;
- copy_string(c,det_mon->section.serial);
- break;
- case ASCII_STR:
- det_mon->type = DS_ASCII_STR;
- copy_string(c,det_mon->section.ascii_data);
- break;
- case MONITOR_RANGES:
- det_mon->type = DS_RANGES;
- get_monitor_ranges(c,&det_mon->section.ranges);
- break;
- case MONITOR_NAME:
- det_mon->type = DS_NAME;
- copy_string(c,det_mon->section.name);
- break;
- case ADD_COLOR_POINT:
- det_mon->type = DS_WHITE_P;
- get_whitepoint_section(c,det_mon->section.wp);
- break;
- case ADD_STD_TIMINGS:
- det_mon->type = DS_STD_TIMINGS;
- get_dst_timing_section(c,det_mon->section.std_t, ver);
- break;
- case COLOR_MANAGEMENT_DATA:
- det_mon->type = DS_CMD;
- break;
- case CVT_3BYTE_DATA:
- det_mon->type = DS_CVT;
- get_cvt_timing_section(c, det_mon->section.cvt);
- break;
- case ADD_EST_TIMINGS:
- det_mon->type = DS_EST_III;
- memcpy(det_mon->section.est_iii, c + 6, 6);
- break;
- case ADD_DUMMY:
- det_mon->type = DS_DUMMY;
- break;
- default:
- det_mon->type = DS_UNKOWN;
- break;
- }
- if (c[3] <= 0x0F && memcmp(c, empty_block, sizeof(empty_block))) {
- det_mon->type = DS_VENDOR + c[3];
- }
- } else {
- det_mon->type = DT;
- get_detailed_timing_section(c, &det_mon->section.d_timings);
- }
-}
-
-static void
-get_dt_md_section(Uchar *c, struct edid_version *ver,
- struct detailed_monitor_section *det_mon)
-{
- int i;
-
- for (i=0; i < DET_TIMINGS; i++) {
- fetch_detailed_block(c, ver, det_mon + i);
- NEXT_DT_MD_SECTION;
- }
-}
-
-static void
-copy_string(Uchar *c, Uchar *s)
-{
- int i;
- c = c + 5;
- for (i = 0; (i < 13 && *c != 0x0A); i++)
- *(s++) = *(c++);
- *s = 0;
- while (i-- && (*--s == 0x20)) *s = 0;
-}
-
-static void
-get_dst_timing_section(Uchar *c, struct std_timings *t,
- struct edid_version *v)
-{
- int j;
- c = c + 5;
- for (j = 0; j < 5; j++) {
- t[j].hsize = HSIZE1;
- VSIZE1(t[j].vsize);
- t[j].refresh = REFRESH_R;
- t[j].id = STD_TIMING_ID;
- NEXT_STD_TIMING;
- }
-}
-
-static void
-get_monitor_ranges(Uchar *c, struct monitor_ranges *r)
-{
- r->min_v = MIN_V;
- r->max_v = MAX_V;
- r->min_h = MIN_H;
- r->max_h = MAX_H;
- r->max_clock = 0;
- if(MAX_CLOCK != 0xff) /* is specified? */
- r->max_clock = MAX_CLOCK * 10;
- if (HAVE_2ND_GTF) {
- r->gtf_2nd_f = F_2ND_GTF;
- r->gtf_2nd_c = C_2ND_GTF;
- r->gtf_2nd_m = M_2ND_GTF;
- r->gtf_2nd_k = K_2ND_GTF;
- r->gtf_2nd_j = J_2ND_GTF;
- } else {
- r->gtf_2nd_f = 0;
- }
- if (HAVE_CVT) {
- r->max_clock_khz = MAX_CLOCK_KHZ;
- r->max_clock = r->max_clock_khz / 1000;
- r->maxwidth = MAXWIDTH;
- r->supported_aspect = SUPPORTED_ASPECT;
- r->preferred_aspect = PREFERRED_ASPECT;
- r->supported_blanking = SUPPORTED_BLANKING;
- r->supported_scaling = SUPPORTED_SCALING;
- r->preferred_refresh = PREFERRED_REFRESH;
- } else {
- r->max_clock_khz = 0;
- }
-}
-
-static void
-get_whitepoint_section(Uchar *c, struct whitePoints *wp)
-{
- wp[0].white_x = WHITEX1;
- wp[0].white_y = WHITEY1;
- wp[1].white_x = WHITEX2;
- wp[1].white_y = WHITEY2;
- wp[0].index = WHITE_INDEX1;
- wp[1].index = WHITE_INDEX2;
- wp[0].white_gamma = WHITE_GAMMA1;
- wp[1].white_gamma = WHITE_GAMMA2;
-}
-
-static void
-get_detailed_timing_section(Uchar *c, struct detailed_timings *r)
-{
- r->clock = PIXEL_CLOCK;
- r->h_active = H_ACTIVE;
- r->h_blanking = H_BLANK;
- r->v_active = V_ACTIVE;
- r->v_blanking = V_BLANK;
- r->h_sync_off = H_SYNC_OFF;
- r->h_sync_width = H_SYNC_WIDTH;
- r->v_sync_off = V_SYNC_OFF;
- r->v_sync_width = V_SYNC_WIDTH;
- r->h_size = H_SIZE;
- r->v_size = V_SIZE;
- r->h_border = H_BORDER;
- r->v_border = V_BORDER;
- r->interlaced = INTERLACED;
- r->stereo = STEREO;
- r->stereo_1 = STEREO1;
- r->sync = SYNC_T;
- r->misc = MISC;
-}
-
-#define MAX_EDID_MINOR 4
-
-static Bool
-validate_version(int scrnIndex, struct edid_version *r)
-{
- if (r->version != 1) {
- xf86DrvMsg(scrnIndex, X_ERROR, "Unknown EDID version %d\n",
- r->version);
- return FALSE;
- }
-
- if (r->revision > MAX_EDID_MINOR)
- xf86DrvMsg(scrnIndex, X_WARNING,
- "Assuming version 1.%d is compatible with 1.%d\n",
- r->revision, MAX_EDID_MINOR);
-
- return TRUE;
-}
-
-/*
- * Returns true if HDMI, false if definitely not or unknown.
- */
-Bool
-xf86MonitorIsHDMI(xf86MonPtr mon)
-{
- int i = 0, version, offset;
- char *edid = NULL;
-
- if (!mon)
- return FALSE;
-
- if (!(mon->flags & EDID_COMPLETE_RAWDATA))
- return FALSE;
-
- if (!mon->no_sections)
- return FALSE;
-
- edid = (char *)mon->rawData;
- if (!edid)
- return FALSE;
-
- /* find the CEA extension block */
- for (i = 1; i <= mon->no_sections; i++)
- if (edid[i * 128] == 0x02)
- break;
- if (i == mon->no_sections + 1)
- return FALSE;
- edid += (i * 128);
-
- version = edid[1];
- offset = edid[2];
- if (version < 3 || offset < 4)
- return FALSE;
-
- /* walk the cea data blocks */
- for (i = 4; i < offset; i += (edid[i] & 0x1f) + 1) {
- char *x = edid + i;
-
- /* find a vendor specific block */
- if ((x[0] & 0xe0) >> 5 == 0x03) {
- int oui = (x[3] << 16) + (x[2] << 8) + x[1];
-
- /* find the HDMI vendor OUI */
- if (oui == 0x000c03)
- return TRUE;
- }
- }
-
- /* guess it's not HDMI after all */
- return FALSE;
-}
+/*
+ * Copyright 1998 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
+ * Copyright 2007 Red Hat, 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 (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * interpret_edid.c: interpret a primary EDID block
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "misc.h"
+#include "xf86.h"
+#include "xf86_OSproc.h"
+#define _PARSE_EDID_
+#include "xf86DDC.h"
+#include <string.h>
+
+static void get_vendor_section(Uchar*, struct vendor *);
+static void get_version_section(Uchar*, struct edid_version *);
+static void get_display_section(Uchar*, struct disp_features *,
+ struct edid_version *);
+static void get_established_timing_section(Uchar*, struct established_timings *);
+static void get_std_timing_section(Uchar*, struct std_timings *,
+ struct edid_version *);
+static void fetch_detailed_block(Uchar *c, struct edid_version *ver,
+ struct detailed_monitor_section *det_mon);
+static void get_dt_md_section(Uchar *, struct edid_version *,
+ struct detailed_monitor_section *det_mon);
+static void copy_string(Uchar *, Uchar *);
+static void get_dst_timing_section(Uchar *, struct std_timings *,
+ struct edid_version *);
+static void get_monitor_ranges(Uchar *, struct monitor_ranges *);
+static void get_whitepoint_section(Uchar *, struct whitePoints *);
+static void get_detailed_timing_section(Uchar*, struct detailed_timings *);
+static Bool validate_version(int scrnIndex, struct edid_version *);
+
+static void
+find_ranges_section(struct detailed_monitor_section *det, void *ranges)
+{
+ if (det->type == DS_RANGES && det->section.ranges.max_clock)
+ *(struct monitor_ranges **)ranges = &det->section.ranges;
+}
+
+static void
+find_max_detailed_clock(struct detailed_monitor_section *det, void *ret)
+{
+ if (det->type == DT) {
+ *(int *)ret = max(*((int *)ret),
+ det->section.d_timings.clock);
+ }
+}
+
+static void
+handle_edid_quirks(xf86MonPtr m)
+{
+ struct monitor_ranges *ranges = NULL;
+
+ /*
+ * max_clock is only encoded in EDID in tens of MHz, so occasionally we
+ * find a monitor claiming a max of 160 with a mode requiring 162, or
+ * similar. Strictly we should refuse to round up too far, but let's
+ * see how well this works.
+ */
+
+ /* Try to find Monitor Range and max clock, then re-set range value*/
+ xf86ForEachDetailedBlock(m, find_ranges_section, &ranges);
+ if (ranges && ranges->max_clock) {
+ int clock = 0;
+ xf86ForEachDetailedBlock(m, find_max_detailed_clock, &clock);
+ if (clock && (ranges->max_clock * 1e6 < clock)) {
+ xf86Msg(X_WARNING, "EDID timing clock %.2f exceeds claimed max "
+ "%dMHz, fixing\n", clock / 1.0e6, ranges->max_clock);
+ ranges->max_clock = (clock+999999)/1e6;
+ }
+ }
+}
+
+struct det_hv_parameter {
+ int real_hsize;
+ int real_vsize;
+ float target_aspect;
+};
+
+static void handle_detailed_hvsize(struct detailed_monitor_section *det_mon,
+ void *data)
+{
+ struct det_hv_parameter *p = (struct det_hv_parameter *)data;
+ float timing_aspect;
+
+ if (det_mon->type == DT) {
+ struct detailed_timings *timing;
+ timing = &det_mon->section.d_timings;
+
+ if (!timing->v_size)
+ return;
+
+ timing_aspect = (float)timing->h_size / timing->v_size;
+ if (fabs(1 - (timing_aspect / p->target_aspect)) < 0.05) {
+ p->real_hsize = max(p->real_hsize, timing->h_size);
+ p->real_vsize = max(p->real_vsize, timing->v_size);
+ }
+ }
+}
+
+static void encode_aspect_ratio(xf86MonPtr m)
+{
+ /*
+ * some monitors encode the aspect ratio instead of the physical size.
+ * try to find the largest detailed timing that matches that aspect
+ * ratio and use that to fill in the feature section.
+ */
+ if ((m->features.hsize == 16 && m->features.vsize == 9) ||
+ (m->features.hsize == 16 && m->features.vsize == 10) ||
+ (m->features.hsize == 4 && m->features.vsize == 3) ||
+ (m->features.hsize == 5 && m->features.vsize == 4)) {
+
+ struct det_hv_parameter p;
+ p.real_hsize = 0;
+ p.real_vsize = 0;
+ p.target_aspect = (float)m->features.hsize /m->features.vsize;
+
+ xf86ForEachDetailedBlock(m, handle_detailed_hvsize, &p);
+
+ if (!p.real_hsize || !p.real_vsize) {
+ m->features.hsize = m->features.vsize = 0;
+ } else if ((m->features.hsize * 10 == p.real_hsize) &&
+ (m->features.vsize * 10 == p.real_vsize)) {
+ /* exact match is just unlikely, should do a better check though */
+ m->features.hsize = m->features.vsize = 0;
+ } else {
+ /* convert mm to cm */
+ m->features.hsize = (p.real_hsize + 5) / 10;
+ m->features.vsize = (p.real_vsize + 5) / 10;
+ }
+
+ xf86Msg(X_INFO, "Quirked EDID physical size to %dx%d cm\n",
+ m->features.hsize, m->features.vsize);
+ }
+}
+
+xf86MonPtr
+xf86InterpretEDID(int scrnIndex, Uchar *block)
+{
+ xf86MonPtr m;
+
+ if (!block) return NULL;
+ if (! (m = xnfcalloc(sizeof(xf86Monitor),1))) return NULL;
+ m->scrnIndex = scrnIndex;
+ m->rawData = block;
+
+ get_vendor_section(SECTION(VENDOR_SECTION,block),&m->vendor);
+ get_version_section(SECTION(VERSION_SECTION,block),&m->ver);
+ if (!validate_version(scrnIndex, &m->ver)) goto error;
+ get_display_section(SECTION(DISPLAY_SECTION,block),&m->features,
+ &m->ver);
+ get_established_timing_section(SECTION(ESTABLISHED_TIMING_SECTION,block),
+ &m->timings1);
+ get_std_timing_section(SECTION(STD_TIMING_SECTION,block),m->timings2,
+ &m->ver);
+ get_dt_md_section(SECTION(DET_TIMING_SECTION,block),&m->ver, m->det_mon);
+ m->no_sections = (int)*(char *)SECTION(NO_EDID,block);
+
+ handle_edid_quirks(m);
+ encode_aspect_ratio(m);
+
+ return (m);
+
+ error:
+ free(m);
+ return NULL;
+}
+
+static int get_cea_detail_timing(Uchar *blk, xf86MonPtr mon,
+ struct detailed_monitor_section *det_mon)
+{
+ int dt_num;
+ int dt_offset = ((struct cea_ext_body *)blk)->dt_offset;
+
+ dt_num = 0;
+
+ if (dt_offset < CEA_EXT_MIN_DATA_OFFSET)
+ return dt_num;
+
+ for (; dt_offset < (CEA_EXT_MAX_DATA_OFFSET - DET_TIMING_INFO_LEN) &&
+ dt_num < CEA_EXT_DET_TIMING_NUM;
+ _NEXT_DT_MD_SECTION(dt_offset)) {
+
+ fetch_detailed_block(blk + dt_offset, &mon->ver, det_mon + dt_num);
+ dt_num = dt_num + 1 ;
+ }
+
+ return dt_num;
+}
+
+static void handle_cea_detail_block(Uchar *ext, xf86MonPtr mon,
+ handle_detailed_fn fn,
+ void *data)
+{
+ int i;
+ struct detailed_monitor_section det_mon[CEA_EXT_DET_TIMING_NUM];
+ int det_mon_num;
+
+ det_mon_num = get_cea_detail_timing(ext, mon, det_mon);
+
+ for (i = 0; i < det_mon_num; i++)
+ fn(det_mon + i, data);
+}
+
+void xf86ForEachDetailedBlock(xf86MonPtr mon,
+ handle_detailed_fn fn,
+ void *data)
+{
+ int i;
+ Uchar *ext;
+
+ if (mon == NULL)
+ return;
+
+ for (i = 0; i < DET_TIMINGS; i++)
+ fn(mon->det_mon + i, data);
+
+ for (i = 0; i < mon->no_sections; i++) {
+ ext = mon->rawData + EDID1_LEN * (i + 1);
+ switch (ext[EXT_TAG]){
+ case CEA_EXT:
+ handle_cea_detail_block(ext, mon, fn, data);
+ break;
+ case VTB_EXT:
+ case DI_EXT:
+ case LS_EXT:
+ case MI_EXT:
+ break;
+ }
+ }
+}
+
+static struct cea_data_block *
+extract_cea_data_block(Uchar *ext, int data_type)
+{
+ struct cea_ext_body *cea;
+ struct cea_data_block *data_collection;
+ struct cea_data_block *data_end;
+
+ cea = (struct cea_ext_body *)ext;
+
+ if (cea->dt_offset <= CEA_EXT_MIN_DATA_OFFSET)
+ return NULL;
+
+ data_collection = &cea->data_collection;
+ data_end = (struct cea_data_block *)(cea->dt_offset + ext);
+
+ for ( ;data_collection < data_end;) {
+
+ if (data_type == data_collection->tag) {
+ return data_collection;
+ }
+ data_collection = (void *)((unsigned char *)data_collection +
+ data_collection->len + 1);
+ }
+
+ return NULL;
+}
+
+static void handle_cea_video_block(Uchar *ext, handle_video_fn fn, void *data)
+{
+ struct cea_video_block *video;
+ struct cea_video_block *video_end;
+ struct cea_data_block *data_collection;
+
+ data_collection = extract_cea_data_block(ext, CEA_VIDEO_BLK);
+ if (data_collection == NULL)
+ return;
+
+ video = &data_collection->u.video;
+ video_end = (struct cea_video_block *)
+ ((Uchar *)video + data_collection->len);
+
+ for (; video < video_end; video = video + 1) {
+ fn(video, data);
+ }
+}
+
+void xf86ForEachVideoBlock(xf86MonPtr mon,
+ handle_video_fn fn,
+ void *data)
+{
+ int i;
+ Uchar *ext;
+
+ if (mon == NULL)
+ return;
+
+ for (i = 0; i < mon->no_sections; i++) {
+ ext = mon->rawData + EDID1_LEN * (i + 1);
+ switch (ext[EXT_TAG]) {
+ case CEA_EXT:
+ handle_cea_video_block(ext, fn, data);
+ break;
+ case VTB_EXT:
+ case DI_EXT:
+ case LS_EXT:
+ case MI_EXT:
+ break;
+ }
+ }
+}
+
+xf86MonPtr
+xf86InterpretEEDID(int scrnIndex, Uchar *block)
+{
+ xf86MonPtr m;
+
+ m = xf86InterpretEDID(scrnIndex, block);
+ if (!m)
+ return NULL;
+
+ /* extension parse */
+
+ return m;
+}
+
+static void
+get_vendor_section(Uchar *c, struct vendor *r)
+{
+ r->name[0] = L1;
+ r->name[1] = L2;
+ r->name[2] = L3;
+ r->name[3] = '\0';
+
+ r->prod_id = PROD_ID;
+ r->serial = SERIAL_NO;
+ r->week = WEEK;
+ r->year = YEAR;
+}
+
+static void
+get_version_section(Uchar *c, struct edid_version *r)
+{
+ r->version = VERSION;
+ r->revision = REVISION;
+}
+
+static void
+get_display_section(Uchar *c, struct disp_features *r,
+ struct edid_version *v)
+{
+ r->input_type = INPUT_TYPE;
+ if (!DIGITAL(r->input_type)) {
+ r->input_voltage = INPUT_VOLTAGE;
+ r->input_setup = SETUP;
+ r->input_sync = SYNC;
+ } else if (v->revision == 2 || v->revision == 3) {
+ r->input_dfp = DFP;
+ } else if (v->revision >= 4) {
+ r->input_bpc = BPC;
+ r->input_interface = DIGITAL_INTERFACE;
+ }
+ r->hsize = HSIZE_MAX;
+ r->vsize = VSIZE_MAX;
+ r->gamma = GAMMA;
+ r->dpms = DPMS;
+ r->display_type = DISPLAY_TYPE;
+ r->msc = MSC;
+ r->redx = REDX;
+ r->redy = REDY;
+ r->greenx = GREENX;
+ r->greeny = GREENY;
+ r->bluex = BLUEX;
+ r->bluey = BLUEY;
+ r->whitex = WHITEX;
+ r->whitey = WHITEY;
+}
+
+static void
+get_established_timing_section(Uchar *c, struct established_timings *r)
+{
+ r->t1 = T1;
+ r->t2 = T2;
+ r->t_manu = T_MANU;
+}
+
+static void
+get_cvt_timing_section(Uchar *c, struct cvt_timings *r)
+{
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ if (c[0] && c[1] && c[2]) {
+ r[i].height = (c[0] + ((c[1] & 0xF0) << 8) + 1) * 2;
+ switch (c[1] & 0xc0) {
+ case 0x00: r[i].width = r[i].height * 4 / 3; break;
+ case 0x40: r[i].width = r[i].height * 16 / 9; break;
+ case 0x80: r[i].width = r[i].height * 16 / 10; break;
+ case 0xc0: r[i].width = r[i].height * 15 / 9; break;
+ }
+ switch (c[2] & 0x60) {
+ case 0x00: r[i].rate = 50; break;
+ case 0x20: r[i].rate = 60; break;
+ case 0x40: r[i].rate = 75; break;
+ case 0x60: r[i].rate = 85; break;
+ }
+ r[i].rates = c[2] & 0x1f;
+ } else {
+ return;
+ }
+ c += 3;
+ }
+}
+
+static void
+get_std_timing_section(Uchar *c, struct std_timings *r,
+ struct edid_version *v)
+{
+ int i;
+
+ for (i=0;i<STD_TIMINGS;i++){
+ if (VALID_TIMING) {
+ r[i].hsize = HSIZE1;
+ VSIZE1(r[i].vsize);
+ r[i].refresh = REFRESH_R;
+ r[i].id = STD_TIMING_ID;
+ } else {
+ r[i].hsize = r[i].vsize = r[i].refresh = r[i].id = 0;
+ }
+ NEXT_STD_TIMING;
+ }
+}
+
+static const unsigned char empty_block[18];
+
+static void
+fetch_detailed_block(Uchar *c, struct edid_version *ver,
+ struct detailed_monitor_section *det_mon)
+{
+ if (ver->version == 1 && ver->revision >= 1 && IS_MONITOR_DESC) {
+ switch (MONITOR_DESC_TYPE) {
+ case SERIAL_NUMBER:
+ det_mon->type = DS_SERIAL;
+ copy_string(c,det_mon->section.serial);
+ break;
+ case ASCII_STR:
+ det_mon->type = DS_ASCII_STR;
+ copy_string(c,det_mon->section.ascii_data);
+ break;
+ case MONITOR_RANGES:
+ det_mon->type = DS_RANGES;
+ get_monitor_ranges(c,&det_mon->section.ranges);
+ break;
+ case MONITOR_NAME:
+ det_mon->type = DS_NAME;
+ copy_string(c,det_mon->section.name);
+ break;
+ case ADD_COLOR_POINT:
+ det_mon->type = DS_WHITE_P;
+ get_whitepoint_section(c,det_mon->section.wp);
+ break;
+ case ADD_STD_TIMINGS:
+ det_mon->type = DS_STD_TIMINGS;
+ get_dst_timing_section(c,det_mon->section.std_t, ver);
+ break;
+ case COLOR_MANAGEMENT_DATA:
+ det_mon->type = DS_CMD;
+ break;
+ case CVT_3BYTE_DATA:
+ det_mon->type = DS_CVT;
+ get_cvt_timing_section(c, det_mon->section.cvt);
+ break;
+ case ADD_EST_TIMINGS:
+ det_mon->type = DS_EST_III;
+ memcpy(det_mon->section.est_iii, c + 6, 6);
+ break;
+ case ADD_DUMMY:
+ det_mon->type = DS_DUMMY;
+ break;
+ default:
+ det_mon->type = DS_UNKOWN;
+ break;
+ }
+ if (c[3] <= 0x0F && memcmp(c, empty_block, sizeof(empty_block))) {
+ det_mon->type = DS_VENDOR + c[3];
+ }
+ } else {
+ det_mon->type = DT;
+ get_detailed_timing_section(c, &det_mon->section.d_timings);
+ }
+}
+
+static void
+get_dt_md_section(Uchar *c, struct edid_version *ver,
+ struct detailed_monitor_section *det_mon)
+{
+ int i;
+
+ for (i=0; i < DET_TIMINGS; i++) {
+ fetch_detailed_block(c, ver, det_mon + i);
+ NEXT_DT_MD_SECTION;
+ }
+}
+
+static void
+copy_string(Uchar *c, Uchar *s)
+{
+ int i;
+ c = c + 5;
+ for (i = 0; (i < 13 && *c != 0x0A); i++)
+ *(s++) = *(c++);
+ *s = 0;
+ while (i-- && (*--s == 0x20)) *s = 0;
+}
+
+static void
+get_dst_timing_section(Uchar *c, struct std_timings *t,
+ struct edid_version *v)
+{
+ int j;
+ c = c + 5;
+ for (j = 0; j < 5; j++) {
+ t[j].hsize = HSIZE1;
+ VSIZE1(t[j].vsize);
+ t[j].refresh = REFRESH_R;
+ t[j].id = STD_TIMING_ID;
+ NEXT_STD_TIMING;
+ }
+}
+
+static void
+get_monitor_ranges(Uchar *c, struct monitor_ranges *r)
+{
+ r->min_v = MIN_V;
+ r->max_v = MAX_V;
+ r->min_h = MIN_H;
+ r->max_h = MAX_H;
+ r->max_clock = 0;
+ if(MAX_CLOCK != 0xff) /* is specified? */
+ r->max_clock = MAX_CLOCK * 10;
+ if (HAVE_2ND_GTF) {
+ r->gtf_2nd_f = F_2ND_GTF;
+ r->gtf_2nd_c = C_2ND_GTF;
+ r->gtf_2nd_m = M_2ND_GTF;
+ r->gtf_2nd_k = K_2ND_GTF;
+ r->gtf_2nd_j = J_2ND_GTF;
+ } else {
+ r->gtf_2nd_f = 0;
+ }
+ if (HAVE_CVT) {
+ r->max_clock_khz = MAX_CLOCK_KHZ;
+ r->max_clock = r->max_clock_khz / 1000;
+ r->maxwidth = MAXWIDTH;
+ r->supported_aspect = SUPPORTED_ASPECT;
+ r->preferred_aspect = PREFERRED_ASPECT;
+ r->supported_blanking = SUPPORTED_BLANKING;
+ r->supported_scaling = SUPPORTED_SCALING;
+ r->preferred_refresh = PREFERRED_REFRESH;
+ } else {
+ r->max_clock_khz = 0;
+ }
+}
+
+static void
+get_whitepoint_section(Uchar *c, struct whitePoints *wp)
+{
+ wp[0].white_x = WHITEX1;
+ wp[0].white_y = WHITEY1;
+ wp[1].white_x = WHITEX2;
+ wp[1].white_y = WHITEY2;
+ wp[0].index = WHITE_INDEX1;
+ wp[1].index = WHITE_INDEX2;
+ wp[0].white_gamma = WHITE_GAMMA1;
+ wp[1].white_gamma = WHITE_GAMMA2;
+}
+
+static void
+get_detailed_timing_section(Uchar *c, struct detailed_timings *r)
+{
+ r->clock = PIXEL_CLOCK;
+ r->h_active = H_ACTIVE;
+ r->h_blanking = H_BLANK;
+ r->v_active = V_ACTIVE;
+ r->v_blanking = V_BLANK;
+ r->h_sync_off = H_SYNC_OFF;
+ r->h_sync_width = H_SYNC_WIDTH;
+ r->v_sync_off = V_SYNC_OFF;
+ r->v_sync_width = V_SYNC_WIDTH;
+ r->h_size = H_SIZE;
+ r->v_size = V_SIZE;
+ r->h_border = H_BORDER;
+ r->v_border = V_BORDER;
+ r->interlaced = INTERLACED;
+ r->stereo = STEREO;
+ r->stereo_1 = STEREO1;
+ r->sync = SYNC_T;
+ r->misc = MISC;
+}
+
+#define MAX_EDID_MINOR 4
+
+static Bool
+validate_version(int scrnIndex, struct edid_version *r)
+{
+ if (r->version != 1) {
+ xf86DrvMsg(scrnIndex, X_ERROR, "Unknown EDID version %d\n",
+ r->version);
+ return FALSE;
+ }
+
+ if (r->revision > MAX_EDID_MINOR)
+ xf86DrvMsg(scrnIndex, X_WARNING,
+ "Assuming version 1.%d is compatible with 1.%d\n",
+ r->revision, MAX_EDID_MINOR);
+
+ return TRUE;
+}
+
+/*
+ * Returns true if HDMI, false if definitely not or unknown.
+ */
+Bool
+xf86MonitorIsHDMI(xf86MonPtr mon)
+{
+ int i = 0, version, offset;
+ char *edid = NULL;
+
+ if (!mon)
+ return FALSE;
+
+ if (!(mon->flags & EDID_COMPLETE_RAWDATA))
+ return FALSE;
+
+ if (!mon->no_sections)
+ return FALSE;
+
+ edid = (char *)mon->rawData;
+ if (!edid)
+ return FALSE;
+
+ /* find the CEA extension block */
+ for (i = 1; i <= mon->no_sections; i++)
+ if (edid[i * 128] == 0x02)
+ break;
+ if (i == mon->no_sections + 1)
+ return FALSE;
+ edid += (i * 128);
+
+ version = edid[1];
+ offset = edid[2];
+ if (version < 3 || offset < 4)
+ return FALSE;
+
+ /* walk the cea data blocks */
+ for (i = 4; i < offset; i += (edid[i] & 0x1f) + 1) {
+ char *x = edid + i;
+
+ /* find a vendor specific block */
+ if ((x[0] & 0xe0) >> 5 == 0x03) {
+ int oui = (x[3] << 16) + (x[2] << 8) + x[1];
+
+ /* find the HDMI vendor OUI */
+ if (oui == 0x000c03)
+ return TRUE;
+ }
+ }
+
+ /* guess it's not HDMI after all */
+ return FALSE;
+}