Re: [PATCH v2 1/2] usb: typec: Add USB Power Delivery sink port support

From: Oliver Neukum
Date: Wed Jul 27 2016 - 05:25:53 EST


On Tue, 2016-07-26 at 11:37 -0700, Bin Gao wrote:
> +#define MAKE_HEADER(port, header, msg, objs) \
> +do { \
> + header->type = msg; \
> + header->data_role = PD_DATA_ROLE_UFP; \
> + header->revision = port->pd_rev; \
> + header->power_role = PD_POWER_ROLE_SINK; \
> + header->id = roll_msg_id(port); \
> + header->nr_objs = objs; \
> + header->extended = PD_MSG_NOT_EXTENDED; \
> +} while (0)
> +
> +static struct pd_sink_port *sink_ports[MAX_NR_SINK_PORTS];
> +static int nr_ports;
> +
> +BLOCKING_NOTIFIER_HEAD(pd_sink_notifier_list);
> +
> +static char *state_strings[] = {
> + "WAIT_FOR_SOURCE_CAPABILITY",
> + "REQUEST_SENT",
> + "ACCEPT_RECEIVED",
> + "POWER_SUPPLY_READY",
> +};
> +
> +/* Control messages */
> +static char *cmsg_strings[] = {
> + "GOODCRC", /* 1 */
> + "GOTOMIN", /* 2 */
> + "ACCEPT", /* 3 */
> + "REJECT", /* 4 */
> + "PING", /* 5 */
> + "PS_RDY", /* 6 */
> + "GET_SRC_CAP", /* 7 */
> + "GET_SINK_CAP", /* 8 */
> + "DR_SWAP", /* 9 */
> + "PR_SWAP", /* 10 */
> + "VCONN_SWAP", /* 11 */
> + "WAIT", /* 12 */
> + "SOFT_RESET", /* 13 */
> + "RESERVED", /* 14 */
> + "RESERVED", /* 15 */
> + "NOT_SUPPORTED", /* 16 */
> + "GET_SOURCE_CAP_EXTENDED", /* 17 */
> + "GET_STATUS", /* 18 */
> + "FR_SWAP", /* 19 */
> + /* RESERVED 20 - 31 */
> +};
> +
> +/* Data messages */
> +static char *dmsg_strings[] = {
> + "SOURCE_CAP", /* 1 */
> + "REQUEST", /* 2 */
> + "BIST", /* 3 */
> + "SINK_CAP", /* 4 */
> + "BATTERY_STATUS", /* 5 */
> + "ALERT", /* 6 */
> + "RESERVED", /* 7 */
> + "RESERVED", /* 8 */
> + "RESERVED", /* 9 */
> + "RESERVED", /* 10 */
> + "RESERVED", /* 11 */
> + "RESERVED", /* 12 */
> + "RESERVED", /* 13 */
> + "RESERVED", /* 14 */
> + "VENDOR_DEFINED", /* 15 */
> + /* RESERVED 16 - 31 */
> +};
> +
> +static char *state_to_string(enum pd_sink_state state)
> +{
> + if (state < ARRAY_SIZE(state_strings))
> + return state_strings[state];
> + else
> + return NULL;
> +}
> +
> +static char *msg_to_string(bool is_cmsg, u8 msg)
> +{
> + int nr = is_cmsg ? ARRAY_SIZE(cmsg_strings) :
> ARRAY_SIZE(dmsg_strings);
> +
> + if (msg <= nr)
> + return is_cmsg ? cmsg_strings[msg - 1] :
> dmsg_strings[msg - 1];
> + else
> + return "RESERVED";
> +}
> +
> +static void print_message(int port, bool is_cmsg, u8 msg, bool recv)
> +{
> + pr_debug("sink port %d: %s message %s %s\n", port,
> + is_cmsg ? "Control" : "Data",
> + msg_to_string(is_cmsg, msg),
> + recv ? "received" : "sent)");
> +}
> +
> +static inline bool fixed_ps_equal(struct sink_ps *p1,
> + struct sink_ps *p2)
> +{
> + return p1->ps_type == p2->ps_type &&
> + p1->ps_fixed.voltage_fixed ==
> p2->ps_fixed.voltage_fixed &&
> + p1->ps_fixed.current_default ==
> p2->ps_fixed.current_default;
> +}
> +
> +/* The message ID increments each time we send out a new message */
> +static u8 roll_msg_id(struct pd_sink_port *port)
> +{
> + u8 msg_id = port->msg_id;
> +
> + if (msg_id == PD_MSG_ID_MAX)
> + msg_id = PD_MSG_ID_MIN;
> + else
> + msg_id++;
> +
> + port->msg_id = msg_id;
> + return msg_id;
> +}
> +

These pieces of code are completely generic. They should be shared
among drivers. Please move them into the include files.

Regards
Oliver