blob: 63e08e149774fc6a0a30fcd9228abb83f434aae7 [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -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 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/string.h>
9#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/ctype.h>
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/openprom.h>
16#include <asm/oplib.h>
17
18extern void restore_current(void);
19
20static char promlib_buf[128];
21
22/* Internal version of prom_getchild that does not alter return values. */
Andres Salomon8d125562010-10-08 14:18:11 -070023phandle __prom_getchild(phandle node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 unsigned long flags;
Andres Salomon8d125562010-10-08 14:18:11 -070026 phandle cnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 spin_lock_irqsave(&prom_lock, flags);
29 cnode = prom_nodeops->no_child(node);
30 restore_current();
31 spin_unlock_irqrestore(&prom_lock, flags);
32
33 return cnode;
34}
35
36/* Return the child of node 'node' or zero if no this node has no
37 * direct descendent.
38 */
Andres Salomon8d125562010-10-08 14:18:11 -070039phandle prom_getchild(phandle node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Andres Salomon8d125562010-10-08 14:18:11 -070041 phandle cnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 if (node == -1)
44 return 0;
45
46 cnode = __prom_getchild(node);
47 if (cnode == 0 || cnode == -1)
48 return 0;
49
50 return cnode;
51}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080052EXPORT_SYMBOL(prom_getchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* Internal version of prom_getsibling that does not alter return values. */
Andres Salomon8d125562010-10-08 14:18:11 -070055phandle __prom_getsibling(phandle node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 unsigned long flags;
Andres Salomon8d125562010-10-08 14:18:11 -070058 phandle cnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 spin_lock_irqsave(&prom_lock, flags);
61 cnode = prom_nodeops->no_nextnode(node);
62 restore_current();
63 spin_unlock_irqrestore(&prom_lock, flags);
64
65 return cnode;
66}
67
68/* Return the next sibling of node 'node' or zero if no more siblings
69 * at this level of depth in the tree.
70 */
Andres Salomon8d125562010-10-08 14:18:11 -070071phandle prom_getsibling(phandle node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Andres Salomon8d125562010-10-08 14:18:11 -070073 phandle sibnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (node == -1)
76 return 0;
77
78 sibnode = __prom_getsibling(node);
79 if (sibnode == 0 || sibnode == -1)
80 return 0;
81
82 return sibnode;
83}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080084EXPORT_SYMBOL(prom_getsibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Return the length in bytes of property 'prop' at node 'node'.
87 * Return -1 on error.
88 */
Andres Salomon8d125562010-10-08 14:18:11 -070089int prom_getproplen(phandle node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int ret;
92 unsigned long flags;
93
94 if((!node) || (!prop))
95 return -1;
96
97 spin_lock_irqsave(&prom_lock, flags);
98 ret = prom_nodeops->no_proplen(node, prop);
99 restore_current();
100 spin_unlock_irqrestore(&prom_lock, flags);
101 return ret;
102}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800103EXPORT_SYMBOL(prom_getproplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* Acquire a property 'prop' at node 'node' and place it in
106 * 'buffer' which has a size of 'bufsize'. If the acquisition
107 * was successful the length will be returned, else -1 is returned.
108 */
Andres Salomon8d125562010-10-08 14:18:11 -0700109int prom_getproperty(phandle node, const char *prop, char *buffer, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int plen, ret;
112 unsigned long flags;
113
114 plen = prom_getproplen(node, prop);
115 if((plen > bufsize) || (plen == 0) || (plen == -1))
116 return -1;
117 /* Ok, things seem all right. */
118 spin_lock_irqsave(&prom_lock, flags);
119 ret = prom_nodeops->no_getprop(node, prop, buffer);
120 restore_current();
121 spin_unlock_irqrestore(&prom_lock, flags);
122 return ret;
123}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800124EXPORT_SYMBOL(prom_getproperty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* Acquire an integer property and return its value. Returns -1
127 * on failure.
128 */
Andres Salomon8d125562010-10-08 14:18:11 -0700129int prom_getint(phandle node, char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 static int intprop;
132
133 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
134 return intprop;
135
136 return -1;
137}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800138EXPORT_SYMBOL(prom_getint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Acquire an integer property, upon error return the passed default
141 * integer.
142 */
Andres Salomon8d125562010-10-08 14:18:11 -0700143int prom_getintdefault(phandle node, char *property, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int retval;
146
147 retval = prom_getint(node, property);
148 if(retval == -1) return deflt;
149
150 return retval;
151}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800152EXPORT_SYMBOL(prom_getintdefault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/* Acquire a boolean property, 1=TRUE 0=FALSE. */
Andres Salomon8d125562010-10-08 14:18:11 -0700155int prom_getbool(phandle node, char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int retval;
158
159 retval = prom_getproplen(node, prop);
160 if(retval == -1) return 0;
161 return 1;
162}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800163EXPORT_SYMBOL(prom_getbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* Acquire a property whose value is a string, returns a null
166 * string on error. The char pointer is the user supplied string
167 * buffer.
168 */
Andres Salomon8d125562010-10-08 14:18:11 -0700169void prom_getstring(phandle node, char *prop, char *user_buf, int ubuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int len;
172
173 len = prom_getproperty(node, prop, user_buf, ubuf_size);
174 if(len != -1) return;
175 user_buf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800177EXPORT_SYMBOL(prom_getstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179
180/* Does the device at node 'node' have name 'name'?
181 * YES = 1 NO = 0
182 */
Andres Salomon8d125562010-10-08 14:18:11 -0700183int prom_nodematch(phandle node, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int error;
186
187 static char namebuf[128];
188 error = prom_getproperty(node, "name", namebuf, sizeof(namebuf));
189 if (error == -1) return 0;
190 if(strcmp(namebuf, name) == 0) return 1;
191 return 0;
192}
193
194/* Search siblings at 'node_start' for a node with name
195 * 'nodename'. Return node if successful, zero if not.
196 */
Andres Salomon8d125562010-10-08 14:18:11 -0700197phandle prom_searchsiblings(phandle node_start, char *nodename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199
Andres Salomon8d125562010-10-08 14:18:11 -0700200 phandle thisnode;
201 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 for(thisnode = node_start; thisnode;
204 thisnode=prom_getsibling(thisnode)) {
205 error = prom_getproperty(thisnode, "name", promlib_buf,
206 sizeof(promlib_buf));
207 /* Should this ever happen? */
208 if(error == -1) continue;
209 if(strcmp(nodename, promlib_buf)==0) return thisnode;
210 }
211
212 return 0;
213}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800214EXPORT_SYMBOL(prom_searchsiblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* Interal version of nextprop that does not alter return values. */
Andres Salomon8d125562010-10-08 14:18:11 -0700217char *__prom_nextprop(phandle node, char * oprop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 unsigned long flags;
220 char *prop;
221
222 spin_lock_irqsave(&prom_lock, flags);
223 prop = prom_nodeops->no_nextprop(node, oprop);
224 restore_current();
225 spin_unlock_irqrestore(&prom_lock, flags);
226
227 return prop;
228}
229
230/* Return the first property name for node 'node'. */
231/* buffer is unused argument, but as v9 uses it, we need to have the same interface */
Andres Salomon8d125562010-10-08 14:18:11 -0700232char *prom_firstprop(phandle node, char *bufer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 if (node == 0 || node == -1)
235 return "";
236
237 return __prom_nextprop(node, "");
238}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800239EXPORT_SYMBOL(prom_firstprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* Return the property type string after property type 'oprop'
242 * at node 'node' . Returns empty string if no more
243 * property types for this node.
244 */
Andres Salomon8d125562010-10-08 14:18:11 -0700245char *prom_nextprop(phandle node, char *oprop, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 if (node == 0 || node == -1)
248 return "";
249
250 return __prom_nextprop(node, oprop);
251}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800252EXPORT_SYMBOL(prom_nextprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Andres Salomon8d125562010-10-08 14:18:11 -0700254phandle prom_finddevice(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 char nbuf[128];
257 char *s = name, *d;
Andres Salomon8d125562010-10-08 14:18:11 -0700258 phandle node = prom_root_node, node2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned int which_io, phys_addr;
260 struct linux_prom_registers reg[PROMREG_MAX];
261
262 while (*s++) {
263 if (!*s) return node; /* path '.../' is legal */
264 node = prom_getchild(node);
265
266 for (d = nbuf; *s != 0 && *s != '@' && *s != '/';)
267 *d++ = *s++;
268 *d = 0;
269
270 node = prom_searchsiblings(node, nbuf);
271 if (!node)
272 return 0;
273
274 if (*s == '@') {
275 if (isxdigit(s[1]) && s[2] == ',') {
276 which_io = simple_strtoul(s+1, NULL, 16);
277 phys_addr = simple_strtoul(s+3, &d, 16);
278 if (d != s + 3 && (!*d || *d == '/')
279 && d <= s + 3 + 8) {
280 node2 = node;
281 while (node2 && node2 != -1) {
282 if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) {
283 if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) {
284 node = node2;
285 break;
286 }
287 }
288 node2 = prom_getsibling(node2);
289 if (!node2 || node2 == -1)
290 break;
291 node2 = prom_searchsiblings(prom_getsibling(node2), nbuf);
292 }
293 }
294 }
295 while (*s != 0 && *s != '/') s++;
296 }
297 }
298 return node;
299}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800300EXPORT_SYMBOL(prom_finddevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Andres Salomon8d125562010-10-08 14:18:11 -0700302int prom_node_has_property(phandle node, char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 char *current_property = "";
305
306 do {
307 current_property = prom_nextprop(node, current_property, NULL);
308 if(!strcmp(current_property, prop))
309 return 1;
310 } while (*current_property);
311 return 0;
312}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800313EXPORT_SYMBOL(prom_node_has_property);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/* Set property 'pname' at node 'node' to value 'value' which has a length
316 * of 'size' bytes. Return the number of bytes the prom accepted.
317 */
Andres Salomon8d125562010-10-08 14:18:11 -0700318int prom_setprop(phandle node, const char *pname, char *value, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 unsigned long flags;
321 int ret;
322
323 if(size == 0) return 0;
324 if((pname == 0) || (value == 0)) return 0;
325 spin_lock_irqsave(&prom_lock, flags);
326 ret = prom_nodeops->no_setprop(node, pname, value, size);
327 restore_current();
328 spin_unlock_irqrestore(&prom_lock, flags);
329 return ret;
330}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800331EXPORT_SYMBOL(prom_setprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Andres Salomon8d125562010-10-08 14:18:11 -0700333phandle prom_inst2pkg(int inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Andres Salomon8d125562010-10-08 14:18:11 -0700335 phandle node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned long flags;
337
338 spin_lock_irqsave(&prom_lock, flags);
339 node = (*romvec->pv_v2devops.v2_inst2pkg)(inst);
340 restore_current();
341 spin_unlock_irqrestore(&prom_lock, flags);
342 if (node == -1) return 0;
343 return node;
344}
345
346/* Return 'node' assigned to a particular prom 'path'
347 * FIXME: Should work for v0 as well
348 */
Andres Salomon8d125562010-10-08 14:18:11 -0700349phandle prom_pathtoinode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Andres Salomon8d125562010-10-08 14:18:11 -0700351 phandle node;
352 int inst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 inst = prom_devopen (path);
355 if (inst == -1) return 0;
356 node = prom_inst2pkg (inst);
357 prom_devclose (inst);
358 if (node == -1) return 0;
359 return node;
360}