Skip to content

Commit

Permalink
Lib: SMF: Update USB-C to use new SMF return code
Browse files Browse the repository at this point in the history
Update USB-C subsystem to use smf_state_result return code

Signed-off-by: Glenn Andrews <glenn.andrews.42@gmail.com>
  • Loading branch information
glenn-andrews committed Jan 12, 2025
1 parent a765e53 commit 9a824a4
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 150 deletions.
96 changes: 51 additions & 45 deletions subsys/usb/usb_c/usbc_pe_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ void pe_report_error(const struct device *dev, const enum pe_error e,
* Generate Hard Reset if Protocol Error occurred
* while in PE_Send_Soft_Reset state.
*/
if (pe_get_state(dev) == PE_SEND_SOFT_RESET ||
pe_get_state(dev) == PE_SOFT_RESET) {
if (pe_get_state(dev) == PE_SEND_SOFT_RESET || pe_get_state(dev) == PE_SOFT_RESET) {
atomic_set_bit(pe->flags, PE_FLAGS_PROTOCOL_ERROR);
return;
}
Expand Down Expand Up @@ -479,7 +478,7 @@ void pe_send_soft_reset(const struct device *dev, const enum pd_packet_type type
* @brief Send a Power Delivery Data Message
*/
void pe_send_data_msg(const struct device *dev, const enum pd_packet_type type,
const enum pd_data_msg_type msg)
const enum pd_data_msg_type msg)
{
struct usbc_port_data *data = dev->data;
struct policy_engine *pe = data->pe;
Expand All @@ -493,7 +492,7 @@ void pe_send_data_msg(const struct device *dev, const enum pd_packet_type type,
* @brief Send a Power Delivery Control Message
*/
void pe_send_ctrl_msg(const struct device *dev, const enum pd_packet_type type,
const enum pd_ctrl_msg_type msg)
const enum pd_ctrl_msg_type msg)
{
struct usbc_port_data *data = dev->data;
struct policy_engine *pe = data->pe;
Expand Down Expand Up @@ -669,9 +668,8 @@ void policy_get_snk_cap(const struct device *dev, uint32_t **pdos, int *num_pdos
/**
* @brief Send the received sink caps to the DPM
*/
void policy_set_port_partner_snk_cap(const struct device *dev,
const uint32_t *pdos,
const int num_pdos)
void policy_set_port_partner_snk_cap(const struct device *dev, const uint32_t *pdos,
const int num_pdos)
{
struct usbc_port_data *data = dev->data;

Expand All @@ -684,28 +682,26 @@ void policy_set_port_partner_snk_cap(const struct device *dev,
* @brief Check if Sink Request can be met by DPM
*/
enum usbc_snk_req_reply_t policy_check_sink_request(const struct device *dev,
const uint32_t request_msg)
const uint32_t request_msg)
{
struct usbc_port_data *data = dev->data;

/* This callback must be implemented */
__ASSERT(data->policy_cb_check_sink_request != NULL,
"Callback pointer should not be NULL");
__ASSERT(data->policy_cb_check_sink_request != NULL, "Callback pointer should not be NULL");

return data->policy_cb_check_sink_request(dev, request_msg);
}

/**
* @brief Check if the present contract is still valid
*/
bool policy_present_contract_is_valid(const struct device *dev,
const uint32_t present_contract)
bool policy_present_contract_is_valid(const struct device *dev, const uint32_t present_contract)
{
struct usbc_port_data *data = dev->data;

/* This callback must be implemented */
__ASSERT(data->policy_present_contract_is_valid != NULL,
"Callback pointer should not be NULL");
"Callback pointer should not be NULL");

return data->policy_present_contract_is_valid(dev, present_contract);
}
Expand All @@ -718,8 +714,7 @@ bool policy_is_ps_ready(const struct device *dev)
struct usbc_port_data *data = dev->data;

/* This callback must be implemented */
__ASSERT(data->policy_is_ps_ready != NULL,
"Callback pointer should not be NULL");
__ASSERT(data->policy_is_ps_ready != NULL, "Callback pointer should not be NULL");

return data->policy_is_ps_ready(dev);
}
Expand Down Expand Up @@ -771,7 +766,7 @@ static void pe_drs_evaluate_swap_entry(void *obj)
/**
* @brief PE_DRS_Evaluate_Swap Run state
*/
static void pe_drs_evaluate_swap_run(void *obj)
static enum smf_state_result pe_drs_evaluate_swap_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand All @@ -783,8 +778,8 @@ static void pe_drs_evaluate_swap_run(void *obj)
/* Only update data roles if last message sent was Accept */
if (prl_tx->msg_type == PD_CTRL_ACCEPT) {
/* Update Data Role */
pe_set_data_role(dev, (pe->data_role == TC_ROLE_UFP)
? TC_ROLE_DFP : TC_ROLE_UFP);
pe_set_data_role(dev, (pe->data_role == TC_ROLE_UFP) ? TC_ROLE_DFP
: TC_ROLE_UFP);
/* Inform Device Policy Manager of Data Role Change */
policy_notify(dev, (pe->data_role == TC_ROLE_UFP) ? DATA_ROLE_IS_UFP
: DATA_ROLE_IS_DFP);
Expand All @@ -798,6 +793,7 @@ static void pe_drs_evaluate_swap_run(void *obj)
policy_notify(dev, MSG_DISCARDED);
pe_send_soft_reset(dev, prl_rx->emsg.type);
}
return SMF_EVENT_PROPAGATE;
}

/**
Expand All @@ -818,7 +814,7 @@ static void pe_drs_send_swap_entry(void *obj)
* @brief PE_DRS_Send_Swap Run state
* NOTE: Sender Response Timer is handled in super state.
*/
static void pe_drs_send_swap_run(void *obj)
static enum smf_state_result pe_drs_send_swap_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand Down Expand Up @@ -858,21 +854,22 @@ static void pe_drs_send_swap_run(void *obj)
* a Type-C Error Recovery.
*/
usbc_request(dev, REQUEST_TC_ERROR_RECOVERY);
return;
return SMF_EVENT_PROPAGATE;
}

/* return to ready state */
pe_set_ready_state(dev);
return;
return SMF_EVENT_PROPAGATE;
} else if (atomic_test_and_clear_bit(pe->flags, PE_FLAGS_MSG_DISCARDED)) {
/*
* Inform Device Policy Manager that the message
* was discarded
*/
policy_notify(dev, MSG_DISCARDED);
pe_set_ready_state(dev);
return;
return SMF_EVENT_PROPAGATE;
}
return SMF_EVENT_PROPAGATE;
}

