Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file.c - part of debugfs, a tiny little debug file system |
| 3 | * |
| 4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> |
| 5 | * Copyright (C) 2004 IBM Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * debugfs is for people to use instead of /proc or /sys. |
Robert P. J. Day | 883ce42 | 2008-04-25 08:52:51 -0400 | [diff] [blame] | 12 | * See Documentation/DocBook/filesystems for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/fs.h> |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 18 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/debugfs.h> |
Alessandro Rubini | 03e099f | 2011-11-21 10:01:40 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 22 | #include <linux/slab.h> |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 23 | #include <linux/atomic.h> |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 24 | #include <linux/device.h> |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 25 | #include <linux/srcu.h> |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 26 | #include <asm/poll.h> |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 27 | |
| 28 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 30 | struct poll_table_struct; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static ssize_t default_read_file(struct file *file, char __user *buf, |
| 33 | size_t count, loff_t *ppos) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static ssize_t default_write_file(struct file *file, const char __user *buf, |
| 39 | size_t count, loff_t *ppos) |
| 40 | { |
| 41 | return count; |
| 42 | } |
| 43 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 44 | const struct file_operations debugfs_noop_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | .read = default_read_file, |
| 46 | .write = default_write_file, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 47 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 48 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 51 | /** |
| 52 | * debugfs_use_file_start - mark the beginning of file data access |
| 53 | * @dentry: the dentry object whose data is being accessed. |
| 54 | * @srcu_idx: a pointer to some memory to store a SRCU index in. |
| 55 | * |
| 56 | * Up to a matching call to debugfs_use_file_finish(), any |
| 57 | * successive call into the file removing functions debugfs_remove() |
| 58 | * and debugfs_remove_recursive() will block. Since associated private |
| 59 | * file data may only get freed after a successful return of any of |
| 60 | * the removal functions, you may safely access it after a successful |
| 61 | * call to debugfs_use_file_start() without worrying about |
| 62 | * lifetime issues. |
| 63 | * |
| 64 | * If -%EIO is returned, the file has already been removed and thus, |
| 65 | * it is not safe to access any of its data. If, on the other hand, |
| 66 | * it is allowed to access the file data, zero is returned. |
| 67 | * |
| 68 | * Regardless of the return code, any call to |
| 69 | * debugfs_use_file_start() must be followed by a matching call |
| 70 | * to debugfs_use_file_finish(). |
| 71 | */ |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 72 | int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx) |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 73 | __acquires(&debugfs_srcu) |
| 74 | { |
| 75 | *srcu_idx = srcu_read_lock(&debugfs_srcu); |
| 76 | barrier(); |
| 77 | if (d_unlinked(dentry)) |
| 78 | return -EIO; |
| 79 | return 0; |
| 80 | } |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 81 | EXPORT_SYMBOL_GPL(debugfs_use_file_start); |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * debugfs_use_file_finish - mark the end of file data access |
| 85 | * @srcu_idx: the SRCU index "created" by a former call to |
| 86 | * debugfs_use_file_start(). |
| 87 | * |
| 88 | * Allow any ongoing concurrent call into debugfs_remove() or |
| 89 | * debugfs_remove_recursive() blocked by a former call to |
| 90 | * debugfs_use_file_start() to proceed and return to its caller. |
| 91 | */ |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 92 | void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu) |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 93 | { |
| 94 | srcu_read_unlock(&debugfs_srcu, srcu_idx); |
| 95 | } |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(debugfs_use_file_finish); |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 97 | |
| 98 | #define F_DENTRY(filp) ((filp)->f_path.dentry) |
| 99 | |
| 100 | #define REAL_FOPS_DEREF(dentry) \ |
| 101 | ((const struct file_operations *)(dentry)->d_fsdata) |
| 102 | |
| 103 | static int open_proxy_open(struct inode *inode, struct file *filp) |
| 104 | { |
| 105 | const struct dentry *dentry = F_DENTRY(filp); |
| 106 | const struct file_operations *real_fops = NULL; |
| 107 | int srcu_idx, r; |
| 108 | |
| 109 | r = debugfs_use_file_start(dentry, &srcu_idx); |
| 110 | if (r) { |
| 111 | r = -ENOENT; |
| 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | real_fops = REAL_FOPS_DEREF(dentry); |
| 116 | real_fops = fops_get(real_fops); |
| 117 | if (!real_fops) { |
| 118 | /* Huh? Module did not clean up after itself at exit? */ |
| 119 | WARN(1, "debugfs file owner did not clean up at exit: %pd", |
| 120 | dentry); |
| 121 | r = -ENXIO; |
| 122 | goto out; |
| 123 | } |
| 124 | replace_fops(filp, real_fops); |
| 125 | |
| 126 | if (real_fops->open) |
| 127 | r = real_fops->open(inode, filp); |
| 128 | |
| 129 | out: |
| 130 | fops_put(real_fops); |
| 131 | debugfs_use_file_finish(srcu_idx); |
| 132 | return r; |
| 133 | } |
| 134 | |
| 135 | const struct file_operations debugfs_open_proxy_file_operations = { |
| 136 | .open = open_proxy_open, |
| 137 | }; |
| 138 | |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 139 | #define PROTO(args...) args |
| 140 | #define ARGS(args...) args |
| 141 | |
| 142 | #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ |
| 143 | static ret_type full_proxy_ ## name(proto) \ |
| 144 | { \ |
| 145 | const struct dentry *dentry = F_DENTRY(filp); \ |
| 146 | const struct file_operations *real_fops = \ |
| 147 | REAL_FOPS_DEREF(dentry); \ |
| 148 | int srcu_idx; \ |
| 149 | ret_type r; \ |
| 150 | \ |
| 151 | r = debugfs_use_file_start(dentry, &srcu_idx); \ |
| 152 | if (likely(!r)) \ |
| 153 | r = real_fops->name(args); \ |
| 154 | debugfs_use_file_finish(srcu_idx); \ |
| 155 | return r; \ |
| 156 | } |
| 157 | |
| 158 | FULL_PROXY_FUNC(llseek, loff_t, filp, |
| 159 | PROTO(struct file *filp, loff_t offset, int whence), |
| 160 | ARGS(filp, offset, whence)); |
| 161 | |
| 162 | FULL_PROXY_FUNC(read, ssize_t, filp, |
| 163 | PROTO(struct file *filp, char __user *buf, size_t size, |
| 164 | loff_t *ppos), |
| 165 | ARGS(filp, buf, size, ppos)); |
| 166 | |
| 167 | FULL_PROXY_FUNC(write, ssize_t, filp, |
| 168 | PROTO(struct file *filp, const char __user *buf, size_t size, |
| 169 | loff_t *ppos), |
| 170 | ARGS(filp, buf, size, ppos)); |
| 171 | |
| 172 | FULL_PROXY_FUNC(unlocked_ioctl, long, filp, |
| 173 | PROTO(struct file *filp, unsigned int cmd, unsigned long arg), |
| 174 | ARGS(filp, cmd, arg)); |
| 175 | |
| 176 | static unsigned int full_proxy_poll(struct file *filp, |
| 177 | struct poll_table_struct *wait) |
| 178 | { |
| 179 | const struct dentry *dentry = F_DENTRY(filp); |
| 180 | const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry); |
| 181 | int srcu_idx; |
| 182 | unsigned int r = 0; |
| 183 | |
| 184 | if (debugfs_use_file_start(dentry, &srcu_idx)) { |
| 185 | debugfs_use_file_finish(srcu_idx); |
| 186 | return POLLHUP; |
| 187 | } |
| 188 | |
| 189 | r = real_fops->poll(filp, wait); |
| 190 | debugfs_use_file_finish(srcu_idx); |
| 191 | return r; |
| 192 | } |
| 193 | |
| 194 | static int full_proxy_release(struct inode *inode, struct file *filp) |
| 195 | { |
| 196 | const struct dentry *dentry = F_DENTRY(filp); |
| 197 | const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry); |
| 198 | const struct file_operations *proxy_fops = filp->f_op; |
| 199 | int r = 0; |
| 200 | |
| 201 | /* |
| 202 | * We must not protect this against removal races here: the |
| 203 | * original releaser should be called unconditionally in order |
| 204 | * not to leak any resources. Releasers must not assume that |
| 205 | * ->i_private is still being meaningful here. |
| 206 | */ |
| 207 | if (real_fops->release) |
| 208 | r = real_fops->release(inode, filp); |
| 209 | |
| 210 | replace_fops(filp, d_inode(dentry)->i_fop); |
| 211 | kfree((void *)proxy_fops); |
| 212 | fops_put(real_fops); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void __full_proxy_fops_init(struct file_operations *proxy_fops, |
| 217 | const struct file_operations *real_fops) |
| 218 | { |
| 219 | proxy_fops->release = full_proxy_release; |
| 220 | if (real_fops->llseek) |
| 221 | proxy_fops->llseek = full_proxy_llseek; |
| 222 | if (real_fops->read) |
| 223 | proxy_fops->read = full_proxy_read; |
| 224 | if (real_fops->write) |
| 225 | proxy_fops->write = full_proxy_write; |
| 226 | if (real_fops->poll) |
| 227 | proxy_fops->poll = full_proxy_poll; |
| 228 | if (real_fops->unlocked_ioctl) |
| 229 | proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; |
| 230 | } |
| 231 | |
| 232 | static int full_proxy_open(struct inode *inode, struct file *filp) |
| 233 | { |
| 234 | const struct dentry *dentry = F_DENTRY(filp); |
| 235 | const struct file_operations *real_fops = NULL; |
| 236 | struct file_operations *proxy_fops = NULL; |
| 237 | int srcu_idx, r; |
| 238 | |
| 239 | r = debugfs_use_file_start(dentry, &srcu_idx); |
| 240 | if (r) { |
| 241 | r = -ENOENT; |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | real_fops = REAL_FOPS_DEREF(dentry); |
| 246 | real_fops = fops_get(real_fops); |
| 247 | if (!real_fops) { |
| 248 | /* Huh? Module did not cleanup after itself at exit? */ |
| 249 | WARN(1, "debugfs file owner did not clean up at exit: %pd", |
| 250 | dentry); |
| 251 | r = -ENXIO; |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); |
| 256 | if (!proxy_fops) { |
| 257 | r = -ENOMEM; |
| 258 | goto free_proxy; |
| 259 | } |
| 260 | __full_proxy_fops_init(proxy_fops, real_fops); |
| 261 | replace_fops(filp, proxy_fops); |
| 262 | |
| 263 | if (real_fops->open) { |
| 264 | r = real_fops->open(inode, filp); |
| 265 | |
| 266 | if (filp->f_op != proxy_fops) { |
| 267 | /* No protection against file removal anymore. */ |
| 268 | WARN(1, "debugfs file owner replaced proxy fops: %pd", |
| 269 | dentry); |
| 270 | goto free_proxy; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | goto out; |
| 275 | free_proxy: |
| 276 | kfree(proxy_fops); |
| 277 | fops_put(real_fops); |
| 278 | out: |
| 279 | debugfs_use_file_finish(srcu_idx); |
| 280 | return r; |
| 281 | } |
| 282 | |
| 283 | const struct file_operations debugfs_full_proxy_file_operations = { |
| 284 | .open = full_proxy_open, |
| 285 | }; |
| 286 | |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 287 | ssize_t debugfs_attr_read(struct file *file, char __user *buf, |
| 288 | size_t len, loff_t *ppos) |
| 289 | { |
| 290 | ssize_t ret; |
| 291 | int srcu_idx; |
| 292 | |
| 293 | ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); |
| 294 | if (likely(!ret)) |
| 295 | ret = simple_attr_read(file, buf, len, ppos); |
| 296 | debugfs_use_file_finish(srcu_idx); |
| 297 | return ret; |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(debugfs_attr_read); |
| 300 | |
| 301 | ssize_t debugfs_attr_write(struct file *file, const char __user *buf, |
| 302 | size_t len, loff_t *ppos) |
| 303 | { |
| 304 | ssize_t ret; |
| 305 | int srcu_idx; |
| 306 | |
| 307 | ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); |
| 308 | if (likely(!ret)) |
| 309 | ret = simple_attr_write(file, buf, len, ppos); |
| 310 | debugfs_use_file_finish(srcu_idx); |
| 311 | return ret; |
| 312 | } |
| 313 | EXPORT_SYMBOL_GPL(debugfs_attr_write); |
| 314 | |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 315 | static struct dentry *debugfs_create_mode(const char *name, umode_t mode, |
| 316 | struct dentry *parent, void *value, |
| 317 | const struct file_operations *fops, |
| 318 | const struct file_operations *fops_ro, |
| 319 | const struct file_operations *fops_wo) |
| 320 | { |
| 321 | /* if there are no write bits set, make read only */ |
| 322 | if (!(mode & S_IWUGO)) |
| 323 | return debugfs_create_file(name, mode, parent, value, fops_ro); |
| 324 | /* if there are no read bits set, make write only */ |
| 325 | if (!(mode & S_IRUGO)) |
| 326 | return debugfs_create_file(name, mode, parent, value, fops_wo); |
| 327 | |
| 328 | return debugfs_create_file(name, mode, parent, value, fops); |
| 329 | } |
| 330 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 331 | static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, |
| 332 | struct dentry *parent, void *value, |
| 333 | const struct file_operations *fops, |
| 334 | const struct file_operations *fops_ro, |
| 335 | const struct file_operations *fops_wo) |
| 336 | { |
| 337 | /* if there are no write bits set, make read only */ |
| 338 | if (!(mode & S_IWUGO)) |
| 339 | return debugfs_create_file_unsafe(name, mode, parent, value, |
| 340 | fops_ro); |
| 341 | /* if there are no read bits set, make write only */ |
| 342 | if (!(mode & S_IRUGO)) |
| 343 | return debugfs_create_file_unsafe(name, mode, parent, value, |
| 344 | fops_wo); |
| 345 | |
| 346 | return debugfs_create_file_unsafe(name, mode, parent, value, fops); |
| 347 | } |
| 348 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 349 | static int debugfs_u8_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 350 | { |
| 351 | *(u8 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 352 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 353 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 354 | static int debugfs_u8_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 355 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 356 | *val = *(u8 *)data; |
| 357 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 358 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 359 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
| 360 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); |
| 361 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 364 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * @name: a pointer to a string containing the name of the file to create. |
| 366 | * @mode: the permission that the file should have |
| 367 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 368 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * file will be created in the root of the debugfs filesystem. |
| 370 | * @value: a pointer to the variable that the file should read to and write |
| 371 | * from. |
| 372 | * |
| 373 | * This function creates a file in debugfs with the given name that |
| 374 | * contains the value of the variable @value. If the @mode variable is so |
| 375 | * set, it can be read from, and written to. |
| 376 | * |
| 377 | * This function will return a pointer to a dentry if it succeeds. This |
| 378 | * pointer must be passed to the debugfs_remove() function when the file is |
| 379 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 380 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 382 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 384 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * code. |
| 386 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 387 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct dentry *parent, u8 *value) |
| 389 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 390 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 391 | &fops_u8_ro, &fops_u8_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | EXPORT_SYMBOL_GPL(debugfs_create_u8); |
| 394 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 395 | static int debugfs_u16_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 396 | { |
| 397 | *(u16 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 398 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 399 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 400 | static int debugfs_u16_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 401 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 402 | *val = *(u16 *)data; |
| 403 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 404 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 405 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
| 406 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); |
| 407 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 410 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * @name: a pointer to a string containing the name of the file to create. |
| 412 | * @mode: the permission that the file should have |
| 413 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 414 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | * file will be created in the root of the debugfs filesystem. |
| 416 | * @value: a pointer to the variable that the file should read to and write |
| 417 | * from. |
| 418 | * |
| 419 | * This function creates a file in debugfs with the given name that |
| 420 | * contains the value of the variable @value. If the @mode variable is so |
| 421 | * set, it can be read from, and written to. |
| 422 | * |
| 423 | * This function will return a pointer to a dentry if it succeeds. This |
| 424 | * pointer must be passed to the debugfs_remove() function when the file is |
| 425 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 426 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 428 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 430 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * code. |
| 432 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 433 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | struct dentry *parent, u16 *value) |
| 435 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 436 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 437 | &fops_u16_ro, &fops_u16_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | EXPORT_SYMBOL_GPL(debugfs_create_u16); |
| 440 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 441 | static int debugfs_u32_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 442 | { |
| 443 | *(u32 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 444 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 445 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 446 | static int debugfs_u32_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 447 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 448 | *val = *(u32 *)data; |
| 449 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 450 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 451 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
| 452 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); |
| 453 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 456 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * @name: a pointer to a string containing the name of the file to create. |
| 458 | * @mode: the permission that the file should have |
| 459 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 460 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | * file will be created in the root of the debugfs filesystem. |
| 462 | * @value: a pointer to the variable that the file should read to and write |
| 463 | * from. |
| 464 | * |
| 465 | * This function creates a file in debugfs with the given name that |
| 466 | * contains the value of the variable @value. If the @mode variable is so |
| 467 | * set, it can be read from, and written to. |
| 468 | * |
| 469 | * This function will return a pointer to a dentry if it succeeds. This |
| 470 | * pointer must be passed to the debugfs_remove() function when the file is |
| 471 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 472 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 474 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 476 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | * code. |
| 478 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 479 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct dentry *parent, u32 *value) |
| 481 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 482 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 483 | &fops_u32_ro, &fops_u32_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | EXPORT_SYMBOL_GPL(debugfs_create_u32); |
| 486 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 487 | static int debugfs_u64_set(void *data, u64 val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 488 | { |
| 489 | *(u64 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 490 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 491 | } |
| 492 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 493 | static int debugfs_u64_get(void *data, u64 *val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 494 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 495 | *val = *(u64 *)data; |
| 496 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 497 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 498 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); |
| 499 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); |
| 500 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value |
| 504 | * @name: a pointer to a string containing the name of the file to create. |
| 505 | * @mode: the permission that the file should have |
| 506 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 507 | * directory dentry if set. If this parameter is %NULL, then the |
| 508 | * file will be created in the root of the debugfs filesystem. |
| 509 | * @value: a pointer to the variable that the file should read to and write |
| 510 | * from. |
| 511 | * |
| 512 | * This function creates a file in debugfs with the given name that |
| 513 | * contains the value of the variable @value. If the @mode variable is so |
| 514 | * set, it can be read from, and written to. |
| 515 | * |
| 516 | * This function will return a pointer to a dentry if it succeeds. This |
| 517 | * pointer must be passed to the debugfs_remove() function when the file is |
| 518 | * to be removed (no automatic cleanup happens if your module is unloaded, |
| 519 | * you are responsible here.) If an error occurs, %NULL will be returned. |
| 520 | * |
| 521 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
| 522 | * returned. It is not wise to check for this value, but rather, check for |
| 523 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
| 524 | * code. |
| 525 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 526 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 527 | struct dentry *parent, u64 *value) |
| 528 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 529 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 530 | &fops_u64_ro, &fops_u64_wo); |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 531 | } |
| 532 | EXPORT_SYMBOL_GPL(debugfs_create_u64); |
| 533 | |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 534 | static int debugfs_ulong_set(void *data, u64 val) |
| 535 | { |
| 536 | *(unsigned long *)data = val; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int debugfs_ulong_get(void *data, u64 *val) |
| 541 | { |
| 542 | *val = *(unsigned long *)data; |
| 543 | return 0; |
| 544 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 545 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, |
| 546 | "%llu\n"); |
| 547 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); |
| 548 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 549 | |
| 550 | /** |
| 551 | * debugfs_create_ulong - create a debugfs file that is used to read and write |
| 552 | * an unsigned long value. |
| 553 | * @name: a pointer to a string containing the name of the file to create. |
| 554 | * @mode: the permission that the file should have |
| 555 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 556 | * directory dentry if set. If this parameter is %NULL, then the |
| 557 | * file will be created in the root of the debugfs filesystem. |
| 558 | * @value: a pointer to the variable that the file should read to and write |
| 559 | * from. |
| 560 | * |
| 561 | * This function creates a file in debugfs with the given name that |
| 562 | * contains the value of the variable @value. If the @mode variable is so |
| 563 | * set, it can be read from, and written to. |
| 564 | * |
| 565 | * This function will return a pointer to a dentry if it succeeds. This |
| 566 | * pointer must be passed to the debugfs_remove() function when the file is |
| 567 | * to be removed (no automatic cleanup happens if your module is unloaded, |
| 568 | * you are responsible here.) If an error occurs, %NULL will be returned. |
| 569 | * |
| 570 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
| 571 | * returned. It is not wise to check for this value, but rather, check for |
| 572 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
| 573 | * code. |
| 574 | */ |
| 575 | struct dentry *debugfs_create_ulong(const char *name, umode_t mode, |
| 576 | struct dentry *parent, unsigned long *value) |
| 577 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 578 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
| 579 | &fops_ulong, &fops_ulong_ro, |
| 580 | &fops_ulong_wo); |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 581 | } |
| 582 | EXPORT_SYMBOL_GPL(debugfs_create_ulong); |
| 583 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 584 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
| 585 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); |
| 586 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 587 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 588 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, |
| 589 | "0x%04llx\n"); |
| 590 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); |
| 591 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 592 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 593 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, |
| 594 | "0x%08llx\n"); |
| 595 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); |
| 596 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 597 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 598 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, |
| 599 | "0x%016llx\n"); |
| 600 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); |
| 601 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 602 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 603 | /* |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 604 | * 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 |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 605 | * |
| 606 | * These functions are exactly the same as the above functions (but use a hex |
| 607 | * output for the decimal challenged). For details look at the above unsigned |
| 608 | * decimal functions. |
| 609 | */ |
| 610 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 611 | /** |
| 612 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 613 | * @name: a pointer to a string containing the name of the file to create. |
| 614 | * @mode: the permission that the file should have |
| 615 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 616 | * directory dentry if set. If this parameter is %NULL, then the |
| 617 | * file will be created in the root of the debugfs filesystem. |
| 618 | * @value: a pointer to the variable that the file should read to and write |
| 619 | * from. |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 620 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 621 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 622 | struct dentry *parent, u8 *value) |
| 623 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 624 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 625 | &fops_x8_ro, &fops_x8_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 626 | } |
| 627 | EXPORT_SYMBOL_GPL(debugfs_create_x8); |
| 628 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 629 | /** |
| 630 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
| 631 | * @name: a pointer to a string containing the name of the file to create. |
| 632 | * @mode: the permission that the file should have |
| 633 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 634 | * directory dentry if set. If this parameter is %NULL, then the |
| 635 | * file will be created in the root of the debugfs filesystem. |
| 636 | * @value: a pointer to the variable that the file should read to and write |
| 637 | * from. |
| 638 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 639 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 640 | struct dentry *parent, u16 *value) |
| 641 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 642 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 643 | &fops_x16_ro, &fops_x16_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 644 | } |
| 645 | EXPORT_SYMBOL_GPL(debugfs_create_x16); |
| 646 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 647 | /** |
| 648 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
| 649 | * @name: a pointer to a string containing the name of the file to create. |
| 650 | * @mode: the permission that the file should have |
| 651 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 652 | * directory dentry if set. If this parameter is %NULL, then the |
| 653 | * file will be created in the root of the debugfs filesystem. |
| 654 | * @value: a pointer to the variable that the file should read to and write |
| 655 | * from. |
| 656 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 657 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 658 | struct dentry *parent, u32 *value) |
| 659 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 660 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 661 | &fops_x32_ro, &fops_x32_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 662 | } |
| 663 | EXPORT_SYMBOL_GPL(debugfs_create_x32); |
| 664 | |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 665 | /** |
| 666 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value |
| 667 | * @name: a pointer to a string containing the name of the file to create. |
| 668 | * @mode: the permission that the file should have |
| 669 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 670 | * directory dentry if set. If this parameter is %NULL, then the |
| 671 | * file will be created in the root of the debugfs filesystem. |
| 672 | * @value: a pointer to the variable that the file should read to and write |
| 673 | * from. |
| 674 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 675 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 676 | struct dentry *parent, u64 *value) |
| 677 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 678 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, |
Stephen Boyd | 82b7d4f | 2015-10-12 18:09:10 -0700 | [diff] [blame] | 679 | &fops_x64_ro, &fops_x64_wo); |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 680 | } |
| 681 | EXPORT_SYMBOL_GPL(debugfs_create_x64); |
| 682 | |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 683 | |
| 684 | static int debugfs_size_t_set(void *data, u64 val) |
| 685 | { |
| 686 | *(size_t *)data = val; |
| 687 | return 0; |
| 688 | } |
| 689 | static int debugfs_size_t_get(void *data, u64 *val) |
| 690 | { |
| 691 | *val = *(size_t *)data; |
| 692 | return 0; |
| 693 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 694 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, |
| 695 | "%llu\n"); /* %llu and %zu are more or less the same */ |
| 696 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); |
| 697 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 698 | |
| 699 | /** |
| 700 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value |
| 701 | * @name: a pointer to a string containing the name of the file to create. |
| 702 | * @mode: the permission that the file should have |
| 703 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 704 | * directory dentry if set. If this parameter is %NULL, then the |
| 705 | * file will be created in the root of the debugfs filesystem. |
| 706 | * @value: a pointer to the variable that the file should read to and write |
| 707 | * from. |
| 708 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 709 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 710 | struct dentry *parent, size_t *value) |
| 711 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 712 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
| 713 | &fops_size_t, &fops_size_t_ro, |
| 714 | &fops_size_t_wo); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 715 | } |
| 716 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); |
| 717 | |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 718 | static int debugfs_atomic_t_set(void *data, u64 val) |
| 719 | { |
| 720 | atomic_set((atomic_t *)data, val); |
| 721 | return 0; |
| 722 | } |
| 723 | static int debugfs_atomic_t_get(void *data, u64 *val) |
| 724 | { |
| 725 | *val = atomic_read((atomic_t *)data); |
| 726 | return 0; |
| 727 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 728 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 729 | debugfs_atomic_t_set, "%lld\n"); |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 730 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, |
| 731 | "%lld\n"); |
| 732 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, |
| 733 | "%lld\n"); |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 734 | |
| 735 | /** |
| 736 | * debugfs_create_atomic_t - create a debugfs file that is used to read and |
| 737 | * write an atomic_t value |
| 738 | * @name: a pointer to a string containing the name of the file to create. |
| 739 | * @mode: the permission that the file should have |
| 740 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 741 | * directory dentry if set. If this parameter is %NULL, then the |
| 742 | * file will be created in the root of the debugfs filesystem. |
| 743 | * @value: a pointer to the variable that the file should read to and write |
| 744 | * from. |
| 745 | */ |
| 746 | struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, |
| 747 | struct dentry *parent, atomic_t *value) |
| 748 | { |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame^] | 749 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
| 750 | &fops_atomic_t, &fops_atomic_t_ro, |
| 751 | &fops_atomic_t_wo); |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 752 | } |
| 753 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 754 | |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 755 | ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, |
| 756 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
| 758 | char buf[3]; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 759 | bool *val = file->private_data; |
Rahul Bedarkar | 88e412e | 2014-06-06 23:12:04 +0530 | [diff] [blame] | 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if (*val) |
| 762 | buf[0] = 'Y'; |
| 763 | else |
| 764 | buf[0] = 'N'; |
| 765 | buf[1] = '\n'; |
| 766 | buf[2] = 0x00; |
| 767 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 768 | } |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 769 | EXPORT_SYMBOL_GPL(debugfs_read_file_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 771 | ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, |
| 772 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
| 774 | char buf[32]; |
Stephen Boyd | c42d223 | 2011-05-12 16:50:07 -0700 | [diff] [blame] | 775 | size_t buf_size; |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 776 | bool bv; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 777 | bool *val = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | buf_size = min(count, (sizeof(buf)-1)); |
| 780 | if (copy_from_user(buf, user_buf, buf_size)) |
| 781 | return -EFAULT; |
| 782 | |
Mathias Krause | a3b2c8c7 | 2013-05-31 23:24:29 +0200 | [diff] [blame] | 783 | buf[buf_size] = '\0'; |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 784 | if (strtobool(buf, &bv) == 0) |
| 785 | *val = bv; |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return count; |
| 788 | } |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 789 | EXPORT_SYMBOL_GPL(debugfs_write_file_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 791 | static const struct file_operations fops_bool = { |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 792 | .read = debugfs_read_file_bool, |
| 793 | .write = debugfs_write_file_bool, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 794 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 795 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | }; |
| 797 | |
Stephen Boyd | 6713e8f | 2015-10-12 18:09:12 -0700 | [diff] [blame] | 798 | static const struct file_operations fops_bool_ro = { |
| 799 | .read = debugfs_read_file_bool, |
| 800 | .open = simple_open, |
| 801 | .llseek = default_llseek, |
| 802 | }; |
| 803 | |
| 804 | static const struct file_operations fops_bool_wo = { |
| 805 | .write = debugfs_write_file_bool, |
| 806 | .open = simple_open, |
| 807 | .llseek = default_llseek, |
| 808 | }; |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 811 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | * @name: a pointer to a string containing the name of the file to create. |
| 813 | * @mode: the permission that the file should have |
| 814 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 815 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | * file will be created in the root of the debugfs filesystem. |
| 817 | * @value: a pointer to the variable that the file should read to and write |
| 818 | * from. |
| 819 | * |
| 820 | * This function creates a file in debugfs with the given name that |
| 821 | * contains the value of the variable @value. If the @mode variable is so |
| 822 | * set, it can be read from, and written to. |
| 823 | * |
| 824 | * This function will return a pointer to a dentry if it succeeds. This |
| 825 | * pointer must be passed to the debugfs_remove() function when the file is |
| 826 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 827 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 829 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 831 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | * code. |
| 833 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 834 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 835 | struct dentry *parent, bool *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
Stephen Boyd | 6713e8f | 2015-10-12 18:09:12 -0700 | [diff] [blame] | 837 | return debugfs_create_mode(name, mode, parent, value, &fops_bool, |
| 838 | &fops_bool_ro, &fops_bool_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | EXPORT_SYMBOL_GPL(debugfs_create_bool); |
| 841 | |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 842 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
| 843 | size_t count, loff_t *ppos) |
| 844 | { |
| 845 | struct debugfs_blob_wrapper *blob = file->private_data; |
| 846 | return simple_read_from_buffer(user_buf, count, ppos, blob->data, |
| 847 | blob->size); |
| 848 | } |
| 849 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 850 | static const struct file_operations fops_blob = { |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 851 | .read = read_file_blob, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 852 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 853 | .llseek = default_llseek, |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | /** |
Jonathan Corbet | 400ced6 | 2009-05-25 10:15:27 -0600 | [diff] [blame] | 857 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 858 | * @name: a pointer to a string containing the name of the file to create. |
| 859 | * @mode: the permission that the file should have |
| 860 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 861 | * directory dentry if set. If this parameter is %NULL, then the |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 862 | * file will be created in the root of the debugfs filesystem. |
| 863 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer |
| 864 | * to the blob data and the size of the data. |
| 865 | * |
| 866 | * This function creates a file in debugfs with the given name that exports |
| 867 | * @blob->data as a binary blob. If the @mode variable is so set it can be |
| 868 | * read from. Writing is not supported. |
| 869 | * |
| 870 | * This function will return a pointer to a dentry if it succeeds. This |
| 871 | * pointer must be passed to the debugfs_remove() function when the file is |
| 872 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 873 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 874 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 875 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 876 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 877 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 878 | * code. |
| 879 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 880 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 881 | struct dentry *parent, |
| 882 | struct debugfs_blob_wrapper *blob) |
| 883 | { |
| 884 | return debugfs_create_file(name, mode, parent, blob, &fops_blob); |
| 885 | } |
| 886 | EXPORT_SYMBOL_GPL(debugfs_create_blob); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 887 | |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 888 | struct array_data { |
| 889 | void *array; |
| 890 | u32 elements; |
| 891 | }; |
| 892 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 893 | static size_t u32_format_array(char *buf, size_t bufsize, |
| 894 | u32 *array, int array_size) |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 895 | { |
| 896 | size_t ret = 0; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 897 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 898 | while (--array_size >= 0) { |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 899 | size_t len; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 900 | char term = array_size ? ' ' : '\n'; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 901 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 902 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 903 | ret += len; |
| 904 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 905 | buf += len; |
| 906 | bufsize -= len; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 907 | } |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 908 | return ret; |
| 909 | } |
| 910 | |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 911 | static int u32_array_open(struct inode *inode, struct file *file) |
| 912 | { |
| 913 | struct array_data *data = inode->i_private; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 914 | int size, elements = data->elements; |
| 915 | char *buf; |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 916 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 917 | /* |
| 918 | * Max size: |
| 919 | * - 10 digits + ' '/'\n' = 11 bytes per number |
| 920 | * - terminating NUL character |
| 921 | */ |
| 922 | size = elements*11; |
| 923 | buf = kmalloc(size+1, GFP_KERNEL); |
| 924 | if (!buf) |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 925 | return -ENOMEM; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 926 | buf[size] = 0; |
| 927 | |
| 928 | file->private_data = buf; |
| 929 | u32_format_array(buf, size, data->array, data->elements); |
| 930 | |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 931 | return nonseekable_open(inode, file); |
| 932 | } |
| 933 | |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 934 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
| 935 | loff_t *ppos) |
| 936 | { |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 937 | size_t size = strlen(file->private_data); |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 938 | |
| 939 | return simple_read_from_buffer(buf, len, ppos, |
| 940 | file->private_data, size); |
| 941 | } |
| 942 | |
| 943 | static int u32_array_release(struct inode *inode, struct file *file) |
| 944 | { |
| 945 | kfree(file->private_data); |
| 946 | |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | static const struct file_operations u32_array_fops = { |
| 951 | .owner = THIS_MODULE, |
| 952 | .open = u32_array_open, |
| 953 | .release = u32_array_release, |
| 954 | .read = u32_array_read, |
| 955 | .llseek = no_llseek, |
| 956 | }; |
| 957 | |
| 958 | /** |
| 959 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 |
| 960 | * array. |
| 961 | * @name: a pointer to a string containing the name of the file to create. |
| 962 | * @mode: the permission that the file should have. |
| 963 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 964 | * directory dentry if set. If this parameter is %NULL, then the |
| 965 | * file will be created in the root of the debugfs filesystem. |
| 966 | * @array: u32 array that provides data. |
| 967 | * @elements: total number of elements in the array. |
| 968 | * |
| 969 | * This function creates a file in debugfs with the given name that exports |
| 970 | * @array as data. If the @mode variable is so set it can be read from. |
| 971 | * Writing is not supported. Seek within the file is also not supported. |
| 972 | * Once array is created its size can not be changed. |
| 973 | * |
| 974 | * The function returns a pointer to dentry on success. If debugfs is not |
| 975 | * enabled in the kernel, the value -%ENODEV will be returned. |
| 976 | */ |
| 977 | struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, |
| 978 | struct dentry *parent, |
| 979 | u32 *array, u32 elements) |
| 980 | { |
| 981 | struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 982 | |
| 983 | if (data == NULL) |
| 984 | return NULL; |
| 985 | |
| 986 | data->array = array; |
| 987 | data->elements = elements; |
| 988 | |
| 989 | return debugfs_create_file(name, mode, parent, data, &u32_array_fops); |
| 990 | } |
| 991 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); |
| 992 | |
Heiko Carstens | 3b85e4a | 2011-12-27 15:08:28 +0100 | [diff] [blame] | 993 | #ifdef CONFIG_HAS_IOMEM |
| 994 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 995 | /* |
| 996 | * The regset32 stuff is used to print 32-bit registers using the |
| 997 | * seq_file utilities. We offer printing a register set in an already-opened |
| 998 | * sequential file or create a debugfs file that only prints a regset32. |
| 999 | */ |
| 1000 | |
| 1001 | /** |
| 1002 | * debugfs_print_regs32 - use seq_print to describe a set of registers |
| 1003 | * @s: the seq_file structure being used to generate output |
| 1004 | * @regs: an array if struct debugfs_reg32 structures |
Randy Dunlap | b5763ac | 2012-01-21 11:02:42 -0800 | [diff] [blame] | 1005 | * @nregs: the length of the above array |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1006 | * @base: the base address to be used in reading the registers |
| 1007 | * @prefix: a string to be prefixed to every output line |
| 1008 | * |
| 1009 | * This function outputs a text block describing the current values of |
| 1010 | * some 32-bit hardware registers. It is meant to be used within debugfs |
| 1011 | * files based on seq_file that need to show registers, intermixed with other |
| 1012 | * information. The prefix argument may be used to specify a leading string, |
| 1013 | * because some peripherals have several blocks of identical registers, |
| 1014 | * for example configuration of dma channels |
| 1015 | */ |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1016 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
| 1017 | int nregs, void __iomem *base, char *prefix) |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1018 | { |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1019 | int i; |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1020 | |
| 1021 | for (i = 0; i < nregs; i++, regs++) { |
| 1022 | if (prefix) |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1023 | seq_printf(s, "%s", prefix); |
| 1024 | seq_printf(s, "%s = 0x%08x\n", regs->name, |
| 1025 | readl(base + regs->offset)); |
| 1026 | if (seq_has_overflowed(s)) |
| 1027 | break; |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1028 | } |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1029 | } |
| 1030 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); |
| 1031 | |
| 1032 | static int debugfs_show_regset32(struct seq_file *s, void *data) |
| 1033 | { |
| 1034 | struct debugfs_regset32 *regset = s->private; |
| 1035 | |
| 1036 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | static int debugfs_open_regset32(struct inode *inode, struct file *file) |
| 1041 | { |
| 1042 | return single_open(file, debugfs_show_regset32, inode->i_private); |
| 1043 | } |
| 1044 | |
| 1045 | static const struct file_operations fops_regset32 = { |
| 1046 | .open = debugfs_open_regset32, |
| 1047 | .read = seq_read, |
| 1048 | .llseek = seq_lseek, |
| 1049 | .release = single_release, |
| 1050 | }; |
| 1051 | |
| 1052 | /** |
| 1053 | * debugfs_create_regset32 - create a debugfs file that returns register values |
| 1054 | * @name: a pointer to a string containing the name of the file to create. |
| 1055 | * @mode: the permission that the file should have |
| 1056 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1057 | * directory dentry if set. If this parameter is %NULL, then the |
| 1058 | * file will be created in the root of the debugfs filesystem. |
| 1059 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer |
| 1060 | * to an array of register definitions, the array size and the base |
| 1061 | * address where the register bank is to be found. |
| 1062 | * |
| 1063 | * This function creates a file in debugfs with the given name that reports |
| 1064 | * the names and values of a set of 32-bit registers. If the @mode variable |
| 1065 | * is so set it can be read from. Writing is not supported. |
| 1066 | * |
| 1067 | * This function will return a pointer to a dentry if it succeeds. This |
| 1068 | * pointer must be passed to the debugfs_remove() function when the file is |
| 1069 | * to be removed (no automatic cleanup happens if your module is unloaded, |
| 1070 | * you are responsible here.) If an error occurs, %NULL will be returned. |
| 1071 | * |
| 1072 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
| 1073 | * returned. It is not wise to check for this value, but rather, check for |
| 1074 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
| 1075 | * code. |
| 1076 | */ |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 1077 | struct dentry *debugfs_create_regset32(const char *name, umode_t mode, |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1078 | struct dentry *parent, |
| 1079 | struct debugfs_regset32 *regset) |
| 1080 | { |
| 1081 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); |
| 1082 | } |
| 1083 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); |
Heiko Carstens | 3b85e4a | 2011-12-27 15:08:28 +0100 | [diff] [blame] | 1084 | |
| 1085 | #endif /* CONFIG_HAS_IOMEM */ |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1086 | |
| 1087 | struct debugfs_devm_entry { |
| 1088 | int (*read)(struct seq_file *seq, void *data); |
| 1089 | struct device *dev; |
| 1090 | }; |
| 1091 | |
| 1092 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) |
| 1093 | { |
| 1094 | struct debugfs_devm_entry *entry = inode->i_private; |
| 1095 | |
| 1096 | return single_open(f, entry->read, entry->dev); |
| 1097 | } |
| 1098 | |
| 1099 | static const struct file_operations debugfs_devm_entry_ops = { |
| 1100 | .owner = THIS_MODULE, |
| 1101 | .open = debugfs_devm_entry_open, |
| 1102 | .release = single_release, |
| 1103 | .read = seq_read, |
| 1104 | .llseek = seq_lseek |
| 1105 | }; |
| 1106 | |
| 1107 | /** |
| 1108 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. |
| 1109 | * |
| 1110 | * @dev: device related to this debugfs file. |
| 1111 | * @name: name of the debugfs file. |
| 1112 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1113 | * directory dentry if set. If this parameter is %NULL, then the |
| 1114 | * file will be created in the root of the debugfs filesystem. |
| 1115 | * @read_fn: function pointer called to print the seq_file content. |
| 1116 | */ |
| 1117 | struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, |
| 1118 | struct dentry *parent, |
| 1119 | int (*read_fn)(struct seq_file *s, |
| 1120 | void *data)) |
| 1121 | { |
| 1122 | struct debugfs_devm_entry *entry; |
| 1123 | |
| 1124 | if (IS_ERR(parent)) |
| 1125 | return ERR_PTR(-ENOENT); |
| 1126 | |
| 1127 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); |
| 1128 | if (!entry) |
| 1129 | return ERR_PTR(-ENOMEM); |
| 1130 | |
| 1131 | entry->read = read_fn; |
| 1132 | entry->dev = dev; |
| 1133 | |
| 1134 | return debugfs_create_file(name, S_IRUGO, parent, entry, |
| 1135 | &debugfs_devm_entry_ops); |
| 1136 | } |
| 1137 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); |
| 1138 | |