proc_devtree.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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,
  15. struct proc_dir_entry *de)
  16. {
  17. }
  18. #endif
  19. static struct proc_dir_entry *proc_device_tree;
  20. /*
  21. * Supply data on a read from /proc/device-tree/node/property.
  22. */
  23. static int property_read_proc(char *page, char **start, off_t off,
  24. int count, int *eof, void *data)
  25. {
  26. struct property *pp = data;
  27. int n;
  28. if (off >= pp->length) {
  29. *eof = 1;
  30. return 0;
  31. }
  32. n = pp->length - off;
  33. if (n > count)
  34. n = count;
  35. else
  36. *eof = 1;
  37. memcpy(page, pp->value + off, n);
  38. *start = page;
  39. return n;
  40. }
  41. /*
  42. * For a node with a name like "gc@10", we make symlinks called "gc"
  43. * and "@10" to it.
  44. */
  45. /*
  46. * Add a property to a node
  47. */
  48. static struct proc_dir_entry *
  49. __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp)
  50. {
  51. struct proc_dir_entry *ent;
  52. /*
  53. * Unfortunately proc_register puts each new entry
  54. * at the beginning of the list. So we rearrange them.
  55. */
  56. ent = create_proc_read_entry(pp->name,
  57. strncmp(pp->name, "security-", 9)
  58. ? S_IRUGO : S_IRUSR, de,
  59. property_read_proc, pp);
  60. if (ent == NULL)
  61. return NULL;
  62. if (!strncmp(pp->name, "security-", 9))
  63. ent->size = 0; /* don't leak number of password chars */
  64. else
  65. ent->size = pp->length;
  66. return ent;
  67. }
  68. void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
  69. {
  70. __proc_device_tree_add_prop(pde, prop);
  71. }
  72. /*
  73. * Process a node, adding entries for its children and its properties.
  74. */
  75. void proc_device_tree_add_node(struct device_node *np,
  76. struct proc_dir_entry *de)
  77. {
  78. struct property *pp;
  79. struct proc_dir_entry *ent;
  80. struct device_node *child;
  81. const char *p;
  82. set_node_proc_entry(np, de);
  83. for (child = NULL; (child = of_get_next_child(np, child));) {
  84. p = strrchr(child->full_name, '/');
  85. if (!p)
  86. p = child->full_name;
  87. else
  88. ++p;
  89. ent = proc_mkdir(p, de);
  90. if (ent == 0)
  91. break;
  92. proc_device_tree_add_node(child, ent);
  93. }
  94. of_node_put(child);
  95. for (pp = np->properties; pp != 0; pp = pp->next) {
  96. /*
  97. * Yet another Apple device-tree bogosity: on some machines,
  98. * they have properties & nodes with the same name. Those
  99. * properties are quite unimportant for us though, thus we
  100. * simply "skip" them here, but we do have to check.
  101. */
  102. for (ent = de->subdir; ent != NULL; ent = ent->next)
  103. if (!strcmp(ent->name, pp->name))
  104. break;
  105. if (ent != NULL) {
  106. printk(KERN_WARNING "device-tree: property \"%s\" name"
  107. " conflicts with node in %s\n", pp->name,
  108. np->full_name);
  109. continue;
  110. }
  111. ent = __proc_device_tree_add_prop(de, pp);
  112. if (ent == 0)
  113. break;
  114. }
  115. }
  116. /*
  117. * Called on initialization to set up the /proc/device-tree subtree
  118. */
  119. void proc_device_tree_init(void)
  120. {
  121. struct device_node *root;
  122. if ( !have_of )
  123. return;
  124. proc_device_tree = proc_mkdir("device-tree", NULL);
  125. if (proc_device_tree == 0)
  126. return;
  127. root = of_find_node_by_path("/");
  128. if (root == 0) {
  129. printk(KERN_ERR "/proc/device-tree: can't find root\n");
  130. return;
  131. }
  132. proc_device_tree_add_node(root, proc_device_tree);
  133. of_node_put(root);
  134. }