udl_connector.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. 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. struct drm_mode_object *obj;
  92. struct drm_encoder *encoder;
  93. obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
  94. if (!obj)
  95. return NULL;
  96. encoder = obj_to_encoder(obj);
  97. return encoder;
  98. }
  99. static int udl_connector_set_property(struct drm_connector *connector,
  100. struct drm_property *property,
  101. uint64_t val)
  102. {
  103. return 0;
  104. }
  105. static void udl_connector_destroy(struct drm_connector *connector)
  106. {
  107. drm_sysfs_connector_remove(connector);
  108. drm_connector_cleanup(connector);
  109. kfree(connector);
  110. }
  111. static struct drm_connector_helper_funcs udl_connector_helper_funcs = {
  112. .get_modes = udl_get_modes,
  113. .mode_valid = udl_mode_valid,
  114. .best_encoder = udl_best_single_encoder,
  115. };
  116. static struct drm_connector_funcs udl_connector_funcs = {
  117. .dpms = drm_helper_connector_dpms,
  118. .detect = udl_detect,
  119. .fill_modes = drm_helper_probe_single_connector_modes,
  120. .destroy = udl_connector_destroy,
  121. .set_property = udl_connector_set_property,
  122. };
  123. int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder)
  124. {
  125. struct drm_connector *connector;
  126. connector = kzalloc(sizeof(struct drm_connector), GFP_KERNEL);
  127. if (!connector)
  128. return -ENOMEM;
  129. drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_DVII);
  130. drm_connector_helper_add(connector, &udl_connector_helper_funcs);
  131. drm_sysfs_connector_add(connector);
  132. drm_mode_connector_attach_encoder(connector, encoder);
  133. drm_object_attach_property(&connector->base,
  134. dev->mode_config.dirty_info_property,
  135. 1);
  136. return 0;
  137. }