vesa.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include </386/include/ureg.h>
  5. typedef struct Ureg Ureg;
  6. #include "pci.h"
  7. #include "vga.h"
  8. typedef struct Vbe Vbe;
  9. typedef struct Vmode Vmode;
  10. typedef struct Modelist Modelist;
  11. typedef struct Edid Edid;
  12. enum
  13. {
  14. MemSize = 1024*1024,
  15. PageSize = 4096,
  16. RealModeBuf = 0x9000,
  17. };
  18. struct Vbe
  19. {
  20. int rmfd; /* /dev/realmode */
  21. int memfd; /* /dev/realmem */
  22. uchar *mem; /* copy of memory; 1MB */
  23. uchar *isvalid; /* 1byte per 4kB in mem */
  24. uchar *buf;
  25. uchar *modebuf;
  26. };
  27. struct Vmode
  28. {
  29. char name[32];
  30. char chan[32];
  31. int id;
  32. int attr; /* flags */
  33. int bpl;
  34. int dx, dy;
  35. int depth;
  36. char *model;
  37. int r, g, b, x;
  38. int ro, go, bo, xo;
  39. int directcolor; /* flags */
  40. ulong paddr;
  41. };
  42. struct Edid {
  43. char mfr[4]; /* manufacturer */
  44. char serialstr[16]; /* serial number as string (in extended data) */
  45. char name[16]; /* monitor name as string (in extended data) */
  46. ushort product; /* product code, 0 = unused */
  47. ulong serial; /* serial number, 0 = unused */
  48. uchar version; /* major version number */
  49. uchar revision; /* minor version number */
  50. uchar mfrweek; /* week of manufacture, 0 = unused */
  51. int mfryear; /* year of manufacture, 0 = unused */
  52. uchar dxcm; /* horizontal image size in cm. */
  53. uchar dycm; /* vertical image size in cm. */
  54. int gamma; /* gamma*100 */
  55. int rrmin; /* minimum vertical refresh rate */
  56. int rrmax; /* maximum vertical refresh rate */
  57. int hrmin; /* minimum horizontal refresh rate */
  58. int hrmax; /* maximum horizontal refresh rate */
  59. ulong pclkmax; /* maximum pixel clock */
  60. int flags;
  61. Modelist *modelist; /* list of supported modes */
  62. };
  63. struct Modelist
  64. {
  65. Mode;
  66. Modelist *next;
  67. };
  68. enum {
  69. Fdigital = 1<<0, /* is a digital display */
  70. Fdpmsstandby = 1<<1, /* supports DPMS standby mode */
  71. Fdpmssuspend = 1<<2, /* supports DPMS suspend mode */
  72. Fdpmsactiveoff = 1<<3, /* supports DPMS active off mode */
  73. Fmonochrome = 1<<4, /* is a monochrome display */
  74. Fgtf = 1<<5, /* supports VESA GTF: see /public/doc/vesa/gtf10.pdf */
  75. };
  76. #define WORD(p) ((p)[0] | ((p)[1]<<8))
  77. #define LONG(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
  78. #define PWORD(p, v) (p)[0] = (v); (p)[1] = (v)>>8
  79. #define PLONG(p, v) (p)[0] = (v); (p)[1] = (v)>>8; (p)[2] = (v)>>16; (p)[3] = (v)>>24
  80. static Vbe *vbe;
  81. static Edid edid;
  82. Vbe *mkvbe(void);
  83. int vbecheck(Vbe*);
  84. uchar *vbemodes(Vbe*);
  85. int vbemodeinfo(Vbe*, int, Vmode*);
  86. int vbegetmode(Vbe*);
  87. int vbesetmode(Vbe*, int);
  88. void vbeprintinfo(Vbe*);
  89. void vbeprintmodeinfo(Vbe*, int, char*);
  90. int vbesnarf(Vbe*, Vga*);
  91. void vesaddc(void);
  92. int vbeddcedid(Vbe *vbe, Edid *e);
  93. void printedid(Edid*);
  94. int
  95. dbvesa(Vga* vga)
  96. {
  97. vbe = mkvbe();
  98. if(vbe == nil){
  99. fprint(2, "mkvbe: %r\n");
  100. return 0;
  101. }
  102. if(vbecheck(vbe) < 0){
  103. fprint(2, "dbvesa: %r\n");
  104. return 0;
  105. }
  106. vga->link = alloc(sizeof(Ctlr));
  107. *vga->link = vesa;
  108. vga->vesa = vga->link;
  109. vga->ctlr = vga->link;
  110. vga->link->link = alloc(sizeof(Ctlr));
  111. *vga->link->link = softhwgc;
  112. vga->hwgc = vga->link->link;
  113. return 1;
  114. }
  115. Mode*
  116. dbvesamode(char *mode)
  117. {
  118. int i;
  119. uchar *p, *ep;
  120. Vmode vm;
  121. Mode *m;
  122. if(vbe == nil)
  123. return nil;
  124. p = vbemodes(vbe);
  125. if(p == nil)
  126. return nil;
  127. for(ep=p+1024; (p[0]!=0xFF || p[1]!=0xFF) && p<ep; p+=2){
  128. if(vbemodeinfo(vbe, WORD(p), &vm) < 0)
  129. continue;
  130. if(strcmp(vm.name, mode) == 0)
  131. goto havemode;
  132. }
  133. if(1){
  134. fprint(2, "warning: scanning for unoffered vesa modes\n");
  135. for(i=0x100; i<0x200; i++){
  136. if(vbemodeinfo(vbe, i, &vm) < 0)
  137. continue;
  138. if(strcmp(vm.name, mode) == 0)
  139. goto havemode;
  140. }
  141. }
  142. werrstr("no such vesa mode");
  143. return nil;
  144. havemode:
  145. m = alloc(sizeof(Mode));
  146. strcpy(m->type, "vesa");
  147. strcpy(m->size, vm.name);
  148. strcpy(m->chan, vm.chan);
  149. m->frequency = 100;
  150. m->x = vm.dx;
  151. m->y = vm.dy;
  152. m->z = vm.depth;
  153. m->ht = m->x;
  154. m->shb = m->x;
  155. m->ehb = m->x;
  156. m->shs = m->x;
  157. m->ehs = m->x;
  158. m->vt = m->y;
  159. m->vrs = m->y;
  160. m->vre = m->y;
  161. m->attr = alloc(sizeof(Attr));
  162. m->attr->attr = "id";
  163. m->attr->val = alloc(32);
  164. sprint(m->attr->val, "0x%x", vm.id);
  165. return m;
  166. }
  167. static void
  168. snarf(Vga* vga, Ctlr* ctlr)
  169. {
  170. if(!vbe)
  171. vbe = mkvbe();
  172. if(vbe)
  173. vga->vesa = ctlr;
  174. vbesnarf(vbe, vga);
  175. vga->linear = 1;
  176. ctlr->flag |= Hlinear|Ulinear;
  177. }
  178. static void
  179. load(Vga* vga, Ctlr* ctlr)
  180. {
  181. if(vbe == nil)
  182. error("no vesa bios");
  183. if(vbesetmode(vbe, atoi(dbattr(vga->mode->attr, "id"))) < 0){
  184. ctlr->flag |= Ferror;
  185. fprint(2, "vbesetmode: %r\n");
  186. }
  187. }
  188. static void
  189. dump(Vga*, Ctlr*)
  190. {
  191. int i;
  192. char did[0x200];
  193. uchar *p, *ep;
  194. if(!vbe){
  195. Bprint(&stdout, "no vesa bios\n");
  196. return;
  197. }
  198. memset(did, 0, sizeof did);
  199. vbeprintinfo(vbe);
  200. p = vbemodes(vbe);
  201. if(p){
  202. for(ep=p+1024; (p[0]!=0xFF || p[1]!=0xFF) && p<ep; p+=2){
  203. vbeprintmodeinfo(vbe, WORD(p), "");
  204. if(WORD(p) < nelem(did))
  205. did[WORD(p)] = 1;
  206. }
  207. }
  208. for(i=0x100; i<0x1FF; i++)
  209. if(!did[i])
  210. vbeprintmodeinfo(vbe, i, " (unoffered)");
  211. if(vbeddcedid(vbe, &edid) < 0)
  212. fprint(2, "warning: reading edid: %r\n");
  213. else
  214. printedid(&edid);
  215. }
  216. Ctlr vesa = {
  217. "vesa", /* name */
  218. snarf, /* snarf */
  219. 0, /* options */
  220. 0, /* init */
  221. load, /* load */
  222. dump, /* dump */
  223. };
  224. Ctlr softhwgc = {
  225. "soft",
  226. };
  227. /*
  228. * VESA bios extension
  229. */
  230. typedef struct Flag Flag;
  231. struct Flag {
  232. int bit;
  233. char *desc;
  234. };
  235. static Flag capabilityflag[] = {
  236. 0x01, "8-bit-dac",
  237. 0x02, "not-vga",
  238. 0x04, "ramdac-needs-blank",
  239. 0x08, "stereoscopic",
  240. 0x10, "stereo-evc",
  241. 0
  242. };
  243. static Flag modeattributesflags[] = {
  244. 1<<0, "supported",
  245. 1<<2, "tty",
  246. 1<<3, "color",
  247. 1<<4, "graphics",
  248. 1<<5, "not-vga",
  249. 1<<6, "no-windowed-vga",
  250. 1<<7, "linear",
  251. 1<<8, "double-scan",
  252. 1<<9, "interlace",
  253. 1<<10, "triple-buffer",
  254. 1<<11, "stereoscopic",
  255. 1<<12, "dual-start-addr",
  256. 0
  257. };
  258. static Flag winattributesflags[] = {
  259. 1<<0, "relocatable",
  260. 1<<1, "readable",
  261. 1<<2, "writeable",
  262. 0
  263. };
  264. static Flag directcolorflags[] = {
  265. 1<<0, "programmable-color-ramp",
  266. 1<<1, "x-usable",
  267. 0
  268. };
  269. static char *modelstr[] = {
  270. "text", "cga", "hercules", "planar", "packed", "non-chain4", "direct", "YUV"
  271. };
  272. static void
  273. printflags(Flag *f, int b)
  274. {
  275. int i;
  276. for(i=0; f[i].bit; i++)
  277. if(f[i].bit & b)
  278. Bprint(&stdout, " %s", f[i].desc);
  279. Bprint(&stdout, "\n");
  280. }
  281. Vbe*
  282. mkvbe(void)
  283. {
  284. Vbe *vbe;
  285. vbe = alloc(sizeof(Vbe));
  286. if((vbe->rmfd = open("/dev/realmode", ORDWR)) < 0)
  287. return nil;
  288. if((vbe->memfd = open("/dev/realmodemem", ORDWR)) < 0)
  289. return nil;
  290. vbe->mem = alloc(MemSize);
  291. vbe->isvalid = alloc(MemSize/PageSize);
  292. vbe->buf = alloc(PageSize);
  293. vbe->modebuf = alloc(PageSize);
  294. return vbe;
  295. }
  296. static void
  297. loadpage(Vbe *vbe, int p)
  298. {
  299. if(p >= MemSize/PageSize || vbe->isvalid[p])
  300. return;
  301. if(pread(vbe->memfd, vbe->mem+p*PageSize, PageSize, p*PageSize) != PageSize)
  302. error("read /dev/realmodemem: %r\n");
  303. vbe->isvalid[p] = 1;
  304. }
  305. static void*
  306. unfarptr(Vbe *vbe, uchar *p)
  307. {
  308. int seg, off;
  309. seg = WORD(p+2);
  310. off = WORD(p);
  311. if(seg==0 && off==0)
  312. return nil;
  313. off += seg<<4;
  314. if(off >= MemSize)
  315. return nil;
  316. loadpage(vbe, off/PageSize);
  317. loadpage(vbe, off/PageSize+1); /* just in case */
  318. return vbe->mem+off;
  319. }
  320. uchar*
  321. vbesetup(Vbe *vbe, Ureg *u, int ax)
  322. {
  323. memset(vbe->buf, 0, PageSize);
  324. memset(u, 0, sizeof *u);
  325. u->ax = ax;
  326. u->es = (RealModeBuf>>4)&0xF000;
  327. u->di = RealModeBuf&0xFFFF;
  328. return vbe->buf;
  329. }
  330. int
  331. vbecall(Vbe *vbe, Ureg *u)
  332. {
  333. u->trap = 0x10;
  334. if(pwrite(vbe->memfd, vbe->buf, PageSize, RealModeBuf) != PageSize)
  335. error("write /dev/realmodemem: %r\n");
  336. if(pwrite(vbe->rmfd, u, sizeof *u, 0) != sizeof *u)
  337. error("write /dev/realmode: %r\n");
  338. if(pread(vbe->rmfd, u, sizeof *u, 0) != sizeof *u)
  339. error("read /dev/realmode: %r\n");
  340. if(pread(vbe->memfd, vbe->buf, PageSize, RealModeBuf) != PageSize)
  341. error("read /dev/realmodemem: %r\n");
  342. if((u->ax&0xFFFF) != 0x004F){
  343. werrstr("VBE error %#.4lux", u->ax&0xFFFF);
  344. return -1;
  345. }
  346. memset(vbe->isvalid, 0, MemSize/PageSize);
  347. return 0;
  348. }
  349. int
  350. vbecheck(Vbe *vbe)
  351. {
  352. uchar *p;
  353. Ureg u;
  354. p = vbesetup(vbe, &u, 0x4F00);
  355. strcpy((char*)p, "VBE2");
  356. if(vbecall(vbe, &u) < 0)
  357. return -1;
  358. if(memcmp(p, "VESA", 4) != 0 || p[5] < 2){
  359. werrstr("invalid vesa signature %.4H %.4H\n", p, p+4);
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. int
  365. vbesnarf(Vbe *vbe, Vga *vga)
  366. {
  367. uchar *p;
  368. Ureg u;
  369. p = vbesetup(vbe, &u, 0x4F00);
  370. strcpy((char*)p, "VBE2");
  371. if(vbecall(vbe, &u) < 0)
  372. return -1;
  373. if(memcmp(p, "VESA", 4) != 0 || p[5] < 2)
  374. return -1;
  375. vga->apz = WORD(p+18)*0x10000UL;
  376. return 0;
  377. }
  378. void
  379. vbeprintinfo(Vbe *vbe)
  380. {
  381. uchar *p;
  382. Ureg u;
  383. p = vbesetup(vbe, &u, 0x4F00);
  384. strcpy((char*)p, "VBE2");
  385. if(vbecall(vbe, &u) < 0)
  386. return;
  387. printitem("vesa", "sig");
  388. Bprint(&stdout, "%.4s %d.%d\n", (char*)p, p[5], p[4]);
  389. if(p[5] < 2)
  390. return;
  391. printitem("vesa", "oem");
  392. Bprint(&stdout, "%s %d.%d\n", unfarptr(vbe, p+6), p[21], p[20]);
  393. printitem("vesa", "vendor");
  394. Bprint(&stdout, "%s\n", unfarptr(vbe, p+22));
  395. printitem("vesa", "product");
  396. Bprint(&stdout, "%s\n", unfarptr(vbe, p+26));
  397. printitem("vesa", "rev");
  398. Bprint(&stdout, "%s\n", unfarptr(vbe, p+30));
  399. printitem("vesa", "cap");
  400. printflags(capabilityflag, p[10]);
  401. printitem("vesa", "mem");
  402. Bprint(&stdout, "%lud\n", WORD(p+18)*0x10000UL);
  403. }
  404. uchar*
  405. vbemodes(Vbe *vbe)
  406. {
  407. uchar *p;
  408. Ureg u;
  409. p = vbesetup(vbe, &u, 0x4F00);
  410. strcpy((char*)p, "VBE2");
  411. if(vbecall(vbe, &u) < 0)
  412. return nil;
  413. memmove(vbe->modebuf, unfarptr(vbe, p+14), 1024);
  414. return vbe->modebuf;
  415. }
  416. int
  417. vbemodeinfo(Vbe *vbe, int id, Vmode *m)
  418. {
  419. uchar *p;
  420. Ureg u;
  421. p = vbesetup(vbe, &u, 0x4F01);
  422. u.cx = id;
  423. if(vbecall(vbe, &u) < 0)
  424. return -1;
  425. m->id = id;
  426. m->attr = WORD(p);
  427. m->bpl = WORD(p+16);
  428. m->dx = WORD(p+18);
  429. m->dy = WORD(p+20);
  430. m->depth = p[25];
  431. m->model = p[27] < nelem(modelstr) ? modelstr[p[27]] : "unknown";
  432. m->r = p[31];
  433. m->g = p[33];
  434. m->b = p[35];
  435. m->x = p[37];
  436. m->ro = p[32];
  437. m->go = p[34];
  438. m->bo = p[36];
  439. m->xo = p[38];
  440. m->directcolor = p[39];
  441. m->paddr = LONG(p+40);
  442. snprint(m->name, sizeof m->name, "%dx%dx%d",
  443. m->dx, m->dy, m->depth);
  444. if(m->depth <= 8)
  445. snprint(m->chan, sizeof m->chan, "m%d", m->depth);
  446. else if(m->xo)
  447. snprint(m->chan, sizeof m->chan, "x%dr%dg%db%d", m->x, m->r, m->g, m->b);
  448. else
  449. snprint(m->chan, sizeof m->chan, "r%dg%db%d", m->r, m->g, m->b);
  450. return 0;
  451. }
  452. void
  453. vbeprintmodeinfo(Vbe *vbe, int id, char *suffix)
  454. {
  455. Vmode m;
  456. if(vbemodeinfo(vbe, id, &m) < 0){
  457. // Bprint(&stdout, "vesa: cannot get mode 0x%ux: %r\n", id);
  458. return;
  459. }
  460. printitem("vesa", "mode");
  461. Bprint(&stdout, "0x%ux %s %s %s%s\n",
  462. m.id, m.name, m.chan, m.model, suffix);
  463. }
  464. int
  465. vbegetmode(Vbe *vbe)
  466. {
  467. Ureg u;
  468. vbesetup(vbe, &u, 0x4F03);
  469. if(vbecall(vbe, &u) < 0)
  470. return 0;
  471. return u.bx;
  472. }
  473. int
  474. vbesetmode(Vbe *vbe, int id)
  475. {
  476. uchar *p;
  477. Ureg u;
  478. p = vbesetup(vbe, &u, 0x4F02);
  479. if(id != 3)
  480. id |= 3<<14; /* graphics: use linear, do not clear */
  481. u.bx = id;
  482. USED(p);
  483. /*
  484. * can set mode specifics (ht hss hse vt vss vse 0 clockhz refreshhz):
  485. *
  486. u.bx |= 1<<11;
  487. n = atoi(argv[2]); PWORD(p, n); p+=2;
  488. n = atoi(argv[3]); PWORD(p, n); p+=2;
  489. n = atoi(argv[4]); PWORD(p, n); p+=2;
  490. n = atoi(argv[5]); PWORD(p, n); p+=2;
  491. n = atoi(argv[6]); PWORD(p, n); p+=2;
  492. n = atoi(argv[7]); PWORD(p, n); p+=2;
  493. *p++ = atoi(argv[8]);
  494. n = atoi(argv[9]); PLONG(p, n); p += 4;
  495. n = atoi(argv[10]); PWORD(p, n); p += 2;
  496. if(p != vbe.buf+19){
  497. fprint(2, "prog error\n");
  498. return;
  499. }
  500. *
  501. */
  502. return vbecall(vbe, &u);
  503. }
  504. void
  505. vesatextmode(void)
  506. {
  507. if(vbe == nil){
  508. vbe = mkvbe();
  509. if(!vbe)
  510. error("mkvbe: %r\n");
  511. }
  512. if(vbecheck(vbe) < 0)
  513. error("vbecheck: %r\n");
  514. if(vbesetmode(vbe, 3) < 0)
  515. error("vbesetmode: %r\n");
  516. }
  517. static Flag edidflags[] = {
  518. Fdigital, "digital",
  519. Fdpmsstandby, "standby",
  520. Fdpmssuspend, "suspend",
  521. Fdpmsactiveoff, "activeoff",
  522. Fmonochrome, "monochrome",
  523. Fgtf, "gtf",
  524. 0
  525. };
  526. int parseedid128(Edid *e, void *v);
  527. int
  528. vbeddcedid(Vbe *vbe, Edid *e)
  529. {
  530. uchar *p;
  531. Ureg u;
  532. p = vbesetup(vbe, &u, 0x4F15);
  533. u.bx = 0x0001;
  534. if(vbecall(vbe, &u) < 0)
  535. return -1;
  536. if(parseedid128(e, p) < 0){
  537. werrstr("parseedid128: %r");
  538. return -1;
  539. }
  540. return 0;
  541. }
  542. void
  543. printedid(Edid *e)
  544. {
  545. Modelist *l;
  546. printitem("edid", "mfr");
  547. Bprint(&stdout, "%s\n", e->mfr);
  548. printitem("edid", "serialstr");
  549. Bprint(&stdout, "%s\n", e->serialstr);
  550. printitem("edid", "name");
  551. Bprint(&stdout, "%s\n", e->name);
  552. printitem("edid", "product");
  553. Bprint(&stdout, "%d\n", e->product);
  554. printitem("edid", "serial");
  555. Bprint(&stdout, "%lud\n", e->serial);
  556. printitem("edid", "version");
  557. Bprint(&stdout, "%d.%d\n", e->version, e->revision);
  558. printitem("edid", "mfrdate");
  559. Bprint(&stdout, "%d.%d\n", e->mfryear, e->mfrweek);
  560. printitem("edid", "size (cm)");
  561. Bprint(&stdout, "%dx%d\n", e->dxcm, e->dycm);
  562. printitem("edid", "gamma");
  563. Bprint(&stdout, "%.2f\n", e->gamma/100.);
  564. printitem("edid", "vert (Hz)");
  565. Bprint(&stdout, "%d-%d\n", e->rrmin, e->rrmax);
  566. printitem("edid", "horz (Hz)");
  567. Bprint(&stdout, "%d-%d\n", e->hrmin, e->hrmax);
  568. printitem("edid", "pclkmax");
  569. Bprint(&stdout, "%lud\n", e->pclkmax);
  570. printitem("edid", "flags");
  571. printflags(edidflags, e->flags);
  572. for(l=e->modelist; l; l=l->next){
  573. printitem("edid", l->name);
  574. Bprint(&stdout, "\n\t\tclock=%g\n\t\tshb=%d ehb=%d ht=%d\n\t\tvrs=%d vre=%d vt=%d\n\t\thsync=%c vsync=%c %s\n",
  575. l->frequency/1.e6, l->shb, l->ehb, l->ht, l->vrs, l->vre, l->vt, l->hsync?l->hsync:'?', l->vsync?l->vsync:'?', l->interlace?"interlace=v" : "");
  576. }
  577. }
  578. Modelist*
  579. addmode(Modelist *l, Mode m)
  580. {
  581. int rr;
  582. Modelist **lp;
  583. //m.z = 8; // BUG
  584. rr = (m.frequency+m.ht*m.vt/2)/(m.ht*m.vt);
  585. snprint(m.name, sizeof m.name, "%dx%dx%d@%dHz", m.x, m.y, m.z, rr);
  586. if(m.shs == 0)
  587. m.shs = m.shb;
  588. if(m.ehs == 0)
  589. m.ehs = m.ehb;
  590. if(m.vbs == 0)
  591. m.vbs = m.vrs;
  592. if(m.vbe == 0)
  593. m.vbe = m.vbs+1;
  594. for(lp=&l; *lp; lp=&(*lp)->next){
  595. if(strcmp((*lp)->name, m.name) == 0){
  596. (*lp)->Mode = m;
  597. return l;
  598. }
  599. }
  600. *lp = alloc(sizeof(**lp));
  601. (*lp)->Mode = m;
  602. return l;
  603. }
  604. /*
  605. * Parse VESA EDID information. Based on the VESA
  606. * Extended Display Identification Data standard, Version 3,
  607. * November 13, 1997. See /public/doc/vesa/edidv3.pdf.
  608. *
  609. * This only handles 128-byte EDID blocks. Until I find
  610. * a monitor that produces 256-byte blocks, I'm not going
  611. * to try to decode them.
  612. */
  613. /*
  614. * Established timings block. There is a bitmap
  615. * that says whether each mode is supported. Most
  616. * of these have VESA definitions. Those that don't are marked
  617. * as such, and we ignore them (the lookup fails).
  618. */
  619. static char *estabtime[] = {
  620. "720x400@70Hz", /* non-VESA: IBM, VGA */
  621. "720x400@88Hz", /* non-VESA: IBM, XGA2 */
  622. "640x480@60Hz",
  623. "640x480@67Hz", /* non-VESA: Apple, Mac II */
  624. "640x480@72Hz",
  625. "640x480@75Hz",
  626. "800x600@56Hz",
  627. "800x600@60Hz",
  628. "800x600@72Hz",
  629. "800x600@75Hz",
  630. "832x624@75Hz", /* non-VESA: Apple, Mac II */
  631. "1024x768i@87Hz", /* non-VESA: IBM */
  632. "1024x768@60Hz",
  633. "1024x768@70Hz",
  634. "1024x768@75Hz",
  635. "1280x1024@75Hz",
  636. "1152x870@75Hz", /* non-VESA: Apple, Mac II */
  637. };
  638. /*
  639. * Decode the EDID detailed timing block. See pp. 20-21 of the standard.
  640. */
  641. static int
  642. decodedtb(Mode *m, uchar *p)
  643. {
  644. int ha, hb, hso, hspw, rr, va, vb, vso, vspw;
  645. /* int dxmm, dymm, hbord, vbord; */
  646. memset(m, 0, sizeof *m);
  647. m->frequency = ((p[1]<<8) | p[0]) * 10000;
  648. ha = ((p[4] & 0xF0)<<4) | p[2]; /* horizontal active */
  649. hb = ((p[4] & 0x0F)<<8) | p[3]; /* horizontal blanking */
  650. va = ((p[7] & 0xF0)<<4) | p[5]; /* vertical active */
  651. vb = ((p[7] & 0x0F)<<8) | p[6]; /* vertical blanking */
  652. hso = ((p[11] & 0xC0)<<2) | p[8]; /* horizontal sync offset */
  653. hspw = ((p[11] & 0x30)<<4) | p[9]; /* horizontal sync pulse width */
  654. vso = ((p[11] & 0x0C)<<2) | ((p[10] & 0xF0)>>4); /* vertical sync offset */
  655. vspw = ((p[11] & 0x03)<<4) | (p[10] & 0x0F); /* vertical sync pulse width */
  656. /* dxmm = (p[14] & 0xF0)<<4) | p[12]; /* horizontal image size (mm) */
  657. /* dymm = (p[14] & 0x0F)<<8) | p[13]; /* vertical image size (mm) */
  658. /* hbord = p[15]; /* horizontal border (pixels) */
  659. /* vbord = p[16]; /* vertical border (pixels) */
  660. m->x = ha;
  661. m->y = va;
  662. m->ht = ha+hb;
  663. m->shs = ha;
  664. m->shb = ha+hso;
  665. m->ehb = ha+hso+hspw;
  666. m->ehs = ha+hb;
  667. m->vt = va+vb;
  668. m->vbs = va;
  669. m->vrs = va+vso;
  670. m->vre = va+vso+vspw;
  671. m->vbe = va+vb;
  672. if(p[17] & 0x80) /* interlaced */
  673. m->interlace = 'v';
  674. if(p[17] & 0x60) /* some form of stereo monitor mode; no support */
  675. return -1;
  676. /*
  677. * Sync signal description. I have no idea how to properly handle the
  678. * first three cases, which I think are aimed at things other than
  679. * canonical SVGA monitors.
  680. */
  681. switch((p[17] & 0x18)>>3) {
  682. case 0: /* analog composite sync signal*/
  683. case 1: /* bipolar analog composite sync signal */
  684. /* p[17] & 0x04 means serration: hsync during vsync */
  685. /* p[17] & 0x02 means sync pulse appears on RGB not just G */
  686. break;
  687. case 2: /* digital composite sync signal */
  688. /* p[17] & 0x04 means serration: hsync during vsync */
  689. /* p[17] & 0x02 means hsync positive outside vsync */
  690. break;
  691. case 3: /* digital separate sync signal; the norm */
  692. m->vsync = (p[17] & 0x04) ? '+' : '-';
  693. m->hsync = (p[17] & 0x02) ? '+' : '-';
  694. break;
  695. }
  696. /* p[17] & 0x01 is another stereo bit, only referenced if p[17] & 0x60 != 0 */
  697. rr = (m->frequency+m->ht*m->vt/2) / (m->ht*m->vt);
  698. snprint(m->name, sizeof m->name, "%dx%d@%dHz", m->x, m->y, rr);
  699. return 0;
  700. }
  701. extern Mode *vesamodes[];
  702. int
  703. vesalookup(Mode *m, char *name)
  704. {
  705. Mode **p;
  706. for(p=vesamodes; *p; p++)
  707. if(strcmp((*p)->name, name) == 0) {
  708. *m = **p;
  709. return 0;
  710. }
  711. return -1;
  712. }
  713. static int
  714. decodesti(Mode *m, uchar *p)
  715. {
  716. int x, y, rr;
  717. char str[20];
  718. x = (p[0]+31)*8;
  719. switch((p[1]>>6) & 3){
  720. default:
  721. case 0:
  722. y = x;
  723. break;
  724. case 1:
  725. y = (x*4)/3;
  726. break;
  727. case 2:
  728. y = (x*5)/4;
  729. break;
  730. case 3:
  731. y = (x*16)/9;
  732. break;
  733. }
  734. rr = (p[1] & 0x1F) + 60;
  735. sprint(str, "%dx%d@%dHz", x, y, rr);
  736. return vesalookup(m, str);
  737. }
  738. int
  739. parseedid128(Edid *e, void *v)
  740. {
  741. static uchar magic[8] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
  742. uchar *p, *q, sum;
  743. int dpms, estab, i, m, vid;
  744. Mode mode;
  745. memset(e, 0, sizeof *e);
  746. p = (uchar*)v;
  747. if(memcmp(p, magic, 8) != 0) {
  748. werrstr("bad edid header");
  749. return -1;
  750. }
  751. sum = 0;
  752. for(i=0; i<128; i++)
  753. sum += p[i];
  754. if(sum != 0) {
  755. werrstr("bad edid checksum");
  756. return -1;
  757. }
  758. p += 8;
  759. assert(p == (uchar*)v+8); /* assertion offsets from pp. 12-13 of the standard */
  760. /*
  761. * Manufacturer name is three 5-bit ascii letters, packed
  762. * into a big endian [sic] short in big endian order. The high bit is unused.
  763. */
  764. i = (p[0]<<8) | p[1];
  765. p += 2;
  766. e->mfr[0] = 'A'-1 + ((i>>10) & 0x1F);
  767. e->mfr[1] = 'A'-1 + ((i>>5) & 0x1F);
  768. e->mfr[2] = 'A'-1 + (i & 0x1F);
  769. e->mfr[3] = '\0';
  770. /*
  771. * Product code is a little endian short.
  772. */
  773. e->product = (p[1]<<8) | p[0];
  774. p += 2;
  775. /*
  776. * Serial number is a little endian long, 0x01010101 = unused.
  777. */
  778. e->serial = (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  779. p += 4;
  780. if(e->serial == 0x01010101)
  781. e->serial = 0;
  782. e->mfrweek = *p++;
  783. e->mfryear = 1990 + *p++;
  784. assert(p == (uchar*)v+8+10);
  785. /*
  786. * Structure version is next two bytes: major.minor.
  787. */
  788. e->version = *p++;
  789. e->revision = *p++;
  790. assert(p == (uchar*)v+8+10+2);
  791. /*
  792. * Basic display parameters / features.
  793. */
  794. /*
  795. * Video input definition byte: 0x80 tells whether it is
  796. * an analog or digital screen; we ignore the other bits.
  797. * See p. 15 of the standard.
  798. */
  799. vid = *p++;
  800. if(vid & 0x80)
  801. e->flags |= Fdigital;
  802. e->dxcm = *p++;
  803. e->dycm = *p++;
  804. e->gamma = 100 + *p++;
  805. dpms = *p++;
  806. if(dpms & 0x80)
  807. e->flags |= Fdpmsstandby;
  808. if(dpms & 0x40)
  809. e->flags |= Fdpmssuspend;
  810. if(dpms & 0x20)
  811. e->flags |= Fdpmsactiveoff;
  812. if((dpms & 0x18) == 0x00)
  813. e->flags |= Fmonochrome;
  814. if(dpms & 0x01)
  815. e->flags |= Fgtf;
  816. assert(p == (uchar*)v+8+10+2+5);
  817. /*
  818. * Color characteristics currently ignored.
  819. */
  820. p += 10;
  821. assert(p == (uchar*)v+8+10+2+5+10);
  822. /*
  823. * Established timings: a bitmask of 19 preset timings.
  824. */
  825. estab = (p[0]<<16) | (p[1]<<8) | p[2];
  826. p += 3;
  827. for(i=0, m=1<<23; i<nelem(estabtime); i++, m>>=1)
  828. if(estab & m)
  829. if(vesalookup(&mode, estabtime[i]) == 0)
  830. e->modelist = addmode(e->modelist, mode);
  831. assert(p == (uchar*)v+8+10+2+5+10+3);
  832. /*
  833. * Standard Timing Identifications: eight 2-byte selectors
  834. * of more standard timings.
  835. */
  836. for(i=0; i<8; i++, p+=2)
  837. if(decodesti(&mode, p+2*i) == 0)
  838. e->modelist = addmode(e->modelist, mode);
  839. assert(p == (uchar*)v+8+10+2+5+10+3+16);
  840. /*
  841. * Detailed Timings
  842. */
  843. fprint(2, "dt\n");
  844. for(i=0; i<4; i++, p+=18) {
  845. fprint(2, "%.8H\n", p);
  846. if(p[0] || p[1]) { /* detailed timing block: p[0] or p[1] != 0 */
  847. if(decodedtb(&mode, p) == 0)
  848. e->modelist = addmode(e->modelist, mode);
  849. } else if(p[2]==0) { /* monitor descriptor block */
  850. switch(p[3]) {
  851. case 0xFF: /* monitor serial number (13-byte ascii, 0A terminated) */
  852. if(q = memchr(p+5, 0x0A, 13))
  853. *q = '\0';
  854. memset(e->serialstr, 0, sizeof(e->serialstr));
  855. strncpy(e->serialstr, (char*)p+5, 13);
  856. break;
  857. case 0xFE: /* ascii string (13-byte ascii, 0A terminated) */
  858. break;
  859. case 0xFD: /* monitor range limits */
  860. print("fd %.18H\n", p);
  861. e->rrmin = p[5];
  862. e->rrmax = p[6];
  863. e->hrmin = p[7]*1000;
  864. e->hrmax = p[8]*1000;
  865. if(p[9] != 0xFF)
  866. e->pclkmax = p[9]*10*1000000;
  867. break;
  868. case 0xFC: /* monitor name (13-byte ascii, 0A terminated) */
  869. if(q = memchr(p+5, 0x0A, 13))
  870. *q = '\0';
  871. memset(e->name, 0, sizeof(e->name));
  872. strncpy(e->name, (char*)p+5, 13);
  873. break;
  874. case 0xFB: /* extra color point data */
  875. break;
  876. case 0xFA: /* extra standard timing identifications */
  877. for(i=0; i<6; i++)
  878. if(decodesti(&mode, p+5+2*i) == 0)
  879. e->modelist = addmode(e->modelist, mode);
  880. break;
  881. }
  882. }
  883. }
  884. assert(p == (uchar*)v+8+10+2+5+10+3+16+72);
  885. return 0;
  886. }