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