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>
31 #include <linux/types.h>
35 /* Change USB protocol when there is a protocol change */
36 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
40 if (fsm->protocol != protocol) {
41 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
42 fsm->protocol, protocol);
43 /* stop old protocol */
44 if (fsm->protocol == PROTO_HOST)
45 ret = fsm->ops->start_host(fsm, 0);
46 else if (fsm->protocol == PROTO_GADGET)
47 ret = fsm->ops->start_gadget(fsm, 0);
51 /* start new protocol */
52 if (protocol == PROTO_HOST)
53 ret = fsm->ops->start_host(fsm, 1);
54 else if (protocol == PROTO_GADGET)
55 ret = fsm->ops->start_gadget(fsm, 1);
59 fsm->protocol = protocol;
66 static int state_changed;
68 /* Called when leaving a state. Do state clean up jobs here */
69 void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
72 case OTG_STATE_B_IDLE:
73 otg_del_timer(fsm, b_se0_srp_tmr);
76 case OTG_STATE_B_SRP_INIT:
79 case OTG_STATE_B_PERIPHERAL:
81 case OTG_STATE_B_WAIT_ACON:
82 otg_del_timer(fsm, b_ase0_brst_tmr);
83 fsm->b_ase0_brst_tmout = 0;
85 case OTG_STATE_B_HOST:
87 case OTG_STATE_A_IDLE:
89 case OTG_STATE_A_WAIT_VRISE:
90 otg_del_timer(fsm, a_wait_vrise_tmr);
91 fsm->a_wait_vrise_tmout = 0;
93 case OTG_STATE_A_WAIT_BCON:
94 otg_del_timer(fsm, a_wait_bcon_tmr);
95 fsm->a_wait_bcon_tmout = 0;
97 case OTG_STATE_A_HOST:
98 otg_del_timer(fsm, a_wait_enum_tmr);
100 case OTG_STATE_A_SUSPEND:
101 otg_del_timer(fsm, a_aidl_bdis_tmr);
102 fsm->a_aidl_bdis_tmout = 0;
103 fsm->a_suspend_req = 0;
105 case OTG_STATE_A_PERIPHERAL:
107 case OTG_STATE_A_WAIT_VFALL:
108 otg_del_timer(fsm, a_wait_vrise_tmr);
110 case OTG_STATE_A_VBUS_ERR:
117 /* Called when entering a state */
118 int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
121 if (fsm->transceiver->state == new_state)
123 VDBG("Set state: %s\n", otg_state_string(new_state));
124 otg_leave_state(fsm, fsm->transceiver->state);
126 case OTG_STATE_B_IDLE:
127 otg_drv_vbus(fsm, 0);
128 otg_chrg_vbus(fsm, 0);
129 otg_loc_conn(fsm, 0);
131 otg_set_protocol(fsm, PROTO_UNDEF);
132 otg_add_timer(fsm, b_se0_srp_tmr);
134 case OTG_STATE_B_SRP_INIT:
135 otg_start_pulse(fsm);
137 otg_set_protocol(fsm, PROTO_UNDEF);
138 otg_add_timer(fsm, b_srp_fail_tmr);
140 case OTG_STATE_B_PERIPHERAL:
141 otg_chrg_vbus(fsm, 0);
142 otg_loc_conn(fsm, 1);
144 otg_set_protocol(fsm, PROTO_GADGET);
146 case OTG_STATE_B_WAIT_ACON:
147 otg_chrg_vbus(fsm, 0);
148 otg_loc_conn(fsm, 0);
150 otg_set_protocol(fsm, PROTO_HOST);
151 otg_add_timer(fsm, b_ase0_brst_tmr);
152 fsm->a_bus_suspend = 0;
154 case OTG_STATE_B_HOST:
155 otg_chrg_vbus(fsm, 0);
156 otg_loc_conn(fsm, 0);
158 otg_set_protocol(fsm, PROTO_HOST);
159 usb_bus_start_enum(fsm->transceiver->host,
160 fsm->transceiver->host->otg_port);
162 case OTG_STATE_A_IDLE:
163 otg_drv_vbus(fsm, 0);
164 otg_chrg_vbus(fsm, 0);
165 otg_loc_conn(fsm, 0);
167 otg_set_protocol(fsm, PROTO_HOST);
169 case OTG_STATE_A_WAIT_VRISE:
170 otg_drv_vbus(fsm, 1);
171 otg_loc_conn(fsm, 0);
173 otg_set_protocol(fsm, PROTO_HOST);
174 otg_add_timer(fsm, a_wait_vrise_tmr);
176 case OTG_STATE_A_WAIT_BCON:
177 otg_drv_vbus(fsm, 1);
178 otg_loc_conn(fsm, 0);
180 otg_set_protocol(fsm, PROTO_HOST);
181 otg_add_timer(fsm, a_wait_bcon_tmr);
183 case OTG_STATE_A_HOST:
184 otg_drv_vbus(fsm, 1);
185 otg_loc_conn(fsm, 0);
187 otg_set_protocol(fsm, PROTO_HOST);
189 * When HNP is triggered while a_bus_req = 0, a_host will
190 * suspend too fast to complete a_set_b_hnp_en
192 if (!fsm->a_bus_req || fsm->a_suspend_req)
193 otg_add_timer(fsm, a_wait_enum_tmr);
195 case OTG_STATE_A_SUSPEND:
196 otg_drv_vbus(fsm, 1);
197 otg_loc_conn(fsm, 0);
199 otg_set_protocol(fsm, PROTO_HOST);
200 otg_add_timer(fsm, a_aidl_bdis_tmr);
203 case OTG_STATE_A_PERIPHERAL:
204 otg_loc_conn(fsm, 1);
206 otg_set_protocol(fsm, PROTO_GADGET);
207 otg_drv_vbus(fsm, 1);
209 case OTG_STATE_A_WAIT_VFALL:
210 otg_drv_vbus(fsm, 0);
211 otg_loc_conn(fsm, 0);
213 otg_set_protocol(fsm, PROTO_HOST);
215 case OTG_STATE_A_VBUS_ERR:
216 otg_drv_vbus(fsm, 0);
217 otg_loc_conn(fsm, 0);
219 otg_set_protocol(fsm, PROTO_UNDEF);
225 fsm->transceiver->state = new_state;
229 /* State change judgement */
230 int otg_statemachine(struct otg_fsm *fsm)
232 enum usb_otg_state state;
235 spin_lock_irqsave(&fsm->lock, flags);
237 state = fsm->transceiver->state;
239 /* State machine state change judgement */
242 case OTG_STATE_UNDEFINED:
243 VDBG("fsm->id = %d\n", fsm->id);
245 otg_set_state(fsm, OTG_STATE_B_IDLE);
247 otg_set_state(fsm, OTG_STATE_A_IDLE);
249 case OTG_STATE_B_IDLE:
251 otg_set_state(fsm, OTG_STATE_A_IDLE);
252 else if (fsm->b_sess_vld && fsm->transceiver->gadget)
253 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
254 else if (fsm->b_bus_req && fsm->b_sess_end && fsm->b_se0_srp)
255 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
257 case OTG_STATE_B_SRP_INIT:
258 if (!fsm->id || fsm->b_srp_done)
259 otg_set_state(fsm, OTG_STATE_B_IDLE);
261 case OTG_STATE_B_PERIPHERAL:
262 if (!fsm->id || !fsm->b_sess_vld)
263 otg_set_state(fsm, OTG_STATE_B_IDLE);
264 else if (fsm->b_bus_req && fsm->transceiver->
265 gadget->b_hnp_enable && fsm->a_bus_suspend)
266 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
268 case OTG_STATE_B_WAIT_ACON:
270 otg_set_state(fsm, OTG_STATE_B_HOST);
271 else if (!fsm->id || !fsm->b_sess_vld)
272 otg_set_state(fsm, OTG_STATE_B_IDLE);
273 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
274 fsm->b_ase0_brst_tmout = 0;
275 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
278 case OTG_STATE_B_HOST:
279 if (!fsm->id || !fsm->b_sess_vld)
280 otg_set_state(fsm, OTG_STATE_B_IDLE);
281 else if (!fsm->b_bus_req || !fsm->a_conn)
282 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
284 case OTG_STATE_A_IDLE:
286 otg_set_state(fsm, OTG_STATE_B_IDLE);
287 else if (!fsm->a_bus_drop && (fsm->a_bus_req || fsm->a_srp_det))
288 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
290 case OTG_STATE_A_WAIT_VRISE:
291 if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
292 fsm->a_wait_vrise_tmout) {
293 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
296 case OTG_STATE_A_WAIT_BCON:
297 if (!fsm->a_vbus_vld)
298 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
299 else if (fsm->b_conn)
300 otg_set_state(fsm, OTG_STATE_A_HOST);
301 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
302 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
304 case OTG_STATE_A_HOST:
305 if ((!fsm->a_bus_req || fsm->a_suspend_req) &&
306 fsm->transceiver->host->b_hnp_enable)
307 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
308 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
309 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
310 else if (!fsm->a_vbus_vld)
311 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
313 case OTG_STATE_A_SUSPEND:
314 if (!fsm->b_conn && fsm->transceiver->host->b_hnp_enable)
315 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
316 else if (!fsm->b_conn && !fsm->transceiver->host->b_hnp_enable)
317 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
318 else if (fsm->a_bus_req || fsm->b_bus_resume)
319 otg_set_state(fsm, OTG_STATE_A_HOST);
320 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
321 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
322 else if (!fsm->a_vbus_vld)
323 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
325 case OTG_STATE_A_PERIPHERAL:
326 if (fsm->id || fsm->a_bus_drop)
327 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
328 else if (fsm->b_bus_suspend)
329 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
330 else if (!fsm->a_vbus_vld)
331 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
333 case OTG_STATE_A_WAIT_VFALL:
334 if (fsm->id || fsm->a_bus_req || (!fsm->a_sess_vld &&
336 otg_set_state(fsm, OTG_STATE_A_IDLE);
338 case OTG_STATE_A_VBUS_ERR:
339 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
340 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
345 spin_unlock_irqrestore(&fsm->lock, flags);
347 VDBG("quit statemachine, changed = %d\n", state_changed);
348 return state_changed;