blob: 7a0350535cb10146ffd4e1936da7dced6bc477ca [file] [log] [blame]
Li Yang0807c502011-04-18 22:01:59 +02001/* 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
Anton Tikhomirovd49dd782013-11-26 10:08:58 -060018#ifndef __LINUX_USB_OTG_FSM_H
19#define __LINUX_USB_OTG_FSM_H
20
Anton Tikhomirov16e569e2013-11-26 11:46:05 +090021#include <linux/mutex.h>
Felipe Balbicc9d9cc2013-11-26 10:06:00 -060022#include <linux/errno.h>
23
Li Yang0807c502011-04-18 22:01:59 +020024#undef VERBOSE
25
Li Yang0807c502011-04-18 22:01:59 +020026#ifdef VERBOSE
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -070027#define VDBG(fmt, args...) pr_debug("[%s] " fmt , \
28 __func__, ## args)
Li Yang0807c502011-04-18 22:01:59 +020029#else
30#define VDBG(stuff...) do {} while (0)
31#endif
32
33#ifdef VERBOSE
34#define MPC_LOC printk("Current Location [%s]:[%d]\n", __FILE__, __LINE__)
35#else
36#define MPC_LOC do {} while (0)
37#endif
38
39#define PROTO_UNDEF (0)
40#define PROTO_HOST (1)
41#define PROTO_GADGET (2)
42
Li Junae57e972016-02-19 10:04:42 +080043#define OTG_STS_SELECTOR 0xF000 /* OTG status selector, according to
44 * OTG and EH 2.0 Chapter 6.2.3
45 * Table:6-4
46 */
47
48#define HOST_REQUEST_FLAG 1 /* Host request flag, according to
49 * OTG and EH 2.0 Charpter 6.2.3
50 * Table:6-5
51 */
52
53#define T_HOST_REQ_POLL (1500) /* 1500ms, HNP polling interval */
54
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090055enum otg_fsm_timer {
Anton Tikhomirov32949082013-10-03 12:42:04 +090056 /* Standard OTG timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090057 A_WAIT_VRISE,
Anton Tikhomirov32949082013-10-03 12:42:04 +090058 A_WAIT_VFALL,
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090059 A_WAIT_BCON,
60 A_AIDL_BDIS,
61 B_ASE0_BRST,
Anton Tikhomirov32949082013-10-03 12:42:04 +090062 A_BIDL_ADIS,
Li Jun9c527f42016-02-19 10:04:48 +080063 B_AIDL_BDIS,
Anton Tikhomirov32949082013-10-03 12:42:04 +090064
65 /* Auxiliary timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090066 B_SE0_SRP,
67 B_SRP_FAIL,
68 A_WAIT_ENUM,
Li Jun2f8a4672015-03-20 16:28:05 +080069 B_DATA_PLS,
70 B_SSEND_SRP,
Anton Tikhomirov32949082013-10-03 12:42:04 +090071
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090072 NUM_OTG_FSM_TIMERS,
73};
74
Roger Quadrosfa59507f2016-03-30 12:56:28 +030075/**
76 * struct otg_fsm - OTG state machine according to the OTG spec
77 *
78 * OTG hardware Inputs
79 *
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
86 *
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
92 * a resume (K state)
93 * B-Device state inputs
94 * @a_bus_suspend: TRUE when the B-device detects that the A-device has put the
95 * bus into suspend
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
105 *
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
110 *
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
114 *
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
120 * resume on the bus
121 *
122 * OTG Output status. Read only for users. Updated by OTG FSM helpers defined
123 * in this file
124 *
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
128 * to the bus
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
131 * ADP probing
132 *
133 * Outputs for B-device state
134 * @adp_sns: TRUE when the B-device is in the process of carrying out
135 * ADP sensing
136 * @data_pulse: TRUE when the B-device is performing data line pulsing
137 *
138 * Internal Variables
139 *
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
149 * to a_wait_vfall
150 */
Li Yang0807c502011-04-18 22:01:59 +0200151struct otg_fsm {
152 /* Input */
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +0900153 int id;
Anton Tikhomirovec049962013-10-03 12:42:04 +0900154 int adp_change;
155 int power_up;
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +0900156 int a_srp_det;
157 int a_vbus_vld;
158 int b_conn;
Li Yang0807c502011-04-18 22:01:59 +0200159 int a_bus_resume;
160 int a_bus_suspend;
161 int a_conn;
Li Yang0807c502011-04-18 22:01:59 +0200162 int b_se0_srp;
Anton Tikhomirov68041782013-10-03 12:42:04 +0900163 int b_ssend_srp;
Li Yang0807c502011-04-18 22:01:59 +0200164 int b_sess_vld;
Roger Quadrosfa59507f2016-03-30 12:56:28 +0300165 int test_device;
166 int a_bus_drop;
167 int a_bus_req;
168 int b_bus_req;
169
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +0900170 /* Auxilary inputs */
171 int a_sess_vld;
172 int b_bus_resume;
173 int b_bus_suspend;
Li Yang0807c502011-04-18 22:01:59 +0200174
175 /* Output */
176 int drv_vbus;
177 int loc_conn;
178 int loc_sof;
Anton Tikhomirovec049962013-10-03 12:42:04 +0900179 int adp_prb;
180 int adp_sns;
Roger Quadrosfa59507f2016-03-30 12:56:28 +0300181 int data_pulse;
Li Yang0807c502011-04-18 22:01:59 +0200182
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +0900183 /* Internal variables */
184 int a_set_b_hnp_en;
185 int b_srp_done;
186 int b_hnp_enable;
187 int a_clr_err;
188
Roger Quadrosfa59507f2016-03-30 12:56:28 +0300189 /* Informative variables. All unused as of now */
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +0900190 int a_bus_drop_inf;
191 int a_bus_req_inf;
192 int a_clr_err_inf;
193 int b_bus_req_inf;
194 /* Auxilary informative variables */
195 int a_suspend_req_inf;
196
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;
204
Li Yang0807c502011-04-18 22:01:59 +0200205 struct otg_fsm_ops *ops;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200206 struct usb_otg *otg;
Li Yang0807c502011-04-18 22:01:59 +0200207
208 /* Current usb protocol used: 0:undefine; 1:host; 2:client */
209 int protocol;
Anton Tikhomirov16e569e2013-11-26 11:46:05 +0900210 struct mutex lock;
Li Junae57e972016-02-19 10:04:42 +0800211 u8 *host_req_flag;
212 struct delayed_work hnp_polling_work;
Roger Quadros4e332df2016-03-30 12:56:29 +0300213 bool state_changed;
Li Yang0807c502011-04-18 22:01:59 +0200214};
215
216struct otg_fsm_ops {
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900217 void (*chrg_vbus)(struct otg_fsm *fsm, int on);
218 void (*drv_vbus)(struct otg_fsm *fsm, int on);
219 void (*loc_conn)(struct otg_fsm *fsm, int on);
220 void (*loc_sof)(struct otg_fsm *fsm, int on);
221 void (*start_pulse)(struct otg_fsm *fsm);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900222 void (*start_adp_prb)(struct otg_fsm *fsm);
223 void (*start_adp_sns)(struct otg_fsm *fsm);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900224 void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
225 void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
Li Yang0807c502011-04-18 22:01:59 +0200226 int (*start_host)(struct otg_fsm *fsm, int on);
227 int (*start_gadget)(struct otg_fsm *fsm, int on);
228};
229
230
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900231static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200232{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900233 if (!fsm->ops->chrg_vbus)
234 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900235 fsm->ops->chrg_vbus(fsm, on);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900236 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200237}
238
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900239static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200240{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900241 if (!fsm->ops->drv_vbus)
242 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200243 if (fsm->drv_vbus != on) {
244 fsm->drv_vbus = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900245 fsm->ops->drv_vbus(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200246 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900247 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200248}
249
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900250static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200251{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900252 if (!fsm->ops->loc_conn)
253 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200254 if (fsm->loc_conn != on) {
255 fsm->loc_conn = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900256 fsm->ops->loc_conn(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200257 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900258 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200259}
260
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900261static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200262{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900263 if (!fsm->ops->loc_sof)
264 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200265 if (fsm->loc_sof != on) {
266 fsm->loc_sof = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900267 fsm->ops->loc_sof(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200268 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900269 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200270}
271
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900272static inline int otg_start_pulse(struct otg_fsm *fsm)
Li Yang0807c502011-04-18 22:01:59 +0200273{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900274 if (!fsm->ops->start_pulse)
275 return -EOPNOTSUPP;
Anton Tikhomirovec049962013-10-03 12:42:04 +0900276 if (!fsm->data_pulse) {
277 fsm->data_pulse = 1;
278 fsm->ops->start_pulse(fsm);
279 }
280 return 0;
281}
282
283static inline int otg_start_adp_prb(struct otg_fsm *fsm)
284{
285 if (!fsm->ops->start_adp_prb)
286 return -EOPNOTSUPP;
287 if (!fsm->adp_prb) {
288 fsm->adp_sns = 0;
289 fsm->adp_prb = 1;
290 fsm->ops->start_adp_prb(fsm);
291 }
292 return 0;
293}
294
295static inline int otg_start_adp_sns(struct otg_fsm *fsm)
296{
297 if (!fsm->ops->start_adp_sns)
298 return -EOPNOTSUPP;
299 if (!fsm->adp_sns) {
300 fsm->adp_sns = 1;
301 fsm->ops->start_adp_sns(fsm);
302 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900303 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200304}
305
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900306static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200307{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900308 if (!fsm->ops->add_timer)
309 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900310 fsm->ops->add_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900311 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200312}
313
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900314static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200315{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900316 if (!fsm->ops->del_timer)
317 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900318 fsm->ops->del_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900319 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200320}
321
Anton Tikhomirov425d7102013-10-03 12:42:04 +0900322static inline int otg_start_host(struct otg_fsm *fsm, int on)
323{
324 if (!fsm->ops->start_host)
325 return -EOPNOTSUPP;
326 return fsm->ops->start_host(fsm, on);
327}
328
329static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
330{
331 if (!fsm->ops->start_gadget)
332 return -EOPNOTSUPP;
333 return fsm->ops->start_gadget(fsm, on);
334}
335
Li Yang0807c502011-04-18 22:01:59 +0200336int otg_statemachine(struct otg_fsm *fsm);
Anton Tikhomirovd49dd782013-11-26 10:08:58 -0600337
338#endif /* __LINUX_USB_OTG_FSM_H */