file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * file.c - part of debugfs, a tiny little debug file system
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 2004 IBM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * debugfs is for people to use instead of /proc or /sys.
  12. * See Documentation/DocBook/kernel-api for more details.
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/debugfs.h>
  20. static ssize_t default_read_file(struct file *file, char __user *buf,
  21. size_t count, loff_t *ppos)
  22. {
  23. return 0;
  24. }
  25. static ssize_t default_write_file(struct file *file, const char __user *buf,
  26. size_t count, loff_t *ppos)
  27. {
  28. return count;
  29. }
  30. static int default_open(struct inode *inode, struct file *file)
  31. {
  32. if (inode->u.generic_ip)
  33. file->private_data = inode->u.generic_ip;
  34. return 0;
  35. }
  36. const struct file_operations debugfs_file_operations = {
  37. .read = default_read_file,
  38. .write = default_write_file,
  39. .open = default_open,
  40. };
  41. static void debugfs_u8_set(void *data, u64 val)
  42. {
  43. *(u8 *)data = val;
  44. }
  45. static u64 debugfs_u8_get(void *data)
  46. {
  47. return *(u8 *)data;
  48. }
  49. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  50. /**
  51. * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write an unsigned 8 bit value.
  52. *
  53. * @name: a pointer to a string containing the name of the file to create.
  54. * @mode: the permission that the file should have
  55. * @parent: a pointer to the parent dentry for this file. This should be a
  56. * directory dentry if set. If this paramater is NULL, then the
  57. * file will be created in the root of the debugfs filesystem.
  58. * @value: a pointer to the variable that the file should read to and write
  59. * from.
  60. *
  61. * This function creates a file in debugfs with the given name that
  62. * contains the value of the variable @value. If the @mode variable is so
  63. * set, it can be read from, and written to.
  64. *
  65. * This function will return a pointer to a dentry if it succeeds. This
  66. * pointer must be passed to the debugfs_remove() function when the file is
  67. * to be removed (no automatic cleanup happens if your module is unloaded,
  68. * you are responsible here.) If an error occurs, NULL will be returned.
  69. *
  70. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  71. * returned. It is not wise to check for this value, but rather, check for
  72. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  73. * code.
  74. */
  75. struct dentry *debugfs_create_u8(const char *name, mode_t mode,
  76. struct dentry *parent, u8 *value)
  77. {
  78. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  79. }
  80. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  81. static void debugfs_u16_set(void *data, u64 val)
  82. {
  83. *(u16 *)data = val;
  84. }
  85. static u64 debugfs_u16_get(void *data)
  86. {
  87. return *(u16 *)data;
  88. }
  89. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  90. /**
  91. * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write an unsigned 16 bit value.
  92. *
  93. * @name: a pointer to a string containing the name of the file to create.
  94. * @mode: the permission that the file should have
  95. * @parent: a pointer to the parent dentry for this file. This should be a
  96. * directory dentry if set. If this paramater is NULL, then the
  97. * file will be created in the root of the debugfs filesystem.
  98. * @value: a pointer to the variable that the file should read to and write
  99. * from.
  100. *
  101. * This function creates a file in debugfs with the given name that
  102. * contains the value of the variable @value. If the @mode variable is so
  103. * set, it can be read from, and written to.
  104. *
  105. * This function will return a pointer to a dentry if it succeeds. This
  106. * pointer must be passed to the debugfs_remove() function when the file is
  107. * to be removed (no automatic cleanup happens if your module is unloaded,
  108. * you are responsible here.) If an error occurs, NULL will be returned.
  109. *
  110. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  111. * returned. It is not wise to check for this value, but rather, check for
  112. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  113. * code.
  114. */
  115. struct dentry *debugfs_create_u16(const char *name, mode_t mode,
  116. struct dentry *parent, u16 *value)
  117. {
  118. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  119. }
  120. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  121. static void debugfs_u32_set(void *data, u64 val)
  122. {
  123. *(u32 *)data = val;
  124. }
  125. static u64 debugfs_u32_get(void *data)
  126. {
  127. return *(u32 *)data;
  128. }
  129. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  130. /**
  131. * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write an unsigned 32 bit value.
  132. *
  133. * @name: a pointer to a string containing the name of the file to create.
  134. * @mode: the permission that the file should have
  135. * @parent: a pointer to the parent dentry for this file. This should be a
  136. * directory dentry if set. If this paramater is NULL, then the
  137. * file will be created in the root of the debugfs filesystem.
  138. * @value: a pointer to the variable that the file should read to and write
  139. * from.
  140. *
  141. * This function creates a file in debugfs with the given name that
  142. * contains the value of the variable @value. If the @mode variable is so
  143. * set, it can be read from, and written to.
  144. *
  145. * This function will return a pointer to a dentry if it succeeds. This
  146. * pointer must be passed to the debugfs_remove() function when the file is
  147. * to be removed (no automatic cleanup happens if your module is unloaded,
  148. * you are responsible here.) If an error occurs, NULL will be returned.
  149. *
  150. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  151. * returned. It is not wise to check for this value, but rather, check for
  152. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  153. * code.
  154. */
  155. struct dentry *debugfs_create_u32(const char *name, mode_t mode,
  156. struct dentry *parent, u32 *value)
  157. {
  158. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  159. }
  160. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  161. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  162. size_t count, loff_t *ppos)
  163. {
  164. char buf[3];
  165. u32 *val = file->private_data;
  166. if (*val)
  167. buf[0] = 'Y';
  168. else
  169. buf[0] = 'N';
  170. buf[1] = '\n';
  171. buf[2] = 0x00;
  172. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  173. }
  174. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  175. size_t count, loff_t *ppos)
  176. {
  177. char buf[32];
  178. int buf_size;
  179. u32 *val = file->private_data;
  180. buf_size = min(count, (sizeof(buf)-1));
  181. if (copy_from_user(buf, user_buf, buf_size))
  182. return -EFAULT;
  183. switch (buf[0]) {
  184. case 'y':
  185. case 'Y':
  186. case '1':
  187. *val = 1;
  188. break;
  189. case 'n':
  190. case 'N':
  191. case '0':
  192. *val = 0;
  193. break;
  194. }
  195. return count;
  196. }
  197. static const struct file_operations fops_bool = {
  198. .read = read_file_bool,
  199. .write = write_file_bool,
  200. .open = default_open,
  201. };
  202. /**
  203. * debugfs_create_bool - create a file in the debugfs filesystem that is used to read and write a boolean value.
  204. *
  205. * @name: a pointer to a string containing the name of the file to create.
  206. * @mode: the permission that the file should have
  207. * @parent: a pointer to the parent dentry for this file. This should be a
  208. * directory dentry if set. If this paramater is NULL, then the
  209. * file will be created in the root of the debugfs filesystem.
  210. * @value: a pointer to the variable that the file should read to and write
  211. * from.
  212. *
  213. * This function creates a file in debugfs with the given name that
  214. * contains the value of the variable @value. If the @mode variable is so
  215. * set, it can be read from, and written to.
  216. *
  217. * This function will return a pointer to a dentry if it succeeds. This
  218. * pointer must be passed to the debugfs_remove() function when the file is
  219. * to be removed (no automatic cleanup happens if your module is unloaded,
  220. * you are responsible here.) If an error occurs, NULL will be returned.
  221. *
  222. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  223. * returned. It is not wise to check for this value, but rather, check for
  224. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  225. * code.
  226. */
  227. struct dentry *debugfs_create_bool(const char *name, mode_t mode,
  228. struct dentry *parent, u32 *value)
  229. {
  230. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  231. }
  232. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  233. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  234. size_t count, loff_t *ppos)
  235. {
  236. struct debugfs_blob_wrapper *blob = file->private_data;
  237. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  238. blob->size);
  239. }
  240. static struct file_operations fops_blob = {
  241. .read = read_file_blob,
  242. .open = default_open,
  243. };
  244. /**
  245. * debugfs_create_blob - create a file in the debugfs filesystem that is
  246. * used to read and write a binary blob.
  247. *
  248. * @name: a pointer to a string containing the name of the file to create.
  249. * @mode: the permission that the file should have
  250. * @parent: a pointer to the parent dentry for this file. This should be a
  251. * directory dentry if set. If this paramater is NULL, then the
  252. * file will be created in the root of the debugfs filesystem.
  253. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  254. * to the blob data and the size of the data.
  255. *
  256. * This function creates a file in debugfs with the given name that exports
  257. * @blob->data as a binary blob. If the @mode variable is so set it can be
  258. * read from. Writing is not supported.
  259. *
  260. * This function will return a pointer to a dentry if it succeeds. This
  261. * pointer must be passed to the debugfs_remove() function when the file is
  262. * to be removed (no automatic cleanup happens if your module is unloaded,
  263. * you are responsible here.) If an error occurs, NULL will be returned.
  264. *
  265. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  266. * returned. It is not wise to check for this value, but rather, check for
  267. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  268. * code.
  269. */
  270. struct dentry *debugfs_create_blob(const char *name, mode_t mode,
  271. struct dentry *parent,
  272. struct debugfs_blob_wrapper *blob)
  273. {
  274. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  275. }
  276. EXPORT_SYMBOL_GPL(debugfs_create_blob);