1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it
5*4882a593Smuzhiyun * under the terms of the GNU General Public License as published by the
6*4882a593Smuzhiyun * Free Software Foundation; either version 2 of the License, or (at your
7*4882a593Smuzhiyun * option) any later version.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
10*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
11*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12*4882a593Smuzhiyun * General Public License for more details.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License along
15*4882a593Smuzhiyun * with this program; if not, write to the Free Software Foundation, Inc.,
16*4882a593Smuzhiyun * 675 Mass Ave, Cambridge, MA 02139, USA.
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #ifndef __LINUX_USB_OTG_FSM_H
20*4882a593Smuzhiyun #define __LINUX_USB_OTG_FSM_H
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/mutex.h>
23*4882a593Smuzhiyun #include <linux/errno.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define PROTO_UNDEF (0)
26*4882a593Smuzhiyun #define PROTO_HOST (1)
27*4882a593Smuzhiyun #define PROTO_GADGET (2)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define OTG_STS_SELECTOR 0xF000 /* OTG status selector, according to
30*4882a593Smuzhiyun * OTG and EH 2.0 Chapter 6.2.3
31*4882a593Smuzhiyun * Table:6-4
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define HOST_REQUEST_FLAG 1 /* Host request flag, according to
35*4882a593Smuzhiyun * OTG and EH 2.0 Charpter 6.2.3
36*4882a593Smuzhiyun * Table:6-5
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define T_HOST_REQ_POLL (1500) /* 1500ms, HNP polling interval */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun enum otg_fsm_timer {
42*4882a593Smuzhiyun /* Standard OTG timers */
43*4882a593Smuzhiyun A_WAIT_VRISE,
44*4882a593Smuzhiyun A_WAIT_VFALL,
45*4882a593Smuzhiyun A_WAIT_BCON,
46*4882a593Smuzhiyun A_AIDL_BDIS,
47*4882a593Smuzhiyun B_ASE0_BRST,
48*4882a593Smuzhiyun A_BIDL_ADIS,
49*4882a593Smuzhiyun B_AIDL_BDIS,
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* Auxiliary timers */
52*4882a593Smuzhiyun B_SE0_SRP,
53*4882a593Smuzhiyun B_SRP_FAIL,
54*4882a593Smuzhiyun A_WAIT_ENUM,
55*4882a593Smuzhiyun B_DATA_PLS,
56*4882a593Smuzhiyun B_SSEND_SRP,
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun NUM_OTG_FSM_TIMERS,
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun * struct otg_fsm - OTG state machine according to the OTG spec
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * OTG hardware Inputs
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * Common inputs for A and B device
67*4882a593Smuzhiyun * @id: TRUE for B-device, FALSE for A-device.
68*4882a593Smuzhiyun * @adp_change: TRUE when current ADP measurement (n) value, compared to the
69*4882a593Smuzhiyun * ADP measurement taken at n-2, differs by more than CADP_THR
70*4882a593Smuzhiyun * @power_up: TRUE when the OTG device first powers up its USB system and
71*4882a593Smuzhiyun * ADP measurement taken if ADP capable
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * A-Device state inputs
74*4882a593Smuzhiyun * @a_srp_det: TRUE if the A-device detects SRP
75*4882a593Smuzhiyun * @a_vbus_vld: TRUE when VBUS voltage is in regulation
76*4882a593Smuzhiyun * @b_conn: TRUE if the A-device detects connection from the B-device
77*4882a593Smuzhiyun * @a_bus_resume: TRUE when the B-device detects that the A-device is signaling
78*4882a593Smuzhiyun * a resume (K state)
79*4882a593Smuzhiyun * B-Device state inputs
80*4882a593Smuzhiyun * @a_bus_suspend: TRUE when the B-device detects that the A-device has put the
81*4882a593Smuzhiyun * bus into suspend
82*4882a593Smuzhiyun * @a_conn: TRUE if the B-device detects a connection from the A-device
83*4882a593Smuzhiyun * @b_se0_srp: TRUE when the line has been at SE0 for more than the minimum
84*4882a593Smuzhiyun * time before generating SRP
85*4882a593Smuzhiyun * @b_ssend_srp: TRUE when the VBUS has been below VOTG_SESS_VLD for more than
86*4882a593Smuzhiyun * the minimum time before generating SRP
87*4882a593Smuzhiyun * @b_sess_vld: TRUE when the B-device detects that the voltage on VBUS is
88*4882a593Smuzhiyun * above VOTG_SESS_VLD
89*4882a593Smuzhiyun * @test_device: TRUE when the B-device switches to B-Host and detects an OTG
90*4882a593Smuzhiyun * test device. This must be set by host/hub driver
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * Application inputs (A-Device)
93*4882a593Smuzhiyun * @a_bus_drop: TRUE when A-device application needs to power down the bus
94*4882a593Smuzhiyun * @a_bus_req: TRUE when A-device application wants to use the bus.
95*4882a593Smuzhiyun * FALSE to suspend the bus
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * Application inputs (B-Device)
98*4882a593Smuzhiyun * @b_bus_req: TRUE during the time that the Application running on the
99*4882a593Smuzhiyun * B-device wants to use the bus
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * Auxilary inputs (OTG v1.3 only. Obsolete now.)
102*4882a593Smuzhiyun * @a_sess_vld: TRUE if the A-device detects that VBUS is above VA_SESS_VLD
103*4882a593Smuzhiyun * @b_bus_suspend: TRUE when the A-device detects that the B-device has put
104*4882a593Smuzhiyun * the bus into suspend
105*4882a593Smuzhiyun * @b_bus_resume: TRUE when the A-device detects that the B-device is signaling
106*4882a593Smuzhiyun * resume on the bus
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * OTG Output status. Read only for users. Updated by OTG FSM helpers defined
109*4882a593Smuzhiyun * in this file
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * Outputs for Both A and B device
112*4882a593Smuzhiyun * @drv_vbus: TRUE when A-device is driving VBUS
113*4882a593Smuzhiyun * @loc_conn: TRUE when the local device has signaled that it is connected
114*4882a593Smuzhiyun * to the bus
115*4882a593Smuzhiyun * @loc_sof: TRUE when the local device is generating activity on the bus
116*4882a593Smuzhiyun * @adp_prb: TRUE when the local device is in the process of doing
117*4882a593Smuzhiyun * ADP probing
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * Outputs for B-device state
120*4882a593Smuzhiyun * @adp_sns: TRUE when the B-device is in the process of carrying out
121*4882a593Smuzhiyun * ADP sensing
122*4882a593Smuzhiyun * @data_pulse: TRUE when the B-device is performing data line pulsing
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * Internal Variables
125*4882a593Smuzhiyun *
126*4882a593Smuzhiyun * a_set_b_hnp_en: TRUE when the A-device has successfully set the
127*4882a593Smuzhiyun * b_hnp_enable bit in the B-device.
128*4882a593Smuzhiyun * Unused as OTG fsm uses otg->host->b_hnp_enable instead
129*4882a593Smuzhiyun * b_srp_done: TRUE when the B-device has completed initiating SRP
130*4882a593Smuzhiyun * b_hnp_enable: TRUE when the B-device has accepted the
131*4882a593Smuzhiyun * SetFeature(b_hnp_enable) B-device.
132*4882a593Smuzhiyun * Unused as OTG fsm uses otg->gadget->b_hnp_enable instead
133*4882a593Smuzhiyun * a_clr_err: Asserted (by application ?) to clear a_vbus_err due to an
134*4882a593Smuzhiyun * overcurrent condition and causes the A-device to transition
135*4882a593Smuzhiyun * to a_wait_vfall
136*4882a593Smuzhiyun */
137*4882a593Smuzhiyun struct otg_fsm {
138*4882a593Smuzhiyun /* Input */
139*4882a593Smuzhiyun int id;
140*4882a593Smuzhiyun int adp_change;
141*4882a593Smuzhiyun int power_up;
142*4882a593Smuzhiyun int a_srp_det;
143*4882a593Smuzhiyun int a_vbus_vld;
144*4882a593Smuzhiyun int b_conn;
145*4882a593Smuzhiyun int a_bus_resume;
146*4882a593Smuzhiyun int a_bus_suspend;
147*4882a593Smuzhiyun int a_conn;
148*4882a593Smuzhiyun int b_se0_srp;
149*4882a593Smuzhiyun int b_ssend_srp;
150*4882a593Smuzhiyun int b_sess_vld;
151*4882a593Smuzhiyun int test_device;
152*4882a593Smuzhiyun int a_bus_drop;
153*4882a593Smuzhiyun int a_bus_req;
154*4882a593Smuzhiyun int b_bus_req;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* Auxilary inputs */
157*4882a593Smuzhiyun int a_sess_vld;
158*4882a593Smuzhiyun int b_bus_resume;
159*4882a593Smuzhiyun int b_bus_suspend;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Output */
162*4882a593Smuzhiyun int drv_vbus;
163*4882a593Smuzhiyun int loc_conn;
164*4882a593Smuzhiyun int loc_sof;
165*4882a593Smuzhiyun int adp_prb;
166*4882a593Smuzhiyun int adp_sns;
167*4882a593Smuzhiyun int data_pulse;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* Internal variables */
170*4882a593Smuzhiyun int a_set_b_hnp_en;
171*4882a593Smuzhiyun int b_srp_done;
172*4882a593Smuzhiyun int b_hnp_enable;
173*4882a593Smuzhiyun int a_clr_err;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* Informative variables. All unused as of now */
176*4882a593Smuzhiyun int a_bus_drop_inf;
177*4882a593Smuzhiyun int a_bus_req_inf;
178*4882a593Smuzhiyun int a_clr_err_inf;
179*4882a593Smuzhiyun int b_bus_req_inf;
180*4882a593Smuzhiyun /* Auxilary informative variables */
181*4882a593Smuzhiyun int a_suspend_req_inf;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /* Timeout indicator for timers */
184*4882a593Smuzhiyun int a_wait_vrise_tmout;
185*4882a593Smuzhiyun int a_wait_vfall_tmout;
186*4882a593Smuzhiyun int a_wait_bcon_tmout;
187*4882a593Smuzhiyun int a_aidl_bdis_tmout;
188*4882a593Smuzhiyun int b_ase0_brst_tmout;
189*4882a593Smuzhiyun int a_bidl_adis_tmout;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun struct otg_fsm_ops *ops;
192*4882a593Smuzhiyun struct usb_otg *otg;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Current usb protocol used: 0:undefine; 1:host; 2:client */
195*4882a593Smuzhiyun int protocol;
196*4882a593Smuzhiyun struct mutex lock;
197*4882a593Smuzhiyun u8 *host_req_flag;
198*4882a593Smuzhiyun struct delayed_work hnp_polling_work;
199*4882a593Smuzhiyun bool hnp_work_inited;
200*4882a593Smuzhiyun bool state_changed;
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun struct otg_fsm_ops {
204*4882a593Smuzhiyun void (*chrg_vbus)(struct otg_fsm *fsm, int on);
205*4882a593Smuzhiyun void (*drv_vbus)(struct otg_fsm *fsm, int on);
206*4882a593Smuzhiyun void (*loc_conn)(struct otg_fsm *fsm, int on);
207*4882a593Smuzhiyun void (*loc_sof)(struct otg_fsm *fsm, int on);
208*4882a593Smuzhiyun void (*start_pulse)(struct otg_fsm *fsm);
209*4882a593Smuzhiyun void (*start_adp_prb)(struct otg_fsm *fsm);
210*4882a593Smuzhiyun void (*start_adp_sns)(struct otg_fsm *fsm);
211*4882a593Smuzhiyun void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
212*4882a593Smuzhiyun void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
213*4882a593Smuzhiyun int (*start_host)(struct otg_fsm *fsm, int on);
214*4882a593Smuzhiyun int (*start_gadget)(struct otg_fsm *fsm, int on);
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun
otg_chrg_vbus(struct otg_fsm * fsm,int on)218*4882a593Smuzhiyun static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun if (!fsm->ops->chrg_vbus)
221*4882a593Smuzhiyun return -EOPNOTSUPP;
222*4882a593Smuzhiyun fsm->ops->chrg_vbus(fsm, on);
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
otg_drv_vbus(struct otg_fsm * fsm,int on)226*4882a593Smuzhiyun static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun if (!fsm->ops->drv_vbus)
229*4882a593Smuzhiyun return -EOPNOTSUPP;
230*4882a593Smuzhiyun if (fsm->drv_vbus != on) {
231*4882a593Smuzhiyun fsm->drv_vbus = on;
232*4882a593Smuzhiyun fsm->ops->drv_vbus(fsm, on);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun return 0;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
otg_loc_conn(struct otg_fsm * fsm,int on)237*4882a593Smuzhiyun static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun if (!fsm->ops->loc_conn)
240*4882a593Smuzhiyun return -EOPNOTSUPP;
241*4882a593Smuzhiyun if (fsm->loc_conn != on) {
242*4882a593Smuzhiyun fsm->loc_conn = on;
243*4882a593Smuzhiyun fsm->ops->loc_conn(fsm, on);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun return 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
otg_loc_sof(struct otg_fsm * fsm,int on)248*4882a593Smuzhiyun static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun if (!fsm->ops->loc_sof)
251*4882a593Smuzhiyun return -EOPNOTSUPP;
252*4882a593Smuzhiyun if (fsm->loc_sof != on) {
253*4882a593Smuzhiyun fsm->loc_sof = on;
254*4882a593Smuzhiyun fsm->ops->loc_sof(fsm, on);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun return 0;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
otg_start_pulse(struct otg_fsm * fsm)259*4882a593Smuzhiyun static inline int otg_start_pulse(struct otg_fsm *fsm)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun if (!fsm->ops->start_pulse)
262*4882a593Smuzhiyun return -EOPNOTSUPP;
263*4882a593Smuzhiyun if (!fsm->data_pulse) {
264*4882a593Smuzhiyun fsm->data_pulse = 1;
265*4882a593Smuzhiyun fsm->ops->start_pulse(fsm);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
otg_start_adp_prb(struct otg_fsm * fsm)270*4882a593Smuzhiyun static inline int otg_start_adp_prb(struct otg_fsm *fsm)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun if (!fsm->ops->start_adp_prb)
273*4882a593Smuzhiyun return -EOPNOTSUPP;
274*4882a593Smuzhiyun if (!fsm->adp_prb) {
275*4882a593Smuzhiyun fsm->adp_sns = 0;
276*4882a593Smuzhiyun fsm->adp_prb = 1;
277*4882a593Smuzhiyun fsm->ops->start_adp_prb(fsm);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
otg_start_adp_sns(struct otg_fsm * fsm)282*4882a593Smuzhiyun static inline int otg_start_adp_sns(struct otg_fsm *fsm)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun if (!fsm->ops->start_adp_sns)
285*4882a593Smuzhiyun return -EOPNOTSUPP;
286*4882a593Smuzhiyun if (!fsm->adp_sns) {
287*4882a593Smuzhiyun fsm->adp_sns = 1;
288*4882a593Smuzhiyun fsm->ops->start_adp_sns(fsm);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun return 0;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
otg_add_timer(struct otg_fsm * fsm,enum otg_fsm_timer timer)293*4882a593Smuzhiyun static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun if (!fsm->ops->add_timer)
296*4882a593Smuzhiyun return -EOPNOTSUPP;
297*4882a593Smuzhiyun fsm->ops->add_timer(fsm, timer);
298*4882a593Smuzhiyun return 0;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
otg_del_timer(struct otg_fsm * fsm,enum otg_fsm_timer timer)301*4882a593Smuzhiyun static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun if (!fsm->ops->del_timer)
304*4882a593Smuzhiyun return -EOPNOTSUPP;
305*4882a593Smuzhiyun fsm->ops->del_timer(fsm, timer);
306*4882a593Smuzhiyun return 0;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
otg_start_host(struct otg_fsm * fsm,int on)309*4882a593Smuzhiyun static inline int otg_start_host(struct otg_fsm *fsm, int on)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun if (!fsm->ops->start_host)
312*4882a593Smuzhiyun return -EOPNOTSUPP;
313*4882a593Smuzhiyun return fsm->ops->start_host(fsm, on);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
otg_start_gadget(struct otg_fsm * fsm,int on)316*4882a593Smuzhiyun static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun if (!fsm->ops->start_gadget)
319*4882a593Smuzhiyun return -EOPNOTSUPP;
320*4882a593Smuzhiyun return fsm->ops->start_gadget(fsm, on);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun int otg_statemachine(struct otg_fsm *fsm);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun #endif /* __LINUX_USB_OTG_FSM_H */
326