Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 0/7] usb: typec: Small API improvement
@ 2019-10-01  9:48 Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration Heikki Krogerus
                   ` (6 more replies)
  0 siblings, 7 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Hi guys,

This series moves the callback members from struct typec_capabilities
to a new struct typec_operations. That removes the need for the
drivers to keep a copy of the struct typec_capabilites if they don't
need it, and struct typec_operations can probable always be
constified.

The change is small, however I think the code ends up being a bit more
cleaner looking this way. Let me know if it's OK.

thanks,

Heikki Krogerus (7):
  usb: typec: Copy everything from struct typec_capability during
    registration
  usb: typec: Introduce typec_get_drvdata()
  usb: typec: Separate the operations vector
  usb: typec: tcpm: Start using struct typec_operations
  usb: typec: tps6598x: Start using struct typec_operations
  usb: typec: ucsi: Start using struct typec_operations
  usb: typec: Remove the callback members from struct typec_capability

 drivers/usb/typec/class.c     | 125 +++++++++++++++++++++-------------
 drivers/usb/typec/tcpm/tcpm.c |  47 ++++++-------
 drivers/usb/typec/tps6598x.c  |  49 ++++++-------
 drivers/usb/typec/ucsi/ucsi.c |  22 +++---
 include/linux/usb/typec.h     |  38 ++++++-----
 5 files changed, 157 insertions(+), 124 deletions(-)

-- 
2.23.0


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

* [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
@ 2019-10-01  9:48 ` Heikki Krogerus
  2019-10-01 13:08   ` Guenter Roeck
  2019-10-01  9:48 ` [PATCH 2/7] usb: typec: Introduce typec_get_drvdata() Heikki Krogerus
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

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(-)

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;
-- 
2.23.0


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

* [PATCH 2/7] usb: typec: Introduce typec_get_drvdata()
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration Heikki Krogerus
@ 2019-10-01  9:48 ` Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 3/7] usb: typec: Separate the operations vector Heikki Krogerus
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Leaving the private driver_data pointer of the port device
to the port drivers.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/class.c | 11 +++++++++++
 include/linux/usb/typec.h |  4 ++++
 2 files changed, 15 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 3835e2d9fba6..9fab0be8f08c 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -1492,6 +1492,16 @@ EXPORT_SYMBOL_GPL(typec_set_mode);
 
 /* --------------------------------------- */
 
+/**
+ * typec_get_drvdata - Return private driver data pointer
+ * @port: USB Type-C port
+ */
+void *typec_get_drvdata(struct typec_port *port)
+{
+	return dev_get_drvdata(&port->dev);
+}
+EXPORT_SYMBOL_GPL(typec_get_drvdata);
+
 /**
  * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
  * @port: USB Type-C Port that supports the alternate mode
@@ -1604,6 +1614,7 @@ struct typec_port *typec_register_port(struct device *parent,
 	port->dev.fwnode = cap->fwnode;
 	port->dev.type = &typec_port_dev_type;
 	dev_set_name(&port->dev, "port%d", id);
+	dev_set_drvdata(&port->dev, cap->driver_data);
 
 	port->sw = typec_switch_get(&port->dev);
 	if (IS_ERR(port->sw)) {
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 7df4ecabc78a..8b90cd77331c 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -179,6 +179,7 @@ struct typec_partner_desc {
  * @sw: Cable plug orientation switch
  * @mux: Multiplexer switch for Alternate/Accessory Modes
  * @fwnode: Optional fwnode of the port
+ * @driver_data: Private pointer for driver specific info
  * @try_role: Set data role preference for DRP port
  * @dr_set: Set Data Role
  * @pr_set: Set Power Role
@@ -198,6 +199,7 @@ struct typec_capability {
 	struct typec_switch	*sw;
 	struct typec_mux	*mux;
 	struct fwnode_handle	*fwnode;
+	void			*driver_data;
 
 	int		(*try_role)(const struct typec_capability *,
 				    int role);
@@ -241,6 +243,8 @@ int typec_set_orientation(struct typec_port *port,
 enum typec_orientation typec_get_orientation(struct typec_port *port);
 int typec_set_mode(struct typec_port *port, int mode);
 
+void *typec_get_drvdata(struct typec_port *port);
+
 int typec_find_port_power_role(const char *name);
 int typec_find_power_role(const char *name);
 int typec_find_port_data_role(const char *name);
-- 
2.23.0


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

* [PATCH 3/7] usb: typec: Separate the operations vector
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 2/7] usb: typec: Introduce typec_get_drvdata() Heikki Krogerus
@ 2019-10-01  9:48 ` Heikki Krogerus
  2019-10-01 13:22   ` Guenter Roeck
  2019-10-01  9:48 ` [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations Heikki Krogerus
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Introducing struct typec_operations which has the same
callbacks as struct typec_capability. The old callbacks are
kept for now, but after all users have been converted, they
will be removed.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/class.c | 90 +++++++++++++++++++++++++--------------
 include/linux/usb/typec.h | 19 +++++++++
 2 files changed, 76 insertions(+), 33 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 9fab0be8f08c..542be63795db 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -59,6 +59,7 @@ struct typec_port {
 	struct typec_mux		*mux;
 
 	const struct typec_capability	*cap;
+	const struct typec_operations	*ops;
 };
 
 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
@@ -961,11 +962,6 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
 		return -EOPNOTSUPP;
 	}
 
-	if (!port->cap->try_role) {
-		dev_dbg(dev, "Setting preferred role not supported\n");
-		return -EOPNOTSUPP;
-	}
-
 	role = sysfs_match_string(typec_roles, buf);
 	if (role < 0) {
 		if (sysfs_streq(buf, "none"))
@@ -974,9 +970,18 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
 			return -EINVAL;
 	}
 
-	ret = port->cap->try_role(port->cap, role);
-	if (ret)
-		return ret;
+	if (port->ops && port->ops->try_role) {
+		ret = port->ops->try_role(port, role);
+		if (ret)
+			return ret;
+	} else if (port->cap && port->cap->try_role) {
+		ret = port->cap->try_role(port->cap, role);
+		if (ret)
+			return ret;
+	} else {
+		dev_dbg(dev, "Setting preferred role not supported\n");
+		return -EOPNOTSUPP;
+	}
 
 	port->prefer_role = role;
 	return size;
