1 /* 2 * file.c - part of debugfs, a tiny little debug file system 3 * 4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> 5 * Copyright (C) 2004 IBM Inc. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License version 9 * 2 as published by the Free Software Foundation. 10 * 11 * debugfs is for people to use instead of /proc or /sys. 12 * See Documentation/DocBook/filesystems for more details. 13 * 14 */ 15 16 #include <linux/module.h> 17 #include <linux/fs.h> 18 #include <linux/seq_file.h> 19 #include <linux/pagemap.h> 20 #include <linux/debugfs.h> 21 #include <linux/io.h> 22 #include <linux/slab.h> 23 #include <linux/atomic.h> 24 #include <linux/device.h> 25 26 static ssize_t default_read_file(struct file *file, char __user *buf, 27 size_t count, loff_t *ppos) 28 { 29 return 0; 30 } 31 32 static ssize_t default_write_file(struct file *file, const char __user *buf, 33 size_t count, loff_t *ppos) 34 { 35 return count; 36 } 37 38 const struct file_operations debugfs_file_operations = { 39 .read = default_read_file, 40 .write = default_write_file, 41 .open = simple_open, 42 .llseek = noop_llseek, 43 }; 44 45 static int debugfs_u8_set(void *data, u64 val) 46 { 47 *(u8 *)data = val; 48 return 0; 49 } 50 static int debugfs_u8_get(void *data, u64 *val) 51 { 52 *val = *(u8 *)data; 53 return 0; 54 } 55 DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 56 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 57 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 58 59 /** 60 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 61 * @name: a pointer to a string containing the name of the file to create. 62 * @mode: the permission that the file should have 63 * @parent: a pointer to the parent dentry for this file. This should be a 64 * directory dentry if set. If this parameter is %NULL, then the 65 * file will be created in the root of the debugfs filesystem. 66 * @value: a pointer to the variable that the file should read to and write 67 * from. 68 * 69 * This function creates a file in debugfs with the given name that 70 * contains the value of the variable @value. If the @mode variable is so 71 * set, it can be read from, and written to. 72 * 73 * This function will return a pointer to a dentry if it succeeds. This 74 * pointer must be passed to the debugfs_remove() function when the file is 75 * to be removed (no automatic cleanup happens if your module is unloaded, 76 * you are responsible here.) If an error occurs, %NULL will be returned. 77 * 78 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 79 * returned. It is not wise to check for this value, but rather, check for 80 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 81 * code. 82 */ 83 struct dentry *debugfs_create_u8(const char *name, umode_t mode, 84 struct dentry *parent, u8 *value) 85 { 86 /* if there are no write bits set, make read only */ 87 if (!(mode & S_IWUGO)) 88 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); 89 /* if there are no read bits set, make write only */ 90 if (!(mode & S_IRUGO)) 91 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); 92 93 return debugfs_create_file(name, mode, parent, value, &fops_u8); 94 } 95 EXPORT_SYMBOL_GPL(debugfs_create_u8); 96 97 static int debugfs_u16_set(void *data, u64 val) 98 { 99 *(u16 *)data = val; 100 return 0; 101 } 102 static int debugfs_u16_get(void *data, u64 *val) 103 { 104 *val = *(u16 *)data; 105 return 0; 106 } 107 DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 108 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 109 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 110 111 /** 112 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 113 * @name: a pointer to a string containing the name of the file to create. 114 * @mode: the permission that the file should have 115 * @parent: a pointer to the parent dentry for this file. This should be a 116 * directory dentry if set. If this parameter is %NULL, then the 117 * file will be created in the root of the debugfs filesystem. 118 * @value: a pointer to the variable that the file should read to and write 119 * from. 120 * 121 * This function creates a file in debugfs with the given name that 122 * contains the value of the variable @value. If the @mode variable is so 123 * set, it can be read from, and written to. 124 * 125 * This function will return a pointer to a dentry if it succeeds. This 126 * pointer must be passed to the debugfs_remove() function when the file is 127 * to be removed (no automatic cleanup happens if your module is unloaded, 128 * you are responsible here.) If an error occurs, %NULL will be returned. 129 * 130 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 131 * returned. It is not wise to check for this value, but rather, check for 132 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 133 * code. 134 */ 135 struct dentry *debugfs_create_u16(const char *name, umode_t mode, 136 struct dentry *parent, u16 *value) 137 { 138 /* if there are no write bits set, make read only */ 139 if (!(mode & S_IWUGO)) 140 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); 141 /* if there are no read bits set, make write only */ 142 if (!(mode & S_IRUGO)) 143 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); 144 145 return debugfs_create_file(name, mode, parent, value, &fops_u16); 146 } 147 EXPORT_SYMBOL_GPL(debugfs_create_u16); 148 149 static int debugfs_u32_set(void *data, u64 val) 150 { 151 *(u32 *)data = val; 152 return 0; 153 } 154 static int debugfs_u32_get(void *data, u64 *val) 155 { 156 *val = *(u32 *)data; 157 return 0; 158 } 159 DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 160 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 161 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 162 163 /** 164 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 165 * @name: a pointer to a string containing the name of the file to create. 166 * @mode: the permission that the file should have 167 * @parent: a pointer to the parent dentry for this file. This should be a 168 * directory dentry if set. If this parameter is %NULL, then the 169 * file will be created in the root of the debugfs filesystem. 170 * @value: a pointer to the variable that the file should read to and write 171 * from. 172 * 173 * This function creates a file in debugfs with the given name that 174 * contains the value of the variable @value. If the @mode variable is so 175 * set, it can be read from, and written to. 176 * 177 * This function will return a pointer to a dentry if it succeeds. This 178 * pointer must be passed to the debugfs_remove() function when the file is 179 * to be removed (no automatic cleanup happens if your module is unloaded, 180 * you are responsible here.) If an error occurs, %NULL will be returned. 181 * 182 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 183 * returned. It is not wise to check for this value, but rather, check for 184 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 185 * code. 186 */ 187 struct dentry *debugfs_create_u32(const char *name, umode_t mode, 188 struct dentry *parent, u32 *value) 189 { 190 /* if there are no write bits set, make read only */ 191 if (!(mode & S_IWUGO)) 192 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); 193 /* if there are no read bits set, make write only */ 194 if (!(mode & S_IRUGO)) 195 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); 196 197 return debugfs_create_file(name, mode, parent, value, &fops_u32); 198 } 199 EXPORT_SYMBOL_GPL(debugfs_create_u32); 200 201 static int debugfs_u64_set(void *data, u64 val) 202 { 203 *(u64 *)data = val; 204 return 0; 205 } 206 207 static int debugfs_u64_get(void *data, u64 *val) 208 { 209 *val = *(u64 *)data; 210 return 0; 211 } 212 DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 213 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 214 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 215 216 /** 217 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 218 * @name: a pointer to a string containing the name of the file to create. 219 * @mode: the permission that the file should have 220 * @parent: a pointer to the parent dentry for this file. This should be a 221 * directory dentry if set. If this parameter is %NULL, then the 222 * file will be created in the root of the debugfs filesystem. 223 * @value: a pointer to the variable that the file should read to and write 224 * from. 225 * 226 * This function creates a file in debugfs with the given name that 227 * contains the value of the variable @value. If the @mode variable is so 228 * set, it can be read from, and written to. 229 * 230 * This function will return a pointer to a dentry if it succeeds. This 231 * pointer must be passed to the debugfs_remove() function when the file is 232 * to be removed (no automatic cleanup happens if your module is unloaded, 233 * you are responsible here.) If an error occurs, %NULL will be returned. 234 * 235 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 236 * returned. It is not wise to check for this value, but rather, check for 237 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 238 * code. 239 */ 240 struct dentry *debugfs_create_u64(const char *name, umode_t mode, 241 struct dentry *parent, u64 *value) 242 { 243 /* if there are no write bits set, make read only */ 244 if (!(mode & S_IWUGO)) 245 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); 246 /* if there are no read bits set, make write only */ 247 if (!(mode & S_IRUGO)) 248 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); 249 250 return debugfs_create_file(name, mode, parent, value, &fops_u64); 251 } 252 EXPORT_SYMBOL_GPL(debugfs_create_u64); 253 254 DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 255 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 256 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 257 258 DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); 259 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 260 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 261 262 DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); 263 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 264 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 265 266 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); 267 268 /* 269 * 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 270 * 271 * These functions are exactly the same as the above functions (but use a hex 272 * output for the decimal challenged). For details look at the above unsigned 273 * decimal functions. 274 */ 275 276 /** 277 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 278 * @name: a pointer to a string containing the name of the file to create. 279 * @mode: the permission that the file should have 280 * @parent: a pointer to the parent dentry for this file. This should be a 281 * directory dentry if set. If this parameter is %NULL, then the 282 * file will be created in the root of the debugfs filesystem. 283 * @value: a pointer to the variable that the file should read to and write 284 * from. 285 */ 286 struct dentry *debugfs_create_x8(const char *name, umode_t mode, 287 struct dentry *parent, u8 *value) 288 { 289 /* if there are no write bits set, make read only */ 290 if (!(mode & S_IWUGO)) 291 return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); 292 /* if there are no read bits set, make write only */ 293 if (!(mode & S_IRUGO)) 294 return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); 295 296 return debugfs_create_file(name, mode, parent, value, &fops_x8); 297 } 298 EXPORT_SYMBOL_GPL(debugfs_create_x8); 299 300 /** 301 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 302 * @name: a pointer to a string containing the name of the file to create. 303 * @mode: the permission that the file should have 304 * @parent: a pointer to the parent dentry for this file. This should be a 305 * directory dentry if set. If this parameter is %NULL, then the 306 * file will be created in the root of the debugfs filesystem. 307 * @value: a pointer to the variable that the file should read to and write 308 * from. 309 */ 310 struct dentry *debugfs_create_x16(const char *name, umode_t mode, 311 struct dentry *parent, u16 *value) 312 { 313 /* if there are no write bits set, make read only */ 314 if (!(mode & S_IWUGO)) 315 return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); 316 /* if there are no read bits set, make write only */ 317 if (!(mode & S_IRUGO)) 318 return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); 319 320 return debugfs_create_file(name, mode, parent, value, &fops_x16); 321 } 322 EXPORT_SYMBOL_GPL(debugfs_create_x16); 323 324 /** 325 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 326 * @name: a pointer to a string containing the name of the file to create. 327 * @mode: the permission that the file should have 328 * @parent: a pointer to the parent dentry for this file. This should be a 329 * directory dentry if set. If this parameter is %NULL, then the 330 * file will be created in the root of the debugfs filesystem. 331 * @value: a pointer to the variable that the file should read to and write 332 * from. 333 */ 334 struct dentry *debugfs_create_x32(const char *name, umode_t mode, 335 struct dentry *parent, u32 *value) 336 { 337 /* if there are no write bits set, make read only */ 338 if (!(mode & S_IWUGO)) 339 return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); 340 /* if there are no read bits set, make write only */ 341 if (!(mode & S_IRUGO)) 342 return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); 343 344 return debugfs_create_file(name, mode, parent, value, &fops_x32); 345 } 346 EXPORT_SYMBOL_GPL(debugfs_create_x32); 347 348 /** 349 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 350 * @name: a pointer to a string containing the name of the file to create. 351 * @mode: the permission that the file should have 352 * @parent: a pointer to the parent dentry for this file. This should be a 353 * directory dentry if set. If this parameter is %NULL, then the 354 * file will be created in the root of the debugfs filesystem. 355 * @value: a pointer to the variable that the file should read to and write 356 * from. 357 */ 358 struct dentry *debugfs_create_x64(const char *name, umode_t mode, 359 struct dentry *parent, u64 *value) 360 { 361 return debugfs_create_file(name, mode, parent, value, &fops_x64); 362 } 363 EXPORT_SYMBOL_GPL(debugfs_create_x64); 364 365 366 static int debugfs_size_t_set(void *data, u64 val) 367 { 368 *(size_t *)data = val; 369 return 0; 370 } 371 static int debugfs_size_t_get(void *data, u64 *val) 372 { 373 *val = *(size_t *)data; 374 return 0; 375 } 376 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 377 "%llu\n"); /* %llu and %zu are more or less the same */ 378 379 /** 380 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 381 * @name: a pointer to a string containing the name of the file to create. 382 * @mode: the permission that the file should have 383 * @parent: a pointer to the parent dentry for this file. This should be a 384 * directory dentry if set. If this parameter is %NULL, then the 385 * file will be created in the root of the debugfs filesystem. 386 * @value: a pointer to the variable that the file should read to and write 387 * from. 388 */ 389 struct dentry *debugfs_create_size_t(const char *name, umode_t mode, 390 struct dentry *parent, size_t *value) 391 { 392 return debugfs_create_file(name, mode, parent, value, &fops_size_t); 393 } 394 EXPORT_SYMBOL_GPL(debugfs_create_size_t); 395 396 static int debugfs_atomic_t_set(void *data, u64 val) 397 { 398 atomic_set((atomic_t *)data, val); 399 return 0; 400 } 401 static int debugfs_atomic_t_get(void *data, u64 *val) 402 { 403 *val = atomic_read((atomic_t *)data); 404 return 0; 405 } 406 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, 407 debugfs_atomic_t_set, "%lld\n"); 408 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n"); 409 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n"); 410 411 /** 412 * debugfs_create_atomic_t - create a debugfs file that is used to read and 413 * write an atomic_t value 414 * @name: a pointer to a string containing the name of the file to create. 415 * @mode: the permission that the file should have 416 * @parent: a pointer to the parent dentry for this file. This should be a 417 * directory dentry if set. If this parameter is %NULL, then the 418 * file will be created in the root of the debugfs filesystem. 419 * @value: a pointer to the variable that the file should read to and write 420 * from. 421 */ 422 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, 423 struct dentry *parent, atomic_t *value) 424 { 425 /* if there are no write bits set, make read only */ 426 if (!(mode & S_IWUGO)) 427 return debugfs_create_file(name, mode, parent, value, 428 &fops_atomic_t_ro); 429 /* if there are no read bits set, make write only */ 430 if (!(mode & S_IRUGO)) 431 return debugfs_create_file(name, mode, parent, value, 432 &fops_atomic_t_wo); 433 434 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); 435 } 436 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 437 438 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 439 size_t count, loff_t *ppos) 440 { 441 char buf[3]; 442 u32 *val = file->private_data; 443 444 if (*val) 445 buf[0] = 'Y'; 446 else 447 buf[0] = 'N'; 448 buf[1] = '\n'; 449 buf[2] = 0x00; 450 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 451 } 452 EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 453 454 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 455 size_t count, loff_t *ppos) 456 { 457 char buf[32]; 458 size_t buf_size; 459 bool bv; 460 u32 *val = file->private_data; 461 462 buf_size = min(count, (sizeof(buf)-1)); 463 if (copy_from_user(buf, user_buf, buf_size)) 464 return -EFAULT; 465 466 buf[buf_size] = '\0'; 467 if (strtobool(buf, &bv) == 0) 468 *val = bv; 469 470 return count; 471 } 472 EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 473 474 static const struct file_operations fops_bool = { 475 .read = debugfs_read_file_bool, 476 .write = debugfs_write_file_bool, 477 .open = simple_open, 478 .llseek = default_llseek, 479 }; 480 481 /** 482 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 483 * @name: a pointer to a string containing the name of the file to create. 484 * @mode: the permission that the file should have 485 * @parent: a pointer to the parent dentry for this file. This should be a 486 * directory dentry if set. If this parameter is %NULL, then the 487 * file will be created in the root of the debugfs filesystem. 488 * @value: a pointer to the variable that the file should read to and write 489 * from. 490 * 491 * This function creates a file in debugfs with the given name that 492 * contains the value of the variable @value. If the @mode variable is so 493 * set, it can be read from, and written to. 494 * 495 * This function will return a pointer to a dentry if it succeeds. This 496 * pointer must be passed to the debugfs_remove() function when the file is 497 * to be removed (no automatic cleanup happens if your module is unloaded, 498 * you are responsible here.) If an error occurs, %NULL will be returned. 499 * 500 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 501 * returned. It is not wise to check for this value, but rather, check for 502 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 503 * code. 504 */ 505 struct dentry *debugfs_create_bool(const char *name, umode_t mode, 506 struct dentry *parent, u32 *value) 507 { 508 return debugfs_create_file(name, mode, parent, value, &fops_bool); 509 } 510 EXPORT_SYMBOL_GPL(debugfs_create_bool); 511 512 static ssize_t read_file_blob(struct file *file, char __user *user_buf, 513 size_t count, loff_t *ppos) 514 { 515 struct debugfs_blob_wrapper *blob = file->private_data; 516 return simple_read_from_buffer(user_buf, count, ppos, blob->data, 517 blob->size); 518 } 519 520 static const struct file_operations fops_blob = { 521 .read = read_file_blob, 522 .open = simple_open, 523 .llseek = default_llseek, 524 }; 525 526 /** 527 * debugfs_create_blob - create a debugfs file that is used to read a binary blob 528 * @name: a pointer to a string containing the name of the file to create. 529 * @mode: the permission that the file should have 530 * @parent: a pointer to the parent dentry for this file. This should be a 531 * directory dentry if set. If this parameter is %NULL, then the 532 * file will be created in the root of the debugfs filesystem. 533 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 534 * to the blob data and the size of the data. 535 * 536 * This function creates a file in debugfs with the given name that exports 537 * @blob->data as a binary blob. If the @mode variable is so set it can be 538 * read from. Writing is not supported. 539 * 540 * This function will return a pointer to a dentry if it succeeds. This 541 * pointer must be passed to the debugfs_remove() function when the file is 542 * to be removed (no automatic cleanup happens if your module is unloaded, 543 * you are responsible here.) If an error occurs, %NULL will be returned. 544 * 545 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 546 * returned. It is not wise to check for this value, but rather, check for 547 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 548 * code. 549 */ 550 struct dentry *debugfs_create_blob(const char *name, umode_t mode, 551 struct dentry *parent, 552 struct debugfs_blob_wrapper *blob) 553 { 554 return debugfs_create_file(name, mode, parent, blob, &fops_blob); 555 } 556 EXPORT_SYMBOL_GPL(debugfs_create_blob); 557 558 struct array_data { 559 void *array; 560 u32 elements; 561 }; 562 563 static size_t u32_format_array(char *buf, size_t bufsize, 564 u32 *array, int array_size) 565 { 566 size_t ret = 0; 567 568 while (--array_size >= 0) { 569 size_t len; 570 char term = array_size ? ' ' : '\n'; 571 572 len = snprintf(buf, bufsize, "%u%c", *array++, term); 573 ret += len; 574 575 buf += len; 576 bufsize -= len; 577 } 578 return ret; 579 } 580 581 static int u32_array_open(struct inode *inode, struct file *file) 582 { 583 struct array_data *data = inode->i_private; 584 int size, elements = data->elements; 585 char *buf; 586 587 /* 588 * Max size: 589 * - 10 digits + ' '/'\n' = 11 bytes per number 590 * - terminating NUL character 591 */ 592 size = elements*11; 593 buf = kmalloc(size+1, GFP_KERNEL); 594 if (!buf) 595 return -ENOMEM; 596 buf[size] = 0; 597 598 file->private_data = buf; 599 u32_format_array(buf, size, data->array, data->elements); 600 601 return nonseekable_open(inode, file); 602 } 603 604 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 605 loff_t *ppos) 606 { 607 size_t size = strlen(file->private_data); 608 609 return simple_read_from_buffer(buf, len, ppos, 610 file->private_data, size); 611 } 612 613 static int u32_array_release(struct inode *inode, struct file *file) 614 { 615 kfree(file->private_data); 616 617 return 0; 618 } 619 620 static const struct file_operations u32_array_fops = { 621 .owner = THIS_MODULE, 622 .open = u32_array_open, 623 .release = u32_array_release, 624 .read = u32_array_read, 625 .llseek = no_llseek, 626 }; 627 628 /** 629 * debugfs_create_u32_array - create a debugfs file that is used to read u32 630 * array. 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 * @array: u32 array that provides data. 637 * @elements: total number of elements in the array. 638 * 639 * This function creates a file in debugfs with the given name that exports 640 * @array as data. If the @mode variable is so set it can be read from. 641 * Writing is not supported. Seek within the file is also not supported. 642 * Once array is created its size can not be changed. 643 * 644 * The function returns a pointer to dentry on success. If debugfs is not 645 * enabled in the kernel, the value -%ENODEV will be returned. 646 */ 647 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, 648 struct dentry *parent, 649 u32 *array, u32 elements) 650 { 651 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); 652 653 if (data == NULL) 654 return NULL; 655 656 data->array = array; 657 data->elements = elements; 658 659 return debugfs_create_file(name, mode, parent, data, &u32_array_fops); 660 } 661 EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 662 663 #ifdef CONFIG_HAS_IOMEM 664 665 /* 666 * The regset32 stuff is used to print 32-bit registers using the 667 * seq_file utilities. We offer printing a register set in an already-opened 668 * sequential file or create a debugfs file that only prints a regset32. 669 */ 670 671 /** 672 * debugfs_print_regs32 - use seq_print to describe a set of registers 673 * @s: the seq_file structure being used to generate output 674 * @regs: an array if struct debugfs_reg32 structures 675 * @nregs: the length of the above array 676 * @base: the base address to be used in reading the registers 677 * @prefix: a string to be prefixed to every output line 678 * 679 * This function outputs a text block describing the current values of 680 * some 32-bit hardware registers. It is meant to be used within debugfs 681 * files based on seq_file that need to show registers, intermixed with other 682 * information. The prefix argument may be used to specify a leading string, 683 * because some peripherals have several blocks of identical registers, 684 * for example configuration of dma channels 685 */ 686 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 687 int nregs, void __iomem *base, char *prefix) 688 { 689 int i; 690 691 for (i = 0; i < nregs; i++, regs++) { 692 if (prefix) 693 seq_printf(s, "%s", prefix); 694 seq_printf(s, "%s = 0x%08x\n", regs->name, 695 readl(base + regs->offset)); 696 if (seq_has_overflowed(s)) 697 break; 698 } 699 } 700 EXPORT_SYMBOL_GPL(debugfs_print_regs32); 701 702 static int debugfs_show_regset32(struct seq_file *s, void *data) 703 { 704 struct debugfs_regset32 *regset = s->private; 705 706 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 707 return 0; 708 } 709 710 static int debugfs_open_regset32(struct inode *inode, struct file *file) 711 { 712 return single_open(file, debugfs_show_regset32, inode->i_private); 713 } 714 715 static const struct file_operations fops_regset32 = { 716 .open = debugfs_open_regset32, 717 .read = seq_read, 718 .llseek = seq_lseek, 719 .release = single_release, 720 }; 721 722 /** 723 * debugfs_create_regset32 - create a debugfs file that returns register values 724 * @name: a pointer to a string containing the name of the file to create. 725 * @mode: the permission that the file should have 726 * @parent: a pointer to the parent dentry for this file. This should be a 727 * directory dentry if set. If this parameter is %NULL, then the 728 * file will be created in the root of the debugfs filesystem. 729 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 730 * to an array of register definitions, the array size and the base 731 * address where the register bank is to be found. 732 * 733 * This function creates a file in debugfs with the given name that reports 734 * the names and values of a set of 32-bit registers. If the @mode variable 735 * is so set it can be read from. Writing is not supported. 736 * 737 * This function will return a pointer to a dentry if it succeeds. This 738 * pointer must be passed to the debugfs_remove() function when the file is 739 * to be removed (no automatic cleanup happens if your module is unloaded, 740 * you are responsible here.) If an error occurs, %NULL will be returned. 741 * 742 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 743 * returned. It is not wise to check for this value, but rather, check for 744 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling 745 * code. 746 */ 747 struct dentry *debugfs_create_regset32(const char *name, umode_t mode, 748 struct dentry *parent, 749 struct debugfs_regset32 *regset) 750 { 751 return debugfs_create_file(name, mode, parent, regset, &fops_regset32); 752 } 753 EXPORT_SYMBOL_GPL(debugfs_create_regset32); 754 755 #endif /* CONFIG_HAS_IOMEM */ 756 757 struct debugfs_devm_entry { 758 int (*read)(struct seq_file *seq, void *data); 759 struct device *dev; 760 }; 761 762 static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 763 { 764 struct debugfs_devm_entry *entry = inode->i_private; 765 766 return single_open(f, entry->read, entry->dev); 767 } 768 769 static const struct file_operations debugfs_devm_entry_ops = { 770 .owner = THIS_MODULE, 771 .open = debugfs_devm_entry_open, 772 .release = single_release, 773 .read = seq_read, 774 .llseek = seq_lseek 775 }; 776 777 /** 778 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 779 * 780 * @dev: device related to this debugfs file. 781 * @name: name of the debugfs file. 782 * @parent: a pointer to the parent dentry for this file. This should be a 783 * directory dentry if set. If this parameter is %NULL, then the 784 * file will be created in the root of the debugfs filesystem. 785 * @read_fn: function pointer called to print the seq_file content. 786 */ 787 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, 788 struct dentry *parent, 789 int (*read_fn)(struct seq_file *s, 790 void *data)) 791 { 792 struct debugfs_devm_entry *entry; 793 794 if (IS_ERR(parent)) 795 return ERR_PTR(-ENOENT); 796 797 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 798 if (!entry) 799 return ERR_PTR(-ENOMEM); 800 801 entry->read = read_fn; 802 entry->dev = dev; 803 804 return debugfs_create_file(name, S_IRUGO, parent, entry, 805 &debugfs_devm_entry_ops); 806 } 807 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 808 809