Skip to contentSkip to conversation

usb: USB Type-C connector class

PATCH Guenter Roeck 2 participants 2 messages Sep 8, 2016 – Sep 9, 2016

lore
Share & tools
lore
#0 lore raw
Quoted 9 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.

---

...

Review

Quoted 11 lines
(quoted diff) +11 -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;
+		if (!sysfs_streq(item, str))

This doesn't work ... sysfs_streq() returns true if there is a match, so the "!" is wrong.

Quoted 6 lines
(quoted diff) +6 -0
+			return index;
+	}
+
+	return -EINVAL;
+}
+

[ ... ]

Quoted 13 lines
(quoted diff) +13 -0
+
+static ssize_t
+preferred_role_store(struct device *dev, struct device_attribute *attr,
+		     const char *buf, size_t size)
+{
+	struct typec_port *port = to_typec_port(dev);
+	enum typec_role role;
+	int ret;
+
+	if (port->cap->type != TYPEC_PORT_DRP) {
+		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
+		return -EOPNOTSUPP;
+	}

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	if (!port->cap->try_role) {
+		dev_dbg(dev, "Setting preferred role not supported\n");
+		return -EOPNOTSUPP;
+	}
+
+	ret = sysfs_strmatch(typec_roles, ARRAY_SIZE(typec_roles), buf);
+	if (ret < 0) {
+		port->prefer_role = -1;
+		return size;

Are you sure about that ? It is kind of unusual to accept "bad" strings. Why not return -EINVAL ?

Quoted 12 lines
(quoted diff) +12 -0
+	}
+
+	role = ret;
+
+	ret = port->cap->try_role(port->cap, role);
+	if (ret)
+		return ret;
+
+	port->prefer_role = role;
+	return size;
+}
+

[ ... ]

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+static ssize_t supported_accessory_modes_show(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct typec_port *port = to_typec_port(dev);
+	ssize_t ret = 0;
+	int i;
+
+	if (!port->cap->accessory[0])

You probably want

	if (!port->cap->accessory)

here. Otherwise the check is quite pointless (and crashes if the pointer is NULL).

Review

Quoted 9 lines
(quoted diff) +9 -0
+		return 0;
+
+	for (i = 0; port->cap->accessory[i]; i++)
+		ret += sprintf(buf + ret, "%s\n",
+			       typec_accessory_modes[port->cap->accessory[i]]);
+	return ret;
+}
+static DEVICE_ATTR_RO(supported_accessory_modes);
+

Guenter

#1 lore raw
Quoted 12 lines · Guenter Roeck
Quoted 9 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 14 lines

Review

Quoted 11 lines
(quoted diff) +11 -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;
+		if (!sysfs_streq(item, str))

This doesn't work ... sysfs_streq() returns true if there is a match, so the "!" is wrong.

Oh no! I've been testing (stupidly) with a patch on top where I move this function to lib/string.c (I guess I had this correctly in that). Sorry about this.

Quoted 25 lines
Quoted 6 lines
(quoted diff) +6 -0
+			return index;
+	}
+
+	return -EINVAL;
+}
+

[ ... ]

Quoted 18 lines
(quoted diff) +18 -0
+
+static ssize_t
+preferred_role_store(struct device *dev, struct device_attribute *attr,
+		     const char *buf, size_t size)
+{
+	struct typec_port *port = to_typec_port(dev);
+	enum typec_role role;
+	int ret;
+
+	if (port->cap->type != TYPEC_PORT_DRP) {
+		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
+		return -EOPNOTSUPP;
+	}
+
+	if (!port->cap->try_role) {
+		dev_dbg(dev, "Setting preferred role not supported\n");
+		return -EOPNOTSUPP;
+	}
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+	ret = sysfs_strmatch(typec_roles, ARRAY_SIZE(typec_roles), buf);
+	if (ret < 0) {
+		port->prefer_role = -1;
+		return size;

Are you sure about that ? It is kind of unusual to accept "bad" strings. Why not return -EINVAL ?

I'm considering a "bad" string as clearing the preference. I did explain this in the ABI document. But I suppose it's better to define a specific string also for clearing. I'll change this.

Quoted 25 lines
Quoted 12 lines
(quoted diff) +12 -0
+	}
+
+	role = ret;
+
+	ret = port->cap->try_role(port->cap, role);
+	if (ret)
+		return ret;
+
+	port->prefer_role = role;
+	return size;
+}
+

[ ... ]

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+static ssize_t supported_accessory_modes_show(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct typec_port *port = to_typec_port(dev);
+	ssize_t ret = 0;
+	int i;
+
+	if (!port->cap->accessory[0])

You probably want

Quoted 3 lines
	if (!port->cap->accessory)

here. Otherwise the check is quite pointless (and crashes if the pointer is NULL).

OK.

Thanks,

Signature · 1 line
heikki