PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-3.5.0/drivers/gpu/drm/drm_edid_load.c

https://bitbucket.org/jpaglier/cse536-kernel-dev
C | 255 lines | 209 code | 26 blank | 20 comment | 33 complexity | 81c7459ac24d21755f871820f92faeb2 MD5 | raw file
  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 "drmP.h"
  20. #include "drm_crtc.h"
  21. #include "drm_crtc_helper.h"
  22. #include "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 4
  28. static char *generic_edid_name[GENERIC_EDIDS] = {
  29. "edid/1024x768.bin",
  30. "edid/1280x1024.bin",
  31. "edid/1680x1050.bin",
  32. "edid/1920x1080.bin",
  33. };
  34. static u8 generic_edid[GENERIC_EDIDS][128] = {
  35. {
  36. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  37. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x23, 0x1a, 0x78,
  39. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  40. 0x20, 0x50, 0x54, 0x00, 0x08, 0x00, 0x61, 0x40,
  41. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  42. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x64, 0x19,
  43. 0x00, 0x40, 0x41, 0x00, 0x26, 0x30, 0x08, 0x90,
  44. 0x36, 0x00, 0x63, 0x0a, 0x11, 0x00, 0x00, 0x18,
  45. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  46. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  47. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  48. 0x3d, 0x2f, 0x31, 0x07, 0x00, 0x0a, 0x20, 0x20,
  49. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  50. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x58,
  51. 0x47, 0x41, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x55,
  52. },
  53. {
  54. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  55. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2c, 0x23, 0x78,
  57. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  58. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0x81, 0x80,
  59. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  60. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x30, 0x2a,
  61. 0x00, 0x98, 0x51, 0x00, 0x2a, 0x40, 0x30, 0x70,
  62. 0x13, 0x00, 0xbc, 0x63, 0x11, 0x00, 0x00, 0x1e,
  63. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  64. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  65. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  66. 0x3d, 0x3e, 0x40, 0x0b, 0x00, 0x0a, 0x20, 0x20,
  67. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  68. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
  69. 0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xa0,
  70. },
  71. {
  72. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  73. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x2b, 0x1b, 0x78,
  75. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  76. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xb3, 0x00,
  77. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  78. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x21, 0x39,
  79. 0x90, 0x30, 0x62, 0x1a, 0x27, 0x40, 0x68, 0xb0,
  80. 0x36, 0x00, 0xb5, 0x11, 0x11, 0x00, 0x00, 0x1e,
  81. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  82. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  83. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  84. 0x3d, 0x40, 0x42, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  85. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  86. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x57,
  87. 0x53, 0x58, 0x47, 0x41, 0x0a, 0x20, 0x00, 0x26,
  88. },
  89. {
  90. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  91. 0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x05, 0x16, 0x01, 0x03, 0x6d, 0x32, 0x1c, 0x78,
  93. 0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
  94. 0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xd1, 0xc0,
  95. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  96. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
  97. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  98. 0x45, 0x00, 0xf4, 0x19, 0x11, 0x00, 0x00, 0x1e,
  99. 0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
  100. 0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
  101. 0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
  102. 0x3d, 0x42, 0x44, 0x0f, 0x00, 0x0a, 0x20, 0x20,
  103. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  104. 0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x46,
  105. 0x48, 0x44, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x05,
  106. },
  107. };
  108. static u8 *edid_load(struct drm_connector *connector, char *name,
  109. char *connector_name)
  110. {
  111. const struct firmware *fw;
  112. struct platform_device *pdev;
  113. u8 *fwdata = NULL, *edid, *new_edid;
  114. int fwsize, expected;
  115. int builtin = 0, err = 0;
  116. int i, valid_extensions = 0;
  117. pdev = platform_device_register_simple(connector_name, -1, NULL, 0);
  118. if (IS_ERR(pdev)) {
  119. DRM_ERROR("Failed to register EDID firmware platform device "
  120. "for connector \"%s\"\n", connector_name);
  121. err = -EINVAL;
  122. goto out;
  123. }
  124. err = request_firmware(&fw, name, &pdev->dev);
  125. platform_device_unregister(pdev);
  126. if (err) {
  127. i = 0;
  128. while (i < GENERIC_EDIDS && strcmp(name, generic_edid_name[i]))
  129. i++;
  130. if (i < GENERIC_EDIDS) {
  131. err = 0;
  132. builtin = 1;
  133. fwdata = generic_edid[i];
  134. fwsize = sizeof(generic_edid[i]);
  135. }
  136. }
  137. if (err) {
  138. DRM_ERROR("Requesting EDID firmware \"%s\" failed (err=%d)\n",
  139. name, err);
  140. goto out;
  141. }
  142. if (fwdata == NULL) {
  143. fwdata = (u8 *) fw->data;
  144. fwsize = fw->size;
  145. }
  146. expected = (fwdata[0x7e] + 1) * EDID_LENGTH;
  147. if (expected != fwsize) {
  148. DRM_ERROR("Size of EDID firmware \"%s\" is invalid "
  149. "(expected %d, got %d)\n", name, expected, (int) fwsize);
  150. err = -EINVAL;
  151. goto relfw_out;
  152. }
  153. edid = kmalloc(fwsize, GFP_KERNEL);
  154. if (edid == NULL) {
  155. err = -ENOMEM;
  156. goto relfw_out;
  157. }
  158. memcpy(edid, fwdata, fwsize);
  159. if (!drm_edid_block_valid(edid, 0)) {
  160. DRM_ERROR("Base block of EDID firmware \"%s\" is invalid ",
  161. name);
  162. kfree(edid);
  163. err = -EINVAL;
  164. goto relfw_out;
  165. }
  166. for (i = 1; i <= edid[0x7e]; i++) {
  167. if (i != valid_extensions + 1)
  168. memcpy(edid + (valid_extensions + 1) * EDID_LENGTH,
  169. edid + i * EDID_LENGTH, EDID_LENGTH);
  170. if (drm_edid_block_valid(edid + i * EDID_LENGTH, i))
  171. valid_extensions++;
  172. }
  173. if (valid_extensions != edid[0x7e]) {
  174. edid[EDID_LENGTH-1] += edid[0x7e] - valid_extensions;
  175. DRM_INFO("Found %d valid extensions instead of %d in EDID data "
  176. "\"%s\" for connector \"%s\"\n", valid_extensions,
  177. edid[0x7e], name, connector_name);
  178. edid[0x7e] = valid_extensions;
  179. new_edid = krealloc(edid, (valid_extensions + 1) * EDID_LENGTH,
  180. GFP_KERNEL);
  181. if (new_edid == NULL) {
  182. err = -ENOMEM;
  183. kfree(edid);
  184. goto relfw_out;
  185. }
  186. edid = new_edid;
  187. }
  188. DRM_INFO("Got %s EDID base block and %d extension%s from "
  189. "\"%s\" for connector \"%s\"\n", builtin ? "built-in" :
  190. "external", valid_extensions, valid_extensions == 1 ? "" : "s",
  191. name, connector_name);
  192. relfw_out:
  193. release_firmware(fw);
  194. out:
  195. if (err)
  196. return ERR_PTR(err);
  197. return edid;
  198. }
  199. int drm_load_edid_firmware(struct drm_connector *connector)
  200. {
  201. char *connector_name = drm_get_connector_name(connector);
  202. char *edidname = edid_firmware, *last, *colon;
  203. int ret;
  204. struct edid *edid;
  205. if (*edidname == '\0')
  206. return 0;
  207. colon = strchr(edidname, ':');
  208. if (colon != NULL) {
  209. if (strncmp(connector_name, edidname, colon - edidname))
  210. return 0;
  211. edidname = colon + 1;
  212. if (*edidname == '\0')
  213. return 0;
  214. }
  215. last = edidname + strlen(edidname) - 1;
  216. if (*last == '\n')
  217. *last = '\0';
  218. edid = (struct edid *) edid_load(connector, edidname, connector_name);
  219. if (IS_ERR_OR_NULL(edid))
  220. return 0;
  221. drm_mode_connector_update_edid_property(connector, edid);
  222. ret = drm_add_edid_modes(connector, edid);
  223. kfree(edid);
  224. return ret;
  225. }