xref: /linux-6.15/fs/debugfs/file.c (revision 4909f168)
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.
12883ce42eSRobert P. J. Day  *  See Documentation/DocBook/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>
259fd4dcecSNicolai Stange #include <linux/srcu.h>
2649d200deSNicolai Stange #include <asm/poll.h>
279fd4dcecSNicolai Stange 
289fd4dcecSNicolai Stange #include "internal.h"
291da177e4SLinus Torvalds 
3049d200deSNicolai Stange struct poll_table_struct;
3149d200deSNicolai Stange 
321da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
331da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	return 0;
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
391da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	return count;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
449fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
451da177e4SLinus Torvalds 	.read =		default_read_file,
461da177e4SLinus Torvalds 	.write =	default_write_file,
47234e3405SStephen Boyd 	.open =		simple_open,
486038f373SArnd Bergmann 	.llseek =	noop_llseek,
491da177e4SLinus Torvalds };
501da177e4SLinus Torvalds 
519fd4dcecSNicolai Stange /**
529fd4dcecSNicolai Stange  * debugfs_use_file_start - mark the beginning of file data access
539fd4dcecSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
549fd4dcecSNicolai Stange  * @srcu_idx: a pointer to some memory to store a SRCU index in.
559fd4dcecSNicolai Stange  *
569fd4dcecSNicolai Stange  * Up to a matching call to debugfs_use_file_finish(), any
579fd4dcecSNicolai Stange  * successive call into the file removing functions debugfs_remove()
589fd4dcecSNicolai Stange  * and debugfs_remove_recursive() will block. Since associated private
599fd4dcecSNicolai Stange  * file data may only get freed after a successful return of any of
609fd4dcecSNicolai Stange  * the removal functions, you may safely access it after a successful
619fd4dcecSNicolai Stange  * call to debugfs_use_file_start() without worrying about
629fd4dcecSNicolai Stange  * lifetime issues.
639fd4dcecSNicolai Stange  *
649fd4dcecSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
659fd4dcecSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
669fd4dcecSNicolai Stange  * it is allowed to access the file data, zero is returned.
679fd4dcecSNicolai Stange  *
689fd4dcecSNicolai Stange  * Regardless of the return code, any call to
699fd4dcecSNicolai Stange  * debugfs_use_file_start() must be followed by a matching call
709fd4dcecSNicolai Stange  * to debugfs_use_file_finish().
719fd4dcecSNicolai Stange  */
7249d200deSNicolai Stange int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
739fd4dcecSNicolai Stange 	__acquires(&debugfs_srcu)
749fd4dcecSNicolai Stange {
759fd4dcecSNicolai Stange 	*srcu_idx = srcu_read_lock(&debugfs_srcu);
769fd4dcecSNicolai Stange 	barrier();
779fd4dcecSNicolai Stange 	if (d_unlinked(dentry))
789fd4dcecSNicolai Stange 		return -EIO;
799fd4dcecSNicolai Stange 	return 0;
809fd4dcecSNicolai Stange }
8149d200deSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_use_file_start);
829fd4dcecSNicolai Stange 
839fd4dcecSNicolai Stange /**
849fd4dcecSNicolai Stange  * debugfs_use_file_finish - mark the end of file data access
859fd4dcecSNicolai Stange  * @srcu_idx: the SRCU index "created" by a former call to
869fd4dcecSNicolai Stange  *            debugfs_use_file_start().
879fd4dcecSNicolai Stange  *
889fd4dcecSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
899fd4dcecSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
909fd4dcecSNicolai Stange  * debugfs_use_file_start() to proceed and return to its caller.
919fd4dcecSNicolai Stange  */
9249d200deSNicolai Stange void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
939fd4dcecSNicolai Stange {
949fd4dcecSNicolai Stange 	srcu_read_unlock(&debugfs_srcu, srcu_idx);
959fd4dcecSNicolai Stange }
9649d200deSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
979fd4dcecSNicolai Stange 
989fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
999fd4dcecSNicolai Stange 
1009fd4dcecSNicolai Stange #define REAL_FOPS_DEREF(dentry)					\
1019fd4dcecSNicolai Stange 	((const struct file_operations *)(dentry)->d_fsdata)
1029fd4dcecSNicolai Stange 
1039fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
1049fd4dcecSNicolai Stange {
1059fd4dcecSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
1069fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
1079fd4dcecSNicolai Stange 	int srcu_idx, r;
1089fd4dcecSNicolai Stange 
1099fd4dcecSNicolai Stange 	r = debugfs_use_file_start(dentry, &srcu_idx);
1109fd4dcecSNicolai Stange 	if (r) {
1119fd4dcecSNicolai Stange 		r = -ENOENT;
1129fd4dcecSNicolai Stange 		goto out;
1139fd4dcecSNicolai Stange 	}
1149fd4dcecSNicolai Stange 
1159fd4dcecSNicolai Stange 	real_fops = REAL_FOPS_DEREF(dentry);
1169fd4dcecSNicolai Stange 	real_fops = fops_get(real_fops);
1179fd4dcecSNicolai Stange 	if (!real_fops) {
1189fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
1199fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
1209fd4dcecSNicolai Stange 			dentry);
1219fd4dcecSNicolai Stange 		r = -ENXIO;
1229fd4dcecSNicolai Stange 		goto out;
1239fd4dcecSNicolai Stange 	}
1249fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
1259fd4dcecSNicolai Stange 
1269fd4dcecSNicolai Stange 	if (real_fops->open)
1279fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
1289fd4dcecSNicolai Stange 
1299fd4dcecSNicolai Stange out:
1309fd4dcecSNicolai Stange 	fops_put(real_fops);
1319fd4dcecSNicolai Stange 	debugfs_use_file_finish(srcu_idx);
1329fd4dcecSNicolai Stange 	return r;
1339fd4dcecSNicolai Stange }
1349fd4dcecSNicolai Stange 
1359fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
1369fd4dcecSNicolai Stange 	.open = open_proxy_open,
1379fd4dcecSNicolai Stange };
1389fd4dcecSNicolai Stange 
13949d200deSNicolai Stange #define PROTO(args...) args
14049d200deSNicolai Stange #define ARGS(args...) args
14149d200deSNicolai Stange 
14249d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
14349d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
14449d200deSNicolai Stange {									\
14549d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);			\
14649d200deSNicolai Stange 	const struct file_operations *real_fops =			\
14749d200deSNicolai Stange 		REAL_FOPS_DEREF(dentry);				\
14849d200deSNicolai Stange 	int srcu_idx;							\
14949d200deSNicolai Stange 	ret_type r;							\
15049d200deSNicolai Stange 									\
15149d200deSNicolai Stange 	r = debugfs_use_file_start(dentry, &srcu_idx);			\
15249d200deSNicolai Stange 	if (likely(!r))						\
15349d200deSNicolai Stange 		r = real_fops->name(args);				\
15449d200deSNicolai Stange 	debugfs_use_file_finish(srcu_idx);				\
15549d200deSNicolai Stange 	return r;							\
15649d200deSNicolai Stange }
15749d200deSNicolai Stange 
15849d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp,
15949d200deSNicolai Stange 		PROTO(struct file *filp, loff_t offset, int whence),
16049d200deSNicolai Stange 		ARGS(filp, offset, whence));
16149d200deSNicolai Stange 
16249d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp,
16349d200deSNicolai Stange 		PROTO(struct file *filp, char __user *buf, size_t size,
16449d200deSNicolai Stange 			loff_t *ppos),
16549d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
16649d200deSNicolai Stange 
16749d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp,
16849d200deSNicolai Stange 		PROTO(struct file *filp, const char __user *buf, size_t size,
16949d200deSNicolai Stange 			loff_t *ppos),
17049d200deSNicolai Stange 		ARGS(filp, buf, size, ppos));
17149d200deSNicolai Stange 
17249d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
17349d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
17449d200deSNicolai Stange 		ARGS(filp, cmd, arg));
17549d200deSNicolai Stange 
17649d200deSNicolai Stange static unsigned int full_proxy_poll(struct file *filp,
17749d200deSNicolai Stange 				struct poll_table_struct *wait)
17849d200deSNicolai Stange {
17949d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
18049d200deSNicolai Stange 	const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
18149d200deSNicolai Stange 	int srcu_idx;
18249d200deSNicolai Stange 	unsigned int r = 0;
18349d200deSNicolai Stange 
18449d200deSNicolai Stange 	if (debugfs_use_file_start(dentry, &srcu_idx)) {
18549d200deSNicolai Stange 		debugfs_use_file_finish(srcu_idx);
18649d200deSNicolai Stange 		return POLLHUP;
18749d200deSNicolai Stange 	}
18849d200deSNicolai Stange 
18949d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
19049d200deSNicolai Stange 	debugfs_use_file_finish(srcu_idx);
19149d200deSNicolai Stange 	return r;
19249d200deSNicolai Stange }
19349d200deSNicolai Stange 
19449d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
19549d200deSNicolai Stange {
19649d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
19749d200deSNicolai Stange 	const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
19849d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
19949d200deSNicolai Stange 	int r = 0;
20049d200deSNicolai Stange 
20149d200deSNicolai Stange 	/*
20249d200deSNicolai Stange 	 * We must not protect this against removal races here: the
20349d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
20449d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
20549d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
20649d200deSNicolai Stange 	 */
20749d200deSNicolai Stange 	if (real_fops->release)
20849d200deSNicolai Stange 		r = real_fops->release(inode, filp);
20949d200deSNicolai Stange 
21049d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
21149d200deSNicolai Stange 	kfree((void *)proxy_fops);
21249d200deSNicolai Stange 	fops_put(real_fops);
21349d200deSNicolai Stange 	return 0;
21449d200deSNicolai Stange }
21549d200deSNicolai Stange 
21649d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
21749d200deSNicolai Stange 				const struct file_operations *real_fops)
21849d200deSNicolai Stange {
21949d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
22049d200deSNicolai Stange 	if (real_fops->llseek)
22149d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
22249d200deSNicolai Stange 	if (real_fops->read)
22349d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
22449d200deSNicolai Stange 	if (real_fops->write)
22549d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
22649d200deSNicolai Stange 	if (real_fops->poll)
22749d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
22849d200deSNicolai Stange 	if (real_fops->unlocked_ioctl)
22949d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
23049d200deSNicolai Stange }
23149d200deSNicolai Stange 
23249d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
23349d200deSNicolai Stange {
23449d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
23549d200deSNicolai Stange 	const struct file_operations *real_fops = NULL;
23649d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
23749d200deSNicolai Stange 	int srcu_idx, r;
23849d200deSNicolai Stange 
23949d200deSNicolai Stange 	r = debugfs_use_file_start(dentry, &srcu_idx);
24049d200deSNicolai Stange 	if (r) {
24149d200deSNicolai Stange 		r = -ENOENT;
24249d200deSNicolai Stange 		goto out;
24349d200deSNicolai Stange 	}
24449d200deSNicolai Stange 
24549d200deSNicolai Stange 	real_fops = REAL_FOPS_DEREF(dentry);
24649d200deSNicolai Stange 	real_fops = fops_get(real_fops);
24749d200deSNicolai Stange 	if (!real_fops) {
24849d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
24949d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
25049d200deSNicolai Stange 			dentry);
25149d200deSNicolai Stange 		r = -ENXIO;
25249d200deSNicolai Stange 		goto out;
25349d200deSNicolai Stange 	}
25449d200deSNicolai Stange 
25549d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
25649d200deSNicolai Stange 	if (!proxy_fops) {
25749d200deSNicolai Stange 		r = -ENOMEM;
25849d200deSNicolai Stange 		goto free_proxy;
25949d200deSNicolai Stange 	}
26049d200deSNicolai Stange 	__full_proxy_fops_init(proxy_fops, real_fops);
26149d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
26249d200deSNicolai Stange 
26349d200deSNicolai Stange 	if (real_fops->open) {
26449d200deSNicolai Stange 		r = real_fops->open(inode, filp);
26549d200deSNicolai Stange 
26649d200deSNicolai Stange 		if (filp->f_op != proxy_fops) {
26749d200deSNicolai Stange 			/* No protection against file removal anymore. */
26849d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
26949d200deSNicolai Stange 				dentry);
27049d200deSNicolai Stange 			goto free_proxy;
27149d200deSNicolai Stange 		}
27249d200deSNicolai Stange 	}
27349d200deSNicolai Stange 
27449d200deSNicolai Stange 	goto out;
27549d200deSNicolai Stange free_proxy:
27649d200deSNicolai Stange 	kfree(proxy_fops);
27749d200deSNicolai Stange 	fops_put(real_fops);
27849d200deSNicolai Stange out:
27949d200deSNicolai Stange 	debugfs_use_file_finish(srcu_idx);
28049d200deSNicolai Stange 	return r;
28149d200deSNicolai Stange }
28249d200deSNicolai Stange 
28349d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
28449d200deSNicolai Stange 	.open = full_proxy_open,
28549d200deSNicolai Stange };
28649d200deSNicolai Stange 
287c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
288c6468808SNicolai Stange 			size_t len, loff_t *ppos)
289c6468808SNicolai Stange {
290c6468808SNicolai Stange 	ssize_t ret;
291c6468808SNicolai Stange 	int srcu_idx;
292c6468808SNicolai Stange 
293c6468808SNicolai Stange 	ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
294c6468808SNicolai Stange 	if (likely(!ret))
295c6468808SNicolai Stange 		ret = simple_attr_read(file, buf, len, ppos);
296c6468808SNicolai Stange 	debugfs_use_file_finish(srcu_idx);
297c6468808SNicolai Stange 	return ret;
298c6468808SNicolai Stange }
299c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
300c6468808SNicolai Stange 
301c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
302c6468808SNicolai Stange 			 size_t len, loff_t *ppos)
303c6468808SNicolai Stange {
304c6468808SNicolai Stange 	ssize_t ret;
305c6468808SNicolai Stange 	int srcu_idx;
306c6468808SNicolai Stange 
307c6468808SNicolai Stange 	ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
308c6468808SNicolai Stange 	if (likely(!ret))
309c6468808SNicolai Stange 		ret = simple_attr_write(file, buf, len, ppos);
310c6468808SNicolai Stange 	debugfs_use_file_finish(srcu_idx);
311c6468808SNicolai Stange 	return ret;
312c6468808SNicolai Stange }
313c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
314c6468808SNicolai Stange 
315b97f6799SStephen Boyd static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
316b97f6799SStephen Boyd 					  struct dentry *parent, void *value,
317b97f6799SStephen Boyd 				          const struct file_operations *fops,
318b97f6799SStephen Boyd 				          const struct file_operations *fops_ro,
319b97f6799SStephen Boyd 				          const struct file_operations *fops_wo)
320b97f6799SStephen Boyd {
321b97f6799SStephen Boyd 	/* if there are no write bits set, make read only */
322b97f6799SStephen Boyd 	if (!(mode & S_IWUGO))
323b97f6799SStephen Boyd 		return debugfs_create_file(name, mode, parent, value, fops_ro);
324b97f6799SStephen Boyd 	/* if there are no read bits set, make write only */
325b97f6799SStephen Boyd 	if (!(mode & S_IRUGO))
326b97f6799SStephen Boyd 		return debugfs_create_file(name, mode, parent, value, fops_wo);
327b97f6799SStephen Boyd 
328b97f6799SStephen Boyd 	return debugfs_create_file(name, mode, parent, value, fops);
329b97f6799SStephen Boyd }
330b97f6799SStephen Boyd 
331*4909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
332*4909f168SNicolai Stange 					struct dentry *parent, void *value,
333*4909f168SNicolai Stange 					const struct file_operations *fops,
334*4909f168SNicolai Stange 					const struct file_operations *fops_ro,
335*4909f168SNicolai Stange 					const struct file_operations *fops_wo)
336*4909f168SNicolai Stange {
337*4909f168SNicolai Stange 	/* if there are no write bits set, make read only */
338*4909f168SNicolai Stange 	if (!(mode & S_IWUGO))
339*4909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
340*4909f168SNicolai Stange 						fops_ro);
341*4909f168SNicolai Stange 	/* if there are no read bits set, make write only */
342*4909f168SNicolai Stange 	if (!(mode & S_IRUGO))
343*4909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
344*4909f168SNicolai Stange 						fops_wo);
345*4909f168SNicolai Stange 
346*4909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
347*4909f168SNicolai Stange }
348*4909f168SNicolai Stange 
3498b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
350acaefc25SArnd Bergmann {
351acaefc25SArnd Bergmann 	*(u8 *)data = val;
3528b88b099SChristoph Hellwig 	return 0;
353acaefc25SArnd Bergmann }
3548b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
355acaefc25SArnd Bergmann {
3568b88b099SChristoph Hellwig 	*val = *(u8 *)data;
3578b88b099SChristoph Hellwig 	return 0;
358acaefc25SArnd Bergmann }
359*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
360*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
361*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /**
3646468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
3651da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3661da177e4SLinus Torvalds  * @mode: the permission that the file should have
3671da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3686468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3691da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3701da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3711da177e4SLinus Torvalds  *         from.
3721da177e4SLinus Torvalds  *
3731da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3741da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3751da177e4SLinus Torvalds  * set, it can be read from, and written to.
3761da177e4SLinus Torvalds  *
3771da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3781da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3791da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3806468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3811da177e4SLinus Torvalds  *
3826468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
3831da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
3846468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
3851da177e4SLinus Torvalds  * code.
3861da177e4SLinus Torvalds  */
387f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode,
3881da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
3891da177e4SLinus Torvalds {
390*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
391b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
3941da177e4SLinus Torvalds 
3958b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
396acaefc25SArnd Bergmann {
397acaefc25SArnd Bergmann 	*(u16 *)data = val;
3988b88b099SChristoph Hellwig 	return 0;
399acaefc25SArnd Bergmann }
4008b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
401acaefc25SArnd Bergmann {
4028b88b099SChristoph Hellwig 	*val = *(u16 *)data;
4038b88b099SChristoph Hellwig 	return 0;
404acaefc25SArnd Bergmann }
405*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
406*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
407*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
408acaefc25SArnd Bergmann 
4091da177e4SLinus Torvalds /**
4106468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
4111da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4121da177e4SLinus Torvalds  * @mode: the permission that the file should have
4131da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4146468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4151da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4161da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4171da177e4SLinus Torvalds  *         from.
4181da177e4SLinus Torvalds  *
4191da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4201da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4211da177e4SLinus Torvalds  * set, it can be read from, and written to.
4221da177e4SLinus Torvalds  *
4231da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4241da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4251da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4266468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4271da177e4SLinus Torvalds  *
4286468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4291da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4306468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4311da177e4SLinus Torvalds  * code.
4321da177e4SLinus Torvalds  */
433f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode,
4341da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
4351da177e4SLinus Torvalds {
436*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
437b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
4401da177e4SLinus Torvalds 
4418b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
442acaefc25SArnd Bergmann {
443acaefc25SArnd Bergmann 	*(u32 *)data = val;
4448b88b099SChristoph Hellwig 	return 0;
445acaefc25SArnd Bergmann }
4468b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
447acaefc25SArnd Bergmann {
4488b88b099SChristoph Hellwig 	*val = *(u32 *)data;
4498b88b099SChristoph Hellwig 	return 0;
450acaefc25SArnd Bergmann }
451*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
452*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
453*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
454acaefc25SArnd Bergmann 
4551da177e4SLinus Torvalds /**
4566468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
4571da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4581da177e4SLinus Torvalds  * @mode: the permission that the file should have
4591da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4606468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4611da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4621da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4631da177e4SLinus Torvalds  *         from.
4641da177e4SLinus Torvalds  *
4651da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4661da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4671da177e4SLinus Torvalds  * set, it can be read from, and written to.
4681da177e4SLinus Torvalds  *
4691da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4701da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4711da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4726468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4731da177e4SLinus Torvalds  *
4746468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4751da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4766468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4771da177e4SLinus Torvalds  * code.
4781da177e4SLinus Torvalds  */
479f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
4801da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
4811da177e4SLinus Torvalds {
482*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
483b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
4861da177e4SLinus Torvalds 
4878b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
4888447891fSMichael Ellerman {
4898447891fSMichael Ellerman 	*(u64 *)data = val;
4908b88b099SChristoph Hellwig 	return 0;
4918447891fSMichael Ellerman }
4928447891fSMichael Ellerman 
4938b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
4948447891fSMichael Ellerman {
4958b88b099SChristoph Hellwig 	*val = *(u64 *)data;
4968b88b099SChristoph Hellwig 	return 0;
4978447891fSMichael Ellerman }
498*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
499*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
500*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
5018447891fSMichael Ellerman 
5028447891fSMichael Ellerman /**
5038447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
5048447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
5058447891fSMichael Ellerman  * @mode: the permission that the file should have
5068447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5078447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
5088447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
5098447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
5108447891fSMichael Ellerman  *         from.
5118447891fSMichael Ellerman  *
5128447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
5138447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
5148447891fSMichael Ellerman  * set, it can be read from, and written to.
5158447891fSMichael Ellerman  *
5168447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
5178447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
5188447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
5198447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
5208447891fSMichael Ellerman  *
5218447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
5228447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
5238447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
5248447891fSMichael Ellerman  * code.
5258447891fSMichael Ellerman  */
526f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode,
5278447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
5288447891fSMichael Ellerman {
529*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
530b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
5318447891fSMichael Ellerman }
5328447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
5338447891fSMichael Ellerman 
534c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
535c23fe831SViresh Kumar {
536c23fe831SViresh Kumar 	*(unsigned long *)data = val;
537c23fe831SViresh Kumar 	return 0;
538c23fe831SViresh Kumar }
539c23fe831SViresh Kumar 
540c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
541c23fe831SViresh Kumar {
542c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
543c23fe831SViresh Kumar 	return 0;
544c23fe831SViresh Kumar }
545*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
546*4909f168SNicolai Stange 			"%llu\n");
547*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
548*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
549c23fe831SViresh Kumar 
550c23fe831SViresh Kumar /**
551c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
552c23fe831SViresh Kumar  * an unsigned long value.
553c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
554c23fe831SViresh Kumar  * @mode: the permission that the file should have
555c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
556c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
557c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
558c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
559c23fe831SViresh Kumar  *         from.
560c23fe831SViresh Kumar  *
561c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
562c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
563c23fe831SViresh Kumar  * set, it can be read from, and written to.
564c23fe831SViresh Kumar  *
565c23fe831SViresh Kumar  * This function will return a pointer to a dentry if it succeeds.  This
566c23fe831SViresh Kumar  * pointer must be passed to the debugfs_remove() function when the file is
567c23fe831SViresh Kumar  * to be removed (no automatic cleanup happens if your module is unloaded,
568c23fe831SViresh Kumar  * you are responsible here.)  If an error occurs, %NULL will be returned.
569c23fe831SViresh Kumar  *
570c23fe831SViresh Kumar  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
571c23fe831SViresh Kumar  * returned.  It is not wise to check for this value, but rather, check for
572c23fe831SViresh Kumar  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
573c23fe831SViresh Kumar  * code.
574c23fe831SViresh Kumar  */
575c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
576c23fe831SViresh Kumar 				    struct dentry *parent, unsigned long *value)
577c23fe831SViresh Kumar {
578*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
579*4909f168SNicolai Stange 					&fops_ulong, &fops_ulong_ro,
580*4909f168SNicolai Stange 					&fops_ulong_wo);
581c23fe831SViresh Kumar }
582c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
583c23fe831SViresh Kumar 
584*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
585*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
586*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
5872ebefc50SRobin Getz 
588*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
589*4909f168SNicolai Stange 			"0x%04llx\n");
590*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
591*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
5922ebefc50SRobin Getz 
593*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
594*4909f168SNicolai Stange 			"0x%08llx\n");
595*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
596*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
5972ebefc50SRobin Getz 
598*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
599*4909f168SNicolai Stange 			"0x%016llx\n");
600*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
601*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
60215b0beaaSHuang Ying 
603e6716b87SRandy Dunlap /*
60415b0beaaSHuang 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
605e6716b87SRandy Dunlap  *
606e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
607e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
608e6716b87SRandy Dunlap  * decimal functions.
609e6716b87SRandy Dunlap  */
610e6716b87SRandy Dunlap 
6112ebefc50SRobin Getz /**
6122ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
613e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
614e6716b87SRandy Dunlap  * @mode: the permission that the file should have
615e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
616e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
617e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
618e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
619e6716b87SRandy Dunlap  *         from.
6202ebefc50SRobin Getz  */
621f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
6222ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
6232ebefc50SRobin Getz {
624*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
625b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
6262ebefc50SRobin Getz }
6272ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
6282ebefc50SRobin Getz 
629e6716b87SRandy Dunlap /**
630e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
631e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
632e6716b87SRandy Dunlap  * @mode: the permission that the file should have
633e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
634e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
635e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
636e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
637e6716b87SRandy Dunlap  *         from.
638e6716b87SRandy Dunlap  */
639f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode,
6402ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
6412ebefc50SRobin Getz {
642*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
643b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
6442ebefc50SRobin Getz }
6452ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
6462ebefc50SRobin Getz 
647e6716b87SRandy Dunlap /**
648e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
649e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
650e6716b87SRandy Dunlap  * @mode: the permission that the file should have
651e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
652e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
653e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
654e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
655e6716b87SRandy Dunlap  *         from.
656e6716b87SRandy Dunlap  */
657f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode,
6582ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
6592ebefc50SRobin Getz {
660*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
661b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
6622ebefc50SRobin Getz }
6632ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
6642ebefc50SRobin Getz 
66515b0beaaSHuang Ying /**
66615b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
66715b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
66815b0beaaSHuang Ying  * @mode: the permission that the file should have
66915b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
67015b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
67115b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
67215b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
67315b0beaaSHuang Ying  *         from.
67415b0beaaSHuang Ying  */
675f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
67615b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
67715b0beaaSHuang Ying {
678*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
67982b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
68015b0beaaSHuang Ying }
68115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
68215b0beaaSHuang Ying 
6835e078787SInaky Perez-Gonzalez 
6845e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
6855e078787SInaky Perez-Gonzalez {
6865e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
6875e078787SInaky Perez-Gonzalez 	return 0;
6885e078787SInaky Perez-Gonzalez }
6895e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
6905e078787SInaky Perez-Gonzalez {
6915e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
6925e078787SInaky Perez-Gonzalez 	return 0;
6935e078787SInaky Perez-Gonzalez }
694*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
6955e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
696*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
697*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
6985e078787SInaky Perez-Gonzalez 
6995e078787SInaky Perez-Gonzalez /**
7005e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
7015e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
7025e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
7035e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
7045e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
7055e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
7065e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
7075e078787SInaky Perez-Gonzalez  *         from.
7085e078787SInaky Perez-Gonzalez  */
709f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
7105e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
7115e078787SInaky Perez-Gonzalez {
712*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
713*4909f168SNicolai Stange 					&fops_size_t, &fops_size_t_ro,
714*4909f168SNicolai Stange 					&fops_size_t_wo);
7155e078787SInaky Perez-Gonzalez }
7165e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
7175e078787SInaky Perez-Gonzalez 
7183a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
7193a76e5e0SSeth Jennings {
7203a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
7213a76e5e0SSeth Jennings 	return 0;
7223a76e5e0SSeth Jennings }
7233a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
7243a76e5e0SSeth Jennings {
7253a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
7263a76e5e0SSeth Jennings 	return 0;
7273a76e5e0SSeth Jennings }
728*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
7293a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
730*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
731*4909f168SNicolai Stange 			"%lld\n");
732*4909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
733*4909f168SNicolai Stange 			"%lld\n");
7343a76e5e0SSeth Jennings 
7353a76e5e0SSeth Jennings /**
7363a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
7373a76e5e0SSeth Jennings  * write an atomic_t value
7383a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
7393a76e5e0SSeth Jennings  * @mode: the permission that the file should have
7403a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
7413a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
7423a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
7433a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
7443a76e5e0SSeth Jennings  *         from.
7453a76e5e0SSeth Jennings  */
7463a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
7473a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
7483a76e5e0SSeth Jennings {
749*4909f168SNicolai Stange 	return debugfs_create_mode_unsafe(name, mode, parent, value,
750*4909f168SNicolai Stange 					&fops_atomic_t, &fops_atomic_t_ro,
751*4909f168SNicolai Stange 					&fops_atomic_t_wo);
7523a76e5e0SSeth Jennings }
7533a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
7545e078787SInaky Perez-Gonzalez 
7550642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
7561da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	char buf[3];
759621a5f7aSViresh Kumar 	bool *val = file->private_data;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (*val)
7621da177e4SLinus Torvalds 		buf[0] = 'Y';
7631da177e4SLinus Torvalds 	else
7641da177e4SLinus Torvalds 		buf[0] = 'N';
7651da177e4SLinus Torvalds 	buf[1] = '\n';
7661da177e4SLinus Torvalds 	buf[2] = 0x00;
7671da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
7681da177e4SLinus Torvalds }
7690642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
7701da177e4SLinus Torvalds 
7710642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
7721da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	char buf[32];
775c42d2237SStephen Boyd 	size_t buf_size;
7768705b48eSJonathan Cameron 	bool bv;
777621a5f7aSViresh Kumar 	bool *val = file->private_data;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
7801da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
7811da177e4SLinus Torvalds 		return -EFAULT;
7821da177e4SLinus Torvalds 
783a3b2c8c7SMathias Krause 	buf[buf_size] = '\0';
7848705b48eSJonathan Cameron 	if (strtobool(buf, &bv) == 0)
7858705b48eSJonathan Cameron 		*val = bv;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	return count;
7881da177e4SLinus Torvalds }
7890642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
7901da177e4SLinus Torvalds 
7914b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
7920642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
7930642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
794234e3405SStephen Boyd 	.open =		simple_open,
7956038f373SArnd Bergmann 	.llseek =	default_llseek,
7961da177e4SLinus Torvalds };
7971da177e4SLinus Torvalds 
7986713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
7996713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
8006713e8fbSStephen Boyd 	.open =		simple_open,
8016713e8fbSStephen Boyd 	.llseek =	default_llseek,
8026713e8fbSStephen Boyd };
8036713e8fbSStephen Boyd 
8046713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
8056713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
8066713e8fbSStephen Boyd 	.open =		simple_open,
8076713e8fbSStephen Boyd 	.llseek =	default_llseek,
8086713e8fbSStephen Boyd };
8096713e8fbSStephen Boyd 
8101da177e4SLinus Torvalds /**
8116468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
8121da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
8131da177e4SLinus Torvalds  * @mode: the permission that the file should have
8141da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
8156468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
8161da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
8171da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
8181da177e4SLinus Torvalds  *         from.
8191da177e4SLinus Torvalds  *
8201da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
8211da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
8221da177e4SLinus Torvalds  * set, it can be read from, and written to.
8231da177e4SLinus Torvalds  *
8241da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
8251da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
8261da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
8276468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
8281da177e4SLinus Torvalds  *
8296468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
8301da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
8316468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
8321da177e4SLinus Torvalds  * code.
8331da177e4SLinus Torvalds  */
834f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
835621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
8361da177e4SLinus Torvalds {
8376713e8fbSStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_bool,
8386713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
8411da177e4SLinus Torvalds 
842dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
843dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
844dd308bc3SMichael Ellerman {
845dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
846dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
847dd308bc3SMichael Ellerman 			blob->size);
848dd308bc3SMichael Ellerman }
849dd308bc3SMichael Ellerman 
85000977a59SArjan van de Ven static const struct file_operations fops_blob = {
851dd308bc3SMichael Ellerman 	.read =		read_file_blob,
852234e3405SStephen Boyd 	.open =		simple_open,
8536038f373SArnd Bergmann 	.llseek =	default_llseek,
854dd308bc3SMichael Ellerman };
855dd308bc3SMichael Ellerman 
856dd308bc3SMichael Ellerman /**
857400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
858dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
859dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
860dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
8616468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
862dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
863dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
864dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
865dd308bc3SMichael Ellerman  *
866dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
867dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
868dd308bc3SMichael Ellerman  * read from. Writing is not supported.
869dd308bc3SMichael Ellerman  *
870dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
871dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
872dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
8736468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
874dd308bc3SMichael Ellerman  *
8756468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
876dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
8776468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
878dd308bc3SMichael Ellerman  * code.
879dd308bc3SMichael Ellerman  */
880f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
881dd308bc3SMichael Ellerman 				   struct dentry *parent,
882dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
883dd308bc3SMichael Ellerman {
884dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
885dd308bc3SMichael Ellerman }
886dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
8871a087c6aSAlessandro Rubini 
8889fe2a701SSrivatsa Vaddagiri struct array_data {
8899fe2a701SSrivatsa Vaddagiri 	void *array;
8909fe2a701SSrivatsa Vaddagiri 	u32 elements;
8919fe2a701SSrivatsa Vaddagiri };
8929fe2a701SSrivatsa Vaddagiri 
893e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
894e05e279eSLinus Torvalds 			       u32 *array, int array_size)
8959fe2a701SSrivatsa Vaddagiri {
8969fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
8979fe2a701SSrivatsa Vaddagiri 
898e05e279eSLinus Torvalds 	while (--array_size >= 0) {
8999fe2a701SSrivatsa Vaddagiri 		size_t len;
900e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
9019fe2a701SSrivatsa Vaddagiri 
902e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
9039fe2a701SSrivatsa Vaddagiri 		ret += len;
9049fe2a701SSrivatsa Vaddagiri 
9059fe2a701SSrivatsa Vaddagiri 		buf += len;
9069fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
9079fe2a701SSrivatsa Vaddagiri 	}
9089fe2a701SSrivatsa Vaddagiri 	return ret;
9099fe2a701SSrivatsa Vaddagiri }
9109fe2a701SSrivatsa Vaddagiri 
91136048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
9129fe2a701SSrivatsa Vaddagiri {
9139fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
914e05e279eSLinus Torvalds 	int size, elements = data->elements;
915e05e279eSLinus Torvalds 	char *buf;
9169fe2a701SSrivatsa Vaddagiri 
917e05e279eSLinus Torvalds 	/*
918e05e279eSLinus Torvalds 	 * Max size:
919e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
920e05e279eSLinus Torvalds 	 *  - terminating NUL character
921e05e279eSLinus Torvalds 	 */
922e05e279eSLinus Torvalds 	size = elements*11;
923e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
924e05e279eSLinus Torvalds 	if (!buf)
92536048853SDavid Rientjes 		return -ENOMEM;
926e05e279eSLinus Torvalds 	buf[size] = 0;
927e05e279eSLinus Torvalds 
928e05e279eSLinus Torvalds 	file->private_data = buf;
929e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
930e05e279eSLinus Torvalds 
93136048853SDavid Rientjes 	return nonseekable_open(inode, file);
9329fe2a701SSrivatsa Vaddagiri }
9339fe2a701SSrivatsa Vaddagiri 
93436048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
93536048853SDavid Rientjes 			      loff_t *ppos)
93636048853SDavid Rientjes {
93736048853SDavid Rientjes 	size_t size = strlen(file->private_data);
9389fe2a701SSrivatsa Vaddagiri 
9399fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
9409fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
9419fe2a701SSrivatsa Vaddagiri }
9429fe2a701SSrivatsa Vaddagiri 
9439fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
9449fe2a701SSrivatsa Vaddagiri {
9459fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
9469fe2a701SSrivatsa Vaddagiri 
9479fe2a701SSrivatsa Vaddagiri 	return 0;
9489fe2a701SSrivatsa Vaddagiri }
9499fe2a701SSrivatsa Vaddagiri 
9509fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
9519fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
9529fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
9539fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
9549fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
9559fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
9569fe2a701SSrivatsa Vaddagiri };
9579fe2a701SSrivatsa Vaddagiri 
9589fe2a701SSrivatsa Vaddagiri /**
9599fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
9609fe2a701SSrivatsa Vaddagiri  * array.
9619fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
9629fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
9639fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
9649fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
9659fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
9669fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
9679fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
9689fe2a701SSrivatsa Vaddagiri  *
9699fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
9709fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
9719fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
9729fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
9739fe2a701SSrivatsa Vaddagiri  *
9749fe2a701SSrivatsa Vaddagiri  * The function returns a pointer to dentry on success. If debugfs is not
9759fe2a701SSrivatsa Vaddagiri  * enabled in the kernel, the value -%ENODEV will be returned.
9769fe2a701SSrivatsa Vaddagiri  */
9779fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
9789fe2a701SSrivatsa Vaddagiri 					    struct dentry *parent,
9799fe2a701SSrivatsa Vaddagiri 					    u32 *array, u32 elements)
9809fe2a701SSrivatsa Vaddagiri {
9819fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
9829fe2a701SSrivatsa Vaddagiri 
9839fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
9849fe2a701SSrivatsa Vaddagiri 		return NULL;
9859fe2a701SSrivatsa Vaddagiri 
9869fe2a701SSrivatsa Vaddagiri 	data->array = array;
9879fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
9889fe2a701SSrivatsa Vaddagiri 
9899fe2a701SSrivatsa Vaddagiri 	return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
9909fe2a701SSrivatsa Vaddagiri }
9919fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
9929fe2a701SSrivatsa Vaddagiri 
9933b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
9943b85e4abSHeiko Carstens 
9951a087c6aSAlessandro Rubini /*
9961a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
9971a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
9981a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
9991a087c6aSAlessandro Rubini  */
10001a087c6aSAlessandro Rubini 
10011a087c6aSAlessandro Rubini /**
10021a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
10031a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
10041a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1005b5763accSRandy Dunlap  * @nregs: the length of the above array
10061a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
10071a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
10081a087c6aSAlessandro Rubini  *
10091a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
10101a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
10111a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
10121a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
10131a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
10141a087c6aSAlessandro Rubini  * for example configuration of dma channels
10151a087c6aSAlessandro Rubini  */
10169761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
10171a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
10181a087c6aSAlessandro Rubini {
10199761536eSJoe Perches 	int i;
10201a087c6aSAlessandro Rubini 
10211a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
10221a087c6aSAlessandro Rubini 		if (prefix)
10239761536eSJoe Perches 			seq_printf(s, "%s", prefix);
10249761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
10255858441cSDan Carpenter 			   readl(base + regs->offset));
10269761536eSJoe Perches 		if (seq_has_overflowed(s))
10279761536eSJoe Perches 			break;
10281a087c6aSAlessandro Rubini 	}
10291a087c6aSAlessandro Rubini }
10301a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
10311a087c6aSAlessandro Rubini 
10321a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
10331a087c6aSAlessandro Rubini {
10341a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
10351a087c6aSAlessandro Rubini 
10361a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
10371a087c6aSAlessandro Rubini 	return 0;
10381a087c6aSAlessandro Rubini }
10391a087c6aSAlessandro Rubini 
10401a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
10411a087c6aSAlessandro Rubini {
10421a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
10431a087c6aSAlessandro Rubini }
10441a087c6aSAlessandro Rubini 
10451a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
10461a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
10471a087c6aSAlessandro Rubini 	.read =		seq_read,
10481a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
10491a087c6aSAlessandro Rubini 	.release =	single_release,
10501a087c6aSAlessandro Rubini };
10511a087c6aSAlessandro Rubini 
10521a087c6aSAlessandro Rubini /**
10531a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
10541a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
10551a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
10561a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
10571a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
10581a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
10591a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
10601a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
10611a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
10621a087c6aSAlessandro Rubini  *
10631a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
10641a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
10651a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
10661a087c6aSAlessandro Rubini  *
10671a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
10681a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
10691a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
10701a087c6aSAlessandro Rubini  * you are responsible here.)  If an error occurs, %NULL will be returned.
10711a087c6aSAlessandro Rubini  *
10721a087c6aSAlessandro Rubini  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
10731a087c6aSAlessandro Rubini  * returned.  It is not wise to check for this value, but rather, check for
10741a087c6aSAlessandro Rubini  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
10751a087c6aSAlessandro Rubini  * code.
10761a087c6aSAlessandro Rubini  */
107788187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
10781a087c6aSAlessandro Rubini 				       struct dentry *parent,
10791a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
10801a087c6aSAlessandro Rubini {
10811a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
10821a087c6aSAlessandro Rubini }
10831a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
10843b85e4abSHeiko Carstens 
10853b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
108698210b7fSArend van Spriel 
108798210b7fSArend van Spriel struct debugfs_devm_entry {
108898210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
108998210b7fSArend van Spriel 	struct device *dev;
109098210b7fSArend van Spriel };
109198210b7fSArend van Spriel 
109298210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
109398210b7fSArend van Spriel {
109498210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
109598210b7fSArend van Spriel 
109698210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
109798210b7fSArend van Spriel }
109898210b7fSArend van Spriel 
109998210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
110098210b7fSArend van Spriel 	.owner = THIS_MODULE,
110198210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
110298210b7fSArend van Spriel 	.release = single_release,
110398210b7fSArend van Spriel 	.read = seq_read,
110498210b7fSArend van Spriel 	.llseek = seq_lseek
110598210b7fSArend van Spriel };
110698210b7fSArend van Spriel 
110798210b7fSArend van Spriel /**
110898210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
110998210b7fSArend van Spriel  *
111098210b7fSArend van Spriel  * @dev: device related to this debugfs file.
111198210b7fSArend van Spriel  * @name: name of the debugfs file.
111298210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
111398210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
111498210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
111598210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
111698210b7fSArend van Spriel  */
111798210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
111898210b7fSArend van Spriel 					   struct dentry *parent,
111998210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
112098210b7fSArend van Spriel 							  void *data))
112198210b7fSArend van Spriel {
112298210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
112398210b7fSArend van Spriel 
112498210b7fSArend van Spriel 	if (IS_ERR(parent))
112598210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
112698210b7fSArend van Spriel 
112798210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
112898210b7fSArend van Spriel 	if (!entry)
112998210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
113098210b7fSArend van Spriel 
113198210b7fSArend van Spriel 	entry->read = read_fn;
113298210b7fSArend van Spriel 	entry->dev = dev;
113398210b7fSArend van Spriel 
113498210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
113598210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
113698210b7fSArend van Spriel }
113798210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
113898210b7fSArend van Spriel 
1139