xref: /openbmc/linux/drivers/usb/common/common.c (revision 365038f24b3e9d2b7c9e499f03f432040e28a35c)
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>
18812086d3SGreg Kroah-Hartman #include <linux/debugfs.h>
19812086d3SGreg Kroah-Hartman #include "common.h"
20aa923ef1SMichal Sojka 
214d537f37SChunfeng Yun static const char *const ep_type_names[] = {
224d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_CONTROL] = "ctrl",
234d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_ISOC] = "isoc",
244d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_BULK] = "bulk",
254d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_INT] = "intr",
264d537f37SChunfeng Yun };
274d537f37SChunfeng Yun 
28*365038f2SChunfeng Yun /**
29*365038f2SChunfeng Yun  * usb_ep_type_string() - Returns human readable-name of the endpoint type.
30*365038f2SChunfeng Yun  * @ep_type: The endpoint type to return human-readable name for.  If it's not
31*365038f2SChunfeng Yun  *   any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
32*365038f2SChunfeng Yun  *   usually got by usb_endpoint_type(), the string 'unknown' will be returned.
33*365038f2SChunfeng Yun  */
344d537f37SChunfeng Yun const char *usb_ep_type_string(int ep_type)
354d537f37SChunfeng Yun {
364d537f37SChunfeng Yun 	if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
374d537f37SChunfeng Yun 		return "unknown";
384d537f37SChunfeng Yun 
394d537f37SChunfeng Yun 	return ep_type_names[ep_type];
404d537f37SChunfeng Yun }
414d537f37SChunfeng Yun EXPORT_SYMBOL_GPL(usb_ep_type_string);
424d537f37SChunfeng Yun 
43aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state)
44aa923ef1SMichal Sojka {
45aa923ef1SMichal Sojka 	static const char *const names[] = {
46aa923ef1SMichal Sojka 		[OTG_STATE_A_IDLE] = "a_idle",
47aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
48aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
49aa923ef1SMichal Sojka 		[OTG_STATE_A_HOST] = "a_host",
50aa923ef1SMichal Sojka 		[OTG_STATE_A_SUSPEND] = "a_suspend",
51aa923ef1SMichal Sojka 		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
52aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
53aa923ef1SMichal Sojka 		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
54aa923ef1SMichal Sojka 		[OTG_STATE_B_IDLE] = "b_idle",
55aa923ef1SMichal Sojka 		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
56aa923ef1SMichal Sojka 		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
57aa923ef1SMichal Sojka 		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
58aa923ef1SMichal Sojka 		[OTG_STATE_B_HOST] = "b_host",
59aa923ef1SMichal Sojka 	};
60aa923ef1SMichal Sojka 
61aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
62aa923ef1SMichal Sojka 		return "UNDEFINED";
63aa923ef1SMichal Sojka 
64aa923ef1SMichal Sojka 	return names[state];
65aa923ef1SMichal Sojka }
66aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string);
67aa923ef1SMichal Sojka 
68aa923ef1SMichal Sojka static const char *const speed_names[] = {
69aa923ef1SMichal Sojka 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
70aa923ef1SMichal Sojka 	[USB_SPEED_LOW] = "low-speed",
71aa923ef1SMichal Sojka 	[USB_SPEED_FULL] = "full-speed",
72aa923ef1SMichal Sojka 	[USB_SPEED_HIGH] = "high-speed",
73aa923ef1SMichal Sojka 	[USB_SPEED_WIRELESS] = "wireless",
74aa923ef1SMichal Sojka 	[USB_SPEED_SUPER] = "super-speed",
758a1b2725SMathias Nyman 	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
76aa923ef1SMichal Sojka };
77aa923ef1SMichal Sojka 
7852c2d157SThinh Nguyen static const char *const ssp_rate[] = {
7952c2d157SThinh Nguyen 	[USB_SSP_GEN_UNKNOWN] = "UNKNOWN",
8052c2d157SThinh Nguyen 	[USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1",
8152c2d157SThinh Nguyen 	[USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2",
8252c2d157SThinh Nguyen 	[USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2",
8352c2d157SThinh Nguyen };
8452c2d157SThinh Nguyen 
85*365038f2SChunfeng Yun /**
86*365038f2SChunfeng Yun  * usb_speed_string() - Returns human readable-name of the speed.
87*365038f2SChunfeng Yun  * @speed: The speed to return human-readable name for.  If it's not
88*365038f2SChunfeng Yun  *   any of the speeds defined in usb_device_speed enum, string for
89*365038f2SChunfeng Yun  *   USB_SPEED_UNKNOWN will be returned.
90*365038f2SChunfeng Yun  */
91aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed)
92aa923ef1SMichal Sojka {
93aa923ef1SMichal Sojka 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
94aa923ef1SMichal Sojka 		speed = USB_SPEED_UNKNOWN;
95aa923ef1SMichal Sojka 	return speed_names[speed];
96aa923ef1SMichal Sojka }
97aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string);
98aa923ef1SMichal Sojka 
99*365038f2SChunfeng Yun /**
100*365038f2SChunfeng Yun  * usb_get_maximum_speed - Get maximum requested speed for a given USB
101*365038f2SChunfeng Yun  * controller.
102*365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
103*365038f2SChunfeng Yun  *
104*365038f2SChunfeng Yun  * The function gets the maximum speed string from property "maximum-speed",
105*365038f2SChunfeng Yun  * and returns the corresponding enum usb_device_speed.
106*365038f2SChunfeng Yun  */
10763863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev)
10863863b98SHeikki Krogerus {
10963863b98SHeikki Krogerus 	const char *maximum_speed;
110efc2cd79SHeikki Krogerus 	int ret;
11163863b98SHeikki Krogerus 
112efc2cd79SHeikki Krogerus 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
113efc2cd79SHeikki Krogerus 	if (ret < 0)
11463863b98SHeikki Krogerus 		return USB_SPEED_UNKNOWN;
11563863b98SHeikki Krogerus 
11652c2d157SThinh Nguyen 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
11752c2d157SThinh Nguyen 	if (ret > 0)
11852c2d157SThinh Nguyen 		return USB_SPEED_SUPER_PLUS;
11963863b98SHeikki Krogerus 
12052c2d157SThinh Nguyen 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
121efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
12263863b98SHeikki Krogerus }
12363863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
12463863b98SHeikki Krogerus 
125*365038f2SChunfeng Yun /**
126*365038f2SChunfeng Yun  * usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count
127*365038f2SChunfeng Yun  *	of a SuperSpeed Plus capable device.
128*365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
129*365038f2SChunfeng Yun  *
130*365038f2SChunfeng Yun  * If the string from "maximum-speed" property is super-speed-plus-genXxY where
131*365038f2SChunfeng Yun  * 'X' is the generation number and 'Y' is the number of lanes, then this
132*365038f2SChunfeng Yun  * function returns the corresponding enum usb_ssp_rate.
133*365038f2SChunfeng Yun  */
13452c2d157SThinh Nguyen enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
13552c2d157SThinh Nguyen {
13652c2d157SThinh Nguyen 	const char *maximum_speed;
13752c2d157SThinh Nguyen 	int ret;
13852c2d157SThinh Nguyen 
13952c2d157SThinh Nguyen 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
14052c2d157SThinh Nguyen 	if (ret < 0)
14152c2d157SThinh Nguyen 		return USB_SSP_GEN_UNKNOWN;
14252c2d157SThinh Nguyen 
14352c2d157SThinh Nguyen 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
14452c2d157SThinh Nguyen 	return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
14552c2d157SThinh Nguyen }
14652c2d157SThinh Nguyen EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
14752c2d157SThinh Nguyen 
148*365038f2SChunfeng Yun /**
149*365038f2SChunfeng Yun  * usb_state_string - Returns human readable name for the state.
150*365038f2SChunfeng Yun  * @state: The state to return a human-readable name for. If it's not
151*365038f2SChunfeng Yun  *	any of the states devices in usb_device_state_string enum,
152*365038f2SChunfeng Yun  *	the string UNKNOWN will be returned.
153*365038f2SChunfeng Yun  */
154aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state)
155aa923ef1SMichal Sojka {
156aa923ef1SMichal Sojka 	static const char *const names[] = {
157aa923ef1SMichal Sojka 		[USB_STATE_NOTATTACHED] = "not attached",
158aa923ef1SMichal Sojka 		[USB_STATE_ATTACHED] = "attached",
159aa923ef1SMichal Sojka 		[USB_STATE_POWERED] = "powered",
160aa923ef1SMichal Sojka 		[USB_STATE_RECONNECTING] = "reconnecting",
161aa923ef1SMichal Sojka 		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
162aa923ef1SMichal Sojka 		[USB_STATE_DEFAULT] = "default",
163aa923ef1SMichal Sojka 		[USB_STATE_ADDRESS] = "addressed",
164aa923ef1SMichal Sojka 		[USB_STATE_CONFIGURED] = "configured",
165aa923ef1SMichal Sojka 		[USB_STATE_SUSPENDED] = "suspended",
166aa923ef1SMichal Sojka 	};
167aa923ef1SMichal Sojka 
168aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
169aa923ef1SMichal Sojka 		return "UNKNOWN";
170aa923ef1SMichal Sojka 
171aa923ef1SMichal Sojka 	return names[state];
172aa923ef1SMichal Sojka }
173aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string);
174aa923ef1SMichal Sojka 
175aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = {
176aa923ef1SMichal Sojka 	[USB_DR_MODE_UNKNOWN]		= "",
177aa923ef1SMichal Sojka 	[USB_DR_MODE_HOST]		= "host",
178aa923ef1SMichal Sojka 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
179aa923ef1SMichal Sojka 	[USB_DR_MODE_OTG]		= "otg",
180aa923ef1SMichal Sojka };
181aa923ef1SMichal Sojka 
18298bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
18398bfb394SBin Liu {
184efc2cd79SHeikki Krogerus 	int ret;
18598bfb394SBin Liu 
186efc2cd79SHeikki Krogerus 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
187efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
18898bfb394SBin Liu }
18998bfb394SBin Liu 
19006e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev)
191aa923ef1SMichal Sojka {
192aa923ef1SMichal Sojka 	const char *dr_mode;
19398bfb394SBin Liu 	int err;
194aa923ef1SMichal Sojka 
19506e7114fSHeikki Krogerus 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
196aa923ef1SMichal Sojka 	if (err < 0)
197aa923ef1SMichal Sojka 		return USB_DR_MODE_UNKNOWN;
198aa923ef1SMichal Sojka 
19998bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
200aa923ef1SMichal Sojka }
20106e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode);
202aa923ef1SMichal Sojka 
203fb95c7cfSChunfeng Yun /**
204fb95c7cfSChunfeng Yun  * usb_decode_interval - Decode bInterval into the time expressed in 1us unit
205fb95c7cfSChunfeng Yun  * @epd: The descriptor of the endpoint
206fb95c7cfSChunfeng Yun  * @speed: The speed that the endpoint works as
207fb95c7cfSChunfeng Yun  *
208fb95c7cfSChunfeng Yun  * Function returns the interval expressed in 1us unit for servicing
209fb95c7cfSChunfeng Yun  * endpoint for data transfers.
210fb95c7cfSChunfeng Yun  */
211fb95c7cfSChunfeng Yun unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
212fb95c7cfSChunfeng Yun 				 enum usb_device_speed speed)
213fb95c7cfSChunfeng Yun {
214fb95c7cfSChunfeng Yun 	unsigned int interval = 0;
215fb95c7cfSChunfeng Yun 
216fb95c7cfSChunfeng Yun 	switch (usb_endpoint_type(epd)) {
217fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_CONTROL:
218fb95c7cfSChunfeng Yun 		/* uframes per NAK */
219fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH)
220fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
221fb95c7cfSChunfeng Yun 		break;
222fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_ISOC:
223fb95c7cfSChunfeng Yun 		interval = 1 << (epd->bInterval - 1);
224fb95c7cfSChunfeng Yun 		break;
225fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_BULK:
226fb95c7cfSChunfeng Yun 		/* uframes per NAK */
227fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
228fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
229fb95c7cfSChunfeng Yun 		break;
230fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_INT:
231fb95c7cfSChunfeng Yun 		if (speed >= USB_SPEED_HIGH)
232fb95c7cfSChunfeng Yun 			interval = 1 << (epd->bInterval - 1);
233fb95c7cfSChunfeng Yun 		else
234fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
235fb95c7cfSChunfeng Yun 		break;
236fb95c7cfSChunfeng Yun 	}
237fb95c7cfSChunfeng Yun 
238fb95c7cfSChunfeng Yun 	interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
239fb95c7cfSChunfeng Yun 
240fb95c7cfSChunfeng Yun 	return interval;
241fb95c7cfSChunfeng Yun }
242fb95c7cfSChunfeng Yun EXPORT_SYMBOL_GPL(usb_decode_interval);
243fb95c7cfSChunfeng Yun 
24406e7114fSHeikki Krogerus #ifdef CONFIG_OF
245aa923ef1SMichal Sojka /**
24698bfb394SBin Liu  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
24798bfb394SBin Liu  * which is associated with the given phy device_node
24898bfb394SBin Liu  * @np:	Pointer to the given phy device_node
249ce15ed4cSHans de Goede  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
250ce15ed4cSHans de Goede  *        phys which do not have phy-cells
25198bfb394SBin Liu  *
25298bfb394SBin Liu  * In dts a usb controller associates with phy devices.  The function gets
25398bfb394SBin Liu  * the string from property 'dr_mode' of the controller associated with the
25498bfb394SBin Liu  * given phy device node, and returns the correspondig enum usb_dr_mode.
25598bfb394SBin Liu  */
256ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
25798bfb394SBin Liu {
25898bfb394SBin Liu 	struct device_node *controller = NULL;
259ce15ed4cSHans de Goede 	struct of_phandle_args args;
26098bfb394SBin Liu 	const char *dr_mode;
26198bfb394SBin Liu 	int index;
26298bfb394SBin Liu 	int err;
26398bfb394SBin Liu 
26498bfb394SBin Liu 	do {
26598bfb394SBin Liu 		controller = of_find_node_with_property(controller, "phys");
266238e0268SFabrizio Castro 		if (!of_device_is_available(controller))
267238e0268SFabrizio Castro 			continue;
26898bfb394SBin Liu 		index = 0;
26998bfb394SBin Liu 		do {
270ce15ed4cSHans de Goede 			if (arg0 == -1) {
271ce15ed4cSHans de Goede 				args.np = of_parse_phandle(controller, "phys",
272ce15ed4cSHans de Goede 							index);
273ce15ed4cSHans de Goede 				args.args_count = 0;
274ce15ed4cSHans de Goede 			} else {
275ce15ed4cSHans de Goede 				err = of_parse_phandle_with_args(controller,
276ce15ed4cSHans de Goede 							"phys", "#phy-cells",
277ce15ed4cSHans de Goede 							index, &args);
278ce15ed4cSHans de Goede 				if (err)
279ce15ed4cSHans de Goede 					break;
280ce15ed4cSHans de Goede 			}
281ce15ed4cSHans de Goede 
282ce15ed4cSHans de Goede 			of_node_put(args.np);
283ce15ed4cSHans de Goede 			if (args.np == np && (args.args_count == 0 ||
284ce15ed4cSHans de Goede 					      args.args[0] == arg0))
28598bfb394SBin Liu 				goto finish;
28698bfb394SBin Liu 			index++;
287ce15ed4cSHans de Goede 		} while (args.np);
28898bfb394SBin Liu 	} while (controller);
28998bfb394SBin Liu 
29098bfb394SBin Liu finish:
29198bfb394SBin Liu 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
29298bfb394SBin Liu 	of_node_put(controller);
29398bfb394SBin Liu 
29498bfb394SBin Liu 	if (err < 0)
29598bfb394SBin Liu 		return USB_DR_MODE_UNKNOWN;
29698bfb394SBin Liu 
29798bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
29898bfb394SBin Liu }
29998bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
30098bfb394SBin Liu 
30198bfb394SBin Liu /**
302aa923ef1SMichal Sojka  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
303aa923ef1SMichal Sojka  * for given targeted hosts (non-PC hosts)
304aa923ef1SMichal Sojka  * @np: Pointer to the given device_node
305aa923ef1SMichal Sojka  *
306aa923ef1SMichal Sojka  * The function gets if the targeted hosts support TPL or not
307aa923ef1SMichal Sojka  */
308aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np)
309aa923ef1SMichal Sojka {
310334007d5SSergei Shtylyov 	return of_property_read_bool(np, "tpl-support");
311aa923ef1SMichal Sojka }
312aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
313929412d9SLi Jun 
314929412d9SLi Jun /**
315929412d9SLi Jun  * of_usb_update_otg_caps - to update usb otg capabilities according to
316929412d9SLi Jun  * the passed properties in DT.
317929412d9SLi Jun  * @np: Pointer to the given device_node
318929412d9SLi Jun  * @otg_caps: Pointer to the target usb_otg_caps to be set
319929412d9SLi Jun  *
320929412d9SLi Jun  * The function updates the otg capabilities
321929412d9SLi Jun  */
322929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np,
323929412d9SLi Jun 			struct usb_otg_caps *otg_caps)
324929412d9SLi Jun {
325929412d9SLi Jun 	u32 otg_rev;
326929412d9SLi Jun 
327929412d9SLi Jun 	if (!otg_caps)
328929412d9SLi Jun 		return -EINVAL;
329929412d9SLi Jun 
330929412d9SLi Jun 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
331929412d9SLi Jun 		switch (otg_rev) {
332929412d9SLi Jun 		case 0x0100:
333929412d9SLi Jun 		case 0x0120:
334929412d9SLi Jun 		case 0x0130:
335929412d9SLi Jun 		case 0x0200:
336929412d9SLi Jun 			/* Choose the lesser one if it's already been set */
337929412d9SLi Jun 			if (otg_caps->otg_rev)
338929412d9SLi Jun 				otg_caps->otg_rev = min_t(u16, otg_rev,
339929412d9SLi Jun 							otg_caps->otg_rev);
340929412d9SLi Jun 			else
341929412d9SLi Jun 				otg_caps->otg_rev = otg_rev;
342929412d9SLi Jun 			break;
343929412d9SLi Jun 		default:
344d9241ff2SRob Herring 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
345d9241ff2SRob Herring 						np, otg_rev);
346929412d9SLi Jun 			return -EINVAL;
347929412d9SLi Jun 		}
348929412d9SLi Jun 	} else {
349929412d9SLi Jun 		/*
350929412d9SLi Jun 		 * otg-rev is mandatory for otg properties, if not passed
351929412d9SLi Jun 		 * we set it to be 0 and assume it's a legacy otg device.
352929412d9SLi Jun 		 * Non-dt platform can set it afterwards.
353929412d9SLi Jun 		 */
354929412d9SLi Jun 		otg_caps->otg_rev = 0;
355929412d9SLi Jun 	}
356929412d9SLi Jun 
357334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "hnp-disable"))
358929412d9SLi Jun 		otg_caps->hnp_support = false;
359334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "srp-disable"))
360929412d9SLi Jun 		otg_caps->srp_support = false;
361334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "adp-disable") ||
362929412d9SLi Jun 				(otg_caps->otg_rev < 0x0200))
363929412d9SLi Jun 		otg_caps->adp_support = false;
364929412d9SLi Jun 
365929412d9SLi Jun 	return 0;
366929412d9SLi Jun }
367929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
368929412d9SLi Jun 
369fa827966SYoshihiro Shimoda /**
370fa827966SYoshihiro Shimoda  * usb_of_get_companion_dev - Find the companion device
371fa827966SYoshihiro Shimoda  * @dev: the device pointer to find a companion
372fa827966SYoshihiro Shimoda  *
373fa827966SYoshihiro Shimoda  * Find the companion device from platform bus.
374fa827966SYoshihiro Shimoda  *
375fa827966SYoshihiro Shimoda  * Takes a reference to the returned struct device which needs to be dropped
376fa827966SYoshihiro Shimoda  * after use.
377fa827966SYoshihiro Shimoda  *
378fa827966SYoshihiro Shimoda  * Return: On success, a pointer to the companion device, %NULL on failure.
379fa827966SYoshihiro Shimoda  */
380fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev)
381fa827966SYoshihiro Shimoda {
382fa827966SYoshihiro Shimoda 	struct device_node *node;
383fa827966SYoshihiro Shimoda 	struct platform_device *pdev = NULL;
384fa827966SYoshihiro Shimoda 
385fa827966SYoshihiro Shimoda 	node = of_parse_phandle(dev->of_node, "companion", 0);
386fa827966SYoshihiro Shimoda 	if (node)
387fa827966SYoshihiro Shimoda 		pdev = of_find_device_by_node(node);
388fa827966SYoshihiro Shimoda 
389fa827966SYoshihiro Shimoda 	of_node_put(node);
390fa827966SYoshihiro Shimoda 
391fa827966SYoshihiro Shimoda 	return pdev ? &pdev->dev : NULL;
392fa827966SYoshihiro Shimoda }
393fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
394aa923ef1SMichal Sojka #endif
395aa923ef1SMichal Sojka 
396812086d3SGreg Kroah-Hartman struct dentry *usb_debug_root;
397812086d3SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_debug_root);
398812086d3SGreg Kroah-Hartman 
399812086d3SGreg Kroah-Hartman static int __init usb_common_init(void)
400812086d3SGreg Kroah-Hartman {
401812086d3SGreg Kroah-Hartman 	usb_debug_root = debugfs_create_dir("usb", NULL);
402812086d3SGreg Kroah-Hartman 	ledtrig_usb_init();
403812086d3SGreg Kroah-Hartman 	return 0;
404812086d3SGreg Kroah-Hartman }
405812086d3SGreg Kroah-Hartman 
406812086d3SGreg Kroah-Hartman static void __exit usb_common_exit(void)
407812086d3SGreg Kroah-Hartman {
408812086d3SGreg Kroah-Hartman 	ledtrig_usb_exit();
409812086d3SGreg Kroah-Hartman 	debugfs_remove_recursive(usb_debug_root);
410812086d3SGreg Kroah-Hartman }
411812086d3SGreg Kroah-Hartman 
412812086d3SGreg Kroah-Hartman subsys_initcall(usb_common_init);
413812086d3SGreg Kroah-Hartman module_exit(usb_common_exit);
414812086d3SGreg Kroah-Hartman 
415aa923ef1SMichal Sojka MODULE_LICENSE("GPL");
416