]>
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. | |
883ce42e | 12 | * See Documentation/DocBook/filesystems for more details. |
1da177e4 LT |
13 | * |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/fs.h> | |
1a087c6a | 18 | #include <linux/seq_file.h> |
1da177e4 | 19 | #include <linux/pagemap.h> |
66f54963 | 20 | #include <linux/namei.h> |
1da177e4 | 21 | #include <linux/debugfs.h> |
03e099fb | 22 | #include <linux/io.h> |
9fe2a701 | 23 | #include <linux/slab.h> |
3a76e5e0 | 24 | #include <linux/atomic.h> |
98210b7f | 25 | #include <linux/device.h> |
1da177e4 LT |
26 | |
27 | static ssize_t default_read_file(struct file *file, char __user *buf, | |
28 | size_t count, loff_t *ppos) | |
29 | { | |
30 | return 0; | |
31 | } | |
32 | ||
33 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
34 | size_t count, loff_t *ppos) | |
35 | { | |
36 | return count; | |
37 | } | |
38 | ||
4b6f5d20 | 39 | const struct file_operations debugfs_file_operations = { |
1da177e4 LT |
40 | .read = default_read_file, |
41 | .write = default_write_file, | |
234e3405 | 42 | .open = simple_open, |
6038f373 | 43 | .llseek = noop_llseek, |
1da177e4 LT |
44 | }; |
45 | ||
66f54963 PO |
46 | static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
47 | { | |
48 | nd_set_link(nd, dentry->d_inode->i_private); | |
49 | return NULL; | |
50 | } | |
51 | ||
52 | const struct inode_operations debugfs_link_operations = { | |
53 | .readlink = generic_readlink, | |
54 | .follow_link = debugfs_follow_link, | |
55 | }; | |
56 | ||
8b88b099 | 57 | static int debugfs_u8_set(void *data, u64 val) |
acaefc25 AB |
58 | { |
59 | *(u8 *)data = val; | |
8b88b099 | 60 | return 0; |
acaefc25 | 61 | } |
8b88b099 | 62 | static int debugfs_u8_get(void *data, u64 *val) |
acaefc25 | 63 | { |
8b88b099 CH |
64 | *val = *(u8 *)data; |
65 | return 0; | |
acaefc25 AB |
66 | } |
67 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); | |
e4792aa3 RG |
68 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); |
69 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
70 | |
71 | /** | |
6468b3af | 72 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
1da177e4 LT |
73 | * @name: a pointer to a string containing the name of the file to create. |
74 | * @mode: the permission that the file should have | |
75 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 76 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
77 | * file will be created in the root of the debugfs filesystem. |
78 | * @value: a pointer to the variable that the file should read to and write | |
79 | * from. | |
80 | * | |
81 | * This function creates a file in debugfs with the given name that | |
82 | * contains the value of the variable @value. If the @mode variable is so | |
83 | * set, it can be read from, and written to. | |
84 | * | |
85 | * This function will return a pointer to a dentry if it succeeds. This | |
86 | * pointer must be passed to the debugfs_remove() function when the file is | |
87 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 88 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 89 | * |
6468b3af | 90 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 91 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 92 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
93 | * code. |
94 | */ | |
f4ae40a6 | 95 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, |
1da177e4 LT |
96 | struct dentry *parent, u8 *value) |
97 | { | |
e4792aa3 RG |
98 | /* if there are no write bits set, make read only */ |
99 | if (!(mode & S_IWUGO)) | |
100 | return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); | |
101 | /* if there are no read bits set, make write only */ | |
102 | if (!(mode & S_IRUGO)) | |
103 | return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); | |
104 | ||
1da177e4 LT |
105 | return debugfs_create_file(name, mode, parent, value, &fops_u8); |
106 | } | |
107 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
108 | ||
8b88b099 | 109 | static int debugfs_u16_set(void *data, u64 val) |
acaefc25 AB |
110 | { |
111 | *(u16 *)data = val; | |
8b88b099 | 112 | return 0; |
acaefc25 | 113 | } |
8b88b099 | 114 | static int debugfs_u16_get(void *data, u64 *val) |
acaefc25 | 115 | { |
8b88b099 CH |
116 | *val = *(u16 *)data; |
117 | return 0; | |
acaefc25 AB |
118 | } |
119 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); | |
e4792aa3 RG |
120 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); |
121 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | |
acaefc25 | 122 | |
1da177e4 | 123 | /** |
6468b3af | 124 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
1da177e4 LT |
125 | * @name: a pointer to a string containing the name of the file to create. |
126 | * @mode: the permission that the file should have | |
127 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 128 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
129 | * file will be created in the root of the debugfs filesystem. |
130 | * @value: a pointer to the variable that the file should read to and write | |
131 | * from. | |
132 | * | |
133 | * This function creates a file in debugfs with the given name that | |
134 | * contains the value of the variable @value. If the @mode variable is so | |
135 | * set, it can be read from, and written to. | |
136 | * | |
137 | * This function will return a pointer to a dentry if it succeeds. This | |
138 | * pointer must be passed to the debugfs_remove() function when the file is | |
139 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 140 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 141 | * |
6468b3af | 142 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 143 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 144 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
145 | * code. |
146 | */ | |
f4ae40a6 | 147 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, |
1da177e4 LT |
148 | struct dentry *parent, u16 *value) |
149 | { | |
e4792aa3 RG |
150 | /* if there are no write bits set, make read only */ |
151 | if (!(mode & S_IWUGO)) | |
152 | return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); | |
153 | /* if there are no read bits set, make write only */ | |
154 | if (!(mode & S_IRUGO)) | |
155 | return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); | |
156 | ||
1da177e4 LT |
157 | return debugfs_create_file(name, mode, parent, value, &fops_u16); |
158 | } | |
159 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
160 | ||
8b88b099 | 161 | static int debugfs_u32_set(void *data, u64 val) |
acaefc25 AB |
162 | { |
163 | *(u32 *)data = val; | |
8b88b099 | 164 | return 0; |
acaefc25 | 165 | } |
8b88b099 | 166 | static int debugfs_u32_get(void *data, u64 *val) |
acaefc25 | 167 | { |
8b88b099 CH |
168 | *val = *(u32 *)data; |
169 | return 0; | |
acaefc25 AB |
170 | } |
171 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); | |
e4792aa3 RG |
172 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); |
173 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | |
acaefc25 | 174 | |
1da177e4 | 175 | /** |
6468b3af | 176 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
1da177e4 LT |
177 | * @name: a pointer to a string containing the name of the file to create. |
178 | * @mode: the permission that the file should have | |
179 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 180 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
181 | * file will be created in the root of the debugfs filesystem. |
182 | * @value: a pointer to the variable that the file should read to and write | |
183 | * from. | |
184 | * | |
185 | * This function creates a file in debugfs with the given name that | |
186 | * contains the value of the variable @value. If the @mode variable is so | |
187 | * set, it can be read from, and written to. | |
188 | * | |
189 | * This function will return a pointer to a dentry if it succeeds. This | |
190 | * pointer must be passed to the debugfs_remove() function when the file is | |
191 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 192 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 193 | * |
6468b3af | 194 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 195 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 196 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
197 | * code. |
198 | */ | |
f4ae40a6 | 199 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, |
1da177e4 LT |
200 | struct dentry *parent, u32 *value) |
201 | { | |
e4792aa3 RG |
202 | /* if there are no write bits set, make read only */ |
203 | if (!(mode & S_IWUGO)) | |
204 | return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); | |
205 | /* if there are no read bits set, make write only */ | |
206 | if (!(mode & S_IRUGO)) | |
207 | return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); | |
208 | ||
1da177e4 LT |
209 | return debugfs_create_file(name, mode, parent, value, &fops_u32); |
210 | } | |
211 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
212 | ||
8b88b099 | 213 | static int debugfs_u64_set(void *data, u64 val) |
8447891f ME |
214 | { |
215 | *(u64 *)data = val; | |
8b88b099 | 216 | return 0; |
8447891f ME |
217 | } |
218 | ||
8b88b099 | 219 | static int debugfs_u64_get(void *data, u64 *val) |
8447891f | 220 | { |
8b88b099 CH |
221 | *val = *(u64 *)data; |
222 | return 0; | |
8447891f ME |
223 | } |
224 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); | |
e4792aa3 RG |
225 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); |
226 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | |
8447891f ME |
227 | |
228 | /** | |
229 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
230 | * @name: a pointer to a string containing the name of the file to create. | |
231 | * @mode: the permission that the file should have | |
232 | * @parent: a pointer to the parent dentry for this file. This should be a | |
233 | * directory dentry if set. If this parameter is %NULL, then the | |
234 | * file will be created in the root of the debugfs filesystem. | |
235 | * @value: a pointer to the variable that the file should read to and write | |
236 | * from. | |
237 | * | |
238 | * This function creates a file in debugfs with the given name that | |
239 | * contains the value of the variable @value. If the @mode variable is so | |
240 | * set, it can be read from, and written to. | |
241 | * | |
242 | * This function will return a pointer to a dentry if it succeeds. This | |
243 | * pointer must be passed to the debugfs_remove() function when the file is | |
244 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
245 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
246 | * | |
247 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
248 | * returned. It is not wise to check for this value, but rather, check for | |
249 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
250 | * code. | |
251 | */ | |
f4ae40a6 | 252 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, |
8447891f ME |
253 | struct dentry *parent, u64 *value) |
254 | { | |
e4792aa3 RG |
255 | /* if there are no write bits set, make read only */ |
256 | if (!(mode & S_IWUGO)) | |
257 | return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); | |
258 | /* if there are no read bits set, make write only */ | |
259 | if (!(mode & S_IRUGO)) | |
260 | return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); | |
261 | ||
8447891f ME |
262 | return debugfs_create_file(name, mode, parent, value, &fops_u64); |
263 | } | |
264 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | |
265 | ||
2ebefc50 | 266 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
e4792aa3 RG |
267 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); |
268 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); | |
2ebefc50 RG |
269 | |
270 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); | |
e4792aa3 RG |
271 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); |
272 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); | |
2ebefc50 RG |
273 | |
274 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); | |
e4792aa3 RG |
275 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); |
276 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); | |
2ebefc50 | 277 | |
15b0beaa HY |
278 | DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); |
279 | ||
e6716b87 | 280 | /* |
15b0beaa | 281 | * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value |
2ebefc50 | 282 | * |
e6716b87 RD |
283 | * These functions are exactly the same as the above functions (but use a hex |
284 | * output for the decimal challenged). For details look at the above unsigned | |
2ebefc50 RG |
285 | * decimal functions. |
286 | */ | |
e6716b87 RD |
287 | |
288 | /** | |
289 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value | |
290 | * @name: a pointer to a string containing the name of the file to create. | |
291 | * @mode: the permission that the file should have | |
292 | * @parent: a pointer to the parent dentry for this file. This should be a | |
293 | * directory dentry if set. If this parameter is %NULL, then the | |
294 | * file will be created in the root of the debugfs filesystem. | |
295 | * @value: a pointer to the variable that the file should read to and write | |
296 | * from. | |
297 | */ | |
f4ae40a6 | 298 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, |
2ebefc50 RG |
299 | struct dentry *parent, u8 *value) |
300 | { | |
e4792aa3 RG |
301 | /* if there are no write bits set, make read only */ |
302 | if (!(mode & S_IWUGO)) | |
303 | return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); | |
304 | /* if there are no read bits set, make write only */ | |
305 | if (!(mode & S_IRUGO)) | |
306 | return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); | |
307 | ||
2ebefc50 RG |
308 | return debugfs_create_file(name, mode, parent, value, &fops_x8); |
309 | } | |
310 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | |
311 | ||
e6716b87 RD |
312 | /** |
313 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | |
314 | * @name: a pointer to a string containing the name of the file to create. | |
315 | * @mode: the permission that the file should have | |
316 | * @parent: a pointer to the parent dentry for this file. This should be a | |
317 | * directory dentry if set. If this parameter is %NULL, then the | |
318 | * file will be created in the root of the debugfs filesystem. | |
319 | * @value: a pointer to the variable that the file should read to and write | |
320 | * from. | |
321 | */ | |
f4ae40a6 | 322 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, |
2ebefc50 RG |
323 | struct dentry *parent, u16 *value) |
324 | { | |
e4792aa3 RG |
325 | /* if there are no write bits set, make read only */ |
326 | if (!(mode & S_IWUGO)) | |
327 | return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); | |
328 | /* if there are no read bits set, make write only */ | |
329 | if (!(mode & S_IRUGO)) | |
330 | return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); | |
331 | ||
2ebefc50 RG |
332 | return debugfs_create_file(name, mode, parent, value, &fops_x16); |
333 | } | |
334 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | |
335 | ||
e6716b87 RD |
336 | /** |
337 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | |
338 | * @name: a pointer to a string containing the name of the file to create. | |
339 | * @mode: the permission that the file should have | |
340 | * @parent: a pointer to the parent dentry for this file. This should be a | |
341 | * directory dentry if set. If this parameter is %NULL, then the | |
342 | * file will be created in the root of the debugfs filesystem. | |
343 | * @value: a pointer to the variable that the file should read to and write | |
344 | * from. | |
345 | */ | |
f4ae40a6 | 346 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, |
2ebefc50 RG |
347 | struct dentry *parent, u32 *value) |
348 | { | |
e4792aa3 RG |
349 | /* if there are no write bits set, make read only */ |
350 | if (!(mode & S_IWUGO)) | |
351 | return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); | |
352 | /* if there are no read bits set, make write only */ | |
353 | if (!(mode & S_IRUGO)) | |
354 | return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); | |
355 | ||
2ebefc50 RG |
356 | return debugfs_create_file(name, mode, parent, value, &fops_x32); |
357 | } | |
358 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | |
359 | ||
15b0beaa HY |
360 | /** |
361 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
362 | * @name: a pointer to a string containing the name of the file to create. | |
363 | * @mode: the permission that the file should have | |
364 | * @parent: a pointer to the parent dentry for this file. This should be a | |
365 | * directory dentry if set. If this parameter is %NULL, then the | |
366 | * file will be created in the root of the debugfs filesystem. | |
367 | * @value: a pointer to the variable that the file should read to and write | |
368 | * from. | |
369 | */ | |
f4ae40a6 | 370 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, |
15b0beaa HY |
371 | struct dentry *parent, u64 *value) |
372 | { | |
373 | return debugfs_create_file(name, mode, parent, value, &fops_x64); | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | |
376 | ||
5e078787 IPG |
377 | |
378 | static int debugfs_size_t_set(void *data, u64 val) | |
379 | { | |
380 | *(size_t *)data = val; | |
381 | return 0; | |
382 | } | |
383 | static int debugfs_size_t_get(void *data, u64 *val) | |
384 | { | |
385 | *val = *(size_t *)data; | |
386 | return 0; | |
387 | } | |
388 | DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, | |
389 | "%llu\n"); /* %llu and %zu are more or less the same */ | |
390 | ||
391 | /** | |
392 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | |
393 | * @name: a pointer to a string containing the name of the file to create. | |
394 | * @mode: the permission that the file should have | |
395 | * @parent: a pointer to the parent dentry for this file. This should be a | |
396 | * directory dentry if set. If this parameter is %NULL, then the | |
397 | * file will be created in the root of the debugfs filesystem. | |
398 | * @value: a pointer to the variable that the file should read to and write | |
399 | * from. | |
400 | */ | |
f4ae40a6 | 401 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, |
5e078787 IPG |
402 | struct dentry *parent, size_t *value) |
403 | { | |
404 | return debugfs_create_file(name, mode, parent, value, &fops_size_t); | |
405 | } | |
406 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | |
407 | ||
3a76e5e0 SJ |
408 | static int debugfs_atomic_t_set(void *data, u64 val) |
409 | { | |
410 | atomic_set((atomic_t *)data, val); | |
411 | return 0; | |
412 | } | |
413 | static int debugfs_atomic_t_get(void *data, u64 *val) | |
414 | { | |
415 | *val = atomic_read((atomic_t *)data); | |
416 | return 0; | |
417 | } | |
418 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, | |
419 | debugfs_atomic_t_set, "%lld\n"); | |
420 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n"); | |
421 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n"); | |
422 | ||
423 | /** | |
424 | * debugfs_create_atomic_t - create a debugfs file that is used to read and | |
425 | * write an atomic_t value | |
426 | * @name: a pointer to a string containing the name of the file to create. | |
427 | * @mode: the permission that the file should have | |
428 | * @parent: a pointer to the parent dentry for this file. This should be a | |
429 | * directory dentry if set. If this parameter is %NULL, then the | |
430 | * file will be created in the root of the debugfs filesystem. | |
431 | * @value: a pointer to the variable that the file should read to and write | |
432 | * from. | |
433 | */ | |
434 | struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, | |
435 | struct dentry *parent, atomic_t *value) | |
436 | { | |
437 | /* if there are no write bits set, make read only */ | |
438 | if (!(mode & S_IWUGO)) | |
439 | return debugfs_create_file(name, mode, parent, value, | |
440 | &fops_atomic_t_ro); | |
441 | /* if there are no read bits set, make write only */ | |
442 | if (!(mode & S_IRUGO)) | |
443 | return debugfs_create_file(name, mode, parent, value, | |
444 | &fops_atomic_t_wo); | |
445 | ||
446 | return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); | |
447 | } | |
448 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); | |
5e078787 | 449 | |
1da177e4 LT |
450 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, |
451 | size_t count, loff_t *ppos) | |
452 | { | |
453 | char buf[3]; | |
454 | u32 *val = file->private_data; | |
88e412ea | 455 | |
1da177e4 LT |
456 | if (*val) |
457 | buf[0] = 'Y'; | |
458 | else | |
459 | buf[0] = 'N'; | |
460 | buf[1] = '\n'; | |
461 | buf[2] = 0x00; | |
462 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
463 | } | |
464 | ||
465 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, | |
466 | size_t count, loff_t *ppos) | |
467 | { | |
468 | char buf[32]; | |
c42d2237 | 469 | size_t buf_size; |
8705b48e | 470 | bool bv; |
1da177e4 LT |
471 | u32 *val = file->private_data; |
472 | ||
473 | buf_size = min(count, (sizeof(buf)-1)); | |
474 | if (copy_from_user(buf, user_buf, buf_size)) | |
475 | return -EFAULT; | |
476 | ||
a3b2c8c7 | 477 | buf[buf_size] = '\0'; |
8705b48e JC |
478 | if (strtobool(buf, &bv) == 0) |
479 | *val = bv; | |
480 | ||
1da177e4 LT |
481 | return count; |
482 | } | |
483 | ||
4b6f5d20 | 484 | static const struct file_operations fops_bool = { |
1da177e4 LT |
485 | .read = read_file_bool, |
486 | .write = write_file_bool, | |
234e3405 | 487 | .open = simple_open, |
6038f373 | 488 | .llseek = default_llseek, |
1da177e4 LT |
489 | }; |
490 | ||
491 | /** | |
6468b3af | 492 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
1da177e4 LT |
493 | * @name: a pointer to a string containing the name of the file to create. |
494 | * @mode: the permission that the file should have | |
495 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 496 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
497 | * file will be created in the root of the debugfs filesystem. |
498 | * @value: a pointer to the variable that the file should read to and write | |
499 | * from. | |
500 | * | |
501 | * This function creates a file in debugfs with the given name that | |
502 | * contains the value of the variable @value. If the @mode variable is so | |
503 | * set, it can be read from, and written to. | |
504 | * | |
505 | * This function will return a pointer to a dentry if it succeeds. This | |
506 | * pointer must be passed to the debugfs_remove() function when the file is | |
507 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 508 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 509 | * |
6468b3af | 510 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 511 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 512 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
513 | * code. |
514 | */ | |
f4ae40a6 | 515 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, |
1da177e4 LT |
516 | struct dentry *parent, u32 *value) |
517 | { | |
518 | return debugfs_create_file(name, mode, parent, value, &fops_bool); | |
519 | } | |
520 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
521 | ||
dd308bc3 ME |
522 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
523 | size_t count, loff_t *ppos) | |
524 | { | |
525 | struct debugfs_blob_wrapper *blob = file->private_data; | |
526 | return simple_read_from_buffer(user_buf, count, ppos, blob->data, | |
527 | blob->size); | |
528 | } | |
529 | ||
00977a59 | 530 | static const struct file_operations fops_blob = { |
dd308bc3 | 531 | .read = read_file_blob, |
234e3405 | 532 | .open = simple_open, |
6038f373 | 533 | .llseek = default_llseek, |
dd308bc3 ME |
534 | }; |
535 | ||
536 | /** | |
400ced61 | 537 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
dd308bc3 ME |
538 | * @name: a pointer to a string containing the name of the file to create. |
539 | * @mode: the permission that the file should have | |
540 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 541 | * directory dentry if set. If this parameter is %NULL, then the |
dd308bc3 ME |
542 | * file will be created in the root of the debugfs filesystem. |
543 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | |
544 | * to the blob data and the size of the data. | |
545 | * | |
546 | * This function creates a file in debugfs with the given name that exports | |
547 | * @blob->data as a binary blob. If the @mode variable is so set it can be | |
548 | * read from. Writing is not supported. | |
549 | * | |
550 | * This function will return a pointer to a dentry if it succeeds. This | |
551 | * pointer must be passed to the debugfs_remove() function when the file is | |
552 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 553 | * you are responsible here.) If an error occurs, %NULL will be returned. |
dd308bc3 | 554 | * |
6468b3af | 555 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
dd308bc3 | 556 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 557 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
dd308bc3 ME |
558 | * code. |
559 | */ | |
f4ae40a6 | 560 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
dd308bc3 ME |
561 | struct dentry *parent, |
562 | struct debugfs_blob_wrapper *blob) | |
563 | { | |
564 | return debugfs_create_file(name, mode, parent, blob, &fops_blob); | |
565 | } | |
566 | EXPORT_SYMBOL_GPL(debugfs_create_blob); | |
1a087c6a | 567 | |
9fe2a701 SV |
568 | struct array_data { |
569 | void *array; | |
570 | u32 elements; | |
571 | }; | |
572 | ||
e05e279e LT |
573 | static size_t u32_format_array(char *buf, size_t bufsize, |
574 | u32 *array, int array_size) | |
9fe2a701 SV |
575 | { |
576 | size_t ret = 0; | |
9fe2a701 | 577 | |
e05e279e | 578 | while (--array_size >= 0) { |
9fe2a701 | 579 | size_t len; |
e05e279e | 580 | char term = array_size ? ' ' : '\n'; |
9fe2a701 | 581 | |
e05e279e | 582 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
9fe2a701 SV |
583 | ret += len; |
584 | ||
e05e279e LT |
585 | buf += len; |
586 | bufsize -= len; | |
9fe2a701 | 587 | } |
9fe2a701 SV |
588 | return ret; |
589 | } | |
590 | ||
36048853 | 591 | static int u32_array_open(struct inode *inode, struct file *file) |
9fe2a701 | 592 | { |
9fe2a701 | 593 | struct array_data *data = inode->i_private; |
e05e279e LT |
594 | int size, elements = data->elements; |
595 | char *buf; | |
596 | ||
597 | /* | |
598 | * Max size: | |
599 | * - 10 digits + ' '/'\n' = 11 bytes per number | |
600 | * - terminating NUL character | |
601 | */ | |
602 | size = elements*11; | |
603 | buf = kmalloc(size+1, GFP_KERNEL); | |
604 | if (!buf) | |
36048853 | 605 | return -ENOMEM; |
e05e279e LT |
606 | buf[size] = 0; |
607 | ||
608 | file->private_data = buf; | |
609 | u32_format_array(buf, size, data->array, data->elements); | |
610 | ||
36048853 DR |
611 | return nonseekable_open(inode, file); |
612 | } | |
9fe2a701 | 613 | |
36048853 DR |
614 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
615 | loff_t *ppos) | |
616 | { | |
617 | size_t size = strlen(file->private_data); | |
9fe2a701 SV |
618 | |
619 | return simple_read_from_buffer(buf, len, ppos, | |
620 | file->private_data, size); | |
621 | } | |
622 | ||
623 | static int u32_array_release(struct inode *inode, struct file *file) | |
624 | { | |
625 | kfree(file->private_data); | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | static const struct file_operations u32_array_fops = { | |
631 | .owner = THIS_MODULE, | |
632 | .open = u32_array_open, | |
633 | .release = u32_array_release, | |
634 | .read = u32_array_read, | |
635 | .llseek = no_llseek, | |
636 | }; | |
637 | ||
638 | /** | |
639 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 | |
640 | * array. | |
641 | * @name: a pointer to a string containing the name of the file to create. | |
642 | * @mode: the permission that the file should have. | |
643 | * @parent: a pointer to the parent dentry for this file. This should be a | |
644 | * directory dentry if set. If this parameter is %NULL, then the | |
645 | * file will be created in the root of the debugfs filesystem. | |
646 | * @array: u32 array that provides data. | |
647 | * @elements: total number of elements in the array. | |
648 | * | |
649 | * This function creates a file in debugfs with the given name that exports | |
650 | * @array as data. If the @mode variable is so set it can be read from. | |
651 | * Writing is not supported. Seek within the file is also not supported. | |
652 | * Once array is created its size can not be changed. | |
653 | * | |
654 | * The function returns a pointer to dentry on success. If debugfs is not | |
655 | * enabled in the kernel, the value -%ENODEV will be returned. | |
656 | */ | |
657 | struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, | |
658 | struct dentry *parent, | |
659 | u32 *array, u32 elements) | |
660 | { | |
661 | struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); | |
662 | ||
663 | if (data == NULL) | |
664 | return NULL; | |
665 | ||
666 | data->array = array; | |
667 | data->elements = elements; | |
668 | ||
669 | return debugfs_create_file(name, mode, parent, data, &u32_array_fops); | |
670 | } | |
671 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); | |
672 | ||
3b85e4ab HC |
673 | #ifdef CONFIG_HAS_IOMEM |
674 | ||
1a087c6a AR |
675 | /* |
676 | * The regset32 stuff is used to print 32-bit registers using the | |
677 | * seq_file utilities. We offer printing a register set in an already-opened | |
678 | * sequential file or create a debugfs file that only prints a regset32. | |
679 | */ | |
680 | ||
681 | /** | |
682 | * debugfs_print_regs32 - use seq_print to describe a set of registers | |
683 | * @s: the seq_file structure being used to generate output | |
684 | * @regs: an array if struct debugfs_reg32 structures | |
b5763acc | 685 | * @nregs: the length of the above array |
1a087c6a AR |
686 | * @base: the base address to be used in reading the registers |
687 | * @prefix: a string to be prefixed to every output line | |
688 | * | |
689 | * This function outputs a text block describing the current values of | |
690 | * some 32-bit hardware registers. It is meant to be used within debugfs | |
691 | * files based on seq_file that need to show registers, intermixed with other | |
692 | * information. The prefix argument may be used to specify a leading string, | |
693 | * because some peripherals have several blocks of identical registers, | |
694 | * for example configuration of dma channels | |
695 | */ | |
9761536e JP |
696 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
697 | int nregs, void __iomem *base, char *prefix) | |
1a087c6a | 698 | { |
9761536e | 699 | int i; |
1a087c6a AR |
700 | |
701 | for (i = 0; i < nregs; i++, regs++) { | |
702 | if (prefix) | |
9761536e JP |
703 | seq_printf(s, "%s", prefix); |
704 | seq_printf(s, "%s = 0x%08x\n", regs->name, | |
705 | readl(base + regs->offset)); | |
706 | if (seq_has_overflowed(s)) | |
707 | break; | |
1a087c6a | 708 | } |
1a087c6a AR |
709 | } |
710 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); | |
711 | ||
712 | static int debugfs_show_regset32(struct seq_file *s, void *data) | |
713 | { | |
714 | struct debugfs_regset32 *regset = s->private; | |
715 | ||
716 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); | |
717 | return 0; | |
718 | } | |
719 | ||
720 | static int debugfs_open_regset32(struct inode *inode, struct file *file) | |
721 | { | |
722 | return single_open(file, debugfs_show_regset32, inode->i_private); | |
723 | } | |
724 | ||
725 | static const struct file_operations fops_regset32 = { | |
726 | .open = debugfs_open_regset32, | |
727 | .read = seq_read, | |
728 | .llseek = seq_lseek, | |
729 | .release = single_release, | |
730 | }; | |
731 | ||
732 | /** | |
733 | * debugfs_create_regset32 - create a debugfs file that returns register values | |
734 | * @name: a pointer to a string containing the name of the file to create. | |
735 | * @mode: the permission that the file should have | |
736 | * @parent: a pointer to the parent dentry for this file. This should be a | |
737 | * directory dentry if set. If this parameter is %NULL, then the | |
738 | * file will be created in the root of the debugfs filesystem. | |
739 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer | |
740 | * to an array of register definitions, the array size and the base | |
741 | * address where the register bank is to be found. | |
742 | * | |
743 | * This function creates a file in debugfs with the given name that reports | |
744 | * the names and values of a set of 32-bit registers. If the @mode variable | |
745 | * is so set it can be read from. Writing is not supported. | |
746 | * | |
747 | * This function will return a pointer to a dentry if it succeeds. This | |
748 | * pointer must be passed to the debugfs_remove() function when the file is | |
749 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
750 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
751 | * | |
752 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
753 | * returned. It is not wise to check for this value, but rather, check for | |
754 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
755 | * code. | |
756 | */ | |
88187398 | 757 | struct dentry *debugfs_create_regset32(const char *name, umode_t mode, |
1a087c6a AR |
758 | struct dentry *parent, |
759 | struct debugfs_regset32 *regset) | |
760 | { | |
761 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); | |
762 | } | |
763 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); | |
3b85e4ab HC |
764 | |
765 | #endif /* CONFIG_HAS_IOMEM */ | |
98210b7f AS |
766 | |
767 | struct debugfs_devm_entry { | |
768 | int (*read)(struct seq_file *seq, void *data); | |
769 | struct device *dev; | |
770 | }; | |
771 | ||
772 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) | |
773 | { | |
774 | struct debugfs_devm_entry *entry = inode->i_private; | |
775 | ||
776 | return single_open(f, entry->read, entry->dev); | |
777 | } | |
778 | ||
779 | static const struct file_operations debugfs_devm_entry_ops = { | |
780 | .owner = THIS_MODULE, | |
781 | .open = debugfs_devm_entry_open, | |
782 | .release = single_release, | |
783 | .read = seq_read, | |
784 | .llseek = seq_lseek | |
785 | }; | |
786 | ||
787 | /** | |
788 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. | |
789 | * | |
790 | * @dev: device related to this debugfs file. | |
791 | * @name: name of the debugfs file. | |
792 | * @parent: a pointer to the parent dentry for this file. This should be a | |
793 | * directory dentry if set. If this parameter is %NULL, then the | |
794 | * file will be created in the root of the debugfs filesystem. | |
795 | * @read_fn: function pointer called to print the seq_file content. | |
796 | */ | |
797 | struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, | |
798 | struct dentry *parent, | |
799 | int (*read_fn)(struct seq_file *s, | |
800 | void *data)) | |
801 | { | |
802 | struct debugfs_devm_entry *entry; | |
803 | ||
804 | if (IS_ERR(parent)) | |
805 | return ERR_PTR(-ENOENT); | |
806 | ||
807 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); | |
808 | if (!entry) | |
809 | return ERR_PTR(-ENOMEM); | |
810 | ||
811 | entry->read = read_fn; | |
812 | entry->dev = dev; | |
813 | ||
814 | return debugfs_create_file(name, S_IRUGO, parent, entry, | |
815 | &debugfs_devm_entry_ops); | |
816 | } | |
817 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); | |
818 |