blob: 175c709e39a9c5e5f0edffda95de04109ae8b482 [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}
Sam Ravnborg917c3662009-01-08 16:58:20 -080026EXPORT_SYMBOL(__prom_getchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David S. Millerd979f172007-10-27 00:13:04 -070028inline int prom_getchild(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 int cnode;
31
32 if(node == -1) return 0;
33 cnode = __prom_getchild(node);
34 if(cnode == -1) return 0;
35 return (int)cnode;
36}
Sam Ravnborg917c3662009-01-08 16:58:20 -080037EXPORT_SYMBOL(prom_getchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David S. Millerd979f172007-10-27 00:13:04 -070039inline int prom_getparent(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 int cnode;
42
43 if(node == -1) return 0;
44 cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
45 if(cnode == -1) return 0;
46 return (int)cnode;
47}
48
49/* Return the next sibling of node 'node' or zero if no more siblings
50 * at this level of depth in the tree.
51 */
David S. Millerd979f172007-10-27 00:13:04 -070052inline int __prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
David S. Millerd82ace72006-02-09 02:52:44 -080054 return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
Sam Ravnborg917c3662009-01-08 16:58:20 -080056EXPORT_SYMBOL(__prom_getsibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David S. Millerd979f172007-10-27 00:13:04 -070058inline int prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int sibnode;
61
David S. Millerd82ace72006-02-09 02:52:44 -080062 if (node == -1)
63 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 sibnode = __prom_getsibling(node);
David S. Millerd82ace72006-02-09 02:52:44 -080065 if (sibnode == -1)
66 return 0;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return sibnode;
69}
Sam Ravnborg917c3662009-01-08 16:58:20 -080070EXPORT_SYMBOL(prom_getsibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* Return the length in bytes of property 'prop' at node 'node'.
73 * Return -1 on error.
74 */
David S. Millerd979f172007-10-27 00:13:04 -070075inline int prom_getproplen(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 if((!node) || (!prop)) return -1;
78 return p1275_cmd ("getproplen",
79 P1275_ARG(1,P1275_ARG_IN_STRING)|
80 P1275_INOUT(2, 1),
81 node, prop);
82}
Sam Ravnborg917c3662009-01-08 16:58:20 -080083EXPORT_SYMBOL(prom_getproplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
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.
88 */
David S. Millerd979f172007-10-27 00:13:04 -070089inline int prom_getproperty(int node, const char *prop,
90 char *buffer, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int plen;
93
94 plen = prom_getproplen(node, prop);
David S. Millerbff06d52005-09-22 20:11:33 -070095 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -1;
David S. Millerbff06d52005-09-22 20:11:33 -070097 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* Ok, things seem all right. */
David S. Millerbff06d52005-09-22 20:11:33 -070099 return p1275_cmd(prom_getprop_name,
100 P1275_ARG(1,P1275_ARG_IN_STRING)|
101 P1275_ARG(2,P1275_ARG_OUT_BUF)|
102 P1275_INOUT(4, 1),
103 node, prop, buffer, P1275_SIZE(plen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800106EXPORT_SYMBOL(prom_getproperty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* Acquire an integer property and return its value. Returns -1
109 * on failure.
110 */
David S. Millerd979f172007-10-27 00:13:04 -0700111inline int prom_getint(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int intprop;
114
115 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
116 return intprop;
117
118 return -1;
119}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800120EXPORT_SYMBOL(prom_getint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* Acquire an integer property, upon error return the passed default
123 * integer.
124 */
125
David S. Millerd979f172007-10-27 00:13:04 -0700126int prom_getintdefault(int node, const char *property, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int retval;
129
130 retval = prom_getint(node, property);
131 if(retval == -1) return deflt;
132
133 return retval;
134}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800135EXPORT_SYMBOL(prom_getintdefault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Acquire a boolean property, 1=TRUE 0=FALSE. */
David S. Millerd979f172007-10-27 00:13:04 -0700138int prom_getbool(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int retval;
141
142 retval = prom_getproplen(node, prop);
143 if(retval == -1) return 0;
144 return 1;
145}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800146EXPORT_SYMBOL(prom_getbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Acquire a property whose value is a string, returns a null
149 * string on error. The char pointer is the user supplied string
150 * buffer.
151 */
David S. Millerd979f172007-10-27 00:13:04 -0700152void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int len;
155
156 len = prom_getproperty(node, prop, user_buf, ubuf_size);
157 if(len != -1) return;
158 user_buf[0] = 0;
159 return;
160}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800161EXPORT_SYMBOL(prom_getstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/* Does the device at node 'node' have name 'name'?
164 * YES = 1 NO = 0
165 */
David S. Millerd979f172007-10-27 00:13:04 -0700166int prom_nodematch(int node, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 char namebuf[128];
169 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
170 if(strcmp(namebuf, name) == 0) return 1;
171 return 0;
172}
173
174/* Search siblings at 'node_start' for a node with name
175 * 'nodename'. Return node if successful, zero if not.
176 */
David S. Millerd979f172007-10-27 00:13:04 -0700177int prom_searchsiblings(int node_start, const char *nodename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179
180 int thisnode, error;
181 char promlib_buf[128];
182
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;
190 }
191
192 return 0;
193}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800194EXPORT_SYMBOL(prom_searchsiblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* Return the first property type for node 'node'.
197 * buffer should be at least 32B in length
198 */
David S. Millerd979f172007-10-27 00:13:04 -0700199inline char *prom_firstprop(int node, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 *buffer = 0;
202 if(node == -1) return buffer;
203 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
204 P1275_INOUT(3, 0),
205 node, (char *) 0x0, buffer);
206 return buffer;
207}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800208EXPORT_SYMBOL(prom_firstprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
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.
213 */
David S. Millerd979f172007-10-27 00:13:04 -0700214inline char *prom_nextprop(int node, const char *oprop, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 char buf[32];
217
218 if(node == -1) {
219 *buffer = 0;
220 return buffer;
221 }
222 if (oprop == buffer) {
223 strcpy (buf, oprop);
224 oprop = buf;
225 }
226 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
227 P1275_ARG(2,P1275_ARG_OUT_32B)|
228 P1275_INOUT(3, 0),
229 node, oprop, buffer);
230 return buffer;
231}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800232EXPORT_SYMBOL(prom_nextprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234int
David S. Millerbff06d52005-09-22 20:11:33 -0700235prom_finddevice(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
David S. Millerbff06d52005-09-22 20:11:33 -0700237 if (!name)
238 return 0;
239 return p1275_cmd(prom_finddev_name,
240 P1275_ARG(0,P1275_ARG_IN_STRING)|
241 P1275_INOUT(1, 1),
242 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800244EXPORT_SYMBOL(prom_finddevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David S. Millerbff06d52005-09-22 20:11:33 -0700246int prom_node_has_property(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 char buf [32];
249
250 *buf = 0;
251 do {
252 prom_nextprop(node, buf, buf);
253 if(!strcmp(buf, prop))
254 return 1;
255 } while (*buf);
256 return 0;
257}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800258EXPORT_SYMBOL(prom_node_has_property);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/* 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.
262 */
263int
David S. Millerbff06d52005-09-22 20:11:33 -0700264prom_setprop(int node, const char *pname, char *value, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
David S. Millerb3e13fb2007-07-12 15:55:55 -0700266 if (size == 0)
267 return 0;
268 if ((pname == 0) || (value == 0))
269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David S. Millerb3e13fb2007-07-12 15:55:55 -0700271#ifdef CONFIG_SUN_LDOMS
272 if (ldom_domaining_enabled) {
273 ldom_set_var(pname, value);
274 return 0;
275 }
276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
278 P1275_ARG(2,P1275_ARG_IN_BUF)|
279 P1275_INOUT(4, 1),
280 node, pname, value, P1275_SIZE(size));
281}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800282EXPORT_SYMBOL(prom_setprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
David S. Millerd979f172007-10-27 00:13:04 -0700284inline int prom_inst2pkg(int inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int node;
287
288 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
289 if (node == -1) return 0;
290 return node;
291}
292
293/* Return 'node' assigned to a particular prom 'path'
294 * FIXME: Should work for v0 as well
295 */
296int
David S. Millerbff06d52005-09-22 20:11:33 -0700297prom_pathtoinode(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int node, inst;
300
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;
306 return node;
307}
David S. Millerc73fcc82007-07-20 16:59:26 -0700308
309int prom_ihandle2path(int handle, char *buffer, int bufsize)
310{
311 return p1275_cmd("instance-to-path",
312 P1275_ARG(1,P1275_ARG_OUT_BUF)|
313 P1275_INOUT(3, 1),
314 handle, buffer, P1275_SIZE(bufsize));
315}