common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. const char *usb_otg_state_string(enum usb_otg_state state)
  18. {
  19. static const char *const names[] = {
  20. [OTG_STATE_A_IDLE] = "a_idle",
  21. [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
  22. [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
  23. [OTG_STATE_A_HOST] = "a_host",
  24. [OTG_STATE_A_SUSPEND] = "a_suspend",
  25. [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
  26. [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
  27. [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
  28. [OTG_STATE_B_IDLE] = "b_idle",
  29. [OTG_STATE_B_SRP_INIT] = "b_srp_init",
  30. [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
  31. [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
  32. [OTG_STATE_B_HOST] = "b_host",
  33. };
  34. if (state < 0 || state >= ARRAY_SIZE(names))
  35. return "UNDEFINED";
  36. return names[state];
  37. }
  38. EXPORT_SYMBOL_GPL(usb_otg_state_string);
  39. static const char *const speed_names[] = {
  40. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  41. [USB_SPEED_LOW] = "low-speed",
  42. [USB_SPEED_FULL] = "full-speed",
  43. [USB_SPEED_HIGH] = "high-speed",
  44. [USB_SPEED_WIRELESS] = "wireless",
  45. [USB_SPEED_SUPER] = "super-speed",
  46. [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
  47. };
  48. const char *usb_speed_string(enum usb_device_speed speed)
  49. {
  50. if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
  51. speed = USB_SPEED_UNKNOWN;
  52. return speed_names[speed];
  53. }
  54. EXPORT_SYMBOL_GPL(usb_speed_string);
  55. enum usb_device_speed usb_get_maximum_speed(struct device *dev)
  56. {
  57. const char *maximum_speed;
  58. int ret;
  59. ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
  60. if (ret < 0)
  61. return USB_SPEED_UNKNOWN;
  62. ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
  63. return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
  64. }
  65. EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
  66. const char *usb_state_string(enum usb_device_state state)
  67. {
  68. static const char *const names[] = {
  69. [USB_STATE_NOTATTACHED] = "not attached",
  70. [USB_STATE_ATTACHED] = "attached",
  71. [USB_STATE_POWERED] = "powered",
  72. [USB_STATE_RECONNECTING] = "reconnecting",
  73. [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
  74. [USB_STATE_DEFAULT] = "default",
  75. [USB_STATE_ADDRESS] = "addressed",
  76. [USB_STATE_CONFIGURED] = "configured",
  77. [USB_STATE_SUSPENDED] = "suspended",
  78. };
  79. if (state < 0 || state >= ARRAY_SIZE(names))
  80. return "UNKNOWN";
  81. return names[state];
  82. }
  83. EXPORT_SYMBOL_GPL(usb_state_string);
  84. static const char *const usb_dr_modes[] = {
  85. [USB_DR_MODE_UNKNOWN] = "",
  86. [USB_DR_MODE_HOST] = "host",
  87. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  88. [USB_DR_MODE_OTG] = "otg",
  89. };
  90. static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
  91. {
  92. int ret;
  93. ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
  94. return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
  95. }
  96. enum usb_dr_mode usb_get_dr_mode(struct device *dev)
  97. {
  98. const char *dr_mode;
  99. int err;
  100. err = device_property_read_string(dev, "dr_mode", &dr_mode);
  101. if (err < 0)
  102. return USB_DR_MODE_UNKNOWN;
  103. return usb_get_dr_mode_from_string(dr_mode);
  104. }
  105. EXPORT_SYMBOL_GPL(usb_get_dr_mode);
  106. #ifdef CONFIG_OF
  107. /**
  108. * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
  109. * which is associated with the given phy device_node
  110. * @np: Pointer to the given phy device_node
  111. * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
  112. * phys which do not have phy-cells
  113. *
  114. * In dts a usb controller associates with phy devices. The function gets
  115. * the string from property 'dr_mode' of the controller associated with the
  116. * given phy device node, and returns the correspondig enum usb_dr_mode.
  117. */
  118. enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
  119. {
  120. struct device_node *controller = NULL;
  121. struct of_phandle_args args;
  122. const char *dr_mode;
  123. int index;
  124. int err;
  125. do {
  126. controller = of_find_node_with_property(controller, "phys");
  127. index = 0;
  128. do {
  129. if (arg0 == -1) {
  130. args.np = of_parse_phandle(controller, "phys",
  131. index);
  132. args.args_count = 0;
  133. } else {
  134. err = of_parse_phandle_with_args(controller,
  135. "phys", "#phy-cells",
  136. index, &args);
  137. if (err)
  138. break;
  139. }
  140. of_node_put(args.np);
  141. if (args.np == np && (args.args_count == 0 ||
  142. args.args[0] == arg0))
  143. goto finish;
  144. index++;
  145. } while (args.np);
  146. } while (controller);
  147. finish:
  148. err = of_property_read_string(controller, "dr_mode", &dr_mode);
  149. of_node_put(controller);
  150. if (err < 0)
  151. return USB_DR_MODE_UNKNOWN;
  152. return usb_get_dr_mode_from_string(dr_mode);
  153. }
  154. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
  155. /**
  156. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  157. * for given targeted hosts (non-PC hosts)
  158. * @np: Pointer to the given device_node
  159. *
  160. * The function gets if the targeted hosts support TPL or not
  161. */
  162. bool of_usb_host_tpl_support(struct device_node *np)
  163. {
  164. return of_property_read_bool(np, "tpl-support");
  165. }
  166. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  167. /**
  168. * of_usb_update_otg_caps - to update usb otg capabilities according to
  169. * the passed properties in DT.
  170. * @np: Pointer to the given device_node
  171. * @otg_caps: Pointer to the target usb_otg_caps to be set
  172. *
  173. * The function updates the otg capabilities
  174. */
  175. int of_usb_update_otg_caps(struct device_node *np,
  176. struct usb_otg_caps *otg_caps)
  177. {
  178. u32 otg_rev;
  179. if (!otg_caps)
  180. return -EINVAL;
  181. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  182. switch (otg_rev) {
  183. case 0x0100:
  184. case 0x0120:
  185. case 0x0130:
  186. case 0x0200:
  187. /* Choose the lesser one if it's already been set */
  188. if (otg_caps->otg_rev)
  189. otg_caps->otg_rev = min_t(u16, otg_rev,
  190. otg_caps->otg_rev);
  191. else
  192. otg_caps->otg_rev = otg_rev;
  193. break;
  194. default:
  195. pr_err("%pOF: unsupported otg-rev: 0x%x\n",
  196. np, otg_rev);
  197. return -EINVAL;
  198. }
  199. } else {
  200. /*
  201. * otg-rev is mandatory for otg properties, if not passed
  202. * we set it to be 0 and assume it's a legacy otg device.
  203. * Non-dt platform can set it afterwards.
  204. */
  205. otg_caps->otg_rev = 0;
  206. }
  207. if (of_property_read_bool(np, "hnp-disable"))
  208. otg_caps->hnp_support = false;
  209. if (of_property_read_bool(np, "srp-disable"))
  210. otg_caps->srp_support = false;
  211. if (of_property_read_bool(np, "adp-disable") ||
  212. (otg_caps->otg_rev < 0x0200))
  213. otg_caps->adp_support = false;
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  217. /**
  218. * usb_of_get_companion_dev - Find the companion device
  219. * @dev: the device pointer to find a companion
  220. *
  221. * Find the companion device from platform bus.
  222. *
  223. * Takes a reference to the returned struct device which needs to be dropped
  224. * after use.
  225. *
  226. * Return: On success, a pointer to the companion device, %NULL on failure.
  227. */
  228. struct device *usb_of_get_companion_dev(struct device *dev)
  229. {
  230. struct device_node *node;
  231. struct platform_device *pdev = NULL;
  232. node = of_parse_phandle(dev->of_node, "companion", 0);
  233. if (node)
  234. pdev = of_find_device_by_node(node);
  235. of_node_put(node);
  236. return pdev ? &pdev->dev : NULL;
  237. }
  238. EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
  239. #endif
  240. MODULE_LICENSE("GPL");