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