Linux Audio

Check our new training course

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