Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * tree.c: Basic device tree traversal/scanning for the Linux
4 * prom library.
5 *
6 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
7 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 */
9
10#include <linux/string.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/module.h>
15
16#include <asm/openprom.h>
17#include <asm/oplib.h>
18#include <asm/ldc.h>
19
20static phandle prom_node_to_node(const char *type, phandle node)
21{
22 unsigned long args[5];
23
24 args[0] = (unsigned long) type;
25 args[1] = 1;
26 args[2] = 1;
27 args[3] = (unsigned int) node;
28 args[4] = (unsigned long) -1;
29
30 p1275_cmd_direct(args);
31
32 return (phandle) args[4];
33}
34
35/* Return the child of node 'node' or zero if no this node has no
36 * direct descendent.
37 */
38inline phandle __prom_getchild(phandle node)
39{
40 return prom_node_to_node("child", node);
41}
42
43phandle prom_getchild(phandle node)
44{
45 phandle cnode;
46
47 if ((s32)node == -1)
48 return 0;
49 cnode = __prom_getchild(node);
50 if ((s32)cnode == -1)
51 return 0;
52 return cnode;
53}
54EXPORT_SYMBOL(prom_getchild);
55
56inline phandle prom_getparent(phandle node)
57{
58 phandle cnode;
59
60 if ((s32)node == -1)
61 return 0;
62 cnode = prom_node_to_node("parent", node);
63 if ((s32)cnode == -1)
64 return 0;
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 */
71inline phandle __prom_getsibling(phandle node)
72{
73 return prom_node_to_node(prom_peer_name, node);
74}
75
76phandle prom_getsibling(phandle node)
77{
78 phandle sibnode;
79
80 if ((s32)node == -1)
81 return 0;
82 sibnode = __prom_getsibling(node);
83 if ((s32)sibnode == -1)
84 return 0;
85
86 return sibnode;
87}
88EXPORT_SYMBOL(prom_getsibling);
89
90/* Return the length in bytes of property 'prop' at node 'node'.
91 * Return -1 on error.
92 */
93int prom_getproplen(phandle node, const char *prop)
94{
95 unsigned long args[6];
96
97 if (!node || !prop)
98 return -1;
99
100 args[0] = (unsigned long) "getproplen";
101 args[1] = 2;
102 args[2] = 1;
103 args[3] = (unsigned int) node;
104 args[4] = (unsigned long) prop;
105 args[5] = (unsigned long) -1;
106
107 p1275_cmd_direct(args);
108
109 return (int) args[5];
110}
111EXPORT_SYMBOL(prom_getproplen);
112
113/* Acquire a property 'prop' at node 'node' and place it in
114 * 'buffer' which has a size of 'bufsize'. If the acquisition
115 * was successful the length will be returned, else -1 is returned.
116 */
117int prom_getproperty(phandle node, const char *prop,
118 char *buffer, int bufsize)
119{
120 unsigned long args[8];
121 int plen;
122
123 plen = prom_getproplen(node, prop);
124 if ((plen > bufsize) || (plen == 0) || (plen == -1))
125 return -1;
126
127 args[0] = (unsigned long) prom_getprop_name;
128 args[1] = 4;
129 args[2] = 1;
130 args[3] = (unsigned int) node;
131 args[4] = (unsigned long) prop;
132 args[5] = (unsigned long) buffer;
133 args[6] = bufsize;
134 args[7] = (unsigned long) -1;
135
136 p1275_cmd_direct(args);
137
138 return (int) args[7];
139}
140EXPORT_SYMBOL(prom_getproperty);
141
142/* Acquire an integer property and return its value. Returns -1
143 * on failure.
144 */
145int prom_getint(phandle node, const char *prop)
146{
147 int intprop;
148
149 if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
150 return intprop;
151
152 return -1;
153}
154EXPORT_SYMBOL(prom_getint);
155
156/* Acquire an integer property, upon error return the passed default
157 * integer.
158 */
159
160int prom_getintdefault(phandle node, const char *property, int deflt)
161{
162 int retval;
163
164 retval = prom_getint(node, property);
165 if (retval == -1)
166 return deflt;
167
168 return retval;
169}
170EXPORT_SYMBOL(prom_getintdefault);
171
172/* Acquire a boolean property, 1=TRUE 0=FALSE. */
173int prom_getbool(phandle node, const char *prop)
174{
175 int retval;
176
177 retval = prom_getproplen(node, prop);
178 if (retval == -1)
179 return 0;
180 return 1;
181}
182EXPORT_SYMBOL(prom_getbool);
183
184/* Acquire a property whose value is a string, returns a null
185 * string on error. The char pointer is the user supplied string
186 * buffer.
187 */
188void prom_getstring(phandle node, const char *prop, char *user_buf,
189 int ubuf_size)
190{
191 int len;
192
193 len = prom_getproperty(node, prop, user_buf, ubuf_size);
194 if (len != -1)
195 return;
196 user_buf[0] = 0;
197}
198EXPORT_SYMBOL(prom_getstring);
199
200/* Does the device at node 'node' have name 'name'?
201 * YES = 1 NO = 0
202 */
203int prom_nodematch(phandle node, const char *name)
204{
205 char namebuf[128];
206 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
207 if (strcmp(namebuf, name) == 0)
208 return 1;
209 return 0;
210}
211
212/* Search siblings at 'node_start' for a node with name
213 * 'nodename'. Return node if successful, zero if not.
214 */
215phandle prom_searchsiblings(phandle node_start, const char *nodename)
216{
217 phandle thisnode;
218 int error;
219 char promlib_buf[128];
220
221 for(thisnode = node_start; thisnode;
222 thisnode=prom_getsibling(thisnode)) {
223 error = prom_getproperty(thisnode, "name", promlib_buf,
224 sizeof(promlib_buf));
225 /* Should this ever happen? */
226 if(error == -1) continue;
227 if(strcmp(nodename, promlib_buf)==0) return thisnode;
228 }
229
230 return 0;
231}
232EXPORT_SYMBOL(prom_searchsiblings);
233
234static const char *prom_nextprop_name = "nextprop";
235
236/* Return the first property type for node 'node'.
237 * buffer should be at least 32B in length
238 */
239char *prom_firstprop(phandle node, char *buffer)
240{
241 unsigned long args[7];
242
243 *buffer = 0;
244 if ((s32)node == -1)
245 return buffer;
246
247 args[0] = (unsigned long) prom_nextprop_name;
248 args[1] = 3;
249 args[2] = 1;
250 args[3] = (unsigned int) node;
251 args[4] = 0;
252 args[5] = (unsigned long) buffer;
253 args[6] = (unsigned long) -1;
254
255 p1275_cmd_direct(args);
256
257 return buffer;
258}
259EXPORT_SYMBOL(prom_firstprop);
260
261/* Return the property type string after property type 'oprop'
262 * at node 'node' . Returns NULL string if no more
263 * property types for this node.
264 */
265char *prom_nextprop(phandle node, const char *oprop, char *buffer)
266{
267 unsigned long args[7];
268 char buf[32];
269
270 if ((s32)node == -1) {
271 *buffer = 0;
272 return buffer;
273 }
274 if (oprop == buffer) {
275 strcpy (buf, oprop);
276 oprop = buf;
277 }
278
279 args[0] = (unsigned long) prom_nextprop_name;
280 args[1] = 3;
281 args[2] = 1;
282 args[3] = (unsigned int) node;
283 args[4] = (unsigned long) oprop;
284 args[5] = (unsigned long) buffer;
285 args[6] = (unsigned long) -1;
286
287 p1275_cmd_direct(args);
288
289 return buffer;
290}
291EXPORT_SYMBOL(prom_nextprop);
292
293phandle prom_finddevice(const char *name)
294{
295 unsigned long args[5];
296
297 if (!name)
298 return 0;
299 args[0] = (unsigned long) "finddevice";
300 args[1] = 1;
301 args[2] = 1;
302 args[3] = (unsigned long) name;
303 args[4] = (unsigned long) -1;
304
305 p1275_cmd_direct(args);
306
307 return (int) args[4];
308}
309EXPORT_SYMBOL(prom_finddevice);
310
311int prom_node_has_property(phandle node, const char *prop)
312{
313 char buf [32];
314
315 *buf = 0;
316 do {
317 prom_nextprop(node, buf, buf);
318 if (!strcmp(buf, prop))
319 return 1;
320 } while (*buf);
321 return 0;
322}
323EXPORT_SYMBOL(prom_node_has_property);
324
325/* Set property 'pname' at node 'node' to value 'value' which has a length
326 * of 'size' bytes. Return the number of bytes the prom accepted.
327 */
328int
329prom_setprop(phandle node, const char *pname, char *value, int size)
330{
331 unsigned long args[8];
332
333 if (size == 0)
334 return 0;
335 if ((pname == 0) || (value == 0))
336 return 0;
337
338#ifdef CONFIG_SUN_LDOMS
339 if (ldom_domaining_enabled) {
340 ldom_set_var(pname, value);
341 return 0;
342 }
343#endif
344 args[0] = (unsigned long) "setprop";
345 args[1] = 4;
346 args[2] = 1;
347 args[3] = (unsigned int) node;
348 args[4] = (unsigned long) pname;
349 args[5] = (unsigned long) value;
350 args[6] = size;
351 args[7] = (unsigned long) -1;
352
353 p1275_cmd_direct(args);
354
355 return (int) args[7];
356}
357EXPORT_SYMBOL(prom_setprop);
358
359inline phandle prom_inst2pkg(int inst)
360{
361 unsigned long args[5];
362 phandle node;
363
364 args[0] = (unsigned long) "instance-to-package";
365 args[1] = 1;
366 args[2] = 1;
367 args[3] = (unsigned int) inst;
368 args[4] = (unsigned long) -1;
369
370 p1275_cmd_direct(args);
371
372 node = (int) args[4];
373 if ((s32)node == -1)
374 return 0;
375 return node;
376}
377
378int prom_ihandle2path(int handle, char *buffer, int bufsize)
379{
380 unsigned long args[7];
381
382 args[0] = (unsigned long) "instance-to-path";
383 args[1] = 3;
384 args[2] = 1;
385 args[3] = (unsigned int) handle;
386 args[4] = (unsigned long) buffer;
387 args[5] = bufsize;
388 args[6] = (unsigned long) -1;
389
390 p1275_cmd_direct(args);
391
392 return (int) args[6];
393}
1/*
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#include <linux/module.h>
14
15#include <asm/openprom.h>
16#include <asm/oplib.h>
17#include <asm/ldc.h>
18
19static phandle prom_node_to_node(const char *type, phandle node)
20{
21 unsigned long args[5];
22
23 args[0] = (unsigned long) type;
24 args[1] = 1;
25 args[2] = 1;
26 args[3] = (unsigned int) node;
27 args[4] = (unsigned long) -1;
28
29 p1275_cmd_direct(args);
30
31 return (phandle) args[4];
32}
33
34/* Return the child of node 'node' or zero if no this node has no
35 * direct descendent.
36 */
37inline phandle __prom_getchild(phandle node)
38{
39 return prom_node_to_node("child", node);
40}
41
42phandle prom_getchild(phandle node)
43{
44 phandle cnode;
45
46 if ((s32)node == -1)
47 return 0;
48 cnode = __prom_getchild(node);
49 if ((s32)cnode == -1)
50 return 0;
51 return cnode;
52}
53EXPORT_SYMBOL(prom_getchild);
54
55inline phandle prom_getparent(phandle node)
56{
57 phandle cnode;
58
59 if ((s32)node == -1)
60 return 0;
61 cnode = prom_node_to_node("parent", node);
62 if ((s32)cnode == -1)
63 return 0;
64 return cnode;
65}
66
67/* Return the next sibling of node 'node' or zero if no more siblings
68 * at this level of depth in the tree.
69 */
70inline phandle __prom_getsibling(phandle node)
71{
72 return prom_node_to_node(prom_peer_name, node);
73}
74
75phandle prom_getsibling(phandle node)
76{
77 phandle sibnode;
78
79 if ((s32)node == -1)
80 return 0;
81 sibnode = __prom_getsibling(node);
82 if ((s32)sibnode == -1)
83 return 0;
84
85 return sibnode;
86}
87EXPORT_SYMBOL(prom_getsibling);
88
89/* Return the length in bytes of property 'prop' at node 'node'.
90 * Return -1 on error.
91 */
92int prom_getproplen(phandle node, const char *prop)
93{
94 unsigned long args[6];
95
96 if (!node || !prop)
97 return -1;
98
99 args[0] = (unsigned long) "getproplen";
100 args[1] = 2;
101 args[2] = 1;
102 args[3] = (unsigned int) node;
103 args[4] = (unsigned long) prop;
104 args[5] = (unsigned long) -1;
105
106 p1275_cmd_direct(args);
107
108 return (int) args[5];
109}
110EXPORT_SYMBOL(prom_getproplen);
111
112/* Acquire a property 'prop' at node 'node' and place it in
113 * 'buffer' which has a size of 'bufsize'. If the acquisition
114 * was successful the length will be returned, else -1 is returned.
115 */
116int prom_getproperty(phandle node, const char *prop,
117 char *buffer, int bufsize)
118{
119 unsigned long args[8];
120 int plen;
121
122 plen = prom_getproplen(node, prop);
123 if ((plen > bufsize) || (plen == 0) || (plen == -1))
124 return -1;
125
126 args[0] = (unsigned long) prom_getprop_name;
127 args[1] = 4;
128 args[2] = 1;
129 args[3] = (unsigned int) node;
130 args[4] = (unsigned long) prop;
131 args[5] = (unsigned long) buffer;
132 args[6] = bufsize;
133 args[7] = (unsigned long) -1;
134
135 p1275_cmd_direct(args);
136
137 return (int) args[7];
138}
139EXPORT_SYMBOL(prom_getproperty);
140
141/* Acquire an integer property and return its value. Returns -1
142 * on failure.
143 */
144int prom_getint(phandle node, const char *prop)
145{
146 int intprop;
147
148 if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
149 return intprop;
150
151 return -1;
152}
153EXPORT_SYMBOL(prom_getint);
154
155/* Acquire an integer property, upon error return the passed default
156 * integer.
157 */
158
159int prom_getintdefault(phandle node, const char *property, int deflt)
160{
161 int retval;
162
163 retval = prom_getint(node, property);
164 if (retval == -1)
165 return deflt;
166
167 return retval;
168}
169EXPORT_SYMBOL(prom_getintdefault);
170
171/* Acquire a boolean property, 1=TRUE 0=FALSE. */
172int prom_getbool(phandle node, const char *prop)
173{
174 int retval;
175
176 retval = prom_getproplen(node, prop);
177 if (retval == -1)
178 return 0;
179 return 1;
180}
181EXPORT_SYMBOL(prom_getbool);
182
183/* Acquire a property whose value is a string, returns a null
184 * string on error. The char pointer is the user supplied string
185 * buffer.
186 */
187void prom_getstring(phandle node, const char *prop, char *user_buf,
188 int ubuf_size)
189{
190 int len;
191
192 len = prom_getproperty(node, prop, user_buf, ubuf_size);
193 if (len != -1)
194 return;
195 user_buf[0] = 0;
196}
197EXPORT_SYMBOL(prom_getstring);
198
199/* Does the device at node 'node' have name 'name'?
200 * YES = 1 NO = 0
201 */
202int prom_nodematch(phandle node, const char *name)
203{
204 char namebuf[128];
205 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
206 if (strcmp(namebuf, name) == 0)
207 return 1;
208 return 0;
209}
210
211/* Search siblings at 'node_start' for a node with name
212 * 'nodename'. Return node if successful, zero if not.
213 */
214phandle prom_searchsiblings(phandle node_start, const char *nodename)
215{
216 phandle thisnode;
217 int error;
218 char promlib_buf[128];
219
220 for(thisnode = node_start; thisnode;
221 thisnode=prom_getsibling(thisnode)) {
222 error = prom_getproperty(thisnode, "name", promlib_buf,
223 sizeof(promlib_buf));
224 /* Should this ever happen? */
225 if(error == -1) continue;
226 if(strcmp(nodename, promlib_buf)==0) return thisnode;
227 }
228
229 return 0;
230}
231EXPORT_SYMBOL(prom_searchsiblings);
232
233static const char *prom_nextprop_name = "nextprop";
234
235/* Return the first property type for node 'node'.
236 * buffer should be at least 32B in length
237 */
238char *prom_firstprop(phandle node, char *buffer)
239{
240 unsigned long args[7];
241
242 *buffer = 0;
243 if ((s32)node == -1)
244 return buffer;
245
246 args[0] = (unsigned long) prom_nextprop_name;
247 args[1] = 3;
248 args[2] = 1;
249 args[3] = (unsigned int) node;
250 args[4] = 0;
251 args[5] = (unsigned long) buffer;
252 args[6] = (unsigned long) -1;
253
254 p1275_cmd_direct(args);
255
256 return buffer;
257}
258EXPORT_SYMBOL(prom_firstprop);
259
260/* Return the property type string after property type 'oprop'
261 * at node 'node' . Returns NULL string if no more
262 * property types for this node.
263 */
264char *prom_nextprop(phandle node, const char *oprop, char *buffer)
265{
266 unsigned long args[7];
267 char buf[32];
268
269 if ((s32)node == -1) {
270 *buffer = 0;
271 return buffer;
272 }
273 if (oprop == buffer) {
274 strcpy (buf, oprop);
275 oprop = buf;
276 }
277
278 args[0] = (unsigned long) prom_nextprop_name;
279 args[1] = 3;
280 args[2] = 1;
281 args[3] = (unsigned int) node;
282 args[4] = (unsigned long) oprop;
283 args[5] = (unsigned long) buffer;
284 args[6] = (unsigned long) -1;
285
286 p1275_cmd_direct(args);
287
288 return buffer;
289}
290EXPORT_SYMBOL(prom_nextprop);
291
292phandle prom_finddevice(const char *name)
293{
294 unsigned long args[5];
295
296 if (!name)
297 return 0;
298 args[0] = (unsigned long) "finddevice";
299 args[1] = 1;
300 args[2] = 1;
301 args[3] = (unsigned long) name;
302 args[4] = (unsigned long) -1;
303
304 p1275_cmd_direct(args);
305
306 return (int) args[4];
307}
308EXPORT_SYMBOL(prom_finddevice);
309
310int prom_node_has_property(phandle node, const char *prop)
311{
312 char buf [32];
313
314 *buf = 0;
315 do {
316 prom_nextprop(node, buf, buf);
317 if (!strcmp(buf, prop))
318 return 1;
319 } while (*buf);
320 return 0;
321}
322EXPORT_SYMBOL(prom_node_has_property);
323
324/* Set property 'pname' at node 'node' to value 'value' which has a length
325 * of 'size' bytes. Return the number of bytes the prom accepted.
326 */
327int
328prom_setprop(phandle node, const char *pname, char *value, int size)
329{
330 unsigned long args[8];
331
332 if (size == 0)
333 return 0;
334 if ((pname == 0) || (value == 0))
335 return 0;
336
337#ifdef CONFIG_SUN_LDOMS
338 if (ldom_domaining_enabled) {
339 ldom_set_var(pname, value);
340 return 0;
341 }
342#endif
343 args[0] = (unsigned long) "setprop";
344 args[1] = 4;
345 args[2] = 1;
346 args[3] = (unsigned int) node;
347 args[4] = (unsigned long) pname;
348 args[5] = (unsigned long) value;
349 args[6] = size;
350 args[7] = (unsigned long) -1;
351
352 p1275_cmd_direct(args);
353
354 return (int) args[7];
355}
356EXPORT_SYMBOL(prom_setprop);
357
358inline phandle prom_inst2pkg(int inst)
359{
360 unsigned long args[5];
361 phandle node;
362
363 args[0] = (unsigned long) "instance-to-package";
364 args[1] = 1;
365 args[2] = 1;
366 args[3] = (unsigned int) inst;
367 args[4] = (unsigned long) -1;
368
369 p1275_cmd_direct(args);
370
371 node = (int) args[4];
372 if ((s32)node == -1)
373 return 0;
374 return node;
375}
376
377int prom_ihandle2path(int handle, char *buffer, int bufsize)
378{
379 unsigned long args[7];
380
381 args[0] = (unsigned long) "instance-to-path";
382 args[1] = 3;
383 args[2] = 1;
384 args[3] = (unsigned int) handle;
385 args[4] = (unsigned long) buffer;
386 args[5] = bufsize;
387 args[6] = (unsigned long) -1;
388
389 p1275_cmd_direct(args);
390
391 return (int) args[6];
392}