Home | History | Annotate | Line # | Download | only in drm
drm_edid.c revision 1.1.1.1.2.8
      1 /*
      2  * Copyright (c) 2006 Luc Verhaegen (quirks list)
      3  * Copyright (c) 2007-2008 Intel Corporation
      4  *   Jesse Barnes <jesse.barnes (at) intel.com>
      5  * Copyright 2010 Red Hat, Inc.
      6  *
      7  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
      8  * FB layer.
      9  *   Copyright (C) 2006 Dennis Munsie <dmunsie (at) cecropia.com>
     10  *
     11  * Permission is hereby granted, free of charge, to any person obtaining a
     12  * copy of this software and associated documentation files (the "Software"),
     13  * to deal in the Software without restriction, including without limitation
     14  * the rights to use, copy, modify, merge, publish, distribute, sub license,
     15  * and/or sell copies of the Software, and to permit persons to whom the
     16  * Software is furnished to do so, subject to the following conditions:
     17  *
     18  * The above copyright notice and this permission notice (including the
     19  * next paragraph) shall be included in all copies or substantial portions
     20  * of the Software.
     21  *
     22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28  * DEALINGS IN THE SOFTWARE.
     29  */
     30 #include <linux/kernel.h>
     31 #include <linux/slab.h>
     32 #include <linux/i2c.h>
     33 #include <linux/module.h>
     34 #include <linux/moduleparam.h>
     35 #include <linux/export.h>
     36 #include <linux/printk.h>
     37 #include <linux/device.h>
     38 #include <asm/byteorder.h>
     39 #include <drm/drmP.h>
     40 #include <drm/drm_edid.h>
     41 #include "drm_edid_modes.h"
     42 
     43 #define version_greater(edid, maj, min) \
     44 	(((edid)->version > (maj)) || \
     45 	 ((edid)->version == (maj) && (edid)->revision > (min)))
     46 
     47 #define EDID_EST_TIMINGS 16
     48 #define EDID_STD_TIMINGS 8
     49 #define EDID_DETAILED_TIMINGS 4
     50 
     51 /*
     52  * EDID blocks out in the wild have a variety of bugs, try to collect
     53  * them here (note that userspace may work around broken monitors first,
     54  * but fixes should make their way here so that the kernel "just works"
     55  * on as many displays as possible).
     56  */
     57 
     58 /* First detailed mode wrong, use largest 60Hz mode */
     59 #define EDID_QUIRK_PREFER_LARGE_60		(1 << 0)
     60 /* Reported 135MHz pixel clock is too high, needs adjustment */
     61 #define EDID_QUIRK_135_CLOCK_TOO_HIGH		(1 << 1)
     62 /* Prefer the largest mode at 75 Hz */
     63 #define EDID_QUIRK_PREFER_LARGE_75		(1 << 2)
     64 /* Detail timing is in cm not mm */
     65 #define EDID_QUIRK_DETAILED_IN_CM		(1 << 3)
     66 /* Detailed timing descriptors have bogus size values, so just take the
     67  * maximum size and use that.
     68  */
     69 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE	(1 << 4)
     70 /* Monitor forgot to set the first detailed is preferred bit. */
     71 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED	(1 << 5)
     72 /* use +hsync +vsync for detailed mode */
     73 #define EDID_QUIRK_DETAILED_SYNC_PP		(1 << 6)
     74 /* Force reduced-blanking timings for detailed modes */
     75 #define EDID_QUIRK_FORCE_REDUCED_BLANKING	(1 << 7)
     76 
     77 struct detailed_mode_closure {
     78 	struct drm_connector *connector;
     79 	struct edid *edid;
     80 	bool preferred;
     81 	u32 quirks;
     82 	int modes;
     83 };
     84 
     85 #define LEVEL_DMT	0
     86 #define LEVEL_GTF	1
     87 #define LEVEL_GTF2	2
     88 #define LEVEL_CVT	3
     89 
     90 static struct edid_quirk {
     91 	char vendor[4];
     92 	int product_id;
     93 	u32 quirks;
     94 } edid_quirk_list[] = {
     95 	/* ASUS VW222S */
     96 	{ "ACI", 0x22a2, EDID_QUIRK_FORCE_REDUCED_BLANKING },
     97 
     98 	/* Acer AL1706 */
     99 	{ "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
    100 	/* Acer F51 */
    101 	{ "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
    102 	/* Unknown Acer */
    103 	{ "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    104 
    105 	/* Belinea 10 15 55 */
    106 	{ "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
    107 	{ "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
    108 
    109 	/* Envision Peripherals, Inc. EN-7100e */
    110 	{ "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
    111 	/* Envision EN2028 */
    112 	{ "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
    113 
    114 	/* Funai Electronics PM36B */
    115 	{ "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
    116 	  EDID_QUIRK_DETAILED_IN_CM },
    117 
    118 	/* LG Philips LCD LP154W01-A5 */
    119 	{ "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
    120 	{ "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
    121 
    122 	/* Philips 107p5 CRT */
    123 	{ "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    124 
    125 	/* Proview AY765C */
    126 	{ "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
    127 
    128 	/* Samsung SyncMaster 205BW.  Note: irony */
    129 	{ "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
    130 	/* Samsung SyncMaster 22[5-6]BW */
    131 	{ "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
    132 	{ "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
    133 
    134 	/* ViewSonic VA2026w */
    135 	{ "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
    136 };
    137 
    138 /*** DDC fetch and block validation ***/
    139 
    140 static const u8 edid_header[] = {
    141 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
    142 };
    143 
    144  /*
    145  * Sanity check the header of the base EDID block.  Return 8 if the header
    146  * is perfect, down to 0 if it's totally wrong.
    147  */
    148 int drm_edid_header_is_valid(const u8 *raw_edid)
    149 {
    150 	int i, score = 0;
    151 
    152 	for (i = 0; i < sizeof(edid_header); i++)
    153 		if (raw_edid[i] == edid_header[i])
    154 			score++;
    155 
    156 	return score;
    157 }
    158 EXPORT_SYMBOL(drm_edid_header_is_valid);
    159 
    160 static int edid_fixup __read_mostly = 6;
    161 module_param_named(edid_fixup, edid_fixup, int, 0400);
    162 MODULE_PARM_DESC(edid_fixup,
    163 		 "Minimum number of valid EDID header bytes (0-8, default 6)");
    164 
    165 /*
    166  * Sanity check the EDID block (base or extension).  Return 0 if the block
    167  * doesn't check out, or 1 if it's valid.
    168  */
    169 bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid)
    170 {
    171 	int i;
    172 	u8 csum = 0;
    173 	struct edid *edid = (struct edid *)raw_edid;
    174 
    175 	if (edid_fixup > 8 || edid_fixup < 0)
    176 		edid_fixup = 6;
    177 
    178 	if (block == 0) {
    179 		int score = drm_edid_header_is_valid(raw_edid);
    180 		if (score == 8) ;
    181 		else if (score >= edid_fixup) {
    182 			DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
    183 			memcpy(raw_edid, edid_header, sizeof(edid_header));
    184 		} else {
    185 			goto bad;
    186 		}
    187 	}
    188 
    189 	for (i = 0; i < EDID_LENGTH; i++)
    190 		csum += raw_edid[i];
    191 	if (csum) {
    192 		if (print_bad_edid) {
    193 			DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
    194 		}
    195 
    196 		/* allow CEA to slide through, switches mangle this */
    197 		if (raw_edid[0] != 0x02)
    198 			goto bad;
    199 	}
    200 
    201 	/* per-block-type checks */
    202 	switch (raw_edid[0]) {
    203 	case 0: /* base */
    204 		if (edid->version != 1) {
    205 			DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
    206 			goto bad;
    207 		}
    208 
    209 		if (edid->revision > 4)
    210 			DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
    211 		break;
    212 
    213 	default:
    214 		break;
    215 	}
    216 
    217 	return 1;
    218 
    219 bad:
    220 	if (raw_edid && print_bad_edid) {
    221 		printk(KERN_ERR "Raw EDID:\n");
    222 #ifdef __NetBSD__
    223 		for (i = 0; i < EDID_LENGTH; i++) {
    224 			printf("%02x", raw_edid[i]);
    225 			if ((i % 16) == 15)
    226 				printf("\n");
    227 			else
    228 				printf(" ");
    229 		}
    230 #else
    231 		print_hex_dump(KERN_ERR, " \t", DUMP_PREFIX_NONE, 16, 1,
    232 			       raw_edid, EDID_LENGTH, false);
    233 #endif
    234 	}
    235 	return 0;
    236 }
    237 EXPORT_SYMBOL(drm_edid_block_valid);
    238 
    239 /**
    240  * drm_edid_is_valid - sanity check EDID data
    241  * @edid: EDID data
    242  *
    243  * Sanity-check an entire EDID record (including extensions)
    244  */
    245 bool drm_edid_is_valid(struct edid *edid)
    246 {
    247 	int i;
    248 	u8 *raw = (u8 *)edid;
    249 
    250 	if (!edid)
    251 		return false;
    252 
    253 	for (i = 0; i <= edid->extensions; i++)
    254 		if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true))
    255 			return false;
    256 
    257 	return true;
    258 }
    259 EXPORT_SYMBOL(drm_edid_is_valid);
    260 
    261 #ifndef __NetBSD__		/* XXX i2c */
    262 
    263 #define DDC_SEGMENT_ADDR 0x30
    264 /**
    265  * Get EDID information via I2C.
    266  *
    267  * \param adapter : i2c device adaptor
    268  * \param buf     : EDID data buffer to be filled
    269  * \param len     : EDID data buffer length
    270  * \return 0 on success or -1 on failure.
    271  *
    272  * Try to fetch EDID information by calling i2c driver function.
    273  */
    274 static int
    275 drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
    276 		      int block, int len)
    277 {
    278 	unsigned char start = block * EDID_LENGTH;
    279 	unsigned char segment = block >> 1;
    280 	unsigned char xfers = segment ? 3 : 2;
    281 	int ret, retries = 5;
    282 
    283 	/* The core i2c driver will automatically retry the transfer if the
    284 	 * adapter reports EAGAIN. However, we find that bit-banging transfers
    285 	 * are susceptible to errors under a heavily loaded machine and
    286 	 * generate spurious NAKs and timeouts. Retrying the transfer
    287 	 * of the individual block a few times seems to overcome this.
    288 	 */
    289 	do {
    290 		struct i2c_msg msgs[] = {
    291 			{
    292 				.addr	= DDC_SEGMENT_ADDR,
    293 				.flags	= 0,
    294 				.len	= 1,
    295 				.buf	= &segment,
    296 			}, {
    297 				.addr	= DDC_ADDR,
    298 				.flags	= 0,
    299 				.len	= 1,
    300 				.buf	= &start,
    301 			}, {
    302 				.addr	= DDC_ADDR,
    303 				.flags	= I2C_M_RD,
    304 				.len	= len,
    305 				.buf	= buf,
    306 			}
    307 		};
    308 
    309 	/*
    310 	 * Avoid sending the segment addr to not upset non-compliant ddc
    311 	 * monitors.
    312 	 */
    313 		ret = i2c_transfer(adapter, &msgs[3 - xfers], xfers);
    314 
    315 		if (ret == -ENXIO) {
    316 			DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
    317 					adapter->name);
    318 			break;
    319 		}
    320 	} while (ret != xfers && --retries);
    321 
    322 	return ret == xfers ? 0 : -1;
    323 }
    324 
    325 static bool drm_edid_is_zero(u8 *in_edid, int length)
    326 {
    327 	if (memchr_inv(in_edid, 0, length))
    328 		return false;
    329 
    330 	return true;
    331 }
    332 
    333 static u8 *
    334 drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
    335 {
    336 	int i, j = 0, valid_extensions = 0;
    337 	u8 *block, *new;
    338 	bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
    339 
    340 	if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
    341 		return NULL;
    342 
    343 	/* base block fetch */
    344 	for (i = 0; i < 4; i++) {
    345 		if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
    346 			goto out;
    347 		if (drm_edid_block_valid(block, 0, print_bad_edid))
    348 			break;
    349 		if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
    350 			connector->null_edid_counter++;
    351 			goto carp;
    352 		}
    353 	}
    354 	if (i == 4)
    355 		goto carp;
    356 
    357 	/* if there's no extensions, we're done */
    358 	if (block[0x7e] == 0)
    359 		return block;
    360 
    361 	new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
    362 	if (!new)
    363 		goto out;
    364 	block = new;
    365 
    366 	for (j = 1; j <= block[0x7e]; j++) {
    367 		for (i = 0; i < 4; i++) {
    368 			if (drm_do_probe_ddc_edid(adapter,
    369 				  block + (valid_extensions + 1) * EDID_LENGTH,
    370 				  j, EDID_LENGTH))
    371 				goto out;
    372 			if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
    373 				valid_extensions++;
    374 				break;
    375 			}
    376 		}
    377 		if (i == 4)
    378 			dev_warn(connector->dev->dev,
    379 			 "%s: Ignoring invalid EDID block %d.\n",
    380 			 drm_get_connector_name(connector), j);
    381 	}
    382 
    383 	if (valid_extensions != block[0x7e]) {
    384 		block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
    385 		block[0x7e] = valid_extensions;
    386 		new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
    387 		if (!new)
    388 			goto out;
    389 		block = new;
    390 	}
    391 
    392 	return block;
    393 
    394 carp:
    395 	if (print_bad_edid) {
    396 		dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
    397 			 drm_get_connector_name(connector), j);
    398 	}
    399 	connector->bad_edid_counter++;
    400 
    401 out:
    402 	kfree(block);
    403 	return NULL;
    404 }
    405 
    406 /**
    407  * Probe DDC presence.
    408  *
    409  * \param adapter : i2c device adaptor
    410  * \return 1 on success
    411  */
    412 bool
    413 drm_probe_ddc(struct i2c_adapter *adapter)
    414 {
    415 	unsigned char out;
    416 
    417 	return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
    418 }
    419 EXPORT_SYMBOL(drm_probe_ddc);
    420 
    421 /**
    422  * drm_get_edid - get EDID data, if available
    423  * @connector: connector we're probing
    424  * @adapter: i2c adapter to use for DDC
    425  *
    426  * Poke the given i2c channel to grab EDID data if possible.  If found,
    427  * attach it to the connector.
    428  *
    429  * Return edid data or NULL if we couldn't find any.
    430  */
    431 struct edid *drm_get_edid(struct drm_connector *connector,
    432 			  struct i2c_adapter *adapter)
    433 {
    434 	struct edid *edid = NULL;
    435 
    436 	if (drm_probe_ddc(adapter))
    437 		edid = (struct edid *)drm_do_get_edid(connector, adapter);
    438 
    439 	return edid;
    440 }
    441 EXPORT_SYMBOL(drm_get_edid);
    442 
    443 #endif	/* !defined(__NetBSD__) */
    444 
    445 /*** EDID parsing ***/
    446 
    447 /**
    448  * edid_vendor - match a string against EDID's obfuscated vendor field
    449  * @edid: EDID to match
    450  * @vendor: vendor string
    451  *
    452  * Returns true if @vendor is in @edid, false otherwise
    453  */
    454 static bool edid_vendor(struct edid *edid, char *vendor)
    455 {
    456 #ifdef __NetBSD__		/* XXX Avoid shadowing global definition.  */
    457 	char edidv[3];
    458 
    459 	edidv[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
    460 	edidv[1] = (((edid->mfg_id[0] & 0x3) << 3) |
    461 			  ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
    462 	edidv[2] = (edid->mfg_id[1] & 0x1f) + '@';
    463 
    464 	return !strncmp(edidv, vendor, 3);
    465 #else
    466 	char edid_vendor[3];
    467 
    468 	edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
    469 	edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
    470 			  ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
    471 	edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
    472 
    473 	return !strncmp(edid_vendor, vendor, 3);
    474 #endif
    475 }
    476 
    477 /**
    478  * edid_get_quirks - return quirk flags for a given EDID
    479  * @edid: EDID to process
    480  *
    481  * This tells subsequent routines what fixes they need to apply.
    482  */
    483 static u32 edid_get_quirks(struct edid *edid)
    484 {
    485 	struct edid_quirk *quirk;
    486 	int i;
    487 
    488 	for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
    489 		quirk = &edid_quirk_list[i];
    490 
    491 		if (edid_vendor(edid, quirk->vendor) &&
    492 		    (EDID_PRODUCT_ID(edid) == quirk->product_id))
    493 			return quirk->quirks;
    494 	}
    495 
    496 	return 0;
    497 }
    498 
    499 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
    500 #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
    501 
    502 /**
    503  * edid_fixup_preferred - set preferred modes based on quirk list
    504  * @connector: has mode list to fix up
    505  * @quirks: quirks list
    506  *
    507  * Walk the mode list for @connector, clearing the preferred status
    508  * on existing modes and setting it anew for the right mode ala @quirks.
    509  */
    510 static void edid_fixup_preferred(struct drm_connector *connector,
    511 				 u32 quirks)
    512 {
    513 	struct drm_display_mode *t, *cur_mode, *preferred_mode;
    514 	int target_refresh = 0;
    515 
    516 	if (list_empty(&connector->probed_modes))
    517 		return;
    518 
    519 	if (quirks & EDID_QUIRK_PREFER_LARGE_60)
    520 		target_refresh = 60;
    521 	if (quirks & EDID_QUIRK_PREFER_LARGE_75)
    522 		target_refresh = 75;
    523 
    524 	preferred_mode = list_first_entry(&connector->probed_modes,
    525 					  struct drm_display_mode, head);
    526 
    527 	list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
    528 		cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
    529 
    530 		if (cur_mode == preferred_mode)
    531 			continue;
    532 
    533 		/* Largest mode is preferred */
    534 		if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
    535 			preferred_mode = cur_mode;
    536 
    537 		/* At a given size, try to get closest to target refresh */
    538 		if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
    539 		    MODE_REFRESH_DIFF(cur_mode, target_refresh) <
    540 		    MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
    541 			preferred_mode = cur_mode;
    542 		}
    543 	}
    544 
    545 	preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
    546 }
    547 
    548 static bool
    549 mode_is_rb(const struct drm_display_mode *mode)
    550 {
    551 	return (mode->htotal - mode->hdisplay == 160) &&
    552 	       (mode->hsync_end - mode->hdisplay == 80) &&
    553 	       (mode->hsync_end - mode->hsync_start == 32) &&
    554 	       (mode->vsync_start - mode->vdisplay == 3);
    555 }
    556 
    557 /*
    558  * drm_mode_find_dmt - Create a copy of a mode if present in DMT
    559  * @dev: Device to duplicate against
    560  * @hsize: Mode width
    561  * @vsize: Mode height
    562  * @fresh: Mode refresh rate
    563  * @rb: Mode reduced-blanking-ness
    564  *
    565  * Walk the DMT mode list looking for a match for the given parameters.
    566  * Return a newly allocated copy of the mode, or NULL if not found.
    567  */
    568 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
    569 					   int hsize, int vsize, int fresh,
    570 					   bool rb)
    571 {
    572 	int i;
    573 
    574 	for (i = 0; i < drm_num_dmt_modes; i++) {
    575 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
    576 		if (hsize != ptr->hdisplay)
    577 			continue;
    578 		if (vsize != ptr->vdisplay)
    579 			continue;
    580 		if (fresh != drm_mode_vrefresh(ptr))
    581 			continue;
    582 		if (rb != mode_is_rb(ptr))
    583 			continue;
    584 
    585 		return drm_mode_duplicate(dev, ptr);
    586 	}
    587 
    588 	return NULL;
    589 }
    590 EXPORT_SYMBOL(drm_mode_find_dmt);
    591 
    592 typedef void detailed_cb(struct detailed_timing *timing, void *closure);
    593 
    594 static void
    595 cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
    596 {
    597 	int i, n = 0;
    598 	u8 d = ext[0x02];
    599 	u8 *det_base = ext + d;
    600 
    601 	n = (127 - d) / 18;
    602 	for (i = 0; i < n; i++)
    603 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
    604 }
    605 
    606 static void
    607 vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
    608 {
    609 	unsigned int i, n = min((int)ext[0x02], 6);
    610 	u8 *det_base = ext + 5;
    611 
    612 	if (ext[0x01] != 1)
    613 		return; /* unknown version */
    614 
    615 	for (i = 0; i < n; i++)
    616 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
    617 }
    618 
    619 static void
    620 drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
    621 {
    622 	int i;
    623 	struct edid *edid = (struct edid *)raw_edid;
    624 
    625 	if (edid == NULL)
    626 		return;
    627 
    628 	for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
    629 		cb(&(edid->detailed_timings[i]), closure);
    630 
    631 	for (i = 1; i <= raw_edid[0x7e]; i++) {
    632 		u8 *ext = raw_edid + (i * EDID_LENGTH);
    633 		switch (*ext) {
    634 		case CEA_EXT:
    635 			cea_for_each_detailed_block(ext, cb, closure);
    636 			break;
    637 		case VTB_EXT:
    638 			vtb_for_each_detailed_block(ext, cb, closure);
    639 			break;
    640 		default:
    641 			break;
    642 		}
    643 	}
    644 }
    645 
    646 static void
    647 is_rb(struct detailed_timing *t, void *data)
    648 {
    649 	u8 *r = (u8 *)t;
    650 	if (r[3] == EDID_DETAIL_MONITOR_RANGE)
    651 		if (r[15] & 0x10)
    652 			*(bool *)data = true;
    653 }
    654 
    655 /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
    656 static bool
    657 drm_monitor_supports_rb(struct edid *edid)
    658 {
    659 	if (edid->revision >= 4) {
    660 		bool ret = false;
    661 		drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
    662 		return ret;
    663 	}
    664 
    665 	return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
    666 }
    667 
    668 static void
    669 find_gtf2(struct detailed_timing *t, void *data)
    670 {
    671 	u8 *r = (u8 *)t;
    672 	if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
    673 		*(u8 **)data = r;
    674 }
    675 
    676 /* Secondary GTF curve kicks in above some break frequency */
    677 static int
    678 drm_gtf2_hbreak(struct edid *edid)
    679 {
    680 	u8 *r = NULL;
    681 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    682 	return r ? (r[12] * 2) : 0;
    683 }
    684 
    685 static int
    686 drm_gtf2_2c(struct edid *edid)
    687 {
    688 	u8 *r = NULL;
    689 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    690 	return r ? r[13] : 0;
    691 }
    692 
    693 static int
    694 drm_gtf2_m(struct edid *edid)
    695 {
    696 	u8 *r = NULL;
    697 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    698 	return r ? (r[15] << 8) + r[14] : 0;
    699 }
    700 
    701 static int
    702 drm_gtf2_k(struct edid *edid)
    703 {
    704 	u8 *r = NULL;
    705 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    706 	return r ? r[16] : 0;
    707 }
    708 
    709 static int
    710 drm_gtf2_2j(struct edid *edid)
    711 {
    712 	u8 *r = NULL;
    713 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
    714 	return r ? r[17] : 0;
    715 }
    716 
    717 /**
    718  * standard_timing_level - get std. timing level(CVT/GTF/DMT)
    719  * @edid: EDID block to scan
    720  */
    721 static int standard_timing_level(struct edid *edid)
    722 {
    723 	if (edid->revision >= 2) {
    724 		if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
    725 			return LEVEL_CVT;
    726 		if (drm_gtf2_hbreak(edid))
    727 			return LEVEL_GTF2;
    728 		return LEVEL_GTF;
    729 	}
    730 	return LEVEL_DMT;
    731 }
    732 
    733 /*
    734  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
    735  * monitors fill with ascii space (0x20) instead.
    736  */
    737 static int
    738 bad_std_timing(u8 a, u8 b)
    739 {
    740 	return (a == 0x00 && b == 0x00) ||
    741 	       (a == 0x01 && b == 0x01) ||
    742 	       (a == 0x20 && b == 0x20);
    743 }
    744 
    745 /**
    746  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
    747  * @t: standard timing params
    748  * @timing_level: standard timing level
    749  *
    750  * Take the standard timing params (in this case width, aspect, and refresh)
    751  * and convert them into a real mode using CVT/GTF/DMT.
    752  */
    753 static struct drm_display_mode *
    754 drm_mode_std(struct drm_connector *connector, struct edid *edid,
    755 	     struct std_timing *t, int revision)
    756 {
    757 	struct drm_device *dev = connector->dev;
    758 	struct drm_display_mode *m, *mode = NULL;
    759 	int hsize, vsize;
    760 	int vrefresh_rate;
    761 	unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
    762 		>> EDID_TIMING_ASPECT_SHIFT;
    763 	unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
    764 		>> EDID_TIMING_VFREQ_SHIFT;
    765 	int timing_level = standard_timing_level(edid);
    766 
    767 	if (bad_std_timing(t->hsize, t->vfreq_aspect))
    768 		return NULL;
    769 
    770 	/* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
    771 	hsize = t->hsize * 8 + 248;
    772 	/* vrefresh_rate = vfreq + 60 */
    773 	vrefresh_rate = vfreq + 60;
    774 	/* the vdisplay is calculated based on the aspect ratio */
    775 	if (aspect_ratio == 0) {
    776 		if (revision < 3)
    777 			vsize = hsize;
    778 		else
    779 			vsize = (hsize * 10) / 16;
    780 	} else if (aspect_ratio == 1)
    781 		vsize = (hsize * 3) / 4;
    782 	else if (aspect_ratio == 2)
    783 		vsize = (hsize * 4) / 5;
    784 	else
    785 		vsize = (hsize * 9) / 16;
    786 
    787 	/* HDTV hack, part 1 */
    788 	if (vrefresh_rate == 60 &&
    789 	    ((hsize == 1360 && vsize == 765) ||
    790 	     (hsize == 1368 && vsize == 769))) {
    791 		hsize = 1366;
    792 		vsize = 768;
    793 	}
    794 
    795 	/*
    796 	 * If this connector already has a mode for this size and refresh
    797 	 * rate (because it came from detailed or CVT info), use that
    798 	 * instead.  This way we don't have to guess at interlace or
    799 	 * reduced blanking.
    800 	 */
    801 	list_for_each_entry(m, &connector->probed_modes, head)
    802 		if (m->hdisplay == hsize && m->vdisplay == vsize &&
    803 		    drm_mode_vrefresh(m) == vrefresh_rate)
    804 			return NULL;
    805 
    806 	/* HDTV hack, part 2 */
    807 	if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
    808 		mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
    809 				    false);
    810 		mode->hdisplay = 1366;
    811 		mode->hsync_start = mode->hsync_start - 1;
    812 		mode->hsync_end = mode->hsync_end - 1;
    813 		return mode;
    814 	}
    815 
    816 	/* check whether it can be found in default mode table */
    817 	if (drm_monitor_supports_rb(edid)) {
    818 		mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
    819 					 true);
    820 		if (mode)
    821 			return mode;
    822 	}
    823 	mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
    824 	if (mode)
    825 		return mode;
    826 
    827 	/* okay, generate it */
    828 	switch (timing_level) {
    829 	case LEVEL_DMT:
    830 		break;
    831 	case LEVEL_GTF:
    832 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
    833 		break;
    834 	case LEVEL_GTF2:
    835 		/*
    836 		 * This is potentially wrong if there's ever a monitor with
    837 		 * more than one ranges section, each claiming a different
    838 		 * secondary GTF curve.  Please don't do that.
    839 		 */
    840 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
    841 		if (!mode)
    842 			return NULL;
    843 		if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
    844 			drm_mode_destroy(dev, mode);
    845 			mode = drm_gtf_mode_complex(dev, hsize, vsize,
    846 						    vrefresh_rate, 0, 0,
    847 						    drm_gtf2_m(edid),
    848 						    drm_gtf2_2c(edid),
    849 						    drm_gtf2_k(edid),
    850 						    drm_gtf2_2j(edid));
    851 		}
    852 		break;
    853 	case LEVEL_CVT:
    854 		mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
    855 				    false);
    856 		break;
    857 	}
    858 	return mode;
    859 }
    860 
    861 /*
    862  * EDID is delightfully ambiguous about how interlaced modes are to be
    863  * encoded.  Our internal representation is of frame height, but some
    864  * HDTV detailed timings are encoded as field height.
    865  *
    866  * The format list here is from CEA, in frame size.  Technically we
    867  * should be checking refresh rate too.  Whatever.
    868  */
    869 static void
    870 drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
    871 			    struct detailed_pixel_timing *pt)
    872 {
    873 	int i;
    874 	static const struct {
    875 		int w, h;
    876 	} cea_interlaced[] = {
    877 		{ 1920, 1080 },
    878 		{  720,  480 },
    879 		{ 1440,  480 },
    880 		{ 2880,  480 },
    881 		{  720,  576 },
    882 		{ 1440,  576 },
    883 		{ 2880,  576 },
    884 	};
    885 
    886 	if (!(pt->misc & DRM_EDID_PT_INTERLACED))
    887 		return;
    888 
    889 	for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
    890 		if ((mode->hdisplay == cea_interlaced[i].w) &&
    891 		    (mode->vdisplay == cea_interlaced[i].h / 2)) {
    892 			mode->vdisplay *= 2;
    893 			mode->vsync_start *= 2;
    894 			mode->vsync_end *= 2;
    895 			mode->vtotal *= 2;
    896 			mode->vtotal |= 1;
    897 		}
    898 	}
    899 
    900 	mode->flags |= DRM_MODE_FLAG_INTERLACE;
    901 }
    902 
    903 /**
    904  * drm_mode_detailed - create a new mode from an EDID detailed timing section
    905  * @dev: DRM device (needed to create new mode)
    906  * @edid: EDID block
    907  * @timing: EDID detailed timing info
    908  * @quirks: quirks to apply
    909  *
    910  * An EDID detailed timing block contains enough info for us to create and
    911  * return a new struct drm_display_mode.
    912  */
    913 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
    914 						  struct edid *edid,
    915 						  struct detailed_timing *timing,
    916 						  u32 quirks)
    917 {
    918 	struct drm_display_mode *mode;
    919 	struct detailed_pixel_timing *pt = &timing->data.pixel_data;
    920 	unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
    921 	unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
    922 	unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
    923 	unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
    924 	unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
    925 	unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
    926 	unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
    927 	unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
    928 
    929 	/* ignore tiny modes */
    930 	if (hactive < 64 || vactive < 64)
    931 		return NULL;
    932 
    933 	if (pt->misc & DRM_EDID_PT_STEREO) {
    934 		printk(KERN_WARNING "stereo mode not supported\n");
    935 		return NULL;
    936 	}
    937 	if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
    938 		printk(KERN_WARNING "composite sync not supported\n");
    939 	}
    940 
    941 	/* it is incorrect if hsync/vsync width is zero */
    942 	if (!hsync_pulse_width || !vsync_pulse_width) {
    943 		DRM_DEBUG_KMS("Incorrect Detailed timing. "
    944 				"Wrong Hsync/Vsync pulse width\n");
    945 		return NULL;
    946 	}
    947 
    948 	if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
    949 		mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
    950 		if (!mode)
    951 			return NULL;
    952 
    953 		goto set_size;
    954 	}
    955 
    956 	mode = drm_mode_create(dev);
    957 	if (!mode)
    958 		return NULL;
    959 
    960 	if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
    961 		timing->pixel_clock = cpu_to_le16(1088);
    962 
    963 	mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
    964 
    965 	mode->hdisplay = hactive;
    966 	mode->hsync_start = mode->hdisplay + hsync_offset;
    967 	mode->hsync_end = mode->hsync_start + hsync_pulse_width;
    968 	mode->htotal = mode->hdisplay + hblank;
    969 
    970 	mode->vdisplay = vactive;
    971 	mode->vsync_start = mode->vdisplay + vsync_offset;
    972 	mode->vsync_end = mode->vsync_start + vsync_pulse_width;
    973 	mode->vtotal = mode->vdisplay + vblank;
    974 
    975 	/* Some EDIDs have bogus h/vtotal values */
    976 	if (mode->hsync_end > mode->htotal)
    977 		mode->htotal = mode->hsync_end + 1;
    978 	if (mode->vsync_end > mode->vtotal)
    979 		mode->vtotal = mode->vsync_end + 1;
    980 
    981 	drm_mode_do_interlace_quirk(mode, pt);
    982 
    983 	if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
    984 		pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
    985 	}
    986 
    987 	mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
    988 		DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
    989 	mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
    990 		DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
    991 
    992 set_size:
    993 	mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
    994 	mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
    995 
    996 	if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
    997 		mode->width_mm *= 10;
    998 		mode->height_mm *= 10;
    999 	}
   1000 
   1001 	if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
   1002 		mode->width_mm = edid->width_cm * 10;
   1003 		mode->height_mm = edid->height_cm * 10;
   1004 	}
   1005 
   1006 	mode->type = DRM_MODE_TYPE_DRIVER;
   1007 	drm_mode_set_name(mode);
   1008 
   1009 	return mode;
   1010 }
   1011 
   1012 static bool
   1013 mode_in_hsync_range(const struct drm_display_mode *mode,
   1014 		    struct edid *edid, u8 *t)
   1015 {
   1016 	int hsync, hmin, hmax;
   1017 
   1018 	hmin = t[7];
   1019 	if (edid->revision >= 4)
   1020 	    hmin += ((t[4] & 0x04) ? 255 : 0);
   1021 	hmax = t[8];
   1022 	if (edid->revision >= 4)
   1023 	    hmax += ((t[4] & 0x08) ? 255 : 0);
   1024 	hsync = drm_mode_hsync(mode);
   1025 
   1026 	return (hsync <= hmax && hsync >= hmin);
   1027 }
   1028 
   1029 static bool
   1030 mode_in_vsync_range(const struct drm_display_mode *mode,
   1031 		    struct edid *edid, u8 *t)
   1032 {
   1033 	int vsync, vmin, vmax;
   1034 
   1035 	vmin = t[5];
   1036 	if (edid->revision >= 4)
   1037 	    vmin += ((t[4] & 0x01) ? 255 : 0);
   1038 	vmax = t[6];
   1039 	if (edid->revision >= 4)
   1040 	    vmax += ((t[4] & 0x02) ? 255 : 0);
   1041 	vsync = drm_mode_vrefresh(mode);
   1042 
   1043 	return (vsync <= vmax && vsync >= vmin);
   1044 }
   1045 
   1046 static u32
   1047 range_pixel_clock(struct edid *edid, u8 *t)
   1048 {
   1049 	/* unspecified */
   1050 	if (t[9] == 0 || t[9] == 255)
   1051 		return 0;
   1052 
   1053 	/* 1.4 with CVT support gives us real precision, yay */
   1054 	if (edid->revision >= 4 && t[10] == 0x04)
   1055 		return (t[9] * 10000) - ((t[12] >> 2) * 250);
   1056 
   1057 	/* 1.3 is pathetic, so fuzz up a bit */
   1058 	return t[9] * 10000 + 5001;
   1059 }
   1060 
   1061 static bool
   1062 mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
   1063 	      struct detailed_timing *timing)
   1064 {
   1065 	u32 max_clock;
   1066 	u8 *t = (u8 *)timing;
   1067 
   1068 	if (!mode_in_hsync_range(mode, edid, t))
   1069 		return false;
   1070 
   1071 	if (!mode_in_vsync_range(mode, edid, t))
   1072 		return false;
   1073 
   1074 	if ((max_clock = range_pixel_clock(edid, t)))
   1075 		if (mode->clock > max_clock)
   1076 			return false;
   1077 
   1078 	/* 1.4 max horizontal check */
   1079 	if (edid->revision >= 4 && t[10] == 0x04)
   1080 		if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
   1081 			return false;
   1082 
   1083 	if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
   1084 		return false;
   1085 
   1086 	return true;
   1087 }
   1088 
   1089 static bool valid_inferred_mode(const struct drm_connector *connector,
   1090 				const struct drm_display_mode *mode)
   1091 {
   1092 	struct drm_display_mode *m;
   1093 	bool ok = false;
   1094 
   1095 	list_for_each_entry(m, &connector->probed_modes, head) {
   1096 		if (mode->hdisplay == m->hdisplay &&
   1097 		    mode->vdisplay == m->vdisplay &&
   1098 		    drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
   1099 			return false; /* duplicated */
   1100 		if (mode->hdisplay <= m->hdisplay &&
   1101 		    mode->vdisplay <= m->vdisplay)
   1102 			ok = true;
   1103 	}
   1104 	return ok;
   1105 }
   1106 
   1107 static int
   1108 drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1109 			struct detailed_timing *timing)
   1110 {
   1111 	int i, modes = 0;
   1112 	struct drm_display_mode *newmode;
   1113 	struct drm_device *dev = connector->dev;
   1114 
   1115 	for (i = 0; i < drm_num_dmt_modes; i++) {
   1116 		if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
   1117 		    valid_inferred_mode(connector, drm_dmt_modes + i)) {
   1118 			newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
   1119 			if (newmode) {
   1120 				drm_mode_probed_add(connector, newmode);
   1121 				modes++;
   1122 			}
   1123 		}
   1124 	}
   1125 
   1126 	return modes;
   1127 }
   1128 
   1129 /* fix up 1366x768 mode from 1368x768;
   1130  * GFT/CVT can't express 1366 width which isn't dividable by 8
   1131  */
   1132 static void fixup_mode_1366x768(struct drm_display_mode *mode)
   1133 {
   1134 	if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
   1135 		mode->hdisplay = 1366;
   1136 		mode->hsync_start--;
   1137 		mode->hsync_end--;
   1138 		drm_mode_set_name(mode);
   1139 	}
   1140 }
   1141 
   1142 static int
   1143 drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1144 			struct detailed_timing *timing)
   1145 {
   1146 	int i, modes = 0;
   1147 	struct drm_display_mode *newmode;
   1148 	struct drm_device *dev = connector->dev;
   1149 
   1150 	for (i = 0; i < num_extra_modes; i++) {
   1151 		const struct minimode *m = &extra_modes[i];
   1152 		newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
   1153 		if (!newmode)
   1154 			return modes;
   1155 
   1156 		fixup_mode_1366x768(newmode);
   1157 		if (!mode_in_range(newmode, edid, timing) ||
   1158 		    !valid_inferred_mode(connector, newmode)) {
   1159 			drm_mode_destroy(dev, newmode);
   1160 			continue;
   1161 		}
   1162 
   1163 		drm_mode_probed_add(connector, newmode);
   1164 		modes++;
   1165 	}
   1166 
   1167 	return modes;
   1168 }
   1169 
   1170 static int
   1171 drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
   1172 			struct detailed_timing *timing)
   1173 {
   1174 	int i, modes = 0;
   1175 	struct drm_display_mode *newmode;
   1176 	struct drm_device *dev = connector->dev;
   1177 	bool rb = drm_monitor_supports_rb(edid);
   1178 
   1179 	for (i = 0; i < num_extra_modes; i++) {
   1180 		const struct minimode *m = &extra_modes[i];
   1181 		newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
   1182 		if (!newmode)
   1183 			return modes;
   1184 
   1185 		fixup_mode_1366x768(newmode);
   1186 		if (!mode_in_range(newmode, edid, timing) ||
   1187 		    !valid_inferred_mode(connector, newmode)) {
   1188 			drm_mode_destroy(dev, newmode);
   1189 			continue;
   1190 		}
   1191 
   1192 		drm_mode_probed_add(connector, newmode);
   1193 		modes++;
   1194 	}
   1195 
   1196 	return modes;
   1197 }
   1198 
   1199 static void
   1200 do_inferred_modes(struct detailed_timing *timing, void *c)
   1201 {
   1202 	struct detailed_mode_closure *closure = c;
   1203 	struct detailed_non_pixel *data = &timing->data.other_data;
   1204 	struct detailed_data_monitor_range *range = &data->data.range;
   1205 
   1206 	if (data->type != EDID_DETAIL_MONITOR_RANGE)
   1207 		return;
   1208 
   1209 	closure->modes += drm_dmt_modes_for_range(closure->connector,
   1210 						  closure->edid,
   1211 						  timing);
   1212 
   1213 	if (!version_greater(closure->edid, 1, 1))
   1214 		return; /* GTF not defined yet */
   1215 
   1216 	switch (range->flags) {
   1217 	case 0x02: /* secondary gtf, XXX could do more */
   1218 	case 0x00: /* default gtf */
   1219 		closure->modes += drm_gtf_modes_for_range(closure->connector,
   1220 							  closure->edid,
   1221 							  timing);
   1222 		break;
   1223 	case 0x04: /* cvt, only in 1.4+ */
   1224 		if (!version_greater(closure->edid, 1, 3))
   1225 			break;
   1226 
   1227 		closure->modes += drm_cvt_modes_for_range(closure->connector,
   1228 							  closure->edid,
   1229 							  timing);
   1230 		break;
   1231 	case 0x01: /* just the ranges, no formula */
   1232 	default:
   1233 		break;
   1234 	}
   1235 }
   1236 
   1237 static int
   1238 add_inferred_modes(struct drm_connector *connector, struct edid *edid)
   1239 {
   1240 	struct detailed_mode_closure closure = {
   1241 		connector, edid, 0, 0, 0
   1242 	};
   1243 
   1244 	if (version_greater(edid, 1, 0))
   1245 		drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
   1246 					    &closure);
   1247 
   1248 	return closure.modes;
   1249 }
   1250 
   1251 static int
   1252 drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
   1253 {
   1254 	int i, j, m, modes = 0;
   1255 	struct drm_display_mode *mode;
   1256 	u8 *est = ((u8 *)timing) + 5;
   1257 
   1258 	for (i = 0; i < 6; i++) {
   1259 		for (j = 7; j > 0; j--) {
   1260 			m = (i * 8) + (7 - j);
   1261 			if (m >= ARRAY_SIZE(est3_modes))
   1262 				break;
   1263 			if (est[i] & (1 << j)) {
   1264 				mode = drm_mode_find_dmt(connector->dev,
   1265 							 est3_modes[m].w,
   1266 							 est3_modes[m].h,
   1267 							 est3_modes[m].r,
   1268 							 est3_modes[m].rb);
   1269 				if (mode) {
   1270 					drm_mode_probed_add(connector, mode);
   1271 					modes++;
   1272 				}
   1273 			}
   1274 		}
   1275 	}
   1276 
   1277 	return modes;
   1278 }
   1279 
   1280 static void
   1281 do_established_modes(struct detailed_timing *timing, void *c)
   1282 {
   1283 	struct detailed_mode_closure *closure = c;
   1284 	struct detailed_non_pixel *data = &timing->data.other_data;
   1285 
   1286 	if (data->type == EDID_DETAIL_EST_TIMINGS)
   1287 		closure->modes += drm_est3_modes(closure->connector, timing);
   1288 }
   1289 
   1290 /**
   1291  * add_established_modes - get est. modes from EDID and add them
   1292  * @edid: EDID block to scan
   1293  *
   1294  * Each EDID block contains a bitmap of the supported "established modes" list
   1295  * (defined above).  Tease them out and add them to the global modes list.
   1296  */
   1297 static int
   1298 add_established_modes(struct drm_connector *connector, struct edid *edid)
   1299 {
   1300 	struct drm_device *dev = connector->dev;
   1301 	unsigned long est_bits = edid->established_timings.t1 |
   1302 		(edid->established_timings.t2 << 8) |
   1303 		((edid->established_timings.mfg_rsvd & 0x80) << 9);
   1304 	int i, modes = 0;
   1305 	struct detailed_mode_closure closure = {
   1306 		connector, edid, 0, 0, 0
   1307 	};
   1308 
   1309 	for (i = 0; i <= EDID_EST_TIMINGS; i++) {
   1310 		if (est_bits & (1<<i)) {
   1311 			struct drm_display_mode *newmode;
   1312 			newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
   1313 			if (newmode) {
   1314 				drm_mode_probed_add(connector, newmode);
   1315 				modes++;
   1316 			}
   1317 		}
   1318 	}
   1319 
   1320 	if (version_greater(edid, 1, 0))
   1321 		    drm_for_each_detailed_block((u8 *)edid,
   1322 						do_established_modes, &closure);
   1323 
   1324 	return modes + closure.modes;
   1325 }
   1326 
   1327 static void
   1328 do_standard_modes(struct detailed_timing *timing, void *c)
   1329 {
   1330 	struct detailed_mode_closure *closure = c;
   1331 	struct detailed_non_pixel *data = &timing->data.other_data;
   1332 	struct drm_connector *connector = closure->connector;
   1333 	struct edid *edid = closure->edid;
   1334 
   1335 	if (data->type == EDID_DETAIL_STD_MODES) {
   1336 		int i;
   1337 		for (i = 0; i < 6; i++) {
   1338 			struct std_timing *std;
   1339 			struct drm_display_mode *newmode;
   1340 
   1341 			std = &data->data.timings[i];
   1342 			newmode = drm_mode_std(connector, edid, std,
   1343 					       edid->revision);
   1344 			if (newmode) {
   1345 				drm_mode_probed_add(connector, newmode);
   1346 				closure->modes++;
   1347 			}
   1348 		}
   1349 	}
   1350 }
   1351 
   1352 /**
   1353  * add_standard_modes - get std. modes from EDID and add them
   1354  * @edid: EDID block to scan
   1355  *
   1356  * Standard modes can be calculated using the appropriate standard (DMT,
   1357  * GTF or CVT. Grab them from @edid and add them to the list.
   1358  */
   1359 static int
   1360 add_standard_modes(struct drm_connector *connector, struct edid *edid)
   1361 {
   1362 	int i, modes = 0;
   1363 	struct detailed_mode_closure closure = {
   1364 		connector, edid, 0, 0, 0
   1365 	};
   1366 
   1367 	for (i = 0; i < EDID_STD_TIMINGS; i++) {
   1368 		struct drm_display_mode *newmode;
   1369 
   1370 		newmode = drm_mode_std(connector, edid,
   1371 				       &edid->standard_timings[i],
   1372 				       edid->revision);
   1373 		if (newmode) {
   1374 			drm_mode_probed_add(connector, newmode);
   1375 			modes++;
   1376 		}
   1377 	}
   1378 
   1379 	if (version_greater(edid, 1, 0))
   1380 		drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
   1381 					    &closure);
   1382 
   1383 	/* XXX should also look for standard codes in VTB blocks */
   1384 
   1385 	return modes + closure.modes;
   1386 }
   1387 
   1388 static int drm_cvt_modes(struct drm_connector *connector,
   1389 			 struct detailed_timing *timing)
   1390 {
   1391 	int i, j, modes = 0;
   1392 	struct drm_display_mode *newmode;
   1393 	struct drm_device *dev = connector->dev;
   1394 	struct cvt_timing *cvt;
   1395 	const int rates[] = { 60, 85, 75, 60, 50 };
   1396 	const u8 empty[3] = { 0, 0, 0 };
   1397 
   1398 	for (i = 0; i < 4; i++) {
   1399 		int uninitialized_var(width), height;
   1400 		cvt = &(timing->data.other_data.data.cvt[i]);
   1401 
   1402 		if (!memcmp(cvt->code, empty, 3))
   1403 			continue;
   1404 
   1405 		height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
   1406 		switch (cvt->code[1] & 0x0c) {
   1407 		case 0x00:
   1408 			width = height * 4 / 3;
   1409 			break;
   1410 		case 0x04:
   1411 			width = height * 16 / 9;
   1412 			break;
   1413 		case 0x08:
   1414 			width = height * 16 / 10;
   1415 			break;
   1416 		case 0x0c:
   1417 			width = height * 15 / 9;
   1418 			break;
   1419 		}
   1420 
   1421 		for (j = 1; j < 5; j++) {
   1422 			if (cvt->code[2] & (1 << j)) {
   1423 				newmode = drm_cvt_mode(dev, width, height,
   1424 						       rates[j], j == 0,
   1425 						       false, false);
   1426 				if (newmode) {
   1427 					drm_mode_probed_add(connector, newmode);
   1428 					modes++;
   1429 				}
   1430 			}
   1431 		}
   1432 	}
   1433 
   1434 	return modes;
   1435 }
   1436 
   1437 static void
   1438 do_cvt_mode(struct detailed_timing *timing, void *c)
   1439 {
   1440 	struct detailed_mode_closure *closure = c;
   1441 	struct detailed_non_pixel *data = &timing->data.other_data;
   1442 
   1443 	if (data->type == EDID_DETAIL_CVT_3BYTE)
   1444 		closure->modes += drm_cvt_modes(closure->connector, timing);
   1445 }
   1446 
   1447 static int
   1448 add_cvt_modes(struct drm_connector *connector, struct edid *edid)
   1449 {
   1450 	struct detailed_mode_closure closure = {
   1451 		connector, edid, 0, 0, 0
   1452 	};
   1453 
   1454 	if (version_greater(edid, 1, 2))
   1455 		drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
   1456 
   1457 	/* XXX should also look for CVT codes in VTB blocks */
   1458 
   1459 	return closure.modes;
   1460 }
   1461 
   1462 static void
   1463 do_detailed_mode(struct detailed_timing *timing, void *c)
   1464 {
   1465 	struct detailed_mode_closure *closure = c;
   1466 	struct drm_display_mode *newmode;
   1467 
   1468 	if (timing->pixel_clock) {
   1469 		newmode = drm_mode_detailed(closure->connector->dev,
   1470 					    closure->edid, timing,
   1471 					    closure->quirks);
   1472 		if (!newmode)
   1473 			return;
   1474 
   1475 		if (closure->preferred)
   1476 			newmode->type |= DRM_MODE_TYPE_PREFERRED;
   1477 
   1478 		drm_mode_probed_add(closure->connector, newmode);
   1479 		closure->modes++;
   1480 		closure->preferred = 0;
   1481 	}
   1482 }
   1483 
   1484 /*
   1485  * add_detailed_modes - Add modes from detailed timings
   1486  * @connector: attached connector
   1487  * @edid: EDID block to scan
   1488  * @quirks: quirks to apply
   1489  */
   1490 static int
   1491 add_detailed_modes(struct drm_connector *connector, struct edid *edid,
   1492 		   u32 quirks)
   1493 {
   1494 	struct detailed_mode_closure closure = {
   1495 		connector,
   1496 		edid,
   1497 		1,
   1498 		quirks,
   1499 		0
   1500 	};
   1501 
   1502 	if (closure.preferred && !version_greater(edid, 1, 3))
   1503 		closure.preferred =
   1504 		    (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
   1505 
   1506 	drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
   1507 
   1508 	return closure.modes;
   1509 }
   1510 
   1511 #define HDMI_IDENTIFIER 0x000C03
   1512 #define AUDIO_BLOCK	0x01
   1513 #define VIDEO_BLOCK     0x02
   1514 #define VENDOR_BLOCK    0x03
   1515 #define SPEAKER_BLOCK	0x04
   1516 #define EDID_BASIC_AUDIO	(1 << 6)
   1517 #define EDID_CEA_YCRCB444	(1 << 5)
   1518 #define EDID_CEA_YCRCB422	(1 << 4)
   1519 
   1520 /**
   1521  * Search EDID for CEA extension block.
   1522  */
   1523 u8 *drm_find_cea_extension(struct edid *edid)
   1524 {
   1525 	u8 *edid_ext = NULL;
   1526 	int i;
   1527 
   1528 	/* No EDID or EDID extensions */
   1529 	if (edid == NULL || edid->extensions == 0)
   1530 		return NULL;
   1531 
   1532 	/* Find CEA extension */
   1533 	for (i = 0; i < edid->extensions; i++) {
   1534 		edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
   1535 		if (edid_ext[0] == CEA_EXT)
   1536 			break;
   1537 	}
   1538 
   1539 	if (i == edid->extensions)
   1540 		return NULL;
   1541 
   1542 	return edid_ext;
   1543 }
   1544 EXPORT_SYMBOL(drm_find_cea_extension);
   1545 
   1546 /*
   1547  * Looks for a CEA mode matching given drm_display_mode.
   1548  * Returns its CEA Video ID code, or 0 if not found.
   1549  */
   1550 u8 drm_match_cea_mode(struct drm_display_mode *to_match)
   1551 {
   1552 #ifdef __NetBSD__
   1553 	const struct drm_display_mode *cea_mode;
   1554 #else
   1555 	struct drm_display_mode *cea_mode;
   1556 #endif
   1557 	u8 mode;
   1558 
   1559 	for (mode = 0; mode < drm_num_cea_modes; mode++) {
   1560 #ifdef __NetBSD__
   1561 		cea_mode = &edid_cea_modes[mode];
   1562 #else
   1563 		cea_mode = (struct drm_display_mode *)&edid_cea_modes[mode];
   1564 #endif
   1565 
   1566 		if (drm_mode_equal(to_match, cea_mode))
   1567 			return mode + 1;
   1568 	}
   1569 	return 0;
   1570 }
   1571 EXPORT_SYMBOL(drm_match_cea_mode);
   1572 
   1573 
   1574 static int
   1575 do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
   1576 {
   1577 	struct drm_device *dev = connector->dev;
   1578 	u8 * mode, cea_mode;
   1579 	int modes = 0;
   1580 
   1581 	for (mode = db; mode < db + len; mode++) {
   1582 		cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
   1583 		if (cea_mode < drm_num_cea_modes) {
   1584 			struct drm_display_mode *newmode;
   1585 			newmode = drm_mode_duplicate(dev,
   1586 						     &edid_cea_modes[cea_mode]);
   1587 			if (newmode) {
   1588 				drm_mode_probed_add(connector, newmode);
   1589 				modes++;
   1590 			}
   1591 		}
   1592 	}
   1593 
   1594 	return modes;
   1595 }
   1596 
   1597 static int
   1598 cea_db_payload_len(const u8 *db)
   1599 {
   1600 	return db[0] & 0x1f;
   1601 }
   1602 
   1603 static int
   1604 cea_db_tag(const u8 *db)
   1605 {
   1606 	return db[0] >> 5;
   1607 }
   1608 
   1609 static int
   1610 cea_revision(const u8 *cea)
   1611 {
   1612 	return cea[1];
   1613 }
   1614 
   1615 static int
   1616 cea_db_offsets(const u8 *cea, int *start, int *end)
   1617 {
   1618 	/* Data block offset in CEA extension block */
   1619 	*start = 4;
   1620 	*end = cea[2];
   1621 	if (*end == 0)
   1622 		*end = 127;
   1623 	if (*end < 4 || *end > 127)
   1624 		return -ERANGE;
   1625 	return 0;
   1626 }
   1627 
   1628 #define for_each_cea_db(cea, i, start, end) \
   1629 	for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
   1630 
   1631 static int
   1632 add_cea_modes(struct drm_connector *connector, struct edid *edid)
   1633 {
   1634 	u8 * cea = drm_find_cea_extension(edid);
   1635 	u8 * db, dbl;
   1636 	int modes = 0;
   1637 
   1638 	if (cea && cea_revision(cea) >= 3) {
   1639 		int i, start, end;
   1640 
   1641 		if (cea_db_offsets(cea, &start, &end))
   1642 			return 0;
   1643 
   1644 		for_each_cea_db(cea, i, start, end) {
   1645 			db = &cea[i];
   1646 			dbl = cea_db_payload_len(db);
   1647 
   1648 			if (cea_db_tag(db) == VIDEO_BLOCK)
   1649 				modes += do_cea_modes (connector, db+1, dbl);
   1650 		}
   1651 	}
   1652 
   1653 	return modes;
   1654 }
   1655 
   1656 static void
   1657 parse_hdmi_vsdb(struct drm_connector *connector, const u8 *db)
   1658 {
   1659 	u8 len = cea_db_payload_len(db);
   1660 
   1661 	if (len >= 6) {
   1662 		connector->eld[5] |= (db[6] >> 7) << 1;  /* Supports_AI */
   1663 		connector->dvi_dual = db[6] & 1;
   1664 	}
   1665 	if (len >= 7)
   1666 		connector->max_tmds_clock = db[7] * 5;
   1667 	if (len >= 8) {
   1668 		connector->latency_present[0] = db[8] >> 7;
   1669 		connector->latency_present[1] = (db[8] >> 6) & 1;
   1670 	}
   1671 	if (len >= 9)
   1672 		connector->video_latency[0] = db[9];
   1673 	if (len >= 10)
   1674 		connector->audio_latency[0] = db[10];
   1675 	if (len >= 11)
   1676 		connector->video_latency[1] = db[11];
   1677 	if (len >= 12)
   1678 		connector->audio_latency[1] = db[12];
   1679 
   1680 	DRM_DEBUG_KMS("HDMI: DVI dual %d, "
   1681 		    "max TMDS clock %d, "
   1682 		    "latency present %d %d, "
   1683 		    "video latency %d %d, "
   1684 		    "audio latency %d %d\n",
   1685 		    connector->dvi_dual,
   1686 		    connector->max_tmds_clock,
   1687 	      (int) connector->latency_present[0],
   1688 	      (int) connector->latency_present[1],
   1689 		    connector->video_latency[0],
   1690 		    connector->video_latency[1],
   1691 		    connector->audio_latency[0],
   1692 		    connector->audio_latency[1]);
   1693 }
   1694 
   1695 static void
   1696 monitor_name(struct detailed_timing *t, void *data)
   1697 {
   1698 	if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
   1699 		*(u8 **)data = t->data.other_data.data.str.str;
   1700 }
   1701 
   1702 static bool cea_db_is_hdmi_vsdb(const u8 *db)
   1703 {
   1704 	int hdmi_id;
   1705 
   1706 	if (cea_db_tag(db) != VENDOR_BLOCK)
   1707 		return false;
   1708 
   1709 	if (cea_db_payload_len(db) < 5)
   1710 		return false;
   1711 
   1712 	hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
   1713 
   1714 	return hdmi_id == HDMI_IDENTIFIER;
   1715 }
   1716 
   1717 /**
   1718  * drm_edid_to_eld - build ELD from EDID
   1719  * @connector: connector corresponding to the HDMI/DP sink
   1720  * @edid: EDID to parse
   1721  *
   1722  * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
   1723  * Some ELD fields are left to the graphics driver caller:
   1724  * - Conn_Type
   1725  * - HDCP
   1726  * - Port_ID
   1727  */
   1728 void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
   1729 {
   1730 	uint8_t *eld = connector->eld;
   1731 	u8 *cea;
   1732 	u8 *name;
   1733 	u8 *db;
   1734 	int sad_count = 0;
   1735 	int mnl;
   1736 	int dbl;
   1737 
   1738 	memset(eld, 0, sizeof(connector->eld));
   1739 
   1740 	cea = drm_find_cea_extension(edid);
   1741 	if (!cea) {
   1742 		DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
   1743 		return;
   1744 	}
   1745 
   1746 	name = NULL;
   1747 	drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
   1748 	for (mnl = 0; name && mnl < 13; mnl++) {
   1749 		if (name[mnl] == 0x0a)
   1750 			break;
   1751 		eld[20 + mnl] = name[mnl];
   1752 	}
   1753 	eld[4] = (cea[1] << 5) | mnl;
   1754 	DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
   1755 
   1756 	eld[0] = 2 << 3;		/* ELD version: 2 */
   1757 
   1758 	eld[16] = edid->mfg_id[0];
   1759 	eld[17] = edid->mfg_id[1];
   1760 	eld[18] = edid->prod_code[0];
   1761 	eld[19] = edid->prod_code[1];
   1762 
   1763 	if (cea_revision(cea) >= 3) {
   1764 		int i, start, end;
   1765 
   1766 		if (cea_db_offsets(cea, &start, &end)) {
   1767 			start = 0;
   1768 			end = 0;
   1769 		}
   1770 
   1771 		for_each_cea_db(cea, i, start, end) {
   1772 			db = &cea[i];
   1773 			dbl = cea_db_payload_len(db);
   1774 
   1775 			switch (cea_db_tag(db)) {
   1776 			case AUDIO_BLOCK:
   1777 				/* Audio Data Block, contains SADs */
   1778 				sad_count = dbl / 3;
   1779 				if (dbl >= 1)
   1780 					memcpy(eld + 20 + mnl, &db[1], dbl);
   1781 				break;
   1782 			case SPEAKER_BLOCK:
   1783 				/* Speaker Allocation Data Block */
   1784 				if (dbl >= 1)
   1785 					eld[7] = db[1];
   1786 				break;
   1787 			case VENDOR_BLOCK:
   1788 				/* HDMI Vendor-Specific Data Block */
   1789 				if (cea_db_is_hdmi_vsdb(db))
   1790 					parse_hdmi_vsdb(connector, db);
   1791 				break;
   1792 			default:
   1793 				break;
   1794 			}
   1795 		}
   1796 	}
   1797 	eld[5] |= sad_count << 4;
   1798 	eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
   1799 
   1800 	DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
   1801 }
   1802 EXPORT_SYMBOL(drm_edid_to_eld);
   1803 
   1804 /**
   1805  * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
   1806  * @connector: connector associated with the HDMI/DP sink
   1807  * @mode: the display mode
   1808  */
   1809 int drm_av_sync_delay(struct drm_connector *connector,
   1810 		      struct drm_display_mode *mode)
   1811 {
   1812 	int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
   1813 	int a, v;
   1814 
   1815 	if (!connector->latency_present[0])
   1816 		return 0;
   1817 	if (!connector->latency_present[1])
   1818 		i = 0;
   1819 
   1820 	a = connector->audio_latency[i];
   1821 	v = connector->video_latency[i];
   1822 
   1823 	/*
   1824 	 * HDMI/DP sink doesn't support audio or video?
   1825 	 */
   1826 	if (a == 255 || v == 255)
   1827 		return 0;
   1828 
   1829 	/*
   1830 	 * Convert raw EDID values to millisecond.
   1831 	 * Treat unknown latency as 0ms.
   1832 	 */
   1833 	if (a)
   1834 		a = min(2 * (a - 1), 500);
   1835 	if (v)
   1836 		v = min(2 * (v - 1), 500);
   1837 
   1838 	return max(v - a, 0);
   1839 }
   1840 EXPORT_SYMBOL(drm_av_sync_delay);
   1841 
   1842 /**
   1843  * drm_select_eld - select one ELD from multiple HDMI/DP sinks
   1844  * @encoder: the encoder just changed display mode
   1845  * @mode: the adjusted display mode
   1846  *
   1847  * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
   1848  * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
   1849  */
   1850 struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
   1851 				     struct drm_display_mode *mode)
   1852 {
   1853 	struct drm_connector *connector;
   1854 	struct drm_device *dev = encoder->dev;
   1855 
   1856 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
   1857 		if (connector->encoder == encoder && connector->eld[0])
   1858 			return connector;
   1859 
   1860 	return NULL;
   1861 }
   1862 EXPORT_SYMBOL(drm_select_eld);
   1863 
   1864 /**
   1865  * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
   1866  * @edid: monitor EDID information
   1867  *
   1868  * Parse the CEA extension according to CEA-861-B.
   1869  * Return true if HDMI, false if not or unknown.
   1870  */
   1871 bool drm_detect_hdmi_monitor(struct edid *edid)
   1872 {
   1873 	u8 *edid_ext;
   1874 	int i;
   1875 	int start_offset, end_offset;
   1876 
   1877 	edid_ext = drm_find_cea_extension(edid);
   1878 	if (!edid_ext)
   1879 		return false;
   1880 
   1881 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
   1882 		return false;
   1883 
   1884 	/*
   1885 	 * Because HDMI identifier is in Vendor Specific Block,
   1886 	 * search it from all data blocks of CEA extension.
   1887 	 */
   1888 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
   1889 		if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
   1890 			return true;
   1891 	}
   1892 
   1893 	return false;
   1894 }
   1895 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
   1896 
   1897 /**
   1898  * drm_detect_monitor_audio - check monitor audio capability
   1899  *
   1900  * Monitor should have CEA extension block.
   1901  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
   1902  * audio' only. If there is any audio extension block and supported
   1903  * audio format, assume at least 'basic audio' support, even if 'basic
   1904  * audio' is not defined in EDID.
   1905  *
   1906  */
   1907 bool drm_detect_monitor_audio(struct edid *edid)
   1908 {
   1909 	u8 *edid_ext;
   1910 	int i, j;
   1911 	bool has_audio = false;
   1912 	int start_offset, end_offset;
   1913 
   1914 	edid_ext = drm_find_cea_extension(edid);
   1915 	if (!edid_ext)
   1916 		goto end;
   1917 
   1918 	has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
   1919 
   1920 	if (has_audio) {
   1921 		DRM_DEBUG_KMS("Monitor has basic audio support\n");
   1922 		goto end;
   1923 	}
   1924 
   1925 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
   1926 		goto end;
   1927 
   1928 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
   1929 		if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
   1930 			has_audio = true;
   1931 			for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
   1932 				DRM_DEBUG_KMS("CEA audio format %d\n",
   1933 					      (edid_ext[i + j] >> 3) & 0xf);
   1934 			goto end;
   1935 		}
   1936 	}
   1937 end:
   1938 	return has_audio;
   1939 }
   1940 EXPORT_SYMBOL(drm_detect_monitor_audio);
   1941 
   1942 /**
   1943  * drm_add_display_info - pull display info out if present
   1944  * @edid: EDID data
   1945  * @info: display info (attached to connector)
   1946  *
   1947  * Grab any available display info and stuff it into the drm_display_info
   1948  * structure that's part of the connector.  Useful for tracking bpp and
   1949  * color spaces.
   1950  */
   1951 static void drm_add_display_info(struct edid *edid,
   1952 				 struct drm_display_info *info)
   1953 {
   1954 	u8 *edid_ext;
   1955 
   1956 	info->width_mm = edid->width_cm * 10;
   1957 	info->height_mm = edid->height_cm * 10;
   1958 
   1959 	/* driver figures it out in this case */
   1960 	info->bpc = 0;
   1961 	info->color_formats = 0;
   1962 
   1963 	if (edid->revision < 3)
   1964 		return;
   1965 
   1966 	if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
   1967 		return;
   1968 
   1969 	/* Get data from CEA blocks if present */
   1970 	edid_ext = drm_find_cea_extension(edid);
   1971 	if (edid_ext) {
   1972 		info->cea_rev = edid_ext[1];
   1973 
   1974 		/* The existence of a CEA block should imply RGB support */
   1975 		info->color_formats = DRM_COLOR_FORMAT_RGB444;
   1976 		if (edid_ext[3] & EDID_CEA_YCRCB444)
   1977 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
   1978 		if (edid_ext[3] & EDID_CEA_YCRCB422)
   1979 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
   1980 	}
   1981 
   1982 	/* Only defined for 1.4 with digital displays */
   1983 	if (edid->revision < 4)
   1984 		return;
   1985 
   1986 	switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
   1987 	case DRM_EDID_DIGITAL_DEPTH_6:
   1988 		info->bpc = 6;
   1989 		break;
   1990 	case DRM_EDID_DIGITAL_DEPTH_8:
   1991 		info->bpc = 8;
   1992 		break;
   1993 	case DRM_EDID_DIGITAL_DEPTH_10:
   1994 		info->bpc = 10;
   1995 		break;
   1996 	case DRM_EDID_DIGITAL_DEPTH_12:
   1997 		info->bpc = 12;
   1998 		break;
   1999 	case DRM_EDID_DIGITAL_DEPTH_14:
   2000 		info->bpc = 14;
   2001 		break;
   2002 	case DRM_EDID_DIGITAL_DEPTH_16:
   2003 		info->bpc = 16;
   2004 		break;
   2005 	case DRM_EDID_DIGITAL_DEPTH_UNDEF:
   2006 	default:
   2007 		info->bpc = 0;
   2008 		break;
   2009 	}
   2010 
   2011 	info->color_formats |= DRM_COLOR_FORMAT_RGB444;
   2012 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
   2013 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
   2014 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
   2015 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
   2016 }
   2017 
   2018 /**
   2019  * drm_add_edid_modes - add modes from EDID data, if available
   2020  * @connector: connector we're probing
   2021  * @edid: edid data
   2022  *
   2023  * Add the specified modes to the connector's mode list.
   2024  *
   2025  * Return number of modes added or 0 if we couldn't find any.
   2026  */
   2027 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
   2028 {
   2029 	int num_modes = 0;
   2030 	u32 quirks;
   2031 
   2032 	if (edid == NULL) {
   2033 		return 0;
   2034 	}
   2035 	if (!drm_edid_is_valid(edid)) {
   2036 		dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
   2037 			 drm_get_connector_name(connector));
   2038 		return 0;
   2039 	}
   2040 
   2041 	quirks = edid_get_quirks(edid);
   2042 
   2043 	/*
   2044 	 * EDID spec says modes should be preferred in this order:
   2045 	 * - preferred detailed mode
   2046 	 * - other detailed modes from base block
   2047 	 * - detailed modes from extension blocks
   2048 	 * - CVT 3-byte code modes
   2049 	 * - standard timing codes
   2050 	 * - established timing codes
   2051 	 * - modes inferred from GTF or CVT range information
   2052 	 *
   2053 	 * We get this pretty much right.
   2054 	 *
   2055 	 * XXX order for additional mode types in extension blocks?
   2056 	 */
   2057 	num_modes += add_detailed_modes(connector, edid, quirks);
   2058 	num_modes += add_cvt_modes(connector, edid);
   2059 	num_modes += add_standard_modes(connector, edid);
   2060 	num_modes += add_established_modes(connector, edid);
   2061 	num_modes += add_inferred_modes(connector, edid);
   2062 	num_modes += add_cea_modes(connector, edid);
   2063 
   2064 	if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
   2065 		edid_fixup_preferred(connector, quirks);
   2066 
   2067 	drm_add_display_info(edid, &connector->display_info);
   2068 
   2069 	return num_modes;
   2070 }
   2071 EXPORT_SYMBOL(drm_add_edid_modes);
   2072 
   2073 /**
   2074  * drm_add_modes_noedid - add modes for the connectors without EDID
   2075  * @connector: connector we're probing
   2076  * @hdisplay: the horizontal display limit
   2077  * @vdisplay: the vertical display limit
   2078  *
   2079  * Add the specified modes to the connector's mode list. Only when the
   2080  * hdisplay/vdisplay is not beyond the given limit, it will be added.
   2081  *
   2082  * Return number of modes added or 0 if we couldn't find any.
   2083  */
   2084 int drm_add_modes_noedid(struct drm_connector *connector,
   2085 			int hdisplay, int vdisplay)
   2086 {
   2087 	int i, count, num_modes = 0;
   2088 	struct drm_display_mode *mode;
   2089 	struct drm_device *dev = connector->dev;
   2090 
   2091 	count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
   2092 	if (hdisplay < 0)
   2093 		hdisplay = 0;
   2094 	if (vdisplay < 0)
   2095 		vdisplay = 0;
   2096 
   2097 	for (i = 0; i < count; i++) {
   2098 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
   2099 		if (hdisplay && vdisplay) {
   2100 			/*
   2101 			 * Only when two are valid, they will be used to check
   2102 			 * whether the mode should be added to the mode list of
   2103 			 * the connector.
   2104 			 */
   2105 			if (ptr->hdisplay > hdisplay ||
   2106 					ptr->vdisplay > vdisplay)
   2107 				continue;
   2108 		}
   2109 		if (drm_mode_vrefresh(ptr) > 61)
   2110 			continue;
   2111 		mode = drm_mode_duplicate(dev, ptr);
   2112 		if (mode) {
   2113 			drm_mode_probed_add(connector, mode);
   2114 			num_modes++;
   2115 		}
   2116 	}
   2117 	return num_modes;
   2118 }
   2119 EXPORT_SYMBOL(drm_add_modes_noedid);
   2120 
   2121 /**
   2122  * drm_mode_cea_vic - return the CEA-861 VIC of a given mode
   2123  * @mode: mode
   2124  *
   2125  * RETURNS:
   2126  * The VIC number, 0 in case it's not a CEA-861 mode.
   2127  */
   2128 uint8_t drm_mode_cea_vic(const struct drm_display_mode *mode)
   2129 {
   2130 	uint8_t i;
   2131 
   2132 	for (i = 0; i < drm_num_cea_modes; i++)
   2133 		if (drm_mode_equal(mode, &edid_cea_modes[i]))
   2134 			return i + 1;
   2135 
   2136 	return 0;
   2137 }
   2138 EXPORT_SYMBOL(drm_mode_cea_vic);
   2139