udl_connector.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2012 Red Hat
  3. * based in parts on udlfb.c:
  4. * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  5. * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  6. * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License v2. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_crtc.h>
  14. #include <drm/drm_edid.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "udl_drv.h"
  17. /* dummy connector to just get EDID,
  18. all UDL appear to have a DVI-D */
  19. static u8 *udl_get_edid(struct udl_device *udl)
  20. {
  21. u8 *block;
  22. char *rbuf;
  23. int ret, i;
  24. block = kmalloc(EDID_LENGTH, GFP_KERNEL);
  25. if (block == NULL)
  26. return NULL;
  27. rbuf = kmalloc(2, GFP_KERNEL);
  28. if (rbuf == NULL)
  29. goto error;
  30. for (i = 0; i < EDID_LENGTH; i++) {
  31. ret = usb_control_msg(udl->udev,
  32. usb_rcvctrlpipe(udl->udev, 0), (0x02),
  33. (0x80 | (0x02 << 5)), i << 8, 0xA1, rbuf, 2,
  34. HZ);
  35. if (ret < 1) {
  36. DRM_ERROR("Read EDID byte %d failed err %x\n", i, ret);
  37. goto error;
  38. }
  39. block[i] = rbuf[1];
  40. }
  41. kfree(rbuf);
  42. return block;
  43. error:
  44. kfree(block);
  45. kfree(rbuf);
  46. return NULL;
  47. }
  48. static int udl_get_modes(struct drm_connector *connector)
  49. {
  50. struct udl_device *udl = connector->dev->dev_private;
  51. struct edid *edid;
  52. int ret;
  53. edid = (struct edid *)udl_get_edid(udl);
  54. if (!edid) {
  55. drm_mode_connector_update_edid_property(connector, NULL);
  56. return 0;
  57. }
  58. /*
  59. * We only read the main block, but if the monitor reports extension
  60. * blocks then the drm edid code expects them to be present, so patch
  61. * the extension count to 0.
  62. */
  63. edid->checksum += edid->extensions;
  64. edid->extensions = 0;
  65. drm_mode_connector_update_edid_property(connector, edid);
  66. ret = drm_add_edid_modes(connector, edid);
  67. kfree(edid);
  68. return ret;
  69. }
  70. static int udl_mode_valid(struct drm_connector *connector,
  71. struct drm_display_mode *mode)
  72. {
  73. struct udl_device *udl = connector->dev->dev_private;
  74. if (!udl->sku_pixel_limit)
  75. return 0;
  76. if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
  77. return MODE_VIRTUAL_Y;
  78. return 0;
  79. }
  80. static enum drm_connector_status
  81. udl_detect(struct drm_connector *connector, bool force)
  82. {
  83. if (drm_device_is_unplugged(connector->dev))
  84. return connector_status_disconnected;
  85. return connector_status_connected;
  86. }
  87. static struct drm_encoder*
  88. udl_best_single_encoder(struct drm_connector *connector)
  89. {
  90. int enc_id = connector->encoder_ids[0];
  91. return drm_encoder_find(connector->dev, enc_id);
  92. }
  93. static int udl_connector_set_property(struct drm_connector *connector,
  94. struct drm_property *property,
  95. uint64_t val)
  96. {
  97. return 0;
  98. }
  99. static void udl_connector_destroy(struct drm_connector *connector)
  100. {
  101. drm_connector_unregister(connector);
  102. drm_connector_cleanup(connector);
  103. kfree(connector);
  104. }
  105. static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
  106. .get_modes = udl_get_modes,
  107. .mode_valid = udl_mode_valid,
  108. .best_encoder = udl_best_single_encoder,
  109. };
  110. static const struct drm_connector_funcs udl_connector_funcs = {
  111. .dpms = drm_helper_connector_dpms,
  112. .detect = udl_detect,
  113. .fill_modes = drm_helper_probe_single_connector_modes,
  114. .destroy = udl_connector_destroy,
  115. .set_property = udl_connector_set_property,
  116. };
  117. int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
  118. {
  119. struct drm_connector *connector;
  120. connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
  121. if (!connector)
  122. return -ENOMEM;
  123. drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
  124. drm_connector_helper_add(connector, &udl_connector_helper_funcs);
  125. drm_connector_register(connector);
  126. drm_mode_connector_attach_encoder(connector, encoder);
  127. return 0;
  128. }