Received: by 10.223.164.197 with SMTP id h5csp344439wrb; Sat, 4 Nov 2017 12:15:09 -0700 (PDT) X-Google-Smtp-Source: ABhQp+S5bhipPUYqxt6XEWs1rqRsDjUka6hXUdnOQ9oDA3ZX+JUsUCIsLRyyd/RxoVvz70a5CAIi X-Received: by 10.84.179.99 with SMTP id a90mr1439692plc.142.1509822909207; Sat, 04 Nov 2017 12:15:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509822909; cv=none; d=google.com; s=arc-20160816; b=nEuxBXDcZJywB5UVI+NBNpf+zRVpOebeJAXo7wn2TbzXvshOamIUqfrBlgMEnTz8NU TPL+KlfAH4HQdZmhFBSyQc5Tuw294lKnCWSZtL0h70tQE5/y2LMEc9lvRB/7pf6dNELf rPvwhCubANkY16UjG09bSaAz53K8jXidkrgvQlqC8cksS01f9sH4eSOIOPlKqKjlgYaC 1+41ug2rJRf0hh6MgoJRZGY6aJR3/rpJtjBbRhm74ZDrto88lCjcWxVU4qHnHCEJdz/E sYvx/hEBlvqyE8hx//aX6mwns5lsTP4WYbUfX/OYSr3a8WzvvcS+O7qa56ANiOcQx4h8 UQGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=9m/vLQMP8Oe71y/EYvMuXLV+IJhZHJbQrIGW+IYyaTs=; b=shQyirclm6py5nE0CehHl7RYZc9uaGhtt7OQsS/tzAIxaUPw7WLxvFiU7ttAw2a0xA DoisaTYmZkWIc9B3Oc7N1+06xt3yCb/nSoO/mLHvrEPvb7Coa1srGURo59IPN0tEJOKi 3ZIAvGqzMUZVq91AtavqbqKBY/uv8/BCohNPA93L6v+OnbQ8s5i4YceX4vg3SAESE6ID ABEnhe1tOA9YtkAOTPGtvH4z83A8QBCLTQahcsZxSO5tRky9qlWal/oUF2jrRq/doErB ahmyOQQfRTABHvgItfZqnKUt7mUkOrridSKV7nBy1nZVXiRqms3P2eLWtfXACTs0rwpn +IsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=teTPYaXE; 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 z4si8563509pgs.301.2017.11.04.12.14.55; Sat, 04 Nov 2017 12:15:09 -0700 (PDT) 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=teTPYaXE; 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 S932192AbdKDTOP (ORCPT + 94 others); Sat, 4 Nov 2017 15:14:15 -0400 Received: from mail-oi0-f65.google.com ([209.85.218.65]:51007 "EHLO mail-oi0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751388AbdKDTON (ORCPT ); Sat, 4 Nov 2017 15:14:13 -0400 Received: by mail-oi0-f65.google.com with SMTP id q4so4410247oic.7 for ; Sat, 04 Nov 2017 12:14:13 -0700 (PDT) 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:content-transfer-encoding; bh=9m/vLQMP8Oe71y/EYvMuXLV+IJhZHJbQrIGW+IYyaTs=; b=teTPYaXEiLTg5XtJcIjI5LW/ztRvdDV1O6laPeR7g+Yza0aRknBTL9d6Dc2rcyg274 zR4x1WCfVcwaP8GgZGn1pJkUYnM5DR8Fj2kaIuagi0GKE0NDun8JwMtGlFZtKKZJ/ENr zueb6f4x4PIDe9Dld11JsyWO58ASL1PY/uHVO8fmX2MmuojQOgXmByic9b0s+1Ab3ou1 ha8ktr6Y8Ap/NIDppu/9DklqSK9uG4kHlWdYc5v8ybBYFXc6MPE3Z/fGRL8gK9PVkkd4 5lwbVPicpIZBsh1bU/sWgaOdAoeP6PP+5Iif78PRyw27mse1CFc95b3hBu7cnigOYHMv 58bg== 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:content-transfer-encoding; bh=9m/vLQMP8Oe71y/EYvMuXLV+IJhZHJbQrIGW+IYyaTs=; b=qn46xzHEjFhZn/KL7Eup9g+8z/TJXrRZXl9I2Pcn1kOcCvMad3qP46OFysvmebzTyj bqhZOMnU/xhwca0hOGmSodqChyqCDnSP1i28d+AQTq77xsYWRU5MJgLHK7Bo4ZgzxF45 L7hkGDGaHwLi9SHUiU5FqTEWrS/NL5hoi8oj8YYrkjBX3EnLTUPG8+oWqcSrS8doIfVc l5hffLY74ET5IAJqMzlszex+tZ/fuLvQ4c8ZZzSuMveRJvfZhAwfHSPzncA14UCitQvp //JjAKO7Q2nDHOF/9MLcmNASVE4ugg6WlKCgVmjo0QEsz9ZhYsSadpcfJMiJNpetlG8g K5Xg== X-Gm-Message-State: AJaThX7ttph3cFfBX7VIiwlN+cOgjXdSKjM2Ff3Q0t6cwgojEfHZ1Mwx fHGiVXhYLE+UjFvkKA4rT4Ky0QvlQQkqGWm41ymJgQ== X-Received: by 10.202.170.66 with SMTP id t63mr1974842oie.399.1509822852387; Sat, 04 Nov 2017 12:14:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.168.131 with HTTP; Sat, 4 Nov 2017 12:13:31 -0700 (PDT) In-Reply-To: <20171104185953.9532-1-Badhri@google.com> References: <20171104185953.9532-1-Badhri@google.com> From: Badhri Jagan Sridharan Date: Sat, 4 Nov 2017 12:13:31 -0700 Message-ID: Subject: Re: [PATCH 1/2 v4] typec: tcpm: Validate source and sink caps To: Heikki Krogerus , Greg Kroah-Hartman , Dan Carpenter , Guenter Roeck Cc: USB , LKML , Badhri Jagan Sridharan Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Please disregard v4. Going to send another version again. On Sat, Nov 4, 2017 at 11:59 AM, 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=E2=80=99s 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 > --- > Changelog 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 > > 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 8483d3e33853..ecdad71a6159 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_str= uct *work) > mutex_unlock(&port->lock); > } > > +static const char * const pdo_err[] =3D { > + "", > + " err: source/sink caps should atleast have vSafe5V", > + " err: vSafe5V Fixed Supply Object Shall always be the first obje= ct", > + " err: PDOs should be in the following order: Fixed; Battery; Var= iable", > + " err: Fixed supply pdos should be in increasing order of their f= ixed 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 obje= ct */ > + if (pdo_type(pdo[0]) !=3D PDO_TYPE_FIXED || > + pdo_fixed_voltage(pdo[0]) !=3D VSAFE5V) > + return 2; > + > + for (i =3D 1; i < nr_pdo; i++) { > + if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) { > + return 3; > + } else if (pdo_type(pdo[i]) =3D=3D pdo_type(pdo[i - 1])) = { > + enum pd_pdo_type type =3D 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]) <=3D > + 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]) =3D=3D > + pdo_min_voltage(pdo[i - 1])) && > + (pdo_max_voltage(pdo[i]) =3D=3D > + 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 =3D 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 *p= do, > + unsigned int nr_pdo) > { > + if (tcpm_validate_caps(port, pdo, nr_pdo)) > + return -EINVAL; > + > mutex_lock(&port->lock); > port->nr_src_pdo =3D 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 *pd= o, > - 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 =3D tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo); > port->max_snk_mv =3D 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 =3D -EINVAL; > + goto out_destroy_wq; > + } > port->nr_src_pdo =3D tcpm_copy_pdos(port->src_pdo, tcpc->config->= src_pdo, > tcpc->config->nr_src_pdo); > port->nr_snk_pdo =3D 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 *pd= o, > - 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 *p= do, > + 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 > From 1583163198448891335@xxx Sat Nov 04 19:01:21 +0000 2017 X-GM-THRID: 1583163198448891335 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread