debugfs.h 12 KB

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