blob: 677b3f01deaa668aa80c25358a87bcde91a3a9a4 [file] [log] [blame]
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001/*
2 * Provides code common for host and device side USB.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2.
7 *
8 * If either host side (ie. CONFIG_USB=y) or device side USB stack
9 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
10 * compiled-in as well. Otherwise, if either of the two stacks is
11 * compiled as module, this file is compiled as module as well.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
Michael Grzeschik1c9af652013-06-13 17:59:55 +030016#include <linux/of.h>
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020017#include <linux/usb/ch9.h>
Michael Grzeschik1c9af652013-06-13 17:59:55 +030018#include <linux/usb/of.h>
Felipe Balbi7009bdd72013-03-07 10:45:56 +020019#include <linux/usb/otg.h>
Bin Liu98bfb392015-11-03 11:51:15 -060020#include <linux/of_platform.h>
Felipe Balbi7009bdd72013-03-07 10:45:56 +020021
22const char *usb_otg_state_string(enum usb_otg_state state)
23{
24 static const char *const names[] = {
25 [OTG_STATE_A_IDLE] = "a_idle",
26 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
27 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
28 [OTG_STATE_A_HOST] = "a_host",
29 [OTG_STATE_A_SUSPEND] = "a_suspend",
30 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
31 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
32 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
33 [OTG_STATE_B_IDLE] = "b_idle",
34 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
35 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
36 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
37 [OTG_STATE_B_HOST] = "b_host",
38 };
39
40 if (state < 0 || state >= ARRAY_SIZE(names))
41 return "UNDEFINED";
42
43 return names[state];
44}
45EXPORT_SYMBOL_GPL(usb_otg_state_string);
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020046
Felipe Balbi1494a1f2013-06-30 13:56:45 +030047static const char *const speed_names[] = {
48 [USB_SPEED_UNKNOWN] = "UNKNOWN",
49 [USB_SPEED_LOW] = "low-speed",
50 [USB_SPEED_FULL] = "full-speed",
51 [USB_SPEED_HIGH] = "high-speed",
52 [USB_SPEED_WIRELESS] = "wireless",
53 [USB_SPEED_SUPER] = "super-speed",
54};
55
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020056const char *usb_speed_string(enum usb_device_speed speed)
57{
Felipe Balbi1494a1f2013-06-30 13:56:45 +030058 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020059 speed = USB_SPEED_UNKNOWN;
Felipe Balbi1494a1f2013-06-30 13:56:45 +030060 return speed_names[speed];
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020061}
62EXPORT_SYMBOL_GPL(usb_speed_string);
63
Heikki Krogerus63863b92015-09-21 11:14:32 +030064enum usb_device_speed usb_get_maximum_speed(struct device *dev)
65{
66 const char *maximum_speed;
Heikki Krogerusefc2cd72016-03-17 14:22:38 -070067 int ret;
Heikki Krogerus63863b92015-09-21 11:14:32 +030068
Heikki Krogerusefc2cd72016-03-17 14:22:38 -070069 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
70 if (ret < 0)
Heikki Krogerus63863b92015-09-21 11:14:32 +030071 return USB_SPEED_UNKNOWN;
72
Heikki Krogerusefc2cd72016-03-17 14:22:38 -070073 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
Heikki Krogerus63863b92015-09-21 11:14:32 +030074
Heikki Krogerusefc2cd72016-03-17 14:22:38 -070075 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
Heikki Krogerus63863b92015-09-21 11:14:32 +030076}
77EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
78
Felipe Balbid1e3d752013-01-24 22:29:48 +020079const char *usb_state_string(enum usb_device_state state)
80{
81 static const char *const names[] = {
82 [USB_STATE_NOTATTACHED] = "not attached",
83 [USB_STATE_ATTACHED] = "attached",
84 [USB_STATE_POWERED] = "powered",
85 [USB_STATE_RECONNECTING] = "reconnecting",
86 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
87 [USB_STATE_DEFAULT] = "default",
Peter Chenf9076e22014-04-09 14:48:58 +080088 [USB_STATE_ADDRESS] = "addressed",
Felipe Balbid1e3d752013-01-24 22:29:48 +020089 [USB_STATE_CONFIGURED] = "configured",
90 [USB_STATE_SUSPENDED] = "suspended",
91 };
92
93 if (state < 0 || state >= ARRAY_SIZE(names))
94 return "UNKNOWN";
95
96 return names[state];
97}
98EXPORT_SYMBOL_GPL(usb_state_string);
99
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300100static const char *const usb_dr_modes[] = {
101 [USB_DR_MODE_UNKNOWN] = "",
102 [USB_DR_MODE_HOST] = "host",
103 [USB_DR_MODE_PERIPHERAL] = "peripheral",
104 [USB_DR_MODE_OTG] = "otg",
105};
106
Bin Liu98bfb392015-11-03 11:51:15 -0600107static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
108{
Heikki Krogerusefc2cd72016-03-17 14:22:38 -0700109 int ret;
Bin Liu98bfb392015-11-03 11:51:15 -0600110
Heikki Krogerusefc2cd72016-03-17 14:22:38 -0700111 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
112 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
Bin Liu98bfb392015-11-03 11:51:15 -0600113}
114
Heikki Krogerus06e71142015-09-21 11:14:34 +0300115enum usb_dr_mode usb_get_dr_mode(struct device *dev)
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300116{
117 const char *dr_mode;
Bin Liu98bfb392015-11-03 11:51:15 -0600118 int err;
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300119
Heikki Krogerus06e71142015-09-21 11:14:34 +0300120 err = device_property_read_string(dev, "dr_mode", &dr_mode);
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300121 if (err < 0)
122 return USB_DR_MODE_UNKNOWN;
123
Bin Liu98bfb392015-11-03 11:51:15 -0600124 return usb_get_dr_mode_from_string(dr_mode);
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300125}
Heikki Krogerus06e71142015-09-21 11:14:34 +0300126EXPORT_SYMBOL_GPL(usb_get_dr_mode);
Felipe Balbi1494a1f2013-06-30 13:56:45 +0300127
Heikki Krogerus06e71142015-09-21 11:14:34 +0300128#ifdef CONFIG_OF
Felipe Balbi1494a1f2013-06-30 13:56:45 +0300129/**
Bin Liu98bfb392015-11-03 11:51:15 -0600130 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
131 * which is associated with the given phy device_node
132 * @np: Pointer to the given phy device_node
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 */
138enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
139{
140 struct device_node *controller = NULL;
141 struct device_node *phy;
142 const char *dr_mode;
143 int index;
144 int err;
145
146 do {
147 controller = of_find_node_with_property(controller, "phys");
148 index = 0;
149 do {
150 phy = of_parse_phandle(controller, "phys", index);
151 of_node_put(phy);
152 if (phy == phy_np)
153 goto finish;
154 index++;
155 } while (phy);
156 } while (controller);
157
158finish:
159 err = of_property_read_string(controller, "dr_mode", &dr_mode);
160 of_node_put(controller);
161
162 if (err < 0)
163 return USB_DR_MODE_UNKNOWN;
164
165 return usb_get_dr_mode_from_string(dr_mode);
166}
167EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
168
169/**
Peter Chen05f8b352014-08-19 09:51:55 +0800170 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
171 * for given targeted hosts (non-PC hosts)
172 * @np: Pointer to the given device_node
173 *
174 * The function gets if the targeted hosts support TPL or not
175 */
176bool of_usb_host_tpl_support(struct device_node *np)
177{
178 if (of_find_property(np, "tpl-support", NULL))
179 return true;
180
181 return false;
182}
183EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
Li Jun929412d2015-07-09 15:18:44 +0800184
185/**
186 * of_usb_update_otg_caps - to update usb otg capabilities according to
187 * the passed properties in DT.
188 * @np: Pointer to the given device_node
189 * @otg_caps: Pointer to the target usb_otg_caps to be set
190 *
191 * The function updates the otg capabilities
192 */
193int of_usb_update_otg_caps(struct device_node *np,
194 struct usb_otg_caps *otg_caps)
195{
196 u32 otg_rev;
197
198 if (!otg_caps)
199 return -EINVAL;
200
201 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
202 switch (otg_rev) {
203 case 0x0100:
204 case 0x0120:
205 case 0x0130:
206 case 0x0200:
207 /* Choose the lesser one if it's already been set */
208 if (otg_caps->otg_rev)
209 otg_caps->otg_rev = min_t(u16, otg_rev,
210 otg_caps->otg_rev);
211 else
212 otg_caps->otg_rev = otg_rev;
213 break;
214 default:
215 pr_err("%s: unsupported otg-rev: 0x%x\n",
216 np->full_name, otg_rev);
217 return -EINVAL;
218 }
219 } else {
220 /*
221 * otg-rev is mandatory for otg properties, if not passed
222 * we set it to be 0 and assume it's a legacy otg device.
223 * Non-dt platform can set it afterwards.
224 */
225 otg_caps->otg_rev = 0;
226 }
227
228 if (of_find_property(np, "hnp-disable", NULL))
229 otg_caps->hnp_support = false;
230 if (of_find_property(np, "srp-disable", NULL))
231 otg_caps->srp_support = false;
232 if (of_find_property(np, "adp-disable", NULL) ||
233 (otg_caps->otg_rev < 0x0200))
234 otg_caps->adp_support = false;
235
236 return 0;
237}
238EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
239
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300240#endif
241
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200242MODULE_LICENSE("GPL");