common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. if (!of_device_is_available(controller))
  128. continue;
  129. index = 0;
  130. do {
  131. if (arg0 == -1) {
  132. args.np = of_parse_phandle(controller, "phys",
  133. index);
  134. args.args_count = 0;
  135. } else {
  136. err = of_parse_phandle_with_args(controller,
  137. "phys", "#phy-cells",
  138. index, &args);
  139. if (err)
  140. break;
  141. }
  142. of_node_put(args.np);
  143. if (args.np == np && (args.args_count == 0 ||
  144. args.args[0] == arg0))
  145. goto finish;
  146. index++;
  147. } while (args.np);
  148. } while (controller);
  149. finish:
  150. err = of_property_read_string(controller, "dr_mode", &dr_mode);
  151. of_node_put(controller);
  152. if (err < 0)
  153. return USB_DR_MODE_UNKNOWN;
  154. return usb_get_dr_mode_from_string(dr_mode);
  155. }
  156. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
  157. /**
  158. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  159. * for given targeted hosts (non-PC hosts)
  160. * @np: Pointer to the given device_node
  161. *
  162. * The function gets if the targeted hosts support TPL or not
  163. */
  164. bool of_usb_host_tpl_support(struct device_node *np)
  165. {
  166. return of_property_read_bool(np, "tpl-support");
  167. }
  168. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  169. /**
  170. * of_usb_update_otg_caps - to update usb otg capabilities according to
  171. * the passed properties in DT.
  172. * @np: Pointer to the given device_node
  173. * @otg_caps: Pointer to the target usb_otg_caps to be set
  174. *
  175. * The function updates the otg capabilities
  176. */
  177. int of_usb_update_otg_caps(struct device_node *np,
  178. struct usb_otg_caps *otg_caps)
  179. {
  180. u32 otg_rev;
  181. if (!otg_caps)
  182. return -EINVAL;
  183. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  184. switch (otg_rev) {
  185. case 0x0100:
  186. case 0x0120:
  187. case 0x0130:
  188. case 0x0200:
  189. /* Choose the lesser one if it's already been set */
  190. if (otg_caps->otg_rev)
  191. otg_caps->otg_rev = min_t(u16, otg_rev,
  192. otg_caps->otg_rev);
  193. else
  194. otg_caps->otg_rev = otg_rev;
  195. break;
  196. default:
  197. pr_err("%pOF: unsupported otg-rev: 0x%x\n",
  198. np, otg_rev);
  199. return -EINVAL;
  200. }
  201. } else {
  202. /*
  203. * otg-rev is mandatory for otg properties, if not passed
  204. * we set it to be 0 and assume it's a legacy otg device.
  205. * Non-dt platform can set it afterwards.
  206. */
  207. otg_caps->otg_rev = 0;
  208. }
  209. if (of_property_read_bool(np, "hnp-disable"))
  210. otg_caps->hnp_support = false;
  211. if (of_property_read_bool(np, "srp-disable"))
  212. otg_caps->srp_support = false;
  213. if (of_property_read_bool(np, "adp-disable") ||
  214. (otg_caps->otg_rev < 0x0200))
  215. otg_caps->adp_support = false;
  216. return 0;
  217. }
  218. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  219. /**
  220. * usb_of_get_companion_dev - Find the companion device
  221. * @dev: the device pointer to find a companion
  222. *
  223. * Find the companion device from platform bus.
  224. *
  225. * Takes a reference to the returned struct device which needs to be dropped
  226. * after use.
  227. *
  228. * Return: On success, a pointer to the companion device, %NULL on failure.
  229. */
  230. struct device *usb_of_get_companion_dev(struct device *dev)
  231. {
  232. struct device_node *node;
  233. struct platform_device *pdev = NULL;
  234. node = of_parse_phandle(dev->of_node, "companion", 0);
  235. if (node)
  236. pdev = of_find_device_by_node(node);
  237. of_node_put(node);
  238. return pdev ? &pdev->dev : NULL;
  239. }
  240. EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
  241. #endif
  242. MODULE_LICENSE("GPL");