xref: /linux-6.15/fs/debugfs/file.c (revision d616f56d)
13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
9e1b4fc7aSMauro Carvalho Chehab  *  See Documentation/filesystems/ for more details.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141a087c6aSAlessandro Rubini #include <linux/seq_file.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/debugfs.h>
1703e099fbSAlessandro Rubini #include <linux/io.h>
189fe2a701SSrivatsa Vaddagiri #include <linux/slab.h>
193a76e5e0SSeth Jennings #include <linux/atomic.h>
2098210b7fSArend van Spriel #include <linux/device.h>
2130332eeeSGeert Uytterhoeven #include <linux/pm_runtime.h>
22cfe39442SAl Viro #include <linux/poll.h>
235496197fSDavid Howells #include <linux/security.h>
249fd4dcecSNicolai Stange 
259fd4dcecSNicolai Stange #include "internal.h"
261da177e4SLinus Torvalds 
2749d200deSNicolai Stange struct poll_table_struct;
2849d200deSNicolai Stange 
291da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
301da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
361da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	return count;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
419fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
421da177e4SLinus Torvalds 	.read =		default_read_file,
431da177e4SLinus Torvalds 	.write =	default_write_file,
44234e3405SStephen Boyd 	.open =		simple_open,
456038f373SArnd Bergmann 	.llseek =	noop_llseek,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
489fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
499fd4dcecSNicolai Stange 
507c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
517c8d4698SNicolai Stange {
527c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
53055ab8e3SNicolai Stange 
547d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
557d39bc50SNicolai Stange 		/*
567d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
577d39bc50SNicolai Stange 		 * debugfs_file_get().
587d39bc50SNicolai Stange 		 */
597d39bc50SNicolai Stange 		WARN_ON(1);
607d39bc50SNicolai Stange 		return NULL;
617d39bc50SNicolai Stange 	}
627d39bc50SNicolai Stange 
637c8d4698SNicolai Stange 	return fsd->real_fops;
647c8d4698SNicolai Stange }
657c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
667c8d4698SNicolai Stange 
67e9117a5aSNicolai Stange /**
68e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
69e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
70e9117a5aSNicolai Stange  *
71e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
72e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
73e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
74e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
75e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
76e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
77e9117a5aSNicolai Stange  *
78e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
79e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
80e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
81e9117a5aSNicolai Stange  */
82e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
83e9117a5aSNicolai Stange {
847d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
857d39bc50SNicolai Stange 	void *d_fsd;
86e9117a5aSNicolai Stange 
877d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
887d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
897d39bc50SNicolai Stange 		fsd = d_fsd;
907d39bc50SNicolai Stange 	} else {
917d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
927d39bc50SNicolai Stange 		if (!fsd)
937d39bc50SNicolai Stange 			return -ENOMEM;
947d39bc50SNicolai Stange 
957d39bc50SNicolai Stange 		fsd->real_fops = (void *)((unsigned long)d_fsd &
967d39bc50SNicolai Stange 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
977d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
987d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
997d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
1007d39bc50SNicolai Stange 			kfree(fsd);
1017d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
1027d39bc50SNicolai Stange 		}
1037d39bc50SNicolai Stange 	}
1047d39bc50SNicolai Stange 
1057d39bc50SNicolai Stange 	/*
1067d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1077d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1087d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1097d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1107d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1117d39bc50SNicolai Stange 	 * removers.
1127d39bc50SNicolai Stange 	 */
113e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
114e9117a5aSNicolai Stange 		return -EIO;
115e9117a5aSNicolai Stange 
116e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
117e9117a5aSNicolai Stange 		return -EIO;
118e9117a5aSNicolai Stange 
119e9117a5aSNicolai Stange 	return 0;
120e9117a5aSNicolai Stange }
121e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
122e9117a5aSNicolai Stange 
123e9117a5aSNicolai Stange /**
124e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
125e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
126e9117a5aSNicolai Stange  *          debugfs_file_get().
127e9117a5aSNicolai Stange  *
128e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
129e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
130e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
131e9117a5aSNicolai Stange  */
132e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
133e9117a5aSNicolai Stange {
1347d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
135e9117a5aSNicolai Stange 
136e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
137e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
138e9117a5aSNicolai Stange }
139e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
140e9117a5aSNicolai Stange 
1415496197fSDavid Howells /*
1425496197fSDavid Howells  * Only permit access to world-readable files when the kernel is locked down.
1435496197fSDavid Howells  * We also need to exclude any file that has ways to write or alter it as root
1445496197fSDavid Howells  * can bypass the permissions check.
1455496197fSDavid Howells  */
146a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode,
1475496197fSDavid Howells 			       struct file *filp,
1485496197fSDavid Howells 			       const struct file_operations *real_fops)
1495496197fSDavid Howells {
1505496197fSDavid Howells 	if ((inode->i_mode & 07777) == 0444 &&
1515496197fSDavid Howells 	    !(filp->f_mode & FMODE_WRITE) &&
1525496197fSDavid Howells 	    !real_fops->unlocked_ioctl &&
1535496197fSDavid Howells 	    !real_fops->compat_ioctl &&
1545496197fSDavid Howells 	    !real_fops->mmap)
155a37f4958SEric Snowberg 		return 0;
1565496197fSDavid Howells 
157a37f4958SEric Snowberg 	if (security_locked_down(LOCKDOWN_DEBUGFS))
158a37f4958SEric Snowberg 		return -EPERM;
159a37f4958SEric Snowberg 
160a37f4958SEric Snowberg 	return 0;
1615496197fSDavid Howells }
1625496197fSDavid Howells 
1639fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1649fd4dcecSNicolai Stange {
16569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
1669fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
1677d39bc50SNicolai Stange 	int r;
1689fd4dcecSNicolai Stange 
1697d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
1707d39bc50SNicolai Stange 	if (r)
1717d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
1729fd4dcecSNicolai Stange 
17386f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
1745496197fSDavid Howells 
175a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
1765496197fSDavid Howells 	if (r)
1775496197fSDavid Howells 		goto out;
1785496197fSDavid Howells 
179275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
180e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
181275678e7STaehee Yoo 		if (real_fops->owner &&
182275678e7STaehee Yoo 		    real_fops->owner->state == MODULE_STATE_GOING)
183275678e7STaehee Yoo 			goto out;
184275678e7STaehee Yoo #endif
185275678e7STaehee Yoo 
1869fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1879fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1889fd4dcecSNicolai Stange 			dentry);
1899fd4dcecSNicolai Stange 		r = -ENXIO;
1909fd4dcecSNicolai Stange 		goto out;
1919fd4dcecSNicolai Stange 	}
1929fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1939fd4dcecSNicolai Stange 
1949fd4dcecSNicolai Stange 	if (real_fops->open)
1959fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1969fd4dcecSNicolai Stange 
1979fd4dcecSNicolai Stange out:
19869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
1999fd4dcecSNicolai Stange 	return r;
2009fd4dcecSNicolai Stange }
2019fd4dcecSNicolai Stange 
2029fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
2039fd4dcecSNicolai Stange 	.open = open_proxy_open,
2049fd4dcecSNicolai Stange };
2059fd4dcecSNicolai Stange 
20649d200deSNicolai Stange #define PROTO(args...) args
20749d200deSNicolai Stange #define ARGS(args...) args
20849d200deSNicolai Stange 
20949d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
21049d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
21149d200deSNicolai Stange {									\
21269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
213154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
21449d200deSNicolai Stange 	ret_type r;							\
21549d200deSNicolai Stange 									\
21669d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
21769d29f9eSNicolai Stange 	if (unlikely(r))						\
21869d29f9eSNicolai Stange 		return r;						\
219154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
22049d200deSNicolai Stange 	r = real_fops->name(args);					\
22169d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
22249d200deSNicolai Stange 	return r;							\
22349d200deSNicolai Stange }
22449d200deSNicolai Stange 
22549d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
22649d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
22749d200deSNicolai Stange 		ARGS(filp, offset, whence));
22849d200deSNicolai Stange 
22949d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
23049d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
23149d200deSNicolai Stange 			loff_t *ppos),
23249d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
23349d200deSNicolai Stange 
23449d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
23549d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
23649d200deSNicolai Stange 			loff_t *ppos),
23749d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
23849d200deSNicolai Stange 
23949d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
24049d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
24149d200deSNicolai Stange 		ARGS(filp, cmd, arg));
24249d200deSNicolai Stange 
243076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
24449d200deSNicolai Stange 				struct poll_table_struct *wait)
24549d200deSNicolai Stange {
24669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
247e6c8adcaSAl Viro 	__poll_t r = 0;
248154b9d75SNicolai Stange 	const struct file_operations *real_fops;
24949d200deSNicolai Stange 
25069d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
251a9a08845SLinus Torvalds 		return EPOLLHUP;
25249d200deSNicolai Stange 
253154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
25449d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
25569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
25649d200deSNicolai Stange 	return r;
25749d200deSNicolai Stange }
25849d200deSNicolai Stange 
25949d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
26049d200deSNicolai Stange {
26149d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
26286f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
26349d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
26449d200deSNicolai Stange 	int r = 0;
26549d200deSNicolai Stange 
26649d200deSNicolai Stange 	/*
26749d200deSNicolai Stange 	 * We must not protect this against removal races here: the
26849d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
26949d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
27049d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
27149d200deSNicolai Stange 	 */
27249d200deSNicolai Stange 	if (real_fops->release)
27349d200deSNicolai Stange 		r = real_fops->release(inode, filp);
27449d200deSNicolai Stange 
27549d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
276c80a67bdSXu Wang 	kfree(proxy_fops);
27749d200deSNicolai Stange 	fops_put(real_fops);
278a1a9e5d2SEric Engestrom 	return r;
27949d200deSNicolai Stange }
28049d200deSNicolai Stange 
28149d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
28249d200deSNicolai Stange 				const struct file_operations *real_fops)
28349d200deSNicolai Stange {
28449d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
28549d200deSNicolai Stange 	if (real_fops->llseek)
28649d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
28749d200deSNicolai Stange 	if (real_fops->read)
28849d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
28949d200deSNicolai Stange 	if (real_fops->write)
29049d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
29149d200deSNicolai Stange 	if (real_fops->poll)
29249d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
29349d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
29449d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
29549d200deSNicolai Stange }
29649d200deSNicolai Stange 
29749d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
29849d200deSNicolai Stange {
29969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
30049d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
30149d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
3027d39bc50SNicolai Stange 	int r;
30349d200deSNicolai Stange 
3047d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
3057d39bc50SNicolai Stange 	if (r)
3067d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
30749d200deSNicolai Stange 
30886f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
3095496197fSDavid Howells 
310a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
3115496197fSDavid Howells 	if (r)
3125496197fSDavid Howells 		goto out;
3135496197fSDavid Howells 
314275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
315e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
316275678e7STaehee Yoo 		if (real_fops->owner &&
317275678e7STaehee Yoo 		    real_fops->owner->state == MODULE_STATE_GOING)
318275678e7STaehee Yoo 			goto out;
319275678e7STaehee Yoo #endif
320275678e7STaehee Yoo 
32149d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
32249d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
32349d200deSNicolai Stange 			dentry);
32449d200deSNicolai Stange 		r = -ENXIO;
32549d200deSNicolai Stange 		goto out;
32649d200deSNicolai Stange 	}
32749d200deSNicolai Stange 
32849d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
32949d200deSNicolai Stange 	if (!proxy_fops) {
33049d200deSNicolai Stange 		r = -ENOMEM;
33149d200deSNicolai Stange 		goto free_proxy;
33249d200deSNicolai Stange 	}
33349d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
33449d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
33549d200deSNicolai Stange 
33649d200deSNicolai Stange 	if (real_fops->open) {
33749d200deSNicolai Stange 		r = real_fops->open(inode, filp);
338b10e3e90SNicolai Stange 		if (r) {
339b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
340b10e3e90SNicolai Stange 			goto free_proxy;
341b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
34249d200deSNicolai Stange 			/* No protection against file removal anymore. */
34349d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
34449d200deSNicolai Stange 				dentry);
34549d200deSNicolai Stange 			goto free_proxy;
34649d200deSNicolai Stange 		}
34749d200deSNicolai Stange 	}
34849d200deSNicolai Stange 
34949d200deSNicolai Stange 	goto out;
35049d200deSNicolai Stange free_proxy:
35149d200deSNicolai Stange 	kfree(proxy_fops);
35249d200deSNicolai Stange 	fops_put(real_fops);
35349d200deSNicolai Stange out:
35469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
35549d200deSNicolai Stange 	return r;
35649d200deSNicolai Stange }
35749d200deSNicolai Stange 
35849d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
35949d200deSNicolai Stange 	.open = full_proxy_open,
36049d200deSNicolai Stange };
36149d200deSNicolai Stange 
362c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
363c6468808SNicolai Stange 			size_t len, loff_t *ppos)
364c6468808SNicolai Stange {
36569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
366c6468808SNicolai Stange 	ssize_t ret;
367c6468808SNicolai Stange 
36869d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
36969d29f9eSNicolai Stange 	if (unlikely(ret))
37069d29f9eSNicolai Stange 		return ret;
371c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
37269d29f9eSNicolai Stange 	debugfs_file_put(dentry);
373c6468808SNicolai Stange 	return ret;
374c6468808SNicolai Stange }
375c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
376c6468808SNicolai Stange 
377c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
378c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
379c6468808SNicolai Stange {
38069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
381c6468808SNicolai Stange 	ssize_t ret;
382c6468808SNicolai Stange 
38369d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
38469d29f9eSNicolai Stange 	if (unlikely(ret))
38569d29f9eSNicolai Stange 		return ret;
386c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
38769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
388c6468808SNicolai Stange 	return ret;
389c6468808SNicolai Stange }
390c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
391c6468808SNicolai Stange 
3924909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3934909f168SNicolai Stange 					struct dentry *parent, void *value,
3944909f168SNicolai Stange 					const struct file_operations *fops,
3954909f168SNicolai Stange 					const struct file_operations *fops_ro,
3964909f168SNicolai Stange 					const struct file_operations *fops_wo)
3974909f168SNicolai Stange {
3984909f168SNicolai Stange 	/* if there are no write bits set, make read only */
3994909f168SNicolai Stange 	if (!(mode & S_IWUGO))
4004909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
4014909f168SNicolai Stange 						fops_ro);
4024909f168SNicolai Stange 	/* if there are no read bits set, make write only */
4034909f168SNicolai Stange 	if (!(mode & S_IRUGO))
4044909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
4054909f168SNicolai Stange 						fops_wo);
4064909f168SNicolai Stange 
4074909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
4084909f168SNicolai Stange }
4094909f168SNicolai Stange 
4108b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
411acaefc25SArnd Bergmann {
412acaefc25SArnd Bergmann 	*(u8 *)data = val;
4138b88b099SChristoph Hellwig 	return 0;
414acaefc25SArnd Bergmann }
4158b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
416acaefc25SArnd Bergmann {
4178b88b099SChristoph Hellwig 	*val = *(u8 *)data;
4188b88b099SChristoph Hellwig 	return 0;
419acaefc25SArnd Bergmann }
4204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
4214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
4224909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /**
4256468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
4261da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4271da177e4SLinus Torvalds  * @mode: the permission that the file should have
4281da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4296468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4301da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4311da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4321da177e4SLinus Torvalds  *         from.
4331da177e4SLinus Torvalds  *
4341da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4351da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4361da177e4SLinus Torvalds  * set, it can be read from, and written to.
4371da177e4SLinus Torvalds  */
4389655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
4399655ac4aSGreg Kroah-Hartman 		       u8 *value)
4401da177e4SLinus Torvalds {
4419655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
442b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
4451da177e4SLinus Torvalds 
4468b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
447acaefc25SArnd Bergmann {
448acaefc25SArnd Bergmann 	*(u16 *)data = val;
4498b88b099SChristoph Hellwig 	return 0;
450acaefc25SArnd Bergmann }
4518b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
452acaefc25SArnd Bergmann {
4538b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4548b88b099SChristoph Hellwig 	return 0;
455acaefc25SArnd Bergmann }
4564909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
4574909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
4584909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
459acaefc25SArnd Bergmann 
4601da177e4SLinus Torvalds /**
4616468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4621da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4631da177e4SLinus Torvalds  * @mode: the permission that the file should have
4641da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4656468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4661da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4671da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4681da177e4SLinus Torvalds  *         from.
4691da177e4SLinus Torvalds  *
4701da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4711da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4721da177e4SLinus Torvalds  * set, it can be read from, and written to.
4731da177e4SLinus Torvalds  */
474313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
475313f5dbbSGreg Kroah-Hartman 			u16 *value)
4761da177e4SLinus Torvalds {
477313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
478b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4811da177e4SLinus Torvalds 
4828b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
483acaefc25SArnd Bergmann {
484acaefc25SArnd Bergmann 	*(u32 *)data = val;
4858b88b099SChristoph Hellwig 	return 0;
486acaefc25SArnd Bergmann }
4878b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
488acaefc25SArnd Bergmann {
4898b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4908b88b099SChristoph Hellwig 	return 0;
491acaefc25SArnd Bergmann }
4924909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4934909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4944909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
495acaefc25SArnd Bergmann 
4961da177e4SLinus Torvalds /**
4976468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4981da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4991da177e4SLinus Torvalds  * @mode: the permission that the file should have
5001da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
5016468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
5021da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
5031da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
5041da177e4SLinus Torvalds  *         from.
5051da177e4SLinus Torvalds  *
5061da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
5071da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
5081da177e4SLinus Torvalds  * set, it can be read from, and written to.
5091da177e4SLinus Torvalds  */
5102b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
5112b07021aSGreg Kroah-Hartman 			u32 *value)
5121da177e4SLinus Torvalds {
5132b07021aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
514b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
5171da177e4SLinus Torvalds 
5188b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
5198447891fSMichael Ellerman {
5208447891fSMichael Ellerman 	*(u64 *)data = val;
5218b88b099SChristoph Hellwig 	return 0;
5228447891fSMichael Ellerman }
5238447891fSMichael Ellerman 
5248b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
5258447891fSMichael Ellerman {
5268b88b099SChristoph Hellwig 	*val = *(u64 *)data;
5278b88b099SChristoph Hellwig 	return 0;
5288447891fSMichael Ellerman }
5294909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
5304909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
5314909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5328447891fSMichael Ellerman 
5338447891fSMichael Ellerman /**
5348447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5358447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5368447891fSMichael Ellerman  * @mode: the permission that the file should have
5378447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5388447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5398447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5408447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5418447891fSMichael Ellerman  *         from.
5428447891fSMichael Ellerman  *
5438447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5448447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5458447891fSMichael Ellerman  * set, it can be read from, and written to.
5468447891fSMichael Ellerman  */
547ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
548ad26221fSGreg Kroah-Hartman 			u64 *value)
5498447891fSMichael Ellerman {
550ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
551b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5528447891fSMichael Ellerman }
5538447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5548447891fSMichael Ellerman 
555c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
556c23fe831SViresh Kumar {
557c23fe831SViresh Kumar 	*(unsigned long *)data = val;
558c23fe831SViresh Kumar 	return 0;
559c23fe831SViresh Kumar }
560c23fe831SViresh Kumar 
561c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
562c23fe831SViresh Kumar {
563c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
564c23fe831SViresh Kumar 	return 0;
565c23fe831SViresh Kumar }
5664909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5674909f168SNicolai Stange 			"%llu\n");
5684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5694909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
570c23fe831SViresh Kumar 
571c23fe831SViresh Kumar /**
572c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
573c23fe831SViresh Kumar  * an unsigned long value.
574c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
575c23fe831SViresh Kumar  * @mode: the permission that the file should have
576c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
577c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
578c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
579c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
580c23fe831SViresh Kumar  *         from.
581c23fe831SViresh Kumar  *
582c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
583c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
584c23fe831SViresh Kumar  * set, it can be read from, and written to.
585c23fe831SViresh Kumar  *
586c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
587c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
588c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
589adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
5909abb2499SRonald Tschalär  * returned.
591c23fe831SViresh Kumar  *
592adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
5939abb2499SRonald Tschalär  * be returned.
594c23fe831SViresh Kumar  */
595c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
596c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
597c23fe831SViresh Kumar {
5984909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
5994909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
6004909f168SNicolai Stange 					&fops_ulong_wo);
601c23fe831SViresh Kumar }
602c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
603c23fe831SViresh Kumar 
6044909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
6072ebefc50SRobin Getz 
6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
6094909f168SNicolai Stange 			"0x%04llx\n");
6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
6114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
6122ebefc50SRobin Getz 
6134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
6144909f168SNicolai Stange 			"0x%08llx\n");
6154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
6164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
6172ebefc50SRobin Getz 
6184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
6194909f168SNicolai Stange 			"0x%016llx\n");
6204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
6214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
62215b0beaaSHuang Ying 
623e6716b87SRandy Dunlap /*
62415b0beaaSHuang Ying  * 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
625e6716b87SRandy Dunlap  *
626e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
627e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
628e6716b87SRandy Dunlap  * decimal functions.
629e6716b87SRandy Dunlap  */
630e6716b87SRandy Dunlap 
6312ebefc50SRobin Getz /**
6322ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
633e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
634e6716b87SRandy Dunlap  * @mode: the permission that the file should have
635e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
636e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
637e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
638e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
639e6716b87SRandy Dunlap  *         from.
6402ebefc50SRobin Getz  */
641c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
642c7c11689SGreg Kroah-Hartman 		       u8 *value)
6432ebefc50SRobin Getz {
644c7c11689SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
645b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6462ebefc50SRobin Getz }
6472ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6482ebefc50SRobin Getz 
649e6716b87SRandy Dunlap /**
650e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
651e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
652e6716b87SRandy Dunlap  * @mode: the permission that the file should have
653e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
654e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
655e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
656e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
657e6716b87SRandy Dunlap  *         from.
658e6716b87SRandy Dunlap  */
659e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
660e40d38f2SGreg Kroah-Hartman 			u16 *value)
6612ebefc50SRobin Getz {
662e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
663b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6642ebefc50SRobin Getz }
6652ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6662ebefc50SRobin Getz 
667e6716b87SRandy Dunlap /**
668e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
669e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
670e6716b87SRandy Dunlap  * @mode: the permission that the file should have
671e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
672e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
673e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
674e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
675e6716b87SRandy Dunlap  *         from.
676e6716b87SRandy Dunlap  */
677f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
678f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
6792ebefc50SRobin Getz {
680f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
681b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6822ebefc50SRobin Getz }
6832ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6842ebefc50SRobin Getz 
68515b0beaaSHuang Ying /**
68615b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
68715b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
68815b0beaaSHuang Ying  * @mode: the permission that the file should have
68915b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
69015b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
69115b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
69215b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
69315b0beaaSHuang Ying  *         from.
69415b0beaaSHuang Ying  */
6950864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
6960864c408SGreg Kroah-Hartman 			u64 *value)
69715b0beaaSHuang Ying {
6980864c408SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
69982b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
70015b0beaaSHuang Ying }
70115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
70215b0beaaSHuang Ying 
7035e078787SInaky Perez-Gonzalez 
7045e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
7055e078787SInaky Perez-Gonzalez {
7065e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
7075e078787SInaky Perez-Gonzalez 	return 0;
7085e078787SInaky Perez-Gonzalez }
7095e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
7105e078787SInaky Perez-Gonzalez {
7115e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
7125e078787SInaky Perez-Gonzalez 	return 0;
7135e078787SInaky Perez-Gonzalez }
7144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
7155e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
7164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
7174909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
7185e078787SInaky Perez-Gonzalez 
7195e078787SInaky Perez-Gonzalez /**
7205e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7215e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7225e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7235e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7245e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7255e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7265e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7275e078787SInaky Perez-Gonzalez  *         from.
7285e078787SInaky Perez-Gonzalez  */
7298e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
7305e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
7315e078787SInaky Perez-Gonzalez {
7328e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
7338e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
7345e078787SInaky Perez-Gonzalez }
7355e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7365e078787SInaky Perez-Gonzalez 
7373a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7383a76e5e0SSeth Jennings {
7393a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7403a76e5e0SSeth Jennings 	return 0;
7413a76e5e0SSeth Jennings }
7423a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7433a76e5e0SSeth Jennings {
7443a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7453a76e5e0SSeth Jennings 	return 0;
7463a76e5e0SSeth Jennings }
7474909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7483a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7504909f168SNicolai Stange 			"%lld\n");
7514909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7524909f168SNicolai Stange 			"%lld\n");
7533a76e5e0SSeth Jennings 
7543a76e5e0SSeth Jennings /**
7553a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7563a76e5e0SSeth Jennings  * write an atomic_t value
7573a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7583a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7593a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7603a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7613a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7623a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7633a76e5e0SSeth Jennings  *         from.
7643a76e5e0SSeth Jennings  */
7659927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode,
7663a76e5e0SSeth Jennings 			     struct dentry *parent, atomic_t *value)
7673a76e5e0SSeth Jennings {
7689927c6faSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
7699927c6faSGreg Kroah-Hartman 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
7703a76e5e0SSeth Jennings }
7713a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7725e078787SInaky Perez-Gonzalez 
7730642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7741da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7751da177e4SLinus Torvalds {
776c8a9c285SRasmus Villemoes 	char buf[2];
7774d45f797SNicolai Stange 	bool val;
77869d29f9eSNicolai Stange 	int r;
77969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7801da177e4SLinus Torvalds 
78169d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
78269d29f9eSNicolai Stange 	if (unlikely(r))
7834d45f797SNicolai Stange 		return r;
78469d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
78569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7864d45f797SNicolai Stange 
7874d45f797SNicolai Stange 	if (val)
7881da177e4SLinus Torvalds 		buf[0] = 'Y';
7891da177e4SLinus Torvalds 	else
7901da177e4SLinus Torvalds 		buf[0] = 'N';
7911da177e4SLinus Torvalds 	buf[1] = '\n';
7921da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7931da177e4SLinus Torvalds }
7940642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7951da177e4SLinus Torvalds 
7960642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7971da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7981da177e4SLinus Torvalds {
7998705b48eSJonathan Cameron 	bool bv;
80069d29f9eSNicolai Stange 	int r;
801621a5f7aSViresh Kumar 	bool *val = file->private_data;
80269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
8031da177e4SLinus Torvalds 
804964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
805964f8363SAndy Shevchenko 	if (!r) {
80669d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
80769d29f9eSNicolai Stange 		if (unlikely(r))
8084d45f797SNicolai Stange 			return r;
80969d29f9eSNicolai Stange 		*val = bv;
81069d29f9eSNicolai Stange 		debugfs_file_put(dentry);
8114d45f797SNicolai Stange 	}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	return count;
8141da177e4SLinus Torvalds }
8150642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
8161da177e4SLinus Torvalds 
8174b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
8180642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
8190642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
820234e3405SStephen Boyd 	.open =		simple_open,
8216038f373SArnd Bergmann 	.llseek =	default_llseek,
8221da177e4SLinus Torvalds };
8231da177e4SLinus Torvalds 
8246713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
8256713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8266713e8fbSStephen Boyd 	.open =		simple_open,
8276713e8fbSStephen Boyd 	.llseek =	default_llseek,
8286713e8fbSStephen Boyd };
8296713e8fbSStephen Boyd 
8306713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8316713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8326713e8fbSStephen Boyd 	.open =		simple_open,
8336713e8fbSStephen Boyd 	.llseek =	default_llseek,
8346713e8fbSStephen Boyd };
8356713e8fbSStephen Boyd 
8361da177e4SLinus Torvalds /**
8376468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8381da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8391da177e4SLinus Torvalds  * @mode: the permission that the file should have
8401da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8416468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8421da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8431da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8441da177e4SLinus Torvalds  *         from.
8451da177e4SLinus Torvalds  *
8461da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8471da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8481da177e4SLinus Torvalds  * set, it can be read from, and written to.
8491da177e4SLinus Torvalds  *
8501da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8511da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8521da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
853adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
8549abb2499SRonald Tschalär  * returned.
8551da177e4SLinus Torvalds  *
856adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
8579abb2499SRonald Tschalär  * be returned.
8581da177e4SLinus Torvalds  */
859f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
860621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8611da177e4SLinus Torvalds {
8624d45f797SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8636713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8661da177e4SLinus Torvalds 
8679af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
8689af0440eSPeter Zijlstra 			      size_t count, loff_t *ppos)
8699af0440eSPeter Zijlstra {
8709af0440eSPeter Zijlstra 	struct dentry *dentry = F_DENTRY(file);
8719af0440eSPeter Zijlstra 	char *str, *copy = NULL;
8729af0440eSPeter Zijlstra 	int copy_len, len;
8739af0440eSPeter Zijlstra 	ssize_t ret;
8749af0440eSPeter Zijlstra 
8759af0440eSPeter Zijlstra 	ret = debugfs_file_get(dentry);
8769af0440eSPeter Zijlstra 	if (unlikely(ret))
8779af0440eSPeter Zijlstra 		return ret;
8789af0440eSPeter Zijlstra 
8799af0440eSPeter Zijlstra 	str = *(char **)file->private_data;
8809af0440eSPeter Zijlstra 	len = strlen(str) + 1;
8819af0440eSPeter Zijlstra 	copy = kmalloc(len, GFP_KERNEL);
8829af0440eSPeter Zijlstra 	if (!copy) {
8839af0440eSPeter Zijlstra 		debugfs_file_put(dentry);
8849af0440eSPeter Zijlstra 		return -ENOMEM;
8859af0440eSPeter Zijlstra 	}
8869af0440eSPeter Zijlstra 
8879af0440eSPeter Zijlstra 	copy_len = strscpy(copy, str, len);
8889af0440eSPeter Zijlstra 	debugfs_file_put(dentry);
8899af0440eSPeter Zijlstra 	if (copy_len < 0) {
8909af0440eSPeter Zijlstra 		kfree(copy);
8919af0440eSPeter Zijlstra 		return copy_len;
8929af0440eSPeter Zijlstra 	}
8939af0440eSPeter Zijlstra 
8949af0440eSPeter Zijlstra 	copy[copy_len] = '\n';
8959af0440eSPeter Zijlstra 
8969af0440eSPeter Zijlstra 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, copy_len);
8979af0440eSPeter Zijlstra 	kfree(copy);
8989af0440eSPeter Zijlstra 
8999af0440eSPeter Zijlstra 	return ret;
9009af0440eSPeter Zijlstra }
9019af0440eSPeter Zijlstra 
9029af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
9039af0440eSPeter Zijlstra 				      size_t count, loff_t *ppos)
9049af0440eSPeter Zijlstra {
9059af0440eSPeter Zijlstra 	/* This is really only for read-only strings */
9069af0440eSPeter Zijlstra 	return -EINVAL;
9079af0440eSPeter Zijlstra }
9089af0440eSPeter Zijlstra 
9099af0440eSPeter Zijlstra static const struct file_operations fops_str = {
9109af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
9119af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
9129af0440eSPeter Zijlstra 	.open =		simple_open,
9139af0440eSPeter Zijlstra 	.llseek =	default_llseek,
9149af0440eSPeter Zijlstra };
9159af0440eSPeter Zijlstra 
9169af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = {
9179af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
9189af0440eSPeter Zijlstra 	.open =		simple_open,
9199af0440eSPeter Zijlstra 	.llseek =	default_llseek,
9209af0440eSPeter Zijlstra };
9219af0440eSPeter Zijlstra 
9229af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = {
9239af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
9249af0440eSPeter Zijlstra 	.open =		simple_open,
9259af0440eSPeter Zijlstra 	.llseek =	default_llseek,
9269af0440eSPeter Zijlstra };
9279af0440eSPeter Zijlstra 
9289af0440eSPeter Zijlstra /**
9299af0440eSPeter Zijlstra  * debugfs_create_str - create a debugfs file that is used to read and write a string value
9309af0440eSPeter Zijlstra  * @name: a pointer to a string containing the name of the file to create.
9319af0440eSPeter Zijlstra  * @mode: the permission that the file should have
9329af0440eSPeter Zijlstra  * @parent: a pointer to the parent dentry for this file.  This should be a
9339af0440eSPeter Zijlstra  *          directory dentry if set.  If this parameter is %NULL, then the
9349af0440eSPeter Zijlstra  *          file will be created in the root of the debugfs filesystem.
9359af0440eSPeter Zijlstra  * @value: a pointer to the variable that the file should read to and write
9369af0440eSPeter Zijlstra  *         from.
9379af0440eSPeter Zijlstra  *
9389af0440eSPeter Zijlstra  * This function creates a file in debugfs with the given name that
9399af0440eSPeter Zijlstra  * contains the value of the variable @value.  If the @mode variable is so
9409af0440eSPeter Zijlstra  * set, it can be read from, and written to.
9419af0440eSPeter Zijlstra  *
9429af0440eSPeter Zijlstra  * This function will return a pointer to a dentry if it succeeds.  This
9439af0440eSPeter Zijlstra  * pointer must be passed to the debugfs_remove() function when the file is
9449af0440eSPeter Zijlstra  * to be removed (no automatic cleanup happens if your module is unloaded,
9459af0440eSPeter Zijlstra  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
9469af0440eSPeter Zijlstra  * returned.
9479af0440eSPeter Zijlstra  *
9489af0440eSPeter Zijlstra  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
9499af0440eSPeter Zijlstra  * be returned.
9509af0440eSPeter Zijlstra  */
9519af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode,
9529af0440eSPeter Zijlstra 			struct dentry *parent, char **value)
9539af0440eSPeter Zijlstra {
9549af0440eSPeter Zijlstra 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
9559af0440eSPeter Zijlstra 				   &fops_str_ro, &fops_str_wo);
9569af0440eSPeter Zijlstra }
9579af0440eSPeter Zijlstra 
958dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
959dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
960dd308bc3SMichael Ellerman {
961dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
96269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
96383b711cbSNicolai Stange 	ssize_t r;
96483b711cbSNicolai Stange 
96569d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
96669d29f9eSNicolai Stange 	if (unlikely(r))
96769d29f9eSNicolai Stange 		return r;
96883b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
969dd308bc3SMichael Ellerman 				blob->size);
97069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
97183b711cbSNicolai Stange 	return r;
972dd308bc3SMichael Ellerman }
973dd308bc3SMichael Ellerman 
97400977a59SArjan van de Ven static const struct file_operations fops_blob = {
975dd308bc3SMichael Ellerman 	.read =		read_file_blob,
976234e3405SStephen Boyd 	.open =		simple_open,
9776038f373SArnd Bergmann 	.llseek =	default_llseek,
978dd308bc3SMichael Ellerman };
979dd308bc3SMichael Ellerman 
980dd308bc3SMichael Ellerman /**
981400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
982dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
983*d616f56dSWolfram Sang  * @mode: the read permission that the file should have (other permissions are
984*d616f56dSWolfram Sang  *	  masked out)
985dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
9866468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
987dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
988dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
989dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
990dd308bc3SMichael Ellerman  *
991dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
992dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
993dd308bc3SMichael Ellerman  * read from. Writing is not supported.
994dd308bc3SMichael Ellerman  *
995dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
996dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
997dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
998adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
9999abb2499SRonald Tschalär  * returned.
1000dd308bc3SMichael Ellerman  *
1001adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
10029abb2499SRonald Tschalär  * be returned.
1003dd308bc3SMichael Ellerman  */
1004f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1005dd308bc3SMichael Ellerman 				   struct dentry *parent,
1006dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
1007dd308bc3SMichael Ellerman {
1008*d616f56dSWolfram Sang 	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
1009dd308bc3SMichael Ellerman }
1010dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
10111a087c6aSAlessandro Rubini 
1012e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
1013e05e279eSLinus Torvalds 			       u32 *array, int array_size)
10149fe2a701SSrivatsa Vaddagiri {
10159fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
10169fe2a701SSrivatsa Vaddagiri 
1017e05e279eSLinus Torvalds 	while (--array_size >= 0) {
10189fe2a701SSrivatsa Vaddagiri 		size_t len;
1019e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
10209fe2a701SSrivatsa Vaddagiri 
1021e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
10229fe2a701SSrivatsa Vaddagiri 		ret += len;
10239fe2a701SSrivatsa Vaddagiri 
10249fe2a701SSrivatsa Vaddagiri 		buf += len;
10259fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
10269fe2a701SSrivatsa Vaddagiri 	}
10279fe2a701SSrivatsa Vaddagiri 	return ret;
10289fe2a701SSrivatsa Vaddagiri }
10299fe2a701SSrivatsa Vaddagiri 
103036048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
10319fe2a701SSrivatsa Vaddagiri {
1032a2b992c8SJakub Kicinski 	struct debugfs_u32_array *data = inode->i_private;
1033a2b992c8SJakub Kicinski 	int size, elements = data->n_elements;
1034e05e279eSLinus Torvalds 	char *buf;
10359fe2a701SSrivatsa Vaddagiri 
1036e05e279eSLinus Torvalds 	/*
1037e05e279eSLinus Torvalds 	 * Max size:
1038e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1039e05e279eSLinus Torvalds 	 *  - terminating NUL character
1040e05e279eSLinus Torvalds 	 */
1041e05e279eSLinus Torvalds 	size = elements*11;
1042e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
1043e05e279eSLinus Torvalds 	if (!buf)
104436048853SDavid Rientjes 		return -ENOMEM;
1045e05e279eSLinus Torvalds 	buf[size] = 0;
1046e05e279eSLinus Torvalds 
1047e05e279eSLinus Torvalds 	file->private_data = buf;
1048a2b992c8SJakub Kicinski 	u32_format_array(buf, size, data->array, data->n_elements);
1049e05e279eSLinus Torvalds 
105036048853SDavid Rientjes 	return nonseekable_open(inode, file);
10519fe2a701SSrivatsa Vaddagiri }
10529fe2a701SSrivatsa Vaddagiri 
105336048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
105436048853SDavid Rientjes 			      loff_t *ppos)
105536048853SDavid Rientjes {
105636048853SDavid Rientjes 	size_t size = strlen(file->private_data);
10579fe2a701SSrivatsa Vaddagiri 
10589fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
10599fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
10609fe2a701SSrivatsa Vaddagiri }
10619fe2a701SSrivatsa Vaddagiri 
10629fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
10639fe2a701SSrivatsa Vaddagiri {
10649fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
10659fe2a701SSrivatsa Vaddagiri 
10669fe2a701SSrivatsa Vaddagiri 	return 0;
10679fe2a701SSrivatsa Vaddagiri }
10689fe2a701SSrivatsa Vaddagiri 
10699fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
10709fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
10719fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
10729fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
10739fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
10749fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
10759fe2a701SSrivatsa Vaddagiri };
10769fe2a701SSrivatsa Vaddagiri 
10779fe2a701SSrivatsa Vaddagiri /**
10789fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
10799fe2a701SSrivatsa Vaddagiri  * array.
10809fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
10819fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
10829fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
10839fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
10849fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
1085a2b992c8SJakub Kicinski  * @array: wrapper struct containing data pointer and size of the array.
10869fe2a701SSrivatsa Vaddagiri  *
10879fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
10889fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
10899fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
10909fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
10919fe2a701SSrivatsa Vaddagiri  */
1092c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1093a2b992c8SJakub Kicinski 			      struct dentry *parent,
1094a2b992c8SJakub Kicinski 			      struct debugfs_u32_array *array)
10959fe2a701SSrivatsa Vaddagiri {
1096a2b992c8SJakub Kicinski 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
10979fe2a701SSrivatsa Vaddagiri }
10989fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10999fe2a701SSrivatsa Vaddagiri 
11003b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
11013b85e4abSHeiko Carstens 
11021a087c6aSAlessandro Rubini /*
11031a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
11041a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
11051a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
11061a087c6aSAlessandro Rubini  */
11071a087c6aSAlessandro Rubini 
11081a087c6aSAlessandro Rubini /**
11091a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
11101a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
11111a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1112b5763accSRandy Dunlap  * @nregs: the length of the above array
11131a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
11141a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
11151a087c6aSAlessandro Rubini  *
11161a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
11171a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
11181a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
11191a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
11201a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
11211a087c6aSAlessandro Rubini  * for example configuration of dma channels
11221a087c6aSAlessandro Rubini  */
11239761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
11241a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
11251a087c6aSAlessandro Rubini {
11269761536eSJoe Perches 	int i;
11271a087c6aSAlessandro Rubini 
11281a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
11291a087c6aSAlessandro Rubini 		if (prefix)
11309761536eSJoe Perches 			seq_printf(s, "%s", prefix);
11319761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
11325858441cSDan Carpenter 			   readl(base + regs->offset));
11339761536eSJoe Perches 		if (seq_has_overflowed(s))
11349761536eSJoe Perches 			break;
11351a087c6aSAlessandro Rubini 	}
11361a087c6aSAlessandro Rubini }
11371a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
11381a087c6aSAlessandro Rubini 
11391a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
11401a087c6aSAlessandro Rubini {
11411a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
11421a087c6aSAlessandro Rubini 
114330332eeeSGeert Uytterhoeven 	if (regset->dev)
114430332eeeSGeert Uytterhoeven 		pm_runtime_get_sync(regset->dev);
114530332eeeSGeert Uytterhoeven 
11461a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
114730332eeeSGeert Uytterhoeven 
114830332eeeSGeert Uytterhoeven 	if (regset->dev)
114930332eeeSGeert Uytterhoeven 		pm_runtime_put(regset->dev);
115030332eeeSGeert Uytterhoeven 
11511a087c6aSAlessandro Rubini 	return 0;
11521a087c6aSAlessandro Rubini }
11531a087c6aSAlessandro Rubini 
11541a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
11551a087c6aSAlessandro Rubini {
11561a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
11571a087c6aSAlessandro Rubini }
11581a087c6aSAlessandro Rubini 
11591a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
11601a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
11611a087c6aSAlessandro Rubini 	.read =		seq_read,
11621a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
11631a087c6aSAlessandro Rubini 	.release =	single_release,
11641a087c6aSAlessandro Rubini };
11651a087c6aSAlessandro Rubini 
11661a087c6aSAlessandro Rubini /**
11671a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
11681a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
11691a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
11701a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
11711a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
11721a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
11731a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
11741a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
11751a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
11761a087c6aSAlessandro Rubini  *
11771a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
11781a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
11791a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
11801a087c6aSAlessandro Rubini  */
1181ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode,
11821a087c6aSAlessandro Rubini 			     struct dentry *parent,
11831a087c6aSAlessandro Rubini 			     struct debugfs_regset32 *regset)
11841a087c6aSAlessandro Rubini {
1185ae91c925SGreg Kroah-Hartman 	debugfs_create_file(name, mode, parent, regset, &fops_regset32);
11861a087c6aSAlessandro Rubini }
11871a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
11883b85e4abSHeiko Carstens 
11893b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
119098210b7fSArend van Spriel 
119198210b7fSArend van Spriel struct debugfs_devm_entry {
119298210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
119398210b7fSArend van Spriel 	struct device *dev;
119498210b7fSArend van Spriel };
119598210b7fSArend van Spriel 
119698210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
119798210b7fSArend van Spriel {
119898210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
119998210b7fSArend van Spriel 
120098210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
120198210b7fSArend van Spriel }
120298210b7fSArend van Spriel 
120398210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
120498210b7fSArend van Spriel 	.owner = THIS_MODULE,
120598210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
120698210b7fSArend van Spriel 	.release = single_release,
120798210b7fSArend van Spriel 	.read = seq_read,
120898210b7fSArend van Spriel 	.llseek = seq_lseek
120998210b7fSArend van Spriel };
121098210b7fSArend van Spriel 
121198210b7fSArend van Spriel /**
121298210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
121398210b7fSArend van Spriel  *
121498210b7fSArend van Spriel  * @dev: device related to this debugfs file.
121598210b7fSArend van Spriel  * @name: name of the debugfs file.
121698210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
121798210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
121898210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
121998210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
122098210b7fSArend van Spriel  */
12210d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name,
122298210b7fSArend van Spriel 				 struct dentry *parent,
12230d519cbfSGreg Kroah-Hartman 				 int (*read_fn)(struct seq_file *s, void *data))
122498210b7fSArend van Spriel {
122598210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
122698210b7fSArend van Spriel 
122798210b7fSArend van Spriel 	if (IS_ERR(parent))
12280d519cbfSGreg Kroah-Hartman 		return;
122998210b7fSArend van Spriel 
123098210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
123198210b7fSArend van Spriel 	if (!entry)
12320d519cbfSGreg Kroah-Hartman 		return;
123398210b7fSArend van Spriel 
123498210b7fSArend van Spriel 	entry->read = read_fn;
123598210b7fSArend van Spriel 	entry->dev = dev;
123698210b7fSArend van Spriel 
12370d519cbfSGreg Kroah-Hartman 	debugfs_create_file(name, S_IRUGO, parent, entry,
123898210b7fSArend van Spriel 			    &debugfs_devm_entry_ops);
123998210b7fSArend van Spriel }
124098210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1241