Skip to contentSkip to conversation

usb: typec: qcom-pmic-typec: properly tear down port on remove and shutdown

PATCH Superseded Neil Armstrong 3 participants 8 messages Nov 20

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover usb: typec: qcom-pmic-typec: properly tear down port on remove and shutdown

The current driver doesn't leave the port in the same state as found when probing, and leaves the port enabled on system shutdown.

---
Neil Armstrong (2):
usb: typec: qcom-pmic-typec: pdphy: disable the PD PHY on stop
usb: typec: qcom-pmic-typec: detach the port on shutdown

 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c       | 11 +++++++++++
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.h       |  1 +
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c |  2 +-
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c  | 11 +++++++++++
 4 files changed, 24 insertions(+), 1 deletion(-)

---
base-commit: adcad7f4e6332cff3d6d7165f41584899f598534
change-id: 20261120-topic-tcpm-qcom-fixes-c9ce1e65c570

Best regards,

Signature · 1 line
Neil Armstrong <[email protected]>
#1 lore raw

PATCH 1/2 usb: typec: qcom-pmic-typec: pdphy: disable the PD PHY on stop

qcom_pmic_typec_pdphy_stop() only terminates the ongoing transmission and clears the RX frame filter, the PD PHY itself stays enabled.

Use qcom_pmic_typec_pdphy_disable() so the PD PHY is also turned off before its supply is released, leaving it in the same state as before qcom_pmic_typec_pdphy_start().

Assisted-by LLM
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +1 -1
index 435e07659d65..08cf65951796 100644
@@ -545,7 +545,7 @@ static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm)
 
 	cancel_work_sync(&pmic_typec_pdphy->reset_work);
 
-	qcom_pmic_typec_pdphy_reset_on(pmic_typec_pdphy);
+	qcom_pmic_typec_pdphy_disable(pmic_typec_pdphy);
 
 	regulator_disable(pmic_typec_pdphy->vdd_pdphy);
 }
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] usb: typec: qcom-pmic-typec: pdphy: disable the PD PHY on stop

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] usb: typec: qcom-pmic-typec: missing Fixes trailer for pdphy stop fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] usb: typec: qcom-pmic-typec: missing Fixes trailer for pdphy stop fix
--

commit b45aad2ca233d0a40118f71870955c8a4770e204
Author: Neil Armstrong <[email protected]>

Quoted 11 lines

usb: typec: qcom-pmic-typec: pdphy: disable the PD PHY on stop

qcom_pmic_typec_pdphy_stop() only terminates the ongoing transmission and clears the RX frame filter, the PD PHY itself stays enabled.

Use qcom_pmic_typec_pdphy_disable() so the PD PHY is also turned off before its supply is released, leaving it in the same state as before qcom_pmic_typec_pdphy_start().

Assisted-by LLM

[Severity: Low]
This fixes qcom_pmic_typec_pdphy_stop() leaving the PD PHY enabled when stopping. Should this include:

Fixes a4422ff22142 ("usb: typec: qcom: Add Qualcomm PMIC Type-C driver")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] usb: typec: qcom-pmic-typec: pdphy: disable the PD PHY on stop

In reply to Neil Armstrong
Quoted 19 lines · Neil Armstrong

qcom_pmic_typec_pdphy_stop() only terminates the ongoing transmission and clears the RX frame filter, the PD PHY itself stays enabled.

Use qcom_pmic_typec_pdphy_disable() so the PD PHY is also turned off before its supply is released, leaving it in the same state as before qcom_pmic_typec_pdphy_start().

Assisted-by LLM
---
  drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +0 -0
index 435e07659d65..08cf65951796 100644

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm) · line 551

Quoted 8 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +1 -1
@@ -545,7 +545,7 @@ static void qcom_pmic_typec_pdphy_stop(struct pmic_typec *tcpm)
 
  	cancel_work_sync(&pmic_typec_pdphy->reset_work);
 
-	qcom_pmic_typec_pdphy_reset_on(pmic_typec_pdphy);
+	qcom_pmic_typec_pdphy_disable(pmic_typec_pdphy);
 
  	regulator_disable(pmic_typec_pdphy->vdd_pdphy);
  }
#4 lore raw

PATCH 2/2 usb: typec: qcom-pmic-typec: detach the port on shutdown

In reply to Neil Armstrong

On reboot or power off the port is left as is: a source partner keeps the negotiated contract, possibly above 5V, and a sink partner stays powered by the OTG boost while nothing services the port anymore.

Add a shutdown callback stopping the port and the PD PHY, unregistering the TCPM port, which turns VBUS and VCONN off, and finally disabling the Type-C block so the partner sees a detach.

Assisted-by LLM
---
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c      | 11 +++++++++++
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.h      |  1 +
 drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c | 11 +++++++++++
 3 files changed, 23 insertions(+)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +11 -0
