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)) { 180*e3b9fc7eSVladis 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)) { 315*e3b9fc7eSVladis 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 * 586c23fe831SViresh Kumar * This function will return a pointer to a dentry if it succeeds. This 587c23fe831SViresh Kumar * pointer must be passed to the debugfs_remove() function when the file is 588c23fe831SViresh Kumar * to be removed (no automatic cleanup happens if your module is unloaded, 589adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 5909abb2499SRonald Tschalär * returned. 591c23fe831SViresh Kumar * 592adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 5939abb2499SRonald Tschalär * be returned. 594c23fe831SViresh Kumar */ 595c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode, 596c23fe831SViresh Kumar struct dentry *parent, unsigned long *value) 597c23fe831SViresh Kumar { 5984909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, 5994909f168SNicolai Stange &fops_ulong, &fops_ulong_ro, 6004909f168SNicolai Stange &fops_ulong_wo); 601c23fe831SViresh Kumar } 602c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong); 603c23fe831SViresh Kumar 6044909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 6072ebefc50SRobin Getz 6084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 6094909f168SNicolai Stange "0x%04llx\n"); 6104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 6114909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 6122ebefc50SRobin Getz 6134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 6144909f168SNicolai Stange "0x%08llx\n"); 6154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 6164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 6172ebefc50SRobin Getz 6184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 6194909f168SNicolai Stange "0x%016llx\n"); 6204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 6214909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 62215b0beaaSHuang Ying 623e6716b87SRandy Dunlap /* 62415b0beaaSHuang 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 625e6716b87SRandy Dunlap * 626e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 627e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 628e6716b87SRandy Dunlap * decimal functions. 629e6716b87SRandy Dunlap */ 630e6716b87SRandy Dunlap 6312ebefc50SRobin Getz /** 6322ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 633e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 634e6716b87SRandy Dunlap * @mode: the permission that the file should have 635e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 636e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 637e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 638e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 639e6716b87SRandy Dunlap * from. 6402ebefc50SRobin Getz */ 641c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 642c7c11689SGreg Kroah-Hartman u8 *value) 6432ebefc50SRobin Getz { 644c7c11689SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 645b97f6799SStephen Boyd &fops_x8_ro, &fops_x8_wo); 6462ebefc50SRobin Getz } 6472ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 6482ebefc50SRobin Getz 649e6716b87SRandy Dunlap /** 650e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 651e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 652e6716b87SRandy Dunlap * @mode: the permission that the file should have 653e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 654e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 655e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 656e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 657e6716b87SRandy Dunlap * from. 658e6716b87SRandy Dunlap */ 659e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 660e40d38f2SGreg Kroah-Hartman u16 *value) 6612ebefc50SRobin Getz { 662e40d38f2SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 663b97f6799SStephen Boyd &fops_x16_ro, &fops_x16_wo); 6642ebefc50SRobin Getz } 6652ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 6662ebefc50SRobin Getz 667e6716b87SRandy Dunlap /** 668e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 669e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 670e6716b87SRandy Dunlap * @mode: the permission that the file should have 671e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 672e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 673e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 674e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 675e6716b87SRandy Dunlap * from. 676e6716b87SRandy Dunlap */ 677f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 678f5cb0a7eSGreg Kroah-Hartman u32 *value) 6792ebefc50SRobin Getz { 680f5cb0a7eSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 681b97f6799SStephen Boyd &fops_x32_ro, &fops_x32_wo); 6822ebefc50SRobin Getz } 6832ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 6842ebefc50SRobin Getz 68515b0beaaSHuang Ying /** 68615b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 68715b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 68815b0beaaSHuang Ying * @mode: the permission that the file should have 68915b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 69015b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 69115b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 69215b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 69315b0beaaSHuang Ying * from. 69415b0beaaSHuang Ying */ 6950864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 6960864c408SGreg Kroah-Hartman u64 *value) 69715b0beaaSHuang Ying { 6980864c408SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 69982b7d4fbSStephen Boyd &fops_x64_ro, &fops_x64_wo); 70015b0beaaSHuang Ying } 70115b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 70215b0beaaSHuang Ying 7035e078787SInaky Perez-Gonzalez 7045e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 7055e078787SInaky Perez-Gonzalez { 7065e078787SInaky Perez-Gonzalez *(size_t *)data = val; 7075e078787SInaky Perez-Gonzalez return 0; 7085e078787SInaky Perez-Gonzalez } 7095e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 7105e078787SInaky Perez-Gonzalez { 7115e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 7125e078787SInaky Perez-Gonzalez return 0; 7135e078787SInaky Perez-Gonzalez } 7144909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 7155e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 7164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 7174909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 7185e078787SInaky Perez-Gonzalez 7195e078787SInaky Perez-Gonzalez /** 7205e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 7215e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 7225e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 7235e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 7245e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 7255e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 7265e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 7275e078787SInaky Perez-Gonzalez * from. 7285e078787SInaky Perez-Gonzalez */ 7298e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode, 7305e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 7315e078787SInaky Perez-Gonzalez { 7328e580263SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 7338e580263SGreg Kroah-Hartman &fops_size_t_ro, &fops_size_t_wo); 7345e078787SInaky Perez-Gonzalez } 7355e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 7365e078787SInaky Perez-Gonzalez 7373a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val) 7383a76e5e0SSeth Jennings { 7393a76e5e0SSeth Jennings atomic_set((atomic_t *)data, val); 7403a76e5e0SSeth Jennings return 0; 7413a76e5e0SSeth Jennings } 7423a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val) 7433a76e5e0SSeth Jennings { 7443a76e5e0SSeth Jennings *val = atomic_read((atomic_t *)data); 7453a76e5e0SSeth Jennings return 0; 7463a76e5e0SSeth Jennings } 7474909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, 7483a76e5e0SSeth Jennings debugfs_atomic_t_set, "%lld\n"); 7494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 7504909f168SNicolai Stange "%lld\n"); 7514909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 7524909f168SNicolai Stange "%lld\n"); 7533a76e5e0SSeth Jennings 7543a76e5e0SSeth Jennings /** 7553a76e5e0SSeth Jennings * debugfs_create_atomic_t - create a debugfs file that is used to read and 7563a76e5e0SSeth Jennings * write an atomic_t value 7573a76e5e0SSeth Jennings * @name: a pointer to a string containing the name of the file to create. 7583a76e5e0SSeth Jennings * @mode: the permission that the file should have 7593a76e5e0SSeth Jennings * @parent: a pointer to the parent dentry for this file. This should be a 7603a76e5e0SSeth Jennings * directory dentry if set. If this parameter is %NULL, then the 7613a76e5e0SSeth Jennings * file will be created in the root of the debugfs filesystem. 7623a76e5e0SSeth Jennings * @value: a pointer to the variable that the file should read to and write 7633a76e5e0SSeth Jennings * from. 7643a76e5e0SSeth Jennings */ 7659927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode, 7663a76e5e0SSeth Jennings struct dentry *parent, atomic_t *value) 7673a76e5e0SSeth Jennings { 7689927c6faSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 7699927c6faSGreg Kroah-Hartman &fops_atomic_t_ro, &fops_atomic_t_wo); 7703a76e5e0SSeth Jennings } 7713a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 7725e078787SInaky Perez-Gonzalez 7730642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 7741da177e4SLinus Torvalds size_t count, loff_t *ppos) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds char buf[3]; 7774d45f797SNicolai Stange bool val; 77869d29f9eSNicolai Stange int r; 77969d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 7801da177e4SLinus Torvalds 78169d29f9eSNicolai Stange r = debugfs_file_get(dentry); 78269d29f9eSNicolai Stange if (unlikely(r)) 7834d45f797SNicolai Stange return r; 78469d29f9eSNicolai Stange val = *(bool *)file->private_data; 78569d29f9eSNicolai Stange debugfs_file_put(dentry); 7864d45f797SNicolai Stange 7874d45f797SNicolai Stange if (val) 7881da177e4SLinus Torvalds buf[0] = 'Y'; 7891da177e4SLinus Torvalds else 7901da177e4SLinus Torvalds buf[0] = 'N'; 7911da177e4SLinus Torvalds buf[1] = '\n'; 7921da177e4SLinus Torvalds buf[2] = 0x00; 7931da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 7941da177e4SLinus Torvalds } 7950642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 7961da177e4SLinus Torvalds 7970642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 7981da177e4SLinus Torvalds size_t count, loff_t *ppos) 7991da177e4SLinus Torvalds { 8008705b48eSJonathan Cameron bool bv; 80169d29f9eSNicolai Stange int r; 802621a5f7aSViresh Kumar bool *val = file->private_data; 80369d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 8041da177e4SLinus Torvalds 805964f8363SAndy Shevchenko r = kstrtobool_from_user(user_buf, count, &bv); 806964f8363SAndy Shevchenko if (!r) { 80769d29f9eSNicolai Stange r = debugfs_file_get(dentry); 80869d29f9eSNicolai Stange if (unlikely(r)) 8094d45f797SNicolai Stange return r; 81069d29f9eSNicolai Stange *val = bv; 81169d29f9eSNicolai Stange debugfs_file_put(dentry); 8124d45f797SNicolai Stange } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds return count; 8151da177e4SLinus Torvalds } 8160642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 8171da177e4SLinus Torvalds 8184b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 8190642ef6fSRichard Fitzgerald .read = debugfs_read_file_bool, 8200642ef6fSRichard Fitzgerald .write = debugfs_write_file_bool, 821234e3405SStephen Boyd .open = simple_open, 8226038f373SArnd Bergmann .llseek = default_llseek, 8231da177e4SLinus Torvalds }; 8241da177e4SLinus Torvalds 8256713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = { 8266713e8fbSStephen Boyd .read = debugfs_read_file_bool, 8276713e8fbSStephen Boyd .open = simple_open, 8286713e8fbSStephen Boyd .llseek = default_llseek, 8296713e8fbSStephen Boyd }; 8306713e8fbSStephen Boyd 8316713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = { 8326713e8fbSStephen Boyd .write = debugfs_write_file_bool, 8336713e8fbSStephen Boyd .open = simple_open, 8346713e8fbSStephen Boyd .llseek = default_llseek, 8356713e8fbSStephen Boyd }; 8366713e8fbSStephen Boyd 8371da177e4SLinus Torvalds /** 8386468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 8391da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 8401da177e4SLinus Torvalds * @mode: the permission that the file should have 8411da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 8426468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 8431da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 8441da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 8451da177e4SLinus Torvalds * from. 8461da177e4SLinus Torvalds * 8471da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 8481da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 8491da177e4SLinus Torvalds * set, it can be read from, and written to. 8501da177e4SLinus Torvalds * 8511da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 8521da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 8531da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 854adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 8559abb2499SRonald Tschalär * returned. 8561da177e4SLinus Torvalds * 857adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 8589abb2499SRonald Tschalär * be returned. 8591da177e4SLinus Torvalds */ 860f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode, 861621a5f7aSViresh Kumar struct dentry *parent, bool *value) 8621da177e4SLinus Torvalds { 8634d45f797SNicolai Stange return 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 868dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 869dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 870dd308bc3SMichael Ellerman { 871dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 87269d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 87383b711cbSNicolai Stange ssize_t r; 87483b711cbSNicolai Stange 87569d29f9eSNicolai Stange r = debugfs_file_get(dentry); 87669d29f9eSNicolai Stange if (unlikely(r)) 87769d29f9eSNicolai Stange return r; 87883b711cbSNicolai Stange r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 879dd308bc3SMichael Ellerman blob->size); 88069d29f9eSNicolai Stange debugfs_file_put(dentry); 88183b711cbSNicolai Stange return r; 882dd308bc3SMichael Ellerman } 883dd308bc3SMichael Ellerman 88400977a59SArjan van de Ven static const struct file_operations fops_blob = { 885dd308bc3SMichael Ellerman .read = read_file_blob, 886234e3405SStephen Boyd .open = simple_open, 8876038f373SArnd Bergmann .llseek = default_llseek, 888dd308bc3SMichael Ellerman }; 889dd308bc3SMichael Ellerman 890dd308bc3SMichael Ellerman /** 891400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 892dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 893dd308bc3SMichael Ellerman * @mode: the permission that the file should have 894dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 8956468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 896dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 897dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 898dd308bc3SMichael Ellerman * to the blob data and the size of the data. 899dd308bc3SMichael Ellerman * 900dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 901dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 902dd308bc3SMichael Ellerman * read from. Writing is not supported. 903dd308bc3SMichael Ellerman * 904dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 905dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 906dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 907adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 9089abb2499SRonald Tschalär * returned. 909dd308bc3SMichael Ellerman * 910adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 9119abb2499SRonald Tschalär * be returned. 912dd308bc3SMichael Ellerman */ 913f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 914dd308bc3SMichael Ellerman struct dentry *parent, 915dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 916dd308bc3SMichael Ellerman { 91783b711cbSNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob); 918dd308bc3SMichael Ellerman } 919dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 9201a087c6aSAlessandro Rubini 921e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize, 922e05e279eSLinus Torvalds u32 *array, int array_size) 9239fe2a701SSrivatsa Vaddagiri { 9249fe2a701SSrivatsa Vaddagiri size_t ret = 0; 9259fe2a701SSrivatsa Vaddagiri 926e05e279eSLinus Torvalds while (--array_size >= 0) { 9279fe2a701SSrivatsa Vaddagiri size_t len; 928e05e279eSLinus Torvalds char term = array_size ? ' ' : '\n'; 9299fe2a701SSrivatsa Vaddagiri 930e05e279eSLinus Torvalds len = snprintf(buf, bufsize, "%u%c", *array++, term); 9319fe2a701SSrivatsa Vaddagiri ret += len; 9329fe2a701SSrivatsa Vaddagiri 9339fe2a701SSrivatsa Vaddagiri buf += len; 9349fe2a701SSrivatsa Vaddagiri bufsize -= len; 9359fe2a701SSrivatsa Vaddagiri } 9369fe2a701SSrivatsa Vaddagiri return ret; 9379fe2a701SSrivatsa Vaddagiri } 9389fe2a701SSrivatsa Vaddagiri 93936048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file) 9409fe2a701SSrivatsa Vaddagiri { 941a2b992c8SJakub Kicinski struct debugfs_u32_array *data = inode->i_private; 942a2b992c8SJakub Kicinski int size, elements = data->n_elements; 943e05e279eSLinus Torvalds char *buf; 9449fe2a701SSrivatsa Vaddagiri 945e05e279eSLinus Torvalds /* 946e05e279eSLinus Torvalds * Max size: 947e05e279eSLinus Torvalds * - 10 digits + ' '/'\n' = 11 bytes per number 948e05e279eSLinus Torvalds * - terminating NUL character 949e05e279eSLinus Torvalds */ 950e05e279eSLinus Torvalds size = elements*11; 951e05e279eSLinus Torvalds buf = kmalloc(size+1, GFP_KERNEL); 952e05e279eSLinus Torvalds if (!buf) 95336048853SDavid Rientjes return -ENOMEM; 954e05e279eSLinus Torvalds buf[size] = 0; 955e05e279eSLinus Torvalds 956e05e279eSLinus Torvalds file->private_data = buf; 957a2b992c8SJakub Kicinski u32_format_array(buf, size, data->array, data->n_elements); 958e05e279eSLinus Torvalds 95936048853SDavid Rientjes return nonseekable_open(inode, file); 9609fe2a701SSrivatsa Vaddagiri } 9619fe2a701SSrivatsa Vaddagiri 96236048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 96336048853SDavid Rientjes loff_t *ppos) 96436048853SDavid Rientjes { 96536048853SDavid Rientjes size_t size = strlen(file->private_data); 9669fe2a701SSrivatsa Vaddagiri 9679fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 9689fe2a701SSrivatsa Vaddagiri file->private_data, size); 9699fe2a701SSrivatsa Vaddagiri } 9709fe2a701SSrivatsa Vaddagiri 9719fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 9729fe2a701SSrivatsa Vaddagiri { 9739fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 9749fe2a701SSrivatsa Vaddagiri 9759fe2a701SSrivatsa Vaddagiri return 0; 9769fe2a701SSrivatsa Vaddagiri } 9779fe2a701SSrivatsa Vaddagiri 9789fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 9799fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 9809fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 9819fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 9829fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 9839fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 9849fe2a701SSrivatsa Vaddagiri }; 9859fe2a701SSrivatsa Vaddagiri 9869fe2a701SSrivatsa Vaddagiri /** 9879fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 9889fe2a701SSrivatsa Vaddagiri * array. 9899fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 9909fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 9919fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 9929fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 9939fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 994a2b992c8SJakub Kicinski * @array: wrapper struct containing data pointer and size of the array. 9959fe2a701SSrivatsa Vaddagiri * 9969fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 9979fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 9989fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 9999fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 10009fe2a701SSrivatsa Vaddagiri */ 1001c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode, 1002a2b992c8SJakub Kicinski struct dentry *parent, 1003a2b992c8SJakub Kicinski struct debugfs_u32_array *array) 10049fe2a701SSrivatsa Vaddagiri { 1005a2b992c8SJakub Kicinski debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 10069fe2a701SSrivatsa Vaddagiri } 10079fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 10089fe2a701SSrivatsa Vaddagiri 10093b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 10103b85e4abSHeiko Carstens 10111a087c6aSAlessandro Rubini /* 10121a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 10131a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 10141a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 10151a087c6aSAlessandro Rubini */ 10161a087c6aSAlessandro Rubini 10171a087c6aSAlessandro Rubini /** 10181a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 10191a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 10201a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 1021b5763accSRandy Dunlap * @nregs: the length of the above array 10221a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 10231a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 10241a087c6aSAlessandro Rubini * 10251a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 10261a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 10271a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 10281a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 10291a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 10301a087c6aSAlessandro Rubini * for example configuration of dma channels 10311a087c6aSAlessandro Rubini */ 10329761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 10331a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 10341a087c6aSAlessandro Rubini { 10359761536eSJoe Perches int i; 10361a087c6aSAlessandro Rubini 10371a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 10381a087c6aSAlessandro Rubini if (prefix) 10399761536eSJoe Perches seq_printf(s, "%s", prefix); 10409761536eSJoe Perches seq_printf(s, "%s = 0x%08x\n", regs->name, 10415858441cSDan Carpenter readl(base + regs->offset)); 10429761536eSJoe Perches if (seq_has_overflowed(s)) 10439761536eSJoe Perches break; 10441a087c6aSAlessandro Rubini } 10451a087c6aSAlessandro Rubini } 10461a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 10471a087c6aSAlessandro Rubini 10481a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 10491a087c6aSAlessandro Rubini { 10501a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 10511a087c6aSAlessandro Rubini 105230332eeeSGeert Uytterhoeven if (regset->dev) 105330332eeeSGeert Uytterhoeven pm_runtime_get_sync(regset->dev); 105430332eeeSGeert Uytterhoeven 10551a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 105630332eeeSGeert Uytterhoeven 105730332eeeSGeert Uytterhoeven if (regset->dev) 105830332eeeSGeert Uytterhoeven pm_runtime_put(regset->dev); 105930332eeeSGeert Uytterhoeven 10601a087c6aSAlessandro Rubini return 0; 10611a087c6aSAlessandro Rubini } 10621a087c6aSAlessandro Rubini 10631a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 10641a087c6aSAlessandro Rubini { 10651a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 10661a087c6aSAlessandro Rubini } 10671a087c6aSAlessandro Rubini 10681a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 10691a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 10701a087c6aSAlessandro Rubini .read = seq_read, 10711a087c6aSAlessandro Rubini .llseek = seq_lseek, 10721a087c6aSAlessandro Rubini .release = single_release, 10731a087c6aSAlessandro Rubini }; 10741a087c6aSAlessandro Rubini 10751a087c6aSAlessandro Rubini /** 10761a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 10771a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 10781a087c6aSAlessandro Rubini * @mode: the permission that the file should have 10791a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 10801a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 10811a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 10821a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 10831a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 10841a087c6aSAlessandro Rubini * address where the register bank is to be found. 10851a087c6aSAlessandro Rubini * 10861a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 10871a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 10881a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 10891a087c6aSAlessandro Rubini */ 1090ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode, 10911a087c6aSAlessandro Rubini struct dentry *parent, 10921a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 10931a087c6aSAlessandro Rubini { 1094ae91c925SGreg Kroah-Hartman debugfs_create_file(name, mode, parent, regset, &fops_regset32); 10951a087c6aSAlessandro Rubini } 10961a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 10973b85e4abSHeiko Carstens 10983b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 109998210b7fSArend van Spriel 110098210b7fSArend van Spriel struct debugfs_devm_entry { 110198210b7fSArend van Spriel int (*read)(struct seq_file *seq, void *data); 110298210b7fSArend van Spriel struct device *dev; 110398210b7fSArend van Spriel }; 110498210b7fSArend van Spriel 110598210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 110698210b7fSArend van Spriel { 110798210b7fSArend van Spriel struct debugfs_devm_entry *entry = inode->i_private; 110898210b7fSArend van Spriel 110998210b7fSArend van Spriel return single_open(f, entry->read, entry->dev); 111098210b7fSArend van Spriel } 111198210b7fSArend van Spriel 111298210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = { 111398210b7fSArend van Spriel .owner = THIS_MODULE, 111498210b7fSArend van Spriel .open = debugfs_devm_entry_open, 111598210b7fSArend van Spriel .release = single_release, 111698210b7fSArend van Spriel .read = seq_read, 111798210b7fSArend van Spriel .llseek = seq_lseek 111898210b7fSArend van Spriel }; 111998210b7fSArend van Spriel 112098210b7fSArend van Spriel /** 112198210b7fSArend van Spriel * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 112298210b7fSArend van Spriel * 112398210b7fSArend van Spriel * @dev: device related to this debugfs file. 112498210b7fSArend van Spriel * @name: name of the debugfs file. 112598210b7fSArend van Spriel * @parent: a pointer to the parent dentry for this file. This should be a 112698210b7fSArend van Spriel * directory dentry if set. If this parameter is %NULL, then the 112798210b7fSArend van Spriel * file will be created in the root of the debugfs filesystem. 112898210b7fSArend van Spriel * @read_fn: function pointer called to print the seq_file content. 112998210b7fSArend van Spriel */ 113098210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, 113198210b7fSArend van Spriel struct dentry *parent, 113298210b7fSArend van Spriel int (*read_fn)(struct seq_file *s, 113398210b7fSArend van Spriel void *data)) 113498210b7fSArend van Spriel { 113598210b7fSArend van Spriel struct debugfs_devm_entry *entry; 113698210b7fSArend van Spriel 113798210b7fSArend van Spriel if (IS_ERR(parent)) 113898210b7fSArend van Spriel return ERR_PTR(-ENOENT); 113998210b7fSArend van Spriel 114098210b7fSArend van Spriel entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 114198210b7fSArend van Spriel if (!entry) 114298210b7fSArend van Spriel return ERR_PTR(-ENOMEM); 114398210b7fSArend van Spriel 114498210b7fSArend van Spriel entry->read = read_fn; 114598210b7fSArend van Spriel entry->dev = dev; 114698210b7fSArend van Spriel 114798210b7fSArend van Spriel return debugfs_create_file(name, S_IRUGO, parent, entry, 114898210b7fSArend van Spriel &debugfs_devm_entry_ops); 114998210b7fSArend van Spriel } 115098210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1151