at master 44 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * file.c - part of debugfs, a tiny little debug file system 4 * 5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> 6 * Copyright (C) 2004 IBM Inc. 7 * 8 * debugfs is for people to use instead of /proc or /sys. 9 * See Documentation/filesystems/ for more details. 10 */ 11 12#include <linux/module.h> 13#include <linux/fs.h> 14#include <linux/seq_file.h> 15#include <linux/pagemap.h> 16#include <linux/debugfs.h> 17#include <linux/io.h> 18#include <linux/slab.h> 19#include <linux/atomic.h> 20#include <linux/device.h> 21#include <linux/pm_runtime.h> 22#include <linux/poll.h> 23#include <linux/security.h> 24 25#include "internal.h" 26 27struct poll_table_struct; 28 29static ssize_t default_read_file(struct file *file, char __user *buf, 30 size_t count, loff_t *ppos) 31{ 32 return 0; 33} 34 35static ssize_t default_write_file(struct file *file, const char __user *buf, 36 size_t count, loff_t *ppos) 37{ 38 return count; 39} 40 41const struct file_operations debugfs_noop_file_operations = { 42 .read = default_read_file, 43 .write = default_write_file, 44 .open = simple_open, 45 .llseek = noop_llseek, 46}; 47 48#define F_DENTRY(filp) ((filp)->f_path.dentry) 49 50void *debugfs_get_aux(const struct file *file) 51{ 52 return DEBUGFS_I(file_inode(file))->aux; 53} 54EXPORT_SYMBOL_GPL(debugfs_get_aux); 55 56enum dbgfs_get_mode { 57 DBGFS_GET_ALREADY, 58 DBGFS_GET_REGULAR, 59 DBGFS_GET_SHORT, 60}; 61 62static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode) 63{ 64 struct debugfs_fsdata *fsd; 65 void *d_fsd; 66 67 /* 68 * This could only happen if some debugfs user erroneously calls 69 * debugfs_file_get() on a dentry that isn't even a file, let 70 * them know about it. 71 */ 72 if (WARN_ON(!d_is_reg(dentry))) 73 return -EINVAL; 74 75 d_fsd = READ_ONCE(dentry->d_fsdata); 76 if (d_fsd) { 77 fsd = d_fsd; 78 } else { 79 struct inode *inode = dentry->d_inode; 80 unsigned int methods = 0; 81 82 if (WARN_ON(mode == DBGFS_GET_ALREADY)) 83 return -EINVAL; 84 85 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL); 86 if (!fsd) 87 return -ENOMEM; 88 89 if (mode == DBGFS_GET_SHORT) { 90 const struct debugfs_short_fops *ops; 91 ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops; 92 if (ops->llseek) 93 methods |= HAS_LSEEK; 94 if (ops->read) 95 methods |= HAS_READ; 96 if (ops->write) 97 methods |= HAS_WRITE; 98 fsd->real_fops = NULL; 99 } else { 100 const struct file_operations *ops; 101 ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops; 102 if (ops->llseek) 103 methods |= HAS_LSEEK; 104 if (ops->read) 105 methods |= HAS_READ; 106 if (ops->write) 107 methods |= HAS_WRITE; 108 if (ops->unlocked_ioctl) 109 methods |= HAS_IOCTL; 110 if (ops->poll) 111 methods |= HAS_POLL; 112 fsd->short_fops = NULL; 113 } 114 fsd->methods = methods; 115 refcount_set(&fsd->active_users, 1); 116 init_completion(&fsd->active_users_drained); 117 INIT_LIST_HEAD(&fsd->cancellations); 118 mutex_init(&fsd->cancellations_mtx); 119 120 d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd); 121 if (d_fsd) { 122 mutex_destroy(&fsd->cancellations_mtx); 123 kfree(fsd); 124 fsd = d_fsd; 125 } 126 } 127 128 /* 129 * In case of a successful cmpxchg() above, this check is 130 * strictly necessary and must follow it, see the comment in 131 * __debugfs_remove_file(). 132 * OTOH, if the cmpxchg() hasn't been executed or wasn't 133 * successful, this serves the purpose of not starving 134 * removers. 135 */ 136 if (d_unlinked(dentry)) 137 return -EIO; 138 139 if (!refcount_inc_not_zero(&fsd->active_users)) 140 return -EIO; 141 142 return 0; 143} 144 145/** 146 * debugfs_file_get - mark the beginning of file data access 147 * @dentry: the dentry object whose data is being accessed. 148 * 149 * Up to a matching call to debugfs_file_put(), any successive call 150 * into the file removing functions debugfs_remove() and 151 * debugfs_remove_recursive() will block. Since associated private 152 * file data may only get freed after a successful return of any of 153 * the removal functions, you may safely access it after a successful 154 * call to debugfs_file_get() without worrying about lifetime issues. 155 * 156 * If -%EIO is returned, the file has already been removed and thus, 157 * it is not safe to access any of its data. If, on the other hand, 158 * it is allowed to access the file data, zero is returned. 159 */ 160int debugfs_file_get(struct dentry *dentry) 161{ 162 return __debugfs_file_get(dentry, DBGFS_GET_ALREADY); 163} 164EXPORT_SYMBOL_GPL(debugfs_file_get); 165 166/** 167 * debugfs_file_put - mark the end of file data access 168 * @dentry: the dentry object formerly passed to 169 * debugfs_file_get(). 170 * 171 * Allow any ongoing concurrent call into debugfs_remove() or 172 * debugfs_remove_recursive() blocked by a former call to 173 * debugfs_file_get() to proceed and return to its caller. 174 */ 175void debugfs_file_put(struct dentry *dentry) 176{ 177 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); 178 179 if (refcount_dec_and_test(&fsd->active_users)) 180 complete(&fsd->active_users_drained); 181} 182EXPORT_SYMBOL_GPL(debugfs_file_put); 183 184/** 185 * debugfs_enter_cancellation - enter a debugfs cancellation 186 * @file: the file being accessed 187 * @cancellation: the cancellation object, the cancel callback 188 * inside of it must be initialized 189 * 190 * When a debugfs file is removed it needs to wait for all active 191 * operations to complete. However, the operation itself may need 192 * to wait for hardware or completion of some asynchronous process 193 * or similar. As such, it may need to be cancelled to avoid long 194 * waits or even deadlocks. 195 * 196 * This function can be used inside a debugfs handler that may 197 * need to be cancelled. As soon as this function is called, the 198 * cancellation's 'cancel' callback may be called, at which point 199 * the caller should proceed to call debugfs_leave_cancellation() 200 * and leave the debugfs handler function as soon as possible. 201 * Note that the 'cancel' callback is only ever called in the 202 * context of some kind of debugfs_remove(). 203 * 204 * This function must be paired with debugfs_leave_cancellation(). 205 */ 206void debugfs_enter_cancellation(struct file *file, 207 struct debugfs_cancellation *cancellation) 208{ 209 struct debugfs_fsdata *fsd; 210 struct dentry *dentry = F_DENTRY(file); 211 212 INIT_LIST_HEAD(&cancellation->list); 213 214 if (WARN_ON(!d_is_reg(dentry))) 215 return; 216 217 if (WARN_ON(!cancellation->cancel)) 218 return; 219 220 fsd = READ_ONCE(dentry->d_fsdata); 221 if (WARN_ON(!fsd)) 222 return; 223 224 mutex_lock(&fsd->cancellations_mtx); 225 list_add(&cancellation->list, &fsd->cancellations); 226 mutex_unlock(&fsd->cancellations_mtx); 227 228 /* if we're already removing wake it up to cancel */ 229 if (d_unlinked(dentry)) 230 complete(&fsd->active_users_drained); 231} 232EXPORT_SYMBOL_GPL(debugfs_enter_cancellation); 233 234/** 235 * debugfs_leave_cancellation - leave cancellation section 236 * @file: the file being accessed 237 * @cancellation: the cancellation previously registered with 238 * debugfs_enter_cancellation() 239 * 240 * See the documentation of debugfs_enter_cancellation(). 241 */ 242void debugfs_leave_cancellation(struct file *file, 243 struct debugfs_cancellation *cancellation) 244{ 245 struct debugfs_fsdata *fsd; 246 struct dentry *dentry = F_DENTRY(file); 247 248 if (WARN_ON(!d_is_reg(dentry))) 249 return; 250 251 fsd = READ_ONCE(dentry->d_fsdata); 252 if (WARN_ON(!fsd)) 253 return; 254 255 mutex_lock(&fsd->cancellations_mtx); 256 if (!list_empty(&cancellation->list)) 257 list_del(&cancellation->list); 258 mutex_unlock(&fsd->cancellations_mtx); 259} 260EXPORT_SYMBOL_GPL(debugfs_leave_cancellation); 261 262/* 263 * Only permit access to world-readable files when the kernel is locked down. 264 * We also need to exclude any file that has ways to write or alter it as root 265 * can bypass the permissions check. 266 */ 267static int debugfs_locked_down(struct inode *inode, 268 struct file *filp, 269 const struct file_operations *real_fops) 270{ 271 if ((inode->i_mode & 07777 & ~0444) == 0 && 272 !(filp->f_mode & FMODE_WRITE) && 273 (!real_fops || 274 (!real_fops->unlocked_ioctl && 275 !real_fops->compat_ioctl && 276 !real_fops->mmap))) 277 return 0; 278 279 if (security_locked_down(LOCKDOWN_DEBUGFS)) 280 return -EPERM; 281 282 return 0; 283} 284 285static int open_proxy_open(struct inode *inode, struct file *filp) 286{ 287 struct dentry *dentry = F_DENTRY(filp); 288 const struct file_operations *real_fops = DEBUGFS_I(inode)->real_fops; 289 int r; 290 291 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR); 292 if (r) 293 return r == -EIO ? -ENOENT : r; 294 295 r = debugfs_locked_down(inode, filp, real_fops); 296 if (r) 297 goto out; 298 299 if (!fops_get(real_fops)) { 300#ifdef CONFIG_MODULES 301 if (real_fops->owner && 302 real_fops->owner->state == MODULE_STATE_GOING) { 303 r = -ENXIO; 304 goto out; 305 } 306#endif 307 308 /* Huh? Module did not clean up after itself at exit? */ 309 WARN(1, "debugfs file owner did not clean up at exit: %pd", 310 dentry); 311 r = -ENXIO; 312 goto out; 313 } 314 replace_fops(filp, real_fops); 315 316 if (real_fops->open) 317 r = real_fops->open(inode, filp); 318 319out: 320 debugfs_file_put(dentry); 321 return r; 322} 323 324const struct file_operations debugfs_open_proxy_file_operations = { 325 .open = open_proxy_open, 326}; 327 328#define PROTO(args...) args 329#define ARGS(args...) args 330 331#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \ 332static ret_type full_proxy_ ## name(proto) \ 333{ \ 334 struct dentry *dentry = F_DENTRY(filp); \ 335 struct debugfs_fsdata *fsd = dentry->d_fsdata; \ 336 ret_type r; \ 337 \ 338 if (!(fsd->methods & bit)) \ 339 return ret; \ 340 r = debugfs_file_get(dentry); \ 341 if (unlikely(r)) \ 342 return r; \ 343 r = fsd->real_fops->name(args); \ 344 debugfs_file_put(dentry); \ 345 return r; \ 346} 347 348#define SHORT_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \ 349static ret_type short_proxy_ ## name(proto) \ 350{ \ 351 struct dentry *dentry = F_DENTRY(filp); \ 352 struct debugfs_fsdata *fsd = dentry->d_fsdata; \ 353 ret_type r; \ 354 \ 355 if (!(fsd->methods & bit)) \ 356 return ret; \ 357 r = debugfs_file_get(dentry); \ 358 if (unlikely(r)) \ 359 return r; \ 360 r = fsd->short_fops->name(args); \ 361 debugfs_file_put(dentry); \ 362 return r; \ 363} 364 365SHORT_PROXY_FUNC(llseek, loff_t, filp, 366 PROTO(struct file *filp, loff_t offset, int whence), 367 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); 368 369FULL_PROXY_FUNC(llseek, loff_t, filp, 370 PROTO(struct file *filp, loff_t offset, int whence), 371 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); 372 373SHORT_PROXY_FUNC(read, ssize_t, filp, 374 PROTO(struct file *filp, char __user *buf, size_t size, 375 loff_t *ppos), 376 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); 377 378FULL_PROXY_FUNC(read, ssize_t, filp, 379 PROTO(struct file *filp, char __user *buf, size_t size, 380 loff_t *ppos), 381 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); 382 383SHORT_PROXY_FUNC(write, ssize_t, filp, 384 PROTO(struct file *filp, const char __user *buf, 385 size_t size, loff_t *ppos), 386 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); 387 388FULL_PROXY_FUNC(write, ssize_t, filp, 389 PROTO(struct file *filp, const char __user *buf, 390 size_t size, loff_t *ppos), 391 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); 392 393FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 394 PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 395 ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY); 396 397static __poll_t full_proxy_poll(struct file *filp, 398 struct poll_table_struct *wait) 399{ 400 struct dentry *dentry = F_DENTRY(filp); 401 struct debugfs_fsdata *fsd = dentry->d_fsdata; 402 __poll_t r = 0; 403 404 if (!(fsd->methods & HAS_POLL)) 405 return DEFAULT_POLLMASK; 406 if (debugfs_file_get(dentry)) 407 return EPOLLHUP; 408 409 r = fsd->real_fops->poll(filp, wait); 410 debugfs_file_put(dentry); 411 return r; 412} 413 414static int full_proxy_release(struct inode *inode, struct file *file) 415{ 416 struct debugfs_fsdata *fsd = F_DENTRY(file)->d_fsdata; 417 const struct file_operations *real_fops = fsd->real_fops; 418 int r = 0; 419 420 /* 421 * We must not protect this against removal races here: the 422 * original releaser should be called unconditionally in order 423 * not to leak any resources. Releasers must not assume that 424 * ->i_private is still being meaningful here. 425 */ 426 if (real_fops->release) 427 r = real_fops->release(inode, file); 428 429 fops_put(real_fops); 430 return r; 431} 432 433static int full_proxy_open_regular(struct inode *inode, struct file *filp) 434{ 435 struct dentry *dentry = F_DENTRY(filp); 436 const struct file_operations *real_fops; 437 struct debugfs_fsdata *fsd; 438 int r; 439 440 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR); 441 if (r) 442 return r == -EIO ? -ENOENT : r; 443 444 fsd = dentry->d_fsdata; 445 real_fops = fsd->real_fops; 446 r = debugfs_locked_down(inode, filp, real_fops); 447 if (r) 448 goto out; 449 450 if (!fops_get(real_fops)) { 451#ifdef CONFIG_MODULES 452 if (real_fops->owner && 453 real_fops->owner->state == MODULE_STATE_GOING) { 454 r = -ENXIO; 455 goto out; 456 } 457#endif 458 459 /* Huh? Module did not cleanup after itself at exit? */ 460 WARN(1, "debugfs file owner did not clean up at exit: %pd", 461 dentry); 462 r = -ENXIO; 463 goto out; 464 } 465 466 if (real_fops->open) { 467 r = real_fops->open(inode, filp); 468 if (r) { 469 fops_put(real_fops); 470 } else if (filp->f_op != &debugfs_full_proxy_file_operations) { 471 /* No protection against file removal anymore. */ 472 WARN(1, "debugfs file owner replaced proxy fops: %pd", 473 dentry); 474 fops_put(real_fops); 475 } 476 } 477out: 478 debugfs_file_put(dentry); 479 return r; 480} 481 482const struct file_operations debugfs_full_proxy_file_operations = { 483 .open = full_proxy_open_regular, 484 .release = full_proxy_release, 485 .llseek = full_proxy_llseek, 486 .read = full_proxy_read, 487 .write = full_proxy_write, 488 .poll = full_proxy_poll, 489 .unlocked_ioctl = full_proxy_unlocked_ioctl 490}; 491 492static int full_proxy_open_short(struct inode *inode, struct file *filp) 493{ 494 struct dentry *dentry = F_DENTRY(filp); 495 int r; 496 497 r = __debugfs_file_get(dentry, DBGFS_GET_SHORT); 498 if (r) 499 return r == -EIO ? -ENOENT : r; 500 r = debugfs_locked_down(inode, filp, NULL); 501 if (!r) 502 r = simple_open(inode, filp); 503 debugfs_file_put(dentry); 504 return r; 505} 506 507const struct file_operations debugfs_full_short_proxy_file_operations = { 508 .open = full_proxy_open_short, 509 .llseek = short_proxy_llseek, 510 .read = short_proxy_read, 511 .write = short_proxy_write, 512}; 513 514ssize_t debugfs_attr_read(struct file *file, char __user *buf, 515 size_t len, loff_t *ppos) 516{ 517 struct dentry *dentry = F_DENTRY(file); 518 ssize_t ret; 519 520 ret = debugfs_file_get(dentry); 521 if (unlikely(ret)) 522 return ret; 523 ret = simple_attr_read(file, buf, len, ppos); 524 debugfs_file_put(dentry); 525 return ret; 526} 527EXPORT_SYMBOL_GPL(debugfs_attr_read); 528 529static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, 530 size_t len, loff_t *ppos, bool is_signed) 531{ 532 struct dentry *dentry = F_DENTRY(file); 533 ssize_t ret; 534 535 ret = debugfs_file_get(dentry); 536 if (unlikely(ret)) 537 return ret; 538 if (is_signed) 539 ret = simple_attr_write_signed(file, buf, len, ppos); 540 else 541 ret = simple_attr_write(file, buf, len, ppos); 542 debugfs_file_put(dentry); 543 return ret; 544} 545 546ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 547 size_t len, loff_t *ppos) 548{ 549 return debugfs_attr_write_xsigned(file, buf, len, ppos, false); 550} 551EXPORT_SYMBOL_GPL(debugfs_attr_write); 552 553ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 554 size_t len, loff_t *ppos) 555{ 556 return debugfs_attr_write_xsigned(file, buf, len, ppos, true); 557} 558EXPORT_SYMBOL_GPL(debugfs_attr_write_signed); 559 560static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 561 struct dentry *parent, void *value, 562 const struct file_operations *fops, 563 const struct file_operations *fops_ro, 564 const struct file_operations *fops_wo) 565{ 566 /* if there are no write bits set, make read only */ 567 if (!(mode & S_IWUGO)) 568 return debugfs_create_file_unsafe(name, mode, parent, value, 569 fops_ro); 570 /* if there are no read bits set, make write only */ 571 if (!(mode & S_IRUGO)) 572 return debugfs_create_file_unsafe(name, mode, parent, value, 573 fops_wo); 574 575 return debugfs_create_file_unsafe(name, mode, parent, value, fops); 576} 577 578static int debugfs_u8_set(void *data, u64 val) 579{ 580 *(u8 *)data = val; 581 return 0; 582} 583static int debugfs_u8_get(void *data, u64 *val) 584{ 585 *val = *(u8 *)data; 586 return 0; 587} 588DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 589DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 590DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 591 592/** 593 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 594 * @name: a pointer to a string containing the name of the file to create. 595 * @mode: the permission that the file should have 596 * @parent: a pointer to the parent dentry for this file. This should be a 597 * directory dentry if set. If this parameter is %NULL, then the 598 * file will be created in the root of the debugfs filesystem. 599 * @value: a pointer to the variable that the file should read to and write 600 * from. 601 * 602 * This function creates a file in debugfs with the given name that 603 * contains the value of the variable @value. If the @mode variable is so 604 * set, it can be read from, and written to. 605 */ 606void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 607 u8 *value) 608{ 609 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 610 &fops_u8_ro, &fops_u8_wo); 611} 612EXPORT_SYMBOL_GPL(debugfs_create_u8); 613 614static int debugfs_u16_set(void *data, u64 val) 615{ 616 *(u16 *)data = val; 617 return 0; 618} 619static int debugfs_u16_get(void *data, u64 *val) 620{ 621 *val = *(u16 *)data; 622 return 0; 623} 624DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 625DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 626DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 627 628/** 629 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 630 * @name: a pointer to a string containing the name of the file to create. 631 * @mode: the permission that the file should have 632 * @parent: a pointer to the parent dentry for this file. This should be a 633 * directory dentry if set. If this parameter is %NULL, then the 634 * file will be created in the root of the debugfs filesystem. 635 * @value: a pointer to the variable that the file should read to and write 636 * from. 637 * 638 * This function creates a file in debugfs with the given name that 639 * contains the value of the variable @value. If the @mode variable is so 640 * set, it can be read from, and written to. 641 */ 642void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 643 u16 *value) 644{ 645 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 646 &fops_u16_ro, &fops_u16_wo); 647} 648EXPORT_SYMBOL_GPL(debugfs_create_u16); 649 650static int debugfs_u32_set(void *data, u64 val) 651{ 652 *(u32 *)data = val; 653 return 0; 654} 655static int debugfs_u32_get(void *data, u64 *val) 656{ 657 *val = *(u32 *)data; 658 return 0; 659} 660DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 661DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 662DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 663 664/** 665 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 666 * @name: a pointer to a string containing the name of the file to create. 667 * @mode: the permission that the file should have 668 * @parent: a pointer to the parent dentry for this file. This should be a 669 * directory dentry if set. If this parameter is %NULL, then the 670 * file will be created in the root of the debugfs filesystem. 671 * @value: a pointer to the variable that the file should read to and write 672 * from. 673 * 674 * This function creates a file in debugfs with the given name that 675 * contains the value of the variable @value. If the @mode variable is so 676 * set, it can be read from, and written to. 677 */ 678void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 679 u32 *value) 680{ 681 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 682 &fops_u32_ro, &fops_u32_wo); 683} 684EXPORT_SYMBOL_GPL(debugfs_create_u32); 685 686static int debugfs_u64_set(void *data, u64 val) 687{ 688 *(u64 *)data = val; 689 return 0; 690} 691 692static int debugfs_u64_get(void *data, u64 *val) 693{ 694 *val = *(u64 *)data; 695 return 0; 696} 697DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 698DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 699DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 700 701/** 702 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 703 * @name: a pointer to a string containing the name of the file to create. 704 * @mode: the permission that the file should have 705 * @parent: a pointer to the parent dentry for this file. This should be a 706 * directory dentry if set. If this parameter is %NULL, then the 707 * file will be created in the root of the debugfs filesystem. 708 * @value: a pointer to the variable that the file should read to and write 709 * from. 710 * 711 * This function creates a file in debugfs with the given name that 712 * contains the value of the variable @value. If the @mode variable is so 713 * set, it can be read from, and written to. 714 */ 715void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 716 u64 *value) 717{ 718 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 719 &fops_u64_ro, &fops_u64_wo); 720} 721EXPORT_SYMBOL_GPL(debugfs_create_u64); 722 723static int debugfs_ulong_set(void *data, u64 val) 724{ 725 *(unsigned long *)data = val; 726 return 0; 727} 728 729static int debugfs_ulong_get(void *data, u64 *val) 730{ 731 *val = *(unsigned long *)data; 732 return 0; 733} 734DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 735 "%llu\n"); 736DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 737DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 738 739/** 740 * debugfs_create_ulong - create a debugfs file that is used to read and write 741 * an unsigned long value. 742 * @name: a pointer to a string containing the name of the file to create. 743 * @mode: the permission that the file should have 744 * @parent: a pointer to the parent dentry for this file. This should be a 745 * directory dentry if set. If this parameter is %NULL, then the 746 * file will be created in the root of the debugfs filesystem. 747 * @value: a pointer to the variable that the file should read to and write 748 * from. 749 * 750 * This function creates a file in debugfs with the given name that 751 * contains the value of the variable @value. If the @mode variable is so 752 * set, it can be read from, and written to. 753 */ 754void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 755 unsigned long *value) 756{ 757 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 758 &fops_ulong_ro, &fops_ulong_wo); 759} 760EXPORT_SYMBOL_GPL(debugfs_create_ulong); 761 762DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 763DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 764DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 765 766DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 767 "0x%04llx\n"); 768DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 769DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 770 771DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 772 "0x%08llx\n"); 773DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 774DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 775 776DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 777 "0x%016llx\n"); 778DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 779DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 780 781/* 782 * 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 783 * 784 * These functions are exactly the same as the above functions (but use a hex 785 * output for the decimal challenged). For details look at the above unsigned 786 * decimal functions. 787 */ 788 789/** 790 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 791 * @name: a pointer to a string containing the name of the file to create. 792 * @mode: the permission that the file should have 793 * @parent: a pointer to the parent dentry for this file. This should be a 794 * directory dentry if set. If this parameter is %NULL, then the 795 * file will be created in the root of the debugfs filesystem. 796 * @value: a pointer to the variable that the file should read to and write 797 * from. 798 */ 799void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 800 u8 *value) 801{ 802 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 803 &fops_x8_ro, &fops_x8_wo); 804} 805EXPORT_SYMBOL_GPL(debugfs_create_x8); 806 807/** 808 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 809 * @name: a pointer to a string containing the name of the file to create. 810 * @mode: the permission that the file should have 811 * @parent: a pointer to the parent dentry for this file. This should be a 812 * directory dentry if set. If this parameter is %NULL, then the 813 * file will be created in the root of the debugfs filesystem. 814 * @value: a pointer to the variable that the file should read to and write 815 * from. 816 */ 817void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 818 u16 *value) 819{ 820 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 821 &fops_x16_ro, &fops_x16_wo); 822} 823EXPORT_SYMBOL_GPL(debugfs_create_x16); 824 825/** 826 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 827 * @name: a pointer to a string containing the name of the file to create. 828 * @mode: the permission that the file should have 829 * @parent: a pointer to the parent dentry for this file. This should be a 830 * directory dentry if set. If this parameter is %NULL, then the 831 * file will be created in the root of the debugfs filesystem. 832 * @value: a pointer to the variable that the file should read to and write 833 * from. 834 */ 835void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 836 u32 *value) 837{ 838 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 839 &fops_x32_ro, &fops_x32_wo); 840} 841EXPORT_SYMBOL_GPL(debugfs_create_x32); 842 843/** 844 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 845 * @name: a pointer to a string containing the name of the file to create. 846 * @mode: the permission that the file should have 847 * @parent: a pointer to the parent dentry for this file. This should be a 848 * directory dentry if set. If this parameter is %NULL, then the 849 * file will be created in the root of the debugfs filesystem. 850 * @value: a pointer to the variable that the file should read to and write 851 * from. 852 */ 853void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 854 u64 *value) 855{ 856 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 857 &fops_x64_ro, &fops_x64_wo); 858} 859EXPORT_SYMBOL_GPL(debugfs_create_x64); 860 861 862static int debugfs_size_t_set(void *data, u64 val) 863{ 864 *(size_t *)data = val; 865 return 0; 866} 867static int debugfs_size_t_get(void *data, u64 *val) 868{ 869 *val = *(size_t *)data; 870 return 0; 871} 872DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 873 "%llu\n"); /* %llu and %zu are more or less the same */ 874DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 875DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 876 877/** 878 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 879 * @name: a pointer to a string containing the name of the file to create. 880 * @mode: the permission that the file should have 881 * @parent: a pointer to the parent dentry for this file. This should be a 882 * directory dentry if set. If this parameter is %NULL, then the 883 * file will be created in the root of the debugfs filesystem. 884 * @value: a pointer to the variable that the file should read to and write 885 * from. 886 */ 887void debugfs_create_size_t(const char *name, umode_t mode, 888 struct dentry *parent, size_t *value) 889{ 890 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 891 &fops_size_t_ro, &fops_size_t_wo); 892} 893EXPORT_SYMBOL_GPL(debugfs_create_size_t); 894 895static int debugfs_atomic_t_set(void *data, u64 val) 896{ 897 atomic_set((atomic_t *)data, val); 898 return 0; 899} 900static int debugfs_atomic_t_get(void *data, u64 *val) 901{ 902 *val = atomic_read((atomic_t *)data); 903 return 0; 904} 905DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, 906 debugfs_atomic_t_set, "%lld\n"); 907DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 908 "%lld\n"); 909DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 910 "%lld\n"); 911 912/** 913 * debugfs_create_atomic_t - create a debugfs file that is used to read and 914 * write an atomic_t value 915 * @name: a pointer to a string containing the name of the file to create. 916 * @mode: the permission that the file should have 917 * @parent: a pointer to the parent dentry for this file. This should be a 918 * directory dentry if set. If this parameter is %NULL, then the 919 * file will be created in the root of the debugfs filesystem. 920 * @value: a pointer to the variable that the file should read to and write 921 * from. 922 */ 923void debugfs_create_atomic_t(const char *name, umode_t mode, 924 struct dentry *parent, atomic_t *value) 925{ 926 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 927 &fops_atomic_t_ro, &fops_atomic_t_wo); 928} 929EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 930 931ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 932 size_t count, loff_t *ppos) 933{ 934 char buf[2]; 935 bool val; 936 int r; 937 struct dentry *dentry = F_DENTRY(file); 938 939 r = debugfs_file_get(dentry); 940 if (unlikely(r)) 941 return r; 942 val = *(bool *)file->private_data; 943 debugfs_file_put(dentry); 944 945 if (val) 946 buf[0] = 'Y'; 947 else 948 buf[0] = 'N'; 949 buf[1] = '\n'; 950 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 951} 952EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 953 954ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 955 size_t count, loff_t *ppos) 956{ 957 bool bv; 958 int r; 959 bool *val = file->private_data; 960 struct dentry *dentry = F_DENTRY(file); 961 962 r = kstrtobool_from_user(user_buf, count, &bv); 963 if (!r) { 964 r = debugfs_file_get(dentry); 965 if (unlikely(r)) 966 return r; 967 *val = bv; 968 debugfs_file_put(dentry); 969 } 970 971 return count; 972} 973EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 974 975static const struct file_operations fops_bool = { 976 .read = debugfs_read_file_bool, 977 .write = debugfs_write_file_bool, 978 .open = simple_open, 979 .llseek = default_llseek, 980}; 981 982static const struct file_operations fops_bool_ro = { 983 .read = debugfs_read_file_bool, 984 .open = simple_open, 985 .llseek = default_llseek, 986}; 987 988static const struct file_operations fops_bool_wo = { 989 .write = debugfs_write_file_bool, 990 .open = simple_open, 991 .llseek = default_llseek, 992}; 993 994/** 995 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 996 * @name: a pointer to a string containing the name of the file to create. 997 * @mode: the permission that the file should have 998 * @parent: a pointer to the parent dentry for this file. This should be a 999 * directory dentry if set. If this parameter is %NULL, then the 1000 * file will be created in the root of the debugfs filesystem. 1001 * @value: a pointer to the variable that the file should read to and write 1002 * from. 1003 * 1004 * This function creates a file in debugfs with the given name that 1005 * contains the value of the variable @value. If the @mode variable is so 1006 * set, it can be read from, and written to. 1007 */ 1008void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 1009 bool *value) 1010{ 1011 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 1012 &fops_bool_ro, &fops_bool_wo); 1013} 1014EXPORT_SYMBOL_GPL(debugfs_create_bool); 1015 1016ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 1017 size_t count, loff_t *ppos) 1018{ 1019 struct dentry *dentry = F_DENTRY(file); 1020 char *str, *copy = NULL; 1021 int copy_len, len; 1022 ssize_t ret; 1023 1024 ret = debugfs_file_get(dentry); 1025 if (unlikely(ret)) 1026 return ret; 1027 1028 str = *(char **)file->private_data; 1029 len = strlen(str) + 1; 1030 copy = kmalloc(len, GFP_KERNEL); 1031 if (!copy) { 1032 debugfs_file_put(dentry); 1033 return -ENOMEM; 1034 } 1035 1036 copy_len = strscpy(copy, str, len); 1037 debugfs_file_put(dentry); 1038 if (copy_len < 0) { 1039 kfree(copy); 1040 return copy_len; 1041 } 1042 1043 copy[copy_len] = '\n'; 1044 1045 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); 1046 kfree(copy); 1047 1048 return ret; 1049} 1050EXPORT_SYMBOL_GPL(debugfs_create_str); 1051 1052static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, 1053 size_t count, loff_t *ppos) 1054{ 1055 struct dentry *dentry = F_DENTRY(file); 1056 char *old, *new = NULL; 1057 int pos = *ppos; 1058 int r; 1059 1060 r = debugfs_file_get(dentry); 1061 if (unlikely(r)) 1062 return r; 1063 1064 old = *(char **)file->private_data; 1065 1066 /* only allow strict concatenation */ 1067 r = -EINVAL; 1068 if (pos && pos != strlen(old)) 1069 goto error; 1070 1071 r = -E2BIG; 1072 if (pos + count + 1 > PAGE_SIZE) 1073 goto error; 1074 1075 r = -ENOMEM; 1076 new = kmalloc(pos + count + 1, GFP_KERNEL); 1077 if (!new) 1078 goto error; 1079 1080 if (pos) 1081 memcpy(new, old, pos); 1082 1083 r = -EFAULT; 1084 if (copy_from_user(new + pos, user_buf, count)) 1085 goto error; 1086 1087 new[pos + count] = '\0'; 1088 strim(new); 1089 1090 rcu_assign_pointer(*(char __rcu **)file->private_data, new); 1091 synchronize_rcu(); 1092 kfree(old); 1093 1094 debugfs_file_put(dentry); 1095 return count; 1096 1097error: 1098 kfree(new); 1099 debugfs_file_put(dentry); 1100 return r; 1101} 1102 1103static const struct file_operations fops_str = { 1104 .read = debugfs_read_file_str, 1105 .write = debugfs_write_file_str, 1106 .open = simple_open, 1107 .llseek = default_llseek, 1108}; 1109 1110static const struct file_operations fops_str_ro = { 1111 .read = debugfs_read_file_str, 1112 .open = simple_open, 1113 .llseek = default_llseek, 1114}; 1115 1116static const struct file_operations fops_str_wo = { 1117 .write = debugfs_write_file_str, 1118 .open = simple_open, 1119 .llseek = default_llseek, 1120}; 1121 1122/** 1123 * debugfs_create_str - create a debugfs file that is used to read and write a string value 1124 * @name: a pointer to a string containing the name of the file to create. 1125 * @mode: the permission that the file should have 1126 * @parent: a pointer to the parent dentry for this file. This should be a 1127 * directory dentry if set. If this parameter is %NULL, then the 1128 * file will be created in the root of the debugfs filesystem. 1129 * @value: a pointer to the variable that the file should read to and write 1130 * from. 1131 * 1132 * This function creates a file in debugfs with the given name that 1133 * contains the value of the variable @value. If the @mode variable is so 1134 * set, it can be read from, and written to. 1135 */ 1136void debugfs_create_str(const char *name, umode_t mode, 1137 struct dentry *parent, char **value) 1138{ 1139 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 1140 &fops_str_ro, &fops_str_wo); 1141} 1142 1143static ssize_t read_file_blob(struct file *file, char __user *user_buf, 1144 size_t count, loff_t *ppos) 1145{ 1146 struct debugfs_blob_wrapper *blob = file->private_data; 1147 struct dentry *dentry = F_DENTRY(file); 1148 ssize_t r; 1149 1150 r = debugfs_file_get(dentry); 1151 if (unlikely(r)) 1152 return r; 1153 r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 1154 blob->size); 1155 debugfs_file_put(dentry); 1156 return r; 1157} 1158 1159static ssize_t write_file_blob(struct file *file, const char __user *user_buf, 1160 size_t count, loff_t *ppos) 1161{ 1162 struct debugfs_blob_wrapper *blob = file->private_data; 1163 struct dentry *dentry = F_DENTRY(file); 1164 ssize_t r; 1165 1166 r = debugfs_file_get(dentry); 1167 if (unlikely(r)) 1168 return r; 1169 r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf, 1170 count); 1171 1172 debugfs_file_put(dentry); 1173 return r; 1174} 1175 1176static const struct file_operations fops_blob = { 1177 .read = read_file_blob, 1178 .write = write_file_blob, 1179 .open = simple_open, 1180 .llseek = default_llseek, 1181}; 1182 1183/** 1184 * debugfs_create_blob - create a debugfs file that is used to read and write 1185 * a binary blob 1186 * @name: a pointer to a string containing the name of the file to create. 1187 * @mode: the permission that the file should have 1188 * @parent: a pointer to the parent dentry for this file. This should be a 1189 * directory dentry if set. If this parameter is %NULL, then the 1190 * file will be created in the root of the debugfs filesystem. 1191 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 1192 * to the blob data and the size of the data. 1193 * 1194 * This function creates a file in debugfs with the given name that exports 1195 * @blob->data as a binary blob. If the @mode variable is so set it can be 1196 * read from and written to. 1197 * 1198 * This function will return a pointer to a dentry if it succeeds. This 1199 * pointer must be passed to the debugfs_remove() function when the file is 1200 * to be removed (no automatic cleanup happens if your module is unloaded, 1201 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 1202 * returned. 1203 * 1204 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 1205 * be returned. 1206 */ 1207struct dentry *debugfs_create_blob(const char *name, umode_t mode, 1208 struct dentry *parent, 1209 struct debugfs_blob_wrapper *blob) 1210{ 1211 return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob); 1212} 1213EXPORT_SYMBOL_GPL(debugfs_create_blob); 1214 1215static size_t u32_format_array(char *buf, size_t bufsize, 1216 u32 *array, int array_size) 1217{ 1218 size_t ret = 0; 1219 1220 while (--array_size >= 0) { 1221 size_t len; 1222 char term = array_size ? ' ' : '\n'; 1223 1224 len = snprintf(buf, bufsize, "%u%c", *array++, term); 1225 ret += len; 1226 1227 buf += len; 1228 bufsize -= len; 1229 } 1230 return ret; 1231} 1232 1233static int u32_array_open(struct inode *inode, struct file *file) 1234{ 1235 struct debugfs_u32_array *data = inode->i_private; 1236 int size, elements = data->n_elements; 1237 char *buf; 1238 1239 /* 1240 * Max size: 1241 * - 10 digits + ' '/'\n' = 11 bytes per number 1242 * - terminating NUL character 1243 */ 1244 size = elements*11; 1245 buf = kmalloc(size+1, GFP_KERNEL); 1246 if (!buf) 1247 return -ENOMEM; 1248 buf[size] = 0; 1249 1250 file->private_data = buf; 1251 u32_format_array(buf, size, data->array, data->n_elements); 1252 1253 return nonseekable_open(inode, file); 1254} 1255 1256static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 1257 loff_t *ppos) 1258{ 1259 size_t size = strlen(file->private_data); 1260 1261 return simple_read_from_buffer(buf, len, ppos, 1262 file->private_data, size); 1263} 1264 1265static int u32_array_release(struct inode *inode, struct file *file) 1266{ 1267 kfree(file->private_data); 1268 1269 return 0; 1270} 1271 1272static const struct file_operations u32_array_fops = { 1273 .owner = THIS_MODULE, 1274 .open = u32_array_open, 1275 .release = u32_array_release, 1276 .read = u32_array_read, 1277}; 1278 1279/** 1280 * debugfs_create_u32_array - create a debugfs file that is used to read u32 1281 * array. 1282 * @name: a pointer to a string containing the name of the file to create. 1283 * @mode: the permission that the file should have. 1284 * @parent: a pointer to the parent dentry for this file. This should be a 1285 * directory dentry if set. If this parameter is %NULL, then the 1286 * file will be created in the root of the debugfs filesystem. 1287 * @array: wrapper struct containing data pointer and size of the array. 1288 * 1289 * This function creates a file in debugfs with the given name that exports 1290 * @array as data. If the @mode variable is so set it can be read from. 1291 * Writing is not supported. Seek within the file is also not supported. 1292 * Once array is created its size can not be changed. 1293 */ 1294void debugfs_create_u32_array(const char *name, umode_t mode, 1295 struct dentry *parent, 1296 struct debugfs_u32_array *array) 1297{ 1298 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 1299} 1300EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 1301 1302#ifdef CONFIG_HAS_IOMEM 1303 1304/* 1305 * The regset32 stuff is used to print 32-bit registers using the 1306 * seq_file utilities. We offer printing a register set in an already-opened 1307 * sequential file or create a debugfs file that only prints a regset32. 1308 */ 1309 1310/** 1311 * debugfs_print_regs32 - use seq_print to describe a set of registers 1312 * @s: the seq_file structure being used to generate output 1313 * @regs: an array if struct debugfs_reg32 structures 1314 * @nregs: the length of the above array 1315 * @base: the base address to be used in reading the registers 1316 * @prefix: a string to be prefixed to every output line 1317 * 1318 * This function outputs a text block describing the current values of 1319 * some 32-bit hardware registers. It is meant to be used within debugfs 1320 * files based on seq_file that need to show registers, intermixed with other 1321 * information. The prefix argument may be used to specify a leading string, 1322 * because some peripherals have several blocks of identical registers, 1323 * for example configuration of dma channels 1324 */ 1325void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 1326 int nregs, void __iomem *base, char *prefix) 1327{ 1328 int i; 1329 1330 for (i = 0; i < nregs; i++, regs++) { 1331 if (prefix) 1332 seq_printf(s, "%s", prefix); 1333 seq_printf(s, "%s = 0x%08x\n", regs->name, 1334 readl(base + regs->offset)); 1335 if (seq_has_overflowed(s)) 1336 break; 1337 } 1338} 1339EXPORT_SYMBOL_GPL(debugfs_print_regs32); 1340 1341static int debugfs_regset32_show(struct seq_file *s, void *data) 1342{ 1343 struct debugfs_regset32 *regset = s->private; 1344 1345 if (regset->dev) 1346 pm_runtime_get_sync(regset->dev); 1347 1348 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 1349 1350 if (regset->dev) 1351 pm_runtime_put(regset->dev); 1352 1353 return 0; 1354} 1355 1356DEFINE_SHOW_ATTRIBUTE(debugfs_regset32); 1357 1358/** 1359 * debugfs_create_regset32 - create a debugfs file that returns register values 1360 * @name: a pointer to a string containing the name of the file to create. 1361 * @mode: the permission that the file should have 1362 * @parent: a pointer to the parent dentry for this file. This should be a 1363 * directory dentry if set. If this parameter is %NULL, then the 1364 * file will be created in the root of the debugfs filesystem. 1365 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 1366 * to an array of register definitions, the array size and the base 1367 * address where the register bank is to be found. 1368 * 1369 * This function creates a file in debugfs with the given name that reports 1370 * the names and values of a set of 32-bit registers. If the @mode variable 1371 * is so set it can be read from. Writing is not supported. 1372 */ 1373void debugfs_create_regset32(const char *name, umode_t mode, 1374 struct dentry *parent, 1375 struct debugfs_regset32 *regset) 1376{ 1377 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); 1378} 1379EXPORT_SYMBOL_GPL(debugfs_create_regset32); 1380 1381#endif /* CONFIG_HAS_IOMEM */ 1382 1383struct debugfs_devm_entry { 1384 int (*read)(struct seq_file *seq, void *data); 1385 struct device *dev; 1386}; 1387 1388static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 1389{ 1390 struct debugfs_devm_entry *entry = inode->i_private; 1391 1392 return single_open(f, entry->read, entry->dev); 1393} 1394 1395static const struct file_operations debugfs_devm_entry_ops = { 1396 .owner = THIS_MODULE, 1397 .open = debugfs_devm_entry_open, 1398 .release = single_release, 1399 .read = seq_read, 1400 .llseek = seq_lseek 1401}; 1402 1403/** 1404 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 1405 * 1406 * @dev: device related to this debugfs file. 1407 * @name: name of the debugfs file. 1408 * @parent: a pointer to the parent dentry for this file. This should be a 1409 * directory dentry if set. If this parameter is %NULL, then the 1410 * file will be created in the root of the debugfs filesystem. 1411 * @read_fn: function pointer called to print the seq_file content. 1412 */ 1413void debugfs_create_devm_seqfile(struct device *dev, const char *name, 1414 struct dentry *parent, 1415 int (*read_fn)(struct seq_file *s, void *data)) 1416{ 1417 struct debugfs_devm_entry *entry; 1418 1419 if (IS_ERR(parent)) 1420 return; 1421 1422 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 1423 if (!entry) 1424 return; 1425 1426 entry->read = read_fn; 1427 entry->dev = dev; 1428 1429 debugfs_create_file(name, S_IRUGO, parent, entry, 1430 &debugfs_devm_entry_ops); 1431} 1432EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);