tree_32.c 8.1 KB

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