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> 2066f54963SPeter Oberparleiter #include <linux/namei.h> 211da177e4SLinus Torvalds #include <linux/debugfs.h> 2203e099fbSAlessandro Rubini #include <linux/io.h> 23*9fe2a701SSrivatsa Vaddagiri #include <linux/slab.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 261da177e4SLinus Torvalds size_t count, loff_t *ppos) 271da177e4SLinus Torvalds { 281da177e4SLinus Torvalds return 0; 291da177e4SLinus Torvalds } 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 321da177e4SLinus Torvalds size_t count, loff_t *ppos) 331da177e4SLinus Torvalds { 341da177e4SLinus Torvalds return count; 351da177e4SLinus Torvalds } 361da177e4SLinus Torvalds 374b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = { 381da177e4SLinus Torvalds .read = default_read_file, 391da177e4SLinus Torvalds .write = default_write_file, 40234e3405SStephen Boyd .open = simple_open, 416038f373SArnd Bergmann .llseek = noop_llseek, 421da177e4SLinus Torvalds }; 431da177e4SLinus Torvalds 4466f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) 4566f54963SPeter Oberparleiter { 4666f54963SPeter Oberparleiter nd_set_link(nd, dentry->d_inode->i_private); 4766f54963SPeter Oberparleiter return NULL; 4866f54963SPeter Oberparleiter } 4966f54963SPeter Oberparleiter 5066f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = { 5166f54963SPeter Oberparleiter .readlink = generic_readlink, 5266f54963SPeter Oberparleiter .follow_link = debugfs_follow_link, 5366f54963SPeter Oberparleiter }; 5466f54963SPeter Oberparleiter 558b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 56acaefc25SArnd Bergmann { 57acaefc25SArnd Bergmann *(u8 *)data = val; 588b88b099SChristoph Hellwig return 0; 59acaefc25SArnd Bergmann } 608b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 61acaefc25SArnd Bergmann { 628b88b099SChristoph Hellwig *val = *(u8 *)data; 638b88b099SChristoph Hellwig return 0; 64acaefc25SArnd Bergmann } 65acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 66e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 67e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /** 706468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 711da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 721da177e4SLinus Torvalds * @mode: the permission that the file should have 731da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 746468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 751da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 761da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 771da177e4SLinus Torvalds * from. 781da177e4SLinus Torvalds * 791da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 801da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 811da177e4SLinus Torvalds * set, it can be read from, and written to. 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 841da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 851da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 866468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 871da177e4SLinus Torvalds * 886468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 891da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 906468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 911da177e4SLinus Torvalds * code. 921da177e4SLinus Torvalds */ 93f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode, 941da177e4SLinus Torvalds struct dentry *parent, u8 *value) 951da177e4SLinus Torvalds { 96e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 97e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 98e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); 99e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 100e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 101e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); 102e4792aa3SRobin Getz 1031da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u8); 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 { 148e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 149e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 150e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); 151e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 152e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 153e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); 154e4792aa3SRobin Getz 1551da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u16); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 1581da177e4SLinus Torvalds 1598b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 160acaefc25SArnd Bergmann { 161acaefc25SArnd Bergmann *(u32 *)data = val; 1628b88b099SChristoph Hellwig return 0; 163acaefc25SArnd Bergmann } 1648b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 165acaefc25SArnd Bergmann { 1668b88b099SChristoph Hellwig *val = *(u32 *)data; 1678b88b099SChristoph Hellwig return 0; 168acaefc25SArnd Bergmann } 169acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 170e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 171e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 172acaefc25SArnd Bergmann 1731da177e4SLinus Torvalds /** 1746468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 1751da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1761da177e4SLinus Torvalds * @mode: the permission that the file should have 1771da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1786468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1791da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1801da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1811da177e4SLinus Torvalds * from. 1821da177e4SLinus Torvalds * 1831da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1841da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1851da177e4SLinus Torvalds * set, it can be read from, and written to. 1861da177e4SLinus Torvalds * 1871da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1881da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1891da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1906468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1911da177e4SLinus Torvalds * 1926468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1931da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 1946468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1951da177e4SLinus Torvalds * code. 1961da177e4SLinus Torvalds */ 197f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode, 1981da177e4SLinus Torvalds struct dentry *parent, u32 *value) 1991da177e4SLinus Torvalds { 200e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 201e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 202e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); 203e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 204e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 205e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); 206e4792aa3SRobin Getz 2071da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u32); 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 2101da177e4SLinus Torvalds 2118b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 2128447891fSMichael Ellerman { 2138447891fSMichael Ellerman *(u64 *)data = val; 2148b88b099SChristoph Hellwig return 0; 2158447891fSMichael Ellerman } 2168447891fSMichael Ellerman 2178b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 2188447891fSMichael Ellerman { 2198b88b099SChristoph Hellwig *val = *(u64 *)data; 2208b88b099SChristoph Hellwig return 0; 2218447891fSMichael Ellerman } 2228447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 223e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 224e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 2258447891fSMichael Ellerman 2268447891fSMichael Ellerman /** 2278447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 2288447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 2298447891fSMichael Ellerman * @mode: the permission that the file should have 2308447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 2318447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 2328447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 2338447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 2348447891fSMichael Ellerman * from. 2358447891fSMichael Ellerman * 2368447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 2378447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 2388447891fSMichael Ellerman * set, it can be read from, and written to. 2398447891fSMichael Ellerman * 2408447891fSMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 2418447891fSMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 2428447891fSMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 2438447891fSMichael Ellerman * you are responsible here.) If an error occurs, %NULL will be returned. 2448447891fSMichael Ellerman * 2458447891fSMichael Ellerman * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2468447891fSMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 2478447891fSMichael Ellerman * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2488447891fSMichael Ellerman * code. 2498447891fSMichael Ellerman */ 250f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode, 2518447891fSMichael Ellerman struct dentry *parent, u64 *value) 2528447891fSMichael Ellerman { 253e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 254e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 255e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); 256e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 257e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 258e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); 259e4792aa3SRobin Getz 2608447891fSMichael Ellerman return debugfs_create_file(name, mode, parent, value, &fops_u64); 2618447891fSMichael Ellerman } 2628447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 2638447891fSMichael Ellerman 2642ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 265e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 266e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 2672ebefc50SRobin Getz 2682ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); 269e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 270e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 2712ebefc50SRobin Getz 2722ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); 273e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 274e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 2752ebefc50SRobin Getz 27615b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 27715b0beaaSHuang Ying 278e6716b87SRandy Dunlap /* 27915b0beaaSHuang 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 280e6716b87SRandy Dunlap * 281e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 282e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 283e6716b87SRandy Dunlap * decimal functions. 284e6716b87SRandy Dunlap */ 285e6716b87SRandy Dunlap 2862ebefc50SRobin Getz /** 2872ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 288e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 289e6716b87SRandy Dunlap * @mode: the permission that the file should have 290e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 291e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 292e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 293e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 294e6716b87SRandy Dunlap * from. 2952ebefc50SRobin Getz */ 296f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode, 2972ebefc50SRobin Getz struct dentry *parent, u8 *value) 2982ebefc50SRobin Getz { 299e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 300e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 301e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); 302e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 303e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 304e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); 305e4792aa3SRobin Getz 3062ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8); 3072ebefc50SRobin Getz } 3082ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 3092ebefc50SRobin Getz 310e6716b87SRandy Dunlap /** 311e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 312e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 313e6716b87SRandy Dunlap * @mode: the permission that the file should have 314e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 315e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 316e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 317e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 318e6716b87SRandy Dunlap * from. 319e6716b87SRandy Dunlap */ 320f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode, 3212ebefc50SRobin Getz struct dentry *parent, u16 *value) 3222ebefc50SRobin Getz { 323e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 324e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 325e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); 326e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 327e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 328e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); 329e4792aa3SRobin Getz 3302ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16); 3312ebefc50SRobin Getz } 3322ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 3332ebefc50SRobin Getz 334e6716b87SRandy Dunlap /** 335e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 336e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 337e6716b87SRandy Dunlap * @mode: the permission that the file should have 338e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 339e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 340e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 341e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 342e6716b87SRandy Dunlap * from. 343e6716b87SRandy Dunlap */ 344f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode, 3452ebefc50SRobin Getz struct dentry *parent, u32 *value) 3462ebefc50SRobin Getz { 347e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 348e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 349e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); 350e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 351e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 352e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); 353e4792aa3SRobin Getz 3542ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32); 3552ebefc50SRobin Getz } 3562ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 3572ebefc50SRobin Getz 35815b0beaaSHuang Ying /** 35915b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 36015b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 36115b0beaaSHuang Ying * @mode: the permission that the file should have 36215b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 36315b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 36415b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 36515b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 36615b0beaaSHuang Ying * from. 36715b0beaaSHuang Ying */ 368f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode, 36915b0beaaSHuang Ying struct dentry *parent, u64 *value) 37015b0beaaSHuang Ying { 37115b0beaaSHuang Ying return debugfs_create_file(name, mode, parent, value, &fops_x64); 37215b0beaaSHuang Ying } 37315b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 37415b0beaaSHuang Ying 3755e078787SInaky Perez-Gonzalez 3765e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 3775e078787SInaky Perez-Gonzalez { 3785e078787SInaky Perez-Gonzalez *(size_t *)data = val; 3795e078787SInaky Perez-Gonzalez return 0; 3805e078787SInaky Perez-Gonzalez } 3815e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 3825e078787SInaky Perez-Gonzalez { 3835e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 3845e078787SInaky Perez-Gonzalez return 0; 3855e078787SInaky Perez-Gonzalez } 3865e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 3875e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 3885e078787SInaky Perez-Gonzalez 3895e078787SInaky Perez-Gonzalez /** 3905e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 3915e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 3925e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 3935e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 3945e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 3955e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 3965e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 3975e078787SInaky Perez-Gonzalez * from. 3985e078787SInaky Perez-Gonzalez */ 399f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode, 4005e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 4015e078787SInaky Perez-Gonzalez { 4025e078787SInaky Perez-Gonzalez return debugfs_create_file(name, mode, parent, value, &fops_size_t); 4035e078787SInaky Perez-Gonzalez } 4045e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 4055e078787SInaky Perez-Gonzalez 4065e078787SInaky Perez-Gonzalez 4071da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf, 4081da177e4SLinus Torvalds size_t count, loff_t *ppos) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds char buf[3]; 4111da177e4SLinus Torvalds u32 *val = file->private_data; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds if (*val) 4141da177e4SLinus Torvalds buf[0] = 'Y'; 4151da177e4SLinus Torvalds else 4161da177e4SLinus Torvalds buf[0] = 'N'; 4171da177e4SLinus Torvalds buf[1] = '\n'; 4181da177e4SLinus Torvalds buf[2] = 0x00; 4191da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf, 4231da177e4SLinus Torvalds size_t count, loff_t *ppos) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds char buf[32]; 426c42d2237SStephen Boyd size_t buf_size; 4278705b48eSJonathan Cameron bool bv; 4281da177e4SLinus Torvalds u32 *val = file->private_data; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 4311da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 4321da177e4SLinus Torvalds return -EFAULT; 4331da177e4SLinus Torvalds 4348705b48eSJonathan Cameron if (strtobool(buf, &bv) == 0) 4358705b48eSJonathan Cameron *val = bv; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds return count; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4404b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 4411da177e4SLinus Torvalds .read = read_file_bool, 4421da177e4SLinus Torvalds .write = write_file_bool, 443234e3405SStephen Boyd .open = simple_open, 4446038f373SArnd Bergmann .llseek = default_llseek, 4451da177e4SLinus Torvalds }; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /** 4486468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 4491da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4501da177e4SLinus Torvalds * @mode: the permission that the file should have 4511da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4526468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4531da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4541da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4551da177e4SLinus Torvalds * from. 4561da177e4SLinus Torvalds * 4571da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4581da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4591da177e4SLinus Torvalds * set, it can be read from, and written to. 4601da177e4SLinus Torvalds * 4611da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4621da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4631da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4646468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4651da177e4SLinus Torvalds * 4666468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 4671da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 4686468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 4691da177e4SLinus Torvalds * code. 4701da177e4SLinus Torvalds */ 471f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode, 4721da177e4SLinus Torvalds struct dentry *parent, u32 *value) 4731da177e4SLinus Torvalds { 4741da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_bool); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 4771da177e4SLinus Torvalds 478dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 479dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 480dd308bc3SMichael Ellerman { 481dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 482dd308bc3SMichael Ellerman return simple_read_from_buffer(user_buf, count, ppos, blob->data, 483dd308bc3SMichael Ellerman blob->size); 484dd308bc3SMichael Ellerman } 485dd308bc3SMichael Ellerman 48600977a59SArjan van de Ven static const struct file_operations fops_blob = { 487dd308bc3SMichael Ellerman .read = read_file_blob, 488234e3405SStephen Boyd .open = simple_open, 4896038f373SArnd Bergmann .llseek = default_llseek, 490dd308bc3SMichael Ellerman }; 491dd308bc3SMichael Ellerman 492dd308bc3SMichael Ellerman /** 493400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 494dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 495dd308bc3SMichael Ellerman * @mode: the permission that the file should have 496dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 4976468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 498dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 499dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 500dd308bc3SMichael Ellerman * to the blob data and the size of the data. 501dd308bc3SMichael Ellerman * 502dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 503dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 504dd308bc3SMichael Ellerman * read from. Writing is not supported. 505dd308bc3SMichael Ellerman * 506dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 507dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 508dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 5096468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 510dd308bc3SMichael Ellerman * 5116468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 512dd308bc3SMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 5136468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 514dd308bc3SMichael Ellerman * code. 515dd308bc3SMichael Ellerman */ 516f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 517dd308bc3SMichael Ellerman struct dentry *parent, 518dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 519dd308bc3SMichael Ellerman { 520dd308bc3SMichael Ellerman return debugfs_create_file(name, mode, parent, blob, &fops_blob); 521dd308bc3SMichael Ellerman } 522dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 5231a087c6aSAlessandro Rubini 524*9fe2a701SSrivatsa Vaddagiri struct array_data { 525*9fe2a701SSrivatsa Vaddagiri void *array; 526*9fe2a701SSrivatsa Vaddagiri u32 elements; 527*9fe2a701SSrivatsa Vaddagiri }; 528*9fe2a701SSrivatsa Vaddagiri 529*9fe2a701SSrivatsa Vaddagiri static int u32_array_open(struct inode *inode, struct file *file) 530*9fe2a701SSrivatsa Vaddagiri { 531*9fe2a701SSrivatsa Vaddagiri file->private_data = NULL; 532*9fe2a701SSrivatsa Vaddagiri return nonseekable_open(inode, file); 533*9fe2a701SSrivatsa Vaddagiri } 534*9fe2a701SSrivatsa Vaddagiri 535*9fe2a701SSrivatsa Vaddagiri static size_t format_array(char *buf, size_t bufsize, const char *fmt, 536*9fe2a701SSrivatsa Vaddagiri u32 *array, u32 array_size) 537*9fe2a701SSrivatsa Vaddagiri { 538*9fe2a701SSrivatsa Vaddagiri size_t ret = 0; 539*9fe2a701SSrivatsa Vaddagiri u32 i; 540*9fe2a701SSrivatsa Vaddagiri 541*9fe2a701SSrivatsa Vaddagiri for (i = 0; i < array_size; i++) { 542*9fe2a701SSrivatsa Vaddagiri size_t len; 543*9fe2a701SSrivatsa Vaddagiri 544*9fe2a701SSrivatsa Vaddagiri len = snprintf(buf, bufsize, fmt, array[i]); 545*9fe2a701SSrivatsa Vaddagiri len++; /* ' ' or '\n' */ 546*9fe2a701SSrivatsa Vaddagiri ret += len; 547*9fe2a701SSrivatsa Vaddagiri 548*9fe2a701SSrivatsa Vaddagiri if (buf) { 549*9fe2a701SSrivatsa Vaddagiri buf += len; 550*9fe2a701SSrivatsa Vaddagiri bufsize -= len; 551*9fe2a701SSrivatsa Vaddagiri buf[-1] = (i == array_size-1) ? '\n' : ' '; 552*9fe2a701SSrivatsa Vaddagiri } 553*9fe2a701SSrivatsa Vaddagiri } 554*9fe2a701SSrivatsa Vaddagiri 555*9fe2a701SSrivatsa Vaddagiri ret++; /* \0 */ 556*9fe2a701SSrivatsa Vaddagiri if (buf) 557*9fe2a701SSrivatsa Vaddagiri *buf = '\0'; 558*9fe2a701SSrivatsa Vaddagiri 559*9fe2a701SSrivatsa Vaddagiri return ret; 560*9fe2a701SSrivatsa Vaddagiri } 561*9fe2a701SSrivatsa Vaddagiri 562*9fe2a701SSrivatsa Vaddagiri static char *format_array_alloc(const char *fmt, u32 *array, 563*9fe2a701SSrivatsa Vaddagiri u32 array_size) 564*9fe2a701SSrivatsa Vaddagiri { 565*9fe2a701SSrivatsa Vaddagiri size_t len = format_array(NULL, 0, fmt, array, array_size); 566*9fe2a701SSrivatsa Vaddagiri char *ret; 567*9fe2a701SSrivatsa Vaddagiri 568*9fe2a701SSrivatsa Vaddagiri ret = kmalloc(len, GFP_KERNEL); 569*9fe2a701SSrivatsa Vaddagiri if (ret == NULL) 570*9fe2a701SSrivatsa Vaddagiri return NULL; 571*9fe2a701SSrivatsa Vaddagiri 572*9fe2a701SSrivatsa Vaddagiri format_array(ret, len, fmt, array, array_size); 573*9fe2a701SSrivatsa Vaddagiri return ret; 574*9fe2a701SSrivatsa Vaddagiri } 575*9fe2a701SSrivatsa Vaddagiri 576*9fe2a701SSrivatsa Vaddagiri static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 577*9fe2a701SSrivatsa Vaddagiri loff_t *ppos) 578*9fe2a701SSrivatsa Vaddagiri { 579*9fe2a701SSrivatsa Vaddagiri struct inode *inode = file->f_path.dentry->d_inode; 580*9fe2a701SSrivatsa Vaddagiri struct array_data *data = inode->i_private; 581*9fe2a701SSrivatsa Vaddagiri size_t size; 582*9fe2a701SSrivatsa Vaddagiri 583*9fe2a701SSrivatsa Vaddagiri if (*ppos == 0) { 584*9fe2a701SSrivatsa Vaddagiri if (file->private_data) { 585*9fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 586*9fe2a701SSrivatsa Vaddagiri file->private_data = NULL; 587*9fe2a701SSrivatsa Vaddagiri } 588*9fe2a701SSrivatsa Vaddagiri 589*9fe2a701SSrivatsa Vaddagiri file->private_data = format_array_alloc("%u", data->array, 590*9fe2a701SSrivatsa Vaddagiri data->elements); 591*9fe2a701SSrivatsa Vaddagiri } 592*9fe2a701SSrivatsa Vaddagiri 593*9fe2a701SSrivatsa Vaddagiri size = 0; 594*9fe2a701SSrivatsa Vaddagiri if (file->private_data) 595*9fe2a701SSrivatsa Vaddagiri size = strlen(file->private_data); 596*9fe2a701SSrivatsa Vaddagiri 597*9fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 598*9fe2a701SSrivatsa Vaddagiri file->private_data, size); 599*9fe2a701SSrivatsa Vaddagiri } 600*9fe2a701SSrivatsa Vaddagiri 601*9fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 602*9fe2a701SSrivatsa Vaddagiri { 603*9fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 604*9fe2a701SSrivatsa Vaddagiri 605*9fe2a701SSrivatsa Vaddagiri return 0; 606*9fe2a701SSrivatsa Vaddagiri } 607*9fe2a701SSrivatsa Vaddagiri 608*9fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 609*9fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 610*9fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 611*9fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 612*9fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 613*9fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 614*9fe2a701SSrivatsa Vaddagiri }; 615*9fe2a701SSrivatsa Vaddagiri 616*9fe2a701SSrivatsa Vaddagiri /** 617*9fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 618*9fe2a701SSrivatsa Vaddagiri * array. 619*9fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 620*9fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 621*9fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 622*9fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 623*9fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 624*9fe2a701SSrivatsa Vaddagiri * @array: u32 array that provides data. 625*9fe2a701SSrivatsa Vaddagiri * @elements: total number of elements in the array. 626*9fe2a701SSrivatsa Vaddagiri * 627*9fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 628*9fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 629*9fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 630*9fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 631*9fe2a701SSrivatsa Vaddagiri * 632*9fe2a701SSrivatsa Vaddagiri * The function returns a pointer to dentry on success. If debugfs is not 633*9fe2a701SSrivatsa Vaddagiri * enabled in the kernel, the value -%ENODEV will be returned. 634*9fe2a701SSrivatsa Vaddagiri */ 635*9fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, 636*9fe2a701SSrivatsa Vaddagiri struct dentry *parent, 637*9fe2a701SSrivatsa Vaddagiri u32 *array, u32 elements) 638*9fe2a701SSrivatsa Vaddagiri { 639*9fe2a701SSrivatsa Vaddagiri struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); 640*9fe2a701SSrivatsa Vaddagiri 641*9fe2a701SSrivatsa Vaddagiri if (data == NULL) 642*9fe2a701SSrivatsa Vaddagiri return NULL; 643*9fe2a701SSrivatsa Vaddagiri 644*9fe2a701SSrivatsa Vaddagiri data->array = array; 645*9fe2a701SSrivatsa Vaddagiri data->elements = elements; 646*9fe2a701SSrivatsa Vaddagiri 647*9fe2a701SSrivatsa Vaddagiri return debugfs_create_file(name, mode, parent, data, &u32_array_fops); 648*9fe2a701SSrivatsa Vaddagiri } 649*9fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 650*9fe2a701SSrivatsa Vaddagiri 6513b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 6523b85e4abSHeiko Carstens 6531a087c6aSAlessandro Rubini /* 6541a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 6551a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 6561a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 6571a087c6aSAlessandro Rubini */ 6581a087c6aSAlessandro Rubini 6591a087c6aSAlessandro Rubini /** 6601a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 6611a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 6621a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 663b5763accSRandy Dunlap * @nregs: the length of the above array 6641a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 6651a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 6661a087c6aSAlessandro Rubini * 6671a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 6681a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 6691a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 6701a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 6711a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 6721a087c6aSAlessandro Rubini * for example configuration of dma channels 6731a087c6aSAlessandro Rubini */ 6748ee4dd9fSAlessandro Rubini int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 6751a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 6761a087c6aSAlessandro Rubini { 6771a087c6aSAlessandro Rubini int i, ret = 0; 6781a087c6aSAlessandro Rubini 6791a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 6801a087c6aSAlessandro Rubini if (prefix) 6811a087c6aSAlessandro Rubini ret += seq_printf(s, "%s", prefix); 6821a087c6aSAlessandro Rubini ret += seq_printf(s, "%s = 0x%08x\n", regs->name, 6835858441cSDan Carpenter readl(base + regs->offset)); 6841a087c6aSAlessandro Rubini } 6851a087c6aSAlessandro Rubini return ret; 6861a087c6aSAlessandro Rubini } 6871a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 6881a087c6aSAlessandro Rubini 6891a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 6901a087c6aSAlessandro Rubini { 6911a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 6921a087c6aSAlessandro Rubini 6931a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 6941a087c6aSAlessandro Rubini return 0; 6951a087c6aSAlessandro Rubini } 6961a087c6aSAlessandro Rubini 6971a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 6981a087c6aSAlessandro Rubini { 6991a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 7001a087c6aSAlessandro Rubini } 7011a087c6aSAlessandro Rubini 7021a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 7031a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 7041a087c6aSAlessandro Rubini .read = seq_read, 7051a087c6aSAlessandro Rubini .llseek = seq_lseek, 7061a087c6aSAlessandro Rubini .release = single_release, 7071a087c6aSAlessandro Rubini }; 7081a087c6aSAlessandro Rubini 7091a087c6aSAlessandro Rubini /** 7101a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 7111a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 7121a087c6aSAlessandro Rubini * @mode: the permission that the file should have 7131a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 7141a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 7151a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 7161a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 7171a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 7181a087c6aSAlessandro Rubini * address where the register bank is to be found. 7191a087c6aSAlessandro Rubini * 7201a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 7211a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 7221a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 7231a087c6aSAlessandro Rubini * 7241a087c6aSAlessandro Rubini * This function will return a pointer to a dentry if it succeeds. This 7251a087c6aSAlessandro Rubini * pointer must be passed to the debugfs_remove() function when the file is 7261a087c6aSAlessandro Rubini * to be removed (no automatic cleanup happens if your module is unloaded, 7271a087c6aSAlessandro Rubini * you are responsible here.) If an error occurs, %NULL will be returned. 7281a087c6aSAlessandro Rubini * 7291a087c6aSAlessandro Rubini * If debugfs is not enabled in the kernel, the value -%ENODEV will be 7301a087c6aSAlessandro Rubini * returned. It is not wise to check for this value, but rather, check for 7311a087c6aSAlessandro Rubini * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 7321a087c6aSAlessandro Rubini * code. 7331a087c6aSAlessandro Rubini */ 73488187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode, 7351a087c6aSAlessandro Rubini struct dentry *parent, 7361a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 7371a087c6aSAlessandro Rubini { 7381a087c6aSAlessandro Rubini return debugfs_create_file(name, mode, parent, regset, &fops_regset32); 7391a087c6aSAlessandro Rubini } 7401a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 7413b85e4abSHeiko Carstens 7423b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 743