common.c 6.8 KB

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