drivers: phy: Handle gracefully NULL pointers
authorJean-Jacques Hiblot <jjhiblot@ti.com>
Tue, 1 Oct 2019 12:03:26 +0000 (14:03 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 31 Oct 2019 11:22:53 +0000 (07:22 -0400)
For some controllers PHYs can be optional. Handling NULL pointers without
crashing nor failing, makes it easy to handle optional PHYs.

Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
drivers/phy/phy-uclass.c
include/generic-phy.h

index a0ac30aa7138aa25536c4c9cdb719a839594c3ac..e201a90c8c8df3d5ac604bc62f8a4dba30de5a2c 100644 (file)
@@ -108,35 +108,55 @@ int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
 
 int generic_phy_init(struct phy *phy)
 {
-       struct phy_ops const *ops = phy_dev_ops(phy->dev);
+       struct phy_ops const *ops;
+
+       if (!phy)
+               return 0;
+       ops = phy_dev_ops(phy->dev);
 
        return ops->init ? ops->init(phy) : 0;
 }
 
 int generic_phy_reset(struct phy *phy)
 {
-       struct phy_ops const *ops = phy_dev_ops(phy->dev);
+       struct phy_ops const *ops;
+
+       if (!phy)
+               return 0;
+       ops = phy_dev_ops(phy->dev);
 
        return ops->reset ? ops->reset(phy) : 0;
 }
 
 int generic_phy_exit(struct phy *phy)
 {
-       struct phy_ops const *ops = phy_dev_ops(phy->dev);
+       struct phy_ops const *ops;
+
+       if (!phy)
+               return 0;
+       ops = phy_dev_ops(phy->dev);
 
        return ops->exit ? ops->exit(phy) : 0;
 }
 
 int generic_phy_power_on(struct phy *phy)
 {
-       struct phy_ops const *ops = phy_dev_ops(phy->dev);
+       struct phy_ops const *ops;
+
+       if (!phy)
+               return 0;
+       ops = phy_dev_ops(phy->dev);
 
        return ops->power_on ? ops->power_on(phy) : 0;
 }
 
 int generic_phy_power_off(struct phy *phy)
 {
-       struct phy_ops const *ops = phy_dev_ops(phy->dev);
+       struct phy_ops const *ops;
+
+       if (!phy)
+               return 0;
+       ops = phy_dev_ops(phy->dev);
 
        return ops->power_off ? ops->power_off(phy) : 0;
 }
index 947c582f68b1f07e9d22f03de85328fcc7e9b873..95caf583413ce303c17662911060b7e7355feba1 100644 (file)
@@ -270,7 +270,7 @@ static inline int generic_phy_get_by_name(struct udevice *user, const char *phy_
  */
 static inline bool generic_phy_valid(struct phy *phy)
 {
-       return phy->dev != NULL;
+       return phy && phy->dev;
 }
 
 #endif /*__GENERIC_PHY_H */