otg_fsm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * OTG Finite State Machine from OTG spec
  3. *
  4. * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Li Yang <LeoLi@freescale.com>
  7. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/delay.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/gadget.h>
  29. #include <linux/usb/otg.h>
  30. #include <linux/types.h>
  31. #include "otg_fsm.h"
  32. /* Change USB protocol when there is a protocol change */
  33. static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
  34. {
  35. int ret = 0;
  36. if (fsm->protocol != protocol) {
  37. VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
  38. fsm->protocol, protocol);
  39. /* stop old protocol */
  40. if (fsm->protocol == PROTO_HOST)
  41. ret = fsm->ops->start_host(fsm, 0);
  42. else if (fsm->protocol == PROTO_GADGET)
  43. ret = fsm->ops->start_gadget(fsm, 0);
  44. if (ret)
  45. return ret;
  46. /* start new protocol */
  47. if (protocol == PROTO_HOST)
  48. ret = fsm->ops->start_host(fsm, 1);
  49. else if (protocol == PROTO_GADGET)
  50. ret = fsm->ops->start_gadget(fsm, 1);
  51. if (ret)
  52. return ret;
  53. fsm->protocol = protocol;
  54. return 0;
  55. }
  56. return 0;
  57. }
  58. static int state_changed;
  59. /* Called when leaving a state. Do state clean up jobs here */
  60. void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
  61. {
  62. switch (old_state) {
  63. case OTG_STATE_B_IDLE:
  64. otg_del_timer(fsm, b_se0_srp_tmr);
  65. fsm->b_se0_srp = 0;
  66. break;
  67. case OTG_STATE_B_SRP_INIT:
  68. fsm->b_srp_done = 0;
  69. break;
  70. case OTG_STATE_B_PERIPHERAL:
  71. break;
  72. case OTG_STATE_B_WAIT_ACON:
  73. otg_del_timer(fsm, b_ase0_brst_tmr);
  74. fsm->b_ase0_brst_tmout = 0;
  75. break;
  76. case OTG_STATE_B_HOST:
  77. break;
  78. case OTG_STATE_A_IDLE:
  79. break;
  80. case OTG_STATE_A_WAIT_VRISE:
  81. otg_del_timer(fsm, a_wait_vrise_tmr);
  82. fsm->a_wait_vrise_tmout = 0;
  83. break;
  84. case OTG_STATE_A_WAIT_BCON:
  85. otg_del_timer(fsm, a_wait_bcon_tmr);
  86. fsm->a_wait_bcon_tmout = 0;
  87. break;
  88. case OTG_STATE_A_HOST:
  89. otg_del_timer(fsm, a_wait_enum_tmr);
  90. break;
  91. case OTG_STATE_A_SUSPEND:
  92. otg_del_timer(fsm, a_aidl_bdis_tmr);
  93. fsm->a_aidl_bdis_tmout = 0;
  94. fsm->a_suspend_req = 0;
  95. break;
  96. case OTG_STATE_A_PERIPHERAL:
  97. break;
  98. case OTG_STATE_A_WAIT_VFALL:
  99. otg_del_timer(fsm, a_wait_vrise_tmr);
  100. break;
  101. case OTG_STATE_A_VBUS_ERR:
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. /* Called when entering a state */
  108. int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
  109. {
  110. state_changed = 1;
  111. if (fsm->transceiver->state == new_state)
  112. return 0;
  113. VDBG("Set state: %s\n", otg_state_string(new_state));
  114. otg_leave_state(fsm, fsm->transceiver->state);
  115. switch (new_state) {
  116. case OTG_STATE_B_IDLE:
  117. otg_drv_vbus(fsm, 0);
  118. otg_chrg_vbus(fsm, 0);
  119. otg_loc_conn(fsm, 0);
  120. otg_loc_sof(fsm, 0);
  121. otg_set_protocol(fsm, PROTO_UNDEF);
  122. otg_add_timer(fsm, b_se0_srp_tmr);
  123. break;
  124. case OTG_STATE_B_SRP_INIT:
  125. otg_start_pulse(fsm);
  126. otg_loc_sof(fsm, 0);
  127. otg_set_protocol(fsm, PROTO_UNDEF);
  128. otg_add_timer(fsm, b_srp_fail_tmr);
  129. break;
  130. case OTG_STATE_B_PERIPHERAL:
  131. otg_chrg_vbus(fsm, 0);
  132. otg_loc_conn(fsm, 1);
  133. otg_loc_sof(fsm, 0);
  134. otg_set_protocol(fsm, PROTO_GADGET);
  135. break;
  136. case OTG_STATE_B_WAIT_ACON:
  137. otg_chrg_vbus(fsm, 0);
  138. otg_loc_conn(fsm, 0);
  139. otg_loc_sof(fsm, 0);
  140. otg_set_protocol(fsm, PROTO_HOST);
  141. otg_add_timer(fsm, b_ase0_brst_tmr);
  142. fsm->a_bus_suspend = 0;
  143. break;
  144. case OTG_STATE_B_HOST:
  145. otg_chrg_vbus(fsm, 0);
  146. otg_loc_conn(fsm, 0);
  147. otg_loc_sof(fsm, 1);
  148. otg_set_protocol(fsm, PROTO_HOST);
  149. usb_bus_start_enum(fsm->transceiver->host,
  150. fsm->transceiver->host->otg_port);
  151. break;
  152. case OTG_STATE_A_IDLE:
  153. otg_drv_vbus(fsm, 0);
  154. otg_chrg_vbus(fsm, 0);
  155. otg_loc_conn(fsm, 0);
  156. otg_loc_sof(fsm, 0);
  157. otg_set_protocol(fsm, PROTO_HOST);
  158. break;
  159. case OTG_STATE_A_WAIT_VRISE:
  160. otg_drv_vbus(fsm, 1);
  161. otg_loc_conn(fsm, 0);
  162. otg_loc_sof(fsm, 0);
  163. otg_set_protocol(fsm, PROTO_HOST);
  164. otg_add_timer(fsm, a_wait_vrise_tmr);
  165. break;
  166. case OTG_STATE_A_WAIT_BCON:
  167. otg_drv_vbus(fsm, 1);
  168. otg_loc_conn(fsm, 0);
  169. otg_loc_sof(fsm, 0);
  170. otg_set_protocol(fsm, PROTO_HOST);
  171. otg_add_timer(fsm, a_wait_bcon_tmr);
  172. break;
  173. case OTG_STATE_A_HOST:
  174. otg_drv_vbus(fsm, 1);
  175. otg_loc_conn(fsm, 0);
  176. otg_loc_sof(fsm, 1);
  177. otg_set_protocol(fsm, PROTO_HOST);
  178. /*
  179. * When HNP is triggered while a_bus_req = 0, a_host will
  180. * suspend too fast to complete a_set_b_hnp_en
  181. */
  182. if (!fsm->a_bus_req || fsm->a_suspend_req)
  183. otg_add_timer(fsm, a_wait_enum_tmr);
  184. break;
  185. case OTG_STATE_A_SUSPEND:
  186. otg_drv_vbus(fsm, 1);
  187. otg_loc_conn(fsm, 0);
  188. otg_loc_sof(fsm, 0);
  189. otg_set_protocol(fsm, PROTO_HOST);
  190. otg_add_timer(fsm, a_aidl_bdis_tmr);
  191. break;
  192. case OTG_STATE_A_PERIPHERAL:
  193. otg_loc_conn(fsm, 1);
  194. otg_loc_sof(fsm, 0);
  195. otg_set_protocol(fsm, PROTO_GADGET);
  196. otg_drv_vbus(fsm, 1);
  197. break;
  198. case OTG_STATE_A_WAIT_VFALL:
  199. otg_drv_vbus(fsm, 0);
  200. otg_loc_conn(fsm, 0);
  201. otg_loc_sof(fsm, 0);
  202. otg_set_protocol(fsm, PROTO_HOST);
  203. break;
  204. case OTG_STATE_A_VBUS_ERR:
  205. otg_drv_vbus(fsm, 0);
  206. otg_loc_conn(fsm, 0);
  207. otg_loc_sof(fsm, 0);
  208. otg_set_protocol(fsm, PROTO_UNDEF);
  209. break;
  210. default:
  211. break;
  212. }
  213. fsm->transceiver->state = new_state;
  214. return 0;
  215. }
  216. /* State change judgement */
  217. int otg_statemachine(struct otg_fsm *fsm)
  218. {
  219. enum usb_otg_state state;
  220. unsigned long flags;
  221. spin_lock_irqsave(&fsm->lock, flags);
  222. state = fsm->transceiver->state;
  223. state_changed = 0;
  224. /* State machine state change judgement */
  225. switch (state) {
  226. case OTG_STATE_UNDEFINED:
  227. VDBG("fsm->id = %d\n", fsm->id);
  228. if (fsm->id)
  229. otg_set_state(fsm, OTG_STATE_B_IDLE);
  230. else
  231. otg_set_state(fsm, OTG_STATE_A_IDLE);
  232. break;
  233. case OTG_STATE_B_IDLE:
  234. if (!fsm->id)
  235. otg_set_state(fsm, OTG_STATE_A_IDLE);
  236. else if (fsm->b_sess_vld && fsm->transceiver->gadget)
  237. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  238. else if (fsm->b_bus_req && fsm->b_sess_end && fsm->b_se0_srp)
  239. otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
  240. break;
  241. case OTG_STATE_B_SRP_INIT:
  242. if (!fsm->id || fsm->b_srp_done)
  243. otg_set_state(fsm, OTG_STATE_B_IDLE);
  244. break;
  245. case OTG_STATE_B_PERIPHERAL:
  246. if (!fsm->id || !fsm->b_sess_vld)
  247. otg_set_state(fsm, OTG_STATE_B_IDLE);
  248. else if (fsm->b_bus_req && fsm->transceiver->
  249. gadget->b_hnp_enable && fsm->a_bus_suspend)
  250. otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
  251. break;
  252. case OTG_STATE_B_WAIT_ACON:
  253. if (fsm->a_conn)
  254. otg_set_state(fsm, OTG_STATE_B_HOST);
  255. else if (!fsm->id || !fsm->b_sess_vld)
  256. otg_set_state(fsm, OTG_STATE_B_IDLE);
  257. else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
  258. fsm->b_ase0_brst_tmout = 0;
  259. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  260. }
  261. break;
  262. case OTG_STATE_B_HOST:
  263. if (!fsm->id || !fsm->b_sess_vld)
  264. otg_set_state(fsm, OTG_STATE_B_IDLE);
  265. else if (!fsm->b_bus_req || !fsm->a_conn)
  266. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  267. break;
  268. case OTG_STATE_A_IDLE:
  269. if (fsm->id)
  270. otg_set_state(fsm, OTG_STATE_B_IDLE);
  271. else if (!fsm->a_bus_drop && (fsm->a_bus_req || fsm->a_srp_det))
  272. otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
  273. break;
  274. case OTG_STATE_A_WAIT_VRISE:
  275. if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
  276. fsm->a_wait_vrise_tmout) {
  277. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  278. }
  279. break;
  280. case OTG_STATE_A_WAIT_BCON:
  281. if (!fsm->a_vbus_vld)
  282. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  283. else if (fsm->b_conn)
  284. otg_set_state(fsm, OTG_STATE_A_HOST);
  285. else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
  286. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  287. break;
  288. case OTG_STATE_A_HOST:
  289. if ((!fsm->a_bus_req || fsm->a_suspend_req) &&
  290. fsm->transceiver->host->b_hnp_enable)
  291. otg_set_state(fsm, OTG_STATE_A_SUSPEND);
  292. else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
  293. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  294. else if (!fsm->a_vbus_vld)
  295. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  296. break;
  297. case OTG_STATE_A_SUSPEND:
  298. if (!fsm->b_conn && fsm->transceiver->host->b_hnp_enable)
  299. otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
  300. else if (!fsm->b_conn && !fsm->transceiver->host->b_hnp_enable)
  301. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  302. else if (fsm->a_bus_req || fsm->b_bus_resume)
  303. otg_set_state(fsm, OTG_STATE_A_HOST);
  304. else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
  305. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  306. else if (!fsm->a_vbus_vld)
  307. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  308. break;
  309. case OTG_STATE_A_PERIPHERAL:
  310. if (fsm->id || fsm->a_bus_drop)
  311. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  312. else if (fsm->b_bus_suspend)
  313. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  314. else if (!fsm->a_vbus_vld)
  315. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  316. break;
  317. case OTG_STATE_A_WAIT_VFALL:
  318. if (fsm->id || fsm->a_bus_req || (!fsm->a_sess_vld &&
  319. !fsm->b_conn))
  320. otg_set_state(fsm, OTG_STATE_A_IDLE);
  321. break;
  322. case OTG_STATE_A_VBUS_ERR:
  323. if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
  324. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  325. break;
  326. default:
  327. break;
  328. }
  329. spin_unlock_irqrestore(&fsm->lock, flags);
  330. VDBG("quit statemachine, changed = %d\n", state_changed);
  331. return state_changed;
  332. }