1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#ifndef __LINUX_USB_OTG_FSM_H
20#define __LINUX_USB_OTG_FSM_H
21
22#include <linux/mutex.h>
23#include <linux/errno.h>
24
25#define PROTO_UNDEF (0)
26#define PROTO_HOST (1)
27#define PROTO_GADGET (2)
28
29#define OTG_STS_SELECTOR 0xF000
30
31
32
33
34#define HOST_REQUEST_FLAG 1
35
36
37
38
39#define T_HOST_REQ_POLL (1500)
40
41enum otg_fsm_timer {
42
43 A_WAIT_VRISE,
44 A_WAIT_VFALL,
45 A_WAIT_BCON,
46 A_AIDL_BDIS,
47 B_ASE0_BRST,
48 A_BIDL_ADIS,
49 B_AIDL_BDIS,
50
51
52 B_SE0_SRP,
53 B_SRP_FAIL,
54 A_WAIT_ENUM,
55 B_DATA_PLS,
56 B_SSEND_SRP,
57
58 NUM_OTG_FSM_TIMERS,
59};
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137struct otg_fsm {
138
139 int id;
140 int adp_change;
141 int power_up;
142 int a_srp_det;
143 int a_vbus_vld;
144 int b_conn;
145 int a_bus_resume;
146 int a_bus_suspend;
147 int a_conn;
148 int b_se0_srp;
149 int b_ssend_srp;
150 int b_sess_vld;
151 int test_device;
152 int a_bus_drop;
153 int a_bus_req;
154 int b_bus_req;
155
156
157 int a_sess_vld;
158 int b_bus_resume;
159 int b_bus_suspend;
160
161
162 int drv_vbus;
163 int loc_conn;
164 int loc_sof;
165 int adp_prb;
166 int adp_sns;
167 int data_pulse;
168
169
170 int a_set_b_hnp_en;
171 int b_srp_done;
172 int b_hnp_enable;
173 int a_clr_err;
174
175
176 int a_bus_drop_inf;
177 int a_bus_req_inf;
178 int a_clr_err_inf;
179 int b_bus_req_inf;
180
181 int a_suspend_req_inf;
182
183
184 int a_wait_vrise_tmout;
185 int a_wait_vfall_tmout;
186 int a_wait_bcon_tmout;
187 int a_aidl_bdis_tmout;
188 int b_ase0_brst_tmout;
189 int a_bidl_adis_tmout;
190
191 struct otg_fsm_ops *ops;
192 struct usb_otg *otg;
193
194
195 int protocol;
196 struct mutex lock;
197 u8 *host_req_flag;
198 struct delayed_work hnp_polling_work;
199 bool hnp_work_inited;
200 bool state_changed;
201};
202
203struct otg_fsm_ops {
204 void (*chrg_vbus)(struct otg_fsm *fsm, int on);
205 void (*drv_vbus)(struct otg_fsm *fsm, int on);
206 void (*loc_conn)(struct otg_fsm *fsm, int on);
207 void (*loc_sof)(struct otg_fsm *fsm, int on);
208 void (*start_pulse)(struct otg_fsm *fsm);
209 void (*start_adp_prb)(struct otg_fsm *fsm);
210 void (*start_adp_sns)(struct otg_fsm *fsm);
211 void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
212 void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
213 int (*start_host)(struct otg_fsm *fsm, int on);
214 int (*start_gadget)(struct otg_fsm *fsm, int on);
215};
216
217
218static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
219{
220 if (!fsm->ops->chrg_vbus)
221 return -EOPNOTSUPP;
222 fsm->ops->chrg_vbus(fsm, on);
223 return 0;
224}
225
226static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
227{
228 if (!fsm->ops->drv_vbus)
229 return -EOPNOTSUPP;
230 if (fsm->drv_vbus != on) {
231 fsm->drv_vbus = on;
232 fsm->ops->drv_vbus(fsm, on);
233 }
234 return 0;
235}
236
237static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
238{
239 if (!fsm->ops->loc_conn)
240 return -EOPNOTSUPP;
241 if (fsm->loc_conn != on) {
242 fsm->loc_conn = on;
243 fsm->ops->loc_conn(fsm, on);
244 }
245 return 0;
246}
247
248static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
249{
250 if (!fsm->ops->loc_sof)
251 return -EOPNOTSUPP;
252 if (fsm->loc_sof != on) {
253 fsm->loc_sof = on;
254 fsm->ops->loc_sof(fsm, on);
255 }
256 return 0;
257}
258
259static inline int otg_start_pulse(struct otg_fsm *fsm)
260{
261 if (!fsm->ops->start_pulse)
262 return -EOPNOTSUPP;
263 if (!fsm->data_pulse) {
264 fsm->data_pulse = 1;
265 fsm->ops->start_pulse(fsm);
266 }
267 return 0;
268}
269
270static inline int otg_start_adp_prb(struct otg_fsm *fsm)
271{
272 if (!fsm->ops->start_adp_prb)
273 return -EOPNOTSUPP;
274 if (!fsm->adp_prb) {
275 fsm->adp_sns = 0;
276 fsm->adp_prb = 1;
277 fsm->ops->start_adp_prb(fsm);
278 }
279 return 0;
280}
281
282static inline int otg_start_adp_sns(struct otg_fsm *fsm)
283{
284 if (!fsm->ops->start_adp_sns)
285 return -EOPNOTSUPP;
286 if (!fsm->adp_sns) {
287 fsm->adp_sns = 1;
288 fsm->ops->start_adp_sns(fsm);
289 }
290 return 0;
291}
292
293static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
294{
295 if (!fsm->ops->add_timer)
296 return -EOPNOTSUPP;
297 fsm->ops->add_timer(fsm, timer);
298 return 0;
299}
300
301static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
302{
303 if (!fsm->ops->del_timer)
304 return -EOPNOTSUPP;
305 fsm->ops->del_timer(fsm, timer);
306 return 0;
307}
308
309static inline int otg_start_host(struct otg_fsm *fsm, int on)
310{
311 if (!fsm->ops->start_host)
312 return -EOPNOTSUPP;
313 return fsm->ops->start_host(fsm, on);
314}
315
316static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
317{
318 if (!fsm->ops->start_gadget)
319 return -EOPNOTSUPP;
320 return fsm->ops->start_gadget(fsm, on);
321}
322
323int otg_statemachine(struct otg_fsm *fsm);
324
325#endif
326