2 * tree.c: Basic device tree traversal/scanning for the Linux
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
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>
19 /* Return the child of node 'node' or zero if no this node has no
22 inline int __prom_getchild(int node)
24 return p1275_cmd ("child", P1275_INOUT(1, 1), node);
26 EXPORT_SYMBOL(__prom_getchild);
28 inline int prom_getchild(int node)
32 if(node == -1) return 0;
33 cnode = __prom_getchild(node);
34 if(cnode == -1) return 0;
37 EXPORT_SYMBOL(prom_getchild);
39 inline int prom_getparent(int node)
43 if(node == -1) return 0;
44 cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
45 if(cnode == -1) return 0;
49 /* Return the next sibling of node 'node' or zero if no more siblings
50 * at this level of depth in the tree.
52 inline int __prom_getsibling(int node)
54 return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
56 EXPORT_SYMBOL(__prom_getsibling);
58 inline int prom_getsibling(int node)
64 sibnode = __prom_getsibling(node);
70 EXPORT_SYMBOL(prom_getsibling);
72 /* Return the length in bytes of property 'prop' at node 'node'.
75 inline int prom_getproplen(int node, const char *prop)
77 if((!node) || (!prop)) return -1;
78 return p1275_cmd ("getproplen",
79 P1275_ARG(1,P1275_ARG_IN_STRING)|
83 EXPORT_SYMBOL(prom_getproplen);
85 /* Acquire a property 'prop' at node 'node' and place it in
86 * 'buffer' which has a size of 'bufsize'. If the acquisition
87 * was successful the length will be returned, else -1 is returned.
89 inline int prom_getproperty(int node, const char *prop,
90 char *buffer, int bufsize)
94 plen = prom_getproplen(node, prop);
95 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
98 /* Ok, things seem all right. */
99 return p1275_cmd(prom_getprop_name,
100 P1275_ARG(1,P1275_ARG_IN_STRING)|
101 P1275_ARG(2,P1275_ARG_OUT_BUF)|
103 node, prop, buffer, P1275_SIZE(plen));
106 EXPORT_SYMBOL(prom_getproperty);
108 /* Acquire an integer property and return its value. Returns -1
111 inline int prom_getint(int node, const char *prop)
115 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
120 EXPORT_SYMBOL(prom_getint);
122 /* Acquire an integer property, upon error return the passed default
126 int prom_getintdefault(int node, const char *property, int deflt)
130 retval = prom_getint(node, property);
131 if(retval == -1) return deflt;
135 EXPORT_SYMBOL(prom_getintdefault);
137 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
138 int prom_getbool(int node, const char *prop)
142 retval = prom_getproplen(node, prop);
143 if(retval == -1) return 0;
146 EXPORT_SYMBOL(prom_getbool);
148 /* Acquire a property whose value is a string, returns a null
149 * string on error. The char pointer is the user supplied string
152 void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
156 len = prom_getproperty(node, prop, user_buf, ubuf_size);
157 if(len != -1) return;
161 EXPORT_SYMBOL(prom_getstring);
163 /* Does the device at node 'node' have name 'name'?
166 int prom_nodematch(int node, const char *name)
169 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
170 if(strcmp(namebuf, name) == 0) return 1;
174 /* Search siblings at 'node_start' for a node with name
175 * 'nodename'. Return node if successful, zero if not.
177 int prom_searchsiblings(int node_start, const char *nodename)
181 char promlib_buf[128];
183 for(thisnode = node_start; thisnode;
184 thisnode=prom_getsibling(thisnode)) {
185 error = prom_getproperty(thisnode, "name", promlib_buf,
186 sizeof(promlib_buf));
187 /* Should this ever happen? */
188 if(error == -1) continue;
189 if(strcmp(nodename, promlib_buf)==0) return thisnode;
194 EXPORT_SYMBOL(prom_searchsiblings);
196 /* Return the first property type for node 'node'.
197 * buffer should be at least 32B in length
199 inline char *prom_firstprop(int node, char *buffer)
202 if(node == -1) return buffer;
203 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
205 node, (char *) 0x0, buffer);
208 EXPORT_SYMBOL(prom_firstprop);
210 /* Return the property type string after property type 'oprop'
211 * at node 'node' . Returns NULL string if no more
212 * property types for this node.
214 inline char *prom_nextprop(int node, const char *oprop, char *buffer)
222 if (oprop == buffer) {
226 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
227 P1275_ARG(2,P1275_ARG_OUT_32B)|
229 node, oprop, buffer);
232 EXPORT_SYMBOL(prom_nextprop);
235 prom_finddevice(const char *name)
239 return p1275_cmd(prom_finddev_name,
240 P1275_ARG(0,P1275_ARG_IN_STRING)|
244 EXPORT_SYMBOL(prom_finddevice);
246 int prom_node_has_property(int node, const char *prop)
252 prom_nextprop(node, buf, buf);
253 if(!strcmp(buf, prop))
258 EXPORT_SYMBOL(prom_node_has_property);
260 /* Set property 'pname' at node 'node' to value 'value' which has a length
261 * of 'size' bytes. Return the number of bytes the prom accepted.
264 prom_setprop(int node, const char *pname, char *value, int size)
268 if ((pname == 0) || (value == 0))
271 #ifdef CONFIG_SUN_LDOMS
272 if (ldom_domaining_enabled) {
273 ldom_set_var(pname, value);
277 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
278 P1275_ARG(2,P1275_ARG_IN_BUF)|
280 node, pname, value, P1275_SIZE(size));
282 EXPORT_SYMBOL(prom_setprop);
284 inline int prom_inst2pkg(int inst)
288 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
289 if (node == -1) return 0;
293 /* Return 'node' assigned to a particular prom 'path'
294 * FIXME: Should work for v0 as well
297 prom_pathtoinode(const char *path)
301 inst = prom_devopen (path);
302 if (inst == 0) return 0;
303 node = prom_inst2pkg (inst);
304 prom_devclose (inst);
305 if (node == -1) return 0;
309 int prom_ihandle2path(int handle, char *buffer, int bufsize)
311 return p1275_cmd("instance-to-path",
312 P1275_ARG(1,P1275_ARG_OUT_BUF)|
314 handle, buffer, P1275_SIZE(bufsize));