file.c 29 KB

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