1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8 #include <linux/of_device.h>
10 #include <asm/errno.h>
13 static void of_device_make_bus_id(struct of_device
*dev
)
15 static atomic_t bus_no_reg_magic
;
16 struct device_node
*node
= dev
->node
;
17 char *name
= dev
->dev
.bus_id
;
23 * If it's a DCR based device, use 'd' for native DCRs
24 * and 'D' for MMIO DCRs.
27 reg
= of_get_property(node
, "dcr-reg", NULL
);
29 #ifdef CONFIG_PPC_DCR_NATIVE
30 snprintf(name
, BUS_ID_SIZE
, "d%x.%s",
32 #else /* CONFIG_PPC_DCR_NATIVE */
33 addr
= of_translate_dcr_address(node
, *reg
, NULL
);
34 if (addr
!= OF_BAD_ADDR
) {
35 snprintf(name
, BUS_ID_SIZE
,
36 "D%llx.%s", (unsigned long long)addr
,
40 #endif /* !CONFIG_PPC_DCR_NATIVE */
42 #endif /* CONFIG_PPC_DCR */
45 * For MMIO, get the physical address
47 reg
= of_get_property(node
, "reg", NULL
);
49 addr
= of_translate_address(node
, reg
);
50 if (addr
!= OF_BAD_ADDR
) {
51 snprintf(name
, BUS_ID_SIZE
,
52 "%llx.%s", (unsigned long long)addr
,
59 * No BusID, use the node name and add a globally incremented
60 * counter (and pray...)
62 magic
= atomic_add_return(1, &bus_no_reg_magic
);
63 snprintf(name
, BUS_ID_SIZE
, "%s.%d", node
->name
, magic
- 1);
66 struct of_device
*of_device_alloc(struct device_node
*np
,
68 struct device
*parent
)
70 struct of_device
*dev
;
72 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
76 dev
->node
= of_node_get(np
);
77 dev
->dev
.dma_mask
= &dev
->dma_mask
;
78 dev
->dev
.parent
= parent
;
79 dev
->dev
.release
= of_release_dev
;
80 dev
->dev
.archdata
.of_node
= np
;
81 dev
->dev
.archdata
.numa_node
= of_node_to_nid(np
);
84 strlcpy(dev
->dev
.bus_id
, bus_id
, BUS_ID_SIZE
);
86 of_device_make_bus_id(dev
);
90 EXPORT_SYMBOL(of_device_alloc
);
92 ssize_t
of_device_get_modalias(struct of_device
*ofdev
,
93 char *str
, ssize_t len
)
97 ssize_t tsize
, csize
, repend
;
100 csize
= snprintf(str
, len
, "of:N%sT%s",
101 ofdev
->node
->name
, ofdev
->node
->type
);
103 /* Get compatible property if any */
104 compat
= of_get_property(ofdev
->node
, "compatible", &cplen
);
108 /* Find true end (we tolerate multiple \0 at the end */
109 for (i
=(cplen
-1); i
>=0 && !compat
[i
]; i
--)
115 /* Check space (need cplen+1 chars including final \0) */
116 tsize
= csize
+ cplen
;
119 if (csize
>=len
) /* @ the limit, all is already filled */
122 if (tsize
>=len
) { /* limit compat list */
127 /* Copy and do char replacement */
128 memcpy(&str
[csize
+1], compat
, cplen
);
129 for (i
=csize
; i
<repend
; i
++) {
140 int of_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
142 struct of_device
*ofdev
;
144 int seen
= 0, cplen
, sl
;
149 ofdev
= to_of_device(dev
);
151 if (add_uevent_var(env
, "OF_NAME=%s", ofdev
->node
->name
))
154 if (add_uevent_var(env
, "OF_TYPE=%s", ofdev
->node
->type
))
157 /* Since the compatible field can contain pretty much anything
158 * it's not really legal to split it out with commas. We split it
159 * up using a number of environment variables instead. */
161 compat
= of_get_property(ofdev
->node
, "compatible", &cplen
);
162 while (compat
&& *compat
&& cplen
> 0) {
163 if (add_uevent_var(env
, "OF_COMPATIBLE_%d=%s", seen
, compat
))
166 sl
= strlen (compat
) + 1;
172 if (add_uevent_var(env
, "OF_COMPATIBLE_N=%d", seen
))
175 /* modalias is trickier, we add it in 2 steps */
176 if (add_uevent_var(env
, "MODALIAS="))
178 sl
= of_device_get_modalias(ofdev
, &env
->buf
[env
->buflen
-1],
179 sizeof(env
->buf
) - env
->buflen
);
180 if (sl
>= (sizeof(env
->buf
) - env
->buflen
))
186 EXPORT_SYMBOL(of_device_uevent
);
187 EXPORT_SYMBOL(of_device_get_modalias
);