file.c 39 KB

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