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. | |
e1b4fc7a | 12 | * See Documentation/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 LT |
19 | #include <linux/pagemap.h> |
20 | #include <linux/debugfs.h> | |
03e099fb | 21 | #include <linux/io.h> |
9fe2a701 | 22 | #include <linux/slab.h> |
3a76e5e0 | 23 | #include <linux/atomic.h> |
98210b7f | 24 | #include <linux/device.h> |
49d200de | 25 | #include <asm/poll.h> |
9fd4dcec NS |
26 | |
27 | #include "internal.h" | |
1da177e4 | 28 | |
49d200de NS |
29 | struct poll_table_struct; |
30 | ||
1da177e4 LT |
31 | static ssize_t default_read_file(struct file *file, char __user *buf, |
32 | size_t count, loff_t *ppos) | |
33 | { | |
34 | return 0; | |
35 | } | |
36 | ||
37 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
38 | size_t count, loff_t *ppos) | |
39 | { | |
40 | return count; | |
41 | } | |
42 | ||
9fd4dcec | 43 | const struct file_operations debugfs_noop_file_operations = { |
1da177e4 LT |
44 | .read = default_read_file, |
45 | .write = default_write_file, | |
234e3405 | 46 | .open = simple_open, |
6038f373 | 47 | .llseek = noop_llseek, |
1da177e4 LT |
48 | }; |
49 | ||
9fd4dcec NS |
50 | #define F_DENTRY(filp) ((filp)->f_path.dentry) |
51 | ||
7c8d4698 | 52 | const struct file_operations *debugfs_real_fops(const struct file *filp) |
7c8d4698 NS |
53 | { |
54 | struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; | |
055ab8e3 | 55 | |
7c8d4698 NS |
56 | return fsd->real_fops; |
57 | } | |
58 | EXPORT_SYMBOL_GPL(debugfs_real_fops); | |
59 | ||
e9117a5a NS |
60 | /** |
61 | * debugfs_file_get - mark the beginning of file data access | |
62 | * @dentry: the dentry object whose data is being accessed. | |
63 | * | |
64 | * Up to a matching call to debugfs_file_put(), any successive call | |
65 | * into the file removing functions debugfs_remove() and | |
66 | * debugfs_remove_recursive() will block. Since associated private | |
67 | * file data may only get freed after a successful return of any of | |
68 | * the removal functions, you may safely access it after a successful | |
69 | * call to debugfs_file_get() without worrying about lifetime issues. | |
70 | * | |
71 | * If -%EIO is returned, the file has already been removed and thus, | |
72 | * it is not safe to access any of its data. If, on the other hand, | |
73 | * it is allowed to access the file data, zero is returned. | |
74 | */ | |
75 | int debugfs_file_get(struct dentry *dentry) | |
76 | { | |
77 | struct debugfs_fsdata *fsd = dentry->d_fsdata; | |
78 | ||
79 | /* Avoid starvation of removers. */ | |
80 | if (d_unlinked(dentry)) | |
81 | return -EIO; | |
82 | ||
83 | if (!refcount_inc_not_zero(&fsd->active_users)) | |
84 | return -EIO; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | EXPORT_SYMBOL_GPL(debugfs_file_get); | |
89 | ||
90 | /** | |
91 | * debugfs_file_put - mark the end of file data access | |
92 | * @dentry: the dentry object formerly passed to | |
93 | * debugfs_file_get(). | |
94 | * | |
95 | * Allow any ongoing concurrent call into debugfs_remove() or | |
96 | * debugfs_remove_recursive() blocked by a former call to | |
97 | * debugfs_file_get() to proceed and return to its caller. | |
98 | */ | |
99 | void debugfs_file_put(struct dentry *dentry) | |
100 | { | |
101 | struct debugfs_fsdata *fsd = dentry->d_fsdata; | |
102 | ||
103 | if (refcount_dec_and_test(&fsd->active_users)) | |
104 | complete(&fsd->active_users_drained); | |
105 | } | |
106 | EXPORT_SYMBOL_GPL(debugfs_file_put); | |
107 | ||
9fd4dcec NS |
108 | static int open_proxy_open(struct inode *inode, struct file *filp) |
109 | { | |
69d29f9e | 110 | struct dentry *dentry = F_DENTRY(filp); |
9fd4dcec | 111 | const struct file_operations *real_fops = NULL; |
69d29f9e | 112 | int r = 0; |
9fd4dcec | 113 | |
69d29f9e NS |
114 | if (debugfs_file_get(dentry)) |
115 | return -ENOENT; | |
9fd4dcec | 116 | |
86f0e067 | 117 | real_fops = debugfs_real_fops(filp); |
9fd4dcec NS |
118 | real_fops = fops_get(real_fops); |
119 | if (!real_fops) { | |
120 | /* Huh? Module did not clean up after itself at exit? */ | |
121 | WARN(1, "debugfs file owner did not clean up at exit: %pd", | |
122 | dentry); | |
123 | r = -ENXIO; | |
124 | goto out; | |
125 | } | |
126 | replace_fops(filp, real_fops); | |
127 | ||
128 | if (real_fops->open) | |
129 | r = real_fops->open(inode, filp); | |
130 | ||
131 | out: | |
69d29f9e | 132 | debugfs_file_put(dentry); |
9fd4dcec NS |
133 | return r; |
134 | } | |
135 | ||
136 | const struct file_operations debugfs_open_proxy_file_operations = { | |
137 | .open = open_proxy_open, | |
138 | }; | |
139 | ||
49d200de NS |
140 | #define PROTO(args...) args |
141 | #define ARGS(args...) args | |
142 | ||
143 | #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ | |
144 | static ret_type full_proxy_ ## name(proto) \ | |
145 | { \ | |
69d29f9e | 146 | struct dentry *dentry = F_DENTRY(filp); \ |
49d200de | 147 | const struct file_operations *real_fops = \ |
86f0e067 | 148 | debugfs_real_fops(filp); \ |
49d200de NS |
149 | ret_type r; \ |
150 | \ | |
69d29f9e NS |
151 | r = debugfs_file_get(dentry); \ |
152 | if (unlikely(r)) \ | |
153 | return r; \ | |
154 | r = real_fops->name(args); \ | |
155 | debugfs_file_put(dentry); \ | |
49d200de NS |
156 | return r; \ |
157 | } | |
158 | ||
159 | FULL_PROXY_FUNC(llseek, loff_t, filp, | |
160 | PROTO(struct file *filp, loff_t offset, int whence), | |
161 | ARGS(filp, offset, whence)); | |
162 | ||
163 | FULL_PROXY_FUNC(read, ssize_t, filp, | |
164 | PROTO(struct file *filp, char __user *buf, size_t size, | |
165 | loff_t *ppos), | |
166 | ARGS(filp, buf, size, ppos)); | |
167 | ||
168 | FULL_PROXY_FUNC(write, ssize_t, filp, | |
169 | PROTO(struct file *filp, const char __user *buf, size_t size, | |
170 | loff_t *ppos), | |
171 | ARGS(filp, buf, size, ppos)); | |
172 | ||
173 | FULL_PROXY_FUNC(unlocked_ioctl, long, filp, | |
174 | PROTO(struct file *filp, unsigned int cmd, unsigned long arg), | |
175 | ARGS(filp, cmd, arg)); | |
176 | ||
177 | static unsigned int full_proxy_poll(struct file *filp, | |
178 | struct poll_table_struct *wait) | |
179 | { | |
86f0e067 | 180 | const struct file_operations *real_fops = debugfs_real_fops(filp); |
69d29f9e | 181 | struct dentry *dentry = F_DENTRY(filp); |
49d200de NS |
182 | unsigned int r = 0; |
183 | ||
69d29f9e | 184 | if (debugfs_file_get(dentry)) |
49d200de | 185 | return POLLHUP; |
49d200de NS |
186 | |
187 | r = real_fops->poll(filp, wait); | |
69d29f9e | 188 | debugfs_file_put(dentry); |
49d200de NS |
189 | return r; |
190 | } | |
191 | ||
192 | static int full_proxy_release(struct inode *inode, struct file *filp) | |
193 | { | |
194 | const struct dentry *dentry = F_DENTRY(filp); | |
86f0e067 | 195 | const struct file_operations *real_fops = debugfs_real_fops(filp); |
49d200de NS |
196 | const struct file_operations *proxy_fops = filp->f_op; |
197 | int r = 0; | |
198 | ||
199 | /* | |
200 | * We must not protect this against removal races here: the | |
201 | * original releaser should be called unconditionally in order | |
202 | * not to leak any resources. Releasers must not assume that | |
203 | * ->i_private is still being meaningful here. | |
204 | */ | |
205 | if (real_fops->release) | |
206 | r = real_fops->release(inode, filp); | |
207 | ||
208 | replace_fops(filp, d_inode(dentry)->i_fop); | |
209 | kfree((void *)proxy_fops); | |
210 | fops_put(real_fops); | |
a1a9e5d2 | 211 | return r; |
49d200de NS |
212 | } |
213 | ||
214 | static void __full_proxy_fops_init(struct file_operations *proxy_fops, | |
215 | const struct file_operations *real_fops) | |
216 | { | |
217 | proxy_fops->release = full_proxy_release; | |
218 | if (real_fops->llseek) | |
219 | proxy_fops->llseek = full_proxy_llseek; | |
220 | if (real_fops->read) | |
221 | proxy_fops->read = full_proxy_read; | |
222 | if (real_fops->write) | |
223 | proxy_fops->write = full_proxy_write; | |
224 | if (real_fops->poll) | |
225 | proxy_fops->poll = full_proxy_poll; | |
226 | if (real_fops->unlocked_ioctl) | |
227 | proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; | |
228 | } | |
229 | ||
230 | static int full_proxy_open(struct inode *inode, struct file *filp) | |
231 | { | |
69d29f9e | 232 | struct dentry *dentry = F_DENTRY(filp); |
49d200de NS |
233 | const struct file_operations *real_fops = NULL; |
234 | struct file_operations *proxy_fops = NULL; | |
69d29f9e | 235 | int r = 0; |
49d200de | 236 | |
69d29f9e NS |
237 | if (debugfs_file_get(dentry)) |
238 | return -ENOENT; | |
49d200de | 239 | |
86f0e067 | 240 | real_fops = debugfs_real_fops(filp); |
49d200de NS |
241 | real_fops = fops_get(real_fops); |
242 | if (!real_fops) { | |
243 | /* Huh? Module did not cleanup after itself at exit? */ | |
244 | WARN(1, "debugfs file owner did not clean up at exit: %pd", | |
245 | dentry); | |
246 | r = -ENXIO; | |
247 | goto out; | |
248 | } | |
249 | ||
250 | proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); | |
251 | if (!proxy_fops) { | |
252 | r = -ENOMEM; | |
253 | goto free_proxy; | |
254 | } | |
255 | __full_proxy_fops_init(proxy_fops, real_fops); | |
256 | replace_fops(filp, proxy_fops); | |
257 | ||
258 | if (real_fops->open) { | |
259 | r = real_fops->open(inode, filp); | |
b10e3e90 NS |
260 | if (r) { |
261 | replace_fops(filp, d_inode(dentry)->i_fop); | |
262 | goto free_proxy; | |
263 | } else if (filp->f_op != proxy_fops) { | |
49d200de NS |
264 | /* No protection against file removal anymore. */ |
265 | WARN(1, "debugfs file owner replaced proxy fops: %pd", | |
266 | dentry); | |
267 | goto free_proxy; | |
268 | } | |
269 | } | |
270 | ||
271 | goto out; | |
272 | free_proxy: | |
273 | kfree(proxy_fops); | |
274 | fops_put(real_fops); | |
275 | out: | |
69d29f9e | 276 | debugfs_file_put(dentry); |
49d200de NS |
277 | return r; |
278 | } | |
279 | ||
280 | const struct file_operations debugfs_full_proxy_file_operations = { | |
281 | .open = full_proxy_open, | |
282 | }; | |
283 | ||
c6468808 NS |
284 | ssize_t debugfs_attr_read(struct file *file, char __user *buf, |
285 | size_t len, loff_t *ppos) | |
286 | { | |
69d29f9e | 287 | struct dentry *dentry = F_DENTRY(file); |
c6468808 | 288 | ssize_t ret; |
c6468808 | 289 | |
69d29f9e NS |
290 | ret = debugfs_file_get(dentry); |
291 | if (unlikely(ret)) | |
292 | return ret; | |
293 | ret = simple_attr_read(file, buf, len, ppos); | |
294 | debugfs_file_put(dentry); | |
c6468808 NS |
295 | return ret; |
296 | } | |
297 | EXPORT_SYMBOL_GPL(debugfs_attr_read); | |
298 | ||
299 | ssize_t debugfs_attr_write(struct file *file, const char __user *buf, | |
300 | size_t len, loff_t *ppos) | |
301 | { | |
69d29f9e | 302 | struct dentry *dentry = F_DENTRY(file); |
c6468808 | 303 | ssize_t ret; |
c6468808 | 304 | |
69d29f9e NS |
305 | ret = debugfs_file_get(dentry); |
306 | if (unlikely(ret)) | |
307 | return ret; | |
308 | ret = simple_attr_write(file, buf, len, ppos); | |
309 | debugfs_file_put(dentry); | |
c6468808 NS |
310 | return ret; |
311 | } | |
312 | EXPORT_SYMBOL_GPL(debugfs_attr_write); | |
313 | ||
4909f168 NS |
314 | static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, |
315 | struct dentry *parent, void *value, | |
316 | const struct file_operations *fops, | |
317 | const struct file_operations *fops_ro, | |
318 | const struct file_operations *fops_wo) | |
319 | { | |
320 | /* if there are no write bits set, make read only */ | |
321 | if (!(mode & S_IWUGO)) | |
322 | return debugfs_create_file_unsafe(name, mode, parent, value, | |
323 | fops_ro); | |
324 | /* if there are no read bits set, make write only */ | |
325 | if (!(mode & S_IRUGO)) | |
326 | return debugfs_create_file_unsafe(name, mode, parent, value, | |
327 | fops_wo); | |
328 | ||
329 | return debugfs_create_file_unsafe(name, mode, parent, value, fops); | |
330 | } | |
331 | ||
8b88b099 | 332 | static int debugfs_u8_set(void *data, u64 val) |
acaefc25 AB |
333 | { |
334 | *(u8 *)data = val; | |
8b88b099 | 335 | return 0; |
acaefc25 | 336 | } |
8b88b099 | 337 | static int debugfs_u8_get(void *data, u64 *val) |
acaefc25 | 338 | { |
8b88b099 CH |
339 | *val = *(u8 *)data; |
340 | return 0; | |
acaefc25 | 341 | } |
4909f168 NS |
342 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
343 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); | |
344 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
345 | |
346 | /** | |
6468b3af | 347 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
1da177e4 LT |
348 | * @name: a pointer to a string containing the name of the file to create. |
349 | * @mode: the permission that the file should have | |
350 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 351 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
352 | * file will be created in the root of the debugfs filesystem. |
353 | * @value: a pointer to the variable that the file should read to and write | |
354 | * from. | |
355 | * | |
356 | * This function creates a file in debugfs with the given name that | |
357 | * contains the value of the variable @value. If the @mode variable is so | |
358 | * set, it can be read from, and written to. | |
359 | * | |
360 | * This function will return a pointer to a dentry if it succeeds. This | |
361 | * pointer must be passed to the debugfs_remove() function when the file is | |
362 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 363 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 364 | * |
6468b3af | 365 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 366 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 367 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
368 | * code. |
369 | */ | |
f4ae40a6 | 370 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, |
1da177e4 LT |
371 | struct dentry *parent, u8 *value) |
372 | { | |
4909f168 | 373 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, |
b97f6799 | 374 | &fops_u8_ro, &fops_u8_wo); |
1da177e4 LT |
375 | } |
376 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
377 | ||
8b88b099 | 378 | static int debugfs_u16_set(void *data, u64 val) |
acaefc25 AB |
379 | { |
380 | *(u16 *)data = val; | |
8b88b099 | 381 | return 0; |
acaefc25 | 382 | } |
8b88b099 | 383 | static int debugfs_u16_get(void *data, u64 *val) |
acaefc25 | 384 | { |
8b88b099 CH |
385 | *val = *(u16 *)data; |
386 | return 0; | |
acaefc25 | 387 | } |
4909f168 NS |
388 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
389 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); | |
390 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | |
acaefc25 | 391 | |
1da177e4 | 392 | /** |
6468b3af | 393 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
1da177e4 LT |
394 | * @name: a pointer to a string containing the name of the file to create. |
395 | * @mode: the permission that the file should have | |
396 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 397 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
398 | * file will be created in the root of the debugfs filesystem. |
399 | * @value: a pointer to the variable that the file should read to and write | |
400 | * from. | |
401 | * | |
402 | * This function creates a file in debugfs with the given name that | |
403 | * contains the value of the variable @value. If the @mode variable is so | |
404 | * set, it can be read from, and written to. | |
405 | * | |
406 | * This function will return a pointer to a dentry if it succeeds. This | |
407 | * pointer must be passed to the debugfs_remove() function when the file is | |
408 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 409 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 410 | * |
6468b3af | 411 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 412 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 413 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
414 | * code. |
415 | */ | |
f4ae40a6 | 416 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, |
1da177e4 LT |
417 | struct dentry *parent, u16 *value) |
418 | { | |
4909f168 | 419 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, |
b97f6799 | 420 | &fops_u16_ro, &fops_u16_wo); |
1da177e4 LT |
421 | } |
422 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
423 | ||
8b88b099 | 424 | static int debugfs_u32_set(void *data, u64 val) |
acaefc25 AB |
425 | { |
426 | *(u32 *)data = val; | |
8b88b099 | 427 | return 0; |
acaefc25 | 428 | } |
8b88b099 | 429 | static int debugfs_u32_get(void *data, u64 *val) |
acaefc25 | 430 | { |
8b88b099 CH |
431 | *val = *(u32 *)data; |
432 | return 0; | |
acaefc25 | 433 | } |
4909f168 NS |
434 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
435 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); | |
436 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | |
acaefc25 | 437 | |
1da177e4 | 438 | /** |
6468b3af | 439 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
1da177e4 LT |
440 | * @name: a pointer to a string containing the name of the file to create. |
441 | * @mode: the permission that the file should have | |
442 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 443 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
444 | * file will be created in the root of the debugfs filesystem. |
445 | * @value: a pointer to the variable that the file should read to and write | |
446 | * from. | |
447 | * | |
448 | * This function creates a file in debugfs with the given name that | |
449 | * contains the value of the variable @value. If the @mode variable is so | |
450 | * set, it can be read from, and written to. | |
451 | * | |
452 | * This function will return a pointer to a dentry if it succeeds. This | |
453 | * pointer must be passed to the debugfs_remove() function when the file is | |
454 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 455 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 456 | * |
6468b3af | 457 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 458 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 459 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
460 | * code. |
461 | */ | |
f4ae40a6 | 462 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, |
1da177e4 LT |
463 | struct dentry *parent, u32 *value) |
464 | { | |
4909f168 | 465 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, |
b97f6799 | 466 | &fops_u32_ro, &fops_u32_wo); |
1da177e4 LT |
467 | } |
468 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
469 | ||
8b88b099 | 470 | static int debugfs_u64_set(void *data, u64 val) |
8447891f ME |
471 | { |
472 | *(u64 *)data = val; | |
8b88b099 | 473 | return 0; |
8447891f ME |
474 | } |
475 | ||
8b88b099 | 476 | static int debugfs_u64_get(void *data, u64 *val) |
8447891f | 477 | { |
8b88b099 CH |
478 | *val = *(u64 *)data; |
479 | return 0; | |
8447891f | 480 | } |
4909f168 NS |
481 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); |
482 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); | |
483 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | |
8447891f ME |
484 | |
485 | /** | |
486 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
487 | * @name: a pointer to a string containing the name of the file to create. | |
488 | * @mode: the permission that the file should have | |
489 | * @parent: a pointer to the parent dentry for this file. This should be a | |
490 | * directory dentry if set. If this parameter is %NULL, then the | |
491 | * file will be created in the root of the debugfs filesystem. | |
492 | * @value: a pointer to the variable that the file should read to and write | |
493 | * from. | |
494 | * | |
495 | * This function creates a file in debugfs with the given name that | |
496 | * contains the value of the variable @value. If the @mode variable is so | |
497 | * set, it can be read from, and written to. | |
498 | * | |
499 | * This function will return a pointer to a dentry if it succeeds. This | |
500 | * pointer must be passed to the debugfs_remove() function when the file is | |
501 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
502 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
503 | * | |
504 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
505 | * returned. It is not wise to check for this value, but rather, check for | |
506 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
507 | * code. | |
508 | */ | |
f4ae40a6 | 509 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, |
8447891f ME |
510 | struct dentry *parent, u64 *value) |
511 | { | |
4909f168 | 512 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, |
b97f6799 | 513 | &fops_u64_ro, &fops_u64_wo); |
8447891f ME |
514 | } |
515 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | |
516 | ||
c23fe831 VK |
517 | static int debugfs_ulong_set(void *data, u64 val) |
518 | { | |
519 | *(unsigned long *)data = val; | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static int debugfs_ulong_get(void *data, u64 *val) | |
524 | { | |
525 | *val = *(unsigned long *)data; | |
526 | return 0; | |
527 | } | |
4909f168 NS |
528 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, |
529 | "%llu\n"); | |
530 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); | |
531 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); | |
c23fe831 VK |
532 | |
533 | /** | |
534 | * debugfs_create_ulong - create a debugfs file that is used to read and write | |
535 | * an unsigned long value. | |
536 | * @name: a pointer to a string containing the name of the file to create. | |
537 | * @mode: the permission that the file should have | |
538 | * @parent: a pointer to the parent dentry for this file. This should be a | |
539 | * directory dentry if set. If this parameter is %NULL, then the | |
540 | * file will be created in the root of the debugfs filesystem. | |
541 | * @value: a pointer to the variable that the file should read to and write | |
542 | * from. | |
543 | * | |
544 | * This function creates a file in debugfs with the given name that | |
545 | * contains the value of the variable @value. If the @mode variable is so | |
546 | * set, it can be read from, and written to. | |
547 | * | |
548 | * This function will return a pointer to a dentry if it succeeds. This | |
549 | * pointer must be passed to the debugfs_remove() function when the file is | |
550 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
551 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
552 | * | |
553 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
554 | * returned. It is not wise to check for this value, but rather, check for | |
555 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
556 | * code. | |
557 | */ | |
558 | struct dentry *debugfs_create_ulong(const char *name, umode_t mode, | |
559 | struct dentry *parent, unsigned long *value) | |
560 | { | |
4909f168 NS |
561 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
562 | &fops_ulong, &fops_ulong_ro, | |
563 | &fops_ulong_wo); | |
c23fe831 VK |
564 | } |
565 | EXPORT_SYMBOL_GPL(debugfs_create_ulong); | |
566 | ||
4909f168 NS |
567 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
568 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); | |
569 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); | |
2ebefc50 | 570 | |
4909f168 NS |
571 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, |
572 | "0x%04llx\n"); | |
573 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); | |
574 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); | |
2ebefc50 | 575 | |
4909f168 NS |
576 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, |
577 | "0x%08llx\n"); | |
578 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); | |
579 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); | |
2ebefc50 | 580 | |
4909f168 NS |
581 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, |
582 | "0x%016llx\n"); | |
583 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); | |
584 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); | |
15b0beaa | 585 | |
e6716b87 | 586 | /* |
15b0beaa | 587 | * 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 | 588 | * |
e6716b87 RD |
589 | * These functions are exactly the same as the above functions (but use a hex |
590 | * output for the decimal challenged). For details look at the above unsigned | |
2ebefc50 RG |
591 | * decimal functions. |
592 | */ | |
e6716b87 RD |
593 | |
594 | /** | |
595 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value | |
596 | * @name: a pointer to a string containing the name of the file to create. | |
597 | * @mode: the permission that the file should have | |
598 | * @parent: a pointer to the parent dentry for this file. This should be a | |
599 | * directory dentry if set. If this parameter is %NULL, then the | |
600 | * file will be created in the root of the debugfs filesystem. | |
601 | * @value: a pointer to the variable that the file should read to and write | |
602 | * from. | |
603 | */ | |
f4ae40a6 | 604 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, |
2ebefc50 RG |
605 | struct dentry *parent, u8 *value) |
606 | { | |
4909f168 | 607 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, |
b97f6799 | 608 | &fops_x8_ro, &fops_x8_wo); |
2ebefc50 RG |
609 | } |
610 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | |
611 | ||
e6716b87 RD |
612 | /** |
613 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | |
614 | * @name: a pointer to a string containing the name of the file to create. | |
615 | * @mode: the permission that the file should have | |
616 | * @parent: a pointer to the parent dentry for this file. This should be a | |
617 | * directory dentry if set. If this parameter is %NULL, then the | |
618 | * file will be created in the root of the debugfs filesystem. | |
619 | * @value: a pointer to the variable that the file should read to and write | |
620 | * from. | |
621 | */ | |
f4ae40a6 | 622 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, |
2ebefc50 RG |
623 | struct dentry *parent, u16 *value) |
624 | { | |
4909f168 | 625 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, |
b97f6799 | 626 | &fops_x16_ro, &fops_x16_wo); |
2ebefc50 RG |
627 | } |
628 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | |
629 | ||
e6716b87 RD |
630 | /** |
631 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | |
632 | * @name: a pointer to a string containing the name of the file to create. | |
633 | * @mode: the permission that the file should have | |
634 | * @parent: a pointer to the parent dentry for this file. This should be a | |
635 | * directory dentry if set. If this parameter is %NULL, then the | |
636 | * file will be created in the root of the debugfs filesystem. | |
637 | * @value: a pointer to the variable that the file should read to and write | |
638 | * from. | |
639 | */ | |
f4ae40a6 | 640 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, |
2ebefc50 RG |
641 | struct dentry *parent, u32 *value) |
642 | { | |
4909f168 | 643 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, |
b97f6799 | 644 | &fops_x32_ro, &fops_x32_wo); |
2ebefc50 RG |
645 | } |
646 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | |
647 | ||
15b0beaa HY |
648 | /** |
649 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
650 | * @name: a pointer to a string containing the name of the file to create. | |
651 | * @mode: the permission that the file should have | |
652 | * @parent: a pointer to the parent dentry for this file. This should be a | |
653 | * directory dentry if set. If this parameter is %NULL, then the | |
654 | * file will be created in the root of the debugfs filesystem. | |
655 | * @value: a pointer to the variable that the file should read to and write | |
656 | * from. | |
657 | */ | |
f4ae40a6 | 658 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, |
15b0beaa HY |
659 | struct dentry *parent, u64 *value) |
660 | { | |
4909f168 | 661 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, |
82b7d4fb | 662 | &fops_x64_ro, &fops_x64_wo); |
15b0beaa HY |
663 | } |
664 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | |
665 | ||
5e078787 IPG |
666 | |
667 | static int debugfs_size_t_set(void *data, u64 val) | |
668 | { | |
669 | *(size_t *)data = val; | |
670 | return 0; | |
671 | } | |
672 | static int debugfs_size_t_get(void *data, u64 *val) | |
673 | { | |
674 | *val = *(size_t *)data; | |
675 | return 0; | |
676 | } | |
4909f168 NS |
677 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, |
678 | "%llu\n"); /* %llu and %zu are more or less the same */ | |
679 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); | |
680 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); | |
5e078787 IPG |
681 | |
682 | /** | |
683 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | |
684 | * @name: a pointer to a string containing the name of the file to create. | |
685 | * @mode: the permission that the file should have | |
686 | * @parent: a pointer to the parent dentry for this file. This should be a | |
687 | * directory dentry if set. If this parameter is %NULL, then the | |
688 | * file will be created in the root of the debugfs filesystem. | |
689 | * @value: a pointer to the variable that the file should read to and write | |
690 | * from. | |
691 | */ | |
f4ae40a6 | 692 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, |
5e078787 IPG |
693 | struct dentry *parent, size_t *value) |
694 | { | |
4909f168 NS |
695 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
696 | &fops_size_t, &fops_size_t_ro, | |
697 | &fops_size_t_wo); | |
5e078787 IPG |
698 | } |
699 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | |
700 | ||
3a76e5e0 SJ |
701 | static int debugfs_atomic_t_set(void *data, u64 val) |
702 | { | |
703 | atomic_set((atomic_t *)data, val); | |
704 | return 0; | |
705 | } | |
706 | static int debugfs_atomic_t_get(void *data, u64 *val) | |
707 | { | |
708 | *val = atomic_read((atomic_t *)data); | |
709 | return 0; | |
710 | } | |
4909f168 | 711 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, |
3a76e5e0 | 712 | debugfs_atomic_t_set, "%lld\n"); |
4909f168 NS |
713 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, |
714 | "%lld\n"); | |
715 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, | |
716 | "%lld\n"); | |
3a76e5e0 SJ |
717 | |
718 | /** | |
719 | * debugfs_create_atomic_t - create a debugfs file that is used to read and | |
720 | * write an atomic_t value | |
721 | * @name: a pointer to a string containing the name of the file to create. | |
722 | * @mode: the permission that the file should have | |
723 | * @parent: a pointer to the parent dentry for this file. This should be a | |
724 | * directory dentry if set. If this parameter is %NULL, then the | |
725 | * file will be created in the root of the debugfs filesystem. | |
726 | * @value: a pointer to the variable that the file should read to and write | |
727 | * from. | |
728 | */ | |
729 | struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, | |
730 | struct dentry *parent, atomic_t *value) | |
731 | { | |
4909f168 NS |
732 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
733 | &fops_atomic_t, &fops_atomic_t_ro, | |
734 | &fops_atomic_t_wo); | |
3a76e5e0 SJ |
735 | } |
736 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); | |
5e078787 | 737 | |
0642ef6f RF |
738 | ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, |
739 | size_t count, loff_t *ppos) | |
1da177e4 LT |
740 | { |
741 | char buf[3]; | |
4d45f797 | 742 | bool val; |
69d29f9e NS |
743 | int r; |
744 | struct dentry *dentry = F_DENTRY(file); | |
4d45f797 | 745 | |
69d29f9e NS |
746 | r = debugfs_file_get(dentry); |
747 | if (unlikely(r)) | |
4d45f797 | 748 | return r; |
69d29f9e NS |
749 | val = *(bool *)file->private_data; |
750 | debugfs_file_put(dentry); | |
88e412ea | 751 | |
4d45f797 | 752 | if (val) |
1da177e4 LT |
753 | buf[0] = 'Y'; |
754 | else | |
755 | buf[0] = 'N'; | |
756 | buf[1] = '\n'; | |
757 | buf[2] = 0x00; | |
758 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
759 | } | |
0642ef6f | 760 | EXPORT_SYMBOL_GPL(debugfs_read_file_bool); |
1da177e4 | 761 | |
0642ef6f RF |
762 | ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, |
763 | size_t count, loff_t *ppos) | |
1da177e4 LT |
764 | { |
765 | char buf[32]; | |
c42d2237 | 766 | size_t buf_size; |
8705b48e | 767 | bool bv; |
69d29f9e | 768 | int r; |
621a5f7a | 769 | bool *val = file->private_data; |
69d29f9e | 770 | struct dentry *dentry = F_DENTRY(file); |
1da177e4 LT |
771 | |
772 | buf_size = min(count, (sizeof(buf)-1)); | |
773 | if (copy_from_user(buf, user_buf, buf_size)) | |
774 | return -EFAULT; | |
775 | ||
a3b2c8c7 | 776 | buf[buf_size] = '\0'; |
4d45f797 | 777 | if (strtobool(buf, &bv) == 0) { |
69d29f9e NS |
778 | r = debugfs_file_get(dentry); |
779 | if (unlikely(r)) | |
4d45f797 | 780 | return r; |
69d29f9e NS |
781 | *val = bv; |
782 | debugfs_file_put(dentry); | |
4d45f797 | 783 | } |
8705b48e | 784 | |
1da177e4 LT |
785 | return count; |
786 | } | |
0642ef6f | 787 | EXPORT_SYMBOL_GPL(debugfs_write_file_bool); |
1da177e4 | 788 | |
4b6f5d20 | 789 | static const struct file_operations fops_bool = { |
0642ef6f RF |
790 | .read = debugfs_read_file_bool, |
791 | .write = debugfs_write_file_bool, | |
234e3405 | 792 | .open = simple_open, |
6038f373 | 793 | .llseek = default_llseek, |
1da177e4 LT |
794 | }; |
795 | ||
6713e8fb SB |
796 | static const struct file_operations fops_bool_ro = { |
797 | .read = debugfs_read_file_bool, | |
798 | .open = simple_open, | |
799 | .llseek = default_llseek, | |
800 | }; | |
801 | ||
802 | static const struct file_operations fops_bool_wo = { | |
803 | .write = debugfs_write_file_bool, | |
804 | .open = simple_open, | |
805 | .llseek = default_llseek, | |
806 | }; | |
807 | ||
1da177e4 | 808 | /** |
6468b3af | 809 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
1da177e4 LT |
810 | * @name: a pointer to a string containing the name of the file to create. |
811 | * @mode: the permission that the file should have | |
812 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 813 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
814 | * file will be created in the root of the debugfs filesystem. |
815 | * @value: a pointer to the variable that the file should read to and write | |
816 | * from. | |
817 | * | |
818 | * This function creates a file in debugfs with the given name that | |
819 | * contains the value of the variable @value. If the @mode variable is so | |
820 | * set, it can be read from, and written to. | |
821 | * | |
822 | * This function will return a pointer to a dentry if it succeeds. This | |
823 | * pointer must be passed to the debugfs_remove() function when the file is | |
824 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 825 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 826 | * |
6468b3af | 827 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 828 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 829 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
830 | * code. |
831 | */ | |
f4ae40a6 | 832 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, |
621a5f7a | 833 | struct dentry *parent, bool *value) |
1da177e4 | 834 | { |
4d45f797 | 835 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, |
6713e8fb | 836 | &fops_bool_ro, &fops_bool_wo); |
1da177e4 LT |
837 | } |
838 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
839 | ||
dd308bc3 ME |
840 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
841 | size_t count, loff_t *ppos) | |
842 | { | |
843 | struct debugfs_blob_wrapper *blob = file->private_data; | |
69d29f9e | 844 | struct dentry *dentry = F_DENTRY(file); |
83b711cb | 845 | ssize_t r; |
83b711cb | 846 | |
69d29f9e NS |
847 | r = debugfs_file_get(dentry); |
848 | if (unlikely(r)) | |
849 | return r; | |
850 | r = simple_read_from_buffer(user_buf, count, ppos, blob->data, | |
851 | blob->size); | |
852 | debugfs_file_put(dentry); | |
83b711cb | 853 | return r; |
dd308bc3 ME |
854 | } |
855 | ||
00977a59 | 856 | static const struct file_operations fops_blob = { |
dd308bc3 | 857 | .read = read_file_blob, |
234e3405 | 858 | .open = simple_open, |
6038f373 | 859 | .llseek = default_llseek, |
dd308bc3 ME |
860 | }; |
861 | ||
862 | /** | |
400ced61 | 863 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
dd308bc3 ME |
864 | * @name: a pointer to a string containing the name of the file to create. |
865 | * @mode: the permission that the file should have | |
866 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 867 | * directory dentry if set. If this parameter is %NULL, then the |
dd308bc3 ME |
868 | * file will be created in the root of the debugfs filesystem. |
869 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | |
870 | * to the blob data and the size of the data. | |
871 | * | |
872 | * This function creates a file in debugfs with the given name that exports | |
873 | * @blob->data as a binary blob. If the @mode variable is so set it can be | |
874 | * read from. Writing is not supported. | |
875 | * | |
876 | * This function will return a pointer to a dentry if it succeeds. This | |
877 | * pointer must be passed to the debugfs_remove() function when the file is | |
878 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 879 | * you are responsible here.) If an error occurs, %NULL will be returned. |
dd308bc3 | 880 | * |
6468b3af | 881 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
dd308bc3 | 882 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 883 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
dd308bc3 ME |
884 | * code. |
885 | */ | |
f4ae40a6 | 886 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
dd308bc3 ME |
887 | struct dentry *parent, |
888 | struct debugfs_blob_wrapper *blob) | |
889 | { | |
83b711cb | 890 | return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob); |
dd308bc3 ME |
891 | } |
892 | EXPORT_SYMBOL_GPL(debugfs_create_blob); | |
1a087c6a | 893 | |
9fe2a701 SV |
894 | struct array_data { |
895 | void *array; | |
896 | u32 elements; | |
897 | }; | |
898 | ||
e05e279e LT |
899 | static size_t u32_format_array(char *buf, size_t bufsize, |
900 | u32 *array, int array_size) | |
9fe2a701 SV |
901 | { |
902 | size_t ret = 0; | |
9fe2a701 | 903 | |
e05e279e | 904 | while (--array_size >= 0) { |
9fe2a701 | 905 | size_t len; |
e05e279e | 906 | char term = array_size ? ' ' : '\n'; |
9fe2a701 | 907 | |
e05e279e | 908 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
9fe2a701 SV |
909 | ret += len; |
910 | ||
e05e279e LT |
911 | buf += len; |
912 | bufsize -= len; | |
9fe2a701 | 913 | } |
9fe2a701 SV |
914 | return ret; |
915 | } | |
916 | ||
36048853 | 917 | static int u32_array_open(struct inode *inode, struct file *file) |
9fe2a701 | 918 | { |
9fe2a701 | 919 | struct array_data *data = inode->i_private; |
e05e279e LT |
920 | int size, elements = data->elements; |
921 | char *buf; | |
922 | ||
923 | /* | |
924 | * Max size: | |
925 | * - 10 digits + ' '/'\n' = 11 bytes per number | |
926 | * - terminating NUL character | |
927 | */ | |
928 | size = elements*11; | |
929 | buf = kmalloc(size+1, GFP_KERNEL); | |
930 | if (!buf) | |
36048853 | 931 | return -ENOMEM; |
e05e279e LT |
932 | buf[size] = 0; |
933 | ||
934 | file->private_data = buf; | |
935 | u32_format_array(buf, size, data->array, data->elements); | |
936 | ||
36048853 DR |
937 | return nonseekable_open(inode, file); |
938 | } | |
9fe2a701 | 939 | |
36048853 DR |
940 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
941 | loff_t *ppos) | |
942 | { | |
943 | size_t size = strlen(file->private_data); | |
9fe2a701 SV |
944 | |
945 | return simple_read_from_buffer(buf, len, ppos, | |
946 | file->private_data, size); | |
947 | } | |
948 | ||
949 | static int u32_array_release(struct inode *inode, struct file *file) | |
950 | { | |
951 | kfree(file->private_data); | |
952 | ||
953 | return 0; | |
954 | } | |
955 | ||
956 | static const struct file_operations u32_array_fops = { | |
957 | .owner = THIS_MODULE, | |
958 | .open = u32_array_open, | |
959 | .release = u32_array_release, | |
960 | .read = u32_array_read, | |
961 | .llseek = no_llseek, | |
962 | }; | |
963 | ||
964 | /** | |
965 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 | |
966 | * array. | |
967 | * @name: a pointer to a string containing the name of the file to create. | |
968 | * @mode: the permission that the file should have. | |
969 | * @parent: a pointer to the parent dentry for this file. This should be a | |
970 | * directory dentry if set. If this parameter is %NULL, then the | |
971 | * file will be created in the root of the debugfs filesystem. | |
972 | * @array: u32 array that provides data. | |
973 | * @elements: total number of elements in the array. | |
974 | * | |
975 | * This function creates a file in debugfs with the given name that exports | |
976 | * @array as data. If the @mode variable is so set it can be read from. | |
977 | * Writing is not supported. Seek within the file is also not supported. | |
978 | * Once array is created its size can not be changed. | |
979 | * | |
980 | * The function returns a pointer to dentry on success. If debugfs is not | |
981 | * enabled in the kernel, the value -%ENODEV will be returned. | |
982 | */ | |
983 | struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, | |
984 | struct dentry *parent, | |
985 | u32 *array, u32 elements) | |
986 | { | |
987 | struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); | |
988 | ||
989 | if (data == NULL) | |
990 | return NULL; | |
991 | ||
992 | data->array = array; | |
993 | data->elements = elements; | |
994 | ||
c4a74f63 NS |
995 | return debugfs_create_file_unsafe(name, mode, parent, data, |
996 | &u32_array_fops); | |
9fe2a701 SV |
997 | } |
998 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); | |
999 | ||
3b85e4ab HC |
1000 | #ifdef CONFIG_HAS_IOMEM |
1001 | ||
1a087c6a AR |
1002 | /* |
1003 | * The regset32 stuff is used to print 32-bit registers using the | |
1004 | * seq_file utilities. We offer printing a register set in an already-opened | |
1005 | * sequential file or create a debugfs file that only prints a regset32. | |
1006 | */ | |
1007 | ||
1008 | /** | |
1009 | * debugfs_print_regs32 - use seq_print to describe a set of registers | |
1010 | * @s: the seq_file structure being used to generate output | |
1011 | * @regs: an array if struct debugfs_reg32 structures | |
b5763acc | 1012 | * @nregs: the length of the above array |
1a087c6a AR |
1013 | * @base: the base address to be used in reading the registers |
1014 | * @prefix: a string to be prefixed to every output line | |
1015 | * | |
1016 | * This function outputs a text block describing the current values of | |
1017 | * some 32-bit hardware registers. It is meant to be used within debugfs | |
1018 | * files based on seq_file that need to show registers, intermixed with other | |
1019 | * information. The prefix argument may be used to specify a leading string, | |
1020 | * because some peripherals have several blocks of identical registers, | |
1021 | * for example configuration of dma channels | |
1022 | */ | |
9761536e JP |
1023 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
1024 | int nregs, void __iomem *base, char *prefix) | |
1a087c6a | 1025 | { |
9761536e | 1026 | int i; |
1a087c6a AR |
1027 | |
1028 | for (i = 0; i < nregs; i++, regs++) { | |
1029 | if (prefix) | |
9761536e JP |
1030 | seq_printf(s, "%s", prefix); |
1031 | seq_printf(s, "%s = 0x%08x\n", regs->name, | |
1032 | readl(base + regs->offset)); | |
1033 | if (seq_has_overflowed(s)) | |
1034 | break; | |
1a087c6a | 1035 | } |
1a087c6a AR |
1036 | } |
1037 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); | |
1038 | ||
1039 | static int debugfs_show_regset32(struct seq_file *s, void *data) | |
1040 | { | |
1041 | struct debugfs_regset32 *regset = s->private; | |
1042 | ||
1043 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static int debugfs_open_regset32(struct inode *inode, struct file *file) | |
1048 | { | |
1049 | return single_open(file, debugfs_show_regset32, inode->i_private); | |
1050 | } | |
1051 | ||
1052 | static const struct file_operations fops_regset32 = { | |
1053 | .open = debugfs_open_regset32, | |
1054 | .read = seq_read, | |
1055 | .llseek = seq_lseek, | |
1056 | .release = single_release, | |
1057 | }; | |
1058 | ||
1059 | /** | |
1060 | * debugfs_create_regset32 - create a debugfs file that returns register values | |
1061 | * @name: a pointer to a string containing the name of the file to create. | |
1062 | * @mode: the permission that the file should have | |
1063 | * @parent: a pointer to the parent dentry for this file. This should be a | |
1064 | * directory dentry if set. If this parameter is %NULL, then the | |
1065 | * file will be created in the root of the debugfs filesystem. | |
1066 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer | |
1067 | * to an array of register definitions, the array size and the base | |
1068 | * address where the register bank is to be found. | |
1069 | * | |
1070 | * This function creates a file in debugfs with the given name that reports | |
1071 | * the names and values of a set of 32-bit registers. If the @mode variable | |
1072 | * is so set it can be read from. Writing is not supported. | |
1073 | * | |
1074 | * This function will return a pointer to a dentry if it succeeds. This | |
1075 | * pointer must be passed to the debugfs_remove() function when the file is | |
1076 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
1077 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
1078 | * | |
1079 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
1080 | * returned. It is not wise to check for this value, but rather, check for | |
1081 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
1082 | * code. | |
1083 | */ | |
88187398 | 1084 | struct dentry *debugfs_create_regset32(const char *name, umode_t mode, |
1a087c6a AR |
1085 | struct dentry *parent, |
1086 | struct debugfs_regset32 *regset) | |
1087 | { | |
1088 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); | |
1089 | } | |
1090 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); | |
3b85e4ab HC |
1091 | |
1092 | #endif /* CONFIG_HAS_IOMEM */ | |
98210b7f AS |
1093 | |
1094 | struct debugfs_devm_entry { | |
1095 | int (*read)(struct seq_file *seq, void *data); | |
1096 | struct device *dev; | |
1097 | }; | |
1098 | ||
1099 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) | |
1100 | { | |
1101 | struct debugfs_devm_entry *entry = inode->i_private; | |
1102 | ||
1103 | return single_open(f, entry->read, entry->dev); | |
1104 | } | |
1105 | ||
1106 | static const struct file_operations debugfs_devm_entry_ops = { | |
1107 | .owner = THIS_MODULE, | |
1108 | .open = debugfs_devm_entry_open, | |
1109 | .release = single_release, | |
1110 | .read = seq_read, | |
1111 | .llseek = seq_lseek | |
1112 | }; | |
1113 | ||
1114 | /** | |
1115 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. | |
1116 | * | |
1117 | * @dev: device related to this debugfs file. | |
1118 | * @name: name of the debugfs file. | |
1119 | * @parent: a pointer to the parent dentry for this file. This should be a | |
1120 | * directory dentry if set. If this parameter is %NULL, then the | |
1121 | * file will be created in the root of the debugfs filesystem. | |
1122 | * @read_fn: function pointer called to print the seq_file content. | |
1123 | */ | |
1124 | struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, | |
1125 | struct dentry *parent, | |
1126 | int (*read_fn)(struct seq_file *s, | |
1127 | void *data)) | |
1128 | { | |
1129 | struct debugfs_devm_entry *entry; | |
1130 | ||
1131 | if (IS_ERR(parent)) | |
1132 | return ERR_PTR(-ENOENT); | |
1133 | ||
1134 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); | |
1135 | if (!entry) | |
1136 | return ERR_PTR(-ENOMEM); | |
1137 | ||
1138 | entry->read = read_fn; | |
1139 | entry->dev = dev; | |
1140 | ||
1141 | return debugfs_create_file(name, S_IRUGO, parent, entry, | |
1142 | &debugfs_devm_entry_ops); | |
1143 | } | |
1144 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); | |
1145 |