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 = 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 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);
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);
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);
90 fsm->a_wait_vrise_tmout = 0;
92 case OTG_STATE_A_WAIT_BCON:
93 otg_del_timer(fsm, A_WAIT_BCON);
94 fsm->a_wait_bcon_tmout = 0;
96 case OTG_STATE_A_HOST:
97 otg_del_timer(fsm, A_WAIT_ENUM);
99 case OTG_STATE_A_SUSPEND:
100 otg_del_timer(fsm, A_AIDL_BDIS);
101 fsm->a_aidl_bdis_tmout = 0;
102 fsm->a_suspend_req_inf = 0;
104 case OTG_STATE_A_PERIPHERAL:
105 otg_del_timer(fsm, A_BIDL_ADIS);
106 fsm->a_bidl_adis_tmout = 0;
108 case OTG_STATE_A_WAIT_VFALL:
109 otg_del_timer(fsm, A_WAIT_VFALL);
110 fsm->a_wait_vfall_tmout = 0;
111 otg_del_timer(fsm, A_WAIT_VRISE);
113 case OTG_STATE_A_VBUS_ERR:
120 /* Called when entering a state */
121 int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
124 if (fsm->otg->phy->state == new_state)
126 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
127 otg_leave_state(fsm, fsm->otg->phy->state);
129 case OTG_STATE_B_IDLE:
130 otg_drv_vbus(fsm, 0);
131 otg_chrg_vbus(fsm, 0);
132 otg_loc_conn(fsm, 0);
134 otg_set_protocol(fsm, PROTO_UNDEF);
135 otg_add_timer(fsm, B_SE0_SRP);
137 case OTG_STATE_B_SRP_INIT:
138 otg_start_pulse(fsm);
140 otg_set_protocol(fsm, PROTO_UNDEF);
141 otg_add_timer(fsm, B_SRP_FAIL);
143 case OTG_STATE_B_PERIPHERAL:
144 otg_chrg_vbus(fsm, 0);
145 otg_loc_conn(fsm, 1);
147 otg_set_protocol(fsm, PROTO_GADGET);
149 case OTG_STATE_B_WAIT_ACON:
150 otg_chrg_vbus(fsm, 0);
151 otg_loc_conn(fsm, 0);
153 otg_set_protocol(fsm, PROTO_HOST);
154 otg_add_timer(fsm, B_ASE0_BRST);
155 fsm->a_bus_suspend = 0;
157 case OTG_STATE_B_HOST:
158 otg_chrg_vbus(fsm, 0);
159 otg_loc_conn(fsm, 0);
161 otg_set_protocol(fsm, PROTO_HOST);
162 usb_bus_start_enum(fsm->otg->host,
163 fsm->otg->host->otg_port);
165 case OTG_STATE_A_IDLE:
166 otg_drv_vbus(fsm, 0);
167 otg_chrg_vbus(fsm, 0);
168 otg_loc_conn(fsm, 0);
170 otg_set_protocol(fsm, PROTO_HOST);
172 case OTG_STATE_A_WAIT_VRISE:
173 otg_drv_vbus(fsm, 1);
174 otg_loc_conn(fsm, 0);
176 otg_set_protocol(fsm, PROTO_HOST);
177 otg_add_timer(fsm, A_WAIT_VRISE);
179 case OTG_STATE_A_WAIT_BCON:
180 otg_drv_vbus(fsm, 1);
181 otg_loc_conn(fsm, 0);
183 otg_set_protocol(fsm, PROTO_HOST);
184 otg_add_timer(fsm, A_WAIT_BCON);
186 case OTG_STATE_A_HOST:
187 otg_drv_vbus(fsm, 1);
188 otg_loc_conn(fsm, 0);
190 otg_set_protocol(fsm, PROTO_HOST);
192 * When HNP is triggered while a_bus_req = 0, a_host will
193 * suspend too fast to complete a_set_b_hnp_en
195 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
196 otg_add_timer(fsm, A_WAIT_ENUM);
198 case OTG_STATE_A_SUSPEND:
199 otg_drv_vbus(fsm, 1);
200 otg_loc_conn(fsm, 0);
202 otg_set_protocol(fsm, PROTO_HOST);
203 otg_add_timer(fsm, A_AIDL_BDIS);
206 case OTG_STATE_A_PERIPHERAL:
207 otg_loc_conn(fsm, 1);
209 otg_set_protocol(fsm, PROTO_GADGET);
210 otg_drv_vbus(fsm, 1);
211 otg_add_timer(fsm, A_BIDL_ADIS);
213 case OTG_STATE_A_WAIT_VFALL:
214 otg_drv_vbus(fsm, 0);
215 otg_loc_conn(fsm, 0);
217 otg_set_protocol(fsm, PROTO_HOST);
218 otg_add_timer(fsm, A_WAIT_VFALL);
220 case OTG_STATE_A_VBUS_ERR:
221 otg_drv_vbus(fsm, 0);
222 otg_loc_conn(fsm, 0);
224 otg_set_protocol(fsm, PROTO_UNDEF);
230 fsm->otg->phy->state = new_state;
234 /* State change judgement */
235 int otg_statemachine(struct otg_fsm *fsm)
237 enum usb_otg_state state;
240 spin_lock_irqsave(&fsm->lock, flags);
242 state = fsm->otg->phy->state;
244 /* State machine state change judgement */
247 case OTG_STATE_UNDEFINED:
248 VDBG("fsm->id = %d\n", fsm->id);
250 otg_set_state(fsm, OTG_STATE_B_IDLE);
252 otg_set_state(fsm, OTG_STATE_A_IDLE);
254 case OTG_STATE_B_IDLE:
256 otg_set_state(fsm, OTG_STATE_A_IDLE);
257 else if (fsm->b_sess_vld && fsm->otg->gadget)
258 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
259 else if (fsm->b_bus_req && fsm->b_ssend_srp && fsm->b_se0_srp)
260 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
262 case OTG_STATE_B_SRP_INIT:
263 if (!fsm->id || fsm->b_srp_done)
264 otg_set_state(fsm, OTG_STATE_B_IDLE);
266 case OTG_STATE_B_PERIPHERAL:
267 if (!fsm->id || !fsm->b_sess_vld)
268 otg_set_state(fsm, OTG_STATE_B_IDLE);
269 else if (fsm->b_bus_req && fsm->otg->
270 gadget->b_hnp_enable && fsm->a_bus_suspend)
271 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
273 case OTG_STATE_B_WAIT_ACON:
275 otg_set_state(fsm, OTG_STATE_B_HOST);
276 else if (!fsm->id || !fsm->b_sess_vld)
277 otg_set_state(fsm, OTG_STATE_B_IDLE);
278 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
279 fsm->b_ase0_brst_tmout = 0;
280 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
283 case OTG_STATE_B_HOST:
284 if (!fsm->id || !fsm->b_sess_vld)
285 otg_set_state(fsm, OTG_STATE_B_IDLE);
286 else if (!fsm->b_bus_req || !fsm->a_conn)
287 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
289 case OTG_STATE_A_IDLE:
291 otg_set_state(fsm, OTG_STATE_B_IDLE);
292 else if (!fsm->a_bus_drop && (fsm->a_bus_req || fsm->a_srp_det))
293 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
295 case OTG_STATE_A_WAIT_VRISE:
296 if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
297 fsm->a_wait_vrise_tmout) {
298 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
301 case OTG_STATE_A_WAIT_BCON:
302 if (!fsm->a_vbus_vld)
303 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
304 else if (fsm->b_conn)
305 otg_set_state(fsm, OTG_STATE_A_HOST);
306 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
307 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
309 case OTG_STATE_A_HOST:
310 if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
311 fsm->otg->host->b_hnp_enable)
312 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
313 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
314 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
315 else if (!fsm->a_vbus_vld)
316 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
318 case OTG_STATE_A_SUSPEND:
319 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
320 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
321 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
322 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
323 else if (fsm->a_bus_req || fsm->b_bus_resume)
324 otg_set_state(fsm, OTG_STATE_A_HOST);
325 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
326 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
327 else if (!fsm->a_vbus_vld)
328 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
330 case OTG_STATE_A_PERIPHERAL:
331 if (fsm->id || fsm->a_bus_drop)
332 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
333 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
334 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
335 else if (!fsm->a_vbus_vld)
336 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
338 case OTG_STATE_A_WAIT_VFALL:
339 if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req ||
340 (!fsm->a_sess_vld && !fsm->b_conn))
341 otg_set_state(fsm, OTG_STATE_A_IDLE);
343 case OTG_STATE_A_VBUS_ERR:
344 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
345 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
350 spin_unlock_irqrestore(&fsm->lock, flags);
352 VDBG("quit statemachine, changed = %d\n", state_changed);
353 return state_changed;