ARM: 7989/1: Delete asm/system.h
[linux/fpc-iii.git] / drivers / usb / phy / phy-fsm-usb.c
blob7aa314ef4a8ade13bac4ba73b41645d1b522dd84
1 /*
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)
36 int ret = 0;
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);
46 if (ret)
47 return ret;
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);
54 if (ret)
55 return ret;
57 fsm->protocol = protocol;
58 return 0;
61 return 0;
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)
69 switch (old_state) {
70 case OTG_STATE_B_IDLE:
71 otg_del_timer(fsm, B_SE0_SRP);
72 fsm->b_se0_srp = 0;
73 fsm->adp_sns = 0;
74 fsm->adp_prb = 0;
75 break;
76 case OTG_STATE_B_SRP_INIT:
77 fsm->data_pulse = 0;
78 fsm->b_srp_done = 0;
79 break;
80 case OTG_STATE_B_PERIPHERAL:
81 break;
82 case OTG_STATE_B_WAIT_ACON:
83 otg_del_timer(fsm, B_ASE0_BRST);
84 fsm->b_ase0_brst_tmout = 0;
85 break;
86 case OTG_STATE_B_HOST:
87 break;
88 case OTG_STATE_A_IDLE:
89 fsm->adp_prb = 0;
90 break;
91 case OTG_STATE_A_WAIT_VRISE:
92 otg_del_timer(fsm, A_WAIT_VRISE);
93 fsm->a_wait_vrise_tmout = 0;
94 break;
95 case OTG_STATE_A_WAIT_BCON:
96 otg_del_timer(fsm, A_WAIT_BCON);
97 fsm->a_wait_bcon_tmout = 0;
98 break;
99 case OTG_STATE_A_HOST:
100 otg_del_timer(fsm, A_WAIT_ENUM);
101 break;
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;
106 break;
107 case OTG_STATE_A_PERIPHERAL:
108 otg_del_timer(fsm, A_BIDL_ADIS);
109 fsm->a_bidl_adis_tmout = 0;
110 break;
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);
115 break;
116 case OTG_STATE_A_VBUS_ERR:
117 break;
118 default:
119 break;
123 /* Called when entering a state */
124 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
126 state_changed = 1;
127 if (fsm->otg->phy->state == new_state)
128 return 0;
129 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
130 otg_leave_state(fsm, fsm->otg->phy->state);
131 switch (new_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);
136 otg_loc_sof(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);
144 break;
145 case OTG_STATE_B_SRP_INIT:
146 otg_start_pulse(fsm);
147 otg_loc_sof(fsm, 0);
148 otg_set_protocol(fsm, PROTO_UNDEF);
149 otg_add_timer(fsm, B_SRP_FAIL);
150 break;
151 case OTG_STATE_B_PERIPHERAL:
152 otg_chrg_vbus(fsm, 0);
153 otg_loc_conn(fsm, 1);
154 otg_loc_sof(fsm, 0);
155 otg_set_protocol(fsm, PROTO_GADGET);
156 break;
157 case OTG_STATE_B_WAIT_ACON:
158 otg_chrg_vbus(fsm, 0);
159 otg_loc_conn(fsm, 0);
160 otg_loc_sof(fsm, 0);
161 otg_set_protocol(fsm, PROTO_HOST);
162 otg_add_timer(fsm, B_ASE0_BRST);
163 fsm->a_bus_suspend = 0;
164 break;
165 case OTG_STATE_B_HOST:
166 otg_chrg_vbus(fsm, 0);
167 otg_loc_conn(fsm, 0);
168 otg_loc_sof(fsm, 1);
169 otg_set_protocol(fsm, PROTO_HOST);
170 usb_bus_start_enum(fsm->otg->host,
171 fsm->otg->host->otg_port);
172 break;
173 case OTG_STATE_A_IDLE:
174 otg_drv_vbus(fsm, 0);
175 otg_chrg_vbus(fsm, 0);
176 otg_loc_conn(fsm, 0);
177 otg_loc_sof(fsm, 0);
178 otg_start_adp_prb(fsm);
179 otg_set_protocol(fsm, PROTO_HOST);
180 break;
181 case OTG_STATE_A_WAIT_VRISE:
182 otg_drv_vbus(fsm, 1);
183 otg_loc_conn(fsm, 0);
184 otg_loc_sof(fsm, 0);
185 otg_set_protocol(fsm, PROTO_HOST);
186 otg_add_timer(fsm, A_WAIT_VRISE);
187 break;
188 case OTG_STATE_A_WAIT_BCON:
189 otg_drv_vbus(fsm, 1);
190 otg_loc_conn(fsm, 0);
191 otg_loc_sof(fsm, 0);
192 otg_set_protocol(fsm, PROTO_HOST);
193 otg_add_timer(fsm, A_WAIT_BCON);
194 break;
195 case OTG_STATE_A_HOST:
196 otg_drv_vbus(fsm, 1);
197 otg_loc_conn(fsm, 0);
198 otg_loc_sof(fsm, 1);
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);
206 break;
207 case OTG_STATE_A_SUSPEND:
208 otg_drv_vbus(fsm, 1);
209 otg_loc_conn(fsm, 0);
210 otg_loc_sof(fsm, 0);
211 otg_set_protocol(fsm, PROTO_HOST);
212 otg_add_timer(fsm, A_AIDL_BDIS);
214 break;
215 case OTG_STATE_A_PERIPHERAL:
216 otg_loc_conn(fsm, 1);
217 otg_loc_sof(fsm, 0);
218 otg_set_protocol(fsm, PROTO_GADGET);
219 otg_drv_vbus(fsm, 1);
220 otg_add_timer(fsm, A_BIDL_ADIS);
221 break;
222 case OTG_STATE_A_WAIT_VFALL:
223 otg_drv_vbus(fsm, 0);
224 otg_loc_conn(fsm, 0);
225 otg_loc_sof(fsm, 0);
226 otg_set_protocol(fsm, PROTO_HOST);
227 otg_add_timer(fsm, A_WAIT_VFALL);
228 break;
229 case OTG_STATE_A_VBUS_ERR:
230 otg_drv_vbus(fsm, 0);
231 otg_loc_conn(fsm, 0);
232 otg_loc_sof(fsm, 0);
233 otg_set_protocol(fsm, PROTO_UNDEF);
234 break;
235 default:
236 break;
239 fsm->otg->phy->state = new_state;
240 return 0;
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;
251 state_changed = 0;
252 /* State machine state change judgement */
254 switch (state) {
255 case OTG_STATE_UNDEFINED:
256 VDBG("fsm->id = %d\n", fsm->id);
257 if (fsm->id)
258 otg_set_state(fsm, OTG_STATE_B_IDLE);
259 else
260 otg_set_state(fsm, OTG_STATE_A_IDLE);
261 break;
262 case OTG_STATE_B_IDLE:
263 if (!fsm->id)
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);
270 break;
271 case OTG_STATE_B_SRP_INIT:
272 if (!fsm->id || fsm->b_srp_done)
273 otg_set_state(fsm, OTG_STATE_B_IDLE);
274 break;
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);
281 break;
282 case OTG_STATE_B_WAIT_ACON:
283 if (fsm->a_conn)
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);
291 break;
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);
297 break;
298 case OTG_STATE_A_IDLE:
299 if (fsm->id)
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);
304 break;
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);
310 break;
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);
318 break;
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);
327 break;
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);
339 break;
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);
347 break;
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);
352 break;
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);
356 break;
357 default:
358 break;
360 mutex_unlock(&fsm->lock);
362 VDBG("quit statemachine, changed = %d\n", state_changed);
363 return state_changed;