proc_devtree.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * proc_devtree.c - handles /proc/device-tree
  3. *
  4. * Copyright 1997 Paul Mackerras
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. #include <linux/time.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/stat.h>
  11. #include <linux/string.h>
  12. #include <asm/prom.h>
  13. #include <asm/uaccess.h>
  14. #include "internal.h"
  15. #ifndef HAVE_ARCH_DEVTREE_FIXUPS
  16. static inline void set_node_proc_entry(struct device_node *np,
  17. struct proc_dir_entry *de)
  18. {
  19. }
  20. #endif
  21. static struct proc_dir_entry *proc_device_tree;
  22. /*
  23. * Supply data on a read from /proc/device-tree/node/property.
  24. */
  25. static int property_read_proc(char *page, char **start, off_t off,
  26. int count, int *eof, void *data)
  27. {
  28. struct property *pp = data;
  29. int n;
  30. if (off >= pp->length) {
  31. *eof = 1;
  32. return 0;
  33. }
  34. n = pp->length - off;
  35. if (n > count)
  36. n = count;
  37. else
  38. *eof = 1;
  39. memcpy(page, (char *)pp->value + off, n);
  40. *start = page;
  41. return n;
  42. }
  43. /*
  44. * For a node with a name like "gc@10", we make symlinks called "gc"
  45. * and "@10" to it.
  46. */
  47. /*
  48. * Add a property to a node
  49. */
  50. static struct proc_dir_entry *
  51. __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
  52. const char *name)
  53. {
  54. struct proc_dir_entry *ent;
  55. /*
  56. * Unfortunately proc_register puts each new entry
  57. * at the beginning of the list. So we rearrange them.
  58. */
  59. ent = create_proc_read_entry(name,
  60. strncmp(name, "security-", 9)
  61. ? S_IRUGO : S_IRUSR, de,
  62. property_read_proc, pp);
  63. if (ent == NULL)
  64. return NULL;
  65. if (!strncmp(name, "security-", 9))
  66. ent->size = 0; /* don't leak number of password chars */
  67. else
  68. ent->size = pp->length;
  69. return ent;
  70. }
  71. void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
  72. {
  73. __proc_device_tree_add_prop(pde, prop, prop->name);
  74. }
  75. void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
  76. struct property *prop)
  77. {
  78. remove_proc_entry(prop->name, pde);
  79. }
  80. void proc_device_tree_update_prop(struct proc_dir_entry *pde,
  81. struct property *newprop,
  82. struct property *oldprop)
  83. {
  84. struct proc_dir_entry *ent;
  85. for (ent = pde->subdir; ent != NULL; ent = ent->next)
  86. if (ent->data == oldprop)
  87. break;
  88. if (ent == NULL) {
  89. printk(KERN_WARNING "device-tree: property \"%s\" "
  90. " does not exist\n", oldprop->name);
  91. } else {
  92. ent->data = newprop;
  93. ent->size = newprop->length;
  94. }
  95. }
  96. /*
  97. * Various dodgy firmware might give us nodes and/or properties with
  98. * conflicting names. That's generally ok, except for exporting via /proc,
  99. * so munge names here to ensure they're unique.
  100. */
  101. static int duplicate_name(struct proc_dir_entry *de, const char *name)
  102. {
  103. struct proc_dir_entry *ent;
  104. int found = 0;
  105. spin_lock(&proc_subdir_lock);
  106. for (ent = de->subdir; ent != NULL; ent = ent->next) {
  107. if (strcmp(ent->name, name) == 0) {
  108. found = 1;
  109. break;
  110. }
  111. }
  112. spin_unlock(&proc_subdir_lock);
  113. return found;
  114. }
  115. static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
  116. const char *name)
  117. {
  118. char *fixed_name;
  119. int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
  120. int i = 1, size;
  121. realloc:
  122. fixed_name = kmalloc(fixup_len, GFP_KERNEL);
  123. if (fixed_name == NULL) {
  124. printk(KERN_ERR "device-tree: Out of memory trying to fixup "
  125. "name \"%s\"\n", name);
  126. return name;
  127. }
  128. retry:
  129. size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
  130. size++; /* account for NULL */
  131. if (size > fixup_len) {
  132. /* We ran out of space, free and reallocate. */
  133. kfree(fixed_name);
  134. fixup_len = size;
  135. goto realloc;
  136. }
  137. if (duplicate_name(de, fixed_name)) {
  138. /* Multiple duplicates. Retry with a different offset. */
  139. i++;
  140. goto retry;
  141. }
  142. printk(KERN_WARNING "device-tree: Duplicate name in %s, "
  143. "renamed to \"%s\"\n", np->full_name, fixed_name);
  144. return fixed_name;
  145. }
  146. /*
  147. * Process a node, adding entries for its children and its properties.
  148. */
  149. void proc_device_tree_add_node(struct device_node *np,
  150. struct proc_dir_entry *de)
  151. {
  152. struct property *pp;
  153. struct proc_dir_entry *ent;
  154. struct device_node *child;
  155. const char *p;
  156. set_node_proc_entry(np, de);
  157. for (child = NULL; (child = of_get_next_child(np, child));) {
  158. /* Use everything after the last slash, or the full name */
  159. p = strrchr(child->full_name, '/');
  160. if (!p)
  161. p = child->full_name;
  162. else
  163. ++p;
  164. if (duplicate_name(de, p))
  165. p = fixup_name(np, de, p);
  166. ent = proc_mkdir(p, de);
  167. if (ent == NULL)
  168. break;
  169. proc_device_tree_add_node(child, ent);
  170. }
  171. of_node_put(child);
  172. for (pp = np->properties; pp != NULL; pp = pp->next) {
  173. p = pp->name;
  174. if (duplicate_name(de, p))
  175. p = fixup_name(np, de, p);
  176. ent = __proc_device_tree_add_prop(de, pp, p);
  177. if (ent == NULL)
  178. break;
  179. }
  180. }
  181. /*
  182. * Called on initialization to set up the /proc/device-tree subtree
  183. */
  184. void __init proc_device_tree_init(void)
  185. {
  186. struct device_node *root;
  187. proc_device_tree = proc_mkdir("device-tree", NULL);
  188. if (proc_device_tree == NULL)
  189. return;
  190. root = of_find_node_by_path("/");
  191. if (root == NULL) {
  192. printk(KERN_ERR "/proc/device-tree: can't find root\n");
  193. return;
  194. }
  195. proc_device_tree_add_node(root, proc_device_tree);
  196. of_node_put(root);
  197. }