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/platform_device.h>
  14. #include <linux/usb/ch9.h>
  15. #include <linux/usb/of.h>
  16. #include <linux/usb/otg.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/debugfs.h>
  19. #include "common.h"
  20. static const char *const ep_type_names[] = {
  21. [USB_ENDPOINT_XFER_CONTROL] = "ctrl",
  22. [USB_ENDPOINT_XFER_ISOC] = "isoc",
  23. [USB_ENDPOINT_XFER_BULK] = "bulk",
  24. [USB_ENDPOINT_XFER_INT] = "intr",
  25. };
  26. /**
  27. * usb_ep_type_string() - Returns human readable-name of the endpoint type.
  28. * @ep_type: The endpoint type to return human-readable name for. If it's not
  29. * any of the types: USB_ENDPOINT_XFER_{CONTROL, ISOC, BULK, INT},
  30. * usually got by usb_endpoint_type(), the string 'unknown' will be returned.
  31. */
  32. const char *usb_ep_type_string(int ep_type)
  33. {
  34. if (ep_type < 0 || ep_type >= ARRAY_SIZE(ep_type_names))
  35. return "unknown";
  36. return ep_type_names[ep_type];
  37. }
  38. EXPORT_SYMBOL_GPL(usb_ep_type_string);
  39. const char *usb_otg_state_string(enum usb_otg_state state)
  40. {
  41. static const char *const names[] = {
  42. [OTG_STATE_A_IDLE] = "a_idle",
  43. [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
  44. [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
  45. [OTG_STATE_A_HOST] = "a_host",
  46. [OTG_STATE_A_SUSPEND] = "a_suspend",
  47. [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
  48. [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
  49. [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
  50. [OTG_STATE_B_IDLE] = "b_idle",
  51. [OTG_STATE_B_SRP_INIT] = "b_srp_init",
  52. [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
  53. [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
  54. [OTG_STATE_B_HOST] = "b_host",
  55. };
  56. if (state < 0 || state >= ARRAY_SIZE(names))
  57. return "UNDEFINED";
  58. return names[state];
  59. }
  60. EXPORT_SYMBOL_GPL(usb_otg_state_string);
  61. static const char *const speed_names[] = {
  62. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  63. [USB_SPEED_LOW] = "low-speed",
  64. [USB_SPEED_FULL] = "full-speed",
  65. [USB_SPEED_HIGH] = "high-speed",
  66. [USB_SPEED_WIRELESS] = "wireless",
  67. [USB_SPEED_SUPER] = "super-speed",
  68. [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
  69. };
  70. static const char *const ssp_rate[] = {
  71. [USB_SSP_GEN_UNKNOWN] = "UNKNOWN",
  72. [USB_SSP_GEN_2x1] = "super-speed-plus-gen2x1",
  73. [USB_SSP_GEN_1x2] = "super-speed-plus-gen1x2",
  74. [USB_SSP_GEN_2x2] = "super-speed-plus-gen2x2",
  75. };
  76. /**
  77. * usb_speed_string() - Returns human readable-name of the speed.
  78. * @speed: The speed to return human-readable name for. If it's not
  79. * any of the speeds defined in usb_device_speed enum, string for
  80. * USB_SPEED_UNKNOWN will be returned.
  81. */
  82. const char *usb_speed_string(enum usb_device_speed speed)
  83. {
  84. if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
  85. speed = USB_SPEED_UNKNOWN;
  86. return speed_names[speed];
  87. }
  88. EXPORT_SYMBOL_GPL(usb_speed_string);
  89. /**
  90. * usb_get_maximum_speed - Get maximum requested speed for a given USB
  91. * controller.
  92. * @dev: Pointer to the given USB controller device
  93. *
  94. * The function gets the maximum speed string from property "maximum-speed",
  95. * and returns the corresponding enum usb_device_speed.
  96. */
  97. enum usb_device_speed usb_get_maximum_speed(struct device *dev)
  98. {
  99. const char *p = "maximum-speed";
  100. int ret;
  101. ret = device_property_match_property_string(dev, p, ssp_rate, ARRAY_SIZE(ssp_rate));
  102. if (ret > 0)
  103. return USB_SPEED_SUPER_PLUS;
  104. ret = device_property_match_property_string(dev, p, speed_names, ARRAY_SIZE(speed_names));
  105. if (ret > 0)
  106. return ret;
  107. return USB_SPEED_UNKNOWN;
  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;
  245. struct of_phandle_args args;
  246. const char *dr_mode;
  247. int index;
  248. int err;
  249. for_each_node_with_property(controller, "phys") {
  250. if (!of_device_is_available(controller))
  251. continue;
  252. index = 0;
  253. do {
  254. if (arg0 == -1) {
  255. args.np = of_parse_phandle(controller, "phys",
  256. index);
  257. args.args_count = 0;
  258. } else {
  259. err = of_parse_phandle_with_args(controller,
  260. "phys", "#phy-cells",
  261. index, &args);
  262. if (err)
  263. break;
  264. }
  265. of_node_put(args.np);
  266. if (args.np == np && (args.args_count == 0 ||
  267. args.args[0] == arg0))
  268. goto finish;
  269. index++;
  270. } while (args.np);
  271. }
  272. finish:
  273. err = of_property_read_string(controller, "dr_mode", &dr_mode);
  274. of_node_put(controller);
  275. if (err < 0)
  276. return USB_DR_MODE_UNKNOWN;
  277. return usb_get_dr_mode_from_string(dr_mode);
  278. }
  279. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
  280. /**
  281. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  282. * for given targeted hosts (non-PC hosts)
  283. * @np: Pointer to the given device_node
  284. *
  285. * The function gets if the targeted hosts support TPL or not
  286. */
  287. bool of_usb_host_tpl_support(struct device_node *np)
  288. {
  289. return of_property_read_bool(np, "tpl-support");
  290. }
  291. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  292. /**
  293. * of_usb_update_otg_caps - to update usb otg capabilities according to
  294. * the passed properties in DT.
  295. * @np: Pointer to the given device_node
  296. * @otg_caps: Pointer to the target usb_otg_caps to be set
  297. *
  298. * The function updates the otg capabilities
  299. */
  300. int of_usb_update_otg_caps(struct device_node *np,
  301. struct usb_otg_caps *otg_caps)
  302. {
  303. u32 otg_rev;
  304. if (!otg_caps)
  305. return -EINVAL;
  306. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  307. switch (otg_rev) {
  308. case 0x0100:
  309. case 0x0120:
  310. case 0x0130:
  311. case 0x0200:
  312. /* Choose the lesser one if it's already been set */
  313. if (otg_caps->otg_rev)
  314. otg_caps->otg_rev = min_t(u16, otg_rev,
  315. otg_caps->otg_rev);
  316. else
  317. otg_caps->otg_rev = otg_rev;
  318. break;
  319. default:
  320. pr_err("%pOF: unsupported otg-rev: 0x%x\n",
  321. np, otg_rev);
  322. return -EINVAL;
  323. }
  324. } else {
  325. /*
  326. * otg-rev is mandatory for otg properties, if not passed
  327. * we set it to be 0 and assume it's a legacy otg device.
  328. * Non-dt platform can set it afterwards.
  329. */
  330. otg_caps->otg_rev = 0;
  331. }
  332. if (of_property_read_bool(np, "hnp-disable"))
  333. otg_caps->hnp_support = false;
  334. if (of_property_read_bool(np, "srp-disable"))
  335. otg_caps->srp_support = false;
  336. if (of_property_read_bool(np, "adp-disable") ||
  337. (otg_caps->otg_rev < 0x0200))
  338. otg_caps->adp_support = false;
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  342. /**
  343. * usb_of_get_companion_dev - Find the companion device
  344. * @dev: the device pointer to find a companion
  345. *
  346. * Find the companion device from platform bus.
  347. *
  348. * Takes a reference to the returned struct device which needs to be dropped
  349. * after use.
  350. *
  351. * Return: On success, a pointer to the companion device, %NULL on failure.
  352. */
  353. struct device *usb_of_get_companion_dev(struct device *dev)
  354. {
  355. struct device_node *node;
  356. struct platform_device *pdev = NULL;
  357. node = of_parse_phandle(dev->of_node, "companion", 0);
  358. if (node)
  359. pdev = of_find_device_by_node(node);
  360. of_node_put(node);
  361. return pdev ? &pdev->dev : NULL;
  362. }
  363. EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
  364. #endif
  365. struct dentry *usb_debug_root;
  366. EXPORT_SYMBOL_GPL(usb_debug_root);
  367. static int __init usb_common_init(void)
  368. {
  369. usb_debug_root = debugfs_create_dir("usb", NULL);
  370. ledtrig_usb_init();
  371. return 0;
  372. }
  373. static void __exit usb_common_exit(void)
  374. {
  375. ledtrig_usb_exit();
  376. debugfs_remove_recursive(usb_debug_root);
  377. }
  378. subsys_initcall(usb_common_init);
  379. module_exit(usb_common_exit);
  380. MODULE_DESCRIPTION("Common code for host and device side USB");
  381. MODULE_LICENSE("GPL");