diff mbox series

[v2,3/6] usb: typec: Port mapping utility

Message ID 20210329084426.78138-4-heikki.krogerus@linux.intel.com (mailing list archive)
State Superseded
Headers show
Series usb: Linking ports to their Type-C connectors | expand

Commit Message

Heikki Krogerus March 29, 2021, 8:44 a.m. UTC
Adding functions that can be used to link/unlink ports -
USB ports, TBT3/USB4 ports, DisplayPorts and so on - to
the USB Type-C connectors they are attached to inside a
system. The symlink that is created for the port device is
named "connector".

Initially only ACPI is supported. ACPI port object shares
the _PLD (Physical Location of Device) with the USB Type-C
connector that it's attached to.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/Makefile      |   2 +-
 drivers/usb/typec/class.c       |   7 +-
 drivers/usb/typec/class.h       |   9 ++
 drivers/usb/typec/port-mapper.c | 219 ++++++++++++++++++++++++++++++++
 include/linux/usb/typec.h       |  13 ++
 5 files changed, 248 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/typec/port-mapper.c

Comments

kernel test robot March 29, 2021, 3:15 p.m. UTC | #1
Hi Heikki,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20210326]
[also build test ERROR on v5.12-rc5]
[cannot apply to usb/usb-testing chrome-platform-linux/for-next linus/master v5.12-rc5 v5.12-rc4 v5.12-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Heikki-Krogerus/usb-Linking-ports-to-their-Type-C-connectors/20210329-164859
base:    931294922e65a23e1aad6398b9ae02df74044679
config: nios2-allyesconfig (attached as .config)
compiler: nios2-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/e86970d606657b7ce44bbdb80f4d25048bca710f
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Heikki-Krogerus/usb-Linking-ports-to-their-Type-C-connectors/20210329-164859
        git checkout e86970d606657b7ce44bbdb80f4d25048bca710f
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=nios2 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/usb/typec/port-mapper.c:156:5: error: redefinition of 'typec_link_port'
     156 | int typec_link_port(struct device *port)
         |     ^~~~~~~~~~~~~~~
   In file included from drivers/usb/typec/port-mapper.c:11:
   include/linux/usb/typec.h:306:19: note: previous definition of 'typec_link_port' was here
     306 | static inline int typec_link_port(struct device *port)
         |                   ^~~~~~~~~~~~~~~
>> drivers/usb/typec/port-mapper.c:215:6: error: redefinition of 'typec_unlink_port'
     215 | void typec_unlink_port(struct device *port)
         |      ^~~~~~~~~~~~~~~~~
   In file included from drivers/usb/typec/port-mapper.c:11:
   include/linux/usb/typec.h:311:20: note: previous definition of 'typec_unlink_port' was here
     311 | static inline void typec_unlink_port(struct device *port) { }
         |                    ^~~~~~~~~~~~~~~~~


vim +/typec_unlink_port +215 drivers/usb/typec/port-mapper.c

   146	
   147	/**
   148	 * typec_link_port - Link a port to its connector
   149	 * @port: The port device
   150	 *
   151	 * Find the connector of @port and create symlink named "connector" for it.
   152	 * Returns 0 on success, or errno in case of a failure.
   153	 *
   154	 * NOTE. The function increments the reference count of @port on success.
   155	 */
 > 156	int typec_link_port(struct device *port)
   157	{
   158		struct device *connector;
   159		struct port_node *node;
   160		int ret = 0;
   161	
   162		node = create_port_node(port);
   163		if (IS_ERR(node))
   164			return PTR_ERR(node);
   165	
   166		connector = find_connector(node);
   167		if (!connector)
   168			goto remove_node;
   169	
   170		ret = link_port(to_typec_port(connector), node);
   171		if (ret)
   172			goto put_connector;
   173	
   174		return 0;
   175	
   176	put_connector:
   177		put_device(connector);
   178	remove_node:
   179		remove_port_node(node);
   180	
   181		return ret;
   182	}
   183	EXPORT_SYMBOL_GPL(typec_link_port);
   184	
   185	static int port_match_and_unlink(struct device *connector, void *port)
   186	{
   187		struct port_node *node;
   188		struct port_node *tmp;
   189		int ret = 0;
   190	
   191		if (!is_typec_port(connector))
   192			return 0;
   193	
   194		mutex_lock(&to_typec_port(connector)->port_list_lock);
   195		list_for_each_entry_safe(node, tmp, &to_typec_port(connector)->port_list, list) {
   196			ret = node->dev == port;
   197			if (ret) {
   198				unlink_port(to_typec_port(connector), node);
   199				remove_port_node(node);
   200				put_device(connector);
   201				break;
   202			}
   203		}
   204		mutex_unlock(&to_typec_port(connector)->port_list_lock);
   205	
   206		return ret;
   207	}
   208	
   209	/**
   210	 * typec_unlink_port - Unlink port from its connector
   211	 * @port: The port device
   212	 *
   213	 * Removes the symlink "connector" and decrements the reference count of @port.
   214	 */
 > 215	void typec_unlink_port(struct device *port)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot March 29, 2021, 4:06 p.m. UTC | #2
Hi Heikki,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20210326]
[also build test ERROR on v5.12-rc5]
[cannot apply to usb/usb-testing chrome-platform-linux/for-next linus/master v5.12-rc5 v5.12-rc4 v5.12-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Heikki-Krogerus/usb-Linking-ports-to-their-Type-C-connectors/20210329-164859
base:    931294922e65a23e1aad6398b9ae02df74044679
config: m68k-randconfig-c004-20210329 (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/e86970d606657b7ce44bbdb80f4d25048bca710f
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Heikki-Krogerus/usb-Linking-ports-to-their-Type-C-connectors/20210329-164859
        git checkout e86970d606657b7ce44bbdb80f4d25048bca710f
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=m68k 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> drivers/usb/typec/port-mapper.c:156:5: error: redefinition of 'typec_link_port'
     156 | int typec_link_port(struct device *port)
         |     ^~~~~~~~~~~~~~~
   In file included from drivers/usb/typec/port-mapper.c:11:
   include/linux/usb/typec.h:306:19: note: previous definition of 'typec_link_port' was here
     306 | static inline int typec_link_port(struct device *port)
         |                   ^~~~~~~~~~~~~~~
>> drivers/usb/typec/port-mapper.c:215:6: error: redefinition of 'typec_unlink_port'
     215 | void typec_unlink_port(struct device *port)
         |      ^~~~~~~~~~~~~~~~~
   In file included from drivers/usb/typec/port-mapper.c:11:
   include/linux/usb/typec.h:311:20: note: previous definition of 'typec_unlink_port' was here
     311 | static inline void typec_unlink_port(struct device *port) { }
         |                    ^~~~~~~~~~~~~~~~~


vim +/typec_link_port +156 drivers/usb/typec/port-mapper.c

   146	
   147	/**
   148	 * typec_link_port - Link a port to its connector
   149	 * @port: The port device
   150	 *
   151	 * Find the connector of @port and create symlink named "connector" for it.
   152	 * Returns 0 on success, or errno in case of a failure.
   153	 *
   154	 * NOTE. The function increments the reference count of @port on success.
   155	 */
 > 156	int typec_link_port(struct device *port)
   157	{
   158		struct device *connector;
   159		struct port_node *node;
   160		int ret = 0;
   161	
   162		node = create_port_node(port);
   163		if (IS_ERR(node))
   164			return PTR_ERR(node);
   165	
   166		connector = find_connector(node);
   167		if (!connector)
   168			goto remove_node;
   169	
   170		ret = link_port(to_typec_port(connector), node);
   171		if (ret)
   172			goto put_connector;
   173	
   174		return 0;
   175	
   176	put_connector:
   177		put_device(connector);
   178	remove_node:
   179		remove_port_node(node);
   180	
   181		return ret;
   182	}
   183	EXPORT_SYMBOL_GPL(typec_link_port);
   184	
   185	static int port_match_and_unlink(struct device *connector, void *port)
   186	{
   187		struct port_node *node;
   188		struct port_node *tmp;
   189		int ret = 0;
   190	
   191		if (!is_typec_port(connector))
   192			return 0;
   193	
   194		mutex_lock(&to_typec_port(connector)->port_list_lock);
   195		list_for_each_entry_safe(node, tmp, &to_typec_port(connector)->port_list, list) {
   196			ret = node->dev == port;
   197			if (ret) {
   198				unlink_port(to_typec_port(connector), node);
   199				remove_port_node(node);
   200				put_device(connector);
   201				break;
   202			}
   203		}
   204		mutex_unlock(&to_typec_port(connector)->port_list_lock);
   205	
   206		return ret;
   207	}
   208	
   209	/**
   210	 * typec_unlink_port - Unlink port from its connector
   211	 * @port: The port device
   212	 *
   213	 * Removes the symlink "connector" and decrements the reference count of @port.
   214	 */
 > 215	void typec_unlink_port(struct device *port)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
index a820e6e8c1ffc..1e1868832b8d8 100644
--- a/drivers/usb/typec/Makefile
+++ b/drivers/usb/typec/Makefile
@@ -3,7 +3,7 @@ 
 CFLAGS_tps6598x.o		:= -I$(src)
 
 obj-$(CONFIG_TYPEC)		+= typec.o
-typec-y				:= class.o mux.o bus.o
+typec-y				:= class.o mux.o bus.o port-mapper.o
 obj-$(CONFIG_TYPEC)		+= altmodes/
 obj-$(CONFIG_TYPEC_TCPM)	+= tcpm/
 obj-$(CONFIG_TYPEC_UCSI)	+= ucsi/
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index d3e1002386357..ff199e2d26c7b 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -18,7 +18,7 @@ 
 
 static DEFINE_IDA(typec_index_ida);
 
-static struct class typec_class = {
+struct class typec_class = {
 	.name = "typec",
 	.owner = THIS_MODULE,
 };
@@ -1601,6 +1601,7 @@  static void typec_release(struct device *dev)
 	ida_destroy(&port->mode_ids);
 	typec_switch_put(port->sw);
 	typec_mux_put(port->mux);
+	free_pld(port->pld);
 	kfree(port->cap);
 	kfree(port);
 }
@@ -1983,6 +1984,8 @@  struct typec_port *typec_register_port(struct device *parent,
 
 	ida_init(&port->mode_ids);
 	mutex_init(&port->port_type_lock);
+	mutex_init(&port->port_list_lock);
+	INIT_LIST_HEAD(&port->port_list);
 
 	port->id = id;
 	port->ops = cap->ops;
@@ -2024,6 +2027,8 @@  struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
+	port->pld = get_pld(&port->dev);
+
 	return port;
 }
 EXPORT_SYMBOL_GPL(typec_register_port);
diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
index d414be58d122e..52294f7020a8b 100644
--- a/drivers/usb/typec/class.h
+++ b/drivers/usb/typec/class.h
@@ -54,6 +54,11 @@  struct typec_port {
 
 	const struct typec_capability	*cap;
 	const struct typec_operations   *ops;
+
+	struct list_head		port_list;
+	struct mutex			port_list_lock; /* Port list lock */
+
+	void				*pld;
 };
 
 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
@@ -72,5 +77,9 @@  extern const struct device_type typec_port_dev_type;
 #define is_typec_port(dev) ((dev)->type == &typec_port_dev_type)
 
 extern struct class typec_mux_class;
+extern struct class typec_class;
+
+void *get_pld(struct device *dev);
+void free_pld(void *pld);
 
 #endif /* __USB_TYPEC_CLASS__ */
diff --git a/drivers/usb/typec/port-mapper.c b/drivers/usb/typec/port-mapper.c
new file mode 100644
index 0000000000000..5bee7a97242fe
--- /dev/null
+++ b/drivers/usb/typec/port-mapper.c
@@ -0,0 +1,219 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * USB Type-C Connector Class Port Mapping Utility
+ *
+ * Copyright (C) 2021, Intel Corporation
+ * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
+ */
+
+#include <linux/acpi.h>
+#include <linux/usb.h>
+#include <linux/usb/typec.h>
+
+#include "class.h"
+
+struct port_node {
+	struct list_head list;
+	struct device *dev;
+	void *pld;
+};
+
+static int acpi_pld_match(const struct acpi_pld_info *pld1,
+			  const struct acpi_pld_info *pld2)
+{
+	if (!pld1 || !pld2)
+		return 0;
+
+	/*
+	 * To speed things up, first checking only the group_position. It seems
+	 * to often have the first unique value in the _PLD.
+	 */
+	if (pld1->group_position == pld2->group_position)
+		return !memcmp(pld1, pld2, sizeof(struct acpi_pld_info));
+
+	return 0;
+}
+
+void *get_pld(struct device *dev)
+{
+#ifdef CONFIG_ACPI
+	struct acpi_pld_info *pld;
+	acpi_status status;
+
+	if (!has_acpi_companion(dev))
+		return NULL;
+
+	status = acpi_get_physical_device_location(ACPI_HANDLE(dev), &pld);
+	if (ACPI_FAILURE(status))
+		return NULL;
+
+	return pld;
+#else
+	return NULL;
+#endif
+}
+
+void free_pld(void *pld)
+{
+#ifdef CONFIG_ACPI
+	ACPI_FREE(pld);
+#endif
+}
+
+static int __link_port(struct typec_port *con, struct port_node *node)
+{
+	int ret;
+
+	ret = sysfs_create_link(&node->dev->kobj, &con->dev.kobj, "connector");
+	if (ret)
+		return ret;
+
+	ret = sysfs_create_link(&con->dev.kobj, &node->dev->kobj,
+				dev_name(node->dev));
+	if (ret) {
+		sysfs_remove_link(&node->dev->kobj, "connector");
+		return ret;
+	}
+
+	list_add_tail(&node->list, &con->port_list);
+
+	return 0;
+}
+
+static int link_port(struct typec_port *con, struct port_node *node)
+{
+	int ret;
+
+	mutex_lock(&con->port_list_lock);
+	ret = __link_port(con, node);
+	mutex_unlock(&con->port_list_lock);
+
+	return ret;
+}
+
+static void __unlink_port(struct typec_port *con, struct port_node *node)
+{
+	sysfs_remove_link(&con->dev.kobj, dev_name(node->dev));
+	sysfs_remove_link(&node->dev->kobj, "connector");
+	list_del(&node->list);
+}
+
+static void unlink_port(struct typec_port *con, struct port_node *node)
+{
+	mutex_lock(&con->port_list_lock);
+	__unlink_port(con, node);
+	mutex_unlock(&con->port_list_lock);
+}
+
+static struct port_node *create_port_node(struct device *port)
+{
+	struct port_node *node;
+
+	node = kzalloc(sizeof(*node), GFP_KERNEL);
+	if (!node)
+		return ERR_PTR(-ENOMEM);
+
+	node->dev = get_device(port);
+	node->pld = get_pld(port);
+
+	return node;
+}
+
+static void remove_port_node(struct port_node *node)
+{
+	put_device(node->dev);
+	free_pld(node->pld);
+	kfree(node);
+}
+
+static int connector_match(struct device *dev, const void *data)
+{
+	const struct port_node *node = data;
+
+	if (!is_typec_port(dev))
+		return 0;
+
+	return acpi_pld_match(to_typec_port(dev)->pld, node->pld);
+}
+
+static struct device *find_connector(struct port_node *node)
+{
+	if (!node->pld)
+		return NULL;
+
+	return class_find_device(&typec_class, NULL, node, connector_match);
+}
+
+/**
+ * typec_link_port - Link a port to its connector
+ * @port: The port device
+ *
+ * Find the connector of @port and create symlink named "connector" for it.
+ * Returns 0 on success, or errno in case of a failure.
+ *
+ * NOTE. The function increments the reference count of @port on success.
+ */
+int typec_link_port(struct device *port)
+{
+	struct device *connector;
+	struct port_node *node;
+	int ret = 0;
+
+	node = create_port_node(port);
+	if (IS_ERR(node))
+		return PTR_ERR(node);
+
+	connector = find_connector(node);
+	if (!connector)
+		goto remove_node;
+
+	ret = link_port(to_typec_port(connector), node);
+	if (ret)
+		goto put_connector;
+
+	return 0;
+
+put_connector:
+	put_device(connector);
+remove_node:
+	remove_port_node(node);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(typec_link_port);
+
+static int port_match_and_unlink(struct device *connector, void *port)
+{
+	struct port_node *node;
+	struct port_node *tmp;
+	int ret = 0;
+
+	if (!is_typec_port(connector))
+		return 0;
+
+	mutex_lock(&to_typec_port(connector)->port_list_lock);
+	list_for_each_entry_safe(node, tmp, &to_typec_port(connector)->port_list, list) {
+		ret = node->dev == port;
+		if (ret) {
+			unlink_port(to_typec_port(connector), node);
+			remove_port_node(node);
+			put_device(connector);
+			break;
+		}
+	}
+	mutex_unlock(&to_typec_port(connector)->port_list_lock);
+
+	return ret;
+}
+
+/**
+ * typec_unlink_port - Unlink port from its connector
+ * @port: The port device
+ *
+ * Removes the symlink "connector" and decrements the reference count of @port.
+ */
+void typec_unlink_port(struct device *port)
+{
+	class_for_each_device(&typec_class, NULL, port, port_match_and_unlink);
+}
+EXPORT_SYMBOL_GPL(typec_unlink_port);
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 91b4303ca305c..604cd2da15e83 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -298,4 +298,17 @@  int typec_find_port_data_role(const char *name);
 void typec_partner_set_svdm_version(struct typec_partner *partner,
 				    enum usb_pd_svdm_ver svdm_version);
 int typec_get_negotiated_svdm_version(struct typec_port *port);
+
+#if IS_ENABLED(CONFIG_ACPI) && IS_REACHABLE(CONFIG_TYPEC)
+int typec_link_port(struct device *port);
+void typec_unlink_port(struct device *port);
+#else
+static inline int typec_link_port(struct device *port)
+{
+	return 0;
+}
+
+static inline void typec_unlink_port(struct device *port) { }
+#endif
+
 #endif /* __LINUX_USB_TYPEC_H */