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 INIT_DELAYED_WORK(&fsm
->hnp_polling_work
, otg_hnp_polling_work
);
210 schedule_delayed_work(&fsm
->hnp_polling_work
,
211 msecs_to_jiffies(T_HOST_REQ_POLL
));
214 /* Called when entering a state */
215 static int otg_set_state(struct otg_fsm
*fsm
, enum usb_otg_state new_state
)
217 if (fsm
->otg
->state
== new_state
)
219 VDBG("Set state: %s\n", usb_otg_state_string(new_state
));
220 otg_leave_state(fsm
, fsm
->otg
->state
);
222 case OTG_STATE_B_IDLE
:
223 otg_drv_vbus(fsm
, 0);
224 otg_chrg_vbus(fsm
, 0);
225 otg_loc_conn(fsm
, 0);
228 * Driver is responsible for starting ADP probing
229 * if ADP sensing times out.
231 otg_start_adp_sns(fsm
);
232 otg_set_protocol(fsm
, PROTO_UNDEF
);
233 otg_add_timer(fsm
, B_SE0_SRP
);
235 case OTG_STATE_B_SRP_INIT
:
236 otg_start_pulse(fsm
);
238 otg_set_protocol(fsm
, PROTO_UNDEF
);
239 otg_add_timer(fsm
, B_SRP_FAIL
);
241 case OTG_STATE_B_PERIPHERAL
:
242 otg_chrg_vbus(fsm
, 0);
244 otg_set_protocol(fsm
, PROTO_GADGET
);
245 otg_loc_conn(fsm
, 1);
247 case OTG_STATE_B_WAIT_ACON
:
248 otg_chrg_vbus(fsm
, 0);
249 otg_loc_conn(fsm
, 0);
251 otg_set_protocol(fsm
, PROTO_HOST
);
252 otg_add_timer(fsm
, B_ASE0_BRST
);
253 fsm
->a_bus_suspend
= 0;
255 case OTG_STATE_B_HOST
:
256 otg_chrg_vbus(fsm
, 0);
257 otg_loc_conn(fsm
, 0);
259 otg_set_protocol(fsm
, PROTO_HOST
);
260 usb_bus_start_enum(fsm
->otg
->host
,
261 fsm
->otg
->host
->otg_port
);
262 otg_start_hnp_polling(fsm
);
264 case OTG_STATE_A_IDLE
:
265 otg_drv_vbus(fsm
, 0);
266 otg_chrg_vbus(fsm
, 0);
267 otg_loc_conn(fsm
, 0);
269 otg_start_adp_prb(fsm
);
270 otg_set_protocol(fsm
, PROTO_HOST
);
272 case OTG_STATE_A_WAIT_VRISE
:
273 otg_drv_vbus(fsm
, 1);
274 otg_loc_conn(fsm
, 0);
276 otg_set_protocol(fsm
, PROTO_HOST
);
277 otg_add_timer(fsm
, A_WAIT_VRISE
);
279 case OTG_STATE_A_WAIT_BCON
:
280 otg_drv_vbus(fsm
, 1);
281 otg_loc_conn(fsm
, 0);
283 otg_set_protocol(fsm
, PROTO_HOST
);
284 otg_add_timer(fsm
, A_WAIT_BCON
);
286 case OTG_STATE_A_HOST
:
287 otg_drv_vbus(fsm
, 1);
288 otg_loc_conn(fsm
, 0);
290 otg_set_protocol(fsm
, PROTO_HOST
);
292 * When HNP is triggered while a_bus_req = 0, a_host will
293 * suspend too fast to complete a_set_b_hnp_en
295 if (!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
)
296 otg_add_timer(fsm
, A_WAIT_ENUM
);
297 otg_start_hnp_polling(fsm
);
299 case OTG_STATE_A_SUSPEND
:
300 otg_drv_vbus(fsm
, 1);
301 otg_loc_conn(fsm
, 0);
303 otg_set_protocol(fsm
, PROTO_HOST
);
304 otg_add_timer(fsm
, A_AIDL_BDIS
);
307 case OTG_STATE_A_PERIPHERAL
:
309 otg_set_protocol(fsm
, PROTO_GADGET
);
310 otg_drv_vbus(fsm
, 1);
311 otg_loc_conn(fsm
, 1);
312 otg_add_timer(fsm
, A_BIDL_ADIS
);
314 case OTG_STATE_A_WAIT_VFALL
:
315 otg_drv_vbus(fsm
, 0);
316 otg_loc_conn(fsm
, 0);
318 otg_set_protocol(fsm
, PROTO_HOST
);
319 otg_add_timer(fsm
, A_WAIT_VFALL
);
321 case OTG_STATE_A_VBUS_ERR
:
322 otg_drv_vbus(fsm
, 0);
323 otg_loc_conn(fsm
, 0);
325 otg_set_protocol(fsm
, PROTO_UNDEF
);
331 fsm
->otg
->state
= new_state
;
332 fsm
->state_changed
= 1;
336 /* State change judgement */
337 int otg_statemachine(struct otg_fsm
*fsm
)
339 enum usb_otg_state state
;
341 mutex_lock(&fsm
->lock
);
343 state
= fsm
->otg
->state
;
344 fsm
->state_changed
= 0;
345 /* State machine state change judgement */
348 case OTG_STATE_UNDEFINED
:
349 VDBG("fsm->id = %d\n", fsm
->id
);
351 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
353 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
355 case OTG_STATE_B_IDLE
:
357 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
358 else if (fsm
->b_sess_vld
&& fsm
->otg
->gadget
)
359 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
360 else if ((fsm
->b_bus_req
|| fsm
->adp_change
|| fsm
->power_up
) &&
361 fsm
->b_ssend_srp
&& fsm
->b_se0_srp
)
362 otg_set_state(fsm
, OTG_STATE_B_SRP_INIT
);
364 case OTG_STATE_B_SRP_INIT
:
365 if (!fsm
->id
|| fsm
->b_srp_done
)
366 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
368 case OTG_STATE_B_PERIPHERAL
:
369 if (!fsm
->id
|| !fsm
->b_sess_vld
)
370 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
371 else if (fsm
->b_bus_req
&& fsm
->otg
->
372 gadget
->b_hnp_enable
&& fsm
->a_bus_suspend
)
373 otg_set_state(fsm
, OTG_STATE_B_WAIT_ACON
);
375 case OTG_STATE_B_WAIT_ACON
:
377 otg_set_state(fsm
, OTG_STATE_B_HOST
);
378 else if (!fsm
->id
|| !fsm
->b_sess_vld
)
379 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
380 else if (fsm
->a_bus_resume
|| fsm
->b_ase0_brst_tmout
) {
381 fsm
->b_ase0_brst_tmout
= 0;
382 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
385 case OTG_STATE_B_HOST
:
386 if (!fsm
->id
|| !fsm
->b_sess_vld
)
387 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
388 else if (!fsm
->b_bus_req
|| !fsm
->a_conn
|| fsm
->test_device
)
389 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
391 case OTG_STATE_A_IDLE
:
393 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
394 else if (!fsm
->a_bus_drop
&& (fsm
->a_bus_req
||
395 fsm
->a_srp_det
|| fsm
->adp_change
|| fsm
->power_up
))
396 otg_set_state(fsm
, OTG_STATE_A_WAIT_VRISE
);
398 case OTG_STATE_A_WAIT_VRISE
:
400 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
401 else if (fsm
->id
|| fsm
->a_bus_drop
||
402 fsm
->a_wait_vrise_tmout
)
403 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
405 case OTG_STATE_A_WAIT_BCON
:
406 if (!fsm
->a_vbus_vld
)
407 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
408 else if (fsm
->b_conn
)
409 otg_set_state(fsm
, OTG_STATE_A_HOST
);
410 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_wait_bcon_tmout
)
411 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
413 case OTG_STATE_A_HOST
:
414 if (fsm
->id
|| fsm
->a_bus_drop
)
415 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
416 else if ((!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
) &&
417 fsm
->otg
->host
->b_hnp_enable
)
418 otg_set_state(fsm
, OTG_STATE_A_SUSPEND
);
419 else if (!fsm
->b_conn
)
420 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
421 else if (!fsm
->a_vbus_vld
)
422 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
424 case OTG_STATE_A_SUSPEND
:
425 if (!fsm
->b_conn
&& fsm
->otg
->host
->b_hnp_enable
)
426 otg_set_state(fsm
, OTG_STATE_A_PERIPHERAL
);
427 else if (!fsm
->b_conn
&& !fsm
->otg
->host
->b_hnp_enable
)
428 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
429 else if (fsm
->a_bus_req
|| fsm
->b_bus_resume
)
430 otg_set_state(fsm
, OTG_STATE_A_HOST
);
431 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_aidl_bdis_tmout
)
432 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
433 else if (!fsm
->a_vbus_vld
)
434 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
436 case OTG_STATE_A_PERIPHERAL
:
437 if (fsm
->id
|| fsm
->a_bus_drop
)
438 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
439 else if (fsm
->a_bidl_adis_tmout
|| fsm
->b_bus_suspend
)
440 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
441 else if (!fsm
->a_vbus_vld
)
442 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
444 case OTG_STATE_A_WAIT_VFALL
:
445 if (fsm
->a_wait_vfall_tmout
)
446 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
448 case OTG_STATE_A_VBUS_ERR
:
449 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_clr_err
)
450 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
455 mutex_unlock(&fsm
->lock
);
457 VDBG("quit statemachine, changed = %d\n", fsm
->state_changed
);
458 return fsm
->state_changed
;
460 EXPORT_SYMBOL_GPL(otg_statemachine
);
461 MODULE_LICENSE("GPL");