xref: /linux-6.15/fs/debugfs/file.c (revision 154b9d75)
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 
567c8d4698SNicolai Stange 	return fsd->real_fops;
577c8d4698SNicolai Stange }
587c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
597c8d4698SNicolai Stange 
60e9117a5aSNicolai Stange /**
61e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
62e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
63e9117a5aSNicolai Stange  *
64e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
65e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
66e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
67e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
68e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
69e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
70e9117a5aSNicolai Stange  *
71e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
72e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
73e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
74e9117a5aSNicolai Stange  */
75e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
76e9117a5aSNicolai Stange {
77e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
78e9117a5aSNicolai Stange 
79e9117a5aSNicolai Stange 	/* Avoid starvation of removers. */
80e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
81e9117a5aSNicolai Stange 		return -EIO;
82e9117a5aSNicolai Stange 
83e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
84e9117a5aSNicolai Stange 		return -EIO;
85e9117a5aSNicolai Stange 
86e9117a5aSNicolai Stange 	return 0;
87e9117a5aSNicolai Stange }
88e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
89e9117a5aSNicolai Stange 
90e9117a5aSNicolai Stange /**
91e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
92e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
93e9117a5aSNicolai Stange  *          debugfs_file_get().
94e9117a5aSNicolai Stange  *
95e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
96e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
97e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
98e9117a5aSNicolai Stange  */
99e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
100e9117a5aSNicolai Stange {
101e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
102e9117a5aSNicolai Stange 
103e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
104e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
105e9117a5aSNicolai Stange }
106e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
107e9117a5aSNicolai Stange 
1089fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1099fd4dcecSNicolai Stange {
11069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
1119fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
11269d29f9eSNicolai Stange 	int r = 0;
1139fd4dcecSNicolai Stange 
11469d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
11569d29f9eSNicolai Stange 		return -ENOENT;
1169fd4dcecSNicolai Stange 
11786f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
1189fd4dcecSNicolai Stange 	real_fops = fops_get(real_fops);
1199fd4dcecSNicolai Stange 	if (!real_fops) {
1209fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1219fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1229fd4dcecSNicolai Stange 			dentry);
1239fd4dcecSNicolai Stange 		r = -ENXIO;
1249fd4dcecSNicolai Stange 		goto out;
1259fd4dcecSNicolai Stange 	}
1269fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1279fd4dcecSNicolai Stange 
1289fd4dcecSNicolai Stange 	if (real_fops->open)
1299fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1309fd4dcecSNicolai Stange 
1319fd4dcecSNicolai Stange out:
13269d29f9eSNicolai Stange 	debugfs_file_put(dentry);
1339fd4dcecSNicolai Stange 	return r;
1349fd4dcecSNicolai Stange }
1359fd4dcecSNicolai Stange 
1369fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
1379fd4dcecSNicolai Stange 	.open = open_proxy_open,
1389fd4dcecSNicolai Stange };
1399fd4dcecSNicolai Stange 
14049d200deSNicolai Stange #define PROTO(args...) args
14149d200deSNicolai Stange #define ARGS(args...) args
14249d200deSNicolai Stange 
14349d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
14449d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
14549d200deSNicolai Stange {									\
14669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
147*154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
14849d200deSNicolai Stange 	ret_type r;							\
14949d200deSNicolai Stange 									\
15069d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
15169d29f9eSNicolai Stange 	if (unlikely(r))						\
15269d29f9eSNicolai Stange 		return r;						\
153*154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
15449d200deSNicolai Stange 	r = real_fops->name(args);					\
15569d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
15649d200deSNicolai Stange 	return r;							\
15749d200deSNicolai Stange }
15849d200deSNicolai Stange 
15949d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
16049d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
16149d200deSNicolai Stange 		ARGS(filp, offset, whence));
16249d200deSNicolai Stange 
16349d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
16449d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
16549d200deSNicolai Stange 			loff_t *ppos),
16649d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
16749d200deSNicolai Stange 
16849d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
16949d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
17049d200deSNicolai Stange 			loff_t *ppos),
17149d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
17249d200deSNicolai Stange 
17349d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
17449d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
17549d200deSNicolai Stange 		ARGS(filp, cmd, arg));
17649d200deSNicolai Stange 
17749d200deSNicolai Stange static unsigned int full_proxy_poll(struct file *filp,
17849d200deSNicolai Stange 				struct poll_table_struct *wait)
17949d200deSNicolai Stange {
18069d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
18149d200deSNicolai Stange 	unsigned int r = 0;
182*154b9d75SNicolai Stange 	const struct file_operations *real_fops;
18349d200deSNicolai Stange 
18469d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
18549d200deSNicolai Stange 		return POLLHUP;
18649d200deSNicolai Stange 
187*154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
18849d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
18969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
19049d200deSNicolai Stange 	return r;
19149d200deSNicolai Stange }
19249d200deSNicolai Stange 
19349d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
19449d200deSNicolai Stange {
19549d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
19686f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
19749d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
19849d200deSNicolai Stange 	int r = 0;
19949d200deSNicolai Stange 
20049d200deSNicolai Stange 	/*
20149d200deSNicolai Stange 	 * We must not protect this against removal races here: the
20249d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
20349d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
20449d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
20549d200deSNicolai Stange 	 */
20649d200deSNicolai Stange 	if (real_fops->release)
20749d200deSNicolai Stange 		r = real_fops->release(inode, filp);
20849d200deSNicolai Stange 
20949d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
21049d200deSNicolai Stange 	kfree((void *)proxy_fops);
21149d200deSNicolai Stange 	fops_put(real_fops);
212a1a9e5d2SEric Engestrom 	return r;
21349d200deSNicolai Stange }
21449d200deSNicolai Stange 
21549d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
21649d200deSNicolai Stange 				const struct file_operations *real_fops)
21749d200deSNicolai Stange {
21849d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
21949d200deSNicolai Stange 	if (real_fops->llseek)
22049d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
22149d200deSNicolai Stange 	if (real_fops->read)
22249d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
22349d200deSNicolai Stange 	if (real_fops->write)
22449d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
22549d200deSNicolai Stange 	if (real_fops->poll)
22649d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
22749d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
22849d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
22949d200deSNicolai Stange }
23049d200deSNicolai Stange 
23149d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
23249d200deSNicolai Stange {
23369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
23449d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
23549d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
23669d29f9eSNicolai Stange 	int r = 0;
23749d200deSNicolai Stange 
23869d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
23969d29f9eSNicolai Stange 		return -ENOENT;
24049d200deSNicolai Stange 
24186f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
24249d200deSNicolai Stange 	real_fops = fops_get(real_fops);
24349d200deSNicolai Stange 	if (!real_fops) {
24449d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
24549d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
24649d200deSNicolai Stange 			dentry);
24749d200deSNicolai Stange 		r = -ENXIO;
24849d200deSNicolai Stange 		goto out;
24949d200deSNicolai Stange 	}
25049d200deSNicolai Stange 
25149d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
25249d200deSNicolai Stange 	if (!proxy_fops) {
25349d200deSNicolai Stange 		r = -ENOMEM;
25449d200deSNicolai Stange 		goto free_proxy;
25549d200deSNicolai Stange 	}
25649d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
25749d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
25849d200deSNicolai Stange 
25949d200deSNicolai Stange 	if (real_fops->open) {
26049d200deSNicolai Stange 		r = real_fops->open(inode, filp);
261b10e3e90SNicolai Stange 		if (r) {
262b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
263b10e3e90SNicolai Stange 			goto free_proxy;
264b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
26549d200deSNicolai Stange 			/* No protection against file removal anymore. */
26649d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
26749d200deSNicolai Stange 				dentry);
26849d200deSNicolai Stange 			goto free_proxy;
26949d200deSNicolai Stange 		}
27049d200deSNicolai Stange 	}
27149d200deSNicolai Stange 
27249d200deSNicolai Stange 	goto out;
27349d200deSNicolai Stange free_proxy:
27449d200deSNicolai Stange 	kfree(proxy_fops);
27549d200deSNicolai Stange 	fops_put(real_fops);
27649d200deSNicolai Stange out:
27769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
27849d200deSNicolai Stange 	return r;
27949d200deSNicolai Stange }
28049d200deSNicolai Stange 
28149d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
28249d200deSNicolai Stange 	.open = full_proxy_open,
28349d200deSNicolai Stange };
28449d200deSNicolai Stange 
285c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
286c6468808SNicolai Stange 			size_t len, loff_t *ppos)
287c6468808SNicolai Stange {
28869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
289c6468808SNicolai Stange 	ssize_t ret;
290c6468808SNicolai Stange 
29169d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
29269d29f9eSNicolai Stange 	if (unlikely(ret))
29369d29f9eSNicolai Stange 		return ret;
294c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
29569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
296c6468808SNicolai Stange 	return ret;
297c6468808SNicolai Stange }
298c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
299c6468808SNicolai Stange 
300c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
301c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
302c6468808SNicolai Stange {
30369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
304c6468808SNicolai Stange 	ssize_t ret;
305c6468808SNicolai Stange 
30669d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
30769d29f9eSNicolai Stange 	if (unlikely(ret))
30869d29f9eSNicolai Stange 		return ret;
309c6468808SNicolai Stange 	ret = simple_attr_write(file, buf, len, ppos);
31069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
311c6468808SNicolai Stange 	return ret;
312c6468808SNicolai Stange }
313c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
314c6468808SNicolai Stange 
3154909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
3164909f168SNicolai Stange 					struct dentry *parent, void *value,
3174909f168SNicolai Stange 					const struct file_operations *fops,
3184909f168SNicolai Stange 					const struct file_operations *fops_ro,
3194909f168SNicolai Stange 					const struct file_operations *fops_wo)
3204909f168SNicolai Stange {
3214909f168SNicolai Stange 	/* if there are no write bits set, make read only */
3224909f168SNicolai Stange 	if (!(mode & S_IWUGO))
3234909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3244909f168SNicolai Stange 						fops_ro);
3254909f168SNicolai Stange 	/* if there are no read bits set, make write only */
3264909f168SNicolai Stange 	if (!(mode & S_IRUGO))
3274909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
3284909f168SNicolai Stange 						fops_wo);
3294909f168SNicolai Stange 
3304909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
3314909f168SNicolai Stange }
3324909f168SNicolai Stange 
3338b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
334acaefc25SArnd Bergmann {
335acaefc25SArnd Bergmann 	*(u8 *)data = val;
3368b88b099SChristoph Hellwig 	return 0;
337acaefc25SArnd Bergmann }
3388b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
339acaefc25SArnd Bergmann {
3408b88b099SChristoph Hellwig 	*val = *(u8 *)data;
3418b88b099SChristoph Hellwig 	return 0;
342acaefc25SArnd Bergmann }
3434909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
3444909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
3454909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /**
3486468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
3491da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3501da177e4SLinus Torvalds  * @mode: the permission that the file should have
3511da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3526468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3531da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3541da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3551da177e4SLinus Torvalds  *         from.
3561da177e4SLinus Torvalds  *
3571da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3581da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3591da177e4SLinus Torvalds  * set, it can be read from, and written to.
3601da177e4SLinus Torvalds  *
3611da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3621da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3631da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3646468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3651da177e4SLinus Torvalds  *
3666468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
3671da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
3686468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
3691da177e4SLinus Torvalds  * code.
3701da177e4SLinus Torvalds  */
371f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode,
3721da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
3731da177e4SLinus Torvalds {
3744909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
375b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
3781da177e4SLinus Torvalds 
3798b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
380acaefc25SArnd Bergmann {
381acaefc25SArnd Bergmann 	*(u16 *)data = val;
3828b88b099SChristoph Hellwig 	return 0;
383acaefc25SArnd Bergmann }
3848b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
385acaefc25SArnd Bergmann {
3868b88b099SChristoph Hellwig 	*val = *(u16 *)data;
3878b88b099SChristoph Hellwig 	return 0;
388acaefc25SArnd Bergmann }
3894909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
3904909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
3914909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
392acaefc25SArnd Bergmann 
3931da177e4SLinus Torvalds /**
3946468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
3951da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3961da177e4SLinus Torvalds  * @mode: the permission that the file should have
3971da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3986468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3991da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4001da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4011da177e4SLinus Torvalds  *         from.
4021da177e4SLinus Torvalds  *
4031da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4041da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4051da177e4SLinus Torvalds  * set, it can be read from, and written to.
4061da177e4SLinus Torvalds  *
4071da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4081da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4091da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4106468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4111da177e4SLinus Torvalds  *
4126468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4131da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4146468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4151da177e4SLinus Torvalds  * code.
4161da177e4SLinus Torvalds  */
417f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode,
4181da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
4191da177e4SLinus Torvalds {
4204909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
421b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4241da177e4SLinus Torvalds 
4258b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
426acaefc25SArnd Bergmann {
427acaefc25SArnd Bergmann 	*(u32 *)data = val;
4288b88b099SChristoph Hellwig 	return 0;
429acaefc25SArnd Bergmann }
4308b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
431acaefc25SArnd Bergmann {
4328b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4338b88b099SChristoph Hellwig 	return 0;
434acaefc25SArnd Bergmann }
4354909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
4364909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
4374909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
438acaefc25SArnd Bergmann 
4391da177e4SLinus Torvalds /**
4406468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4411da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4421da177e4SLinus Torvalds  * @mode: the permission that the file should have
4431da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4446468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4451da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4461da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4471da177e4SLinus Torvalds  *         from.
4481da177e4SLinus Torvalds  *
4491da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4501da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4511da177e4SLinus Torvalds  * set, it can be read from, and written to.
4521da177e4SLinus Torvalds  *
4531da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4541da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4551da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4566468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4571da177e4SLinus Torvalds  *
4586468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4591da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4606468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4611da177e4SLinus Torvalds  * code.
4621da177e4SLinus Torvalds  */
463f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
4641da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
4651da177e4SLinus Torvalds {
4664909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
467b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
4701da177e4SLinus Torvalds 
4718b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
4728447891fSMichael Ellerman {
4738447891fSMichael Ellerman 	*(u64 *)data = val;
4748b88b099SChristoph Hellwig 	return 0;
4758447891fSMichael Ellerman }
4768447891fSMichael Ellerman 
4778b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
4788447891fSMichael Ellerman {
4798b88b099SChristoph Hellwig 	*val = *(u64 *)data;
4808b88b099SChristoph Hellwig 	return 0;
4818447891fSMichael Ellerman }
4824909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
4834909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
4844909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
4858447891fSMichael Ellerman 
4868447891fSMichael Ellerman /**
4878447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
4888447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
4898447891fSMichael Ellerman  * @mode: the permission that the file should have
4908447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
4918447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
4928447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
4938447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
4948447891fSMichael Ellerman  *         from.
4958447891fSMichael Ellerman  *
4968447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
4978447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
4988447891fSMichael Ellerman  * set, it can be read from, and written to.
4998447891fSMichael Ellerman  *
5008447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
5018447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
5028447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
5038447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
5048447891fSMichael Ellerman  *
5058447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
5068447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
5078447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
5088447891fSMichael Ellerman  * code.
5098447891fSMichael Ellerman  */
510f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode,
5118447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
5128447891fSMichael Ellerman {
5134909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
514b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5158447891fSMichael Ellerman }
5168447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5178447891fSMichael Ellerman 
518c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
519c23fe831SViresh Kumar {
520c23fe831SViresh Kumar 	*(unsigned long *)data = val;
521c23fe831SViresh Kumar 	return 0;
522c23fe831SViresh Kumar }
523c23fe831SViresh Kumar 
524c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
525c23fe831SViresh Kumar {
526c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
527c23fe831SViresh Kumar 	return 0;
528c23fe831SViresh Kumar }
5294909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
5304909f168SNicolai Stange 			"%llu\n");
5314909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
5324909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
533c23fe831SViresh Kumar 
534c23fe831SViresh Kumar /**
535c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
536c23fe831SViresh Kumar  * an unsigned long value.
537c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
538c23fe831SViresh Kumar  * @mode: the permission that the file should have
539c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
540c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
541c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
542c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
543c23fe831SViresh Kumar  *         from.
544c23fe831SViresh Kumar  *
545c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
546c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
547c23fe831SViresh Kumar  * set, it can be read from, and written to.
548c23fe831SViresh Kumar  *
549c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
550c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
551c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
552c23fe831SViresh Kumar  * you are responsible here.)  If an error occurs, %NULL will be returned.
553c23fe831SViresh Kumar  *
554c23fe831SViresh Kumar  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
555c23fe831SViresh Kumar  * returned.  It is not wise to check for this value, but rather, check for
556c23fe831SViresh Kumar  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
557c23fe831SViresh Kumar  * code.
558c23fe831SViresh Kumar  */
559c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
560c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
561c23fe831SViresh Kumar {
5624909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
5634909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
5644909f168SNicolai Stange 					&fops_ulong_wo);
565c23fe831SViresh Kumar }
566c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
567c23fe831SViresh Kumar 
5684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
5694909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
5704909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
5712ebefc50SRobin Getz 
5724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
5734909f168SNicolai Stange 			"0x%04llx\n");
5744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
5754909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
5762ebefc50SRobin Getz 
5774909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
5784909f168SNicolai Stange 			"0x%08llx\n");
5794909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
5804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
5812ebefc50SRobin Getz 
5824909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
5834909f168SNicolai Stange 			"0x%016llx\n");
5844909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
5854909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
58615b0beaaSHuang Ying 
587e6716b87SRandy Dunlap /*
58815b0beaaSHuang 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
589e6716b87SRandy Dunlap  *
590e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
591e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
592e6716b87SRandy Dunlap  * decimal functions.
593e6716b87SRandy Dunlap  */
594e6716b87SRandy Dunlap 
5952ebefc50SRobin Getz /**
5962ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
597e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
598e6716b87SRandy Dunlap  * @mode: the permission that the file should have
599e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
600e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
601e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
602e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
603e6716b87SRandy Dunlap  *         from.
6042ebefc50SRobin Getz  */
605f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
6062ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
6072ebefc50SRobin Getz {
6084909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
609b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6102ebefc50SRobin Getz }
6112ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6122ebefc50SRobin Getz 
613e6716b87SRandy Dunlap /**
614e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
615e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
616e6716b87SRandy Dunlap  * @mode: the permission that the file should have
617e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
618e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
619e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
620e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
621e6716b87SRandy Dunlap  *         from.
622e6716b87SRandy Dunlap  */
623f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode,
6242ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
6252ebefc50SRobin Getz {
6264909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
627b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6282ebefc50SRobin Getz }
6292ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6302ebefc50SRobin Getz 
631e6716b87SRandy Dunlap /**
632e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
633e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
634e6716b87SRandy Dunlap  * @mode: the permission that the file should have
635e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
636e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
637e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
638e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
639e6716b87SRandy Dunlap  *         from.
640e6716b87SRandy Dunlap  */
641f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode,
6422ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
6432ebefc50SRobin Getz {
6444909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
645b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6462ebefc50SRobin Getz }
6472ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6482ebefc50SRobin Getz 
64915b0beaaSHuang Ying /**
65015b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
65115b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
65215b0beaaSHuang Ying  * @mode: the permission that the file should have
65315b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
65415b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
65515b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
65615b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
65715b0beaaSHuang Ying  *         from.
65815b0beaaSHuang Ying  */
659f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
66015b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
66115b0beaaSHuang Ying {
6624909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
66382b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
66415b0beaaSHuang Ying }
66515b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
66615b0beaaSHuang Ying 
6675e078787SInaky Perez-Gonzalez 
6685e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
6695e078787SInaky Perez-Gonzalez {
6705e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
6715e078787SInaky Perez-Gonzalez 	return 0;
6725e078787SInaky Perez-Gonzalez }
6735e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
6745e078787SInaky Perez-Gonzalez {
6755e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
6765e078787SInaky Perez-Gonzalez 	return 0;
6775e078787SInaky Perez-Gonzalez }
6784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
6795e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
6804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
6814909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
6825e078787SInaky Perez-Gonzalez 
6835e078787SInaky Perez-Gonzalez /**
6845e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
6855e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
6865e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
6875e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
6885e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
6895e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
6905e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
6915e078787SInaky Perez-Gonzalez  *         from.
6925e078787SInaky Perez-Gonzalez  */
693f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
6945e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
6955e078787SInaky Perez-Gonzalez {
6964909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
6974909f168SNicolai Stange 					&fops_size_t, &fops_size_t_ro,
6984909f168SNicolai Stange 					&fops_size_t_wo);
6995e078787SInaky Perez-Gonzalez }
7005e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7015e078787SInaky Perez-Gonzalez 
7023a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7033a76e5e0SSeth Jennings {
7043a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7053a76e5e0SSeth Jennings 	return 0;
7063a76e5e0SSeth Jennings }
7073a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7083a76e5e0SSeth Jennings {
7093a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7103a76e5e0SSeth Jennings 	return 0;
7113a76e5e0SSeth Jennings }
7124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7133a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
7144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
7154909f168SNicolai Stange 			"%lld\n");
7164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
7174909f168SNicolai Stange 			"%lld\n");
7183a76e5e0SSeth Jennings 
7193a76e5e0SSeth Jennings /**
7203a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7213a76e5e0SSeth Jennings  * write an atomic_t value
7223a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7233a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7243a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7253a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7263a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7273a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7283a76e5e0SSeth Jennings  *         from.
7293a76e5e0SSeth Jennings  */
7303a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
7313a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
7323a76e5e0SSeth Jennings {
7334909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
7344909f168SNicolai Stange 					&fops_atomic_t, &fops_atomic_t_ro,
7354909f168SNicolai Stange 					&fops_atomic_t_wo);
7363a76e5e0SSeth Jennings }
7373a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7385e078787SInaky Perez-Gonzalez 
7390642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7401da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	char buf[3];
7434d45f797SNicolai Stange 	bool val;
74469d29f9eSNicolai Stange 	int r;
74569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7461da177e4SLinus Torvalds 
74769d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
74869d29f9eSNicolai Stange 	if (unlikely(r))
7494d45f797SNicolai Stange 		return r;
75069d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
75169d29f9eSNicolai Stange 	debugfs_file_put(dentry);
7524d45f797SNicolai Stange 
7534d45f797SNicolai Stange 	if (val)
7541da177e4SLinus Torvalds 		buf[0] = 'Y';
7551da177e4SLinus Torvalds 	else
7561da177e4SLinus Torvalds 		buf[0] = 'N';
7571da177e4SLinus Torvalds 	buf[1] = '\n';
7581da177e4SLinus Torvalds 	buf[2] = 0x00;
7591da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7601da177e4SLinus Torvalds }
7610642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7621da177e4SLinus Torvalds 
7630642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7641da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	char buf[32];
767c42d2237SStephen Boyd 	size_t buf_size;
7688705b48eSJonathan Cameron 	bool bv;
76969d29f9eSNicolai Stange 	int r;
770621a5f7aSViresh Kumar 	bool *val = file->private_data;
77169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
7741da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
7751da177e4SLinus Torvalds 		return -EFAULT;
7761da177e4SLinus Torvalds 
777a3b2c8c7SMathias Krause 	buf[buf_size] = '\0';
7784d45f797SNicolai Stange 	if (strtobool(buf, &bv) == 0) {
77969d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
78069d29f9eSNicolai Stange 		if (unlikely(r))
7814d45f797SNicolai Stange 			return r;
78269d29f9eSNicolai Stange 		*val = bv;
78369d29f9eSNicolai Stange 		debugfs_file_put(dentry);
7844d45f797SNicolai Stange 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	return count;
7871da177e4SLinus Torvalds }
7880642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
7891da177e4SLinus Torvalds 
7904b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
7910642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
7920642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
793234e3405SStephen Boyd 	.open =		simple_open,
7946038f373SArnd Bergmann 	.llseek =	default_llseek,
7951da177e4SLinus Torvalds };
7961da177e4SLinus Torvalds 
7976713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
7986713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
7996713e8fbSStephen Boyd 	.open =		simple_open,
8006713e8fbSStephen Boyd 	.llseek =	default_llseek,
8016713e8fbSStephen Boyd };
8026713e8fbSStephen Boyd 
8036713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8046713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8056713e8fbSStephen Boyd 	.open =		simple_open,
8066713e8fbSStephen Boyd 	.llseek =	default_llseek,
8076713e8fbSStephen Boyd };
8086713e8fbSStephen Boyd 
8091da177e4SLinus Torvalds /**
8106468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8111da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8121da177e4SLinus Torvalds  * @mode: the permission that the file should have
8131da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8146468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8151da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8161da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8171da177e4SLinus Torvalds  *         from.
8181da177e4SLinus Torvalds  *
8191da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8201da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8211da177e4SLinus Torvalds  * set, it can be read from, and written to.
8221da177e4SLinus Torvalds  *
8231da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8241da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8251da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
8266468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
8271da177e4SLinus Torvalds  *
8286468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
8291da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
8306468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
8311da177e4SLinus Torvalds  * code.
8321da177e4SLinus Torvalds  */
833f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
834621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8351da177e4SLinus Torvalds {
8364d45f797SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
8376713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8401da177e4SLinus Torvalds 
841dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
842dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
843dd308bc3SMichael Ellerman {
844dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
84569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
84683b711cbSNicolai Stange 	ssize_t r;
84783b711cbSNicolai Stange 
84869d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
84969d29f9eSNicolai Stange 	if (unlikely(r))
85069d29f9eSNicolai Stange 		return r;
85183b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
852dd308bc3SMichael Ellerman 				blob->size);
85369d29f9eSNicolai Stange 	debugfs_file_put(dentry);
85483b711cbSNicolai Stange 	return r;
855dd308bc3SMichael Ellerman }
856dd308bc3SMichael Ellerman 
85700977a59SArjan van de Ven static const struct file_operations fops_blob = {
858dd308bc3SMichael Ellerman 	.read =		read_file_blob,
859234e3405SStephen Boyd 	.open =		simple_open,
8606038f373SArnd Bergmann 	.llseek =	default_llseek,
861dd308bc3SMichael Ellerman };
862dd308bc3SMichael Ellerman 
863dd308bc3SMichael Ellerman /**
864400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
865dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
866dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
867dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
8686468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
869dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
870dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
871dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
872dd308bc3SMichael Ellerman  *
873dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
874dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
875dd308bc3SMichael Ellerman  * read from. Writing is not supported.
876dd308bc3SMichael Ellerman  *
877dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
878dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
879dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
8806468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
881dd308bc3SMichael Ellerman  *
8826468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
883dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
8846468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
885dd308bc3SMichael Ellerman  * code.
886dd308bc3SMichael Ellerman  */
887f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
888dd308bc3SMichael Ellerman 				   struct dentry *parent,
889dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
890dd308bc3SMichael Ellerman {
89183b711cbSNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
892dd308bc3SMichael Ellerman }
893dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
8941a087c6aSAlessandro Rubini 
8959fe2a701SSrivatsa Vaddagiri struct array_data {
8969fe2a701SSrivatsa Vaddagiri 	void *array;
8979fe2a701SSrivatsa Vaddagiri 	u32 elements;
8989fe2a701SSrivatsa Vaddagiri };
8999fe2a701SSrivatsa Vaddagiri 
900e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
901e05e279eSLinus Torvalds 			       u32 *array, int array_size)
9029fe2a701SSrivatsa Vaddagiri {
9039fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
9049fe2a701SSrivatsa Vaddagiri 
905e05e279eSLinus Torvalds 	while (--array_size >= 0) {
9069fe2a701SSrivatsa Vaddagiri 		size_t len;
907e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
9089fe2a701SSrivatsa Vaddagiri 
909e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
9109fe2a701SSrivatsa Vaddagiri 		ret += len;
9119fe2a701SSrivatsa Vaddagiri 
9129fe2a701SSrivatsa Vaddagiri 		buf += len;
9139fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
9149fe2a701SSrivatsa Vaddagiri 	}
9159fe2a701SSrivatsa Vaddagiri 	return ret;
9169fe2a701SSrivatsa Vaddagiri }
9179fe2a701SSrivatsa Vaddagiri 
91836048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
9199fe2a701SSrivatsa Vaddagiri {
9209fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
921e05e279eSLinus Torvalds 	int size, elements = data->elements;
922e05e279eSLinus Torvalds 	char *buf;
9239fe2a701SSrivatsa Vaddagiri 
924e05e279eSLinus Torvalds 	/*
925e05e279eSLinus Torvalds 	 * Max size:
926e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
927e05e279eSLinus Torvalds 	 *  - terminating NUL character
928e05e279eSLinus Torvalds 	 */
929e05e279eSLinus Torvalds 	size = elements*11;
930e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
931e05e279eSLinus Torvalds 	if (!buf)
93236048853SDavid Rientjes 		return -ENOMEM;
933e05e279eSLinus Torvalds 	buf[size] = 0;
934e05e279eSLinus Torvalds 
935e05e279eSLinus Torvalds 	file->private_data = buf;
936e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
937e05e279eSLinus Torvalds 
93836048853SDavid Rientjes 	return nonseekable_open(inode, file);
9399fe2a701SSrivatsa Vaddagiri }
9409fe2a701SSrivatsa Vaddagiri 
94136048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
94236048853SDavid Rientjes 			      loff_t *ppos)
94336048853SDavid Rientjes {
94436048853SDavid Rientjes 	size_t size = strlen(file->private_data);
9459fe2a701SSrivatsa Vaddagiri 
9469fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
9479fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
9489fe2a701SSrivatsa Vaddagiri }
9499fe2a701SSrivatsa Vaddagiri 
9509fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
9519fe2a701SSrivatsa Vaddagiri {
9529fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
9539fe2a701SSrivatsa Vaddagiri 
9549fe2a701SSrivatsa Vaddagiri 	return 0;
9559fe2a701SSrivatsa Vaddagiri }
9569fe2a701SSrivatsa Vaddagiri 
9579fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
9589fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
9599fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
9609fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
9619fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
9629fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
9639fe2a701SSrivatsa Vaddagiri };
9649fe2a701SSrivatsa Vaddagiri 
9659fe2a701SSrivatsa Vaddagiri /**
9669fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
9679fe2a701SSrivatsa Vaddagiri  * array.
9689fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
9699fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
9709fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
9719fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
9729fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
9739fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
9749fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
9759fe2a701SSrivatsa Vaddagiri  *
9769fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
9779fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
9789fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
9799fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
9809fe2a701SSrivatsa Vaddagiri  *
9819fe2a701SSrivatsa Vaddagiri  * The function returns a pointer to dentry on success. If debugfs is not
9829fe2a701SSrivatsa Vaddagiri  * enabled in the kernel, the value -%ENODEV will be returned.
9839fe2a701SSrivatsa Vaddagiri  */
9849fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
9859fe2a701SSrivatsa Vaddagiri 					    struct dentry *parent,
9869fe2a701SSrivatsa Vaddagiri 					    u32 *array, u32 elements)
9879fe2a701SSrivatsa Vaddagiri {
9889fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
9899fe2a701SSrivatsa Vaddagiri 
9909fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
9919fe2a701SSrivatsa Vaddagiri 		return NULL;
9929fe2a701SSrivatsa Vaddagiri 
9939fe2a701SSrivatsa Vaddagiri 	data->array = array;
9949fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
9959fe2a701SSrivatsa Vaddagiri 
996c4a74f63SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, data,
997c4a74f63SNicolai Stange 					&u32_array_fops);
9989fe2a701SSrivatsa Vaddagiri }
9999fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10009fe2a701SSrivatsa Vaddagiri 
10013b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
10023b85e4abSHeiko Carstens 
10031a087c6aSAlessandro Rubini /*
10041a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
10051a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
10061a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
10071a087c6aSAlessandro Rubini  */
10081a087c6aSAlessandro Rubini 
10091a087c6aSAlessandro Rubini /**
10101a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10111a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10121a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1013b5763accSRandy Dunlap  * @nregs: the length of the above array
10141a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10151a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
10161a087c6aSAlessandro Rubini  *
10171a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
10181a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
10191a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
10201a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
10211a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
10221a087c6aSAlessandro Rubini  * for example configuration of dma channels
10231a087c6aSAlessandro Rubini  */
10249761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
10251a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
10261a087c6aSAlessandro Rubini {
10279761536eSJoe Perches 	int i;
10281a087c6aSAlessandro Rubini 
10291a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
10301a087c6aSAlessandro Rubini 		if (prefix)
10319761536eSJoe Perches 			seq_printf(s, "%s", prefix);
10329761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
10335858441cSDan Carpenter 			   readl(base + regs->offset));
10349761536eSJoe Perches 		if (seq_has_overflowed(s))
10359761536eSJoe Perches 			break;
10361a087c6aSAlessandro Rubini 	}
10371a087c6aSAlessandro Rubini }
10381a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
10391a087c6aSAlessandro Rubini 
10401a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
10411a087c6aSAlessandro Rubini {
10421a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
10431a087c6aSAlessandro Rubini 
10441a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
10451a087c6aSAlessandro Rubini 	return 0;
10461a087c6aSAlessandro Rubini }
10471a087c6aSAlessandro Rubini 
10481a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
10491a087c6aSAlessandro Rubini {
10501a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
10511a087c6aSAlessandro Rubini }
10521a087c6aSAlessandro Rubini 
10531a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
10541a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
10551a087c6aSAlessandro Rubini 	.read =		seq_read,
10561a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
10571a087c6aSAlessandro Rubini 	.release =	single_release,
10581a087c6aSAlessandro Rubini };
10591a087c6aSAlessandro Rubini 
10601a087c6aSAlessandro Rubini /**
10611a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
10621a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
10631a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
10641a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
10651a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
10661a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
10671a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
10681a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
10691a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
10701a087c6aSAlessandro Rubini  *
10711a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
10721a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
10731a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
10741a087c6aSAlessandro Rubini  *
10751a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
10761a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
10771a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
10781a087c6aSAlessandro Rubini  * you are responsible here.)  If an error occurs, %NULL will be returned.
10791a087c6aSAlessandro Rubini  *
10801a087c6aSAlessandro Rubini  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
10811a087c6aSAlessandro Rubini  * returned.  It is not wise to check for this value, but rather, check for
10821a087c6aSAlessandro Rubini  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
10831a087c6aSAlessandro Rubini  * code.
10841a087c6aSAlessandro Rubini  */
108588187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
10861a087c6aSAlessandro Rubini 				       struct dentry *parent,
10871a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
10881a087c6aSAlessandro Rubini {
10891a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
10901a087c6aSAlessandro Rubini }
10911a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
10923b85e4abSHeiko Carstens 
10933b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
109498210b7fSArend van Spriel 
109598210b7fSArend van Spriel struct debugfs_devm_entry {
109698210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
109798210b7fSArend van Spriel 	struct device *dev;
109898210b7fSArend van Spriel };
109998210b7fSArend van Spriel 
110098210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
110198210b7fSArend van Spriel {
110298210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
110398210b7fSArend van Spriel 
110498210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
110598210b7fSArend van Spriel }
110698210b7fSArend van Spriel 
110798210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
110898210b7fSArend van Spriel 	.owner = THIS_MODULE,
110998210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
111098210b7fSArend van Spriel 	.release = single_release,
111198210b7fSArend van Spriel 	.read = seq_read,
111298210b7fSArend van Spriel 	.llseek = seq_lseek
111398210b7fSArend van Spriel };
111498210b7fSArend van Spriel 
111598210b7fSArend van Spriel /**
111698210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
111798210b7fSArend van Spriel  *
111898210b7fSArend van Spriel  * @dev: device related to this debugfs file.
111998210b7fSArend van Spriel  * @name: name of the debugfs file.
112098210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
112198210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
112298210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
112398210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
112498210b7fSArend van Spriel  */
112598210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
112698210b7fSArend van Spriel 					   struct dentry *parent,
112798210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
112898210b7fSArend van Spriel 							  void *data))
112998210b7fSArend van Spriel {
113098210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
113198210b7fSArend van Spriel 
113298210b7fSArend van Spriel 	if (IS_ERR(parent))
113398210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
113498210b7fSArend van Spriel 
113598210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
113698210b7fSArend van Spriel 	if (!entry)
113798210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
113898210b7fSArend van Spriel 
113998210b7fSArend van Spriel 	entry->read = read_fn;
114098210b7fSArend van Spriel 	entry->dev = dev;
114198210b7fSArend van Spriel 
114298210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
114398210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
114498210b7fSArend van Spriel }
114598210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
114698210b7fSArend van Spriel 
1147