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 /* 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)
68 case OTG_STATE_B_IDLE:
69 otg_del_timer(fsm, B_SE0_SRP);
74 case OTG_STATE_B_SRP_INIT:
78 case OTG_STATE_B_PERIPHERAL:
80 fsm->otg->gadget->host_request_flag = 0;
82 case OTG_STATE_B_WAIT_ACON:
83 otg_del_timer(fsm, B_ASE0_BRST);
84 fsm->b_ase0_brst_tmout = 0;
86 case OTG_STATE_B_HOST:
88 case OTG_STATE_A_IDLE:
91 case OTG_STATE_A_WAIT_VRISE:
92 otg_del_timer(fsm, A_WAIT_VRISE);
93 fsm->a_wait_vrise_tmout = 0;
95 case OTG_STATE_A_WAIT_BCON:
96 otg_del_timer(fsm, A_WAIT_BCON);
97 fsm->a_wait_bcon_tmout = 0;
99 case OTG_STATE_A_HOST:
100 otg_del_timer(fsm, A_WAIT_ENUM);
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;
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;
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);
118 case OTG_STATE_A_VBUS_ERR:
125 static void otg_hnp_polling_work(struct work_struct *work)
127 struct otg_fsm *fsm = container_of(to_delayed_work(work),
128 struct otg_fsm, hnp_polling_work);
129 struct usb_device *udev;
130 enum usb_otg_state state = fsm->otg->state;
134 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
137 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
139 dev_err(fsm->otg->host->controller,
140 "no usb dev connected, can't start HNP polling\n");
144 *fsm->host_req_flag = 0;
145 /* Get host request flag from connected USB device */
146 retval = usb_control_msg(udev,
147 usb_rcvctrlpipe(udev, 0),
149 USB_DIR_IN | USB_RECIP_DEVICE,
154 USB_CTRL_GET_TIMEOUT);
156 dev_err(&udev->dev, "Get one byte OTG status failed\n");
160 flag = *fsm->host_req_flag;
162 /* Continue HNP polling */
163 schedule_delayed_work(&fsm->hnp_polling_work,
164 msecs_to_jiffies(T_HOST_REQ_POLL));
166 } else if (flag != HOST_REQUEST_FLAG) {
167 dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
171 /* Host request flag is set */
172 if (state == OTG_STATE_A_HOST) {
173 /* Set b_hnp_enable */
174 if (!fsm->otg->host->b_hnp_enable) {
175 retval = usb_control_msg(udev,
176 usb_sndctrlpipe(udev, 0),
177 USB_REQ_SET_FEATURE, 0,
178 USB_DEVICE_B_HNP_ENABLE,
180 USB_CTRL_SET_TIMEOUT);
182 fsm->otg->host->b_hnp_enable = 1;
185 } else if (state == OTG_STATE_B_HOST) {
189 otg_statemachine(fsm);
192 static void otg_start_hnp_polling(struct otg_fsm *fsm)
195 * The memory of host_req_flag should be allocated by
196 * controller driver, otherwise, hnp polling is not started.
198 if (!fsm->host_req_flag)
201 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
202 schedule_delayed_work(&fsm->hnp_polling_work,
203 msecs_to_jiffies(T_HOST_REQ_POLL));
206 /* Called when entering a state */
207 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
209 if (fsm->otg->state == new_state)
211 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
212 otg_leave_state(fsm, fsm->otg->state);
214 case OTG_STATE_B_IDLE:
215 otg_drv_vbus(fsm, 0);
216 otg_chrg_vbus(fsm, 0);
217 otg_loc_conn(fsm, 0);
220 * Driver is responsible for starting ADP probing
221 * if ADP sensing times out.
223 otg_start_adp_sns(fsm);
224 otg_set_protocol(fsm, PROTO_UNDEF);
225 otg_add_timer(fsm, B_SE0_SRP);
227 case OTG_STATE_B_SRP_INIT:
228 otg_start_pulse(fsm);
230 otg_set_protocol(fsm, PROTO_UNDEF);
231 otg_add_timer(fsm, B_SRP_FAIL);
233 case OTG_STATE_B_PERIPHERAL:
234 otg_chrg_vbus(fsm, 0);
236 otg_set_protocol(fsm, PROTO_GADGET);
237 otg_loc_conn(fsm, 1);
239 case OTG_STATE_B_WAIT_ACON:
240 otg_chrg_vbus(fsm, 0);
241 otg_loc_conn(fsm, 0);
243 otg_set_protocol(fsm, PROTO_HOST);
244 otg_add_timer(fsm, B_ASE0_BRST);
245 fsm->a_bus_suspend = 0;
247 case OTG_STATE_B_HOST:
248 otg_chrg_vbus(fsm, 0);
249 otg_loc_conn(fsm, 0);
251 otg_set_protocol(fsm, PROTO_HOST);
252 usb_bus_start_enum(fsm->otg->host,
253 fsm->otg->host->otg_port);
254 otg_start_hnp_polling(fsm);
256 case OTG_STATE_A_IDLE:
257 otg_drv_vbus(fsm, 0);
258 otg_chrg_vbus(fsm, 0);
259 otg_loc_conn(fsm, 0);
261 otg_start_adp_prb(fsm);
262 otg_set_protocol(fsm, PROTO_HOST);
264 case OTG_STATE_A_WAIT_VRISE:
265 otg_drv_vbus(fsm, 1);
266 otg_loc_conn(fsm, 0);
268 otg_set_protocol(fsm, PROTO_HOST);
269 otg_add_timer(fsm, A_WAIT_VRISE);
271 case OTG_STATE_A_WAIT_BCON:
272 otg_drv_vbus(fsm, 1);
273 otg_loc_conn(fsm, 0);
275 otg_set_protocol(fsm, PROTO_HOST);
276 otg_add_timer(fsm, A_WAIT_BCON);
278 case OTG_STATE_A_HOST:
279 otg_drv_vbus(fsm, 1);
280 otg_loc_conn(fsm, 0);
282 otg_set_protocol(fsm, PROTO_HOST);
284 * When HNP is triggered while a_bus_req = 0, a_host will
285 * suspend too fast to complete a_set_b_hnp_en
287 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
288 otg_add_timer(fsm, A_WAIT_ENUM);
289 otg_start_hnp_polling(fsm);
291 case OTG_STATE_A_SUSPEND:
292 otg_drv_vbus(fsm, 1);
293 otg_loc_conn(fsm, 0);
295 otg_set_protocol(fsm, PROTO_HOST);
296 otg_add_timer(fsm, A_AIDL_BDIS);
299 case OTG_STATE_A_PERIPHERAL:
301 otg_set_protocol(fsm, PROTO_GADGET);
302 otg_drv_vbus(fsm, 1);
303 otg_loc_conn(fsm, 1);
304 otg_add_timer(fsm, A_BIDL_ADIS);
306 case OTG_STATE_A_WAIT_VFALL:
307 otg_drv_vbus(fsm, 0);
308 otg_loc_conn(fsm, 0);
310 otg_set_protocol(fsm, PROTO_HOST);
311 otg_add_timer(fsm, A_WAIT_VFALL);
313 case OTG_STATE_A_VBUS_ERR:
314 otg_drv_vbus(fsm, 0);
315 otg_loc_conn(fsm, 0);
317 otg_set_protocol(fsm, PROTO_UNDEF);
323 fsm->otg->state = new_state;
324 fsm->state_changed = 1;
328 /* State change judgement */
329 int otg_statemachine(struct otg_fsm *fsm)
331 enum usb_otg_state state;
333 mutex_lock(&fsm->lock);
335 state = fsm->otg->state;
336 fsm->state_changed = 0;
337 /* State machine state change judgement */
340 case OTG_STATE_UNDEFINED:
341 VDBG("fsm->id = %d\n", fsm->id);
343 otg_set_state(fsm, OTG_STATE_B_IDLE);
345 otg_set_state(fsm, OTG_STATE_A_IDLE);
347 case OTG_STATE_B_IDLE:
349 otg_set_state(fsm, OTG_STATE_A_IDLE);
350 else if (fsm->b_sess_vld && fsm->otg->gadget)
351 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
352 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
353 fsm->b_ssend_srp && fsm->b_se0_srp)
354 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
356 case OTG_STATE_B_SRP_INIT:
357 if (!fsm->id || fsm->b_srp_done)
358 otg_set_state(fsm, OTG_STATE_B_IDLE);
360 case OTG_STATE_B_PERIPHERAL:
361 if (!fsm->id || !fsm->b_sess_vld)
362 otg_set_state(fsm, OTG_STATE_B_IDLE);
363 else if (fsm->b_bus_req && fsm->otg->
364 gadget->b_hnp_enable && fsm->a_bus_suspend)
365 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
367 case OTG_STATE_B_WAIT_ACON:
369 otg_set_state(fsm, OTG_STATE_B_HOST);
370 else if (!fsm->id || !fsm->b_sess_vld)
371 otg_set_state(fsm, OTG_STATE_B_IDLE);
372 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
373 fsm->b_ase0_brst_tmout = 0;
374 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
377 case OTG_STATE_B_HOST:
378 if (!fsm->id || !fsm->b_sess_vld)
379 otg_set_state(fsm, OTG_STATE_B_IDLE);
380 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
381 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
383 case OTG_STATE_A_IDLE:
385 otg_set_state(fsm, OTG_STATE_B_IDLE);
386 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
387 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
388 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
390 case OTG_STATE_A_WAIT_VRISE:
392 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
393 else if (fsm->id || fsm->a_bus_drop ||
394 fsm->a_wait_vrise_tmout)
395 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
397 case OTG_STATE_A_WAIT_BCON:
398 if (!fsm->a_vbus_vld)
399 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
400 else if (fsm->b_conn)
401 otg_set_state(fsm, OTG_STATE_A_HOST);
402 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
403 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
405 case OTG_STATE_A_HOST:
406 if (fsm->id || fsm->a_bus_drop)
407 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
408 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
409 fsm->otg->host->b_hnp_enable)
410 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
411 else if (!fsm->b_conn)
412 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
413 else if (!fsm->a_vbus_vld)
414 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
416 case OTG_STATE_A_SUSPEND:
417 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
418 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
419 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
420 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
421 else if (fsm->a_bus_req || fsm->b_bus_resume)
422 otg_set_state(fsm, OTG_STATE_A_HOST);
423 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
424 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
425 else if (!fsm->a_vbus_vld)
426 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
428 case OTG_STATE_A_PERIPHERAL:
429 if (fsm->id || fsm->a_bus_drop)
430 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
431 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
432 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
433 else if (!fsm->a_vbus_vld)
434 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
436 case OTG_STATE_A_WAIT_VFALL:
437 if (fsm->a_wait_vfall_tmout)
438 otg_set_state(fsm, OTG_STATE_A_IDLE);
440 case OTG_STATE_A_VBUS_ERR:
441 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
442 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
447 mutex_unlock(&fsm->lock);
449 VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
450 return fsm->state_changed;
452 EXPORT_SYMBOL_GPL(otg_statemachine);