file.c 38 KB

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