xref: /openbmc/linux/drivers/usb/common/common.c (revision d9fd5a71)
1 // 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 
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",
26 };
27 
28 const 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 }
35 EXPORT_SYMBOL_GPL(usb_ep_type_string);
36 
37 const char *usb_otg_state_string(enum usb_otg_state state)
38 {
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",
53 	};
54 
55 	if (state < 0 || state >= ARRAY_SIZE(names))
56 		return "UNDEFINED";
57 
58 	return names[state];
59 }
60 EXPORT_SYMBOL_GPL(usb_otg_state_string);
61 
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",
70 };
71 
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",
77 };
78 
79 const char *usb_speed_string(enum usb_device_speed speed)
80 {
81 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
82 		speed = USB_SPEED_UNKNOWN;
83 	return speed_names[speed];
84 }
85 EXPORT_SYMBOL_GPL(usb_speed_string);
86 
87 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
88 {
89 	const char *maximum_speed;
90 	int ret;
91 
92 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
93 	if (ret < 0)
94 		return USB_SPEED_UNKNOWN;
95 
96 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
97 	if (ret > 0)
98 		return USB_SPEED_SUPER_PLUS;
99 
100 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
101 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
102 }
103 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
104 
105 enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
106 {
107 	const char *maximum_speed;
108 	int ret;
109 
110 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
111 	if (ret < 0)
112 		return USB_SSP_GEN_UNKNOWN;
113 
114 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
115 	return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
116 }
117 EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
118 
119 const char *usb_state_string(enum usb_device_state state)
120 {
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",
131 	};
132 
133 	if (state < 0 || state >= ARRAY_SIZE(names))
134 		return "UNKNOWN";
135 
136 	return names[state];
137 }
138 EXPORT_SYMBOL_GPL(usb_state_string);
139 
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",
145 };
146 
147 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
148 {
149 	int ret;
150 
151 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
152 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
153 }
154 
155 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
156 {
157 	const char *dr_mode;
158 	int err;
159 
160 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
161 	if (err < 0)
162 		return USB_DR_MODE_UNKNOWN;
163 
164 	return usb_get_dr_mode_from_string(dr_mode);
165 }
166 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
167 
168 #ifdef CONFIG_OF
169 /**
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
175  *
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.
179  */
180 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
181 {
182 	struct device_node *controller = NULL;
183 	struct of_phandle_args args;
184 	const char *dr_mode;
185 	int index;
186 	int err;
187 
188 	do {
189 		controller = of_find_node_with_property(controller, "phys");
190 		if (!of_device_is_available(controller))
191 			continue;
192 		index = 0;
193 		do {
194 			if (arg0 == -1) {
195 				args.np = of_parse_phandle(controller, "phys",
196 							index);
197 				args.args_count = 0;
198 			} else {
199 				err = of_parse_phandle_with_args(controller,
200 							"phys", "#phy-cells",
201 							index, &args);
202 				if (err)
203 					break;
204 			}
205 
206 			of_node_put(args.np);
207 			if (args.np == np && (args.args_count == 0 ||
208 					      args.args[0] == arg0))
209 				goto finish;
210 			index++;
211 		} while (args.np);
212 	} while (controller);
213 
214 finish:
215 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
216 	of_node_put(controller);
217 
218 	if (err < 0)
219 		return USB_DR_MODE_UNKNOWN;
220 
221 	return usb_get_dr_mode_from_string(dr_mode);
222 }
223 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
224 
225 /**
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
229  *
230  * The function gets if the targeted hosts support TPL or not
231  */
232 bool of_usb_host_tpl_support(struct device_node *np)
233 {
234 	return of_property_read_bool(np, "tpl-support");
235 }
236 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
237 
238 /**
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
243  *
244  * The function updates the otg capabilities
245  */
246 int of_usb_update_otg_caps(struct device_node *np,
247 			struct usb_otg_caps *otg_caps)
248 {
249 	u32 otg_rev;
250 
251 	if (!otg_caps)
252 		return -EINVAL;
253 
254 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
255 		switch (otg_rev) {
256 		case 0x0100:
257 		case 0x0120:
258 		case 0x0130:
259 		case 0x0200:
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,
263 							otg_caps->otg_rev);
264 			else
265 				otg_caps->otg_rev = otg_rev;
266 			break;
267 		default:
268 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
269 						np, otg_rev);
270 			return -EINVAL;
271 		}
272 	} else {
273 		/*
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.
277 		 */
278 		otg_caps->otg_rev = 0;
279 	}
280 
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;
288 
289 	return 0;
290 }
291 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
292 
293 /**
294  * usb_of_get_companion_dev - Find the companion device
295  * @dev: the device pointer to find a companion
296  *
297  * Find the companion device from platform bus.
298  *
299  * Takes a reference to the returned struct device which needs to be dropped
300  * after use.
301  *
302  * Return: On success, a pointer to the companion device, %NULL on failure.
303  */
304 struct device *usb_of_get_companion_dev(struct device *dev)
305 {
306 	struct device_node *node;
307 	struct platform_device *pdev = NULL;
308 
309 	node = of_parse_phandle(dev->of_node, "companion", 0);
310 	if (node)
311 		pdev = of_find_device_by_node(node);
312 
313 	of_node_put(node);
314 
315 	return pdev ? &pdev->dev : NULL;
316 }
317 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
318 #endif
319 
320 struct dentry *usb_debug_root;
321 EXPORT_SYMBOL_GPL(usb_debug_root);
322 
323 static int __init usb_common_init(void)
324 {
325 	usb_debug_root = debugfs_create_dir("usb", NULL);
326 	ledtrig_usb_init();
327 	return 0;
328 }
329 
330 static void __exit usb_common_exit(void)
331 {
332 	ledtrig_usb_exit();
333 	debugfs_remove_recursive(usb_debug_root);
334 }
335 
336 subsys_initcall(usb_common_init);
337 module_exit(usb_common_exit);
338 
339 MODULE_LICENSE("GPL");
340