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 { 1505496197fSDavid Howells if ((inode->i_mode & 07777) == 0444 && 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 && 182275678e7STaehee Yoo real_fops->owner->state == MODULE_STATE_GOING) 183275678e7STaehee Yoo goto out; 184275678e7STaehee Yoo #endif 185275678e7STaehee Yoo 1869fd4dcecSNicolai Stange /* Huh? Module did not clean up after itself at exit? */ 1879fd4dcecSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 1889fd4dcecSNicolai Stange dentry); 1899fd4dcecSNicolai Stange r = -ENXIO; 1909fd4dcecSNicolai Stange goto out; 1919fd4dcecSNicolai Stange } 1929fd4dcecSNicolai Stange replace_fops(filp, real_fops); 1939fd4dcecSNicolai Stange 1949fd4dcecSNicolai Stange if (real_fops->open) 1959fd4dcecSNicolai Stange r = real_fops->open(inode, filp); 1969fd4dcecSNicolai Stange 1979fd4dcecSNicolai Stange out: 19869d29f9eSNicolai Stange debugfs_file_put(dentry); 1999fd4dcecSNicolai Stange return r; 2009fd4dcecSNicolai Stange } 2019fd4dcecSNicolai Stange 2029fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = { 2039fd4dcecSNicolai Stange .open = open_proxy_open, 2049fd4dcecSNicolai Stange }; 2059fd4dcecSNicolai Stange 20649d200deSNicolai Stange #define PROTO(args...) args 20749d200deSNicolai Stange #define ARGS(args...) args 20849d200deSNicolai Stange 20949d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ 21049d200deSNicolai Stange static ret_type full_proxy_ ## name(proto) \ 21149d200deSNicolai Stange { \ 21269d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); \ 213154b9d75SNicolai Stange const struct file_operations *real_fops; \ 21449d200deSNicolai Stange ret_type r; \ 21549d200deSNicolai Stange \ 21669d29f9eSNicolai Stange r = debugfs_file_get(dentry); \ 21769d29f9eSNicolai Stange if (unlikely(r)) \ 21869d29f9eSNicolai Stange return r; \ 219154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); \ 22049d200deSNicolai Stange r = real_fops->name(args); \ 22169d29f9eSNicolai Stange debugfs_file_put(dentry); \ 22249d200deSNicolai Stange return r; \ 22349d200deSNicolai Stange } 22449d200deSNicolai Stange 22549d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp, 22649d200deSNicolai Stange PROTO(struct file *filp, loff_t offset, int whence), 22749d200deSNicolai Stange ARGS(filp, offset, whence)); 22849d200deSNicolai Stange 22949d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp, 23049d200deSNicolai Stange PROTO(struct file *filp, char __user *buf, size_t size, 23149d200deSNicolai Stange loff_t *ppos), 23249d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 23349d200deSNicolai Stange 23449d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp, 23549d200deSNicolai Stange PROTO(struct file *filp, const char __user *buf, size_t size, 23649d200deSNicolai Stange loff_t *ppos), 23749d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 23849d200deSNicolai Stange 23949d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 24049d200deSNicolai Stange PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 24149d200deSNicolai Stange ARGS(filp, cmd, arg)); 24249d200deSNicolai Stange 243076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp, 24449d200deSNicolai Stange struct poll_table_struct *wait) 24549d200deSNicolai Stange { 24669d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 247e6c8adcaSAl Viro __poll_t r = 0; 248154b9d75SNicolai Stange const struct file_operations *real_fops; 24949d200deSNicolai Stange 25069d29f9eSNicolai Stange if (debugfs_file_get(dentry)) 251a9a08845SLinus Torvalds return EPOLLHUP; 25249d200deSNicolai Stange 253154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); 25449d200deSNicolai Stange r = real_fops->poll(filp, wait); 25569d29f9eSNicolai Stange debugfs_file_put(dentry); 25649d200deSNicolai Stange return r; 25749d200deSNicolai Stange } 25849d200deSNicolai Stange 25949d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp) 26049d200deSNicolai Stange { 26149d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 26286f0e067SChristian Lamparter const struct file_operations *real_fops = debugfs_real_fops(filp); 26349d200deSNicolai Stange const struct file_operations *proxy_fops = filp->f_op; 26449d200deSNicolai Stange int r = 0; 26549d200deSNicolai Stange 26649d200deSNicolai Stange /* 26749d200deSNicolai Stange * We must not protect this against removal races here: the 26849d200deSNicolai Stange * original releaser should be called unconditionally in order 26949d200deSNicolai Stange * not to leak any resources. Releasers must not assume that 27049d200deSNicolai Stange * ->i_private is still being meaningful here. 27149d200deSNicolai Stange */ 27249d200deSNicolai Stange if (real_fops->release) 27349d200deSNicolai Stange r = real_fops->release(inode, filp); 27449d200deSNicolai Stange 27549d200deSNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 276c80a67bdSXu Wang kfree(proxy_fops); 27749d200deSNicolai Stange fops_put(real_fops); 278a1a9e5d2SEric Engestrom return r; 27949d200deSNicolai Stange } 28049d200deSNicolai Stange 28149d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops, 28249d200deSNicolai Stange const struct file_operations *real_fops) 28349d200deSNicolai Stange { 28449d200deSNicolai Stange proxy_fops->release = full_proxy_release; 28549d200deSNicolai Stange if (real_fops->llseek) 28649d200deSNicolai Stange proxy_fops->llseek = full_proxy_llseek; 28749d200deSNicolai Stange if (real_fops->read) 28849d200deSNicolai Stange proxy_fops->read = full_proxy_read; 28949d200deSNicolai Stange if (real_fops->write) 29049d200deSNicolai Stange proxy_fops->write = full_proxy_write; 29149d200deSNicolai Stange if (real_fops->poll) 29249d200deSNicolai Stange proxy_fops->poll = full_proxy_poll; 29349d200deSNicolai Stange if (real_fops->unlocked_ioctl) 29449d200deSNicolai Stange proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; 29549d200deSNicolai Stange } 29649d200deSNicolai Stange 29749d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp) 29849d200deSNicolai Stange { 29969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 30049d200deSNicolai Stange const struct file_operations *real_fops = NULL; 30149d200deSNicolai Stange struct file_operations *proxy_fops = NULL; 3027d39bc50SNicolai Stange int r; 30349d200deSNicolai Stange 3047d39bc50SNicolai Stange r = debugfs_file_get(dentry); 3057d39bc50SNicolai Stange if (r) 3067d39bc50SNicolai Stange return r == -EIO ? -ENOENT : r; 30749d200deSNicolai Stange 30886f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 3095496197fSDavid Howells 310a37f4958SEric Snowberg r = debugfs_locked_down(inode, filp, real_fops); 3115496197fSDavid Howells if (r) 3125496197fSDavid Howells goto out; 3135496197fSDavid Howells 314275678e7STaehee Yoo if (!fops_get(real_fops)) { 315e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES 316275678e7STaehee Yoo if (real_fops->owner && 317275678e7STaehee Yoo real_fops->owner->state == MODULE_STATE_GOING) 318275678e7STaehee Yoo goto out; 319275678e7STaehee Yoo #endif 320275678e7STaehee Yoo 32149d200deSNicolai Stange /* Huh? Module did not cleanup after itself at exit? */ 32249d200deSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 32349d200deSNicolai Stange dentry); 32449d200deSNicolai Stange r = -ENXIO; 32549d200deSNicolai Stange goto out; 32649d200deSNicolai Stange } 32749d200deSNicolai Stange 32849d200deSNicolai Stange proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); 32949d200deSNicolai Stange if (!proxy_fops) { 33049d200deSNicolai Stange r = -ENOMEM; 33149d200deSNicolai Stange goto free_proxy; 33249d200deSNicolai Stange } 33349d200deSNicolai Stange __full_proxy_fops_init(proxy_fops, real_fops); 33449d200deSNicolai Stange replace_fops(filp, proxy_fops); 33549d200deSNicolai Stange 33649d200deSNicolai Stange if (real_fops->open) { 33749d200deSNicolai Stange r = real_fops->open(inode, filp); 338b10e3e90SNicolai Stange if (r) { 339b10e3e90SNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 340b10e3e90SNicolai Stange goto free_proxy; 341b10e3e90SNicolai Stange } else if (filp->f_op != proxy_fops) { 34249d200deSNicolai Stange /* No protection against file removal anymore. */ 34349d200deSNicolai Stange WARN(1, "debugfs file owner replaced proxy fops: %pd", 34449d200deSNicolai Stange dentry); 34549d200deSNicolai Stange goto free_proxy; 34649d200deSNicolai Stange } 34749d200deSNicolai Stange } 34849d200deSNicolai Stange 34949d200deSNicolai Stange goto out; 35049d200deSNicolai Stange free_proxy: 35149d200deSNicolai Stange kfree(proxy_fops); 35249d200deSNicolai Stange fops_put(real_fops); 35349d200deSNicolai Stange out: 35469d29f9eSNicolai Stange debugfs_file_put(dentry); 35549d200deSNicolai Stange return r; 35649d200deSNicolai Stange } 35749d200deSNicolai Stange 35849d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = { 35949d200deSNicolai Stange .open = full_proxy_open, 36049d200deSNicolai Stange }; 36149d200deSNicolai Stange 362c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf, 363c6468808SNicolai Stange size_t len, loff_t *ppos) 364c6468808SNicolai Stange { 36569d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 366c6468808SNicolai Stange ssize_t ret; 367c6468808SNicolai Stange 36869d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 36969d29f9eSNicolai Stange if (unlikely(ret)) 37069d29f9eSNicolai Stange return ret; 371c6468808SNicolai Stange ret = simple_attr_read(file, buf, len, ppos); 37269d29f9eSNicolai Stange debugfs_file_put(dentry); 373c6468808SNicolai Stange return ret; 374c6468808SNicolai Stange } 375c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read); 376c6468808SNicolai Stange 377c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 378c6468808SNicolai Stange size_t len, loff_t *ppos) 379c6468808SNicolai Stange { 38069d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 381c6468808SNicolai Stange ssize_t ret; 382c6468808SNicolai Stange 38369d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 38469d29f9eSNicolai Stange if (unlikely(ret)) 38569d29f9eSNicolai Stange return ret; 386c6468808SNicolai Stange ret = simple_attr_write(file, buf, len, ppos); 38769d29f9eSNicolai Stange debugfs_file_put(dentry); 388c6468808SNicolai Stange return ret; 389c6468808SNicolai Stange } 390c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write); 391c6468808SNicolai Stange 3924909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 3934909f168SNicolai Stange struct dentry *parent, void *value, 3944909f168SNicolai Stange const struct file_operations *fops, 3954909f168SNicolai Stange const struct file_operations *fops_ro, 3964909f168SNicolai Stange const struct file_operations *fops_wo) 3974909f168SNicolai Stange { 3984909f168SNicolai Stange /* if there are no write bits set, make read only */ 3994909f168SNicolai Stange if (!(mode & S_IWUGO)) 4004909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 4014909f168SNicolai Stange fops_ro); 4024909f168SNicolai Stange /* if there are no read bits set, make write only */ 4034909f168SNicolai Stange if (!(mode & S_IRUGO)) 4044909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 4054909f168SNicolai Stange fops_wo); 4064909f168SNicolai Stange 4074909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, fops); 4084909f168SNicolai Stange } 4094909f168SNicolai Stange 4108b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 411acaefc25SArnd Bergmann { 412acaefc25SArnd Bergmann *(u8 *)data = val; 4138b88b099SChristoph Hellwig return 0; 414acaefc25SArnd Bergmann } 4158b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 416acaefc25SArnd Bergmann { 4178b88b099SChristoph Hellwig *val = *(u8 *)data; 4188b88b099SChristoph Hellwig return 0; 419acaefc25SArnd Bergmann } 4204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 4214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 4224909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /** 4256468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 4261da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4271da177e4SLinus Torvalds * @mode: the permission that the file should have 4281da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4296468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4301da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4311da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4321da177e4SLinus Torvalds * from. 4331da177e4SLinus Torvalds * 4341da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4351da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4361da177e4SLinus Torvalds * set, it can be read from, and written to. 4371da177e4SLinus Torvalds */ 4389655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 4399655ac4aSGreg Kroah-Hartman u8 *value) 4401da177e4SLinus Torvalds { 4419655ac4aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 442b97f6799SStephen Boyd &fops_u8_ro, &fops_u8_wo); 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 4451da177e4SLinus Torvalds 4468b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 447acaefc25SArnd Bergmann { 448acaefc25SArnd Bergmann *(u16 *)data = val; 4498b88b099SChristoph Hellwig return 0; 450acaefc25SArnd Bergmann } 4518b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 452acaefc25SArnd Bergmann { 4538b88b099SChristoph Hellwig *val = *(u16 *)data; 4548b88b099SChristoph Hellwig return 0; 455acaefc25SArnd Bergmann } 4564909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 4574909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 4584909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 459acaefc25SArnd Bergmann 4601da177e4SLinus Torvalds /** 4616468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 4621da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4631da177e4SLinus Torvalds * @mode: the permission that the file should have 4641da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4656468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4661da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4671da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4681da177e4SLinus Torvalds * from. 4691da177e4SLinus Torvalds * 4701da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4711da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4721da177e4SLinus Torvalds * set, it can be read from, and written to. 4731da177e4SLinus Torvalds */ 474313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 475313f5dbbSGreg Kroah-Hartman u16 *value) 4761da177e4SLinus Torvalds { 477313f5dbbSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 478b97f6799SStephen Boyd &fops_u16_ro, &fops_u16_wo); 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 4811da177e4SLinus Torvalds 4828b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 483acaefc25SArnd Bergmann { 484acaefc25SArnd Bergmann *(u32 *)data = val; 4858b88b099SChristoph Hellwig return 0; 486acaefc25SArnd Bergmann } 4878b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 488acaefc25SArnd Bergmann { 4898b88b099SChristoph Hellwig *val = *(u32 *)data; 4908b88b099SChristoph Hellwig return 0; 491acaefc25SArnd Bergmann } 4924909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 4934909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 4944909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 495acaefc25SArnd Bergmann 4961da177e4SLinus Torvalds /** 4976468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 4981da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4991da177e4SLinus Torvalds * @mode: the permission that the file should have 5001da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 5016468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 5021da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 5031da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 5041da177e4SLinus Torvalds * from. 5051da177e4SLinus Torvalds * 5061da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 5071da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 5081da177e4SLinus Torvalds * set, it can be read from, and written to. 5091da177e4SLinus Torvalds */ 5102b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 5112b07021aSGreg Kroah-Hartman u32 *value) 5121da177e4SLinus Torvalds { 5132b07021aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 514b97f6799SStephen Boyd &fops_u32_ro, &fops_u32_wo); 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 5171da177e4SLinus Torvalds 5188b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 5198447891fSMichael Ellerman { 5208447891fSMichael Ellerman *(u64 *)data = val; 5218b88b099SChristoph Hellwig return 0; 5228447891fSMichael Ellerman } 5238447891fSMichael Ellerman 5248b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 5258447891fSMichael Ellerman { 5268b88b099SChristoph Hellwig *val = *(u64 *)data; 5278b88b099SChristoph Hellwig return 0; 5288447891fSMichael Ellerman } 5294909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 5304909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 5314909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 5328447891fSMichael Ellerman 5338447891fSMichael Ellerman /** 5348447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 5358447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 5368447891fSMichael Ellerman * @mode: the permission that the file should have 5378447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 5388447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 5398447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 5408447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 5418447891fSMichael Ellerman * from. 5428447891fSMichael Ellerman * 5438447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 5448447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 5458447891fSMichael Ellerman * set, it can be read from, and written to. 5468447891fSMichael Ellerman */ 547ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 548ad26221fSGreg Kroah-Hartman u64 *value) 5498447891fSMichael Ellerman { 550ad26221fSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 551b97f6799SStephen Boyd &fops_u64_ro, &fops_u64_wo); 5528447891fSMichael Ellerman } 5538447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 5548447891fSMichael Ellerman 555c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val) 556c23fe831SViresh Kumar { 557c23fe831SViresh Kumar *(unsigned long *)data = val; 558c23fe831SViresh Kumar return 0; 559c23fe831SViresh Kumar } 560c23fe831SViresh Kumar 561c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val) 562c23fe831SViresh Kumar { 563c23fe831SViresh Kumar *val = *(unsigned long *)data; 564c23fe831SViresh Kumar return 0; 565c23fe831SViresh Kumar } 5664909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 5674909f168SNicolai Stange "%llu\n"); 5684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 5694909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 570c23fe831SViresh Kumar 571c23fe831SViresh Kumar /** 572c23fe831SViresh Kumar * debugfs_create_ulong - create a debugfs file that is used to read and write 573c23fe831SViresh Kumar * an unsigned long value. 574c23fe831SViresh Kumar * @name: a pointer to a string containing the name of the file to create. 575c23fe831SViresh Kumar * @mode: the permission that the file should have 576c23fe831SViresh Kumar * @parent: a pointer to the parent dentry for this file. This should be a 577c23fe831SViresh Kumar * directory dentry if set. If this parameter is %NULL, then the 578c23fe831SViresh Kumar * file will be created in the root of the debugfs filesystem. 579c23fe831SViresh Kumar * @value: a pointer to the variable that the file should read to and write 580c23fe831SViresh Kumar * from. 581c23fe831SViresh Kumar * 582c23fe831SViresh Kumar * This function creates a file in debugfs with the given name that 583c23fe831SViresh Kumar * contains the value of the variable @value. If the @mode variable is so 584c23fe831SViresh Kumar * set, it can be read from, and written to. 585c23fe831SViresh Kumar */ 586*fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 587*fb05b14cSGreg Kroah-Hartman unsigned long *value) 588c23fe831SViresh Kumar { 589*fb05b14cSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 590*fb05b14cSGreg Kroah-Hartman &fops_ulong_ro, &fops_ulong_wo); 591c23fe831SViresh Kumar } 592c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong); 593c23fe831SViresh Kumar 5944909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 5954909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 5964909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 5972ebefc50SRobin Getz 5984909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 5994909f168SNicolai Stange "0x%04llx\n"); 6004909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 6014909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 6022ebefc50SRobin Getz 6034909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 6044909f168SNicolai Stange "0x%08llx\n"); 6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 6072ebefc50SRobin Getz 6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 6094909f168SNicolai Stange "0x%016llx\n"); 6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 6114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 61215b0beaaSHuang Ying 613e6716b87SRandy Dunlap /* 61415b0beaaSHuang 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 615e6716b87SRandy Dunlap * 616e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 617e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 618e6716b87SRandy Dunlap * decimal functions. 619e6716b87SRandy Dunlap */ 620e6716b87SRandy Dunlap 6212ebefc50SRobin Getz /** 6222ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 623e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 624e6716b87SRandy Dunlap * @mode: the permission that the file should have 625e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 626e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 627e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 628e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 629e6716b87SRandy Dunlap * from. 6302ebefc50SRobin Getz */ 631c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 632c7c11689SGreg Kroah-Hartman u8 *value) 6332ebefc50SRobin Getz { 634c7c11689SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 635b97f6799SStephen Boyd &fops_x8_ro, &fops_x8_wo); 6362ebefc50SRobin Getz } 6372ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 6382ebefc50SRobin Getz 639e6716b87SRandy Dunlap /** 640e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 641e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 642e6716b87SRandy Dunlap * @mode: the permission that the file should have 643e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 644e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 645e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 646e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 647e6716b87SRandy Dunlap * from. 648e6716b87SRandy Dunlap */ 649e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 650e40d38f2SGreg Kroah-Hartman u16 *value) 6512ebefc50SRobin Getz { 652e40d38f2SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 653b97f6799SStephen Boyd &fops_x16_ro, &fops_x16_wo); 6542ebefc50SRobin Getz } 6552ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 6562ebefc50SRobin Getz 657e6716b87SRandy Dunlap /** 658e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 659e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 660e6716b87SRandy Dunlap * @mode: the permission that the file should have 661e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 662e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 663e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 664e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 665e6716b87SRandy Dunlap * from. 666e6716b87SRandy Dunlap */ 667f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 668f5cb0a7eSGreg Kroah-Hartman u32 *value) 6692ebefc50SRobin Getz { 670f5cb0a7eSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 671b97f6799SStephen Boyd &fops_x32_ro, &fops_x32_wo); 6722ebefc50SRobin Getz } 6732ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 6742ebefc50SRobin Getz 67515b0beaaSHuang Ying /** 67615b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 67715b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 67815b0beaaSHuang Ying * @mode: the permission that the file should have 67915b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 68015b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 68115b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 68215b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 68315b0beaaSHuang Ying * from. 68415b0beaaSHuang Ying */ 6850864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 6860864c408SGreg Kroah-Hartman u64 *value) 68715b0beaaSHuang Ying { 6880864c408SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 68982b7d4fbSStephen Boyd &fops_x64_ro, &fops_x64_wo); 69015b0beaaSHuang Ying } 69115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 69215b0beaaSHuang Ying 6935e078787SInaky Perez-Gonzalez 6945e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 6955e078787SInaky Perez-Gonzalez { 6965e078787SInaky Perez-Gonzalez *(size_t *)data = val; 6975e078787SInaky Perez-Gonzalez return 0; 6985e078787SInaky Perez-Gonzalez } 6995e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 7005e078787SInaky Perez-Gonzalez { 7015e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 7025e078787SInaky Perez-Gonzalez return 0; 7035e078787SInaky Perez-Gonzalez } 7044909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 7055e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 7064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 7074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 7085e078787SInaky Perez-Gonzalez 7095e078787SInaky Perez-Gonzalez /** 7105e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 7115e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 7125e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 7135e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 7145e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 7155e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 7165e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 7175e078787SInaky Perez-Gonzalez * from. 7185e078787SInaky Perez-Gonzalez */ 7198e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode, 7205e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 7215e078787SInaky Perez-Gonzalez { 7228e580263SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 7238e580263SGreg Kroah-Hartman &fops_size_t_ro, &fops_size_t_wo); 7245e078787SInaky Perez-Gonzalez } 7255e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 7265e078787SInaky Perez-Gonzalez 7273a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val) 7283a76e5e0SSeth Jennings { 7293a76e5e0SSeth Jennings atomic_set((atomic_t *)data, val); 7303a76e5e0SSeth Jennings return 0; 7313a76e5e0SSeth Jennings } 7323a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val) 7333a76e5e0SSeth Jennings { 7343a76e5e0SSeth Jennings *val = atomic_read((atomic_t *)data); 7353a76e5e0SSeth Jennings return 0; 7363a76e5e0SSeth Jennings } 7374909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, 7383a76e5e0SSeth Jennings debugfs_atomic_t_set, "%lld\n"); 7394909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 7404909f168SNicolai Stange "%lld\n"); 7414909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 7424909f168SNicolai Stange "%lld\n"); 7433a76e5e0SSeth Jennings 7443a76e5e0SSeth Jennings /** 7453a76e5e0SSeth Jennings * debugfs_create_atomic_t - create a debugfs file that is used to read and 7463a76e5e0SSeth Jennings * write an atomic_t value 7473a76e5e0SSeth Jennings * @name: a pointer to a string containing the name of the file to create. 7483a76e5e0SSeth Jennings * @mode: the permission that the file should have 7493a76e5e0SSeth Jennings * @parent: a pointer to the parent dentry for this file. This should be a 7503a76e5e0SSeth Jennings * directory dentry if set. If this parameter is %NULL, then the 7513a76e5e0SSeth Jennings * file will be created in the root of the debugfs filesystem. 7523a76e5e0SSeth Jennings * @value: a pointer to the variable that the file should read to and write 7533a76e5e0SSeth Jennings * from. 7543a76e5e0SSeth Jennings */ 7559927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode, 7563a76e5e0SSeth Jennings struct dentry *parent, atomic_t *value) 7573a76e5e0SSeth Jennings { 7589927c6faSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 7599927c6faSGreg Kroah-Hartman &fops_atomic_t_ro, &fops_atomic_t_wo); 7603a76e5e0SSeth Jennings } 7613a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 7625e078787SInaky Perez-Gonzalez 7630642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 7641da177e4SLinus Torvalds size_t count, loff_t *ppos) 7651da177e4SLinus Torvalds { 766c8a9c285SRasmus Villemoes char buf[2]; 7674d45f797SNicolai Stange bool val; 76869d29f9eSNicolai Stange int r; 76969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 7701da177e4SLinus Torvalds 77169d29f9eSNicolai Stange r = debugfs_file_get(dentry); 77269d29f9eSNicolai Stange if (unlikely(r)) 7734d45f797SNicolai Stange return r; 77469d29f9eSNicolai Stange val = *(bool *)file->private_data; 77569d29f9eSNicolai Stange debugfs_file_put(dentry); 7764d45f797SNicolai Stange 7774d45f797SNicolai Stange if (val) 7781da177e4SLinus Torvalds buf[0] = 'Y'; 7791da177e4SLinus Torvalds else 7801da177e4SLinus Torvalds buf[0] = 'N'; 7811da177e4SLinus Torvalds buf[1] = '\n'; 7821da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 7831da177e4SLinus Torvalds } 7840642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 7851da177e4SLinus Torvalds 7860642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 7871da177e4SLinus Torvalds size_t count, loff_t *ppos) 7881da177e4SLinus Torvalds { 7898705b48eSJonathan Cameron bool bv; 79069d29f9eSNicolai Stange int r; 791621a5f7aSViresh Kumar bool *val = file->private_data; 79269d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 7931da177e4SLinus Torvalds 794964f8363SAndy Shevchenko r = kstrtobool_from_user(user_buf, count, &bv); 795964f8363SAndy Shevchenko if (!r) { 79669d29f9eSNicolai Stange r = debugfs_file_get(dentry); 79769d29f9eSNicolai Stange if (unlikely(r)) 7984d45f797SNicolai Stange return r; 79969d29f9eSNicolai Stange *val = bv; 80069d29f9eSNicolai Stange debugfs_file_put(dentry); 8014d45f797SNicolai Stange } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds return count; 8041da177e4SLinus Torvalds } 8050642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 8061da177e4SLinus Torvalds 8074b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 8080642ef6fSRichard Fitzgerald .read = debugfs_read_file_bool, 8090642ef6fSRichard Fitzgerald .write = debugfs_write_file_bool, 810234e3405SStephen Boyd .open = simple_open, 8116038f373SArnd Bergmann .llseek = default_llseek, 8121da177e4SLinus Torvalds }; 8131da177e4SLinus Torvalds 8146713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = { 8156713e8fbSStephen Boyd .read = debugfs_read_file_bool, 8166713e8fbSStephen Boyd .open = simple_open, 8176713e8fbSStephen Boyd .llseek = default_llseek, 8186713e8fbSStephen Boyd }; 8196713e8fbSStephen Boyd 8206713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = { 8216713e8fbSStephen Boyd .write = debugfs_write_file_bool, 8226713e8fbSStephen Boyd .open = simple_open, 8236713e8fbSStephen Boyd .llseek = default_llseek, 8246713e8fbSStephen Boyd }; 8256713e8fbSStephen Boyd 8261da177e4SLinus Torvalds /** 8276468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 8281da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 8291da177e4SLinus Torvalds * @mode: the permission that the file should have 8301da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 8316468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 8321da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 8331da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 8341da177e4SLinus Torvalds * from. 8351da177e4SLinus Torvalds * 8361da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 8371da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 8381da177e4SLinus Torvalds * set, it can be read from, and written to. 8391da177e4SLinus Torvalds */ 840393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 841393b0638SGreg Kroah-Hartman bool *value) 8421da177e4SLinus Torvalds { 843393b0638SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 8446713e8fbSStephen Boyd &fops_bool_ro, &fops_bool_wo); 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 8471da177e4SLinus Torvalds 8489af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 8499af0440eSPeter Zijlstra size_t count, loff_t *ppos) 8509af0440eSPeter Zijlstra { 8519af0440eSPeter Zijlstra struct dentry *dentry = F_DENTRY(file); 8529af0440eSPeter Zijlstra char *str, *copy = NULL; 8539af0440eSPeter Zijlstra int copy_len, len; 8549af0440eSPeter Zijlstra ssize_t ret; 8559af0440eSPeter Zijlstra 8569af0440eSPeter Zijlstra ret = debugfs_file_get(dentry); 8579af0440eSPeter Zijlstra if (unlikely(ret)) 8589af0440eSPeter Zijlstra return ret; 8599af0440eSPeter Zijlstra 8609af0440eSPeter Zijlstra str = *(char **)file->private_data; 8619af0440eSPeter Zijlstra len = strlen(str) + 1; 8629af0440eSPeter Zijlstra copy = kmalloc(len, GFP_KERNEL); 8639af0440eSPeter Zijlstra if (!copy) { 8649af0440eSPeter Zijlstra debugfs_file_put(dentry); 8659af0440eSPeter Zijlstra return -ENOMEM; 8669af0440eSPeter Zijlstra } 8679af0440eSPeter Zijlstra 8689af0440eSPeter Zijlstra copy_len = strscpy(copy, str, len); 8699af0440eSPeter Zijlstra debugfs_file_put(dentry); 8709af0440eSPeter Zijlstra if (copy_len < 0) { 8719af0440eSPeter Zijlstra kfree(copy); 8729af0440eSPeter Zijlstra return copy_len; 8739af0440eSPeter Zijlstra } 8749af0440eSPeter Zijlstra 8759af0440eSPeter Zijlstra copy[copy_len] = '\n'; 8769af0440eSPeter Zijlstra 8779af0440eSPeter Zijlstra ret = simple_read_from_buffer(user_buf, count, ppos, copy, copy_len); 8789af0440eSPeter Zijlstra kfree(copy); 8799af0440eSPeter Zijlstra 8809af0440eSPeter Zijlstra return ret; 8819af0440eSPeter Zijlstra } 8829af0440eSPeter Zijlstra 8839af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, 8849af0440eSPeter Zijlstra size_t count, loff_t *ppos) 8859af0440eSPeter Zijlstra { 8869af0440eSPeter Zijlstra /* This is really only for read-only strings */ 8879af0440eSPeter Zijlstra return -EINVAL; 8889af0440eSPeter Zijlstra } 8899af0440eSPeter Zijlstra 8909af0440eSPeter Zijlstra static const struct file_operations fops_str = { 8919af0440eSPeter Zijlstra .read = debugfs_read_file_str, 8929af0440eSPeter Zijlstra .write = debugfs_write_file_str, 8939af0440eSPeter Zijlstra .open = simple_open, 8949af0440eSPeter Zijlstra .llseek = default_llseek, 8959af0440eSPeter Zijlstra }; 8969af0440eSPeter Zijlstra 8979af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = { 8989af0440eSPeter Zijlstra .read = debugfs_read_file_str, 8999af0440eSPeter Zijlstra .open = simple_open, 9009af0440eSPeter Zijlstra .llseek = default_llseek, 9019af0440eSPeter Zijlstra }; 9029af0440eSPeter Zijlstra 9039af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = { 9049af0440eSPeter Zijlstra .write = debugfs_write_file_str, 9059af0440eSPeter Zijlstra .open = simple_open, 9069af0440eSPeter Zijlstra .llseek = default_llseek, 9079af0440eSPeter Zijlstra }; 9089af0440eSPeter Zijlstra 9099af0440eSPeter Zijlstra /** 9109af0440eSPeter Zijlstra * debugfs_create_str - create a debugfs file that is used to read and write a string value 9119af0440eSPeter Zijlstra * @name: a pointer to a string containing the name of the file to create. 9129af0440eSPeter Zijlstra * @mode: the permission that the file should have 9139af0440eSPeter Zijlstra * @parent: a pointer to the parent dentry for this file. This should be a 9149af0440eSPeter Zijlstra * directory dentry if set. If this parameter is %NULL, then the 9159af0440eSPeter Zijlstra * file will be created in the root of the debugfs filesystem. 9169af0440eSPeter Zijlstra * @value: a pointer to the variable that the file should read to and write 9179af0440eSPeter Zijlstra * from. 9189af0440eSPeter Zijlstra * 9199af0440eSPeter Zijlstra * This function creates a file in debugfs with the given name that 9209af0440eSPeter Zijlstra * contains the value of the variable @value. If the @mode variable is so 9219af0440eSPeter Zijlstra * set, it can be read from, and written to. 9229af0440eSPeter Zijlstra * 9239af0440eSPeter Zijlstra * This function will return a pointer to a dentry if it succeeds. This 9249af0440eSPeter Zijlstra * pointer must be passed to the debugfs_remove() function when the file is 9259af0440eSPeter Zijlstra * to be removed (no automatic cleanup happens if your module is unloaded, 9269af0440eSPeter Zijlstra * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 9279af0440eSPeter Zijlstra * returned. 9289af0440eSPeter Zijlstra * 9299af0440eSPeter Zijlstra * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 9309af0440eSPeter Zijlstra * be returned. 9319af0440eSPeter Zijlstra */ 9329af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode, 9339af0440eSPeter Zijlstra struct dentry *parent, char **value) 9349af0440eSPeter Zijlstra { 9359af0440eSPeter Zijlstra debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 9369af0440eSPeter Zijlstra &fops_str_ro, &fops_str_wo); 9379af0440eSPeter Zijlstra } 9389af0440eSPeter Zijlstra 939dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 940dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 941dd308bc3SMichael Ellerman { 942dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 94369d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 94483b711cbSNicolai Stange ssize_t r; 94583b711cbSNicolai Stange 94669d29f9eSNicolai Stange r = debugfs_file_get(dentry); 94769d29f9eSNicolai Stange if (unlikely(r)) 94869d29f9eSNicolai Stange return r; 94983b711cbSNicolai Stange r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 950dd308bc3SMichael Ellerman blob->size); 95169d29f9eSNicolai Stange debugfs_file_put(dentry); 95283b711cbSNicolai Stange return r; 953dd308bc3SMichael Ellerman } 954dd308bc3SMichael Ellerman 95500977a59SArjan van de Ven static const struct file_operations fops_blob = { 956dd308bc3SMichael Ellerman .read = read_file_blob, 957234e3405SStephen Boyd .open = simple_open, 9586038f373SArnd Bergmann .llseek = default_llseek, 959dd308bc3SMichael Ellerman }; 960dd308bc3SMichael Ellerman 961dd308bc3SMichael Ellerman /** 962400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 963dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 964d616f56dSWolfram Sang * @mode: the read permission that the file should have (other permissions are 965d616f56dSWolfram Sang * masked out) 966dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 9676468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 968dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 969dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 970dd308bc3SMichael Ellerman * to the blob data and the size of the data. 971dd308bc3SMichael Ellerman * 972dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 973dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 974dd308bc3SMichael Ellerman * read from. Writing is not supported. 975dd308bc3SMichael Ellerman * 976dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 977dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 978dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 979adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 9809abb2499SRonald Tschalär * returned. 981dd308bc3SMichael Ellerman * 982adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 9839abb2499SRonald Tschalär * be returned. 984dd308bc3SMichael Ellerman */ 985f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 986dd308bc3SMichael Ellerman struct dentry *parent, 987dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 988dd308bc3SMichael Ellerman { 989d616f56dSWolfram Sang return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob); 990dd308bc3SMichael Ellerman } 991dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 9921a087c6aSAlessandro Rubini 993e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize, 994e05e279eSLinus Torvalds u32 *array, int array_size) 9959fe2a701SSrivatsa Vaddagiri { 9969fe2a701SSrivatsa Vaddagiri size_t ret = 0; 9979fe2a701SSrivatsa Vaddagiri 998e05e279eSLinus Torvalds while (--array_size >= 0) { 9999fe2a701SSrivatsa Vaddagiri size_t len; 1000e05e279eSLinus Torvalds char term = array_size ? ' ' : '\n'; 10019fe2a701SSrivatsa Vaddagiri 1002e05e279eSLinus Torvalds len = snprintf(buf, bufsize, "%u%c", *array++, term); 10039fe2a701SSrivatsa Vaddagiri ret += len; 10049fe2a701SSrivatsa Vaddagiri 10059fe2a701SSrivatsa Vaddagiri buf += len; 10069fe2a701SSrivatsa Vaddagiri bufsize -= len; 10079fe2a701SSrivatsa Vaddagiri } 10089fe2a701SSrivatsa Vaddagiri return ret; 10099fe2a701SSrivatsa Vaddagiri } 10109fe2a701SSrivatsa Vaddagiri 101136048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file) 10129fe2a701SSrivatsa Vaddagiri { 1013a2b992c8SJakub Kicinski struct debugfs_u32_array *data = inode->i_private; 1014a2b992c8SJakub Kicinski int size, elements = data->n_elements; 1015e05e279eSLinus Torvalds char *buf; 10169fe2a701SSrivatsa Vaddagiri 1017e05e279eSLinus Torvalds /* 1018e05e279eSLinus Torvalds * Max size: 1019e05e279eSLinus Torvalds * - 10 digits + ' '/'\n' = 11 bytes per number 1020e05e279eSLinus Torvalds * - terminating NUL character 1021e05e279eSLinus Torvalds */ 1022e05e279eSLinus Torvalds size = elements*11; 1023e05e279eSLinus Torvalds buf = kmalloc(size+1, GFP_KERNEL); 1024e05e279eSLinus Torvalds if (!buf) 102536048853SDavid Rientjes return -ENOMEM; 1026e05e279eSLinus Torvalds buf[size] = 0; 1027e05e279eSLinus Torvalds 1028e05e279eSLinus Torvalds file->private_data = buf; 1029a2b992c8SJakub Kicinski u32_format_array(buf, size, data->array, data->n_elements); 1030e05e279eSLinus Torvalds 103136048853SDavid Rientjes return nonseekable_open(inode, file); 10329fe2a701SSrivatsa Vaddagiri } 10339fe2a701SSrivatsa Vaddagiri 103436048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 103536048853SDavid Rientjes loff_t *ppos) 103636048853SDavid Rientjes { 103736048853SDavid Rientjes size_t size = strlen(file->private_data); 10389fe2a701SSrivatsa Vaddagiri 10399fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 10409fe2a701SSrivatsa Vaddagiri file->private_data, size); 10419fe2a701SSrivatsa Vaddagiri } 10429fe2a701SSrivatsa Vaddagiri 10439fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 10449fe2a701SSrivatsa Vaddagiri { 10459fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 10469fe2a701SSrivatsa Vaddagiri 10479fe2a701SSrivatsa Vaddagiri return 0; 10489fe2a701SSrivatsa Vaddagiri } 10499fe2a701SSrivatsa Vaddagiri 10509fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 10519fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 10529fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 10539fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 10549fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 10559fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 10569fe2a701SSrivatsa Vaddagiri }; 10579fe2a701SSrivatsa Vaddagiri 10589fe2a701SSrivatsa Vaddagiri /** 10599fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 10609fe2a701SSrivatsa Vaddagiri * array. 10619fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 10629fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 10639fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 10649fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 10659fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 1066a2b992c8SJakub Kicinski * @array: wrapper struct containing data pointer and size of the array. 10679fe2a701SSrivatsa Vaddagiri * 10689fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 10699fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 10709fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 10719fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 10729fe2a701SSrivatsa Vaddagiri */ 1073c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode, 1074a2b992c8SJakub Kicinski struct dentry *parent, 1075a2b992c8SJakub Kicinski struct debugfs_u32_array *array) 10769fe2a701SSrivatsa Vaddagiri { 1077a2b992c8SJakub Kicinski debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 10789fe2a701SSrivatsa Vaddagiri } 10799fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 10809fe2a701SSrivatsa Vaddagiri 10813b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 10823b85e4abSHeiko Carstens 10831a087c6aSAlessandro Rubini /* 10841a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 10851a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 10861a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 10871a087c6aSAlessandro Rubini */ 10881a087c6aSAlessandro Rubini 10891a087c6aSAlessandro Rubini /** 10901a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 10911a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 10921a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 1093b5763accSRandy Dunlap * @nregs: the length of the above array 10941a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 10951a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 10961a087c6aSAlessandro Rubini * 10971a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 10981a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 10991a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 11001a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 11011a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 11021a087c6aSAlessandro Rubini * for example configuration of dma channels 11031a087c6aSAlessandro Rubini */ 11049761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 11051a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 11061a087c6aSAlessandro Rubini { 11079761536eSJoe Perches int i; 11081a087c6aSAlessandro Rubini 11091a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 11101a087c6aSAlessandro Rubini if (prefix) 11119761536eSJoe Perches seq_printf(s, "%s", prefix); 11129761536eSJoe Perches seq_printf(s, "%s = 0x%08x\n", regs->name, 11135858441cSDan Carpenter readl(base + regs->offset)); 11149761536eSJoe Perches if (seq_has_overflowed(s)) 11159761536eSJoe Perches break; 11161a087c6aSAlessandro Rubini } 11171a087c6aSAlessandro Rubini } 11181a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 11191a087c6aSAlessandro Rubini 11201a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 11211a087c6aSAlessandro Rubini { 11221a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 11231a087c6aSAlessandro Rubini 112430332eeeSGeert Uytterhoeven if (regset->dev) 112530332eeeSGeert Uytterhoeven pm_runtime_get_sync(regset->dev); 112630332eeeSGeert Uytterhoeven 11271a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 112830332eeeSGeert Uytterhoeven 112930332eeeSGeert Uytterhoeven if (regset->dev) 113030332eeeSGeert Uytterhoeven pm_runtime_put(regset->dev); 113130332eeeSGeert Uytterhoeven 11321a087c6aSAlessandro Rubini return 0; 11331a087c6aSAlessandro Rubini } 11341a087c6aSAlessandro Rubini 11351a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 11361a087c6aSAlessandro Rubini { 11371a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 11381a087c6aSAlessandro Rubini } 11391a087c6aSAlessandro Rubini 11401a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 11411a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 11421a087c6aSAlessandro Rubini .read = seq_read, 11431a087c6aSAlessandro Rubini .llseek = seq_lseek, 11441a087c6aSAlessandro Rubini .release = single_release, 11451a087c6aSAlessandro Rubini }; 11461a087c6aSAlessandro Rubini 11471a087c6aSAlessandro Rubini /** 11481a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 11491a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 11501a087c6aSAlessandro Rubini * @mode: the permission that the file should have 11511a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 11521a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 11531a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 11541a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 11551a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 11561a087c6aSAlessandro Rubini * address where the register bank is to be found. 11571a087c6aSAlessandro Rubini * 11581a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 11591a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 11601a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 11611a087c6aSAlessandro Rubini */ 1162ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode, 11631a087c6aSAlessandro Rubini struct dentry *parent, 11641a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 11651a087c6aSAlessandro Rubini { 1166ae91c925SGreg Kroah-Hartman debugfs_create_file(name, mode, parent, regset, &fops_regset32); 11671a087c6aSAlessandro Rubini } 11681a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 11693b85e4abSHeiko Carstens 11703b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 117198210b7fSArend van Spriel 117298210b7fSArend van Spriel struct debugfs_devm_entry { 117398210b7fSArend van Spriel int (*read)(struct seq_file *seq, void *data); 117498210b7fSArend van Spriel struct device *dev; 117598210b7fSArend van Spriel }; 117698210b7fSArend van Spriel 117798210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 117898210b7fSArend van Spriel { 117998210b7fSArend van Spriel struct debugfs_devm_entry *entry = inode->i_private; 118098210b7fSArend van Spriel 118198210b7fSArend van Spriel return single_open(f, entry->read, entry->dev); 118298210b7fSArend van Spriel } 118398210b7fSArend van Spriel 118498210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = { 118598210b7fSArend van Spriel .owner = THIS_MODULE, 118698210b7fSArend van Spriel .open = debugfs_devm_entry_open, 118798210b7fSArend van Spriel .release = single_release, 118898210b7fSArend van Spriel .read = seq_read, 118998210b7fSArend van Spriel .llseek = seq_lseek 119098210b7fSArend van Spriel }; 119198210b7fSArend van Spriel 119298210b7fSArend van Spriel /** 119398210b7fSArend van Spriel * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 119498210b7fSArend van Spriel * 119598210b7fSArend van Spriel * @dev: device related to this debugfs file. 119698210b7fSArend van Spriel * @name: name of the debugfs file. 119798210b7fSArend van Spriel * @parent: a pointer to the parent dentry for this file. This should be a 119898210b7fSArend van Spriel * directory dentry if set. If this parameter is %NULL, then the 119998210b7fSArend van Spriel * file will be created in the root of the debugfs filesystem. 120098210b7fSArend van Spriel * @read_fn: function pointer called to print the seq_file content. 120198210b7fSArend van Spriel */ 12020d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name, 120398210b7fSArend van Spriel struct dentry *parent, 12040d519cbfSGreg Kroah-Hartman int (*read_fn)(struct seq_file *s, void *data)) 120598210b7fSArend van Spriel { 120698210b7fSArend van Spriel struct debugfs_devm_entry *entry; 120798210b7fSArend van Spriel 120898210b7fSArend van Spriel if (IS_ERR(parent)) 12090d519cbfSGreg Kroah-Hartman return; 121098210b7fSArend van Spriel 121198210b7fSArend van Spriel entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 121298210b7fSArend van Spriel if (!entry) 12130d519cbfSGreg Kroah-Hartman return; 121498210b7fSArend van Spriel 121598210b7fSArend van Spriel entry->read = read_fn; 121698210b7fSArend van Spriel entry->dev = dev; 121798210b7fSArend van Spriel 12180d519cbfSGreg Kroah-Hartman debugfs_create_file(name, S_IRUGO, parent, entry, 121998210b7fSArend van Spriel &debugfs_devm_entry_ops); 122098210b7fSArend van Spriel } 122198210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1222