otg.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. struct usb_phy;
  39. /* for transceivers connected thru an ULPI interface, the user must
  40. * provide access ops
  41. */
  42. struct usb_phy_io_ops {
  43. int (*read)(struct usb_phy *x, u32 reg);
  44. int (*write)(struct usb_phy *x, u32 val, u32 reg);
  45. };
  46. struct usb_otg {
  47. u8 default_a;
  48. struct usb_phy *phy;
  49. struct usb_bus *host;
  50. struct usb_gadget *gadget;
  51. /* bind/unbind the host controller */
  52. int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
  53. /* bind/unbind the peripheral controller */
  54. int (*set_peripheral)(struct usb_otg *otg,
  55. struct usb_gadget *gadget);
  56. /* effective for A-peripheral, ignored for B devices */
  57. int (*set_vbus)(struct usb_otg *otg, bool enabled);
  58. /* for B devices only: start session with A-Host */
  59. int (*start_srp)(struct usb_otg *otg);
  60. /* start or continue HNP role switch */
  61. int (*start_hnp)(struct usb_otg *otg);
  62. };
  63. /*
  64. * the otg driver needs to interact with both device side and host side
  65. * usb controllers. it decides which controller is active at a given
  66. * moment, using the transceiver, ID signal, HNP and sometimes static
  67. * configuration information (including "board isn't wired for otg").
  68. */
  69. struct usb_phy {
  70. struct device *dev;
  71. const char *label;
  72. unsigned int flags;
  73. enum usb_otg_state state;
  74. enum usb_phy_events last_event;
  75. struct usb_otg *otg;
  76. struct usb_phy_io_ops *io_ops;
  77. void __iomem *io_priv;
  78. /* for notification of usb_phy_events */
  79. struct atomic_notifier_head notifier;
  80. /* to pass extra port status to the root hub */
  81. u16 port_status;
  82. u16 port_change;
  83. /* initialize/shutdown the OTG controller */
  84. int (*init)(struct usb_phy *x);
  85. void (*shutdown)(struct usb_phy *x);
  86. /* effective for B devices, ignored for A-peripheral */
  87. int (*set_power)(struct usb_phy *x,
  88. unsigned mA);
  89. /* for non-OTG B devices: set transceiver into suspend mode */
  90. int (*set_suspend)(struct usb_phy *x,
  91. int suspend);
  92. };
  93. /* for board-specific init logic */
  94. extern int usb_set_transceiver(struct usb_phy *);
  95. #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
  96. /* sometimes transceivers are accessed only through e.g. ULPI */
  97. extern void usb_nop_xceiv_register(void);
  98. extern void usb_nop_xceiv_unregister(void);
  99. #else
  100. static inline void usb_nop_xceiv_register(void)
  101. {
  102. }
  103. static inline void usb_nop_xceiv_unregister(void)
  104. {
  105. }
  106. #endif
  107. /* helpers for direct access thru low-level io interface */
  108. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  109. {
  110. if (x->io_ops && x->io_ops->read)
  111. return x->io_ops->read(x, reg);
  112. return -EINVAL;
  113. }
  114. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  115. {
  116. if (x->io_ops && x->io_ops->write)
  117. return x->io_ops->write(x, val, reg);
  118. return -EINVAL;
  119. }
  120. static inline int
  121. usb_phy_init(struct usb_phy *x)
  122. {
  123. if (x->init)
  124. return x->init(x);
  125. return 0;
  126. }
  127. static inline void
  128. usb_phy_shutdown(struct usb_phy *x)
  129. {
  130. if (x->shutdown)
  131. x->shutdown(x);
  132. }
  133. /* for usb host and peripheral controller drivers */
  134. #ifdef CONFIG_USB_OTG_UTILS
  135. extern struct usb_phy *usb_get_transceiver(void);
  136. extern void usb_put_transceiver(struct usb_phy *);
  137. extern const char *otg_state_string(enum usb_otg_state state);
  138. #else
  139. static inline struct usb_phy *usb_get_transceiver(void)
  140. {
  141. return NULL;
  142. }
  143. static inline void usb_put_transceiver(struct usb_phy *x)
  144. {
  145. }
  146. static inline const char *otg_state_string(enum usb_otg_state state)
  147. {
  148. return NULL;
  149. }
  150. #endif
  151. /* Context: can sleep */
  152. static inline int
  153. otg_start_hnp(struct usb_phy *x)
  154. {
  155. if (x->otg && x->otg->start_hnp)
  156. return x->otg->start_hnp(x->otg);
  157. return -ENOTSUPP;
  158. }
  159. /* Context: can sleep */
  160. static inline int
  161. otg_set_vbus(struct usb_phy *x, bool enabled)
  162. {
  163. if (x->otg && x->otg->set_vbus)
  164. return x->otg->set_vbus(x->otg, enabled);
  165. return -ENOTSUPP;
  166. }
  167. /* for HCDs */
  168. static inline int
  169. otg_set_host(struct usb_phy *x, struct usb_bus *host)
  170. {
  171. if (x->otg && x->otg->set_host)
  172. return x->otg->set_host(x->otg, host);
  173. return -ENOTSUPP;
  174. }
  175. /* for usb peripheral controller drivers */
  176. /* Context: can sleep */
  177. static inline int
  178. otg_set_peripheral(struct usb_phy *x, struct usb_gadget *periph)
  179. {
  180. if (x->otg && x->otg->set_peripheral)
  181. return x->otg->set_peripheral(x->otg, periph);
  182. return -ENOTSUPP;
  183. }
  184. static inline int
  185. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  186. {
  187. if (x && x->set_power)
  188. return x->set_power(x, mA);
  189. return 0;
  190. }
  191. /* Context: can sleep */
  192. static inline int
  193. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  194. {
  195. if (x->set_suspend != NULL)
  196. return x->set_suspend(x, suspend);
  197. else
  198. return 0;
  199. }
  200. static inline int
  201. otg_start_srp(struct usb_phy *x)
  202. {
  203. if (x->otg && x->otg->start_srp)
  204. return x->otg->start_srp(x->otg);
  205. return -ENOTSUPP;
  206. }
  207. /* notifiers */
  208. static inline int
  209. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  210. {
  211. return atomic_notifier_chain_register(&x->notifier, nb);
  212. }
  213. static inline void
  214. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  215. {
  216. atomic_notifier_chain_unregister(&x->notifier, nb);
  217. }
  218. /* for OTG controller drivers (and maybe other stuff) */
  219. extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
  220. #endif /* __LINUX_USB_OTG_H */