Received: by 10.223.164.202 with SMTP id h10csp2692589wrb; Sun, 12 Nov 2017 16:07:12 -0800 (PST) X-Google-Smtp-Source: AGs4zMak7SeQ0OjuUxnvG1ORIOjq94yj7v9MkP095LmGWGWn5j5k8jqeNWwy+cfgyI/CgXObvU2w X-Received: by 10.99.96.87 with SMTP id u84mr7070043pgb.424.1510531632765; Sun, 12 Nov 2017 16:07:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510531632; cv=none; d=google.com; s=arc-20160816; b=QO3W69rxxtsq6KV0anpkXHkDnexTN0v7iR+cZLnJCXf7ZZLuYlCtHE/RmIHy5mYQT2 ysXyoelIEgLjSAMg9pfo2cSt/S9ciMcitEsb2KiPoay9bDjtHCX4cCIBSMYtuvQ/Dv5k v4GpgZfdFJtRWLV4OaaOhmKL5M+bthDMjN6VF+4ixc0LlypXCCv8QNP9ytb8QEWKCaFM TvM06F2u59wafffegLc0YNDde7/Tc1Bs0zcZqRCmojPv8ZywDwZODCmQbU/cfQgcgcjQ TLFpxCwBl7YpB5mcicPB1cyrCGFwwv86jQRzyL8sTg/tbpMu7k2YX9UY+FniA/B1412D Yb2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=qVgYCs8WKQe6+7cHEIOOJyokfgwxhm0XYkQxauBISHY=; b=qvNiJGtPnF8KsryUUDdmJQJ+nV00n/9p6pbgxSjHICf4+pcggZ10OG85UYbVXycwJH 7xyEOug0P5hsChnZvcMeNGer5jqmBO298ECB+i/rXsmhOJocV8GhcwsvSBbc+0yoBWhW NG64WJcn2basGe4QtICsyZGtbaQvQ6QgNBRJmdLxXg8kQTehvKcjFf7+bTolR4iBDyXt abMvT31J9Jqhw4b6alZAlDroKrnagUYUNKCk9+ykojohcEleUqiSXxjl+u+IwTLmVWc6 nkh9rQsSrmxAvduXQOZMXwdFGVaY11PVPMjEjW+SHOTTnsQMDbr8niQphY5ILgR1f8dz DgwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WKLXM4P9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b89si13304378plb.556.2017.11.12.16.07.00; Sun, 12 Nov 2017 16:07:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WKLXM4P9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751343AbdKMAGY (ORCPT + 86 others); Sun, 12 Nov 2017 19:06:24 -0500 Received: from mail-oi0-f66.google.com ([209.85.218.66]:46709 "EHLO mail-oi0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750985AbdKMAGW (ORCPT ); Sun, 12 Nov 2017 19:06:22 -0500 Received: by mail-oi0-f66.google.com with SMTP id n16so1129903oig.3 for ; Sun, 12 Nov 2017 16:06:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=qVgYCs8WKQe6+7cHEIOOJyokfgwxhm0XYkQxauBISHY=; b=WKLXM4P9DW6L8FV68HFt1jIZTK32A24YyUWNXhiz3dm3f4ZpuLyQpniSkXYlYKL/Va r+7wVRiVefpM8Hht+I5D12NvKkzC5yJKuvgKLUozrZUjxrBbAXLCm1/nkcmB6qOtKTYM qo5bm6aRe6xUTmWwOYanC+o17N/bB2x9/hps34VhCJ7JIYrKOxjdP2ma6KeGEF9SNSCU jHYHV+uqxVtjLhPF04yVpBQJ/54g6vA1LDERnwM7onwRg615ynjN5OZDEcCjR6WSgCvI doWyjrTAzlxdUEL71yWbu5TW5WmKV4fwBNkYuTyyxgwbPK3BasXTNTibdk2bnW1VWIVh /Wrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=qVgYCs8WKQe6+7cHEIOOJyokfgwxhm0XYkQxauBISHY=; b=rUxPplLGuwE2/DDfCAPvGMG7Lw7mrY90mowI3Q2y6LKHcgDXar0JnCx3S/Vs5Qbuyl 6Xxk5iWP84+PX5vyGX+SCyPYA1QoF6NROkNqgO+/51PR0kQsgO0Zc/WOaTG2h2DOrPMe 21YSWJBn+9yUslN9M2cgSmiZJX+Ovpij9lEzdwOpAvlmnwCYr3Wqss6VXJDW+WT7QHZE snlhCWx7WbyGxUCH13NOPFwzcmliLG40K+1lyHFdGrkGgNVfWcf8S38eDYMDxbGV8Dur b3U7Dm57DrT1gyk3YcmfXmCXWyRxgdZQSPv4keWB1+oTBIVeUNZWnofGCMUGDU+4TAU+ 2bhw== X-Gm-Message-State: AJaThX65ZnHPLT8MeXDF/2zs6yQLrjWBcboMkdwZrQmpB0oUxVJbekHU 0Qz8qdZjC1AfNQNfQp6XhT68zYUCZ3wjtwmaoY6ooA== X-Received: by 10.202.221.2 with SMTP id u2mr4312781oig.78.1510531581459; Sun, 12 Nov 2017 16:06:21 -0800 (PST) MIME-Version: 1.0 Received: by 10.74.168.131 with HTTP; Sun, 12 Nov 2017 16:05:40 -0800 (PST) In-Reply-To: <20171107110030.GD18063@kuha.fi.intel.com> References: <20171104192213.14117-1-Badhri@google.com> <20171107110030.GD18063@kuha.fi.intel.com> From: Badhri Jagan Sridharan Date: Sun, 12 Nov 2017 16:05:40 -0800 Message-ID: Subject: Re: [PATCH 1/2 v5] typec: tcpm: Validate source and sink caps To: Heikki Krogerus Cc: Greg Kroah-Hartman , Dan Carpenter , Guenter Roeck , USB , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks Heikki ! I am making another version addressing your nits as well. Guenter, Are you fine with the patch as well ? Regards, Badhri On Tue, Nov 7, 2017 at 3:00 AM, Heikki Krogerus wrote: > On Sat, Nov 04, 2017 at 12:22:12PM -0700, Badhri Jagan Sridharan wrote: >> The source and sink caps should follow the following rules. >> This patch validates whether the src_caps/snk_caps adheres >> to it. >> >> 6.4.1 Capabilities Message >> A Capabilities message (Source Capabilities message or Sink >> Capabilities message) shall have at least one Power >> Data Object for vSafe5V. The Capabilities message shall also >> contain the sending Port???s information followed by up to >> 6 additional Power Data Objects. Power Data Objects in a >> Capabilities message shall be sent in the following order: >> >> 1. The vSafe5V Fixed Supply Object shall always be the first object. >> 2. The remaining Fixed Supply Objects, if present, shall be sent >> in voltage order; lowest to highest. >> 3. The Battery Supply Objects, if present shall be sent in Minimum >> Voltage order; lowest to highest. >> 4. The Variable Supply (non-battery) Objects, if present, shall be >> sent in Minimum Voltage order; lowest to highest. >> >> Errors in source/sink_caps of the local port will prevent >> the port registration. Whereas, errors in source caps of partner >> device would only log them. >> >> Signed-off-by: Badhri Jagan Sridharan > > It would have been nice to have enum for the return values used in > tcpm_cap_err() IMHO, but never mind. Is Guenter OK with these? > > FWIW: > > Acked-by: Heikki Krogerus > > > Thanks, > >> --- >> hangelog since v1: >> - rebased on top drivers/usb/typec/tcpm.c as suggested by >> gregkh@linuxfoundation.org >> - renamed nr_snk_*_pdo as suggested by dan.carpenter@oracle.com >> - removed stale comment as suggested by linux@roeck-us.net >> - removed the tests for nr_snk_*_pdo as suggested by >> dan.carpenter@oracle.com >> - Fixed sytling as suggested by dan.carpenter@oracle.com >> - renamed tcpm_get_nr_type_pdos to nr_type_pdos as suggested by >> dan.carpenter@oracle.com >> - fixed nr_type_pdos as suggested by dan.carpenter@oracle.com >> - tcpm_pd_select_pdo now checks for all matching variable/batt pdos >> instead of the first matching one. >> >> Changelog since v2: >> - refactored error messages as suggested by >> heikki.krogerus@linux.intel.com >> >> Changelog since v3: >> - fixed formatting errors as suggested by >> heikki.krogerus@linux.intel.com >> >> Changelog since v4: >> - Reusing the macro >> >> drivers/usb/typec/tcpm.c | 115 +++++++++++++++++++++++++++++++++++++++++++---- >> include/linux/usb/pd.h | 2 + >> include/linux/usb/tcpm.h | 16 +++---- >> 3 files changed, 116 insertions(+), 17 deletions(-) >> >> diff --git a/drivers/usb/typec/tcpm.c b/drivers/usb/typec/tcpm.c >> index 2b735f3e5765..8b30ab69ed79 100644 >> --- a/drivers/usb/typec/tcpm.c >> +++ b/drivers/usb/typec/tcpm.c >> @@ -1256,6 +1256,85 @@ static void vdm_state_machine_work(struct work_struct *work) >> mutex_unlock(&port->lock); >> } >> >> +static const char * const pdo_err[] = { >> + "", >> + " err: source/sink caps should atleast have vSafe5V", >> + " err: vSafe5V Fixed Supply Object Shall always be the first object", >> + " err: PDOs should be in the following order: Fixed; Battery; Variable", >> + " err: Fixed supply pdos should be in increasing order of their fixed voltage", >> + " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage", >> + " err: Variable/Batt supply pdos cannot have same min/max voltage", >> +}; >> + >> +static int tcpm_caps_err(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo) >> +{ >> + unsigned int i; >> + >> + /* Should at least contain vSafe5v */ >> + if (nr_pdo < 1) >> + return 1; >> + >> + /* The vSafe5V Fixed Supply Object Shall always be the first object */ >> + if (pdo_type(pdo[0]) != PDO_TYPE_FIXED || >> + pdo_fixed_voltage(pdo[0]) != VSAFE5V) >> + return 2; >> + >> + for (i = 1; i < nr_pdo; i++) { >> + if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) { >> + return 3; >> + } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) { >> + enum pd_pdo_type type = pdo_type(pdo[i]); >> + >> + switch (type) { >> + /* >> + * The remaining Fixed Supply Objects, if >> + * present, shall be sent in voltage order; >> + * lowest to highest. >> + */ >> + case PDO_TYPE_FIXED: >> + if (pdo_fixed_voltage(pdo[i]) <= >> + pdo_fixed_voltage(pdo[i - 1])) >> + return 4; >> + break; >> + /* >> + * The Battery Supply Objects and Variable >> + * supply, if present shall be sent in Minimum >> + * Voltage order; lowest to highest. >> + */ >> + case PDO_TYPE_VAR: >> + case PDO_TYPE_BATT: >> + if (pdo_min_voltage(pdo[i]) < >> + pdo_min_voltage(pdo[i - 1])) >> + return 5; >> + else if ((pdo_min_voltage(pdo[i]) == >> + pdo_min_voltage(pdo[i - 1])) && >> + (pdo_max_voltage(pdo[i]) == >> + pdo_min_voltage(pdo[i - 1]))) >> + return 6; >> + break; >> + default: >> + tcpm_log_force(port, " Unknown pdo type"); >> + } >> + } >> + } >> + >> + return 0; >> +} >> + >> +static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo) >> +{ >> + int err_index = tcpm_caps_err(port, pdo, nr_pdo); >> + >> + if (err_index) { >> + tcpm_log_force(port, " %s", pdo_err[err_index]); >> + return -EINVAL; >> + } >> + >> + return 0; >> +} >> + >> /* >> * PD (data, control) command handling functions >> */ >> @@ -1278,6 +1357,9 @@ static void tcpm_pd_data_request(struct tcpm_port *port, >> >> tcpm_log_source_caps(port); >> >> + tcpm_validate_caps(port, port->source_caps, >> + port->nr_source_caps); >> + >> /* >> * This message may be received even if VBUS is not >> * present. This is quite unexpected; see USB PD >> @@ -3444,9 +3526,12 @@ static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo, >> return nr_vdo; >> } >> >> -void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, >> - unsigned int nr_pdo) >> +int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo) >> { >> + if (tcpm_validate_caps(port, pdo, nr_pdo)) >> + return -EINVAL; >> + >> mutex_lock(&port->lock); >> port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo); >> switch (port->state) { >> @@ -3466,16 +3551,20 @@ void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, >> break; >> } >> mutex_unlock(&port->lock); >> + return 0; >> } >> EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities); >> >> -void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, >> - unsigned int nr_pdo, >> - unsigned int max_snk_mv, >> - unsigned int max_snk_ma, >> - unsigned int max_snk_mw, >> - unsigned int operating_snk_mw) >> +int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo, >> + unsigned int max_snk_mv, >> + unsigned int max_snk_ma, >> + unsigned int max_snk_mw, >> + unsigned int operating_snk_mw) >> { >> + if (tcpm_validate_caps(port, pdo, nr_pdo)) >> + return -EINVAL; >> + >> mutex_lock(&port->lock); >> port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo); >> port->max_snk_mv = max_snk_mv; >> @@ -3494,6 +3583,7 @@ void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, >> break; >> } >> mutex_unlock(&port->lock); >> + return 0; >> } >> EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities); >> >> @@ -3529,7 +3619,15 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) >> >> init_completion(&port->tx_complete); >> init_completion(&port->swap_complete); >> + tcpm_debugfs_init(port); >> >> + if (tcpm_validate_caps(port, tcpc->config->src_pdo, >> + tcpc->config->nr_src_pdo) || >> + tcpm_validate_caps(port, tcpc->config->snk_pdo, >> + tcpc->config->nr_snk_pdo)) { >> + err = -EINVAL; >> + goto out_destroy_wq; >> + } >> port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcpc->config->src_pdo, >> tcpc->config->nr_src_pdo); >> port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcpc->config->snk_pdo, >> @@ -3584,7 +3682,6 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) >> } >> } >> >> - tcpm_debugfs_init(port); >> mutex_lock(&port->lock); >> tcpm_init(port); >> mutex_unlock(&port->lock); >> diff --git a/include/linux/usb/pd.h b/include/linux/usb/pd.h >> index e00051ced806..b3d41d7409b3 100644 >> --- a/include/linux/usb/pd.h >> +++ b/include/linux/usb/pd.h >> @@ -148,6 +148,8 @@ enum pd_pdo_type { >> (PDO_TYPE(PDO_TYPE_FIXED) | (flags) | \ >> PDO_FIXED_VOLT(mv) | PDO_FIXED_CURR(ma)) >> >> +#define VSAFE5V 5000 /* mv units */ >> + >> #define PDO_BATT_MAX_VOLT_SHIFT 20 /* 50mV units */ >> #define PDO_BATT_MIN_VOLT_SHIFT 10 /* 50mV units */ >> #define PDO_BATT_MAX_PWR_SHIFT 0 /* 250mW units */ >> diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h >> index 073197f0d2bb..ca1c0b57f03f 100644 >> --- a/include/linux/usb/tcpm.h >> +++ b/include/linux/usb/tcpm.h >> @@ -183,14 +183,14 @@ struct tcpm_port; >> struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc); >> void tcpm_unregister_port(struct tcpm_port *port); >> >> -void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, >> - unsigned int nr_pdo); >> -void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, >> - unsigned int nr_pdo, >> - unsigned int max_snk_mv, >> - unsigned int max_snk_ma, >> - unsigned int max_snk_mw, >> - unsigned int operating_snk_mw); >> +int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo); >> +int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo, >> + unsigned int nr_pdo, >> + unsigned int max_snk_mv, >> + unsigned int max_snk_ma, >> + unsigned int max_snk_mw, >> + unsigned int operating_snk_mw); >> >> void tcpm_vbus_change(struct tcpm_port *port); >> void tcpm_cc_change(struct tcpm_port *port); >> -- >> 2.15.0.403.gc27cc4dac6-goog > > -- > heikki From 1583411413298104815@xxx Tue Nov 07 12:46:37 +0000 2017 X-GM-THRID: 1583163198448891335 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread