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> 22*03e099fbSAlessandro Rubini #include <linux/io.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 251da177e4SLinus Torvalds size_t count, loff_t *ppos) 261da177e4SLinus Torvalds { 271da177e4SLinus Torvalds return 0; 281da177e4SLinus Torvalds } 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 311da177e4SLinus Torvalds size_t count, loff_t *ppos) 321da177e4SLinus Torvalds { 331da177e4SLinus Torvalds return count; 341da177e4SLinus Torvalds } 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file) 371da177e4SLinus Torvalds { 388e18e294STheodore Ts'o if (inode->i_private) 398e18e294STheodore Ts'o file->private_data = inode->i_private; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds return 0; 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 444b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = { 451da177e4SLinus Torvalds .read = default_read_file, 461da177e4SLinus Torvalds .write = default_write_file, 471da177e4SLinus Torvalds .open = default_open, 486038f373SArnd Bergmann .llseek = noop_llseek, 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 5166f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) 5266f54963SPeter Oberparleiter { 5366f54963SPeter Oberparleiter nd_set_link(nd, dentry->d_inode->i_private); 5466f54963SPeter Oberparleiter return NULL; 5566f54963SPeter Oberparleiter } 5666f54963SPeter Oberparleiter 5766f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = { 5866f54963SPeter Oberparleiter .readlink = generic_readlink, 5966f54963SPeter Oberparleiter .follow_link = debugfs_follow_link, 6066f54963SPeter Oberparleiter }; 6166f54963SPeter Oberparleiter 628b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 63acaefc25SArnd Bergmann { 64acaefc25SArnd Bergmann *(u8 *)data = val; 658b88b099SChristoph Hellwig return 0; 66acaefc25SArnd Bergmann } 678b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 68acaefc25SArnd Bergmann { 698b88b099SChristoph Hellwig *val = *(u8 *)data; 708b88b099SChristoph Hellwig return 0; 71acaefc25SArnd Bergmann } 72acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 73e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 74e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /** 776468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 781da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 791da177e4SLinus Torvalds * @mode: the permission that the file should have 801da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 816468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 821da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 831da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 841da177e4SLinus Torvalds * from. 851da177e4SLinus Torvalds * 861da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 871da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 881da177e4SLinus Torvalds * set, it can be read from, and written to. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 911da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 921da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 936468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 941da177e4SLinus Torvalds * 956468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 961da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 976468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 981da177e4SLinus Torvalds * code. 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode, 1011da177e4SLinus Torvalds struct dentry *parent, u8 *value) 1021da177e4SLinus Torvalds { 103e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 104e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 105e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); 106e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 107e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 108e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); 109e4792aa3SRobin Getz 1101da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u8); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 1131da177e4SLinus Torvalds 1148b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 115acaefc25SArnd Bergmann { 116acaefc25SArnd Bergmann *(u16 *)data = val; 1178b88b099SChristoph Hellwig return 0; 118acaefc25SArnd Bergmann } 1198b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 120acaefc25SArnd Bergmann { 1218b88b099SChristoph Hellwig *val = *(u16 *)data; 1228b88b099SChristoph Hellwig return 0; 123acaefc25SArnd Bergmann } 124acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 125e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 126e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 127acaefc25SArnd Bergmann 1281da177e4SLinus Torvalds /** 1296468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 1301da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1311da177e4SLinus Torvalds * @mode: the permission that the file should have 1321da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1336468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1341da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1351da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1361da177e4SLinus Torvalds * from. 1371da177e4SLinus Torvalds * 1381da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1391da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1401da177e4SLinus Torvalds * set, it can be read from, and written to. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1431da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1441da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1456468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1461da177e4SLinus Torvalds * 1476468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1481da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 1496468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1501da177e4SLinus Torvalds * code. 1511da177e4SLinus Torvalds */ 1521da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode, 1531da177e4SLinus Torvalds struct dentry *parent, u16 *value) 1541da177e4SLinus Torvalds { 155e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 156e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 157e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); 158e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 159e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 160e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); 161e4792aa3SRobin Getz 1621da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u16); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 1651da177e4SLinus Torvalds 1668b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 167acaefc25SArnd Bergmann { 168acaefc25SArnd Bergmann *(u32 *)data = val; 1698b88b099SChristoph Hellwig return 0; 170acaefc25SArnd Bergmann } 1718b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 172acaefc25SArnd Bergmann { 1738b88b099SChristoph Hellwig *val = *(u32 *)data; 1748b88b099SChristoph Hellwig return 0; 175acaefc25SArnd Bergmann } 176acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 177e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 178e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 179acaefc25SArnd Bergmann 1801da177e4SLinus Torvalds /** 1816468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 1821da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1831da177e4SLinus Torvalds * @mode: the permission that the file should have 1841da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1856468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1861da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1871da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1881da177e4SLinus Torvalds * from. 1891da177e4SLinus Torvalds * 1901da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1911da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1921da177e4SLinus Torvalds * set, it can be read from, and written to. 1931da177e4SLinus Torvalds * 1941da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1951da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1961da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1976468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1981da177e4SLinus Torvalds * 1996468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2001da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 2016468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2021da177e4SLinus Torvalds * code. 2031da177e4SLinus Torvalds */ 2041da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode, 2051da177e4SLinus Torvalds struct dentry *parent, u32 *value) 2061da177e4SLinus Torvalds { 207e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 208e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 209e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); 210e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 211e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 212e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); 213e4792aa3SRobin Getz 2141da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u32); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 2171da177e4SLinus Torvalds 2188b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 2198447891fSMichael Ellerman { 2208447891fSMichael Ellerman *(u64 *)data = val; 2218b88b099SChristoph Hellwig return 0; 2228447891fSMichael Ellerman } 2238447891fSMichael Ellerman 2248b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 2258447891fSMichael Ellerman { 2268b88b099SChristoph Hellwig *val = *(u64 *)data; 2278b88b099SChristoph Hellwig return 0; 2288447891fSMichael Ellerman } 2298447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 230e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 231e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 2328447891fSMichael Ellerman 2338447891fSMichael Ellerman /** 2348447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 2358447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 2368447891fSMichael Ellerman * @mode: the permission that the file should have 2378447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 2388447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 2398447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 2408447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 2418447891fSMichael Ellerman * from. 2428447891fSMichael Ellerman * 2438447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 2448447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 2458447891fSMichael Ellerman * set, it can be read from, and written to. 2468447891fSMichael Ellerman * 2478447891fSMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 2488447891fSMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 2498447891fSMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 2508447891fSMichael Ellerman * you are responsible here.) If an error occurs, %NULL will be returned. 2518447891fSMichael Ellerman * 2528447891fSMichael Ellerman * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2538447891fSMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 2548447891fSMichael Ellerman * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2558447891fSMichael Ellerman * code. 2568447891fSMichael Ellerman */ 2578447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode, 2588447891fSMichael Ellerman struct dentry *parent, u64 *value) 2598447891fSMichael Ellerman { 260e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 261e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 262e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); 263e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 264e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 265e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); 266e4792aa3SRobin Getz 2678447891fSMichael Ellerman return debugfs_create_file(name, mode, parent, value, &fops_u64); 2688447891fSMichael Ellerman } 2698447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 2708447891fSMichael Ellerman 2712ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 272e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 273e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 2742ebefc50SRobin Getz 2752ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); 276e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 277e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 2782ebefc50SRobin Getz 2792ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); 280e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 281e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 2822ebefc50SRobin Getz 28315b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 28415b0beaaSHuang Ying 285e6716b87SRandy Dunlap /* 28615b0beaaSHuang 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 287e6716b87SRandy Dunlap * 288e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 289e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 290e6716b87SRandy Dunlap * decimal functions. 291e6716b87SRandy Dunlap */ 292e6716b87SRandy Dunlap 2932ebefc50SRobin Getz /** 2942ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 295e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 296e6716b87SRandy Dunlap * @mode: the permission that the file should have 297e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 298e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 299e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 300e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 301e6716b87SRandy Dunlap * from. 3022ebefc50SRobin Getz */ 3032ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode, 3042ebefc50SRobin Getz struct dentry *parent, u8 *value) 3052ebefc50SRobin Getz { 306e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 307e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 308e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); 309e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 310e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 311e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); 312e4792aa3SRobin Getz 3132ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8); 3142ebefc50SRobin Getz } 3152ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 3162ebefc50SRobin Getz 317e6716b87SRandy Dunlap /** 318e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 319e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 320e6716b87SRandy Dunlap * @mode: the permission that the file should have 321e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 322e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 323e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 324e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 325e6716b87SRandy Dunlap * from. 326e6716b87SRandy Dunlap */ 3272ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode, 3282ebefc50SRobin Getz struct dentry *parent, u16 *value) 3292ebefc50SRobin Getz { 330e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 331e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 332e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); 333e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 334e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 335e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); 336e4792aa3SRobin Getz 3372ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16); 3382ebefc50SRobin Getz } 3392ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 3402ebefc50SRobin Getz 341e6716b87SRandy Dunlap /** 342e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 343e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 344e6716b87SRandy Dunlap * @mode: the permission that the file should have 345e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 346e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 347e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 348e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 349e6716b87SRandy Dunlap * from. 350e6716b87SRandy Dunlap */ 3512ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode, 3522ebefc50SRobin Getz struct dentry *parent, u32 *value) 3532ebefc50SRobin Getz { 354e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 355e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 356e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); 357e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 358e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 359e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); 360e4792aa3SRobin Getz 3612ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32); 3622ebefc50SRobin Getz } 3632ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 3642ebefc50SRobin Getz 36515b0beaaSHuang Ying /** 36615b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 36715b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 36815b0beaaSHuang Ying * @mode: the permission that the file should have 36915b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 37015b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 37115b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 37215b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 37315b0beaaSHuang Ying * from. 37415b0beaaSHuang Ying */ 37515b0beaaSHuang Ying struct dentry *debugfs_create_x64(const char *name, mode_t mode, 37615b0beaaSHuang Ying struct dentry *parent, u64 *value) 37715b0beaaSHuang Ying { 37815b0beaaSHuang Ying return debugfs_create_file(name, mode, parent, value, &fops_x64); 37915b0beaaSHuang Ying } 38015b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 38115b0beaaSHuang Ying 3825e078787SInaky Perez-Gonzalez 3835e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 3845e078787SInaky Perez-Gonzalez { 3855e078787SInaky Perez-Gonzalez *(size_t *)data = val; 3865e078787SInaky Perez-Gonzalez return 0; 3875e078787SInaky Perez-Gonzalez } 3885e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 3895e078787SInaky Perez-Gonzalez { 3905e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 3915e078787SInaky Perez-Gonzalez return 0; 3925e078787SInaky Perez-Gonzalez } 3935e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 3945e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 3955e078787SInaky Perez-Gonzalez 3965e078787SInaky Perez-Gonzalez /** 3975e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 3985e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 3995e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 4005e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 4015e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 4025e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 4035e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 4045e078787SInaky Perez-Gonzalez * from. 4055e078787SInaky Perez-Gonzalez */ 4065e078787SInaky Perez-Gonzalez struct dentry *debugfs_create_size_t(const char *name, mode_t mode, 4075e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 4085e078787SInaky Perez-Gonzalez { 4095e078787SInaky Perez-Gonzalez return debugfs_create_file(name, mode, parent, value, &fops_size_t); 4105e078787SInaky Perez-Gonzalez } 4115e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 4125e078787SInaky Perez-Gonzalez 4135e078787SInaky Perez-Gonzalez 4141da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf, 4151da177e4SLinus Torvalds size_t count, loff_t *ppos) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds char buf[3]; 4181da177e4SLinus Torvalds u32 *val = file->private_data; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds if (*val) 4211da177e4SLinus Torvalds buf[0] = 'Y'; 4221da177e4SLinus Torvalds else 4231da177e4SLinus Torvalds buf[0] = 'N'; 4241da177e4SLinus Torvalds buf[1] = '\n'; 4251da177e4SLinus Torvalds buf[2] = 0x00; 4261da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf, 4301da177e4SLinus Torvalds size_t count, loff_t *ppos) 4311da177e4SLinus Torvalds { 4321da177e4SLinus Torvalds char buf[32]; 433c42d2237SStephen Boyd size_t buf_size; 4348705b48eSJonathan Cameron bool bv; 4351da177e4SLinus Torvalds u32 *val = file->private_data; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 4381da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 4391da177e4SLinus Torvalds return -EFAULT; 4401da177e4SLinus Torvalds 4418705b48eSJonathan Cameron if (strtobool(buf, &bv) == 0) 4428705b48eSJonathan Cameron *val = bv; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds return count; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4474b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 4481da177e4SLinus Torvalds .read = read_file_bool, 4491da177e4SLinus Torvalds .write = write_file_bool, 4501da177e4SLinus Torvalds .open = default_open, 4516038f373SArnd Bergmann .llseek = default_llseek, 4521da177e4SLinus Torvalds }; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /** 4556468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 4561da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4571da177e4SLinus Torvalds * @mode: the permission that the file should have 4581da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4596468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4601da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4611da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4621da177e4SLinus Torvalds * from. 4631da177e4SLinus Torvalds * 4641da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4651da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4661da177e4SLinus Torvalds * set, it can be read from, and written to. 4671da177e4SLinus Torvalds * 4681da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4691da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4701da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4716468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4721da177e4SLinus Torvalds * 4736468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 4741da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 4756468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 4761da177e4SLinus Torvalds * code. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode, 4791da177e4SLinus Torvalds struct dentry *parent, u32 *value) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_bool); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 4841da177e4SLinus Torvalds 485dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 486dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 487dd308bc3SMichael Ellerman { 488dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 489dd308bc3SMichael Ellerman return simple_read_from_buffer(user_buf, count, ppos, blob->data, 490dd308bc3SMichael Ellerman blob->size); 491dd308bc3SMichael Ellerman } 492dd308bc3SMichael Ellerman 49300977a59SArjan van de Ven static const struct file_operations fops_blob = { 494dd308bc3SMichael Ellerman .read = read_file_blob, 495dd308bc3SMichael Ellerman .open = default_open, 4966038f373SArnd Bergmann .llseek = default_llseek, 497dd308bc3SMichael Ellerman }; 498dd308bc3SMichael Ellerman 499dd308bc3SMichael Ellerman /** 500400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 501dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 502dd308bc3SMichael Ellerman * @mode: the permission that the file should have 503dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 5046468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 505dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 506dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 507dd308bc3SMichael Ellerman * to the blob data and the size of the data. 508dd308bc3SMichael Ellerman * 509dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 510dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 511dd308bc3SMichael Ellerman * read from. Writing is not supported. 512dd308bc3SMichael Ellerman * 513dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 514dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 515dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 5166468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 517dd308bc3SMichael Ellerman * 5186468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 519dd308bc3SMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 5206468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 521dd308bc3SMichael Ellerman * code. 522dd308bc3SMichael Ellerman */ 523dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode, 524dd308bc3SMichael Ellerman struct dentry *parent, 525dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 526dd308bc3SMichael Ellerman { 527dd308bc3SMichael Ellerman return debugfs_create_file(name, mode, parent, blob, &fops_blob); 528dd308bc3SMichael Ellerman } 529dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 5301a087c6aSAlessandro Rubini 5311a087c6aSAlessandro Rubini /* 5321a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 5331a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 5341a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 5351a087c6aSAlessandro Rubini */ 5361a087c6aSAlessandro Rubini 5371a087c6aSAlessandro Rubini /** 5381a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 5391a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 5401a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 5411a087c6aSAlessandro Rubini * @mregs: the length of the above array 5421a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 5431a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 5441a087c6aSAlessandro Rubini * 5451a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 5461a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 5471a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 5481a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 5491a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 5501a087c6aSAlessandro Rubini * for example configuration of dma channels 5511a087c6aSAlessandro Rubini */ 5528ee4dd9fSAlessandro Rubini int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 5531a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 5541a087c6aSAlessandro Rubini { 5551a087c6aSAlessandro Rubini int i, ret = 0; 5561a087c6aSAlessandro Rubini 5571a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 5581a087c6aSAlessandro Rubini if (prefix) 5591a087c6aSAlessandro Rubini ret += seq_printf(s, "%s", prefix); 5601a087c6aSAlessandro Rubini ret += seq_printf(s, "%s = 0x%08x\n", regs->name, 5611a087c6aSAlessandro Rubini readl((void *)(base + regs->offset))); 5621a087c6aSAlessandro Rubini } 5631a087c6aSAlessandro Rubini return ret; 5641a087c6aSAlessandro Rubini } 5651a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 5661a087c6aSAlessandro Rubini 5671a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 5681a087c6aSAlessandro Rubini { 5691a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 5701a087c6aSAlessandro Rubini 5711a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 5721a087c6aSAlessandro Rubini return 0; 5731a087c6aSAlessandro Rubini } 5741a087c6aSAlessandro Rubini 5751a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 5761a087c6aSAlessandro Rubini { 5771a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 5781a087c6aSAlessandro Rubini } 5791a087c6aSAlessandro Rubini 5801a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 5811a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 5821a087c6aSAlessandro Rubini .read = seq_read, 5831a087c6aSAlessandro Rubini .llseek = seq_lseek, 5841a087c6aSAlessandro Rubini .release = single_release, 5851a087c6aSAlessandro Rubini }; 5861a087c6aSAlessandro Rubini 5871a087c6aSAlessandro Rubini /** 5881a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 5891a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 5901a087c6aSAlessandro Rubini * @mode: the permission that the file should have 5911a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 5921a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 5931a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 5941a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 5951a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 5961a087c6aSAlessandro Rubini * address where the register bank is to be found. 5971a087c6aSAlessandro Rubini * 5981a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 5991a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 6001a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 6011a087c6aSAlessandro Rubini * 6021a087c6aSAlessandro Rubini * This function will return a pointer to a dentry if it succeeds. This 6031a087c6aSAlessandro Rubini * pointer must be passed to the debugfs_remove() function when the file is 6041a087c6aSAlessandro Rubini * to be removed (no automatic cleanup happens if your module is unloaded, 6051a087c6aSAlessandro Rubini * you are responsible here.) If an error occurs, %NULL will be returned. 6061a087c6aSAlessandro Rubini * 6071a087c6aSAlessandro Rubini * If debugfs is not enabled in the kernel, the value -%ENODEV will be 6081a087c6aSAlessandro Rubini * returned. It is not wise to check for this value, but rather, check for 6091a087c6aSAlessandro Rubini * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 6101a087c6aSAlessandro Rubini * code. 6111a087c6aSAlessandro Rubini */ 6121a087c6aSAlessandro Rubini struct dentry *debugfs_create_regset32(const char *name, mode_t mode, 6131a087c6aSAlessandro Rubini struct dentry *parent, 6141a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 6151a087c6aSAlessandro Rubini { 6161a087c6aSAlessandro Rubini return debugfs_create_file(name, mode, parent, regset, &fops_regset32); 6171a087c6aSAlessandro Rubini } 6181a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 619