/**
Expand All @@ -899,7 +896,7 @@ void pe_get_sink_cap_entry(void *obj)
* @brief PE_Get_Sink_Cap Run state
* NOTE: Sender Response Timer is handled in super state.
*/
void pe_get_sink_cap_run(void *obj)
enum smf_state_result pe_get_sink_cap_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand All @@ -922,23 +919,23 @@ void pe_get_sink_cap_run(void *obj)
if (received_data_message(dev, header, PD_DATA_SINK_CAP)) {
#ifdef CONFIG_USBC_CSM_SOURCE_ONLY
uint32_t *pdos = (uint32_t *)prl_rx->emsg.data;
uint32_t num_pdos =
PD_CONVERT_BYTES_TO_PD_HEADER_COUNT(prl_rx->emsg.len);
uint32_t num_pdos = PD_CONVERT_BYTES_TO_PD_HEADER_COUNT(
prl_rx->emsg.len);

policy_set_port_partner_snk_cap(dev, pdos, num_pdos);
#endif
pe_set_ready_state(dev);
return;
return SMF_EVENT_PROPAGATE;
} else if (received_control_message(dev, header, PD_CTRL_REJECT) ||
received_control_message(dev,
header, PD_CTRL_NOT_SUPPORTED)) {
received_control_message(dev, header,
PD_CTRL_NOT_SUPPORTED)) {
pe_set_ready_state(dev);
return;
return SMF_EVENT_PROPAGATE;
}
/* Unexpected messages fall through to soft reset */
}
pe_send_soft_reset(dev, PD_PACKET_SOP);
return;
return SMF_EVENT_PROPAGATE;
}
/*
* Inform Device Policy Manager that the message
Expand All @@ -947,19 +944,21 @@ void pe_get_sink_cap_run(void *obj)
else if (atomic_test_and_clear_bit(pe->flags, PE_FLAGS_MSG_DISCARDED)) {
policy_notify(dev, MSG_DISCARDED);
pe_set_ready_state(dev);
return;
return SMF_EVENT_PROPAGATE;
}
}
return SMF_EVENT_PROPAGATE;
}

static void pe_suspend_entry(void *obj)
{
LOG_INF("PE_SUSPEND");
}

static void pe_suspend_run(void *obj)
static enum smf_state_result pe_suspend_run(void *obj)
{
/* DO NOTHING */
return SMF_EVENT_PROPAGATE;
}

/**
Expand Down Expand Up @@ -988,13 +987,13 @@ static void pe_soft_reset_entry(void *obj)
pe->submachine = PE_SOFT_RESET_RUN_SEND_ACCEPT_MSG;
}

static void pe_soft_reset_run(void *obj)
static enum smf_state_result pe_soft_reset_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;

if (!prl_is_running(dev)) {
return;
return SMF_EVENT_PROPAGATE;
}

switch (pe->submachine) {
Expand Down Expand Up @@ -1023,6 +1022,7 @@ static void pe_soft_reset_run(void *obj)
}
break;
}
return SMF_EVENT_PROPAGATE;
}

/**
Expand All @@ -1044,7 +1044,7 @@ static void pe_send_soft_reset_entry(void *obj)
/**
* @brief PE_Send_Soft_Reset Run State
*/
static void pe_send_soft_reset_run(void *obj)
static enum smf_state_result pe_send_soft_reset_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand All @@ -1053,13 +1053,13 @@ static void pe_send_soft_reset_run(void *obj)
union pd_header header;

