sparc: move EXPORT_SYMBOL to the symbols definition
[linux-2.6/verdex.git] / arch / sparc / prom / tree_32.c
blob9d9965cd8bcafff298e5cb421c41aa58d0bcd3f7
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 #define PROMLIB_INTERNAL
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/ctype.h>
15 #include <linux/module.h>
17 #include <asm/openprom.h>
18 #include <asm/oplib.h>
20 extern void restore_current(void);
22 static char promlib_buf[128];
24 /* Internal version of prom_getchild that does not alter return values. */
25 int __prom_getchild(int node)
27 unsigned long flags;
28 int cnode;
30 spin_lock_irqsave(&prom_lock, flags);
31 cnode = prom_nodeops->no_child(node);
32 restore_current();
33 spin_unlock_irqrestore(&prom_lock, flags);
35 return cnode;
37 EXPORT_SYMBOL(__prom_getchild);
39 /* Return the child of node 'node' or zero if no this node has no
40 * direct descendent.
42 int prom_getchild(int node)
44 int cnode;
46 if (node == -1)
47 return 0;
49 cnode = __prom_getchild(node);
50 if (cnode == 0 || cnode == -1)
51 return 0;
53 return cnode;
55 EXPORT_SYMBOL(prom_getchild);
57 /* Internal version of prom_getsibling that does not alter return values. */
58 int __prom_getsibling(int node)
60 unsigned long flags;
61 int cnode;
63 spin_lock_irqsave(&prom_lock, flags);
64 cnode = prom_nodeops->no_nextnode(node);
65 restore_current();
66 spin_unlock_irqrestore(&prom_lock, flags);
68 return cnode;
70 EXPORT_SYMBOL(__prom_getsibling);
72 /* Return the next sibling of node 'node' or zero if no more siblings
73 * at this level of depth in the tree.
75 int prom_getsibling(int node)
77 int sibnode;
79 if (node == -1)
80 return 0;
82 sibnode = __prom_getsibling(node);
83 if (sibnode == 0 || sibnode == -1)
84 return 0;
86 return sibnode;
88 EXPORT_SYMBOL(prom_getsibling);
90 /* Return the length in bytes of property 'prop' at node 'node'.
91 * Return -1 on error.
93 int prom_getproplen(int node, const char *prop)
95 int ret;
96 unsigned long flags;
98 if((!node) || (!prop))
99 return -1;
101 spin_lock_irqsave(&prom_lock, flags);
102 ret = prom_nodeops->no_proplen(node, prop);
103 restore_current();
104 spin_unlock_irqrestore(&prom_lock, flags);
105 return ret;
107 EXPORT_SYMBOL(prom_getproplen);
109 /* Acquire a property 'prop' at node 'node' and place it in
110 * 'buffer' which has a size of 'bufsize'. If the acquisition
111 * was successful the length will be returned, else -1 is returned.
113 int prom_getproperty(int node, const char *prop, char *buffer, int bufsize)
115 int plen, ret;
116 unsigned long flags;
118 plen = prom_getproplen(node, prop);
119 if((plen > bufsize) || (plen == 0) || (plen == -1))
120 return -1;
121 /* Ok, things seem all right. */
122 spin_lock_irqsave(&prom_lock, flags);
123 ret = prom_nodeops->no_getprop(node, prop, buffer);
124 restore_current();
125 spin_unlock_irqrestore(&prom_lock, flags);
126 return ret;
128 EXPORT_SYMBOL(prom_getproperty);
130 /* Acquire an integer property and return its value. Returns -1
131 * on failure.
133 int prom_getint(int node, char *prop)
135 static int intprop;
137 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
138 return intprop;
140 return -1;
142 EXPORT_SYMBOL(prom_getint);
144 /* Acquire an integer property, upon error return the passed default
145 * integer.
147 int prom_getintdefault(int node, char *property, int deflt)
149 int retval;
151 retval = prom_getint(node, property);
152 if(retval == -1) return deflt;
154 return retval;
156 EXPORT_SYMBOL(prom_getintdefault);
158 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
159 int prom_getbool(int node, char *prop)
161 int retval;
163 retval = prom_getproplen(node, prop);
164 if(retval == -1) return 0;
165 return 1;
167 EXPORT_SYMBOL(prom_getbool);
169 /* Acquire a property whose value is a string, returns a null
170 * string on error. The char pointer is the user supplied string
171 * buffer.
173 void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
175 int len;
177 len = prom_getproperty(node, prop, user_buf, ubuf_size);
178 if(len != -1) return;
179 user_buf[0] = 0;
180 return;
182 EXPORT_SYMBOL(prom_getstring);
185 /* Does the device at node 'node' have name 'name'?
186 * YES = 1 NO = 0
188 int prom_nodematch(int node, char *name)
190 int error;
192 static char namebuf[128];
193 error = prom_getproperty(node, "name", namebuf, sizeof(namebuf));
194 if (error == -1) return 0;
195 if(strcmp(namebuf, name) == 0) return 1;
196 return 0;
199 /* Search siblings at 'node_start' for a node with name
200 * 'nodename'. Return node if successful, zero if not.
202 int prom_searchsiblings(int node_start, char *nodename)
205 int thisnode, error;
207 for(thisnode = node_start; thisnode;
208 thisnode=prom_getsibling(thisnode)) {
209 error = prom_getproperty(thisnode, "name", promlib_buf,
210 sizeof(promlib_buf));
211 /* Should this ever happen? */
212 if(error == -1) continue;
213 if(strcmp(nodename, promlib_buf)==0) return thisnode;
216 return 0;
218 EXPORT_SYMBOL(prom_searchsiblings);
220 /* Interal version of nextprop that does not alter return values. */
221 char * __prom_nextprop(int node, char * oprop)
223 unsigned long flags;
224 char *prop;
226 spin_lock_irqsave(&prom_lock, flags);
227 prop = prom_nodeops->no_nextprop(node, oprop);
228 restore_current();
229 spin_unlock_irqrestore(&prom_lock, flags);
231 return prop;
234 /* Return the first property name for node 'node'. */
235 /* buffer is unused argument, but as v9 uses it, we need to have the same interface */
236 char * prom_firstprop(int node, char *bufer)
238 if (node == 0 || node == -1)
239 return "";
241 return __prom_nextprop(node, "");
243 EXPORT_SYMBOL(prom_firstprop);
245 /* Return the property type string after property type 'oprop'
246 * at node 'node' . Returns empty string if no more
247 * property types for this node.
249 char * prom_nextprop(int node, char *oprop, char *buffer)
251 if (node == 0 || node == -1)
252 return "";
254 return __prom_nextprop(node, oprop);
256 EXPORT_SYMBOL(prom_nextprop);
258 int prom_finddevice(char *name)
260 char nbuf[128];
261 char *s = name, *d;
262 int node = prom_root_node, node2;
263 unsigned int which_io, phys_addr;
264 struct linux_prom_registers reg[PROMREG_MAX];
266 while (*s++) {
267 if (!*s) return node; /* path '.../' is legal */
268 node = prom_getchild(node);
270 for (d = nbuf; *s != 0 && *s != '@' && *s != '/';)
271 *d++ = *s++;
272 *d = 0;
274 node = prom_searchsiblings(node, nbuf);
275 if (!node)
276 return 0;
278 if (*s == '@') {
279 if (isxdigit(s[1]) && s[2] == ',') {
280 which_io = simple_strtoul(s+1, NULL, 16);
281 phys_addr = simple_strtoul(s+3, &d, 16);
282 if (d != s + 3 && (!*d || *d == '/')
283 && d <= s + 3 + 8) {
284 node2 = node;
285 while (node2 && node2 != -1) {
286 if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) {
287 if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) {
288 node = node2;
289 break;
292 node2 = prom_getsibling(node2);
293 if (!node2 || node2 == -1)
294 break;
295 node2 = prom_searchsiblings(prom_getsibling(node2), nbuf);
299 while (*s != 0 && *s != '/') s++;
302 return node;
304 EXPORT_SYMBOL(prom_finddevice);
306 int prom_node_has_property(int node, char *prop)
308 char *current_property = "";
310 do {
311 current_property = prom_nextprop(node, current_property, NULL);
312 if(!strcmp(current_property, prop))
313 return 1;
314 } while (*current_property);
315 return 0;
317 EXPORT_SYMBOL(prom_node_has_property);
319 /* Set property 'pname' at node 'node' to value 'value' which has a length
320 * of 'size' bytes. Return the number of bytes the prom accepted.
322 int prom_setprop(int node, const char *pname, char *value, int size)
324 unsigned long flags;
325 int ret;
327 if(size == 0) return 0;
328 if((pname == 0) || (value == 0)) return 0;
329 spin_lock_irqsave(&prom_lock, flags);
330 ret = prom_nodeops->no_setprop(node, pname, value, size);
331 restore_current();
332 spin_unlock_irqrestore(&prom_lock, flags);
333 return ret;
335 EXPORT_SYMBOL(prom_setprop);
337 int prom_inst2pkg(int inst)
339 int node;
340 unsigned long flags;
342 spin_lock_irqsave(&prom_lock, flags);
343 node = (*romvec->pv_v2devops.v2_inst2pkg)(inst);
344 restore_current();
345 spin_unlock_irqrestore(&prom_lock, flags);
346 if (node == -1) return 0;
347 return node;
350 /* Return 'node' assigned to a particular prom 'path'
351 * FIXME: Should work for v0 as well
353 int prom_pathtoinode(char *path)
355 int node, inst;
357 inst = prom_devopen (path);
358 if (inst == -1) return 0;
359 node = prom_inst2pkg (inst);
360 prom_devclose (inst);
361 if (node == -1) return 0;
362 return node;