tree_64.c 7.9 KB

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