Skip to contentSkip to conversation

USB Type-C Connector class

PATCH In review Heikki Krogerus 5 participants 28 messages Sep 19, 2016 – Nov 17, 2016

Subsystems (5)
lore
Share & tools
lore

Heikki Krogerus

author maintainer
#0 lore raw

cover USB Type-C Connector class

The USB Type-C class is meant to provide unified interface to the userspace to present the USB Type-C ports in a system.

Changes since v9:
- Minor typec_wcove.c cleanup as proposed by Guenter Roeck. No function affect.

Changes since v8:
- checking sysfs_streq() result correctly in sysfs_strmatch
- fixed accessory check in supported_accessory_mode
- using "none" as the only string that can clear the preferred role

Changes since v7:
- Removed "type" attribute from partners
- Added supports_usb_power_delivery attribute for partner and cable

Changes since v6:
- current_vconn_role attr renamed to vconn_source (no API changes)
- Small documentation improvements proposed by Vincent Palatin

Changes since v5:
- Only updating the roles based on driver notifications
- Added MODULE_ALIAS for the WhiskeyCove module
- Including the patch that creates the actual platform device for the WhiskeyCove Type-C PHY in this series.

Changes since v4:
- Remove the port lock completely

Changes since v3:
- Documentation cleanup as proposed by Roger Quadros
- Setting partner altmodes member to NULL on removal and fixing a warning, as proposed by Guenter Roeck
- Added the following attributes for partners and cables:
* supports_usb_power_delivery
* id_header_vdo
- "id_header_vdo" is visible only when the partner or cable supports USB Power Delivery communication.
- Partner attribute "accessory" is hidden when the partner type is not "Accessory".

Changes since v2:
- Notification on role and alternate mode changes
- cleanups

Changes since v1:
- Completely rewrote alternate mode support
- Patners, cables and cable plugs presented as devices.

Heikki Krogerus (2):
usb: USB Type-C connector class
usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY

 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |   21 +
 drivers/usb/typec/Makefile                  |    2 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 drivers/usb/typec/typec_wcove.c             |  372 +++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 10 files changed, 2056 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 drivers/usb/typec/typec_wcove.c
 create mode 100644 include/linux/usb/typec.h
Signature · 1 line
2.9.3
author maintainer
#1 lore raw

PATCH 1/2 usb: USB Type-C connector class

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h
Documentation/ABI/testing/sysfs-class-typec new file +218 -0
new file mode 100644
index 0000000..dcca6bd
@@ -0,0 +1,218 @@
+USB Type-C port devices (eg. /sys/class/typec/usbc0/)
+
+What:		/sys/class/typec/<port>/current_data_role
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		The current USB data role the port is operating in. This
+		attribute can be used for requesting data role swapping on the
+		port. Swapping is supported as synchronous operation, so
+		write(2) to the attribute will not return until the operation
+		has finished. The attribute is notified about role changes so
+		that poll(2) on the attribute wakes up.
+
+		Valid values:
+		- host
+		- device
+
+What:		/sys/class/typec/<port>/current_power_role
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		The current power role of the port. This attribute can be used
+		to request power role swap on the port when the port supports
+		USB Power Delivery. Swapping is supported as synchronous
+		operation, so write(2) to the attribute will not return until
+		the operation has finished. The attribute is notified about role
+		changes so that poll(2) on the attribute wakes up.
+
+		Valid values:
+		- source
+		- sink
+
+What:		/sys/class/typec/<port>/vconn_source
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows is the port VCONN Source. This attribute can be used to
+		request VCONN swap to change the VCONN Source during connection
+		when both the port and the partner support USB Power Delivery.
+		Swapping is supported as synchronous operation, so write(2) to
+		the attribute will not return until the operation has finished.
+		The attribute is notified about VCONN source changes so that
+		poll(2) on the attribute wakes up.
+
+		Valid values are:
+		- 0 when the port is not the VCONN Source
+		- 1 when the port is the VCONN Source
+
+What:		/sys/class/typec/<port>/power_operation_mode
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows the current power operational mode the port is in.
+
+		Valid values:
+		- USB - Normal power levels defined in USB specifications
+		- BC1.2 - Power levels defined in Battery Charging Specification
+			  v1.2
+		- USB Type-C 1.5A - Higher 1.5A current defined in USB Type-C
+				    specification.
+		- USB Type-C 3.0A - Higher 3A current defined in USB Type-C
+				    specification.
+                - USB Power Delivery - The voltages and currents defined in USB
+				       Power Delivery specification
+
+What:		/sys/class/typec/<port>/preferred_role
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		The user space can notify the driver about the preferred role.
+		It should be handled as enabling of Try.SRC or Try.SNK, as
+		defined in USB Type-C specification, in the port drivers. By
+		default there is no preferred role.
+
+		Valid values:
+		- source
+		- sink
+		- none - to remove preference
+
+What:		/sys/class/typec/<port>/supported_accessory_modes
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Lists the Accessory Modes, defined in the USB Type-C
+		specification, the port supports.
+
+What:		/sys/class/typec/<port>/supported_data_roles
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Lists the USB data roles the port is capable of supporting.
+
+		Valid values:
+		- device
+		- host
+		- device, host (DRD as defined in USB Type-C specification v1.2)
+
+What:		/sys/class/typec/<port>/supported_power_roles
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Lists the power roles the port is capable of supporting.
+
+		Valid values:
+		- source
+		- sink
+		- source, sink (DRP as defined in USB Type-C specification v1.2)
+
+What:		/sys/class/typec/<port>/supports_usb_power_delivery
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows if the port supports USB Power Delivery.
+		- 1 if USB Power Delivery is supported
+		- 0 when it's not
+
+
+USB Type-C partner devices (eg. /sys/class/typec/usbc0-partner/)
+
+What:		/sys/class/typec/<port>-partner/accessory_mode
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows the Accessory Mode name, or "no" when the partner does not
+		support Accesory Modes. The Accessory Modes are defined in USB
+		Type-C Specification.
+
+What:		/sys/class/typec/<port>-partner/supports_usb_power_delivery
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows if the partner supports USB Power Delivery:
+		- 0 when USB Power Delivery is not supported
+		- 1 when USB Power Delivery is supported
+
+
+USB Type-C cable devices (eg. /sys/class/typec/usbc0-cable/)
+
+Note: Electronically Marked Cables will have a device also for one cable plug
+(eg. /sys/class/typec/usbc0-plug0). If the cable is active and has also SOP
+Double Prime controller (USB Power Deliver specification ch. 2.4) it will have
+second device also for the other plug. Both plugs may have their alternate modes
+as described in USB Type-C and USB Power Delivery specifications.
+
+What:		/sys/class/typec/<port>-cable/active
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows if the cable is active or passive.
+
+		Valid values:
+		- 0 when the cable is passive
+		- 1 when the cable is active
+
+What:		/sys/class/typec/<port>-cable/plug_type
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows type of the plug on the cable:
+		- Type-A - Standard A
+		- Type-B - Standard B
+		- Type-C - USB Type-C
+		- Captive - Non-standard
+
+What:		/sys/class/typec/<port>-cable/supports_usb_power_delivery
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows if the cable supports USB Power Delivery:
+		- 0 when USB Power Delivery is not supported
+		- 1 when USB Power Delivery is supported
+
+
+Alternate Mode devices (For example,
+/sys/class/typec/usbc0-partner/usbc0-partner.svid:xxxx/). The ports, partners
+and cable plugs can have alternate modes.
+
+What:		/sys/class/typec/<dev>/<dev>.svid:<svid>/<mode>/active
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows if the mode is active or not. The attribute can be used
+		for entering/exiting the mode with partners and cable plugs, and
+		with the port alternate modes it can be used for disabling
+		support for specific alternate modes. Entering/exiting modes is
+		supported as synchronous operation so write(2) to the attribute
+		does not return until the enter/exit mode operation has
+		finished. The attribute is notified when the mode is
+		entered/exited so poll(2) on the attribute wakes up.
+
+		Valid values:
+		- 0 when the mode is deactive
+		- 1 when the mode is active
+
+What:		/sys/class/typec/<dev>/<dev>.svid:<svid>/<mode>/description
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows description of the mode. The description is optional for
+		the drivers, just like with the Billboard Devices.
+
+What:		/sys/class/typec/<dev>/<dev>.svid:<svid>/<mode>/vdo
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows the VDO in hexadecimal returned from the Discover Modes
+		command.
+
+What:		/sys/class/typec/<port>/<port>.svid:<svid>/<mode>/supported_roles
+Date:		June 2016
+Contact:	Heikki Krogerus <[email protected]>
+Description:
+		Shows the roles, source or sink, the mode is supported with.
+
+		This attribute is available for the devices describing the
+		alternate modes a port supports, and it will not be exposed with
+		the devices presenting the alternate modes the partners or cable
+		plugs support.
Documentation/usb/typec.txt new file +103 -0

