Commit | Line | Data |
---|---|---|
0807c500 LY |
1 | /* |
2 | * OTG Finite State Machine from OTG spec | |
3 | * | |
4 | * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. | |
5 | * | |
6 | * Author: Li Yang <LeoLi@freescale.com> | |
7 | * Jerry Huang <Chang-Ming.Huang@freescale.com> | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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. | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/types.h> | |
16e569e9 | 26 | #include <linux/mutex.h> |
0807c500 LY |
27 | #include <linux/delay.h> |
28 | #include <linux/usb.h> | |
29 | #include <linux/usb/gadget.h> | |
30 | #include <linux/usb/otg.h> | |
5653668c | 31 | #include <linux/usb/otg-fsm.h> |
0807c500 LY |
32 | |
33 | /* Change USB protocol when there is a protocol change */ | |
34 | static int otg_set_protocol(struct otg_fsm *fsm, int protocol) | |
35 | { | |
36 | int ret = 0; | |
37 | ||
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) | |
425d7101 | 43 | ret = otg_start_host(fsm, 0); |
0807c500 | 44 | else if (fsm->protocol == PROTO_GADGET) |
425d7101 | 45 | ret = otg_start_gadget(fsm, 0); |
0807c500 LY |
46 | if (ret) |
47 | return ret; | |
48 | ||
49 | /* start new protocol */ | |
50 | if (protocol == PROTO_HOST) | |
425d7101 | 51 | ret = otg_start_host(fsm, 1); |
0807c500 | 52 | else if (protocol == PROTO_GADGET) |
425d7101 | 53 | ret = otg_start_gadget(fsm, 1); |
0807c500 LY |
54 | if (ret) |
55 | return ret; | |
56 | ||
57 | fsm->protocol = protocol; | |
58 | return 0; | |
59 | } | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | static int state_changed; | |
65 | ||
66 | /* Called when leaving a state. Do state clean up jobs here */ | |
7410f172 | 67 | static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state) |
0807c500 LY |
68 | { |
69 | switch (old_state) { | |
70 | case OTG_STATE_B_IDLE: | |
f6de27ee | 71 | otg_del_timer(fsm, B_SE0_SRP); |
0807c500 | 72 | fsm->b_se0_srp = 0; |
ec04996a AT |
73 | fsm->adp_sns = 0; |
74 | fsm->adp_prb = 0; | |
0807c500 LY |
75 | break; |
76 | case OTG_STATE_B_SRP_INIT: | |
ec04996a | 77 | fsm->data_pulse = 0; |
0807c500 LY |
78 | fsm->b_srp_done = 0; |
79 | break; | |
80 | case OTG_STATE_B_PERIPHERAL: | |
81 | break; | |
82 | case OTG_STATE_B_WAIT_ACON: | |
f6de27ee | 83 | otg_del_timer(fsm, B_ASE0_BRST); |
0807c500 LY |
84 | fsm->b_ase0_brst_tmout = 0; |
85 | break; | |
86 | case OTG_STATE_B_HOST: | |
87 | break; | |
88 | case OTG_STATE_A_IDLE: | |
ec04996a | 89 | fsm->adp_prb = 0; |
0807c500 LY |
90 | break; |
91 | case OTG_STATE_A_WAIT_VRISE: | |
f6de27ee | 92 | otg_del_timer(fsm, A_WAIT_VRISE); |
0807c500 LY |
93 | fsm->a_wait_vrise_tmout = 0; |
94 | break; | |
95 | case OTG_STATE_A_WAIT_BCON: | |
f6de27ee | 96 | otg_del_timer(fsm, A_WAIT_BCON); |
0807c500 LY |
97 | fsm->a_wait_bcon_tmout = 0; |
98 | break; | |
99 | case OTG_STATE_A_HOST: | |
f6de27ee | 100 | otg_del_timer(fsm, A_WAIT_ENUM); |
0807c500 LY |
101 | break; |
102 | case OTG_STATE_A_SUSPEND: | |
f6de27ee | 103 | otg_del_timer(fsm, A_AIDL_BDIS); |
0807c500 | 104 | fsm->a_aidl_bdis_tmout = 0; |
cff4dab4 | 105 | fsm->a_suspend_req_inf = 0; |
0807c500 LY |
106 | break; |
107 | case OTG_STATE_A_PERIPHERAL: | |
3294908b AT |
108 | otg_del_timer(fsm, A_BIDL_ADIS); |
109 | fsm->a_bidl_adis_tmout = 0; | |
0807c500 LY |
110 | break; |
111 | case OTG_STATE_A_WAIT_VFALL: | |
3294908b AT |
112 | otg_del_timer(fsm, A_WAIT_VFALL); |
113 | fsm->a_wait_vfall_tmout = 0; | |
f6de27ee | 114 | otg_del_timer(fsm, A_WAIT_VRISE); |
0807c500 LY |
115 | break; |
116 | case OTG_STATE_A_VBUS_ERR: | |
117 | break; | |
118 | default: | |
119 | break; | |
120 | } | |
121 | } | |
122 | ||
123 | /* Called when entering a state */ | |
7410f172 | 124 | static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state) |
0807c500 LY |
125 | { |
126 | state_changed = 1; | |
e47d9254 | 127 | if (fsm->otg->state == new_state) |
0807c500 | 128 | return 0; |
42c0bf1c | 129 | VDBG("Set state: %s\n", usb_otg_state_string(new_state)); |
e47d9254 | 130 | otg_leave_state(fsm, fsm->otg->state); |
0807c500 LY |
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); | |
ec04996a AT |
137 | /* |
138 | * Driver is responsible for starting ADP probing | |
139 | * if ADP sensing times out. | |
140 | */ | |
141 | otg_start_adp_sns(fsm); | |
0807c500 | 142 | otg_set_protocol(fsm, PROTO_UNDEF); |
f6de27ee | 143 | otg_add_timer(fsm, B_SE0_SRP); |
0807c500 LY |
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); | |
f6de27ee | 149 | otg_add_timer(fsm, B_SRP_FAIL); |
0807c500 LY |
150 | break; |
151 | case OTG_STATE_B_PERIPHERAL: | |
152 | otg_chrg_vbus(fsm, 0); | |
0807c500 LY |
153 | otg_loc_sof(fsm, 0); |
154 | otg_set_protocol(fsm, PROTO_GADGET); | |
a886bd92 | 155 | otg_loc_conn(fsm, 1); |
0807c500 LY |
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); | |
f6de27ee | 162 | otg_add_timer(fsm, B_ASE0_BRST); |
0807c500 LY |
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); | |
7e062c0f HK |
170 | usb_bus_start_enum(fsm->otg->host, |
171 | fsm->otg->host->otg_port); | |
0807c500 LY |
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); | |
ec04996a | 178 | otg_start_adp_prb(fsm); |
0807c500 LY |
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); | |
f6de27ee | 186 | otg_add_timer(fsm, A_WAIT_VRISE); |
0807c500 LY |
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); | |
f6de27ee | 193 | otg_add_timer(fsm, A_WAIT_BCON); |
0807c500 LY |
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); | |
200 | /* | |
201 | * When HNP is triggered while a_bus_req = 0, a_host will | |
202 | * suspend too fast to complete a_set_b_hnp_en | |
203 | */ | |
cff4dab4 | 204 | if (!fsm->a_bus_req || fsm->a_suspend_req_inf) |
f6de27ee | 205 | otg_add_timer(fsm, A_WAIT_ENUM); |
0807c500 LY |
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); | |
f6de27ee | 212 | otg_add_timer(fsm, A_AIDL_BDIS); |
0807c500 LY |
213 | |
214 | break; | |
215 | case OTG_STATE_A_PERIPHERAL: | |
0807c500 LY |
216 | otg_loc_sof(fsm, 0); |
217 | otg_set_protocol(fsm, PROTO_GADGET); | |
218 | otg_drv_vbus(fsm, 1); | |
a886bd92 | 219 | otg_loc_conn(fsm, 1); |
3294908b | 220 | otg_add_timer(fsm, A_BIDL_ADIS); |
0807c500 LY |
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); | |
3294908b | 227 | otg_add_timer(fsm, A_WAIT_VFALL); |
0807c500 LY |
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; | |
237 | } | |
238 | ||
e47d9254 | 239 | fsm->otg->state = new_state; |
0807c500 LY |
240 | return 0; |
241 | } | |
242 | ||
243 | /* State change judgement */ | |
244 | int otg_statemachine(struct otg_fsm *fsm) | |
245 | { | |
246 | enum usb_otg_state state; | |
0807c500 | 247 | |
16e569e9 | 248 | mutex_lock(&fsm->lock); |
0807c500 | 249 | |
e47d9254 | 250 | state = fsm->otg->state; |
0807c500 LY |
251 | state_changed = 0; |
252 | /* State machine state change judgement */ | |
253 | ||
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); | |
7e062c0f | 265 | else if (fsm->b_sess_vld && fsm->otg->gadget) |
0807c500 | 266 | otg_set_state(fsm, OTG_STATE_B_PERIPHERAL); |
ec04996a AT |
267 | else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) && |
268 | fsm->b_ssend_srp && fsm->b_se0_srp) | |
0807c500 LY |
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); | |
7e062c0f | 278 | else if (fsm->b_bus_req && fsm->otg-> |
0807c500 LY |
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); | |
290 | } | |
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); | |
ec04996a | 295 | else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device) |
0807c500 LY |
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); | |
ec04996a AT |
301 | else if (!fsm->a_bus_drop && (fsm->a_bus_req || |
302 | fsm->a_srp_det || fsm->adp_change || fsm->power_up)) | |
0807c500 LY |
303 | otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE); |
304 | break; | |
305 | case OTG_STATE_A_WAIT_VRISE: | |
2284bb35 | 306 | if (fsm->a_vbus_vld) |
0807c500 | 307 | otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); |
2284bb35 LJ |
308 | else if (fsm->id || fsm->a_bus_drop || |
309 | fsm->a_wait_vrise_tmout) | |
310 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); | |
0807c500 LY |
311 | break; |
312 | case OTG_STATE_A_WAIT_BCON: | |
313 | if (!fsm->a_vbus_vld) | |
314 | otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); | |
315 | else if (fsm->b_conn) | |
316 | otg_set_state(fsm, OTG_STATE_A_HOST); | |
66668991 | 317 | else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout) |
0807c500 LY |
318 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); |
319 | break; | |
320 | case OTG_STATE_A_HOST: | |
8bebbe8d LJ |
321 | if (fsm->id || fsm->a_bus_drop) |
322 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); | |
323 | else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) && | |
7e062c0f | 324 | fsm->otg->host->b_hnp_enable) |
0807c500 | 325 | otg_set_state(fsm, OTG_STATE_A_SUSPEND); |
8bebbe8d | 326 | else if (!fsm->b_conn) |
0807c500 LY |
327 | otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); |
328 | else if (!fsm->a_vbus_vld) | |
329 | otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); | |
330 | break; | |
331 | case OTG_STATE_A_SUSPEND: | |
7e062c0f | 332 | if (!fsm->b_conn && fsm->otg->host->b_hnp_enable) |
0807c500 | 333 | otg_set_state(fsm, OTG_STATE_A_PERIPHERAL); |
7e062c0f | 334 | else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable) |
0807c500 LY |
335 | otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); |
336 | else if (fsm->a_bus_req || fsm->b_bus_resume) | |
337 | otg_set_state(fsm, OTG_STATE_A_HOST); | |
338 | else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout) | |
339 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); | |
340 | else if (!fsm->a_vbus_vld) | |
341 | otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); | |
342 | break; | |
343 | case OTG_STATE_A_PERIPHERAL: | |
344 | if (fsm->id || fsm->a_bus_drop) | |
345 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); | |
3294908b | 346 | else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend) |
0807c500 LY |
347 | otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); |
348 | else if (!fsm->a_vbus_vld) | |
349 | otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); | |
350 | break; | |
351 | case OTG_STATE_A_WAIT_VFALL: | |
8bebbe8d | 352 | if (fsm->a_wait_vfall_tmout) |
0807c500 LY |
353 | otg_set_state(fsm, OTG_STATE_A_IDLE); |
354 | break; | |
355 | case OTG_STATE_A_VBUS_ERR: | |
356 | if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err) | |
357 | otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); | |
358 | break; | |
359 | default: | |
360 | break; | |
361 | } | |
16da4b17 | 362 | mutex_unlock(&fsm->lock); |
0807c500 LY |
363 | |
364 | VDBG("quit statemachine, changed = %d\n", state_changed); | |
365 | return state_changed; | |
366 | } | |
be696aac | 367 | EXPORT_SYMBOL_GPL(otg_statemachine); |