file.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. struct file_operations debugfs_file_operations = {
  37. .read = default_read_file,
  38. .write = default_write_file,
  39. .open = default_open,
  40. };
  41. #define simple_type(type, format, temptype, strtolfn) \
  42. static ssize_t read_file_##type(struct file *file, char __user *user_buf, \
  43. size_t count, loff_t *ppos) \
  44. { \
  45. char buf[32]; \
  46. type *val = file->private_data; \
  47. \
  48. snprintf(buf, sizeof(buf), format "\n", *val); \
  49. return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));\
  50. } \
  51. static ssize_t write_file_##type(struct file *file, const char __user *user_buf,\
  52. size_t count, loff_t *ppos) \
  53. { \
  54. char *endp; \
  55. char buf[32]; \
  56. int buf_size; \
  57. type *val = file->private_data; \
  58. temptype tmp; \
  59. \
  60. memset(buf, 0x00, sizeof(buf)); \
  61. buf_size = min(count, (sizeof(buf)-1)); \
  62. if (copy_from_user(buf, user_buf, buf_size)) \
  63. return -EFAULT; \
  64. \
  65. tmp = strtolfn(buf, &endp, 0); \
  66. if ((endp == buf) || ((type)tmp != tmp)) \
  67. return -EINVAL; \
  68. *val = tmp; \
  69. return count; \
  70. } \
  71. static struct file_operations fops_##type = { \
  72. .read = read_file_##type, \
  73. .write = write_file_##type, \
  74. .open = default_open, \
  75. };
  76. simple_type(u8, "%c", unsigned long, simple_strtoul);
  77. simple_type(u16, "%hi", unsigned long, simple_strtoul);
  78. simple_type(u32, "%i", unsigned long, simple_strtoul);
  79. /**
  80. * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
  81. *
  82. * @name: a pointer to a string containing the name of the file to create.
  83. * @mode: the permission that the file should have
  84. * @parent: a pointer to the parent dentry for this file. This should be a
  85. * directory dentry if set. If this paramater is NULL, then the
  86. * file will be created in the root of the debugfs filesystem.
  87. * @value: a pointer to the variable that the file should read to and write
  88. * from.
  89. *
  90. * This function creates a file in debugfs with the given name that
  91. * contains the value of the variable @value. If the @mode variable is so
  92. * set, it can be read from, and written to.
  93. *
  94. * This function will return a pointer to a dentry if it succeeds. This
  95. * pointer must be passed to the debugfs_remove() function when the file is
  96. * to be removed (no automatic cleanup happens if your module is unloaded,
  97. * you are responsible here.) If an error occurs, NULL will be returned.
  98. *
  99. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  100. * returned. It is not wise to check for this value, but rather, check for
  101. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  102. * code.
  103. */
  104. struct dentry *debugfs_create_u8(const char *name, mode_t mode,
  105. struct dentry *parent, u8 *value)
  106. {
  107. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  108. }
  109. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  110. /**
  111. * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
  112. *
  113. * @name: a pointer to a string containing the name of the file to create.
  114. * @mode: the permission that the file should have
  115. * @parent: a pointer to the parent dentry for this file. This should be a
  116. * directory dentry if set. If this paramater is NULL, then the
  117. * file will be created in the root of the debugfs filesystem.
  118. * @value: a pointer to the variable that the file should read to and write
  119. * from.
  120. *
  121. * This function creates a file in debugfs with the given name that
  122. * contains the value of the variable @value. If the @mode variable is so
  123. * set, it can be read from, and written to.
  124. *
  125. * This function will return a pointer to a dentry if it succeeds. This
  126. * pointer must be passed to the debugfs_remove() function when the file is
  127. * to be removed (no automatic cleanup happens if your module is unloaded,
  128. * you are responsible here.) If an error occurs, NULL will be returned.
  129. *
  130. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  131. * returned. It is not wise to check for this value, but rather, check for
  132. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  133. * code.
  134. */
  135. struct dentry *debugfs_create_u16(const char *name, mode_t mode,
  136. struct dentry *parent, u16 *value)
  137. {
  138. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  139. }
  140. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  141. /**
  142. * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
  143. *
  144. * @name: a pointer to a string containing the name of the file to create.
  145. * @mode: the permission that the file should have
  146. * @parent: a pointer to the parent dentry for this file. This should be a
  147. * directory dentry if set. If this paramater is NULL, then the
  148. * file will be created in the root of the debugfs filesystem.
  149. * @value: a pointer to the variable that the file should read to and write
  150. * from.
  151. *
  152. * This function creates a file in debugfs with the given name that
  153. * contains the value of the variable @value. If the @mode variable is so
  154. * set, it can be read from, and written to.
  155. *
  156. * This function will return a pointer to a dentry if it succeeds. This
  157. * pointer must be passed to the debugfs_remove() function when the file is
  158. * to be removed (no automatic cleanup happens if your module is unloaded,
  159. * you are responsible here.) If an error occurs, NULL will be returned.
  160. *
  161. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  162. * returned. It is not wise to check for this value, but rather, check for
  163. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  164. * code.
  165. */
  166. struct dentry *debugfs_create_u32(const char *name, mode_t mode,
  167. struct dentry *parent, u32 *value)
  168. {
  169. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  170. }
  171. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  172. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. char buf[3];
  176. u32 *val = file->private_data;
  177. if (*val)
  178. buf[0] = 'Y';
  179. else
  180. buf[0] = 'N';
  181. buf[1] = '\n';
  182. buf[2] = 0x00;
  183. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  184. }
  185. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  186. size_t count, loff_t *ppos)
  187. {
  188. char buf[32];
  189. int buf_size;
  190. u32 *val = file->private_data;
  191. buf_size = min(count, (sizeof(buf)-1));
  192. if (copy_from_user(buf, user_buf, buf_size))
  193. return -EFAULT;
  194. switch (buf[0]) {
  195. case 'y':
  196. case 'Y':
  197. case '1':
  198. *val = 1;
  199. break;
  200. case 'n':
  201. case 'N':
  202. case '0':
  203. *val = 0;
  204. break;
  205. }
  206. return count;
  207. }
  208. static struct file_operations fops_bool = {
  209. .read = read_file_bool,
  210. .write = write_file_bool,
  211. .open = default_open,
  212. };
  213. /**
  214. * debugfs_create_bool - create a file in the debugfs filesystem that is used to read and write a boolean value.
  215. *
  216. * @name: a pointer to a string containing the name of the file to create.
  217. * @mode: the permission that the file should have
  218. * @parent: a pointer to the parent dentry for this file. This should be a
  219. * directory dentry if set. If this paramater is NULL, then the
  220. * file will be created in the root of the debugfs filesystem.
  221. * @value: a pointer to the variable that the file should read to and write
  222. * from.
  223. *
  224. * This function creates a file in debugfs with the given name that
  225. * contains the value of the variable @value. If the @mode variable is so
  226. * set, it can be read from, and written to.
  227. *
  228. * This function will return a pointer to a dentry if it succeeds. This
  229. * pointer must be passed to the debugfs_remove() function when the file is
  230. * to be removed (no automatic cleanup happens if your module is unloaded,
  231. * you are responsible here.) If an error occurs, NULL will be returned.
  232. *
  233. * If debugfs is not enabled in the kernel, the value -ENODEV will be
  234. * returned. It is not wise to check for this value, but rather, check for
  235. * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
  236. * code.
  237. */
  238. struct dentry *debugfs_create_bool(const char *name, mode_t mode,
  239. struct dentry *parent, u32 *value)
  240. {
  241. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  242. }
  243. EXPORT_SYMBOL_GPL(debugfs_create_bool);