xref: /linux-6.15/fs/debugfs/file.c (revision 8dc6d81c)
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 
67e9117a5aSNicolai Stange /**
68e9117a5aSNicolai Stange  * debugfs_file_get - mark the beginning of file data access
69e9117a5aSNicolai Stange  * @dentry: the dentry object whose data is being accessed.
70e9117a5aSNicolai Stange  *
71e9117a5aSNicolai Stange  * Up to a matching call to debugfs_file_put(), any successive call
72e9117a5aSNicolai Stange  * into the file removing functions debugfs_remove() and
73e9117a5aSNicolai Stange  * debugfs_remove_recursive() will block. Since associated private
74e9117a5aSNicolai Stange  * file data may only get freed after a successful return of any of
75e9117a5aSNicolai Stange  * the removal functions, you may safely access it after a successful
76e9117a5aSNicolai Stange  * call to debugfs_file_get() without worrying about lifetime issues.
77e9117a5aSNicolai Stange  *
78e9117a5aSNicolai Stange  * If -%EIO is returned, the file has already been removed and thus,
79e9117a5aSNicolai Stange  * it is not safe to access any of its data. If, on the other hand,
80e9117a5aSNicolai Stange  * it is allowed to access the file data, zero is returned.
81e9117a5aSNicolai Stange  */
82e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry)
83e9117a5aSNicolai Stange {
847d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd;
857d39bc50SNicolai Stange 	void *d_fsd;
86e9117a5aSNicolai Stange 
870ed04a18SJohannes Berg 	/*
880ed04a18SJohannes Berg 	 * This could only happen if some debugfs user erroneously calls
890ed04a18SJohannes Berg 	 * debugfs_file_get() on a dentry that isn't even a file, let
900ed04a18SJohannes Berg 	 * them know about it.
910ed04a18SJohannes Berg 	 */
920ed04a18SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
930ed04a18SJohannes Berg 		return -EINVAL;
940ed04a18SJohannes Berg 
957d39bc50SNicolai Stange 	d_fsd = READ_ONCE(dentry->d_fsdata);
967d39bc50SNicolai Stange 	if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
977d39bc50SNicolai Stange 		fsd = d_fsd;
987d39bc50SNicolai Stange 	} else {
997d39bc50SNicolai Stange 		fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
1007d39bc50SNicolai Stange 		if (!fsd)
1017d39bc50SNicolai Stange 			return -ENOMEM;
1027d39bc50SNicolai Stange 
103*8dc6d81cSJohannes Berg 		if ((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_SHORT_FOPS_BIT) {
104*8dc6d81cSJohannes Berg 			fsd->real_fops = NULL;
105*8dc6d81cSJohannes Berg 			fsd->short_fops = (void *)((unsigned long)d_fsd &
106*8dc6d81cSJohannes Berg 						~(DEBUGFS_FSDATA_IS_REAL_FOPS_BIT |
107*8dc6d81cSJohannes Berg 						  DEBUGFS_FSDATA_IS_SHORT_FOPS_BIT));
108*8dc6d81cSJohannes Berg 		} else {
1097d39bc50SNicolai Stange 			fsd->real_fops = (void *)((unsigned long)d_fsd &
1107d39bc50SNicolai Stange 						~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
111*8dc6d81cSJohannes Berg 			fsd->short_fops = NULL;
112*8dc6d81cSJohannes Berg 		}
1137d39bc50SNicolai Stange 		refcount_set(&fsd->active_users, 1);
1147d39bc50SNicolai Stange 		init_completion(&fsd->active_users_drained);
115159f5bdaSJohannes Berg 		INIT_LIST_HEAD(&fsd->cancellations);
116159f5bdaSJohannes Berg 		mutex_init(&fsd->cancellations_mtx);
117159f5bdaSJohannes Berg 
1187d39bc50SNicolai Stange 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
119159f5bdaSJohannes Berg 			mutex_destroy(&fsd->cancellations_mtx);
1207d39bc50SNicolai Stange 			kfree(fsd);
1217d39bc50SNicolai Stange 			fsd = READ_ONCE(dentry->d_fsdata);
1227d39bc50SNicolai Stange 		}
1237d39bc50SNicolai Stange 	}
1247d39bc50SNicolai Stange 
1257d39bc50SNicolai Stange 	/*
1267d39bc50SNicolai Stange 	 * In case of a successful cmpxchg() above, this check is
1277d39bc50SNicolai Stange 	 * strictly necessary and must follow it, see the comment in
1287d39bc50SNicolai Stange 	 * __debugfs_remove_file().
1297d39bc50SNicolai Stange 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
1307d39bc50SNicolai Stange 	 * successful, this serves the purpose of not starving
1317d39bc50SNicolai Stange 	 * removers.
1327d39bc50SNicolai Stange 	 */
133e9117a5aSNicolai Stange 	if (d_unlinked(dentry))
134e9117a5aSNicolai Stange 		return -EIO;
135e9117a5aSNicolai Stange 
136e9117a5aSNicolai Stange 	if (!refcount_inc_not_zero(&fsd->active_users))
137e9117a5aSNicolai Stange 		return -EIO;
138e9117a5aSNicolai Stange 
139e9117a5aSNicolai Stange 	return 0;
140e9117a5aSNicolai Stange }
141e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get);
142e9117a5aSNicolai Stange 
143e9117a5aSNicolai Stange /**
144e9117a5aSNicolai Stange  * debugfs_file_put - mark the end of file data access
145e9117a5aSNicolai Stange  * @dentry: the dentry object formerly passed to
146e9117a5aSNicolai Stange  *          debugfs_file_get().
147e9117a5aSNicolai Stange  *
148e9117a5aSNicolai Stange  * Allow any ongoing concurrent call into debugfs_remove() or
149e9117a5aSNicolai Stange  * debugfs_remove_recursive() blocked by a former call to
150e9117a5aSNicolai Stange  * debugfs_file_get() to proceed and return to its caller.
151e9117a5aSNicolai Stange  */
152e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry)
153e9117a5aSNicolai Stange {
1547d39bc50SNicolai Stange 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
155e9117a5aSNicolai Stange 
156e9117a5aSNicolai Stange 	if (refcount_dec_and_test(&fsd->active_users))
157e9117a5aSNicolai Stange 		complete(&fsd->active_users_drained);
158e9117a5aSNicolai Stange }
159e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put);
160e9117a5aSNicolai Stange 
1618c88a474SJohannes Berg /**
1628c88a474SJohannes Berg  * debugfs_enter_cancellation - enter a debugfs cancellation
1638c88a474SJohannes Berg  * @file: the file being accessed
1648c88a474SJohannes Berg  * @cancellation: the cancellation object, the cancel callback
1658c88a474SJohannes Berg  *	inside of it must be initialized
1668c88a474SJohannes Berg  *
1678c88a474SJohannes Berg  * When a debugfs file is removed it needs to wait for all active
1688c88a474SJohannes Berg  * operations to complete. However, the operation itself may need
1698c88a474SJohannes Berg  * to wait for hardware or completion of some asynchronous process
1708c88a474SJohannes Berg  * or similar. As such, it may need to be cancelled to avoid long
1718c88a474SJohannes Berg  * waits or even deadlocks.
1728c88a474SJohannes Berg  *
1738c88a474SJohannes Berg  * This function can be used inside a debugfs handler that may
1748c88a474SJohannes Berg  * need to be cancelled. As soon as this function is called, the
1758c88a474SJohannes Berg  * cancellation's 'cancel' callback may be called, at which point
1768c88a474SJohannes Berg  * the caller should proceed to call debugfs_leave_cancellation()
1778c88a474SJohannes Berg  * and leave the debugfs handler function as soon as possible.
1788c88a474SJohannes Berg  * Note that the 'cancel' callback is only ever called in the
1798c88a474SJohannes Berg  * context of some kind of debugfs_remove().
1808c88a474SJohannes Berg  *
1818c88a474SJohannes Berg  * This function must be paired with debugfs_leave_cancellation().
1828c88a474SJohannes Berg  */
1838c88a474SJohannes Berg void debugfs_enter_cancellation(struct file *file,
1848c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
1858c88a474SJohannes Berg {
1868c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
1878c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
1888c88a474SJohannes Berg 
1898c88a474SJohannes Berg 	INIT_LIST_HEAD(&cancellation->list);
1908c88a474SJohannes Berg 
1918c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
1928c88a474SJohannes Berg 		return;
1938c88a474SJohannes Berg 
1948c88a474SJohannes Berg 	if (WARN_ON(!cancellation->cancel))
1958c88a474SJohannes Berg 		return;
1968c88a474SJohannes Berg 
1978c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
1988c88a474SJohannes Berg 	if (WARN_ON(!fsd ||
1998c88a474SJohannes Berg 		    ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
2008c88a474SJohannes Berg 		return;
2018c88a474SJohannes Berg 
2028c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2038c88a474SJohannes Berg 	list_add(&cancellation->list, &fsd->cancellations);
2048c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2058c88a474SJohannes Berg 
2068c88a474SJohannes Berg 	/* if we're already removing wake it up to cancel */
2078c88a474SJohannes Berg 	if (d_unlinked(dentry))
2088c88a474SJohannes Berg 		complete(&fsd->active_users_drained);
2098c88a474SJohannes Berg }
2108c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
2118c88a474SJohannes Berg 
2128c88a474SJohannes Berg /**
2138c88a474SJohannes Berg  * debugfs_leave_cancellation - leave cancellation section
2148c88a474SJohannes Berg  * @file: the file being accessed
2158c88a474SJohannes Berg  * @cancellation: the cancellation previously registered with
2168c88a474SJohannes Berg  *	debugfs_enter_cancellation()
2178c88a474SJohannes Berg  *
2188c88a474SJohannes Berg  * See the documentation of debugfs_enter_cancellation().
2198c88a474SJohannes Berg  */
2208c88a474SJohannes Berg void debugfs_leave_cancellation(struct file *file,
2218c88a474SJohannes Berg 				struct debugfs_cancellation *cancellation)
2228c88a474SJohannes Berg {
2238c88a474SJohannes Berg 	struct debugfs_fsdata *fsd;
2248c88a474SJohannes Berg 	struct dentry *dentry = F_DENTRY(file);
2258c88a474SJohannes Berg 
2268c88a474SJohannes Berg 	if (WARN_ON(!d_is_reg(dentry)))
2278c88a474SJohannes Berg 		return;
2288c88a474SJohannes Berg 
2298c88a474SJohannes Berg 	fsd = READ_ONCE(dentry->d_fsdata);
2308c88a474SJohannes Berg 	if (WARN_ON(!fsd ||
2318c88a474SJohannes Berg 		    ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
2328c88a474SJohannes Berg 		return;
2338c88a474SJohannes Berg 
2348c88a474SJohannes Berg 	mutex_lock(&fsd->cancellations_mtx);
2358c88a474SJohannes Berg 	if (!list_empty(&cancellation->list))
2368c88a474SJohannes Berg 		list_del(&cancellation->list);
2378c88a474SJohannes Berg 	mutex_unlock(&fsd->cancellations_mtx);
2388c88a474SJohannes Berg }
2398c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
2408c88a474SJohannes Berg 
2415496197fSDavid Howells /*
2425496197fSDavid Howells  * Only permit access to world-readable files when the kernel is locked down.
2435496197fSDavid Howells  * We also need to exclude any file that has ways to write or alter it as root
2445496197fSDavid Howells  * can bypass the permissions check.
2455496197fSDavid Howells  */
246a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode,
2475496197fSDavid Howells 			       struct file *filp,
2485496197fSDavid Howells 			       const struct file_operations *real_fops)
2495496197fSDavid Howells {
250358fcf5dSMichal Suchanek 	if ((inode->i_mode & 07777 & ~0444) == 0 &&
2515496197fSDavid Howells 	    !(filp->f_mode & FMODE_WRITE) &&
252*8dc6d81cSJohannes Berg 	    (!real_fops ||
253*8dc6d81cSJohannes Berg 	     (!real_fops->unlocked_ioctl &&
2545496197fSDavid Howells 	      !real_fops->compat_ioctl &&
255*8dc6d81cSJohannes Berg 	      !real_fops->mmap)))
256a37f4958SEric Snowberg 		return 0;
2575496197fSDavid Howells 
258a37f4958SEric Snowberg 	if (security_locked_down(LOCKDOWN_DEBUGFS))
259a37f4958SEric Snowberg 		return -EPERM;
260a37f4958SEric Snowberg 
261a37f4958SEric Snowberg 	return 0;
2625496197fSDavid Howells }
2635496197fSDavid Howells 
2649fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp)
2659fd4dcecSNicolai Stange {
26669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
2679fd4dcecSNicolai Stange 	const struct file_operations *real_fops = NULL;
2687d39bc50SNicolai Stange 	int r;
2699fd4dcecSNicolai Stange 
2707d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
2717d39bc50SNicolai Stange 	if (r)
2727d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
2739fd4dcecSNicolai Stange 
27486f0e067SChristian Lamparter 	real_fops = debugfs_real_fops(filp);
2755496197fSDavid Howells 
276a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
2775496197fSDavid Howells 	if (r)
2785496197fSDavid Howells 		goto out;
2795496197fSDavid Howells 
280275678e7STaehee Yoo 	if (!fops_get(real_fops)) {
281e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
282275678e7STaehee Yoo 		if (real_fops->owner &&
283112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
284112cedc8SSven Eckelmann 			r = -ENXIO;
285275678e7STaehee Yoo 			goto out;
286112cedc8SSven Eckelmann 		}
287275678e7STaehee Yoo #endif
288275678e7STaehee Yoo 
2899fd4dcecSNicolai Stange 		/* Huh? Module did not clean up after itself at exit? */
2909fd4dcecSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
2919fd4dcecSNicolai Stange 			dentry);
2929fd4dcecSNicolai Stange 		r = -ENXIO;
2939fd4dcecSNicolai Stange 		goto out;
2949fd4dcecSNicolai Stange 	}
2959fd4dcecSNicolai Stange 	replace_fops(filp, real_fops);
2969fd4dcecSNicolai Stange 
2979fd4dcecSNicolai Stange 	if (real_fops->open)
2989fd4dcecSNicolai Stange 		r = real_fops->open(inode, filp);
2999fd4dcecSNicolai Stange 
3009fd4dcecSNicolai Stange out:
30169d29f9eSNicolai Stange 	debugfs_file_put(dentry);
3029fd4dcecSNicolai Stange 	return r;
3039fd4dcecSNicolai Stange }
3049fd4dcecSNicolai Stange 
3059fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = {
3069fd4dcecSNicolai Stange 	.open = open_proxy_open,
3079fd4dcecSNicolai Stange };
3089fd4dcecSNicolai Stange 
30949d200deSNicolai Stange #define PROTO(args...) args
31049d200deSNicolai Stange #define ARGS(args...) args
31149d200deSNicolai Stange 
31249d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
31349d200deSNicolai Stange static ret_type full_proxy_ ## name(proto)				\
31449d200deSNicolai Stange {									\
31569d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);			\
316154b9d75SNicolai Stange 	const struct file_operations *real_fops;			\
31749d200deSNicolai Stange 	ret_type r;							\
31849d200deSNicolai Stange 									\
31969d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);					\
32069d29f9eSNicolai Stange 	if (unlikely(r))						\
32169d29f9eSNicolai Stange 		return r;						\
322154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);				\
32349d200deSNicolai Stange 	r = real_fops->name(args);					\
32469d29f9eSNicolai Stange 	debugfs_file_put(dentry);					\
32549d200deSNicolai Stange 	return r;							\
32649d200deSNicolai Stange }
32749d200deSNicolai Stange 
328*8dc6d81cSJohannes Berg #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args)		\
329*8dc6d81cSJohannes Berg static ret_type full_proxy_ ## name(proto)				\
330*8dc6d81cSJohannes Berg {									\
331*8dc6d81cSJohannes Berg 	struct dentry *dentry = F_DENTRY(filp);				\
332*8dc6d81cSJohannes Berg 	struct debugfs_fsdata *fsd;					\
333*8dc6d81cSJohannes Berg 	ret_type r;							\
334*8dc6d81cSJohannes Berg 									\
335*8dc6d81cSJohannes Berg 	r = debugfs_file_get(dentry);					\
336*8dc6d81cSJohannes Berg 	if (unlikely(r))						\
337*8dc6d81cSJohannes Berg 		return r;						\
338*8dc6d81cSJohannes Berg 	fsd = dentry->d_fsdata;						\
339*8dc6d81cSJohannes Berg 	if (fsd->real_fops)						\
340*8dc6d81cSJohannes Berg 		r = fsd->real_fops->name(args);				\
341*8dc6d81cSJohannes Berg 	else								\
342*8dc6d81cSJohannes Berg 		r = fsd->short_fops->name(args);			\
343*8dc6d81cSJohannes Berg 	debugfs_file_put(dentry);					\
344*8dc6d81cSJohannes Berg 	return r;							\
345*8dc6d81cSJohannes Berg }
346*8dc6d81cSJohannes Berg 
347*8dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
34849d200deSNicolai Stange 		     PROTO(struct file *filp, loff_t offset, int whence),
34949d200deSNicolai Stange 		     ARGS(filp, offset, whence));
35049d200deSNicolai Stange 
351*8dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
35249d200deSNicolai Stange 		     PROTO(struct file *filp, char __user *buf, size_t size,
35349d200deSNicolai Stange 			   loff_t *ppos),
35449d200deSNicolai Stange 		     ARGS(filp, buf, size, ppos));
35549d200deSNicolai Stange 
356*8dc6d81cSJohannes Berg FULL_PROXY_FUNC_BOTH(write, ssize_t, filp,
357*8dc6d81cSJohannes Berg 		     PROTO(struct file *filp, const char __user *buf,
358*8dc6d81cSJohannes Berg 			   size_t size, loff_t *ppos),
35949d200deSNicolai Stange 		     ARGS(filp, buf, size, ppos));
36049d200deSNicolai Stange 
36149d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
36249d200deSNicolai Stange 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
36349d200deSNicolai Stange 		ARGS(filp, cmd, arg));
36449d200deSNicolai Stange 
365076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp,
36649d200deSNicolai Stange 				struct poll_table_struct *wait)
36749d200deSNicolai Stange {
36869d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
369e6c8adcaSAl Viro 	__poll_t r = 0;
370154b9d75SNicolai Stange 	const struct file_operations *real_fops;
37149d200deSNicolai Stange 
37269d29f9eSNicolai Stange 	if (debugfs_file_get(dentry))
373a9a08845SLinus Torvalds 		return EPOLLHUP;
37449d200deSNicolai Stange 
375154b9d75SNicolai Stange 	real_fops = debugfs_real_fops(filp);
37649d200deSNicolai Stange 	r = real_fops->poll(filp, wait);
37769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
37849d200deSNicolai Stange 	return r;
37949d200deSNicolai Stange }
38049d200deSNicolai Stange 
38149d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp)
38249d200deSNicolai Stange {
38349d200deSNicolai Stange 	const struct dentry *dentry = F_DENTRY(filp);
38486f0e067SChristian Lamparter 	const struct file_operations *real_fops = debugfs_real_fops(filp);
38549d200deSNicolai Stange 	const struct file_operations *proxy_fops = filp->f_op;
38649d200deSNicolai Stange 	int r = 0;
38749d200deSNicolai Stange 
38849d200deSNicolai Stange 	/*
38949d200deSNicolai Stange 	 * We must not protect this against removal races here: the
39049d200deSNicolai Stange 	 * original releaser should be called unconditionally in order
39149d200deSNicolai Stange 	 * not to leak any resources. Releasers must not assume that
39249d200deSNicolai Stange 	 * ->i_private is still being meaningful here.
39349d200deSNicolai Stange 	 */
394*8dc6d81cSJohannes Berg 	if (real_fops && real_fops->release)
39549d200deSNicolai Stange 		r = real_fops->release(inode, filp);
39649d200deSNicolai Stange 
39749d200deSNicolai Stange 	replace_fops(filp, d_inode(dentry)->i_fop);
398c80a67bdSXu Wang 	kfree(proxy_fops);
39949d200deSNicolai Stange 	fops_put(real_fops);
400a1a9e5d2SEric Engestrom 	return r;
40149d200deSNicolai Stange }
40249d200deSNicolai Stange 
40349d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops,
404*8dc6d81cSJohannes Berg 				   struct debugfs_fsdata *fsd)
40549d200deSNicolai Stange {
40649d200deSNicolai Stange 	proxy_fops->release = full_proxy_release;
407*8dc6d81cSJohannes Berg 
408*8dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->llseek) ||
409*8dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->llseek))
41049d200deSNicolai Stange 		proxy_fops->llseek = full_proxy_llseek;
411*8dc6d81cSJohannes Berg 
412*8dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->read) ||
413*8dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->read))
41449d200deSNicolai Stange 		proxy_fops->read = full_proxy_read;
415*8dc6d81cSJohannes Berg 
416*8dc6d81cSJohannes Berg 	if ((fsd->real_fops && fsd->real_fops->write) ||
417*8dc6d81cSJohannes Berg 	    (fsd->short_fops && fsd->short_fops->write))
41849d200deSNicolai Stange 		proxy_fops->write = full_proxy_write;
419*8dc6d81cSJohannes Berg 
420*8dc6d81cSJohannes Berg 	if (fsd->real_fops && fsd->real_fops->poll)
42149d200deSNicolai Stange 		proxy_fops->poll = full_proxy_poll;
422*8dc6d81cSJohannes Berg 
423*8dc6d81cSJohannes Berg 	if (fsd->real_fops && fsd->real_fops->unlocked_ioctl)
42449d200deSNicolai Stange 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
42549d200deSNicolai Stange }
42649d200deSNicolai Stange 
42749d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp)
42849d200deSNicolai Stange {
42969d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(filp);
430*8dc6d81cSJohannes Berg 	const struct file_operations *real_fops;
43149d200deSNicolai Stange 	struct file_operations *proxy_fops = NULL;
432*8dc6d81cSJohannes Berg 	struct debugfs_fsdata *fsd;
4337d39bc50SNicolai Stange 	int r;
43449d200deSNicolai Stange 
4357d39bc50SNicolai Stange 	r = debugfs_file_get(dentry);
4367d39bc50SNicolai Stange 	if (r)
4377d39bc50SNicolai Stange 		return r == -EIO ? -ENOENT : r;
43849d200deSNicolai Stange 
439*8dc6d81cSJohannes Berg 	fsd = dentry->d_fsdata;
440*8dc6d81cSJohannes Berg 	real_fops = fsd->real_fops;
441a37f4958SEric Snowberg 	r = debugfs_locked_down(inode, filp, real_fops);
4425496197fSDavid Howells 	if (r)
4435496197fSDavid Howells 		goto out;
4445496197fSDavid Howells 
445*8dc6d81cSJohannes Berg 	if (real_fops && !fops_get(real_fops)) {
446e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES
447275678e7STaehee Yoo 		if (real_fops->owner &&
448112cedc8SSven Eckelmann 		    real_fops->owner->state == MODULE_STATE_GOING) {
449112cedc8SSven Eckelmann 			r = -ENXIO;
450275678e7STaehee Yoo 			goto out;
451112cedc8SSven Eckelmann 		}
452275678e7STaehee Yoo #endif
453275678e7STaehee Yoo 
45449d200deSNicolai Stange 		/* Huh? Module did not cleanup after itself at exit? */
45549d200deSNicolai Stange 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
45649d200deSNicolai Stange 			dentry);
45749d200deSNicolai Stange 		r = -ENXIO;
45849d200deSNicolai Stange 		goto out;
45949d200deSNicolai Stange 	}
46049d200deSNicolai Stange 
46149d200deSNicolai Stange 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
46249d200deSNicolai Stange 	if (!proxy_fops) {
46349d200deSNicolai Stange 		r = -ENOMEM;
46449d200deSNicolai Stange 		goto free_proxy;
46549d200deSNicolai Stange 	}
466*8dc6d81cSJohannes Berg 	__full_proxy_fops_init(proxy_fops, fsd);
46749d200deSNicolai Stange 	replace_fops(filp, proxy_fops);
46849d200deSNicolai Stange 
469*8dc6d81cSJohannes Berg 	if (!real_fops || real_fops->open) {
470*8dc6d81cSJohannes Berg 		if (real_fops)
47149d200deSNicolai Stange 			r = real_fops->open(inode, filp);
472*8dc6d81cSJohannes Berg 		else
473*8dc6d81cSJohannes Berg 			r = simple_open(inode, filp);
474b10e3e90SNicolai Stange 		if (r) {
475b10e3e90SNicolai Stange 			replace_fops(filp, d_inode(dentry)->i_fop);
476b10e3e90SNicolai Stange 			goto free_proxy;
477b10e3e90SNicolai Stange 		} else if (filp->f_op != proxy_fops) {
47849d200deSNicolai Stange 			/* No protection against file removal anymore. */
47949d200deSNicolai Stange 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
48049d200deSNicolai Stange 				dentry);
48149d200deSNicolai Stange 			goto free_proxy;
48249d200deSNicolai Stange 		}
48349d200deSNicolai Stange 	}
48449d200deSNicolai Stange 
48549d200deSNicolai Stange 	goto out;
48649d200deSNicolai Stange free_proxy:
48749d200deSNicolai Stange 	kfree(proxy_fops);
48849d200deSNicolai Stange 	fops_put(real_fops);
48949d200deSNicolai Stange out:
49069d29f9eSNicolai Stange 	debugfs_file_put(dentry);
49149d200deSNicolai Stange 	return r;
49249d200deSNicolai Stange }
49349d200deSNicolai Stange 
49449d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = {
49549d200deSNicolai Stange 	.open = full_proxy_open,
49649d200deSNicolai Stange };
49749d200deSNicolai Stange 
498c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf,
499c6468808SNicolai Stange 			size_t len, loff_t *ppos)
500c6468808SNicolai Stange {
50169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
502c6468808SNicolai Stange 	ssize_t ret;
503c6468808SNicolai Stange 
50469d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
50569d29f9eSNicolai Stange 	if (unlikely(ret))
50669d29f9eSNicolai Stange 		return ret;
507c6468808SNicolai Stange 	ret = simple_attr_read(file, buf, len, ppos);
50869d29f9eSNicolai Stange 	debugfs_file_put(dentry);
509c6468808SNicolai Stange 	return ret;
510c6468808SNicolai Stange }
511c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read);
512c6468808SNicolai Stange 
513d472cf79SAkinobu Mita static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
514d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos, bool is_signed)
515c6468808SNicolai Stange {
51669d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
517c6468808SNicolai Stange 	ssize_t ret;
518c6468808SNicolai Stange 
51969d29f9eSNicolai Stange 	ret = debugfs_file_get(dentry);
52069d29f9eSNicolai Stange 	if (unlikely(ret))
52169d29f9eSNicolai Stange 		return ret;
522d472cf79SAkinobu Mita 	if (is_signed)
523d472cf79SAkinobu Mita 		ret = simple_attr_write_signed(file, buf, len, ppos);
524d472cf79SAkinobu Mita 	else
525c6468808SNicolai Stange 		ret = simple_attr_write(file, buf, len, ppos);
52669d29f9eSNicolai Stange 	debugfs_file_put(dentry);
527c6468808SNicolai Stange 	return ret;
528c6468808SNicolai Stange }
529d472cf79SAkinobu Mita 
530d472cf79SAkinobu Mita ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
531d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
532d472cf79SAkinobu Mita {
533d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
534d472cf79SAkinobu Mita }
535c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write);
536c6468808SNicolai Stange 
537d472cf79SAkinobu Mita ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
538d472cf79SAkinobu Mita 			 size_t len, loff_t *ppos)
539d472cf79SAkinobu Mita {
540d472cf79SAkinobu Mita 	return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
541d472cf79SAkinobu Mita }
542d472cf79SAkinobu Mita EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
543d472cf79SAkinobu Mita 
5444909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
5454909f168SNicolai Stange 					struct dentry *parent, void *value,
5464909f168SNicolai Stange 					const struct file_operations *fops,
5474909f168SNicolai Stange 					const struct file_operations *fops_ro,
5484909f168SNicolai Stange 					const struct file_operations *fops_wo)
5494909f168SNicolai Stange {
5504909f168SNicolai Stange 	/* if there are no write bits set, make read only */
5514909f168SNicolai Stange 	if (!(mode & S_IWUGO))
5524909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5534909f168SNicolai Stange 						fops_ro);
5544909f168SNicolai Stange 	/* if there are no read bits set, make write only */
5554909f168SNicolai Stange 	if (!(mode & S_IRUGO))
5564909f168SNicolai Stange 		return debugfs_create_file_unsafe(name, mode, parent, value,
5574909f168SNicolai Stange 						fops_wo);
5584909f168SNicolai Stange 
5594909f168SNicolai Stange 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
5604909f168SNicolai Stange }
5614909f168SNicolai Stange 
5628b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
563acaefc25SArnd Bergmann {
564acaefc25SArnd Bergmann 	*(u8 *)data = val;
5658b88b099SChristoph Hellwig 	return 0;
566acaefc25SArnd Bergmann }
5678b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
568acaefc25SArnd Bergmann {
5698b88b099SChristoph Hellwig 	*val = *(u8 *)data;
5708b88b099SChristoph Hellwig 	return 0;
571acaefc25SArnd Bergmann }
5724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
5734909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
5744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /**
5776468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
5781da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
5791da177e4SLinus Torvalds  * @mode: the permission that the file should have
5801da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
5816468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
5821da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
5831da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
5841da177e4SLinus Torvalds  *         from.
5851da177e4SLinus Torvalds  *
5861da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
5871da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
5881da177e4SLinus Torvalds  * set, it can be read from, and written to.
5891da177e4SLinus Torvalds  */
5909655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
5919655ac4aSGreg Kroah-Hartman 		       u8 *value)
5921da177e4SLinus Torvalds {
5939655ac4aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
594b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
5971da177e4SLinus Torvalds 
5988b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
599acaefc25SArnd Bergmann {
600acaefc25SArnd Bergmann 	*(u16 *)data = val;
6018b88b099SChristoph Hellwig 	return 0;
602acaefc25SArnd Bergmann }
6038b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
604acaefc25SArnd Bergmann {
6058b88b099SChristoph Hellwig 	*val = *(u16 *)data;
6068b88b099SChristoph Hellwig 	return 0;
607acaefc25SArnd Bergmann }
6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
6094909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
611acaefc25SArnd Bergmann 
6121da177e4SLinus Torvalds /**
6136468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
6141da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6151da177e4SLinus Torvalds  * @mode: the permission that the file should have
6161da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6176468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6181da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6191da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6201da177e4SLinus Torvalds  *         from.
6211da177e4SLinus Torvalds  *
6221da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6231da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6241da177e4SLinus Torvalds  * set, it can be read from, and written to.
6251da177e4SLinus Torvalds  */
626313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
627313f5dbbSGreg Kroah-Hartman 			u16 *value)
6281da177e4SLinus Torvalds {
629313f5dbbSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
630b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
6331da177e4SLinus Torvalds 
6348b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
635acaefc25SArnd Bergmann {
636acaefc25SArnd Bergmann 	*(u32 *)data = val;
6378b88b099SChristoph Hellwig 	return 0;
638acaefc25SArnd Bergmann }
6398b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
640acaefc25SArnd Bergmann {
6418b88b099SChristoph Hellwig 	*val = *(u32 *)data;
6428b88b099SChristoph Hellwig 	return 0;
643acaefc25SArnd Bergmann }
6444909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
6454909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
6464909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
647acaefc25SArnd Bergmann 
6481da177e4SLinus Torvalds /**
6496468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
6501da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
6511da177e4SLinus Torvalds  * @mode: the permission that the file should have
6521da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
6536468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
6541da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
6551da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
6561da177e4SLinus Torvalds  *         from.
6571da177e4SLinus Torvalds  *
6581da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
6591da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
6601da177e4SLinus Torvalds  * set, it can be read from, and written to.
6611da177e4SLinus Torvalds  */
6622b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
6632b07021aSGreg Kroah-Hartman 			u32 *value)
6641da177e4SLinus Torvalds {
6652b07021aSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
666b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
6691da177e4SLinus Torvalds 
6708b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
6718447891fSMichael Ellerman {
6728447891fSMichael Ellerman 	*(u64 *)data = val;
6738b88b099SChristoph Hellwig 	return 0;
6748447891fSMichael Ellerman }
6758447891fSMichael Ellerman 
6768b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
6778447891fSMichael Ellerman {
6788b88b099SChristoph Hellwig 	*val = *(u64 *)data;
6798b88b099SChristoph Hellwig 	return 0;
6808447891fSMichael Ellerman }
6814909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
6824909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
6834909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
6848447891fSMichael Ellerman 
6858447891fSMichael Ellerman /**
6868447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
6878447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
6888447891fSMichael Ellerman  * @mode: the permission that the file should have
6898447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
6908447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
6918447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
6928447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
6938447891fSMichael Ellerman  *         from.
6948447891fSMichael Ellerman  *
6958447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
6968447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
6978447891fSMichael Ellerman  * set, it can be read from, and written to.
6988447891fSMichael Ellerman  */
699ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
700ad26221fSGreg Kroah-Hartman 			u64 *value)
7018447891fSMichael Ellerman {
702ad26221fSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
703b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
7048447891fSMichael Ellerman }
7058447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
7068447891fSMichael Ellerman 
707c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val)
708c23fe831SViresh Kumar {
709c23fe831SViresh Kumar 	*(unsigned long *)data = val;
710c23fe831SViresh Kumar 	return 0;
711c23fe831SViresh Kumar }
712c23fe831SViresh Kumar 
713c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val)
714c23fe831SViresh Kumar {
715c23fe831SViresh Kumar 	*val = *(unsigned long *)data;
716c23fe831SViresh Kumar 	return 0;
717c23fe831SViresh Kumar }
7184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
7194909f168SNicolai Stange 			"%llu\n");
7204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
7214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
722c23fe831SViresh Kumar 
723c23fe831SViresh Kumar /**
724c23fe831SViresh Kumar  * debugfs_create_ulong - create a debugfs file that is used to read and write
725c23fe831SViresh Kumar  * an unsigned long value.
726c23fe831SViresh Kumar  * @name: a pointer to a string containing the name of the file to create.
727c23fe831SViresh Kumar  * @mode: the permission that the file should have
728c23fe831SViresh Kumar  * @parent: a pointer to the parent dentry for this file.  This should be a
729c23fe831SViresh Kumar  *          directory dentry if set.  If this parameter is %NULL, then the
730c23fe831SViresh Kumar  *          file will be created in the root of the debugfs filesystem.
731c23fe831SViresh Kumar  * @value: a pointer to the variable that the file should read to and write
732c23fe831SViresh Kumar  *         from.
733c23fe831SViresh Kumar  *
734c23fe831SViresh Kumar  * This function creates a file in debugfs with the given name that
735c23fe831SViresh Kumar  * contains the value of the variable @value.  If the @mode variable is so
736c23fe831SViresh Kumar  * set, it can be read from, and written to.
737c23fe831SViresh Kumar  */
738fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
739fb05b14cSGreg Kroah-Hartman 			  unsigned long *value)
740c23fe831SViresh Kumar {
741fb05b14cSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
742fb05b14cSGreg Kroah-Hartman 				   &fops_ulong_ro, &fops_ulong_wo);
743c23fe831SViresh Kumar }
744c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong);
745c23fe831SViresh Kumar 
7464909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
7474909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
7484909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
7492ebefc50SRobin Getz 
7504909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
7514909f168SNicolai Stange 			"0x%04llx\n");
7524909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
7534909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
7542ebefc50SRobin Getz 
7554909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
7564909f168SNicolai Stange 			"0x%08llx\n");
7574909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
7584909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
7592ebefc50SRobin Getz 
7604909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
7614909f168SNicolai Stange 			"0x%016llx\n");
7624909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
7634909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
76415b0beaaSHuang Ying 
765e6716b87SRandy Dunlap /*
76615b0beaaSHuang 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
767e6716b87SRandy Dunlap  *
768e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
769e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
770e6716b87SRandy Dunlap  * decimal functions.
771e6716b87SRandy Dunlap  */
772e6716b87SRandy Dunlap 
7732ebefc50SRobin Getz /**
7742ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
775e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
776e6716b87SRandy Dunlap  * @mode: the permission that the file should have
777e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
778e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
779e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
780e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
781e6716b87SRandy Dunlap  *         from.
7822ebefc50SRobin Getz  */
783c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
784c7c11689SGreg Kroah-Hartman 		       u8 *value)
7852ebefc50SRobin Getz {
786c7c11689SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
787b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
7882ebefc50SRobin Getz }
7892ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
7902ebefc50SRobin Getz 
791e6716b87SRandy Dunlap /**
792e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
793e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
794e6716b87SRandy Dunlap  * @mode: the permission that the file should have
795e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
796e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
797e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
798e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
799e6716b87SRandy Dunlap  *         from.
800e6716b87SRandy Dunlap  */
801e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
802e40d38f2SGreg Kroah-Hartman 			u16 *value)
8032ebefc50SRobin Getz {
804e40d38f2SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
805b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
8062ebefc50SRobin Getz }
8072ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
8082ebefc50SRobin Getz 
809e6716b87SRandy Dunlap /**
810e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
811e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
812e6716b87SRandy Dunlap  * @mode: the permission that the file should have
813e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
814e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
815e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
816e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
817e6716b87SRandy Dunlap  *         from.
818e6716b87SRandy Dunlap  */
819f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
820f5cb0a7eSGreg Kroah-Hartman 			u32 *value)
8212ebefc50SRobin Getz {
822f5cb0a7eSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
823b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
8242ebefc50SRobin Getz }
8252ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
8262ebefc50SRobin Getz 
82715b0beaaSHuang Ying /**
82815b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
82915b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
83015b0beaaSHuang Ying  * @mode: the permission that the file should have
83115b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
83215b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
83315b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
83415b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
83515b0beaaSHuang Ying  *         from.
83615b0beaaSHuang Ying  */
8370864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
8380864c408SGreg Kroah-Hartman 			u64 *value)
83915b0beaaSHuang Ying {
8400864c408SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
84182b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
84215b0beaaSHuang Ying }
84315b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
84415b0beaaSHuang Ying 
8455e078787SInaky Perez-Gonzalez 
8465e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
8475e078787SInaky Perez-Gonzalez {
8485e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
8495e078787SInaky Perez-Gonzalez 	return 0;
8505e078787SInaky Perez-Gonzalez }
8515e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
8525e078787SInaky Perez-Gonzalez {
8535e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
8545e078787SInaky Perez-Gonzalez 	return 0;
8555e078787SInaky Perez-Gonzalez }
8564909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
8575e078787SInaky Perez-Gonzalez 			"%llu\n"); /* %llu and %zu are more or less the same */
8584909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
8594909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
8605e078787SInaky Perez-Gonzalez 
8615e078787SInaky Perez-Gonzalez /**
8625e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
8635e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
8645e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
8655e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
8665e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
8675e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
8685e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
8695e078787SInaky Perez-Gonzalez  *         from.
8705e078787SInaky Perez-Gonzalez  */
8718e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode,
8725e078787SInaky Perez-Gonzalez 			   struct dentry *parent, size_t *value)
8735e078787SInaky Perez-Gonzalez {
8748e580263SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
8758e580263SGreg Kroah-Hartman 				   &fops_size_t_ro, &fops_size_t_wo);
8765e078787SInaky Perez-Gonzalez }
8775e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
8785e078787SInaky Perez-Gonzalez 
8793a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
8803a76e5e0SSeth Jennings {
8813a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
8823a76e5e0SSeth Jennings 	return 0;
8833a76e5e0SSeth Jennings }
8843a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
8853a76e5e0SSeth Jennings {
8863a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
8873a76e5e0SSeth Jennings 	return 0;
8883a76e5e0SSeth Jennings }
889d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
8903a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
891d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
8924909f168SNicolai Stange 			"%lld\n");
893d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
8944909f168SNicolai Stange 			"%lld\n");
8953a76e5e0SSeth Jennings 
8963a76e5e0SSeth Jennings /**
8973a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
8983a76e5e0SSeth Jennings  * write an atomic_t value
8993a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
9003a76e5e0SSeth Jennings  * @mode: the permission that the file should have
9013a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
9023a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
9033a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
9043a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
9053a76e5e0SSeth Jennings  *         from.
9063a76e5e0SSeth Jennings  */
9079927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode,
9083a76e5e0SSeth Jennings 			     struct dentry *parent, atomic_t *value)
9093a76e5e0SSeth Jennings {
9109927c6faSGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
9119927c6faSGreg Kroah-Hartman 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
9123a76e5e0SSeth Jennings }
9133a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
9145e078787SInaky Perez-Gonzalez 
9150642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
9161da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
9171da177e4SLinus Torvalds {
918c8a9c285SRasmus Villemoes 	char buf[2];
9194d45f797SNicolai Stange 	bool val;
92069d29f9eSNicolai Stange 	int r;
92169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9221da177e4SLinus Torvalds 
92369d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
92469d29f9eSNicolai Stange 	if (unlikely(r))
9254d45f797SNicolai Stange 		return r;
92669d29f9eSNicolai Stange 	val = *(bool *)file->private_data;
92769d29f9eSNicolai Stange 	debugfs_file_put(dentry);
9284d45f797SNicolai Stange 
9294d45f797SNicolai Stange 	if (val)
9301da177e4SLinus Torvalds 		buf[0] = 'Y';
9311da177e4SLinus Torvalds 	else
9321da177e4SLinus Torvalds 		buf[0] = 'N';
9331da177e4SLinus Torvalds 	buf[1] = '\n';
9341da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
9351da177e4SLinus Torvalds }
9360642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
9371da177e4SLinus Torvalds 
9380642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
9391da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9401da177e4SLinus Torvalds {
9418705b48eSJonathan Cameron 	bool bv;
94269d29f9eSNicolai Stange 	int r;
943621a5f7aSViresh Kumar 	bool *val = file->private_data;
94469d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
9451da177e4SLinus Torvalds 
946964f8363SAndy Shevchenko 	r = kstrtobool_from_user(user_buf, count, &bv);
947964f8363SAndy Shevchenko 	if (!r) {
94869d29f9eSNicolai Stange 		r = debugfs_file_get(dentry);
94969d29f9eSNicolai Stange 		if (unlikely(r))
9504d45f797SNicolai Stange 			return r;
95169d29f9eSNicolai Stange 		*val = bv;
95269d29f9eSNicolai Stange 		debugfs_file_put(dentry);
9534d45f797SNicolai Stange 	}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	return count;
9561da177e4SLinus Torvalds }
9570642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
9581da177e4SLinus Torvalds 
9594b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
9600642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
9610642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
962234e3405SStephen Boyd 	.open =		simple_open,
9636038f373SArnd Bergmann 	.llseek =	default_llseek,
9641da177e4SLinus Torvalds };
9651da177e4SLinus Torvalds 
9666713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = {
9676713e8fbSStephen Boyd 	.read =		debugfs_read_file_bool,
9686713e8fbSStephen Boyd 	.open =		simple_open,
9696713e8fbSStephen Boyd 	.llseek =	default_llseek,
9706713e8fbSStephen Boyd };
9716713e8fbSStephen Boyd 
9726713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = {
9736713e8fbSStephen Boyd 	.write =	debugfs_write_file_bool,
9746713e8fbSStephen Boyd 	.open =		simple_open,
9756713e8fbSStephen Boyd 	.llseek =	default_llseek,
9766713e8fbSStephen Boyd };
9776713e8fbSStephen Boyd 
9781da177e4SLinus Torvalds /**
9796468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
9801da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
9811da177e4SLinus Torvalds  * @mode: the permission that the file should have
9821da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
9836468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
9841da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
9851da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
9861da177e4SLinus Torvalds  *         from.
9871da177e4SLinus Torvalds  *
9881da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
9891da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
9901da177e4SLinus Torvalds  * set, it can be read from, and written to.
9911da177e4SLinus Torvalds  */
992393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
993393b0638SGreg Kroah-Hartman 			 bool *value)
9941da177e4SLinus Torvalds {
995393b0638SGreg Kroah-Hartman 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
9966713e8fbSStephen Boyd 				   &fops_bool_ro, &fops_bool_wo);
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
9991da177e4SLinus Torvalds 
10009af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
10019af0440eSPeter Zijlstra 			      size_t count, loff_t *ppos)
10029af0440eSPeter Zijlstra {
10039af0440eSPeter Zijlstra 	struct dentry *dentry = F_DENTRY(file);
10049af0440eSPeter Zijlstra 	char *str, *copy = NULL;
10059af0440eSPeter Zijlstra 	int copy_len, len;
10069af0440eSPeter Zijlstra 	ssize_t ret;
10079af0440eSPeter Zijlstra 
10089af0440eSPeter Zijlstra 	ret = debugfs_file_get(dentry);
10099af0440eSPeter Zijlstra 	if (unlikely(ret))
10109af0440eSPeter Zijlstra 		return ret;
10119af0440eSPeter Zijlstra 
10129af0440eSPeter Zijlstra 	str = *(char **)file->private_data;
10139af0440eSPeter Zijlstra 	len = strlen(str) + 1;
10149af0440eSPeter Zijlstra 	copy = kmalloc(len, GFP_KERNEL);
10159af0440eSPeter Zijlstra 	if (!copy) {
10169af0440eSPeter Zijlstra 		debugfs_file_put(dentry);
10179af0440eSPeter Zijlstra 		return -ENOMEM;
10189af0440eSPeter Zijlstra 	}
10199af0440eSPeter Zijlstra 
10209af0440eSPeter Zijlstra 	copy_len = strscpy(copy, str, len);
10219af0440eSPeter Zijlstra 	debugfs_file_put(dentry);
10229af0440eSPeter Zijlstra 	if (copy_len < 0) {
10239af0440eSPeter Zijlstra 		kfree(copy);
10249af0440eSPeter Zijlstra 		return copy_len;
10259af0440eSPeter Zijlstra 	}
10269af0440eSPeter Zijlstra 
10279af0440eSPeter Zijlstra 	copy[copy_len] = '\n';
10289af0440eSPeter Zijlstra 
1029f501b6a2SDietmar Eggemann 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
10309af0440eSPeter Zijlstra 	kfree(copy);
10319af0440eSPeter Zijlstra 
10329af0440eSPeter Zijlstra 	return ret;
10339af0440eSPeter Zijlstra }
1034d60b59b9SCristian Marussi EXPORT_SYMBOL_GPL(debugfs_create_str);
10359af0440eSPeter Zijlstra 
10369af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
10379af0440eSPeter Zijlstra 				      size_t count, loff_t *ppos)
10389af0440eSPeter Zijlstra {
103986b54881SMike Tipton 	struct dentry *dentry = F_DENTRY(file);
104086b54881SMike Tipton 	char *old, *new = NULL;
104186b54881SMike Tipton 	int pos = *ppos;
104286b54881SMike Tipton 	int r;
104386b54881SMike Tipton 
104486b54881SMike Tipton 	r = debugfs_file_get(dentry);
104586b54881SMike Tipton 	if (unlikely(r))
104686b54881SMike Tipton 		return r;
104786b54881SMike Tipton 
104886b54881SMike Tipton 	old = *(char **)file->private_data;
104986b54881SMike Tipton 
105086b54881SMike Tipton 	/* only allow strict concatenation */
105186b54881SMike Tipton 	r = -EINVAL;
105286b54881SMike Tipton 	if (pos && pos != strlen(old))
105386b54881SMike Tipton 		goto error;
105486b54881SMike Tipton 
105586b54881SMike Tipton 	r = -E2BIG;
105686b54881SMike Tipton 	if (pos + count + 1 > PAGE_SIZE)
105786b54881SMike Tipton 		goto error;
105886b54881SMike Tipton 
105986b54881SMike Tipton 	r = -ENOMEM;
106086b54881SMike Tipton 	new = kmalloc(pos + count + 1, GFP_KERNEL);
106186b54881SMike Tipton 	if (!new)
106286b54881SMike Tipton 		goto error;
106386b54881SMike Tipton 
106486b54881SMike Tipton 	if (pos)
106586b54881SMike Tipton 		memcpy(new, old, pos);
106686b54881SMike Tipton 
106786b54881SMike Tipton 	r = -EFAULT;
106886b54881SMike Tipton 	if (copy_from_user(new + pos, user_buf, count))
106986b54881SMike Tipton 		goto error;
107086b54881SMike Tipton 
107186b54881SMike Tipton 	new[pos + count] = '\0';
107286b54881SMike Tipton 	strim(new);
107386b54881SMike Tipton 
10747360a48bSMike Tipton 	rcu_assign_pointer(*(char __rcu **)file->private_data, new);
107586b54881SMike Tipton 	synchronize_rcu();
107686b54881SMike Tipton 	kfree(old);
107786b54881SMike Tipton 
107886b54881SMike Tipton 	debugfs_file_put(dentry);
107986b54881SMike Tipton 	return count;
108086b54881SMike Tipton 
108186b54881SMike Tipton error:
108286b54881SMike Tipton 	kfree(new);
108386b54881SMike Tipton 	debugfs_file_put(dentry);
108486b54881SMike Tipton 	return r;
10859af0440eSPeter Zijlstra }
10869af0440eSPeter Zijlstra 
10879af0440eSPeter Zijlstra static const struct file_operations fops_str = {
10889af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
10899af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
10909af0440eSPeter Zijlstra 	.open =		simple_open,
10919af0440eSPeter Zijlstra 	.llseek =	default_llseek,
10929af0440eSPeter Zijlstra };
10939af0440eSPeter Zijlstra 
10949af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = {
10959af0440eSPeter Zijlstra 	.read =		debugfs_read_file_str,
10969af0440eSPeter Zijlstra 	.open =		simple_open,
10979af0440eSPeter Zijlstra 	.llseek =	default_llseek,
10989af0440eSPeter Zijlstra };
10999af0440eSPeter Zijlstra 
11009af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = {
11019af0440eSPeter Zijlstra 	.write =	debugfs_write_file_str,
11029af0440eSPeter Zijlstra 	.open =		simple_open,
11039af0440eSPeter Zijlstra 	.llseek =	default_llseek,
11049af0440eSPeter Zijlstra };
11059af0440eSPeter Zijlstra 
11069af0440eSPeter Zijlstra /**
11079af0440eSPeter Zijlstra  * debugfs_create_str - create a debugfs file that is used to read and write a string value
11089af0440eSPeter Zijlstra  * @name: a pointer to a string containing the name of the file to create.
11099af0440eSPeter Zijlstra  * @mode: the permission that the file should have
11109af0440eSPeter Zijlstra  * @parent: a pointer to the parent dentry for this file.  This should be a
11119af0440eSPeter Zijlstra  *          directory dentry if set.  If this parameter is %NULL, then the
11129af0440eSPeter Zijlstra  *          file will be created in the root of the debugfs filesystem.
11139af0440eSPeter Zijlstra  * @value: a pointer to the variable that the file should read to and write
11149af0440eSPeter Zijlstra  *         from.
11159af0440eSPeter Zijlstra  *
11169af0440eSPeter Zijlstra  * This function creates a file in debugfs with the given name that
11179af0440eSPeter Zijlstra  * contains the value of the variable @value.  If the @mode variable is so
11189af0440eSPeter Zijlstra  * set, it can be read from, and written to.
11199af0440eSPeter Zijlstra  */
11209af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode,
11219af0440eSPeter Zijlstra 			struct dentry *parent, char **value)
11229af0440eSPeter Zijlstra {
11239af0440eSPeter Zijlstra 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
11249af0440eSPeter Zijlstra 				   &fops_str_ro, &fops_str_wo);
11259af0440eSPeter Zijlstra }
11269af0440eSPeter Zijlstra 
1127dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1128dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
1129dd308bc3SMichael Ellerman {
1130dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
113169d29f9eSNicolai Stange 	struct dentry *dentry = F_DENTRY(file);
113283b711cbSNicolai Stange 	ssize_t r;
113383b711cbSNicolai Stange 
113469d29f9eSNicolai Stange 	r = debugfs_file_get(dentry);
113569d29f9eSNicolai Stange 	if (unlikely(r))
113669d29f9eSNicolai Stange 		return r;
113783b711cbSNicolai Stange 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1138dd308bc3SMichael Ellerman 				blob->size);
113969d29f9eSNicolai Stange 	debugfs_file_put(dentry);
114083b711cbSNicolai Stange 	return r;
1141dd308bc3SMichael Ellerman }
1142dd308bc3SMichael Ellerman 
114371cd3c63SAvadhut Naik static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
114471cd3c63SAvadhut Naik 			       size_t count, loff_t *ppos)
114571cd3c63SAvadhut Naik {
114671cd3c63SAvadhut Naik 	struct debugfs_blob_wrapper *blob = file->private_data;
114771cd3c63SAvadhut Naik 	struct dentry *dentry = F_DENTRY(file);
114871cd3c63SAvadhut Naik 	ssize_t r;
114971cd3c63SAvadhut Naik 
115071cd3c63SAvadhut Naik 	r = debugfs_file_get(dentry);
115171cd3c63SAvadhut Naik 	if (unlikely(r))
115271cd3c63SAvadhut Naik 		return r;
115371cd3c63SAvadhut Naik 	r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
115471cd3c63SAvadhut Naik 				   count);
115571cd3c63SAvadhut Naik 
115671cd3c63SAvadhut Naik 	debugfs_file_put(dentry);
115771cd3c63SAvadhut Naik 	return r;
115871cd3c63SAvadhut Naik }
115971cd3c63SAvadhut Naik 
116000977a59SArjan van de Ven static const struct file_operations fops_blob = {
1161dd308bc3SMichael Ellerman 	.read =		read_file_blob,
116271cd3c63SAvadhut Naik 	.write =	write_file_blob,
1163234e3405SStephen Boyd 	.open =		simple_open,
11646038f373SArnd Bergmann 	.llseek =	default_llseek,
1165dd308bc3SMichael Ellerman };
1166dd308bc3SMichael Ellerman 
1167dd308bc3SMichael Ellerman /**
116871cd3c63SAvadhut Naik  * debugfs_create_blob - create a debugfs file that is used to read and write
116971cd3c63SAvadhut Naik  * a binary blob
1170dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
117171cd3c63SAvadhut Naik  * @mode: the permission that the file should have
1172dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
11736468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1174dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
1175dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1176dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
1177dd308bc3SMichael Ellerman  *
1178dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
1179dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
118071cd3c63SAvadhut Naik  * read from and written to.
1181dd308bc3SMichael Ellerman  *
1182dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
1183dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
1184dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
1185adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
11869abb2499SRonald Tschalär  * returned.
1187dd308bc3SMichael Ellerman  *
1188adc92dd4SDaniel W. S. Almeida  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
11899abb2499SRonald Tschalär  * be returned.
1190dd308bc3SMichael Ellerman  */
1191f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1192dd308bc3SMichael Ellerman 				   struct dentry *parent,
1193dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
1194dd308bc3SMichael Ellerman {
119571cd3c63SAvadhut Naik 	return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
1196dd308bc3SMichael Ellerman }
1197dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
11981a087c6aSAlessandro Rubini 
1199e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
1200e05e279eSLinus Torvalds 			       u32 *array, int array_size)
12019fe2a701SSrivatsa Vaddagiri {
12029fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
12039fe2a701SSrivatsa Vaddagiri 
1204e05e279eSLinus Torvalds 	while (--array_size >= 0) {
12059fe2a701SSrivatsa Vaddagiri 		size_t len;
1206e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
12079fe2a701SSrivatsa Vaddagiri 
1208e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
12099fe2a701SSrivatsa Vaddagiri 		ret += len;
12109fe2a701SSrivatsa Vaddagiri 
12119fe2a701SSrivatsa Vaddagiri 		buf += len;
12129fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
12139fe2a701SSrivatsa Vaddagiri 	}
12149fe2a701SSrivatsa Vaddagiri 	return ret;
12159fe2a701SSrivatsa Vaddagiri }
12169fe2a701SSrivatsa Vaddagiri 
121736048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
12189fe2a701SSrivatsa Vaddagiri {
1219a2b992c8SJakub Kicinski 	struct debugfs_u32_array *data = inode->i_private;
1220a2b992c8SJakub Kicinski 	int size, elements = data->n_elements;
1221e05e279eSLinus Torvalds 	char *buf;
12229fe2a701SSrivatsa Vaddagiri 
1223e05e279eSLinus Torvalds 	/*
1224e05e279eSLinus Torvalds 	 * Max size:
1225e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1226e05e279eSLinus Torvalds 	 *  - terminating NUL character
1227e05e279eSLinus Torvalds 	 */
1228e05e279eSLinus Torvalds 	size = elements*11;
1229e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
1230e05e279eSLinus Torvalds 	if (!buf)
123136048853SDavid Rientjes 		return -ENOMEM;
1232e05e279eSLinus Torvalds 	buf[size] = 0;
1233e05e279eSLinus Torvalds 
1234e05e279eSLinus Torvalds 	file->private_data = buf;
1235a2b992c8SJakub Kicinski 	u32_format_array(buf, size, data->array, data->n_elements);
1236e05e279eSLinus Torvalds 
123736048853SDavid Rientjes 	return nonseekable_open(inode, file);
12389fe2a701SSrivatsa Vaddagiri }
12399fe2a701SSrivatsa Vaddagiri 
124036048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
124136048853SDavid Rientjes 			      loff_t *ppos)
124236048853SDavid Rientjes {
124336048853SDavid Rientjes 	size_t size = strlen(file->private_data);
12449fe2a701SSrivatsa Vaddagiri 
12459fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
12469fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
12479fe2a701SSrivatsa Vaddagiri }
12489fe2a701SSrivatsa Vaddagiri 
12499fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
12509fe2a701SSrivatsa Vaddagiri {
12519fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
12529fe2a701SSrivatsa Vaddagiri 
12539fe2a701SSrivatsa Vaddagiri 	return 0;
12549fe2a701SSrivatsa Vaddagiri }
12559fe2a701SSrivatsa Vaddagiri 
12569fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
12579fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
12589fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
12599fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
12609fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
12619fe2a701SSrivatsa Vaddagiri };
12629fe2a701SSrivatsa Vaddagiri 
12639fe2a701SSrivatsa Vaddagiri /**
12649fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
12659fe2a701SSrivatsa Vaddagiri  * array.
12669fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
12679fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
12689fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
12699fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
12709fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
1271a2b992c8SJakub Kicinski  * @array: wrapper struct containing data pointer and size of the array.
12729fe2a701SSrivatsa Vaddagiri  *
12739fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
12749fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
12759fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
12769fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
12779fe2a701SSrivatsa Vaddagiri  */
1278c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode,
1279a2b992c8SJakub Kicinski 			      struct dentry *parent,
1280a2b992c8SJakub Kicinski 			      struct debugfs_u32_array *array)
12819fe2a701SSrivatsa Vaddagiri {
1282a2b992c8SJakub Kicinski 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
12839fe2a701SSrivatsa Vaddagiri }
12849fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
12859fe2a701SSrivatsa Vaddagiri 
12863b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
12873b85e4abSHeiko Carstens 
12881a087c6aSAlessandro Rubini /*
12891a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
12901a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
12911a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
12921a087c6aSAlessandro Rubini  */
12931a087c6aSAlessandro Rubini 
12941a087c6aSAlessandro Rubini /**
12951a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
12961a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
12971a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
1298b5763accSRandy Dunlap  * @nregs: the length of the above array
12991a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
13001a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
13011a087c6aSAlessandro Rubini  *
13021a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
13031a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
13041a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
13051a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
13061a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
13071a087c6aSAlessandro Rubini  * for example configuration of dma channels
13081a087c6aSAlessandro Rubini  */
13099761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
13101a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
13111a087c6aSAlessandro Rubini {
13129761536eSJoe Perches 	int i;
13131a087c6aSAlessandro Rubini 
13141a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
13151a087c6aSAlessandro Rubini 		if (prefix)
13169761536eSJoe Perches 			seq_printf(s, "%s", prefix);
13179761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
13185858441cSDan Carpenter 			   readl(base + regs->offset));
13199761536eSJoe Perches 		if (seq_has_overflowed(s))
13209761536eSJoe Perches 			break;
13211a087c6aSAlessandro Rubini 	}
13221a087c6aSAlessandro Rubini }
13231a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
13241a087c6aSAlessandro Rubini 
132519029f3fSChenXiaoSong static int debugfs_regset32_show(struct seq_file *s, void *data)
13261a087c6aSAlessandro Rubini {
13271a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
13281a087c6aSAlessandro Rubini 
132930332eeeSGeert Uytterhoeven 	if (regset->dev)
133030332eeeSGeert Uytterhoeven 		pm_runtime_get_sync(regset->dev);
133130332eeeSGeert Uytterhoeven 
13321a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
133330332eeeSGeert Uytterhoeven 
133430332eeeSGeert Uytterhoeven 	if (regset->dev)
133530332eeeSGeert Uytterhoeven 		pm_runtime_put(regset->dev);
133630332eeeSGeert Uytterhoeven 
13371a087c6aSAlessandro Rubini 	return 0;
13381a087c6aSAlessandro Rubini }
13391a087c6aSAlessandro Rubini 
134019029f3fSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
13411a087c6aSAlessandro Rubini 
13421a087c6aSAlessandro Rubini /**
13431a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
13441a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
13451a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
13461a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
13471a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
13481a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
13491a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
13501a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
13511a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
13521a087c6aSAlessandro Rubini  *
13531a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
13541a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
13551a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
13561a087c6aSAlessandro Rubini  */
1357ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode,
13581a087c6aSAlessandro Rubini 			     struct dentry *parent,
13591a087c6aSAlessandro Rubini 			     struct debugfs_regset32 *regset)
13601a087c6aSAlessandro Rubini {
136119029f3fSChenXiaoSong 	debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
13621a087c6aSAlessandro Rubini }
13631a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
13643b85e4abSHeiko Carstens 
13653b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
136698210b7fSArend van Spriel 
136798210b7fSArend van Spriel struct debugfs_devm_entry {
136898210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
136998210b7fSArend van Spriel 	struct device *dev;
137098210b7fSArend van Spriel };
137198210b7fSArend van Spriel 
137298210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
137398210b7fSArend van Spriel {
137498210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
137598210b7fSArend van Spriel 
137698210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
137798210b7fSArend van Spriel }
137898210b7fSArend van Spriel 
137998210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
138098210b7fSArend van Spriel 	.owner = THIS_MODULE,
138198210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
138298210b7fSArend van Spriel 	.release = single_release,
138398210b7fSArend van Spriel 	.read = seq_read,
138498210b7fSArend van Spriel 	.llseek = seq_lseek
138598210b7fSArend van Spriel };
138698210b7fSArend van Spriel 
138798210b7fSArend van Spriel /**
138898210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
138998210b7fSArend van Spriel  *
139098210b7fSArend van Spriel  * @dev: device related to this debugfs file.
139198210b7fSArend van Spriel  * @name: name of the debugfs file.
139298210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
139398210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
139498210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
139598210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
139698210b7fSArend van Spriel  */
13970d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name,
139898210b7fSArend van Spriel 				 struct dentry *parent,
13990d519cbfSGreg Kroah-Hartman 				 int (*read_fn)(struct seq_file *s, void *data))
140098210b7fSArend van Spriel {
140198210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
140298210b7fSArend van Spriel 
140398210b7fSArend van Spriel 	if (IS_ERR(parent))
14040d519cbfSGreg Kroah-Hartman 		return;
140598210b7fSArend van Spriel 
140698210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
140798210b7fSArend van Spriel 	if (!entry)
14080d519cbfSGreg Kroah-Hartman 		return;
140998210b7fSArend van Spriel 
141098210b7fSArend van Spriel 	entry->read = read_fn;
141198210b7fSArend van Spriel 	entry->dev = dev;
141298210b7fSArend van Spriel 
14130d519cbfSGreg Kroah-Hartman 	debugfs_create_file(name, S_IRUGO, parent, entry,
141498210b7fSArend van Spriel 			    &debugfs_devm_entry_ops);
141598210b7fSArend van Spriel }
141698210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1417