xref: /openbmc/linux/drivers/usb/common/common.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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>
14*484468fbSRob Herring #include <linux/platform_device.h>
15aa923ef1SMichal Sojka #include <linux/usb/ch9.h>
16aa923ef1SMichal Sojka #include <linux/usb/of.h>
17aa923ef1SMichal Sojka #include <linux/usb/otg.h>
1898bfb394SBin Liu #include <linux/of_platform.h>
19812086d3SGreg Kroah-Hartman #include <linux/debugfs.h>
20812086d3SGreg Kroah-Hartman #include "common.h"
21aa923ef1SMichal Sojka 
224d537f37SChunfeng Yun static const char *const ep_type_names[] = {
234d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_CONTROL] = "ctrl",
244d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_ISOC] = "isoc",
254d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_BULK] = "bulk",
264d537f37SChunfeng Yun 	[USB_ENDPOINT_XFER_INT] = "intr",
274d537f37SChunfeng Yun };
284d537f37SChunfeng Yun 
29365038f2SChunfeng Yun /**
30365038f2SChunfeng Yun  * usb_ep_type_string() - Returns human readable-name of the endpoint type.
31365038f2SChunfeng Yun  * @ep_type: The endpoint type to return human-readable name for.  If it's not
32365038f2SChunfeng Yun  *   any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
33365038f2SChunfeng Yun  *   usually got by usb_endpoint_type(), the string 'unknown' will be returned.
34365038f2SChunfeng Yun  */
usb_ep_type_string(int ep_type)354d537f37SChunfeng Yun const char *usb_ep_type_string(int ep_type)
364d537f37SChunfeng Yun {
374d537f37SChunfeng Yun 	if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
384d537f37SChunfeng Yun 		return "unknown";
394d537f37SChunfeng Yun 
404d537f37SChunfeng Yun 	return ep_type_names[ep_type];
414d537f37SChunfeng Yun }
424d537f37SChunfeng Yun EXPORT_SYMBOL_GPL(usb_ep_type_string);
434d537f37SChunfeng Yun 
usb_otg_state_string(enum usb_otg_state state)44aa923ef1SMichal Sojka const char *usb_otg_state_string(enum usb_otg_state state)
45aa923ef1SMichal Sojka {
46aa923ef1SMichal Sojka 	static const char *const names[] = {
47aa923ef1SMichal Sojka 		[OTG_STATE_A_IDLE] = "a_idle",
48aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
49aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
50aa923ef1SMichal Sojka 		[OTG_STATE_A_HOST] = "a_host",
51aa923ef1SMichal Sojka 		[OTG_STATE_A_SUSPEND] = "a_suspend",
52aa923ef1SMichal Sojka 		[OTG_STATE_A_PERIPHERAL] = "a_peripheral",
53aa923ef1SMichal Sojka 		[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
54aa923ef1SMichal Sojka 		[OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
55aa923ef1SMichal Sojka 		[OTG_STATE_B_IDLE] = "b_idle",
56aa923ef1SMichal Sojka 		[OTG_STATE_B_SRP_INIT] = "b_srp_init",
57aa923ef1SMichal Sojka 		[OTG_STATE_B_PERIPHERAL] = "b_peripheral",
58aa923ef1SMichal Sojka 		[OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
59aa923ef1SMichal Sojka 		[OTG_STATE_B_HOST] = "b_host",
60aa923ef1SMichal Sojka 	};
61aa923ef1SMichal Sojka 
62aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
63aa923ef1SMichal Sojka 		return "UNDEFINED";
64aa923ef1SMichal Sojka 
65aa923ef1SMichal Sojka 	return names[state];
66aa923ef1SMichal Sojka }
67aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_otg_state_string);
68aa923ef1SMichal Sojka 
69aa923ef1SMichal Sojka static const char *const speed_names[] = {
70aa923ef1SMichal Sojka 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
71aa923ef1SMichal Sojka 	[USB_SPEED_LOW] = "low-speed",
72aa923ef1SMichal Sojka 	[USB_SPEED_FULL] = "full-speed",
73aa923ef1SMichal Sojka 	[USB_SPEED_HIGH] = "high-speed",
74aa923ef1SMichal Sojka 	[USB_SPEED_WIRELESS] = "wireless",
75aa923ef1SMichal Sojka 	[USB_SPEED_SUPER] = "super-speed",
768a1b2725SMathias Nyman 	[USB_SPEED_SUPER_PLUS] = "super-speed-plus",
77aa923ef1SMichal Sojka };
78aa923ef1SMichal Sojka 
7952c2d157SThinh Nguyen static const char *const ssp_rate[] = {
8052c2d157SThinh Nguyen 	[USB_SSP_GEN_UNKNOWN] = "UNKNOWN",
8152c2d157SThinh Nguyen 	[USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1",
8252c2d157SThinh Nguyen 	[USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2",
8352c2d157SThinh Nguyen 	[USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2",
8452c2d157SThinh Nguyen };
8552c2d157SThinh Nguyen 
86365038f2SChunfeng Yun /**
87365038f2SChunfeng Yun  * usb_speed_string() - Returns human readable-name of the speed.
88365038f2SChunfeng Yun  * @speed: The speed to return human-readable name for.  If it's not
89365038f2SChunfeng Yun  *   any of the speeds defined in usb_device_speed enum, string for
90365038f2SChunfeng Yun  *   USB_SPEED_UNKNOWN will be returned.
91365038f2SChunfeng Yun  */
usb_speed_string(enum usb_device_speed speed)92aa923ef1SMichal Sojka const char *usb_speed_string(enum usb_device_speed speed)
93aa923ef1SMichal Sojka {
94aa923ef1SMichal Sojka 	if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
95aa923ef1SMichal Sojka 		speed = USB_SPEED_UNKNOWN;
96aa923ef1SMichal Sojka 	return speed_names[speed];
97aa923ef1SMichal Sojka }
98aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_speed_string);
99aa923ef1SMichal Sojka 
100365038f2SChunfeng Yun /**
101365038f2SChunfeng Yun  * usb_get_maximum_speed - Get maximum requested speed for a given USB
102365038f2SChunfeng Yun  * controller.
103365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
104365038f2SChunfeng Yun  *
105365038f2SChunfeng Yun  * The function gets the maximum speed string from property "maximum-speed",
106365038f2SChunfeng Yun  * and returns the corresponding enum usb_device_speed.
107365038f2SChunfeng Yun  */
usb_get_maximum_speed(struct device * dev)10863863b98SHeikki Krogerus enum usb_device_speed usb_get_maximum_speed(struct device *dev)
10963863b98SHeikki Krogerus {
11063863b98SHeikki Krogerus 	const char *maximum_speed;
111efc2cd79SHeikki Krogerus 	int ret;
11263863b98SHeikki Krogerus 
113efc2cd79SHeikki Krogerus 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
114efc2cd79SHeikki Krogerus 	if (ret < 0)
11563863b98SHeikki Krogerus 		return USB_SPEED_UNKNOWN;
11663863b98SHeikki Krogerus 
11752c2d157SThinh Nguyen 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
11852c2d157SThinh Nguyen 	if (ret > 0)
11952c2d157SThinh Nguyen 		return USB_SPEED_SUPER_PLUS;
12063863b98SHeikki Krogerus 
12152c2d157SThinh Nguyen 	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
122efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
12363863b98SHeikki Krogerus }
12463863b98SHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
12563863b98SHeikki Krogerus 
126365038f2SChunfeng Yun /**
127365038f2SChunfeng Yun  * usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count
128365038f2SChunfeng Yun  *	of a SuperSpeed Plus capable device.
129365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
130365038f2SChunfeng Yun  *
131365038f2SChunfeng Yun  * If the string from "maximum-speed" property is super-speed-plus-genXxY where
132365038f2SChunfeng Yun  * 'X' is the generation number and 'Y' is the number of lanes, then this
133365038f2SChunfeng Yun  * function returns the corresponding enum usb_ssp_rate.
134365038f2SChunfeng Yun  */
usb_get_maximum_ssp_rate(struct device * dev)13552c2d157SThinh Nguyen enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev)
13652c2d157SThinh Nguyen {
13752c2d157SThinh Nguyen 	const char *maximum_speed;
13852c2d157SThinh Nguyen 	int ret;
13952c2d157SThinh Nguyen 
14052c2d157SThinh Nguyen 	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
14152c2d157SThinh Nguyen 	if (ret < 0)
14252c2d157SThinh Nguyen 		return USB_SSP_GEN_UNKNOWN;
14352c2d157SThinh Nguyen 
14452c2d157SThinh Nguyen 	ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed);
14552c2d157SThinh Nguyen 	return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret;
14652c2d157SThinh Nguyen }
14752c2d157SThinh Nguyen EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate);
14852c2d157SThinh Nguyen 
149365038f2SChunfeng Yun /**
150365038f2SChunfeng Yun  * usb_state_string - Returns human readable name for the state.
151365038f2SChunfeng Yun  * @state: The state to return a human-readable name for. If it's not
152365038f2SChunfeng Yun  *	any of the states devices in usb_device_state_string enum,
153365038f2SChunfeng Yun  *	the string UNKNOWN will be returned.
154365038f2SChunfeng Yun  */
usb_state_string(enum usb_device_state state)155aa923ef1SMichal Sojka const char *usb_state_string(enum usb_device_state state)
156aa923ef1SMichal Sojka {
157aa923ef1SMichal Sojka 	static const char *const names[] = {
158aa923ef1SMichal Sojka 		[USB_STATE_NOTATTACHED] = "not attached",
159aa923ef1SMichal Sojka 		[USB_STATE_ATTACHED] = "attached",
160aa923ef1SMichal Sojka 		[USB_STATE_POWERED] = "powered",
161aa923ef1SMichal Sojka 		[USB_STATE_RECONNECTING] = "reconnecting",
162aa923ef1SMichal Sojka 		[USB_STATE_UNAUTHENTICATED] = "unauthenticated",
163aa923ef1SMichal Sojka 		[USB_STATE_DEFAULT] = "default",
164aa923ef1SMichal Sojka 		[USB_STATE_ADDRESS] = "addressed",
165aa923ef1SMichal Sojka 		[USB_STATE_CONFIGURED] = "configured",
166aa923ef1SMichal Sojka 		[USB_STATE_SUSPENDED] = "suspended",
167aa923ef1SMichal Sojka 	};
168aa923ef1SMichal Sojka 
169aa923ef1SMichal Sojka 	if (state < 0 || state >= ARRAY_SIZE(names))
170aa923ef1SMichal Sojka 		return "UNKNOWN";
171aa923ef1SMichal Sojka 
172aa923ef1SMichal Sojka 	return names[state];
173aa923ef1SMichal Sojka }
174aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(usb_state_string);
175aa923ef1SMichal Sojka 
176aa923ef1SMichal Sojka static const char *const usb_dr_modes[] = {
177aa923ef1SMichal Sojka 	[USB_DR_MODE_UNKNOWN]		= "",
178aa923ef1SMichal Sojka 	[USB_DR_MODE_HOST]		= "host",
179aa923ef1SMichal Sojka 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
180aa923ef1SMichal Sojka 	[USB_DR_MODE_OTG]		= "otg",
181aa923ef1SMichal Sojka };
182aa923ef1SMichal Sojka 
usb_get_dr_mode_from_string(const char * str)18398bfb394SBin Liu static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
18498bfb394SBin Liu {
185efc2cd79SHeikki Krogerus 	int ret;
18698bfb394SBin Liu 
187efc2cd79SHeikki Krogerus 	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
188efc2cd79SHeikki Krogerus 	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
18998bfb394SBin Liu }
19098bfb394SBin Liu 
usb_get_dr_mode(struct device * dev)19106e7114fSHeikki Krogerus enum usb_dr_mode usb_get_dr_mode(struct device *dev)
192aa923ef1SMichal Sojka {
193aa923ef1SMichal Sojka 	const char *dr_mode;
19498bfb394SBin Liu 	int err;
195aa923ef1SMichal Sojka 
19606e7114fSHeikki Krogerus 	err = device_property_read_string(dev, "dr_mode", &dr_mode);
197aa923ef1SMichal Sojka 	if (err < 0)
198aa923ef1SMichal Sojka 		return USB_DR_MODE_UNKNOWN;
199aa923ef1SMichal Sojka 
20098bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
201aa923ef1SMichal Sojka }
20206e7114fSHeikki Krogerus EXPORT_SYMBOL_GPL(usb_get_dr_mode);
203aa923ef1SMichal Sojka 
204fb95c7cfSChunfeng Yun /**
2052037f299SChunfeng Yun  * usb_get_role_switch_default_mode - Get default mode for given device
2062037f299SChunfeng Yun  * @dev: Pointer to the given device
2072037f299SChunfeng Yun  *
2082037f299SChunfeng Yun  * The function gets string from property 'role-switch-default-mode',
2092037f299SChunfeng Yun  * and returns the corresponding enum usb_dr_mode.
2102037f299SChunfeng Yun  */
usb_get_role_switch_default_mode(struct device * dev)2112037f299SChunfeng Yun enum usb_dr_mode usb_get_role_switch_default_mode(struct device *dev)
2122037f299SChunfeng Yun {
2132037f299SChunfeng Yun 	const char *str;
2142037f299SChunfeng Yun 	int ret;
2152037f299SChunfeng Yun 
2162037f299SChunfeng Yun 	ret = device_property_read_string(dev, "role-switch-default-mode", &str);
2172037f299SChunfeng Yun 	if (ret < 0)
2182037f299SChunfeng Yun 		return USB_DR_MODE_UNKNOWN;
2192037f299SChunfeng Yun 
2202037f299SChunfeng Yun 	return usb_get_dr_mode_from_string(str);
2212037f299SChunfeng Yun }
2222037f299SChunfeng Yun EXPORT_SYMBOL_GPL(usb_get_role_switch_default_mode);
2232037f299SChunfeng Yun 
2242037f299SChunfeng Yun /**
225fb95c7cfSChunfeng Yun  * usb_decode_interval - Decode bInterval into the time expressed in 1us unit
226fb95c7cfSChunfeng Yun  * @epd: The descriptor of the endpoint
227fb95c7cfSChunfeng Yun  * @speed: The speed that the endpoint works as
228fb95c7cfSChunfeng Yun  *
229fb95c7cfSChunfeng Yun  * Function returns the interval expressed in 1us unit for servicing
230fb95c7cfSChunfeng Yun  * endpoint for data transfers.
231fb95c7cfSChunfeng Yun  */
usb_decode_interval(const struct usb_endpoint_descriptor * epd,enum usb_device_speed speed)232fb95c7cfSChunfeng Yun unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
233fb95c7cfSChunfeng Yun 				 enum usb_device_speed speed)
234fb95c7cfSChunfeng Yun {
235fb95c7cfSChunfeng Yun 	unsigned int interval = 0;
236fb95c7cfSChunfeng Yun 
237fb95c7cfSChunfeng Yun 	switch (usb_endpoint_type(epd)) {
238fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_CONTROL:
239fb95c7cfSChunfeng Yun 		/* uframes per NAK */
240fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH)
241fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
242fb95c7cfSChunfeng Yun 		break;
243fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_ISOC:
244fb95c7cfSChunfeng Yun 		interval = 1 << (epd->bInterval - 1);
245fb95c7cfSChunfeng Yun 		break;
246fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_BULK:
247fb95c7cfSChunfeng Yun 		/* uframes per NAK */
248fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
249fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
250fb95c7cfSChunfeng Yun 		break;
251fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_INT:
252fb95c7cfSChunfeng Yun 		if (speed >= USB_SPEED_HIGH)
253fb95c7cfSChunfeng Yun 			interval = 1 << (epd->bInterval - 1);
254fb95c7cfSChunfeng Yun 		else
255fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
256fb95c7cfSChunfeng Yun 		break;
257fb95c7cfSChunfeng Yun 	}
258fb95c7cfSChunfeng Yun 
259fb95c7cfSChunfeng Yun 	interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
260fb95c7cfSChunfeng Yun 
261fb95c7cfSChunfeng Yun 	return interval;
262fb95c7cfSChunfeng Yun }
263fb95c7cfSChunfeng Yun EXPORT_SYMBOL_GPL(usb_decode_interval);
264fb95c7cfSChunfeng Yun 
26506e7114fSHeikki Krogerus #ifdef CONFIG_OF
266aa923ef1SMichal Sojka /**
26798bfb394SBin Liu  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
26898bfb394SBin Liu  * which is associated with the given phy device_node
26998bfb394SBin Liu  * @np:	Pointer to the given phy device_node
270ce15ed4cSHans de Goede  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
271ce15ed4cSHans de Goede  *        phys which do not have phy-cells
27298bfb394SBin Liu  *
27398bfb394SBin Liu  * In dts a usb controller associates with phy devices.  The function gets
27498bfb394SBin Liu  * the string from property 'dr_mode' of the controller associated with the
27598bfb394SBin Liu  * given phy device node, and returns the correspondig enum usb_dr_mode.
27698bfb394SBin Liu  */
of_usb_get_dr_mode_by_phy(struct device_node * np,int arg0)277ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
27898bfb394SBin Liu {
27998bfb394SBin Liu 	struct device_node *controller = NULL;
280ce15ed4cSHans de Goede 	struct of_phandle_args args;
28198bfb394SBin Liu 	const char *dr_mode;
28298bfb394SBin Liu 	int index;
28398bfb394SBin Liu 	int err;
28498bfb394SBin Liu 
28598bfb394SBin Liu 	do {
28698bfb394SBin Liu 		controller = of_find_node_with_property(controller, "phys");
287238e0268SFabrizio Castro 		if (!of_device_is_available(controller))
288238e0268SFabrizio Castro 			continue;
28998bfb394SBin Liu 		index = 0;
29098bfb394SBin Liu 		do {
291ce15ed4cSHans de Goede 			if (arg0 == -1) {
292ce15ed4cSHans de Goede 				args.np = of_parse_phandle(controller, "phys",
293ce15ed4cSHans de Goede 							index);
294ce15ed4cSHans de Goede 				args.args_count = 0;
295ce15ed4cSHans de Goede 			} else {
296ce15ed4cSHans de Goede 				err = of_parse_phandle_with_args(controller,
297ce15ed4cSHans de Goede 							"phys", "#phy-cells",
298ce15ed4cSHans de Goede 							index, &args);
299ce15ed4cSHans de Goede 				if (err)
300ce15ed4cSHans de Goede 					break;
301ce15ed4cSHans de Goede 			}
302ce15ed4cSHans de Goede 
303ce15ed4cSHans de Goede 			of_node_put(args.np);
304ce15ed4cSHans de Goede 			if (args.np == np && (args.args_count == 0 ||
305ce15ed4cSHans de Goede 					      args.args[0] == arg0))
30698bfb394SBin Liu 				goto finish;
30798bfb394SBin Liu 			index++;
308ce15ed4cSHans de Goede 		} while (args.np);
30998bfb394SBin Liu 	} while (controller);
31098bfb394SBin Liu 
31198bfb394SBin Liu finish:
31298bfb394SBin Liu 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
31398bfb394SBin Liu 	of_node_put(controller);
31498bfb394SBin Liu 
31598bfb394SBin Liu 	if (err < 0)
31698bfb394SBin Liu 		return USB_DR_MODE_UNKNOWN;
31798bfb394SBin Liu 
31898bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
31998bfb394SBin Liu }
32098bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
32198bfb394SBin Liu 
32298bfb394SBin Liu /**
323aa923ef1SMichal Sojka  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
324aa923ef1SMichal Sojka  * for given targeted hosts (non-PC hosts)
325aa923ef1SMichal Sojka  * @np: Pointer to the given device_node
326aa923ef1SMichal Sojka  *
327aa923ef1SMichal Sojka  * The function gets if the targeted hosts support TPL or not
328aa923ef1SMichal Sojka  */
of_usb_host_tpl_support(struct device_node * np)329aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np)
330aa923ef1SMichal Sojka {
331334007d5SSergei Shtylyov 	return of_property_read_bool(np, "tpl-support");
332aa923ef1SMichal Sojka }
333aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
334929412d9SLi Jun 
335929412d9SLi Jun /**
336929412d9SLi Jun  * of_usb_update_otg_caps - to update usb otg capabilities according to
337929412d9SLi Jun  * the passed properties in DT.
338929412d9SLi Jun  * @np: Pointer to the given device_node
339929412d9SLi Jun  * @otg_caps: Pointer to the target usb_otg_caps to be set
340929412d9SLi Jun  *
341929412d9SLi Jun  * The function updates the otg capabilities
342929412d9SLi Jun  */
of_usb_update_otg_caps(struct device_node * np,struct usb_otg_caps * otg_caps)343929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np,
344929412d9SLi Jun 			struct usb_otg_caps *otg_caps)
345929412d9SLi Jun {
346929412d9SLi Jun 	u32 otg_rev;
347929412d9SLi Jun 
348929412d9SLi Jun 	if (!otg_caps)
349929412d9SLi Jun 		return -EINVAL;
350929412d9SLi Jun 
351929412d9SLi Jun 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
352929412d9SLi Jun 		switch (otg_rev) {
353929412d9SLi Jun 		case 0x0100:
354929412d9SLi Jun 		case 0x0120:
355929412d9SLi Jun 		case 0x0130:
356929412d9SLi Jun 		case 0x0200:
357929412d9SLi Jun 			/* Choose the lesser one if it's already been set */
358929412d9SLi Jun 			if (otg_caps->otg_rev)
359929412d9SLi Jun 				otg_caps->otg_rev = min_t(u16, otg_rev,
360929412d9SLi Jun 							otg_caps->otg_rev);
361929412d9SLi Jun 			else
362929412d9SLi Jun 				otg_caps->otg_rev = otg_rev;
363929412d9SLi Jun 			break;
364929412d9SLi Jun 		default:
365d9241ff2SRob Herring 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
366d9241ff2SRob Herring 						np, otg_rev);
367929412d9SLi Jun 			return -EINVAL;
368929412d9SLi Jun 		}
369929412d9SLi Jun 	} else {
370929412d9SLi Jun 		/*
371929412d9SLi Jun 		 * otg-rev is mandatory for otg properties, if not passed
372929412d9SLi Jun 		 * we set it to be 0 and assume it's a legacy otg device.
373929412d9SLi Jun 		 * Non-dt platform can set it afterwards.
374929412d9SLi Jun 		 */
375929412d9SLi Jun 		otg_caps->otg_rev = 0;
376929412d9SLi Jun 	}
377929412d9SLi Jun 
378334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "hnp-disable"))
379929412d9SLi Jun 		otg_caps->hnp_support = false;
380334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "srp-disable"))
381929412d9SLi Jun 		otg_caps->srp_support = false;
382334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "adp-disable") ||
383929412d9SLi Jun 				(otg_caps->otg_rev < 0x0200))
384929412d9SLi Jun 		otg_caps->adp_support = false;
385929412d9SLi Jun 
386929412d9SLi Jun 	return 0;
387929412d9SLi Jun }
388929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
389929412d9SLi Jun 
390fa827966SYoshihiro Shimoda /**
391fa827966SYoshihiro Shimoda  * usb_of_get_companion_dev - Find the companion device
392fa827966SYoshihiro Shimoda  * @dev: the device pointer to find a companion
393fa827966SYoshihiro Shimoda  *
394fa827966SYoshihiro Shimoda  * Find the companion device from platform bus.
395fa827966SYoshihiro Shimoda  *
396fa827966SYoshihiro Shimoda  * Takes a reference to the returned struct device which needs to be dropped
397fa827966SYoshihiro Shimoda  * after use.
398fa827966SYoshihiro Shimoda  *
399fa827966SYoshihiro Shimoda  * Return: On success, a pointer to the companion device, %NULL on failure.
400fa827966SYoshihiro Shimoda  */
usb_of_get_companion_dev(struct device * dev)401fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev)
402fa827966SYoshihiro Shimoda {
403fa827966SYoshihiro Shimoda 	struct device_node *node;
404fa827966SYoshihiro Shimoda 	struct platform_device *pdev = NULL;
405fa827966SYoshihiro Shimoda 
406fa827966SYoshihiro Shimoda 	node = of_parse_phandle(dev->of_node, "companion", 0);
407fa827966SYoshihiro Shimoda 	if (node)
408fa827966SYoshihiro Shimoda 		pdev = of_find_device_by_node(node);
409fa827966SYoshihiro Shimoda 
410fa827966SYoshihiro Shimoda 	of_node_put(node);
411fa827966SYoshihiro Shimoda 
412fa827966SYoshihiro Shimoda 	return pdev ? &pdev->dev : NULL;
413fa827966SYoshihiro Shimoda }
414fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
415aa923ef1SMichal Sojka #endif
416aa923ef1SMichal Sojka 
417812086d3SGreg Kroah-Hartman struct dentry *usb_debug_root;
418812086d3SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_debug_root);
419812086d3SGreg Kroah-Hartman 
usb_common_init(void)420812086d3SGreg Kroah-Hartman static int __init usb_common_init(void)
421812086d3SGreg Kroah-Hartman {
422812086d3SGreg Kroah-Hartman 	usb_debug_root = debugfs_create_dir("usb", NULL);
423812086d3SGreg Kroah-Hartman 	ledtrig_usb_init();
424812086d3SGreg Kroah-Hartman 	return 0;
425812086d3SGreg Kroah-Hartman }
426812086d3SGreg Kroah-Hartman 
usb_common_exit(void)427812086d3SGreg Kroah-Hartman static void __exit usb_common_exit(void)
428812086d3SGreg Kroah-Hartman {
429812086d3SGreg Kroah-Hartman 	ledtrig_usb_exit();
430812086d3SGreg Kroah-Hartman 	debugfs_remove_recursive(usb_debug_root);
431812086d3SGreg Kroah-Hartman }
432812086d3SGreg Kroah-Hartman 
433812086d3SGreg Kroah-Hartman subsys_initcall(usb_common_init);
434812086d3SGreg Kroah-Hartman module_exit(usb_common_exit);
435812086d3SGreg Kroah-Hartman 
436aa923ef1SMichal Sojka MODULE_LICENSE("GPL");
437