if (prl_is_running(dev) == false) {
return;
return SMF_EVENT_PROPAGATE;
}

if (atomic_test_and_clear_bit(pe->flags, PE_FLAGS_SEND_SOFT_RESET)) {
/* Send Soft Reset message */
pe_send_ctrl_msg(dev, pe->soft_reset_sop, PD_CTRL_SOFT_RESET);
return;
return SMF_EVENT_PROPAGATE;
}

if (atomic_test_and_clear_bit(pe->flags, PE_FLAGS_MSG_DISCARDED)) {
Expand All @@ -1085,6 +1085,7 @@ static void pe_send_soft_reset_run(void *obj)
*/
pe_set_state(dev, PE_SNK_HARD_RESET);
}
return SMF_EVENT_PROPAGATE;
}

/**
Expand All @@ -1108,17 +1109,18 @@ static void pe_send_not_supported_entry(void *obj)
}
}

static void pe_send_not_supported_run(void *obj)
static enum smf_state_result pe_send_not_supported_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;

if (atomic_test_bit(pe->flags, PE_FLAGS_TX_COMPLETE) ||
atomic_test_bit(pe->flags, PE_FLAGS_MSG_DISCARDED)) {
atomic_test_bit(pe->flags, PE_FLAGS_MSG_DISCARDED)) {
atomic_clear_bit(pe->flags, PE_FLAGS_TX_COMPLETE);
atomic_clear_bit(pe->flags, PE_FLAGS_MSG_DISCARDED);
pe_set_ready_state(dev);
}
return SMF_EVENT_PROPAGATE;
}

/**
Expand All @@ -1140,7 +1142,7 @@ static void pe_chunk_received_entry(void *obj)
/**
* @brief PE_Chunk_Received Run State
*/
static void pe_chunk_received_run(void *obj)
static enum smf_state_result pe_chunk_received_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand All @@ -1153,13 +1155,14 @@ static void pe_chunk_received_run(void *obj)
if (usbc_timer_expired(&pe->pd_t_chunking_not_supported)) {
pe_set_state(dev, PE_SEND_NOT_SUPPORTED);
}
return SMF_EVENT_PROPAGATE;
}

/*
* @brief Super State for any message that requires
* Sender Response Timer functionality
*/
static void pe_sender_response_run(void *obj)
static enum smf_state_result pe_sender_response_run(void *obj)
{
struct policy_engine *pe = (struct policy_engine *)obj;
const struct device *dev = pe->dev;
Expand Down Expand Up @@ -1194,8 +1197,8 @@ static void pe_sender_response_run(void *obj)
* 2) And the NoResponseTimer times out
* 3) And the HardResetCounter > nHardResetCount.
*/
if ((atomic_test_bit(pe->flags, PE_FLAGS_HAS_BEEN_PD_CONNECTED) == false)
&& pe->hard_reset_counter > PD_N_HARD_RESET_COUNT) {
if ((atomic_test_bit(pe->flags, PE_FLAGS_HAS_BEEN_PD_CONNECTED) == false) &&
pe->hard_reset_counter > PD_N_HARD_RESET_COUNT) {
pe_set_state(dev, PE_SUSPEND);
}
break;
Expand All @@ -1206,8 +1209,8 @@ static void pe_sender_response_run(void *obj)
* 2) And the NoResponseTimer times out
* 3) And the HardResetCounter > nHardResetCount
*/
if (atomic_test_bit(pe->flags, PE_FLAGS_HAS_BEEN_PD_CONNECTED)
&& pe->hard_reset_counter > PD_N_HARD_RESET_COUNT) {
if (atomic_test_bit(pe->flags, PE_FLAGS_HAS_BEEN_PD_CONNECTED) &&
pe->hard_reset_counter > PD_N_HARD_RESET_COUNT) {
usbc_request(dev, REQUEST_TC_ERROR_RECOVERY);
}
/*
Expand Down Expand Up @@ -1240,6 +1243,7 @@ static void pe_sender_response_run(void *obj)
LOG_INF("Unhandled Sender Response Timeout State!");
}
}
return SMF_EVENT_PROPAGATE;
}

static void pe_sender_response_exit(void *obj)
Expand All @@ -1253,6 +1257,7 @@ static void pe_sender_response_exit(void *obj)
/**
* @brief Policy engine State table
*/
/* clang-format off */
static const struct smf_state pe_states[PE_STATE_COUNT] = {
/* PE Super States */
[PE_SENDER_RESPONSE_PARENT] = SMF_CREATE_STATE(
Expand Down Expand Up @@ -1453,4 +1458,5 @@ static const struct smf_state pe_states[PE_STATE_COUNT] = {
NULL,
NULL),
};
/* clang-format on */
BUILD_ASSERT(ARRAY_SIZE(pe_states) == PE_STATE_COUNT);
Loading

0 comments on commit 9a824a4

Please sign in to comment.