GNU Linux-libre 5.10.217-gnu1
[releases.git] / include / linux / debugfs.h
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 struct debugfs_u32_array {
42         u32 *array;
43         u32 n_elements;
44 };
45
46 extern struct dentry *arch_debugfs_dir;
47
48 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed)      \
49 static 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 }                                                                       \
54 static 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   = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write,      \
60         .llseek  = no_llseek,                                           \
61 }
62
63 #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
69 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
70
71 #if defined(CONFIG_DEBUG_FS)
72
73 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
74
75 struct dentry *debugfs_create_file(const char *name, umode_t mode,
76                                    struct dentry *parent, void *data,
77                                    const struct file_operations *fops);
78 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
79                                    struct dentry *parent, void *data,
80                                    const struct file_operations *fops);
81
82 void 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);
86
87 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
88
89 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
90                                       const char *dest);
91
92 struct dentry *debugfs_create_automount(const char *name,
93                                         struct dentry *parent,
94                                         debugfs_automount_t f,
95                                         void *data);
96
97 void debugfs_remove(struct dentry *dentry);
98 #define debugfs_remove_recursive debugfs_remove
99
100 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
101
102 const struct file_operations *debugfs_real_fops(const struct file *filp);
103
104 int debugfs_file_get(struct dentry *dentry);
105 void debugfs_file_put(struct dentry *dentry);
106
107 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
108                         size_t len, loff_t *ppos);
109 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
110                         size_t len, loff_t *ppos);
111 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
112                         size_t len, loff_t *ppos);
113
114 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
115                 struct dentry *new_dir, const char *new_name);
116
117 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
118                        u8 *value);
119 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
120                         u16 *value);
121 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
122                         u32 *value);
123 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
124                         u64 *value);
125 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
126                                     struct dentry *parent, unsigned long *value);
127 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
128                        u8 *value);
129 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
130                         u16 *value);
131 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
132                         u32 *value);
133 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
134                         u64 *value);
135 void debugfs_create_size_t(const char *name, umode_t mode,
136                            struct dentry *parent, size_t *value);
137 void debugfs_create_atomic_t(const char *name, umode_t mode,
138                              struct dentry *parent, atomic_t *value);
139 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
140                                   struct dentry *parent, bool *value);
141
142 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
143                                   struct dentry *parent,
144                                   struct debugfs_blob_wrapper *blob);
145
146 void debugfs_create_regset32(const char *name, umode_t mode,
147                              struct dentry *parent,
148                              struct debugfs_regset32 *regset);
149
150 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
151                           int nregs, void __iomem *base, char *prefix);
152
153 void debugfs_create_u32_array(const char *name, umode_t mode,
154                               struct dentry *parent,
155                               struct debugfs_u32_array *array);
156
157 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
158                                  struct dentry *parent,
159                                  int (*read_fn)(struct seq_file *s, void *data));
160
161 bool debugfs_initialized(void);
162
163 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
164                                size_t count, loff_t *ppos);
165
166 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
167                                 size_t count, loff_t *ppos);
168
169 #else
170
171 #include <linux/err.h>
172
173 /*
174  * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
175  * so users have a chance to detect if there was a real error or not.  We don't
176  * want to duplicate the design decision mistakes of procfs and devfs again.
177  */
178
179 static inline struct dentry *debugfs_lookup(const char *name,
180                                             struct dentry *parent)
181 {
182         return ERR_PTR(-ENODEV);
183 }
184
185 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
186                                         struct dentry *parent, void *data,
187                                         const struct file_operations *fops)
188 {
189         return ERR_PTR(-ENODEV);
190 }
191
192 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
193                                         umode_t mode, struct dentry *parent,
194                                         void *data,
195                                         const struct file_operations *fops)
196 {
197         return ERR_PTR(-ENODEV);
198 }
199
200 static inline void debugfs_create_file_size(const char *name, umode_t mode,
201                                             struct dentry *parent, void *data,
202                                             const struct file_operations *fops,
203                                             loff_t file_size)
204 { }
205
206 static inline struct dentry *debugfs_create_dir(const char *name,
207                                                 struct dentry *parent)
208 {
209         return ERR_PTR(-ENODEV);
210 }
211
212 static inline struct dentry *debugfs_create_symlink(const char *name,
213                                                     struct dentry *parent,
214                                                     const char *dest)
215 {
216         return ERR_PTR(-ENODEV);
217 }
218
219 static inline struct dentry *debugfs_create_automount(const char *name,
220                                         struct dentry *parent,
221                                         debugfs_automount_t f,
222                                         void *data)
223 {
224         return ERR_PTR(-ENODEV);
225 }
226
227 static inline void debugfs_remove(struct dentry *dentry)
228 { }
229
230 static inline void debugfs_remove_recursive(struct dentry *dentry)
231 { }
232
233 static inline void debugfs_lookup_and_remove(const char *name,
234                                              struct dentry *parent)
235 { }
236
237 const struct file_operations *debugfs_real_fops(const struct file *filp);
238
239 static inline int debugfs_file_get(struct dentry *dentry)
240 {
241         return 0;
242 }
243
244 static inline void debugfs_file_put(struct dentry *dentry)
245 { }
246
247 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
248                                         size_t len, loff_t *ppos)
249 {
250         return -ENODEV;
251 }
252
253 static inline ssize_t debugfs_attr_write(struct file *file,
254                                         const char __user *buf,
255                                         size_t len, loff_t *ppos)
256 {
257         return -ENODEV;
258 }
259
260 static inline ssize_t debugfs_attr_write_signed(struct file *file,
261                                         const char __user *buf,
262                                         size_t len, loff_t *ppos)
263 {
264         return -ENODEV;
265 }
266
267 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
268                 struct dentry *new_dir, char *new_name)
269 {
270         return ERR_PTR(-ENODEV);
271 }
272
273 static inline void debugfs_create_u8(const char *name, umode_t mode,
274                                      struct dentry *parent, u8 *value) { }
275
276 static inline void debugfs_create_u16(const char *name, umode_t mode,
277                                       struct dentry *parent, u16 *value) { }
278
279 static inline void debugfs_create_u32(const char *name, umode_t mode,
280                                       struct dentry *parent, u32 *value) { }
281
282 static inline void debugfs_create_u64(const char *name, umode_t mode,
283                                       struct dentry *parent, u64 *value) { }
284
285 static inline struct dentry *debugfs_create_ulong(const char *name,
286                                                 umode_t mode,
287                                                 struct dentry *parent,
288                                                 unsigned long *value)
289 {
290         return ERR_PTR(-ENODEV);
291 }
292
293 static inline void debugfs_create_x8(const char *name, umode_t mode,
294                                      struct dentry *parent, u8 *value) { }
295
296 static inline void debugfs_create_x16(const char *name, umode_t mode,
297                                       struct dentry *parent, u16 *value) { }
298
299 static inline void debugfs_create_x32(const char *name, umode_t mode,
300                                       struct dentry *parent, u32 *value) { }
301
302 static inline void debugfs_create_x64(const char *name, umode_t mode,
303                                       struct dentry *parent, u64 *value) { }
304
305 static inline void debugfs_create_size_t(const char *name, umode_t mode,
306                                          struct dentry *parent, size_t *value)
307 { }
308
309 static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
310                                            struct dentry *parent,
311                                            atomic_t *value)
312 { }
313
314 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
315                                                  struct dentry *parent,
316                                                  bool *value)
317 {
318         return ERR_PTR(-ENODEV);
319 }
320
321 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
322                                   struct dentry *parent,
323                                   struct debugfs_blob_wrapper *blob)
324 {
325         return ERR_PTR(-ENODEV);
326 }
327
328 static inline void debugfs_create_regset32(const char *name, umode_t mode,
329                                            struct dentry *parent,
330                                            struct debugfs_regset32 *regset)
331 {
332 }
333
334 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
335                          int nregs, void __iomem *base, char *prefix)
336 {
337 }
338
339 static inline bool debugfs_initialized(void)
340 {
341         return false;
342 }
343
344 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
345                                             struct dentry *parent,
346                                             struct debugfs_u32_array *array)
347 {
348 }
349
350 static inline void debugfs_create_devm_seqfile(struct device *dev,
351                                                const char *name,
352                                                struct dentry *parent,
353                                                int (*read_fn)(struct seq_file *s,
354                                                               void *data))
355 {
356 }
357
358 static inline ssize_t debugfs_read_file_bool(struct file *file,
359                                              char __user *user_buf,
360                                              size_t count, loff_t *ppos)
361 {
362         return -ENODEV;
363 }
364
365 static inline ssize_t debugfs_write_file_bool(struct file *file,
366                                               const char __user *user_buf,
367                                               size_t count, loff_t *ppos)
368 {
369         return -ENODEV;
370 }
371
372 #endif
373
374 /**
375  * debugfs_create_xul - create a debugfs file that is used to read and write an
376  * unsigned long value, formatted in hexadecimal
377  * @name: a pointer to a string containing the name of the file to create.
378  * @mode: the permission that the file should have
379  * @parent: a pointer to the parent dentry for this file.  This should be a
380  *          directory dentry if set.  If this parameter is %NULL, then the
381  *          file will be created in the root of the debugfs filesystem.
382  * @value: a pointer to the variable that the file should read to and write
383  *         from.
384  */
385 static inline void debugfs_create_xul(const char *name, umode_t mode,
386                                       struct dentry *parent,
387                                       unsigned long *value)
388 {
389         if (sizeof(*value) == sizeof(u32))
390                 debugfs_create_x32(name, mode, parent, (u32 *)value);
391         else
392                 debugfs_create_x64(name, mode, parent, (u64 *)value);
393 }
394
395 #endif