1/*
2 * Copyright 2006 Luc Verhaegen.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#ifdef HAVE_XORG_CONFIG_H
25#include <xorg-config.h>
26#endif
27
28#include "xf86.h"
29#include "xf86DDC.h"
30#include <X11/Xatom.h>
31#include "property.h"
32#include "propertyst.h"
33#include "xf86DDC.h"
34#include <string.h>
35
36#define EDID1_ATOM_NAME         "XFree86_DDC_EDID1_RAWDATA"
37#define EDID2_ATOM_NAME         "XFree86_DDC_EDID2_RAWDATA"
38
39static void
40edidMakeAtom(int i, const char *name, CARD8 *data, int size)
41{
42    Atom atom;
43    unsigned char *atom_data;
44
45    if (!(atom_data = malloc(size*sizeof(CARD8))))
46	return;
47
48    atom = MakeAtom(name, strlen(name), TRUE);
49    memcpy(atom_data, data, size);
50    xf86RegisterRootWindowProperty(i, atom, XA_INTEGER, 8, size, atom_data);
51}
52
53static void
54addRootWindowProperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
55{
56    int i, scrnIndex = pScrn->scrnIndex;
57    Bool makeEDID1prop = FALSE;
58    Bool makeEDID2prop = FALSE;
59
60    if (DDC->flags & MONITOR_DISPLAYID) {
61	/* Don't bother, use RANDR already */
62	return;
63    } else if (DDC->ver.version == 1) {
64	makeEDID1prop = TRUE;
65    } else if (DDC->ver.version == 2) {
66	int checksum1;
67	int checksum2;
68	makeEDID2prop = TRUE;
69
70	/* Some monitors (eg Panasonic PanaSync4)
71	 * report version==2 because they used EDID v2 spec document,
72	 * although they use EDID v1 data structure :-(
73	 *
74	 * Try using checksum to determine when we have such a monitor.
75	 */
76	checksum2 = 0;
77	for (i = 0; i < 256; i++)
78	    checksum2 += DDC->rawData[i];
79	if (checksum2 % 256) {
80	    xf86DrvMsg(scrnIndex, X_INFO, "Monitor EDID v2 checksum failed\n");
81	    xf86DrvMsg(scrnIndex, X_INFO,
82		    "XFree86_DDC_EDID2_RAWDATA property may be bad\n");
83	    checksum1 = 0;
84	    for (i = 0; i < 128; i++)
85		checksum1 += DDC->rawData[i];
86	    if (!(checksum1 % 256)) {
87		xf86DrvMsg(scrnIndex, X_INFO,
88			"Monitor EDID v1 checksum passed,\n");
89		xf86DrvMsg(scrnIndex, X_INFO,
90			"XFree86_DDC_EDID1_RAWDATA property created\n");
91		makeEDID1prop = TRUE;
92	    }
93	}
94    } else {
95	xf86DrvMsg(scrnIndex, X_PROBED, "unexpected EDID version %d.%d\n",
96		DDC->ver.version, DDC->ver.revision);
97	return;
98    }
99
100    if (makeEDID1prop) {
101	int size = 128 +
102	    (DDC->flags & EDID_COMPLETE_RAWDATA ? DDC->no_sections * 128 : 0);
103
104	edidMakeAtom(scrnIndex, EDID1_ATOM_NAME, DDC->rawData, size);
105    }
106
107    if (makeEDID2prop)
108	edidMakeAtom(scrnIndex, EDID2_ATOM_NAME, DDC->rawData, 256);
109}
110
111Bool
112xf86SetDDCproperties(ScrnInfoPtr pScrn, xf86MonPtr DDC)
113{
114    if (!pScrn || !pScrn->monitor || !DDC)
115        return FALSE;
116
117    if (DDC->flags & MONITOR_DISPLAYID)
118	;
119    else
120	xf86EdidMonitorSet(pScrn->scrnIndex, pScrn->monitor, DDC);
121
122    addRootWindowProperties(pScrn, DDC);
123
124    return TRUE;
125}
126