1 // SPDX-License-Identifier: GPL-2.0
3 * tree.c: Basic device tree traversal/scanning for the Linux
6 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/ctype.h>
14 #include <linux/module.h>
16 #include <asm/openprom.h>
17 #include <asm/oplib.h>
19 extern void restore_current(void);
21 static char promlib_buf
[128];
23 /* Internal version of prom_getchild that does not alter return values. */
24 static phandle
__prom_getchild(phandle node
)
29 spin_lock_irqsave(&prom_lock
, flags
);
30 cnode
= prom_nodeops
->no_child(node
);
32 spin_unlock_irqrestore(&prom_lock
, flags
);
37 /* Return the child of node 'node' or zero if no this node has no
40 phandle
prom_getchild(phandle node
)
47 cnode
= __prom_getchild(node
);
48 if (cnode
== 0 || (s32
)cnode
== -1)
53 EXPORT_SYMBOL(prom_getchild
);
55 /* Internal version of prom_getsibling that does not alter return values. */
56 static phandle
__prom_getsibling(phandle node
)
61 spin_lock_irqsave(&prom_lock
, flags
);
62 cnode
= prom_nodeops
->no_nextnode(node
);
64 spin_unlock_irqrestore(&prom_lock
, flags
);
69 /* Return the next sibling of node 'node' or zero if no more siblings
70 * at this level of depth in the tree.
72 phandle
prom_getsibling(phandle node
)
79 sibnode
= __prom_getsibling(node
);
80 if (sibnode
== 0 || (s32
)sibnode
== -1)
85 EXPORT_SYMBOL(prom_getsibling
);
87 /* Return the length in bytes of property 'prop' at node 'node'.
90 int prom_getproplen(phandle node
, const char *prop
)
95 if((!node
) || (!prop
))
98 spin_lock_irqsave(&prom_lock
, flags
);
99 ret
= prom_nodeops
->no_proplen(node
, prop
);
101 spin_unlock_irqrestore(&prom_lock
, flags
);
104 EXPORT_SYMBOL(prom_getproplen
);
106 /* Acquire a property 'prop' at node 'node' and place it in
107 * 'buffer' which has a size of 'bufsize'. If the acquisition
108 * was successful the length will be returned, else -1 is returned.
110 int prom_getproperty(phandle node
, const char *prop
, char *buffer
, int bufsize
)
115 plen
= prom_getproplen(node
, prop
);
116 if((plen
> bufsize
) || (plen
== 0) || (plen
== -1))
118 /* Ok, things seem all right. */
119 spin_lock_irqsave(&prom_lock
, flags
);
120 ret
= prom_nodeops
->no_getprop(node
, prop
, buffer
);
122 spin_unlock_irqrestore(&prom_lock
, flags
);
125 EXPORT_SYMBOL(prom_getproperty
);
127 /* Acquire an integer property and return its value. Returns -1
130 int prom_getint(phandle node
, char *prop
)
134 if(prom_getproperty(node
, prop
, (char *) &intprop
, sizeof(int)) != -1)
139 EXPORT_SYMBOL(prom_getint
);
141 /* Acquire an integer property, upon error return the passed default
144 int prom_getintdefault(phandle node
, char *property
, int deflt
)
148 retval
= prom_getint(node
, property
);
149 if(retval
== -1) return deflt
;
153 EXPORT_SYMBOL(prom_getintdefault
);
155 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
156 int prom_getbool(phandle node
, char *prop
)
160 retval
= prom_getproplen(node
, prop
);
161 if(retval
== -1) return 0;
164 EXPORT_SYMBOL(prom_getbool
);
166 /* Acquire a property whose value is a string, returns a null
167 * string on error. The char pointer is the user supplied string
170 void prom_getstring(phandle node
, char *prop
, char *user_buf
, int ubuf_size
)
174 len
= prom_getproperty(node
, prop
, user_buf
, ubuf_size
);
175 if(len
!= -1) return;
178 EXPORT_SYMBOL(prom_getstring
);
181 /* Search siblings at 'node_start' for a node with name
182 * 'nodename'. Return node if successful, zero if not.
184 phandle
prom_searchsiblings(phandle node_start
, char *nodename
)
190 for(thisnode
= node_start
; thisnode
;
191 thisnode
=prom_getsibling(thisnode
)) {
192 error
= prom_getproperty(thisnode
, "name", promlib_buf
,
193 sizeof(promlib_buf
));
194 /* Should this ever happen? */
195 if(error
== -1) continue;
196 if(strcmp(nodename
, promlib_buf
)==0) return thisnode
;
201 EXPORT_SYMBOL(prom_searchsiblings
);
203 /* Interal version of nextprop that does not alter return values. */
204 static char *__prom_nextprop(phandle node
, char * oprop
)
209 spin_lock_irqsave(&prom_lock
, flags
);
210 prop
= prom_nodeops
->no_nextprop(node
, oprop
);
212 spin_unlock_irqrestore(&prom_lock
, flags
);
217 /* Return the property type string after property type 'oprop'
218 * at node 'node' . Returns empty string if no more
219 * property types for this node.
221 char *prom_nextprop(phandle node
, char *oprop
, char *buffer
)
223 if (node
== 0 || (s32
)node
== -1)
226 return __prom_nextprop(node
, oprop
);
228 EXPORT_SYMBOL(prom_nextprop
);
230 phandle
prom_finddevice(char *name
)
234 phandle node
= prom_root_node
, node2
;
235 unsigned int which_io
, phys_addr
;
236 struct linux_prom_registers reg
[PROMREG_MAX
];
239 if (!*s
) return node
; /* path '.../' is legal */
240 node
= prom_getchild(node
);
242 for (d
= nbuf
; *s
!= 0 && *s
!= '@' && *s
!= '/';)
246 node
= prom_searchsiblings(node
, nbuf
);
251 if (isxdigit(s
[1]) && s
[2] == ',') {
252 which_io
= simple_strtoul(s
+1, NULL
, 16);
253 phys_addr
= simple_strtoul(s
+3, &d
, 16);
254 if (d
!= s
+ 3 && (!*d
|| *d
== '/')
257 while (node2
&& (s32
)node2
!= -1) {
258 if (prom_getproperty (node2
, "reg", (char *)reg
, sizeof (reg
)) > 0) {
259 if (which_io
== reg
[0].which_io
&& phys_addr
== reg
[0].phys_addr
) {
264 node2
= prom_getsibling(node2
);
265 if (!node2
|| (s32
)node2
== -1)
267 node2
= prom_searchsiblings(prom_getsibling(node2
), nbuf
);
271 while (*s
!= 0 && *s
!= '/') s
++;
276 EXPORT_SYMBOL(prom_finddevice
);
278 /* Set property 'pname' at node 'node' to value 'value' which has a length
279 * of 'size' bytes. Return the number of bytes the prom accepted.
281 int prom_setprop(phandle node
, const char *pname
, char *value
, int size
)
288 if ((pname
== NULL
) || (value
== NULL
))
290 spin_lock_irqsave(&prom_lock
, flags
);
291 ret
= prom_nodeops
->no_setprop(node
, pname
, value
, size
);
293 spin_unlock_irqrestore(&prom_lock
, flags
);
296 EXPORT_SYMBOL(prom_setprop
);
298 phandle
prom_inst2pkg(int inst
)
303 spin_lock_irqsave(&prom_lock
, flags
);
304 node
= (*romvec
->pv_v2devops
.v2_inst2pkg
)(inst
);
306 spin_unlock_irqrestore(&prom_lock
, flags
);