]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/thunderbolt/cap.c
Merge tag 'nfs-for-4.13-3' of git://git.linux-nfs.org/projects/anna/linux-nfs
[karo-tx-linux.git] / drivers / thunderbolt / cap.c
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
12 #define CAP_OFFSET_MAX          0xff
13 #define VSE_CAP_OFFSET_MAX      0xffff
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
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)
33 {
34         u32 offset;
35
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;
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
63 static 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;
82 }
83
84 /**
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
88  *
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.
93  */
94 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
95 {
96         struct tb_cap_any header;
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)
117                                 return offset;
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;
125                 }
126         }
127
128         return -ENOENT;
129 }