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 27 struct poll_table_struct; 28 29 static ssize_t default_read_file(struct file *file, char __user *buf, 30 size_t count, loff_t *ppos) 31 { 32 return 0; 33 } 34 35 static 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 41 const 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 50 void *debugfs_get_aux(const struct file *file) 51 { 52 return DEBUGFS_I(file_inode(file))->aux; 53 } 54 EXPORT_SYMBOL_GPL(debugfs_get_aux); 55 56 enum dbgfs_get_mode { 57 DBGFS_GET_ALREADY, 58 DBGFS_GET_REGULAR, 59 DBGFS_GET_SHORT, 60 }; 61 62 static 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_obj(*fsd); 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 */ 160 int debugfs_file_get(struct dentry *dentry) 161 { 162 return __debugfs_file_get(dentry, DBGFS_GET_ALREADY); 163 } 164 EXPORT_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 */ 175 void 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 } 182 EXPORT_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 */ 206 void 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 } 232 EXPORT_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 */ 242 void 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 } 260 EXPORT_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 */ 267 static 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 !real_fops->mmap_prepare))) 278 return 0; 279 280 if (security_locked_down(LOCKDOWN_DEBUGFS)) 281 return -EPERM; 282 283 return 0; 284 } 285 286 static int open_proxy_open(struct inode *inode, struct file *filp) 287 { 288 struct dentry *dentry = F_DENTRY(filp); 289 const struct file_operations *real_fops = DEBUGFS_I(inode)->real_fops; 290 int r; 291 292 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR); 293 if (r) 294 return r == -EIO ? -ENOENT : r; 295 296 r = debugfs_locked_down(inode, filp, real_fops); 297 if (r) 298 goto out; 299 300 if (!fops_get(real_fops)) { 301 #ifdef CONFIG_MODULES 302 if (real_fops->owner && 303 real_fops->owner->state == MODULE_STATE_GOING) { 304 r = -ENXIO; 305 goto out; 306 } 307 #endif 308 309 /* Huh? Module did not clean up after itself at exit? */ 310 WARN(1, "debugfs file owner did not clean up at exit: %pd", 311 dentry); 312 r = -ENXIO; 313 goto out; 314 } 315 replace_fops(filp, real_fops); 316 317 if (real_fops->open) 318 r = real_fops->open(inode, filp); 319 320 out: 321 debugfs_file_put(dentry); 322 return r; 323 } 324 325 const struct file_operations debugfs_open_proxy_file_operations = { 326 .open = open_proxy_open, 327 }; 328 329 #define PROTO(args...) args 330 #define ARGS(args...) args 331 332 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \ 333 static ret_type full_proxy_ ## name(proto) \ 334 { \ 335 struct dentry *dentry = F_DENTRY(filp); \ 336 struct debugfs_fsdata *fsd = dentry->d_fsdata; \ 337 ret_type r; \ 338 \ 339 if (!(fsd->methods & bit)) \ 340 return ret; \ 341 r = debugfs_file_get(dentry); \ 342 if (unlikely(r)) \ 343 return r; \ 344 r = fsd->real_fops->name(args); \ 345 debugfs_file_put(dentry); \ 346 return r; \ 347 } 348 349 #define SHORT_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \ 350 static ret_type short_proxy_ ## name(proto) \ 351 { \ 352 struct dentry *dentry = F_DENTRY(filp); \ 353 struct debugfs_fsdata *fsd = dentry->d_fsdata; \ 354 ret_type r; \ 355 \ 356 if (!(fsd->methods & bit)) \ 357 return ret; \ 358 r = debugfs_file_get(dentry); \ 359 if (unlikely(r)) \ 360 return r; \ 361 r = fsd->short_fops->name(args); \ 362 debugfs_file_put(dentry); \ 363 return r; \ 364 } 365 366 SHORT_PROXY_FUNC(llseek, loff_t, filp, 367 PROTO(struct file *filp, loff_t offset, int whence), 368 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); 369 370 FULL_PROXY_FUNC(llseek, loff_t, filp, 371 PROTO(struct file *filp, loff_t offset, int whence), 372 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); 373 374 SHORT_PROXY_FUNC(read, ssize_t, filp, 375 PROTO(struct file *filp, char __user *buf, size_t size, 376 loff_t *ppos), 377 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); 378 379 FULL_PROXY_FUNC(read, ssize_t, filp, 380 PROTO(struct file *filp, char __user *buf, size_t size, 381 loff_t *ppos), 382 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); 383 384 SHORT_PROXY_FUNC(write, ssize_t, filp, 385 PROTO(struct file *filp, const char __user *buf, 386 size_t size, loff_t *ppos), 387 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); 388 389 FULL_PROXY_FUNC(write, ssize_t, filp, 390 PROTO(struct file *filp, const char __user *buf, 391 size_t size, loff_t *ppos), 392 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); 393 394 FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 395 PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 396 ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY); 397 398 static __poll_t full_proxy_poll(struct file *filp, 399 struct poll_table_struct *wait) 400 { 401 struct dentry *dentry = F_DENTRY(filp); 402 struct debugfs_fsdata *fsd = dentry->d_fsdata; 403 __poll_t r = 0; 404 405 if (!(fsd->methods & HAS_POLL)) 406 return DEFAULT_POLLMASK; 407 if (debugfs_file_get(dentry)) 408 return EPOLLHUP; 409 410 r = fsd->real_fops->poll(filp, wait); 411 debugfs_file_put(dentry); 412 return r; 413 } 414 415 static int full_proxy_release(struct inode *inode, struct file *file) 416 { 417 struct debugfs_fsdata *fsd = F_DENTRY(file)->d_fsdata; 418 const struct file_operations *real_fops = fsd->real_fops; 419 int r = 0; 420 421 /* 422 * We must not protect this against removal races here: the 423 * original releaser should be called unconditionally in order 424 * not to leak any resources. Releasers must not assume that 425 * ->i_private is still being meaningful here. 426 */ 427 if (real_fops->release) 428 r = real_fops->release(inode, file); 429 430 fops_put(real_fops); 431 return r; 432 } 433 434 static int full_proxy_open_regular(struct inode *inode, struct file *filp) 435 { 436 struct dentry *dentry = F_DENTRY(filp); 437 const struct file_operations *real_fops; 438 struct debugfs_fsdata *fsd; 439 int r; 440 441 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR); 442 if (r) 443 return r == -EIO ? -ENOENT : r; 444 445 fsd = dentry->d_fsdata; 446 real_fops = fsd->real_fops; 447 r = debugfs_locked_down(inode, filp, real_fops); 448 if (r) 449 goto out; 450 451 if (!fops_get(real_fops)) { 452 #ifdef CONFIG_MODULES 453 if (real_fops->owner && 454 real_fops->owner->state == MODULE_STATE_GOING) { 455 r = -ENXIO; 456 goto out; 457 } 458 #endif 459 460 /* Huh? Module did not cleanup after itself at exit? */ 461 WARN(1, "debugfs file owner did not clean up at exit: %pd", 462 dentry); 463 r = -ENXIO; 464 goto out; 465 } 466 467 if (real_fops->open) { 468 r = real_fops->open(inode, filp); 469 if (r) { 470 fops_put(real_fops); 471 } else if (filp->f_op != &debugfs_full_proxy_file_operations) { 472 /* No protection against file removal anymore. */ 473 WARN(1, "debugfs file owner replaced proxy fops: %pd", 474 dentry); 475 fops_put(real_fops); 476 } 477 } 478 out: 479 debugfs_file_put(dentry); 480 return r; 481 } 482 483 const struct file_operations debugfs_full_proxy_file_operations = { 484 .open = full_proxy_open_regular, 485 .release = full_proxy_release, 486 .llseek = full_proxy_llseek, 487 .read = full_proxy_read, 488 .write = full_proxy_write, 489 .poll = full_proxy_poll, 490 .unlocked_ioctl = full_proxy_unlocked_ioctl 491 }; 492 493 static int full_proxy_open_short(struct inode *inode, struct file *filp) 494 { 495 struct dentry *dentry = F_DENTRY(filp); 496 int r; 497 498 r = __debugfs_file_get(dentry, DBGFS_GET_SHORT); 499 if (r) 500 return r == -EIO ? -ENOENT : r; 501 r = debugfs_locked_down(inode, filp, NULL); 502 if (!r) 503 r = simple_open(inode, filp); 504 debugfs_file_put(dentry); 505 return r; 506 } 507 508 const struct file_operations debugfs_full_short_proxy_file_operations = { 509 .open = full_proxy_open_short, 510 .llseek = short_proxy_llseek, 511 .read = short_proxy_read, 512 .write = short_proxy_write, 513 }; 514 515 ssize_t debugfs_attr_read(struct file *file, char __user *buf, 516 size_t len, loff_t *ppos) 517 { 518 struct dentry *dentry = F_DENTRY(file); 519 ssize_t ret; 520 521 ret = debugfs_file_get(dentry); 522 if (unlikely(ret)) 523 return ret; 524 ret = simple_attr_read(file, buf, len, ppos); 525 debugfs_file_put(dentry); 526 return ret; 527 } 528 EXPORT_SYMBOL_GPL(debugfs_attr_read); 529 530 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, 531 size_t len, loff_t *ppos, bool is_signed) 532 { 533 struct dentry *dentry = F_DENTRY(file); 534 ssize_t ret; 535 536 ret = debugfs_file_get(dentry); 537 if (unlikely(ret)) 538 return ret; 539 if (is_signed) 540 ret = simple_attr_write_signed(file, buf, len, ppos); 541 else 542 ret = simple_attr_write(file, buf, len, ppos); 543 debugfs_file_put(dentry); 544 return ret; 545 } 546 547 ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 548 size_t len, loff_t *ppos) 549 { 550 return debugfs_attr_write_xsigned(file, buf, len, ppos, false); 551 } 552 EXPORT_SYMBOL_GPL(debugfs_attr_write); 553 554 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 555 size_t len, loff_t *ppos) 556 { 557 return debugfs_attr_write_xsigned(file, buf, len, ppos, true); 558 } 559 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed); 560 561 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 562 struct dentry *parent, void *value, 563 const struct file_operations *fops, 564 const struct file_operations *fops_ro, 565 const struct file_operations *fops_wo) 566 { 567 /* if there are no write bits set, make read only */ 568 if (!(mode & S_IWUGO)) 569 return debugfs_create_file_unsafe(name, mode, parent, value, 570 fops_ro); 571 /* if there are no read bits set, make write only */ 572 if (!(mode & S_IRUGO)) 573 return debugfs_create_file_unsafe(name, mode, parent, value, 574 fops_wo); 575 576 return debugfs_create_file_unsafe(name, mode, parent, value, fops); 577 } 578 579 static int debugfs_u8_set(void *data, u64 val) 580 { 581 *(u8 *)data = val; 582 return 0; 583 } 584 static int debugfs_u8_get(void *data, u64 *val) 585 { 586 *val = *(u8 *)data; 587 return 0; 588 } 589 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 590 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 591 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 592 593 /** 594 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 595 * @name: a pointer to a string containing the name of the file to create. 596 * @mode: the permission that the file should have 597 * @parent: a pointer to the parent dentry for this file. This should be a 598 * directory dentry if set. If this parameter is %NULL, then the 599 * file will be created in the root of the debugfs filesystem. 600 * @value: a pointer to the variable that the file should read to and write 601 * from. 602 * 603 * This function creates a file in debugfs with the given name that 604 * contains the value of the variable @value. If the @mode variable is so 605 * set, it can be read from, and written to. 606 */ 607 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 608 u8 *value) 609 { 610 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 611 &fops_u8_ro, &fops_u8_wo); 612 } 613 EXPORT_SYMBOL_GPL(debugfs_create_u8); 614 615 static int debugfs_u16_set(void *data, u64 val) 616 { 617 *(u16 *)data = val; 618 return 0; 619 } 620 static int debugfs_u16_get(void *data, u64 *val) 621 { 622 *val = *(u16 *)data; 623 return 0; 624 } 625 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 626 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 627 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 628 629 /** 630 * debugfs_create_u16 - 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 * 639 * This function creates a file in debugfs with the given name that 640 * contains the value of the variable @value. If the @mode variable is so 641 * set, it can be read from, and written to. 642 */ 643 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 644 u16 *value) 645 { 646 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 647 &fops_u16_ro, &fops_u16_wo); 648 } 649 EXPORT_SYMBOL_GPL(debugfs_create_u16); 650 651 static int debugfs_u32_set(void *data, u64 val) 652 { 653 *(u32 *)data = val; 654 return 0; 655 } 656 static int debugfs_u32_get(void *data, u64 *val) 657 { 658 *val = *(u32 *)data; 659 return 0; 660 } 661 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 662 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 663 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 664 665 /** 666 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-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 * 675 * This function creates a file in debugfs with the given name that 676 * contains the value of the variable @value. If the @mode variable is so 677 * set, it can be read from, and written to. 678 */ 679 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 680 u32 *value) 681 { 682 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 683 &fops_u32_ro, &fops_u32_wo); 684 } 685 EXPORT_SYMBOL_GPL(debugfs_create_u32); 686 687 static int debugfs_u64_set(void *data, u64 val) 688 { 689 *(u64 *)data = val; 690 return 0; 691 } 692 693 static int debugfs_u64_get(void *data, u64 *val) 694 { 695 *val = *(u64 *)data; 696 return 0; 697 } 698 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 699 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 700 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 701 702 /** 703 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 704 * @name: a pointer to a string containing the name of the file to create. 705 * @mode: the permission that the file should have 706 * @parent: a pointer to the parent dentry for this file. This should be a 707 * directory dentry if set. If this parameter is %NULL, then the 708 * file will be created in the root of the debugfs filesystem. 709 * @value: a pointer to the variable that the file should read to and write 710 * from. 711 * 712 * This function creates a file in debugfs with the given name that 713 * contains the value of the variable @value. If the @mode variable is so 714 * set, it can be read from, and written to. 715 */ 716 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 717 u64 *value) 718 { 719 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 720 &fops_u64_ro, &fops_u64_wo); 721 } 722 EXPORT_SYMBOL_GPL(debugfs_create_u64); 723 724 static int debugfs_ulong_set(void *data, u64 val) 725 { 726 *(unsigned long *)data = val; 727 return 0; 728 } 729 730 static int debugfs_ulong_get(void *data, u64 *val) 731 { 732 *val = *(unsigned long *)data; 733 return 0; 734 } 735 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 736 "%llu\n"); 737 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 738 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 739 740 /** 741 * debugfs_create_ulong - create a debugfs file that is used to read and write 742 * an unsigned long value. 743 * @name: a pointer to a string containing the name of the file to create. 744 * @mode: the permission that the file should have 745 * @parent: a pointer to the parent dentry for this file. This should be a 746 * directory dentry if set. If this parameter is %NULL, then the 747 * file will be created in the root of the debugfs filesystem. 748 * @value: a pointer to the variable that the file should read to and write 749 * from. 750 * 751 * This function creates a file in debugfs with the given name that 752 * contains the value of the variable @value. If the @mode variable is so 753 * set, it can be read from, and written to. 754 */ 755 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 756 unsigned long *value) 757 { 758 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 759 &fops_ulong_ro, &fops_ulong_wo); 760 } 761 EXPORT_SYMBOL_GPL(debugfs_create_ulong); 762 763 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 764 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 765 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 766 767 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 768 "0x%04llx\n"); 769 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 770 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 771 772 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 773 "0x%08llx\n"); 774 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 775 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 776 777 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 778 "0x%016llx\n"); 779 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 780 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 781 782 /* 783 * 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 784 * 785 * These functions are exactly the same as the above functions (but use a hex 786 * output for the decimal challenged). For details look at the above unsigned 787 * decimal functions. 788 */ 789 790 /** 791 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 792 * @name: a pointer to a string containing the name of the file to create. 793 * @mode: the permission that the file should have 794 * @parent: a pointer to the parent dentry for this file. This should be a 795 * directory dentry if set. If this parameter is %NULL, then the 796 * file will be created in the root of the debugfs filesystem. 797 * @value: a pointer to the variable that the file should read to and write 798 * from. 799 */ 800 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 801 u8 *value) 802 { 803 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 804 &fops_x8_ro, &fops_x8_wo); 805 } 806 EXPORT_SYMBOL_GPL(debugfs_create_x8); 807 808 /** 809 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 810 * @name: a pointer to a string containing the name of the file to create. 811 * @mode: the permission that the file should have 812 * @parent: a pointer to the parent dentry for this file. This should be a 813 * directory dentry if set. If this parameter is %NULL, then the 814 * file will be created in the root of the debugfs filesystem. 815 * @value: a pointer to the variable that the file should read to and write 816 * from. 817 */ 818 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 819 u16 *value) 820 { 821 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 822 &fops_x16_ro, &fops_x16_wo); 823 } 824 EXPORT_SYMBOL_GPL(debugfs_create_x16); 825 826 /** 827 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 828 * @name: a pointer to a string containing the name of the file to create. 829 * @mode: the permission that the file should have 830 * @parent: a pointer to the parent dentry for this file. This should be a 831 * directory dentry if set. If this parameter is %NULL, then the 832 * file will be created in the root of the debugfs filesystem. 833 * @value: a pointer to the variable that the file should read to and write 834 * from. 835 */ 836 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 837 u32 *value) 838 { 839 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 840 &fops_x32_ro, &fops_x32_wo); 841 } 842 EXPORT_SYMBOL_GPL(debugfs_create_x32); 843 844 /** 845 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 846 * @name: a pointer to a string containing the name of the file to create. 847 * @mode: the permission that the file should have 848 * @parent: a pointer to the parent dentry for this file. This should be a 849 * directory dentry if set. If this parameter is %NULL, then the 850 * file will be created in the root of the debugfs filesystem. 851 * @value: a pointer to the variable that the file should read to and write 852 * from. 853 */ 854 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 855 u64 *value) 856 { 857 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 858 &fops_x64_ro, &fops_x64_wo); 859 } 860 EXPORT_SYMBOL_GPL(debugfs_create_x64); 861 862 863 static int debugfs_size_t_set(void *data, u64 val) 864 { 865 *(size_t *)data = val; 866 return 0; 867 } 868 static int debugfs_size_t_get(void *data, u64 *val) 869 { 870 *val = *(size_t *)data; 871 return 0; 872 } 873 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 874 "%llu\n"); /* %llu and %zu are more or less the same */ 875 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 876 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 877 878 /** 879 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 880 * @name: a pointer to a string containing the name of the file to create. 881 * @mode: the permission that the file should have 882 * @parent: a pointer to the parent dentry for this file. This should be a 883 * directory dentry if set. If this parameter is %NULL, then the 884 * file will be created in the root of the debugfs filesystem. 885 * @value: a pointer to the variable that the file should read to and write 886 * from. 887 */ 888 void debugfs_create_size_t(const char *name, umode_t mode, 889 struct dentry *parent, size_t *value) 890 { 891 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 892 &fops_size_t_ro, &fops_size_t_wo); 893 } 894 EXPORT_SYMBOL_GPL(debugfs_create_size_t); 895 896 static int debugfs_atomic_t_set(void *data, u64 val) 897 { 898 atomic_set((atomic_t *)data, val); 899 return 0; 900 } 901 static int debugfs_atomic_t_get(void *data, u64 *val) 902 { 903 *val = atomic_read((atomic_t *)data); 904 return 0; 905 } 906 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, 907 debugfs_atomic_t_set, "%lld\n"); 908 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 909 "%lld\n"); 910 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 911 "%lld\n"); 912 913 /** 914 * debugfs_create_atomic_t - create a debugfs file that is used to read and 915 * write an atomic_t value 916 * @name: a pointer to a string containing the name of the file to create. 917 * @mode: the permission that the file should have 918 * @parent: a pointer to the parent dentry for this file. This should be a 919 * directory dentry if set. If this parameter is %NULL, then the 920 * file will be created in the root of the debugfs filesystem. 921 * @value: a pointer to the variable that the file should read to and write 922 * from. 923 */ 924 void debugfs_create_atomic_t(const char *name, umode_t mode, 925 struct dentry *parent, atomic_t *value) 926 { 927 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 928 &fops_atomic_t_ro, &fops_atomic_t_wo); 929 } 930 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 931 932 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 933 size_t count, loff_t *ppos) 934 { 935 char buf[2]; 936 bool val; 937 int r; 938 struct dentry *dentry = F_DENTRY(file); 939 940 r = debugfs_file_get(dentry); 941 if (unlikely(r)) 942 return r; 943 val = *(bool *)file->private_data; 944 debugfs_file_put(dentry); 945 946 if (val) 947 buf[0] = 'Y'; 948 else 949 buf[0] = 'N'; 950 buf[1] = '\n'; 951 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 952 } 953 EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 954 955 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 956 size_t count, loff_t *ppos) 957 { 958 bool bv; 959 int r; 960 bool *val = file->private_data; 961 struct dentry *dentry = F_DENTRY(file); 962 963 r = kstrtobool_from_user(user_buf, count, &bv); 964 if (!r) { 965 r = debugfs_file_get(dentry); 966 if (unlikely(r)) 967 return r; 968 *val = bv; 969 debugfs_file_put(dentry); 970 } 971 972 return count; 973 } 974 EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 975 976 static const struct file_operations fops_bool = { 977 .read = debugfs_read_file_bool, 978 .write = debugfs_write_file_bool, 979 .open = simple_open, 980 .llseek = default_llseek, 981 }; 982 983 static const struct file_operations fops_bool_ro = { 984 .read = debugfs_read_file_bool, 985 .open = simple_open, 986 .llseek = default_llseek, 987 }; 988 989 static const struct file_operations fops_bool_wo = { 990 .write = debugfs_write_file_bool, 991 .open = simple_open, 992 .llseek = default_llseek, 993 }; 994 995 /** 996 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 997 * @name: a pointer to a string containing the name of the file to create. 998 * @mode: the permission that the file should have 999 * @parent: a pointer to the parent dentry for this file. This should be a 1000 * directory dentry if set. If this parameter is %NULL, then the 1001 * file will be created in the root of the debugfs filesystem. 1002 * @value: a pointer to the variable that the file should read to and write 1003 * from. 1004 * 1005 * This function creates a file in debugfs with the given name that 1006 * contains the value of the variable @value. If the @mode variable is so 1007 * set, it can be read from, and written to. 1008 */ 1009 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 1010 bool *value) 1011 { 1012 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 1013 &fops_bool_ro, &fops_bool_wo); 1014 } 1015 EXPORT_SYMBOL_GPL(debugfs_create_bool); 1016 1017 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 1018 size_t count, loff_t *ppos) 1019 { 1020 struct dentry *dentry = F_DENTRY(file); 1021 char *str, *copy = NULL; 1022 int copy_len, len; 1023 ssize_t ret; 1024 1025 ret = debugfs_file_get(dentry); 1026 if (unlikely(ret)) 1027 return ret; 1028 1029 str = *(char **)file->private_data; 1030 len = strlen(str) + 1; 1031 copy = kmalloc(len, GFP_KERNEL); 1032 if (!copy) { 1033 debugfs_file_put(dentry); 1034 return -ENOMEM; 1035 } 1036 1037 copy_len = strscpy(copy, str, len); 1038 debugfs_file_put(dentry); 1039 if (copy_len < 0) { 1040 kfree(copy); 1041 return copy_len; 1042 } 1043 1044 copy[copy_len] = '\n'; 1045 1046 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); 1047 kfree(copy); 1048 1049 return ret; 1050 } 1051 1052 static 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 1097 error: 1098 kfree(new); 1099 debugfs_file_put(dentry); 1100 return r; 1101 } 1102 1103 static 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 1110 static const struct file_operations fops_str_ro = { 1111 .read = debugfs_read_file_str, 1112 .open = simple_open, 1113 .llseek = default_llseek, 1114 }; 1115 1116 static 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. This pointer and the string it points to must not be %NULL. 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 */ 1136 void debugfs_create_str(const char *name, umode_t mode, 1137 struct dentry *parent, char **value) 1138 { 1139 if (WARN_ON(!value || !*value)) 1140 return; 1141 1142 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 1143 &fops_str_ro, &fops_str_wo); 1144 } 1145 EXPORT_SYMBOL_GPL(debugfs_create_str); 1146 1147 static ssize_t read_file_blob(struct file *file, char __user *user_buf, 1148 size_t count, loff_t *ppos) 1149 { 1150 struct debugfs_blob_wrapper *blob = file->private_data; 1151 struct dentry *dentry = F_DENTRY(file); 1152 ssize_t r; 1153 1154 r = debugfs_file_get(dentry); 1155 if (unlikely(r)) 1156 return r; 1157 r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 1158 blob->size); 1159 debugfs_file_put(dentry); 1160 return r; 1161 } 1162 1163 static ssize_t write_file_blob(struct file *file, const char __user *user_buf, 1164 size_t count, loff_t *ppos) 1165 { 1166 struct debugfs_blob_wrapper *blob = file->private_data; 1167 struct dentry *dentry = F_DENTRY(file); 1168 ssize_t r; 1169 1170 r = debugfs_file_get(dentry); 1171 if (unlikely(r)) 1172 return r; 1173 r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf, 1174 count); 1175 1176 debugfs_file_put(dentry); 1177 return r; 1178 } 1179 1180 static const struct file_operations fops_blob = { 1181 .read = read_file_blob, 1182 .write = write_file_blob, 1183 .open = simple_open, 1184 .llseek = default_llseek, 1185 }; 1186 1187 /** 1188 * debugfs_create_blob - create a debugfs file that is used to read and write 1189 * a binary blob 1190 * @name: a pointer to a string containing the name of the file to create. 1191 * @mode: the permission that the file should have 1192 * @parent: a pointer to the parent dentry for this file. This should be a 1193 * directory dentry if set. If this parameter is %NULL, then the 1194 * file will be created in the root of the debugfs filesystem. 1195 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 1196 * to the blob data and the size of the data. 1197 * 1198 * This function creates a file in debugfs with the given name that exports 1199 * @blob->data as a binary blob. If the @mode variable is so set it can be 1200 * read from and written to. 1201 * 1202 * This function will return a pointer to a dentry if it succeeds. This 1203 * pointer must be passed to the debugfs_remove() function when the file is 1204 * to be removed (no automatic cleanup happens if your module is unloaded, 1205 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 1206 * returned. 1207 * 1208 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 1209 * be returned. 1210 */ 1211 struct dentry *debugfs_create_blob(const char *name, umode_t mode, 1212 struct dentry *parent, 1213 struct debugfs_blob_wrapper *blob) 1214 { 1215 return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob); 1216 } 1217 EXPORT_SYMBOL_GPL(debugfs_create_blob); 1218 1219 static size_t u32_format_array(char *buf, size_t bufsize, 1220 u32 *array, int array_size) 1221 { 1222 size_t ret = 0; 1223 1224 while (--array_size >= 0) { 1225 size_t len; 1226 char term = array_size ? ' ' : '\n'; 1227 1228 len = snprintf(buf, bufsize, "%u%c", *array++, term); 1229 ret += len; 1230 1231 buf += len; 1232 bufsize -= len; 1233 } 1234 return ret; 1235 } 1236 1237 static int u32_array_open(struct inode *inode, struct file *file) 1238 { 1239 struct debugfs_u32_array *data = inode->i_private; 1240 int size, elements = data->n_elements; 1241 char *buf; 1242 1243 /* 1244 * Max size: 1245 * - 10 digits + ' '/'\n' = 11 bytes per number 1246 * - terminating NUL character 1247 */ 1248 size = elements*11; 1249 buf = kmalloc(size+1, GFP_KERNEL); 1250 if (!buf) 1251 return -ENOMEM; 1252 buf[size] = 0; 1253 1254 file->private_data = buf; 1255 u32_format_array(buf, size, data->array, data->n_elements); 1256 1257 return nonseekable_open(inode, file); 1258 } 1259 1260 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 1261 loff_t *ppos) 1262 { 1263 size_t size = strlen(file->private_data); 1264 1265 return simple_read_from_buffer(buf, len, ppos, 1266 file->private_data, size); 1267 } 1268 1269 static int u32_array_release(struct inode *inode, struct file *file) 1270 { 1271 kfree(file->private_data); 1272 1273 return 0; 1274 } 1275 1276 static const struct file_operations u32_array_fops = { 1277 .owner = THIS_MODULE, 1278 .open = u32_array_open, 1279 .release = u32_array_release, 1280 .read = u32_array_read, 1281 }; 1282 1283 /** 1284 * debugfs_create_u32_array - create a debugfs file that is used to read u32 1285 * array. 1286 * @name: a pointer to a string containing the name of the file to create. 1287 * @mode: the permission that the file should have. 1288 * @parent: a pointer to the parent dentry for this file. This should be a 1289 * directory dentry if set. If this parameter is %NULL, then the 1290 * file will be created in the root of the debugfs filesystem. 1291 * @array: wrapper struct containing data pointer and size of the array. 1292 * 1293 * This function creates a file in debugfs with the given name that exports 1294 * @array as data. If the @mode variable is so set it can be read from. 1295 * Writing is not supported. Seek within the file is also not supported. 1296 * Once array is created its size can not be changed. 1297 */ 1298 void debugfs_create_u32_array(const char *name, umode_t mode, 1299 struct dentry *parent, 1300 struct debugfs_u32_array *array) 1301 { 1302 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 1303 } 1304 EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 1305 1306 #ifdef CONFIG_HAS_IOMEM 1307 1308 /* 1309 * The regset32 stuff is used to print 32-bit registers using the 1310 * seq_file utilities. We offer printing a register set in an already-opened 1311 * sequential file or create a debugfs file that only prints a regset32. 1312 */ 1313 1314 /** 1315 * debugfs_print_regs32 - use seq_print to describe a set of registers 1316 * @s: the seq_file structure being used to generate output 1317 * @regs: an array if struct debugfs_reg32 structures 1318 * @nregs: the length of the above array 1319 * @base: the base address to be used in reading the registers 1320 * @prefix: a string to be prefixed to every output line 1321 * 1322 * This function outputs a text block describing the current values of 1323 * some 32-bit hardware registers. It is meant to be used within debugfs 1324 * files based on seq_file that need to show registers, intermixed with other 1325 * information. The prefix argument may be used to specify a leading string, 1326 * because some peripherals have several blocks of identical registers, 1327 * for example configuration of dma channels 1328 */ 1329 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 1330 int nregs, void __iomem *base, char *prefix) 1331 { 1332 int i; 1333 1334 for (i = 0; i < nregs; i++, regs++) { 1335 if (prefix) 1336 seq_printf(s, "%s", prefix); 1337 seq_printf(s, "%s = 0x%08x\n", regs->name, 1338 readl(base + regs->offset)); 1339 if (seq_has_overflowed(s)) 1340 break; 1341 } 1342 } 1343 EXPORT_SYMBOL_GPL(debugfs_print_regs32); 1344 1345 static int debugfs_regset32_show(struct seq_file *s, void *data) 1346 { 1347 struct debugfs_regset32 *regset = s->private; 1348 1349 if (regset->dev) 1350 pm_runtime_get_sync(regset->dev); 1351 1352 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 1353 1354 if (regset->dev) 1355 pm_runtime_put(regset->dev); 1356 1357 return 0; 1358 } 1359 1360 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32); 1361 1362 /** 1363 * debugfs_create_regset32 - create a debugfs file that returns register values 1364 * @name: a pointer to a string containing the name of the file to create. 1365 * @mode: the permission that the file should have 1366 * @parent: a pointer to the parent dentry for this file. This should be a 1367 * directory dentry if set. If this parameter is %NULL, then the 1368 * file will be created in the root of the debugfs filesystem. 1369 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 1370 * to an array of register definitions, the array size and the base 1371 * address where the register bank is to be found. 1372 * 1373 * This function creates a file in debugfs with the given name that reports 1374 * the names and values of a set of 32-bit registers. If the @mode variable 1375 * is so set it can be read from. Writing is not supported. 1376 */ 1377 void debugfs_create_regset32(const char *name, umode_t mode, 1378 struct dentry *parent, 1379 struct debugfs_regset32 *regset) 1380 { 1381 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); 1382 } 1383 EXPORT_SYMBOL_GPL(debugfs_create_regset32); 1384 1385 #endif /* CONFIG_HAS_IOMEM */ 1386 1387 struct debugfs_devm_entry { 1388 int (*read)(struct seq_file *seq, void *data); 1389 struct device *dev; 1390 }; 1391 1392 static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 1393 { 1394 struct debugfs_devm_entry *entry = inode->i_private; 1395 1396 return single_open(f, entry->read, entry->dev); 1397 } 1398 1399 static const struct file_operations debugfs_devm_entry_ops = { 1400 .owner = THIS_MODULE, 1401 .open = debugfs_devm_entry_open, 1402 .release = single_release, 1403 .read = seq_read, 1404 .llseek = seq_lseek 1405 }; 1406 1407 /** 1408 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 1409 * 1410 * @dev: device related to this debugfs file. 1411 * @name: name of the debugfs file. 1412 * @parent: a pointer to the parent dentry for this file. This should be a 1413 * directory dentry if set. If this parameter is %NULL, then the 1414 * file will be created in the root of the debugfs filesystem. 1415 * @read_fn: function pointer called to print the seq_file content. 1416 */ 1417 void debugfs_create_devm_seqfile(struct device *dev, const char *name, 1418 struct dentry *parent, 1419 int (*read_fn)(struct seq_file *s, void *data)) 1420 { 1421 struct debugfs_devm_entry *entry; 1422 1423 if (IS_ERR(parent)) 1424 return; 1425 1426 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 1427 if (!entry) 1428 return; 1429 1430 entry->read = read_fn; 1431 entry->dev = dev; 1432 1433 debugfs_create_file(name, S_IRUGO, parent, entry, 1434 &debugfs_devm_entry_ops); 1435 } 1436 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1437