2 * Thunderbolt Cactus Ridge driver - capabilities lookup
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
12 #define CAP_OFFSET_MAX 0xff
13 #define VSE_CAP_OFFSET_MAX 0xffff
17 struct tb_cap_basic basic
;
18 struct tb_cap_extended_short extended_short
;
19 struct tb_cap_extended_long extended_long
;
24 * tb_port_find_cap() - Find port capability
25 * @port: Port to find the capability for
26 * @cap: Capability to look
28 * Returns offset to start of capability or %-ENOENT if no such
29 * capability was found. Negative errno is returned if there was an
32 int tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
)
37 * 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.
41 if (port
->config
.type
== TB_TYPE_DP_HDMI_OUT
)
47 struct tb_cap_any header
;
50 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, offset
, 1);
54 if (header
.basic
.cap
== cap
)
57 offset
= header
.basic
.next
;
63 static int tb_switch_find_cap(struct tb_switch
*sw
, enum tb_switch_cap cap
)
65 int offset
= sw
->config
.first_cap_offset
;
67 while (offset
> 0 && offset
< CAP_OFFSET_MAX
) {
68 struct tb_cap_any header
;
71 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 1);
75 if (header
.basic
.cap
== cap
)
78 offset
= header
.basic
.next
;
85 * 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
89 * 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
94 int tb_switch_find_vse_cap(struct tb_switch
*sw
, enum tb_switch_vse_cap vsec
)
96 struct tb_cap_any header
;
99 offset
= tb_switch_find_cap(sw
, TB_SWITCH_CAP_VSE
);
103 while (offset
> 0 && offset
< VSE_CAP_OFFSET_MAX
) {
106 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 2);
111 * Extended vendor specific capabilities come in two
112 * flavors: short and long. The latter is used when
113 * offset is over 0xff.
115 if (offset
>= CAP_OFFSET_MAX
) {
116 if (header
.extended_long
.vsec_id
== vsec
)
118 offset
= header
.extended_long
.next
;
120 if (header
.extended_short
.vsec_id
== vsec
)
122 if (!header
.extended_short
.length
)
124 offset
= header
.extended_short
.next
;