2 * file.c - part of debugfs, a tiny little debug file system
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
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.
11 * debugfs is for people to use instead of /proc or /sys.
12 * See Documentation/DocBook/filesystems for more details.
16 #include <linux/module.h>
18 #include <linux/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/namei.h>
21 #include <linux/debugfs.h>
24 static ssize_t
default_read_file(struct file
*file
, char __user
*buf
,
25 size_t count
, loff_t
*ppos
)
30 static ssize_t
default_write_file(struct file
*file
, const char __user
*buf
,
31 size_t count
, loff_t
*ppos
)
36 const struct file_operations debugfs_file_operations
= {
37 .read
= default_read_file
,
38 .write
= default_write_file
,
40 .llseek
= noop_llseek
,
43 static void *debugfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
45 nd_set_link(nd
, dentry
->d_inode
->i_private
);
49 const struct inode_operations debugfs_link_operations
= {
50 .readlink
= generic_readlink
,
51 .follow_link
= debugfs_follow_link
,
54 static int debugfs_u8_set(void *data
, u64 val
)
59 static int debugfs_u8_get(void *data
, u64
*val
)
64 DEFINE_SIMPLE_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
65 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro
, debugfs_u8_get
, NULL
, "%llu\n");
66 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo
, NULL
, debugfs_u8_set
, "%llu\n");
69 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
70 * @name: a pointer to a string containing the name of the file to create.
71 * @mode: the permission that the file should have
72 * @parent: a pointer to the parent dentry for this file. This should be a
73 * directory dentry if set. If this parameter is %NULL, then the
74 * file will be created in the root of the debugfs filesystem.
75 * @value: a pointer to the variable that the file should read to and write
78 * This function creates a file in debugfs with the given name that
79 * contains the value of the variable @value. If the @mode variable is so
80 * set, it can be read from, and written to.
82 * This function will return a pointer to a dentry if it succeeds. This
83 * pointer must be passed to the debugfs_remove() function when the file is
84 * to be removed (no automatic cleanup happens if your module is unloaded,
85 * you are responsible here.) If an error occurs, %NULL will be returned.
87 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
88 * returned. It is not wise to check for this value, but rather, check for
89 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
92 struct dentry
*debugfs_create_u8(const char *name
, umode_t mode
,
93 struct dentry
*parent
, u8
*value
)
95 /* if there are no write bits set, make read only */
96 if (!(mode
& S_IWUGO
))
97 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u8_ro
);
98 /* if there are no read bits set, make write only */
99 if (!(mode
& S_IRUGO
))
100 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u8_wo
);
102 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u8
);
104 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
106 static int debugfs_u16_set(void *data
, u64 val
)
111 static int debugfs_u16_get(void *data
, u64
*val
)
116 DEFINE_SIMPLE_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
117 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro
, debugfs_u16_get
, NULL
, "%llu\n");
118 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo
, NULL
, debugfs_u16_set
, "%llu\n");
121 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
122 * @name: a pointer to a string containing the name of the file to create.
123 * @mode: the permission that the file should have
124 * @parent: a pointer to the parent dentry for this file. This should be a
125 * directory dentry if set. If this parameter is %NULL, then the
126 * file will be created in the root of the debugfs filesystem.
127 * @value: a pointer to the variable that the file should read to and write
130 * This function creates a file in debugfs with the given name that
131 * contains the value of the variable @value. If the @mode variable is so
132 * set, it can be read from, and written to.
134 * This function will return a pointer to a dentry if it succeeds. This
135 * pointer must be passed to the debugfs_remove() function when the file is
136 * to be removed (no automatic cleanup happens if your module is unloaded,
137 * you are responsible here.) If an error occurs, %NULL will be returned.
139 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
140 * returned. It is not wise to check for this value, but rather, check for
141 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
144 struct dentry
*debugfs_create_u16(const char *name
, umode_t mode
,
145 struct dentry
*parent
, u16
*value
)
147 /* if there are no write bits set, make read only */
148 if (!(mode
& S_IWUGO
))
149 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u16_ro
);
150 /* if there are no read bits set, make write only */
151 if (!(mode
& S_IRUGO
))
152 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u16_wo
);
154 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u16
);
156 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
158 static int debugfs_u32_set(void *data
, u64 val
)
163 static int debugfs_u32_get(void *data
, u64
*val
)
168 DEFINE_SIMPLE_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
169 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro
, debugfs_u32_get
, NULL
, "%llu\n");
170 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo
, NULL
, debugfs_u32_set
, "%llu\n");
173 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
174 * @name: a pointer to a string containing the name of the file to create.
175 * @mode: the permission that the file should have
176 * @parent: a pointer to the parent dentry for this file. This should be a
177 * directory dentry if set. If this parameter is %NULL, then the
178 * file will be created in the root of the debugfs filesystem.
179 * @value: a pointer to the variable that the file should read to and write
182 * This function creates a file in debugfs with the given name that
183 * contains the value of the variable @value. If the @mode variable is so
184 * set, it can be read from, and written to.
186 * This function will return a pointer to a dentry if it succeeds. This
187 * pointer must be passed to the debugfs_remove() function when the file is
188 * to be removed (no automatic cleanup happens if your module is unloaded,
189 * you are responsible here.) If an error occurs, %NULL will be returned.
191 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
192 * returned. It is not wise to check for this value, but rather, check for
193 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
196 struct dentry
*debugfs_create_u32(const char *name
, umode_t mode
,
197 struct dentry
*parent
, u32
*value
)
199 /* if there are no write bits set, make read only */
200 if (!(mode
& S_IWUGO
))
201 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u32_ro
);
202 /* if there are no read bits set, make write only */
203 if (!(mode
& S_IRUGO
))
204 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u32_wo
);
206 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u32
);
208 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
210 static int debugfs_u64_set(void *data
, u64 val
)
216 static int debugfs_u64_get(void *data
, u64
*val
)
221 DEFINE_SIMPLE_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
222 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro
, debugfs_u64_get
, NULL
, "%llu\n");
223 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo
, NULL
, debugfs_u64_set
, "%llu\n");
226 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
227 * @name: a pointer to a string containing the name of the file to create.
228 * @mode: the permission that the file should have
229 * @parent: a pointer to the parent dentry for this file. This should be a
230 * directory dentry if set. If this parameter is %NULL, then the
231 * file will be created in the root of the debugfs filesystem.
232 * @value: a pointer to the variable that the file should read to and write
235 * This function creates a file in debugfs with the given name that
236 * contains the value of the variable @value. If the @mode variable is so
237 * set, it can be read from, and written to.
239 * This function will return a pointer to a dentry if it succeeds. This
240 * pointer must be passed to the debugfs_remove() function when the file is
241 * to be removed (no automatic cleanup happens if your module is unloaded,
242 * you are responsible here.) If an error occurs, %NULL will be returned.
244 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
245 * returned. It is not wise to check for this value, but rather, check for
246 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
249 struct dentry
*debugfs_create_u64(const char *name
, umode_t mode
,
250 struct dentry
*parent
, u64
*value
)
252 /* if there are no write bits set, make read only */
253 if (!(mode
& S_IWUGO
))
254 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u64_ro
);
255 /* if there are no read bits set, make write only */
256 if (!(mode
& S_IRUGO
))
257 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u64_wo
);
259 return debugfs_create_file(name
, mode
, parent
, value
, &fops_u64
);
261 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
263 DEFINE_SIMPLE_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
264 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro
, debugfs_u8_get
, NULL
, "0x%02llx\n");
265 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo
, NULL
, debugfs_u8_set
, "0x%02llx\n");
267 DEFINE_SIMPLE_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
, "0x%04llx\n");
268 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro
, debugfs_u16_get
, NULL
, "0x%04llx\n");
269 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo
, NULL
, debugfs_u16_set
, "0x%04llx\n");
271 DEFINE_SIMPLE_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
, "0x%08llx\n");
272 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro
, debugfs_u32_get
, NULL
, "0x%08llx\n");
273 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo
, NULL
, debugfs_u32_set
, "0x%08llx\n");
275 DEFINE_SIMPLE_ATTRIBUTE(fops_x64
, debugfs_u64_get
, debugfs_u64_set
, "0x%016llx\n");
278 * 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
280 * These functions are exactly the same as the above functions (but use a hex
281 * output for the decimal challenged). For details look at the above unsigned
286 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
287 * @name: a pointer to a string containing the name of the file to create.
288 * @mode: the permission that the file should have
289 * @parent: a pointer to the parent dentry for this file. This should be a
290 * directory dentry if set. If this parameter is %NULL, then the
291 * file will be created in the root of the debugfs filesystem.
292 * @value: a pointer to the variable that the file should read to and write
295 struct dentry
*debugfs_create_x8(const char *name
, umode_t mode
,
296 struct dentry
*parent
, u8
*value
)
298 /* if there are no write bits set, make read only */
299 if (!(mode
& S_IWUGO
))
300 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x8_ro
);
301 /* if there are no read bits set, make write only */
302 if (!(mode
& S_IRUGO
))
303 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x8_wo
);
305 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x8
);
307 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
310 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
311 * @name: a pointer to a string containing the name of the file to create.
312 * @mode: the permission that the file should have
313 * @parent: a pointer to the parent dentry for this file. This should be a
314 * directory dentry if set. If this parameter is %NULL, then the
315 * file will be created in the root of the debugfs filesystem.
316 * @value: a pointer to the variable that the file should read to and write
319 struct dentry
*debugfs_create_x16(const char *name
, umode_t mode
,
320 struct dentry
*parent
, u16
*value
)
322 /* if there are no write bits set, make read only */
323 if (!(mode
& S_IWUGO
))
324 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x16_ro
);
325 /* if there are no read bits set, make write only */
326 if (!(mode
& S_IRUGO
))
327 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x16_wo
);
329 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x16
);
331 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
334 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
335 * @name: a pointer to a string containing the name of the file to create.
336 * @mode: the permission that the file should have
337 * @parent: a pointer to the parent dentry for this file. This should be a
338 * directory dentry if set. If this parameter is %NULL, then the
339 * file will be created in the root of the debugfs filesystem.
340 * @value: a pointer to the variable that the file should read to and write
343 struct dentry
*debugfs_create_x32(const char *name
, umode_t mode
,
344 struct dentry
*parent
, u32
*value
)
346 /* if there are no write bits set, make read only */
347 if (!(mode
& S_IWUGO
))
348 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x32_ro
);
349 /* if there are no read bits set, make write only */
350 if (!(mode
& S_IRUGO
))
351 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x32_wo
);
353 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x32
);
355 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
358 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
359 * @name: a pointer to a string containing the name of the file to create.
360 * @mode: the permission that the file should have
361 * @parent: a pointer to the parent dentry for this file. This should be a
362 * directory dentry if set. If this parameter is %NULL, then the
363 * file will be created in the root of the debugfs filesystem.
364 * @value: a pointer to the variable that the file should read to and write
367 struct dentry
*debugfs_create_x64(const char *name
, umode_t mode
,
368 struct dentry
*parent
, u64
*value
)
370 return debugfs_create_file(name
, mode
, parent
, value
, &fops_x64
);
372 EXPORT_SYMBOL_GPL(debugfs_create_x64
);
375 static int debugfs_size_t_set(void *data
, u64 val
)
377 *(size_t *)data
= val
;
380 static int debugfs_size_t_get(void *data
, u64
*val
)
382 *val
= *(size_t *)data
;
385 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
386 "%llu\n"); /* %llu and %zu are more or less the same */
389 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
390 * @name: a pointer to a string containing the name of the file to create.
391 * @mode: the permission that the file should have
392 * @parent: a pointer to the parent dentry for this file. This should be a
393 * directory dentry if set. If this parameter is %NULL, then the
394 * file will be created in the root of the debugfs filesystem.
395 * @value: a pointer to the variable that the file should read to and write
398 struct dentry
*debugfs_create_size_t(const char *name
, umode_t mode
,
399 struct dentry
*parent
, size_t *value
)
401 return debugfs_create_file(name
, mode
, parent
, value
, &fops_size_t
);
403 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
406 static ssize_t
read_file_bool(struct file
*file
, char __user
*user_buf
,
407 size_t count
, loff_t
*ppos
)
410 u32
*val
= file
->private_data
;
418 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
421 static ssize_t
write_file_bool(struct file
*file
, const char __user
*user_buf
,
422 size_t count
, loff_t
*ppos
)
427 u32
*val
= file
->private_data
;
429 buf_size
= min(count
, (sizeof(buf
)-1));
430 if (copy_from_user(buf
, user_buf
, buf_size
))
433 if (strtobool(buf
, &bv
) == 0)
439 static const struct file_operations fops_bool
= {
440 .read
= read_file_bool
,
441 .write
= write_file_bool
,
443 .llseek
= default_llseek
,
447 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
448 * @name: a pointer to a string containing the name of the file to create.
449 * @mode: the permission that the file should have
450 * @parent: a pointer to the parent dentry for this file. This should be a
451 * directory dentry if set. If this parameter is %NULL, then the
452 * file will be created in the root of the debugfs filesystem.
453 * @value: a pointer to the variable that the file should read to and write
456 * This function creates a file in debugfs with the given name that
457 * contains the value of the variable @value. If the @mode variable is so
458 * set, it can be read from, and written to.
460 * This function will return a pointer to a dentry if it succeeds. This
461 * pointer must be passed to the debugfs_remove() function when the file is
462 * to be removed (no automatic cleanup happens if your module is unloaded,
463 * you are responsible here.) If an error occurs, %NULL will be returned.
465 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
466 * returned. It is not wise to check for this value, but rather, check for
467 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
470 struct dentry
*debugfs_create_bool(const char *name
, umode_t mode
,
471 struct dentry
*parent
, u32
*value
)
473 return debugfs_create_file(name
, mode
, parent
, value
, &fops_bool
);
475 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
477 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
478 size_t count
, loff_t
*ppos
)
480 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
481 return simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
485 static const struct file_operations fops_blob
= {
486 .read
= read_file_blob
,
488 .llseek
= default_llseek
,
492 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
493 * @name: a pointer to a string containing the name of the file to create.
494 * @mode: the permission that the file should have
495 * @parent: a pointer to the parent dentry for this file. This should be a
496 * directory dentry if set. If this parameter is %NULL, then the
497 * file will be created in the root of the debugfs filesystem.
498 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
499 * to the blob data and the size of the data.
501 * This function creates a file in debugfs with the given name that exports
502 * @blob->data as a binary blob. If the @mode variable is so set it can be
503 * read from. Writing is not supported.
505 * This function will return a pointer to a dentry if it succeeds. This
506 * pointer must be passed to the debugfs_remove() function when the file is
507 * to be removed (no automatic cleanup happens if your module is unloaded,
508 * you are responsible here.) If an error occurs, %NULL will be returned.
510 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
511 * returned. It is not wise to check for this value, but rather, check for
512 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
515 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
516 struct dentry
*parent
,
517 struct debugfs_blob_wrapper
*blob
)
519 return debugfs_create_file(name
, mode
, parent
, blob
, &fops_blob
);
521 EXPORT_SYMBOL_GPL(debugfs_create_blob
);
523 #ifdef CONFIG_HAS_IOMEM
526 * The regset32 stuff is used to print 32-bit registers using the
527 * seq_file utilities. We offer printing a register set in an already-opened
528 * sequential file or create a debugfs file that only prints a regset32.
532 * debugfs_print_regs32 - use seq_print to describe a set of registers
533 * @s: the seq_file structure being used to generate output
534 * @regs: an array if struct debugfs_reg32 structures
535 * @nregs: the length of the above array
536 * @base: the base address to be used in reading the registers
537 * @prefix: a string to be prefixed to every output line
539 * This function outputs a text block describing the current values of
540 * some 32-bit hardware registers. It is meant to be used within debugfs
541 * files based on seq_file that need to show registers, intermixed with other
542 * information. The prefix argument may be used to specify a leading string,
543 * because some peripherals have several blocks of identical registers,
544 * for example configuration of dma channels
546 int debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
547 int nregs
, void __iomem
*base
, char *prefix
)
551 for (i
= 0; i
< nregs
; i
++, regs
++) {
553 ret
+= seq_printf(s
, "%s", prefix
);
554 ret
+= seq_printf(s
, "%s = 0x%08x\n", regs
->name
,
555 readl(base
+ regs
->offset
));
559 EXPORT_SYMBOL_GPL(debugfs_print_regs32
);
561 static int debugfs_show_regset32(struct seq_file
*s
, void *data
)
563 struct debugfs_regset32
*regset
= s
->private;
565 debugfs_print_regs32(s
, regset
->regs
, regset
->nregs
, regset
->base
, "");
569 static int debugfs_open_regset32(struct inode
*inode
, struct file
*file
)
571 return single_open(file
, debugfs_show_regset32
, inode
->i_private
);
574 static const struct file_operations fops_regset32
= {
575 .open
= debugfs_open_regset32
,
578 .release
= single_release
,
582 * debugfs_create_regset32 - create a debugfs file that returns register values
583 * @name: a pointer to a string containing the name of the file to create.
584 * @mode: the permission that the file should have
585 * @parent: a pointer to the parent dentry for this file. This should be a
586 * directory dentry if set. If this parameter is %NULL, then the
587 * file will be created in the root of the debugfs filesystem.
588 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
589 * to an array of register definitions, the array size and the base
590 * address where the register bank is to be found.
592 * This function creates a file in debugfs with the given name that reports
593 * the names and values of a set of 32-bit registers. If the @mode variable
594 * is so set it can be read from. Writing is not supported.
596 * This function will return a pointer to a dentry if it succeeds. This
597 * pointer must be passed to the debugfs_remove() function when the file is
598 * to be removed (no automatic cleanup happens if your module is unloaded,
599 * you are responsible here.) If an error occurs, %NULL will be returned.
601 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
602 * returned. It is not wise to check for this value, but rather, check for
603 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
606 struct dentry
*debugfs_create_regset32(const char *name
, umode_t mode
,
607 struct dentry
*parent
,
608 struct debugfs_regset32
*regset
)
610 return debugfs_create_file(name
, mode
, parent
, regset
, &fops_regset32
);
612 EXPORT_SYMBOL_GPL(debugfs_create_regset32
);
614 #endif /* CONFIG_HAS_IOMEM */