Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2] usb: typec: tcpm: set correct data role for non-DRD
@ 2020-02-12  6:00 jun.li
  2020-02-13 17:12 ` Heikki Krogerus
  0 siblings, 1 reply; 3+ messages in thread
From: jun.li @ 2020-02-12  6:00 UTC (permalink / raw)
  To: linux, heikki.krogerus; +Cc: gregkh, jun.li, linux-imx, linux-usb

From: Li Jun <jun.li@nxp.com>

Since the typec port data role is separated from power role,
so check the port data capability when setting data role.

Signed-off-by: Li Jun <jun.li@nxp.com>
---
 drivers/usb/typec/tcpm/tcpm.c | 53 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index f3087ef..7009644 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -788,10 +788,30 @@ static int tcpm_set_roles(struct tcpm_port *port, bool attached,
 	else
 		orientation = TYPEC_ORIENTATION_REVERSE;
 
-	if (data == TYPEC_HOST)
-		usb_role = USB_ROLE_HOST;
-	else
-		usb_role = USB_ROLE_DEVICE;
+	if (port->typec_caps.data == TYPEC_PORT_DRD) {
+		if (data == TYPEC_HOST)
+			usb_role = USB_ROLE_HOST;
+		else
+			usb_role = USB_ROLE_DEVICE;
+	} else if (port->typec_caps.data == TYPEC_PORT_DFP) {
+		if (data == TYPEC_HOST) {
+			if (role == TYPEC_SOURCE)
+				usb_role = USB_ROLE_HOST;
+			else
+				usb_role = USB_ROLE_NONE;
+		} else {
+			return -ENOTSUPP;
+		}
+	} else {
+		if (data == TYPEC_DEVICE) {
+			if (role == TYPEC_SINK)
+				usb_role = USB_ROLE_DEVICE;
+			else
+				usb_role = USB_ROLE_NONE;
+		} else {
+			return -ENOTSUPP;
+		}
+	}
 
 	ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
 	if (ret < 0)
@@ -1817,7 +1837,7 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
 		tcpm_set_state(port, SOFT_RESET, 0);
 		break;
 	case PD_CTRL_DR_SWAP:
-		if (port->port_type != TYPEC_PORT_DRP) {
+		if (port->typec_caps.data != TYPEC_PORT_DRD) {
 			tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
 			break;
 		}
@@ -2609,6 +2629,9 @@ static int tcpm_src_attach(struct tcpm_port *port)
 	enum typec_cc_polarity polarity =
 				port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
 							 : TYPEC_POLARITY_CC1;
+	enum typec_data_role data_role =
+				port->typec_caps.data == TYPEC_PORT_UFP ?
+				TYPEC_DEVICE : TYPEC_HOST;
 	int ret;
 
 	if (port->attached)
@@ -2618,7 +2641,7 @@ static int tcpm_src_attach(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
-	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
+	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
 	if (ret < 0)
 		return ret;
 
@@ -2730,6 +2753,9 @@ static void tcpm_src_detach(struct tcpm_port *port)
 
 static int tcpm_snk_attach(struct tcpm_port *port)
 {
+	enum typec_data_role data_role =
+				port->typec_caps.data == TYPEC_PORT_DFP ?
+				TYPEC_HOST : TYPEC_DEVICE;
 	int ret;
 
 	if (port->attached)
@@ -2740,7 +2766,7 @@ static int tcpm_snk_attach(struct tcpm_port *port)
 	if (ret < 0)
 		return ret;
 
-	ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
+	ret = tcpm_set_roles(port, true, TYPEC_SINK, data_role);
 	if (ret < 0)
 		return ret;
 
@@ -2761,12 +2787,15 @@ static void tcpm_snk_detach(struct tcpm_port *port)
 
 static int tcpm_acc_attach(struct tcpm_port *port)
 {
+	enum typec_data_role data_role =
+				port->typec_caps.data == TYPEC_PORT_UFP ?
+				TYPEC_DEVICE : TYPEC_HOST;
 	int ret;
 
 	if (port->attached)
 		return 0;
 
-	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
+	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
 	if (ret < 0)
 		return ret;
 
@@ -3293,7 +3322,8 @@ static void run_state_machine(struct tcpm_port *port)
 		tcpm_set_vconn(port, true);
 		tcpm_set_vbus(port, false);
 		tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
-			       TYPEC_HOST);
+			       port->typec_caps.data == TYPEC_PORT_UFP ?
+			       TYPEC_DEVICE : TYPEC_HOST);
 		tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
 		break;
 	case SRC_HARD_RESET_VBUS_ON:
@@ -3308,7 +3338,8 @@ static void run_state_machine(struct tcpm_port *port)
 		if (port->pd_capable)
 			tcpm_set_charge(port, false);
 		tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
-			       TYPEC_DEVICE);
+			       port->typec_caps.data == TYPEC_PORT_DFP ?
+			       TYPEC_HOST : TYPEC_DEVICE);
 		/*
 		 * VBUS may or may not toggle, depending on the adapter.
 		 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
@@ -3969,7 +4000,7 @@ static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
 	mutex_lock(&port->swap_lock);
 	mutex_lock(&port->lock);
 
-	if (port->port_type != TYPEC_PORT_DRP) {
+	if (port->typec_caps.data != TYPEC_PORT_DRD) {
 		ret = -EINVAL;
 		goto port_unlock;
 	}
-- 
2.7.4


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] usb: typec: tcpm: set correct data role for non-DRD
  2020-02-12  6:00 [PATCH v2] usb: typec: tcpm: set correct data role for non-DRD jun.li
@ 2020-02-13 17:12 ` Heikki Krogerus
  2020-02-13 17:42   ` Guenter Roeck
  0 siblings, 1 reply; 3+ messages in thread
From: Heikki Krogerus @ 2020-02-13 17:12 UTC (permalink / raw)
  To: jun.li; +Cc: linux, gregkh, linux-imx, linux-usb

Hi,

On Wed, Feb 12, 2020 at 02:00:03PM +0800, jun.li@nxp.com wrote:
> From: Li Jun <jun.li@nxp.com>
> 
> Since the typec port data role is separated from power role,
> so check the port data capability when setting data role.
> 
> Signed-off-by: Li Jun <jun.li@nxp.com>

You forgot the change log. I put a minor nitpick bellow, but feel
free to ignore it if there are no other comments.

Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
>  drivers/usb/typec/tcpm/tcpm.c | 53 ++++++++++++++++++++++++++++++++++---------
>  1 file changed, 42 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index f3087ef..7009644 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -788,10 +788,30 @@ static int tcpm_set_roles(struct tcpm_port *port, bool attached,
>  	else
>  		orientation = TYPEC_ORIENTATION_REVERSE;
>  
> -	if (data == TYPEC_HOST)
> -		usb_role = USB_ROLE_HOST;
> -	else
> -		usb_role = USB_ROLE_DEVICE;
> +	if (port->typec_caps.data == TYPEC_PORT_DRD) {
> +		if (data == TYPEC_HOST)
> +			usb_role = USB_ROLE_HOST;
> +		else
> +			usb_role = USB_ROLE_DEVICE;
> +	} else if (port->typec_caps.data == TYPEC_PORT_DFP) {
> +		if (data == TYPEC_HOST) {
> +			if (role == TYPEC_SOURCE)
> +				usb_role = USB_ROLE_HOST;
> +			else
> +				usb_role = USB_ROLE_NONE;
> +		} else {
> +			return -ENOTSUPP;
> +		}
> +	} else {
> +		if (data == TYPEC_DEVICE) {
> +			if (role == TYPEC_SINK)
> +				usb_role = USB_ROLE_DEVICE;
> +			else
> +				usb_role = USB_ROLE_NONE;
> +		} else {
> +			return -ENOTSUPP;
> +		}
> +	}
>  
>  	ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
>  	if (ret < 0)
> @@ -1817,7 +1837,7 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
>  		tcpm_set_state(port, SOFT_RESET, 0);
>  		break;
>  	case PD_CTRL_DR_SWAP:
> -		if (port->port_type != TYPEC_PORT_DRP) {
> +		if (port->typec_caps.data != TYPEC_PORT_DRD) {
>  			tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
>  			break;
>  		}
> @@ -2609,6 +2629,9 @@ static int tcpm_src_attach(struct tcpm_port *port)
>  	enum typec_cc_polarity polarity =
>  				port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
>  							 : TYPEC_POLARITY_CC1;
> +	enum typec_data_role data_role =
> +				port->typec_caps.data == TYPEC_PORT_UFP ?
> +				TYPEC_DEVICE : TYPEC_HOST;

To me something like this would be more readable:

	enum typec_data_role data_role = TYPEC_HOST;

        if (port->typec_caps.data == TYPEC_PORT_UFP)
                data_role = TYPEC_DEVICE;

It does not actually add any lines in this case.

>  	int ret;
>  
>  	if (port->attached)
> @@ -2618,7 +2641,7 @@ static int tcpm_src_attach(struct tcpm_port *port)
>  	if (ret < 0)
>  		return ret;
>  
> -	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
> +	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
>  	if (ret < 0)
>  		return ret;
>  
> @@ -2730,6 +2753,9 @@ static void tcpm_src_detach(struct tcpm_port *port)
>  
>  static int tcpm_snk_attach(struct tcpm_port *port)
>  {
> +	enum typec_data_role data_role =
> +				port->typec_caps.data == TYPEC_PORT_DFP ?
> +				TYPEC_HOST : TYPEC_DEVICE;

ditto.

>  	int ret;
>  
>  	if (port->attached)
> @@ -2740,7 +2766,7 @@ static int tcpm_snk_attach(struct tcpm_port *port)
>  	if (ret < 0)
>  		return ret;
>  
> -	ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
> +	ret = tcpm_set_roles(port, true, TYPEC_SINK, data_role);
>  	if (ret < 0)
>  		return ret;
>  
> @@ -2761,12 +2787,15 @@ static void tcpm_snk_detach(struct tcpm_port *port)
>  
>  static int tcpm_acc_attach(struct tcpm_port *port)
>  {
> +	enum typec_data_role data_role =
> +				port->typec_caps.data == TYPEC_PORT_UFP ?
> +				TYPEC_DEVICE : TYPEC_HOST;

Here as well.

>  	int ret;
>  
>  	if (port->attached)
>  		return 0;
>  
> -	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
> +	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
>  	if (ret < 0)
>  		return ret;
>  
> @@ -3293,7 +3322,8 @@ static void run_state_machine(struct tcpm_port *port)
>  		tcpm_set_vconn(port, true);
>  		tcpm_set_vbus(port, false);
>  		tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
> -			       TYPEC_HOST);
> +			       port->typec_caps.data == TYPEC_PORT_UFP ?
> +			       TYPEC_DEVICE : TYPEC_HOST);
>  		tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
>  		break;
>  	case SRC_HARD_RESET_VBUS_ON:
> @@ -3308,7 +3338,8 @@ static void run_state_machine(struct tcpm_port *port)
>  		if (port->pd_capable)
>  			tcpm_set_charge(port, false);
>  		tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
> -			       TYPEC_DEVICE);
> +			       port->typec_caps.data == TYPEC_PORT_DFP ?
> +			       TYPEC_HOST : TYPEC_DEVICE);
>  		/*
>  		 * VBUS may or may not toggle, depending on the adapter.
>  		 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
> @@ -3969,7 +4000,7 @@ static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
>  	mutex_lock(&port->swap_lock);
>  	mutex_lock(&port->lock);
>  
> -	if (port->port_type != TYPEC_PORT_DRP) {
> +	if (port->typec_caps.data != TYPEC_PORT_DRD) {
>  		ret = -EINVAL;
>  		goto port_unlock;
>  	}
> -- 
> 2.7.4

thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] usb: typec: tcpm: set correct data role for non-DRD
  2020-02-13 17:12 ` Heikki Krogerus
