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