debugfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #ifndef _DEBUGFS_H_
  12. #define _DEBUGFS_H_
  13. #include <linux/fs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/types.h>
  16. #include <linux/compiler.h>
  17. struct device;
  18. struct file_operations;
  19. struct debugfs_blob_wrapper {
  20. void *data;
  21. unsigned long size;
  22. };
  23. struct debugfs_reg32 {
  24. char *name;
  25. unsigned long offset;
  26. };
  27. struct debugfs_regset32 {
  28. const struct debugfs_reg32 *regs;
  29. int nregs;
  30. void __iomem *base;
  31. struct device *dev; /* Optional device for Runtime PM */
  32. };
  33. struct debugfs_u32_array {
  34. u32 *array;
  35. u32 n_elements;
  36. };
  37. extern struct dentry *arch_debugfs_dir;
  38. #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
  39. static int __fops ## _open(struct inode *inode, struct file *file) \
  40. { \
  41. __simple_attr_check_format(__fmt, 0ull); \
  42. return simple_attr_open(inode, file, __get, __set, __fmt); \
  43. } \
  44. static const struct file_operations __fops = { \
  45. .owner = THIS_MODULE, \
  46. .open = __fops ## _open, \
  47. .release = simple_attr_release, \
  48. .read = debugfs_attr_read, \
  49. .write = debugfs_attr_write, \
  50. .llseek = no_llseek, \
  51. }
  52. typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
  53. #if defined(CONFIG_DEBUG_FS)
  54. struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
  55. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  56. struct dentry *parent, void *data,
  57. const struct file_operations *fops);
  58. struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
  59. struct dentry *parent, void *data,
  60. const struct file_operations *fops);
  61. void debugfs_create_file_size(const char *name, umode_t mode,
  62. struct dentry *parent, void *data,
  63. const struct file_operations *fops,
  64. loff_t file_size);
  65. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
  66. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  67. const char *dest);
  68. struct dentry *debugfs_create_automount(const char *name,
  69. struct dentry *parent,
  70. debugfs_automount_t f,
  71. void *data);
  72. void debugfs_remove(struct dentry *dentry);
  73. #define debugfs_remove_recursive debugfs_remove
  74. const struct file_operations *debugfs_real_fops(const struct file *filp);
  75. int debugfs_file_get(struct dentry *dentry);
  76. void debugfs_file_put(struct dentry *dentry);
  77. ssize_t debugfs_attr_read(struct file *file, char __user *buf,
  78. size_t len, loff_t *ppos);
  79. ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
  80. size_t len, loff_t *ppos);
  81. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  82. struct dentry *new_dir, const char *new_name);
  83. void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
  84. u8 *value);
  85. void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
  86. u16 *value);
  87. void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
  88. u32 *value);
  89. void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
  90. u64 *value);
  91. struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
  92. struct dentry *parent, unsigned long *value);
  93. void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
  94. u8 *value);
  95. void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
  96. u16 *value);
  97. void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
  98. u32 *value);
  99. void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
  100. u64 *value);
  101. void debugfs_create_size_t(const char *name, umode_t mode,
  102. struct dentry *parent, size_t *value);
  103. void debugfs_create_atomic_t(const char *name, umode_t mode,
  104. struct dentry *parent, atomic_t *value);
  105. struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  106. struct dentry *parent, bool *value);
  107. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  108. struct dentry *parent,
  109. struct debugfs_blob_wrapper *blob);
  110. void debugfs_create_regset32(const char *name, umode_t mode,
  111. struct dentry *parent,
  112. struct debugfs_regset32 *regset);
  113. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  114. int nregs, void __iomem *base, char *prefix);
  115. void debugfs_create_u32_array(const char *name, umode_t mode,
  116. struct dentry *parent,
  117. struct debugfs_u32_array *array);
  118. void debugfs_create_devm_seqfile(struct device *dev, const char *name,
  119. struct dentry *parent,
  120. int (*read_fn)(struct seq_file *s, void *data));
  121. bool debugfs_initialized(void);
  122. ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
  123. size_t count, loff_t *ppos);
  124. ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  125. size_t count, loff_t *ppos);
  126. #else
  127. #include <linux/err.h>
  128. /*
  129. * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
  130. * so users have a chance to detect if there was a real error or not. We don't
  131. * want to duplicate the design decision mistakes of procfs and devfs again.
  132. */
  133. static inline struct dentry *debugfs_lookup(const char *name,
  134. struct dentry *parent)
  135. {
  136. return ERR_PTR(-ENODEV);
  137. }
  138. static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
  139. struct dentry *parent, void *data,
  140. const struct file_operations *fops)
  141. {
  142. return ERR_PTR(-ENODEV);
  143. }
  144. static inline struct dentry *debugfs_create_file_unsafe(const char *name,
  145. umode_t mode, struct dentry *parent,
  146. void *data,
  147. const struct file_operations *fops)
  148. {
  149. return ERR_PTR(-ENODEV);
  150. }
  151. static inline void debugfs_create_file_size(const char *name, umode_t mode,
  152. struct dentry *parent, void *data,
  153. const struct file_operations *fops,
  154. loff_t file_size)
  155. { }
  156. static inline struct dentry *debugfs_create_dir(const char *name,
  157. struct dentry *parent)
  158. {
  159. return ERR_PTR(-ENODEV);
  160. }
  161. static inline struct dentry *debugfs_create_symlink(const char *name,
  162. struct dentry *parent,
  163. const char *dest)
  164. {
  165. return ERR_PTR(-ENODEV);
  166. }
  167. static inline struct dentry *debugfs_create_automount(const char *name,
  168. struct dentry *parent,
  169. debugfs_automount_t f,
  170. void *data)
  171. {
  172. return ERR_PTR(-ENODEV);
  173. }
  174. static inline void debugfs_remove(struct dentry *dentry)
  175. { }
  176. static inline void debugfs_remove_recursive(struct dentry *dentry)
  177. { }
  178. const struct file_operations *debugfs_real_fops(const struct file *filp);
  179. static inline int debugfs_file_get(struct dentry *dentry)
  180. {
  181. return 0;
  182. }
  183. static inline void debugfs_file_put(struct dentry *dentry)
  184. { }
  185. static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
  186. size_t len, loff_t *ppos)
  187. {
  188. return -ENODEV;
  189. }
  190. static inline ssize_t debugfs_attr_write(struct file *file,
  191. const char __user *buf,
  192. size_t len, loff_t *ppos)
  193. {
  194. return -ENODEV;
  195. }
  196. static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  197. struct dentry *new_dir, char *new_name)
  198. {
  199. return ERR_PTR(-ENODEV);
  200. }
  201. static inline void debugfs_create_u8(const char *name, umode_t mode,
  202. struct dentry *parent, u8 *value) { }
  203. static inline void debugfs_create_u16(const char *name, umode_t mode,
  204. struct dentry *parent, u16 *value) { }
  205. static inline void debugfs_create_u32(const char *name, umode_t mode,
  206. struct dentry *parent, u32 *value) { }
  207. static inline void debugfs_create_u64(const char *name, umode_t mode,
  208. struct dentry *parent, u64 *value) { }
  209. static inline struct dentry *debugfs_create_ulong(const char *name,
  210. umode_t mode,
  211. struct dentry *parent,
  212. unsigned long *value)
  213. {
  214. return ERR_PTR(-ENODEV);
  215. }
  216. static inline void debugfs_create_x8(const char *name, umode_t mode,
  217. struct dentry *parent, u8 *value) { }
  218. static inline void debugfs_create_x16(const char *name, umode_t mode,
  219. struct dentry *parent, u16 *value) { }
  220. static inline void debugfs_create_x32(const char *name, umode_t mode,
  221. struct dentry *parent, u32 *value) { }
  222. static inline void debugfs_create_x64(const char *name, umode_t mode,
  223. struct dentry *parent, u64 *value) { }
  224. static inline void debugfs_create_size_t(const char *name, umode_t mode,
  225. struct dentry *parent, size_t *value)
  226. { }
  227. static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
  228. struct dentry *parent,
  229. atomic_t *value)
  230. { }
  231. static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  232. struct dentry *parent,
  233. bool *value)
  234. {
  235. return ERR_PTR(-ENODEV);
  236. }
  237. static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  238. struct dentry *parent,
  239. struct debugfs_blob_wrapper *blob)
  240. {
  241. return ERR_PTR(-ENODEV);
  242. }
  243. static inline void debugfs_create_regset32(const char *name, umode_t mode,
  244. struct dentry *parent,
  245. struct debugfs_regset32 *regset)
  246. {
  247. }
  248. static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  249. int nregs, void __iomem *base, char *prefix)
  250. {
  251. }
  252. static inline bool debugfs_initialized(void)
  253. {
  254. return false;
  255. }
  256. static inline void debugfs_create_u32_array(const char *name, umode_t mode,
  257. struct dentry *parent,
  258. struct debugfs_u32_array *array)
  259. {
  260. }
  261. static inline void debugfs_create_devm_seqfile(struct device *dev,
  262. const char *name,
  263. struct dentry *parent,
  264. int (*read_fn)(struct seq_file *s,
  265. void *data))
  266. {
  267. }
  268. static inline ssize_t debugfs_read_file_bool(struct file *file,
  269. char __user *user_buf,
  270. size_t count, loff_t *ppos)
  271. {
  272. return -ENODEV;
  273. }
  274. static inline ssize_t debugfs_write_file_bool(struct file *file,
  275. const char __user *user_buf,
  276. size_t count, loff_t *ppos)
  277. {
  278. return -ENODEV;
  279. }
  280. #endif
  281. /**
  282. * debugfs_create_xul - create a debugfs file that is used to read and write an
  283. * unsigned long value, formatted in hexadecimal
  284. * @name: a pointer to a string containing the name of the file to create.
  285. * @mode: the permission that the file should have
  286. * @parent: a pointer to the parent dentry for this file. This should be a
  287. * directory dentry if set. If this parameter is %NULL, then the
  288. * file will be created in the root of the debugfs filesystem.
  289. * @value: a pointer to the variable that the file should read to and write
  290. * from.
  291. */
  292. static inline void debugfs_create_xul(const char *name, umode_t mode,
  293. struct dentry *parent,
  294. unsigned long *value)
  295. {
  296. if (sizeof(*value) == sizeof(u32))
  297. debugfs_create_x32(name, mode, parent, (u32 *)value);
  298. else
  299. debugfs_create_x64(name, mode, parent, (u64 *)value);
  300. }
  301. #endif