1 // SPDX-License-Identifier: GPL-2.0
3 * tree.c: Basic device tree traversal/scanning for the Linux
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/module.h>
16 #include <asm/openprom.h>
17 #include <asm/oplib.h>
20 static phandle prom_node_to_node(const char *type, phandle node)
22 unsigned long args[5];
24 args[0] = (unsigned long) type;
27 args[3] = (unsigned int) node;
28 args[4] = (unsigned long) -1;
30 p1275_cmd_direct(args);
32 return (phandle) args[4];
35 /* Return the child of node 'node' or zero if no this node has no
38 inline phandle __prom_getchild(phandle node)
40 return prom_node_to_node("child", node);
43 phandle prom_getchild(phandle node)
49 cnode = __prom_getchild(node);
54 EXPORT_SYMBOL(prom_getchild);
56 inline phandle prom_getparent(phandle node)
62 cnode = prom_node_to_node("parent", node);
68 /* Return the next sibling of node 'node' or zero if no more siblings
69 * at this level of depth in the tree.
71 inline phandle __prom_getsibling(phandle node)
73 return prom_node_to_node(prom_peer_name, node);
76 phandle prom_getsibling(phandle node)
82 sibnode = __prom_getsibling(node);
83 if ((s32)sibnode == -1)
88 EXPORT_SYMBOL(prom_getsibling);
90 /* Return the length in bytes of property 'prop' at node 'node'.
93 int prom_getproplen(phandle node, const char *prop)
95 unsigned long args[6];
100 args[0] = (unsigned long) "getproplen";
103 args[3] = (unsigned int) node;
104 args[4] = (unsigned long) prop;
105 args[5] = (unsigned long) -1;
107 p1275_cmd_direct(args);
109 return (int) args[5];
111 EXPORT_SYMBOL(prom_getproplen);
113 /* Acquire a property 'prop' at node 'node' and place it in
114 * 'buffer' which has a size of 'bufsize'. If the acquisition
115 * was successful the length will be returned, else -1 is returned.
117 int prom_getproperty(phandle node, const char *prop,
118 char *buffer, int bufsize)
120 unsigned long args[8];
123 plen = prom_getproplen(node, prop);
124 if ((plen > bufsize) || (plen == 0) || (plen == -1))
127 args[0] = (unsigned long) prom_getprop_name;
130 args[3] = (unsigned int) node;
131 args[4] = (unsigned long) prop;
132 args[5] = (unsigned long) buffer;
134 args[7] = (unsigned long) -1;
136 p1275_cmd_direct(args);
138 return (int) args[7];
140 EXPORT_SYMBOL(prom_getproperty);
142 /* Acquire an integer property and return its value. Returns -1
145 int prom_getint(phandle node, const char *prop)
149 if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
154 EXPORT_SYMBOL(prom_getint);
156 /* Acquire an integer property, upon error return the passed default
160 int prom_getintdefault(phandle node, const char *property, int deflt)
164 retval = prom_getint(node, property);
170 EXPORT_SYMBOL(prom_getintdefault);
172 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
173 int prom_getbool(phandle node, const char *prop)
177 retval = prom_getproplen(node, prop);
182 EXPORT_SYMBOL(prom_getbool);
184 /* Acquire a property whose value is a string, returns a null
185 * string on error. The char pointer is the user supplied string
188 void prom_getstring(phandle node, const char *prop, char *user_buf,
193 len = prom_getproperty(node, prop, user_buf, ubuf_size);
198 EXPORT_SYMBOL(prom_getstring);
200 /* Does the device at node 'node' have name 'name'?
203 int prom_nodematch(phandle node, const char *name)
206 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
207 if (strcmp(namebuf, name) == 0)
212 /* Search siblings at 'node_start' for a node with name
213 * 'nodename'. Return node if successful, zero if not.
215 phandle prom_searchsiblings(phandle node_start, const char *nodename)
219 char promlib_buf[128];
221 for(thisnode = node_start; thisnode;
222 thisnode=prom_getsibling(thisnode)) {
223 error = prom_getproperty(thisnode, "name", promlib_buf,
224 sizeof(promlib_buf));
225 /* Should this ever happen? */
226 if(error == -1) continue;
227 if(strcmp(nodename, promlib_buf)==0) return thisnode;
232 EXPORT_SYMBOL(prom_searchsiblings);
234 static const char *prom_nextprop_name = "nextprop";
236 /* Return the first property type for node 'node'.
237 * buffer should be at least 32B in length
239 char *prom_firstprop(phandle node, char *buffer)
241 unsigned long args[7];
247 args[0] = (unsigned long) prom_nextprop_name;
250 args[3] = (unsigned int) node;
252 args[5] = (unsigned long) buffer;
253 args[6] = (unsigned long) -1;
255 p1275_cmd_direct(args);
259 EXPORT_SYMBOL(prom_firstprop);
261 /* Return the property type string after property type 'oprop'
262 * at node 'node' . Returns NULL string if no more
263 * property types for this node.
265 char *prom_nextprop(phandle node, const char *oprop, char *buffer)
267 unsigned long args[7];
270 if ((s32)node == -1) {
274 if (oprop == buffer) {
279 args[0] = (unsigned long) prom_nextprop_name;
282 args[3] = (unsigned int) node;
283 args[4] = (unsigned long) oprop;
284 args[5] = (unsigned long) buffer;
285 args[6] = (unsigned long) -1;
287 p1275_cmd_direct(args);
291 EXPORT_SYMBOL(prom_nextprop);
293 phandle prom_finddevice(const char *name)
295 unsigned long args[5];
299 args[0] = (unsigned long) "finddevice";
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(phandle node, const char *prop)
317 prom_nextprop(node, buf, buf);
318 if (!strcmp(buf, prop))
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(phandle node, const char *pname, char *value, int size)
331 unsigned long args[8];
335 if ((pname == 0) || (value == 0))
338 #ifdef CONFIG_SUN_LDOMS
339 if (ldom_domaining_enabled) {
340 ldom_set_var(pname, value);
344 args[0] = (unsigned long) "setprop";
347 args[3] = (unsigned int) node;
348 args[4] = (unsigned long) pname;
349 args[5] = (unsigned long) value;
351 args[7] = (unsigned long) -1;
353 p1275_cmd_direct(args);
355 return (int) args[7];
357 EXPORT_SYMBOL(prom_setprop);
359 inline phandle prom_inst2pkg(int inst)
361 unsigned long args[5];
364 args[0] = (unsigned long) "instance-to-package";
367 args[3] = (unsigned int) inst;
368 args[4] = (unsigned long) -1;
370 p1275_cmd_direct(args);
372 node = (int) args[4];
378 int prom_ihandle2path(int handle, char *buffer, int bufsize)
380 unsigned long args[7];
382 args[0] = (unsigned long) "instance-to-path";
385 args[3] = (unsigned int) handle;
386 args[4] = (unsigned long) buffer;
388 args[6] = (unsigned long) -1;
390 p1275_cmd_direct(args);
392 return (int) args[6];