proc_devtree.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Process a node, adding entries for its children and its properties.
  47. */
  48. void proc_device_tree_add_node(struct device_node *np,
  49. struct proc_dir_entry *de)
  50. {
  51. struct property *pp;
  52. struct proc_dir_entry *ent;
  53. struct device_node *child;
  54. struct proc_dir_entry *list = NULL, **lastp;
  55. const char *p;
  56. set_node_proc_entry(np, de);
  57. lastp = &list;
  58. for (child = NULL; (child = of_get_next_child(np, child));) {
  59. p = strrchr(child->full_name, '/');
  60. if (!p)
  61. p = child->full_name;
  62. else
  63. ++p;
  64. ent = proc_mkdir(p, de);
  65. if (ent == 0)
  66. break;
  67. *lastp = ent;
  68. ent->next = NULL;
  69. lastp = &ent->next;
  70. proc_device_tree_add_node(child, ent);
  71. }
  72. of_node_put(child);
  73. for (pp = np->properties; pp != 0; pp = pp->next) {
  74. /*
  75. * Yet another Apple device-tree bogosity: on some machines,
  76. * they have properties & nodes with the same name. Those
  77. * properties are quite unimportant for us though, thus we
  78. * simply "skip" them here, but we do have to check.
  79. */
  80. for (ent = list; ent != NULL; ent = ent->next)
  81. if (!strcmp(ent->name, pp->name))
  82. break;
  83. if (ent != NULL) {
  84. printk(KERN_WARNING "device-tree: property \"%s\" name"
  85. " conflicts with node in %s\n", pp->name,
  86. np->full_name);
  87. continue;
  88. }
  89. /*
  90. * Unfortunately proc_register puts each new entry
  91. * at the beginning of the list. So we rearrange them.
  92. */
  93. ent = create_proc_read_entry(pp->name,
  94. strncmp(pp->name, "security-", 9)
  95. ? S_IRUGO : S_IRUSR, de,
  96. property_read_proc, pp);
  97. if (ent == 0)
  98. break;
  99. if (!strncmp(pp->name, "security-", 9))
  100. ent->size = 0; /* don't leak number of password chars */
  101. else
  102. ent->size = pp->length;
  103. ent->next = NULL;
  104. *lastp = ent;
  105. lastp = &ent->next;
  106. }
  107. de->subdir = list;
  108. }
  109. /*
  110. * Called on initialization to set up the /proc/device-tree subtree
  111. */
  112. void proc_device_tree_init(void)
  113. {
  114. struct device_node *root;
  115. if ( !have_of )
  116. return;
  117. proc_device_tree = proc_mkdir("device-tree", NULL);
  118. if (proc_device_tree == 0)
  119. return;
  120. root = of_find_node_by_path("/");
  121. if (root == 0) {
  122. printk(KERN_ERR "/proc/device-tree: can't find root\n");
  123. return;
  124. }
  125. proc_device_tree_add_node(root, proc_device_tree);
  126. of_node_put(root);
  127. }