@@ -1005,11 +1010,6 @@ static ssize_t data_role_store(struct device *dev,
 	struct typec_port *port = to_typec_port(dev);
 	int ret;
 
-	if (!port->cap->dr_set) {
-		dev_dbg(dev, "data role swapping not supported\n");
-		return -EOPNOTSUPP;
-	}
-
 	ret = sysfs_match_string(typec_data_roles, buf);
 	if (ret < 0)
 		return ret;
@@ -1020,9 +1020,19 @@ static ssize_t data_role_store(struct device *dev,
 		goto unlock_and_ret;
 	}
 
-	ret = port->cap->dr_set(port->cap, ret);
-	if (ret)
+	if (port->ops && port->ops->dr_set) {
+		ret = port->ops->dr_set(port, ret);
+		if (ret)
+			goto unlock_and_ret;
+	} else if (port->cap && port->cap->dr_set) {
+		ret = port->cap->dr_set(port->cap, ret);
+		if (ret)
+			goto unlock_and_ret;
+	} else {
+		dev_dbg(dev, "data role swapping not supported\n");
+		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
+	}
 
 	ret = size;
 unlock_and_ret:
@@ -1055,11 +1065,6 @@ static ssize_t power_role_store(struct device *dev,
 		return -EOPNOTSUPP;
 	}
 
-	if (!port->cap->pr_set) {
-		dev_dbg(dev, "power role swapping not supported\n");
-		return -EOPNOTSUPP;
-	}
-
 	if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
 		dev_dbg(dev, "partner unable to swap power role\n");
 		return -EIO;
@@ -1077,11 +1082,21 @@ static ssize_t power_role_store(struct device *dev,
 		goto unlock_and_ret;
 	}
 
-	ret = port->cap->pr_set(port->cap, ret);
-	if (ret)
+	if (port->ops && port->ops->pr_set) {
+		ret = port->ops->pr_set(port, ret);
+		if (ret)
+			goto unlock_and_ret;
+	} else if (port->cap && port->cap->pr_set) {
+		ret = port->cap->pr_set(port->cap, ret);
+		if (ret)
+			goto unlock_and_ret;
+	} else {
+		dev_dbg(dev, "power role swapping not supported\n");
+		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
-
+	}
 	ret = size;
+
 unlock_and_ret:
 	mutex_unlock(&port->port_type_lock);
 	return ret;
@@ -1108,7 +1123,8 @@ port_type_store(struct device *dev, struct device_attribute *attr,
 	int ret;
 	enum typec_port_type type;
 
-	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
+	if ((!port->ops || !port->ops->port_type_set) ||
+	    !port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
 		dev_dbg(dev, "changing port type not supported\n");
 		return -EOPNOTSUPP;
 	}
@@ -1125,7 +1141,10 @@ port_type_store(struct device *dev, struct device_attribute *attr,
 		goto unlock_and_ret;
 	}
 
-	ret = port->cap->port_type_set(port->cap, type);
+	if (port->ops && port->ops->port_type_set)
+		ret = port->ops->port_type_set(port, type);
+	else
+		ret = port->cap->port_type_set(port->cap, type);
 	if (ret)
 		goto unlock_and_ret;
 
@@ -1181,18 +1200,22 @@ static ssize_t vconn_source_store(struct device *dev,
 		return -EOPNOTSUPP;
 	}
 
-	if (!port->cap->vconn_set) {
-		dev_dbg(dev, "VCONN swapping not supported\n");
-		return -EOPNOTSUPP;
-	}
-
 	ret = kstrtobool(buf, &source);
 	if (ret)
 		return ret;
 
-	ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
-	if (ret)
-		return ret;
+	if (port->ops && port->ops->vconn_set) {
+		ret = port->ops->vconn_set(port, source);
+		if (ret)
+			return ret;
+	} else if (port->cap && port->cap->vconn_set) {
+		ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
+		if (ret)
+			return ret;
+	} else {
+		dev_dbg(dev, "VCONN swapping not supported\n");
+		return -EOPNOTSUPP;
+	}
 
 	return size;
 }
@@ -1597,6 +1620,7 @@ struct typec_port *typec_register_port(struct device *parent,
 
 	port->id = id;
 	port->cap = cap;
+	port->ops = cap->ops;
 	port->port_type = cap->type;
 	port->fixed_role = cap->type;
 	port->port_roles = cap->data;
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 8b90cd77331c..6c95a9ff43c6 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -168,6 +168,22 @@ struct typec_partner_desc {
 	struct usb_pd_identity	*identity;
 };
 
+/*
+ * struct typec_operations - USB Type-C Port Operations
+ * @try_role: Set data role preference for DRP port
+ * @dr_set: Set Data Role
+ * @pr_set: Set Power Role
+ * @vconn_set: Source VCONN
+ * @port_type_set: Set port type
+ */
+struct typec_operations {
+	int (*try_role)(struct typec_port *port, int role);
+	int (*dr_set)(struct typec_port *port, enum typec_data_role);
+	int (*pr_set)(struct typec_port *port, enum typec_role);
+	int (*vconn_set)(struct typec_port *port, bool source);
+	int (*port_type_set)(struct typec_port *port, enum typec_port_type);
+};
+
 /*
  * struct typec_capability - USB Type-C Port Capabilities
  * @type: Supported power role of the port
@@ -180,6 +196,7 @@ struct typec_partner_desc {
  * @mux: Multiplexer switch for Alternate/Accessory Modes
  * @fwnode: Optional fwnode of the port
  * @driver_data: Private pointer for driver specific info
+ * @ops: Port operations vector
  * @try_role: Set data role preference for DRP port
  * @dr_set: Set Data Role
  * @pr_set: Set Power Role
@@ -201,6 +218,8 @@ struct typec_capability {
 	struct fwnode_handle	*fwnode;
 	void			*driver_data;
 
+	const struct typec_operations	*ops;
+
 	int		(*try_role)(const struct typec_capability *,
 				    int role);
 
-- 
2.23.0


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

* [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
                   ` (2 preceding siblings ...)
  2019-10-01  9:48 ` [PATCH 3/7] usb: typec: Separate the operations vector Heikki Krogerus
@ 2019-10-01  9:48 ` Heikki Krogerus
  2019-10-01 13:30   ` Guenter Roeck
  2019-10-01  9:48 ` [PATCH 5/7] usb: typec: tps6598x: " Heikki Krogerus
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Supplying the operation callbacks as part of a struct
typec_operations instead of as part of struct
typec_capability during port registration.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/tcpm/tcpm.c | 47 ++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 26 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 96562744101c..0fde7e042d5f 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -390,12 +390,6 @@ static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
 	return SRC_UNATTACHED;
 }
 
-static inline
-struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
-{
-	return container_of(cap, struct tcpm_port, typec_caps);
-}
-
 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
 {
 	return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
@@ -3970,10 +3964,9 @@ void tcpm_pd_hard_reset(struct tcpm_port *port)
 }
 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
 
-static int tcpm_dr_set(const struct typec_capability *cap,
-		       enum typec_data_role data)
+static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
 {
-	struct tcpm_port *port = typec_cap_to_tcpm(cap);
+	struct tcpm_port *port = typec_get_drvdata(p);
 	int ret;
 
 	mutex_lock(&port->swap_lock);
@@ -4038,10 +4031,9 @@ static int tcpm_dr_set(const struct typec_capability *cap,
 	return ret;
 }
 
-static int tcpm_pr_set(const struct typec_capability *cap,
-		       enum typec_role role)
+static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
 {
-	struct tcpm_port *port = typec_cap_to_tcpm(cap);
+	struct tcpm_port *port = typec_get_drvdata(p);
 	int ret;
 
 	mutex_lock(&port->swap_lock);
@@ -4082,10 +4074,9 @@ static int tcpm_pr_set(const struct typec_capability *cap,
 	return ret;
 }
 
-static int tcpm_vconn_set(const struct typec_capability *cap,
-			  enum typec_role role)
+static int tcpm_vconn_set(struct typec_port *p, bool source)
 {
-	struct tcpm_port *port = typec_cap_to_tcpm(cap);
+	struct tcpm_port *port = typec_get_drvdata(p);
 	int ret;
 
 	mutex_lock(&port->swap_lock);
@@ -4096,7 +4087,7 @@ static int tcpm_vconn_set(const struct typec_capability *cap,
 		goto port_unlock;
 	}
 
-	if (role == port->vconn_role) {
+	if (source == port->vconn_role) {
 		ret = 0;
 		goto port_unlock;
 	}
@@ -4122,9 +4113,9 @@ static int tcpm_vconn_set(const struct typec_capability *cap,
 	return ret;
 }
 
-static int tcpm_try_role(const struct typec_capability *cap, int role)
+static int tcpm_try_role(struct typec_port *p, int role)
 {
-	struct tcpm_port *port = typec_cap_to_tcpm(cap);
+	struct tcpm_port *port = typec_get_drvdata(p);
 	struct tcpc_dev	*tcpc = port->tcpc;
 	int ret = 0;
 
@@ -4331,10 +4322,9 @@ static void tcpm_init(struct tcpm_port *port)
 	tcpm_set_state(port, PORT_RESET, 0);
 }
 
-static int tcpm_port_type_set(const struct typec_capability *cap,
-			      enum typec_port_type type)
+static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
 {
-	struct tcpm_port *port = typec_cap_to_tcpm(cap);
+	struct tcpm_port *port = typec_get_drvdata(p);
 
 	mutex_lock(&port->lock);
 	if (type == port->port_type)
@@ -4359,6 +4349,14 @@ static int tcpm_port_type_set(const struct typec_capability *cap,
 	return 0;
 }
 
+static const struct typec_operations tcpm_ops = {
+	.try_role = tcpm_try_role,
+	.dr_set = tcpm_dr_set,
+	.pr_set = tcpm_pr_set,
+	.vconn_set = tcpm_vconn_set,
+	.port_type_set = tcpm_port_type_set
+};
+
 void tcpm_tcpc_reset(struct tcpm_port *port)
 {
 	mutex_lock(&port->lock);
@@ -4770,11 +4768,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 	port->typec_caps.fwnode = tcpc->fwnode;
 	port->typec_caps.revision = 0x0120;	/* Type-C spec release 1.2 */
 	port->typec_caps.pd_revision = 0x0300;	/* USB-PD spec release 3.0 */
-	port->typec_caps.dr_set = tcpm_dr_set;
-	port->typec_caps.pr_set = tcpm_pr_set;
-	port->typec_caps.vconn_set = tcpm_vconn_set;
-	port->typec_caps.try_role = tcpm_try_role;
-	port->typec_caps.port_type_set = tcpm_port_type_set;
+	port->typec_caps.driver_data = port;
+	port->typec_caps.ops = &tcpm_ops;
 
 	port->partner_desc.identity = &port->partner_ident;
 	port->port_type = port->typec_caps.type;
-- 
2.23.0


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

* [PATCH 5/7] usb: typec: tps6598x: Start using struct typec_operations
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
                   ` (3 preceding siblings ...)
  2019-10-01  9:48 ` [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations Heikki Krogerus
@ 2019-10-01  9:48 ` " Heikki Krogerus
  2019-10-01 13:34   ` Guenter Roeck
  2019-10-01 13:35   ` Guenter Roeck
  2019-10-01  9:48 ` [PATCH 6/7] usb: typec: ucsi: " Heikki Krogerus
  2019-10-01  9:48 ` [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability Heikki Krogerus
  6 siblings, 2 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Supplying the operation callbacks as part of a struct
typec_operations instead of as part of struct
typec_capability during port registration. After this there
is not need to keep the capabilities stored anywhere in the
driver.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/tps6598x.c | 49 +++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 23 deletions(-)

diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
index a38d1409f15b..0698addd1185 100644
--- a/drivers/usb/typec/tps6598x.c
+++ b/drivers/usb/typec/tps6598x.c
@@ -94,7 +94,6 @@ struct tps6598x {
 	struct typec_port *port;
 	struct typec_partner *partner;
 	struct usb_pd_identity partner_identity;
-	struct typec_capability typec_cap;
 };
 
 /*
@@ -307,11 +306,10 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
 	return 0;
 }
 
-static int
-tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
+static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
 {
-	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
 	const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
+	struct tps6598x *tps = typec_get_drvdata(port);
 	u32 status;
 	int ret;
 
@@ -338,11 +336,10 @@ tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
 	return ret;
 }
 
-static int
-tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
+static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
 {
-	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
 	const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
+	struct tps6598x *tps = typec_get_drvdata(port);
 	u32 status;
 	int ret;
 
@@ -369,6 +366,11 @@ tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
 	return ret;
 }
 
+static const struct typec_operations tps6598x_ops = {
+	.dr_set = tps6598x_dr_set,
+	.pr_set = tps6598x_pr_set,
+};
+
 static irqreturn_t tps6598x_interrupt(int irq, void *data)
 {
 	struct tps6598x *tps = data;
@@ -448,6 +450,7 @@ static const struct regmap_config tps6598x_regmap_config = {
 
 static int tps6598x_probe(struct i2c_client *client)
 {
+	struct typec_capability typec_cap = { };
 	struct tps6598x *tps;
 	u32 status;
 	u32 conf;
@@ -492,40 +495,40 @@ static int tps6598x_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
-	tps->typec_cap.revision = USB_TYPEC_REV_1_2;
-	tps->typec_cap.pd_revision = 0x200;
-	tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
-	tps->typec_cap.pr_set = tps6598x_pr_set;
-	tps->typec_cap.dr_set = tps6598x_dr_set;
+	typec_cap.revision = USB_TYPEC_REV_1_2;
+	typec_cap.pd_revision = 0x200;
+	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
+	typec_cap.driver_data = tps;
+	typec_cap.ops = &tps6598x_ops;
 
 	switch (TPS_SYSCONF_PORTINFO(conf)) {
 	case TPS_PORTINFO_SINK_ACCESSORY:
 	case TPS_PORTINFO_SINK:
-		tps->typec_cap.type = TYPEC_PORT_SNK;
-		tps->typec_cap.data = TYPEC_PORT_UFP;
+		typec_cap.type = TYPEC_PORT_SNK;
+		typec_cap.data = TYPEC_PORT_UFP;
 		break;
 	case TPS_PORTINFO_DRP_UFP_DRD:
 	case TPS_PORTINFO_DRP_DFP_DRD:
-		tps->typec_cap.type = TYPEC_PORT_DRP;
-		tps->typec_cap.data = TYPEC_PORT_DRD;
+		typec_cap.type = TYPEC_PORT_DRP;
+		typec_cap.data = TYPEC_PORT_DRD;
 		break;
 	case TPS_PORTINFO_DRP_UFP:
-		tps->typec_cap.type = TYPEC_PORT_DRP;
-		tps->typec_cap.data = TYPEC_PORT_UFP;
+		typec_cap.type = TYPEC_PORT_DRP;
+		typec_cap.data = TYPEC_PORT_UFP;
 		break;
 	case TPS_PORTINFO_DRP_DFP:
-		tps->typec_cap.type = TYPEC_PORT_DRP;
-		tps->typec_cap.data = TYPEC_PORT_DFP;
+		typec_cap.type = TYPEC_PORT_DRP;
+		typec_cap.data = TYPEC_PORT_DFP;
 		break;
 	case TPS_PORTINFO_SOURCE:
-		tps->typec_cap.type = TYPEC_PORT_SRC;
-		tps->typec_cap.data = TYPEC_PORT_DFP;
+		typec_cap.type = TYPEC_PORT_SRC;
+		typec_cap.data = TYPEC_PORT_DFP;
 		break;
 	default:
 		return -ENODEV;
 	}
 
-	tps->port = typec_register_port(&client->dev, &tps->typec_cap);
+	tps->port = typec_register_port(&client->dev, &typec_cap);
 	if (IS_ERR(tps->port))
 		return PTR_ERR(tps->port);
 
-- 
2.23.0


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

* [PATCH 6/7] usb: typec: ucsi: Start using struct typec_operations
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
                   ` (4 preceding siblings ...)
  2019-10-01  9:48 ` [PATCH 5/7] usb: typec: tps6598x: " Heikki Krogerus
@ 2019-10-01  9:48 ` " Heikki Krogerus
  2019-10-01 13:35   ` Guenter Roeck
  2019-10-01  9:48 ` [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability Heikki Krogerus
  6 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

Supplying the operation callbacks as part of a struct
typec_operations instead of as part of struct
typec_capability during port registration.

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

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index ba288b964dc8..edd722fb88b8 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -17,9 +17,6 @@
 #include "ucsi.h"
 #include "trace.h"
 
-#define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
-					      typec_cap)
-
 /*
  * UCSI_TIMEOUT_MS - PPM communication timeout
  *
@@ -713,10 +710,9 @@ static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
 	return ret;
 }
 
-static int
-ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
+static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
 {
-	struct ucsi_connector *con = to_ucsi_connector(cap);
+	struct ucsi_connector *con = typec_get_drvdata(port);
 	struct ucsi_control ctrl;
 	int ret = 0;
 
@@ -748,10 +744,9 @@ ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
 	return ret < 0 ? ret : 0;
 }
 
-static int
-ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
+static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 {
-	struct ucsi_connector *con = to_ucsi_connector(cap);
+	struct ucsi_connector *con = typec_get_drvdata(port);
 	struct ucsi_control ctrl;
 	int ret = 0;
 
@@ -788,6 +783,11 @@ ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
 	return ret;
 }
 
+static const struct typec_operations ucsi_ops = {
+	.dr_set = ucsi_dr_swap,
+	.pr_set = ucsi_pr_swap
+};
+
 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
 {
 	struct fwnode_handle *fwnode;
@@ -843,8 +843,8 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)
 		*accessory = TYPEC_ACCESSORY_DEBUG;
 
 	cap->fwnode = ucsi_find_fwnode(con);
-	cap->dr_set = ucsi_dr_swap;
-	cap->pr_set = ucsi_pr_swap;
+	cap->driver_data = con;
+	cap->ops = &ucsi_ops;
 
 	/* Register the connector */
 	con->port = typec_register_port(ucsi->dev, cap);
-- 
2.23.0


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

* [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability
  2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
                   ` (5 preceding siblings ...)
  2019-10-01  9:48 ` [PATCH 6/7] usb: typec: ucsi: " Heikki Krogerus
@ 2019-10-01  9:48 ` Heikki Krogerus
  2019-10-01 13:37   ` Guenter Roeck
  6 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-01  9:48 UTC (permalink / raw)
  To: Guenter Roeck, Hans de Goede; +Cc: linux-usb

There are no more users for them.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/class.c | 65 +++++++++++++--------------------------
 include/linux/usb/typec.h | 17 ----------
 2 files changed, 22 insertions(+), 60 deletions(-)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 542be63795db..58e83fc54aa6 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -58,7 +58,6 @@ struct typec_port {
 	struct typec_switch		*sw;
 	struct typec_mux		*mux;
 
-	const struct typec_capability	*cap;
 	const struct typec_operations	*ops;
 };
 
@@ -970,19 +969,15 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
 			return -EINVAL;
 	}
 
-	if (port->ops && port->ops->try_role) {
-		ret = port->ops->try_role(port, role);
-		if (ret)
-			return ret;
-	} else if (port->cap && port->cap->try_role) {
-		ret = port->cap->try_role(port->cap, role);
-		if (ret)
-			return ret;
-	} else {
+	if (!port->ops || !port->ops->try_role) {
 		dev_dbg(dev, "Setting preferred role not supported\n");
 		return -EOPNOTSUPP;
 	}
 
+	ret = port->ops->try_role(port, role);
+	if (ret)
+		return ret;
+
 	port->prefer_role = role;
 	return size;
 }
@@ -1020,20 +1015,16 @@ static ssize_t data_role_store(struct device *dev,
 		goto unlock_and_ret;
 	}
 
-	if (port->ops && port->ops->dr_set) {
-		ret = port->ops->dr_set(port, ret);
-		if (ret)
-			goto unlock_and_ret;
-	} else if (port->cap && port->cap->dr_set) {
-		ret = port->cap->dr_set(port->cap, ret);
-		if (ret)
-			goto unlock_and_ret;
-	} else {
+	if (!port->ops || !port->ops->dr_set) {
 		dev_dbg(dev, "data role swapping not supported\n");
 		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
 	}
 
+	ret = port->ops->dr_set(port, ret);
+	if (ret)
+		goto unlock_and_ret;
+
 	ret = size;
 unlock_and_ret:
 	mutex_unlock(&port->port_type_lock);
@@ -1082,21 +1073,17 @@ static ssize_t power_role_store(struct device *dev,
 		goto unlock_and_ret;
 	}
 
-	if (port->ops && port->ops->pr_set) {
-		ret = port->ops->pr_set(port, ret);
-		if (ret)
-			goto unlock_and_ret;
-	} else if (port->cap && port->cap->pr_set) {
-		ret = port->cap->pr_set(port->cap, ret);
-		if (ret)
-			goto unlock_and_ret;
-	} else {
+	if (!port->ops || !port->ops->pr_set) {
 		dev_dbg(dev, "power role swapping not supported\n");
 		ret = -EOPNOTSUPP;
 		goto unlock_and_ret;
 	}
 	ret = size;
 
+	ret = port->ops->pr_set(port, ret);
+	if (ret)
+		goto unlock_and_ret;
+
 unlock_and_ret:
 	mutex_unlock(&port->port_type_lock);
 	return ret;
@@ -1124,7 +1111,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
 	enum typec_port_type type;
 
 	if ((!port->ops || !port->ops->port_type_set) ||
-	    !port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
+	    port->fixed_role != TYPEC_PORT_DRP) {
 		dev_dbg(dev, "changing port type not supported\n");
 		return -EOPNOTSUPP;
 	}
@@ -1141,10 +1128,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
 		goto unlock_and_ret;
 	}
 
-	if (port->ops && port->ops->port_type_set)
-		ret = port->ops->port_type_set(port, type);
-	else
-		ret = port->cap->port_type_set(port->cap, type);
+	ret = port->ops->port_type_set(port, type);
 	if (ret)
 		goto unlock_and_ret;
 
@@ -1204,19 +1188,15 @@ static ssize_t vconn_source_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	if (port->ops && port->ops->vconn_set) {
-		ret = port->ops->vconn_set(port, source);
-		if (ret)
-			return ret;
-	} else if (port->cap && port->cap->vconn_set) {
-		ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
-		if (ret)
-			return ret;
-	} else {
+	if (!port->ops || !port->ops->vconn_set) {
 		dev_dbg(dev, "VCONN swapping not supported\n");
 		return -EOPNOTSUPP;
 	}
 
+	ret = port->ops->vconn_set(port, source);
+	if (ret)
+		return ret;
+
 	return size;
 }
 
@@ -1619,7 +1599,6 @@ struct typec_port *typec_register_port(struct device *parent,
 	mutex_init(&port->port_type_lock);
 
 	port->id = id;
-	port->cap = cap;
 	port->ops = cap->ops;
 	port->port_type = cap->type;
 	port->fixed_role = cap->type;
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 6c95a9ff43c6..e759668f8af9 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -197,11 +197,6 @@ struct typec_operations {
  * @fwnode: Optional fwnode of the port
  * @driver_data: Private pointer for driver specific info
  * @ops: Port operations vector
- * @try_role: Set data role preference for DRP port
- * @dr_set: Set Data Role
- * @pr_set: Set Power Role
- * @vconn_set: Set VCONN Role
- * @port_type_set: Set port type
  *
  * Static capabilities of a single USB Type-C port.
  */
@@ -219,18 +214,6 @@ struct typec_capability {
 	void			*driver_data;
 
 	const struct typec_operations	*ops;
-
-	int		(*try_role)(const struct typec_capability *,
-				    int role);
-
-	int		(*dr_set)(const struct typec_capability *,
-				  enum typec_data_role);
-	int		(*pr_set)(const struct typec_capability *,
-				  enum typec_role);
-	int		(*vconn_set)(const struct typec_capability *,
-				     enum typec_role);
-	int		(*port_type_set)(const struct typec_capability *,
-					 enum typec_port_type);
 };
 
 /* Specific to try_role(). Indicates the user want's to clear the preference. */
-- 
2.23.0


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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-01  9:48 ` [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration Heikki Krogerus
@ 2019-10-01 13:08   ` Guenter Roeck
  2019-10-02 16:06     ` Heikki Krogerus
  0 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:08 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

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;
> 


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

* Re: [PATCH 3/7] usb: typec: Separate the operations vector
  2019-10-01  9:48 ` [PATCH 3/7] usb: typec: Separate the operations vector Heikki Krogerus
@ 2019-10-01 13:22   ` Guenter Roeck
  2019-10-04  8:45     ` Heikki Krogerus
  0 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:22 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Introducing struct typec_operations which has the same
> callbacks as struct typec_capability. The old callbacks are
> kept for now, but after all users have been converted, they
> will be removed.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>   drivers/usb/typec/class.c | 90 +++++++++++++++++++++++++--------------
>   include/linux/usb/typec.h | 19 +++++++++
>   2 files changed, 76 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 9fab0be8f08c..542be63795db 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -59,6 +59,7 @@ struct typec_port {
>   	struct typec_mux		*mux;
>   
>   	const struct typec_capability	*cap;
> +	const struct typec_operations	*ops;
>   };
>   
>   #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
> @@ -961,11 +962,6 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
>   		return -EOPNOTSUPP;
>   	}
>   
> -	if (!port->cap->try_role) {
> -		dev_dbg(dev, "Setting preferred role not supported\n");
> -		return -EOPNOTSUPP;
> -	}
> -
>   	role = sysfs_match_string(typec_roles, buf);
>   	if (role < 0) {
>   		if (sysfs_streq(buf, "none"))
> @@ -974,9 +970,18 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
>   			return -EINVAL;
>   	}
>   
> -	ret = port->cap->try_role(port->cap, role);
> -	if (ret)
> -		return ret;
> +	if (port->ops && port->ops->try_role) {
> +		ret = port->ops->try_role(port, role);
> +		if (ret)
> +			return ret;
> +	} else if (port->cap && port->cap->try_role) {
> +		ret = port->cap->try_role(port->cap, role);
> +		if (ret)
> +			return ret;
> +	} else {
> +		dev_dbg(dev, "Setting preferred role not supported\n");
> +		return -EOPNOTSUPP;
> +	}
>   

This is a semantic change: Support is now checked _after_ the string is evaluated.
I understand the reason, but it should be noted in the patch description
(not sure if it is worth it, though - it seems to me it makes the code more
difficult to read).

>   	port->prefer_role = role;
>   	return size;
> @@ -1005,11 +1010,6 @@ static ssize_t data_role_store(struct device *dev,
>   	struct typec_port *port = to_typec_port(dev);
>   	int ret;
>   
> -	if (!port->cap->dr_set) {
> -		dev_dbg(dev, "data role swapping not supported\n");
> -		return -EOPNOTSUPP;
> -	}
> -
>   	ret = sysfs_match_string(typec_data_roles, buf);
>   	if (ret < 0)
>   		return ret;
> @@ -1020,9 +1020,19 @@ static ssize_t data_role_store(struct device *dev,
>   		goto unlock_and_ret;
>   	}
>   
> -	ret = port->cap->dr_set(port->cap, ret);
> -	if (ret)
> +	if (port->ops && port->ops->dr_set) {
> +		ret = port->ops->dr_set(port, ret);
> +		if (ret)
> +			goto unlock_and_ret;
> +	} else if (port->cap && port->cap->dr_set) {
> +		ret = port->cap->dr_set(port->cap, ret);
> +		if (ret)
> +			goto unlock_and_ret;
> +	} else {
> +		dev_dbg(dev, "data role swapping not supported\n");
> +		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;

This really makes me wonder if the semantic change makes sense. Support
is now evaluated _after_ the lock has been obtained. That seems like a
waste.

> +	}
>   
>   	ret = size;
>   unlock_and_ret:
> @@ -1055,11 +1065,6 @@ static ssize_t power_role_store(struct device *dev,
>   		return -EOPNOTSUPP;
>   	}
>   
> -	if (!port->cap->pr_set) {
> -		dev_dbg(dev, "power role swapping not supported\n");
> -		return -EOPNOTSUPP;
> -	}
> -
>   	if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
>   		dev_dbg(dev, "partner unable to swap power role\n");
>   		return -EIO;
> @@ -1077,11 +1082,21 @@ static ssize_t power_role_store(struct device *dev,
>   		goto unlock_and_ret;
>   	}
>   
> -	ret = port->cap->pr_set(port->cap, ret);
> -	if (ret)
> +	if (port->ops && port->ops->pr_set) {
> +		ret = port->ops->pr_set(port, ret);
> +		if (ret)
> +			goto unlock_and_ret;
> +	} else if (port->cap && port->cap->pr_set) {
> +		ret = port->cap->pr_set(port->cap, ret);
> +		if (ret)
> +			goto unlock_and_ret;
> +	} else {
> +		dev_dbg(dev, "power role swapping not supported\n");
> +		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;
> -
> +	}
>   	ret = size;
> +
>   unlock_and_ret:
>   	mutex_unlock(&port->port_type_lock);
>   	return ret;
> @@ -1108,7 +1123,8 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   	int ret;
>   	enum typec_port_type type;
>   
> -	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> +	if ((!port->ops || !port->ops->port_type_set) ||
> +	    !port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {

The above now requires _all_ callbacks to exist, both ops and cap based ones.
Is that on purpose ? Maybe this should be as follows ?

	if (((!port->ops || !port->ops->port_type_set) &&
	     !port->cap->port_type_set) || port->fixed_role != TYPEC_PORT_DRP) {

or a bit better to read
	if (port->fixed_role != TYPEC_PORT_DRP ||
	    ((!port->ops || !port->ops->port_type_set) && !port->cap->port_type_set))

>   		dev_dbg(dev, "changing port type not supported\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -1125,7 +1141,10 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   		goto unlock_and_ret;
>   	}
>   
> -	ret = port->cap->port_type_set(port->cap, type);
> +	if (port->ops && port->ops->port_type_set)
> +		ret = port->ops->port_type_set(port, type);
> +	else
> +		ret = port->cap->port_type_set(port->cap, type);
>   	if (ret)
>   		goto unlock_and_ret;
>   
> @@ -1181,18 +1200,22 @@ static ssize_t vconn_source_store(struct device *dev,
>   		return -EOPNOTSUPP;
>   	}
>   
> -	if (!port->cap->vconn_set) {
> -		dev_dbg(dev, "VCONN swapping not supported\n");
> -		return -EOPNOTSUPP;
> -	}
> -
>   	ret = kstrtobool(buf, &source);
>   	if (ret)
>   		return ret;
>   
> -	ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
> -	if (ret)
> -		return ret;
> +	if (port->ops && port->ops->vconn_set) {
> +		ret = port->ops->vconn_set(port, source);
> +		if (ret)
> +			return ret;
> +	} else if (port->cap && port->cap->vconn_set) {
> +		ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
> +		if (ret)
> +			return ret;
> +	} else {
> +		dev_dbg(dev, "VCONN swapping not supported\n");
> +		return -EOPNOTSUPP;
> +	}
>   
>   	return size;
>   }
> @@ -1597,6 +1620,7 @@ struct typec_port *typec_register_port(struct device *parent,
>   
>   	port->id = id;
>   	port->cap = cap;
> +	port->ops = cap->ops;
>   	port->port_type = cap->type;
>   	port->fixed_role = cap->type;
>   	port->port_roles = cap->data;
> diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
> index 8b90cd77331c..6c95a9ff43c6 100644
> --- a/include/linux/usb/typec.h
> +++ b/include/linux/usb/typec.h
> @@ -168,6 +168,22 @@ struct typec_partner_desc {
>   	struct usb_pd_identity	*identity;
>   };
>   
> +/*
> + * struct typec_operations - USB Type-C Port Operations
> + * @try_role: Set data role preference for DRP port
> + * @dr_set: Set Data Role
> + * @pr_set: Set Power Role
> + * @vconn_set: Source VCONN
> + * @port_type_set: Set port type
> + */
> +struct typec_operations {
> +	int (*try_role)(struct typec_port *port, int role);
> +	int (*dr_set)(struct typec_port *port, enum typec_data_role);
> +	int (*pr_set)(struct typec_port *port, enum typec_role);
> +	int (*vconn_set)(struct typec_port *port, bool source);
> +	int (*port_type_set)(struct typec_port *port, enum typec_port_type);
> +};
> +
>   /*
>    * struct typec_capability - USB Type-C Port Capabilities
>    * @type: Supported power role of the port
> @@ -180,6 +196,7 @@ struct typec_partner_desc {
>    * @mux: Multiplexer switch for Alternate/Accessory Modes
>    * @fwnode: Optional fwnode of the port
>    * @driver_data: Private pointer for driver specific info
> + * @ops: Port operations vector
>    * @try_role: Set data role preference for DRP port
>    * @dr_set: Set Data Role
>    * @pr_set: Set Power Role
> @@ -201,6 +218,8 @@ struct typec_capability {
>   	struct fwnode_handle	*fwnode;
>   	void			*driver_data;
>   
> +	const struct typec_operations	*ops;
> +
>   	int		(*try_role)(const struct typec_capability *,
>   				    int role);
>   
> 


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

* Re: [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations
  2019-10-01  9:48 ` [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations Heikki Krogerus
@ 2019-10-01 13:30   ` Guenter Roeck
  2019-10-04  8:46     ` Heikki Krogerus
  0 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:30 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Supplying the operation callbacks as part of a struct
> typec_operations instead of as part of struct
> typec_capability during port registration.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>   drivers/usb/typec/tcpm/tcpm.c | 47 ++++++++++++++++-------------------
>   1 file changed, 21 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 96562744101c..0fde7e042d5f 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -390,12 +390,6 @@ static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
>   	return SRC_UNATTACHED;
>   }
>   
> -static inline
> -struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
> -{
> -	return container_of(cap, struct tcpm_port, typec_caps);
> -}
> -
>   static bool tcpm_port_is_disconnected(struct tcpm_port *port)
>   {
>   	return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
> @@ -3970,10 +3964,9 @@ void tcpm_pd_hard_reset(struct tcpm_port *port)
>   }
>   EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
>   
> -static int tcpm_dr_set(const struct typec_capability *cap,
> -		       enum typec_data_role data)
> +static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
>   {
> -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> +	struct tcpm_port *port = typec_get_drvdata(p);
>   	int ret;
>   
>   	mutex_lock(&port->swap_lock);
> @@ -4038,10 +4031,9 @@ static int tcpm_dr_set(const struct typec_capability *cap,
>   	return ret;
>   }
>   
> -static int tcpm_pr_set(const struct typec_capability *cap,
> -		       enum typec_role role)
> +static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
>   {
> -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> +	struct tcpm_port *port = typec_get_drvdata(p);
>   	int ret;
>   
>   	mutex_lock(&port->swap_lock);
> @@ -4082,10 +4074,9 @@ static int tcpm_pr_set(const struct typec_capability *cap,
>   	return ret;
>   }
>   
> -static int tcpm_vconn_set(const struct typec_capability *cap,
> -			  enum typec_role role)
> +static int tcpm_vconn_set(struct typec_port *p, bool source)
>   {
> -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> +	struct tcpm_port *port = typec_get_drvdata(p);
>   	int ret;
>   
>   	mutex_lock(&port->swap_lock);
> @@ -4096,7 +4087,7 @@ static int tcpm_vconn_set(const struct typec_capability *cap,
>   		goto port_unlock;
>   	}
>   
> -	if (role == port->vconn_role) {
> +	if (source == port->vconn_role) {

source is boolean, vconn_role is enum typec_role.
The original typec code took advantage of typec_role == TYPEC_SINK matching false,
and typec_role == TYPEC_SOURCE matching true, but I don't think it is a good
idea to carry that down to low level drivers. This will confuse everyone who wants
to contribute a driver in the future.

>   		ret = 0;
>   		goto port_unlock;
>   	}
> @@ -4122,9 +4113,9 @@ static int tcpm_vconn_set(const struct typec_capability *cap,
>   	return ret;
>   }
>   
> -static int tcpm_try_role(const struct typec_capability *cap, int role)
> +static int tcpm_try_role(struct typec_port *p, int role)
>   {
> -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> +	struct tcpm_port *port = typec_get_drvdata(p);
>   	struct tcpc_dev	*tcpc = port->tcpc;
>   	int ret = 0;
>   
> @@ -4331,10 +4322,9 @@ static void tcpm_init(struct tcpm_port *port)
>   	tcpm_set_state(port, PORT_RESET, 0);
>   }
>   
> -static int tcpm_port_type_set(const struct typec_capability *cap,
> -			      enum typec_port_type type)
> +static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
>   {
> -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> +	struct tcpm_port *port = typec_get_drvdata(p);
>   
>   	mutex_lock(&port->lock);
>   	if (type == port->port_type)
> @@ -4359,6 +4349,14 @@ static int tcpm_port_type_set(const struct typec_capability *cap,
>   	return 0;
>   }
>   
> +static const struct typec_operations tcpm_ops = {
> +	.try_role = tcpm_try_role,
> +	.dr_set = tcpm_dr_set,
> +	.pr_set = tcpm_pr_set,
> +	.vconn_set = tcpm_vconn_set,
> +	.port_type_set = tcpm_port_type_set
> +};
> +
>   void tcpm_tcpc_reset(struct tcpm_port *port)
>   {
>   	mutex_lock(&port->lock);
> @@ -4770,11 +4768,8 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
>   	port->typec_caps.fwnode = tcpc->fwnode;
>   	port->typec_caps.revision = 0x0120;	/* Type-C spec release 1.2 */
>   	port->typec_caps.pd_revision = 0x0300;	/* USB-PD spec release 3.0 */
> -	port->typec_caps.dr_set = tcpm_dr_set;
> -	port->typec_caps.pr_set = tcpm_pr_set;
> -	port->typec_caps.vconn_set = tcpm_vconn_set;
> -	port->typec_caps.try_role = tcpm_try_role;
> -	port->typec_caps.port_type_set = tcpm_port_type_set;
> +	port->typec_caps.driver_data = port;
> +	port->typec_caps.ops = &tcpm_ops;
>   
>   	port->partner_desc.identity = &port->partner_ident;
>   	port->port_type = port->typec_caps.type;
> 


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

* Re: [PATCH 5/7] usb: typec: tps6598x: Start using struct typec_operations
  2019-10-01  9:48 ` [PATCH 5/7] usb: typec: tps6598x: " Heikki Krogerus
@ 2019-10-01 13:34   ` Guenter Roeck
  2019-10-01 13:35   ` Guenter Roeck
  1 sibling, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:34 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Supplying the operation callbacks as part of a struct
> typec_operations instead of as part of struct
> typec_capability during port registration. After this there
> is not need to keep the capabilities stored anywhere in the
> driver.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>   drivers/usb/typec/tps6598x.c | 49 +++++++++++++++++++-----------------
>   1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
> index a38d1409f15b..0698addd1185 100644
> --- a/drivers/usb/typec/tps6598x.c
> +++ b/drivers/usb/typec/tps6598x.c
> @@ -94,7 +94,6 @@ struct tps6598x {
>   	struct typec_port *port;
>   	struct typec_partner *partner;
>   	struct usb_pd_identity partner_identity;
> -	struct typec_capability typec_cap;
>   };
>   
>   /*
> @@ -307,11 +306,10 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
>   	return 0;
>   }
>   
> -static int
> -tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
> +static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
>   {
> -	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
>   	const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
> +	struct tps6598x *tps = typec_get_drvdata(port);
>   	u32 status;
>   	int ret;
>   
> @@ -338,11 +336,10 @@ tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
>   	return ret;
>   }
>   
> -static int
> -tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
> +static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
>   {
> -	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
>   	const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
> +	struct tps6598x *tps = typec_get_drvdata(port);
>   	u32 status;
>   	int ret;
>   
> @@ -369,6 +366,11 @@ tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
>   	return ret;
>   }
>   
> +static const struct typec_operations tps6598x_ops = {
> +	.dr_set = tps6598x_dr_set,
> +	.pr_set = tps6598x_pr_set,
> +};
> +
>   static irqreturn_t tps6598x_interrupt(int irq, void *data)
>   {
>   	struct tps6598x *tps = data;
> @@ -448,6 +450,7 @@ static const struct regmap_config tps6598x_regmap_config = {
>   
>   static int tps6598x_probe(struct i2c_client *client)
>   {
> +	struct typec_capability typec_cap = { };
>   	struct tps6598x *tps;
>   	u32 status;
>   	u32 conf;
> @@ -492,40 +495,40 @@ static int tps6598x_probe(struct i2c_client *client)
>   	if (ret < 0)
>   		return ret;
>   
> -	tps->typec_cap.revision = USB_TYPEC_REV_1_2;
> -	tps->typec_cap.pd_revision = 0x200;
> -	tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
> -	tps->typec_cap.pr_set = tps6598x_pr_set;
> -	tps->typec_cap.dr_set = tps6598x_dr_set;
> +	typec_cap.revision = USB_TYPEC_REV_1_2;
> +	typec_cap.pd_revision = 0x200;
> +	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;

Nitpick:
	struct typec_capability typec_cap = {
		.revision = USB_TYPEC_REV_1_2,
		.pd_revision = 0x200,
		.prefer_role = TYPEC_NO_PREFERRED_ROLE,
	};

Your call, though.

> +	typec_cap.driver_data = tps;
> +	typec_cap.ops = &tps6598x_ops;
>   
>   	switch (TPS_SYSCONF_PORTINFO(conf)) {
>   	case TPS_PORTINFO_SINK_ACCESSORY:
>   	case TPS_PORTINFO_SINK:
> -		tps->typec_cap.type = TYPEC_PORT_SNK;
> -		tps->typec_cap.data = TYPEC_PORT_UFP;
> +		typec_cap.type = TYPEC_PORT_SNK;
> +		typec_cap.data = TYPEC_PORT_UFP;
>   		break;
>   	case TPS_PORTINFO_DRP_UFP_DRD:
>   	case TPS_PORTINFO_DRP_DFP_DRD:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_DRD;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_DRD;
>   		break;
>   	case TPS_PORTINFO_DRP_UFP:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_UFP;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_UFP;
>   		break;
>   	case TPS_PORTINFO_DRP_DFP:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_DFP;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_DFP;
>   		break;
>   	case TPS_PORTINFO_SOURCE:
> -		tps->typec_cap.type = TYPEC_PORT_SRC;
> -		tps->typec_cap.data = TYPEC_PORT_DFP;
> +		typec_cap.type = TYPEC_PORT_SRC;
> +		typec_cap.data = TYPEC_PORT_DFP;
>   		break;
>   	default:
>   		return -ENODEV;
>   	}
>   
> -	tps->port = typec_register_port(&client->dev, &tps->typec_cap);
> +	tps->port = typec_register_port(&client->dev, &typec_cap);
>   	if (IS_ERR(tps->port))
>   		return PTR_ERR(tps->port);
>   
> 


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

* Re: [PATCH 6/7] usb: typec: ucsi: Start using struct typec_operations
  2019-10-01  9:48 ` [PATCH 6/7] usb: typec: ucsi: " Heikki Krogerus
@ 2019-10-01 13:35   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:35 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Supplying the operation callbacks as part of a struct
> typec_operations instead of as part of struct
> typec_capability during port registration.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> ---
>   drivers/usb/typec/ucsi/ucsi.c | 22 +++++++++++-----------
>   1 file changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index ba288b964dc8..edd722fb88b8 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -17,9 +17,6 @@
>   #include "ucsi.h"
>   #include "trace.h"
>   
> -#define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
> -					      typec_cap)
> -
>   /*
>    * UCSI_TIMEOUT_MS - PPM communication timeout
>    *
> @@ -713,10 +710,9 @@ static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
>   	return ret;
>   }
>   
> -static int
> -ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
> +static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
>   {
> -	struct ucsi_connector *con = to_ucsi_connector(cap);
> +	struct ucsi_connector *con = typec_get_drvdata(port);
>   	struct ucsi_control ctrl;
>   	int ret = 0;
>   
> @@ -748,10 +744,9 @@ ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
>   	return ret < 0 ? ret : 0;
>   }
>   
> -static int
> -ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
> +static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
>   {
> -	struct ucsi_connector *con = to_ucsi_connector(cap);
> +	struct ucsi_connector *con = typec_get_drvdata(port);
>   	struct ucsi_control ctrl;
>   	int ret = 0;
>   
> @@ -788,6 +783,11 @@ ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
>   	return ret;
>   }
>   
> +static const struct typec_operations ucsi_ops = {
> +	.dr_set = ucsi_dr_swap,
> +	.pr_set = ucsi_pr_swap
> +};
> +
>   static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
>   {
>   	struct fwnode_handle *fwnode;
> @@ -843,8 +843,8 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)
>   		*accessory = TYPEC_ACCESSORY_DEBUG;
>   
>   	cap->fwnode = ucsi_find_fwnode(con);
> -	cap->dr_set = ucsi_dr_swap;
> -	cap->pr_set = ucsi_pr_swap;
> +	cap->driver_data = con;
> +	cap->ops = &ucsi_ops;
>   
>   	/* Register the connector */
>   	con->port = typec_register_port(ucsi->dev, cap);
> 


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

* Re: [PATCH 5/7] usb: typec: tps6598x: Start using struct typec_operations
  2019-10-01  9:48 ` [PATCH 5/7] usb: typec: tps6598x: " Heikki Krogerus
  2019-10-01 13:34   ` Guenter Roeck
@ 2019-10-01 13:35   ` Guenter Roeck
  2019-10-04  8:49     ` Heikki Krogerus
  1 sibling, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:35 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> Supplying the operation callbacks as part of a struct
> typec_operations instead of as part of struct
> typec_capability during port registration. After this there
> is not need to keep the capabilities stored anywhere in the
> driver.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

Nitpick or not:

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> ---
>   drivers/usb/typec/tps6598x.c | 49 +++++++++++++++++++-----------------
>   1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
> index a38d1409f15b..0698addd1185 100644
> --- a/drivers/usb/typec/tps6598x.c
> +++ b/drivers/usb/typec/tps6598x.c
> @@ -94,7 +94,6 @@ struct tps6598x {
>   	struct typec_port *port;
>   	struct typec_partner *partner;
>   	struct usb_pd_identity partner_identity;
> -	struct typec_capability typec_cap;
>   };
>   
>   /*
> @@ -307,11 +306,10 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
>   	return 0;
>   }
>   
> -static int
> -tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
> +static int tps6598x_dr_set(struct typec_port *port, enum typec_data_role role)
>   {
> -	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
>   	const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
> +	struct tps6598x *tps = typec_get_drvdata(port);
>   	u32 status;
>   	int ret;
>   
> @@ -338,11 +336,10 @@ tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
>   	return ret;
>   }
>   
> -static int
> -tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
> +static int tps6598x_pr_set(struct typec_port *port, enum typec_role role)
>   {
> -	struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
>   	const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
> +	struct tps6598x *tps = typec_get_drvdata(port);
>   	u32 status;
>   	int ret;
>   
> @@ -369,6 +366,11 @@ tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
>   	return ret;
>   }
>   
> +static const struct typec_operations tps6598x_ops = {
> +	.dr_set = tps6598x_dr_set,
> +	.pr_set = tps6598x_pr_set,
> +};
> +
>   static irqreturn_t tps6598x_interrupt(int irq, void *data)
>   {
>   	struct tps6598x *tps = data;
> @@ -448,6 +450,7 @@ static const struct regmap_config tps6598x_regmap_config = {
>   
>   static int tps6598x_probe(struct i2c_client *client)
>   {
> +	struct typec_capability typec_cap = { };
>   	struct tps6598x *tps;
>   	u32 status;
>   	u32 conf;
> @@ -492,40 +495,40 @@ static int tps6598x_probe(struct i2c_client *client)
>   	if (ret < 0)
>   		return ret;
>   
> -	tps->typec_cap.revision = USB_TYPEC_REV_1_2;
> -	tps->typec_cap.pd_revision = 0x200;
> -	tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
> -	tps->typec_cap.pr_set = tps6598x_pr_set;
> -	tps->typec_cap.dr_set = tps6598x_dr_set;
> +	typec_cap.revision = USB_TYPEC_REV_1_2;
> +	typec_cap.pd_revision = 0x200;
> +	typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
> +	typec_cap.driver_data = tps;
> +	typec_cap.ops = &tps6598x_ops;
>   
>   	switch (TPS_SYSCONF_PORTINFO(conf)) {
>   	case TPS_PORTINFO_SINK_ACCESSORY:
>   	case TPS_PORTINFO_SINK:
> -		tps->typec_cap.type = TYPEC_PORT_SNK;
> -		tps->typec_cap.data = TYPEC_PORT_UFP;
> +		typec_cap.type = TYPEC_PORT_SNK;
> +		typec_cap.data = TYPEC_PORT_UFP;
>   		break;
>   	case TPS_PORTINFO_DRP_UFP_DRD:
>   	case TPS_PORTINFO_DRP_DFP_DRD:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_DRD;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_DRD;
>   		break;
>   	case TPS_PORTINFO_DRP_UFP:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_UFP;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_UFP;
>   		break;
>   	case TPS_PORTINFO_DRP_DFP:
> -		tps->typec_cap.type = TYPEC_PORT_DRP;
> -		tps->typec_cap.data = TYPEC_PORT_DFP;
> +		typec_cap.type = TYPEC_PORT_DRP;
> +		typec_cap.data = TYPEC_PORT_DFP;
>   		break;
>   	case TPS_PORTINFO_SOURCE:
> -		tps->typec_cap.type = TYPEC_PORT_SRC;
> -		tps->typec_cap.data = TYPEC_PORT_DFP;
> +		typec_cap.type = TYPEC_PORT_SRC;
> +		typec_cap.data = TYPEC_PORT_DFP;
>   		break;
>   	default:
>   		return -ENODEV;
>   	}
>   
> -	tps->port = typec_register_port(&client->dev, &tps->typec_cap);
> +	tps->port = typec_register_port(&client->dev, &typec_cap);
>   	if (IS_ERR(tps->port))
>   		return PTR_ERR(tps->port);
>   
> 


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

* Re: [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability
  2019-10-01  9:48 ` [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability Heikki Krogerus
@ 2019-10-01 13:37   ` Guenter Roeck
  0 siblings, 0 replies; 26+ messages in thread
From: Guenter Roeck @ 2019-10-01 13:37 UTC (permalink / raw)
  To: Heikki Krogerus, Hans de Goede; +Cc: linux-usb

On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> There are no more users for them.
> 
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>   drivers/usb/typec/class.c | 65 +++++++++++++--------------------------
>   include/linux/usb/typec.h | 17 ----------
>   2 files changed, 22 insertions(+), 60 deletions(-)
> 
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 542be63795db..58e83fc54aa6 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -58,7 +58,6 @@ struct typec_port {
>   	struct typec_switch		*sw;
>   	struct typec_mux		*mux;
>   
> -	const struct typec_capability	*cap;
>   	const struct typec_operations	*ops;
>   };
>   
> @@ -970,19 +969,15 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
>   			return -EINVAL;
>   	}
>   
> -	if (port->ops && port->ops->try_role) {
> -		ret = port->ops->try_role(port, role);
> -		if (ret)
> -			return ret;
> -	} else if (port->cap && port->cap->try_role) {
> -		ret = port->cap->try_role(port->cap, role);
> -		if (ret)
> -			return ret;
> -	} else {
> +	if (!port->ops || !port->ops->try_role) {
>   		dev_dbg(dev, "Setting preferred role not supported\n");
>   		return -EOPNOTSUPP;
>   	}

This leaves the semantic change in place. I think it would have been better
to keep the support checks as in the original code.

>   
> +	ret = port->ops->try_role(port, role);
> +	if (ret)
> +		return ret;
> +
>   	port->prefer_role = role;
>   	return size;
>   }
> @@ -1020,20 +1015,16 @@ static ssize_t data_role_store(struct device *dev,
>   		goto unlock_and_ret;
>   	}
>   
> -	if (port->ops && port->ops->dr_set) {
> -		ret = port->ops->dr_set(port, ret);
> -		if (ret)
> -			goto unlock_and_ret;
> -	} else if (port->cap && port->cap->dr_set) {
> -		ret = port->cap->dr_set(port->cap, ret);
> -		if (ret)
> -			goto unlock_and_ret;
> -	} else {
> +	if (!port->ops || !port->ops->dr_set) {
>   		dev_dbg(dev, "data role swapping not supported\n");
>   		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;
>   	}
>   
> +	ret = port->ops->dr_set(port, ret);
> +	if (ret)
> +		goto unlock_and_ret;
> +
>   	ret = size;
>   unlock_and_ret:
>   	mutex_unlock(&port->port_type_lock);
> @@ -1082,21 +1073,17 @@ static ssize_t power_role_store(struct device *dev,
>   		goto unlock_and_ret;
>   	}
>   
> -	if (port->ops && port->ops->pr_set) {
> -		ret = port->ops->pr_set(port, ret);
> -		if (ret)
> -			goto unlock_and_ret;
> -	} else if (port->cap && port->cap->pr_set) {
> -		ret = port->cap->pr_set(port->cap, ret);
> -		if (ret)
> -			goto unlock_and_ret;
> -	} else {
> +	if (!port->ops || !port->ops->pr_set) {
>   		dev_dbg(dev, "power role swapping not supported\n");
>   		ret = -EOPNOTSUPP;
>   		goto unlock_and_ret;
>   	}
>   	ret = size;
>   
> +	ret = port->ops->pr_set(port, ret);
> +	if (ret)
> +		goto unlock_and_ret;
> +
>   unlock_and_ret:
>   	mutex_unlock(&port->port_type_lock);
>   	return ret;
> @@ -1124,7 +1111,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   	enum typec_port_type type;
>   
>   	if ((!port->ops || !port->ops->port_type_set) ||
> -	    !port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> +	    port->fixed_role != TYPEC_PORT_DRP) {
>   		dev_dbg(dev, "changing port type not supported\n");
>   		return -EOPNOTSUPP;
>   	}
> @@ -1141,10 +1128,7 @@ port_type_store(struct device *dev, struct device_attribute *attr,
>   		goto unlock_and_ret;
>   	}
>   
> -	if (port->ops && port->ops->port_type_set)
> -		ret = port->ops->port_type_set(port, type);
> -	else
> -		ret = port->cap->port_type_set(port->cap, type);
> +	ret = port->ops->port_type_set(port, type);
>   	if (ret)
>   		goto unlock_and_ret;
>   
> @@ -1204,19 +1188,15 @@ static ssize_t vconn_source_store(struct device *dev,
>   	if (ret)
>   		return ret;
>   
> -	if (port->ops && port->ops->vconn_set) {
> -		ret = port->ops->vconn_set(port, source);
> -		if (ret)
> -			return ret;
> -	} else if (port->cap && port->cap->vconn_set) {
> -		ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
> -		if (ret)
> -			return ret;
> -	} else {
> +	if (!port->ops || !port->ops->vconn_set) {
>   		dev_dbg(dev, "VCONN swapping not supported\n");
>   		return -EOPNOTSUPP;
>   	}
>   
> +	ret = port->ops->vconn_set(port, source);
> +	if (ret)
> +		return ret;
> +
>   	return size;
>   }
>   
> @@ -1619,7 +1599,6 @@ struct typec_port *typec_register_port(struct device *parent,
>   	mutex_init(&port->port_type_lock);
>   
>   	port->id = id;
> -	port->cap = cap;
>   	port->ops = cap->ops;
>   	port->port_type = cap->type;
>   	port->fixed_role = cap->type;
> diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
> index 6c95a9ff43c6..e759668f8af9 100644
> --- a/include/linux/usb/typec.h
> +++ b/include/linux/usb/typec.h
> @@ -197,11 +197,6 @@ struct typec_operations {
>    * @fwnode: Optional fwnode of the port
>    * @driver_data: Private pointer for driver specific info
>    * @ops: Port operations vector
> - * @try_role: Set data role preference for DRP port
> - * @dr_set: Set Data Role
> - * @pr_set: Set Power Role
> - * @vconn_set: Set VCONN Role
> - * @port_type_set: Set port type
>    *
>    * Static capabilities of a single USB Type-C port.
>    */
> @@ -219,18 +214,6 @@ struct typec_capability {
>   	void			*driver_data;
>   
>   	const struct typec_operations	*ops;
> -
> -	int		(*try_role)(const struct typec_capability *,
> -				    int role);
> -
> -	int		(*dr_set)(const struct typec_capability *,
> -				  enum typec_data_role);
> -	int		(*pr_set)(const struct typec_capability *,
> -				  enum typec_role);
> -	int		(*vconn_set)(const struct typec_capability *,
> -				     enum typec_role);
> -	int		(*port_type_set)(const struct typec_capability *,
> -					 enum typec_port_type);
>   };
>   
>   /* Specific to try_role(). Indicates the user want's to clear the preference. */
> 


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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-01 13:08   ` Guenter Roeck
@ 2019-10-02 16:06     ` Heikki Krogerus
  2019-10-02 16:36       ` Guenter Roeck
  2019-10-02 19:16       ` Heikki Krogerus
  0 siblings, 2 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-02 16:06 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

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,

-- 
heikki

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-02 16:06     ` Heikki Krogerus
@ 2019-10-02 16:36       ` Guenter Roeck
  2019-10-02 18:29         ` Heikki Krogerus
  2019-10-02 19:16       ` Heikki Krogerus
  1 sibling, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-02 16:36 UTC (permalink / raw)
  To: Heikki Krogerus; +Cc: Hans de Goede, linux-usb

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

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-02 16:36       ` Guenter Roeck
@ 2019-10-02 18:29         ` Heikki Krogerus
  2019-10-03  3:45           ` Guenter Roeck
  0 siblings, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-02 18:29 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

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

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-02 16:06     ` Heikki Krogerus
  2019-10-02 16:36       ` Guenter Roeck
@ 2019-10-02 19:16       ` Heikki Krogerus
  2019-10-03  3:51         ` Guenter Roeck
  1 sibling, 1 reply; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-02 19:16 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

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

-- 
heikki

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-02 18:29         ` Heikki Krogerus
@ 2019-10-03  3:45           ` Guenter Roeck
  2019-10-03  8:03             ` Heikki Krogerus
  0 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-03  3:45 UTC (permalink / raw)
  To: Heikki Krogerus; +Cc: Hans de Goede, linux-usb

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.
> 

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-02 19:16       ` Heikki Krogerus
@ 2019-10-03  3:51         ` Guenter Roeck
  2019-10-03 13:29           ` Heikki Krogerus
  0 siblings, 1 reply; 26+ messages in thread
From: Guenter Roeck @ 2019-10-03  3:51 UTC (permalink / raw)
  To: Heikki Krogerus; +Cc: Hans de Goede, linux-usb

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
> 


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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-03  3:45           ` Guenter Roeck
@ 2019-10-03  8:03             ` Heikki Krogerus
  0 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-03  8:03 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

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

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

* Re: [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration
  2019-10-03  3:51         ` Guenter Roeck
@ 2019-10-03 13:29           ` Heikki Krogerus
  0 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-03 13:29 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

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
> > 

-- 
heikki

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

* Re: [PATCH 3/7] usb: typec: Separate the operations vector
  2019-10-01 13:22   ` Guenter Roeck
@ 2019-10-04  8:45     ` Heikki Krogerus
  0 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-04  8:45 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

On Tue, Oct 01, 2019 at 06:22:36AM -0700, Guenter Roeck wrote:
> On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> > Introducing struct typec_operations which has the same
> > callbacks as struct typec_capability. The old callbacks are
> > kept for now, but after all users have been converted, they
> > will be removed.
> > 
> > Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> > ---
> >   drivers/usb/typec/class.c | 90 +++++++++++++++++++++++++--------------
> >   include/linux/usb/typec.h | 19 +++++++++
> >   2 files changed, 76 insertions(+), 33 deletions(-)
> > 
> > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> > index 9fab0be8f08c..542be63795db 100644
> > --- a/drivers/usb/typec/class.c
> > +++ b/drivers/usb/typec/class.c
> > @@ -59,6 +59,7 @@ struct typec_port {
> >   	struct typec_mux		*mux;
> >   	const struct typec_capability	*cap;
> > +	const struct typec_operations	*ops;
> >   };
> >   #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
> > @@ -961,11 +962,6 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
> >   		return -EOPNOTSUPP;
> >   	}
> > -	if (!port->cap->try_role) {
> > -		dev_dbg(dev, "Setting preferred role not supported\n");
> > -		return -EOPNOTSUPP;
> > -	}
> > -
> >   	role = sysfs_match_string(typec_roles, buf);
> >   	if (role < 0) {
> >   		if (sysfs_streq(buf, "none"))
> > @@ -974,9 +970,18 @@ preferred_role_store(struct device *dev, struct device_attribute *attr,
> >   			return -EINVAL;
> >   	}
> > -	ret = port->cap->try_role(port->cap, role);
> > -	if (ret)
> > -		return ret;
> > +	if (port->ops && port->ops->try_role) {
> > +		ret = port->ops->try_role(port, role);
> > +		if (ret)
> > +			return ret;
> > +	} else if (port->cap && port->cap->try_role) {
> > +		ret = port->cap->try_role(port->cap, role);
> > +		if (ret)
> > +			return ret;
> > +	} else {
> > +		dev_dbg(dev, "Setting preferred role not supported\n");
> > +		return -EOPNOTSUPP;
> > +	}
> 
> This is a semantic change: Support is now checked _after_ the string is evaluated.
> I understand the reason, but it should be noted in the patch description
> (not sure if it is worth it, though - it seems to me it makes the code more
> difficult to read).
> 
> >   	port->prefer_role = role;
> >   	return size;
> > @@ -1005,11 +1010,6 @@ static ssize_t data_role_store(struct device *dev,
> >   	struct typec_port *port = to_typec_port(dev);
> >   	int ret;
> > -	if (!port->cap->dr_set) {
> > -		dev_dbg(dev, "data role swapping not supported\n");
> > -		return -EOPNOTSUPP;
> > -	}
> > -
> >   	ret = sysfs_match_string(typec_data_roles, buf);
> >   	if (ret < 0)
> >   		return ret;
> > @@ -1020,9 +1020,19 @@ static ssize_t data_role_store(struct device *dev,
> >   		goto unlock_and_ret;
> >   	}
> > -	ret = port->cap->dr_set(port->cap, ret);
> > -	if (ret)
> > +	if (port->ops && port->ops->dr_set) {
> > +		ret = port->ops->dr_set(port, ret);
> > +		if (ret)
> > +			goto unlock_and_ret;
> > +	} else if (port->cap && port->cap->dr_set) {
> > +		ret = port->cap->dr_set(port->cap, ret);
> > +		if (ret)
> > +			goto unlock_and_ret;
> > +	} else {
> > +		dev_dbg(dev, "data role swapping not supported\n");
> > +		ret = -EOPNOTSUPP;
> >   		goto unlock_and_ret;
> 
> This really makes me wonder if the semantic change makes sense. Support
> is now evaluated _after_ the lock has been obtained. That seems like a
> waste.

OK, I'll re-think this.

> > +	}
> >   	ret = size;
> >   unlock_and_ret:
> > @@ -1055,11 +1065,6 @@ static ssize_t power_role_store(struct device *dev,
> >   		return -EOPNOTSUPP;
> >   	}
> > -	if (!port->cap->pr_set) {
> > -		dev_dbg(dev, "power role swapping not supported\n");
> > -		return -EOPNOTSUPP;
> > -	}
> > -
> >   	if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
> >   		dev_dbg(dev, "partner unable to swap power role\n");
> >   		return -EIO;
> > @@ -1077,11 +1082,21 @@ static ssize_t power_role_store(struct device *dev,
> >   		goto unlock_and_ret;
> >   	}
> > -	ret = port->cap->pr_set(port->cap, ret);
> > -	if (ret)
> > +	if (port->ops && port->ops->pr_set) {
> > +		ret = port->ops->pr_set(port, ret);
> > +		if (ret)
> > +			goto unlock_and_ret;
> > +	} else if (port->cap && port->cap->pr_set) {
> > +		ret = port->cap->pr_set(port->cap, ret);
> > +		if (ret)
> > +			goto unlock_and_ret;
> > +	} else {
> > +		dev_dbg(dev, "power role swapping not supported\n");
> > +		ret = -EOPNOTSUPP;
> >   		goto unlock_and_ret;
> > -
> > +	}
> >   	ret = size;
> > +
> >   unlock_and_ret:
> >   	mutex_unlock(&port->port_type_lock);
> >   	return ret;
> > @@ -1108,7 +1123,8 @@ port_type_store(struct device *dev, struct device_attribute *attr,
> >   	int ret;
> >   	enum typec_port_type type;
> > -	if (!port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> > +	if ((!port->ops || !port->ops->port_type_set) ||
> > +	    !port->cap->port_type_set || port->fixed_role != TYPEC_PORT_DRP) {
> 
> The above now requires _all_ callbacks to exist, both ops and cap based ones.
> Is that on purpose ? Maybe this should be as follows ?
> 
> 	if (((!port->ops || !port->ops->port_type_set) &&
> 	     !port->cap->port_type_set) || port->fixed_role != TYPEC_PORT_DRP) {
> 
> or a bit better to read
> 	if (port->fixed_role != TYPEC_PORT_DRP ||
> 	    ((!port->ops || !port->ops->port_type_set) && !port->cap->port_type_set))

OK.


thanks,

-- 
heikki

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

* Re: [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations
  2019-10-01 13:30   ` Guenter Roeck
@ 2019-10-04  8:46     ` Heikki Krogerus
  0 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-04  8:46 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

On Tue, Oct 01, 2019 at 06:30:42AM -0700, Guenter Roeck wrote:
> > @@ -4082,10 +4074,9 @@ static int tcpm_pr_set(const struct typec_capability *cap,
> >   	return ret;
> >   }
> > -static int tcpm_vconn_set(const struct typec_capability *cap,
> > -			  enum typec_role role)
> > +static int tcpm_vconn_set(struct typec_port *p, bool source)
> >   {
> > -	struct tcpm_port *port = typec_cap_to_tcpm(cap);
> > +	struct tcpm_port *port = typec_get_drvdata(p);
> >   	int ret;
> >   	mutex_lock(&port->swap_lock);
> > @@ -4096,7 +4087,7 @@ static int tcpm_vconn_set(const struct typec_capability *cap,
> >   		goto port_unlock;
> >   	}
> > -	if (role == port->vconn_role) {
> > +	if (source == port->vconn_role) {
> 
> source is boolean, vconn_role is enum typec_role.
> The original typec code took advantage of typec_role == TYPEC_SINK matching false,
> and typec_role == TYPEC_SOURCE matching true, but I don't think it is a good
> idea to carry that down to low level drivers. This will confuse everyone who wants
> to contribute a driver in the future.

OK, I'll keep the parameter as emum typec_role.


thanks,

-- 
heikki

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

* Re: [PATCH 5/7] usb: typec: tps6598x: Start using struct typec_operations
  2019-10-01 13:35   ` Guenter Roeck
@ 2019-10-04  8:49     ` Heikki Krogerus
  0 siblings, 0 replies; 26+ messages in thread
From: Heikki Krogerus @ 2019-10-04  8:49 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Hans de Goede, linux-usb

On Tue, Oct 01, 2019 at 06:35:59AM -0700, Guenter Roeck wrote:
> On 10/1/19 2:48 AM, Heikki Krogerus wrote:
> > Supplying the operation callbacks as part of a struct
> > typec_operations instead of as part of struct
> > typec_capability during port registration. After this there
> > is not need to keep the capabilities stored anywhere in the
> > driver.
> > 
> > Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> Nitpick or not:
> 
> Reviewed-by: Guenter Roeck <linux@roeck-us.net>

Thanks for reviewing these Guenter. I'll preper v2.

cheers,

-- 
heikki

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

end of thread, back to index

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-01  9:48 [PATCH 0/7] usb: typec: Small API improvement Heikki Krogerus
2019-10-01  9:48 ` [PATCH 1/7] usb: typec: Copy everything from struct typec_capability during registration Heikki Krogerus
2019-10-01 13:08   ` Guenter Roeck
2019-10-02 16:06     ` Heikki Krogerus
2019-10-02 16:36       ` Guenter Roeck
2019-10-02 18:29         ` Heikki Krogerus
2019-10-03  3:45           ` Guenter Roeck
2019-10-03  8:03             ` Heikki Krogerus
2019-10-02 19:16       ` Heikki Krogerus
2019-10-03  3:51         ` Guenter Roeck
2019-10-03 13:29           ` Heikki Krogerus
2019-10-01  9:48 ` [PATCH 2/7] usb: typec: Introduce typec_get_drvdata() Heikki Krogerus
2019-10-01  9:48 ` [PATCH 3/7] usb: typec: Separate the operations vector Heikki Krogerus
2019-10-01 13:22   ` Guenter Roeck
2019-10-04  8:45     ` Heikki Krogerus
2019-10-01  9:48 ` [PATCH 4/7] usb: typec: tcpm: Start using struct typec_operations Heikki Krogerus
2019-10-01 13:30   ` Guenter Roeck
2019-10-04  8:46     ` Heikki Krogerus
2019-10-01  9:48 ` [PATCH 5/7] usb: typec: tps6598x: " Heikki Krogerus
2019-10-01 13:34   ` Guenter Roeck
2019-10-01 13:35   ` Guenter Roeck
2019-10-04  8:49     ` Heikki Krogerus
2019-10-01  9:48 ` [PATCH 6/7] usb: typec: ucsi: " Heikki Krogerus
2019-10-01 13:35   ` Guenter Roeck
2019-10-01  9:48 ` [PATCH 7/7] usb: typec: Remove the callback members from struct typec_capability Heikki Krogerus
2019-10-01 13:37   ` 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