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