blob: 3fd7b1652c4bc9afc2512305f04d464def84dd55 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Provides code common for host and device side USB.
4 *
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.
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_UNDEFINED] = "UNDEFINED",
41 [OTG_STATE_A_IDLE] = "a_idle",
42 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
43 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
44 [OTG_STATE_A_HOST] = "a_host",
45 [OTG_STATE_A_SUSPEND] = "a_suspend",
46 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
47 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
48 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
49 [OTG_STATE_B_IDLE] = "b_idle",
50 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
51 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
52 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
53 [OTG_STATE_B_HOST] = "b_host",
54 };
55
56 if (state < 0 || state >= ARRAY_SIZE(names))
57 return "UNDEFINED";
58
59 return names[state];
60}
61EXPORT_SYMBOL_GPL(usb_otg_state_string);
62
63static const char *const speed_names[] = {
64 [USB_SPEED_UNKNOWN] = "UNKNOWN",
65 [USB_SPEED_LOW] = "low-speed",
66 [USB_SPEED_FULL] = "full-speed",
67 [USB_SPEED_HIGH] = "high-speed",
68 [USB_SPEED_WIRELESS] = "wireless",
69 [USB_SPEED_SUPER] = "super-speed",
70 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
71};
72
73const char *usb_speed_string(enum usb_device_speed speed)
74{
75 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
76 speed = USB_SPEED_UNKNOWN;
77 return speed_names[speed];
78}
79EXPORT_SYMBOL_GPL(usb_speed_string);
80
81enum usb_device_speed usb_get_maximum_speed(struct device *dev)
82{
83 const char *maximum_speed;
84 int ret;
85
86 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
87 if (ret < 0)
88 return USB_SPEED_UNKNOWN;
89
90 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
91
92 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
93}
94EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
95
96const char *usb_state_string(enum usb_device_state state)
97{
98 static const char *const names[] = {
99 [USB_STATE_NOTATTACHED] = "not attached",
100 [USB_STATE_ATTACHED] = "attached",
101 [USB_STATE_POWERED] = "powered",
102 [USB_STATE_RECONNECTING] = "reconnecting",
103 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
104 [USB_STATE_DEFAULT] = "default",
105 [USB_STATE_ADDRESS] = "addressed",
106 [USB_STATE_CONFIGURED] = "configured",
107 [USB_STATE_SUSPENDED] = "suspended",
108 };
109
110 if (state < 0 || state >= ARRAY_SIZE(names))
111 return "UNKNOWN";
112
113 return names[state];
114}
115EXPORT_SYMBOL_GPL(usb_state_string);
116
117static const char *const usb_dr_modes[] = {
118 [USB_DR_MODE_UNKNOWN] = "",
119 [USB_DR_MODE_HOST] = "host",
120 [USB_DR_MODE_PERIPHERAL] = "peripheral",
121 [USB_DR_MODE_OTG] = "otg",
122};
123
124static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
125{
126 int ret;
127
128 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
129 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
130}
131
132enum usb_dr_mode usb_get_dr_mode(struct device *dev)
133{
134 const char *dr_mode;
135 int err;
136
137 err = device_property_read_string(dev, "dr_mode", &dr_mode);
138 if (err < 0)
139 return USB_DR_MODE_UNKNOWN;
140
141 return usb_get_dr_mode_from_string(dr_mode);
142}
143EXPORT_SYMBOL_GPL(usb_get_dr_mode);
144
145#ifdef CONFIG_OF
146/**
147 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
148 * which is associated with the given phy device_node
149 * @np: Pointer to the given phy device_node
150 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
151 * phys which do not have phy-cells
152 *
153 * In dts a usb controller associates with phy devices. The function gets
154 * the string from property 'dr_mode' of the controller associated with the
155 * given phy device node, and returns the correspondig enum usb_dr_mode.
156 */
157enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
158{
159 struct device_node *controller = NULL;
160 struct of_phandle_args args;
161 const char *dr_mode;
162 int index;
163 int err;
164
165 do {
166 controller = of_find_node_with_property(controller, "phys");
167 if (!of_device_is_available(controller))
168 continue;
169 index = 0;
170 do {
171 if (arg0 == -1) {
172 args.np = of_parse_phandle(controller, "phys",
173 index);
174 args.args_count = 0;
175 } else {
176 err = of_parse_phandle_with_args(controller,
177 "phys", "#phy-cells",
178 index, &args);
179 if (err)
180 break;
181 }
182
183 of_node_put(args.np);
184 if (args.np == np && (args.args_count == 0 ||
185 args.args[0] == arg0))
186 goto finish;
187 index++;
188 } while (args.np);
189 } while (controller);
190
191finish:
192 err = of_property_read_string(controller, "dr_mode", &dr_mode);
193 of_node_put(controller);
194
195 if (err < 0)
196 return USB_DR_MODE_UNKNOWN;
197
198 return usb_get_dr_mode_from_string(dr_mode);
199}
200EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
201
202/**
203 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
204 * for given targeted hosts (non-PC hosts)
205 * @np: Pointer to the given device_node
206 *
207 * The function gets if the targeted hosts support TPL or not
208 */
209bool of_usb_host_tpl_support(struct device_node *np)
210{
211 return of_property_read_bool(np, "tpl-support");
212}
213EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
214
215/**
216 * of_usb_update_otg_caps - to update usb otg capabilities according to
217 * the passed properties in DT.
218 * @np: Pointer to the given device_node
219 * @otg_caps: Pointer to the target usb_otg_caps to be set
220 *
221 * The function updates the otg capabilities
222 */
223int of_usb_update_otg_caps(struct device_node *np,
224 struct usb_otg_caps *otg_caps)
225{
226 u32 otg_rev;
227
228 if (!otg_caps)
229 return -EINVAL;
230
231 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
232 switch (otg_rev) {
233 case 0x0100:
234 case 0x0120:
235 case 0x0130:
236 case 0x0200:
237 /* Choose the lesser one if it's already been set */
238 if (otg_caps->otg_rev)
239 otg_caps->otg_rev = min_t(u16, otg_rev,
240 otg_caps->otg_rev);
241 else
242 otg_caps->otg_rev = otg_rev;
243 break;
244 default:
245 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
246 np, otg_rev);
247 return -EINVAL;
248 }
249 } else {
250 /*
251 * otg-rev is mandatory for otg properties, if not passed
252 * we set it to be 0 and assume it's a legacy otg device.
253 * Non-dt platform can set it afterwards.
254 */
255 otg_caps->otg_rev = 0;
256 }
257
258 if (of_property_read_bool(np, "hnp-disable"))
259 otg_caps->hnp_support = false;
260 if (of_property_read_bool(np, "srp-disable"))
261 otg_caps->srp_support = false;
262 if (of_property_read_bool(np, "adp-disable") ||
263 (otg_caps->otg_rev < 0x0200))
264 otg_caps->adp_support = false;
265
266 return 0;
267}
268EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
269
270/**
271 * usb_of_get_companion_dev - Find the companion device
272 * @dev: the device pointer to find a companion
273 *
274 * Find the companion device from platform bus.
275 *
276 * Takes a reference to the returned struct device which needs to be dropped
277 * after use.
278 *
279 * Return: On success, a pointer to the companion device, %NULL on failure.
280 */
281struct device *usb_of_get_companion_dev(struct device *dev)
282{
283 struct device_node *node;
284 struct platform_device *pdev = NULL;
285
286 node = of_parse_phandle(dev->of_node, "companion", 0);
287 if (node)
288 pdev = of_find_device_by_node(node);
289
290 of_node_put(node);
291
292 return pdev ? &pdev->dev : NULL;
293}
294EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
295#endif
296
297struct dentry *usb_debug_root;
298EXPORT_SYMBOL_GPL(usb_debug_root);
299
300static int __init usb_common_init(void)
301{
302 usb_debug_root = debugfs_create_dir("usb", NULL);
303 ledtrig_usb_init();
304 return 0;
305}
306
307static void __exit usb_common_exit(void)
308{
309 ledtrig_usb_exit();
310 debugfs_remove_recursive(usb_debug_root);
311}
312
313subsys_initcall(usb_common_init);
314module_exit(usb_common_exit);
315
316MODULE_LICENSE("GPL");