blob: f728f1854829e01c99663ded8f50973b3e311c80 [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
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090043enum otg_fsm_timer {
Anton Tikhomirov32949082013-10-03 12:42:04 +090044 /* Standard OTG timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090045 A_WAIT_VRISE,
Anton Tikhomirov32949082013-10-03 12:42:04 +090046 A_WAIT_VFALL,
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090047 A_WAIT_BCON,
48 A_AIDL_BDIS,
49 B_ASE0_BRST,
Anton Tikhomirov32949082013-10-03 12:42:04 +090050 A_BIDL_ADIS,
51
52 /* Auxiliary timers */
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090053 B_SE0_SRP,
54 B_SRP_FAIL,
55 A_WAIT_ENUM,
Li Jun2f8a4672015-03-20 16:28:05 +080056 B_DATA_PLS,
57 B_SSEND_SRP,
Anton Tikhomirov32949082013-10-03 12:42:04 +090058
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +090059 NUM_OTG_FSM_TIMERS,
60};
61
Li Yang0807c502011-04-18 22:01:59 +020062/* OTG state machine according to the OTG spec */
63struct otg_fsm {
64 /* Input */
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090065 int id;
Anton Tikhomirovec049962013-10-03 12:42:04 +090066 int adp_change;
67 int power_up;
68 int test_device;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +090069 int a_bus_drop;
70 int a_bus_req;
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090071 int a_srp_det;
72 int a_vbus_vld;
73 int b_conn;
Li Yang0807c502011-04-18 22:01:59 +020074 int a_bus_resume;
75 int a_bus_suspend;
76 int a_conn;
Anton Tikhomirovcff4dab2013-10-03 12:42:04 +090077 int b_bus_req;
Li Yang0807c502011-04-18 22:01:59 +020078 int b_se0_srp;
Anton Tikhomirov68041782013-10-03 12:42:04 +090079 int b_ssend_srp;
Li Yang0807c502011-04-18 22:01:59 +020080 int b_sess_vld;
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090081 /* Auxilary inputs */
82 int a_sess_vld;
83 int b_bus_resume;
84 int b_bus_suspend;
Li Yang0807c502011-04-18 22:01:59 +020085
86 /* Output */
Anton Tikhomirovec049962013-10-03 12:42:04 +090087 int data_pulse;
Li Yang0807c502011-04-18 22:01:59 +020088 int drv_vbus;
89 int loc_conn;
90 int loc_sof;
Anton Tikhomirovec049962013-10-03 12:42:04 +090091 int adp_prb;
92 int adp_sns;
Li Yang0807c502011-04-18 22:01:59 +020093
Anton Tikhomirov4662e5e2013-10-03 12:42:04 +090094 /* Internal variables */
95 int a_set_b_hnp_en;
96 int b_srp_done;
97 int b_hnp_enable;
98 int a_clr_err;
99
100 /* Informative variables */
101 int a_bus_drop_inf;
102 int a_bus_req_inf;
103 int a_clr_err_inf;
104 int b_bus_req_inf;
105 /* Auxilary informative variables */
106 int a_suspend_req_inf;
107
108 /* Timeout indicator for timers */
109 int a_wait_vrise_tmout;
110 int a_wait_vfall_tmout;
111 int a_wait_bcon_tmout;
112 int a_aidl_bdis_tmout;
113 int b_ase0_brst_tmout;
114 int a_bidl_adis_tmout;
115
Li Yang0807c502011-04-18 22:01:59 +0200116 struct otg_fsm_ops *ops;
Heikki Krogerus7e062c02012-02-13 13:24:06 +0200117 struct usb_otg *otg;
Li Yang0807c502011-04-18 22:01:59 +0200118
119 /* Current usb protocol used: 0:undefine; 1:host; 2:client */
120 int protocol;
Anton Tikhomirov16e569e2013-11-26 11:46:05 +0900121 struct mutex lock;
Li Yang0807c502011-04-18 22:01:59 +0200122};
123
124struct otg_fsm_ops {
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900125 void (*chrg_vbus)(struct otg_fsm *fsm, int on);
126 void (*drv_vbus)(struct otg_fsm *fsm, int on);
127 void (*loc_conn)(struct otg_fsm *fsm, int on);
128 void (*loc_sof)(struct otg_fsm *fsm, int on);
129 void (*start_pulse)(struct otg_fsm *fsm);
Anton Tikhomirovec049962013-10-03 12:42:04 +0900130 void (*start_adp_prb)(struct otg_fsm *fsm);
131 void (*start_adp_sns)(struct otg_fsm *fsm);
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900132 void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
133 void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer);
Li Yang0807c502011-04-18 22:01:59 +0200134 int (*start_host)(struct otg_fsm *fsm, int on);
135 int (*start_gadget)(struct otg_fsm *fsm, int on);
136};
137
138
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900139static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200140{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900141 if (!fsm->ops->chrg_vbus)
142 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900143 fsm->ops->chrg_vbus(fsm, on);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900144 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200145}
146
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900147static inline int otg_drv_vbus(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200148{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900149 if (!fsm->ops->drv_vbus)
150 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200151 if (fsm->drv_vbus != on) {
152 fsm->drv_vbus = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900153 fsm->ops->drv_vbus(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200154 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900155 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200156}
157
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900158static inline int otg_loc_conn(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200159{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900160 if (!fsm->ops->loc_conn)
161 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200162 if (fsm->loc_conn != on) {
163 fsm->loc_conn = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900164 fsm->ops->loc_conn(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200165 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900166 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200167}
168
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900169static inline int otg_loc_sof(struct otg_fsm *fsm, int on)
Li Yang0807c502011-04-18 22:01:59 +0200170{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900171 if (!fsm->ops->loc_sof)
172 return -EOPNOTSUPP;
Li Yang0807c502011-04-18 22:01:59 +0200173 if (fsm->loc_sof != on) {
174 fsm->loc_sof = on;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900175 fsm->ops->loc_sof(fsm, on);
Li Yang0807c502011-04-18 22:01:59 +0200176 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900177 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200178}
179
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900180static inline int otg_start_pulse(struct otg_fsm *fsm)
Li Yang0807c502011-04-18 22:01:59 +0200181{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900182 if (!fsm->ops->start_pulse)
183 return -EOPNOTSUPP;
Anton Tikhomirovec049962013-10-03 12:42:04 +0900184 if (!fsm->data_pulse) {
185 fsm->data_pulse = 1;
186 fsm->ops->start_pulse(fsm);
187 }
188 return 0;
189}
190
191static inline int otg_start_adp_prb(struct otg_fsm *fsm)
192{
193 if (!fsm->ops->start_adp_prb)
194 return -EOPNOTSUPP;
195 if (!fsm->adp_prb) {
196 fsm->adp_sns = 0;
197 fsm->adp_prb = 1;
198 fsm->ops->start_adp_prb(fsm);
199 }
200 return 0;
201}
202
203static inline int otg_start_adp_sns(struct otg_fsm *fsm)
204{
205 if (!fsm->ops->start_adp_sns)
206 return -EOPNOTSUPP;
207 if (!fsm->adp_sns) {
208 fsm->adp_sns = 1;
209 fsm->ops->start_adp_sns(fsm);
210 }
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900211 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200212}
213
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900214static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200215{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900216 if (!fsm->ops->add_timer)
217 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900218 fsm->ops->add_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900219 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200220}
221
Anton Tikhomirovf6de27e2013-10-03 12:42:04 +0900222static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer)
Li Yang0807c502011-04-18 22:01:59 +0200223{
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900224 if (!fsm->ops->del_timer)
225 return -EOPNOTSUPP;
Anton Tikhomirovda8cc162013-10-03 12:42:04 +0900226 fsm->ops->del_timer(fsm, timer);
Anton Tikhomirov737cc662013-10-03 12:42:04 +0900227 return 0;
Li Yang0807c502011-04-18 22:01:59 +0200228}
229
Anton Tikhomirov425d7102013-10-03 12:42:04 +0900230static inline int otg_start_host(struct otg_fsm *fsm, int on)
231{
232 if (!fsm->ops->start_host)
233 return -EOPNOTSUPP;
234 return fsm->ops->start_host(fsm, on);
235}
236
237static inline int otg_start_gadget(struct otg_fsm *fsm, int on)
238{
239 if (!fsm->ops->start_gadget)
240 return -EOPNOTSUPP;
241 return fsm->ops->start_gadget(fsm, on);
242}
243
Li Yang0807c502011-04-18 22:01:59 +0200244int otg_statemachine(struct otg_fsm *fsm);
Anton Tikhomirovd49dd782013-11-26 10:08:58 -0600245
246#endif /* __LINUX_USB_OTG_FSM_H */