Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: typec: tcpm: allow external charger detection to set USB_TYPE
@ 2026-11-06 11:57 Daniel Baluta
  2026-11-06 12:10 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Baluta @ 2026-11-06 11:57 UTC (permalink / raw)
  To: badhri, heikki.krogerus
  Cc: gregkh, linux-usb, linux-kernel, stefan.ghetu, imx, Li Jun,
	Daniel Baluta

From: Li Jun <jun.li@nxp.com>

Teach tcpm_psy_set_prop() to accept writes of
POWER_SUPPLY_PROP_USB_TYPE and store the value in port->usb_type, and
advertise the BC1.2 USB types (SDP/DCP/CDP/ACA) in
psy_desc.usb_types so power_supply_set_property() callers are not
rejected by power_supply_has_property().

This is a prerequisite for a follow-up PHY charger-detection driver
that calls power_supply_set_property() on the tcpm-backed power
supply once it determines the attached BC1.2 charger type.

Signed-off-by: Li Jun <jun.li@nxp.com>
Signed-off-by: Daniel Baluta <daniel.baluta@nxp.com>
---
 drivers/usb/typec/tcpm/tcpm.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index d721baef52104..24f4e8854f37f 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -8766,6 +8766,10 @@ static int tcpm_psy_set_prop(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_CURRENT_NOW:
 		ret = tcpm_aug_set_op_curr(port, val->intval / 1000);
 		break;
+	case POWER_SUPPLY_PROP_USB_TYPE:
+		port->usb_type = val->intval;
+		ret = 0;
+		break;
 	default:
 		ret = -EINVAL;
 		break;
@@ -8807,7 +8811,11 @@ static int devm_tcpm_psy_register(struct tcpm_port *port)
 		 port_dev_name);
 	port->psy_desc.name = psy_name;
 	port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
-	port->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C)  |
+	port->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) |
+				   BIT(POWER_SUPPLY_USB_TYPE_DCP) |
+				   BIT(POWER_SUPPLY_USB_TYPE_CDP) |
+				   BIT(POWER_SUPPLY_USB_TYPE_ACA) |
+				   BIT(POWER_SUPPLY_USB_TYPE_C)  |
 				   BIT(POWER_SUPPLY_USB_TYPE_PD) |
 				   BIT(POWER_SUPPLY_USB_TYPE_PD_PPS) |
 				   BIT(POWER_SUPPLY_USB_TYPE_PD_PPS_SPR_AVS) |
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: typec: tcpm: allow external charger detection to set USB_TYPE
  2026-11-06 11:57 [PATCH] usb: typec: tcpm: allow external charger detection to set USB_TYPE Daniel Baluta
@ 2026-11-06 12:10 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-06 12:10 UTC (permalink / raw)
  To: Daniel Baluta; +Cc: linux-usb, imx, Frank.Li

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] usb: typec: tcpm: tcpm_psy_set_prop() rejects USB_TYPE on
  non-PD ports
- [Medium] usb: typec: tcpm: unlocked usb_type write in
  tcpm_psy_set_prop()
- [Low] usb: typec: tcpm: inaccurate psy_desc.usb_types claim in commit
  message
--

commit 5e903d3047946eea57f9c3fe92ba30e6462da7b9
Author: Li Jun <jun.li@nxp.com>

usb: typec: tcpm: allow external charger detection to set USB_TYPE

This commit teaches tcpm_psy_set_prop() to accept POWER_SUPPLY_PROP_USB_TYPE
and adds the BC1.2 USB types to psy_desc.usb_types.

>     Teach tcpm_psy_set_prop() to accept writes of
>     POWER_SUPPLY_PROP_USB_TYPE and store the value in port->usb_type, and
>     advertise the BC1.2 USB types (SDP/DCP/CDP/ACA) in
>     psy_desc.usb_types so power_supply_set_property() callers are not
>     rejected by power_supply_has_property().

[Severity: Low]
Is the commit message accurate about how psy_desc.usb_types is checked?

power_supply_set_property() calls psy->desc->set_property() directly, and
power_supply_has_property() only checks psy->desc->properties.  Instead,
psy_desc.usb_types is used by power_supply_format_property() and
power_supply_show_enum_with_available() when formatting
POWER_SUPPLY_PROP_USB_TYPE for sysfs reads and uevents.

> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index d721baef52104..24f4e8854f37f 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -8766,6 +8766,10 @@ static int tcpm_psy_set_prop(struct power_supply *psy,
>  	case POWER_SUPPLY_PROP_CURRENT_NOW:
>  		ret = tcpm_aug_set_op_curr(port, val->intval / 1000);
>  		break;
> +	case POWER_SUPPLY_PROP_USB_TYPE:
> +		port->usb_type = val->intval;
> +		ret = 0;
> +		break;

[Severity: Medium]
Does tcpm_psy_set_prop() reject POWER_SUPPLY_PROP_USB_TYPE on non-PD ports?

At the start of tcpm_psy_set_prop(), the !port->pd_supported check still
runs unconditionally before the switch statement:

drivers/usb/typec/tcpm/tcpm.c:tcpm_psy_set_prop() {
	/*
	 * All the properties below are related to USB PD. The check needs to be
	 * property specific when a non-pd related property is added.
	 */
	if (!port->pd_supported)
		return -EOPNOTSUPP;
}

On a Type-C port with pd-disable set, power_supply_set_property() for
POWER_SUPPLY_PROP_USB_TYPE will always fail with -EOPNOTSUPP instead of
updating port->usb_type.

[Severity: Medium]
Can this unlocked write to port->usb_type race with the TCPM state machine
and overwrite a negotiated PD type or a disconnect reset?

All TCPM updates to port->usb_type in tcpm_pd_select_pdo() and
tcpm_reset_port() run under port->lock, while BC1.2 detection runs
asynchronously in PHY workqueues after tcpm_mux_set() sets USB_ROLE_DEVICE:

CPU0 (TCPM kthread)               CPU1 (PHY charger work)
tcpm_pd_select_pdo()
  mutex_lock(&port->lock)
  port->usb_type =
    POWER_SUPPLY_USB_TYPE_PD;
  mutex_unlock(&port->lock)
                                  power_supply_set_property()
                                    tcpm_psy_set_prop()
                                      port->usb_type = val->intval;
                                      <-- clobbers PD usb_type with BC1.2

If BC1.2 detection finishes after PD capability negotiation (or after a
Data Role Swap to device mode) or after unplugging the cable,
tcpm_psy_set_prop() overwrites POWER_SUPPLY_USB_TYPE_PD or
POWER_SUPPLY_USB_TYPE_C with a stale BC1.2 type because it neither holds
port->lock nor checks port->attached and port->pd_capable.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261106115742.730704-1-daniel.baluta@nxp.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-06 12:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-06 11:57 [PATCH] usb: typec: tcpm: allow external charger detection to set USB_TYPE Daniel Baluta
2026-11-06 12:10 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox