tree_32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. static phandle __prom_getchild(phandle node)
  19. {
  20. unsigned long flags;
  21. phandle 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. phandle prom_getchild(phandle node)
  32. {
  33. phandle cnode;
  34. if ((s32)node == -1)
  35. return 0;
  36. cnode = __prom_getchild(node);
  37. if (cnode == 0 || (s32)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. static phandle __prom_getsibling(phandle node)
  44. {
  45. unsigned long flags;
  46. phandle 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. phandle prom_getsibling(phandle node)
  57. {
  58. phandle sibnode;
  59. if ((s32)node == -1)
  60. return 0;
  61. sibnode = __prom_getsibling(node);
  62. if (sibnode == 0 || (s32)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(phandle 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(phandle 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(phandle 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(phandle 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(phandle 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(phandle 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. }
  144. EXPORT_SYMBOL(prom_getstring);
  145. /* Search siblings at 'node_start' for a node with name
  146. * 'nodename'. Return node if successful, zero if not.
  147. */
  148. phandle prom_searchsiblings(phandle node_start, char *nodename)
  149. {
  150. phandle thisnode;
  151. int error;
  152. for(thisnode = node_start; thisnode;
  153. thisnode=prom_getsibling(thisnode)) {
  154. error = prom_getproperty(thisnode, "name", promlib_buf,
  155. sizeof(promlib_buf));
  156. /* Should this ever happen? */
  157. if(error == -1) continue;
  158. if(strcmp(nodename, promlib_buf)==0) return thisnode;
  159. }
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(prom_searchsiblings);
  163. /* Interal version of nextprop that does not alter return values. */
  164. static char *__prom_nextprop(phandle node, char * oprop)
  165. {
  166. unsigned long flags;
  167. char *prop;
  168. spin_lock_irqsave(&prom_lock, flags);
  169. prop = prom_nodeops->no_nextprop(node, oprop);
  170. restore_current();
  171. spin_unlock_irqrestore(&prom_lock, flags);
  172. return prop;
  173. }
  174. /* Return the property type string after property type 'oprop'
  175. * at node 'node' . Returns empty string if no more
  176. * property types for this node.
  177. */
  178. char *prom_nextprop(phandle node, char *oprop, char *buffer)
  179. {
  180. if (node == 0 || (s32)node == -1)
  181. return "";
  182. return __prom_nextprop(node, oprop);
  183. }
  184. EXPORT_SYMBOL(prom_nextprop);
  185. phandle prom_finddevice(char *name)
  186. {
  187. char nbuf[128];
  188. char *s = name, *d;
  189. phandle node = prom_root_node, node2;
  190. unsigned int which_io, phys_addr;
  191. struct linux_prom_registers reg[PROMREG_MAX];
  192. while (*s++) {
  193. if (!*s) return node; /* path '.../' is legal */
  194. node = prom_getchild(node);
  195. for (d = nbuf; *s != 0 && *s != '@' && *s != '/';)
  196. *d++ = *s++;
  197. *d = 0;
  198. node = prom_searchsiblings(node, nbuf);
  199. if (!node)
  200. return 0;
  201. if (*s == '@') {
  202. if (isxdigit(s[1]) && s[2] == ',') {
  203. which_io = simple_strtoul(s+1, NULL, 16);
  204. phys_addr = simple_strtoul(s+3, &d, 16);
  205. if (d != s + 3 && (!*d || *d == '/')
  206. && d <= s + 3 + 8) {
  207. node2 = node;
  208. while (node2 && (s32)node2 != -1) {
  209. if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) {
  210. if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) {
  211. node = node2;
  212. break;
  213. }
  214. }
  215. node2 = prom_getsibling(node2);
  216. if (!node2 || (s32)node2 == -1)
  217. break;
  218. node2 = prom_searchsiblings(prom_getsibling(node2), nbuf);
  219. }
  220. }
  221. }
  222. while (*s != 0 && *s != '/') s++;
  223. }
  224. }
  225. return node;
  226. }
  227. EXPORT_SYMBOL(prom_finddevice);
  228. /* Set property 'pname' at node 'node' to value 'value' which has a length
  229. * of 'size' bytes. Return the number of bytes the prom accepted.
  230. */
  231. int prom_setprop(phandle node, const char *pname, char *value, int size)
  232. {
  233. unsigned long flags;
  234. int ret;
  235. if (size == 0)
  236. return 0;
  237. if ((pname == NULL) || (value == NULL))
  238. return 0;
  239. spin_lock_irqsave(&prom_lock, flags);
  240. ret = prom_nodeops->no_setprop(node, pname, value, size);
  241. restore_current();
  242. spin_unlock_irqrestore(&prom_lock, flags);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(prom_setprop);
  246. phandle prom_inst2pkg(int inst)
  247. {
  248. phandle node;
  249. unsigned long flags;
  250. spin_lock_irqsave(&prom_lock, flags);
  251. node = (*romvec->pv_v2devops.v2_inst2pkg)(inst);
  252. restore_current();
  253. spin_unlock_irqrestore(&prom_lock, flags);
  254. if ((s32)node == -1)
  255. return 0;
  256. return node;
  257. }