otg.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. };
  103. /* for board-specific init logic */
  104. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  105. extern void usb_remove_phy(struct usb_phy *);
  106. #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
  107. /* sometimes transceivers are accessed only through e.g. ULPI */
  108. extern void usb_nop_xceiv_register(void);
  109. extern void usb_nop_xceiv_unregister(void);
  110. #else
  111. static inline void usb_nop_xceiv_register(void)
  112. {
  113. }
  114. static inline void usb_nop_xceiv_unregister(void)
  115. {
  116. }
  117. #endif
  118. /* helpers for direct access thru low-level io interface */
  119. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  120. {
  121. if (x->io_ops && x->io_ops->read)
  122. return x->io_ops->read(x, reg);
  123. return -EINVAL;
  124. }
  125. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  126. {
  127. if (x->io_ops && x->io_ops->write)
  128. return x->io_ops->write(x, val, reg);
  129. return -EINVAL;
  130. }
  131. static inline int
  132. usb_phy_init(struct usb_phy *x)
  133. {
  134. if (x->init)
  135. return x->init(x);
  136. return 0;
  137. }
  138. static inline void
  139. usb_phy_shutdown(struct usb_phy *x)
  140. {
  141. if (x->shutdown)
  142. x->shutdown(x);
  143. }
  144. /* for usb host and peripheral controller drivers */
  145. #ifdef CONFIG_USB_OTG_UTILS
  146. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  147. extern void usb_put_phy(struct usb_phy *);
  148. extern const char *otg_state_string(enum usb_otg_state state);
  149. #else
  150. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  151. {
  152. return NULL;
  153. }
  154. static inline void usb_put_phy(struct usb_phy *x)
  155. {
  156. }
  157. static inline const char *otg_state_string(enum usb_otg_state state)
  158. {
  159. return NULL;
  160. }
  161. #endif
  162. /* Context: can sleep */
  163. static inline int
  164. otg_start_hnp(struct usb_otg *otg)
  165. {
  166. if (otg && otg->start_hnp)
  167. return otg->start_hnp(otg);
  168. return -ENOTSUPP;
  169. }
  170. /* Context: can sleep */
  171. static inline int
  172. otg_set_vbus(struct usb_otg *otg, bool enabled)
  173. {
  174. if (otg && otg->set_vbus)
  175. return otg->set_vbus(otg, enabled);
  176. return -ENOTSUPP;
  177. }
  178. /* for HCDs */
  179. static inline int
  180. otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  181. {
  182. if (otg && otg->set_host)
  183. return otg->set_host(otg, host);
  184. return -ENOTSUPP;
  185. }
  186. /* for usb peripheral controller drivers */
  187. /* Context: can sleep */
  188. static inline int
  189. otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
  190. {
  191. if (otg && otg->set_peripheral)
  192. return otg->set_peripheral(otg, periph);
  193. return -ENOTSUPP;
  194. }
  195. static inline int
  196. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  197. {
  198. if (x && x->set_power)
  199. return x->set_power(x, mA);
  200. return 0;
  201. }
  202. /* Context: can sleep */
  203. static inline int
  204. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  205. {
  206. if (x->set_suspend != NULL)
  207. return x->set_suspend(x, suspend);
  208. else
  209. return 0;
  210. }
  211. static inline int
  212. otg_start_srp(struct usb_otg *otg)
  213. {
  214. if (otg && otg->start_srp)
  215. return otg->start_srp(otg);
  216. return -ENOTSUPP;
  217. }
  218. /* notifiers */
  219. static inline int
  220. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  221. {
  222. return atomic_notifier_chain_register(&x->notifier, nb);
  223. }
  224. static inline void
  225. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  226. {
  227. atomic_notifier_chain_unregister(&x->notifier, nb);
  228. }
  229. /* for OTG controller drivers (and maybe other stuff) */
  230. extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
  231. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  232. {
  233. switch (type) {
  234. case USB_PHY_TYPE_USB2:
  235. return "USB2 PHY";
  236. case USB_PHY_TYPE_USB3:
  237. return "USB3 PHY";
  238. default:
  239. return "UNKNOWN PHY TYPE";
  240. }
  241. }
  242. #endif /* __LINUX_USB_OTG_H */