file.c 29 KB

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