usb-otg-fsm.c 12 KB

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