Commit f018ae7a authored by Russell King's avatar Russell King Committed by David S. Miller

of_mdio: fix MDIO phy device refcounting

bus_find_device() is defined as:

 * This is similar to the bus_for_each_dev() function above, but it
 * returns a reference to a device that is 'found' for later use, as
 * determined by the @match callback.

and it does indeed return a reference-counted pointer to the device:

        while ((dev = next_device(&i)))
                if (match(dev, data) && get_device(dev))
                                        ^^^^^^^^^^^^^^^
                        break;
        klist_iter_exit(&i);
        return dev;

What that means is that when we're done with the struct device, we must
drop that reference.  Neither of_phy_connect() nor of_phy_attach() did
this when phy_connect_direct() or phy_attach_direct() failed.

With our previous patch, phy_connect_direct() and phy_attach_direct()
take a new refcount on the phy device when successful, so we can drop
our local reference immediatley after these functions, whether or not
they succeeded.
Signed-off-by: default avatarRussell King <rmk+kernel@arm.linux.org.uk>
Acked-by: default avatarRob Herring <robh@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7322967b
...@@ -197,7 +197,8 @@ static int of_phy_match(struct device *dev, void *phy_np) ...@@ -197,7 +197,8 @@ static int of_phy_match(struct device *dev, void *phy_np)
* of_phy_find_device - Give a PHY node, find the phy_device * of_phy_find_device - Give a PHY node, find the phy_device
* @phy_np: Pointer to the phy's device tree node * @phy_np: Pointer to the phy's device tree node
* *
* Returns a pointer to the phy_device. * If successful, returns a pointer to the phy_device with the embedded
* struct device refcount incremented by one, or NULL on failure.
*/ */
struct phy_device *of_phy_find_device(struct device_node *phy_np) struct phy_device *of_phy_find_device(struct device_node *phy_np)
{ {
...@@ -217,7 +218,9 @@ EXPORT_SYMBOL(of_phy_find_device); ...@@ -217,7 +218,9 @@ EXPORT_SYMBOL(of_phy_find_device);
* @hndlr: Link state callback for the network device * @hndlr: Link state callback for the network device
* @iface: PHY data interface type * @iface: PHY data interface type
* *
* Returns a pointer to the phy_device if successful. NULL otherwise * If successful, returns a pointer to the phy_device with the embedded
* struct device refcount incremented by one, or NULL on failure. The
* refcount must be dropped by calling phy_disconnect() or phy_detach().
*/ */
struct phy_device *of_phy_connect(struct net_device *dev, struct phy_device *of_phy_connect(struct net_device *dev,
struct device_node *phy_np, struct device_node *phy_np,
...@@ -225,13 +228,19 @@ struct phy_device *of_phy_connect(struct net_device *dev, ...@@ -225,13 +228,19 @@ struct phy_device *of_phy_connect(struct net_device *dev,
phy_interface_t iface) phy_interface_t iface)
{ {
struct phy_device *phy = of_phy_find_device(phy_np); struct phy_device *phy = of_phy_find_device(phy_np);
int ret;
if (!phy) if (!phy)
return NULL; return NULL;
phy->dev_flags = flags; phy->dev_flags = flags;
return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy; ret = phy_connect_direct(dev, phy, hndlr, iface);
/* refcount is held by phy_connect_direct() on success */
put_device(&phy->dev);
return ret ? NULL : phy;
} }
EXPORT_SYMBOL(of_phy_connect); EXPORT_SYMBOL(of_phy_connect);
...@@ -241,17 +250,27 @@ EXPORT_SYMBOL(of_phy_connect); ...@@ -241,17 +250,27 @@ EXPORT_SYMBOL(of_phy_connect);
* @phy_np: Node pointer for the PHY * @phy_np: Node pointer for the PHY
* @flags: flags to pass to the PHY * @flags: flags to pass to the PHY
* @iface: PHY data interface type * @iface: PHY data interface type
*
* If successful, returns a pointer to the phy_device with the embedded
* struct device refcount incremented by one, or NULL on failure. The
* refcount must be dropped by calling phy_disconnect() or phy_detach().
*/ */
struct phy_device *of_phy_attach(struct net_device *dev, struct phy_device *of_phy_attach(struct net_device *dev,
struct device_node *phy_np, u32 flags, struct device_node *phy_np, u32 flags,
phy_interface_t iface) phy_interface_t iface)
{ {
struct phy_device *phy = of_phy_find_device(phy_np); struct phy_device *phy = of_phy_find_device(phy_np);
int ret;
if (!phy) if (!phy)
return NULL; return NULL;
return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy; ret = phy_attach_direct(dev, phy, flags, iface);
/* refcount is held by phy_attach_direct() on success */
put_device(&phy->dev);
return ret ? NULL : phy;
} }
EXPORT_SYMBOL(of_phy_attach); EXPORT_SYMBOL(of_phy_attach);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment