diff mbox series

[1/7] usb: typec: Copy everything from struct typec_capability during registration

Message ID 20191001094858.68643-2-heikki.krogerus@linux.intel.com (mailing list archive)
State Superseded
Headers show
Series usb: typec: Small API improvement | expand

Commit Message

Heikki Krogerus Oct. 1, 2019, 9:48 a.m. UTC
Copying everything from struct typec_capability to struct
typec_port during port registration.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/class.c | 55 +++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 20 deletions(-)

Comments

Guenter Roeck Oct. 1, 2019, 1:08 p.m. UTC | #1
On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Copying everything from struct typec_capability to struct
> typec_port during port registration.
> 
What is the purpose of this patch ? To reduce the number of indirections at
runtime, or to avoid having to have cap around ?

FWIW, it looks like the code doesn't copy _all_ variables (eg cap->try_role),
and it doesn't drop port->cap. Am I missing something ?

> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>   drivers/usb/typec/class.c | 55 +++++++++++++++++++++++++--------------
>   1 file changed, 35 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 94a3eda62add..3835e2d9fba6 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -46,8 +46,14 @@ struct typec_port {
>   	enum typec_role			vconn_role;
>   	enum typec_pwr_opmode		pwr_opmode;
>   	enum typec_port_type		port_type;
> +	enum typec_port_type		fixed_role;
> +	enum typec_port_data		port_roles;
> +	enum typec_accessory		accessory[TYPEC_MAX_ACCESSORY];

Would a pointer to cap->accessory be sufficient ? Or is there a reason to copy
the actual array ?

>   	struct mutex			port_type_lock;
>   
> +	u16				revision;
> +	u16				pd_revision;
> +
>   	enum typec_orientation		orientation;
>   	struct typec_switch		*sw;
>   	struct typec_mux		*mux;
> @@ -950,7 +956,7 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
>   	int role;
>   	int ret;
>   
> -	if (port->cap->type != TYPEC_PORT_DRP) {
> +	if (port->fixed_role != TYPEC_PORT_DRP) {
>   		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -982,7 +988,7 @@ preferred_role_show(struct device *dev, struct device_attribute *attr,
>   {
>   	struct typec_port *port = to_typec_port(dev);
>   
> -	if (port->cap->type != TYPEC_PORT_DRP)
> +	if (port->fixed_role != TYPEC_PORT_DRP)
>   		return 0;
>   
>   	if (port->prefer_role < 0)
> @@ -1009,7 +1015,7 @@ static ssize_t data_role_store(struct device *dev,
>   		return ret;
>   
>   	mutex_lock(&port->port_type_lock);
> -	if (port->cap->data != TYPEC_PORT_DRD) {
> +	if (port->port_roles != TYPEC_PORT_DRD) {
>   		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;
>   	}
> @@ -1029,7 +1035,7 @@ static ssize_t data_role_show(struct device *dev,
>   {
>   	struct typec_port *port = to_typec_port(dev);
>   
> -	if (port->cap->data == TYPEC_PORT_DRD)
> +	if (port->port_roles == TYPEC_PORT_DRD)
>   		return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
>   			       "[host] device" : "host [device]");
>   
> @@ -1044,7 +1050,7 @@ static ssize_t power_role_store(struct device *dev,
>   	struct typec_port *port = to_typec_port(dev);
>   	int ret;
>   
> -	if (!port->cap->pd_revision) {
> +	if (!port->pd_revision) {
>   		dev_dbg(dev, "USB Power Delivery not supported\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -1064,9 +1070,9 @@ static ssize_t power_role_store(struct device *dev,
>   		return ret;
>   
>   	mutex_lock(&port->port_type_lock);
> -	if (port->port_type != TYPEC_PORT_DRP) {
> +	if (port->fixed_role != TYPEC_PORT_DRP) {

This is a semantic change: Previously, it compared the _current_ port type.
Now it compares the initial (fixed) port type. Is this on purpose ?

[ comment written before I noticed the change below. See there. ]

>   		dev_dbg(dev, "port type fixed at \"%s\"",
> -			     typec_port_power_roles[port->port_type]);
> +			     typec_port_power_roles[port->fixed_role]);
>   		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;
>   	}
> @@ -1086,7 +1092,7 @@ static ssize_t power_role_show(struct device *dev,
>   {
>   	struct typec_port *port = to_typec_port(dev);
>   
> -	if (port->cap->type == TYPEC_PORT_DRP)
> +	if (port->fixed_role == TYPEC_PORT_DRP)
>   		return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
>   			       "[source] sink" : "source [sink]");
>   
> @@ -1102,7 +1108,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   	int ret;
>   	enum typec_port_type type;
>   
> -	if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
> +	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
>   		dev_dbg(dev, "changing port type not supported\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -1114,7 +1120,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   	type = ret;
>   	mutex_lock(&port->port_type_lock);
>   
> -	if (port->port_type == type) {
> +	if (port->fixed_role == type) {

This seems wrong.

>   		ret = size;
>   		goto unlock_and_ret;
>   	}
> @@ -1123,7 +1129,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   	if (ret)
>   		goto unlock_and_ret;
>   
> -	port->port_type = type;
> +	port->fixed_role = type;

As does this. It changes the semantics of all checks that used to be against
port->cap->type, except for the one I commented on above. If that is intentional,
the variable name "fixed_role" seems inappropriate.

Overall, I would have thought that "fixed_role" could essentially be a boolean,
set to true if cap->type is not TYPEC_PORT_DRP. That would make the code easier
to understand. Right now I am just confused about the use of port_type vs.
fixed_role.

>   	ret = size;
>   
>   unlock_and_ret:
> @@ -1137,11 +1143,11 @@ port_type_show(struct device *dev, struct device_attribute *attr,
>   {
>   	struct typec_port *port = to_typec_port(dev);
>   
> -	if (port->cap->type == TYPEC_PORT_DRP)
> +	if (port->fixed_role == TYPEC_PORT_DRP)
>   		return sprintf(buf, "%s\n",
> -			       typec_port_types_drp[port->port_type]);
> +			       typec_port_types_drp[port->fixed_role]);
>   
> -	return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
> +	return sprintf(buf, "[%s]\n", typec_port_power_roles[port->fixed_role]);
>   }
>   static DEVICE_ATTR_RW(port_type);
>   
> @@ -1170,7 +1176,7 @@ static ssize_t vconn_source_store(struct device *dev,
>   	bool source;
>   	int ret;
>   
> -	if (!port->cap->pd_revision) {
> +	if (!port->pd_revision) {
>   		dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -1209,10 +1215,10 @@ static ssize_t supported_accessory_modes_show(struct device *dev,
>   	ssize_t ret = 0;
>   	int i;
>   
> -	for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
> -		if (port->cap->accessory[i])
> +	for (i = 0; i < ARRAY_SIZE(port->accessory); i++) {
> +		if (port->accessory[i])
>   			ret += sprintf(buf + ret, "%s ",
> -			       typec_accessory_modes[port->cap->accessory[i]]);
> +			       typec_accessory_modes[port->accessory[i]]);
>   	}
>   
>   	if (!ret)
> @@ -1229,7 +1235,7 @@ static ssize_t usb_typec_revision_show(struct device *dev,
>   				       char *buf)
>   {
>   	struct typec_port *port = to_typec_port(dev);
> -	u16 rev = port->cap->revision;
> +	u16 rev = port->revision;
>   
>   	return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
>   }
> @@ -1241,7 +1247,7 @@ static ssize_t usb_power_delivery_revision_show(struct device *dev,
>   {
>   	struct typec_port *p = to_typec_port(dev);
>   
> -	return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
> +	return sprintf(buf, "%d\n", (p->pd_revision >> 8) & 0xff);
>   }
>   static DEVICE_ATTR_RO(usb_power_delivery_revision);
>   
> @@ -1532,6 +1538,7 @@ struct typec_port *typec_register_port(struct device *parent,
>   	struct typec_port *port;
>   	int ret;
>   	int id;
> +	int i;
>   
>   	port = kzalloc(sizeof(*port), GFP_KERNEL);
>   	if (!port)
> @@ -1581,8 +1588,16 @@ struct typec_port *typec_register_port(struct device *parent,
>   	port->id = id;
>   	port->cap = cap;
>   	port->port_type = cap->type;
> +	port->fixed_role = cap->type;
> +	port->port_roles = cap->data;
>   	port->prefer_role = cap->prefer_role;
>   
> +	port->revision = cap->revision;
> +	port->pd_revision = cap->pd_revision;
> +
> +	for (i = 0; i < TYPEC_MAX_ACCESSORY; i++)
> +		port->accessory[i] = cap->accessory[i];
> +
>   	device_initialize(&port->dev);
>   	port->dev.class = typec_class;
>   	port->dev.parent = parent;
>
Heikki Krogerus Oct. 2, 2019, 4:06 p.m. UTC | #2
On Tue, Oct 01, 2019 at 06:08:17AM -0700, Guenter Roeck wrote:
> On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> > Copying everything from struct typec_capability to struct
> > typec_port during port registration.
> > 
> What is the purpose of this patch ? To reduce the number of indirections at
> runtime, or to avoid having to have cap around ?

To get rid of the cap member.

> FWIW, it looks like the code doesn't copy _all_ variables (eg cap->try_role),
> and it doesn't drop port->cap. Am I missing something ?

We can't drop port->cap at this point because the drivers still depend
on it. This patch is the "prepare" phase of the series. The last patch
in the series finally drops the member. I'll improve the commit message.

> > Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> > ---
> >   drivers/usb/typec/class.c | 55 +++++++++++++++++++++++++--------------
> >   1 file changed, 35 insertions(+), 20 deletions(-)
> > 
> > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> > index 94a3eda62add..3835e2d9fba6 100644
> > --- a/drivers/usb/typec/class.c
> > +++ b/drivers/usb/typec/class.c
> > @@ -46,8 +46,14 @@ struct typec_port {
> >   	enum typec_role			vconn_role;
> >   	enum typec_pwr_opmode		pwr_opmode;
> >   	enum typec_port_type		port_type;
> > +	enum typec_port_type		fixed_role;
> > +	enum typec_port_data		port_roles;
> > +	enum typec_accessory		accessory[TYPEC_MAX_ACCESSORY];
> 
> Would a pointer to cap->accessory be sufficient ? Or is there a reason to copy
> the actual array ?

No. The point is to get rid of the cap member.

> >   	struct mutex			port_type_lock;
> > +	u16				revision;
> > +	u16				pd_revision;
> > +
> >   	enum typec_orientation		orientation;
> >   	struct typec_switch		*sw;
> >   	struct typec_mux		*mux;
> > @@ -950,7 +956,7 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
> >   	int role;
> >   	int ret;
> > -	if (port->cap->type != TYPEC_PORT_DRP) {
> > +	if (port->fixed_role != TYPEC_PORT_DRP) {
> >   		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
> >   		return -EOPNOTSUPP;
> >   	}
> > @@ -982,7 +988,7 @@ preferred_role_show(struct device *dev, struct device_attribute *attr,
> >   {
> >   	struct typec_port *port = to_typec_port(dev);
> > -	if (port->cap->type != TYPEC_PORT_DRP)
> > +	if (port->fixed_role != TYPEC_PORT_DRP)
> >   		return 0;
> >   	if (port->prefer_role < 0)
> > @@ -1009,7 +1015,7 @@ static ssize_t data_role_store(struct device *dev,
> >   		return ret;
> >   	mutex_lock(&port->port_type_lock);
> > -	if (port->cap->data != TYPEC_PORT_DRD) {
> > +	if (port->port_roles != TYPEC_PORT_DRD) {
> >   		ret = -EOPNOTSUPP;
> >   		goto unlock_and_ret;
> >   	}
> > @@ -1029,7 +1035,7 @@ static ssize_t data_role_show(struct device *dev,
> >   {
> >   	struct typec_port *port = to_typec_port(dev);
> > -	if (port->cap->data == TYPEC_PORT_DRD)
> > +	if (port->port_roles == TYPEC_PORT_DRD)
> >   		return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
> >   			       "[host] device" : "host [device]");
> > @@ -1044,7 +1050,7 @@ static ssize_t power_role_store(struct device *dev,
> >   	struct typec_port *port = to_typec_port(dev);
> >   	int ret;
> > -	if (!port->cap->pd_revision) {
> > +	if (!port->pd_revision) {
> >   		dev_dbg(dev, "USB Power Delivery not supported\n");
> >   		return -EOPNOTSUPP;
> >   	}
> > @@ -1064,9 +1070,9 @@ static ssize_t power_role_store(struct device *dev,
> >   		return ret;
> >   	mutex_lock(&port->port_type_lock);
> > -	if (port->port_type != TYPEC_PORT_DRP) {
> > +	if (port->fixed_role != TYPEC_PORT_DRP) {
> 
> This is a semantic change: Previously, it compared the _current_ port type.
> Now it compares the initial (fixed) port type. Is this on purpose ?
> 
> [ comment written before I noticed the change below. See there. ]
> 
> >   		dev_dbg(dev, "port type fixed at \"%s\"",
> > -			     typec_port_power_roles[port->port_type]);
> > +			     typec_port_power_roles[port->fixed_role]);
> >   		ret = -EOPNOTSUPP;
> >   		goto unlock_and_ret;
> >   	}
> > @@ -1086,7 +1092,7 @@ static ssize_t power_role_show(struct device *dev,
> >   {
> >   	struct typec_port *port = to_typec_port(dev);
> > -	if (port->cap->type == TYPEC_PORT_DRP)
> > +	if (port->fixed_role == TYPEC_PORT_DRP)
> >   		return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
> >   			       "[source] sink" : "source [sink]");
> > @@ -1102,7 +1108,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> >   	int ret;
> >   	enum typec_port_type type;
> > -	if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
> > +	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> >   		dev_dbg(dev, "changing port type not supported\n");
> >   		return -EOPNOTSUPP;
> >   	}
> > @@ -1114,7 +1120,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> >   	type = ret;
> >   	mutex_lock(&port->port_type_lock);
> > -	if (port->port_type == type) {
> > +	if (port->fixed_role == type) {
> 
> This seems wrong.
> 
> >   		ret = size;
> >   		goto unlock_and_ret;
> >   	}
> > @@ -1123,7 +1129,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> >   	if (ret)
> >   		goto unlock_and_ret;
> > -	port->port_type = type;
> > +	port->fixed_role = type;
> 
> As does this. It changes the semantics of all checks that used to be against
> port->cap->type, except for the one I commented on above. If that is intentional,
> the variable name "fixed_role" seems inappropriate.
> 
> Overall, I would have thought that "fixed_role" could essentially be a boolean,
> set to true if cap->type is not TYPEC_PORT_DRP. That would make the code easier
> to understand. Right now I am just confused about the use of port_type vs.
> fixed_role.

Because the idea is to get rid of the cap member, I have to store the
actual capability of the port in one member, and the one supplied by
the user in another new member. I chose to use the "port_type" member
to hold the actual capability of the port, and introduced the
"fixed_type" to hold the one given by the user.

I'm happy to improve this, but I'm not sure what are you proposing
here? Note. We can not use boolean variable here, because the user may
also decide to set the value to "dual".

This is a bit off topic, but that attribute file is really horrible.
Right now there is no way to know the actual capability of the
port in user space. If something changes a DRP port into sink or
source, there is no way to know after that that the port is actually
DRP capable.

So that ABI is "buggy", but even without the problem, I still really
think that allowing the capabilities to be changed like that in
general is completely wrong. I don't have a problem with changing the
capabilities, but IMO it should be handled at one level higher, at the
controller device level. If the capabilities of a port need to be
changed, the old port should be removed, and a new with the new
capabilities registered. That is the only way to handle it without
making things unnecessarily difficult for the user space.

I'm pretty sure that that was my counter proposal already at the time
when the attribute file was introduced, but I don't remember why
wasn't it accepted at the time? My protest against adding that
attribute file was in any case ignored :-(. In any case, my plan was
to later propose a new sysfs group that we offer to the parent
controller devices instead assigning it to the port devices, and that
group is meant to allow port capability changes the way I explained
above. Unless of course you are against it?

thanks,
Guenter Roeck Oct. 2, 2019, 4:36 p.m. UTC | #3
On Wed, Oct 02, 2019 at 07:06:52PM +0300, Heikki Krogerus wrote:
> On Tue, Oct 01, 2019 at 06:08:17AM -0700, Guenter Roeck wrote:
> > On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> > > Copying everything from struct typec_capability to struct
> > > typec_port during port registration.
> > > 
> > What is the purpose of this patch ? To reduce the number of indirections at
> > runtime, or to avoid having to have cap around ?
> 
> To get rid of the cap member.
> 
> > FWIW, it looks like the code doesn't copy _all_ variables (eg cap->try_role),
> > and it doesn't drop port->cap. Am I missing something ?
> 
> We can't drop port->cap at this point because the drivers still depend
> on it. This patch is the "prepare" phase of the series. The last patch
> in the series finally drops the member. I'll improve the commit message.
> 
Yes, I figured that with the later patches. Sorry for the noise.

> > > Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> > > ---
> > >   drivers/usb/typec/class.c | 55 +++++++++++++++++++++++++--------------
> > >   1 file changed, 35 insertions(+), 20 deletions(-)
> > > 
> > > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> > > index 94a3eda62add..3835e2d9fba6 100644
> > > --- a/drivers/usb/typec/class.c
> > > +++ b/drivers/usb/typec/class.c
> > > @@ -46,8 +46,14 @@ struct typec_port {
> > >   	enum typec_role			vconn_role;
> > >   	enum typec_pwr_opmode		pwr_opmode;
> > >   	enum typec_port_type		port_type;
> > > +	enum typec_port_type		fixed_role;
> > > +	enum typec_port_data		port_roles;
> > > +	enum typec_accessory		accessory[TYPEC_MAX_ACCESSORY];
> > 
> > Would a pointer to cap->accessory be sufficient ? Or is there a reason to copy
> > the actual array ?
> 
> No. The point is to get rid of the cap member.
> 
> > >   	struct mutex			port_type_lock;
> > > +	u16				revision;
> > > +	u16				pd_revision;
> > > +
> > >   	enum typec_orientation		orientation;
> > >   	struct typec_switch		*sw;
> > >   	struct typec_mux		*mux;
> > > @@ -950,7 +956,7 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
> > >   	int role;
> > >   	int ret;
> > > -	if (port->cap->type != TYPEC_PORT_DRP) {
> > > +	if (port->fixed_role != TYPEC_PORT_DRP) {
> > >   		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
> > >   		return -EOPNOTSUPP;
> > >   	}
> > > @@ -982,7 +988,7 @@ preferred_role_show(struct device *dev, struct device_attribute *attr,
> > >   {
> > >   	struct typec_port *port = to_typec_port(dev);
> > > -	if (port->cap->type != TYPEC_PORT_DRP)
> > > +	if (port->fixed_role != TYPEC_PORT_DRP)
> > >   		return 0;
> > >   	if (port->prefer_role < 0)
> > > @@ -1009,7 +1015,7 @@ static ssize_t data_role_store(struct device *dev,
> > >   		return ret;
> > >   	mutex_lock(&port->port_type_lock);
> > > -	if (port->cap->data != TYPEC_PORT_DRD) {
> > > +	if (port->port_roles != TYPEC_PORT_DRD) {
> > >   		ret = -EOPNOTSUPP;
> > >   		goto unlock_and_ret;
> > >   	}
> > > @@ -1029,7 +1035,7 @@ static ssize_t data_role_show(struct device *dev,
> > >   {
> > >   	struct typec_port *port = to_typec_port(dev);
> > > -	if (port->cap->data == TYPEC_PORT_DRD)
> > > +	if (port->port_roles == TYPEC_PORT_DRD)
> > >   		return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
> > >   			       "[host] device" : "host [device]");
> > > @@ -1044,7 +1050,7 @@ static ssize_t power_role_store(struct device *dev,
> > >   	struct typec_port *port = to_typec_port(dev);
> > >   	int ret;
> > > -	if (!port->cap->pd_revision) {
> > > +	if (!port->pd_revision) {
> > >   		dev_dbg(dev, "USB Power Delivery not supported\n");
> > >   		return -EOPNOTSUPP;
> > >   	}
> > > @@ -1064,9 +1070,9 @@ static ssize_t power_role_store(struct device *dev,
> > >   		return ret;
> > >   	mutex_lock(&port->port_type_lock);
> > > -	if (port->port_type != TYPEC_PORT_DRP) {
> > > +	if (port->fixed_role != TYPEC_PORT_DRP) {
> > 
> > This is a semantic change: Previously, it compared the _current_ port type.
> > Now it compares the initial (fixed) port type. Is this on purpose ?
> > 
> > [ comment written before I noticed the change below. See there. ]
> > 
> > >   		dev_dbg(dev, "port type fixed at \"%s\"",
> > > -			     typec_port_power_roles[port->port_type]);
> > > +			     typec_port_power_roles[port->fixed_role]);
> > >   		ret = -EOPNOTSUPP;
> > >   		goto unlock_and_ret;
> > >   	}
> > > @@ -1086,7 +1092,7 @@ static ssize_t power_role_show(struct device *dev,
> > >   {
> > >   	struct typec_port *port = to_typec_port(dev);
> > > -	if (port->cap->type == TYPEC_PORT_DRP)
> > > +	if (port->fixed_role == TYPEC_PORT_DRP)
> > >   		return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
> > >   			       "[source] sink" : "source [sink]");
> > > @@ -1102,7 +1108,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> > >   	int ret;
> > >   	enum typec_port_type type;
> > > -	if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
> > > +	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> > >   		dev_dbg(dev, "changing port type not supported\n");
> > >   		return -EOPNOTSUPP;
> > >   	}
> > > @@ -1114,7 +1120,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> > >   	type = ret;
> > >   	mutex_lock(&port->port_type_lock);
> > > -	if (port->port_type == type) {
> > > +	if (port->fixed_role == type) {
> > 
> > This seems wrong.
> > 
> > >   		ret = size;
> > >   		goto unlock_and_ret;
> > >   	}
> > > @@ -1123,7 +1129,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> > >   	if (ret)
> > >   		goto unlock_and_ret;
> > > -	port->port_type = type;
> > > +	port->fixed_role = type;
> > 
> > As does this. It changes the semantics of all checks that used to be against
> > port->cap->type, except for the one I commented on above. If that is intentional,
> > the variable name "fixed_role" seems inappropriate.
> > 
> > Overall, I would have thought that "fixed_role" could essentially be a boolean,
> > set to true if cap->type is not TYPEC_PORT_DRP. That would make the code easier
> > to understand. Right now I am just confused about the use of port_type vs.
> > fixed_role.
> 
> Because the idea is to get rid of the cap member, I have to store the
> actual capability of the port in one member, and the one supplied by
> the user in another new member. I chose to use the "port_type" member
> to hold the actual capability of the port, and introduced the
> "fixed_type" to hold the one given by the user.
> 

port->cap->type used to be the role provided by the low level driver.
That was static, and it was not possible to override it. It did not
resemble the current port type, or a configured port type, it resembled
port capabilities.

Your code changes that, meaning even if the low level driver (effectively
the hardware) states that it can not support DRP, it is now configurable
anyway. That seems to me like a substantial change to the original meaning
of this attribute.

Effectiv ely there are now three values,
- port->port_type	the current port tyle
- port->fixed_type	the type selected by the user
- port->cap->type	the type provided by low level code,
			now no longer available / used

Even if the low level driver (hardware) says that it can not support
TYPEC_PORT_DRP, that can be overwritten by the user. Maybe there is a
good reason for that, but I don't see it, sorry.

Maybe it would make sense to introduce port->fixed_type in a separate patch.
As part of that patch you could explain why port->cap->type, ie a reflection
of the port capabilities, is no longer needed.

Thanks,
Guenter
Heikki Krogerus Oct. 2, 2019, 6:29 p.m. UTC | #4
On Wed, Oct 02, 2019 at 09:36:39AM -0700, Guenter Roeck wrote:
> port->cap->type used to be the role provided by the low level driver.
> That was static, and it was not possible to override it. It did not
> resemble the current port type, or a configured port type, it resembled
> port capabilities.
> 
> Your code changes that, meaning even if the low level driver (effectively
> the hardware) states that it can not support DRP, it is now configurable
> anyway. That seems to me like a substantial change to the original meaning
> of this attribute.
> 
> Effectiv ely there are now three values,
> - port->port_type	the current port tyle
> - port->fixed_type	the type selected by the user
> - port->cap->type	the type provided by low level code,
> 			now no longer available / used
> 
> Even if the low level driver (hardware) says that it can not support
> TYPEC_PORT_DRP, that can be overwritten by the user. Maybe there is a
> good reason for that, but I don't see it, sorry.

No, that was not my intention. Clearly there is a bug in my code.

> Maybe it would make sense to introduce port->fixed_type in a separate patch.
> As part of that patch you could explain why port->cap->type, ie a reflection
> of the port capabilities, is no longer needed.

Or, I could make this really simple. I could just copy the content of
the cap as is to another struct typec_capability during port
registration. My goal is really just remove the need for the device
drivers keep the struct typec_capability instance if they don't need
it, and I don't need to remove the cap member to achieve that.

Nevertheless, IMO this attribute file really needs to be deprecated.
On top of making things unnecessarily complicated for the userspace,
it's making it difficult to make changes to the rest of the class
code. We may not be able to get rid of the file, but there is nothing
preventing us from supplying a better solution as an option.

thanks,
Heikki Krogerus Oct. 2, 2019, 7:16 p.m. UTC | #5
On Wed, Oct 02, 2019 at 07:06:55PM +0300, Heikki Krogerus wrote:
> This is a bit off topic, but that attribute file is really horrible.
> Right now there is no way to know the actual capability of the
> port in user space. If something changes a DRP port into sink or
> source, there is no way to know after that that the port is actually
> DRP capable.

That statement is not correct. I'm sorry. I don't know why did I put
it that way.

I wanted to say that now the userpsace is forced to keep track of a
specific sysfs file in order to be sure of the currently supported
role, That alone is wrong, but there is no way to guarantee that
one day we would not need to support another capability in a similar
fashion, and that would mean another sysfs file, and we just forced
the userspace to be modified. The capabilities of the port really need
to be static.

We can handle the capability changes like I propose below without
affecting the userspace.

> So that ABI is "buggy", but even without the problem, I still really
> think that allowing the capabilities to be changed like that in
> general is completely wrong. I don't have a problem with changing the
> capabilities, but IMO it should be handled at one level higher, at the
> controller device level. If the capabilities of a port need to be
> changed, the old port should be removed, and a new with the new
> capabilities registered. That is the only way to handle it without
> making things unnecessarily difficult for the user space.
> 
> I'm pretty sure that that was my counter proposal already at the time
> when the attribute file was introduced, but I don't remember why
> wasn't it accepted at the time? My protest against adding that
> attribute file was in any case ignored :-(. In any case, my plan was
> to later propose a new sysfs group that we offer to the parent
> controller devices instead assigning it to the port devices, and that
> group is meant to allow port capability changes the way I explained
> above. Unless of course you are against it?
> 
> thanks,
> 
> -- 
> heikki
Guenter Roeck Oct. 3, 2019, 3:45 a.m. UTC | #6
On 10/2/19 11:29 AM, Heikki Krogerus wrote:
> On Wed, Oct 02, 2019 at 09:36:39AM -0700, Guenter Roeck wrote:
>> port->cap->type used to be the role provided by the low level driver.
>> That was static, and it was not possible to override it. It did not
>> resemble the current port type, or a configured port type, it resembled
>> port capabilities.
>>
>> Your code changes that, meaning even if the low level driver (effectively
>> the hardware) states that it can not support DRP, it is now configurable
>> anyway. That seems to me like a substantial change to the original meaning
>> of this attribute.
>>
>> Effectiv ely there are now three values,
>> - port->port_type	the current port tyle
>> - port->fixed_type	the type selected by the user
>> - port->cap->type	the type provided by low level code,
>> 			now no longer available / used
>>
>> Even if the low level driver (hardware) says that it can not support
>> TYPEC_PORT_DRP, that can be overwritten by the user. Maybe there is a
>> good reason for that, but I don't see it, sorry.
> 
> No, that was not my intention. Clearly there is a bug in my code.
> 
>> Maybe it would make sense to introduce port->fixed_type in a separate patch.
>> As part of that patch you could explain why port->cap->type, ie a reflection
>> of the port capabilities, is no longer needed.
> 
> Or, I could make this really simple. I could just copy the content of
> the cap as is to another struct typec_capability during port
> registration. My goal is really just remove the need for the device
> drivers keep the struct typec_capability instance if they don't need
> it, and I don't need to remove the cap member to achieve that.
> 

Maybe just use devm_kmemdup() ?

Guenter

> Nevertheless, IMO this attribute file really needs to be deprecated.
> On top of making things unnecessarily complicated for the userspace,
> it's making it difficult to make changes to the rest of the class
> code. We may not be able to get rid of the file, but there is nothing
> preventing us from supplying a better solution as an option.
>
Guenter Roeck Oct. 3, 2019, 3:51 a.m. UTC | #7
On 10/2/19 12:16 PM, Heikki Krogerus wrote:
> On Wed, Oct 02, 2019 at 07:06:55PM +0300, Heikki Krogerus wrote:
>> This is a bit off topic, but that attribute file is really horrible.
>> Right now there is no way to know the actual capability of the
>> port in user space. If something changes a DRP port into sink or
>> source, there is no way to know after that that the port is actually
>> DRP capable.
> 
> That statement is not correct. I'm sorry. I don't know why did I put
> it that way.
> 
> I wanted to say that now the userpsace is forced to keep track of a
> specific sysfs file in order to be sure of the currently supported
> role, That alone is wrong, but there is no way to guarantee that
> one day we would not need to support another capability in a similar
> fashion, and that would mean another sysfs file, and we just forced
> the userspace to be modified. The capabilities of the port really need
> to be static.
> 

I assume you refer to port_type. If I remember correctly, this is actually
used by Android userspace code to specifically select if a port can be
source, sink, or drp. I am not sure if it is a good idea to enforce
port removal and re-registration in conjunction with this - the port
can, after all, be connected to some storage device or to a monitor.
I am not sure how we could "sell" to users the idea that if they change
the port type, their screen will go dark for a few seconds.

Am I missing something ?

Thanks,
Guenter

> We can handle the capability changes like I propose below without
> affecting the userspace.
> 
>> So that ABI is "buggy", but even without the problem, I still really
>> think that allowing the capabilities to be changed like that in
>> general is completely wrong. I don't have a problem with changing the
>> capabilities, but IMO it should be handled at one level higher, at the
>> controller device level. If the capabilities of a port need to be
>> changed, the old port should be removed, and a new with the new
>> capabilities registered. That is the only way to handle it without
>> making things unnecessarily difficult for the user space.
>>
>> I'm pretty sure that that was my counter proposal already at the time
>> when the attribute file was introduced, but I don't remember why
>> wasn't it accepted at the time? My protest against adding that
>> attribute file was in any case ignored :-(. In any case, my plan was
>> to later propose a new sysfs group that we offer to the parent
>> controller devices instead assigning it to the port devices, and that
>> group is meant to allow port capability changes the way I explained
>> above. Unless of course you are against it?
>>
>> thanks,
>>
>> -- 
>> heikki
>
Heikki Krogerus Oct. 3, 2019, 8:03 a.m. UTC | #8
On Wed, Oct 02, 2019 at 08:45:28PM -0700, Guenter Roeck wrote:
> On 10/2/19 11:29 AM, Heikki Krogerus wrote:
> > On Wed, Oct 02, 2019 at 09:36:39AM -0700, Guenter Roeck wrote:
> > > port->cap->type used to be the role provided by the low level driver.
> > > That was static, and it was not possible to override it. It did not
> > > resemble the current port type, or a configured port type, it resembled
> > > port capabilities.
> > > 
> > > Your code changes that, meaning even if the low level driver (effectively
> > > the hardware) states that it can not support DRP, it is now configurable
> > > anyway. That seems to me like a substantial change to the original meaning
> > > of this attribute.
> > > 
> > > Effectiv ely there are now three values,
> > > - port->port_type	the current port tyle
> > > - port->fixed_type	the type selected by the user
> > > - port->cap->type	the type provided by low level code,
> > > 			now no longer available / used
> > > 
> > > Even if the low level driver (hardware) says that it can not support
> > > TYPEC_PORT_DRP, that can be overwritten by the user. Maybe there is a
> > > good reason for that, but I don't see it, sorry.
> > 
> > No, that was not my intention. Clearly there is a bug in my code.
> > 
> > > Maybe it would make sense to introduce port->fixed_type in a separate patch.
> > > As part of that patch you could explain why port->cap->type, ie a reflection
> > > of the port capabilities, is no longer needed.
> > 
> > Or, I could make this really simple. I could just copy the content of
> > the cap as is to another struct typec_capability during port
> > registration. My goal is really just remove the need for the device
> > drivers keep the struct typec_capability instance if they don't need
> > it, and I don't need to remove the cap member to achieve that.
> > 
> 
> Maybe just use devm_kmemdup() ?

For example.

thanks,
Heikki Krogerus Oct. 3, 2019, 1:29 p.m. UTC | #9
On Wed, Oct 02, 2019 at 08:51:32PM -0700, Guenter Roeck wrote:
> On 10/2/19 12:16 PM, Heikki Krogerus wrote:
> > On Wed, Oct 02, 2019 at 07:06:55PM +0300, Heikki Krogerus wrote:
> > > This is a bit off topic, but that attribute file is really horrible.
> > > Right now there is no way to know the actual capability of the
> > > port in user space. If something changes a DRP port into sink or
> > > source, there is no way to know after that that the port is actually
> > > DRP capable.
> > 
> > That statement is not correct. I'm sorry. I don't know why did I put
> > it that way.
> > 
> > I wanted to say that now the userpsace is forced to keep track of a
> > specific sysfs file in order to be sure of the currently supported
> > role, That alone is wrong, but there is no way to guarantee that
> > one day we would not need to support another capability in a similar
> > fashion, and that would mean another sysfs file, and we just forced
> > the userspace to be modified. The capabilities of the port really need
> > to be static.
> > 
> 
> I assume you refer to port_type. If I remember correctly, this is actually
> used by Android userspace code to specifically select if a port can be
> source, sink, or drp. I am not sure if it is a good idea to enforce
> port removal and re-registration in conjunction with this - the port
> can, after all, be connected to some storage device or to a monitor.
> I am not sure how we could "sell" to users the idea that if they change
> the port type, their screen will go dark for a few seconds.
> 
> Am I missing something ?

I'm not sure how can you avoid flickering screen even with the current
port_type sysfs flag? If you change the port type, you will reset the
port, which means you have to also re-enter the altmode again, no? If
so, then does unregistering and registering the ports actually make
that much difference from the users perspective?

But why do you need the supported roles to be changed when there is
already a partner device plugged to the connector? What is the
use-case/requirement here?

One note about my proposal: With my proposal the userspace can be
given the possibility to influence the port capabilities even before
the ports have been registered. I would imagine that should cover at
least some of the requirements.

The major problem here is that we can not guarantee how the ports
behave if the supported roles are changed when a device is already
plugged to the connector (don't forget, we are not always using
tcpm.c). With UCSI at least it really depends on the underlying
firmware implementation. It also creates risks for the user, like
writing "sink" to that port_type and ending up with a black screen,
and I mean permanently black screen. Un-plugging and re-plugging the
monitor back will not help. Only changing the port_type again, or
reboot work.

Ideally we should not allow the capabilities to be changed after the
partner device is already registered at all, but if we really have to
allow it, then I still think we should do it the way I proposed.

thanks,

> Thanks,
> Guenter
> 
> > We can handle the capability changes like I propose below without
> > affecting the userspace.
> > 
> > > So that ABI is "buggy", but even without the problem, I still really
> > > think that allowing the capabilities to be changed like that in
> > > general is completely wrong. I don't have a problem with changing the
> > > capabilities, but IMO it should be handled at one level higher, at the
> > > controller device level. If the capabilities of a port need to be
> > > changed, the old port should be removed, and a new with the new
> > > capabilities registered. That is the only way to handle it without
> > > making things unnecessarily difficult for the user space.
> > > 
> > > I'm pretty sure that that was my counter proposal already at the time
> > > when the attribute file was introduced, but I don't remember why
> > > wasn't it accepted at the time? My protest against adding that
> > > attribute file was in any case ignored :-(. In any case, my plan was
> > > to later propose a new sysfs group that we offer to the parent
> > > controller devices instead assigning it to the port devices, and that
> > > group is meant to allow port capability changes the way I explained
> > > above. Unless of course you are against it?
> > > 
> > > thanks,
> > > 
> > > -- 
> > > heikki
> >
diff mbox series

Patch

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 94a3eda62add..3835e2d9fba6 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -46,8 +46,14 @@  struct typec_port {
 	enum typec_role			vconn_role;
 	enum typec_pwr_opmode		pwr_opmode;
 	enum typec_port_type		port_type;
+	enum typec_port_type		fixed_role;
+	enum typec_port_data		port_roles;
+	enum typec_accessory		accessory[TYPEC_MAX_ACCESSORY];
 	struct mutex			port_type_lock;
 
+	u16				revision;
+	u16				pd_revision;
+
 	enum typec_orientation		orientation;
 	struct typec_switch		*sw;
 	struct typec_mux		*mux;
@@ -950,7 +956,7 @@  preferred_role_store(struct device *dev, struct device_attribute *attr,
 	int role;
 	int ret;
 
-	if (port->cap->type != TYPEC_PORT_DRP) {
+	if (port->fixed_role != TYPEC_PORT_DRP) {
 		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
 		return -EOPNOTSUPP;
 	}
@@ -982,7 +988,7 @@  preferred_role_show(struct device *dev, struct device_attribute *attr,
 {
 	struct typec_port *port = to_typec_port(dev);
 
-	if (port->cap->type != TYPEC_PORT_DRP)
+	if (port->fixed_role != TYPEC_PORT_DRP)
 		return 0;
 
 	if (port->prefer_role < 0)
@@ -1009,7 +1015,7 @@  static ssize_t data_role_store(struct device *dev,
 		return ret;
 
 	mutex_lock(&port->port_type_lock);
-	if (port->cap->data != TYPEC_PORT_DRD) {
+	if (port->port_roles != TYPEC_PORT_DRD) {
 		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
 	}
@@ -1029,7 +1035,7 @@  static ssize_t data_role_show(struct device *dev,
 {
 	struct typec_port *port = to_typec_port(dev);
 
-	if (port->cap->data == TYPEC_PORT_DRD)
+	if (port->port_roles == TYPEC_PORT_DRD)
 		return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
 			       "[host] device" : "host [device]");
 
@@ -1044,7 +1050,7 @@  static ssize_t power_role_store(struct device *dev,
 	struct typec_port *port = to_typec_port(dev);
 	int ret;
 
-	if (!port->cap->pd_revision) {
+	if (!port->pd_revision) {
 		dev_dbg(dev, "USB Power Delivery not supported\n");
 		return -EOPNOTSUPP;
 	}
@@ -1064,9 +1070,9 @@  static ssize_t power_role_store(struct device *dev,
 		return ret;
 
 	mutex_lock(&port->port_type_lock);
-	if (port->port_type != TYPEC_PORT_DRP) {
+	if (port->fixed_role != TYPEC_PORT_DRP) {
 		dev_dbg(dev, "port type fixed at \"%s\"",
-			     typec_port_power_roles[port->port_type]);
+			     typec_port_power_roles[port->fixed_role]);
 		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
 	}
@@ -1086,7 +1092,7 @@  static ssize_t power_role_show(struct device *dev,
 {
 	struct typec_port *port = to_typec_port(dev);
 
-	if (port->cap->type == TYPEC_PORT_DRP)
+	if (port->fixed_role == TYPEC_PORT_DRP)
 		return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
 			       "[source] sink" : "source [sink]");
 
@@ -1102,7 +1108,7 @@  port_type_store(struct device *dev, struct device_attribute *attr,
 	int ret;
 	enum typec_port_type type;
 
-	if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
+	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
 		dev_dbg(dev, "changing port type not supported\n");
 		return -EOPNOTSUPP;
 	}
@@ -1114,7 +1120,7 @@  port_type_store(struct device *dev, struct device_attribute *attr,
 	type = ret;
 	mutex_lock(&port->port_type_lock);
 
-	if (port->port_type == type) {
+	if (port->fixed_role == type) {
 		ret = size;
 		goto unlock_and_ret;
 	}
@@ -1123,7 +1129,7 @@  port_type_store(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		goto unlock_and_ret;
 
-	port->port_type = type;
+	port->fixed_role = type;
 	ret = size;
 
 unlock_and_ret:
@@ -1137,11 +1143,11 @@  port_type_show(struct device *dev, struct device_attribute *attr,
 {
 	struct typec_port *port = to_typec_port(dev);
 
-	if (port->cap->type == TYPEC_PORT_DRP)
+	if (port->fixed_role == TYPEC_PORT_DRP)
 		return sprintf(buf, "%s\n",
-			       typec_port_types_drp[port->port_type]);
+			       typec_port_types_drp[port->fixed_role]);
 
-	return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
+	return sprintf(buf, "[%s]\n", typec_port_power_roles[port->fixed_role]);
 }
 static DEVICE_ATTR_RW(port_type);
 
@@ -1170,7 +1176,7 @@  static ssize_t vconn_source_store(struct device *dev,
 	bool source;
 	int ret;
 
-	if (!port->cap->pd_revision) {
+	if (!port->pd_revision) {
 		dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
 		return -EOPNOTSUPP;
 	}
@@ -1209,10 +1215,10 @@  static ssize_t supported_accessory_modes_show(struct device *dev,
 	ssize_t ret = 0;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
-		if (port->cap->accessory[i])
+	for (i = 0; i < ARRAY_SIZE(port->accessory); i++) {
+		if (port->accessory[i])
 			ret += sprintf(buf + ret, "%s ",
-			       typec_accessory_modes[port->cap->accessory[i]]);
+			       typec_accessory_modes[port->accessory[i]]);
 	}
 
 	if (!ret)
@@ -1229,7 +1235,7 @@  static ssize_t usb_typec_revision_show(struct device *dev,
 				       char *buf)
 {
 	struct typec_port *port = to_typec_port(dev);
-	u16 rev = port->cap->revision;
+	u16 rev = port->revision;
 
 	return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
 }
@@ -1241,7 +1247,7 @@  static ssize_t usb_power_delivery_revision_show(struct device *dev,
 {
 	struct typec_port *p = to_typec_port(dev);
 
-	return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
+	return sprintf(buf, "%d\n", (p->pd_revision >> 8) & 0xff);
 }
 static DEVICE_ATTR_RO(usb_power_delivery_revision);
 
@@ -1532,6 +1538,7 @@  struct typec_port *typec_register_port(struct device *parent,
 	struct typec_port *port;
 	int ret;
 	int id;
+	int i;
 
 	port = kzalloc(sizeof(*port), GFP_KERNEL);
 	if (!port)
@@ -1581,8 +1588,16 @@  struct typec_port *typec_register_port(struct device *parent,
 	port->id = id;
 	port->cap = cap;
 	port->port_type = cap->type;
+	port->fixed_role = cap->type;
+	port->port_roles = cap->data;
 	port->prefer_role = cap->prefer_role;
 
+	port->revision = cap->revision;
+	port->pd_revision = cap->pd_revision;
+
+	for (i = 0; i < TYPEC_MAX_ACCESSORY; i++)
+		port->accessory[i] = cap->accessory[i];
+
 	device_initialize(&port->dev);
 	port->dev.class = typec_class;
 	port->dev.parent = parent;