Commit | Line | Data |
---|---|---|
0807c500 LY |
1 | /* Copyright (C) 2007,2008 Freescale Semiconductor, Inc. |
2 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
0807c500 LY |
18 | #undef VERBOSE |
19 | ||
0807c500 | 20 | #ifdef VERBOSE |
523e531e GKH |
21 | #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \ |
22 | __func__, ## args) | |
0807c500 LY |
23 | #else |
24 | #define VDBG(stuff...) do {} while (0) | |
25 | #endif | |
26 | ||
27 | #ifdef VERBOSE | |
28 | #define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__) | |
29 | #else | |
30 | #define MPC_LOC do {} while (0) | |
31 | #endif | |
32 | ||
33 | #define PROTO_UNDEF (0) | |
34 | #define PROTO_HOST (1) | |
35 | #define PROTO_GADGET (2) | |
36 | ||
f6de27ee | 37 | enum otg_fsm_timer { |
3294908b | 38 | /* Standard OTG timers */ |
f6de27ee | 39 | A_WAIT_VRISE, |
3294908b | 40 | A_WAIT_VFALL, |
f6de27ee AT |
41 | A_WAIT_BCON, |
42 | A_AIDL_BDIS, | |
43 | B_ASE0_BRST, | |
3294908b AT |
44 | A_BIDL_ADIS, |
45 | ||
46 | /* Auxiliary timers */ | |
f6de27ee AT |
47 | B_SE0_SRP, |
48 | B_SRP_FAIL, | |
49 | A_WAIT_ENUM, | |
3294908b | 50 | |
f6de27ee AT |
51 | NUM_OTG_FSM_TIMERS, |
52 | }; | |
53 | ||
0807c500 LY |
54 | /* OTG state machine according to the OTG spec */ |
55 | struct otg_fsm { | |
56 | /* Input */ | |
4662e5ef | 57 | int id; |
ec04996a AT |
58 | int adp_change; |
59 | int power_up; | |
60 | int test_device; | |
cff4dab4 AT |
61 | int a_bus_drop; |
62 | int a_bus_req; | |
4662e5ef AT |
63 | int a_srp_det; |
64 | int a_vbus_vld; | |
65 | int b_conn; | |
0807c500 LY |
66 | int a_bus_resume; |
67 | int a_bus_suspend; | |
68 | int a_conn; | |
cff4dab4 | 69 | int b_bus_req; |
0807c500 | 70 | int b_se0_srp; |
68041785 | 71 | int b_ssend_srp; |
0807c500 | 72 | int b_sess_vld; |
4662e5ef AT |
73 | /* Auxilary inputs */ |
74 | int a_sess_vld; | |
75 | int b_bus_resume; | |
76 | int b_bus_suspend; | |
77 | ||
78 | /* Output */ | |
79 | int data_pulse; | |
80 | int drv_vbus; | |
81 | int loc_conn; | |
82 | int loc_sof; | |
83 | int adp_prb; | |
84 | int adp_sns; | |
0807c500 LY |
85 | |
86 | /* Internal variables */ | |
87 | int a_set_b_hnp_en; | |
88 | int b_srp_done; | |
89 | int b_hnp_enable; | |
cff4dab4 | 90 | int a_clr_err; |
0807c500 | 91 | |
4662e5ef AT |
92 | /* Informative variables */ |
93 | int a_bus_drop_inf; | |
94 | int a_bus_req_inf; | |
95 | int a_clr_err_inf; | |
96 | int b_bus_req_inf; | |
97 | /* Auxilary informative variables */ | |
98 | int a_suspend_req_inf; | |
99 | ||
0807c500 LY |
100 | /* Timeout indicator for timers */ |
101 | int a_wait_vrise_tmout; | |
3294908b | 102 | int a_wait_vfall_tmout; |
0807c500 LY |
103 | int a_wait_bcon_tmout; |
104 | int a_aidl_bdis_tmout; | |
105 | int b_ase0_brst_tmout; | |
3294908b | 106 | int a_bidl_adis_tmout; |
0807c500 | 107 | |
0807c500 | 108 | struct otg_fsm_ops *ops; |
7e062c0f | 109 | struct usb_otg *otg; |
0807c500 LY |
110 | |
111 | /* Current usb protocol used: 0:undefine; 1:host; 2:client */ | |
112 | int protocol; | |
113 | spinlock_t lock; | |
114 | }; | |
115 | ||
116 | struct otg_fsm_ops { | |
da8cc167 AT |
117 | void (*chrg_vbus)(struct otg_fsm *fsm, int on); |
118 | void (*drv_vbus)(struct otg_fsm *fsm, int on); | |
119 | void (*loc_conn)(struct otg_fsm *fsm, int on); | |
120 | void (*loc_sof)(struct otg_fsm *fsm, int on); | |
121 | void (*start_pulse)(struct otg_fsm *fsm); | |
ec04996a AT |
122 | void (*start_adp_prb)(struct otg_fsm *fsm); |
123 | void (*start_adp_sns)(struct otg_fsm *fsm); | |
f6de27ee AT |
124 | void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); |
125 | void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); | |
0807c500 LY |
126 | int (*start_host)(struct otg_fsm *fsm, int on); |
127 | int (*start_gadget)(struct otg_fsm *fsm, int on); | |
128 | }; | |
129 | ||
130 | ||
737cc66e | 131 | static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on) |
0807c500 | 132 | { |
737cc66e AT |
133 | if (!fsm->ops->chrg_vbus) |
134 | return -EOPNOTSUPP; | |
da8cc167 | 135 | fsm->ops->chrg_vbus(fsm, on); |
737cc66e | 136 | return 0; |
0807c500 LY |
137 | } |
138 | ||
737cc66e | 139 | static inline int otg_drv_vbus(struct otg_fsm *fsm, int on) |
0807c500 | 140 | { |
737cc66e AT |
141 | if (!fsm->ops->drv_vbus) |
142 | return -EOPNOTSUPP; | |
0807c500 LY |
143 | if (fsm->drv_vbus != on) { |
144 | fsm->drv_vbus = on; | |
da8cc167 | 145 | fsm->ops->drv_vbus(fsm, on); |
0807c500 | 146 | } |
737cc66e | 147 | return 0; |
0807c500 LY |
148 | } |
149 | ||
737cc66e | 150 | static inline int otg_loc_conn(struct otg_fsm *fsm, int on) |
0807c500 | 151 | { |
737cc66e AT |
152 | if (!fsm->ops->loc_conn) |
153 | return -EOPNOTSUPP; | |
0807c500 LY |
154 | if (fsm->loc_conn != on) { |
155 | fsm->loc_conn = on; | |
da8cc167 | 156 | fsm->ops->loc_conn(fsm, on); |
0807c500 | 157 | } |
737cc66e | 158 | return 0; |
0807c500 LY |
159 | } |
160 | ||
737cc66e | 161 | static inline int otg_loc_sof(struct otg_fsm *fsm, int on) |
0807c500 | 162 | { |
737cc66e AT |
163 | if (!fsm->ops->loc_sof) |
164 | return -EOPNOTSUPP; | |
0807c500 LY |
165 | if (fsm->loc_sof != on) { |
166 | fsm->loc_sof = on; | |
da8cc167 | 167 | fsm->ops->loc_sof(fsm, on); |
0807c500 | 168 | } |
737cc66e | 169 | return 0; |
0807c500 LY |
170 | } |
171 | ||
737cc66e | 172 | static inline int otg_start_pulse(struct otg_fsm *fsm) |
0807c500 | 173 | { |
737cc66e AT |
174 | if (!fsm->ops->start_pulse) |
175 | return -EOPNOTSUPP; | |
ec04996a AT |
176 | if (!fsm->data_pulse) { |
177 | fsm->data_pulse = 1; | |
178 | fsm->ops->start_pulse(fsm); | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static inline int otg_start_adp_prb(struct otg_fsm *fsm) | |
184 | { | |
185 | if (!fsm->ops->start_adp_prb) | |
186 | return -EOPNOTSUPP; | |
187 | if (!fsm->adp_prb) { | |
188 | fsm->adp_sns = 0; | |
189 | fsm->adp_prb = 1; | |
190 | fsm->ops->start_adp_prb(fsm); | |
191 | } | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static inline int otg_start_adp_sns(struct otg_fsm *fsm) | |
196 | { | |
197 | if (!fsm->ops->start_adp_sns) | |
198 | return -EOPNOTSUPP; | |
199 | if (!fsm->adp_sns) { | |
200 | fsm->adp_sns = 1; | |
201 | fsm->ops->start_adp_sns(fsm); | |
202 | } | |
737cc66e | 203 | return 0; |
0807c500 LY |
204 | } |
205 | ||
f6de27ee | 206 | static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer) |
0807c500 | 207 | { |
737cc66e AT |
208 | if (!fsm->ops->add_timer) |
209 | return -EOPNOTSUPP; | |
da8cc167 | 210 | fsm->ops->add_timer(fsm, timer); |
737cc66e | 211 | return 0; |
0807c500 LY |
212 | } |
213 | ||
f6de27ee | 214 | static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer) |
0807c500 | 215 | { |
737cc66e AT |
216 | if (!fsm->ops->del_timer) |
217 | return -EOPNOTSUPP; | |
da8cc167 | 218 | fsm->ops->del_timer(fsm, timer); |
737cc66e | 219 | return 0; |
0807c500 LY |
220 | } |
221 | ||
425d7101 AT |
222 | static inline int otg_start_host(struct otg_fsm *fsm, int on) |
223 | { | |
224 | if (!fsm->ops->start_host) | |
225 | return -EOPNOTSUPP; | |
226 | return fsm->ops->start_host(fsm, on); | |
227 | } | |
228 | ||
229 | static inline int otg_start_gadget(struct otg_fsm *fsm, int on) | |
230 | { | |
231 | if (!fsm->ops->start_gadget) | |
232 | return -EOPNOTSUPP; | |
233 | return fsm->ops->start_gadget(fsm, on); | |
234 | } | |
235 | ||
0807c500 | 236 | int otg_statemachine(struct otg_fsm *fsm); |