GNU Linux-libre 4.14.266-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 #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         if (!fsm->hnp_work_inited) {
210                 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work);
211                 fsm->hnp_work_inited = true;
212         }
213
214         schedule_delayed_work(&fsm->hnp_polling_work,
215                                         msecs_to_jiffies(T_HOST_REQ_POLL));
216 }
217
218 /* Called when entering a state */
219 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state)
220 {
221         if (fsm->otg->state == new_state)
222                 return 0;
223         VDBG("Set state: %s\n", usb_otg_state_string(new_state));
224         otg_leave_state(fsm, fsm->otg->state);
225         switch (new_state) {
226         case OTG_STATE_B_IDLE:
227                 otg_drv_vbus(fsm, 0);
228                 otg_chrg_vbus(fsm, 0);
229                 otg_loc_conn(fsm, 0);
230                 otg_loc_sof(fsm, 0);
231                 /*
232                  * Driver is responsible for starting ADP probing
233                  * if ADP sensing times out.
234                  */
235                 otg_start_adp_sns(fsm);
236                 otg_set_protocol(fsm, PROTO_UNDEF);
237                 otg_add_timer(fsm, B_SE0_SRP);
238                 break;
239         case OTG_STATE_B_SRP_INIT:
240                 otg_start_pulse(fsm);
241                 otg_loc_sof(fsm, 0);
242                 otg_set_protocol(fsm, PROTO_UNDEF);
243                 otg_add_timer(fsm, B_SRP_FAIL);
244                 break;
245         case OTG_STATE_B_PERIPHERAL:
246                 otg_chrg_vbus(fsm, 0);
247                 otg_loc_sof(fsm, 0);
248                 otg_set_protocol(fsm, PROTO_GADGET);
249                 otg_loc_conn(fsm, 1);
250                 break;
251         case OTG_STATE_B_WAIT_ACON:
252                 otg_chrg_vbus(fsm, 0);
253                 otg_loc_conn(fsm, 0);
254                 otg_loc_sof(fsm, 0);
255                 otg_set_protocol(fsm, PROTO_HOST);
256                 otg_add_timer(fsm, B_ASE0_BRST);
257                 fsm->a_bus_suspend = 0;
258                 break;
259         case OTG_STATE_B_HOST:
260                 otg_chrg_vbus(fsm, 0);
261                 otg_loc_conn(fsm, 0);
262                 otg_loc_sof(fsm, 1);
263                 otg_set_protocol(fsm, PROTO_HOST);
264                 usb_bus_start_enum(fsm->otg->host,
265                                 fsm->otg->host->otg_port);
266                 otg_start_hnp_polling(fsm);
267                 break;
268         case OTG_STATE_A_IDLE:
269                 otg_drv_vbus(fsm, 0);
270                 otg_chrg_vbus(fsm, 0);
271                 otg_loc_conn(fsm, 0);
272                 otg_loc_sof(fsm, 0);
273                 otg_start_adp_prb(fsm);
274                 otg_set_protocol(fsm, PROTO_HOST);
275                 break;
276         case OTG_STATE_A_WAIT_VRISE:
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_VRISE);
282                 break;
283         case OTG_STATE_A_WAIT_BCON:
284                 otg_drv_vbus(fsm, 1);
285                 otg_loc_conn(fsm, 0);
286                 otg_loc_sof(fsm, 0);
287                 otg_set_protocol(fsm, PROTO_HOST);
288                 otg_add_timer(fsm, A_WAIT_BCON);
289                 break;
290         case OTG_STATE_A_HOST:
291                 otg_drv_vbus(fsm, 1);
292                 otg_loc_conn(fsm, 0);
293                 otg_loc_sof(fsm, 1);
294                 otg_set_protocol(fsm, PROTO_HOST);
295                 /*
296                  * When HNP is triggered while a_bus_req = 0, a_host will
297                  * suspend too fast to complete a_set_b_hnp_en
298                  */
299                 if (!fsm->a_bus_req || fsm->a_suspend_req_inf)
300                         otg_add_timer(fsm, A_WAIT_ENUM);
301                 otg_start_hnp_polling(fsm);
302                 break;
303         case OTG_STATE_A_SUSPEND:
304                 otg_drv_vbus(fsm, 1);
305                 otg_loc_conn(fsm, 0);
306                 otg_loc_sof(fsm, 0);
307                 otg_set_protocol(fsm, PROTO_HOST);
308                 otg_add_timer(fsm, A_AIDL_BDIS);
309
310                 break;
311         case OTG_STATE_A_PERIPHERAL:
312                 otg_loc_sof(fsm, 0);
313                 otg_set_protocol(fsm, PROTO_GADGET);
314                 otg_drv_vbus(fsm, 1);
315                 otg_loc_conn(fsm, 1);
316                 otg_add_timer(fsm, A_BIDL_ADIS);
317                 break;
318         case OTG_STATE_A_WAIT_VFALL:
319                 otg_drv_vbus(fsm, 0);
320                 otg_loc_conn(fsm, 0);
321                 otg_loc_sof(fsm, 0);
322                 otg_set_protocol(fsm, PROTO_HOST);
323                 otg_add_timer(fsm, A_WAIT_VFALL);
324                 break;
325         case OTG_STATE_A_VBUS_ERR:
326                 otg_drv_vbus(fsm, 0);
327                 otg_loc_conn(fsm, 0);
328                 otg_loc_sof(fsm, 0);
329                 otg_set_protocol(fsm, PROTO_UNDEF);
330                 break;
331         default:
332                 break;
333         }
334
335         fsm->otg->state = new_state;
336         fsm->state_changed = 1;
337         return 0;
338 }
339
340 /* State change judgement */
341 int otg_statemachine(struct otg_fsm *fsm)
342 {
343         enum usb_otg_state state;
344
345         mutex_lock(&fsm->lock);
346
347         state = fsm->otg->state;
348         fsm->state_changed = 0;
349         /* State machine state change judgement */
350
351         switch (state) {
352         case OTG_STATE_UNDEFINED:
353                 VDBG("fsm->id = %d\n", fsm->id);
354                 if (fsm->id)
355                         otg_set_state(fsm, OTG_STATE_B_IDLE);
356                 else
357                         otg_set_state(fsm, OTG_STATE_A_IDLE);
358                 break;
359         case OTG_STATE_B_IDLE:
360                 if (!fsm->id)
361                         otg_set_state(fsm, OTG_STATE_A_IDLE);
362                 else if (fsm->b_sess_vld && fsm->otg->gadget)
363                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
364                 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) &&
365                                 fsm->b_ssend_srp && fsm->b_se0_srp)
366                         otg_set_state(fsm, OTG_STATE_B_SRP_INIT);
367                 break;
368         case OTG_STATE_B_SRP_INIT:
369                 if (!fsm->id || fsm->b_srp_done)
370                         otg_set_state(fsm, OTG_STATE_B_IDLE);
371                 break;
372         case OTG_STATE_B_PERIPHERAL:
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->otg->
376                                 gadget->b_hnp_enable && fsm->a_bus_suspend)
377                         otg_set_state(fsm, OTG_STATE_B_WAIT_ACON);
378                 break;
379         case OTG_STATE_B_WAIT_ACON:
380                 if (fsm->a_conn)
381                         otg_set_state(fsm, OTG_STATE_B_HOST);
382                 else if (!fsm->id || !fsm->b_sess_vld)
383                         otg_set_state(fsm, OTG_STATE_B_IDLE);
384                 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) {
385                         fsm->b_ase0_brst_tmout = 0;
386                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
387                 }
388                 break;
389         case OTG_STATE_B_HOST:
390                 if (!fsm->id || !fsm->b_sess_vld)
391                         otg_set_state(fsm, OTG_STATE_B_IDLE);
392                 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device)
393                         otg_set_state(fsm, OTG_STATE_B_PERIPHERAL);
394                 break;
395         case OTG_STATE_A_IDLE:
396                 if (fsm->id)
397                         otg_set_state(fsm, OTG_STATE_B_IDLE);
398                 else if (!fsm->a_bus_drop && (fsm->a_bus_req ||
399                           fsm->a_srp_det || fsm->adp_change || fsm->power_up))
400                         otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE);
401                 break;
402         case OTG_STATE_A_WAIT_VRISE:
403                 if (fsm->a_vbus_vld)
404                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
405                 else if (fsm->id || fsm->a_bus_drop ||
406                                 fsm->a_wait_vrise_tmout)
407                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
408                 break;
409         case OTG_STATE_A_WAIT_BCON:
410                 if (!fsm->a_vbus_vld)
411                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
412                 else if (fsm->b_conn)
413                         otg_set_state(fsm, OTG_STATE_A_HOST);
414                 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout)
415                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
416                 break;
417         case OTG_STATE_A_HOST:
418                 if (fsm->id || fsm->a_bus_drop)
419                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
420                 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) &&
421                                 fsm->otg->host->b_hnp_enable)
422                         otg_set_state(fsm, OTG_STATE_A_SUSPEND);
423                 else if (!fsm->b_conn)
424                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
425                 else if (!fsm->a_vbus_vld)
426                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
427                 break;
428         case OTG_STATE_A_SUSPEND:
429                 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable)
430                         otg_set_state(fsm, OTG_STATE_A_PERIPHERAL);
431                 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable)
432                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
433                 else if (fsm->a_bus_req || fsm->b_bus_resume)
434                         otg_set_state(fsm, OTG_STATE_A_HOST);
435                 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout)
436                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
437                 else if (!fsm->a_vbus_vld)
438                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
439                 break;
440         case OTG_STATE_A_PERIPHERAL:
441                 if (fsm->id || fsm->a_bus_drop)
442                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
443                 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend)
444                         otg_set_state(fsm, OTG_STATE_A_WAIT_BCON);
445                 else if (!fsm->a_vbus_vld)
446                         otg_set_state(fsm, OTG_STATE_A_VBUS_ERR);
447                 break;
448         case OTG_STATE_A_WAIT_VFALL:
449                 if (fsm->a_wait_vfall_tmout)
450                         otg_set_state(fsm, OTG_STATE_A_IDLE);
451                 break;
452         case OTG_STATE_A_VBUS_ERR:
453                 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err)
454                         otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL);
455                 break;
456         default:
457                 break;
458         }
459         mutex_unlock(&fsm->lock);
460
461         VDBG("quit statemachine, changed = %d\n", fsm->state_changed);
462         return fsm->state_changed;
463 }
464 EXPORT_SYMBOL_GPL(otg_statemachine);
465 MODULE_LICENSE("GPL");