xref: /linux-6.15/fs/debugfs/file.c (revision 67510d7e)
13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
9e1b4fc7aSMauro Carvalho Chehab  *  See Documentation/filesystems/ for more details.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141a087c6aSAlessandro Rubini #include <linux/seq_file.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/debugfs.h>
1703e099fbSAlessandro Rubini #include <linux/io.h>
189fe2a701SSrivatsa Vaddagiri #include <linux/slab.h>
193a76e5e0SSeth Jennings #include <linux/atomic.h>
2098210b7fSArend van Spriel #include <linux/device.h>
2130332eeeSGeert Uytterhoeven #include <linux/pm_runtime.h>
22cfe39442SAl Viro #include <linux/poll.h>
235496197fSDavid Howells #include <linux/security.h>
249fd4dcecSNicolai Stange 
259fd4dcecSNicolai Stange #include "internal.h"
261da177e4SLinus Torvalds 
2749d200deSNicolai Stange struct poll_table_struct;
2849d200deSNicolai Stange 
291da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
301da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
361da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	return count;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
419fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = {
421da177e4SLinus Torvalds 	.read =		default_read_file,
431da177e4SLinus Torvalds 	.write =	default_write_file,
44234e3405SStephen Boyd 	.open =		simple_open,
456038f373SArnd Bergmann 	.llseek =	noop_llseek,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
489fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry)
499fd4dcecSNicolai Stange 
507c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp)
517c8d4698SNicolai Stange {
527c8d4698SNicolai Stange 	struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
53055ab8e3SNicolai Stange 
547d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
557d39bc50SNicolai Stange 		/*
567d39bc50SNicolai Stange 		 * Urgh, we've been called w/o a protecting
577d39bc50SNicolai Stange 		 * debugfs_file_get().
587d39bc50SNicolai Stange 		 */
597d39bc50SNicolai Stange 		WARN_ON(1);
607d39bc50SNicolai Stange 		return NULL;
617d39bc50SNicolai Stange 	}
627d39bc50SNicolai Stange 
637c8d4698SNicolai Stange 	return fsd->real_fops;
647c8d4698SNicolai Stange }
657c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops);
667c8d4698SNicolai Stange 
67f8f25893SJohannes Berg enum dbgfs_get_mode {
68f8f25893SJohannes Berg 	DBGFS_GET_ALREADY,
69f8f25893SJohannes Berg 	DBGFS_GET_REGULAR,
70f8f25893SJohannes Berg 	DBGFS_GET_SHORT,
71f8f25893SJohannes Berg };
72f8f25893SJohannes Berg 
73f8f25893SJohannes Berg static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode)
74e9117a5aSNicolai Stange {
757d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
767d39bc50SNicolai Stange 	void *d_fsd;
77e9117a5aSNicolai Stange 
780ed04a18SJohannes Berg 	/*
790ed04a18SJohannes Berg 	 * This could only happen if some debugfs user erroneously calls
800ed04a18SJohannes Berg 	 * debugfs_file_get() on a dentry that isn't even a file, let
810ed04a18SJohannes Berg 	 * them know about it.
820ed04a18SJohannes Berg 	 */
830ed04a18SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
840ed04a18SJohannes Berg 		return -EINVAL;
850ed04a18SJohannes Berg 
867d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
877d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
887d39bc50SNicolai Stange 		fsd = d_fsd;
897d39bc50SNicolai Stange 	} else {
90f8f25893SJohannes Berg 		if (WARN_ON(mode == DBGFS_GET_ALREADY))
91f8f25893SJohannes Berg 			return -EINVAL;
92f8f25893SJohannes Berg 
937d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
947d39bc50SNicolai Stange 		if (!fsd)
957d39bc50SNicolai Stange 			return -ENOMEM;
967d39bc50SNicolai Stange 
97f8f25893SJohannes Berg 		if (mode == DBGFS_GET_SHORT) {
988dc6d81cSJohannes Berg 			fsd->real_fops = NULL;
998dc6d81cSJohannes Berg 			fsd->short_fops = (void *)((unsigned long)d_fsd &
100f8f25893SJohannes Berg 						~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
1018dc6d81cSJohannes Berg 		} else {
1027d39bc50SNicolai Stange 			fsd->real_fops = (void *)((unsigned long)d_fsd &
1037d39bc50SNicolai Stange 						~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
1048dc6d81cSJohannes Berg 			fsd->short_fops = NULL;
1058dc6d81cSJohannes Berg 		}
1067d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
1077d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
108159f5bdaSJohannes Berg 		INIT_LIST_HEAD(&fsd->cancellations);
109159f5bdaSJohannes Berg 		mutex_init(&fsd->cancellations_mtx);
110159f5bdaSJohannes Berg 
1117d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
112159f5bdaSJohannes Berg 			mutex_destroy(&fsd->cancellations_mtx);
1137d39bc50SNicolai Stange 			kfree(fsd);
1147d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
1157d39bc50SNicolai Stange 		}
1167d39bc50SNicolai Stange 	}
1177d39bc50SNicolai Stange 
1187d39bc50SNicolai Stange 	/*
1197d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1207d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1217d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1227d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1237d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1247d39bc50SNicolai Stange 	 * removers.
1257d39bc50SNicolai Stange 	 */
126e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
127e9117a5aSNicolai Stange 		return -EIO;
128e9117a5aSNicolai Stange 
129e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
130e9117a5aSNicolai Stange 		return -EIO;
131e9117a5aSNicolai Stange 
132e9117a5aSNicolai Stange 	return 0;
133e9117a5aSNicolai Stange }
134f8f25893SJohannes Berg 
135f8f25893SJohannes Berg /**
136f8f25893SJohannes Berg  * debugfs_file_get - mark the beginning of file data access
137f8f25893SJohannes Berg  * @dentry: the dentry object whose data is being accessed.
138f8f25893SJohannes Berg  *
139f8f25893SJohannes Berg  * Up to a matching call to debugfs_file_put(), any successive call
140f8f25893SJohannes Berg  * into the file removing functions debugfs_remove() and
141f8f25893SJohannes Berg  * debugfs_remove_recursive() will block. Since associated private
142f8f25893SJohannes Berg  * file data may only get freed after a successful return of any of
143f8f25893SJohannes Berg  * the removal functions, you may safely access it after a successful
144f8f25893SJohannes Berg  * call to debugfs_file_get() without worrying about lifetime issues.
145f8f25893SJohannes Berg  *
146f8f25893SJohannes Berg  * If -%EIO is returned, the file has already been removed and thus,
147f8f25893SJohannes Berg  * it is not safe to access any of its data. If, on the other hand,
148f8f25893SJohannes Berg  * it is allowed to access the file data, zero is returned.
149f8f25893SJohannes Berg  */
150f8f25893SJohannes Berg int debugfs_file_get(struct dentry *dentry)
151f8f25893SJohannes Berg {
152f8f25893SJohannes Berg 	return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);
153f8f25893SJohannes Berg }
154e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
155e9117a5aSNicolai Stange 
156e9117a5aSNicolai Stange /**
157e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
158e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
159e9117a5aSNicolai Stange  *          debugfs_file_get().
160e9117a5aSNicolai Stange  *
161e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
162e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
163e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
164e9117a5aSNicolai Stange  */
165e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
166e9117a5aSNicolai Stange {
1677d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
168e9117a5aSNicolai Stange 
169e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
170e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
171e9117a5aSNicolai Stange }
172e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
173e9117a5aSNicolai Stange 
1748c88a474SJohannes Berg /**
1758c88a474SJohannes Berg  * debugfs_enter_cancellation - enter a debugfs cancellation
1768c88a474SJohannes Berg  * @file: the file being accessed
1778c88a474SJohannes Berg  * @cancellation: the cancellation object, the cancel callback
1788c88a474SJohannes Berg  *	inside of it must be initialized
1798c88a474SJohannes Berg  *
1808c88a474SJohannes Berg  * When a debugfs file is removed it needs to wait for all active
1818c88a474SJohannes Berg  * operations to complete. However, the operation itself may need
1828c88a474SJohannes Berg  * to wait for hardware or completion of some asynchronous process
1838c88a474SJohannes Berg  * or similar. As such, it may need to be cancelled to avoid long
1848c88a474SJohannes Berg  * waits or even deadlocks.
1858c88a474SJohannes Berg  *
1868c88a474SJohannes Berg  * This function can be used inside a debugfs handler that may
1878c88a474SJohannes Berg  * need to be cancelled. As soon as this function is called, the
1888c88a474SJohannes Berg  * cancellation's 'cancel' callback may be called, at which point
1898c88a474SJohannes Berg  * the caller should proceed to call debugfs_leave_cancellation()
1908c88a474SJohannes Berg  * and leave the debugfs handler function as soon as possible.
1918c88a474SJohannes Berg  * Note that the 'cancel' callback is only ever called in the
1928c88a474SJohannes Berg  * context of some kind of debugfs_remove().
1938c88a474SJohannes Berg  *
1948c88a474SJohannes Berg  * This function must be paired with debugfs_leave_cancellation().
1958c88a474SJohannes Berg  */
1968c88a474SJohannes Berg void debugfs_enter_cancellation(struct file *file,
1978c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
1988c88a474SJohannes Berg {
1998c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
2008c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
2018c88a474SJohannes Berg 
2028c88a474SJohannes Berg 	INIT_LIST_HEAD(&cancellation->list);
2038c88a474SJohannes Berg 
2048c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
2058c88a474SJohannes Berg 		return;
2068c88a474SJohannes Berg 
2078c88a474SJohannes Berg 	if (WARN_ON(!cancellation->cancel))
2088c88a474SJohannes Berg 		return;
2098c88a474SJohannes Berg 
2108c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
2118c88a474SJohannes Berg 	if (WARN_ON(!fsd ||
2128c88a474SJohannes Berg 		    ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
2138c88a474SJohannes Berg 		return;
2148c88a474SJohannes Berg 
2158c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2168c88a474SJohannes Berg 	list_add(&cancellation->list, &fsd->cancellations);
2178c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2188c88a474SJohannes Berg 
2198c88a474SJohannes Berg 	/* if we're already removing wake it up to cancel */
2208c88a474SJohannes Berg 	if (d_unlinked(dentry))
2218c88a474SJohannes Berg 		complete(&fsd->active_users_drained);
2228c88a474SJohannes Berg }
2238c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
2248c88a474SJohannes Berg 
2258c88a474SJohannes Berg /**
2268c88a474SJohannes Berg  * debugfs_leave_cancellation - leave cancellation section
2278c88a474SJohannes Berg  * @file: the file being accessed
2288c88a474SJohannes Berg  * @cancellation: the cancellation previously registered with
2298c88a474SJohannes Berg  *	debugfs_enter_cancellation()
2308c88a474SJohannes Berg  *
2318c88a474SJohannes Berg  * See the documentation of debugfs_enter_cancellation().
2328c88a474SJohannes Berg  */
2338c88a474SJohannes Berg void debugfs_leave_cancellation(struct file *file,
2348c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
2358c88a474SJohannes Berg {
2368c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
2378c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
2388c88a474SJohannes Berg 
2398c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
2408c88a474SJohannes Berg 		return;
2418c88a474SJohannes Berg 
2428c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
2438c88a474SJohannes Berg 	if (WARN_ON(!fsd ||
2448c88a474SJohannes Berg 		    ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
2458c88a474SJohannes Berg 		return;
2468c88a474SJohannes Berg 
2478c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2488c88a474SJohannes Berg 	if (!list_empty(&cancellation->list))
2498c88a474SJohannes Berg 		list_del(&cancellation->list);
2508c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2518c88a474SJohannes Berg }
2528c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
2538c88a474SJohannes Berg 
2545496197fSDavid Howells /*
2555496197fSDavid Howells  * Only permit access to world-readable files when the kernel is locked down.
2565496197fSDavid Howells  * We also need to exclude any file that has ways to write or alter it as root
2575496197fSDavid Howells  * can bypass the permissions check.
2585496197fSDavid Howells  */
259a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode,
2605496197fSDavid Howells 			       struct file *filp,
2615496197fSDavid Howells 			       const struct file_operations *real_fops)
2625496197fSDavid Howells {
263358fcf5dSMichal Suchanek 	if ((inode->i_mode & 07777 & ~0444) == 0 &&
2645496197fSDavid Howells 	    !(filp->f_mode & FMODE_WRITE) &&
2658dc6d81cSJohannes Berg 	    (!real_fops ||
2668dc6d81cSJohannes Berg 	     (!real_fops->unlocked_ioctl &&
2675496197fSDavid Howells 	      !real_fops->compat_ioctl &&
2688dc6d81cSJohannes Berg 	      !real_fops->mmap)))
269a37f4958SEric Snowberg 		return 0;
2705496197fSDavid Howells 
271a37f4958SEric Snowberg 	if (security_locked_down(LOCKDOWN_DEBUGFS))
272a37f4958SEric Snowberg 		return -EPERM;
273a37f4958SEric Snowberg 
274a37f4958SEric Snowberg 	return 0;
2755496197fSDavid Howells }
2765496197fSDavid Howells 
2779fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
2789fd4dcecSNicolai Stange {
27969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
2809fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
2817d39bc50SNicolai Stange 	int r;
2829fd4dcecSNicolai Stange 
283*67510d7eSJohannes Berg 	r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
2847d39bc50SNicolai Stange 	if (r)
2857d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
2869fd4dcecSNicolai Stange 
28786f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
2885496197fSDavid Howells 
289a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
2905496197fSDavid Howells 	if (r)
2915496197fSDavid Howells 		goto out;
2925496197fSDavid Howells 
293275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
294e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
295275678e7STaehee Yoo 		if (real_fops->owner &&
296112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
297112cedc8SSven Eckelmann 			r = -ENXIO;
298275678e7STaehee Yoo 			goto out;
299112cedc8SSven Eckelmann 		}
300275678e7STaehee Yoo #endif
301275678e7STaehee Yoo 
3029fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
3039fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
3049fd4dcecSNicolai Stange 			dentry);
3059fd4dcecSNicolai Stange 		r = -ENXIO;
3069fd4dcecSNicolai Stange 		goto out;
3079fd4dcecSNicolai Stange 	}
3089fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
3099fd4dcecSNicolai Stange 
3109fd4dcecSNicolai Stange 	if (real_fops->open)
3119fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
3129fd4dcecSNicolai Stange 
3139fd4dcecSNicolai Stange out:
31469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
3159fd4dcecSNicolai Stange 	return r;
3169fd4dcecSNicolai Stange }
3179fd4dcecSNicolai Stange 
3189fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
3199fd4dcecSNicolai Stange 	.open = open_proxy_open,
3209fd4dcecSNicolai Stange };
3219fd4dcecSNicolai Stange 
32249d200deSNicolai Stange #define PROTO(args...) args
32349d200deSNicolai Stange #define ARGS(args...) args
32449d200deSNicolai Stange 
32549d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
32649d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
32749d200deSNicolai Stange {									\
32869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
329154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
33049d200deSNicolai Stange 	ret_type r;							\
33149d200deSNicolai Stange 									\
33269d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
33369d29f9eSNicolai Stange 	if (unlikely(r))						\
33469d29f9eSNicolai Stange 		return r;						\
335154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
33649d200deSNicolai Stange 	r = real_fops->name(args);					\
33769d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
33849d200deSNicolai Stange 	return r;							\
33949d200deSNicolai Stange }
34049d200deSNicolai Stange 
3418dc6d81cSJohannes Berg #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args)		\
3428dc6d81cSJohannes Berg static ret_type full_proxy_ ## name(proto)				\
3438dc6d81cSJohannes Berg {									\
3448dc6d81cSJohannes Berg 	struct dentry *dentry = F_DENTRY(filp);				\
3458dc6d81cSJohannes Berg 	struct debugfs_fsdata *fsd;					\
3468dc6d81cSJohannes Berg 	ret_type r;							\
3478dc6d81cSJohannes Berg 									\
3488dc6d81cSJohannes Berg 	r = debugfs_file_get(dentry);					\
3498dc6d81cSJohannes Berg 	if (unlikely(r))						\
3508dc6d81cSJohannes Berg 		return r;						\
3518dc6d81cSJohannes Berg 	fsd = dentry->d_fsdata;						\
3528dc6d81cSJohannes Berg 	if (fsd->real_fops)						\
3538dc6d81cSJohannes Berg 		r = fsd->real_fops->name(args);				\
3548dc6d81cSJohannes Berg 	else								\
3558dc6d81cSJohannes Berg 		r = fsd->short_fops->name(args);			\
3568dc6d81cSJohannes Berg 	debugfs_file_put(dentry);					\
3578dc6d81cSJohannes Berg 	return r;							\
3588dc6d81cSJohannes Berg }
3598dc6d81cSJohannes Berg 
3608dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
36149d200deSNicolai Stange 		     PROTO(struct file *filp, loff_t offset, int whence),
36249d200deSNicolai Stange 		     ARGS(filp, offset, whence));
36349d200deSNicolai Stange 
3648dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
36549d200deSNicolai Stange 		     PROTO(struct file *filp, char __user *buf, size_t size,
36649d200deSNicolai Stange 			   loff_t *ppos),
36749d200deSNicolai Stange 		     ARGS(filp, buf, size, ppos));
36849d200deSNicolai Stange 
3698dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(write, ssize_t, filp,
3708dc6d81cSJohannes Berg 		     PROTO(struct file *filp, const char __user *buf,
3718dc6d81cSJohannes Berg 			   size_t size, loff_t *ppos),
37249d200deSNicolai Stange 		     ARGS(filp, buf, size, ppos));
37349d200deSNicolai Stange 
37449d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
37549d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
37649d200deSNicolai Stange 		ARGS(filp, cmd, arg));
37749d200deSNicolai Stange 
378076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
37949d200deSNicolai Stange 				struct poll_table_struct *wait)
38049d200deSNicolai Stange {
38169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
382e6c8adcaSAl Viro 	__poll_t r = 0;
383154b9d75SNicolai Stange 	const struct file_operations *real_fops;
38449d200deSNicolai Stange 
38569d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
386a9a08845SLinus Torvalds 		return EPOLLHUP;
38749d200deSNicolai Stange 
388154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
38949d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
39069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
39149d200deSNicolai Stange 	return r;
39249d200deSNicolai Stange }
39349d200deSNicolai Stange 
39449d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
39549d200deSNicolai Stange {
39649d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
39786f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
39849d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
39949d200deSNicolai Stange 	int r = 0;
40049d200deSNicolai Stange 
40149d200deSNicolai Stange 	/*
40249d200deSNicolai Stange 	 * We must not protect this against removal races here: the
40349d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
40449d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
40549d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
40649d200deSNicolai Stange 	 */
4078dc6d81cSJohannes Berg 	if (real_fops && real_fops->release)
40849d200deSNicolai Stange 		r = real_fops->release(inode, filp);
40949d200deSNicolai Stange 
41049d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
411c80a67bdSXu Wang 	kfree(proxy_fops);
41249d200deSNicolai Stange 	fops_put(real_fops);
413a1a9e5d2SEric Engestrom 	return r;
41449d200deSNicolai Stange }
41549d200deSNicolai Stange 
41649d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
4178dc6d81cSJohannes Berg 				   struct debugfs_fsdata *fsd)
41849d200deSNicolai Stange {
41949d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
4208dc6d81cSJohannes Berg 
4218dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->llseek) ||
4228dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->llseek))
42349d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
4248dc6d81cSJohannes Berg 
4258dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->read) ||
4268dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->read))
42749d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
4288dc6d81cSJohannes Berg 
4298dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->write) ||
4308dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->write))
43149d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
4328dc6d81cSJohannes Berg 
4338dc6d81cSJohannes Berg 	if (fsd->real_fops && fsd->real_fops->poll)
43449d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
4358dc6d81cSJohannes Berg 
4368dc6d81cSJohannes Berg 	if (fsd->real_fops && fsd->real_fops->unlocked_ioctl)
43749d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
43849d200deSNicolai Stange }
43949d200deSNicolai Stange 
440f8f25893SJohannes Berg static int full_proxy_open(struct inode *inode, struct file *filp,
441f8f25893SJohannes Berg 			   enum dbgfs_get_mode mode)
44249d200deSNicolai Stange {
44369d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
4448dc6d81cSJohannes Berg 	const struct file_operations *real_fops;
44549d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
4468dc6d81cSJohannes Berg 	struct debugfs_fsdata *fsd;
4477d39bc50SNicolai Stange 	int r;
44849d200deSNicolai Stange 
449f8f25893SJohannes Berg 	r = __debugfs_file_get(dentry, mode);
4507d39bc50SNicolai Stange 	if (r)
4517d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
45249d200deSNicolai Stange 
4538dc6d81cSJohannes Berg 	fsd = dentry->d_fsdata;
4548dc6d81cSJohannes Berg 	real_fops = fsd->real_fops;
455a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
4565496197fSDavid Howells 	if (r)
4575496197fSDavid Howells 		goto out;
4585496197fSDavid Howells 
4598dc6d81cSJohannes Berg 	if (real_fops && !fops_get(real_fops)) {
460e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
461275678e7STaehee Yoo 		if (real_fops->owner &&
462112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
463112cedc8SSven Eckelmann 			r = -ENXIO;
464275678e7STaehee Yoo 			goto out;
465112cedc8SSven Eckelmann 		}
466275678e7STaehee Yoo #endif
467275678e7STaehee Yoo 
46849d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
46949d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
47049d200deSNicolai Stange 			dentry);
47149d200deSNicolai Stange 		r = -ENXIO;
47249d200deSNicolai Stange 		goto out;
47349d200deSNicolai Stange 	}
47449d200deSNicolai Stange 
47549d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
47649d200deSNicolai Stange 	if (!proxy_fops) {
47749d200deSNicolai Stange 		r = -ENOMEM;
47849d200deSNicolai Stange 		goto free_proxy;
47949d200deSNicolai Stange 	}
4808dc6d81cSJohannes Berg 	__full_proxy_fops_init(proxy_fops, fsd);
48149d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
48249d200deSNicolai Stange 
4838dc6d81cSJohannes Berg 	if (!real_fops || real_fops->open) {
4848dc6d81cSJohannes Berg 		if (real_fops)
48549d200deSNicolai Stange 			r = real_fops->open(inode, filp);
4868dc6d81cSJohannes Berg 		else
4878dc6d81cSJohannes Berg 			r = simple_open(inode, filp);
488b10e3e90SNicolai Stange 		if (r) {
489b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
490b10e3e90SNicolai Stange 			goto free_proxy;
491b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
49249d200deSNicolai Stange 			/* No protection against file removal anymore. */
49349d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
49449d200deSNicolai Stange 				dentry);
49549d200deSNicolai Stange 			goto free_proxy;
49649d200deSNicolai Stange 		}
49749d200deSNicolai Stange 	}
49849d200deSNicolai Stange 
49949d200deSNicolai Stange 	goto out;
50049d200deSNicolai Stange free_proxy:
50149d200deSNicolai Stange 	kfree(proxy_fops);
50249d200deSNicolai Stange 	fops_put(real_fops);
50349d200deSNicolai Stange out:
50469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
50549d200deSNicolai Stange 	return r;
50649d200deSNicolai Stange }
50749d200deSNicolai Stange 
508f8f25893SJohannes Berg static int full_proxy_open_regular(struct inode *inode, struct file *filp)
509f8f25893SJohannes Berg {
510f8f25893SJohannes Berg 	return full_proxy_open(inode, filp, DBGFS_GET_REGULAR);
511f8f25893SJohannes Berg }
512f8f25893SJohannes Berg 
51349d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
514f8f25893SJohannes Berg 	.open = full_proxy_open_regular,
515f8f25893SJohannes Berg };
516f8f25893SJohannes Berg 
517f8f25893SJohannes Berg static int full_proxy_open_short(struct inode *inode, struct file *filp)
518f8f25893SJohannes Berg {
519f8f25893SJohannes Berg 	return full_proxy_open(inode, filp, DBGFS_GET_SHORT);
520f8f25893SJohannes Berg }
521f8f25893SJohannes Berg 
522f8f25893SJohannes Berg const struct file_operations debugfs_full_short_proxy_file_operations = {
523f8f25893SJohannes Berg 	.open = full_proxy_open_short,
52449d200deSNicolai Stange };
52549d200deSNicolai Stange 
526c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
527c6468808SNicolai Stange 			size_t len, loff_t *ppos)
528c6468808SNicolai Stange {
52969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
530c6468808SNicolai Stange 	ssize_t ret;
531c6468808SNicolai Stange 
53269d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
53369d29f9eSNicolai Stange 	if (unlikely(ret))
53469d29f9eSNicolai Stange 		return ret;
535c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
53669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
537c6468808SNicolai Stange 	return ret;
538c6468808SNicolai Stange }
539c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
540c6468808SNicolai Stange 
541d472cf79SAkinobu Mita static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
542d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos, bool is_signed)
543c6468808SNicolai Stange {
54469d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
545c6468808SNicolai Stange 	ssize_t ret;
546c6468808SNicolai Stange 
54769d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
54869d29f9eSNicolai Stange 	if (unlikely(ret))
54969d29f9eSNicolai Stange 		return ret;
550d472cf79SAkinobu Mita 	if (is_signed)
551d472cf79SAkinobu Mita 		ret = simple_attr_write_signed(file, buf, len, ppos);
552d472cf79SAkinobu Mita 	else
553c6468808SNicolai Stange 		ret = simple_attr_write(file, buf, len, ppos);
55469d29f9eSNicolai Stange 	debugfs_file_put(dentry);
555c6468808SNicolai Stange 	return ret;
556c6468808SNicolai Stange }
557d472cf79SAkinobu Mita 
558d472cf79SAkinobu Mita ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
559d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
560d472cf79SAkinobu Mita {
561d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
562d472cf79SAkinobu Mita }
563c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
564c6468808SNicolai Stange 
565d472cf79SAkinobu Mita ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
566d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
567d472cf79SAkinobu Mita {
568d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
569d472cf79SAkinobu Mita }
570d472cf79SAkinobu Mita EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
571d472cf79SAkinobu Mita 
5724909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
5734909f168SNicolai Stange 					struct dentry *parent, void *value,
5744909f168SNicolai Stange 					const struct file_operations *fops,
5754909f168SNicolai Stange 					const struct file_operations *fops_ro,
5764909f168SNicolai Stange 					const struct file_operations *fops_wo)
5774909f168SNicolai Stange {
5784909f168SNicolai Stange 	/* if there are no write bits set, make read only */
5794909f168SNicolai Stange 	if (!(mode & S_IWUGO))
5804909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5814909f168SNicolai Stange 						fops_ro);
5824909f168SNicolai Stange 	/* if there are no read bits set, make write only */
5834909f168SNicolai Stange 	if (!(mode & S_IRUGO))
5844909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5854909f168SNicolai Stange 						fops_wo);
5864909f168SNicolai Stange 
5874909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
5884909f168SNicolai Stange }
5894909f168SNicolai Stange 
5908b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
591acaefc25SArnd Bergmann {
592acaefc25SArnd Bergmann 	*(u8 *)data = val;
5938b88b099SChristoph Hellwig 	return 0;
594acaefc25SArnd Bergmann }
5958b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
596acaefc25SArnd Bergmann {
5978b88b099SChristoph Hellwig 	*val = *(u8 *)data;
5988b88b099SChristoph Hellwig 	return 0;
599acaefc25SArnd Bergmann }
6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
6014909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
6024909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds /**
6056468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
6061da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6071da177e4SLinus Torvalds  * @mode: the permission that the file should have
6081da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6096468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6101da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6111da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6121da177e4SLinus Torvalds  *         from.
6131da177e4SLinus Torvalds  *
6141da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6151da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6161da177e4SLinus Torvalds  * set, it can be read from, and written to.
6171da177e4SLinus Torvalds  */
6189655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
6199655ac4aSGreg Kroah-Hartman 		       u8 *value)
6201da177e4SLinus Torvalds {
6219655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
622b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
6251da177e4SLinus Torvalds 
6268b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
627acaefc25SArnd Bergmann {
628acaefc25SArnd Bergmann 	*(u16 *)data = val;
6298b88b099SChristoph Hellwig 	return 0;
630acaefc25SArnd Bergmann }
6318b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
632acaefc25SArnd Bergmann {
6338b88b099SChristoph Hellwig 	*val = *(u16 *)data;
6348b88b099SChristoph Hellwig 	return 0;
635acaefc25SArnd Bergmann }
6364909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
6374909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
6384909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
639acaefc25SArnd Bergmann 
6401da177e4SLinus Torvalds /**
6416468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
6421da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6431da177e4SLinus Torvalds  * @mode: the permission that the file should have
6441da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6456468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6461da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6471da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6481da177e4SLinus Torvalds  *         from.
6491da177e4SLinus Torvalds  *
6501da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6511da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6521da177e4SLinus Torvalds  * set, it can be read from, and written to.
6531da177e4SLinus Torvalds  */
654313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
655313f5dbbSGreg Kroah-Hartman 			u16 *value)
6561da177e4SLinus Torvalds {
657313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
658b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
6611da177e4SLinus Torvalds 
6628b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
663acaefc25SArnd Bergmann {
664acaefc25SArnd Bergmann 	*(u32 *)data = val;
6658b88b099SChristoph Hellwig 	return 0;
666acaefc25SArnd Bergmann }
6678b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
668acaefc25SArnd Bergmann {
6698b88b099SChristoph Hellwig 	*val = *(u32 *)data;
6708b88b099SChristoph Hellwig 	return 0;
671acaefc25SArnd Bergmann }
6724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
6734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
6744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
675acaefc25SArnd Bergmann 
6761da177e4SLinus Torvalds /**
6776468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
6781da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6791da177e4SLinus Torvalds  * @mode: the permission that the file should have
6801da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6816468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6821da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6831da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6841da177e4SLinus Torvalds  *         from.
6851da177e4SLinus Torvalds  *
6861da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6871da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6881da177e4SLinus Torvalds  * set, it can be read from, and written to.
6891da177e4SLinus Torvalds  */
6902b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
6912b07021aSGreg Kroah-Hartman 			u32 *value)
6921da177e4SLinus Torvalds {
6932b07021aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
694b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
6971da177e4SLinus Torvalds 
6988b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
6998447891fSMichael Ellerman {
7008447891fSMichael Ellerman 	*(u64 *)data = val;
7018b88b099SChristoph Hellwig 	return 0;
7028447891fSMichael Ellerman }
7038447891fSMichael Ellerman 
7048b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
7058447891fSMichael Ellerman {
7068b88b099SChristoph Hellwig 	*val = *(u64 *)data;
7078b88b099SChristoph Hellwig 	return 0;
7088447891fSMichael Ellerman }
7094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
7104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
7114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
7128447891fSMichael Ellerman 
7138447891fSMichael Ellerman /**
7148447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
7158447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
7168447891fSMichael Ellerman  * @mode: the permission that the file should have
7178447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
7188447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
7198447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
7208447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
7218447891fSMichael Ellerman  *         from.
7228447891fSMichael Ellerman  *
7238447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
7248447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
7258447891fSMichael Ellerman  * set, it can be read from, and written to.
7268447891fSMichael Ellerman  */
727ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
728ad26221fSGreg Kroah-Hartman 			u64 *value)
7298447891fSMichael Ellerman {
730ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
731b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
7328447891fSMichael Ellerman }
7338447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
7348447891fSMichael Ellerman 
735c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
736c23fe831SViresh Kumar {
737c23fe831SViresh Kumar 	*(unsigned long *)data = val;
738c23fe831SViresh Kumar 	return 0;
739c23fe831SViresh Kumar }
740c23fe831SViresh Kumar 
741c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
742c23fe831SViresh Kumar {
743c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
744c23fe831SViresh Kumar 	return 0;
745c23fe831SViresh Kumar }
7464909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
7474909f168SNicolai Stange 			"%llu\n");
7484909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
7494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
750c23fe831SViresh Kumar 
751c23fe831SViresh Kumar /**
752c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
753c23fe831SViresh Kumar  * an unsigned long value.
754c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
755c23fe831SViresh Kumar  * @mode: the permission that the file should have
756c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
757c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
758c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
759c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
760c23fe831SViresh Kumar  *         from.
761c23fe831SViresh Kumar  *
762c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
763c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
764c23fe831SViresh Kumar  * set, it can be read from, and written to.
765c23fe831SViresh Kumar  */
766fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
767fb05b14cSGreg Kroah-Hartman 			  unsigned long *value)
768c23fe831SViresh Kumar {
769fb05b14cSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
770fb05b14cSGreg Kroah-Hartman 				   &fops_ulong_ro, &fops_ulong_wo);
771c23fe831SViresh Kumar }
772c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
773c23fe831SViresh Kumar 
7744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
7754909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
7764909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
7772ebefc50SRobin Getz 
7784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
7794909f168SNicolai Stange 			"0x%04llx\n");
7804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
7814909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
7822ebefc50SRobin Getz 
7834909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
7844909f168SNicolai Stange 			"0x%08llx\n");
7854909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
7864909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
7872ebefc50SRobin Getz 
7884909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
7894909f168SNicolai Stange 			"0x%016llx\n");
7904909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
7914909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
79215b0beaaSHuang Ying 
793e6716b87SRandy Dunlap /*
79415b0beaaSHuang 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
795e6716b87SRandy Dunlap  *
796e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
797e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
798e6716b87SRandy Dunlap  * decimal functions.
799e6716b87SRandy Dunlap  */
800e6716b87SRandy Dunlap 
8012ebefc50SRobin Getz /**
8022ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
803e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
804e6716b87SRandy Dunlap  * @mode: the permission that the file should have
805e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
806e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
807e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
808e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
809e6716b87SRandy Dunlap  *         from.
8102ebefc50SRobin Getz  */
811c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
812c7c11689SGreg Kroah-Hartman 		       u8 *value)
8132ebefc50SRobin Getz {
814c7c11689SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
815b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
8162ebefc50SRobin Getz }
8172ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
8182ebefc50SRobin Getz 
819e6716b87SRandy Dunlap /**
820e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
821e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
822e6716b87SRandy Dunlap  * @mode: the permission that the file should have
823e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
824e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
825e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
826e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
827e6716b87SRandy Dunlap  *         from.
828e6716b87SRandy Dunlap  */
829e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
830e40d38f2SGreg Kroah-Hartman 			u16 *value)
8312ebefc50SRobin Getz {
832e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
833b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
8342ebefc50SRobin Getz }
8352ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
8362ebefc50SRobin Getz 
837e6716b87SRandy Dunlap /**
838e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
839e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
840e6716b87SRandy Dunlap  * @mode: the permission that the file should have
841e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
842e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
843e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
844e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
845e6716b87SRandy Dunlap  *         from.
846e6716b87SRandy Dunlap  */
847f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
848f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
8492ebefc50SRobin Getz {
850f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
851b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
8522ebefc50SRobin Getz }
8532ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
8542ebefc50SRobin Getz 
85515b0beaaSHuang Ying /**
85615b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
85715b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
85815b0beaaSHuang Ying  * @mode: the permission that the file should have
85915b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
86015b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
86115b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
86215b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
86315b0beaaSHuang Ying  *         from.
86415b0beaaSHuang Ying  */
8650864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
8660864c408SGreg Kroah-Hartman 			u64 *value)
86715b0beaaSHuang Ying {
8680864c408SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
86982b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
87015b0beaaSHuang Ying }
87115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
87215b0beaaSHuang Ying 
8735e078787SInaky Perez-Gonzalez 
8745e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
8755e078787SInaky Perez-Gonzalez {
8765e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
8775e078787SInaky Perez-Gonzalez 	return 0;
8785e078787SInaky Perez-Gonzalez }
8795e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
8805e078787SInaky Perez-Gonzalez {
8815e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
8825e078787SInaky Perez-Gonzalez 	return 0;
8835e078787SInaky Perez-Gonzalez }
8844909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
8855e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
8864909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
8874909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
8885e078787SInaky Perez-Gonzalez 
8895e078787SInaky Perez-Gonzalez /**
8905e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
8915e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
8925e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
8935e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
8945e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
8955e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
8965e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
8975e078787SInaky Perez-Gonzalez  *         from.
8985e078787SInaky Perez-Gonzalez  */
8998e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
9005e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
9015e078787SInaky Perez-Gonzalez {
9028e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
9038e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
9045e078787SInaky Perez-Gonzalez }
9055e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
9065e078787SInaky Perez-Gonzalez 
9073a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
9083a76e5e0SSeth Jennings {
9093a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
9103a76e5e0SSeth Jennings 	return 0;
9113a76e5e0SSeth Jennings }
9123a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
9133a76e5e0SSeth Jennings {
9143a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
9153a76e5e0SSeth Jennings 	return 0;
9163a76e5e0SSeth Jennings }
917d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
9183a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
919d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
9204909f168SNicolai Stange 			"%lld\n");
921d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
9224909f168SNicolai Stange 			"%lld\n");
9233a76e5e0SSeth Jennings 
9243a76e5e0SSeth Jennings /**
9253a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
9263a76e5e0SSeth Jennings  * write an atomic_t value
9273a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
9283a76e5e0SSeth Jennings  * @mode: the permission that the file should have
9293a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
9303a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
9313a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
9323a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
9333a76e5e0SSeth Jennings  *         from.
9343a76e5e0SSeth Jennings  */
9359927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode,
9363a76e5e0SSeth Jennings 			     struct dentry *parent, atomic_t *value)
9373a76e5e0SSeth Jennings {
9389927c6faSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
9399927c6faSGreg Kroah-Hartman 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
9403a76e5e0SSeth Jennings }
9413a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
9425e078787SInaky Perez-Gonzalez 
9430642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
9441da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
9451da177e4SLinus Torvalds {
946c8a9c285SRasmus Villemoes 	char buf[2];
9474d45f797SNicolai Stange 	bool val;
94869d29f9eSNicolai Stange 	int r;
94969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9501da177e4SLinus Torvalds 
95169d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
95269d29f9eSNicolai Stange 	if (unlikely(r))
9534d45f797SNicolai Stange 		return r;
95469d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
95569d29f9eSNicolai Stange 	debugfs_file_put(dentry);
9564d45f797SNicolai Stange 
9574d45f797SNicolai Stange 	if (val)
9581da177e4SLinus Torvalds 		buf[0] = 'Y';
9591da177e4SLinus Torvalds 	else
9601da177e4SLinus Torvalds 		buf[0] = 'N';
9611da177e4SLinus Torvalds 	buf[1] = '\n';
9621da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
9631da177e4SLinus Torvalds }
9640642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
9651da177e4SLinus Torvalds 
9660642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
9671da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9681da177e4SLinus Torvalds {
9698705b48eSJonathan Cameron 	bool bv;
97069d29f9eSNicolai Stange 	int r;
971621a5f7aSViresh Kumar 	bool *val = file->private_data;
97269d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9731da177e4SLinus Torvalds 
974964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
975964f8363SAndy Shevchenko 	if (!r) {
97669d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
97769d29f9eSNicolai Stange 		if (unlikely(r))
9784d45f797SNicolai Stange 			return r;
97969d29f9eSNicolai Stange 		*val = bv;
98069d29f9eSNicolai Stange 		debugfs_file_put(dentry);
9814d45f797SNicolai Stange 	}
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	return count;
9841da177e4SLinus Torvalds }
9850642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
9861da177e4SLinus Torvalds 
9874b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
9880642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
9890642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
990234e3405SStephen Boyd 	.open =		simple_open,
9916038f373SArnd Bergmann 	.llseek =	default_llseek,
9921da177e4SLinus Torvalds };
9931da177e4SLinus Torvalds 
9946713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
9956713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
9966713e8fbSStephen Boyd 	.open =		simple_open,
9976713e8fbSStephen Boyd 	.llseek =	default_llseek,
9986713e8fbSStephen Boyd };
9996713e8fbSStephen Boyd 
10006713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
10016713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
10026713e8fbSStephen Boyd 	.open =		simple_open,
10036713e8fbSStephen Boyd 	.llseek =	default_llseek,
10046713e8fbSStephen Boyd };
10056713e8fbSStephen Boyd 
10061da177e4SLinus Torvalds /**
10076468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
10081da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
10091da177e4SLinus Torvalds  * @mode: the permission that the file should have
10101da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
10116468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
10121da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
10131da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
10141da177e4SLinus Torvalds  *         from.
10151da177e4SLinus Torvalds  *
10161da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
10171da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
10181da177e4SLinus Torvalds  * set, it can be read from, and written to.
10191da177e4SLinus Torvalds  */
1020393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
1021393b0638SGreg Kroah-Hartman 			 bool *value)
10221da177e4SLinus Torvalds {
1023393b0638SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
10246713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
10271da177e4SLinus Torvalds 
10289af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
10299af0440eSPeter Zijlstra 			      size_t count, loff_t *ppos)
10309af0440eSPeter Zijlstra {
10319af0440eSPeter Zijlstra 	struct dentry *dentry = F_DENTRY(file);
10329af0440eSPeter Zijlstra 	char *str, *copy = NULL;
10339af0440eSPeter Zijlstra 	int copy_len, len;
10349af0440eSPeter Zijlstra 	ssize_t ret;
10359af0440eSPeter Zijlstra 
10369af0440eSPeter Zijlstra 	ret = debugfs_file_get(dentry);
10379af0440eSPeter Zijlstra 	if (unlikely(ret))
10389af0440eSPeter Zijlstra 		return ret;
10399af0440eSPeter Zijlstra 
10409af0440eSPeter Zijlstra 	str = *(char **)file->private_data;
10419af0440eSPeter Zijlstra 	len = strlen(str) + 1;
10429af0440eSPeter Zijlstra 	copy = kmalloc(len, GFP_KERNEL);
10439af0440eSPeter Zijlstra 	if (!copy) {
10449af0440eSPeter Zijlstra 		debugfs_file_put(dentry);
10459af0440eSPeter Zijlstra 		return -ENOMEM;
10469af0440eSPeter Zijlstra 	}
10479af0440eSPeter Zijlstra 
10489af0440eSPeter Zijlstra 	copy_len = strscpy(copy, str, len);
10499af0440eSPeter Zijlstra 	debugfs_file_put(dentry);
10509af0440eSPeter Zijlstra 	if (copy_len < 0) {
10519af0440eSPeter Zijlstra 		kfree(copy);
10529af0440eSPeter Zijlstra 		return copy_len;
10539af0440eSPeter Zijlstra 	}
10549af0440eSPeter Zijlstra 
10559af0440eSPeter Zijlstra 	copy[copy_len] = '\n';
10569af0440eSPeter Zijlstra 
1057f501b6a2SDietmar Eggemann 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
10589af0440eSPeter Zijlstra 	kfree(copy);
10599af0440eSPeter Zijlstra 
10609af0440eSPeter Zijlstra 	return ret;
10619af0440eSPeter Zijlstra }
1062d60b59b9SCristian Marussi EXPORT_SYMBOL_GPL(debugfs_create_str);
10639af0440eSPeter Zijlstra 
10649af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
10659af0440eSPeter Zijlstra 				      size_t count, loff_t *ppos)
10669af0440eSPeter Zijlstra {
106786b54881SMike Tipton 	struct dentry *dentry = F_DENTRY(file);
106886b54881SMike Tipton 	char *old, *new = NULL;
106986b54881SMike Tipton 	int pos = *ppos;
107086b54881SMike Tipton 	int r;
107186b54881SMike Tipton 
107286b54881SMike Tipton 	r = debugfs_file_get(dentry);
107386b54881SMike Tipton 	if (unlikely(r))
107486b54881SMike Tipton 		return r;
107586b54881SMike Tipton 
107686b54881SMike Tipton 	old = *(char **)file->private_data;
107786b54881SMike Tipton 
107886b54881SMike Tipton 	/* only allow strict concatenation */
107986b54881SMike Tipton 	r = -EINVAL;
108086b54881SMike Tipton 	if (pos && pos != strlen(old))
108186b54881SMike Tipton 		goto error;
108286b54881SMike Tipton 
108386b54881SMike Tipton 	r = -E2BIG;
108486b54881SMike Tipton 	if (pos + count + 1 > PAGE_SIZE)
108586b54881SMike Tipton 		goto error;
108686b54881SMike Tipton 
108786b54881SMike Tipton 	r = -ENOMEM;
108886b54881SMike Tipton 	new = kmalloc(pos + count + 1, GFP_KERNEL);
108986b54881SMike Tipton 	if (!new)
109086b54881SMike Tipton 		goto error;
109186b54881SMike Tipton 
109286b54881SMike Tipton 	if (pos)
109386b54881SMike Tipton 		memcpy(new, old, pos);
109486b54881SMike Tipton 
109586b54881SMike Tipton 	r = -EFAULT;
109686b54881SMike Tipton 	if (copy_from_user(new + pos, user_buf, count))
109786b54881SMike Tipton 		goto error;
109886b54881SMike Tipton 
109986b54881SMike Tipton 	new[pos + count] = '\0';
110086b54881SMike Tipton 	strim(new);
110186b54881SMike Tipton 
11027360a48bSMike Tipton 	rcu_assign_pointer(*(char __rcu **)file->private_data, new);
110386b54881SMike Tipton 	synchronize_rcu();
110486b54881SMike Tipton 	kfree(old);
110586b54881SMike Tipton 
110686b54881SMike Tipton 	debugfs_file_put(dentry);
110786b54881SMike Tipton 	return count;
110886b54881SMike Tipton 
110986b54881SMike Tipton error:
111086b54881SMike Tipton 	kfree(new);
111186b54881SMike Tipton 	debugfs_file_put(dentry);
111286b54881SMike Tipton 	return r;
11139af0440eSPeter Zijlstra }
11149af0440eSPeter Zijlstra 
11159af0440eSPeter Zijlstra static const struct file_operations fops_str = {
11169af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
11179af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
11189af0440eSPeter Zijlstra 	.open =		simple_open,
11199af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11209af0440eSPeter Zijlstra };
11219af0440eSPeter Zijlstra 
11229af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = {
11239af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
11249af0440eSPeter Zijlstra 	.open =		simple_open,
11259af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11269af0440eSPeter Zijlstra };
11279af0440eSPeter Zijlstra 
11289af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = {
11299af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
11309af0440eSPeter Zijlstra 	.open =		simple_open,
11319af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11329af0440eSPeter Zijlstra };
11339af0440eSPeter Zijlstra 
11349af0440eSPeter Zijlstra /**
11359af0440eSPeter Zijlstra  * debugfs_create_str - create a debugfs file that is used to read and write a string value
11369af0440eSPeter Zijlstra  * @name: a pointer to a string containing the name of the file to create.
11379af0440eSPeter Zijlstra  * @mode: the permission that the file should have
11389af0440eSPeter Zijlstra  * @parent: a pointer to the parent dentry for this file.  This should be a
11399af0440eSPeter Zijlstra  *          directory dentry if set.  If this parameter is %NULL, then the
11409af0440eSPeter Zijlstra  *          file will be created in the root of the debugfs filesystem.
11419af0440eSPeter Zijlstra  * @value: a pointer to the variable that the file should read to and write
11429af0440eSPeter Zijlstra  *         from.
11439af0440eSPeter Zijlstra  *
11449af0440eSPeter Zijlstra  * This function creates a file in debugfs with the given name that
11459af0440eSPeter Zijlstra  * contains the value of the variable @value.  If the @mode variable is so
11469af0440eSPeter Zijlstra  * set, it can be read from, and written to.
11479af0440eSPeter Zijlstra  */
11489af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode,
11499af0440eSPeter Zijlstra 			struct dentry *parent, char **value)
11509af0440eSPeter Zijlstra {
11519af0440eSPeter Zijlstra 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
11529af0440eSPeter Zijlstra 				   &fops_str_ro, &fops_str_wo);
11539af0440eSPeter Zijlstra }
11549af0440eSPeter Zijlstra 
1155dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1156dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
1157dd308bc3SMichael Ellerman {
1158dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
115969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
116083b711cbSNicolai Stange 	ssize_t r;
116183b711cbSNicolai Stange 
116269d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
116369d29f9eSNicolai Stange 	if (unlikely(r))
116469d29f9eSNicolai Stange 		return r;
116583b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1166dd308bc3SMichael Ellerman 				blob->size);
116769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
116883b711cbSNicolai Stange 	return r;
1169dd308bc3SMichael Ellerman }
1170dd308bc3SMichael Ellerman 
117171cd3c63SAvadhut Naik static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
117271cd3c63SAvadhut Naik 			       size_t count, loff_t *ppos)
117371cd3c63SAvadhut Naik {
117471cd3c63SAvadhut Naik 	struct debugfs_blob_wrapper *blob = file->private_data;
117571cd3c63SAvadhut Naik 	struct dentry *dentry = F_DENTRY(file);
117671cd3c63SAvadhut Naik 	ssize_t r;
117771cd3c63SAvadhut Naik 
117871cd3c63SAvadhut Naik 	r = debugfs_file_get(dentry);
117971cd3c63SAvadhut Naik 	if (unlikely(r))
118071cd3c63SAvadhut Naik 		return r;
118171cd3c63SAvadhut Naik 	r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
118271cd3c63SAvadhut Naik 				   count);
118371cd3c63SAvadhut Naik 
118471cd3c63SAvadhut Naik 	debugfs_file_put(dentry);
118571cd3c63SAvadhut Naik 	return r;
118671cd3c63SAvadhut Naik }
118771cd3c63SAvadhut Naik 
118800977a59SArjan van de Ven static const struct file_operations fops_blob = {
1189dd308bc3SMichael Ellerman 	.read =		read_file_blob,
119071cd3c63SAvadhut Naik 	.write =	write_file_blob,
1191234e3405SStephen Boyd 	.open =		simple_open,
11926038f373SArnd Bergmann 	.llseek =	default_llseek,
1193dd308bc3SMichael Ellerman };
1194dd308bc3SMichael Ellerman 
1195dd308bc3SMichael Ellerman /**
119671cd3c63SAvadhut Naik  * debugfs_create_blob - create a debugfs file that is used to read and write
119771cd3c63SAvadhut Naik  * a binary blob
1198dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
119971cd3c63SAvadhut Naik  * @mode: the permission that the file should have
1200dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
12016468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1202dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
1203dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1204dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
1205dd308bc3SMichael Ellerman  *
1206dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
1207dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
120871cd3c63SAvadhut Naik  * read from and written to.
1209dd308bc3SMichael Ellerman  *
1210dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
1211dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
1212dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
1213adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
12149abb2499SRonald Tschalär  * returned.
1215dd308bc3SMichael Ellerman  *
1216adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
12179abb2499SRonald Tschalär  * be returned.
1218dd308bc3SMichael Ellerman  */
1219f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1220dd308bc3SMichael Ellerman 				   struct dentry *parent,
1221dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
1222dd308bc3SMichael Ellerman {
122371cd3c63SAvadhut Naik 	return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
1224dd308bc3SMichael Ellerman }
1225dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
12261a087c6aSAlessandro Rubini 
1227e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
1228e05e279eSLinus Torvalds 			       u32 *array, int array_size)
12299fe2a701SSrivatsa Vaddagiri {
12309fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
12319fe2a701SSrivatsa Vaddagiri 
1232e05e279eSLinus Torvalds 	while (--array_size >= 0) {
12339fe2a701SSrivatsa Vaddagiri 		size_t len;
1234e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
12359fe2a701SSrivatsa Vaddagiri 
1236e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
12379fe2a701SSrivatsa Vaddagiri 		ret += len;
12389fe2a701SSrivatsa Vaddagiri 
12399fe2a701SSrivatsa Vaddagiri 		buf += len;
12409fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
12419fe2a701SSrivatsa Vaddagiri 	}
12429fe2a701SSrivatsa Vaddagiri 	return ret;
12439fe2a701SSrivatsa Vaddagiri }
12449fe2a701SSrivatsa Vaddagiri 
124536048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
12469fe2a701SSrivatsa Vaddagiri {
1247a2b992c8SJakub Kicinski 	struct debugfs_u32_array *data = inode->i_private;
1248a2b992c8SJakub Kicinski 	int size, elements = data->n_elements;
1249e05e279eSLinus Torvalds 	char *buf;
12509fe2a701SSrivatsa Vaddagiri 
1251e05e279eSLinus Torvalds 	/*
1252e05e279eSLinus Torvalds 	 * Max size:
1253e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1254e05e279eSLinus Torvalds 	 *  - terminating NUL character
1255e05e279eSLinus Torvalds 	 */
1256e05e279eSLinus Torvalds 	size = elements*11;
1257e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
1258e05e279eSLinus Torvalds 	if (!buf)
125936048853SDavid Rientjes 		return -ENOMEM;
1260e05e279eSLinus Torvalds 	buf[size] = 0;
1261e05e279eSLinus Torvalds 
1262e05e279eSLinus Torvalds 	file->private_data = buf;
1263a2b992c8SJakub Kicinski 	u32_format_array(buf, size, data->array, data->n_elements);
1264e05e279eSLinus Torvalds 
126536048853SDavid Rientjes 	return nonseekable_open(inode, file);
12669fe2a701SSrivatsa Vaddagiri }
12679fe2a701SSrivatsa Vaddagiri 
126836048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
126936048853SDavid Rientjes 			      loff_t *ppos)
127036048853SDavid Rientjes {
127136048853SDavid Rientjes 	size_t size = strlen(file->private_data);
12729fe2a701SSrivatsa Vaddagiri 
12739fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
12749fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
12759fe2a701SSrivatsa Vaddagiri }
12769fe2a701SSrivatsa Vaddagiri 
12779fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
12789fe2a701SSrivatsa Vaddagiri {
12799fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
12809fe2a701SSrivatsa Vaddagiri 
12819fe2a701SSrivatsa Vaddagiri 	return 0;
12829fe2a701SSrivatsa Vaddagiri }
12839fe2a701SSrivatsa Vaddagiri 
12849fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
12859fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
12869fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
12879fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
12889fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
12899fe2a701SSrivatsa Vaddagiri };
12909fe2a701SSrivatsa Vaddagiri 
12919fe2a701SSrivatsa Vaddagiri /**
12929fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
12939fe2a701SSrivatsa Vaddagiri  * array.
12949fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
12959fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
12969fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
12979fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
12989fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
1299a2b992c8SJakub Kicinski  * @array: wrapper struct containing data pointer and size of the array.
13009fe2a701SSrivatsa Vaddagiri  *
13019fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
13029fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
13039fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
13049fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
13059fe2a701SSrivatsa Vaddagiri  */
1306c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1307a2b992c8SJakub Kicinski 			      struct dentry *parent,
1308a2b992c8SJakub Kicinski 			      struct debugfs_u32_array *array)
13099fe2a701SSrivatsa Vaddagiri {
1310a2b992c8SJakub Kicinski 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
13119fe2a701SSrivatsa Vaddagiri }
13129fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
13139fe2a701SSrivatsa Vaddagiri 
13143b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
13153b85e4abSHeiko Carstens 
13161a087c6aSAlessandro Rubini /*
13171a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
13181a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
13191a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
13201a087c6aSAlessandro Rubini  */
13211a087c6aSAlessandro Rubini 
13221a087c6aSAlessandro Rubini /**
13231a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
13241a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
13251a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1326b5763accSRandy Dunlap  * @nregs: the length of the above array
13271a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
13281a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
13291a087c6aSAlessandro Rubini  *
13301a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
13311a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
13321a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
13331a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
13341a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
13351a087c6aSAlessandro Rubini  * for example configuration of dma channels
13361a087c6aSAlessandro Rubini  */
13379761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
13381a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
13391a087c6aSAlessandro Rubini {
13409761536eSJoe Perches 	int i;
13411a087c6aSAlessandro Rubini 
13421a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
13431a087c6aSAlessandro Rubini 		if (prefix)
13449761536eSJoe Perches 			seq_printf(s, "%s", prefix);
13459761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
13465858441cSDan Carpenter 			   readl(base + regs->offset));
13479761536eSJoe Perches 		if (seq_has_overflowed(s))
13489761536eSJoe Perches 			break;
13491a087c6aSAlessandro Rubini 	}
13501a087c6aSAlessandro Rubini }
13511a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
13521a087c6aSAlessandro Rubini 
135319029f3fSChenXiaoSong static int debugfs_regset32_show(struct seq_file *s, void *data)
13541a087c6aSAlessandro Rubini {
13551a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
13561a087c6aSAlessandro Rubini 
135730332eeeSGeert Uytterhoeven 	if (regset->dev)
135830332eeeSGeert Uytterhoeven 		pm_runtime_get_sync(regset->dev);
135930332eeeSGeert Uytterhoeven 
13601a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
136130332eeeSGeert Uytterhoeven 
136230332eeeSGeert Uytterhoeven 	if (regset->dev)
136330332eeeSGeert Uytterhoeven 		pm_runtime_put(regset->dev);
136430332eeeSGeert Uytterhoeven 
13651a087c6aSAlessandro Rubini 	return 0;
13661a087c6aSAlessandro Rubini }
13671a087c6aSAlessandro Rubini 
136819029f3fSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
13691a087c6aSAlessandro Rubini 
13701a087c6aSAlessandro Rubini /**
13711a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
13721a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
13731a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
13741a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
13751a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
13761a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
13771a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
13781a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
13791a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
13801a087c6aSAlessandro Rubini  *
13811a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
13821a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
13831a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
13841a087c6aSAlessandro Rubini  */
1385ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode,
13861a087c6aSAlessandro Rubini 			     struct dentry *parent,
13871a087c6aSAlessandro Rubini 			     struct debugfs_regset32 *regset)
13881a087c6aSAlessandro Rubini {
138919029f3fSChenXiaoSong 	debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
13901a087c6aSAlessandro Rubini }
13911a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
13923b85e4abSHeiko Carstens 
13933b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
139498210b7fSArend van Spriel 
139598210b7fSArend van Spriel struct debugfs_devm_entry {
139698210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
139798210b7fSArend van Spriel 	struct device *dev;
139898210b7fSArend van Spriel };
139998210b7fSArend van Spriel 
140098210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
140198210b7fSArend van Spriel {
140298210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
140398210b7fSArend van Spriel 
140498210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
140598210b7fSArend van Spriel }
140698210b7fSArend van Spriel 
140798210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
140898210b7fSArend van Spriel 	.owner = THIS_MODULE,
140998210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
141098210b7fSArend van Spriel 	.release = single_release,
141198210b7fSArend van Spriel 	.read = seq_read,
141298210b7fSArend van Spriel 	.llseek = seq_lseek
141398210b7fSArend van Spriel };
141498210b7fSArend van Spriel 
141598210b7fSArend van Spriel /**
141698210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
141798210b7fSArend van Spriel  *
141898210b7fSArend van Spriel  * @dev: device related to this debugfs file.
141998210b7fSArend van Spriel  * @name: name of the debugfs file.
142098210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
142198210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
142298210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
142398210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
142498210b7fSArend van Spriel  */
14250d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name,
142698210b7fSArend van Spriel 				 struct dentry *parent,
14270d519cbfSGreg Kroah-Hartman 				 int (*read_fn)(struct seq_file *s, void *data))
142898210b7fSArend van Spriel {
142998210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
143098210b7fSArend van Spriel 
143198210b7fSArend van Spriel 	if (IS_ERR(parent))
14320d519cbfSGreg Kroah-Hartman 		return;
143398210b7fSArend van Spriel 
143498210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
143598210b7fSArend van Spriel 	if (!entry)
14360d519cbfSGreg Kroah-Hartman 		return;
143798210b7fSArend van Spriel 
143898210b7fSArend van Spriel 	entry->read = read_fn;
143998210b7fSArend van Spriel 	entry->dev = dev;
144098210b7fSArend van Spriel 
14410d519cbfSGreg Kroah-Hartman 	debugfs_create_file(name, S_IRUGO, parent, entry,
144298210b7fSArend van Spriel 			    &debugfs_devm_entry_ops);
144398210b7fSArend van Spriel }
144498210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1445