Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / usb / common / common.c
1 /*
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>
16 #include <linux/of.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/of.h>
19 #include <linux/usb/otg.h>
20
21 const 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 }
44 EXPORT_SYMBOL_GPL(usb_otg_state_string);
45
46 static 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         [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
54 };
55
56 const char *usb_speed_string(enum usb_device_speed speed)
57 {
58         if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
59                 speed = USB_SPEED_UNKNOWN;
60         return speed_names[speed];
61 }
62 EXPORT_SYMBOL_GPL(usb_speed_string);
63
64 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
65 {
66         const char *maximum_speed;
67         int err;
68         int i;
69
70         err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
71         if (err < 0)
72                 return USB_SPEED_UNKNOWN;
73
74         for (i = 0; i < ARRAY_SIZE(speed_names); i++)
75                 if (strcmp(maximum_speed, speed_names[i]) == 0)
76                         return i;
77
78         return USB_SPEED_UNKNOWN;
79 }
80 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
81
82 const char *usb_state_string(enum usb_device_state state)
83 {
84         static const char *const names[] = {
85                 [USB_STATE_NOTATTACHED] = "not attached",
86                 [USB_STATE_ATTACHED] = "attached",
87                 [USB_STATE_POWERED] = "powered",
88                 [USB_STATE_RECONNECTING] = "reconnecting",
89                 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
90                 [USB_STATE_DEFAULT] = "default",
91                 [USB_STATE_ADDRESS] = "addressed",
92                 [USB_STATE_CONFIGURED] = "configured",
93                 [USB_STATE_SUSPENDED] = "suspended",
94         };
95
96         if (state < 0 || state >= ARRAY_SIZE(names))
97                 return "UNKNOWN";
98
99         return names[state];
100 }
101 EXPORT_SYMBOL_GPL(usb_state_string);
102
103 static 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 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
111 {
112         const char *dr_mode;
113         int err, i;
114
115         err = device_property_read_string(dev, "dr_mode", &dr_mode);
116         if (err < 0)
117                 return USB_DR_MODE_UNKNOWN;
118
119         for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
120                 if (!strcmp(dr_mode, usb_dr_modes[i]))
121                         return i;
122
123         return USB_DR_MODE_UNKNOWN;
124 }
125 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
126
127 #ifdef CONFIG_OF
128 /**
129  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
130  * for given targeted hosts (non-PC hosts)
131  * @np: Pointer to the given device_node
132  *
133  * The function gets if the targeted hosts support TPL or not
134  */
135 bool of_usb_host_tpl_support(struct device_node *np)
136 {
137         if (of_find_property(np, "tpl-support", NULL))
138                 return true;
139
140         return false;
141 }
142 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
143
144 /**
145  * of_usb_update_otg_caps - to update usb otg capabilities according to
146  * the passed properties in DT.
147  * @np: Pointer to the given device_node
148  * @otg_caps: Pointer to the target usb_otg_caps to be set
149  *
150  * The function updates the otg capabilities
151  */
152 int of_usb_update_otg_caps(struct device_node *np,
153                         struct usb_otg_caps *otg_caps)
154 {
155         u32 otg_rev;
156
157         if (!otg_caps)
158                 return -EINVAL;
159
160         if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
161                 switch (otg_rev) {
162                 case 0x0100:
163                 case 0x0120:
164                 case 0x0130:
165                 case 0x0200:
166                         /* Choose the lesser one if it's already been set */
167                         if (otg_caps->otg_rev)
168                                 otg_caps->otg_rev = min_t(u16, otg_rev,
169                                                         otg_caps->otg_rev);
170                         else
171                                 otg_caps->otg_rev = otg_rev;
172                         break;
173                 default:
174                         pr_err("%s: unsupported otg-rev: 0x%x\n",
175                                                 np->full_name, otg_rev);
176                         return -EINVAL;
177                 }
178         } else {
179                 /*
180                  * otg-rev is mandatory for otg properties, if not passed
181                  * we set it to be 0 and assume it's a legacy otg device.
182                  * Non-dt platform can set it afterwards.
183                  */
184                 otg_caps->otg_rev = 0;
185         }
186
187         if (of_find_property(np, "hnp-disable", NULL))
188                 otg_caps->hnp_support = false;
189         if (of_find_property(np, "srp-disable", NULL))
190                 otg_caps->srp_support = false;
191         if (of_find_property(np, "adp-disable", NULL) ||
192                                 (otg_caps->otg_rev < 0x0200))
193                 otg_caps->adp_support = false;
194
195         return 0;
196 }
197 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
198
199 #endif
200
201 MODULE_LICENSE("GPL");