blob: 851dd1f9a8a5f5466933e23709c50d7c39e13d5b [file] [log] [blame]
Greg Kroah-Hartman3bce94fd2017-11-07 16:59:23 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * debugfs.h - a tiny little debug file system
4 *
5 * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
6 * Copyright (C) 2004 IBM Inc.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * debugfs is for people to use instead of /proc or /sys.
Mauro Carvalho Chehabe1b4fc72017-05-14 12:04:55 -03009 * See Documentation/filesystems/ for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#ifndef _DEBUGFS_H_
13#define _DEBUGFS_H_
14
15#include <linux/fs.h>
Alessandro Rubini1a087c62011-11-18 14:50:21 +010016#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Roland Dreiera7a76ce2005-04-18 21:57:33 -070018#include <linux/types.h>
Nicolai Stange49d200d2016-03-22 14:11:14 +010019#include <linux/compiler.h>
Roland Dreiera7a76ce2005-04-18 21:57:33 -070020
Arend van Sprielf30d0a82014-11-30 16:31:21 +010021struct device;
Roland Dreiera7a76ce2005-04-18 21:57:33 -070022struct file_operations;
23
Michael Ellermandd308bc2006-03-07 21:41:59 +110024struct debugfs_blob_wrapper {
25 void *data;
26 unsigned long size;
27};
28
Alessandro Rubini1a087c62011-11-18 14:50:21 +010029struct debugfs_reg32 {
30 char *name;
31 unsigned long offset;
32};
33
34struct debugfs_regset32 {
Felipe Balbi833d6e02013-01-18 22:40:32 +020035 const struct debugfs_reg32 *regs;
Alessandro Rubini1a087c62011-11-18 14:50:21 +010036 int nregs;
37 void __iomem *base;
Geert Uytterhoeven30332ee2020-02-11 19:18:55 +010038 struct device *dev; /* Optional device for Runtime PM */
Alessandro Rubini1a087c62011-11-18 14:50:21 +010039};
40
Jakub Kicinskia2b992c2020-07-09 17:42:44 -070041struct debugfs_u32_array {
42 u32 *array;
43 u32 n_elements;
44};
45
[email protected]ae79cda2008-07-18 16:08:13 -070046extern struct dentry *arch_debugfs_dir;
47
Arnd Bergmann7f847dd2016-10-20 22:07:53 +020048#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
49static int __fops ## _open(struct inode *inode, struct file *file) \
50{ \
51 __simple_attr_check_format(__fmt, 0ull); \
52 return simple_attr_open(inode, file, __get, __set, __fmt); \
53} \
54static const struct file_operations __fops = { \
55 .owner = THIS_MODULE, \
56 .open = __fops ## _open, \
57 .release = simple_attr_release, \
58 .read = debugfs_attr_read, \
59 .write = debugfs_attr_write, \
Geliang Tang895ce6c2017-03-11 08:44:57 +080060 .llseek = no_llseek, \
Arnd Bergmann7f847dd2016-10-20 22:07:53 +020061}
62
Kusanagi Kouichi4250b042019-11-21 19:20:21 +090063typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#if defined(CONFIG_DEBUG_FS)
Harvey Harrison36346342008-02-13 17:08:16 -080066
Omar Sandovala7c54372017-01-31 14:53:17 -080067struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
68
Al Virof4ae40a62011-07-24 04:33:43 -040069struct dentry *debugfs_create_file(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct dentry *parent, void *data,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080071 const struct file_operations *fops);
Nicolai Stangec6468802016-03-22 14:11:15 +010072struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
73 struct dentry *parent, void *data,
74 const struct file_operations *fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Greg Kroah-Hartman526ee722020-03-09 17:36:40 +010076void debugfs_create_file_size(const char *name, umode_t mode,
77 struct dentry *parent, void *data,
78 const struct file_operations *fops,
79 loff_t file_size);
David Howellse59b4e92015-01-21 20:03:40 +000080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
82
Peter Oberparleiter66f54962007-02-13 12:13:54 +010083struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
84 const char *dest);
85
Al Viro77b3da62015-01-25 15:10:32 -050086struct dentry *debugfs_create_automount(const char *name,
87 struct dentry *parent,
Eric W. Biederman93faccbb2017-02-01 06:06:16 +130088 debugfs_automount_t f,
Al Viro77b3da62015-01-25 15:10:32 -050089 void *data);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void debugfs_remove(struct dentry *dentry);
Al Viroa3d1e7e2019-11-18 09:43:10 -050092#define debugfs_remove_recursive debugfs_remove
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Nicolai Stange055ab8e2017-10-31 00:15:49 +010094const struct file_operations *debugfs_real_fops(const struct file *filp);
Nicolai Stange7c8d4692017-10-31 00:15:47 +010095
Nicolai Stangee9117a52017-10-31 00:15:48 +010096int debugfs_file_get(struct dentry *dentry);
97void debugfs_file_put(struct dentry *dentry);
98
Nicolai Stangec6468802016-03-22 14:11:15 +010099ssize_t debugfs_attr_read(struct file *file, char __user *buf,
100 size_t len, loff_t *ppos);
101ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
102 size_t len, loff_t *ppos);
103
Jan Karacfc94cd2007-05-09 13:19:52 +0200104struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
105 struct dentry *new_dir, const char *new_name);
106
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200107void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
108 u8 *value);
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200109void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
110 u16 *value);
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200111void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
112 u32 *value);
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200113void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
114 u64 *value);
Viresh Kumarc23fe832015-10-18 22:43:19 +0530115struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
116 struct dentry *parent, unsigned long *value);
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200117void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
118 u8 *value);
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200119void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
120 u16 *value);
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200121void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
122 u32 *value);
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200123void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
124 u64 *value);
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200125void debugfs_create_size_t(const char *name, umode_t mode,
126 struct dentry *parent, size_t *value);
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700127void debugfs_create_atomic_t(const char *name, umode_t mode,
128 struct dentry *parent, atomic_t *value);
Al Virof4ae40a62011-07-24 04:33:43 -0400129struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700130 struct dentry *parent, bool *value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Al Virof4ae40a62011-07-24 04:33:43 -0400132struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100133 struct dentry *parent,
134 struct debugfs_blob_wrapper *blob);
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100135
Greg Kroah-Hartmanae91c922019-11-22 11:44:53 +0100136void debugfs_create_regset32(const char *name, umode_t mode,
137 struct dentry *parent,
138 struct debugfs_regset32 *regset);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100139
Joe Perches97615362014-09-29 16:08:26 -0700140void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
141 int nregs, void __iomem *base, char *prefix);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100142
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +0200143void debugfs_create_u32_array(const char *name, umode_t mode,
Jakub Kicinskia2b992c2020-07-09 17:42:44 -0700144 struct dentry *parent,
145 struct debugfs_u32_array *array);
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530146
Arend van Spriel98210b72014-11-09 11:31:58 +0100147struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
148 struct dentry *parent,
149 int (*read_fn)(struct seq_file *s,
150 void *data));
151
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100152bool debugfs_initialized(void);
153
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100154ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
155 size_t count, loff_t *ppos);
156
157ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
158 size_t count, loff_t *ppos);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#else
Roland Dreiera7a76ce2005-04-18 21:57:33 -0700161
162#include <linux/err.h>
163
Arend van Spriel98210b72014-11-09 11:31:58 +0100164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
166 * so users have a chance to detect if there was a real error or not. We don't
167 * want to duplicate the design decision mistakes of procfs and devfs again.
168 */
169
Omar Sandovala7c54372017-01-31 14:53:17 -0800170static inline struct dentry *debugfs_lookup(const char *name,
171 struct dentry *parent)
172{
173 return ERR_PTR(-ENODEV);
174}
175
Al Virof4ae40a62011-07-24 04:33:43 -0400176static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
Jean Delvarebde11d72006-04-18 21:30:22 -0700177 struct dentry *parent, void *data,
178 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 return ERR_PTR(-ENODEV);
181}
182
Viresh Kumarc2a737e2017-06-29 09:39:02 +0530183static inline struct dentry *debugfs_create_file_unsafe(const char *name,
184 umode_t mode, struct dentry *parent,
185 void *data,
186 const struct file_operations *fops)
187{
188 return ERR_PTR(-ENODEV);
189}
190
Greg Kroah-Hartman526ee722020-03-09 17:36:40 +0100191static inline void debugfs_create_file_size(const char *name, umode_t mode,
192 struct dentry *parent, void *data,
193 const struct file_operations *fops,
194 loff_t file_size)
195{ }
David Howellse59b4e92015-01-21 20:03:40 +0000196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static inline struct dentry *debugfs_create_dir(const char *name,
198 struct dentry *parent)
199{
200 return ERR_PTR(-ENODEV);
201}
202
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100203static inline struct dentry *debugfs_create_symlink(const char *name,
204 struct dentry *parent,
205 const char *dest)
206{
207 return ERR_PTR(-ENODEV);
208}
209
Jiaxing Wang94e1dec2015-11-22 16:05:10 +0800210static inline struct dentry *debugfs_create_automount(const char *name,
211 struct dentry *parent,
Kusanagi Kouichi4250b042019-11-21 19:20:21 +0900212 debugfs_automount_t f,
Jiaxing Wang94e1dec2015-11-22 16:05:10 +0800213 void *data)
214{
215 return ERR_PTR(-ENODEV);
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static inline void debugfs_remove(struct dentry *dentry)
219{ }
220
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200221static inline void debugfs_remove_recursive(struct dentry *dentry)
222{ }
223
Arnd Bergmannf50caa92017-11-14 12:40:31 +0100224const struct file_operations *debugfs_real_fops(const struct file *filp);
225
Nicolai Stangee9117a52017-10-31 00:15:48 +0100226static inline int debugfs_file_get(struct dentry *dentry)
227{
228 return 0;
229}
230
231static inline void debugfs_file_put(struct dentry *dentry)
232{ }
233
Arnd Bergmann7f847dd2016-10-20 22:07:53 +0200234static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
235 size_t len, loff_t *ppos)
236{
237 return -ENODEV;
238}
239
240static inline ssize_t debugfs_attr_write(struct file *file,
241 const char __user *buf,
242 size_t len, loff_t *ppos)
243{
244 return -ENODEV;
245}
Nicolai Stangec6468802016-03-22 14:11:15 +0100246
Jan Karacfc94cd2007-05-09 13:19:52 +0200247static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
248 struct dentry *new_dir, char *new_name)
249{
250 return ERR_PTR(-ENODEV);
251}
252
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200253static inline void debugfs_create_u8(const char *name, umode_t mode,
254 struct dentry *parent, u8 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200256static inline void debugfs_create_u16(const char *name, umode_t mode,
257 struct dentry *parent, u16 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200259static inline void debugfs_create_u32(const char *name, umode_t mode,
260 struct dentry *parent, u32 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200262static inline void debugfs_create_u64(const char *name, umode_t mode,
263 struct dentry *parent, u64 *value) { }
Michael Ellerman84478912007-04-17 15:59:36 +1000264
Viresh Kumarc2a737e2017-06-29 09:39:02 +0530265static inline struct dentry *debugfs_create_ulong(const char *name,
266 umode_t mode,
267 struct dentry *parent,
268 unsigned long *value)
269{
270 return ERR_PTR(-ENODEV);
271}
272
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200273static inline void debugfs_create_x8(const char *name, umode_t mode,
274 struct dentry *parent, u8 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400275
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200276static inline void debugfs_create_x16(const char *name, umode_t mode,
277 struct dentry *parent, u16 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400278
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200279static inline void debugfs_create_x32(const char *name, umode_t mode,
280 struct dentry *parent, u32 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400281
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200282static inline void debugfs_create_x64(const char *name, umode_t mode,
283 struct dentry *parent, u64 *value) { }
Johannes Berg31592692013-08-26 09:58:30 +0200284
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200285static inline void debugfs_create_size_t(const char *name, umode_t mode,
286 struct dentry *parent, size_t *value)
287{ }
Inaky Perez-Gonzalez8adb7112009-01-20 12:17:28 -0800288
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700289static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
290 struct dentry *parent,
291 atomic_t *value)
292{ }
Weijie Yang5b880212013-09-27 17:09:07 +0800293
Al Virof4ae40a62011-07-24 04:33:43 -0400294static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct dentry *parent,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700296 bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 return ERR_PTR(-ENODEV);
299}
300
Al Virof4ae40a62011-07-24 04:33:43 -0400301static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100302 struct dentry *parent,
303 struct debugfs_blob_wrapper *blob)
304{
305 return ERR_PTR(-ENODEV);
306}
307
Greg Kroah-Hartmanae91c922019-11-22 11:44:53 +0100308static inline void debugfs_create_regset32(const char *name, umode_t mode,
309 struct dentry *parent,
310 struct debugfs_regset32 *regset)
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100311{
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100312}
313
Joe Perches97615362014-09-29 16:08:26 -0700314static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
Weijie Yang5b880212013-09-27 17:09:07 +0800315 int nregs, void __iomem *base, char *prefix)
316{
Weijie Yang5b880212013-09-27 17:09:07 +0800317}
318
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100319static inline bool debugfs_initialized(void)
320{
321 return false;
322}
323
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +0200324static inline void debugfs_create_u32_array(const char *name, umode_t mode,
Jakub Kicinskia2b992c2020-07-09 17:42:44 -0700325 struct dentry *parent,
326 struct debugfs_u32_array *array)
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530327{
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530328}
329
Arend van Spriel98210b72014-11-09 11:31:58 +0100330static inline struct dentry *debugfs_create_devm_seqfile(struct device *dev,
331 const char *name,
332 struct dentry *parent,
333 int (*read_fn)(struct seq_file *s,
334 void *data))
335{
336 return ERR_PTR(-ENODEV);
337}
338
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100339static inline ssize_t debugfs_read_file_bool(struct file *file,
340 char __user *user_buf,
341 size_t count, loff_t *ppos)
342{
343 return -ENODEV;
344}
345
346static inline ssize_t debugfs_write_file_bool(struct file *file,
347 const char __user *user_buf,
348 size_t count, loff_t *ppos)
349{
350 return -ENODEV;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#endif
354
Geert Uytterhoevend3504752019-10-25 11:41:24 +0200355/**
356 * debugfs_create_xul - create a debugfs file that is used to read and write an
357 * unsigned long value, formatted in hexadecimal
358 * @name: a pointer to a string containing the name of the file to create.
359 * @mode: the permission that the file should have
360 * @parent: a pointer to the parent dentry for this file. This should be a
361 * directory dentry if set. If this parameter is %NULL, then the
362 * file will be created in the root of the debugfs filesystem.
363 * @value: a pointer to the variable that the file should read to and write
364 * from.
365 */
366static inline void debugfs_create_xul(const char *name, umode_t mode,
367 struct dentry *parent,
368 unsigned long *value)
369{
370 if (sizeof(*value) == sizeof(u32))
371 debugfs_create_x32(name, mode, parent, (u32 *)value);
372 else
373 debugfs_create_x64(name, mode, parent, (u64 *)value);
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif