xref: /linux-6.15/fs/debugfs/file.c (revision 6db6652a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  file.c - part of debugfs, a tiny little debug file system
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
51da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
91da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
12883ce42eSRobert P. J. Day  *  See Documentation/DocBook/filesystems for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181a087c6aSAlessandro Rubini #include <linux/seq_file.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/debugfs.h>
2103e099fbSAlessandro Rubini #include <linux/io.h>
229fe2a701SSrivatsa Vaddagiri #include <linux/slab.h>
233a76e5e0SSeth Jennings #include <linux/atomic.h>
2498210b7fSArend van Spriel #include <linux/device.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf,
271da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	return 0;
301da177e4SLinus Torvalds }
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf,
331da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	return count;
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
384b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = {
391da177e4SLinus Torvalds 	.read =		default_read_file,
401da177e4SLinus Torvalds 	.write =	default_write_file,
41234e3405SStephen Boyd 	.open =		simple_open,
426038f373SArnd Bergmann 	.llseek =	noop_llseek,
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds 
45b97f6799SStephen Boyd static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
46b97f6799SStephen Boyd 					  struct dentry *parent, void *value,
47b97f6799SStephen Boyd 				          const struct file_operations *fops,
48b97f6799SStephen Boyd 				          const struct file_operations *fops_ro,
49b97f6799SStephen Boyd 				          const struct file_operations *fops_wo)
50b97f6799SStephen Boyd {
51b97f6799SStephen Boyd 	/* if there are no write bits set, make read only */
52b97f6799SStephen Boyd 	if (!(mode & S_IWUGO))
53b97f6799SStephen Boyd 		return debugfs_create_file(name, mode, parent, value, fops_ro);
54b97f6799SStephen Boyd 	/* if there are no read bits set, make write only */
55b97f6799SStephen Boyd 	if (!(mode & S_IRUGO))
56b97f6799SStephen Boyd 		return debugfs_create_file(name, mode, parent, value, fops_wo);
57b97f6799SStephen Boyd 
58b97f6799SStephen Boyd 	return debugfs_create_file(name, mode, parent, value, fops);
59b97f6799SStephen Boyd }
60b97f6799SStephen Boyd 
618b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val)
62acaefc25SArnd Bergmann {
63acaefc25SArnd Bergmann 	*(u8 *)data = val;
648b88b099SChristoph Hellwig 	return 0;
65acaefc25SArnd Bergmann }
668b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val)
67acaefc25SArnd Bergmann {
688b88b099SChristoph Hellwig 	*val = *(u8 *)data;
698b88b099SChristoph Hellwig 	return 0;
70acaefc25SArnd Bergmann }
71acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
72e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
73e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /**
766468b3afSRandy Dunlap  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
771da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
781da177e4SLinus Torvalds  * @mode: the permission that the file should have
791da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
806468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
811da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
821da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
831da177e4SLinus Torvalds  *         from.
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
861da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
871da177e4SLinus Torvalds  * set, it can be read from, and written to.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
901da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
911da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
926468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
931da177e4SLinus Torvalds  *
946468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
951da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
966468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
971da177e4SLinus Torvalds  * code.
981da177e4SLinus Torvalds  */
99f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode,
1001da177e4SLinus Torvalds 				 struct dentry *parent, u8 *value)
1011da177e4SLinus Torvalds {
102b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_u8,
103b97f6799SStephen Boyd 				   &fops_u8_ro, &fops_u8_wo);
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8);
1061da177e4SLinus Torvalds 
1078b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val)
108acaefc25SArnd Bergmann {
109acaefc25SArnd Bergmann 	*(u16 *)data = val;
1108b88b099SChristoph Hellwig 	return 0;
111acaefc25SArnd Bergmann }
1128b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val)
113acaefc25SArnd Bergmann {
1148b88b099SChristoph Hellwig 	*val = *(u16 *)data;
1158b88b099SChristoph Hellwig 	return 0;
116acaefc25SArnd Bergmann }
117acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
118e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
119e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
120acaefc25SArnd Bergmann 
1211da177e4SLinus Torvalds /**
1226468b3afSRandy Dunlap  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1231da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1241da177e4SLinus Torvalds  * @mode: the permission that the file should have
1251da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1266468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1271da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1281da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1291da177e4SLinus Torvalds  *         from.
1301da177e4SLinus Torvalds  *
1311da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1321da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1331da177e4SLinus Torvalds  * set, it can be read from, and written to.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1361da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1371da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1386468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1391da177e4SLinus Torvalds  *
1406468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1411da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1426468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1431da177e4SLinus Torvalds  * code.
1441da177e4SLinus Torvalds  */
145f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode,
1461da177e4SLinus Torvalds 				  struct dentry *parent, u16 *value)
1471da177e4SLinus Torvalds {
148b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_u16,
149b97f6799SStephen Boyd 				   &fops_u16_ro, &fops_u16_wo);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16);
1521da177e4SLinus Torvalds 
1538b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val)
154acaefc25SArnd Bergmann {
155acaefc25SArnd Bergmann 	*(u32 *)data = val;
1568b88b099SChristoph Hellwig 	return 0;
157acaefc25SArnd Bergmann }
1588b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val)
159acaefc25SArnd Bergmann {
1608b88b099SChristoph Hellwig 	*val = *(u32 *)data;
1618b88b099SChristoph Hellwig 	return 0;
162acaefc25SArnd Bergmann }
163acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
164e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
165e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
166acaefc25SArnd Bergmann 
1671da177e4SLinus Torvalds /**
1686468b3afSRandy Dunlap  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1691da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
1701da177e4SLinus Torvalds  * @mode: the permission that the file should have
1711da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
1726468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
1731da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
1741da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
1751da177e4SLinus Torvalds  *         from.
1761da177e4SLinus Torvalds  *
1771da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
1781da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
1791da177e4SLinus Torvalds  * set, it can be read from, and written to.
1801da177e4SLinus Torvalds  *
1811da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
1821da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
1831da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
1846468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
1851da177e4SLinus Torvalds  *
1866468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1871da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
1886468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1891da177e4SLinus Torvalds  * code.
1901da177e4SLinus Torvalds  */
191f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode,
1921da177e4SLinus Torvalds 				 struct dentry *parent, u32 *value)
1931da177e4SLinus Torvalds {
194b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_u32,
195b97f6799SStephen Boyd 				   &fops_u32_ro, &fops_u32_wo);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32);
1981da177e4SLinus Torvalds 
1998b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val)
2008447891fSMichael Ellerman {
2018447891fSMichael Ellerman 	*(u64 *)data = val;
2028b88b099SChristoph Hellwig 	return 0;
2038447891fSMichael Ellerman }
2048447891fSMichael Ellerman 
2058b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val)
2068447891fSMichael Ellerman {
2078b88b099SChristoph Hellwig 	*val = *(u64 *)data;
2088b88b099SChristoph Hellwig 	return 0;
2098447891fSMichael Ellerman }
2108447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
211e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
212e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
2138447891fSMichael Ellerman 
2148447891fSMichael Ellerman /**
2158447891fSMichael Ellerman  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
2168447891fSMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
2178447891fSMichael Ellerman  * @mode: the permission that the file should have
2188447891fSMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
2198447891fSMichael Ellerman  *          directory dentry if set.  If this parameter is %NULL, then the
2208447891fSMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
2218447891fSMichael Ellerman  * @value: a pointer to the variable that the file should read to and write
2228447891fSMichael Ellerman  *         from.
2238447891fSMichael Ellerman  *
2248447891fSMichael Ellerman  * This function creates a file in debugfs with the given name that
2258447891fSMichael Ellerman  * contains the value of the variable @value.  If the @mode variable is so
2268447891fSMichael Ellerman  * set, it can be read from, and written to.
2278447891fSMichael Ellerman  *
2288447891fSMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
2298447891fSMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
2308447891fSMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
2318447891fSMichael Ellerman  * you are responsible here.)  If an error occurs, %NULL will be returned.
2328447891fSMichael Ellerman  *
2338447891fSMichael Ellerman  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
2348447891fSMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
2358447891fSMichael Ellerman  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
2368447891fSMichael Ellerman  * code.
2378447891fSMichael Ellerman  */
238f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode,
2398447891fSMichael Ellerman 				 struct dentry *parent, u64 *value)
2408447891fSMichael Ellerman {
241b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_u64,
242b97f6799SStephen Boyd 				   &fops_u64_ro, &fops_u64_wo);
2438447891fSMichael Ellerman }
2448447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64);
2458447891fSMichael Ellerman 
2462ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
247e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
248e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
2492ebefc50SRobin Getz 
2502ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
251e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
252e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
2532ebefc50SRobin Getz 
2542ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
255e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
256e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
2572ebefc50SRobin Getz 
25815b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
25982b7d4fbSStephen Boyd DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
26082b7d4fbSStephen Boyd DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
26115b0beaaSHuang Ying 
262e6716b87SRandy Dunlap /*
26315b0beaaSHuang 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
264e6716b87SRandy Dunlap  *
265e6716b87SRandy Dunlap  * These functions are exactly the same as the above functions (but use a hex
266e6716b87SRandy Dunlap  * output for the decimal challenged). For details look at the above unsigned
267e6716b87SRandy Dunlap  * decimal functions.
268e6716b87SRandy Dunlap  */
269e6716b87SRandy Dunlap 
2702ebefc50SRobin Getz /**
2712ebefc50SRobin Getz  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
272e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
273e6716b87SRandy Dunlap  * @mode: the permission that the file should have
274e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
275e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
276e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
277e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
278e6716b87SRandy Dunlap  *         from.
2792ebefc50SRobin Getz  */
280f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode,
2812ebefc50SRobin Getz 				 struct dentry *parent, u8 *value)
2822ebefc50SRobin Getz {
283b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_x8,
284b97f6799SStephen Boyd 				   &fops_x8_ro, &fops_x8_wo);
2852ebefc50SRobin Getz }
2862ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8);
2872ebefc50SRobin Getz 
288e6716b87SRandy Dunlap /**
289e6716b87SRandy Dunlap  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
290e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
291e6716b87SRandy Dunlap  * @mode: the permission that the file should have
292e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
293e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
294e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
295e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
296e6716b87SRandy Dunlap  *         from.
297e6716b87SRandy Dunlap  */
298f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode,
2992ebefc50SRobin Getz 				 struct dentry *parent, u16 *value)
3002ebefc50SRobin Getz {
301b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_x16,
302b97f6799SStephen Boyd 				   &fops_x16_ro, &fops_x16_wo);
3032ebefc50SRobin Getz }
3042ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16);
3052ebefc50SRobin Getz 
306e6716b87SRandy Dunlap /**
307e6716b87SRandy Dunlap  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
308e6716b87SRandy Dunlap  * @name: a pointer to a string containing the name of the file to create.
309e6716b87SRandy Dunlap  * @mode: the permission that the file should have
310e6716b87SRandy Dunlap  * @parent: a pointer to the parent dentry for this file.  This should be a
311e6716b87SRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
312e6716b87SRandy Dunlap  *          file will be created in the root of the debugfs filesystem.
313e6716b87SRandy Dunlap  * @value: a pointer to the variable that the file should read to and write
314e6716b87SRandy Dunlap  *         from.
315e6716b87SRandy Dunlap  */
316f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode,
3172ebefc50SRobin Getz 				 struct dentry *parent, u32 *value)
3182ebefc50SRobin Getz {
319b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_x32,
320b97f6799SStephen Boyd 				   &fops_x32_ro, &fops_x32_wo);
3212ebefc50SRobin Getz }
3222ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32);
3232ebefc50SRobin Getz 
32415b0beaaSHuang Ying /**
32515b0beaaSHuang Ying  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
32615b0beaaSHuang Ying  * @name: a pointer to a string containing the name of the file to create.
32715b0beaaSHuang Ying  * @mode: the permission that the file should have
32815b0beaaSHuang Ying  * @parent: a pointer to the parent dentry for this file.  This should be a
32915b0beaaSHuang Ying  *          directory dentry if set.  If this parameter is %NULL, then the
33015b0beaaSHuang Ying  *          file will be created in the root of the debugfs filesystem.
33115b0beaaSHuang Ying  * @value: a pointer to the variable that the file should read to and write
33215b0beaaSHuang Ying  *         from.
33315b0beaaSHuang Ying  */
334f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode,
33515b0beaaSHuang Ying 				 struct dentry *parent, u64 *value)
33615b0beaaSHuang Ying {
33782b7d4fbSStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_x64,
33882b7d4fbSStephen Boyd 				   &fops_x64_ro, &fops_x64_wo);
33915b0beaaSHuang Ying }
34015b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64);
34115b0beaaSHuang Ying 
3425e078787SInaky Perez-Gonzalez 
3435e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val)
3445e078787SInaky Perez-Gonzalez {
3455e078787SInaky Perez-Gonzalez 	*(size_t *)data = val;
3465e078787SInaky Perez-Gonzalez 	return 0;
3475e078787SInaky Perez-Gonzalez }
3485e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val)
3495e078787SInaky Perez-Gonzalez {
3505e078787SInaky Perez-Gonzalez 	*val = *(size_t *)data;
3515e078787SInaky Perez-Gonzalez 	return 0;
3525e078787SInaky Perez-Gonzalez }
3535e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
3545e078787SInaky Perez-Gonzalez 			"%llu\n");	/* %llu and %zu are more or less the same */
355*6db6652aSStephen Boyd DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
356*6db6652aSStephen Boyd DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
3575e078787SInaky Perez-Gonzalez 
3585e078787SInaky Perez-Gonzalez /**
3595e078787SInaky Perez-Gonzalez  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
3605e078787SInaky Perez-Gonzalez  * @name: a pointer to a string containing the name of the file to create.
3615e078787SInaky Perez-Gonzalez  * @mode: the permission that the file should have
3625e078787SInaky Perez-Gonzalez  * @parent: a pointer to the parent dentry for this file.  This should be a
3635e078787SInaky Perez-Gonzalez  *          directory dentry if set.  If this parameter is %NULL, then the
3645e078787SInaky Perez-Gonzalez  *          file will be created in the root of the debugfs filesystem.
3655e078787SInaky Perez-Gonzalez  * @value: a pointer to the variable that the file should read to and write
3665e078787SInaky Perez-Gonzalez  *         from.
3675e078787SInaky Perez-Gonzalez  */
368f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
3695e078787SInaky Perez-Gonzalez 				     struct dentry *parent, size_t *value)
3705e078787SInaky Perez-Gonzalez {
371*6db6652aSStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
372*6db6652aSStephen Boyd 				   &fops_size_t_ro, &fops_size_t_wo);
3735e078787SInaky Perez-Gonzalez }
3745e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t);
3755e078787SInaky Perez-Gonzalez 
3763a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val)
3773a76e5e0SSeth Jennings {
3783a76e5e0SSeth Jennings 	atomic_set((atomic_t *)data, val);
3793a76e5e0SSeth Jennings 	return 0;
3803a76e5e0SSeth Jennings }
3813a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val)
3823a76e5e0SSeth Jennings {
3833a76e5e0SSeth Jennings 	*val = atomic_read((atomic_t *)data);
3843a76e5e0SSeth Jennings 	return 0;
3853a76e5e0SSeth Jennings }
3863a76e5e0SSeth Jennings DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
3873a76e5e0SSeth Jennings 			debugfs_atomic_t_set, "%lld\n");
3883a76e5e0SSeth Jennings DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
3893a76e5e0SSeth Jennings DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
3903a76e5e0SSeth Jennings 
3913a76e5e0SSeth Jennings /**
3923a76e5e0SSeth Jennings  * debugfs_create_atomic_t - create a debugfs file that is used to read and
3933a76e5e0SSeth Jennings  * write an atomic_t value
3943a76e5e0SSeth Jennings  * @name: a pointer to a string containing the name of the file to create.
3953a76e5e0SSeth Jennings  * @mode: the permission that the file should have
3963a76e5e0SSeth Jennings  * @parent: a pointer to the parent dentry for this file.  This should be a
3973a76e5e0SSeth Jennings  *          directory dentry if set.  If this parameter is %NULL, then the
3983a76e5e0SSeth Jennings  *          file will be created in the root of the debugfs filesystem.
3993a76e5e0SSeth Jennings  * @value: a pointer to the variable that the file should read to and write
4003a76e5e0SSeth Jennings  *         from.
4013a76e5e0SSeth Jennings  */
4023a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
4033a76e5e0SSeth Jennings 				 struct dentry *parent, atomic_t *value)
4043a76e5e0SSeth Jennings {
405b97f6799SStephen Boyd 	return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
406b97f6799SStephen Boyd 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
4073a76e5e0SSeth Jennings }
4083a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
4095e078787SInaky Perez-Gonzalez 
4100642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
4111da177e4SLinus Torvalds 			       size_t count, loff_t *ppos)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	char buf[3];
414621a5f7aSViresh Kumar 	bool *val = file->private_data;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	if (*val)
4171da177e4SLinus Torvalds 		buf[0] = 'Y';
4181da177e4SLinus Torvalds 	else
4191da177e4SLinus Torvalds 		buf[0] = 'N';
4201da177e4SLinus Torvalds 	buf[1] = '\n';
4211da177e4SLinus Torvalds 	buf[2] = 0x00;
4221da177e4SLinus Torvalds 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
4231da177e4SLinus Torvalds }
4240642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
4251da177e4SLinus Torvalds 
4260642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
4271da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	char buf[32];
430c42d2237SStephen Boyd 	size_t buf_size;
4318705b48eSJonathan Cameron 	bool bv;
432621a5f7aSViresh Kumar 	bool *val = file->private_data;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	buf_size = min(count, (sizeof(buf)-1));
4351da177e4SLinus Torvalds 	if (copy_from_user(buf, user_buf, buf_size))
4361da177e4SLinus Torvalds 		return -EFAULT;
4371da177e4SLinus Torvalds 
438a3b2c8c7SMathias Krause 	buf[buf_size] = '\0';
4398705b48eSJonathan Cameron 	if (strtobool(buf, &bv) == 0)
4408705b48eSJonathan Cameron 		*val = bv;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	return count;
4431da177e4SLinus Torvalds }
4440642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
4451da177e4SLinus Torvalds 
4464b6f5d20SArjan van de Ven static const struct file_operations fops_bool = {
4470642ef6fSRichard Fitzgerald 	.read =		debugfs_read_file_bool,
4480642ef6fSRichard Fitzgerald 	.write =	debugfs_write_file_bool,
449234e3405SStephen Boyd 	.open =		simple_open,
4506038f373SArnd Bergmann 	.llseek =	default_llseek,
4511da177e4SLinus Torvalds };
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds /**
4546468b3afSRandy Dunlap  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
4551da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
4561da177e4SLinus Torvalds  * @mode: the permission that the file should have
4571da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
4586468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
4591da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4601da177e4SLinus Torvalds  * @value: a pointer to the variable that the file should read to and write
4611da177e4SLinus Torvalds  *         from.
4621da177e4SLinus Torvalds  *
4631da177e4SLinus Torvalds  * This function creates a file in debugfs with the given name that
4641da177e4SLinus Torvalds  * contains the value of the variable @value.  If the @mode variable is so
4651da177e4SLinus Torvalds  * set, it can be read from, and written to.
4661da177e4SLinus Torvalds  *
4671da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4681da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4691da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4706468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4711da177e4SLinus Torvalds  *
4726468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
4731da177e4SLinus Torvalds  * returned.  It is not wise to check for this value, but rather, check for
4746468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
4751da177e4SLinus Torvalds  * code.
4761da177e4SLinus Torvalds  */
477f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode,
478621a5f7aSViresh Kumar 				   struct dentry *parent, bool *value)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool);
4831da177e4SLinus Torvalds 
484dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf,
485dd308bc3SMichael Ellerman 			      size_t count, loff_t *ppos)
486dd308bc3SMichael Ellerman {
487dd308bc3SMichael Ellerman 	struct debugfs_blob_wrapper *blob = file->private_data;
488dd308bc3SMichael Ellerman 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
489dd308bc3SMichael Ellerman 			blob->size);
490dd308bc3SMichael Ellerman }
491dd308bc3SMichael Ellerman 
49200977a59SArjan van de Ven static const struct file_operations fops_blob = {
493dd308bc3SMichael Ellerman 	.read =		read_file_blob,
494234e3405SStephen Boyd 	.open =		simple_open,
4956038f373SArnd Bergmann 	.llseek =	default_llseek,
496dd308bc3SMichael Ellerman };
497dd308bc3SMichael Ellerman 
498dd308bc3SMichael Ellerman /**
499400ced61SJonathan Corbet  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
500dd308bc3SMichael Ellerman  * @name: a pointer to a string containing the name of the file to create.
501dd308bc3SMichael Ellerman  * @mode: the permission that the file should have
502dd308bc3SMichael Ellerman  * @parent: a pointer to the parent dentry for this file.  This should be a
5036468b3afSRandy Dunlap  *          directory dentry if set.  If this parameter is %NULL, then the
504dd308bc3SMichael Ellerman  *          file will be created in the root of the debugfs filesystem.
505dd308bc3SMichael Ellerman  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
506dd308bc3SMichael Ellerman  *        to the blob data and the size of the data.
507dd308bc3SMichael Ellerman  *
508dd308bc3SMichael Ellerman  * This function creates a file in debugfs with the given name that exports
509dd308bc3SMichael Ellerman  * @blob->data as a binary blob. If the @mode variable is so set it can be
510dd308bc3SMichael Ellerman  * read from. Writing is not supported.
511dd308bc3SMichael Ellerman  *
512dd308bc3SMichael Ellerman  * This function will return a pointer to a dentry if it succeeds.  This
513dd308bc3SMichael Ellerman  * pointer must be passed to the debugfs_remove() function when the file is
514dd308bc3SMichael Ellerman  * to be removed (no automatic cleanup happens if your module is unloaded,
5156468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
516dd308bc3SMichael Ellerman  *
5176468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
518dd308bc3SMichael Ellerman  * returned.  It is not wise to check for this value, but rather, check for
5196468b3afSRandy Dunlap  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
520dd308bc3SMichael Ellerman  * code.
521dd308bc3SMichael Ellerman  */
522f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode,
523dd308bc3SMichael Ellerman 				   struct dentry *parent,
524dd308bc3SMichael Ellerman 				   struct debugfs_blob_wrapper *blob)
525dd308bc3SMichael Ellerman {
526dd308bc3SMichael Ellerman 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
527dd308bc3SMichael Ellerman }
528dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob);
5291a087c6aSAlessandro Rubini 
5309fe2a701SSrivatsa Vaddagiri struct array_data {
5319fe2a701SSrivatsa Vaddagiri 	void *array;
5329fe2a701SSrivatsa Vaddagiri 	u32 elements;
5339fe2a701SSrivatsa Vaddagiri };
5349fe2a701SSrivatsa Vaddagiri 
535e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize,
536e05e279eSLinus Torvalds 			       u32 *array, int array_size)
5379fe2a701SSrivatsa Vaddagiri {
5389fe2a701SSrivatsa Vaddagiri 	size_t ret = 0;
5399fe2a701SSrivatsa Vaddagiri 
540e05e279eSLinus Torvalds 	while (--array_size >= 0) {
5419fe2a701SSrivatsa Vaddagiri 		size_t len;
542e05e279eSLinus Torvalds 		char term = array_size ? ' ' : '\n';
5439fe2a701SSrivatsa Vaddagiri 
544e05e279eSLinus Torvalds 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
5459fe2a701SSrivatsa Vaddagiri 		ret += len;
5469fe2a701SSrivatsa Vaddagiri 
5479fe2a701SSrivatsa Vaddagiri 		buf += len;
5489fe2a701SSrivatsa Vaddagiri 		bufsize -= len;
5499fe2a701SSrivatsa Vaddagiri 	}
5509fe2a701SSrivatsa Vaddagiri 	return ret;
5519fe2a701SSrivatsa Vaddagiri }
5529fe2a701SSrivatsa Vaddagiri 
55336048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file)
5549fe2a701SSrivatsa Vaddagiri {
5559fe2a701SSrivatsa Vaddagiri 	struct array_data *data = inode->i_private;
556e05e279eSLinus Torvalds 	int size, elements = data->elements;
557e05e279eSLinus Torvalds 	char *buf;
5589fe2a701SSrivatsa Vaddagiri 
559e05e279eSLinus Torvalds 	/*
560e05e279eSLinus Torvalds 	 * Max size:
561e05e279eSLinus Torvalds 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
562e05e279eSLinus Torvalds 	 *  - terminating NUL character
563e05e279eSLinus Torvalds 	 */
564e05e279eSLinus Torvalds 	size = elements*11;
565e05e279eSLinus Torvalds 	buf = kmalloc(size+1, GFP_KERNEL);
566e05e279eSLinus Torvalds 	if (!buf)
56736048853SDavid Rientjes 		return -ENOMEM;
568e05e279eSLinus Torvalds 	buf[size] = 0;
569e05e279eSLinus Torvalds 
570e05e279eSLinus Torvalds 	file->private_data = buf;
571e05e279eSLinus Torvalds 	u32_format_array(buf, size, data->array, data->elements);
572e05e279eSLinus Torvalds 
57336048853SDavid Rientjes 	return nonseekable_open(inode, file);
5749fe2a701SSrivatsa Vaddagiri }
5759fe2a701SSrivatsa Vaddagiri 
57636048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
57736048853SDavid Rientjes 			      loff_t *ppos)
57836048853SDavid Rientjes {
57936048853SDavid Rientjes 	size_t size = strlen(file->private_data);
5809fe2a701SSrivatsa Vaddagiri 
5819fe2a701SSrivatsa Vaddagiri 	return simple_read_from_buffer(buf, len, ppos,
5829fe2a701SSrivatsa Vaddagiri 					file->private_data, size);
5839fe2a701SSrivatsa Vaddagiri }
5849fe2a701SSrivatsa Vaddagiri 
5859fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file)
5869fe2a701SSrivatsa Vaddagiri {
5879fe2a701SSrivatsa Vaddagiri 	kfree(file->private_data);
5889fe2a701SSrivatsa Vaddagiri 
5899fe2a701SSrivatsa Vaddagiri 	return 0;
5909fe2a701SSrivatsa Vaddagiri }
5919fe2a701SSrivatsa Vaddagiri 
5929fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = {
5939fe2a701SSrivatsa Vaddagiri 	.owner	 = THIS_MODULE,
5949fe2a701SSrivatsa Vaddagiri 	.open	 = u32_array_open,
5959fe2a701SSrivatsa Vaddagiri 	.release = u32_array_release,
5969fe2a701SSrivatsa Vaddagiri 	.read	 = u32_array_read,
5979fe2a701SSrivatsa Vaddagiri 	.llseek  = no_llseek,
5989fe2a701SSrivatsa Vaddagiri };
5999fe2a701SSrivatsa Vaddagiri 
6009fe2a701SSrivatsa Vaddagiri /**
6019fe2a701SSrivatsa Vaddagiri  * debugfs_create_u32_array - create a debugfs file that is used to read u32
6029fe2a701SSrivatsa Vaddagiri  * array.
6039fe2a701SSrivatsa Vaddagiri  * @name: a pointer to a string containing the name of the file to create.
6049fe2a701SSrivatsa Vaddagiri  * @mode: the permission that the file should have.
6059fe2a701SSrivatsa Vaddagiri  * @parent: a pointer to the parent dentry for this file.  This should be a
6069fe2a701SSrivatsa Vaddagiri  *          directory dentry if set.  If this parameter is %NULL, then the
6079fe2a701SSrivatsa Vaddagiri  *          file will be created in the root of the debugfs filesystem.
6089fe2a701SSrivatsa Vaddagiri  * @array: u32 array that provides data.
6099fe2a701SSrivatsa Vaddagiri  * @elements: total number of elements in the array.
6109fe2a701SSrivatsa Vaddagiri  *
6119fe2a701SSrivatsa Vaddagiri  * This function creates a file in debugfs with the given name that exports
6129fe2a701SSrivatsa Vaddagiri  * @array as data. If the @mode variable is so set it can be read from.
6139fe2a701SSrivatsa Vaddagiri  * Writing is not supported. Seek within the file is also not supported.
6149fe2a701SSrivatsa Vaddagiri  * Once array is created its size can not be changed.
6159fe2a701SSrivatsa Vaddagiri  *
6169fe2a701SSrivatsa Vaddagiri  * The function returns a pointer to dentry on success. If debugfs is not
6179fe2a701SSrivatsa Vaddagiri  * enabled in the kernel, the value -%ENODEV will be returned.
6189fe2a701SSrivatsa Vaddagiri  */
6199fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
6209fe2a701SSrivatsa Vaddagiri 					    struct dentry *parent,
6219fe2a701SSrivatsa Vaddagiri 					    u32 *array, u32 elements)
6229fe2a701SSrivatsa Vaddagiri {
6239fe2a701SSrivatsa Vaddagiri 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
6249fe2a701SSrivatsa Vaddagiri 
6259fe2a701SSrivatsa Vaddagiri 	if (data == NULL)
6269fe2a701SSrivatsa Vaddagiri 		return NULL;
6279fe2a701SSrivatsa Vaddagiri 
6289fe2a701SSrivatsa Vaddagiri 	data->array = array;
6299fe2a701SSrivatsa Vaddagiri 	data->elements = elements;
6309fe2a701SSrivatsa Vaddagiri 
6319fe2a701SSrivatsa Vaddagiri 	return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
6329fe2a701SSrivatsa Vaddagiri }
6339fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
6349fe2a701SSrivatsa Vaddagiri 
6353b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM
6363b85e4abSHeiko Carstens 
6371a087c6aSAlessandro Rubini /*
6381a087c6aSAlessandro Rubini  * The regset32 stuff is used to print 32-bit registers using the
6391a087c6aSAlessandro Rubini  * seq_file utilities. We offer printing a register set in an already-opened
6401a087c6aSAlessandro Rubini  * sequential file or create a debugfs file that only prints a regset32.
6411a087c6aSAlessandro Rubini  */
6421a087c6aSAlessandro Rubini 
6431a087c6aSAlessandro Rubini /**
6441a087c6aSAlessandro Rubini  * debugfs_print_regs32 - use seq_print to describe a set of registers
6451a087c6aSAlessandro Rubini  * @s: the seq_file structure being used to generate output
6461a087c6aSAlessandro Rubini  * @regs: an array if struct debugfs_reg32 structures
647b5763accSRandy Dunlap  * @nregs: the length of the above array
6481a087c6aSAlessandro Rubini  * @base: the base address to be used in reading the registers
6491a087c6aSAlessandro Rubini  * @prefix: a string to be prefixed to every output line
6501a087c6aSAlessandro Rubini  *
6511a087c6aSAlessandro Rubini  * This function outputs a text block describing the current values of
6521a087c6aSAlessandro Rubini  * some 32-bit hardware registers. It is meant to be used within debugfs
6531a087c6aSAlessandro Rubini  * files based on seq_file that need to show registers, intermixed with other
6541a087c6aSAlessandro Rubini  * information. The prefix argument may be used to specify a leading string,
6551a087c6aSAlessandro Rubini  * because some peripherals have several blocks of identical registers,
6561a087c6aSAlessandro Rubini  * for example configuration of dma channels
6571a087c6aSAlessandro Rubini  */
6589761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
6591a087c6aSAlessandro Rubini 			  int nregs, void __iomem *base, char *prefix)
6601a087c6aSAlessandro Rubini {
6619761536eSJoe Perches 	int i;
6621a087c6aSAlessandro Rubini 
6631a087c6aSAlessandro Rubini 	for (i = 0; i < nregs; i++, regs++) {
6641a087c6aSAlessandro Rubini 		if (prefix)
6659761536eSJoe Perches 			seq_printf(s, "%s", prefix);
6669761536eSJoe Perches 		seq_printf(s, "%s = 0x%08x\n", regs->name,
6675858441cSDan Carpenter 			   readl(base + regs->offset));
6689761536eSJoe Perches 		if (seq_has_overflowed(s))
6699761536eSJoe Perches 			break;
6701a087c6aSAlessandro Rubini 	}
6711a087c6aSAlessandro Rubini }
6721a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32);
6731a087c6aSAlessandro Rubini 
6741a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data)
6751a087c6aSAlessandro Rubini {
6761a087c6aSAlessandro Rubini 	struct debugfs_regset32 *regset = s->private;
6771a087c6aSAlessandro Rubini 
6781a087c6aSAlessandro Rubini 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
6791a087c6aSAlessandro Rubini 	return 0;
6801a087c6aSAlessandro Rubini }
6811a087c6aSAlessandro Rubini 
6821a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file)
6831a087c6aSAlessandro Rubini {
6841a087c6aSAlessandro Rubini 	return single_open(file, debugfs_show_regset32, inode->i_private);
6851a087c6aSAlessandro Rubini }
6861a087c6aSAlessandro Rubini 
6871a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = {
6881a087c6aSAlessandro Rubini 	.open =		debugfs_open_regset32,
6891a087c6aSAlessandro Rubini 	.read =		seq_read,
6901a087c6aSAlessandro Rubini 	.llseek =	seq_lseek,
6911a087c6aSAlessandro Rubini 	.release =	single_release,
6921a087c6aSAlessandro Rubini };
6931a087c6aSAlessandro Rubini 
6941a087c6aSAlessandro Rubini /**
6951a087c6aSAlessandro Rubini  * debugfs_create_regset32 - create a debugfs file that returns register values
6961a087c6aSAlessandro Rubini  * @name: a pointer to a string containing the name of the file to create.
6971a087c6aSAlessandro Rubini  * @mode: the permission that the file should have
6981a087c6aSAlessandro Rubini  * @parent: a pointer to the parent dentry for this file.  This should be a
6991a087c6aSAlessandro Rubini  *          directory dentry if set.  If this parameter is %NULL, then the
7001a087c6aSAlessandro Rubini  *          file will be created in the root of the debugfs filesystem.
7011a087c6aSAlessandro Rubini  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
7021a087c6aSAlessandro Rubini  *          to an array of register definitions, the array size and the base
7031a087c6aSAlessandro Rubini  *          address where the register bank is to be found.
7041a087c6aSAlessandro Rubini  *
7051a087c6aSAlessandro Rubini  * This function creates a file in debugfs with the given name that reports
7061a087c6aSAlessandro Rubini  * the names and values of a set of 32-bit registers. If the @mode variable
7071a087c6aSAlessandro Rubini  * is so set it can be read from. Writing is not supported.
7081a087c6aSAlessandro Rubini  *
7091a087c6aSAlessandro Rubini  * This function will return a pointer to a dentry if it succeeds.  This
7101a087c6aSAlessandro Rubini  * pointer must be passed to the debugfs_remove() function when the file is
7111a087c6aSAlessandro Rubini  * to be removed (no automatic cleanup happens if your module is unloaded,
7121a087c6aSAlessandro Rubini  * you are responsible here.)  If an error occurs, %NULL will be returned.
7131a087c6aSAlessandro Rubini  *
7141a087c6aSAlessandro Rubini  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
7151a087c6aSAlessandro Rubini  * returned.  It is not wise to check for this value, but rather, check for
7161a087c6aSAlessandro Rubini  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
7171a087c6aSAlessandro Rubini  * code.
7181a087c6aSAlessandro Rubini  */
71988187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
7201a087c6aSAlessandro Rubini 				       struct dentry *parent,
7211a087c6aSAlessandro Rubini 				       struct debugfs_regset32 *regset)
7221a087c6aSAlessandro Rubini {
7231a087c6aSAlessandro Rubini 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
7241a087c6aSAlessandro Rubini }
7251a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32);
7263b85e4abSHeiko Carstens 
7273b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */
72898210b7fSArend van Spriel 
72998210b7fSArend van Spriel struct debugfs_devm_entry {
73098210b7fSArend van Spriel 	int (*read)(struct seq_file *seq, void *data);
73198210b7fSArend van Spriel 	struct device *dev;
73298210b7fSArend van Spriel };
73398210b7fSArend van Spriel 
73498210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
73598210b7fSArend van Spriel {
73698210b7fSArend van Spriel 	struct debugfs_devm_entry *entry = inode->i_private;
73798210b7fSArend van Spriel 
73898210b7fSArend van Spriel 	return single_open(f, entry->read, entry->dev);
73998210b7fSArend van Spriel }
74098210b7fSArend van Spriel 
74198210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = {
74298210b7fSArend van Spriel 	.owner = THIS_MODULE,
74398210b7fSArend van Spriel 	.open = debugfs_devm_entry_open,
74498210b7fSArend van Spriel 	.release = single_release,
74598210b7fSArend van Spriel 	.read = seq_read,
74698210b7fSArend van Spriel 	.llseek = seq_lseek
74798210b7fSArend van Spriel };
74898210b7fSArend van Spriel 
74998210b7fSArend van Spriel /**
75098210b7fSArend van Spriel  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
75198210b7fSArend van Spriel  *
75298210b7fSArend van Spriel  * @dev: device related to this debugfs file.
75398210b7fSArend van Spriel  * @name: name of the debugfs file.
75498210b7fSArend van Spriel  * @parent: a pointer to the parent dentry for this file.  This should be a
75598210b7fSArend van Spriel  *	directory dentry if set.  If this parameter is %NULL, then the
75698210b7fSArend van Spriel  *	file will be created in the root of the debugfs filesystem.
75798210b7fSArend van Spriel  * @read_fn: function pointer called to print the seq_file content.
75898210b7fSArend van Spriel  */
75998210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
76098210b7fSArend van Spriel 					   struct dentry *parent,
76198210b7fSArend van Spriel 					   int (*read_fn)(struct seq_file *s,
76298210b7fSArend van Spriel 							  void *data))
76398210b7fSArend van Spriel {
76498210b7fSArend van Spriel 	struct debugfs_devm_entry *entry;
76598210b7fSArend van Spriel 
76698210b7fSArend van Spriel 	if (IS_ERR(parent))
76798210b7fSArend van Spriel 		return ERR_PTR(-ENOENT);
76898210b7fSArend van Spriel 
76998210b7fSArend van Spriel 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
77098210b7fSArend van Spriel 	if (!entry)
77198210b7fSArend van Spriel 		return ERR_PTR(-ENOMEM);
77298210b7fSArend van Spriel 
77398210b7fSArend van Spriel 	entry->read = read_fn;
77498210b7fSArend van Spriel 	entry->dev = dev;
77598210b7fSArend van Spriel 
77698210b7fSArend van Spriel 	return debugfs_create_file(name, S_IRUGO, parent, entry,
77798210b7fSArend van Spriel 				   &debugfs_devm_entry_ops);
77898210b7fSArend van Spriel }
77998210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
78098210b7fSArend van Spriel 
781