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