2 * OTG Finite State Machine from OTG spec
4 * Copyright (C) 2007-2015 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)
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);
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);
57 fsm->protocol = protocol;
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)
70 case OTG_STATE_B_IDLE:
71 otg_del_timer(fsm, B_SE0_SRP);
76 case OTG_STATE_B_SRP_INIT:
80 case OTG_STATE_B_PERIPHERAL:
82 fsm->otg->gadget->host_request_flag = 0;
84 case OTG_STATE_B_WAIT_ACON:
85 otg_del_timer(fsm, B_ASE0_BRST);
86 fsm->b_ase0_brst_tmout = 0;
88 case OTG_STATE_B_HOST:
90 case OTG_STATE_A_IDLE:
93 case OTG_STATE_A_WAIT_VRISE:
94 otg_del_timer(fsm, A_WAIT_VRISE);
95 fsm->a_wait_vrise_tmout = 0;
97 case OTG_STATE_A_WAIT_BCON:
98 otg_del_timer(fsm, A_WAIT_BCON);
99 fsm->a_wait_bcon_tmout = 0;
101 case OTG_STATE_A_HOST:
102 otg_del_timer(fsm, A_WAIT_ENUM);
104 case OTG_STATE_A_SUSPEND:
105 otg_del_timer(fsm, A_AIDL_BDIS);
106 fsm->a_aidl_bdis_tmout = 0;
107 fsm->a_suspend_req_inf = 0;
109 case OTG_STATE_A_PERIPHERAL:
110 otg_del_timer(fsm, A_BIDL_ADIS);
111 fsm->a_bidl_adis_tmout = 0;
112 if (fsm->otg->gadget)
113 fsm->otg->gadget->host_request_flag = 0;
115 case OTG_STATE_A_WAIT_VFALL:
116 otg_del_timer(fsm, A_WAIT_VFALL);
117 fsm->a_wait_vfall_tmout = 0;
118 otg_del_timer(fsm, A_WAIT_VRISE);
120 case OTG_STATE_A_VBUS_ERR:
127 /* Called when entering a state */
128 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
131 if (fsm->otg->state == new_state)
133 VDBG("Set state: %s\n", usb_otg_state_string(new_state));
134 otg_leave_state(fsm, fsm->otg->state);
136 case OTG_STATE_B_IDLE:
137 otg_drv_vbus(fsm, 0);
138 otg_chrg_vbus(fsm, 0);
139 otg_loc_conn(fsm, 0);
142 * Driver is responsible for starting ADP probing
143 * if ADP sensing times out.
145 otg_start_adp_sns(fsm);
146 otg_set_protocol(fsm, PROTO_UNDEF);
147 otg_add_timer(fsm, B_SE0_SRP);
149 case OTG_STATE_B_SRP_INIT:
150 otg_start_pulse(fsm);
152 otg_set_protocol(fsm, PROTO_UNDEF);
153 otg_add_timer(fsm, B_SRP_FAIL);
155 case OTG_STATE_B_PERIPHERAL:
156 otg_chrg_vbus(fsm, 0);
157 otg_loc_conn(fsm, 1);
159 otg_set_protocol(fsm, PROTO_GADGET);
161 case OTG_STATE_B_WAIT_ACON:
162 otg_chrg_vbus(fsm, 0);
163 otg_loc_conn(fsm, 0);
165 otg_set_protocol(fsm, PROTO_HOST);
166 otg_add_timer(fsm, B_ASE0_BRST);
167 fsm->a_bus_suspend = 0;
169 case OTG_STATE_B_HOST:
170 otg_chrg_vbus(fsm, 0);
171 otg_loc_conn(fsm, 0);
173 otg_set_protocol(fsm, PROTO_HOST);
174 usb_bus_start_enum(fsm->otg->host,
175 fsm->otg->host->otg_port);
176 otg_add_timer(fsm, HNP_POLLING);
178 case OTG_STATE_A_IDLE:
179 otg_drv_vbus(fsm, 0);
180 otg_chrg_vbus(fsm, 0);
181 otg_loc_conn(fsm, 0);
183 otg_start_adp_prb(fsm);
184 otg_set_protocol(fsm, PROTO_HOST);
186 case OTG_STATE_A_WAIT_VRISE:
187 otg_drv_vbus(fsm, 1);
188 otg_loc_conn(fsm, 0);
190 otg_set_protocol(fsm, PROTO_HOST);
191 otg_add_timer(fsm, A_WAIT_VRISE);
193 case OTG_STATE_A_WAIT_BCON:
194 otg_drv_vbus(fsm, 1);
195 otg_loc_conn(fsm, 0);
197 otg_set_protocol(fsm, PROTO_HOST);
198 otg_add_timer(fsm, A_WAIT_BCON);
200 case OTG_STATE_A_HOST:
201 otg_drv_vbus(fsm, 1);
202 otg_loc_conn(fsm, 0);
204 otg_set_protocol(fsm, PROTO_HOST);
206 * When HNP is triggered while a_bus_req = 0, a_host will
207 * suspend too fast to complete a_set_b_hnp_en
209 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
210 otg_add_timer(fsm, A_WAIT_ENUM);
211 otg_add_timer(fsm, HNP_POLLING);
213 case OTG_STATE_A_SUSPEND:
214 otg_drv_vbus(fsm, 1);
215 otg_loc_conn(fsm, 0);
217 otg_set_protocol(fsm, PROTO_HOST);
218 otg_add_timer(fsm, A_AIDL_BDIS);
221 case OTG_STATE_A_PERIPHERAL:
222 otg_loc_conn(fsm, 1);
224 otg_set_protocol(fsm, PROTO_GADGET);
225 otg_drv_vbus(fsm, 1);
226 otg_add_timer(fsm, A_BIDL_ADIS);
228 case OTG_STATE_A_WAIT_VFALL:
229 otg_drv_vbus(fsm, 0);
230 otg_loc_conn(fsm, 0);
232 otg_set_protocol(fsm, PROTO_HOST);
233 otg_add_timer(fsm, A_WAIT_VFALL);
235 case OTG_STATE_A_VBUS_ERR:
236 otg_drv_vbus(fsm, 0);
237 otg_loc_conn(fsm, 0);
239 otg_set_protocol(fsm, PROTO_UNDEF);
245 fsm->otg->state = new_state;
249 /* State change judgement */
250 int otg_statemachine(struct otg_fsm *fsm)
252 enum usb_otg_state state;
254 mutex_lock(&fsm->lock);
256 state = fsm->otg->state;
258 /* State machine state change judgement */
261 case OTG_STATE_UNDEFINED:
262 VDBG("fsm->id = %d\n", fsm->id);
264 otg_set_state(fsm, OTG_STATE_B_IDLE);
266 otg_set_state(fsm, OTG_STATE_A_IDLE);
268 case OTG_STATE_B_IDLE:
270 otg_set_state(fsm, OTG_STATE_A_IDLE);
271 else if (fsm->b_sess_vld && fsm->otg->gadget)
272 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
273 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
274 fsm->b_ssend_srp && fsm->b_se0_srp)
275 otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
277 case OTG_STATE_B_SRP_INIT:
278 if (!fsm->id || fsm->b_srp_done)
279 otg_set_state(fsm, OTG_STATE_B_IDLE);
281 case OTG_STATE_B_PERIPHERAL:
282 if (!fsm->id || !fsm->b_sess_vld)
283 otg_set_state(fsm, OTG_STATE_B_IDLE);
284 else if (fsm->b_bus_req && fsm->otg->
285 gadget->b_hnp_enable && fsm->a_bus_suspend)
286 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
288 case OTG_STATE_B_WAIT_ACON:
290 otg_set_state(fsm, OTG_STATE_B_HOST);
291 else if (!fsm->id || !fsm->b_sess_vld)
292 otg_set_state(fsm, OTG_STATE_B_IDLE);
293 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
294 fsm->b_ase0_brst_tmout = 0;
295 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
298 case OTG_STATE_B_HOST:
299 if (!fsm->id || !fsm->b_sess_vld)
300 otg_set_state(fsm, OTG_STATE_B_IDLE);
301 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
302 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
304 case OTG_STATE_A_IDLE:
306 otg_set_state(fsm, OTG_STATE_B_IDLE);
307 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
308 fsm->a_srp_det || fsm->adp_change || fsm->power_up))
309 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
311 case OTG_STATE_A_WAIT_VRISE:
313 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
314 else if (fsm->id || fsm->a_bus_drop ||
315 fsm->a_wait_vrise_tmout)
316 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
318 case OTG_STATE_A_WAIT_BCON:
319 if (!fsm->a_vbus_vld)
320 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
321 else if (fsm->b_conn)
322 otg_set_state(fsm, OTG_STATE_A_HOST);
323 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
324 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
326 case OTG_STATE_A_HOST:
327 if (fsm->id || fsm->a_bus_drop)
328 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
329 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
330 fsm->otg->host->b_hnp_enable)
331 otg_set_state(fsm, OTG_STATE_A_SUSPEND);
332 else if (!fsm->b_conn)
333 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
334 else if (!fsm->a_vbus_vld)
335 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
337 case OTG_STATE_A_SUSPEND:
338 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
339 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
340 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
341 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
342 else if (fsm->a_bus_req || fsm->b_bus_resume)
343 otg_set_state(fsm, OTG_STATE_A_HOST);
344 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
345 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
346 else if (!fsm->a_vbus_vld)
347 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
349 case OTG_STATE_A_PERIPHERAL:
350 if (fsm->id || fsm->a_bus_drop)
351 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
352 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
353 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
354 else if (!fsm->a_vbus_vld)
355 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
357 case OTG_STATE_A_WAIT_VFALL:
358 if (fsm->a_wait_vfall_tmout)
359 otg_set_state(fsm, OTG_STATE_A_IDLE);
361 case OTG_STATE_A_VBUS_ERR:
362 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
363 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
368 mutex_unlock(&fsm->lock);
370 VDBG("quit statemachine, changed = %d\n", state_changed);
371 return state_changed;
373 EXPORT_SYMBOL_GPL(otg_statemachine);
375 static int otg_handle_role_switch(struct otg_fsm *fsm, struct usb_device *udev)
378 enum usb_otg_state state = fsm->otg->state;
380 if (state == OTG_STATE_A_HOST) {
381 /* Set b_hnp_enable */
382 if (!fsm->a_set_b_hnp_en) {
383 err = usb_control_msg(udev,
384 usb_sndctrlpipe(udev, 0),
385 USB_REQ_SET_FEATURE, 0,
386 USB_DEVICE_B_HNP_ENABLE,
388 USB_CTRL_SET_TIMEOUT);
390 /* Continue polling */
391 otg_add_timer(fsm, HNP_POLLING);
394 fsm->a_set_b_hnp_en = 1;
398 return HOST_REQUEST_FLAG;
399 } else if (state == OTG_STATE_B_HOST) {
401 return HOST_REQUEST_FLAG;
408 * Called by host to poll peripheral if it wants to be host
410 * - host request flag(1) if the device wants to be host,
411 * - host request flag(0) if the device keeps peripheral role,
412 * - otherwise, error code.
414 int otg_hnp_polling(struct otg_fsm *fsm)
416 struct usb_device *udev;
419 enum usb_otg_state state = fsm->otg->state;
421 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
424 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
426 dev_err(fsm->otg->host->controller,
427 "no usb dev connected, can't start HNP polling\n");
431 /* Get host request flag from connected USB device */
432 retval = usb_control_msg(udev,
433 usb_rcvctrlpipe(udev, 0),
435 USB_DIR_IN | USB_RECIP_DEVICE,
440 USB_CTRL_GET_TIMEOUT);
442 if (host_req_flag == HOST_REQUEST_FLAG) {
443 retval = otg_handle_role_switch(fsm, udev);
444 } else if (host_req_flag == 0) {
445 /* Continue polling */
446 otg_add_timer(fsm, HNP_POLLING);
449 dev_err(&udev->dev, "host request flag is invalid\n");
453 dev_err(&udev->dev, "Get one byte OTG status failed\n");
458 EXPORT_SYMBOL_GPL(otg_hnp_polling);