file.c 28 KB

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