1 /* $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
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
14 #include <asm/openprom.h>
15 #include <asm/oplib.h>
17 /* Return the child of node 'node' or zero if no this node has no
21 __prom_getchild(int node
)
23 return p1275_cmd ("child", P1275_INOUT(1, 1), node
);
27 prom_getchild(int node
)
31 if(node
== -1) return 0;
32 cnode
= __prom_getchild(node
);
33 if(cnode
== -1) return 0;
38 prom_getparent(int node
)
42 if(node
== -1) return 0;
43 cnode
= p1275_cmd ("parent", P1275_INOUT(1, 1), node
);
44 if(cnode
== -1) return 0;
48 /* Return the next sibling of node 'node' or zero if no more siblings
49 * at this level of depth in the tree.
52 __prom_getsibling(int node
)
54 return p1275_cmd ("peer", P1275_INOUT(1, 1), node
);
58 prom_getsibling(int node
)
62 if(node
== -1) return 0;
63 sibnode
= __prom_getsibling(node
);
64 if(sibnode
== -1) return 0;
68 /* Return the length in bytes of property 'prop' at node 'node'.
72 prom_getproplen(int node
, char *prop
)
74 if((!node
) || (!prop
)) return -1;
75 return p1275_cmd ("getproplen",
76 P1275_ARG(1,P1275_ARG_IN_STRING
)|
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.
86 prom_getproperty(int node
, char *prop
, char *buffer
, int bufsize
)
90 plen
= prom_getproplen(node
, prop
);
91 if((plen
> bufsize
) || (plen
== 0) || (plen
== -1))
94 /* Ok, things seem all right. */
95 return p1275_cmd ("getprop",
96 P1275_ARG(1,P1275_ARG_IN_STRING
)|
97 P1275_ARG(2,P1275_ARG_OUT_BUF
)|
99 node
, prop
, buffer
, P1275_SIZE(plen
));
103 /* Acquire an integer property and return its value. Returns -1
107 prom_getint(int node
, char *prop
)
111 if(prom_getproperty(node
, prop
, (char *) &intprop
, sizeof(int)) != -1)
117 /* Acquire an integer property, upon error return the passed default
122 prom_getintdefault(int node
, char *property
, int deflt
)
126 retval
= prom_getint(node
, property
);
127 if(retval
== -1) return deflt
;
132 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
134 prom_getbool(int node
, char *prop
)
138 retval
= prom_getproplen(node
, prop
);
139 if(retval
== -1) return 0;
143 /* Acquire a property whose value is a string, returns a null
144 * string on error. The char pointer is the user supplied string
148 prom_getstring(int node
, char *prop
, char *user_buf
, int ubuf_size
)
152 len
= prom_getproperty(node
, prop
, user_buf
, ubuf_size
);
153 if(len
!= -1) return;
159 /* Does the device at node 'node' have name 'name'?
163 prom_nodematch(int node
, char *name
)
166 prom_getproperty(node
, "name", namebuf
, sizeof(namebuf
));
167 if(strcmp(namebuf
, name
) == 0) return 1;
171 /* Search siblings at 'node_start' for a node with name
172 * 'nodename'. Return node if successful, zero if not.
175 prom_searchsiblings(int node_start
, char *nodename
)
179 char promlib_buf
[128];
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
;
193 /* Gets name in the {name@x,yyyyy|name (if no reg)} form */
195 prom_getname (int node
, char *buffer
, int len
)
198 int pci
= 0, ebus
= 0, ide
= 0;
199 struct linux_prom_registers
*reg
;
200 struct linux_prom64_registers reg64
[PROMREG_MAX
];
202 for (sbus
= prom_getparent (node
); sbus
; sbus
= prom_getparent (sbus
)) {
203 i
= prom_getproperty (sbus
, "name", buffer
, len
);
206 if (!strcmp (buffer
, "sbus"))
210 if ((pci
= prom_getparent (node
))) {
211 i
= prom_getproperty (pci
, "name", buffer
, len
);
214 if (!strcmp (buffer
, "pci"))
219 if ((ebus
= prom_getparent (node
))) {
220 i
= prom_getproperty (ebus
, "name", buffer
, len
);
223 if (!strcmp (buffer
, "ebus"))
228 if ((ide
= prom_getparent (node
))) {
229 i
= prom_getproperty (ide
, "name", buffer
, len
);
232 if (!strcmp (buffer
, "ide"))
238 i
= prom_getproperty (node
, "name", buffer
, len
);
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);
250 reg
= (struct linux_prom_registers
*)reg64
;
251 sprintf (buffer
, "@%x,%x", reg
[0].which_io
, (uint
)reg
[0].phys_addr
);
254 reg
= (struct linux_prom_registers
*)reg64
;
255 fn
= (reg
[0].which_io
>> 8) & 0x07;
256 dev
= (reg
[0].which_io
>> 11) & 0x1f;
258 sprintf (buffer
, "@%x,%x", dev
, fn
);
260 sprintf (buffer
, "@%x", dev
);
262 reg
= (struct linux_prom_registers
*)reg64
;
263 sprintf (buffer
, "@%x,%x", reg
[0].which_io
, reg
[0].phys_addr
);
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
);
271 sprintf (buffer
, "@%x,%x",
272 (unsigned int)(reg64
[0].phys_addr
>> 36),
273 (unsigned int)(reg64
[0].phys_addr
));
278 /* Return the first property type for node 'node'.
279 * buffer should be at least 32B in length
282 prom_firstprop(int node
, char *buffer
)
285 if(node
== -1) return buffer
;
286 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B
)|
288 node
, (char *) 0x0, buffer
);
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.
297 prom_nextprop(int node
, char *oprop
, char *buffer
)
305 if (oprop
== buffer
) {
309 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING
)|
310 P1275_ARG(2,P1275_ARG_OUT_32B
)|
312 node
, oprop
, buffer
);
317 prom_finddevice(char *name
)
320 return p1275_cmd ("finddevice", P1275_ARG(0,P1275_ARG_IN_STRING
)|
325 int prom_node_has_property(int node
, char *prop
)
331 prom_nextprop(node
, buf
, buf
);
332 if(!strcmp(buf
, prop
))
338 /* Set property 'pname' at node 'node' to value 'value' which has a length
339 * of 'size' bytes. Return the number of bytes the prom accepted.
342 prom_setprop(int node
, char *pname
, char *value
, int size
)
344 if(size
== 0) return 0;
345 if((pname
== 0) || (value
== 0)) return 0;
347 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING
)|
348 P1275_ARG(2,P1275_ARG_IN_BUF
)|
350 node
, pname
, value
, P1275_SIZE(size
));
354 prom_inst2pkg(int inst
)
358 node
= p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst
);
359 if (node
== -1) return 0;
363 /* Return 'node' assigned to a particular prom 'path'
364 * FIXME: Should work for v0 as well
367 prom_pathtoinode(char *path
)
371 inst
= prom_devopen (path
);
372 if (inst
== 0) return 0;
373 node
= prom_inst2pkg (inst
);
374 prom_devclose (inst
);
375 if (node
== -1) return 0;