common.c 12 KB

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