Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  debugfs.h - a tiny little debug file system
0004  *
0005  *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
0006  *  Copyright (C) 2004 IBM Inc.
0007  *
0008  *  debugfs is for people to use instead of /proc or /sys.
0009  *  See Documentation/filesystems/ for more details.
0010  */
0011 
0012 #ifndef _DEBUGFS_H_
0013 #define _DEBUGFS_H_
0014 
0015 #include <linux/fs.h>
0016 #include <linux/seq_file.h>
0017 
0018 #include <linux/types.h>
0019 #include <linux/compiler.h>
0020 
0021 struct device;
0022 struct file_operations;
0023 
0024 struct debugfs_blob_wrapper {
0025     void *data;
0026     unsigned long size;
0027 };
0028 
0029 struct debugfs_reg32 {
0030     char *name;
0031     unsigned long offset;
0032 };
0033 
0034 struct debugfs_regset32 {
0035     const struct debugfs_reg32 *regs;
0036     int nregs;
0037     void __iomem *base;
0038     struct device *dev; /* Optional device for Runtime PM */
0039 };
0040 
0041 struct debugfs_u32_array {
0042     u32 *array;
0043     u32 n_elements;
0044 };
0045 
0046 extern struct dentry *arch_debugfs_dir;
0047 
0048 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt)       \
0049 static int __fops ## _open(struct inode *inode, struct file *file)  \
0050 {                                   \
0051     __simple_attr_check_format(__fmt, 0ull);            \
0052     return simple_attr_open(inode, file, __get, __set, __fmt);  \
0053 }                                   \
0054 static const struct file_operations __fops = {              \
0055     .owner   = THIS_MODULE,                     \
0056     .open    = __fops ## _open,                 \
0057     .release = simple_attr_release,                 \
0058     .read    = debugfs_attr_read,                   \
0059     .write   = debugfs_attr_write,                  \
0060     .llseek  = no_llseek,                       \
0061 }
0062 
0063 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
0064 
0065 #if defined(CONFIG_DEBUG_FS)
0066 
0067 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
0068 
0069 struct dentry *debugfs_create_file(const char *name, umode_t mode,
0070                    struct dentry *parent, void *data,
0071                    const struct file_operations *fops);
0072 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
0073                    struct dentry *parent, void *data,
0074                    const struct file_operations *fops);
0075 
0076 void debugfs_create_file_size(const char *name, umode_t mode,
0077                   struct dentry *parent, void *data,
0078                   const struct file_operations *fops,
0079                   loff_t file_size);
0080 
0081 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
0082 
0083 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
0084                       const char *dest);
0085 
0086 struct dentry *debugfs_create_automount(const char *name,
0087                     struct dentry *parent,
0088                     debugfs_automount_t f,
0089                     void *data);
0090 
0091 void debugfs_remove(struct dentry *dentry);
0092 #define debugfs_remove_recursive debugfs_remove
0093 
0094 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
0095 
0096 const struct file_operations *debugfs_real_fops(const struct file *filp);
0097 
0098 int debugfs_file_get(struct dentry *dentry);
0099 void debugfs_file_put(struct dentry *dentry);
0100 
0101 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
0102             size_t len, loff_t *ppos);
0103 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
0104             size_t len, loff_t *ppos);
0105 
0106 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
0107                 struct dentry *new_dir, const char *new_name);
0108 
0109 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
0110                u8 *value);
0111 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
0112             u16 *value);
0113 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
0114             u32 *value);
0115 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
0116             u64 *value);
0117 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
0118               unsigned long *value);
0119 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
0120                u8 *value);
0121 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
0122             u16 *value);
0123 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
0124             u32 *value);
0125 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
0126             u64 *value);
0127 void debugfs_create_size_t(const char *name, umode_t mode,
0128                struct dentry *parent, size_t *value);
0129 void debugfs_create_atomic_t(const char *name, umode_t mode,
0130                  struct dentry *parent, atomic_t *value);
0131 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
0132              bool *value);
0133 void debugfs_create_str(const char *name, umode_t mode,
0134             struct dentry *parent, char **value);
0135 
0136 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
0137                   struct dentry *parent,
0138                   struct debugfs_blob_wrapper *blob);
0139 
0140 void debugfs_create_regset32(const char *name, umode_t mode,
0141                  struct dentry *parent,
0142                  struct debugfs_regset32 *regset);
0143 
0144 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
0145               int nregs, void __iomem *base, char *prefix);
0146 
0147 void debugfs_create_u32_array(const char *name, umode_t mode,
0148                   struct dentry *parent,
0149                   struct debugfs_u32_array *array);
0150 
0151 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
0152                  struct dentry *parent,
0153                  int (*read_fn)(struct seq_file *s, void *data));
0154 
0155 bool debugfs_initialized(void);
0156 
0157 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
0158                    size_t count, loff_t *ppos);
0159 
0160 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
0161                 size_t count, loff_t *ppos);
0162 
0163 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
0164                   size_t count, loff_t *ppos);
0165 
0166 #else
0167 
0168 #include <linux/err.h>
0169 
0170 /*
0171  * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
0172  * so users have a chance to detect if there was a real error or not.  We don't
0173  * want to duplicate the design decision mistakes of procfs and devfs again.
0174  */
0175 
0176 static inline struct dentry *debugfs_lookup(const char *name,
0177                         struct dentry *parent)
0178 {
0179     return ERR_PTR(-ENODEV);
0180 }
0181 
0182 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
0183                     struct dentry *parent, void *data,
0184                     const struct file_operations *fops)
0185 {
0186     return ERR_PTR(-ENODEV);
0187 }
0188 
0189 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
0190                     umode_t mode, struct dentry *parent,
0191                     void *data,
0192                     const struct file_operations *fops)
0193 {
0194     return ERR_PTR(-ENODEV);
0195 }
0196 
0197 static inline void debugfs_create_file_size(const char *name, umode_t mode,
0198                         struct dentry *parent, void *data,
0199                         const struct file_operations *fops,
0200                         loff_t file_size)
0201 { }
0202 
0203 static inline struct dentry *debugfs_create_dir(const char *name,
0204                         struct dentry *parent)
0205 {
0206     return ERR_PTR(-ENODEV);
0207 }
0208 
0209 static inline struct dentry *debugfs_create_symlink(const char *name,
0210                             struct dentry *parent,
0211                             const char *dest)
0212 {
0213     return ERR_PTR(-ENODEV);
0214 }
0215 
0216 static inline struct dentry *debugfs_create_automount(const char *name,
0217                     struct dentry *parent,
0218                     debugfs_automount_t f,
0219                     void *data)
0220 {
0221     return ERR_PTR(-ENODEV);
0222 }
0223 
0224 static inline void debugfs_remove(struct dentry *dentry)
0225 { }
0226 
0227 static inline void debugfs_remove_recursive(struct dentry *dentry)
0228 { }
0229 
0230 static inline void debugfs_lookup_and_remove(const char *name,
0231                          struct dentry *parent)
0232 { }
0233 
0234 const struct file_operations *debugfs_real_fops(const struct file *filp);
0235 
0236 static inline int debugfs_file_get(struct dentry *dentry)
0237 {
0238     return 0;
0239 }
0240 
0241 static inline void debugfs_file_put(struct dentry *dentry)
0242 { }
0243 
0244 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
0245                     size_t len, loff_t *ppos)
0246 {
0247     return -ENODEV;
0248 }
0249 
0250 static inline ssize_t debugfs_attr_write(struct file *file,
0251                     const char __user *buf,
0252                     size_t len, loff_t *ppos)
0253 {
0254     return -ENODEV;
0255 }
0256 
0257 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
0258                 struct dentry *new_dir, char *new_name)
0259 {
0260     return ERR_PTR(-ENODEV);
0261 }
0262 
0263 static inline void debugfs_create_u8(const char *name, umode_t mode,
0264                      struct dentry *parent, u8 *value) { }
0265 
0266 static inline void debugfs_create_u16(const char *name, umode_t mode,
0267                       struct dentry *parent, u16 *value) { }
0268 
0269 static inline void debugfs_create_u32(const char *name, umode_t mode,
0270                       struct dentry *parent, u32 *value) { }
0271 
0272 static inline void debugfs_create_u64(const char *name, umode_t mode,
0273                       struct dentry *parent, u64 *value) { }
0274 
0275 static inline void debugfs_create_ulong(const char *name, umode_t mode,
0276                     struct dentry *parent,
0277                     unsigned long *value) { }
0278 
0279 static inline void debugfs_create_x8(const char *name, umode_t mode,
0280                      struct dentry *parent, u8 *value) { }
0281 
0282 static inline void debugfs_create_x16(const char *name, umode_t mode,
0283                       struct dentry *parent, u16 *value) { }
0284 
0285 static inline void debugfs_create_x32(const char *name, umode_t mode,
0286                       struct dentry *parent, u32 *value) { }
0287 
0288 static inline void debugfs_create_x64(const char *name, umode_t mode,
0289                       struct dentry *parent, u64 *value) { }
0290 
0291 static inline void debugfs_create_size_t(const char *name, umode_t mode,
0292                      struct dentry *parent, size_t *value)
0293 { }
0294 
0295 static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
0296                        struct dentry *parent,
0297                        atomic_t *value)
0298 { }
0299 
0300 static inline void debugfs_create_bool(const char *name, umode_t mode,
0301                        struct dentry *parent, bool *value) { }
0302 
0303 static inline void debugfs_create_str(const char *name, umode_t mode,
0304                       struct dentry *parent,
0305                       char **value)
0306 { }
0307 
0308 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
0309                   struct dentry *parent,
0310                   struct debugfs_blob_wrapper *blob)
0311 {
0312     return ERR_PTR(-ENODEV);
0313 }
0314 
0315 static inline void debugfs_create_regset32(const char *name, umode_t mode,
0316                        struct dentry *parent,
0317                        struct debugfs_regset32 *regset)
0318 {
0319 }
0320 
0321 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
0322              int nregs, void __iomem *base, char *prefix)
0323 {
0324 }
0325 
0326 static inline bool debugfs_initialized(void)
0327 {
0328     return false;
0329 }
0330 
0331 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
0332                         struct dentry *parent,
0333                         struct debugfs_u32_array *array)
0334 {
0335 }
0336 
0337 static inline void debugfs_create_devm_seqfile(struct device *dev,
0338                            const char *name,
0339                            struct dentry *parent,
0340                            int (*read_fn)(struct seq_file *s,
0341                                   void *data))
0342 {
0343 }
0344 
0345 static inline ssize_t debugfs_read_file_bool(struct file *file,
0346                          char __user *user_buf,
0347                          size_t count, loff_t *ppos)
0348 {
0349     return -ENODEV;
0350 }
0351 
0352 static inline ssize_t debugfs_write_file_bool(struct file *file,
0353                           const char __user *user_buf,
0354                           size_t count, loff_t *ppos)
0355 {
0356     return -ENODEV;
0357 }
0358 
0359 static inline ssize_t debugfs_read_file_str(struct file *file,
0360                         char __user *user_buf,
0361                         size_t count, loff_t *ppos)
0362 {
0363     return -ENODEV;
0364 }
0365 
0366 #endif
0367 
0368 /**
0369  * debugfs_create_xul - create a debugfs file that is used to read and write an
0370  * unsigned long value, formatted in hexadecimal
0371  * @name: a pointer to a string containing the name of the file to create.
0372  * @mode: the permission that the file should have
0373  * @parent: a pointer to the parent dentry for this file.  This should be a
0374  *          directory dentry if set.  If this parameter is %NULL, then the
0375  *          file will be created in the root of the debugfs filesystem.
0376  * @value: a pointer to the variable that the file should read to and write
0377  *         from.
0378  */
0379 static inline void debugfs_create_xul(const char *name, umode_t mode,
0380                       struct dentry *parent,
0381                       unsigned long *value)
0382 {
0383     if (sizeof(*value) == sizeof(u32))
0384         debugfs_create_x32(name, mode, parent, (u32 *)value);
0385     else
0386         debugfs_create_x64(name, mode, parent, (u64 *)value);
0387 }
0388 
0389 #endif