drm_edid_load.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. drm_edid_load.c: use a built-in EDID data set or load it via the firmware
  3. interface
  4. Copyright (C) 2012 Carsten Emde <C.Emde@osadl.org>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <drm/drmP.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_edid.h>
  23. static char edid_firmware[PATH_MAX];
  24. module_param_string(edid_firmware, edid_firmware, sizeof(edid_firmware), 0644);
  25. MODULE_PARM_DESC(edid_firmware, "Do not probe monitor, use specified EDID blob "
  26. "from built-in data or /lib/firmware instead. ");
  27. #define GENERIC_EDIDS 6
  28. static const char * const generic_edid_name[GENERIC_EDIDS] = {
  29. "edid/800x600.bin",
  30. "edid/1024x768.bin",
  31. "edid/1280x1024.bin",
  32. "edid/1600x1200.bin",
  33. "edid/1680x1050.bin",
  34. "edid/1920x1080.bin",
  35. };
  36. static const u8 generic_edid[GENERIC_EDIDS][128] = {
  37. {
  38. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  39. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x1b, 0x14, 0x78,
  41. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  42. 0x20, 0x50, 0x54, 0x01, 0x00, 0x00, 0x45, 0x40,
  43. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  44. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xa0, 0x0f,
  45. 0x20, 0x00, 0x31, 0x58, 0x1c, 0x20, 0x28, 0x80,
  46. 0x14, 0x00, 0x15, 0xd0, 0x10, 0x00, 0x00, 0x1e,
  47. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  48. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  49. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  50. 0x3d, 0x24, 0x26, 0x05, 0x00, 0x0a, 0x20, 0x20,
  51. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  52. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
  53. 0x56, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xc2,
  54. },
  55. {
  56. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  57. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x23, 0x1a, 0x78,
  59. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  60. 0x20, 0x50, 0x54, 0x00, 0x08, 0x00, 0x61, 0x40,
  61. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  62. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x64, 0x19,
  63. 0x00, 0x40, 0x41, 0x00, 0x26, 0x30, 0x08, 0x90,
  64. 0x36, 0x00, 0x63, 0x0a, 0x11, 0x00, 0x00, 0x18,
  65. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  66. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  67. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  68. 0x3d, 0x2f, 0x31, 0x07, 0x00, 0x0a, 0x20, 0x20,
  69. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  70. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x58,
  71. 0x47, 0x41, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x55,
  72. },
  73. {
  74. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  75. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2c, 0x23, 0x78,
  77. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  78. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0x81, 0x80,
  79. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  80. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x30, 0x2a,
  81. 0x00, 0x98, 0x51, 0x00, 0x2a, 0x40, 0x30, 0x70,
  82. 0x13, 0x00, 0xbc, 0x63, 0x11, 0x00, 0x00, 0x1e,
  83. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  84. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  85. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  86. 0x3d, 0x3e, 0x40, 0x0b, 0x00, 0x0a, 0x20, 0x20,
  87. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  88. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
  89. 0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xa0,
  90. },
  91. {
  92. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  93. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x37, 0x29, 0x78,
  95. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  96. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xa9, 0x40,
  97. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  98. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x3f,
  99. 0x40, 0x30, 0x62, 0xb0, 0x32, 0x40, 0x40, 0xc0,
  100. 0x13, 0x00, 0x2b, 0xa0, 0x21, 0x00, 0x00, 0x1e,
  101. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  102. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  103. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  104. 0x3d, 0x4a, 0x4c, 0x11, 0x00, 0x0a, 0x20, 0x20,
  105. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  106. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x55,
  107. 0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0x9d,
  108. },
  109. {
  110. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  111. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2b, 0x1b, 0x78,
  113. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  114. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xb3, 0x00,
  115. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  116. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x21, 0x39,
  117. 0x90, 0x30, 0x62, 0x1a, 0x27, 0x40, 0x68, 0xb0,
  118. 0x36, 0x00, 0xb5, 0x11, 0x11, 0x00, 0x00, 0x1e,
  119. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  120. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  121. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  122. 0x3d, 0x40, 0x42, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  123. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  124. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x57,
  125. 0x53, 0x58, 0x47, 0x41, 0x0a, 0x20, 0x00, 0x26,
  126. },
  127. {
  128. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  129. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x32, 0x1c, 0x78,
  131. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  132. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xd1, 0xc0,
  133. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  134. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
  135. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  136. 0x45, 0x00, 0xf4, 0x19, 0x11, 0x00, 0x00, 0x1e,
  137. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  138. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  139. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  140. 0x3d, 0x42, 0x44, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  141. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  142. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x46,
  143. 0x48, 0x44, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x05,
  144. },
  145. };
  146. static int edid_size(const u8 *edid, int data_size)
  147. {
  148. if (data_size < EDID_LENGTH)
  149. return 0;
  150. return (edid[0x7e] + 1) * EDID_LENGTH;
  151. }
  152. static void *edid_load(struct drm_connector *connector, const char *name,
  153. const char *connector_name)
  154. {
  155. const struct firmware *fw = NULL;
  156. const u8 *fwdata;
  157. u8 *edid;
  158. int fwsize, builtin;
  159. int i, valid_extensions = 0;
  160. bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
  161. builtin = 0;
  162. for (i = 0; i < GENERIC_EDIDS; i++) {
  163. if (strcmp(name, generic_edid_name[i]) == 0) {
  164. fwdata = generic_edid[i];
  165. fwsize = sizeof(generic_edid[i]);
  166. builtin = 1;
  167. break;
  168. }
  169. }
  170. if (!builtin) {
  171. struct platform_device *pdev;
  172. int err;
  173. pdev = platform_device_register_simple(connector_name, -1, NULL, 0);
  174. if (IS_ERR(pdev)) {
  175. DRM_ERROR("Failed to register EDID firmware platform device "
  176. "for connector \"%s\"\n", connector_name);
  177. return ERR_CAST(pdev);
  178. }
  179. err = request_firmware(&fw, name, &pdev->dev);
  180. platform_device_unregister(pdev);
  181. if (err) {
  182. DRM_ERROR("Requesting EDID firmware \"%s\" failed (err=%d)\n",
  183. name, err);
  184. return ERR_PTR(err);
  185. }
  186. fwdata = fw->data;
  187. fwsize = fw->size;
  188. }
  189. if (edid_size(fwdata, fwsize) != fwsize) {
  190. DRM_ERROR("Size of EDID firmware \"%s\" is invalid "
  191. "(expected %d, got %d\n", name,
  192. edid_size(fwdata, fwsize), (int)fwsize);
  193. edid = ERR_PTR(-EINVAL);
  194. goto out;
  195. }
  196. edid = kmemdup(fwdata, fwsize, GFP_KERNEL);
  197. if (edid == NULL) {
  198. edid = ERR_PTR(-ENOMEM);
  199. goto out;
  200. }
  201. if (!drm_edid_block_valid(edid, 0, print_bad_edid,
  202. &connector->edid_corrupt)) {
  203. connector->bad_edid_counter++;
  204. DRM_ERROR("Base block of EDID firmware \"%s\" is invalid ",
  205. name);
  206. kfree(edid);
  207. edid = ERR_PTR(-EINVAL);
  208. goto out;
  209. }
  210. for (i = 1; i <= edid[0x7e]; i++) {
  211. if (i != valid_extensions + 1)
  212. memcpy(edid + (valid_extensions + 1) * EDID_LENGTH,
  213. edid + i * EDID_LENGTH, EDID_LENGTH);
  214. if (drm_edid_block_valid(edid + i * EDID_LENGTH, i,
  215. print_bad_edid,
  216. NULL))
  217. valid_extensions++;
  218. }
  219. if (valid_extensions != edid[0x7e]) {
  220. u8 *new_edid;
  221. edid[EDID_LENGTH-1] += edid[0x7e] - valid_extensions;
  222. DRM_INFO("Found %d valid extensions instead of %d in EDID data "
  223. "\"%s\" for connector \"%s\"\n", valid_extensions,
  224. edid[0x7e], name, connector_name);
  225. edid[0x7e] = valid_extensions;
  226. new_edid = krealloc(edid, (valid_extensions + 1) * EDID_LENGTH,
  227. GFP_KERNEL);
  228. if (new_edid)
  229. edid = new_edid;
  230. }
  231. DRM_INFO("Got %s EDID base block and %d extension%s from "
  232. "\"%s\" for connector \"%s\"\n", builtin ? "built-in" :
  233. "external", valid_extensions, valid_extensions == 1 ? "" : "s",
  234. name, connector_name);
  235. out:
  236. release_firmware(fw);
  237. return edid;
  238. }
  239. int drm_load_edid_firmware(struct drm_connector *connector)
  240. {
  241. const char *connector_name = connector->name;
  242. char *edidname, *last, *colon, *fwstr, *edidstr, *fallback = NULL;
  243. int ret;
  244. struct edid *edid;
  245. if (edid_firmware[0] == '\0')
  246. return 0;
  247. /*
  248. * If there are multiple edid files specified and separated
  249. * by commas, search through the list looking for one that
  250. * matches the connector.
  251. *
  252. * If there's one or more that don't't specify a connector, keep
  253. * the last one found one as a fallback.
  254. */
  255. fwstr = kstrdup(edid_firmware, GFP_KERNEL);
  256. edidstr = fwstr;
  257. while ((edidname = strsep(&edidstr, ","))) {
  258. colon = strchr(edidname, ':');
  259. if (colon != NULL) {
  260. if (strncmp(connector_name, edidname, colon - edidname))
  261. continue;
  262. edidname = colon + 1;
  263. break;
  264. }
  265. if (*edidname != '\0') /* corner case: multiple ',' */
  266. fallback = edidname;
  267. }
  268. if (!edidname) {
  269. if (!fallback) {
  270. kfree(fwstr);
  271. return 0;
  272. }
  273. edidname = fallback;
  274. }
  275. last = edidname + strlen(edidname) - 1;
  276. if (*last == '\n')
  277. *last = '\0';
  278. edid = edid_load(connector, edidname, connector_name);
  279. kfree(fwstr);
  280. if (IS_ERR_OR_NULL(edid))
  281. return 0;
  282. drm_mode_connector_update_edid_property(connector, edid);
  283. ret = drm_add_edid_modes(connector, edid);
  284. drm_edid_to_eld(connector, edid);
  285. kfree(edid);
  286. return ret;
  287. }