cap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Thunderbolt Cactus Ridge driver - capabilities lookup
  3. *
  4. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/errno.h>
  8. #include "tb.h"
  9. #define CAP_OFFSET_MAX 0xff
  10. #define VSE_CAP_OFFSET_MAX 0xffff
  11. struct tb_cap_any {
  12. union {
  13. struct tb_cap_basic basic;
  14. struct tb_cap_extended_short extended_short;
  15. struct tb_cap_extended_long extended_long;
  16. };
  17. } __packed;
  18. /**
  19. * tb_port_find_cap() - Find port capability
  20. * @port: Port to find the capability for
  21. * @cap: Capability to look
  22. *
  23. * Returns offset to start of capability or %-ENOENT if no such
  24. * capability was found. Negative errno is returned if there was an
  25. * error.
  26. */
  27. int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
  28. {
  29. u32 offset;
  30. /*
  31. * DP out adapters claim to implement TMU capability but in
  32. * reality they do not so we hard code the adapter specific
  33. * capability offset here.
  34. */
  35. if (port->config.type == TB_TYPE_DP_HDMI_OUT)
  36. offset = 0x39;
  37. else
  38. offset = 0x1;
  39. do {
  40. struct tb_cap_any header;
  41. int ret;
  42. ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
  43. if (ret)
  44. return ret;
  45. if (header.basic.cap == cap)
  46. return offset;
  47. offset = header.basic.next;
  48. } while (offset);
  49. return -ENOENT;
  50. }
  51. static int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
  52. {
  53. int offset = sw->config.first_cap_offset;
  54. while (offset > 0 && offset < CAP_OFFSET_MAX) {
  55. struct tb_cap_any header;
  56. int ret;
  57. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
  58. if (ret)
  59. return ret;
  60. if (header.basic.cap == cap)
  61. return offset;
  62. offset = header.basic.next;
  63. }
  64. return -ENOENT;
  65. }
  66. /**
  67. * tb_switch_find_vse_cap() - Find switch vendor specific capability
  68. * @sw: Switch to find the capability for
  69. * @vsec: Vendor specific capability to look
  70. *
  71. * Functions enumerates vendor specific capabilities (VSEC) of a switch
  72. * and returns offset when capability matching @vsec is found. If no
  73. * such capability is found returns %-ENOENT. In case of error returns
  74. * negative errno.
  75. */
  76. int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
  77. {
  78. struct tb_cap_any header;
  79. int offset;
  80. offset = tb_switch_find_cap(sw, TB_SWITCH_CAP_VSE);
  81. if (offset < 0)
  82. return offset;
  83. while (offset > 0 && offset < VSE_CAP_OFFSET_MAX) {
  84. int ret;
  85. ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
  86. if (ret)
  87. return ret;
  88. /*
  89. * Extended vendor specific capabilities come in two
  90. * flavors: short and long. The latter is used when
  91. * offset is over 0xff.
  92. */
  93. if (offset >= CAP_OFFSET_MAX) {
  94. if (header.extended_long.vsec_id == vsec)
  95. return offset;
  96. offset = header.extended_long.next;
  97. } else {
  98. if (header.extended_short.vsec_id == vsec)
  99. return offset;
  100. if (!header.extended_short.length)
  101. return -ENOENT;
  102. offset = header.extended_short.next;
  103. }
  104. }
  105. return -ENOENT;
  106. }