1706f2543Smrg/*
2706f2543Smrg * Copyright 2006 Luc Verhaegen.
3706f2543Smrg *
4706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5706f2543Smrg * copy of this software and associated documentation files (the "Software"),
6706f2543Smrg * to deal in the Software without restriction, including without limitation
7706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
8706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
9706f2543Smrg * Software is furnished to do so, subject to the following conditions:
10706f2543Smrg *
11706f2543Smrg * The above copyright notice and this permission notice (including the
12706f2543Smrg * next paragraph) shall be included in all copies or substantial portions
13706f2543Smrg * of the Software.
14706f2543Smrg *
15706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18706f2543Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19706f2543Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20706f2543Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21706f2543Smrg * DEALINGS IN THE SOFTWARE.
22706f2543Smrg */
23706f2543Smrg
24706f2543Smrg#ifdef HAVE_XORG_CONFIG_H
25706f2543Smrg#include <xorg-config.h>
26706f2543Smrg#endif
27706f2543Smrg
28706f2543Smrg#include "xf86.h"
29706f2543Smrg#include "xf86DDC.h"
30706f2543Smrg#include <X11/Xatom.h>
31706f2543Smrg#include "property.h"
32706f2543Smrg#include "propertyst.h"
33706f2543Smrg#include "xf86DDC.h"
34706f2543Smrg#include <string.h>
35706f2543Smrg
36706f2543Smrg#define EDID1_ATOM_NAME         "XFree86_DDC_EDID1_RAWDATA"
37706f2543Smrg#define EDID2_ATOM_NAME         "XFree86_DDC_EDID2_RAWDATA"
38706f2543Smrg
39706f2543Smrgstatic void
40706f2543SmrgedidMakeAtom(int i, const char *name, CARD8 *data, int size)
41706f2543Smrg{
42706f2543Smrg    Atom atom;
43706f2543Smrg    unsigned char *atom_data;
44706f2543Smrg
45706f2543Smrg    if (!(atom_data = malloc(size*sizeof(CARD8))))
46706f2543Smrg	return;
47706f2543Smrg
48706f2543Smrg    atom = MakeAtom(name, strlen(name), TRUE);
49706f2543Smrg    memcpy(atom_data, data, size);
50706f2543Smrg    xf86RegisterRootWindowProperty(i, atom, XA_INTEGER, 8, size, atom_data);
51706f2543Smrg}
52706f2543Smrg
53706f2543Smrgstatic void
54706f2543SmrgaddRootWindowProperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
55706f2543Smrg{
56706f2543Smrg    int i, scrnIndex = pScrn->scrnIndex;
57706f2543Smrg    Bool makeEDID1prop = FALSE;
58706f2543Smrg    Bool makeEDID2prop = FALSE;
59706f2543Smrg
60706f2543Smrg    if (DDC->flags & MONITOR_DISPLAYID) {
61706f2543Smrg	/* Don't bother, use RANDR already */
62706f2543Smrg	return;
63706f2543Smrg    } else if (DDC->ver.version == 1) {
64706f2543Smrg	makeEDID1prop = TRUE;
65706f2543Smrg    } else if (DDC->ver.version == 2) {
66706f2543Smrg	int checksum1;
67706f2543Smrg	int checksum2;
68706f2543Smrg	makeEDID2prop = TRUE;
69706f2543Smrg
70706f2543Smrg	/* Some monitors (eg Panasonic PanaSync4)
71706f2543Smrg	 * report version==2 because they used EDID v2 spec document,
72706f2543Smrg	 * although they use EDID v1 data structure :-(
73706f2543Smrg	 *
74706f2543Smrg	 * Try using checksum to determine when we have such a monitor.
75706f2543Smrg	 */
76706f2543Smrg	checksum2 = 0;
77706f2543Smrg	for (i = 0; i < 256; i++)
78706f2543Smrg	    checksum2 += DDC->rawData[i];
79706f2543Smrg	if (checksum2 % 256) {
80706f2543Smrg	    xf86DrvMsg(scrnIndex, X_INFO, "Monitor EDID v2 checksum failed\n");
81706f2543Smrg	    xf86DrvMsg(scrnIndex, X_INFO,
82706f2543Smrg		    "XFree86_DDC_EDID2_RAWDATA property may be bad\n");
83706f2543Smrg	    checksum1 = 0;
84706f2543Smrg	    for (i = 0; i < 128; i++)
85706f2543Smrg		checksum1 += DDC->rawData[i];
86706f2543Smrg	    if (!(checksum1 % 256)) {
87706f2543Smrg		xf86DrvMsg(scrnIndex, X_INFO,
88706f2543Smrg			"Monitor EDID v1 checksum passed,\n");
89706f2543Smrg		xf86DrvMsg(scrnIndex, X_INFO,
90706f2543Smrg			"XFree86_DDC_EDID1_RAWDATA property created\n");
91706f2543Smrg		makeEDID1prop = TRUE;
92706f2543Smrg	    }
93706f2543Smrg	}
94706f2543Smrg    } else {
95706f2543Smrg	xf86DrvMsg(scrnIndex, X_PROBED, "unexpected EDID version %d.%d\n",
96706f2543Smrg		DDC->ver.version, DDC->ver.revision);
97706f2543Smrg	return;
98706f2543Smrg    }
99706f2543Smrg
100706f2543Smrg    if (makeEDID1prop) {
101706f2543Smrg	int size = 128 +
102706f2543Smrg	    (DDC->flags & EDID_COMPLETE_RAWDATA ? DDC->no_sections * 128 : 0);
103706f2543Smrg
104706f2543Smrg	edidMakeAtom(scrnIndex, EDID1_ATOM_NAME, DDC->rawData, size);
105706f2543Smrg    }
106706f2543Smrg
107706f2543Smrg    if (makeEDID2prop)
108706f2543Smrg	edidMakeAtom(scrnIndex, EDID2_ATOM_NAME, DDC->rawData, 256);
109706f2543Smrg}
110706f2543Smrg
111706f2543SmrgBool
112706f2543Smrgxf86SetDDCproperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
113706f2543Smrg{
114706f2543Smrg    if (!pScrn || !pScrn->monitor || !DDC)
115706f2543Smrg        return FALSE;
116706f2543Smrg
117706f2543Smrg    if (DDC->flags & MONITOR_DISPLAYID)
118706f2543Smrg	;
119706f2543Smrg    else
120706f2543Smrg	xf86EdidMonitorSet(pScrn->scrnIndex, pScrn->monitor, DDC);
121706f2543Smrg
122706f2543Smrg    addRootWindowProperties(pScrn, DDC);
123706f2543Smrg
124706f2543Smrg    return TRUE;
125706f2543Smrg}
126