xref: /linux-6.15/fs/debugfs/file.c (revision f5cb0a7e)
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>
21cfe39442SAl Viro #include <linux/poll.h>
225496197fSDavid Howells #include <linux/security.h>
239fd4dcecSNicolai Stange 
249fd4dcecSNicolai Stange #include "internal.h"
251da177e4SLinus Torvalds 
2649d200deSNicolai Stange struct poll_table_struct;
2749d200deSNicolai Stange 
281da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
291da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	return 0;
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
351da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	return count;
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds 
409fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
411da177e4SLinus Torvalds 	.read =		default_read_file,
421da177e4SLinus Torvalds 	.write =	default_write_file,
43234e3405SStephen Boyd 	.open =		simple_open,
446038f373SArnd Bergmann 	.llseek =	noop_llseek,
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
479fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
489fd4dcecSNicolai Stange 
497c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
507c8d4698SNicolai Stange {
517c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
52055ab8e3SNicolai Stange 
537d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
547d39bc50SNicolai Stange 		/*
557d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
567d39bc50SNicolai Stange 		 * debugfs_file_get().
577d39bc50SNicolai Stange 		 */
587d39bc50SNicolai Stange 		WARN_ON(1);
597d39bc50SNicolai Stange 		return NULL;
607d39bc50SNicolai Stange 	}
617d39bc50SNicolai Stange 
627c8d4698SNicolai Stange 	return fsd->real_fops;
637c8d4698SNicolai Stange }
647c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
657c8d4698SNicolai Stange 
66e9117a5aSNicolai Stange /**
67e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
68e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
69e9117a5aSNicolai Stange  *
70e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
71e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
72e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
73e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
74e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
75e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
76e9117a5aSNicolai Stange  *
77e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
78e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
79e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
80e9117a5aSNicolai Stange  */
81e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
82e9117a5aSNicolai Stange {
837d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
847d39bc50SNicolai Stange 	void *d_fsd;
85e9117a5aSNicolai Stange 
867d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
877d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
887d39bc50SNicolai Stange 		fsd = d_fsd;
897d39bc50SNicolai Stange 	} else {
907d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
917d39bc50SNicolai Stange 		if (!fsd)
927d39bc50SNicolai Stange 			return -ENOMEM;
937d39bc50SNicolai Stange 
947d39bc50SNicolai Stange 		fsd->real_fops = (void *)((unsigned long)d_fsd &
957d39bc50SNicolai Stange 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
967d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
977d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
987d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
997d39bc50SNicolai Stange 			kfree(fsd);
1007d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
1017d39bc50SNicolai Stange 		}
1027d39bc50SNicolai Stange 	}
1037d39bc50SNicolai Stange 
1047d39bc50SNicolai Stange 	/*
1057d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1067d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1077d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1087d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1097d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1107d39bc50SNicolai Stange 	 * removers.
1117d39bc50SNicolai Stange 	 */
112e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
113e9117a5aSNicolai Stange 		return -EIO;
114e9117a5aSNicolai Stange 
115e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
116e9117a5aSNicolai Stange 		return -EIO;
117e9117a5aSNicolai Stange 
118e9117a5aSNicolai Stange 	return 0;
119e9117a5aSNicolai Stange }
120e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
121e9117a5aSNicolai Stange 
122e9117a5aSNicolai Stange /**
123e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
124e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
125e9117a5aSNicolai Stange  *          debugfs_file_get().
126e9117a5aSNicolai Stange  *
127e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
128e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
129e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
130e9117a5aSNicolai Stange  */
131e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
132e9117a5aSNicolai Stange {
1337d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
134e9117a5aSNicolai Stange 
135e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
136e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
137e9117a5aSNicolai Stange }
138e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
139e9117a5aSNicolai Stange 
1405496197fSDavid Howells /*
1415496197fSDavid Howells  * Only permit access to world-readable files when the kernel is locked down.
1425496197fSDavid Howells  * We also need to exclude any file that has ways to write or alter it as root
1435496197fSDavid Howells  * can bypass the permissions check.
1445496197fSDavid Howells  */
1455496197fSDavid Howells static bool debugfs_is_locked_down(struct inode *inode,
1465496197fSDavid Howells 				   struct file *filp,
1475496197fSDavid Howells 				   const struct file_operations *real_fops)
1485496197fSDavid Howells {
1495496197fSDavid Howells 	if ((inode->i_mode & 07777) == 0444 &&
1505496197fSDavid Howells 	    !(filp->f_mode & FMODE_WRITE) &&
1515496197fSDavid Howells 	    !real_fops->unlocked_ioctl &&
1525496197fSDavid Howells 	    !real_fops->compat_ioctl &&
1535496197fSDavid Howells 	    !real_fops->mmap)
1545496197fSDavid Howells 		return false;
1555496197fSDavid Howells 
1565496197fSDavid Howells 	return security_locked_down(LOCKDOWN_DEBUGFS);
1575496197fSDavid Howells }
1585496197fSDavid Howells 
1599fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1609fd4dcecSNicolai Stange {
16169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
1629fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
1637d39bc50SNicolai Stange 	int r;
1649fd4dcecSNicolai Stange 
1657d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
1667d39bc50SNicolai Stange 	if (r)
1677d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
1689fd4dcecSNicolai Stange 
16986f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
1705496197fSDavid Howells 
1715496197fSDavid Howells 	r = debugfs_is_locked_down(inode, filp, real_fops);
1725496197fSDavid Howells 	if (r)
1735496197fSDavid Howells 		goto out;
1745496197fSDavid Howells 
1759fd4dcecSNicolai Stange 	real_fops = fops_get(real_fops);
1769fd4dcecSNicolai Stange 	if (!real_fops) {
1779fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1789fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1799fd4dcecSNicolai Stange 			dentry);
1809fd4dcecSNicolai Stange 		r = -ENXIO;
1819fd4dcecSNicolai Stange 		goto out;
1829fd4dcecSNicolai Stange 	}
1839fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1849fd4dcecSNicolai Stange 
1859fd4dcecSNicolai Stange 	if (real_fops->open)
1869fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1879fd4dcecSNicolai Stange 
1889fd4dcecSNicolai Stange out:
18969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
1909fd4dcecSNicolai Stange 	return r;
1919fd4dcecSNicolai Stange }
1929fd4dcecSNicolai Stange 
1939fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
1949fd4dcecSNicolai Stange 	.open = open_proxy_open,
1959fd4dcecSNicolai Stange };
1969fd4dcecSNicolai Stange 
19749d200deSNicolai Stange #define PROTO(args...) args
19849d200deSNicolai Stange #define ARGS(args...) args
19949d200deSNicolai Stange 
20049d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
20149d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
20249d200deSNicolai Stange {									\
20369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
204154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
20549d200deSNicolai Stange 	ret_type r;							\
20649d200deSNicolai Stange 									\
20769d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
20869d29f9eSNicolai Stange 	if (unlikely(r))						\
20969d29f9eSNicolai Stange 		return r;						\
210154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
21149d200deSNicolai Stange 	r = real_fops->name(args);					\
21269d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
21349d200deSNicolai Stange 	return r;							\
21449d200deSNicolai Stange }
21549d200deSNicolai Stange 
21649d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
21749d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
21849d200deSNicolai Stange 		ARGS(filp, offset, whence));
21949d200deSNicolai Stange 
22049d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
22149d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
22249d200deSNicolai Stange 			loff_t *ppos),
22349d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
22449d200deSNicolai Stange 
22549d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
22649d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
22749d200deSNicolai Stange 			loff_t *ppos),
22849d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
22949d200deSNicolai Stange 
23049d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
23149d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
23249d200deSNicolai Stange 		ARGS(filp, cmd, arg));
23349d200deSNicolai Stange 
234076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
23549d200deSNicolai Stange 				struct poll_table_struct *wait)
23649d200deSNicolai Stange {
23769d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
238e6c8adcaSAl Viro 	__poll_t r = 0;
239154b9d75SNicolai Stange 	const struct file_operations *real_fops;
24049d200deSNicolai Stange 
24169d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
242a9a08845SLinus Torvalds 		return EPOLLHUP;
24349d200deSNicolai Stange 
244154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
24549d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
24669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
24749d200deSNicolai Stange 	return r;
24849d200deSNicolai Stange }
24949d200deSNicolai Stange 
25049d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
25149d200deSNicolai Stange {
25249d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
25386f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
25449d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
25549d200deSNicolai Stange 	int r = 0;
25649d200deSNicolai Stange 
25749d200deSNicolai Stange 	/*
25849d200deSNicolai Stange 	 * We must not protect this against removal races here: the
25949d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
26049d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
26149d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
26249d200deSNicolai Stange 	 */
26349d200deSNicolai Stange 	if (real_fops->release)
26449d200deSNicolai Stange 		r = real_fops->release(inode, filp);
26549d200deSNicolai Stange 
26649d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
26749d200deSNicolai Stange 	kfree((void *)proxy_fops);
26849d200deSNicolai Stange 	fops_put(real_fops);
269a1a9e5d2SEric Engestrom 	return r;
27049d200deSNicolai Stange }
27149d200deSNicolai Stange 
27249d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
27349d200deSNicolai Stange 				const struct file_operations *real_fops)
27449d200deSNicolai Stange {
27549d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
27649d200deSNicolai Stange 	if (real_fops->llseek)
27749d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
27849d200deSNicolai Stange 	if (real_fops->read)
27949d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
28049d200deSNicolai Stange 	if (real_fops->write)
28149d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
28249d200deSNicolai Stange 	if (real_fops->poll)
28349d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
28449d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
28549d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
28649d200deSNicolai Stange }
28749d200deSNicolai Stange 
28849d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
28949d200deSNicolai Stange {
29069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
29149d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
29249d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
2937d39bc50SNicolai Stange 	int r;
29449d200deSNicolai Stange 
2957d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
2967d39bc50SNicolai Stange 	if (r)
2977d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
29849d200deSNicolai Stange 
29986f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
3005496197fSDavid Howells 
3015496197fSDavid Howells 	r = debugfs_is_locked_down(inode, filp, real_fops);
3025496197fSDavid Howells 	if (r)
3035496197fSDavid Howells 		goto out;
3045496197fSDavid Howells 
30549d200deSNicolai Stange 	real_fops = fops_get(real_fops);
30649d200deSNicolai Stange 	if (!real_fops) {
30749d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
30849d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
30949d200deSNicolai Stange 			dentry);
31049d200deSNicolai Stange 		r = -ENXIO;
31149d200deSNicolai Stange 		goto out;
31249d200deSNicolai Stange 	}
31349d200deSNicolai Stange 
31449d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
31549d200deSNicolai Stange 	if (!proxy_fops) {
31649d200deSNicolai Stange 		r = -ENOMEM;
31749d200deSNicolai Stange 		goto free_proxy;
31849d200deSNicolai Stange 	}
31949d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
32049d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
32149d200deSNicolai Stange 
32249d200deSNicolai Stange 	if (real_fops->open) {
32349d200deSNicolai Stange 		r = real_fops->open(inode, filp);
324b10e3e90SNicolai Stange 		if (r) {
325b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
326b10e3e90SNicolai Stange 			goto free_proxy;
327b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
32849d200deSNicolai Stange 			/* No protection against file removal anymore. */
32949d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
33049d200deSNicolai Stange 				dentry);
33149d200deSNicolai Stange 			goto free_proxy;
33249d200deSNicolai Stange 		}
33349d200deSNicolai Stange 	}
33449d200deSNicolai Stange 
33549d200deSNicolai Stange 	goto out;
33649d200deSNicolai Stange free_proxy:
33749d200deSNicolai Stange 	kfree(proxy_fops);
33849d200deSNicolai Stange 	fops_put(real_fops);
33949d200deSNicolai Stange out:
34069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
34149d200deSNicolai Stange 	return r;
34249d200deSNicolai Stange }
34349d200deSNicolai Stange 
34449d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
34549d200deSNicolai Stange 	.open = full_proxy_open,
34649d200deSNicolai Stange };
34749d200deSNicolai Stange 
348c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
349c6468808SNicolai Stange 			size_t len, loff_t *ppos)
350c6468808SNicolai Stange {
35169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
352c6468808SNicolai Stange 	ssize_t ret;
353c6468808SNicolai Stange 
35469d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
35569d29f9eSNicolai Stange 	if (unlikely(ret))
35669d29f9eSNicolai Stange 		return ret;
357c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
35869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
359c6468808SNicolai Stange 	return ret;
360c6468808SNicolai Stange }
361c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
362c6468808SNicolai Stange 
363c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
364c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
365c6468808SNicolai Stange {
36669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
367c6468808SNicolai Stange 	ssize_t ret;
368c6468808SNicolai Stange 
36969d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
37069d29f9eSNicolai Stange 	if (unlikely(ret))
37169d29f9eSNicolai Stange 		return ret;
372c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
37369d29f9eSNicolai Stange 	debugfs_file_put(dentry);
374c6468808SNicolai Stange 	return ret;
375c6468808SNicolai Stange }
376c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
377c6468808SNicolai Stange 
3784909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3794909f168SNicolai Stange 					struct dentry *parent, void *value,
3804909f168SNicolai Stange 					const struct file_operations *fops,
3814909f168SNicolai Stange 					const struct file_operations *fops_ro,
3824909f168SNicolai Stange 					const struct file_operations *fops_wo)
3834909f168SNicolai Stange {
3844909f168SNicolai Stange 	/* if there are no write bits set, make read only */
3854909f168SNicolai Stange 	if (!(mode & S_IWUGO))
3864909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3874909f168SNicolai Stange 						fops_ro);
3884909f168SNicolai Stange 	/* if there are no read bits set, make write only */
3894909f168SNicolai Stange 	if (!(mode & S_IRUGO))
3904909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3914909f168SNicolai Stange 						fops_wo);
3924909f168SNicolai Stange 
3934909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
3944909f168SNicolai Stange }
3954909f168SNicolai Stange 
3968b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
397acaefc25SArnd Bergmann {
398acaefc25SArnd Bergmann 	*(u8 *)data = val;
3998b88b099SChristoph Hellwig 	return 0;
400acaefc25SArnd Bergmann }
4018b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
402acaefc25SArnd Bergmann {
4038b88b099SChristoph Hellwig 	*val = *(u8 *)data;
4048b88b099SChristoph Hellwig 	return 0;
405acaefc25SArnd Bergmann }
4064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
4074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
4084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /**
4116468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
4121da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4131da177e4SLinus Torvalds  * @mode: the permission that the file should have
4141da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4156468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4161da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4171da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4181da177e4SLinus Torvalds  *         from.
4191da177e4SLinus Torvalds  *
4201da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4211da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4221da177e4SLinus Torvalds  * set, it can be read from, and written to.
4231da177e4SLinus Torvalds  */
4249655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
4259655ac4aSGreg Kroah-Hartman 		       u8 *value)
4261da177e4SLinus Torvalds {
4279655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
428b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
4311da177e4SLinus Torvalds 
4328b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
433acaefc25SArnd Bergmann {
434acaefc25SArnd Bergmann 	*(u16 *)data = val;
4358b88b099SChristoph Hellwig 	return 0;
436acaefc25SArnd Bergmann }
4378b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
438acaefc25SArnd Bergmann {
4398b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4408b88b099SChristoph Hellwig 	return 0;
441acaefc25SArnd Bergmann }
4424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
4434909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
4444909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
445acaefc25SArnd Bergmann 
4461da177e4SLinus Torvalds /**
4476468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4481da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4491da177e4SLinus Torvalds  * @mode: the permission that the file should have
4501da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4516468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4521da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4531da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4541da177e4SLinus Torvalds  *         from.
4551da177e4SLinus Torvalds  *
4561da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4571da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4581da177e4SLinus Torvalds  * set, it can be read from, and written to.
4591da177e4SLinus Torvalds  */
460313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
461313f5dbbSGreg Kroah-Hartman 			u16 *value)
4621da177e4SLinus Torvalds {
463313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
464b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4671da177e4SLinus Torvalds 
4688b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
469acaefc25SArnd Bergmann {
470acaefc25SArnd Bergmann 	*(u32 *)data = val;
4718b88b099SChristoph Hellwig 	return 0;
472acaefc25SArnd Bergmann }
4738b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
474acaefc25SArnd Bergmann {
4758b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4768b88b099SChristoph Hellwig 	return 0;
477acaefc25SArnd Bergmann }
4784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4794909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
481acaefc25SArnd Bergmann 
4821da177e4SLinus Torvalds /**
4836468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4841da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4851da177e4SLinus Torvalds  * @mode: the permission that the file should have
4861da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4876468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4881da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4891da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4901da177e4SLinus Torvalds  *         from.
4911da177e4SLinus Torvalds  *
4921da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4931da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4941da177e4SLinus Torvalds  * set, it can be read from, and written to.
4951da177e4SLinus Torvalds  *
4961da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4971da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4981da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4999abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
5009abb2499SRonald Tschalär  * returned.
5011da177e4SLinus Torvalds  *
5029abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
5039abb2499SRonald Tschalär  * be returned.
5041da177e4SLinus Torvalds  */
505f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
5061da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
5071da177e4SLinus Torvalds {
5084909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
509b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
5121da177e4SLinus Torvalds 
5138b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
5148447891fSMichael Ellerman {
5158447891fSMichael Ellerman 	*(u64 *)data = val;
5168b88b099SChristoph Hellwig 	return 0;
5178447891fSMichael Ellerman }
5188447891fSMichael Ellerman 
5198b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
5208447891fSMichael Ellerman {
5218b88b099SChristoph Hellwig 	*val = *(u64 *)data;
5228b88b099SChristoph Hellwig 	return 0;
5238447891fSMichael Ellerman }
5244909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
5254909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
5264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5278447891fSMichael Ellerman 
5288447891fSMichael Ellerman /**
5298447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5308447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5318447891fSMichael Ellerman  * @mode: the permission that the file should have
5328447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5338447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5348447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5358447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5368447891fSMichael Ellerman  *         from.
5378447891fSMichael Ellerman  *
5388447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5398447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5408447891fSMichael Ellerman  * set, it can be read from, and written to.
5418447891fSMichael Ellerman  */
542ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
543ad26221fSGreg Kroah-Hartman 			u64 *value)
5448447891fSMichael Ellerman {
545ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
546b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5478447891fSMichael Ellerman }
5488447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5498447891fSMichael Ellerman 
550c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
551c23fe831SViresh Kumar {
552c23fe831SViresh Kumar 	*(unsigned long *)data = val;
553c23fe831SViresh Kumar 	return 0;
554c23fe831SViresh Kumar }
555c23fe831SViresh Kumar 
556c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
557c23fe831SViresh Kumar {
558c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
559c23fe831SViresh Kumar 	return 0;
560c23fe831SViresh Kumar }
5614909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5624909f168SNicolai Stange 			"%llu\n");
5634909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5644909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
565c23fe831SViresh Kumar 
566c23fe831SViresh Kumar /**
567c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
568c23fe831SViresh Kumar  * an unsigned long value.
569c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
570c23fe831SViresh Kumar  * @mode: the permission that the file should have
571c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
572c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
573c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
574c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
575c23fe831SViresh Kumar  *         from.
576c23fe831SViresh Kumar  *
577c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
578c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
579c23fe831SViresh Kumar  * set, it can be read from, and written to.
580c23fe831SViresh Kumar  *
581c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
582c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
583c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
5849abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
5859abb2499SRonald Tschalär  * returned.
586c23fe831SViresh Kumar  *
5879abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
5889abb2499SRonald Tschalär  * be returned.
589c23fe831SViresh Kumar  */
590c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
591c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
592c23fe831SViresh Kumar {
5934909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
5944909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
5954909f168SNicolai Stange 					&fops_ulong_wo);
596c23fe831SViresh Kumar }
597c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
598c23fe831SViresh Kumar 
5994909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
6014909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
6022ebefc50SRobin Getz 
6034909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
6044909f168SNicolai Stange 			"0x%04llx\n");
6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
6072ebefc50SRobin Getz 
6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
6094909f168SNicolai Stange 			"0x%08llx\n");
6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
6114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
6122ebefc50SRobin Getz 
6134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
6144909f168SNicolai Stange 			"0x%016llx\n");
6154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
6164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
61715b0beaaSHuang Ying 
618e6716b87SRandy Dunlap /*
61915b0beaaSHuang 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
620e6716b87SRandy Dunlap  *
621e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
622e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
623e6716b87SRandy Dunlap  * decimal functions.
624e6716b87SRandy Dunlap  */
625e6716b87SRandy Dunlap 
6262ebefc50SRobin Getz /**
6272ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
628e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
629e6716b87SRandy Dunlap  * @mode: the permission that the file should have
630e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
631e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
632e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
633e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
634e6716b87SRandy Dunlap  *         from.
6352ebefc50SRobin Getz  */
636f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
6372ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
6382ebefc50SRobin Getz {
6394909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
640b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6412ebefc50SRobin Getz }
6422ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6432ebefc50SRobin Getz 
644e6716b87SRandy Dunlap /**
645e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
646e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
647e6716b87SRandy Dunlap  * @mode: the permission that the file should have
648e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
649e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
650e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
651e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
652e6716b87SRandy Dunlap  *         from.
653e6716b87SRandy Dunlap  */
654e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
655e40d38f2SGreg Kroah-Hartman 			u16 *value)
6562ebefc50SRobin Getz {
657e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
658b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6592ebefc50SRobin Getz }
6602ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6612ebefc50SRobin Getz 
662e6716b87SRandy Dunlap /**
663e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
664e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
665e6716b87SRandy Dunlap  * @mode: the permission that the file should have
666e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
667e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
668e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
669e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
670e6716b87SRandy Dunlap  *         from.
671e6716b87SRandy Dunlap  */
672*f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
673*f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
6742ebefc50SRobin Getz {
675*f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
676b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6772ebefc50SRobin Getz }
6782ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6792ebefc50SRobin Getz 
68015b0beaaSHuang Ying /**
68115b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
68215b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
68315b0beaaSHuang Ying  * @mode: the permission that the file should have
68415b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
68515b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
68615b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
68715b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
68815b0beaaSHuang Ying  *         from.
68915b0beaaSHuang Ying  */
690f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
69115b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
69215b0beaaSHuang Ying {
6934909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
69482b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
69515b0beaaSHuang Ying }
69615b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
69715b0beaaSHuang Ying 
6985e078787SInaky Perez-Gonzalez 
6995e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
7005e078787SInaky Perez-Gonzalez {
7015e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
7025e078787SInaky Perez-Gonzalez 	return 0;
7035e078787SInaky Perez-Gonzalez }
7045e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
7055e078787SInaky Perez-Gonzalez {
7065e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
7075e078787SInaky Perez-Gonzalez 	return 0;
7085e078787SInaky Perez-Gonzalez }
7094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
7105e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
7114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
7124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
7135e078787SInaky Perez-Gonzalez 
7145e078787SInaky Perez-Gonzalez /**
7155e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7165e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7175e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7185e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7195e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7205e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7215e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7225e078787SInaky Perez-Gonzalez  *         from.
7235e078787SInaky Perez-Gonzalez  */
7248e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
7255e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
7265e078787SInaky Perez-Gonzalez {
7278e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
7288e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
7295e078787SInaky Perez-Gonzalez }
7305e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7315e078787SInaky Perez-Gonzalez 
7323a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7333a76e5e0SSeth Jennings {
7343a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7353a76e5e0SSeth Jennings 	return 0;
7363a76e5e0SSeth Jennings }
7373a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7383a76e5e0SSeth Jennings {
7393a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7403a76e5e0SSeth Jennings 	return 0;
7413a76e5e0SSeth Jennings }
7424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7433a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7444909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7454909f168SNicolai Stange 			"%lld\n");
7464909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7474909f168SNicolai Stange 			"%lld\n");
7483a76e5e0SSeth Jennings 
7493a76e5e0SSeth Jennings /**
7503a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7513a76e5e0SSeth Jennings  * write an atomic_t value
7523a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7533a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7543a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7553a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7563a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7573a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7583a76e5e0SSeth Jennings  *         from.
7593a76e5e0SSeth Jennings  */
7603a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
7613a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
7623a76e5e0SSeth Jennings {
7634909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7644909f168SNicolai Stange 					&fops_atomic_t, &fops_atomic_t_ro,
7654909f168SNicolai Stange 					&fops_atomic_t_wo);
7663a76e5e0SSeth Jennings }
7673a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7685e078787SInaky Perez-Gonzalez 
7690642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7701da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7711da177e4SLinus Torvalds {
7721da177e4SLinus Torvalds 	char buf[3];
7734d45f797SNicolai Stange 	bool val;
77469d29f9eSNicolai Stange 	int r;
77569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7761da177e4SLinus Torvalds 
77769d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
77869d29f9eSNicolai Stange 	if (unlikely(r))
7794d45f797SNicolai Stange 		return r;
78069d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
78169d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7824d45f797SNicolai Stange 
7834d45f797SNicolai Stange 	if (val)
7841da177e4SLinus Torvalds 		buf[0] = 'Y';
7851da177e4SLinus Torvalds 	else
7861da177e4SLinus Torvalds 		buf[0] = 'N';
7871da177e4SLinus Torvalds 	buf[1] = '\n';
7881da177e4SLinus Torvalds 	buf[2] = 0x00;
7891da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7901da177e4SLinus Torvalds }
7910642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7921da177e4SLinus Torvalds 
7930642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7941da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7951da177e4SLinus Torvalds {
7968705b48eSJonathan Cameron 	bool bv;
79769d29f9eSNicolai Stange 	int r;
798621a5f7aSViresh Kumar 	bool *val = file->private_data;
79969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
8001da177e4SLinus Torvalds 
801964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
802964f8363SAndy Shevchenko 	if (!r) {
80369d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
80469d29f9eSNicolai Stange 		if (unlikely(r))
8054d45f797SNicolai Stange 			return r;
80669d29f9eSNicolai Stange 		*val = bv;
80769d29f9eSNicolai Stange 		debugfs_file_put(dentry);
8084d45f797SNicolai Stange 	}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	return count;
8111da177e4SLinus Torvalds }
8120642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
8131da177e4SLinus Torvalds 
8144b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
8150642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
8160642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
817234e3405SStephen Boyd 	.open =		simple_open,
8186038f373SArnd Bergmann 	.llseek =	default_llseek,
8191da177e4SLinus Torvalds };
8201da177e4SLinus Torvalds 
8216713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
8226713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8236713e8fbSStephen Boyd 	.open =		simple_open,
8246713e8fbSStephen Boyd 	.llseek =	default_llseek,
8256713e8fbSStephen Boyd };
8266713e8fbSStephen Boyd 
8276713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8286713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8296713e8fbSStephen Boyd 	.open =		simple_open,
8306713e8fbSStephen Boyd 	.llseek =	default_llseek,
8316713e8fbSStephen Boyd };
8326713e8fbSStephen Boyd 
8331da177e4SLinus Torvalds /**
8346468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8351da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8361da177e4SLinus Torvalds  * @mode: the permission that the file should have
8371da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8386468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8391da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8401da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8411da177e4SLinus Torvalds  *         from.
8421da177e4SLinus Torvalds  *
8431da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8441da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8451da177e4SLinus Torvalds  * set, it can be read from, and written to.
8461da177e4SLinus Torvalds  *
8471da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8481da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8491da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
8509abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
8519abb2499SRonald Tschalär  * returned.
8521da177e4SLinus Torvalds  *
8539abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
8549abb2499SRonald Tschalär  * be returned.
8551da177e4SLinus Torvalds  */
856f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
857621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8581da177e4SLinus Torvalds {
8594d45f797SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8606713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8631da177e4SLinus Torvalds 
864dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
865dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
866dd308bc3SMichael Ellerman {
867dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
86869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
86983b711cbSNicolai Stange 	ssize_t r;
87083b711cbSNicolai Stange 
87169d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
87269d29f9eSNicolai Stange 	if (unlikely(r))
87369d29f9eSNicolai Stange 		return r;
87483b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
875dd308bc3SMichael Ellerman 				blob->size);
87669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
87783b711cbSNicolai Stange 	return r;
878dd308bc3SMichael Ellerman }
879dd308bc3SMichael Ellerman 
88000977a59SArjan van de Ven static const struct file_operations fops_blob = {
881dd308bc3SMichael Ellerman 	.read =		read_file_blob,
882234e3405SStephen Boyd 	.open =		simple_open,
8836038f373SArnd Bergmann 	.llseek =	default_llseek,
884dd308bc3SMichael Ellerman };
885dd308bc3SMichael Ellerman 
886dd308bc3SMichael Ellerman /**
887400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
888dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
889dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
890dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
8916468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
892dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
893dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
894dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
895dd308bc3SMichael Ellerman  *
896dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
897dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
898dd308bc3SMichael Ellerman  * read from. Writing is not supported.
899dd308bc3SMichael Ellerman  *
900dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
901dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
902dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
9039abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
9049abb2499SRonald Tschalär  * returned.
905dd308bc3SMichael Ellerman  *
9069abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
9079abb2499SRonald Tschalär  * be returned.
908dd308bc3SMichael Ellerman  */
909f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
910dd308bc3SMichael Ellerman 				   struct dentry *parent,
911dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
912dd308bc3SMichael Ellerman {
91383b711cbSNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
914dd308bc3SMichael Ellerman }
915dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
9161a087c6aSAlessandro Rubini 
9179fe2a701SSrivatsa Vaddagiri struct array_data {
9189fe2a701SSrivatsa Vaddagiri 	void *array;
9199fe2a701SSrivatsa Vaddagiri 	u32 elements;
9209fe2a701SSrivatsa Vaddagiri };
9219fe2a701SSrivatsa Vaddagiri 
922e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
923e05e279eSLinus Torvalds 			       u32 *array, int array_size)
9249fe2a701SSrivatsa Vaddagiri {
9259fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
9269fe2a701SSrivatsa Vaddagiri 
927e05e279eSLinus Torvalds 	while (--array_size >= 0) {
9289fe2a701SSrivatsa Vaddagiri 		size_t len;
929e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
9309fe2a701SSrivatsa Vaddagiri 
931e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
9329fe2a701SSrivatsa Vaddagiri 		ret += len;
9339fe2a701SSrivatsa Vaddagiri 
9349fe2a701SSrivatsa Vaddagiri 		buf += len;
9359fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
9369fe2a701SSrivatsa Vaddagiri 	}
9379fe2a701SSrivatsa Vaddagiri 	return ret;
9389fe2a701SSrivatsa Vaddagiri }
9399fe2a701SSrivatsa Vaddagiri 
94036048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
9419fe2a701SSrivatsa Vaddagiri {
9429fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
943e05e279eSLinus Torvalds 	int size, elements = data->elements;
944e05e279eSLinus Torvalds 	char *buf;
9459fe2a701SSrivatsa Vaddagiri 
946e05e279eSLinus Torvalds 	/*
947e05e279eSLinus Torvalds 	 * Max size:
948e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
949e05e279eSLinus Torvalds 	 *  - terminating NUL character
950e05e279eSLinus Torvalds 	 */
951e05e279eSLinus Torvalds 	size = elements*11;
952e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
953e05e279eSLinus Torvalds 	if (!buf)
95436048853SDavid Rientjes 		return -ENOMEM;
955e05e279eSLinus Torvalds 	buf[size] = 0;
956e05e279eSLinus Torvalds 
957e05e279eSLinus Torvalds 	file->private_data = buf;
958e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
959e05e279eSLinus Torvalds 
96036048853SDavid Rientjes 	return nonseekable_open(inode, file);
9619fe2a701SSrivatsa Vaddagiri }
9629fe2a701SSrivatsa Vaddagiri 
96336048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
96436048853SDavid Rientjes 			      loff_t *ppos)
96536048853SDavid Rientjes {
96636048853SDavid Rientjes 	size_t size = strlen(file->private_data);
9679fe2a701SSrivatsa Vaddagiri 
9689fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
9699fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
9709fe2a701SSrivatsa Vaddagiri }
9719fe2a701SSrivatsa Vaddagiri 
9729fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
9739fe2a701SSrivatsa Vaddagiri {
9749fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
9759fe2a701SSrivatsa Vaddagiri 
9769fe2a701SSrivatsa Vaddagiri 	return 0;
9779fe2a701SSrivatsa Vaddagiri }
9789fe2a701SSrivatsa Vaddagiri 
9799fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
9809fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
9819fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
9829fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
9839fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
9849fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
9859fe2a701SSrivatsa Vaddagiri };
9869fe2a701SSrivatsa Vaddagiri 
9879fe2a701SSrivatsa Vaddagiri /**
9889fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
9899fe2a701SSrivatsa Vaddagiri  * array.
9909fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
9919fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
9929fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
9939fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
9949fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
9959fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
9969fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
9979fe2a701SSrivatsa Vaddagiri  *
9989fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
9999fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
10009fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
10019fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
10029fe2a701SSrivatsa Vaddagiri  */
1003c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1004c9c2c27dSGreg Kroah-Hartman 			      struct dentry *parent, u32 *array, u32 elements)
10059fe2a701SSrivatsa Vaddagiri {
10069fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
10079fe2a701SSrivatsa Vaddagiri 
10089fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
1009c9c2c27dSGreg Kroah-Hartman 		return;
10109fe2a701SSrivatsa Vaddagiri 
10119fe2a701SSrivatsa Vaddagiri 	data->array = array;
10129fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
10139fe2a701SSrivatsa Vaddagiri 
1014c9c2c27dSGreg Kroah-Hartman 	debugfs_create_file_unsafe(name, mode, parent, data, &u32_array_fops);
10159fe2a701SSrivatsa Vaddagiri }
10169fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10179fe2a701SSrivatsa Vaddagiri 
10183b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
10193b85e4abSHeiko Carstens 
10201a087c6aSAlessandro Rubini /*
10211a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
10221a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
10231a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
10241a087c6aSAlessandro Rubini  */
10251a087c6aSAlessandro Rubini 
10261a087c6aSAlessandro Rubini /**
10271a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10281a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10291a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1030b5763accSRandy Dunlap  * @nregs: the length of the above array
10311a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10321a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
10331a087c6aSAlessandro Rubini  *
10341a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
10351a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
10361a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
10371a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
10381a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
10391a087c6aSAlessandro Rubini  * for example configuration of dma channels
10401a087c6aSAlessandro Rubini  */
10419761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
10421a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
10431a087c6aSAlessandro Rubini {
10449761536eSJoe Perches 	int i;
10451a087c6aSAlessandro Rubini 
10461a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
10471a087c6aSAlessandro Rubini 		if (prefix)
10489761536eSJoe Perches 			seq_printf(s, "%s", prefix);
10499761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
10505858441cSDan Carpenter 			   readl(base + regs->offset));
10519761536eSJoe Perches 		if (seq_has_overflowed(s))
10529761536eSJoe Perches 			break;
10531a087c6aSAlessandro Rubini 	}
10541a087c6aSAlessandro Rubini }
10551a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
10561a087c6aSAlessandro Rubini 
10571a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
10581a087c6aSAlessandro Rubini {
10591a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
10601a087c6aSAlessandro Rubini 
10611a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
10621a087c6aSAlessandro Rubini 	return 0;
10631a087c6aSAlessandro Rubini }
10641a087c6aSAlessandro Rubini 
10651a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
10661a087c6aSAlessandro Rubini {
10671a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
10681a087c6aSAlessandro Rubini }
10691a087c6aSAlessandro Rubini 
10701a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
10711a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
10721a087c6aSAlessandro Rubini 	.read =		seq_read,
10731a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
10741a087c6aSAlessandro Rubini 	.release =	single_release,
10751a087c6aSAlessandro Rubini };
10761a087c6aSAlessandro Rubini 
10771a087c6aSAlessandro Rubini /**
10781a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
10791a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
10801a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
10811a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
10821a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
10831a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
10841a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
10851a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
10861a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
10871a087c6aSAlessandro Rubini  *
10881a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
10891a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
10901a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
10911a087c6aSAlessandro Rubini  *
10921a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
10931a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
10941a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
10959abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
10969abb2499SRonald Tschalär  * returned.
10971a087c6aSAlessandro Rubini  *
10989abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
10999abb2499SRonald Tschalär  * be returned.
11001a087c6aSAlessandro Rubini  */
110188187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
11021a087c6aSAlessandro Rubini 				       struct dentry *parent,
11031a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
11041a087c6aSAlessandro Rubini {
11051a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
11061a087c6aSAlessandro Rubini }
11071a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
11083b85e4abSHeiko Carstens 
11093b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
111098210b7fSArend van Spriel 
111198210b7fSArend van Spriel struct debugfs_devm_entry {
111298210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
111398210b7fSArend van Spriel 	struct device *dev;
111498210b7fSArend van Spriel };
111598210b7fSArend van Spriel 
111698210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
111798210b7fSArend van Spriel {
111898210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
111998210b7fSArend van Spriel 
112098210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
112198210b7fSArend van Spriel }
112298210b7fSArend van Spriel 
112398210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
112498210b7fSArend van Spriel 	.owner = THIS_MODULE,
112598210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
112698210b7fSArend van Spriel 	.release = single_release,
112798210b7fSArend van Spriel 	.read = seq_read,
112898210b7fSArend van Spriel 	.llseek = seq_lseek
112998210b7fSArend van Spriel };
113098210b7fSArend van Spriel 
113198210b7fSArend van Spriel /**
113298210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
113398210b7fSArend van Spriel  *
113498210b7fSArend van Spriel  * @dev: device related to this debugfs file.
113598210b7fSArend van Spriel  * @name: name of the debugfs file.
113698210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
113798210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
113898210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
113998210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
114098210b7fSArend van Spriel  */
114198210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
114298210b7fSArend van Spriel 					   struct dentry *parent,
114398210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
114498210b7fSArend van Spriel 							  void *data))
114598210b7fSArend van Spriel {
114698210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
114798210b7fSArend van Spriel 
114898210b7fSArend van Spriel 	if (IS_ERR(parent))
114998210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
115098210b7fSArend van Spriel 
115198210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
115298210b7fSArend van Spriel 	if (!entry)
115398210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
115498210b7fSArend van Spriel 
115598210b7fSArend van Spriel 	entry->read = read_fn;
115698210b7fSArend van Spriel 	entry->dev = dev;
115798210b7fSArend van Spriel 
115898210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
115998210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
116098210b7fSArend van Spriel }
116198210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
116298210b7fSArend van Spriel 
1163