file.c 24 KB

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