common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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_decode_interval - Decode bInterval into the time expressed in 1us unit
  178. * @epd: The descriptor of the endpoint
  179. * @speed: The speed that the endpoint works as
  180. *
  181. * Function returns the interval expressed in 1us unit for servicing
  182. * endpoint for data transfers.
  183. */
  184. unsigned int usb_decode_interval(const struct usb_endpoint_descriptor *epd,
  185. enum usb_device_speed speed)
  186. {
  187. unsigned int interval = 0;
  188. switch (usb_endpoint_type(epd)) {
  189. case USB_ENDPOINT_XFER_CONTROL:
  190. /* uframes per NAK */
  191. if (speed == USB_SPEED_HIGH)
  192. interval = epd->bInterval;
  193. break;
  194. case USB_ENDPOINT_XFER_ISOC:
  195. interval = 1 << (epd->bInterval - 1);
  196. break;
  197. case USB_ENDPOINT_XFER_BULK:
  198. /* uframes per NAK */
  199. if (speed == USB_SPEED_HIGH && usb_endpoint_dir_out(epd))
  200. interval = epd->bInterval;
  201. break;
  202. case USB_ENDPOINT_XFER_INT:
  203. if (speed >= USB_SPEED_HIGH)
  204. interval = 1 << (epd->bInterval - 1);
  205. else
  206. interval = epd->bInterval;
  207. break;
  208. }
  209. interval *= (speed >= USB_SPEED_HIGH) ? 125 : 1000;
  210. return interval;
  211. }
  212. EXPORT_SYMBOL_GPL(usb_decode_interval);
  213. #ifdef CONFIG_OF
  214. /**
  215. * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
  216. * which is associated with the given phy device_node
  217. * @np: Pointer to the given phy device_node
  218. * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
  219. * phys which do not have phy-cells
  220. *
  221. * In dts a usb controller associates with phy devices. The function gets
  222. * the string from property 'dr_mode' of the controller associated with the
  223. * given phy device node, and returns the correspondig enum usb_dr_mode.
  224. */
  225. enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
  226. {
  227. struct device_node *controller = NULL;
  228. struct of_phandle_args args;
  229. const char *dr_mode;
  230. int index;
  231. int err;
  232. do {
  233. controller = of_find_node_with_property(controller, "phys");
  234. if (!of_device_is_available(controller))
  235. continue;
  236. index = 0;
  237. do {
  238. if (arg0 == -1) {
  239. args.np = of_parse_phandle(controller, "phys",
  240. index);
  241. args.args_count = 0;
  242. } else {
  243. err = of_parse_phandle_with_args(controller,
  244. "phys", "#phy-cells",
  245. index, &args);
  246. if (err)
  247. break;
  248. }
  249. of_node_put(args.np);
  250. if (args.np == np && (args.args_count == 0 ||
  251. args.args[0] == arg0))
  252. goto finish;
  253. index++;
  254. } while (args.np);
  255. } while (controller);
  256. finish:
  257. err = of_property_read_string(controller, "dr_mode", &dr_mode);
  258. of_node_put(controller);
  259. if (err < 0)
  260. return USB_DR_MODE_UNKNOWN;
  261. return usb_get_dr_mode_from_string(dr_mode);
  262. }
  263. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
  264. /**
  265. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  266. * for given targeted hosts (non-PC hosts)
  267. * @np: Pointer to the given device_node
  268. *
  269. * The function gets if the targeted hosts support TPL or not
  270. */
  271. bool of_usb_host_tpl_support(struct device_node *np)
  272. {
  273. return of_property_read_bool(np, "tpl-support");
  274. }
  275. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  276. /**
  277. * of_usb_update_otg_caps - to update usb otg capabilities according to
  278. * the passed properties in DT.
  279. * @np: Pointer to the given device_node
  280. * @otg_caps: Pointer to the target usb_otg_caps to be set
  281. *
  282. * The function updates the otg capabilities
  283. */
  284. int of_usb_update_otg_caps(struct device_node *np,
  285. struct usb_otg_caps *otg_caps)
  286. {
  287. u32 otg_rev;
  288. if (!otg_caps)
  289. return -EINVAL;
  290. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  291. switch (otg_rev) {
  292. case 0x0100:
  293. case 0x0120:
  294. case 0x0130:
  295. case 0x0200:
  296. /* Choose the lesser one if it's already been set */
  297. if (otg_caps->otg_rev)
  298. otg_caps->otg_rev = min_t(u16, otg_rev,
  299. otg_caps->otg_rev);
  300. else
  301. otg_caps->otg_rev = otg_rev;
  302. break;
  303. default:
  304. pr_err("%pOF: unsupported otg-rev: 0x%x\n",
  305. np, otg_rev);
  306. return -EINVAL;
  307. }
  308. } else {
  309. /*
  310. * otg-rev is mandatory for otg properties, if not passed
  311. * we set it to be 0 and assume it's a legacy otg device.
  312. * Non-dt platform can set it afterwards.
  313. */
  314. otg_caps->otg_rev = 0;
  315. }
  316. if (of_property_read_bool(np, "hnp-disable"))
  317. otg_caps->hnp_support = false;
  318. if (of_property_read_bool(np, "srp-disable"))
  319. otg_caps->srp_support = false;
  320. if (of_property_read_bool(np, "adp-disable") ||
  321. (otg_caps->otg_rev < 0x0200))
  322. otg_caps->adp_support = false;
  323. return 0;
  324. }
  325. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  326. /**
  327. * usb_of_get_companion_dev - Find the companion device
  328. * @dev: the device pointer to find a companion
  329. *
  330. * Find the companion device from platform bus.
  331. *
  332. * Takes a reference to the returned struct device which needs to be dropped
  333. * after use.
  334. *
  335. * Return: On success, a pointer to the companion device, %NULL on failure.
  336. */
  337. struct device *usb_of_get_companion_dev(struct device *dev)
  338. {
  339. struct device_node *node;
  340. struct platform_device *pdev = NULL;
  341. node = of_parse_phandle(dev->of_node, "companion", 0);
  342. if (node)
  343. pdev = of_find_device_by_node(node);
  344. of_node_put(node);
  345. return pdev ? &pdev->dev : NULL;
  346. }
  347. EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
  348. #endif
  349. struct dentry *usb_debug_root;
  350. EXPORT_SYMBOL_GPL(usb_debug_root);
  351. static int __init usb_common_init(void)
  352. {
  353. usb_debug_root = debugfs_create_dir("usb", NULL);
  354. ledtrig_usb_init();
  355. return 0;
  356. }
  357. static void __exit usb_common_exit(void)
  358. {
  359. ledtrig_usb_exit();
  360. debugfs_remove_recursive(usb_debug_root);
  361. }
  362. subsys_initcall(usb_common_init);
  363. module_exit(usb_common_exit);
  364. MODULE_LICENSE("GPL");