Linux 2.6.35-rc2
[linux/fpc-iii.git] / arch / sparc / prom / tree_32.c
blobb21592f8e3fe98db86e3fd1c69e72666989d8bce
1 /*
2 * tree.c: Basic device tree traversal/scanning for the Linux
3 * prom library.
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 */
8 #include <linux/string.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/ctype.h>
13 #include <linux/module.h>
15 #include <asm/openprom.h>
16 #include <asm/oplib.h>
18 extern void restore_current(void);
20 static char promlib_buf[128];
22 /* Internal version of prom_getchild that does not alter return values. */
23 int __prom_getchild(int node)
25 unsigned long flags;
26 int cnode;
28 spin_lock_irqsave(&prom_lock, flags);
29 cnode = prom_nodeops->no_child(node);
30 restore_current();
31 spin_unlock_irqrestore(&prom_lock, flags);
33 return cnode;
36 /* Return the child of node 'node' or zero if no this node has no
37 * direct descendent.
39 int prom_getchild(int node)
41 int cnode;
43 if (node == -1)
44 return 0;
46 cnode = __prom_getchild(node);
47 if (cnode == 0 || cnode == -1)
48 return 0;
50 return cnode;
52 EXPORT_SYMBOL(prom_getchild);
54 /* Internal version of prom_getsibling that does not alter return values. */
55 int __prom_getsibling(int node)
57 unsigned long flags;
58 int cnode;
60 spin_lock_irqsave(&prom_lock, flags);
61 cnode = prom_nodeops->no_nextnode(node);
62 restore_current();
63 spin_unlock_irqrestore(&prom_lock, flags);
65 return cnode;
68 /* Return the next sibling of node 'node' or zero if no more siblings
69 * at this level of depth in the tree.
71 int prom_getsibling(int node)
73 int sibnode;
75 if (node == -1)
76 return 0;
78 sibnode = __prom_getsibling(node);
79 if (sibnode == 0 || sibnode == -1)
80 return 0;
82 return sibnode;
84 EXPORT_SYMBOL(prom_getsibling);
86 /* Return the length in bytes of property 'prop' at node 'node'.
87 * Return -1 on error.
89 int prom_getproplen(int node, const char *prop)
91 int ret;
92 unsigned long flags;
94 if((!node) || (!prop))
95 return -1;
97 spin_lock_irqsave(&prom_lock, flags);
98 ret = prom_nodeops->no_proplen(node, prop);
99 restore_current();
100 spin_unlock_irqrestore(&prom_lock, flags);
101 return ret;
103 EXPORT_SYMBOL(prom_getproplen);
105 /* Acquire a property 'prop' at node 'node' and place it in
106 * 'buffer' which has a size of 'bufsize'. If the acquisition
107 * was successful the length will be returned, else -1 is returned.
109 int prom_getproperty(int node, const char *prop, char *buffer, int bufsize)
111 int plen, ret;
112 unsigned long flags;
114 plen = prom_getproplen(node, prop);
115 if((plen > bufsize) || (plen == 0) || (plen == -1))
116 return -1;
117 /* Ok, things seem all right. */
118 spin_lock_irqsave(&prom_lock, flags);
119 ret = prom_nodeops->no_getprop(node, prop, buffer);
120 restore_current();
121 spin_unlock_irqrestore(&prom_lock, flags);
122 return ret;
124 EXPORT_SYMBOL(prom_getproperty);
126 /* Acquire an integer property and return its value. Returns -1
127 * on failure.
129 int prom_getint(int node, char *prop)
131 static int intprop;
133 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
134 return intprop;
136 return -1;
138 EXPORT_SYMBOL(prom_getint);
140 /* Acquire an integer property, upon error return the passed default
141 * integer.
143 int prom_getintdefault(int node, char *property, int deflt)
145 int retval;
147 retval = prom_getint(node, property);
148 if(retval == -1) return deflt;
150 return retval;
152 EXPORT_SYMBOL(prom_getintdefault);
154 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
155 int prom_getbool(int node, char *prop)
157 int retval;
159 retval = prom_getproplen(node, prop);
160 if(retval == -1) return 0;
161 return 1;
163 EXPORT_SYMBOL(prom_getbool);
165 /* Acquire a property whose value is a string, returns a null
166 * string on error. The char pointer is the user supplied string
167 * buffer.
169 void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
171 int len;
173 len = prom_getproperty(node, prop, user_buf, ubuf_size);
174 if(len != -1) return;
175 user_buf[0] = 0;
177 EXPORT_SYMBOL(prom_getstring);
180 /* Does the device at node 'node' have name 'name'?
181 * YES = 1 NO = 0
183 int prom_nodematch(int node, char *name)
185 int error;
187 static char namebuf[128];
188 error = prom_getproperty(node, "name", namebuf, sizeof(namebuf));
189 if (error == -1) return 0;
190 if(strcmp(namebuf, name) == 0) return 1;
191 return 0;
194 /* Search siblings at 'node_start' for a node with name
195 * 'nodename'. Return node if successful, zero if not.
197 int prom_searchsiblings(int node_start, char *nodename)
200 int thisnode, error;
202 for(thisnode = node_start; thisnode;
203 thisnode=prom_getsibling(thisnode)) {
204 error = prom_getproperty(thisnode, "name", promlib_buf,
205 sizeof(promlib_buf));
206 /* Should this ever happen? */
207 if(error == -1) continue;
208 if(strcmp(nodename, promlib_buf)==0) return thisnode;
211 return 0;
213 EXPORT_SYMBOL(prom_searchsiblings);
215 /* Interal version of nextprop that does not alter return values. */
216 char * __prom_nextprop(int node, char * oprop)
218 unsigned long flags;
219 char *prop;
221 spin_lock_irqsave(&prom_lock, flags);
222 prop = prom_nodeops->no_nextprop(node, oprop);
223 restore_current();
224 spin_unlock_irqrestore(&prom_lock, flags);
226 return prop;
229 /* Return the first property name for node 'node'. */
230 /* buffer is unused argument, but as v9 uses it, we need to have the same interface */
231 char * prom_firstprop(int node, char *bufer)
233 if (node == 0 || node == -1)
234 return "";
236 return __prom_nextprop(node, "");
238 EXPORT_SYMBOL(prom_firstprop);
240 /* Return the property type string after property type 'oprop'
241 * at node 'node' . Returns empty string if no more
242 * property types for this node.
244 char * prom_nextprop(int node, char *oprop, char *buffer)
246 if (node == 0 || node == -1)
247 return "";
249 return __prom_nextprop(node, oprop);
251 EXPORT_SYMBOL(prom_nextprop);
253 int prom_finddevice(char *name)
255 char nbuf[128];
256 char *s = name, *d;
257 int node = prom_root_node, node2;
258 unsigned int which_io, phys_addr;
259 struct linux_prom_registers reg[PROMREG_MAX];
261 while (*s++) {
262 if (!*s) return node; /* path '.../' is legal */
263 node = prom_getchild(node);
265 for (d = nbuf; *s != 0 && *s != '@' && *s != '/';)
266 *d++ = *s++;
267 *d = 0;
269 node = prom_searchsiblings(node, nbuf);
270 if (!node)
271 return 0;
273 if (*s == '@') {
274 if (isxdigit(s[1]) && s[2] == ',') {
275 which_io = simple_strtoul(s+1, NULL, 16);
276 phys_addr = simple_strtoul(s+3, &d, 16);
277 if (d != s + 3 && (!*d || *d == '/')
278 && d <= s + 3 + 8) {
279 node2 = node;
280 while (node2 && node2 != -1) {
281 if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) {
282 if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) {
283 node = node2;
284 break;
287 node2 = prom_getsibling(node2);
288 if (!node2 || node2 == -1)
289 break;
290 node2 = prom_searchsiblings(prom_getsibling(node2), nbuf);
294 while (*s != 0 && *s != '/') s++;
297 return node;
299 EXPORT_SYMBOL(prom_finddevice);
301 int prom_node_has_property(int node, char *prop)
303 char *current_property = "";
305 do {
306 current_property = prom_nextprop(node, current_property, NULL);
307 if(!strcmp(current_property, prop))
308 return 1;
309 } while (*current_property);
310 return 0;
312 EXPORT_SYMBOL(prom_node_has_property);
314 /* Set property 'pname' at node 'node' to value 'value' which has a length
315 * of 'size' bytes. Return the number of bytes the prom accepted.
317 int prom_setprop(int node, const char *pname, char *value, int size)
319 unsigned long flags;
320 int ret;
322 if(size == 0) return 0;
323 if((pname == 0) || (value == 0)) return 0;
324 spin_lock_irqsave(&prom_lock, flags);
325 ret = prom_nodeops->no_setprop(node, pname, value, size);
326 restore_current();
327 spin_unlock_irqrestore(&prom_lock, flags);
328 return ret;
330 EXPORT_SYMBOL(prom_setprop);
332 int prom_inst2pkg(int inst)
334 int node;
335 unsigned long flags;
337 spin_lock_irqsave(&prom_lock, flags);
338 node = (*romvec->pv_v2devops.v2_inst2pkg)(inst);
339 restore_current();
340 spin_unlock_irqrestore(&prom_lock, flags);
341 if (node == -1) return 0;
342 return node;
345 /* Return 'node' assigned to a particular prom 'path'
346 * FIXME: Should work for v0 as well
348 int prom_pathtoinode(char *path)
350 int node, inst;
352 inst = prom_devopen (path);
353 if (inst == -1) return 0;
354 node = prom_inst2pkg (inst);
355 prom_devclose (inst);
356 if (node == -1) return 0;
357 return node;