otg-fsm.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. B_DATA_PLS,
  49. B_SSEND_SRP,
  50. NUM_OTG_FSM_TIMERS,
  51. };
  52. /* OTG state machine according to the OTG spec */
  53. struct otg_fsm {
  54. /* Input */
  55. int id;
  56. int adp_change;
  57. int power_up;
  58. int test_device;
  59. int a_bus_drop;
  60. int a_bus_req;
  61. int a_srp_det;
  62. int a_vbus_vld;
  63. int b_conn;
  64. int a_bus_resume;
  65. int a_bus_suspend;
  66. int a_conn;
  67. int b_bus_req;
  68. int b_se0_srp;
  69. int b_ssend_srp;
  70. int b_sess_vld;
  71. /* Auxilary inputs */
  72. int a_sess_vld;
  73. int b_bus_resume;
  74. int b_bus_suspend;
  75. /* Output */
  76. int data_pulse;
  77. int drv_vbus;
  78. int loc_conn;
  79. int loc_sof;
  80. int adp_prb;
  81. int adp_sns;
  82. /* Internal variables */
  83. int a_set_b_hnp_en;
  84. int b_srp_done;
  85. int b_hnp_enable;
  86. int a_clr_err;
  87. /* Informative variables */
  88. int a_bus_drop_inf;
  89. int a_bus_req_inf;
  90. int a_clr_err_inf;
  91. int b_bus_req_inf;
  92. /* Auxilary informative variables */
  93. int a_suspend_req_inf;
  94. /* Timeout indicator for timers */
  95. int a_wait_vrise_tmout;
  96. int a_wait_vfall_tmout;
  97. int a_wait_bcon_tmout;
  98. int a_aidl_bdis_tmout;
  99. int b_ase0_brst_tmout;
  100. int a_bidl_adis_tmout;
  101. struct otg_fsm_ops *ops;
  102. struct usb_otg *otg;
  103. /* Current usb protocol used: 0:undefine; 1:host; 2:client */
  104. int protocol;
  105. struct mutex lock;
  106. };
  107. struct otg_fsm_ops {
  108. void (*chrg_vbus)(struct otg_fsm *fsm, int on);
  109. void (*drv_vbus)(struct otg_fsm *fsm, int on);
  110. void (*loc_conn)(struct otg_fsm *fsm, int on);
  111. void (*loc_sof)(struct otg_fsm *fsm, int on);
  112. void (*start_pulse)(struct otg_fsm *fsm);
  113. void (*start_adp_prb)(struct otg_fsm *fsm);
  114. void (*start_adp_sns)(struct otg_fsm *fsm);
  115. void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
  116. void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
  117. int (*start_host)(struct otg_fsm *fsm, int on);
  118. int (*start_gadget)(struct otg_fsm *fsm, int on);
  119. };
  120. static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
  121. {
  122. if (!fsm->ops->chrg_vbus)
  123. return -EOPNOTSUPP;
  124. fsm->ops->chrg_vbus(fsm, on);
  125. return 0;
  126. }
  127. static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
  128. {
  129. if (!fsm->ops->drv_vbus)
  130. return -EOPNOTSUPP;
  131. if (fsm->drv_vbus != on) {
  132. fsm->drv_vbus = on;
  133. fsm->ops->drv_vbus(fsm, on);
  134. }
  135. return 0;
  136. }
  137. static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
  138. {
  139. if (!fsm->ops->loc_conn)
  140. return -EOPNOTSUPP;
  141. if (fsm->loc_conn != on) {
  142. fsm->loc_conn = on;
  143. fsm->ops->loc_conn(fsm, on);
  144. }
  145. return 0;
  146. }
  147. static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
  148. {
  149. if (!fsm->ops->loc_sof)
  150. return -EOPNOTSUPP;
  151. if (fsm->loc_sof != on) {
  152. fsm->loc_sof = on;
  153. fsm->ops->loc_sof(fsm, on);
  154. }
  155. return 0;
  156. }
  157. static inline int otg_start_pulse(struct otg_fsm *fsm)
  158. {
  159. if (!fsm->ops->start_pulse)
  160. return -EOPNOTSUPP;
  161. if (!fsm->data_pulse) {
  162. fsm->data_pulse = 1;
  163. fsm->ops->start_pulse(fsm);
  164. }
  165. return 0;
  166. }
  167. static inline int otg_start_adp_prb(struct otg_fsm *fsm)
  168. {
  169. if (!fsm->ops->start_adp_prb)
  170. return -EOPNOTSUPP;
  171. if (!fsm->adp_prb) {
  172. fsm->adp_sns = 0;
  173. fsm->adp_prb = 1;
  174. fsm->ops->start_adp_prb(fsm);
  175. }
  176. return 0;
  177. }
  178. static inline int otg_start_adp_sns(struct otg_fsm *fsm)
  179. {
  180. if (!fsm->ops->start_adp_sns)
  181. return -EOPNOTSUPP;
  182. if (!fsm->adp_sns) {
  183. fsm->adp_sns = 1;
  184. fsm->ops->start_adp_sns(fsm);
  185. }
  186. return 0;
  187. }
  188. static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
  189. {
  190. if (!fsm->ops->add_timer)
  191. return -EOPNOTSUPP;
  192. fsm->ops->add_timer(fsm, timer);
  193. return 0;
  194. }
  195. static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
  196. {
  197. if (!fsm->ops->del_timer)
  198. return -EOPNOTSUPP;
  199. fsm->ops->del_timer(fsm, timer);
  200. return 0;
  201. }
  202. static inline int otg_start_host(struct otg_fsm *fsm, int on)
  203. {
  204. if (!fsm->ops->start_host)
  205. return -EOPNOTSUPP;
  206. return fsm->ops->start_host(fsm, on);
  207. }
  208. static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
  209. {
  210. if (!fsm->ops->start_gadget)
  211. return -EOPNOTSUPP;
  212. return fsm->ops->start_gadget(fsm, on);
  213. }
  214. int otg_statemachine(struct otg_fsm *fsm);
  215. #endif /* __LINUX_USB_OTG_FSM_H */