cap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. struct tb_cap_any {
  10. union {
  11. struct tb_cap_basic basic;
  12. struct tb_cap_extended_short extended_short;
  13. struct tb_cap_extended_long extended_long;
  14. };
  15. } __packed;
  16. static bool tb_cap_is_basic(struct tb_cap_any *cap)
  17. {
  18. /* basic.cap is u8. This checks only the lower 8 bit of cap. */
  19. return cap->basic.cap != 5;
  20. }
  21. static bool tb_cap_is_long(struct tb_cap_any *cap)
  22. {
  23. return !tb_cap_is_basic(cap)
  24. && cap->extended_short.next == 0
  25. && cap->extended_short.length == 0;
  26. }
  27. static enum tb_cap tb_cap(struct tb_cap_any *cap)
  28. {
  29. if (tb_cap_is_basic(cap))
  30. return cap->basic.cap;
  31. else
  32. /* extended_short/long have cap at the same offset. */
  33. return cap->extended_short.cap;
  34. }
  35. static u32 tb_cap_next(struct tb_cap_any *cap, u32 offset)
  36. {
  37. int next;
  38. if (offset == 1) {
  39. /*
  40. * The first pointer is part of the switch header and always
  41. * a simple pointer.
  42. */
  43. next = cap->basic.next;
  44. } else {
  45. /*
  46. * Somehow Intel decided to use 3 different types of capability
  47. * headers. It is not like anyone could have predicted that
  48. * single byte offsets are not enough...
  49. */
  50. if (tb_cap_is_basic(cap))
  51. next = cap->basic.next;
  52. else if (!tb_cap_is_long(cap))
  53. next = cap->extended_short.next;
  54. else
  55. next = cap->extended_long.next;
  56. }
  57. /*
  58. * "Hey, we could terminate some capability lists with a null offset
  59. * and others with a pointer to the last element." - "Great idea!"
  60. */
  61. if (next == offset)
  62. return 0;
  63. return next;
  64. }
  65. /**
  66. * tb_find_cap() - find a capability
  67. *
  68. * Return: Returns a positive offset if the capability was found and 0 if not.
  69. * Returns an error code on failure.
  70. */
  71. int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, enum tb_cap cap)
  72. {
  73. u32 offset = 1;
  74. struct tb_cap_any header;
  75. int res;
  76. int retries = 10;
  77. while (retries--) {
  78. res = tb_port_read(port, &header, space, offset, 1);
  79. if (res) {
  80. /* Intel needs some help with linked lists. */
  81. if (space == TB_CFG_PORT && offset == 0xa
  82. && port->config.type == TB_TYPE_DP_HDMI_OUT) {
  83. offset = 0x39;
  84. continue;
  85. }
  86. return res;
  87. }
  88. if (offset != 1) {
  89. if (tb_cap(&header) == cap)
  90. return offset;
  91. if (tb_cap_is_long(&header)) {
  92. /* tb_cap_extended_long is 2 dwords */
  93. res = tb_port_read(port, &header, space,
  94. offset, 2);
  95. if (res)
  96. return res;
  97. }
  98. }
  99. offset = tb_cap_next(&header, offset);
  100. if (!offset)
  101. return 0;
  102. }
  103. tb_port_WARN(port,
  104. "run out of retries while looking for cap %#x in config space %d, last offset: %#x\n",
  105. cap, space, offset);
  106. return -EIO;
  107. }