xref: /openbmc/linux/drivers/usb/common/common.c (revision ba61bb17)
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 const char *usb_otg_state_string(enum usb_otg_state state)
20 {
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",
35 	};
36 
37 	if (state < 0 || state >= ARRAY_SIZE(names))
38 		return "UNDEFINED";
39 
40 	return names[state];
41 }
42 EXPORT_SYMBOL_GPL(usb_otg_state_string);
43 
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",
52 };
53 
54 const char *usb_speed_string(enum usb_device_speed speed)
55 {
56 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
57 		speed = USB_SPEED_UNKNOWN;
58 	return speed_names[speed];
59 }
60 EXPORT_SYMBOL_GPL(usb_speed_string);
61 
62 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
63 {
64 	const char *maximum_speed;
65 	int ret;
66 
67 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
68 	if (ret < 0)
69 		return USB_SPEED_UNKNOWN;
70 
71 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
72 
73 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
74 }
75 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
76 
77 const char *usb_state_string(enum usb_device_state state)
78 {
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",
89 	};
90 
91 	if (state < 0 || state >= ARRAY_SIZE(names))
92 		return "UNKNOWN";
93 
94 	return names[state];
95 }
96 EXPORT_SYMBOL_GPL(usb_state_string);
97 
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",
103 };
104 
105 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
106 {
107 	int ret;
108 
109 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
110 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
111 }
112 
113 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
114 {
115 	const char *dr_mode;
116 	int err;
117 
118 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
119 	if (err < 0)
120 		return USB_DR_MODE_UNKNOWN;
121 
122 	return usb_get_dr_mode_from_string(dr_mode);
123 }
124 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
125 
126 #ifdef CONFIG_OF
127 /**
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
133  *
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.
137  */
138 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
139 {
140 	struct device_node *controller = NULL;
141 	struct of_phandle_args args;
142 	const char *dr_mode;
143 	int index;
144 	int err;
145 
146 	do {
147 		controller = of_find_node_with_property(controller, "phys");
148 		index = 0;
149 		do {
150 			if (arg0 == -1) {
151 				args.np = of_parse_phandle(controller, "phys",
152 							index);
153 				args.args_count = 0;
154 			} else {
155 				err = of_parse_phandle_with_args(controller,
156 							"phys", "#phy-cells",
157 							index, &args);
158 				if (err)
159 					break;
160 			}
161 
162 			of_node_put(args.np);
163 			if (args.np == np && (args.args_count == 0 ||
164 					      args.args[0] == arg0))
165 				goto finish;
166 			index++;
167 		} while (args.np);
168 	} while (controller);
169 
170 finish:
171 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
172 	of_node_put(controller);
173 
174 	if (err < 0)
175 		return USB_DR_MODE_UNKNOWN;
176 
177 	return usb_get_dr_mode_from_string(dr_mode);
178 }
179 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
180 
181 /**
182  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
183  * for given targeted hosts (non-PC hosts)
184  * @np: Pointer to the given device_node
185  *
186  * The function gets if the targeted hosts support TPL or not
187  */
188 bool of_usb_host_tpl_support(struct device_node *np)
189 {
190 	return of_property_read_bool(np, "tpl-support");
191 }
192 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
193 
194 /**
195  * of_usb_update_otg_caps - to update usb otg capabilities according to
196  * the passed properties in DT.
197  * @np: Pointer to the given device_node
198  * @otg_caps: Pointer to the target usb_otg_caps to be set
199  *
200  * The function updates the otg capabilities
201  */
202 int of_usb_update_otg_caps(struct device_node *np,
203 			struct usb_otg_caps *otg_caps)
204 {
205 	u32 otg_rev;
206 
207 	if (!otg_caps)
208 		return -EINVAL;
209 
210 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
211 		switch (otg_rev) {
212 		case 0x0100:
213 		case 0x0120:
214 		case 0x0130:
215 		case 0x0200:
216 			/* Choose the lesser one if it's already been set */
217 			if (otg_caps->otg_rev)
218 				otg_caps->otg_rev = min_t(u16, otg_rev,
219 							otg_caps->otg_rev);
220 			else
221 				otg_caps->otg_rev = otg_rev;
222 			break;
223 		default:
224 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
225 						np, otg_rev);
226 			return -EINVAL;
227 		}
228 	} else {
229 		/*
230 		 * otg-rev is mandatory for otg properties, if not passed
231 		 * we set it to be 0 and assume it's a legacy otg device.
232 		 * Non-dt platform can set it afterwards.
233 		 */
234 		otg_caps->otg_rev = 0;
235 	}
236 
237 	if (of_property_read_bool(np, "hnp-disable"))
238 		otg_caps->hnp_support = false;
239 	if (of_property_read_bool(np, "srp-disable"))
240 		otg_caps->srp_support = false;
241 	if (of_property_read_bool(np, "adp-disable") ||
242 				(otg_caps->otg_rev < 0x0200))
243 		otg_caps->adp_support = false;
244 
245 	return 0;
246 }
247 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
248 
249 #endif
250 
251 MODULE_LICENSE("GPL");
252