file.c 30 KB

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