[PATCH 3/4] staging: typec: Clean endianness of pd_message header assignment.

Guenter Roeck linux at roeck-us.net
Sun Jun 11 19:26:28 UTC 2017


On 06/11/2017 03:54 AM, ? ? wrote:
> From: Pan Li <incarnation.p.lee at outlook.com>
> 
>      Makes header assignmen happy by shared the memory between
> __le16 and u16.
> 
> Signed-off-by: Pan Li <incarnation.p.lee at outlook.com>
> ---
>   drivers/staging/typec/pd.h    | 10 +++++++++-
>   drivers/staging/typec/tcpci.c |  2 +-
>   2 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/typec/pd.h b/drivers/staging/typec/pd.h
> index 510ef72..b85c0ab 100644
> --- a/drivers/staging/typec/pd.h
> +++ b/drivers/staging/typec/pd.h
> @@ -105,10 +105,18 @@ static inline unsigned int pd_header_msgid_le(__le16 header)
>   #define PD_MAX_PAYLOAD		7
>   
>   struct pd_message {
> -	__le16 header;
> +	union {
> +		__le16 header;
> +		u16 header_u16;
> +	};

That really doesn't look like the way to go. A variable should be either
little endian or big endian, but not both. Since this is the packet as
transmitted on the wire, it should be treated as such. There must be
a better solution.

Guenter

>   	__le32 payload[PD_MAX_PAYLOAD];
>   } __packed;
>   
> +static inline void pd_message_header_set(struct pd_message *msg, u16 header)
> +{
> +	msg->header_u16 = header;
> +}
> +
>   /* PDO: Power Data Object */
>   #define PDO_MAX_OBJECTS		7
>   
> diff --git a/drivers/staging/typec/tcpci.c b/drivers/staging/typec/tcpci.c
> index b6bf15b..8437ea1 100644
> --- a/drivers/staging/typec/tcpci.c
> +++ b/drivers/staging/typec/tcpci.c
> @@ -390,7 +390,7 @@ static irqreturn_t tcpci_irq(int irq, void *dev_id)
>   		regmap_read(tcpci->regmap, TCPC_RX_BYTE_CNT, &cnt);
>   
>   		tcpci_read16(tcpci, TCPC_RX_HDR, &header);
> -		msg.header = header;
> +		pd_message_header_set(&msg, header);
>   
>   		if (WARN_ON(cnt > sizeof(msg.payload)))
>   			cnt = sizeof(msg.payload);
> 



More information about the devel mailing list