diff options
author | Sam Hurst <shurst@google.com> | 2019-04-18 12:47:52 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-07-30 21:25:24 +0000 |
commit | 2e3109ec6b964776e7c5bd12f1fc058ca96d33f7 (patch) | |
tree | 23ea5eaf67e50094ae683f5d16f09a332a1303c4 | |
parent | b99b1b10c3f2070e3fbd8d5cb2d0927f04f642ea (diff) | |
download | chrome-ec-2e3109ec6b964776e7c5bd12f1fc058ca96d33f7.tar.gz |
type-c: USB Type-C State Machine based on Release 1.4 of the spec.
Implements DRP with Accessory, and Try.SRC as detailed in Release
1.4 of the USB Type-C specification.
BUG=b:130895206
BRANCH=none
TEST=manual
Used Atlas device to verify that it could be charged from PD and
none PD charges at 5V/3A. Attached USB dock and verifed access
to USB Thumb drive.
Performed same tests on Hatch
Port 0 on Hatch was used to run this CL, merged with PD functionality,
on the PD2.0 Compliance tester. All tests pass except for a few
physical layer tests. The test report has been added to the bug.
Change-Id: Ic4869e20e5b4c2ba6c827d92e40c70f3140f2518
Signed-off-by: Sam Hurst <shurst@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1574667
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
Tested-by: Sam Hurst <shurst@google.com>
Commit-Queue: Sam Hurst <shurst@google.com>
-rw-r--r-- | common/build.mk | 11 | ||||
-rw-r--r-- | common/usbc/build.mk | 22 | ||||
-rw-r--r-- | common/usbc/usb_pe_ctvpd_sm.c (renamed from common/usb_pe_ctvpd_sm.c) | 16 | ||||
-rw-r--r-- | common/usbc/usb_prl_sm.c (renamed from common/usb_prl_sm.c) | 423 | ||||
-rw-r--r-- | common/usbc/usb_sm.c (renamed from common/usb_sm.c) | 42 | ||||
-rw-r--r-- | common/usbc/usb_tc_ctvpd_sm.c (renamed from common/usb_tc_ctvpd_sm.c) | 553 | ||||
-rw-r--r-- | common/usbc/usb_tc_drp_acc_trysrc_sm.c | 1724 | ||||
-rw-r--r-- | common/usbc/usb_tc_vpd_sm.c (renamed from common/usb_tc_vpd_sm.c) | 123 | ||||
-rw-r--r-- | common/usbc/usbc_task.c (renamed from common/usb_tc_sm.c) | 137 | ||||
-rw-r--r-- | include/config.h | 3 | ||||
-rw-r--r-- | include/usb_pd.h | 2 | ||||
-rw-r--r-- | include/usb_pe_sm.h | 2 | ||||
-rw-r--r-- | include/usb_prl_sm.h | 2 | ||||
-rw-r--r-- | include/usb_sm.h | 92 | ||||
-rw-r--r-- | include/usb_tc_ctvpd_sm.h | 3 | ||||
-rw-r--r-- | include/usb_tc_drp_acc_trysrc_sm.h | 85 | ||||
-rw-r--r-- | include/usb_tc_sm.h | 76 | ||||
-rw-r--r-- | include/usb_tc_vpd_sm.h | 3 | ||||
-rw-r--r-- | test/usb_prl.c | 2 | ||||
-rw-r--r-- | test/usb_sm_framework_h3.c | 394 | ||||
-rw-r--r-- | test/usb_typec_ctvpd.c | 120 |
21 files changed, 2776 insertions, 1059 deletions
diff --git a/common/build.mk b/common/build.mk index 41c2a2b213..77732b18f2 100644 --- a/common/build.mk +++ b/common/build.mk @@ -136,16 +136,6 @@ common-$(CONFIG_USB_PORT_POWER_DUMB)+=usb_port_power_dumb.o common-$(CONFIG_USB_PORT_POWER_SMART)+=usb_port_power_smart.o ifeq ($(CONFIG_USB_SM_FRAMEWORK),) common-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_protocol.o usb_pd_policy.o -else -common-$(CONFIG_USB_SM_FRAMEWORK)+=usb_sm.o -common-$(CONFIG_USB_TYPEC_SM)+=usb_tc_sm.o -common-$(CONFIG_USB_PRL_SM)+=usb_prl_sm.o -ifneq ($(CONFIG_USB_PE_SM),) -common-$(CONFIG_USB_TYPEC_VPD)+=usb_pe_ctvpd_sm.o -common-$(CONFIG_USB_TYPEC_CTVPD)+=usb_pe_ctvpd_sm.o -endif -common-$(CONFIG_USB_TYPEC_VPD)+=usb_tc_vpd_sm.o -common-$(CONFIG_USB_TYPEC_CTVPD)+=usb_tc_ctvpd_sm.o endif common-$(CONFIG_USB_PD_LOGGING)+=event_log.o pd_log.o common-$(CONFIG_USB_PD_TCPC)+=usb_pd_tcpc.o @@ -250,3 +240,4 @@ $(out)/rma_key_from_blob.h: board/$(BOARD)/$(BLOB_FILE) util/bin2h.sh endif include $(_common_dir)fpsensor/build.mk +include $(_common_dir)usbc/build.mk diff --git a/common/usbc/build.mk b/common/usbc/build.mk new file mode 100644 index 0000000000..e1a90cdb32 --- /dev/null +++ b/common/usbc/build.mk @@ -0,0 +1,22 @@ +# Copyright 2019 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Build for USB Type-C and Power Delivery + +# Note that this variable includes the trailing "/" +_usbc_dir:=$(dir $(lastword $(MAKEFILE_LIST))) + +ifneq ($(CONFIG_USB_SM_FRAMEWORK),) +all-obj-$(CONFIG_USB_SM_FRAMEWORK)+=$(_usbc_dir)usb_sm.o +all-obj-$(CONFIG_USB_TYPEC_SM)+=$(_usbc_dir)usbc_task.o +all-obj-$(CONFIG_USB_PRL_SM)+=$(_usbc_dir)usb_prl_sm.o +ifneq ($(CONFIG_USB_PE_SM),) +all-obj-$(CONFIG_USB_TYPEC_VPD)+=$(_usbc_dir)usb_pe_ctvpd_sm.o +all-obj-$(CONFIG_USB_TYPEC_CTVPD)+=$(_usbc_dir)usb_pe_ctvpd_sm.o +endif +all-obj-$(CONFIG_USB_TYPEC_VPD)+=$(_usbc_dir)usb_tc_vpd_sm.o +all-obj-$(CONFIG_USB_TYPEC_CTVPD)+=$(_usbc_dir)usb_tc_ctvpd_sm.o +all-obj-$(CONFIG_USB_TYPEC_DRP_ACC_TRYSRC)+=\ + $(_usbc_dir)usb_tc_drp_acc_trysrc_sm.o +endif diff --git a/common/usb_pe_ctvpd_sm.c b/common/usbc/usb_pe_ctvpd_sm.c index a8b1b1f0fe..fd4cbcbf0e 100644 --- a/common/usb_pe_ctvpd_sm.c +++ b/common/usbc/usb_pe_ctvpd_sm.c @@ -44,15 +44,15 @@ static struct policy_engine { } pe[CONFIG_USB_PD_PORT_COUNT]; /* Policy Engine states */ -DECLARE_STATE(pe, request, NOOP_EXIT); +DECLARE_STATE(pe, request, WITH_RUN, NOOP); void pe_init(int port) { pe[port].flags = 0; - init_state(port, PE_OBJ(port), pe_request); + sm_init_state(port, PE_OBJ(port), pe_request); } -void policy_engine(int port, int evt, int en) +void usbc_policy_engine(int port, int evt, int en) { switch (local_state) { case PE_INIT: @@ -65,7 +65,7 @@ void policy_engine(int port, int evt, int en) break; } - exe_state(port, PE_OBJ(port), RUN_SIG); + sm_run_state_machine(port, PE_OBJ(port), SM_RUN_SIG); break; case PE_PAUSED: if (en) @@ -105,20 +105,20 @@ void pe_message_sent(int port) /* Do nothing */ } -static unsigned int pe_request(int port, enum signal sig) +static int pe_request(int port, enum sm_signal sig) { int ret; ret = (*pe_request_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int pe_request_entry(int port) +static int pe_request_entry(int port) { return 0; } -static unsigned int pe_request_run(int port) +static int pe_request_run(int port) { uint32_t *payload = (uint32_t *)emsg[port].buf; uint32_t header = emsg[port].header; diff --git a/common/usb_prl_sm.c b/common/usbc/usb_prl_sm.c index ec29b66fbb..f013497cc4 100644 --- a/common/usb_prl_sm.c +++ b/common/usbc/usb_prl_sm.c @@ -153,43 +153,43 @@ struct extended_msg emsg[CONFIG_USB_PD_PORT_COUNT]; /* Common Protocol Layer Message Transmission */ static void prl_tx_construct_message(int port); -DECLARE_STATE(prl, tx_phy_layer_reset, NOOP_EXIT); -DECLARE_STATE(prl, tx_wait_for_message_request, NOOP_EXIT); -DECLARE_STATE(prl, tx_layer_reset_for_transmit, NOOP_EXIT); -DECLARE_STATE(prl, tx_wait_for_phy_response, WITH_EXIT); -DECLARE_STATE(prl, tx_src_source_tx, NOOP_EXIT); -DECLARE_STATE(prl, tx_snk_start_ams, NOOP_EXIT); +DECLARE_STATE(prl, tx_phy_layer_reset, WITH_RUN, NOOP); +DECLARE_STATE(prl, tx_wait_for_message_request, WITH_RUN, NOOP); +DECLARE_STATE(prl, tx_layer_reset_for_transmit, WITH_RUN, NOOP); +DECLARE_STATE(prl, tx_wait_for_phy_response, WITH_RUN, WITH_EXIT); +DECLARE_STATE(prl, tx_src_source_tx, WITH_RUN, NOOP); +DECLARE_STATE(prl, tx_snk_start_ams, WITH_RUN, NOOP); /* Source Protocol Layser Message Transmission */ -DECLARE_STATE(prl, tx_src_pending, NOOP_EXIT); +DECLARE_STATE(prl, tx_src_pending, WITH_RUN, NOOP); /* Sink Protocol Layer Message Transmission */ -DECLARE_STATE(prl, tx_snk_pending, NOOP_EXIT); -DECLARE_STATE(prl, tx_discard_message, NOOP_EXIT); +DECLARE_STATE(prl, tx_snk_pending, WITH_RUN, NOOP); +DECLARE_STATE(prl, tx_discard_message, WITH_RUN, NOOP); /* Protocol Layer Message Reception */ -static unsigned int prl_rx_wait_for_phy_message(int port, int evt); +static int prl_rx_wait_for_phy_message(int port, int evt); /* Hard Reset Operation */ -DECLARE_STATE(prl, hr_wait_for_request, NOOP_EXIT); -DECLARE_STATE(prl, hr_reset_layer, NOOP_EXIT); -DECLARE_STATE(prl, hr_wait_for_phy_hard_reset_complete, NOOP_EXIT); -DECLARE_STATE(prl, hr_wait_for_pe_hard_reset_complete, WITH_EXIT); +DECLARE_STATE(prl, hr_wait_for_request, WITH_RUN, NOOP); +DECLARE_STATE(prl, hr_reset_layer, WITH_RUN, NOOP); +DECLARE_STATE(prl, hr_wait_for_phy_hard_reset_complete, WITH_RUN, NOOP); +DECLARE_STATE(prl, hr_wait_for_pe_hard_reset_complete, WITH_RUN, WITH_EXIT); /* Chunked Rx */ -DECLARE_STATE(rch, wait_for_message_from_protocol_layer, NOOP_EXIT); -DECLARE_STATE(rch, processing_extended_message, NOOP_EXIT); -DECLARE_STATE(rch, requesting_chunk, NOOP_EXIT); -DECLARE_STATE(rch, waiting_chunk, NOOP_EXIT); -DECLARE_STATE(rch, report_error, NOOP_EXIT); +DECLARE_STATE(rch, wait_for_message_from_protocol_layer, WITH_RUN, NOOP); +DECLARE_STATE(rch, processing_extended_message, WITH_RUN, NOOP); +DECLARE_STATE(rch, requesting_chunk, WITH_RUN, NOOP); +DECLARE_STATE(rch, waiting_chunk, WITH_RUN, NOOP); +DECLARE_STATE(rch, report_error, WITH_RUN, NOOP); /* Chunked Tx */ -DECLARE_STATE(tch, wait_for_message_request_from_pe, NOOP_EXIT); -DECLARE_STATE(tch, wait_for_transmission_complete, NOOP_EXIT); -DECLARE_STATE(tch, construct_chunked_message, NOOP_EXIT); -DECLARE_STATE(tch, sending_chunked_message, NOOP_EXIT); -DECLARE_STATE(tch, wait_chunk_request, NOOP_EXIT); -DECLARE_STATE(tch, message_received, NOOP_EXIT); +DECLARE_STATE(tch, wait_for_message_request_from_pe, WITH_RUN, NOOP); +DECLARE_STATE(tch, wait_for_transmission_complete, WITH_RUN, NOOP); +DECLARE_STATE(tch, construct_chunked_message, WITH_RUN, NOOP); +DECLARE_STATE(tch, sending_chunked_message, WITH_RUN, NOOP); +DECLARE_STATE(tch, wait_chunk_request, WITH_RUN, NOOP); +DECLARE_STATE(tch, message_received, WITH_RUN, NOOP); void pd_transmit_complete(int port, int status) { @@ -203,7 +203,7 @@ void pd_execute_hard_reset(int port) return; prl_hr[port].flags |= PRL_FLAGS_PORT_PARTNER_HARD_RESET; - set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SM, 0); } @@ -214,7 +214,7 @@ void prl_execute_hard_reset(int port) return; prl_hr[port].flags |= PRL_FLAGS_PE_HARD_RESET; - set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SM, 0); } @@ -243,11 +243,12 @@ void prl_init(int port) prl_tx[port].msg_id_counter[i] = 0; } - init_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); - init_state(port, RCH_OBJ(port), + sm_init_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); + sm_init_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); - init_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); - init_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_request); + sm_init_state(port, TCH_OBJ(port), + tch_wait_for_message_request_from_pe); + sm_init_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_request); } enum rch_state_id get_rch_state_id(int port) @@ -328,7 +329,7 @@ void prl_reset(int port) local_state[port] = SM_INIT; } -void protocol_layer(int port, int evt, int en) +void usbc_protocol_layer(int port, int evt, int en) { switch (local_state[port]) { case SM_INIT: @@ -353,16 +354,16 @@ void protocol_layer(int port, int evt, int en) prl_rx_wait_for_phy_message(port, evt); /* Run RX Chunked state machine */ - exe_state(port, RCH_OBJ(port), RUN_SIG); + sm_run_state_machine(port, RCH_OBJ(port), SM_RUN_SIG); /* Run TX Chunked state machine */ - exe_state(port, TCH_OBJ(port), RUN_SIG); + sm_run_state_machine(port, TCH_OBJ(port), SM_RUN_SIG); /* Run Protocol Layer Message Transmission state machine */ - exe_state(port, PRL_TX_OBJ(port), RUN_SIG); + sm_run_state_machine(port, PRL_TX_OBJ(port), SM_RUN_SIG); /* Run Protocol Layer Hard Reset state machine */ - exe_state(port, PRL_HR_OBJ(port), RUN_SIG); + sm_run_state_machine(port, PRL_HR_OBJ(port), SM_RUN_SIG); break; case SM_PAUSED: if (en) @@ -387,15 +388,15 @@ enum pd_rev_type prl_get_rev(int port) } /* Common Protocol Layer Message Transmission */ -static unsigned int prl_tx_phy_layer_reset(int port, enum signal sig) +static int prl_tx_phy_layer_reset(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_phy_layer_reset_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_phy_layer_reset_entry(int port) +static int prl_tx_phy_layer_reset_entry(int port) { prl_tx[port].state_id = PRL_TX_PHY_LAYER_RESET; @@ -409,22 +410,22 @@ static unsigned int prl_tx_phy_layer_reset_entry(int port) return 0; } -static unsigned int prl_tx_phy_layer_reset_run(int port) +static int prl_tx_phy_layer_reset_run(int port) { - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_message_request); return 0; } -static unsigned int prl_tx_wait_for_message_request(int port, enum signal sig) +static int prl_tx_wait_for_message_request(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_wait_for_message_request_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_wait_for_message_request_entry(int port) +static int prl_tx_wait_for_message_request_entry(int port) { prl_tx[port].state_id = PRL_TX_WAIT_FOR_MESSAGE_REQUEST; @@ -434,7 +435,7 @@ static unsigned int prl_tx_wait_for_message_request_entry(int port) return 0; } -static unsigned int prl_tx_wait_for_message_request_run(int port) +static int prl_tx_wait_for_message_request_run(int port) { if (prl_tx[port].flags & PRL_FLAGS_MSG_XMIT) { prl_tx[port].flags &= ~PRL_FLAGS_MSG_XMIT; @@ -443,7 +444,7 @@ static unsigned int prl_tx_wait_for_message_request_run(int port) */ if ((pdmsg[port].msg_type == PD_CTRL_SOFT_RESET) && (emsg[port].len == 0)) { - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_layer_reset_for_transmit); } /* @@ -452,7 +453,7 @@ static unsigned int prl_tx_wait_for_message_request_run(int port) else { /* NOTE: PRL_TX_Construct_Message State embedded here */ prl_tx_construct_message(port); - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_phy_response); } @@ -467,7 +468,7 @@ static unsigned int prl_tx_wait_for_message_request_run(int port) */ if (prl_tx[port].flags & PRL_FLAGS_START_AMS) { prl_tx[port].flags &= ~PRL_FLAGS_START_AMS; - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_src_source_tx); return 0; } @@ -490,14 +491,14 @@ static unsigned int prl_tx_wait_for_message_request_run(int port) * First Message in AMS notification * received from Policy Engine. */ - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_snk_start_ams); return 0; } } } - return RUN_SUPER; + return SM_RUN_SUPER; } static void increment_msgid_counter(int port) @@ -510,42 +511,42 @@ static void increment_msgid_counter(int port) /* * PrlTxDiscard */ -static unsigned int prl_tx_discard_message(int port, enum signal sig) +static int prl_tx_discard_message(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_discard_message_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_discard_message_entry(int port) +static int prl_tx_discard_message_entry(int port) { prl_tx[port].state_id = PRL_TX_DISCARD_MESSAGE; /* Increment msgidCounter */ increment_msgid_counter(port); - set_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); return 0; } -static unsigned int prl_tx_discard_message_run(int port) +static int prl_tx_discard_message_run(int port) { - return RUN_SUPER; + return SM_RUN_SUPER; } /* * PrlTxSrcSourceTx */ -static unsigned int prl_tx_src_source_tx(int port, enum signal sig) +static int prl_tx_src_source_tx(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_src_source_tx_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_src_source_tx_entry(int port) +static int prl_tx_src_source_tx_entry(int port) { prl_tx[port].state_id = PRL_TX_SRC_SOURCE_TX; @@ -556,58 +557,58 @@ static unsigned int prl_tx_src_source_tx_entry(int port) return 0; } -static unsigned int prl_tx_src_source_tx_run(int port) +static int prl_tx_src_source_tx_run(int port) { if (prl_tx[port].flags & PRL_FLAGS_MSG_XMIT) { prl_tx[port].flags &= ~PRL_FLAGS_MSG_XMIT; - set_state(port, PRL_TX_OBJ(port), prl_tx_src_pending); + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_src_pending); } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * PrlTxSnkStartAms */ -static unsigned int prl_tx_snk_start_ams(int port, enum signal sig) +static int prl_tx_snk_start_ams(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_snk_start_ams_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_snk_start_ams_entry(int port) +static int prl_tx_snk_start_ams_entry(int port) { prl_tx[port].state_id = PRL_TX_SNK_START_OF_AMS; return 0; } -static unsigned int prl_tx_snk_start_ams_run(int port) +static int prl_tx_snk_start_ams_run(int port) { if (prl_tx[port].flags & PRL_FLAGS_MSG_XMIT) { prl_tx[port].flags &= ~PRL_FLAGS_MSG_XMIT; - set_state(port, PRL_TX_OBJ(port), prl_tx_snk_pending); + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_snk_pending); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * PrlTxLayerResetForTransmit */ -static unsigned int prl_tx_layer_reset_for_transmit(int port, enum signal sig) +static int prl_tx_layer_reset_for_transmit(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_layer_reset_for_transmit_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_layer_reset_for_transmit_entry(int port) +static int prl_tx_layer_reset_for_transmit_entry(int port) { int i; @@ -620,11 +621,11 @@ static unsigned int prl_tx_layer_reset_for_transmit_entry(int port) return 0; } -static unsigned int prl_tx_layer_reset_for_transmit_run(int port) +static int prl_tx_layer_reset_for_transmit_run(int port) { /* NOTE: PRL_Tx_Construct_Message State embedded here */ prl_tx_construct_message(port); - set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_phy_response); + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_phy_response); return 0; } @@ -651,15 +652,15 @@ static void prl_tx_construct_message(int port) /* * PrlTxWaitForPhyResponse */ -static unsigned int prl_tx_wait_for_phy_response(int port, enum signal sig) +static int prl_tx_wait_for_phy_response(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_wait_for_phy_response_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_wait_for_phy_response_entry(int port) +static int prl_tx_wait_for_phy_response_entry(int port) { prl_tx[port].state_id = PRL_TX_WAIT_FOR_PHY_RESPONSE; @@ -667,7 +668,7 @@ static unsigned int prl_tx_wait_for_phy_response_entry(int port) return 0; } -static unsigned int prl_tx_wait_for_phy_response_run(int port) +static int prl_tx_wait_for_phy_response_run(int port) { /* Wait until TX is complete */ @@ -708,7 +709,7 @@ static unsigned int prl_tx_wait_for_phy_response_run(int port) /* Increment message id counter */ increment_msgid_counter(port); - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_message_request); return 0; } @@ -727,15 +728,15 @@ static unsigned int prl_tx_wait_for_phy_response_run(int port) increment_msgid_counter(port); /* Inform Policy Engine Message was sent */ pdmsg[port].status_flags |= PRL_FLAGS_TX_COMPLETE; - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_message_request); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int prl_tx_wait_for_phy_response_exit(int port) +static int prl_tx_wait_for_phy_response_exit(int port) { prl_tx[port].xmit_status = TCPC_TX_UNSET; return 0; @@ -745,15 +746,15 @@ static unsigned int prl_tx_wait_for_phy_response_exit(int port) /* * PrlTxSrcPending */ -static unsigned int prl_tx_src_pending(int port, enum signal sig) +static int prl_tx_src_pending(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_src_pending_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_src_pending_entry(int port) +static int prl_tx_src_pending_entry(int port) { prl_tx[port].state_id = PRL_TX_SRC_PENDING; @@ -763,7 +764,7 @@ static unsigned int prl_tx_src_pending_entry(int port) return 0; } -static unsigned int prl_tx_src_pending_run(int port) +static int prl_tx_src_pending_run(int port) { if (get_time().val > prl_tx[port].sink_tx_timer) { @@ -773,7 +774,7 @@ static unsigned int prl_tx_src_pending_run(int port) */ if ((emsg[port].len == 0) && (pdmsg[port].msg_type == PD_CTRL_SOFT_RESET)) { - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_layer_reset_for_transmit); } /* Message pending (except Soft Reset) & @@ -781,34 +782,34 @@ static unsigned int prl_tx_src_pending_run(int port) */ else { prl_tx_construct_message(port); - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_phy_response); } return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * PrlTxSnkPending */ -static unsigned int prl_tx_snk_pending(int port, enum signal sig) +static int prl_tx_snk_pending(int port, enum sm_signal sig) { int ret; ret = (*prl_tx_snk_pending_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_tx_snk_pending_entry(int port) +static int prl_tx_snk_pending_entry(int port) { prl_tx[port].state_id = PRL_TX_SNK_PENDING; return 0; } -static unsigned int prl_tx_snk_pending_run(int port) +static int prl_tx_snk_pending_run(int port) { int cc1; int cc2; @@ -821,7 +822,7 @@ static unsigned int prl_tx_snk_pending_run(int port) */ if ((pdmsg[port].msg_type == PD_CTRL_SOFT_RESET) && (emsg[port].len == 0)) { - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_layer_reset_for_transmit); } /* @@ -830,26 +831,26 @@ static unsigned int prl_tx_snk_pending_run(int port) */ else { prl_tx_construct_message(port); - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_phy_response); } return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } /* Hard Reset Operation */ -static unsigned int prl_hr_wait_for_request(int port, enum signal sig) +static int prl_hr_wait_for_request(int port, enum sm_signal sig) { int ret; ret = (*prl_hr_wait_for_request_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_hr_wait_for_request_entry(int port) +static int prl_hr_wait_for_request_entry(int port) { prl_hr[port].state_id = PRL_HR_WAIT_FOR_REQUEST; @@ -857,11 +858,11 @@ static unsigned int prl_hr_wait_for_request_entry(int port) return 0; } -static unsigned int prl_hr_wait_for_request_run(int port) +static int prl_hr_wait_for_request_run(int port) { if (prl_hr[port].flags & PRL_FLAGS_PE_HARD_RESET || prl_hr[port].flags & PRL_FLAGS_PORT_PARTNER_HARD_RESET) { - set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_reset_layer); } return 0; @@ -870,15 +871,15 @@ static unsigned int prl_hr_wait_for_request_run(int port) /* * PrlHrResetLayer */ -static unsigned int prl_hr_reset_layer(int port, enum signal sig) +static int prl_hr_reset_layer(int port, enum sm_signal sig) { int ret; ret = (*prl_hr_reset_layer_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_hr_reset_layer_entry(int port) +static int prl_hr_reset_layer_entry(int port) { int i; @@ -891,13 +892,13 @@ static unsigned int prl_hr_reset_layer_entry(int port) * Protocol Layer message transmission transitions to * PRL_Tx_Wait_For_Message_Request state. */ - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_wait_for_message_request); return 0; } -static unsigned int prl_hr_reset_layer_run(int port) +static int prl_hr_reset_layer_run(int port) { /* * Protocol Layer reset Complete & @@ -906,7 +907,7 @@ static unsigned int prl_hr_reset_layer_run(int port) if (prl_hr[port].flags & PRL_FLAGS_PE_HARD_RESET) { /* Request PHY to perform a Hard Reset */ prl_send_ctrl_msg(port, TCPC_TX_HARD_RESET, 0); - set_state(port, PRL_HR_OBJ(port), + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_phy_hard_reset_complete); } /* @@ -916,7 +917,7 @@ static unsigned int prl_hr_reset_layer_run(int port) else { /* Inform Policy Engine of the Hard Reset */ pe_got_hard_reset(port); - set_state(port, PRL_HR_OBJ(port), + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_pe_hard_reset_complete); } @@ -926,16 +927,15 @@ static unsigned int prl_hr_reset_layer_run(int port) /* * PrlHrWaitForPhyHardResetComplete */ -static unsigned int - prl_hr_wait_for_phy_hard_reset_complete(int port, enum signal sig) +static int prl_hr_wait_for_phy_hard_reset_complete(int port, enum sm_signal sig) { int ret; ret = (*prl_hr_wait_for_phy_hard_reset_complete_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_hr_wait_for_phy_hard_reset_complete_entry(int port) +static int prl_hr_wait_for_phy_hard_reset_complete_entry(int port) { prl_hr[port].state_id = PRL_HR_WAIT_FOR_PHY_HARD_RESET_COMPLETE; @@ -946,7 +946,7 @@ static unsigned int prl_hr_wait_for_phy_hard_reset_complete_entry(int port) return 0; } -static unsigned int prl_hr_wait_for_phy_hard_reset_complete_run(int port) +static int prl_hr_wait_for_phy_hard_reset_complete_run(int port) { /* * Wait for hard reset from PHY @@ -958,52 +958,51 @@ static unsigned int prl_hr_wait_for_phy_hard_reset_complete_run(int port) /* Inform Policy Engine Hard Reset was sent */ pe_hard_reset_sent(port); - set_state(port, PRL_HR_OBJ(port), + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_pe_hard_reset_complete); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * PrlHrWaitForPeHardResetComplete */ -static unsigned int - prl_hr_wait_for_pe_hard_reset_complete(int port, enum signal sig) +static int prl_hr_wait_for_pe_hard_reset_complete(int port, enum sm_signal sig) { int ret; ret = (*prl_hr_wait_for_pe_hard_reset_complete_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int prl_hr_wait_for_pe_hard_reset_complete_entry(int port) +static int prl_hr_wait_for_pe_hard_reset_complete_entry(int port) { prl_hr[port].state_id = PRL_HR_WAIT_FOR_PE_HARD_RESET_COMPLETE; return 0; } -static unsigned int prl_hr_wait_for_pe_hard_reset_complete_run(int port) +static int prl_hr_wait_for_pe_hard_reset_complete_run(int port) { /* * Wait for Hard Reset complete indication from Policy Engine */ if (prl_hr[port].flags & PRL_FLAGS_HARD_RESET_COMPLETE) - set_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_request); + sm_set_state(port, PRL_HR_OBJ(port), prl_hr_wait_for_request); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int prl_hr_wait_for_pe_hard_reset_complete_exit(int port) +static int prl_hr_wait_for_pe_hard_reset_complete_exit(int port) { /* Exit from Hard Reset */ - set_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); - set_state(port, RCH_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); + sm_set_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); - set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); return 0; } @@ -1024,13 +1023,13 @@ static void copy_chunk_to_ext(int port) /* * Chunked Rx State Machine */ -static unsigned int - rch_wait_for_message_from_protocol_layer(int port, enum signal sig) +static int rch_wait_for_message_from_protocol_layer(int port, + enum sm_signal sig) { int ret; ret = (*rch_wait_for_message_from_protocol_layer_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } static inline void rch_clear_abort_set_chunking(int port) @@ -1042,14 +1041,14 @@ static inline void rch_clear_abort_set_chunking(int port) rch[port].flags = PRL_FLAGS_CHUNKING; } -static unsigned int rch_wait_for_message_from_protocol_layer_entry(int port) +static int rch_wait_for_message_from_protocol_layer_entry(int port) { rch[port].state_id = RCH_WAIT_FOR_MESSAGE_FROM_PROTOCOL_LAYER; rch_clear_abort_set_chunking(port); return 0; } -static unsigned int rch_wait_for_message_from_protocol_layer_run(int port) +static int rch_wait_for_message_from_protocol_layer_run(int port) { if (rch[port].flags & PRL_FLAGS_MSG_RECEIVED) { rch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; @@ -1068,7 +1067,7 @@ static unsigned int rch_wait_for_message_from_protocol_layer_run(int port) */ if ((rch[port].flags & PRL_FLAGS_CHUNKING) && chunked) { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_processing_extended_message); return 0; } @@ -1089,7 +1088,7 @@ static unsigned int rch_wait_for_message_from_protocol_layer_run(int port) * Chunked != Chunking */ else { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_report_error); return 0; } @@ -1110,27 +1109,27 @@ static unsigned int rch_wait_for_message_from_protocol_layer_run(int port) * revision lower than PD3.0 */ else { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_report_error); return 0; } } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * RchProcessingExtendedMessage */ -static unsigned int rch_processing_extended_message(int port, enum signal sig) +static int rch_processing_extended_message(int port, enum sm_signal sig) { int ret; ret = (*rch_processing_extended_message_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int rch_processing_extended_message_entry(int port) +static int rch_processing_extended_message_entry(int port) { uint32_t header = emsg[port].header; uint16_t exhdr = GET_EXT_HEADER(pdmsg[port].chk_buf[0]); @@ -1152,7 +1151,7 @@ static unsigned int rch_processing_extended_message_entry(int port) return 0; } -static unsigned int rch_processing_extended_message_run(int port) +static int rch_processing_extended_message_run(int port) { uint16_t exhdr = GET_EXT_HEADER(pdmsg[port].chk_buf[0]); uint8_t chunk_num = PD_EXT_HEADER_CHUNK_NUM(exhdr); @@ -1163,7 +1162,7 @@ static unsigned int rch_processing_extended_message_run(int port) * Abort Flag Set */ if (pdmsg[port].status_flags & PRL_FLAGS_ABORT) { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); } /* @@ -1181,7 +1180,7 @@ static unsigned int rch_processing_extended_message_run(int port) /* Make sure extended message buffer does not overflow */ if (pdmsg[port].num_bytes_received + byte_num > EXTENDED_BUFFER_SIZE) { - set_state(port, RCH_OBJ(port), rch_report_error); + sm_set_state(port, RCH_OBJ(port), rch_report_error); return 0; } @@ -1200,20 +1199,20 @@ static unsigned int rch_processing_extended_message_run(int port) emsg[port].len = pdmsg[port].num_bytes_received; /* Pass Message to Policy Engine */ pe_pass_up_message(port); - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); } /* * Message not Complete */ else - set_state(port, RCH_OBJ(port), rch_requesting_chunk); + sm_set_state(port, RCH_OBJ(port), rch_requesting_chunk); } /* * Unexpected Chunk Number */ else - set_state(port, RCH_OBJ(port), rch_report_error); + sm_set_state(port, RCH_OBJ(port), rch_report_error); return 0; } @@ -1221,15 +1220,15 @@ static unsigned int rch_processing_extended_message_run(int port) /* * RchRequestingChunk */ -static unsigned int rch_requesting_chunk(int port, enum signal sig) +static int rch_requesting_chunk(int port, enum sm_signal sig) { int ret; ret = (*rch_requesting_chunk_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int rch_requesting_chunk_entry(int port) +static int rch_requesting_chunk_entry(int port) { rch[port].state_id = RCH_REQUESTING_CHUNK; @@ -1251,7 +1250,7 @@ static unsigned int rch_requesting_chunk_entry(int port) return 0; } -static unsigned int rch_requesting_chunk_run(int port) +static int rch_requesting_chunk_run(int port) { /* * Transmission Error from Protocol Layer or @@ -1263,16 +1262,16 @@ static unsigned int rch_requesting_chunk_run(int port) * Leave PRL_FLAGS_MSG_RECEIVED flag set. It'll be * cleared in rch_report_error state */ - set_state(port, RCH_OBJ(port), rch_report_error); + sm_set_state(port, RCH_OBJ(port), rch_report_error); } /* * Message Transmitted received from Protocol Layer */ else if (pdmsg[port].status_flags & PRL_FLAGS_TX_COMPLETE) { pdmsg[port].status_flags &= ~PRL_FLAGS_TX_COMPLETE; - set_state(port, RCH_OBJ(port), rch_waiting_chunk); + sm_set_state(port, RCH_OBJ(port), rch_waiting_chunk); } else - return RUN_SUPER; + return SM_RUN_SUPER; return 0; } @@ -1280,15 +1279,15 @@ static unsigned int rch_requesting_chunk_run(int port) /* * RchWaitingChunk */ -static unsigned int rch_waiting_chunk(int port, enum signal sig) +static int rch_waiting_chunk(int port, enum sm_signal sig) { int ret; ret = (*rch_waiting_chunk_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int rch_waiting_chunk_entry(int port) +static int rch_waiting_chunk_entry(int port) { rch[port].state_id = RCH_WAITING_CHUNK; @@ -1301,7 +1300,7 @@ static unsigned int rch_waiting_chunk_entry(int port) return 0; } -static unsigned int rch_waiting_chunk_run(int port) +static int rch_waiting_chunk_run(int port) { if ((rch[port].flags & PRL_FLAGS_MSG_RECEIVED)) { /* @@ -1317,7 +1316,7 @@ static unsigned int rch_waiting_chunk_run(int port) */ if (PD_EXT_HEADER_REQ_CHUNK(exhdr) || !PD_EXT_HEADER_CHUNKED(exhdr)) { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_report_error); } /* @@ -1329,7 +1328,7 @@ static unsigned int rch_waiting_chunk_run(int port) * PRL_FLAGS_MSG_RECEIVED flag. */ rch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_processing_extended_message); } @@ -1340,25 +1339,25 @@ static unsigned int rch_waiting_chunk_run(int port) * ChunkSenderResponseTimer Timeout */ else if (get_time().val > rch[port].chunk_sender_response_timer) { - set_state(port, RCH_OBJ(port), rch_report_error); + sm_set_state(port, RCH_OBJ(port), rch_report_error); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * RchReportError */ -static unsigned int rch_report_error(int port, enum signal sig) +static int rch_report_error(int port, enum sm_signal sig) { int ret; ret = (*rch_report_error_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int rch_report_error_entry(int port) +static int rch_report_error_entry(int port) { rch[port].state_id = RCH_REPORT_ERROR; @@ -1383,9 +1382,9 @@ static unsigned int rch_report_error_entry(int port) return 0; } -static unsigned int rch_report_error_run(int port) +static int rch_report_error_run(int port) { - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); return 0; @@ -1394,13 +1393,12 @@ static unsigned int rch_report_error_run(int port) /* * Chunked Tx State Machine */ -static unsigned int - tch_wait_for_message_request_from_pe(int port, enum signal sig) +static int tch_wait_for_message_request_from_pe(int port, enum sm_signal sig) { int ret; ret = (*tch_wait_for_message_request_from_pe_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } static inline void tch_clear_abort_set_chunking(int port) @@ -1412,21 +1410,21 @@ static inline void tch_clear_abort_set_chunking(int port) tch[port].flags = PRL_FLAGS_CHUNKING; } -static unsigned int tch_wait_for_message_request_from_pe_entry(int port) +static int tch_wait_for_message_request_from_pe_entry(int port) { tch[port].state_id = TCH_WAIT_FOR_MESSAGE_REQUEST_FROM_PE; tch_clear_abort_set_chunking(port); return 0; } -static unsigned int tch_wait_for_message_request_from_pe_run(int port) +static int tch_wait_for_message_request_from_pe_run(int port) { /* * Any message received and not in state TCH_Wait_Chunk_Request */ if (tch[port].flags & PRL_FLAGS_MSG_RECEIVED) { tch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; - set_state(port, TCH_OBJ(port), tch_message_received); + sm_set_state(port, TCH_OBJ(port), tch_message_received); return 0; } else if (tch[port].flags & PRL_FLAGS_MSG_XMIT) { tch[port].flags &= ~PRL_FLAGS_MSG_XMIT; @@ -1449,7 +1447,7 @@ static unsigned int tch_wait_for_message_request_from_pe_run(int port) (tch[port].flags & PRL_FLAGS_CHUNKING)) { pdmsg[port].send_offset = 0; pdmsg[port].chunk_number_to_send = 0; - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_construct_chunked_message); } else /* @@ -1481,7 +1479,7 @@ static unsigned int tch_wait_for_message_request_from_pe_run(int port) (emsg[port].len + 3) >> 2; /* Pass Message to Protocol Layer */ prl_tx[port].flags |= PRL_FLAGS_MSG_XMIT; - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_transmission_complete); } @@ -1489,35 +1487,34 @@ static unsigned int tch_wait_for_message_request_from_pe_run(int port) } } - return RUN_SUPER; + return SM_RUN_SUPER; } /* * TchWaitForTransmissionComplete */ -static unsigned int - tch_wait_for_transmission_complete(int port, enum signal sig) +static int tch_wait_for_transmission_complete(int port, enum sm_signal sig) { int ret; ret = (*tch_wait_for_transmission_complete_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tch_wait_for_transmission_complete_entry(int port) +static int tch_wait_for_transmission_complete_entry(int port) { tch[port].state_id = TCH_WAIT_FOR_TRANSMISSION_COMPLETE; return 0; } -static unsigned int tch_wait_for_transmission_complete_run(int port) +static int tch_wait_for_transmission_complete_run(int port) { /* * Any message received and not in state TCH_Wait_Chunk_Request */ if (tch[port].flags & PRL_FLAGS_MSG_RECEIVED) { tch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; - set_state(port, TCH_OBJ(port), tch_message_received); + sm_set_state(port, TCH_OBJ(port), tch_message_received); return 0; } @@ -1526,7 +1523,7 @@ static unsigned int tch_wait_for_transmission_complete_run(int port) */ if (pdmsg[port].status_flags & PRL_FLAGS_TX_COMPLETE) { pdmsg[port].status_flags &= ~PRL_FLAGS_TX_COMPLETE; - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); /* Tell PE message was sent */ @@ -1539,7 +1536,7 @@ static unsigned int tch_wait_for_transmission_complete_run(int port) pdmsg[port].status_flags &= ~PRL_FLAGS_TX_ERROR; /* Tell PE an error occurred */ pe_report_error(port, ERR_TCH_XMIT); - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); } @@ -1549,15 +1546,15 @@ static unsigned int tch_wait_for_transmission_complete_run(int port) /* * TchConstructChunkedMessage */ -static unsigned int tch_construct_chunked_message(int port, enum signal sig) +static int tch_construct_chunked_message(int port, enum sm_signal sig) { int ret; ret = (*tch_construct_chunked_message_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tch_construct_chunked_message_entry(int port) +static int tch_construct_chunked_message_entry(int port) { uint16_t *ext_hdr; uint8_t *data; @@ -1570,7 +1567,7 @@ static unsigned int tch_construct_chunked_message_entry(int port) */ if (tch[port].flags & PRL_FLAGS_MSG_RECEIVED) { tch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; - set_state(port, TCH_OBJ(port), tch_message_received); + sm_set_state(port, TCH_OBJ(port), tch_message_received); return 0; } /* Prepare to copy chunk into chk_buf */ @@ -1609,13 +1606,13 @@ static unsigned int tch_construct_chunked_message_entry(int port) return 0; } -static unsigned int tch_construct_chunked_message_run(int port) +static int tch_construct_chunked_message_run(int port) { if (pdmsg[port].status_flags & PRL_FLAGS_ABORT) - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); else - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_sending_chunked_message); return 0; } @@ -1623,28 +1620,28 @@ static unsigned int tch_construct_chunked_message_run(int port) /* * TchSendingChunkedMessage */ -static unsigned int tch_sending_chunked_message(int port, enum signal sig) +static int tch_sending_chunked_message(int port, enum sm_signal sig) { int ret; ret = (*tch_sending_chunked_message_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tch_sending_chunked_message_entry(int port) +static int tch_sending_chunked_message_entry(int port) { tch[port].state_id = TCH_SENDING_CHUNKED_MESSAGE; return 0; } -static unsigned int tch_sending_chunked_message_run(int port) +static int tch_sending_chunked_message_run(int port) { /* * Any message received and not in state TCH_Wait_Chunk_Request */ if (tch[port].flags & PRL_FLAGS_MSG_RECEIVED) { tch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; - set_state(port, TCH_OBJ(port), tch_message_received); + sm_set_state(port, TCH_OBJ(port), tch_message_received); return 0; } @@ -1653,7 +1650,7 @@ static unsigned int tch_sending_chunked_message_run(int port) */ if (pdmsg[port].status_flags & PRL_FLAGS_TX_ERROR) { pe_report_error(port, ERR_TCH_XMIT); - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); } /* @@ -1661,7 +1658,7 @@ static unsigned int tch_sending_chunked_message_run(int port) * Last Chunk */ else if (emsg[port].len == pdmsg[port].send_offset) { - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); /* Tell PE message was sent */ @@ -1672,7 +1669,7 @@ static unsigned int tch_sending_chunked_message_run(int port) * Not Last Chunk */ else - set_state(port, TCH_OBJ(port), tch_wait_chunk_request); + sm_set_state(port, TCH_OBJ(port), tch_wait_chunk_request); return 0; } @@ -1680,15 +1677,15 @@ static unsigned int tch_sending_chunked_message_run(int port) /* * TchWaitChunkRequest */ -static unsigned int tch_wait_chunk_request(int port, enum signal sig) +static int tch_wait_chunk_request(int port, enum sm_signal sig) { int ret; ret = (*tch_wait_chunk_request_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tch_wait_chunk_request_entry(int port) +static int tch_wait_chunk_request_entry(int port) { tch[port].state_id = TCH_WAIT_CHUNK_REQUEST; @@ -1700,7 +1697,7 @@ static unsigned int tch_wait_chunk_request_entry(int port) return 0; } -static unsigned int tch_wait_chunk_request_run(int port) +static int tch_wait_chunk_request_run(int port) { if (tch[port].flags & PRL_FLAGS_MSG_RECEIVED) { tch[port].flags &= ~PRL_FLAGS_MSG_RECEIVED; @@ -1716,7 +1713,7 @@ static unsigned int tch_wait_chunk_request_run(int port) */ if (PD_EXT_HEADER_CHUNK_NUM(exthdr) == pdmsg[port].chunk_number_to_send) { - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_construct_chunked_message); } /* @@ -1725,7 +1722,7 @@ static unsigned int tch_wait_chunk_request_run(int port) */ else { pe_report_error(port, ERR_TCH_CHUNKED); - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); } return 0; @@ -1735,14 +1732,14 @@ static unsigned int tch_wait_chunk_request_run(int port) /* * Other message received */ - set_state(port, TCH_OBJ(port), tch_message_received); + sm_set_state(port, TCH_OBJ(port), tch_message_received); } /* * ChunkSenderRequestTimer timeout */ else if (get_time().val >= tch[port].chunk_sender_request_timer) { - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); /* Tell PE message was sent */ @@ -1755,15 +1752,15 @@ static unsigned int tch_wait_chunk_request_run(int port) /* * TchMessageReceived */ -static unsigned int tch_message_received(int port, enum signal sig) +static int tch_message_received(int port, enum sm_signal sig) { int ret; ret = (*tch_message_received_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tch_message_received_entry(int port) +static int tch_message_received_entry(int port) { tch[port].state_id = TCH_MESSAGE_RECEIVED; @@ -1774,9 +1771,9 @@ static unsigned int tch_message_received_entry(int port) return 0; } -static unsigned int tch_message_received_run(int port) +static int tch_message_received_run(int port) { - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); return 0; @@ -1785,7 +1782,7 @@ static unsigned int tch_message_received_run(int port) /* * Protocol Layer Message Reception State Machine */ -static unsigned int prl_rx_wait_for_phy_message(int port, int evt) +static int prl_rx_wait_for_phy_message(int port, int evt) { uint32_t header; uint8_t type; @@ -1818,11 +1815,11 @@ static unsigned int prl_rx_wait_for_phy_message(int port, int evt) pe_got_soft_reset(port); /* Soft Reset occurred */ - set_state(port, PRL_TX_OBJ(port), + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_phy_layer_reset); - set_state(port, RCH_OBJ(port), + sm_set_state(port, RCH_OBJ(port), rch_wait_for_message_from_protocol_layer); - set_state(port, TCH_OBJ(port), + sm_set_state(port, TCH_OBJ(port), tch_wait_for_message_request_from_pe); } @@ -1840,7 +1837,7 @@ static unsigned int prl_rx_wait_for_phy_message(int port, int evt) PRL_TX_SRC_PENDING || prl_tx[port].state_id == PRL_TX_SNK_PENDING) { - set_state(port, + sm_set_state(port, PRL_TX_OBJ(port), prl_tx_discard_message); } diff --git a/common/usb_sm.c b/common/usbc/usb_sm.c index de6d7959e3..0260406450 100644 --- a/common/usb_sm.c +++ b/common/usbc/usb_sm.c @@ -10,7 +10,7 @@ #include "util.h" #include "console.h" -void init_state(int port, struct sm_obj *obj, sm_state target) +void sm_init_state(int port, struct sm_obj *obj, sm_state target) { #if (CONFIG_SM_NESTING_NUM > 0) int i; @@ -30,13 +30,13 @@ void init_state(int port, struct sm_obj *obj, sm_state target) * has no super state */ tmp_super[CONFIG_SM_NESTING_NUM - 1] = - (sm_state)(uintptr_t)target(port, SUPER_SIG); + (sm_state)(uintptr_t)target(port, SM_SUPER_SIG); /* Get all super states of the target */ for (i = CONFIG_SM_NESTING_NUM - 1; i > 0; i--) { if (tmp_super[i] != NULL) tmp_super[i - 1] = - (sm_state)(uintptr_t)tmp_super[i](port, SUPER_SIG); + (sm_state)(uintptr_t)tmp_super[i](port, SM_SUPER_SIG); else tmp_super[i - 1] = NULL; } @@ -44,14 +44,14 @@ void init_state(int port, struct sm_obj *obj, sm_state target) /* Execute all super state entry actions in forward order */ for (i = 0; i < CONFIG_SM_NESTING_NUM; i++) if (tmp_super[i] != NULL) - tmp_super[i](port, ENTRY_SIG); + tmp_super[i](port, SM_ENTRY_SIG); #endif /* Now execute the target entry action */ - target(port, ENTRY_SIG); + target(port, SM_ENTRY_SIG); } -int set_state(int port, struct sm_obj *obj, sm_state target) +int sm_set_state(int port, struct sm_obj *obj, sm_state target) { #if (CONFIG_SM_NESTING_NUM > 0) int i; @@ -65,16 +65,16 @@ int set_state(int port, struct sm_obj *obj, sm_state target) /* Execute all exit actions is reverse order */ /* Get target's super state */ - target_super = (sm_state)(uintptr_t)target(port, SUPER_SIG); + target_super = (sm_state)(uintptr_t)target(port, SM_SUPER_SIG); tmp_super[0] = obj->task_state; do { /* Execute exit action */ - tmp_super[0](port, EXIT_SIG); + tmp_super[0](port, SM_EXIT_SIG); /* Get super state */ tmp_super[0] = - (sm_state)(uintptr_t)tmp_super[0](port, SUPER_SIG); + (sm_state)(uintptr_t)tmp_super[0](port, SM_SUPER_SIG); /* * No need to execute a super state's exit action that has * shared ancestry with the target. @@ -87,13 +87,13 @@ int set_state(int port, struct sm_obj *obj, sm_state target) } /* Get target state next super state if it exists */ - super = (sm_state)(uintptr_t)super(port, SUPER_SIG); + super = (sm_state)(uintptr_t)super(port, SM_SUPER_SIG); } } while (tmp_super[0] != NULL); /* All done executing the exit actions */ #else - obj->task_state(port, EXIT_SIG); + obj->task_state(port, SM_EXIT_SIG); #endif /* update the state variables */ obj->last_state = obj->task_state; @@ -103,19 +103,19 @@ int set_state(int port, struct sm_obj *obj, sm_state target) /* Prepare to execute all entry actions of the target's super states */ tmp_super[CONFIG_SM_NESTING_NUM - 1] = - (sm_state)(uintptr_t)target(port, SUPER_SIG); + (sm_state)(uintptr_t)target(port, SM_SUPER_SIG); /* Get all super states of the target */ for (i = CONFIG_SM_NESTING_NUM - 1; i > 0; i--) { if (tmp_super[i] != NULL) tmp_super[i - 1] = - (sm_state)(uintptr_t)tmp_super[i](port, SUPER_SIG); + (sm_state)(uintptr_t)tmp_super[i](port, SM_SUPER_SIG); else tmp_super[i - 1] = NULL; } /* Get super state of last state */ - last_super = (sm_state)(uintptr_t)obj->last_state(port, SUPER_SIG); + last_super = (sm_state)(uintptr_t)obj->last_state(port, SM_SUPER_SIG); /* Execute all super state entry actions in forward order */ for (i = 0; i < CONFIG_SM_NESTING_NUM; i++) { @@ -137,22 +137,22 @@ int set_state(int port, struct sm_obj *obj, sm_state target) } /* Get last state's next super state if it exists */ - super = (sm_state)(uintptr_t)super(port, SUPER_SIG); + super = (sm_state)(uintptr_t)super(port, SM_SUPER_SIG); } /* Execute super state's entry */ if (!no_execute) - tmp_super[i](port, ENTRY_SIG); + tmp_super[i](port, SM_ENTRY_SIG); } #endif /* Now execute the target entry action */ - target(port, ENTRY_SIG); + target(port, SM_ENTRY_SIG); return 0; } -void exe_state(int port, struct sm_obj *obj, enum signal sig) +void sm_run_state_machine(int port, struct sm_obj *obj, enum sm_signal sig) { #if (CONFIG_SM_NESTING_NUM > 0) sm_state state = obj->task_state; @@ -165,13 +165,13 @@ void exe_state(int port, struct sm_obj *obj, enum signal sig) #endif } -unsigned int do_nothing_exit(int port) +int sm_do_nothing(int port) { return 0; } -unsigned int get_super_state(int port) +int sm_get_super_state(int port) { - return RUN_SUPER; + return SM_RUN_SUPER; } diff --git a/common/usb_tc_ctvpd_sm.c b/common/usbc/usb_tc_ctvpd_sm.c index 5b471cf330..4097f98ce7 100644 --- a/common/usb_tc_ctvpd_sm.c +++ b/common/usbc/usb_tc_ctvpd_sm.c @@ -26,10 +26,6 @@ /* Type-C Layer Flags */ #define TC_FLAGS_VCONN_ON (1 << 0) -#undef PD_DEFAULT_STATE -/* Port default state at startup */ -#define PD_DEFAULT_STATE(port) tc_unattached_snk - #define SUPPORT_TIMER_RESET_INIT 0 #define SUPPORT_TIMER_RESET_REQUEST 1 #define SUPPORT_TIMER_RESET_COMPLETE 2 @@ -41,49 +37,53 @@ struct type_c tc[CONFIG_USB_PD_PORT_COUNT]; /* Type-C states */ -DECLARE_STATE(tc, disabled, WITH_EXIT); -DECLARE_STATE(tc, error_recovery, NOOP_EXIT); -DECLARE_STATE(tc, unattached_snk, NOOP_EXIT); -DECLARE_STATE(tc, attach_wait_snk, NOOP_EXIT); -DECLARE_STATE(tc, attached_snk, WITH_EXIT); -DECLARE_STATE(tc, try_snk, NOOP_EXIT); -DECLARE_STATE(tc, unattached_src, NOOP_EXIT); -DECLARE_STATE(tc, attach_wait_src, NOOP_EXIT); -DECLARE_STATE(tc, try_wait_src, NOOP_EXIT); -DECLARE_STATE(tc, attached_src, NOOP_EXIT); -DECLARE_STATE(tc, ct_try_snk, WITH_EXIT); -DECLARE_STATE(tc, ct_attach_wait_unsupported, WITH_EXIT); -DECLARE_STATE(tc, ct_attached_unsupported, WITH_EXIT); -DECLARE_STATE(tc, ct_unattached_unsupported, WITH_EXIT); -DECLARE_STATE(tc, ct_unattached_vpd, WITH_EXIT); -DECLARE_STATE(tc, ct_disabled_vpd, NOOP_EXIT); -DECLARE_STATE(tc, ct_attached_vpd, NOOP_EXIT); -DECLARE_STATE(tc, ct_attach_wait_vpd, WITH_EXIT); +DECLARE_STATE(tc, disabled, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, error_recovery, WITH_RUN, NOOP); +DECLARE_STATE(tc, unattached_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attach_wait_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attached_snk, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, try_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, unattached_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, attach_wait_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, try_wait_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, attached_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, ct_try_snk, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, ct_attach_wait_unsupported, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, ct_attached_unsupported, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, ct_unattached_unsupported, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, ct_unattached_vpd, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, ct_disabled_vpd, WITH_RUN, NOOP); +DECLARE_STATE(tc, ct_attached_vpd, WITH_RUN, NOOP); +DECLARE_STATE(tc, ct_attach_wait_vpd, WITH_RUN, WITH_EXIT); /* Super States */ -DECLARE_STATE(tc, host_rard_ct_rd, NOOP_EXIT); -DECLARE_STATE(tc, host_open_ct_open, NOOP_EXIT); -DECLARE_STATE(tc, vbus_cc_iso, NOOP_EXIT); -DECLARE_STATE(tc, host_rp3_ct_rd, NOOP_EXIT); -DECLARE_STATE(tc, host_rp3_ct_rpu, NOOP_EXIT); -DECLARE_STATE(tc, host_rpu_ct_rd, NOOP_EXIT); +DECLARE_STATE(tc, host_rard_ct_rd, NOOP, NOOP); +DECLARE_STATE(tc, host_open_ct_open, NOOP, NOOP); +DECLARE_STATE(tc, vbus_cc_iso, NOOP, NOOP); +DECLARE_STATE(tc, host_rp3_ct_rd, NOOP, NOOP); +DECLARE_STATE(tc, host_rp3_ct_rpu, NOOP, NOOP); +DECLARE_STATE(tc, host_rpu_ct_rd, NOOP, NOOP); void tc_reset_support_timer(int port) { tc[port].support_timer_reset |= SUPPORT_TIMER_RESET_REQUEST; } -void tc_state_init(int port) +void tc_state_init(int port, enum typec_state_id start_state) { int res = 0; sm_state this_state; res = tc_restart_tcpc(port); + if (res) + this_state = tc_disabled; + else + this_state = (start_state == TC_UNATTACHED_SRC) ? + tc_unattached_src : tc_unattached_snk; CPRINTS("TCPC p%d init %s", port, res ? "failed" : "ready"); - this_state = res ? tc_disabled : PD_DEFAULT_STATE(port); - init_state(port, TC_OBJ(port), this_state); + sm_init_state(port, TC_OBJ(port), this_state); /* Disable pd state machines */ tc[port].pd_enable = 0; @@ -108,28 +108,28 @@ void tc_event_check(int port, int evt) * Remove the terminations from Host * Remove the terminations from Charge-Through */ -static unsigned int tc_disabled(int port, enum signal sig) +static int tc_disabled(int port, enum sm_signal sig) { int ret = 0; ret = (*tc_disabled_sig[sig])(port); - return SUPER(ret, sig, tc_host_open_ct_open); + return SM_SUPER(ret, sig, tc_host_open_ct_open); } -static unsigned int tc_disabled_entry(int port) +static int tc_disabled_entry(int port) { - tc[port].state_id = DISABLED; + tc[port].state_id = TC_DISABLED; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); return 0; } -static unsigned int tc_disabled_run(int port) +static int tc_disabled_run(int port) { task_wait_event(-1); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_disabled_exit(int port) +static int tc_disabled_exit(int port) { #ifndef CONFIG_USB_PD_TCPC if (tc_restart_tcpc(port) != 0) { @@ -138,7 +138,7 @@ static unsigned int tc_disabled_exit(int port) } #endif CPRINTS("TCPC p%d resumed!", port); - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -152,31 +152,29 @@ static unsigned int tc_disabled_exit(int port) * Remove the terminations from Host * Remove the terminations from Charge-Through */ -static unsigned int tc_error_recovery(int port, enum signal sig) +static int tc_error_recovery(int port, enum sm_signal sig) { int ret = 0; ret = (*tc_error_recovery_sig[sig])(port); - return SUPER(ret, sig, tc_host_open_ct_open); + return SM_SUPER(ret, sig, tc_host_open_ct_open); } -static unsigned int tc_error_recovery_entry(int port) +static int tc_error_recovery_entry(int port) { - tc[port].state_id = ERROR_RECOVERY; + tc[port].state_id = TC_ERROR_RECOVERY; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Use cc_debounce state variable for error recovery timeout */ tc[port].cc_debounce = get_time().val + PD_T_ERROR_RECOVERY; return 0; } -static unsigned int tc_error_recovery_run(int port) +static int tc_error_recovery_run(int port) { - if (get_time().val > tc[port].cc_debounce) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (get_time().val > tc[port].cc_debounce) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); - return RUN_SUPER; + return SM_RUN_SUPER; } /** @@ -188,17 +186,17 @@ static unsigned int tc_error_recovery_run(int port) * Place Ra on VCONN and Rd on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_unattached_snk(int port, enum signal sig) +static int tc_unattached_snk(int port, enum sm_signal sig) { int ret = 0; ret = (*tc_unattached_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rard_ct_rd); + return SM_SUPER(ret, sig, tc_host_rard_ct_rd); } -static unsigned int tc_unattached_snk_entry(int port) +static int tc_unattached_snk_entry(int port) { - tc[port].state_id = UNATTACHED_SNK; + tc[port].state_id = TC_UNATTACHED_SNK; if (tc[port].obj.last_state != tc_unattached_src) CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); @@ -208,7 +206,7 @@ static unsigned int tc_unattached_snk_entry(int port) return 0; } -static unsigned int tc_unattached_snk_run(int port) +static int tc_unattached_snk_run(int port) { int host_cc; int new_cc_state; @@ -223,10 +221,8 @@ static unsigned int tc_unattached_snk_run(int port) * detected, as indicated by the SNK.Rp state on its Host-side * port’s CC pin. */ - if (cc_is_rp(host_cc)) { - set_state(port, TC_OBJ(port), tc_attach_wait_snk); - return 0; - } + if (cc_is_rp(host_cc)) + return sm_set_state(port, TC_OBJ(port), tc_attach_wait_snk); /* Check Charge-Through CCs for connection */ vpd_ct_get_cc(&cc1, &cc2); @@ -258,12 +254,10 @@ static unsigned int tc_unattached_snk_run(int port) * 2) VBUS is detected */ if (vpd_is_ct_vbus_present() && - tc[port].cc_state == PD_CC_DFP_ATTACHED) { - set_state(port, TC_OBJ(port), tc_unattached_src); - return 0; - } + tc[port].cc_state == PD_CC_DFP_ATTACHED) + return sm_set_state(port, TC_OBJ(port), tc_unattached_src); - return RUN_SUPER; + return SM_RUN_SUPER; } /** @@ -275,24 +269,24 @@ static unsigned int tc_unattached_snk_run(int port) * Place Ra on VCONN and Rd on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_attach_wait_snk(int port, enum signal sig) +static int tc_attach_wait_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_attach_wait_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rard_ct_rd); + return SM_SUPER(ret, sig, tc_host_rard_ct_rd); } -static unsigned int tc_attach_wait_snk_entry(int port) +static int tc_attach_wait_snk_entry(int port) { - tc[port].state_id = ATTACH_WAIT_SNK; + tc[port].state_id = TC_ATTACH_WAIT_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); tc[port].host_cc_state = PD_CC_UNSET; return 0; } -static unsigned int tc_attach_wait_snk_run(int port) +static int tc_attach_wait_snk_run(int port) { int host_new_cc_state; int host_cc; @@ -332,9 +326,9 @@ static unsigned int tc_attach_wait_snk_run(int port) */ if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED && (vpd_is_vconn_present() || vpd_is_host_vbus_present())) - set_state(port, TC_OBJ(port), tc_attached_snk); + sm_set_state(port, TC_OBJ(port), tc_attached_snk); else if (tc[port].host_cc_state == PD_CC_NONE) - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -342,17 +336,17 @@ static unsigned int tc_attach_wait_snk_run(int port) /** * Attached.SNK */ -static unsigned int tc_attached_snk(int port, enum signal sig) +static int tc_attached_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_attached_snk_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_attached_snk_entry(int port) +static int tc_attached_snk_entry(int port) { - tc[port].state_id = ATTACHED_SNK; + tc[port].state_id = TC_ATTACHED_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -379,16 +373,14 @@ static unsigned int tc_attached_snk_entry(int port) return 0; } -static unsigned int tc_attached_snk_run(int port) +static int tc_attached_snk_run(int port) { int host_new_cc_state; int host_cc; /* Has host vbus and vconn been removed */ - if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* * Reset the Charge-Through Support Timer when it first @@ -432,10 +424,9 @@ static unsigned int tc_attached_snk_run(int port) * to CTUnattached.VPD if VCONN is present and the state of * its Host-side port’s CC pin is SNK.Open for tVPDCTDD. */ - if (tc[port].host_cc_state == PD_CC_NONE) { - set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); - return 0; - } + if (tc[port].host_cc_state == PD_CC_NONE) + return sm_set_state(port, TC_OBJ(port), + tc_ct_unattached_vpd); } /* Check the Support Timer */ @@ -452,7 +443,7 @@ static unsigned int tc_attached_snk_run(int port) return 0; } -static unsigned int tc_attached_snk_exit(int port) +static int tc_attached_snk_exit(int port) { /* Reset timeout value to 10ms */ tc_set_timeout(port, 10*MSEC); @@ -465,15 +456,15 @@ static unsigned int tc_attached_snk_exit(int port) /** * Super State HOST_RA_CT_RD */ -static unsigned int tc_host_rard_ct_rd(int port, enum signal sig) +static int tc_host_rard_ct_rd(int port, enum sm_signal sig) { int ret; ret = (*tc_host_rard_ct_rd_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_rard_ct_rd_entry(int port) +static int tc_host_rard_ct_rd_entry(int port) { /* Place Ra on VCONN and Rd on Host CC */ vpd_host_set_pull(TYPEC_CC_RA_RD, 0); @@ -484,23 +475,18 @@ static unsigned int tc_host_rard_ct_rd_entry(int port) return 0; } -static unsigned int tc_host_rard_ct_rd_run(int port) -{ - return RUN_SUPER; -} - /** * Super State HOST_OPEN_CT_OPEN */ -static unsigned int tc_host_open_ct_open(int port, enum signal sig) +static int tc_host_open_ct_open(int port, enum sm_signal sig) { int ret; ret = (*tc_host_open_ct_open_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_open_ct_open_entry(int port) +static int tc_host_open_ct_open_entry(int port) { /* Remove the terminations from Host */ vpd_host_set_pull(TYPEC_CC_OPEN, 0); @@ -511,23 +497,18 @@ static unsigned int tc_host_open_ct_open_entry(int port) return 0; } -static unsigned int tc_host_open_ct_open_run(int port) -{ - return RUN_SUPER; -} - /** * Super State VBUS_CC_ISO */ -static unsigned int tc_vbus_cc_iso(int port, enum signal sig) +static int tc_vbus_cc_iso(int port, enum sm_signal sig) { int ret; ret = (*tc_vbus_cc_iso_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_vbus_cc_iso_entry(int port) +static int tc_vbus_cc_iso_entry(int port) { /* Isolate the Host-side port from the Charge-Through port */ vpd_vbus_pass_en(0); @@ -541,11 +522,6 @@ static unsigned int tc_vbus_cc_iso_entry(int port) return 0; } -static unsigned int tc_vbus_cc_iso_run(int port) -{ - return 0; -} - /** * Unattached.SRC * @@ -555,17 +531,17 @@ static unsigned int tc_vbus_cc_iso_run(int port) * Place RpUSB on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_unattached_src(int port, enum signal sig) +static int tc_unattached_src(int port, enum sm_signal sig) { int ret; ret = (*tc_unattached_src_sig[sig])(port); - return SUPER(ret, sig, tc_host_rpu_ct_rd); + return SM_SUPER(ret, sig, tc_host_rpu_ct_rd); } -static unsigned int tc_unattached_src_entry(int port) +static int tc_unattached_src_entry(int port) { - tc[port].state_id = UNATTACHED_SRC; + tc[port].state_id = TC_UNATTACHED_SRC; if (tc[port].obj.last_state != tc_unattached_snk) CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); @@ -573,17 +549,15 @@ static unsigned int tc_unattached_src_entry(int port) vpd_vconn_pwr_sel_odl(PWR_VBUS); /* Make sure it's the Charge-Through Port's VBUS */ - if (!vpd_is_ct_vbus_present()) { - set_state(port, TC_OBJ(port), tc_error_recovery); - return 0; - } + if (!vpd_is_ct_vbus_present()) + return sm_set_state(port, TC_OBJ(port), tc_error_recovery); tc[port].next_role_swap = get_time().val + PD_T_DRP_SRC; return 0; } -static unsigned int tc_unattached_src_run(int port) +static int tc_unattached_src_run(int port) { int host_cc; @@ -595,22 +569,18 @@ static unsigned int tc_unattached_src_run(int port) * vSafe0V and SRC.Rd state is detected on the Host-side * port’s CC pin. */ - if (!vpd_is_host_vbus_present() && host_cc == TYPEC_CC_VOLT_RD) { - set_state(port, TC_OBJ(port), tc_attach_wait_src); - return 0; - } + if (!vpd_is_host_vbus_present() && host_cc == TYPEC_CC_VOLT_RD) + return sm_set_state(port, TC_OBJ(port), tc_attach_wait_src); /* * Transition to Unattached.SNK within tDRPTransition or * if Charge-Through VBUS is removed. */ if (!vpd_is_ct_vbus_present() || - get_time().val > tc[port].next_role_swap) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + get_time().val > tc[port].next_role_swap) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); - return RUN_SUPER; + return SM_RUN_SUPER; } /** @@ -622,17 +592,17 @@ static unsigned int tc_unattached_src_run(int port) * Place RpUSB on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_attach_wait_src(int port, enum signal sig) +static int tc_attach_wait_src(int port, enum sm_signal sig) { int ret; ret = (*tc_attach_wait_src_sig[sig])(port); - return SUPER(ret, sig, tc_host_rpu_ct_rd); + return SM_SUPER(ret, sig, tc_host_rpu_ct_rd); } -static unsigned int tc_attach_wait_src_entry(int port) +static int tc_attach_wait_src_entry(int port) { - tc[port].state_id = ATTACH_WAIT_SRC; + tc[port].state_id = TC_ATTACH_WAIT_SRC; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); tc[port].host_cc_state = PD_CC_UNSET; @@ -640,7 +610,7 @@ static unsigned int tc_attach_wait_src_entry(int port) return 0; } -static unsigned int tc_attach_wait_src_run(int port) +static int tc_attach_wait_src_run(int port) { int host_new_cc_state; int host_cc; @@ -661,10 +631,8 @@ static unsigned int tc_attach_wait_src_run(int port) * shall detect the SRC.Open state within tSRCDisconnect, but * should detect it as quickly as possible. */ - if (host_new_cc_state == PD_CC_NONE || !vpd_is_ct_vbus_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (host_new_cc_state == PD_CC_NONE || !vpd_is_ct_vbus_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* Debounce the Host CC state */ if (tc[port].host_cc_state != host_new_cc_state) { @@ -683,28 +651,26 @@ static unsigned int tc_attach_wait_src_run(int port) * state is on the Host-side port’s CC pin for at least tCCDebounce. */ if (tc[port].host_cc_state == PD_CC_UFP_ATTACHED && - !vpd_is_host_vbus_present()) { - set_state(port, TC_OBJ(port), tc_try_snk); - return 0; - } + !vpd_is_host_vbus_present()) + return sm_set_state(port, TC_OBJ(port), tc_try_snk); - return RUN_SUPER; + return SM_RUN_SUPER; } /** * Attached.SRC */ -static unsigned int tc_attached_src(int port, enum signal sig) +static int tc_attached_src(int port, enum sm_signal sig) { int ret; ret = (*tc_attached_src_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_attached_src_entry(int port) +static int tc_attached_src_entry(int port) { - tc[port].state_id = ATTACHED_SRC; + tc[port].state_id = TC_ATTACHED_SRC; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -724,7 +690,7 @@ static unsigned int tc_attached_src_entry(int port) return 0; } -static unsigned int tc_attached_src_run(int port) +static int tc_attached_src_run(int port) { int host_cc; @@ -739,7 +705,7 @@ static unsigned int tc_attached_src_run(int port) * tSRCDisconnect, but should detect it as quickly as possible. */ if (!vpd_is_ct_vbus_present() || host_cc == TYPEC_CC_VOLT_OPEN) - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -747,15 +713,15 @@ static unsigned int tc_attached_src_run(int port) /** * Super State HOST_RPU_CT_RD */ -static unsigned int tc_host_rpu_ct_rd(int port, enum signal sig) +static int tc_host_rpu_ct_rd(int port, enum sm_signal sig) { int ret; ret = (*tc_host_rpu_ct_rd_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_rpu_ct_rd_entry(int port) +static int tc_host_rpu_ct_rd_entry(int port) { /* Place RpUSB on Host CC */ vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_USB); @@ -766,11 +732,6 @@ static unsigned int tc_host_rpu_ct_rd_entry(int port) return 0; } -static unsigned int tc_host_rpu_ct_rd_run(int port) -{ - return RUN_SUPER; -} - /** * Try.SNK * @@ -780,27 +741,25 @@ static unsigned int tc_host_rpu_ct_rd_run(int port) * Place Ra on VCONN and Rd on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_try_snk(int port, enum signal sig) +static int tc_try_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_try_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rard_ct_rd); + return SM_SUPER(ret, sig, tc_host_rard_ct_rd); } -static unsigned int tc_try_snk_entry(int port) +static int tc_try_snk_entry(int port) { - tc[port].state_id = TRY_SNK; + tc[port].state_id = TC_TRY_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Get power from VBUS */ vpd_vconn_pwr_sel_odl(PWR_VBUS); /* Make sure it's the Charge-Through Port's VBUS */ - if (!vpd_is_ct_vbus_present()) { - set_state(port, TC_OBJ(port), tc_error_recovery); - return 0; - } + if (!vpd_is_ct_vbus_present()) + return sm_set_state(port, TC_OBJ(port), tc_error_recovery); tc[port].host_cc_state = PD_CC_UNSET; @@ -810,7 +769,7 @@ static unsigned int tc_try_snk_entry(int port) return 0; } -static unsigned int tc_try_snk_run(int port) +static int tc_try_snk_run(int port) { int host_new_cc_state; int host_cc; @@ -853,9 +812,9 @@ static unsigned int tc_try_snk_run(int port) */ if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED && (vpd_is_host_vbus_present() || vpd_is_vconn_present())) - set_state(port, TC_OBJ(port), tc_attached_snk); + sm_set_state(port, TC_OBJ(port), tc_attached_snk); else if (tc[port].host_cc_state == PD_CC_NONE) - set_state(port, TC_OBJ(port), tc_try_wait_src); + sm_set_state(port, TC_OBJ(port), tc_try_wait_src); return 0; } @@ -869,17 +828,17 @@ static unsigned int tc_try_snk_run(int port) * Place RpUSB on Host CC * Place Rd on Charge-Through CCs */ -static unsigned int tc_try_wait_src(int port, enum signal sig) +static int tc_try_wait_src(int port, enum sm_signal sig) { int ret; ret = (*tc_try_wait_src_sig[sig])(port); - return SUPER(ret, sig, tc_host_rpu_ct_rd); + return SM_SUPER(ret, sig, tc_host_rpu_ct_rd); } -static unsigned int tc_try_wait_src_entry(int port) +static int tc_try_wait_src_entry(int port) { - tc[port].state_id = TRY_WAIT_SRC; + tc[port].state_id = TC_TRY_WAIT_SRC; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); tc[port].host_cc_state = PD_CC_UNSET; @@ -888,7 +847,7 @@ static unsigned int tc_try_wait_src_entry(int port) return 0; } -static unsigned int tc_try_wait_src_run(int port) +static int tc_try_wait_src_run(int port) { int host_new_cc_state; int host_cc; @@ -917,10 +876,9 @@ static unsigned int tc_try_wait_src_run(int port) * at least tTryCCDebounce. */ if (tc[port].host_cc_state == PD_CC_UFP_ATTACHED && - !vpd_is_host_vbus_present()) { - set_state(port, TC_OBJ(port), tc_attached_src); - return 0; - } + !vpd_is_host_vbus_present()) + return sm_set_state(port, TC_OBJ(port), + tc_attached_src); } if (get_time().val > tc[port].next_role_swap) { @@ -929,13 +887,12 @@ static unsigned int tc_try_wait_src_run(int port) * to Unattached.SNK after tDRPTry if the Host-side port’s CC * pin is not in the SRC.Rd state. */ - if (tc[port].host_cc_state == PD_CC_NONE) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (tc[port].host_cc_state == PD_CC_NONE) + return sm_set_state(port, TC_OBJ(port), + tc_unattached_snk); } - return RUN_SUPER; + return SM_RUN_SUPER; } /** @@ -948,17 +905,17 @@ static unsigned int tc_try_wait_src_run(int port) * Connect Charge-Through Rd * Get power from VCONN */ -static unsigned int tc_ct_try_snk(int port, enum signal sig) +static int tc_ct_try_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_try_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rd); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rd); } -static unsigned int tc_ct_try_snk_entry(int port) +static int tc_ct_try_snk_entry(int port) { - tc[port].state_id = CTTRY_SNK; + tc[port].state_id = TC_CTTRY_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -971,7 +928,7 @@ static unsigned int tc_ct_try_snk_entry(int port) return 0; } -static unsigned int tc_ct_try_snk_run(int port) +static int tc_ct_try_snk_run(int port) { int new_cc_state; int cc1; @@ -996,10 +953,8 @@ static unsigned int tc_ct_try_snk_run(int port) * The Charge-Through VCONN-Powered USB Device shall transition * to Unattached.SNK if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* Debounce the CT CC state */ if (tc[port].cc_state != new_cc_state) { @@ -1019,10 +974,9 @@ static unsigned int tc_ct_try_snk_run(int port) * Charge-Through port. */ if (tc[port].cc_state == PD_CC_DFP_ATTACHED && - vpd_is_ct_vbus_present()) { - set_state(port, TC_OBJ(port), tc_ct_attached_vpd); - return 0; - } + vpd_is_ct_vbus_present()) + return sm_set_state(port, TC_OBJ(port), + tc_ct_attached_vpd); } if (get_time().val > tc[port].try_wait_debounce) { @@ -1031,17 +985,15 @@ static unsigned int tc_ct_try_snk_run(int port) * to CTAttached.Unsupported if SNK.Rp state is not detected * for tDRPTryWait. */ - if (tc[port].cc_state == PD_CC_NONE) { - set_state(port, TC_OBJ(port), + if (tc[port].cc_state == PD_CC_NONE) + return sm_set_state(port, TC_OBJ(port), tc_ct_attached_unsupported); - return 0; - } } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_ct_try_snk_exit(int port) +static int tc_ct_try_snk_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -1059,17 +1011,17 @@ static unsigned int tc_ct_try_snk_exit(int port) * Place RPUSB on Charge-Through CC * Get power from VCONN */ -static unsigned int tc_ct_attach_wait_unsupported(int port, enum signal sig) +static int tc_ct_attach_wait_unsupported(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_attach_wait_unsupported_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rpu); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rpu); } -static unsigned int tc_ct_attach_wait_unsupported_entry(int port) +static int tc_ct_attach_wait_unsupported_entry(int port) { - tc[port].state_id = CTATTACH_WAIT_UNSUPPORTED; + tc[port].state_id = TC_CTATTACH_WAIT_UNSUPPORTED; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -1081,7 +1033,7 @@ static unsigned int tc_ct_attach_wait_unsupported_entry(int port) return 0; } -static unsigned int tc_ct_attach_wait_unsupported_run(int port) +static int tc_ct_attach_wait_unsupported_run(int port) { int new_cc_state; int cc1; @@ -1101,10 +1053,8 @@ static unsigned int tc_ct_attach_wait_unsupported_run(int port) * A Charge-Through VCONN-Powered USB Device shall transition to * Unattached.SNK if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* Debounce the cc state */ if (tc[port].cc_state != new_cc_state) { @@ -1128,14 +1078,14 @@ static unsigned int tc_ct_attach_wait_unsupported_run(int port) * pins is SRC.Ra. for at least tCCDebounce. */ if (new_cc_state == PD_CC_NONE) - set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); + sm_set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); else /* PD_CC_DFP_ATTACHED or PD_CC_AUDIO_ACC */ - set_state(port, TC_OBJ(port), tc_ct_try_snk); + sm_set_state(port, TC_OBJ(port), tc_ct_try_snk); return 0; } -static unsigned int tc_ct_attach_wait_unsupported_exit(int port) +static int tc_ct_attach_wait_unsupported_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -1153,17 +1103,17 @@ static unsigned int tc_ct_attach_wait_unsupported_exit(int port) * Place RPUSB on Charge-Through CC * Get power from VCONN */ -static unsigned int tc_ct_attached_unsupported(int port, enum signal sig) +static int tc_ct_attached_unsupported(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_attached_unsupported_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rpu); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rpu); } -static unsigned int tc_ct_attached_unsupported_entry(int port) +static int tc_ct_attached_unsupported_entry(int port) { - tc[port].state_id = CTATTACHED_UNSUPPORTED; + tc[port].state_id = TC_CTATTACHED_UNSUPPORTED; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Present Billboard device */ @@ -1172,7 +1122,7 @@ static unsigned int tc_ct_attached_unsupported_entry(int port) return 0; } -static unsigned int tc_ct_attached_unsupported_run(int port) +static int tc_ct_attached_unsupported_run(int port) { int cc1; int cc2; @@ -1180,10 +1130,8 @@ static unsigned int tc_ct_attached_unsupported_run(int port) /* Check CT CC for connection */ vpd_ct_get_cc(&cc1, &cc2); - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* * The Charge-Through VCONN-Powered USB Device shall transition to @@ -1193,15 +1141,13 @@ static unsigned int tc_ct_attached_unsupported_run(int port) */ if ((cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN) || (cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_RA) || - (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_OPEN)) { - set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); - return 0; - } + (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_OPEN)) + return sm_set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_ct_attached_unsupported_exit(int port) +static int tc_ct_attached_unsupported_exit(int port) { vpd_present_billboard(BB_NONE); @@ -1218,17 +1164,17 @@ static unsigned int tc_ct_attached_unsupported_exit(int port) * Place RPUSB on Charge-Through CC * Get power from VCONN */ -static unsigned int tc_ct_unattached_unsupported(int port, enum signal sig) +static int tc_ct_unattached_unsupported(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_unattached_unsupported_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rpu); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rpu); } -static unsigned int tc_ct_unattached_unsupported_entry(int port) +static int tc_ct_unattached_unsupported_entry(int port) { - tc[port].state_id = CTUNATTACHED_UNSUPPORTED; + tc[port].state_id = TC_CTUNATTACHED_UNSUPPORTED; if (tc[port].obj.last_state != tc_ct_unattached_vpd) CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); @@ -1241,7 +1187,7 @@ static unsigned int tc_ct_unattached_unsupported_entry(int port) return 0; } -static unsigned int tc_ct_unattached_unsupported_run(int port) +static int tc_ct_unattached_unsupported_run(int port) { int cc1; int cc2; @@ -1256,34 +1202,28 @@ static unsigned int tc_ct_unattached_unsupported_run(int port) * least one of the Charge-Through port’s CC pins or SRC.Ra state * on both the CC1 and CC2 pins. */ - if (cc_is_at_least_one_rd(cc1, cc2) || cc_is_audio_acc(cc1, cc2)) { - set_state(port, TC_OBJ(port), + if (cc_is_at_least_one_rd(cc1, cc2) || cc_is_audio_acc(cc1, cc2)) + return sm_set_state(port, TC_OBJ(port), tc_ct_attach_wait_unsupported); - return 0; - } /* * A Charge-Through VCONN-Powered USB Device shall transition to * Unattached.SNK if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); /* * A Charge-Through VCONN-Powered USB Device shall transition to * CTUnattached.VPD within tDRPTransition after dcSRC.DRP ∙ tDRP. */ - if (get_time().val > tc[port].next_role_swap) { - set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); - return 0; - } + if (get_time().val > tc[port].next_role_swap) + return sm_set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_ct_unattached_unsupported_exit(int port) +static int tc_ct_unattached_unsupported_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -1301,17 +1241,17 @@ static unsigned int tc_ct_unattached_unsupported_exit(int port) * Connect Charge-Through Rd * Get power from VCONN */ -static unsigned int tc_ct_unattached_vpd(int port, enum signal sig) +static int tc_ct_unattached_vpd(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_unattached_vpd_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rd); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rd); } -static unsigned int tc_ct_unattached_vpd_entry(int port) +static int tc_ct_unattached_vpd_entry(int port) { - tc[port].state_id = CTUNATTACHED_VPD; + tc[port].state_id = TC_CTUNATTACHED_VPD; if (tc[port].obj.last_state != tc_ct_unattached_unsupported) CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); @@ -1324,7 +1264,7 @@ static unsigned int tc_ct_unattached_vpd_entry(int port) return 0; } -static unsigned int tc_ct_unattached_vpd_run(int port) +static int tc_ct_unattached_vpd_run(int port) { int new_cc_state; int cc1; @@ -1346,21 +1286,17 @@ static unsigned int tc_ct_unattached_vpd_run(int port) * Charge-Through port, as indicated by the SNK.Rp state on * exactly one of the Charge-Through port’s CC pins. */ - if (new_cc_state == PD_CC_DFP_ATTACHED) { - set_state(port, TC_OBJ(port), + if (new_cc_state == PD_CC_DFP_ATTACHED) + return sm_set_state(port, TC_OBJ(port), tc_ct_attach_wait_vpd); - return 0; - } /* * A Charge-Through VCONN-Powered USB Device shall transition to * Unattached.SNK if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } /* Debounce the cc state */ if (new_cc_state != tc[port].cc_state) { @@ -1378,16 +1314,14 @@ static unsigned int tc_ct_unattached_vpd_run(int port) * of both the Charge-Through port’s CC1 and CC2 pins is SNK.Open * for tDRP-dcSRC.DRP ∙ tDRP, or if directed. */ - if (tc[port].cc_state == PD_CC_NONE) { - set_state(port, TC_OBJ(port), - tc_ct_unattached_unsupported); - return 0; - } + if (tc[port].cc_state == PD_CC_NONE) + return sm_set_state(port, TC_OBJ(port), + tc_ct_unattached_unsupported); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_ct_unattached_vpd_exit(int port) +static int tc_ct_unattached_vpd_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -1404,17 +1338,17 @@ static unsigned int tc_ct_unattached_vpd_exit(int port) * Remove the terminations from Host * Remove the terminations from Charge-Through */ -static unsigned int tc_ct_disabled_vpd(int port, enum signal sig) +static int tc_ct_disabled_vpd(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_disabled_vpd_sig[sig])(port); - return SUPER(ret, sig, tc_host_open_ct_open); + return SM_SUPER(ret, sig, tc_host_open_ct_open); } -static unsigned int tc_ct_disabled_vpd_entry(int port) +static int tc_ct_disabled_vpd_entry(int port) { - tc[port].state_id = CTDISABLED_VPD; + tc[port].state_id = TC_CTDISABLED_VPD; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Get power from VBUS */ @@ -1425,14 +1359,14 @@ static unsigned int tc_ct_disabled_vpd_entry(int port) return 0; } -static unsigned int tc_ct_disabled_vpd_run(int port) +static int tc_ct_disabled_vpd_run(int port) { /* * A Charge-Through VCONN-Powered USB Device shall transition * to Unattached.SNK after tVPDDisable. */ if (get_time().val > tc[port].next_role_swap) - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -1440,20 +1374,20 @@ static unsigned int tc_ct_disabled_vpd_run(int port) /** * CTAttached.VPD */ -static unsigned int tc_ct_attached_vpd(int port, enum signal sig) +static int tc_ct_attached_vpd(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_attached_vpd_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_ct_attached_vpd_entry(int port) +static int tc_ct_attached_vpd_entry(int port) { int cc1; int cc2; - tc[port].state_id = CTATTACHED_VPD; + tc[port].state_id = TC_CTATTACHED_VPD; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Get power from VCONN */ @@ -1501,7 +1435,7 @@ static unsigned int tc_ct_attached_vpd_entry(int port) return 0; } -static unsigned int tc_ct_attached_vpd_run(int port) +static int tc_ct_attached_vpd_run(int port) { int new_cc_state; int cc1; @@ -1511,10 +1445,8 @@ static unsigned int tc_ct_attached_vpd_run(int port) * A Charge-Through VCONN-Powered USB Device shall transition to * CTDisabled.VPD if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_ct_disabled_vpd); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_ct_disabled_vpd); /* Check CT CC for connection */ vpd_ct_get_cc(&cc1, &cc2); @@ -1539,7 +1471,7 @@ static unsigned int tc_ct_attached_vpd_run(int port) * state of the passed-through CC pin is SNK.Open for tVPDCTDD. */ if (tc[port].cc_state == PD_CC_NONE && !vpd_is_ct_vbus_present()) - set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); + sm_set_state(port, TC_OBJ(port), tc_ct_unattached_vpd); return 0; } @@ -1554,17 +1486,17 @@ static unsigned int tc_ct_attached_vpd_run(int port) * Connect Charge-Through Rd * Get power from VCONN */ -static unsigned int tc_ct_attach_wait_vpd(int port, enum signal sig) +static int tc_ct_attach_wait_vpd(int port, enum sm_signal sig) { int ret; ret = (*tc_ct_attach_wait_vpd_sig[sig])(port); - return SUPER(ret, sig, tc_host_rp3_ct_rd); + return SM_SUPER(ret, sig, tc_host_rp3_ct_rd); } -static unsigned int tc_ct_attach_wait_vpd_entry(int port) +static int tc_ct_attach_wait_vpd_entry(int port) { - tc[port].state_id = CTATTACH_WAIT_VPD; + tc[port].state_id = TC_CTATTACH_WAIT_VPD; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -1578,7 +1510,7 @@ static unsigned int tc_ct_attach_wait_vpd_entry(int port) return 0; } -static unsigned int tc_ct_attach_wait_vpd_run(int port) +static int tc_ct_attach_wait_vpd_run(int port) { int new_cc_state; int cc1; @@ -1598,10 +1530,8 @@ static unsigned int tc_ct_attach_wait_vpd_run(int port) * A Charge-Through VCONN-Powered USB Device shall transition to * CTDisabled.VPD if VCONN falls below vVCONNDisconnect. */ - if (!vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_ct_disabled_vpd); - return 0; - } + if (!vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_ct_disabled_vpd); /* Debounce the cc state */ if (new_cc_state != tc[port].cc_state) { @@ -1620,11 +1550,9 @@ static unsigned int tc_ct_attach_wait_vpd_run(int port) * port’s CC1 and CC2 pins are SNK.Open for at least * tPDDebounce. */ - if (tc[port].cc_state == PD_CC_NONE) { - set_state(port, TC_OBJ(port), - tc_ct_unattached_vpd); - return 0; - } + if (tc[port].cc_state == PD_CC_NONE) + return sm_set_state(port, TC_OBJ(port), + tc_ct_unattached_vpd); } if (get_time().val > tc[port].cc_debounce) { @@ -1636,16 +1564,15 @@ static unsigned int tc_ct_attach_wait_vpd_run(int port) * detected. */ if (tc[port].cc_state == PD_CC_DFP_ATTACHED && - vpd_is_ct_vbus_present()) { - set_state(port, TC_OBJ(port), tc_ct_attached_vpd); - return 0; - } + vpd_is_ct_vbus_present()) + return sm_set_state(port, TC_OBJ(port), + tc_ct_attached_vpd); } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_ct_attach_wait_vpd_exit(int port) +static int tc_ct_attach_wait_vpd_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -1659,16 +1586,16 @@ static unsigned int tc_ct_attach_wait_vpd_exit(int port) /** * Super State HOST_RP3_CT_RD */ -static unsigned int tc_host_rp3_ct_rd(int port, enum signal sig) +static int tc_host_rp3_ct_rd(int port, enum sm_signal sig) { int ret; ret = (*tc_host_rp3_ct_rd_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_rp3_ct_rd_entry(int port) +static int tc_host_rp3_ct_rd_entry(int port) { /* Place RP3A0 on Host CC */ vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_3A0); @@ -1683,7 +1610,7 @@ static unsigned int tc_host_rp3_ct_rd_entry(int port) /* Make sure vconn is on */ if (!vpd_is_vconn_present()) - set_state(port, TC_OBJ(port), tc_error_recovery); + sm_set_state(port, TC_OBJ(port), tc_error_recovery); /* Get power from VCONN */ vpd_vconn_pwr_sel_odl(PWR_VCONN); @@ -1691,23 +1618,18 @@ static unsigned int tc_host_rp3_ct_rd_entry(int port) return 0; } -static unsigned int tc_host_rp3_ct_rd_run(int port) -{ - return 0; -} - /** * Super State HOST_RP3_CT_RPU */ -static unsigned int tc_host_rp3_ct_rpu(int port, enum signal sig) +static int tc_host_rp3_ct_rpu(int port, enum sm_signal sig) { int ret; ret = (*tc_host_rp3_ct_rpu_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_rp3_ct_rpu_entry(int port) +static int tc_host_rp3_ct_rpu_entry(int port) { /* Place RP3A0 on Host CC */ vpd_host_set_pull(TYPEC_CC_RP, TYPEC_RP_3A0); @@ -1722,15 +1644,10 @@ static unsigned int tc_host_rp3_ct_rpu_entry(int port) /* Make sure vconn is on */ if (!vpd_is_vconn_present()) - set_state(port, TC_OBJ(port), tc_error_recovery); + sm_set_state(port, TC_OBJ(port), tc_error_recovery); /* Get power from VCONN */ vpd_vconn_pwr_sel_odl(PWR_VCONN); return 0; } - -static unsigned int tc_host_rp3_ct_rpu_run(int port) -{ - return 0; -} diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c new file mode 100644 index 0000000000..cd2d8f3612 --- /dev/null +++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c @@ -0,0 +1,1724 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "charge_manager.h" +#include "charge_state.h" +#include "common.h" +#include "console.h" +#include "hooks.h" +#include "system.h" +#include "task.h" +#include "tcpm.h" +#include "usb_mux.h" +#include "usb_pd.h" +#include "usb_tc_drp_acc_trysrc_sm.h" +#include "usb_tc_sm.h" +#include "usbc_ppc.h" + +/* + * USB Type-C DRP with Accessory and Try.SRC module + * See Figure 4-16 in Release 1.4 of USB Type-C Spec. + */ + +#ifdef CONFIG_COMMON_RUNTIME +#define CPRINTF(format, args...) cprintf(CC_HOOK, format, ## args) +#define CPRINTS(format, args...) cprints(CC_HOOK, format, ## args) +#else /* CONFIG_COMMON_RUNTIME */ +#define CPRINTF(format, args...) +#define CPRINTS(format, args...) +#endif + +/* Type-C Layer Flags */ +#define TC_FLAGS_VCONN_ON BIT(0) +#define TC_FLAGS_TS_DTS_PARTNER BIT(1) +#define TC_FLAGS_VBUS_NEVER_LOW BIT(2) +#define TC_FLAGS_LPM_TRANSITION BIT(3) +#define TC_FLAGS_LPM_ENGAGED BIT(4) +#define TC_FLAGS_LPM_REQUESTED BIT(5) + + +struct type_c tc[CONFIG_USB_PD_PORT_COUNT]; + +/* Port dual-role state */ +enum pd_dual_role_states drp_state[CONFIG_USB_PD_PORT_COUNT] = { + [0 ... (CONFIG_USB_PD_PORT_COUNT - 1)] = + CONFIG_USB_PD_INITIAL_DRP_STATE}; + +/* + * 4 entry rw_hash table of type-C devices that AP has firmware updates for. + */ +#ifdef CONFIG_COMMON_RUNTIME +#define RW_HASH_ENTRIES 4 +static struct ec_params_usb_pd_rw_hash_entry rw_hash_table[RW_HASH_ENTRIES]; +#endif + +static void tc_set_data_role(int port, int role); + +#ifdef CONFIG_USB_PD_TRY_SRC +/* Enable variable for Try.SRC states */ +static uint8_t pd_try_src_enable; +static void pd_update_try_source(void); +#endif + +/* + * Type-C State Hierarchy (Sub-States are listed inside the boxes) + * + * |TC_CC_RD --------------| |TC_CC_RP ------------------------| + * | | | | + * | TC_UNATTACHED_SNK | | TC_UNATTACHED_SRC | + * | TC_ATTACH_WAIT_SNK | | TC_ATTACH_WAIT_SRC | + * | TC_TRY_WAIT_SNK | | TC_TRY_SRC | + * | TC_DBG_ACC_SNK | | TC_UNORIENTED_DBG_ACC_SRC | + * |-----------------------| |---------------------------------| + * + * |TC_CC_OPEN -----------| + * | | + * | TC_DISABLED | + * | TC_ERROR_RECOVERY | + * |----------------------| + * + * TC_ATTACHED_SNK TC_ATTACHED_SRC + * + */ + +/* + * Type-C states + */ +DECLARE_STATE(tc, disabled, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, error_recovery, WITH_RUN, NOOP); +DECLARE_STATE(tc, unattached_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attach_wait_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attached_snk, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, dbg_acc_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, unattached_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, attach_wait_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, attached_src, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, unoriented_dbg_acc_src, WITH_RUN, NOOP); + +#ifdef CONFIG_USB_PD_TRY_SRC +DECLARE_STATE(tc, try_src, WITH_RUN, NOOP); +DECLARE_STATE(tc, try_wait_snk, WITH_RUN, NOOP); +#endif + +/* Super States */ +DECLARE_STATE(tc, cc_rd, NOOP, NOOP); +DECLARE_STATE(tc, cc_rp, NOOP, NOOP); +DECLARE_STATE(tc, cc_open, NOOP, NOOP); + +/* + * Public Functions + * + * NOTE: Functions prefixed with pd_ are defined in usb_pd.h + * Functions prefixed with tc_ are defined int usb_tc_sm.h + */ + +#ifndef CONFIG_USB_PRL_SM + +/* + * These pd_ functions are implemented in common/usb_prl_sm.c + */ + +void pd_transmit_complete(int port, int status) +{ + /* DO NOTHING */ +} + +void pd_execute_hard_reset(int port) +{ + /* DO NOTHING */ +} + +void pd_set_vbus_discharge(int port, int enable) +{ + /* DO NOTHING */ +} + +uint16_t pd_get_identity_vid(int port) +{ + /* DO NOTHING */ + return 0; +} + +#endif /* !defined(CONFIG_USB_PRL_SM) */ + +void pd_update_contract(int port) +{ + /* DO NOTHING */ +} + +void pd_set_new_power_request(int port) +{ + /* DO NOTHING */ +} + +void pd_request_power_swap(int port) +{ + /* DO NOTHING */ +} + +void pd_set_suspend(int port, int enable) +{ + sm_state state; + + if (pd_is_port_enabled(port) == !enable) + return; + + if (enable) + state = tc_disabled; + else + state = (TC_DEFAULT_STATE(port) == TC_UNATTACHED_SRC) ? + tc_unattached_src : tc_unattached_snk; + + sm_set_state(port, TC_OBJ(port), state); +} + +int pd_is_port_enabled(int port) +{ + return !(tc[port].state_id == TC_DISABLED); +} + +int pd_fetch_acc_log_entry(int port) +{ + return EC_RES_SUCCESS; +} + +int pd_get_polarity(int port) +{ + return tc[port].polarity; +} + +int pd_get_role(int port) +{ + return tc[port].data_role; +} + +int pd_is_vbus_present(int port) +{ + if (IS_ENABLED(CONFIG_USB_PD_VBUS_DETECT_TCPC)) + return tcpm_get_vbus_level(port); + else + return pd_snk_is_vbus_provided(port); +} + +void pd_vbus_low(int port) +{ + TC_CLR_FLAG(port, TC_FLAGS_VBUS_NEVER_LOW); +} + +int pd_is_connected(int port) +{ + return (tc[port].state_id == TC_ATTACHED_SNK) || + (tc[port].state_id == TC_ATTACHED_SRC); +} + +#ifdef CONFIG_USB_PD_ALT_MODE_DFP +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +void pd_prepare_sysjump(void) +{ + /* + * We can't be in an alternate mode since PD comm is disabled, so + * no need to send the event + */ +} +#endif + +void tc_src_power_off(int port) +{ + if (tc[port].state_id == TC_ATTACHED_SRC) { + /* Remove VBUS */ + pd_power_supply_reset(port); + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { + charge_manager_set_ceil(port, CEIL_REQUESTOR_PD, + CHARGE_CEIL_NONE); + } + } +} + +void tc_start_error_recovery(int port) +{ + /* + * Async. function call: + * The port should transition to the ErrorRecovery state + * from any other state when directed. + */ + sm_set_state(port, TC_OBJ(port), tc_error_recovery); +} + +void tc_state_init(int port, enum typec_state_id start_state) +{ + int res = 0; + sm_state this_state; + + res = tc_restart_tcpc(port); + if (res) + this_state = tc_disabled; + else + this_state = (start_state == TC_UNATTACHED_SRC) ? + tc_unattached_src : tc_unattached_snk; + + CPRINTS("TCPC p%d init %s", port, res ? "failed" : "ready"); + + sm_init_state(port, TC_OBJ(port), this_state); + + if (IS_ENABLED(CONFIG_USBC_SS_MUX)) + /* Initialize USB mux to its default state */ + usb_mux_init(port); + + tcpm_select_rp_value(port, CONFIG_USB_PD_PULLUP); + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { + /* Initialize PD and type-C supplier current limits to 0 */ + pd_set_input_current_limit(port, 0, 0); + typec_set_input_current_limit(port, 0, 0); + charge_manager_update_dualrole(port, CAP_UNKNOWN); + } + + tc[port].flags = 0; + tc[port].evt_timeout = 5*MSEC; +} + +/* + * Private Functions + */ + +void tc_event_check(int port, int evt) +{ + /* NO EVENTS TO CHECK */ +} + +/* + * CC values for regular sources and Debug sources (aka DTS) + * + * Source type Mode of Operation CC1 CC2 + * --------------------------------------------- + * Regular Default USB Power RpUSB Open + * Regular USB-C @ 1.5 A Rp1A5 Open + * Regular USB-C @ 3 A Rp3A0 Open + * DTS Default USB Power Rp3A0 Rp1A5 + * DTS USB-C @ 1.5 A Rp1A5 RpUSB + * DTS USB-C @ 3 A Rp3A0 RpUSB + */ + +static void tc_set_data_role(int port, int role) +{ + tc[port].data_role = role; + + if (IS_ENABLED(CONFIG_USBC_SS_MUX)) + set_usb_mux_with_current_data_role(port); + + /* Notify TCPC of role update */ + tcpm_set_msg_header(port, tc[port].power_role, tc[port].data_role); +} + +static void sink_stop_drawing_current(int port) +{ + pd_set_input_current_limit(port, 0, 0); + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { + typec_set_input_current_limit(port, 0, 0); + charge_manager_set_ceil(port, + CEIL_REQUESTOR_PD, CHARGE_CEIL_NONE); + } +} + +#ifdef CONFIG_USB_PD_TRY_SRC +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +static void pd_update_try_source(void) +{ + int i; + int try_src = 0; + +#ifndef CONFIG_CHARGER + int batt_soc = board_get_battery_soc(); +#else + int batt_soc = charge_get_percent(); +#endif + + try_src = 0; + for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; i++) + try_src |= drp_state[i] == PD_DRP_TOGGLE_ON; + + /* + * Enable try source when dual-role toggling AND battery is present + * and at some minimum percentage. + */ + pd_try_src_enable = try_src && + batt_soc >= CONFIG_USB_PD_TRY_SRC_MIN_BATT_SOC; + +#ifdef CONFIG_BATTERY_REVIVE_DISCONNECT + /* + * Don't attempt Try.Src if the battery is in the disconnect state. The + * discharge FET may not be enabled and so attempting Try.Src may cut + * off our only power source at the time. + */ + pd_try_src_enable &= (battery_get_disconnect_state() == + BATTERY_NOT_DISCONNECTED); +#elif defined(CONFIG_BATTERY_PRESENT_CUSTOM) || \ + defined(CONFIG_BATTERY_PRESENT_GPIO) + /* + * When battery is cutoff in ship mode it may not be reliable to + * check if battery is present with its state of charge. + * Also check if battery is initialized and ready to provide power. + */ + pd_try_src_enable &= (battery_is_present() == BP_YES); +#endif /* CONFIG_BATTERY_PRESENT_[CUSTOM|GPIO] */ + +} +DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, pd_update_try_source, HOOK_PRIO_DEFAULT); +#endif /* CONFIG_USB_PD_TRY_SRC */ + +#ifdef CONFIG_CMD_PD_DEV_DUMP_INFO +static inline void pd_dev_dump_info(uint16_t dev_id, uint8_t *hash) +{ + int j; + + ccprintf("DevId:%d.%d Hash:", HW_DEV_ID_MAJ(dev_id), + HW_DEV_ID_MIN(dev_id)); + for (j = 0; j < PD_RW_HASH_SIZE; j += 4) { + ccprintf(" 0x%02x%02x%02x%02x", hash[j + 3], hash[j + 2], + hash[j + 1], hash[j]); + } + ccprintf("\n"); +} +#endif /* CONFIG_CMD_PD_DEV_DUMP_INFO */ + +#if defined(CONFIG_CHARGE_MANAGER) +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ + +/* + * Returns type C current limit (mA) based upon cc_voltage (mV). + */ +static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) +{ + typec_current_t charge; + int cc = polarity ? cc2 : cc1; + int cc_alt = polarity ? cc1 : cc2; + + if (cc == TYPEC_CC_VOLT_RP_3_0 && cc_alt != TYPEC_CC_VOLT_RP_1_5) + charge = 3000; + else if (cc == TYPEC_CC_VOLT_RP_1_5) + charge = 1500; + else if (cc == TYPEC_CC_VOLT_RP_DEF) + charge = 500; + else + charge = 0; + + if (cc_is_rp(cc_alt)) + charge |= TYPEC_CURRENT_DTS_MASK; + + return charge; +} +#endif + +static void set_vconn(int port, int enable) +{ + if (enable == TC_CHK_FLAG(port, TC_FLAGS_VCONN_ON)) + return; + + if (enable) + TC_SET_FLAG(port, TC_FLAGS_VCONN_ON); + else + TC_CLR_FLAG(port, TC_FLAGS_VCONN_ON); + + /* + * We always need to tell the TCPC to enable Vconn first, otherwise some + * TCPCs get confused and think the CC line is in over voltage mode and + * immediately disconnects. If there is a PPC, both devices will + * potentially source Vconn, but that should be okay since Vconn has + * "make before break" electrical requirements when swapping anyway. + */ + tcpm_set_vconn(port, enable); + + if (IS_ENABLED(CONFIG_USBC_PPC_VCONN)) + ppc_set_vconn(port, enable); +} + +#ifdef CONFIG_USB_PD_TCPM_TCPCI +static uint32_t pd_ports_to_resume; +static void resume_pd_port(void) +{ + uint32_t port; + uint32_t suspended_ports = atomic_read_clear(&pd_ports_to_resume); + + while (suspended_ports) { + port = __builtin_ctz(suspended_ports); + suspended_ports &= ~(1 << port); + pd_set_suspend(port, 0); + } +} +DECLARE_DEFERRED(resume_pd_port); + +void pd_deferred_resume(int port) +{ + atomic_or(&pd_ports_to_resume, 1 << port); + hook_call_deferred(&resume_pd_port_data, SECOND); +} +#endif /* CONFIG_USB_PD_DEFERRED_RESUME */ + +/* + * HOST COMMANDS + */ +static int hc_pd_ports(struct host_cmd_handler_args *args) +{ + struct ec_response_usb_pd_ports *r = args->response; + + r->num_ports = CONFIG_USB_PD_PORT_COUNT; + args->response_size = sizeof(*r); + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_USB_PD_PORTS, + hc_pd_ports, + EC_VER_MASK(0)); + +static int hc_remote_rw_hash_entry(struct host_cmd_handler_args *args) +{ + int i; + int idx = 0; + int found = 0; + const struct ec_params_usb_pd_rw_hash_entry *p = args->params; + static int rw_hash_next_idx; + + if (!p->dev_id) + return EC_RES_INVALID_PARAM; + + for (i = 0; i < RW_HASH_ENTRIES; i++) { + if (p->dev_id == rw_hash_table[i].dev_id) { + idx = i; + found = 1; + break; + } + } + + if (!found) { + idx = rw_hash_next_idx; + rw_hash_next_idx = rw_hash_next_idx + 1; + if (rw_hash_next_idx == RW_HASH_ENTRIES) + rw_hash_next_idx = 0; + } + + memcpy(&rw_hash_table[idx], p, sizeof(*p)); + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_USB_PD_RW_HASH_ENTRY, + hc_remote_rw_hash_entry, + EC_VER_MASK(0)); + +static int hc_remote_pd_dev_info(struct host_cmd_handler_args *args) +{ + const uint8_t *port = args->params; + struct ec_params_usb_pd_rw_hash_entry *r = args->response; + + if (*port >= CONFIG_USB_PD_PORT_COUNT) + return EC_RES_INVALID_PARAM; + + r->dev_id = tc[*port].dev_id; + + if (r->dev_id) + memcpy(r->dev_rw_hash, tc[*port].dev_rw_hash, PD_RW_HASH_SIZE); + + r->current_image = tc[*port].current_image; + + args->response_size = sizeof(*r); + return EC_RES_SUCCESS; +} + +DECLARE_HOST_COMMAND(EC_CMD_USB_PD_DEV_INFO, + hc_remote_pd_dev_info, + EC_VER_MASK(0)); + +#ifdef CONFIG_USBC_PPC +static void pd_send_hard_reset(int port) +{ + task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SEND_HARD_RESET, 0); +} + +static uint32_t port_oc_reset_req; + +static void re_enable_ports(void) +{ + uint32_t ports = atomic_read_clear(&port_oc_reset_req); + + while (ports) { + int port = __fls(ports); + + ports &= ~BIT(port); + + /* + * Let the board know that the overcurrent is + * over since we're going to attempt re-enabling + * the port. + */ + board_overcurrent_event(port, 0); + + pd_send_hard_reset(port); + /* + * TODO(b/117854867): PD3.0 to send an alert message + * indicating OCP after explicit contract. + */ + } +} +DECLARE_DEFERRED(re_enable_ports); + +void pd_handle_overcurrent(int port) +{ + /* Keep track of the overcurrent events. */ + CPRINTS("C%d: overcurrent!", port); + + if (IS_ENABLED(CONFIG_USB_PD_LOGGING)) + pd_log_event(PD_EVENT_PS_FAULT, PD_LOG_PORT_SIZE(port, 0), + PS_FAULT_OCP, NULL); + + ppc_add_oc_event(port); + /* Let the board specific code know about the OC event. */ + board_overcurrent_event(port, 1); + + /* Wait 1s before trying to re-enable the port. */ + atomic_or(&port_oc_reset_req, BIT(port)); + hook_call_deferred(&re_enable_ports_data, SECOND); +} +#endif /* defined(CONFIG_USBC_PPC) */ + +#ifdef CONFIG_USB_PD_TCPC_LOW_POWER +/* 10 ms is enough time for any TCPC transaction to complete. */ +#define PD_LPM_DEBOUNCE_US (10 * MSEC) + +/* This is only called from the PD tasks that owns the port. */ +static void handle_device_access(int port) +{ + /* This should only be called from the PD task */ + assert(port == TASK_ID_TO_PD_PORT(task_get_current())); + + tc[port].low_power_time = get_time().val + PD_LPM_DEBOUNCE_US; + if (TC_CHK_FLAG(port, TC_FLAGS_LPM_ENGAGED)) { + CPRINTS("TCPC p%d Exit Low Power Mode", port); + TC_CLR_FLAG(port, TC_FLAGS_LPM_ENGAGED | + TC_FLAGS_LPM_REQUESTED); + /* + * Wake to ensure we make another pass through the main task + * loop after clearing the flags. + */ + task_wake(PD_PORT_TO_TASK_ID(port)); + } +} + +static int pd_device_in_low_power(int port) +{ + /* + * If we are actively waking the device up in the PD task, do not + * let TCPC operation wait or retry because we are in low power mode. + */ + if (port == TASK_ID_TO_PD_PORT(task_get_current()) && + TC_CHK_FLAG(port, TC_FLAGS_LPM_TRANSITION)) + return 0; + + return TC_CHK_FLAG(port, TC_FLAGS_LPM_ENGAGED); +} + +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +static int reset_device_and_notify(int port) +{ + int rv; + int task, waiting_tasks; + + /* This should only be called from the PD task */ + assert(port == TASK_ID_TO_PD_PORT(task_get_current())); + + TC_SET_FLAG(port, TC_FLAGS_LPM_TRANSITION); + rv = tcpm_init(port); + TC_CLR_FLAG(port, TC_FLAGS_LPM_TRANSITION); + + if (rv == EC_SUCCESS) + CPRINTS("TCPC p%d init ready", port); + else + CPRINTS("TCPC p%d init failed!", port); + + /* + * Before getting the other tasks that are waiting, clear the reset + * event from this PD task to prevent multiple reset/init events + * occurring. + * + * The double reset event happens when the higher priority PD interrupt + * task gets an interrupt during the above tcpm_init function. When that + * occurs, the higher priority task waits correctly for us to finish + * waking the TCPC, but it has also set PD_EVENT_TCPC_RESET again, which + * would result in a second, unnecessary init. + */ + atomic_clear(task_get_event_bitmap(task_get_current()), + PD_EVENT_TCPC_RESET); + + waiting_tasks = atomic_read_clear(&tc[port].tasks_waiting_on_reset); + + /* + * Now that we are done waking up the device, handle device access + * manually because we ignored it while waking up device. + */ + handle_device_access(port); + + /* Clear SW LPM state; the state machine will set it again if needed */ + TC_CLR_FLAG(port, TC_FLAGS_LPM_REQUESTED); + + /* Wake up all waiting tasks. */ + while (waiting_tasks) { + task = __fls(waiting_tasks); + waiting_tasks &= ~BIT(task); + task_set_event(task, TASK_EVENT_PD_AWAKE, 0); + } + + return rv; +} + +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +static void pd_wait_for_wakeup(int port) +{ + if (port == TASK_ID_TO_PD_PORT(task_get_current())) { + /* If we are in the PD task, we can directly reset */ + reset_device_and_notify(port); + } else { + /* Otherwise, we need to wait for the TCPC reset to complete */ + atomic_or(&tc[port].tasks_waiting_on_reset, + 1 << task_get_current()); + /* + * NOTE: We could be sending the PD task the reset event while + * it is already processing the reset event. If that occurs, + * then we will reset the TCPC multiple times, which is + * undesirable but most likely benign. Empirically, this doesn't + * happen much, but it if starts occurring, we can add a guard + * to prevent/reduce it. + */ + task_set_event(PD_PORT_TO_TASK_ID(port), + PD_EVENT_TCPC_RESET, 0); + task_wait_event_mask(TASK_EVENT_PD_AWAKE, -1); + } +} + +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +void pd_wait_exit_low_power(int port) +{ + if (pd_device_in_low_power(port)) + pd_wait_for_wakeup(port); +} + +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +/* + * This can be called from any task. If we are in the PD task, we can handle + * immediately. Otherwise, we need to notify the PD task via event. + */ +void pd_device_accessed(int port) +{ + if (port == TASK_ID_TO_PD_PORT(task_get_current())) { + /* Ignore any access to device while it is waking up */ + if (TC_CHK_FLAG(port, TC_FLAGS_LPM_TRANSITION)) + return; + + handle_device_access(port); + } else { + task_set_event(PD_PORT_TO_TASK_ID(port), + PD_EVENT_DEVICE_ACCESSED, 0); + } +} + +/* + * TODO(b/137493121): Move this function to a separate file that's shared + * between the this and the original stack. + */ +void pd_prevent_low_power_mode(int port, int prevent) +{ + const int current_task_mask = (1 << task_get_current()); + + if (prevent) + atomic_or(&tc[port].tasks_preventing_lpm, current_task_mask); + else + atomic_clear(&tc[port].tasks_preventing_lpm, current_task_mask); +} + +#endif /* CONFIG_USB_PD_TCPC_LOW_POWER */ + +static void sink_power_sub_states(int port) +{ + int cc1; + int cc2; + enum tcpc_cc_voltage_status new_cc_voltage; + + tcpm_get_cc(port, &cc1, &cc2); + + if (tc[port].polarity) + cc1 = cc2; + + if (cc1 == TYPEC_CC_VOLT_RP_DEF) + new_cc_voltage = TYPEC_CC_VOLT_RP_DEF; + else if (cc1 == TYPEC_CC_VOLT_RP_1_5) + new_cc_voltage = TYPEC_CC_VOLT_RP_1_5; + else if (cc1 == TYPEC_CC_VOLT_RP_3_0) + new_cc_voltage = TYPEC_CC_VOLT_RP_3_0; + else + new_cc_voltage = TYPEC_CC_VOLT_OPEN; + + /* Debounce the cc state */ + if (new_cc_voltage != tc[port].cc_voltage) { + tc[port].cc_voltage = new_cc_voltage; + tc[port].cc_debounce = + get_time().val + PD_T_RP_VALUE_CHANGE; + return; + } + + if (tc[port].cc_debounce == 0 || + get_time().val < tc[port].cc_debounce) + return; + + tc[port].cc_debounce = 0; + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { + tc[port].typec_curr = + get_typec_current_limit(tc[port].polarity, cc1, cc1); + + typec_set_input_current_limit(port, + tc[port].typec_curr, TYPE_C_VOLTAGE); + charge_manager_update_dualrole(port, CAP_DEDICATED); + } +} + + +/* + * TYPE-C State Implementations + */ + +/** + * Disabled + * + * Super State Entry Actions: + * Remove the terminations from CC + * Set's VBUS and VCONN off + */ +static int tc_disabled(int port, enum sm_signal sig) +{ + int ret = 0; + + ret = (*tc_disabled_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_open); +} + +static int tc_disabled_entry(int port) +{ + tc[port].state_id = TC_DISABLED; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + return 0; +} + +static int tc_disabled_run(int port) +{ + task_wait_event(-1); + return SM_RUN_SUPER; +} + +static int tc_disabled_exit(int port) +{ + if (!IS_ENABLED(CONFIG_USB_PD_TCPC)) { + if (tc_restart_tcpc(port) != 0) { + CPRINTS("TCPC p%d restart failed!", port); + return 0; + } + } + + CPRINTS("TCPC p%d resumed!", port); + + return 0; +} + +/** + * ErrorRecovery + * + * Super State Entry Actions: + * Remove the terminations from CC + * Set's VBUS and VCONN off + */ +static int tc_error_recovery(int port, enum sm_signal sig) +{ + int ret = 0; + + ret = (*tc_error_recovery_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_open); +} + +static int tc_error_recovery_entry(int port) +{ + tc[port].state_id = TC_ERROR_RECOVERY; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + tc[port].timeout = get_time().val + PD_T_ERROR_RECOVERY; + return 0; +} + +static int tc_error_recovery_run(int port) +{ + if (tc[port].timeout > 0 && get_time().val > tc[port].timeout) { + tc[port].timeout = 0; + tc_state_init(port, TC_UNATTACHED_SRC); + } + + return 0; +} + +/** + * Unattached.SNK + * + * Super State Entry Actions: + * Vconn Off + * Place Rd on CC + * Set power role to SINK + */ +static int tc_unattached_snk(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_unattached_snk_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rd); +} + +static int tc_unattached_snk_entry(int port) +{ + tc[port].state_id = TC_UNATTACHED_SNK; + if (tc[port].obj.last_state != tc_unattached_src) + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) + charge_manager_update_dualrole(port, CAP_UNKNOWN); + + /* + * Indicate that the port is disconnected so the board + * can restore state from any previous data swap. + */ + pd_execute_data_swap(port, PD_ROLE_DISCONNECTED); + tc[port].next_role_swap = get_time().val + PD_T_DRP_SNK; + + return 0; +} + +static int tc_unattached_snk_run(int port) +{ + int cc1; + int cc2; + + /* + * TODO(b/137498392): Add wait before sampling the CC + * status after role changes + */ + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + /* + * The port shall transition to AttachWait.SNK when a Source + * connection is detected, as indicated by the SNK.Rp state + * on at least one of its CC pins. + * + * A DRP shall transition to Unattached.SRC within tDRPTransition + * after the state of both CC pins is SNK.Open for + * tDRP − dcSRC.DRP ∙ tDRP. + */ + if (cc_is_rp(cc1) || cc_is_rp(cc2)) { + /* Connection Detected */ + sm_set_state(port, TC_OBJ(port), tc_attach_wait_snk); + } else if (get_time().val > tc[port].next_role_swap) { + /* DRP Toggle */ + sm_set_state(port, TC_OBJ(port), tc_unattached_src); + } + + return 0; +} + +/** + * AttachWait.SNK + * + * Super State Entry Actions: + * Vconn Off + * Place Rd on CC + * Set power role to SINK + */ +static int tc_attach_wait_snk(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_attach_wait_snk_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rd); +} + +static int tc_attach_wait_snk_entry(int port) +{ + tc[port].state_id = TC_ATTACH_WAIT_SNK; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + tc[port].cc_state = PD_CC_UNSET; + return 0; +} + +static int tc_attach_wait_snk_run(int port) +{ + int cc1; + int cc2; + enum pd_cc_states new_cc_state; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + if (cc_is_rp(cc1) && cc_is_rp(cc2)) + new_cc_state = PD_CC_DEBUG_ACC; + else if (cc_is_rp(cc1) || cc_is_rp(cc2)) + new_cc_state = PD_CC_DFP_ATTACHED; + else + new_cc_state = PD_CC_NONE; + + /* Debounce the cc state */ + if (new_cc_state != tc[port].cc_state) { + tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; + tc[port].pd_debounce = get_time().val + PD_T_PD_DEBOUNCE; + tc[port].cc_state = new_cc_state; + return 0; + } + + /* + * A DRP shall transition to Unattached.SNK when the state of both + * the CC1 and CC2 pins is SNK.Open for at least tPDDebounce. + */ + if (new_cc_state == PD_CC_NONE && + get_time().val > tc[port].pd_debounce) { + /* We are detached */ + return sm_set_state(port, TC_OBJ(port), tc_unattached_src); + } + + /* Wait for CC debounce */ + if (get_time().val < tc[port].cc_debounce) + return 0; + + /* + * The port shall transition to Attached.SNK after the state of only one + * of the CC1 or CC2 pins is SNK.Rp for at least tCCDebounce and VBUS is + * detected. + * + * A DRP that strongly prefers the Source role may optionally transition + * to Try.SRC instead of Attached.SNK when the state of only one CC pin + * has been SNK.Rp for at least tCCDebounce and VBUS is detected. + * + * If the port supports Debug Accessory Mode, the port shall transition + * to DebugAccessory.SNK if the state of both the CC1 and CC2 pins is + * SNK.Rp for at least tCCDebounce and VBUS is detected. + */ + if (pd_is_vbus_present(port)) { + if (new_cc_state == PD_CC_DFP_ATTACHED) { +#ifdef CONFIG_USB_PD_TRY_SRC + if (pd_try_src_enable) + sm_set_state(port, TC_OBJ(port), tc_try_src); + else +#endif + sm_set_state(port, TC_OBJ(port), + tc_attached_snk); + } else { + /* new_cc_state is PD_CC_DEBUG_ACC */ + TC_SET_FLAG(port, TC_FLAGS_TS_DTS_PARTNER); + sm_set_state(port, TC_OBJ(port), tc_dbg_acc_snk); + } + } + + return SM_RUN_SUPER; +} + +/** + * Attached.SNK + */ +static int tc_attached_snk(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_attached_snk_sig[sig])(port); + return SM_SUPER(ret, sig, 0); +} + +static int tc_attached_snk_entry(int port) +{ + int cc1; + int cc2; + + tc[port].state_id = TC_ATTACHED_SNK; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + /* Get connector orientation */ + tcpm_get_cc(port, &cc1, &cc2); + tc[port].polarity = get_snk_polarity(cc1, cc2); + set_polarity(port, tc[port].polarity); + + /* + * Initial data role for sink is UFP + * This also sets the usb mux + */ + tc_set_data_role(port, PD_ROLE_UFP); + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) { + tc[port].typec_curr = + get_typec_current_limit(tc[port].polarity, cc1, cc2); + typec_set_input_current_limit(port, tc[port].typec_curr, + TYPE_C_VOLTAGE); + charge_manager_update_dualrole(port, CAP_DEDICATED); + tc[port].cc_state = (tc[port].polarity) ? cc2 : cc1; + } + + /* Apply Rd */ + tcpm_set_cc(port, TYPEC_CC_RD); + + tc[port].cc_debounce = 0; + return 0; +} + +static int tc_attached_snk_run(int port) +{ + /* Detach detection */ + if (!pd_is_vbus_present(port)) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + + /* Run Sink Power Sub-State */ + sink_power_sub_states(port); + + return 0; +} + +static int tc_attached_snk_exit(int port) +{ + /* Stop drawing power */ + sink_stop_drawing_current(port); + + return 0; +} + +/** + * UnorientedDebugAccessory.SRC + * + * Super State Entry Actions: + * Vconn Off + * Place Rp on CC + * Set power role to SOURCE + */ +static int tc_unoriented_dbg_acc_src(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_unoriented_dbg_acc_src_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rp); +} + +static int tc_unoriented_dbg_acc_src_entry(int port) +{ + tc[port].state_id = TC_UNORIENTED_DEBUG_ACCESSORY_SRC; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + /* Enable VBUS */ + pd_set_power_supply_ready(port); + + /* Any board specific unoriented debug setup should be added below */ + + return 0; +} + +static int tc_unoriented_dbg_acc_src_run(int port) +{ + int cc1; + int cc2; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + /* + * A DRP, the port shall transition to Unattached.SNK when the + * SRC.Open state is detected on either the CC1 or CC2 pin. + */ + if (cc1 == TYPEC_CC_VOLT_OPEN || cc2 == TYPEC_CC_VOLT_OPEN) { + /* Remove VBUS */ + pd_power_supply_reset(port); + charge_manager_set_ceil(port, CEIL_REQUESTOR_PD, + CHARGE_CEIL_NONE); + + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + } + + return 0; +} + +/** + * Debug Accessory.SNK + * + * Super State Entry Actions: + * Vconn Off + * Place Rd on CC + * Set power role to SINK + */ +static int tc_dbg_acc_snk(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_dbg_acc_snk_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rd); +} + +static int tc_dbg_acc_snk_entry(int port) +{ + tc[port].state_id = TC_DEBUG_ACCESSORY_SNK; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + /* + * TODO(b/137759869): Board specific debug accessory setup should + * be add here. + */ + + return 0; +} + +static int tc_dbg_acc_snk_run(int port) +{ + if (!pd_is_vbus_present(port)) + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + + return 0; +} + +/** + * Unattached.SRC + * + * Super State Entry Actions: + * Vconn Off + * Place Rp on CC + * Set power role to SOURCE + */ +static int tc_unattached_src(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_unattached_src_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rp); +} + +static int tc_unattached_src_entry(int port) +{ + tc[port].state_id = TC_UNATTACHED_SRC; + if (tc[port].obj.last_state != tc_unattached_snk) + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + if (IS_ENABLED(CONFIG_USBC_PPC)) { + /* There is no sink connected. */ + ppc_sink_is_connected(port, 0); + + /* + * Clear the overcurrent event counter + * since we've detected a disconnect. + */ + ppc_clear_oc_event_counter(port); + } + + if (IS_ENABLED(CONFIG_CHARGE_MANAGER)) + charge_manager_update_dualrole(port, CAP_UNKNOWN); + + tc_set_data_role(port, PD_ROLE_DFP); + + /* + * Indicate that the port is disconnected so the board + * can restore state from any previous data swap. + */ + pd_execute_data_swap(port, PD_ROLE_DISCONNECTED); + + tc[port].next_role_swap = get_time().val + PD_T_DRP_SRC; + + return 0; +} + +static int tc_unattached_src_run(int port) +{ + int cc1; + int cc2; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + /* + * Transition to AttachWait.SRC when VBUS is vSafe0V and: + * 1) The SRC.Rd state is detected on either CC1 or CC2 pin or + * 2) The SRC.Ra state is detected on both the CC1 and CC2 pins. + * + * A DRP shall transition to Unattached.SNK within tDRPTransition + * after dcSRC.DRP ∙ tDRP + */ + if (cc_is_at_least_one_rd(cc1, cc2) || cc_is_audio_acc(cc1, cc2)) + sm_set_state(port, TC_OBJ(port), tc_attach_wait_src); + else if (get_time().val > tc[port].next_role_swap) + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + + return SM_RUN_SUPER; +} + +/** + * AttachWait.SRC + * + * Super State Entry Actions: + * Vconn Off + * Place Rp on CC + * Set power role to SOURCE + */ +static int tc_attach_wait_src(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_attach_wait_src_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rp); +} + +static int tc_attach_wait_src_entry(int port) +{ + tc[port].state_id = TC_ATTACH_WAIT_SRC; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + tc[port].cc_state = PD_CC_UNSET; + + return 0; +} + +static int tc_attach_wait_src_run(int port) +{ + int cc1; + int cc2; + enum pd_cc_states new_cc_state; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + /* Debug accessory */ + if (cc_is_snk_dbg_acc(cc1, cc2)) { + /* Debug accessory */ + new_cc_state = PD_CC_DEBUG_ACC; + } else if (cc_is_at_least_one_rd(cc1, cc2)) { + /* UFP attached */ + new_cc_state = PD_CC_UFP_ATTACHED; + } else if (cc_is_audio_acc(cc1, cc2)) { + /* AUDIO Accessory not supported. Just ignore */ + new_cc_state = PD_CC_AUDIO_ACC; + } else { + /* No UFP */ + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + } + + /* Debounce the cc state */ + if (new_cc_state != tc[port].cc_state) { + tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; + tc[port].cc_state = new_cc_state; + return 0; + } + + /* Wait for CC debounce */ + if (get_time().val < tc[port].cc_debounce) + return 0; + + /* + * The port shall transition to Attached.SRC when VBUS is at vSafe0V + * and the SRC.Rd state is detected on exactly one of the CC1 or CC2 + * pins for at least tCCDebounce. + * + * If the port supports Debug Accessory Mode, it shall transition to + * UnorientedDebugAccessory.SRC when VBUS is at vSafe0V and the SRC.Rd + * state is detected on both the CC1 and CC2 pins for at least + * tCCDebounce. + */ + if (!pd_is_vbus_present(port)) { + if (new_cc_state == PD_CC_UFP_ATTACHED) + return sm_set_state(port, TC_OBJ(port), + tc_attached_src); + else if (new_cc_state == PD_CC_DEBUG_ACC) + return sm_set_state(port, TC_OBJ(port), + tc_unoriented_dbg_acc_src); + } + + return 0; +} + +/** + * Attached.SRC + */ +static int tc_attached_src(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_attached_src_sig[sig])(port); + return SM_SUPER(ret, sig, 0); +} + +static int tc_attached_src_entry(int port) +{ + int cc1; + int cc2; + + tc[port].state_id = TC_ATTACHED_SRC; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + /* Get connector orientation */ + tcpm_get_cc(port, &cc1, &cc2); + tc[port].polarity = (cc1 != TYPEC_CC_VOLT_RD); + set_polarity(port, tc[port].polarity); + + /* + * Initial data role for sink is DFP + * This also sets the usb mux + */ + tc_set_data_role(port, PD_ROLE_DFP); + + /* + * Start sourcing Vconn before Vbus to ensure + * we are within USB Type-C Spec 1.4 tVconnON + */ + if (IS_ENABLED(CONFIG_USBC_VCONN)) + set_vconn(port, 1); + + /* Enable VBUS */ + if (pd_set_power_supply_ready(port)) { + /* Stop sourcing Vconn if Vbus failed */ + if (IS_ENABLED(CONFIG_USBC_VCONN)) + set_vconn(port, 0); + + if (IS_ENABLED(CONFIG_USBC_SS_MUX)) + usb_mux_set(port, TYPEC_MUX_NONE, + USB_SWITCH_DISCONNECT, tc[port].polarity); + } + + /* Apply Rp */ + tcpm_set_cc(port, TYPEC_CC_RP); + + tc[port].cc_debounce = 0; + tc[port].cc_state = PD_CC_NONE; + + /* Inform PPC that a sink is connected. */ + if (IS_ENABLED(CONFIG_USBC_PPC)) + ppc_sink_is_connected(port, 1); + + return 0; +} + +static int tc_attached_src_run(int port) +{ + int cc1; + int cc2; + enum pd_cc_states new_cc_state; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + if (tc[port].polarity) + cc1 = cc2; + + if (cc1 == TYPEC_CC_VOLT_OPEN) + new_cc_state = PD_CC_NO_UFP; + else + new_cc_state = PD_CC_NONE; + + /* Debounce the cc state */ + if (new_cc_state != tc[port].cc_state) { + tc[port].cc_state = new_cc_state; + tc[port].cc_debounce = get_time().val + PD_T_SRC_DISCONNECT; + } + + if (get_time().val < tc[port].cc_debounce) + return 0; + + /* + * When the SRC.Open state is detected on the monitored CC pin, a DRP + * shall transition to Unattached.SNK unless it strongly prefers the + * Source role. In that case, it shall transition to TryWait.SNK. + * This transition to TryWait.SNK is needed so that two devices that + * both prefer the Source role do not loop endlessly between Source + * and Sink. In other words, a DRP that would enter Try.SRC from + * AttachWait.SNK shall enter TryWait.SNK for a Sink detach from + * Attached.SRC. + */ + if (tc[port].cc_state == PD_CC_NO_UFP) { + if (IS_ENABLED(CONFIG_USB_PD_TRY_SRC)) + return sm_set_state(port, TC_OBJ(port), + tc_try_wait_snk); + else + return sm_set_state(port, TC_OBJ(port), + tc_unattached_snk); + } + + return 0; +} + +static int tc_attached_src_exit(int port) +{ + /* + * A port shall cease to supply VBUS within tVBUSOFF of exiting + * Attached.SRC. + */ + tc_src_power_off(port); + + return 0; +} + +/** + * Try.SRC + * + * Super State Entry Actions: + * Vconn Off + * Place Rp on CC + * Set power role to SOURCE + */ +#ifdef CONFIG_USB_PD_TRY_SRC +static int tc_try_src(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_try_src_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rp); +} + +static int tc_try_src_entry(int port) +{ + tc[port].state_id = TC_TRY_SRC; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + tc[port].cc_state = PD_CC_UNSET; + tc[port].try_wait_debounce = get_time().val + PD_T_DRP_TRY; + tc[port].timeout = get_time().val + PD_T_TRY_TIMEOUT; + return 0; +} + +static int tc_try_src_run(int port) +{ + int cc1; + int cc2; + enum pd_cc_states new_cc_state; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + if ((cc1 == TYPEC_CC_VOLT_RD && cc2 != TYPEC_CC_VOLT_RD) || + (cc1 != TYPEC_CC_VOLT_RD && cc2 == TYPEC_CC_VOLT_RD)) + new_cc_state = PD_CC_UFP_ATTACHED; + else + new_cc_state = PD_CC_NONE; + + /* Debounce the cc state */ + if (new_cc_state != tc[port].cc_state) { + tc[port].cc_state = new_cc_state; + tc[port].cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; + } + + /* + * The port shall transition to Attached.SRC when the SRC.Rd state is + * detected on exactly one of the CC1 or CC2 pins for at least + * tTryCCDebounce. + */ + if (get_time().val > tc[port].cc_debounce) { + if (new_cc_state == PD_CC_UFP_ATTACHED) + sm_set_state(port, TC_OBJ(port), tc_attached_src); + } + + /* + * The port shall transition to TryWait.SNK after tDRPTry and the + * SRC.Rd state has not been detected and VBUS is within vSafe0V, + * or after tTryTimeout and the SRC.Rd state has not been detected. + */ + if (new_cc_state == PD_CC_NONE) { + if ((get_time().val > tc[port].try_wait_debounce && + !pd_is_vbus_present(port)) || + get_time().val > tc[port].timeout) { + sm_set_state(port, TC_OBJ(port), tc_try_wait_snk); + } + } + + return 0; +} + +/** + * TryWait.SNK + * + * Super State Entry Actions: + * Vconn Off + * Place Rd on CC + * Set power role to SINK + */ +static int tc_try_wait_snk(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_try_wait_snk_sig[sig])(port); + return SM_SUPER(ret, sig, tc_cc_rd); +} + +static int tc_try_wait_snk_entry(int port) +{ + tc[port].state_id = TC_TRY_WAIT_SNK; + CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); + + tc[port].cc_state = PD_CC_UNSET; + tc[port].try_wait_debounce = get_time().val + PD_T_CC_DEBOUNCE; + + return 0; +} + +static int tc_try_wait_snk_run(int port) +{ + int cc1; + int cc2; + enum pd_cc_states new_cc_state; + + /* Check for connection */ + tcpm_get_cc(port, &cc1, &cc2); + + /* We only care about CCs being open */ + if (cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN) + new_cc_state = PD_CC_NONE; + else + new_cc_state = PD_CC_UNSET; + + /* Debounce the cc state */ + if (new_cc_state != tc[port].cc_state) { + tc[port].cc_state = new_cc_state; + tc[port].pd_debounce = get_time().val + PD_T_PD_DEBOUNCE; + } + + /* + * The port shall transition to Unattached.SNK when the state of both + * of the CC1 and CC2 pins is SNK.Open for at least tPDDebounce. + */ + if ((get_time().val > tc[port].pd_debounce) && + (new_cc_state == PD_CC_NONE)) { + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); + } + + /* + * The port shall transition to Attached.SNK after tCCDebounce if or + * when VBUS is detected. + */ + if (get_time().val > tc[port].try_wait_debounce && + pd_is_vbus_present(port)) + sm_set_state(port, TC_OBJ(port), tc_attached_snk); + + return 0; +} + +#endif + +/** + * Super State CC_RD + */ +static int tc_cc_rd(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_cc_rd_sig[sig])(port); + return SM_SUPER(ret, sig, 0); +} + +static int tc_cc_rd_entry(int port) +{ + /* Disable VCONN */ + if (IS_ENABLED(CONFIG_USBC_VCONN)) + set_vconn(port, 0); + + /* + * Both CC1 and CC2 pins shall be independently terminated to + * ground through Rd. + */ + tcpm_set_cc(port, TYPEC_CC_RD); + + /* Set power role to sink */ + tc_set_power_role(port, PD_ROLE_SINK); + tcpm_set_msg_header(port, tc[port].power_role, tc[port].data_role); + + return 0; +} + +/** + * Super State CC_RP + */ +static int tc_cc_rp(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_cc_rp_sig[sig])(port); + return SM_SUPER(ret, sig, 0); +} + +static int tc_cc_rp_entry(int port) +{ + /* Disable VCONN */ + if (IS_ENABLED(CONFIG_USBC_VCONN)) + set_vconn(port, 0); + + /* Set power role to source */ + tc_set_power_role(port, PD_ROLE_SOURCE); + tcpm_set_msg_header(port, tc[port].power_role, tc[port].data_role); + + /* + * Both CC1 and CC2 pins shall be independently pulled + * up through Rp. + */ + tcpm_select_rp_value(port, CONFIG_USB_PD_PULLUP); + tcpm_set_cc(port, TYPEC_CC_RP); + + return 0; +} + +/** + * Super State CC_OPEN + */ +static int tc_cc_open(int port, enum sm_signal sig) +{ + int ret; + + ret = (*tc_cc_open_sig[sig])(port); + return SM_SUPER(ret, sig, 0); +} + +static int tc_cc_open_entry(int port) +{ + /* Disable VBUS */ + pd_power_supply_reset(port); + + /* Disable VCONN */ + if (IS_ENABLED(CONFIG_USBC_VCONN)) + set_vconn(port, 0); + + /* Remove terminations from CC */ + tcpm_set_cc(port, TYPEC_CC_OPEN); + + if (IS_ENABLED(CONFIG_USBC_PPC)) { + /* There is no sink connected. */ + ppc_sink_is_connected(port, 0); + + /* + * Clear the overcurrent event counter + * since we've detected a disconnect. + */ + ppc_clear_oc_event_counter(port); + } + + return 0; +} diff --git a/common/usb_tc_vpd_sm.c b/common/usbc/usb_tc_vpd_sm.c index 33762257a0..b8002b0e2a 100644 --- a/common/usb_tc_vpd_sm.c +++ b/common/usbc/usb_tc_vpd_sm.c @@ -26,10 +26,6 @@ /* Type-C Layer Flags */ #define TC_FLAGS_VCONN_ON BIT(0) -#undef PD_DEFAULT_STATE -/* Port default state at startup */ -#define PD_DEFAULT_STATE(port) tc_unattached_snk - /** * This is the Type-C Port object that contains information needed to * implement a VCONN Powered Device. @@ -37,17 +33,17 @@ struct type_c tc[CONFIG_USB_PD_PORT_COUNT]; /* Type-C states */ -DECLARE_STATE(tc, disabled, WITH_EXIT); -DECLARE_STATE(tc, unattached_snk, NOOP_EXIT); -DECLARE_STATE(tc, attach_wait_snk, NOOP_EXIT); -DECLARE_STATE(tc, attached_snk, WITH_EXIT); +DECLARE_STATE(tc, disabled, WITH_RUN, WITH_EXIT); +DECLARE_STATE(tc, unattached_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attach_wait_snk, WITH_RUN, NOOP); +DECLARE_STATE(tc, attached_snk, WITH_RUN, WITH_EXIT); /* Super States */ -DECLARE_STATE(tc, host_rard, NOOP_EXIT); -DECLARE_STATE(tc, host_open, NOOP_EXIT); -DECLARE_STATE(tc, vbus_cc_iso, NOOP_EXIT); +DECLARE_STATE(tc, host_rard, NOOP, NOOP); +DECLARE_STATE(tc, host_open, NOOP, NOOP); +DECLARE_STATE(tc, vbus_cc_iso, NOOP, NOOP); -void tc_state_init(int port) +void tc_state_init(int port, enum typec_state_id start_state) { int res = 0; sm_state this_state; @@ -55,12 +51,12 @@ void tc_state_init(int port) res = tc_restart_tcpc(port); CPRINTS("TCPC p%d init %s", port, res ? "failed" : "ready"); - this_state = res ? tc_disabled : PD_DEFAULT_STATE(port); + this_state = res ? tc_disabled : tc_unattached_snk; /* Disable TCPC RX until connection is established */ tcpm_set_rx_enable(port, 0); - init_state(port, TC_OBJ(port), this_state); + sm_init_state(port, TC_OBJ(port), this_state); /* Disable pd state machines */ tc[port].pd_enable = 0; @@ -82,30 +78,30 @@ void tc_event_check(int port, int evt) * Enable mcu communication * Remove the terminations from Host CC */ -static unsigned int tc_disabled(int port, enum signal sig) +static int tc_disabled(int port, enum sm_signal sig) { int ret = 0; ret = (*tc_disabled_sig[sig])(port); - return SUPER(ret, sig, tc_host_open); + return SM_SUPER(ret, sig, tc_host_open); } -static unsigned int tc_disabled_entry(int port) +static int tc_disabled_entry(int port) { - tc[port].state_id = DISABLED; + tc[port].state_id = TC_DISABLED; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); return 0; } -static unsigned int tc_disabled_run(int port) +static int tc_disabled_run(int port) { task_wait_event(-1); - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int tc_disabled_exit(int port) +static int tc_disabled_exit(int port) { #ifndef CONFIG_USB_PD_TCPC if (tc_restart_tcpc(port) != 0) { @@ -114,7 +110,7 @@ static unsigned int tc_disabled_exit(int port) } #endif CPRINTS("TCPC p%d resumed!", port); - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -126,23 +122,23 @@ static unsigned int tc_disabled_exit(int port) * Enable mcu communication * Place Ra on VCONN and Rd on Host CC */ -static unsigned int tc_unattached_snk(int port, enum signal sig) +static int tc_unattached_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_unattached_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rard); + return SM_SUPER(ret, sig, tc_host_rard); } -static unsigned int tc_unattached_snk_entry(int port) +static int tc_unattached_snk_entry(int port) { - tc[port].state_id = UNATTACHED_SNK; + tc[port].state_id = TC_UNATTACHED_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); return 0; } -static unsigned int tc_unattached_snk_run(int port) +static int tc_unattached_snk_run(int port) { int host_cc; @@ -154,12 +150,10 @@ static unsigned int tc_unattached_snk_run(int port) * detected, as indicated by the SNK.Rp state on its Host-side * port’s CC pin. */ - if (cc_is_rp(host_cc)) { - set_state(port, TC_OBJ(port), tc_attach_wait_snk); - return 0; - } + if (cc_is_rp(host_cc)) + return sm_set_state(port, TC_OBJ(port), tc_attach_wait_snk); - return RUN_SUPER; + return SM_RUN_SUPER; } /** @@ -169,24 +163,24 @@ static unsigned int tc_unattached_snk_run(int port) * Enable mcu communication * Place Ra on VCONN and Rd on Host CC */ -static unsigned int tc_attach_wait_snk(int port, enum signal sig) +static int tc_attach_wait_snk(int port, enum sm_signal sig) { int ret = 0; ret = (*tc_attach_wait_snk_sig[sig])(port); - return SUPER(ret, sig, tc_host_rard); + return SM_SUPER(ret, sig, tc_host_rard); } -static unsigned int tc_attach_wait_snk_entry(int port) +static int tc_attach_wait_snk_entry(int port) { - tc[port].state_id = ATTACH_WAIT_SNK; + tc[port].state_id = TC_ATTACH_WAIT_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); tc[port].host_cc_state = PD_CC_UNSET; return 0; } -static unsigned int tc_attach_wait_snk_run(int port) +static int tc_attach_wait_snk_run(int port) { int host_new_cc_state; int host_cc; @@ -227,9 +221,9 @@ static unsigned int tc_attach_wait_snk_run(int port) */ if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED && (vpd_is_vconn_present() || vpd_is_host_vbus_present())) - set_state(port, TC_OBJ(port), tc_attached_snk); + sm_set_state(port, TC_OBJ(port), tc_attached_snk); else if (tc[port].host_cc_state == PD_CC_NONE) - set_state(port, TC_OBJ(port), tc_unattached_snk); + sm_set_state(port, TC_OBJ(port), tc_unattached_snk); return 0; } @@ -237,17 +231,17 @@ static unsigned int tc_attach_wait_snk_run(int port) /** * Attached.SNK */ -static unsigned int tc_attached_snk(int port, enum signal sig) +static int tc_attached_snk(int port, enum sm_signal sig) { int ret; ret = (*tc_attached_snk_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_attached_snk_entry(int port) +static int tc_attached_snk_entry(int port) { - tc[port].state_id = ATTACHED_SNK; + tc[port].state_id = TC_ATTACHED_SNK; CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]); /* Enable PD */ @@ -257,13 +251,11 @@ static unsigned int tc_attached_snk_entry(int port) return 0; } -static unsigned int tc_attached_snk_run(int port) +static int tc_attached_snk_run(int port) { /* Has host vbus and vconn been removed */ - if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) { - set_state(port, TC_OBJ(port), tc_unattached_snk); - return 0; - } + if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) + return sm_set_state(port, TC_OBJ(port), tc_unattached_snk); if (vpd_is_vconn_present()) { if (!(tc[port].flags & TC_FLAGS_VCONN_ON)) { @@ -276,7 +268,7 @@ static unsigned int tc_attached_snk_run(int port) return 0; } -static unsigned int tc_attached_snk_exit(int port) +static int tc_attached_snk_exit(int port) { /* Disable PD */ tc[port].pd_enable = 0; @@ -288,15 +280,15 @@ static unsigned int tc_attached_snk_exit(int port) /** * Super State HOST_RARD */ -static unsigned int tc_host_rard(int port, enum signal sig) +static int tc_host_rard(int port, enum sm_signal sig) { int ret; ret = (*tc_host_rard_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_rard_entry(int port) +static int tc_host_rard_entry(int port) { /* Place Ra on VCONN and Rd on Host CC */ vpd_host_set_pull(TYPEC_CC_RA_RD, 0); @@ -304,23 +296,18 @@ static unsigned int tc_host_rard_entry(int port) return 0; } -static unsigned int tc_host_rard_run(int port) -{ - return RUN_SUPER; -} - /** * Super State HOST_OPEN */ -static unsigned int tc_host_open(int port, enum signal sig) +static int tc_host_open(int port, enum sm_signal sig) { int ret; ret = (*tc_host_open_sig[sig])(port); - return SUPER(ret, sig, tc_vbus_cc_iso); + return SM_SUPER(ret, sig, tc_vbus_cc_iso); } -static unsigned int tc_host_open_entry(int port) +static int tc_host_open_entry(int port) { /* Remove the terminations from Host CC */ vpd_host_set_pull(TYPEC_CC_OPEN, 0); @@ -328,31 +315,21 @@ static unsigned int tc_host_open_entry(int port) return 0; } -static unsigned int tc_host_open_run(int port) -{ - return RUN_SUPER; -} - /** * Super State VBUS_CC_ISO */ -static unsigned int tc_vbus_cc_iso(int port, enum signal sig) +static int tc_vbus_cc_iso(int port, enum sm_signal sig) { int ret; ret = (*tc_vbus_cc_iso_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int tc_vbus_cc_iso_entry(int port) +static int tc_vbus_cc_iso_entry(int port) { /* Enable mcu communication and cc */ vpd_mcu_cc_en(1); return 0; } - -static unsigned int tc_vbus_cc_iso_run(int port) -{ - return 0; -} diff --git a/common/usb_tc_sm.c b/common/usbc/usbc_task.c index 68912b0c63..7ff8bbf44f 100644 --- a/common/usb_tc_sm.c +++ b/common/usbc/usbc_task.c @@ -29,6 +29,7 @@ #include "usb_prl_sm.h" #include "usb_sm.h" #include "usb_tc_sm.h" +#include "usbc_ppc.h" #include "version.h" /* Include USB Type-C State Machine Header File */ @@ -36,6 +37,8 @@ #include "usb_tc_ctvpd_sm.h" #elif defined(CONFIG_USB_TYPEC_VPD) #include "usb_tc_vpd_sm.h" +#elif defined(CONFIG_USB_TYPEC_DRP_ACC_TRYSRC) +#include "usb_tc_drp_acc_trysrc_sm.h" #else #error "A USB Type-C State Machine must be defined." #endif @@ -98,6 +101,11 @@ int tc_get_data_role(int port) return tc[port].data_role; } +void tc_set_power_role(int port, int role) +{ + tc[port].power_role = role; +} + void tc_set_timeout(int port, uint64_t timeout) { tc[port].evt_timeout = timeout; @@ -139,16 +147,125 @@ int tc_restart_tcpc(int port) void set_polarity(int port, int polarity) { tcpm_set_polarity(port, polarity); -#ifdef CONFIG_USBC_PPC_POLARITY - ppc_set_polarity(port, polarity); -#endif /* defined(CONFIG_USBC_PPC_POLARITY) */ + + if (IS_ENABLED(CONFIG_USBC_PPC_POLARITY)) + ppc_set_polarity(port, polarity); +} + +void set_usb_mux_with_current_data_role(int port) +{ +#ifdef CONFIG_USBC_SS_MUX + /* + * If the SoC is down, then we disconnect the MUX to save power since + * no one cares about the data lines. + */ +#ifdef CONFIG_POWER_COMMON + if (chipset_in_or_transitioning_to_state(CHIPSET_STATE_ANY_OFF)) { + usb_mux_set(port, TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, + tc[port].polarity); + return; + } +#endif /* CONFIG_POWER_COMMON */ + + /* + * When PD stack is disconnected, then mux should be disconnected, which + * is also what happens in the set_state disconnection code. Once the + * PD state machine progresses out of disconnect, the MUX state will + * be set correctly again. + */ + if (!pd_is_connected(port)) + usb_mux_set(port, TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, + tc[port].polarity); + /* + * If new data role isn't DFP and we only support DFP, also disconnect. + */ + else if (IS_ENABLED(CONFIG_USBC_SS_MUX_DFP_ONLY) && + tc[port].data_role != PD_ROLE_DFP) + usb_mux_set(port, TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, + tc[port].polarity); + /* + * Otherwise connect mux since we are in S3+ + */ + else + usb_mux_set(port, TYPEC_MUX_USB, USB_SWITCH_CONNECT, + tc[port].polarity); +#endif /* CONFIG_USBC_SS_MUX */ +} + +/* High-priority interrupt tasks implementations */ +#if defined(HAS_TASK_PD_INT_C0) || defined(HAS_TASK_PD_INT_C1) || \ + defined(HAS_TASK_PD_INT_C2) + +/* Used to conditionally compile code in main pd task. */ +#define HAS_DEFFERED_INTERRUPT_HANDLER + +/* Events for pd_interrupt_handler_task */ +#define PD_PROCESS_INTERRUPT (1<<0) + +static uint8_t pd_int_task_id[CONFIG_USB_PD_PORT_COUNT]; + +void schedule_deferred_pd_interrupt(const int port) +{ + task_set_event(pd_int_task_id[port], PD_PROCESS_INTERRUPT, 0); } +/* + * Main task entry point that handles PD interrupts for a single port + * + * @param p The PD port number for which to handle interrupts (pointer is + * reinterpreted as an integer directly). + */ +void pd_interrupt_handler_task(void *p) +{ + const int port = (int) p; + const int port_mask = (PD_STATUS_TCPC_ALERT_0 << port); + + ASSERT(port >= 0 && port < CONFIG_USB_PD_PORT_COUNT); + + pd_int_task_id[port] = task_get_current(); + + while (1) { + const int evt = task_wait_event(-1); + + if (evt & PD_PROCESS_INTERRUPT) { + /* + * While the interrupt signal is asserted; we have more + * work to do. This effectively makes the interrupt a + * level-interrupt instead of an edge-interrupt without + * having to enable/disable a real level-interrupt in + * multiple locations. + * + * Also, if the port is disabled do not process + * interrupts. Upon existing suspend, we schedule a + * PD_PROCESS_INTERRUPT to check if we missed anything. + */ + while ((tcpc_get_alert_status() & port_mask) && + pd_is_port_enabled(port)) + tcpc_alert(port); + } + } +} +#endif /* HAS_TASK_PD_INT_C0 || HAS_TASK_PD_INT_C1 || HAS_TASK_PD_INT_C2 */ + void pd_task(void *u) { int port = TASK_ID_TO_PD_PORT(task_get_current()); - tc_state_init(port); + tc_state_init(port, TC_DEFAULT_STATE(port)); + + if (IS_ENABLED(CONFIG_USBC_PPC)) + ppc_init(port); + + /* + * Since most boards configure the TCPC interrupt as edge + * and it is possible that the interrupt line was asserted between init + * and calling set_state, we need to process any pending interrupts now. + * Otherwise future interrupts will never fire because another edge + * never happens. Note this needs to happen after set_state() is called. + */ + if (IS_ENABLED(HAS_DEFFERED_INTERRUPT_HANDLER)) + schedule_deferred_pd_interrupt(port); + while (1) { /* wait for next event/packet or timeout expiration */ @@ -167,15 +284,15 @@ void pd_task(void *u) #ifdef CONFIG_USB_PE_SM /* run policy engine state machine */ - policy_engine(port, tc[port].evt, tc[port].pd_enable); -#endif /* CONFIG_USB_PE_SM */ + usbc_policy_engine(port, tc[port].evt, tc[port].pd_enable); +#endif #ifdef CONFIG_USB_PRL_SM /* run protocol state machine */ - protocol_layer(port, tc[port].evt, tc[port].pd_enable); -#endif /* CONFIG_USB_PRL_SM */ + usbc_protocol_layer(port, tc[port].evt, tc[port].pd_enable); +#endif - /* run state machine */ - exe_state(port, TC_OBJ(port), RUN_SIG); + /* run typec state machine */ + sm_run_state_machine(port, TC_OBJ(port), SM_RUN_SIG); } } diff --git a/include/config.h b/include/config.h index e147b9bfb5..14a7344f7a 100644 --- a/include/config.h +++ b/include/config.h @@ -3786,6 +3786,9 @@ /* Type-C Charge Through VCONN Powered Device */ #undef CONFIG_USB_TYPEC_CTVPD +/* Type-C DRP with Accessory and Try.SRC */ +#undef CONFIG_USB_TYPEC_DRP_ACC_TRYSRC + /* USB Product ID. */ #undef CONFIG_USB_PID diff --git a/include/usb_pd.h b/include/usb_pd.h index 5cd7eb8851..3f693e40f0 100644 --- a/include/usb_pd.h +++ b/include/usb_pd.h @@ -199,6 +199,8 @@ enum pd_rx_errors { #define PD_T_CHUNK_SENDER_RESPONSE (25*MSEC) /* 25ms */ #define PD_T_CHUNK_SENDER_REQUEST (25*MSEC) /* 25ms */ #define PD_T_SWAP_SOURCE_START (25*MSEC) /* Min of 20ms */ +#define PD_T_RP_VALUE_CHANGE (20*MSEC) /* 20ms */ +#define PD_T_SRC_DISCONNECT (15*MSEC) /* 15ms */ /* number of edges and time window to detect CC line is not idle */ #define PD_RX_TRANSITION_COUNT 3 diff --git a/include/usb_pe_sm.h b/include/usb_pe_sm.h index ae925c270e..c536389d36 100644 --- a/include/usb_pe_sm.h +++ b/include/usb_pe_sm.h @@ -37,7 +37,7 @@ void pe_init(int port); * @param evt system event, ie: PD_EVENT_RX * @param en 0 to disable the machine, 1 to enable the machine */ -void policy_engine(int port, int evt, int en); +void usbc_policy_engine(int port, int evt, int en); /** * Informs the Policy Engine that a message was successfully sent diff --git a/include/usb_prl_sm.h b/include/usb_prl_sm.h index c02725baad..28f3a44078 100644 --- a/include/usb_prl_sm.h +++ b/include/usb_prl_sm.h @@ -130,7 +130,7 @@ enum sm_local_state prl_get_local_state(int port); * @param evt system event, ie: PD_EVENT_RX * @param en 0 to disable the machine, 1 to enable the machine */ -void protocol_layer(int port, int evt, int en); +void usbc_protocol_layer(int port, int evt, int en); /** * Set the PD revision diff --git a/include/usb_sm.h b/include/usb_sm.h index 1fd6688a1f..e57cd0b873 100644 --- a/include/usb_sm.h +++ b/include/usb_sm.h @@ -8,30 +8,41 @@ #ifndef __CROS_EC_USB_SM_H #define __CROS_EC_USB_SM_H -#define DECLARE_SM_FUNC_(prefix, name, exit) \ - DECLARE_SM_FUNC_##exit(prefix, name) -#define DECLARE_SM_FUNC_WITH_EXIT(prefix, name) static unsigned int \ +#define DECLARE_SM_FUNC_(prefix, name, run, exit) \ + DECLARE_SM_FUNC_##run(prefix, name); \ + DECLARE_SM_FUNC_##exit(prefix, name) + +#define DECLARE_SM_FUNC_WITH_RUN(prefix, name) static int \ + prefix##_##name##_run(int port) + +#define DECLARE_SM_FUNC_WITH_EXIT(prefix, name) static int \ prefix##_##name##_exit(int port) -#define DECLARE_SM_FUNC_NOOP_EXIT(prefix, name) -#define DECLARE_SM_SIG_(prefix, name, exit) DECLARE_SM_##exit(prefix, name) +#define DECLARE_SM_FUNC_NOOP(prefix, name) + +#define DECLARE_SM_SIG_RUN(prefix, name, run) DECLARE_SM_##run(prefix, name) +#define DECLARE_SM_WITH_RUN(prefix, name) prefix##_##name##_run + +#define DECLARE_SM_SIG_EXIT(prefix, name, exit) DECLARE_SM_##exit(prefix, name) #define DECLARE_SM_WITH_EXIT(prefix, name) prefix##_##name##_exit -#define DECLARE_SM_NOOP_EXIT(prefix, name) do_nothing_exit +#define DECLARE_SM_NOOP(prefix, name) sm_do_nothing /* * Helper macro for the declaration of states. * * @param prefix - prefix of state function name * @param name - name of state + * @param run - if WITH_RUN, generates a run state function name + * if NOOP, generates a do nothing function name * @param exit - if WITH_EXIT, generates an exit state function name - * if NOOP_EXIT, generates do_nothing_exit function name + * if NOOP, generates a do nothing function name * * EXAMPLE: * - * DECLARE_STATE(tc, test, WITH_EXIT); generates the following: + * DECLARE_STATE(tc, test, WITH_RUN, WITH_EXIT); generates the following: * - * static unsigned int tc_test(int port, enum signal sig); + * static unsigned int tc_test(int port, enum sm_signal sig); * static unsigned int tc_test_entry(int port); * static unsigned int tc_test_run(int port); * static unsigned int tc_test_exit(int port); @@ -39,35 +50,33 @@ * tc_test_entry, * tc_test_run, * tc_test_exit, - * get_super_state }; + * sm_get_super_state }; * - * DECLARE_STATE(tc, test, NOOP_EXIT); generates the following: + * DECLARE_STATE(tc, test, NOOP, NOOP); generates the following: * - * static unsigned int tc_test(int port, enum signal sig); + * static unsigned int tc_test(int port, enum sm_signal sig); * static unsigned int tc_test_entry(int port); - * static unsigned int tc_test_run(int port); * static const state_sig tc_test_sig[] = { * tc_test_entry, - * tc_test_run, - * do_nothing_exit, - * get_super_state }; + * sm_do_nothing, + * sm_do_nothing, + * sm_get_super_state }; */ -#define DECLARE_STATE(prefix, name, exit) \ -static unsigned int prefix##_##name(int port, enum signal sig); \ -static unsigned int prefix##_##name##_entry(int port); \ -static unsigned int prefix##_##name##_run(int port); \ -DECLARE_SM_FUNC_(prefix, name, exit); \ +#define DECLARE_STATE(prefix, name, run, exit) \ +static int prefix##_##name(int port, enum sm_signal sig); \ +static int prefix##_##name##_entry(int port); \ +DECLARE_SM_FUNC_(prefix, name, run, exit); \ static const state_sig prefix##_##name##_sig[] = { \ prefix##_##name##_entry, \ -prefix##_##name##_run, \ -DECLARE_SM_SIG_(prefix, name, exit), \ -get_super_state \ +DECLARE_SM_SIG_RUN(prefix, name, run), \ +DECLARE_SM_SIG_EXIT(prefix, name, exit), \ +sm_get_super_state \ } #define SM_OBJ(smo) ((struct sm_obj *)&smo) -#define SUPER(r, sig, s) ((((r) == 0) || ((sig) == ENTRY_SIG) || \ - ((sig) == EXIT_SIG)) ? 0 : ((uintptr_t)(s))) -#define RUN_SUPER 1 +#define SM_SUPER(r, sig, s) ((((r) == 0) || ((sig) == SM_ENTRY_SIG) || \ + ((sig) == SM_EXIT_SIG)) ? 0 : ((uintptr_t)(s))) +#define SM_RUN_SUPER 1 /* Local state machine states */ enum sm_local_state { @@ -77,15 +86,15 @@ enum sm_local_state { }; /* State Machine signals */ -enum signal { - ENTRY_SIG = 0, - RUN_SIG, - EXIT_SIG, - SUPER_SIG, +enum sm_signal { + SM_ENTRY_SIG = 0, + SM_RUN_SIG, + SM_EXIT_SIG, + SM_SUPER_SIG, }; -typedef unsigned int (*state_sig)(int port); -typedef unsigned int (*sm_state)(int port, enum signal sig); +typedef int (*state_sig)(int port); +typedef int (*sm_state)(int port, enum sm_signal sig); struct sm_obj { sm_state task_state; @@ -99,7 +108,7 @@ struct sm_obj { * @param obj State machine object * @param target Initial state of state machine */ -void init_state(int port, struct sm_obj *obj, sm_state target); +void sm_init_state(int port, struct sm_obj *obj, sm_state target); /** * Changes a state machines state @@ -109,24 +118,25 @@ void init_state(int port, struct sm_obj *obj, sm_state target); * @param target State to transition to * @return 0 */ -int set_state(int port, struct sm_obj *obj, sm_state target); +int sm_set_state(int port, struct sm_obj *obj, sm_state target); /** - * Executes a state machine + * Runs a state machine * * @param port USB-C port number * @param obj State machine object * @param sig State machine signal */ -void exe_state(int port, struct sm_obj *obj, enum signal sig); +void sm_run_state_machine(int port, struct sm_obj *obj, enum sm_signal sig); /** - * Substitute this function for states that do not implement an exit state. + * Substitute this function for states that do not implement a + * run or exit state. * * @param port USB-C port number * @return 0 */ -unsigned int do_nothing_exit(int port); +int sm_do_nothing(int port); /** * Called by the state machine framework to execute a states super state. @@ -134,6 +144,6 @@ unsigned int do_nothing_exit(int port); * @param port USB-C port number * @return RUN_SUPER */ -unsigned int get_super_state(int port); +int sm_get_super_state(int port); #endif /* __CROS_EC_USB_SM_H */ diff --git a/include/usb_tc_ctvpd_sm.h b/include/usb_tc_ctvpd_sm.h index 72ce16f317..ab964761f9 100644 --- a/include/usb_tc_ctvpd_sm.h +++ b/include/usb_tc_ctvpd_sm.h @@ -9,6 +9,9 @@ #include "usb_sm.h" #include "usb_tc_sm.h" +/* Port default state at startup */ +#define TC_DEFAULT_STATE(port) TC_UNATTACHED_SNK + /** * This is the Type-C Port object that contains information needed to * implement a Charge Through VCONN Powered Device. diff --git a/include/usb_tc_drp_acc_trysrc_sm.h b/include/usb_tc_drp_acc_trysrc_sm.h new file mode 100644 index 0000000000..03327c0e3a --- /dev/null +++ b/include/usb_tc_drp_acc_trysrc_sm.h @@ -0,0 +1,85 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_USB_TC_DRP_ACC_TRYSRC_H +#define __CROS_EC_USB_TC_DRP_ACC_TRYSRC_H + +#include "usb_sm.h" +#include "usb_tc_sm.h" + +/* Port default state at startup */ +#define TC_DEFAULT_STATE(port) TC_UNATTACHED_SNK + +/** + * This is the Type-C Port object that contains information needed to + * implement a USB Type-C DRP with Accessory and Try.SRC module + * See Figure 4-16 in Release 1.4 of USB Type-C Spec. + */ +struct type_c { + /* + * struct sm_obj must be first. This is the state machine + * object that keeps track of the current and last state + * of the state machine. + */ + struct sm_obj obj; + /* state id */ + enum typec_state_id state_id; + /* current port power role (SOURCE or SINK) */ + uint8_t power_role; + /* current port data role (DFP or UFP) */ + uint8_t data_role; + /* Port polarity : 0 => CC1 is CC line, 1 => CC2 is CC line */ + uint8_t polarity; + /* port flags, see TC_FLAGS_* */ + uint32_t flags; + /* event timeout */ + uint64_t evt_timeout; + /* state machine event */ + int evt; + /* Time a port shall wait before it can determine it is attached */ + uint64_t cc_debounce; + /* + * Time a Sink port shall wait before it can determine it is detached + * due to the potential for USB PD signaling on CC as described in + * the state definitions. + */ + uint64_t pd_debounce; +#ifdef CONFIG_USB_PD_TRY_SRC + /* + * Time a port shall wait before it can determine it is + * re-attached during the try-wait process. + */ + uint64_t try_wait_debounce; +#endif + /* Voltage on CC pin */ + enum tcpc_cc_voltage_status cc_voltage; + /* The cc state */ + enum pd_cc_states cc_state; + /* Role toggle timer */ + uint64_t next_role_swap; + /* Generic timer */ + uint64_t timeout; + +#ifdef CONFIG_USB_PD_TCPC_LOW_POWER + /* Time to enter low power mode */ + uint64_t low_power_time; + /* Tasks to notify after TCPC has been reset */ + int tasks_waiting_on_reset; + /* Tasks preventing TCPC from entering low power mode */ + int tasks_preventing_lpm; +#endif + /* Type-C current */ + typec_current_t typec_curr; + /* Type-C current change */ + typec_current_t typec_curr_change; + /* Attached ChromeOS device id, RW hash, and current RO / RW image */ + uint16_t dev_id; + uint32_t dev_rw_hash[PD_RW_HASH_SIZE/4]; + enum ec_current_image current_image; +}; + +extern struct type_c tc[]; + +#endif /* __CROS_EC_USB_TC_DRP_ACC_TRYSRC_H */ diff --git a/include/usb_tc_sm.h b/include/usb_tc_sm.h index 4a2af0fa6f..fedee1c7ec 100644 --- a/include/usb_tc_sm.h +++ b/include/usb_tc_sm.h @@ -11,37 +11,37 @@ #include "usb_sm.h" enum typec_state_id { - DISABLED, - UNATTACHED_SNK, - ATTACH_WAIT_SNK, - ATTACHED_SNK, + TC_DISABLED, + TC_UNATTACHED_SNK, + TC_ATTACH_WAIT_SNK, + TC_ATTACHED_SNK, #if !defined(CONFIG_USB_TYPEC_VPD) - ERROR_RECOVERY, - UNATTACHED_SRC, - ATTACH_WAIT_SRC, - ATTACHED_SRC, + TC_ERROR_RECOVERY, + TC_UNATTACHED_SRC, + TC_ATTACH_WAIT_SRC, + TC_ATTACHED_SRC, #endif #if !defined(CONFIG_USB_TYPEC_CTVPD) && !defined(CONFIG_USB_TYPEC_VPD) - AUDIO_ACCESSORY, - ORIENTED_DEBUG_ACCESSORY_SRC, - UNORIENTED_DEBUG_ACCESSORY_SRC, - DEBUG_ACCESSORY_SNK, - TRY_SRC, - TRY_WAIT_SNK, - CTUNATTACHED_SNK, - CTATTACHED_SNK, + TC_AUDIO_ACCESSORY, + TC_ORIENTED_DEBUG_ACCESSORY_SRC, + TC_UNORIENTED_DEBUG_ACCESSORY_SRC, + TC_DEBUG_ACCESSORY_SNK, + TC_TRY_SRC, + TC_TRY_WAIT_SNK, + TC_CTUNATTACHED_SNK, + TC_CTATTACHED_SNK, #endif #if defined(CONFIG_USB_TYPEC_CTVPD) - CTTRY_SNK, - CTATTACHED_UNSUPPORTED, - CTATTACH_WAIT_UNSUPPORTED, - CTUNATTACHED_UNSUPPORTED, - CTUNATTACHED_VPD, - CTATTACH_WAIT_VPD, - CTATTACHED_VPD, - CTDISABLED_VPD, - TRY_SNK, - TRY_WAIT_SRC, + TC_CTTRY_SNK, + TC_CTATTACHED_UNSUPPORTED, + TC_CTATTACH_WAIT_UNSUPPORTED, + TC_CTUNATTACHED_UNSUPPORTED, + TC_CTUNATTACHED_VPD, + TC_CTATTACH_WAIT_VPD, + TC_CTATTACHED_VPD, + TC_CTDISABLED_VPD, + TC_TRY_SNK, + TC_TRY_WAIT_SRC, #endif /* Number of states. Not an actual state. */ TC_STATE_COUNT, @@ -99,6 +99,24 @@ int tc_get_data_role(int port); int tc_get_power_role(int port); /** + * Set the power role + * + * @param port USB-C port number + * @param role power role + */ +void tc_set_power_role(int port, int role); + +/** + * Sets the USB Mux depending on current data role + * Mux is connected except when: + * 1) PD is disconnected + * 2) Current data role is UFP and we only support DFP + * + * @param port USB-C port number + */ +void set_usb_mux_with_current_data_role(int port); + +/** * Set loop timeout value * * @param port USB-C port number @@ -136,8 +154,12 @@ void set_polarity(int port, int polarity); * TypeC state machine * * @param port USB-C port number + * @param sm_state initial state of the state machine. Must be + * UNATTACHED_SNK or UNATTACHED_SRC, any other + * state id value will be interpreted as + * UNATTACHED_SNK. */ -void tc_state_init(int port); +void tc_state_init(int port, enum typec_state_id start_state); /** * Called by the state machine framework to handle events diff --git a/include/usb_tc_vpd_sm.h b/include/usb_tc_vpd_sm.h index cd564cb3ce..8beaebe5ba 100644 --- a/include/usb_tc_vpd_sm.h +++ b/include/usb_tc_vpd_sm.h @@ -9,6 +9,9 @@ #include "usb_sm.h" #include "usb_tc_sm.h" +/* Port default state at startup */ +#define TC_DEFAULT_STATE(port) TC_UNATTACHED_SNK + /** * This is the Type-C Port object that contains information needed to * implement a VCONN Powered Device. diff --git a/test/usb_prl.c b/test/usb_prl.c index ee15143986..36f007b057 100644 --- a/test/usb_prl.c +++ b/test/usb_prl.c @@ -1255,7 +1255,7 @@ int pd_task(void *u) evt = task_wait_event(-1); tcpc_run(port, evt); - protocol_layer(port, evt, pd_port[port].pd_enable); + usbc_protocol_layer(port, evt, pd_port[port].pd_enable); } return EC_SUCCESS; diff --git a/test/usb_sm_framework_h3.c b/test/usb_sm_framework_h3.c index 37266517ce..dc3a1238cd 100644 --- a/test/usb_sm_framework_h3.c +++ b/test/usb_sm_framework_h3.c @@ -124,186 +124,30 @@ struct sm_ { } sm[1]; #if defined(TEST_USB_SM_FRAMEWORK_H3) -static unsigned int sm_test_super_A1(int port, enum signal sig); -static unsigned int sm_test_super_A1_entry(int port); -static unsigned int sm_test_super_A1_run(int port); -static unsigned int sm_test_super_A1_exit(int port); - -static unsigned int sm_test_super_B1(int port, enum signal sig); -static unsigned int sm_test_super_B1_entry(int port); -static unsigned int sm_test_super_B1_run(int port); -static unsigned int sm_test_super_B1_exit(int port); +DECLARE_STATE(sm, test_super_A1, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_super_B1, WITH_RUN, WITH_EXIT); #endif #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) -static unsigned int sm_test_super_A2(int port, enum signal sig); -static unsigned int sm_test_super_A2_entry(int port); -static unsigned int sm_test_super_A2_run(int port); -static unsigned int sm_test_super_A2_exit(int port); - -static unsigned int sm_test_super_B2(int port, enum signal sig); -static unsigned int sm_test_super_B2_entry(int port); -static unsigned int sm_test_super_B2_run(int port); -static unsigned int sm_test_super_B2_exit(int port); +DECLARE_STATE(sm, test_super_A2, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_super_B2, WITH_RUN, WITH_EXIT); #endif #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ defined(TEST_USB_SM_FRAMEWORK_H1) -static unsigned int sm_test_super_A3(int port, enum signal sig); -static unsigned int sm_test_super_A3_entry(int port); -static unsigned int sm_test_super_A3_run(int port); -static unsigned int sm_test_super_A3_exit(int port); - -static unsigned int sm_test_super_B3(int port, enum signal sig); -static unsigned int sm_test_super_B3_entry(int port); -static unsigned int sm_test_super_B3_run(int port); -static unsigned int sm_test_super_B3_exit(int port); +DECLARE_STATE(sm, test_super_A3, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_super_B3, WITH_RUN, WITH_EXIT); #endif -static unsigned int sm_test_A4(int port, enum signal sig); -static unsigned int sm_test_A4_entry(int port); -static unsigned int sm_test_A4_run(int port); -static unsigned int sm_test_A4_exit(int port); - -static unsigned int sm_test_A5(int port, enum signal sig); -static unsigned int sm_test_A5_entry(int port); -static unsigned int sm_test_A5_run(int port); -static unsigned int sm_test_A5_exit(int port); - -static unsigned int sm_test_A6(int port, enum signal sig); -static unsigned int sm_test_A6_entry(int port); -static unsigned int sm_test_A6_run(int port); -static unsigned int sm_test_A6_exit(int port); - -static unsigned int sm_test_A7(int port, enum signal sig); -static unsigned int sm_test_A7_entry(int port); -static unsigned int sm_test_A7_run(int port); -static unsigned int sm_test_A7_exit(int port); - -static unsigned int sm_test_B4(int port, enum signal sig); -static unsigned int sm_test_B4_entry(int port); -static unsigned int sm_test_B4_run(int port); -static unsigned int sm_test_B4_exit(int port); - -static unsigned int sm_test_B5(int port, enum signal sig); -static unsigned int sm_test_B5_entry(int port); -static unsigned int sm_test_B5_run(int port); -static unsigned int sm_test_B5_exit(int port); - -static unsigned int sm_test_B6(int port, enum signal sig); -static unsigned int sm_test_B6_entry(int port); -static unsigned int sm_test_B6_run(int port); -static unsigned int sm_test_B6_exit(int port); - -static unsigned int sm_test_C(int port, enum signal sig); -static unsigned int sm_test_C_entry(int port); -static unsigned int sm_test_C_run(int port); -static unsigned int sm_test_C_exit(int port); +DECLARE_STATE(sm, test_A4, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_A5, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_A6, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_A7, WITH_RUN, WITH_EXIT); -#if defined(TEST_USB_SM_FRAMEWORK_H3) -static const state_sig sm_test_super_A1_sig[] = { - sm_test_super_A1_entry, - sm_test_super_A1_run, - sm_test_super_A1_exit, - get_super_state -}; - -static const state_sig sm_test_super_B1_sig[] = { - sm_test_super_B1_entry, - sm_test_super_B1_run, - sm_test_super_B1_exit, - get_super_state -}; -#endif - -#if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) -static const state_sig sm_test_super_A2_sig[] = { - sm_test_super_A2_entry, - sm_test_super_A2_run, - sm_test_super_A2_exit, - get_super_state -}; - -static const state_sig sm_test_super_B2_sig[] = { - sm_test_super_B2_entry, - sm_test_super_B2_run, - sm_test_super_B2_exit, - get_super_state -}; -#endif - -#if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ - defined(TEST_USB_SM_FRAMEWORK_H1) -static const state_sig sm_test_super_A3_sig[] = { - sm_test_super_A3_entry, - sm_test_super_A3_run, - sm_test_super_A3_exit, - get_super_state -}; - -static const state_sig sm_test_super_B3_sig[] = { - sm_test_super_B3_entry, - sm_test_super_B3_run, - sm_test_super_B3_exit, - get_super_state -}; -#endif - -static const state_sig sm_test_A4_sig[] = { - sm_test_A4_entry, - sm_test_A4_run, - sm_test_A4_exit, - get_super_state -}; - -static const state_sig sm_test_A5_sig[] = { - sm_test_A5_entry, - sm_test_A5_run, - sm_test_A5_exit, - get_super_state -}; - -static const state_sig sm_test_A6_sig[] = { - sm_test_A6_entry, - sm_test_A6_run, - sm_test_A6_exit, - get_super_state -}; - -static const state_sig sm_test_A7_sig[] = { - sm_test_A7_entry, - sm_test_A7_run, - sm_test_A7_exit, - get_super_state -}; - -static const state_sig sm_test_B4_sig[] = { - sm_test_B4_entry, - sm_test_B4_run, - sm_test_B4_exit, - get_super_state -}; - -static const state_sig sm_test_B5_sig[] = { - sm_test_B5_entry, - sm_test_B5_run, - sm_test_B5_exit, - get_super_state -}; - -static const state_sig sm_test_B6_sig[] = { - sm_test_B6_entry, - sm_test_B6_run, - sm_test_B6_exit, - get_super_state -}; - -static const state_sig sm_test_C_sig[] = { - sm_test_C_entry, - sm_test_C_run, - sm_test_C_exit, - get_super_state -}; +DECLARE_STATE(sm, test_B4, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_B5, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_B6, WITH_RUN, WITH_EXIT); +DECLARE_STATE(sm, test_C, WITH_RUN, WITH_EXIT); static void clear_seq(int port) { @@ -316,53 +160,53 @@ static void clear_seq(int port) } #if defined(TEST_USB_SM_FRAMEWORK_H3) -static unsigned int sm_test_super_A1(int port, enum signal sig) +static int sm_test_super_A1(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_A1_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int sm_test_super_A1_entry(int port) +static int sm_test_super_A1_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_A1; return 0; } -static unsigned int sm_test_super_A1_run(int port) +static int sm_test_super_A1_run(int port) { sm[port].seq[sm[port].idx++] = RUN_A1; return 0; } -static unsigned int sm_test_super_A1_exit(int port) +static int sm_test_super_A1_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A1; return 0; } -static unsigned int sm_test_super_B1(int port, enum signal sig) +static int sm_test_super_B1(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_B1_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int sm_test_super_B1_entry(int port) +static int sm_test_super_B1_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_B1; return 0; } -static unsigned int sm_test_super_B1_run(int port) +static int sm_test_super_B1_run(int port) { sm[port].seq[sm[port].idx++] = RUN_B1; return 0; } -static unsigned int sm_test_super_B1_exit(int port) +static int sm_test_super_B1_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B1; return 0; @@ -370,61 +214,61 @@ static unsigned int sm_test_super_B1_exit(int port) #endif #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) -static unsigned int sm_test_super_A2(int port, enum signal sig) +static int sm_test_super_A2(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_A2_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) - return SUPER(ret, sig, sm_test_super_A1); + return SM_SUPER(ret, sig, sm_test_super_A1); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_super_A2_entry(int port) +static int sm_test_super_A2_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_A2; return 0; } -static unsigned int sm_test_super_A2_run(int port) +static int sm_test_super_A2_run(int port) { sm[port].seq[sm[port].idx++] = RUN_A2; - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_super_A2_exit(int port) +static int sm_test_super_A2_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A2; return 0; } -static unsigned int sm_test_super_B2(int port, enum signal sig) +static int sm_test_super_B2(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_B2_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) - return SUPER(ret, sig, sm_test_super_B1); + return SM_SUPER(ret, sig, sm_test_super_B1); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_super_B2_entry(int port) +static int sm_test_super_B2_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_B2; return 0; } -static unsigned int sm_test_super_B2_run(int port) +static int sm_test_super_B2_run(int port) { sm[port].seq[sm[port].idx++] = RUN_B2; - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_super_B2_exit(int port) +static int sm_test_super_B2_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B2; return 0; @@ -433,69 +277,69 @@ static unsigned int sm_test_super_B2_exit(int port) #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ defined(TEST_USB_SM_FRAMEWORK_H1) -static unsigned int sm_test_super_A3(int port, enum signal sig) +static int sm_test_super_A3(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_A3_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return SUPER(ret, sig, sm_test_super_A2); + return SM_SUPER(ret, sig, sm_test_super_A2); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_super_A3_entry(int port) +static int sm_test_super_A3_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_A3; return 0; } -static unsigned int sm_test_super_A3_run(int port) +static int sm_test_super_A3_run(int port) { sm[port].seq[sm[port].idx++] = RUN_A3; #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return RUN_SUPER; + return SM_RUN_SUPER; #else return 0; #endif } -static unsigned int sm_test_super_A3_exit(int port) +static int sm_test_super_A3_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A3; return 0; } -static unsigned int sm_test_super_B3(int port, enum signal sig) +static int sm_test_super_B3(int port, enum sm_signal sig) { int ret; ret = (*sm_test_super_B3_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return SUPER(ret, sig, sm_test_super_B2); + return SM_SUPER(ret, sig, sm_test_super_B2); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_super_B3_entry(int port) +static int sm_test_super_B3_entry(int port) { sm[port].seq[sm[port].idx++] = ENTER_B3; return 0; } -static unsigned int sm_test_super_B3_run(int port) +static int sm_test_super_B3_run(int port) { sm[port].seq[sm[port].idx++] = RUN_B3; #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return RUN_SUPER; + return SM_RUN_SUPER; #else return 0; #endif } -static unsigned int sm_test_super_B3_exit(int port) +static int sm_test_super_B3_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B3; return 0; @@ -503,303 +347,303 @@ static unsigned int sm_test_super_B3_exit(int port) #endif -static unsigned int sm_test_A4(int port, enum signal sig) +static int sm_test_A4(int port, enum sm_signal sig) { int ret; ret = (*sm_test_A4_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ defined(TEST_USB_SM_FRAMEWORK_H1) - return SUPER(ret, sig, sm_test_super_A3); + return SM_SUPER(ret, sig, sm_test_super_A3); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_A4_entry(int port) +static int sm_test_A4_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_A4; return 0; } -static unsigned int sm_test_A4_run(int port) +static int sm_test_A4_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_A4; } else { - set_state(port, TSM_OBJ(port), sm_test_B4); + sm_set_state(port, TSM_OBJ(port), sm_test_B4); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_A4_exit(int port) +static int sm_test_A4_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A4; return 0; } -static unsigned int sm_test_A5(int port, enum signal sig) +static int sm_test_A5(int port, enum sm_signal sig) { int ret; ret = (*sm_test_A5_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ defined(TEST_USB_SM_FRAMEWORK_H1) - return SUPER(ret, sig, sm_test_super_A3); + return SM_SUPER(ret, sig, sm_test_super_A3); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_A5_entry(int port) +static int sm_test_A5_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_A5; return 0; } -static unsigned int sm_test_A5_run(int port) +static int sm_test_A5_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_A5; } else { - set_state(port, TSM_OBJ(port), sm_test_A4); + sm_set_state(port, TSM_OBJ(port), sm_test_A4); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_A5_exit(int port) +static int sm_test_A5_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A5; return 0; } -static unsigned int sm_test_A6(int port, enum signal sig) +static int sm_test_A6(int port, enum sm_signal sig) { int ret; ret = (*sm_test_A6_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return SUPER(ret, sig, sm_test_super_A2); + return SM_SUPER(ret, sig, sm_test_super_A2); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_A6_entry(int port) +static int sm_test_A6_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_A6; return 0; } -static unsigned int sm_test_A6_run(int port) +static int sm_test_A6_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_A6; } else { - set_state(port, TSM_OBJ(port), sm_test_A5); + sm_set_state(port, TSM_OBJ(port), sm_test_A5); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_A6_exit(int port) +static int sm_test_A6_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A6; return 0; } -static unsigned int sm_test_A7(int port, enum signal sig) +static int sm_test_A7(int port, enum sm_signal sig) { int ret; ret = (*sm_test_A7_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) - return SUPER(ret, sig, sm_test_super_A1); + return SM_SUPER(ret, sig, sm_test_super_A1); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_A7_entry(int port) +static int sm_test_A7_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_A7; return 0; } -static unsigned int sm_test_A7_run(int port) +static int sm_test_A7_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_A7; } else { - set_state(port, TSM_OBJ(port), sm_test_A6); + sm_set_state(port, TSM_OBJ(port), sm_test_A6); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_A7_exit(int port) +static int sm_test_A7_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_A7; return 0; } -static unsigned int sm_test_B4(int port, enum signal sig) +static int sm_test_B4(int port, enum sm_signal sig) { int ret; ret = (*sm_test_B4_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) || \ defined(TEST_USB_SM_FRAMEWORK_H1) - return SUPER(ret, sig, sm_test_super_B3); + return SM_SUPER(ret, sig, sm_test_super_B3); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_B4_entry(int port) +static int sm_test_B4_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_B4; return 0; } -static unsigned int sm_test_B4_run(int port) +static int sm_test_B4_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].seq[sm[port].idx++] = RUN_B4; sm[port].sv_tmp = 1; } else { - set_state(port, TSM_OBJ(port), sm_test_B5); + sm_set_state(port, TSM_OBJ(port), sm_test_B5); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_B4_exit(int port) +static int sm_test_B4_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B4; return 0; } -static unsigned int sm_test_B5(int port, enum signal sig) +static int sm_test_B5(int port, enum sm_signal sig) { int ret; ret = (*sm_test_B5_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) || defined(TEST_USB_SM_FRAMEWORK_H2) - return SUPER(ret, sig, sm_test_super_B2); + return SM_SUPER(ret, sig, sm_test_super_B2); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_B5_entry(int port) +static int sm_test_B5_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_B5; return 0; } -static unsigned int sm_test_B5_run(int port) +static int sm_test_B5_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_B5; } else { - set_state(port, TSM_OBJ(port), sm_test_B6); + sm_set_state(port, TSM_OBJ(port), sm_test_B6); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_B5_exit(int port) +static int sm_test_B5_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B5; return 0; } -static unsigned int sm_test_B6(int port, enum signal sig) +static int sm_test_B6(int port, enum sm_signal sig) { int ret; ret = (*sm_test_B6_sig[sig])(port); #if defined(TEST_USB_SM_FRAMEWORK_H3) - return SUPER(ret, sig, sm_test_super_B1); + return SM_SUPER(ret, sig, sm_test_super_B1); #else - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); #endif } -static unsigned int sm_test_B6_entry(int port) +static int sm_test_B6_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_B6; return 0; } -static unsigned int sm_test_B6_run(int port) +static int sm_test_B6_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].sv_tmp = 1; sm[port].seq[sm[port].idx++] = RUN_B6; } else { - set_state(port, TSM_OBJ(port), sm_test_C); + sm_set_state(port, TSM_OBJ(port), sm_test_C); return 0; } - return RUN_SUPER; + return SM_RUN_SUPER; } -static unsigned int sm_test_B6_exit(int port) +static int sm_test_B6_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_B6; return 0; } -static unsigned int sm_test_C(int port, enum signal sig) +static int sm_test_C(int port, enum sm_signal sig) { int ret; ret = (*sm_test_C_sig[sig])(port); - return SUPER(ret, sig, 0); + return SM_SUPER(ret, sig, 0); } -static unsigned int sm_test_C_entry(int port) +static int sm_test_C_entry(int port) { sm[port].sv_tmp = 0; sm[port].seq[sm[port].idx++] = ENTER_C; return 0; } -static unsigned int sm_test_C_run(int port) +static int sm_test_C_run(int port) { if (sm[port].sv_tmp == 0) { sm[port].seq[sm[port].idx++] = RUN_C; sm[port].sv_tmp = 1; } else { - set_state(port, TSM_OBJ(port), sm_test_A7); + sm_set_state(port, TSM_OBJ(port), sm_test_A7); } return 0; } -static unsigned int sm_test_C_exit(int port) +static int sm_test_C_exit(int port) { sm[port].seq[sm[port].idx++] = EXIT_C; return 0; @@ -812,7 +656,7 @@ static int test_hierarchy_0(void) int i; clear_seq(port); - init_state(port, TSM_OBJ(port), sm_test_A4); + sm_init_state(port, TSM_OBJ(port), sm_test_A4); for (i = 0; i < 17; i++) { task_wake(TASK_ID_TEST); @@ -893,7 +737,7 @@ static int test_hierarchy_1(void) int i; clear_seq(port); - init_state(port, TSM_OBJ(port), sm_test_A4); + sm_init_state(port, TSM_OBJ(port), sm_test_A4); for (i = 0; i < 17; i++) { task_wake(TASK_ID_TEST); @@ -983,7 +827,7 @@ static int test_hierarchy_2(void) int i; clear_seq(port); - init_state(port, TSM_OBJ(port), sm_test_A4); + sm_init_state(port, TSM_OBJ(port), sm_test_A4); for (i = 0; i < 17; i++) { task_wake(TASK_ID_TEST); @@ -1082,7 +926,7 @@ static int test_hierarchy_3(void) int i; clear_seq(port); - init_state(port, TSM_OBJ(port), sm_test_A4); + sm_init_state(port, TSM_OBJ(port), sm_test_A4); for (i = 0; i < 17; i++) { task_wake(TASK_ID_TEST); @@ -1190,7 +1034,7 @@ int test_task(void *u) /* wait for next event/packet or timeout expiration */ task_wait_event(-1); /* run state machine */ - exe_state(port, TSM_OBJ(port), RUN_SIG); + sm_run_state_machine(port, TSM_OBJ(port), SM_RUN_SIG); } return EC_SUCCESS; diff --git a/test/usb_typec_ctvpd.c b/test/usb_typec_ctvpd.c index 19fbfa1b8a..b2a100ad25 100644 --- a/test/usb_typec_ctvpd.c +++ b/test/usb_typec_ctvpd.c @@ -330,7 +330,7 @@ static int test_vpd_host_src_detection(void) * Host is configured properly and start state is UNATTACHED_SNK */ TEST_ASSERT(check_host_ra_rd()); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); /* * TEST: @@ -342,7 +342,7 @@ static int test_vpd_host_src_detection(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); /* * TEST: @@ -373,7 +373,7 @@ static int test_vpd_host_src_detection(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); return EC_SUCCESS; } @@ -394,7 +394,7 @@ static int test_vpd_host_src_detection_vbus(void) */ TEST_ASSERT(check_host_ra_rd()); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); /* * TEST: @@ -406,7 +406,7 @@ static int test_vpd_host_src_detection_vbus(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); /* * TEST: @@ -418,7 +418,7 @@ static int test_vpd_host_src_detection_vbus(void) wait_for_state_change(port, PD_T_CC_DEBOUNCE + 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); /* * TEST: @@ -429,7 +429,7 @@ static int test_vpd_host_src_detection_vbus(void) wait_for_state_change(port, 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); return EC_SUCCESS; } @@ -450,7 +450,7 @@ static int test_vpd_host_src_detection_vconn(void) */ TEST_ASSERT(check_host_ra_rd()); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); /* * TEST: @@ -462,7 +462,7 @@ static int test_vpd_host_src_detection_vconn(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); /* * TEST: @@ -474,7 +474,7 @@ static int test_vpd_host_src_detection_vconn(void) wait_for_state_change(port, PD_T_CC_DEBOUNCE + 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); /* VCONN was detected. Make sure RA is removed */ task_wake(PD_PORT_TO_TASK_ID(port)); @@ -490,7 +490,7 @@ static int test_vpd_host_src_detection_vconn(void) wait_for_state_change(port, 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); host_disconnect_source(); @@ -540,7 +540,7 @@ static int test_vpd_host_src_detection_message_reception(void) */ TEST_ASSERT(check_host_ra_rd()); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); /* * Transition to ATTACHED_SNK @@ -550,11 +550,11 @@ static int test_vpd_host_src_detection_message_reception(void) wait_for_state_change(port, 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 20 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); /* Run state machines to enable rx monitoring */ task_wake(PD_PORT_TO_TASK_ID(port)); @@ -606,7 +606,7 @@ static int test_vpd_host_src_detection_message_reception(void) wait_for_state_change(port, 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); host_disconnect_source(); @@ -634,7 +634,7 @@ static int test_ctvpd_behavior_case1(void) */ /* 1. DRP and CTVPD are both in the unattached state */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); /* * a. DRP alternates between Unattached.SRC and Unattached.SNK @@ -670,10 +670,10 @@ static int test_ctvpd_behavior_case1(void) * Charge-Through port’s CC1 and CC2 pins */ wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); TEST_ASSERT(check_ct_ccs_hz()); /* @@ -702,7 +702,7 @@ static int test_ctvpd_behavior_case1(void) * d. CTVPD apply Rd on its Charge-Through port’s CC1 and CC2 pins */ wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); /* * 6. While the CTVPD in CTUnattached.VPD state and the DRP in @@ -723,32 +723,32 @@ static int test_ctvpd_behavior_case1(void) TEST_ASSERT(ct_connect_source(CC2, VBUS_0)); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACH_WAIT_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACH_WAIT_VPD); /* Remove Power Source */ TEST_ASSERT(ct_disconnect_source()); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); /* Attach Sink */ TEST_ASSERT(ct_connect_sink(CC1, SRC_CON_DEF)); wait_for_state_change(port, PD_T_DRP_SNK); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_UNSUPPORTED); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACH_WAIT_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACH_WAIT_UNSUPPORTED); /* Remove VCONN (Host detach) */ mock_set_vconn(VCONN_0); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); return EC_SUCCESS; } @@ -780,25 +780,25 @@ static int test_ctvpd_behavior_case2(void) * pins and Rp termination advertising 3.0 A on the Host-side * port’s CC pin */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); host_connect_source(VBUS_5); mock_set_vconn(VCONN_3); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); /* Remove Host CC */ mock_set_host_cc_source_voltage(0); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_rp3a0()); @@ -836,11 +836,11 @@ static int test_ctvpd_behavior_case2(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACH_WAIT_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACH_WAIT_VPD); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACHED_VPD); TEST_ASSERT(moch_get_ct_cl_sel() == CT_CC2); TEST_ASSERT(check_host_cc_open()); TEST_ASSERT(check_ct_ccs_hz()); @@ -870,7 +870,7 @@ static int test_ctvpd_behavior_case2(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTDISABLED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTDISABLED_VPD); return EC_SUCCESS; } @@ -899,7 +899,7 @@ static int test_ctvpd_behavior_case3(void) * a. CTVPD has applied Rd on the Charge-Through port’s CC1 and CC2 * pins and Rd on the Host-side port’s CC pin */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_ra_rd()); @@ -923,7 +923,7 @@ static int test_ctvpd_behavior_case3(void) * between Unattached.SRC and Unattached.SNK */ wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SRC); /* * 4. While the CTVPD alternates between Unattached.SRC and @@ -942,14 +942,14 @@ static int test_ctvpd_behavior_case3(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SRC); /* Remove VBUS */ TEST_ASSERT(ct_disconnect_source()); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); return EC_SUCCESS; } @@ -1004,7 +1004,7 @@ static int test_ctvpd_behavior_case4(void) * b. CTVPD has applied Rd on its Charge-Through port’s CC1 and CC2 * pins and Rd on the Host-side port’s CC pin */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_ra_rd()); @@ -1038,11 +1038,11 @@ static int test_ctvpd_behavior_case4(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); TEST_ASSERT(check_ct_ccs_hz()); /* @@ -1109,7 +1109,7 @@ static int test_ctvpd_behavior_case4(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_rp3a0()); @@ -1119,16 +1119,16 @@ static int test_ctvpd_behavior_case4(void) */ wait_for_state_change(port, PD_T_DRP_SRC + 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_UNSUPPORTED); wait_for_state_change(port, PD_T_DRP_SRC + 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); TEST_ASSERT(ct_connect_source(CC2, VBUS_5)); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACH_WAIT_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACH_WAIT_VPD); return EC_SUCCESS; } @@ -1159,7 +1159,7 @@ static int test_ctvpd_behavior_case5(void) * b. CTVPD apply Rd on the Charge-Through port’s CC1 and CC2 pins * and Rd on the Host-side port’s CC pin */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_ra_rd()); @@ -1184,7 +1184,7 @@ static int test_ctvpd_behavior_case5(void) */ wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SRC); /* Connect Host With Dead Battery */ host_connect_sink(SRC_CON_DEF); @@ -1204,11 +1204,11 @@ static int test_ctvpd_behavior_case5(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SRC); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 10 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == TRY_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_TRY_SNK); TEST_ASSERT(check_host_ra_rd()); /* 5. DRP in dead battery condition remains in Unattached.SNK */ @@ -1230,12 +1230,12 @@ static int test_ctvpd_behavior_case5(void) wait_for_state_change(port, PD_T_TRY_CC_DEBOUNCE + PD_T_DRP_TRY + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == TRY_WAIT_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_TRY_WAIT_SRC); TEST_ASSERT(check_host_rpusb()); wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SRC); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SRC); TEST_ASSERT(mock_get_vbus_pass_en()); /* @@ -1268,7 +1268,7 @@ static int test_ctvpd_behavior_case5(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); return EC_SUCCESS; } @@ -1299,7 +1299,7 @@ static int test_ctvpd_behavior_case6(void) * b. CTVPD has applied Rd on its Charge-Through port’s CC1 and CC2 * pins and Rd on the Host-side port’s CC pin */ - TEST_ASSERT(get_typec_state_id(port) == UNATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_UNATTACHED_SNK); TEST_ASSERT(check_ct_ccs_rd()); TEST_ASSERT(check_host_ra_rd()); @@ -1330,11 +1330,11 @@ static int test_ctvpd_behavior_case6(void) */ wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACH_WAIT_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACH_WAIT_SNK); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == ATTACHED_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_ATTACHED_SNK); TEST_ASSERT(check_ct_ccs_hz()); /* @@ -1367,7 +1367,7 @@ static int test_ctvpd_behavior_case6(void) */ wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); TEST_ASSERT(check_host_rp3a0()); TEST_ASSERT(mock_get_vbus_pass_en() == 0); TEST_ASSERT(check_ct_ccs_rd()); @@ -1382,15 +1382,15 @@ static int test_ctvpd_behavior_case6(void) */ wait_for_state_change(port, PD_T_DRP_SNK + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_UNSUPPORTED); wait_for_state_change(port, PD_T_DRP_SNK + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); wait_for_state_change(port, PD_T_DRP_SNK + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_UNSUPPORTED); /* * 7. CTVPD transitions from CTUnattached.Unsupported to CTTry.SNK @@ -1409,11 +1409,11 @@ static int test_ctvpd_behavior_case6(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACH_WAIT_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACH_WAIT_UNSUPPORTED); wait_for_state_change(port, PD_T_CC_DEBOUNCE + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTTRY_SNK); + TEST_ASSERT(get_typec_state_id(port) == TC_CTTRY_SNK); TEST_ASSERT(check_ct_ccs_rd()); /* @@ -1426,7 +1426,7 @@ static int test_ctvpd_behavior_case6(void) wait_for_state_change(port, PD_T_DRP_TRY + PD_T_TRY_WAIT + 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTATTACHED_UNSUPPORTED); + TEST_ASSERT(get_typec_state_id(port) == TC_CTATTACHED_UNSUPPORTED); /* * 9. While the CTVPD in CTAttached.Unsupported state, the DRP in @@ -1455,7 +1455,7 @@ static int test_ctvpd_behavior_case6(void) wait_for_state_change(port, 40 * MSEC); - TEST_ASSERT(get_typec_state_id(port) == CTUNATTACHED_VPD); + TEST_ASSERT(get_typec_state_id(port) == TC_CTUNATTACHED_VPD); return EC_SUCCESS; } |