xref: /linux/drivers/usb/common/usb-otg-fsm.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/usb.h>
30 #include <linux/usb/gadget.h>
31 #include <linux/usb/otg.h>
32 #include <linux/usb/otg-fsm.h>
33 
34 #ifdef VERBOSE
35 #define VDBG(fmt, args...) pr_debug("[%s]  " fmt, \
36 				 __func__, ## args)
37 #else
38 #define VDBG(stuff...)	do {} while (0)
39 #endif
40 
41 /* Change USB protocol when there is a protocol change */
42 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
43 {
44 	int ret = 0;
45 
46 	if (fsm->protocol != protocol) {
47 		VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
48 			fsm->protocol, protocol);
49 		/* stop old protocol */
50 		if (fsm->protocol == PROTO_HOST)
51 			ret = otg_start_host(fsm, 0);
52 		else if (fsm->protocol == PROTO_GADGET)
53 			ret = otg_start_gadget(fsm, 0);
54 		if (ret)
55 			return ret;
56 
57 		/* start new protocol */
58 		if (protocol == PROTO_HOST)
59 			ret = otg_start_host(fsm, 1);
60 		else if (protocol == PROTO_GADGET)
61 			ret = otg_start_gadget(fsm, 1);
62 		if (ret)
63 			return ret;
64 
65 		fsm->protocol = protocol;
66 		return 0;
67 	}
68 
69 	return 0;
70 }
71 
72 /* Called when leaving a state.  Do state clean up jobs here */
73 static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
74 {
75 	switch (old_state) {
76 	case OTG_STATE_B_IDLE:
77 		otg_del_timer(fsm, B_SE0_SRP);
78 		fsm->b_se0_srp = 0;
79 		fsm->adp_sns = 0;
80 		fsm->adp_prb = 0;
81 		break;
82 	case OTG_STATE_B_SRP_INIT:
83 		fsm->data_pulse = 0;
84 		fsm->b_srp_done = 0;
85 		break;
86 	case OTG_STATE_B_PERIPHERAL:
87 		if (fsm->otg->gadget)
88 			fsm->otg->gadget->host_request_flag = 0;
89 		break;
90 	case OTG_STATE_B_WAIT_ACON:
91 		otg_del_timer(fsm, B_ASE0_BRST);
92 		fsm->b_ase0_brst_tmout = 0;
93 		break;
94 	case OTG_STATE_B_HOST:
95 		break;
96 	case OTG_STATE_A_IDLE:
97 		fsm->adp_prb = 0;
98 		break;
99 	case OTG_STATE_A_WAIT_VRISE:
100 		otg_del_timer(fsm, A_WAIT_VRISE);
101 		fsm->a_wait_vrise_tmout = 0;
102 		break;
103 	case OTG_STATE_A_WAIT_BCON:
104 		otg_del_timer(fsm, A_WAIT_BCON);
105 		fsm->a_wait_bcon_tmout = 0;
106 		break;
107 	case OTG_STATE_A_HOST:
108 		otg_del_timer(fsm, A_WAIT_ENUM);
109 		break;
110 	case OTG_STATE_A_SUSPEND:
111 		otg_del_timer(fsm, A_AIDL_BDIS);
112 		fsm->a_aidl_bdis_tmout = 0;
113 		fsm->a_suspend_req_inf = 0;
114 		break;
115 	case OTG_STATE_A_PERIPHERAL:
116 		otg_del_timer(fsm, A_BIDL_ADIS);
117 		fsm->a_bidl_adis_tmout = 0;
118 		if (fsm->otg->gadget)
119 			fsm->otg->gadget->host_request_flag = 0;
120 		break;
121 	case OTG_STATE_A_WAIT_VFALL:
122 		otg_del_timer(fsm, A_WAIT_VFALL);
123 		fsm->a_wait_vfall_tmout = 0;
124 		otg_del_timer(fsm, A_WAIT_VRISE);
125 		break;
126 	case OTG_STATE_A_VBUS_ERR:
127 		break;
128 	default:
129 		break;
130 	}
131 }
132 
133 static void otg_hnp_polling_work(struct work_struct *work)
134 {
135 	struct otg_fsm *fsm = container_of(to_delayed_work(work),
136 				struct otg_fsm, hnp_polling_work);
137 	struct usb_device *udev;
138 	enum usb_otg_state state = fsm->otg->state;
139 	u8 flag;
140 	int retval;
141 
142 	if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST)
143 		return;
144 
145 	udev = usb_hub_find_child(fsm->otg->host->root_hub, 1);
146 	if (!udev) {
147 		dev_err(fsm->otg->host->controller,
148 			"no usb dev connected, can't start HNP polling\n");
149 		return;
150 	}
151 
152 	*fsm->host_req_flag = 0;
153 	/* Get host request flag from connected USB device */
154 	retval = usb_control_msg(udev,
155 				usb_rcvctrlpipe(udev, 0),
156 				USB_REQ_GET_STATUS,
157 				USB_DIR_IN | USB_RECIP_DEVICE,
158 				0,
159 				OTG_STS_SELECTOR,
160 				fsm->host_req_flag,
161 				1,
162 				USB_CTRL_GET_TIMEOUT);
163 	if (retval != 1) {
164 		dev_err(&udev->dev, "Get one byte OTG status failed\n");
165 		return;
166 	}
167 
168 	flag = *fsm->host_req_flag;
169 	if (flag == 0) {
170 		/* Continue HNP polling */
171 		schedule_delayed_work(&fsm->hnp_polling_work,
172 					msecs_to_jiffies(T_HOST_REQ_POLL));
173 		return;
174 	} else if (flag != HOST_REQUEST_FLAG) {
175 		dev_err(&udev->dev, "host request flag %d is invalid\n", flag);
176 		return;
177 	}
178 
179 	/* Host request flag is set */
180 	if (state == OTG_STATE_A_HOST) {
181 		/* Set b_hnp_enable */
182 		if (!fsm->otg->host->b_hnp_enable) {
183 			retval = usb_control_msg(udev,
184 					usb_sndctrlpipe(udev, 0),
185 					USB_REQ_SET_FEATURE, 0,
186 					USB_DEVICE_B_HNP_ENABLE,
187 					0, NULL, 0,
188 					USB_CTRL_SET_TIMEOUT);
189 			if (retval >= 0)
190 				fsm->otg->host->b_hnp_enable = 1;
191 		}
192 		fsm->a_bus_req = 0;
193 	} else if (state == OTG_STATE_B_HOST) {
194 		fsm->b_bus_req = 0;
195 	}
196 
197 	otg_statemachine(fsm);
198 }
199 
200 static void otg_start_hnp_polling(struct otg_fsm *fsm)
201 {
202 	/*
203 	 * The memory of host_req_flag should be allocated by
204 	 * controller driver, otherwise, hnp polling is not started.
205 	 */
206 	if (!fsm->host_req_flag)
207 		return;
208 
209 	INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
210 	schedule_delayed_work(&fsm->hnp_polling_work,
211 					msecs_to_jiffies(T_HOST_REQ_POLL));
212 }
213 
214 /* Called when entering a state */
215 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
216 {
217 	if (fsm->otg->state == new_state)
218 		return 0;
219 	VDBG("Set state: %s\n", usb_otg_state_string(new_state));
220 	otg_leave_state(fsm, fsm->otg->state);
221 	switch (new_state) {
222 	case OTG_STATE_B_IDLE:
223 		otg_drv_vbus(fsm, 0);
224 		otg_chrg_vbus(fsm, 0);
225 		otg_loc_conn(fsm, 0);
226 		otg_loc_sof(fsm, 0);
227 		/*
228 		 * Driver is responsible for starting ADP probing
229 		 * if ADP sensing times out.
230 		 */
231 		otg_start_adp_sns(fsm);
232 		otg_set_protocol(fsm, PROTO_UNDEF);
233 		otg_add_timer(fsm, B_SE0_SRP);
234 		break;
235 	case OTG_STATE_B_SRP_INIT:
236 		otg_start_pulse(fsm);
237 		otg_loc_sof(fsm, 0);
238 		otg_set_protocol(fsm, PROTO_UNDEF);
239 		otg_add_timer(fsm, B_SRP_FAIL);
240 		break;
241 	case OTG_STATE_B_PERIPHERAL:
242 		otg_chrg_vbus(fsm, 0);
243 		otg_loc_sof(fsm, 0);
244 		otg_set_protocol(fsm, PROTO_GADGET);
245 		otg_loc_conn(fsm, 1);
246 		break;
247 	case OTG_STATE_B_WAIT_ACON:
248 		otg_chrg_vbus(fsm, 0);
249 		otg_loc_conn(fsm, 0);
250 		otg_loc_sof(fsm, 0);
251 		otg_set_protocol(fsm, PROTO_HOST);
252 		otg_add_timer(fsm, B_ASE0_BRST);
253 		fsm->a_bus_suspend = 0;
254 		break;
255 	case OTG_STATE_B_HOST:
256 		otg_chrg_vbus(fsm, 0);
257 		otg_loc_conn(fsm, 0);
258 		otg_loc_sof(fsm, 1);
259 		otg_set_protocol(fsm, PROTO_HOST);
260 		usb_bus_start_enum(fsm->otg->host,
261 				fsm->otg->host->otg_port);
262 		otg_start_hnp_polling(fsm);
263 		break;
264 	case OTG_STATE_A_IDLE:
265 		otg_drv_vbus(fsm, 0);
266 		otg_chrg_vbus(fsm, 0);
267 		otg_loc_conn(fsm, 0);
268 		otg_loc_sof(fsm, 0);
269 		otg_start_adp_prb(fsm);
270 		otg_set_protocol(fsm, PROTO_HOST);
271 		break;
272 	case OTG_STATE_A_WAIT_VRISE:
273 		otg_drv_vbus(fsm, 1);
274 		otg_loc_conn(fsm, 0);
275 		otg_loc_sof(fsm, 0);
276 		otg_set_protocol(fsm, PROTO_HOST);
277 		otg_add_timer(fsm, A_WAIT_VRISE);
278 		break;
279 	case OTG_STATE_A_WAIT_BCON:
280 		otg_drv_vbus(fsm, 1);
281 		otg_loc_conn(fsm, 0);
282 		otg_loc_sof(fsm, 0);
283 		otg_set_protocol(fsm, PROTO_HOST);
284 		otg_add_timer(fsm, A_WAIT_BCON);
285 		break;
286 	case OTG_STATE_A_HOST:
287 		otg_drv_vbus(fsm, 1);
288 		otg_loc_conn(fsm, 0);
289 		otg_loc_sof(fsm, 1);
290 		otg_set_protocol(fsm, PROTO_HOST);
291 		/*
292 		 * When HNP is triggered while a_bus_req = 0, a_host will
293 		 * suspend too fast to complete a_set_b_hnp_en
294 		 */
295 		if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
296 			otg_add_timer(fsm, A_WAIT_ENUM);
297 		otg_start_hnp_polling(fsm);
298 		break;
299 	case OTG_STATE_A_SUSPEND:
300 		otg_drv_vbus(fsm, 1);
301 		otg_loc_conn(fsm, 0);
302 		otg_loc_sof(fsm, 0);
303 		otg_set_protocol(fsm, PROTO_HOST);
304 		otg_add_timer(fsm, A_AIDL_BDIS);
305 
306 		break;
307 	case OTG_STATE_A_PERIPHERAL:
308 		otg_loc_sof(fsm, 0);
309 		otg_set_protocol(fsm, PROTO_GADGET);
310 		otg_drv_vbus(fsm, 1);
311 		otg_loc_conn(fsm, 1);
312 		otg_add_timer(fsm, A_BIDL_ADIS);
313 		break;
314 	case OTG_STATE_A_WAIT_VFALL:
315 		otg_drv_vbus(fsm, 0);
316 		otg_loc_conn(fsm, 0);
317 		otg_loc_sof(fsm, 0);
318 		otg_set_protocol(fsm, PROTO_HOST);
319 		otg_add_timer(fsm, A_WAIT_VFALL);
320 		break;
321 	case OTG_STATE_A_VBUS_ERR:
322 		otg_drv_vbus(fsm, 0);
323 		otg_loc_conn(fsm, 0);
324 		otg_loc_sof(fsm, 0);
325 		otg_set_protocol(fsm, PROTO_UNDEF);
326 		break;
327 	default:
328 		break;
329 	}
330 
331 	fsm->otg->state = new_state;
332 	fsm->state_changed = 1;
333 	return 0;
334 }
335 
336 /* State change judgement */
337 int otg_statemachine(struct otg_fsm *fsm)
338 {
339 	enum usb_otg_state state;
340 
341 	mutex_lock(&fsm->lock);
342 
343 	state = fsm->otg->state;
344 	fsm->state_changed = 0;
345 	/* State machine state change judgement */
346 
347 	switch (state) {
348 	case OTG_STATE_UNDEFINED:
349 		VDBG("fsm->id = %d\n", fsm->id);
350 		if (fsm->id)
351 			otg_set_state(fsm, OTG_STATE_B_IDLE);
352 		else
353 			otg_set_state(fsm, OTG_STATE_A_IDLE);
354 		break;
355 	case OTG_STATE_B_IDLE:
356 		if (!fsm->id)
357 			otg_set_state(fsm, OTG_STATE_A_IDLE);
358 		else if (fsm->b_sess_vld && fsm->otg->gadget)
359 			otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
360 		else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
361 				fsm->b_ssend_srp && fsm->b_se0_srp)
362 			otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
363 		break;
364 	case OTG_STATE_B_SRP_INIT:
365 		if (!fsm->id || fsm->b_srp_done)
366 			otg_set_state(fsm, OTG_STATE_B_IDLE);
367 		break;
368 	case OTG_STATE_B_PERIPHERAL:
369 		if (!fsm->id || !fsm->b_sess_vld)
370 			otg_set_state(fsm, OTG_STATE_B_IDLE);
371 		else if (fsm->b_bus_req && fsm->otg->
372 				gadget->b_hnp_enable && fsm->a_bus_suspend)
373 			otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
374 		break;
375 	case OTG_STATE_B_WAIT_ACON:
376 		if (fsm->a_conn)
377 			otg_set_state(fsm, OTG_STATE_B_HOST);
378 		else if (!fsm->id || !fsm->b_sess_vld)
379 			otg_set_state(fsm, OTG_STATE_B_IDLE);
380 		else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
381 			fsm->b_ase0_brst_tmout = 0;
382 			otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
383 		}
384 		break;
385 	case OTG_STATE_B_HOST:
386 		if (!fsm->id || !fsm->b_sess_vld)
387 			otg_set_state(fsm, OTG_STATE_B_IDLE);
388 		else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
389 			otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
390 		break;
391 	case OTG_STATE_A_IDLE:
392 		if (fsm->id)
393 			otg_set_state(fsm, OTG_STATE_B_IDLE);
394 		else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
395 			  fsm->a_srp_det || fsm->adp_change || fsm->power_up))
396 			otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
397 		break;
398 	case OTG_STATE_A_WAIT_VRISE:
399 		if (fsm->a_vbus_vld)
400 			otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
401 		else if (fsm->id || fsm->a_bus_drop ||
402 				fsm->a_wait_vrise_tmout)
403 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
404 		break;
405 	case OTG_STATE_A_WAIT_BCON:
406 		if (!fsm->a_vbus_vld)
407 			otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
408 		else if (fsm->b_conn)
409 			otg_set_state(fsm, OTG_STATE_A_HOST);
410 		else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
411 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
412 		break;
413 	case OTG_STATE_A_HOST:
414 		if (fsm->id || fsm->a_bus_drop)
415 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
416 		else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
417 				fsm->otg->host->b_hnp_enable)
418 			otg_set_state(fsm, OTG_STATE_A_SUSPEND);
419 		else if (!fsm->b_conn)
420 			otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
421 		else if (!fsm->a_vbus_vld)
422 			otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
423 		break;
424 	case OTG_STATE_A_SUSPEND:
425 		if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
426 			otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
427 		else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
428 			otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
429 		else if (fsm->a_bus_req || fsm->b_bus_resume)
430 			otg_set_state(fsm, OTG_STATE_A_HOST);
431 		else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
432 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
433 		else if (!fsm->a_vbus_vld)
434 			otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
435 		break;
436 	case OTG_STATE_A_PERIPHERAL:
437 		if (fsm->id || fsm->a_bus_drop)
438 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
439 		else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
440 			otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
441 		else if (!fsm->a_vbus_vld)
442 			otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
443 		break;
444 	case OTG_STATE_A_WAIT_VFALL:
445 		if (fsm->a_wait_vfall_tmout)
446 			otg_set_state(fsm, OTG_STATE_A_IDLE);
447 		break;
448 	case OTG_STATE_A_VBUS_ERR:
449 		if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
450 			otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
451 		break;
452 	default:
453 		break;
454 	}
455 	mutex_unlock(&fsm->lock);
456 
457 	VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
458 	return fsm->state_changed;
459 }
460 EXPORT_SYMBOL_GPL(otg_statemachine);
461 MODULE_LICENSE("GPL");
462