cap.c 2.9 KB

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