file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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/filesystems for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/namei.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/io.h>
  22. #include <linux/atomic.h>
  23. static ssize_t default_read_file(struct file *file, char __user *buf,
  24. size_t count, loff_t *ppos)
  25. {
  26. return 0;
  27. }
  28. static ssize_t default_write_file(struct file *file, const char __user *buf,
  29. size_t count, loff_t *ppos)
  30. {
  31. return count;
  32. }
  33. const struct file_operations debugfs_file_operations = {
  34. .read = default_read_file,
  35. .write = default_write_file,
  36. .open = simple_open,
  37. .llseek = noop_llseek,
  38. };
  39. static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  40. {
  41. nd_set_link(nd, dentry->d_inode->i_private);
  42. return NULL;
  43. }
  44. const struct inode_operations debugfs_link_operations = {
  45. .readlink = generic_readlink,
  46. .follow_link = debugfs_follow_link,
  47. };
  48. static int debugfs_u8_set(void *data, u64 val)
  49. {
  50. *(u8 *)data = val;
  51. return 0;
  52. }
  53. static int debugfs_u8_get(void *data, u64 *val)
  54. {
  55. *val = *(u8 *)data;
  56. return 0;
  57. }
  58. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  59. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  60. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  61. /**
  62. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  63. * @name: a pointer to a string containing the name of the file to create.
  64. * @mode: the permission that the file should have
  65. * @parent: a pointer to the parent dentry for this file. This should be a
  66. * directory dentry if set. If this parameter is %NULL, then the
  67. * file will be created in the root of the debugfs filesystem.
  68. * @value: a pointer to the variable that the file should read to and write
  69. * from.
  70. *
  71. * This function creates a file in debugfs with the given name that
  72. * contains the value of the variable @value. If the @mode variable is so
  73. * set, it can be read from, and written to.
  74. *
  75. * This function will return a pointer to a dentry if it succeeds. This
  76. * pointer must be passed to the debugfs_remove() function when the file is
  77. * to be removed (no automatic cleanup happens if your module is unloaded,
  78. * you are responsible here.) If an error occurs, %NULL will be returned.
  79. *
  80. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  81. * returned. It is not wise to check for this value, but rather, check for
  82. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  83. * code.
  84. */
  85. struct dentry *debugfs_create_u8(const char *name, umode_t mode,
  86. struct dentry *parent, u8 *value)
  87. {
  88. /* if there are no write bits set, make read only */
  89. if (!(mode & S_IWUGO))
  90. return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
  91. /* if there are no read bits set, make write only */
  92. if (!(mode & S_IRUGO))
  93. return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
  94. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  95. }
  96. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  97. static int debugfs_u16_set(void *data, u64 val)
  98. {
  99. *(u16 *)data = val;
  100. return 0;
  101. }
  102. static int debugfs_u16_get(void *data, u64 *val)
  103. {
  104. *val = *(u16 *)data;
  105. return 0;
  106. }
  107. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  108. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  109. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  110. /**
  111. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  112. * @name: a pointer to a string containing the name of the file to create.
  113. * @mode: the permission that the file should have
  114. * @parent: a pointer to the parent dentry for this file. This should be a
  115. * directory dentry if set. If this parameter is %NULL, then the
  116. * file will be created in the root of the debugfs filesystem.
  117. * @value: a pointer to the variable that the file should read to and write
  118. * from.
  119. *
  120. * This function creates a file in debugfs with the given name that
  121. * contains the value of the variable @value. If the @mode variable is so
  122. * set, it can be read from, and written to.
  123. *
  124. * This function will return a pointer to a dentry if it succeeds. This
  125. * pointer must be passed to the debugfs_remove() function when the file is
  126. * to be removed (no automatic cleanup happens if your module is unloaded,
  127. * you are responsible here.) If an error occurs, %NULL will be returned.
  128. *
  129. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  130. * returned. It is not wise to check for this value, but rather, check for
  131. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  132. * code.
  133. */
  134. struct dentry *debugfs_create_u16(const char *name, umode_t mode,
  135. struct dentry *parent, u16 *value)
  136. {
  137. /* if there are no write bits set, make read only */
  138. if (!(mode & S_IWUGO))
  139. return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
  140. /* if there are no read bits set, make write only */
  141. if (!(mode & S_IRUGO))
  142. return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
  143. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  144. }
  145. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  146. static int debugfs_u32_set(void *data, u64 val)
  147. {
  148. *(u32 *)data = val;
  149. return 0;
  150. }
  151. static int debugfs_u32_get(void *data, u64 *val)
  152. {
  153. *val = *(u32 *)data;
  154. return 0;
  155. }
  156. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  157. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  158. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  159. /**
  160. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  161. * @name: a pointer to a string containing the name of the file to create.
  162. * @mode: the permission that the file should have
  163. * @parent: a pointer to the parent dentry for this file. This should be a
  164. * directory dentry if set. If this parameter is %NULL, then the
  165. * file will be created in the root of the debugfs filesystem.
  166. * @value: a pointer to the variable that the file should read to and write
  167. * from.
  168. *
  169. * This function creates a file in debugfs with the given name that
  170. * contains the value of the variable @value. If the @mode variable is so
  171. * set, it can be read from, and written to.
  172. *
  173. * This function will return a pointer to a dentry if it succeeds. This
  174. * pointer must be passed to the debugfs_remove() function when the file is
  175. * to be removed (no automatic cleanup happens if your module is unloaded,
  176. * you are responsible here.) If an error occurs, %NULL will be returned.
  177. *
  178. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  179. * returned. It is not wise to check for this value, but rather, check for
  180. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  181. * code.
  182. */
  183. struct dentry *debugfs_create_u32(const char *name, umode_t mode,
  184. struct dentry *parent, u32 *value)
  185. {
  186. /* if there are no write bits set, make read only */
  187. if (!(mode & S_IWUGO))
  188. return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
  189. /* if there are no read bits set, make write only */
  190. if (!(mode & S_IRUGO))
  191. return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
  192. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  193. }
  194. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  195. static int debugfs_u64_set(void *data, u64 val)
  196. {
  197. *(u64 *)data = val;
  198. return 0;
  199. }
  200. static int debugfs_u64_get(void *data, u64 *val)
  201. {
  202. *val = *(u64 *)data;
  203. return 0;
  204. }
  205. DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  206. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  207. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  208. /**
  209. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  210. * @name: a pointer to a string containing the name of the file to create.
  211. * @mode: the permission that the file should have
  212. * @parent: a pointer to the parent dentry for this file. This should be a
  213. * directory dentry if set. If this parameter is %NULL, then the
  214. * file will be created in the root of the debugfs filesystem.
  215. * @value: a pointer to the variable that the file should read to and write
  216. * from.
  217. *
  218. * This function creates a file in debugfs with the given name that
  219. * contains the value of the variable @value. If the @mode variable is so
  220. * set, it can be read from, and written to.
  221. *
  222. * This function will return a pointer to a dentry if it succeeds. This
  223. * pointer must be passed to the debugfs_remove() function when the file is
  224. * to be removed (no automatic cleanup happens if your module is unloaded,
  225. * you are responsible here.) If an error occurs, %NULL will be returned.
  226. *
  227. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  228. * returned. It is not wise to check for this value, but rather, check for
  229. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  230. * code.
  231. */
  232. struct dentry *debugfs_create_u64(const char *name, umode_t mode,
  233. struct dentry *parent, u64 *value)
  234. {
  235. /* if there are no write bits set, make read only */
  236. if (!(mode & S_IWUGO))
  237. return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
  238. /* if there are no read bits set, make write only */
  239. if (!(mode & S_IRUGO))
  240. return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
  241. return debugfs_create_file(name, mode, parent, value, &fops_u64);
  242. }
  243. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  244. DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  245. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  246. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  247. DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
  248. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  249. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  250. DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
  251. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  252. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  253. DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
  254. /*
  255. * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
  256. *
  257. * These functions are exactly the same as the above functions (but use a hex
  258. * output for the decimal challenged). For details look at the above unsigned
  259. * decimal functions.
  260. */
  261. /**
  262. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  263. * @name: a pointer to a string containing the name of the file to create.
  264. * @mode: the permission that the file should have
  265. * @parent: a pointer to the parent dentry for this file. This should be a
  266. * directory dentry if set. If this parameter is %NULL, then the
  267. * file will be created in the root of the debugfs filesystem.
  268. * @value: a pointer to the variable that the file should read to and write
  269. * from.
  270. */
  271. struct dentry *debugfs_create_x8(const char *name, umode_t mode,
  272. struct dentry *parent, u8 *value)
  273. {
  274. /* if there are no write bits set, make read only */
  275. if (!(mode & S_IWUGO))
  276. return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
  277. /* if there are no read bits set, make write only */
  278. if (!(mode & S_IRUGO))
  279. return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
  280. return debugfs_create_file(name, mode, parent, value, &fops_x8);
  281. }
  282. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  283. /**
  284. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  285. * @name: a pointer to a string containing the name of the file to create.
  286. * @mode: the permission that the file should have
  287. * @parent: a pointer to the parent dentry for this file. This should be a
  288. * directory dentry if set. If this parameter is %NULL, then the
  289. * file will be created in the root of the debugfs filesystem.
  290. * @value: a pointer to the variable that the file should read to and write
  291. * from.
  292. */
  293. struct dentry *debugfs_create_x16(const char *name, umode_t mode,
  294. struct dentry *parent, u16 *value)
  295. {
  296. /* if there are no write bits set, make read only */
  297. if (!(mode & S_IWUGO))
  298. return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
  299. /* if there are no read bits set, make write only */
  300. if (!(mode & S_IRUGO))
  301. return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
  302. return debugfs_create_file(name, mode, parent, value, &fops_x16);
  303. }
  304. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  305. /**
  306. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  307. * @name: a pointer to a string containing the name of the file to create.
  308. * @mode: the permission that the file should have
  309. * @parent: a pointer to the parent dentry for this file. This should be a
  310. * directory dentry if set. If this parameter is %NULL, then the
  311. * file will be created in the root of the debugfs filesystem.
  312. * @value: a pointer to the variable that the file should read to and write
  313. * from.
  314. */
  315. struct dentry *debugfs_create_x32(const char *name, umode_t mode,
  316. struct dentry *parent, u32 *value)
  317. {
  318. /* if there are no write bits set, make read only */
  319. if (!(mode & S_IWUGO))
  320. return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
  321. /* if there are no read bits set, make write only */
  322. if (!(mode & S_IRUGO))
  323. return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
  324. return debugfs_create_file(name, mode, parent, value, &fops_x32);
  325. }
  326. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  327. /**
  328. * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  329. * @name: a pointer to a string containing the name of the file to create.
  330. * @mode: the permission that the file should have
  331. * @parent: a pointer to the parent dentry for this file. This should be a
  332. * directory dentry if set. If this parameter is %NULL, then the
  333. * file will be created in the root of the debugfs filesystem.
  334. * @value: a pointer to the variable that the file should read to and write
  335. * from.
  336. */
  337. struct dentry *debugfs_create_x64(const char *name, umode_t mode,
  338. struct dentry *parent, u64 *value)
  339. {
  340. return debugfs_create_file(name, mode, parent, value, &fops_x64);
  341. }
  342. EXPORT_SYMBOL_GPL(debugfs_create_x64);
  343. static int debugfs_size_t_set(void *data, u64 val)
  344. {
  345. *(size_t *)data = val;
  346. return 0;
  347. }
  348. static int debugfs_size_t_get(void *data, u64 *val)
  349. {
  350. *val = *(size_t *)data;
  351. return 0;
  352. }
  353. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  354. "%llu\n"); /* %llu and %zu are more or less the same */
  355. /**
  356. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  357. * @name: a pointer to a string containing the name of the file to create.
  358. * @mode: the permission that the file should have
  359. * @parent: a pointer to the parent dentry for this file. This should be a
  360. * directory dentry if set. If this parameter is %NULL, then the
  361. * file will be created in the root of the debugfs filesystem.
  362. * @value: a pointer to the variable that the file should read to and write
  363. * from.
  364. */
  365. struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
  366. struct dentry *parent, size_t *value)
  367. {
  368. return debugfs_create_file(name, mode, parent, value, &fops_size_t);
  369. }
  370. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  371. static int debugfs_atomic_t_set(void *data, u64 val)
  372. {
  373. atomic_set((atomic_t *)data, val);
  374. return 0;
  375. }
  376. static int debugfs_atomic_t_get(void *data, u64 *val)
  377. {
  378. *val = atomic_read((atomic_t *)data);
  379. return 0;
  380. }
  381. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  382. debugfs_atomic_t_set, "%lld\n");
  383. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
  384. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
  385. /**
  386. * debugfs_create_atomic_t - create a debugfs file that is used to read and
  387. * write an atomic_t value
  388. * @name: a pointer to a string containing the name of the file to create.
  389. * @mode: the permission that the file should have
  390. * @parent: a pointer to the parent dentry for this file. This should be a
  391. * directory dentry if set. If this parameter is %NULL, then the
  392. * file will be created in the root of the debugfs filesystem.
  393. * @value: a pointer to the variable that the file should read to and write
  394. * from.
  395. */
  396. struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
  397. struct dentry *parent, atomic_t *value)
  398. {
  399. /* if there are no write bits set, make read only */
  400. if (!(mode & S_IWUGO))
  401. return debugfs_create_file(name, mode, parent, value,
  402. &fops_atomic_t_ro);
  403. /* if there are no read bits set, make write only */
  404. if (!(mode & S_IRUGO))
  405. return debugfs_create_file(name, mode, parent, value,
  406. &fops_atomic_t_wo);
  407. return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
  408. }
  409. EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
  410. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  411. size_t count, loff_t *ppos)
  412. {
  413. char buf[3];
  414. u32 *val = file->private_data;
  415. if (*val)
  416. buf[0] = 'Y';
  417. else
  418. buf[0] = 'N';
  419. buf[1] = '\n';
  420. buf[2] = 0x00;
  421. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  422. }
  423. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  424. size_t count, loff_t *ppos)
  425. {
  426. char buf[32];
  427. size_t buf_size;
  428. bool bv;
  429. u32 *val = file->private_data;
  430. buf_size = min(count, (sizeof(buf)-1));
  431. if (copy_from_user(buf, user_buf, buf_size))
  432. return -EFAULT;
  433. if (strtobool(buf, &bv) == 0)
  434. *val = bv;
  435. return count;
  436. }
  437. static const struct file_operations fops_bool = {
  438. .read = read_file_bool,
  439. .write = write_file_bool,
  440. .open = simple_open,
  441. .llseek = default_llseek,
  442. };
  443. /**
  444. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  445. * @name: a pointer to a string containing the name of the file to create.
  446. * @mode: the permission that the file should have
  447. * @parent: a pointer to the parent dentry for this file. This should be a
  448. * directory dentry if set. If this parameter is %NULL, then the
  449. * file will be created in the root of the debugfs filesystem.
  450. * @value: a pointer to the variable that the file should read to and write
  451. * from.
  452. *
  453. * This function creates a file in debugfs with the given name that
  454. * contains the value of the variable @value. If the @mode variable is so
  455. * set, it can be read from, and written to.
  456. *
  457. * This function will return a pointer to a dentry if it succeeds. This
  458. * pointer must be passed to the debugfs_remove() function when the file is
  459. * to be removed (no automatic cleanup happens if your module is unloaded,
  460. * you are responsible here.) If an error occurs, %NULL will be returned.
  461. *
  462. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  463. * returned. It is not wise to check for this value, but rather, check for
  464. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  465. * code.
  466. */
  467. struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  468. struct dentry *parent, u32 *value)
  469. {
  470. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  471. }
  472. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  473. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  474. size_t count, loff_t *ppos)
  475. {
  476. struct debugfs_blob_wrapper *blob = file->private_data;
  477. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  478. blob->size);
  479. }
  480. static const struct file_operations fops_blob = {
  481. .read = read_file_blob,
  482. .open = simple_open,
  483. .llseek = default_llseek,
  484. };
  485. /**
  486. * debugfs_create_blob - create a debugfs file that is used to read a binary blob
  487. * @name: a pointer to a string containing the name of the file to create.
  488. * @mode: the permission that the file should have
  489. * @parent: a pointer to the parent dentry for this file. This should be a
  490. * directory dentry if set. If this parameter is %NULL, then the
  491. * file will be created in the root of the debugfs filesystem.
  492. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  493. * to the blob data and the size of the data.
  494. *
  495. * This function creates a file in debugfs with the given name that exports
  496. * @blob->data as a binary blob. If the @mode variable is so set it can be
  497. * read from. Writing is not supported.
  498. *
  499. * This function will return a pointer to a dentry if it succeeds. This
  500. * pointer must be passed to the debugfs_remove() function when the file is
  501. * to be removed (no automatic cleanup happens if your module is unloaded,
  502. * you are responsible here.) If an error occurs, %NULL will be returned.
  503. *
  504. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  505. * returned. It is not wise to check for this value, but rather, check for
  506. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  507. * code.
  508. */
  509. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  510. struct dentry *parent,
  511. struct debugfs_blob_wrapper *blob)
  512. {
  513. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  514. }
  515. EXPORT_SYMBOL_GPL(debugfs_create_blob);
  516. #ifdef CONFIG_HAS_IOMEM
  517. /*
  518. * The regset32 stuff is used to print 32-bit registers using the
  519. * seq_file utilities. We offer printing a register set in an already-opened
  520. * sequential file or create a debugfs file that only prints a regset32.
  521. */
  522. /**
  523. * debugfs_print_regs32 - use seq_print to describe a set of registers
  524. * @s: the seq_file structure being used to generate output
  525. * @regs: an array if struct debugfs_reg32 structures
  526. * @nregs: the length of the above array
  527. * @base: the base address to be used in reading the registers
  528. * @prefix: a string to be prefixed to every output line
  529. *
  530. * This function outputs a text block describing the current values of
  531. * some 32-bit hardware registers. It is meant to be used within debugfs
  532. * files based on seq_file that need to show registers, intermixed with other
  533. * information. The prefix argument may be used to specify a leading string,
  534. * because some peripherals have several blocks of identical registers,
  535. * for example configuration of dma channels
  536. */
  537. int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  538. int nregs, void __iomem *base, char *prefix)
  539. {
  540. int i, ret = 0;
  541. for (i = 0; i < nregs; i++, regs++) {
  542. if (prefix)
  543. ret += seq_printf(s, "%s", prefix);
  544. ret += seq_printf(s, "%s = 0x%08x\n", regs->name,
  545. readl(base + regs->offset));
  546. }
  547. return ret;
  548. }
  549. EXPORT_SYMBOL_GPL(debugfs_print_regs32);
  550. static int debugfs_show_regset32(struct seq_file *s, void *data)
  551. {
  552. struct debugfs_regset32 *regset = s->private;
  553. debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
  554. return 0;
  555. }
  556. static int debugfs_open_regset32(struct inode *inode, struct file *file)
  557. {
  558. return single_open(file, debugfs_show_regset32, inode->i_private);
  559. }
  560. static const struct file_operations fops_regset32 = {
  561. .open = debugfs_open_regset32,
  562. .read = seq_read,
  563. .llseek = seq_lseek,
  564. .release = single_release,
  565. };
  566. /**
  567. * debugfs_create_regset32 - create a debugfs file that returns register values
  568. * @name: a pointer to a string containing the name of the file to create.
  569. * @mode: the permission that the file should have
  570. * @parent: a pointer to the parent dentry for this file. This should be a
  571. * directory dentry if set. If this parameter is %NULL, then the
  572. * file will be created in the root of the debugfs filesystem.
  573. * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
  574. * to an array of register definitions, the array size and the base
  575. * address where the register bank is to be found.
  576. *
  577. * This function creates a file in debugfs with the given name that reports
  578. * the names and values of a set of 32-bit registers. If the @mode variable
  579. * is so set it can be read from. Writing is not supported.
  580. *
  581. * This function will return a pointer to a dentry if it succeeds. This
  582. * pointer must be passed to the debugfs_remove() function when the file is
  583. * to be removed (no automatic cleanup happens if your module is unloaded,
  584. * you are responsible here.) If an error occurs, %NULL will be returned.
  585. *
  586. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  587. * returned. It is not wise to check for this value, but rather, check for
  588. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  589. * code.
  590. */
  591. struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
  592. struct dentry *parent,
  593. struct debugfs_regset32 *regset)
  594. {
  595. return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
  596. }
  597. EXPORT_SYMBOL_GPL(debugfs_create_regset32);
  598. #endif /* CONFIG_HAS_IOMEM */