xref: /linux/drivers/usb/common/common.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Provides code common for host and device side USB.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation, version 2.
8  *
9  * If either host side (ie. CONFIG_USB=y) or device side USB stack
10  * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
11  * compiled-in as well.  Otherwise, if either of the two stacks is
12  * compiled as module, this file is compiled as module as well.
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/of.h>
20 #include <linux/usb/otg.h>
21 #include <linux/of_platform.h>
22 
23 const char *usb_otg_state_string(enum usb_otg_state state)
24 {
25 	static const char *const names[] = {
26 		[OTG_STATE_A_IDLE] = "a_idle",
27 		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
28 		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
29 		[OTG_STATE_A_HOST] = "a_host",
30 		[OTG_STATE_A_SUSPEND] = "a_suspend",
31 		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
32 		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
33 		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
34 		[OTG_STATE_B_IDLE] = "b_idle",
35 		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
36 		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
37 		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
38 		[OTG_STATE_B_HOST] = "b_host",
39 	};
40 
41 	if (state < 0 || state >= ARRAY_SIZE(names))
42 		return "UNDEFINED";
43 
44 	return names[state];
45 }
46 EXPORT_SYMBOL_GPL(usb_otg_state_string);
47 
48 static const char *const speed_names[] = {
49 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
50 	[USB_SPEED_LOW] = "low-speed",
51 	[USB_SPEED_FULL] = "full-speed",
52 	[USB_SPEED_HIGH] = "high-speed",
53 	[USB_SPEED_WIRELESS] = "wireless",
54 	[USB_SPEED_SUPER] = "super-speed",
55 	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
56 };
57 
58 const char *usb_speed_string(enum usb_device_speed speed)
59 {
60 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
61 		speed = USB_SPEED_UNKNOWN;
62 	return speed_names[speed];
63 }
64 EXPORT_SYMBOL_GPL(usb_speed_string);
65 
66 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
67 {
68 	const char *maximum_speed;
69 	int ret;
70 
71 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
72 	if (ret < 0)
73 		return USB_SPEED_UNKNOWN;
74 
75 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
76 
77 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
78 }
79 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
80 
81 const char *usb_state_string(enum usb_device_state state)
82 {
83 	static const char *const names[] = {
84 		[USB_STATE_NOTATTACHED] = "not attached",
85 		[USB_STATE_ATTACHED] = "attached",
86 		[USB_STATE_POWERED] = "powered",
87 		[USB_STATE_RECONNECTING] = "reconnecting",
88 		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
89 		[USB_STATE_DEFAULT] = "default",
90 		[USB_STATE_ADDRESS] = "addressed",
91 		[USB_STATE_CONFIGURED] = "configured",
92 		[USB_STATE_SUSPENDED] = "suspended",
93 	};
94 
95 	if (state < 0 || state >= ARRAY_SIZE(names))
96 		return "UNKNOWN";
97 
98 	return names[state];
99 }
100 EXPORT_SYMBOL_GPL(usb_state_string);
101 
102 static const char *const usb_dr_modes[] = {
103 	[USB_DR_MODE_UNKNOWN]		= "",
104 	[USB_DR_MODE_HOST]		= "host",
105 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
106 	[USB_DR_MODE_OTG]		= "otg",
107 };
108 
109 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
110 {
111 	int ret;
112 
113 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
114 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
115 }
116 
117 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
118 {
119 	const char *dr_mode;
120 	int err;
121 
122 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
123 	if (err < 0)
124 		return USB_DR_MODE_UNKNOWN;
125 
126 	return usb_get_dr_mode_from_string(dr_mode);
127 }
128 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
129 
130 #ifdef CONFIG_OF
131 /**
132  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
133  * which is associated with the given phy device_node
134  * @np:	Pointer to the given phy device_node
135  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
136  *        phys which do not have phy-cells
137  *
138  * In dts a usb controller associates with phy devices.  The function gets
139  * the string from property 'dr_mode' of the controller associated with the
140  * given phy device node, and returns the correspondig enum usb_dr_mode.
141  */
142 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
143 {
144 	struct device_node *controller = NULL;
145 	struct of_phandle_args args;
146 	const char *dr_mode;
147 	int index;
148 	int err;
149 
150 	do {
151 		controller = of_find_node_with_property(controller, "phys");
152 		index = 0;
153 		do {
154 			if (arg0 == -1) {
155 				args.np = of_parse_phandle(controller, "phys",
156 							index);
157 				args.args_count = 0;
158 			} else {
159 				err = of_parse_phandle_with_args(controller,
160 							"phys", "#phy-cells",
161 							index, &args);
162 				if (err)
163 					break;
164 			}
165 
166 			of_node_put(args.np);
167 			if (args.np == np && (args.args_count == 0 ||
168 					      args.args[0] == arg0))
169 				goto finish;
170 			index++;
171 		} while (args.np);
172 	} while (controller);
173 
174 finish:
175 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
176 	of_node_put(controller);
177 
178 	if (err < 0)
179 		return USB_DR_MODE_UNKNOWN;
180 
181 	return usb_get_dr_mode_from_string(dr_mode);
182 }
183 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
184 
185 /**
186  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
187  * for given targeted hosts (non-PC hosts)
188  * @np: Pointer to the given device_node
189  *
190  * The function gets if the targeted hosts support TPL or not
191  */
192 bool of_usb_host_tpl_support(struct device_node *np)
193 {
194 	return of_property_read_bool(np, "tpl-support");
195 }
196 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
197 
198 /**
199  * of_usb_update_otg_caps - to update usb otg capabilities according to
200  * the passed properties in DT.
201  * @np: Pointer to the given device_node
202  * @otg_caps: Pointer to the target usb_otg_caps to be set
203  *
204  * The function updates the otg capabilities
205  */
206 int of_usb_update_otg_caps(struct device_node *np,
207 			struct usb_otg_caps *otg_caps)
208 {
209 	u32 otg_rev;
210 
211 	if (!otg_caps)
212 		return -EINVAL;
213 
214 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
215 		switch (otg_rev) {
216 		case 0x0100:
217 		case 0x0120:
218 		case 0x0130:
219 		case 0x0200:
220 			/* Choose the lesser one if it's already been set */
221 			if (otg_caps->otg_rev)
222 				otg_caps->otg_rev = min_t(u16, otg_rev,
223 							otg_caps->otg_rev);
224 			else
225 				otg_caps->otg_rev = otg_rev;
226 			break;
227 		default:
228 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
229 						np, otg_rev);
230 			return -EINVAL;
231 		}
232 	} else {
233 		/*
234 		 * otg-rev is mandatory for otg properties, if not passed
235 		 * we set it to be 0 and assume it's a legacy otg device.
236 		 * Non-dt platform can set it afterwards.
237 		 */
238 		otg_caps->otg_rev = 0;
239 	}
240 
241 	if (of_property_read_bool(np, "hnp-disable"))
242 		otg_caps->hnp_support = false;
243 	if (of_property_read_bool(np, "srp-disable"))
244 		otg_caps->srp_support = false;
245 	if (of_property_read_bool(np, "adp-disable") ||
246 				(otg_caps->otg_rev < 0x0200))
247 		otg_caps->adp_support = false;
248 
249 	return 0;
250 }
251 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
252 
253 #endif
254 
255 MODULE_LICENSE("GPL");
256