Skip to contentSkip to conversation

Some fixes for select_usb_power_delivery

PATCH In review Kyle Tso 2 participants 7 messages Jun 23, 2023 – Jun 30, 2023

Subsystems (3)
lore
Share & tools
lore

Kyle Tso

author
#0 lore raw

v2 cover Some fixes for select_usb_power_delivery

Update v2

- Add "Cc: [email protected]" to each patch

== original cover letter ==

Hi, here are some fixes about the attribute "select_usb_power_delivery" in typec/class.c

===

usb: typec: Set port->pd before adding device for typec_port

This one is about the visibility of the attribute. port->pd should be set before device_add otherwise the visibility will be false because port->pd is NULL.

===

usb: typec: Iterate pds array when showing the pd list

This patch fixes a problem about the incorrect fetching of the pointers to each usb_power_delivery handle.

===

usb: typec: Use sysfs_emit_at when concatenating the string

This patch changes the use of the API from sysfs_emit to sysfs_emit_at because the buffer address is not aligned to PAGE_SIZE.

===

Kyle Tso (3):
usb: typec: Set port->pd before adding device for typec_port
usb: typec: Iterate pds array when showing the pd list
usb: typec: Use sysfs_emit_at when concatenating the string

 drivers/usb/typec/class.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author
#1 lore raw

PATCH v2 1/3 usb: typec: Set port->pd before adding device for typec_port

When calling device_add in the registration of typec_port, it will do the NULL check on usb_power_delivery handle in typec_port for the visibility of the device attributes. It is always NULL because port->pd is set in typec_port_set_usb_power_delivery which is later than the device_add call.

Set port->pd before device_add and only link the device after that.

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

---
update v2:
- Add "Cc: [email protected]"

drivers/usb/typec/class.c | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/typec/class.c +3 -1
index faa184ae3dac..3b30948bf4b0 100644
@@ -2288,6 +2288,8 @@ struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
+	port->pd = cap->pd;
+
 	ret = device_add(&port->dev);
 	if (ret) {
 		dev_err(parent, "failed to register port (%d)\n", ret);
@@ -2295,7 +2297,7 @@ struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
-	ret = typec_port_set_usb_power_delivery(port, cap->pd);
+	ret = usb_power_delivery_link_device(port->pd, &port->dev);
 	if (ret) {
 		dev_err(&port->dev, "failed to link pd\n");
 		device_unregister(&port->dev);
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer Acked-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] usb: typec: Set port->pd before adding device for typec_port

Quoted 11 lines · Kyle Tso

When calling device_add in the registration of typec_port, it will do the NULL check on usb_power_delivery handle in typec_port for the visibility of the device attributes. It is always NULL because port->pd is set in typec_port_set_usb_power_delivery which is later than the device_add call.

Set port->pd before device_add and only link the device after that.

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

thanks,

Signature · 1 line
heikki
author
#3 lore raw

PATCH v2 2/3 usb: typec: Iterate pds array when showing the pd list

In reply to Kyle Tso

The pointers of each usb_power_delivery handles are stored in "pds" array returned from the pd_get ops but not in the adjacent memory calculated from "pd". Get the handles from "pds" array directly instead of deriving them from "pd".

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

---
Update v2:
- Add "Cc: [email protected]"

drivers/usb/typec/class.c | 11 +++++------

 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/usb/typec/class.c +5 -6
index 3b30948bf4b0..e7312295f8c9 100644
@@ -1277,8 +1277,7 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,
 {
 	struct typec_port *port = to_typec_port(dev);
 	struct usb_power_delivery **pds;
-	struct usb_power_delivery *pd;
-	int ret = 0;
+	int i, ret = 0;
 
 	if (!port->ops || !port->ops->pd_get)
 		return -EOPNOTSUPP;
@@ -1287,11 +1286,11 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,
 	if (!pds)
 		return 0;
 
-	for (pd = pds[0]; pd; pd++) {
-		if (pd == port->pd)
-			ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pd->dev));
+	for (i = 0; pds[i]; i++) {
+		if (pds[i] == port->pd)
+			ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pds[i]->dev));
 		else
-			ret += sysfs_emit(buf + ret, "%s ", dev_name(&pd->dev));
+			ret += sysfs_emit(buf + ret, "%s ", dev_name(&pds[i]->dev));
 	}
 
 	buf[ret - 1] = '\n';
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer Acked-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] usb: typec: Iterate pds array when showing the pd list

Quoted 8 lines · Kyle Tso

The pointers of each usb_power_delivery handles are stored in "pds" array returned from the pd_get ops but not in the adjacent memory calculated from "pd". Get the handles from "pds" array directly instead of deriving them from "pd".

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

thanks,

Signature · 1 line
heikki
author
#5 lore raw

PATCH v2 3/3 usb: typec: Use sysfs_emit_at when concatenating the string

In reply to Kyle Tso

The buffer address used in sysfs_emit should be aligned to PAGE_SIZE. Use sysfs_emit_at instead to offset the buffer.

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")

---
Update v2:
- Add "Cc: [email protected]"

drivers/usb/typec/class.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/typec/class.c +2 -2
index e7312295f8c9..9c1dbf3c00e0 100644
@@ -1288,9 +1288,9 @@ static ssize_t select_usb_power_delivery_show(struct device *dev,
 
 	for (i = 0; pds[i]; i++) {
 		if (pds[i] == port->pd)
-			ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pds[i]->dev));
+			ret += sysfs_emit_at(buf, ret, "[%s] ", dev_name(&pds[i]->dev));
 		else
-			ret += sysfs_emit(buf + ret, "%s ", dev_name(&pds[i]->dev));
+			ret += sysfs_emit_at(buf, ret, "%s ", dev_name(&pds[i]->dev));
 	}
 
 	buf[ret - 1] = '\n';
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer Acked-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] usb: typec: Use sysfs_emit_at when concatenating the string

Quoted 6 lines · Kyle Tso

The buffer address used in sysfs_emit should be aligned to PAGE_SIZE. Use sysfs_emit_at instead to offset the buffer.

Fixes a7cff92f0635 ("usb: typec: USB Power Delivery helpers for ports and partners")
Signature · 1 line
heikki