Skip to contentSkip to conversation

phy: phy-can-transceiver: Ad-hoc cleanups and refactoring

PATCH Superseded Andy Shevchenko 2 participants 11 messages May 12 – May 13

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#9 lore raw

PATCH v4 5/5 phy: phy-can-transceiver: Drop unused include

In reply to Andy Shevchenko

This file does not use the symbols from the legacy <linux/gpio.h> header, so let's drop it.

---
 drivers/phy/phy-can-transceiver.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/phy/phy-can-transceiver.c +1 -2
index 973cc7ce651f..534a4fc482aa 100644
@@ -5,12 +5,11 @@
  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
  *
  */
+#include <linux/gpio/consumer.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/module.h>
-#include <linux/gpio.h>
-#include <linux/gpio/consumer.h>
 #include <linux/mux/consumer.h>
 
 struct can_transceiver_data {
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/5 phy: phy-can-transceiver: Check driver match and driver data against NULL

In reply to Andy Shevchenko

Every platform driver can be forced to match a device that doesn't match its list of device IDs because of device_match_driver_override() so platform drivers that rely on the existence of a device's driver data need to verify its presence.

Accordingly, add requisite match and driver data checks against NULL to the driver where they are missing.

Fixes a4a86d273ff1 ("phy: phy-can-transceiver: Add support for generic CAN transceiver driver")
---
 drivers/phy/phy-can-transceiver.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/phy/phy-can-transceiver.c +3 -0
index 2b52e47f247a..1808f903c057 100644
@@ -162,6 +162,9 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	int err, i, num_ch = 1;
 
 	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
+	if (!match || !match->data)
+		return -ENODEV;
+
 	drvdata = match->data;
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
 		num_ch = 2;
Signature · 1 line
2.50.1
#0 lore raw

v4 cover phy: phy-can-transceiver: Ad-hoc cleanups and refactoring

The driver does two things that need to be addressed:
- includes subject to remove gpio.h
- checks for error code from device property APIs when it can be done in a robust way

This series addresses the above and adds a couple of additional refactoring.

On top of that it fixes potential NULL pointer dereference when driver_override is in use.

Changelog v4:
- prepended the series with match and driver data check (Sashiko, Vinod)
- made the max_bitrate==0 warning only when property is present (Sashiko, Vinod)
- rebased the rest accordingly

v3: [email protected]

Changelog v3:
- fixed commit message in patch 1 (Josua)
- dropped stray change (Vinod)
- collected tags (Josua)

v2: [email protected]

Changelog v2:
- rebased on top of the latest changes in the driver
- Cc'ed to Ulf and Josua due to above
- elaborated dropping of_node parameter (Vladimir)

v1: [email protected]

Andy Shevchenko (5):
phy: phy-can-transceiver: Check driver match and driver data against
NULL
phy: phy-can-transceiver: use device_get_match_data()
phy: phy-can-transceiver: Move OF ID table closer to their user
phy: phy-can-transceiver: Don't check for specific errors when parsing
properties
phy: phy-can-transceiver: Drop unused include

 drivers/phy/phy-can-transceiver.c | 89 +++++++++++++++++--------------
 1 file changed, 49 insertions(+), 40 deletions(-)
Signature · 1 line
2.50.1
#5 lore raw

PATCH v4 3/5 phy: phy-can-transceiver: Move OF ID table closer to their user

There is no code that uses ID table directly, except the struct device_driver at the end of the file. Hence, move table closer to its user. It's always possible to access them via a pointer.

---
 drivers/phy/phy-can-transceiver.c | 58 +++++++++++++++----------------
 1 file changed, 29 insertions(+), 29 deletions(-)
drivers/phy/phy-can-transceiver.c +29 -29
index 5cfd09735204..23655956ace8 100644
@@ -97,35 +97,6 @@ static const struct can_transceiver_data tja1057_drvdata = {
 	.flags = CAN_TRANSCEIVER_SILENT_PRESENT,
 };
 
-static const struct of_device_id can_transceiver_phy_ids[] = {
-	{
-		.compatible = "ti,tcan1042",
-		.data = &tcan1042_drvdata
-	},
-	{
-		.compatible = "ti,tcan1043",
-		.data = &tcan1043_drvdata
-	},
-	{
-		.compatible = "nxp,tja1048",
-		.data = &tja1048_drvdata
-	},
-	{
-		.compatible = "nxp,tja1051",
-		.data = &tja1051_drvdata
-	},
-	{
-		.compatible = "nxp,tja1057",
-		.data = &tja1057_drvdata
-	},
-	{
-		.compatible = "nxp,tjr1443",
-		.data = &tcan1043_drvdata
-	},
-	{ }
-};
-MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
-
 static struct phy *can_transceiver_phy_xlate(struct device *dev,
 					     const struct of_phandle_args *args)
 {
@@ -232,6 +203,35 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	return PTR_ERR_OR_ZERO(phy_provider);
 }
 
+static const struct of_device_id can_transceiver_phy_ids[] = {
+	{
+		.compatible = "ti,tcan1042",
+		.data = &tcan1042_drvdata
+	},
+	{
+		.compatible = "ti,tcan1043",
+		.data = &tcan1043_drvdata
+	},
+	{
+		.compatible = "nxp,tja1048",
+		.data = &tja1048_drvdata
+	},
+	{
+		.compatible = "nxp,tja1051",
+		.data = &tja1051_drvdata
+	},
+	{
+		.compatible = "nxp,tja1057",
+		.data = &tja1057_drvdata
+	},
+	{
+		.compatible = "nxp,tjr1443",
+		.data = &tcan1043_drvdata
+	},
+	{ }
+};
+MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
+
 static struct platform_driver can_transceiver_phy_driver = {
 	.probe = can_transceiver_phy_probe,
 	.driver = {
Signature · 1 line
2.50.1
#2 lore raw

PATCH v4 2/5 phy: phy-can-transceiver: use device_get_match_data()

In reply to Andy Shevchenko

Use the generic firmware node interface for retrieving device match data instead of the OF-specific one.

While at it, drop unneeded argument to devm_phy_create() which extracts device node from the given device by default.

---
 drivers/phy/phy-can-transceiver.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/phy/phy-can-transceiver.c +4 -6
index 1808f903c057..5cfd09735204 100644
@@ -5,9 +5,9 @@
  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
  *
  */
-#include <linux/of.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/module.h>
 #include <linux/gpio.h>
 #include <linux/gpio/consumer.h>
@@ -152,7 +152,6 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct can_transceiver_phy *can_transceiver_phy;
 	struct can_transceiver_priv *priv;
 	const struct can_transceiver_data *drvdata;
-	const struct of_device_id *match;
 	struct phy *phy;
 	struct gpio_desc *silent_gpio;
 	struct gpio_desc *standby_gpio;
@@ -161,11 +160,10 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
 
-	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
-	if (!match || !match->data)
+	drvdata = device_get_match_data(dev);
+	if (drvdata)
 		return -ENODEV;
 
-	drvdata = match->data;
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
 		num_ch = 2;
 
@@ -190,7 +188,7 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 		can_transceiver_phy = &priv->can_transceiver_phy[i];
 		can_transceiver_phy->priv = priv;
 
-		phy = devm_phy_create(dev, dev->of_node, &can_transceiver_phy_ops);
+		phy = devm_phy_create(dev, NULL, &can_transceiver_phy_ops);
 		if (IS_ERR(phy)) {
 			dev_err(dev, "failed to create can transceiver phy\n");
 			return PTR_ERR(phy);
Signature · 1 line
2.50.1
#7 lore raw

PATCH v4 4/5 phy: phy-can-transceiver: Don't check for specific errors when parsing properties

In reply to Andy Shevchenko

Instead of checking for the specific error codes (that can be considered a layering violation to some extent) check for the property existence first and then either parse it, or apply a default value.

With that, return an error when parsing of the existing property fails.

---
 drivers/phy/phy-can-transceiver.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/phy/phy-can-transceiver.c +13 -4
index 23655956ace8..973cc7ce651f 100644
@@ -128,8 +128,9 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct gpio_desc *standby_gpio;
 	struct gpio_desc *enable_gpio;
 	struct mux_state *mux_state;
-	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
+	const char *propname;
+	u32 max_bitrate;
 
 	drvdata = device_get_match_data(dev);
 	if (drvdata)
@@ -151,9 +152,17 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 
 	priv->mux_state = mux_state;
 
-	err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
-	if ((err != -EINVAL) && !max_bitrate)
-		dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
+	propname = "max-bitrate";
+	if (device_property_present(dev, propname)) {
+		err = device_property_read_u32(dev, propname, &max_bitrate);
+		if (err)
+			return dev_err_probe(dev, err, "failed to parse %s\n", propname);
+
+		if (max_bitrate == 0)
+			dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
+	} else {
+		max_bitrate = 0;
+	}
 
 	for (i = 0; i < num_ch; i++) {
 		can_transceiver_phy = &priv->can_transceiver_phy[i];
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH v4 2/5] phy: phy-can-transceiver: use device_get_match_data()

In reply to Andy Shevchenko
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The error check for `device_get_match_data(dev)` is inverted, causing the driver probe to fail on…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The error check for device_get_match_data(dev) is inverted, causing the driver probe to fail on success and triggering a NULL pointer dereference on failure. --

commit f9915d9d74989682b2b7e20d67129e0dd4abdab9
Author: Andy Shevchenko <[email protected]>

phy: phy-can-transceiver: use device_get_match_data()

This commit transitions the driver to use the generic firmware node interface for retrieving device match data instead of the OF-specific one. It also drops the unneeded device node argument from devm_phy_create().

Quoted 4 lines
drivers/phy/phy-can-transceiver.c +0 -0
index 1808f903c0570..5cfd097352048 100644

[ ... ]

Quoted 9 lines
drivers/phy/phy-can-transceiver.c +0 -1
@@ -152,7 +152,6 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct can_transceiver_phy *can_transceiver_phy;
 	struct can_transceiver_priv *priv;
 	const struct can_transceiver_data *drvdata;
-	const struct of_device_id *match;
 	struct phy *phy;
 	struct gpio_desc *silent_gpio;
 	struct gpio_desc *standby_gpio;

drivers/phy/phy-can-transceiver.c static int can_transceiver_phy_probe(struct platform_device *pdev) · line 165

Quoted 8 lines
drivers/phy/phy-can-transceiver.c +2 -2
@@ -161,11 +160,10 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
 
-	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
-	if (!match || !match->data)
+	drvdata = device_get_match_data(dev);
+	if (drvdata)
 		return -ENODEV;

Does this invert the error check?

When device_get_match_data() successfully returns valid match data, it looks like this condition will evaluate to true and erroneously abort the probe with -ENODEV.

drivers/phy/phy-can-transceiver.c static int can_transceiver_phy_probe(struct platform_device *pdev) · line 168

Quoted 4 lines
drivers/phy/phy-can-transceiver.c +0 -1
-	drvdata = match->data;
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
 		num_ch = 2;

Conversely, if device_get_match_data() fails and returns NULL, won't the probe continue and cause a NULL pointer dereference when evaluating drvdata->flags here?

[ ... ]

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

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
#6 lore raw

Re: [PATCH v4 3/5] phy: phy-can-transceiver: Move OF ID table closer to their user

In reply to Andy Shevchenko
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The check `if (drvdata)` incorrectly returns `-ENODEV` when match data is found, and leads to…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The check if (drvdata) incorrectly returns -ENODEV when match data is found, and leads to a NULL pointer dereference when match data is missing. --

commit e6497a416a7e21916ac93bad3296c090e87a31d4
Author: Andy Shevchenko <[email protected]>

phy: phy-can-transceiver: Move OF ID table closer to their user

This commit moves the can_transceiver_phy_ids table closer to its user at the end of the file.

drivers/phy/phy-can-transceiver.c

Quoted 4 lines
drivers/phy/phy-can-transceiver.c +0 -0
index 5cfd097352048..23655956ace80 100644

[ ... ]

This regression wasn't introduced by this patch, but by a previous commit in the series ("phy: phy-can-transceiver: use device_get_match_data()").

Looking at can_transceiver_phy_probe(), does this code correctly handle the match data?

drivers/phy/phy-can-transceiver.c:can_transceiver_phy_probe() {
    ...
	drvdata = device_get_match_data(dev);
	if (drvdata)
		return -ENODEV;

	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
    ...
}

It appears that if match data is successfully found, the probe will return -ENODEV.

Conversely, if no match data is found, the condition evaluates to false, and execution proceeds to dereference the NULL drvdata pointer.

Should the condition be changed to check for !drvdata instead?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
#8 lore raw

Re: [PATCH v4 4/5] phy: phy-can-transceiver: Don't check for specific errors when parsing properties

In reply to Andy Shevchenko
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The driver unconditionally fails or crashes due to incorrect checking of `drvdata`. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The driver unconditionally fails or crashes due to incorrect checking of drvdata. --

commit 64758b3de7d3ae700ae3ccdaef989d7410af4921
Author: Andy Shevchenko <[email protected]>

phy: phy-can-transceiver: Don't check for specific errors when parsing properties

This commit modifies the max-bitrate property parsing to check for the property existence first before attempting to read it. It applies a default value if the property is missing, rather than relying on specific error codes.

Quoted 5 lines
drivers/phy/phy-can-transceiver.c +0 -0
index 23655956ace80..973cc7ce651fb 100644

drivers/phy/phy-can-transceiver.c static int can_transceiver_phy_probe(struct platform_device *pdev) · line 136

Quoted 10 lines
drivers/phy/phy-can-transceiver.c +2 -1
@@ -128,8 +128,9 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	struct gpio_desc *standby_gpio;
 	struct gpio_desc *enable_gpio;
 	struct mux_state *mux_state;
-	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
+	const char *propname;
+	u32 max_bitrate;
 
 	drvdata = device_get_match_data(dev);
 	if (drvdata)

I noticed a potential issue in the context of this diff. Is the check for drvdata inverted here? This logic appears to have been introduced by a previous patch in the series.

If device_get_match_data() successfully finds match data, the probe function aborts and returns -ENODEV. If no match data is found, drvdata is NULL and the function proceeds, which leads to a dereference on the next check:

	drvdata = device_get_match_data(dev);
	if (drvdata)
		return -ENODEV;

	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
		num_ch = 2;

Could this cause the driver to fail on valid probes, or dereference a NULL pointer when match data is missing?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
#10 lore raw

Re: [PATCH v4 5/5] phy: phy-can-transceiver: Drop unused include

In reply to Andy Shevchenko
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The probe function incorrectly aborts when match data is found and dereferences a NULL pointer…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The probe function incorrectly aborts when match data is found and dereferences a NULL pointer when match data is absent. --

commit 8e35724d73ac65aed30d62ac9342ff1ac6be9401
Author: Andy Shevchenko <[email protected]>

phy: phy-can-transceiver: Drop unused include

This commit removes the unused <linux/gpio.h> legacy header from the PHY CAN transceiver driver, as its symbols are not used in the file. It also adjusts the header order slightly.

Quoted 8 lines
drivers/phy/phy-can-transceiver.c +0 -0
index 973cc7ce651fb..534a4fc482aa8 100644
@@ -5,12 +5,11 @@
  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
  *
  */

drivers/phy/phy-can-transceiver.c line 15

Quoted 10 lines
drivers/phy/phy-can-transceiver.c +1 -2
+#include <linux/gpio/consumer.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/module.h>
-#include <linux/gpio.h>
-#include <linux/gpio/consumer.h>
 #include <linux/mux/consumer.h>
 
 struct can_transceiver_data {

While reviewing this patch, I noticed an issue introduced earlier in the series by commit "phy: phy-can-transceiver: use device_get_match_data()":

Quoted 2 lines
drivers/phy/phy-can-transceiver.c +0 -0

[ ... ]

drivers/phy/phy-can-transceiver.c static int can_transceiver_phy_probe(struct platform_device *pdev) · line 168

Quoted 13 lines
drivers/phy/phy-can-transceiver.c +2 -3
@@ -161,11 +160,10 @@ static int can_transceiver_phy_probe(struct platform_device *pdev)
 	u32 max_bitrate = 0;
 	int err, i, num_ch = 1;
 
-	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
-	if (!match || !match->data)
+	drvdata = device_get_match_data(dev);
+	if (drvdata)
 		return -ENODEV;
 
-	drvdata = match->data;
 	if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
 		num_ch = 2;

Does this incorrectly abort the probe when match data is successfully found? It looks like the condition is inverted, returning -ENODEV upon success.

Additionally, if match data is absent, drvdata will be NULL and execution continues to the next line where it is dereferenced. Can this cause a NULL pointer dereference?

Should the check be corrected to if (!drvdata)?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5

-- 
linux-phy mailing list
[email protected]
https://lists.infradead.org/mailman/listinfo/linux-phy
#4 lore raw

Re: [PATCH v4 2/5] phy: phy-can-transceiver: use device_get_match_data()

In reply to [email protected]
Quoted 2 lines · On Wed, May 13, 2026 at 09:26:11PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The error check for device_get_match_data(dev) is inverted, causing the driver probe to fail on success and triggering a NULL pointer dereference on failure.

Right, the typo due to rebase. Will fix in v5.

Signature · 2 lines
With Best Regards,
Andy Shevchenko