1 /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc.
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the
5 * Free Software Foundation; either version 2 of the License, or (at your
6 * option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 675 Mass Ave, Cambridge, MA 02139, USA.
18 #ifndef __LINUX_USB_OTG_FSM_H
19 #define __LINUX_USB_OTG_FSM_H
21 #include <linux/mutex.h>
22 #include <linux/errno.h>
27 #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \
30 #define VDBG(stuff...) do {} while (0)
34 #define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__)
36 #define MPC_LOC do {} while (0)
39 #define PROTO_UNDEF (0)
40 #define PROTO_HOST (1)
41 #define PROTO_GADGET (2)
43 #define OTG_STS_SELECTOR 0xF000 /* OTG status selector, according to
44 * OTG and EH 2.0 Chapter 6.2.3
48 #define HOST_REQUEST_FLAG 1 /* Host request flag, according to
49 * OTG and EH 2.0 Charpter 6.2.3
53 #define T_HOST_REQ_POLL (1500) /* 1500ms, HNP polling interval */
56 /* Standard OTG timers */
65 /* Auxiliary timers */
76 * struct otg_fsm - OTG state machine according to the OTG spec
80 * Common inputs for A and B device
81 * @id: TRUE for B-device, FALSE for A-device.
82 * @adp_change: TRUE when current ADP measurement (n) value, compared to the
83 * ADP measurement taken at n-2, differs by more than CADP_THR
84 * @power_up: TRUE when the OTG device first powers up its USB system and
85 * ADP measurement taken if ADP capable
87 * A-Device state inputs
88 * @a_srp_det: TRUE if the A-device detects SRP
89 * @a_vbus_vld: TRUE when VBUS voltage is in regulation
90 * @b_conn: TRUE if the A-device detects connection from the B-device
91 * @a_bus_resume: TRUE when the B-device detects that the A-device is signaling
93 * B-Device state inputs
94 * @a_bus_suspend: TRUE when the B-device detects that the A-device has put the
96 * @a_conn: TRUE if the B-device detects a connection from the A-device
97 * @b_se0_srp: TRUE when the line has been at SE0 for more than the minimum
98 * time before generating SRP
99 * @b_ssend_srp: TRUE when the VBUS has been below VOTG_SESS_VLD for more than
100 * the minimum time before generating SRP
101 * @b_sess_vld: TRUE when the B-device detects that the voltage on VBUS is
102 * above VOTG_SESS_VLD
103 * @test_device: TRUE when the B-device switches to B-Host and detects an OTG
104 * test device. This must be set by host/hub driver
106 * Application inputs (A-Device)
107 * @a_bus_drop: TRUE when A-device application needs to power down the bus
108 * @a_bus_req: TRUE when A-device application wants to use the bus.
109 * FALSE to suspend the bus
111 * Application inputs (B-Device)
112 * @b_bus_req: TRUE during the time that the Application running on the
113 * B-device wants to use the bus
115 * Auxilary inputs (OTG v1.3 only. Obsolete now.)
116 * @a_sess_vld: TRUE if the A-device detects that VBUS is above VA_SESS_VLD
117 * @b_bus_suspend: TRUE when the A-device detects that the B-device has put
118 * the bus into suspend
119 * @b_bus_resume: TRUE when the A-device detects that the B-device is signaling
122 * OTG Output status. Read only for users. Updated by OTG FSM helpers defined
125 * Outputs for Both A and B device
126 * @drv_vbus: TRUE when A-device is driving VBUS
127 * @loc_conn: TRUE when the local device has signaled that it is connected
129 * @loc_sof: TRUE when the local device is generating activity on the bus
130 * @adp_prb: TRUE when the local device is in the process of doing
133 * Outputs for B-device state
134 * @adp_sns: TRUE when the B-device is in the process of carrying out
136 * @data_pulse: TRUE when the B-device is performing data line pulsing
140 * a_set_b_hnp_en: TRUE when the A-device has successfully set the
141 * b_hnp_enable bit in the B-device.
142 * Unused as OTG fsm uses otg->host->b_hnp_enable instead
143 * b_srp_done: TRUE when the B-device has completed initiating SRP
144 * b_hnp_enable: TRUE when the B-device has accepted the
145 * SetFeature(b_hnp_enable) B-device.
146 * Unused as OTG fsm uses otg->gadget->b_hnp_enable instead
147 * a_clr_err: Asserted (by application ?) to clear a_vbus_err due to an
148 * overcurrent condition and causes the A-device to transition
170 /* Auxilary inputs */
183 /* Internal variables */
189 /* Informative variables. All unused as of now */
194 /* Auxilary informative variables */
195 int a_suspend_req_inf;
197 /* Timeout indicator for timers */
198 int a_wait_vrise_tmout;
199 int a_wait_vfall_tmout;
200 int a_wait_bcon_tmout;
201 int a_aidl_bdis_tmout;
202 int b_ase0_brst_tmout;
203 int a_bidl_adis_tmout;
205 struct otg_fsm_ops *ops;
208 /* Current usb protocol used: 0:undefine; 1:host; 2:client */
212 struct delayed_work hnp_polling_work;
213 bool hnp_work_inited;
218 void (*chrg_vbus)(struct otg_fsm *fsm, int on);
219 void (*drv_vbus)(struct otg_fsm *fsm, int on);
220 void (*loc_conn)(struct otg_fsm *fsm, int on);
221 void (*loc_sof)(struct otg_fsm *fsm, int on);
222 void (*start_pulse)(struct otg_fsm *fsm);
223 void (*start_adp_prb)(struct otg_fsm *fsm);
224 void (*start_adp_sns)(struct otg_fsm *fsm);
225 void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
226 void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
227 int (*start_host)(struct otg_fsm *fsm, int on);
228 int (*start_gadget)(struct otg_fsm *fsm, int on);
232 static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
234 if (!fsm->ops->chrg_vbus)
236 fsm->ops->chrg_vbus(fsm, on);
240 static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
242 if (!fsm->ops->drv_vbus)
244 if (fsm->drv_vbus != on) {
246 fsm->ops->drv_vbus(fsm, on);
251 static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
253 if (!fsm->ops->loc_conn)
255 if (fsm->loc_conn != on) {
257 fsm->ops->loc_conn(fsm, on);
262 static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
264 if (!fsm->ops->loc_sof)
266 if (fsm->loc_sof != on) {
268 fsm->ops->loc_sof(fsm, on);
273 static inline int otg_start_pulse(struct otg_fsm *fsm)
275 if (!fsm->ops->start_pulse)
277 if (!fsm->data_pulse) {
279 fsm->ops->start_pulse(fsm);
284 static inline int otg_start_adp_prb(struct otg_fsm *fsm)
286 if (!fsm->ops->start_adp_prb)
291 fsm->ops->start_adp_prb(fsm);
296 static inline int otg_start_adp_sns(struct otg_fsm *fsm)
298 if (!fsm->ops->start_adp_sns)
302 fsm->ops->start_adp_sns(fsm);
307 static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
309 if (!fsm->ops->add_timer)
311 fsm->ops->add_timer(fsm, timer);
315 static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
317 if (!fsm->ops->del_timer)
319 fsm->ops->del_timer(fsm, timer);
323 static inline int otg_start_host(struct otg_fsm *fsm, int on)
325 if (!fsm->ops->start_host)
327 return fsm->ops->start_host(fsm, on);
330 static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
332 if (!fsm->ops->start_gadget)
334 return fsm->ops->start_gadget(fsm, on);
337 int otg_statemachine(struct otg_fsm *fsm);
339 #endif /* __LINUX_USB_OTG_FSM_H */