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