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