| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
 | 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 | 
 | 27 | 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 |  | 
 | 37 | __inline__ int | 
 | 38 | 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 |  */ | 
 | 51 | __inline__ int | 
 | 52 | __prom_getsibling(int node) | 
 | 53 | { | 
| David S. Miller | d82ace7 | 2006-02-09 02:52:44 -0800 | [diff] [blame] | 54 | 	return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } | 
 | 56 |  | 
 | 57 | __inline__ int | 
 | 58 | prom_getsibling(int node) | 
 | 59 | { | 
 | 60 | 	int sibnode; | 
 | 61 |  | 
| David S. Miller | d82ace7 | 2006-02-09 02:52:44 -0800 | [diff] [blame] | 62 | 	if (node == -1) | 
 | 63 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | 	sibnode = __prom_getsibling(node); | 
| David S. Miller | d82ace7 | 2006-02-09 02:52:44 -0800 | [diff] [blame] | 65 | 	if (sibnode == -1) | 
 | 66 | 		return 0; | 
 | 67 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | 	return sibnode; | 
 | 69 | } | 
 | 70 |  | 
 | 71 | /* Return the length in bytes of property 'prop' at node 'node'. | 
 | 72 |  * Return -1 on error. | 
 | 73 |  */ | 
 | 74 | __inline__ int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 75 | prom_getproplen(int node, const char *prop) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { | 
 | 77 | 	if((!node) || (!prop)) return -1; | 
 | 78 | 	return p1275_cmd ("getproplen",  | 
 | 79 | 			  P1275_ARG(1,P1275_ARG_IN_STRING)| | 
 | 80 | 			  P1275_INOUT(2, 1),  | 
 | 81 | 			  node, prop); | 
 | 82 | } | 
 | 83 |  | 
 | 84 | /* Acquire a property 'prop' at node 'node' and place it in | 
 | 85 |  * 'buffer' which has a size of 'bufsize'.  If the acquisition | 
 | 86 |  * was successful the length will be returned, else -1 is returned. | 
 | 87 |  */ | 
 | 88 | __inline__ int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 89 | prom_getproperty(int node, const char *prop, char *buffer, int bufsize) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { | 
 | 91 | 	int plen; | 
 | 92 |  | 
 | 93 | 	plen = prom_getproplen(node, prop); | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 94 | 	if ((plen > bufsize) || (plen == 0) || (plen == -1)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | 		return -1; | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 96 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 		/* Ok, things seem all right. */ | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 98 | 		return p1275_cmd(prom_getprop_name,  | 
 | 99 | 				 P1275_ARG(1,P1275_ARG_IN_STRING)| | 
 | 100 | 				 P1275_ARG(2,P1275_ARG_OUT_BUF)| | 
 | 101 | 				 P1275_INOUT(4, 1),  | 
 | 102 | 				 node, prop, buffer, P1275_SIZE(plen)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | 	} | 
 | 104 | } | 
 | 105 |  | 
 | 106 | /* Acquire an integer property and return its value.  Returns -1 | 
 | 107 |  * on failure. | 
 | 108 |  */ | 
 | 109 | __inline__ int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 110 | prom_getint(int node, const char *prop) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { | 
 | 112 | 	int intprop; | 
 | 113 |  | 
 | 114 | 	if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1) | 
 | 115 | 		return intprop; | 
 | 116 |  | 
 | 117 | 	return -1; | 
 | 118 | } | 
 | 119 |  | 
 | 120 | /* Acquire an integer property, upon error return the passed default | 
 | 121 |  * integer. | 
 | 122 |  */ | 
 | 123 |  | 
 | 124 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 125 | prom_getintdefault(int node, const char *property, int deflt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { | 
 | 127 | 	int retval; | 
 | 128 |  | 
 | 129 | 	retval = prom_getint(node, property); | 
 | 130 | 	if(retval == -1) return deflt; | 
 | 131 |  | 
 | 132 | 	return retval; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | /* Acquire a boolean property, 1=TRUE 0=FALSE. */ | 
 | 136 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 137 | prom_getbool(int node, const char *prop) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { | 
 | 139 | 	int retval; | 
 | 140 |  | 
 | 141 | 	retval = prom_getproplen(node, prop); | 
 | 142 | 	if(retval == -1) return 0; | 
 | 143 | 	return 1; | 
 | 144 | } | 
 | 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 |  */ | 
 | 150 | void | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 151 | prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { | 
 | 153 | 	int len; | 
 | 154 |  | 
 | 155 | 	len = prom_getproperty(node, prop, user_buf, ubuf_size); | 
 | 156 | 	if(len != -1) return; | 
 | 157 | 	user_buf[0] = 0; | 
 | 158 | 	return; | 
 | 159 | } | 
 | 160 |  | 
 | 161 |  | 
 | 162 | /* Does the device at node 'node' have name 'name'? | 
 | 163 |  * YES = 1   NO = 0 | 
 | 164 |  */ | 
 | 165 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 166 | prom_nodematch(int node, const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { | 
 | 168 | 	char namebuf[128]; | 
 | 169 | 	prom_getproperty(node, "name", namebuf, sizeof(namebuf)); | 
 | 170 | 	if(strcmp(namebuf, name) == 0) return 1; | 
 | 171 | 	return 0; | 
 | 172 | } | 
 | 173 |  | 
 | 174 | /* Search siblings at 'node_start' for a node with name | 
 | 175 |  * 'nodename'.  Return node if successful, zero if not. | 
 | 176 |  */ | 
 | 177 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 178 | prom_searchsiblings(int node_start, const char *nodename) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { | 
 | 180 |  | 
 | 181 | 	int thisnode, error; | 
 | 182 | 	char promlib_buf[128]; | 
 | 183 |  | 
 | 184 | 	for(thisnode = node_start; thisnode; | 
 | 185 | 	    thisnode=prom_getsibling(thisnode)) { | 
 | 186 | 		error = prom_getproperty(thisnode, "name", promlib_buf, | 
 | 187 | 					 sizeof(promlib_buf)); | 
 | 188 | 		/* Should this ever happen? */ | 
 | 189 | 		if(error == -1) continue; | 
 | 190 | 		if(strcmp(nodename, promlib_buf)==0) return thisnode; | 
 | 191 | 	} | 
 | 192 |  | 
 | 193 | 	return 0; | 
 | 194 | } | 
 | 195 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* Return the first property type for node 'node'. | 
 | 197 |  * buffer should be at least 32B in length | 
 | 198 |  */ | 
 | 199 | __inline__ char * | 
 | 200 | prom_firstprop(int node, char *buffer) | 
 | 201 | { | 
 | 202 | 	*buffer = 0; | 
 | 203 | 	if(node == -1) return buffer; | 
 | 204 | 	p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)| | 
 | 205 | 			       P1275_INOUT(3, 0),  | 
 | 206 | 			       node, (char *) 0x0, buffer); | 
 | 207 | 	return buffer; | 
 | 208 | } | 
 | 209 |  | 
 | 210 | /* Return the property type string after property type 'oprop' | 
 | 211 |  * at node 'node' .  Returns NULL string if no more | 
 | 212 |  * property types for this node. | 
 | 213 |  */ | 
 | 214 | __inline__ char * | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 215 | prom_nextprop(int node, const char *oprop, char *buffer) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { | 
 | 217 | 	char buf[32]; | 
 | 218 |  | 
 | 219 | 	if(node == -1) { | 
 | 220 | 		*buffer = 0; | 
 | 221 | 		return buffer; | 
 | 222 | 	} | 
 | 223 | 	if (oprop == buffer) { | 
 | 224 | 		strcpy (buf, oprop); | 
 | 225 | 		oprop = buf; | 
 | 226 | 	} | 
 | 227 | 	p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)| | 
 | 228 | 				    P1275_ARG(2,P1275_ARG_OUT_32B)| | 
 | 229 | 				    P1275_INOUT(3, 0),  | 
 | 230 | 				    node, oprop, buffer);  | 
 | 231 | 	return buffer; | 
 | 232 | } | 
 | 233 |  | 
 | 234 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 235 | prom_finddevice(const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 237 | 	if (!name) | 
 | 238 | 		return 0; | 
 | 239 | 	return p1275_cmd(prom_finddev_name, | 
 | 240 | 			 P1275_ARG(0,P1275_ARG_IN_STRING)| | 
 | 241 | 			 P1275_INOUT(1, 1),  | 
 | 242 | 			 name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } | 
 | 244 |  | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 245 | int prom_node_has_property(int node, const char *prop) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { | 
 | 247 | 	char buf [32]; | 
 | 248 |          | 
 | 249 | 	*buf = 0; | 
 | 250 | 	do { | 
 | 251 | 		prom_nextprop(node, buf, buf); | 
 | 252 | 		if(!strcmp(buf, prop)) | 
 | 253 | 			return 1; | 
 | 254 | 	} while (*buf); | 
 | 255 | 	return 0; | 
 | 256 | } | 
 | 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 | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 262 | prom_setprop(int node, const char *pname, char *value, int size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { | 
 | 264 | 	if(size == 0) return 0; | 
 | 265 | 	if((pname == 0) || (value == 0)) return 0; | 
 | 266 | 	 | 
 | 267 | 	return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)| | 
 | 268 | 					  P1275_ARG(2,P1275_ARG_IN_BUF)| | 
 | 269 | 					  P1275_INOUT(4, 1),  | 
 | 270 | 					  node, pname, value, P1275_SIZE(size)); | 
 | 271 | } | 
 | 272 |  | 
 | 273 | __inline__ int | 
 | 274 | prom_inst2pkg(int inst) | 
 | 275 | { | 
 | 276 | 	int node; | 
 | 277 | 	 | 
 | 278 | 	node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst); | 
 | 279 | 	if (node == -1) return 0; | 
 | 280 | 	return node; | 
 | 281 | } | 
 | 282 |  | 
 | 283 | /* Return 'node' assigned to a particular prom 'path' | 
 | 284 |  * FIXME: Should work for v0 as well | 
 | 285 |  */ | 
 | 286 | int | 
| David S. Miller | bff06d5 | 2005-09-22 20:11:33 -0700 | [diff] [blame] | 287 | prom_pathtoinode(const char *path) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { | 
 | 289 | 	int node, inst; | 
 | 290 |  | 
 | 291 | 	inst = prom_devopen (path); | 
 | 292 | 	if (inst == 0) return 0; | 
 | 293 | 	node = prom_inst2pkg (inst); | 
 | 294 | 	prom_devclose (inst); | 
 | 295 | 	if (node == -1) return 0; | 
 | 296 | 	return node; | 
 | 297 | } |