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> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 241da177e4SLinus Torvalds size_t count, loff_t *ppos) 251da177e4SLinus Torvalds { 261da177e4SLinus Torvalds return 0; 271da177e4SLinus Torvalds } 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 301da177e4SLinus Torvalds size_t count, loff_t *ppos) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds return count; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file) 361da177e4SLinus Torvalds { 378e18e294STheodore Ts'o if (inode->i_private) 388e18e294STheodore Ts'o file->private_data = inode->i_private; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds return 0; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 434b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = { 441da177e4SLinus Torvalds .read = default_read_file, 451da177e4SLinus Torvalds .write = default_write_file, 461da177e4SLinus Torvalds .open = default_open, 476038f373SArnd Bergmann .llseek = noop_llseek, 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 5066f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) 5166f54963SPeter Oberparleiter { 5266f54963SPeter Oberparleiter nd_set_link(nd, dentry->d_inode->i_private); 5366f54963SPeter Oberparleiter return NULL; 5466f54963SPeter Oberparleiter } 5566f54963SPeter Oberparleiter 5666f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = { 5766f54963SPeter Oberparleiter .readlink = generic_readlink, 5866f54963SPeter Oberparleiter .follow_link = debugfs_follow_link, 5966f54963SPeter Oberparleiter }; 6066f54963SPeter Oberparleiter 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 */ 991da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode, 1001da177e4SLinus Torvalds struct dentry *parent, u8 *value) 1011da177e4SLinus Torvalds { 102e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 103e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 104e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); 105e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 106e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 107e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); 108e4792aa3SRobin Getz 1091da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u8); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 1121da177e4SLinus Torvalds 1138b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 114acaefc25SArnd Bergmann { 115acaefc25SArnd Bergmann *(u16 *)data = val; 1168b88b099SChristoph Hellwig return 0; 117acaefc25SArnd Bergmann } 1188b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 119acaefc25SArnd Bergmann { 1208b88b099SChristoph Hellwig *val = *(u16 *)data; 1218b88b099SChristoph Hellwig return 0; 122acaefc25SArnd Bergmann } 123acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 124e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 125e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 126acaefc25SArnd Bergmann 1271da177e4SLinus Torvalds /** 1286468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 1291da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1301da177e4SLinus Torvalds * @mode: the permission that the file should have 1311da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1326468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1331da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1341da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1351da177e4SLinus Torvalds * from. 1361da177e4SLinus Torvalds * 1371da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1381da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1391da177e4SLinus Torvalds * set, it can be read from, and written to. 1401da177e4SLinus Torvalds * 1411da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1421da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1431da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1446468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1451da177e4SLinus Torvalds * 1466468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1471da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 1486468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1491da177e4SLinus Torvalds * code. 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode, 1521da177e4SLinus Torvalds struct dentry *parent, u16 *value) 1531da177e4SLinus Torvalds { 154e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 155e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 156e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); 157e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 158e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 159e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); 160e4792aa3SRobin Getz 1611da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u16); 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 1641da177e4SLinus Torvalds 1658b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 166acaefc25SArnd Bergmann { 167acaefc25SArnd Bergmann *(u32 *)data = val; 1688b88b099SChristoph Hellwig return 0; 169acaefc25SArnd Bergmann } 1708b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 171acaefc25SArnd Bergmann { 1728b88b099SChristoph Hellwig *val = *(u32 *)data; 1738b88b099SChristoph Hellwig return 0; 174acaefc25SArnd Bergmann } 175acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 176e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 177e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 178acaefc25SArnd Bergmann 1791da177e4SLinus Torvalds /** 1806468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 1811da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1821da177e4SLinus Torvalds * @mode: the permission that the file should have 1831da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1846468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1851da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1861da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1871da177e4SLinus Torvalds * from. 1881da177e4SLinus Torvalds * 1891da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1901da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1911da177e4SLinus Torvalds * set, it can be read from, and written to. 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1941da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1951da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1966468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1971da177e4SLinus Torvalds * 1986468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1991da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 2006468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2011da177e4SLinus Torvalds * code. 2021da177e4SLinus Torvalds */ 2031da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode, 2041da177e4SLinus Torvalds struct dentry *parent, u32 *value) 2051da177e4SLinus Torvalds { 206e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 207e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 208e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); 209e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 210e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 211e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); 212e4792aa3SRobin Getz 2131da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u32); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 2161da177e4SLinus Torvalds 2178b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 2188447891fSMichael Ellerman { 2198447891fSMichael Ellerman *(u64 *)data = val; 2208b88b099SChristoph Hellwig return 0; 2218447891fSMichael Ellerman } 2228447891fSMichael Ellerman 2238b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 2248447891fSMichael Ellerman { 2258b88b099SChristoph Hellwig *val = *(u64 *)data; 2268b88b099SChristoph Hellwig return 0; 2278447891fSMichael Ellerman } 2288447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 229e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 230e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 2318447891fSMichael Ellerman 2328447891fSMichael Ellerman /** 2338447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 2348447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 2358447891fSMichael Ellerman * @mode: the permission that the file should have 2368447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 2378447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 2388447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 2398447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 2408447891fSMichael Ellerman * from. 2418447891fSMichael Ellerman * 2428447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 2438447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 2448447891fSMichael Ellerman * set, it can be read from, and written to. 2458447891fSMichael Ellerman * 2468447891fSMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 2478447891fSMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 2488447891fSMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 2498447891fSMichael Ellerman * you are responsible here.) If an error occurs, %NULL will be returned. 2508447891fSMichael Ellerman * 2518447891fSMichael Ellerman * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2528447891fSMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 2538447891fSMichael Ellerman * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2548447891fSMichael Ellerman * code. 2558447891fSMichael Ellerman */ 2568447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode, 2578447891fSMichael Ellerman struct dentry *parent, u64 *value) 2588447891fSMichael Ellerman { 259e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 260e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 261e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); 262e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 263e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 264e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); 265e4792aa3SRobin Getz 2668447891fSMichael Ellerman return debugfs_create_file(name, mode, parent, value, &fops_u64); 2678447891fSMichael Ellerman } 2688447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 2698447891fSMichael Ellerman 2702ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 271e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 272e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 2732ebefc50SRobin Getz 2742ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); 275e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 276e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 2772ebefc50SRobin Getz 2782ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); 279e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 280e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 2812ebefc50SRobin Getz 28215b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 28315b0beaaSHuang Ying 284e6716b87SRandy Dunlap /* 28515b0beaaSHuang 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 286e6716b87SRandy Dunlap * 287e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 288e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 289e6716b87SRandy Dunlap * decimal functions. 290e6716b87SRandy Dunlap */ 291e6716b87SRandy Dunlap 2922ebefc50SRobin Getz /** 2932ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 294e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 295e6716b87SRandy Dunlap * @mode: the permission that the file should have 296e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 297e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 298e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 299e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 300e6716b87SRandy Dunlap * from. 3012ebefc50SRobin Getz */ 3022ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode, 3032ebefc50SRobin Getz struct dentry *parent, u8 *value) 3042ebefc50SRobin Getz { 305e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 306e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 307e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); 308e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 309e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 310e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); 311e4792aa3SRobin Getz 3122ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8); 3132ebefc50SRobin Getz } 3142ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 3152ebefc50SRobin Getz 316e6716b87SRandy Dunlap /** 317e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 318e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 319e6716b87SRandy Dunlap * @mode: the permission that the file should have 320e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 321e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 322e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 323e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 324e6716b87SRandy Dunlap * from. 325e6716b87SRandy Dunlap */ 3262ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode, 3272ebefc50SRobin Getz struct dentry *parent, u16 *value) 3282ebefc50SRobin Getz { 329e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 330e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 331e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); 332e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 333e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 334e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); 335e4792aa3SRobin Getz 3362ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16); 3372ebefc50SRobin Getz } 3382ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 3392ebefc50SRobin Getz 340e6716b87SRandy Dunlap /** 341e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 342e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 343e6716b87SRandy Dunlap * @mode: the permission that the file should have 344e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 345e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 346e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 347e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 348e6716b87SRandy Dunlap * from. 349e6716b87SRandy Dunlap */ 3502ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode, 3512ebefc50SRobin Getz struct dentry *parent, u32 *value) 3522ebefc50SRobin Getz { 353e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 354e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 355e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); 356e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 357e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 358e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); 359e4792aa3SRobin Getz 3602ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32); 3612ebefc50SRobin Getz } 3622ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 3632ebefc50SRobin Getz 36415b0beaaSHuang Ying /** 36515b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 36615b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 36715b0beaaSHuang Ying * @mode: the permission that the file should have 36815b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 36915b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 37015b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 37115b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 37215b0beaaSHuang Ying * from. 37315b0beaaSHuang Ying */ 37415b0beaaSHuang Ying struct dentry *debugfs_create_x64(const char *name, mode_t mode, 37515b0beaaSHuang Ying struct dentry *parent, u64 *value) 37615b0beaaSHuang Ying { 37715b0beaaSHuang Ying return debugfs_create_file(name, mode, parent, value, &fops_x64); 37815b0beaaSHuang Ying } 37915b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 38015b0beaaSHuang Ying 3815e078787SInaky Perez-Gonzalez 3825e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 3835e078787SInaky Perez-Gonzalez { 3845e078787SInaky Perez-Gonzalez *(size_t *)data = val; 3855e078787SInaky Perez-Gonzalez return 0; 3865e078787SInaky Perez-Gonzalez } 3875e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 3885e078787SInaky Perez-Gonzalez { 3895e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 3905e078787SInaky Perez-Gonzalez return 0; 3915e078787SInaky Perez-Gonzalez } 3925e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 3935e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 3945e078787SInaky Perez-Gonzalez 3955e078787SInaky Perez-Gonzalez /** 3965e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 3975e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 3985e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 3995e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 4005e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 4015e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 4025e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 4035e078787SInaky Perez-Gonzalez * from. 4045e078787SInaky Perez-Gonzalez */ 4055e078787SInaky Perez-Gonzalez struct dentry *debugfs_create_size_t(const char *name, mode_t mode, 4065e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 4075e078787SInaky Perez-Gonzalez { 4085e078787SInaky Perez-Gonzalez return debugfs_create_file(name, mode, parent, value, &fops_size_t); 4095e078787SInaky Perez-Gonzalez } 4105e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 4115e078787SInaky Perez-Gonzalez 4125e078787SInaky Perez-Gonzalez 4131da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf, 4141da177e4SLinus Torvalds size_t count, loff_t *ppos) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds char buf[3]; 4171da177e4SLinus Torvalds u32 *val = file->private_data; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds if (*val) 4201da177e4SLinus Torvalds buf[0] = 'Y'; 4211da177e4SLinus Torvalds else 4221da177e4SLinus Torvalds buf[0] = 'N'; 4231da177e4SLinus Torvalds buf[1] = '\n'; 4241da177e4SLinus Torvalds buf[2] = 0x00; 4251da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf, 4291da177e4SLinus Torvalds size_t count, loff_t *ppos) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds char buf[32]; 432c42d2237SStephen Boyd size_t buf_size; 4338705b48eSJonathan Cameron bool bv; 4341da177e4SLinus Torvalds u32 *val = file->private_data; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 4371da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 4381da177e4SLinus Torvalds return -EFAULT; 4391da177e4SLinus Torvalds 4408705b48eSJonathan Cameron if (strtobool(buf, &bv) == 0) 4418705b48eSJonathan Cameron *val = bv; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds return count; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4464b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 4471da177e4SLinus Torvalds .read = read_file_bool, 4481da177e4SLinus Torvalds .write = write_file_bool, 4491da177e4SLinus Torvalds .open = default_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 */ 4771da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode, 4781da177e4SLinus Torvalds struct dentry *parent, u32 *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, 494dd308bc3SMichael Ellerman .open = default_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 */ 522dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_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 5301a087c6aSAlessandro Rubini /* 5311a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 5321a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 5331a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 5341a087c6aSAlessandro Rubini */ 5351a087c6aSAlessandro Rubini 5361a087c6aSAlessandro Rubini /** 5371a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 5381a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 5391a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 5401a087c6aSAlessandro Rubini * @mregs: the length of the above array 5411a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 5421a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 5431a087c6aSAlessandro Rubini * 5441a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 5451a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 5461a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 5471a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 5481a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 5491a087c6aSAlessandro Rubini * for example configuration of dma channels 5501a087c6aSAlessandro Rubini */ 551*8ee4dd9fSAlessandro Rubini int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 5521a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 5531a087c6aSAlessandro Rubini { 5541a087c6aSAlessandro Rubini int i, ret = 0; 5551a087c6aSAlessandro Rubini 5561a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 5571a087c6aSAlessandro Rubini if (prefix) 5581a087c6aSAlessandro Rubini ret += seq_printf(s, "%s", prefix); 5591a087c6aSAlessandro Rubini ret += seq_printf(s, "%s = 0x%08x\n", regs->name, 5601a087c6aSAlessandro Rubini readl((void *)(base + regs->offset))); 5611a087c6aSAlessandro Rubini } 5621a087c6aSAlessandro Rubini return ret; 5631a087c6aSAlessandro Rubini } 5641a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 5651a087c6aSAlessandro Rubini 5661a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 5671a087c6aSAlessandro Rubini { 5681a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 5691a087c6aSAlessandro Rubini 5701a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 5711a087c6aSAlessandro Rubini return 0; 5721a087c6aSAlessandro Rubini } 5731a087c6aSAlessandro Rubini 5741a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 5751a087c6aSAlessandro Rubini { 5761a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 5771a087c6aSAlessandro Rubini } 5781a087c6aSAlessandro Rubini 5791a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 5801a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 5811a087c6aSAlessandro Rubini .read = seq_read, 5821a087c6aSAlessandro Rubini .llseek = seq_lseek, 5831a087c6aSAlessandro Rubini .release = single_release, 5841a087c6aSAlessandro Rubini }; 5851a087c6aSAlessandro Rubini 5861a087c6aSAlessandro Rubini /** 5871a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 5881a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 5891a087c6aSAlessandro Rubini * @mode: the permission that the file should have 5901a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 5911a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 5921a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 5931a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 5941a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 5951a087c6aSAlessandro Rubini * address where the register bank is to be found. 5961a087c6aSAlessandro Rubini * 5971a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 5981a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 5991a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 6001a087c6aSAlessandro Rubini * 6011a087c6aSAlessandro Rubini * This function will return a pointer to a dentry if it succeeds. This 6021a087c6aSAlessandro Rubini * pointer must be passed to the debugfs_remove() function when the file is 6031a087c6aSAlessandro Rubini * to be removed (no automatic cleanup happens if your module is unloaded, 6041a087c6aSAlessandro Rubini * you are responsible here.) If an error occurs, %NULL will be returned. 6051a087c6aSAlessandro Rubini * 6061a087c6aSAlessandro Rubini * If debugfs is not enabled in the kernel, the value -%ENODEV will be 6071a087c6aSAlessandro Rubini * returned. It is not wise to check for this value, but rather, check for 6081a087c6aSAlessandro Rubini * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 6091a087c6aSAlessandro Rubini * code. 6101a087c6aSAlessandro Rubini */ 6111a087c6aSAlessandro Rubini struct dentry *debugfs_create_regset32(const char *name, mode_t mode, 6121a087c6aSAlessandro Rubini struct dentry *parent, 6131a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 6141a087c6aSAlessandro Rubini { 6151a087c6aSAlessandro Rubini return debugfs_create_file(name, mode, parent, regset, &fops_regset32); 6161a087c6aSAlessandro Rubini } 6171a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 618