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/spinlock.h>
27 #include <linux/delay.h>
28 #include <linux/usb.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/otg.h>
32 #include "phy-fsm-usb.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
= fsm
->ops
->start_host(fsm
, 0);
45 else if (fsm
->protocol
== PROTO_GADGET
)
46 ret
= fsm
->ops
->start_gadget(fsm
, 0);
50 /* start new protocol */
51 if (protocol
== PROTO_HOST
)
52 ret
= fsm
->ops
->start_host(fsm
, 1);
53 else if (protocol
== PROTO_GADGET
)
54 ret
= fsm
->ops
->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 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_tmr
);
75 case OTG_STATE_B_SRP_INIT
:
78 case OTG_STATE_B_PERIPHERAL
:
80 case OTG_STATE_B_WAIT_ACON
:
81 otg_del_timer(fsm
, b_ase0_brst_tmr
);
82 fsm
->b_ase0_brst_tmout
= 0;
84 case OTG_STATE_B_HOST
:
86 case OTG_STATE_A_IDLE
:
88 case OTG_STATE_A_WAIT_VRISE
:
89 otg_del_timer(fsm
, a_wait_vrise_tmr
);
90 fsm
->a_wait_vrise_tmout
= 0;
92 case OTG_STATE_A_WAIT_BCON
:
93 otg_del_timer(fsm
, a_wait_bcon_tmr
);
94 fsm
->a_wait_bcon_tmout
= 0;
96 case OTG_STATE_A_HOST
:
97 otg_del_timer(fsm
, a_wait_enum_tmr
);
99 case OTG_STATE_A_SUSPEND
:
100 otg_del_timer(fsm
, a_aidl_bdis_tmr
);
101 fsm
->a_aidl_bdis_tmout
= 0;
102 fsm
->a_suspend_req
= 0;
104 case OTG_STATE_A_PERIPHERAL
:
106 case OTG_STATE_A_WAIT_VFALL
:
107 otg_del_timer(fsm
, a_wait_vrise_tmr
);
109 case OTG_STATE_A_VBUS_ERR
:
116 /* Called when entering a state */
117 int otg_set_state(struct otg_fsm
*fsm
, enum usb_otg_state new_state
)
120 if (fsm
->otg
->phy
->state
== new_state
)
122 VDBG("Set state: %s\n", usb_otg_state_string(new_state
));
123 otg_leave_state(fsm
, fsm
->otg
->phy
->state
);
125 case OTG_STATE_B_IDLE
:
126 otg_drv_vbus(fsm
, 0);
127 otg_chrg_vbus(fsm
, 0);
128 otg_loc_conn(fsm
, 0);
130 otg_set_protocol(fsm
, PROTO_UNDEF
);
131 otg_add_timer(fsm
, b_se0_srp_tmr
);
133 case OTG_STATE_B_SRP_INIT
:
134 otg_start_pulse(fsm
);
136 otg_set_protocol(fsm
, PROTO_UNDEF
);
137 otg_add_timer(fsm
, b_srp_fail_tmr
);
139 case OTG_STATE_B_PERIPHERAL
:
140 otg_chrg_vbus(fsm
, 0);
141 otg_loc_conn(fsm
, 1);
143 otg_set_protocol(fsm
, PROTO_GADGET
);
145 case OTG_STATE_B_WAIT_ACON
:
146 otg_chrg_vbus(fsm
, 0);
147 otg_loc_conn(fsm
, 0);
149 otg_set_protocol(fsm
, PROTO_HOST
);
150 otg_add_timer(fsm
, b_ase0_brst_tmr
);
151 fsm
->a_bus_suspend
= 0;
153 case OTG_STATE_B_HOST
:
154 otg_chrg_vbus(fsm
, 0);
155 otg_loc_conn(fsm
, 0);
157 otg_set_protocol(fsm
, PROTO_HOST
);
158 usb_bus_start_enum(fsm
->otg
->host
,
159 fsm
->otg
->host
->otg_port
);
161 case OTG_STATE_A_IDLE
:
162 otg_drv_vbus(fsm
, 0);
163 otg_chrg_vbus(fsm
, 0);
164 otg_loc_conn(fsm
, 0);
166 otg_set_protocol(fsm
, PROTO_HOST
);
168 case OTG_STATE_A_WAIT_VRISE
:
169 otg_drv_vbus(fsm
, 1);
170 otg_loc_conn(fsm
, 0);
172 otg_set_protocol(fsm
, PROTO_HOST
);
173 otg_add_timer(fsm
, a_wait_vrise_tmr
);
175 case OTG_STATE_A_WAIT_BCON
:
176 otg_drv_vbus(fsm
, 1);
177 otg_loc_conn(fsm
, 0);
179 otg_set_protocol(fsm
, PROTO_HOST
);
180 otg_add_timer(fsm
, a_wait_bcon_tmr
);
182 case OTG_STATE_A_HOST
:
183 otg_drv_vbus(fsm
, 1);
184 otg_loc_conn(fsm
, 0);
186 otg_set_protocol(fsm
, PROTO_HOST
);
188 * When HNP is triggered while a_bus_req = 0, a_host will
189 * suspend too fast to complete a_set_b_hnp_en
191 if (!fsm
->a_bus_req
|| fsm
->a_suspend_req
)
192 otg_add_timer(fsm
, a_wait_enum_tmr
);
194 case OTG_STATE_A_SUSPEND
:
195 otg_drv_vbus(fsm
, 1);
196 otg_loc_conn(fsm
, 0);
198 otg_set_protocol(fsm
, PROTO_HOST
);
199 otg_add_timer(fsm
, a_aidl_bdis_tmr
);
202 case OTG_STATE_A_PERIPHERAL
:
203 otg_loc_conn(fsm
, 1);
205 otg_set_protocol(fsm
, PROTO_GADGET
);
206 otg_drv_vbus(fsm
, 1);
208 case OTG_STATE_A_WAIT_VFALL
:
209 otg_drv_vbus(fsm
, 0);
210 otg_loc_conn(fsm
, 0);
212 otg_set_protocol(fsm
, PROTO_HOST
);
214 case OTG_STATE_A_VBUS_ERR
:
215 otg_drv_vbus(fsm
, 0);
216 otg_loc_conn(fsm
, 0);
218 otg_set_protocol(fsm
, PROTO_UNDEF
);
224 fsm
->otg
->phy
->state
= new_state
;
228 /* State change judgement */
229 int otg_statemachine(struct otg_fsm
*fsm
)
231 enum usb_otg_state state
;
234 spin_lock_irqsave(&fsm
->lock
, flags
);
236 state
= fsm
->otg
->phy
->state
;
238 /* State machine state change judgement */
241 case OTG_STATE_UNDEFINED
:
242 VDBG("fsm->id = %d\n", fsm
->id
);
244 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
246 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
248 case OTG_STATE_B_IDLE
:
250 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
251 else if (fsm
->b_sess_vld
&& fsm
->otg
->gadget
)
252 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
253 else if (fsm
->b_bus_req
&& fsm
->b_sess_end
&& fsm
->b_se0_srp
)
254 otg_set_state(fsm
, OTG_STATE_B_SRP_INIT
);
256 case OTG_STATE_B_SRP_INIT
:
257 if (!fsm
->id
|| fsm
->b_srp_done
)
258 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
260 case OTG_STATE_B_PERIPHERAL
:
261 if (!fsm
->id
|| !fsm
->b_sess_vld
)
262 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
263 else if (fsm
->b_bus_req
&& fsm
->otg
->
264 gadget
->b_hnp_enable
&& fsm
->a_bus_suspend
)
265 otg_set_state(fsm
, OTG_STATE_B_WAIT_ACON
);
267 case OTG_STATE_B_WAIT_ACON
:
269 otg_set_state(fsm
, OTG_STATE_B_HOST
);
270 else if (!fsm
->id
|| !fsm
->b_sess_vld
)
271 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
272 else if (fsm
->a_bus_resume
|| fsm
->b_ase0_brst_tmout
) {
273 fsm
->b_ase0_brst_tmout
= 0;
274 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
277 case OTG_STATE_B_HOST
:
278 if (!fsm
->id
|| !fsm
->b_sess_vld
)
279 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
280 else if (!fsm
->b_bus_req
|| !fsm
->a_conn
)
281 otg_set_state(fsm
, OTG_STATE_B_PERIPHERAL
);
283 case OTG_STATE_A_IDLE
:
285 otg_set_state(fsm
, OTG_STATE_B_IDLE
);
286 else if (!fsm
->a_bus_drop
&& (fsm
->a_bus_req
|| fsm
->a_srp_det
))
287 otg_set_state(fsm
, OTG_STATE_A_WAIT_VRISE
);
289 case OTG_STATE_A_WAIT_VRISE
:
290 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_vbus_vld
||
291 fsm
->a_wait_vrise_tmout
) {
292 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
295 case OTG_STATE_A_WAIT_BCON
:
296 if (!fsm
->a_vbus_vld
)
297 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
298 else if (fsm
->b_conn
)
299 otg_set_state(fsm
, OTG_STATE_A_HOST
);
300 else if (fsm
->id
| fsm
->a_bus_drop
| fsm
->a_wait_bcon_tmout
)
301 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
303 case OTG_STATE_A_HOST
:
304 if ((!fsm
->a_bus_req
|| fsm
->a_suspend_req
) &&
305 fsm
->otg
->host
->b_hnp_enable
)
306 otg_set_state(fsm
, OTG_STATE_A_SUSPEND
);
307 else if (fsm
->id
|| !fsm
->b_conn
|| fsm
->a_bus_drop
)
308 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
309 else if (!fsm
->a_vbus_vld
)
310 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
312 case OTG_STATE_A_SUSPEND
:
313 if (!fsm
->b_conn
&& fsm
->otg
->host
->b_hnp_enable
)
314 otg_set_state(fsm
, OTG_STATE_A_PERIPHERAL
);
315 else if (!fsm
->b_conn
&& !fsm
->otg
->host
->b_hnp_enable
)
316 otg_set_state(fsm
, OTG_STATE_A_WAIT_BCON
);
317 else if (fsm
->a_bus_req
|| fsm
->b_bus_resume
)
318 otg_set_state(fsm
, OTG_STATE_A_HOST
);
319 else if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_aidl_bdis_tmout
)
320 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
321 else if (!fsm
->a_vbus_vld
)
322 otg_set_state(fsm
, OTG_STATE_A_VBUS_ERR
);
324 case OTG_STATE_A_PERIPHERAL
:
325 if (fsm
->id
|| fsm
->a_bus_drop
)
326 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
327 else if (fsm
->b_bus_suspend
)
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_WAIT_VFALL
:
333 if (fsm
->id
|| fsm
->a_bus_req
|| (!fsm
->a_sess_vld
&&
335 otg_set_state(fsm
, OTG_STATE_A_IDLE
);
337 case OTG_STATE_A_VBUS_ERR
:
338 if (fsm
->id
|| fsm
->a_bus_drop
|| fsm
->a_clr_err
)
339 otg_set_state(fsm
, OTG_STATE_A_WAIT_VFALL
);
344 spin_unlock_irqrestore(&fsm
->lock
, flags
);
346 VDBG("quit statemachine, changed = %d\n", state_changed
);
347 return state_changed
;