@ 2020-02-13 17:42   ` Guenter Roeck
  0 siblings, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2020-02-13 17:42 UTC (permalink / raw)
  To: Heikki Krogerus; +Cc: jun.li, gregkh, linux-imx, linux-usb

On Thu, Feb 13, 2020 at 07:12:16PM +0200, Heikki Krogerus wrote:
> Hi,
> 
> On Wed, Feb 12, 2020 at 02:00:03PM +0800, jun.li@nxp.com wrote:
> > From: Li Jun <jun.li@nxp.com>
> > 
> > Since the typec port data role is separated from power role,
> > so check the port data capability when setting data role.
> > 
> > Signed-off-by: Li Jun <jun.li@nxp.com>
> 
> You forgot the change log. I put a minor nitpick bellow, but feel
> free to ignore it if there are no other comments.
> 
> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> > ---
> >  drivers/usb/typec/tcpm/tcpm.c | 53 ++++++++++++++++++++++++++++++++++---------
> >  1 file changed, 42 insertions(+), 11 deletions(-)
> > 
> > diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> > index f3087ef..7009644 100644
> > --- a/drivers/usb/typec/tcpm/tcpm.c
> > +++ b/drivers/usb/typec/tcpm/tcpm.c
> > @@ -788,10 +788,30 @@ static int tcpm_set_roles(struct tcpm_port *port, bool attached,
> >  	else
> >  		orientation = TYPEC_ORIENTATION_REVERSE;
> >  
> > -	if (data == TYPEC_HOST)
> > -		usb_role = USB_ROLE_HOST;
> > -	else
> > -		usb_role = USB_ROLE_DEVICE;
> > +	if (port->typec_caps.data == TYPEC_PORT_DRD) {
> > +		if (data == TYPEC_HOST)
> > +			usb_role = USB_ROLE_HOST;
> > +		else
> > +			usb_role = USB_ROLE_DEVICE;
> > +	} else if (port->typec_caps.data == TYPEC_PORT_DFP) {
> > +		if (data == TYPEC_HOST) {
> > +			if (role == TYPEC_SOURCE)
> > +				usb_role = USB_ROLE_HOST;
> > +			else
> > +				usb_role = USB_ROLE_NONE;
> > +		} else {
> > +			return -ENOTSUPP;
> > +		}
> > +	} else {
> > +		if (data == TYPEC_DEVICE) {
> > +			if (role == TYPEC_SINK)
> > +				usb_role = USB_ROLE_DEVICE;
> > +			else
> > +				usb_role = USB_ROLE_NONE;
> > +		} else {
> > +			return -ENOTSUPP;
> > +		}
> > +	}
> >  
> >  	ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
> >  	if (ret < 0)
> > @@ -1817,7 +1837,7 @@ static void tcpm_pd_ctrl_request(struct tcpm_port *port,
> >  		tcpm_set_state(port, SOFT_RESET, 0);
> >  		break;
> >  	case PD_CTRL_DR_SWAP:
> > -		if (port->port_type != TYPEC_PORT_DRP) {
> > +		if (port->typec_caps.data != TYPEC_PORT_DRD) {
> >  			tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
> >  			break;
> >  		}
> > @@ -2609,6 +2629,9 @@ static int tcpm_src_attach(struct tcpm_port *port)
> >  	enum typec_cc_polarity polarity =
> >  				port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
> >  							 : TYPEC_POLARITY_CC1;
> > +	enum typec_data_role data_role =
> > +				port->typec_caps.data == TYPEC_PORT_UFP ?
> > +				TYPEC_DEVICE : TYPEC_HOST;
> 
> To me something like this would be more readable:
> 
> 	enum typec_data_role data_role = TYPEC_HOST;
> 
>         if (port->typec_caps.data == TYPEC_PORT_UFP)
>                 data_role = TYPEC_DEVICE;
> 
> It does not actually add any lines in this case.
> 

A macro for those might be even better readable, especially since
the same sequence is needed several times.

Guenter

> >  	int ret;
> >  
> >  	if (port->attached)
> > @@ -2618,7 +2641,7 @@ static int tcpm_src_attach(struct tcpm_port *port)
> >  	if (ret < 0)
> >  		return ret;
> >  
> > -	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
> > +	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
> >  	if (ret < 0)
> >  		return ret;
> >  
> > @@ -2730,6 +2753,9 @@ static void tcpm_src_detach(struct tcpm_port *port)
> >  
> >  static int tcpm_snk_attach(struct tcpm_port *port)
> >  {
> > +	enum typec_data_role data_role =
> > +				port->typec_caps.data == TYPEC_PORT_DFP ?
> > +				TYPEC_HOST : TYPEC_DEVICE;
> 
> ditto.
> 
> >  	int ret;
> >  
> >  	if (port->attached)
> > @@ -2740,7 +2766,7 @@ static int tcpm_snk_attach(struct tcpm_port *port)
> >  	if (ret < 0)
> >  		return ret;
> >  
> > -	ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
> > +	ret = tcpm_set_roles(port, true, TYPEC_SINK, data_role);
> >  	if (ret < 0)
> >  		return ret;
> >  
> > @@ -2761,12 +2787,15 @@ static void tcpm_snk_detach(struct tcpm_port *port)
> >  
> >  static int tcpm_acc_attach(struct tcpm_port *port)
> >  {
> > +	enum typec_data_role data_role =
> > +				port->typec_caps.data == TYPEC_PORT_UFP ?
> > +				TYPEC_DEVICE : TYPEC_HOST;
> 
> Here as well.
> 
> >  	int ret;
> >  
> >  	if (port->attached)
> >  		return 0;
> >  
> > -	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
> > +	ret = tcpm_set_roles(port, true, TYPEC_SOURCE, data_role);
> >  	if (ret < 0)
> >  		return ret;
> >  
> > @@ -3293,7 +3322,8 @@ static void run_state_machine(struct tcpm_port *port)
> >  		tcpm_set_vconn(port, true);
> >  		tcpm_set_vbus(port, false);
> >  		tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
> > -			       TYPEC_HOST);
> > +			       port->typec_caps.data == TYPEC_PORT_UFP ?
> > +			       TYPEC_DEVICE : TYPEC_HOST);
> >  		tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
> >  		break;
> >  	case SRC_HARD_RESET_VBUS_ON:
> > @@ -3308,7 +3338,8 @@ static void run_state_machine(struct tcpm_port *port)
> >  		if (port->pd_capable)
> >  			tcpm_set_charge(port, false);
> >  		tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
> > -			       TYPEC_DEVICE);
> > +			       port->typec_caps.data == TYPEC_PORT_DFP ?
> > +			       TYPEC_HOST : TYPEC_DEVICE);
> >  		/*
> >  		 * VBUS may or may not toggle, depending on the adapter.
> >  		 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
> > @@ -3969,7 +4000,7 @@ static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
> >  	mutex_lock(&port->swap_lock);
> >  	mutex_lock(&port->lock);
> >  
> > -	if (port->port_type != TYPEC_PORT_DRP) {
> > +	if (port->typec_caps.data != TYPEC_PORT_DRD) {
> >  		ret = -EINVAL;
> >  		goto port_unlock;
> >  	}
> > -- 
> > 2.7.4
> 
> thanks,
> 
> -- 
> heikki

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-12  6:00 [PATCH v2] usb: typec: tcpm: set correct data role for non-DRD jun.li
2020-02-13 17:12 ` Heikki Krogerus
2020-02-13 17:42   ` Guenter Roeck

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org
	public-inbox-index linux-usb

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git