11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * file.c - part of debugfs, a tiny little debug file system 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 51da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License version 91da177e4SLinus Torvalds * 2 as published by the Free Software Foundation. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 12883ce42eSRobert P. J. Day * See Documentation/DocBook/filesystems for more details. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/fs.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 1966f54963SPeter Oberparleiter #include <linux/namei.h> 201da177e4SLinus Torvalds #include <linux/debugfs.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 231da177e4SLinus Torvalds size_t count, loff_t *ppos) 241da177e4SLinus Torvalds { 251da177e4SLinus Torvalds return 0; 261da177e4SLinus Torvalds } 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 291da177e4SLinus Torvalds size_t count, loff_t *ppos) 301da177e4SLinus Torvalds { 311da177e4SLinus Torvalds return count; 321da177e4SLinus Torvalds } 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file) 351da177e4SLinus Torvalds { 368e18e294STheodore Ts'o if (inode->i_private) 378e18e294STheodore Ts'o file->private_data = inode->i_private; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds return 0; 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 424b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = { 431da177e4SLinus Torvalds .read = default_read_file, 441da177e4SLinus Torvalds .write = default_write_file, 451da177e4SLinus Torvalds .open = default_open, 461da177e4SLinus Torvalds }; 471da177e4SLinus Torvalds 4866f54963SPeter Oberparleiter static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) 4966f54963SPeter Oberparleiter { 5066f54963SPeter Oberparleiter nd_set_link(nd, dentry->d_inode->i_private); 5166f54963SPeter Oberparleiter return NULL; 5266f54963SPeter Oberparleiter } 5366f54963SPeter Oberparleiter 5466f54963SPeter Oberparleiter const struct inode_operations debugfs_link_operations = { 5566f54963SPeter Oberparleiter .readlink = generic_readlink, 5666f54963SPeter Oberparleiter .follow_link = debugfs_follow_link, 5766f54963SPeter Oberparleiter }; 5866f54963SPeter Oberparleiter 598b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 60acaefc25SArnd Bergmann { 61acaefc25SArnd Bergmann *(u8 *)data = val; 628b88b099SChristoph Hellwig return 0; 63acaefc25SArnd Bergmann } 648b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 65acaefc25SArnd Bergmann { 668b88b099SChristoph Hellwig *val = *(u8 *)data; 678b88b099SChristoph Hellwig return 0; 68acaefc25SArnd Bergmann } 69acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 70e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 71e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /** 746468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 751da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 761da177e4SLinus Torvalds * @mode: the permission that the file should have 771da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 786468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 791da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 801da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 811da177e4SLinus Torvalds * from. 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 841da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 851da177e4SLinus Torvalds * set, it can be read from, and written to. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 881da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 891da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 906468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 911da177e4SLinus Torvalds * 926468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 931da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 946468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 951da177e4SLinus Torvalds * code. 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode, 981da177e4SLinus Torvalds struct dentry *parent, u8 *value) 991da177e4SLinus Torvalds { 100e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 101e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 102e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); 103e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 104e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 105e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); 106e4792aa3SRobin Getz 1071da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u8); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 1101da177e4SLinus Torvalds 1118b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 112acaefc25SArnd Bergmann { 113acaefc25SArnd Bergmann *(u16 *)data = val; 1148b88b099SChristoph Hellwig return 0; 115acaefc25SArnd Bergmann } 1168b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 117acaefc25SArnd Bergmann { 1188b88b099SChristoph Hellwig *val = *(u16 *)data; 1198b88b099SChristoph Hellwig return 0; 120acaefc25SArnd Bergmann } 121acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 122e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 123e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 124acaefc25SArnd Bergmann 1251da177e4SLinus Torvalds /** 1266468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 1271da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1281da177e4SLinus Torvalds * @mode: the permission that the file should have 1291da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1306468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1311da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1321da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1331da177e4SLinus Torvalds * from. 1341da177e4SLinus Torvalds * 1351da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1361da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1371da177e4SLinus Torvalds * set, it can be read from, and written to. 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1401da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1411da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1426468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1431da177e4SLinus Torvalds * 1446468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1451da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 1466468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1471da177e4SLinus Torvalds * code. 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode, 1501da177e4SLinus Torvalds struct dentry *parent, u16 *value) 1511da177e4SLinus Torvalds { 152e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 153e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 154e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); 155e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 156e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 157e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); 158e4792aa3SRobin Getz 1591da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u16); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 1621da177e4SLinus Torvalds 1638b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 164acaefc25SArnd Bergmann { 165acaefc25SArnd Bergmann *(u32 *)data = val; 1668b88b099SChristoph Hellwig return 0; 167acaefc25SArnd Bergmann } 1688b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 169acaefc25SArnd Bergmann { 1708b88b099SChristoph Hellwig *val = *(u32 *)data; 1718b88b099SChristoph Hellwig return 0; 172acaefc25SArnd Bergmann } 173acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 174e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 175e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 176acaefc25SArnd Bergmann 1771da177e4SLinus Torvalds /** 1786468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 1791da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1801da177e4SLinus Torvalds * @mode: the permission that the file should have 1811da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 1826468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1831da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1841da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1851da177e4SLinus Torvalds * from. 1861da177e4SLinus Torvalds * 1871da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1881da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1891da177e4SLinus Torvalds * set, it can be read from, and written to. 1901da177e4SLinus Torvalds * 1911da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1921da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1931da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 1946468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1951da177e4SLinus Torvalds * 1966468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1971da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 1986468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1991da177e4SLinus Torvalds * code. 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode, 2021da177e4SLinus Torvalds struct dentry *parent, u32 *value) 2031da177e4SLinus Torvalds { 204e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 205e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 206e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); 207e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 208e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 209e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); 210e4792aa3SRobin Getz 2111da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u32); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 2141da177e4SLinus Torvalds 2158b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 2168447891fSMichael Ellerman { 2178447891fSMichael Ellerman *(u64 *)data = val; 2188b88b099SChristoph Hellwig return 0; 2198447891fSMichael Ellerman } 2208447891fSMichael Ellerman 2218b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 2228447891fSMichael Ellerman { 2238b88b099SChristoph Hellwig *val = *(u64 *)data; 2248b88b099SChristoph Hellwig return 0; 2258447891fSMichael Ellerman } 2268447891fSMichael Ellerman DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 227e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 228e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 2298447891fSMichael Ellerman 2308447891fSMichael Ellerman /** 2318447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 2328447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 2338447891fSMichael Ellerman * @mode: the permission that the file should have 2348447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 2358447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 2368447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 2378447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 2388447891fSMichael Ellerman * from. 2398447891fSMichael Ellerman * 2408447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 2418447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 2428447891fSMichael Ellerman * set, it can be read from, and written to. 2438447891fSMichael Ellerman * 2448447891fSMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 2458447891fSMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 2468447891fSMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 2478447891fSMichael Ellerman * you are responsible here.) If an error occurs, %NULL will be returned. 2488447891fSMichael Ellerman * 2498447891fSMichael Ellerman * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2508447891fSMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 2518447891fSMichael Ellerman * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2528447891fSMichael Ellerman * code. 2538447891fSMichael Ellerman */ 2548447891fSMichael Ellerman struct dentry *debugfs_create_u64(const char *name, mode_t mode, 2558447891fSMichael Ellerman struct dentry *parent, u64 *value) 2568447891fSMichael Ellerman { 257e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 258e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 259e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); 260e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 261e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 262e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); 263e4792aa3SRobin Getz 2648447891fSMichael Ellerman return debugfs_create_file(name, mode, parent, value, &fops_u64); 2658447891fSMichael Ellerman } 2668447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 2678447891fSMichael Ellerman 2682ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 269e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 270e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 2712ebefc50SRobin Getz 2722ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); 273e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 274e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 2752ebefc50SRobin Getz 2762ebefc50SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); 277e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 278e4792aa3SRobin Getz DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 2792ebefc50SRobin Getz 280*15b0beaaSHuang Ying DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 281*15b0beaaSHuang Ying 282e6716b87SRandy Dunlap /* 283*15b0beaaSHuang 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 284e6716b87SRandy Dunlap * 285e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 286e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 287e6716b87SRandy Dunlap * decimal functions. 288e6716b87SRandy Dunlap */ 289e6716b87SRandy Dunlap 2902ebefc50SRobin Getz /** 2912ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 292e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 293e6716b87SRandy Dunlap * @mode: the permission that the file should have 294e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 295e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 296e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 297e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 298e6716b87SRandy Dunlap * from. 2992ebefc50SRobin Getz */ 3002ebefc50SRobin Getz struct dentry *debugfs_create_x8(const char *name, mode_t mode, 3012ebefc50SRobin Getz struct dentry *parent, u8 *value) 3022ebefc50SRobin Getz { 303e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 304e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 305e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); 306e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 307e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 308e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); 309e4792aa3SRobin Getz 3102ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x8); 3112ebefc50SRobin Getz } 3122ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 3132ebefc50SRobin Getz 314e6716b87SRandy Dunlap /** 315e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 316e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 317e6716b87SRandy Dunlap * @mode: the permission that the file should have 318e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 319e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 320e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 321e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 322e6716b87SRandy Dunlap * from. 323e6716b87SRandy Dunlap */ 3242ebefc50SRobin Getz struct dentry *debugfs_create_x16(const char *name, mode_t mode, 3252ebefc50SRobin Getz struct dentry *parent, u16 *value) 3262ebefc50SRobin Getz { 327e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 328e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 329e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); 330e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 331e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 332e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); 333e4792aa3SRobin Getz 3342ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x16); 3352ebefc50SRobin Getz } 3362ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 3372ebefc50SRobin Getz 338e6716b87SRandy Dunlap /** 339e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 340e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 341e6716b87SRandy Dunlap * @mode: the permission that the file should have 342e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 343e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 344e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 345e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 346e6716b87SRandy Dunlap * from. 347e6716b87SRandy Dunlap */ 3482ebefc50SRobin Getz struct dentry *debugfs_create_x32(const char *name, mode_t mode, 3492ebefc50SRobin Getz struct dentry *parent, u32 *value) 3502ebefc50SRobin Getz { 351e4792aa3SRobin Getz /* if there are no write bits set, make read only */ 352e4792aa3SRobin Getz if (!(mode & S_IWUGO)) 353e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); 354e4792aa3SRobin Getz /* if there are no read bits set, make write only */ 355e4792aa3SRobin Getz if (!(mode & S_IRUGO)) 356e4792aa3SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); 357e4792aa3SRobin Getz 3582ebefc50SRobin Getz return debugfs_create_file(name, mode, parent, value, &fops_x32); 3592ebefc50SRobin Getz } 3602ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 3612ebefc50SRobin Getz 362*15b0beaaSHuang Ying /** 363*15b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 364*15b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 365*15b0beaaSHuang Ying * @mode: the permission that the file should have 366*15b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 367*15b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 368*15b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 369*15b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 370*15b0beaaSHuang Ying * from. 371*15b0beaaSHuang Ying */ 372*15b0beaaSHuang Ying struct dentry *debugfs_create_x64(const char *name, mode_t mode, 373*15b0beaaSHuang Ying struct dentry *parent, u64 *value) 374*15b0beaaSHuang Ying { 375*15b0beaaSHuang Ying return debugfs_create_file(name, mode, parent, value, &fops_x64); 376*15b0beaaSHuang Ying } 377*15b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 378*15b0beaaSHuang Ying 3795e078787SInaky Perez-Gonzalez 3805e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 3815e078787SInaky Perez-Gonzalez { 3825e078787SInaky Perez-Gonzalez *(size_t *)data = val; 3835e078787SInaky Perez-Gonzalez return 0; 3845e078787SInaky Perez-Gonzalez } 3855e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 3865e078787SInaky Perez-Gonzalez { 3875e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 3885e078787SInaky Perez-Gonzalez return 0; 3895e078787SInaky Perez-Gonzalez } 3905e078787SInaky Perez-Gonzalez DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 3915e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 3925e078787SInaky Perez-Gonzalez 3935e078787SInaky Perez-Gonzalez /** 3945e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 3955e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 3965e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 3975e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 3985e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 3995e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 4005e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 4015e078787SInaky Perez-Gonzalez * from. 4025e078787SInaky Perez-Gonzalez */ 4035e078787SInaky Perez-Gonzalez struct dentry *debugfs_create_size_t(const char *name, mode_t mode, 4045e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 4055e078787SInaky Perez-Gonzalez { 4065e078787SInaky Perez-Gonzalez return debugfs_create_file(name, mode, parent, value, &fops_size_t); 4075e078787SInaky Perez-Gonzalez } 4085e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 4095e078787SInaky Perez-Gonzalez 4105e078787SInaky Perez-Gonzalez 4111da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf, 4121da177e4SLinus Torvalds size_t count, loff_t *ppos) 4131da177e4SLinus Torvalds { 4141da177e4SLinus Torvalds char buf[3]; 4151da177e4SLinus Torvalds u32 *val = file->private_data; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (*val) 4181da177e4SLinus Torvalds buf[0] = 'Y'; 4191da177e4SLinus Torvalds else 4201da177e4SLinus Torvalds buf[0] = 'N'; 4211da177e4SLinus Torvalds buf[1] = '\n'; 4221da177e4SLinus Torvalds buf[2] = 0x00; 4231da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf, 4271da177e4SLinus Torvalds size_t count, loff_t *ppos) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds char buf[32]; 4301da177e4SLinus Torvalds int buf_size; 4311da177e4SLinus Torvalds u32 *val = file->private_data; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 4341da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 4351da177e4SLinus Torvalds return -EFAULT; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds switch (buf[0]) { 4381da177e4SLinus Torvalds case 'y': 4391da177e4SLinus Torvalds case 'Y': 4401da177e4SLinus Torvalds case '1': 4411da177e4SLinus Torvalds *val = 1; 4421da177e4SLinus Torvalds break; 4431da177e4SLinus Torvalds case 'n': 4441da177e4SLinus Torvalds case 'N': 4451da177e4SLinus Torvalds case '0': 4461da177e4SLinus Torvalds *val = 0; 4471da177e4SLinus Torvalds break; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds return count; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4534b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 4541da177e4SLinus Torvalds .read = read_file_bool, 4551da177e4SLinus Torvalds .write = write_file_bool, 4561da177e4SLinus Torvalds .open = default_open, 4571da177e4SLinus Torvalds }; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds /** 4606468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 4611da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4621da177e4SLinus Torvalds * @mode: the permission that the file should have 4631da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4646468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4651da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4661da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4671da177e4SLinus Torvalds * from. 4681da177e4SLinus Torvalds * 4691da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4701da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4711da177e4SLinus Torvalds * set, it can be read from, and written to. 4721da177e4SLinus Torvalds * 4731da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4741da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4751da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4766468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4771da177e4SLinus Torvalds * 4786468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 4791da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 4806468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 4811da177e4SLinus Torvalds * code. 4821da177e4SLinus Torvalds */ 4831da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode, 4841da177e4SLinus Torvalds struct dentry *parent, u32 *value) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_bool); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 4891da177e4SLinus Torvalds 490dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 491dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 492dd308bc3SMichael Ellerman { 493dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 494dd308bc3SMichael Ellerman return simple_read_from_buffer(user_buf, count, ppos, blob->data, 495dd308bc3SMichael Ellerman blob->size); 496dd308bc3SMichael Ellerman } 497dd308bc3SMichael Ellerman 49800977a59SArjan van de Ven static const struct file_operations fops_blob = { 499dd308bc3SMichael Ellerman .read = read_file_blob, 500dd308bc3SMichael Ellerman .open = default_open, 501dd308bc3SMichael Ellerman }; 502dd308bc3SMichael Ellerman 503dd308bc3SMichael Ellerman /** 504400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 505dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 506dd308bc3SMichael Ellerman * @mode: the permission that the file should have 507dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 5086468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 509dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 510dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 511dd308bc3SMichael Ellerman * to the blob data and the size of the data. 512dd308bc3SMichael Ellerman * 513dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 514dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 515dd308bc3SMichael Ellerman * read from. Writing is not supported. 516dd308bc3SMichael Ellerman * 517dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 518dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 519dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 5206468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 521dd308bc3SMichael Ellerman * 5226468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 523dd308bc3SMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 5246468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 525dd308bc3SMichael Ellerman * code. 526dd308bc3SMichael Ellerman */ 527dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode, 528dd308bc3SMichael Ellerman struct dentry *parent, 529dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 530dd308bc3SMichael Ellerman { 531dd308bc3SMichael Ellerman return debugfs_create_file(name, mode, parent, blob, &fops_blob); 532dd308bc3SMichael Ellerman } 533dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 534