file.c 29 KB

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