usb-otg-fsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. if (fsm->otg->gadget)
  74. fsm->otg->gadget->host_request_flag = 0;
  75. break;
  76. case OTG_STATE_B_WAIT_ACON:
  77. otg_del_timer(fsm, B_ASE0_BRST);
  78. fsm->b_ase0_brst_tmout = 0;
  79. break;
  80. case OTG_STATE_B_HOST:
  81. break;
  82. case OTG_STATE_A_IDLE:
  83. fsm->adp_prb = 0;
  84. break;
  85. case OTG_STATE_A_WAIT_VRISE:
  86. otg_del_timer(fsm, A_WAIT_VRISE);
  87. fsm->a_wait_vrise_tmout = 0;
  88. break;
  89. case OTG_STATE_A_WAIT_BCON:
  90. otg_del_timer(fsm, A_WAIT_BCON);
  91. fsm->a_wait_bcon_tmout = 0;
  92. break;
  93. case OTG_STATE_A_HOST:
  94. otg_del_timer(fsm, A_WAIT_ENUM);
  95. break;
  96. case OTG_STATE_A_SUSPEND:
  97. otg_del_timer(fsm, A_AIDL_BDIS);
  98. fsm->a_aidl_bdis_tmout = 0;
  99. fsm->a_suspend_req_inf = 0;
  100. break;
  101. case OTG_STATE_A_PERIPHERAL:
  102. otg_del_timer(fsm, A_BIDL_ADIS);
  103. fsm->a_bidl_adis_tmout = 0;
  104. if (fsm->otg->gadget)
  105. fsm->otg->gadget->host_request_flag = 0;
  106. break;
  107. case OTG_STATE_A_WAIT_VFALL:
  108. otg_del_timer(fsm, A_WAIT_VFALL);
  109. fsm->a_wait_vfall_tmout = 0;
  110. otg_del_timer(fsm, A_WAIT_VRISE);
  111. break;
  112. case OTG_STATE_A_VBUS_ERR:
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. static void otg_hnp_polling_work(struct work_struct *work)
  119. {
  120. struct otg_fsm *fsm = container_of(to_delayed_work(work),
  121. struct otg_fsm, hnp_polling_work);
  122. struct usb_device *udev;
  123. enum usb_otg_state state = fsm->otg->state;
  124. u8 flag;
  125. int retval;
  126. if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
  127. return;
  128. udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
  129. if (!udev) {
  130. dev_err(fsm->otg->host->controller,
  131. "no usb dev connected, can't start HNP polling\n");
  132. return;
  133. }
  134. *fsm->host_req_flag = 0;
  135. /* Get host request flag from connected USB device */
  136. retval = usb_control_msg(udev,
  137. usb_rcvctrlpipe(udev, 0),
  138. USB_REQ_GET_STATUS,
  139. USB_DIR_IN | USB_RECIP_DEVICE,
  140. 0,
  141. OTG_STS_SELECTOR,
  142. fsm->host_req_flag,
  143. 1,
  144. USB_CTRL_GET_TIMEOUT);
  145. if (retval != 1) {
  146. dev_err(&udev->dev, "Get one byte OTG status failed\n");
  147. return;
  148. }
  149. flag = *fsm->host_req_flag;
  150. if (flag == 0) {
  151. /* Continue HNP polling */
  152. schedule_delayed_work(&fsm->hnp_polling_work,
  153. msecs_to_jiffies(T_HOST_REQ_POLL));
  154. return;
  155. } else if (flag != HOST_REQUEST_FLAG) {
  156. dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
  157. return;
  158. }
  159. /* Host request flag is set */
  160. if (state == OTG_STATE_A_HOST) {
  161. /* Set b_hnp_enable */
  162. if (!fsm->otg->host->b_hnp_enable) {
  163. retval = usb_control_msg(udev,
  164. usb_sndctrlpipe(udev, 0),
  165. USB_REQ_SET_FEATURE, 0,
  166. USB_DEVICE_B_HNP_ENABLE,
  167. 0, NULL, 0,
  168. USB_CTRL_SET_TIMEOUT);
  169. if (retval >= 0)
  170. fsm->otg->host->b_hnp_enable = 1;
  171. }
  172. fsm->a_bus_req = 0;
  173. } else if (state == OTG_STATE_B_HOST) {
  174. fsm->b_bus_req = 0;
  175. }
  176. otg_statemachine(fsm);
  177. }
  178. static void otg_start_hnp_polling(struct otg_fsm *fsm)
  179. {
  180. /*
  181. * The memory of host_req_flag should be allocated by
  182. * controller driver, otherwise, hnp polling is not started.
  183. */
  184. if (!fsm->host_req_flag)
  185. return;
  186. INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
  187. schedule_delayed_work(&fsm->hnp_polling_work,
  188. msecs_to_jiffies(T_HOST_REQ_POLL));
  189. }
  190. /* Called when entering a state */
  191. static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
  192. {
  193. state_changed = 1;
  194. if (fsm->otg->state == new_state)
  195. return 0;
  196. VDBG("Set state: %s\n", usb_otg_state_string(new_state));
  197. otg_leave_state(fsm, fsm->otg->state);
  198. switch (new_state) {
  199. case OTG_STATE_B_IDLE:
  200. otg_drv_vbus(fsm, 0);
  201. otg_chrg_vbus(fsm, 0);
  202. otg_loc_conn(fsm, 0);
  203. otg_loc_sof(fsm, 0);
  204. /*
  205. * Driver is responsible for starting ADP probing
  206. * if ADP sensing times out.
  207. */
  208. otg_start_adp_sns(fsm);
  209. otg_set_protocol(fsm, PROTO_UNDEF);
  210. otg_add_timer(fsm, B_SE0_SRP);
  211. break;
  212. case OTG_STATE_B_SRP_INIT:
  213. otg_start_pulse(fsm);
  214. otg_loc_sof(fsm, 0);
  215. otg_set_protocol(fsm, PROTO_UNDEF);
  216. otg_add_timer(fsm, B_SRP_FAIL);
  217. break;
  218. case OTG_STATE_B_PERIPHERAL:
  219. otg_chrg_vbus(fsm, 0);
  220. otg_loc_sof(fsm, 0);
  221. otg_set_protocol(fsm, PROTO_GADGET);
  222. otg_loc_conn(fsm, 1);
  223. break;
  224. case OTG_STATE_B_WAIT_ACON:
  225. otg_chrg_vbus(fsm, 0);
  226. otg_loc_conn(fsm, 0);
  227. otg_loc_sof(fsm, 0);
  228. otg_set_protocol(fsm, PROTO_HOST);
  229. otg_add_timer(fsm, B_ASE0_BRST);
  230. fsm->a_bus_suspend = 0;
  231. break;
  232. case OTG_STATE_B_HOST:
  233. otg_chrg_vbus(fsm, 0);
  234. otg_loc_conn(fsm, 0);
  235. otg_loc_sof(fsm, 1);
  236. otg_set_protocol(fsm, PROTO_HOST);
  237. usb_bus_start_enum(fsm->otg->host,
  238. fsm->otg->host->otg_port);
  239. otg_start_hnp_polling(fsm);
  240. break;
  241. case OTG_STATE_A_IDLE:
  242. otg_drv_vbus(fsm, 0);
  243. otg_chrg_vbus(fsm, 0);
  244. otg_loc_conn(fsm, 0);
  245. otg_loc_sof(fsm, 0);
  246. otg_start_adp_prb(fsm);
  247. otg_set_protocol(fsm, PROTO_HOST);
  248. break;
  249. case OTG_STATE_A_WAIT_VRISE:
  250. otg_drv_vbus(fsm, 1);
  251. otg_loc_conn(fsm, 0);
  252. otg_loc_sof(fsm, 0);
  253. otg_set_protocol(fsm, PROTO_HOST);
  254. otg_add_timer(fsm, A_WAIT_VRISE);
  255. break;
  256. case OTG_STATE_A_WAIT_BCON:
  257. otg_drv_vbus(fsm, 1);
  258. otg_loc_conn(fsm, 0);
  259. otg_loc_sof(fsm, 0);
  260. otg_set_protocol(fsm, PROTO_HOST);
  261. otg_add_timer(fsm, A_WAIT_BCON);
  262. break;
  263. case OTG_STATE_A_HOST:
  264. otg_drv_vbus(fsm, 1);
  265. otg_loc_conn(fsm, 0);
  266. otg_loc_sof(fsm, 1);
  267. otg_set_protocol(fsm, PROTO_HOST);
  268. /*
  269. * When HNP is triggered while a_bus_req = 0, a_host will
  270. * suspend too fast to complete a_set_b_hnp_en
  271. */
  272. if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
  273. otg_add_timer(fsm, A_WAIT_ENUM);
  274. otg_start_hnp_polling(fsm);
  275. break;
  276. case OTG_STATE_A_SUSPEND:
  277. otg_drv_vbus(fsm, 1);
  278. otg_loc_conn(fsm, 0);
  279. otg_loc_sof(fsm, 0);
  280. otg_set_protocol(fsm, PROTO_HOST);
  281. otg_add_timer(fsm, A_AIDL_BDIS);
  282. break;
  283. case OTG_STATE_A_PERIPHERAL:
  284. otg_loc_sof(fsm, 0);
  285. otg_set_protocol(fsm, PROTO_GADGET);
  286. otg_drv_vbus(fsm, 1);
  287. otg_loc_conn(fsm, 1);
  288. otg_add_timer(fsm, A_BIDL_ADIS);
  289. break;
  290. case OTG_STATE_A_WAIT_VFALL:
  291. otg_drv_vbus(fsm, 0);
  292. otg_loc_conn(fsm, 0);
  293. otg_loc_sof(fsm, 0);
  294. otg_set_protocol(fsm, PROTO_HOST);
  295. otg_add_timer(fsm, A_WAIT_VFALL);
  296. break;
  297. case OTG_STATE_A_VBUS_ERR:
  298. otg_drv_vbus(fsm, 0);
  299. otg_loc_conn(fsm, 0);
  300. otg_loc_sof(fsm, 0);
  301. otg_set_protocol(fsm, PROTO_UNDEF);
  302. break;
  303. default:
  304. break;
  305. }
  306. fsm->otg->state = new_state;
  307. return 0;
  308. }
  309. /* State change judgement */
  310. int otg_statemachine(struct otg_fsm *fsm)
  311. {
  312. enum usb_otg_state state;
  313. mutex_lock(&fsm->lock);
  314. state = fsm->otg->state;
  315. state_changed = 0;
  316. /* State machine state change judgement */
  317. switch (state) {
  318. case OTG_STATE_UNDEFINED:
  319. VDBG("fsm->id = %d\n", fsm->id);
  320. if (fsm->id)
  321. otg_set_state(fsm, OTG_STATE_B_IDLE);
  322. else
  323. otg_set_state(fsm, OTG_STATE_A_IDLE);
  324. break;
  325. case OTG_STATE_B_IDLE:
  326. if (!fsm->id)
  327. otg_set_state(fsm, OTG_STATE_A_IDLE);
  328. else if (fsm->b_sess_vld && fsm->otg->gadget)
  329. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  330. else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
  331. fsm->b_ssend_srp && fsm->b_se0_srp)
  332. otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
  333. break;
  334. case OTG_STATE_B_SRP_INIT:
  335. if (!fsm->id || fsm->b_srp_done)
  336. otg_set_state(fsm, OTG_STATE_B_IDLE);
  337. break;
  338. case OTG_STATE_B_PERIPHERAL:
  339. if (!fsm->id || !fsm->b_sess_vld)
  340. otg_set_state(fsm, OTG_STATE_B_IDLE);
  341. else if (fsm->b_bus_req && fsm->otg->
  342. gadget->b_hnp_enable && fsm->a_bus_suspend)
  343. otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
  344. break;
  345. case OTG_STATE_B_WAIT_ACON:
  346. if (fsm->a_conn)
  347. otg_set_state(fsm, OTG_STATE_B_HOST);
  348. else if (!fsm->id || !fsm->b_sess_vld)
  349. otg_set_state(fsm, OTG_STATE_B_IDLE);
  350. else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
  351. fsm->b_ase0_brst_tmout = 0;
  352. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  353. }
  354. break;
  355. case OTG_STATE_B_HOST:
  356. if (!fsm->id || !fsm->b_sess_vld)
  357. otg_set_state(fsm, OTG_STATE_B_IDLE);
  358. else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
  359. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  360. break;
  361. case OTG_STATE_A_IDLE:
  362. if (fsm->id)
  363. otg_set_state(fsm, OTG_STATE_B_IDLE);
  364. else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
  365. fsm->a_srp_det || fsm->adp_change || fsm->power_up))
  366. otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
  367. break;
  368. case OTG_STATE_A_WAIT_VRISE:
  369. if (fsm->a_vbus_vld)
  370. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  371. else if (fsm->id || fsm->a_bus_drop ||
  372. fsm->a_wait_vrise_tmout)
  373. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  374. break;
  375. case OTG_STATE_A_WAIT_BCON:
  376. if (!fsm->a_vbus_vld)
  377. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  378. else if (fsm->b_conn)
  379. otg_set_state(fsm, OTG_STATE_A_HOST);
  380. else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
  381. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  382. break;
  383. case OTG_STATE_A_HOST:
  384. if (fsm->id || fsm->a_bus_drop)
  385. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  386. else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
  387. fsm->otg->host->b_hnp_enable)
  388. otg_set_state(fsm, OTG_STATE_A_SUSPEND);
  389. else if (!fsm->b_conn)
  390. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  391. else if (!fsm->a_vbus_vld)
  392. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  393. break;
  394. case OTG_STATE_A_SUSPEND:
  395. if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
  396. otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
  397. else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
  398. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  399. else if (fsm->a_bus_req || fsm->b_bus_resume)
  400. otg_set_state(fsm, OTG_STATE_A_HOST);
  401. else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
  402. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  403. else if (!fsm->a_vbus_vld)
  404. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  405. break;
  406. case OTG_STATE_A_PERIPHERAL:
  407. if (fsm->id || fsm->a_bus_drop)
  408. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  409. else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
  410. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  411. else if (!fsm->a_vbus_vld)
  412. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  413. break;
  414. case OTG_STATE_A_WAIT_VFALL:
  415. if (fsm->a_wait_vfall_tmout)
  416. otg_set_state(fsm, OTG_STATE_A_IDLE);
  417. break;
  418. case OTG_STATE_A_VBUS_ERR:
  419. if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
  420. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  421. break;
  422. default:
  423. break;
  424. }
  425. mutex_unlock(&fsm->lock);
  426. VDBG("quit statemachine, changed = %d\n", state_changed);
  427. return state_changed;
  428. }
  429. EXPORT_SYMBOL_GPL(otg_statemachine);