| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Provides code common for host and device side USB. |
| 4 |
* |
| 5 |
* If either host side (ie. CONFIG_USB=y) or device side USB stack |
| 6 |
* (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is |
| 7 |
* compiled-in as well. Otherwise, if either of the two stacks is |
| 8 |
* compiled as module, this file is compiled as module as well. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <linux/kernel.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/of.h> |
| 14 |
#include <linux/platform_device.h> |
| 15 |
#include <linux/usb/ch9.h> |
| 16 |
#include <linux/usb/of.h> |
| 17 |
#include <linux/usb/otg.h> |
| 18 |
#include <linux/of_platform.h> |
| 19 |
#include <linux/debugfs.h> |
| 20 |
#include "common.h" |
| 21 |
|
| 22 |
static const char *const ep_type_names[] = { |
| 23 |
[USB_ENDPOINT_XFER_CONTROL] = "ctrl", |
| 24 |
[USB_ENDPOINT_XFER_ISOC] = "isoc", |
| 25 |
[USB_ENDPOINT_XFER_BULK] = "bulk", |
| 26 |
[USB_ENDPOINT_XFER_INT] = "intr", |
| 27 |
}; |
| 28 |
|
| 29 |
/** |
| 30 |
* usb_ep_type_string() - Returns human readable-name of the endpoint type. |
| 31 |
* @ep_type: The endpoint type to return human-readable name for. If it's not |
| 32 |
* any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT}, |
| 33 |
* usually got by usb_endpoint_type(), the string 'unknown' will be returned. |
| 34 |
*/ |
| 35 |
const char *usb_ep_type_string(int ep_type) |
| 36 |
{ |
| 37 |
if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names)) |
| 38 |
return "unknown"; |
| 39 |
|
| 40 |
return ep_type_names[ep_type]; |
| 41 |
} |
| 42 |
EXPORT_SYMBOL_GPL(usb_ep_type_string); |
| 43 |
|
| 44 |
/** |
| 45 |
* usb_otg_state_string() - returns human readable name of OTG state. |
| 46 |
* @state: the OTG state to return the human readable name of. If it's not |
| 47 |
* any of the states defined in usb_otg_state enum, 'UNDEFINED' will be |
| 48 |
* returned. |
| 49 |
*/ |
| 50 |
const char *usb_otg_state_string(enum usb_otg_state state) |
| 51 |
{ |
| 52 |
static const char *const names[] = { |
| 53 |
[OTG_STATE_A_IDLE] = "a_idle", |
| 54 |
[OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise", |
| 55 |
[OTG_STATE_A_WAIT_BCON] = "a_wait_bcon", |
| 56 |
[OTG_STATE_A_HOST] = "a_host", |
| 57 |
[OTG_STATE_A_SUSPEND] = "a_suspend", |
| 58 |
[OTG_STATE_A_PERIPHERAL] = "a_peripheral", |
| 59 |
[OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall", |
| 60 |
[OTG_STATE_A_VBUS_ERR] = "a_vbus_err", |
| 61 |
[OTG_STATE_B_IDLE] = "b_idle", |
| 62 |
[OTG_STATE_B_SRP_INIT] = "b_srp_init", |
| 63 |
[OTG_STATE_B_PERIPHERAL] = "b_peripheral", |
| 64 |
[OTG_STATE_B_WAIT_ACON] = "b_wait_acon", |
| 65 |
[OTG_STATE_B_HOST] = "b_host", |
| 66 |
}; |
| 67 |
|
| 68 |
if (state < 0 || state >= ARRAY_SIZE(names)) |
| 69 |
return "UNDEFINED"; |
| 70 |
|
| 71 |
return names[state]; |
| 72 |
} |
| 73 |
EXPORT_SYMBOL_GPL(usb_otg_state_string); |
| 74 |
|
| 75 |
static const char *const speed_names[] = { |
| 76 |
[USB_SPEED_UNKNOWN] = "UNKNOWN", |
| 77 |
[USB_SPEED_LOW] = "low-speed", |
| 78 |
[USB_SPEED_FULL] = "full-speed", |
| 79 |
[USB_SPEED_HIGH] = "high-speed", |
| 80 |
[USB_SPEED_WIRELESS] = "wireless", |
| 81 |
[USB_SPEED_SUPER] = "super-speed", |
| 82 |
[USB_SPEED_SUPER_PLUS] = "super-speed-plus", |
| 83 |
}; |
| 84 |
|
| 85 |
static const char *const ssp_rate[] = { |
| 86 |
[USB_SSP_GEN_UNKNOWN] = "UNKNOWN", |
| 87 |
[USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1", |
| 88 |
[USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2", |
| 89 |
[USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2", |
| 90 |
}; |
| 91 |
|
| 92 |
/** |
| 93 |
* usb_speed_string() - Returns human readable-name of the speed. |
| 94 |
* @speed: The speed to return human-readable name for. If it's not |
| 95 |
* any of the speeds defined in usb_device_speed enum, string for |
| 96 |
* USB_SPEED_UNKNOWN will be returned. |
| 97 |
*/ |
| 98 |
const char *usb_speed_string(enum usb_device_speed speed) |
| 99 |
{ |
| 100 |
if (speed < 0 || speed >= ARRAY_SIZE(speed_names)) |
| 101 |
speed = USB_SPEED_UNKNOWN; |
| 102 |
return speed_names[speed]; |
| 103 |
} |
| 104 |
EXPORT_SYMBOL_GPL(usb_speed_string); |
| 105 |
|
| 106 |
/** |
| 107 |
* usb_get_maximum_speed - Get maximum requested speed for a given USB |
| 108 |
* controller. |
| 109 |
* @dev: Pointer to the given USB controller device |
| 110 |
* |
| 111 |
* The function gets the maximum speed string from property "maximum-speed", |
| 112 |
* and returns the corresponding enum usb_device_speed. |
| 113 |
*/ |
| 114 |
enum usb_device_speed usb_get_maximum_speed(struct device *dev) |
| 115 |
{ |
| 116 |
const char *p = "maximum-speed"; |
| 117 |
int ret; |
| 118 |
|
| 119 |
ret = device_property_match_property_string(dev, p, ssp_rate, ARRAY_SIZE(ssp_rate)); |
| 120 |
if (ret > 0) |
| 121 |
return USB_SPEED_SUPER_PLUS; |
| 122 |
|
| 123 |
ret = device_property_match_property_string(dev, p, speed_names, ARRAY_SIZE(speed_names)); |
| 124 |
if (ret > 0) |
| 125 |
return ret; |
| 126 |
|
| 127 |
return USB_SPEED_UNKNOWN; |
| 128 |
} |
| 129 |
EXPORT_SYMBOL_GPL(usb_get_maximum_speed); |
| 130 |
|
| 131 |
/** |
| 132 |
* usb_get_maximum_ssp_rate - Get the signaling rate generation and lane count |
| 133 |
* of a SuperSpeed Plus capable device. |
| 134 |
* @dev: Pointer to the given USB controller device |
| 135 |
* |
| 136 |
* If the string from "maximum-speed" property is super-speed-plus-genXxY where |
| 137 |
* 'X' is the generation number and 'Y' is the number of lanes, then this |
| 138 |
* function returns the corresponding enum usb_ssp_rate. |
| 139 |
*/ |
| 140 |
enum usb_ssp_rate usb_get_maximum_ssp_rate(struct device *dev) |
| 141 |
{ |
| 142 |
const char *maximum_speed; |
| 143 |
int ret; |
| 144 |
|
| 145 |
ret = device_property_read_string(dev, "maximum-speed", &maximum_speed); |
| 146 |
if (ret < 0) |
| 147 |
return USB_SSP_GEN_UNKNOWN; |
| 148 |
|
| 149 |
ret = match_string(ssp_rate, ARRAY_SIZE(ssp_rate), maximum_speed); |
| 150 |
return (ret < 0) ? USB_SSP_GEN_UNKNOWN : ret; |
| 151 |
} |
| 152 |
EXPORT_SYMBOL_GPL(usb_get_maximum_ssp_rate); |
| 153 |
|
| 154 |
/** |
| 155 |
* usb_state_string - Returns human readable name for the state. |
| 156 |
* @state: The state to return a human-readable name for. If it's not |
| 157 |
* any of the states devices in usb_device_state_string enum, |
| 158 |
* the string UNKNOWN will be returned. |
| 159 |
*/ |
| 160 |
const char *usb_state_string(enum usb_device_state state) |
| 161 |
{ |
| 162 |
static const char *const names[] = { |
| 163 |
[USB_STATE_NOTATTACHED] = "not attached", |
| 164 |
[USB_STATE_ATTACHED] = "attached", |
| 165 |
[USB_STATE_POWERED] = "powered", |
| 166 |
[USB_STATE_RECONNECTING] = "reconnecting", |
| 167 |
[USB_STATE_UNAUTHENTICATED] = "unauthenticated", |
| 168 |
[USB_STATE_DEFAULT] = "default", |
| 169 |
[USB_STATE_ADDRESS] = "addressed", |
| 170 |
[USB_STATE_CONFIGURED] = "configured", |
| 171 |
[USB_STATE_SUSPENDED] = "suspended", |
| 172 |
}; |
| 173 |
|
| 174 |
if (state < 0 || state >= ARRAY_SIZE(names)) |
| 175 |
return "UNKNOWN"; |
| 176 |
|
| 177 |
return names[state]; |
| 178 |
} |
| 179 |
EXPORT_SYMBOL_GPL(usb_state_string); |
| 180 |
|
| 181 |
static const char *const usb_dr_modes[] = { |
| 182 |
[USB_DR_MODE_UNKNOWN] = "", |
| 183 |
[USB_DR_MODE_HOST] = "host", |
| 184 |
[USB_DR_MODE_PERIPHERAL] = "peripheral", |
| 185 |
[USB_DR_MODE_OTG] = "otg", |
| 186 |
}; |
| 187 |
|
| 188 |
/** |
| 189 |
* usb_get_dr_mode_from_string() - Get dual role mode for given string |
| 190 |
* @str: String to find the corresponding dual role mode for |
| 191 |
* |
| 192 |
* This function performs a lookup for the given string and returns the |
| 193 |
* corresponding enum usb_dr_mode. If no match for the string could be found, |
| 194 |
* 'USB_DR_MODE_UNKNOWN' is returned. |
| 195 |
*/ |
| 196 |
static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str) |
| 197 |
{ |
| 198 |
int ret; |
| 199 |
|
| 200 |
ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str); |
| 201 |
return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret; |
| 202 |
} |
| 203 |
|
| 204 |
enum usb_dr_mode usb_get_dr_mode(struct device *dev) |
| 205 |
{ |
| 206 |
const char *dr_mode; |
| 207 |
int err; |
| 208 |
|
| 209 |
err = device_property_read_string(dev, "dr_mode", &dr_mode); |
| 210 |
if (err < 0) |
| 211 |
return USB_DR_MODE_UNKNOWN; |
| 212 |
|
| 213 |
return usb_get_dr_mode_from_string(dr_mode); |
| 214 |
} |
| 215 |
EXPORT_SYMBOL_GPL(usb_get_dr_mode); |
| 216 |
|
| 217 |
/** |
| 218 |
* usb_get_role_switch_default_mode - Get default mode for given device |
| 219 |
* @dev: Pointer to the given device |
| 220 |
* |
| 221 |
* The function gets string from property 'role-switch-default-mode', |
| 222 |
* and returns the corresponding enum usb_dr_mode. |
| 223 |
*/ |
| 224 |
enum usb_dr_mode usb_get_role_switch_default_mode(struct device *dev) |
| 225 |
{ |
| 226 |
const char *str; |
| 227 |
int ret; |
| 228 |
|
| 229 |
ret = device_property_read_string(dev, "role-switch-default-mode", &str); |
| 230 |
if (ret < 0) |
| 231 |
return USB_DR_MODE_UNKNOWN; |
| 232 |
|
| 233 |
return usb_get_dr_mode_from_string(str); |
| 234 |
} |
| 235 |
EXPORT_SYMBOL_GPL(usb_get_role_switch_default_mode); |
| 236 |
|
| 237 |
/** |
| 238 |
* usb_decode_interval - Decode bInterval into the time expressed in 1us unit |
| 239 |
* @epd: The descriptor of the endpoint |
| 240 |
* @speed: The speed that the endpoint works as |
| 241 |
* |
| 242 |
* Function returns the interval expressed in 1us unit for servicing |
| 243 |
* endpoint for data transfers. |
| 244 |
*/ |
| 245 |
unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd, |
| 246 |
enum usb_device_speed speed) |
| 247 |
{ |
| 248 |
unsigned int interval = 0; |
| 249 |
|
| 250 |
switch (usb_endpoint_type(epd)) { |
| 251 |
case USB_ENDPOINT_XFER_CONTROL: |
| 252 |
/* uframes per NAK */ |
| 253 |
if (speed == USB_SPEED_HIGH) |
| 254 |
interval = epd->bInterval; |
| 255 |
break; |
| 256 |
case USB_ENDPOINT_XFER_ISOC: |
| 257 |
interval = 1 << (epd->bInterval - 1); |
| 258 |
break; |
| 259 |
case USB_ENDPOINT_XFER_BULK: |
| 260 |
/* uframes per NAK */ |
| 261 |
if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd)) |
| 262 |
interval = epd->bInterval; |
| 263 |
break; |
| 264 |
case USB_ENDPOINT_XFER_INT: |
| 265 |
if (speed >= USB_SPEED_HIGH) |
| 266 |
interval = 1 << (epd->bInterval - 1); |
| 267 |
else |
| 268 |
interval = epd->bInterval; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000; |
| 273 |
|
| 274 |
return interval; |
| 275 |
} |
| 276 |
EXPORT_SYMBOL_GPL(usb_decode_interval); |
| 277 |
|
| 278 |
#ifdef CONFIG_OF |
| 279 |
/** |
| 280 |
* of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device |
| 281 |
* which is associated with the given phy device_node |
| 282 |
* @np: Pointer to the given phy device_node |
| 283 |
* @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for |
| 284 |
* phys which do not have phy-cells |
| 285 |
* |
| 286 |
* In dts a usb controller associates with phy devices. The function gets |
| 287 |
* the string from property 'dr_mode' of the controller associated with the |
| 288 |
* given phy device node, and returns the correspondig enum usb_dr_mode. |
| 289 |
*/ |
| 290 |
enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0) |
| 291 |
{ |
| 292 |
struct device_node *controller; |
| 293 |
struct of_phandle_args args; |
| 294 |
const char *dr_mode; |
| 295 |
int index; |
| 296 |
int err; |
| 297 |
|
| 298 |
for_each_node_with_property(controller, "phys") { |
| 299 |
if (!of_device_is_available(controller)) |
| 300 |
continue; |
| 301 |
index = 0; |
| 302 |
do { |
| 303 |
if (arg0 == -1) { |
| 304 |
args.np = of_parse_phandle(controller, "phys", |
| 305 |
index); |
| 306 |
args.args_count = 0; |
| 307 |
} else { |
| 308 |
err = of_parse_phandle_with_args(controller, |
| 309 |
"phys", "#phy-cells", |
| 310 |
index, &args); |
| 311 |
if (err) |
| 312 |
break; |
| 313 |
} |
| 314 |
|
| 315 |
of_node_put(args.np); |
| 316 |
if (args.np == np && (args.args_count == 0 || |
| 317 |
args.args[0] == arg0)) |
| 318 |
goto finish; |
| 319 |
index++; |
| 320 |
} while (args.np); |
| 321 |
} |
| 322 |
|
| 323 |
finish: |
| 324 |
err = of_property_read_string(controller, "dr_mode", &dr_mode); |
| 325 |
of_node_put(controller); |
| 326 |
|
| 327 |
if (err < 0) |
| 328 |
return USB_DR_MODE_UNKNOWN; |
| 329 |
|
| 330 |
return usb_get_dr_mode_from_string(dr_mode); |
| 331 |
} |
| 332 |
EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy); |
| 333 |
|
| 334 |
/** |
| 335 |
* of_usb_host_tpl_support - to get if Targeted Peripheral List is supported |
| 336 |
* for given targeted hosts (non-PC hosts) |
| 337 |
* @np: Pointer to the given device_node |
| 338 |
* |
| 339 |
* The function gets if the targeted hosts support TPL or not |
| 340 |
*/ |
| 341 |
bool of_usb_host_tpl_support(struct device_node *np) |
| 342 |
{ |
| 343 |
return of_property_read_bool(np, "tpl-support"); |
| 344 |
} |
| 345 |
EXPORT_SYMBOL_GPL(of_usb_host_tpl_support); |
| 346 |
|
| 347 |
/** |
| 348 |
* of_usb_update_otg_caps - to update usb otg capabilities according to |
| 349 |
* the passed properties in DT. |
| 350 |
* @np: Pointer to the given device_node |
| 351 |
* @otg_caps: Pointer to the target usb_otg_caps to be set |
| 352 |
* |
| 353 |
* The function updates the otg capabilities |
| 354 |
*/ |
| 355 |
int of_usb_update_otg_caps(struct device_node *np, |
| 356 |
struct usb_otg_caps *otg_caps) |
| 357 |
{ |
| 358 |
u32 otg_rev; |
| 359 |
|
| 360 |
if (!otg_caps) |
| 361 |
return -EINVAL; |
| 362 |
|
| 363 |
if (!of_property_read_u32(np, "otg-rev", &otg_rev)) { |
| 364 |
switch (otg_rev) { |
| 365 |
case 0x0100: |
| 366 |
case 0x0120: |
| 367 |
case 0x0130: |
| 368 |
case 0x0200: |
| 369 |
/* Choose the lesser one if it's already been set */ |
| 370 |
if (otg_caps->otg_rev) |
| 371 |
otg_caps->otg_rev = min_t(u16, otg_rev, |
| 372 |
otg_caps->otg_rev); |
| 373 |
else |
| 374 |
otg_caps->otg_rev = otg_rev; |
| 375 |
break; |
| 376 |
default: |
| 377 |
pr_err("%pOF: unsupported otg-rev: 0x%x\n", |
| 378 |
np, otg_rev); |
| 379 |
return -EINVAL; |
| 380 |
} |
| 381 |
} else { |
| 382 |
/* |
| 383 |
* otg-rev is mandatory for otg properties, if not passed |
| 384 |
* we set it to be 0 and assume it's a legacy otg device. |
| 385 |
* Non-dt platform can set it afterwards. |
| 386 |
*/ |
| 387 |
otg_caps->otg_rev = 0; |
| 388 |
} |
| 389 |
|
| 390 |
if (of_property_read_bool(np, "hnp-disable")) |
| 391 |
otg_caps->hnp_support = false; |
| 392 |
if (of_property_read_bool(np, "srp-disable")) |
| 393 |
otg_caps->srp_support = false; |
| 394 |
if (of_property_read_bool(np, "adp-disable") || |
| 395 |
(otg_caps->otg_rev < 0x0200)) |
| 396 |
otg_caps->adp_support = false; |
| 397 |
|
| 398 |
return 0; |
| 399 |
} |
| 400 |
EXPORT_SYMBOL_GPL(of_usb_update_otg_caps); |
| 401 |
|
| 402 |
/** |
| 403 |
* usb_of_get_companion_dev - Find the companion device |
| 404 |
* @dev: the device pointer to find a companion |
| 405 |
* |
| 406 |
* Find the companion device from platform bus. |
| 407 |
* |
| 408 |
* Takes a reference to the returned struct device which needs to be dropped |
| 409 |
* after use. |
| 410 |
* |
| 411 |
* Return: On success, a pointer to the companion device, %NULL on failure. |
| 412 |
*/ |
| 413 |
struct device *usb_of_get_companion_dev(struct device *dev) |
| 414 |
{ |
| 415 |
struct device_node *node; |
| 416 |
struct platform_device *pdev = NULL; |
| 417 |
|
| 418 |
node = of_parse_phandle(dev->of_node, "companion", 0); |
| 419 |
if (node) |
| 420 |
pdev = of_find_device_by_node(node); |
| 421 |
|
| 422 |
of_node_put(node); |
| 423 |
|
| 424 |
return pdev ? &pdev->dev : NULL; |
| 425 |
} |
| 426 |
EXPORT_SYMBOL_GPL(usb_of_get_companion_dev); |
| 427 |
#endif |
| 428 |
|
| 429 |
struct dentry *usb_debug_root; |
| 430 |
EXPORT_SYMBOL_GPL(usb_debug_root); |
| 431 |
|
| 432 |
DEFINE_MUTEX(usb_dynids_lock); |
| 433 |
EXPORT_SYMBOL_GPL(usb_dynids_lock); |
| 434 |
|
| 435 |
static int __init usb_common_init(void) |
| 436 |
{ |
| 437 |
usb_debug_root = debugfs_create_dir("usb", NULL); |
| 438 |
ledtrig_usb_init(); |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
static void __exit usb_common_exit(void) |
| 443 |
{ |
| 444 |
ledtrig_usb_exit(); |
| 445 |
debugfs_remove_recursive(usb_debug_root); |
| 446 |
} |
| 447 |
|
| 448 |
subsys_initcall(usb_common_init); |
| 449 |
module_exit(usb_common_exit); |
| 450 |
|
| 451 |
MODULE_DESCRIPTION("Common code for host and device side USB"); |
| 452 |
MODULE_LICENSE("GPL"); |