1 /* 2 * OTG Finite State Machine from OTG spec 3 * 4 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. 5 * 6 * Author: Li Yang <LeoLi@freescale.com> 7 * Jerry Huang <Chang-Ming.Huang@freescale.com> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write to the Free Software Foundation, Inc., 21 * 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 24 #include <linux/kernel.h> 25 #include <linux/types.h> 26 #include <linux/mutex.h> 27 #include <linux/delay.h> 28 #include <linux/usb.h> 29 #include <linux/usb/gadget.h> 30 #include <linux/usb/otg.h> 31 #include <linux/usb/otg-fsm.h> 32 33 /* Change USB protocol when there is a protocol change */ 34 static int otg_set_protocol(struct otg_fsm *fsm, int protocol) 35 { 36 int ret = 0; 37 38 if (fsm->protocol != protocol) { 39 VDBG("Changing role fsm->protocol= %d; new protocol= %d\n", 40 fsm->protocol, protocol); 41 /* stop old protocol */ 42 if (fsm->protocol == PROTO_HOST) 43 ret = otg_start_host(fsm, 0); 44 else if (fsm->protocol == PROTO_GADGET) 45 ret = otg_start_gadget(fsm, 0); 46 if (ret) 47 return ret; 48 49 /* start new protocol */ 50 if (protocol == PROTO_HOST) 51 ret = otg_start_host(fsm, 1); 52 else if (protocol == PROTO_GADGET) 53 ret = otg_start_gadget(fsm, 1); 54 if (ret) 55 return ret; 56 57 fsm->protocol = protocol; 58 return 0; 59 } 60 61 return 0; 62 } 63 64 /* Called when leaving a state. Do state clean up jobs here */ 65 static void otg_leave_state(struct otg_fsm *fsm, enum usb_otg_state old_state) 66 { 67 switch (old_state) { 68 case OTG_STATE_B_IDLE: 69 otg_del_timer(fsm, B_SE0_SRP); 70 fsm->b_se0_srp = 0; 71 fsm->adp_sns = 0; 72 fsm->adp_prb = 0; 73 break; 74 case OTG_STATE_B_SRP_INIT: 75 fsm->data_pulse = 0; 76 fsm->b_srp_done = 0; 77 break; 78 case OTG_STATE_B_PERIPHERAL: 79 if (fsm->otg->gadget) 80 fsm->otg->gadget->host_request_flag = 0; 81 break; 82 case OTG_STATE_B_WAIT_ACON: 83 otg_del_timer(fsm, B_ASE0_BRST); 84 fsm->b_ase0_brst_tmout = 0; 85 break; 86 case OTG_STATE_B_HOST: 87 break; 88 case OTG_STATE_A_IDLE: 89 fsm->adp_prb = 0; 90 break; 91 case OTG_STATE_A_WAIT_VRISE: 92 otg_del_timer(fsm, A_WAIT_VRISE); 93 fsm->a_wait_vrise_tmout = 0; 94 break; 95 case OTG_STATE_A_WAIT_BCON: 96 otg_del_timer(fsm, A_WAIT_BCON); 97 fsm->a_wait_bcon_tmout = 0; 98 break; 99 case OTG_STATE_A_HOST: 100 otg_del_timer(fsm, A_WAIT_ENUM); 101 break; 102 case OTG_STATE_A_SUSPEND: 103 otg_del_timer(fsm, A_AIDL_BDIS); 104 fsm->a_aidl_bdis_tmout = 0; 105 fsm->a_suspend_req_inf = 0; 106 break; 107 case OTG_STATE_A_PERIPHERAL: 108 otg_del_timer(fsm, A_BIDL_ADIS); 109 fsm->a_bidl_adis_tmout = 0; 110 if (fsm->otg->gadget) 111 fsm->otg->gadget->host_request_flag = 0; 112 break; 113 case OTG_STATE_A_WAIT_VFALL: 114 otg_del_timer(fsm, A_WAIT_VFALL); 115 fsm->a_wait_vfall_tmout = 0; 116 otg_del_timer(fsm, A_WAIT_VRISE); 117 break; 118 case OTG_STATE_A_VBUS_ERR: 119 break; 120 default: 121 break; 122 } 123 } 124 125 static void otg_hnp_polling_work(struct work_struct *work) 126 { 127 struct otg_fsm *fsm = container_of(to_delayed_work(work), 128 struct otg_fsm, hnp_polling_work); 129 struct usb_device *udev; 130 enum usb_otg_state state = fsm->otg->state; 131 u8 flag; 132 int retval; 133 134 if (state != OTG_STATE_A_HOST && state != OTG_STATE_B_HOST) 135 return; 136 137 udev = usb_hub_find_child(fsm->otg->host->root_hub, 1); 138 if (!udev) { 139 dev_err(fsm->otg->host->controller, 140 "no usb dev connected, can't start HNP polling\n"); 141 return; 142 } 143 144 *fsm->host_req_flag = 0; 145 /* Get host request flag from connected USB device */ 146 retval = usb_control_msg(udev, 147 usb_rcvctrlpipe(udev, 0), 148 USB_REQ_GET_STATUS, 149 USB_DIR_IN | USB_RECIP_DEVICE, 150 0, 151 OTG_STS_SELECTOR, 152 fsm->host_req_flag, 153 1, 154 USB_CTRL_GET_TIMEOUT); 155 if (retval != 1) { 156 dev_err(&udev->dev, "Get one byte OTG status failed\n"); 157 return; 158 } 159 160 flag = *fsm->host_req_flag; 161 if (flag == 0) { 162 /* Continue HNP polling */ 163 schedule_delayed_work(&fsm->hnp_polling_work, 164 msecs_to_jiffies(T_HOST_REQ_POLL)); 165 return; 166 } else if (flag != HOST_REQUEST_FLAG) { 167 dev_err(&udev->dev, "host request flag %d is invalid\n", flag); 168 return; 169 } 170 171 /* Host request flag is set */ 172 if (state == OTG_STATE_A_HOST) { 173 /* Set b_hnp_enable */ 174 if (!fsm->otg->host->b_hnp_enable) { 175 retval = usb_control_msg(udev, 176 usb_sndctrlpipe(udev, 0), 177 USB_REQ_SET_FEATURE, 0, 178 USB_DEVICE_B_HNP_ENABLE, 179 0, NULL, 0, 180 USB_CTRL_SET_TIMEOUT); 181 if (retval >= 0) 182 fsm->otg->host->b_hnp_enable = 1; 183 } 184 fsm->a_bus_req = 0; 185 } else if (state == OTG_STATE_B_HOST) { 186 fsm->b_bus_req = 0; 187 } 188 189 otg_statemachine(fsm); 190 } 191 192 static void otg_start_hnp_polling(struct otg_fsm *fsm) 193 { 194 /* 195 * The memory of host_req_flag should be allocated by 196 * controller driver, otherwise, hnp polling is not started. 197 */ 198 if (!fsm->host_req_flag) 199 return; 200 201 INIT_DELAYED_WORK(&fsm->hnp_polling_work, otg_hnp_polling_work); 202 schedule_delayed_work(&fsm->hnp_polling_work, 203 msecs_to_jiffies(T_HOST_REQ_POLL)); 204 } 205 206 /* Called when entering a state */ 207 static int otg_set_state(struct otg_fsm *fsm, enum usb_otg_state new_state) 208 { 209 if (fsm->otg->state == new_state) 210 return 0; 211 VDBG("Set state: %s\n", usb_otg_state_string(new_state)); 212 otg_leave_state(fsm, fsm->otg->state); 213 switch (new_state) { 214 case OTG_STATE_B_IDLE: 215 otg_drv_vbus(fsm, 0); 216 otg_chrg_vbus(fsm, 0); 217 otg_loc_conn(fsm, 0); 218 otg_loc_sof(fsm, 0); 219 /* 220 * Driver is responsible for starting ADP probing 221 * if ADP sensing times out. 222 */ 223 otg_start_adp_sns(fsm); 224 otg_set_protocol(fsm, PROTO_UNDEF); 225 otg_add_timer(fsm, B_SE0_SRP); 226 break; 227 case OTG_STATE_B_SRP_INIT: 228 otg_start_pulse(fsm); 229 otg_loc_sof(fsm, 0); 230 otg_set_protocol(fsm, PROTO_UNDEF); 231 otg_add_timer(fsm, B_SRP_FAIL); 232 break; 233 case OTG_STATE_B_PERIPHERAL: 234 otg_chrg_vbus(fsm, 0); 235 otg_loc_sof(fsm, 0); 236 otg_set_protocol(fsm, PROTO_GADGET); 237 otg_loc_conn(fsm, 1); 238 break; 239 case OTG_STATE_B_WAIT_ACON: 240 otg_chrg_vbus(fsm, 0); 241 otg_loc_conn(fsm, 0); 242 otg_loc_sof(fsm, 0); 243 otg_set_protocol(fsm, PROTO_HOST); 244 otg_add_timer(fsm, B_ASE0_BRST); 245 fsm->a_bus_suspend = 0; 246 break; 247 case OTG_STATE_B_HOST: 248 otg_chrg_vbus(fsm, 0); 249 otg_loc_conn(fsm, 0); 250 otg_loc_sof(fsm, 1); 251 otg_set_protocol(fsm, PROTO_HOST); 252 usb_bus_start_enum(fsm->otg->host, 253 fsm->otg->host->otg_port); 254 otg_start_hnp_polling(fsm); 255 break; 256 case OTG_STATE_A_IDLE: 257 otg_drv_vbus(fsm, 0); 258 otg_chrg_vbus(fsm, 0); 259 otg_loc_conn(fsm, 0); 260 otg_loc_sof(fsm, 0); 261 otg_start_adp_prb(fsm); 262 otg_set_protocol(fsm, PROTO_HOST); 263 break; 264 case OTG_STATE_A_WAIT_VRISE: 265 otg_drv_vbus(fsm, 1); 266 otg_loc_conn(fsm, 0); 267 otg_loc_sof(fsm, 0); 268 otg_set_protocol(fsm, PROTO_HOST); 269 otg_add_timer(fsm, A_WAIT_VRISE); 270 break; 271 case OTG_STATE_A_WAIT_BCON: 272 otg_drv_vbus(fsm, 1); 273 otg_loc_conn(fsm, 0); 274 otg_loc_sof(fsm, 0); 275 otg_set_protocol(fsm, PROTO_HOST); 276 otg_add_timer(fsm, A_WAIT_BCON); 277 break; 278 case OTG_STATE_A_HOST: 279 otg_drv_vbus(fsm, 1); 280 otg_loc_conn(fsm, 0); 281 otg_loc_sof(fsm, 1); 282 otg_set_protocol(fsm, PROTO_HOST); 283 /* 284 * When HNP is triggered while a_bus_req = 0, a_host will 285 * suspend too fast to complete a_set_b_hnp_en 286 */ 287 if (!fsm->a_bus_req || fsm->a_suspend_req_inf) 288 otg_add_timer(fsm, A_WAIT_ENUM); 289 otg_start_hnp_polling(fsm); 290 break; 291 case OTG_STATE_A_SUSPEND: 292 otg_drv_vbus(fsm, 1); 293 otg_loc_conn(fsm, 0); 294 otg_loc_sof(fsm, 0); 295 otg_set_protocol(fsm, PROTO_HOST); 296 otg_add_timer(fsm, A_AIDL_BDIS); 297 298 break; 299 case OTG_STATE_A_PERIPHERAL: 300 otg_loc_sof(fsm, 0); 301 otg_set_protocol(fsm, PROTO_GADGET); 302 otg_drv_vbus(fsm, 1); 303 otg_loc_conn(fsm, 1); 304 otg_add_timer(fsm, A_BIDL_ADIS); 305 break; 306 case OTG_STATE_A_WAIT_VFALL: 307 otg_drv_vbus(fsm, 0); 308 otg_loc_conn(fsm, 0); 309 otg_loc_sof(fsm, 0); 310 otg_set_protocol(fsm, PROTO_HOST); 311 otg_add_timer(fsm, A_WAIT_VFALL); 312 break; 313 case OTG_STATE_A_VBUS_ERR: 314 otg_drv_vbus(fsm, 0); 315 otg_loc_conn(fsm, 0); 316 otg_loc_sof(fsm, 0); 317 otg_set_protocol(fsm, PROTO_UNDEF); 318 break; 319 default: 320 break; 321 } 322 323 fsm->otg->state = new_state; 324 fsm->state_changed = 1; 325 return 0; 326 } 327 328 /* State change judgement */ 329 int otg_statemachine(struct otg_fsm *fsm) 330 { 331 enum usb_otg_state state; 332 333 mutex_lock(&fsm->lock); 334 335 state = fsm->otg->state; 336 fsm->state_changed = 0; 337 /* State machine state change judgement */ 338 339 switch (state) { 340 case OTG_STATE_UNDEFINED: 341 VDBG("fsm->id = %d\n", fsm->id); 342 if (fsm->id) 343 otg_set_state(fsm, OTG_STATE_B_IDLE); 344 else 345 otg_set_state(fsm, OTG_STATE_A_IDLE); 346 break; 347 case OTG_STATE_B_IDLE: 348 if (!fsm->id) 349 otg_set_state(fsm, OTG_STATE_A_IDLE); 350 else if (fsm->b_sess_vld && fsm->otg->gadget) 351 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL); 352 else if ((fsm->b_bus_req || fsm->adp_change || fsm->power_up) && 353 fsm->b_ssend_srp && fsm->b_se0_srp) 354 otg_set_state(fsm, OTG_STATE_B_SRP_INIT); 355 break; 356 case OTG_STATE_B_SRP_INIT: 357 if (!fsm->id || fsm->b_srp_done) 358 otg_set_state(fsm, OTG_STATE_B_IDLE); 359 break; 360 case OTG_STATE_B_PERIPHERAL: 361 if (!fsm->id || !fsm->b_sess_vld) 362 otg_set_state(fsm, OTG_STATE_B_IDLE); 363 else if (fsm->b_bus_req && fsm->otg-> 364 gadget->b_hnp_enable && fsm->a_bus_suspend) 365 otg_set_state(fsm, OTG_STATE_B_WAIT_ACON); 366 break; 367 case OTG_STATE_B_WAIT_ACON: 368 if (fsm->a_conn) 369 otg_set_state(fsm, OTG_STATE_B_HOST); 370 else if (!fsm->id || !fsm->b_sess_vld) 371 otg_set_state(fsm, OTG_STATE_B_IDLE); 372 else if (fsm->a_bus_resume || fsm->b_ase0_brst_tmout) { 373 fsm->b_ase0_brst_tmout = 0; 374 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL); 375 } 376 break; 377 case OTG_STATE_B_HOST: 378 if (!fsm->id || !fsm->b_sess_vld) 379 otg_set_state(fsm, OTG_STATE_B_IDLE); 380 else if (!fsm->b_bus_req || !fsm->a_conn || fsm->test_device) 381 otg_set_state(fsm, OTG_STATE_B_PERIPHERAL); 382 break; 383 case OTG_STATE_A_IDLE: 384 if (fsm->id) 385 otg_set_state(fsm, OTG_STATE_B_IDLE); 386 else if (!fsm->a_bus_drop && (fsm->a_bus_req || 387 fsm->a_srp_det || fsm->adp_change || fsm->power_up)) 388 otg_set_state(fsm, OTG_STATE_A_WAIT_VRISE); 389 break; 390 case OTG_STATE_A_WAIT_VRISE: 391 if (fsm->a_vbus_vld) 392 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); 393 else if (fsm->id || fsm->a_bus_drop || 394 fsm->a_wait_vrise_tmout) 395 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 396 break; 397 case OTG_STATE_A_WAIT_BCON: 398 if (!fsm->a_vbus_vld) 399 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 400 else if (fsm->b_conn) 401 otg_set_state(fsm, OTG_STATE_A_HOST); 402 else if (fsm->id || fsm->a_bus_drop || fsm->a_wait_bcon_tmout) 403 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 404 break; 405 case OTG_STATE_A_HOST: 406 if (fsm->id || fsm->a_bus_drop) 407 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 408 else if ((!fsm->a_bus_req || fsm->a_suspend_req_inf) && 409 fsm->otg->host->b_hnp_enable) 410 otg_set_state(fsm, OTG_STATE_A_SUSPEND); 411 else if (!fsm->b_conn) 412 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); 413 else if (!fsm->a_vbus_vld) 414 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 415 break; 416 case OTG_STATE_A_SUSPEND: 417 if (!fsm->b_conn && fsm->otg->host->b_hnp_enable) 418 otg_set_state(fsm, OTG_STATE_A_PERIPHERAL); 419 else if (!fsm->b_conn && !fsm->otg->host->b_hnp_enable) 420 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); 421 else if (fsm->a_bus_req || fsm->b_bus_resume) 422 otg_set_state(fsm, OTG_STATE_A_HOST); 423 else if (fsm->id || fsm->a_bus_drop || fsm->a_aidl_bdis_tmout) 424 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 425 else if (!fsm->a_vbus_vld) 426 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 427 break; 428 case OTG_STATE_A_PERIPHERAL: 429 if (fsm->id || fsm->a_bus_drop) 430 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 431 else if (fsm->a_bidl_adis_tmout || fsm->b_bus_suspend) 432 otg_set_state(fsm, OTG_STATE_A_WAIT_BCON); 433 else if (!fsm->a_vbus_vld) 434 otg_set_state(fsm, OTG_STATE_A_VBUS_ERR); 435 break; 436 case OTG_STATE_A_WAIT_VFALL: 437 if (fsm->a_wait_vfall_tmout) 438 otg_set_state(fsm, OTG_STATE_A_IDLE); 439 break; 440 case OTG_STATE_A_VBUS_ERR: 441 if (fsm->id || fsm->a_bus_drop || fsm->a_clr_err) 442 otg_set_state(fsm, OTG_STATE_A_WAIT_VFALL); 443 break; 444 default: 445 break; 446 } 447 mutex_unlock(&fsm->lock); 448 449 VDBG("quit statemachine, changed = %d\n", fsm->state_changed); 450 return fsm->state_changed; 451 } 452 EXPORT_SYMBOL_GPL(otg_statemachine); 453