2 * OTG Finite State Machine from OTG spec
4 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
6 * Author: Li Yang <LeoLi@freescale.com>
7 * Jerry Huang <Chang-Ming.Huang@freescale.com>
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/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/usb.h>
30 #include <linux/usb/gadget.h>
31 #include <linux/usb/otg.h>
32 #include <linux/usb/otg-fsm.h>
35 #define VDBG(fmt, args...) pr_debug("[%s] " fmt, \
38 #define VDBG(stuff...) do {} while (0)
41 /* Change USB protocol when there is a protocol change */
42 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
46 if (fsm->protocol != protocol) {
47 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
48 fsm->protocol, protocol);
49 /* stop old protocol */
50 if (fsm->protocol == PROTO_HOST)
51 ret = otg_start_host(fsm, 0);
52 else if (fsm->protocol == PROTO_GADGET)
53 ret = otg_start_gadget(fsm, 0);
57 /* start new protocol */
58 if (protocol == PROTO_HOST)
59 ret = otg_start_host(fsm, 1);
60 else if (protocol == PROTO_GADGET)
61 ret = otg_start_gadget(fsm, 1);
65 fsm->protocol = protocol;
72 /* Called when leaving a state. Do state clean up jobs here */
73 static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
76 case OTG_STATE_B_IDLE:
77 otg_del_timer(fsm, B_SE0_SRP);
82 case OTG_STATE_B_SRP_INIT:
86 case OTG_STATE_B_PERIPHERAL:
88 fsm->otg->gadget->host_request_flag = 0;
90 case OTG_STATE_B_WAIT_ACON:
91 otg_del_timer(fsm, B_ASE0_BRST);
92 fsm->b_ase0_brst_tmout = 0;
94 case OTG_STATE_B_HOST:
96 case OTG_STATE_A_IDLE:
99 case OTG_STATE_A_WAIT_VRISE:
100 otg_del_timer(fsm, A_WAIT_VRISE);
101 fsm->a_wait_vrise_tmout = 0;
103 case OTG_STATE_A_WAIT_BCON:
104 otg_del_timer(fsm, A_WAIT_BCON);
105 fsm->a_wait_bcon_tmout = 0;
107 case OTG_STATE_A_HOST:
108 otg_del_timer(fsm, A_WAIT_ENUM);
110 case OTG_STATE_A_SUSPEND:
111 otg_del_timer(fsm, A_AIDL_BDIS);
112 fsm->a_aidl_bdis_tmout = 0;
113 fsm->a_suspend_req_inf = 0;
115 case OTG_STATE_A_PERIPHERAL:
116 otg_del_timer(fsm, A_BIDL_ADIS);
117 fsm->a_bidl_adis_tmout = 0;
118 if (fsm->otg->gadget)
119 fsm->otg->gadget->host_request_flag = 0;
121 case OTG_STATE_A_WAIT_VFALL:
122 otg_del_timer(fsm, A_WAIT_VFALL);
123 fsm->a_wait_vfall_tmout = 0;
124 otg_del_timer(fsm, A_WAIT_VRISE);
126 case OTG_STATE_A_VBUS_ERR:
133 static void otg_hnp_polling_work(struct work_struct *work)
135 struct otg_fsm *fsm = container_of(to_delayed_work(work),
136 struct otg_fsm, hnp_polling_work);
137 struct usb_device *udev;
138 enum usb_otg_state state = fsm->otg->state;
142 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
145 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
147 dev_err(fsm->otg->host->controller,
148 "no usb dev connected, can't start HNP polling\n");
152 *fsm->host_req_flag = 0;
153 /* Get host request flag from connected USB device */
154 retval = usb_control_msg(udev,
155 usb_rcvctrlpipe(udev, 0),
157 USB_DIR_IN | USB_RECIP_DEVICE,
162 USB_CTRL_GET_TIMEOUT);
164 dev_err(&udev->dev, "Get one byte OTG status failed\n");
168 flag = *fsm->host_req_flag;
170 /* Continue HNP polling */
171 schedule_delayed_work(&fsm->hnp_polling_work,
172 msecs_to_jiffies(T_HOST_REQ_POLL));
174 } else if (flag != HOST_REQUEST_FLAG) {
175 dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
179 /* Host request flag is set */
180 if (state == OTG_STATE_A_HOST) {
181 /* Set b_hnp_enable */
182 if (!fsm->otg->host->b_hnp_enable) {
183 retval = usb_control_msg(udev,
184 usb_sndctrlpipe(udev, 0),
185 USB_REQ_SET_FEATURE, 0,
186 USB_DEVICE_B_HNP_ENABLE,
188 USB_CTRL_SET_TIMEOUT);
190 fsm->otg->host->b_hnp_enable = 1;
193 } else if (state == OTG_STATE_B_HOST) {
197 otg_statemachine(fsm);
200 static void otg_start_hnp_polling(struct otg_fsm *fsm)
203 * The memory of host_req_flag should be allocated by
204 * controller driver, otherwise, hnp polling is not started.
206 if (!fsm->host_req_flag)
209 if (!fsm->hnp_work_inited) {
210 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
211 fsm->hnp_work_inited = true;
214 schedule_delayed_work(&fsm->hnp_polling_work,
215 msecs_to_jiffies(T_HOST_REQ_POLL));
218 /* Called when entering a state */
219 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
221 if (fsm->otg->state == new_state)
223 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
224 otg_leave_state(fsm, fsm->otg->state);
226 case OTG_STATE_B_IDLE:
227 otg_drv_vbus(fsm, 0);
228 otg_chrg_vbus(fsm, 0);
229 otg_loc_conn(fsm, 0);
232 * Driver is responsible for starting ADP probing
233 * if ADP sensing times out.
235 otg_start_adp_sns(fsm);
236 otg_set_protocol(fsm, PROTO_UNDEF);
237 otg_add_timer(fsm, B_SE0_SRP);
239 case OTG_STATE_B_SRP_INIT:
240 otg_start_pulse(fsm);
242 otg_set_protocol(fsm, PROTO_UNDEF);
243 otg_add_timer(fsm, B_SRP_FAIL);
245 case OTG_STATE_B_PERIPHERAL:
246 otg_chrg_vbus(fsm, 0);
248 otg_set_protocol(fsm, PROTO_GADGET);
249 otg_loc_conn(fsm, 1);
251 case OTG_STATE_B_WAIT_ACON:
252 otg_chrg_vbus(fsm, 0);
253 otg_loc_conn(fsm, 0);
255 otg_set_protocol(fsm, PROTO_HOST);
256 otg_add_timer(fsm, B_ASE0_BRST);
257 fsm->a_bus_suspend = 0;
259 case OTG_STATE_B_HOST:
260 otg_chrg_vbus(fsm, 0);
261 otg_loc_conn(fsm, 0);
263 otg_set_protocol(fsm, PROTO_HOST);
264 usb_bus_start_enum(fsm->otg->host,
265 fsm->otg->host->otg_port);
266 otg_start_hnp_polling(fsm);
268 case OTG_STATE_A_IDLE:
269 otg_drv_vbus(fsm, 0);
270 otg_chrg_vbus(fsm, 0);
271 otg_loc_conn(fsm, 0);
273 otg_start_adp_prb(fsm);
274 otg_set_protocol(fsm, PROTO_HOST);
276 case OTG_STATE_A_WAIT_VRISE:
277 otg_drv_vbus(fsm, 1);
278 otg_loc_conn(fsm, 0);
280 otg_set_protocol(fsm, PROTO_HOST);
281 otg_add_timer(fsm, A_WAIT_VRISE);
283 case OTG_STATE_A_WAIT_BCON:
284 otg_drv_vbus(fsm, 1);
285 otg_loc_conn(fsm, 0);
287 otg_set_protocol(fsm, PROTO_HOST);
288 otg_add_timer(fsm, A_WAIT_BCON);
290 case OTG_STATE_A_HOST:
291 otg_drv_vbus(fsm, 1);
292 otg_loc_conn(fsm, 0);
294 otg_set_protocol(fsm, PROTO_HOST);
296 * When HNP is triggered while a_bus_req = 0, a_host will
297 * suspend too fast to complete a_set_b_hnp_en
299 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
300 otg_add_timer(fsm, A_WAIT_ENUM);
301 otg_start_hnp_polling(fsm);
303 case OTG_STATE_A_SUSPEND:
304 otg_drv_vbus(fsm, 1);
305 otg_loc_conn(fsm, 0);
307 otg_set_protocol(fsm, PROTO_HOST);
308 otg_add_timer(fsm, A_AIDL_BDIS);
311 case OTG_STATE_A_PERIPHERAL:
313 otg_set_protocol(fsm, PROTO_GADGET);
314 otg_drv_vbus(fsm, 1);
315 otg_loc_conn(fsm, 1);
316 otg_add_timer(fsm, A_BIDL_ADIS);
318 case OTG_STATE_A_WAIT_VFALL:
319 otg_drv_vbus(fsm, 0);
320 otg_loc_conn(fsm, 0);
322 otg_set_protocol(fsm, PROTO_HOST);
323 otg_add_timer(fsm, A_WAIT_VFALL);
325 case OTG_STATE_A_VBUS_ERR:
326 otg_drv_vbus(fsm, 0);
327 otg_loc_conn(fsm, 0);
329 otg_set_protocol(fsm, PROTO_UNDEF);
335 fsm->otg->state = new_state;
336 fsm->state_changed = 1;
340 /* State change judgement */
341 int otg_statemachine(struct otg_fsm *fsm)
343 enum usb_otg_state state;
345 mutex_lock(&fsm->lock);
347 state = fsm->otg->state;
348 fsm->state_changed = 0;
349 /* State machine state change judgement */
352 case OTG_STATE_UNDEFINED:
353 VDBG("fsm->id = %d\n", fsm->id);
355 otg_set_state(fsm, OTG_STATE_B_IDLE);
357 otg_set_state(fsm, OTG_STATE_A_IDLE);
359 case OTG_STATE_B_IDLE:
361 otg_set_state(fsm, OTG_STATE_A_IDLE);
362 else if (fsm->b_sess_vld && fsm->otg->gadget)
363 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
364 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
365 fsm->b_ssend_srp && fsm->b_se0_srp)
366 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
368 case OTG_STATE_B_SRP_INIT:
369 if (!fsm->id || fsm->b_srp_done)
370 otg_set_state(fsm, OTG_STATE_B_IDLE);
372 case OTG_STATE_B_PERIPHERAL:
373 if (!fsm->id || !fsm->b_sess_vld)
374 otg_set_state(fsm, OTG_STATE_B_IDLE);
375 else if (fsm->b_bus_req && fsm->otg->
376 gadget->b_hnp_enable && fsm->a_bus_suspend)
377 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
379 case OTG_STATE_B_WAIT_ACON:
381 otg_set_state(fsm, OTG_STATE_B_HOST);
382 else if (!fsm->id || !fsm->b_sess_vld)
383 otg_set_state(fsm, OTG_STATE_B_IDLE);
384 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
385 fsm->b_ase0_brst_tmout = 0;
386 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
389 case OTG_STATE_B_HOST:
390 if (!fsm->id || !fsm->b_sess_vld)
391 otg_set_state(fsm, OTG_STATE_B_IDLE);
392 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
393 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
395 case OTG_STATE_A_IDLE:
397 otg_set_state(fsm, OTG_STATE_B_IDLE);
398 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
399 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
400 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
402 case OTG_STATE_A_WAIT_VRISE:
404 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
405 else if (fsm->id || fsm->a_bus_drop ||
406 fsm->a_wait_vrise_tmout)
407 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
409 case OTG_STATE_A_WAIT_BCON:
410 if (!fsm->a_vbus_vld)
411 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
412 else if (fsm->b_conn)
413 otg_set_state(fsm, OTG_STATE_A_HOST);
414 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
415 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
417 case OTG_STATE_A_HOST:
418 if (fsm->id || fsm->a_bus_drop)
419 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
420 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
421 fsm->otg->host->b_hnp_enable)
422 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
423 else if (!fsm->b_conn)
424 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
425 else if (!fsm->a_vbus_vld)
426 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
428 case OTG_STATE_A_SUSPEND:
429 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
430 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
431 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
432 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
433 else if (fsm->a_bus_req || fsm->b_bus_resume)
434 otg_set_state(fsm, OTG_STATE_A_HOST);
435 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
436 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
437 else if (!fsm->a_vbus_vld)
438 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
440 case OTG_STATE_A_PERIPHERAL:
441 if (fsm->id || fsm->a_bus_drop)
442 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
443 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
444 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
445 else if (!fsm->a_vbus_vld)
446 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
448 case OTG_STATE_A_WAIT_VFALL:
449 if (fsm->a_wait_vfall_tmout)
450 otg_set_state(fsm, OTG_STATE_A_IDLE);
452 case OTG_STATE_A_VBUS_ERR:
453 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
454 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
459 mutex_unlock(&fsm->lock);
461 VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
462 return fsm->state_changed;
464 EXPORT_SYMBOL_GPL(otg_statemachine);
465 MODULE_LICENSE("GPL");