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