]>
Commit | Line | Data |
---|---|---|
e2b8785e AN |
1 | /* |
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 | ||
da2da04b MW |
12 | #define CAP_OFFSET_MAX 0xff |
13 | #define VSE_CAP_OFFSET_MAX 0xffff | |
e2b8785e AN |
14 | |
15 | struct 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 | ||
da2da04b MW |
23 | /** |
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 | */ | |
32 | int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap) | |
e2b8785e | 33 | { |
da2da04b | 34 | u32 offset; |
e2b8785e | 35 | |
da2da04b MW |
36 | /* |
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. | |
40 | */ | |
41 | if (port->config.type == TB_TYPE_DP_HDMI_OUT) | |
42 | offset = 0x39; | |
e2b8785e | 43 | else |
da2da04b MW |
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; | |
e2b8785e AN |
61 | } |
62 | ||
da2da04b | 63 | static int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap) |
e2b8785e | 64 | { |
da2da04b MW |
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; | |
e2b8785e | 79 | } |
da2da04b MW |
80 | |
81 | return -ENOENT; | |
e2b8785e AN |
82 | } |
83 | ||
84 | /** | |
da2da04b MW |
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 | |
e2b8785e | 88 | * |
da2da04b MW |
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 | |
92 | * negative errno. | |
e2b8785e | 93 | */ |
da2da04b | 94 | int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec) |
e2b8785e | 95 | { |
e2b8785e | 96 | struct tb_cap_any header; |
da2da04b MW |
97 | 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) | |
e2b8785e | 117 | return offset; |
da2da04b MW |
118 | 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; | |
e2b8785e | 125 | } |
e2b8785e | 126 | } |
da2da04b MW |
127 | |
128 | return -ENOENT; | |
e2b8785e | 129 | } |