driver-core: Add device node pointer to struct device
[deliverable/linux.git] / arch / powerpc / kernel / of_device.c
CommitLineData
2e686bc3
PM
1#include <linux/string.h>
2#include <linux/kernel.h>
f85ff305 3#include <linux/of.h>
2e686bc3
PM
4#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/mod_devicetable.h>
734d6524 7#include <linux/slab.h>
0173d422 8#include <linux/of_device.h>
734d6524 9
2e686bc3 10#include <asm/errno.h>
fec738dd 11#include <asm/dcr.h>
2e686bc3 12
fec738dd
JF
13static void of_device_make_bus_id(struct of_device *dev)
14{
15 static atomic_t bus_no_reg_magic;
16 struct device_node *node = dev->node;
fec738dd
JF
17 const u32 *reg;
18 u64 addr;
19 int magic;
20
21 /*
22 * If it's a DCR based device, use 'd' for native DCRs
23 * and 'D' for MMIO DCRs.
24 */
25#ifdef CONFIG_PPC_DCR
26 reg = of_get_property(node, "dcr-reg", NULL);
27 if (reg) {
28#ifdef CONFIG_PPC_DCR_NATIVE
aab0d375 29 dev_set_name(&dev->dev, "d%x.%s", *reg, node->name);
fec738dd
JF
30#else /* CONFIG_PPC_DCR_NATIVE */
31 addr = of_translate_dcr_address(node, *reg, NULL);
32 if (addr != OF_BAD_ADDR) {
aab0d375
KS
33 dev_set_name(&dev->dev, "D%llx.%s",
34 (unsigned long long)addr, node->name);
fec738dd
JF
35 return;
36 }
37#endif /* !CONFIG_PPC_DCR_NATIVE */
38 }
39#endif /* CONFIG_PPC_DCR */
40
41 /*
42 * For MMIO, get the physical address
43 */
44 reg = of_get_property(node, "reg", NULL);
45 if (reg) {
46 addr = of_translate_address(node, reg);
47 if (addr != OF_BAD_ADDR) {
aab0d375
KS
48 dev_set_name(&dev->dev, "%llx.%s",
49 (unsigned long long)addr, node->name);
fec738dd
JF
50 return;
51 }
52 }
53
54 /*
55 * No BusID, use the node name and add a globally incremented
56 * counter (and pray...)
57 */
58 magic = atomic_add_return(1, &bus_no_reg_magic);
aab0d375 59 dev_set_name(&dev->dev, "%s.%d", node->name, magic - 1);
fec738dd
JF
60}
61
62struct of_device *of_device_alloc(struct device_node *np,
63 const char *bus_id,
64 struct device *parent)
65{
66 struct of_device *dev;
67
68 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
69 if (!dev)
70 return NULL;
71
72 dev->node = of_node_get(np);
73 dev->dev.dma_mask = &dev->dma_mask;
74 dev->dev.parent = parent;
75 dev->dev.release = of_release_dev;
76 dev->dev.archdata.of_node = np;
d706c1b0 77 dev->dev.of_node = np;
fec738dd
JF
78
79 if (bus_id)
03c01aa7 80 dev_set_name(&dev->dev, "%s", bus_id);
fec738dd
JF
81 else
82 of_device_make_bus_id(dev);
83
84 return dev;
85}
86EXPORT_SYMBOL(of_device_alloc);
87
7eff2e7a 88int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
eb0cb8a0
SM
89{
90 struct of_device *ofdev;
91 const char *compat;
7eff2e7a 92 int seen = 0, cplen, sl;
eb0cb8a0
SM
93
94 if (!dev)
95 return -ENODEV;
96
97 ofdev = to_of_device(dev);
98
7eff2e7a 99 if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
eb0cb8a0
SM
100 return -ENOMEM;
101
7eff2e7a 102 if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
eb0cb8a0
SM
103 return -ENOMEM;
104
105 /* Since the compatible field can contain pretty much anything
106 * it's not really legal to split it out with commas. We split it
107 * up using a number of environment variables instead. */
108
b3a6d2a5 109 compat = of_get_property(ofdev->node, "compatible", &cplen);
eb0cb8a0 110 while (compat && *compat && cplen > 0) {
7eff2e7a 111 if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
eb0cb8a0
SM
112 return -ENOMEM;
113
114 sl = strlen (compat) + 1;
115 compat += sl;
116 cplen -= sl;
117 seen++;
118 }
119
7eff2e7a 120 if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
eb0cb8a0
SM
121 return -ENOMEM;
122
123 /* modalias is trickier, we add it in 2 steps */
7eff2e7a 124 if (add_uevent_var(env, "MODALIAS="))
eb0cb8a0 125 return -ENOMEM;
7eff2e7a
KS
126 sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
127 sizeof(env->buf) - env->buflen);
128 if (sl >= (sizeof(env->buf) - env->buflen))
eb0cb8a0 129 return -ENOMEM;
7eff2e7a 130 env->buflen += sl;
eb0cb8a0
SM
131
132 return 0;
133}
eb0cb8a0 134EXPORT_SYMBOL(of_device_uevent);
de41189b 135EXPORT_SYMBOL(of_device_get_modalias);
This page took 0.358859 seconds and 5 git commands to generate.