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/debugfs.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/device.h>
26 static ssize_t default_read_file(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos)
32 static ssize_t default_write_file(struct file *file, const char __user *buf,
33 size_t count, loff_t *ppos)
38 const struct file_operations debugfs_file_operations = {
39 .read = default_read_file,
40 .write = default_write_file,
42 .llseek = noop_llseek,
45 static int debugfs_u8_set(void *data, u64 val)
50 static int debugfs_u8_get(void *data, u64 *val)
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");
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
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.
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.
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
83 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
84 struct dentry *parent, u8 *value)
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);
93 return debugfs_create_file(name, mode, parent, value, &fops_u8);
95 EXPORT_SYMBOL_GPL(debugfs_create_u8);
97 static int debugfs_u16_set(void *data, u64 val)
102 static int debugfs_u16_get(void *data, u64 *val)
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");
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
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.
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.
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
135 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
136 struct dentry *parent, u16 *value)
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);
145 return debugfs_create_file(name, mode, parent, value, &fops_u16);
147 EXPORT_SYMBOL_GPL(debugfs_create_u16);
149 static int debugfs_u32_set(void *data, u64 val)
154 static int debugfs_u32_get(void *data, u64 *val)
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");
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
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.
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.
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
187 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
188 struct dentry *parent, u32 *value)
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);
197 return debugfs_create_file(name, mode, parent, value, &fops_u32);
199 EXPORT_SYMBOL_GPL(debugfs_create_u32);
201 static int debugfs_u64_set(void *data, u64 val)
207 static int debugfs_u64_get(void *data, u64 *val)
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");
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
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.
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.
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
240 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
241 struct dentry *parent, u64 *value)
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);
250 return debugfs_create_file(name, mode, parent, value, &fops_u64);
252 EXPORT_SYMBOL_GPL(debugfs_create_u64);
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");
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");
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");
266 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
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
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
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
286 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
287 struct dentry *parent, u8 *value)
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);
296 return debugfs_create_file(name, mode, parent, value, &fops_x8);
298 EXPORT_SYMBOL_GPL(debugfs_create_x8);
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
310 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
311 struct dentry *parent, u16 *value)
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);
320 return debugfs_create_file(name, mode, parent, value, &fops_x16);
322 EXPORT_SYMBOL_GPL(debugfs_create_x16);
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
334 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
335 struct dentry *parent, u32 *value)
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);
344 return debugfs_create_file(name, mode, parent, value, &fops_x32);
346 EXPORT_SYMBOL_GPL(debugfs_create_x32);
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
358 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
359 struct dentry *parent, u64 *value)
361 return debugfs_create_file(name, mode, parent, value, &fops_x64);
363 EXPORT_SYMBOL_GPL(debugfs_create_x64);
366 static int debugfs_size_t_set(void *data, u64 val)
368 *(size_t *)data = val;
371 static int debugfs_size_t_get(void *data, u64 *val)
373 *val = *(size_t *)data;
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 */
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
389 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
390 struct dentry *parent, size_t *value)
392 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
394 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
396 static int debugfs_atomic_t_set(void *data, u64 val)
398 atomic_set((atomic_t *)data, val);
401 static int debugfs_atomic_t_get(void *data, u64 *val)
403 *val = atomic_read((atomic_t *)data);
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");
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
422 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
423 struct dentry *parent, atomic_t *value)
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,
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,
434 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
436 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
438 static ssize_t read_file_bool(struct file *file, char __user *user_buf,
439 size_t count, loff_t *ppos)
442 u32 *val = file->private_data;
450 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
453 static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
454 size_t count, loff_t *ppos)
459 u32 *val = file->private_data;
461 buf_size = min(count, (sizeof(buf)-1));
462 if (copy_from_user(buf, user_buf, buf_size))
465 buf[buf_size] = '\0';
466 if (strtobool(buf, &bv) == 0)
472 static const struct file_operations fops_bool = {
473 .read = read_file_bool,
474 .write = write_file_bool,
476 .llseek = default_llseek,
480 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
481 * @name: a pointer to a string containing the name of the file to create.
482 * @mode: the permission that the file should have
483 * @parent: a pointer to the parent dentry for this file. This should be a
484 * directory dentry if set. If this parameter is %NULL, then the
485 * file will be created in the root of the debugfs filesystem.
486 * @value: a pointer to the variable that the file should read to and write
489 * This function creates a file in debugfs with the given name that
490 * contains the value of the variable @value. If the @mode variable is so
491 * set, it can be read from, and written to.
493 * This function will return a pointer to a dentry if it succeeds. This
494 * pointer must be passed to the debugfs_remove() function when the file is
495 * to be removed (no automatic cleanup happens if your module is unloaded,
496 * you are responsible here.) If an error occurs, %NULL will be returned.
498 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
499 * returned. It is not wise to check for this value, but rather, check for
500 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
503 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
504 struct dentry *parent, u32 *value)
506 return debugfs_create_file(name, mode, parent, value, &fops_bool);
508 EXPORT_SYMBOL_GPL(debugfs_create_bool);
510 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
511 size_t count, loff_t *ppos)
513 struct debugfs_blob_wrapper *blob = file->private_data;
514 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
518 static const struct file_operations fops_blob = {
519 .read = read_file_blob,
521 .llseek = default_llseek,
525 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
526 * @name: a pointer to a string containing the name of the file to create.
527 * @mode: the permission that the file should have
528 * @parent: a pointer to the parent dentry for this file. This should be a
529 * directory dentry if set. If this parameter is %NULL, then the
530 * file will be created in the root of the debugfs filesystem.
531 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
532 * to the blob data and the size of the data.
534 * This function creates a file in debugfs with the given name that exports
535 * @blob->data as a binary blob. If the @mode variable is so set it can be
536 * read from. Writing is not supported.
538 * This function will return a pointer to a dentry if it succeeds. This
539 * pointer must be passed to the debugfs_remove() function when the file is
540 * to be removed (no automatic cleanup happens if your module is unloaded,
541 * you are responsible here.) If an error occurs, %NULL will be returned.
543 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
544 * returned. It is not wise to check for this value, but rather, check for
545 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
548 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
549 struct dentry *parent,
550 struct debugfs_blob_wrapper *blob)
552 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
554 EXPORT_SYMBOL_GPL(debugfs_create_blob);
561 static size_t u32_format_array(char *buf, size_t bufsize,
562 u32 *array, int array_size)
566 while (--array_size >= 0) {
568 char term = array_size ? ' ' : '\n';
570 len = snprintf(buf, bufsize, "%u%c", *array++, term);
579 static int u32_array_open(struct inode *inode, struct file *file)
581 struct array_data *data = inode->i_private;
582 int size, elements = data->elements;
587 * - 10 digits + ' '/'\n' = 11 bytes per number
588 * - terminating NUL character
591 buf = kmalloc(size+1, GFP_KERNEL);
596 file->private_data = buf;
597 u32_format_array(buf, size, data->array, data->elements);
599 return nonseekable_open(inode, file);
602 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
605 size_t size = strlen(file->private_data);
607 return simple_read_from_buffer(buf, len, ppos,
608 file->private_data, size);
611 static int u32_array_release(struct inode *inode, struct file *file)
613 kfree(file->private_data);
618 static const struct file_operations u32_array_fops = {
619 .owner = THIS_MODULE,
620 .open = u32_array_open,
621 .release = u32_array_release,
622 .read = u32_array_read,
627 * debugfs_create_u32_array - create a debugfs file that is used to read u32
629 * @name: a pointer to a string containing the name of the file to create.
630 * @mode: the permission that the file should have.
631 * @parent: a pointer to the parent dentry for this file. This should be a
632 * directory dentry if set. If this parameter is %NULL, then the
633 * file will be created in the root of the debugfs filesystem.
634 * @array: u32 array that provides data.
635 * @elements: total number of elements in the array.
637 * This function creates a file in debugfs with the given name that exports
638 * @array as data. If the @mode variable is so set it can be read from.
639 * Writing is not supported. Seek within the file is also not supported.
640 * Once array is created its size can not be changed.
642 * The function returns a pointer to dentry on success. If debugfs is not
643 * enabled in the kernel, the value -%ENODEV will be returned.
645 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
646 struct dentry *parent,
647 u32 *array, u32 elements)
649 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
655 data->elements = elements;
657 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
659 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
661 #ifdef CONFIG_HAS_IOMEM
664 * The regset32 stuff is used to print 32-bit registers using the
665 * seq_file utilities. We offer printing a register set in an already-opened
666 * sequential file or create a debugfs file that only prints a regset32.
670 * debugfs_print_regs32 - use seq_print to describe a set of registers
671 * @s: the seq_file structure being used to generate output
672 * @regs: an array if struct debugfs_reg32 structures
673 * @nregs: the length of the above array
674 * @base: the base address to be used in reading the registers
675 * @prefix: a string to be prefixed to every output line
677 * This function outputs a text block describing the current values of
678 * some 32-bit hardware registers. It is meant to be used within debugfs
679 * files based on seq_file that need to show registers, intermixed with other
680 * information. The prefix argument may be used to specify a leading string,
681 * because some peripherals have several blocks of identical registers,
682 * for example configuration of dma channels
684 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
685 int nregs, void __iomem *base, char *prefix)
689 for (i = 0; i < nregs; i++, regs++) {
691 seq_printf(s, "%s", prefix);
692 seq_printf(s, "%s = 0x%08x\n", regs->name,
693 readl(base + regs->offset));
694 if (seq_has_overflowed(s))
698 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
700 static int debugfs_show_regset32(struct seq_file *s, void *data)
702 struct debugfs_regset32 *regset = s->private;
704 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
708 static int debugfs_open_regset32(struct inode *inode, struct file *file)
710 return single_open(file, debugfs_show_regset32, inode->i_private);
713 static const struct file_operations fops_regset32 = {
714 .open = debugfs_open_regset32,
717 .release = single_release,
721 * debugfs_create_regset32 - create a debugfs file that returns register values
722 * @name: a pointer to a string containing the name of the file to create.
723 * @mode: the permission that the file should have
724 * @parent: a pointer to the parent dentry for this file. This should be a
725 * directory dentry if set. If this parameter is %NULL, then the
726 * file will be created in the root of the debugfs filesystem.
727 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
728 * to an array of register definitions, the array size and the base
729 * address where the register bank is to be found.
731 * This function creates a file in debugfs with the given name that reports
732 * the names and values of a set of 32-bit registers. If the @mode variable
733 * is so set it can be read from. Writing is not supported.
735 * This function will return a pointer to a dentry if it succeeds. This
736 * pointer must be passed to the debugfs_remove() function when the file is
737 * to be removed (no automatic cleanup happens if your module is unloaded,
738 * you are responsible here.) If an error occurs, %NULL will be returned.
740 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
741 * returned. It is not wise to check for this value, but rather, check for
742 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
745 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
746 struct dentry *parent,
747 struct debugfs_regset32 *regset)
749 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
751 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
753 #endif /* CONFIG_HAS_IOMEM */
755 struct debugfs_devm_entry {
756 int (*read)(struct seq_file *seq, void *data);
760 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
762 struct debugfs_devm_entry *entry = inode->i_private;
764 return single_open(f, entry->read, entry->dev);
767 static const struct file_operations debugfs_devm_entry_ops = {
768 .owner = THIS_MODULE,
769 .open = debugfs_devm_entry_open,
770 .release = single_release,
776 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
778 * @dev: device related to this debugfs file.
779 * @name: name of the debugfs file.
780 * @parent: a pointer to the parent dentry for this file. This should be a
781 * directory dentry if set. If this parameter is %NULL, then the
782 * file will be created in the root of the debugfs filesystem.
783 * @read_fn: function pointer called to print the seq_file content.
785 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
786 struct dentry *parent,
787 int (*read_fn)(struct seq_file *s,
790 struct debugfs_devm_entry *entry;
793 return ERR_PTR(-ENOENT);
795 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
797 return ERR_PTR(-ENOMEM);
799 entry->read = read_fn;
802 return debugfs_create_file(name, S_IRUGO, parent, entry,
803 &debugfs_devm_entry_ops);
805 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);