1 // SPDX-License-Identifier: GPL-2.0+
3 * OTG Finite State Machine from OTG spec
5 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
7 * Author: Li Yang <LeoLi@freescale.com>
8 * Jerry Huang <Chang-Ming.Huang@freescale.com>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/mutex.h>
15 #include <linux/delay.h>
16 #include <linux/usb.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/otg.h>
19 #include <linux/usb/otg-fsm.h>
22 #define VDBG(fmt, args...) pr_debug("[%s] " fmt, \
25 #define VDBG(stuff...) do {} while (0)
28 /* Change USB protocol when there is a protocol change */
29 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
33 if (fsm->protocol != protocol) {
34 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
35 fsm->protocol, protocol);
36 /* stop old protocol */
37 if (fsm->protocol == PROTO_HOST)
38 ret = otg_start_host(fsm, 0);
39 else if (fsm->protocol == PROTO_GADGET)
40 ret = otg_start_gadget(fsm, 0);
44 /* start new protocol */
45 if (protocol == PROTO_HOST)
46 ret = otg_start_host(fsm, 1);
47 else if (protocol == PROTO_GADGET)
48 ret = otg_start_gadget(fsm, 1);
52 fsm->protocol = protocol;
59 /* Called when leaving a state. Do state clean up jobs here */
60 static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
63 case OTG_STATE_B_IDLE:
64 otg_del_timer(fsm, B_SE0_SRP);
69 case OTG_STATE_B_SRP_INIT:
73 case OTG_STATE_B_PERIPHERAL:
75 fsm->otg->gadget->host_request_flag = 0;
77 case OTG_STATE_B_WAIT_ACON:
78 otg_del_timer(fsm, B_ASE0_BRST);
79 fsm->b_ase0_brst_tmout = 0;
81 case OTG_STATE_B_HOST:
83 case OTG_STATE_A_IDLE:
86 case OTG_STATE_A_WAIT_VRISE:
87 otg_del_timer(fsm, A_WAIT_VRISE);
88 fsm->a_wait_vrise_tmout = 0;
90 case OTG_STATE_A_WAIT_BCON:
91 otg_del_timer(fsm, A_WAIT_BCON);
92 fsm->a_wait_bcon_tmout = 0;
94 case OTG_STATE_A_HOST:
95 otg_del_timer(fsm, A_WAIT_ENUM);
97 case OTG_STATE_A_SUSPEND:
98 otg_del_timer(fsm, A_AIDL_BDIS);
99 fsm->a_aidl_bdis_tmout = 0;
100 fsm->a_suspend_req_inf = 0;
102 case OTG_STATE_A_PERIPHERAL:
103 otg_del_timer(fsm, A_BIDL_ADIS);
104 fsm->a_bidl_adis_tmout = 0;
105 if (fsm->otg->gadget)
106 fsm->otg->gadget->host_request_flag = 0;
108 case OTG_STATE_A_WAIT_VFALL:
109 otg_del_timer(fsm, A_WAIT_VFALL);
110 fsm->a_wait_vfall_tmout = 0;
111 otg_del_timer(fsm, A_WAIT_VRISE);
113 case OTG_STATE_A_VBUS_ERR:
120 static void otg_hnp_polling_work(struct work_struct *work)
122 struct otg_fsm *fsm = container_of(to_delayed_work(work),
123 struct otg_fsm, hnp_polling_work);
124 struct usb_device *udev;
125 enum usb_otg_state state = fsm->otg->state;
129 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
132 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
134 dev_err(fsm->otg->host->controller,
135 "no usb dev connected, can't start HNP polling\n");
139 *fsm->host_req_flag = 0;
140 /* Get host request flag from connected USB device */
141 retval = usb_control_msg(udev,
142 usb_rcvctrlpipe(udev, 0),
144 USB_DIR_IN | USB_RECIP_DEVICE,
149 USB_CTRL_GET_TIMEOUT);
151 dev_err(&udev->dev, "Get one byte OTG status failed\n");
155 flag = *fsm->host_req_flag;
157 /* Continue HNP polling */
158 schedule_delayed_work(&fsm->hnp_polling_work,
159 msecs_to_jiffies(T_HOST_REQ_POLL));
161 } else if (flag != HOST_REQUEST_FLAG) {
162 dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
166 /* Host request flag is set */
167 if (state == OTG_STATE_A_HOST) {
168 /* Set b_hnp_enable */
169 if (!fsm->otg->host->b_hnp_enable) {
170 retval = usb_control_msg(udev,
171 usb_sndctrlpipe(udev, 0),
172 USB_REQ_SET_FEATURE, 0,
173 USB_DEVICE_B_HNP_ENABLE,
175 USB_CTRL_SET_TIMEOUT);
177 fsm->otg->host->b_hnp_enable = 1;
180 } else if (state == OTG_STATE_B_HOST) {
184 otg_statemachine(fsm);
187 static void otg_start_hnp_polling(struct otg_fsm *fsm)
190 * The memory of host_req_flag should be allocated by
191 * controller driver, otherwise, hnp polling is not started.
193 if (!fsm->host_req_flag)
196 if (!fsm->hnp_work_inited) {
197 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
198 fsm->hnp_work_inited = true;
201 schedule_delayed_work(&fsm->hnp_polling_work,
202 msecs_to_jiffies(T_HOST_REQ_POLL));
205 /* Called when entering a state */
206 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
208 if (fsm->otg->state == new_state)
210 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
211 otg_leave_state(fsm, fsm->otg->state);
213 case OTG_STATE_B_IDLE:
214 otg_drv_vbus(fsm, 0);
215 otg_chrg_vbus(fsm, 0);
216 otg_loc_conn(fsm, 0);
219 * Driver is responsible for starting ADP probing
220 * if ADP sensing times out.
222 otg_start_adp_sns(fsm);
223 otg_set_protocol(fsm, PROTO_UNDEF);
224 otg_add_timer(fsm, B_SE0_SRP);
226 case OTG_STATE_B_SRP_INIT:
227 otg_start_pulse(fsm);
229 otg_set_protocol(fsm, PROTO_UNDEF);
230 otg_add_timer(fsm, B_SRP_FAIL);
232 case OTG_STATE_B_PERIPHERAL:
233 otg_chrg_vbus(fsm, 0);
235 otg_set_protocol(fsm, PROTO_GADGET);
236 otg_loc_conn(fsm, 1);
238 case OTG_STATE_B_WAIT_ACON:
239 otg_chrg_vbus(fsm, 0);
240 otg_loc_conn(fsm, 0);
242 otg_set_protocol(fsm, PROTO_HOST);
243 otg_add_timer(fsm, B_ASE0_BRST);
244 fsm->a_bus_suspend = 0;
246 case OTG_STATE_B_HOST:
247 otg_chrg_vbus(fsm, 0);
248 otg_loc_conn(fsm, 0);
250 otg_set_protocol(fsm, PROTO_HOST);
251 usb_bus_start_enum(fsm->otg->host,
252 fsm->otg->host->otg_port);
253 otg_start_hnp_polling(fsm);
255 case OTG_STATE_A_IDLE:
256 otg_drv_vbus(fsm, 0);
257 otg_chrg_vbus(fsm, 0);
258 otg_loc_conn(fsm, 0);
260 otg_start_adp_prb(fsm);
261 otg_set_protocol(fsm, PROTO_HOST);
263 case OTG_STATE_A_WAIT_VRISE:
264 otg_drv_vbus(fsm, 1);
265 otg_loc_conn(fsm, 0);
267 otg_set_protocol(fsm, PROTO_HOST);
268 otg_add_timer(fsm, A_WAIT_VRISE);
270 case OTG_STATE_A_WAIT_BCON:
271 otg_drv_vbus(fsm, 1);
272 otg_loc_conn(fsm, 0);
274 otg_set_protocol(fsm, PROTO_HOST);
275 otg_add_timer(fsm, A_WAIT_BCON);
277 case OTG_STATE_A_HOST:
278 otg_drv_vbus(fsm, 1);
279 otg_loc_conn(fsm, 0);
281 otg_set_protocol(fsm, PROTO_HOST);
283 * When HNP is triggered while a_bus_req = 0, a_host will
284 * suspend too fast to complete a_set_b_hnp_en
286 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
287 otg_add_timer(fsm, A_WAIT_ENUM);
288 otg_start_hnp_polling(fsm);
290 case OTG_STATE_A_SUSPEND:
291 otg_drv_vbus(fsm, 1);
292 otg_loc_conn(fsm, 0);
294 otg_set_protocol(fsm, PROTO_HOST);
295 otg_add_timer(fsm, A_AIDL_BDIS);
298 case OTG_STATE_A_PERIPHERAL:
300 otg_set_protocol(fsm, PROTO_GADGET);
301 otg_drv_vbus(fsm, 1);
302 otg_loc_conn(fsm, 1);
303 otg_add_timer(fsm, A_BIDL_ADIS);
305 case OTG_STATE_A_WAIT_VFALL:
306 otg_drv_vbus(fsm, 0);
307 otg_loc_conn(fsm, 0);
309 otg_set_protocol(fsm, PROTO_HOST);
310 otg_add_timer(fsm, A_WAIT_VFALL);
312 case OTG_STATE_A_VBUS_ERR:
313 otg_drv_vbus(fsm, 0);
314 otg_loc_conn(fsm, 0);
316 otg_set_protocol(fsm, PROTO_UNDEF);
322 fsm->otg->state = new_state;
323 fsm->state_changed = 1;
327 /* State change judgement */
328 int otg_statemachine(struct otg_fsm *fsm)
330 enum usb_otg_state state;
332 mutex_lock(&fsm->lock);
334 state = fsm->otg->state;
335 fsm->state_changed = 0;
336 /* State machine state change judgement */
339 case OTG_STATE_UNDEFINED:
340 VDBG("fsm->id = %d\n", fsm->id);
342 otg_set_state(fsm, OTG_STATE_B_IDLE);
344 otg_set_state(fsm, OTG_STATE_A_IDLE);
346 case OTG_STATE_B_IDLE:
348 otg_set_state(fsm, OTG_STATE_A_IDLE);
349 else if (fsm->b_sess_vld && fsm->otg->gadget)
350 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
351 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
352 fsm->b_ssend_srp && fsm->b_se0_srp)
353 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
355 case OTG_STATE_B_SRP_INIT:
356 if (!fsm->id || fsm->b_srp_done)
357 otg_set_state(fsm, OTG_STATE_B_IDLE);
359 case OTG_STATE_B_PERIPHERAL:
360 if (!fsm->id || !fsm->b_sess_vld)
361 otg_set_state(fsm, OTG_STATE_B_IDLE);
362 else if (fsm->b_bus_req && fsm->otg->
363 gadget->b_hnp_enable && fsm->a_bus_suspend)
364 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
366 case OTG_STATE_B_WAIT_ACON:
368 otg_set_state(fsm, OTG_STATE_B_HOST);
369 else if (!fsm->id || !fsm->b_sess_vld)
370 otg_set_state(fsm, OTG_STATE_B_IDLE);
371 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
372 fsm->b_ase0_brst_tmout = 0;
373 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
376 case OTG_STATE_B_HOST:
377 if (!fsm->id || !fsm->b_sess_vld)
378 otg_set_state(fsm, OTG_STATE_B_IDLE);
379 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
380 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
382 case OTG_STATE_A_IDLE:
384 otg_set_state(fsm, OTG_STATE_B_IDLE);
385 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
386 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
387 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
389 case OTG_STATE_A_WAIT_VRISE:
391 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
392 else if (fsm->id || fsm->a_bus_drop ||
393 fsm->a_wait_vrise_tmout)
394 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
396 case OTG_STATE_A_WAIT_BCON:
397 if (!fsm->a_vbus_vld)
398 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
399 else if (fsm->b_conn)
400 otg_set_state(fsm, OTG_STATE_A_HOST);
401 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
402 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
404 case OTG_STATE_A_HOST:
405 if (fsm->id || fsm->a_bus_drop)
406 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
407 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
408 fsm->otg->host->b_hnp_enable)
409 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
410 else if (!fsm->b_conn)
411 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
412 else if (!fsm->a_vbus_vld)
413 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
415 case OTG_STATE_A_SUSPEND:
416 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
417 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
418 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
419 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
420 else if (fsm->a_bus_req || fsm->b_bus_resume)
421 otg_set_state(fsm, OTG_STATE_A_HOST);
422 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
423 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
424 else if (!fsm->a_vbus_vld)
425 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
427 case OTG_STATE_A_PERIPHERAL:
428 if (fsm->id || fsm->a_bus_drop)
429 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
430 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
431 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
432 else if (!fsm->a_vbus_vld)
433 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
435 case OTG_STATE_A_WAIT_VFALL:
436 if (fsm->a_wait_vfall_tmout)
437 otg_set_state(fsm, OTG_STATE_A_IDLE);
439 case OTG_STATE_A_VBUS_ERR:
440 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
441 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
446 mutex_unlock(&fsm->lock);
448 VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
449 return fsm->state_changed;
451 EXPORT_SYMBOL_GPL(otg_statemachine);
452 MODULE_LICENSE("GPL");