Skip to content

Commit 07e461c

Browse files
committed
of: Ensure unique names without sacrificing determinism
The way the driver core is implemented, every device using the same bus type is required to have a unique name because a symlink to each device is created in the appropriate /sys/bus/*/devices directory, and two identical names causes a collision. The current code handles the requirement by using an globally incremented counter that is appended to the device name. It works, but it means any change to device registration will change the assigned numbers. Instead, if we build up the name by using information from the parent nodes, then it can be guaranteed to be unique without adding a random number to the end of it. Signed-off-by: Grant Likely <[email protected]> Cc: Ezequiel Garcia <[email protected]> Cc: Rob Herring <[email protected]>
1 parent eafd370 commit 07e461c

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

drivers/of/platform.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,15 @@ EXPORT_SYMBOL(of_find_device_by_node);
6868
* of_device_make_bus_id - Use the device node data to assign a unique name
6969
* @dev: pointer to device structure that is linked to a device tree node
7070
*
71-
* This routine will first try using either the dcr-reg or the reg property
72-
* value to derive a unique name. As a last resort it will use the node
73-
* name followed by a unique number.
71+
* This routine will first try using the translated bus address to
72+
* derive a unique name. If it cannot, then it will prepend names from
73+
* parent nodes until a unique name can be derived.
7474
*/
7575
void of_device_make_bus_id(struct device *dev)
7676
{
77-
static atomic_t bus_no_reg_magic;
7877
struct device_node *node = dev->of_node;
7978
const __be32 *reg;
8079
u64 addr;
81-
int magic;
8280

8381
#ifdef CONFIG_PPC_DCR
8482
/*
@@ -100,25 +98,25 @@ void of_device_make_bus_id(struct device *dev)
10098
}
10199
#endif /* CONFIG_PPC_DCR */
102100

103-
/*
104-
* For MMIO, get the physical address
105-
*/
106-
reg = of_get_property(node, "reg", NULL);
107-
if (reg) {
108-
addr = of_translate_address(node, reg);
109-
if (addr != OF_BAD_ADDR) {
110-
dev_set_name(dev, "%llx.%s",
111-
(unsigned long long)addr, node->name);
101+
/* Construct the name, using parent nodes if necessary to ensure uniqueness */
102+
while (node->parent) {
103+
/*
104+
* If the address can be translated, then that is as much
105+
* uniqueness as we need. Make it the first component and return
106+
*/
107+
reg = of_get_property(node, "reg", NULL);
108+
if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
109+
dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
110+
(unsigned long long)addr, node->name,
111+
dev_name(dev));
112112
return;
113113
}
114-
}
115114

116-
/*
117-
* No BusID, use the node name and add a globally incremented
118-
* counter (and pray...)
119-
*/
120-
magic = atomic_add_return(1, &bus_no_reg_magic);
121-
dev_set_name(dev, "%s.%d", node->name, magic - 1);
115+
/* format arguments only used if dev_name() resolves to NULL */
116+
dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
117+
strrchr(node->full_name, '/') + 1, dev_name(dev));
118+
node = node->parent;
119+
}
122120
}
123121

124122
/**

0 commit comments

Comments
 (0)