usb-otg-fsm.c 10 KB

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