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>
14 #include <asm/openprom.h>
15 #include <asm/oplib.h>
18 /* Return the child of node 'node' or zero if no this node has no
21 inline int __prom_getchild(int node)
23 return p1275_cmd ("child", P1275_INOUT(1, 1), node);
26 inline int prom_getchild(int node)
30 if(node == -1) return 0;
31 cnode = __prom_getchild(node);
32 if(cnode == -1) return 0;
36 inline int prom_getparent(int node)
40 if(node == -1) return 0;
41 cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
42 if(cnode == -1) return 0;
46 /* Return the next sibling of node 'node' or zero if no more siblings
47 * at this level of depth in the tree.
49 inline int __prom_getsibling(int node)
51 return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
54 inline int prom_getsibling(int node)
60 sibnode = __prom_getsibling(node);
67 /* Return the length in bytes of property 'prop' at node 'node'.
70 inline int prom_getproplen(int node, const char *prop)
72 if((!node) || (!prop)) return -1;
73 return p1275_cmd ("getproplen",
74 P1275_ARG(1,P1275_ARG_IN_STRING)|
79 /* Acquire a property 'prop' at node 'node' and place it in
80 * 'buffer' which has a size of 'bufsize'. If the acquisition
81 * was successful the length will be returned, else -1 is returned.
83 inline int prom_getproperty(int node, const char *prop,
84 char *buffer, int bufsize)
88 plen = prom_getproplen(node, prop);
89 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
92 /* Ok, things seem all right. */
93 return p1275_cmd(prom_getprop_name,
94 P1275_ARG(1,P1275_ARG_IN_STRING)|
95 P1275_ARG(2,P1275_ARG_OUT_BUF)|
97 node, prop, buffer, P1275_SIZE(plen));
101 /* Acquire an integer property and return its value. Returns -1
104 inline int prom_getint(int node, const char *prop)
108 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
114 /* Acquire an integer property, upon error return the passed default
118 int prom_getintdefault(int node, const char *property, int deflt)
122 retval = prom_getint(node, property);
123 if(retval == -1) return deflt;
128 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
129 int prom_getbool(int node, const char *prop)
133 retval = prom_getproplen(node, prop);
134 if(retval == -1) return 0;
138 /* Acquire a property whose value is a string, returns a null
139 * string on error. The char pointer is the user supplied string
142 void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
146 len = prom_getproperty(node, prop, user_buf, ubuf_size);
147 if(len != -1) return;
153 /* Does the device at node 'node' have name 'name'?
156 int prom_nodematch(int node, const char *name)
159 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
160 if(strcmp(namebuf, name) == 0) return 1;
164 /* Search siblings at 'node_start' for a node with name
165 * 'nodename'. Return node if successful, zero if not.
167 int prom_searchsiblings(int node_start, const char *nodename)
171 char promlib_buf[128];
173 for(thisnode = node_start; thisnode;
174 thisnode=prom_getsibling(thisnode)) {
175 error = prom_getproperty(thisnode, "name", promlib_buf,
176 sizeof(promlib_buf));
177 /* Should this ever happen? */
178 if(error == -1) continue;
179 if(strcmp(nodename, promlib_buf)==0) return thisnode;
185 /* Return the first property type for node 'node'.
186 * buffer should be at least 32B in length
188 inline char *prom_firstprop(int node, char *buffer)
191 if(node == -1) return buffer;
192 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
194 node, (char *) 0x0, buffer);
198 /* Return the property type string after property type 'oprop'
199 * at node 'node' . Returns NULL string if no more
200 * property types for this node.
202 inline char *prom_nextprop(int node, const char *oprop, char *buffer)
210 if (oprop == buffer) {
214 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
215 P1275_ARG(2,P1275_ARG_OUT_32B)|
217 node, oprop, buffer);
222 prom_finddevice(const char *name)
226 return p1275_cmd(prom_finddev_name,
227 P1275_ARG(0,P1275_ARG_IN_STRING)|
232 int prom_node_has_property(int node, const char *prop)
238 prom_nextprop(node, buf, buf);
239 if(!strcmp(buf, prop))
245 /* Set property 'pname' at node 'node' to value 'value' which has a length
246 * of 'size' bytes. Return the number of bytes the prom accepted.
249 prom_setprop(int node, const char *pname, char *value, int size)
253 if ((pname == 0) || (value == 0))
256 #ifdef CONFIG_SUN_LDOMS
257 if (ldom_domaining_enabled) {
258 ldom_set_var(pname, value);
262 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
263 P1275_ARG(2,P1275_ARG_IN_BUF)|
265 node, pname, value, P1275_SIZE(size));
268 inline int prom_inst2pkg(int inst)
272 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
273 if (node == -1) return 0;
277 /* Return 'node' assigned to a particular prom 'path'
278 * FIXME: Should work for v0 as well
281 prom_pathtoinode(const char *path)
285 inst = prom_devopen (path);
286 if (inst == 0) return 0;
287 node = prom_inst2pkg (inst);
288 prom_devclose (inst);
289 if (node == -1) return 0;
293 int prom_ihandle2path(int handle, char *buffer, int bufsize)
295 return p1275_cmd("instance-to-path",
296 P1275_ARG(1,P1275_ARG_OUT_BUF)|
298 handle, buffer, P1275_SIZE(bufsize));