tree.c 7.6 KB

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