file.c 28 KB

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