11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * file.c - part of debugfs, a tiny little debug file system 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 51da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 81da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License version 91da177e4SLinus Torvalds * 2 as published by the Free Software Foundation. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 12e1b4fc7aSMauro Carvalho Chehab * See Documentation/filesystems/ for more details. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/fs.h> 181a087c6aSAlessandro Rubini #include <linux/seq_file.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/debugfs.h> 2103e099fbSAlessandro Rubini #include <linux/io.h> 229fe2a701SSrivatsa Vaddagiri #include <linux/slab.h> 233a76e5e0SSeth Jennings #include <linux/atomic.h> 2498210b7fSArend van Spriel #include <linux/device.h> 259fd4dcecSNicolai Stange #include <linux/srcu.h> 2649d200deSNicolai Stange #include <asm/poll.h> 279fd4dcecSNicolai Stange 289fd4dcecSNicolai Stange #include "internal.h" 291da177e4SLinus Torvalds 3049d200deSNicolai Stange struct poll_table_struct; 3149d200deSNicolai Stange 321da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 331da177e4SLinus Torvalds size_t count, loff_t *ppos) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds return 0; 361da177e4SLinus Torvalds } 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 391da177e4SLinus Torvalds size_t count, loff_t *ppos) 401da177e4SLinus Torvalds { 411da177e4SLinus Torvalds return count; 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 449fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = { 451da177e4SLinus Torvalds .read = default_read_file, 461da177e4SLinus Torvalds .write = default_write_file, 47234e3405SStephen Boyd .open = simple_open, 486038f373SArnd Bergmann .llseek = noop_llseek, 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 519fd4dcecSNicolai Stange /** 529fd4dcecSNicolai Stange * debugfs_use_file_start - mark the beginning of file data access 539fd4dcecSNicolai Stange * @dentry: the dentry object whose data is being accessed. 549fd4dcecSNicolai Stange * @srcu_idx: a pointer to some memory to store a SRCU index in. 559fd4dcecSNicolai Stange * 569fd4dcecSNicolai Stange * Up to a matching call to debugfs_use_file_finish(), any 579fd4dcecSNicolai Stange * successive call into the file removing functions debugfs_remove() 589fd4dcecSNicolai Stange * and debugfs_remove_recursive() will block. Since associated private 599fd4dcecSNicolai Stange * file data may only get freed after a successful return of any of 609fd4dcecSNicolai Stange * the removal functions, you may safely access it after a successful 619fd4dcecSNicolai Stange * call to debugfs_use_file_start() without worrying about 629fd4dcecSNicolai Stange * lifetime issues. 639fd4dcecSNicolai Stange * 649fd4dcecSNicolai Stange * If -%EIO is returned, the file has already been removed and thus, 659fd4dcecSNicolai Stange * it is not safe to access any of its data. If, on the other hand, 669fd4dcecSNicolai Stange * it is allowed to access the file data, zero is returned. 679fd4dcecSNicolai Stange * 689fd4dcecSNicolai Stange * Regardless of the return code, any call to 699fd4dcecSNicolai Stange * debugfs_use_file_start() must be followed by a matching call 709fd4dcecSNicolai Stange * to debugfs_use_file_finish(). 719fd4dcecSNicolai Stange */ 7249d200deSNicolai Stange int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx) 739fd4dcecSNicolai Stange __acquires(&debugfs_srcu) 749fd4dcecSNicolai Stange { 759fd4dcecSNicolai Stange *srcu_idx = srcu_read_lock(&debugfs_srcu); 769fd4dcecSNicolai Stange barrier(); 779fd4dcecSNicolai Stange if (d_unlinked(dentry)) 789fd4dcecSNicolai Stange return -EIO; 799fd4dcecSNicolai Stange return 0; 809fd4dcecSNicolai Stange } 8149d200deSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_use_file_start); 829fd4dcecSNicolai Stange 839fd4dcecSNicolai Stange /** 849fd4dcecSNicolai Stange * debugfs_use_file_finish - mark the end of file data access 859fd4dcecSNicolai Stange * @srcu_idx: the SRCU index "created" by a former call to 869fd4dcecSNicolai Stange * debugfs_use_file_start(). 879fd4dcecSNicolai Stange * 889fd4dcecSNicolai Stange * Allow any ongoing concurrent call into debugfs_remove() or 899fd4dcecSNicolai Stange * debugfs_remove_recursive() blocked by a former call to 909fd4dcecSNicolai Stange * debugfs_use_file_start() to proceed and return to its caller. 919fd4dcecSNicolai Stange */ 9249d200deSNicolai Stange void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu) 939fd4dcecSNicolai Stange { 949fd4dcecSNicolai Stange srcu_read_unlock(&debugfs_srcu, srcu_idx); 959fd4dcecSNicolai Stange } 9649d200deSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_use_file_finish); 979fd4dcecSNicolai Stange 989fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry) 999fd4dcecSNicolai Stange 1007c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp) 1017c8d4698SNicolai Stange __must_hold(&debugfs_srcu) 1027c8d4698SNicolai Stange { 1037c8d4698SNicolai Stange struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; 1047c8d4698SNicolai Stange /* 1057c8d4698SNicolai Stange * Neither the pointer to the struct file_operations, nor its 1067c8d4698SNicolai Stange * contents ever change -- srcu_dereference() is not needed here. 1077c8d4698SNicolai Stange */ 1087c8d4698SNicolai Stange return fsd->real_fops; 1097c8d4698SNicolai Stange } 1107c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops); 1117c8d4698SNicolai Stange 112*e9117a5aSNicolai Stange /** 113*e9117a5aSNicolai Stange * debugfs_file_get - mark the beginning of file data access 114*e9117a5aSNicolai Stange * @dentry: the dentry object whose data is being accessed. 115*e9117a5aSNicolai Stange * 116*e9117a5aSNicolai Stange * Up to a matching call to debugfs_file_put(), any successive call 117*e9117a5aSNicolai Stange * into the file removing functions debugfs_remove() and 118*e9117a5aSNicolai Stange * debugfs_remove_recursive() will block. Since associated private 119*e9117a5aSNicolai Stange * file data may only get freed after a successful return of any of 120*e9117a5aSNicolai Stange * the removal functions, you may safely access it after a successful 121*e9117a5aSNicolai Stange * call to debugfs_file_get() without worrying about lifetime issues. 122*e9117a5aSNicolai Stange * 123*e9117a5aSNicolai Stange * If -%EIO is returned, the file has already been removed and thus, 124*e9117a5aSNicolai Stange * it is not safe to access any of its data. If, on the other hand, 125*e9117a5aSNicolai Stange * it is allowed to access the file data, zero is returned. 126*e9117a5aSNicolai Stange */ 127*e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry) 128*e9117a5aSNicolai Stange { 129*e9117a5aSNicolai Stange struct debugfs_fsdata *fsd = dentry->d_fsdata; 130*e9117a5aSNicolai Stange 131*e9117a5aSNicolai Stange /* Avoid starvation of removers. */ 132*e9117a5aSNicolai Stange if (d_unlinked(dentry)) 133*e9117a5aSNicolai Stange return -EIO; 134*e9117a5aSNicolai Stange 135*e9117a5aSNicolai Stange if (!refcount_inc_not_zero(&fsd->active_users)) 136*e9117a5aSNicolai Stange return -EIO; 137*e9117a5aSNicolai Stange 138*e9117a5aSNicolai Stange return 0; 139*e9117a5aSNicolai Stange } 140*e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get); 141*e9117a5aSNicolai Stange 142*e9117a5aSNicolai Stange /** 143*e9117a5aSNicolai Stange * debugfs_file_put - mark the end of file data access 144*e9117a5aSNicolai Stange * @dentry: the dentry object formerly passed to 145*e9117a5aSNicolai Stange * debugfs_file_get(). 146*e9117a5aSNicolai Stange * 147*e9117a5aSNicolai Stange * Allow any ongoing concurrent call into debugfs_remove() or 148*e9117a5aSNicolai Stange * debugfs_remove_recursive() blocked by a former call to 149*e9117a5aSNicolai Stange * debugfs_file_get() to proceed and return to its caller. 150*e9117a5aSNicolai Stange */ 151*e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry) 152*e9117a5aSNicolai Stange { 153*e9117a5aSNicolai Stange struct debugfs_fsdata *fsd = dentry->d_fsdata; 154*e9117a5aSNicolai Stange 155*e9117a5aSNicolai Stange if (refcount_dec_and_test(&fsd->active_users)) 156*e9117a5aSNicolai Stange complete(&fsd->active_users_drained); 157*e9117a5aSNicolai Stange } 158*e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put); 159*e9117a5aSNicolai Stange 1609fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp) 1619fd4dcecSNicolai Stange { 1629fd4dcecSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 1639fd4dcecSNicolai Stange const struct file_operations *real_fops = NULL; 1649fd4dcecSNicolai Stange int srcu_idx, r; 1659fd4dcecSNicolai Stange 1669fd4dcecSNicolai Stange r = debugfs_use_file_start(dentry, &srcu_idx); 1679fd4dcecSNicolai Stange if (r) { 1689fd4dcecSNicolai Stange r = -ENOENT; 1699fd4dcecSNicolai Stange goto out; 1709fd4dcecSNicolai Stange } 1719fd4dcecSNicolai Stange 17286f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 1739fd4dcecSNicolai Stange real_fops = fops_get(real_fops); 1749fd4dcecSNicolai Stange if (!real_fops) { 1759fd4dcecSNicolai Stange /* Huh? Module did not clean up after itself at exit? */ 1769fd4dcecSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 1779fd4dcecSNicolai Stange dentry); 1789fd4dcecSNicolai Stange r = -ENXIO; 1799fd4dcecSNicolai Stange goto out; 1809fd4dcecSNicolai Stange } 1819fd4dcecSNicolai Stange replace_fops(filp, real_fops); 1829fd4dcecSNicolai Stange 1839fd4dcecSNicolai Stange if (real_fops->open) 1849fd4dcecSNicolai Stange r = real_fops->open(inode, filp); 1859fd4dcecSNicolai Stange 1869fd4dcecSNicolai Stange out: 1879fd4dcecSNicolai Stange debugfs_use_file_finish(srcu_idx); 1889fd4dcecSNicolai Stange return r; 1899fd4dcecSNicolai Stange } 1909fd4dcecSNicolai Stange 1919fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = { 1929fd4dcecSNicolai Stange .open = open_proxy_open, 1939fd4dcecSNicolai Stange }; 1949fd4dcecSNicolai Stange 19549d200deSNicolai Stange #define PROTO(args...) args 19649d200deSNicolai Stange #define ARGS(args...) args 19749d200deSNicolai Stange 19849d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ 19949d200deSNicolai Stange static ret_type full_proxy_ ## name(proto) \ 20049d200deSNicolai Stange { \ 20149d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); \ 20249d200deSNicolai Stange const struct file_operations *real_fops = \ 20386f0e067SChristian Lamparter debugfs_real_fops(filp); \ 20449d200deSNicolai Stange int srcu_idx; \ 20549d200deSNicolai Stange ret_type r; \ 20649d200deSNicolai Stange \ 20749d200deSNicolai Stange r = debugfs_use_file_start(dentry, &srcu_idx); \ 20849d200deSNicolai Stange if (likely(!r)) \ 20949d200deSNicolai Stange r = real_fops->name(args); \ 21049d200deSNicolai Stange debugfs_use_file_finish(srcu_idx); \ 21149d200deSNicolai Stange return r; \ 21249d200deSNicolai Stange } 21349d200deSNicolai Stange 21449d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp, 21549d200deSNicolai Stange PROTO(struct file *filp, loff_t offset, int whence), 21649d200deSNicolai Stange ARGS(filp, offset, whence)); 21749d200deSNicolai Stange 21849d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp, 21949d200deSNicolai Stange PROTO(struct file *filp, char __user *buf, size_t size, 22049d200deSNicolai Stange loff_t *ppos), 22149d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 22249d200deSNicolai Stange 22349d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp, 22449d200deSNicolai Stange PROTO(struct file *filp, const char __user *buf, size_t size, 22549d200deSNicolai Stange loff_t *ppos), 22649d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 22749d200deSNicolai Stange 22849d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 22949d200deSNicolai Stange PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 23049d200deSNicolai Stange ARGS(filp, cmd, arg)); 23149d200deSNicolai Stange 23249d200deSNicolai Stange static unsigned int full_proxy_poll(struct file *filp, 23349d200deSNicolai Stange struct poll_table_struct *wait) 23449d200deSNicolai Stange { 23549d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 23686f0e067SChristian Lamparter const struct file_operations *real_fops = debugfs_real_fops(filp); 23749d200deSNicolai Stange int srcu_idx; 23849d200deSNicolai Stange unsigned int r = 0; 23949d200deSNicolai Stange 24049d200deSNicolai Stange if (debugfs_use_file_start(dentry, &srcu_idx)) { 24149d200deSNicolai Stange debugfs_use_file_finish(srcu_idx); 24249d200deSNicolai Stange return POLLHUP; 24349d200deSNicolai Stange } 24449d200deSNicolai Stange 24549d200deSNicolai Stange r = real_fops->poll(filp, wait); 24649d200deSNicolai Stange debugfs_use_file_finish(srcu_idx); 24749d200deSNicolai Stange return r; 24849d200deSNicolai Stange } 24949d200deSNicolai Stange 25049d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp) 25149d200deSNicolai Stange { 25249d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 25386f0e067SChristian Lamparter const struct file_operations *real_fops = debugfs_real_fops(filp); 25449d200deSNicolai Stange const struct file_operations *proxy_fops = filp->f_op; 25549d200deSNicolai Stange int r = 0; 25649d200deSNicolai Stange 25749d200deSNicolai Stange /* 25849d200deSNicolai Stange * We must not protect this against removal races here: the 25949d200deSNicolai Stange * original releaser should be called unconditionally in order 26049d200deSNicolai Stange * not to leak any resources. Releasers must not assume that 26149d200deSNicolai Stange * ->i_private is still being meaningful here. 26249d200deSNicolai Stange */ 26349d200deSNicolai Stange if (real_fops->release) 26449d200deSNicolai Stange r = real_fops->release(inode, filp); 26549d200deSNicolai Stange 26649d200deSNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 26749d200deSNicolai Stange kfree((void *)proxy_fops); 26849d200deSNicolai Stange fops_put(real_fops); 269a1a9e5d2SEric Engestrom return r; 27049d200deSNicolai Stange } 27149d200deSNicolai Stange 27249d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops, 27349d200deSNicolai Stange const struct file_operations *real_fops) 27449d200deSNicolai Stange { 27549d200deSNicolai Stange proxy_fops->release = full_proxy_release; 27649d200deSNicolai Stange if (real_fops->llseek) 27749d200deSNicolai Stange proxy_fops->llseek = full_proxy_llseek; 27849d200deSNicolai Stange if (real_fops->read) 27949d200deSNicolai Stange proxy_fops->read = full_proxy_read; 28049d200deSNicolai Stange if (real_fops->write) 28149d200deSNicolai Stange proxy_fops->write = full_proxy_write; 28249d200deSNicolai Stange if (real_fops->poll) 28349d200deSNicolai Stange proxy_fops->poll = full_proxy_poll; 28449d200deSNicolai Stange if (real_fops->unlocked_ioctl) 28549d200deSNicolai Stange proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; 28649d200deSNicolai Stange } 28749d200deSNicolai Stange 28849d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp) 28949d200deSNicolai Stange { 29049d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 29149d200deSNicolai Stange const struct file_operations *real_fops = NULL; 29249d200deSNicolai Stange struct file_operations *proxy_fops = NULL; 29349d200deSNicolai Stange int srcu_idx, r; 29449d200deSNicolai Stange 29549d200deSNicolai Stange r = debugfs_use_file_start(dentry, &srcu_idx); 29649d200deSNicolai Stange if (r) { 29749d200deSNicolai Stange r = -ENOENT; 29849d200deSNicolai Stange goto out; 29949d200deSNicolai Stange } 30049d200deSNicolai Stange 30186f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 30249d200deSNicolai Stange real_fops = fops_get(real_fops); 30349d200deSNicolai Stange if (!real_fops) { 30449d200deSNicolai Stange /* Huh? Module did not cleanup after itself at exit? */ 30549d200deSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 30649d200deSNicolai Stange dentry); 30749d200deSNicolai Stange r = -ENXIO; 30849d200deSNicolai Stange goto out; 30949d200deSNicolai Stange } 31049d200deSNicolai Stange 31149d200deSNicolai Stange proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); 31249d200deSNicolai Stange if (!proxy_fops) { 31349d200deSNicolai Stange r = -ENOMEM; 31449d200deSNicolai Stange goto free_proxy; 31549d200deSNicolai Stange } 31649d200deSNicolai Stange __full_proxy_fops_init(proxy_fops, real_fops); 31749d200deSNicolai Stange replace_fops(filp, proxy_fops); 31849d200deSNicolai Stange 31949d200deSNicolai Stange if (real_fops->open) { 32049d200deSNicolai Stange r = real_fops->open(inode, filp); 321b10e3e90SNicolai Stange if (r) { 322b10e3e90SNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 323b10e3e90SNicolai Stange goto free_proxy; 324b10e3e90SNicolai Stange } else if (filp->f_op != proxy_fops) { 32549d200deSNicolai Stange /* No protection against file removal anymore. */ 32649d200deSNicolai Stange WARN(1, "debugfs file owner replaced proxy fops: %pd", 32749d200deSNicolai Stange dentry); 32849d200deSNicolai Stange goto free_proxy; 32949d200deSNicolai Stange } 33049d200deSNicolai Stange } 33149d200deSNicolai Stange 33249d200deSNicolai Stange goto out; 33349d200deSNicolai Stange free_proxy: 33449d200deSNicolai Stange kfree(proxy_fops); 33549d200deSNicolai Stange fops_put(real_fops); 33649d200deSNicolai Stange out: 33749d200deSNicolai Stange debugfs_use_file_finish(srcu_idx); 33849d200deSNicolai Stange return r; 33949d200deSNicolai Stange } 34049d200deSNicolai Stange 34149d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = { 34249d200deSNicolai Stange .open = full_proxy_open, 34349d200deSNicolai Stange }; 34449d200deSNicolai Stange 345c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf, 346c6468808SNicolai Stange size_t len, loff_t *ppos) 347c6468808SNicolai Stange { 348c6468808SNicolai Stange ssize_t ret; 349c6468808SNicolai Stange int srcu_idx; 350c6468808SNicolai Stange 351c6468808SNicolai Stange ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); 352c6468808SNicolai Stange if (likely(!ret)) 353c6468808SNicolai Stange ret = simple_attr_read(file, buf, len, ppos); 354c6468808SNicolai Stange debugfs_use_file_finish(srcu_idx); 355c6468808SNicolai Stange return ret; 356c6468808SNicolai Stange } 357c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read); 358c6468808SNicolai Stange 359c6468808SNicolai Stange ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 360c6468808SNicolai Stange size_t len, loff_t *ppos) 361c6468808SNicolai Stange { 362c6468808SNicolai Stange ssize_t ret; 363c6468808SNicolai Stange int srcu_idx; 364c6468808SNicolai Stange 365c6468808SNicolai Stange ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); 366c6468808SNicolai Stange if (likely(!ret)) 367c6468808SNicolai Stange ret = simple_attr_write(file, buf, len, ppos); 368c6468808SNicolai Stange debugfs_use_file_finish(srcu_idx); 369c6468808SNicolai Stange return ret; 370c6468808SNicolai Stange } 371c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write); 372c6468808SNicolai Stange 3734909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 3744909f168SNicolai Stange struct dentry *parent, void *value, 3754909f168SNicolai Stange const struct file_operations *fops, 3764909f168SNicolai Stange const struct file_operations *fops_ro, 3774909f168SNicolai Stange const struct file_operations *fops_wo) 3784909f168SNicolai Stange { 3794909f168SNicolai Stange /* if there are no write bits set, make read only */ 3804909f168SNicolai Stange if (!(mode & S_IWUGO)) 3814909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 3824909f168SNicolai Stange fops_ro); 3834909f168SNicolai Stange /* if there are no read bits set, make write only */ 3844909f168SNicolai Stange if (!(mode & S_IRUGO)) 3854909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 3864909f168SNicolai Stange fops_wo); 3874909f168SNicolai Stange 3884909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, fops); 3894909f168SNicolai Stange } 3904909f168SNicolai Stange 3918b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 392acaefc25SArnd Bergmann { 393acaefc25SArnd Bergmann *(u8 *)data = val; 3948b88b099SChristoph Hellwig return 0; 395acaefc25SArnd Bergmann } 3968b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 397acaefc25SArnd Bergmann { 3988b88b099SChristoph Hellwig *val = *(u8 *)data; 3998b88b099SChristoph Hellwig return 0; 400acaefc25SArnd Bergmann } 4014909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 4024909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 4034909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /** 4066468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 4071da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4081da177e4SLinus Torvalds * @mode: the permission that the file should have 4091da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4106468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4111da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4121da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4131da177e4SLinus Torvalds * from. 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4161da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4171da177e4SLinus Torvalds * set, it can be read from, and written to. 4181da177e4SLinus Torvalds * 4191da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4201da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4211da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4226468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4231da177e4SLinus Torvalds * 4246468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 4251da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 4266468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 4271da177e4SLinus Torvalds * code. 4281da177e4SLinus Torvalds */ 429f4ae40a6SAl Viro struct dentry *debugfs_create_u8(const char *name, umode_t mode, 4301da177e4SLinus Torvalds struct dentry *parent, u8 *value) 4311da177e4SLinus Torvalds { 4324909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 433b97f6799SStephen Boyd &fops_u8_ro, &fops_u8_wo); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 4361da177e4SLinus Torvalds 4378b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 438acaefc25SArnd Bergmann { 439acaefc25SArnd Bergmann *(u16 *)data = val; 4408b88b099SChristoph Hellwig return 0; 441acaefc25SArnd Bergmann } 4428b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 443acaefc25SArnd Bergmann { 4448b88b099SChristoph Hellwig *val = *(u16 *)data; 4458b88b099SChristoph Hellwig return 0; 446acaefc25SArnd Bergmann } 4474909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 4484909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 4494909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 450acaefc25SArnd Bergmann 4511da177e4SLinus Torvalds /** 4526468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 4531da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 4541da177e4SLinus Torvalds * @mode: the permission that the file should have 4551da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 4566468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 4571da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 4581da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 4591da177e4SLinus Torvalds * from. 4601da177e4SLinus Torvalds * 4611da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 4621da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 4631da177e4SLinus Torvalds * set, it can be read from, and written to. 4641da177e4SLinus Torvalds * 4651da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 4661da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 4671da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 4686468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 4691da177e4SLinus Torvalds * 4706468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 4711da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 4726468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 4731da177e4SLinus Torvalds * code. 4741da177e4SLinus Torvalds */ 475f4ae40a6SAl Viro struct dentry *debugfs_create_u16(const char *name, umode_t mode, 4761da177e4SLinus Torvalds struct dentry *parent, u16 *value) 4771da177e4SLinus Torvalds { 4784909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 479b97f6799SStephen Boyd &fops_u16_ro, &fops_u16_wo); 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 4821da177e4SLinus Torvalds 4838b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 484acaefc25SArnd Bergmann { 485acaefc25SArnd Bergmann *(u32 *)data = val; 4868b88b099SChristoph Hellwig return 0; 487acaefc25SArnd Bergmann } 4888b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 489acaefc25SArnd Bergmann { 4908b88b099SChristoph Hellwig *val = *(u32 *)data; 4918b88b099SChristoph Hellwig return 0; 492acaefc25SArnd Bergmann } 4934909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 4944909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 4954909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 496acaefc25SArnd Bergmann 4971da177e4SLinus Torvalds /** 4986468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 4991da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 5001da177e4SLinus Torvalds * @mode: the permission that the file should have 5011da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 5026468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 5031da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 5041da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 5051da177e4SLinus Torvalds * from. 5061da177e4SLinus Torvalds * 5071da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 5081da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 5091da177e4SLinus Torvalds * set, it can be read from, and written to. 5101da177e4SLinus Torvalds * 5111da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 5121da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 5131da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 5146468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 5151da177e4SLinus Torvalds * 5166468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 5171da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 5186468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 5191da177e4SLinus Torvalds * code. 5201da177e4SLinus Torvalds */ 521f4ae40a6SAl Viro struct dentry *debugfs_create_u32(const char *name, umode_t mode, 5221da177e4SLinus Torvalds struct dentry *parent, u32 *value) 5231da177e4SLinus Torvalds { 5244909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 525b97f6799SStephen Boyd &fops_u32_ro, &fops_u32_wo); 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 5281da177e4SLinus Torvalds 5298b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 5308447891fSMichael Ellerman { 5318447891fSMichael Ellerman *(u64 *)data = val; 5328b88b099SChristoph Hellwig return 0; 5338447891fSMichael Ellerman } 5348447891fSMichael Ellerman 5358b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 5368447891fSMichael Ellerman { 5378b88b099SChristoph Hellwig *val = *(u64 *)data; 5388b88b099SChristoph Hellwig return 0; 5398447891fSMichael Ellerman } 5404909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 5414909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 5424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 5438447891fSMichael Ellerman 5448447891fSMichael Ellerman /** 5458447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 5468447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 5478447891fSMichael Ellerman * @mode: the permission that the file should have 5488447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 5498447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 5508447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 5518447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 5528447891fSMichael Ellerman * from. 5538447891fSMichael Ellerman * 5548447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 5558447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 5568447891fSMichael Ellerman * set, it can be read from, and written to. 5578447891fSMichael Ellerman * 5588447891fSMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 5598447891fSMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 5608447891fSMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 5618447891fSMichael Ellerman * you are responsible here.) If an error occurs, %NULL will be returned. 5628447891fSMichael Ellerman * 5638447891fSMichael Ellerman * If debugfs is not enabled in the kernel, the value -%ENODEV will be 5648447891fSMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 5658447891fSMichael Ellerman * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 5668447891fSMichael Ellerman * code. 5678447891fSMichael Ellerman */ 568f4ae40a6SAl Viro struct dentry *debugfs_create_u64(const char *name, umode_t mode, 5698447891fSMichael Ellerman struct dentry *parent, u64 *value) 5708447891fSMichael Ellerman { 5714909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 572b97f6799SStephen Boyd &fops_u64_ro, &fops_u64_wo); 5738447891fSMichael Ellerman } 5748447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 5758447891fSMichael Ellerman 576c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val) 577c23fe831SViresh Kumar { 578c23fe831SViresh Kumar *(unsigned long *)data = val; 579c23fe831SViresh Kumar return 0; 580c23fe831SViresh Kumar } 581c23fe831SViresh Kumar 582c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val) 583c23fe831SViresh Kumar { 584c23fe831SViresh Kumar *val = *(unsigned long *)data; 585c23fe831SViresh Kumar return 0; 586c23fe831SViresh Kumar } 5874909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 5884909f168SNicolai Stange "%llu\n"); 5894909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 5904909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 591c23fe831SViresh Kumar 592c23fe831SViresh Kumar /** 593c23fe831SViresh Kumar * debugfs_create_ulong - create a debugfs file that is used to read and write 594c23fe831SViresh Kumar * an unsigned long value. 595c23fe831SViresh Kumar * @name: a pointer to a string containing the name of the file to create. 596c23fe831SViresh Kumar * @mode: the permission that the file should have 597c23fe831SViresh Kumar * @parent: a pointer to the parent dentry for this file. This should be a 598c23fe831SViresh Kumar * directory dentry if set. If this parameter is %NULL, then the 599c23fe831SViresh Kumar * file will be created in the root of the debugfs filesystem. 600c23fe831SViresh Kumar * @value: a pointer to the variable that the file should read to and write 601c23fe831SViresh Kumar * from. 602c23fe831SViresh Kumar * 603c23fe831SViresh Kumar * This function creates a file in debugfs with the given name that 604c23fe831SViresh Kumar * contains the value of the variable @value. If the @mode variable is so 605c23fe831SViresh Kumar * set, it can be read from, and written to. 606c23fe831SViresh Kumar * 607c23fe831SViresh Kumar * This function will return a pointer to a dentry if it succeeds. This 608c23fe831SViresh Kumar * pointer must be passed to the debugfs_remove() function when the file is 609c23fe831SViresh Kumar * to be removed (no automatic cleanup happens if your module is unloaded, 610c23fe831SViresh Kumar * you are responsible here.) If an error occurs, %NULL will be returned. 611c23fe831SViresh Kumar * 612c23fe831SViresh Kumar * If debugfs is not enabled in the kernel, the value -%ENODEV will be 613c23fe831SViresh Kumar * returned. It is not wise to check for this value, but rather, check for 614c23fe831SViresh Kumar * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 615c23fe831SViresh Kumar * code. 616c23fe831SViresh Kumar */ 617c23fe831SViresh Kumar struct dentry *debugfs_create_ulong(const char *name, umode_t mode, 618c23fe831SViresh Kumar struct dentry *parent, unsigned long *value) 619c23fe831SViresh Kumar { 6204909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, 6214909f168SNicolai Stange &fops_ulong, &fops_ulong_ro, 6224909f168SNicolai Stange &fops_ulong_wo); 623c23fe831SViresh Kumar } 624c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong); 625c23fe831SViresh Kumar 6264909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 6274909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 6284909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 6292ebefc50SRobin Getz 6304909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 6314909f168SNicolai Stange "0x%04llx\n"); 6324909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 6334909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 6342ebefc50SRobin Getz 6354909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 6364909f168SNicolai Stange "0x%08llx\n"); 6374909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 6384909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 6392ebefc50SRobin Getz 6404909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 6414909f168SNicolai Stange "0x%016llx\n"); 6424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 6434909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 64415b0beaaSHuang Ying 645e6716b87SRandy Dunlap /* 64615b0beaaSHuang 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 647e6716b87SRandy Dunlap * 648e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 649e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 650e6716b87SRandy Dunlap * decimal functions. 651e6716b87SRandy Dunlap */ 652e6716b87SRandy Dunlap 6532ebefc50SRobin Getz /** 6542ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 655e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 656e6716b87SRandy Dunlap * @mode: the permission that the file should have 657e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 658e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 659e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 660e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 661e6716b87SRandy Dunlap * from. 6622ebefc50SRobin Getz */ 663f4ae40a6SAl Viro struct dentry *debugfs_create_x8(const char *name, umode_t mode, 6642ebefc50SRobin Getz struct dentry *parent, u8 *value) 6652ebefc50SRobin Getz { 6664909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 667b97f6799SStephen Boyd &fops_x8_ro, &fops_x8_wo); 6682ebefc50SRobin Getz } 6692ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 6702ebefc50SRobin Getz 671e6716b87SRandy Dunlap /** 672e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 673e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 674e6716b87SRandy Dunlap * @mode: the permission that the file should have 675e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 676e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 677e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 678e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 679e6716b87SRandy Dunlap * from. 680e6716b87SRandy Dunlap */ 681f4ae40a6SAl Viro struct dentry *debugfs_create_x16(const char *name, umode_t mode, 6822ebefc50SRobin Getz struct dentry *parent, u16 *value) 6832ebefc50SRobin Getz { 6844909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 685b97f6799SStephen Boyd &fops_x16_ro, &fops_x16_wo); 6862ebefc50SRobin Getz } 6872ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 6882ebefc50SRobin Getz 689e6716b87SRandy Dunlap /** 690e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 691e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 692e6716b87SRandy Dunlap * @mode: the permission that the file should have 693e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 694e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 695e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 696e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 697e6716b87SRandy Dunlap * from. 698e6716b87SRandy Dunlap */ 699f4ae40a6SAl Viro struct dentry *debugfs_create_x32(const char *name, umode_t mode, 7002ebefc50SRobin Getz struct dentry *parent, u32 *value) 7012ebefc50SRobin Getz { 7024909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 703b97f6799SStephen Boyd &fops_x32_ro, &fops_x32_wo); 7042ebefc50SRobin Getz } 7052ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 7062ebefc50SRobin Getz 70715b0beaaSHuang Ying /** 70815b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 70915b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 71015b0beaaSHuang Ying * @mode: the permission that the file should have 71115b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 71215b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 71315b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 71415b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 71515b0beaaSHuang Ying * from. 71615b0beaaSHuang Ying */ 717f4ae40a6SAl Viro struct dentry *debugfs_create_x64(const char *name, umode_t mode, 71815b0beaaSHuang Ying struct dentry *parent, u64 *value) 71915b0beaaSHuang Ying { 7204909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 72182b7d4fbSStephen Boyd &fops_x64_ro, &fops_x64_wo); 72215b0beaaSHuang Ying } 72315b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 72415b0beaaSHuang Ying 7255e078787SInaky Perez-Gonzalez 7265e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 7275e078787SInaky Perez-Gonzalez { 7285e078787SInaky Perez-Gonzalez *(size_t *)data = val; 7295e078787SInaky Perez-Gonzalez return 0; 7305e078787SInaky Perez-Gonzalez } 7315e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 7325e078787SInaky Perez-Gonzalez { 7335e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 7345e078787SInaky Perez-Gonzalez return 0; 7355e078787SInaky Perez-Gonzalez } 7364909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 7375e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 7384909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 7394909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 7405e078787SInaky Perez-Gonzalez 7415e078787SInaky Perez-Gonzalez /** 7425e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 7435e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 7445e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 7455e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 7465e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 7475e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 7485e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 7495e078787SInaky Perez-Gonzalez * from. 7505e078787SInaky Perez-Gonzalez */ 751f4ae40a6SAl Viro struct dentry *debugfs_create_size_t(const char *name, umode_t mode, 7525e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 7535e078787SInaky Perez-Gonzalez { 7544909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, 7554909f168SNicolai Stange &fops_size_t, &fops_size_t_ro, 7564909f168SNicolai Stange &fops_size_t_wo); 7575e078787SInaky Perez-Gonzalez } 7585e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 7595e078787SInaky Perez-Gonzalez 7603a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val) 7613a76e5e0SSeth Jennings { 7623a76e5e0SSeth Jennings atomic_set((atomic_t *)data, val); 7633a76e5e0SSeth Jennings return 0; 7643a76e5e0SSeth Jennings } 7653a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val) 7663a76e5e0SSeth Jennings { 7673a76e5e0SSeth Jennings *val = atomic_read((atomic_t *)data); 7683a76e5e0SSeth Jennings return 0; 7693a76e5e0SSeth Jennings } 7704909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, 7713a76e5e0SSeth Jennings debugfs_atomic_t_set, "%lld\n"); 7724909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 7734909f168SNicolai Stange "%lld\n"); 7744909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 7754909f168SNicolai Stange "%lld\n"); 7763a76e5e0SSeth Jennings 7773a76e5e0SSeth Jennings /** 7783a76e5e0SSeth Jennings * debugfs_create_atomic_t - create a debugfs file that is used to read and 7793a76e5e0SSeth Jennings * write an atomic_t value 7803a76e5e0SSeth Jennings * @name: a pointer to a string containing the name of the file to create. 7813a76e5e0SSeth Jennings * @mode: the permission that the file should have 7823a76e5e0SSeth Jennings * @parent: a pointer to the parent dentry for this file. This should be a 7833a76e5e0SSeth Jennings * directory dentry if set. If this parameter is %NULL, then the 7843a76e5e0SSeth Jennings * file will be created in the root of the debugfs filesystem. 7853a76e5e0SSeth Jennings * @value: a pointer to the variable that the file should read to and write 7863a76e5e0SSeth Jennings * from. 7873a76e5e0SSeth Jennings */ 7883a76e5e0SSeth Jennings struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, 7893a76e5e0SSeth Jennings struct dentry *parent, atomic_t *value) 7903a76e5e0SSeth Jennings { 7914909f168SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, 7924909f168SNicolai Stange &fops_atomic_t, &fops_atomic_t_ro, 7934909f168SNicolai Stange &fops_atomic_t_wo); 7943a76e5e0SSeth Jennings } 7953a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 7965e078787SInaky Perez-Gonzalez 7970642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 7981da177e4SLinus Torvalds size_t count, loff_t *ppos) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds char buf[3]; 8014d45f797SNicolai Stange bool val; 8024d45f797SNicolai Stange int r, srcu_idx; 8031da177e4SLinus Torvalds 8044d45f797SNicolai Stange r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); 8054d45f797SNicolai Stange if (likely(!r)) 8064d45f797SNicolai Stange val = *(bool *)file->private_data; 8074d45f797SNicolai Stange debugfs_use_file_finish(srcu_idx); 8084d45f797SNicolai Stange if (r) 8094d45f797SNicolai Stange return r; 8104d45f797SNicolai Stange 8114d45f797SNicolai Stange if (val) 8121da177e4SLinus Torvalds buf[0] = 'Y'; 8131da177e4SLinus Torvalds else 8141da177e4SLinus Torvalds buf[0] = 'N'; 8151da177e4SLinus Torvalds buf[1] = '\n'; 8161da177e4SLinus Torvalds buf[2] = 0x00; 8171da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 8181da177e4SLinus Torvalds } 8190642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 8201da177e4SLinus Torvalds 8210642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 8221da177e4SLinus Torvalds size_t count, loff_t *ppos) 8231da177e4SLinus Torvalds { 8241da177e4SLinus Torvalds char buf[32]; 825c42d2237SStephen Boyd size_t buf_size; 8268705b48eSJonathan Cameron bool bv; 8274d45f797SNicolai Stange int r, srcu_idx; 828621a5f7aSViresh Kumar bool *val = file->private_data; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds buf_size = min(count, (sizeof(buf)-1)); 8311da177e4SLinus Torvalds if (copy_from_user(buf, user_buf, buf_size)) 8321da177e4SLinus Torvalds return -EFAULT; 8331da177e4SLinus Torvalds 834a3b2c8c7SMathias Krause buf[buf_size] = '\0'; 8354d45f797SNicolai Stange if (strtobool(buf, &bv) == 0) { 8364d45f797SNicolai Stange r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); 8374d45f797SNicolai Stange if (likely(!r)) 8388705b48eSJonathan Cameron *val = bv; 8394d45f797SNicolai Stange debugfs_use_file_finish(srcu_idx); 8404d45f797SNicolai Stange if (r) 8414d45f797SNicolai Stange return r; 8424d45f797SNicolai Stange } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds return count; 8451da177e4SLinus Torvalds } 8460642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 8471da177e4SLinus Torvalds 8484b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 8490642ef6fSRichard Fitzgerald .read = debugfs_read_file_bool, 8500642ef6fSRichard Fitzgerald .write = debugfs_write_file_bool, 851234e3405SStephen Boyd .open = simple_open, 8526038f373SArnd Bergmann .llseek = default_llseek, 8531da177e4SLinus Torvalds }; 8541da177e4SLinus Torvalds 8556713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = { 8566713e8fbSStephen Boyd .read = debugfs_read_file_bool, 8576713e8fbSStephen Boyd .open = simple_open, 8586713e8fbSStephen Boyd .llseek = default_llseek, 8596713e8fbSStephen Boyd }; 8606713e8fbSStephen Boyd 8616713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = { 8626713e8fbSStephen Boyd .write = debugfs_write_file_bool, 8636713e8fbSStephen Boyd .open = simple_open, 8646713e8fbSStephen Boyd .llseek = default_llseek, 8656713e8fbSStephen Boyd }; 8666713e8fbSStephen Boyd 8671da177e4SLinus Torvalds /** 8686468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 8691da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 8701da177e4SLinus Torvalds * @mode: the permission that the file should have 8711da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 8726468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 8731da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 8741da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 8751da177e4SLinus Torvalds * from. 8761da177e4SLinus Torvalds * 8771da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 8781da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 8791da177e4SLinus Torvalds * set, it can be read from, and written to. 8801da177e4SLinus Torvalds * 8811da177e4SLinus Torvalds * This function will return a pointer to a dentry if it succeeds. This 8821da177e4SLinus Torvalds * pointer must be passed to the debugfs_remove() function when the file is 8831da177e4SLinus Torvalds * to be removed (no automatic cleanup happens if your module is unloaded, 8846468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 8851da177e4SLinus Torvalds * 8866468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 8871da177e4SLinus Torvalds * returned. It is not wise to check for this value, but rather, check for 8886468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 8891da177e4SLinus Torvalds * code. 8901da177e4SLinus Torvalds */ 891f4ae40a6SAl Viro struct dentry *debugfs_create_bool(const char *name, umode_t mode, 892621a5f7aSViresh Kumar struct dentry *parent, bool *value) 8931da177e4SLinus Torvalds { 8944d45f797SNicolai Stange return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 8956713e8fbSStephen Boyd &fops_bool_ro, &fops_bool_wo); 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 8981da177e4SLinus Torvalds 899dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 900dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 901dd308bc3SMichael Ellerman { 902dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 90383b711cbSNicolai Stange ssize_t r; 90483b711cbSNicolai Stange int srcu_idx; 90583b711cbSNicolai Stange 90683b711cbSNicolai Stange r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); 90783b711cbSNicolai Stange if (likely(!r)) 90883b711cbSNicolai Stange r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 909dd308bc3SMichael Ellerman blob->size); 91083b711cbSNicolai Stange debugfs_use_file_finish(srcu_idx); 91183b711cbSNicolai Stange return r; 912dd308bc3SMichael Ellerman } 913dd308bc3SMichael Ellerman 91400977a59SArjan van de Ven static const struct file_operations fops_blob = { 915dd308bc3SMichael Ellerman .read = read_file_blob, 916234e3405SStephen Boyd .open = simple_open, 9176038f373SArnd Bergmann .llseek = default_llseek, 918dd308bc3SMichael Ellerman }; 919dd308bc3SMichael Ellerman 920dd308bc3SMichael Ellerman /** 921400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 922dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 923dd308bc3SMichael Ellerman * @mode: the permission that the file should have 924dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 9256468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 926dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 927dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 928dd308bc3SMichael Ellerman * to the blob data and the size of the data. 929dd308bc3SMichael Ellerman * 930dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 931dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 932dd308bc3SMichael Ellerman * read from. Writing is not supported. 933dd308bc3SMichael Ellerman * 934dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 935dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 936dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 9376468b3afSRandy Dunlap * you are responsible here.) If an error occurs, %NULL will be returned. 938dd308bc3SMichael Ellerman * 9396468b3afSRandy Dunlap * If debugfs is not enabled in the kernel, the value -%ENODEV will be 940dd308bc3SMichael Ellerman * returned. It is not wise to check for this value, but rather, check for 9416468b3afSRandy Dunlap * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 942dd308bc3SMichael Ellerman * code. 943dd308bc3SMichael Ellerman */ 944f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 945dd308bc3SMichael Ellerman struct dentry *parent, 946dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 947dd308bc3SMichael Ellerman { 94883b711cbSNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob); 949dd308bc3SMichael Ellerman } 950dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 9511a087c6aSAlessandro Rubini 9529fe2a701SSrivatsa Vaddagiri struct array_data { 9539fe2a701SSrivatsa Vaddagiri void *array; 9549fe2a701SSrivatsa Vaddagiri u32 elements; 9559fe2a701SSrivatsa Vaddagiri }; 9569fe2a701SSrivatsa Vaddagiri 957e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize, 958e05e279eSLinus Torvalds u32 *array, int array_size) 9599fe2a701SSrivatsa Vaddagiri { 9609fe2a701SSrivatsa Vaddagiri size_t ret = 0; 9619fe2a701SSrivatsa Vaddagiri 962e05e279eSLinus Torvalds while (--array_size >= 0) { 9639fe2a701SSrivatsa Vaddagiri size_t len; 964e05e279eSLinus Torvalds char term = array_size ? ' ' : '\n'; 9659fe2a701SSrivatsa Vaddagiri 966e05e279eSLinus Torvalds len = snprintf(buf, bufsize, "%u%c", *array++, term); 9679fe2a701SSrivatsa Vaddagiri ret += len; 9689fe2a701SSrivatsa Vaddagiri 9699fe2a701SSrivatsa Vaddagiri buf += len; 9709fe2a701SSrivatsa Vaddagiri bufsize -= len; 9719fe2a701SSrivatsa Vaddagiri } 9729fe2a701SSrivatsa Vaddagiri return ret; 9739fe2a701SSrivatsa Vaddagiri } 9749fe2a701SSrivatsa Vaddagiri 97536048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file) 9769fe2a701SSrivatsa Vaddagiri { 9779fe2a701SSrivatsa Vaddagiri struct array_data *data = inode->i_private; 978e05e279eSLinus Torvalds int size, elements = data->elements; 979e05e279eSLinus Torvalds char *buf; 9809fe2a701SSrivatsa Vaddagiri 981e05e279eSLinus Torvalds /* 982e05e279eSLinus Torvalds * Max size: 983e05e279eSLinus Torvalds * - 10 digits + ' '/'\n' = 11 bytes per number 984e05e279eSLinus Torvalds * - terminating NUL character 985e05e279eSLinus Torvalds */ 986e05e279eSLinus Torvalds size = elements*11; 987e05e279eSLinus Torvalds buf = kmalloc(size+1, GFP_KERNEL); 988e05e279eSLinus Torvalds if (!buf) 98936048853SDavid Rientjes return -ENOMEM; 990e05e279eSLinus Torvalds buf[size] = 0; 991e05e279eSLinus Torvalds 992e05e279eSLinus Torvalds file->private_data = buf; 993e05e279eSLinus Torvalds u32_format_array(buf, size, data->array, data->elements); 994e05e279eSLinus Torvalds 99536048853SDavid Rientjes return nonseekable_open(inode, file); 9969fe2a701SSrivatsa Vaddagiri } 9979fe2a701SSrivatsa Vaddagiri 99836048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 99936048853SDavid Rientjes loff_t *ppos) 100036048853SDavid Rientjes { 100136048853SDavid Rientjes size_t size = strlen(file->private_data); 10029fe2a701SSrivatsa Vaddagiri 10039fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 10049fe2a701SSrivatsa Vaddagiri file->private_data, size); 10059fe2a701SSrivatsa Vaddagiri } 10069fe2a701SSrivatsa Vaddagiri 10079fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 10089fe2a701SSrivatsa Vaddagiri { 10099fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 10109fe2a701SSrivatsa Vaddagiri 10119fe2a701SSrivatsa Vaddagiri return 0; 10129fe2a701SSrivatsa Vaddagiri } 10139fe2a701SSrivatsa Vaddagiri 10149fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 10159fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 10169fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 10179fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 10189fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 10199fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 10209fe2a701SSrivatsa Vaddagiri }; 10219fe2a701SSrivatsa Vaddagiri 10229fe2a701SSrivatsa Vaddagiri /** 10239fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 10249fe2a701SSrivatsa Vaddagiri * array. 10259fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 10269fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 10279fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 10289fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 10299fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 10309fe2a701SSrivatsa Vaddagiri * @array: u32 array that provides data. 10319fe2a701SSrivatsa Vaddagiri * @elements: total number of elements in the array. 10329fe2a701SSrivatsa Vaddagiri * 10339fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 10349fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 10359fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 10369fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 10379fe2a701SSrivatsa Vaddagiri * 10389fe2a701SSrivatsa Vaddagiri * The function returns a pointer to dentry on success. If debugfs is not 10399fe2a701SSrivatsa Vaddagiri * enabled in the kernel, the value -%ENODEV will be returned. 10409fe2a701SSrivatsa Vaddagiri */ 10419fe2a701SSrivatsa Vaddagiri struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, 10429fe2a701SSrivatsa Vaddagiri struct dentry *parent, 10439fe2a701SSrivatsa Vaddagiri u32 *array, u32 elements) 10449fe2a701SSrivatsa Vaddagiri { 10459fe2a701SSrivatsa Vaddagiri struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); 10469fe2a701SSrivatsa Vaddagiri 10479fe2a701SSrivatsa Vaddagiri if (data == NULL) 10489fe2a701SSrivatsa Vaddagiri return NULL; 10499fe2a701SSrivatsa Vaddagiri 10509fe2a701SSrivatsa Vaddagiri data->array = array; 10519fe2a701SSrivatsa Vaddagiri data->elements = elements; 10529fe2a701SSrivatsa Vaddagiri 1053c4a74f63SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, data, 1054c4a74f63SNicolai Stange &u32_array_fops); 10559fe2a701SSrivatsa Vaddagiri } 10569fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 10579fe2a701SSrivatsa Vaddagiri 10583b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 10593b85e4abSHeiko Carstens 10601a087c6aSAlessandro Rubini /* 10611a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 10621a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 10631a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 10641a087c6aSAlessandro Rubini */ 10651a087c6aSAlessandro Rubini 10661a087c6aSAlessandro Rubini /** 10671a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 10681a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 10691a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 1070b5763accSRandy Dunlap * @nregs: the length of the above array 10711a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 10721a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 10731a087c6aSAlessandro Rubini * 10741a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 10751a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 10761a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 10771a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 10781a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 10791a087c6aSAlessandro Rubini * for example configuration of dma channels 10801a087c6aSAlessandro Rubini */ 10819761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 10821a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 10831a087c6aSAlessandro Rubini { 10849761536eSJoe Perches int i; 10851a087c6aSAlessandro Rubini 10861a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 10871a087c6aSAlessandro Rubini if (prefix) 10889761536eSJoe Perches seq_printf(s, "%s", prefix); 10899761536eSJoe Perches seq_printf(s, "%s = 0x%08x\n", regs->name, 10905858441cSDan Carpenter readl(base + regs->offset)); 10919761536eSJoe Perches if (seq_has_overflowed(s)) 10929761536eSJoe Perches break; 10931a087c6aSAlessandro Rubini } 10941a087c6aSAlessandro Rubini } 10951a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 10961a087c6aSAlessandro Rubini 10971a087c6aSAlessandro Rubini static int debugfs_show_regset32(struct seq_file *s, void *data) 10981a087c6aSAlessandro Rubini { 10991a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 11001a087c6aSAlessandro Rubini 11011a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 11021a087c6aSAlessandro Rubini return 0; 11031a087c6aSAlessandro Rubini } 11041a087c6aSAlessandro Rubini 11051a087c6aSAlessandro Rubini static int debugfs_open_regset32(struct inode *inode, struct file *file) 11061a087c6aSAlessandro Rubini { 11071a087c6aSAlessandro Rubini return single_open(file, debugfs_show_regset32, inode->i_private); 11081a087c6aSAlessandro Rubini } 11091a087c6aSAlessandro Rubini 11101a087c6aSAlessandro Rubini static const struct file_operations fops_regset32 = { 11111a087c6aSAlessandro Rubini .open = debugfs_open_regset32, 11121a087c6aSAlessandro Rubini .read = seq_read, 11131a087c6aSAlessandro Rubini .llseek = seq_lseek, 11141a087c6aSAlessandro Rubini .release = single_release, 11151a087c6aSAlessandro Rubini }; 11161a087c6aSAlessandro Rubini 11171a087c6aSAlessandro Rubini /** 11181a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 11191a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 11201a087c6aSAlessandro Rubini * @mode: the permission that the file should have 11211a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 11221a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 11231a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 11241a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 11251a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 11261a087c6aSAlessandro Rubini * address where the register bank is to be found. 11271a087c6aSAlessandro Rubini * 11281a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 11291a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 11301a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 11311a087c6aSAlessandro Rubini * 11321a087c6aSAlessandro Rubini * This function will return a pointer to a dentry if it succeeds. This 11331a087c6aSAlessandro Rubini * pointer must be passed to the debugfs_remove() function when the file is 11341a087c6aSAlessandro Rubini * to be removed (no automatic cleanup happens if your module is unloaded, 11351a087c6aSAlessandro Rubini * you are responsible here.) If an error occurs, %NULL will be returned. 11361a087c6aSAlessandro Rubini * 11371a087c6aSAlessandro Rubini * If debugfs is not enabled in the kernel, the value -%ENODEV will be 11381a087c6aSAlessandro Rubini * returned. It is not wise to check for this value, but rather, check for 11391a087c6aSAlessandro Rubini * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 11401a087c6aSAlessandro Rubini * code. 11411a087c6aSAlessandro Rubini */ 114288187398SAl Viro struct dentry *debugfs_create_regset32(const char *name, umode_t mode, 11431a087c6aSAlessandro Rubini struct dentry *parent, 11441a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 11451a087c6aSAlessandro Rubini { 11461a087c6aSAlessandro Rubini return debugfs_create_file(name, mode, parent, regset, &fops_regset32); 11471a087c6aSAlessandro Rubini } 11481a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 11493b85e4abSHeiko Carstens 11503b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 115198210b7fSArend van Spriel 115298210b7fSArend van Spriel struct debugfs_devm_entry { 115398210b7fSArend van Spriel int (*read)(struct seq_file *seq, void *data); 115498210b7fSArend van Spriel struct device *dev; 115598210b7fSArend van Spriel }; 115698210b7fSArend van Spriel 115798210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 115898210b7fSArend van Spriel { 115998210b7fSArend van Spriel struct debugfs_devm_entry *entry = inode->i_private; 116098210b7fSArend van Spriel 116198210b7fSArend van Spriel return single_open(f, entry->read, entry->dev); 116298210b7fSArend van Spriel } 116398210b7fSArend van Spriel 116498210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = { 116598210b7fSArend van Spriel .owner = THIS_MODULE, 116698210b7fSArend van Spriel .open = debugfs_devm_entry_open, 116798210b7fSArend van Spriel .release = single_release, 116898210b7fSArend van Spriel .read = seq_read, 116998210b7fSArend van Spriel .llseek = seq_lseek 117098210b7fSArend van Spriel }; 117198210b7fSArend van Spriel 117298210b7fSArend van Spriel /** 117398210b7fSArend van Spriel * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 117498210b7fSArend van Spriel * 117598210b7fSArend van Spriel * @dev: device related to this debugfs file. 117698210b7fSArend van Spriel * @name: name of the debugfs file. 117798210b7fSArend van Spriel * @parent: a pointer to the parent dentry for this file. This should be a 117898210b7fSArend van Spriel * directory dentry if set. If this parameter is %NULL, then the 117998210b7fSArend van Spriel * file will be created in the root of the debugfs filesystem. 118098210b7fSArend van Spriel * @read_fn: function pointer called to print the seq_file content. 118198210b7fSArend van Spriel */ 118298210b7fSArend van Spriel struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, 118398210b7fSArend van Spriel struct dentry *parent, 118498210b7fSArend van Spriel int (*read_fn)(struct seq_file *s, 118598210b7fSArend van Spriel void *data)) 118698210b7fSArend van Spriel { 118798210b7fSArend van Spriel struct debugfs_devm_entry *entry; 118898210b7fSArend van Spriel 118998210b7fSArend van Spriel if (IS_ERR(parent)) 119098210b7fSArend van Spriel return ERR_PTR(-ENOENT); 119198210b7fSArend van Spriel 119298210b7fSArend van Spriel entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 119398210b7fSArend van Spriel if (!entry) 119498210b7fSArend van Spriel return ERR_PTR(-ENOMEM); 119598210b7fSArend van Spriel 119698210b7fSArend van Spriel entry->read = read_fn; 119798210b7fSArend van Spriel entry->dev = dev; 119898210b7fSArend van Spriel 119998210b7fSArend van Spriel return debugfs_create_file(name, S_IRUGO, parent, entry, 120098210b7fSArend van Spriel &debugfs_devm_entry_ops); 120198210b7fSArend van Spriel } 120298210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 120398210b7fSArend van Spriel 1204