These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[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 };
54
55 const char *usb_speed_string(enum usb_device_speed speed)
56 {
57         if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
58                 speed = USB_SPEED_UNKNOWN;
59         return speed_names[speed];
60 }
61 EXPORT_SYMBOL_GPL(usb_speed_string);
62
63 enum 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 }
79 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
80
81 const 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",
90                 [USB_STATE_ADDRESS] = "addressed",
91                 [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 }
100 EXPORT_SYMBOL_GPL(usb_state_string);
101
102 static const char *const usb_dr_modes[] = {
103         [USB_DR_MODE_UNKNOWN]           = "",
104         [USB_DR_MODE_HOST]              = "host",
105         [USB_DR_MODE_PERIPHERAL]        = "peripheral",
106         [USB_DR_MODE_OTG]               = "otg",
107 };
108
109 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
110 {
111         const char *dr_mode;
112         int err, i;
113
114         err = device_property_read_string(dev, "dr_mode", &dr_mode);
115         if (err < 0)
116                 return USB_DR_MODE_UNKNOWN;
117
118         for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
119                 if (!strcmp(dr_mode, usb_dr_modes[i]))
120                         return i;
121
122         return USB_DR_MODE_UNKNOWN;
123 }
124 EXPORT_SYMBOL_GPL(usb_get_dr_mode);
125
126 #ifdef CONFIG_OF
127 /**
128  * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
129  * for given targeted hosts (non-PC hosts)
130  * @np: Pointer to the given device_node
131  *
132  * The function gets if the targeted hosts support TPL or not
133  */
134 bool of_usb_host_tpl_support(struct device_node *np)
135 {
136         if (of_find_property(np, "tpl-support", NULL))
137                 return true;
138
139         return false;
140 }
141 EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
142
143 /**
144  * of_usb_update_otg_caps - to update usb otg capabilities according to
145  * the passed properties in DT.
146  * @np: Pointer to the given device_node
147  * @otg_caps: Pointer to the target usb_otg_caps to be set
148  *
149  * The function updates the otg capabilities
150  */
151 int of_usb_update_otg_caps(struct device_node *np,
152                         struct usb_otg_caps *otg_caps)
153 {
154         u32 otg_rev;
155
156         if (!otg_caps)
157                 return -EINVAL;
158
159         if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
160                 switch (otg_rev) {
161                 case 0x0100:
162                 case 0x0120:
163                 case 0x0130:
164                 case 0x0200:
165                         /* Choose the lesser one if it's already been set */
166                         if (otg_caps->otg_rev)
167                                 otg_caps->otg_rev = min_t(u16, otg_rev,
168                                                         otg_caps->otg_rev);
169                         else
170                                 otg_caps->otg_rev = otg_rev;
171                         break;
172                 default:
173                         pr_err("%s: unsupported otg-rev: 0x%x\n",
174                                                 np->full_name, otg_rev);
175                         return -EINVAL;
176                 }
177         } else {
178                 /*
179                  * otg-rev is mandatory for otg properties, if not passed
180                  * we set it to be 0 and assume it's a legacy otg device.
181                  * Non-dt platform can set it afterwards.
182                  */
183                 otg_caps->otg_rev = 0;
184         }
185
186         if (of_find_property(np, "hnp-disable", NULL))
187                 otg_caps->hnp_support = false;
188         if (of_find_property(np, "srp-disable", NULL))
189                 otg_caps->srp_support = false;
190         if (of_find_property(np, "adp-disable", NULL) ||
191                                 (otg_caps->otg_rev < 0x0200))
192                 otg_caps->adp_support = false;
193
194         return 0;
195 }
196 EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
197
198 #endif
199
200 MODULE_LICENSE("GPL");