xref: /openbmc/linux/drivers/usb/common/common.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1*5fd54aceSGreg 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  * This program is free software; you can redistribute it and/or
6aa923ef1SMichal Sojka  * modify it under the terms of the GNU General Public License as
7aa923ef1SMichal Sojka  * published by the Free Software Foundation, version 2.
8aa923ef1SMichal Sojka  *
9aa923ef1SMichal Sojka  * If either host side (ie. CONFIG_USB=y) or device side USB stack
10aa923ef1SMichal Sojka  * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
11aa923ef1SMichal Sojka  * compiled-in as well.  Otherwise, if either of the two stacks is
12aa923ef1SMichal Sojka  * compiled as module, this file is compiled as module as well.
13aa923ef1SMichal Sojka  */
14aa923ef1SMichal Sojka 
15aa923ef1SMichal Sojka #include <linux/kernel.h>
16aa923ef1SMichal Sojka #include <linux/module.h>
17aa923ef1SMichal Sojka #include <linux/of.h>
18aa923ef1SMichal Sojka #include <linux/usb/ch9.h>
19aa923ef1SMichal Sojka #include <linux/usb/of.h>
20aa923ef1SMichal Sojka #include <linux/usb/otg.h>
2198bfb394SBin Liu #include <linux/of_platform.h>
22aa923ef1SMichal Sojka 
23aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state)
24aa923ef1SMichal Sojka {
25aa923ef1SMichal Sojka 	static const char *const names[] = {
26aa923ef1SMichal Sojka 		[OTG_STATE_A_IDLE] = "a_idle",
27aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
28aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
29aa923ef1SMichal Sojka 		[OTG_STATE_A_HOST] = "a_host",
30aa923ef1SMichal Sojka 		[OTG_STATE_A_SUSPEND] = "a_suspend",
31aa923ef1SMichal Sojka 		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
32aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
33aa923ef1SMichal Sojka 		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
34aa923ef1SMichal Sojka 		[OTG_STATE_B_IDLE] = "b_idle",
35aa923ef1SMichal Sojka 		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
36aa923ef1SMichal Sojka 		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
37aa923ef1SMichal Sojka 		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
38aa923ef1SMichal Sojka 		[OTG_STATE_B_HOST] = "b_host",
39aa923ef1SMichal Sojka 	};
40aa923ef1SMichal Sojka 
41aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
42aa923ef1SMichal Sojka 		return "UNDEFINED";
43aa923ef1SMichal Sojka 
44aa923ef1SMichal Sojka 	return names[state];
45aa923ef1SMichal Sojka }
46aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string);
47aa923ef1SMichal Sojka 
48aa923ef1SMichal Sojka static const char *const speed_names[] = {
49aa923ef1SMichal Sojka 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
50aa923ef1SMichal Sojka 	[USB_SPEED_LOW] = "low-speed",
51aa923ef1SMichal Sojka 	[USB_SPEED_FULL] = "full-speed",
52aa923ef1SMichal Sojka 	[USB_SPEED_HIGH] = "high-speed",
53aa923ef1SMichal Sojka 	[USB_SPEED_WIRELESS] = "wireless",
54aa923ef1SMichal Sojka 	[USB_SPEED_SUPER] = "super-speed",
558a1b2725SMathias Nyman 	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
56aa923ef1SMichal Sojka };
57aa923ef1SMichal Sojka 
58aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed)
59aa923ef1SMichal Sojka {
60aa923ef1SMichal Sojka 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
61aa923ef1SMichal Sojka 		speed = USB_SPEED_UNKNOWN;
62aa923ef1SMichal Sojka 	return speed_names[speed];
63aa923ef1SMichal Sojka }
64aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string);
65aa923ef1SMichal Sojka 
6663863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev)
6763863b98SHeikki Krogerus {
6863863b98SHeikki Krogerus 	const char *maximum_speed;
69efc2cd79SHeikki Krogerus 	int ret;
7063863b98SHeikki Krogerus 
71efc2cd79SHeikki Krogerus 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
72efc2cd79SHeikki Krogerus 	if (ret < 0)
7363863b98SHeikki Krogerus 		return USB_SPEED_UNKNOWN;
7463863b98SHeikki Krogerus 
75efc2cd79SHeikki Krogerus 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
7663863b98SHeikki Krogerus 
77efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
7863863b98SHeikki Krogerus }
7963863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
8063863b98SHeikki Krogerus 
81aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state)
82aa923ef1SMichal Sojka {
83aa923ef1SMichal Sojka 	static const char *const names[] = {
84aa923ef1SMichal Sojka 		[USB_STATE_NOTATTACHED] = "not attached",
85aa923ef1SMichal Sojka 		[USB_STATE_ATTACHED] = "attached",
86aa923ef1SMichal Sojka 		[USB_STATE_POWERED] = "powered",
87aa923ef1SMichal Sojka 		[USB_STATE_RECONNECTING] = "reconnecting",
88aa923ef1SMichal Sojka 		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
89aa923ef1SMichal Sojka 		[USB_STATE_DEFAULT] = "default",
90aa923ef1SMichal Sojka 		[USB_STATE_ADDRESS] = "addressed",
91aa923ef1SMichal Sojka 		[USB_STATE_CONFIGURED] = "configured",
92aa923ef1SMichal Sojka 		[USB_STATE_SUSPENDED] = "suspended",
93aa923ef1SMichal Sojka 	};
94aa923ef1SMichal Sojka 
95aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
96aa923ef1SMichal Sojka 		return "UNKNOWN";
97aa923ef1SMichal Sojka 
98aa923ef1SMichal Sojka 	return names[state];
99aa923ef1SMichal Sojka }
100aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string);
101aa923ef1SMichal Sojka 
102aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = {
103aa923ef1SMichal Sojka 	[USB_DR_MODE_UNKNOWN]		= "",
104aa923ef1SMichal Sojka 	[USB_DR_MODE_HOST]		= "host",
105aa923ef1SMichal Sojka 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
106aa923ef1SMichal Sojka 	[USB_DR_MODE_OTG]		= "otg",
107aa923ef1SMichal Sojka };
108aa923ef1SMichal Sojka 
10998bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
11098bfb394SBin Liu {
111efc2cd79SHeikki Krogerus 	int ret;
11298bfb394SBin Liu 
113efc2cd79SHeikki Krogerus 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
114efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
11598bfb394SBin Liu }
11698bfb394SBin Liu 
11706e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev)
118aa923ef1SMichal Sojka {
119aa923ef1SMichal Sojka 	const char *dr_mode;
12098bfb394SBin Liu 	int err;
121aa923ef1SMichal Sojka 
12206e7114fSHeikki Krogerus 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
123aa923ef1SMichal Sojka 	if (err < 0)
124aa923ef1SMichal Sojka 		return USB_DR_MODE_UNKNOWN;
125aa923ef1SMichal Sojka 
12698bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
127aa923ef1SMichal Sojka }
12806e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode);
129aa923ef1SMichal Sojka 
13006e7114fSHeikki Krogerus #ifdef CONFIG_OF
131aa923ef1SMichal Sojka /**
13298bfb394SBin Liu  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
13398bfb394SBin Liu  * which is associated with the given phy device_node
13498bfb394SBin Liu  * @np:	Pointer to the given phy device_node
135ce15ed4cSHans de Goede  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
136ce15ed4cSHans de Goede  *        phys which do not have phy-cells
13798bfb394SBin Liu  *
13898bfb394SBin Liu  * In dts a usb controller associates with phy devices.  The function gets
13998bfb394SBin Liu  * the string from property 'dr_mode' of the controller associated with the
14098bfb394SBin Liu  * given phy device node, and returns the correspondig enum usb_dr_mode.
14198bfb394SBin Liu  */
142ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
14398bfb394SBin Liu {
14498bfb394SBin Liu 	struct device_node *controller = NULL;
145ce15ed4cSHans de Goede 	struct of_phandle_args args;
14698bfb394SBin Liu 	const char *dr_mode;
14798bfb394SBin Liu 	int index;
14898bfb394SBin Liu 	int err;
14998bfb394SBin Liu 
15098bfb394SBin Liu 	do {
15198bfb394SBin Liu 		controller = of_find_node_with_property(controller, "phys");
15298bfb394SBin Liu 		index = 0;
15398bfb394SBin Liu 		do {
154ce15ed4cSHans de Goede 			if (arg0 == -1) {
155ce15ed4cSHans de Goede 				args.np = of_parse_phandle(controller, "phys",
156ce15ed4cSHans de Goede 							index);
157ce15ed4cSHans de Goede 				args.args_count = 0;
158ce15ed4cSHans de Goede 			} else {
159ce15ed4cSHans de Goede 				err = of_parse_phandle_with_args(controller,
160ce15ed4cSHans de Goede 							"phys", "#phy-cells",
161ce15ed4cSHans de Goede 							index, &args);
162ce15ed4cSHans de Goede 				if (err)
163ce15ed4cSHans de Goede 					break;
164ce15ed4cSHans de Goede 			}
165ce15ed4cSHans de Goede 
166ce15ed4cSHans de Goede 			of_node_put(args.np);
167ce15ed4cSHans de Goede 			if (args.np == np && (args.args_count == 0 ||
168ce15ed4cSHans de Goede 					      args.args[0] == arg0))
16998bfb394SBin Liu 				goto finish;
17098bfb394SBin Liu 			index++;
171ce15ed4cSHans de Goede 		} while (args.np);
17298bfb394SBin Liu 	} while (controller);
17398bfb394SBin Liu 
17498bfb394SBin Liu finish:
17598bfb394SBin Liu 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
17698bfb394SBin Liu 	of_node_put(controller);
17798bfb394SBin Liu 
17898bfb394SBin Liu 	if (err < 0)
17998bfb394SBin Liu 		return USB_DR_MODE_UNKNOWN;
18098bfb394SBin Liu 
18198bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
18298bfb394SBin Liu }
18398bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
18498bfb394SBin Liu 
18598bfb394SBin Liu /**
186aa923ef1SMichal Sojka  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
187aa923ef1SMichal Sojka  * for given targeted hosts (non-PC hosts)
188aa923ef1SMichal Sojka  * @np: Pointer to the given device_node
189aa923ef1SMichal Sojka  *
190aa923ef1SMichal Sojka  * The function gets if the targeted hosts support TPL or not
191aa923ef1SMichal Sojka  */
192aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np)
193aa923ef1SMichal Sojka {
194334007d5SSergei Shtylyov 	return of_property_read_bool(np, "tpl-support");
195aa923ef1SMichal Sojka }
196aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
197929412d9SLi Jun 
198929412d9SLi Jun /**
199929412d9SLi Jun  * of_usb_update_otg_caps - to update usb otg capabilities according to
200929412d9SLi Jun  * the passed properties in DT.
201929412d9SLi Jun  * @np: Pointer to the given device_node
202929412d9SLi Jun  * @otg_caps: Pointer to the target usb_otg_caps to be set
203929412d9SLi Jun  *
204929412d9SLi Jun  * The function updates the otg capabilities
205929412d9SLi Jun  */
206929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np,
207929412d9SLi Jun 			struct usb_otg_caps *otg_caps)
208929412d9SLi Jun {
209929412d9SLi Jun 	u32 otg_rev;
210929412d9SLi Jun 
211929412d9SLi Jun 	if (!otg_caps)
212929412d9SLi Jun 		return -EINVAL;
213929412d9SLi Jun 
214929412d9SLi Jun 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
215929412d9SLi Jun 		switch (otg_rev) {
216929412d9SLi Jun 		case 0x0100:
217929412d9SLi Jun 		case 0x0120:
218929412d9SLi Jun 		case 0x0130:
219929412d9SLi Jun 		case 0x0200:
220929412d9SLi Jun 			/* Choose the lesser one if it's already been set */
221929412d9SLi Jun 			if (otg_caps->otg_rev)
222929412d9SLi Jun 				otg_caps->otg_rev = min_t(u16, otg_rev,
223929412d9SLi Jun 							otg_caps->otg_rev);
224929412d9SLi Jun 			else
225929412d9SLi Jun 				otg_caps->otg_rev = otg_rev;
226929412d9SLi Jun 			break;
227929412d9SLi Jun 		default:
228d9241ff2SRob Herring 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
229d9241ff2SRob Herring 						np, otg_rev);
230929412d9SLi Jun 			return -EINVAL;
231929412d9SLi Jun 		}
232929412d9SLi Jun 	} else {
233929412d9SLi Jun 		/*
234929412d9SLi Jun 		 * otg-rev is mandatory for otg properties, if not passed
235929412d9SLi Jun 		 * we set it to be 0 and assume it's a legacy otg device.
236929412d9SLi Jun 		 * Non-dt platform can set it afterwards.
237929412d9SLi Jun 		 */
238929412d9SLi Jun 		otg_caps->otg_rev = 0;
239929412d9SLi Jun 	}
240929412d9SLi Jun 
241334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "hnp-disable"))
242929412d9SLi Jun 		otg_caps->hnp_support = false;
243334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "srp-disable"))
244929412d9SLi Jun 		otg_caps->srp_support = false;
245334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "adp-disable") ||
246929412d9SLi Jun 				(otg_caps->otg_rev < 0x0200))
247929412d9SLi Jun 		otg_caps->adp_support = false;
248929412d9SLi Jun 
249929412d9SLi Jun 	return 0;
250929412d9SLi Jun }
251929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
252929412d9SLi Jun 
253aa923ef1SMichal Sojka #endif
254aa923ef1SMichal Sojka 
255aa923ef1SMichal Sojka MODULE_LICENSE("GPL");
256