Commit | Line | Data |
---|---|---|
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 |
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->node; | |
17 | char *name = dev->dev.bus_id; | |
18 | const u32 *reg; | |
19 | u64 addr; | |
20 | int magic; | |
21 | ||
22 | /* | |
23 | * If it's a DCR based device, use 'd' for native DCRs | |
24 | * and 'D' for MMIO DCRs. | |
25 | */ | |
26 | #ifdef CONFIG_PPC_DCR | |
27 | reg = of_get_property(node, "dcr-reg", NULL); | |
28 | if (reg) { | |
29 | #ifdef CONFIG_PPC_DCR_NATIVE | |
30 | snprintf(name, BUS_ID_SIZE, "d%x.%s", | |
31 | *reg, node->name); | |
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, | |
37 | node->name); | |
38 | return; | |
39 | } | |
40 | #endif /* !CONFIG_PPC_DCR_NATIVE */ | |
41 | } | |
42 | #endif /* CONFIG_PPC_DCR */ | |
43 | ||
44 | /* | |
45 | * For MMIO, get the physical address | |
46 | */ | |
47 | reg = of_get_property(node, "reg", NULL); | |
48 | if (reg) { | |
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, | |
53 | node->name); | |
54 | return; | |
55 | } | |
56 | } | |
57 | ||
58 | /* | |
59 | * No BusID, use the node name and add a globally incremented | |
60 | * counter (and pray...) | |
61 | */ | |
62 | magic = atomic_add_return(1, &bus_no_reg_magic); | |
63 | snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1); | |
64 | } | |
65 | ||
66 | struct of_device *of_device_alloc(struct device_node *np, | |
67 | const char *bus_id, | |
68 | struct device *parent) | |
69 | { | |
70 | struct of_device *dev; | |
71 | ||
72 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
73 | if (!dev) | |
74 | return NULL; | |
75 | ||
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; | |
fec738dd JF |
81 | |
82 | if (bus_id) | |
83 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); | |
84 | else | |
85 | of_device_make_bus_id(dev); | |
86 | ||
87 | return dev; | |
88 | } | |
89 | EXPORT_SYMBOL(of_device_alloc); | |
90 | ||
7eff2e7a | 91 | int of_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
eb0cb8a0 SM |
92 | { |
93 | struct of_device *ofdev; | |
94 | const char *compat; | |
7eff2e7a | 95 | int seen = 0, cplen, sl; |
eb0cb8a0 SM |
96 | |
97 | if (!dev) | |
98 | return -ENODEV; | |
99 | ||
100 | ofdev = to_of_device(dev); | |
101 | ||
7eff2e7a | 102 | if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name)) |
eb0cb8a0 SM |
103 | return -ENOMEM; |
104 | ||
7eff2e7a | 105 | if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type)) |
eb0cb8a0 SM |
106 | return -ENOMEM; |
107 | ||
108 | /* Since the compatible field can contain pretty much anything | |
109 | * it's not really legal to split it out with commas. We split it | |
110 | * up using a number of environment variables instead. */ | |
111 | ||
b3a6d2a5 | 112 | compat = of_get_property(ofdev->node, "compatible", &cplen); |
eb0cb8a0 | 113 | while (compat && *compat && cplen > 0) { |
7eff2e7a | 114 | if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat)) |
eb0cb8a0 SM |
115 | return -ENOMEM; |
116 | ||
117 | sl = strlen (compat) + 1; | |
118 | compat += sl; | |
119 | cplen -= sl; | |
120 | seen++; | |
121 | } | |
122 | ||
7eff2e7a | 123 | if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen)) |
eb0cb8a0 SM |
124 | return -ENOMEM; |
125 | ||
126 | /* modalias is trickier, we add it in 2 steps */ | |
7eff2e7a | 127 | if (add_uevent_var(env, "MODALIAS=")) |
eb0cb8a0 | 128 | return -ENOMEM; |
7eff2e7a KS |
129 | sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1], |
130 | sizeof(env->buf) - env->buflen); | |
131 | if (sl >= (sizeof(env->buf) - env->buflen)) | |
eb0cb8a0 | 132 | return -ENOMEM; |
7eff2e7a | 133 | env->buflen += sl; |
eb0cb8a0 SM |
134 | |
135 | return 0; | |
136 | } | |
eb0cb8a0 | 137 | EXPORT_SYMBOL(of_device_uevent); |
de41189b | 138 | EXPORT_SYMBOL(of_device_get_modalias); |