xref: /linux-6.15/fs/debugfs/file.c (revision 358fcf5d)
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 {
150*358fcf5dSMichal Suchanek 	if ((inode->i_mode & 07777 & ~0444) == 0 &&
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 &&
182112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
183112cedc8SSven Eckelmann 			r = -ENXIO;
184275678e7STaehee Yoo 			goto out;
185112cedc8SSven Eckelmann 		}
186275678e7STaehee Yoo #endif
187275678e7STaehee Yoo 
1889fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1899fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1909fd4dcecSNicolai Stange 			dentry);
1919fd4dcecSNicolai Stange 		r = -ENXIO;
1929fd4dcecSNicolai Stange 		goto out;
1939fd4dcecSNicolai Stange 	}
1949fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1959fd4dcecSNicolai Stange 
1969fd4dcecSNicolai Stange 	if (real_fops->open)
1979fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1989fd4dcecSNicolai Stange 
1999fd4dcecSNicolai Stange out:
20069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
2019fd4dcecSNicolai Stange 	return r;
2029fd4dcecSNicolai Stange }
2039fd4dcecSNicolai Stange 
2049fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
2059fd4dcecSNicolai Stange 	.open = open_proxy_open,
2069fd4dcecSNicolai Stange };
2079fd4dcecSNicolai Stange 
20849d200deSNicolai Stange #define PROTO(args...) args
20949d200deSNicolai Stange #define ARGS(args...) args
21049d200deSNicolai Stange 
21149d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
21249d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
21349d200deSNicolai Stange {									\
21469d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
215154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
21649d200deSNicolai Stange 	ret_type r;							\
21749d200deSNicolai Stange 									\
21869d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
21969d29f9eSNicolai Stange 	if (unlikely(r))						\
22069d29f9eSNicolai Stange 		return r;						\
221154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
22249d200deSNicolai Stange 	r = real_fops->name(args);					\
22369d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
22449d200deSNicolai Stange 	return r;							\
22549d200deSNicolai Stange }
22649d200deSNicolai Stange 
22749d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
22849d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
22949d200deSNicolai Stange 		ARGS(filp, offset, whence));
23049d200deSNicolai Stange 
23149d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
23249d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
23349d200deSNicolai Stange 			loff_t *ppos),
23449d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
23549d200deSNicolai Stange 
23649d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
23749d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
23849d200deSNicolai Stange 			loff_t *ppos),
23949d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
24049d200deSNicolai Stange 
24149d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
24249d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
24349d200deSNicolai Stange 		ARGS(filp, cmd, arg));
24449d200deSNicolai Stange 
245076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
24649d200deSNicolai Stange 				struct poll_table_struct *wait)
24749d200deSNicolai Stange {
24869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
249e6c8adcaSAl Viro 	__poll_t r = 0;
250154b9d75SNicolai Stange 	const struct file_operations *real_fops;
25149d200deSNicolai Stange 
25269d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
253a9a08845SLinus Torvalds 		return EPOLLHUP;
25449d200deSNicolai Stange 
255154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
25649d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
25769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
25849d200deSNicolai Stange 	return r;
25949d200deSNicolai Stange }
26049d200deSNicolai Stange 
26149d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
26249d200deSNicolai Stange {
26349d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
26486f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
26549d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
26649d200deSNicolai Stange 	int r = 0;
26749d200deSNicolai Stange 
26849d200deSNicolai Stange 	/*
26949d200deSNicolai Stange 	 * We must not protect this against removal races here: the
27049d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
27149d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
27249d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
27349d200deSNicolai Stange 	 */
27449d200deSNicolai Stange 	if (real_fops->release)
27549d200deSNicolai Stange 		r = real_fops->release(inode, filp);
27649d200deSNicolai Stange 
27749d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
278c80a67bdSXu Wang 	kfree(proxy_fops);
27949d200deSNicolai Stange 	fops_put(real_fops);
280a1a9e5d2SEric Engestrom 	return r;
28149d200deSNicolai Stange }
28249d200deSNicolai Stange 
28349d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
28449d200deSNicolai Stange 				const struct file_operations *real_fops)
28549d200deSNicolai Stange {
28649d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
28749d200deSNicolai Stange 	if (real_fops->llseek)
28849d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
28949d200deSNicolai Stange 	if (real_fops->read)
29049d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
29149d200deSNicolai Stange 	if (real_fops->write)
29249d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
29349d200deSNicolai Stange 	if (real_fops->poll)
29449d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
29549d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
29649d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
29749d200deSNicolai Stange }
29849d200deSNicolai Stange 
29949d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
30049d200deSNicolai Stange {
30169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
30249d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
30349d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
3047d39bc50SNicolai Stange 	int r;
30549d200deSNicolai Stange 
3067d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
3077d39bc50SNicolai Stange 	if (r)
3087d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
30949d200deSNicolai Stange 
31086f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
3115496197fSDavid Howells 
312a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
3135496197fSDavid Howells 	if (r)
3145496197fSDavid Howells 		goto out;
3155496197fSDavid Howells 
316275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
317e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
318275678e7STaehee Yoo 		if (real_fops->owner &&
319112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
320112cedc8SSven Eckelmann 			r = -ENXIO;
321275678e7STaehee Yoo 			goto out;
322112cedc8SSven Eckelmann 		}
323275678e7STaehee Yoo #endif
324275678e7STaehee Yoo 
32549d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
32649d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
32749d200deSNicolai Stange 			dentry);
32849d200deSNicolai Stange 		r = -ENXIO;
32949d200deSNicolai Stange 		goto out;
33049d200deSNicolai Stange 	}
33149d200deSNicolai Stange 
33249d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
33349d200deSNicolai Stange 	if (!proxy_fops) {
33449d200deSNicolai Stange 		r = -ENOMEM;
33549d200deSNicolai Stange 		goto free_proxy;
33649d200deSNicolai Stange 	}
33749d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
33849d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
33949d200deSNicolai Stange 
34049d200deSNicolai Stange 	if (real_fops->open) {
34149d200deSNicolai Stange 		r = real_fops->open(inode, filp);
342b10e3e90SNicolai Stange 		if (r) {
343b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
344b10e3e90SNicolai Stange 			goto free_proxy;
345b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
34649d200deSNicolai Stange 			/* No protection against file removal anymore. */
34749d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
34849d200deSNicolai Stange 				dentry);
34949d200deSNicolai Stange 			goto free_proxy;
35049d200deSNicolai Stange 		}
35149d200deSNicolai Stange 	}
35249d200deSNicolai Stange 
35349d200deSNicolai Stange 	goto out;
35449d200deSNicolai Stange free_proxy:
35549d200deSNicolai Stange 	kfree(proxy_fops);
35649d200deSNicolai Stange 	fops_put(real_fops);
35749d200deSNicolai Stange out:
35869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
35949d200deSNicolai Stange 	return r;
36049d200deSNicolai Stange }
36149d200deSNicolai Stange 
36249d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
36349d200deSNicolai Stange 	.open = full_proxy_open,
36449d200deSNicolai Stange };
36549d200deSNicolai Stange 
366c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
367c6468808SNicolai Stange 			size_t len, loff_t *ppos)
368c6468808SNicolai Stange {
36969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
370c6468808SNicolai Stange 	ssize_t ret;
371c6468808SNicolai Stange 
37269d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
37369d29f9eSNicolai Stange 	if (unlikely(ret))
37469d29f9eSNicolai Stange 		return ret;
375c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
37669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
377c6468808SNicolai Stange 	return ret;
378c6468808SNicolai Stange }
379c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
380c6468808SNicolai Stange 
381c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
382c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
383c6468808SNicolai Stange {
38469d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
385c6468808SNicolai Stange 	ssize_t ret;
386c6468808SNicolai Stange 
38769d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
38869d29f9eSNicolai Stange 	if (unlikely(ret))
38969d29f9eSNicolai Stange 		return ret;
390c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
39169d29f9eSNicolai Stange 	debugfs_file_put(dentry);
392c6468808SNicolai Stange 	return ret;
393c6468808SNicolai Stange }
394c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
395c6468808SNicolai Stange 
3964909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3974909f168SNicolai Stange 					struct dentry *parent, void *value,
3984909f168SNicolai Stange 					const struct file_operations *fops,
3994909f168SNicolai Stange 					const struct file_operations *fops_ro,
4004909f168SNicolai Stange 					const struct file_operations *fops_wo)
4014909f168SNicolai Stange {
4024909f168SNicolai Stange 	/* if there are no write bits set, make read only */
4034909f168SNicolai Stange 	if (!(mode & S_IWUGO))
4044909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
4054909f168SNicolai Stange 						fops_ro);
4064909f168SNicolai Stange 	/* if there are no read bits set, make write only */
4074909f168SNicolai Stange 	if (!(mode & S_IRUGO))
4084909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
4094909f168SNicolai Stange 						fops_wo);
4104909f168SNicolai Stange 
4114909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
4124909f168SNicolai Stange }
4134909f168SNicolai Stange 
4148b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
415acaefc25SArnd Bergmann {
416acaefc25SArnd Bergmann 	*(u8 *)data = val;
4178b88b099SChristoph Hellwig 	return 0;
418acaefc25SArnd Bergmann }
4198b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
420acaefc25SArnd Bergmann {
4218b88b099SChristoph Hellwig 	*val = *(u8 *)data;
4228b88b099SChristoph Hellwig 	return 0;
423acaefc25SArnd Bergmann }
4244909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
4254909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
4264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds /**
4296468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
4301da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4311da177e4SLinus Torvalds  * @mode: the permission that the file should have
4321da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4336468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4341da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4351da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4361da177e4SLinus Torvalds  *         from.
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4391da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4401da177e4SLinus Torvalds  * set, it can be read from, and written to.
4411da177e4SLinus Torvalds  */
4429655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
4439655ac4aSGreg Kroah-Hartman 		       u8 *value)
4441da177e4SLinus Torvalds {
4459655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
446b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
4491da177e4SLinus Torvalds 
4508b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
451acaefc25SArnd Bergmann {
452acaefc25SArnd Bergmann 	*(u16 *)data = val;
4538b88b099SChristoph Hellwig 	return 0;
454acaefc25SArnd Bergmann }
4558b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
456acaefc25SArnd Bergmann {
4578b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4588b88b099SChristoph Hellwig 	return 0;
459acaefc25SArnd Bergmann }
4604909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
4614909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
4624909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
463acaefc25SArnd Bergmann 
4641da177e4SLinus Torvalds /**
4656468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4661da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4671da177e4SLinus Torvalds  * @mode: the permission that the file should have
4681da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4696468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4701da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4711da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4721da177e4SLinus Torvalds  *         from.
4731da177e4SLinus Torvalds  *
4741da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4751da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4761da177e4SLinus Torvalds  * set, it can be read from, and written to.
4771da177e4SLinus Torvalds  */
478313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
479313f5dbbSGreg Kroah-Hartman 			u16 *value)
4801da177e4SLinus Torvalds {
481313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
482b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4851da177e4SLinus Torvalds 
4868b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
487acaefc25SArnd Bergmann {
488acaefc25SArnd Bergmann 	*(u32 *)data = val;
4898b88b099SChristoph Hellwig 	return 0;
490acaefc25SArnd Bergmann }
4918b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
492acaefc25SArnd Bergmann {
4938b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4948b88b099SChristoph Hellwig 	return 0;
495acaefc25SArnd Bergmann }
4964909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4974909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4984909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
499acaefc25SArnd Bergmann 
5001da177e4SLinus Torvalds /**
5016468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
5021da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
5031da177e4SLinus Torvalds  * @mode: the permission that the file should have
5041da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
5056468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
5061da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
5071da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
5081da177e4SLinus Torvalds  *         from.
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
5111da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
5121da177e4SLinus Torvalds  * set, it can be read from, and written to.
5131da177e4SLinus Torvalds  */
5142b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
5152b07021aSGreg Kroah-Hartman 			u32 *value)
5161da177e4SLinus Torvalds {
5172b07021aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
518b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
5211da177e4SLinus Torvalds 
5228b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
5238447891fSMichael Ellerman {
5248447891fSMichael Ellerman 	*(u64 *)data = val;
5258b88b099SChristoph Hellwig 	return 0;
5268447891fSMichael Ellerman }
5278447891fSMichael Ellerman 
5288b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
5298447891fSMichael Ellerman {
5308b88b099SChristoph Hellwig 	*val = *(u64 *)data;
5318b88b099SChristoph Hellwig 	return 0;
5328447891fSMichael Ellerman }
5334909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
5344909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
5354909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5368447891fSMichael Ellerman 
5378447891fSMichael Ellerman /**
5388447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5398447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5408447891fSMichael Ellerman  * @mode: the permission that the file should have
5418447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5428447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5438447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5448447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5458447891fSMichael Ellerman  *         from.
5468447891fSMichael Ellerman  *
5478447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5488447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5498447891fSMichael Ellerman  * set, it can be read from, and written to.
5508447891fSMichael Ellerman  */
551ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
552ad26221fSGreg Kroah-Hartman 			u64 *value)
5538447891fSMichael Ellerman {
554ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
555b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5568447891fSMichael Ellerman }
5578447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5588447891fSMichael Ellerman 
559c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
560c23fe831SViresh Kumar {
561c23fe831SViresh Kumar 	*(unsigned long *)data = val;
562c23fe831SViresh Kumar 	return 0;
563c23fe831SViresh Kumar }
564c23fe831SViresh Kumar 
565c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
566c23fe831SViresh Kumar {
567c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
568c23fe831SViresh Kumar 	return 0;
569c23fe831SViresh Kumar }
5704909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5714909f168SNicolai Stange 			"%llu\n");
5724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
574c23fe831SViresh Kumar 
575c23fe831SViresh Kumar /**
576c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
577c23fe831SViresh Kumar  * an unsigned long value.
578c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
579c23fe831SViresh Kumar  * @mode: the permission that the file should have
580c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
581c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
582c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
583c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
584c23fe831SViresh Kumar  *         from.
585c23fe831SViresh Kumar  *
586c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
587c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
588c23fe831SViresh Kumar  * set, it can be read from, and written to.
589c23fe831SViresh Kumar  */
590fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
591fb05b14cSGreg Kroah-Hartman 			  unsigned long *value)
592c23fe831SViresh Kumar {
593fb05b14cSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
594fb05b14cSGreg Kroah-Hartman 				   &fops_ulong_ro, &fops_ulong_wo);
595c23fe831SViresh Kumar }
596c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
597c23fe831SViresh Kumar 
5984909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
5994909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
6012ebefc50SRobin Getz 
6024909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
6034909f168SNicolai Stange 			"0x%04llx\n");
6044909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
6062ebefc50SRobin Getz 
6074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
6084909f168SNicolai Stange 			"0x%08llx\n");
6094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
6112ebefc50SRobin Getz 
6124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
6134909f168SNicolai Stange 			"0x%016llx\n");
6144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
6154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
61615b0beaaSHuang Ying 
617e6716b87SRandy Dunlap /*
61815b0beaaSHuang 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
619e6716b87SRandy Dunlap  *
620e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
621e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
622e6716b87SRandy Dunlap  * decimal functions.
623e6716b87SRandy Dunlap  */
624e6716b87SRandy Dunlap 
6252ebefc50SRobin Getz /**
6262ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
627e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
628e6716b87SRandy Dunlap  * @mode: the permission that the file should have
629e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
630e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
631e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
632e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
633e6716b87SRandy Dunlap  *         from.
6342ebefc50SRobin Getz  */
635c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
636c7c11689SGreg Kroah-Hartman 		       u8 *value)
6372ebefc50SRobin Getz {
638c7c11689SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
639b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6402ebefc50SRobin Getz }
6412ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6422ebefc50SRobin Getz 
643e6716b87SRandy Dunlap /**
644e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
645e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
646e6716b87SRandy Dunlap  * @mode: the permission that the file should have
647e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
648e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
649e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
650e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
651e6716b87SRandy Dunlap  *         from.
652e6716b87SRandy Dunlap  */
653e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
654e40d38f2SGreg Kroah-Hartman 			u16 *value)
6552ebefc50SRobin Getz {
656e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
657b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6582ebefc50SRobin Getz }
6592ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6602ebefc50SRobin Getz 
661e6716b87SRandy Dunlap /**
662e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
663e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
664e6716b87SRandy Dunlap  * @mode: the permission that the file should have
665e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
666e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
667e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
668e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
669e6716b87SRandy Dunlap  *         from.
670e6716b87SRandy Dunlap  */
671f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
672f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
6732ebefc50SRobin Getz {
674f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
675b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6762ebefc50SRobin Getz }
6772ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6782ebefc50SRobin Getz 
67915b0beaaSHuang Ying /**
68015b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
68115b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
68215b0beaaSHuang Ying  * @mode: the permission that the file should have
68315b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
68415b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
68515b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
68615b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
68715b0beaaSHuang Ying  *         from.
68815b0beaaSHuang Ying  */
6890864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
6900864c408SGreg Kroah-Hartman 			u64 *value)
69115b0beaaSHuang Ying {
6920864c408SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
69382b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
69415b0beaaSHuang Ying }
69515b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
69615b0beaaSHuang Ying 
6975e078787SInaky Perez-Gonzalez 
6985e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
6995e078787SInaky Perez-Gonzalez {
7005e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
7015e078787SInaky Perez-Gonzalez 	return 0;
7025e078787SInaky Perez-Gonzalez }
7035e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
7045e078787SInaky Perez-Gonzalez {
7055e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
7065e078787SInaky Perez-Gonzalez 	return 0;
7075e078787SInaky Perez-Gonzalez }
7084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
7095e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
7104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
7114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
7125e078787SInaky Perez-Gonzalez 
7135e078787SInaky Perez-Gonzalez /**
7145e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7155e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7165e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7175e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7185e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7195e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7205e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7215e078787SInaky Perez-Gonzalez  *         from.
7225e078787SInaky Perez-Gonzalez  */
7238e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
7245e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
7255e078787SInaky Perez-Gonzalez {
7268e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
7278e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
7285e078787SInaky Perez-Gonzalez }
7295e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7305e078787SInaky Perez-Gonzalez 
7313a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7323a76e5e0SSeth Jennings {
7333a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7343a76e5e0SSeth Jennings 	return 0;
7353a76e5e0SSeth Jennings }
7363a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7373a76e5e0SSeth Jennings {
7383a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7393a76e5e0SSeth Jennings 	return 0;
7403a76e5e0SSeth Jennings }
7414909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7423a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7434909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7444909f168SNicolai Stange 			"%lld\n");
7454909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7464909f168SNicolai Stange 			"%lld\n");
7473a76e5e0SSeth Jennings 
7483a76e5e0SSeth Jennings /**
7493a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7503a76e5e0SSeth Jennings  * write an atomic_t value
7513a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7523a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7533a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7543a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7553a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7563a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7573a76e5e0SSeth Jennings  *         from.
7583a76e5e0SSeth Jennings  */
7599927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode,
7603a76e5e0SSeth Jennings 			     struct dentry *parent, atomic_t *value)
7613a76e5e0SSeth Jennings {
7629927c6faSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
7639927c6faSGreg Kroah-Hartman 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
7643a76e5e0SSeth Jennings }
7653a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7665e078787SInaky Perez-Gonzalez 
7670642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7681da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7691da177e4SLinus Torvalds {
770c8a9c285SRasmus Villemoes 	char buf[2];
7714d45f797SNicolai Stange 	bool val;
77269d29f9eSNicolai Stange 	int r;
77369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7741da177e4SLinus Torvalds 
77569d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
77669d29f9eSNicolai Stange 	if (unlikely(r))
7774d45f797SNicolai Stange 		return r;
77869d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
77969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7804d45f797SNicolai Stange 
7814d45f797SNicolai Stange 	if (val)
7821da177e4SLinus Torvalds 		buf[0] = 'Y';
7831da177e4SLinus Torvalds 	else
7841da177e4SLinus Torvalds 		buf[0] = 'N';
7851da177e4SLinus Torvalds 	buf[1] = '\n';
7861da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7871da177e4SLinus Torvalds }
7880642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7891da177e4SLinus Torvalds 
7900642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7911da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7921da177e4SLinus Torvalds {
7938705b48eSJonathan Cameron 	bool bv;
79469d29f9eSNicolai Stange 	int r;
795621a5f7aSViresh Kumar 	bool *val = file->private_data;
79669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7971da177e4SLinus Torvalds 
798964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
799964f8363SAndy Shevchenko 	if (!r) {
80069d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
80169d29f9eSNicolai Stange 		if (unlikely(r))
8024d45f797SNicolai Stange 			return r;
80369d29f9eSNicolai Stange 		*val = bv;
80469d29f9eSNicolai Stange 		debugfs_file_put(dentry);
8054d45f797SNicolai Stange 	}
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	return count;
8081da177e4SLinus Torvalds }
8090642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
8101da177e4SLinus Torvalds 
8114b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
8120642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
8130642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
814234e3405SStephen Boyd 	.open =		simple_open,
8156038f373SArnd Bergmann 	.llseek =	default_llseek,
8161da177e4SLinus Torvalds };
8171da177e4SLinus Torvalds 
8186713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
8196713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8206713e8fbSStephen Boyd 	.open =		simple_open,
8216713e8fbSStephen Boyd 	.llseek =	default_llseek,
8226713e8fbSStephen Boyd };
8236713e8fbSStephen Boyd 
8246713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8256713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8266713e8fbSStephen Boyd 	.open =		simple_open,
8276713e8fbSStephen Boyd 	.llseek =	default_llseek,
8286713e8fbSStephen Boyd };
8296713e8fbSStephen Boyd 
8301da177e4SLinus Torvalds /**
8316468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8321da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8331da177e4SLinus Torvalds  * @mode: the permission that the file should have
8341da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8356468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8361da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8371da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8381da177e4SLinus Torvalds  *         from.
8391da177e4SLinus Torvalds  *
8401da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8411da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8421da177e4SLinus Torvalds  * set, it can be read from, and written to.
8431da177e4SLinus Torvalds  */
844393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
845393b0638SGreg Kroah-Hartman 			 bool *value)
8461da177e4SLinus Torvalds {
847393b0638SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8486713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8511da177e4SLinus Torvalds 
8529af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
8539af0440eSPeter Zijlstra 			      size_t count, loff_t *ppos)
8549af0440eSPeter Zijlstra {
8559af0440eSPeter Zijlstra 	struct dentry *dentry = F_DENTRY(file);
8569af0440eSPeter Zijlstra 	char *str, *copy = NULL;
8579af0440eSPeter Zijlstra 	int copy_len, len;
8589af0440eSPeter Zijlstra 	ssize_t ret;
8599af0440eSPeter Zijlstra 
8609af0440eSPeter Zijlstra 	ret = debugfs_file_get(dentry);
8619af0440eSPeter Zijlstra 	if (unlikely(ret))
8629af0440eSPeter Zijlstra 		return ret;
8639af0440eSPeter Zijlstra 
8649af0440eSPeter Zijlstra 	str = *(char **)file->private_data;
8659af0440eSPeter Zijlstra 	len = strlen(str) + 1;
8669af0440eSPeter Zijlstra 	copy = kmalloc(len, GFP_KERNEL);
8679af0440eSPeter Zijlstra 	if (!copy) {
8689af0440eSPeter Zijlstra 		debugfs_file_put(dentry);
8699af0440eSPeter Zijlstra 		return -ENOMEM;
8709af0440eSPeter Zijlstra 	}
8719af0440eSPeter Zijlstra 
8729af0440eSPeter Zijlstra 	copy_len = strscpy(copy, str, len);
8739af0440eSPeter Zijlstra 	debugfs_file_put(dentry);
8749af0440eSPeter Zijlstra 	if (copy_len < 0) {
8759af0440eSPeter Zijlstra 		kfree(copy);
8769af0440eSPeter Zijlstra 		return copy_len;
8779af0440eSPeter Zijlstra 	}
8789af0440eSPeter Zijlstra 
8799af0440eSPeter Zijlstra 	copy[copy_len] = '\n';
8809af0440eSPeter Zijlstra 
881f501b6a2SDietmar Eggemann 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
8829af0440eSPeter Zijlstra 	kfree(copy);
8839af0440eSPeter Zijlstra 
8849af0440eSPeter Zijlstra 	return ret;
8859af0440eSPeter Zijlstra }
8869af0440eSPeter Zijlstra 
8879af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
8889af0440eSPeter Zijlstra 				      size_t count, loff_t *ppos)
8899af0440eSPeter Zijlstra {
8909af0440eSPeter Zijlstra 	/* This is really only for read-only strings */
8919af0440eSPeter Zijlstra 	return -EINVAL;
8929af0440eSPeter Zijlstra }
8939af0440eSPeter Zijlstra 
8949af0440eSPeter Zijlstra static const struct file_operations fops_str = {
8959af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
8969af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
8979af0440eSPeter Zijlstra 	.open =		simple_open,
8989af0440eSPeter Zijlstra 	.llseek =	default_llseek,
8999af0440eSPeter Zijlstra };
9009af0440eSPeter Zijlstra 
9019af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = {
9029af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
9039af0440eSPeter Zijlstra 	.open =		simple_open,
9049af0440eSPeter Zijlstra 	.llseek =	default_llseek,
9059af0440eSPeter Zijlstra };
9069af0440eSPeter Zijlstra 
9079af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = {
9089af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
9099af0440eSPeter Zijlstra 	.open =		simple_open,
9109af0440eSPeter Zijlstra 	.llseek =	default_llseek,
9119af0440eSPeter Zijlstra };
9129af0440eSPeter Zijlstra 
9139af0440eSPeter Zijlstra /**
9149af0440eSPeter Zijlstra  * debugfs_create_str - create a debugfs file that is used to read and write a string value
9159af0440eSPeter Zijlstra  * @name: a pointer to a string containing the name of the file to create.
9169af0440eSPeter Zijlstra  * @mode: the permission that the file should have
9179af0440eSPeter Zijlstra  * @parent: a pointer to the parent dentry for this file.  This should be a
9189af0440eSPeter Zijlstra  *          directory dentry if set.  If this parameter is %NULL, then the
9199af0440eSPeter Zijlstra  *          file will be created in the root of the debugfs filesystem.
9209af0440eSPeter Zijlstra  * @value: a pointer to the variable that the file should read to and write
9219af0440eSPeter Zijlstra  *         from.
9229af0440eSPeter Zijlstra  *
9239af0440eSPeter Zijlstra  * This function creates a file in debugfs with the given name that
9249af0440eSPeter Zijlstra  * contains the value of the variable @value.  If the @mode variable is so
9259af0440eSPeter Zijlstra  * set, it can be read from, and written to.
9269af0440eSPeter Zijlstra  *
9279af0440eSPeter Zijlstra  * This function will return a pointer to a dentry if it succeeds.  This
9289af0440eSPeter Zijlstra  * pointer must be passed to the debugfs_remove() function when the file is
9299af0440eSPeter Zijlstra  * to be removed (no automatic cleanup happens if your module is unloaded,
9309af0440eSPeter Zijlstra  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
9319af0440eSPeter Zijlstra  * returned.
9329af0440eSPeter Zijlstra  *
9339af0440eSPeter Zijlstra  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
9349af0440eSPeter Zijlstra  * be returned.
9359af0440eSPeter Zijlstra  */
9369af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode,
9379af0440eSPeter Zijlstra 			struct dentry *parent, char **value)
9389af0440eSPeter Zijlstra {
9399af0440eSPeter Zijlstra 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
9409af0440eSPeter Zijlstra 				   &fops_str_ro, &fops_str_wo);
9419af0440eSPeter Zijlstra }
9429af0440eSPeter Zijlstra 
943dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
944dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
945dd308bc3SMichael Ellerman {
946dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
94769d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
94883b711cbSNicolai Stange 	ssize_t r;
94983b711cbSNicolai Stange 
95069d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
95169d29f9eSNicolai Stange 	if (unlikely(r))
95269d29f9eSNicolai Stange 		return r;
95383b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
954dd308bc3SMichael Ellerman 				blob->size);
95569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
95683b711cbSNicolai Stange 	return r;
957dd308bc3SMichael Ellerman }
958dd308bc3SMichael Ellerman 
95900977a59SArjan van de Ven static const struct file_operations fops_blob = {
960dd308bc3SMichael Ellerman 	.read =		read_file_blob,
961234e3405SStephen Boyd 	.open =		simple_open,
9626038f373SArnd Bergmann 	.llseek =	default_llseek,
963dd308bc3SMichael Ellerman };
964dd308bc3SMichael Ellerman 
965dd308bc3SMichael Ellerman /**
966400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
967dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
968d616f56dSWolfram Sang  * @mode: the read permission that the file should have (other permissions are
969d616f56dSWolfram Sang  *	  masked out)
970dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
9716468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
972dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
973dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
974dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
975dd308bc3SMichael Ellerman  *
976dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
977dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
978dd308bc3SMichael Ellerman  * read from. Writing is not supported.
979dd308bc3SMichael Ellerman  *
980dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
981dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
982dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
983adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
9849abb2499SRonald Tschalär  * returned.
985dd308bc3SMichael Ellerman  *
986adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
9879abb2499SRonald Tschalär  * be returned.
988dd308bc3SMichael Ellerman  */
989f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
990dd308bc3SMichael Ellerman 				   struct dentry *parent,
991dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
992dd308bc3SMichael Ellerman {
993d616f56dSWolfram Sang 	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
994dd308bc3SMichael Ellerman }
995dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
9961a087c6aSAlessandro Rubini 
997e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
998e05e279eSLinus Torvalds 			       u32 *array, int array_size)
9999fe2a701SSrivatsa Vaddagiri {
10009fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
10019fe2a701SSrivatsa Vaddagiri 
1002e05e279eSLinus Torvalds 	while (--array_size >= 0) {
10039fe2a701SSrivatsa Vaddagiri 		size_t len;
1004e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
10059fe2a701SSrivatsa Vaddagiri 
1006e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
10079fe2a701SSrivatsa Vaddagiri 		ret += len;
10089fe2a701SSrivatsa Vaddagiri 
10099fe2a701SSrivatsa Vaddagiri 		buf += len;
10109fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
10119fe2a701SSrivatsa Vaddagiri 	}
10129fe2a701SSrivatsa Vaddagiri 	return ret;
10139fe2a701SSrivatsa Vaddagiri }
10149fe2a701SSrivatsa Vaddagiri 
101536048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
10169fe2a701SSrivatsa Vaddagiri {
1017a2b992c8SJakub Kicinski 	struct debugfs_u32_array *data = inode->i_private;
1018a2b992c8SJakub Kicinski 	int size, elements = data->n_elements;
1019e05e279eSLinus Torvalds 	char *buf;
10209fe2a701SSrivatsa Vaddagiri 
1021e05e279eSLinus Torvalds 	/*
1022e05e279eSLinus Torvalds 	 * Max size:
1023e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1024e05e279eSLinus Torvalds 	 *  - terminating NUL character
1025e05e279eSLinus Torvalds 	 */
1026e05e279eSLinus Torvalds 	size = elements*11;
1027e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
1028e05e279eSLinus Torvalds 	if (!buf)
102936048853SDavid Rientjes 		return -ENOMEM;
1030e05e279eSLinus Torvalds 	buf[size] = 0;
1031e05e279eSLinus Torvalds 
1032e05e279eSLinus Torvalds 	file->private_data = buf;
1033a2b992c8SJakub Kicinski 	u32_format_array(buf, size, data->array, data->n_elements);
1034e05e279eSLinus Torvalds 
103536048853SDavid Rientjes 	return nonseekable_open(inode, file);
10369fe2a701SSrivatsa Vaddagiri }
10379fe2a701SSrivatsa Vaddagiri 
103836048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
103936048853SDavid Rientjes 			      loff_t *ppos)
104036048853SDavid Rientjes {
104136048853SDavid Rientjes 	size_t size = strlen(file->private_data);
10429fe2a701SSrivatsa Vaddagiri 
10439fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
10449fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
10459fe2a701SSrivatsa Vaddagiri }
10469fe2a701SSrivatsa Vaddagiri 
10479fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
10489fe2a701SSrivatsa Vaddagiri {
10499fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
10509fe2a701SSrivatsa Vaddagiri 
10519fe2a701SSrivatsa Vaddagiri 	return 0;
10529fe2a701SSrivatsa Vaddagiri }
10539fe2a701SSrivatsa Vaddagiri 
10549fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
10559fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
10569fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
10579fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
10589fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
10599fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
10609fe2a701SSrivatsa Vaddagiri };
10619fe2a701SSrivatsa Vaddagiri 
10629fe2a701SSrivatsa Vaddagiri /**
10639fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
10649fe2a701SSrivatsa Vaddagiri  * array.
10659fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
10669fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
10679fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
10689fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
10699fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
1070a2b992c8SJakub Kicinski  * @array: wrapper struct containing data pointer and size of the array.
10719fe2a701SSrivatsa Vaddagiri  *
10729fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
10739fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
10749fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
10759fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
10769fe2a701SSrivatsa Vaddagiri  */
1077c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1078a2b992c8SJakub Kicinski 			      struct dentry *parent,
1079a2b992c8SJakub Kicinski 			      struct debugfs_u32_array *array)
10809fe2a701SSrivatsa Vaddagiri {
1081a2b992c8SJakub Kicinski 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
10829fe2a701SSrivatsa Vaddagiri }
10839fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10849fe2a701SSrivatsa Vaddagiri 
10853b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
10863b85e4abSHeiko Carstens 
10871a087c6aSAlessandro Rubini /*
10881a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
10891a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
10901a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
10911a087c6aSAlessandro Rubini  */
10921a087c6aSAlessandro Rubini 
10931a087c6aSAlessandro Rubini /**
10941a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10951a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10961a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1097b5763accSRandy Dunlap  * @nregs: the length of the above array
10981a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10991a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
11001a087c6aSAlessandro Rubini  *
11011a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
11021a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
11031a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
11041a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
11051a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
11061a087c6aSAlessandro Rubini  * for example configuration of dma channels
11071a087c6aSAlessandro Rubini  */
11089761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
11091a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
11101a087c6aSAlessandro Rubini {
11119761536eSJoe Perches 	int i;
11121a087c6aSAlessandro Rubini 
11131a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
11141a087c6aSAlessandro Rubini 		if (prefix)
11159761536eSJoe Perches 			seq_printf(s, "%s", prefix);
11169761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
11175858441cSDan Carpenter 			   readl(base + regs->offset));
11189761536eSJoe Perches 		if (seq_has_overflowed(s))
11199761536eSJoe Perches 			break;
11201a087c6aSAlessandro Rubini 	}
11211a087c6aSAlessandro Rubini }
11221a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
11231a087c6aSAlessandro Rubini 
11241a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
11251a087c6aSAlessandro Rubini {
11261a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
11271a087c6aSAlessandro Rubini 
112830332eeeSGeert Uytterhoeven 	if (regset->dev)
112930332eeeSGeert Uytterhoeven 		pm_runtime_get_sync(regset->dev);
113030332eeeSGeert Uytterhoeven 
11311a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
113230332eeeSGeert Uytterhoeven 
113330332eeeSGeert Uytterhoeven 	if (regset->dev)
113430332eeeSGeert Uytterhoeven 		pm_runtime_put(regset->dev);
113530332eeeSGeert Uytterhoeven 
11361a087c6aSAlessandro Rubini 	return 0;
11371a087c6aSAlessandro Rubini }
11381a087c6aSAlessandro Rubini 
11391a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
11401a087c6aSAlessandro Rubini {
11411a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
11421a087c6aSAlessandro Rubini }
11431a087c6aSAlessandro Rubini 
11441a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
11451a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
11461a087c6aSAlessandro Rubini 	.read =		seq_read,
11471a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
11481a087c6aSAlessandro Rubini 	.release =	single_release,
11491a087c6aSAlessandro Rubini };
11501a087c6aSAlessandro Rubini 
11511a087c6aSAlessandro Rubini /**
11521a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
11531a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
11541a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
11551a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
11561a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
11571a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
11581a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
11591a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
11601a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
11611a087c6aSAlessandro Rubini  *
11621a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
11631a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
11641a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
11651a087c6aSAlessandro Rubini  */
1166ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode,
11671a087c6aSAlessandro Rubini 			     struct dentry *parent,
11681a087c6aSAlessandro Rubini 			     struct debugfs_regset32 *regset)
11691a087c6aSAlessandro Rubini {
1170ae91c925SGreg Kroah-Hartman 	debugfs_create_file(name, mode, parent, regset, &fops_regset32);
11711a087c6aSAlessandro Rubini }
11721a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
11733b85e4abSHeiko Carstens 
11743b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
117598210b7fSArend van Spriel 
117698210b7fSArend van Spriel struct debugfs_devm_entry {
117798210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
117898210b7fSArend van Spriel 	struct device *dev;
117998210b7fSArend van Spriel };
118098210b7fSArend van Spriel 
118198210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
118298210b7fSArend van Spriel {
118398210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
118498210b7fSArend van Spriel 
118598210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
118698210b7fSArend van Spriel }
118798210b7fSArend van Spriel 
118898210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
118998210b7fSArend van Spriel 	.owner = THIS_MODULE,
119098210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
119198210b7fSArend van Spriel 	.release = single_release,
119298210b7fSArend van Spriel 	.read = seq_read,
119398210b7fSArend van Spriel 	.llseek = seq_lseek
119498210b7fSArend van Spriel };
119598210b7fSArend van Spriel 
119698210b7fSArend van Spriel /**
119798210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
119898210b7fSArend van Spriel  *
119998210b7fSArend van Spriel  * @dev: device related to this debugfs file.
120098210b7fSArend van Spriel  * @name: name of the debugfs file.
120198210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
120298210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
120398210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
120498210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
120598210b7fSArend van Spriel  */
12060d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name,
120798210b7fSArend van Spriel 				 struct dentry *parent,
12080d519cbfSGreg Kroah-Hartman 				 int (*read_fn)(struct seq_file *s, void *data))
120998210b7fSArend van Spriel {
121098210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
121198210b7fSArend van Spriel 
121298210b7fSArend van Spriel 	if (IS_ERR(parent))
12130d519cbfSGreg Kroah-Hartman 		return;
121498210b7fSArend van Spriel 
121598210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
121698210b7fSArend van Spriel 	if (!entry)
12170d519cbfSGreg Kroah-Hartman 		return;
121898210b7fSArend van Spriel 
121998210b7fSArend van Spriel 	entry->read = read_fn;
122098210b7fSArend van Spriel 	entry->dev = dev;
122198210b7fSArend van Spriel 
12220d519cbfSGreg Kroah-Hartman 	debugfs_create_file(name, S_IRUGO, parent, entry,
122398210b7fSArend van Spriel 			    &debugfs_devm_entry_ops);
122498210b7fSArend van Spriel }
122598210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1226