blob: b25a111903ab5f42f4e6e174e16299fe2d00296b [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>
20
21const char *usb_otg_state_string(enum usb_otg_state state)
22{
23 static const char *const names[] = {
24 [OTG_STATE_A_IDLE] = "a_idle",
25 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
26 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
27 [OTG_STATE_A_HOST] = "a_host",
28 [OTG_STATE_A_SUSPEND] = "a_suspend",
29 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
30 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
31 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
32 [OTG_STATE_B_IDLE] = "b_idle",
33 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
34 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
35 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
36 [OTG_STATE_B_HOST] = "b_host",
37 };
38
39 if (state < 0 || state >= ARRAY_SIZE(names))
40 return "UNDEFINED";
41
42 return names[state];
43}
44EXPORT_SYMBOL_GPL(usb_otg_state_string);
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020045
Felipe Balbi1494a1f2013-06-30 13:56:45 +030046static const char *const speed_names[] = {
47 [USB_SPEED_UNKNOWN] = "UNKNOWN",
48 [USB_SPEED_LOW] = "low-speed",
49 [USB_SPEED_FULL] = "full-speed",
50 [USB_SPEED_HIGH] = "high-speed",
51 [USB_SPEED_WIRELESS] = "wireless",
52 [USB_SPEED_SUPER] = "super-speed",
53};
54
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020055const char *usb_speed_string(enum usb_device_speed speed)
56{
Felipe Balbi1494a1f2013-06-30 13:56:45 +030057 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020058 speed = USB_SPEED_UNKNOWN;
Felipe Balbi1494a1f2013-06-30 13:56:45 +030059 return speed_names[speed];
Michal Nazarewicze538dfd2011-08-30 17:11:19 +020060}
61EXPORT_SYMBOL_GPL(usb_speed_string);
62
Heikki Krogerus63863b92015-09-21 11:14:32 +030063enum usb_device_speed usb_get_maximum_speed(struct device *dev)
64{
65 const char *maximum_speed;
66 int err;
67 int i;
68
69 err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
70 if (err < 0)
71 return USB_SPEED_UNKNOWN;
72
73 for (i = 0; i < ARRAY_SIZE(speed_names); i++)
74 if (strcmp(maximum_speed, speed_names[i]) == 0)
75 return i;
76
77 return USB_SPEED_UNKNOWN;
78}
79EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
80
Felipe Balbid1e3d752013-01-24 22:29:48 +020081const char *usb_state_string(enum usb_device_state state)
82{
83 static const char *const names[] = {
84 [USB_STATE_NOTATTACHED] = "not attached",
85 [USB_STATE_ATTACHED] = "attached",
86 [USB_STATE_POWERED] = "powered",
87 [USB_STATE_RECONNECTING] = "reconnecting",
88 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
89 [USB_STATE_DEFAULT] = "default",
Peter Chenf9076e22014-04-09 14:48:58 +080090 [USB_STATE_ADDRESS] = "addressed",
Felipe Balbid1e3d752013-01-24 22:29:48 +020091 [USB_STATE_CONFIGURED] = "configured",
92 [USB_STATE_SUSPENDED] = "suspended",
93 };
94
95 if (state < 0 || state >= ARRAY_SIZE(names))
96 return "UNKNOWN";
97
98 return names[state];
99}
100EXPORT_SYMBOL_GPL(usb_state_string);
101
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300102#ifdef CONFIG_OF
103static const char *const usb_dr_modes[] = {
104 [USB_DR_MODE_UNKNOWN] = "",
105 [USB_DR_MODE_HOST] = "host",
106 [USB_DR_MODE_PERIPHERAL] = "peripheral",
107 [USB_DR_MODE_OTG] = "otg",
108};
109
110/**
111 * of_usb_get_dr_mode - Get dual role mode for given device_node
112 * @np: Pointer to the given device_node
113 *
114 * The function gets phy interface string from property 'dr_mode',
115 * and returns the correspondig enum usb_dr_mode
116 */
117enum usb_dr_mode of_usb_get_dr_mode(struct device_node *np)
118{
119 const char *dr_mode;
120 int err, i;
121
122 err = of_property_read_string(np, "dr_mode", &dr_mode);
123 if (err < 0)
124 return USB_DR_MODE_UNKNOWN;
125
126 for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
127 if (!strcmp(dr_mode, usb_dr_modes[i]))
128 return i;
129
130 return USB_DR_MODE_UNKNOWN;
131}
132EXPORT_SYMBOL_GPL(of_usb_get_dr_mode);
Felipe Balbi1494a1f2013-06-30 13:56:45 +0300133
134/**
Peter Chen05f8b352014-08-19 09:51:55 +0800135 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
136 * for given targeted hosts (non-PC hosts)
137 * @np: Pointer to the given device_node
138 *
139 * The function gets if the targeted hosts support TPL or not
140 */
141bool of_usb_host_tpl_support(struct device_node *np)
142{
143 if (of_find_property(np, "tpl-support", NULL))
144 return true;
145
146 return false;
147}
148EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
Li Jun929412d2015-07-09 15:18:44 +0800149
150/**
151 * of_usb_update_otg_caps - to update usb otg capabilities according to
152 * the passed properties in DT.
153 * @np: Pointer to the given device_node
154 * @otg_caps: Pointer to the target usb_otg_caps to be set
155 *
156 * The function updates the otg capabilities
157 */
158int of_usb_update_otg_caps(struct device_node *np,
159 struct usb_otg_caps *otg_caps)
160{
161 u32 otg_rev;
162
163 if (!otg_caps)
164 return -EINVAL;
165
166 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
167 switch (otg_rev) {
168 case 0x0100:
169 case 0x0120:
170 case 0x0130:
171 case 0x0200:
172 /* Choose the lesser one if it's already been set */
173 if (otg_caps->otg_rev)
174 otg_caps->otg_rev = min_t(u16, otg_rev,
175 otg_caps->otg_rev);
176 else
177 otg_caps->otg_rev = otg_rev;
178 break;
179 default:
180 pr_err("%s: unsupported otg-rev: 0x%x\n",
181 np->full_name, otg_rev);
182 return -EINVAL;
183 }
184 } else {
185 /*
186 * otg-rev is mandatory for otg properties, if not passed
187 * we set it to be 0 and assume it's a legacy otg device.
188 * Non-dt platform can set it afterwards.
189 */
190 otg_caps->otg_rev = 0;
191 }
192
193 if (of_find_property(np, "hnp-disable", NULL))
194 otg_caps->hnp_support = false;
195 if (of_find_property(np, "srp-disable", NULL))
196 otg_caps->srp_support = false;
197 if (of_find_property(np, "adp-disable", NULL) ||
198 (otg_caps->otg_rev < 0x0200))
199 otg_caps->adp_support = false;
200
201 return 0;
202}
203EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
204
Michael Grzeschik1c9af652013-06-13 17:59:55 +0300205#endif
206
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200207MODULE_LICENSE("GPL");