xref: /linux-6.15/fs/debugfs/file.c (revision 5e078787)
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,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
4866f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
4966f54963SPeter Oberparleiter {
5066f54963SPeter Oberparleiter 	nd_set_link(nd, dentry->d_inode->i_private);
5166f54963SPeter Oberparleiter 	return NULL;
5266f54963SPeter Oberparleiter }
5366f54963SPeter Oberparleiter 
5466f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = {
5566f54963SPeter Oberparleiter 	.readlink       = generic_readlink,
5666f54963SPeter Oberparleiter 	.follow_link    = debugfs_follow_link,
5766f54963SPeter Oberparleiter };
5866f54963SPeter Oberparleiter 
598b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
60acaefc25SArnd Bergmann {
61acaefc25SArnd Bergmann 	*(u8 *)data = val;
628b88b099SChristoph Hellwig 	return 0;
63acaefc25SArnd Bergmann }
648b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
65acaefc25SArnd Bergmann {
668b88b099SChristoph Hellwig 	*val = *(u8 *)data;
678b88b099SChristoph Hellwig 	return 0;
68acaefc25SArnd Bergmann }
69acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /**
726468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
731da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
741da177e4SLinus Torvalds  * @mode: the permission that the file should have
751da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
766468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
771da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
781da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
791da177e4SLinus Torvalds  *         from.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
821da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
831da177e4SLinus Torvalds  * set, it can be read from, and written to.
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
861da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
871da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
886468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
891da177e4SLinus Torvalds  *
906468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
911da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
926468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
931da177e4SLinus Torvalds  * code.
941da177e4SLinus Torvalds  */
951da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode,
961da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u8);
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
1011da177e4SLinus Torvalds 
1028b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
103acaefc25SArnd Bergmann {
104acaefc25SArnd Bergmann 	*(u16 *)data = val;
1058b88b099SChristoph Hellwig 	return 0;
106acaefc25SArnd Bergmann }
1078b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
108acaefc25SArnd Bergmann {
1098b88b099SChristoph Hellwig 	*val = *(u16 *)data;
1108b88b099SChristoph Hellwig 	return 0;
111acaefc25SArnd Bergmann }
112acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
113acaefc25SArnd Bergmann 
1141da177e4SLinus Torvalds /**
1156468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1161da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1171da177e4SLinus Torvalds  * @mode: the permission that the file should have
1181da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1196468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1201da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1211da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1221da177e4SLinus Torvalds  *         from.
1231da177e4SLinus Torvalds  *
1241da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1251da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1261da177e4SLinus Torvalds  * set, it can be read from, and written to.
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1291da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1301da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1316468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1321da177e4SLinus Torvalds  *
1336468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1341da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1356468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1361da177e4SLinus Torvalds  * code.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode,
1391da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u16);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
1441da177e4SLinus Torvalds 
1458b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
146acaefc25SArnd Bergmann {
147acaefc25SArnd Bergmann 	*(u32 *)data = val;
1488b88b099SChristoph Hellwig 	return 0;
149acaefc25SArnd Bergmann }
1508b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
151acaefc25SArnd Bergmann {
1528b88b099SChristoph Hellwig 	*val = *(u32 *)data;
1538b88b099SChristoph Hellwig 	return 0;
154acaefc25SArnd Bergmann }
155acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
156acaefc25SArnd Bergmann 
1571da177e4SLinus Torvalds /**
1586468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1591da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1601da177e4SLinus Torvalds  * @mode: the permission that the file should have
1611da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1626468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1631da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1641da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1651da177e4SLinus Torvalds  *         from.
1661da177e4SLinus Torvalds  *
1671da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1681da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1691da177e4SLinus Torvalds  * set, it can be read from, and written to.
1701da177e4SLinus Torvalds  *
1711da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1721da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1731da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1746468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1751da177e4SLinus Torvalds  *
1766468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1771da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1786468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1791da177e4SLinus Torvalds  * code.
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode,
1821da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_u32);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
1871da177e4SLinus Torvalds 
1888b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
1898447891fSMichael Ellerman {
1908447891fSMichael Ellerman 	*(u64 *)data = val;
1918b88b099SChristoph Hellwig 	return 0;
1928447891fSMichael Ellerman }
1938447891fSMichael Ellerman 
1948b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
1958447891fSMichael Ellerman {
1968b88b099SChristoph Hellwig 	*val = *(u64 *)data;
1978b88b099SChristoph Hellwig 	return 0;
1988447891fSMichael Ellerman }
1998447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
2008447891fSMichael Ellerman 
2018447891fSMichael Ellerman /**
2028447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
2038447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
2048447891fSMichael Ellerman  * @mode: the permission that the file should have
2058447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
2068447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
2078447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
2088447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
2098447891fSMichael Ellerman  *         from.
2108447891fSMichael Ellerman  *
2118447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
2128447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
2138447891fSMichael Ellerman  * set, it can be read from, and written to.
2148447891fSMichael Ellerman  *
2158447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
2168447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
2178447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
2188447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
2198447891fSMichael Ellerman  *
2208447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
2218447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
2228447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2238447891fSMichael Ellerman  * code.
2248447891fSMichael Ellerman  */
2258447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode,
2268447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
2278447891fSMichael Ellerman {
2288447891fSMichael Ellerman 	return debugfs_create_file(name, mode, parent, value, &fops_u64);
2298447891fSMichael Ellerman }
2308447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
2318447891fSMichael Ellerman 
2322ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
2332ebefc50SRobin Getz 
2342ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
2352ebefc50SRobin Getz 
2362ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
2372ebefc50SRobin Getz 
238e6716b87SRandy Dunlap /*
239e6716b87SRandy Dunlap  * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
240e6716b87SRandy Dunlap  *
241e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
242e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
243e6716b87SRandy Dunlap  * decimal functions.
244e6716b87SRandy Dunlap  */
245e6716b87SRandy Dunlap 
2462ebefc50SRobin Getz /**
2472ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
248e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
249e6716b87SRandy Dunlap  * @mode: the permission that the file should have
250e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
251e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
252e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
253e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
254e6716b87SRandy Dunlap  *         from.
2552ebefc50SRobin Getz  */
2562ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode,
2572ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
2582ebefc50SRobin Getz {
2592ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x8);
2602ebefc50SRobin Getz }
2612ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
2622ebefc50SRobin Getz 
263e6716b87SRandy Dunlap /**
264e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
265e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
266e6716b87SRandy Dunlap  * @mode: the permission that the file should have
267e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
268e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
269e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
270e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
271e6716b87SRandy Dunlap  *         from.
272e6716b87SRandy Dunlap  */
2732ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode,
2742ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
2752ebefc50SRobin Getz {
2762ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x16);
2772ebefc50SRobin Getz }
2782ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
2792ebefc50SRobin Getz 
280e6716b87SRandy Dunlap /**
281e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
282e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
283e6716b87SRandy Dunlap  * @mode: the permission that the file should have
284e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
285e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
286e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
287e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
288e6716b87SRandy Dunlap  *         from.
289e6716b87SRandy Dunlap  */
2902ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode,
2912ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
2922ebefc50SRobin Getz {
2932ebefc50SRobin Getz 	return debugfs_create_file(name, mode, parent, value, &fops_x32);
2942ebefc50SRobin Getz }
2952ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
2962ebefc50SRobin Getz 
297*5e078787SInaky Perez-Gonzalez 
298*5e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
299*5e078787SInaky Perez-Gonzalez {
300*5e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
301*5e078787SInaky Perez-Gonzalez 	return 0;
302*5e078787SInaky Perez-Gonzalez }
303*5e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
304*5e078787SInaky Perez-Gonzalez {
305*5e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
306*5e078787SInaky Perez-Gonzalez 	return 0;
307*5e078787SInaky Perez-Gonzalez }
308*5e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
309*5e078787SInaky Perez-Gonzalez 			"%llu\n");	/* %llu and %zu are more or less the same */
310*5e078787SInaky Perez-Gonzalez 
311*5e078787SInaky Perez-Gonzalez /**
312*5e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
313*5e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
314*5e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
315*5e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
316*5e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
317*5e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
318*5e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
319*5e078787SInaky Perez-Gonzalez  *         from.
320*5e078787SInaky Perez-Gonzalez  */
321*5e078787SInaky Perez-Gonzalez struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
322*5e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
323*5e078787SInaky Perez-Gonzalez {
324*5e078787SInaky Perez-Gonzalez 	return debugfs_create_file(name, mode, parent, value, &fops_size_t);
325*5e078787SInaky Perez-Gonzalez }
326*5e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
327*5e078787SInaky Perez-Gonzalez 
328*5e078787SInaky Perez-Gonzalez 
3291da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf,
3301da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	char buf[3];
3331da177e4SLinus Torvalds 	u32 *val = file->private_data;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (*val)
3361da177e4SLinus Torvalds 		buf[0] = 'Y';
3371da177e4SLinus Torvalds 	else
3381da177e4SLinus Torvalds 		buf[0] = 'N';
3391da177e4SLinus Torvalds 	buf[1] = '\n';
3401da177e4SLinus Torvalds 	buf[2] = 0x00;
3411da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
3451da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	char buf[32];
3481da177e4SLinus Torvalds 	int buf_size;
3491da177e4SLinus Torvalds 	u32 *val = file->private_data;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
3521da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
3531da177e4SLinus Torvalds 		return -EFAULT;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	switch (buf[0]) {
3561da177e4SLinus Torvalds 	case 'y':
3571da177e4SLinus Torvalds 	case 'Y':
3581da177e4SLinus Torvalds 	case '1':
3591da177e4SLinus Torvalds 		*val = 1;
3601da177e4SLinus Torvalds 		break;
3611da177e4SLinus Torvalds 	case 'n':
3621da177e4SLinus Torvalds 	case 'N':
3631da177e4SLinus Torvalds 	case '0':
3641da177e4SLinus Torvalds 		*val = 0;
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	return count;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3714b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
3721da177e4SLinus Torvalds 	.read =		read_file_bool,
3731da177e4SLinus Torvalds 	.write =	write_file_bool,
3741da177e4SLinus Torvalds 	.open =		default_open,
3751da177e4SLinus Torvalds };
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /**
3786468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
3791da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
3801da177e4SLinus Torvalds  * @mode: the permission that the file should have
3811da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3826468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
3831da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3841da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
3851da177e4SLinus Torvalds  *         from.
3861da177e4SLinus Torvalds  *
3871da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
3881da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
3891da177e4SLinus Torvalds  * set, it can be read from, and written to.
3901da177e4SLinus Torvalds  *
3911da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3921da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3931da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3946468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3951da177e4SLinus Torvalds  *
3966468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
3971da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
3986468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
3991da177e4SLinus Torvalds  * code.
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode,
4021da177e4SLinus Torvalds 				   struct dentry *parent, u32 *value)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
4071da177e4SLinus Torvalds 
408dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
409dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
410dd308bc3SMichael Ellerman {
411dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
412dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
413dd308bc3SMichael Ellerman 			blob->size);
414dd308bc3SMichael Ellerman }
415dd308bc3SMichael Ellerman 
41600977a59SArjan van de Ven static const struct file_operations fops_blob = {
417dd308bc3SMichael Ellerman 	.read =		read_file_blob,
418dd308bc3SMichael Ellerman 	.open =		default_open,
419dd308bc3SMichael Ellerman };
420dd308bc3SMichael Ellerman 
421dd308bc3SMichael Ellerman /**
4226468b3afSRandy Dunlap  * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
423dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
424dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
425dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
4266468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
427dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
428dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
429dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
430dd308bc3SMichael Ellerman  *
431dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
432dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
433dd308bc3SMichael Ellerman  * read from. Writing is not supported.
434dd308bc3SMichael Ellerman  *
435dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
436dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
437dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
4386468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
439dd308bc3SMichael Ellerman  *
4406468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
441dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
4426468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
443dd308bc3SMichael Ellerman  * code.
444dd308bc3SMichael Ellerman  */
445dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode,
446dd308bc3SMichael Ellerman 				   struct dentry *parent,
447dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
448dd308bc3SMichael Ellerman {
449dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
450dd308bc3SMichael Ellerman }
451dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
452