mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Badhri Jagan Sridharan <badhri@google.com>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	 kernel@collabora.com,
	Sebastian Reichel <sebastian.reichel@collabora.com>
Subject: [PATCH 3/8] usb: typec: fusb302: Switch to device managed resources
Date: Fri, 13 Nov 2026 17:42:39 +0100	[thread overview]
Message-ID: <20261113-fusb302-probe-cleanup-v1-3-e5c65a8c58c8@collabora.com> (raw)
In-Reply-To: <20261113-fusb302-probe-cleanup-v1-0-e5c65a8c58c8@collabora.com>

The driver's probe routine is currently mixing registration calls for
device managed resources with unmananged ones. This results in issues
such as resource leaks or use-after-free. Fix this up by fully
converting the driver to device managed resources.

Resource acquisition has been reordered a bit, so that the work is
initialized after the TCPM port is registered as the work uses the
TCPM devices.

Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
---
 drivers/usb/typec/tcpm/fusb302.c | 112 ++++++++++++++++++++++-----------------
 1 file changed, 62 insertions(+), 50 deletions(-)

diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
index 83bd287dadc7..a7a1dce6cf86 100644
--- a/drivers/usb/typec/tcpm/fusb302.c
+++ b/drivers/usb/typec/tcpm/fusb302.c
@@ -8,6 +8,7 @@
 #include <drm/bridge/aux-bridge.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/errno.h>
 #include <linux/extcon.h>
 #include <linux/gpio/consumer.h>
@@ -210,20 +211,28 @@ static int fusb302_debug_show(struct seq_file *s, void *v)
 }
 DEFINE_SHOW_ATTRIBUTE(fusb302_debug);
 
-static void fusb302_debugfs_init(struct fusb302_chip *chip)
+static void fusb302_debugfs_exit(void *data)
+{
+	struct fusb302_chip *chip = data;
+
+	debugfs_remove(chip->dentry);
+}
+
+static int fusb302_debugfs_init(struct fusb302_chip *chip)
 {
 	char name[NAME_MAX];
+	int ret;
+
+	ret = devm_mutex_init(chip->dev, &chip->logbuffer_lock);
+	if (ret < 0)
+		return ret;
 
-	mutex_init(&chip->logbuffer_lock);
 	snprintf(name, NAME_MAX, "fusb302-%s", dev_name(chip->dev));
 	chip->dentry = debugfs_create_dir(name, usb_debug_root);
 	debugfs_create_file("log", S_IFREG | 0444, chip->dentry, chip,
 			    &fusb302_debug_fops);
-}
 
-static void fusb302_debugfs_exit(struct fusb302_chip *chip)
-{
-	debugfs_remove(chip->dentry);
+	return devm_add_action_or_reset(chip->dev, fusb302_debugfs_exit, chip);
 }
 
 #else
@@ -231,7 +240,6 @@ static void fusb302_debugfs_exit(struct fusb302_chip *chip)
 static void fusb302_log(const struct fusb302_chip *chip,
 			const char *fmt, ...) { }
 static void fusb302_debugfs_init(const struct fusb302_chip *chip) { }
-static void fusb302_debugfs_exit(const struct fusb302_chip *chip) { }
 
 #endif
 
@@ -1686,6 +1694,13 @@ static struct fwnode_handle *fusb302_fwnode_get(struct device *dev)
 	return fwnode;
 }
 
