edid: move xtra3 descriptor
[qemu/kevin.git] / hw / display / edid-generate.c
blob25f790c7bd851c1f888ab7610a4d9652c58098f0
1 /*
2 * QEMU EDID generator.
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"
11 static const struct edid_mode {
12 uint32_t xres;
13 uint32_t yres;
14 uint32_t byte;
15 uint32_t xtra3;
16 uint32_t bit;
17 uint32_t dta;
18 } modes[] = {
19 /* dea/dta extension timings (all @ 50 Hz) */
20 { .xres = 5120, .yres = 2160, .dta = 125 },
21 { .xres = 4096, .yres = 2160, .dta = 101 },
22 { .xres = 3840, .yres = 2160, .dta = 96 },
23 { .xres = 2560, .yres = 1080, .dta = 89 },
24 { .xres = 2048, .yres = 1152 },
25 { .xres = 1920, .yres = 1080, .dta = 31 },
27 /* additional standard timings 3 (all @ 60Hz) */
28 { .xres = 1920, .yres = 1200, .xtra3 = 10, .bit = 0 },
29 { .xres = 1600, .yres = 1200, .xtra3 = 9, .bit = 2 },
30 { .xres = 1680, .yres = 1050, .xtra3 = 9, .bit = 5 },
31 { .xres = 1440, .yres = 900, .xtra3 = 8, .bit = 5 },
32 { .xres = 1280, .yres = 1024, .xtra3 = 7, .bit = 1 },
33 { .xres = 1280, .yres = 960, .xtra3 = 7, .bit = 3 },
34 { .xres = 1280, .yres = 768, .xtra3 = 7, .bit = 6 },
36 { .xres = 1920, .yres = 1440, .xtra3 = 11, .bit = 5 },
37 { .xres = 1856, .yres = 1392, .xtra3 = 10, .bit = 3 },
38 { .xres = 1792, .yres = 1344, .xtra3 = 10, .bit = 5 },
39 { .xres = 1440, .yres = 1050, .xtra3 = 8, .bit = 1 },
40 { .xres = 1360, .yres = 768, .xtra3 = 8, .bit = 7 },
42 /* established timings (all @ 60Hz) */
43 { .xres = 1024, .yres = 768, .byte = 36, .bit = 3 },
44 { .xres = 800, .yres = 600, .byte = 35, .bit = 0 },
45 { .xres = 640, .yres = 480, .byte = 35, .bit = 5 },
48 static void edid_ext_dta(uint8_t *dta)
50 dta[0] = 0x02;
51 dta[1] = 0x03;
52 dta[2] = 0x05;
53 dta[3] = 0x00;
55 /* video data block */
56 dta[4] = 0x40;
59 static void edid_ext_dta_mode(uint8_t *dta, uint8_t nr)
61 dta[dta[2]] = nr;
62 dta[2]++;
63 dta[4]++;
66 static int edid_std_mode(uint8_t *mode, uint32_t xres, uint32_t yres)
68 uint32_t aspect;
70 if (xres == 0 || yres == 0) {
71 mode[0] = 0x01;
72 mode[1] = 0x01;
73 return 0;
75 } else if (xres * 10 == yres * 16) {
76 aspect = 0;
77 } else if (xres * 3 == yres * 4) {
78 aspect = 1;
79 } else if (xres * 4 == yres * 5) {
80 aspect = 2;
81 } else if (xres * 9 == yres * 16) {
82 aspect = 3;
83 } else {
84 return -1;
87 if ((xres / 8) - 31 > 255) {
88 return -1;
91 mode[0] = (xres / 8) - 31;
92 mode[1] = ((aspect << 6) | (60 - 60));
93 return 0;
96 static void edid_fill_modes(uint8_t *edid, uint8_t *xtra3, uint8_t *dta,
97 uint32_t maxx, uint32_t maxy)
99 const struct edid_mode *mode;
100 int std = 38;
101 int rc, i;
103 for (i = 0; i < ARRAY_SIZE(modes); i++) {
104 mode = modes + i;
106 if ((maxx && mode->xres > maxx) ||
107 (maxy && mode->yres > maxy)) {
108 continue;
111 if (mode->byte) {
112 edid[mode->byte] |= (1 << mode->bit);
113 } else if (std < 54) {
114 rc = edid_std_mode(edid + std, mode->xres, mode->yres);
115 if (rc == 0) {
116 std += 2;
118 } else if (mode->xtra3 && xtra3) {
119 xtra3[mode->xtra3] |= (1 << mode->bit);
122 if (dta && mode->dta) {
123 edid_ext_dta_mode(dta, mode->dta);
127 while (std < 54) {
128 edid_std_mode(edid + std, 0, 0);
129 std += 2;
133 static void edid_checksum(uint8_t *edid)
135 uint32_t sum = 0;
136 int i;
138 for (i = 0; i < 127; i++) {
139 sum += edid[i];
141 sum &= 0xff;
142 if (sum) {
143 edid[127] = 0x100 - sum;
147 static uint8_t *edid_desc_next(uint8_t *edid, uint8_t *dta, uint8_t *desc)
149 if (desc == NULL) {
150 return NULL;
152 if (desc + 18 + 18 < edid + 127) {
153 return desc + 18;
155 return NULL;
158 static void edid_desc_type(uint8_t *desc, uint8_t type)
160 desc[0] = 0;
161 desc[1] = 0;
162 desc[2] = 0;
163 desc[3] = type;
164 desc[4] = 0;
167 static void edid_desc_text(uint8_t *desc, uint8_t type,
168 const char *text)
170 size_t len;
172 edid_desc_type(desc, type);
173 memset(desc + 5, ' ', 13);
175 len = strlen(text);
176 if (len > 12) {
177 len = 12;
179 memcpy(desc + 5, text, len);
180 desc[5 + len] = '\n';
183 static void edid_desc_ranges(uint8_t *desc)
185 edid_desc_type(desc, 0xfd);
187 /* vertical (50 -> 125 Hz) */
188 desc[5] = 50;
189 desc[6] = 125;
191 /* horizontal (30 -> 160 kHz) */
192 desc[7] = 30;
193 desc[8] = 160;
195 /* max dot clock (1200 MHz) */
196 desc[9] = 1200 / 10;
198 /* no extended timing information */
199 desc[10] = 0x01;
201 /* padding */
202 desc[11] = '\n';
203 memset(desc + 12, ' ', 6);
206 /* additional standard timings 3 */
207 static void edid_desc_xtra3_std(uint8_t *desc)
209 edid_desc_type(desc, 0xf7);
210 desc[5] = 10;
213 static void edid_desc_dummy(uint8_t *desc)
215 edid_desc_type(desc, 0x10);
218 static void edid_desc_timing(uint8_t *desc,
219 uint32_t xres, uint32_t yres,
220 uint32_t xmm, uint32_t ymm)
222 /* pull some realistic looking timings out of thin air */
223 uint32_t xfront = xres * 25 / 100;
224 uint32_t xsync = xres * 3 / 100;
225 uint32_t xblank = xres * 35 / 100;
227 uint32_t yfront = yres * 5 / 1000;
228 uint32_t ysync = yres * 5 / 1000;
229 uint32_t yblank = yres * 35 / 1000;
231 uint32_t clock = 75 * (xres + xblank) * (yres + yblank);
233 stl_le_p(desc, clock / 10000);
235 desc[2] = xres & 0xff;
236 desc[3] = xblank & 0xff;
237 desc[4] = (((xres & 0xf00) >> 4) |
238 ((xblank & 0xf00) >> 8));
240 desc[5] = yres & 0xff;
241 desc[6] = yblank & 0xff;
242 desc[7] = (((yres & 0xf00) >> 4) |
243 ((yblank & 0xf00) >> 8));
245 desc[8] = xfront & 0xff;
246 desc[9] = xsync & 0xff;
248 desc[10] = (((yfront & 0x00f) << 4) |
249 ((ysync & 0x00f) << 0));
250 desc[11] = (((xfront & 0x300) >> 2) |
251 ((xsync & 0x300) >> 4) |
252 ((yfront & 0x030) >> 2) |
253 ((ysync & 0x030) >> 4));
255 desc[12] = xmm & 0xff;
256 desc[13] = ymm & 0xff;
257 desc[14] = (((xmm & 0xf00) >> 4) |
258 ((ymm & 0xf00) >> 8));
260 desc[17] = 0x18;
263 static uint32_t edid_to_10bit(float value)
265 return (uint32_t)(value * 1024 + 0.5);
268 static void edid_colorspace(uint8_t *edid,
269 float rx, float ry,
270 float gx, float gy,
271 float bx, float by,
272 float wx, float wy)
274 uint32_t red_x = edid_to_10bit(rx);
275 uint32_t red_y = edid_to_10bit(ry);
276 uint32_t green_x = edid_to_10bit(gx);
277 uint32_t green_y = edid_to_10bit(gy);
278 uint32_t blue_x = edid_to_10bit(bx);
279 uint32_t blue_y = edid_to_10bit(by);
280 uint32_t white_x = edid_to_10bit(wx);
281 uint32_t white_y = edid_to_10bit(wy);
283 edid[25] = (((red_x & 0x03) << 6) |
284 ((red_y & 0x03) << 4) |
285 ((green_x & 0x03) << 2) |
286 ((green_y & 0x03) << 0));
287 edid[26] = (((blue_x & 0x03) << 6) |
288 ((blue_y & 0x03) << 4) |
289 ((white_x & 0x03) << 2) |
290 ((white_y & 0x03) << 0));
291 edid[27] = red_x >> 2;
292 edid[28] = red_y >> 2;
293 edid[29] = green_x >> 2;
294 edid[30] = green_y >> 2;
295 edid[31] = blue_x >> 2;
296 edid[32] = blue_y >> 2;
297 edid[33] = white_x >> 2;
298 edid[34] = white_y >> 2;
301 static uint32_t qemu_edid_dpi_from_mm(uint32_t mm, uint32_t res)
303 return res * 254 / 10 / mm;
306 uint32_t qemu_edid_dpi_to_mm(uint32_t dpi, uint32_t res)
308 return res * 254 / 10 / dpi;
311 void qemu_edid_generate(uint8_t *edid, size_t size,
312 qemu_edid_info *info)
314 uint8_t *desc = edid + 54;
315 uint8_t *xtra3 = NULL;
316 uint8_t *dta = NULL;
317 uint32_t width_mm, height_mm;
318 uint32_t dpi = 100; /* if no width_mm/height_mm */
320 /* =============== set defaults =============== */
322 if (!info->vendor || strlen(info->vendor) != 3) {
323 info->vendor = "RHT";
325 if (!info->name) {
326 info->name = "QEMU Monitor";
328 if (!info->prefx) {
329 info->prefx = 1024;
331 if (!info->prefy) {
332 info->prefy = 768;
334 if (info->width_mm && info->height_mm) {
335 width_mm = info->width_mm;
336 height_mm = info->height_mm;
337 dpi = qemu_edid_dpi_from_mm(width_mm, info->prefx);
338 } else {
339 width_mm = qemu_edid_dpi_to_mm(dpi, info->prefx);
340 height_mm = qemu_edid_dpi_to_mm(dpi, info->prefy);
343 /* =============== extensions =============== */
345 if (size >= 256) {
346 dta = edid + 128;
347 edid[126]++;
348 edid_ext_dta(dta);
351 /* =============== header information =============== */
353 /* fixed */
354 edid[0] = 0x00;
355 edid[1] = 0xff;
356 edid[2] = 0xff;
357 edid[3] = 0xff;
358 edid[4] = 0xff;
359 edid[5] = 0xff;
360 edid[6] = 0xff;
361 edid[7] = 0x00;
363 /* manufacturer id, product code, serial number */
364 uint16_t vendor_id = ((((info->vendor[0] - '@') & 0x1f) << 10) |
365 (((info->vendor[1] - '@') & 0x1f) << 5) |
366 (((info->vendor[2] - '@') & 0x1f) << 0));
367 uint16_t model_nr = 0x1234;
368 uint32_t serial_nr = info->serial ? atoi(info->serial) : 0;
369 stw_be_p(edid + 8, vendor_id);
370 stw_le_p(edid + 10, model_nr);
371 stl_le_p(edid + 12, serial_nr);
373 /* manufacture week and year */
374 edid[16] = 42;
375 edid[17] = 2014 - 1990;
377 /* edid version */
378 edid[18] = 1;
379 edid[19] = 4;
382 /* =============== basic display parameters =============== */
384 /* video input: digital, 8bpc, displayport */
385 edid[20] = 0xa5;
387 /* screen size: undefined */
388 edid[21] = width_mm / 10;
389 edid[22] = height_mm / 10;
391 /* display gamma: 2.2 */
392 edid[23] = 220 - 100;
394 /* supported features bitmap: std sRGB, preferred timing */
395 edid[24] = 0x06;
398 /* =============== chromaticity coordinates =============== */
400 /* standard sRGB colorspace */
401 edid_colorspace(edid,
402 0.6400, 0.3300, /* red */
403 0.3000, 0.6000, /* green */
404 0.1500, 0.0600, /* blue */
405 0.3127, 0.3290); /* white point */
407 /* =============== established timing bitmap =============== */
408 /* =============== standard timing information =============== */
410 /* both filled by edid_fill_modes() */
413 /* =============== descriptor blocks =============== */
415 edid_desc_timing(desc, info->prefx, info->prefy,
416 width_mm, height_mm);
417 desc = edid_desc_next(edid, dta, desc);
419 xtra3 = desc;
420 edid_desc_xtra3_std(xtra3);
421 desc = edid_desc_next(edid, dta, desc);
422 edid_fill_modes(edid, xtra3, dta, info->maxx, info->maxy);
424 * dta video data block is finished at thus point,
425 * so dta descriptor offsets don't move any more.
428 edid_desc_ranges(desc);
429 desc = edid_desc_next(edid, dta, desc);
431 if (desc && info->name) {
432 edid_desc_text(desc, 0xfc, info->name);
433 desc = edid_desc_next(edid, dta, desc);
436 if (desc && info->serial) {
437 edid_desc_text(desc, 0xff, info->serial);
438 desc = edid_desc_next(edid, dta, desc);
441 while (desc) {
442 edid_desc_dummy(desc);
443 desc = edid_desc_next(edid, dta, desc);
446 /* =============== finish up =============== */
448 edid_checksum(edid);
449 if (dta) {
450 edid_checksum(dta);
454 size_t qemu_edid_size(uint8_t *edid)
456 uint32_t exts;
458 if (edid[0] != 0x00 ||
459 edid[1] != 0xff) {
460 /* doesn't look like a valid edid block */
461 return 0;
464 exts = edid[126];
465 return 128 * (exts + 1);