eeepc-laptop: Register as a pci-hotplug device
[linux-2.6/linux-acpi-2.6.git] / arch / sparc / kernel / prom_common.c
blob0fb5789d43c82e84977757251ad78a6fa834d9b5
1 /* prom_common.c: OF device tree support common code.
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
9 * Adapted for sparc by David S. Miller davem@davemloft.net
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <asm/prom.h>
24 #include <asm/oplib.h>
26 #include "prom.h"
28 struct device_node *of_console_device;
29 EXPORT_SYMBOL(of_console_device);
31 char *of_console_path;
32 EXPORT_SYMBOL(of_console_path);
34 char *of_console_options;
35 EXPORT_SYMBOL(of_console_options);
37 struct device_node *of_find_node_by_phandle(phandle handle)
39 struct device_node *np;
41 for (np = allnodes; np; np = np->allnext)
42 if (np->node == handle)
43 break;
45 return np;
47 EXPORT_SYMBOL(of_find_node_by_phandle);
49 int of_getintprop_default(struct device_node *np, const char *name, int def)
51 struct property *prop;
52 int len;
54 prop = of_find_property(np, name, &len);
55 if (!prop || len != 4)
56 return def;
58 return *(int *) prop->value;
60 EXPORT_SYMBOL(of_getintprop_default);
62 DEFINE_MUTEX(of_set_property_mutex);
63 EXPORT_SYMBOL(of_set_property_mutex);
65 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
67 struct property **prevp;
68 void *new_val;
69 int err;
71 new_val = kmalloc(len, GFP_KERNEL);
72 if (!new_val)
73 return -ENOMEM;
75 memcpy(new_val, val, len);
77 err = -ENODEV;
79 write_lock(&devtree_lock);
80 prevp = &dp->properties;
81 while (*prevp) {
82 struct property *prop = *prevp;
84 if (!strcasecmp(prop->name, name)) {
85 void *old_val = prop->value;
86 int ret;
88 mutex_lock(&of_set_property_mutex);
89 ret = prom_setprop(dp->node, name, val, len);
90 mutex_unlock(&of_set_property_mutex);
92 err = -EINVAL;
93 if (ret >= 0) {
94 prop->value = new_val;
95 prop->length = len;
97 if (OF_IS_DYNAMIC(prop))
98 kfree(old_val);
100 OF_MARK_DYNAMIC(prop);
102 err = 0;
104 break;
106 prevp = &(*prevp)->next;
108 write_unlock(&devtree_lock);
110 /* XXX Upate procfs if necessary... */
112 return err;
114 EXPORT_SYMBOL(of_set_property);
116 int of_find_in_proplist(const char *list, const char *match, int len)
118 while (len > 0) {
119 int l;
121 if (!strcmp(list, match))
122 return 1;
123 l = strlen(list) + 1;
124 list += l;
125 len -= l;
127 return 0;
129 EXPORT_SYMBOL(of_find_in_proplist);
131 unsigned int prom_unique_id;
133 static struct property * __init build_one_prop(phandle node, char *prev,
134 char *special_name,
135 void *special_val,
136 int special_len)
138 static struct property *tmp = NULL;
139 struct property *p;
140 const char *name;
142 if (tmp) {
143 p = tmp;
144 memset(p, 0, sizeof(*p) + 32);
145 tmp = NULL;
146 } else {
147 p = prom_early_alloc(sizeof(struct property) + 32);
148 p->unique_id = prom_unique_id++;
151 p->name = (char *) (p + 1);
152 if (special_name) {
153 strcpy(p->name, special_name);
154 p->length = special_len;
155 p->value = prom_early_alloc(special_len);
156 memcpy(p->value, special_val, special_len);
157 } else {
158 if (prev == NULL) {
159 name = prom_firstprop(node, p->name);
160 } else {
161 name = prom_nextprop(node, prev, p->name);
164 if (strlen(name) == 0) {
165 tmp = p;
166 return NULL;
168 #ifdef CONFIG_SPARC32
169 strcpy(p->name, name);
170 #endif
171 p->length = prom_getproplen(node, p->name);
172 if (p->length <= 0) {
173 p->length = 0;
174 } else {
175 int len;
177 p->value = prom_early_alloc(p->length + 1);
178 len = prom_getproperty(node, p->name, p->value,
179 p->length);
180 if (len <= 0)
181 p->length = 0;
182 ((unsigned char *)p->value)[p->length] = '\0';
185 return p;
188 static struct property * __init build_prop_list(phandle node)
190 struct property *head, *tail;
192 head = tail = build_one_prop(node, NULL,
193 ".node", &node, sizeof(node));
195 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
196 tail = tail->next;
197 while(tail) {
198 tail->next = build_one_prop(node, tail->name,
199 NULL, NULL, 0);
200 tail = tail->next;
203 return head;
206 static char * __init get_one_property(phandle node, const char *name)
208 char *buf = "<NULL>";
209 int len;
211 len = prom_getproplen(node, name);
212 if (len > 0) {
213 buf = prom_early_alloc(len);
214 len = prom_getproperty(node, name, buf, len);
217 return buf;
220 static struct device_node * __init prom_create_node(phandle node,
221 struct device_node *parent)
223 struct device_node *dp;
225 if (!node)
226 return NULL;
228 dp = prom_early_alloc(sizeof(*dp));
229 dp->unique_id = prom_unique_id++;
230 dp->parent = parent;
232 kref_init(&dp->kref);
234 dp->name = get_one_property(node, "name");
235 dp->type = get_one_property(node, "device_type");
236 dp->node = node;
238 dp->properties = build_prop_list(node);
240 irq_trans_init(dp);
242 return dp;
245 static char * __init build_full_name(struct device_node *dp)
247 int len, ourlen, plen;
248 char *n;
250 plen = strlen(dp->parent->full_name);
251 ourlen = strlen(dp->path_component_name);
252 len = ourlen + plen + 2;
254 n = prom_early_alloc(len);
255 strcpy(n, dp->parent->full_name);
256 if (!is_root_node(dp->parent)) {
257 strcpy(n + plen, "/");
258 plen++;
260 strcpy(n + plen, dp->path_component_name);
262 return n;
265 static struct device_node * __init prom_build_tree(struct device_node *parent,
266 phandle node,
267 struct device_node ***nextp)
269 struct device_node *ret = NULL, *prev_sibling = NULL;
270 struct device_node *dp;
272 while (1) {
273 dp = prom_create_node(node, parent);
274 if (!dp)
275 break;
277 if (prev_sibling)
278 prev_sibling->sibling = dp;
280 if (!ret)
281 ret = dp;
282 prev_sibling = dp;
284 *(*nextp) = dp;
285 *nextp = &dp->allnext;
287 dp->path_component_name = build_path_component(dp);
288 dp->full_name = build_full_name(dp);
290 dp->child = prom_build_tree(dp, prom_getchild(node), nextp);
292 node = prom_getsibling(node);
295 return ret;
298 unsigned int prom_early_allocated __initdata;
300 void __init prom_build_devicetree(void)
302 struct device_node **nextp;
304 allnodes = prom_create_node(prom_root_node, NULL);
305 allnodes->path_component_name = "";
306 allnodes->full_name = "/";
308 nextp = &allnodes->allnext;
309 allnodes->child = prom_build_tree(allnodes,
310 prom_getchild(allnodes->node),
311 &nextp);
312 of_console_init();
314 printk("PROM: Built device tree with %u bytes of memory.\n",
315 prom_early_allocated);