tree.c 6.6 KB

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