index 58d1e6aae637..8169db478b8a 100644
@@ -134,6 +134,16 @@ static void qcom_pmic_typec_remove(struct platform_device *pdev)
 	fwnode_handle_put(tcpm->tcpc.fwnode);
 }
 
+static void qcom_pmic_typec_shutdown(struct platform_device *pdev)
+{
+	struct pmic_typec *tcpm = platform_get_drvdata(pdev);
+
+	tcpm->pdphy_stop(tcpm);
+	tcpm->port_stop(tcpm);
+	tcpm_unregister_port(tcpm->tcpm_port);
+	tcpm->port_disable(tcpm);
+}
+
 static const struct pmic_typec_resources pm8150b_typec_res = {
 	.pdphy_res = &pm8150b_pdphy_res,
 	.port_res = &pm8150b_port_res,
@@ -158,6 +168,7 @@ static struct platform_driver qcom_pmic_typec_driver = {
 	},
 	.probe = qcom_pmic_typec_probe,
 	.remove = qcom_pmic_typec_remove,
+	.shutdown = qcom_pmic_typec_shutdown,
 };
 
 module_platform_driver(qcom_pmic_typec_driver);
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.h +1 -0
index 3c75820c9187..0818582fe850 100644
@@ -20,6 +20,7 @@ struct pmic_typec {
 	int (*port_start)(struct pmic_typec *tcpm,
 			  struct tcpm_port *tcpm_port);
 	void (*port_stop)(struct pmic_typec *tcpm);
+	void (*port_disable)(struct pmic_typec *tcpm);
 };
 
 #define tcpc_to_tcpm(_tcpc_) container_of(_tcpc_, struct pmic_typec, tcpc)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +11 -0
index b2ad31e85b5e..4dc302d732e8 100644
@@ -700,6 +700,16 @@ static void qcom_pmic_typec_port_stop(struct pmic_typec *tcpm)
 	disable_delayed_work_sync(&pmic_typec_port->cc_debounce_dwork);
 }
 
+static void qcom_pmic_typec_port_disable(struct pmic_typec *tcpm)
+{
+	struct pmic_typec_port *pmic_typec_port = tcpm->pmic_typec_port;
+
+	/* Present neither Rp nor Rd, so a connected partner detects a detach */
+	regmap_write(pmic_typec_port->regmap,
+		     pmic_typec_port->base + TYPEC_MODE_CFG_REG,
+		     TYPEC_DISABLE_CMD);
+}
+
 int qcom_pmic_typec_port_probe(struct platform_device *pdev,
 			       struct pmic_typec *tcpm,
 			       const struct pmic_typec_port_resources *res,
@@ -779,6 +789,7 @@ int qcom_pmic_typec_port_probe(struct platform_device *pdev,
 
 	tcpm->port_start = qcom_pmic_typec_port_start;
 	tcpm->port_stop = qcom_pmic_typec_port_stop;
+	tcpm->port_disable = qcom_pmic_typec_port_disable;
 
 	return 0;
 }
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic-typec: detach the port on shutdown

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: typec: qcom: NULL deref in qcom_pmic_typec_shutdown() when VBUS is on - [Medium] usb: typec…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] usb: typec: qcom: NULL deref in qcom_pmic_typec_shutdown() when VBUS is on
- [Medium] usb: typec: qcom-pmic-typec: missing port_disable() in remove and probe unwind
--

commit 27e120f037d3faf6c51dac7774569601de458e75
Author: Neil Armstrong <[email protected]>

usb: typec: qcom-pmic-typec: detach the port on shutdown

This commit adds a platform driver shutdown callback that stops the port and PD PHY, unregisters the TCPM port, and disables the Type-C block so that a connected partner sees a detach on reboot or power off.

Quoted 5 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +0 -0
index 58d1e6aae637d..8169db478b8aa 100644

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c static void qcom_pmic_typec_remove(struct platform_device *pdev) · line 143

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +7 -0
@@ -134,6 +134,16 @@ static void qcom_pmic_typec_remove(struct platform_device *pdev)
 	fwnode_handle_put(tcpm->tcpc.fwnode);
 }
 
+static void qcom_pmic_typec_shutdown(struct platform_device *pdev)
+{
+	struct pmic_typec *tcpm = platform_get_drvdata(pdev);
+
+	tcpm->pdphy_stop(tcpm);
+	tcpm->port_stop(tcpm);
+	tcpm_unregister_port(tcpm->tcpm_port);

[Severity: High]
Can calling tcpm_unregister_port() here cause a NULL pointer dereference on port->wq when shutting down with an attached sink partner powered by VBUS?

tcpm_unregister_port() destroys port->wq and sets it to NULL before calling tcpm_reset_port() -> tcpm_init_vbus() -> qcom_pmic_typec_port_set_vbus(). When VBUS transitions from enabled to disabled, qcom_pmic_typec_port_set_vbus() calls tcpm_vbus_change(), which queues work onto port->wq:

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c:qcom_pmic_typec_port_set_vbus() {
	...
	ret = qcom_pmic_typec_port_vbus_toggle(pmic_typec_port, on);
	if (ret)
		goto done;

	pmic_typec_port->vbus_enabled = on;
	tcpm_vbus_change(tcpm->tcpm_port);
	...
}

kthread_queue_work() then dereferences NULL in &worker->lock, crashing before tcpm->port_disable(tcpm) is reached.

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c static void qcom_pmic_typec_remove(struct platform_device *pdev) · line 145

Quoted 2 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +2 -0
+	tcpm->port_disable(tcpm);
+}

