tree_64.c 7.9 KB

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