1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
6 #ifndef __LINUX_USB_OTG_FSM_H
7 #define __LINUX_USB_OTG_FSM_H
9 #include <linux/mutex.h>
10 #include <linux/errno.h>
12 #define PROTO_UNDEF (0)
13 #define PROTO_HOST (1)
14 #define PROTO_GADGET (2)
16 #define OTG_STS_SELECTOR 0xF000 /* OTG status selector, according to
17 * OTG and EH 2.0 Chapter 6.2.3
21 #define HOST_REQUEST_FLAG 1 /* Host request flag, according to
22 * OTG and EH 2.0 Charpter 6.2.3
26 #define T_HOST_REQ_POLL (1500) /* 1500ms, HNP polling interval */
29 /* Standard OTG timers */
38 /* Auxiliary timers */
49 * struct otg_fsm - OTG state machine according to the OTG spec
53 * Common inputs for A and B device
54 * @id: TRUE for B-device, FALSE for A-device.
55 * @adp_change: TRUE when current ADP measurement (n) value, compared to the
56 * ADP measurement taken at n-2, differs by more than CADP_THR
57 * @power_up: TRUE when the OTG device first powers up its USB system and
58 * ADP measurement taken if ADP capable
60 * A-Device state inputs
61 * @a_srp_det: TRUE if the A-device detects SRP
62 * @a_vbus_vld: TRUE when VBUS voltage is in regulation
63 * @b_conn: TRUE if the A-device detects connection from the B-device
64 * @a_bus_resume: TRUE when the B-device detects that the A-device is signaling
66 * B-Device state inputs
67 * @a_bus_suspend: TRUE when the B-device detects that the A-device has put the
69 * @a_conn: TRUE if the B-device detects a connection from the A-device
70 * @b_se0_srp: TRUE when the line has been at SE0 for more than the minimum
71 * time before generating SRP
72 * @b_ssend_srp: TRUE when the VBUS has been below VOTG_SESS_VLD for more than
73 * the minimum time before generating SRP
74 * @b_sess_vld: TRUE when the B-device detects that the voltage on VBUS is
76 * @test_device: TRUE when the B-device switches to B-Host and detects an OTG
77 * test device. This must be set by host/hub driver
79 * Application inputs (A-Device)
80 * @a_bus_drop: TRUE when A-device application needs to power down the bus
81 * @a_bus_req: TRUE when A-device application wants to use the bus.
82 * FALSE to suspend the bus
84 * Application inputs (B-Device)
85 * @b_bus_req: TRUE during the time that the Application running on the
86 * B-device wants to use the bus
88 * Auxiliary inputs (OTG v1.3 only. Obsolete now.)
89 * @a_sess_vld: TRUE if the A-device detects that VBUS is above VA_SESS_VLD
90 * @b_bus_suspend: TRUE when the A-device detects that the B-device has put
91 * the bus into suspend
92 * @b_bus_resume: TRUE when the A-device detects that the B-device is signaling
95 * OTG Output status. Read only for users. Updated by OTG FSM helpers defined
98 * Outputs for Both A and B device
99 * @drv_vbus: TRUE when A-device is driving VBUS
100 * @loc_conn: TRUE when the local device has signaled that it is connected
102 * @loc_sof: TRUE when the local device is generating activity on the bus
103 * @adp_prb: TRUE when the local device is in the process of doing
106 * Outputs for B-device state
107 * @adp_sns: TRUE when the B-device is in the process of carrying out
109 * @data_pulse: TRUE when the B-device is performing data line pulsing
113 * a_set_b_hnp_en: TRUE when the A-device has successfully set the
114 * b_hnp_enable bit in the B-device.
115 * Unused as OTG fsm uses otg->host->b_hnp_enable instead
116 * b_srp_done: TRUE when the B-device has completed initiating SRP
117 * b_hnp_enable: TRUE when the B-device has accepted the
118 * SetFeature(b_hnp_enable) B-device.
119 * Unused as OTG fsm uses otg->gadget->b_hnp_enable instead
120 * a_clr_err: Asserted (by application ?) to clear a_vbus_err due to an
121 * overcurrent condition and causes the A-device to transition
143 /* Auxiliary inputs */
156 /* Internal variables */
162 /* Informative variables. All unused as of now */
167 /* Auxiliary informative variables */
168 int a_suspend_req_inf
;
170 /* Timeout indicator for timers */
171 int a_wait_vrise_tmout
;
172 int a_wait_vfall_tmout
;
173 int a_wait_bcon_tmout
;
174 int a_aidl_bdis_tmout
;
175 int b_ase0_brst_tmout
;
176 int a_bidl_adis_tmout
;
178 struct otg_fsm_ops
*ops
;
181 /* Current usb protocol used: 0:undefine; 1:host; 2:client */
185 struct delayed_work hnp_polling_work
;
186 bool hnp_work_inited
;
191 void (*chrg_vbus
)(struct otg_fsm
*fsm
, int on
);
192 void (*drv_vbus
)(struct otg_fsm
*fsm
, int on
);
193 void (*loc_conn
)(struct otg_fsm
*fsm
, int on
);
194 void (*loc_sof
)(struct otg_fsm
*fsm
, int on
);
195 void (*start_pulse
)(struct otg_fsm
*fsm
);
196 void (*start_adp_prb
)(struct otg_fsm
*fsm
);
197 void (*start_adp_sns
)(struct otg_fsm
*fsm
);
198 void (*add_timer
)(struct otg_fsm
*fsm
, enum otg_fsm_timer timer
);
199 void (*del_timer
)(struct otg_fsm
*fsm
, enum otg_fsm_timer timer
);
200 int (*start_host
)(struct otg_fsm
*fsm
, int on
);
201 int (*start_gadget
)(struct otg_fsm
*fsm
, int on
);
205 static inline int otg_chrg_vbus(struct otg_fsm
*fsm
, int on
)
207 if (!fsm
->ops
->chrg_vbus
)
209 fsm
->ops
->chrg_vbus(fsm
, on
);
213 static inline int otg_drv_vbus(struct otg_fsm
*fsm
, int on
)
215 if (!fsm
->ops
->drv_vbus
)
217 if (fsm
->drv_vbus
!= on
) {
219 fsm
->ops
->drv_vbus(fsm
, on
);
224 static inline int otg_loc_conn(struct otg_fsm
*fsm
, int on
)
226 if (!fsm
->ops
->loc_conn
)
228 if (fsm
->loc_conn
!= on
) {
230 fsm
->ops
->loc_conn(fsm
, on
);
235 static inline int otg_loc_sof(struct otg_fsm
*fsm
, int on
)
237 if (!fsm
->ops
->loc_sof
)
239 if (fsm
->loc_sof
!= on
) {
241 fsm
->ops
->loc_sof(fsm
, on
);
246 static inline int otg_start_pulse(struct otg_fsm
*fsm
)
248 if (!fsm
->ops
->start_pulse
)
250 if (!fsm
->data_pulse
) {
252 fsm
->ops
->start_pulse(fsm
);
257 static inline int otg_start_adp_prb(struct otg_fsm
*fsm
)
259 if (!fsm
->ops
->start_adp_prb
)
264 fsm
->ops
->start_adp_prb(fsm
);
269 static inline int otg_start_adp_sns(struct otg_fsm
*fsm
)
271 if (!fsm
->ops
->start_adp_sns
)
275 fsm
->ops
->start_adp_sns(fsm
);
280 static inline int otg_add_timer(struct otg_fsm
*fsm
, enum otg_fsm_timer timer
)
282 if (!fsm
->ops
->add_timer
)
284 fsm
->ops
->add_timer(fsm
, timer
);
288 static inline int otg_del_timer(struct otg_fsm
*fsm
, enum otg_fsm_timer timer
)
290 if (!fsm
->ops
->del_timer
)
292 fsm
->ops
->del_timer(fsm
, timer
);
296 static inline int otg_start_host(struct otg_fsm
*fsm
, int on
)
298 if (!fsm
->ops
->start_host
)
300 return fsm
->ops
->start_host(fsm
, on
);
303 static inline int otg_start_gadget(struct otg_fsm
*fsm
, int on
)
305 if (!fsm
->ops
->start_gadget
)
307 return fsm
->ops
->start_gadget(fsm
, on
);
310 int otg_statemachine(struct otg_fsm
*fsm
);
312 #endif /* __LINUX_USB_OTG_FSM_H */