xref: /openbmc/linux/drivers/usb/common/common.c (revision 238e0268c82789e4c107a37045d529a6dbce51a9)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2aa923ef1SMichal Sojka /*
3aa923ef1SMichal Sojka  * Provides code common for host and device side USB.
4aa923ef1SMichal Sojka  *
5aa923ef1SMichal Sojka  * If either host side (ie. CONFIG_USB=y) or device side USB stack
6aa923ef1SMichal Sojka  * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
7aa923ef1SMichal Sojka  * compiled-in as well.  Otherwise, if either of the two stacks is
8aa923ef1SMichal Sojka  * compiled as module, this file is compiled as module as well.
9aa923ef1SMichal Sojka  */
10aa923ef1SMichal Sojka 
11aa923ef1SMichal Sojka #include <linux/kernel.h>
12aa923ef1SMichal Sojka #include <linux/module.h>
13aa923ef1SMichal Sojka #include <linux/of.h>
14aa923ef1SMichal Sojka #include <linux/usb/ch9.h>
15aa923ef1SMichal Sojka #include <linux/usb/of.h>
16aa923ef1SMichal Sojka #include <linux/usb/otg.h>
1798bfb394SBin Liu #include <linux/of_platform.h>
18aa923ef1SMichal Sojka 
19aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state)
20aa923ef1SMichal Sojka {
21aa923ef1SMichal Sojka 	static const char *const names[] = {
22aa923ef1SMichal Sojka 		[OTG_STATE_A_IDLE] = "a_idle",
23aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
24aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
25aa923ef1SMichal Sojka 		[OTG_STATE_A_HOST] = "a_host",
26aa923ef1SMichal Sojka 		[OTG_STATE_A_SUSPEND] = "a_suspend",
27aa923ef1SMichal Sojka 		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
28aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
29aa923ef1SMichal Sojka 		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
30aa923ef1SMichal Sojka 		[OTG_STATE_B_IDLE] = "b_idle",
31aa923ef1SMichal Sojka 		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
32aa923ef1SMichal Sojka 		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
33aa923ef1SMichal Sojka 		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
34aa923ef1SMichal Sojka 		[OTG_STATE_B_HOST] = "b_host",
35aa923ef1SMichal Sojka 	};
36aa923ef1SMichal Sojka 
37aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
38aa923ef1SMichal Sojka 		return "UNDEFINED";
39aa923ef1SMichal Sojka 
40aa923ef1SMichal Sojka 	return names[state];
41aa923ef1SMichal Sojka }
42aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string);
43aa923ef1SMichal Sojka 
44aa923ef1SMichal Sojka static const char *const speed_names[] = {
45aa923ef1SMichal Sojka 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
46aa923ef1SMichal Sojka 	[USB_SPEED_LOW] = "low-speed",
47aa923ef1SMichal Sojka 	[USB_SPEED_FULL] = "full-speed",
48aa923ef1SMichal Sojka 	[USB_SPEED_HIGH] = "high-speed",
49aa923ef1SMichal Sojka 	[USB_SPEED_WIRELESS] = "wireless",
50aa923ef1SMichal Sojka 	[USB_SPEED_SUPER] = "super-speed",
518a1b2725SMathias Nyman 	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
52aa923ef1SMichal Sojka };
53aa923ef1SMichal Sojka 
54aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed)
55aa923ef1SMichal Sojka {
56aa923ef1SMichal Sojka 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
57aa923ef1SMichal Sojka 		speed = USB_SPEED_UNKNOWN;
58aa923ef1SMichal Sojka 	return speed_names[speed];
59aa923ef1SMichal Sojka }
60aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string);
61aa923ef1SMichal Sojka 
6263863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev)
6363863b98SHeikki Krogerus {
6463863b98SHeikki Krogerus 	const char *maximum_speed;
65efc2cd79SHeikki Krogerus 	int ret;
6663863b98SHeikki Krogerus 
67efc2cd79SHeikki Krogerus 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
68efc2cd79SHeikki Krogerus 	if (ret < 0)
6963863b98SHeikki Krogerus 		return USB_SPEED_UNKNOWN;
7063863b98SHeikki Krogerus 
71efc2cd79SHeikki Krogerus 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
7263863b98SHeikki Krogerus 
73efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
7463863b98SHeikki Krogerus }
7563863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
7663863b98SHeikki Krogerus 
77aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state)
78aa923ef1SMichal Sojka {
79aa923ef1SMichal Sojka 	static const char *const names[] = {
80aa923ef1SMichal Sojka 		[USB_STATE_NOTATTACHED] = "not attached",
81aa923ef1SMichal Sojka 		[USB_STATE_ATTACHED] = "attached",
82aa923ef1SMichal Sojka 		[USB_STATE_POWERED] = "powered",
83aa923ef1SMichal Sojka 		[USB_STATE_RECONNECTING] = "reconnecting",
84aa923ef1SMichal Sojka 		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
85aa923ef1SMichal Sojka 		[USB_STATE_DEFAULT] = "default",
86aa923ef1SMichal Sojka 		[USB_STATE_ADDRESS] = "addressed",
87aa923ef1SMichal Sojka 		[USB_STATE_CONFIGURED] = "configured",
88aa923ef1SMichal Sojka 		[USB_STATE_SUSPENDED] = "suspended",
89aa923ef1SMichal Sojka 	};
90aa923ef1SMichal Sojka 
91aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
92aa923ef1SMichal Sojka 		return "UNKNOWN";
93aa923ef1SMichal Sojka 
94aa923ef1SMichal Sojka 	return names[state];
95aa923ef1SMichal Sojka }
96aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string);
97aa923ef1SMichal Sojka 
98aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = {
99aa923ef1SMichal Sojka 	[USB_DR_MODE_UNKNOWN]		= "",
100aa923ef1SMichal Sojka 	[USB_DR_MODE_HOST]		= "host",
101aa923ef1SMichal Sojka 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
102aa923ef1SMichal Sojka 	[USB_DR_MODE_OTG]		= "otg",
103aa923ef1SMichal Sojka };
104aa923ef1SMichal Sojka 
10598bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
10698bfb394SBin Liu {
107efc2cd79SHeikki Krogerus 	int ret;
10898bfb394SBin Liu 
109efc2cd79SHeikki Krogerus 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
110efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
11198bfb394SBin Liu }
11298bfb394SBin Liu 
11306e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev)
114aa923ef1SMichal Sojka {
115aa923ef1SMichal Sojka 	const char *dr_mode;
11698bfb394SBin Liu 	int err;
117aa923ef1SMichal Sojka 
11806e7114fSHeikki Krogerus 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
119aa923ef1SMichal Sojka 	if (err < 0)
120aa923ef1SMichal Sojka 		return USB_DR_MODE_UNKNOWN;
121aa923ef1SMichal Sojka 
12298bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
123aa923ef1SMichal Sojka }
12406e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode);
125aa923ef1SMichal Sojka 
12606e7114fSHeikki Krogerus #ifdef CONFIG_OF
127aa923ef1SMichal Sojka /**
12898bfb394SBin Liu  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
12998bfb394SBin Liu  * which is associated with the given phy device_node
13098bfb394SBin Liu  * @np:	Pointer to the given phy device_node
131ce15ed4cSHans de Goede  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
132ce15ed4cSHans de Goede  *        phys which do not have phy-cells
13398bfb394SBin Liu  *
13498bfb394SBin Liu  * In dts a usb controller associates with phy devices.  The function gets
13598bfb394SBin Liu  * the string from property 'dr_mode' of the controller associated with the
13698bfb394SBin Liu  * given phy device node, and returns the correspondig enum usb_dr_mode.
13798bfb394SBin Liu  */
138ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
13998bfb394SBin Liu {
14098bfb394SBin Liu 	struct device_node *controller = NULL;
141ce15ed4cSHans de Goede 	struct of_phandle_args args;
14298bfb394SBin Liu 	const char *dr_mode;
14398bfb394SBin Liu 	int index;
14498bfb394SBin Liu 	int err;
14598bfb394SBin Liu 
14698bfb394SBin Liu 	do {
14798bfb394SBin Liu 		controller = of_find_node_with_property(controller, "phys");
148*238e0268SFabrizio Castro 		if (!of_device_is_available(controller))
149*238e0268SFabrizio Castro 			continue;
15098bfb394SBin Liu 		index = 0;
15198bfb394SBin Liu 		do {
152ce15ed4cSHans de Goede 			if (arg0 == -1) {
153ce15ed4cSHans de Goede 				args.np = of_parse_phandle(controller, "phys",
154ce15ed4cSHans de Goede 							index);
155ce15ed4cSHans de Goede 				args.args_count = 0;
156ce15ed4cSHans de Goede 			} else {
157ce15ed4cSHans de Goede 				err = of_parse_phandle_with_args(controller,
158ce15ed4cSHans de Goede 							"phys", "#phy-cells",
159ce15ed4cSHans de Goede 							index, &args);
160ce15ed4cSHans de Goede 				if (err)
161ce15ed4cSHans de Goede 					break;
162ce15ed4cSHans de Goede 			}
163ce15ed4cSHans de Goede 
164ce15ed4cSHans de Goede 			of_node_put(args.np);
165ce15ed4cSHans de Goede 			if (args.np == np && (args.args_count == 0 ||
166ce15ed4cSHans de Goede 					      args.args[0] == arg0))
16798bfb394SBin Liu 				goto finish;
16898bfb394SBin Liu 			index++;
169ce15ed4cSHans de Goede 		} while (args.np);
17098bfb394SBin Liu 	} while (controller);
17198bfb394SBin Liu 
17298bfb394SBin Liu finish:
17398bfb394SBin Liu 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
17498bfb394SBin Liu 	of_node_put(controller);
17598bfb394SBin Liu 
17698bfb394SBin Liu 	if (err < 0)
17798bfb394SBin Liu 		return USB_DR_MODE_UNKNOWN;
17898bfb394SBin Liu 
17998bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
18098bfb394SBin Liu }
18198bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
18298bfb394SBin Liu 
18398bfb394SBin Liu /**
184aa923ef1SMichal Sojka  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
185aa923ef1SMichal Sojka  * for given targeted hosts (non-PC hosts)
186aa923ef1SMichal Sojka  * @np: Pointer to the given device_node
187aa923ef1SMichal Sojka  *
188aa923ef1SMichal Sojka  * The function gets if the targeted hosts support TPL or not
189aa923ef1SMichal Sojka  */
190aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np)
191aa923ef1SMichal Sojka {
192334007d5SSergei Shtylyov 	return of_property_read_bool(np, "tpl-support");
193aa923ef1SMichal Sojka }
194aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
195929412d9SLi Jun 
196929412d9SLi Jun /**
197929412d9SLi Jun  * of_usb_update_otg_caps - to update usb otg capabilities according to
198929412d9SLi Jun  * the passed properties in DT.
199929412d9SLi Jun  * @np: Pointer to the given device_node
200929412d9SLi Jun  * @otg_caps: Pointer to the target usb_otg_caps to be set
201929412d9SLi Jun  *
202929412d9SLi Jun  * The function updates the otg capabilities
203929412d9SLi Jun  */
204929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np,
205929412d9SLi Jun 			struct usb_otg_caps *otg_caps)
206929412d9SLi Jun {
207929412d9SLi Jun 	u32 otg_rev;
208929412d9SLi Jun 
209929412d9SLi Jun 	if (!otg_caps)
210929412d9SLi Jun 		return -EINVAL;
211929412d9SLi Jun 
212929412d9SLi Jun 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
213929412d9SLi Jun 		switch (otg_rev) {
214929412d9SLi Jun 		case 0x0100:
215929412d9SLi Jun 		case 0x0120:
216929412d9SLi Jun 		case 0x0130:
217929412d9SLi Jun 		case 0x0200:
218929412d9SLi Jun 			/* Choose the lesser one if it's already been set */
219929412d9SLi Jun 			if (otg_caps->otg_rev)
220929412d9SLi Jun 				otg_caps->otg_rev = min_t(u16, otg_rev,
221929412d9SLi Jun 							otg_caps->otg_rev);
222929412d9SLi Jun 			else
223929412d9SLi Jun 				otg_caps->otg_rev = otg_rev;
224929412d9SLi Jun 			break;
225929412d9SLi Jun 		default:
226d9241ff2SRob Herring 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
227d9241ff2SRob Herring 						np, otg_rev);
228929412d9SLi Jun 			return -EINVAL;
229929412d9SLi Jun 		}
230929412d9SLi Jun 	} else {
231929412d9SLi Jun 		/*
232929412d9SLi Jun 		 * otg-rev is mandatory for otg properties, if not passed
233929412d9SLi Jun 		 * we set it to be 0 and assume it's a legacy otg device.
234929412d9SLi Jun 		 * Non-dt platform can set it afterwards.
235929412d9SLi Jun 		 */
236929412d9SLi Jun 		otg_caps->otg_rev = 0;
237929412d9SLi Jun 	}
238929412d9SLi Jun 
239334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "hnp-disable"))
240929412d9SLi Jun 		otg_caps->hnp_support = false;
241334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "srp-disable"))
242929412d9SLi Jun 		otg_caps->srp_support = false;
243334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "adp-disable") ||
244929412d9SLi Jun 				(otg_caps->otg_rev < 0x0200))
245929412d9SLi Jun 		otg_caps->adp_support = false;
246929412d9SLi Jun 
247929412d9SLi Jun 	return 0;
248929412d9SLi Jun }
249929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
250929412d9SLi Jun 
251fa827966SYoshihiro Shimoda /**
252fa827966SYoshihiro Shimoda  * usb_of_get_companion_dev - Find the companion device
253fa827966SYoshihiro Shimoda  * @dev: the device pointer to find a companion
254fa827966SYoshihiro Shimoda  *
255fa827966SYoshihiro Shimoda  * Find the companion device from platform bus.
256fa827966SYoshihiro Shimoda  *
257fa827966SYoshihiro Shimoda  * Takes a reference to the returned struct device which needs to be dropped
258fa827966SYoshihiro Shimoda  * after use.
259fa827966SYoshihiro Shimoda  *
260fa827966SYoshihiro Shimoda  * Return: On success, a pointer to the companion device, %NULL on failure.
261fa827966SYoshihiro Shimoda  */
262fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev)
263fa827966SYoshihiro Shimoda {
264fa827966SYoshihiro Shimoda 	struct device_node *node;
265fa827966SYoshihiro Shimoda 	struct platform_device *pdev = NULL;
266fa827966SYoshihiro Shimoda 
267fa827966SYoshihiro Shimoda 	node = of_parse_phandle(dev->of_node, "companion", 0);
268fa827966SYoshihiro Shimoda 	if (node)
269fa827966SYoshihiro Shimoda 		pdev = of_find_device_by_node(node);
270fa827966SYoshihiro Shimoda 
271fa827966SYoshihiro Shimoda 	of_node_put(node);
272fa827966SYoshihiro Shimoda 
273fa827966SYoshihiro Shimoda 	return pdev ? &pdev->dev : NULL;
274fa827966SYoshihiro Shimoda }
275fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
276aa923ef1SMichal Sojka #endif
277aa923ef1SMichal Sojka 
278aa923ef1SMichal Sojka MODULE_LICENSE("GPL");
279