1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/usb/ch9.h>
15#include <linux/usb/of.h>
16#include <linux/usb/otg.h>
17#include <linux/of_platform.h>
18#include <linux/debugfs.h>
19#include "common.h"
20
21static const char *const ep_type_names[] = {
22 [USB_ENDPOINT_XFER_CONTROL] = "ctrl",
23 [USB_ENDPOINT_XFER_ISOC] = "isoc",
24 [USB_ENDPOINT_XFER_BULK] = "bulk",
25 [USB_ENDPOINT_XFER_INT] = "intr",
26};
27
28const char *usb_ep_type_string(int ep_type)
29{
30 if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
31 return "unknown";
32
33 return ep_type_names[ep_type];
34}
35EXPORT_SYMBOL_GPL(usb_ep_type_string);
36
37const char *usb_otg_state_string(enum usb_otg_state state)
38{
39 static const char *const names[] = {
40 [OTG_STATE_A_IDLE] = "a_idle",
41 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
42 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
43 [OTG_STATE_A_HOST] = "a_host",
44 [OTG_STATE_A_SUSPEND] = "a_suspend",
45 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
46 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
47 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
48 [OTG_STATE_B_IDLE] = "b_idle",
49 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
50 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
51 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
52 [OTG_STATE_B_HOST] = "b_host",
53 };
54
55 if (state < 0 || state >= ARRAY_SIZE(names))
56 return "UNDEFINED";
57
58 return names[state];
59}
60EXPORT_SYMBOL_GPL(usb_otg_state_string);
61
62static const char *const speed_names[] = {
63 [USB_SPEED_UNKNOWN] = "UNKNOWN",
64 [USB_SPEED_LOW] = "low-speed",
65 [USB_SPEED_FULL] = "full-speed",
66 [USB_SPEED_HIGH] = "high-speed",
67 [USB_SPEED_WIRELESS] = "wireless",
68 [USB_SPEED_SUPER] = "super-speed",
69 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
70};
71
72static const char *const ssp_rate[] = {
73 [USB_SSP_GEN_UNKNOWN] = "UNKNOWN",
74 [USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1",
75 [USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2",
76 [USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2",
77};
78
79const char *usb_speed_string(enum usb_device_speed speed)
80{
81 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
82 speed = USB_SPEED_UNKNOWN;
83 return speed_names[speed];
84}
85EXPORT_SYMBOL_GPL(usb_speed_string);
86
87enum usb_device_speed usb_get_maximum_speed(struct device *dev)
88{
89 const char *maximum_speed;
90 int ret;
91
92 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
93 if (ret < 0)
94 return USB_SPEED_UNKNOWN;
95
96 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
97 if (ret > 0)
98 return USB_SPEED_SUPER_PLUS;
99
100 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
101 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
102}
103EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
104
105enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
106{
107 const char *maximum_speed;
108 int ret;
109
110 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
111 if (ret < 0)
112 return USB_SSP_GEN_UNKNOWN;
113
114 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
115 return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
116}
117EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
118
119const char *usb_state_string(enum usb_device_state state)
120{
121 static const char *const names[] = {
122 [USB_STATE_NOTATTACHED] = "not attached",
123 [USB_STATE_ATTACHED] = "attached",
124 [USB_STATE_POWERED] = "powered",
125 [USB_STATE_RECONNECTING] = "reconnecting",
126 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
127 [USB_STATE_DEFAULT] = "default",
128 [USB_STATE_ADDRESS] = "addressed",
129 [USB_STATE_CONFIGURED] = "configured",
130 [USB_STATE_SUSPENDED] = "suspended",
131 };
132
133 if (state < 0 || state >= ARRAY_SIZE(names))
134 return "UNKNOWN";
135
136 return names[state];
137}
138EXPORT_SYMBOL_GPL(usb_state_string);
139
140static const char *const usb_dr_modes[] = {
141 [USB_DR_MODE_UNKNOWN] = "",
142 [USB_DR_MODE_HOST] = "host",
143 [USB_DR_MODE_PERIPHERAL] = "peripheral",
144 [USB_DR_MODE_OTG] = "otg",
145};
146
147static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
148{
149 int ret;
150
151 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
152 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
153}
154
155enum usb_dr_mode usb_get_dr_mode(struct device *dev)
156{
157 const char *dr_mode;
158 int err;
159
160 err = device_property_read_string(dev, "dr_mode", &dr_mode);
161 if (err < 0)
162 return USB_DR_MODE_UNKNOWN;
163
164 return usb_get_dr_mode_from_string(dr_mode);
165}
166EXPORT_SYMBOL_GPL(usb_get_dr_mode);
167
168#ifdef CONFIG_OF
169
170
171
172
173
174
175
176
177
178
179
180enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
181{
182 struct device_node *controller = NULL;
183 struct of_phandle_args args;
184 const char *dr_mode;
185 int index;
186 int err;
187
188 do {
189 controller = of_find_node_with_property(controller, "phys");
190 if (!of_device_is_available(controller))
191 continue;
192 index = 0;
193 do {
194 if (arg0 == -1) {
195 args.np = of_parse_phandle(controller, "phys",
196 index);
197 args.args_count = 0;
198 } else {
199 err = of_parse_phandle_with_args(controller,
200 "phys", "#phy-cells",
201 index, &args);
202 if (err)
203 break;
204 }
205
206 of_node_put(args.np);
207 if (args.np == np && (args.args_count == 0 ||
208 args.args[0] == arg0))
209 goto finish;
210 index++;
211 } while (args.np);
212 } while (controller);
213
214finish:
215 err = of_property_read_string(controller, "dr_mode", &dr_mode);
216 of_node_put(controller);
217
218 if (err < 0)
219 return USB_DR_MODE_UNKNOWN;
220
221 return usb_get_dr_mode_from_string(dr_mode);
222}
223EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
224
225
226
227
228
229
230
231
232bool of_usb_host_tpl_support(struct device_node *np)
233{
234 return of_property_read_bool(np, "tpl-support");
235}
236EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
237
238
239
240
241
242
243
244
245
246int of_usb_update_otg_caps(struct device_node *np,
247 struct usb_otg_caps *otg_caps)
248{
249 u32 otg_rev;
250
251 if (!otg_caps)
252 return -EINVAL;
253
254 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
255 switch (otg_rev) {
256 case 0x0100:
257 case 0x0120:
258 case 0x0130:
259 case 0x0200:
260
261 if (otg_caps->otg_rev)
262 otg_caps->otg_rev = min_t(u16, otg_rev,
263 otg_caps->otg_rev);
264 else
265 otg_caps->otg_rev = otg_rev;
266 break;
267 default:
268 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
269 np, otg_rev);
270 return -EINVAL;
271 }
272 } else {
273
274
275
276
277
278 otg_caps->otg_rev = 0;
279 }
280
281 if (of_property_read_bool(np, "hnp-disable"))
282 otg_caps->hnp_support = false;
283 if (of_property_read_bool(np, "srp-disable"))
284 otg_caps->srp_support = false;
285 if (of_property_read_bool(np, "adp-disable") ||
286 (otg_caps->otg_rev < 0x0200))
287 otg_caps->adp_support = false;
288
289 return 0;
290}
291EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
292
293
294
295
296
297
298
299
300
301
302
303
304struct device *usb_of_get_companion_dev(struct device *dev)
305{
306 struct device_node *node;
307 struct platform_device *pdev = NULL;
308
309 node = of_parse_phandle(dev->of_node, "companion", 0);
310 if (node)
311 pdev = of_find_device_by_node(node);
312
313 of_node_put(node);
314
315 return pdev ? &pdev->dev : NULL;
316}
317EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
318#endif
319
320struct dentry *usb_debug_root;
321EXPORT_SYMBOL_GPL(usb_debug_root);
322
323static int __init usb_common_init(void)
324{
325 usb_debug_root = debugfs_create_dir("usb", NULL);
326 ledtrig_usb_init();
327 return 0;
328}
329
330static void __exit usb_common_exit(void)
331{
332 ledtrig_usb_exit();
333 debugfs_remove_recursive(usb_debug_root);
334}
335
336subsys_initcall(usb_common_init);
337module_exit(usb_common_exit);
338
339MODULE_LICENSE("GPL");
340