udl_edid.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/string.h>
  3. #include <drm/drm_drv.h>
  4. #include <drm/drm_edid.h>
  5. #include "udl_drv.h"
  6. #include "udl_edid.h"
  7. static int udl_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
  8. {
  9. struct udl_device *udl = data;
  10. struct drm_device *dev = &udl->drm;
  11. struct usb_device *udev = udl_to_usb_device(udl);
  12. u8 *read_buff;
  13. int idx, ret;
  14. size_t i;
  15. read_buff = kmalloc(2, GFP_KERNEL);
  16. if (!read_buff)
  17. return -ENOMEM;
  18. if (!drm_dev_enter(dev, &idx)) {
  19. ret = -ENODEV;
  20. goto err_kfree;
  21. }
  22. for (i = 0; i < len; i++) {
  23. int bval = (i + block * EDID_LENGTH) << 8;
  24. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  25. 0x02, (0x80 | (0x02 << 5)), bval,
  26. 0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT);
  27. if (ret < 0) {
  28. drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret);
  29. goto err_drm_dev_exit;
  30. } else if (ret < 1) {
  31. ret = -EIO;
  32. drm_err(dev, "Read EDID byte %zu failed\n", i);
  33. goto err_drm_dev_exit;
  34. }
  35. buf[i] = read_buff[1];
  36. }
  37. drm_dev_exit(idx);
  38. kfree(read_buff);
  39. return 0;
  40. err_drm_dev_exit:
  41. drm_dev_exit(idx);
  42. err_kfree:
  43. kfree(read_buff);
  44. return ret;
  45. }
  46. bool udl_probe_edid(struct udl_device *udl)
  47. {
  48. u8 hdr[8];
  49. int ret;
  50. ret = udl_read_edid_block(udl, hdr, 0, sizeof(hdr));
  51. if (ret)
  52. return false;
  53. /*
  54. * The adapter sends all-zeros if no monitor has been
  55. * connected. We consider anything else a connection.
  56. */
  57. return !mem_is_zero(hdr, sizeof(hdr));
  58. }
  59. const struct drm_edid *udl_edid_read(struct drm_connector *connector)
  60. {
  61. struct udl_device *udl = to_udl(connector->dev);
  62. return drm_edid_read_custom(connector, udl_read_edid_block, udl);
  63. }