xref: /linux-6.15/fs/debugfs/file.c (revision 7d39bc50)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
51da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
91da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
12e1b4fc7aSMauro Carvalho Chehab  *  See Documentation/filesystems/ for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181a087c6aSAlessandro Rubini #include <linux/seq_file.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/debugfs.h>
2103e099fbSAlessandro Rubini #include <linux/io.h>
229fe2a701SSrivatsa Vaddagiri #include <linux/slab.h>
233a76e5e0SSeth Jennings #include <linux/atomic.h>
2498210b7fSArend van Spriel #include <linux/device.h>
2549d200deSNicolai Stange #include <asm/poll.h>
269fd4dcecSNicolai Stange 
279fd4dcecSNicolai Stange #include "internal.h"
281da177e4SLinus Torvalds 
2949d200deSNicolai Stange struct poll_table_struct;
3049d200deSNicolai Stange 
311da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
321da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	return 0;
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
381da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	return count;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
439fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
441da177e4SLinus Torvalds 	.read =		default_read_file,
451da177e4SLinus Torvalds 	.write =	default_write_file,
46234e3405SStephen Boyd 	.open =		simple_open,
476038f373SArnd Bergmann 	.llseek =	noop_llseek,
481da177e4SLinus Torvalds };
491da177e4SLinus Torvalds 
509fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
519fd4dcecSNicolai Stange 
527c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
537c8d4698SNicolai Stange {
547c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
55055ab8e3SNicolai Stange 
56*7d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
57*7d39bc50SNicolai Stange 		/*
58*7d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
59*7d39bc50SNicolai Stange 		 * debugfs_file_get().
60*7d39bc50SNicolai Stange 		 */
61*7d39bc50SNicolai Stange 		WARN_ON(1);
62*7d39bc50SNicolai Stange 		return NULL;
63*7d39bc50SNicolai Stange 	}
64*7d39bc50SNicolai Stange 
657c8d4698SNicolai Stange 	return fsd->real_fops;
667c8d4698SNicolai Stange }
677c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
687c8d4698SNicolai Stange 
69e9117a5aSNicolai Stange /**
70e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
71e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
72e9117a5aSNicolai Stange  *
73e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
74e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
75e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
76e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
77e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
78e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
79e9117a5aSNicolai Stange  *
80e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
81e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
82e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
83e9117a5aSNicolai Stange  */
84e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
85e9117a5aSNicolai Stange {
86*7d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
87*7d39bc50SNicolai Stange 	void *d_fsd;
88e9117a5aSNicolai Stange 
89*7d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
90*7d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
91*7d39bc50SNicolai Stange 		fsd = d_fsd;
92*7d39bc50SNicolai Stange 	} else {
93*7d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
94*7d39bc50SNicolai Stange 		if (!fsd)
95*7d39bc50SNicolai Stange 			return -ENOMEM;
96*7d39bc50SNicolai Stange 
97*7d39bc50SNicolai Stange 		fsd->real_fops = (void *)((unsigned long)d_fsd &
98*7d39bc50SNicolai Stange 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
99*7d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
100*7d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
101*7d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
102*7d39bc50SNicolai Stange 			kfree(fsd);
103*7d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
104*7d39bc50SNicolai Stange 		}
105*7d39bc50SNicolai Stange 	}
106*7d39bc50SNicolai Stange 
107*7d39bc50SNicolai Stange 	/*
108*7d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
109*7d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
110*7d39bc50SNicolai Stange 	 * __debugfs_remove_file().
111*7d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
112*7d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
113*7d39bc50SNicolai Stange 	 * removers.
114*7d39bc50SNicolai Stange 	 */
115e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
116e9117a5aSNicolai Stange 		return -EIO;
117e9117a5aSNicolai Stange 
118e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
119e9117a5aSNicolai Stange 		return -EIO;
120e9117a5aSNicolai Stange 
121e9117a5aSNicolai Stange 	return 0;
122e9117a5aSNicolai Stange }
123e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
124e9117a5aSNicolai Stange 
125e9117a5aSNicolai Stange /**
126e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
127e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
128e9117a5aSNicolai Stange  *          debugfs_file_get().
129e9117a5aSNicolai Stange  *
130e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
131e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
132e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
133e9117a5aSNicolai Stange  */
134e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
135e9117a5aSNicolai Stange {
136*7d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
137e9117a5aSNicolai Stange 
138e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
139e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
140e9117a5aSNicolai Stange }
141e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
142e9117a5aSNicolai Stange 
1439fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1449fd4dcecSNicolai Stange {
14569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
1469fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
147*7d39bc50SNicolai Stange 	int r;
1489fd4dcecSNicolai Stange 
149*7d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
150*7d39bc50SNicolai Stange 	if (r)
151*7d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
1529fd4dcecSNicolai Stange 
15386f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
1549fd4dcecSNicolai Stange 	real_fops = fops_get(real_fops);
1559fd4dcecSNicolai Stange 	if (!real_fops) {
1569fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1579fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1589fd4dcecSNicolai Stange 			dentry);
1599fd4dcecSNicolai Stange 		r = -ENXIO;
1609fd4dcecSNicolai Stange 		goto out;
1619fd4dcecSNicolai Stange 	}
1629fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1639fd4dcecSNicolai Stange 
1649fd4dcecSNicolai Stange 	if (real_fops->open)
1659fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1669fd4dcecSNicolai Stange 
1679fd4dcecSNicolai Stange out:
16869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
1699fd4dcecSNicolai Stange 	return r;
1709fd4dcecSNicolai Stange }
1719fd4dcecSNicolai Stange 
1729fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
1739fd4dcecSNicolai Stange 	.open = open_proxy_open,
1749fd4dcecSNicolai Stange };
1759fd4dcecSNicolai Stange 
17649d200deSNicolai Stange #define PROTO(args...) args
17749d200deSNicolai Stange #define ARGS(args...) args
17849d200deSNicolai Stange 
17949d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
18049d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
18149d200deSNicolai Stange {									\
18269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
183154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
18449d200deSNicolai Stange 	ret_type r;							\
18549d200deSNicolai Stange 									\
18669d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
18769d29f9eSNicolai Stange 	if (unlikely(r))						\
18869d29f9eSNicolai Stange 		return r;						\
189154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
19049d200deSNicolai Stange 	r = real_fops->name(args);					\
19169d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
19249d200deSNicolai Stange 	return r;							\
19349d200deSNicolai Stange }
19449d200deSNicolai Stange 
19549d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
19649d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
19749d200deSNicolai Stange 		ARGS(filp, offset, whence));
19849d200deSNicolai Stange 
19949d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
20049d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
20149d200deSNicolai Stange 			loff_t *ppos),
20249d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
20349d200deSNicolai Stange 
20449d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
20549d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
20649d200deSNicolai Stange 			loff_t *ppos),
20749d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
20849d200deSNicolai Stange 
20949d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
21049d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
21149d200deSNicolai Stange 		ARGS(filp, cmd, arg));
21249d200deSNicolai Stange 
21349d200deSNicolai Stange static unsigned int full_proxy_poll(struct file *filp,
21449d200deSNicolai Stange 				struct poll_table_struct *wait)
21549d200deSNicolai Stange {
21669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
21749d200deSNicolai Stange 	unsigned int r = 0;
218154b9d75SNicolai Stange 	const struct file_operations *real_fops;
21949d200deSNicolai Stange 
22069d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
22149d200deSNicolai Stange 		return POLLHUP;
22249d200deSNicolai Stange 
223154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
22449d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
22569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
22649d200deSNicolai Stange 	return r;
22749d200deSNicolai Stange }
22849d200deSNicolai Stange 
22949d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
23049d200deSNicolai Stange {
23149d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
23286f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
23349d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
23449d200deSNicolai Stange 	int r = 0;
23549d200deSNicolai Stange 
23649d200deSNicolai Stange 	/*
23749d200deSNicolai Stange 	 * We must not protect this against removal races here: the
23849d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
23949d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
24049d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
24149d200deSNicolai Stange 	 */
24249d200deSNicolai Stange 	if (real_fops->release)
24349d200deSNicolai Stange 		r = real_fops->release(inode, filp);
24449d200deSNicolai Stange 
24549d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
24649d200deSNicolai Stange 	kfree((void *)proxy_fops);
24749d200deSNicolai Stange 	fops_put(real_fops);
248a1a9e5d2SEric Engestrom 	return r;
24949d200deSNicolai Stange }
25049d200deSNicolai Stange 
25149d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
25249d200deSNicolai Stange 				const struct file_operations *real_fops)
25349d200deSNicolai Stange {
25449d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
25549d200deSNicolai Stange 	if (real_fops->llseek)
25649d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
25749d200deSNicolai Stange 	if (real_fops->read)
25849d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
25949d200deSNicolai Stange 	if (real_fops->write)
26049d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
26149d200deSNicolai Stange 	if (real_fops->poll)
26249d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
26349d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
26449d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
26549d200deSNicolai Stange }
26649d200deSNicolai Stange 
26749d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
26849d200deSNicolai Stange {
26969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
27049d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
27149d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
272*7d39bc50SNicolai Stange 	int r;
27349d200deSNicolai Stange 
274*7d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
275*7d39bc50SNicolai Stange 	if (r)
276*7d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
27749d200deSNicolai Stange 
27886f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
27949d200deSNicolai Stange 	real_fops = fops_get(real_fops);
28049d200deSNicolai Stange 	if (!real_fops) {
28149d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
28249d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
28349d200deSNicolai Stange 			dentry);
28449d200deSNicolai Stange 		r = -ENXIO;
28549d200deSNicolai Stange 		goto out;
28649d200deSNicolai Stange 	}
28749d200deSNicolai Stange 
28849d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
28949d200deSNicolai Stange 	if (!proxy_fops) {
29049d200deSNicolai Stange 		r = -ENOMEM;
29149d200deSNicolai Stange 		goto free_proxy;
29249d200deSNicolai Stange 	}
29349d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
29449d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
29549d200deSNicolai Stange 
29649d200deSNicolai Stange 	if (real_fops->open) {
29749d200deSNicolai Stange 		r = real_fops->open(inode, filp);
298b10e3e90SNicolai Stange 		if (r) {
299b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
300b10e3e90SNicolai Stange 			goto free_proxy;
301b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
30249d200deSNicolai Stange 			/* No protection against file removal anymore. */
30349d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
30449d200deSNicolai Stange 				dentry);
30549d200deSNicolai Stange 			goto free_proxy;
30649d200deSNicolai Stange 		}
30749d200deSNicolai Stange 	}
30849d200deSNicolai Stange 
30949d200deSNicolai Stange 	goto out;
31049d200deSNicolai Stange free_proxy:
31149d200deSNicolai Stange 	kfree(proxy_fops);
31249d200deSNicolai Stange 	fops_put(real_fops);
31349d200deSNicolai Stange out:
31469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
31549d200deSNicolai Stange 	return r;
31649d200deSNicolai Stange }
31749d200deSNicolai Stange 
31849d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
31949d200deSNicolai Stange 	.open = full_proxy_open,
32049d200deSNicolai Stange };
32149d200deSNicolai Stange 
322c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
323c6468808SNicolai Stange 			size_t len, loff_t *ppos)
324c6468808SNicolai Stange {
32569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
326c6468808SNicolai Stange 	ssize_t ret;
327c6468808SNicolai Stange 
32869d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
32969d29f9eSNicolai Stange 	if (unlikely(ret))
33069d29f9eSNicolai Stange 		return ret;
331c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
33269d29f9eSNicolai Stange 	debugfs_file_put(dentry);
333c6468808SNicolai Stange 	return ret;
334c6468808SNicolai Stange }
335c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
336c6468808SNicolai Stange 
337c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
338c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
339c6468808SNicolai Stange {
34069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
341c6468808SNicolai Stange 	ssize_t ret;
342c6468808SNicolai Stange 
34369d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
34469d29f9eSNicolai Stange 	if (unlikely(ret))
34569d29f9eSNicolai Stange 		return ret;
346c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
34769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
348c6468808SNicolai Stange 	return ret;
349c6468808SNicolai Stange }
350c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
351c6468808SNicolai Stange 
3524909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3534909f168SNicolai Stange 					struct dentry *parent, void *value,
3544909f168SNicolai Stange 					const struct file_operations *fops,
3554909f168SNicolai Stange 					const struct file_operations *fops_ro,
3564909f168SNicolai Stange 					const struct file_operations *fops_wo)
3574909f168SNicolai Stange {
3584909f168SNicolai Stange 	/* if there are no write bits set, make read only */
3594909f168SNicolai Stange 	if (!(mode & S_IWUGO))
3604909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3614909f168SNicolai Stange 						fops_ro);
3624909f168SNicolai Stange 	/* if there are no read bits set, make write only */
3634909f168SNicolai Stange 	if (!(mode & S_IRUGO))
3644909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3654909f168SNicolai Stange 						fops_wo);
3664909f168SNicolai Stange 
3674909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
3684909f168SNicolai Stange }
3694909f168SNicolai Stange 
3708b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
371acaefc25SArnd Bergmann {
372acaefc25SArnd Bergmann 	*(u8 *)data = val;
3738b88b099SChristoph Hellwig 	return 0;
374acaefc25SArnd Bergmann }
3758b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
376acaefc25SArnd Bergmann {
3778b88b099SChristoph Hellwig 	*val = *(u8 *)data;
3788b88b099SChristoph Hellwig 	return 0;
379acaefc25SArnd Bergmann }
3804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
3814909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
3824909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds /**
3856468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
3861da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3871da177e4SLinus Torvalds  * @mode: the permission that the file should have
3881da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3896468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3901da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3911da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3921da177e4SLinus Torvalds  *         from.
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3951da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3961da177e4SLinus Torvalds  * set, it can be read from, and written to.
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3991da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4001da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4016468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4021da177e4SLinus Torvalds  *
4036468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4041da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4056468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4061da177e4SLinus Torvalds  * code.
4071da177e4SLinus Torvalds  */
408f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode,
4091da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
4101da177e4SLinus Torvalds {
4114909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
412b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
4151da177e4SLinus Torvalds 
4168b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
417acaefc25SArnd Bergmann {
418acaefc25SArnd Bergmann 	*(u16 *)data = val;
4198b88b099SChristoph Hellwig 	return 0;
420acaefc25SArnd Bergmann }
4218b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
422acaefc25SArnd Bergmann {
4238b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4248b88b099SChristoph Hellwig 	return 0;
425acaefc25SArnd Bergmann }
4264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
4274909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
4284909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
429acaefc25SArnd Bergmann 
4301da177e4SLinus Torvalds /**
4316468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4321da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4331da177e4SLinus Torvalds  * @mode: the permission that the file should have
4341da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4356468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4361da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4371da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4381da177e4SLinus Torvalds  *         from.
4391da177e4SLinus Torvalds  *
4401da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4411da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4421da177e4SLinus Torvalds  * set, it can be read from, and written to.
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4451da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4461da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4476468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4481da177e4SLinus Torvalds  *
4496468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4501da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4516468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4521da177e4SLinus Torvalds  * code.
4531da177e4SLinus Torvalds  */
454f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode,
4551da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
4561da177e4SLinus Torvalds {
4574909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
458b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4611da177e4SLinus Torvalds 
4628b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
463acaefc25SArnd Bergmann {
464acaefc25SArnd Bergmann 	*(u32 *)data = val;
4658b88b099SChristoph Hellwig 	return 0;
466acaefc25SArnd Bergmann }
4678b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
468acaefc25SArnd Bergmann {
4698b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4708b88b099SChristoph Hellwig 	return 0;
471acaefc25SArnd Bergmann }
4724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
475acaefc25SArnd Bergmann 
4761da177e4SLinus Torvalds /**
4776468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4781da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4791da177e4SLinus Torvalds  * @mode: the permission that the file should have
4801da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4816468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4821da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4831da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4841da177e4SLinus Torvalds  *         from.
4851da177e4SLinus Torvalds  *
4861da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4871da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4881da177e4SLinus Torvalds  * set, it can be read from, and written to.
4891da177e4SLinus Torvalds  *
4901da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4911da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4921da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4936468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4941da177e4SLinus Torvalds  *
4956468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4961da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4976468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4981da177e4SLinus Torvalds  * code.
4991da177e4SLinus Torvalds  */
500f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
5011da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
5021da177e4SLinus Torvalds {
5034909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
504b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
5071da177e4SLinus Torvalds 
5088b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
5098447891fSMichael Ellerman {
5108447891fSMichael Ellerman 	*(u64 *)data = val;
5118b88b099SChristoph Hellwig 	return 0;
5128447891fSMichael Ellerman }
5138447891fSMichael Ellerman 
5148b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
5158447891fSMichael Ellerman {
5168b88b099SChristoph Hellwig 	*val = *(u64 *)data;
5178b88b099SChristoph Hellwig 	return 0;
5188447891fSMichael Ellerman }
5194909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
5204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
5214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5228447891fSMichael Ellerman 
5238447891fSMichael Ellerman /**
5248447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5258447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5268447891fSMichael Ellerman  * @mode: the permission that the file should have
5278447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5288447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5298447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5308447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5318447891fSMichael Ellerman  *         from.
5328447891fSMichael Ellerman  *
5338447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5348447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5358447891fSMichael Ellerman  * set, it can be read from, and written to.
5368447891fSMichael Ellerman  *
5378447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
5388447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
5398447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
5408447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
5418447891fSMichael Ellerman  *
5428447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
5438447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
5448447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
5458447891fSMichael Ellerman  * code.
5468447891fSMichael Ellerman  */
547f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode,
5488447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
5498447891fSMichael Ellerman {
5504909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
551b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5528447891fSMichael Ellerman }
5538447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5548447891fSMichael Ellerman 
555c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
556c23fe831SViresh Kumar {
557c23fe831SViresh Kumar 	*(unsigned long *)data = val;
558c23fe831SViresh Kumar 	return 0;
559c23fe831SViresh Kumar }
560c23fe831SViresh Kumar 
561c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
562c23fe831SViresh Kumar {
563c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
564c23fe831SViresh Kumar 	return 0;
565c23fe831SViresh Kumar }
5664909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5674909f168SNicolai Stange 			"%llu\n");
5684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5694909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
570c23fe831SViresh Kumar 
571c23fe831SViresh Kumar /**
572c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
573c23fe831SViresh Kumar  * an unsigned long value.
574c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
575c23fe831SViresh Kumar  * @mode: the permission that the file should have
576c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
577c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
578c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
579c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
580c23fe831SViresh Kumar  *         from.
581c23fe831SViresh Kumar  *
582c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
583c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
584c23fe831SViresh Kumar  * set, it can be read from, and written to.
585c23fe831SViresh Kumar  *
586c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
587c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
588c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
589c23fe831SViresh Kumar  * you are responsible here.)  If an error occurs, %NULL will be returned.
590c23fe831SViresh Kumar  *
591c23fe831SViresh Kumar  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
592c23fe831SViresh Kumar  * returned.  It is not wise to check for this value, but rather, check for
593c23fe831SViresh Kumar  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
594c23fe831SViresh Kumar  * code.
595c23fe831SViresh Kumar  */
596c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
597c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
598c23fe831SViresh Kumar {
5994909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
6004909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
6014909f168SNicolai Stange 					&fops_ulong_wo);
602c23fe831SViresh Kumar }
603c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
604c23fe831SViresh Kumar 
6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
6074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
6082ebefc50SRobin Getz 
6094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
6104909f168SNicolai Stange 			"0x%04llx\n");
6114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
6124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
6132ebefc50SRobin Getz 
6144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
6154909f168SNicolai Stange 			"0x%08llx\n");
6164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
6174909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
6182ebefc50SRobin Getz 
6194909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
6204909f168SNicolai Stange 			"0x%016llx\n");
6214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
6224909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
62315b0beaaSHuang Ying 
624e6716b87SRandy Dunlap /*
62515b0beaaSHuang 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
626e6716b87SRandy Dunlap  *
627e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
628e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
629e6716b87SRandy Dunlap  * decimal functions.
630e6716b87SRandy Dunlap  */
631e6716b87SRandy Dunlap 
6322ebefc50SRobin Getz /**
6332ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
634e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
635e6716b87SRandy Dunlap  * @mode: the permission that the file should have
636e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
637e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
638e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
639e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
640e6716b87SRandy Dunlap  *         from.
6412ebefc50SRobin Getz  */
642f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
6432ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
6442ebefc50SRobin Getz {
6454909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
646b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6472ebefc50SRobin Getz }
6482ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6492ebefc50SRobin Getz 
650e6716b87SRandy Dunlap /**
651e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
652e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
653e6716b87SRandy Dunlap  * @mode: the permission that the file should have
654e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
655e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
656e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
657e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
658e6716b87SRandy Dunlap  *         from.
659e6716b87SRandy Dunlap  */
660f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode,
6612ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
6622ebefc50SRobin Getz {
6634909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
664b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6652ebefc50SRobin Getz }
6662ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6672ebefc50SRobin Getz 
668e6716b87SRandy Dunlap /**
669e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
670e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
671e6716b87SRandy Dunlap  * @mode: the permission that the file should have
672e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
673e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
674e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
675e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
676e6716b87SRandy Dunlap  *         from.
677e6716b87SRandy Dunlap  */
678f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode,
6792ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
6802ebefc50SRobin Getz {
6814909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
682b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6832ebefc50SRobin Getz }
6842ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6852ebefc50SRobin Getz 
68615b0beaaSHuang Ying /**
68715b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
68815b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
68915b0beaaSHuang Ying  * @mode: the permission that the file should have
69015b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
69115b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
69215b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
69315b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
69415b0beaaSHuang Ying  *         from.
69515b0beaaSHuang Ying  */
696f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
69715b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
69815b0beaaSHuang Ying {
6994909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
70082b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
70115b0beaaSHuang Ying }
70215b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
70315b0beaaSHuang Ying 
7045e078787SInaky Perez-Gonzalez 
7055e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
7065e078787SInaky Perez-Gonzalez {
7075e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
7085e078787SInaky Perez-Gonzalez 	return 0;
7095e078787SInaky Perez-Gonzalez }
7105e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
7115e078787SInaky Perez-Gonzalez {
7125e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
7135e078787SInaky Perez-Gonzalez 	return 0;
7145e078787SInaky Perez-Gonzalez }
7154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
7165e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
7174909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
7184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
7195e078787SInaky Perez-Gonzalez 
7205e078787SInaky Perez-Gonzalez /**
7215e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7225e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7235e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7245e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7255e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7265e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7275e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7285e078787SInaky Perez-Gonzalez  *         from.
7295e078787SInaky Perez-Gonzalez  */
730f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
7315e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
7325e078787SInaky Perez-Gonzalez {
7334909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7344909f168SNicolai Stange 					&fops_size_t, &fops_size_t_ro,
7354909f168SNicolai Stange 					&fops_size_t_wo);
7365e078787SInaky Perez-Gonzalez }
7375e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7385e078787SInaky Perez-Gonzalez 
7393a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7403a76e5e0SSeth Jennings {
7413a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7423a76e5e0SSeth Jennings 	return 0;
7433a76e5e0SSeth Jennings }
7443a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7453a76e5e0SSeth Jennings {
7463a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7473a76e5e0SSeth Jennings 	return 0;
7483a76e5e0SSeth Jennings }
7494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7503a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7514909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7524909f168SNicolai Stange 			"%lld\n");
7534909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7544909f168SNicolai Stange 			"%lld\n");
7553a76e5e0SSeth Jennings 
7563a76e5e0SSeth Jennings /**
7573a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7583a76e5e0SSeth Jennings  * write an atomic_t value
7593a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7603a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7613a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7623a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7633a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7643a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7653a76e5e0SSeth Jennings  *         from.
7663a76e5e0SSeth Jennings  */
7673a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
7683a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
7693a76e5e0SSeth Jennings {
7704909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7714909f168SNicolai Stange 					&fops_atomic_t, &fops_atomic_t_ro,
7724909f168SNicolai Stange 					&fops_atomic_t_wo);
7733a76e5e0SSeth Jennings }
7743a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7755e078787SInaky Perez-Gonzalez 
7760642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7771da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	char buf[3];
7804d45f797SNicolai Stange 	bool val;
78169d29f9eSNicolai Stange 	int r;
78269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7831da177e4SLinus Torvalds 
78469d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
78569d29f9eSNicolai Stange 	if (unlikely(r))
7864d45f797SNicolai Stange 		return r;
78769d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
78869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7894d45f797SNicolai Stange 
7904d45f797SNicolai Stange 	if (val)
7911da177e4SLinus Torvalds 		buf[0] = 'Y';
7921da177e4SLinus Torvalds 	else
7931da177e4SLinus Torvalds 		buf[0] = 'N';
7941da177e4SLinus Torvalds 	buf[1] = '\n';
7951da177e4SLinus Torvalds 	buf[2] = 0x00;
7961da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7971da177e4SLinus Torvalds }
7980642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7991da177e4SLinus Torvalds 
8000642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
8011da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	char buf[32];
804c42d2237SStephen Boyd 	size_t buf_size;
8058705b48eSJonathan Cameron 	bool bv;
80669d29f9eSNicolai Stange 	int r;
807621a5f7aSViresh Kumar 	bool *val = file->private_data;
80869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
8111da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
8121da177e4SLinus Torvalds 		return -EFAULT;
8131da177e4SLinus Torvalds 
814a3b2c8c7SMathias Krause 	buf[buf_size] = '\0';
8154d45f797SNicolai Stange 	if (strtobool(buf, &bv) == 0) {
81669d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
81769d29f9eSNicolai Stange 		if (unlikely(r))
8184d45f797SNicolai Stange 			return r;
81969d29f9eSNicolai Stange 		*val = bv;
82069d29f9eSNicolai Stange 		debugfs_file_put(dentry);
8214d45f797SNicolai Stange 	}
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	return count;
8241da177e4SLinus Torvalds }
8250642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
8261da177e4SLinus Torvalds 
8274b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
8280642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
8290642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
830234e3405SStephen Boyd 	.open =		simple_open,
8316038f373SArnd Bergmann 	.llseek =	default_llseek,
8321da177e4SLinus Torvalds };
8331da177e4SLinus Torvalds 
8346713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
8356713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8366713e8fbSStephen Boyd 	.open =		simple_open,
8376713e8fbSStephen Boyd 	.llseek =	default_llseek,
8386713e8fbSStephen Boyd };
8396713e8fbSStephen Boyd 
8406713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8416713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8426713e8fbSStephen Boyd 	.open =		simple_open,
8436713e8fbSStephen Boyd 	.llseek =	default_llseek,
8446713e8fbSStephen Boyd };
8456713e8fbSStephen Boyd 
8461da177e4SLinus Torvalds /**
8476468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8481da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8491da177e4SLinus Torvalds  * @mode: the permission that the file should have
8501da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8516468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8521da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8531da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8541da177e4SLinus Torvalds  *         from.
8551da177e4SLinus Torvalds  *
8561da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8571da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8581da177e4SLinus Torvalds  * set, it can be read from, and written to.
8591da177e4SLinus Torvalds  *
8601da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8611da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8621da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
8636468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
8641da177e4SLinus Torvalds  *
8656468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
8661da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
8676468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
8681da177e4SLinus Torvalds  * code.
8691da177e4SLinus Torvalds  */
870f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
871621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8721da177e4SLinus Torvalds {
8734d45f797SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8746713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8771da177e4SLinus Torvalds 
878dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
879dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
880dd308bc3SMichael Ellerman {
881dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
88269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
88383b711cbSNicolai Stange 	ssize_t r;
88483b711cbSNicolai Stange 
88569d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
88669d29f9eSNicolai Stange 	if (unlikely(r))
88769d29f9eSNicolai Stange 		return r;
88883b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
889dd308bc3SMichael Ellerman 				blob->size);
89069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
89183b711cbSNicolai Stange 	return r;
892dd308bc3SMichael Ellerman }
893dd308bc3SMichael Ellerman 
89400977a59SArjan van de Ven static const struct file_operations fops_blob = {
895dd308bc3SMichael Ellerman 	.read =		read_file_blob,
896234e3405SStephen Boyd 	.open =		simple_open,
8976038f373SArnd Bergmann 	.llseek =	default_llseek,
898dd308bc3SMichael Ellerman };
899dd308bc3SMichael Ellerman 
900dd308bc3SMichael Ellerman /**
901400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
902dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
903dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
904dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
9056468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
906dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
907dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
908dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
909dd308bc3SMichael Ellerman  *
910dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
911dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
912dd308bc3SMichael Ellerman  * read from. Writing is not supported.
913dd308bc3SMichael Ellerman  *
914dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
915dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
916dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
9176468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
918dd308bc3SMichael Ellerman  *
9196468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
920dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
9216468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
922dd308bc3SMichael Ellerman  * code.
923dd308bc3SMichael Ellerman  */
924f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
925dd308bc3SMichael Ellerman 				   struct dentry *parent,
926dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
927dd308bc3SMichael Ellerman {
92883b711cbSNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
929dd308bc3SMichael Ellerman }
930dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
9311a087c6aSAlessandro Rubini 
9329fe2a701SSrivatsa Vaddagiri struct array_data {
9339fe2a701SSrivatsa Vaddagiri 	void *array;
9349fe2a701SSrivatsa Vaddagiri 	u32 elements;
9359fe2a701SSrivatsa Vaddagiri };
9369fe2a701SSrivatsa Vaddagiri 
937e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
938e05e279eSLinus Torvalds 			       u32 *array, int array_size)
9399fe2a701SSrivatsa Vaddagiri {
9409fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
9419fe2a701SSrivatsa Vaddagiri 
942e05e279eSLinus Torvalds 	while (--array_size >= 0) {
9439fe2a701SSrivatsa Vaddagiri 		size_t len;
944e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
9459fe2a701SSrivatsa Vaddagiri 
946e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
9479fe2a701SSrivatsa Vaddagiri 		ret += len;
9489fe2a701SSrivatsa Vaddagiri 
9499fe2a701SSrivatsa Vaddagiri 		buf += len;
9509fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
9519fe2a701SSrivatsa Vaddagiri 	}
9529fe2a701SSrivatsa Vaddagiri 	return ret;
9539fe2a701SSrivatsa Vaddagiri }
9549fe2a701SSrivatsa Vaddagiri 
95536048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
9569fe2a701SSrivatsa Vaddagiri {
9579fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
958e05e279eSLinus Torvalds 	int size, elements = data->elements;
959e05e279eSLinus Torvalds 	char *buf;
9609fe2a701SSrivatsa Vaddagiri 
961e05e279eSLinus Torvalds 	/*
962e05e279eSLinus Torvalds 	 * Max size:
963e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
964e05e279eSLinus Torvalds 	 *  - terminating NUL character
965e05e279eSLinus Torvalds 	 */
966e05e279eSLinus Torvalds 	size = elements*11;
967e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
968e05e279eSLinus Torvalds 	if (!buf)
96936048853SDavid Rientjes 		return -ENOMEM;
970e05e279eSLinus Torvalds 	buf[size] = 0;
971e05e279eSLinus Torvalds 
972e05e279eSLinus Torvalds 	file->private_data = buf;
973e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
974e05e279eSLinus Torvalds 
97536048853SDavid Rientjes 	return nonseekable_open(inode, file);
9769fe2a701SSrivatsa Vaddagiri }
9779fe2a701SSrivatsa Vaddagiri 
97836048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
97936048853SDavid Rientjes 			      loff_t *ppos)
98036048853SDavid Rientjes {
98136048853SDavid Rientjes 	size_t size = strlen(file->private_data);
9829fe2a701SSrivatsa Vaddagiri 
9839fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
9849fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
9859fe2a701SSrivatsa Vaddagiri }
9869fe2a701SSrivatsa Vaddagiri 
9879fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
9889fe2a701SSrivatsa Vaddagiri {
9899fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
9909fe2a701SSrivatsa Vaddagiri 
9919fe2a701SSrivatsa Vaddagiri 	return 0;
9929fe2a701SSrivatsa Vaddagiri }
9939fe2a701SSrivatsa Vaddagiri 
9949fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
9959fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
9969fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
9979fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
9989fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
9999fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
10009fe2a701SSrivatsa Vaddagiri };
10019fe2a701SSrivatsa Vaddagiri 
10029fe2a701SSrivatsa Vaddagiri /**
10039fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
10049fe2a701SSrivatsa Vaddagiri  * array.
10059fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
10069fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
10079fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
10089fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
10099fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
10109fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
10119fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
10129fe2a701SSrivatsa Vaddagiri  *
10139fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
10149fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
10159fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
10169fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
10179fe2a701SSrivatsa Vaddagiri  *
10189fe2a701SSrivatsa Vaddagiri  * The function returns a pointer to dentry on success. If debugfs is not
10199fe2a701SSrivatsa Vaddagiri  * enabled in the kernel, the value -%ENODEV will be returned.
10209fe2a701SSrivatsa Vaddagiri  */
10219fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
10229fe2a701SSrivatsa Vaddagiri 					    struct dentry *parent,
10239fe2a701SSrivatsa Vaddagiri 					    u32 *array, u32 elements)
10249fe2a701SSrivatsa Vaddagiri {
10259fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
10269fe2a701SSrivatsa Vaddagiri 
10279fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
10289fe2a701SSrivatsa Vaddagiri 		return NULL;
10299fe2a701SSrivatsa Vaddagiri 
10309fe2a701SSrivatsa Vaddagiri 	data->array = array;
10319fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
10329fe2a701SSrivatsa Vaddagiri 
1033c4a74f63SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, data,
1034c4a74f63SNicolai Stange 					&u32_array_fops);
10359fe2a701SSrivatsa Vaddagiri }
10369fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10379fe2a701SSrivatsa Vaddagiri 
10383b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
10393b85e4abSHeiko Carstens 
10401a087c6aSAlessandro Rubini /*
10411a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
10421a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
10431a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
10441a087c6aSAlessandro Rubini  */
10451a087c6aSAlessandro Rubini 
10461a087c6aSAlessandro Rubini /**
10471a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10481a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10491a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1050b5763accSRandy Dunlap  * @nregs: the length of the above array
10511a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10521a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
10531a087c6aSAlessandro Rubini  *
10541a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
10551a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
10561a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
10571a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
10581a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
10591a087c6aSAlessandro Rubini  * for example configuration of dma channels
10601a087c6aSAlessandro Rubini  */
10619761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
10621a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
10631a087c6aSAlessandro Rubini {
10649761536eSJoe Perches 	int i;
10651a087c6aSAlessandro Rubini 
10661a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
10671a087c6aSAlessandro Rubini 		if (prefix)
10689761536eSJoe Perches 			seq_printf(s, "%s", prefix);
10699761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
10705858441cSDan Carpenter 			   readl(base + regs->offset));
10719761536eSJoe Perches 		if (seq_has_overflowed(s))
10729761536eSJoe Perches 			break;
10731a087c6aSAlessandro Rubini 	}
10741a087c6aSAlessandro Rubini }
10751a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
10761a087c6aSAlessandro Rubini 
10771a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
10781a087c6aSAlessandro Rubini {
10791a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
10801a087c6aSAlessandro Rubini 
10811a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
10821a087c6aSAlessandro Rubini 	return 0;
10831a087c6aSAlessandro Rubini }
10841a087c6aSAlessandro Rubini 
10851a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
10861a087c6aSAlessandro Rubini {
10871a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
10881a087c6aSAlessandro Rubini }
10891a087c6aSAlessandro Rubini 
10901a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
10911a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
10921a087c6aSAlessandro Rubini 	.read =		seq_read,
10931a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
10941a087c6aSAlessandro Rubini 	.release =	single_release,
10951a087c6aSAlessandro Rubini };
10961a087c6aSAlessandro Rubini 
10971a087c6aSAlessandro Rubini /**
10981a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
10991a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
11001a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
11011a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
11021a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
11031a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
11041a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
11051a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
11061a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
11071a087c6aSAlessandro Rubini  *
11081a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
11091a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
11101a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
11111a087c6aSAlessandro Rubini  *
11121a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
11131a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
11141a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
11151a087c6aSAlessandro Rubini  * you are responsible here.)  If an error occurs, %NULL will be returned.
11161a087c6aSAlessandro Rubini  *
11171a087c6aSAlessandro Rubini  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
11181a087c6aSAlessandro Rubini  * returned.  It is not wise to check for this value, but rather, check for
11191a087c6aSAlessandro Rubini  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
11201a087c6aSAlessandro Rubini  * code.
11211a087c6aSAlessandro Rubini  */
112288187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
11231a087c6aSAlessandro Rubini 				       struct dentry *parent,
11241a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
11251a087c6aSAlessandro Rubini {
11261a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
11271a087c6aSAlessandro Rubini }
11281a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
11293b85e4abSHeiko Carstens 
11303b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
113198210b7fSArend van Spriel 
113298210b7fSArend van Spriel struct debugfs_devm_entry {
113398210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
113498210b7fSArend van Spriel 	struct device *dev;
113598210b7fSArend van Spriel };
113698210b7fSArend van Spriel 
113798210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
113898210b7fSArend van Spriel {
113998210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
114098210b7fSArend van Spriel 
114198210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
114298210b7fSArend van Spriel }
114398210b7fSArend van Spriel 
114498210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
114598210b7fSArend van Spriel 	.owner = THIS_MODULE,
114698210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
114798210b7fSArend van Spriel 	.release = single_release,
114898210b7fSArend van Spriel 	.read = seq_read,
114998210b7fSArend van Spriel 	.llseek = seq_lseek
115098210b7fSArend van Spriel };
115198210b7fSArend van Spriel 
115298210b7fSArend van Spriel /**
115398210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
115498210b7fSArend van Spriel  *
115598210b7fSArend van Spriel  * @dev: device related to this debugfs file.
115698210b7fSArend van Spriel  * @name: name of the debugfs file.
115798210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
115898210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
115998210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
116098210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
116198210b7fSArend van Spriel  */
116298210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
116398210b7fSArend van Spriel 					   struct dentry *parent,
116498210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
116598210b7fSArend van Spriel 							  void *data))
116698210b7fSArend van Spriel {
116798210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
116898210b7fSArend van Spriel 
116998210b7fSArend van Spriel 	if (IS_ERR(parent))
117098210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
117198210b7fSArend van Spriel 
117298210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
117398210b7fSArend van Spriel 	if (!entry)
117498210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
117598210b7fSArend van Spriel 
117698210b7fSArend van Spriel 	entry->read = read_fn;
117798210b7fSArend van Spriel 	entry->dev = dev;
117898210b7fSArend van Spriel 
117998210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
118098210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
118198210b7fSArend van Spriel }
118298210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
118398210b7fSArend van Spriel 
1184