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