Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
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> | |
b3e13fbe | 16 | #include <asm/ldc.h> |
1da177e4 LT |
17 | |
18 | /* Return the child of node 'node' or zero if no this node has no | |
19 | * direct descendent. | |
20 | */ | |
21 | __inline__ int | |
22 | __prom_getchild(int node) | |
23 | { | |
24 | return p1275_cmd ("child", P1275_INOUT(1, 1), node); | |
25 | } | |
26 | ||
27 | __inline__ int | |
28 | prom_getchild(int node) | |
29 | { | |
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 | } | |
37 | ||
38 | __inline__ int | |
39 | prom_getparent(int node) | |
40 | { | |
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 | */ | |
52 | __inline__ int | |
53 | __prom_getsibling(int node) | |
54 | { | |
d82ace7d | 55 | return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node); |
1da177e4 LT |
56 | } |
57 | ||
58 | __inline__ int | |
59 | prom_getsibling(int node) | |
60 | { | |
61 | int sibnode; | |
62 | ||
d82ace7d DM |
63 | if (node == -1) |
64 | return 0; | |
1da177e4 | 65 | sibnode = __prom_getsibling(node); |
d82ace7d DM |
66 | if (sibnode == -1) |
67 | return 0; | |
68 | ||
1da177e4 LT |
69 | return sibnode; |
70 | } | |
71 | ||
72 | /* Return the length in bytes of property 'prop' at node 'node'. | |
73 | * Return -1 on error. | |
74 | */ | |
75 | __inline__ int | |
bff06d55 | 76 | prom_getproplen(int node, const char *prop) |
1da177e4 LT |
77 | { |
78 | if((!node) || (!prop)) return -1; | |
79 | return p1275_cmd ("getproplen", | |
80 | P1275_ARG(1,P1275_ARG_IN_STRING)| | |
81 | P1275_INOUT(2, 1), | |
82 | node, prop); | |
83 | } | |
84 | ||
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 | */ | |
89 | __inline__ int | |
bff06d55 | 90 | prom_getproperty(int node, const char *prop, char *buffer, int bufsize) |
1da177e4 LT |
91 | { |
92 | int plen; | |
93 | ||
94 | plen = prom_getproplen(node, prop); | |
bff06d55 | 95 | if ((plen > bufsize) || (plen == 0) || (plen == -1)) { |
1da177e4 | 96 | return -1; |
bff06d55 | 97 | } else { |
1da177e4 | 98 | /* Ok, things seem all right. */ |
bff06d55 DM |
99 | 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)); | |
1da177e4 LT |
104 | } |
105 | } | |
106 | ||
107 | /* Acquire an integer property and return its value. Returns -1 | |
108 | * on failure. | |
109 | */ | |
110 | __inline__ int | |
bff06d55 | 111 | prom_getint(int node, const char *prop) |
1da177e4 LT |
112 | { |
113 | int intprop; | |
114 | ||
115 | if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1) | |
116 | return intprop; | |
117 | ||
118 | return -1; | |
119 | } | |
120 | ||
121 | /* Acquire an integer property, upon error return the passed default | |
122 | * integer. | |
123 | */ | |
124 | ||
125 | int | |
bff06d55 | 126 | prom_getintdefault(int node, const char *property, int deflt) |
1da177e4 LT |
127 | { |
128 | int retval; | |
129 | ||
130 | retval = prom_getint(node, property); | |
131 | if(retval == -1) return deflt; | |
132 | ||
133 | return retval; | |
134 | } | |
135 | ||
136 | /* Acquire a boolean property, 1=TRUE 0=FALSE. */ | |
137 | int | |
bff06d55 | 138 | prom_getbool(int node, const char *prop) |
1da177e4 LT |
139 | { |
140 | int retval; | |
141 | ||
142 | retval = prom_getproplen(node, prop); | |
143 | if(retval == -1) return 0; | |
144 | return 1; | |
145 | } | |
146 | ||
147 | /* Acquire a property whose value is a string, returns a null | |
148 | * string on error. The char pointer is the user supplied string | |
149 | * buffer. | |
150 | */ | |
151 | void | |
bff06d55 | 152 | prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size) |
1da177e4 LT |
153 | { |
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 | } | |
161 | ||
162 | ||
163 | /* Does the device at node 'node' have name 'name'? | |
164 | * YES = 1 NO = 0 | |
165 | */ | |
166 | int | |
bff06d55 | 167 | prom_nodematch(int node, const char *name) |
1da177e4 LT |
168 | { |
169 | char namebuf[128]; | |
170 | prom_getproperty(node, "name", namebuf, sizeof(namebuf)); | |
171 | if(strcmp(namebuf, name) == 0) return 1; | |
172 | return 0; | |
173 | } | |
174 | ||
175 | /* Search siblings at 'node_start' for a node with name | |
176 | * 'nodename'. Return node if successful, zero if not. | |
177 | */ | |
178 | int | |
bff06d55 | 179 | prom_searchsiblings(int node_start, const char *nodename) |
1da177e4 LT |
180 | { |
181 | ||
182 | int thisnode, error; | |
183 | char promlib_buf[128]; | |
184 | ||
185 | for(thisnode = node_start; thisnode; | |
186 | thisnode=prom_getsibling(thisnode)) { | |
187 | error = prom_getproperty(thisnode, "name", promlib_buf, | |
188 | sizeof(promlib_buf)); | |
189 | /* Should this ever happen? */ | |
190 | if(error == -1) continue; | |
191 | if(strcmp(nodename, promlib_buf)==0) return thisnode; | |
192 | } | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
1da177e4 LT |
197 | /* Return the first property type for node 'node'. |
198 | * buffer should be at least 32B in length | |
199 | */ | |
200 | __inline__ char * | |
201 | prom_firstprop(int node, char *buffer) | |
202 | { | |
203 | *buffer = 0; | |
204 | if(node == -1) return buffer; | |
205 | p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)| | |
206 | P1275_INOUT(3, 0), | |
207 | node, (char *) 0x0, buffer); | |
208 | return buffer; | |
209 | } | |
210 | ||
211 | /* Return the property type string after property type 'oprop' | |
212 | * at node 'node' . Returns NULL string if no more | |
213 | * property types for this node. | |
214 | */ | |
215 | __inline__ char * | |
bff06d55 | 216 | prom_nextprop(int node, const char *oprop, char *buffer) |
1da177e4 LT |
217 | { |
218 | char buf[32]; | |
219 | ||
220 | if(node == -1) { | |
221 | *buffer = 0; | |
222 | return buffer; | |
223 | } | |
224 | if (oprop == buffer) { | |
225 | strcpy (buf, oprop); | |
226 | oprop = buf; | |
227 | } | |
228 | p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)| | |
229 | P1275_ARG(2,P1275_ARG_OUT_32B)| | |
230 | P1275_INOUT(3, 0), | |
231 | node, oprop, buffer); | |
232 | return buffer; | |
233 | } | |
234 | ||
235 | int | |
bff06d55 | 236 | prom_finddevice(const char *name) |
1da177e4 | 237 | { |
bff06d55 DM |
238 | if (!name) |
239 | return 0; | |
240 | return p1275_cmd(prom_finddev_name, | |
241 | P1275_ARG(0,P1275_ARG_IN_STRING)| | |
242 | P1275_INOUT(1, 1), | |
243 | name); | |
1da177e4 LT |
244 | } |
245 | ||
bff06d55 | 246 | int prom_node_has_property(int node, const char *prop) |
1da177e4 LT |
247 | { |
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 | } | |
258 | ||
259 | /* Set property 'pname' at node 'node' to value 'value' which has a length | |
260 | * of 'size' bytes. Return the number of bytes the prom accepted. | |
261 | */ | |
262 | int | |
bff06d55 | 263 | prom_setprop(int node, const char *pname, char *value, int size) |
1da177e4 | 264 | { |
b3e13fbe DM |
265 | if (size == 0) |
266 | return 0; | |
267 | if ((pname == 0) || (value == 0)) | |
268 | return 0; | |
1da177e4 | 269 | |
b3e13fbe DM |
270 | #ifdef CONFIG_SUN_LDOMS |
271 | if (ldom_domaining_enabled) { | |
272 | ldom_set_var(pname, value); | |
273 | return 0; | |
274 | } | |
275 | #endif | |
1da177e4 LT |
276 | return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)| |
277 | P1275_ARG(2,P1275_ARG_IN_BUF)| | |
278 | P1275_INOUT(4, 1), | |
279 | node, pname, value, P1275_SIZE(size)); | |
280 | } | |
281 | ||
282 | __inline__ int | |
283 | prom_inst2pkg(int inst) | |
284 | { | |
285 | int node; | |
286 | ||
287 | node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst); | |
288 | if (node == -1) return 0; | |
289 | return node; | |
290 | } | |
291 | ||
292 | /* Return 'node' assigned to a particular prom 'path' | |
293 | * FIXME: Should work for v0 as well | |
294 | */ | |
295 | int | |
bff06d55 | 296 | prom_pathtoinode(const char *path) |
1da177e4 LT |
297 | { |
298 | int node, inst; | |
299 | ||
300 | inst = prom_devopen (path); | |
301 | if (inst == 0) return 0; | |
302 | node = prom_inst2pkg (inst); | |
303 | prom_devclose (inst); | |
304 | if (node == -1) return 0; | |
305 | return node; | |
306 | } | |
c73fcc84 DM |
307 | |
308 | int prom_ihandle2path(int handle, char *buffer, int bufsize) | |
309 | { | |
310 | return p1275_cmd("instance-to-path", | |
311 | P1275_ARG(1,P1275_ARG_OUT_BUF)| | |
312 | P1275_INOUT(3, 1), | |
313 | handle, buffer, P1275_SIZE(bufsize)); | |
314 | } |