Skip to content

Commit 4e75e1d

Browse files
jhovoldgregkh
authored andcommitted
driver core: add helper to reuse a device-tree node
Add a helper function to be used when reusing the device-tree node of another device. It is fairly common for drivers to reuse the device-tree node of a parent (or other ancestor) device when creating class or bus devices (e.g. gpio chips, i2c adapters, iio chips, spi masters, serdev, phys, usb root hubs). But reusing a device-tree node may cause problems if the new device is later probed as for example driver core would currently attempt to reinitialise an already active associated pinmux configuration. Other potential issues include the platform-bus code unconditionally dropping the device-tree node reference in its device destructor, reinitialisation of other bus-managed resources such as clocks, and the recently added DMA-setup in driver core. Note that for most examples above this is currently not an issue as the devices are never probed, but this is a problem for the USB bus which has recently gained device-tree support. This was discovered and worked-around in a rather ad-hoc fashion by commit dc5878a ("usb: core: move root hub's device node assignment after it is added to bus") by not setting the of_node pointer until after the root-hub device has been registered. Instead we can allow devices to reuse a device-tree node by setting a flag in their struct device that can be used by core, bus and driver code to avoid resources from being over-allocated. Note that the helper also grabs an extra reference to the device node, which specifically balances the unconditional put in the platform-device destructor. Signed-off-by: Johan Hovold <johan@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 60a93cf commit 4e75e1d

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎drivers/base/core.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,3 +2884,19 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
28842884
else
28852885
dev->fwnode = fwnode;
28862886
}
2887+
2888+
/**
2889+
* device_set_of_node_from_dev - reuse device-tree node of another device
2890+
* @dev: device whose device-tree node is being set
2891+
* @dev2: device whose device-tree node is being reused
2892+
*
2893+
* Takes another reference to the new device-tree node after first dropping
2894+
* any reference held to the old node.
2895+
*/
2896+
void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
2897+
{
2898+
of_node_put(dev->of_node);
2899+
dev->of_node = of_node_get(dev2->of_node);
2900+
dev->of_node_reused = true;
2901+
}
2902+
EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);

‎include/linux/device.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ struct dev_links_info {
879879
*
880880
* @offline_disabled: If set, the device is permanently online.
881881
* @offline: Set after successful invocation of bus type's .offline().
882+
* @of_node_reused: Set if the device-tree node is shared with an ancestor
883+
* device.
882884
*
883885
* At the lowest level, every device in a Linux system is represented by an
884886
* instance of struct device. The device structure contains the information
@@ -966,6 +968,7 @@ struct device {
966968

967969
bool offline_disabled:1;
968970
bool offline:1;
971+
bool of_node_reused:1;
969972
};
970973

971974
static inline struct device *kobj_to_dev(struct kobject *kobj)
@@ -1144,6 +1147,7 @@ extern int device_offline(struct device *dev);
11441147
extern int device_online(struct device *dev);
11451148
extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
11461149
extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1150+
void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
11471151

11481152
static inline int dev_num_vf(struct device *dev)
11491153
{

0 commit comments

Comments
 (0)