usb-otg-fsm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. if (!fsm->hnp_work_inited) {
  180. INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
  181. fsm->hnp_work_inited = true;
  182. }
  183. schedule_delayed_work(&fsm->hnp_polling_work,
  184. msecs_to_jiffies(T_HOST_REQ_POLL));
  185. }
  186. /* Called when entering a state */
  187. static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
  188. {
  189. if (fsm->otg->state == new_state)
  190. return 0;
  191. VDBG("Set state: %s\n", usb_otg_state_string(new_state));
  192. otg_leave_state(fsm, fsm->otg->state);
  193. switch (new_state) {
  194. case OTG_STATE_B_IDLE:
  195. otg_drv_vbus(fsm, 0);
  196. otg_chrg_vbus(fsm, 0);
  197. otg_loc_conn(fsm, 0);
  198. otg_loc_sof(fsm, 0);
  199. /*
  200. * Driver is responsible for starting ADP probing
  201. * if ADP sensing times out.
  202. */
  203. otg_start_adp_sns(fsm);
  204. otg_set_protocol(fsm, PROTO_UNDEF);
  205. otg_add_timer(fsm, B_SE0_SRP);
  206. break;
  207. case OTG_STATE_B_SRP_INIT:
  208. otg_start_pulse(fsm);
  209. otg_loc_sof(fsm, 0);
  210. otg_set_protocol(fsm, PROTO_UNDEF);
  211. otg_add_timer(fsm, B_SRP_FAIL);
  212. break;
  213. case OTG_STATE_B_PERIPHERAL:
  214. otg_chrg_vbus(fsm, 0);
  215. otg_loc_sof(fsm, 0);
  216. otg_set_protocol(fsm, PROTO_GADGET);
  217. otg_loc_conn(fsm, 1);
  218. break;
  219. case OTG_STATE_B_WAIT_ACON:
  220. otg_chrg_vbus(fsm, 0);
  221. otg_loc_conn(fsm, 0);
  222. otg_loc_sof(fsm, 0);
  223. otg_set_protocol(fsm, PROTO_HOST);
  224. otg_add_timer(fsm, B_ASE0_BRST);
  225. fsm->a_bus_suspend = 0;
  226. break;
  227. case OTG_STATE_B_HOST:
  228. otg_chrg_vbus(fsm, 0);
  229. otg_loc_conn(fsm, 0);
  230. otg_loc_sof(fsm, 1);
  231. otg_set_protocol(fsm, PROTO_HOST);
  232. usb_bus_start_enum(fsm->otg->host,
  233. fsm->otg->host->otg_port);
  234. otg_start_hnp_polling(fsm);
  235. break;
  236. case OTG_STATE_A_IDLE:
  237. otg_drv_vbus(fsm, 0);
  238. otg_chrg_vbus(fsm, 0);
  239. otg_loc_conn(fsm, 0);
  240. otg_loc_sof(fsm, 0);
  241. otg_start_adp_prb(fsm);
  242. otg_set_protocol(fsm, PROTO_HOST);
  243. break;
  244. case OTG_STATE_A_WAIT_VRISE:
  245. otg_drv_vbus(fsm, 1);
  246. otg_loc_conn(fsm, 0);
  247. otg_loc_sof(fsm, 0);
  248. otg_set_protocol(fsm, PROTO_HOST);
  249. otg_add_timer(fsm, A_WAIT_VRISE);
  250. break;
  251. case OTG_STATE_A_WAIT_BCON:
  252. otg_drv_vbus(fsm, 1);
  253. otg_loc_conn(fsm, 0);
  254. otg_loc_sof(fsm, 0);
  255. otg_set_protocol(fsm, PROTO_HOST);
  256. otg_add_timer(fsm, A_WAIT_BCON);
  257. break;
  258. case OTG_STATE_A_HOST:
  259. otg_drv_vbus(fsm, 1);
  260. otg_loc_conn(fsm, 0);
  261. otg_loc_sof(fsm, 1);
  262. otg_set_protocol(fsm, PROTO_HOST);
  263. /*
  264. * When HNP is triggered while a_bus_req = 0, a_host will
  265. * suspend too fast to complete a_set_b_hnp_en
  266. */
  267. if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
  268. otg_add_timer(fsm, A_WAIT_ENUM);
  269. otg_start_hnp_polling(fsm);
  270. break;
  271. case OTG_STATE_A_SUSPEND:
  272. otg_drv_vbus(fsm, 1);
  273. otg_loc_conn(fsm, 0);
  274. otg_loc_sof(fsm, 0);
  275. otg_set_protocol(fsm, PROTO_HOST);
  276. otg_add_timer(fsm, A_AIDL_BDIS);
  277. break;
  278. case OTG_STATE_A_PERIPHERAL:
  279. otg_loc_sof(fsm, 0);
  280. otg_set_protocol(fsm, PROTO_GADGET);
  281. otg_drv_vbus(fsm, 1);
  282. otg_loc_conn(fsm, 1);
  283. otg_add_timer(fsm, A_BIDL_ADIS);
  284. break;
  285. case OTG_STATE_A_WAIT_VFALL:
  286. otg_drv_vbus(fsm, 0);
  287. otg_loc_conn(fsm, 0);
  288. otg_loc_sof(fsm, 0);
  289. otg_set_protocol(fsm, PROTO_HOST);
  290. otg_add_timer(fsm, A_WAIT_VFALL);
  291. break;
  292. case OTG_STATE_A_VBUS_ERR:
  293. otg_drv_vbus(fsm, 0);
  294. otg_loc_conn(fsm, 0);
  295. otg_loc_sof(fsm, 0);
  296. otg_set_protocol(fsm, PROTO_UNDEF);
  297. break;
  298. default:
  299. break;
  300. }
  301. fsm->otg->state = new_state;
  302. fsm->state_changed = 1;
  303. return 0;
  304. }
  305. /* State change judgement */
  306. int otg_statemachine(struct otg_fsm *fsm)
  307. {
  308. enum usb_otg_state state;
  309. mutex_lock(&fsm->lock);
  310. state = fsm->otg->state;
  311. fsm->state_changed = 0;
  312. /* State machine state change judgement */
  313. switch (state) {
  314. case OTG_STATE_UNDEFINED:
  315. VDBG("fsm->id = %d\n", fsm->id);
  316. if (fsm->id)
  317. otg_set_state(fsm, OTG_STATE_B_IDLE);
  318. else
  319. otg_set_state(fsm, OTG_STATE_A_IDLE);
  320. break;
  321. case OTG_STATE_B_IDLE:
  322. if (!fsm->id)
  323. otg_set_state(fsm, OTG_STATE_A_IDLE);
  324. else if (fsm->b_sess_vld && fsm->otg->gadget)
  325. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  326. else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
  327. fsm->b_ssend_srp && fsm->b_se0_srp)
  328. otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
  329. break;
  330. case OTG_STATE_B_SRP_INIT:
  331. if (!fsm->id || fsm->b_srp_done)
  332. otg_set_state(fsm, OTG_STATE_B_IDLE);
  333. break;
  334. case OTG_STATE_B_PERIPHERAL:
  335. if (!fsm->id || !fsm->b_sess_vld)
  336. otg_set_state(fsm, OTG_STATE_B_IDLE);
  337. else if (fsm->b_bus_req && fsm->otg->
  338. gadget->b_hnp_enable && fsm->a_bus_suspend)
  339. otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
  340. break;
  341. case OTG_STATE_B_WAIT_ACON:
  342. if (fsm->a_conn)
  343. otg_set_state(fsm, OTG_STATE_B_HOST);
  344. else if (!fsm->id || !fsm->b_sess_vld)
  345. otg_set_state(fsm, OTG_STATE_B_IDLE);
  346. else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
  347. fsm->b_ase0_brst_tmout = 0;
  348. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  349. }
  350. break;
  351. case OTG_STATE_B_HOST:
  352. if (!fsm->id || !fsm->b_sess_vld)
  353. otg_set_state(fsm, OTG_STATE_B_IDLE);
  354. else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
  355. otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
  356. break;
  357. case OTG_STATE_A_IDLE:
  358. if (fsm->id)
  359. otg_set_state(fsm, OTG_STATE_B_IDLE);
  360. else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
  361. fsm->a_srp_det || fsm->adp_change || fsm->power_up))
  362. otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
  363. break;
  364. case OTG_STATE_A_WAIT_VRISE:
  365. if (fsm->a_vbus_vld)
  366. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  367. else if (fsm->id || fsm->a_bus_drop ||
  368. fsm->a_wait_vrise_tmout)
  369. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  370. break;
  371. case OTG_STATE_A_WAIT_BCON:
  372. if (!fsm->a_vbus_vld)
  373. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  374. else if (fsm->b_conn)
  375. otg_set_state(fsm, OTG_STATE_A_HOST);
  376. else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
  377. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  378. break;
  379. case OTG_STATE_A_HOST:
  380. if (fsm->id || fsm->a_bus_drop)
  381. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  382. else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
  383. fsm->otg->host->b_hnp_enable)
  384. otg_set_state(fsm, OTG_STATE_A_SUSPEND);
  385. else if (!fsm->b_conn)
  386. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  387. else if (!fsm->a_vbus_vld)
  388. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  389. break;
  390. case OTG_STATE_A_SUSPEND:
  391. if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
  392. otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
  393. else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
  394. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  395. else if (fsm->a_bus_req || fsm->b_bus_resume)
  396. otg_set_state(fsm, OTG_STATE_A_HOST);
  397. else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
  398. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  399. else if (!fsm->a_vbus_vld)
  400. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  401. break;
  402. case OTG_STATE_A_PERIPHERAL:
  403. if (fsm->id || fsm->a_bus_drop)
  404. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  405. else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
  406. otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
  407. else if (!fsm->a_vbus_vld)
  408. otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
  409. break;
  410. case OTG_STATE_A_WAIT_VFALL:
  411. if (fsm->a_wait_vfall_tmout)
  412. otg_set_state(fsm, OTG_STATE_A_IDLE);
  413. break;
  414. case OTG_STATE_A_VBUS_ERR:
  415. if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
  416. otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
  417. break;
  418. default:
  419. break;
  420. }
  421. mutex_unlock(&fsm->lock);
  422. VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
  423. return fsm->state_changed;
  424. }
  425. EXPORT_SYMBOL_GPL(otg_statemachine);
  426. MODULE_LICENSE("GPL");