cap.c 2.9 KB

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