drm_edid_load.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /* Use only for backward compatibility with drm_kms_helper.edid_firmware */
  28. int __drm_set_edid_firmware_path(const char *path)
  29. {
  30. scnprintf(edid_firmware, sizeof(edid_firmware), "%s", path);
  31. return 0;
  32. }
  33. EXPORT_SYMBOL(__drm_set_edid_firmware_path);
  34. /* Use only for backward compatibility with drm_kms_helper.edid_firmware */
  35. int __drm_get_edid_firmware_path(char *buf, size_t bufsize)
  36. {
  37. return scnprintf(buf, bufsize, "%s", edid_firmware);
  38. }
  39. EXPORT_SYMBOL(__drm_get_edid_firmware_path);
  40. #define GENERIC_EDIDS 6
  41. static const char * const generic_edid_name[GENERIC_EDIDS] = {
  42. "edid/800x600.bin",
  43. "edid/1024x768.bin",
  44. "edid/1280x1024.bin",
  45. "edid/1600x1200.bin",
  46. "edid/1680x1050.bin",
  47. "edid/1920x1080.bin",
  48. };
  49. static const u8 generic_edid[GENERIC_EDIDS][128] = {
  50. {
  51. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  52. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x1b, 0x14, 0x78,
  54. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  55. 0x20, 0x50, 0x54, 0x01, 0x00, 0x00, 0x45, 0x40,
  56. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  57. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xa0, 0x0f,
  58. 0x20, 0x00, 0x31, 0x58, 0x1c, 0x20, 0x28, 0x80,
  59. 0x14, 0x00, 0x15, 0xd0, 0x10, 0x00, 0x00, 0x1e,
  60. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  61. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  62. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  63. 0x3d, 0x24, 0x26, 0x05, 0x00, 0x0a, 0x20, 0x20,
  64. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  65. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
  66. 0x56, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xc2,
  67. },
  68. {
  69. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  70. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x23, 0x1a, 0x78,
  72. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  73. 0x20, 0x50, 0x54, 0x00, 0x08, 0x00, 0x61, 0x40,
  74. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  75. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x64, 0x19,
  76. 0x00, 0x40, 0x41, 0x00, 0x26, 0x30, 0x08, 0x90,
  77. 0x36, 0x00, 0x63, 0x0a, 0x11, 0x00, 0x00, 0x18,
  78. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  79. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  80. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  81. 0x3d, 0x2f, 0x31, 0x07, 0x00, 0x0a, 0x20, 0x20,
  82. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  83. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x58,
  84. 0x47, 0x41, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x55,
  85. },
  86. {
  87. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  88. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2c, 0x23, 0x78,
  90. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  91. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0x81, 0x80,
  92. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  93. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x30, 0x2a,
  94. 0x00, 0x98, 0x51, 0x00, 0x2a, 0x40, 0x30, 0x70,
  95. 0x13, 0x00, 0xbc, 0x63, 0x11, 0x00, 0x00, 0x1e,
  96. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  97. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  98. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  99. 0x3d, 0x3e, 0x40, 0x0b, 0x00, 0x0a, 0x20, 0x20,
  100. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  101. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
  102. 0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xa0,
  103. },
  104. {
  105. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  106. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x37, 0x29, 0x78,
  108. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  109. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xa9, 0x40,
  110. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  111. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x3f,
  112. 0x40, 0x30, 0x62, 0xb0, 0x32, 0x40, 0x40, 0xc0,
  113. 0x13, 0x00, 0x2b, 0xa0, 0x21, 0x00, 0x00, 0x1e,
  114. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  115. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  116. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  117. 0x3d, 0x4a, 0x4c, 0x11, 0x00, 0x0a, 0x20, 0x20,
  118. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  119. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x55,
  120. 0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0x9d,
  121. },
  122. {
  123. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  124. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2b, 0x1b, 0x78,
  126. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  127. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xb3, 0x00,
  128. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  129. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x21, 0x39,
  130. 0x90, 0x30, 0x62, 0x1a, 0x27, 0x40, 0x68, 0xb0,
  131. 0x36, 0x00, 0xb5, 0x11, 0x11, 0x00, 0x00, 0x1e,
  132. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  133. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  134. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  135. 0x3d, 0x40, 0x42, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  136. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  137. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x57,
  138. 0x53, 0x58, 0x47, 0x41, 0x0a, 0x20, 0x00, 0x26,
  139. },
  140. {
  141. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  142. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x32, 0x1c, 0x78,
  144. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  145. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xd1, 0xc0,
  146. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  147. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
  148. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  149. 0x45, 0x00, 0xf4, 0x19, 0x11, 0x00, 0x00, 0x1e,
  150. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  151. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  152. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  153. 0x3d, 0x42, 0x44, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  154. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  155. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x46,
  156. 0x48, 0x44, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x05,
  157. },
  158. };
  159. static int edid_size(const u8 *edid, int data_size)
  160. {
  161. if (data_size < EDID_LENGTH)
  162. return 0;
  163. return (edid[0x7e] + 1) * EDID_LENGTH;
  164. }
  165. static void *edid_load(struct drm_connector *connector, const char *name,
  166. const char *connector_name)
  167. {
  168. const struct firmware *fw = NULL;
  169. const u8 *fwdata;
  170. u8 *edid;
  171. int fwsize, builtin;
  172. int i, valid_extensions = 0;
  173. bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
  174. builtin = match_string(generic_edid_name, GENERIC_EDIDS, name);
  175. if (builtin >= 0) {
  176. fwdata = generic_edid[builtin];
  177. fwsize = sizeof(generic_edid[builtin]);
  178. } else {
  179. struct platform_device *pdev;
  180. int err;
  181. pdev = platform_device_register_simple(connector_name, -1, NULL, 0);
  182. if (IS_ERR(pdev)) {
  183. DRM_ERROR("Failed to register EDID firmware platform device "
  184. "for connector \"%s\"\n", connector_name);
  185. return ERR_CAST(pdev);
  186. }
  187. err = request_firmware(&fw, name, &pdev->dev);
  188. platform_device_unregister(pdev);
  189. if (err) {
  190. DRM_ERROR("Requesting EDID firmware \"%s\" failed (err=%d)\n",
  191. name, err);
  192. return ERR_PTR(err);
  193. }
  194. fwdata = fw->data;
  195. fwsize = fw->size;
  196. }
  197. if (edid_size(fwdata, fwsize) != fwsize) {
  198. DRM_ERROR("Size of EDID firmware \"%s\" is invalid "
  199. "(expected %d, got %d\n", name,
  200. edid_size(fwdata, fwsize), (int)fwsize);
  201. edid = ERR_PTR(-EINVAL);
  202. goto out;
  203. }
  204. edid = kmemdup(fwdata, fwsize, GFP_KERNEL);
  205. if (edid == NULL) {
  206. edid = ERR_PTR(-ENOMEM);
  207. goto out;
  208. }
  209. if (!drm_edid_block_valid(edid, 0, print_bad_edid,
  210. &connector->edid_corrupt)) {
  211. connector->bad_edid_counter++;
  212. DRM_ERROR("Base block of EDID firmware \"%s\" is invalid ",
  213. name);
  214. kfree(edid);
  215. edid = ERR_PTR(-EINVAL);
  216. goto out;
  217. }
  218. for (i = 1; i <= edid[0x7e]; i++) {
  219. if (i != valid_extensions + 1)
  220. memcpy(edid + (valid_extensions + 1) * EDID_LENGTH,
  221. edid + i * EDID_LENGTH, EDID_LENGTH);
  222. if (drm_edid_block_valid(edid + i * EDID_LENGTH, i,
  223. print_bad_edid,
  224. NULL))
  225. valid_extensions++;
  226. }
  227. if (valid_extensions != edid[0x7e]) {
  228. u8 *new_edid;
  229. edid[EDID_LENGTH-1] += edid[0x7e] - valid_extensions;
  230. DRM_INFO("Found %d valid extensions instead of %d in EDID data "
  231. "\"%s\" for connector \"%s\"\n", valid_extensions,
  232. edid[0x7e], name, connector_name);
  233. edid[0x7e] = valid_extensions;
  234. new_edid = krealloc(edid, (valid_extensions + 1) * EDID_LENGTH,
  235. GFP_KERNEL);
  236. if (new_edid)
  237. edid = new_edid;
  238. }
  239. DRM_INFO("Got %s EDID base block and %d extension%s from "
  240. "\"%s\" for connector \"%s\"\n", (builtin >= 0) ? "built-in" :
  241. "external", valid_extensions, valid_extensions == 1 ? "" : "s",
  242. name, connector_name);
  243. out:
  244. release_firmware(fw);
  245. return edid;
  246. }
  247. struct edid *drm_load_edid_firmware(struct drm_connector *connector)
  248. {
  249. const char *connector_name = connector->name;
  250. char *edidname, *last, *colon, *fwstr, *edidstr, *fallback = NULL;
  251. struct edid *edid;
  252. if (edid_firmware[0] == '\0')
  253. return ERR_PTR(-ENOENT);
  254. /*
  255. * If there are multiple edid files specified and separated
  256. * by commas, search through the list looking for one that
  257. * matches the connector.
  258. *
  259. * If there's one or more that doesn't specify a connector, keep
  260. * the last one found one as a fallback.
  261. */
  262. fwstr = kstrdup(edid_firmware, GFP_KERNEL);
  263. if (!fwstr)
  264. return ERR_PTR(-ENOMEM);
  265. edidstr = fwstr;
  266. while ((edidname = strsep(&edidstr, ","))) {
  267. colon = strchr(edidname, ':');
  268. if (colon != NULL) {
  269. if (strncmp(connector_name, edidname, colon - edidname))
  270. continue;
  271. edidname = colon + 1;
  272. break;
  273. }
  274. if (*edidname != '\0') /* corner case: multiple ',' */
  275. fallback = edidname;
  276. }
  277. if (!edidname) {
  278. if (!fallback) {
  279. kfree(fwstr);
  280. return ERR_PTR(-ENOENT);
  281. }
  282. edidname = fallback;
  283. }
  284. last = edidname + strlen(edidname) - 1;
  285. if (*last == '\n')
  286. *last = '\0';
  287. edid = edid_load(connector, edidname, connector_name);
  288. kfree(fwstr);
  289. return edid;
  290. }