+static void fusb302_fwnode_put(void *data)
+{
+	struct fusb302_chip *chip = data;
+
+	fwnode_handle_put(chip->tcpc_dev.fwnode);
+}
+
 static int fusb302_probe(struct i2c_client *client)
 {
 	struct fusb302_chip *chip;
@@ -1706,7 +1721,10 @@ static int fusb302_probe(struct i2c_client *client)
 
 	chip->i2c_client = client;
 	chip->dev = &client->dev;
-	mutex_init(&chip->lock);
+
+	ret = devm_mutex_init(dev, &chip->lock);
+	if (ret < 0)
+		return ret;
 
 	/*
 	 * Devicetree platforms should get extcon via phandle (not yet
@@ -1725,67 +1743,68 @@ static int fusb302_probe(struct i2c_client *client)
 	if (IS_ERR(chip->vbus))
 		return PTR_ERR(chip->vbus);
 
-	chip->wq = create_singlethread_workqueue(dev_name(chip->dev));
+	chip->wq = devm_alloc_ordered_workqueue(dev, dev_name(dev), 0);
 	if (!chip->wq)
 		return -ENOMEM;
 
 	spin_lock_init(&chip->irq_lock);
-	INIT_WORK(&chip->irq_work, fusb302_irq_work);
-	INIT_DELAYED_WORK(&chip->bc_lvl_handler, fusb302_bc_lvl_handler_work);
 	init_tcpc_dev(&chip->tcpc_dev);
-	fusb302_debugfs_init(chip);
+
+	ret = fusb302_debugfs_init(chip);
+	if (ret < 0)
+		return ret;
 
 	if (client->irq) {
 		chip->gpio_int_n_irq = client->irq;
 	} else {
 		ret = init_gpio(chip);
 		if (ret < 0)
-			goto destroy_workqueue;
+			return ret;
 	}
 
 	chip->tcpc_dev.fwnode = fusb302_fwnode_get(dev);
 	if (IS_ERR(chip->tcpc_dev.fwnode)) {
 		ret = PTR_ERR(chip->tcpc_dev.fwnode);
-		goto destroy_workqueue;
+		return ret;
 	}
 
+	ret = devm_add_action_or_reset(dev, fusb302_fwnode_put, chip);
+	if (ret < 0)
+		return ret;
+
 	bridge_dev = devm_drm_dp_hpd_bridge_alloc(chip->dev, to_of_node(chip->tcpc_dev.fwnode));
-	if (IS_ERR(bridge_dev)) {
-		ret = dev_err_probe(chip->dev, PTR_ERR(bridge_dev),
-				    "failed to alloc bridge\n");
-		goto fwnode_put;
-	}
+	if (IS_ERR(bridge_dev))
+		return dev_err_probe(chip->dev, PTR_ERR(bridge_dev),
+				     "failed to alloc bridge\n");
 
-	chip->tcpm_port = tcpm_register_port(&client->dev, &chip->tcpc_dev);
-	if (IS_ERR(chip->tcpm_port)) {
-		ret = dev_err_probe(dev, PTR_ERR(chip->tcpm_port),
+	chip->tcpm_port = devm_tcpm_register_port(&client->dev, &chip->tcpc_dev);
+	if (IS_ERR(chip->tcpm_port))
+		return dev_err_probe(dev, PTR_ERR(chip->tcpm_port),
 				    "cannot register tcpm port\n");
-		goto fwnode_put;
-	}
 
-	ret = devm_drm_dp_hpd_bridge_add(chip->dev, bridge_dev);
-	if (ret)
-		goto tcpm_unregister_port;
+	ret = devm_delayed_work_autocancel(dev, &chip->bc_lvl_handler,
+					   fusb302_bc_lvl_handler_work);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_work_autocancel(dev, &chip->irq_work, fusb302_irq_work);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_threaded_irq(dev, chip->gpio_int_n_irq, NULL,
+					fusb302_irq_intn,
+					IRQF_ONESHOT | IRQF_TRIGGER_LOW,
+					"fsc_interrupt_int_n", chip);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "failed to request IRQ");
 
-	ret = request_threaded_irq(chip->gpio_int_n_irq, NULL, fusb302_irq_intn,
-				   IRQF_ONESHOT | IRQF_TRIGGER_LOW,
-				   "fsc_interrupt_int_n", chip);
-	if (ret < 0) {
-		dev_err(dev, "cannot request IRQ for GPIO Int_N, ret=%d", ret);
-		goto tcpm_unregister_port;
-	}
-	enable_irq_wake(chip->gpio_int_n_irq);
 	i2c_set_clientdata(client, chip);
 
-	return 0;
+	ret = devm_drm_dp_hpd_bridge_add(chip->dev, bridge_dev);
+	if (ret)
+		return ret;
 
-tcpm_unregister_port:
-	tcpm_unregister_port(chip->tcpm_port);
-fwnode_put:
-	fwnode_handle_put(chip->tcpc_dev.fwnode);
-destroy_workqueue:
-	fusb302_debugfs_exit(chip);
-	destroy_workqueue(chip->wq);
+	enable_irq_wake(chip->gpio_int_n_irq);
 
 	return ret;
 }
@@ -1795,13 +1814,6 @@ static void fusb302_remove(struct i2c_client *client)
 	struct fusb302_chip *chip = i2c_get_clientdata(client);
 
 	disable_irq_wake(chip->gpio_int_n_irq);
-	free_irq(chip->gpio_int_n_irq, chip);
-	cancel_work_sync(&chip->irq_work);
-	cancel_delayed_work_sync(&chip->bc_lvl_handler);
-	tcpm_unregister_port(chip->tcpm_port);
-	fwnode_handle_put(chip->tcpc_dev.fwnode);
-	destroy_workqueue(chip->wq);
-	fusb302_debugfs_exit(chip);
 }
 
 static int fusb302_pm_suspend(struct device *dev)

-- 
2.53.0


  parent reply	other threads:[~2026-11-13 16:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 16:42 [PATCH 0/8] usb: typec: fusb302: cleanup device driver probe Sebastian Reichel
2026-11-13 16:42 ` [PATCH 1/8] usb: typec: tcpm: allocate pd_list entries non device managed Sebastian Reichel
2026-11-13 16:42 ` [PATCH 2/8] usb: typec: tcpm: add device managed port registration Sebastian Reichel
2026-11-13 16:42 ` Sebastian Reichel [this message]
2026-11-13 16:42 ` [PATCH 4/8] usb: typec: fusb302: switch to devm_pm_set_wake_irq Sebastian Reichel
2026-11-13 16:42 ` [PATCH 5/8] usb: typec: fusb302: rename init_gpio into fusb302_init_irq Sebastian Reichel
2026-11-13 16:42 ` [PATCH 6/8] usb: typec: fusb302: move gpio_int_n into function local scope Sebastian Reichel
2026-11-13 16:42 ` [PATCH 7/8] usb: typec: fusb302: rework gpio_int_n_irq handling Sebastian Reichel
2026-11-13 16:42 ` [PATCH 8/8] usb: typec: fusb302: drop custom gpio interrupt logic Sebastian Reichel
2026-11-16  8:32 ` [PATCH 0/8] usb: typec: fusb302: cleanup device driver probe Chaoyi Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113-fusb302-probe-cleanup-v1-3-e5c65a8c58c8@collabora.com \
    --to=sebastian.reichel@collabora.com \
    --cc=badhri@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=kernel@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®