103 lines not on this page · raw message

MAINTAINERS +9 -0
index 238535e..ce6858a 100644
@@ -12440,6 +12440,15 @@ F:	drivers/usb/
 F:	include/linux/usb.h
 F:	include/linux/usb/
 
+USB TYPEC SUBSYSTEM
+M:	Heikki Krogerus <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/ABI/testing/sysfs-class-typec
+F:	Documentation/usb/typec.txt
+F:	drivers/usb/typec/
+F:	include/linux/usb/typec.h
+
 USB UHCI DRIVER
 M:	Alan Stern <[email protected]>
 L:	[email protected]
drivers/usb/Kconfig +2 -0
index 2b9159a..49bc82e 100644
@@ -150,6 +150,8 @@ source "drivers/usb/phy/Kconfig"
 
 source "drivers/usb/gadget/Kconfig"
 
+source "drivers/usb/typec/Kconfig"
+
 config USB_LED_TRIG
 	bool "USB LED Triggers"
 	depends on LEDS_CLASS && USB_COMMON && LEDS_TRIGGERS
drivers/usb/Makefile +2 -0
index dca7856..51e381e 100644
@@ -61,3 +61,5 @@ obj-$(CONFIG_USB_GADGET)	+= gadget/
 obj-$(CONFIG_USB_COMMON)	+= common/
 
 obj-$(CONFIG_USBIP_CORE)	+= usbip/
+
+obj-$(CONFIG_TYPEC)		+= typec/
drivers/usb/typec/Kconfig new file +7 -0
new file mode 100644
index 0000000..b229fb9
@@ -0,0 +1,7 @@
+
+menu "USB PD and Type-C drivers"
+
+config TYPEC
+	tristate
+
+endmenu
drivers/usb/typec/Makefile new file +1 -0
new file mode 100644
index 0000000..1012a8b
@@ -0,0 +1 @@
+obj-$(CONFIG_TYPEC)		+= typec.o
drivers/usb/typec/typec.c new file +1075 -0

1,075 lines not on this page · raw message

include/linux/usb/typec.h new file +252 -0

252 lines not on this page · raw message

Signature · 1 line
2.9.3
maintainer
#2 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 19 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
Quoted 8 lines
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

Overall, this looks good, just a few minor comments that will require you to respin this...

Documentation/ABI/testing/sysfs-class-typec line 4

Quoted 11 lines
Documentation/ABI/testing/sysfs-class-typec new file +4 -0
new file mode 100644
index 0000000..dcca6bd
@@ -0,0 +1,218 @@
+USB Type-C port devices (eg. /sys/class/typec/usbc0/)
+
+What:		/sys/class/typec/<port>/current_data_role
+Date:		June 2016

It's no longer June. I don't know why we have these dates, sorry, just make it be December and you should be fine.

Documentation/usb/typec.txt

Quoted 2 lines
Documentation/usb/typec.txt new file +0 -0

We want to use .rst formats now, but this should be fine as-is for now.

Quoted 5 lines
Documentation/usb/typec.txt new file +5 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+	const char *item;
+	int index;
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}

should we make this a core sysfs function?

Documentation/usb/typec.txt

Quoted 7 lines
Documentation/usb/typec.txt new file +7 -0
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Partners */
+
+static void typec_dev_release(struct device *dev)
+{
+}

Yeah, thanks to the in-kernel documentation, I now get to make fun of you!!!!

