Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/string.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/ctype.h> |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <asm/openprom.h> |
| 16 | #include <asm/oplib.h> |
| 17 | |
| 18 | extern void restore_current(void); |
| 19 | |
| 20 | static char promlib_buf[128]; |
| 21 | |
| 22 | /* Internal version of prom_getchild that does not alter return values. */ |
Sam Ravnborg | 1f42be4 | 2011-01-01 12:53:11 +0000 | [diff] [blame^] | 23 | static phandle __prom_getchild(phandle node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | unsigned long flags; |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 26 | phandle cnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | spin_lock_irqsave(&prom_lock, flags); |
| 29 | cnode = prom_nodeops->no_child(node); |
| 30 | restore_current(); |
| 31 | spin_unlock_irqrestore(&prom_lock, flags); |
| 32 | |
| 33 | return cnode; |
| 34 | } |
| 35 | |
| 36 | /* Return the child of node 'node' or zero if no this node has no |
| 37 | * direct descendent. |
| 38 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 39 | phandle prom_getchild(phandle node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 41 | phandle cnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (node == -1) |
| 44 | return 0; |
| 45 | |
| 46 | cnode = __prom_getchild(node); |
| 47 | if (cnode == 0 || cnode == -1) |
| 48 | return 0; |
| 49 | |
| 50 | return cnode; |
| 51 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 52 | EXPORT_SYMBOL(prom_getchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* Internal version of prom_getsibling that does not alter return values. */ |
Sam Ravnborg | 1f42be4 | 2011-01-01 12:53:11 +0000 | [diff] [blame^] | 55 | static phandle __prom_getsibling(phandle node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | unsigned long flags; |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 58 | phandle cnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | spin_lock_irqsave(&prom_lock, flags); |
| 61 | cnode = prom_nodeops->no_nextnode(node); |
| 62 | restore_current(); |
| 63 | spin_unlock_irqrestore(&prom_lock, flags); |
| 64 | |
| 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 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 71 | phandle prom_getsibling(phandle node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 73 | phandle sibnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | if (node == -1) |
| 76 | return 0; |
| 77 | |
| 78 | sibnode = __prom_getsibling(node); |
| 79 | if (sibnode == 0 || sibnode == -1) |
| 80 | return 0; |
| 81 | |
| 82 | return sibnode; |
| 83 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 84 | EXPORT_SYMBOL(prom_getsibling); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* Return the length in bytes of property 'prop' at node 'node'. |
| 87 | * Return -1 on error. |
| 88 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 89 | int prom_getproplen(phandle node, const char *prop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | int ret; |
| 92 | unsigned long flags; |
| 93 | |
| 94 | if((!node) || (!prop)) |
| 95 | return -1; |
| 96 | |
| 97 | spin_lock_irqsave(&prom_lock, flags); |
| 98 | ret = prom_nodeops->no_proplen(node, prop); |
| 99 | restore_current(); |
| 100 | spin_unlock_irqrestore(&prom_lock, flags); |
| 101 | return ret; |
| 102 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 103 | EXPORT_SYMBOL(prom_getproplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /* Acquire a property 'prop' at node 'node' and place it in |
| 106 | * 'buffer' which has a size of 'bufsize'. If the acquisition |
| 107 | * was successful the length will be returned, else -1 is returned. |
| 108 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 109 | int prom_getproperty(phandle node, const char *prop, char *buffer, int bufsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | int plen, ret; |
| 112 | unsigned long flags; |
| 113 | |
| 114 | plen = prom_getproplen(node, prop); |
| 115 | if((plen > bufsize) || (plen == 0) || (plen == -1)) |
| 116 | return -1; |
| 117 | /* Ok, things seem all right. */ |
| 118 | spin_lock_irqsave(&prom_lock, flags); |
| 119 | ret = prom_nodeops->no_getprop(node, prop, buffer); |
| 120 | restore_current(); |
| 121 | spin_unlock_irqrestore(&prom_lock, flags); |
| 122 | return ret; |
| 123 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 124 | EXPORT_SYMBOL(prom_getproperty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* Acquire an integer property and return its value. Returns -1 |
| 127 | * on failure. |
| 128 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 129 | int prom_getint(phandle node, char *prop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | static int intprop; |
| 132 | |
| 133 | if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1) |
| 134 | return intprop; |
| 135 | |
| 136 | return -1; |
| 137 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 138 | EXPORT_SYMBOL(prom_getint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* Acquire an integer property, upon error return the passed default |
| 141 | * integer. |
| 142 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 143 | int prom_getintdefault(phandle node, char *property, int deflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | int retval; |
| 146 | |
| 147 | retval = prom_getint(node, property); |
| 148 | if(retval == -1) return deflt; |
| 149 | |
| 150 | return retval; |
| 151 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 152 | EXPORT_SYMBOL(prom_getintdefault); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /* Acquire a boolean property, 1=TRUE 0=FALSE. */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 155 | int prom_getbool(phandle node, char *prop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | int retval; |
| 158 | |
| 159 | retval = prom_getproplen(node, prop); |
| 160 | if(retval == -1) return 0; |
| 161 | return 1; |
| 162 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 163 | EXPORT_SYMBOL(prom_getbool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* Acquire a property whose value is a string, returns a null |
| 166 | * string on error. The char pointer is the user supplied string |
| 167 | * buffer. |
| 168 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 169 | void prom_getstring(phandle node, char *prop, char *user_buf, int ubuf_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | int len; |
| 172 | |
| 173 | len = prom_getproperty(node, prop, user_buf, ubuf_size); |
| 174 | if(len != -1) return; |
| 175 | user_buf[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL(prom_getstring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* Search siblings at 'node_start' for a node with name |
| 181 | * 'nodename'. Return node if successful, zero if not. |
| 182 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 183 | phandle prom_searchsiblings(phandle node_start, char *nodename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 186 | phandle thisnode; |
| 187 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | for(thisnode = node_start; thisnode; |
| 190 | thisnode=prom_getsibling(thisnode)) { |
| 191 | error = prom_getproperty(thisnode, "name", promlib_buf, |
| 192 | sizeof(promlib_buf)); |
| 193 | /* Should this ever happen? */ |
| 194 | if(error == -1) continue; |
| 195 | if(strcmp(nodename, promlib_buf)==0) return thisnode; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 200 | EXPORT_SYMBOL(prom_searchsiblings); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* Interal version of nextprop that does not alter return values. */ |
Sam Ravnborg | 1f42be4 | 2011-01-01 12:53:11 +0000 | [diff] [blame^] | 203 | static char *__prom_nextprop(phandle node, char * oprop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
| 205 | unsigned long flags; |
| 206 | char *prop; |
| 207 | |
| 208 | spin_lock_irqsave(&prom_lock, flags); |
| 209 | prop = prom_nodeops->no_nextprop(node, oprop); |
| 210 | restore_current(); |
| 211 | spin_unlock_irqrestore(&prom_lock, flags); |
| 212 | |
| 213 | return prop; |
| 214 | } |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* Return the property type string after property type 'oprop' |
| 217 | * at node 'node' . Returns empty string if no more |
| 218 | * property types for this node. |
| 219 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 220 | char *prom_nextprop(phandle node, char *oprop, char *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | if (node == 0 || node == -1) |
| 223 | return ""; |
| 224 | |
| 225 | return __prom_nextprop(node, oprop); |
| 226 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 227 | EXPORT_SYMBOL(prom_nextprop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 229 | phandle prom_finddevice(char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | char nbuf[128]; |
| 232 | char *s = name, *d; |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 233 | phandle node = prom_root_node, node2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | unsigned int which_io, phys_addr; |
| 235 | struct linux_prom_registers reg[PROMREG_MAX]; |
| 236 | |
| 237 | while (*s++) { |
| 238 | if (!*s) return node; /* path '.../' is legal */ |
| 239 | node = prom_getchild(node); |
| 240 | |
| 241 | for (d = nbuf; *s != 0 && *s != '@' && *s != '/';) |
| 242 | *d++ = *s++; |
| 243 | *d = 0; |
| 244 | |
| 245 | node = prom_searchsiblings(node, nbuf); |
| 246 | if (!node) |
| 247 | return 0; |
| 248 | |
| 249 | if (*s == '@') { |
| 250 | if (isxdigit(s[1]) && s[2] == ',') { |
| 251 | which_io = simple_strtoul(s+1, NULL, 16); |
| 252 | phys_addr = simple_strtoul(s+3, &d, 16); |
| 253 | if (d != s + 3 && (!*d || *d == '/') |
| 254 | && d <= s + 3 + 8) { |
| 255 | node2 = node; |
| 256 | while (node2 && node2 != -1) { |
| 257 | if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) { |
| 258 | if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) { |
| 259 | node = node2; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | node2 = prom_getsibling(node2); |
| 264 | if (!node2 || node2 == -1) |
| 265 | break; |
| 266 | node2 = prom_searchsiblings(prom_getsibling(node2), nbuf); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | while (*s != 0 && *s != '/') s++; |
| 271 | } |
| 272 | } |
| 273 | return node; |
| 274 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 275 | EXPORT_SYMBOL(prom_finddevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* Set property 'pname' at node 'node' to value 'value' which has a length |
| 278 | * of 'size' bytes. Return the number of bytes the prom accepted. |
| 279 | */ |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 280 | int prom_setprop(phandle node, const char *pname, char *value, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | unsigned long flags; |
| 283 | int ret; |
| 284 | |
| 285 | if(size == 0) return 0; |
| 286 | if((pname == 0) || (value == 0)) return 0; |
| 287 | spin_lock_irqsave(&prom_lock, flags); |
| 288 | ret = prom_nodeops->no_setprop(node, pname, value, size); |
| 289 | restore_current(); |
| 290 | spin_unlock_irqrestore(&prom_lock, flags); |
| 291 | return ret; |
| 292 | } |
Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 293 | EXPORT_SYMBOL(prom_setprop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 295 | phandle prom_inst2pkg(int inst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Andres Salomon | 8d12556 | 2010-10-08 14:18:11 -0700 | [diff] [blame] | 297 | phandle node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | unsigned long flags; |
| 299 | |
| 300 | spin_lock_irqsave(&prom_lock, flags); |
| 301 | node = (*romvec->pv_v2devops.v2_inst2pkg)(inst); |
| 302 | restore_current(); |
| 303 | spin_unlock_irqrestore(&prom_lock, flags); |
| 304 | if (node == -1) return 0; |
| 305 | return node; |
| 306 | } |