file.c 38 KB

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