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 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 18 | #undef VERBOSE |
| 19 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 20 | #ifdef VERBOSE |
Greg Kroah-Hartman | 523e531 | 2013-06-28 11:32:55 -0700 | [diff] [blame] | 21 | #define VDBG(fmt, args...) pr_debug("[%s] " fmt , \ |
| 22 | __func__, ## args) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 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 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 37 | enum otg_fsm_timer { |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 38 | /* Standard OTG timers */ |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 39 | A_WAIT_VRISE, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 40 | A_WAIT_VFALL, |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 41 | A_WAIT_BCON, |
| 42 | A_AIDL_BDIS, |
| 43 | B_ASE0_BRST, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 44 | A_BIDL_ADIS, |
| 45 | |
| 46 | /* Auxiliary timers */ |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 47 | B_SE0_SRP, |
| 48 | B_SRP_FAIL, |
| 49 | A_WAIT_ENUM, |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 50 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 51 | NUM_OTG_FSM_TIMERS, |
| 52 | }; |
| 53 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 54 | /* OTG state machine according to the OTG spec */ |
| 55 | struct otg_fsm { |
| 56 | /* Input */ |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 57 | int a_bus_drop; |
| 58 | int a_bus_req; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 59 | int a_bus_resume; |
| 60 | int a_bus_suspend; |
| 61 | int a_conn; |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 62 | int b_bus_req; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 63 | int a_sess_vld; |
| 64 | int a_srp_det; |
| 65 | int a_vbus_vld; |
| 66 | int b_bus_resume; |
| 67 | int b_bus_suspend; |
| 68 | int b_conn; |
| 69 | int b_se0_srp; |
| 70 | int b_sess_end; |
| 71 | int b_sess_vld; |
| 72 | int id; |
| 73 | |
| 74 | /* Internal variables */ |
| 75 | int a_set_b_hnp_en; |
| 76 | int b_srp_done; |
| 77 | int b_hnp_enable; |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 78 | int a_clr_err; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 79 | |
| 80 | /* Timeout indicator for timers */ |
| 81 | int a_wait_vrise_tmout; |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 82 | int a_wait_vfall_tmout; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 83 | int a_wait_bcon_tmout; |
| 84 | int a_aidl_bdis_tmout; |
| 85 | int b_ase0_brst_tmout; |
Anton Tikhomirov | 3294908 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 86 | int a_bidl_adis_tmout; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 87 | |
| 88 | /* Informative variables */ |
Anton Tikhomirov | cff4dab | 2013-10-03 12:42:04 +0900 | [diff] [blame^] | 89 | int a_bus_drop_inf; |
| 90 | int a_bus_req_inf; |
| 91 | int a_clr_err_inf; |
| 92 | int a_suspend_req_inf; |
| 93 | int b_bus_req_inf; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 94 | |
| 95 | /* Output */ |
| 96 | int drv_vbus; |
| 97 | int loc_conn; |
| 98 | int loc_sof; |
| 99 | |
| 100 | struct otg_fsm_ops *ops; |
Heikki Krogerus | 7e062c0 | 2012-02-13 13:24:06 +0200 | [diff] [blame] | 101 | struct usb_otg *otg; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 102 | |
| 103 | /* Current usb protocol used: 0:undefine; 1:host; 2:client */ |
| 104 | int protocol; |
| 105 | spinlock_t lock; |
| 106 | }; |
| 107 | |
| 108 | struct otg_fsm_ops { |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 109 | void (*chrg_vbus)(struct otg_fsm *fsm, int on); |
| 110 | void (*drv_vbus)(struct otg_fsm *fsm, int on); |
| 111 | void (*loc_conn)(struct otg_fsm *fsm, int on); |
| 112 | void (*loc_sof)(struct otg_fsm *fsm, int on); |
| 113 | void (*start_pulse)(struct otg_fsm *fsm); |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 114 | void (*add_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); |
| 115 | void (*del_timer)(struct otg_fsm *fsm, enum otg_fsm_timer timer); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 116 | int (*start_host)(struct otg_fsm *fsm, int on); |
| 117 | int (*start_gadget)(struct otg_fsm *fsm, int on); |
| 118 | }; |
| 119 | |
| 120 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 121 | static inline int otg_chrg_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 122 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 123 | if (!fsm->ops->chrg_vbus) |
| 124 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 125 | fsm->ops->chrg_vbus(fsm, on); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 126 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 129 | static inline int otg_drv_vbus(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 130 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 131 | if (!fsm->ops->drv_vbus) |
| 132 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 133 | if (fsm->drv_vbus != on) { |
| 134 | fsm->drv_vbus = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 135 | fsm->ops->drv_vbus(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 136 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 137 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 140 | static inline int otg_loc_conn(struct otg_fsm *fsm, int on) |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 141 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 142 | if (!fsm->ops->loc_conn) |
| 143 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 144 | if (fsm->loc_conn != on) { |
| 145 | fsm->loc_conn = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 146 | fsm->ops->loc_conn(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 147 | } |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 148 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 151 | static inline int otg_loc_sof(struct otg_fsm *fsm, int 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 | if (!fsm->ops->loc_sof) |
| 154 | return -EOPNOTSUPP; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 155 | if (fsm->loc_sof != on) { |
| 156 | fsm->loc_sof = on; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 157 | fsm->ops->loc_sof(fsm, on); |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 158 | } |
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_start_pulse(struct otg_fsm *fsm) |
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->start_pulse) |
| 165 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 166 | fsm->ops->start_pulse(fsm); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 167 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Anton Tikhomirov | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 170 | 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] | 171 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 172 | if (!fsm->ops->add_timer) |
| 173 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 174 | fsm->ops->add_timer(fsm, timer); |
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 | f6de27e | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 178 | 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] | 179 | { |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 180 | if (!fsm->ops->del_timer) |
| 181 | return -EOPNOTSUPP; |
Anton Tikhomirov | da8cc16 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 182 | fsm->ops->del_timer(fsm, timer); |
Anton Tikhomirov | 737cc66 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 183 | return 0; |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Anton Tikhomirov | 425d710 | 2013-10-03 12:42:04 +0900 | [diff] [blame] | 186 | static inline int otg_start_host(struct otg_fsm *fsm, int on) |
| 187 | { |
| 188 | if (!fsm->ops->start_host) |
| 189 | return -EOPNOTSUPP; |
| 190 | return fsm->ops->start_host(fsm, on); |
| 191 | } |
| 192 | |
| 193 | static inline int otg_start_gadget(struct otg_fsm *fsm, int on) |
| 194 | { |
| 195 | if (!fsm->ops->start_gadget) |
| 196 | return -EOPNOTSUPP; |
| 197 | return fsm->ops->start_gadget(fsm, on); |
| 198 | } |
| 199 | |
Li Yang | 0807c50 | 2011-04-18 22:01:59 +0200 | [diff] [blame] | 200 | int otg_statemachine(struct otg_fsm *fsm); |