usb-otg-fsm.c 12 KB

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