file.c 27 KB

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