common.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 err;
  62. int i;
  63. err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
  64. if (err < 0)
  65. return USB_SPEED_UNKNOWN;
  66. for (i = 0; i < ARRAY_SIZE(speed_names); i++)
  67. if (strcmp(maximum_speed, speed_names[i]) == 0)
  68. return i;
  69. return USB_SPEED_UNKNOWN;
  70. }
  71. EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
  72. const char *usb_state_string(enum usb_device_state state)
  73. {
  74. static const char *const names[] = {
  75. [USB_STATE_NOTATTACHED] = "not attached",
  76. [USB_STATE_ATTACHED] = "attached",
  77. [USB_STATE_POWERED] = "powered",
  78. [USB_STATE_RECONNECTING] = "reconnecting",
  79. [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
  80. [USB_STATE_DEFAULT] = "default",
  81. [USB_STATE_ADDRESS] = "addressed",
  82. [USB_STATE_CONFIGURED] = "configured",
  83. [USB_STATE_SUSPENDED] = "suspended",
  84. };
  85. if (state < 0 || state >= ARRAY_SIZE(names))
  86. return "UNKNOWN";
  87. return names[state];
  88. }
  89. EXPORT_SYMBOL_GPL(usb_state_string);
  90. static const char *const usb_dr_modes[] = {
  91. [USB_DR_MODE_UNKNOWN] = "",
  92. [USB_DR_MODE_HOST] = "host",
  93. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  94. [USB_DR_MODE_OTG] = "otg",
  95. };
  96. static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
  97. {
  98. int i;
  99. for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
  100. if (!strcmp(usb_dr_modes[i], str))
  101. return i;
  102. return USB_DR_MODE_UNKNOWN;
  103. }
  104. enum usb_dr_mode usb_get_dr_mode(struct device *dev)
  105. {
  106. const char *dr_mode;
  107. int err;
  108. err = device_property_read_string(dev, "dr_mode", &dr_mode);
  109. if (err < 0)
  110. return USB_DR_MODE_UNKNOWN;
  111. return usb_get_dr_mode_from_string(dr_mode);
  112. }
  113. EXPORT_SYMBOL_GPL(usb_get_dr_mode);
  114. #ifdef CONFIG_OF
  115. /**
  116. * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
  117. * which is associated with the given phy device_node
  118. * @np: Pointer to the given phy device_node
  119. *
  120. * In dts a usb controller associates with phy devices. The function gets
  121. * the string from property 'dr_mode' of the controller associated with the
  122. * given phy device node, and returns the correspondig enum usb_dr_mode.
  123. */
  124. enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
  125. {
  126. struct device_node *controller = NULL;
  127. struct device_node *phy;
  128. const char *dr_mode;
  129. int index;
  130. int err;
  131. do {
  132. controller = of_find_node_with_property(controller, "phys");
  133. index = 0;
  134. do {
  135. phy = of_parse_phandle(controller, "phys", index);
  136. of_node_put(phy);
  137. if (phy == phy_np)
  138. goto finish;
  139. index++;
  140. } while (phy);
  141. } while (controller);
  142. finish:
  143. err = of_property_read_string(controller, "dr_mode", &dr_mode);
  144. of_node_put(controller);
  145. if (err < 0)
  146. return USB_DR_MODE_UNKNOWN;
  147. return usb_get_dr_mode_from_string(dr_mode);
  148. }
  149. EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
  150. /**
  151. * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
  152. * for given targeted hosts (non-PC hosts)
  153. * @np: Pointer to the given device_node
  154. *
  155. * The function gets if the targeted hosts support TPL or not
  156. */
  157. bool of_usb_host_tpl_support(struct device_node *np)
  158. {
  159. if (of_find_property(np, "tpl-support", NULL))
  160. return true;
  161. return false;
  162. }
  163. EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
  164. /**
  165. * of_usb_update_otg_caps - to update usb otg capabilities according to
  166. * the passed properties in DT.
  167. * @np: Pointer to the given device_node
  168. * @otg_caps: Pointer to the target usb_otg_caps to be set
  169. *
  170. * The function updates the otg capabilities
  171. */
  172. int of_usb_update_otg_caps(struct device_node *np,
  173. struct usb_otg_caps *otg_caps)
  174. {
  175. u32 otg_rev;
  176. if (!otg_caps)
  177. return -EINVAL;
  178. if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
  179. switch (otg_rev) {
  180. case 0x0100:
  181. case 0x0120:
  182. case 0x0130:
  183. case 0x0200:
  184. /* Choose the lesser one if it's already been set */
  185. if (otg_caps->otg_rev)
  186. otg_caps->otg_rev = min_t(u16, otg_rev,
  187. otg_caps->otg_rev);
  188. else
  189. otg_caps->otg_rev = otg_rev;
  190. break;
  191. default:
  192. pr_err("%s: unsupported otg-rev: 0x%x\n",
  193. np->full_name, otg_rev);
  194. return -EINVAL;
  195. }
  196. } else {
  197. /*
  198. * otg-rev is mandatory for otg properties, if not passed
  199. * we set it to be 0 and assume it's a legacy otg device.
  200. * Non-dt platform can set it afterwards.
  201. */
  202. otg_caps->otg_rev = 0;
  203. }
  204. if (of_find_property(np, "hnp-disable", NULL))
  205. otg_caps->hnp_support = false;
  206. if (of_find_property(np, "srp-disable", NULL))
  207. otg_caps->srp_support = false;
  208. if (of_find_property(np, "adp-disable", NULL) ||
  209. (otg_caps->otg_rev < 0x0200))
  210. otg_caps->adp_support = false;
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
  214. #endif
  215. MODULE_LICENSE("GPL");