1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * debugfs.h - 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 #ifndef _DEBUGFS_H_
13 #define _DEBUGFS_H_
14
15 #include <linux/fs.h>
16 #include <linux/seq_file.h>
17
18 #include <linux/types.h>
19 #include <linux/compiler.h>
20
21 struct device;
22 struct file_operations;
23
24 struct debugfs_blob_wrapper {
25 void *data;
26 unsigned long size;
27 };
28
29 struct debugfs_reg32 {
30 char *name;
31 unsigned long offset;
32 };
33
34 struct debugfs_regset32 {
35 const struct debugfs_reg32 *regs;
36 int nregs;
37 void __iomem *base;
38 struct device *dev; /* Optional device for Runtime PM */
39 };
40
41 extern struct dentry *arch_debugfs_dir;
42
43 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
44 static int __fops ## _open(struct inode *inode, struct file *file) \
45 { \
46 __simple_attr_check_format(__fmt, 0ull); \
47 return simple_attr_open(inode, file, __get, __set, __fmt); \
48 } \
49 static const struct file_operations __fops = { \
50 .owner = THIS_MODULE, \
51 .open = __fops ## _open, \
52 .release = simple_attr_release, \
53 .read = debugfs_attr_read, \
54 .write = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write, \
55 .llseek = no_llseek, \
56 }
57
58 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
59 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
60
61 #define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
62 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
63
64 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
65
66 #if defined(CONFIG_DEBUG_FS)
67
68 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
69
70 struct dentry *debugfs_create_file(const char *name, umode_t mode,
71 struct dentry *parent, void *data,
72 const struct file_operations *fops);
73 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
74 struct dentry *parent, void *data,
75 const struct file_operations *fops);
76
77 struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
78 struct dentry *parent, void *data,
79 const struct file_operations *fops,
80 loff_t file_size);
81
82 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
83
84 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
85 const char *dest);
86
87 struct dentry *debugfs_create_automount(const char *name,
88 struct dentry *parent,
89 debugfs_automount_t f,
90 void *data);
91
92 void debugfs_remove(struct dentry *dentry);
93 void debugfs_remove_recursive(struct dentry *dentry);
94
95 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
96
97 const struct file_operations *debugfs_real_fops(const struct file *filp);
98
99 int debugfs_file_get(struct dentry *dentry);
100 void debugfs_file_put(struct dentry *dentry);
101
102 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
103 size_t len, loff_t *ppos);
104 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
105 size_t len, loff_t *ppos);
106 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
107 size_t len, loff_t *ppos);
108
109 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
110 struct dentry *new_dir, const char *new_name);
111
112 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
113 struct dentry *parent, u8 *value);
114 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
115 struct dentry *parent, u16 *value);
116 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
117 struct dentry *parent, u32 *value);
118 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
119 struct dentry *parent, u64 *value);
120 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
121 struct dentry *parent, unsigned long *value);
122 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
123 struct dentry *parent, u8 *value);
124 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
125 struct dentry *parent, u16 *value);
126 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
127 struct dentry *parent, u32 *value);
128 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
129 struct dentry *parent, u64 *value);
130 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
131 struct dentry *parent, size_t *value);
132 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
133 struct dentry *parent, atomic_t *value);
134 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
135 struct dentry *parent, bool *value);
136
137 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
138 struct dentry *parent,
139 struct debugfs_blob_wrapper *blob);
140
141 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
142 struct dentry *parent,
143 struct debugfs_regset32 *regset);
144
145 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
146 int nregs, void __iomem *base, char *prefix);
147
148 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
149 struct dentry *parent,
150 u32 *array, u32 elements);
151
152 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
153 struct dentry *parent,
154 int (*read_fn)(struct seq_file *s,
155 void *data));
156
157 bool debugfs_initialized(void);
158
159 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
160 size_t count, loff_t *ppos);
161
162 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
163 size_t count, loff_t *ppos);
164
165 #else
166
167 #include <linux/err.h>
168
169 /*
170 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
171 * so users have a chance to detect if there was a real error or not. We don't
172 * want to duplicate the design decision mistakes of procfs and devfs again.
173 */
174
debugfs_lookup(const char * name,struct dentry * parent)175 static inline struct dentry *debugfs_lookup(const char *name,
176 struct dentry *parent)
177 {
178 return ERR_PTR(-ENODEV);
179 }
180
debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)181 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
182 struct dentry *parent, void *data,
183 const struct file_operations *fops)
184 {
185 return ERR_PTR(-ENODEV);
186 }
187
debugfs_create_file_unsafe(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)188 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
189 umode_t mode, struct dentry *parent,
190 void *data,
191 const struct file_operations *fops)
192 {
193 return ERR_PTR(-ENODEV);
194 }
195
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)196 static inline struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
197 struct dentry *parent, void *data,
198 const struct file_operations *fops,
199 loff_t file_size)
200 {
201 return ERR_PTR(-ENODEV);
202 }
203
debugfs_create_dir(const char * name,struct dentry * parent)204 static inline struct dentry *debugfs_create_dir(const char *name,
205 struct dentry *parent)
206 {
207 return ERR_PTR(-ENODEV);
208 }
209
debugfs_create_symlink(const char * name,struct dentry * parent,const char * dest)210 static inline struct dentry *debugfs_create_symlink(const char *name,
211 struct dentry *parent,
212 const char *dest)
213 {
214 return ERR_PTR(-ENODEV);
215 }
216
debugfs_create_automount(const char * name,struct dentry * parent,debugfs_automount_t f,void * data)217 static inline struct dentry *debugfs_create_automount(const char *name,
218 struct dentry *parent,
219 debugfs_automount_t f,
220 void *data)
221 {
222 return ERR_PTR(-ENODEV);
223 }
224
debugfs_remove(struct dentry * dentry)225 static inline void debugfs_remove(struct dentry *dentry)
226 { }
227
debugfs_remove_recursive(struct dentry * dentry)228 static inline void debugfs_remove_recursive(struct dentry *dentry)
229 { }
230
debugfs_lookup_and_remove(const char * name,struct dentry * parent)231 static inline void debugfs_lookup_and_remove(const char *name,
232 struct dentry *parent)
233 { }
234
235 const struct file_operations *debugfs_real_fops(const struct file *filp);
236
debugfs_file_get(struct dentry * dentry)237 static inline int debugfs_file_get(struct dentry *dentry)
238 {
239 return 0;
240 }
241
debugfs_file_put(struct dentry * dentry)242 static inline void debugfs_file_put(struct dentry *dentry)
243 { }
244
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)245 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
246 size_t len, loff_t *ppos)
247 {
248 return -ENODEV;
249 }
250
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)251 static inline ssize_t debugfs_attr_write(struct file *file,
252 const char __user *buf,
253 size_t len, loff_t *ppos)
254 {
255 return -ENODEV;
256 }
257
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)258 static inline ssize_t debugfs_attr_write_signed(struct file *file,
259 const char __user *buf,
260 size_t len, loff_t *ppos)
261 {
262 return -ENODEV;
263 }
264
debugfs_rename(struct dentry * old_dir,struct dentry * old_dentry,struct dentry * new_dir,char * new_name)265 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
266 struct dentry *new_dir, char *new_name)
267 {
268 return ERR_PTR(-ENODEV);
269 }
270
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)271 static inline struct dentry *debugfs_create_u8(const char *name, umode_t mode,
272 struct dentry *parent,
273 u8 *value)
274 {
275 return ERR_PTR(-ENODEV);
276 }
277
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)278 static inline struct dentry *debugfs_create_u16(const char *name, umode_t mode,
279 struct dentry *parent,
280 u16 *value)
281 {
282 return ERR_PTR(-ENODEV);
283 }
284
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)285 static inline struct dentry *debugfs_create_u32(const char *name, umode_t mode,
286 struct dentry *parent,
287 u32 *value)
288 {
289 return ERR_PTR(-ENODEV);
290 }
291
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)292 static inline struct dentry *debugfs_create_u64(const char *name, umode_t mode,
293 struct dentry *parent,
294 u64 *value)
295 {
296 return ERR_PTR(-ENODEV);
297 }
298
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)299 static inline struct dentry *debugfs_create_ulong(const char *name,
300 umode_t mode,
301 struct dentry *parent,
302 unsigned long *value)
303 {
304 return ERR_PTR(-ENODEV);
305 }
306
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)307 static inline struct dentry *debugfs_create_x8(const char *name, umode_t mode,
308 struct dentry *parent,
309 u8 *value)
310 {
311 return ERR_PTR(-ENODEV);
312 }
313
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)314 static inline struct dentry *debugfs_create_x16(const char *name, umode_t mode,
315 struct dentry *parent,
316 u16 *value)
317 {
318 return ERR_PTR(-ENODEV);
319 }
320
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)321 static inline struct dentry *debugfs_create_x32(const char *name, umode_t mode,
322 struct dentry *parent,
323 u32 *value)
324 {
325 return ERR_PTR(-ENODEV);
326 }
327
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)328 static inline struct dentry *debugfs_create_x64(const char *name, umode_t mode,
329 struct dentry *parent,
330 u64 *value)
331 {
332 return ERR_PTR(-ENODEV);
333 }
334
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)335 static inline struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
336 struct dentry *parent,
337 size_t *value)
338 {
339 return ERR_PTR(-ENODEV);
340 }
341
debugfs_create_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)342 static inline struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
343 struct dentry *parent, atomic_t *value)
344 {
345 return ERR_PTR(-ENODEV);
346 }
347
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)348 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
349 struct dentry *parent,
350 bool *value)
351 {
352 return ERR_PTR(-ENODEV);
353 }
354
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)355 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
356 struct dentry *parent,
357 struct debugfs_blob_wrapper *blob)
358 {
359 return ERR_PTR(-ENODEV);
360 }
361
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)362 static inline struct dentry *debugfs_create_regset32(const char *name,
363 umode_t mode, struct dentry *parent,
364 struct debugfs_regset32 *regset)
365 {
366 return ERR_PTR(-ENODEV);
367 }
368
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)369 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
370 int nregs, void __iomem *base, char *prefix)
371 {
372 }
373
debugfs_initialized(void)374 static inline bool debugfs_initialized(void)
375 {
376 return false;
377 }
378
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,u32 * array,u32 elements)379 static inline struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
380 struct dentry *parent,
381 u32 *array, u32 elements)
382 {
383 return ERR_PTR(-ENODEV);
384 }
385
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))386 static inline struct dentry *debugfs_create_devm_seqfile(struct device *dev,
387 const char *name,
388 struct dentry *parent,
389 int (*read_fn)(struct seq_file *s,
390 void *data))
391 {
392 return ERR_PTR(-ENODEV);
393 }
394
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)395 static inline ssize_t debugfs_read_file_bool(struct file *file,
396 char __user *user_buf,
397 size_t count, loff_t *ppos)
398 {
399 return -ENODEV;
400 }
401
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)402 static inline ssize_t debugfs_write_file_bool(struct file *file,
403 const char __user *user_buf,
404 size_t count, loff_t *ppos)
405 {
406 return -ENODEV;
407 }
408
409 #endif
410
411 #endif
412