Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / usb / common / common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Provides code common for host and device side USB.
4  *
5  * If either host side (ie. CONFIG_USB=y) or device side USB stack
6  * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
7  * compiled-in as well.  Otherwise, if either of the two stacks is
8  * compiled as module, this file is compiled as module as well.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/of.h>
16 #include <linux/usb/otg.h>
17 #include <linux/of_platform.h>
18
19 const char *usb_otg_state_string(enum usb_otg_state state)
20 {
21         static const char *const names[] = {
22                 [OTG_STATE_A_IDLE] = "a_idle",
23                 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
24                 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
25                 [OTG_STATE_A_HOST] = "a_host",
26                 [OTG_STATE_A_SUSPEND] = "a_suspend",
27                 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
28                 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
29                 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
30                 [OTG_STATE_B_IDLE] = "b_idle",
31                 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
32                 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
33                 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
34                 [OTG_STATE_B_HOST] = "b_host",
35         };
36
37         if (state < 0 || state >= ARRAY_SIZE(names))
38                 return "UNDEFINED";
39
40         return names[state];
41 }
42 EXPORT_SYMBOL_GPL(usb_otg_state_string);
43
44 static const char *const speed_names[] = {
45         [USB_SPEED_UNKNOWN] = "UNKNOWN",
46         [USB_SPEED_LOW] = "low-speed",
47         [USB_SPEED_FULL] = "full-speed",
48         [USB_SPEED_HIGH] = "high-speed",
49         [USB_SPEED_WIRELESS] = "wireless",
50         [USB_SPEED_SUPER] = "super-speed",
51         [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
52 };
53
54 const char *usb_speed_string(enum usb_device_speed speed)
55 {
56         if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
57                 speed = USB_SPEED_UNKNOWN;
58         return speed_names[speed];
59 }
60 EXPORT_SYMBOL_GPL(usb_speed_string);
61
62 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
63 {
64         const char *maximum_speed;
65         int ret;
66
67         ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
68         if (ret < 0)
69                 return USB_SPEED_UNKNOWN;
70
71         ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
72
73         return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
74 }
75 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
76
77 const char *usb_state_string(enum usb_device_state state)
78 {
79         static const char *const names[] = {
80                 [USB_STATE_NOTATTACHED] = "not attached",
81                 [USB_STATE_ATTACHED] = "attached",
82                 [USB_STATE_POWERED] = "powered",
83                 [USB_STATE_RECONNECTING] = "reconnecting",
84                 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
85                 [USB_STATE_DEFAULT] = "default",
86                 [USB_STATE_ADDRESS] = "addressed",
87                 [USB_STATE_CONFIGURED] = "configured",
88                 [USB_STATE_SUSPENDED] = "suspended",
89         };
90
91         if (state < 0 || state >= ARRAY_SIZE(names))
92                 return "UNKNOWN";
93
94         return names[state];
95 }
96 EXPORT_SYMBOL_GPL(usb_state_string);
97
98 static const char *const usb_dr_modes[] = {
99         [USB_DR_MODE_UNKNOWN]           = "",
100         [USB_DR_MODE_HOST]              = "host",
101         [USB_DR_MODE_PERIPHERAL]        = "peripheral",
102         [USB_DR_MODE_OTG]               = "otg",
103 };
104
105 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
106 {
107         int ret;
108
109         ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
110         return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
111 }
112
113 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
114 {
115         const char *dr_mode;
116         int err;
117
118         err = device_property_read_string(dev, "dr_mode", &dr_mode);
119         if (err < 0)
120                 return USB_DR_MODE_UNKNOWN;
121
122         return usb_get_dr_mode_from_string(dr_mode);
123 }
124 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
125
126 #ifdef CONFIG_OF
127 /**
128  * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
129  * which is associated with the given phy device_node
130  * @np: Pointer to the given phy device_node
131  * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
132  *        phys which do not have phy-cells
133  *
134  * In dts a usb controller associates with phy devices.  The function gets
135  * the string from property 'dr_mode' of the controller associated with the
136  * given phy device node, and returns the correspondig enum usb_dr_mode.
137  */
138 enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
139 {
140         struct device_node *controller = NULL;
141         struct of_phandle_args args;
142         const char *dr_mode;
143         int index;
144         int err;
145
146         do {
147                 controller = of_find_node_with_property(controller, "phys");
148                 if (!of_device_is_available(controller))
149                         continue;
150                 index = 0;
151                 do {
152                         if (arg0 == -1) {
153                                 args.np = of_parse_phandle(controller, "phys",
154                                                         index);
155                                 args.args_count = 0;
156                         } else {
157                                 err = of_parse_phandle_with_args(controller,
158                                                         "phys", "#phy-cells",
159                                                         index, &args);
160                                 if (err)
161                                         break;
162                         }
163
164                         of_node_put(args.np);
165                         if (args.np == np && (args.args_count == 0 ||
166                                               args.args[0] == arg0))
167                                 goto finish;
168                         index++;
169                 } while (args.np);
170         } while (controller);
171
172 finish:
173         err = of_property_read_string(controller, "dr_mode", &dr_mode);
174         of_node_put(controller);
175
176         if (err < 0)
177                 return USB_DR_MODE_UNKNOWN;
178
179         return usb_get_dr_mode_from_string(dr_mode);
180 }
181 EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
182
183 /**
184  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
185  * for given targeted hosts (non-PC hosts)
186  * @np: Pointer to the given device_node
187  *
188  * The function gets if the targeted hosts support TPL or not
189  */
190 bool of_usb_host_tpl_support(struct device_node *np)
191 {
192         return of_property_read_bool(np, "tpl-support");
193 }
194 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
195
196 /**
197  * of_usb_update_otg_caps - to update usb otg capabilities according to
198  * the passed properties in DT.
199  * @np: Pointer to the given device_node
200  * @otg_caps: Pointer to the target usb_otg_caps to be set
201  *
202  * The function updates the otg capabilities
203  */
204 int of_usb_update_otg_caps(struct device_node *np,
205                         struct usb_otg_caps *otg_caps)
206 {
207         u32 otg_rev;
208
209         if (!otg_caps)
210                 return -EINVAL;
211
212         if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
213                 switch (otg_rev) {
214                 case 0x0100:
215                 case 0x0120:
216                 case 0x0130:
217                 case 0x0200:
218                         /* Choose the lesser one if it's already been set */
219                         if (otg_caps->otg_rev)
220                                 otg_caps->otg_rev = min_t(u16, otg_rev,
221                                                         otg_caps->otg_rev);
222                         else
223                                 otg_caps->otg_rev = otg_rev;
224                         break;
225                 default:
226                         pr_err("%pOF: unsupported otg-rev: 0x%x\n",
227                                                 np, otg_rev);
228                         return -EINVAL;
229                 }
230         } else {
231                 /*
232                  * otg-rev is mandatory for otg properties, if not passed
233                  * we set it to be 0 and assume it's a legacy otg device.
234                  * Non-dt platform can set it afterwards.
235                  */
236                 otg_caps->otg_rev = 0;
237         }
238
239         if (of_property_read_bool(np, "hnp-disable"))
240                 otg_caps->hnp_support = false;
241         if (of_property_read_bool(np, "srp-disable"))
242                 otg_caps->srp_support = false;
243         if (of_property_read_bool(np, "adp-disable") ||
244                                 (otg_caps->otg_rev < 0x0200))
245                 otg_caps->adp_support = false;
246
247         return 0;
248 }
249 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
250
251 /**
252  * usb_of_get_companion_dev - Find the companion device
253  * @dev: the device pointer to find a companion
254  *
255  * Find the companion device from platform bus.
256  *
257  * Takes a reference to the returned struct device which needs to be dropped
258  * after use.
259  *
260  * Return: On success, a pointer to the companion device, %NULL on failure.
261  */
262 struct device *usb_of_get_companion_dev(struct device *dev)
263 {
264         struct device_node *node;
265         struct platform_device *pdev = NULL;
266
267         node = of_parse_phandle(dev->of_node, "companion", 0);
268         if (node)
269                 pdev = of_find_device_by_node(node);
270
271         of_node_put(node);
272
273         return pdev ? &pdev->dev : NULL;
274 }
275 EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
276 #endif
277
278 MODULE_LICENSE("GPL");