usb-otg-fsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. /* 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. if (fsm->otg->state == new_state)
  194. return 0;
  195. VDBG("Set state: %s\n", usb_otg_state_string(new_state));
  196. otg_leave_state(fsm, fsm->otg->state);
  197. switch (new_state) {
  198. case OTG_STATE_B_IDLE:
  199. otg_drv_vbus(fsm, 0);
  200. otg_chrg_vbus(fsm, 0);
  201. otg_loc_conn(fsm, 0);
  202. otg_loc_sof(fsm, 0);
  203. /*
  204. * Driver is responsible for starting ADP probing
  205. * if ADP sensing times out.
  206. */
  207. otg_start_adp_sns(fsm);
  208. otg_set_protocol(fsm, PROTO_UNDEF);
  209. otg_add_timer(fsm, B_SE0_SRP);
  210. break;
  211. case OTG_STATE_B_SRP_INIT:
  212. otg_start_pulse(fsm);
  213. otg_loc_sof(fsm, 0);
  214. otg_set_protocol(fsm, PROTO_UNDEF);
  215. otg_add_timer(fsm, B_SRP_FAIL);
  216. break;
  217. case OTG_STATE_B_PERIPHERAL:
  218. otg_chrg_vbus(fsm, 0);
  219. otg_loc_sof(fsm, 0);
  220. otg_set_protocol(fsm, PROTO_GADGET);
  221. otg_loc_conn(fsm, 1);
  222. break;
  223. case OTG_STATE_B_WAIT_ACON:
  224. otg_chrg_vbus(fsm, 0);
  225. otg_loc_conn(fsm, 0);
  226. otg_loc_sof(fsm, 0);
  227. otg_set_protocol(fsm, PROTO_HOST);
  228. otg_add_timer(fsm, B_ASE0_BRST);
  229. fsm->a_bus_suspend = 0;
  230. break;
  231. case OTG_STATE_B_HOST:
  232. otg_chrg_vbus(fsm, 0);
  233. otg_loc_conn(fsm, 0);
  234. otg_loc_sof(fsm, 1);
  235. otg_set_protocol(fsm, PROTO_HOST);
  236. usb_bus_start_enum(fsm->otg->host,
  237. fsm->otg->host->otg_port);
  238. otg_start_hnp_polling(fsm);
  239. break;
  240. case OTG_STATE_A_IDLE:
  241. otg_drv_vbus(fsm, 0);
  242. otg_chrg_vbus(fsm, 0);
  243. otg_loc_conn(fsm, 0);
  244. otg_loc_sof(fsm, 0);
  245. otg_start_adp_prb(fsm);
  246. otg_set_protocol(fsm, PROTO_HOST);
  247. break;
  248. case OTG_STATE_A_WAIT_VRISE:
  249. otg_drv_vbus(fsm, 1);
  250. otg_loc_conn(fsm, 0);
  251. otg_loc_sof(fsm, 0);
  252. otg_set_protocol(fsm, PROTO_HOST);
  253. otg_add_timer(fsm, A_WAIT_VRISE);
  254. break;
  255. case OTG_STATE_A_WAIT_BCON:
  256. otg_drv_vbus(fsm, 1);
  257. otg_loc_conn(fsm, 0);
  258. otg_loc_sof(fsm, 0);
  259. otg_set_protocol(fsm, PROTO_HOST);
  260. otg_add_timer(fsm, A_WAIT_BCON);
  261. break;
  262. case OTG_STATE_A_HOST:
  263. otg_drv_vbus(fsm, 1);
  264. otg_loc_conn(fsm, 0);
  265. otg_loc_sof(fsm, 1);
  266. otg_set_protocol(fsm, PROTO_HOST);
  267. /*
  268. * When HNP is triggered while a_bus_req = 0, a_host will
  269. * suspend too fast to complete a_set_b_hnp_en
  270. */
  271. if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
  272. otg_add_timer(fsm, A_WAIT_ENUM);
  273. otg_start_hnp_polling(fsm);
  274. break;
  275. case OTG_STATE_A_SUSPEND:
  276. otg_drv_vbus(fsm, 1);
  277. otg_loc_conn(fsm, 0);
  278. otg_loc_sof(fsm, 0);
  279. otg_set_protocol(fsm, PROTO_HOST);
  280. otg_add_timer(fsm, A_AIDL_BDIS);
  281. break;
  282. case OTG_STATE_A_PERIPHERAL:
  283. otg_loc_sof(fsm, 0);
  284. otg_set_protocol(fsm, PROTO_GADGET);
  285. otg_drv_vbus(fsm, 1);
  286. otg_loc_conn(fsm, 1);
  287. otg_add_timer(fsm, A_BIDL_ADIS);
  288. break;
  289. case OTG_STATE_A_WAIT_VFALL:
  290. otg_drv_vbus(fsm, 0);
  291. otg_loc_conn(fsm, 0);
  292. otg_loc_sof(fsm, 0);
  293. otg_set_protocol(fsm, PROTO_HOST);
  294. otg_add_timer(fsm, A_WAIT_VFALL);
  295. break;
  296. case OTG_STATE_A_VBUS_ERR:
  297. otg_drv_vbus(fsm, 0);
  298. otg_loc_conn(fsm, 0);
  299. otg_loc_sof(fsm, 0);
  300. otg_set_protocol(fsm, PROTO_UNDEF);
  301. break;
  302. default:
  303. break;
  304. }
  305. fsm->otg->state = new_state;
  306. fsm->state_changed = 1;
  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. fsm->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", fsm->state_changed);
  427. return fsm->state_changed;
  428. }
  429. EXPORT_SYMBOL_GPL(otg_statemachine);
  430. MODULE_LICENSE("GPL");