tree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* $Id: tree.c,v 1.10 1998/01/10 22:39:00 ecd 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. * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. */
  8. #include <linux/string.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <asm/openprom.h>
  13. #include <asm/oplib.h>
  14. /* Return the child of node 'node' or zero if no this node has no
  15. * direct descendent.
  16. */
  17. __inline__ int
  18. __prom_getchild(int node)
  19. {
  20. return p1275_cmd ("child", P1275_INOUT(1, 1), node);
  21. }
  22. __inline__ int
  23. prom_getchild(int node)
  24. {
  25. int cnode;
  26. if(node == -1) return 0;
  27. cnode = __prom_getchild(node);
  28. if(cnode == -1) return 0;
  29. return (int)cnode;
  30. }
  31. __inline__ int
  32. prom_getparent(int node)
  33. {
  34. int cnode;
  35. if(node == -1) return 0;
  36. cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
  37. if(cnode == -1) return 0;
  38. return (int)cnode;
  39. }
  40. /* Return the next sibling of node 'node' or zero if no more siblings
  41. * at this level of depth in the tree.
  42. */
  43. __inline__ int
  44. __prom_getsibling(int node)
  45. {
  46. return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
  47. }
  48. __inline__ int
  49. prom_getsibling(int node)
  50. {
  51. int sibnode;
  52. if (node == -1)
  53. return 0;
  54. sibnode = __prom_getsibling(node);
  55. if (sibnode == -1)
  56. return 0;
  57. return sibnode;
  58. }
  59. /* Return the length in bytes of property 'prop' at node 'node'.
  60. * Return -1 on error.
  61. */
  62. __inline__ int
  63. prom_getproplen(int node, const char *prop)
  64. {
  65. if((!node) || (!prop)) return -1;
  66. return p1275_cmd ("getproplen",
  67. P1275_ARG(1,P1275_ARG_IN_STRING)|
  68. P1275_INOUT(2, 1),
  69. node, prop);
  70. }
  71. /* Acquire a property 'prop' at node 'node' and place it in
  72. * 'buffer' which has a size of 'bufsize'. If the acquisition
  73. * was successful the length will be returned, else -1 is returned.
  74. */
  75. __inline__ int
  76. prom_getproperty(int node, const char *prop, char *buffer, int bufsize)
  77. {
  78. int plen;
  79. plen = prom_getproplen(node, prop);
  80. if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
  81. return -1;
  82. } else {
  83. /* Ok, things seem all right. */
  84. return p1275_cmd(prom_getprop_name,
  85. P1275_ARG(1,P1275_ARG_IN_STRING)|
  86. P1275_ARG(2,P1275_ARG_OUT_BUF)|
  87. P1275_INOUT(4, 1),
  88. node, prop, buffer, P1275_SIZE(plen));
  89. }
  90. }
  91. /* Acquire an integer property and return its value. Returns -1
  92. * on failure.
  93. */
  94. __inline__ int
  95. prom_getint(int node, const char *prop)
  96. {
  97. int intprop;
  98. if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
  99. return intprop;
  100. return -1;
  101. }
  102. /* Acquire an integer property, upon error return the passed default
  103. * integer.
  104. */
  105. int
  106. prom_getintdefault(int node, const char *property, int deflt)
  107. {
  108. int retval;
  109. retval = prom_getint(node, property);
  110. if(retval == -1) return deflt;
  111. return retval;
  112. }
  113. /* Acquire a boolean property, 1=TRUE 0=FALSE. */
  114. int
  115. prom_getbool(int node, const char *prop)
  116. {
  117. int retval;
  118. retval = prom_getproplen(node, prop);
  119. if(retval == -1) return 0;
  120. return 1;
  121. }
  122. /* Acquire a property whose value is a string, returns a null
  123. * string on error. The char pointer is the user supplied string
  124. * buffer.
  125. */
  126. void
  127. prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
  128. {
  129. int len;
  130. len = prom_getproperty(node, prop, user_buf, ubuf_size);
  131. if(len != -1) return;
  132. user_buf[0] = 0;
  133. return;
  134. }
  135. /* Does the device at node 'node' have name 'name'?
  136. * YES = 1 NO = 0
  137. */
  138. int
  139. prom_nodematch(int node, const char *name)
  140. {
  141. char namebuf[128];
  142. prom_getproperty(node, "name", namebuf, sizeof(namebuf));
  143. if(strcmp(namebuf, name) == 0) return 1;
  144. return 0;
  145. }
  146. /* Search siblings at 'node_start' for a node with name
  147. * 'nodename'. Return node if successful, zero if not.
  148. */
  149. int
  150. prom_searchsiblings(int node_start, const char *nodename)
  151. {
  152. int thisnode, error;
  153. char promlib_buf[128];
  154. for(thisnode = node_start; thisnode;
  155. thisnode=prom_getsibling(thisnode)) {
  156. error = prom_getproperty(thisnode, "name", promlib_buf,
  157. sizeof(promlib_buf));
  158. /* Should this ever happen? */
  159. if(error == -1) continue;
  160. if(strcmp(nodename, promlib_buf)==0) return thisnode;
  161. }
  162. return 0;
  163. }
  164. /* Return the first property type for node 'node'.
  165. * buffer should be at least 32B in length
  166. */
  167. __inline__ char *
  168. prom_firstprop(int node, char *buffer)
  169. {
  170. *buffer = 0;
  171. if(node == -1) return buffer;
  172. p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
  173. P1275_INOUT(3, 0),
  174. node, (char *) 0x0, buffer);
  175. return buffer;
  176. }
  177. /* Return the property type string after property type 'oprop'
  178. * at node 'node' . Returns NULL string if no more
  179. * property types for this node.
  180. */
  181. __inline__ char *
  182. prom_nextprop(int node, const char *oprop, char *buffer)
  183. {
  184. char buf[32];
  185. if(node == -1) {
  186. *buffer = 0;
  187. return buffer;
  188. }
  189. if (oprop == buffer) {
  190. strcpy (buf, oprop);
  191. oprop = buf;
  192. }
  193. p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
  194. P1275_ARG(2,P1275_ARG_OUT_32B)|
  195. P1275_INOUT(3, 0),
  196. node, oprop, buffer);
  197. return buffer;
  198. }
  199. int
  200. prom_finddevice(const char *name)
  201. {
  202. if (!name)
  203. return 0;
  204. return p1275_cmd(prom_finddev_name,
  205. P1275_ARG(0,P1275_ARG_IN_STRING)|
  206. P1275_INOUT(1, 1),
  207. name);
  208. }
  209. int prom_node_has_property(int node, const char *prop)
  210. {
  211. char buf [32];
  212. *buf = 0;
  213. do {
  214. prom_nextprop(node, buf, buf);
  215. if(!strcmp(buf, prop))
  216. return 1;
  217. } while (*buf);
  218. return 0;
  219. }
  220. /* Set property 'pname' at node 'node' to value 'value' which has a length
  221. * of 'size' bytes. Return the number of bytes the prom accepted.
  222. */
  223. int
  224. prom_setprop(int node, const char *pname, char *value, int size)
  225. {
  226. if(size == 0) return 0;
  227. if((pname == 0) || (value == 0)) return 0;
  228. return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
  229. P1275_ARG(2,P1275_ARG_IN_BUF)|
  230. P1275_INOUT(4, 1),
  231. node, pname, value, P1275_SIZE(size));
  232. }
  233. __inline__ int
  234. prom_inst2pkg(int inst)
  235. {
  236. int node;
  237. node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
  238. if (node == -1) return 0;
  239. return node;
  240. }
  241. /* Return 'node' assigned to a particular prom 'path'
  242. * FIXME: Should work for v0 as well
  243. */
  244. int
  245. prom_pathtoinode(const char *path)
  246. {
  247. int node, inst;
  248. inst = prom_devopen (path);
  249. if (inst == 0) return 0;
  250. node = prom_inst2pkg (inst);
  251. prom_devclose (inst);
  252. if (node == -1) return 0;
  253. return node;
  254. }