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