file.c 24 KB

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