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>
34 /* Change USB protocol when there is a protocol change */
35 static int otg_set_protocol(struct otg_fsm
*fsm
, int protocol
)
39 if (fsm
->protocol
!= protocol
) {
40 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
41 fsm
->protocol
, protocol
);
42 /* stop old protocol */
43 if (fsm
->protocol
== PROTO_HOST
)
44 ret
= otg_start_host(fsm
, 0);
45 else if (fsm
->protocol
== PROTO_GADGET
)
46 ret
= otg_start_gadget(fsm
, 0);
50 /* start new protocol */
51 if (protocol
== PROTO_HOST
)
52 ret
= otg_start_host(fsm
, 1);
53 else if (protocol
== PROTO_GADGET
)
54 ret
= otg_start_gadget(fsm
, 1);
58 fsm
->protocol
= protocol
;
65 static int state_changed
;
67 /* Called when leaving a state. Do state clean up jobs here */
68 static void otg_leave_state(struct otg_fsm
*fsm
, enum usb_otg_state old_state
)
71 case OTG_STATE_B_IDLE
:
72 otg_del_timer(fsm
, B_SE0_SRP
);
77 case OTG_STATE_B_SRP_INIT
:
81 case OTG_STATE_B_PERIPHERAL
:
83 case OTG_STATE_B_WAIT_ACON
:
84 otg_del_timer(fsm
, B_ASE0_BRST
);
85 fsm
->b_ase0_brst_tmout
= 0;
87 case OTG_STATE_B_HOST
:
89 case OTG_STATE_A_IDLE
:
92 case OTG_STATE_A_WAIT_VRISE
:
93 otg_del_timer(fsm
, A_WAIT_VRISE
);
94 fsm
->a_wait_vrise_tmout
= 0;
96 case OTG_STATE_A_WAIT_BCON
:
97 otg_del_timer(fsm
, A_WAIT_BCON
);
98 fsm
->a_wait_bcon_tmout
= 0;
100 case OTG_STATE_A_HOST
:
101 otg_del_timer(fsm
, A_WAIT_ENUM
);
103 case OTG_STATE_A_SUSPEND
:
104 otg_del_timer(fsm
, A_AIDL_BDIS
);
105 fsm
->a_aidl_bdis_tmout
= 0;
106 fsm
->a_suspend_req_inf
= 0;
108 case OTG_STATE_A_PERIPHERAL
:
109 otg_del_timer(fsm
, A_BIDL_ADIS
);
110 fsm
->a_bidl_adis_tmout
= 0;
112 case OTG_STATE_A_WAIT_VFALL
:
113 otg_del_timer(fsm
, A_WAIT_VFALL
);
114 fsm
->a_wait_vfall_tmout
= 0;
115 otg_del_timer(fsm
, A_WAIT_VRISE
);
117 case OTG_STATE_A_VBUS_ERR
:
124 /* Called when entering a state */
125 static int otg_set_state(struct otg_fsm
*fsm
, enum usb_otg_state new_state
)
128 if (fsm
->otg
->state
== new_state
)
130 VDBG("Set state: %s\n", usb_otg_state_string(new_state
));
131 otg_leave_state(fsm
, fsm
->otg
->state
);
133 case OTG_STATE_B_IDLE
:
134 otg_drv_vbus(fsm
, 0);
135 otg_chrg_vbus(fsm
, 0);
136 otg_loc_conn(fsm
, 0);
139 * Driver is responsible for starting ADP probing
140 * if ADP sensing times out.
142 otg_start_adp_sns(fsm
);
143 otg_set_protocol(fsm
, PROTO_UNDEF
);
144 otg_add_timer(fsm
, B_SE0_SRP
);
146 case OTG_STATE_B_SRP_INIT
:
147 otg_start_pulse(fsm
);
149 otg_set_protocol(fsm
, PROTO_UNDEF
);
150 otg_add_timer(fsm
, B_SRP_FAIL
);
152 case OTG_STATE_B_PERIPHERAL
:
153 otg_chrg_vbus(fsm
, 0);
155 otg_set_protocol(fsm
, PROTO_GADGET
);
156 otg_loc_conn(fsm
, 1);
158 case OTG_STATE_B_WAIT_ACON
:
159 otg_chrg_vbus(fsm
, 0);
160 otg_loc_conn(fsm
, 0);
162 otg_set_protocol(fsm
, PROTO_HOST
);
163 otg_add_timer(fsm
, B_ASE0_BRST
);
164 fsm
->a_bus_suspend
= 0;
166 case OTG_STATE_B_HOST
:
167 otg_chrg_vbus(fsm
, 0);
168 otg_loc_conn(fsm
, 0);
170 otg_set_protocol(fsm
, PROTO_HOST
);
171 usb_bus_start_enum(fsm
->otg
->host
,
172 fsm
->otg
->host
->otg_port
);
174 case OTG_STATE_A_IDLE
:
175 otg_drv_vbus(fsm
, 0);
176 otg_chrg_vbus(fsm
, 0);
177 otg_loc_conn(fsm
, 0);
179 otg_start_adp_prb(fsm
);
180 otg_set_protocol(fsm
, PROTO_HOST
);
182 case OTG_STATE_A_WAIT_VRISE
:
183 otg_drv_vbus(fsm
, 1);
184 otg_loc_conn(fsm
, 0);
186 otg_set_protocol(fsm
, PROTO_HOST
);
187 otg_add_timer(fsm
, A_WAIT_VRISE
);
189 case OTG_STATE_A_WAIT_BCON
:
190 otg_drv_vbus(fsm
, 1);
191 otg_loc_conn(fsm
, 0);
193 otg_set_protocol(fsm
, PROTO_HOST
);
194 otg_add_timer(fsm
, A_WAIT_BCON
);
196 case OTG_STATE_A_HOST
:
197 otg_drv_vbus(fsm
, 1);
198 otg_loc_conn(fsm
, 0);
200 otg_set_protocol(fsm
, PROTO_HOST
);
202 * When HNP is triggered while a_bus_req = 0, a_host will
203 * suspend too fast to complete a_set_b_hnp_en
205 if (!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
)
206 otg_add_timer(fsm
, A_WAIT_ENUM
);
208 case OTG_STATE_A_SUSPEND
:
209 otg_drv_vbus(fsm
, 1);
210 otg_loc_conn(fsm
, 0);
212 otg_set_protocol(fsm
, PROTO_HOST
);
213 otg_add_timer(fsm
, A_AIDL_BDIS
);
216 case OTG_STATE_A_PERIPHERAL
:
218 otg_set_protocol(fsm
, PROTO_GADGET
);
219 otg_drv_vbus(fsm
, 1);
220 otg_loc_conn(fsm
, 1);
221 otg_add_timer(fsm
, A_BIDL_ADIS
);
223 case OTG_STATE_A_WAIT_VFALL
:
224 otg_drv_vbus(fsm
, 0);
225 otg_loc_conn(fsm
, 0);
227 otg_set_protocol(fsm
, PROTO_HOST
);
228 otg_add_timer(fsm
, A_WAIT_VFALL
);
230 case OTG_STATE_A_VBUS_ERR
:
231 otg_drv_vbus(fsm
, 0);
232 otg_loc_conn(fsm
, 0);
234 otg_set_protocol(fsm
, PROTO_UNDEF
);
240 fsm
->otg
->state
= new_state
;
244 /* State change judgement */
245 int otg_statemachine(struct otg_fsm
*fsm
)
247 enum usb_otg_state state
;
249 mutex_lock(&fsm
->lock
);
251 state
= fsm
->otg
->state
;
253 /* State machine state change judgement */
256 case OTG_STATE_UNDEFINED
:
257 VDBG("fsm->id = %d\n", fsm
->id
);
259 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
261 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
263 case OTG_STATE_B_IDLE
:
265 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
266 else if (fsm
->b_sess_vld
&& fsm
->otg
->gadget
)
267 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
268 else if ((fsm
->b_bus_req
|| fsm
->adp_change
|| fsm
->power_up
) &&
269 fsm
->b_ssend_srp
&& fsm
->b_se0_srp
)
270 otg_set_state(fsm
, OTG_STATE_B_SRP_INIT
);
272 case OTG_STATE_B_SRP_INIT
:
273 if (!fsm
->id
|| fsm
->b_srp_done
)
274 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
276 case OTG_STATE_B_PERIPHERAL
:
277 if (!fsm
->id
|| !fsm
->b_sess_vld
)
278 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
279 else if (fsm
->b_bus_req
&& fsm
->otg
->
280 gadget
->b_hnp_enable
&& fsm
->a_bus_suspend
)
281 otg_set_state(fsm
, OTG_STATE_B_WAIT_ACON
);
283 case OTG_STATE_B_WAIT_ACON
:
285 otg_set_state(fsm
, OTG_STATE_B_HOST
);
286 else if (!fsm
->id
|| !fsm
->b_sess_vld
)
287 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
288 else if (fsm
->a_bus_resume
|| fsm
->b_ase0_brst_tmout
) {
289 fsm
->b_ase0_brst_tmout
= 0;
290 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
293 case OTG_STATE_B_HOST
:
294 if (!fsm
->id
|| !fsm
->b_sess_vld
)
295 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
296 else if (!fsm
->b_bus_req
|| !fsm
->a_conn
|| fsm
->test_device
)
297 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
299 case OTG_STATE_A_IDLE
:
301 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
302 else if (!fsm
->a_bus_drop
&& (fsm
->a_bus_req
||
303 fsm
->a_srp_det
|| fsm
->adp_change
|| fsm
->power_up
))
304 otg_set_state(fsm
, OTG_STATE_A_WAIT_VRISE
);
306 case OTG_STATE_A_WAIT_VRISE
:
308 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
309 else if (fsm
->id
|| fsm
->a_bus_drop
||
310 fsm
->a_wait_vrise_tmout
)
311 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
313 case OTG_STATE_A_WAIT_BCON
:
314 if (!fsm
->a_vbus_vld
)
315 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
316 else if (fsm
->b_conn
)
317 otg_set_state(fsm
, OTG_STATE_A_HOST
);
318 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_wait_bcon_tmout
)
319 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
321 case OTG_STATE_A_HOST
:
322 if (fsm
->id
|| fsm
->a_bus_drop
)
323 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
324 else if ((!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
) &&
325 fsm
->otg
->host
->b_hnp_enable
)
326 otg_set_state(fsm
, OTG_STATE_A_SUSPEND
);
327 else if (!fsm
->b_conn
)
328 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
329 else if (!fsm
->a_vbus_vld
)
330 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
332 case OTG_STATE_A_SUSPEND
:
333 if (!fsm
->b_conn
&& fsm
->otg
->host
->b_hnp_enable
)
334 otg_set_state(fsm
, OTG_STATE_A_PERIPHERAL
);
335 else if (!fsm
->b_conn
&& !fsm
->otg
->host
->b_hnp_enable
)
336 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
337 else if (fsm
->a_bus_req
|| fsm
->b_bus_resume
)
338 otg_set_state(fsm
, OTG_STATE_A_HOST
);
339 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_aidl_bdis_tmout
)
340 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
341 else if (!fsm
->a_vbus_vld
)
342 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
344 case OTG_STATE_A_PERIPHERAL
:
345 if (fsm
->id
|| fsm
->a_bus_drop
)
346 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
347 else if (fsm
->a_bidl_adis_tmout
|| fsm
->b_bus_suspend
)
348 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
349 else if (!fsm
->a_vbus_vld
)
350 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
352 case OTG_STATE_A_WAIT_VFALL
:
353 if (fsm
->a_wait_vfall_tmout
)
354 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
356 case OTG_STATE_A_VBUS_ERR
:
357 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_clr_err
)
358 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
363 mutex_unlock(&fsm
->lock
);
365 VDBG("quit statemachine, changed = %d\n", state_changed
);
366 return state_changed
;
368 EXPORT_SYMBOL_GPL(otg_statemachine
);
369 MODULE_LICENSE("GPL");