udl_connector.c 3.9 KB

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