initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / arch / sparc / prom / tree_64.c
blob6a05c76f58fdec24c135f6734ec5aaecc971b0a3
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 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/module.h>
15 #include <asm/openprom.h>
16 #include <asm/oplib.h>
17 #include <asm/ldc.h>
19 static int prom_node_to_node(const char *type, int node)
21 unsigned long args[5];
23 args[0] = (unsigned long) type;
24 args[1] = 1;
25 args[2] = 1;
26 args[3] = (unsigned int) node;
27 args[4] = (unsigned long) -1;
29 p1275_cmd_direct(args);
31 return (int) args[4];
34 /* Return the child of node 'node' or zero if no this node has no
35 * direct descendent.
37 inline int __prom_getchild(int node)
39 return prom_node_to_node("child", node);
42 inline int prom_getchild(int node)
44 int cnode;
46 if (node == -1)
47 return 0;
48 cnode = __prom_getchild(node);
49 if (cnode == -1)
50 return 0;
51 return cnode;
53 EXPORT_SYMBOL(prom_getchild);
55 inline int prom_getparent(int node)
57 int cnode;
59 if (node == -1)
60 return 0;
61 cnode = prom_node_to_node("parent", node);
62 if (cnode == -1)
63 return 0;
64 return cnode;
67 /* Return the next sibling of node 'node' or zero if no more siblings
68 * at this level of depth in the tree.
70 inline int __prom_getsibling(int node)
72 return prom_node_to_node(prom_peer_name, node);
75 inline int prom_getsibling(int node)
77 int sibnode;
79 if (node == -1)
80 return 0;
81 sibnode = __prom_getsibling(node);
82 if (sibnode == -1)
83 return 0;
85 return sibnode;
87 EXPORT_SYMBOL(prom_getsibling);
89 /* Return the length in bytes of property 'prop' at node 'node'.
90 * Return -1 on error.
92 inline int prom_getproplen(int node, const char *prop)
94 unsigned long args[6];
96 if (!node || !prop)
97 return -1;
99 args[0] = (unsigned long) "getproplen";
100 args[1] = 2;
101 args[2] = 1;
102 args[3] = (unsigned int) node;
103 args[4] = (unsigned long) prop;
104 args[5] = (unsigned long) -1;
106 p1275_cmd_direct(args);
108 return (int) args[5];
110 EXPORT_SYMBOL(prom_getproplen);
112 /* Acquire a property 'prop' at node 'node' and place it in
113 * 'buffer' which has a size of 'bufsize'. If the acquisition
114 * was successful the length will be returned, else -1 is returned.
116 inline int prom_getproperty(int node, const char *prop,
117 char *buffer, int bufsize)
119 unsigned long args[8];
120 int plen;
122 plen = prom_getproplen(node, prop);
123 if ((plen > bufsize) || (plen == 0) || (plen == -1))
124 return -1;
126 args[0] = (unsigned long) prom_getprop_name;
127 args[1] = 4;
128 args[2] = 1;
129 args[3] = (unsigned int) node;
130 args[4] = (unsigned long) prop;
131 args[5] = (unsigned long) buffer;
132 args[6] = bufsize;
133 args[7] = (unsigned long) -1;
135 p1275_cmd_direct(args);
137 return (int) args[7];
139 EXPORT_SYMBOL(prom_getproperty);
141 /* Acquire an integer property and return its value. Returns -1
142 * on failure.
144 inline int prom_getint(int node, const char *prop)
146 int intprop;
148 if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
149 return intprop;
151 return -1;
153 EXPORT_SYMBOL(prom_getint);
155 /* Acquire an integer property, upon error return the passed default
156 * integer.
159 int prom_getintdefault(int node, const char *property, int deflt)
161 int retval;
163 retval = prom_getint(node, property);
164 if (retval == -1)
165 return deflt;
167 return retval;
169 EXPORT_SYMBOL(prom_getintdefault);
171 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
172 int prom_getbool(int node, const char *prop)
174 int retval;
176 retval = prom_getproplen(node, prop);
177 if (retval == -1)
178 return 0;
179 return 1;
181 EXPORT_SYMBOL(prom_getbool);
183 /* Acquire a property whose value is a string, returns a null
184 * string on error. The char pointer is the user supplied string
185 * buffer.
187 void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
189 int len;
191 len = prom_getproperty(node, prop, user_buf, ubuf_size);
192 if (len != -1)
193 return;
194 user_buf[0] = 0;
195 return;
197 EXPORT_SYMBOL(prom_getstring);
199 /* Does the device at node 'node' have name 'name'?
200 * YES = 1 NO = 0
202 int prom_nodematch(int node, const char *name)
204 char namebuf[128];
205 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
206 if (strcmp(namebuf, name) == 0)
207 return 1;
208 return 0;
211 /* Search siblings at 'node_start' for a node with name
212 * 'nodename'. Return node if successful, zero if not.
214 int prom_searchsiblings(int node_start, const char *nodename)
217 int thisnode, error;
218 char promlib_buf[128];
220 for(thisnode = node_start; thisnode;
221 thisnode=prom_getsibling(thisnode)) {
222 error = prom_getproperty(thisnode, "name", promlib_buf,
223 sizeof(promlib_buf));
224 /* Should this ever happen? */
225 if(error == -1) continue;
226 if(strcmp(nodename, promlib_buf)==0) return thisnode;
229 return 0;
231 EXPORT_SYMBOL(prom_searchsiblings);
233 static const char *prom_nextprop_name = "nextprop";
235 /* Return the first property type for node 'node'.
236 * buffer should be at least 32B in length
238 inline char *prom_firstprop(int node, char *buffer)
240 unsigned long args[7];
242 *buffer = 0;
243 if (node == -1)
244 return buffer;
246 args[0] = (unsigned long) prom_nextprop_name;
247 args[1] = 3;
248 args[2] = 1;
249 args[3] = (unsigned int) node;
250 args[4] = 0;
251 args[5] = (unsigned long) buffer;
252 args[6] = (unsigned long) -1;
254 p1275_cmd_direct(args);
256 return buffer;
258 EXPORT_SYMBOL(prom_firstprop);
260 /* Return the property type string after property type 'oprop'
261 * at node 'node' . Returns NULL string if no more
262 * property types for this node.
264 inline char *prom_nextprop(int node, const char *oprop, char *buffer)
266 unsigned long args[7];
267 char buf[32];
269 if (node == -1) {
270 *buffer = 0;
271 return buffer;
273 if (oprop == buffer) {
274 strcpy (buf, oprop);
275 oprop = buf;
278 args[0] = (unsigned long) prom_nextprop_name;
279 args[1] = 3;
280 args[2] = 1;
281 args[3] = (unsigned int) node;
282 args[4] = (unsigned long) oprop;
283 args[5] = (unsigned long) buffer;
284 args[6] = (unsigned long) -1;
286 p1275_cmd_direct(args);
288 return buffer;
290 EXPORT_SYMBOL(prom_nextprop);
293 prom_finddevice(const char *name)
295 unsigned long args[5];
297 if (!name)
298 return 0;
299 args[0] = (unsigned long) "finddevice";
300 args[1] = 1;
301 args[2] = 1;
302 args[3] = (unsigned long) name;
303 args[4] = (unsigned long) -1;
305 p1275_cmd_direct(args);
307 return (int) args[4];
309 EXPORT_SYMBOL(prom_finddevice);
311 int prom_node_has_property(int node, const char *prop)
313 char buf [32];
315 *buf = 0;
316 do {
317 prom_nextprop(node, buf, buf);
318 if (!strcmp(buf, prop))
319 return 1;
320 } while (*buf);
321 return 0;
323 EXPORT_SYMBOL(prom_node_has_property);
325 /* Set property 'pname' at node 'node' to value 'value' which has a length
326 * of 'size' bytes. Return the number of bytes the prom accepted.
329 prom_setprop(int node, const char *pname, char *value, int size)
331 unsigned long args[8];
333 if (size == 0)
334 return 0;
335 if ((pname == 0) || (value == 0))
336 return 0;
338 #ifdef CONFIG_SUN_LDOMS
339 if (ldom_domaining_enabled) {
340 ldom_set_var(pname, value);
341 return 0;
343 #endif
344 args[0] = (unsigned long) "setprop";
345 args[1] = 4;
346 args[2] = 1;
347 args[3] = (unsigned int) node;
348 args[4] = (unsigned long) pname;
349 args[5] = (unsigned long) value;
350 args[6] = size;
351 args[7] = (unsigned long) -1;
353 p1275_cmd_direct(args);
355 return (int) args[7];
357 EXPORT_SYMBOL(prom_setprop);
359 inline int prom_inst2pkg(int inst)
361 unsigned long args[5];
362 int node;
364 args[0] = (unsigned long) "instance-to-package";
365 args[1] = 1;
366 args[2] = 1;
367 args[3] = (unsigned int) inst;
368 args[4] = (unsigned long) -1;
370 p1275_cmd_direct(args);
372 node = (int) args[4];
373 if (node == -1)
374 return 0;
375 return node;
378 /* Return 'node' assigned to a particular prom 'path'
379 * FIXME: Should work for v0 as well
382 prom_pathtoinode(const char *path)
384 int node, inst;
386 inst = prom_devopen (path);
387 if (inst == 0)
388 return 0;
389 node = prom_inst2pkg(inst);
390 prom_devclose(inst);
391 if (node == -1)
392 return 0;
393 return node;
396 int prom_ihandle2path(int handle, char *buffer, int bufsize)
398 unsigned long args[7];
400 args[0] = (unsigned long) "instance-to-path";
401 args[1] = 3;
402 args[2] = 1;
403 args[3] = (unsigned int) handle;
404 args[4] = (unsigned long) buffer;
405 args[5] = bufsize;
406 args[6] = (unsigned long) -1;
408 p1275_cmd_direct(args);
410 return (int) args[6];