13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * file.c - part of debugfs, a tiny little debug file system 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 61da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 9e1b4fc7aSMauro Carvalho Chehab * See Documentation/filesystems/ for more details. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141a087c6aSAlessandro Rubini #include <linux/seq_file.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/debugfs.h> 1703e099fbSAlessandro Rubini #include <linux/io.h> 189fe2a701SSrivatsa Vaddagiri #include <linux/slab.h> 193a76e5e0SSeth Jennings #include <linux/atomic.h> 2098210b7fSArend van Spriel #include <linux/device.h> 2130332eeeSGeert Uytterhoeven #include <linux/pm_runtime.h> 22cfe39442SAl Viro #include <linux/poll.h> 235496197fSDavid Howells #include <linux/security.h> 249fd4dcecSNicolai Stange 259fd4dcecSNicolai Stange #include "internal.h" 261da177e4SLinus Torvalds 2749d200deSNicolai Stange struct poll_table_struct; 2849d200deSNicolai Stange 291da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 301da177e4SLinus Torvalds size_t count, loff_t *ppos) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds return 0; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 361da177e4SLinus Torvalds size_t count, loff_t *ppos) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds return count; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds 419fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = { 421da177e4SLinus Torvalds .read = default_read_file, 431da177e4SLinus Torvalds .write = default_write_file, 44234e3405SStephen Boyd .open = simple_open, 456038f373SArnd Bergmann .llseek = noop_llseek, 461da177e4SLinus Torvalds }; 471da177e4SLinus Torvalds 489fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry) 499fd4dcecSNicolai Stange 507c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp) 517c8d4698SNicolai Stange { 527c8d4698SNicolai Stange struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; 53055ab8e3SNicolai Stange 547d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) { 557d39bc50SNicolai Stange /* 567d39bc50SNicolai Stange * Urgh, we've been called w/o a protecting 577d39bc50SNicolai Stange * debugfs_file_get(). 587d39bc50SNicolai Stange */ 597d39bc50SNicolai Stange WARN_ON(1); 607d39bc50SNicolai Stange return NULL; 617d39bc50SNicolai Stange } 627d39bc50SNicolai Stange 637c8d4698SNicolai Stange return fsd->real_fops; 647c8d4698SNicolai Stange } 657c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops); 667c8d4698SNicolai Stange 67e9117a5aSNicolai Stange /** 68e9117a5aSNicolai Stange * debugfs_file_get - mark the beginning of file data access 69e9117a5aSNicolai Stange * @dentry: the dentry object whose data is being accessed. 70e9117a5aSNicolai Stange * 71e9117a5aSNicolai Stange * Up to a matching call to debugfs_file_put(), any successive call 72e9117a5aSNicolai Stange * into the file removing functions debugfs_remove() and 73e9117a5aSNicolai Stange * debugfs_remove_recursive() will block. Since associated private 74e9117a5aSNicolai Stange * file data may only get freed after a successful return of any of 75e9117a5aSNicolai Stange * the removal functions, you may safely access it after a successful 76e9117a5aSNicolai Stange * call to debugfs_file_get() without worrying about lifetime issues. 77e9117a5aSNicolai Stange * 78e9117a5aSNicolai Stange * If -%EIO is returned, the file has already been removed and thus, 79e9117a5aSNicolai Stange * it is not safe to access any of its data. If, on the other hand, 80e9117a5aSNicolai Stange * it is allowed to access the file data, zero is returned. 81e9117a5aSNicolai Stange */ 82e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry) 83e9117a5aSNicolai Stange { 847d39bc50SNicolai Stange struct debugfs_fsdata *fsd; 857d39bc50SNicolai Stange void *d_fsd; 86e9117a5aSNicolai Stange 877d39bc50SNicolai Stange d_fsd = READ_ONCE(dentry->d_fsdata); 887d39bc50SNicolai Stange if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) { 897d39bc50SNicolai Stange fsd = d_fsd; 907d39bc50SNicolai Stange } else { 917d39bc50SNicolai Stange fsd = kmalloc(sizeof(*fsd), GFP_KERNEL); 927d39bc50SNicolai Stange if (!fsd) 937d39bc50SNicolai Stange return -ENOMEM; 947d39bc50SNicolai Stange 957d39bc50SNicolai Stange fsd->real_fops = (void *)((unsigned long)d_fsd & 967d39bc50SNicolai Stange ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 977d39bc50SNicolai Stange refcount_set(&fsd->active_users, 1); 987d39bc50SNicolai Stange init_completion(&fsd->active_users_drained); 997d39bc50SNicolai Stange if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) { 1007d39bc50SNicolai Stange kfree(fsd); 1017d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 1027d39bc50SNicolai Stange } 1037d39bc50SNicolai Stange } 1047d39bc50SNicolai Stange 1057d39bc50SNicolai Stange /* 1067d39bc50SNicolai Stange * In case of a successful cmpxchg() above, this check is 1077d39bc50SNicolai Stange * strictly necessary and must follow it, see the comment in 1087d39bc50SNicolai Stange * __debugfs_remove_file(). 1097d39bc50SNicolai Stange * OTOH, if the cmpxchg() hasn't been executed or wasn't 1107d39bc50SNicolai Stange * successful, this serves the purpose of not starving 1117d39bc50SNicolai Stange * removers. 1127d39bc50SNicolai Stange */ 113e9117a5aSNicolai Stange if (d_unlinked(dentry)) 114e9117a5aSNicolai Stange return -EIO; 115e9117a5aSNicolai Stange 116e9117a5aSNicolai Stange if (!refcount_inc_not_zero(&fsd->active_users)) 117e9117a5aSNicolai Stange return -EIO; 118e9117a5aSNicolai Stange 119e9117a5aSNicolai Stange return 0; 120e9117a5aSNicolai Stange } 121e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get); 122e9117a5aSNicolai Stange 123e9117a5aSNicolai Stange /** 124e9117a5aSNicolai Stange * debugfs_file_put - mark the end of file data access 125e9117a5aSNicolai Stange * @dentry: the dentry object formerly passed to 126e9117a5aSNicolai Stange * debugfs_file_get(). 127e9117a5aSNicolai Stange * 128e9117a5aSNicolai Stange * Allow any ongoing concurrent call into debugfs_remove() or 129e9117a5aSNicolai Stange * debugfs_remove_recursive() blocked by a former call to 130e9117a5aSNicolai Stange * debugfs_file_get() to proceed and return to its caller. 131e9117a5aSNicolai Stange */ 132e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry) 133e9117a5aSNicolai Stange { 1347d39bc50SNicolai Stange struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); 135e9117a5aSNicolai Stange 136e9117a5aSNicolai Stange if (refcount_dec_and_test(&fsd->active_users)) 137e9117a5aSNicolai Stange complete(&fsd->active_users_drained); 138e9117a5aSNicolai Stange } 139e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put); 140e9117a5aSNicolai Stange 1415496197fSDavid Howells /* 1425496197fSDavid Howells * Only permit access to world-readable files when the kernel is locked down. 1435496197fSDavid Howells * We also need to exclude any file that has ways to write or alter it as root 1445496197fSDavid Howells * can bypass the permissions check. 1455496197fSDavid Howells */ 146a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode, 1475496197fSDavid Howells struct file *filp, 1485496197fSDavid Howells const struct file_operations *real_fops) 1495496197fSDavid Howells { 150358fcf5dSMichal Suchanek if ((inode->i_mode & 07777 & ~0444) == 0 && 1515496197fSDavid Howells !(filp->f_mode & FMODE_WRITE) && 1525496197fSDavid Howells !real_fops->unlocked_ioctl && 1535496197fSDavid Howells !real_fops->compat_ioctl && 1545496197fSDavid Howells !real_fops->mmap) 155a37f4958SEric Snowberg return 0; 1565496197fSDavid Howells 157a37f4958SEric Snowberg if (security_locked_down(LOCKDOWN_DEBUGFS)) 158a37f4958SEric Snowberg return -EPERM; 159a37f4958SEric Snowberg 160a37f4958SEric Snowberg return 0; 1615496197fSDavid Howells } 1625496197fSDavid Howells 1639fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp) 1649fd4dcecSNicolai Stange { 16569d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 1669fd4dcecSNicolai Stange const struct file_operations *real_fops = NULL; 1677d39bc50SNicolai Stange int r; 1689fd4dcecSNicolai Stange 1697d39bc50SNicolai Stange r = debugfs_file_get(dentry); 1707d39bc50SNicolai Stange if (r) 1717d39bc50SNicolai Stange return r == -EIO ? -ENOENT : r; 1729fd4dcecSNicolai Stange 17386f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 1745496197fSDavid Howells 175a37f4958SEric Snowberg r = debugfs_locked_down(inode, filp, real_fops); 1765496197fSDavid Howells if (r) 1775496197fSDavid Howells goto out; 1785496197fSDavid Howells 179275678e7STaehee Yoo if (!fops_get(real_fops)) { 180e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES 181275678e7STaehee Yoo if (real_fops->owner && 182112cedc8SSven Eckelmann real_fops->owner->state == MODULE_STATE_GOING) { 183112cedc8SSven Eckelmann r = -ENXIO; 184275678e7STaehee Yoo goto out; 185112cedc8SSven Eckelmann } 186275678e7STaehee Yoo #endif 187275678e7STaehee Yoo 1889fd4dcecSNicolai Stange /* Huh? Module did not clean up after itself at exit? */ 1899fd4dcecSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 1909fd4dcecSNicolai Stange dentry); 1919fd4dcecSNicolai Stange r = -ENXIO; 1929fd4dcecSNicolai Stange goto out; 1939fd4dcecSNicolai Stange } 1949fd4dcecSNicolai Stange replace_fops(filp, real_fops); 1959fd4dcecSNicolai Stange 1969fd4dcecSNicolai Stange if (real_fops->open) 1979fd4dcecSNicolai Stange r = real_fops->open(inode, filp); 1989fd4dcecSNicolai Stange 1999fd4dcecSNicolai Stange out: 20069d29f9eSNicolai Stange debugfs_file_put(dentry); 2019fd4dcecSNicolai Stange return r; 2029fd4dcecSNicolai Stange } 2039fd4dcecSNicolai Stange 2049fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = { 2059fd4dcecSNicolai Stange .open = open_proxy_open, 2069fd4dcecSNicolai Stange }; 2079fd4dcecSNicolai Stange 20849d200deSNicolai Stange #define PROTO(args...) args 20949d200deSNicolai Stange #define ARGS(args...) args 21049d200deSNicolai Stange 21149d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ 21249d200deSNicolai Stange static ret_type full_proxy_ ## name(proto) \ 21349d200deSNicolai Stange { \ 21469d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); \ 215154b9d75SNicolai Stange const struct file_operations *real_fops; \ 21649d200deSNicolai Stange ret_type r; \ 21749d200deSNicolai Stange \ 21869d29f9eSNicolai Stange r = debugfs_file_get(dentry); \ 21969d29f9eSNicolai Stange if (unlikely(r)) \ 22069d29f9eSNicolai Stange return r; \ 221154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); \ 22249d200deSNicolai Stange r = real_fops->name(args); \ 22369d29f9eSNicolai Stange debugfs_file_put(dentry); \ 22449d200deSNicolai Stange return r; \ 22549d200deSNicolai Stange } 22649d200deSNicolai Stange 22749d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp, 22849d200deSNicolai Stange PROTO(struct file *filp, loff_t offset, int whence), 22949d200deSNicolai Stange ARGS(filp, offset, whence)); 23049d200deSNicolai Stange 23149d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp, 23249d200deSNicolai Stange PROTO(struct file *filp, char __user *buf, size_t size, 23349d200deSNicolai Stange loff_t *ppos), 23449d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 23549d200deSNicolai Stange 23649d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp, 23749d200deSNicolai Stange PROTO(struct file *filp, const char __user *buf, size_t size, 23849d200deSNicolai Stange loff_t *ppos), 23949d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 24049d200deSNicolai Stange 24149d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 24249d200deSNicolai Stange PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 24349d200deSNicolai Stange ARGS(filp, cmd, arg)); 24449d200deSNicolai Stange 245076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp, 24649d200deSNicolai Stange struct poll_table_struct *wait) 24749d200deSNicolai Stange { 24869d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 249e6c8adcaSAl Viro __poll_t r = 0; 250154b9d75SNicolai Stange const struct file_operations *real_fops; 25149d200deSNicolai Stange 25269d29f9eSNicolai Stange if (debugfs_file_get(dentry)) 253a9a08845SLinus Torvalds return EPOLLHUP; 25449d200deSNicolai Stange 255154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); 25649d200deSNicolai Stange r = real_fops->poll(filp, wait); 25769d29f9eSNicolai Stange debugfs_file_put(dentry); 25849d200deSNicolai Stange return r; 25949d200deSNicolai Stange } 26049d200deSNicolai Stange 26149d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp) 26249d200deSNicolai Stange { 26349d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 26486f0e067SChristian Lamparter const struct file_operations *real_fops = debugfs_real_fops(filp); 26549d200deSNicolai Stange const struct file_operations *proxy_fops = filp->f_op; 26649d200deSNicolai Stange int r = 0; 26749d200deSNicolai Stange 26849d200deSNicolai Stange /* 26949d200deSNicolai Stange * We must not protect this against removal races here: the 27049d200deSNicolai Stange * original releaser should be called unconditionally in order 27149d200deSNicolai Stange * not to leak any resources. Releasers must not assume that 27249d200deSNicolai Stange * ->i_private is still being meaningful here. 27349d200deSNicolai Stange */ 27449d200deSNicolai Stange if (real_fops->release) 27549d200deSNicolai Stange r = real_fops->release(inode, filp); 27649d200deSNicolai Stange 27749d200deSNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 278c80a67bdSXu Wang kfree(proxy_fops); 27949d200deSNicolai Stange fops_put(real_fops); 280a1a9e5d2SEric Engestrom return r; 28149d200deSNicolai Stange } 28249d200deSNicolai Stange 28349d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops, 28449d200deSNicolai Stange const struct file_operations *real_fops) 28549d200deSNicolai Stange { 28649d200deSNicolai Stange proxy_fops->release = full_proxy_release; 28749d200deSNicolai Stange if (real_fops->llseek) 28849d200deSNicolai Stange proxy_fops->llseek = full_proxy_llseek; 28949d200deSNicolai Stange if (real_fops->read) 29049d200deSNicolai Stange proxy_fops->read = full_proxy_read; 29149d200deSNicolai Stange if (real_fops->write) 29249d200deSNicolai Stange proxy_fops->write = full_proxy_write; 29349d200deSNicolai Stange if (real_fops->poll) 29449d200deSNicolai Stange proxy_fops->poll = full_proxy_poll; 29549d200deSNicolai Stange if (real_fops->unlocked_ioctl) 29649d200deSNicolai Stange proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; 29749d200deSNicolai Stange } 29849d200deSNicolai Stange 29949d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp) 30049d200deSNicolai Stange { 30169d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 30249d200deSNicolai Stange const struct file_operations *real_fops = NULL; 30349d200deSNicolai Stange struct file_operations *proxy_fops = NULL; 3047d39bc50SNicolai Stange int r; 30549d200deSNicolai Stange 3067d39bc50SNicolai Stange r = debugfs_file_get(dentry); 3077d39bc50SNicolai Stange if (r) 3087d39bc50SNicolai Stange return r == -EIO ? -ENOENT : r; 30949d200deSNicolai Stange 31086f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 3115496197fSDavid Howells 312a37f4958SEric Snowberg r = debugfs_locked_down(inode, filp, real_fops); 3135496197fSDavid Howells if (r) 3145496197fSDavid Howells goto out; 3155496197fSDavid Howells 316275678e7STaehee Yoo if (!fops_get(real_fops)) { 317e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES 318275678e7STaehee Yoo if (real_fops->owner && 319112cedc8SSven Eckelmann real_fops->owner->state == MODULE_STATE_GOING) { 320112cedc8SSven Eckelmann r = -ENXIO; 321275678e7STaehee Yoo goto out; 322112cedc8SSven Eckelmann } 323275678e7STaehee Yoo #endif 324275678e7STaehee Yoo 32549d200deSNicolai Stange /* Huh? Module did not cleanup after itself at exit? */ 32649d200deSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 32749d200deSNicolai Stange dentry); 32849d200deSNicolai Stange r = -ENXIO; 32949d200deSNicolai Stange goto out; 33049d200deSNicolai Stange } 33149d200deSNicolai Stange 33249d200deSNicolai Stange proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); 33349d200deSNicolai Stange if (!proxy_fops) { 33449d200deSNicolai Stange r = -ENOMEM; 33549d200deSNicolai Stange goto free_proxy; 33649d200deSNicolai Stange } 33749d200deSNicolai Stange __full_proxy_fops_init(proxy_fops, real_fops); 33849d200deSNicolai Stange replace_fops(filp, proxy_fops); 33949d200deSNicolai Stange 34049d200deSNicolai Stange if (real_fops->open) { 34149d200deSNicolai Stange r = real_fops->open(inode, filp); 342b10e3e90SNicolai Stange if (r) { 343b10e3e90SNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 344b10e3e90SNicolai Stange goto free_proxy; 345b10e3e90SNicolai Stange } else if (filp->f_op != proxy_fops) { 34649d200deSNicolai Stange /* No protection against file removal anymore. */ 34749d200deSNicolai Stange WARN(1, "debugfs file owner replaced proxy fops: %pd", 34849d200deSNicolai Stange dentry); 34949d200deSNicolai Stange goto free_proxy; 35049d200deSNicolai Stange } 35149d200deSNicolai Stange } 35249d200deSNicolai Stange 35349d200deSNicolai Stange goto out; 35449d200deSNicolai Stange free_proxy: 35549d200deSNicolai Stange kfree(proxy_fops); 35649d200deSNicolai Stange fops_put(real_fops); 35749d200deSNicolai Stange out: 35869d29f9eSNicolai Stange debugfs_file_put(dentry); 35949d200deSNicolai Stange return r; 36049d200deSNicolai Stange } 36149d200deSNicolai Stange 36249d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = { 36349d200deSNicolai Stange .open = full_proxy_open, 36449d200deSNicolai Stange }; 36549d200deSNicolai Stange 366c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf, 367c6468808SNicolai Stange size_t len, loff_t *ppos) 368c6468808SNicolai Stange { 36969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 370c6468808SNicolai Stange ssize_t ret; 371c6468808SNicolai Stange 37269d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 37369d29f9eSNicolai Stange if (unlikely(ret)) 37469d29f9eSNicolai Stange return ret; 375c6468808SNicolai Stange ret = simple_attr_read(file, buf, len, ppos); 37669d29f9eSNicolai Stange debugfs_file_put(dentry); 377c6468808SNicolai Stange return ret; 378c6468808SNicolai Stange } 379c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read); 380c6468808SNicolai Stange 381d472cf79SAkinobu Mita static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, 382d472cf79SAkinobu Mita size_t len, loff_t *ppos, bool is_signed) 383c6468808SNicolai Stange { 38469d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 385c6468808SNicolai Stange ssize_t ret; 386c6468808SNicolai Stange 38769d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 38869d29f9eSNicolai Stange if (unlikely(ret)) 38969d29f9eSNicolai Stange return ret; 390d472cf79SAkinobu Mita if (is_signed) 391d472cf79SAkinobu Mita ret = simple_attr_write_signed(file, buf, len, ppos); 392d472cf79SAkinobu Mita else 393c6468808SNicolai Stange ret = simple_attr_write(file, buf, len, ppos); 39469d29f9eSNicolai Stange debugfs_file_put(dentry); 395c6468808SNicolai Stange return ret; 396c6468808SNicolai Stange } 397d472cf79SAkinobu Mita 398d472cf79SAkinobu Mita ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 399d472cf79SAkinobu Mita size_t len, loff_t *ppos) 400d472cf79SAkinobu Mita { 401d472cf79SAkinobu Mita return debugfs_attr_write_xsigned(file, buf, len, ppos, false); 402d472cf79SAkinobu Mita } 403c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write); 404c6468808SNicolai Stange 405d472cf79SAkinobu Mita ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 406d472cf79SAkinobu Mita size_t len, loff_t *ppos) 407d472cf79SAkinobu Mita { 408d472cf79SAkinobu Mita return debugfs_attr_write_xsigned(file, buf, len, ppos, true); 409d472cf79SAkinobu Mita } 410d472cf79SAkinobu Mita EXPORT_SYMBOL_GPL(debugfs_attr_write_signed); 411d472cf79SAkinobu Mita 4124909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 4134909f168SNicolai Stange struct dentry *parent, void *value, 4144909f168SNicolai Stange const struct file_operations *fops, 4154909f168SNicolai Stange const struct file_operations *fops_ro, 4164909f168SNicolai Stange const struct file_operations *fops_wo) 4174909f168SNicolai Stange { 4184909f168SNicolai Stange /* if there are no write bits set, make read only */ 4194909f168SNicolai Stange if (!(mode & S_IWUGO)) 4204909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 4214909f168SNicolai Stange fops_ro); 4224909f168SNicolai Stange /* if there are no read bits set, make write only */ 4234909f168SNicolai Stange if (!(mode & S_IRUGO)) 4244909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 4254909f168SNicolai Stange fops_wo); 4264909f168SNicolai Stange 4274909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, fops); 4284909f168SNicolai Stange } 4294909f168SNicolai Stange 4308b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 431acaefc25SArnd Bergmann { 432acaefc25SArnd Bergmann *(u8 *)data = val; 4338b88b099SChristoph Hellwig return 0; 434acaefc25SArnd Bergmann } 4358b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 436acaefc25SArnd Bergmann { 4378b88b099SChristoph Hellwig *val = *(u8 *)data; 4388b88b099SChristoph Hellwig return 0; 439acaefc25SArnd Bergmann } 4404909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 4414909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 4424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /** 4456468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 4461da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4471da177e4SLinus Torvalds * @mode: the permission that the file should have 4481da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4496468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4501da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4511da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4521da177e4SLinus Torvalds * from. 4531da177e4SLinus Torvalds * 4541da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4551da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4561da177e4SLinus Torvalds * set, it can be read from, and written to. 4571da177e4SLinus Torvalds */ 4589655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 4599655ac4aSGreg Kroah-Hartman u8 *value) 4601da177e4SLinus Torvalds { 4619655ac4aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 462b97f6799SStephen Boyd &fops_u8_ro, &fops_u8_wo); 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 4651da177e4SLinus Torvalds 4668b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 467acaefc25SArnd Bergmann { 468acaefc25SArnd Bergmann *(u16 *)data = val; 4698b88b099SChristoph Hellwig return 0; 470acaefc25SArnd Bergmann } 4718b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 472acaefc25SArnd Bergmann { 4738b88b099SChristoph Hellwig *val = *(u16 *)data; 4748b88b099SChristoph Hellwig return 0; 475acaefc25SArnd Bergmann } 4764909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 4774909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 4784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 479acaefc25SArnd Bergmann 4801da177e4SLinus Torvalds /** 4816468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 4821da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4831da177e4SLinus Torvalds * @mode: the permission that the file should have 4841da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4856468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4861da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4871da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4881da177e4SLinus Torvalds * from. 4891da177e4SLinus Torvalds * 4901da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4911da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4921da177e4SLinus Torvalds * set, it can be read from, and written to. 4931da177e4SLinus Torvalds */ 494313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 495313f5dbbSGreg Kroah-Hartman u16 *value) 4961da177e4SLinus Torvalds { 497313f5dbbSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 498b97f6799SStephen Boyd &fops_u16_ro, &fops_u16_wo); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 5011da177e4SLinus Torvalds 5028b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 503acaefc25SArnd Bergmann { 504acaefc25SArnd Bergmann *(u32 *)data = val; 5058b88b099SChristoph Hellwig return 0; 506acaefc25SArnd Bergmann } 5078b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 508acaefc25SArnd Bergmann { 5098b88b099SChristoph Hellwig *val = *(u32 *)data; 5108b88b099SChristoph Hellwig return 0; 511acaefc25SArnd Bergmann } 5124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 5134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 5144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 515acaefc25SArnd Bergmann 5161da177e4SLinus Torvalds /** 5176468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 5181da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 5191da177e4SLinus Torvalds * @mode: the permission that the file should have 5201da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 5216468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 5221da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 5231da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 5241da177e4SLinus Torvalds * from. 5251da177e4SLinus Torvalds * 5261da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 5271da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 5281da177e4SLinus Torvalds * set, it can be read from, and written to. 5291da177e4SLinus Torvalds */ 5302b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 5312b07021aSGreg Kroah-Hartman u32 *value) 5321da177e4SLinus Torvalds { 5332b07021aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 534b97f6799SStephen Boyd &fops_u32_ro, &fops_u32_wo); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 5371da177e4SLinus Torvalds 5388b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 5398447891fSMichael Ellerman { 5408447891fSMichael Ellerman *(u64 *)data = val; 5418b88b099SChristoph Hellwig return 0; 5428447891fSMichael Ellerman } 5438447891fSMichael Ellerman 5448b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 5458447891fSMichael Ellerman { 5468b88b099SChristoph Hellwig *val = *(u64 *)data; 5478b88b099SChristoph Hellwig return 0; 5488447891fSMichael Ellerman } 5494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 5504909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 5514909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 5528447891fSMichael Ellerman 5538447891fSMichael Ellerman /** 5548447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 5558447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 5568447891fSMichael Ellerman * @mode: the permission that the file should have 5578447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 5588447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 5598447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 5608447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 5618447891fSMichael Ellerman * from. 5628447891fSMichael Ellerman * 5638447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 5648447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 5658447891fSMichael Ellerman * set, it can be read from, and written to. 5668447891fSMichael Ellerman */ 567ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 568ad26221fSGreg Kroah-Hartman u64 *value) 5698447891fSMichael Ellerman { 570ad26221fSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 571b97f6799SStephen Boyd &fops_u64_ro, &fops_u64_wo); 5728447891fSMichael Ellerman } 5738447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 5748447891fSMichael Ellerman 575c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val) 576c23fe831SViresh Kumar { 577c23fe831SViresh Kumar *(unsigned long *)data = val; 578c23fe831SViresh Kumar return 0; 579c23fe831SViresh Kumar } 580c23fe831SViresh Kumar 581c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val) 582c23fe831SViresh Kumar { 583c23fe831SViresh Kumar *val = *(unsigned long *)data; 584c23fe831SViresh Kumar return 0; 585c23fe831SViresh Kumar } 5864909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 5874909f168SNicolai Stange "%llu\n"); 5884909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 5894909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 590c23fe831SViresh Kumar 591c23fe831SViresh Kumar /** 592c23fe831SViresh Kumar * debugfs_create_ulong - create a debugfs file that is used to read and write 593c23fe831SViresh Kumar * an unsigned long value. 594c23fe831SViresh Kumar * @name: a pointer to a string containing the name of the file to create. 595c23fe831SViresh Kumar * @mode: the permission that the file should have 596c23fe831SViresh Kumar * @parent: a pointer to the parent dentry for this file. This should be a 597c23fe831SViresh Kumar * directory dentry if set. If this parameter is %NULL, then the 598c23fe831SViresh Kumar * file will be created in the root of the debugfs filesystem. 599c23fe831SViresh Kumar * @value: a pointer to the variable that the file should read to and write 600c23fe831SViresh Kumar * from. 601c23fe831SViresh Kumar * 602c23fe831SViresh Kumar * This function creates a file in debugfs with the given name that 603c23fe831SViresh Kumar * contains the value of the variable @value. If the @mode variable is so 604c23fe831SViresh Kumar * set, it can be read from, and written to. 605c23fe831SViresh Kumar */ 606fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 607fb05b14cSGreg Kroah-Hartman unsigned long *value) 608c23fe831SViresh Kumar { 609fb05b14cSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 610fb05b14cSGreg Kroah-Hartman &fops_ulong_ro, &fops_ulong_wo); 611c23fe831SViresh Kumar } 612c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong); 613c23fe831SViresh Kumar 6144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 6154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 6164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 6172ebefc50SRobin Getz 6184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 6194909f168SNicolai Stange "0x%04llx\n"); 6204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 6214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 6222ebefc50SRobin Getz 6234909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 6244909f168SNicolai Stange "0x%08llx\n"); 6254909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 6264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 6272ebefc50SRobin Getz 6284909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 6294909f168SNicolai Stange "0x%016llx\n"); 6304909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 6314909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 63215b0beaaSHuang Ying 633e6716b87SRandy Dunlap /* 63415b0beaaSHuang 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 635e6716b87SRandy Dunlap * 636e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 637e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 638e6716b87SRandy Dunlap * decimal functions. 639e6716b87SRandy Dunlap */ 640e6716b87SRandy Dunlap 6412ebefc50SRobin Getz /** 6422ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 643e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 644e6716b87SRandy Dunlap * @mode: the permission that the file should have 645e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 646e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 647e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 648e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 649e6716b87SRandy Dunlap * from. 6502ebefc50SRobin Getz */ 651c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 652c7c11689SGreg Kroah-Hartman u8 *value) 6532ebefc50SRobin Getz { 654c7c11689SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 655b97f6799SStephen Boyd &fops_x8_ro, &fops_x8_wo); 6562ebefc50SRobin Getz } 6572ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 6582ebefc50SRobin Getz 659e6716b87SRandy Dunlap /** 660e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 661e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 662e6716b87SRandy Dunlap * @mode: the permission that the file should have 663e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 664e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 665e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 666e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 667e6716b87SRandy Dunlap * from. 668e6716b87SRandy Dunlap */ 669e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 670e40d38f2SGreg Kroah-Hartman u16 *value) 6712ebefc50SRobin Getz { 672e40d38f2SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 673b97f6799SStephen Boyd &fops_x16_ro, &fops_x16_wo); 6742ebefc50SRobin Getz } 6752ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 6762ebefc50SRobin Getz 677e6716b87SRandy Dunlap /** 678e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 679e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 680e6716b87SRandy Dunlap * @mode: the permission that the file should have 681e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 682e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 683e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 684e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 685e6716b87SRandy Dunlap * from. 686e6716b87SRandy Dunlap */ 687f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 688f5cb0a7eSGreg Kroah-Hartman u32 *value) 6892ebefc50SRobin Getz { 690f5cb0a7eSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 691b97f6799SStephen Boyd &fops_x32_ro, &fops_x32_wo); 6922ebefc50SRobin Getz } 6932ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 6942ebefc50SRobin Getz 69515b0beaaSHuang Ying /** 69615b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 69715b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 69815b0beaaSHuang Ying * @mode: the permission that the file should have 69915b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 70015b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 70115b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 70215b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 70315b0beaaSHuang Ying * from. 70415b0beaaSHuang Ying */ 7050864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 7060864c408SGreg Kroah-Hartman u64 *value) 70715b0beaaSHuang Ying { 7080864c408SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 70982b7d4fbSStephen Boyd &fops_x64_ro, &fops_x64_wo); 71015b0beaaSHuang Ying } 71115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 71215b0beaaSHuang Ying 7135e078787SInaky Perez-Gonzalez 7145e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 7155e078787SInaky Perez-Gonzalez { 7165e078787SInaky Perez-Gonzalez *(size_t *)data = val; 7175e078787SInaky Perez-Gonzalez return 0; 7185e078787SInaky Perez-Gonzalez } 7195e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 7205e078787SInaky Perez-Gonzalez { 7215e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 7225e078787SInaky Perez-Gonzalez return 0; 7235e078787SInaky Perez-Gonzalez } 7244909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 7255e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 7264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 7274909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 7285e078787SInaky Perez-Gonzalez 7295e078787SInaky Perez-Gonzalez /** 7305e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 7315e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 7325e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 7335e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 7345e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 7355e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 7365e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 7375e078787SInaky Perez-Gonzalez * from. 7385e078787SInaky Perez-Gonzalez */ 7398e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode, 7405e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 7415e078787SInaky Perez-Gonzalez { 7428e580263SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 7438e580263SGreg Kroah-Hartman &fops_size_t_ro, &fops_size_t_wo); 7445e078787SInaky Perez-Gonzalez } 7455e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 7465e078787SInaky Perez-Gonzalez 7473a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val) 7483a76e5e0SSeth Jennings { 7493a76e5e0SSeth Jennings atomic_set((atomic_t *)data, val); 7503a76e5e0SSeth Jennings return 0; 7513a76e5e0SSeth Jennings } 7523a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val) 7533a76e5e0SSeth Jennings { 7543a76e5e0SSeth Jennings *val = atomic_read((atomic_t *)data); 7553a76e5e0SSeth Jennings return 0; 7563a76e5e0SSeth Jennings } 757d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, 7583a76e5e0SSeth Jennings debugfs_atomic_t_set, "%lld\n"); 759d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 7604909f168SNicolai Stange "%lld\n"); 761d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 7624909f168SNicolai Stange "%lld\n"); 7633a76e5e0SSeth Jennings 7643a76e5e0SSeth Jennings /** 7653a76e5e0SSeth Jennings * debugfs_create_atomic_t - create a debugfs file that is used to read and 7663a76e5e0SSeth Jennings * write an atomic_t value 7673a76e5e0SSeth Jennings * @name: a pointer to a string containing the name of the file to create. 7683a76e5e0SSeth Jennings * @mode: the permission that the file should have 7693a76e5e0SSeth Jennings * @parent: a pointer to the parent dentry for this file. This should be a 7703a76e5e0SSeth Jennings * directory dentry if set. If this parameter is %NULL, then the 7713a76e5e0SSeth Jennings * file will be created in the root of the debugfs filesystem. 7723a76e5e0SSeth Jennings * @value: a pointer to the variable that the file should read to and write 7733a76e5e0SSeth Jennings * from. 7743a76e5e0SSeth Jennings */ 7759927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode, 7763a76e5e0SSeth Jennings struct dentry *parent, atomic_t *value) 7773a76e5e0SSeth Jennings { 7789927c6faSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 7799927c6faSGreg Kroah-Hartman &fops_atomic_t_ro, &fops_atomic_t_wo); 7803a76e5e0SSeth Jennings } 7813a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 7825e078787SInaky Perez-Gonzalez 7830642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 7841da177e4SLinus Torvalds size_t count, loff_t *ppos) 7851da177e4SLinus Torvalds { 786c8a9c285SRasmus Villemoes char buf[2]; 7874d45f797SNicolai Stange bool val; 78869d29f9eSNicolai Stange int r; 78969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 7901da177e4SLinus Torvalds 79169d29f9eSNicolai Stange r = debugfs_file_get(dentry); 79269d29f9eSNicolai Stange if (unlikely(r)) 7934d45f797SNicolai Stange return r; 79469d29f9eSNicolai Stange val = *(bool *)file->private_data; 79569d29f9eSNicolai Stange debugfs_file_put(dentry); 7964d45f797SNicolai Stange 7974d45f797SNicolai Stange if (val) 7981da177e4SLinus Torvalds buf[0] = 'Y'; 7991da177e4SLinus Torvalds else 8001da177e4SLinus Torvalds buf[0] = 'N'; 8011da177e4SLinus Torvalds buf[1] = '\n'; 8021da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 8031da177e4SLinus Torvalds } 8040642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 8051da177e4SLinus Torvalds 8060642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 8071da177e4SLinus Torvalds size_t count, loff_t *ppos) 8081da177e4SLinus Torvalds { 8098705b48eSJonathan Cameron bool bv; 81069d29f9eSNicolai Stange int r; 811621a5f7aSViresh Kumar bool *val = file->private_data; 81269d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 8131da177e4SLinus Torvalds 814964f8363SAndy Shevchenko r = kstrtobool_from_user(user_buf, count, &bv); 815964f8363SAndy Shevchenko if (!r) { 81669d29f9eSNicolai Stange r = debugfs_file_get(dentry); 81769d29f9eSNicolai Stange if (unlikely(r)) 8184d45f797SNicolai Stange return r; 81969d29f9eSNicolai Stange *val = bv; 82069d29f9eSNicolai Stange debugfs_file_put(dentry); 8214d45f797SNicolai Stange } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds return count; 8241da177e4SLinus Torvalds } 8250642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 8261da177e4SLinus Torvalds 8274b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 8280642ef6fSRichard Fitzgerald .read = debugfs_read_file_bool, 8290642ef6fSRichard Fitzgerald .write = debugfs_write_file_bool, 830234e3405SStephen Boyd .open = simple_open, 8316038f373SArnd Bergmann .llseek = default_llseek, 8321da177e4SLinus Torvalds }; 8331da177e4SLinus Torvalds 8346713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = { 8356713e8fbSStephen Boyd .read = debugfs_read_file_bool, 8366713e8fbSStephen Boyd .open = simple_open, 8376713e8fbSStephen Boyd .llseek = default_llseek, 8386713e8fbSStephen Boyd }; 8396713e8fbSStephen Boyd 8406713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = { 8416713e8fbSStephen Boyd .write = debugfs_write_file_bool, 8426713e8fbSStephen Boyd .open = simple_open, 8436713e8fbSStephen Boyd .llseek = default_llseek, 8446713e8fbSStephen Boyd }; 8456713e8fbSStephen Boyd 8461da177e4SLinus Torvalds /** 8476468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 8481da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 8491da177e4SLinus Torvalds * @mode: the permission that the file should have 8501da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 8516468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 8521da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 8531da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 8541da177e4SLinus Torvalds * from. 8551da177e4SLinus Torvalds * 8561da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 8571da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 8581da177e4SLinus Torvalds * set, it can be read from, and written to. 8591da177e4SLinus Torvalds */ 860393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 861393b0638SGreg Kroah-Hartman bool *value) 8621da177e4SLinus Torvalds { 863393b0638SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 8646713e8fbSStephen Boyd &fops_bool_ro, &fops_bool_wo); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 8671da177e4SLinus Torvalds 8689af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 8699af0440eSPeter Zijlstra size_t count, loff_t *ppos) 8709af0440eSPeter Zijlstra { 8719af0440eSPeter Zijlstra struct dentry *dentry = F_DENTRY(file); 8729af0440eSPeter Zijlstra char *str, *copy = NULL; 8739af0440eSPeter Zijlstra int copy_len, len; 8749af0440eSPeter Zijlstra ssize_t ret; 8759af0440eSPeter Zijlstra 8769af0440eSPeter Zijlstra ret = debugfs_file_get(dentry); 8779af0440eSPeter Zijlstra if (unlikely(ret)) 8789af0440eSPeter Zijlstra return ret; 8799af0440eSPeter Zijlstra 8809af0440eSPeter Zijlstra str = *(char **)file->private_data; 8819af0440eSPeter Zijlstra len = strlen(str) + 1; 8829af0440eSPeter Zijlstra copy = kmalloc(len, GFP_KERNEL); 8839af0440eSPeter Zijlstra if (!copy) { 8849af0440eSPeter Zijlstra debugfs_file_put(dentry); 8859af0440eSPeter Zijlstra return -ENOMEM; 8869af0440eSPeter Zijlstra } 8879af0440eSPeter Zijlstra 8889af0440eSPeter Zijlstra copy_len = strscpy(copy, str, len); 8899af0440eSPeter Zijlstra debugfs_file_put(dentry); 8909af0440eSPeter Zijlstra if (copy_len < 0) { 8919af0440eSPeter Zijlstra kfree(copy); 8929af0440eSPeter Zijlstra return copy_len; 8939af0440eSPeter Zijlstra } 8949af0440eSPeter Zijlstra 8959af0440eSPeter Zijlstra copy[copy_len] = '\n'; 8969af0440eSPeter Zijlstra 897f501b6a2SDietmar Eggemann ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); 8989af0440eSPeter Zijlstra kfree(copy); 8999af0440eSPeter Zijlstra 9009af0440eSPeter Zijlstra return ret; 9019af0440eSPeter Zijlstra } 902d60b59b9SCristian Marussi EXPORT_SYMBOL_GPL(debugfs_create_str); 9039af0440eSPeter Zijlstra 9049af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, 9059af0440eSPeter Zijlstra size_t count, loff_t *ppos) 9069af0440eSPeter Zijlstra { 907*86b54881SMike Tipton struct dentry *dentry = F_DENTRY(file); 908*86b54881SMike Tipton char *old, *new = NULL; 909*86b54881SMike Tipton int pos = *ppos; 910*86b54881SMike Tipton int r; 911*86b54881SMike Tipton 912*86b54881SMike Tipton r = debugfs_file_get(dentry); 913*86b54881SMike Tipton if (unlikely(r)) 914*86b54881SMike Tipton return r; 915*86b54881SMike Tipton 916*86b54881SMike Tipton old = *(char **)file->private_data; 917*86b54881SMike Tipton 918*86b54881SMike Tipton /* only allow strict concatenation */ 919*86b54881SMike Tipton r = -EINVAL; 920*86b54881SMike Tipton if (pos && pos != strlen(old)) 921*86b54881SMike Tipton goto error; 922*86b54881SMike Tipton 923*86b54881SMike Tipton r = -E2BIG; 924*86b54881SMike Tipton if (pos + count + 1 > PAGE_SIZE) 925*86b54881SMike Tipton goto error; 926*86b54881SMike Tipton 927*86b54881SMike Tipton r = -ENOMEM; 928*86b54881SMike Tipton new = kmalloc(pos + count + 1, GFP_KERNEL); 929*86b54881SMike Tipton if (!new) 930*86b54881SMike Tipton goto error; 931*86b54881SMike Tipton 932*86b54881SMike Tipton if (pos) 933*86b54881SMike Tipton memcpy(new, old, pos); 934*86b54881SMike Tipton 935*86b54881SMike Tipton r = -EFAULT; 936*86b54881SMike Tipton if (copy_from_user(new + pos, user_buf, count)) 937*86b54881SMike Tipton goto error; 938*86b54881SMike Tipton 939*86b54881SMike Tipton new[pos + count] = '\0'; 940*86b54881SMike Tipton strim(new); 941*86b54881SMike Tipton 942*86b54881SMike Tipton rcu_assign_pointer(*(char **)file->private_data, new); 943*86b54881SMike Tipton synchronize_rcu(); 944*86b54881SMike Tipton kfree(old); 945*86b54881SMike Tipton 946*86b54881SMike Tipton debugfs_file_put(dentry); 947*86b54881SMike Tipton return count; 948*86b54881SMike Tipton 949*86b54881SMike Tipton error: 950*86b54881SMike Tipton kfree(new); 951*86b54881SMike Tipton debugfs_file_put(dentry); 952*86b54881SMike Tipton return r; 9539af0440eSPeter Zijlstra } 9549af0440eSPeter Zijlstra 9559af0440eSPeter Zijlstra static const struct file_operations fops_str = { 9569af0440eSPeter Zijlstra .read = debugfs_read_file_str, 9579af0440eSPeter Zijlstra .write = debugfs_write_file_str, 9589af0440eSPeter Zijlstra .open = simple_open, 9599af0440eSPeter Zijlstra .llseek = default_llseek, 9609af0440eSPeter Zijlstra }; 9619af0440eSPeter Zijlstra 9629af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = { 9639af0440eSPeter Zijlstra .read = debugfs_read_file_str, 9649af0440eSPeter Zijlstra .open = simple_open, 9659af0440eSPeter Zijlstra .llseek = default_llseek, 9669af0440eSPeter Zijlstra }; 9679af0440eSPeter Zijlstra 9689af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = { 9699af0440eSPeter Zijlstra .write = debugfs_write_file_str, 9709af0440eSPeter Zijlstra .open = simple_open, 9719af0440eSPeter Zijlstra .llseek = default_llseek, 9729af0440eSPeter Zijlstra }; 9739af0440eSPeter Zijlstra 9749af0440eSPeter Zijlstra /** 9759af0440eSPeter Zijlstra * debugfs_create_str - create a debugfs file that is used to read and write a string value 9769af0440eSPeter Zijlstra * @name: a pointer to a string containing the name of the file to create. 9779af0440eSPeter Zijlstra * @mode: the permission that the file should have 9789af0440eSPeter Zijlstra * @parent: a pointer to the parent dentry for this file. This should be a 9799af0440eSPeter Zijlstra * directory dentry if set. If this parameter is %NULL, then the 9809af0440eSPeter Zijlstra * file will be created in the root of the debugfs filesystem. 9819af0440eSPeter Zijlstra * @value: a pointer to the variable that the file should read to and write 9829af0440eSPeter Zijlstra * from. 9839af0440eSPeter Zijlstra * 9849af0440eSPeter Zijlstra * This function creates a file in debugfs with the given name that 9859af0440eSPeter Zijlstra * contains the value of the variable @value. If the @mode variable is so 9869af0440eSPeter Zijlstra * set, it can be read from, and written to. 9879af0440eSPeter Zijlstra */ 9889af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode, 9899af0440eSPeter Zijlstra struct dentry *parent, char **value) 9909af0440eSPeter Zijlstra { 9919af0440eSPeter Zijlstra debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 9929af0440eSPeter Zijlstra &fops_str_ro, &fops_str_wo); 9939af0440eSPeter Zijlstra } 9949af0440eSPeter Zijlstra 995dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 996dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 997dd308bc3SMichael Ellerman { 998dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 99969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 100083b711cbSNicolai Stange ssize_t r; 100183b711cbSNicolai Stange 100269d29f9eSNicolai Stange r = debugfs_file_get(dentry); 100369d29f9eSNicolai Stange if (unlikely(r)) 100469d29f9eSNicolai Stange return r; 100583b711cbSNicolai Stange r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 1006dd308bc3SMichael Ellerman blob->size); 100769d29f9eSNicolai Stange debugfs_file_put(dentry); 100883b711cbSNicolai Stange return r; 1009dd308bc3SMichael Ellerman } 1010dd308bc3SMichael Ellerman 101100977a59SArjan van de Ven static const struct file_operations fops_blob = { 1012dd308bc3SMichael Ellerman .read = read_file_blob, 1013234e3405SStephen Boyd .open = simple_open, 10146038f373SArnd Bergmann .llseek = default_llseek, 1015dd308bc3SMichael Ellerman }; 1016dd308bc3SMichael Ellerman 1017dd308bc3SMichael Ellerman /** 1018400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 1019dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 1020d616f56dSWolfram Sang * @mode: the read permission that the file should have (other permissions are 1021d616f56dSWolfram Sang * masked out) 1022dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 10236468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1024dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 1025dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 1026dd308bc3SMichael Ellerman * to the blob data and the size of the data. 1027dd308bc3SMichael Ellerman * 1028dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 1029dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 1030dd308bc3SMichael Ellerman * read from. Writing is not supported. 1031dd308bc3SMichael Ellerman * 1032dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 1033dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 1034dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 1035adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 10369abb2499SRonald Tschalär * returned. 1037dd308bc3SMichael Ellerman * 1038adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 10399abb2499SRonald Tschalär * be returned. 1040dd308bc3SMichael Ellerman */ 1041f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 1042dd308bc3SMichael Ellerman struct dentry *parent, 1043dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 1044dd308bc3SMichael Ellerman { 1045d616f56dSWolfram Sang return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob); 1046dd308bc3SMichael Ellerman } 1047dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 10481a087c6aSAlessandro Rubini 1049e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize, 1050e05e279eSLinus Torvalds u32 *array, int array_size) 10519fe2a701SSrivatsa Vaddagiri { 10529fe2a701SSrivatsa Vaddagiri size_t ret = 0; 10539fe2a701SSrivatsa Vaddagiri 1054e05e279eSLinus Torvalds while (--array_size >= 0) { 10559fe2a701SSrivatsa Vaddagiri size_t len; 1056e05e279eSLinus Torvalds char term = array_size ? ' ' : '\n'; 10579fe2a701SSrivatsa Vaddagiri 1058e05e279eSLinus Torvalds len = snprintf(buf, bufsize, "%u%c", *array++, term); 10599fe2a701SSrivatsa Vaddagiri ret += len; 10609fe2a701SSrivatsa Vaddagiri 10619fe2a701SSrivatsa Vaddagiri buf += len; 10629fe2a701SSrivatsa Vaddagiri bufsize -= len; 10639fe2a701SSrivatsa Vaddagiri } 10649fe2a701SSrivatsa Vaddagiri return ret; 10659fe2a701SSrivatsa Vaddagiri } 10669fe2a701SSrivatsa Vaddagiri 106736048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file) 10689fe2a701SSrivatsa Vaddagiri { 1069a2b992c8SJakub Kicinski struct debugfs_u32_array *data = inode->i_private; 1070a2b992c8SJakub Kicinski int size, elements = data->n_elements; 1071e05e279eSLinus Torvalds char *buf; 10729fe2a701SSrivatsa Vaddagiri 1073e05e279eSLinus Torvalds /* 1074e05e279eSLinus Torvalds * Max size: 1075e05e279eSLinus Torvalds * - 10 digits + ' '/'\n' = 11 bytes per number 1076e05e279eSLinus Torvalds * - terminating NUL character 1077e05e279eSLinus Torvalds */ 1078e05e279eSLinus Torvalds size = elements*11; 1079e05e279eSLinus Torvalds buf = kmalloc(size+1, GFP_KERNEL); 1080e05e279eSLinus Torvalds if (!buf) 108136048853SDavid Rientjes return -ENOMEM; 1082e05e279eSLinus Torvalds buf[size] = 0; 1083e05e279eSLinus Torvalds 1084e05e279eSLinus Torvalds file->private_data = buf; 1085a2b992c8SJakub Kicinski u32_format_array(buf, size, data->array, data->n_elements); 1086e05e279eSLinus Torvalds 108736048853SDavid Rientjes return nonseekable_open(inode, file); 10889fe2a701SSrivatsa Vaddagiri } 10899fe2a701SSrivatsa Vaddagiri 109036048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 109136048853SDavid Rientjes loff_t *ppos) 109236048853SDavid Rientjes { 109336048853SDavid Rientjes size_t size = strlen(file->private_data); 10949fe2a701SSrivatsa Vaddagiri 10959fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 10969fe2a701SSrivatsa Vaddagiri file->private_data, size); 10979fe2a701SSrivatsa Vaddagiri } 10989fe2a701SSrivatsa Vaddagiri 10999fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 11009fe2a701SSrivatsa Vaddagiri { 11019fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 11029fe2a701SSrivatsa Vaddagiri 11039fe2a701SSrivatsa Vaddagiri return 0; 11049fe2a701SSrivatsa Vaddagiri } 11059fe2a701SSrivatsa Vaddagiri 11069fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 11079fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 11089fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 11099fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 11109fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 11119fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 11129fe2a701SSrivatsa Vaddagiri }; 11139fe2a701SSrivatsa Vaddagiri 11149fe2a701SSrivatsa Vaddagiri /** 11159fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 11169fe2a701SSrivatsa Vaddagiri * array. 11179fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 11189fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 11199fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 11209fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 11219fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 1122a2b992c8SJakub Kicinski * @array: wrapper struct containing data pointer and size of the array. 11239fe2a701SSrivatsa Vaddagiri * 11249fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 11259fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 11269fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 11279fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 11289fe2a701SSrivatsa Vaddagiri */ 1129c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode, 1130a2b992c8SJakub Kicinski struct dentry *parent, 1131a2b992c8SJakub Kicinski struct debugfs_u32_array *array) 11329fe2a701SSrivatsa Vaddagiri { 1133a2b992c8SJakub Kicinski debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 11349fe2a701SSrivatsa Vaddagiri } 11359fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 11369fe2a701SSrivatsa Vaddagiri 11373b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 11383b85e4abSHeiko Carstens 11391a087c6aSAlessandro Rubini /* 11401a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 11411a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 11421a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 11431a087c6aSAlessandro Rubini */ 11441a087c6aSAlessandro Rubini 11451a087c6aSAlessandro Rubini /** 11461a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 11471a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 11481a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 1149b5763accSRandy Dunlap * @nregs: the length of the above array 11501a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 11511a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 11521a087c6aSAlessandro Rubini * 11531a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 11541a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 11551a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 11561a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 11571a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 11581a087c6aSAlessandro Rubini * for example configuration of dma channels 11591a087c6aSAlessandro Rubini */ 11609761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 11611a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 11621a087c6aSAlessandro Rubini { 11639761536eSJoe Perches int i; 11641a087c6aSAlessandro Rubini 11651a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 11661a087c6aSAlessandro Rubini if (prefix) 11679761536eSJoe Perches seq_printf(s, "%s", prefix); 11689761536eSJoe Perches seq_printf(s, "%s = 0x%08x\n", regs->name, 11695858441cSDan Carpenter readl(base + regs->offset)); 11709761536eSJoe Perches if (seq_has_overflowed(s)) 11719761536eSJoe Perches break; 11721a087c6aSAlessandro Rubini } 11731a087c6aSAlessandro Rubini } 11741a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 11751a087c6aSAlessandro Rubini 117619029f3fSChenXiaoSong static int debugfs_regset32_show(struct seq_file *s, void *data) 11771a087c6aSAlessandro Rubini { 11781a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 11791a087c6aSAlessandro Rubini 118030332eeeSGeert Uytterhoeven if (regset->dev) 118130332eeeSGeert Uytterhoeven pm_runtime_get_sync(regset->dev); 118230332eeeSGeert Uytterhoeven 11831a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 118430332eeeSGeert Uytterhoeven 118530332eeeSGeert Uytterhoeven if (regset->dev) 118630332eeeSGeert Uytterhoeven pm_runtime_put(regset->dev); 118730332eeeSGeert Uytterhoeven 11881a087c6aSAlessandro Rubini return 0; 11891a087c6aSAlessandro Rubini } 11901a087c6aSAlessandro Rubini 119119029f3fSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(debugfs_regset32); 11921a087c6aSAlessandro Rubini 11931a087c6aSAlessandro Rubini /** 11941a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 11951a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 11961a087c6aSAlessandro Rubini * @mode: the permission that the file should have 11971a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 11981a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 11991a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 12001a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 12011a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 12021a087c6aSAlessandro Rubini * address where the register bank is to be found. 12031a087c6aSAlessandro Rubini * 12041a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 12051a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 12061a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 12071a087c6aSAlessandro Rubini */ 1208ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode, 12091a087c6aSAlessandro Rubini struct dentry *parent, 12101a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 12111a087c6aSAlessandro Rubini { 121219029f3fSChenXiaoSong debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); 12131a087c6aSAlessandro Rubini } 12141a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 12153b85e4abSHeiko Carstens 12163b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 121798210b7fSArend van Spriel 121898210b7fSArend van Spriel struct debugfs_devm_entry { 121998210b7fSArend van Spriel int (*read)(struct seq_file *seq, void *data); 122098210b7fSArend van Spriel struct device *dev; 122198210b7fSArend van Spriel }; 122298210b7fSArend van Spriel 122398210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 122498210b7fSArend van Spriel { 122598210b7fSArend van Spriel struct debugfs_devm_entry *entry = inode->i_private; 122698210b7fSArend van Spriel 122798210b7fSArend van Spriel return single_open(f, entry->read, entry->dev); 122898210b7fSArend van Spriel } 122998210b7fSArend van Spriel 123098210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = { 123198210b7fSArend van Spriel .owner = THIS_MODULE, 123298210b7fSArend van Spriel .open = debugfs_devm_entry_open, 123398210b7fSArend van Spriel .release = single_release, 123498210b7fSArend van Spriel .read = seq_read, 123598210b7fSArend van Spriel .llseek = seq_lseek 123698210b7fSArend van Spriel }; 123798210b7fSArend van Spriel 123898210b7fSArend van Spriel /** 123998210b7fSArend van Spriel * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 124098210b7fSArend van Spriel * 124198210b7fSArend van Spriel * @dev: device related to this debugfs file. 124298210b7fSArend van Spriel * @name: name of the debugfs file. 124398210b7fSArend van Spriel * @parent: a pointer to the parent dentry for this file. This should be a 124498210b7fSArend van Spriel * directory dentry if set. If this parameter is %NULL, then the 124598210b7fSArend van Spriel * file will be created in the root of the debugfs filesystem. 124698210b7fSArend van Spriel * @read_fn: function pointer called to print the seq_file content. 124798210b7fSArend van Spriel */ 12480d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name, 124998210b7fSArend van Spriel struct dentry *parent, 12500d519cbfSGreg Kroah-Hartman int (*read_fn)(struct seq_file *s, void *data)) 125198210b7fSArend van Spriel { 125298210b7fSArend van Spriel struct debugfs_devm_entry *entry; 125398210b7fSArend van Spriel 125498210b7fSArend van Spriel if (IS_ERR(parent)) 12550d519cbfSGreg Kroah-Hartman return; 125698210b7fSArend van Spriel 125798210b7fSArend van Spriel entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 125898210b7fSArend van Spriel if (!entry) 12590d519cbfSGreg Kroah-Hartman return; 126098210b7fSArend van Spriel 126198210b7fSArend van Spriel entry->read = read_fn; 126298210b7fSArend van Spriel entry->dev = dev; 126398210b7fSArend van Spriel 12640d519cbfSGreg Kroah-Hartman debugfs_create_file(name, S_IRUGO, parent, entry, 126598210b7fSArend van Spriel &debugfs_devm_entry_ops); 126698210b7fSArend van Spriel } 126798210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1268