proc_devtree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * proc_devtree.c - handles /proc/device-tree
  3. *
  4. * Copyright 1997 Paul Mackerras
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/time.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/stat.h>
  10. #include <linux/string.h>
  11. #include <asm/prom.h>
  12. #include <asm/uaccess.h>
  13. #ifndef HAVE_ARCH_DEVTREE_FIXUPS
  14. static inline void set_node_proc_entry(struct device_node *np, struct proc_dir_entry *de)
  15. {
  16. }
  17. static void inline set_node_name_link(struct device_node *np, struct proc_dir_entry *de)
  18. {
  19. }
  20. static void inline set_node_addr_link(struct device_node *np, struct proc_dir_entry *de)
  21. {
  22. }
  23. #endif
  24. static struct proc_dir_entry *proc_device_tree;
  25. /*
  26. * Supply data on a read from /proc/device-tree/node/property.
  27. */
  28. static int property_read_proc(char *page, char **start, off_t off,
  29. int count, int *eof, void *data)
  30. {
  31. struct property *pp = data;
  32. int n;
  33. if (off >= pp->length) {
  34. *eof = 1;
  35. return 0;
  36. }
  37. n = pp->length - off;
  38. if (n > count)
  39. n = count;
  40. else
  41. *eof = 1;
  42. memcpy(page, pp->value + off, n);
  43. *start = page;
  44. return n;
  45. }
  46. /*
  47. * For a node with a name like "gc@10", we make symlinks called "gc"
  48. * and "@10" to it.
  49. */
  50. /*
  51. * Process a node, adding entries for its children and its properties.
  52. */
  53. void proc_device_tree_add_node(struct device_node *np, struct proc_dir_entry *de)
  54. {
  55. struct property *pp;
  56. struct proc_dir_entry *ent;
  57. struct device_node *child, *sib;
  58. const char *p, *at;
  59. int l;
  60. struct proc_dir_entry *list, **lastp, *al;
  61. set_node_proc_entry(np, de);
  62. lastp = &list;
  63. for (pp = np->properties; pp != 0; pp = pp->next) {
  64. /*
  65. * Unfortunately proc_register puts each new entry
  66. * at the beginning of the list. So we rearrange them.
  67. */
  68. ent = create_proc_read_entry(pp->name, strncmp(pp->name, "security-", 9) ?
  69. S_IRUGO : S_IRUSR, de, property_read_proc, pp);
  70. if (ent == 0)
  71. break;
  72. if (!strncmp(pp->name, "security-", 9))
  73. ent->size = 0; /* don't leak number of password chars */
  74. else
  75. ent->size = pp->length;
  76. *lastp = ent;
  77. lastp = &ent->next;
  78. }
  79. child = NULL;
  80. while ((child = of_get_next_child(np, child))) {
  81. p = strrchr(child->full_name, '/');
  82. if (!p)
  83. p = child->full_name;
  84. else
  85. ++p;
  86. /* chop off '@0' if the name ends with that */
  87. l = strlen(p);
  88. if (l > 2 && p[l-2] == '@' && p[l-1] == '0')
  89. l -= 2;
  90. ent = proc_mkdir(p, de);
  91. if (ent == 0)
  92. break;
  93. *lastp = ent;
  94. lastp = &ent->next;
  95. proc_device_tree_add_node(child, ent);
  96. /*
  97. * If we left the address part on the name, consider
  98. * adding symlinks from the name and address parts.
  99. */
  100. if (p[l] != 0 || (at = strchr(p, '@')) == 0)
  101. continue;
  102. /*
  103. * If this is the first node with a given name property,
  104. * add a symlink with the name property as its name.
  105. */
  106. sib = NULL;
  107. while ((sib = of_get_next_child(np, sib)) && sib != child)
  108. if (sib->name && strcmp(sib->name, child->name) == 0)
  109. break;
  110. if (sib == child && strncmp(p, child->name, l) != 0) {
  111. al = proc_symlink(child->name, de, ent->name);
  112. if (al == 0) {
  113. of_node_put(sib);
  114. break;
  115. }
  116. set_node_name_link(child, al);
  117. *lastp = al;
  118. lastp = &al->next;
  119. }
  120. of_node_put(sib);
  121. /*
  122. * Add another directory with the @address part as its name.
  123. */
  124. al = proc_symlink(at, de, ent->name);
  125. if (al == 0)
  126. break;
  127. set_node_addr_link(child, al);
  128. *lastp = al;
  129. lastp = &al->next;
  130. }
  131. of_node_put(child);
  132. *lastp = NULL;
  133. de->subdir = list;
  134. }
  135. /*
  136. * Called on initialization to set up the /proc/device-tree subtree
  137. */
  138. void proc_device_tree_init(void)
  139. {
  140. struct device_node *root;
  141. if ( !have_of )
  142. return;
  143. proc_device_tree = proc_mkdir("device-tree", NULL);
  144. if (proc_device_tree == 0)
  145. return;
  146. root = of_find_node_by_path("/");
  147. if (root == 0) {
  148. printk(KERN_ERR "/proc/device-tree: can't find root\n");
  149. return;
  150. }
  151. proc_device_tree_add_node(root, proc_device_tree);
  152. of_node_put(root);
  153. }