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",
29 * usb_ep_type_string() - Returns human readable-name of the endpoint type.
30 * @ep_type: The endpoint type to return human-readable name for. If it's not
31 * any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
32 * usually got by usb_endpoint_type(), the string 'unknown' will be returned.
34 const char *usb_ep_type_string(int ep_type)
36 if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
39 return ep_type_names[ep_type];
41 EXPORT_SYMBOL_GPL(usb_ep_type_string);
43 const char *usb_otg_state_string(enum usb_otg_state state)
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",
61 if (state < 0 || state >= ARRAY_SIZE(names))
66 EXPORT_SYMBOL_GPL(usb_otg_state_string);
68 static 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",
78 static 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",
86 * usb_speed_string() - Returns human readable-name of the speed.
87 * @speed: The speed to return human-readable name for. If it's not
88 * any of the speeds defined in usb_device_speed enum, string for
89 * USB_SPEED_UNKNOWN will be returned.
91 const char *usb_speed_string(enum usb_device_speed speed)
93 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
94 speed = USB_SPEED_UNKNOWN;
95 return speed_names[speed];
97 EXPORT_SYMBOL_GPL(usb_speed_string);
100 * usb_get_maximum_speed - Get maximum requested speed for a given USB
102 * @dev: Pointer to the given USB controller device
104 * The function gets the maximum speed string from property "maximum-speed",
105 * and returns the corresponding enum usb_device_speed.
107 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
109 const char *maximum_speed;
112 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
114 return USB_SPEED_UNKNOWN;
116 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
118 return USB_SPEED_SUPER_PLUS;
120 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
121 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
123 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
126 * usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count
127 * of a SuperSpeed Plus capable device.
128 * @dev: Pointer to the given USB controller device
130 * If the string from "maximum-speed" property is super-speed-plus-genXxY where
131 * 'X' is the generation number and 'Y' is the number of lanes, then this
132 * function returns the corresponding enum usb_ssp_rate.
134 enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
136 const char *maximum_speed;
139 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
141 return USB_SSP_GEN_UNKNOWN;
143 ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
144 return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
146 EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
149 * usb_state_string - Returns human readable name for the state.
150 * @state: The state to return a human-readable name for. If it's not
151 * any of the states devices in usb_device_state_string enum,
152 * the string UNKNOWN will be returned.
154 const char *usb_state_string(enum usb_device_state state)
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",
168 if (state < 0 || state >= ARRAY_SIZE(names))
173 EXPORT_SYMBOL_GPL(usb_state_string);
175 static 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",
182 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
186 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
187 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
190 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
195 err = device_property_read_string(dev, "dr_mode", &dr_mode);
197 return USB_DR_MODE_UNKNOWN;
199 return usb_get_dr_mode_from_string(dr_mode);
201 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
204 * usb_decode_interval - Decode bInterval into the time expressed in 1us unit
205 * @epd: The descriptor of the endpoint
206 * @speed: The speed that the endpoint works as
208 * Function returns the interval expressed in 1us unit for servicing
209 * endpoint for data transfers.
211 unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
212 enum usb_device_speed speed)
214 unsigned int interval = 0;
216 switch (usb_endpoint_type(epd)) {
217 case USB_ENDPOINT_XFER_CONTROL:
218 /* uframes per NAK */
219 if (speed == USB_SPEED_HIGH)
220 interval = epd->bInterval;
222 case USB_ENDPOINT_XFER_ISOC:
223 interval = 1 << (epd->bInterval - 1);
225 case USB_ENDPOINT_XFER_BULK:
226 /* uframes per NAK */
227 if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
228 interval = epd->bInterval;
230 case USB_ENDPOINT_XFER_INT:
231 if (speed >= USB_SPEED_HIGH)
232 interval = 1 << (epd->bInterval - 1);
234 interval = epd->bInterval;
238 interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
242 EXPORT_SYMBOL_GPL(usb_decode_interval);
246 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
247 * which is associated with the given phy device_node
248 * @np: Pointer to the given phy device_node
249 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
250 * phys which do not have phy-cells
252 * In dts a usb controller associates with phy devices. The function gets
253 * the string from property 'dr_mode' of the controller associated with the
254 * given phy device node, and returns the correspondig enum usb_dr_mode.
256 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
258 struct device_node *controller = NULL;
259 struct of_phandle_args args;
265 controller = of_find_node_with_property(controller, "phys");
266 if (!of_device_is_available(controller))
271 args.np = of_parse_phandle(controller, "phys",
275 err = of_parse_phandle_with_args(controller,
276 "phys", "#phy-cells",
282 of_node_put(args.np);
283 if (args.np == np && (args.args_count == 0 ||
284 args.args[0] == arg0))
288 } while (controller);
291 err = of_property_read_string(controller, "dr_mode", &dr_mode);
292 of_node_put(controller);
295 return USB_DR_MODE_UNKNOWN;
297 return usb_get_dr_mode_from_string(dr_mode);
299 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
302 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
303 * for given targeted hosts (non-PC hosts)
304 * @np: Pointer to the given device_node
306 * The function gets if the targeted hosts support TPL or not
308 bool of_usb_host_tpl_support(struct device_node *np)
310 return of_property_read_bool(np, "tpl-support");
312 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
315 * of_usb_update_otg_caps - to update usb otg capabilities according to
316 * the passed properties in DT.
317 * @np: Pointer to the given device_node
318 * @otg_caps: Pointer to the target usb_otg_caps to be set
320 * The function updates the otg capabilities
322 int of_usb_update_otg_caps(struct device_node *np,
323 struct usb_otg_caps *otg_caps)
330 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
336 /* Choose the lesser one if it's already been set */
337 if (otg_caps->otg_rev)
338 otg_caps->otg_rev = min_t(u16, otg_rev,
341 otg_caps->otg_rev = otg_rev;
344 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
350 * otg-rev is mandatory for otg properties, if not passed
351 * we set it to be 0 and assume it's a legacy otg device.
352 * Non-dt platform can set it afterwards.
354 otg_caps->otg_rev = 0;
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;
367 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
370 * usb_of_get_companion_dev - Find the companion device
371 * @dev: the device pointer to find a companion
373 * Find the companion device from platform bus.
375 * Takes a reference to the returned struct device which needs to be dropped
378 * Return: On success, a pointer to the companion device, %NULL on failure.
380 struct device *usb_of_get_companion_dev(struct device *dev)
382 struct device_node *node;
383 struct platform_device *pdev = NULL;
385 node = of_parse_phandle(dev->of_node, "companion", 0);
387 pdev = of_find_device_by_node(node);
391 return pdev ? &pdev->dev : NULL;
393 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
396 struct dentry *usb_debug_root;
397 EXPORT_SYMBOL_GPL(usb_debug_root);
399 static int __init usb_common_init(void)
401 usb_debug_root = debugfs_create_dir("usb", NULL);
406 static void __exit usb_common_exit(void)
409 debugfs_remove_recursive(usb_debug_root);
412 subsys_initcall(usb_common_init);
413 module_exit(usb_common_exit);
415 MODULE_LICENSE("GPL");