diff options
-rw-r--r-- | common/usb_pd_policy.c | 35 | ||||
-rw-r--r-- | common/usb_pd_protocol.c | 45 | ||||
-rw-r--r-- | include/usb_pd.h | 41 |
3 files changed, 78 insertions, 43 deletions
diff --git a/common/usb_pd_policy.c b/common/usb_pd_policy.c index 93ce6fccb4..25dfea72f9 100644 --- a/common/usb_pd_policy.c +++ b/common/usb_pd_policy.c @@ -173,21 +173,32 @@ bool is_transmit_msg_sop_prime(int port) (cable[port].flags & CABLE_FLAGS_SOP_PRIME_ENABLE)); } -static bool is_transmit_msg_sop_prime_prime(int port) +bool is_transmit_msg_sop_prime_prime(int port) { return (IS_ENABLED(CONFIG_USB_PD_DECODE_SOP) && (cable[port].flags & CABLE_FLAGS_SOP_PRIME_PRIME_ENABLE)); } -int cable_consume_repeat_message(int port, uint8_t msg_id) +bool consume_sop_prime_repeat_msg(int port, uint8_t msg_id) { - if (cable[port].last_cable_msg_id != msg_id) { - cable[port].last_cable_msg_id = msg_id; - return 0; + if (cable[port].last_sop_p_msg_id != msg_id) { + cable[port].last_sop_p_msg_id = msg_id; + return false; } - CPRINTF("C%d Cable repeat msg_id %d\n", port, msg_id); - return 1; + CPRINTF("C%d SOP Prime repeat msg_id %d\n", port, msg_id); + return true; +} + +bool consume_sop_prime_prime_repeat_msg(int port, uint8_t msg_id) +{ + + if (cable[port].last_sop_p_p_msg_id != msg_id) { + cable[port].last_sop_p_p_msg_id = msg_id; + return false; + } + CPRINTF("C%d SOP Prime Prime repeat msg_id %d\n", port, msg_id); + return true; } static void disable_transmit_sop_prime(int port) @@ -245,14 +256,8 @@ void reset_pd_cable(int port) { if (IS_ENABLED(CONFIG_USB_PD_DECODE_SOP)) { memset(&cable[port], 0, sizeof(cable[port])); - /* - * Invalidate the last cable messageId counter. The cable - * Message id starts from 0 to 7 and if last_cable msg_id - * is initialized to 0, it will lead to repetitive message - * id with first received packet. Hence, initialize it with - * an invalid value 0xff. - */ - cable[port].last_cable_msg_id = 0xff; + cable[port].last_sop_p_msg_id = INVALID_MSG_ID_COUNTER; + cable[port].last_sop_p_p_msg_id = INVALID_MSG_ID_COUNTER; } } diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index ea18fd05d4..a592ffaec3 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -677,12 +677,17 @@ static void pd_update_saved_port_flags(int port, uint8_t flag, uint8_t val) */ static void invalidate_last_message_id(int port) { - /* - * Message id starts from 0 to 7. If last_msg_id is initialized to 0, - * it will lead to repetitive message id with first received packet, - * so initialize it with an invalid value 0xff. - */ - pd[port].last_msg_id = 0xff; + pd[port].last_msg_id = INVALID_MSG_ID_COUNTER; +} + +static bool consume_sop_repeat_message(int port, uint8_t msg_id) +{ + if (pd[port].last_msg_id != msg_id) { + pd[port].last_msg_id = msg_id; + return false; + } + CPRINTF("C%d Repeat msg_id %d\n", port, msg_id); + return true; } /** @@ -690,32 +695,28 @@ static void invalidate_last_message_id(int port) * * @param port USB PD TCPC port number * @param msg_header Message Header containing the RX message ID - * @return 1 if the received message is a duplicate one, 0 otherwise. + * @return True if the received message is a duplicate one, False otherwise. + * + * From USB PD version 1.3 section 6.7.1, the port which communicates + * using SOP* Packets Shall maintain copies of the last MessageID for + * each type of SOP* it uses. */ -static int consume_repeat_message(int port, uint16_t msg_header) +static bool consume_repeat_message(int port, uint16_t msg_header) { uint8_t msg_id = PD_HEADER_ID(msg_header); /* If repeat message ignore, except softreset control request. */ if (PD_HEADER_TYPE(msg_header) == PD_CTRL_SOFT_RESET && PD_HEADER_CNT(msg_header) == 0) { - return 0; - /* TODO: Check for incoming SOP'' messages */ + return false; } else if (is_transmit_msg_sop_prime(port)) { - /* - * From USB PD version 1.3 section 6.7.1, the port which - * communicates using SOP* Packets Shall maintain copy - * of the last MessageID for each type of SOP* it uses. - */ - return cable_consume_repeat_message(port, msg_id); - } else if (pd[port].last_msg_id != msg_id) { - pd[port].last_msg_id = msg_id; - } else if (pd[port].last_msg_id == msg_id) { - CPRINTF("C%d Repeat msg_id %d\n", port, msg_id); - return 1; + return consume_sop_prime_repeat_msg(port, msg_id); + } else if (is_transmit_msg_sop_prime_prime(port)) { + return consume_sop_prime_prime_repeat_msg(port, msg_id); + } else { + return consume_sop_repeat_message(port, msg_id); } - return 0; } /** diff --git a/include/usb_pd.h b/include/usb_pd.h index 812dc98e3d..3c84564243 100644 --- a/include/usb_pd.h +++ b/include/usb_pd.h @@ -503,6 +503,13 @@ enum idh_ptype { #define VDO_PRODUCT(pid, bcd) (((pid) & 0xffff) << 16 | ((bcd) & 0xffff)) #define PD_PRODUCT_PID(vdo) (((vdo) >> 16) & 0xffff) +/* + * Message id starts from 0 to 7. If last_msg_id is initialized to 0, + * it will lead to repetitive message id with first received packet, + * so initialize it with an invalid value 0xff. + */ +#define INVALID_MSG_ID_COUNTER 0xff + union cable_vdo { /* Passive cable VDO */ union passive_cable_vdo_rev20 p_rev20; @@ -529,8 +536,10 @@ enum pd_rev_type { /* Cable structure for storing cable attributes */ struct pd_cable { - /* Last received cable message id counter*/ - uint8_t last_cable_msg_id; + /* Last received SOP' message id counter*/ + uint8_t last_sop_p_msg_id; + /* Last received SOP'' message id counter*/ + uint8_t last_sop_p_p_msg_id; uint8_t is_identified; /* Type of cable */ enum idh_ptype type; @@ -1688,16 +1697,28 @@ uint32_t *pd_get_mode_vdo(int port, uint16_t svid_idx); struct svdm_amode_data *pd_get_amode_data(int port, uint16_t svid); /** - * Returns 0 if previous cable messageId count is different from received + * Returns false if previous SOP' messageId count is different from received + * messageId count. + * + * @param port USB-C port number + * @param msg_id Received cable msg_id + * @return False if Received MessageId count is different from the + * previous one. + * True Otherwise + */ +bool consume_sop_prime_repeat_msg(int port, uint8_t msg_id); + +/** + * Returns false if previous SOP'' messageId count is different from received * messageId count. * * @param port USB-C port number * @param msg_id Received cable msg_id - * @return 0 if Received MessageId count is different from the + * @return False if Received MessageId count is different from the * previous one. - * 1 Otherwise + * True Otherwise */ -int cable_consume_repeat_message(int port, uint8_t msg_id); +bool consume_sop_prime_prime_repeat_msg(int port, uint8_t msg_id); /** * Returns the status of cable flag - CABLE_FLAGS_SOP_PRIME_ENABLE @@ -1708,6 +1729,14 @@ int cable_consume_repeat_message(int port, uint8_t msg_id); bool is_transmit_msg_sop_prime(int port); /** + * Returns the status of cable flag - CABLE_FLAGS_SOP_PRIME_PRIME_ENABLE + * + * @param port USB-C port number + * @return Status of CABLE_FLAGS_SOP_PRIME_PRIME_ENABLE flag + */ +bool is_transmit_msg_sop_prime_prime(int port); + +/** * Returns the type of communication (SOP/SOP'/SOP'') * * @param port USB-C port number |