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>
19 const char *usb_otg_state_string(enum usb_otg_state state
)
21 static const char *const names
[] = {
22 [OTG_STATE_A_IDLE
] = "a_idle",
23 [OTG_STATE_A_WAIT_VRISE
] = "a_wait_vrise",
24 [OTG_STATE_A_WAIT_BCON
] = "a_wait_bcon",
25 [OTG_STATE_A_HOST
] = "a_host",
26 [OTG_STATE_A_SUSPEND
] = "a_suspend",
27 [OTG_STATE_A_PERIPHERAL
] = "a_peripheral",
28 [OTG_STATE_A_WAIT_VFALL
] = "a_wait_vfall",
29 [OTG_STATE_A_VBUS_ERR
] = "a_vbus_err",
30 [OTG_STATE_B_IDLE
] = "b_idle",
31 [OTG_STATE_B_SRP_INIT
] = "b_srp_init",
32 [OTG_STATE_B_PERIPHERAL
] = "b_peripheral",
33 [OTG_STATE_B_WAIT_ACON
] = "b_wait_acon",
34 [OTG_STATE_B_HOST
] = "b_host",
37 if (state
< 0 || state
>= ARRAY_SIZE(names
))
42 EXPORT_SYMBOL_GPL(usb_otg_state_string
);
44 static const char *const speed_names
[] = {
45 [USB_SPEED_UNKNOWN
] = "UNKNOWN",
46 [USB_SPEED_LOW
] = "low-speed",
47 [USB_SPEED_FULL
] = "full-speed",
48 [USB_SPEED_HIGH
] = "high-speed",
49 [USB_SPEED_WIRELESS
] = "wireless",
50 [USB_SPEED_SUPER
] = "super-speed",
51 [USB_SPEED_SUPER_PLUS
] = "super-speed-plus",
54 const char *usb_speed_string(enum usb_device_speed speed
)
56 if (speed
< 0 || speed
>= ARRAY_SIZE(speed_names
))
57 speed
= USB_SPEED_UNKNOWN
;
58 return speed_names
[speed
];
60 EXPORT_SYMBOL_GPL(usb_speed_string
);
62 enum usb_device_speed
usb_get_maximum_speed(struct device
*dev
)
64 const char *maximum_speed
;
67 ret
= device_property_read_string(dev
, "maximum-speed", &maximum_speed
);
69 return USB_SPEED_UNKNOWN
;
71 ret
= match_string(speed_names
, ARRAY_SIZE(speed_names
), maximum_speed
);
73 return (ret
< 0) ? USB_SPEED_UNKNOWN
: ret
;
75 EXPORT_SYMBOL_GPL(usb_get_maximum_speed
);
77 const char *usb_state_string(enum usb_device_state state
)
79 static const char *const names
[] = {
80 [USB_STATE_NOTATTACHED
] = "not attached",
81 [USB_STATE_ATTACHED
] = "attached",
82 [USB_STATE_POWERED
] = "powered",
83 [USB_STATE_RECONNECTING
] = "reconnecting",
84 [USB_STATE_UNAUTHENTICATED
] = "unauthenticated",
85 [USB_STATE_DEFAULT
] = "default",
86 [USB_STATE_ADDRESS
] = "addressed",
87 [USB_STATE_CONFIGURED
] = "configured",
88 [USB_STATE_SUSPENDED
] = "suspended",
91 if (state
< 0 || state
>= ARRAY_SIZE(names
))
96 EXPORT_SYMBOL_GPL(usb_state_string
);
98 static const char *const usb_dr_modes
[] = {
99 [USB_DR_MODE_UNKNOWN
] = "",
100 [USB_DR_MODE_HOST
] = "host",
101 [USB_DR_MODE_PERIPHERAL
] = "peripheral",
102 [USB_DR_MODE_OTG
] = "otg",
105 static enum usb_dr_mode
usb_get_dr_mode_from_string(const char *str
)
109 ret
= match_string(usb_dr_modes
, ARRAY_SIZE(usb_dr_modes
), str
);
110 return (ret
< 0) ? USB_DR_MODE_UNKNOWN
: ret
;
113 enum usb_dr_mode
usb_get_dr_mode(struct device
*dev
)
118 err
= device_property_read_string(dev
, "dr_mode", &dr_mode
);
120 return USB_DR_MODE_UNKNOWN
;
122 return usb_get_dr_mode_from_string(dr_mode
);
124 EXPORT_SYMBOL_GPL(usb_get_dr_mode
);
128 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
129 * which is associated with the given phy device_node
130 * @np: Pointer to the given phy device_node
131 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
132 * phys which do not have phy-cells
134 * In dts a usb controller associates with phy devices. The function gets
135 * the string from property 'dr_mode' of the controller associated with the
136 * given phy device node, and returns the correspondig enum usb_dr_mode.
138 enum usb_dr_mode
of_usb_get_dr_mode_by_phy(struct device_node
*np
, int arg0
)
140 struct device_node
*controller
= NULL
;
141 struct of_phandle_args args
;
147 controller
= of_find_node_with_property(controller
, "phys");
148 if (!of_device_is_available(controller
))
153 args
.np
= of_parse_phandle(controller
, "phys",
157 err
= of_parse_phandle_with_args(controller
,
158 "phys", "#phy-cells",
164 of_node_put(args
.np
);
165 if (args
.np
== np
&& (args
.args_count
== 0 ||
166 args
.args
[0] == arg0
))
170 } while (controller
);
173 err
= of_property_read_string(controller
, "dr_mode", &dr_mode
);
174 of_node_put(controller
);
177 return USB_DR_MODE_UNKNOWN
;
179 return usb_get_dr_mode_from_string(dr_mode
);
181 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy
);
184 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
185 * for given targeted hosts (non-PC hosts)
186 * @np: Pointer to the given device_node
188 * The function gets if the targeted hosts support TPL or not
190 bool of_usb_host_tpl_support(struct device_node
*np
)
192 return of_property_read_bool(np
, "tpl-support");
194 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support
);
197 * of_usb_update_otg_caps - to update usb otg capabilities according to
198 * the passed properties in DT.
199 * @np: Pointer to the given device_node
200 * @otg_caps: Pointer to the target usb_otg_caps to be set
202 * The function updates the otg capabilities
204 int of_usb_update_otg_caps(struct device_node
*np
,
205 struct usb_otg_caps
*otg_caps
)
212 if (!of_property_read_u32(np
, "otg-rev", &otg_rev
)) {
218 /* Choose the lesser one if it's already been set */
219 if (otg_caps
->otg_rev
)
220 otg_caps
->otg_rev
= min_t(u16
, otg_rev
,
223 otg_caps
->otg_rev
= otg_rev
;
226 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
232 * otg-rev is mandatory for otg properties, if not passed
233 * we set it to be 0 and assume it's a legacy otg device.
234 * Non-dt platform can set it afterwards.
236 otg_caps
->otg_rev
= 0;
239 if (of_property_read_bool(np
, "hnp-disable"))
240 otg_caps
->hnp_support
= false;
241 if (of_property_read_bool(np
, "srp-disable"))
242 otg_caps
->srp_support
= false;
243 if (of_property_read_bool(np
, "adp-disable") ||
244 (otg_caps
->otg_rev
< 0x0200))
245 otg_caps
->adp_support
= false;
249 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps
);
252 * usb_of_get_companion_dev - Find the companion device
253 * @dev: the device pointer to find a companion
255 * Find the companion device from platform bus.
257 * Takes a reference to the returned struct device which needs to be dropped
260 * Return: On success, a pointer to the companion device, %NULL on failure.
262 struct device
*usb_of_get_companion_dev(struct device
*dev
)
264 struct device_node
*node
;
265 struct platform_device
*pdev
= NULL
;
267 node
= of_parse_phandle(dev
->of_node
, "companion", 0);
269 pdev
= of_find_device_by_node(node
);
273 return pdev
? &pdev
->dev
: NULL
;
275 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev
);
278 MODULE_LICENSE("GPL");