blob: b1ff9e87dcc6a339b9387f5ae56378adc6274be4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: tree.c,v 1.10 1998/01/10 22:39:00 ecd Exp $
2 * 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>
13
14#include <asm/openprom.h>
15#include <asm/oplib.h>
16
17/* Return the child of node 'node' or zero if no this node has no
18 * direct descendent.
19 */
20__inline__ int
21__prom_getchild(int node)
22{
23 return p1275_cmd ("child", P1275_INOUT(1, 1), node);
24}
25
26__inline__ int
27prom_getchild(int node)
28{
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}
36
37__inline__ int
38prom_getparent(int node)
39{
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 */
51__inline__ int
52__prom_getsibling(int node)
53{
54 return p1275_cmd ("peer", P1275_INOUT(1, 1), node);
55}
56
57__inline__ int
58prom_getsibling(int node)
59{
60 int sibnode;
61
62 if(node == -1) return 0;
63 sibnode = __prom_getsibling(node);
64 if(sibnode == -1) return 0;
65 return sibnode;
66}
67
68/* Return the length in bytes of property 'prop' at node 'node'.
69 * Return -1 on error.
70 */
71__inline__ int
David S. Millerbff06d52005-09-22 20:11:33 -070072prom_getproplen(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 if((!node) || (!prop)) return -1;
75 return p1275_cmd ("getproplen",
76 P1275_ARG(1,P1275_ARG_IN_STRING)|
77 P1275_INOUT(2, 1),
78 node, prop);
79}
80
81/* Acquire a property 'prop' at node 'node' and place it in
82 * 'buffer' which has a size of 'bufsize'. If the acquisition
83 * was successful the length will be returned, else -1 is returned.
84 */
85__inline__ int
David S. Millerbff06d52005-09-22 20:11:33 -070086prom_getproperty(int node, const char *prop, char *buffer, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 int plen;
89
90 plen = prom_getproplen(node, prop);
David S. Millerbff06d52005-09-22 20:11:33 -070091 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -1;
David S. Millerbff06d52005-09-22 20:11:33 -070093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* Ok, things seem all right. */
David S. Millerbff06d52005-09-22 20:11:33 -070095 return p1275_cmd(prom_getprop_name,
96 P1275_ARG(1,P1275_ARG_IN_STRING)|
97 P1275_ARG(2,P1275_ARG_OUT_BUF)|
98 P1275_INOUT(4, 1),
99 node, prop, buffer, P1275_SIZE(plen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101}
102
103/* Acquire an integer property and return its value. Returns -1
104 * on failure.
105 */
106__inline__ int
David S. Millerbff06d52005-09-22 20:11:33 -0700107prom_getint(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int intprop;
110
111 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
112 return intprop;
113
114 return -1;
115}
116
117/* Acquire an integer property, upon error return the passed default
118 * integer.
119 */
120
121int
David S. Millerbff06d52005-09-22 20:11:33 -0700122prom_getintdefault(int node, const char *property, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int retval;
125
126 retval = prom_getint(node, property);
127 if(retval == -1) return deflt;
128
129 return retval;
130}
131
132/* Acquire a boolean property, 1=TRUE 0=FALSE. */
133int
David S. Millerbff06d52005-09-22 20:11:33 -0700134prom_getbool(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 int retval;
137
138 retval = prom_getproplen(node, prop);
139 if(retval == -1) return 0;
140 return 1;
141}
142
143/* Acquire a property whose value is a string, returns a null
144 * string on error. The char pointer is the user supplied string
145 * buffer.
146 */
147void
David S. Millerbff06d52005-09-22 20:11:33 -0700148prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int len;
151
152 len = prom_getproperty(node, prop, user_buf, ubuf_size);
153 if(len != -1) return;
154 user_buf[0] = 0;
155 return;
156}
157
158
159/* Does the device at node 'node' have name 'name'?
160 * YES = 1 NO = 0
161 */
162int
David S. Millerbff06d52005-09-22 20:11:33 -0700163prom_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 */
174int
David S. Millerbff06d52005-09-22 20:11:33 -0700175prom_searchsiblings(int node_start, const char *nodename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177
178 int thisnode, error;
179 char promlib_buf[128];
180
181 for(thisnode = node_start; thisnode;
182 thisnode=prom_getsibling(thisnode)) {
183 error = prom_getproperty(thisnode, "name", promlib_buf,
184 sizeof(promlib_buf));
185 /* Should this ever happen? */
186 if(error == -1) continue;
187 if(strcmp(nodename, promlib_buf)==0) return thisnode;
188 }
189
190 return 0;
191}
192
193/* Gets name in the {name@x,yyyyy|name (if no reg)} form */
194int
195prom_getname (int node, char *buffer, int len)
196{
197 int i, sbus = 0;
198 int pci = 0, ebus = 0, ide = 0;
199 struct linux_prom_registers *reg;
200 struct linux_prom64_registers reg64[PROMREG_MAX];
201
202 for (sbus = prom_getparent (node); sbus; sbus = prom_getparent (sbus)) {
203 i = prom_getproperty (sbus, "name", buffer, len);
204 if (i > 0) {
205 buffer [i] = 0;
206 if (!strcmp (buffer, "sbus"))
207 goto getit;
208 }
209 }
210 if ((pci = prom_getparent (node))) {
211 i = prom_getproperty (pci, "name", buffer, len);
212 if (i > 0) {
213 buffer [i] = 0;
214 if (!strcmp (buffer, "pci"))
215 goto getit;
216 }
217 pci = 0;
218 }
219 if ((ebus = prom_getparent (node))) {
220 i = prom_getproperty (ebus, "name", buffer, len);
221 if (i > 0) {
222 buffer[i] = 0;
223 if (!strcmp (buffer, "ebus"))
224 goto getit;
225 }
226 ebus = 0;
227 }
228 if ((ide = prom_getparent (node))) {
229 i = prom_getproperty (ide, "name", buffer, len);
230 if (i > 0) {
231 buffer [i] = 0;
232 if (!strcmp (buffer, "ide"))
233 goto getit;
234 }
235 ide = 0;
236 }
237getit:
238 i = prom_getproperty (node, "name", buffer, len);
239 if (i <= 0) {
240 buffer [0] = 0;
241 return -1;
242 }
243 buffer [i] = 0;
244 len -= i;
245 i = prom_getproperty (node, "reg", (char *)reg64, sizeof (reg64));
246 if (i <= 0) return 0;
247 if (len < 16) return -1;
248 buffer = strchr (buffer, 0);
249 if (sbus) {
250 reg = (struct linux_prom_registers *)reg64;
251 sprintf (buffer, "@%x,%x", reg[0].which_io, (uint)reg[0].phys_addr);
252 } else if (pci) {
253 int dev, fn;
254 reg = (struct linux_prom_registers *)reg64;
255 fn = (reg[0].which_io >> 8) & 0x07;
256 dev = (reg[0].which_io >> 11) & 0x1f;
257 if (fn)
258 sprintf (buffer, "@%x,%x", dev, fn);
259 else
260 sprintf (buffer, "@%x", dev);
261 } else if (ebus) {
262 reg = (struct linux_prom_registers *)reg64;
263 sprintf (buffer, "@%x,%x", reg[0].which_io, reg[0].phys_addr);
264 } else if (ide) {
265 reg = (struct linux_prom_registers *)reg64;
266 sprintf (buffer, "@%x,%x", reg[0].which_io, reg[0].phys_addr);
267 } else if (i == 4) { /* Happens on 8042's children on Ultra/PCI. */
268 reg = (struct linux_prom_registers *)reg64;
269 sprintf (buffer, "@%x", reg[0].which_io);
270 } else {
271 sprintf (buffer, "@%x,%x",
272 (unsigned int)(reg64[0].phys_addr >> 36),
273 (unsigned int)(reg64[0].phys_addr));
274 }
275 return 0;
276}
277
278/* Return the first property type for node 'node'.
279 * buffer should be at least 32B in length
280 */
281__inline__ char *
282prom_firstprop(int node, char *buffer)
283{
284 *buffer = 0;
285 if(node == -1) return buffer;
286 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
287 P1275_INOUT(3, 0),
288 node, (char *) 0x0, buffer);
289 return buffer;
290}
291
292/* Return the property type string after property type 'oprop'
293 * at node 'node' . Returns NULL string if no more
294 * property types for this node.
295 */
296__inline__ char *
David S. Millerbff06d52005-09-22 20:11:33 -0700297prom_nextprop(int node, const char *oprop, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 char buf[32];
300
301 if(node == -1) {
302 *buffer = 0;
303 return buffer;
304 }
305 if (oprop == buffer) {
306 strcpy (buf, oprop);
307 oprop = buf;
308 }
309 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
310 P1275_ARG(2,P1275_ARG_OUT_32B)|
311 P1275_INOUT(3, 0),
312 node, oprop, buffer);
313 return buffer;
314}
315
316int
David S. Millerbff06d52005-09-22 20:11:33 -0700317prom_finddevice(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
David S. Millerbff06d52005-09-22 20:11:33 -0700319 if (!name)
320 return 0;
321 return p1275_cmd(prom_finddev_name,
322 P1275_ARG(0,P1275_ARG_IN_STRING)|
323 P1275_INOUT(1, 1),
324 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
David S. Millerbff06d52005-09-22 20:11:33 -0700327int prom_node_has_property(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 char buf [32];
330
331 *buf = 0;
332 do {
333 prom_nextprop(node, buf, buf);
334 if(!strcmp(buf, prop))
335 return 1;
336 } while (*buf);
337 return 0;
338}
339
340/* Set property 'pname' at node 'node' to value 'value' which has a length
341 * of 'size' bytes. Return the number of bytes the prom accepted.
342 */
343int
David S. Millerbff06d52005-09-22 20:11:33 -0700344prom_setprop(int node, const char *pname, char *value, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 if(size == 0) return 0;
347 if((pname == 0) || (value == 0)) return 0;
348
349 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
350 P1275_ARG(2,P1275_ARG_IN_BUF)|
351 P1275_INOUT(4, 1),
352 node, pname, value, P1275_SIZE(size));
353}
354
355__inline__ int
356prom_inst2pkg(int inst)
357{
358 int node;
359
360 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
361 if (node == -1) return 0;
362 return node;
363}
364
365/* Return 'node' assigned to a particular prom 'path'
366 * FIXME: Should work for v0 as well
367 */
368int
David S. Millerbff06d52005-09-22 20:11:33 -0700369prom_pathtoinode(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 int node, inst;
372
373 inst = prom_devopen (path);
374 if (inst == 0) return 0;
375 node = prom_inst2pkg (inst);
376 prom_devclose (inst);
377 if (node == -1) return 0;
378 return node;
379}