blob: 38bc27a5ce4fd5599d1b2c0603531356c35e983d [file] [log] [blame]
Andreas Noevere2b87852014-06-03 22:04:03 +02001/*
2 * Thunderbolt Cactus Ridge driver - capabilities lookup
3 *
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
6
7#include <linux/slab.h>
8#include <linux/errno.h>
9
10#include "tb.h"
11
Mika Westerbergda2da042017-06-06 15:24:58 +030012#define CAP_OFFSET_MAX 0xff
13#define VSE_CAP_OFFSET_MAX 0xffff
Andreas Noevere2b87852014-06-03 22:04:03 +020014
15struct tb_cap_any {
16 union {
17 struct tb_cap_basic basic;
18 struct tb_cap_extended_short extended_short;
19 struct tb_cap_extended_long extended_long;
20 };
21} __packed;
22
Mika Westerbergda2da042017-06-06 15:24:58 +030023/**
24 * tb_port_find_cap() - Find port capability
25 * @port: Port to find the capability for
26 * @cap: Capability to look
27 *
28 * Returns offset to start of capability or %-ENOENT if no such
29 * capability was found. Negative errno is returned if there was an
30 * error.
31 */
32int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
Andreas Noevere2b87852014-06-03 22:04:03 +020033{
Mika Westerbergda2da042017-06-06 15:24:58 +030034 u32 offset;
Andreas Noevere2b87852014-06-03 22:04:03 +020035
Andreas Noevere2b87852014-06-03 22:04:03 +020036 /*
Mika Westerbergda2da042017-06-06 15:24:58 +030037 * DP out adapters claim to implement TMU capability but in
38 * reality they do not so we hard code the adapter specific
39 * capability offset here.
Andreas Noevere2b87852014-06-03 22:04:03 +020040 */
Mika Westerbergda2da042017-06-06 15:24:58 +030041 if (port->config.type == TB_TYPE_DP_HDMI_OUT)
42 offset = 0x39;
43 else
44 offset = 0x1;
45
46 do {
47 struct tb_cap_any header;
48 int ret;
49
50 ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
51 if (ret)
52 return ret;
53
54 if (header.basic.cap == cap)
55 return offset;
56
57 offset = header.basic.next;
58 } while (offset);
59
60 return -ENOENT;
61}
62
63static int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
64{
65 int offset = sw->config.first_cap_offset;
66
67 while (offset > 0 && offset < CAP_OFFSET_MAX) {
68 struct tb_cap_any header;
69 int ret;
70
71 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
72 if (ret)
73 return ret;
74
75 if (header.basic.cap == cap)
76 return offset;
77
78 offset = header.basic.next;
79 }
80
81 return -ENOENT;
Andreas Noevere2b87852014-06-03 22:04:03 +020082}
83
84/**
Mika Westerbergda2da042017-06-06 15:24:58 +030085 * tb_switch_find_vse_cap() - Find switch vendor specific capability
86 * @sw: Switch to find the capability for
87 * @vsec: Vendor specific capability to look
Andreas Noevere2b87852014-06-03 22:04:03 +020088 *
Mika Westerbergda2da042017-06-06 15:24:58 +030089 * Functions enumerates vendor specific capabilities (VSEC) of a switch
90 * and returns offset when capability matching @vsec is found. If no
91 * such capability is found returns %-ENOENT. In case of error returns
92 * negative errno.
Andreas Noevere2b87852014-06-03 22:04:03 +020093 */
Mika Westerbergda2da042017-06-06 15:24:58 +030094int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
Andreas Noevere2b87852014-06-03 22:04:03 +020095{
Andreas Noevere2b87852014-06-03 22:04:03 +020096 struct tb_cap_any header;
Mika Westerbergda2da042017-06-06 15:24:58 +030097 int offset;
98
99 offset = tb_switch_find_cap(sw, TB_SWITCH_CAP_VSE);
100 if (offset < 0)
101 return offset;
102
103 while (offset > 0 && offset < VSE_CAP_OFFSET_MAX) {
104 int ret;
105
106 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
107 if (ret)
108 return ret;
109
110 /*
111 * Extended vendor specific capabilities come in two
112 * flavors: short and long. The latter is used when
113 * offset is over 0xff.
114 */
115 if (offset >= CAP_OFFSET_MAX) {
116 if (header.extended_long.vsec_id == vsec)
Andreas Noevere2b87852014-06-03 22:04:03 +0200117 return offset;
Mika Westerbergda2da042017-06-06 15:24:58 +0300118 offset = header.extended_long.next;
119 } else {
120 if (header.extended_short.vsec_id == vsec)
121 return offset;
122 if (!header.extended_short.length)
123 return -ENOENT;
124 offset = header.extended_short.next;
Andreas Noevere2b87852014-06-03 22:04:03 +0200125 }
Andreas Noevere2b87852014-06-03 22:04:03 +0200126 }
Mika Westerbergda2da042017-06-06 15:24:58 +0300127
128 return -ENOENT;
Andreas Noevere2b87852014-06-03 22:04:03 +0200129}