tree.c 6.5 KB

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