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. 121da177e4SLinus Torvalds * See Documentation/DocBook/kernel-api 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> 191da177e4SLinus Torvalds #include <linux/debugfs.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 221da177e4SLinus Torvalds size_t count, loff_t *ppos) 231da177e4SLinus Torvalds { 241da177e4SLinus Torvalds return 0; 251da177e4SLinus Torvalds } 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 281da177e4SLinus Torvalds size_t count, loff_t *ppos) 291da177e4SLinus Torvalds { 301da177e4SLinus Torvalds return count; 311da177e4SLinus Torvalds } 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds static int default_open(struct inode *inode, struct file *file) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds if (inode->u.generic_ip) 361da177e4SLinus Torvalds file->private_data = inode->u.generic_ip; 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds return 0; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds 414b6f5d20SArjan van de Ven const struct file_operations debugfs_file_operations = { 421da177e4SLinus Torvalds .read = default_read_file, 431da177e4SLinus Torvalds .write = default_write_file, 441da177e4SLinus Torvalds .open = default_open, 451da177e4SLinus Torvalds }; 461da177e4SLinus Torvalds 47acaefc25SArnd Bergmann static void debugfs_u8_set(void *data, u64 val) 48acaefc25SArnd Bergmann { 49acaefc25SArnd Bergmann *(u8 *)data = val; 50acaefc25SArnd Bergmann } 51acaefc25SArnd Bergmann static u64 debugfs_u8_get(void *data) 52acaefc25SArnd Bergmann { 53acaefc25SArnd Bergmann return *(u8 *)data; 54acaefc25SArnd Bergmann } 55acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /** 58*6468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 591da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 601da177e4SLinus Torvalds * @mode: the permission that the file should have 611da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 62*6468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 631da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 641da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 651da177e4SLinus Torvalds * from. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 681da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 691da177e4SLinus Torvalds * set, it can be read from, and written to. 701da177e4SLinus Torvalds * 711da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 721da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 731da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 74*6468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 751da177e4SLinus Torvalds * 76*6468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 771da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 78*6468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 791da177e4SLinus Torvalds * code. 801da177e4SLinus Torvalds */ 811da177e4SLinus Torvalds struct dentry *debugfs_create_u8(const char *name, mode_t mode, 821da177e4SLinus Torvalds struct dentry *parent, u8 *value) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u8); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 871da177e4SLinus Torvalds 88acaefc25SArnd Bergmann static void debugfs_u16_set(void *data, u64 val) 89acaefc25SArnd Bergmann { 90acaefc25SArnd Bergmann *(u16 *)data = val; 91acaefc25SArnd Bergmann } 92acaefc25SArnd Bergmann static u64 debugfs_u16_get(void *data) 93acaefc25SArnd Bergmann { 94acaefc25SArnd Bergmann return *(u16 *)data; 95acaefc25SArnd Bergmann } 96acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 97acaefc25SArnd Bergmann 981da177e4SLinus Torvalds /** 99*6468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 1001da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1011da177e4SLinus Torvalds * @mode: the permission that the file should have 1021da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 103*6468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1041da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1051da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1061da177e4SLinus Torvalds * from. 1071da177e4SLinus Torvalds * 1081da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1091da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1101da177e4SLinus Torvalds * set, it can be read from, and written to. 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1131da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1141da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 115*6468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1161da177e4SLinus Torvalds * 117*6468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1181da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 119*6468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1201da177e4SLinus Torvalds * code. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds struct dentry *debugfs_create_u16(const char *name, mode_t mode, 1231da177e4SLinus Torvalds struct dentry *parent, u16 *value) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u16); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 1281da177e4SLinus Torvalds 129acaefc25SArnd Bergmann static void debugfs_u32_set(void *data, u64 val) 130acaefc25SArnd Bergmann { 131acaefc25SArnd Bergmann *(u32 *)data = val; 132acaefc25SArnd Bergmann } 133acaefc25SArnd Bergmann static u64 debugfs_u32_get(void *data) 134acaefc25SArnd Bergmann { 135acaefc25SArnd Bergmann return *(u32 *)data; 136acaefc25SArnd Bergmann } 137acaefc25SArnd Bergmann DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 138acaefc25SArnd Bergmann 1391da177e4SLinus Torvalds /** 140*6468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 1411da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 1421da177e4SLinus Torvalds * @mode: the permission that the file should have 1431da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 144*6468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1451da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 1461da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 1471da177e4SLinus Torvalds * from. 1481da177e4SLinus Torvalds * 1491da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 1501da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 1511da177e4SLinus Torvalds * set, it can be read from, and written to. 1521da177e4SLinus Torvalds * 1531da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 1541da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 1551da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 156*6468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 1571da177e4SLinus Torvalds * 158*6468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 1591da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 160*6468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 1611da177e4SLinus Torvalds * code. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds struct dentry *debugfs_create_u32(const char *name, mode_t mode, 1641da177e4SLinus Torvalds struct dentry *parent, u32 *value) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_u32); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static ssize_t read_file_bool(struct file *file, char __user *user_buf, 1711da177e4SLinus Torvalds size_t count, loff_t *ppos) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds char buf[3]; 1741da177e4SLinus Torvalds u32 *val = file->private_data; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds if (*val) 1771da177e4SLinus Torvalds buf[0] = 'Y'; 1781da177e4SLinus Torvalds else 1791da177e4SLinus Torvalds buf[0] = 'N'; 1801da177e4SLinus Torvalds buf[1] = '\n'; 1811da177e4SLinus Torvalds buf[2] = 0x00; 1821da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds static ssize_t write_file_bool(struct file *file, const char __user *user_buf, 1861da177e4SLinus Torvalds size_t count, loff_t *ppos) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds char buf[32]; 1891da177e4SLinus Torvalds int buf_size; 1901da177e4SLinus Torvalds u32 *val = file->private_data; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 1931da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 1941da177e4SLinus Torvalds return -EFAULT; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds switch (buf[0]) { 1971da177e4SLinus Torvalds case 'y': 1981da177e4SLinus Torvalds case 'Y': 1991da177e4SLinus Torvalds case '1': 2001da177e4SLinus Torvalds *val = 1; 2011da177e4SLinus Torvalds break; 2021da177e4SLinus Torvalds case 'n': 2031da177e4SLinus Torvalds case 'N': 2041da177e4SLinus Torvalds case '0': 2051da177e4SLinus Torvalds *val = 0; 2061da177e4SLinus Torvalds break; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds return count; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2124b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 2131da177e4SLinus Torvalds .read = read_file_bool, 2141da177e4SLinus Torvalds .write = write_file_bool, 2151da177e4SLinus Torvalds .open = default_open, 2161da177e4SLinus Torvalds }; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /** 219*6468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 2201da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 2211da177e4SLinus Torvalds * @mode: the permission that the file should have 2221da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 223*6468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 2241da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 2251da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 2261da177e4SLinus Torvalds * from. 2271da177e4SLinus Torvalds * 2281da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 2291da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 2301da177e4SLinus Torvalds * set, it can be read from, and written to. 2311da177e4SLinus Torvalds * 2321da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 2331da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 2341da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 235*6468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 2361da177e4SLinus Torvalds * 237*6468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 2381da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 239*6468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 2401da177e4SLinus Torvalds * code. 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds struct dentry *debugfs_create_bool(const char *name, mode_t mode, 2431da177e4SLinus Torvalds struct dentry *parent, u32 *value) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds return debugfs_create_file(name, mode, parent, value, &fops_bool); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 2481da177e4SLinus Torvalds 249dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 250dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 251dd308bc3SMichael Ellerman { 252dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 253dd308bc3SMichael Ellerman return simple_read_from_buffer(user_buf, count, ppos, blob->data, 254dd308bc3SMichael Ellerman blob->size); 255dd308bc3SMichael Ellerman } 256dd308bc3SMichael Ellerman 257dd308bc3SMichael Ellerman static struct file_operations fops_blob = { 258dd308bc3SMichael Ellerman .read = read_file_blob, 259dd308bc3SMichael Ellerman .open = default_open, 260dd308bc3SMichael Ellerman }; 261dd308bc3SMichael Ellerman 262dd308bc3SMichael Ellerman /** 263*6468b3afSRandy Dunlap * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob 264dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 265dd308bc3SMichael Ellerman * @mode: the permission that the file should have 266dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 267*6468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 268dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 269dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 270dd308bc3SMichael Ellerman * to the blob data and the size of the data. 271dd308bc3SMichael Ellerman * 272dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 273dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 274dd308bc3SMichael Ellerman * read from. Writing is not supported. 275dd308bc3SMichael Ellerman * 276dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 277dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 278dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 279*6468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 280dd308bc3SMichael Ellerman * 281*6468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 282dd308bc3SMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 283*6468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 284dd308bc3SMichael Ellerman * code. 285dd308bc3SMichael Ellerman */ 286dd308bc3SMichael Ellerman struct dentry *debugfs_create_blob(const char *name, mode_t mode, 287dd308bc3SMichael Ellerman struct dentry *parent, 288dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 289dd308bc3SMichael Ellerman { 290dd308bc3SMichael Ellerman return debugfs_create_file(name, mode, parent, blob, &fops_blob); 291dd308bc3SMichael Ellerman } 292dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 293