file.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. #include <linux/srcu.h>
  25. #include <asm/poll.h>
  26. #include "internal.h"
  27. struct poll_table_struct;
  28. static ssize_t default_read_file(struct file *file, char __user *buf,
  29. size_t count, loff_t *ppos)
  30. {
  31. return 0;
  32. }
  33. static ssize_t default_write_file(struct file *file, const char __user *buf,
  34. size_t count, loff_t *ppos)
  35. {
  36. return count;
  37. }
  38. const struct file_operations debugfs_noop_file_operations = {
  39. .read = default_read_file,
  40. .write = default_write_file,
  41. .open = simple_open,
  42. .llseek = noop_llseek,
  43. };
  44. /**
  45. * debugfs_use_file_start - mark the beginning of file data access
  46. * @dentry: the dentry object whose data is being accessed.
  47. * @srcu_idx: a pointer to some memory to store a SRCU index in.
  48. *
  49. * Up to a matching call to debugfs_use_file_finish(), any
  50. * successive call into the file removing functions debugfs_remove()
  51. * and debugfs_remove_recursive() will block. Since associated private
  52. * file data may only get freed after a successful return of any of
  53. * the removal functions, you may safely access it after a successful
  54. * call to debugfs_use_file_start() without worrying about
  55. * lifetime issues.
  56. *
  57. * If -%EIO is returned, the file has already been removed and thus,
  58. * it is not safe to access any of its data. If, on the other hand,
  59. * it is allowed to access the file data, zero is returned.
  60. *
  61. * Regardless of the return code, any call to
  62. * debugfs_use_file_start() must be followed by a matching call
  63. * to debugfs_use_file_finish().
  64. */
  65. int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
  66. __acquires(&debugfs_srcu)
  67. {
  68. *srcu_idx = srcu_read_lock(&debugfs_srcu);
  69. barrier();
  70. if (d_unlinked(dentry))
  71. return -EIO;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(debugfs_use_file_start);
  75. /**
  76. * debugfs_use_file_finish - mark the end of file data access
  77. * @srcu_idx: the SRCU index "created" by a former call to
  78. * debugfs_use_file_start().
  79. *
  80. * Allow any ongoing concurrent call into debugfs_remove() or
  81. * debugfs_remove_recursive() blocked by a former call to
  82. * debugfs_use_file_start() to proceed and return to its caller.
  83. */
  84. void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
  85. {
  86. srcu_read_unlock(&debugfs_srcu, srcu_idx);
  87. }
  88. EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
  89. #define F_DENTRY(filp) ((filp)->f_path.dentry)
  90. static int open_proxy_open(struct inode *inode, struct file *filp)
  91. {
  92. const struct dentry *dentry = F_DENTRY(filp);
  93. const struct file_operations *real_fops = NULL;
  94. int srcu_idx, r;
  95. r = debugfs_use_file_start(dentry, &srcu_idx);
  96. if (r) {
  97. r = -ENOENT;
  98. goto out;
  99. }
  100. real_fops = debugfs_real_fops(filp);
  101. real_fops = fops_get(real_fops);
  102. if (!real_fops) {
  103. /* Huh? Module did not clean up after itself at exit? */
  104. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  105. dentry);
  106. r = -ENXIO;
  107. goto out;
  108. }
  109. replace_fops(filp, real_fops);
  110. if (real_fops->open)
  111. r = real_fops->open(inode, filp);
  112. out:
  113. debugfs_use_file_finish(srcu_idx);
  114. return r;
  115. }
  116. const struct file_operations debugfs_open_proxy_file_operations = {
  117. .open = open_proxy_open,
  118. };
  119. #define PROTO(args...) args
  120. #define ARGS(args...) args
  121. #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
  122. static ret_type full_proxy_ ## name(proto) \
  123. { \
  124. const struct dentry *dentry = F_DENTRY(filp); \
  125. const struct file_operations *real_fops = \
  126. debugfs_real_fops(filp); \
  127. int srcu_idx; \
  128. ret_type r; \
  129. \
  130. r = debugfs_use_file_start(dentry, &srcu_idx); \
  131. if (likely(!r)) \
  132. r = real_fops->name(args); \
  133. debugfs_use_file_finish(srcu_idx); \
  134. return r; \
  135. }
  136. FULL_PROXY_FUNC(llseek, loff_t, filp,
  137. PROTO(struct file *filp, loff_t offset, int whence),
  138. ARGS(filp, offset, whence));
  139. FULL_PROXY_FUNC(read, ssize_t, filp,
  140. PROTO(struct file *filp, char __user *buf, size_t size,
  141. loff_t *ppos),
  142. ARGS(filp, buf, size, ppos));
  143. FULL_PROXY_FUNC(write, ssize_t, filp,
  144. PROTO(struct file *filp, const char __user *buf, size_t size,
  145. loff_t *ppos),
  146. ARGS(filp, buf, size, ppos));
  147. FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
  148. PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
  149. ARGS(filp, cmd, arg));
  150. static unsigned int full_proxy_poll(struct file *filp,
  151. struct poll_table_struct *wait)
  152. {
  153. const struct dentry *dentry = F_DENTRY(filp);
  154. const struct file_operations *real_fops = debugfs_real_fops(filp);
  155. int srcu_idx;
  156. unsigned int r = 0;
  157. if (debugfs_use_file_start(dentry, &srcu_idx)) {
  158. debugfs_use_file_finish(srcu_idx);
  159. return POLLHUP;
  160. }
  161. r = real_fops->poll(filp, wait);
  162. debugfs_use_file_finish(srcu_idx);
  163. return r;
  164. }
  165. static int full_proxy_release(struct inode *inode, struct file *filp)
  166. {
  167. const struct dentry *dentry = F_DENTRY(filp);
  168. const struct file_operations *real_fops = debugfs_real_fops(filp);
  169. const struct file_operations *proxy_fops = filp->f_op;
  170. int r = 0;
  171. /*
  172. * We must not protect this against removal races here: the
  173. * original releaser should be called unconditionally in order
  174. * not to leak any resources. Releasers must not assume that
  175. * ->i_private is still being meaningful here.
  176. */
  177. if (real_fops->release)
  178. r = real_fops->release(inode, filp);
  179. replace_fops(filp, d_inode(dentry)->i_fop);
  180. kfree((void *)proxy_fops);
  181. fops_put(real_fops);
  182. return r;
  183. }
  184. static void __full_proxy_fops_init(struct file_operations *proxy_fops,
  185. const struct file_operations *real_fops)
  186. {
  187. proxy_fops->release = full_proxy_release;
  188. if (real_fops->llseek)
  189. proxy_fops->llseek = full_proxy_llseek;
  190. if (real_fops->read)
  191. proxy_fops->read = full_proxy_read;
  192. if (real_fops->write)
  193. proxy_fops->write = full_proxy_write;
  194. if (real_fops->poll)
  195. proxy_fops->poll = full_proxy_poll;
  196. if (real_fops->unlocked_ioctl)
  197. proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
  198. }
  199. static int full_proxy_open(struct inode *inode, struct file *filp)
  200. {
  201. const struct dentry *dentry = F_DENTRY(filp);
  202. const struct file_operations *real_fops = NULL;
  203. struct file_operations *proxy_fops = NULL;
  204. int srcu_idx, r;
  205. r = debugfs_use_file_start(dentry, &srcu_idx);
  206. if (r) {
  207. r = -ENOENT;
  208. goto out;
  209. }
  210. real_fops = debugfs_real_fops(filp);
  211. real_fops = fops_get(real_fops);
  212. if (!real_fops) {
  213. /* Huh? Module did not cleanup after itself at exit? */
  214. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  215. dentry);
  216. r = -ENXIO;
  217. goto out;
  218. }
  219. proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
  220. if (!proxy_fops) {
  221. r = -ENOMEM;
  222. goto free_proxy;
  223. }
  224. __full_proxy_fops_init(proxy_fops, real_fops);
  225. replace_fops(filp, proxy_fops);
  226. if (real_fops->open) {
  227. r = real_fops->open(inode, filp);
  228. if (r) {
  229. replace_fops(filp, d_inode(dentry)->i_fop);
  230. goto free_proxy;
  231. } else if (filp->f_op != proxy_fops) {
  232. /* No protection against file removal anymore. */
  233. WARN(1, "debugfs file owner replaced proxy fops: %pd",
  234. dentry);
  235. goto free_proxy;
  236. }
  237. }
  238. goto out;
  239. free_proxy:
  240. kfree(proxy_fops);
  241. fops_put(real_fops);
  242. out:
  243. debugfs_use_file_finish(srcu_idx);
  244. return r;
  245. }
  246. const struct file_operations debugfs_full_proxy_file_operations = {
  247. .open = full_proxy_open,
  248. };
  249. ssize_t debugfs_attr_read(struct file *file, char __user *buf,
  250. size_t len, loff_t *ppos)
  251. {
  252. ssize_t ret;
  253. int srcu_idx;
  254. ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  255. if (likely(!ret))
  256. ret = simple_attr_read(file, buf, len, ppos);
  257. debugfs_use_file_finish(srcu_idx);
  258. return ret;
  259. }
  260. EXPORT_SYMBOL_GPL(debugfs_attr_read);
  261. ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
  262. size_t len, loff_t *ppos)
  263. {
  264. ssize_t ret;
  265. int srcu_idx;
  266. ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  267. if (likely(!ret))
  268. ret = simple_attr_write(file, buf, len, ppos);
  269. debugfs_use_file_finish(srcu_idx);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(debugfs_attr_write);
  273. static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
  274. struct dentry *parent, void *value,
  275. const struct file_operations *fops,
  276. const struct file_operations *fops_ro,
  277. const struct file_operations *fops_wo)
  278. {
  279. /* if there are no write bits set, make read only */
  280. if (!(mode & S_IWUGO))
  281. return debugfs_create_file_unsafe(name, mode, parent, value,
  282. fops_ro);
  283. /* if there are no read bits set, make write only */
  284. if (!(mode & S_IRUGO))
  285. return debugfs_create_file_unsafe(name, mode, parent, value,
  286. fops_wo);
  287. return debugfs_create_file_unsafe(name, mode, parent, value, fops);
  288. }
  289. static int debugfs_u8_set(void *data, u64 val)
  290. {
  291. *(u8 *)data = val;
  292. return 0;
  293. }
  294. static int debugfs_u8_get(void *data, u64 *val)
  295. {
  296. *val = *(u8 *)data;
  297. return 0;
  298. }
  299. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  300. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  301. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  302. /**
  303. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  304. * @name: a pointer to a string containing the name of the file to create.
  305. * @mode: the permission that the file should have
  306. * @parent: a pointer to the parent dentry for this file. This should be a
  307. * directory dentry if set. If this parameter is %NULL, then the
  308. * file will be created in the root of the debugfs filesystem.
  309. * @value: a pointer to the variable that the file should read to and write
  310. * from.
  311. *
  312. * This function creates a file in debugfs with the given name that
  313. * contains the value of the variable @value. If the @mode variable is so
  314. * set, it can be read from, and written to.
  315. *
  316. * This function will return a pointer to a dentry if it succeeds. This
  317. * pointer must be passed to the debugfs_remove() function when the file is
  318. * to be removed (no automatic cleanup happens if your module is unloaded,
  319. * you are responsible here.) If an error occurs, %NULL will be returned.
  320. *
  321. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  322. * returned. It is not wise to check for this value, but rather, check for
  323. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  324. * code.
  325. */
  326. struct dentry *debugfs_create_u8(const char *name, umode_t mode,
  327. struct dentry *parent, u8 *value)
  328. {
  329. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
  330. &fops_u8_ro, &fops_u8_wo);
  331. }
  332. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  333. static int debugfs_u16_set(void *data, u64 val)
  334. {
  335. *(u16 *)data = val;
  336. return 0;
  337. }
  338. static int debugfs_u16_get(void *data, u64 *val)
  339. {
  340. *val = *(u16 *)data;
  341. return 0;
  342. }
  343. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  344. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  345. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  346. /**
  347. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  348. * @name: a pointer to a string containing the name of the file to create.
  349. * @mode: the permission that the file should have
  350. * @parent: a pointer to the parent dentry for this file. This should be a
  351. * directory dentry if set. If this parameter is %NULL, then the
  352. * file will be created in the root of the debugfs filesystem.
  353. * @value: a pointer to the variable that the file should read to and write
  354. * from.
  355. *
  356. * This function creates a file in debugfs with the given name that
  357. * contains the value of the variable @value. If the @mode variable is so
  358. * set, it can be read from, and written to.
  359. *
  360. * This function will return a pointer to a dentry if it succeeds. This
  361. * pointer must be passed to the debugfs_remove() function when the file is
  362. * to be removed (no automatic cleanup happens if your module is unloaded,
  363. * you are responsible here.) If an error occurs, %NULL will be returned.
  364. *
  365. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  366. * returned. It is not wise to check for this value, but rather, check for
  367. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  368. * code.
  369. */
  370. struct dentry *debugfs_create_u16(const char *name, umode_t mode,
  371. struct dentry *parent, u16 *value)
  372. {
  373. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
  374. &fops_u16_ro, &fops_u16_wo);
  375. }
  376. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  377. static int debugfs_u32_set(void *data, u64 val)
  378. {
  379. *(u32 *)data = val;
  380. return 0;
  381. }
  382. static int debugfs_u32_get(void *data, u64 *val)
  383. {
  384. *val = *(u32 *)data;
  385. return 0;
  386. }
  387. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  388. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  389. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  390. /**
  391. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  392. * @name: a pointer to a string containing the name of the file to create.
  393. * @mode: the permission that the file should have
  394. * @parent: a pointer to the parent dentry for this file. This should be a
  395. * directory dentry if set. If this parameter is %NULL, then the
  396. * file will be created in the root of the debugfs filesystem.
  397. * @value: a pointer to the variable that the file should read to and write
  398. * from.
  399. *
  400. * This function creates a file in debugfs with the given name that
  401. * contains the value of the variable @value. If the @mode variable is so
  402. * set, it can be read from, and written to.
  403. *
  404. * This function will return a pointer to a dentry if it succeeds. This
  405. * pointer must be passed to the debugfs_remove() function when the file is
  406. * to be removed (no automatic cleanup happens if your module is unloaded,
  407. * you are responsible here.) If an error occurs, %NULL will be returned.
  408. *
  409. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  410. * returned. It is not wise to check for this value, but rather, check for
  411. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  412. * code.
  413. */
  414. struct dentry *debugfs_create_u32(const char *name, umode_t mode,
  415. struct dentry *parent, u32 *value)
  416. {
  417. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
  418. &fops_u32_ro, &fops_u32_wo);
  419. }
  420. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  421. static int debugfs_u64_set(void *data, u64 val)
  422. {
  423. *(u64 *)data = val;
  424. return 0;
  425. }
  426. static int debugfs_u64_get(void *data, u64 *val)
  427. {
  428. *val = *(u64 *)data;
  429. return 0;
  430. }
  431. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  432. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  433. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  434. /**
  435. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  436. * @name: a pointer to a string containing the name of the file to create.
  437. * @mode: the permission that the file should have
  438. * @parent: a pointer to the parent dentry for this file. This should be a
  439. * directory dentry if set. If this parameter is %NULL, then the
  440. * file will be created in the root of the debugfs filesystem.
  441. * @value: a pointer to the variable that the file should read to and write
  442. * from.
  443. *
  444. * This function creates a file in debugfs with the given name that
  445. * contains the value of the variable @value. If the @mode variable is so
  446. * set, it can be read from, and written to.
  447. *
  448. * This function will return a pointer to a dentry if it succeeds. This
  449. * pointer must be passed to the debugfs_remove() function when the file is
  450. * to be removed (no automatic cleanup happens if your module is unloaded,
  451. * you are responsible here.) If an error occurs, %NULL will be returned.
  452. *
  453. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  454. * returned. It is not wise to check for this value, but rather, check for
  455. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  456. * code.
  457. */
  458. struct dentry *debugfs_create_u64(const char *name, umode_t mode,
  459. struct dentry *parent, u64 *value)
  460. {
  461. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
  462. &fops_u64_ro, &fops_u64_wo);
  463. }
  464. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  465. static int debugfs_ulong_set(void *data, u64 val)
  466. {
  467. *(unsigned long *)data = val;
  468. return 0;
  469. }
  470. static int debugfs_ulong_get(void *data, u64 *val)
  471. {
  472. *val = *(unsigned long *)data;
  473. return 0;
  474. }
  475. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
  476. "%llu\n");
  477. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
  478. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
  479. /**
  480. * debugfs_create_ulong - create a debugfs file that is used to read and write
  481. * an unsigned long value.
  482. * @name: a pointer to a string containing the name of the file to create.
  483. * @mode: the permission that the file should have
  484. * @parent: a pointer to the parent dentry for this file. This should be a
  485. * directory dentry if set. If this parameter is %NULL, then the
  486. * file will be created in the root of the debugfs filesystem.
  487. * @value: a pointer to the variable that the file should read to and write
  488. * from.
  489. *
  490. * This function creates a file in debugfs with the given name that
  491. * contains the value of the variable @value. If the @mode variable is so
  492. * set, it can be read from, and written to.
  493. *
  494. * This function will return a pointer to a dentry if it succeeds. This
  495. * pointer must be passed to the debugfs_remove() function when the file is
  496. * to be removed (no automatic cleanup happens if your module is unloaded,
  497. * you are responsible here.) If an error occurs, %NULL will be returned.
  498. *
  499. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  500. * returned. It is not wise to check for this value, but rather, check for
  501. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  502. * code.
  503. */
  504. struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
  505. struct dentry *parent, unsigned long *value)
  506. {
  507. return debugfs_create_mode_unsafe(name, mode, parent, value,
  508. &fops_ulong, &fops_ulong_ro,
  509. &fops_ulong_wo);
  510. }
  511. EXPORT_SYMBOL_GPL(debugfs_create_ulong);
  512. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  513. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  514. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  515. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
  516. "0x%04llx\n");
  517. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  518. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  519. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
  520. "0x%08llx\n");
  521. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  522. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  523. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
  524. "0x%016llx\n");
  525. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
  526. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
  527. /*
  528. * 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
  529. *
  530. * These functions are exactly the same as the above functions (but use a hex
  531. * output for the decimal challenged). For details look at the above unsigned
  532. * decimal functions.
  533. */
  534. /**
  535. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  536. * @name: a pointer to a string containing the name of the file to create.
  537. * @mode: the permission that the file should have
  538. * @parent: a pointer to the parent dentry for this file. This should be a
  539. * directory dentry if set. If this parameter is %NULL, then the
  540. * file will be created in the root of the debugfs filesystem.
  541. * @value: a pointer to the variable that the file should read to and write
  542. * from.
  543. */
  544. struct dentry *debugfs_create_x8(const char *name, umode_t mode,
  545. struct dentry *parent, u8 *value)
  546. {
  547. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
  548. &fops_x8_ro, &fops_x8_wo);
  549. }
  550. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  551. /**
  552. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  553. * @name: a pointer to a string containing the name of the file to create.
  554. * @mode: the permission that the file should have
  555. * @parent: a pointer to the parent dentry for this file. This should be a
  556. * directory dentry if set. If this parameter is %NULL, then the
  557. * file will be created in the root of the debugfs filesystem.
  558. * @value: a pointer to the variable that the file should read to and write
  559. * from.
  560. */
  561. struct dentry *debugfs_create_x16(const char *name, umode_t mode,
  562. struct dentry *parent, u16 *value)
  563. {
  564. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
  565. &fops_x16_ro, &fops_x16_wo);
  566. }
  567. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  568. /**
  569. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  570. * @name: a pointer to a string containing the name of the file to create.
  571. * @mode: the permission that the file should have
  572. * @parent: a pointer to the parent dentry for this file. This should be a
  573. * directory dentry if set. If this parameter is %NULL, then the
  574. * file will be created in the root of the debugfs filesystem.
  575. * @value: a pointer to the variable that the file should read to and write
  576. * from.
  577. */
  578. struct dentry *debugfs_create_x32(const char *name, umode_t mode,
  579. struct dentry *parent, u32 *value)
  580. {
  581. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
  582. &fops_x32_ro, &fops_x32_wo);
  583. }
  584. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  585. /**
  586. * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  587. * @name: a pointer to a string containing the name of the file to create.
  588. * @mode: the permission that the file should have
  589. * @parent: a pointer to the parent dentry for this file. This should be a
  590. * directory dentry if set. If this parameter is %NULL, then the
  591. * file will be created in the root of the debugfs filesystem.
  592. * @value: a pointer to the variable that the file should read to and write
  593. * from.
  594. */
  595. struct dentry *debugfs_create_x64(const char *name, umode_t mode,
  596. struct dentry *parent, u64 *value)
  597. {
  598. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
  599. &fops_x64_ro, &fops_x64_wo);
  600. }
  601. EXPORT_SYMBOL_GPL(debugfs_create_x64);
  602. static int debugfs_size_t_set(void *data, u64 val)
  603. {
  604. *(size_t *)data = val;
  605. return 0;
  606. }
  607. static int debugfs_size_t_get(void *data, u64 *val)
  608. {
  609. *val = *(size_t *)data;
  610. return 0;
  611. }
  612. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  613. "%llu\n"); /* %llu and %zu are more or less the same */
  614. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
  615. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
  616. /**
  617. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  618. * @name: a pointer to a string containing the name of the file to create.
  619. * @mode: the permission that the file should have
  620. * @parent: a pointer to the parent dentry for this file. This should be a
  621. * directory dentry if set. If this parameter is %NULL, then the
  622. * file will be created in the root of the debugfs filesystem.
  623. * @value: a pointer to the variable that the file should read to and write
  624. * from.
  625. */
  626. struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
  627. struct dentry *parent, size_t *value)
  628. {
  629. return debugfs_create_mode_unsafe(name, mode, parent, value,
  630. &fops_size_t, &fops_size_t_ro,
  631. &fops_size_t_wo);
  632. }
  633. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  634. static int debugfs_atomic_t_set(void *data, u64 val)
  635. {
  636. atomic_set((atomic_t *)data, val);
  637. return 0;
  638. }
  639. static int debugfs_atomic_t_get(void *data, u64 *val)
  640. {
  641. *val = atomic_read((atomic_t *)data);
  642. return 0;
  643. }
  644. DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  645. debugfs_atomic_t_set, "%lld\n");
  646. DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
  647. "%lld\n");
  648. DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
  649. "%lld\n");
  650. /**
  651. * debugfs_create_atomic_t - create a debugfs file that is used to read and
  652. * write an atomic_t value
  653. * @name: a pointer to a string containing the name of the file to create.
  654. * @mode: the permission that the file should have
  655. * @parent: a pointer to the parent dentry for this file. This should be a
  656. * directory dentry if set. If this parameter is %NULL, then the
  657. * file will be created in the root of the debugfs filesystem.
  658. * @value: a pointer to the variable that the file should read to and write
  659. * from.
  660. */
  661. struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
  662. struct dentry *parent, atomic_t *value)
  663. {
  664. return debugfs_create_mode_unsafe(name, mode, parent, value,
  665. &fops_atomic_t, &fops_atomic_t_ro,
  666. &fops_atomic_t_wo);
  667. }
  668. EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
  669. ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
  670. size_t count, loff_t *ppos)
  671. {
  672. char buf[3];
  673. bool val;
  674. int r, srcu_idx;
  675. r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  676. if (likely(!r))
  677. val = *(bool *)file->private_data;
  678. debugfs_use_file_finish(srcu_idx);
  679. if (r)
  680. return r;
  681. if (val)
  682. buf[0] = 'Y';
  683. else
  684. buf[0] = 'N';
  685. buf[1] = '\n';
  686. buf[2] = 0x00;
  687. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  688. }
  689. EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
  690. ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  691. size_t count, loff_t *ppos)
  692. {
  693. char buf[32];
  694. size_t buf_size;
  695. bool bv;
  696. int r, srcu_idx;
  697. bool *val = file->private_data;
  698. buf_size = min(count, (sizeof(buf)-1));
  699. if (copy_from_user(buf, user_buf, buf_size))
  700. return -EFAULT;
  701. buf[buf_size] = '\0';
  702. if (strtobool(buf, &bv) == 0) {
  703. r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  704. if (likely(!r))
  705. *val = bv;
  706. debugfs_use_file_finish(srcu_idx);
  707. if (r)
  708. return r;
  709. }
  710. return count;
  711. }
  712. EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
  713. static const struct file_operations fops_bool = {
  714. .read = debugfs_read_file_bool,
  715. .write = debugfs_write_file_bool,
  716. .open = simple_open,
  717. .llseek = default_llseek,
  718. };
  719. static const struct file_operations fops_bool_ro = {
  720. .read = debugfs_read_file_bool,
  721. .open = simple_open,
  722. .llseek = default_llseek,
  723. };
  724. static const struct file_operations fops_bool_wo = {
  725. .write = debugfs_write_file_bool,
  726. .open = simple_open,
  727. .llseek = default_llseek,
  728. };
  729. /**
  730. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  731. * @name: a pointer to a string containing the name of the file to create.
  732. * @mode: the permission that the file should have
  733. * @parent: a pointer to the parent dentry for this file. This should be a
  734. * directory dentry if set. If this parameter is %NULL, then the
  735. * file will be created in the root of the debugfs filesystem.
  736. * @value: a pointer to the variable that the file should read to and write
  737. * from.
  738. *
  739. * This function creates a file in debugfs with the given name that
  740. * contains the value of the variable @value. If the @mode variable is so
  741. * set, it can be read from, and written to.
  742. *
  743. * This function will return a pointer to a dentry if it succeeds. This
  744. * pointer must be passed to the debugfs_remove() function when the file is
  745. * to be removed (no automatic cleanup happens if your module is unloaded,
  746. * you are responsible here.) If an error occurs, %NULL will be returned.
  747. *
  748. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  749. * returned. It is not wise to check for this value, but rather, check for
  750. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  751. * code.
  752. */
  753. struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  754. struct dentry *parent, bool *value)
  755. {
  756. return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
  757. &fops_bool_ro, &fops_bool_wo);
  758. }
  759. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  760. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  761. size_t count, loff_t *ppos)
  762. {
  763. struct debugfs_blob_wrapper *blob = file->private_data;
  764. ssize_t r;
  765. int srcu_idx;
  766. r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  767. if (likely(!r))
  768. r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
  769. blob->size);
  770. debugfs_use_file_finish(srcu_idx);
  771. return r;
  772. }
  773. static const struct file_operations fops_blob = {
  774. .read = read_file_blob,
  775. .open = simple_open,
  776. .llseek = default_llseek,
  777. };
  778. /**
  779. * debugfs_create_blob - create a debugfs file that is used to read a binary blob
  780. * @name: a pointer to a string containing the name of the file to create.
  781. * @mode: the permission that the file should have
  782. * @parent: a pointer to the parent dentry for this file. This should be a
  783. * directory dentry if set. If this parameter is %NULL, then the
  784. * file will be created in the root of the debugfs filesystem.
  785. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  786. * to the blob data and the size of the data.
  787. *
  788. * This function creates a file in debugfs with the given name that exports
  789. * @blob->data as a binary blob. If the @mode variable is so set it can be
  790. * read from. Writing is not supported.
  791. *
  792. * This function will return a pointer to a dentry if it succeeds. This
  793. * pointer must be passed to the debugfs_remove() function when the file is
  794. * to be removed (no automatic cleanup happens if your module is unloaded,
  795. * you are responsible here.) If an error occurs, %NULL will be returned.
  796. *
  797. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  798. * returned. It is not wise to check for this value, but rather, check for
  799. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  800. * code.
  801. */
  802. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  803. struct dentry *parent,
  804. struct debugfs_blob_wrapper *blob)
  805. {
  806. return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
  807. }
  808. EXPORT_SYMBOL_GPL(debugfs_create_blob);
  809. struct array_data {
  810. void *array;
  811. u32 elements;
  812. };
  813. static size_t u32_format_array(char *buf, size_t bufsize,
  814. u32 *array, int array_size)
  815. {
  816. size_t ret = 0;
  817. while (--array_size >= 0) {
  818. size_t len;
  819. char term = array_size ? ' ' : '\n';
  820. len = snprintf(buf, bufsize, "%u%c", *array++, term);
  821. ret += len;
  822. buf += len;
  823. bufsize -= len;
  824. }
  825. return ret;
  826. }
  827. static int u32_array_open(struct inode *inode, struct file *file)
  828. {
  829. struct array_data *data = inode->i_private;
  830. int size, elements = data->elements;
  831. char *buf;
  832. /*
  833. * Max size:
  834. * - 10 digits + ' '/'\n' = 11 bytes per number
  835. * - terminating NUL character
  836. */
  837. size = elements*11;
  838. buf = kmalloc(size+1, GFP_KERNEL);
  839. if (!buf)
  840. return -ENOMEM;
  841. buf[size] = 0;
  842. file->private_data = buf;
  843. u32_format_array(buf, size, data->array, data->elements);
  844. return nonseekable_open(inode, file);
  845. }
  846. static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
  847. loff_t *ppos)
  848. {
  849. size_t size = strlen(file->private_data);
  850. return simple_read_from_buffer(buf, len, ppos,
  851. file->private_data, size);
  852. }
  853. static int u32_array_release(struct inode *inode, struct file *file)
  854. {
  855. kfree(file->private_data);
  856. return 0;
  857. }
  858. static const struct file_operations u32_array_fops = {
  859. .owner = THIS_MODULE,
  860. .open = u32_array_open,
  861. .release = u32_array_release,
  862. .read = u32_array_read,
  863. .llseek = no_llseek,
  864. };
  865. /**
  866. * debugfs_create_u32_array - create a debugfs file that is used to read u32
  867. * array.
  868. * @name: a pointer to a string containing the name of the file to create.
  869. * @mode: the permission that the file should have.
  870. * @parent: a pointer to the parent dentry for this file. This should be a
  871. * directory dentry if set. If this parameter is %NULL, then the
  872. * file will be created in the root of the debugfs filesystem.
  873. * @array: u32 array that provides data.
  874. * @elements: total number of elements in the array.
  875. *
  876. * This function creates a file in debugfs with the given name that exports
  877. * @array as data. If the @mode variable is so set it can be read from.
  878. * Writing is not supported. Seek within the file is also not supported.
  879. * Once array is created its size can not be changed.
  880. *
  881. * The function returns a pointer to dentry on success. If debugfs is not
  882. * enabled in the kernel, the value -%ENODEV will be returned.
  883. */
  884. struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
  885. struct dentry *parent,
  886. u32 *array, u32 elements)
  887. {
  888. struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
  889. if (data == NULL)
  890. return NULL;
  891. data->array = array;
  892. data->elements = elements;
  893. return debugfs_create_file_unsafe(name, mode, parent, data,
  894. &u32_array_fops);
  895. }
  896. EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
  897. #ifdef CONFIG_HAS_IOMEM
  898. /*
  899. * The regset32 stuff is used to print 32-bit registers using the
  900. * seq_file utilities. We offer printing a register set in an already-opened
  901. * sequential file or create a debugfs file that only prints a regset32.
  902. */
  903. /**
  904. * debugfs_print_regs32 - use seq_print to describe a set of registers
  905. * @s: the seq_file structure being used to generate output
  906. * @regs: an array if struct debugfs_reg32 structures
  907. * @nregs: the length of the above array
  908. * @base: the base address to be used in reading the registers
  909. * @prefix: a string to be prefixed to every output line
  910. *
  911. * This function outputs a text block describing the current values of
  912. * some 32-bit hardware registers. It is meant to be used within debugfs
  913. * files based on seq_file that need to show registers, intermixed with other
  914. * information. The prefix argument may be used to specify a leading string,
  915. * because some peripherals have several blocks of identical registers,
  916. * for example configuration of dma channels
  917. */
  918. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  919. int nregs, void __iomem *base, char *prefix)
  920. {
  921. int i;
  922. for (i = 0; i < nregs; i++, regs++) {
  923. if (prefix)
  924. seq_printf(s, "%s", prefix);
  925. seq_printf(s, "%s = 0x%08x\n", regs->name,
  926. readl(base + regs->offset));
  927. if (seq_has_overflowed(s))
  928. break;
  929. }
  930. }
  931. EXPORT_SYMBOL_GPL(debugfs_print_regs32);
  932. static int debugfs_show_regset32(struct seq_file *s, void *data)
  933. {
  934. struct debugfs_regset32 *regset = s->private;
  935. debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
  936. return 0;
  937. }
  938. static int debugfs_open_regset32(struct inode *inode, struct file *file)
  939. {
  940. return single_open(file, debugfs_show_regset32, inode->i_private);
  941. }
  942. static const struct file_operations fops_regset32 = {
  943. .open = debugfs_open_regset32,
  944. .read = seq_read,
  945. .llseek = seq_lseek,
  946. .release = single_release,
  947. };
  948. /**
  949. * debugfs_create_regset32 - create a debugfs file that returns register values
  950. * @name: a pointer to a string containing the name of the file to create.
  951. * @mode: the permission that the file should have
  952. * @parent: a pointer to the parent dentry for this file. This should be a
  953. * directory dentry if set. If this parameter is %NULL, then the
  954. * file will be created in the root of the debugfs filesystem.
  955. * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
  956. * to an array of register definitions, the array size and the base
  957. * address where the register bank is to be found.
  958. *
  959. * This function creates a file in debugfs with the given name that reports
  960. * the names and values of a set of 32-bit registers. If the @mode variable
  961. * is so set it can be read from. Writing is not supported.
  962. *
  963. * This function will return a pointer to a dentry if it succeeds. This
  964. * pointer must be passed to the debugfs_remove() function when the file is
  965. * to be removed (no automatic cleanup happens if your module is unloaded,
  966. * you are responsible here.) If an error occurs, %NULL will be returned.
  967. *
  968. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  969. * returned. It is not wise to check for this value, but rather, check for
  970. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  971. * code.
  972. */
  973. struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
  974. struct dentry *parent,
  975. struct debugfs_regset32 *regset)
  976. {
  977. return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
  978. }
  979. EXPORT_SYMBOL_GPL(debugfs_create_regset32);
  980. #endif /* CONFIG_HAS_IOMEM */
  981. struct debugfs_devm_entry {
  982. int (*read)(struct seq_file *seq, void *data);
  983. struct device *dev;
  984. };
  985. static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
  986. {
  987. struct debugfs_devm_entry *entry = inode->i_private;
  988. return single_open(f, entry->read, entry->dev);
  989. }
  990. static const struct file_operations debugfs_devm_entry_ops = {
  991. .owner = THIS_MODULE,
  992. .open = debugfs_devm_entry_open,
  993. .release = single_release,
  994. .read = seq_read,
  995. .llseek = seq_lseek
  996. };
  997. /**
  998. * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
  999. *
  1000. * @dev: device related to this debugfs file.
  1001. * @name: name of the debugfs file.
  1002. * @parent: a pointer to the parent dentry for this file. This should be a
  1003. * directory dentry if set. If this parameter is %NULL, then the
  1004. * file will be created in the root of the debugfs filesystem.
  1005. * @read_fn: function pointer called to print the seq_file content.
  1006. */
  1007. struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
  1008. struct dentry *parent,
  1009. int (*read_fn)(struct seq_file *s,
  1010. void *data))
  1011. {
  1012. struct debugfs_devm_entry *entry;
  1013. if (IS_ERR(parent))
  1014. return ERR_PTR(-ENOENT);
  1015. entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
  1016. if (!entry)
  1017. return ERR_PTR(-ENOMEM);
  1018. entry->read = read_fn;
  1019. entry->dev = dev;
  1020. return debugfs_create_file(name, S_IRUGO, parent, entry,
  1021. &debugfs_devm_entry_ops);
  1022. }
  1023. EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);