2
0

edid-generate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * QEMU EDID generator.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qemu/bswap.h"
  9. #include "hw/display/edid.h"
  10. static const struct edid_mode {
  11. uint32_t xres;
  12. uint32_t yres;
  13. uint32_t byte;
  14. uint32_t xtra3;
  15. uint32_t bit;
  16. uint32_t dta;
  17. } modes[] = {
  18. /* dea/dta extension timings (all @ 50 Hz) */
  19. { .xres = 5120, .yres = 2160, .dta = 125 },
  20. { .xres = 4096, .yres = 2160, .dta = 101 },
  21. { .xres = 3840, .yres = 2160, .dta = 96 },
  22. { .xres = 2560, .yres = 1080, .dta = 89 },
  23. { .xres = 2048, .yres = 1152 },
  24. { .xres = 1920, .yres = 1080, .dta = 31 },
  25. /* additional standard timings 3 (all @ 60Hz) */
  26. { .xres = 1920, .yres = 1440, .xtra3 = 11, .bit = 5 },
  27. { .xres = 1920, .yres = 1200, .xtra3 = 10, .bit = 0 },
  28. { .xres = 1856, .yres = 1392, .xtra3 = 10, .bit = 3 },
  29. { .xres = 1792, .yres = 1344, .xtra3 = 10, .bit = 5 },
  30. { .xres = 1600, .yres = 1200, .xtra3 = 9, .bit = 2 },
  31. { .xres = 1680, .yres = 1050, .xtra3 = 9, .bit = 5 },
  32. { .xres = 1440, .yres = 1050, .xtra3 = 8, .bit = 1 },
  33. { .xres = 1440, .yres = 900, .xtra3 = 8, .bit = 5 },
  34. { .xres = 1360, .yres = 768, .xtra3 = 8, .bit = 7 },
  35. { .xres = 1280, .yres = 1024, .xtra3 = 7, .bit = 1 },
  36. { .xres = 1280, .yres = 960, .xtra3 = 7, .bit = 3 },
  37. { .xres = 1280, .yres = 768, .xtra3 = 7, .bit = 6 },
  38. /* established timings (all @ 60Hz) */
  39. { .xres = 1024, .yres = 768, .byte = 36, .bit = 3 },
  40. { .xres = 800, .yres = 600, .byte = 35, .bit = 0 },
  41. { .xres = 640, .yres = 480, .byte = 35, .bit = 5 },
  42. };
  43. static void edid_ext_dta(uint8_t *dta)
  44. {
  45. dta[0] = 0x02;
  46. dta[1] = 0x03;
  47. dta[2] = 0x05;
  48. dta[3] = 0x00;
  49. /* video data block */
  50. dta[4] = 0x40;
  51. }
  52. static void edid_ext_dta_mode(uint8_t *dta, uint8_t nr)
  53. {
  54. dta[dta[2]] = nr;
  55. dta[2]++;
  56. dta[4]++;
  57. }
  58. static int edid_std_mode(uint8_t *mode, uint32_t xres, uint32_t yres)
  59. {
  60. uint32_t aspect;
  61. if (xres == 0 || yres == 0) {
  62. mode[0] = 0x01;
  63. mode[1] = 0x01;
  64. return 0;
  65. } else if (xres * 10 == yres * 16) {
  66. aspect = 0;
  67. } else if (xres * 3 == yres * 4) {
  68. aspect = 1;
  69. } else if (xres * 4 == yres * 5) {
  70. aspect = 2;
  71. } else if (xres * 9 == yres * 16) {
  72. aspect = 3;
  73. } else {
  74. return -1;
  75. }
  76. if ((xres / 8) - 31 > 255) {
  77. return -1;
  78. }
  79. mode[0] = (xres / 8) - 31;
  80. mode[1] = ((aspect << 6) | (60 - 60));
  81. return 0;
  82. }
  83. static void edid_fill_modes(uint8_t *edid, uint8_t *xtra3, uint8_t *dta,
  84. uint32_t maxx, uint32_t maxy)
  85. {
  86. const struct edid_mode *mode;
  87. int std = 38;
  88. int rc, i;
  89. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  90. mode = modes + i;
  91. if ((maxx && mode->xres > maxx) ||
  92. (maxy && mode->yres > maxy)) {
  93. continue;
  94. }
  95. if (mode->byte) {
  96. edid[mode->byte] |= (1 << mode->bit);
  97. } else if (mode->xtra3 && xtra3) {
  98. xtra3[mode->xtra3] |= (1 << mode->bit);
  99. } else if (std < 54) {
  100. rc = edid_std_mode(edid + std, mode->xres, mode->yres);
  101. if (rc == 0) {
  102. std += 2;
  103. }
  104. }
  105. if (dta && mode->dta) {
  106. edid_ext_dta_mode(dta, mode->dta);
  107. }
  108. }
  109. while (std < 54) {
  110. edid_std_mode(edid + std, 0, 0);
  111. std += 2;
  112. }
  113. }
  114. static void edid_checksum(uint8_t *edid)
  115. {
  116. uint32_t sum = 0;
  117. int i;
  118. for (i = 0; i < 127; i++) {
  119. sum += edid[i];
  120. }
  121. sum &= 0xff;
  122. if (sum) {
  123. edid[127] = 0x100 - sum;
  124. }
  125. }
  126. static void edid_desc_type(uint8_t *desc, uint8_t type)
  127. {
  128. desc[0] = 0;
  129. desc[1] = 0;
  130. desc[2] = 0;
  131. desc[3] = type;
  132. desc[4] = 0;
  133. }
  134. static void edid_desc_text(uint8_t *desc, uint8_t type,
  135. const char *text)
  136. {
  137. size_t len;
  138. edid_desc_type(desc, type);
  139. memset(desc + 5, ' ', 13);
  140. len = strlen(text);
  141. if (len > 12) {
  142. len = 12;
  143. }
  144. memcpy(desc + 5, text, len);
  145. desc[5 + len] = '\n';
  146. }
  147. static void edid_desc_ranges(uint8_t *desc)
  148. {
  149. edid_desc_type(desc, 0xfd);
  150. /* vertical (50 -> 125 Hz) */
  151. desc[5] = 50;
  152. desc[6] = 125;
  153. /* horizontal (30 -> 160 kHz) */
  154. desc[7] = 30;
  155. desc[8] = 160;
  156. /* max dot clock (1200 MHz) */
  157. desc[9] = 1200 / 10;
  158. /* no extended timing information */
  159. desc[10] = 0x01;
  160. /* padding */
  161. desc[11] = '\n';
  162. memset(desc + 12, ' ', 6);
  163. }
  164. /* additional standard timings 3 */
  165. static void edid_desc_xtra3_std(uint8_t *desc)
  166. {
  167. edid_desc_type(desc, 0xf7);
  168. desc[5] = 10;
  169. }
  170. static void edid_desc_dummy(uint8_t *desc)
  171. {
  172. edid_desc_type(desc, 0x10);
  173. }
  174. static void edid_desc_timing(uint8_t *desc,
  175. uint32_t xres, uint32_t yres,
  176. uint32_t dpi)
  177. {
  178. /* physical display size */
  179. uint32_t xmm = xres * dpi / 254;
  180. uint32_t ymm = yres * dpi / 254;
  181. /* pull some realistic looking timings out of thin air */
  182. uint32_t xfront = xres * 25 / 100;
  183. uint32_t xsync = xres * 3 / 100;
  184. uint32_t xblank = xres * 35 / 100;
  185. uint32_t yfront = yres * 5 / 1000;
  186. uint32_t ysync = yres * 5 / 1000;
  187. uint32_t yblank = yres * 35 / 1000;
  188. uint32_t clock = 75 * (xres + xblank) * (yres + yblank);
  189. stl_le_p(desc, clock / 10000);
  190. desc[2] = xres & 0xff;
  191. desc[3] = xblank & 0xff;
  192. desc[4] = (((xres & 0xf00) >> 4) |
  193. ((xblank & 0xf00) >> 8));
  194. desc[5] = yres & 0xff;
  195. desc[6] = yblank & 0xff;
  196. desc[7] = (((yres & 0xf00) >> 4) |
  197. ((yblank & 0xf00) >> 8));
  198. desc[8] = xfront & 0xff;
  199. desc[9] = xsync & 0xff;
  200. desc[10] = (((yfront & 0x00f) << 4) |
  201. ((ysync & 0x00f) << 0));
  202. desc[11] = (((xfront & 0x300) >> 2) |
  203. ((xsync & 0x300) >> 4) |
  204. ((yfront & 0x030) >> 2) |
  205. ((ysync & 0x030) >> 4));
  206. desc[12] = xmm & 0xff;
  207. desc[13] = ymm & 0xff;
  208. desc[14] = (((xmm & 0xf00) >> 4) |
  209. ((ymm & 0xf00) >> 8));
  210. desc[17] = 0x18;
  211. }
  212. static uint32_t edid_to_10bit(float value)
  213. {
  214. return (uint32_t)(value * 1024 + 0.5);
  215. }
  216. static void edid_colorspace(uint8_t *edid,
  217. float rx, float ry,
  218. float gx, float gy,
  219. float bx, float by,
  220. float wx, float wy)
  221. {
  222. uint32_t red_x = edid_to_10bit(rx);
  223. uint32_t red_y = edid_to_10bit(ry);
  224. uint32_t green_x = edid_to_10bit(gx);
  225. uint32_t green_y = edid_to_10bit(gy);
  226. uint32_t blue_x = edid_to_10bit(bx);
  227. uint32_t blue_y = edid_to_10bit(by);
  228. uint32_t white_x = edid_to_10bit(wx);
  229. uint32_t white_y = edid_to_10bit(wy);
  230. edid[25] = (((red_x & 0x03) << 6) |
  231. ((red_y & 0x03) << 4) |
  232. ((green_x & 0x03) << 2) |
  233. ((green_y & 0x03) << 0));
  234. edid[26] = (((blue_x & 0x03) << 6) |
  235. ((blue_y & 0x03) << 4) |
  236. ((white_x & 0x03) << 2) |
  237. ((white_y & 0x03) << 0));
  238. edid[27] = red_x >> 2;
  239. edid[28] = red_y >> 2;
  240. edid[29] = green_x >> 2;
  241. edid[30] = green_y >> 2;
  242. edid[31] = blue_x >> 2;
  243. edid[32] = blue_y >> 2;
  244. edid[33] = white_x >> 2;
  245. edid[34] = white_y >> 2;
  246. }
  247. void qemu_edid_generate(uint8_t *edid, size_t size,
  248. qemu_edid_info *info)
  249. {
  250. uint32_t desc = 54;
  251. uint8_t *xtra3 = NULL;
  252. uint8_t *dta = NULL;
  253. /* =============== set defaults =============== */
  254. if (!info->vendor || strlen(info->vendor) != 3) {
  255. info->vendor = "RHT";
  256. }
  257. if (!info->name) {
  258. info->name = "QEMU Monitor";
  259. }
  260. if (!info->dpi) {
  261. info->dpi = 100;
  262. }
  263. if (!info->prefx) {
  264. info->prefx = 1024;
  265. }
  266. if (!info->prefy) {
  267. info->prefy = 768;
  268. }
  269. /* =============== extensions =============== */
  270. if (size >= 256) {
  271. dta = edid + 128;
  272. edid[126]++;
  273. edid_ext_dta(dta);
  274. }
  275. /* =============== header information =============== */
  276. /* fixed */
  277. edid[0] = 0x00;
  278. edid[1] = 0xff;
  279. edid[2] = 0xff;
  280. edid[3] = 0xff;
  281. edid[4] = 0xff;
  282. edid[5] = 0xff;
  283. edid[6] = 0xff;
  284. edid[7] = 0x00;
  285. /* manufacturer id, product code, serial number */
  286. uint16_t vendor_id = ((((info->vendor[0] - '@') & 0x1f) << 10) |
  287. (((info->vendor[1] - '@') & 0x1f) << 5) |
  288. (((info->vendor[2] - '@') & 0x1f) << 0));
  289. uint16_t model_nr = 0x1234;
  290. uint32_t serial_nr = info->serial ? atoi(info->serial) : 0;
  291. stw_be_p(edid + 8, vendor_id);
  292. stw_le_p(edid + 10, model_nr);
  293. stl_le_p(edid + 12, serial_nr);
  294. /* manufacture week and year */
  295. edid[16] = 42;
  296. edid[17] = 2014 - 1990;
  297. /* edid version */
  298. edid[18] = 1;
  299. edid[19] = 4;
  300. /* =============== basic display parameters =============== */
  301. /* video input: digital, 8bpc, displayport */
  302. edid[20] = 0xa5;
  303. /* screen size: undefined */
  304. edid[21] = info->prefx * info->dpi / 2540;
  305. edid[22] = info->prefy * info->dpi / 2540;
  306. /* display gamma: 2.2 */
  307. edid[23] = 220 - 100;
  308. /* supported features bitmap: std sRGB, preferred timing */
  309. edid[24] = 0x06;
  310. /* =============== chromaticity coordinates =============== */
  311. /* standard sRGB colorspace */
  312. edid_colorspace(edid,
  313. 0.6400, 0.3300, /* red */
  314. 0.3000, 0.6000, /* green */
  315. 0.1500, 0.0600, /* blue */
  316. 0.3127, 0.3290); /* white point */
  317. /* =============== established timing bitmap =============== */
  318. /* =============== standard timing information =============== */
  319. /* both filled by edid_fill_modes() */
  320. /* =============== descriptor blocks =============== */
  321. edid_desc_timing(edid + desc, info->prefx, info->prefy, info->dpi);
  322. desc += 18;
  323. edid_desc_ranges(edid + desc);
  324. desc += 18;
  325. if (info->name) {
  326. edid_desc_text(edid + desc, 0xfc, info->name);
  327. desc += 18;
  328. }
  329. if (info->serial) {
  330. edid_desc_text(edid + desc, 0xff, info->serial);
  331. desc += 18;
  332. }
  333. if (desc < 126) {
  334. xtra3 = edid + desc;
  335. edid_desc_xtra3_std(xtra3);
  336. desc += 18;
  337. }
  338. while (desc < 126) {
  339. edid_desc_dummy(edid + desc);
  340. desc += 18;
  341. }
  342. /* =============== finish up =============== */
  343. edid_fill_modes(edid, xtra3, dta, info->maxx, info->maxy);
  344. edid_checksum(edid);
  345. if (dta) {
  346. edid_checksum(dta);
  347. }
  348. }
  349. size_t qemu_edid_size(uint8_t *edid)
  350. {
  351. uint32_t exts;
  352. if (edid[0] != 0x00 ||
  353. edid[1] != 0xff) {
  354. /* doesn't look like a valid edid block */
  355. return 0;
  356. }
  357. exts = edid[126];
  358. return 128 * (exts + 1);
  359. }