xref: /openbmc/linux/drivers/usb/common/common.c (revision 2037f2991ddedded1ef4aaabe4caf11e306158dc)
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 
28365038f2SChunfeng Yun /**
29365038f2SChunfeng Yun  * usb_ep_type_string() - Returns human readable-name of the endpoint type.
30365038f2SChunfeng Yun  * @ep_type: The endpoint type to return human-readable name for.  If it's not
31365038f2SChunfeng Yun  *   any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
32365038f2SChunfeng Yun  *   usually got by usb_endpoint_type(), the string 'unknown' will be returned.
33365038f2SChunfeng 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 
85365038f2SChunfeng Yun /**
86365038f2SChunfeng Yun  * usb_speed_string() - Returns human readable-name of the speed.
87365038f2SChunfeng Yun  * @speed: The speed to return human-readable name for.  If it's not
88365038f2SChunfeng Yun  *   any of the speeds defined in usb_device_speed enum, string for
89365038f2SChunfeng Yun  *   USB_SPEED_UNKNOWN will be returned.
90365038f2SChunfeng 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 
99365038f2SChunfeng Yun /**
100365038f2SChunfeng Yun  * usb_get_maximum_speed - Get maximum requested speed for a given USB
101365038f2SChunfeng Yun  * controller.
102365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
103365038f2SChunfeng Yun  *
104365038f2SChunfeng Yun  * The function gets the maximum speed string from property "maximum-speed",
105365038f2SChunfeng Yun  * and returns the corresponding enum usb_device_speed.
106365038f2SChunfeng 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 
125365038f2SChunfeng Yun /**
126365038f2SChunfeng Yun  * usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count
127365038f2SChunfeng Yun  *	of a SuperSpeed Plus capable device.
128365038f2SChunfeng Yun  * @dev: Pointer to the given USB controller device
129365038f2SChunfeng Yun  *
130365038f2SChunfeng Yun  * If the string from "maximum-speed" property is super-speed-plus-genXxY where
131365038f2SChunfeng Yun  * 'X' is the generation number and 'Y' is the number of lanes, then this
132365038f2SChunfeng Yun  * function returns the corresponding enum usb_ssp_rate.
133365038f2SChunfeng 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 
148365038f2SChunfeng Yun /**
149365038f2SChunfeng Yun  * usb_state_string - Returns human readable name for the state.
150365038f2SChunfeng Yun  * @state: The state to return a human-readable name for. If it's not
151365038f2SChunfeng Yun  *	any of the states devices in usb_device_state_string enum,
152365038f2SChunfeng Yun  *	the string UNKNOWN will be returned.
153365038f2SChunfeng 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 /**
204*2037f299SChunfeng Yun  * usb_get_role_switch_default_mode - Get default mode for given device
205*2037f299SChunfeng Yun  * @dev: Pointer to the given device
206*2037f299SChunfeng Yun  *
207*2037f299SChunfeng Yun  * The function gets string from property 'role-switch-default-mode',
208*2037f299SChunfeng Yun  * and returns the corresponding enum usb_dr_mode.
209*2037f299SChunfeng Yun  */
210*2037f299SChunfeng Yun enum usb_dr_mode usb_get_role_switch_default_mode(struct device *dev)
211*2037f299SChunfeng Yun {
212*2037f299SChunfeng Yun 	const char *str;
213*2037f299SChunfeng Yun 	int ret;
214*2037f299SChunfeng Yun 
215*2037f299SChunfeng Yun 	ret = device_property_read_string(dev, "role-switch-default-mode", &str);
216*2037f299SChunfeng Yun 	if (ret < 0)
217*2037f299SChunfeng Yun 		return USB_DR_MODE_UNKNOWN;
218*2037f299SChunfeng Yun 
219*2037f299SChunfeng Yun 	return usb_get_dr_mode_from_string(str);
220*2037f299SChunfeng Yun }
221*2037f299SChunfeng Yun EXPORT_SYMBOL_GPL(usb_get_role_switch_default_mode);
222*2037f299SChunfeng Yun 
223*2037f299SChunfeng Yun /**
224fb95c7cfSChunfeng Yun  * usb_decode_interval - Decode bInterval into the time expressed in 1us unit
225fb95c7cfSChunfeng Yun  * @epd: The descriptor of the endpoint
226fb95c7cfSChunfeng Yun  * @speed: The speed that the endpoint works as
227fb95c7cfSChunfeng Yun  *
228fb95c7cfSChunfeng Yun  * Function returns the interval expressed in 1us unit for servicing
229fb95c7cfSChunfeng Yun  * endpoint for data transfers.
230fb95c7cfSChunfeng Yun  */
231fb95c7cfSChunfeng Yun unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
232fb95c7cfSChunfeng Yun 				 enum usb_device_speed speed)
233fb95c7cfSChunfeng Yun {
234fb95c7cfSChunfeng Yun 	unsigned int interval = 0;
235fb95c7cfSChunfeng Yun 
236fb95c7cfSChunfeng Yun 	switch (usb_endpoint_type(epd)) {
237fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_CONTROL:
238fb95c7cfSChunfeng Yun 		/* uframes per NAK */
239fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH)
240fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
241fb95c7cfSChunfeng Yun 		break;
242fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_ISOC:
243fb95c7cfSChunfeng Yun 		interval = 1 << (epd->bInterval - 1);
244fb95c7cfSChunfeng Yun 		break;
245fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_BULK:
246fb95c7cfSChunfeng Yun 		/* uframes per NAK */
247fb95c7cfSChunfeng Yun 		if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
248fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
249fb95c7cfSChunfeng Yun 		break;
250fb95c7cfSChunfeng Yun 	case USB_ENDPOINT_XFER_INT:
251fb95c7cfSChunfeng Yun 		if (speed >= USB_SPEED_HIGH)
252fb95c7cfSChunfeng Yun 			interval = 1 << (epd->bInterval - 1);
253fb95c7cfSChunfeng Yun 		else
254fb95c7cfSChunfeng Yun 			interval = epd->bInterval;
255fb95c7cfSChunfeng Yun 		break;
256fb95c7cfSChunfeng Yun 	}
257fb95c7cfSChunfeng Yun 
258fb95c7cfSChunfeng Yun 	interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
259fb95c7cfSChunfeng Yun 
260fb95c7cfSChunfeng Yun 	return interval;
261fb95c7cfSChunfeng Yun }
262fb95c7cfSChunfeng Yun EXPORT_SYMBOL_GPL(usb_decode_interval);
263fb95c7cfSChunfeng Yun 
26406e7114fSHeikki Krogerus #ifdef CONFIG_OF
265aa923ef1SMichal Sojka /**
26698bfb394SBin Liu  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
26798bfb394SBin Liu  * which is associated with the given phy device_node
26898bfb394SBin Liu  * @np:	Pointer to the given phy device_node
269ce15ed4cSHans de Goede  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
270ce15ed4cSHans de Goede  *        phys which do not have phy-cells
27198bfb394SBin Liu  *
27298bfb394SBin Liu  * In dts a usb controller associates with phy devices.  The function gets
27398bfb394SBin Liu  * the string from property 'dr_mode' of the controller associated with the
27498bfb394SBin Liu  * given phy device node, and returns the correspondig enum usb_dr_mode.
27598bfb394SBin Liu  */
276ce15ed4cSHans de Goede enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
27798bfb394SBin Liu {
27898bfb394SBin Liu 	struct device_node *controller = NULL;
279ce15ed4cSHans de Goede 	struct of_phandle_args args;
28098bfb394SBin Liu 	const char *dr_mode;
28198bfb394SBin Liu 	int index;
28298bfb394SBin Liu 	int err;
28398bfb394SBin Liu 
28498bfb394SBin Liu 	do {
28598bfb394SBin Liu 		controller = of_find_node_with_property(controller, "phys");
286238e0268SFabrizio Castro 		if (!of_device_is_available(controller))
287238e0268SFabrizio Castro 			continue;
28898bfb394SBin Liu 		index = 0;
28998bfb394SBin Liu 		do {
290ce15ed4cSHans de Goede 			if (arg0 == -1) {
291ce15ed4cSHans de Goede 				args.np = of_parse_phandle(controller, "phys",
292ce15ed4cSHans de Goede 							index);
293ce15ed4cSHans de Goede 				args.args_count = 0;
294ce15ed4cSHans de Goede 			} else {
295ce15ed4cSHans de Goede 				err = of_parse_phandle_with_args(controller,
296ce15ed4cSHans de Goede 							"phys", "#phy-cells",
297ce15ed4cSHans de Goede 							index, &args);
298ce15ed4cSHans de Goede 				if (err)
299ce15ed4cSHans de Goede 					break;
300ce15ed4cSHans de Goede 			}
301ce15ed4cSHans de Goede 
302ce15ed4cSHans de Goede 			of_node_put(args.np);
303ce15ed4cSHans de Goede 			if (args.np == np && (args.args_count == 0 ||
304ce15ed4cSHans de Goede 					      args.args[0] == arg0))
30598bfb394SBin Liu 				goto finish;
30698bfb394SBin Liu 			index++;
307ce15ed4cSHans de Goede 		} while (args.np);
30898bfb394SBin Liu 	} while (controller);
30998bfb394SBin Liu 
31098bfb394SBin Liu finish:
31198bfb394SBin Liu 	err = of_property_read_string(controller, "dr_mode", &dr_mode);
31298bfb394SBin Liu 	of_node_put(controller);
31398bfb394SBin Liu 
31498bfb394SBin Liu 	if (err < 0)
31598bfb394SBin Liu 		return USB_DR_MODE_UNKNOWN;
31698bfb394SBin Liu 
31798bfb394SBin Liu 	return usb_get_dr_mode_from_string(dr_mode);
31898bfb394SBin Liu }
31998bfb394SBin Liu EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
32098bfb394SBin Liu 
32198bfb394SBin Liu /**
322aa923ef1SMichal Sojka  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
323aa923ef1SMichal Sojka  * for given targeted hosts (non-PC hosts)
324aa923ef1SMichal Sojka  * @np: Pointer to the given device_node
325aa923ef1SMichal Sojka  *
326aa923ef1SMichal Sojka  * The function gets if the targeted hosts support TPL or not
327aa923ef1SMichal Sojka  */
328aa923ef1SMichal Sojka bool of_usb_host_tpl_support(struct device_node *np)
329aa923ef1SMichal Sojka {
330334007d5SSergei Shtylyov 	return of_property_read_bool(np, "tpl-support");
331aa923ef1SMichal Sojka }
332aa923ef1SMichal Sojka EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
333929412d9SLi Jun 
334929412d9SLi Jun /**
335929412d9SLi Jun  * of_usb_update_otg_caps - to update usb otg capabilities according to
336929412d9SLi Jun  * the passed properties in DT.
337929412d9SLi Jun  * @np: Pointer to the given device_node
338929412d9SLi Jun  * @otg_caps: Pointer to the target usb_otg_caps to be set
339929412d9SLi Jun  *
340929412d9SLi Jun  * The function updates the otg capabilities
341929412d9SLi Jun  */
342929412d9SLi Jun int of_usb_update_otg_caps(struct device_node *np,
343929412d9SLi Jun 			struct usb_otg_caps *otg_caps)
344929412d9SLi Jun {
345929412d9SLi Jun 	u32 otg_rev;
346929412d9SLi Jun 
347929412d9SLi Jun 	if (!otg_caps)
348929412d9SLi Jun 		return -EINVAL;
349929412d9SLi Jun 
350929412d9SLi Jun 	if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
351929412d9SLi Jun 		switch (otg_rev) {
352929412d9SLi Jun 		case 0x0100:
353929412d9SLi Jun 		case 0x0120:
354929412d9SLi Jun 		case 0x0130:
355929412d9SLi Jun 		case 0x0200:
356929412d9SLi Jun 			/* Choose the lesser one if it's already been set */
357929412d9SLi Jun 			if (otg_caps->otg_rev)
358929412d9SLi Jun 				otg_caps->otg_rev = min_t(u16, otg_rev,
359929412d9SLi Jun 							otg_caps->otg_rev);
360929412d9SLi Jun 			else
361929412d9SLi Jun 				otg_caps->otg_rev = otg_rev;
362929412d9SLi Jun 			break;
363929412d9SLi Jun 		default:
364d9241ff2SRob Herring 			pr_err("%pOF: unsupported otg-rev: 0x%x\n",
365d9241ff2SRob Herring 						np, otg_rev);
366929412d9SLi Jun 			return -EINVAL;
367929412d9SLi Jun 		}
368929412d9SLi Jun 	} else {
369929412d9SLi Jun 		/*
370929412d9SLi Jun 		 * otg-rev is mandatory for otg properties, if not passed
371929412d9SLi Jun 		 * we set it to be 0 and assume it's a legacy otg device.
372929412d9SLi Jun 		 * Non-dt platform can set it afterwards.
373929412d9SLi Jun 		 */
374929412d9SLi Jun 		otg_caps->otg_rev = 0;
375929412d9SLi Jun 	}
376929412d9SLi Jun 
377334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "hnp-disable"))
378929412d9SLi Jun 		otg_caps->hnp_support = false;
379334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "srp-disable"))
380929412d9SLi Jun 		otg_caps->srp_support = false;
381334007d5SSergei Shtylyov 	if (of_property_read_bool(np, "adp-disable") ||
382929412d9SLi Jun 				(otg_caps->otg_rev < 0x0200))
383929412d9SLi Jun 		otg_caps->adp_support = false;
384929412d9SLi Jun 
385929412d9SLi Jun 	return 0;
386929412d9SLi Jun }
387929412d9SLi Jun EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
388929412d9SLi Jun 
389fa827966SYoshihiro Shimoda /**
390fa827966SYoshihiro Shimoda  * usb_of_get_companion_dev - Find the companion device
391fa827966SYoshihiro Shimoda  * @dev: the device pointer to find a companion
392fa827966SYoshihiro Shimoda  *
393fa827966SYoshihiro Shimoda  * Find the companion device from platform bus.
394fa827966SYoshihiro Shimoda  *
395fa827966SYoshihiro Shimoda  * Takes a reference to the returned struct device which needs to be dropped
396fa827966SYoshihiro Shimoda  * after use.
397fa827966SYoshihiro Shimoda  *
398fa827966SYoshihiro Shimoda  * Return: On success, a pointer to the companion device, %NULL on failure.
399fa827966SYoshihiro Shimoda  */
400fa827966SYoshihiro Shimoda struct device *usb_of_get_companion_dev(struct device *dev)
401fa827966SYoshihiro Shimoda {
402fa827966SYoshihiro Shimoda 	struct device_node *node;
403fa827966SYoshihiro Shimoda 	struct platform_device *pdev = NULL;
404fa827966SYoshihiro Shimoda 
405fa827966SYoshihiro Shimoda 	node = of_parse_phandle(dev->of_node, "companion", 0);
406fa827966SYoshihiro Shimoda 	if (node)
407fa827966SYoshihiro Shimoda 		pdev = of_find_device_by_node(node);
408fa827966SYoshihiro Shimoda 
409fa827966SYoshihiro Shimoda 	of_node_put(node);
410fa827966SYoshihiro Shimoda 
411fa827966SYoshihiro Shimoda 	return pdev ? &pdev->dev : NULL;
412fa827966SYoshihiro Shimoda }
413fa827966SYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
414aa923ef1SMichal Sojka #endif
415aa923ef1SMichal Sojka 
416812086d3SGreg Kroah-Hartman struct dentry *usb_debug_root;
417812086d3SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_debug_root);
418812086d3SGreg Kroah-Hartman 
419812086d3SGreg Kroah-Hartman static int __init usb_common_init(void)
420812086d3SGreg Kroah-Hartman {
421812086d3SGreg Kroah-Hartman 	usb_debug_root = debugfs_create_dir("usb", NULL);
422812086d3SGreg Kroah-Hartman 	ledtrig_usb_init();
423812086d3SGreg Kroah-Hartman 	return 0;
424812086d3SGreg Kroah-Hartman }
425812086d3SGreg Kroah-Hartman 
426812086d3SGreg Kroah-Hartman static void __exit usb_common_exit(void)
427812086d3SGreg Kroah-Hartman {
428812086d3SGreg Kroah-Hartman 	ledtrig_usb_exit();
429812086d3SGreg Kroah-Hartman 	debugfs_remove_recursive(usb_debug_root);
430812086d3SGreg Kroah-Hartman }
431812086d3SGreg Kroah-Hartman 
432812086d3SGreg Kroah-Hartman subsys_initcall(usb_common_init);
433812086d3SGreg Kroah-Hartman module_exit(usb_common_exit);
434812086d3SGreg Kroah-Hartman 
435aa923ef1SMichal Sojka MODULE_LICENSE("GPL");
436