[Severity: Medium]
Should tcpm->port_disable(tcpm) also be called in qcom_pmic_typec_remove() and the port_unregister error path in qcom_pmic_typec_probe()?

Without it, unbinding the driver or failing probe after tcpm_register_port() leaves TYPEC_MODE_CFG_REG presenting Rp or Rd with no TCPM or PD PHY servicing the port, and a subsequent reboot or power off will not invoke qcom_pmic_typec_shutdown() on the unbound device.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic-typec: detach the port on shutdown

Quoted 55 lines · On 11/20/26 11:15, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] usb: typec: qcom: NULL deref in qcom_pmic_typec_shutdown() when VBUS is on
- [Medium] usb: typec: qcom-pmic-typec: missing port_disable() in remove and probe unwind
--

commit 27e120f037d3faf6c51dac7774569601de458e75
Author: Neil Armstrong <[email protected]>

usb: typec: qcom-pmic-typec: detach the port on shutdown

This commit adds a platform driver shutdown callback that stops the port and PD PHY, unregisters the TCPM port, and disables the Type-C block so that a connected partner sees a detach on reboot or power off.

Quoted 5 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +0 -0
index 58d1e6aae637d..8169db478b8aa 100644

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c static void qcom_pmic_typec_remove(struct platform_device *pdev) · line 143

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +7 -0
@@ -134,6 +134,16 @@ static void qcom_pmic_typec_remove(struct platform_device *pdev)
  	fwnode_handle_put(tcpm->tcpc.fwnode);
  }
 
+static void qcom_pmic_typec_shutdown(struct platform_device *pdev)
+{
+	struct pmic_typec *tcpm = platform_get_drvdata(pdev);
+
+	tcpm->pdphy_stop(tcpm);
+	tcpm->port_stop(tcpm);
+	tcpm_unregister_port(tcpm->tcpm_port);

[Severity: High]
Can calling tcpm_unregister_port() here cause a NULL pointer dereference on port->wq when shutting down with an attached sink partner powered by VBUS?

tcpm_unregister_port() destroys port->wq and sets it to NULL before calling tcpm_reset_port() -> tcpm_init_vbus() -> qcom_pmic_typec_port_set_vbus(). When VBUS transitions from enabled to disabled, qcom_pmic_typec_port_set_vbus() calls tcpm_vbus_change(), which queues work onto port->wq:

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c:qcom_pmic_typec_port_set_vbus() {
	...
	ret = qcom_pmic_typec_port_vbus_toggle(pmic_typec_port, on);
	if (ret)
		goto done;

	pmic_typec_port->vbus_enabled = on;
	tcpm_vbus_change(tcpm->tcpm_port);
	...
}
Quoted 2 lines

kthread_queue_work() then dereferences NULL in &worker->lock, crashing before tcpm->port_disable(tcpm) is reached.

What's the difference with qcom_pmic_typec_remove() ? So the same regression occurs with qcom_pmic_typec_remove() and is not related with this change.

I'll try to fix it

Quoted 8 lines

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c static void qcom_pmic_typec_remove(struct platform_device *pdev) · line 145

Quoted 3 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec.c +2 -0
+	tcpm->port_disable(tcpm);
+}

[Severity: Medium]
Should tcpm->port_disable(tcpm) also be called in qcom_pmic_typec_remove() and the port_unregister error path in qcom_pmic_typec_probe()?

Quoted 5 lines

Without it, unbinding the driver or failing probe after tcpm_register_port() leaves TYPEC_MODE_CFG_REG presenting Rp or Rd with no TCPM or PD PHY servicing the port, and a subsequent reboot or power off will not invoke qcom_pmic_typec_shutdown() on the unbound device.

Ok so it should be extended by:
- check if not in TYPEC_DISABLE_CMD on probe, and force a detach is enabled before
- disable on remove and probe fail to have a coherent state

maintainer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] usb: typec: qcom-pmic-typec: detach the port on shutdown

In reply to Neil Armstrong
Quoted 10 lines · Neil Armstrong

On reboot or power off the port is left as is: a source partner keeps the negotiated contract, possibly above 5V, and a sink partner stays powered by the OTG boost while nothing services the port anymore.

Add a shutdown callback stopping the port and the PD PHY, unregistering the TCPM port, which turns VBUS and VCONN off, and finally disabling the Type-C block so the partner sees a detach.

Assisted-by LLM

LGTM