From feb4b919abf39f39faf660ef3d9aedebb54f5db5 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Thu, 31 Aug 2023 22:16:33 +0000 Subject: phy: Set phy->dev to NULL when generic_phy_get_by_name() fails generic_phy_get_by_name() does not initialize phy->dev to NULL before returning when dev_read_stringlist_search() fails. This can lead to an uninitialized or reused struct phy erroneously be report as valid by generic_phy_valid(). Fix this issue by initializing phy->dev to NULL, also extend the dm_test_phy_base test with calls to generic_phy_valid(). Fixes: b9688df3cbf4 ("drivers: phy: Set phy->dev to NULL when generic_phy_get_by_index() fails") Fixes: 868d58f69c7c ("usb: dwc3: Fix non-usb3 configurations") Signed-off-by: Jonas Karlman --- drivers/phy/phy-uclass.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index 629ef3aa3de..0baf314a347 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -211,6 +211,9 @@ int generic_phy_get_by_name(struct udevice *dev, const char *phy_name, debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy); + assert(phy); + phy->dev = NULL; + index = dev_read_stringlist_search(dev, "phy-names", phy_name); if (index < 0) { debug("dev_read_stringlist_search() failed: %d\n", index); -- cgit v1.2.3 From 14639bf14d824d9fbcfd918f0e7924c7f7065422 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Thu, 31 Aug 2023 22:16:35 +0000 Subject: phy: Set phy->dev to NULL when generic_phy_get_by_index_nodev() fails Generic phy helpers typically use generic_phy_valid() to determine if the helper should perform its function on a passed struct phy. generic_phy_valid() treat any struct phy having phy->dev set as valid. With generic_phy_get_by_index_nodev() setting phy->dev to a valid struct udevice early, there can be situations where the struct phy is returned as valid when initialization in fact failed and returned an error. Fix this by setting phy->dev back to NULL when any of the calls to of_xlate ops, device_get_supply_regulator or phy_alloc_counts fail. Also extend the dm_test_phy_base test with a test where of_xlate ops fail. Fixes: 72e5016f878d ("drivers: phy: add generic PHY framework") Fixes: b9688df3cbf4 ("drivers: phy: Set phy->dev to NULL when generic_phy_get_by_index() fails") Signed-off-by: Jonas Karlman --- drivers/phy/phy-uclass.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index 0baf314a347..7d707c02293 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -195,6 +195,7 @@ int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy) return 0; err: + phy->dev = NULL; return ret; } -- cgit v1.2.3 From bd78f2c2c28ca93311a7e1cdddefbd08a7cf9f70 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Thu, 31 Aug 2023 23:07:08 +0000 Subject: phy: Fix generic_setup_phy() return value on power on failure generic_phy_exit() typically return 0 for a struct phy that has been initialized with a generic_phy_init() call. generic_setup_phy() returns the value from a generic_phy_exit() call when generic_phy_power_on() fails. This hides the failed state of the power_on ops from the caller of generic_setup_phy(). Fix this by ignoring the return value of the generic_phy_exit() call and return the value from the generic_phy_power_on() call. Fixes: 84e561407a5f ("phy: Add generic_{setup,shutdown}_phy() helpers") Signed-off-by: Jonas Karlman --- drivers/phy/phy-uclass.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index 7d707c02293..d745e7babc1 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -526,7 +526,7 @@ int generic_setup_phy(struct udevice *dev, struct phy *phy, int index) ret = generic_phy_power_on(phy); if (ret) - ret = generic_phy_exit(phy); + generic_phy_exit(phy); } return ret; -- cgit v1.2.3 From 1a4293e001d1063401b6f94104684e38d6e401c9 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Thu, 31 Aug 2023 23:07:10 +0000 Subject: phy: Return success from generic_setup_phy() when phy is not found Restore the old behavior of ehci_setup_phy() and ohci_setup_phy() to return success when generic_phy_get_by_index() return -ENOENT. Fixes: 84e561407a5f ("phy: Add generic_{setup,shutdown}_phy() helpers") Fixes: 10005004db73 ("usb: ohci: Make usage of generic_{setup,shutdown}_phy() helpers") Fixes: 083f8aa978a8 ("usb: ehci: Make usage of generic_{setup,shutdown}_phy() helpers") Fixes: 75341e9c16aa ("usb: ehci: Remove unused ehci_{setup,shutdown}_phy() helpers") Signed-off-by: Jonas Karlman --- drivers/phy/phy-uclass.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index d745e7babc1..343c23cead0 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -517,8 +517,8 @@ int generic_setup_phy(struct udevice *dev, struct phy *phy, int index) ret = generic_phy_get_by_index(dev, index, phy); if (ret) { - if (ret != -ENOENT) - return ret; + if (ret == -ENOENT) + return 0; } else { ret = generic_phy_init(phy); if (ret) -- cgit v1.2.3 From 565f556d0ba7f36def70dc36422d8730ff70f7a9 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Thu, 31 Aug 2023 23:07:11 +0000 Subject: phy: Refactor generic_{setup, shutdown}_phy() to reduce complexity Refactor generic_{setup,shutdown}_phy() to reduce complexity and indentation. This have no intended functional change. Signed-off-by: Jonas Karlman --- drivers/phy/phy-uclass.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index 343c23cead0..190108e04c7 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -510,44 +510,35 @@ int generic_phy_power_off_bulk(struct phy_bulk *bulk) int generic_setup_phy(struct udevice *dev, struct phy *phy, int index) { - int ret = 0; - - if (!phy) - return 0; + int ret; ret = generic_phy_get_by_index(dev, index, phy); - if (ret) { - if (ret == -ENOENT) - return 0; - } else { - ret = generic_phy_init(phy); - if (ret) - return ret; + if (ret) + return ret == -ENOENT ? 0 : ret; - ret = generic_phy_power_on(phy); - if (ret) - generic_phy_exit(phy); - } + ret = generic_phy_init(phy); + if (ret) + return ret; + + ret = generic_phy_power_on(phy); + if (ret) + generic_phy_exit(phy); return ret; } int generic_shutdown_phy(struct phy *phy) { - int ret = 0; + int ret; - if (!phy) + if (!generic_phy_valid(phy)) return 0; - if (generic_phy_valid(phy)) { - ret = generic_phy_power_off(phy); - if (ret) - return ret; - - ret = generic_phy_exit(phy); - } + ret = generic_phy_power_off(phy); + if (ret) + return ret; - return ret; + return generic_phy_exit(phy); } UCLASS_DRIVER(phy) = { -- cgit v1.2.3 From 1e94b46f73cedcebbff73799203f3266c5b28d90 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 14 Sep 2023 18:21:46 -0600 Subject: common: Drop linux/printk.h from common header This old patch was marked as deferred. Bring it back to life, to continue towards the removal of common.h Move this out of the common header and include it only where needed. Signed-off-by: Simon Glass --- drivers/phy/phy-uclass.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/phy/phy-uclass.c') diff --git a/drivers/phy/phy-uclass.c b/drivers/phy/phy-uclass.c index 190108e04c7..22f2fe91487 100644 --- a/drivers/phy/phy-uclass.c +++ b/drivers/phy/phy-uclass.c @@ -12,6 +12,7 @@ #include #include #include +#include #include /** -- cgit v1.2.3