1 // SPDX-License-Identifier: GPL-2.0
3 * Provides code common for host and device side USB.
5 * If either host side (ie. CONFIG_USB=y) or device side USB stack
6 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
7 * compiled-in as well. Otherwise, if either of the two stacks is
8 * compiled as module, this file is compiled as module as well.
11 #include <linux/kernel.h>
12 #include <linux/module.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>
21 static 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",
28 const char *usb_ep_type_string(int ep_type)
30 if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
33 return ep_type_names[ep_type];
35 EXPORT_SYMBOL_GPL(usb_ep_type_string);
37 const char *usb_otg_state_string(enum usb_otg_state state)
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",
55 if (state < 0 || state >= ARRAY_SIZE(names))
60 EXPORT_SYMBOL_GPL(usb_otg_state_string);
62 static 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",
72 static 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",
79 const char *usb_speed_string(enum usb_device_speed speed)
81 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
82 speed = USB_SPEED_UNKNOWN;
83 return speed_names[speed];
85 EXPORT_SYMBOL_GPL(usb_speed_string);
87 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
89 const char *maximum_speed;
92 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
94 return USB_SPEED_UNKNOWN;
96 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
98 return USB_SPEED_SUPER_PLUS;
100 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
101 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
103 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
105 enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
107 const char *maximum_speed;
110 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
112 return USB_SSP_GEN_UNKNOWN;
114 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
115 return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
117 EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
119 const char *usb_state_string(enum usb_device_state state)
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",
133 if (state < 0 || state >= ARRAY_SIZE(names))
138 EXPORT_SYMBOL_GPL(usb_state_string);
140 static 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",
147 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
151 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
152 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
155 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
160 err = device_property_read_string(dev, "dr_mode", &dr_mode);
162 return USB_DR_MODE_UNKNOWN;
164 return usb_get_dr_mode_from_string(dr_mode);
166 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
170 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
171 * which is associated with the given phy device_node
172 * @np: Pointer to the given phy device_node
173 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
174 * phys which do not have phy-cells
176 * In dts a usb controller associates with phy devices. The function gets
177 * the string from property 'dr_mode' of the controller associated with the
178 * given phy device node, and returns the correspondig enum usb_dr_mode.
180 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
182 struct device_node *controller = NULL;
183 struct of_phandle_args args;
189 controller = of_find_node_with_property(controller, "phys");
190 if (!of_device_is_available(controller))
195 args.np = of_parse_phandle(controller, "phys",
199 err = of_parse_phandle_with_args(controller,
200 "phys", "#phy-cells",
206 of_node_put(args.np);
207 if (args.np == np && (args.args_count == 0 ||
208 args.args[0] == arg0))
212 } while (controller);
215 err = of_property_read_string(controller, "dr_mode", &dr_mode);
216 of_node_put(controller);
219 return USB_DR_MODE_UNKNOWN;
221 return usb_get_dr_mode_from_string(dr_mode);
223 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
226 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
227 * for given targeted hosts (non-PC hosts)
228 * @np: Pointer to the given device_node
230 * The function gets if the targeted hosts support TPL or not
232 bool of_usb_host_tpl_support(struct device_node *np)
234 return of_property_read_bool(np, "tpl-support");
236 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
239 * of_usb_update_otg_caps - to update usb otg capabilities according to
240 * the passed properties in DT.
241 * @np: Pointer to the given device_node
242 * @otg_caps: Pointer to the target usb_otg_caps to be set
244 * The function updates the otg capabilities
246 int of_usb_update_otg_caps(struct device_node *np,
247 struct usb_otg_caps *otg_caps)
254 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
260 /* Choose the lesser one if it's already been set */
261 if (otg_caps->otg_rev)
262 otg_caps->otg_rev = min_t(u16, otg_rev,
265 otg_caps->otg_rev = otg_rev;
268 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
274 * otg-rev is mandatory for otg properties, if not passed
275 * we set it to be 0 and assume it's a legacy otg device.
276 * Non-dt platform can set it afterwards.
278 otg_caps->otg_rev = 0;
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;
291 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
294 * usb_of_get_companion_dev - Find the companion device
295 * @dev: the device pointer to find a companion
297 * Find the companion device from platform bus.
299 * Takes a reference to the returned struct device which needs to be dropped
302 * Return: On success, a pointer to the companion device, %NULL on failure.
304 struct device *usb_of_get_companion_dev(struct device *dev)
306 struct device_node *node;
307 struct platform_device *pdev = NULL;
309 node = of_parse_phandle(dev->of_node, "companion", 0);
311 pdev = of_find_device_by_node(node);
315 return pdev ? &pdev->dev : NULL;
317 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
320 struct dentry *usb_debug_root;
321 EXPORT_SYMBOL_GPL(usb_debug_root);
323 static int __init usb_common_init(void)
325 usb_debug_root = debugfs_create_dir("usb", NULL);
330 static void __exit usb_common_exit(void)
333 debugfs_remove_recursive(usb_debug_root);
336 subsys_initcall(usb_common_init);
337 module_exit(usb_common_exit);
339 MODULE_LICENSE("GPL");