usb: phy: Rename "B-device session end SRP" OTG FSM input
[firefly-linux-kernel-4.4.55.git] / drivers / usb / phy / phy-fsm-usb.c
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/spinlock.h>
27 #include <linux/delay.h>
28 #include <linux/usb.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/otg.h>
31
32 #include "phy-fsm-usb.h"
33
34 /* Change USB protocol when there is a protocol change */
35 static int otg_set_protocol(struct otg_fsm *fsm, int protocol)
36 {
37         int ret = 0;
38
39         if (fsm->protocol != protocol) {
40                 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n",
41                         fsm->protocol, protocol);
42                 /* stop old protocol */
43                 if (fsm->protocol == PROTO_HOST)
44                         ret = otg_start_host(fsm, 0);
45                 else if (fsm->protocol == PROTO_GADGET)
46                         ret = otg_start_gadget(fsm, 0);
47                 if (ret)
48                         return ret;
49
50                 /* start new protocol */
51                 if (protocol == PROTO_HOST)
52                         ret = otg_start_host(fsm, 1);
53                 else if (protocol == PROTO_GADGET)
54                         ret = otg_start_gadget(fsm, 1);
55                 if (ret)
56                         return ret;
57
58                 fsm->protocol = protocol;
59                 return 0;
60         }
61
62         return 0;
63 }
64
65 static int state_changed;
66
67 /* Called when leaving a state.  Do state clean up jobs here */
68 void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state)
69 {
70         switch (old_state) {
71         case OTG_STATE_B_IDLE:
72                 otg_del_timer(fsm, B_SE0_SRP);
73                 fsm->b_se0_srp = 0;
74                 break;
75         case OTG_STATE_B_SRP_INIT:
76                 fsm->b_srp_done = 0;
77                 break;
78         case OTG_STATE_B_PERIPHERAL:
79                 break;
80         case OTG_STATE_B_WAIT_ACON:
81                 otg_del_timer(fsm, B_ASE0_BRST);
82                 fsm->b_ase0_brst_tmout = 0;
83                 break;
84         case OTG_STATE_B_HOST:
85                 break;
86         case OTG_STATE_A_IDLE:
87                 break;
88         case OTG_STATE_A_WAIT_VRISE:
89                 otg_del_timer(fsm, A_WAIT_VRISE);
90                 fsm->a_wait_vrise_tmout = 0;
91                 break;
92         case OTG_STATE_A_WAIT_BCON:
93                 otg_del_timer(fsm, A_WAIT_BCON);
94                 fsm->a_wait_bcon_tmout = 0;
95                 break;
96         case OTG_STATE_A_HOST:
97                 otg_del_timer(fsm, A_WAIT_ENUM);
98                 break;
99         case OTG_STATE_A_SUSPEND:
100                 otg_del_timer(fsm, A_AIDL_BDIS);
101                 fsm->a_aidl_bdis_tmout = 0;
102                 fsm->a_suspend_req_inf = 0;
103                 break;
104         case OTG_STATE_A_PERIPHERAL:
105                 otg_del_timer(fsm, A_BIDL_ADIS);
106                 fsm->a_bidl_adis_tmout = 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
120 /* Called when entering a state */
121 int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
122 {
123         state_changed = 1;
124         if (fsm->otg->phy->state == new_state)
125                 return 0;
126         VDBG("Set state: %s\n", usb_otg_state_string(new_state));
127         otg_leave_state(fsm, fsm->otg->phy->state);
128         switch (new_state) {
129         case OTG_STATE_B_IDLE:
130                 otg_drv_vbus(fsm, 0);
131                 otg_chrg_vbus(fsm, 0);
132                 otg_loc_conn(fsm, 0);
133                 otg_loc_sof(fsm, 0);
134                 otg_set_protocol(fsm, PROTO_UNDEF);
135                 otg_add_timer(fsm, B_SE0_SRP);
136                 break;
137         case OTG_STATE_B_SRP_INIT:
138                 otg_start_pulse(fsm);
139                 otg_loc_sof(fsm, 0);
140                 otg_set_protocol(fsm, PROTO_UNDEF);
141                 otg_add_timer(fsm, B_SRP_FAIL);
142                 break;
143         case OTG_STATE_B_PERIPHERAL:
144                 otg_chrg_vbus(fsm, 0);
145                 otg_loc_conn(fsm, 1);
146                 otg_loc_sof(fsm, 0);
147                 otg_set_protocol(fsm, PROTO_GADGET);
148                 break;
149         case OTG_STATE_B_WAIT_ACON:
150                 otg_chrg_vbus(fsm, 0);
151                 otg_loc_conn(fsm, 0);
152                 otg_loc_sof(fsm, 0);
153                 otg_set_protocol(fsm, PROTO_HOST);
154                 otg_add_timer(fsm, B_ASE0_BRST);
155                 fsm->a_bus_suspend = 0;
156                 break;
157         case OTG_STATE_B_HOST:
158                 otg_chrg_vbus(fsm, 0);
159                 otg_loc_conn(fsm, 0);
160                 otg_loc_sof(fsm, 1);
161                 otg_set_protocol(fsm, PROTO_HOST);
162                 usb_bus_start_enum(fsm->otg->host,
163                                 fsm->otg->host->otg_port);
164                 break;
165         case OTG_STATE_A_IDLE:
166                 otg_drv_vbus(fsm, 0);
167                 otg_chrg_vbus(fsm, 0);
168                 otg_loc_conn(fsm, 0);
169                 otg_loc_sof(fsm, 0);
170                 otg_set_protocol(fsm, PROTO_HOST);
171                 break;
172         case OTG_STATE_A_WAIT_VRISE:
173                 otg_drv_vbus(fsm, 1);
174                 otg_loc_conn(fsm, 0);
175                 otg_loc_sof(fsm, 0);
176                 otg_set_protocol(fsm, PROTO_HOST);
177                 otg_add_timer(fsm, A_WAIT_VRISE);
178                 break;
179         case OTG_STATE_A_WAIT_BCON:
180                 otg_drv_vbus(fsm, 1);
181                 otg_loc_conn(fsm, 0);
182                 otg_loc_sof(fsm, 0);
183                 otg_set_protocol(fsm, PROTO_HOST);
184                 otg_add_timer(fsm, A_WAIT_BCON);
185                 break;
186         case OTG_STATE_A_HOST:
187                 otg_drv_vbus(fsm, 1);
188                 otg_loc_conn(fsm, 0);
189                 otg_loc_sof(fsm, 1);
190                 otg_set_protocol(fsm, PROTO_HOST);
191                 /*
192                  * When HNP is triggered while a_bus_req = 0, a_host will
193                  * suspend too fast to complete a_set_b_hnp_en
194                  */
195                 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
196                         otg_add_timer(fsm, A_WAIT_ENUM);
197                 break;
198         case OTG_STATE_A_SUSPEND:
199                 otg_drv_vbus(fsm, 1);
200                 otg_loc_conn(fsm, 0);
201                 otg_loc_sof(fsm, 0);
202                 otg_set_protocol(fsm, PROTO_HOST);
203                 otg_add_timer(fsm, A_AIDL_BDIS);
204
205                 break;
206         case OTG_STATE_A_PERIPHERAL:
207                 otg_loc_conn(fsm, 1);
208                 otg_loc_sof(fsm, 0);
209                 otg_set_protocol(fsm, PROTO_GADGET);
210                 otg_drv_vbus(fsm, 1);
211                 otg_add_timer(fsm, A_BIDL_ADIS);
212                 break;
213         case OTG_STATE_A_WAIT_VFALL:
214                 otg_drv_vbus(fsm, 0);
215                 otg_loc_conn(fsm, 0);
216                 otg_loc_sof(fsm, 0);
217                 otg_set_protocol(fsm, PROTO_HOST);
218                 otg_add_timer(fsm, A_WAIT_VFALL);
219                 break;
220         case OTG_STATE_A_VBUS_ERR:
221                 otg_drv_vbus(fsm, 0);
222                 otg_loc_conn(fsm, 0);
223                 otg_loc_sof(fsm, 0);
224                 otg_set_protocol(fsm, PROTO_UNDEF);
225                 break;
226         default:
227                 break;
228         }
229
230         fsm->otg->phy->state = new_state;
231         return 0;
232 }
233
234 /* State change judgement */
235 int otg_statemachine(struct otg_fsm *fsm)
236 {
237         enum usb_otg_state state;
238         unsigned long flags;
239
240         spin_lock_irqsave(&fsm->lock, flags);
241
242         state = fsm->otg->phy->state;
243         state_changed = 0;
244         /* State machine state change judgement */
245
246         switch (state) {
247         case OTG_STATE_UNDEFINED:
248                 VDBG("fsm->id = %d\n", fsm->id);
249                 if (fsm->id)
250                         otg_set_state(fsm, OTG_STATE_B_IDLE);
251                 else
252                         otg_set_state(fsm, OTG_STATE_A_IDLE);
253                 break;
254         case OTG_STATE_B_IDLE:
255                 if (!fsm->id)
256                         otg_set_state(fsm, OTG_STATE_A_IDLE);
257                 else if (fsm->b_sess_vld && fsm->otg->gadget)
258                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
259                 else if (fsm->b_bus_req && fsm->b_ssend_srp && fsm->b_se0_srp)
260                         otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
261                 break;
262         case OTG_STATE_B_SRP_INIT:
263                 if (!fsm->id || fsm->b_srp_done)
264                         otg_set_state(fsm, OTG_STATE_B_IDLE);
265                 break;
266         case OTG_STATE_B_PERIPHERAL:
267                 if (!fsm->id || !fsm->b_sess_vld)
268                         otg_set_state(fsm, OTG_STATE_B_IDLE);
269                 else if (fsm->b_bus_req && fsm->otg->
270                                 gadget->b_hnp_enable && fsm->a_bus_suspend)
271                         otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
272                 break;
273         case OTG_STATE_B_WAIT_ACON:
274                 if (fsm->a_conn)
275                         otg_set_state(fsm, OTG_STATE_B_HOST);
276                 else if (!fsm->id || !fsm->b_sess_vld)
277                         otg_set_state(fsm, OTG_STATE_B_IDLE);
278                 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
279                         fsm->b_ase0_brst_tmout = 0;
280                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
281                 }
282                 break;
283         case OTG_STATE_B_HOST:
284                 if (!fsm->id || !fsm->b_sess_vld)
285                         otg_set_state(fsm, OTG_STATE_B_IDLE);
286                 else if (!fsm->b_bus_req || !fsm->a_conn)
287                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
288                 break;
289         case OTG_STATE_A_IDLE:
290                 if (fsm->id)
291                         otg_set_state(fsm, OTG_STATE_B_IDLE);
292                 else if (!fsm->a_bus_drop && (fsm->a_bus_req || fsm->a_srp_det))
293                         otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
294                 break;
295         case OTG_STATE_A_WAIT_VRISE:
296                 if (fsm->id || fsm->a_bus_drop || fsm->a_vbus_vld ||
297                                 fsm->a_wait_vrise_tmout) {
298                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
299                 }
300                 break;
301         case OTG_STATE_A_WAIT_BCON:
302                 if (!fsm->a_vbus_vld)
303                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
304                 else if (fsm->b_conn)
305                         otg_set_state(fsm, OTG_STATE_A_HOST);
306                 else if (fsm->id | fsm->a_bus_drop | fsm->a_wait_bcon_tmout)
307                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
308                 break;
309         case OTG_STATE_A_HOST:
310                 if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
311                                 fsm->otg->host->b_hnp_enable)
312                         otg_set_state(fsm, OTG_STATE_A_SUSPEND);
313                 else if (fsm->id || !fsm->b_conn || fsm->a_bus_drop)
314                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
315                 else if (!fsm->a_vbus_vld)
316                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
317                 break;
318         case OTG_STATE_A_SUSPEND:
319                 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
320                         otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
321                 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
322                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
323                 else if (fsm->a_bus_req || fsm->b_bus_resume)
324                         otg_set_state(fsm, OTG_STATE_A_HOST);
325                 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
326                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
327                 else if (!fsm->a_vbus_vld)
328                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
329                 break;
330         case OTG_STATE_A_PERIPHERAL:
331                 if (fsm->id || fsm->a_bus_drop)
332                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
333                 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
334                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
335                 else if (!fsm->a_vbus_vld)
336                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
337                 break;
338         case OTG_STATE_A_WAIT_VFALL:
339                 if (fsm->a_wait_vfall_tmout || fsm->id || fsm->a_bus_req ||
340                                 (!fsm->a_sess_vld && !fsm->b_conn))
341                         otg_set_state(fsm, OTG_STATE_A_IDLE);
342                 break;
343         case OTG_STATE_A_VBUS_ERR:
344                 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
345                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
346                 break;
347         default:
348                 break;
349         }
350         spin_unlock_irqrestore(&fsm->lock, flags);
351
352         VDBG("quit statemachine, changed = %d\n", state_changed);
353         return state_changed;
354 }