Chameleon

Chameleon Svn Source Tree

Root/branches/slice/old749m/i386/modules/Resolution/edid.c

1/*
2 * edid.c
3 *
4 *
5 * Created by Evan Lojewski on 12/1/09.
6 * Copyright 2009. All rights reserved.
7 *
8 *Slice 2010
9 */
10
11
12//#include "libsaio.h"
13#include "edid.h"
14#include "vbe.h"
15#include "graphics.h"
16#include "boot.h"
17//----------------------------------------------------------------------------------
18
19#define FBMON_FIX_HEADER 1
20#define FBMON_FIX_INPUT 2
21#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22
23//----------------------------------------------------------------------------------
24/*
25struct broken_edid {
26const char manufacturer[4];
27UInt32 model;
28UInt32 fix;
29};
30
31//----------------------------------------------------------------------------------
32
33broken_edid brokendb[] = {
34// DEC FR-PCXAV-YZ *
35{ "DEC", 0x073a, FBMON_FIX_HEADER,},
36// ViewSonic PF775a *
37{ "VSC", 0x5a44, FBMON_FIX_INPUT,}
38};
39//----------------------------------------------------------------------------------
40*/
41const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
42
43//----------------------------------------------------------------------------------
44int edid_compare(unsigned char *edid1, unsigned char *edid2)
45{
46int result = 0;
47unsigned char *block = edid1 + ID_MANUFACTURER_NAME, manufacturer1[4], manufacturer2[4];;
48manufacturer1[0] = ((block[0] & 0x7c) >> 2) + '@';
49manufacturer1[1] = ((block[0] & 0x03) << 3) + ((block[1] & 0xe0) >> 5) + '@';
50manufacturer1[2] = (block[1] & 0x1f) + '@';
51manufacturer1[3] = 0;
52
53block = edid2 + ID_MANUFACTURER_NAME;
54manufacturer2[0] = ((block[0] & 0x7c) >> 2) + '@';
55manufacturer2[1] = ((block[0] & 0x03) << 3) + ((block[1] & 0xe0) >> 5) + '@';
56manufacturer2[2] = (block[1] & 0x1f) + '@';
57manufacturer2[3] = 0;
58int x;
59for(x = 0; x < 4; x++)
60{
61if(manufacturer1[x] == manufacturer2[x])
62result++;
63}
64
65return result;
66}
67
68int check_edid(unsigned char *edid)
69{
70unsigned char *block = edid + ID_MANUFACTURER_NAME, manufacturer[4];
71//unsigned char *b;
72UInt32 model;
73//int i, fix = 0, ret = 0;
74
75manufacturer[0] = ((block[0] & 0x7c) >> 2) + '@';
76manufacturer[1] = ((block[0] & 0x03) << 3) +
77((block[1] & 0xe0) >> 5) + '@';
78manufacturer[2] = (block[1] & 0x1f) + '@';
79manufacturer[3] = 0;
80model = block[2] + (block[3] << 8);
81/*
82for (i = 0; i < (int)ARRAY_SIZE(brokendb); i++) {
83if (!strncmp((const char *)manufacturer, brokendb[i].manufacturer, 4) &&
84brokendb[i].model == model) {
85DEBG("ATIFB: The EDID Block of "
86 "Manufacturer: %s Model: 0x%08lx is known to "
87 "be broken,\n", manufacturer, model);
88fix = brokendb[i].fix;
89break;
90}
91}
92
93switch (fix) {
94case FBMON_FIX_HEADER:
95for (i = 0; i < 8; i++) {
96if (edid[i] != edid_v1_header[i])
97ret = fix;
98}
99break;
100case FBMON_FIX_INPUT:
101b = edid + EDID_STRUCT_DISPLAY;
102/// Only if display is GTF capable will
103 //the input type be reset to analog *
104if (b[4] & 0x01 && b[0] & 0x80)
105ret = fix;
106break;
107}
108*/
109return 0; //ret;
110}
111
112//----------------------------------------------------------------------------------
113
114static void fix_edid(unsigned char *edid, int fix)
115{
116unsigned char *b;
117
118switch (fix) {
119case FBMON_FIX_HEADER:
120msglog("EDID: trying a header reconstruct\n");
121memcpy(edid, edid_v1_header, 8);
122break;
123case FBMON_FIX_INPUT:
124msglog("EDID: trying to fix input type\n");
125b = edid + EDID_STRUCT_DISPLAY;
126b[0] &= ~0x80;
127edid[127] += 0x80;
128}
129}
130
131//----------------------------------------------------------------------------------
132
133int edid_checksum(unsigned char *edid)
134{
135unsigned char i, csum = 0, all_null = 0;
136int err = 0, fix = check_edid(edid);
137
138if (fix)
139fix_edid(edid, fix);
140
141for (i = 0; i < EDID_LENGTH; i++) {
142csum += edid[i];
143all_null |= edid[i];
144}
145
146if (csum == 0x00 && all_null) {
147/* checksum passed, everything's good */
148err = 1;
149}
150
151return err;
152}
153
154//----------------------------------------------------------------------------------
155
156static int edid_check_header(unsigned char *edid)
157{
158int i, err = 1, fix = check_edid(edid);
159
160if (fix)
161fix_edid(edid, fix);
162
163for (i = 0; i < 8; i++) {
164if (edid[i] != edid_v1_header[i])
165err = 0;
166}
167
168return err;
169}
170//------------------------------------------------------------------------
171bool verifyEDID(unsigned char *edid)
172{
173if (edid == NULL || !edid_checksum(edid) ||!edid_check_header(edid))
174{
175return false;
176}
177return true;
178}
179
180int edid_is_timing_block(unsigned char *block)
181{
182if ((block[0] != 0x00) || (block[1] != 0x00) ||
183(block[2] != 0x00) || (block[4] != 0x00))
184return 1;
185else
186return 0;
187}
188//----------------------------------------------------------------------------------
189
190int fb_parse_edid(struct EDID *edid, edid_mode* var) //(struct EDID *edid, UInt32* x, UInt32* y)
191{
192int i;
193unsigned char *block;
194
195if(!verifyEDID((unsigned char *)edid)) return 1;
196
197block = (unsigned char *)edid + DETAILED_TIMING_DESCRIPTIONS_START; //54
198
199for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
200if (edid_is_timing_block(block)) {
201var->h_active = H_ACTIVE;
202var->v_active = V_ACTIVE;
203var->h_sync_offset = H_SYNC_OFFSET;
204var->h_sync_width = H_SYNC_WIDTH;
205var->h_blanking = H_BLANKING;
206var->v_blanking = V_BLANKING;
207var->pixel_clock = PIXEL_CLOCK;
208var->h_sync_width = H_SYNC_WIDTH;
209var->v_sync_width = V_SYNC_WIDTH;
210/*
211var->xres = var->xres_virtual = H_ACTIVE;
212var->yres = var->yres_virtual = V_ACTIVE;
213var->height = var->width = -1;
214var->right_margin = H_SYNC_OFFSET;
215var->left_margin = (H_ACTIVE + H_BLANKING) -
216(H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
217var->upper_margin = V_BLANKING - V_SYNC_OFFSET -
218V_SYNC_WIDTH;
219var->lower_margin = V_SYNC_OFFSET;
220var->hsync_len = H_SYNC_WIDTH;
221var->vsync_len = V_SYNC_WIDTH;
222var->pixclock = PIXEL_CLOCK;
223var->pixclock /= 1000;
224var->pixclock = KHZ2PICOS(var->pixclock);
225
226if (HSYNC_POSITIVE)
227var->sync |= FB_SYNC_HOR_HIGH_ACT;
228if (VSYNC_POSITIVE)
229var->sync |= FB_SYNC_VERT_HIGH_ACT;
230 */
231return 1;
232}
233}
234return 0;
235}
236
237void getResolution(UInt32* x, UInt32* y, UInt32* bp)
238{
239int val;
240static UInt32 xResolution, yResolution, bpResolution;
241
242if(getIntForKey(kScreenWidth, &val, &bootInfo->bootConfig))
243{
244xResolution = val;
245}
246
247if(getIntForKey(kScreenHeight, &val, &bootInfo->bootConfig))
248{
249yResolution = val;
250}
251
252bpResolution = 32;// assume 32bits
253
254
255if(!xResolution || !yResolution || !bpResolution)
256{
257
258char* edidInfo = readEDID();
259
260if(!edidInfo) return;
261edid_mode mode;
262// TODO: check *all* resolutions reported and either use the highest, or the native resolution (if there is a flag for that)
263//xResolution = edidInfo[56] | ((edidInfo[58] & 0xF0) << 4);
264//yResolution = edidInfo[59] | ((edidInfo[61] & 0xF0) << 4);
265//Slice - done here
266
267if(fb_parse_edid((struct EDID *)edidInfo, &mode) == 0)
268{
269xResolution = DEFAULT_SCREEN_WIDTH;
270yResolution = DEFAULT_SCREEN_HEIGHT;
271}
272else {
273xResolution = mode.h_active;
274yResolution = mode.v_active;
275}
276
277/*
278 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x00 0x32 0x0C
279 0x00 0xDF 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0x00
280 0x0C 0xDF 0x00 0x00 0x12 0x03 0x21 0x78 0xE9 0x99
281 0x53 0x28 0xFF 0xFF 0x32 0xDF 0x00 0x12 0x80 0x78
282 0xD5 0x53 0x26 0x00 0x01 0x01 0x01 0x01 0xFF 0x00
283 0xDF 0x00 0x03 0x78 0x99 0x28 0x00 0x01 0x01 0x01
284 0x01 0x21 0x84 0x20 0xFF 0x0C 0x00 0x03 0x0A 0x53
285 0x54 0x01 0x01 0x01 0xDE 0x84 0x56 0x00 0xA0 0x30
286 0xFF 0xDF 0x12 0x78 0x53 0x00 0x01 0x01 0x01 0x84
287 0x00 0x18 0x84 0x00 0x00 0x57 0xFF 0x00 0x80 0x99
288 0x54 0x01 0x01 0x21 0x20 0x00 0x50 0x00 0x00 0x35
289 0x57 0xFE 0x00 0x00 0x78 0x28 0x01 0x01 0x21 0x20
290 0x18 0x30 0x00 0x57 0x34 0xFE 0xAA 0x9A
291
292 */
293
294//msglog("H Active = %d ", edidInfo[56] | ((edidInfo[58] & 0xF0) << 4) );
295//msglog("V Active = %d \n", edidInfo[59] | ((edidInfo[61] & 0xF0) << 4) );
296
297free( edidInfo );
298
299//if(!xResolution) xResolution = DEFAULT_SCREEN_WIDTH;
300//if(!yResolution) yResolution = DEFAULT_SCREEN_HEIGHT;
301
302}
303
304*x = xResolution;
305*y = yResolution;
306*bp = bpResolution;
307
308}
309
310char* readEDID()
311{
312SInt16 last_reported = -1;
313UInt8 edidInfo[EDID_BLOCK_SIZE];
314
315UInt8 header1[] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
316UInt8 header2[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
317
318SInt16 status;
319UInt16 blocks_left = 1;
320//msglog("readEDID\n");
321do
322{
323// TODO: This currently only retrieves the *last* block, make the block buffer expand as needed / calculated from the first block
324
325bzero( edidInfo, EDID_BLOCK_SIZE);
326
327status = getEDID(edidInfo, blocks_left);
328
329
330msglog("Buffer location: 0x%X status: %d\n", SEG(edidInfo) << 16 | OFF(edidInfo), status);
331
332int j, i;
333for (j = 0; j < 8; j++) {
334for(i = 0; i < 16; i++) msglog("0x%02X ", edidInfo[((i+1) * (j + 1)) - 1]);
335msglog("\n");
336}
337
338
339
340if(status == 0)
341{
342//if( edidInfo[0] == 0x00 || edidInfo[0] == 0xFF)
343if((memcmp(edidInfo, header1, sizeof(header1)) != 0) ||
344 (memcmp(edidInfo, header2, sizeof(header2)) != 0) )
345{
346blocks_left--;
347int reported = edidInfo[ EDID_V1_BLOCKS_TO_GO_OFFSET ];
348
349if ( reported > blocks_left )
350{
351
352msglog("EDID claims %d more blocks left\n", reported);
353}
354
355if ( (last_reported <= reported && last_reported != -1)
356|| reported == 0xff
357/* 0xff frequently comes up in corrupt edids */
358//|| reported == MAGIC
359)
360{
361msglog("Last reported %d\n", last_reported);
362msglog( "EDID blocks left is wrong.\n"
363 "Your EDID is probably invalid.\n");
364return 0;
365}
366else
367{
368//printf("Reading EDID block\n");
369//printf("H Active = %d", ebiosInfo[56] | ((ebiosInfo[58] & 0xF0) << 4) );
370//printf("V Active = %d", ebiosInfo[59] | ((ebiosInfo[61] & 0xF0) << 4) );
371
372last_reported = reported;
373blocks_left = reported;
374}
375}
376else
377{
378msglog("Invalid block %d\n", blocks_left);
379msglog("Header1 = %d", memcmp(edidInfo, header1, sizeof(header1)) );
380msglog("Header2 = %d", memcmp(edidInfo, header2, sizeof(header2)) );
381return 0;
382}
383}
384blocks_left = 0;
385} while(blocks_left);
386
387char* ret = malloc(sizeof(edidInfo));
388memcpy(ret, edidInfo, sizeof(edidInfo));
389return ret;
390}
391

Archive Download this file

Revision: 1174