162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * file.c - part of debugfs, a tiny little debug file system 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> 662306a36Sopenharmony_ci * Copyright (C) 2004 IBM Inc. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * debugfs is for people to use instead of /proc or /sys. 962306a36Sopenharmony_ci * See Documentation/filesystems/ for more details. 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/module.h> 1362306a36Sopenharmony_ci#include <linux/fs.h> 1462306a36Sopenharmony_ci#include <linux/seq_file.h> 1562306a36Sopenharmony_ci#include <linux/pagemap.h> 1662306a36Sopenharmony_ci#include <linux/debugfs.h> 1762306a36Sopenharmony_ci#include <linux/io.h> 1862306a36Sopenharmony_ci#include <linux/slab.h> 1962306a36Sopenharmony_ci#include <linux/atomic.h> 2062306a36Sopenharmony_ci#include <linux/device.h> 2162306a36Sopenharmony_ci#include <linux/pm_runtime.h> 2262306a36Sopenharmony_ci#include <linux/poll.h> 2362306a36Sopenharmony_ci#include <linux/security.h> 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include "internal.h" 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistruct poll_table_struct; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic ssize_t default_read_file(struct file *file, char __user *buf, 3062306a36Sopenharmony_ci size_t count, loff_t *ppos) 3162306a36Sopenharmony_ci{ 3262306a36Sopenharmony_ci return 0; 3362306a36Sopenharmony_ci} 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cistatic ssize_t default_write_file(struct file *file, const char __user *buf, 3662306a36Sopenharmony_ci size_t count, loff_t *ppos) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci return count; 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ciconst struct file_operations debugfs_noop_file_operations = { 4262306a36Sopenharmony_ci .read = default_read_file, 4362306a36Sopenharmony_ci .write = default_write_file, 4462306a36Sopenharmony_ci .open = simple_open, 4562306a36Sopenharmony_ci .llseek = noop_llseek, 4662306a36Sopenharmony_ci}; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci#define F_DENTRY(filp) ((filp)->f_path.dentry) 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ciconst struct file_operations *debugfs_real_fops(const struct file *filp) 5162306a36Sopenharmony_ci{ 5262306a36Sopenharmony_ci struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) { 5562306a36Sopenharmony_ci /* 5662306a36Sopenharmony_ci * Urgh, we've been called w/o a protecting 5762306a36Sopenharmony_ci * debugfs_file_get(). 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ci WARN_ON(1); 6062306a36Sopenharmony_ci return NULL; 6162306a36Sopenharmony_ci } 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci return fsd->real_fops; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_real_fops); 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/** 6862306a36Sopenharmony_ci * debugfs_file_get - mark the beginning of file data access 6962306a36Sopenharmony_ci * @dentry: the dentry object whose data is being accessed. 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * Up to a matching call to debugfs_file_put(), any successive call 7262306a36Sopenharmony_ci * into the file removing functions debugfs_remove() and 7362306a36Sopenharmony_ci * debugfs_remove_recursive() will block. Since associated private 7462306a36Sopenharmony_ci * file data may only get freed after a successful return of any of 7562306a36Sopenharmony_ci * the removal functions, you may safely access it after a successful 7662306a36Sopenharmony_ci * call to debugfs_file_get() without worrying about lifetime issues. 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * If -%EIO is returned, the file has already been removed and thus, 7962306a36Sopenharmony_ci * it is not safe to access any of its data. If, on the other hand, 8062306a36Sopenharmony_ci * it is allowed to access the file data, zero is returned. 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_ciint debugfs_file_get(struct dentry *dentry) 8362306a36Sopenharmony_ci{ 8462306a36Sopenharmony_ci struct debugfs_fsdata *fsd; 8562306a36Sopenharmony_ci void *d_fsd; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci /* 8862306a36Sopenharmony_ci * This could only happen if some debugfs user erroneously calls 8962306a36Sopenharmony_ci * debugfs_file_get() on a dentry that isn't even a file, let 9062306a36Sopenharmony_ci * them know about it. 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_ci if (WARN_ON(!d_is_reg(dentry))) 9362306a36Sopenharmony_ci return -EINVAL; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci d_fsd = READ_ONCE(dentry->d_fsdata); 9662306a36Sopenharmony_ci if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) { 9762306a36Sopenharmony_ci fsd = d_fsd; 9862306a36Sopenharmony_ci } else { 9962306a36Sopenharmony_ci fsd = kmalloc(sizeof(*fsd), GFP_KERNEL); 10062306a36Sopenharmony_ci if (!fsd) 10162306a36Sopenharmony_ci return -ENOMEM; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci fsd->real_fops = (void *)((unsigned long)d_fsd & 10462306a36Sopenharmony_ci ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 10562306a36Sopenharmony_ci refcount_set(&fsd->active_users, 1); 10662306a36Sopenharmony_ci init_completion(&fsd->active_users_drained); 10762306a36Sopenharmony_ci if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) { 10862306a36Sopenharmony_ci kfree(fsd); 10962306a36Sopenharmony_ci fsd = READ_ONCE(dentry->d_fsdata); 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci } 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci /* 11462306a36Sopenharmony_ci * In case of a successful cmpxchg() above, this check is 11562306a36Sopenharmony_ci * strictly necessary and must follow it, see the comment in 11662306a36Sopenharmony_ci * __debugfs_remove_file(). 11762306a36Sopenharmony_ci * OTOH, if the cmpxchg() hasn't been executed or wasn't 11862306a36Sopenharmony_ci * successful, this serves the purpose of not starving 11962306a36Sopenharmony_ci * removers. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ci if (d_unlinked(dentry)) 12262306a36Sopenharmony_ci return -EIO; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (!refcount_inc_not_zero(&fsd->active_users)) 12562306a36Sopenharmony_ci return -EIO; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci return 0; 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_file_get); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci/** 13262306a36Sopenharmony_ci * debugfs_file_put - mark the end of file data access 13362306a36Sopenharmony_ci * @dentry: the dentry object formerly passed to 13462306a36Sopenharmony_ci * debugfs_file_get(). 13562306a36Sopenharmony_ci * 13662306a36Sopenharmony_ci * Allow any ongoing concurrent call into debugfs_remove() or 13762306a36Sopenharmony_ci * debugfs_remove_recursive() blocked by a former call to 13862306a36Sopenharmony_ci * debugfs_file_get() to proceed and return to its caller. 13962306a36Sopenharmony_ci */ 14062306a36Sopenharmony_civoid debugfs_file_put(struct dentry *dentry) 14162306a36Sopenharmony_ci{ 14262306a36Sopenharmony_ci struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci if (refcount_dec_and_test(&fsd->active_users)) 14562306a36Sopenharmony_ci complete(&fsd->active_users_drained); 14662306a36Sopenharmony_ci} 14762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_file_put); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* 15062306a36Sopenharmony_ci * Only permit access to world-readable files when the kernel is locked down. 15162306a36Sopenharmony_ci * We also need to exclude any file that has ways to write or alter it as root 15262306a36Sopenharmony_ci * can bypass the permissions check. 15362306a36Sopenharmony_ci */ 15462306a36Sopenharmony_cistatic int debugfs_locked_down(struct inode *inode, 15562306a36Sopenharmony_ci struct file *filp, 15662306a36Sopenharmony_ci const struct file_operations *real_fops) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci if ((inode->i_mode & 07777 & ~0444) == 0 && 15962306a36Sopenharmony_ci !(filp->f_mode & FMODE_WRITE) && 16062306a36Sopenharmony_ci !real_fops->unlocked_ioctl && 16162306a36Sopenharmony_ci !real_fops->compat_ioctl && 16262306a36Sopenharmony_ci !real_fops->mmap) 16362306a36Sopenharmony_ci return 0; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci if (security_locked_down(LOCKDOWN_DEBUGFS)) 16662306a36Sopenharmony_ci return -EPERM; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci return 0; 16962306a36Sopenharmony_ci} 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_cistatic int open_proxy_open(struct inode *inode, struct file *filp) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(filp); 17462306a36Sopenharmony_ci const struct file_operations *real_fops = NULL; 17562306a36Sopenharmony_ci int r; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci r = debugfs_file_get(dentry); 17862306a36Sopenharmony_ci if (r) 17962306a36Sopenharmony_ci return r == -EIO ? -ENOENT : r; 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci real_fops = debugfs_real_fops(filp); 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci r = debugfs_locked_down(inode, filp, real_fops); 18462306a36Sopenharmony_ci if (r) 18562306a36Sopenharmony_ci goto out; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci if (!fops_get(real_fops)) { 18862306a36Sopenharmony_ci#ifdef CONFIG_MODULES 18962306a36Sopenharmony_ci if (real_fops->owner && 19062306a36Sopenharmony_ci real_fops->owner->state == MODULE_STATE_GOING) { 19162306a36Sopenharmony_ci r = -ENXIO; 19262306a36Sopenharmony_ci goto out; 19362306a36Sopenharmony_ci } 19462306a36Sopenharmony_ci#endif 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci /* Huh? Module did not clean up after itself at exit? */ 19762306a36Sopenharmony_ci WARN(1, "debugfs file owner did not clean up at exit: %pd", 19862306a36Sopenharmony_ci dentry); 19962306a36Sopenharmony_ci r = -ENXIO; 20062306a36Sopenharmony_ci goto out; 20162306a36Sopenharmony_ci } 20262306a36Sopenharmony_ci replace_fops(filp, real_fops); 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci if (real_fops->open) 20562306a36Sopenharmony_ci r = real_fops->open(inode, filp); 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ciout: 20862306a36Sopenharmony_ci debugfs_file_put(dentry); 20962306a36Sopenharmony_ci return r; 21062306a36Sopenharmony_ci} 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ciconst struct file_operations debugfs_open_proxy_file_operations = { 21362306a36Sopenharmony_ci .open = open_proxy_open, 21462306a36Sopenharmony_ci}; 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci#define PROTO(args...) args 21762306a36Sopenharmony_ci#define ARGS(args...) args 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ 22062306a36Sopenharmony_cistatic ret_type full_proxy_ ## name(proto) \ 22162306a36Sopenharmony_ci{ \ 22262306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(filp); \ 22362306a36Sopenharmony_ci const struct file_operations *real_fops; \ 22462306a36Sopenharmony_ci ret_type r; \ 22562306a36Sopenharmony_ci \ 22662306a36Sopenharmony_ci r = debugfs_file_get(dentry); \ 22762306a36Sopenharmony_ci if (unlikely(r)) \ 22862306a36Sopenharmony_ci return r; \ 22962306a36Sopenharmony_ci real_fops = debugfs_real_fops(filp); \ 23062306a36Sopenharmony_ci r = real_fops->name(args); \ 23162306a36Sopenharmony_ci debugfs_file_put(dentry); \ 23262306a36Sopenharmony_ci return r; \ 23362306a36Sopenharmony_ci} 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ciFULL_PROXY_FUNC(llseek, loff_t, filp, 23662306a36Sopenharmony_ci PROTO(struct file *filp, loff_t offset, int whence), 23762306a36Sopenharmony_ci ARGS(filp, offset, whence)); 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ciFULL_PROXY_FUNC(read, ssize_t, filp, 24062306a36Sopenharmony_ci PROTO(struct file *filp, char __user *buf, size_t size, 24162306a36Sopenharmony_ci loff_t *ppos), 24262306a36Sopenharmony_ci ARGS(filp, buf, size, ppos)); 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciFULL_PROXY_FUNC(write, ssize_t, filp, 24562306a36Sopenharmony_ci PROTO(struct file *filp, const char __user *buf, size_t size, 24662306a36Sopenharmony_ci loff_t *ppos), 24762306a36Sopenharmony_ci ARGS(filp, buf, size, ppos)); 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciFULL_PROXY_FUNC(unlocked_ioctl, long, filp, 25062306a36Sopenharmony_ci PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 25162306a36Sopenharmony_ci ARGS(filp, cmd, arg)); 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_cistatic __poll_t full_proxy_poll(struct file *filp, 25462306a36Sopenharmony_ci struct poll_table_struct *wait) 25562306a36Sopenharmony_ci{ 25662306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(filp); 25762306a36Sopenharmony_ci __poll_t r = 0; 25862306a36Sopenharmony_ci const struct file_operations *real_fops; 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci if (debugfs_file_get(dentry)) 26162306a36Sopenharmony_ci return EPOLLHUP; 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci real_fops = debugfs_real_fops(filp); 26462306a36Sopenharmony_ci r = real_fops->poll(filp, wait); 26562306a36Sopenharmony_ci debugfs_file_put(dentry); 26662306a36Sopenharmony_ci return r; 26762306a36Sopenharmony_ci} 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_cistatic int full_proxy_release(struct inode *inode, struct file *filp) 27062306a36Sopenharmony_ci{ 27162306a36Sopenharmony_ci const struct dentry *dentry = F_DENTRY(filp); 27262306a36Sopenharmony_ci const struct file_operations *real_fops = debugfs_real_fops(filp); 27362306a36Sopenharmony_ci const struct file_operations *proxy_fops = filp->f_op; 27462306a36Sopenharmony_ci int r = 0; 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci /* 27762306a36Sopenharmony_ci * We must not protect this against removal races here: the 27862306a36Sopenharmony_ci * original releaser should be called unconditionally in order 27962306a36Sopenharmony_ci * not to leak any resources. Releasers must not assume that 28062306a36Sopenharmony_ci * ->i_private is still being meaningful here. 28162306a36Sopenharmony_ci */ 28262306a36Sopenharmony_ci if (real_fops->release) 28362306a36Sopenharmony_ci r = real_fops->release(inode, filp); 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci replace_fops(filp, d_inode(dentry)->i_fop); 28662306a36Sopenharmony_ci kfree(proxy_fops); 28762306a36Sopenharmony_ci fops_put(real_fops); 28862306a36Sopenharmony_ci return r; 28962306a36Sopenharmony_ci} 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_cistatic void __full_proxy_fops_init(struct file_operations *proxy_fops, 29262306a36Sopenharmony_ci const struct file_operations *real_fops) 29362306a36Sopenharmony_ci{ 29462306a36Sopenharmony_ci proxy_fops->release = full_proxy_release; 29562306a36Sopenharmony_ci if (real_fops->llseek) 29662306a36Sopenharmony_ci proxy_fops->llseek = full_proxy_llseek; 29762306a36Sopenharmony_ci if (real_fops->read) 29862306a36Sopenharmony_ci proxy_fops->read = full_proxy_read; 29962306a36Sopenharmony_ci if (real_fops->write) 30062306a36Sopenharmony_ci proxy_fops->write = full_proxy_write; 30162306a36Sopenharmony_ci if (real_fops->poll) 30262306a36Sopenharmony_ci proxy_fops->poll = full_proxy_poll; 30362306a36Sopenharmony_ci if (real_fops->unlocked_ioctl) 30462306a36Sopenharmony_ci proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; 30562306a36Sopenharmony_ci} 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_cistatic int full_proxy_open(struct inode *inode, struct file *filp) 30862306a36Sopenharmony_ci{ 30962306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(filp); 31062306a36Sopenharmony_ci const struct file_operations *real_fops = NULL; 31162306a36Sopenharmony_ci struct file_operations *proxy_fops = NULL; 31262306a36Sopenharmony_ci int r; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci r = debugfs_file_get(dentry); 31562306a36Sopenharmony_ci if (r) 31662306a36Sopenharmony_ci return r == -EIO ? -ENOENT : r; 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci real_fops = debugfs_real_fops(filp); 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci r = debugfs_locked_down(inode, filp, real_fops); 32162306a36Sopenharmony_ci if (r) 32262306a36Sopenharmony_ci goto out; 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci if (!fops_get(real_fops)) { 32562306a36Sopenharmony_ci#ifdef CONFIG_MODULES 32662306a36Sopenharmony_ci if (real_fops->owner && 32762306a36Sopenharmony_ci real_fops->owner->state == MODULE_STATE_GOING) { 32862306a36Sopenharmony_ci r = -ENXIO; 32962306a36Sopenharmony_ci goto out; 33062306a36Sopenharmony_ci } 33162306a36Sopenharmony_ci#endif 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci /* Huh? Module did not cleanup after itself at exit? */ 33462306a36Sopenharmony_ci WARN(1, "debugfs file owner did not clean up at exit: %pd", 33562306a36Sopenharmony_ci dentry); 33662306a36Sopenharmony_ci r = -ENXIO; 33762306a36Sopenharmony_ci goto out; 33862306a36Sopenharmony_ci } 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ci proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); 34162306a36Sopenharmony_ci if (!proxy_fops) { 34262306a36Sopenharmony_ci r = -ENOMEM; 34362306a36Sopenharmony_ci goto free_proxy; 34462306a36Sopenharmony_ci } 34562306a36Sopenharmony_ci __full_proxy_fops_init(proxy_fops, real_fops); 34662306a36Sopenharmony_ci replace_fops(filp, proxy_fops); 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci if (real_fops->open) { 34962306a36Sopenharmony_ci r = real_fops->open(inode, filp); 35062306a36Sopenharmony_ci if (r) { 35162306a36Sopenharmony_ci replace_fops(filp, d_inode(dentry)->i_fop); 35262306a36Sopenharmony_ci goto free_proxy; 35362306a36Sopenharmony_ci } else if (filp->f_op != proxy_fops) { 35462306a36Sopenharmony_ci /* No protection against file removal anymore. */ 35562306a36Sopenharmony_ci WARN(1, "debugfs file owner replaced proxy fops: %pd", 35662306a36Sopenharmony_ci dentry); 35762306a36Sopenharmony_ci goto free_proxy; 35862306a36Sopenharmony_ci } 35962306a36Sopenharmony_ci } 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci goto out; 36262306a36Sopenharmony_cifree_proxy: 36362306a36Sopenharmony_ci kfree(proxy_fops); 36462306a36Sopenharmony_ci fops_put(real_fops); 36562306a36Sopenharmony_ciout: 36662306a36Sopenharmony_ci debugfs_file_put(dentry); 36762306a36Sopenharmony_ci return r; 36862306a36Sopenharmony_ci} 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ciconst struct file_operations debugfs_full_proxy_file_operations = { 37162306a36Sopenharmony_ci .open = full_proxy_open, 37262306a36Sopenharmony_ci}; 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_cissize_t debugfs_attr_read(struct file *file, char __user *buf, 37562306a36Sopenharmony_ci size_t len, loff_t *ppos) 37662306a36Sopenharmony_ci{ 37762306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 37862306a36Sopenharmony_ci ssize_t ret; 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci ret = debugfs_file_get(dentry); 38162306a36Sopenharmony_ci if (unlikely(ret)) 38262306a36Sopenharmony_ci return ret; 38362306a36Sopenharmony_ci ret = simple_attr_read(file, buf, len, ppos); 38462306a36Sopenharmony_ci debugfs_file_put(dentry); 38562306a36Sopenharmony_ci return ret; 38662306a36Sopenharmony_ci} 38762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_attr_read); 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_cistatic ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, 39062306a36Sopenharmony_ci size_t len, loff_t *ppos, bool is_signed) 39162306a36Sopenharmony_ci{ 39262306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 39362306a36Sopenharmony_ci ssize_t ret; 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci ret = debugfs_file_get(dentry); 39662306a36Sopenharmony_ci if (unlikely(ret)) 39762306a36Sopenharmony_ci return ret; 39862306a36Sopenharmony_ci if (is_signed) 39962306a36Sopenharmony_ci ret = simple_attr_write_signed(file, buf, len, ppos); 40062306a36Sopenharmony_ci else 40162306a36Sopenharmony_ci ret = simple_attr_write(file, buf, len, ppos); 40262306a36Sopenharmony_ci debugfs_file_put(dentry); 40362306a36Sopenharmony_ci return ret; 40462306a36Sopenharmony_ci} 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_cissize_t debugfs_attr_write(struct file *file, const char __user *buf, 40762306a36Sopenharmony_ci size_t len, loff_t *ppos) 40862306a36Sopenharmony_ci{ 40962306a36Sopenharmony_ci return debugfs_attr_write_xsigned(file, buf, len, ppos, false); 41062306a36Sopenharmony_ci} 41162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_attr_write); 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_cissize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 41462306a36Sopenharmony_ci size_t len, loff_t *ppos) 41562306a36Sopenharmony_ci{ 41662306a36Sopenharmony_ci return debugfs_attr_write_xsigned(file, buf, len, ppos, true); 41762306a36Sopenharmony_ci} 41862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_attr_write_signed); 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_cistatic struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 42162306a36Sopenharmony_ci struct dentry *parent, void *value, 42262306a36Sopenharmony_ci const struct file_operations *fops, 42362306a36Sopenharmony_ci const struct file_operations *fops_ro, 42462306a36Sopenharmony_ci const struct file_operations *fops_wo) 42562306a36Sopenharmony_ci{ 42662306a36Sopenharmony_ci /* if there are no write bits set, make read only */ 42762306a36Sopenharmony_ci if (!(mode & S_IWUGO)) 42862306a36Sopenharmony_ci return debugfs_create_file_unsafe(name, mode, parent, value, 42962306a36Sopenharmony_ci fops_ro); 43062306a36Sopenharmony_ci /* if there are no read bits set, make write only */ 43162306a36Sopenharmony_ci if (!(mode & S_IRUGO)) 43262306a36Sopenharmony_ci return debugfs_create_file_unsafe(name, mode, parent, value, 43362306a36Sopenharmony_ci fops_wo); 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci return debugfs_create_file_unsafe(name, mode, parent, value, fops); 43662306a36Sopenharmony_ci} 43762306a36Sopenharmony_ci 43862306a36Sopenharmony_cistatic int debugfs_u8_set(void *data, u64 val) 43962306a36Sopenharmony_ci{ 44062306a36Sopenharmony_ci *(u8 *)data = val; 44162306a36Sopenharmony_ci return 0; 44262306a36Sopenharmony_ci} 44362306a36Sopenharmony_cistatic int debugfs_u8_get(void *data, u64 *val) 44462306a36Sopenharmony_ci{ 44562306a36Sopenharmony_ci *val = *(u8 *)data; 44662306a36Sopenharmony_ci return 0; 44762306a36Sopenharmony_ci} 44862306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 44962306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 45062306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci/** 45362306a36Sopenharmony_ci * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 45462306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 45562306a36Sopenharmony_ci * @mode: the permission that the file should have 45662306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 45762306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 45862306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 45962306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 46062306a36Sopenharmony_ci * from. 46162306a36Sopenharmony_ci * 46262306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 46362306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 46462306a36Sopenharmony_ci * set, it can be read from, and written to. 46562306a36Sopenharmony_ci */ 46662306a36Sopenharmony_civoid debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 46762306a36Sopenharmony_ci u8 *value) 46862306a36Sopenharmony_ci{ 46962306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 47062306a36Sopenharmony_ci &fops_u8_ro, &fops_u8_wo); 47162306a36Sopenharmony_ci} 47262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_u8); 47362306a36Sopenharmony_ci 47462306a36Sopenharmony_cistatic int debugfs_u16_set(void *data, u64 val) 47562306a36Sopenharmony_ci{ 47662306a36Sopenharmony_ci *(u16 *)data = val; 47762306a36Sopenharmony_ci return 0; 47862306a36Sopenharmony_ci} 47962306a36Sopenharmony_cistatic int debugfs_u16_get(void *data, u64 *val) 48062306a36Sopenharmony_ci{ 48162306a36Sopenharmony_ci *val = *(u16 *)data; 48262306a36Sopenharmony_ci return 0; 48362306a36Sopenharmony_ci} 48462306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 48562306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 48662306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci/** 48962306a36Sopenharmony_ci * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 49062306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 49162306a36Sopenharmony_ci * @mode: the permission that the file should have 49262306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 49362306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 49462306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 49562306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 49662306a36Sopenharmony_ci * from. 49762306a36Sopenharmony_ci * 49862306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 49962306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 50062306a36Sopenharmony_ci * set, it can be read from, and written to. 50162306a36Sopenharmony_ci */ 50262306a36Sopenharmony_civoid debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 50362306a36Sopenharmony_ci u16 *value) 50462306a36Sopenharmony_ci{ 50562306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 50662306a36Sopenharmony_ci &fops_u16_ro, &fops_u16_wo); 50762306a36Sopenharmony_ci} 50862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_u16); 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_cistatic int debugfs_u32_set(void *data, u64 val) 51162306a36Sopenharmony_ci{ 51262306a36Sopenharmony_ci *(u32 *)data = val; 51362306a36Sopenharmony_ci return 0; 51462306a36Sopenharmony_ci} 51562306a36Sopenharmony_cistatic int debugfs_u32_get(void *data, u64 *val) 51662306a36Sopenharmony_ci{ 51762306a36Sopenharmony_ci *val = *(u32 *)data; 51862306a36Sopenharmony_ci return 0; 51962306a36Sopenharmony_ci} 52062306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 52162306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 52262306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci/** 52562306a36Sopenharmony_ci * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 52662306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 52762306a36Sopenharmony_ci * @mode: the permission that the file should have 52862306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 52962306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 53062306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 53162306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 53262306a36Sopenharmony_ci * from. 53362306a36Sopenharmony_ci * 53462306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 53562306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 53662306a36Sopenharmony_ci * set, it can be read from, and written to. 53762306a36Sopenharmony_ci */ 53862306a36Sopenharmony_civoid debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 53962306a36Sopenharmony_ci u32 *value) 54062306a36Sopenharmony_ci{ 54162306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 54262306a36Sopenharmony_ci &fops_u32_ro, &fops_u32_wo); 54362306a36Sopenharmony_ci} 54462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_u32); 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_cistatic int debugfs_u64_set(void *data, u64 val) 54762306a36Sopenharmony_ci{ 54862306a36Sopenharmony_ci *(u64 *)data = val; 54962306a36Sopenharmony_ci return 0; 55062306a36Sopenharmony_ci} 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_cistatic int debugfs_u64_get(void *data, u64 *val) 55362306a36Sopenharmony_ci{ 55462306a36Sopenharmony_ci *val = *(u64 *)data; 55562306a36Sopenharmony_ci return 0; 55662306a36Sopenharmony_ci} 55762306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 55862306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 55962306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci/** 56262306a36Sopenharmony_ci * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 56362306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 56462306a36Sopenharmony_ci * @mode: the permission that the file should have 56562306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 56662306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 56762306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 56862306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 56962306a36Sopenharmony_ci * from. 57062306a36Sopenharmony_ci * 57162306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 57262306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 57362306a36Sopenharmony_ci * set, it can be read from, and written to. 57462306a36Sopenharmony_ci */ 57562306a36Sopenharmony_civoid debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 57662306a36Sopenharmony_ci u64 *value) 57762306a36Sopenharmony_ci{ 57862306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 57962306a36Sopenharmony_ci &fops_u64_ro, &fops_u64_wo); 58062306a36Sopenharmony_ci} 58162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_u64); 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_cistatic int debugfs_ulong_set(void *data, u64 val) 58462306a36Sopenharmony_ci{ 58562306a36Sopenharmony_ci *(unsigned long *)data = val; 58662306a36Sopenharmony_ci return 0; 58762306a36Sopenharmony_ci} 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_cistatic int debugfs_ulong_get(void *data, u64 *val) 59062306a36Sopenharmony_ci{ 59162306a36Sopenharmony_ci *val = *(unsigned long *)data; 59262306a36Sopenharmony_ci return 0; 59362306a36Sopenharmony_ci} 59462306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 59562306a36Sopenharmony_ci "%llu\n"); 59662306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 59762306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci/** 60062306a36Sopenharmony_ci * debugfs_create_ulong - create a debugfs file that is used to read and write 60162306a36Sopenharmony_ci * an unsigned long value. 60262306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 60362306a36Sopenharmony_ci * @mode: the permission that the file should have 60462306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 60562306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 60662306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 60762306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 60862306a36Sopenharmony_ci * from. 60962306a36Sopenharmony_ci * 61062306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 61162306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 61262306a36Sopenharmony_ci * set, it can be read from, and written to. 61362306a36Sopenharmony_ci */ 61462306a36Sopenharmony_civoid debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 61562306a36Sopenharmony_ci unsigned long *value) 61662306a36Sopenharmony_ci{ 61762306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 61862306a36Sopenharmony_ci &fops_ulong_ro, &fops_ulong_wo); 61962306a36Sopenharmony_ci} 62062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_ulong); 62162306a36Sopenharmony_ci 62262306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 62362306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 62462306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 62762306a36Sopenharmony_ci "0x%04llx\n"); 62862306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 62962306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 63062306a36Sopenharmony_ci 63162306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 63262306a36Sopenharmony_ci "0x%08llx\n"); 63362306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 63462306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 63762306a36Sopenharmony_ci "0x%016llx\n"); 63862306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 63962306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 64062306a36Sopenharmony_ci 64162306a36Sopenharmony_ci/* 64262306a36Sopenharmony_ci * 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 64362306a36Sopenharmony_ci * 64462306a36Sopenharmony_ci * These functions are exactly the same as the above functions (but use a hex 64562306a36Sopenharmony_ci * output for the decimal challenged). For details look at the above unsigned 64662306a36Sopenharmony_ci * decimal functions. 64762306a36Sopenharmony_ci */ 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci/** 65062306a36Sopenharmony_ci * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 65162306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 65262306a36Sopenharmony_ci * @mode: the permission that the file should have 65362306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 65462306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 65562306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 65662306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 65762306a36Sopenharmony_ci * from. 65862306a36Sopenharmony_ci */ 65962306a36Sopenharmony_civoid debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 66062306a36Sopenharmony_ci u8 *value) 66162306a36Sopenharmony_ci{ 66262306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 66362306a36Sopenharmony_ci &fops_x8_ro, &fops_x8_wo); 66462306a36Sopenharmony_ci} 66562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_x8); 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci/** 66862306a36Sopenharmony_ci * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 66962306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 67062306a36Sopenharmony_ci * @mode: the permission that the file should have 67162306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 67262306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 67362306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 67462306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 67562306a36Sopenharmony_ci * from. 67662306a36Sopenharmony_ci */ 67762306a36Sopenharmony_civoid debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 67862306a36Sopenharmony_ci u16 *value) 67962306a36Sopenharmony_ci{ 68062306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 68162306a36Sopenharmony_ci &fops_x16_ro, &fops_x16_wo); 68262306a36Sopenharmony_ci} 68362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_x16); 68462306a36Sopenharmony_ci 68562306a36Sopenharmony_ci/** 68662306a36Sopenharmony_ci * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 68762306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 68862306a36Sopenharmony_ci * @mode: the permission that the file should have 68962306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 69062306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 69162306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 69262306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 69362306a36Sopenharmony_ci * from. 69462306a36Sopenharmony_ci */ 69562306a36Sopenharmony_civoid debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 69662306a36Sopenharmony_ci u32 *value) 69762306a36Sopenharmony_ci{ 69862306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 69962306a36Sopenharmony_ci &fops_x32_ro, &fops_x32_wo); 70062306a36Sopenharmony_ci} 70162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_x32); 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ci/** 70462306a36Sopenharmony_ci * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 70562306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 70662306a36Sopenharmony_ci * @mode: the permission that the file should have 70762306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 70862306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 70962306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 71062306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 71162306a36Sopenharmony_ci * from. 71262306a36Sopenharmony_ci */ 71362306a36Sopenharmony_civoid debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 71462306a36Sopenharmony_ci u64 *value) 71562306a36Sopenharmony_ci{ 71662306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 71762306a36Sopenharmony_ci &fops_x64_ro, &fops_x64_wo); 71862306a36Sopenharmony_ci} 71962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_x64); 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_ci 72262306a36Sopenharmony_cistatic int debugfs_size_t_set(void *data, u64 val) 72362306a36Sopenharmony_ci{ 72462306a36Sopenharmony_ci *(size_t *)data = val; 72562306a36Sopenharmony_ci return 0; 72662306a36Sopenharmony_ci} 72762306a36Sopenharmony_cistatic int debugfs_size_t_get(void *data, u64 *val) 72862306a36Sopenharmony_ci{ 72962306a36Sopenharmony_ci *val = *(size_t *)data; 73062306a36Sopenharmony_ci return 0; 73162306a36Sopenharmony_ci} 73262306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 73362306a36Sopenharmony_ci "%llu\n"); /* %llu and %zu are more or less the same */ 73462306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 73562306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 73662306a36Sopenharmony_ci 73762306a36Sopenharmony_ci/** 73862306a36Sopenharmony_ci * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 73962306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 74062306a36Sopenharmony_ci * @mode: the permission that the file should have 74162306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 74262306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 74362306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 74462306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 74562306a36Sopenharmony_ci * from. 74662306a36Sopenharmony_ci */ 74762306a36Sopenharmony_civoid debugfs_create_size_t(const char *name, umode_t mode, 74862306a36Sopenharmony_ci struct dentry *parent, size_t *value) 74962306a36Sopenharmony_ci{ 75062306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 75162306a36Sopenharmony_ci &fops_size_t_ro, &fops_size_t_wo); 75262306a36Sopenharmony_ci} 75362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_size_t); 75462306a36Sopenharmony_ci 75562306a36Sopenharmony_cistatic int debugfs_atomic_t_set(void *data, u64 val) 75662306a36Sopenharmony_ci{ 75762306a36Sopenharmony_ci atomic_set((atomic_t *)data, val); 75862306a36Sopenharmony_ci return 0; 75962306a36Sopenharmony_ci} 76062306a36Sopenharmony_cistatic int debugfs_atomic_t_get(void *data, u64 *val) 76162306a36Sopenharmony_ci{ 76262306a36Sopenharmony_ci *val = atomic_read((atomic_t *)data); 76362306a36Sopenharmony_ci return 0; 76462306a36Sopenharmony_ci} 76562306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, 76662306a36Sopenharmony_ci debugfs_atomic_t_set, "%lld\n"); 76762306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 76862306a36Sopenharmony_ci "%lld\n"); 76962306a36Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 77062306a36Sopenharmony_ci "%lld\n"); 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ci/** 77362306a36Sopenharmony_ci * debugfs_create_atomic_t - create a debugfs file that is used to read and 77462306a36Sopenharmony_ci * write an atomic_t value 77562306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 77662306a36Sopenharmony_ci * @mode: the permission that the file should have 77762306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 77862306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 77962306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 78062306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 78162306a36Sopenharmony_ci * from. 78262306a36Sopenharmony_ci */ 78362306a36Sopenharmony_civoid debugfs_create_atomic_t(const char *name, umode_t mode, 78462306a36Sopenharmony_ci struct dentry *parent, atomic_t *value) 78562306a36Sopenharmony_ci{ 78662306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 78762306a36Sopenharmony_ci &fops_atomic_t_ro, &fops_atomic_t_wo); 78862306a36Sopenharmony_ci} 78962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_cissize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 79262306a36Sopenharmony_ci size_t count, loff_t *ppos) 79362306a36Sopenharmony_ci{ 79462306a36Sopenharmony_ci char buf[2]; 79562306a36Sopenharmony_ci bool val; 79662306a36Sopenharmony_ci int r; 79762306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 79862306a36Sopenharmony_ci 79962306a36Sopenharmony_ci r = debugfs_file_get(dentry); 80062306a36Sopenharmony_ci if (unlikely(r)) 80162306a36Sopenharmony_ci return r; 80262306a36Sopenharmony_ci val = *(bool *)file->private_data; 80362306a36Sopenharmony_ci debugfs_file_put(dentry); 80462306a36Sopenharmony_ci 80562306a36Sopenharmony_ci if (val) 80662306a36Sopenharmony_ci buf[0] = 'Y'; 80762306a36Sopenharmony_ci else 80862306a36Sopenharmony_ci buf[0] = 'N'; 80962306a36Sopenharmony_ci buf[1] = '\n'; 81062306a36Sopenharmony_ci return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 81162306a36Sopenharmony_ci} 81262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_read_file_bool); 81362306a36Sopenharmony_ci 81462306a36Sopenharmony_cissize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 81562306a36Sopenharmony_ci size_t count, loff_t *ppos) 81662306a36Sopenharmony_ci{ 81762306a36Sopenharmony_ci bool bv; 81862306a36Sopenharmony_ci int r; 81962306a36Sopenharmony_ci bool *val = file->private_data; 82062306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci r = kstrtobool_from_user(user_buf, count, &bv); 82362306a36Sopenharmony_ci if (!r) { 82462306a36Sopenharmony_ci r = debugfs_file_get(dentry); 82562306a36Sopenharmony_ci if (unlikely(r)) 82662306a36Sopenharmony_ci return r; 82762306a36Sopenharmony_ci *val = bv; 82862306a36Sopenharmony_ci debugfs_file_put(dentry); 82962306a36Sopenharmony_ci } 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci return count; 83262306a36Sopenharmony_ci} 83362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_write_file_bool); 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_cistatic const struct file_operations fops_bool = { 83662306a36Sopenharmony_ci .read = debugfs_read_file_bool, 83762306a36Sopenharmony_ci .write = debugfs_write_file_bool, 83862306a36Sopenharmony_ci .open = simple_open, 83962306a36Sopenharmony_ci .llseek = default_llseek, 84062306a36Sopenharmony_ci}; 84162306a36Sopenharmony_ci 84262306a36Sopenharmony_cistatic const struct file_operations fops_bool_ro = { 84362306a36Sopenharmony_ci .read = debugfs_read_file_bool, 84462306a36Sopenharmony_ci .open = simple_open, 84562306a36Sopenharmony_ci .llseek = default_llseek, 84662306a36Sopenharmony_ci}; 84762306a36Sopenharmony_ci 84862306a36Sopenharmony_cistatic const struct file_operations fops_bool_wo = { 84962306a36Sopenharmony_ci .write = debugfs_write_file_bool, 85062306a36Sopenharmony_ci .open = simple_open, 85162306a36Sopenharmony_ci .llseek = default_llseek, 85262306a36Sopenharmony_ci}; 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci/** 85562306a36Sopenharmony_ci * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 85662306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 85762306a36Sopenharmony_ci * @mode: the permission that the file should have 85862306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 85962306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 86062306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 86162306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 86262306a36Sopenharmony_ci * from. 86362306a36Sopenharmony_ci * 86462306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 86562306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 86662306a36Sopenharmony_ci * set, it can be read from, and written to. 86762306a36Sopenharmony_ci */ 86862306a36Sopenharmony_civoid debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 86962306a36Sopenharmony_ci bool *value) 87062306a36Sopenharmony_ci{ 87162306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 87262306a36Sopenharmony_ci &fops_bool_ro, &fops_bool_wo); 87362306a36Sopenharmony_ci} 87462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_bool); 87562306a36Sopenharmony_ci 87662306a36Sopenharmony_cissize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 87762306a36Sopenharmony_ci size_t count, loff_t *ppos) 87862306a36Sopenharmony_ci{ 87962306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 88062306a36Sopenharmony_ci char *str, *copy = NULL; 88162306a36Sopenharmony_ci int copy_len, len; 88262306a36Sopenharmony_ci ssize_t ret; 88362306a36Sopenharmony_ci 88462306a36Sopenharmony_ci ret = debugfs_file_get(dentry); 88562306a36Sopenharmony_ci if (unlikely(ret)) 88662306a36Sopenharmony_ci return ret; 88762306a36Sopenharmony_ci 88862306a36Sopenharmony_ci str = *(char **)file->private_data; 88962306a36Sopenharmony_ci len = strlen(str) + 1; 89062306a36Sopenharmony_ci copy = kmalloc(len, GFP_KERNEL); 89162306a36Sopenharmony_ci if (!copy) { 89262306a36Sopenharmony_ci debugfs_file_put(dentry); 89362306a36Sopenharmony_ci return -ENOMEM; 89462306a36Sopenharmony_ci } 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci copy_len = strscpy(copy, str, len); 89762306a36Sopenharmony_ci debugfs_file_put(dentry); 89862306a36Sopenharmony_ci if (copy_len < 0) { 89962306a36Sopenharmony_ci kfree(copy); 90062306a36Sopenharmony_ci return copy_len; 90162306a36Sopenharmony_ci } 90262306a36Sopenharmony_ci 90362306a36Sopenharmony_ci copy[copy_len] = '\n'; 90462306a36Sopenharmony_ci 90562306a36Sopenharmony_ci ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); 90662306a36Sopenharmony_ci kfree(copy); 90762306a36Sopenharmony_ci 90862306a36Sopenharmony_ci return ret; 90962306a36Sopenharmony_ci} 91062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_str); 91162306a36Sopenharmony_ci 91262306a36Sopenharmony_cistatic ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, 91362306a36Sopenharmony_ci size_t count, loff_t *ppos) 91462306a36Sopenharmony_ci{ 91562306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 91662306a36Sopenharmony_ci char *old, *new = NULL; 91762306a36Sopenharmony_ci int pos = *ppos; 91862306a36Sopenharmony_ci int r; 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci r = debugfs_file_get(dentry); 92162306a36Sopenharmony_ci if (unlikely(r)) 92262306a36Sopenharmony_ci return r; 92362306a36Sopenharmony_ci 92462306a36Sopenharmony_ci old = *(char **)file->private_data; 92562306a36Sopenharmony_ci 92662306a36Sopenharmony_ci /* only allow strict concatenation */ 92762306a36Sopenharmony_ci r = -EINVAL; 92862306a36Sopenharmony_ci if (pos && pos != strlen(old)) 92962306a36Sopenharmony_ci goto error; 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_ci r = -E2BIG; 93262306a36Sopenharmony_ci if (pos + count + 1 > PAGE_SIZE) 93362306a36Sopenharmony_ci goto error; 93462306a36Sopenharmony_ci 93562306a36Sopenharmony_ci r = -ENOMEM; 93662306a36Sopenharmony_ci new = kmalloc(pos + count + 1, GFP_KERNEL); 93762306a36Sopenharmony_ci if (!new) 93862306a36Sopenharmony_ci goto error; 93962306a36Sopenharmony_ci 94062306a36Sopenharmony_ci if (pos) 94162306a36Sopenharmony_ci memcpy(new, old, pos); 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci r = -EFAULT; 94462306a36Sopenharmony_ci if (copy_from_user(new + pos, user_buf, count)) 94562306a36Sopenharmony_ci goto error; 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_ci new[pos + count] = '\0'; 94862306a36Sopenharmony_ci strim(new); 94962306a36Sopenharmony_ci 95062306a36Sopenharmony_ci rcu_assign_pointer(*(char __rcu **)file->private_data, new); 95162306a36Sopenharmony_ci synchronize_rcu(); 95262306a36Sopenharmony_ci kfree(old); 95362306a36Sopenharmony_ci 95462306a36Sopenharmony_ci debugfs_file_put(dentry); 95562306a36Sopenharmony_ci return count; 95662306a36Sopenharmony_ci 95762306a36Sopenharmony_cierror: 95862306a36Sopenharmony_ci kfree(new); 95962306a36Sopenharmony_ci debugfs_file_put(dentry); 96062306a36Sopenharmony_ci return r; 96162306a36Sopenharmony_ci} 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_cistatic const struct file_operations fops_str = { 96462306a36Sopenharmony_ci .read = debugfs_read_file_str, 96562306a36Sopenharmony_ci .write = debugfs_write_file_str, 96662306a36Sopenharmony_ci .open = simple_open, 96762306a36Sopenharmony_ci .llseek = default_llseek, 96862306a36Sopenharmony_ci}; 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_cistatic const struct file_operations fops_str_ro = { 97162306a36Sopenharmony_ci .read = debugfs_read_file_str, 97262306a36Sopenharmony_ci .open = simple_open, 97362306a36Sopenharmony_ci .llseek = default_llseek, 97462306a36Sopenharmony_ci}; 97562306a36Sopenharmony_ci 97662306a36Sopenharmony_cistatic const struct file_operations fops_str_wo = { 97762306a36Sopenharmony_ci .write = debugfs_write_file_str, 97862306a36Sopenharmony_ci .open = simple_open, 97962306a36Sopenharmony_ci .llseek = default_llseek, 98062306a36Sopenharmony_ci}; 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci/** 98362306a36Sopenharmony_ci * debugfs_create_str - create a debugfs file that is used to read and write a string value 98462306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 98562306a36Sopenharmony_ci * @mode: the permission that the file should have 98662306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 98762306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 98862306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 98962306a36Sopenharmony_ci * @value: a pointer to the variable that the file should read to and write 99062306a36Sopenharmony_ci * from. 99162306a36Sopenharmony_ci * 99262306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that 99362306a36Sopenharmony_ci * contains the value of the variable @value. If the @mode variable is so 99462306a36Sopenharmony_ci * set, it can be read from, and written to. 99562306a36Sopenharmony_ci */ 99662306a36Sopenharmony_civoid debugfs_create_str(const char *name, umode_t mode, 99762306a36Sopenharmony_ci struct dentry *parent, char **value) 99862306a36Sopenharmony_ci{ 99962306a36Sopenharmony_ci debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 100062306a36Sopenharmony_ci &fops_str_ro, &fops_str_wo); 100162306a36Sopenharmony_ci} 100262306a36Sopenharmony_ci 100362306a36Sopenharmony_cistatic ssize_t read_file_blob(struct file *file, char __user *user_buf, 100462306a36Sopenharmony_ci size_t count, loff_t *ppos) 100562306a36Sopenharmony_ci{ 100662306a36Sopenharmony_ci struct debugfs_blob_wrapper *blob = file->private_data; 100762306a36Sopenharmony_ci struct dentry *dentry = F_DENTRY(file); 100862306a36Sopenharmony_ci ssize_t r; 100962306a36Sopenharmony_ci 101062306a36Sopenharmony_ci r = debugfs_file_get(dentry); 101162306a36Sopenharmony_ci if (unlikely(r)) 101262306a36Sopenharmony_ci return r; 101362306a36Sopenharmony_ci r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 101462306a36Sopenharmony_ci blob->size); 101562306a36Sopenharmony_ci debugfs_file_put(dentry); 101662306a36Sopenharmony_ci return r; 101762306a36Sopenharmony_ci} 101862306a36Sopenharmony_ci 101962306a36Sopenharmony_cistatic const struct file_operations fops_blob = { 102062306a36Sopenharmony_ci .read = read_file_blob, 102162306a36Sopenharmony_ci .open = simple_open, 102262306a36Sopenharmony_ci .llseek = default_llseek, 102362306a36Sopenharmony_ci}; 102462306a36Sopenharmony_ci 102562306a36Sopenharmony_ci/** 102662306a36Sopenharmony_ci * debugfs_create_blob - create a debugfs file that is used to read a binary blob 102762306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 102862306a36Sopenharmony_ci * @mode: the read permission that the file should have (other permissions are 102962306a36Sopenharmony_ci * masked out) 103062306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 103162306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 103262306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 103362306a36Sopenharmony_ci * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 103462306a36Sopenharmony_ci * to the blob data and the size of the data. 103562306a36Sopenharmony_ci * 103662306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that exports 103762306a36Sopenharmony_ci * @blob->data as a binary blob. If the @mode variable is so set it can be 103862306a36Sopenharmony_ci * read from. Writing is not supported. 103962306a36Sopenharmony_ci * 104062306a36Sopenharmony_ci * This function will return a pointer to a dentry if it succeeds. This 104162306a36Sopenharmony_ci * pointer must be passed to the debugfs_remove() function when the file is 104262306a36Sopenharmony_ci * to be removed (no automatic cleanup happens if your module is unloaded, 104362306a36Sopenharmony_ci * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 104462306a36Sopenharmony_ci * returned. 104562306a36Sopenharmony_ci * 104662306a36Sopenharmony_ci * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 104762306a36Sopenharmony_ci * be returned. 104862306a36Sopenharmony_ci */ 104962306a36Sopenharmony_cistruct dentry *debugfs_create_blob(const char *name, umode_t mode, 105062306a36Sopenharmony_ci struct dentry *parent, 105162306a36Sopenharmony_ci struct debugfs_blob_wrapper *blob) 105262306a36Sopenharmony_ci{ 105362306a36Sopenharmony_ci return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob); 105462306a36Sopenharmony_ci} 105562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_blob); 105662306a36Sopenharmony_ci 105762306a36Sopenharmony_cistatic size_t u32_format_array(char *buf, size_t bufsize, 105862306a36Sopenharmony_ci u32 *array, int array_size) 105962306a36Sopenharmony_ci{ 106062306a36Sopenharmony_ci size_t ret = 0; 106162306a36Sopenharmony_ci 106262306a36Sopenharmony_ci while (--array_size >= 0) { 106362306a36Sopenharmony_ci size_t len; 106462306a36Sopenharmony_ci char term = array_size ? ' ' : '\n'; 106562306a36Sopenharmony_ci 106662306a36Sopenharmony_ci len = snprintf(buf, bufsize, "%u%c", *array++, term); 106762306a36Sopenharmony_ci ret += len; 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci buf += len; 107062306a36Sopenharmony_ci bufsize -= len; 107162306a36Sopenharmony_ci } 107262306a36Sopenharmony_ci return ret; 107362306a36Sopenharmony_ci} 107462306a36Sopenharmony_ci 107562306a36Sopenharmony_cistatic int u32_array_open(struct inode *inode, struct file *file) 107662306a36Sopenharmony_ci{ 107762306a36Sopenharmony_ci struct debugfs_u32_array *data = inode->i_private; 107862306a36Sopenharmony_ci int size, elements = data->n_elements; 107962306a36Sopenharmony_ci char *buf; 108062306a36Sopenharmony_ci 108162306a36Sopenharmony_ci /* 108262306a36Sopenharmony_ci * Max size: 108362306a36Sopenharmony_ci * - 10 digits + ' '/'\n' = 11 bytes per number 108462306a36Sopenharmony_ci * - terminating NUL character 108562306a36Sopenharmony_ci */ 108662306a36Sopenharmony_ci size = elements*11; 108762306a36Sopenharmony_ci buf = kmalloc(size+1, GFP_KERNEL); 108862306a36Sopenharmony_ci if (!buf) 108962306a36Sopenharmony_ci return -ENOMEM; 109062306a36Sopenharmony_ci buf[size] = 0; 109162306a36Sopenharmony_ci 109262306a36Sopenharmony_ci file->private_data = buf; 109362306a36Sopenharmony_ci u32_format_array(buf, size, data->array, data->n_elements); 109462306a36Sopenharmony_ci 109562306a36Sopenharmony_ci return nonseekable_open(inode, file); 109662306a36Sopenharmony_ci} 109762306a36Sopenharmony_ci 109862306a36Sopenharmony_cistatic ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 109962306a36Sopenharmony_ci loff_t *ppos) 110062306a36Sopenharmony_ci{ 110162306a36Sopenharmony_ci size_t size = strlen(file->private_data); 110262306a36Sopenharmony_ci 110362306a36Sopenharmony_ci return simple_read_from_buffer(buf, len, ppos, 110462306a36Sopenharmony_ci file->private_data, size); 110562306a36Sopenharmony_ci} 110662306a36Sopenharmony_ci 110762306a36Sopenharmony_cistatic int u32_array_release(struct inode *inode, struct file *file) 110862306a36Sopenharmony_ci{ 110962306a36Sopenharmony_ci kfree(file->private_data); 111062306a36Sopenharmony_ci 111162306a36Sopenharmony_ci return 0; 111262306a36Sopenharmony_ci} 111362306a36Sopenharmony_ci 111462306a36Sopenharmony_cistatic const struct file_operations u32_array_fops = { 111562306a36Sopenharmony_ci .owner = THIS_MODULE, 111662306a36Sopenharmony_ci .open = u32_array_open, 111762306a36Sopenharmony_ci .release = u32_array_release, 111862306a36Sopenharmony_ci .read = u32_array_read, 111962306a36Sopenharmony_ci .llseek = no_llseek, 112062306a36Sopenharmony_ci}; 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_ci/** 112362306a36Sopenharmony_ci * debugfs_create_u32_array - create a debugfs file that is used to read u32 112462306a36Sopenharmony_ci * array. 112562306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 112662306a36Sopenharmony_ci * @mode: the permission that the file should have. 112762306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 112862306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 112962306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 113062306a36Sopenharmony_ci * @array: wrapper struct containing data pointer and size of the array. 113162306a36Sopenharmony_ci * 113262306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that exports 113362306a36Sopenharmony_ci * @array as data. If the @mode variable is so set it can be read from. 113462306a36Sopenharmony_ci * Writing is not supported. Seek within the file is also not supported. 113562306a36Sopenharmony_ci * Once array is created its size can not be changed. 113662306a36Sopenharmony_ci */ 113762306a36Sopenharmony_civoid debugfs_create_u32_array(const char *name, umode_t mode, 113862306a36Sopenharmony_ci struct dentry *parent, 113962306a36Sopenharmony_ci struct debugfs_u32_array *array) 114062306a36Sopenharmony_ci{ 114162306a36Sopenharmony_ci debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 114262306a36Sopenharmony_ci} 114362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_u32_array); 114462306a36Sopenharmony_ci 114562306a36Sopenharmony_ci#ifdef CONFIG_HAS_IOMEM 114662306a36Sopenharmony_ci 114762306a36Sopenharmony_ci/* 114862306a36Sopenharmony_ci * The regset32 stuff is used to print 32-bit registers using the 114962306a36Sopenharmony_ci * seq_file utilities. We offer printing a register set in an already-opened 115062306a36Sopenharmony_ci * sequential file or create a debugfs file that only prints a regset32. 115162306a36Sopenharmony_ci */ 115262306a36Sopenharmony_ci 115362306a36Sopenharmony_ci/** 115462306a36Sopenharmony_ci * debugfs_print_regs32 - use seq_print to describe a set of registers 115562306a36Sopenharmony_ci * @s: the seq_file structure being used to generate output 115662306a36Sopenharmony_ci * @regs: an array if struct debugfs_reg32 structures 115762306a36Sopenharmony_ci * @nregs: the length of the above array 115862306a36Sopenharmony_ci * @base: the base address to be used in reading the registers 115962306a36Sopenharmony_ci * @prefix: a string to be prefixed to every output line 116062306a36Sopenharmony_ci * 116162306a36Sopenharmony_ci * This function outputs a text block describing the current values of 116262306a36Sopenharmony_ci * some 32-bit hardware registers. It is meant to be used within debugfs 116362306a36Sopenharmony_ci * files based on seq_file that need to show registers, intermixed with other 116462306a36Sopenharmony_ci * information. The prefix argument may be used to specify a leading string, 116562306a36Sopenharmony_ci * because some peripherals have several blocks of identical registers, 116662306a36Sopenharmony_ci * for example configuration of dma channels 116762306a36Sopenharmony_ci */ 116862306a36Sopenharmony_civoid debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 116962306a36Sopenharmony_ci int nregs, void __iomem *base, char *prefix) 117062306a36Sopenharmony_ci{ 117162306a36Sopenharmony_ci int i; 117262306a36Sopenharmony_ci 117362306a36Sopenharmony_ci for (i = 0; i < nregs; i++, regs++) { 117462306a36Sopenharmony_ci if (prefix) 117562306a36Sopenharmony_ci seq_printf(s, "%s", prefix); 117662306a36Sopenharmony_ci seq_printf(s, "%s = 0x%08x\n", regs->name, 117762306a36Sopenharmony_ci readl(base + regs->offset)); 117862306a36Sopenharmony_ci if (seq_has_overflowed(s)) 117962306a36Sopenharmony_ci break; 118062306a36Sopenharmony_ci } 118162306a36Sopenharmony_ci} 118262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_print_regs32); 118362306a36Sopenharmony_ci 118462306a36Sopenharmony_cistatic int debugfs_regset32_show(struct seq_file *s, void *data) 118562306a36Sopenharmony_ci{ 118662306a36Sopenharmony_ci struct debugfs_regset32 *regset = s->private; 118762306a36Sopenharmony_ci 118862306a36Sopenharmony_ci if (regset->dev) 118962306a36Sopenharmony_ci pm_runtime_get_sync(regset->dev); 119062306a36Sopenharmony_ci 119162306a36Sopenharmony_ci debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci if (regset->dev) 119462306a36Sopenharmony_ci pm_runtime_put(regset->dev); 119562306a36Sopenharmony_ci 119662306a36Sopenharmony_ci return 0; 119762306a36Sopenharmony_ci} 119862306a36Sopenharmony_ci 119962306a36Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(debugfs_regset32); 120062306a36Sopenharmony_ci 120162306a36Sopenharmony_ci/** 120262306a36Sopenharmony_ci * debugfs_create_regset32 - create a debugfs file that returns register values 120362306a36Sopenharmony_ci * @name: a pointer to a string containing the name of the file to create. 120462306a36Sopenharmony_ci * @mode: the permission that the file should have 120562306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 120662306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 120762306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 120862306a36Sopenharmony_ci * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 120962306a36Sopenharmony_ci * to an array of register definitions, the array size and the base 121062306a36Sopenharmony_ci * address where the register bank is to be found. 121162306a36Sopenharmony_ci * 121262306a36Sopenharmony_ci * This function creates a file in debugfs with the given name that reports 121362306a36Sopenharmony_ci * the names and values of a set of 32-bit registers. If the @mode variable 121462306a36Sopenharmony_ci * is so set it can be read from. Writing is not supported. 121562306a36Sopenharmony_ci */ 121662306a36Sopenharmony_civoid debugfs_create_regset32(const char *name, umode_t mode, 121762306a36Sopenharmony_ci struct dentry *parent, 121862306a36Sopenharmony_ci struct debugfs_regset32 *regset) 121962306a36Sopenharmony_ci{ 122062306a36Sopenharmony_ci debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); 122162306a36Sopenharmony_ci} 122262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_regset32); 122362306a36Sopenharmony_ci 122462306a36Sopenharmony_ci#endif /* CONFIG_HAS_IOMEM */ 122562306a36Sopenharmony_ci 122662306a36Sopenharmony_cistruct debugfs_devm_entry { 122762306a36Sopenharmony_ci int (*read)(struct seq_file *seq, void *data); 122862306a36Sopenharmony_ci struct device *dev; 122962306a36Sopenharmony_ci}; 123062306a36Sopenharmony_ci 123162306a36Sopenharmony_cistatic int debugfs_devm_entry_open(struct inode *inode, struct file *f) 123262306a36Sopenharmony_ci{ 123362306a36Sopenharmony_ci struct debugfs_devm_entry *entry = inode->i_private; 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci return single_open(f, entry->read, entry->dev); 123662306a36Sopenharmony_ci} 123762306a36Sopenharmony_ci 123862306a36Sopenharmony_cistatic const struct file_operations debugfs_devm_entry_ops = { 123962306a36Sopenharmony_ci .owner = THIS_MODULE, 124062306a36Sopenharmony_ci .open = debugfs_devm_entry_open, 124162306a36Sopenharmony_ci .release = single_release, 124262306a36Sopenharmony_ci .read = seq_read, 124362306a36Sopenharmony_ci .llseek = seq_lseek 124462306a36Sopenharmony_ci}; 124562306a36Sopenharmony_ci 124662306a36Sopenharmony_ci/** 124762306a36Sopenharmony_ci * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 124862306a36Sopenharmony_ci * 124962306a36Sopenharmony_ci * @dev: device related to this debugfs file. 125062306a36Sopenharmony_ci * @name: name of the debugfs file. 125162306a36Sopenharmony_ci * @parent: a pointer to the parent dentry for this file. This should be a 125262306a36Sopenharmony_ci * directory dentry if set. If this parameter is %NULL, then the 125362306a36Sopenharmony_ci * file will be created in the root of the debugfs filesystem. 125462306a36Sopenharmony_ci * @read_fn: function pointer called to print the seq_file content. 125562306a36Sopenharmony_ci */ 125662306a36Sopenharmony_civoid debugfs_create_devm_seqfile(struct device *dev, const char *name, 125762306a36Sopenharmony_ci struct dentry *parent, 125862306a36Sopenharmony_ci int (*read_fn)(struct seq_file *s, void *data)) 125962306a36Sopenharmony_ci{ 126062306a36Sopenharmony_ci struct debugfs_devm_entry *entry; 126162306a36Sopenharmony_ci 126262306a36Sopenharmony_ci if (IS_ERR(parent)) 126362306a36Sopenharmony_ci return; 126462306a36Sopenharmony_ci 126562306a36Sopenharmony_ci entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 126662306a36Sopenharmony_ci if (!entry) 126762306a36Sopenharmony_ci return; 126862306a36Sopenharmony_ci 126962306a36Sopenharmony_ci entry->read = read_fn; 127062306a36Sopenharmony_ci entry->dev = dev; 127162306a36Sopenharmony_ci 127262306a36Sopenharmony_ci debugfs_create_file(name, S_IRUGO, parent, entry, 127362306a36Sopenharmony_ci &debugfs_devm_entry_ops); 127462306a36Sopenharmony_ci} 127562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1276