usb: Link USB devices with their USB Type-C partner counterparts
Subsystems (2)
Heikki Krogerus
maintainercover usb: Link USB devices with their USB Type-C partner counterparts
Hi,
The idea for these comes from Benson. By letting the typec drivers know what exactly was enumerated, they can power off everything that's not needed.
The USB device and USB Type-C partner device will also be linked together with symlinks in sysfs. If I uderstood correctly, you guys (from ChromiumOS team) have already user for that. Let me know if I got that wrong.
The original RFC:
https://lore.kernel.org/linux-usb/[email protected]/T/
thanks,
Heikki Krogerus (2):
usb: typec: Link enumerated USB devices with Type-C partner
usb: Inform the USB Type-C class about enumerated devices
Documentation/ABI/testing/sysfs-bus-usb | 9 ++ drivers/usb/core/hub.c | 4 + drivers/usb/core/hub.h | 3 + drivers/usb/core/port.c | 19 ++++- drivers/usb/typec/class.c | 108 ++++++++++++++++++++++-- drivers/usb/typec/class.h | 16 ++++ drivers/usb/typec/port-mapper.c | 9 +- include/linux/usb/typec.h | 37 ++++++++ 8 files changed, 193 insertions(+), 12 deletions(-)
Signature · 1 line
2.40.1
Heikki Krogerus
maintainerPATCH 1/2 usb: typec: Link enumerated USB devices with Type-C partner
Adding functions that USB hub code can use to inform the Type-C class about connected USB devices.
Once taken into use, it will allow the Type-C port drivers to power off components that are not needed, for example if USB2 device is enumerated, everything that is only relevant for USB3 (retimers, etc.), can be powered off.
This will also create a symlink "typec" for the USB devices pointing to the USB Type-C partner device.
--- Documentation/ABI/testing/sysfs-bus-usb | 9 ++ drivers/usb/typec/class.c | 108 ++++++++++++++++++++++-- drivers/usb/typec/class.h | 16 ++++ drivers/usb/typec/port-mapper.c | 9 +- include/linux/usb/typec.h | 37 ++++++++ 5 files changed, 169 insertions(+), 10 deletions(-)
Documentation/ABI/testing/sysfs-bus-usb +9 -0
@@ -313,6 +313,15 @@ Description: Inter-Chip SSIC devices support asymmetric lanes up to 4 lanes per direction. Devices before USB 3.2 are single lane (tx_lanes = 1) +What: /sys/bus/usb/devices/.../typec +Date: November 2023 +Contact: Heikki Krogerus <[email protected]> +Description: + Symlink to the USB Type-C partner device. USB Type-C partner + represents the component that communicates over the + Configuration Channel (CC signal on USB Type-C connectors and + cables) with the local port. + What: /sys/bus/usb/devices/usbX/bAlternateSetting Description: The current interface alternate setting number, in decimal.
drivers/usb/typec/class.c +100 -8
165 lines not on this page · raw message
drivers/usb/typec/class.h +16 -0
@@ -8,6 +8,7 @@struct typec_mux ;struct typec_switch ; +struct usb_device ;struct typec_plug {struct device dev;
@@ -35,6 +36,9 @@ struct typec_partner {enum usb_pd_svdm_ver svdm_version;struct usb_power_delivery *pd; + +void (*attach)(struct typec_partner *partner,struct device *dev); +void (*deattach)(struct typec_partner *partner,struct device *dev); };struct typec_port {
@@ -59,6 +63,18 @@ struct typec_port {const struct typec_capability *cap;const struct typec_operations *ops; + +struct typec_connector con; + +/* +* REVISIT: Only USB devices for now. If there are others, these need to +* be converted into a list. +* +* NOTE: These may be registered first before the typec_partner, so they +* will always have to be kept here instead of struct typec_partner. +*/ +struct device *usb2_dev; +struct device *usb3_dev; };#define to_typec_port (_dev_) container_of(_dev_, struct typec_port, dev)
drivers/usb/typec/port-mapper.c +7 -2
@@ -8,17 +8,22 @@#include <linux/acpi.h> #include <linux/component.h> +#include <linux/usb.h> #include "class.h" static int typec_aggregate_bind (struct device *dev) { -return component_bind_all (dev,NULL ); +struct typec_port *port =to_typec_port (dev); + +return component_bind_all (dev, &port->con); }static void typec_aggregate_unbind (struct device *dev) { -component_unbind_all (dev,NULL ); +struct typec_port *port =to_typec_port (dev); + +component_unbind_all (dev, &port->con); }static const struct component_master_ops typec_aggregate_ops = {
include/linux/usb/typec.h +37 -0
@@ -202,6 +202,8 @@ struct typec_cable_desc {* @accessory: Audio, Debug or none. * @identity: Discover Identity command data * @pd_revision: USB Power Delivery Specification Revision if supported +* @attach: Notification about attached USB device +* @deattach: Notification about removed USB device * * Details about a partner that is attached to USB Type-C port. If @identity * member exists when partner is registered, a directory named "identity" is
@@ -217,6 +219,9 @@ struct typec_partner_desc {enum typec_accessory accessory;struct usb_pd_identity *identity;u16 pd_revision;/* 0300H = "3.0" */ + +void (*attach)(struct typec_partner *partner,struct device *dev); +void (*deattach)(struct typec_partner *partner,struct device *dev); };/**
@@ -335,4 +340,36 @@ int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_int typec_partner_set_usb_power_delivery (struct typec_partner *partner,struct usb_power_delivery *pd); +/** +* struct typec_connector - Representation of Type-C port for external drivers +* @attach: notification about device removal +* @deattach: notification about device removal +* +* Drivers that control the USB and other ports (DisplayPorts, etc.), that are +* connected to the Type-C connectors, can use these callbacks to inform the +* Type-C connector class about connections and disconnections. That information +* can then be used by the typec-port drivers to power on or off parts that are +* needed or not needed - as an example, in USB mode if USB2 device is +* enumerated, USB3 components (retimers, phys, and what have you) do not need +* to be powered on. +* +* The attached (enumerated) devices will be liked with the typec-partner device. +*/ +struct typec_connector { +void (*attach)(struct typec_connector *con,struct device *dev); +void (*deattach)(struct typec_connector *con,struct device *dev); +}; + +static inline void typec_attach (struct typec_connector *con,struct device *dev) +{ +if (con && con->attach) + con->attach (con, dev); +} + +static inline void typec_deattach (struct typec_connector *con,struct device *dev) +{ +if (con && con->deattach) + con->deattach (con, dev); +} +#endif /* __LINUX_USB_TYPEC_H */
Signature · 1 line
2.40.1
Heikki Krogerus
maintainerPATCH 2/2 usb: Inform the USB Type-C class about enumerated devices
In reply to Heikki KrogerusThe Type-C port drivers can make PM related decisions based on is the device USB3 or USB2.
--- drivers/usb/core/hub.c | 4 ++++ drivers/usb/core/hub.h | 3 +++ drivers/usb/core/port.c | 19 +++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-)
drivers/usb/core/hub.c +4 -0
@@ -2274,6 +2274,8 @@ void usb_disconnect(struct usb_device **pdev)*/ if (!test_and_set_bit (port1, hub->child_usage_bits))pm_runtime_get_sync (&port_dev->dev); + +typec_deattach (port_dev->connector, &udev->dev); }usb_remove_ep_devs (&udev->ep0);
@@ -2620,6 +2622,8 @@ int usb_new_device(struct usb_device *udev)if (!test_and_set_bit (port1, hub->child_usage_bits))pm_runtime_get_sync (&port_dev->dev); + +typec_attach (port_dev->connector, &udev->dev); } (void )usb_create_ep_devs (&udev->dev, &udev->ep0, udev);
drivers/usb/core/hub.h +3 -0
@@ -14,6 +14,7 @@#include <linux/usb.h> #include <linux/usb/ch11.h> #include <linux/usb/hcd.h> +#include <linux/usb/typec.h> #include "usb.h" struct usb_hub {
@@ -82,6 +83,7 @@ struct usb_hub {* @dev: generic device interface * @port_owner: port's owner * @peer: related usb2 and usb3 ports (share the same connector) +* @connector: USB Type-C connector * @req: default pm qos request for hubs without port power control * @connect_type: port's connect type * @state: device state of the usb device attached to the port
@@ -100,6 +102,7 @@ struct usb_port {struct device dev;struct usb_dev_state *port_owner;struct usb_port *peer; +struct typec_connector *connector;struct dev_pm_qos_request *req;enum usb_port_connect_type connect_type;enum usb_device_state state;
drivers/usb/core/port.c +17 -2
@@ -653,6 +653,7 @@ static void find_and_link_peer(struct usb_hub *hub, int port1)static int connector_bind (struct device *dev,struct device *connector,void *data) { +struct usb_port *port_dev =to_usb_port (dev);int ret; ret =sysfs_create_link (&dev->kobj, &connector->kobj,"connector" );
@@ -660,16 +661,30 @@ static int connector_bind(struct device *dev, struct device *connector, void *dareturn ret; ret =sysfs_create_link (&connector->kobj, &dev->kobj,dev_name (dev)); -if (ret) +if (ret) {sysfs_remove_link (&dev->kobj,"connector" ); +return ret; + } + + port_dev->connector = data; + +/* +* If there is already USB device connected to the port, letting the +* Type-C connector know about it immediately. +*/ +if (port_dev->child) +typec_attach (port_dev->connector, &port_dev->child->dev); -return ret; +return 0 ; }static void connector_unbind (struct device *dev,struct device *connector,void *data) { +struct usb_port *port_dev =to_usb_port (dev); +sysfs_remove_link (&connector->kobj,dev_name (dev));sysfs_remove_link (&dev->kobj,"connector" ); + port_dev->connector =NULL ; }static const struct component_ops connector_ops = {
Signature · 1 line
2.40.1