| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #ifdef HAVE_XORG_CONFIG_H1 |
| 14 | #include <xorg-config.h> |
| 15 | #endif |
| 16 | |
| 17 | #include "misc.h" |
| 18 | #include "xf86.h" |
| 19 | #include "xf86_OSproc.h" |
| 20 | #include "xf86DDC.h" |
| 21 | #include <string.h> |
| 22 | |
| 23 | #define RETRIES4 4 |
| 24 | |
| 25 | typedef enum { |
| 26 | DDCOPT_NODDC1, |
| 27 | DDCOPT_NODDC2, |
| 28 | DDCOPT_NODDC |
| 29 | } DDCOpts; |
| 30 | |
| 31 | static const OptionInfoRec DDCOptions[] = { |
| 32 | {DDCOPT_NODDC1, "NoDDC1", OPTV_BOOLEAN, {0}, FALSE0}, |
| 33 | {DDCOPT_NODDC2, "NoDDC2", OPTV_BOOLEAN, {0}, FALSE0}, |
| 34 | {DDCOPT_NODDC, "NoDDC", OPTV_BOOLEAN, {0}, FALSE0}, |
| 35 | {-1, NULL((void*)0), OPTV_NONE, {0}, FALSE0}, |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | |
| 40 | static int |
| 41 | find_start(unsigned int *ptr) |
| 42 | { |
| 43 | unsigned int comp[9], test[9]; |
| 44 | int i, j; |
| 45 | |
| 46 | for (i = 0; i < 9; i++) { |
| 47 | comp[i] = *(ptr++); |
| 48 | test[i] = 1; |
| 49 | } |
| 50 | for (i = 0; i < 127; i++) { |
| 51 | for (j = 0; j < 9; j++) { |
| 52 | test[j] = test[j] & !(comp[j] ^ *(ptr++)); |
| 53 | } |
| 54 | } |
| 55 | for (i = 0; i < 9; i++) |
| 56 | if (test[i]) |
| 57 | return i + 1; |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | static unsigned char * |
| 62 | find_header(unsigned char *block) |
| 63 | { |
| 64 | unsigned char *ptr, *head_ptr, *end; |
| 65 | unsigned char header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 }; |
| 66 | |
| 67 | ptr = block; |
| 68 | end = block + EDID1_LEN128; |
| 69 | while (ptr < end) { |
| 70 | int i; |
| 71 | |
| 72 | head_ptr = ptr; |
| 73 | for (i = 0; i < 8; i++) { |
| 74 | if (header[i] != *(head_ptr++)) |
| 75 | break; |
| 76 | if (head_ptr == end) |
| 77 | head_ptr = block; |
| 78 | } |
| 79 | if (i == 8) |
| 80 | break; |
| 81 | ptr++; |
| 82 | } |
| 83 | if (ptr == end) |
| 84 | return NULL((void*)0); |
| 85 | return ptr; |
| 86 | } |
| 87 | |
| 88 | static unsigned char * |
| 89 | resort(unsigned char *s_block) |
| 90 | { |
| 91 | unsigned char *d_new, *d_ptr, *d_end, *s_ptr, *s_end; |
| 92 | unsigned char tmp; |
| 93 | |
| 94 | s_ptr = find_header(s_block); |
| 95 | if (!s_ptr) |
| 96 | return NULL((void*)0); |
| 97 | s_end = s_block + EDID1_LEN128; |
| 98 | |
| 99 | d_new = malloc(EDID1_LEN128); |
| 100 | if (!d_new) |
| 101 | return NULL((void*)0); |
| 102 | d_end = d_new + EDID1_LEN128; |
| 103 | |
| 104 | for (d_ptr = d_new; d_ptr < d_end; d_ptr++) { |
| 105 | tmp = *(s_ptr++); |
| 106 | *d_ptr = tmp; |
| 107 | if (s_ptr == s_end) |
| 108 | s_ptr = s_block; |
| 109 | } |
| 110 | free(s_block); |
| 111 | return d_new; |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | DDC_checksum(const unsigned char *block, int len) |
| 116 | { |
| 117 | int i, result = 0; |
| 118 | int not_null = 0; |
| 119 | |
| 120 | for (i = 0; i < len; i++) { |
| 121 | not_null |= block[i]; |
| 122 | result += block[i]; |
| 123 | } |
| 124 | |
| 125 | #ifdef DEBUG |
| 126 | if (result & 0xFF) |
| 127 | ErrorF("DDC checksum not correct\n"); |
| 128 | if (!not_null) |
| 129 | ErrorF("DDC read all Null\n"); |
| 130 | #endif |
| 131 | |
| 132 | |
| 133 | if (!not_null) |
| 134 | return 1; |
| 135 | |
| 136 | return result & 0xFF; |
| 137 | } |
| 138 | |
| 139 | static unsigned char * |
| 140 | GetEDID_DDC1(unsigned int *s_ptr) |
| 141 | { |
| 142 | unsigned char *d_block, *d_pos; |
| 143 | unsigned int *s_pos, *s_end; |
| 144 | int s_start; |
| 145 | int i, j; |
| 146 | |
| 147 | s_start = find_start(s_ptr); |
| 148 | if (s_start == -1) |
| |
| 149 | return NULL((void*)0); |
| 150 | s_end = s_ptr + NUM9*128; |
| 151 | s_pos = s_ptr + s_start; |
| 152 | d_block = malloc(EDID1_LEN128); |
| 153 | if (!d_block) |
| 10 | | Assuming 'd_block' is non-null | |
|
| |
| 154 | return NULL((void*)0); |
| 155 | d_pos = d_block; |
| 156 | for (i = 0; i < EDID1_LEN128; i++) { |
| 12 | | Loop condition is true. Entering loop body | |
|
| 157 | for (j = 0; j < 8; j++) { |
| 13 | | Loop condition is true. Entering loop body | |
|
| 158 | *d_pos <<= 1; |
| 14 | | The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage |
|
| 159 | if (*s_pos) { |
| 160 | *d_pos |= 0x01; |
| 161 | } |
| 162 | s_pos++; |
| 163 | if (s_pos == s_end) |
| 164 | s_pos = s_ptr; |
| 165 | }; |
| 166 | s_pos++; |
| 167 | if (s_pos == s_end) |
| 168 | s_pos = s_ptr; |
| 169 | d_pos++; |
| 170 | } |
| 171 | free(s_ptr); |
| 172 | if (d_block && DDC_checksum(d_block, EDID1_LEN128)) { |
| 173 | free(d_block); |
| 174 | return NULL((void*)0); |
| 175 | } |
| 176 | return (resort(d_block)); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static unsigned int * |
| 181 | FetchEDID_DDC1(register ScrnInfoPtr pScrn, |
| 182 | register unsigned int (*read_DDC) (ScrnInfoPtr)) |
| 183 | { |
| 184 | int count = NUM9*128; |
| 185 | unsigned int *ptr, *xp; |
| 186 | |
| 187 | ptr = xp = malloc(sizeof(int) * NUM9*128); |
| 188 | |
| 189 | if (!ptr) |
| 190 | return NULL((void*)0); |
| 191 | do { |
| 192 | |
| 193 | *xp = read_DDC(pScrn); |
| 194 | xp++; |
| 195 | } while (--count); |
| 196 | return ptr; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | static Bool |
| 201 | TestDDC1(ScrnInfoPtr pScrn, unsigned int (*read_DDC) (ScrnInfoPtr)) |
| 202 | { |
| 203 | int old, count; |
| 204 | |
| 205 | old = read_DDC(pScrn); |
| 206 | count = HEADER6 * BITS_PER_BYTE9; |
| 207 | do { |
| 208 | |
| 209 | if (old != read_DDC(pScrn)) |
| 210 | break; |
| 211 | } while (count--); |
| 212 | return count; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | static unsigned char * |
| 221 | EDIDRead_DDC1(ScrnInfoPtr pScrn, DDC1SetSpeedProc DDCSpeed, |
| 222 | unsigned int (*read_DDC) (ScrnInfoPtr)) |
| 223 | { |
| 224 | unsigned char *EDID_block = NULL((void*)0); |
| 225 | int count = RETRIES4; |
| 226 | |
| 227 | if (!read_DDC) { |
| 3 | | Assuming 'read_DDC' is non-null | |
|
| |
| 228 | xf86DrvMsg(pScrn->scrnIndex, X_PROBED, |
| 229 | "chipset doesn't support DDC1\n"); |
| 230 | return NULL((void*)0); |
| 231 | }; |
| 232 | |
| 233 | if (TestDDC1(pScrn, read_DDC) == -1) { |
| |
| 234 | xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "No DDC signal\n"); |
| 235 | return NULL((void*)0); |
| 236 | }; |
| 237 | |
| 238 | if (DDCSpeed) |
| 6 | | Assuming 'DDCSpeed' is null | |
|
| |
| 239 | DDCSpeed(pScrn, DDC_FAST); |
| 240 | do { |
| 241 | EDID_block = GetEDID_DDC1(FetchEDID_DDC1(pScrn, read_DDC)); |
| |
| 242 | count--; |
| 243 | } while (!EDID_block && count); |
| 244 | if (DDCSpeed) |
| 245 | DDCSpeed(pScrn, DDC_SLOW); |
| 246 | |
| 247 | return EDID_block; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | xf86MonPtr |
| 262 | xf86DoEDID_DDC1(ScrnInfoPtr pScrn, DDC1SetSpeedProc DDC1SetSpeed, |
| 263 | unsigned int (*DDC1Read) (ScrnInfoPtr)) |
| 264 | { |
| 265 | unsigned char *EDID_block = NULL((void*)0); |
| 266 | xf86MonPtr tmp = NULL((void*)0); |
| 267 | |
| 268 | |
| 269 | Bool noddc = FALSE0, noddc1 = FALSE0; |
| 270 | OptionInfoPtr options; |
| 271 | |
| 272 | options = xnfalloc(sizeof(DDCOptions))XNFalloc((unsigned long)(sizeof(DDCOptions))); |
| 273 | (void) memcpy(options, DDCOptions, sizeof(DDCOptions))__builtin___memcpy_chk (options, DDCOptions, sizeof(DDCOptions ), __builtin_object_size (options, 0)); |
| 274 | xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options); |
| 275 | |
| 276 | xf86GetOptValBool(options, DDCOPT_NODDC, &noddc); |
| 277 | xf86GetOptValBool(options, DDCOPT_NODDC1, &noddc1); |
| 278 | free(options); |
| 279 | |
| 280 | if (noddc || noddc1) |
| |
| 281 | return NULL((void*)0); |
| 282 | |
| 283 | OsBlockSignals(); |
| 284 | EDID_block = EDIDRead_DDC1(pScrn, DDC1SetSpeed, DDC1Read); |
| |
| 285 | OsReleaseSignals(); |
| 286 | |
| 287 | if (EDID_block) { |
| 288 | tmp = xf86InterpretEDID(pScrn->scrnIndex, EDID_block); |
| 289 | } |
| 290 | #ifdef DEBUG |
| 291 | else |
| 292 | ErrorF("No EDID block returned\n"); |
| 293 | if (!tmp) |
| 294 | ErrorF("Cannot interpret EDID block\n"); |
| 295 | #endif |
| 296 | return tmp; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | static I2CDevPtr |
| 302 | DDC2MakeDevice(I2CBusPtr pBus, int address, const char *name) |
| 303 | { |
| 304 | I2CDevPtr dev = NULL((void*)0); |
| 305 | |
| 306 | if (!(dev = xf86I2CFindDev(pBus, address))) { |
| 307 | dev = xf86CreateI2CDevRec(); |
| 308 | dev->DevName = name; |
| 309 | dev->SlaveAddr = address; |
| 310 | dev->ByteTimeout = 2200; |
| 311 | dev->StartTimeout = 550; |
| 312 | dev->BitTimeout = 40; |
| 313 | dev->AcknTimeout = 40; |
| 314 | |
| 315 | dev->pI2CBus = pBus; |
| 316 | if (!xf86I2CDevInit(dev)) { |
| 317 | xf86DrvMsg(pBus->scrnIndex, X_PROBED, "No DDC2 device\n"); |
| 318 | return NULL((void*)0); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return dev; |
| 323 | } |
| 324 | |
| 325 | static I2CDevPtr |
| 326 | DDC2Init(I2CBusPtr pBus) |
| 327 | { |
| 328 | I2CDevPtr dev = NULL((void*)0); |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | pBus->RiseFallTime = 20; |
| 335 | |
| 336 | dev = DDC2MakeDevice(pBus, 0x00A0, "ddc2"); |
| 337 | if (xf86I2CProbeAddress(pBus, 0x0060)) |
| 338 | DDC2MakeDevice(pBus, 0x0060, "E-EDID segment register"); |
| 339 | |
| 340 | return dev; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | static void |
| 345 | EEDIDStop(I2CDevPtr d) |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | |
| 350 | static Bool |
| 351 | DDC2Read(I2CDevPtr dev, int block, unsigned char *R_Buffer) |
| 352 | { |
| 353 | unsigned char W_Buffer[1]; |
| 354 | int i, segment; |
| 355 | I2CDevPtr seg; |
| 356 | void (*stop) (I2CDevPtr); |
| 357 | |
| 358 | for (i = 0; i < RETRIES4; i++) { |
| 359 | |
| 360 | segment = block >> 1; |
| 361 | if (segment) { |
| 362 | Bool b; |
| 363 | |
| 364 | if (!(seg = xf86I2CFindDev(dev->pI2CBus, 0x0060))) |
| 365 | return FALSE0; |
| 366 | |
| 367 | W_Buffer[0] = segment; |
| 368 | |
| 369 | stop = dev->pI2CBus->I2CStop; |
| 370 | dev->pI2CBus->I2CStop = EEDIDStop; |
| 371 | |
| 372 | b = xf86I2CWriteRead(seg, W_Buffer, 1, NULL((void*)0), 0); |
| 373 | |
| 374 | dev->pI2CBus->I2CStop = stop; |
| 375 | if (!b) { |
| 376 | dev->pI2CBus->I2CStop(dev); |
| 377 | continue; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | W_Buffer[0] = (block & 0x01) * EDID1_LEN128; |
| 382 | |
| 383 | if (xf86I2CWriteRead(dev, W_Buffer, 1, R_Buffer, EDID1_LEN128)) { |
| 384 | if (!DDC_checksum(R_Buffer, EDID1_LEN128)) |
| 385 | return TRUE1; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return FALSE0; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | xf86MonPtr |
| 406 | xf86DoEEDID(ScrnInfoPtr pScrn, I2CBusPtr pBus, Bool complete) |
| 407 | { |
| 408 | unsigned char *EDID_block = NULL((void*)0); |
| 409 | xf86MonPtr tmp = NULL((void*)0); |
| 410 | I2CDevPtr dev = NULL((void*)0); |
| 411 | |
| 412 | |
| 413 | Bool noddc = FALSE0, noddc2 = FALSE0; |
| 414 | OptionInfoPtr options; |
| 415 | |
| 416 | options = malloc(sizeof(DDCOptions)); |
| 417 | if (!options) |
| 418 | return NULL((void*)0); |
| 419 | memcpy(options, DDCOptions, sizeof(DDCOptions))__builtin___memcpy_chk (options, DDCOptions, sizeof(DDCOptions ), __builtin_object_size (options, 0)); |
| 420 | xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, options); |
| 421 | |
| 422 | xf86GetOptValBool(options, DDCOPT_NODDC, &noddc); |
| 423 | xf86GetOptValBool(options, DDCOPT_NODDC2, &noddc2); |
| 424 | free(options); |
| 425 | |
| 426 | if (noddc || noddc2) |
| 427 | return NULL((void*)0); |
| 428 | |
| 429 | if (!(dev = DDC2Init(pBus))) |
| 430 | return NULL((void*)0); |
| 431 | |
| 432 | EDID_block = calloc(1, EDID1_LEN128); |
| 433 | if (!EDID_block) |
| 434 | return NULL((void*)0); |
| 435 | |
| 436 | if (DDC2Read(dev, 0, EDID_block)) { |
| 437 | int i, n = EDID_block[0x7e]; |
| 438 | |
| 439 | if (complete && n) { |
| 440 | EDID_block = reallocarrayxreallocarray(EDID_block, 1 + n, EDID1_LEN128); |
| 441 | |
| 442 | for (i = 0; i < n; i++) |
| 443 | DDC2Read(dev, i + 1, EDID_block + (EDID1_LEN128 * (1 + i))); |
| 444 | } |
| 445 | |
| 446 | tmp = xf86InterpretEEDID(pScrn->scrnIndex, EDID_block); |
| 447 | } |
| 448 | |
| 449 | if (tmp && complete) |
| 450 | tmp->flags |= MONITOR_EDID_COMPLETE_RAWDATA0x01; |
| 451 | |
| 452 | return tmp; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | xf86MonPtr |
| 467 | xf86DoEDID_DDC2(ScrnInfoPtr pScrn, I2CBusPtr pBus) |
| 468 | { |
| 469 | return xf86DoEEDID(pScrn, pBus, FALSE0); |
| 470 | } |