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 void debugfs_create_u32_array(const char *name, umode_t mode,
149 struct dentry *parent, u32 *array, u32 elements);
150
151 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
152 struct dentry *parent,
153 int (*read_fn)(struct seq_file *s,
154 void *data));
155
156 bool debugfs_initialized(void);
157
158 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
159 size_t count, loff_t *ppos);
160
161 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
162 size_t count, loff_t *ppos);
163
164 #else
165
166 #include <linux/err.h>
167
168 /*
169 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
170 * so users have a chance to detect if there was a real error or not. We don't
171 * want to duplicate the design decision mistakes of procfs and devfs again.
172 */
173
debugfs_lookup(const char * name,struct dentry * parent)174 static inline struct dentry *debugfs_lookup(const char *name,
175 struct dentry *parent)
176 {
177 return ERR_PTR(-ENODEV);
178 }
179
debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)180 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
181 struct dentry *parent, void *data,
182 const struct file_operations *fops)
183 {
184 return ERR_PTR(-ENODEV);
185 }
186
debugfs_create_file_unsafe(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)187 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
188 umode_t mode, struct dentry *parent,
189 void *data,
190 const struct file_operations *fops)
191 {
192 return ERR_PTR(-ENODEV);
193 }
194
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)195 static inline struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
196 struct dentry *parent, void *data,
197 const struct file_operations *fops,
198 loff_t file_size)
199 {
200 return ERR_PTR(-ENODEV);
201 }
202
debugfs_create_dir(const char * name,struct dentry * parent)203 static inline struct dentry *debugfs_create_dir(const char *name,
204 struct dentry *parent)
205 {
206 return ERR_PTR(-ENODEV);
207 }
208
debugfs_create_symlink(const char * name,struct dentry * parent,const char * dest)209 static inline struct dentry *debugfs_create_symlink(const char *name,
210 struct dentry *parent,
211 const char *dest)
212 {
213 return ERR_PTR(-ENODEV);
214 }
215
debugfs_create_automount(const char * name,struct dentry * parent,debugfs_automount_t f,void * data)216 static inline struct dentry *debugfs_create_automount(const char *name,
217 struct dentry *parent,
218 debugfs_automount_t f,
219 void *data)
220 {
221 return ERR_PTR(-ENODEV);
222 }
223
debugfs_remove(struct dentry * dentry)224 static inline void debugfs_remove(struct dentry *dentry)
225 { }
226
debugfs_remove_recursive(struct dentry * dentry)227 static inline void debugfs_remove_recursive(struct dentry *dentry)
228 { }
229
debugfs_lookup_and_remove(const char * name,struct dentry * parent)230 static inline void debugfs_lookup_and_remove(const char *name,
231 struct dentry *parent)
232 { }
233
234 const struct file_operations *debugfs_real_fops(const struct file *filp);
235
debugfs_file_get(struct dentry * dentry)236 static inline int debugfs_file_get(struct dentry *dentry)
237 {
238 return 0;
239 }
240
debugfs_file_put(struct dentry * dentry)241 static inline void debugfs_file_put(struct dentry *dentry)
242 { }
243
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)244 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
245 size_t len, loff_t *ppos)
246 {
247 return -ENODEV;
248 }
249
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)250 static inline ssize_t debugfs_attr_write(struct file *file,
251 const char __user *buf,
252 size_t len, loff_t *ppos)
253 {
254 return -ENODEV;
255 }
256
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)257 static inline ssize_t debugfs_attr_write_signed(struct file *file,
258 const char __user *buf,
259 size_t len, loff_t *ppos)
260 {
261 return -ENODEV;
262 }
263
debugfs_rename(struct dentry * old_dir,struct dentry * old_dentry,struct dentry * new_dir,char * new_name)264 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
265 struct dentry *new_dir, char *new_name)
266 {
267 return ERR_PTR(-ENODEV);
268 }
269
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)270 static inline struct dentry *debugfs_create_u8(const char *name, umode_t mode,
271 struct dentry *parent,
272 u8 *value)
273 {
274 return ERR_PTR(-ENODEV);
275 }
276
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)277 static inline struct dentry *debugfs_create_u16(const char *name, umode_t mode,
278 struct dentry *parent,
279 u16 *value)
280 {
281 return ERR_PTR(-ENODEV);
282 }
283
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)284 static inline struct dentry *debugfs_create_u32(const char *name, umode_t mode,
285 struct dentry *parent,
286 u32 *value)
287 {
288 return ERR_PTR(-ENODEV);
289 }
290
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)291 static inline struct dentry *debugfs_create_u64(const char *name, umode_t mode,
292 struct dentry *parent,
293 u64 *value)
294 {
295 return ERR_PTR(-ENODEV);
296 }
297
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)298 static inline struct dentry *debugfs_create_ulong(const char *name,
299 umode_t mode,
300 struct dentry *parent,
301 unsigned long *value)
302 {
303 return ERR_PTR(-ENODEV);
304 }
305
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)306 static inline struct dentry *debugfs_create_x8(const char *name, umode_t mode,
307 struct dentry *parent,
308 u8 *value)
309 {
310 return ERR_PTR(-ENODEV);
311 }
312
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)313 static inline struct dentry *debugfs_create_x16(const char *name, umode_t mode,
314 struct dentry *parent,
315 u16 *value)
316 {
317 return ERR_PTR(-ENODEV);
318 }
319
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)320 static inline struct dentry *debugfs_create_x32(const char *name, umode_t mode,
321 struct dentry *parent,
322 u32 *value)
323 {
324 return ERR_PTR(-ENODEV);
325 }
326
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)327 static inline struct dentry *debugfs_create_x64(const char *name, umode_t mode,
328 struct dentry *parent,
329 u64 *value)
330 {
331 return ERR_PTR(-ENODEV);
332 }
333
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)334 static inline struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
335 struct dentry *parent,
336 size_t *value)
337 {
338 return ERR_PTR(-ENODEV);
339 }
340
debugfs_create_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)341 static inline struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
342 struct dentry *parent, atomic_t *value)
343 {
344 return ERR_PTR(-ENODEV);
345 }
346
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)347 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
348 struct dentry *parent,
349 bool *value)
350 {
351 return ERR_PTR(-ENODEV);
352 }
353
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)354 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
355 struct dentry *parent,
356 struct debugfs_blob_wrapper *blob)
357 {
358 return ERR_PTR(-ENODEV);
359 }
360
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)361 static inline struct dentry *debugfs_create_regset32(const char *name,
362 umode_t mode, struct dentry *parent,
363 struct debugfs_regset32 *regset)
364 {
365 return ERR_PTR(-ENODEV);
366 }
367
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)368 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
369 int nregs, void __iomem *base, char *prefix)
370 {
371 }
372
debugfs_initialized(void)373 static inline bool debugfs_initialized(void)
374 {
375 return false;
376 }
377
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,u32 * array,u32 elements)378 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
379 struct dentry *parent, u32 *array,
380 u32 elements)
381 {
382 }
383
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))384 static inline struct dentry *debugfs_create_devm_seqfile(struct device *dev,
385 const char *name,
386 struct dentry *parent,
387 int (*read_fn)(struct seq_file *s,
388 void *data))
389 {
390 return ERR_PTR(-ENODEV);
391 }
392
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)393 static inline ssize_t debugfs_read_file_bool(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
396 {
397 return -ENODEV;
398 }
399
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)400 static inline ssize_t debugfs_write_file_bool(struct file *file,
401 const char __user *user_buf,
402 size_t count, loff_t *ppos)
403 {
404 return -ENODEV;
405 }
406
407 #endif
408
409 #endif
410