otg.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 device *io_dev;
  77. struct usb_phy_io_ops *io_ops;
  78. void __iomem *io_priv;
  79. /* for notification of usb_phy_events */
  80. struct atomic_notifier_head notifier;
  81. /* to pass extra port status to the root hub */
  82. u16 port_status;
  83. u16 port_change;
  84. /* initialize/shutdown the OTG controller */
  85. int (*init)(struct usb_phy *x);
  86. void (*shutdown)(struct usb_phy *x);
  87. /* effective for B devices, ignored for A-peripheral */
  88. int (*set_power)(struct usb_phy *x,
  89. unsigned mA);
  90. /* for non-OTG B devices: set transceiver into suspend mode */
  91. int (*set_suspend)(struct usb_phy *x,
  92. int suspend);
  93. };
  94. /* for board-specific init logic */
  95. extern int usb_set_transceiver(struct usb_phy *);
  96. #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
  97. /* sometimes transceivers are accessed only through e.g. ULPI */
  98. extern void usb_nop_xceiv_register(void);
  99. extern void usb_nop_xceiv_unregister(void);
  100. #else
  101. static inline void usb_nop_xceiv_register(void)
  102. {
  103. }
  104. static inline void usb_nop_xceiv_unregister(void)
  105. {
  106. }
  107. #endif
  108. /* helpers for direct access thru low-level io interface */
  109. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  110. {
  111. if (x->io_ops && x->io_ops->read)
  112. return x->io_ops->read(x, reg);
  113. return -EINVAL;
  114. }
  115. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  116. {
  117. if (x->io_ops && x->io_ops->write)
  118. return x->io_ops->write(x, val, reg);
  119. return -EINVAL;
  120. }
  121. static inline int
  122. usb_phy_init(struct usb_phy *x)
  123. {
  124. if (x->init)
  125. return x->init(x);
  126. return 0;
  127. }
  128. static inline void
  129. usb_phy_shutdown(struct usb_phy *x)
  130. {
  131. if (x->shutdown)
  132. x->shutdown(x);
  133. }
  134. /* for usb host and peripheral controller drivers */
  135. #ifdef CONFIG_USB_OTG_UTILS
  136. extern struct usb_phy *usb_get_transceiver(void);
  137. extern void usb_put_transceiver(struct usb_phy *);
  138. extern const char *otg_state_string(enum usb_otg_state state);
  139. #else
  140. static inline struct usb_phy *usb_get_transceiver(void)
  141. {
  142. return NULL;
  143. }
  144. static inline void usb_put_transceiver(struct usb_phy *x)
  145. {
  146. }
  147. static inline const char *otg_state_string(enum usb_otg_state state)
  148. {
  149. return NULL;
  150. }
  151. #endif
  152. /* Context: can sleep */
  153. static inline int
  154. otg_start_hnp(struct usb_otg *otg)
  155. {
  156. if (otg && otg->start_hnp)
  157. return otg->start_hnp(otg);
  158. return -ENOTSUPP;
  159. }
  160. /* Context: can sleep */
  161. static inline int
  162. otg_set_vbus(struct usb_otg *otg, bool enabled)
  163. {
  164. if (otg && otg->set_vbus)
  165. return otg->set_vbus(otg, enabled);
  166. return -ENOTSUPP;
  167. }
  168. /* for HCDs */
  169. static inline int
  170. otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  171. {
  172. if (otg && otg->set_host)
  173. return otg->set_host(otg, host);
  174. return -ENOTSUPP;
  175. }
  176. /* for usb peripheral controller drivers */
  177. /* Context: can sleep */
  178. static inline int
  179. otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
  180. {
  181. if (otg && otg->set_peripheral)
  182. return otg->set_peripheral(otg, periph);
  183. return -ENOTSUPP;
  184. }
  185. static inline int
  186. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  187. {
  188. if (x && x->set_power)
  189. return x->set_power(x, mA);
  190. return 0;
  191. }
  192. /* Context: can sleep */
  193. static inline int
  194. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  195. {
  196. if (x->set_suspend != NULL)
  197. return x->set_suspend(x, suspend);
  198. else
  199. return 0;
  200. }
  201. static inline int
  202. otg_start_srp(struct usb_otg *otg)
  203. {
  204. if (otg && otg->start_srp)
  205. return otg->start_srp(otg);
  206. return -ENOTSUPP;
  207. }
  208. /* notifiers */
  209. static inline int
  210. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  211. {
  212. return atomic_notifier_chain_register(&x->notifier, nb);
  213. }
  214. static inline void
  215. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  216. {
  217. atomic_notifier_chain_unregister(&x->notifier, nb);
  218. }
  219. /* for OTG controller drivers (and maybe other stuff) */
  220. extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
  221. #endif /* __LINUX_USB_OTG_H */