otg.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* USB OTG (On The Go) defines */
  2. /*
  3. *
  4. * These APIs may be used between USB controllers. USB device drivers
  5. * (for either host or peripheral roles) don't use these calls; they
  6. * continue to use just usb_device and usb_gadget.
  7. */
  8. #ifndef __LINUX_USB_OTG_H
  9. #define __LINUX_USB_OTG_H
  10. #include <linux/notifier.h>
  11. /* OTG defines lots of enumeration states before device reset */
  12. enum usb_otg_state {
  13. OTG_STATE_UNDEFINED = 0,
  14. /* single-role peripheral, and dual-role default-b */
  15. OTG_STATE_B_IDLE,
  16. OTG_STATE_B_SRP_INIT,
  17. OTG_STATE_B_PERIPHERAL,
  18. /* extra dual-role default-b states */
  19. OTG_STATE_B_WAIT_ACON,
  20. OTG_STATE_B_HOST,
  21. /* dual-role default-a */
  22. OTG_STATE_A_IDLE,
  23. OTG_STATE_A_WAIT_VRISE,
  24. OTG_STATE_A_WAIT_BCON,
  25. OTG_STATE_A_HOST,
  26. OTG_STATE_A_SUSPEND,
  27. OTG_STATE_A_PERIPHERAL,
  28. OTG_STATE_A_WAIT_VFALL,
  29. OTG_STATE_A_VBUS_ERR,
  30. };
  31. enum usb_phy_events {
  32. USB_EVENT_NONE, /* no events or cable disconnected */
  33. USB_EVENT_VBUS, /* vbus valid event */
  34. USB_EVENT_ID, /* id was grounded */
  35. USB_EVENT_CHARGER, /* usb dedicated charger */
  36. USB_EVENT_ENUMERATED, /* gadget driver enumerated */
  37. };
  38. /* associate a type with PHY */
  39. enum usb_phy_type {
  40. USB_PHY_TYPE_UNDEFINED,
  41. USB_PHY_TYPE_USB2,
  42. USB_PHY_TYPE_USB3,
  43. };
  44. struct usb_phy;
  45. /* for transceivers connected thru an ULPI interface, the user must
  46. * provide access ops
  47. */
  48. struct usb_phy_io_ops {
  49. int (*read)(struct usb_phy *x, u32 reg);
  50. int (*write)(struct usb_phy *x, u32 val, u32 reg);
  51. };
  52. struct usb_otg {
  53. u8 default_a;
  54. struct usb_phy *phy;
  55. struct usb_bus *host;
  56. struct usb_gadget *gadget;
  57. /* bind/unbind the host controller */
  58. int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
  59. /* bind/unbind the peripheral controller */
  60. int (*set_peripheral)(struct usb_otg *otg,
  61. struct usb_gadget *gadget);
  62. /* effective for A-peripheral, ignored for B devices */
  63. int (*set_vbus)(struct usb_otg *otg, bool enabled);
  64. /* for B devices only: start session with A-Host */
  65. int (*start_srp)(struct usb_otg *otg);
  66. /* start or continue HNP role switch */
  67. int (*start_hnp)(struct usb_otg *otg);
  68. };
  69. /*
  70. * the otg driver needs to interact with both device side and host side
  71. * usb controllers. it decides which controller is active at a given
  72. * moment, using the transceiver, ID signal, HNP and sometimes static
  73. * configuration information (including "board isn't wired for otg").
  74. */
  75. struct usb_phy {
  76. struct device *dev;
  77. const char *label;
  78. unsigned int flags;
  79. enum usb_phy_type type;
  80. enum usb_otg_state state;
  81. enum usb_phy_events last_event;
  82. struct usb_otg *otg;
  83. struct device *io_dev;
  84. struct usb_phy_io_ops *io_ops;
  85. void __iomem *io_priv;
  86. /* for notification of usb_phy_events */
  87. struct atomic_notifier_head notifier;
  88. /* to pass extra port status to the root hub */
  89. u16 port_status;
  90. u16 port_change;
  91. /* to support controllers that have multiple transceivers */
  92. struct list_head head;
  93. /* initialize/shutdown the OTG controller */
  94. int (*init)(struct usb_phy *x);
  95. void (*shutdown)(struct usb_phy *x);
  96. /* effective for B devices, ignored for A-peripheral */
  97. int (*set_power)(struct usb_phy *x,
  98. unsigned mA);
  99. /* for non-OTG B devices: set transceiver into suspend mode */
  100. int (*set_suspend)(struct usb_phy *x,
  101. int suspend);
  102. /* notify phy connect status change */
  103. int (*notify_connect)(struct usb_phy *x, int port);
  104. int (*notify_disconnect)(struct usb_phy *x, int port);
  105. };
  106. /* for board-specific init logic */
  107. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  108. extern void usb_remove_phy(struct usb_phy *);
  109. #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
  110. /* sometimes transceivers are accessed only through e.g. ULPI */
  111. extern void usb_nop_xceiv_register(void);
  112. extern void usb_nop_xceiv_unregister(void);
  113. #else
  114. static inline void usb_nop_xceiv_register(void)
  115. {
  116. }
  117. static inline void usb_nop_xceiv_unregister(void)
  118. {
  119. }
  120. #endif
  121. /* helpers for direct access thru low-level io interface */
  122. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  123. {
  124. if (x->io_ops && x->io_ops->read)
  125. return x->io_ops->read(x, reg);
  126. return -EINVAL;
  127. }
  128. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  129. {
  130. if (x->io_ops && x->io_ops->write)
  131. return x->io_ops->write(x, val, reg);
  132. return -EINVAL;
  133. }
  134. static inline int
  135. usb_phy_init(struct usb_phy *x)
  136. {
  137. if (x->init)
  138. return x->init(x);
  139. return 0;
  140. }
  141. static inline void
  142. usb_phy_shutdown(struct usb_phy *x)
  143. {
  144. if (x->shutdown)
  145. x->shutdown(x);
  146. }
  147. /* for usb host and peripheral controller drivers */
  148. #ifdef CONFIG_USB_OTG_UTILS
  149. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  150. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  151. enum usb_phy_type type);
  152. extern void usb_put_phy(struct usb_phy *);
  153. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  154. extern const char *otg_state_string(enum usb_otg_state state);
  155. #else
  156. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  157. {
  158. return NULL;
  159. }
  160. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  161. enum usb_phy_type type)
  162. {
  163. return NULL;
  164. }
  165. static inline void usb_put_phy(struct usb_phy *x)
  166. {
  167. }
  168. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  169. {
  170. }
  171. static inline const char *otg_state_string(enum usb_otg_state state)
  172. {
  173. return NULL;
  174. }
  175. #endif
  176. /* Context: can sleep */
  177. static inline int
  178. otg_start_hnp(struct usb_otg *otg)
  179. {
  180. if (otg && otg->start_hnp)
  181. return otg->start_hnp(otg);
  182. return -ENOTSUPP;
  183. }
  184. /* Context: can sleep */
  185. static inline int
  186. otg_set_vbus(struct usb_otg *otg, bool enabled)
  187. {
  188. if (otg && otg->set_vbus)
  189. return otg->set_vbus(otg, enabled);
  190. return -ENOTSUPP;
  191. }
  192. /* for HCDs */
  193. static inline int
  194. otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  195. {
  196. if (otg && otg->set_host)
  197. return otg->set_host(otg, host);
  198. return -ENOTSUPP;
  199. }
  200. /* for usb peripheral controller drivers */
  201. /* Context: can sleep */
  202. static inline int
  203. otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
  204. {
  205. if (otg && otg->set_peripheral)
  206. return otg->set_peripheral(otg, periph);
  207. return -ENOTSUPP;
  208. }
  209. static inline int
  210. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  211. {
  212. if (x && x->set_power)
  213. return x->set_power(x, mA);
  214. return 0;
  215. }
  216. /* Context: can sleep */
  217. static inline int
  218. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  219. {
  220. if (x->set_suspend != NULL)
  221. return x->set_suspend(x, suspend);
  222. else
  223. return 0;
  224. }
  225. static inline int
  226. usb_phy_notify_connect(struct usb_phy *x, int port)
  227. {
  228. if (x->notify_connect)
  229. return x->notify_connect(x, port);
  230. else
  231. return 0;
  232. }
  233. static inline int
  234. usb_phy_notify_disconnect(struct usb_phy *x, int port)
  235. {
  236. if (x->notify_disconnect)
  237. return x->notify_disconnect(x, port);
  238. else
  239. return 0;
  240. }
  241. static inline int
  242. otg_start_srp(struct usb_otg *otg)
  243. {
  244. if (otg && otg->start_srp)
  245. return otg->start_srp(otg);
  246. return -ENOTSUPP;
  247. }
  248. /* notifiers */
  249. static inline int
  250. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  251. {
  252. return atomic_notifier_chain_register(&x->notifier, nb);
  253. }
  254. static inline void
  255. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  256. {
  257. atomic_notifier_chain_unregister(&x->notifier, nb);
  258. }
  259. /* for OTG controller drivers (and maybe other stuff) */
  260. extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
  261. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  262. {
  263. switch (type) {
  264. case USB_PHY_TYPE_USB2:
  265. return "USB2 PHY";
  266. case USB_PHY_TYPE_USB3:
  267. return "USB3 PHY";
  268. default:
  269. return "UNKNOWN PHY TYPE";
  270. }
  271. }
  272. #endif /* __LINUX_USB_OTG_H */