otg.h 6.1 KB

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