blob: 3c0d2dd9f6939faf1daf53f61141c785001429b4 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * tree.c: Basic device tree traversal/scanning for the Linux
3 * prom library.
4 *
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9#include <linux/string.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/sched.h>
Sam Ravnborg917c3662009-01-08 16:58:20 -080013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/openprom.h>
16#include <asm/oplib.h>
David S. Millerb3e13fb2007-07-12 15:55:55 -070017#include <asm/ldc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/* Return the child of node 'node' or zero if no this node has no
20 * direct descendent.
21 */
David S. Millerd979f172007-10-27 00:13:04 -070022inline int __prom_getchild(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 return p1275_cmd ("child", P1275_INOUT(1, 1), node);
25}
26
David S. Millerd979f172007-10-27 00:13:04 -070027inline int prom_getchild(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 int cnode;
30
31 if(node == -1) return 0;
32 cnode = __prom_getchild(node);
33 if(cnode == -1) return 0;
34 return (int)cnode;
35}
Sam Ravnborg917c3662009-01-08 16:58:20 -080036EXPORT_SYMBOL(prom_getchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David S. Millerd979f172007-10-27 00:13:04 -070038inline int prom_getparent(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 int cnode;
41
42 if(node == -1) return 0;
43 cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
44 if(cnode == -1) return 0;
45 return (int)cnode;
46}
47
48/* Return the next sibling of node 'node' or zero if no more siblings
49 * at this level of depth in the tree.
50 */
David S. Millerd979f172007-10-27 00:13:04 -070051inline int __prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
David S. Millerd82ace72006-02-09 02:52:44 -080053 return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
David S. Millerd979f172007-10-27 00:13:04 -070056inline int prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int sibnode;
59
David S. Millerd82ace72006-02-09 02:52:44 -080060 if (node == -1)
61 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 sibnode = __prom_getsibling(node);
David S. Millerd82ace72006-02-09 02:52:44 -080063 if (sibnode == -1)
64 return 0;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return sibnode;
67}
Sam Ravnborg917c3662009-01-08 16:58:20 -080068EXPORT_SYMBOL(prom_getsibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* Return the length in bytes of property 'prop' at node 'node'.
71 * Return -1 on error.
72 */
David S. Millerd979f172007-10-27 00:13:04 -070073inline int prom_getproplen(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 if((!node) || (!prop)) return -1;
76 return p1275_cmd ("getproplen",
77 P1275_ARG(1,P1275_ARG_IN_STRING)|
78 P1275_INOUT(2, 1),
79 node, prop);
80}
Sam Ravnborg917c3662009-01-08 16:58:20 -080081EXPORT_SYMBOL(prom_getproplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* Acquire a property 'prop' at node 'node' and place it in
84 * 'buffer' which has a size of 'bufsize'. If the acquisition
85 * was successful the length will be returned, else -1 is returned.
86 */
David S. Millerd979f172007-10-27 00:13:04 -070087inline int prom_getproperty(int node, const char *prop,
88 char *buffer, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int plen;
91
92 plen = prom_getproplen(node, prop);
David S. Millerbff06d52005-09-22 20:11:33 -070093 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -1;
David S. Millerbff06d52005-09-22 20:11:33 -070095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* Ok, things seem all right. */
David S. Millerbff06d52005-09-22 20:11:33 -070097 return p1275_cmd(prom_getprop_name,
98 P1275_ARG(1,P1275_ARG_IN_STRING)|
99 P1275_ARG(2,P1275_ARG_OUT_BUF)|
100 P1275_INOUT(4, 1),
101 node, prop, buffer, P1275_SIZE(plen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800104EXPORT_SYMBOL(prom_getproperty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* Acquire an integer property and return its value. Returns -1
107 * on failure.
108 */
David S. Millerd979f172007-10-27 00:13:04 -0700109inline int prom_getint(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int intprop;
112
113 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
114 return intprop;
115
116 return -1;
117}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800118EXPORT_SYMBOL(prom_getint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Acquire an integer property, upon error return the passed default
121 * integer.
122 */
123
David S. Millerd979f172007-10-27 00:13:04 -0700124int prom_getintdefault(int node, const char *property, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int retval;
127
128 retval = prom_getint(node, property);
129 if(retval == -1) return deflt;
130
131 return retval;
132}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800133EXPORT_SYMBOL(prom_getintdefault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* Acquire a boolean property, 1=TRUE 0=FALSE. */
David S. Millerd979f172007-10-27 00:13:04 -0700136int prom_getbool(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int retval;
139
140 retval = prom_getproplen(node, prop);
141 if(retval == -1) return 0;
142 return 1;
143}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800144EXPORT_SYMBOL(prom_getbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* Acquire a property whose value is a string, returns a null
147 * string on error. The char pointer is the user supplied string
148 * buffer.
149 */
David S. Millerd979f172007-10-27 00:13:04 -0700150void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int len;
153
154 len = prom_getproperty(node, prop, user_buf, ubuf_size);
155 if(len != -1) return;
156 user_buf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800158EXPORT_SYMBOL(prom_getstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/* Does the device at node 'node' have name 'name'?
161 * YES = 1 NO = 0
162 */
David S. Millerd979f172007-10-27 00:13:04 -0700163int prom_nodematch(int node, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 char namebuf[128];
166 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
167 if(strcmp(namebuf, name) == 0) return 1;
168 return 0;
169}
170
171/* Search siblings at 'node_start' for a node with name
172 * 'nodename'. Return node if successful, zero if not.
173 */
David S. Millerd979f172007-10-27 00:13:04 -0700174int prom_searchsiblings(int node_start, const char *nodename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176
177 int thisnode, error;
178 char promlib_buf[128];
179
180 for(thisnode = node_start; thisnode;
181 thisnode=prom_getsibling(thisnode)) {
182 error = prom_getproperty(thisnode, "name", promlib_buf,
183 sizeof(promlib_buf));
184 /* Should this ever happen? */
185 if(error == -1) continue;
186 if(strcmp(nodename, promlib_buf)==0) return thisnode;
187 }
188
189 return 0;
190}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800191EXPORT_SYMBOL(prom_searchsiblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* Return the first property type for node 'node'.
194 * buffer should be at least 32B in length
195 */
David S. Millerd979f172007-10-27 00:13:04 -0700196inline char *prom_firstprop(int node, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 *buffer = 0;
199 if(node == -1) return buffer;
200 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
201 P1275_INOUT(3, 0),
202 node, (char *) 0x0, buffer);
203 return buffer;
204}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800205EXPORT_SYMBOL(prom_firstprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/* Return the property type string after property type 'oprop'
208 * at node 'node' . Returns NULL string if no more
209 * property types for this node.
210 */
David S. Millerd979f172007-10-27 00:13:04 -0700211inline char *prom_nextprop(int node, const char *oprop, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 char buf[32];
214
215 if(node == -1) {
216 *buffer = 0;
217 return buffer;
218 }
219 if (oprop == buffer) {
220 strcpy (buf, oprop);
221 oprop = buf;
222 }
223 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
224 P1275_ARG(2,P1275_ARG_OUT_32B)|
225 P1275_INOUT(3, 0),
226 node, oprop, buffer);
227 return buffer;
228}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800229EXPORT_SYMBOL(prom_nextprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231int
David S. Millerbff06d52005-09-22 20:11:33 -0700232prom_finddevice(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David S. Millerbff06d52005-09-22 20:11:33 -0700234 if (!name)
235 return 0;
236 return p1275_cmd(prom_finddev_name,
237 P1275_ARG(0,P1275_ARG_IN_STRING)|
238 P1275_INOUT(1, 1),
239 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800241EXPORT_SYMBOL(prom_finddevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David S. Millerbff06d52005-09-22 20:11:33 -0700243int prom_node_has_property(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 char buf [32];
246
247 *buf = 0;
248 do {
249 prom_nextprop(node, buf, buf);
250 if(!strcmp(buf, prop))
251 return 1;
252 } while (*buf);
253 return 0;
254}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800255EXPORT_SYMBOL(prom_node_has_property);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* Set property 'pname' at node 'node' to value 'value' which has a length
258 * of 'size' bytes. Return the number of bytes the prom accepted.
259 */
260int
David S. Millerbff06d52005-09-22 20:11:33 -0700261prom_setprop(int node, const char *pname, char *value, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
David S. Millerb3e13fb2007-07-12 15:55:55 -0700263 if (size == 0)
264 return 0;
265 if ((pname == 0) || (value == 0))
266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
David S. Millerb3e13fb2007-07-12 15:55:55 -0700268#ifdef CONFIG_SUN_LDOMS
269 if (ldom_domaining_enabled) {
270 ldom_set_var(pname, value);
271 return 0;
272 }
273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
275 P1275_ARG(2,P1275_ARG_IN_BUF)|
276 P1275_INOUT(4, 1),
277 node, pname, value, P1275_SIZE(size));
278}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800279EXPORT_SYMBOL(prom_setprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David S. Millerd979f172007-10-27 00:13:04 -0700281inline int prom_inst2pkg(int inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 int node;
284
285 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
286 if (node == -1) return 0;
287 return node;
288}
289
290/* Return 'node' assigned to a particular prom 'path'
291 * FIXME: Should work for v0 as well
292 */
293int
David S. Millerbff06d52005-09-22 20:11:33 -0700294prom_pathtoinode(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int node, inst;
297
298 inst = prom_devopen (path);
299 if (inst == 0) return 0;
300 node = prom_inst2pkg (inst);
301 prom_devclose (inst);
302 if (node == -1) return 0;
303 return node;
304}
David S. Millerc73fcc82007-07-20 16:59:26 -0700305
306int prom_ihandle2path(int handle, char *buffer, int bufsize)
307{
308 return p1275_cmd("instance-to-path",
309 P1275_ARG(1,P1275_ARG_OUT_BUF)|
310 P1275_INOUT(3, 1),
311 handle, buffer, P1275_SIZE(bufsize));
312}