2 * OTG Finite State Machine from OTG spec
4 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
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.
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.
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.
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>
33 /* Change USB protocol when there is a protocol change */
34 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
38 if (fsm->protocol != protocol) {
39 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
40 fsm->protocol, protocol);
41 /* stop old protocol */
42 if (fsm->protocol == PROTO_HOST)
43 ret = otg_start_host(fsm, 0);
44 else if (fsm->protocol == PROTO_GADGET)
45 ret = otg_start_gadget(fsm, 0);
49 /* start new protocol */
50 if (protocol == PROTO_HOST)
51 ret = otg_start_host(fsm, 1);
52 else if (protocol == PROTO_GADGET)
53 ret = otg_start_gadget(fsm, 1);
57 fsm->protocol = protocol;
64 static int state_changed;
66 /* Called when leaving a state. Do state clean up jobs here */
67 static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
70 case OTG_STATE_B_IDLE:
71 otg_del_timer(fsm, B_SE0_SRP);
76 case OTG_STATE_B_SRP_INIT:
80 case OTG_STATE_B_PERIPHERAL:
82 fsm->otg->gadget->host_request_flag = 0;
84 case OTG_STATE_B_WAIT_ACON:
85 otg_del_timer(fsm, B_ASE0_BRST);
86 fsm->b_ase0_brst_tmout = 0;
88 case OTG_STATE_B_HOST:
90 case OTG_STATE_A_IDLE:
93 case OTG_STATE_A_WAIT_VRISE:
94 otg_del_timer(fsm, A_WAIT_VRISE);
95 fsm->a_wait_vrise_tmout = 0;
97 case OTG_STATE_A_WAIT_BCON:
98 otg_del_timer(fsm, A_WAIT_BCON);
99 fsm->a_wait_bcon_tmout = 0;
101 case OTG_STATE_A_HOST:
102 otg_del_timer(fsm, A_WAIT_ENUM);
104 case OTG_STATE_A_SUSPEND:
105 otg_del_timer(fsm, A_AIDL_BDIS);
106 fsm->a_aidl_bdis_tmout = 0;
107 fsm->a_suspend_req_inf = 0;
109 case OTG_STATE_A_PERIPHERAL:
110 otg_del_timer(fsm, A_BIDL_ADIS);
111 fsm->a_bidl_adis_tmout = 0;
112 if (fsm->otg->gadget)
113 fsm->otg->gadget->host_request_flag = 0;
115 case OTG_STATE_A_WAIT_VFALL:
116 otg_del_timer(fsm, A_WAIT_VFALL);
117 fsm->a_wait_vfall_tmout = 0;
118 otg_del_timer(fsm, A_WAIT_VRISE);
120 case OTG_STATE_A_VBUS_ERR:
127 static void otg_hnp_polling_work(struct work_struct *work)
129 struct otg_fsm *fsm = container_of(to_delayed_work(work),
130 struct otg_fsm, hnp_polling_work);
131 struct usb_device *udev;
132 enum usb_otg_state state = fsm->otg->state;
136 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
139 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
141 dev_err(fsm->otg->host->controller,
142 "no usb dev connected, can't start HNP polling\n");
146 *fsm->host_req_flag = 0;
147 /* Get host request flag from connected USB device */
148 retval = usb_control_msg(udev,
149 usb_rcvctrlpipe(udev, 0),
151 USB_DIR_IN | USB_RECIP_DEVICE,
156 USB_CTRL_GET_TIMEOUT);
158 dev_err(&udev->dev, "Get one byte OTG status failed\n");
162 flag = *fsm->host_req_flag;
164 /* Continue HNP polling */
165 schedule_delayed_work(&fsm->hnp_polling_work,
166 msecs_to_jiffies(T_HOST_REQ_POLL));
168 } else if (flag != HOST_REQUEST_FLAG) {
169 dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
173 /* Host request flag is set */
174 if (state == OTG_STATE_A_HOST) {
175 /* Set b_hnp_enable */
176 if (!fsm->otg->host->b_hnp_enable) {
177 retval = usb_control_msg(udev,
178 usb_sndctrlpipe(udev, 0),
179 USB_REQ_SET_FEATURE, 0,
180 USB_DEVICE_B_HNP_ENABLE,
182 USB_CTRL_SET_TIMEOUT);
184 fsm->otg->host->b_hnp_enable = 1;
187 } else if (state == OTG_STATE_B_HOST) {
191 otg_statemachine(fsm);
194 static void otg_start_hnp_polling(struct otg_fsm *fsm)
197 * The memory of host_req_flag should be allocated by
198 * controller driver, otherwise, hnp polling is not started.
200 if (!fsm->host_req_flag)
203 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
204 schedule_delayed_work(&fsm->hnp_polling_work,
205 msecs_to_jiffies(T_HOST_REQ_POLL));
208 /* Called when entering a state */
209 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
212 if (fsm->otg->state == new_state)
214 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
215 otg_leave_state(fsm, fsm->otg->state);
217 case OTG_STATE_B_IDLE:
218 otg_drv_vbus(fsm, 0);
219 otg_chrg_vbus(fsm, 0);
220 otg_loc_conn(fsm, 0);
223 * Driver is responsible for starting ADP probing
224 * if ADP sensing times out.
226 otg_start_adp_sns(fsm);
227 otg_set_protocol(fsm, PROTO_UNDEF);
228 otg_add_timer(fsm, B_SE0_SRP);
230 case OTG_STATE_B_SRP_INIT:
231 otg_start_pulse(fsm);
233 otg_set_protocol(fsm, PROTO_UNDEF);
234 otg_add_timer(fsm, B_SRP_FAIL);
236 case OTG_STATE_B_PERIPHERAL:
237 otg_chrg_vbus(fsm, 0);
239 otg_set_protocol(fsm, PROTO_GADGET);
240 otg_loc_conn(fsm, 1);
242 case OTG_STATE_B_WAIT_ACON:
243 otg_chrg_vbus(fsm, 0);
244 otg_loc_conn(fsm, 0);
246 otg_set_protocol(fsm, PROTO_HOST);
247 otg_add_timer(fsm, B_ASE0_BRST);
248 fsm->a_bus_suspend = 0;
250 case OTG_STATE_B_HOST:
251 otg_chrg_vbus(fsm, 0);
252 otg_loc_conn(fsm, 0);
254 otg_set_protocol(fsm, PROTO_HOST);
255 usb_bus_start_enum(fsm->otg->host,
256 fsm->otg->host->otg_port);
257 otg_start_hnp_polling(fsm);
259 case OTG_STATE_A_IDLE:
260 otg_drv_vbus(fsm, 0);
261 otg_chrg_vbus(fsm, 0);
262 otg_loc_conn(fsm, 0);
264 otg_start_adp_prb(fsm);
265 otg_set_protocol(fsm, PROTO_HOST);
267 case OTG_STATE_A_WAIT_VRISE:
268 otg_drv_vbus(fsm, 1);
269 otg_loc_conn(fsm, 0);
271 otg_set_protocol(fsm, PROTO_HOST);
272 otg_add_timer(fsm, A_WAIT_VRISE);
274 case OTG_STATE_A_WAIT_BCON:
275 otg_drv_vbus(fsm, 1);
276 otg_loc_conn(fsm, 0);
278 otg_set_protocol(fsm, PROTO_HOST);
279 otg_add_timer(fsm, A_WAIT_BCON);
281 case OTG_STATE_A_HOST:
282 otg_drv_vbus(fsm, 1);
283 otg_loc_conn(fsm, 0);
285 otg_set_protocol(fsm, PROTO_HOST);
287 * When HNP is triggered while a_bus_req = 0, a_host will
288 * suspend too fast to complete a_set_b_hnp_en
290 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
291 otg_add_timer(fsm, A_WAIT_ENUM);
292 otg_start_hnp_polling(fsm);
294 case OTG_STATE_A_SUSPEND:
295 otg_drv_vbus(fsm, 1);
296 otg_loc_conn(fsm, 0);
298 otg_set_protocol(fsm, PROTO_HOST);
299 otg_add_timer(fsm, A_AIDL_BDIS);
302 case OTG_STATE_A_PERIPHERAL:
304 otg_set_protocol(fsm, PROTO_GADGET);
305 otg_drv_vbus(fsm, 1);
306 otg_loc_conn(fsm, 1);
307 otg_add_timer(fsm, A_BIDL_ADIS);
309 case OTG_STATE_A_WAIT_VFALL:
310 otg_drv_vbus(fsm, 0);
311 otg_loc_conn(fsm, 0);
313 otg_set_protocol(fsm, PROTO_HOST);
314 otg_add_timer(fsm, A_WAIT_VFALL);
316 case OTG_STATE_A_VBUS_ERR:
317 otg_drv_vbus(fsm, 0);
318 otg_loc_conn(fsm, 0);
320 otg_set_protocol(fsm, PROTO_UNDEF);
326 fsm->otg->state = new_state;
330 /* State change judgement */
331 int otg_statemachine(struct otg_fsm *fsm)
333 enum usb_otg_state state;
335 mutex_lock(&fsm->lock);
337 state = fsm->otg->state;
339 /* State machine state change judgement */
342 case OTG_STATE_UNDEFINED:
343 VDBG("fsm->id = %d\n", fsm->id);
345 otg_set_state(fsm, OTG_STATE_B_IDLE);
347 otg_set_state(fsm, OTG_STATE_A_IDLE);
349 case OTG_STATE_B_IDLE:
351 otg_set_state(fsm, OTG_STATE_A_IDLE);
352 else if (fsm->b_sess_vld && fsm->otg->gadget)
353 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
354 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
355 fsm->b_ssend_srp && fsm->b_se0_srp)
356 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
358 case OTG_STATE_B_SRP_INIT:
359 if (!fsm->id || fsm->b_srp_done)
360 otg_set_state(fsm, OTG_STATE_B_IDLE);
362 case OTG_STATE_B_PERIPHERAL:
363 if (!fsm->id || !fsm->b_sess_vld)
364 otg_set_state(fsm, OTG_STATE_B_IDLE);
365 else if (fsm->b_bus_req && fsm->otg->
366 gadget->b_hnp_enable && fsm->a_bus_suspend)
367 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
369 case OTG_STATE_B_WAIT_ACON:
371 otg_set_state(fsm, OTG_STATE_B_HOST);
372 else if (!fsm->id || !fsm->b_sess_vld)
373 otg_set_state(fsm, OTG_STATE_B_IDLE);
374 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
375 fsm->b_ase0_brst_tmout = 0;
376 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
379 case OTG_STATE_B_HOST:
380 if (!fsm->id || !fsm->b_sess_vld)
381 otg_set_state(fsm, OTG_STATE_B_IDLE);
382 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
383 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
385 case OTG_STATE_A_IDLE:
387 otg_set_state(fsm, OTG_STATE_B_IDLE);
388 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
389 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
390 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
392 case OTG_STATE_A_WAIT_VRISE:
394 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
395 else if (fsm->id || fsm->a_bus_drop ||
396 fsm->a_wait_vrise_tmout)
397 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
399 case OTG_STATE_A_WAIT_BCON:
400 if (!fsm->a_vbus_vld)
401 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
402 else if (fsm->b_conn)
403 otg_set_state(fsm, OTG_STATE_A_HOST);
404 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
405 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
407 case OTG_STATE_A_HOST:
408 if (fsm->id || fsm->a_bus_drop)
409 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
410 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
411 fsm->otg->host->b_hnp_enable)
412 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
413 else if (!fsm->b_conn)
414 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
415 else if (!fsm->a_vbus_vld)
416 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
418 case OTG_STATE_A_SUSPEND:
419 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
420 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
421 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
422 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
423 else if (fsm->a_bus_req || fsm->b_bus_resume)
424 otg_set_state(fsm, OTG_STATE_A_HOST);
425 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
426 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
427 else if (!fsm->a_vbus_vld)
428 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
430 case OTG_STATE_A_PERIPHERAL:
431 if (fsm->id || fsm->a_bus_drop)
432 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
433 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
434 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
435 else if (!fsm->a_vbus_vld)
436 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
438 case OTG_STATE_A_WAIT_VFALL:
439 if (fsm->a_wait_vfall_tmout)
440 otg_set_state(fsm, OTG_STATE_A_IDLE);
442 case OTG_STATE_A_VBUS_ERR:
443 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
444 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
449 mutex_unlock(&fsm->lock);
451 VDBG("quit statemachine, changed = %d\n", state_changed);
452 return state_changed;
454 EXPORT_SYMBOL_GPL(otg_statemachine);