Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * file.c - part of debugfs, a tiny little debug file system | |
3 | * | |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | |
5 | * Copyright (C) 2004 IBM Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * debugfs is for people to use instead of /proc or /sys. | |
12 | * See Documentation/DocBook/kernel-api for more details. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/config.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/debugfs.h> | |
21 | ||
22 | static ssize_t default_read_file(struct file *file, char __user *buf, | |
23 | size_t count, loff_t *ppos) | |
24 | { | |
25 | return 0; | |
26 | } | |
27 | ||
28 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
29 | size_t count, loff_t *ppos) | |
30 | { | |
31 | return count; | |
32 | } | |
33 | ||
34 | static int default_open(struct inode *inode, struct file *file) | |
35 | { | |
36 | if (inode->u.generic_ip) | |
37 | file->private_data = inode->u.generic_ip; | |
38 | ||
39 | return 0; | |
40 | } | |
41 | ||
42 | struct file_operations debugfs_file_operations = { | |
43 | .read = default_read_file, | |
44 | .write = default_write_file, | |
45 | .open = default_open, | |
46 | }; | |
47 | ||
acaefc25 AB |
48 | static void debugfs_u8_set(void *data, u64 val) |
49 | { | |
50 | *(u8 *)data = val; | |
51 | } | |
52 | static u64 debugfs_u8_get(void *data) | |
53 | { | |
54 | return *(u8 *)data; | |
55 | } | |
56 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
57 | |
58 | /** | |
59 | * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | |
60 | * | |
61 | * @name: a pointer to a string containing the name of the file to create. | |
62 | * @mode: the permission that the file should have | |
63 | * @parent: a pointer to the parent dentry for this file. This should be a | |
64 | * directory dentry if set. If this paramater is NULL, then the | |
65 | * file will be created in the root of the debugfs filesystem. | |
66 | * @value: a pointer to the variable that the file should read to and write | |
67 | * from. | |
68 | * | |
69 | * This function creates a file in debugfs with the given name that | |
70 | * contains the value of the variable @value. If the @mode variable is so | |
71 | * set, it can be read from, and written to. | |
72 | * | |
73 | * This function will return a pointer to a dentry if it succeeds. This | |
74 | * pointer must be passed to the debugfs_remove() function when the file is | |
75 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
76 | * you are responsible here.) If an error occurs, NULL will be returned. | |
77 | * | |
78 | * If debugfs is not enabled in the kernel, the value -ENODEV will be | |
79 | * returned. It is not wise to check for this value, but rather, check for | |
80 | * NULL or !NULL instead as to eliminate the need for #ifdef in the calling | |
81 | * code. | |
82 | */ | |
83 | struct dentry *debugfs_create_u8(const char *name, mode_t mode, | |
84 | struct dentry *parent, u8 *value) | |
85 | { | |
86 | return debugfs_create_file(name, mode, parent, value, &fops_u8); | |
87 | } | |
88 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
89 | ||
acaefc25 AB |
90 | static void debugfs_u16_set(void *data, u64 val) |
91 | { | |
92 | *(u16 *)data = val; | |
93 | } | |
94 | static u64 debugfs_u16_get(void *data) | |
95 | { | |
96 | return *(u16 *)data; | |
97 | } | |
98 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); | |
99 | ||
1da177e4 LT |
100 | /** |
101 | * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | |
102 | * | |
103 | * @name: a pointer to a string containing the name of the file to create. | |
104 | * @mode: the permission that the file should have | |
105 | * @parent: a pointer to the parent dentry for this file. This should be a | |
106 | * directory dentry if set. If this paramater is NULL, then the | |
107 | * file will be created in the root of the debugfs filesystem. | |
108 | * @value: a pointer to the variable that the file should read to and write | |
109 | * from. | |
110 | * | |
111 | * This function creates a file in debugfs with the given name that | |
112 | * contains the value of the variable @value. If the @mode variable is so | |
113 | * set, it can be read from, and written to. | |
114 | * | |
115 | * This function will return a pointer to a dentry if it succeeds. This | |
116 | * pointer must be passed to the debugfs_remove() function when the file is | |
117 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
118 | * you are responsible here.) If an error occurs, NULL will be returned. | |
119 | * | |
120 | * If debugfs is not enabled in the kernel, the value -ENODEV will be | |
121 | * returned. It is not wise to check for this value, but rather, check for | |
122 | * NULL or !NULL instead as to eliminate the need for #ifdef in the calling | |
123 | * code. | |
124 | */ | |
125 | struct dentry *debugfs_create_u16(const char *name, mode_t mode, | |
126 | struct dentry *parent, u16 *value) | |
127 | { | |
128 | return debugfs_create_file(name, mode, parent, value, &fops_u16); | |
129 | } | |
130 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
131 | ||
acaefc25 AB |
132 | static void debugfs_u32_set(void *data, u64 val) |
133 | { | |
134 | *(u32 *)data = val; | |
135 | } | |
136 | static u64 debugfs_u32_get(void *data) | |
137 | { | |
138 | return *(u32 *)data; | |
139 | } | |
140 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); | |
141 | ||
1da177e4 LT |
142 | /** |
143 | * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value. | |
144 | * | |
145 | * @name: a pointer to a string containing the name of the file to create. | |
146 | * @mode: the permission that the file should have | |
147 | * @parent: a pointer to the parent dentry for this file. This should be a | |
148 | * directory dentry if set. If this paramater is NULL, then the | |
149 | * file will be created in the root of the debugfs filesystem. | |
150 | * @value: a pointer to the variable that the file should read to and write | |
151 | * from. | |
152 | * | |
153 | * This function creates a file in debugfs with the given name that | |
154 | * contains the value of the variable @value. If the @mode variable is so | |
155 | * set, it can be read from, and written to. | |
156 | * | |
157 | * This function will return a pointer to a dentry if it succeeds. This | |
158 | * pointer must be passed to the debugfs_remove() function when the file is | |
159 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
160 | * you are responsible here.) If an error occurs, NULL will be returned. | |
161 | * | |
162 | * If debugfs is not enabled in the kernel, the value -ENODEV will be | |
163 | * returned. It is not wise to check for this value, but rather, check for | |
164 | * NULL or !NULL instead as to eliminate the need for #ifdef in the calling | |
165 | * code. | |
166 | */ | |
167 | struct dentry *debugfs_create_u32(const char *name, mode_t mode, | |
168 | struct dentry *parent, u32 *value) | |
169 | { | |
170 | return debugfs_create_file(name, mode, parent, value, &fops_u32); | |
171 | } | |
172 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
173 | ||
174 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, | |
175 | size_t count, loff_t *ppos) | |
176 | { | |
177 | char buf[3]; | |
178 | u32 *val = file->private_data; | |
179 | ||
180 | if (*val) | |
181 | buf[0] = 'Y'; | |
182 | else | |
183 | buf[0] = 'N'; | |
184 | buf[1] = '\n'; | |
185 | buf[2] = 0x00; | |
186 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
187 | } | |
188 | ||
189 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, | |
190 | size_t count, loff_t *ppos) | |
191 | { | |
192 | char buf[32]; | |
193 | int buf_size; | |
194 | u32 *val = file->private_data; | |
195 | ||
196 | buf_size = min(count, (sizeof(buf)-1)); | |
197 | if (copy_from_user(buf, user_buf, buf_size)) | |
198 | return -EFAULT; | |
199 | ||
200 | switch (buf[0]) { | |
201 | case 'y': | |
202 | case 'Y': | |
203 | case '1': | |
204 | *val = 1; | |
205 | break; | |
206 | case 'n': | |
207 | case 'N': | |
208 | case '0': | |
209 | *val = 0; | |
210 | break; | |
211 | } | |
212 | ||
213 | return count; | |
214 | } | |
215 | ||
216 | static struct file_operations fops_bool = { | |
217 | .read = read_file_bool, | |
218 | .write = write_file_bool, | |
219 | .open = default_open, | |
220 | }; | |
221 | ||
222 | /** | |
223 | * debugfs_create_bool - create a file in the debugfs filesystem that is used to read and write a boolean value. | |
224 | * | |
225 | * @name: a pointer to a string containing the name of the file to create. | |
226 | * @mode: the permission that the file should have | |
227 | * @parent: a pointer to the parent dentry for this file. This should be a | |
228 | * directory dentry if set. If this paramater is NULL, then the | |
229 | * file will be created in the root of the debugfs filesystem. | |
230 | * @value: a pointer to the variable that the file should read to and write | |
231 | * from. | |
232 | * | |
233 | * This function creates a file in debugfs with the given name that | |
234 | * contains the value of the variable @value. If the @mode variable is so | |
235 | * set, it can be read from, and written to. | |
236 | * | |
237 | * This function will return a pointer to a dentry if it succeeds. This | |
238 | * pointer must be passed to the debugfs_remove() function when the file is | |
239 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
240 | * you are responsible here.) If an error occurs, NULL will be returned. | |
241 | * | |
242 | * If debugfs is not enabled in the kernel, the value -ENODEV will be | |
243 | * returned. It is not wise to check for this value, but rather, check for | |
244 | * NULL or !NULL instead as to eliminate the need for #ifdef in the calling | |
245 | * code. | |
246 | */ | |
247 | struct dentry *debugfs_create_bool(const char *name, mode_t mode, | |
248 | struct dentry *parent, u32 *value) | |
249 | { | |
250 | return debugfs_create_file(name, mode, parent, value, &fops_bool); | |
251 | } | |
252 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
253 |