tree.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* $Id: tree.c,v 1.26 2000/08/26 02:38:03 anton 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. */
  7. #define PROMLIB_INTERNAL
  8. #include <linux/string.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/ctype.h>
  13. #include <asm/openprom.h>
  14. #include <asm/oplib.h>
  15. extern void restore_current(void);
  16. static char promlib_buf[128];
  17. /* Internal version of prom_getchild that does not alter return values. */
  18. int __prom_getchild(int node)
  19. {
  20. unsigned long flags;
  21. int cnode;
  22. spin_lock_irqsave(&prom_lock, flags);
  23. cnode = prom_nodeops->no_child(node);
  24. restore_current();
  25. spin_unlock_irqrestore(&prom_lock, flags);
  26. return cnode;
  27. }
  28. /* Return the child of node 'node' or zero if no this node has no
  29. * direct descendent.
  30. */
  31. int prom_getchild(int node)
  32. {
  33. int cnode;
  34. if (node == -1)
  35. return 0;
  36. cnode = __prom_getchild(node);
  37. if (cnode == 0 || cnode == -1)
  38. return 0;
  39. return cnode;
  40. }
  41. /* Internal version of prom_getsibling that does not alter return values. */
  42. int __prom_getsibling(int node)
  43. {
  44. unsigned long flags;
  45. int cnode;
  46. spin_lock_irqsave(&prom_lock, flags);
  47. cnode = prom_nodeops->no_nextnode(node);
  48. restore_current();
  49. spin_unlock_irqrestore(&prom_lock, flags);
  50. return cnode;
  51. }
  52. /* Return the next sibling of node 'node' or zero if no more siblings
  53. * at this level of depth in the tree.
  54. */
  55. int prom_getsibling(int node)
  56. {
  57. int sibnode;
  58. if (node == -1)
  59. return 0;
  60. sibnode = __prom_getsibling(node);
  61. if (sibnode == 0 || sibnode == -1)
  62. return 0;
  63. return sibnode;
  64. }
  65. /* Return the length in bytes of property 'prop' at node 'node'.
  66. * Return -1 on error.
  67. */
  68. int prom_getproplen(int node, char *prop)
  69. {
  70. int ret;
  71. unsigned long flags;
  72. if((!node) || (!prop))
  73. return -1;
  74. spin_lock_irqsave(&prom_lock, flags);
  75. ret = prom_nodeops->no_proplen(node, prop);
  76. restore_current();
  77. spin_unlock_irqrestore(&prom_lock, flags);
  78. return ret;
  79. }
  80. /* Acquire a property 'prop' at node 'node' and place it in
  81. * 'buffer' which has a size of 'bufsize'. If the acquisition
  82. * was successful the length will be returned, else -1 is returned.
  83. */
  84. int prom_getproperty(int node, char *prop, char *buffer, int bufsize)
  85. {
  86. int plen, ret;
  87. unsigned long flags;
  88. plen = prom_getproplen(node, prop);
  89. if((plen > bufsize) || (plen == 0) || (plen == -1))
  90. return -1;
  91. /* Ok, things seem all right. */
  92. spin_lock_irqsave(&prom_lock, flags);
  93. ret = prom_nodeops->no_getprop(node, prop, buffer);
  94. restore_current();
  95. spin_unlock_irqrestore(&prom_lock, flags);
  96. return ret;
  97. }
  98. /* Acquire an integer property and return its value. Returns -1
  99. * on failure.
  100. */
  101. int prom_getint(int node, char *prop)
  102. {
  103. static int intprop;
  104. if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
  105. return intprop;
  106. return -1;
  107. }
  108. /* Acquire an integer property, upon error return the passed default
  109. * integer.
  110. */
  111. int prom_getintdefault(int node, char *property, int deflt)
  112. {
  113. int retval;
  114. retval = prom_getint(node, property);
  115. if(retval == -1) return deflt;
  116. return retval;
  117. }
  118. /* Acquire a boolean property, 1=TRUE 0=FALSE. */
  119. int prom_getbool(int node, char *prop)
  120. {
  121. int retval;
  122. retval = prom_getproplen(node, prop);
  123. if(retval == -1) return 0;
  124. return 1;
  125. }
  126. /* Acquire a property whose value is a string, returns a null
  127. * string on error. The char pointer is the user supplied string
  128. * buffer.
  129. */
  130. void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
  131. {
  132. int len;
  133. len = prom_getproperty(node, prop, user_buf, ubuf_size);
  134. if(len != -1) return;
  135. user_buf[0] = 0;
  136. return;
  137. }
  138. /* Does the device at node 'node' have name 'name'?
  139. * YES = 1 NO = 0
  140. */
  141. int prom_nodematch(int node, char *name)
  142. {
  143. int error;
  144. static char namebuf[128];
  145. error = prom_getproperty(node, "name", namebuf, sizeof(namebuf));
  146. if (error == -1) return 0;
  147. if(strcmp(namebuf, name) == 0) return 1;
  148. return 0;
  149. }
  150. /* Search siblings at 'node_start' for a node with name
  151. * 'nodename'. Return node if successful, zero if not.
  152. */
  153. int prom_searchsiblings(int node_start, char *nodename)
  154. {
  155. int thisnode, error;
  156. for(thisnode = node_start; thisnode;
  157. thisnode=prom_getsibling(thisnode)) {
  158. error = prom_getproperty(thisnode, "name", promlib_buf,
  159. sizeof(promlib_buf));
  160. /* Should this ever happen? */
  161. if(error == -1) continue;
  162. if(strcmp(nodename, promlib_buf)==0) return thisnode;
  163. }
  164. return 0;
  165. }
  166. /* Gets name in the form prom v2+ uses it (name@x,yyyyy or name (if no reg)) */
  167. int prom_getname (int node, char *buffer, int len)
  168. {
  169. int i;
  170. struct linux_prom_registers reg[PROMREG_MAX];
  171. i = prom_getproperty (node, "name", buffer, len);
  172. if (i <= 0) return -1;
  173. buffer [i] = 0;
  174. len -= i;
  175. i = prom_getproperty (node, "reg", (char *)reg, sizeof (reg));
  176. if (i <= 0) return 0;
  177. if (len < 11) return -1;
  178. buffer = strchr (buffer, 0);
  179. sprintf (buffer, "@%x,%x", reg[0].which_io, (uint)reg[0].phys_addr);
  180. return 0;
  181. }
  182. /* Interal version of nextprop that does not alter return values. */
  183. char * __prom_nextprop(int node, char * oprop)
  184. {
  185. unsigned long flags;
  186. char *prop;
  187. spin_lock_irqsave(&prom_lock, flags);
  188. prop = prom_nodeops->no_nextprop(node, oprop);
  189. restore_current();
  190. spin_unlock_irqrestore(&prom_lock, flags);
  191. return prop;
  192. }
  193. /* Return the first property name for node 'node'. */
  194. /* buffer is unused argument, but as v9 uses it, we need to have the same interface */
  195. char * prom_firstprop(int node, char *bufer)
  196. {
  197. if (node == 0 || node == -1)
  198. return "";
  199. return __prom_nextprop(node, "");
  200. }
  201. /* Return the property type string after property type 'oprop'
  202. * at node 'node' . Returns empty string if no more
  203. * property types for this node.
  204. */
  205. char * prom_nextprop(int node, char *oprop, char *buffer)
  206. {
  207. if (node == 0 || node == -1)
  208. return "";
  209. return __prom_nextprop(node, oprop);
  210. }
  211. int prom_finddevice(char *name)
  212. {
  213. char nbuf[128];
  214. char *s = name, *d;
  215. int node = prom_root_node, node2;
  216. unsigned int which_io, phys_addr;
  217. struct linux_prom_registers reg[PROMREG_MAX];
  218. while (*s++) {
  219. if (!*s) return node; /* path '.../' is legal */
  220. node = prom_getchild(node);
  221. for (d = nbuf; *s != 0 && *s != '@' && *s != '/';)
  222. *d++ = *s++;
  223. *d = 0;
  224. node = prom_searchsiblings(node, nbuf);
  225. if (!node)
  226. return 0;
  227. if (*s == '@') {
  228. if (isxdigit(s[1]) && s[2] == ',') {
  229. which_io = simple_strtoul(s+1, NULL, 16);
  230. phys_addr = simple_strtoul(s+3, &d, 16);
  231. if (d != s + 3 && (!*d || *d == '/')
  232. && d <= s + 3 + 8) {
  233. node2 = node;
  234. while (node2 && node2 != -1) {
  235. if (prom_getproperty (node2, "reg", (char *)reg, sizeof (reg)) > 0) {
  236. if (which_io == reg[0].which_io && phys_addr == reg[0].phys_addr) {
  237. node = node2;
  238. break;
  239. }
  240. }
  241. node2 = prom_getsibling(node2);
  242. if (!node2 || node2 == -1)
  243. break;
  244. node2 = prom_searchsiblings(prom_getsibling(node2), nbuf);
  245. }
  246. }
  247. }
  248. while (*s != 0 && *s != '/') s++;
  249. }
  250. }
  251. return node;
  252. }
  253. int prom_node_has_property(int node, char *prop)
  254. {
  255. char *current_property = "";
  256. do {
  257. current_property = prom_nextprop(node, current_property, NULL);
  258. if(!strcmp(current_property, prop))
  259. return 1;
  260. } while (*current_property);
  261. return 0;
  262. }
  263. /* Set property 'pname' at node 'node' to value 'value' which has a length
  264. * of 'size' bytes. Return the number of bytes the prom accepted.
  265. */
  266. int prom_setprop(int node, char *pname, char *value, int size)
  267. {
  268. unsigned long flags;
  269. int ret;
  270. if(size == 0) return 0;
  271. if((pname == 0) || (value == 0)) return 0;
  272. spin_lock_irqsave(&prom_lock, flags);
  273. ret = prom_nodeops->no_setprop(node, pname, value, size);
  274. restore_current();
  275. spin_unlock_irqrestore(&prom_lock, flags);
  276. return ret;
  277. }
  278. int prom_inst2pkg(int inst)
  279. {
  280. int node;
  281. unsigned long flags;
  282. spin_lock_irqsave(&prom_lock, flags);
  283. node = (*romvec->pv_v2devops.v2_inst2pkg)(inst);
  284. restore_current();
  285. spin_unlock_irqrestore(&prom_lock, flags);
  286. if (node == -1) return 0;
  287. return node;
  288. }
  289. /* Return 'node' assigned to a particular prom 'path'
  290. * FIXME: Should work for v0 as well
  291. */
  292. int prom_pathtoinode(char *path)
  293. {
  294. int node, inst;
  295. inst = prom_devopen (path);
  296. if (inst == -1) return 0;
  297. node = prom_inst2pkg (inst);
  298. prom_devclose (inst);
  299. if (node == -1) return 0;
  300. return node;
  301. }