proc_devtree.c 5.2 KB

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