proc_devtree.c 5.2 KB

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