udl_connector.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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->ddev->usbdev,
  32. usb_rcvctrlpipe(udl->ddev->usbdev, 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. i--;
  38. goto error;
  39. }
  40. block[i] = rbuf[1];
  41. }
  42. kfree(rbuf);
  43. return block;
  44. error:
  45. kfree(block);
  46. kfree(rbuf);
  47. return NULL;
  48. }
  49. static int udl_get_modes(struct drm_connector *connector)
  50. {
  51. struct udl_device *udl = connector->dev->dev_private;
  52. struct edid *edid;
  53. int ret;
  54. edid = (struct edid *)udl_get_edid(udl);
  55. /*
  56. * We only read the main block, but if the monitor reports extension
  57. * blocks then the drm edid code expects them to be present, so patch
  58. * the extension count to 0.
  59. */
  60. edid->checksum += edid->extensions;
  61. edid->extensions = 0;
  62. drm_mode_connector_update_edid_property(connector, edid);
  63. ret = drm_add_edid_modes(connector, edid);
  64. kfree(edid);
  65. return ret;
  66. }
  67. static int udl_mode_valid(struct drm_connector *connector,
  68. struct drm_display_mode *mode)
  69. {
  70. struct udl_device *udl = connector->dev->dev_private;
  71. if (!udl->sku_pixel_limit)
  72. return 0;
  73. if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
  74. return MODE_VIRTUAL_Y;
  75. return 0;
  76. }
  77. static enum drm_connector_status
  78. udl_detect(struct drm_connector *connector, bool force)
  79. {
  80. if (drm_device_is_unplugged(connector->dev))
  81. return connector_status_disconnected;
  82. return connector_status_connected;
  83. }
  84. static struct drm_encoder*
  85. udl_best_single_encoder(struct drm_connector *connector)
  86. {
  87. int enc_id = connector->encoder_ids[0];
  88. struct drm_mode_object *obj;
  89. struct drm_encoder *encoder;
  90. obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
  91. if (!obj)
  92. return NULL;
  93. encoder = obj_to_encoder(obj);
  94. return encoder;
  95. }
  96. static int udl_connector_set_property(struct drm_connector *connector,
  97. struct drm_property *property,
  98. uint64_t val)
  99. {
  100. return 0;
  101. }
  102. static void udl_connector_destroy(struct drm_connector *connector)
  103. {
  104. drm_sysfs_connector_remove(connector);
  105. drm_connector_cleanup(connector);
  106. kfree(connector);
  107. }
  108. static struct drm_connector_helper_funcs udl_connector_helper_funcs = {
  109. .get_modes = udl_get_modes,
  110. .mode_valid = udl_mode_valid,
  111. .best_encoder = udl_best_single_encoder,
  112. };
  113. static struct drm_connector_funcs udl_connector_funcs = {
  114. .dpms = drm_helper_connector_dpms,
  115. .detect = udl_detect,
  116. .fill_modes = drm_helper_probe_single_connector_modes,
  117. .destroy = udl_connector_destroy,
  118. .set_property = udl_connector_set_property,
  119. };
  120. int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
  121. {
  122. struct drm_connector *connector;
  123. connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
  124. if (!connector)
  125. return -ENOMEM;
  126. drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
  127. drm_connector_helper_add(connector, &udl_connector_helper_funcs);
  128. drm_sysfs_connector_add(connector);
  129. drm_mode_connector_attach_encoder(connector, encoder);
  130. drm_object_attach_property(&connector->base,
  131. dev->mode_config.dirty_info_property,
  132. 1);
  133. return 0;
  134. }