tree_32.c 8.0 KB

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