Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[pandora-kernel.git] / arch / powerpc / kernel / of_device.c
1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.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>
9
10 #include <asm/errno.h>
11 #include <asm/dcr.h>
12
13 static 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->dev.of_node;
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
29                 dev_set_name(&dev->dev, "d%x.%s", *reg, node->name);
30 #else /* CONFIG_PPC_DCR_NATIVE */
31                 addr = of_translate_dcr_address(node, *reg, NULL);
32                 if (addr != OF_BAD_ADDR) {
33                         dev_set_name(&dev->dev, "D%llx.%s",
34                                      (unsigned long long)addr, node->name);
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) {
48                         dev_set_name(&dev->dev, "%llx.%s",
49                                      (unsigned long long)addr, node->name);
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);
59         dev_set_name(&dev->dev, "%s.%d", node->name, magic - 1);
60 }
61
62 struct 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->dev.of_node = of_node_get(np);
73         dev->dev.dma_mask = &dev->archdata.dma_mask;
74         dev->dev.parent = parent;
75         dev->dev.release = of_release_dev;
76
77         if (bus_id)
78                 dev_set_name(&dev->dev, "%s", bus_id);
79         else
80                 of_device_make_bus_id(dev);
81
82         return dev;
83 }
84 EXPORT_SYMBOL(of_device_alloc);
85
86 int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
87 {
88         struct of_device *ofdev;
89         const char *compat;
90         int seen = 0, cplen, sl;
91
92         if (!dev)
93                 return -ENODEV;
94
95         ofdev = to_of_device(dev);
96
97         if (add_uevent_var(env, "OF_NAME=%s", ofdev->dev.of_node->name))
98                 return -ENOMEM;
99
100         if (add_uevent_var(env, "OF_TYPE=%s", ofdev->dev.of_node->type))
101                 return -ENOMEM;
102
103         /* Since the compatible field can contain pretty much anything
104          * it's not really legal to split it out with commas. We split it
105          * up using a number of environment variables instead. */
106
107         compat = of_get_property(ofdev->dev.of_node, "compatible", &cplen);
108         while (compat && *compat && cplen > 0) {
109                 if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
110                         return -ENOMEM;
111
112                 sl = strlen (compat) + 1;
113                 compat += sl;
114                 cplen -= sl;
115                 seen++;
116         }
117
118         if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
119                 return -ENOMEM;
120
121         /* modalias is trickier, we add it in 2 steps */
122         if (add_uevent_var(env, "MODALIAS="))
123                 return -ENOMEM;
124         sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
125                                     sizeof(env->buf) - env->buflen);
126         if (sl >= (sizeof(env->buf) - env->buflen))
127                 return -ENOMEM;
128         env->buflen += sl;
129
130         return 0;
131 }
132 EXPORT_SYMBOL(of_device_uevent);
133 EXPORT_SYMBOL(of_device_get_modalias);