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