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