diff options
-rw-r--r-- | common/usbc/usb_pe_drp_sm.c | 126 |
1 files changed, 34 insertions, 92 deletions
diff --git a/common/usbc/usb_pe_drp_sm.c b/common/usbc/usb_pe_drp_sm.c index 4883f7e71f..0474cb1747 100644 --- a/common/usbc/usb_pe_drp_sm.c +++ b/common/usbc/usb_pe_drp_sm.c @@ -651,6 +651,14 @@ int pd_get_vdo_ver(int port, enum tcpm_transmit_type type) return VDM_VER20; } +static void pe_set_ready_state(int port) +{ + if (pe[port].power_role == PD_ROLE_SOURCE) + set_state_pe(port, PE_SRC_READY); + else + set_state_pe(port, PE_SNK_READY); +} + static inline void send_data_msg(int port, enum tcpm_transmit_type type, enum pd_data_msg_type msg) { @@ -1011,10 +1019,7 @@ void pe_report_error(int port, enum pe_error e, enum tcpm_transmit_type type) * Error during an Interruptible AMS. */ else { - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); } } @@ -1770,14 +1775,11 @@ static void pe_src_send_capabilities_run(int port) * PE_SNK/SRC_READY if explicit contract * PE_SEND_SOFT_RESET otherwise */ - if (PE_CHK_FLAG(port, PE_FLAGS_EXPLICIT_CONTRACT)) { - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); - } else { + if (PE_CHK_FLAG(port, PE_FLAGS_EXPLICIT_CONTRACT)) + pe_set_ready_state(port); + else pe_send_soft_reset(port, TCPC_TX_SOP); - } + return; } @@ -3132,11 +3134,8 @@ static void pe_send_not_supported_run(int port) { if (PE_CHK_FLAG(port, PE_FLAGS_TX_COMPLETE)) { PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE); + pe_set_ready_state(port); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); } } @@ -3245,10 +3244,7 @@ static void pe_give_battery_cap_run(int port) { if (PE_CHK_FLAG(port, PE_FLAGS_TX_COMPLETE)) { PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } @@ -3337,12 +3333,8 @@ static void pe_send_alert_entry(int port) print_current_state(port); - if (pd_build_alert_msg(msg, len, pe[port].power_role) != EC_SUCCESS) { - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); - } + if (pd_build_alert_msg(msg, len, pe[port].power_role) != EC_SUCCESS) + pe_set_ready_state(port); /* Request the Protocol Layer to send Alert Message. */ send_data_msg(port, TCPC_TX_SOP, PD_DATA_ALERT); @@ -3352,10 +3344,7 @@ static void pe_send_alert_run(int port) { if (PE_CHK_FLAG(port, PE_FLAGS_TX_COMPLETE)) { PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } #endif /* CONFIG_USB_PD_REV30 */ @@ -3398,10 +3387,7 @@ static void pe_drs_evaluate_swap_run(int port) * Message sent. Transition back to PE_SRC_Ready or * PE_SNK_Ready. */ - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } } @@ -3437,10 +3423,7 @@ static void pe_drs_change_run(int port) * Port changed. Transition back to PE_SRC_Ready or * PE_SNK_Ready. */ - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); } /** @@ -3490,10 +3473,7 @@ static void pe_drs_send_swap_run(int port) PE_SET_FLAG(port, PE_FLAGS_WAITING_DR_SWAP); - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); return; } } @@ -3504,10 +3484,7 @@ static void pe_drs_send_swap_run(int port) * 1) the SenderResponseTimer times out. */ if (get_time().val > pe[port].sender_response_timer) { - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); return; } } @@ -4121,10 +4098,7 @@ static void pe_snk_give_sink_cap_run(int port) { if (PE_CHK_FLAG(port, PE_FLAGS_TX_COMPLETE)) { PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } @@ -4177,10 +4151,7 @@ static void pe_handle_custom_vdm_request_run(int port) * Message sent. Transition back to * PE_SRC_Ready or PE_SINK_Ready */ - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } @@ -4280,10 +4251,7 @@ static void pe_vdm_send_request_run(int port) * Go back to ready on first AMS message discard * (ready states will clear the discard flag) */ - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); return; } @@ -4804,10 +4772,7 @@ static void pe_vdm_response_entry(int port) if (cmd_type != CMDT_INIT) { CPRINTF("ERR:CMDT:%d:%d\n", cmd_type, vdo_cmd); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); return; } @@ -4840,10 +4805,7 @@ static void pe_vdm_response_entry(int port) * (just goodCRC) return zero here. */ dfp_consume_attention(port, rx_payload); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); return; #endif default: @@ -4904,10 +4866,7 @@ static void pe_vdm_response_run(int port) PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE | PE_FLAGS_PROTOCOL_ERROR); - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } @@ -4977,11 +4936,7 @@ static void pe_vcs_evaluate_swap_run(int port) * Message sent. Transition back to PE_SRC_Ready or * PE_SINK_Ready */ - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); - + pe_set_ready_state(port); } } } @@ -5037,10 +4992,7 @@ static void pe_vcs_send_swap_run(int port) * 3) Wait message Received. */ if (type == PD_CTRL_REJECT || type == PD_CTRL_WAIT) { - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); return; } } @@ -5059,12 +5011,8 @@ static void pe_vcs_send_swap_run(int port) * PE_SNK_Ready state when: * 1) SenderResponseTimer Timeout */ - if (get_time().val > pe[port].sender_response_timer) { - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); - } + if (get_time().val > pe[port].sender_response_timer) + pe_set_ready_state(port); } /* @@ -5245,10 +5193,7 @@ static void pe_vcs_send_ps_rdy_swap_run(int port) * Cable plug wasn't present, * return to ready state */ - if (pe[port].power_role == PD_ROLE_SOURCE) - set_state_pe(port, PE_SRC_READY); - else - set_state_pe(port, PE_SNK_READY); + pe_set_ready_state(port); } } } @@ -5357,10 +5302,7 @@ static void pe_sender_response_run(int port) * Go back to ready on first AMS message discard * (ready states will clear the discard flag) */ - if (pe[port].power_role == PD_ROLE_SINK) - set_state_pe(port, PE_SNK_READY); - else - set_state_pe(port, PE_SRC_READY); + pe_set_ready_state(port); return; } |