From ad2887717164edd1a6b296117a239957f8d48c5e Mon Sep 17 00:00:00 2001 From: Jett Rink Date: Wed, 4 Sep 2019 09:58:03 -0600 Subject: usb: clean up statemachine pausing Remove recursive call for resuming after a pause. Reset the state machine by zero'ing out the statemachine context instead of existing the more recent state (which can have other unintended consequences) BRANCH=none BUG=none TEST=builds, pass units tests Change-Id: I107ad6cf158b84fe201cc80a51ee11f8784e9b24 Signed-off-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1783531 Commit-Queue: Denis Brockus Reviewed-by: Denis Brockus --- common/usbc/usb_pe_ctvpd_sm.c | 24 ++++++++++-------------- common/usbc/usb_prl_sm.c | 38 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/common/usbc/usb_pe_ctvpd_sm.c b/common/usbc/usb_pe_ctvpd_sm.c index 90e22fb78c..f7db231907 100644 --- a/common/usbc/usb_pe_ctvpd_sm.c +++ b/common/usbc/usb_pe_ctvpd_sm.c @@ -46,7 +46,10 @@ static void set_state_pe(const int port, enum usb_pe_state new_state) void pe_init(int port) { + const struct sm_ctx cleared = {}; + pe[port].flags = 0; + pe[port].ctx = cleared; set_state_pe(port, PE_REQUEST); } @@ -55,26 +58,19 @@ void pe_run(int port, int evt, int en) static enum sm_local_state local_state[CONFIG_USB_PD_PORT_COUNT]; switch (local_state[port]) { + case SM_PAUSED: + if (!en) + break; + /* fall through */ case SM_INIT: pe_init(port); local_state[port] = SM_RUN; /* fall through */ case SM_RUN: - if (!en) { - /* Exit all states and pause state machine. */ - set_state(port, &pe[port].ctx, NULL); + if (en) + exe_state(port, &pe[port].ctx); + else local_state[port] = SM_PAUSED; - break; - } - - exe_state(port, &pe[port].ctx); - break; - case SM_PAUSED: - if (en) { - /* Restart state machine right now. */ - local_state[port] = SM_INIT; - pe_run(port, evt, en); - } break; } } diff --git a/common/usbc/usb_prl_sm.c b/common/usbc/usb_prl_sm.c index 11cadb9614..546506ad0f 100644 --- a/common/usbc/usb_prl_sm.c +++ b/common/usbc/usb_prl_sm.c @@ -264,6 +264,7 @@ void prl_execute_hard_reset(int port) static void prl_init(int port) { int i; + const struct sm_ctx cleared = {}; prl_tx[port].flags = 0; prl_tx[port].xmit_status = TCPC_TX_UNSET; @@ -286,9 +287,17 @@ static void prl_init(int port) prl_tx[port].msg_id_counter[i] = 0; } + /* Clear state machines and set initial states */ + prl_tx[port].ctx = cleared; set_state_prl_tx(port, PRL_TX_PHY_LAYER_RESET); + + rch[port].ctx = cleared; set_state_rch(port, RCH_WAIT_FOR_MESSAGE_FROM_PROTOCOL_LAYER); + + tch[port].ctx = cleared; set_state_tch(port, TCH_WAIT_FOR_MESSAGE_REQUEST_FROM_PE); + + prl_hr[port].ctx = cleared; set_state_prl_hr(port, PRL_HR_WAIT_FOR_REQUEST); } @@ -348,6 +357,10 @@ void prl_send_ext_data_msg(int port, void prl_run(int port, int evt, int en) { switch (local_state[port]) { + case SM_PAUSED: + if (!en) + break; + /* fall through */ case SM_INIT: prl_init(port); local_state[port] = SM_RUN; @@ -356,20 +369,13 @@ void prl_run(int port, int evt, int en) /* If disabling, wait until message is sent. */ if (!en && tch_get_state(port) == TCH_WAIT_FOR_MESSAGE_REQUEST_FROM_PE) { + /* Disable RX */ -#if defined(CONFIG_USB_TYPEC_CTVPD) || defined(CONFIG_USB_TYPEC_VPD) - vpd_rx_enable(0); -#else - tcpm_set_rx_enable(port, 0); -#endif - /* - * While we are paused, exit all states and wait until - * initialized again. - */ - set_state(port, &prl_tx[port].ctx, NULL); - set_state(port, &rch[port].ctx, NULL); - set_state(port, &tch[port].ctx, NULL); - set_state(port, &prl_hr[port].ctx, NULL); + if (IS_ENABLED(CONFIG_USB_TYPEC_CTVPD) || + IS_ENABLED(CONFIG_USB_TYPEC_VPD)) + vpd_rx_enable(0); + else + tcpm_set_rx_enable(port, 0); local_state[port] = SM_PAUSED; break; @@ -390,12 +396,6 @@ void prl_run(int port, int evt, int en) /* Run Protocol Layer Hard Reset state machine */ exe_state(port, &prl_hr[port].ctx); break; - case SM_PAUSED: - if (en) { - local_state[port] = SM_INIT; - prl_run(port, evt, en); - } - break; } } -- cgit v1.2.1