Please, NEVER DO THIS EVER!!! You are trying to tell the kernel "hey, shut up for your stupid warning about an empty release function!" Did you ever think about _why_ I made the kernel warn about that? Don't think you are smarter than the kernel, you will always loose that bet...

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+
+static ssize_t partner_usb_pd_show(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct typec_partner *p = container_of(dev, struct typec_partner, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%d\n", p->usb_pd);
+}
+
+static struct device_attribute dev_attr_partner_usb_pd = {
+	.attr = {
+		.name = "supports_usb_power_delivery",
+		.mode = S_IRUGO,
+	},
+	.show = partner_usb_pd_show,
+};

DEVICE_ATTR_RO()?

Quoted 7 lines
Documentation/usb/typec.txt new file +7 -0
+
+static ssize_t
+partner_accessory_mode_show(struct device *dev, struct device_attribute *attr,
+			    char *buf)
+{
+	struct typec_partner *p = container_of(dev, struct typec_partner, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
+}
+
+static struct device_attribute dev_attr_partner_accessory = {
+	.attr = {
+		.name = "accessory_mode",
+		.mode = S_IRUGO,
+	},
+	.show = partner_accessory_mode_show,
+};

DEVICE_ATTR_RO()?

Quoted 5 lines
Documentation/usb/typec.txt new file +5 -0
+
+static struct attribute *typec_partner_attrs[] = {
+	&dev_attr_partner_accessory.attr,
+	&dev_attr_partner_usb_pd.attr,
+	NULL

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+};
+
+static struct attribute_group typec_partner_group = {
+	.attrs = typec_partner_attrs,
+};
+
+static const struct attribute_group *typec_partner_groups[] = {
+	&typec_partner_group,
+	NULL
+};

ATTRIBUTE_GROUPS()?

Quoted 21 lines
Documentation/usb/typec.txt new file +21 -0
+
+static struct device_type typec_partner_dev_type = {
+	.name = "typec_partner_device",
+	.groups = typec_partner_groups,
+	.release = typec_dev_release,
+};
+
+static int
+typec_add_partner(struct typec_port *port, struct typec_partner *partner)
+{
+	struct device *dev = &partner->dev;
+	int ret;
+
+	dev->class = &typec_class;
+	dev->parent = &port->dev;
+	dev->type = &typec_partner_dev_type;
+	dev_set_name(dev, "%s-partner", dev_name(&port->dev));
+
+	ret = device_register(dev);
+	if (ret) {
+		put_device(dev);

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+		return ret;
+	}
+
+	port->partner = partner;
+	return 0;
+}
+
+static void typec_remove_partner(struct typec_port *port)
+{
+	WARN_ON(port->partner->alt_modes);

how can this happen? What would a user do if it does?

Quoted 27 lines
Documentation/usb/typec.txt new file +27 -0
+	device_unregister(&port->partner->dev);
+}
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Cable Plugs */
+
+static struct device_type typec_plug_dev_type = {
+	.name = "typec_plug_device",
+	.release = typec_dev_release,
+};
+
+static int
+typec_add_plug(struct typec_port *port, struct typec_plug *plug)
+{
+	struct device *dev = &plug->dev;
+	char name[8];
+	int ret;
+
+	sprintf(name, "plug%d", plug->index);
+
+	dev->class = &typec_class;
+	dev->parent = &port->cable->dev;
+	dev->type = &typec_plug_dev_type;
+	dev_set_name(dev, "%s-%s", dev_name(&port->dev), name);
+
+	ret = device_register(dev);
+	if (ret) {

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+		put_device(dev);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void typec_remove_plug(struct typec_plug *plug)
+{
+	WARN_ON(plug->alt_modes);

again, what can someone do with this?

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	device_unregister(&plug->dev);
+}
+
+/* Type-C Cables */
+
+static ssize_t
+cable_active_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%d\n", cable->active);
+}
+
+static struct device_attribute dev_attr_cable_active = {
+	.attr = {
+		.name = "active",
+		.mode = S_IRUGO,
+	},
+	.show = cable_active_show,
+};

DEVICE_ATTR_RO()

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+
+static ssize_t cable_usb_pd_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%d\n", cable->usb_pd);
+}
+
+static struct device_attribute dev_attr_cable_usb_pd = {
+	.attr = {
+		.name = "supports_usb_power_delivery",
+		.mode = S_IRUGO,
+	},
+	.show = cable_usb_pd_show,
+};

DEVICE_ATTR_RO()

Quoted 14 lines
Documentation/usb/typec.txt new file +14 -0
+
+static const char * const typec_plug_types[] = {
+	[USB_PLUG_NONE]		= "unknown",
+	[USB_PLUG_TYPE_A]	= "Type-A",
+	[USB_PLUG_TYPE_B]	= "Type-B",
+	[USB_PLUG_TYPE_C]	= "Type-C",
+	[USB_PLUG_CAPTIVE]	= "Captive",
+};
+
+static ssize_t cable_plug_type_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
+}
+
+static struct device_attribute dev_attr_plug_type = {
+	.attr = {
+		.name = "plug_type",
+		.mode = S_IRUGO,
+	},
+	.show = cable_plug_type_show,
+};

And so on...

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+
+static struct attribute *typec_cable_attrs[] = {
+	&dev_attr_cable_active.attr,
+	&dev_attr_cable_usb_pd.attr,
+	&dev_attr_plug_type.attr,
+	NULL

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+};
+
+static struct attribute_group typec_cable_group = {
+	.attrs = typec_cable_attrs,
+};
+
+static const struct attribute_group *typec_cable_groups[] = {
+	&typec_cable_group,
+	NULL
+};

ATTRIBUTE_GROUPS()?

Quoted 84 lines
Documentation/usb/typec.txt new file +84 -0
+
+static struct device_type typec_cable_dev_type = {
+	.name = "typec_cable_device",
+	.groups = typec_cable_groups,
+	.release = typec_dev_release,
+};
+
+static int typec_add_cable(struct typec_port *port, struct typec_cable *cable)
+{
+	struct device *dev = &cable->dev;
+	int ret;
+
+	dev->class = &typec_class;
+	dev->parent = &port->dev;
+	dev->type = &typec_cable_dev_type;
+	dev_set_name(dev, "%s-cable", dev_name(&port->dev));
+
+	ret = device_register(dev);
+	if (ret) {
+		put_device(dev);
+		return ret;
+	}
+
+	/* Plug1 */
+	if (!cable->usb_pd)
+		return 0;
+
+	cable->plug[0].index = 1;
+	ret = typec_add_plug(port, &cable->plug[0]);
+	if (ret) {
+		device_unregister(dev);
+		return ret;
+	}
+
+	/* Plug2 */
+	if (!cable->active || !cable->sop_pp_controller)
+		return 0;
+
+	cable->plug[1].index = 2;
+	ret = typec_add_plug(port, &cable->plug[1]);
+	if (ret) {
+		typec_remove_plug(&cable->plug[0]);
+		device_unregister(dev);
+		return ret;
+	}
+
+	port->cable = cable;
+	return 0;
+}
+
+static void typec_remove_cable(struct typec_port *port)
+{
+	if (port->cable->active) {
+		typec_remove_plug(&port->cable->plug[0]);
+		if (port->cable->sop_pp_controller)
+			typec_remove_plug(&port->cable->plug[1]);
+	}
+	device_unregister(&port->cable->dev);
+}
+
+/* ------------------------------------------------------------------------- */
+/* API for the port drivers */
+
+static void typec_init_roles(struct typec_port *port)
+{
+	if (port->prefer_role < 0)
+		return;
+
+	if (port->prefer_role == TYPEC_SOURCE) {
+		port->data_role = TYPEC_HOST;
+		port->pwr_role = TYPEC_SOURCE;
+		port->vconn_role = TYPEC_SOURCE;
+	} else {
+		/* Device mode as default also by default with DRP ports */
+		port->data_role = TYPEC_DEVICE;
+		port->pwr_role = TYPEC_SINK;
+		port->vconn_role = TYPEC_SINK;
+	}
+}
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 39 lines
Documentation/usb/typec.txt new file +39 -0
+
+	if (con->cable) {
+		ret = typec_add_cable(port, con->cable);
+		if (ret)
+			return ret;
+	}
+
+	if (con->partner) {
+		ret = typec_add_partner(port, con->partner);
+		if (ret) {
+			if (con->cable)
+				typec_remove_cable(port);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(typec_connect);
+
+void typec_disconnect(struct typec_port *port)
+{
+	if (port->partner)
+		typec_remove_partner(port);
+
+	if (port->cable)
+		typec_remove_cable(port);
+
+	port->connected = 0;
+	port->partner = NULL;
+	port->cable = NULL;
+
+	port->pwr_opmode = TYPEC_PWR_MODE_USB;
+
+	typec_init_roles(port);
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_disconnect);

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+
+/* --------------------------------------- */
+/* Driver callbacks to report role updates */
+
+void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
+{
+	port->data_role = role;
+	sysfs_notify(&port->dev.kobj, NULL, "current_data_role");
+}
+EXPORT_SYMBOL(typec_set_data_role);

EXPORT_SYMBOL_GPL() everywhere please, don't mix and match for no good reason.

Quoted 36 lines
Documentation/usb/typec.txt new file +36 -0
+static void typec_init_modes(struct typec_altmode *alt, int is_port)
+{
+	struct typec_mode *mode = alt->modes;
+	int i;
+
+	for (i = 0; i < alt->n_modes; i++, mode++) {
+		mode->alt_mode = alt;
+		mode->index = i;
+		sprintf(mode->group_name, "mode%d", i);
+
+		sysfs_attr_init(&mode->vdo_attr.attr);
+		mode->vdo_attr.attr.name = "vdo";
+		mode->vdo_attr.attr.mode = S_IRUGO;
+		mode->vdo_attr.show = typec_altmode_vdo_show;
+
+		sysfs_attr_init(&mode->desc_attr.attr);
+		mode->desc_attr.attr.name = "description";
+		mode->desc_attr.attr.mode = S_IRUGO;
+		mode->desc_attr.show = typec_altmode_desc_show;
+
+		sysfs_attr_init(&mode->active_attr.attr);
+		mode->active_attr.attr.name = "active";
+		mode->active_attr.attr.mode = S_IWUSR | S_IRUGO;
+		mode->active_attr.show = typec_altmode_active_show;
+		mode->active_attr.store = typec_altmode_active_store;
+
+		mode->attrs[0] = &mode->vdo_attr.attr;
+		mode->attrs[1] = &mode->desc_attr.attr;
+		mode->attrs[2] = &mode->active_attr.attr;
+
+		/* With ports, list the roles that the mode is supported with */
+		if (is_port) {
+			sysfs_attr_init(&mode->roles_attr.attr);
+			mode->roles_attr.attr.name = "supported_roles";
+			mode->roles_attr.attr.mode = S_IRUGO;
+			mode->roles_attr.show = typec_altmode_roles_show;

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+
+			mode->attrs[3] = &mode->roles_attr.attr;
+		}
+
+		mode->group.attrs = mode->attrs;
+		mode->group.name = mode->group_name;
+
+		alt->mode_groups[i] = &mode->group;
+	}
+}

Ugh, dynamic attributes on the fly? why? Why not just use the callback to determine if the attribute should be shown or not? Much simpler and you don't have to clean up after yourself. Are you sure you cleaned up properly from these?

Documentation/usb/typec.txt

Quoted 8 lines
Documentation/usb/typec.txt new file +8 -0
+static ssize_t current_power_role_show(struct device *dev,
+				       struct device_attribute *attr, char *buf)
+{
+	struct typec_port *port = to_typec_port(dev);
+
+	return sprintf(buf, "%s\n", typec_roles[port->pwr_role]);
+}
+static DEVICE_ATTR_RW(current_power_role);

Nice, see, you use the macros here, be consistant please...

Documentation/usb/typec.txt

Quoted 8 lines
Documentation/usb/typec.txt new file +8 -0
+static const struct attribute_group typec_group = {
+	.attrs = typec_attrs,
+};
+
+static const struct attribute_group *typec_groups[] = {
+	&typec_group,
+	NULL,
+};

ATTRIBUTE_GROUP() please.

Documentation/usb/typec.txt

Quoted 9 lines
Documentation/usb/typec.txt new file +9 -0
+static int __init typec_init(void)
+{
+	return class_register(&typec_class);
+}
+subsys_initcall(typec_init);
+
+static void __exit typec_exit(void)
+{
+	class_unregister(&typec_class);

You forgot to clean up your idr :(

thanks,

greg k-h

author maintainer
#3 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Hi Greg,

Quoted 34 lines · Greg KH
Quoted 19 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
Quoted 8 lines
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

Overall, this looks good, just a few minor comments that will require you to respin this...

Quoted 1 line
Quoted 13 lines

Documentation/ABI/testing/sysfs-class-typec line 4

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-typec new file +4 -0
new file mode 100644
index 0000000..dcca6bd
@@ -0,0 +1,218 @@
+USB Type-C port devices (eg. /sys/class/typec/usbc0/)
+
+What:		/sys/class/typec/<port>/current_data_role
+Date:		June 2016

It's no longer June. I don't know why we have these dates, sorry, just make it be December and you should be fine.

OK

Quoted 14 lines

Documentation/usb/typec.txt

Quoted 2 lines
Documentation/usb/typec.txt new file +0 -0

We want to use .rst formats now, but this should be fine as-is for now.

Quoted 9 lines
Documentation/usb/typec.txt new file +9 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+	const char *item;
+	int index;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}

should we make this a core sysfs function?

I already have the patch. I was planning on proposing that separately after this series. This turned out to be something that can be used in many drivers, so I was thinking about proposing it for at least few other drivers.

But if you prefer, I can also introduce the function alone as new sysfs core function in this series, and just use it in this driver.

Quoted 11 lines

Documentation/usb/typec.txt

Quoted 7 lines
Documentation/usb/typec.txt new file +7 -0
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Partners */
+
+static void typec_dev_release(struct device *dev)
+{
+}

Yeah, thanks to the in-kernel documentation, I now get to make fun of you!!!!

Quoted 4 lines

Please, NEVER DO THIS EVER!!! You are trying to tell the kernel "hey, shut up for your stupid warning about an empty release function!" Did you ever think about _why_ I made the kernel warn about that? Don't think you are smarter than the kernel, you will always loose that bet...

Point taken.

Quoted 10 lines
Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+
+static ssize_t partner_usb_pd_show(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct typec_partner *p = container_of(dev, struct typec_partner, dev);
+
+	return sprintf(buf, "%d\n", p->usb_pd);
+}
+
+static struct device_attribute dev_attr_partner_usb_pd = {
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+	.attr = {
+		.name = "supports_usb_power_delivery",
+		.mode = S_IRUGO,
+	},
+	.show = partner_usb_pd_show,
+};

DEVICE_ATTR_RO()?

I'm using the same attribute names with different types of devises. It felt more wrong to use shared functions for some of the attributes (but not all), and try to identify the device type in them, then to simply ignore the macros and name the functions how ever I wanted. At least it made the driver look less messy for sure.

But I'll try change these.

Quoted 64 lines
Quoted 7 lines
Documentation/usb/typec.txt new file +7 -0
+
+static ssize_t
+partner_accessory_mode_show(struct device *dev, struct device_attribute *attr,
+			    char *buf)
+{
+	struct typec_partner *p = container_of(dev, struct typec_partner, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
+}
+
+static struct device_attribute dev_attr_partner_accessory = {
+	.attr = {
+		.name = "accessory_mode",
+		.mode = S_IRUGO,
+	},
+	.show = partner_accessory_mode_show,
+};

DEVICE_ATTR_RO()?

Quoted 5 lines
Documentation/usb/typec.txt new file +5 -0
+
+static struct attribute *typec_partner_attrs[] = {
+	&dev_attr_partner_accessory.attr,
+	&dev_attr_partner_usb_pd.attr,
+	NULL

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+};
+
+static struct attribute_group typec_partner_group = {
+	.attrs = typec_partner_attrs,
+};
+
+static const struct attribute_group *typec_partner_groups[] = {
+	&typec_partner_group,
+	NULL
+};

ATTRIBUTE_GROUPS()?

Quoted 25 lines
Documentation/usb/typec.txt new file +25 -0
+
+static struct device_type typec_partner_dev_type = {
+	.name = "typec_partner_device",
+	.groups = typec_partner_groups,
+	.release = typec_dev_release,
+};
+
+static int
+typec_add_partner(struct typec_port *port, struct typec_partner *partner)
+{
+	struct device *dev = &partner->dev;
+	int ret;
+
+	dev->class = &typec_class;
+	dev->parent = &port->dev;
+	dev->type = &typec_partner_dev_type;
+	dev_set_name(dev, "%s-partner", dev_name(&port->dev));
+
+	ret = device_register(dev);
+	if (ret) {
+		put_device(dev);
+		return ret;
+	}
+
+	port->partner = partner;
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+	return 0;
+}
+
+static void typec_remove_partner(struct typec_port *port)
+{
+	WARN_ON(port->partner->alt_modes);

how can this happen? What would a user do if it does?

I'll get rid of that.

Quoted 31 lines
Quoted 31 lines
Documentation/usb/typec.txt new file +31 -0
+	device_unregister(&port->partner->dev);
+}
+
+/* ------------------------------------------------------------------------- */
+/* Type-C Cable Plugs */
+
+static struct device_type typec_plug_dev_type = {
+	.name = "typec_plug_device",
+	.release = typec_dev_release,
+};
+
+static int
+typec_add_plug(struct typec_port *port, struct typec_plug *plug)
+{
+	struct device *dev = &plug->dev;
+	char name[8];
+	int ret;
+
+	sprintf(name, "plug%d", plug->index);
+
+	dev->class = &typec_class;
+	dev->parent = &port->cable->dev;
+	dev->type = &typec_plug_dev_type;
+	dev_set_name(dev, "%s-%s", dev_name(&port->dev), name);
+
+	ret = device_register(dev);
+	if (ret) {
+		put_device(dev);
+		return ret;
+	}
+
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+	return 0;
+}
+
+static void typec_remove_plug(struct typec_plug *plug)
+{
+	WARN_ON(plug->alt_modes);

again, what can someone do with this?

I'll get rid of that too.

Quoted 179 lines
Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	device_unregister(&plug->dev);
+}
+
+/* Type-C Cables */
+
+static ssize_t
+cable_active_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%d\n", cable->active);
+}
+
+static struct device_attribute dev_attr_cable_active = {
+	.attr = {
+		.name = "active",
+		.mode = S_IRUGO,
+	},
+	.show = cable_active_show,
+};

DEVICE_ATTR_RO()

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+
+static ssize_t cable_usb_pd_show(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%d\n", cable->usb_pd);
+}
+
+static struct device_attribute dev_attr_cable_usb_pd = {
+	.attr = {
+		.name = "supports_usb_power_delivery",
+		.mode = S_IRUGO,
+	},
+	.show = cable_usb_pd_show,
+};

DEVICE_ATTR_RO()

Quoted 14 lines
Documentation/usb/typec.txt new file +14 -0
+
+static const char * const typec_plug_types[] = {
+	[USB_PLUG_NONE]		= "unknown",
+	[USB_PLUG_TYPE_A]	= "Type-A",
+	[USB_PLUG_TYPE_B]	= "Type-B",
+	[USB_PLUG_TYPE_C]	= "Type-C",
+	[USB_PLUG_CAPTIVE]	= "Captive",
+};
+
+static ssize_t cable_plug_type_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct typec_cable *cable = container_of(dev, struct typec_cable, dev);
+

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+	return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
+}
+
+static struct device_attribute dev_attr_plug_type = {
+	.attr = {
+		.name = "plug_type",
+		.mode = S_IRUGO,
+	},
+	.show = cable_plug_type_show,
+};

And so on...

Quoted 6 lines
Documentation/usb/typec.txt new file +6 -0
+
+static struct attribute *typec_cable_attrs[] = {
+	&dev_attr_cable_active.attr,
+	&dev_attr_cable_usb_pd.attr,
+	&dev_attr_plug_type.attr,
+	NULL

Documentation/usb/typec.txt

Quoted 10 lines
Documentation/usb/typec.txt new file +10 -0
+};
+
+static struct attribute_group typec_cable_group = {
+	.attrs = typec_cable_attrs,
+};
+
+static const struct attribute_group *typec_cable_groups[] = {
+	&typec_cable_group,
+	NULL
+};

ATTRIBUTE_GROUPS()?

Quoted 89 lines
Documentation/usb/typec.txt new file +89 -0
+
+static struct device_type typec_cable_dev_type = {
+	.name = "typec_cable_device",
+	.groups = typec_cable_groups,
+	.release = typec_dev_release,
+};
+
+static int typec_add_cable(struct typec_port *port, struct typec_cable *cable)
+{
+	struct device *dev = &cable->dev;
+	int ret;
+
+	dev->class = &typec_class;
+	dev->parent = &port->dev;
+	dev->type = &typec_cable_dev_type;
+	dev_set_name(dev, "%s-cable", dev_name(&port->dev));
+
+	ret = device_register(dev);
+	if (ret) {
+		put_device(dev);
+		return ret;
+	}
+
+	/* Plug1 */
+	if (!cable->usb_pd)
+		return 0;
+
+	cable->plug[0].index = 1;
+	ret = typec_add_plug(port, &cable->plug[0]);
+	if (ret) {
+		device_unregister(dev);
+		return ret;
+	}
+
+	/* Plug2 */
+	if (!cable->active || !cable->sop_pp_controller)
+		return 0;
+
+	cable->plug[1].index = 2;
+	ret = typec_add_plug(port, &cable->plug[1]);
+	if (ret) {
+		typec_remove_plug(&cable->plug[0]);
+		device_unregister(dev);
+		return ret;
+	}
+
+	port->cable = cable;
+	return 0;
+}
+
+static void typec_remove_cable(struct typec_port *port)
+{
+	if (port->cable->active) {
+		typec_remove_plug(&port->cable->plug[0]);
+		if (port->cable->sop_pp_controller)
+			typec_remove_plug(&port->cable->plug[1]);
+	}
+	device_unregister(&port->cable->dev);
+}
+
+/* ------------------------------------------------------------------------- */
+/* API for the port drivers */
+
+static void typec_init_roles(struct typec_port *port)
+{
+	if (port->prefer_role < 0)
+		return;
+
+	if (port->prefer_role == TYPEC_SOURCE) {
+		port->data_role = TYPEC_HOST;
+		port->pwr_role = TYPEC_SOURCE;
+		port->vconn_role = TYPEC_SOURCE;
+	} else {
+		/* Device mode as default also by default with DRP ports */
+		port->data_role = TYPEC_DEVICE;
+		port->pwr_role = TYPEC_SINK;
+		port->vconn_role = TYPEC_SINK;
+	}
+}
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 5 lines
Documentation/usb/typec.txt new file +5 -0
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Oliver! Did you need this or can we remove it?

I remember I removed the "connected" attribute because you did not see any use for it at one point. I don't remember the reason exactly why?

Quoted 44 lines
Quoted 44 lines
Documentation/usb/typec.txt new file +44 -0
+
+	if (con->cable) {
+		ret = typec_add_cable(port, con->cable);
+		if (ret)
+			return ret;
+	}
+
+	if (con->partner) {
+		ret = typec_add_partner(port, con->partner);
+		if (ret) {
+			if (con->cable)
+				typec_remove_cable(port);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(typec_connect);
+
+void typec_disconnect(struct typec_port *port)
+{
+	if (port->partner)
+		typec_remove_partner(port);
+
+	if (port->cable)
+		typec_remove_cable(port);
+
+	port->connected = 0;
+	port->partner = NULL;
+	port->cable = NULL;
+
+	port->pwr_opmode = TYPEC_PWR_MODE_USB;
+
+	typec_init_roles(port);
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
+}
+EXPORT_SYMBOL_GPL(typec_disconnect);
+
+/* --------------------------------------- */
+/* Driver callbacks to report role updates */
+
+void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 5 lines
Documentation/usb/typec.txt new file +5 -0
+{
+	port->data_role = role;
+	sysfs_notify(&port->dev.kobj, NULL, "current_data_role");
+}
+EXPORT_SYMBOL(typec_set_data_role);

EXPORT_SYMBOL_GPL() everywhere please, don't mix and match for no good reason.

I'll fix these.

Quoted 43 lines
Quoted 43 lines
Documentation/usb/typec.txt new file +43 -0
+static void typec_init_modes(struct typec_altmode *alt, int is_port)
+{
+	struct typec_mode *mode = alt->modes;
+	int i;
+
+	for (i = 0; i < alt->n_modes; i++, mode++) {
+		mode->alt_mode = alt;
+		mode->index = i;
+		sprintf(mode->group_name, "mode%d", i);
+
+		sysfs_attr_init(&mode->vdo_attr.attr);
+		mode->vdo_attr.attr.name = "vdo";
+		mode->vdo_attr.attr.mode = S_IRUGO;
+		mode->vdo_attr.show = typec_altmode_vdo_show;
+
+		sysfs_attr_init(&mode->desc_attr.attr);
+		mode->desc_attr.attr.name = "description";
+		mode->desc_attr.attr.mode = S_IRUGO;
+		mode->desc_attr.show = typec_altmode_desc_show;
+
+		sysfs_attr_init(&mode->active_attr.attr);
+		mode->active_attr.attr.name = "active";
+		mode->active_attr.attr.mode = S_IWUSR | S_IRUGO;
+		mode->active_attr.show = typec_altmode_active_show;
+		mode->active_attr.store = typec_altmode_active_store;
+
+		mode->attrs[0] = &mode->vdo_attr.attr;
+		mode->attrs[1] = &mode->desc_attr.attr;
+		mode->attrs[2] = &mode->active_attr.attr;
+
+		/* With ports, list the roles that the mode is supported with */
+		if (is_port) {
+			sysfs_attr_init(&mode->roles_attr.attr);
+			mode->roles_attr.attr.name = "supported_roles";
+			mode->roles_attr.attr.mode = S_IRUGO;
+			mode->roles_attr.show = typec_altmode_roles_show;
+
+			mode->attrs[3] = &mode->roles_attr.attr;
+		}
+
+		mode->group.attrs = mode->attrs;
+		mode->group.name = mode->group_name;
+
Quoted 8 lines

Documentation/usb/typec.txt

Quoted 3 lines
Documentation/usb/typec.txt new file +3 -0
+		alt->mode_groups[i] = &mode->group;
+	}
+}

Ugh, dynamic attributes on the fly? why? Why not just use the callback to determine if the attribute should be shown or not? Much simpler and you don't have to clean up after yourself. Are you sure you cleaned up properly from these?

I'll change that.

Quoted 22 lines

Documentation/usb/typec.txt

Quoted 8 lines
Documentation/usb/typec.txt new file +8 -0
+static ssize_t current_power_role_show(struct device *dev,
+				       struct device_attribute *attr, char *buf)
+{
+	struct typec_port *port = to_typec_port(dev);
+
+	return sprintf(buf, "%s\n", typec_roles[port->pwr_role]);
+}
+static DEVICE_ATTR_RW(current_power_role);

Nice, see, you use the macros here, be consistant please...

Documentation/usb/typec.txt

Quoted 8 lines
Documentation/usb/typec.txt new file +8 -0
+static const struct attribute_group typec_group = {
+	.attrs = typec_attrs,
+};
+
+static const struct attribute_group *typec_groups[] = {
+	&typec_group,
+	NULL,
+};

ATTRIBUTE_GROUP() please.

Quoted 11 lines

Documentation/usb/typec.txt

Quoted 9 lines
Documentation/usb/typec.txt new file +9 -0
+static int __init typec_init(void)
+{
+	return class_register(&typec_class);
+}
+subsys_initcall(typec_init);
+
+static void __exit typec_exit(void)
+{
+	class_unregister(&typec_class);

You forgot to clean up your idr :(

Sorry, what idr? The port ids get removed in typec_release().

Thanks,

Signature · 1 line
heikki
maintainer
#4 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 7 lines · Heikki Krogerus
Quoted 5 lines

drivers/usb/typec/typec.c line 1069

Quoted 3 lines
drivers/usb/typec/typec.c new file +3 -0
+static void __exit typec_exit(void)
+{
+	class_unregister(&typec_class);

You forgot to clean up your idr :(

Sorry, what idr? The port ids get removed in typec_release().

You have a static idr structure in the driver, right? You have to clean it up when your code is going away so that it will free any memory it had allocated with a call to idr_destroy() on module exit.

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 7 lines · Greg KH
Quoted 6 lines · Heikki Krogerus
Quoted 5 lines

drivers/usb/typec/typec.c line 1069

Quoted 3 lines
drivers/usb/typec/typec.c new file +3 -0
+static void __exit typec_exit(void)
+{
+	class_unregister(&typec_class);

You forgot to clean up your idr :(

Quoted 5 lines
Quoted 1 line

Sorry, what idr? The port ids get removed in typec_release().

You have a static idr structure in the driver, right? You have to clean it up when your code is going away so that it will free any memory it had allocated with a call to idr_destroy() on module exit.

Ok.

Regarding the DEVICE_ATTR* macros. So I have attributes with same names for different device types. I may be able to identify the device types and deal with the correct attribute based on that, but for example the attribute "active" with alternate modes is writable, but with cables it's not. How do I handle those?

Thanks,

Signature · 1 line
heikki
maintainer
#6 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 14 lines · Heikki Krogerus
Quoted 7 lines · Greg KH
Quoted 6 lines · Heikki Krogerus
Quoted 5 lines

drivers/usb/typec/typec.c line 1069

Quoted 3 lines
drivers/usb/typec/typec.c new file +3 -0
+static void __exit typec_exit(void)
+{
+	class_unregister(&typec_class);

You forgot to clean up your idr :(

Quoted 5 lines
Quoted 1 line

Sorry, what idr? The port ids get removed in typec_release().

You have a static idr structure in the driver, right? You have to clean it up when your code is going away so that it will free any memory it had allocated with a call to idr_destroy() on module exit.

Quoted 7 lines

Ok.

Regarding the DEVICE_ATTR* macros. So I have attributes with same names for different device types. I may be able to identify the device types and deal with the correct attribute based on that, but for example the attribute "active" with alternate modes is writable, but with cables it's not. How do I handle those?

The attribute init callback should let you handle this, right? You'll have to add it for your dynamic attributes.

thanks,

greg k-h

#7 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

In reply to Heikki Krogerus
Quoted 14 lines · Heikki Krogerus

Hi Greg,

Quoted 11 lines · Greg KH
Quoted 10 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

[ ... ]

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/usb/typec/typec.c new file +10 -0
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 5 lines
drivers/usb/typec/typec.c new file +5 -0
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 2 lines

Oliver! Did you need this or can we remove it?

I'll also have to make sure that the Android folks don't use it.

Guenter

#8 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 17 lines · Guenter Roeck
Quoted 1 line
Quoted 1 line
Quoted 1 line

+int typec_connect(struct typec_port *port, struct

typec_connection *con)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/usb/typec/typec.c new file +7 -0
+{
+   int ret;
+
+   if (!con->partner && !con->cable)
+           return -EINVAL;
+
+   port->connected = 1;
Quoted 8 lines
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 6 lines
drivers/usb/typec/typec.c new file +6 -0
+   port->data_role = con->data_role;
+   port->pwr_role = con->pwr_role;
+   port->vconn_role = con->vconn_role;
+   port->pwr_opmode = con->pwr_opmode;
+
+   kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with

Quoted 7 lines

it?

Quoted 4 lines
Quoted 1 line

Shouldn't you just poll on an attribute file instead?

Oliver! Did you need this or can we remove it?

I'll also have to make sure that the Android folks don't use it.

How then do we notify user space? poll()? Yet another demon.

I do not specifically need this, but I note that uevents are the general tool we use to notify user space of that kind of events.

Regards
Oliver

#9 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

In reply to Heikki Krogerus
Quoted 32 lines · Heikki Krogerus

Hi Greg,

Quoted 29 lines · Greg KH
Quoted 27 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

[ ... ]

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/usb/typec/typec.c new file +10 -0
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 5 lines
drivers/usb/typec/typec.c new file +5 -0
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 5 lines

Oliver! Did you need this or can we remove it?

I remember I removed the "connected" attribute because you did not see any use for it at one point. I don't remember the reason exactly why?

The Android team tells me that they are currently using the udev events to track port role changes, and to detect presence of port partner.

Also, there are plans to track changes on usbc*cable to differentiate between cable attach vs. device being attached on the remote end.

What is the problem with using kobject_uevent() and thus presumably udev events ?

Thanks,
Guenter

maintainer
#10 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 60 lines · Guenter Roeck
Quoted 32 lines · Heikki Krogerus

Hi Greg,

Quoted 29 lines · Greg KH
Quoted 27 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

[ ... ]

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/usb/typec/typec.c new file +10 -0
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 5 lines
drivers/usb/typec/typec.c new file +5 -0
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 5 lines

Oliver! Did you need this or can we remove it?

I remember I removed the "connected" attribute because you did not see any use for it at one point. I don't remember the reason exactly why?

Quoted 8 lines

The Android team tells me that they are currently using the udev events to track port role changes, and to detect presence of port partner.

Also, there are plans to track changes on usbc*cable to differentiate between cable attach vs. device being attached on the remote end.

What is the problem with using kobject_uevent() and thus presumably udev events ?

It's not a "normal" thing to do and is pretty "heavy" to do. What does userspace do with that change event? Does it read specific attributes? What causes the event to happen in the kernel, is it really just a change in the specific object, or do new ones get added/removed?

In short, document the heck out of this please so people know how to use it, and what is happening when the event happens.

thanks,

greg k-h

#11 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 70 lines · Greg KH
Quoted 60 lines · Guenter Roeck
Quoted 32 lines · Heikki Krogerus

Hi Greg,

Quoted 29 lines · Greg KH
Quoted 27 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

[ ... ]

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
drivers/usb/typec/typec.c new file +10 -0
+
+int typec_connect(struct typec_port *port, struct typec_connection *con)
+{
+	int ret;
+
+	if (!con->partner && !con->cable)
+		return -EINVAL;
+
+	port->connected = 1;
+	port->data_role = con->data_role;
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 5 lines
drivers/usb/typec/typec.c new file +5 -0
+	port->pwr_role = con->pwr_role;
+	port->vconn_role = con->vconn_role;
+	port->pwr_opmode = con->pwr_opmode;
+
+	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 5 lines

Oliver! Did you need this or can we remove it?

I remember I removed the "connected" attribute because you did not see any use for it at one point. I don't remember the reason exactly why?

Quoted 8 lines

The Android team tells me that they are currently using the udev events to track port role changes, and to detect presence of port partner.

Also, there are plans to track changes on usbc*cable to differentiate between cable attach vs. device being attached on the remote end.

What is the problem with using kobject_uevent() and thus presumably udev events ?

Quoted 8 lines

It's not a "normal" thing to do and is pretty "heavy" to do. What does userspace do with that change event? Does it read specific attributes? What causes the event to happen in the kernel, is it really just a change in the specific object, or do new ones get added/removed?

In short, document the heck out of this please so people know how to use it, and what is happening when the event happens.

Badhri,

can you clarify which events you are using in detail, and what for ?

Thanks,
Guenter

#12 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Hi,

At present I am using the uevent in the userspace to infer the Presence of a port on the remote end through the appearance of usbc*-partner.

Userspace uses this info to decide on when to show a USB notification on the screen and what should be the options provided in the dialog.

I was assuming that this is not something that would be dropped.

Coding using events was relatively easier to program from userspace ..

Is it possible to use POLL for identifying the appearance of port partner ? I did not notice sysfs_notify call in typec_connect/typec_disconnect.

It would also be nice to have uevent notifications when the contents of current_data_role or current_power_role changes.

Is that too costly to have ?

Thanks,
Badhri.

Quoted 98 lines · Guenter Roeck
Quoted 81 lines · Greg KH
Quoted 69 lines · Guenter Roeck
Quoted 37 lines · Heikki Krogerus

Hi Greg,

Quoted 33 lines · Greg KH
Quoted 29 lines · Heikki Krogerus

The purpose of USB Type-C connector class is to provide unified interface for the user space to get the status and basic information about USB Type-C connectors on a system, control over data role swapping, and when the port supports USB Power Delivery, also control over power role swapping and Alternate Modes.

---
 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |    7 +
 drivers/usb/typec/Makefile                  |    1 +
 drivers/usb/typec/typec.c                   | 1075
+++++++++++++++++++++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 9 files changed, 1669 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

[ ... ]

Quoted 22 lines
Quoted 12 lines
Quoted 12 lines
(quoted diff) +2 -0
+
+int typec_connect(struct typec_port *port, struct typec_connection

*con)

drivers/usb/typec/typec.c new file +9 -0
+{
+       int ret;
+
+       if (!con->partner && !con->cable)
+               return -EINVAL;
+
+       port->connected = 1;
+       port->data_role = con->data_role;
+       port->pwr_role = con->pwr_role;
Quoted 8 lines

drivers/usb/typec/typec.c line 357

Quoted 4 lines
drivers/usb/typec/typec.c new file +4 -0
+       port->vconn_role = con->vconn_role;
+       port->pwr_opmode = con->pwr_opmode;
+
+       kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);

This worries me. Who is listening for it? What will you do with it? Shouldn't you just poll on an attribute file instead?

Quoted 5 lines

Oliver! Did you need this or can we remove it?

I remember I removed the "connected" attribute because you did not see any use for it at one point. I don't remember the reason exactly why?

Quoted 8 lines

The Android team tells me that they are currently using the udev events to track port role changes, and to detect presence of port partner.

Also, there are plans to track changes on usbc*cable to differentiate between cable attach vs. device being attached on the remote end.

What is the problem with using kobject_uevent() and thus presumably udev events ?

Quoted 8 lines

It's not a "normal" thing to do and is pretty "heavy" to do. What does userspace do with that change event? Does it read specific attributes? What causes the event to happen in the kernel, is it really just a change in the specific object, or do new ones get added/removed?

In short, document the heck out of this please so people know how to use it, and what is happening when the event happens.

Badhri,

can you clarify which events you are using in detail, and what for ?

Thanks,
Guenter

author maintainer
#13 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 14 lines · Badhri Jagan Sridharan

Hi,

At present I am using the uevent in the userspace to infer the Presence of a port on the remote end through the appearance of usbc*-partner.

Userspace uses this info to decide on when to show a USB notification on the screen and what should be the options provided in the dialog.

I was assuming that this is not something that would be dropped.

Coding using events was relatively easier to program from userspace ..

Quoted 7 lines

Is it possible to use POLL for identifying the appearance of port partner ? I did not notice sysfs_notify call in typec_connect/typec_disconnect.

It would also be nice to have uevent notifications when the contents of current_data_role or current_power_role changes.

Is that too costly to have ?

Greg, could you give your opinion. In this case we do have attribute files that the user space can poll. Data role is the USB data role, so host or device, and it can change for example if the partner executes a swap. The same can happen with the power role.

Thanks,

Signature · 1 line
heikki
#14 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 8 lines · Heikki Krogerus
Quoted 1 line · Badhri Jagan Sridharan

Is that too costly to have ?

Greg, could you give your opinion. In this case we do have attribute files that the user space can poll. Data role is the USB data role, so host or device, and it can change for example if the partner executes a swap. The same can happen with the power role.

IMHO the uevent is cheaper. User space cannot just poll without further infrastructure. A task needs to run to poll. A uevent can be handled through established infrastructure.
Sure from a kernel level it is the heavier gun, but I think this is the wrong angle of looking at this issue.

Regards
Oliver

maintainer
#15 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

In reply to Heikki Krogerus
Quoted 21 lines · Heikki Krogerus
Quoted 20 lines · Badhri Jagan Sridharan

Hi,

At present I am using the uevent in the userspace to infer the Presence of a port on the remote end through the appearance of usbc*-partner.

Userspace uses this info to decide on when to show a USB notification on the screen and what should be the options provided in the dialog.

I was assuming that this is not something that would be dropped.

Coding using events was relatively easier to program from userspace ..

Is it possible to use POLL for identifying the appearance of port partner ? I did not notice sysfs_notify call in typec_connect/typec_disconnect.

It would also be nice to have uevent notifications when the contents of current_data_role or current_power_role changes.

Quoted 6 lines
Quoted 1 line

Is that too costly to have ?

Greg, could you give your opinion. In this case we do have attribute files that the user space can poll. Data role is the USB data role, so host or device, and it can change for example if the partner executes a swap. The same can happen with the power role.

So the same 'struct device' switches roles and attribute files are updated that need to be re-read? If so, yes KOBJ_CHANGE is correct, if a struct device is added/removed for this, then no, it doesn't make sense.

Again, document this to describe what is happening and it might be more obvious to people and so these questions would not come up :)

thanks,

greg k-h

author maintainer
#16 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 29 lines · Greg KH
Quoted 21 lines · Heikki Krogerus
Quoted 20 lines · Badhri Jagan Sridharan

Hi,

At present I am using the uevent in the userspace to infer the Presence of a port on the remote end through the appearance of usbc*-partner.

Userspace uses this info to decide on when to show a USB notification on the screen and what should be the options provided in the dialog.

I was assuming that this is not something that would be dropped.

Coding using events was relatively easier to program from userspace ..

Is it possible to use POLL for identifying the appearance of port partner ? I did not notice sysfs_notify call in typec_connect/typec_disconnect.

It would also be nice to have uevent notifications when the contents of current_data_role or current_power_role changes.

Quoted 6 lines
Quoted 1 line

Is that too costly to have ?

Greg, could you give your opinion. In this case we do have attribute files that the user space can poll. Data role is the USB data role, so host or device, and it can change for example if the partner executes a swap. The same can happen with the power role.

Quoted 4 lines

So the same 'struct device' switches roles and attribute files are updated that need to be re-read? If so, yes KOBJ_CHANGE is correct, if a struct device is added/removed for this, then no, it doesn't make sense.

OK, I'll add KOBJ_CHANGE for those.

So is it OK to everybody if I remove the KOBJ_CHANGE in typec_connect()? We will see uevent KOBJ_ADD since the partner (or cable) is added in any case. Badhri, Oliver?

Thanks,

Signature · 1 line
heikki
#17 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 5 lines · Heikki Krogerus

OK, I'll add KOBJ_CHANGE for those.

So is it OK to everybody if I remove the KOBJ_CHANGE in typec_connect()? We will see uevent KOBJ_ADD since the partner (or cable) is added in any case. Badhri, Oliver?

OK by me.

Regards
Oliver

#18 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 3 lines

IMHO the uevent is cheaper. User space cannot just poll without further infrastructure. A task needs to run to poll. A uevent can be handled through established infrastructure.

Thanks Oliver for stating this. This is exactly what I was facing.

Quoted 5 lines

OK, I'll add KOBJ_CHANGE for those.

So is it OK to everybody if I remove the KOBJ_CHANGE in typec_connect()? We will see uevent KOBJ_ADD since the partner (or cable) is added in any case. Badhri, Oliver?

Yes Heikki.. That's OK for me as well.
Just to get my understanding right. You are planning to add KOBJ_CHANGE uevents when current_power_role or current_data_role changes and KOBJ_ADD when new port-partner or the cable is attached. Is that right ?

Thanks,
Badhri.

Quoted 14 lines · Oliver Neukum
Quoted 5 lines · Heikki Krogerus

OK, I'll add KOBJ_CHANGE for those.

So is it OK to everybody if I remove the KOBJ_CHANGE in typec_connect()? We will see uevent KOBJ_ADD since the partner (or cable) is added in any case. Badhri, Oliver?

OK by me.

Regards
Oliver

author maintainer
#19 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 12 lines · Badhri Jagan Sridharan
Quoted 3 lines

IMHO the uevent is cheaper. User space cannot just poll without further infrastructure. A task needs to run to poll. A uevent can be handled through established infrastructure.

Thanks Oliver for stating this. This is exactly what I was facing.

Quoted 5 lines

OK, I'll add KOBJ_CHANGE for those.

So is it OK to everybody if I remove the KOBJ_CHANGE in typec_connect()? We will see uevent KOBJ_ADD since the partner (or cable) is added in any case. Badhri, Oliver?

Quoted 5 lines

Yes Heikki.. That's OK for me as well.
Just to get my understanding right. You are planning to add KOBJ_CHANGE uevents when current_power_role or current_data_role changes and KOBJ_ADD when new port-partner or the cable is attached. Is that right ?

Yes, though I don't KOBJ_ADD separately with the partners and cables. That uevent is sent when the device for them is registered, so it's already there.

Br,

Signature · 1 line
heikki
author maintainer
#20 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

In reply to Greg KH

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 9 lines
(quoted diff) +9 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+	const char *item;
+	int index;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}

should we make this a core sysfs function?

Last question before I send v11. Is the following (the helper) OK?

include/linux/string.h +10 -0
index 26b6f6a..5606810 100644
@@ -135,6 +135,16 @@ static inline int strtobool(const char *s, bool *res)
 }
 
 int match_string(const char * const *array, size_t n, const char *string);
+int __sysfs_strmatch(const char * const *array, size_t n, const char *string);
+
+/**
+ * sysfs_strmatch - matches given string in an array
+ * @a: array of strings
+ * @s: string to match with
+ *
+ * Helper for __sysfs_strmatch(). Calculates the size of @a automatically.
+ */
+#define sysfs_strmatch(a, s) __sysfs_strmatch(a, ARRAY_SIZE(a), s)
 
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
lib/string.c +26 -0
index ed83562..a4fe035 100644
@@ -656,6 +656,32 @@ int match_string(const char * const *array, size_t n, const char *string)
 }
 EXPORT_SYMBOL(match_string);
 
+/**
+ * __sysfs_strmatch - matches given string in an array
+ * @array: array of strings
+ * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @str: string to match with
+ *
+ * Returns index of @str in the @array or -EINVAL, just like match_string().
+ * Uses sysfs_streq() instead of strcmp for matching.
+ */
+int __sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+       const char *item;
+       int index;
+
+       for (index = 0; index < n; index++) {
+               item = array[index];
+               if (!item)
+                       break;
+               if (!sysfs_streq(item, str))
+                       return index;
+       }
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL(__sysfs_strmatch);
+
 #ifndef __HAVE_ARCH_MEMSET
 /**
  * memset - Fill a region of memory with the given value
Signature · 1 line
heikki
#21 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 3 lines

Yes, though I don't KOBJ_ADD separately with the partners and cables. That uevent is sent when the device for them is registered, so it's already there

Makes sense. Thanks Heikki.

Regards,
Badhri

Quoted 90 lines · Heikki Krogerus

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 9 lines
lib/string.c +9 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+   const char *item;
+   int index;
+
+   for (index = 0; index < n; index++) {
+           item = array[index];
+           if (!item)
+                   break;
Quoted 8 lines

lib/string.c int match_string(const char * const *array, size_t n, const char *string) · line 682

Quoted 6 lines
lib/string.c +6 -0
+           if (sysfs_streq(item, str))
+                   return index;
+   }
+
+   return -EINVAL;
+}

should we make this a core sysfs function?

Last question before I send v11. Is the following (the helper) OK?

include/linux/string.h +10 -0
index 26b6f6a..5606810 100644
@@ -135,6 +135,16 @@ static inline int strtobool(const char *s, bool *res)
 }
 
 int match_string(const char * const *array, size_t n, const char *string);
+int __sysfs_strmatch(const char * const *array, size_t n, const char *string);
+
+/**
+ * sysfs_strmatch - matches given string in an array
+ * @a: array of strings
+ * @s: string to match with
+ *
+ * Helper for __sysfs_strmatch(). Calculates the size of @a automatically.
+ */
+#define sysfs_strmatch(a, s) __sysfs_strmatch(a, ARRAY_SIZE(a), s)
 
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
lib/string.c +26 -1
index ed83562..a4fe035 100644
@@ -656,6 +656,32 @@ int match_string(const char * const *array, size_t n, const char *string)
 }
 EXPORT_SYMBOL(match_string);
 
+/**
+ * __sysfs_strmatch - matches given string in an array
+ * @array: array of strings
+ * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @str: string to match with
+ *
+ * Returns index of @str in the @array or -EINVAL, just like match_string().
+ * Uses sysfs_streq() instead of strcmp for matching.
+ */
+int __sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+       const char *item;
+       int index;
+
+       for (index = 0; index < n; index++) {
+               item = array[index];
+               if (!item)
+                       break;
+               if (!sysfs_streq(item, str))
+                       return index;
+       }
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL(__sysfs_strmatch);
+
 #ifndef __HAVE_ARCH_MEMSET
 /**
  * memset - Fill a region of memory with the given value
 
 
--

heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#22 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

In reply to Heikki Krogerus
Quoted 32 lines · Heikki Krogerus

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 9 lines
lib/string.c +9 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+	const char *item;
+	int index;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
Quoted 8 lines

lib/string.c int match_string(const char * const *array, size_t n, const char *string) · line 682

Quoted 6 lines
lib/string.c +6 -0
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}

should we make this a core sysfs function?

Last question before I send v11. Is the following (the helper) OK?

include/linux/string.h +0 -0
index 26b6f6a..5606810 100644
@@ -135,6 +135,16 @@ static inline int strtobool(const char *s, bool *res)
 }
 
 int match_string(const char * const *array, size_t n, const char *string);

include/linux/string.h static inline int strtobool(const char *s, bool *res) · line 147

Quoted 10 lines
include/linux/string.h +10 -0
+int __sysfs_strmatch(const char * const *array, size_t n, const char *string);
+
+/**
+ * sysfs_strmatch - matches given string in an array
+ * @a: array of strings
+ * @s: string to match with
+ *
+ * Helper for __sysfs_strmatch(). Calculates the size of @a automatically.
+ */
+#define sysfs_strmatch(a, s) __sysfs_strmatch(a, ARRAY_SIZE(a), s)

People will bikeshed the name. Why not just use sysfs_match_string() as this does the same as match_string, but calls sysfs_string instead of strcmp().

thanks,

greg k-h

author maintainer
#23 lore raw

Re: [PATHCv10 1/2] usb: USB Type-C connector class

Quoted 32 lines · Greg KH
Quoted 31 lines · Heikki Krogerus

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 9 lines
lib/string.c +9 -0
+static int sysfs_strmatch(const char * const *array, size_t n, const char *str)
+{
+	const char *item;
+	int index;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
Quoted 8 lines

lib/string.c int match_string(const char * const *array, size_t n, const char *string) · line 682

Quoted 6 lines
lib/string.c +6 -0
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}

should we make this a core sysfs function?

Last question before I send v11. Is the following (the helper) OK?

include/linux/string.h +0 -0
index 26b6f6a..5606810 100644
@@ -135,6 +135,16 @@ static inline int strtobool(const char *s, bool *res)
 }
 
Quoted 15 lines

include/linux/string.h static inline int strtobool(const char *s, bool *res) · line 147

Quoted 11 lines
include/linux/string.h +10 -0
 int match_string(const char * const *array, size_t n, const char *string);
+int __sysfs_strmatch(const char * const *array, size_t n, const char *string);
+
+/**
+ * sysfs_strmatch - matches given string in an array
+ * @a: array of strings
+ * @s: string to match with
+ *
+ * Helper for __sysfs_strmatch(). Calculates the size of @a automatically.
+ */
+#define sysfs_strmatch(a, s) __sysfs_strmatch(a, ARRAY_SIZE(a), s)

People will bikeshed the name. Why not just use sysfs_match_string() as this does the same as match_string, but calls sysfs_string instead of strcmp().

Makes sense. I'll change the name.

Thanks,

Signature · 1 line
heikki
author maintainer
#24 lore raw

PATCH 2/2 usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY

In reply to Heikki Krogerus

This adds driver for the USB Type-C PHY on Intel WhiskeyCove PMIC which is available on some of the Intel Broxton SoC based platforms.

---
 drivers/usb/typec/Kconfig       |  14 ++
 drivers/usb/typec/Makefile      |   1 +
 drivers/usb/typec/typec_wcove.c | 372 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 387 insertions(+)
 create mode 100644 drivers/usb/typec/typec_wcove.c
drivers/usb/typec/Kconfig +14 -0
index b229fb9..7a345a4 100644
@@ -4,4 +4,18 @@ menu "USB PD and Type-C drivers"
 config TYPEC
 	tristate
 
+config TYPEC_WCOVE
+	tristate "Intel WhiskeyCove PMIC USB Type-C PHY driver"
+	depends on ACPI
+	depends on INTEL_SOC_PMIC
+	depends on INTEL_PMC_IPC
+	select TYPEC
+	help
+	  This driver adds support for USB Type-C detection on Intel Broxton
+	  platforms that have Intel Whiskey Cove PMIC. The driver can detect the
+	  role and cable orientation.
+
+	  To compile this driver as module, choose M here: the module will be
+	  called typec_wcove
+
 endmenu
drivers/usb/typec/Makefile +1 -0
index 1012a8b..b9cb862 100644
@@ -1 +1,2 @@
 obj-$(CONFIG_TYPEC)		+= typec.o
+obj-$(CONFIG_TYPEC_WCOVE)	+= typec_wcove.o
drivers/usb/typec/typec_wcove.c new file +372 -0

372 lines not on this page · raw message

Signature · 1 line
2.9.3
#25 lore raw
In reply to Heikki Krogerus
Quoted 3 lines · Heikki Krogerus

The USB Type-C class is meant to provide unified interface to the userspace to present the USB Type-C ports in a system.

Any idea what is happening with this patch series ?

There was no further feedback (at least as far as I know), the series seems to be ready to go, yet I don't see it in -next.

Thanks,
Guenter

Quoted 73 lines

Changes since v9:
- Minor typec_wcove.c cleanup as proposed by Guenter Roeck. No function affect.

Changes since v8:
- checking sysfs_streq() result correctly in sysfs_strmatch
- fixed accessory check in supported_accessory_mode
- using "none" as the only string that can clear the preferred role

Changes since v7:
- Removed "type" attribute from partners
- Added supports_usb_power_delivery attribute for partner and cable

Changes since v6:
- current_vconn_role attr renamed to vconn_source (no API changes)
- Small documentation improvements proposed by Vincent Palatin

Changes since v5:
- Only updating the roles based on driver notifications
- Added MODULE_ALIAS for the WhiskeyCove module
- Including the patch that creates the actual platform device for the WhiskeyCove Type-C PHY in this series.

Changes since v4:
- Remove the port lock completely

Changes since v3:
- Documentation cleanup as proposed by Roger Quadros
- Setting partner altmodes member to NULL on removal and fixing a warning, as proposed by Guenter Roeck
- Added the following attributes for partners and cables:
* supports_usb_power_delivery
* id_header_vdo
- "id_header_vdo" is visible only when the partner or cable supports USB Power Delivery communication.
- Partner attribute "accessory" is hidden when the partner type is not "Accessory".

Changes since v2:
- Notification on role and alternate mode changes
- cleanups

Changes since v1:
- Completely rewrote alternate mode support
- Patners, cables and cable plugs presented as devices.

Heikki Krogerus (2):
usb: USB Type-C connector class
usb: typec: add driver for Intel Whiskey Cove PMIC USB Type-C PHY

 Documentation/ABI/testing/sysfs-class-typec |  218 ++++++
 Documentation/usb/typec.txt                 |  103 +++
 MAINTAINERS                                 |    9 +
 drivers/usb/Kconfig                         |    2 +
 drivers/usb/Makefile                        |    2 +
 drivers/usb/typec/Kconfig                   |   21 +
 drivers/usb/typec/Makefile                  |    2 +
 drivers/usb/typec/typec.c                   | 1075 +++++++++++++++++++++++++++
 drivers/usb/typec/typec_wcove.c             |  372 +++++++++
 include/linux/usb/typec.h                   |  252 +++++++
 10 files changed, 2056 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 drivers/usb/typec/typec_wcove.c
 create mode 100644 include/linux/usb/typec.h
Signature · 1 line
2.9.3
author maintainer
#26 lore raw
Quoted 8 lines · Guenter Roeck
Quoted 3 lines · Heikki Krogerus

The USB Type-C class is meant to provide unified interface to the userspace to present the USB Type-C ports in a system.

Any idea what is happening with this patch series ?

There was no further feedback (at least as far as I know), the series seems to be ready to go, yet I don't see it in -next.

I think Greg has only started to put together his usb-next branch for this cycle.

Cheers,

Signature · 1 line
heikki
maintainer
#27 lore raw
Quoted 5 lines · Heikki Krogerus
Quoted 4 lines · Guenter Roeck
Quoted 3 lines · Heikki Krogerus

The USB Type-C class is meant to provide unified interface to the userspace to present the USB Type-C ports in a system.

Quoted 7 lines
Quoted 4 lines

Any idea what is happening with this patch series ?

There was no further feedback (at least as far as I know), the series seems to be ready to go, yet I don't see it in -next.

I think Greg has only started to put together his usb-next branch for this cycle.

I was waiting to see if anyone else had objections about this before reviewing it myself :)

I'll get to it later today...

thanks,

greg k-h