Skip to content

Commit 7087446

Browse files
Chunfeng Yunkishon
authored andcommitted
phy: core: Get a refcount to phy in devm_of_phy_get_by_index()
On driver detach, devm_phy_release() will put a refcount to the phy, so gets a refconut to it before return. Signed-off-by: Chunfeng Yun <[email protected]> Signed-off-by: Kishon Vijay Abraham I <[email protected]>
1 parent a8c2472 commit 7087446

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

drivers/phy/phy-core.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,9 @@ EXPORT_SYMBOL_GPL(devm_of_phy_get);
636636
* @np: node containing the phy
637637
* @index: index of the phy
638638
*
639-
* Gets the phy using _of_phy_get(), and associates a device with it using
640-
* devres. On driver detach, release function is invoked on the devres data,
639+
* Gets the phy using _of_phy_get(), then gets a refcount to it,
640+
* and associates a device with it using devres. On driver detach,
641+
* release function is invoked on the devres data,
641642
* then, devres data is freed.
642643
*
643644
*/
@@ -651,13 +652,21 @@ struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
651652
return ERR_PTR(-ENOMEM);
652653

653654
phy = _of_phy_get(np, index);
654-
if (!IS_ERR(phy)) {
655-
*ptr = phy;
656-
devres_add(dev, ptr);
657-
} else {
655+
if (IS_ERR(phy)) {
658656
devres_free(ptr);
657+
return phy;
659658
}
660659

660+
if (!try_module_get(phy->ops->owner)) {
661+
devres_free(ptr);
662+
return ERR_PTR(-EPROBE_DEFER);
663+
}
664+
665+
get_device(&phy->dev);
666+
667+
*ptr = phy;
668+
devres_add(dev, ptr);
669+
661670
return phy;
662671
}
663672
EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);

0 commit comments

Comments
 (0)