usb: Linking ports to their Type-C connectors
Subsystems (2)
Heikki Krogerus
maintainerv6 cover usb: Linking ports to their Type-C connectors
Hi,
These are the remaining four patches of the series, now rebased on top of the latest usb-next. No other changes.
v5 cover letter:
I have to use IS_REACHABLE() instead of IS_ENABLED() also in include/linux/usb.h. Otherwise compilation will fail if the Type-C class is build-in while USB is a module.
I'm sorry for re-sending these so fast, immediately after v4. Normally I would wait, but I'll be taking a short vacation starting from right now, and I'm still hoping to get these into v5.13.
v4 cover letter:
One more version. I used #ifdef when I should have used #if IS_DEFINED(). Thanks Guenter for pointing that out.
I'm sending this version right away because of the holidays. I'm not changing anything else except that one fix.
v3: cover letter:
Third version: ifdefs now in the header files as they should be.
v2 cover letter:
This is the second version of this series. The "Iterator for ports" patch is now moved to the end of the series (5/6).
I'm now using usb_for_each_dev() in usb_for_each_port like Alan suggested, and I'm now using usb_port_peer_mutex to lock the ports while we're dealing with them in __each_hub().
The original cover letter:
Adding a simple function typec_link_port() that can be used to create a symlink "connector" that points to the USB Type-C connector of a port. It is used with USB ports initially, but hopefully later also with other things like DisplayPorts.
Being able to see which connector is connected to a port is important in general, but it is really important when for example the data or power role of a device needs to swapped. The user probable wants to know which USB device is disconnected if role swap on a USB Type-C connector is executed.
Hope these are OK.
thanks,
Heikki Krogerus (4):
usb: typec: Port mapping utility
usb: Link the ports to the connectors they are attached to
usb: Iterator for ports
usb: typec: Link all ports during connector registration
Documentation/ABI/testing/sysfs-bus-usb | 9 + drivers/usb/core/port.c | 3 + drivers/usb/core/usb.c | 46 ++++ drivers/usb/typec/Makefile | 2 +- drivers/usb/typec/class.c | 12 +- drivers/usb/typec/class.h | 9 + drivers/usb/typec/port-mapper.c | 277 ++++++++++++++++++++++++ include/linux/usb.h | 9 + include/linux/usb/typec.h | 13 ++ 9 files changed, 377 insertions(+), 3 deletions(-) create mode 100644 drivers/usb/typec/port-mapper.c
Signature · 1 line
2.30.2
Heikki Krogerus
maintainerPATCH v6 1/4 usb: typec: Port mapping utility
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.
--- 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
drivers/usb/typec/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 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/
drivers/usb/typec/class.c +6 -1
@@ -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);
drivers/usb/typec/class.h +9 -0
@@ -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__ */
drivers/usb/typec/port-mapper.c new file +219 -0
219 lines not on this page · raw message
include/linux/usb/typec.h +13 -0
@@ -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_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 */
Signature · 1 line
2.30.2
Heikki Krogerus
maintainerPATCH v6 2/4 usb: Link the ports to the connectors they are attached to
In reply to Heikki KrogerusCreating link to the USB Type-C connector for every new port that is added when possible.
--- Documentation/ABI/testing/sysfs-bus-usb | 9 +++++++++ drivers/usb/core/port.c | 3 +++ 2 files changed, 12 insertions(+)
Documentation/ABI/testing/sysfs-bus-usb +9 -0
@@ -255,6 +255,15 @@ Description: is permitted, "u2" if only u2 is permitted, "u1_u2" if both u1 and u2 are permitted. +What: /sys/bus/usb/devices/.../(hub interface)/portX/connector +Date: April 2021 +Contact: Heikki Krogerus <[email protected]> +Description: + Link to the USB Type-C connector when available. This link is + only created when USB Type-C Connector Class is enabled, and + only if the system firmware is capable of describing the + connection between a port and its connector. + What: /sys/bus/usb/devices/.../power/usb2_lpm_l1_timeout Date: May 2013 Contact: Mathias Nyman <[email protected]>
drivers/usb/core/port.c +3 -0
@@ -9,6 +9,7 @@#include <linux/slab.h> #include <linux/pm_qos.h> +#include <linux/usb/typec.h> #include "hub.h"
@@ -576,6 +577,7 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1) }find_and_link_peer (hub, port1); +typec_link_port (&port_dev->dev);/* * Enable runtime pm and hold a refernce that hub_configure()
@@ -619,5 +621,6 @@ void usb_hub_remove_port_device(struct usb_hub *hub, int port1) peer = port_dev->peer;if (peer)unlink_peers (port_dev, peer); +typec_unlink_port (&port_dev->dev);device_unregister (&port_dev->dev); }
Signature · 1 line
2.30.2
Re: [PATCH v6 2/4] usb: Link the ports to the connectors they are attached to
Hi,
Quoted 4 lines · Heikki Krogerus
Creating link to the USB Type-C connector for every new port that is added when possible.
Signed-off-by Heikki Krogerus <[email protected]>
Did this patch eventually get merged?
I somehow don't see it in the "master" kernel branch [1], although I do see the commit in that repo [2].
[1]:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/usb/core/port.c
Best regards,
-Prashant
Re: [PATCH v6 2/4] usb: Link the ports to the connectors they are attached to
Quoted 12 lines · Prashant Malani
Hi,
Quoted 4 lines · Heikki Krogerus
Creating link to the USB Type-C connector for every new port that is added when possible.
Signed-off-by Heikki Krogerus <[email protected]>Did this patch eventually get merged?
I somehow don't see it in the "master" kernel branch [1], although I do see the commit in that repo [2].
It was applied but later reverted with commit 5bdb080f9603 because it created a module dependency cycle.
Guenter
Re: [PATCH v6 2/4] usb: Link the ports to the connectors they are attached to
Quoted 10 lines · Guenter Roeck
Quoted 8 lines · Prashant Malani
Hi,
Quoted 4 lines · Heikki Krogerus
Creating link to the USB Type-C connector for every new port that is added when possible.
Signed-off-by Heikki Krogerus <[email protected]>
Quoted 8 lines
Quoted 4 lines
Did this patch eventually get merged?
I somehow don't see it in the "master" kernel branch [1], although I do see the commit in that repo [2].It was applied but later reverted with commit
5bdb080f9603because it created a module dependency cycle.
Ah I see. Thanks Guenter.
Hi Heikki, I was interested in knowing if relanding this is on your radar.
Best regards,
-Prashant
Heikki Krogerus
maintainerRe: [PATCH v6 2/4] usb: Link the ports to the connectors they are attached to
Quoted 16 lines · Prashant Malani
Quoted 15 lines · Guenter Roeck
Quoted 12 lines · Prashant Malani
Hi,
Quoted 4 lines · Heikki Krogerus
Creating link to the USB Type-C connector for every new port that is added when possible.
Signed-off-by Heikki Krogerus <[email protected]>Did this patch eventually get merged?
I somehow don't see it in the "master" kernel branch [1], although I do see the commit in that repo [2].
Quoted 6 lines
Quoted 3 lines
It was applied but later reverted with commit
5bdb080f9603because it created a module dependency cycle.Ah I see. Thanks Guenter.
Hi Heikki, I was interested in knowing if relanding this is on your radar.
Ah, I'm sorry guys, I forgot about this patch. I'll put it back to my task list. If you are interested in fixing this, and have time to do that, go right ahead. Let me know.
thanks,
Signature · 1 line
heikki
Heikki Krogerus
maintainerPATCH v6 3/4 usb: Iterator for ports
In reply to Heikki KrogerusIntroducing usb_for_each_port(). It works the same way as usb_for_each_dev(), but instead of going through every USB device in the system, it walks through the USB ports in the system.
--- drivers/usb/core/usb.c | 46 ++++++++++++++++++++++++++++++++++++++++++ include/linux/usb.h | 9 +++++++++ 2 files changed, 55 insertions(+)
drivers/usb/core/usb.c +46 -0
@@ -398,6 +398,52 @@ int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *)) }EXPORT_SYMBOL_GPL (usb_for_each_dev); +struct each_hub_arg { +void *data; +int (*fn)(struct device *,void *); +}; + +static int __each_hub (struct usb_device *hdev,void *data) +{ +struct each_hub_arg *arg = (struct each_hub_arg *)data; +struct usb_hub *hub; +int ret =0 ; +int i; + + hub =usb_hub_to_struct_hub (hdev); +if (!hub) +return 0 ; + +mutex_lock (&usb_port_peer_mutex); + +for (i =0 ; i < hdev->maxchild; i++) { + ret = arg->fn (&hub->ports[i]->dev, arg->data); +if (ret) +break ; + } + +mutex_unlock (&usb_port_peer_mutex); + +return ret; +} + +/** +* usb_for_each_port - interate over all USB ports in the system +* @data: data pointer that will be handed to the callback function +* @fn: callback function to be called for each USB port +* +* Iterate over all USB ports and call @fn for each, passing it @data. If it +* returns anything other than 0, we break the iteration prematurely and return +* that value. +*/ +int usb_for_each_port (void *data,int (*fn)(struct device *,void *)) +{ +struct each_hub_arg arg = {data, fn}; + +return usb_for_each_dev (&arg, __each_hub); +} +EXPORT_SYMBOL_GPL (usb_for_each_port); +/** * usb_release_dev - free a usb device structure when all users of it are finished. * @dev: device that's been disconnected
include/linux/usb.h +9 -0
@@ -882,6 +882,15 @@ extern struct usb_host_interface *usb_find_alt_setting(unsigned int iface_num, unsigned int alt_num); +#if IS_REACHABLE (CONFIG_USB ) +int usb_for_each_port (void *data,int (*fn)(struct device *,void *)); +#else +static inline int usb_for_each_port (void *data,int (*fn)(struct device *,void *)) +{ +return 0 ; +} +#endif +/* port claiming functions */ int usb_hub_claim_port (struct usb_device *hdev,unsigned port1,struct usb_dev_state *owner);
Signature · 1 line
2.30.2
Heikki Krogerus
maintainerPATCH v6 4/4 usb: typec: Link all ports during connector registration
In reply to Heikki KrogerusThe connectors may be registered after the ports, so the "connector" links need to be created for the ports also when ever a new connector gets registered.
--- drivers/usb/typec/class.c | 9 +++-- drivers/usb/typec/class.h | 4 +-- drivers/usb/typec/port-mapper.c | 62 +++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 7 deletions(-)
drivers/usb/typec/class.c +6 -3
@@ -1601,7 +1601,6 @@ 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); }
@@ -2027,7 +2026,9 @@ struct typec_port *typec_register_port(struct device *parent,return ERR_PTR (ret); } - port->pld =get_pld (&port->dev); + ret =typec_link_ports (port); +if (ret) +dev_warn (&port->dev,"failed to create symlinks (%d)\n" , ret);return port; }
@@ -2041,8 +2042,10 @@ EXPORT_SYMBOL_GPL(typec_register_port);*/ void typec_unregister_port (struct typec_port *port) { -if (!IS_ERR_OR_NULL (port)) +if (!IS_ERR_OR_NULL (port)) { +typec_unlink_ports (port);device_unregister (&port->dev); + } }EXPORT_SYMBOL_GPL (typec_unregister_port);
drivers/usb/typec/class.h +2 -2
@@ -79,7 +79,7 @@ extern const struct device_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); +int typec_link_ports (struct typec_port *connector); +void typec_unlink_ports (struct typec_port *connector);#endif /* __USB_TYPEC_CLASS__ */
drivers/usb/typec/port-mapper.c +60 -2
77 lines not on this page · raw message
Signature · 1 line
2.30.2