blob: 736ab3c988f2717eb0d373abc58f67156ed154ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Robert P. J. Day883ce422008-04-25 08:52:51 -040012 * See Documentation/DocBook/filesystems for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/fs.h>
Alessandro Rubini1a087c62011-11-18 14:50:21 +010018#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/pagemap.h>
20#include <linux/debugfs.h>
Alessandro Rubini03e099f2011-11-21 10:01:40 +010021#include <linux/io.h>
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +053022#include <linux/slab.h>
Seth Jennings3a76e5e2013-06-03 15:33:02 -050023#include <linux/atomic.h>
Arend van Spriel98210b72014-11-09 11:31:58 +010024#include <linux/device.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010025#include <linux/srcu.h>
26
27#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static ssize_t default_read_file(struct file *file, char __user *buf,
30 size_t count, loff_t *ppos)
31{
32 return 0;
33}
34
35static 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
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010041const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .read = default_read_file,
43 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070044 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020045 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010048/**
49 * debugfs_use_file_start - mark the beginning of file data access
50 * @dentry: the dentry object whose data is being accessed.
51 * @srcu_idx: a pointer to some memory to store a SRCU index in.
52 *
53 * Up to a matching call to debugfs_use_file_finish(), any
54 * successive call into the file removing functions debugfs_remove()
55 * and debugfs_remove_recursive() will block. Since associated private
56 * file data may only get freed after a successful return of any of
57 * the removal functions, you may safely access it after a successful
58 * call to debugfs_use_file_start() without worrying about
59 * lifetime issues.
60 *
61 * If -%EIO is returned, the file has already been removed and thus,
62 * it is not safe to access any of its data. If, on the other hand,
63 * it is allowed to access the file data, zero is returned.
64 *
65 * Regardless of the return code, any call to
66 * debugfs_use_file_start() must be followed by a matching call
67 * to debugfs_use_file_finish().
68 */
69static int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
70 __acquires(&debugfs_srcu)
71{
72 *srcu_idx = srcu_read_lock(&debugfs_srcu);
73 barrier();
74 if (d_unlinked(dentry))
75 return -EIO;
76 return 0;
77}
78
79/**
80 * debugfs_use_file_finish - mark the end of file data access
81 * @srcu_idx: the SRCU index "created" by a former call to
82 * debugfs_use_file_start().
83 *
84 * Allow any ongoing concurrent call into debugfs_remove() or
85 * debugfs_remove_recursive() blocked by a former call to
86 * debugfs_use_file_start() to proceed and return to its caller.
87 */
88static void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
89{
90 srcu_read_unlock(&debugfs_srcu, srcu_idx);
91}
92
93#define F_DENTRY(filp) ((filp)->f_path.dentry)
94
95#define REAL_FOPS_DEREF(dentry) \
96 ((const struct file_operations *)(dentry)->d_fsdata)
97
98static int open_proxy_open(struct inode *inode, struct file *filp)
99{
100 const struct dentry *dentry = F_DENTRY(filp);
101 const struct file_operations *real_fops = NULL;
102 int srcu_idx, r;
103
104 r = debugfs_use_file_start(dentry, &srcu_idx);
105 if (r) {
106 r = -ENOENT;
107 goto out;
108 }
109
110 real_fops = REAL_FOPS_DEREF(dentry);
111 real_fops = fops_get(real_fops);
112 if (!real_fops) {
113 /* Huh? Module did not clean up after itself at exit? */
114 WARN(1, "debugfs file owner did not clean up at exit: %pd",
115 dentry);
116 r = -ENXIO;
117 goto out;
118 }
119 replace_fops(filp, real_fops);
120
121 if (real_fops->open)
122 r = real_fops->open(inode, filp);
123
124out:
125 fops_put(real_fops);
126 debugfs_use_file_finish(srcu_idx);
127 return r;
128}
129
130const struct file_operations debugfs_open_proxy_file_operations = {
131 .open = open_proxy_open,
132};
133
Stephen Boydb97f6792015-10-12 18:09:09 -0700134static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
135 struct dentry *parent, void *value,
136 const struct file_operations *fops,
137 const struct file_operations *fops_ro,
138 const struct file_operations *fops_wo)
139{
140 /* if there are no write bits set, make read only */
141 if (!(mode & S_IWUGO))
142 return debugfs_create_file(name, mode, parent, value, fops_ro);
143 /* if there are no read bits set, make write only */
144 if (!(mode & S_IRUGO))
145 return debugfs_create_file(name, mode, parent, value, fops_wo);
146
147 return debugfs_create_file(name, mode, parent, value, fops);
148}
149
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800150static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200151{
152 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800153 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200154}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800155static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200156{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800157 *val = *(u8 *)data;
158 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200159}
160DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400161DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
162DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700165 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @name: a pointer to a string containing the name of the file to create.
167 * @mode: the permission that the file should have
168 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700169 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * file will be created in the root of the debugfs filesystem.
171 * @value: a pointer to the variable that the file should read to and write
172 * from.
173 *
174 * This function creates a file in debugfs with the given name that
175 * contains the value of the variable @value. If the @mode variable is so
176 * set, it can be read from, and written to.
177 *
178 * This function will return a pointer to a dentry if it succeeds. This
179 * pointer must be passed to the debugfs_remove() function when the file is
180 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700181 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700183 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700185 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * code.
187 */
Al Virof4ae40a2011-07-24 04:33:43 -0400188struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct dentry *parent, u8 *value)
190{
Stephen Boydb97f6792015-10-12 18:09:09 -0700191 return debugfs_create_mode(name, mode, parent, value, &fops_u8,
192 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194EXPORT_SYMBOL_GPL(debugfs_create_u8);
195
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800196static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200197{
198 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800199 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200200}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800201static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200202{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800203 *val = *(u16 *)data;
204 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200205}
206DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400207DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
208DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700211 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * @name: a pointer to a string containing the name of the file to create.
213 * @mode: the permission that the file should have
214 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700215 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * file will be created in the root of the debugfs filesystem.
217 * @value: a pointer to the variable that the file should read to and write
218 * from.
219 *
220 * This function creates a file in debugfs with the given name that
221 * contains the value of the variable @value. If the @mode variable is so
222 * set, it can be read from, and written to.
223 *
224 * This function will return a pointer to a dentry if it succeeds. This
225 * pointer must be passed to the debugfs_remove() function when the file is
226 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700227 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700229 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700231 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * code.
233 */
Al Virof4ae40a2011-07-24 04:33:43 -0400234struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct dentry *parent, u16 *value)
236{
Stephen Boydb97f6792015-10-12 18:09:09 -0700237 return debugfs_create_mode(name, mode, parent, value, &fops_u16,
238 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240EXPORT_SYMBOL_GPL(debugfs_create_u16);
241
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800242static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200243{
244 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800245 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200246}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800247static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200248{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800249 *val = *(u32 *)data;
250 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200251}
252DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400253DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
254DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700257 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @name: a pointer to a string containing the name of the file to create.
259 * @mode: the permission that the file should have
260 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700261 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * file will be created in the root of the debugfs filesystem.
263 * @value: a pointer to the variable that the file should read to and write
264 * from.
265 *
266 * This function creates a file in debugfs with the given name that
267 * contains the value of the variable @value. If the @mode variable is so
268 * set, it can be read from, and written to.
269 *
270 * This function will return a pointer to a dentry if it succeeds. This
271 * pointer must be passed to the debugfs_remove() function when the file is
272 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700273 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700275 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700277 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * code.
279 */
Al Virof4ae40a2011-07-24 04:33:43 -0400280struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct dentry *parent, u32 *value)
282{
Stephen Boydb97f6792015-10-12 18:09:09 -0700283 return debugfs_create_mode(name, mode, parent, value, &fops_u32,
284 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286EXPORT_SYMBOL_GPL(debugfs_create_u32);
287
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800288static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000289{
290 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800291 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000292}
293
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800294static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000295{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800296 *val = *(u64 *)data;
297 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000298}
299DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400300DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
301DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000302
303/**
304 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
305 * @name: a pointer to a string containing the name of the file to create.
306 * @mode: the permission that the file should have
307 * @parent: a pointer to the parent dentry for this file. This should be a
308 * directory dentry if set. If this parameter is %NULL, then the
309 * file will be created in the root of the debugfs filesystem.
310 * @value: a pointer to the variable that the file should read to and write
311 * from.
312 *
313 * This function creates a file in debugfs with the given name that
314 * contains the value of the variable @value. If the @mode variable is so
315 * set, it can be read from, and written to.
316 *
317 * This function will return a pointer to a dentry if it succeeds. This
318 * pointer must be passed to the debugfs_remove() function when the file is
319 * to be removed (no automatic cleanup happens if your module is unloaded,
320 * you are responsible here.) If an error occurs, %NULL will be returned.
321 *
322 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
323 * returned. It is not wise to check for this value, but rather, check for
324 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
325 * code.
326 */
Al Virof4ae40a2011-07-24 04:33:43 -0400327struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000328 struct dentry *parent, u64 *value)
329{
Stephen Boydb97f6792015-10-12 18:09:09 -0700330 return debugfs_create_mode(name, mode, parent, value, &fops_u64,
331 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000332}
333EXPORT_SYMBOL_GPL(debugfs_create_u64);
334
Viresh Kumarc23fe832015-10-18 22:43:19 +0530335static int debugfs_ulong_set(void *data, u64 val)
336{
337 *(unsigned long *)data = val;
338 return 0;
339}
340
341static int debugfs_ulong_get(void *data, u64 *val)
342{
343 *val = *(unsigned long *)data;
344 return 0;
345}
346DEFINE_SIMPLE_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
347DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
348DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
349
350/**
351 * debugfs_create_ulong - create a debugfs file that is used to read and write
352 * an unsigned long value.
353 * @name: a pointer to a string containing the name of the file to create.
354 * @mode: the permission that the file should have
355 * @parent: a pointer to the parent dentry for this file. This should be a
356 * directory dentry if set. If this parameter is %NULL, then the
357 * file will be created in the root of the debugfs filesystem.
358 * @value: a pointer to the variable that the file should read to and write
359 * from.
360 *
361 * This function creates a file in debugfs with the given name that
362 * contains the value of the variable @value. If the @mode variable is so
363 * set, it can be read from, and written to.
364 *
365 * This function will return a pointer to a dentry if it succeeds. This
366 * pointer must be passed to the debugfs_remove() function when the file is
367 * to be removed (no automatic cleanup happens if your module is unloaded,
368 * you are responsible here.) If an error occurs, %NULL will be returned.
369 *
370 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
371 * returned. It is not wise to check for this value, but rather, check for
372 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
373 * code.
374 */
375struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
376 struct dentry *parent, unsigned long *value)
377{
378 return debugfs_create_mode(name, mode, parent, value, &fops_ulong,
379 &fops_ulong_ro, &fops_ulong_wo);
380}
381EXPORT_SYMBOL_GPL(debugfs_create_ulong);
382
Robin Getz2ebefc52007-08-02 18:23:50 -0400383DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400384DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
385DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400386
387DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400388DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
389DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400390
391DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400392DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
393DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400394
Huang Ying15b0bea2010-05-18 14:35:23 +0800395DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700396DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
397DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800398
Randy Dunlape6716b82007-10-15 17:30:19 -0700399/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800400 * 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
Randy Dunlape6716b82007-10-15 17:30:19 -0700401 *
402 * These functions are exactly the same as the above functions (but use a hex
403 * output for the decimal challenged). For details look at the above unsigned
404 * decimal functions.
405 */
406
Robin Getz2ebefc52007-08-02 18:23:50 -0400407/**
408 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
Randy Dunlape6716b82007-10-15 17:30:19 -0700409 * @name: a pointer to a string containing the name of the file to create.
410 * @mode: the permission that the file should have
411 * @parent: a pointer to the parent dentry for this file. This should be a
412 * directory dentry if set. If this parameter is %NULL, then the
413 * file will be created in the root of the debugfs filesystem.
414 * @value: a pointer to the variable that the file should read to and write
415 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400416 */
Al Virof4ae40a2011-07-24 04:33:43 -0400417struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400418 struct dentry *parent, u8 *value)
419{
Stephen Boydb97f6792015-10-12 18:09:09 -0700420 return debugfs_create_mode(name, mode, parent, value, &fops_x8,
421 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400422}
423EXPORT_SYMBOL_GPL(debugfs_create_x8);
424
Randy Dunlape6716b82007-10-15 17:30:19 -0700425/**
426 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
427 * @name: a pointer to a string containing the name of the file to create.
428 * @mode: the permission that the file should have
429 * @parent: a pointer to the parent dentry for this file. This should be a
430 * directory dentry if set. If this parameter is %NULL, then the
431 * file will be created in the root of the debugfs filesystem.
432 * @value: a pointer to the variable that the file should read to and write
433 * from.
434 */
Al Virof4ae40a2011-07-24 04:33:43 -0400435struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400436 struct dentry *parent, u16 *value)
437{
Stephen Boydb97f6792015-10-12 18:09:09 -0700438 return debugfs_create_mode(name, mode, parent, value, &fops_x16,
439 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400440}
441EXPORT_SYMBOL_GPL(debugfs_create_x16);
442
Randy Dunlape6716b82007-10-15 17:30:19 -0700443/**
444 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
445 * @name: a pointer to a string containing the name of the file to create.
446 * @mode: the permission that the file should have
447 * @parent: a pointer to the parent dentry for this file. This should be a
448 * directory dentry if set. If this parameter is %NULL, then the
449 * file will be created in the root of the debugfs filesystem.
450 * @value: a pointer to the variable that the file should read to and write
451 * from.
452 */
Al Virof4ae40a2011-07-24 04:33:43 -0400453struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400454 struct dentry *parent, u32 *value)
455{
Stephen Boydb97f6792015-10-12 18:09:09 -0700456 return debugfs_create_mode(name, mode, parent, value, &fops_x32,
457 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400458}
459EXPORT_SYMBOL_GPL(debugfs_create_x32);
460
Huang Ying15b0bea2010-05-18 14:35:23 +0800461/**
462 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
463 * @name: a pointer to a string containing the name of the file to create.
464 * @mode: the permission that the file should have
465 * @parent: a pointer to the parent dentry for this file. This should be a
466 * directory dentry if set. If this parameter is %NULL, then the
467 * file will be created in the root of the debugfs filesystem.
468 * @value: a pointer to the variable that the file should read to and write
469 * from.
470 */
Al Virof4ae40a2011-07-24 04:33:43 -0400471struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800472 struct dentry *parent, u64 *value)
473{
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700474 return debugfs_create_mode(name, mode, parent, value, &fops_x64,
475 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800476}
477EXPORT_SYMBOL_GPL(debugfs_create_x64);
478
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800479
480static int debugfs_size_t_set(void *data, u64 val)
481{
482 *(size_t *)data = val;
483 return 0;
484}
485static int debugfs_size_t_get(void *data, u64 *val)
486{
487 *val = *(size_t *)data;
488 return 0;
489}
490DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
491 "%llu\n"); /* %llu and %zu are more or less the same */
Stephen Boyd6db66522015-10-12 18:09:11 -0700492DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
493DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800494
495/**
496 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
497 * @name: a pointer to a string containing the name of the file to create.
498 * @mode: the permission that the file should have
499 * @parent: a pointer to the parent dentry for this file. This should be a
500 * directory dentry if set. If this parameter is %NULL, then the
501 * file will be created in the root of the debugfs filesystem.
502 * @value: a pointer to the variable that the file should read to and write
503 * from.
504 */
Al Virof4ae40a2011-07-24 04:33:43 -0400505struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800506 struct dentry *parent, size_t *value)
507{
Stephen Boyd6db66522015-10-12 18:09:11 -0700508 return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
509 &fops_size_t_ro, &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800510}
511EXPORT_SYMBOL_GPL(debugfs_create_size_t);
512
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500513static int debugfs_atomic_t_set(void *data, u64 val)
514{
515 atomic_set((atomic_t *)data, val);
516 return 0;
517}
518static int debugfs_atomic_t_get(void *data, u64 *val)
519{
520 *val = atomic_read((atomic_t *)data);
521 return 0;
522}
523DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
524 debugfs_atomic_t_set, "%lld\n");
525DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
526DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
527
528/**
529 * debugfs_create_atomic_t - create a debugfs file that is used to read and
530 * write an atomic_t value
531 * @name: a pointer to a string containing the name of the file to create.
532 * @mode: the permission that the file should have
533 * @parent: a pointer to the parent dentry for this file. This should be a
534 * directory dentry if set. If this parameter is %NULL, then the
535 * file will be created in the root of the debugfs filesystem.
536 * @value: a pointer to the variable that the file should read to and write
537 * from.
538 */
539struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
540 struct dentry *parent, atomic_t *value)
541{
Stephen Boydb97f6792015-10-12 18:09:09 -0700542 return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
543 &fops_atomic_t_ro, &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500544}
545EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800546
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100547ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
548 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 char buf[3];
Viresh Kumar621a5f72015-09-26 15:04:07 -0700551 bool *val = file->private_data;
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (*val)
554 buf[0] = 'Y';
555 else
556 buf[0] = 'N';
557 buf[1] = '\n';
558 buf[2] = 0x00;
559 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
560}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100561EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100563ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
564 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700567 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100568 bool bv;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700569 bool *val = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 buf_size = min(count, (sizeof(buf)-1));
572 if (copy_from_user(buf, user_buf, buf_size))
573 return -EFAULT;
574
Mathias Krausea3b2c8c72013-05-31 23:24:29 +0200575 buf[buf_size] = '\0';
Jonathan Cameron8705b482011-04-19 12:43:46 +0100576 if (strtobool(buf, &bv) == 0)
577 *val = bv;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return count;
580}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100581EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800583static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100584 .read = debugfs_read_file_bool,
585 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700586 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200587 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588};
589
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700590static const struct file_operations fops_bool_ro = {
591 .read = debugfs_read_file_bool,
592 .open = simple_open,
593 .llseek = default_llseek,
594};
595
596static const struct file_operations fops_bool_wo = {
597 .write = debugfs_write_file_bool,
598 .open = simple_open,
599 .llseek = default_llseek,
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700603 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * @name: a pointer to a string containing the name of the file to create.
605 * @mode: the permission that the file should have
606 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700607 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * file will be created in the root of the debugfs filesystem.
609 * @value: a pointer to the variable that the file should read to and write
610 * from.
611 *
612 * This function creates a file in debugfs with the given name that
613 * contains the value of the variable @value. If the @mode variable is so
614 * set, it can be read from, and written to.
615 *
616 * This function will return a pointer to a dentry if it succeeds. This
617 * pointer must be passed to the debugfs_remove() function when the file is
618 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700619 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700621 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700623 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * code.
625 */
Al Virof4ae40a2011-07-24 04:33:43 -0400626struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700627 struct dentry *parent, bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700629 return debugfs_create_mode(name, mode, parent, value, &fops_bool,
630 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632EXPORT_SYMBOL_GPL(debugfs_create_bool);
633
Michael Ellermandd308bc2006-03-07 21:41:59 +1100634static ssize_t read_file_blob(struct file *file, char __user *user_buf,
635 size_t count, loff_t *ppos)
636{
637 struct debugfs_blob_wrapper *blob = file->private_data;
638 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
639 blob->size);
640}
641
Arjan van de Ven00977a52007-02-12 00:55:34 -0800642static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100643 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700644 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200645 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100646};
647
648/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600649 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100650 * @name: a pointer to a string containing the name of the file to create.
651 * @mode: the permission that the file should have
652 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700653 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100654 * file will be created in the root of the debugfs filesystem.
655 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
656 * to the blob data and the size of the data.
657 *
658 * This function creates a file in debugfs with the given name that exports
659 * @blob->data as a binary blob. If the @mode variable is so set it can be
660 * read from. Writing is not supported.
661 *
662 * This function will return a pointer to a dentry if it succeeds. This
663 * pointer must be passed to the debugfs_remove() function when the file is
664 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700665 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100666 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700667 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100668 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700669 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100670 * code.
671 */
Al Virof4ae40a2011-07-24 04:33:43 -0400672struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100673 struct dentry *parent,
674 struct debugfs_blob_wrapper *blob)
675{
676 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
677}
678EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100679
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530680struct array_data {
681 void *array;
682 u32 elements;
683};
684
Linus Torvaldse05e2792012-09-21 11:48:05 -0700685static size_t u32_format_array(char *buf, size_t bufsize,
686 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530687{
688 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530689
Linus Torvaldse05e2792012-09-21 11:48:05 -0700690 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530691 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700692 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530693
Linus Torvaldse05e2792012-09-21 11:48:05 -0700694 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530695 ret += len;
696
Linus Torvaldse05e2792012-09-21 11:48:05 -0700697 buf += len;
698 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530699 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530700 return ret;
701}
702
David Rientjes36048852012-09-21 02:16:29 -0700703static int u32_array_open(struct inode *inode, struct file *file)
704{
705 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700706 int size, elements = data->elements;
707 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700708
Linus Torvaldse05e2792012-09-21 11:48:05 -0700709 /*
710 * Max size:
711 * - 10 digits + ' '/'\n' = 11 bytes per number
712 * - terminating NUL character
713 */
714 size = elements*11;
715 buf = kmalloc(size+1, GFP_KERNEL);
716 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700717 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700718 buf[size] = 0;
719
720 file->private_data = buf;
721 u32_format_array(buf, size, data->array, data->elements);
722
David Rientjes36048852012-09-21 02:16:29 -0700723 return nonseekable_open(inode, file);
724}
725
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530726static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
727 loff_t *ppos)
728{
David Rientjes36048852012-09-21 02:16:29 -0700729 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530730
731 return simple_read_from_buffer(buf, len, ppos,
732 file->private_data, size);
733}
734
735static int u32_array_release(struct inode *inode, struct file *file)
736{
737 kfree(file->private_data);
738
739 return 0;
740}
741
742static const struct file_operations u32_array_fops = {
743 .owner = THIS_MODULE,
744 .open = u32_array_open,
745 .release = u32_array_release,
746 .read = u32_array_read,
747 .llseek = no_llseek,
748};
749
750/**
751 * debugfs_create_u32_array - create a debugfs file that is used to read u32
752 * array.
753 * @name: a pointer to a string containing the name of the file to create.
754 * @mode: the permission that the file should have.
755 * @parent: a pointer to the parent dentry for this file. This should be a
756 * directory dentry if set. If this parameter is %NULL, then the
757 * file will be created in the root of the debugfs filesystem.
758 * @array: u32 array that provides data.
759 * @elements: total number of elements in the array.
760 *
761 * This function creates a file in debugfs with the given name that exports
762 * @array as data. If the @mode variable is so set it can be read from.
763 * Writing is not supported. Seek within the file is also not supported.
764 * Once array is created its size can not be changed.
765 *
766 * The function returns a pointer to dentry on success. If debugfs is not
767 * enabled in the kernel, the value -%ENODEV will be returned.
768 */
769struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
770 struct dentry *parent,
771 u32 *array, u32 elements)
772{
773 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
774
775 if (data == NULL)
776 return NULL;
777
778 data->array = array;
779 data->elements = elements;
780
781 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
782}
783EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
784
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100785#ifdef CONFIG_HAS_IOMEM
786
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100787/*
788 * The regset32 stuff is used to print 32-bit registers using the
789 * seq_file utilities. We offer printing a register set in an already-opened
790 * sequential file or create a debugfs file that only prints a regset32.
791 */
792
793/**
794 * debugfs_print_regs32 - use seq_print to describe a set of registers
795 * @s: the seq_file structure being used to generate output
796 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -0800797 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100798 * @base: the base address to be used in reading the registers
799 * @prefix: a string to be prefixed to every output line
800 *
801 * This function outputs a text block describing the current values of
802 * some 32-bit hardware registers. It is meant to be used within debugfs
803 * files based on seq_file that need to show registers, intermixed with other
804 * information. The prefix argument may be used to specify a leading string,
805 * because some peripherals have several blocks of identical registers,
806 * for example configuration of dma channels
807 */
Joe Perches97615362014-09-29 16:08:26 -0700808void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
809 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100810{
Joe Perches97615362014-09-29 16:08:26 -0700811 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100812
813 for (i = 0; i < nregs; i++, regs++) {
814 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -0700815 seq_printf(s, "%s", prefix);
816 seq_printf(s, "%s = 0x%08x\n", regs->name,
817 readl(base + regs->offset));
818 if (seq_has_overflowed(s))
819 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100820 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100821}
822EXPORT_SYMBOL_GPL(debugfs_print_regs32);
823
824static int debugfs_show_regset32(struct seq_file *s, void *data)
825{
826 struct debugfs_regset32 *regset = s->private;
827
828 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
829 return 0;
830}
831
832static int debugfs_open_regset32(struct inode *inode, struct file *file)
833{
834 return single_open(file, debugfs_show_regset32, inode->i_private);
835}
836
837static const struct file_operations fops_regset32 = {
838 .open = debugfs_open_regset32,
839 .read = seq_read,
840 .llseek = seq_lseek,
841 .release = single_release,
842};
843
844/**
845 * debugfs_create_regset32 - create a debugfs file that returns register values
846 * @name: a pointer to a string containing the name of the file to create.
847 * @mode: the permission that the file should have
848 * @parent: a pointer to the parent dentry for this file. This should be a
849 * directory dentry if set. If this parameter is %NULL, then the
850 * file will be created in the root of the debugfs filesystem.
851 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
852 * to an array of register definitions, the array size and the base
853 * address where the register bank is to be found.
854 *
855 * This function creates a file in debugfs with the given name that reports
856 * the names and values of a set of 32-bit registers. If the @mode variable
857 * is so set it can be read from. Writing is not supported.
858 *
859 * This function will return a pointer to a dentry if it succeeds. This
860 * pointer must be passed to the debugfs_remove() function when the file is
861 * to be removed (no automatic cleanup happens if your module is unloaded,
862 * you are responsible here.) If an error occurs, %NULL will be returned.
863 *
864 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
865 * returned. It is not wise to check for this value, but rather, check for
866 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
867 * code.
868 */
Al Viro88187392012-03-20 06:00:24 -0400869struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100870 struct dentry *parent,
871 struct debugfs_regset32 *regset)
872{
873 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
874}
875EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100876
877#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +0100878
879struct debugfs_devm_entry {
880 int (*read)(struct seq_file *seq, void *data);
881 struct device *dev;
882};
883
884static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
885{
886 struct debugfs_devm_entry *entry = inode->i_private;
887
888 return single_open(f, entry->read, entry->dev);
889}
890
891static const struct file_operations debugfs_devm_entry_ops = {
892 .owner = THIS_MODULE,
893 .open = debugfs_devm_entry_open,
894 .release = single_release,
895 .read = seq_read,
896 .llseek = seq_lseek
897};
898
899/**
900 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
901 *
902 * @dev: device related to this debugfs file.
903 * @name: name of the debugfs file.
904 * @parent: a pointer to the parent dentry for this file. This should be a
905 * directory dentry if set. If this parameter is %NULL, then the
906 * file will be created in the root of the debugfs filesystem.
907 * @read_fn: function pointer called to print the seq_file content.
908 */
909struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
910 struct dentry *parent,
911 int (*read_fn)(struct seq_file *s,
912 void *data))
913{
914 struct debugfs_devm_entry *entry;
915
916 if (IS_ERR(parent))
917 return ERR_PTR(-ENOENT);
918
919 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
920 if (!entry)
921 return ERR_PTR(-ENOMEM);
922
923 entry->read = read_fn;
924 entry->dev = dev;
925
926 return debugfs_create_file(name, S_IRUGO, parent, entry,
927 &debugfs_devm_entry_ops);
928}
929EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
930