otg-fsm.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the
  5. * Free Software Foundation; either version 2 of the License, or (at your
  6. * option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #ifndef __LINUX_USB_OTG_FSM_H
  18. #define __LINUX_USB_OTG_FSM_H
  19. #include <linux/mutex.h>
  20. #include <linux/errno.h>
  21. #undef VERBOSE
  22. #ifdef VERBOSE
  23. #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \
  24. __func__, ## args)
  25. #else
  26. #define VDBG(stuff...) do {} while (0)
  27. #endif
  28. #ifdef VERBOSE
  29. #define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__)
  30. #else
  31. #define MPC_LOC do {} while (0)
  32. #endif
  33. #define PROTO_UNDEF (0)
  34. #define PROTO_HOST (1)
  35. #define PROTO_GADGET (2)
  36. enum otg_fsm_timer {
  37. /* Standard OTG timers */
  38. A_WAIT_VRISE,
  39. A_WAIT_VFALL,
  40. A_WAIT_BCON,
  41. A_AIDL_BDIS,
  42. B_ASE0_BRST,
  43. A_BIDL_ADIS,
  44. /* Auxiliary timers */
  45. B_SE0_SRP,
  46. B_SRP_FAIL,
  47. A_WAIT_ENUM,
  48. NUM_OTG_FSM_TIMERS,
  49. };
  50. /* OTG state machine according to the OTG spec */
  51. struct otg_fsm {
  52. /* Input */
  53. int id;
  54. int adp_change;
  55. int power_up;
  56. int test_device;
  57. int a_bus_drop;
  58. int a_bus_req;
  59. int a_srp_det;
  60. int a_vbus_vld;
  61. int b_conn;
  62. int a_bus_resume;
  63. int a_bus_suspend;
  64. int a_conn;
  65. int b_bus_req;
  66. int b_se0_srp;
  67. int b_ssend_srp;
  68. int b_sess_vld;
  69. /* Auxilary inputs */
  70. int a_sess_vld;
  71. int b_bus_resume;
  72. int b_bus_suspend;
  73. /* Output */
  74. int data_pulse;
  75. int drv_vbus;
  76. int loc_conn;
  77. int loc_sof;
  78. int adp_prb;
  79. int adp_sns;
  80. /* Internal variables */
  81. int a_set_b_hnp_en;
  82. int b_srp_done;
  83. int b_hnp_enable;
  84. int a_clr_err;
  85. /* Informative variables */
  86. int a_bus_drop_inf;
  87. int a_bus_req_inf;
  88. int a_clr_err_inf;
  89. int b_bus_req_inf;
  90. /* Auxilary informative variables */
  91. int a_suspend_req_inf;
  92. /* Timeout indicator for timers */
  93. int a_wait_vrise_tmout;
  94. int a_wait_vfall_tmout;
  95. int a_wait_bcon_tmout;
  96. int a_aidl_bdis_tmout;
  97. int b_ase0_brst_tmout;
  98. int a_bidl_adis_tmout;
  99. struct otg_fsm_ops *ops;
  100. struct usb_otg *otg;
  101. /* Current usb protocol used: 0:undefine; 1:host; 2:client */
  102. int protocol;
  103. struct mutex lock;
  104. };
  105. struct otg_fsm_ops {
  106. void (*chrg_vbus)(struct otg_fsm *fsm, int on);
  107. void (*drv_vbus)(struct otg_fsm *fsm, int on);
  108. void (*loc_conn)(struct otg_fsm *fsm, int on);
  109. void (*loc_sof)(struct otg_fsm *fsm, int on);
  110. void (*start_pulse)(struct otg_fsm *fsm);
  111. void (*start_adp_prb)(struct otg_fsm *fsm);
  112. void (*start_adp_sns)(struct otg_fsm *fsm);
  113. void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
  114. void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
  115. int (*start_host)(struct otg_fsm *fsm, int on);
  116. int (*start_gadget)(struct otg_fsm *fsm, int on);
  117. };
  118. static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
  119. {
  120. if (!fsm->ops->chrg_vbus)
  121. return -EOPNOTSUPP;
  122. fsm->ops->chrg_vbus(fsm, on);
  123. return 0;
  124. }
  125. static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
  126. {
  127. if (!fsm->ops->drv_vbus)
  128. return -EOPNOTSUPP;
  129. if (fsm->drv_vbus != on) {
  130. fsm->drv_vbus = on;
  131. fsm->ops->drv_vbus(fsm, on);
  132. }
  133. return 0;
  134. }
  135. static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
  136. {
  137. if (!fsm->ops->loc_conn)
  138. return -EOPNOTSUPP;
  139. if (fsm->loc_conn != on) {
  140. fsm->loc_conn = on;
  141. fsm->ops->loc_conn(fsm, on);
  142. }
  143. return 0;
  144. }
  145. static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
  146. {
  147. if (!fsm->ops->loc_sof)
  148. return -EOPNOTSUPP;
  149. if (fsm->loc_sof != on) {
  150. fsm->loc_sof = on;
  151. fsm->ops->loc_sof(fsm, on);
  152. }
  153. return 0;
  154. }
  155. static inline int otg_start_pulse(struct otg_fsm *fsm)
  156. {
  157. if (!fsm->ops->start_pulse)
  158. return -EOPNOTSUPP;
  159. if (!fsm->data_pulse) {
  160. fsm->data_pulse = 1;
  161. fsm->ops->start_pulse(fsm);
  162. }
  163. return 0;
  164. }
  165. static inline int otg_start_adp_prb(struct otg_fsm *fsm)
  166. {
  167. if (!fsm->ops->start_adp_prb)
  168. return -EOPNOTSUPP;
  169. if (!fsm->adp_prb) {
  170. fsm->adp_sns = 0;
  171. fsm->adp_prb = 1;
  172. fsm->ops->start_adp_prb(fsm);
  173. }
  174. return 0;
  175. }
  176. static inline int otg_start_adp_sns(struct otg_fsm *fsm)
  177. {
  178. if (!fsm->ops->start_adp_sns)
  179. return -EOPNOTSUPP;
  180. if (!fsm->adp_sns) {
  181. fsm->adp_sns = 1;
  182. fsm->ops->start_adp_sns(fsm);
  183. }
  184. return 0;
  185. }
  186. static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
  187. {
  188. if (!fsm->ops->add_timer)
  189. return -EOPNOTSUPP;
  190. fsm->ops->add_timer(fsm, timer);
  191. return 0;
  192. }
  193. static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
  194. {
  195. if (!fsm->ops->del_timer)
  196. return -EOPNOTSUPP;
  197. fsm->ops->del_timer(fsm, timer);
  198. return 0;
  199. }
  200. static inline int otg_start_host(struct otg_fsm *fsm, int on)
  201. {
  202. if (!fsm->ops->start_host)
  203. return -EOPNOTSUPP;
  204. return fsm->ops->start_host(fsm, on);
  205. }
  206. static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
  207. {
  208. if (!fsm->ops->start_gadget)
  209. return -EOPNOTSUPP;
  210. return fsm->ops->start_gadget(fsm, on);
  211. }
  212. int otg_statemachine(struct otg_fsm *fsm);
  213. #endif /* __LINUX_USB_OTG_FSM_H */