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