blob: ea2d919fd9c7990061ba4f469f92bbe5880a7b45 [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
Akinobu Mita92425442022-09-20 02:24:18 +090048#define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
Arnd Bergmann7f847dd2016-10-20 22:07:53 +020049static 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, \
Akinobu Mita92425442022-09-20 02:24:18 +090059 .write = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write, \
Geliang Tang895ce6c2017-03-11 08:44:57 +080060 .llseek = no_llseek, \
Arnd Bergmann7f847dd2016-10-20 22:07:53 +020061}
62
Akinobu Mita92425442022-09-20 02:24:18 +090063#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
64 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
65
66#define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
67 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
68
Kusanagi Kouichi4250b042019-11-21 19:20:21 +090069typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#if defined(CONFIG_DEBUG_FS)
Harvey Harrison36346342008-02-13 17:08:16 -080072
Omar Sandovala7c54372017-01-31 14:53:17 -080073struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
74
Al Virof4ae40a62011-07-24 04:33:43 -040075struct dentry *debugfs_create_file(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct dentry *parent, void *data,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080077 const struct file_operations *fops);
Nicolai Stangec6468802016-03-22 14:11:15 +010078struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
79 struct dentry *parent, void *data,
80 const struct file_operations *fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Greg Kroah-Hartman526ee722020-03-09 17:36:40 +010082void debugfs_create_file_size(const char *name, umode_t mode,
83 struct dentry *parent, void *data,
84 const struct file_operations *fops,
85 loff_t file_size);
David Howellse59b4e92015-01-21 20:03:40 +000086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
88
Peter Oberparleiter66f54962007-02-13 12:13:54 +010089struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
90 const char *dest);
91
Al Viro77b3da62015-01-25 15:10:32 -050092struct dentry *debugfs_create_automount(const char *name,
93 struct dentry *parent,
Eric W. Biederman93faccbb2017-02-01 06:06:16 +130094 debugfs_automount_t f,
Al Viro77b3da62015-01-25 15:10:32 -050095 void *data);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097void debugfs_remove(struct dentry *dentry);
Al Viroa3d1e7e2019-11-18 09:43:10 -050098#define debugfs_remove_recursive debugfs_remove
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Greg Kroah-Hartmandec9b2f2022-09-02 16:59:15 +0200100void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
101
Nicolai Stange055ab8e2017-10-31 00:15:49 +0100102const struct file_operations *debugfs_real_fops(const struct file *filp);
Nicolai Stange7c8d4692017-10-31 00:15:47 +0100103
Nicolai Stangee9117a52017-10-31 00:15:48 +0100104int debugfs_file_get(struct dentry *dentry);
105void debugfs_file_put(struct dentry *dentry);
106
Nicolai Stangec6468802016-03-22 14:11:15 +0100107ssize_t debugfs_attr_read(struct file *file, char __user *buf,
108 size_t len, loff_t *ppos);
109ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
110 size_t len, loff_t *ppos);
Akinobu Mita92425442022-09-20 02:24:18 +0900111ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
112 size_t len, loff_t *ppos);
Nicolai Stangec6468802016-03-22 14:11:15 +0100113
Jan Karacfc94cd2007-05-09 13:19:52 +0200114struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
115 struct dentry *new_dir, const char *new_name);
116
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200117void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
118 u8 *value);
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200119void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
120 u16 *value);
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200121void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
122 u32 *value);
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200123void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
124 u64 *value);
Greg Kroah-Hartmanfb05b142021-05-21 20:43:40 +0200125void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
126 unsigned long *value);
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200127void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
128 u8 *value);
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200129void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
130 u16 *value);
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200131void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
132 u32 *value);
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200133void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
134 u64 *value);
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200135void debugfs_create_size_t(const char *name, umode_t mode,
136 struct dentry *parent, size_t *value);
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700137void debugfs_create_atomic_t(const char *name, umode_t mode,
138 struct dentry *parent, atomic_t *value);
Greg Kroah-Hartman393b0632021-05-21 20:45:19 +0200139void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
140 bool *value);
Peter Zijlstra9af04402021-03-25 10:53:55 +0100141void debugfs_create_str(const char *name, umode_t mode,
142 struct dentry *parent, char **value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Al Virof4ae40a62011-07-24 04:33:43 -0400144struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100145 struct dentry *parent,
146 struct debugfs_blob_wrapper *blob);
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100147
Greg Kroah-Hartmanae91c922019-11-22 11:44:53 +0100148void debugfs_create_regset32(const char *name, umode_t mode,
149 struct dentry *parent,
150 struct debugfs_regset32 *regset);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100151
Joe Perches97615362014-09-29 16:08:26 -0700152void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
153 int nregs, void __iomem *base, char *prefix);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100154
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +0200155void debugfs_create_u32_array(const char *name, umode_t mode,
Jakub Kicinskia2b992c2020-07-09 17:42:44 -0700156 struct dentry *parent,
157 struct debugfs_u32_array *array);
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530158
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +0200159void debugfs_create_devm_seqfile(struct device *dev, const char *name,
160 struct dentry *parent,
161 int (*read_fn)(struct seq_file *s, void *data));
Arend van Spriel98210b72014-11-09 11:31:58 +0100162
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100163bool debugfs_initialized(void);
164
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100165ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
166 size_t count, loff_t *ppos);
167
168ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
169 size_t count, loff_t *ppos);
170
Peter Zijlstra9af04402021-03-25 10:53:55 +0100171ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
172 size_t count, loff_t *ppos);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
Roland Dreiera7a76ce2005-04-18 21:57:33 -0700175
176#include <linux/err.h>
177
Arend van Spriel98210b72014-11-09 11:31:58 +0100178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
180 * so users have a chance to detect if there was a real error or not. We don't
181 * want to duplicate the design decision mistakes of procfs and devfs again.
182 */
183
Omar Sandovala7c54372017-01-31 14:53:17 -0800184static inline struct dentry *debugfs_lookup(const char *name,
185 struct dentry *parent)
186{
187 return ERR_PTR(-ENODEV);
188}
189
Al Virof4ae40a62011-07-24 04:33:43 -0400190static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
Jean Delvarebde11d72006-04-18 21:30:22 -0700191 struct dentry *parent, void *data,
192 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 return ERR_PTR(-ENODEV);
195}
196
Viresh Kumarc2a737e2017-06-29 09:39:02 +0530197static inline struct dentry *debugfs_create_file_unsafe(const char *name,
198 umode_t mode, struct dentry *parent,
199 void *data,
200 const struct file_operations *fops)
201{
202 return ERR_PTR(-ENODEV);
203}
204
Greg Kroah-Hartman526ee722020-03-09 17:36:40 +0100205static inline void debugfs_create_file_size(const char *name, umode_t mode,
206 struct dentry *parent, void *data,
207 const struct file_operations *fops,
208 loff_t file_size)
209{ }
David Howellse59b4e92015-01-21 20:03:40 +0000210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static inline struct dentry *debugfs_create_dir(const char *name,
212 struct dentry *parent)
213{
214 return ERR_PTR(-ENODEV);
215}
216
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100217static inline struct dentry *debugfs_create_symlink(const char *name,
218 struct dentry *parent,
219 const char *dest)
220{
221 return ERR_PTR(-ENODEV);
222}
223
Jiaxing Wang94e1dec2015-11-22 16:05:10 +0800224static inline struct dentry *debugfs_create_automount(const char *name,
225 struct dentry *parent,
Kusanagi Kouichi4250b042019-11-21 19:20:21 +0900226 debugfs_automount_t f,
Jiaxing Wang94e1dec2015-11-22 16:05:10 +0800227 void *data)
228{
229 return ERR_PTR(-ENODEV);
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static inline void debugfs_remove(struct dentry *dentry)
233{ }
234
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200235static inline void debugfs_remove_recursive(struct dentry *dentry)
236{ }
237
Greg Kroah-Hartmandec9b2f2022-09-02 16:59:15 +0200238static inline void debugfs_lookup_and_remove(const char *name,
239 struct dentry *parent)
240{ }
241
Arnd Bergmannf50caa92017-11-14 12:40:31 +0100242const struct file_operations *debugfs_real_fops(const struct file *filp);
243
Nicolai Stangee9117a52017-10-31 00:15:48 +0100244static inline int debugfs_file_get(struct dentry *dentry)
245{
246 return 0;
247}
248
249static inline void debugfs_file_put(struct dentry *dentry)
250{ }
251
Arnd Bergmann7f847dd2016-10-20 22:07:53 +0200252static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
253 size_t len, loff_t *ppos)
254{
255 return -ENODEV;
256}
257
258static inline ssize_t debugfs_attr_write(struct file *file,
259 const char __user *buf,
260 size_t len, loff_t *ppos)
261{
262 return -ENODEV;
263}
Nicolai Stangec6468802016-03-22 14:11:15 +0100264
Akinobu Mita92425442022-09-20 02:24:18 +0900265static inline ssize_t debugfs_attr_write_signed(struct file *file,
266 const char __user *buf,
267 size_t len, loff_t *ppos)
268{
269 return -ENODEV;
270}
271
Jan Karacfc94cd2007-05-09 13:19:52 +0200272static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
273 struct dentry *new_dir, char *new_name)
274{
275 return ERR_PTR(-ENODEV);
276}
277
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200278static inline void debugfs_create_u8(const char *name, umode_t mode,
279 struct dentry *parent, u8 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200281static inline void debugfs_create_u16(const char *name, umode_t mode,
282 struct dentry *parent, u16 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200284static inline void debugfs_create_u32(const char *name, umode_t mode,
285 struct dentry *parent, u32 *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200287static inline void debugfs_create_u64(const char *name, umode_t mode,
288 struct dentry *parent, u64 *value) { }
Michael Ellerman84478912007-04-17 15:59:36 +1000289
Greg Kroah-Hartmanfb05b142021-05-21 20:43:40 +0200290static inline void debugfs_create_ulong(const char *name, umode_t mode,
291 struct dentry *parent,
292 unsigned long *value) { }
Viresh Kumarc2a737e2017-06-29 09:39:02 +0530293
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200294static inline void debugfs_create_x8(const char *name, umode_t mode,
295 struct dentry *parent, u8 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400296
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200297static inline void debugfs_create_x16(const char *name, umode_t mode,
298 struct dentry *parent, u16 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400299
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200300static inline void debugfs_create_x32(const char *name, umode_t mode,
301 struct dentry *parent, u32 *value) { }
Robin Getz2ebefc52007-08-02 18:23:50 -0400302
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200303static inline void debugfs_create_x64(const char *name, umode_t mode,
304 struct dentry *parent, u64 *value) { }
Johannes Berg31592692013-08-26 09:58:30 +0200305
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200306static inline void debugfs_create_size_t(const char *name, umode_t mode,
307 struct dentry *parent, size_t *value)
308{ }
Inaky Perez-Gonzalez8adb7112009-01-20 12:17:28 -0800309
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700310static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
311 struct dentry *parent,
312 atomic_t *value)
313{ }
Weijie Yang5b880212013-09-27 17:09:07 +0800314
Greg Kroah-Hartman393b0632021-05-21 20:45:19 +0200315static inline void debugfs_create_bool(const char *name, umode_t mode,
316 struct dentry *parent, bool *value) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Peter Zijlstra9af04402021-03-25 10:53:55 +0100318static inline void debugfs_create_str(const char *name, umode_t mode,
319 struct dentry *parent,
320 char **value)
321{ }
322
Al Virof4ae40a62011-07-24 04:33:43 -0400323static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100324 struct dentry *parent,
325 struct debugfs_blob_wrapper *blob)
326{
327 return ERR_PTR(-ENODEV);
328}
329
Greg Kroah-Hartmanae91c922019-11-22 11:44:53 +0100330static inline void debugfs_create_regset32(const char *name, umode_t mode,
331 struct dentry *parent,
332 struct debugfs_regset32 *regset)
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100333{
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100334}
335
Joe Perches97615362014-09-29 16:08:26 -0700336static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
Weijie Yang5b880212013-09-27 17:09:07 +0800337 int nregs, void __iomem *base, char *prefix)
338{
Weijie Yang5b880212013-09-27 17:09:07 +0800339}
340
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100341static inline bool debugfs_initialized(void)
342{
343 return false;
344}
345
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +0200346static inline void debugfs_create_u32_array(const char *name, umode_t mode,
Jakub Kicinskia2b992c2020-07-09 17:42:44 -0700347 struct dentry *parent,
348 struct debugfs_u32_array *array)
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530349{
Srivatsa Vaddagiri9fe2a7012012-03-23 13:36:28 +0530350}
351
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +0200352static inline void debugfs_create_devm_seqfile(struct device *dev,
353 const char *name,
354 struct dentry *parent,
355 int (*read_fn)(struct seq_file *s,
356 void *data))
Arend van Spriel98210b72014-11-09 11:31:58 +0100357{
Arend van Spriel98210b72014-11-09 11:31:58 +0100358}
359
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100360static inline ssize_t debugfs_read_file_bool(struct file *file,
361 char __user *user_buf,
362 size_t count, loff_t *ppos)
363{
364 return -ENODEV;
365}
366
367static inline ssize_t debugfs_write_file_bool(struct file *file,
368 const char __user *user_buf,
369 size_t count, loff_t *ppos)
370{
371 return -ENODEV;
372}
373
Peter Zijlstra9af04402021-03-25 10:53:55 +0100374static inline ssize_t debugfs_read_file_str(struct file *file,
375 char __user *user_buf,
376 size_t count, loff_t *ppos)
377{
378 return -ENODEV;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
382
Geert Uytterhoevend3504752019-10-25 11:41:24 +0200383/**
384 * debugfs_create_xul - create a debugfs file that is used to read and write an
385 * unsigned long value, formatted in hexadecimal
386 * @name: a pointer to a string containing the name of the file to create.
387 * @mode: the permission that the file should have
388 * @parent: a pointer to the parent dentry for this file. This should be a
389 * directory dentry if set. If this parameter is %NULL, then the
390 * file will be created in the root of the debugfs filesystem.
391 * @value: a pointer to the variable that the file should read to and write
392 * from.
393 */
394static inline void debugfs_create_xul(const char *name, umode_t mode,
395 struct dentry *parent,
396 unsigned long *value)
397{
398 if (sizeof(*value) == sizeof(u32))
399 debugfs_create_x32(name, mode, parent, (u32 *)value);
400 else
401 debugfs_create_x64(name, mode, parent, (u64 *)value);
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif