phy-fsm-usb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "phy-fsm-usb.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. 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. int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
  116. {
  117. state_changed = 1;
  118. if (fsm->otg->phy->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->phy->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_conn(fsm, 1);
  145. otg_loc_sof(fsm, 0);
  146. otg_set_protocol(fsm, PROTO_GADGET);
  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_conn(fsm, 1);
  207. otg_loc_sof(fsm, 0);
  208. otg_set_protocol(fsm, PROTO_GADGET);
  209. otg_drv_vbus(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->phy->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. unsigned long flags;
  236. spin_lock_irqsave(&fsm->lock, flags);
  237. state = fsm->otg->phy->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->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
  293. fsm->a_wait_vrise_tmout) {
  294. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  295. }
  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->a_bus_req || fsm->a_suspend_req_inf) &&
  307. fsm->otg->host->b_hnp_enable)
  308. otg_set_state(fsm, OTG_STATE_A_SUSPEND);
  309. else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
  310. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  311. else if (!fsm->a_vbus_vld)
  312. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  313. break;
  314. case OTG_STATE_A_SUSPEND:
  315. if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
  316. otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
  317. else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
  318. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  319. else if (fsm->a_bus_req || fsm->b_bus_resume)
  320. otg_set_state(fsm, OTG_STATE_A_HOST);
  321. else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
  322. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  323. else if (!fsm->a_vbus_vld)
  324. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  325. break;
  326. case OTG_STATE_A_PERIPHERAL:
  327. if (fsm->id || fsm->a_bus_drop)
  328. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  329. else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
  330. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  331. else if (!fsm->a_vbus_vld)
  332. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  333. break;
  334. case OTG_STATE_A_WAIT_VFALL:
  335. if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req ||
  336. (!fsm->a_sess_vld && !fsm->b_conn))
  337. otg_set_state(fsm, OTG_STATE_A_IDLE);
  338. break;
  339. case OTG_STATE_A_VBUS_ERR:
  340. if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
  341. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  342. break;
  343. default:
  344. break;
  345. }
  346. spin_unlock_irqrestore(&fsm->lock, flags);
  347. VDBG("quit statemachine, changed = %d\n", state_changed);
  348. return state_changed;
  349. }