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/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 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;
111 case OTG_STATE_A_WAIT_VFALL
:
112 otg_del_timer(fsm
, A_WAIT_VFALL
);
113 fsm
->a_wait_vfall_tmout
= 0;
114 otg_del_timer(fsm
, A_WAIT_VRISE
);
116 case OTG_STATE_A_VBUS_ERR
:
123 /* Called when entering a state */
124 static int otg_set_state(struct otg_fsm
*fsm
, enum usb_otg_state new_state
)
127 if (fsm
->otg
->phy
->state
== new_state
)
129 VDBG("Set state: %s\n", usb_otg_state_string(new_state
));
130 otg_leave_state(fsm
, fsm
->otg
->phy
->state
);
132 case OTG_STATE_B_IDLE
:
133 otg_drv_vbus(fsm
, 0);
134 otg_chrg_vbus(fsm
, 0);
135 otg_loc_conn(fsm
, 0);
138 * Driver is responsible for starting ADP probing
139 * if ADP sensing times out.
141 otg_start_adp_sns(fsm
);
142 otg_set_protocol(fsm
, PROTO_UNDEF
);
143 otg_add_timer(fsm
, B_SE0_SRP
);
145 case OTG_STATE_B_SRP_INIT
:
146 otg_start_pulse(fsm
);
148 otg_set_protocol(fsm
, PROTO_UNDEF
);
149 otg_add_timer(fsm
, B_SRP_FAIL
);
151 case OTG_STATE_B_PERIPHERAL
:
152 otg_chrg_vbus(fsm
, 0);
153 otg_loc_conn(fsm
, 1);
155 otg_set_protocol(fsm
, PROTO_GADGET
);
157 case OTG_STATE_B_WAIT_ACON
:
158 otg_chrg_vbus(fsm
, 0);
159 otg_loc_conn(fsm
, 0);
161 otg_set_protocol(fsm
, PROTO_HOST
);
162 otg_add_timer(fsm
, B_ASE0_BRST
);
163 fsm
->a_bus_suspend
= 0;
165 case OTG_STATE_B_HOST
:
166 otg_chrg_vbus(fsm
, 0);
167 otg_loc_conn(fsm
, 0);
169 otg_set_protocol(fsm
, PROTO_HOST
);
170 usb_bus_start_enum(fsm
->otg
->host
,
171 fsm
->otg
->host
->otg_port
);
173 case OTG_STATE_A_IDLE
:
174 otg_drv_vbus(fsm
, 0);
175 otg_chrg_vbus(fsm
, 0);
176 otg_loc_conn(fsm
, 0);
178 otg_start_adp_prb(fsm
);
179 otg_set_protocol(fsm
, PROTO_HOST
);
181 case OTG_STATE_A_WAIT_VRISE
:
182 otg_drv_vbus(fsm
, 1);
183 otg_loc_conn(fsm
, 0);
185 otg_set_protocol(fsm
, PROTO_HOST
);
186 otg_add_timer(fsm
, A_WAIT_VRISE
);
188 case OTG_STATE_A_WAIT_BCON
:
189 otg_drv_vbus(fsm
, 1);
190 otg_loc_conn(fsm
, 0);
192 otg_set_protocol(fsm
, PROTO_HOST
);
193 otg_add_timer(fsm
, A_WAIT_BCON
);
195 case OTG_STATE_A_HOST
:
196 otg_drv_vbus(fsm
, 1);
197 otg_loc_conn(fsm
, 0);
199 otg_set_protocol(fsm
, PROTO_HOST
);
201 * When HNP is triggered while a_bus_req = 0, a_host will
202 * suspend too fast to complete a_set_b_hnp_en
204 if (!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
)
205 otg_add_timer(fsm
, A_WAIT_ENUM
);
207 case OTG_STATE_A_SUSPEND
:
208 otg_drv_vbus(fsm
, 1);
209 otg_loc_conn(fsm
, 0);
211 otg_set_protocol(fsm
, PROTO_HOST
);
212 otg_add_timer(fsm
, A_AIDL_BDIS
);
215 case OTG_STATE_A_PERIPHERAL
:
216 otg_loc_conn(fsm
, 1);
218 otg_set_protocol(fsm
, PROTO_GADGET
);
219 otg_drv_vbus(fsm
, 1);
220 otg_add_timer(fsm
, A_BIDL_ADIS
);
222 case OTG_STATE_A_WAIT_VFALL
:
223 otg_drv_vbus(fsm
, 0);
224 otg_loc_conn(fsm
, 0);
226 otg_set_protocol(fsm
, PROTO_HOST
);
227 otg_add_timer(fsm
, A_WAIT_VFALL
);
229 case OTG_STATE_A_VBUS_ERR
:
230 otg_drv_vbus(fsm
, 0);
231 otg_loc_conn(fsm
, 0);
233 otg_set_protocol(fsm
, PROTO_UNDEF
);
239 fsm
->otg
->phy
->state
= new_state
;
243 /* State change judgement */
244 int otg_statemachine(struct otg_fsm
*fsm
)
246 enum usb_otg_state state
;
248 mutex_lock(&fsm
->lock
);
250 state
= fsm
->otg
->phy
->state
;
252 /* State machine state change judgement */
255 case OTG_STATE_UNDEFINED
:
256 VDBG("fsm->id = %d\n", fsm
->id
);
258 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
260 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
262 case OTG_STATE_B_IDLE
:
264 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
265 else if (fsm
->b_sess_vld
&& fsm
->otg
->gadget
)
266 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
267 else if ((fsm
->b_bus_req
|| fsm
->adp_change
|| fsm
->power_up
) &&
268 fsm
->b_ssend_srp
&& fsm
->b_se0_srp
)
269 otg_set_state(fsm
, OTG_STATE_B_SRP_INIT
);
271 case OTG_STATE_B_SRP_INIT
:
272 if (!fsm
->id
|| fsm
->b_srp_done
)
273 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
275 case OTG_STATE_B_PERIPHERAL
:
276 if (!fsm
->id
|| !fsm
->b_sess_vld
)
277 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
278 else if (fsm
->b_bus_req
&& fsm
->otg
->
279 gadget
->b_hnp_enable
&& fsm
->a_bus_suspend
)
280 otg_set_state(fsm
, OTG_STATE_B_WAIT_ACON
);
282 case OTG_STATE_B_WAIT_ACON
:
284 otg_set_state(fsm
, OTG_STATE_B_HOST
);
285 else if (!fsm
->id
|| !fsm
->b_sess_vld
)
286 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
287 else if (fsm
->a_bus_resume
|| fsm
->b_ase0_brst_tmout
) {
288 fsm
->b_ase0_brst_tmout
= 0;
289 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
292 case OTG_STATE_B_HOST
:
293 if (!fsm
->id
|| !fsm
->b_sess_vld
)
294 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
295 else if (!fsm
->b_bus_req
|| !fsm
->a_conn
|| fsm
->test_device
)
296 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
298 case OTG_STATE_A_IDLE
:
300 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
301 else if (!fsm
->a_bus_drop
&& (fsm
->a_bus_req
||
302 fsm
->a_srp_det
|| fsm
->adp_change
|| fsm
->power_up
))
303 otg_set_state(fsm
, OTG_STATE_A_WAIT_VRISE
);
305 case OTG_STATE_A_WAIT_VRISE
:
306 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_vbus_vld
||
307 fsm
->a_wait_vrise_tmout
) {
308 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
311 case OTG_STATE_A_WAIT_BCON
:
312 if (!fsm
->a_vbus_vld
)
313 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
314 else if (fsm
->b_conn
)
315 otg_set_state(fsm
, OTG_STATE_A_HOST
);
316 else if (fsm
->id
| fsm
->a_bus_drop
| fsm
->a_wait_bcon_tmout
)
317 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
319 case OTG_STATE_A_HOST
:
320 if ((!fsm
->a_bus_req
|| fsm
->a_suspend_req_inf
) &&
321 fsm
->otg
->host
->b_hnp_enable
)
322 otg_set_state(fsm
, OTG_STATE_A_SUSPEND
);
323 else if (fsm
->id
|| !fsm
->b_conn
|| fsm
->a_bus_drop
)
324 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
325 else if (!fsm
->a_vbus_vld
)
326 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
328 case OTG_STATE_A_SUSPEND
:
329 if (!fsm
->b_conn
&& fsm
->otg
->host
->b_hnp_enable
)
330 otg_set_state(fsm
, OTG_STATE_A_PERIPHERAL
);
331 else if (!fsm
->b_conn
&& !fsm
->otg
->host
->b_hnp_enable
)
332 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
333 else if (fsm
->a_bus_req
|| fsm
->b_bus_resume
)
334 otg_set_state(fsm
, OTG_STATE_A_HOST
);
335 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_aidl_bdis_tmout
)
336 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
337 else if (!fsm
->a_vbus_vld
)
338 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
340 case OTG_STATE_A_PERIPHERAL
:
341 if (fsm
->id
|| fsm
->a_bus_drop
)
342 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
343 else if (fsm
->a_bidl_adis_tmout
|| fsm
->b_bus_suspend
)
344 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
345 else if (!fsm
->a_vbus_vld
)
346 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
348 case OTG_STATE_A_WAIT_VFALL
:
349 if (fsm
->a_wait_vfall_tmout
|| fsm
->id
|| fsm
->a_bus_req
||
350 (!fsm
->a_sess_vld
&& !fsm
->b_conn
))
351 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
353 case OTG_STATE_A_VBUS_ERR
:
354 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_clr_err
)
355 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
360 mutex_unlock(&fsm
->lock
);
362 VDBG("quit statemachine, changed = %d\n", state_changed
);
363 return state_changed
;