usb-otg-fsm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/mutex.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/usb/otg-fsm.h>
  31. /* Change USB protocol when there is a protocol change */
  32. static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
  33. {
  34. int ret = 0;
  35. if (fsm->protocol != protocol) {
  36. VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
  37. fsm->protocol, protocol);
  38. /* stop old protocol */
  39. if (fsm->protocol == PROTO_HOST)
  40. ret = otg_start_host(fsm, 0);
  41. else if (fsm->protocol == PROTO_GADGET)
  42. ret = otg_start_gadget(fsm, 0);
  43. if (ret)
  44. return ret;
  45. /* start new protocol */
  46. if (protocol == PROTO_HOST)
  47. ret = otg_start_host(fsm, 1);
  48. else if (protocol == PROTO_GADGET)
  49. ret = otg_start_gadget(fsm, 1);
  50. if (ret)
  51. return ret;
  52. fsm->protocol = protocol;
  53. return 0;
  54. }
  55. return 0;
  56. }
  57. static int state_changed;
  58. /* Called when leaving a state. Do state clean up jobs here */
  59. static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
  60. {
  61. switch (old_state) {
  62. case OTG_STATE_B_IDLE:
  63. otg_del_timer(fsm, B_SE0_SRP);
  64. fsm->b_se0_srp = 0;
  65. fsm->adp_sns = 0;
  66. fsm->adp_prb = 0;
  67. break;
  68. case OTG_STATE_B_SRP_INIT:
  69. fsm->data_pulse = 0;
  70. fsm->b_srp_done = 0;
  71. break;
  72. case OTG_STATE_B_PERIPHERAL:
  73. break;
  74. case OTG_STATE_B_WAIT_ACON:
  75. otg_del_timer(fsm, B_ASE0_BRST);
  76. fsm->b_ase0_brst_tmout = 0;
  77. break;
  78. case OTG_STATE_B_HOST:
  79. break;
  80. case OTG_STATE_A_IDLE:
  81. fsm->adp_prb = 0;
  82. break;
  83. case OTG_STATE_A_WAIT_VRISE:
  84. otg_del_timer(fsm, A_WAIT_VRISE);
  85. fsm->a_wait_vrise_tmout = 0;
  86. break;
  87. case OTG_STATE_A_WAIT_BCON:
  88. otg_del_timer(fsm, A_WAIT_BCON);
  89. fsm->a_wait_bcon_tmout = 0;
  90. break;
  91. case OTG_STATE_A_HOST:
  92. otg_del_timer(fsm, A_WAIT_ENUM);
  93. break;
  94. case OTG_STATE_A_SUSPEND:
  95. otg_del_timer(fsm, A_AIDL_BDIS);
  96. fsm->a_aidl_bdis_tmout = 0;
  97. fsm->a_suspend_req_inf = 0;
  98. break;
  99. case OTG_STATE_A_PERIPHERAL:
  100. otg_del_timer(fsm, A_BIDL_ADIS);
  101. fsm->a_bidl_adis_tmout = 0;
  102. break;
  103. case OTG_STATE_A_WAIT_VFALL:
  104. otg_del_timer(fsm, A_WAIT_VFALL);
  105. fsm->a_wait_vfall_tmout = 0;
  106. otg_del_timer(fsm, A_WAIT_VRISE);
  107. break;
  108. case OTG_STATE_A_VBUS_ERR:
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. /* Called when entering a state */
  115. static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
  116. {
  117. state_changed = 1;
  118. if (fsm->otg->state == new_state)
  119. return 0;
  120. VDBG("Set state: %s\n", usb_otg_state_string(new_state));
  121. otg_leave_state(fsm, fsm->otg->state);
  122. switch (new_state) {
  123. case OTG_STATE_B_IDLE:
  124. otg_drv_vbus(fsm, 0);
  125. otg_chrg_vbus(fsm, 0);
  126. otg_loc_conn(fsm, 0);
  127. otg_loc_sof(fsm, 0);
  128. /*
  129. * Driver is responsible for starting ADP probing
  130. * if ADP sensing times out.
  131. */
  132. otg_start_adp_sns(fsm);
  133. otg_set_protocol(fsm, PROTO_UNDEF);
  134. otg_add_timer(fsm, B_SE0_SRP);
  135. break;
  136. case OTG_STATE_B_SRP_INIT:
  137. otg_start_pulse(fsm);
  138. otg_loc_sof(fsm, 0);
  139. otg_set_protocol(fsm, PROTO_UNDEF);
  140. otg_add_timer(fsm, B_SRP_FAIL);
  141. break;
  142. case OTG_STATE_B_PERIPHERAL:
  143. otg_chrg_vbus(fsm, 0);
  144. otg_loc_sof(fsm, 0);
  145. otg_set_protocol(fsm, PROTO_GADGET);
  146. otg_loc_conn(fsm, 1);
  147. break;
  148. case OTG_STATE_B_WAIT_ACON:
  149. otg_chrg_vbus(fsm, 0);
  150. otg_loc_conn(fsm, 0);
  151. otg_loc_sof(fsm, 0);
  152. otg_set_protocol(fsm, PROTO_HOST);
  153. otg_add_timer(fsm, B_ASE0_BRST);
  154. fsm->a_bus_suspend = 0;
  155. break;
  156. case OTG_STATE_B_HOST:
  157. otg_chrg_vbus(fsm, 0);
  158. otg_loc_conn(fsm, 0);
  159. otg_loc_sof(fsm, 1);
  160. otg_set_protocol(fsm, PROTO_HOST);
  161. usb_bus_start_enum(fsm->otg->host,
  162. fsm->otg->host->otg_port);
  163. break;
  164. case OTG_STATE_A_IDLE:
  165. otg_drv_vbus(fsm, 0);
  166. otg_chrg_vbus(fsm, 0);
  167. otg_loc_conn(fsm, 0);
  168. otg_loc_sof(fsm, 0);
  169. otg_start_adp_prb(fsm);
  170. otg_set_protocol(fsm, PROTO_HOST);
  171. break;
  172. case OTG_STATE_A_WAIT_VRISE:
  173. otg_drv_vbus(fsm, 1);
  174. otg_loc_conn(fsm, 0);
  175. otg_loc_sof(fsm, 0);
  176. otg_set_protocol(fsm, PROTO_HOST);
  177. otg_add_timer(fsm, A_WAIT_VRISE);
  178. break;
  179. case OTG_STATE_A_WAIT_BCON:
  180. otg_drv_vbus(fsm, 1);
  181. otg_loc_conn(fsm, 0);
  182. otg_loc_sof(fsm, 0);
  183. otg_set_protocol(fsm, PROTO_HOST);
  184. otg_add_timer(fsm, A_WAIT_BCON);
  185. break;
  186. case OTG_STATE_A_HOST:
  187. otg_drv_vbus(fsm, 1);
  188. otg_loc_conn(fsm, 0);
  189. otg_loc_sof(fsm, 1);
  190. otg_set_protocol(fsm, PROTO_HOST);
  191. /*
  192. * When HNP is triggered while a_bus_req = 0, a_host will
  193. * suspend too fast to complete a_set_b_hnp_en
  194. */
  195. if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
  196. otg_add_timer(fsm, A_WAIT_ENUM);
  197. break;
  198. case OTG_STATE_A_SUSPEND:
  199. otg_drv_vbus(fsm, 1);
  200. otg_loc_conn(fsm, 0);
  201. otg_loc_sof(fsm, 0);
  202. otg_set_protocol(fsm, PROTO_HOST);
  203. otg_add_timer(fsm, A_AIDL_BDIS);
  204. break;
  205. case OTG_STATE_A_PERIPHERAL:
  206. otg_loc_sof(fsm, 0);
  207. otg_set_protocol(fsm, PROTO_GADGET);
  208. otg_drv_vbus(fsm, 1);
  209. otg_loc_conn(fsm, 1);
  210. otg_add_timer(fsm, A_BIDL_ADIS);
  211. break;
  212. case OTG_STATE_A_WAIT_VFALL:
  213. otg_drv_vbus(fsm, 0);
  214. otg_loc_conn(fsm, 0);
  215. otg_loc_sof(fsm, 0);
  216. otg_set_protocol(fsm, PROTO_HOST);
  217. otg_add_timer(fsm, A_WAIT_VFALL);
  218. break;
  219. case OTG_STATE_A_VBUS_ERR:
  220. otg_drv_vbus(fsm, 0);
  221. otg_loc_conn(fsm, 0);
  222. otg_loc_sof(fsm, 0);
  223. otg_set_protocol(fsm, PROTO_UNDEF);
  224. break;
  225. default:
  226. break;
  227. }
  228. fsm->otg->state = new_state;
  229. return 0;
  230. }
  231. /* State change judgement */
  232. int otg_statemachine(struct otg_fsm *fsm)
  233. {
  234. enum usb_otg_state state;
  235. mutex_lock(&fsm->lock);
  236. state = fsm->otg->state;
  237. state_changed = 0;
  238. /* State machine state change judgement */
  239. switch (state) {
  240. case OTG_STATE_UNDEFINED:
  241. VDBG("fsm->id = %d\n", fsm->id);
  242. if (fsm->id)
  243. otg_set_state(fsm, OTG_STATE_B_IDLE);
  244. else
  245. otg_set_state(fsm, OTG_STATE_A_IDLE);
  246. break;
  247. case OTG_STATE_B_IDLE:
  248. if (!fsm->id)
  249. otg_set_state(fsm, OTG_STATE_A_IDLE);
  250. else if (fsm->b_sess_vld && fsm->otg->gadget)
  251. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  252. else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
  253. fsm->b_ssend_srp && fsm->b_se0_srp)
  254. otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
  255. break;
  256. case OTG_STATE_B_SRP_INIT:
  257. if (!fsm->id || fsm->b_srp_done)
  258. otg_set_state(fsm, OTG_STATE_B_IDLE);
  259. break;
  260. case OTG_STATE_B_PERIPHERAL:
  261. if (!fsm->id || !fsm->b_sess_vld)
  262. otg_set_state(fsm, OTG_STATE_B_IDLE);
  263. else if (fsm->b_bus_req && fsm->otg->
  264. gadget->b_hnp_enable && fsm->a_bus_suspend)
  265. otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
  266. break;
  267. case OTG_STATE_B_WAIT_ACON:
  268. if (fsm->a_conn)
  269. otg_set_state(fsm, OTG_STATE_B_HOST);
  270. else if (!fsm->id || !fsm->b_sess_vld)
  271. otg_set_state(fsm, OTG_STATE_B_IDLE);
  272. else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
  273. fsm->b_ase0_brst_tmout = 0;
  274. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  275. }
  276. break;
  277. case OTG_STATE_B_HOST:
  278. if (!fsm->id || !fsm->b_sess_vld)
  279. otg_set_state(fsm, OTG_STATE_B_IDLE);
  280. else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
  281. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  282. break;
  283. case OTG_STATE_A_IDLE:
  284. if (fsm->id)
  285. otg_set_state(fsm, OTG_STATE_B_IDLE);
  286. else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
  287. fsm->a_srp_det || fsm->adp_change || fsm->power_up))
  288. otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
  289. break;
  290. case OTG_STATE_A_WAIT_VRISE:
  291. if (fsm->a_vbus_vld)
  292. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  293. else if (fsm->id || fsm->a_bus_drop ||
  294. fsm->a_wait_vrise_tmout)
  295. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  296. break;
  297. case OTG_STATE_A_WAIT_BCON:
  298. if (!fsm->a_vbus_vld)
  299. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  300. else if (fsm->b_conn)
  301. otg_set_state(fsm, OTG_STATE_A_HOST);
  302. else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
  303. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  304. break;
  305. case OTG_STATE_A_HOST:
  306. if (fsm->id || fsm->a_bus_drop)
  307. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  308. else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
  309. fsm->otg->host->b_hnp_enable)
  310. otg_set_state(fsm, OTG_STATE_A_SUSPEND);
  311. else if (!fsm->b_conn)
  312. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  313. else if (!fsm->a_vbus_vld)
  314. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  315. break;
  316. case OTG_STATE_A_SUSPEND:
  317. if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
  318. otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
  319. else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
  320. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  321. else if (fsm->a_bus_req || fsm->b_bus_resume)
  322. otg_set_state(fsm, OTG_STATE_A_HOST);
  323. else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
  324. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  325. else if (!fsm->a_vbus_vld)
  326. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  327. break;
  328. case OTG_STATE_A_PERIPHERAL:
  329. if (fsm->id || fsm->a_bus_drop)
  330. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  331. else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
  332. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  333. else if (!fsm->a_vbus_vld)
  334. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  335. break;
  336. case OTG_STATE_A_WAIT_VFALL:
  337. if (fsm->a_wait_vfall_tmout)
  338. otg_set_state(fsm, OTG_STATE_A_IDLE);
  339. break;
  340. case OTG_STATE_A_VBUS_ERR:
  341. if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
  342. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  343. break;
  344. default:
  345. break;
  346. }
  347. mutex_unlock(&fsm->lock);
  348. VDBG("quit statemachine, changed = %d\n", state_changed);
  349. return state_changed;
  350. }
  351. EXPORT_SYMBOL_GPL(otg_statemachine);