file.c 39 KB

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