Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 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 | |
Anton Tikhomirov | d49dd78 | 2013-11-26 10:08:58 -0600 | [diff] [blame] | 18 | #ifndef __LINUX_USB_OTG_FSM_H |
| 19 | #define __LINUX_USB_OTG_FSM_H |
| 20 | |
Anton Tikhomirov | 16e569e | 2013-11-26 11:46:05 +0900 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Felipe Balbi | cc9d9cc | 2013-11-26 10:06:00 -0600 | [diff] [blame] | 22 | #include <linux/errno.h> |
| 23 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 24 | #undef VERBOSE |
| 25 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 26 | #ifdef VERBOSE |
Greg Kroah-Hartman | 523e531 | 2013-06-28 11:32:55 -0700 | [diff] [blame] | 27 | #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \ |
| 28 | __func__, ## args) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 29 | #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 Jun | ae57e97 | 2016-02-19 10:04:42 +0800 | [diff] [blame] | 43 | #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 Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 55 | enum otg_fsm_timer { |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 56 | /* Standard OTG timers */ |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 57 | A_WAIT_VRISE, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 58 | A_WAIT_VFALL, |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 59 | A_WAIT_BCON, |
| 60 | A_AIDL_BDIS, |
| 61 | B_ASE0_BRST, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 62 | A_BIDL_ADIS, |
Li Jun | 9c527f4 | 2016-02-19 10:04:48 +0800 | [diff] [blame] | 63 | B_AIDL_BDIS, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 64 | |
| 65 | /* Auxiliary timers */ |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 66 | B_SE0_SRP, |
| 67 | B_SRP_FAIL, |
| 68 | A_WAIT_ENUM, |
Li Jun | 2f8a467 | 2015-03-20 16:28:05 +0800 | [diff] [blame] | 69 | B_DATA_PLS, |
| 70 | B_SSEND_SRP, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 71 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 72 | NUM_OTG_FSM_TIMERS, |
| 73 | }; |
| 74 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 75 | /* OTG state machine according to the OTG spec */ |
| 76 | struct otg_fsm { |
| 77 | /* Input */ |
Anton Tikhomirov | 4662e5e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 78 | int id; |
Anton Tikhomirov | ec04996 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 79 | int adp_change; |
| 80 | int power_up; |
| 81 | int test_device; |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 82 | int a_bus_drop; |
| 83 | int a_bus_req; |
Anton Tikhomirov | 4662e5e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 84 | int a_srp_det; |
| 85 | int a_vbus_vld; |
| 86 | int b_conn; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 87 | int a_bus_resume; |
| 88 | int a_bus_suspend; |
| 89 | int a_conn; |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 90 | int b_bus_req; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 91 | int b_se0_srp; |
Anton Tikhomirov | 6804178 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 92 | int b_ssend_srp; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 93 | int b_sess_vld; |
Anton Tikhomirov | 4662e5e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 94 | /* Auxilary inputs */ |
| 95 | int a_sess_vld; |
| 96 | int b_bus_resume; |
| 97 | int b_bus_suspend; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 98 | |
| 99 | /* Output */ |
Anton Tikhomirov | ec04996 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 100 | int data_pulse; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 101 | int drv_vbus; |
| 102 | int loc_conn; |
| 103 | int loc_sof; |
Anton Tikhomirov | ec04996 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 104 | int adp_prb; |
| 105 | int adp_sns; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 106 | |
Anton Tikhomirov | 4662e5e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 107 | /* Internal variables */ |
| 108 | int a_set_b_hnp_en; |
| 109 | int b_srp_done; |
| 110 | int b_hnp_enable; |
| 111 | int a_clr_err; |
| 112 | |
| 113 | /* Informative variables */ |
| 114 | int a_bus_drop_inf; |
| 115 | int a_bus_req_inf; |
| 116 | int a_clr_err_inf; |
| 117 | int b_bus_req_inf; |
| 118 | /* Auxilary informative variables */ |
| 119 | int a_suspend_req_inf; |
| 120 | |
| 121 | /* Timeout indicator for timers */ |
| 122 | int a_wait_vrise_tmout; |
| 123 | int a_wait_vfall_tmout; |
| 124 | int a_wait_bcon_tmout; |
| 125 | int a_aidl_bdis_tmout; |
| 126 | int b_ase0_brst_tmout; |
| 127 | int a_bidl_adis_tmout; |
| 128 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 129 | struct otg_fsm_ops *ops; |
Heikki Krogerus | 7e062c0 | 2012-02-13 13:24:06 +0200 | [diff] [blame] | 130 | struct usb_otg *otg; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 131 | |
| 132 | /* Current usb protocol used: 0:undefine; 1:host; 2:client */ |
| 133 | int protocol; |
Anton Tikhomirov | 16e569e | 2013-11-26 11:46:05 +0900 | [diff] [blame] | 134 | struct mutex lock; |
Li Jun | ae57e97 | 2016-02-19 10:04:42 +0800 | [diff] [blame] | 135 | u8 *host_req_flag; |
| 136 | struct delayed_work hnp_polling_work; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | struct otg_fsm_ops { |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 140 | void (*chrg_vbus)(struct otg_fsm *fsm, int on); |
| 141 | void (*drv_vbus)(struct otg_fsm *fsm, int on); |
| 142 | void (*loc_conn)(struct otg_fsm *fsm, int on); |
| 143 | void (*loc_sof)(struct otg_fsm *fsm, int on); |
| 144 | void (*start_pulse)(struct otg_fsm *fsm); |
Anton Tikhomirov | ec04996 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 145 | void (*start_adp_prb)(struct otg_fsm *fsm); |
| 146 | void (*start_adp_sns)(struct otg_fsm *fsm); |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 147 | void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); |
| 148 | void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 149 | int (*start_host)(struct otg_fsm *fsm, int on); |
| 150 | int (*start_gadget)(struct otg_fsm *fsm, int on); |
| 151 | }; |
| 152 | |
| 153 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 154 | static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 155 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 156 | if (!fsm->ops->chrg_vbus) |
| 157 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 158 | fsm->ops->chrg_vbus(fsm, on); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 159 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 162 | static inline int otg_drv_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 163 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 164 | if (!fsm->ops->drv_vbus) |
| 165 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 166 | if (fsm->drv_vbus != on) { |
| 167 | fsm->drv_vbus = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 168 | fsm->ops->drv_vbus(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 169 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 170 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 173 | static inline int otg_loc_conn(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 174 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 175 | if (!fsm->ops->loc_conn) |
| 176 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 177 | if (fsm->loc_conn != on) { |
| 178 | fsm->loc_conn = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 179 | fsm->ops->loc_conn(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 180 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 181 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 184 | static inline int otg_loc_sof(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 185 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 186 | if (!fsm->ops->loc_sof) |
| 187 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 188 | if (fsm->loc_sof != on) { |
| 189 | fsm->loc_sof = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 190 | fsm->ops->loc_sof(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 191 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 192 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 195 | static inline int otg_start_pulse(struct otg_fsm *fsm) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 196 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 197 | if (!fsm->ops->start_pulse) |
| 198 | return -EOPNOTSUPP; |
Anton Tikhomirov | ec04996 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 199 | if (!fsm->data_pulse) { |
| 200 | fsm->data_pulse = 1; |
| 201 | fsm->ops->start_pulse(fsm); |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static inline int otg_start_adp_prb(struct otg_fsm *fsm) |
| 207 | { |
| 208 | if (!fsm->ops->start_adp_prb) |
| 209 | return -EOPNOTSUPP; |
| 210 | if (!fsm->adp_prb) { |
| 211 | fsm->adp_sns = 0; |
| 212 | fsm->adp_prb = 1; |
| 213 | fsm->ops->start_adp_prb(fsm); |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static inline int otg_start_adp_sns(struct otg_fsm *fsm) |
| 219 | { |
| 220 | if (!fsm->ops->start_adp_sns) |
| 221 | return -EOPNOTSUPP; |
| 222 | if (!fsm->adp_sns) { |
| 223 | fsm->adp_sns = 1; |
| 224 | fsm->ops->start_adp_sns(fsm); |
| 225 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 226 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 229 | static inline int otg_add_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 230 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 231 | if (!fsm->ops->add_timer) |
| 232 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 233 | fsm->ops->add_timer(fsm, timer); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 234 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 237 | static inline int otg_del_timer(struct otg_fsm *fsm, enum otg_fsm_timer timer) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 238 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 239 | if (!fsm->ops->del_timer) |
| 240 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 241 | fsm->ops->del_timer(fsm, timer); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 242 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Anton Tikhomirov | 425d710 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 245 | static inline int otg_start_host(struct otg_fsm *fsm, int on) |
| 246 | { |
| 247 | if (!fsm->ops->start_host) |
| 248 | return -EOPNOTSUPP; |
| 249 | return fsm->ops->start_host(fsm, on); |
| 250 | } |
| 251 | |
| 252 | static inline int otg_start_gadget(struct otg_fsm *fsm, int on) |
| 253 | { |
| 254 | if (!fsm->ops->start_gadget) |
| 255 | return -EOPNOTSUPP; |
| 256 | return fsm->ops->start_gadget(fsm, on); |
| 257 | } |
| 258 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 259 | int otg_statemachine(struct otg_fsm *fsm); |
Anton Tikhomirov | d49dd78 | 2013-11-26 10:08:58 -0600 | [diff] [blame] | 260 | |
| 261 | #endif /* __LINUX_USB_OTG_FSM_H */ |