xref: /linux-6.15/fs/debugfs/file.c (revision c42d2237)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
51da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
91da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
12883ce42eSRobert P. J. Day  *  See Documentation/DocBook/filesystems for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
1966f54963SPeter Oberparleiter #include <linux/namei.h>
201da177e4SLinus Torvalds #include <linux/debugfs.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
231da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	return 0;
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
291da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	return count;
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file)
351da177e4SLinus Torvalds {
368e18e294STheodore Ts'o 	if (inode->i_private)
378e18e294STheodore Ts'o 		file->private_data = inode->i_private;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 	return 0;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
424b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = {
431da177e4SLinus Torvalds 	.read =		default_read_file,
441da177e4SLinus Torvalds 	.write =	default_write_file,
451da177e4SLinus Torvalds 	.open =		default_open,
466038f373SArnd Bergmann 	.llseek =	noop_llseek,
471da177e4SLinus Torvalds };
481da177e4SLinus Torvalds 
4966f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
5066f54963SPeter Oberparleiter {
5166f54963SPeter Oberparleiter 	nd_set_link(nd, dentry->d_inode->i_private);
5266f54963SPeter Oberparleiter 	return NULL;
5366f54963SPeter Oberparleiter }
5466f54963SPeter Oberparleiter 
5566f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = {
5666f54963SPeter Oberparleiter 	.readlink       = generic_readlink,
5766f54963SPeter Oberparleiter 	.follow_link    = debugfs_follow_link,
5866f54963SPeter Oberparleiter };
5966f54963SPeter Oberparleiter 
608b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
61acaefc25SArnd Bergmann {
62acaefc25SArnd Bergmann 	*(u8 *)data = val;
638b88b099SChristoph Hellwig 	return 0;
64acaefc25SArnd Bergmann }
658b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
66acaefc25SArnd Bergmann {
678b88b099SChristoph Hellwig 	*val = *(u8 *)data;
688b88b099SChristoph Hellwig 	return 0;
69acaefc25SArnd Bergmann }
70acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
71e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
72e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /**
756468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
761da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
771da177e4SLinus Torvalds  * @mode: the permission that the file should have
781da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
796468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
801da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
811da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
821da177e4SLinus Torvalds  *         from.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
851da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
861da177e4SLinus Torvalds  * set, it can be read from, and written to.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
891da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
901da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
916468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
921da177e4SLinus Torvalds  *
936468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
941da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
956468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
961da177e4SLinus Torvalds  * code.
971da177e4SLinus Torvalds  */
981da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode,
991da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
1001da177e4SLinus Torvalds {
101e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
102e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
103e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
104e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
105e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
106e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
107e4792aa3SRobin Getz 
1081da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u8);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
1111da177e4SLinus Torvalds 
1128b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
113acaefc25SArnd Bergmann {
114acaefc25SArnd Bergmann 	*(u16 *)data = val;
1158b88b099SChristoph Hellwig 	return 0;
116acaefc25SArnd Bergmann }
1178b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
118acaefc25SArnd Bergmann {
1198b88b099SChristoph Hellwig 	*val = *(u16 *)data;
1208b88b099SChristoph Hellwig 	return 0;
121acaefc25SArnd Bergmann }
122acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
123e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
124e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
125acaefc25SArnd Bergmann 
1261da177e4SLinus Torvalds /**
1276468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1281da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1291da177e4SLinus Torvalds  * @mode: the permission that the file should have
1301da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1316468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1321da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1331da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1341da177e4SLinus Torvalds  *         from.
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1371da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1381da177e4SLinus Torvalds  * set, it can be read from, and written to.
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1411da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1421da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1436468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1441da177e4SLinus Torvalds  *
1456468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1461da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1476468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1481da177e4SLinus Torvalds  * code.
1491da177e4SLinus Torvalds  */
1501da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode,
1511da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
1521da177e4SLinus Torvalds {
153e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
154e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
155e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
156e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
157e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
158e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
159e4792aa3SRobin Getz 
1601da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u16);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
1631da177e4SLinus Torvalds 
1648b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
165acaefc25SArnd Bergmann {
166acaefc25SArnd Bergmann 	*(u32 *)data = val;
1678b88b099SChristoph Hellwig 	return 0;
168acaefc25SArnd Bergmann }
1698b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
170acaefc25SArnd Bergmann {
1718b88b099SChristoph Hellwig 	*val = *(u32 *)data;
1728b88b099SChristoph Hellwig 	return 0;
173acaefc25SArnd Bergmann }
174acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
175e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
176e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
177acaefc25SArnd Bergmann 
1781da177e4SLinus Torvalds /**
1796468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1801da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1811da177e4SLinus Torvalds  * @mode: the permission that the file should have
1821da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1836468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1841da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1851da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1861da177e4SLinus Torvalds  *         from.
1871da177e4SLinus Torvalds  *
1881da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1891da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1901da177e4SLinus Torvalds  * set, it can be read from, and written to.
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1931da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1941da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1956468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1961da177e4SLinus Torvalds  *
1976468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1981da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1996468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2001da177e4SLinus Torvalds  * code.
2011da177e4SLinus Torvalds  */
2021da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode,
2031da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
2041da177e4SLinus Torvalds {
205e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
206e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
207e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
208e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
209e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
210e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
211e4792aa3SRobin Getz 
2121da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u32);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
2151da177e4SLinus Torvalds 
2168b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
2178447891fSMichael Ellerman {
2188447891fSMichael Ellerman 	*(u64 *)data = val;
2198b88b099SChristoph Hellwig 	return 0;
2208447891fSMichael Ellerman }
2218447891fSMichael Ellerman 
2228b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
2238447891fSMichael Ellerman {
2248b88b099SChristoph Hellwig 	*val = *(u64 *)data;
2258b88b099SChristoph Hellwig 	return 0;
2268447891fSMichael Ellerman }
2278447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
228e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
229e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
2308447891fSMichael Ellerman 
2318447891fSMichael Ellerman /**
2328447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
2338447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
2348447891fSMichael Ellerman  * @mode: the permission that the file should have
2358447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
2368447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
2378447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
2388447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
2398447891fSMichael Ellerman  *         from.
2408447891fSMichael Ellerman  *
2418447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
2428447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
2438447891fSMichael Ellerman  * set, it can be read from, and written to.
2448447891fSMichael Ellerman  *
2458447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
2468447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
2478447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
2488447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
2498447891fSMichael Ellerman  *
2508447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
2518447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
2528447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2538447891fSMichael Ellerman  * code.
2548447891fSMichael Ellerman  */
2558447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode,
2568447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
2578447891fSMichael Ellerman {
258e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
259e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
260e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
261e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
262e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
263e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
264e4792aa3SRobin Getz 
2658447891fSMichael Ellerman 	return debugfs_create_file(name, mode, parent, value, &fops_u64);
2668447891fSMichael Ellerman }
2678447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
2688447891fSMichael Ellerman 
2692ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
270e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
271e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
2722ebefc50SRobin Getz 
2732ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
274e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
275e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
2762ebefc50SRobin Getz 
2772ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
278e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
279e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
2802ebefc50SRobin Getz 
28115b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
28215b0beaaSHuang Ying 
283e6716b87SRandy Dunlap /*
28415b0beaaSHuang 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
285e6716b87SRandy Dunlap  *
286e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
287e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
288e6716b87SRandy Dunlap  * decimal functions.
289e6716b87SRandy Dunlap  */
290e6716b87SRandy Dunlap 
2912ebefc50SRobin Getz /**
2922ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
293e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
294e6716b87SRandy Dunlap  * @mode: the permission that the file should have
295e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
296e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
297e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
298e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
299e6716b87SRandy Dunlap  *         from.
3002ebefc50SRobin Getz  */
3012ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode,
3022ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
3032ebefc50SRobin Getz {
304e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
305e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
306e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
307e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
308e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
309e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
310e4792aa3SRobin Getz 
3112ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x8);
3122ebefc50SRobin Getz }
3132ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
3142ebefc50SRobin Getz 
315e6716b87SRandy Dunlap /**
316e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
317e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
318e6716b87SRandy Dunlap  * @mode: the permission that the file should have
319e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
320e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
321e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
322e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
323e6716b87SRandy Dunlap  *         from.
324e6716b87SRandy Dunlap  */
3252ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode,
3262ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
3272ebefc50SRobin Getz {
328e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
329e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
330e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
331e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
332e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
333e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
334e4792aa3SRobin Getz 
3352ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x16);
3362ebefc50SRobin Getz }
3372ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
3382ebefc50SRobin Getz 
339e6716b87SRandy Dunlap /**
340e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
341e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
342e6716b87SRandy Dunlap  * @mode: the permission that the file should have
343e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
344e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
345e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
346e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
347e6716b87SRandy Dunlap  *         from.
348e6716b87SRandy Dunlap  */
3492ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode,
3502ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
3512ebefc50SRobin Getz {
352e4792aa3SRobin Getz 	/* if there are no write bits set, make read only */
353e4792aa3SRobin Getz 	if (!(mode & S_IWUGO))
354e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
355e4792aa3SRobin Getz 	/* if there are no read bits set, make write only */
356e4792aa3SRobin Getz 	if (!(mode & S_IRUGO))
357e4792aa3SRobin Getz 		return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
358e4792aa3SRobin Getz 
3592ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x32);
3602ebefc50SRobin Getz }
3612ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
3622ebefc50SRobin Getz 
36315b0beaaSHuang Ying /**
36415b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
36515b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
36615b0beaaSHuang Ying  * @mode: the permission that the file should have
36715b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
36815b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
36915b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
37015b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
37115b0beaaSHuang Ying  *         from.
37215b0beaaSHuang Ying  */
37315b0beaaSHuang Ying struct dentry *debugfs_create_x64(const char *name, mode_t mode,
37415b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
37515b0beaaSHuang Ying {
37615b0beaaSHuang Ying 	return debugfs_create_file(name, mode, parent, value, &fops_x64);
37715b0beaaSHuang Ying }
37815b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
37915b0beaaSHuang Ying 
3805e078787SInaky Perez-Gonzalez 
3815e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
3825e078787SInaky Perez-Gonzalez {
3835e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
3845e078787SInaky Perez-Gonzalez 	return 0;
3855e078787SInaky Perez-Gonzalez }
3865e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
3875e078787SInaky Perez-Gonzalez {
3885e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
3895e078787SInaky Perez-Gonzalez 	return 0;
3905e078787SInaky Perez-Gonzalez }
3915e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
3925e078787SInaky Perez-Gonzalez 			"%llu\n");	/* %llu and %zu are more or less the same */
3935e078787SInaky Perez-Gonzalez 
3945e078787SInaky Perez-Gonzalez /**
3955e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
3965e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
3975e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
3985e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
3995e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
4005e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
4015e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
4025e078787SInaky Perez-Gonzalez  *         from.
4035e078787SInaky Perez-Gonzalez  */
4045e078787SInaky Perez-Gonzalez struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
4055e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
4065e078787SInaky Perez-Gonzalez {
4075e078787SInaky Perez-Gonzalez 	return debugfs_create_file(name, mode, parent, value, &fops_size_t);
4085e078787SInaky Perez-Gonzalez }
4095e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
4105e078787SInaky Perez-Gonzalez 
4115e078787SInaky Perez-Gonzalez 
4121da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf,
4131da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	char buf[3];
4161da177e4SLinus Torvalds 	u32 *val = file->private_data;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (*val)
4191da177e4SLinus Torvalds 		buf[0] = 'Y';
4201da177e4SLinus Torvalds 	else
4211da177e4SLinus Torvalds 		buf[0] = 'N';
4221da177e4SLinus Torvalds 	buf[1] = '\n';
4231da177e4SLinus Torvalds 	buf[2] = 0x00;
4241da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
4281da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	char buf[32];
431*c42d2237SStephen Boyd 	size_t buf_size;
4328705b48eSJonathan Cameron 	bool bv;
4331da177e4SLinus Torvalds 	u32 *val = file->private_data;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
4361da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
4371da177e4SLinus Torvalds 		return -EFAULT;
4381da177e4SLinus Torvalds 
4398705b48eSJonathan Cameron 	if (strtobool(buf, &bv) == 0)
4408705b48eSJonathan Cameron 		*val = bv;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	return count;
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4454b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
4461da177e4SLinus Torvalds 	.read =		read_file_bool,
4471da177e4SLinus Torvalds 	.write =	write_file_bool,
4481da177e4SLinus Torvalds 	.open =		default_open,
4496038f373SArnd Bergmann 	.llseek =	default_llseek,
4501da177e4SLinus Torvalds };
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /**
4536468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
4541da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4551da177e4SLinus Torvalds  * @mode: the permission that the file should have
4561da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4576468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4581da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4591da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4601da177e4SLinus Torvalds  *         from.
4611da177e4SLinus Torvalds  *
4621da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4631da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4641da177e4SLinus Torvalds  * set, it can be read from, and written to.
4651da177e4SLinus Torvalds  *
4661da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4671da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4681da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4696468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4701da177e4SLinus Torvalds  *
4716468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4721da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4736468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4741da177e4SLinus Torvalds  * code.
4751da177e4SLinus Torvalds  */
4761da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode,
4771da177e4SLinus Torvalds 				   struct dentry *parent, u32 *value)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
4821da177e4SLinus Torvalds 
483dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
484dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
485dd308bc3SMichael Ellerman {
486dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
487dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
488dd308bc3SMichael Ellerman 			blob->size);
489dd308bc3SMichael Ellerman }
490dd308bc3SMichael Ellerman 
49100977a59SArjan van de Ven static const struct file_operations fops_blob = {
492dd308bc3SMichael Ellerman 	.read =		read_file_blob,
493dd308bc3SMichael Ellerman 	.open =		default_open,
4946038f373SArnd Bergmann 	.llseek =	default_llseek,
495dd308bc3SMichael Ellerman };
496dd308bc3SMichael Ellerman 
497dd308bc3SMichael Ellerman /**
498400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
499dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
500dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
501dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5026468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
503dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
504dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
505dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
506dd308bc3SMichael Ellerman  *
507dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
508dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
509dd308bc3SMichael Ellerman  * read from. Writing is not supported.
510dd308bc3SMichael Ellerman  *
511dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
512dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
513dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
5146468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
515dd308bc3SMichael Ellerman  *
5166468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
517dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
5186468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
519dd308bc3SMichael Ellerman  * code.
520dd308bc3SMichael Ellerman  */
521dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode,
522dd308bc3SMichael Ellerman 				   struct dentry *parent,
523dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
524dd308bc3SMichael Ellerman {
525dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
526dd308bc3SMichael Ellerman }
527dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
528