xref: /linux-6.15/fs/debugfs/file.c (revision 9abb2499)
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>
229fd4dcecSNicolai Stange 
239fd4dcecSNicolai Stange #include "internal.h"
241da177e4SLinus Torvalds 
2549d200deSNicolai Stange struct poll_table_struct;
2649d200deSNicolai Stange 
271da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
281da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	return 0;
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
341da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	return count;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
399fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
401da177e4SLinus Torvalds 	.read =		default_read_file,
411da177e4SLinus Torvalds 	.write =	default_write_file,
42234e3405SStephen Boyd 	.open =		simple_open,
436038f373SArnd Bergmann 	.llseek =	noop_llseek,
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
469fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
479fd4dcecSNicolai Stange 
487c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
497c8d4698SNicolai Stange {
507c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
51055ab8e3SNicolai Stange 
527d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
537d39bc50SNicolai Stange 		/*
547d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
557d39bc50SNicolai Stange 		 * debugfs_file_get().
567d39bc50SNicolai Stange 		 */
577d39bc50SNicolai Stange 		WARN_ON(1);
587d39bc50SNicolai Stange 		return NULL;
597d39bc50SNicolai Stange 	}
607d39bc50SNicolai Stange 
617c8d4698SNicolai Stange 	return fsd->real_fops;
627c8d4698SNicolai Stange }
637c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
647c8d4698SNicolai Stange 
65e9117a5aSNicolai Stange /**
66e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
67e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
68e9117a5aSNicolai Stange  *
69e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
70e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
71e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
72e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
73e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
74e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
75e9117a5aSNicolai Stange  *
76e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
77e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
78e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
79e9117a5aSNicolai Stange  */
80e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
81e9117a5aSNicolai Stange {
827d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
837d39bc50SNicolai Stange 	void *d_fsd;
84e9117a5aSNicolai Stange 
857d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
867d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
877d39bc50SNicolai Stange 		fsd = d_fsd;
887d39bc50SNicolai Stange 	} else {
897d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
907d39bc50SNicolai Stange 		if (!fsd)
917d39bc50SNicolai Stange 			return -ENOMEM;
927d39bc50SNicolai Stange 
937d39bc50SNicolai Stange 		fsd->real_fops = (void *)((unsigned long)d_fsd &
947d39bc50SNicolai Stange 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
957d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
967d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
977d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
987d39bc50SNicolai Stange 			kfree(fsd);
997d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
1007d39bc50SNicolai Stange 		}
1017d39bc50SNicolai Stange 	}
1027d39bc50SNicolai Stange 
1037d39bc50SNicolai Stange 	/*
1047d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1057d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1067d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1077d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1087d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1097d39bc50SNicolai Stange 	 * removers.
1107d39bc50SNicolai Stange 	 */
111e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
112e9117a5aSNicolai Stange 		return -EIO;
113e9117a5aSNicolai Stange 
114e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
115e9117a5aSNicolai Stange 		return -EIO;
116e9117a5aSNicolai Stange 
117e9117a5aSNicolai Stange 	return 0;
118e9117a5aSNicolai Stange }
119e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
120e9117a5aSNicolai Stange 
121e9117a5aSNicolai Stange /**
122e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
123e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
124e9117a5aSNicolai Stange  *          debugfs_file_get().
125e9117a5aSNicolai Stange  *
126e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
127e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
128e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
129e9117a5aSNicolai Stange  */
130e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
131e9117a5aSNicolai Stange {
1327d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
133e9117a5aSNicolai Stange 
134e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
135e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
136e9117a5aSNicolai Stange }
137e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
138e9117a5aSNicolai Stange 
1399fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1409fd4dcecSNicolai Stange {
14169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
1429fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
1437d39bc50SNicolai Stange 	int r;
1449fd4dcecSNicolai Stange 
1457d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
1467d39bc50SNicolai Stange 	if (r)
1477d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
1489fd4dcecSNicolai Stange 
14986f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
1509fd4dcecSNicolai Stange 	real_fops = fops_get(real_fops);
1519fd4dcecSNicolai Stange 	if (!real_fops) {
1529fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1539fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1549fd4dcecSNicolai Stange 			dentry);
1559fd4dcecSNicolai Stange 		r = -ENXIO;
1569fd4dcecSNicolai Stange 		goto out;
1579fd4dcecSNicolai Stange 	}
1589fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1599fd4dcecSNicolai Stange 
1609fd4dcecSNicolai Stange 	if (real_fops->open)
1619fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1629fd4dcecSNicolai Stange 
1639fd4dcecSNicolai Stange out:
16469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
1659fd4dcecSNicolai Stange 	return r;
1669fd4dcecSNicolai Stange }
1679fd4dcecSNicolai Stange 
1689fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
1699fd4dcecSNicolai Stange 	.open = open_proxy_open,
1709fd4dcecSNicolai Stange };
1719fd4dcecSNicolai Stange 
17249d200deSNicolai Stange #define PROTO(args...) args
17349d200deSNicolai Stange #define ARGS(args...) args
17449d200deSNicolai Stange 
17549d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
17649d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
17749d200deSNicolai Stange {									\
17869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
179154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
18049d200deSNicolai Stange 	ret_type r;							\
18149d200deSNicolai Stange 									\
18269d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
18369d29f9eSNicolai Stange 	if (unlikely(r))						\
18469d29f9eSNicolai Stange 		return r;						\
185154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
18649d200deSNicolai Stange 	r = real_fops->name(args);					\
18769d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
18849d200deSNicolai Stange 	return r;							\
18949d200deSNicolai Stange }
19049d200deSNicolai Stange 
19149d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
19249d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
19349d200deSNicolai Stange 		ARGS(filp, offset, whence));
19449d200deSNicolai Stange 
19549d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
19649d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
19749d200deSNicolai Stange 			loff_t *ppos),
19849d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
19949d200deSNicolai Stange 
20049d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
20149d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
20249d200deSNicolai Stange 			loff_t *ppos),
20349d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
20449d200deSNicolai Stange 
20549d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
20649d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
20749d200deSNicolai Stange 		ARGS(filp, cmd, arg));
20849d200deSNicolai Stange 
209076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
21049d200deSNicolai Stange 				struct poll_table_struct *wait)
21149d200deSNicolai Stange {
21269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
213e6c8adcaSAl Viro 	__poll_t r = 0;
214154b9d75SNicolai Stange 	const struct file_operations *real_fops;
21549d200deSNicolai Stange 
21669d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
217a9a08845SLinus Torvalds 		return EPOLLHUP;
21849d200deSNicolai Stange 
219154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
22049d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
22169d29f9eSNicolai Stange 	debugfs_file_put(dentry);
22249d200deSNicolai Stange 	return r;
22349d200deSNicolai Stange }
22449d200deSNicolai Stange 
22549d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
22649d200deSNicolai Stange {
22749d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
22886f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
22949d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
23049d200deSNicolai Stange 	int r = 0;
23149d200deSNicolai Stange 
23249d200deSNicolai Stange 	/*
23349d200deSNicolai Stange 	 * We must not protect this against removal races here: the
23449d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
23549d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
23649d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
23749d200deSNicolai Stange 	 */
23849d200deSNicolai Stange 	if (real_fops->release)
23949d200deSNicolai Stange 		r = real_fops->release(inode, filp);
24049d200deSNicolai Stange 
24149d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
24249d200deSNicolai Stange 	kfree((void *)proxy_fops);
24349d200deSNicolai Stange 	fops_put(real_fops);
244a1a9e5d2SEric Engestrom 	return r;
24549d200deSNicolai Stange }
24649d200deSNicolai Stange 
24749d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
24849d200deSNicolai Stange 				const struct file_operations *real_fops)
24949d200deSNicolai Stange {
25049d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
25149d200deSNicolai Stange 	if (real_fops->llseek)
25249d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
25349d200deSNicolai Stange 	if (real_fops->read)
25449d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
25549d200deSNicolai Stange 	if (real_fops->write)
25649d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
25749d200deSNicolai Stange 	if (real_fops->poll)
25849d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
25949d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
26049d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
26149d200deSNicolai Stange }
26249d200deSNicolai Stange 
26349d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
26449d200deSNicolai Stange {
26569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
26649d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
26749d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
2687d39bc50SNicolai Stange 	int r;
26949d200deSNicolai Stange 
2707d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
2717d39bc50SNicolai Stange 	if (r)
2727d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
27349d200deSNicolai Stange 
27486f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
27549d200deSNicolai Stange 	real_fops = fops_get(real_fops);
27649d200deSNicolai Stange 	if (!real_fops) {
27749d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
27849d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
27949d200deSNicolai Stange 			dentry);
28049d200deSNicolai Stange 		r = -ENXIO;
28149d200deSNicolai Stange 		goto out;
28249d200deSNicolai Stange 	}
28349d200deSNicolai Stange 
28449d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
28549d200deSNicolai Stange 	if (!proxy_fops) {
28649d200deSNicolai Stange 		r = -ENOMEM;
28749d200deSNicolai Stange 		goto free_proxy;
28849d200deSNicolai Stange 	}
28949d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
29049d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
29149d200deSNicolai Stange 
29249d200deSNicolai Stange 	if (real_fops->open) {
29349d200deSNicolai Stange 		r = real_fops->open(inode, filp);
294b10e3e90SNicolai Stange 		if (r) {
295b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
296b10e3e90SNicolai Stange 			goto free_proxy;
297b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
29849d200deSNicolai Stange 			/* No protection against file removal anymore. */
29949d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
30049d200deSNicolai Stange 				dentry);
30149d200deSNicolai Stange 			goto free_proxy;
30249d200deSNicolai Stange 		}
30349d200deSNicolai Stange 	}
30449d200deSNicolai Stange 
30549d200deSNicolai Stange 	goto out;
30649d200deSNicolai Stange free_proxy:
30749d200deSNicolai Stange 	kfree(proxy_fops);
30849d200deSNicolai Stange 	fops_put(real_fops);
30949d200deSNicolai Stange out:
31069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
31149d200deSNicolai Stange 	return r;
31249d200deSNicolai Stange }
31349d200deSNicolai Stange 
31449d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
31549d200deSNicolai Stange 	.open = full_proxy_open,
31649d200deSNicolai Stange };
31749d200deSNicolai Stange 
318c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
319c6468808SNicolai Stange 			size_t len, loff_t *ppos)
320c6468808SNicolai Stange {
32169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
322c6468808SNicolai Stange 	ssize_t ret;
323c6468808SNicolai Stange 
32469d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
32569d29f9eSNicolai Stange 	if (unlikely(ret))
32669d29f9eSNicolai Stange 		return ret;
327c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
32869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
329c6468808SNicolai Stange 	return ret;
330c6468808SNicolai Stange }
331c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
332c6468808SNicolai Stange 
333c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
334c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
335c6468808SNicolai Stange {
33669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
337c6468808SNicolai Stange 	ssize_t ret;
338c6468808SNicolai Stange 
33969d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
34069d29f9eSNicolai Stange 	if (unlikely(ret))
34169d29f9eSNicolai Stange 		return ret;
342c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
34369d29f9eSNicolai Stange 	debugfs_file_put(dentry);
344c6468808SNicolai Stange 	return ret;
345c6468808SNicolai Stange }
346c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
347c6468808SNicolai Stange 
3484909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3494909f168SNicolai Stange 					struct dentry *parent, void *value,
3504909f168SNicolai Stange 					const struct file_operations *fops,
3514909f168SNicolai Stange 					const struct file_operations *fops_ro,
3524909f168SNicolai Stange 					const struct file_operations *fops_wo)
3534909f168SNicolai Stange {
3544909f168SNicolai Stange 	/* if there are no write bits set, make read only */
3554909f168SNicolai Stange 	if (!(mode & S_IWUGO))
3564909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3574909f168SNicolai Stange 						fops_ro);
3584909f168SNicolai Stange 	/* if there are no read bits set, make write only */
3594909f168SNicolai Stange 	if (!(mode & S_IRUGO))
3604909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3614909f168SNicolai Stange 						fops_wo);
3624909f168SNicolai Stange 
3634909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
3644909f168SNicolai Stange }
3654909f168SNicolai Stange 
3668b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
367acaefc25SArnd Bergmann {
368acaefc25SArnd Bergmann 	*(u8 *)data = val;
3698b88b099SChristoph Hellwig 	return 0;
370acaefc25SArnd Bergmann }
3718b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
372acaefc25SArnd Bergmann {
3738b88b099SChristoph Hellwig 	*val = *(u8 *)data;
3748b88b099SChristoph Hellwig 	return 0;
375acaefc25SArnd Bergmann }
3764909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
3774909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
3784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /**
3816468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
3821da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3831da177e4SLinus Torvalds  * @mode: the permission that the file should have
3841da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3856468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3861da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3871da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3881da177e4SLinus Torvalds  *         from.
3891da177e4SLinus Torvalds  *
3901da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3911da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3921da177e4SLinus Torvalds  * set, it can be read from, and written to.
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3951da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3961da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
397*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
398*9abb2499SRonald Tschalär  * returned.
3991da177e4SLinus Torvalds  *
400*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
401*9abb2499SRonald Tschalär  * be returned.
4021da177e4SLinus Torvalds  */
403f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode,
4041da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
4051da177e4SLinus Torvalds {
4064909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
407b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
4101da177e4SLinus Torvalds 
4118b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
412acaefc25SArnd Bergmann {
413acaefc25SArnd Bergmann 	*(u16 *)data = val;
4148b88b099SChristoph Hellwig 	return 0;
415acaefc25SArnd Bergmann }
4168b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
417acaefc25SArnd Bergmann {
4188b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4198b88b099SChristoph Hellwig 	return 0;
420acaefc25SArnd Bergmann }
4214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
4224909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
4234909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
424acaefc25SArnd Bergmann 
4251da177e4SLinus Torvalds /**
4266468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4271da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4281da177e4SLinus Torvalds  * @mode: the permission that the file should have
4291da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4306468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4311da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4321da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4331da177e4SLinus Torvalds  *         from.
4341da177e4SLinus Torvalds  *
4351da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4361da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4371da177e4SLinus Torvalds  * set, it can be read from, and written to.
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4401da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4411da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
442*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
443*9abb2499SRonald Tschalär  * returned.
4441da177e4SLinus Torvalds  *
445*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
446*9abb2499SRonald Tschalär  * be returned.
4471da177e4SLinus Torvalds  */
448f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode,
4491da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
4501da177e4SLinus Torvalds {
4514909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
452b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4551da177e4SLinus Torvalds 
4568b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
457acaefc25SArnd Bergmann {
458acaefc25SArnd Bergmann 	*(u32 *)data = val;
4598b88b099SChristoph Hellwig 	return 0;
460acaefc25SArnd Bergmann }
4618b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
462acaefc25SArnd Bergmann {
4638b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4648b88b099SChristoph Hellwig 	return 0;
465acaefc25SArnd Bergmann }
4664909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4674909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
469acaefc25SArnd Bergmann 
4701da177e4SLinus Torvalds /**
4716468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4721da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4731da177e4SLinus Torvalds  * @mode: the permission that the file should have
4741da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4756468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4761da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4771da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4781da177e4SLinus Torvalds  *         from.
4791da177e4SLinus Torvalds  *
4801da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4811da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4821da177e4SLinus Torvalds  * set, it can be read from, and written to.
4831da177e4SLinus Torvalds  *
4841da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4851da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4861da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
487*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
488*9abb2499SRonald Tschalär  * returned.
4891da177e4SLinus Torvalds  *
490*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
491*9abb2499SRonald Tschalär  * be returned.
4921da177e4SLinus Torvalds  */
493f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
4941da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
4951da177e4SLinus Torvalds {
4964909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
497b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
5001da177e4SLinus Torvalds 
5018b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
5028447891fSMichael Ellerman {
5038447891fSMichael Ellerman 	*(u64 *)data = val;
5048b88b099SChristoph Hellwig 	return 0;
5058447891fSMichael Ellerman }
5068447891fSMichael Ellerman 
5078b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
5088447891fSMichael Ellerman {
5098b88b099SChristoph Hellwig 	*val = *(u64 *)data;
5108b88b099SChristoph Hellwig 	return 0;
5118447891fSMichael Ellerman }
5124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
5134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
5144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5158447891fSMichael Ellerman 
5168447891fSMichael Ellerman /**
5178447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5188447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5198447891fSMichael Ellerman  * @mode: the permission that the file should have
5208447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5218447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5228447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5238447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5248447891fSMichael Ellerman  *         from.
5258447891fSMichael Ellerman  *
5268447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5278447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5288447891fSMichael Ellerman  * set, it can be read from, and written to.
5298447891fSMichael Ellerman  *
5308447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
5318447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
5328447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
533*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
534*9abb2499SRonald Tschalär  * returned.
5358447891fSMichael Ellerman  *
536*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
537*9abb2499SRonald Tschalär  * be returned.
5388447891fSMichael Ellerman  */
539f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode,
5408447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
5418447891fSMichael Ellerman {
5424909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
543b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5448447891fSMichael Ellerman }
5458447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5468447891fSMichael Ellerman 
547c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
548c23fe831SViresh Kumar {
549c23fe831SViresh Kumar 	*(unsigned long *)data = val;
550c23fe831SViresh Kumar 	return 0;
551c23fe831SViresh Kumar }
552c23fe831SViresh Kumar 
553c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
554c23fe831SViresh Kumar {
555c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
556c23fe831SViresh Kumar 	return 0;
557c23fe831SViresh Kumar }
5584909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5594909f168SNicolai Stange 			"%llu\n");
5604909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5614909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
562c23fe831SViresh Kumar 
563c23fe831SViresh Kumar /**
564c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
565c23fe831SViresh Kumar  * an unsigned long value.
566c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
567c23fe831SViresh Kumar  * @mode: the permission that the file should have
568c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
569c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
570c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
571c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
572c23fe831SViresh Kumar  *         from.
573c23fe831SViresh Kumar  *
574c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
575c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
576c23fe831SViresh Kumar  * set, it can be read from, and written to.
577c23fe831SViresh Kumar  *
578c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
579c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
580c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
581*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
582*9abb2499SRonald Tschalär  * returned.
583c23fe831SViresh Kumar  *
584*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
585*9abb2499SRonald Tschalär  * be returned.
586c23fe831SViresh Kumar  */
587c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
588c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
589c23fe831SViresh Kumar {
5904909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
5914909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
5924909f168SNicolai Stange 					&fops_ulong_wo);
593c23fe831SViresh Kumar }
594c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
595c23fe831SViresh Kumar 
5964909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
5974909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
5984909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
5992ebefc50SRobin Getz 
6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
6014909f168SNicolai Stange 			"0x%04llx\n");
6024909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
6034909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
6042ebefc50SRobin Getz 
6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
6064909f168SNicolai Stange 			"0x%08llx\n");
6074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
6092ebefc50SRobin Getz 
6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
6114909f168SNicolai Stange 			"0x%016llx\n");
6124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
6134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
61415b0beaaSHuang Ying 
615e6716b87SRandy Dunlap /*
61615b0beaaSHuang 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
617e6716b87SRandy Dunlap  *
618e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
619e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
620e6716b87SRandy Dunlap  * decimal functions.
621e6716b87SRandy Dunlap  */
622e6716b87SRandy Dunlap 
6232ebefc50SRobin Getz /**
6242ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
625e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
626e6716b87SRandy Dunlap  * @mode: the permission that the file should have
627e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
628e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
629e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
630e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
631e6716b87SRandy Dunlap  *         from.
6322ebefc50SRobin Getz  */
633f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
6342ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
6352ebefc50SRobin Getz {
6364909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
637b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6382ebefc50SRobin Getz }
6392ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6402ebefc50SRobin Getz 
641e6716b87SRandy Dunlap /**
642e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
643e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
644e6716b87SRandy Dunlap  * @mode: the permission that the file should have
645e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
646e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
647e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
648e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
649e6716b87SRandy Dunlap  *         from.
650e6716b87SRandy Dunlap  */
651f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode,
6522ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
6532ebefc50SRobin Getz {
6544909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
655b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6562ebefc50SRobin Getz }
6572ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6582ebefc50SRobin Getz 
659e6716b87SRandy Dunlap /**
660e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
661e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
662e6716b87SRandy Dunlap  * @mode: the permission that the file should have
663e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
664e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
665e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
666e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
667e6716b87SRandy Dunlap  *         from.
668e6716b87SRandy Dunlap  */
669f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode,
6702ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
6712ebefc50SRobin Getz {
6724909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
673b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6742ebefc50SRobin Getz }
6752ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6762ebefc50SRobin Getz 
67715b0beaaSHuang Ying /**
67815b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
67915b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
68015b0beaaSHuang Ying  * @mode: the permission that the file should have
68115b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
68215b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
68315b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
68415b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
68515b0beaaSHuang Ying  *         from.
68615b0beaaSHuang Ying  */
687f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
68815b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
68915b0beaaSHuang Ying {
6904909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
69182b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
69215b0beaaSHuang Ying }
69315b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
69415b0beaaSHuang Ying 
6955e078787SInaky Perez-Gonzalez 
6965e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
6975e078787SInaky Perez-Gonzalez {
6985e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
6995e078787SInaky Perez-Gonzalez 	return 0;
7005e078787SInaky Perez-Gonzalez }
7015e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
7025e078787SInaky Perez-Gonzalez {
7035e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
7045e078787SInaky Perez-Gonzalez 	return 0;
7055e078787SInaky Perez-Gonzalez }
7064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
7075e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
7084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
7094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
7105e078787SInaky Perez-Gonzalez 
7115e078787SInaky Perez-Gonzalez /**
7125e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7135e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7145e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7155e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7165e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7175e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7185e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7195e078787SInaky Perez-Gonzalez  *         from.
7205e078787SInaky Perez-Gonzalez  */
721f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
7225e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
7235e078787SInaky Perez-Gonzalez {
7244909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7254909f168SNicolai Stange 					&fops_size_t, &fops_size_t_ro,
7264909f168SNicolai Stange 					&fops_size_t_wo);
7275e078787SInaky Perez-Gonzalez }
7285e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7295e078787SInaky Perez-Gonzalez 
7303a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7313a76e5e0SSeth Jennings {
7323a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7333a76e5e0SSeth Jennings 	return 0;
7343a76e5e0SSeth Jennings }
7353a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7363a76e5e0SSeth Jennings {
7373a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7383a76e5e0SSeth Jennings 	return 0;
7393a76e5e0SSeth Jennings }
7404909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7413a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7434909f168SNicolai Stange 			"%lld\n");
7444909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7454909f168SNicolai Stange 			"%lld\n");
7463a76e5e0SSeth Jennings 
7473a76e5e0SSeth Jennings /**
7483a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7493a76e5e0SSeth Jennings  * write an atomic_t value
7503a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7513a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7523a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7533a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7543a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7553a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7563a76e5e0SSeth Jennings  *         from.
7573a76e5e0SSeth Jennings  */
7583a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
7593a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
7603a76e5e0SSeth Jennings {
7614909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7624909f168SNicolai Stange 					&fops_atomic_t, &fops_atomic_t_ro,
7634909f168SNicolai Stange 					&fops_atomic_t_wo);
7643a76e5e0SSeth Jennings }
7653a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7665e078787SInaky Perez-Gonzalez 
7670642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7681da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	char buf[3];
7714d45f797SNicolai Stange 	bool val;
77269d29f9eSNicolai Stange 	int r;
77369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7741da177e4SLinus Torvalds 
77569d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
77669d29f9eSNicolai Stange 	if (unlikely(r))
7774d45f797SNicolai Stange 		return r;
77869d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
77969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7804d45f797SNicolai Stange 
7814d45f797SNicolai Stange 	if (val)
7821da177e4SLinus Torvalds 		buf[0] = 'Y';
7831da177e4SLinus Torvalds 	else
7841da177e4SLinus Torvalds 		buf[0] = 'N';
7851da177e4SLinus Torvalds 	buf[1] = '\n';
7861da177e4SLinus Torvalds 	buf[2] = 0x00;
7871da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7881da177e4SLinus Torvalds }
7890642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7901da177e4SLinus Torvalds 
7910642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7921da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7931da177e4SLinus Torvalds {
7948705b48eSJonathan Cameron 	bool bv;
79569d29f9eSNicolai Stange 	int r;
796621a5f7aSViresh Kumar 	bool *val = file->private_data;
79769d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7981da177e4SLinus Torvalds 
799964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
800964f8363SAndy Shevchenko 	if (!r) {
80169d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
80269d29f9eSNicolai Stange 		if (unlikely(r))
8034d45f797SNicolai Stange 			return r;
80469d29f9eSNicolai Stange 		*val = bv;
80569d29f9eSNicolai Stange 		debugfs_file_put(dentry);
8064d45f797SNicolai Stange 	}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	return count;
8091da177e4SLinus Torvalds }
8100642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
8111da177e4SLinus Torvalds 
8124b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
8130642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
8140642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
815234e3405SStephen Boyd 	.open =		simple_open,
8166038f373SArnd Bergmann 	.llseek =	default_llseek,
8171da177e4SLinus Torvalds };
8181da177e4SLinus Torvalds 
8196713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
8206713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8216713e8fbSStephen Boyd 	.open =		simple_open,
8226713e8fbSStephen Boyd 	.llseek =	default_llseek,
8236713e8fbSStephen Boyd };
8246713e8fbSStephen Boyd 
8256713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8266713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8276713e8fbSStephen Boyd 	.open =		simple_open,
8286713e8fbSStephen Boyd 	.llseek =	default_llseek,
8296713e8fbSStephen Boyd };
8306713e8fbSStephen Boyd 
8311da177e4SLinus Torvalds /**
8326468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8331da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8341da177e4SLinus Torvalds  * @mode: the permission that the file should have
8351da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8366468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8371da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8381da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8391da177e4SLinus Torvalds  *         from.
8401da177e4SLinus Torvalds  *
8411da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8421da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8431da177e4SLinus Torvalds  * set, it can be read from, and written to.
8441da177e4SLinus Torvalds  *
8451da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8461da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8471da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
848*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
849*9abb2499SRonald Tschalär  * returned.
8501da177e4SLinus Torvalds  *
851*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
852*9abb2499SRonald Tschalär  * be returned.
8531da177e4SLinus Torvalds  */
854f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
855621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8561da177e4SLinus Torvalds {
8574d45f797SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8586713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8611da177e4SLinus Torvalds 
862dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
863dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
864dd308bc3SMichael Ellerman {
865dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
86669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
86783b711cbSNicolai Stange 	ssize_t r;
86883b711cbSNicolai Stange 
86969d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
87069d29f9eSNicolai Stange 	if (unlikely(r))
87169d29f9eSNicolai Stange 		return r;
87283b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
873dd308bc3SMichael Ellerman 				blob->size);
87469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
87583b711cbSNicolai Stange 	return r;
876dd308bc3SMichael Ellerman }
877dd308bc3SMichael Ellerman 
87800977a59SArjan van de Ven static const struct file_operations fops_blob = {
879dd308bc3SMichael Ellerman 	.read =		read_file_blob,
880234e3405SStephen Boyd 	.open =		simple_open,
8816038f373SArnd Bergmann 	.llseek =	default_llseek,
882dd308bc3SMichael Ellerman };
883dd308bc3SMichael Ellerman 
884dd308bc3SMichael Ellerman /**
885400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
886dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
887dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
888dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
8896468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
890dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
891dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
892dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
893dd308bc3SMichael Ellerman  *
894dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
895dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
896dd308bc3SMichael Ellerman  * read from. Writing is not supported.
897dd308bc3SMichael Ellerman  *
898dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
899dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
900dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
901*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
902*9abb2499SRonald Tschalär  * returned.
903dd308bc3SMichael Ellerman  *
904*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
905*9abb2499SRonald Tschalär  * be returned.
906dd308bc3SMichael Ellerman  */
907f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
908dd308bc3SMichael Ellerman 				   struct dentry *parent,
909dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
910dd308bc3SMichael Ellerman {
91183b711cbSNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
912dd308bc3SMichael Ellerman }
913dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
9141a087c6aSAlessandro Rubini 
9159fe2a701SSrivatsa Vaddagiri struct array_data {
9169fe2a701SSrivatsa Vaddagiri 	void *array;
9179fe2a701SSrivatsa Vaddagiri 	u32 elements;
9189fe2a701SSrivatsa Vaddagiri };
9199fe2a701SSrivatsa Vaddagiri 
920e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
921e05e279eSLinus Torvalds 			       u32 *array, int array_size)
9229fe2a701SSrivatsa Vaddagiri {
9239fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
9249fe2a701SSrivatsa Vaddagiri 
925e05e279eSLinus Torvalds 	while (--array_size >= 0) {
9269fe2a701SSrivatsa Vaddagiri 		size_t len;
927e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
9289fe2a701SSrivatsa Vaddagiri 
929e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
9309fe2a701SSrivatsa Vaddagiri 		ret += len;
9319fe2a701SSrivatsa Vaddagiri 
9329fe2a701SSrivatsa Vaddagiri 		buf += len;
9339fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
9349fe2a701SSrivatsa Vaddagiri 	}
9359fe2a701SSrivatsa Vaddagiri 	return ret;
9369fe2a701SSrivatsa Vaddagiri }
9379fe2a701SSrivatsa Vaddagiri 
93836048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
9399fe2a701SSrivatsa Vaddagiri {
9409fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
941e05e279eSLinus Torvalds 	int size, elements = data->elements;
942e05e279eSLinus Torvalds 	char *buf;
9439fe2a701SSrivatsa Vaddagiri 
944e05e279eSLinus Torvalds 	/*
945e05e279eSLinus Torvalds 	 * Max size:
946e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
947e05e279eSLinus Torvalds 	 *  - terminating NUL character
948e05e279eSLinus Torvalds 	 */
949e05e279eSLinus Torvalds 	size = elements*11;
950e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
951e05e279eSLinus Torvalds 	if (!buf)
95236048853SDavid Rientjes 		return -ENOMEM;
953e05e279eSLinus Torvalds 	buf[size] = 0;
954e05e279eSLinus Torvalds 
955e05e279eSLinus Torvalds 	file->private_data = buf;
956e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
957e05e279eSLinus Torvalds 
95836048853SDavid Rientjes 	return nonseekable_open(inode, file);
9599fe2a701SSrivatsa Vaddagiri }
9609fe2a701SSrivatsa Vaddagiri 
96136048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
96236048853SDavid Rientjes 			      loff_t *ppos)
96336048853SDavid Rientjes {
96436048853SDavid Rientjes 	size_t size = strlen(file->private_data);
9659fe2a701SSrivatsa Vaddagiri 
9669fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
9679fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
9689fe2a701SSrivatsa Vaddagiri }
9699fe2a701SSrivatsa Vaddagiri 
9709fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
9719fe2a701SSrivatsa Vaddagiri {
9729fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
9739fe2a701SSrivatsa Vaddagiri 
9749fe2a701SSrivatsa Vaddagiri 	return 0;
9759fe2a701SSrivatsa Vaddagiri }
9769fe2a701SSrivatsa Vaddagiri 
9779fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
9789fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
9799fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
9809fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
9819fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
9829fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
9839fe2a701SSrivatsa Vaddagiri };
9849fe2a701SSrivatsa Vaddagiri 
9859fe2a701SSrivatsa Vaddagiri /**
9869fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
9879fe2a701SSrivatsa Vaddagiri  * array.
9889fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
9899fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
9909fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
9919fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
9929fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
9939fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
9949fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
9959fe2a701SSrivatsa Vaddagiri  *
9969fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
9979fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
9989fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
9999fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
10009fe2a701SSrivatsa Vaddagiri  *
1001*9abb2499SRonald Tschalär  * The function returns a pointer to dentry on success. If an error occurs,
1002*9abb2499SRonald Tschalär  * %ERR_PTR(-ERROR) or NULL will be returned. If debugfs is not enabled in
1003*9abb2499SRonald Tschalär  * the kernel, the value %ERR_PTR(-ENODEV) will be returned.
10049fe2a701SSrivatsa Vaddagiri  */
10059fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
10069fe2a701SSrivatsa Vaddagiri 					    struct dentry *parent,
10079fe2a701SSrivatsa Vaddagiri 					    u32 *array, u32 elements)
10089fe2a701SSrivatsa Vaddagiri {
10099fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
10109fe2a701SSrivatsa Vaddagiri 
10119fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
10129fe2a701SSrivatsa Vaddagiri 		return NULL;
10139fe2a701SSrivatsa Vaddagiri 
10149fe2a701SSrivatsa Vaddagiri 	data->array = array;
10159fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
10169fe2a701SSrivatsa Vaddagiri 
1017c4a74f63SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, data,
1018c4a74f63SNicolai Stange 					&u32_array_fops);
10199fe2a701SSrivatsa Vaddagiri }
10209fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10219fe2a701SSrivatsa Vaddagiri 
10223b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
10233b85e4abSHeiko Carstens 
10241a087c6aSAlessandro Rubini /*
10251a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
10261a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
10271a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
10281a087c6aSAlessandro Rubini  */
10291a087c6aSAlessandro Rubini 
10301a087c6aSAlessandro Rubini /**
10311a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10321a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10331a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1034b5763accSRandy Dunlap  * @nregs: the length of the above array
10351a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10361a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
10371a087c6aSAlessandro Rubini  *
10381a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
10391a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
10401a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
10411a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
10421a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
10431a087c6aSAlessandro Rubini  * for example configuration of dma channels
10441a087c6aSAlessandro Rubini  */
10459761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
10461a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
10471a087c6aSAlessandro Rubini {
10489761536eSJoe Perches 	int i;
10491a087c6aSAlessandro Rubini 
10501a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
10511a087c6aSAlessandro Rubini 		if (prefix)
10529761536eSJoe Perches 			seq_printf(s, "%s", prefix);
10539761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
10545858441cSDan Carpenter 			   readl(base + regs->offset));
10559761536eSJoe Perches 		if (seq_has_overflowed(s))
10569761536eSJoe Perches 			break;
10571a087c6aSAlessandro Rubini 	}
10581a087c6aSAlessandro Rubini }
10591a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
10601a087c6aSAlessandro Rubini 
10611a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
10621a087c6aSAlessandro Rubini {
10631a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
10641a087c6aSAlessandro Rubini 
10651a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
10661a087c6aSAlessandro Rubini 	return 0;
10671a087c6aSAlessandro Rubini }
10681a087c6aSAlessandro Rubini 
10691a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
10701a087c6aSAlessandro Rubini {
10711a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
10721a087c6aSAlessandro Rubini }
10731a087c6aSAlessandro Rubini 
10741a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
10751a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
10761a087c6aSAlessandro Rubini 	.read =		seq_read,
10771a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
10781a087c6aSAlessandro Rubini 	.release =	single_release,
10791a087c6aSAlessandro Rubini };
10801a087c6aSAlessandro Rubini 
10811a087c6aSAlessandro Rubini /**
10821a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
10831a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
10841a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
10851a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
10861a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
10871a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
10881a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
10891a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
10901a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
10911a087c6aSAlessandro Rubini  *
10921a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
10931a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
10941a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
10951a087c6aSAlessandro Rubini  *
10961a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
10971a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
10981a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
1099*9abb2499SRonald Tschalär  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
1100*9abb2499SRonald Tschalär  * returned.
11011a087c6aSAlessandro Rubini  *
1102*9abb2499SRonald Tschalär  * If debugfs is not enabled in the kernel, the value %ERR_PTR(-ENODEV) will
1103*9abb2499SRonald Tschalär  * be returned.
11041a087c6aSAlessandro Rubini  */
110588187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
11061a087c6aSAlessandro Rubini 				       struct dentry *parent,
11071a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
11081a087c6aSAlessandro Rubini {
11091a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
11101a087c6aSAlessandro Rubini }
11111a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
11123b85e4abSHeiko Carstens 
11133b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
111498210b7fSArend van Spriel 
111598210b7fSArend van Spriel struct debugfs_devm_entry {
111698210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
111798210b7fSArend van Spriel 	struct device *dev;
111898210b7fSArend van Spriel };
111998210b7fSArend van Spriel 
112098210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
112198210b7fSArend van Spriel {
112298210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
112398210b7fSArend van Spriel 
112498210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
112598210b7fSArend van Spriel }
112698210b7fSArend van Spriel 
112798210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
112898210b7fSArend van Spriel 	.owner = THIS_MODULE,
112998210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
113098210b7fSArend van Spriel 	.release = single_release,
113198210b7fSArend van Spriel 	.read = seq_read,
113298210b7fSArend van Spriel 	.llseek = seq_lseek
113398210b7fSArend van Spriel };
113498210b7fSArend van Spriel 
113598210b7fSArend van Spriel /**
113698210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
113798210b7fSArend van Spriel  *
113898210b7fSArend van Spriel  * @dev: device related to this debugfs file.
113998210b7fSArend van Spriel  * @name: name of the debugfs file.
114098210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
114198210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
114298210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
114398210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
114498210b7fSArend van Spriel  */
114598210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
114698210b7fSArend van Spriel 					   struct dentry *parent,
114798210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
114898210b7fSArend van Spriel 							  void *data))
114998210b7fSArend van Spriel {
115098210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
115198210b7fSArend van Spriel 
115298210b7fSArend van Spriel 	if (IS_ERR(parent))
115398210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
115498210b7fSArend van Spriel 
115598210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
115698210b7fSArend van Spriel 	if (!entry)
115798210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
115898210b7fSArend van Spriel 
115998210b7fSArend van Spriel 	entry->read = read_fn;
116098210b7fSArend van Spriel 	entry->dev = dev;
116198210b7fSArend van Spriel 
116298210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
116398210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
116498210b7fSArend van Spriel }
116598210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
116698210b7fSArend van Spriel 
1167