blob: 49fbfe8b0f24280ee3b596831346598d9e4c5a24 [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",
Mathias Nyman8a1b2722015-12-10 09:59:25 +020054 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
Felipe Balbi1494a1f2013-06-30 13:56:45 +030055};
56
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020057const char *usb_speed_string(enum usb_device_speed speed)
58{
Felipe Balbi1494a1f2013-06-30 13:56:45 +030059 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020060 speed = USB_SPEED_UNKNOWN;
Felipe Balbi1494a1f2013-06-30 13:56:45 +030061 return speed_names[speed];
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020062}
63EXPORT_SYMBOL_GPL(usb_speed_string);
64
Heikki Krogerus63863b92015-09-21 11:14:32 +030065enum usb_device_speed usb_get_maximum_speed(struct device *dev)
66{
67 const char *maximum_speed;
68 int err;
69 int i;
70
71 err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
72 if (err < 0)
73 return USB_SPEED_UNKNOWN;
74
75 for (i = 0; i < ARRAY_SIZE(speed_names); i++)
76 if (strcmp(maximum_speed, speed_names[i]) == 0)
77 return i;
78
79 return USB_SPEED_UNKNOWN;
80}
81EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
82
Felipe Balbid1e3d752013-01-24 22:29:48 +020083const char *usb_state_string(enum usb_device_state state)
84{
85 static const char *const names[] = {
86 [USB_STATE_NOTATTACHED] = "not attached",
87 [USB_STATE_ATTACHED] = "attached",
88 [USB_STATE_POWERED] = "powered",
89 [USB_STATE_RECONNECTING] = "reconnecting",
90 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
91 [USB_STATE_DEFAULT] = "default",
Peter Chenf9076e22014-04-09 14:48:58 +080092 [USB_STATE_ADDRESS] = "addressed",
Felipe Balbid1e3d752013-01-24 22:29:48 +020093 [USB_STATE_CONFIGURED] = "configured",
94 [USB_STATE_SUSPENDED] = "suspended",
95 };
96
97 if (state < 0 || state >= ARRAY_SIZE(names))
98 return "UNKNOWN";
99
100 return names[state];
101}
102EXPORT_SYMBOL_GPL(usb_state_string);
103
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300104static const char *const usb_dr_modes[] = {
105 [USB_DR_MODE_UNKNOWN] = "",
106 [USB_DR_MODE_HOST] = "host",
107 [USB_DR_MODE_PERIPHERAL] = "peripheral",
108 [USB_DR_MODE_OTG] = "otg",
109};
110
Bin Liu98bfb392015-11-03 11:51:15 -0600111static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
112{
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
116 if (!strcmp(usb_dr_modes[i], str))
117 return i;
118
119 return USB_DR_MODE_UNKNOWN;
120}
121
Heikki Krogerus06e71142015-09-21 11:14:34 +0300122enum usb_dr_mode usb_get_dr_mode(struct device *dev)
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300123{
124 const char *dr_mode;
Bin Liu98bfb392015-11-03 11:51:15 -0600125 int err;
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300126
Heikki Krogerus06e71142015-09-21 11:14:34 +0300127 err = device_property_read_string(dev, "dr_mode", &dr_mode);
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300128 if (err < 0)
129 return USB_DR_MODE_UNKNOWN;
130
Bin Liu98bfb392015-11-03 11:51:15 -0600131 return usb_get_dr_mode_from_string(dr_mode);
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300132}
Heikki Krogerus06e71142015-09-21 11:14:34 +0300133EXPORT_SYMBOL_GPL(usb_get_dr_mode);
Felipe Balbi1494a1f2013-06-30 13:56:45 +0300134
Heikki Krogerus06e71142015-09-21 11:14:34 +0300135#ifdef CONFIG_OF
Felipe Balbi1494a1f2013-06-30 13:56:45 +0300136/**
Bin Liu98bfb392015-11-03 11:51:15 -0600137 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
138 * which is associated with the given phy device_node
139 * @np: Pointer to the given phy device_node
140 *
141 * In dts a usb controller associates with phy devices. The function gets
142 * the string from property 'dr_mode' of the controller associated with the
143 * given phy device node, and returns the correspondig enum usb_dr_mode.
144 */
145enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
146{
147 struct device_node *controller = NULL;
148 struct device_node *phy;
149 const char *dr_mode;
150 int index;
151 int err;
152
153 do {
154 controller = of_find_node_with_property(controller, "phys");
155 index = 0;
156 do {
157 phy = of_parse_phandle(controller, "phys", index);
158 of_node_put(phy);
159 if (phy == phy_np)
160 goto finish;
161 index++;
162 } while (phy);
163 } while (controller);
164
165finish:
166 err = of_property_read_string(controller, "dr_mode", &dr_mode);
167 of_node_put(controller);
168
169 if (err < 0)
170 return USB_DR_MODE_UNKNOWN;
171
172 return usb_get_dr_mode_from_string(dr_mode);
173}
174EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
175
176/**
Peter Chen05f8b352014-08-19 09:51:55 +0800177 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
178 * for given targeted hosts (non-PC hosts)
179 * @np: Pointer to the given device_node
180 *
181 * The function gets if the targeted hosts support TPL or not
182 */
183bool of_usb_host_tpl_support(struct device_node *np)
184{
185 if (of_find_property(np, "tpl-support", NULL))
186 return true;
187
188 return false;
189}
190EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
Li Jun929412d2015-07-09 15:18:44 +0800191
192/**
193 * of_usb_update_otg_caps - to update usb otg capabilities according to
194 * the passed properties in DT.
195 * @np: Pointer to the given device_node
196 * @otg_caps: Pointer to the target usb_otg_caps to be set
197 *
198 * The function updates the otg capabilities
199 */
200int of_usb_update_otg_caps(struct device_node *np,
201 struct usb_otg_caps *otg_caps)
202{
203 u32 otg_rev;
204
205 if (!otg_caps)
206 return -EINVAL;
207
208 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
209 switch (otg_rev) {
210 case 0x0100:
211 case 0x0120:
212 case 0x0130:
213 case 0x0200:
214 /* Choose the lesser one if it's already been set */
215 if (otg_caps->otg_rev)
216 otg_caps->otg_rev = min_t(u16, otg_rev,
217 otg_caps->otg_rev);
218 else
219 otg_caps->otg_rev = otg_rev;
220 break;
221 default:
222 pr_err("%s: unsupported otg-rev: 0x%x\n",
223 np->full_name, otg_rev);
224 return -EINVAL;
225 }
226 } else {
227 /*
228 * otg-rev is mandatory for otg properties, if not passed
229 * we set it to be 0 and assume it's a legacy otg device.
230 * Non-dt platform can set it afterwards.
231 */
232 otg_caps->otg_rev = 0;
233 }
234
235 if (of_find_property(np, "hnp-disable", NULL))
236 otg_caps->hnp_support = false;
237 if (of_find_property(np, "srp-disable", NULL))
238 otg_caps->srp_support = false;
239 if (of_find_property(np, "adp-disable", NULL) ||
240 (otg_caps->otg_rev < 0x0200))
241 otg_caps->adp_support = false;
242
243 return 0;
244}
245EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
246
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300247#endif
248
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200249MODULE_LICENSE("GPL");